blob: 132f2e60bdb43f82c522249b5184237fa7c864f0 [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 Smarta5b141a2021-09-27 11:35:18 -070071static bool lpfc_pldv_detect;
James Smart7bb03bb2013-04-17 20:19:16 -040072
James Smart93a4d6f2019-11-04 16:57:05 -080073static void __lpfc_cpuhp_remove(struct lpfc_hba *phba);
74static void lpfc_cpuhp_remove(struct lpfc_hba *phba);
75static void lpfc_cpuhp_add(struct lpfc_hba *phba);
dea31012005-04-17 16:05:31 -050076static void lpfc_get_hba_model_desc(struct lpfc_hba *, uint8_t *, uint8_t *);
77static int lpfc_post_rcv_buf(struct lpfc_hba *);
James Smart5350d872011-10-10 21:33:49 -040078static int lpfc_sli4_queue_verify(struct lpfc_hba *);
James Smartda0436e2009-05-22 14:51:39 -040079static int lpfc_create_bootstrap_mbox(struct lpfc_hba *);
80static int lpfc_setup_endian_order(struct lpfc_hba *);
James Smartda0436e2009-05-22 14:51:39 -040081static void lpfc_destroy_bootstrap_mbox(struct lpfc_hba *);
James Smart8a9d2e82012-05-09 21:16:12 -040082static void lpfc_free_els_sgl_list(struct lpfc_hba *);
James Smartf358dd02017-02-12 13:52:34 -080083static void lpfc_free_nvmet_sgl_list(struct lpfc_hba *);
James Smart8a9d2e82012-05-09 21:16:12 -040084static void lpfc_init_sgl_list(struct lpfc_hba *);
James Smartda0436e2009-05-22 14:51:39 -040085static int lpfc_init_active_sgl_array(struct lpfc_hba *);
86static void lpfc_free_active_sgl(struct lpfc_hba *);
87static int lpfc_hba_down_post_s3(struct lpfc_hba *phba);
88static int lpfc_hba_down_post_s4(struct lpfc_hba *phba);
89static int lpfc_sli4_cq_event_pool_create(struct lpfc_hba *);
90static void lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *);
91static void lpfc_sli4_cq_event_release_all(struct lpfc_hba *);
James Smart618a5232012-06-12 13:54:36 -040092static void lpfc_sli4_disable_intr(struct lpfc_hba *);
93static uint32_t lpfc_sli4_enable_intr(struct lpfc_hba *, uint32_t);
James Smart1ba981f2014-02-20 09:56:45 -050094static void lpfc_sli4_oas_verify(struct lpfc_hba *phba);
James Smart6a828b02019-01-28 11:14:31 -080095static uint16_t lpfc_find_cpu_handle(struct lpfc_hba *, uint16_t, int);
James Smartaa6ff302019-05-21 17:49:09 -070096static void lpfc_setup_bg(struct lpfc_hba *, struct Scsi_Host *);
James Smart02243832021-08-16 09:28:54 -070097static int lpfc_sli4_cgn_parm_chg_evt(struct lpfc_hba *);
dea31012005-04-17 16:05:31 -050098
99static struct scsi_transport_template *lpfc_transport_template = NULL;
James Smart92d7f7b2007-06-17 19:56:38 -0500100static struct scsi_transport_template *lpfc_vport_transport_template = NULL;
dea31012005-04-17 16:05:31 -0500101static DEFINE_IDR(lpfc_hba_index);
James Smartf358dd02017-02-12 13:52:34 -0800102#define LPFC_NVMET_BUF_POST 254
Gaurav Srivastava5e633302021-06-08 10:05:49 +0530103static int lpfc_vmid_res_alloc(struct lpfc_hba *phba, struct lpfc_vport *vport);
dea31012005-04-17 16:05:31 -0500104
James Smarte59058c2008-08-24 21:49:00 -0400105/**
James Smart3621a712009-04-06 18:47:14 -0400106 * lpfc_config_port_prep - Perform lpfc initialization prior to config port
James Smarte59058c2008-08-24 21:49:00 -0400107 * @phba: pointer to lpfc hba data structure.
108 *
109 * This routine will do LPFC initialization prior to issuing the CONFIG_PORT
110 * mailbox command. It retrieves the revision information from the HBA and
111 * collects the Vital Product Data (VPD) about the HBA for preparing the
112 * configuration of the HBA.
113 *
114 * Return codes:
115 * 0 - success.
116 * -ERESTART - requests the SLI layer to reset the HBA and try again.
117 * Any other value - indicates an error.
118 **/
dea31012005-04-17 16:05:31 -0500119int
James Smart2e0fef82007-06-17 19:56:36 -0500120lpfc_config_port_prep(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500121{
122 lpfc_vpd_t *vp = &phba->vpd;
123 int i = 0, rc;
124 LPFC_MBOXQ_t *pmb;
125 MAILBOX_t *mb;
126 char *lpfc_vpd_data = NULL;
127 uint16_t offset = 0;
128 static char licensed[56] =
129 "key unlock for use with gnu public licensed code only\0";
James Smart65a29c12006-07-06 15:50:50 -0400130 static int init_key = 1;
dea31012005-04-17 16:05:31 -0500131
132 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
133 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -0500134 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500135 return -ENOMEM;
136 }
137
James Smart04c68492009-05-22 14:52:52 -0400138 mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -0500139 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -0500140
141 if (lpfc_is_LC_HBA(phba->pcidev->device)) {
James Smart65a29c12006-07-06 15:50:50 -0400142 if (init_key) {
143 uint32_t *ptext = (uint32_t *) licensed;
dea31012005-04-17 16:05:31 -0500144
James Smart65a29c12006-07-06 15:50:50 -0400145 for (i = 0; i < 56; i += sizeof (uint32_t), ptext++)
146 *ptext = cpu_to_be32(*ptext);
147 init_key = 0;
148 }
dea31012005-04-17 16:05:31 -0500149
150 lpfc_read_nv(phba, pmb);
151 memset((char*)mb->un.varRDnvp.rsvd3, 0,
152 sizeof (mb->un.varRDnvp.rsvd3));
153 memcpy((char*)mb->un.varRDnvp.rsvd3, licensed,
154 sizeof (licensed));
155
156 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
157
158 if (rc != MBX_SUCCESS) {
Dick Kennedy372c1872020-06-30 14:50:00 -0700159 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarte8b62012007-08-02 11:10:09 -0400160 "0324 Config Port initialization "
dea31012005-04-17 16:05:31 -0500161 "error, mbxCmd x%x READ_NVPARM, "
162 "mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500163 mb->mbxCommand, mb->mbxStatus);
164 mempool_free(pmb, phba->mbox_mem_pool);
165 return -ERESTART;
166 }
167 memcpy(phba->wwnn, (char *)mb->un.varRDnvp.nodename,
James Smart2e0fef82007-06-17 19:56:36 -0500168 sizeof(phba->wwnn));
169 memcpy(phba->wwpn, (char *)mb->un.varRDnvp.portname,
170 sizeof(phba->wwpn));
dea31012005-04-17 16:05:31 -0500171 }
172
Martin Wilckdfb75132018-11-12 09:58:37 +0100173 /*
174 * Clear all option bits except LPFC_SLI3_BG_ENABLED,
175 * which was already set in lpfc_get_cfgparam()
176 */
177 phba->sli3_options &= (uint32_t)LPFC_SLI3_BG_ENABLED;
James Smart92d7f7b2007-06-17 19:56:38 -0500178
dea31012005-04-17 16:05:31 -0500179 /* Setup and issue mailbox READ REV command */
180 lpfc_read_rev(phba, pmb);
181 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
182 if (rc != MBX_SUCCESS) {
Dick Kennedy372c1872020-06-30 14:50:00 -0700183 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarte8b62012007-08-02 11:10:09 -0400184 "0439 Adapter failed to init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500185 "READ_REV, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500186 mb->mbxCommand, mb->mbxStatus);
187 mempool_free( pmb, phba->mbox_mem_pool);
188 return -ERESTART;
189 }
190
James Smart92d7f7b2007-06-17 19:56:38 -0500191
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500192 /*
193 * The value of rr must be 1 since the driver set the cv field to 1.
194 * This setting requires the FW to set all revision fields.
dea31012005-04-17 16:05:31 -0500195 */
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500196 if (mb->un.varRdRev.rr == 0) {
dea31012005-04-17 16:05:31 -0500197 vp->rev.rBit = 0;
Dick Kennedy372c1872020-06-30 14:50:00 -0700198 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarte8b62012007-08-02 11:10:09 -0400199 "0440 Adapter failed to init, READ_REV has "
200 "missing revision information.\n");
dea31012005-04-17 16:05:31 -0500201 mempool_free(pmb, phba->mbox_mem_pool);
202 return -ERESTART;
dea31012005-04-17 16:05:31 -0500203 }
204
James Smart495a7142008-06-14 22:52:59 -0400205 if (phba->sli_rev == 3 && !mb->un.varRdRev.v3rsp) {
206 mempool_free(pmb, phba->mbox_mem_pool);
James Smarted957682007-06-17 19:56:37 -0500207 return -EINVAL;
James Smart495a7142008-06-14 22:52:59 -0400208 }
James Smarted957682007-06-17 19:56:37 -0500209
dea31012005-04-17 16:05:31 -0500210 /* Save information as VPD data */
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500211 vp->rev.rBit = 1;
James Smart92d7f7b2007-06-17 19:56:38 -0500212 memcpy(&vp->sli3Feat, &mb->un.varRdRev.sli3Feat, sizeof(uint32_t));
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500213 vp->rev.sli1FwRev = mb->un.varRdRev.sli1FwRev;
214 memcpy(vp->rev.sli1FwName, (char*) mb->un.varRdRev.sli1FwName, 16);
215 vp->rev.sli2FwRev = mb->un.varRdRev.sli2FwRev;
216 memcpy(vp->rev.sli2FwName, (char *) mb->un.varRdRev.sli2FwName, 16);
dea31012005-04-17 16:05:31 -0500217 vp->rev.biuRev = mb->un.varRdRev.biuRev;
218 vp->rev.smRev = mb->un.varRdRev.smRev;
219 vp->rev.smFwRev = mb->un.varRdRev.un.smFwRev;
220 vp->rev.endecRev = mb->un.varRdRev.endecRev;
221 vp->rev.fcphHigh = mb->un.varRdRev.fcphHigh;
222 vp->rev.fcphLow = mb->un.varRdRev.fcphLow;
223 vp->rev.feaLevelHigh = mb->un.varRdRev.feaLevelHigh;
224 vp->rev.feaLevelLow = mb->un.varRdRev.feaLevelLow;
225 vp->rev.postKernRev = mb->un.varRdRev.postKernRev;
226 vp->rev.opFwRev = mb->un.varRdRev.opFwRev;
227
James Smart92d7f7b2007-06-17 19:56:38 -0500228 /* If the sli feature level is less then 9, we must
229 * tear down all RPIs and VPIs on link down if NPIV
230 * is enabled.
231 */
232 if (vp->rev.feaLevelHigh < 9)
233 phba->sli3_options |= LPFC_SLI3_VPORT_TEARDOWN;
234
dea31012005-04-17 16:05:31 -0500235 if (lpfc_is_LC_HBA(phba->pcidev->device))
236 memcpy(phba->RandomData, (char *)&mb->un.varWords[24],
237 sizeof (phba->RandomData));
238
dea31012005-04-17 16:05:31 -0500239 /* Get adapter VPD information */
dea31012005-04-17 16:05:31 -0500240 lpfc_vpd_data = kmalloc(DMP_VPD_SIZE, GFP_KERNEL);
241 if (!lpfc_vpd_data)
James Smartd7c255b2008-08-24 21:50:00 -0400242 goto out_free_mbox;
dea31012005-04-17 16:05:31 -0500243 do {
James Smarta0c87cb2009-07-19 10:01:10 -0400244 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_VPD);
dea31012005-04-17 16:05:31 -0500245 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
246
247 if (rc != MBX_SUCCESS) {
248 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400249 "0441 VPD not present on adapter, "
dea31012005-04-17 16:05:31 -0500250 "mbxCmd x%x DUMP VPD, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500251 mb->mbxCommand, mb->mbxStatus);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500252 mb->un.varDmp.word_cnt = 0;
dea31012005-04-17 16:05:31 -0500253 }
James Smart04c68492009-05-22 14:52:52 -0400254 /* dump mem may return a zero when finished or we got a
255 * mailbox error, either way we are done.
256 */
257 if (mb->un.varDmp.word_cnt == 0)
258 break;
Dick Kennedyd91e3ab2020-06-30 14:49:52 -0700259
James Smarte4ec1022021-04-21 16:45:11 -0700260 if (mb->un.varDmp.word_cnt > DMP_VPD_SIZE - offset)
261 mb->un.varDmp.word_cnt = DMP_VPD_SIZE - offset;
James Smartd7c255b2008-08-24 21:50:00 -0400262 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
James Smarte4ec1022021-04-21 16:45:11 -0700263 lpfc_vpd_data + offset,
264 mb->un.varDmp.word_cnt);
265 offset += mb->un.varDmp.word_cnt;
266 } while (mb->un.varDmp.word_cnt && offset < DMP_VPD_SIZE);
Dick Kennedyd91e3ab2020-06-30 14:49:52 -0700267
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500268 lpfc_parse_vpd(phba, lpfc_vpd_data, offset);
dea31012005-04-17 16:05:31 -0500269
270 kfree(lpfc_vpd_data);
dea31012005-04-17 16:05:31 -0500271out_free_mbox:
272 mempool_free(pmb, phba->mbox_mem_pool);
273 return 0;
274}
275
James Smarte59058c2008-08-24 21:49:00 -0400276/**
James Smart3621a712009-04-06 18:47:14 -0400277 * lpfc_config_async_cmpl - Completion handler for config async event mbox cmd
James Smarte59058c2008-08-24 21:49:00 -0400278 * @phba: pointer to lpfc hba data structure.
279 * @pmboxq: pointer to the driver internal queue element for mailbox command.
280 *
281 * This is the completion handler for driver's configuring asynchronous event
282 * mailbox command to the device. If the mailbox command returns successfully,
283 * it will set internal async event support flag to 1; otherwise, it will
284 * set internal async event support flag to 0.
285 **/
James Smart57127f12007-10-27 13:37:05 -0400286static void
287lpfc_config_async_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
288{
James Smart04c68492009-05-22 14:52:52 -0400289 if (pmboxq->u.mb.mbxStatus == MBX_SUCCESS)
James Smart57127f12007-10-27 13:37:05 -0400290 phba->temp_sensor_support = 1;
291 else
292 phba->temp_sensor_support = 0;
293 mempool_free(pmboxq, phba->mbox_mem_pool);
294 return;
295}
296
James Smarte59058c2008-08-24 21:49:00 -0400297/**
James Smart3621a712009-04-06 18:47:14 -0400298 * lpfc_dump_wakeup_param_cmpl - dump memory mailbox command completion handler
James Smart97207482008-12-04 22:39:19 -0500299 * @phba: pointer to lpfc hba data structure.
300 * @pmboxq: pointer to the driver internal queue element for mailbox command.
301 *
302 * This is the completion handler for dump mailbox command for getting
303 * wake up parameters. When this command complete, the response contain
304 * Option rom version of the HBA. This function translate the version number
305 * into a human readable string and store it in OptionROMVersion.
306 **/
307static void
308lpfc_dump_wakeup_param_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
309{
310 struct prog_id *prg;
311 uint32_t prog_id_word;
312 char dist = ' ';
313 /* character array used for decoding dist type. */
314 char dist_char[] = "nabx";
315
James Smart04c68492009-05-22 14:52:52 -0400316 if (pmboxq->u.mb.mbxStatus != MBX_SUCCESS) {
James Smart9f1e1b52008-12-04 22:39:40 -0500317 mempool_free(pmboxq, phba->mbox_mem_pool);
James Smart97207482008-12-04 22:39:19 -0500318 return;
James Smart9f1e1b52008-12-04 22:39:40 -0500319 }
James Smart97207482008-12-04 22:39:19 -0500320
321 prg = (struct prog_id *) &prog_id_word;
322
323 /* word 7 contain option rom version */
James Smart04c68492009-05-22 14:52:52 -0400324 prog_id_word = pmboxq->u.mb.un.varWords[7];
James Smart97207482008-12-04 22:39:19 -0500325
326 /* Decode the Option rom version word to a readable string */
327 if (prg->dist < 4)
328 dist = dist_char[prg->dist];
329
330 if ((prg->dist == 3) && (prg->num == 0))
James Smarta2fc4aef2014-09-03 12:57:55 -0400331 snprintf(phba->OptionROMVersion, 32, "%d.%d%d",
James Smart97207482008-12-04 22:39:19 -0500332 prg->ver, prg->rev, prg->lev);
333 else
James Smarta2fc4aef2014-09-03 12:57:55 -0400334 snprintf(phba->OptionROMVersion, 32, "%d.%d%d%c%d",
James Smart97207482008-12-04 22:39:19 -0500335 prg->ver, prg->rev, prg->lev,
336 dist, prg->num);
James Smart9f1e1b52008-12-04 22:39:40 -0500337 mempool_free(pmboxq, phba->mbox_mem_pool);
James Smart97207482008-12-04 22:39:19 -0500338 return;
339}
340
341/**
James Smart05580562011-05-24 11:40:48 -0400342 * lpfc_update_vport_wwn - Updates the fc_nodename, fc_portname,
343 * cfg_soft_wwnn, cfg_soft_wwpn
344 * @vport: pointer to lpfc vport data structure.
345 *
346 *
347 * Return codes
348 * None.
349 **/
350void
351lpfc_update_vport_wwn(struct lpfc_vport *vport)
352{
James Smartaeb3c812017-04-21 16:05:02 -0700353 uint8_t vvvl = vport->fc_sparam.cmn.valid_vendor_ver_level;
354 u32 *fawwpn_key = (u32 *)&vport->fc_sparam.un.vendorVersion[0];
355
James Smart05580562011-05-24 11:40:48 -0400356 /* If the soft name exists then update it using the service params */
357 if (vport->phba->cfg_soft_wwnn)
358 u64_to_wwn(vport->phba->cfg_soft_wwnn,
359 vport->fc_sparam.nodeName.u.wwn);
360 if (vport->phba->cfg_soft_wwpn)
361 u64_to_wwn(vport->phba->cfg_soft_wwpn,
362 vport->fc_sparam.portName.u.wwn);
363
364 /*
365 * If the name is empty or there exists a soft name
366 * then copy the service params name, otherwise use the fc name
367 */
368 if (vport->fc_nodename.u.wwn[0] == 0 || vport->phba->cfg_soft_wwnn)
369 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
370 sizeof(struct lpfc_name));
371 else
372 memcpy(&vport->fc_sparam.nodeName, &vport->fc_nodename,
373 sizeof(struct lpfc_name));
374
James Smartaeb3c812017-04-21 16:05:02 -0700375 /*
376 * If the port name has changed, then set the Param changes flag
377 * to unreg the login
378 */
379 if (vport->fc_portname.u.wwn[0] != 0 &&
380 memcmp(&vport->fc_portname, &vport->fc_sparam.portName,
381 sizeof(struct lpfc_name)))
382 vport->vport_flag |= FAWWPN_PARAM_CHG;
383
384 if (vport->fc_portname.u.wwn[0] == 0 ||
385 vport->phba->cfg_soft_wwpn ||
386 (vvvl == 1 && cpu_to_be32(*fawwpn_key) == FAPWWN_KEY_VENDOR) ||
387 vport->vport_flag & FAWWPN_SET) {
James Smart05580562011-05-24 11:40:48 -0400388 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
389 sizeof(struct lpfc_name));
James Smartaeb3c812017-04-21 16:05:02 -0700390 vport->vport_flag &= ~FAWWPN_SET;
391 if (vvvl == 1 && cpu_to_be32(*fawwpn_key) == FAPWWN_KEY_VENDOR)
392 vport->vport_flag |= FAWWPN_SET;
393 }
James Smart05580562011-05-24 11:40:48 -0400394 else
395 memcpy(&vport->fc_sparam.portName, &vport->fc_portname,
396 sizeof(struct lpfc_name));
397}
398
399/**
James Smart3621a712009-04-06 18:47:14 -0400400 * lpfc_config_port_post - Perform lpfc initialization after config port
James Smarte59058c2008-08-24 21:49:00 -0400401 * @phba: pointer to lpfc hba data structure.
402 *
403 * This routine will do LPFC initialization after the CONFIG_PORT mailbox
404 * command call. It performs all internal resource and state setups on the
405 * port: post IOCB buffers, enable appropriate host interrupt attentions,
406 * ELS ring timers, etc.
407 *
408 * Return codes
409 * 0 - success.
410 * Any other value - error.
411 **/
dea31012005-04-17 16:05:31 -0500412int
James Smart2e0fef82007-06-17 19:56:36 -0500413lpfc_config_port_post(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500414{
James Smart2e0fef82007-06-17 19:56:36 -0500415 struct lpfc_vport *vport = phba->pport;
James Smarta257bf92009-04-06 18:48:10 -0400416 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -0500417 LPFC_MBOXQ_t *pmb;
418 MAILBOX_t *mb;
419 struct lpfc_dmabuf *mp;
420 struct lpfc_sli *psli = &phba->sli;
421 uint32_t status, timeout;
James Smart2e0fef82007-06-17 19:56:36 -0500422 int i, j;
423 int rc;
dea31012005-04-17 16:05:31 -0500424
James Smart7af67052007-10-27 13:38:11 -0400425 spin_lock_irq(&phba->hbalock);
426 /*
427 * If the Config port completed correctly the HBA is not
428 * over heated any more.
429 */
430 if (phba->over_temp_state == HBA_OVER_TEMP)
431 phba->over_temp_state = HBA_NORMAL_TEMP;
432 spin_unlock_irq(&phba->hbalock);
433
dea31012005-04-17 16:05:31 -0500434 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
435 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -0500436 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500437 return -ENOMEM;
438 }
James Smart04c68492009-05-22 14:52:52 -0400439 mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -0500440
dea31012005-04-17 16:05:31 -0500441 /* Get login parameters for NID. */
James Smart9f1177a2010-02-26 14:12:57 -0500442 rc = lpfc_read_sparam(phba, pmb, 0);
443 if (rc) {
444 mempool_free(pmb, phba->mbox_mem_pool);
445 return -ENOMEM;
446 }
447
James Smarted957682007-06-17 19:56:37 -0500448 pmb->vport = vport;
dea31012005-04-17 16:05:31 -0500449 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
Dick Kennedy372c1872020-06-30 14:50:00 -0700450 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarte8b62012007-08-02 11:10:09 -0400451 "0448 Adapter failed init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500452 "READ_SPARM mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500453 mb->mbxCommand, mb->mbxStatus);
James Smart2e0fef82007-06-17 19:56:36 -0500454 phba->link_state = LPFC_HBA_ERROR;
James Smart3e1f0712018-11-29 16:09:29 -0800455 mp = (struct lpfc_dmabuf *)pmb->ctx_buf;
James Smart9f1177a2010-02-26 14:12:57 -0500456 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500457 lpfc_mbuf_free(phba, mp->virt, mp->phys);
458 kfree(mp);
459 return -EIO;
460 }
461
James Smart3e1f0712018-11-29 16:09:29 -0800462 mp = (struct lpfc_dmabuf *)pmb->ctx_buf;
dea31012005-04-17 16:05:31 -0500463
James Smart2e0fef82007-06-17 19:56:36 -0500464 memcpy(&vport->fc_sparam, mp->virt, sizeof (struct serv_parm));
dea31012005-04-17 16:05:31 -0500465 lpfc_mbuf_free(phba, mp->virt, mp->phys);
466 kfree(mp);
James Smart3e1f0712018-11-29 16:09:29 -0800467 pmb->ctx_buf = NULL;
James Smart05580562011-05-24 11:40:48 -0400468 lpfc_update_vport_wwn(vport);
James Smarta257bf92009-04-06 18:48:10 -0400469
470 /* Update the fc_host data structures with new wwn. */
471 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
472 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
James Smart21e9a0a2009-05-22 14:53:21 -0400473 fc_host_max_npiv_vports(shost) = phba->max_vpi;
James Smarta257bf92009-04-06 18:48:10 -0400474
dea31012005-04-17 16:05:31 -0500475 /* If no serial number in VPD data, use low 6 bytes of WWNN */
476 /* This should be consolidated into parse_vpd ? - mr */
477 if (phba->SerialNumber[0] == 0) {
478 uint8_t *outptr;
479
James Smart2e0fef82007-06-17 19:56:36 -0500480 outptr = &vport->fc_nodename.u.s.IEEE[0];
dea31012005-04-17 16:05:31 -0500481 for (i = 0; i < 12; i++) {
482 status = *outptr++;
483 j = ((status & 0xf0) >> 4);
484 if (j <= 9)
485 phba->SerialNumber[i] =
486 (char)((uint8_t) 0x30 + (uint8_t) j);
487 else
488 phba->SerialNumber[i] =
489 (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
490 i++;
491 j = (status & 0xf);
492 if (j <= 9)
493 phba->SerialNumber[i] =
494 (char)((uint8_t) 0x30 + (uint8_t) j);
495 else
496 phba->SerialNumber[i] =
497 (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
498 }
499 }
500
dea31012005-04-17 16:05:31 -0500501 lpfc_read_config(phba, pmb);
James Smarted957682007-06-17 19:56:37 -0500502 pmb->vport = vport;
dea31012005-04-17 16:05:31 -0500503 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
Dick Kennedy372c1872020-06-30 14:50:00 -0700504 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarte8b62012007-08-02 11:10:09 -0400505 "0453 Adapter failed to init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500506 "READ_CONFIG, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500507 mb->mbxCommand, mb->mbxStatus);
James Smart2e0fef82007-06-17 19:56:36 -0500508 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500509 mempool_free( pmb, phba->mbox_mem_pool);
510 return -EIO;
511 }
512
James Smarta0c87cb2009-07-19 10:01:10 -0400513 /* Check if the port is disabled */
514 lpfc_sli_read_link_ste(phba);
515
dea31012005-04-17 16:05:31 -0500516 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
James Smartf6770e72020-01-27 16:23:09 -0800517 if (phba->cfg_hba_queue_depth > mb->un.varRdConfig.max_xri) {
James Smart572709e2013-07-15 18:32:43 -0400518 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
519 "3359 HBA queue depth changed from %d to %d\n",
James Smartf6770e72020-01-27 16:23:09 -0800520 phba->cfg_hba_queue_depth,
521 mb->un.varRdConfig.max_xri);
522 phba->cfg_hba_queue_depth = mb->un.varRdConfig.max_xri;
James Smart572709e2013-07-15 18:32:43 -0400523 }
dea31012005-04-17 16:05:31 -0500524
525 phba->lmt = mb->un.varRdConfig.lmt;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500526
527 /* Get the default values for Model Name and Description */
528 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
529
James Smart2e0fef82007-06-17 19:56:36 -0500530 phba->link_state = LPFC_LINK_DOWN;
dea31012005-04-17 16:05:31 -0500531
James Smart0b727fe2007-10-27 13:37:25 -0400532 /* Only process IOCBs on ELS ring till hba_state is READY */
James Smart895427b2017-02-12 13:52:30 -0800533 if (psli->sli3_ring[LPFC_EXTRA_RING].sli.sli3.cmdringaddr)
534 psli->sli3_ring[LPFC_EXTRA_RING].flag |= LPFC_STOP_IOCB_EVENT;
535 if (psli->sli3_ring[LPFC_FCP_RING].sli.sli3.cmdringaddr)
536 psli->sli3_ring[LPFC_FCP_RING].flag |= LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -0500537
538 /* Post receive buffers for desired rings */
James Smarted957682007-06-17 19:56:37 -0500539 if (phba->sli_rev != 3)
540 lpfc_post_rcv_buf(phba);
dea31012005-04-17 16:05:31 -0500541
James Smart93996272008-08-24 21:50:30 -0400542 /*
543 * Configure HBA MSI-X attention conditions to messages if MSI-X mode
544 */
545 if (phba->intr_type == MSIX) {
546 rc = lpfc_config_msi(phba, pmb);
547 if (rc) {
548 mempool_free(pmb, phba->mbox_mem_pool);
549 return -EIO;
550 }
551 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
552 if (rc != MBX_SUCCESS) {
Dick Kennedy372c1872020-06-30 14:50:00 -0700553 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart93996272008-08-24 21:50:30 -0400554 "0352 Config MSI mailbox command "
555 "failed, mbxCmd x%x, mbxStatus x%x\n",
James Smart04c68492009-05-22 14:52:52 -0400556 pmb->u.mb.mbxCommand,
557 pmb->u.mb.mbxStatus);
James Smart93996272008-08-24 21:50:30 -0400558 mempool_free(pmb, phba->mbox_mem_pool);
559 return -EIO;
560 }
561 }
562
James Smart04c68492009-05-22 14:52:52 -0400563 spin_lock_irq(&phba->hbalock);
James Smart93996272008-08-24 21:50:30 -0400564 /* Initialize ERATT handling flag */
565 phba->hba_flag &= ~HBA_ERATT_HANDLED;
566
dea31012005-04-17 16:05:31 -0500567 /* Enable appropriate host interrupts */
James Smart9940b972011-03-11 16:06:12 -0500568 if (lpfc_readl(phba->HCregaddr, &status)) {
569 spin_unlock_irq(&phba->hbalock);
570 return -EIO;
571 }
dea31012005-04-17 16:05:31 -0500572 status |= HC_MBINT_ENA | HC_ERINT_ENA | HC_LAINT_ENA;
573 if (psli->num_rings > 0)
574 status |= HC_R0INT_ENA;
575 if (psli->num_rings > 1)
576 status |= HC_R1INT_ENA;
577 if (psli->num_rings > 2)
578 status |= HC_R2INT_ENA;
579 if (psli->num_rings > 3)
580 status |= HC_R3INT_ENA;
581
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500582 if ((phba->cfg_poll & ENABLE_FCP_RING_POLLING) &&
583 (phba->cfg_poll & DISABLE_FCP_RING_INT))
James Smart93996272008-08-24 21:50:30 -0400584 status &= ~(HC_R0INT_ENA);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500585
dea31012005-04-17 16:05:31 -0500586 writel(status, phba->HCregaddr);
587 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -0500588 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500589
James Smart93996272008-08-24 21:50:30 -0400590 /* Set up ring-0 (ELS) timer */
591 timeout = phba->fc_ratov * 2;
James Smart256ec0d2013-04-17 20:14:58 -0400592 mod_timer(&vport->els_tmofunc,
593 jiffies + msecs_to_jiffies(1000 * timeout));
James Smart93996272008-08-24 21:50:30 -0400594 /* Set up heart beat (HB) timer */
James Smart256ec0d2013-04-17 20:14:58 -0400595 mod_timer(&phba->hb_tmofunc,
596 jiffies + msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smarta22d73b2021-01-04 10:02:38 -0800597 phba->hba_flag &= ~(HBA_HBEAT_INP | HBA_HBEAT_TMO);
James Smart858c9f62007-06-17 19:56:39 -0500598 phba->last_completion_time = jiffies;
James Smart93996272008-08-24 21:50:30 -0400599 /* Set up error attention (ERATT) polling timer */
James Smart256ec0d2013-04-17 20:14:58 -0400600 mod_timer(&phba->eratt_poll,
James Smart65791f12016-07-06 12:35:56 -0700601 jiffies + msecs_to_jiffies(1000 * phba->eratt_poll_interval));
dea31012005-04-17 16:05:31 -0500602
James Smarta0c87cb2009-07-19 10:01:10 -0400603 if (phba->hba_flag & LINK_DISABLED) {
Dick Kennedy372c1872020-06-30 14:50:00 -0700604 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
605 "2598 Adapter Link is disabled.\n");
James Smarta0c87cb2009-07-19 10:01:10 -0400606 lpfc_down_link(phba, pmb);
607 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
608 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
609 if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
Dick Kennedy372c1872020-06-30 14:50:00 -0700610 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
611 "2599 Adapter failed to issue DOWN_LINK"
612 " mbox command rc 0x%x\n", rc);
James Smarta0c87cb2009-07-19 10:01:10 -0400613
614 mempool_free(pmb, phba->mbox_mem_pool);
615 return -EIO;
616 }
James Smarte40a02c2010-02-26 14:13:54 -0500617 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
James Smart026abb82011-12-13 13:20:45 -0500618 mempool_free(pmb, phba->mbox_mem_pool);
619 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
620 if (rc)
621 return rc;
dea31012005-04-17 16:05:31 -0500622 }
623 /* MBOX buffer will be freed in mbox compl */
James Smart57127f12007-10-27 13:37:05 -0400624 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart9f1177a2010-02-26 14:12:57 -0500625 if (!pmb) {
626 phba->link_state = LPFC_HBA_ERROR;
627 return -ENOMEM;
628 }
629
James Smart57127f12007-10-27 13:37:05 -0400630 lpfc_config_async(phba, pmb, LPFC_ELS_RING);
631 pmb->mbox_cmpl = lpfc_config_async_cmpl;
632 pmb->vport = phba->pport;
633 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -0500634
James Smart57127f12007-10-27 13:37:05 -0400635 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
Dick Kennedy372c1872020-06-30 14:50:00 -0700636 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart57127f12007-10-27 13:37:05 -0400637 "0456 Adapter failed to issue "
James Smarte4e74272009-07-19 10:01:38 -0400638 "ASYNCEVT_ENABLE mbox status x%x\n",
James Smart57127f12007-10-27 13:37:05 -0400639 rc);
640 mempool_free(pmb, phba->mbox_mem_pool);
641 }
James Smart97207482008-12-04 22:39:19 -0500642
643 /* Get Option rom version */
644 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart9f1177a2010-02-26 14:12:57 -0500645 if (!pmb) {
646 phba->link_state = LPFC_HBA_ERROR;
647 return -ENOMEM;
648 }
649
James Smart97207482008-12-04 22:39:19 -0500650 lpfc_dump_wakeup_param(phba, pmb);
651 pmb->mbox_cmpl = lpfc_dump_wakeup_param_cmpl;
652 pmb->vport = phba->pport;
653 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
654
655 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
Dick Kennedy372c1872020-06-30 14:50:00 -0700656 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
657 "0435 Adapter failed "
James Smarte4e74272009-07-19 10:01:38 -0400658 "to get Option ROM version status x%x\n", rc);
James Smart97207482008-12-04 22:39:19 -0500659 mempool_free(pmb, phba->mbox_mem_pool);
660 }
661
James Smartd7c255b2008-08-24 21:50:00 -0400662 return 0;
James Smartce8b3ce2006-07-06 15:50:36 -0400663}
664
James Smarte59058c2008-08-24 21:49:00 -0400665/**
James Smart7a1dda92021-10-20 14:14:12 -0700666 * lpfc_sli4_refresh_params - update driver copy of params.
667 * @phba: Pointer to HBA context object.
668 *
669 * This is called to refresh driver copy of dynamic fields from the
670 * common_get_sli4_parameters descriptor.
671 **/
672int
673lpfc_sli4_refresh_params(struct lpfc_hba *phba)
674{
675 LPFC_MBOXQ_t *mboxq;
676 struct lpfc_mqe *mqe;
677 struct lpfc_sli4_parameters *mbx_sli4_parameters;
678 int length, rc;
679
680 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
681 if (!mboxq)
682 return -ENOMEM;
683
684 mqe = &mboxq->u.mqe;
685 /* Read the port's SLI4 Config Parameters */
686 length = (sizeof(struct lpfc_mbx_get_sli4_parameters) -
687 sizeof(struct lpfc_sli4_cfg_mhdr));
688 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
689 LPFC_MBOX_OPCODE_GET_SLI4_PARAMETERS,
690 length, LPFC_SLI4_MBX_EMBED);
691
692 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
693 if (unlikely(rc)) {
694 mempool_free(mboxq, phba->mbox_mem_pool);
695 return rc;
696 }
697 mbx_sli4_parameters = &mqe->un.get_sli4_parameters.sli4_parameters;
698 phba->sli4_hba.pc_sli4_params.mi_ver =
699 bf_get(cfg_mi_ver, mbx_sli4_parameters);
700 phba->sli4_hba.pc_sli4_params.cmf =
701 bf_get(cfg_cmf, mbx_sli4_parameters);
702 phba->sli4_hba.pc_sli4_params.pls =
703 bf_get(cfg_pvl, mbx_sli4_parameters);
704
705 mempool_free(mboxq, phba->mbox_mem_pool);
706 return rc;
707}
708
709/**
James Smart84d1b002010-02-12 14:42:33 -0500710 * lpfc_hba_init_link - Initialize the FC link
711 * @phba: pointer to lpfc hba data structure.
James Smart6e7288d2010-06-07 15:23:35 -0400712 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
James Smart84d1b002010-02-12 14:42:33 -0500713 *
714 * This routine will issue the INIT_LINK mailbox command call.
715 * It is available to other drivers through the lpfc_hba data
716 * structure for use as a delayed link up mechanism with the
717 * module parameter lpfc_suppress_link_up.
718 *
719 * Return code
720 * 0 - success
721 * Any other value - error
722 **/
Rashika Kheriae399b222014-09-03 12:55:28 -0400723static int
James Smart6e7288d2010-06-07 15:23:35 -0400724lpfc_hba_init_link(struct lpfc_hba *phba, uint32_t flag)
James Smart84d1b002010-02-12 14:42:33 -0500725{
James Smart1b511972011-12-13 13:23:09 -0500726 return lpfc_hba_init_link_fc_topology(phba, phba->cfg_topology, flag);
727}
728
729/**
730 * lpfc_hba_init_link_fc_topology - Initialize FC link with desired topology
731 * @phba: pointer to lpfc hba data structure.
732 * @fc_topology: desired fc topology.
733 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
734 *
735 * This routine will issue the INIT_LINK mailbox command call.
736 * It is available to other drivers through the lpfc_hba data
737 * structure for use as a delayed link up mechanism with the
738 * module parameter lpfc_suppress_link_up.
739 *
740 * Return code
741 * 0 - success
742 * Any other value - error
743 **/
744int
745lpfc_hba_init_link_fc_topology(struct lpfc_hba *phba, uint32_t fc_topology,
746 uint32_t flag)
747{
James Smart84d1b002010-02-12 14:42:33 -0500748 struct lpfc_vport *vport = phba->pport;
749 LPFC_MBOXQ_t *pmb;
750 MAILBOX_t *mb;
751 int rc;
752
753 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
754 if (!pmb) {
755 phba->link_state = LPFC_HBA_ERROR;
756 return -ENOMEM;
757 }
758 mb = &pmb->u.mb;
759 pmb->vport = vport;
760
James Smart026abb82011-12-13 13:20:45 -0500761 if ((phba->cfg_link_speed > LPFC_USER_LINK_SPEED_MAX) ||
762 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_1G) &&
763 !(phba->lmt & LMT_1Gb)) ||
764 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_2G) &&
765 !(phba->lmt & LMT_2Gb)) ||
766 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_4G) &&
767 !(phba->lmt & LMT_4Gb)) ||
768 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_8G) &&
769 !(phba->lmt & LMT_8Gb)) ||
770 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_10G) &&
771 !(phba->lmt & LMT_10Gb)) ||
772 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_16G) &&
James Smartd38dd522015-08-31 16:48:17 -0400773 !(phba->lmt & LMT_16Gb)) ||
774 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_32G) &&
James Smartfbd8a6b2018-02-22 08:18:45 -0800775 !(phba->lmt & LMT_32Gb)) ||
776 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_64G) &&
777 !(phba->lmt & LMT_64Gb))) {
James Smart026abb82011-12-13 13:20:45 -0500778 /* Reset link speed to auto */
Dick Kennedy372c1872020-06-30 14:50:00 -0700779 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
780 "1302 Invalid speed for this board:%d "
781 "Reset link speed to auto.\n",
782 phba->cfg_link_speed);
James Smart026abb82011-12-13 13:20:45 -0500783 phba->cfg_link_speed = LPFC_USER_LINK_SPEED_AUTO;
784 }
James Smart1b511972011-12-13 13:23:09 -0500785 lpfc_init_link(phba, pmb, fc_topology, phba->cfg_link_speed);
James Smart84d1b002010-02-12 14:42:33 -0500786 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart1b511972011-12-13 13:23:09 -0500787 if (phba->sli_rev < LPFC_SLI_REV4)
788 lpfc_set_loopback_flag(phba);
James Smart6e7288d2010-06-07 15:23:35 -0400789 rc = lpfc_sli_issue_mbox(phba, pmb, flag);
James Smart76a95d72010-11-20 23:11:48 -0500790 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
Dick Kennedy372c1872020-06-30 14:50:00 -0700791 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
792 "0498 Adapter failed to init, mbxCmd x%x "
793 "INIT_LINK, mbxStatus x%x\n",
794 mb->mbxCommand, mb->mbxStatus);
James Smart76a95d72010-11-20 23:11:48 -0500795 if (phba->sli_rev <= LPFC_SLI_REV3) {
796 /* Clear all interrupt enable conditions */
797 writel(0, phba->HCregaddr);
798 readl(phba->HCregaddr); /* flush */
799 /* Clear all pending interrupts */
800 writel(0xffffffff, phba->HAregaddr);
801 readl(phba->HAregaddr); /* flush */
802 }
James Smart84d1b002010-02-12 14:42:33 -0500803 phba->link_state = LPFC_HBA_ERROR;
James Smart6e7288d2010-06-07 15:23:35 -0400804 if (rc != MBX_BUSY || flag == MBX_POLL)
James Smart84d1b002010-02-12 14:42:33 -0500805 mempool_free(pmb, phba->mbox_mem_pool);
806 return -EIO;
807 }
James Smarte40a02c2010-02-26 14:13:54 -0500808 phba->cfg_suppress_link_up = LPFC_INITIALIZE_LINK;
James Smart6e7288d2010-06-07 15:23:35 -0400809 if (flag == MBX_POLL)
810 mempool_free(pmb, phba->mbox_mem_pool);
James Smart84d1b002010-02-12 14:42:33 -0500811
812 return 0;
813}
814
815/**
816 * lpfc_hba_down_link - this routine downs the FC link
James Smart6e7288d2010-06-07 15:23:35 -0400817 * @phba: pointer to lpfc hba data structure.
818 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
James Smart84d1b002010-02-12 14:42:33 -0500819 *
820 * This routine will issue the DOWN_LINK mailbox command call.
821 * It is available to other drivers through the lpfc_hba data
822 * structure for use to stop the link.
823 *
824 * Return code
825 * 0 - success
826 * Any other value - error
827 **/
Rashika Kheriae399b222014-09-03 12:55:28 -0400828static int
James Smart6e7288d2010-06-07 15:23:35 -0400829lpfc_hba_down_link(struct lpfc_hba *phba, uint32_t flag)
James Smart84d1b002010-02-12 14:42:33 -0500830{
831 LPFC_MBOXQ_t *pmb;
832 int rc;
833
834 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
835 if (!pmb) {
836 phba->link_state = LPFC_HBA_ERROR;
837 return -ENOMEM;
838 }
839
Dick Kennedy372c1872020-06-30 14:50:00 -0700840 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
841 "0491 Adapter Link is disabled.\n");
James Smart84d1b002010-02-12 14:42:33 -0500842 lpfc_down_link(phba, pmb);
843 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart6e7288d2010-06-07 15:23:35 -0400844 rc = lpfc_sli_issue_mbox(phba, pmb, flag);
James Smart84d1b002010-02-12 14:42:33 -0500845 if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
Dick Kennedy372c1872020-06-30 14:50:00 -0700846 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
847 "2522 Adapter failed to issue DOWN_LINK"
848 " mbox command rc 0x%x\n", rc);
James Smart84d1b002010-02-12 14:42:33 -0500849
850 mempool_free(pmb, phba->mbox_mem_pool);
851 return -EIO;
852 }
James Smart6e7288d2010-06-07 15:23:35 -0400853 if (flag == MBX_POLL)
854 mempool_free(pmb, phba->mbox_mem_pool);
855
James Smart84d1b002010-02-12 14:42:33 -0500856 return 0;
857}
858
859/**
James Smart3621a712009-04-06 18:47:14 -0400860 * lpfc_hba_down_prep - Perform lpfc uninitialization prior to HBA reset
James Smarte59058c2008-08-24 21:49:00 -0400861 * @phba: pointer to lpfc HBA data structure.
862 *
863 * This routine will do LPFC uninitialization before the HBA is reset when
864 * bringing down the SLI Layer.
865 *
866 * Return codes
867 * 0 - success.
868 * Any other value - error.
869 **/
dea31012005-04-17 16:05:31 -0500870int
James Smart2e0fef82007-06-17 19:56:36 -0500871lpfc_hba_down_prep(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500872{
James Smart1b32f6a2008-02-08 18:49:39 -0500873 struct lpfc_vport **vports;
874 int i;
James Smart3772a992009-05-22 14:50:54 -0400875
876 if (phba->sli_rev <= LPFC_SLI_REV3) {
877 /* Disable interrupts */
878 writel(0, phba->HCregaddr);
879 readl(phba->HCregaddr); /* flush */
880 }
dea31012005-04-17 16:05:31 -0500881
James Smart1b32f6a2008-02-08 18:49:39 -0500882 if (phba->pport->load_flag & FC_UNLOADING)
883 lpfc_cleanup_discovery_resources(phba->pport);
884 else {
885 vports = lpfc_create_vport_work_array(phba);
886 if (vports != NULL)
James Smart3772a992009-05-22 14:50:54 -0400887 for (i = 0; i <= phba->max_vports &&
888 vports[i] != NULL; i++)
James Smart1b32f6a2008-02-08 18:49:39 -0500889 lpfc_cleanup_discovery_resources(vports[i]);
890 lpfc_destroy_vport_work_array(phba, vports);
James Smart7f5f3d02008-02-08 18:50:14 -0500891 }
892 return 0;
dea31012005-04-17 16:05:31 -0500893}
894
James Smarte59058c2008-08-24 21:49:00 -0400895/**
James Smart68e814f2014-05-21 08:04:59 -0400896 * lpfc_sli4_free_sp_events - Cleanup sp_queue_events to free
897 * rspiocb which got deferred
898 *
899 * @phba: pointer to lpfc HBA data structure.
900 *
901 * This routine will cleanup completed slow path events after HBA is reset
902 * when bringing down the SLI Layer.
903 *
904 *
905 * Return codes
906 * void.
907 **/
908static void
909lpfc_sli4_free_sp_events(struct lpfc_hba *phba)
910{
911 struct lpfc_iocbq *rspiocbq;
912 struct hbq_dmabuf *dmabuf;
913 struct lpfc_cq_event *cq_event;
914
915 spin_lock_irq(&phba->hbalock);
916 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
917 spin_unlock_irq(&phba->hbalock);
918
919 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
920 /* Get the response iocb from the head of work queue */
921 spin_lock_irq(&phba->hbalock);
922 list_remove_head(&phba->sli4_hba.sp_queue_event,
923 cq_event, struct lpfc_cq_event, list);
924 spin_unlock_irq(&phba->hbalock);
925
926 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
927 case CQE_CODE_COMPL_WQE:
928 rspiocbq = container_of(cq_event, struct lpfc_iocbq,
929 cq_event);
930 lpfc_sli_release_iocbq(phba, rspiocbq);
931 break;
932 case CQE_CODE_RECEIVE:
933 case CQE_CODE_RECEIVE_V1:
934 dmabuf = container_of(cq_event, struct hbq_dmabuf,
935 cq_event);
936 lpfc_in_buf_free(phba, &dmabuf->dbuf);
937 }
938 }
939}
940
941/**
James Smartbcece5f2014-04-04 13:51:34 -0400942 * lpfc_hba_free_post_buf - Perform lpfc uninitialization after HBA reset
943 * @phba: pointer to lpfc HBA data structure.
944 *
945 * This routine will cleanup posted ELS buffers after the HBA is reset
946 * when bringing down the SLI Layer.
947 *
948 *
949 * Return codes
950 * void.
951 **/
952static void
953lpfc_hba_free_post_buf(struct lpfc_hba *phba)
954{
955 struct lpfc_sli *psli = &phba->sli;
956 struct lpfc_sli_ring *pring;
957 struct lpfc_dmabuf *mp, *next_mp;
James Smart07eab622014-04-04 13:51:44 -0400958 LIST_HEAD(buflist);
959 int count;
James Smartbcece5f2014-04-04 13:51:34 -0400960
961 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
962 lpfc_sli_hbqbuf_free_all(phba);
963 else {
964 /* Cleanup preposted buffers on the ELS ring */
James Smart895427b2017-02-12 13:52:30 -0800965 pring = &psli->sli3_ring[LPFC_ELS_RING];
James Smart07eab622014-04-04 13:51:44 -0400966 spin_lock_irq(&phba->hbalock);
967 list_splice_init(&pring->postbufq, &buflist);
968 spin_unlock_irq(&phba->hbalock);
969
970 count = 0;
971 list_for_each_entry_safe(mp, next_mp, &buflist, list) {
James Smartbcece5f2014-04-04 13:51:34 -0400972 list_del(&mp->list);
James Smart07eab622014-04-04 13:51:44 -0400973 count++;
James Smartbcece5f2014-04-04 13:51:34 -0400974 lpfc_mbuf_free(phba, mp->virt, mp->phys);
975 kfree(mp);
976 }
James Smart07eab622014-04-04 13:51:44 -0400977
978 spin_lock_irq(&phba->hbalock);
979 pring->postbufq_cnt -= count;
James Smartbcece5f2014-04-04 13:51:34 -0400980 spin_unlock_irq(&phba->hbalock);
981 }
982}
983
984/**
985 * lpfc_hba_clean_txcmplq - Perform lpfc uninitialization after HBA reset
986 * @phba: pointer to lpfc HBA data structure.
987 *
988 * This routine will cleanup the txcmplq after the HBA is reset when bringing
989 * down the SLI Layer.
990 *
991 * Return codes
992 * void
993 **/
994static void
995lpfc_hba_clean_txcmplq(struct lpfc_hba *phba)
996{
997 struct lpfc_sli *psli = &phba->sli;
James Smart895427b2017-02-12 13:52:30 -0800998 struct lpfc_queue *qp = NULL;
James Smartbcece5f2014-04-04 13:51:34 -0400999 struct lpfc_sli_ring *pring;
1000 LIST_HEAD(completions);
1001 int i;
James Smartc1dd9112018-01-30 15:58:57 -08001002 struct lpfc_iocbq *piocb, *next_iocb;
James Smartbcece5f2014-04-04 13:51:34 -04001003
James Smart895427b2017-02-12 13:52:30 -08001004 if (phba->sli_rev != LPFC_SLI_REV4) {
1005 for (i = 0; i < psli->num_rings; i++) {
1006 pring = &psli->sli3_ring[i];
James Smartbcece5f2014-04-04 13:51:34 -04001007 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -08001008 /* At this point in time the HBA is either reset or DOA
1009 * Nothing should be on txcmplq as it will
1010 * NEVER complete.
1011 */
1012 list_splice_init(&pring->txcmplq, &completions);
1013 pring->txcmplq_cnt = 0;
James Smartbcece5f2014-04-04 13:51:34 -04001014 spin_unlock_irq(&phba->hbalock);
1015
James Smart895427b2017-02-12 13:52:30 -08001016 lpfc_sli_abort_iocb_ring(phba, pring);
1017 }
James Smartbcece5f2014-04-04 13:51:34 -04001018 /* Cancel all the IOCBs from the completions list */
James Smart895427b2017-02-12 13:52:30 -08001019 lpfc_sli_cancel_iocbs(phba, &completions,
1020 IOSTAT_LOCAL_REJECT, IOERR_SLI_ABORTED);
1021 return;
1022 }
1023 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
1024 pring = qp->pring;
1025 if (!pring)
1026 continue;
1027 spin_lock_irq(&pring->ring_lock);
James Smartc1dd9112018-01-30 15:58:57 -08001028 list_for_each_entry_safe(piocb, next_iocb,
1029 &pring->txcmplq, list)
1030 piocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smart895427b2017-02-12 13:52:30 -08001031 list_splice_init(&pring->txcmplq, &completions);
1032 pring->txcmplq_cnt = 0;
1033 spin_unlock_irq(&pring->ring_lock);
James Smartbcece5f2014-04-04 13:51:34 -04001034 lpfc_sli_abort_iocb_ring(phba, pring);
1035 }
James Smart895427b2017-02-12 13:52:30 -08001036 /* Cancel all the IOCBs from the completions list */
1037 lpfc_sli_cancel_iocbs(phba, &completions,
1038 IOSTAT_LOCAL_REJECT, IOERR_SLI_ABORTED);
James Smartbcece5f2014-04-04 13:51:34 -04001039}
1040
1041/**
James Smart3772a992009-05-22 14:50:54 -04001042 * lpfc_hba_down_post_s3 - Perform lpfc uninitialization after HBA reset
James Smarte59058c2008-08-24 21:49:00 -04001043 * @phba: pointer to lpfc HBA data structure.
1044 *
1045 * This routine will do uninitialization after the HBA is reset when bring
1046 * down the SLI Layer.
1047 *
1048 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001049 * 0 - success.
James Smarte59058c2008-08-24 21:49:00 -04001050 * Any other value - error.
1051 **/
James Smart3772a992009-05-22 14:50:54 -04001052static int
1053lpfc_hba_down_post_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05001054{
James Smartbcece5f2014-04-04 13:51:34 -04001055 lpfc_hba_free_post_buf(phba);
1056 lpfc_hba_clean_txcmplq(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -05001057 return 0;
1058}
James Smart5af5eee2010-10-22 11:06:38 -04001059
James Smartda0436e2009-05-22 14:51:39 -04001060/**
1061 * lpfc_hba_down_post_s4 - Perform lpfc uninitialization after HBA reset
1062 * @phba: pointer to lpfc HBA data structure.
1063 *
1064 * This routine will do uninitialization after the HBA is reset when bring
1065 * down the SLI Layer.
1066 *
1067 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001068 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -04001069 * Any other value - error.
1070 **/
1071static int
1072lpfc_hba_down_post_s4(struct lpfc_hba *phba)
1073{
James Smartc4908502019-01-28 11:14:28 -08001074 struct lpfc_io_buf *psb, *psb_next;
James Smart7cacae22020-03-31 09:50:03 -07001075 struct lpfc_async_xchg_ctx *ctxp, *ctxp_next;
James Smart5e5b5112019-01-28 11:14:22 -08001076 struct lpfc_sli4_hdw_queue *qp;
James Smartda0436e2009-05-22 14:51:39 -04001077 LIST_HEAD(aborts);
James Smart895427b2017-02-12 13:52:30 -08001078 LIST_HEAD(nvme_aborts);
James Smart86c67372017-04-21 16:05:04 -07001079 LIST_HEAD(nvmet_aborts);
James Smart0f65ff62010-02-26 14:14:23 -05001080 struct lpfc_sglq *sglq_entry = NULL;
James Smart5e5b5112019-01-28 11:14:22 -08001081 int cnt, idx;
James Smart0f65ff62010-02-26 14:14:23 -05001082
James Smart895427b2017-02-12 13:52:30 -08001083
1084 lpfc_sli_hbqbuf_free_all(phba);
James Smartbcece5f2014-04-04 13:51:34 -04001085 lpfc_hba_clean_txcmplq(phba);
1086
James Smartda0436e2009-05-22 14:51:39 -04001087 /* At this point in time the HBA is either reset or DOA. Either
1088 * way, nothing should be on lpfc_abts_els_sgl_list, it needs to be
James Smart895427b2017-02-12 13:52:30 -08001089 * on the lpfc_els_sgl_list so that it can either be freed if the
James Smartda0436e2009-05-22 14:51:39 -04001090 * driver is unloading or reposted if the driver is restarting
1091 * the port.
1092 */
James Smarta7892412021-04-11 18:31:15 -07001093
James Smart895427b2017-02-12 13:52:30 -08001094 /* sgl_list_lock required because worker thread uses this
James Smartda0436e2009-05-22 14:51:39 -04001095 * list.
1096 */
James Smarta7892412021-04-11 18:31:15 -07001097 spin_lock_irq(&phba->sli4_hba.sgl_list_lock);
James Smart0f65ff62010-02-26 14:14:23 -05001098 list_for_each_entry(sglq_entry,
1099 &phba->sli4_hba.lpfc_abts_els_sgl_list, list)
1100 sglq_entry->state = SGL_FREED;
1101
James Smartda0436e2009-05-22 14:51:39 -04001102 list_splice_init(&phba->sli4_hba.lpfc_abts_els_sgl_list,
James Smart895427b2017-02-12 13:52:30 -08001103 &phba->sli4_hba.lpfc_els_sgl_list);
1104
James Smartf358dd02017-02-12 13:52:34 -08001105
James Smarta7892412021-04-11 18:31:15 -07001106 spin_unlock_irq(&phba->sli4_hba.sgl_list_lock);
James Smart5e5b5112019-01-28 11:14:22 -08001107
1108 /* abts_xxxx_buf_list_lock required because worker thread uses this
James Smartda0436e2009-05-22 14:51:39 -04001109 * list.
1110 */
James Smarta7892412021-04-11 18:31:15 -07001111 spin_lock_irq(&phba->hbalock);
James Smart5e5b5112019-01-28 11:14:22 -08001112 cnt = 0;
1113 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
1114 qp = &phba->sli4_hba.hdwq[idx];
1115
James Smartc00f62e2019-08-14 16:57:11 -07001116 spin_lock(&qp->abts_io_buf_list_lock);
1117 list_splice_init(&qp->lpfc_abts_io_buf_list,
James Smart895427b2017-02-12 13:52:30 -08001118 &aborts);
James Smart895427b2017-02-12 13:52:30 -08001119
James Smart0794d602019-01-28 11:14:19 -08001120 list_for_each_entry_safe(psb, psb_next, &aborts, list) {
James Smart86c67372017-04-21 16:05:04 -07001121 psb->pCmd = NULL;
1122 psb->status = IOSTAT_SUCCESS;
James Smartcf1a1d32017-12-08 17:18:03 -08001123 cnt++;
James Smart86c67372017-04-21 16:05:04 -07001124 }
James Smart5e5b5112019-01-28 11:14:22 -08001125 spin_lock(&qp->io_buf_list_put_lock);
1126 list_splice_init(&aborts, &qp->lpfc_io_buf_list_put);
1127 qp->put_io_bufs += qp->abts_scsi_io_bufs;
James Smartc00f62e2019-08-14 16:57:11 -07001128 qp->put_io_bufs += qp->abts_nvme_io_bufs;
James Smart5e5b5112019-01-28 11:14:22 -08001129 qp->abts_scsi_io_bufs = 0;
James Smartc00f62e2019-08-14 16:57:11 -07001130 qp->abts_nvme_io_bufs = 0;
James Smart5e5b5112019-01-28 11:14:22 -08001131 spin_unlock(&qp->io_buf_list_put_lock);
James Smartc00f62e2019-08-14 16:57:11 -07001132 spin_unlock(&qp->abts_io_buf_list_lock);
James Smart5e5b5112019-01-28 11:14:22 -08001133 }
James Smart731eedc2019-03-12 16:30:12 -07001134 spin_unlock_irq(&phba->hbalock);
James Smart5e5b5112019-01-28 11:14:22 -08001135
1136 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
James Smart731eedc2019-03-12 16:30:12 -07001137 spin_lock_irq(&phba->sli4_hba.abts_nvmet_buf_list_lock);
James Smart5e5b5112019-01-28 11:14:22 -08001138 list_splice_init(&phba->sli4_hba.lpfc_abts_nvmet_ctx_list,
1139 &nvmet_aborts);
James Smart731eedc2019-03-12 16:30:12 -07001140 spin_unlock_irq(&phba->sli4_hba.abts_nvmet_buf_list_lock);
James Smart86c67372017-04-21 16:05:04 -07001141 list_for_each_entry_safe(ctxp, ctxp_next, &nvmet_aborts, list) {
James Smart7b7f5512020-03-31 09:50:04 -07001142 ctxp->flag &= ~(LPFC_NVME_XBUSY | LPFC_NVME_ABORT_OP);
James Smart6c621a22017-05-15 15:20:45 -07001143 lpfc_nvmet_ctxbuf_post(phba, ctxp->ctxbuf);
James Smart86c67372017-04-21 16:05:04 -07001144 }
James Smart895427b2017-02-12 13:52:30 -08001145 }
James Smart895427b2017-02-12 13:52:30 -08001146
James Smart68e814f2014-05-21 08:04:59 -04001147 lpfc_sli4_free_sp_events(phba);
James Smart5e5b5112019-01-28 11:14:22 -08001148 return cnt;
James Smartda0436e2009-05-22 14:51:39 -04001149}
1150
1151/**
1152 * lpfc_hba_down_post - Wrapper func for hba down post routine
1153 * @phba: pointer to lpfc HBA data structure.
1154 *
1155 * This routine wraps the actual SLI3 or SLI4 routine for performing
1156 * uninitialization after the HBA is reset when bring down the SLI Layer.
1157 *
1158 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001159 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -04001160 * Any other value - error.
1161 **/
1162int
1163lpfc_hba_down_post(struct lpfc_hba *phba)
1164{
1165 return (*phba->lpfc_hba_down_post)(phba);
1166}
Jamie Wellnitz41415862006-02-28 19:25:27 -05001167
James Smarte59058c2008-08-24 21:49:00 -04001168/**
James Smart3621a712009-04-06 18:47:14 -04001169 * lpfc_hb_timeout - The HBA-timer timeout handler
Lee Jonesfe614ac2020-07-23 13:24:22 +01001170 * @t: timer context used to obtain the pointer to lpfc hba data structure.
James Smarte59058c2008-08-24 21:49:00 -04001171 *
1172 * This is the HBA-timer timeout handler registered to the lpfc driver. When
1173 * this timer fires, a HBA timeout event shall be posted to the lpfc driver
1174 * work-port-events bitmap and the worker thread is notified. This timeout
1175 * event will be used by the worker thread to invoke the actual timeout
1176 * handler routine, lpfc_hb_timeout_handler. Any periodical operations will
1177 * be performed in the timeout handler and the HBA timeout event bit shall
1178 * be cleared by the worker thread after it has taken the event bitmap out.
1179 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001180static void
Kees Cookf22eb4d2017-09-06 20:24:26 -07001181lpfc_hb_timeout(struct timer_list *t)
James Smart858c9f62007-06-17 19:56:39 -05001182{
1183 struct lpfc_hba *phba;
James Smart5e9d9b82008-06-14 22:52:53 -04001184 uint32_t tmo_posted;
James Smart858c9f62007-06-17 19:56:39 -05001185 unsigned long iflag;
1186
Kees Cookf22eb4d2017-09-06 20:24:26 -07001187 phba = from_timer(phba, t, hb_tmofunc);
James Smart93996272008-08-24 21:50:30 -04001188
1189 /* Check for heart beat timeout conditions */
James Smart858c9f62007-06-17 19:56:39 -05001190 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -04001191 tmo_posted = phba->pport->work_port_events & WORKER_HB_TMO;
1192 if (!tmo_posted)
James Smart858c9f62007-06-17 19:56:39 -05001193 phba->pport->work_port_events |= WORKER_HB_TMO;
1194 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
1195
James Smart93996272008-08-24 21:50:30 -04001196 /* Tell the worker thread there is work to do */
James Smart5e9d9b82008-06-14 22:52:53 -04001197 if (!tmo_posted)
1198 lpfc_worker_wake_up(phba);
James Smart858c9f62007-06-17 19:56:39 -05001199 return;
1200}
1201
James Smarte59058c2008-08-24 21:49:00 -04001202/**
James Smart19ca7602010-11-20 23:11:55 -05001203 * lpfc_rrq_timeout - The RRQ-timer timeout handler
Lee Jonesfe614ac2020-07-23 13:24:22 +01001204 * @t: timer context used to obtain the pointer to lpfc hba data structure.
James Smart19ca7602010-11-20 23:11:55 -05001205 *
1206 * This is the RRQ-timer timeout handler registered to the lpfc driver. When
1207 * this timer fires, a RRQ timeout event shall be posted to the lpfc driver
1208 * work-port-events bitmap and the worker thread is notified. This timeout
1209 * event will be used by the worker thread to invoke the actual timeout
1210 * handler routine, lpfc_rrq_handler. Any periodical operations will
1211 * be performed in the timeout handler and the RRQ timeout event bit shall
1212 * be cleared by the worker thread after it has taken the event bitmap out.
1213 **/
1214static void
Kees Cookf22eb4d2017-09-06 20:24:26 -07001215lpfc_rrq_timeout(struct timer_list *t)
James Smart19ca7602010-11-20 23:11:55 -05001216{
1217 struct lpfc_hba *phba;
James Smart19ca7602010-11-20 23:11:55 -05001218 unsigned long iflag;
1219
Kees Cookf22eb4d2017-09-06 20:24:26 -07001220 phba = from_timer(phba, t, rrq_tmr);
James Smart19ca7602010-11-20 23:11:55 -05001221 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart06918ac2014-02-20 09:57:57 -05001222 if (!(phba->pport->load_flag & FC_UNLOADING))
1223 phba->hba_flag |= HBA_RRQ_ACTIVE;
1224 else
1225 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
James Smart19ca7602010-11-20 23:11:55 -05001226 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
James Smart06918ac2014-02-20 09:57:57 -05001227
1228 if (!(phba->pport->load_flag & FC_UNLOADING))
1229 lpfc_worker_wake_up(phba);
James Smart19ca7602010-11-20 23:11:55 -05001230}
1231
1232/**
James Smart3621a712009-04-06 18:47:14 -04001233 * lpfc_hb_mbox_cmpl - The lpfc heart-beat mailbox command callback function
James Smarte59058c2008-08-24 21:49:00 -04001234 * @phba: pointer to lpfc hba data structure.
1235 * @pmboxq: pointer to the driver internal queue element for mailbox command.
1236 *
1237 * This is the callback function to the lpfc heart-beat mailbox command.
1238 * If configured, the lpfc driver issues the heart-beat mailbox command to
1239 * the HBA every LPFC_HB_MBOX_INTERVAL (current 5) seconds. At the time the
1240 * heart-beat mailbox command is issued, the driver shall set up heart-beat
1241 * timeout timer to LPFC_HB_MBOX_TIMEOUT (current 30) seconds and marks
1242 * heart-beat outstanding state. Once the mailbox command comes back and
1243 * no error conditions detected, the heart-beat mailbox command timer is
1244 * reset to LPFC_HB_MBOX_INTERVAL seconds and the heart-beat outstanding
1245 * state is cleared for the next heart-beat. If the timer expired with the
1246 * heart-beat outstanding state set, the driver will put the HBA offline.
1247 **/
James Smart858c9f62007-06-17 19:56:39 -05001248static void
1249lpfc_hb_mbox_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
1250{
1251 unsigned long drvr_flag;
1252
1253 spin_lock_irqsave(&phba->hbalock, drvr_flag);
James Smarta22d73b2021-01-04 10:02:38 -08001254 phba->hba_flag &= ~(HBA_HBEAT_INP | HBA_HBEAT_TMO);
James Smart858c9f62007-06-17 19:56:39 -05001255 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
1256
James Smarta22d73b2021-01-04 10:02:38 -08001257 /* Check and reset heart-beat timer if necessary */
James Smart858c9f62007-06-17 19:56:39 -05001258 mempool_free(pmboxq, phba->mbox_mem_pool);
1259 if (!(phba->pport->fc_flag & FC_OFFLINE_MODE) &&
1260 !(phba->link_state == LPFC_HBA_ERROR) &&
James Smart51ef4c22007-08-02 11:10:31 -04001261 !(phba->pport->load_flag & FC_UNLOADING))
James Smart858c9f62007-06-17 19:56:39 -05001262 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001263 jiffies +
1264 msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smart858c9f62007-06-17 19:56:39 -05001265 return;
1266}
1267
Lee Jonesfe614ac2020-07-23 13:24:22 +01001268/*
Dick Kennedy317aeb82020-06-30 14:49:59 -07001269 * lpfc_idle_stat_delay_work - idle_stat tracking
1270 *
1271 * This routine tracks per-cq idle_stat and determines polling decisions.
1272 *
1273 * Return codes:
1274 * None
1275 **/
1276static void
1277lpfc_idle_stat_delay_work(struct work_struct *work)
1278{
1279 struct lpfc_hba *phba = container_of(to_delayed_work(work),
1280 struct lpfc_hba,
1281 idle_stat_delay_work);
1282 struct lpfc_queue *cq;
1283 struct lpfc_sli4_hdw_queue *hdwq;
1284 struct lpfc_idle_stat *idle_stat;
1285 u32 i, idle_percent;
1286 u64 wall, wall_idle, diff_wall, diff_idle, busy_time;
1287
1288 if (phba->pport->load_flag & FC_UNLOADING)
1289 return;
1290
1291 if (phba->link_state == LPFC_HBA_ERROR ||
James Smart9064aeb2021-08-16 09:28:50 -07001292 phba->pport->fc_flag & FC_OFFLINE_MODE ||
1293 phba->cmf_active_mode != LPFC_CFG_OFF)
Dick Kennedy317aeb82020-06-30 14:49:59 -07001294 goto requeue;
1295
1296 for_each_present_cpu(i) {
1297 hdwq = &phba->sli4_hba.hdwq[phba->sli4_hba.cpu_map[i].hdwq];
1298 cq = hdwq->io_cq;
1299
1300 /* Skip if we've already handled this cq's primary CPU */
1301 if (cq->chann != i)
1302 continue;
1303
1304 idle_stat = &phba->sli4_hba.idle_stat[i];
1305
1306 /* get_cpu_idle_time returns values as running counters. Thus,
1307 * to know the amount for this period, the prior counter values
1308 * need to be subtracted from the current counter values.
1309 * From there, the idle time stat can be calculated as a
1310 * percentage of 100 - the sum of the other consumption times.
1311 */
1312 wall_idle = get_cpu_idle_time(i, &wall, 1);
1313 diff_idle = wall_idle - idle_stat->prev_idle;
1314 diff_wall = wall - idle_stat->prev_wall;
1315
1316 if (diff_wall <= diff_idle)
1317 busy_time = 0;
1318 else
1319 busy_time = diff_wall - diff_idle;
1320
1321 idle_percent = div64_u64(100 * busy_time, diff_wall);
1322 idle_percent = 100 - idle_percent;
1323
1324 if (idle_percent < 15)
1325 cq->poll_mode = LPFC_QUEUE_WORK;
1326 else
1327 cq->poll_mode = LPFC_IRQ_POLL;
1328
1329 idle_stat->prev_idle = wall_idle;
1330 idle_stat->prev_wall = wall;
1331 }
1332
1333requeue:
1334 schedule_delayed_work(&phba->idle_stat_delay_work,
1335 msecs_to_jiffies(LPFC_IDLE_STAT_DELAY));
1336}
1337
James Smart32517fc2019-01-28 11:14:33 -08001338static void
1339lpfc_hb_eq_delay_work(struct work_struct *work)
1340{
1341 struct lpfc_hba *phba = container_of(to_delayed_work(work),
1342 struct lpfc_hba, eq_delay_work);
1343 struct lpfc_eq_intr_info *eqi, *eqi_new;
1344 struct lpfc_queue *eq, *eq_next;
James Smart8156d372019-10-18 14:18:26 -07001345 unsigned char *ena_delay = NULL;
James Smart32517fc2019-01-28 11:14:33 -08001346 uint32_t usdelay;
1347 int i;
1348
1349 if (!phba->cfg_auto_imax || phba->pport->load_flag & FC_UNLOADING)
1350 return;
1351
1352 if (phba->link_state == LPFC_HBA_ERROR ||
1353 phba->pport->fc_flag & FC_OFFLINE_MODE)
1354 goto requeue;
1355
James Smart8156d372019-10-18 14:18:26 -07001356 ena_delay = kcalloc(phba->sli4_hba.num_possible_cpu, sizeof(*ena_delay),
1357 GFP_KERNEL);
1358 if (!ena_delay)
James Smart32517fc2019-01-28 11:14:33 -08001359 goto requeue;
1360
James Smart8156d372019-10-18 14:18:26 -07001361 for (i = 0; i < phba->cfg_irq_chann; i++) {
1362 /* Get the EQ corresponding to the IRQ vector */
1363 eq = phba->sli4_hba.hba_eq_hdl[i].eq;
1364 if (!eq)
1365 continue;
1366 if (eq->q_mode || eq->q_flag & HBA_EQ_DELAY_CHK) {
1367 eq->q_flag &= ~HBA_EQ_DELAY_CHK;
1368 ena_delay[eq->last_cpu] = 1;
James Smart8d34a592019-08-14 16:56:35 -07001369 }
James Smart8156d372019-10-18 14:18:26 -07001370 }
James Smart32517fc2019-01-28 11:14:33 -08001371
1372 for_each_present_cpu(i) {
James Smart32517fc2019-01-28 11:14:33 -08001373 eqi = per_cpu_ptr(phba->sli4_hba.eq_info, i);
James Smart8156d372019-10-18 14:18:26 -07001374 if (ena_delay[i]) {
1375 usdelay = (eqi->icnt >> 10) * LPFC_EQ_DELAY_STEP;
1376 if (usdelay > LPFC_MAX_AUTO_EQ_DELAY)
1377 usdelay = LPFC_MAX_AUTO_EQ_DELAY;
1378 } else {
1379 usdelay = 0;
James Smart8d34a592019-08-14 16:56:35 -07001380 }
James Smart32517fc2019-01-28 11:14:33 -08001381
James Smart32517fc2019-01-28 11:14:33 -08001382 eqi->icnt = 0;
1383
1384 list_for_each_entry_safe(eq, eq_next, &eqi->list, cpu_list) {
James Smart8156d372019-10-18 14:18:26 -07001385 if (unlikely(eq->last_cpu != i)) {
James Smart32517fc2019-01-28 11:14:33 -08001386 eqi_new = per_cpu_ptr(phba->sli4_hba.eq_info,
1387 eq->last_cpu);
1388 list_move_tail(&eq->cpu_list, &eqi_new->list);
1389 continue;
1390 }
1391 if (usdelay != eq->q_mode)
1392 lpfc_modify_hba_eq_delay(phba, eq->hdwq, 1,
1393 usdelay);
1394 }
1395 }
1396
James Smart8156d372019-10-18 14:18:26 -07001397 kfree(ena_delay);
James Smart32517fc2019-01-28 11:14:33 -08001398
1399requeue:
1400 queue_delayed_work(phba->wq, &phba->eq_delay_work,
1401 msecs_to_jiffies(LPFC_EQ_DELAY_MSECS));
1402}
1403
James Smarte59058c2008-08-24 21:49:00 -04001404/**
James Smartc4908502019-01-28 11:14:28 -08001405 * lpfc_hb_mxp_handler - Multi-XRI pools handler to adjust XRI distribution
1406 * @phba: pointer to lpfc hba data structure.
1407 *
1408 * For each heartbeat, this routine does some heuristic methods to adjust
1409 * XRI distribution. The goal is to fully utilize free XRIs.
1410 **/
1411static void lpfc_hb_mxp_handler(struct lpfc_hba *phba)
1412{
1413 u32 i;
1414 u32 hwq_count;
1415
1416 hwq_count = phba->cfg_hdw_queue;
1417 for (i = 0; i < hwq_count; i++) {
1418 /* Adjust XRIs in private pool */
1419 lpfc_adjust_pvt_pool_count(phba, i);
1420
1421 /* Adjust high watermark */
1422 lpfc_adjust_high_watermark(phba, i);
1423
1424#ifdef LPFC_MXP_STAT
1425 /* Snapshot pbl, pvt and busy count */
1426 lpfc_snapshot_mxp(phba, i);
1427#endif
1428 }
1429}
1430
James Smarte59058c2008-08-24 21:49:00 -04001431/**
James Smarta22d73b2021-01-04 10:02:38 -08001432 * lpfc_issue_hb_mbox - Issues heart-beat mailbox command
1433 * @phba: pointer to lpfc hba data structure.
1434 *
1435 * If a HB mbox is not already in progrees, this routine will allocate
1436 * a LPFC_MBOXQ_t, populate it with a MBX_HEARTBEAT (0x31) command,
1437 * and issue it. The HBA_HBEAT_INP flag means the command is in progress.
1438 **/
1439int
1440lpfc_issue_hb_mbox(struct lpfc_hba *phba)
1441{
1442 LPFC_MBOXQ_t *pmboxq;
1443 int retval;
1444
1445 /* Is a Heartbeat mbox already in progress */
1446 if (phba->hba_flag & HBA_HBEAT_INP)
1447 return 0;
1448
1449 pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1450 if (!pmboxq)
1451 return -ENOMEM;
1452
1453 lpfc_heart_beat(phba, pmboxq);
1454 pmboxq->mbox_cmpl = lpfc_hb_mbox_cmpl;
1455 pmboxq->vport = phba->pport;
1456 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
1457
1458 if (retval != MBX_BUSY && retval != MBX_SUCCESS) {
1459 mempool_free(pmboxq, phba->mbox_mem_pool);
1460 return -ENXIO;
1461 }
1462 phba->hba_flag |= HBA_HBEAT_INP;
1463
1464 return 0;
1465}
1466
1467/**
1468 * lpfc_issue_hb_tmo - Signals heartbeat timer to issue mbox command
1469 * @phba: pointer to lpfc hba data structure.
1470 *
1471 * The heartbeat timer (every 5 sec) will fire. If the HBA_HBEAT_TMO
1472 * flag is set, it will force a MBX_HEARTBEAT mbox command, regardless
1473 * of the value of lpfc_enable_hba_heartbeat.
1474 * If lpfc_enable_hba_heartbeat is set, the timeout routine will always
1475 * try to issue a MBX_HEARTBEAT mbox command.
1476 **/
1477void
1478lpfc_issue_hb_tmo(struct lpfc_hba *phba)
1479{
1480 if (phba->cfg_enable_hba_heartbeat)
1481 return;
1482 phba->hba_flag |= HBA_HBEAT_TMO;
1483}
1484
1485/**
James Smart3621a712009-04-06 18:47:14 -04001486 * lpfc_hb_timeout_handler - The HBA-timer timeout handler
James Smarte59058c2008-08-24 21:49:00 -04001487 * @phba: pointer to lpfc hba data structure.
1488 *
1489 * This is the actual HBA-timer timeout handler to be invoked by the worker
1490 * thread whenever the HBA timer fired and HBA-timeout event posted. This
1491 * handler performs any periodic operations needed for the device. If such
1492 * periodic event has already been attended to either in the interrupt handler
1493 * or by processing slow-ring or fast-ring events within the HBA-timer
1494 * timeout window (LPFC_HB_MBOX_INTERVAL), this handler just simply resets
1495 * the timer for the next timeout period. If lpfc heart-beat mailbox command
1496 * is configured and there is no heart-beat mailbox command outstanding, a
1497 * heart-beat mailbox is issued and timer set properly. Otherwise, if there
1498 * has been a heart-beat mailbox command outstanding, the HBA shall be put
1499 * to offline.
1500 **/
James Smart858c9f62007-06-17 19:56:39 -05001501void
1502lpfc_hb_timeout_handler(struct lpfc_hba *phba)
1503{
James Smart45ed1192009-10-02 15:17:02 -04001504 struct lpfc_vport **vports;
James Smart0ff10d42008-01-11 01:52:36 -05001505 struct lpfc_dmabuf *buf_ptr;
James Smarta22d73b2021-01-04 10:02:38 -08001506 int retval = 0;
1507 int i, tmo;
James Smart858c9f62007-06-17 19:56:39 -05001508 struct lpfc_sli *psli = &phba->sli;
James Smart0ff10d42008-01-11 01:52:36 -05001509 LIST_HEAD(completions);
James Smart858c9f62007-06-17 19:56:39 -05001510
James Smartc4908502019-01-28 11:14:28 -08001511 if (phba->cfg_xri_rebalancing) {
1512 /* Multi-XRI pools handler */
1513 lpfc_hb_mxp_handler(phba);
1514 }
James Smart858c9f62007-06-17 19:56:39 -05001515
James Smart45ed1192009-10-02 15:17:02 -04001516 vports = lpfc_create_vport_work_array(phba);
1517 if (vports != NULL)
James Smart4258e982015-12-16 18:11:58 -05001518 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart45ed1192009-10-02 15:17:02 -04001519 lpfc_rcv_seq_check_edtov(vports[i]);
James Smarte3ba04c2019-12-18 15:58:02 -08001520 lpfc_fdmi_change_check(vports[i]);
James Smart4258e982015-12-16 18:11:58 -05001521 }
James Smart45ed1192009-10-02 15:17:02 -04001522 lpfc_destroy_vport_work_array(phba, vports);
1523
James Smart858c9f62007-06-17 19:56:39 -05001524 if ((phba->link_state == LPFC_HBA_ERROR) ||
James Smart51ef4c22007-08-02 11:10:31 -04001525 (phba->pport->load_flag & FC_UNLOADING) ||
James Smart858c9f62007-06-17 19:56:39 -05001526 (phba->pport->fc_flag & FC_OFFLINE_MODE))
1527 return;
1528
James Smart0ff10d42008-01-11 01:52:36 -05001529 if (phba->elsbuf_cnt &&
1530 (phba->elsbuf_cnt == phba->elsbuf_prev_cnt)) {
1531 spin_lock_irq(&phba->hbalock);
1532 list_splice_init(&phba->elsbuf, &completions);
1533 phba->elsbuf_cnt = 0;
1534 phba->elsbuf_prev_cnt = 0;
1535 spin_unlock_irq(&phba->hbalock);
1536
1537 while (!list_empty(&completions)) {
1538 list_remove_head(&completions, buf_ptr,
1539 struct lpfc_dmabuf, list);
1540 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
1541 kfree(buf_ptr);
1542 }
1543 }
1544 phba->elsbuf_prev_cnt = phba->elsbuf_cnt;
1545
James Smart858c9f62007-06-17 19:56:39 -05001546 /* If there is no heart beat outstanding, issue a heartbeat command */
James Smart13815c82008-01-11 01:52:48 -05001547 if (phba->cfg_enable_hba_heartbeat) {
James Smarta22d73b2021-01-04 10:02:38 -08001548 /* If IOs are completing, no need to issue a MBX_HEARTBEAT */
1549 spin_lock_irq(&phba->pport->work_port_lock);
1550 if (time_after(phba->last_completion_time +
1551 msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL),
1552 jiffies)) {
1553 spin_unlock_irq(&phba->pport->work_port_lock);
1554 if (phba->hba_flag & HBA_HBEAT_INP)
1555 tmo = (1000 * LPFC_HB_MBOX_TIMEOUT);
1556 else
1557 tmo = (1000 * LPFC_HB_MBOX_INTERVAL);
1558 goto out;
1559 }
1560 spin_unlock_irq(&phba->pport->work_port_lock);
1561
1562 /* Check if a MBX_HEARTBEAT is already in progress */
1563 if (phba->hba_flag & HBA_HBEAT_INP) {
1564 /*
1565 * If heart beat timeout called with HBA_HBEAT_INP set
1566 * we need to give the hb mailbox cmd a chance to
1567 * complete or TMO.
1568 */
1569 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
1570 "0459 Adapter heartbeat still outstanding: "
1571 "last compl time was %d ms.\n",
1572 jiffies_to_msecs(jiffies
1573 - phba->last_completion_time));
1574 tmo = (1000 * LPFC_HB_MBOX_TIMEOUT);
1575 } else {
James Smartbc739052010-08-04 16:11:18 -04001576 if ((!(psli->sli_flag & LPFC_SLI_MBOX_ACTIVE)) &&
1577 (list_empty(&psli->mboxq))) {
James Smart13815c82008-01-11 01:52:48 -05001578
James Smarta22d73b2021-01-04 10:02:38 -08001579 retval = lpfc_issue_hb_mbox(phba);
1580 if (retval) {
1581 tmo = (1000 * LPFC_HB_MBOX_INTERVAL);
1582 goto out;
James Smartbc739052010-08-04 16:11:18 -04001583 }
1584 phba->skipped_hb = 0;
James Smartbc739052010-08-04 16:11:18 -04001585 } else if (time_before_eq(phba->last_completion_time,
1586 phba->skipped_hb)) {
1587 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
1588 "2857 Last completion time not "
1589 " updated in %d ms\n",
1590 jiffies_to_msecs(jiffies
1591 - phba->last_completion_time));
1592 } else
1593 phba->skipped_hb = jiffies;
1594
James Smarta22d73b2021-01-04 10:02:38 -08001595 tmo = (1000 * LPFC_HB_MBOX_TIMEOUT);
1596 goto out;
James Smart858c9f62007-06-17 19:56:39 -05001597 }
James Smart4258e982015-12-16 18:11:58 -05001598 } else {
James Smarta22d73b2021-01-04 10:02:38 -08001599 /* Check to see if we want to force a MBX_HEARTBEAT */
1600 if (phba->hba_flag & HBA_HBEAT_TMO) {
1601 retval = lpfc_issue_hb_mbox(phba);
1602 if (retval)
1603 tmo = (1000 * LPFC_HB_MBOX_INTERVAL);
1604 else
1605 tmo = (1000 * LPFC_HB_MBOX_TIMEOUT);
1606 goto out;
1607 }
1608 tmo = (1000 * LPFC_HB_MBOX_INTERVAL);
James Smart858c9f62007-06-17 19:56:39 -05001609 }
James Smarta22d73b2021-01-04 10:02:38 -08001610out:
1611 mod_timer(&phba->hb_tmofunc, jiffies + msecs_to_jiffies(tmo));
James Smart858c9f62007-06-17 19:56:39 -05001612}
1613
James Smarte59058c2008-08-24 21:49:00 -04001614/**
James Smart3621a712009-04-06 18:47:14 -04001615 * lpfc_offline_eratt - Bring lpfc offline on hardware error attention
James Smarte59058c2008-08-24 21:49:00 -04001616 * @phba: pointer to lpfc hba data structure.
1617 *
1618 * This routine is called to bring the HBA offline when HBA hardware error
1619 * other than Port Error 6 has been detected.
1620 **/
James Smart09372822008-01-11 01:52:54 -05001621static void
1622lpfc_offline_eratt(struct lpfc_hba *phba)
1623{
1624 struct lpfc_sli *psli = &phba->sli;
1625
1626 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001627 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart09372822008-01-11 01:52:54 -05001628 spin_unlock_irq(&phba->hbalock);
James Smart618a5232012-06-12 13:54:36 -04001629 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
James Smart09372822008-01-11 01:52:54 -05001630
1631 lpfc_offline(phba);
1632 lpfc_reset_barrier(phba);
James Smartf4b4c682009-05-22 14:53:12 -04001633 spin_lock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001634 lpfc_sli_brdreset(phba);
James Smartf4b4c682009-05-22 14:53:12 -04001635 spin_unlock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001636 lpfc_hba_down_post(phba);
1637 lpfc_sli_brdready(phba, HS_MBRDY);
1638 lpfc_unblock_mgmt_io(phba);
1639 phba->link_state = LPFC_HBA_ERROR;
1640 return;
1641}
1642
James Smarte59058c2008-08-24 21:49:00 -04001643/**
James Smartda0436e2009-05-22 14:51:39 -04001644 * lpfc_sli4_offline_eratt - Bring lpfc offline on SLI4 hardware error attention
1645 * @phba: pointer to lpfc hba data structure.
1646 *
1647 * This routine is called to bring a SLI4 HBA offline when HBA hardware error
1648 * other than Port Error 6 has been detected.
1649 **/
James Smarta88dbb62013-04-17 20:18:39 -04001650void
James Smartda0436e2009-05-22 14:51:39 -04001651lpfc_sli4_offline_eratt(struct lpfc_hba *phba)
1652{
James Smart946727d2015-04-07 15:07:09 -04001653 spin_lock_irq(&phba->hbalock);
James Smart25ac2c972021-09-10 16:31:54 -07001654 if (phba->link_state == LPFC_HBA_ERROR &&
1655 phba->hba_flag & HBA_PCI_ERR) {
1656 spin_unlock_irq(&phba->hbalock);
1657 return;
1658 }
James Smart946727d2015-04-07 15:07:09 -04001659 phba->link_state = LPFC_HBA_ERROR;
1660 spin_unlock_irq(&phba->hbalock);
1661
James Smart618a5232012-06-12 13:54:36 -04001662 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
James Smartc00f62e2019-08-14 16:57:11 -07001663 lpfc_sli_flush_io_rings(phba);
James Smartda0436e2009-05-22 14:51:39 -04001664 lpfc_offline(phba);
James Smartda0436e2009-05-22 14:51:39 -04001665 lpfc_hba_down_post(phba);
James Smartda0436e2009-05-22 14:51:39 -04001666 lpfc_unblock_mgmt_io(phba);
James Smartda0436e2009-05-22 14:51:39 -04001667}
1668
1669/**
James Smarta257bf92009-04-06 18:48:10 -04001670 * lpfc_handle_deferred_eratt - The HBA hardware deferred error handler
1671 * @phba: pointer to lpfc hba data structure.
1672 *
1673 * This routine is invoked to handle the deferred HBA hardware error
1674 * conditions. This type of error is indicated by HBA by setting ER1
1675 * and another ER bit in the host status register. The driver will
1676 * wait until the ER1 bit clears before handling the error condition.
1677 **/
1678static void
1679lpfc_handle_deferred_eratt(struct lpfc_hba *phba)
1680{
1681 uint32_t old_host_status = phba->work_hs;
James Smarta257bf92009-04-06 18:48:10 -04001682 struct lpfc_sli *psli = &phba->sli;
1683
James Smartf4b4c682009-05-22 14:53:12 -04001684 /* If the pci channel is offline, ignore possible errors,
1685 * since we cannot communicate with the pci card anyway.
1686 */
1687 if (pci_channel_offline(phba->pcidev)) {
1688 spin_lock_irq(&phba->hbalock);
1689 phba->hba_flag &= ~DEFER_ERATT;
1690 spin_unlock_irq(&phba->hbalock);
1691 return;
1692 }
1693
Dick Kennedy372c1872020-06-30 14:50:00 -07001694 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
1695 "0479 Deferred Adapter Hardware Error "
1696 "Data: x%x x%x x%x\n",
1697 phba->work_hs, phba->work_status[0],
1698 phba->work_status[1]);
James Smarta257bf92009-04-06 18:48:10 -04001699
1700 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001701 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smarta257bf92009-04-06 18:48:10 -04001702 spin_unlock_irq(&phba->hbalock);
1703
1704
1705 /*
1706 * Firmware stops when it triggred erratt. That could cause the I/Os
1707 * dropped by the firmware. Error iocb (I/O) on txcmplq and let the
1708 * SCSI layer retry it after re-establishing link.
1709 */
James Smartdb55fba2014-04-04 13:52:02 -04001710 lpfc_sli_abort_fcp_rings(phba);
James Smarta257bf92009-04-06 18:48:10 -04001711
1712 /*
1713 * There was a firmware error. Take the hba offline and then
1714 * attempt to restart it.
1715 */
James Smart618a5232012-06-12 13:54:36 -04001716 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smarta257bf92009-04-06 18:48:10 -04001717 lpfc_offline(phba);
1718
1719 /* Wait for the ER1 bit to clear.*/
1720 while (phba->work_hs & HS_FFER1) {
1721 msleep(100);
James Smart9940b972011-03-11 16:06:12 -05001722 if (lpfc_readl(phba->HSregaddr, &phba->work_hs)) {
1723 phba->work_hs = UNPLUG_ERR ;
1724 break;
1725 }
James Smarta257bf92009-04-06 18:48:10 -04001726 /* If driver is unloading let the worker thread continue */
1727 if (phba->pport->load_flag & FC_UNLOADING) {
1728 phba->work_hs = 0;
1729 break;
1730 }
1731 }
1732
1733 /*
1734 * This is to ptrotect against a race condition in which
1735 * first write to the host attention register clear the
1736 * host status register.
1737 */
1738 if ((!phba->work_hs) && (!(phba->pport->load_flag & FC_UNLOADING)))
1739 phba->work_hs = old_host_status & ~HS_FFER1;
1740
James Smart3772a992009-05-22 14:50:54 -04001741 spin_lock_irq(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -04001742 phba->hba_flag &= ~DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -04001743 spin_unlock_irq(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -04001744 phba->work_status[0] = readl(phba->MBslimaddr + 0xa8);
1745 phba->work_status[1] = readl(phba->MBslimaddr + 0xac);
1746}
1747
James Smart3772a992009-05-22 14:50:54 -04001748static void
1749lpfc_board_errevt_to_mgmt(struct lpfc_hba *phba)
1750{
1751 struct lpfc_board_event_header board_event;
1752 struct Scsi_Host *shost;
1753
1754 board_event.event_type = FC_REG_BOARD_EVENT;
1755 board_event.subcategory = LPFC_EVENT_PORTINTERR;
1756 shost = lpfc_shost_from_vport(phba->pport);
1757 fc_host_post_vendor_event(shost, fc_get_event_number(),
1758 sizeof(board_event),
1759 (char *) &board_event,
1760 LPFC_NL_VENDOR_ID);
1761}
1762
James Smarta257bf92009-04-06 18:48:10 -04001763/**
James Smart3772a992009-05-22 14:50:54 -04001764 * lpfc_handle_eratt_s3 - The SLI3 HBA hardware error handler
James Smarte59058c2008-08-24 21:49:00 -04001765 * @phba: pointer to lpfc hba data structure.
1766 *
1767 * This routine is invoked to handle the following HBA hardware error
1768 * conditions:
1769 * 1 - HBA error attention interrupt
1770 * 2 - DMA ring index out of range
1771 * 3 - Mailbox command came back as unknown
1772 **/
James Smart3772a992009-05-22 14:50:54 -04001773static void
1774lpfc_handle_eratt_s3(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001775{
James Smart2e0fef82007-06-17 19:56:36 -05001776 struct lpfc_vport *vport = phba->pport;
James Smart2e0fef82007-06-17 19:56:36 -05001777 struct lpfc_sli *psli = &phba->sli;
James Smartd2873e42006-08-18 17:46:43 -04001778 uint32_t event_data;
James Smart57127f12007-10-27 13:37:05 -04001779 unsigned long temperature;
1780 struct temp_event temp_event_data;
James Smart92d7f7b2007-06-17 19:56:38 -05001781 struct Scsi_Host *shost;
James Smart2e0fef82007-06-17 19:56:36 -05001782
Linas Vepstas8d63f372007-02-14 14:28:36 -06001783 /* If the pci channel is offline, ignore possible errors,
James Smart3772a992009-05-22 14:50:54 -04001784 * since we cannot communicate with the pci card anyway.
1785 */
1786 if (pci_channel_offline(phba->pcidev)) {
1787 spin_lock_irq(&phba->hbalock);
1788 phba->hba_flag &= ~DEFER_ERATT;
1789 spin_unlock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -06001790 return;
James Smart3772a992009-05-22 14:50:54 -04001791 }
1792
James Smart13815c82008-01-11 01:52:48 -05001793 /* If resets are disabled then leave the HBA alone and return */
1794 if (!phba->cfg_enable_hba_reset)
1795 return;
dea31012005-04-17 16:05:31 -05001796
James Smartea2151b2008-09-07 11:52:10 -04001797 /* Send an internal error event to mgmt application */
James Smart3772a992009-05-22 14:50:54 -04001798 lpfc_board_errevt_to_mgmt(phba);
James Smartea2151b2008-09-07 11:52:10 -04001799
James Smarta257bf92009-04-06 18:48:10 -04001800 if (phba->hba_flag & DEFER_ERATT)
1801 lpfc_handle_deferred_eratt(phba);
1802
James Smartdcf2a4e2010-09-29 11:18:53 -04001803 if ((phba->work_hs & HS_FFER6) || (phba->work_hs & HS_FFER8)) {
1804 if (phba->work_hs & HS_FFER6)
1805 /* Re-establishing Link */
1806 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1807 "1301 Re-establishing Link "
1808 "Data: x%x x%x x%x\n",
1809 phba->work_hs, phba->work_status[0],
1810 phba->work_status[1]);
1811 if (phba->work_hs & HS_FFER8)
1812 /* Device Zeroization */
1813 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1814 "2861 Host Authentication device "
1815 "zeroization Data:x%x x%x x%x\n",
1816 phba->work_hs, phba->work_status[0],
1817 phba->work_status[1]);
James Smart58da1ff2008-04-07 10:15:56 -04001818
James Smart92d7f7b2007-06-17 19:56:38 -05001819 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001820 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -05001821 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001822
1823 /*
1824 * Firmware stops when it triggled erratt with HS_FFER6.
1825 * That could cause the I/Os dropped by the firmware.
1826 * Error iocb (I/O) on txcmplq and let the SCSI layer
1827 * retry it after re-establishing link.
1828 */
James Smartdb55fba2014-04-04 13:52:02 -04001829 lpfc_sli_abort_fcp_rings(phba);
dea31012005-04-17 16:05:31 -05001830
dea31012005-04-17 16:05:31 -05001831 /*
1832 * There was a firmware error. Take the hba offline and then
1833 * attempt to restart it.
1834 */
James Smart618a5232012-06-12 13:54:36 -04001835 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
dea31012005-04-17 16:05:31 -05001836 lpfc_offline(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -05001837 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05001838 if (lpfc_online(phba) == 0) { /* Initialize the HBA */
James Smart46fa3112007-04-25 09:51:45 -04001839 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05001840 return;
1841 }
James Smart46fa3112007-04-25 09:51:45 -04001842 lpfc_unblock_mgmt_io(phba);
James Smart57127f12007-10-27 13:37:05 -04001843 } else if (phba->work_hs & HS_CRIT_TEMP) {
1844 temperature = readl(phba->MBslimaddr + TEMPERATURE_OFFSET);
1845 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
1846 temp_event_data.event_code = LPFC_CRIT_TEMP;
1847 temp_event_data.data = (uint32_t)temperature;
1848
Dick Kennedy372c1872020-06-30 14:50:00 -07001849 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartd7c255b2008-08-24 21:50:00 -04001850 "0406 Adapter maximum temperature exceeded "
James Smart57127f12007-10-27 13:37:05 -04001851 "(%ld), taking this port offline "
1852 "Data: x%x x%x x%x\n",
1853 temperature, phba->work_hs,
1854 phba->work_status[0], phba->work_status[1]);
1855
1856 shost = lpfc_shost_from_vport(phba->pport);
1857 fc_host_post_vendor_event(shost, fc_get_event_number(),
1858 sizeof(temp_event_data),
1859 (char *) &temp_event_data,
1860 SCSI_NL_VID_TYPE_PCI
1861 | PCI_VENDOR_ID_EMULEX);
1862
James Smart7af67052007-10-27 13:38:11 -04001863 spin_lock_irq(&phba->hbalock);
James Smart7af67052007-10-27 13:38:11 -04001864 phba->over_temp_state = HBA_OVER_TEMP;
1865 spin_unlock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001866 lpfc_offline_eratt(phba);
James Smart57127f12007-10-27 13:37:05 -04001867
dea31012005-04-17 16:05:31 -05001868 } else {
1869 /* The if clause above forces this code path when the status
James Smart93996272008-08-24 21:50:30 -04001870 * failure is a value other than FFER6. Do not call the offline
1871 * twice. This is the adapter hardware error path.
dea31012005-04-17 16:05:31 -05001872 */
Dick Kennedy372c1872020-06-30 14:50:00 -07001873 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04001874 "0457 Adapter Hardware Error "
dea31012005-04-17 16:05:31 -05001875 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001876 phba->work_hs,
dea31012005-04-17 16:05:31 -05001877 phba->work_status[0], phba->work_status[1]);
1878
James Smartd2873e42006-08-18 17:46:43 -04001879 event_data = FC_REG_DUMP_EVENT;
James Smart92d7f7b2007-06-17 19:56:38 -05001880 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05001881 fc_host_post_vendor_event(shost, fc_get_event_number(),
James Smartd2873e42006-08-18 17:46:43 -04001882 sizeof(event_data), (char *) &event_data,
1883 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
1884
James Smart09372822008-01-11 01:52:54 -05001885 lpfc_offline_eratt(phba);
dea31012005-04-17 16:05:31 -05001886 }
James Smart93996272008-08-24 21:50:30 -04001887 return;
dea31012005-04-17 16:05:31 -05001888}
1889
James Smarte59058c2008-08-24 21:49:00 -04001890/**
James Smart618a5232012-06-12 13:54:36 -04001891 * lpfc_sli4_port_sta_fn_reset - The SLI4 function reset due to port status reg
1892 * @phba: pointer to lpfc hba data structure.
1893 * @mbx_action: flag for mailbox shutdown action.
Lee Jonesfe614ac2020-07-23 13:24:22 +01001894 * @en_rn_msg: send reset/port recovery message.
James Smart618a5232012-06-12 13:54:36 -04001895 * This routine is invoked to perform an SLI4 port PCI function reset in
1896 * response to port status register polling attention. It waits for port
1897 * status register (ERR, RDY, RN) bits before proceeding with function reset.
1898 * During this process, interrupt vectors are freed and later requested
1899 * for handling possible port resource change.
1900 **/
1901static int
James Smarte10b2022014-02-20 09:57:43 -05001902lpfc_sli4_port_sta_fn_reset(struct lpfc_hba *phba, int mbx_action,
1903 bool en_rn_msg)
James Smart618a5232012-06-12 13:54:36 -04001904{
1905 int rc;
1906 uint32_t intr_mode;
James Smarta9978e32021-07-07 11:43:43 -07001907 LPFC_MBOXQ_t *mboxq;
James Smart618a5232012-06-12 13:54:36 -04001908
James Smart27d6ac02018-02-22 08:18:42 -08001909 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) >=
James Smart65791f12016-07-06 12:35:56 -07001910 LPFC_SLI_INTF_IF_TYPE_2) {
1911 /*
1912 * On error status condition, driver need to wait for port
1913 * ready before performing reset.
1914 */
1915 rc = lpfc_sli4_pdev_status_reg_wait(phba);
James Smart0e916ee2016-07-06 12:36:06 -07001916 if (rc)
James Smart65791f12016-07-06 12:35:56 -07001917 return rc;
James Smart618a5232012-06-12 13:54:36 -04001918 }
James Smart0e916ee2016-07-06 12:36:06 -07001919
James Smart65791f12016-07-06 12:35:56 -07001920 /* need reset: attempt for port recovery */
1921 if (en_rn_msg)
James Smart0b3ad322021-01-04 10:02:39 -08001922 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart65791f12016-07-06 12:35:56 -07001923 "2887 Reset Needed: Attempting Port "
1924 "Recovery...\n");
James Smart3ba62162021-01-04 10:02:31 -08001925
1926 /* If we are no wait, the HBA has been reset and is not
James Smarta9978e32021-07-07 11:43:43 -07001927 * functional, thus we should clear
1928 * (LPFC_SLI_ACTIVE | LPFC_SLI_MBOX_ACTIVE) flags.
James Smart3ba62162021-01-04 10:02:31 -08001929 */
1930 if (mbx_action == LPFC_MBX_NO_WAIT) {
1931 spin_lock_irq(&phba->hbalock);
1932 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
James Smarta9978e32021-07-07 11:43:43 -07001933 if (phba->sli.mbox_active) {
1934 mboxq = phba->sli.mbox_active;
1935 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
1936 __lpfc_mbox_cmpl_put(phba, mboxq);
1937 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
1938 phba->sli.mbox_active = NULL;
1939 }
James Smart3ba62162021-01-04 10:02:31 -08001940 spin_unlock_irq(&phba->hbalock);
1941 }
1942
James Smart65791f12016-07-06 12:35:56 -07001943 lpfc_offline_prep(phba, mbx_action);
James Smartc00f62e2019-08-14 16:57:11 -07001944 lpfc_sli_flush_io_rings(phba);
James Smart65791f12016-07-06 12:35:56 -07001945 lpfc_offline(phba);
1946 /* release interrupt for possible resource change */
1947 lpfc_sli4_disable_intr(phba);
James Smart5a9eeff2018-11-29 16:09:32 -08001948 rc = lpfc_sli_brdrestart(phba);
1949 if (rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -07001950 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart5a9eeff2018-11-29 16:09:32 -08001951 "6309 Failed to restart board\n");
1952 return rc;
1953 }
James Smart65791f12016-07-06 12:35:56 -07001954 /* request and enable interrupt */
1955 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
1956 if (intr_mode == LPFC_INTR_ERROR) {
Dick Kennedy372c1872020-06-30 14:50:00 -07001957 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart65791f12016-07-06 12:35:56 -07001958 "3175 Failed to enable interrupt\n");
1959 return -EIO;
1960 }
1961 phba->intr_mode = intr_mode;
1962 rc = lpfc_online(phba);
1963 if (rc == 0)
1964 lpfc_unblock_mgmt_io(phba);
1965
James Smart618a5232012-06-12 13:54:36 -04001966 return rc;
1967}
1968
1969/**
James Smartda0436e2009-05-22 14:51:39 -04001970 * lpfc_handle_eratt_s4 - The SLI4 HBA hardware error handler
1971 * @phba: pointer to lpfc hba data structure.
1972 *
1973 * This routine is invoked to handle the SLI4 HBA hardware error attention
1974 * conditions.
1975 **/
1976static void
1977lpfc_handle_eratt_s4(struct lpfc_hba *phba)
1978{
1979 struct lpfc_vport *vport = phba->pport;
1980 uint32_t event_data;
1981 struct Scsi_Host *shost;
James Smart2fcee4b2010-12-15 17:57:46 -05001982 uint32_t if_type;
James Smart2e90f4b2011-12-13 13:22:37 -05001983 struct lpfc_register portstat_reg = {0};
1984 uint32_t reg_err1, reg_err2;
1985 uint32_t uerrlo_reg, uemasklo_reg;
James Smart65791f12016-07-06 12:35:56 -07001986 uint32_t smphr_port_status = 0, pci_rd_rc1, pci_rd_rc2;
James Smarte10b2022014-02-20 09:57:43 -05001987 bool en_rn_msg = true;
James Smart946727d2015-04-07 15:07:09 -04001988 struct temp_event temp_event_data;
James Smart65791f12016-07-06 12:35:56 -07001989 struct lpfc_register portsmphr_reg;
1990 int rc, i;
James Smartda0436e2009-05-22 14:51:39 -04001991
1992 /* If the pci channel is offline, ignore possible errors, since
1993 * we cannot communicate with the pci card anyway.
1994 */
James Smart32a93102019-03-12 16:30:13 -07001995 if (pci_channel_offline(phba->pcidev)) {
Dick Kennedy372c1872020-06-30 14:50:00 -07001996 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart32a93102019-03-12 16:30:13 -07001997 "3166 pci channel is offline\n");
James Smartda0436e2009-05-22 14:51:39 -04001998 return;
James Smart32a93102019-03-12 16:30:13 -07001999 }
James Smartda0436e2009-05-22 14:51:39 -04002000
James Smart65791f12016-07-06 12:35:56 -07002001 memset(&portsmphr_reg, 0, sizeof(portsmphr_reg));
James Smart2fcee4b2010-12-15 17:57:46 -05002002 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
2003 switch (if_type) {
2004 case LPFC_SLI_INTF_IF_TYPE_0:
James Smart2e90f4b2011-12-13 13:22:37 -05002005 pci_rd_rc1 = lpfc_readl(
2006 phba->sli4_hba.u.if_type0.UERRLOregaddr,
2007 &uerrlo_reg);
2008 pci_rd_rc2 = lpfc_readl(
2009 phba->sli4_hba.u.if_type0.UEMASKLOregaddr,
2010 &uemasklo_reg);
2011 /* consider PCI bus read error as pci_channel_offline */
2012 if (pci_rd_rc1 == -EIO && pci_rd_rc2 == -EIO)
2013 return;
James Smart65791f12016-07-06 12:35:56 -07002014 if (!(phba->hba_flag & HBA_RECOVERABLE_UE)) {
2015 lpfc_sli4_offline_eratt(phba);
2016 return;
2017 }
Dick Kennedy372c1872020-06-30 14:50:00 -07002018 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart65791f12016-07-06 12:35:56 -07002019 "7623 Checking UE recoverable");
2020
2021 for (i = 0; i < phba->sli4_hba.ue_to_sr / 1000; i++) {
2022 if (lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
2023 &portsmphr_reg.word0))
2024 continue;
2025
2026 smphr_port_status = bf_get(lpfc_port_smphr_port_status,
2027 &portsmphr_reg);
2028 if ((smphr_port_status & LPFC_PORT_SEM_MASK) ==
2029 LPFC_PORT_SEM_UE_RECOVERABLE)
2030 break;
2031 /*Sleep for 1Sec, before checking SEMAPHORE */
2032 msleep(1000);
2033 }
2034
Dick Kennedy372c1872020-06-30 14:50:00 -07002035 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart65791f12016-07-06 12:35:56 -07002036 "4827 smphr_port_status x%x : Waited %dSec",
2037 smphr_port_status, i);
2038
2039 /* Recoverable UE, reset the HBA device */
2040 if ((smphr_port_status & LPFC_PORT_SEM_MASK) ==
2041 LPFC_PORT_SEM_UE_RECOVERABLE) {
2042 for (i = 0; i < 20; i++) {
2043 msleep(1000);
2044 if (!lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
2045 &portsmphr_reg.word0) &&
2046 (LPFC_POST_STAGE_PORT_READY ==
2047 bf_get(lpfc_port_smphr_port_status,
2048 &portsmphr_reg))) {
2049 rc = lpfc_sli4_port_sta_fn_reset(phba,
2050 LPFC_MBX_NO_WAIT, en_rn_msg);
2051 if (rc == 0)
2052 return;
Dick Kennedy372c1872020-06-30 14:50:00 -07002053 lpfc_printf_log(phba, KERN_ERR,
2054 LOG_TRACE_EVENT,
James Smart65791f12016-07-06 12:35:56 -07002055 "4215 Failed to recover UE");
2056 break;
2057 }
2058 }
2059 }
Dick Kennedy372c1872020-06-30 14:50:00 -07002060 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart65791f12016-07-06 12:35:56 -07002061 "7624 Firmware not ready: Failing UE recovery,"
2062 " waited %dSec", i);
James Smart8c24a4f2019-08-14 16:56:53 -07002063 phba->link_state = LPFC_HBA_ERROR;
James Smart2fcee4b2010-12-15 17:57:46 -05002064 break;
James Smart946727d2015-04-07 15:07:09 -04002065
James Smart2fcee4b2010-12-15 17:57:46 -05002066 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart27d6ac02018-02-22 08:18:42 -08002067 case LPFC_SLI_INTF_IF_TYPE_6:
James Smart2e90f4b2011-12-13 13:22:37 -05002068 pci_rd_rc1 = lpfc_readl(
2069 phba->sli4_hba.u.if_type2.STATUSregaddr,
2070 &portstat_reg.word0);
2071 /* consider PCI bus read error as pci_channel_offline */
James Smart6b5151f2012-01-18 16:24:06 -05002072 if (pci_rd_rc1 == -EIO) {
Dick Kennedy372c1872020-06-30 14:50:00 -07002073 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart6b5151f2012-01-18 16:24:06 -05002074 "3151 PCI bus read access failure: x%x\n",
2075 readl(phba->sli4_hba.u.if_type2.STATUSregaddr));
James Smart32a93102019-03-12 16:30:13 -07002076 lpfc_sli4_offline_eratt(phba);
James Smart2e90f4b2011-12-13 13:22:37 -05002077 return;
James Smart6b5151f2012-01-18 16:24:06 -05002078 }
James Smart2e90f4b2011-12-13 13:22:37 -05002079 reg_err1 = readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
2080 reg_err2 = readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
James Smart2fcee4b2010-12-15 17:57:46 -05002081 if (bf_get(lpfc_sliport_status_oti, &portstat_reg)) {
Dick Kennedy372c1872020-06-30 14:50:00 -07002082 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2083 "2889 Port Overtemperature event, "
2084 "taking port offline Data: x%x x%x\n",
2085 reg_err1, reg_err2);
James Smart946727d2015-04-07 15:07:09 -04002086
James Smart310429e2016-07-06 12:35:54 -07002087 phba->sfp_alarm |= LPFC_TRANSGRESSION_HIGH_TEMPERATURE;
James Smart946727d2015-04-07 15:07:09 -04002088 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
2089 temp_event_data.event_code = LPFC_CRIT_TEMP;
2090 temp_event_data.data = 0xFFFFFFFF;
2091
2092 shost = lpfc_shost_from_vport(phba->pport);
2093 fc_host_post_vendor_event(shost, fc_get_event_number(),
2094 sizeof(temp_event_data),
2095 (char *)&temp_event_data,
2096 SCSI_NL_VID_TYPE_PCI
2097 | PCI_VENDOR_ID_EMULEX);
2098
James Smart2fcee4b2010-12-15 17:57:46 -05002099 spin_lock_irq(&phba->hbalock);
2100 phba->over_temp_state = HBA_OVER_TEMP;
2101 spin_unlock_irq(&phba->hbalock);
2102 lpfc_sli4_offline_eratt(phba);
James Smart946727d2015-04-07 15:07:09 -04002103 return;
James Smart2fcee4b2010-12-15 17:57:46 -05002104 }
James Smart2e90f4b2011-12-13 13:22:37 -05002105 if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
James Smarte10b2022014-02-20 09:57:43 -05002106 reg_err2 == SLIPORT_ERR2_REG_FW_RESTART) {
Dick Kennedy372c1872020-06-30 14:50:00 -07002107 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarte10b2022014-02-20 09:57:43 -05002108 "3143 Port Down: Firmware Update "
2109 "Detected\n");
2110 en_rn_msg = false;
2111 } else if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
James Smart2e90f4b2011-12-13 13:22:37 -05002112 reg_err2 == SLIPORT_ERR2_REG_FORCED_DUMP)
Dick Kennedy372c1872020-06-30 14:50:00 -07002113 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2e90f4b2011-12-13 13:22:37 -05002114 "3144 Port Down: Debug Dump\n");
2115 else if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
2116 reg_err2 == SLIPORT_ERR2_REG_FUNC_PROVISON)
Dick Kennedy372c1872020-06-30 14:50:00 -07002117 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2e90f4b2011-12-13 13:22:37 -05002118 "3145 Port Down: Provisioning\n");
James Smart618a5232012-06-12 13:54:36 -04002119
James Smart946727d2015-04-07 15:07:09 -04002120 /* If resets are disabled then leave the HBA alone and return */
2121 if (!phba->cfg_enable_hba_reset)
2122 return;
2123
James Smart618a5232012-06-12 13:54:36 -04002124 /* Check port status register for function reset */
James Smarte10b2022014-02-20 09:57:43 -05002125 rc = lpfc_sli4_port_sta_fn_reset(phba, LPFC_MBX_NO_WAIT,
2126 en_rn_msg);
James Smart618a5232012-06-12 13:54:36 -04002127 if (rc == 0) {
2128 /* don't report event on forced debug dump */
2129 if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
2130 reg_err2 == SLIPORT_ERR2_REG_FORCED_DUMP)
2131 return;
2132 else
2133 break;
James Smart2fcee4b2010-12-15 17:57:46 -05002134 }
James Smart618a5232012-06-12 13:54:36 -04002135 /* fall through for not able to recover */
Dick Kennedy372c1872020-06-30 14:50:00 -07002136 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart8c24a4f2019-08-14 16:56:53 -07002137 "3152 Unrecoverable error\n");
2138 phba->link_state = LPFC_HBA_ERROR;
James Smart2fcee4b2010-12-15 17:57:46 -05002139 break;
2140 case LPFC_SLI_INTF_IF_TYPE_1:
2141 default:
2142 break;
2143 }
James Smart2e90f4b2011-12-13 13:22:37 -05002144 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
2145 "3123 Report dump event to upper layer\n");
2146 /* Send an internal error event to mgmt application */
2147 lpfc_board_errevt_to_mgmt(phba);
2148
2149 event_data = FC_REG_DUMP_EVENT;
2150 shost = lpfc_shost_from_vport(vport);
2151 fc_host_post_vendor_event(shost, fc_get_event_number(),
2152 sizeof(event_data), (char *) &event_data,
2153 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
James Smartda0436e2009-05-22 14:51:39 -04002154}
2155
2156/**
2157 * lpfc_handle_eratt - Wrapper func for handling hba error attention
2158 * @phba: pointer to lpfc HBA data structure.
2159 *
2160 * This routine wraps the actual SLI3 or SLI4 hba error attention handling
2161 * routine from the API jump table function pointer from the lpfc_hba struct.
2162 *
2163 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002164 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -04002165 * Any other value - error.
2166 **/
2167void
2168lpfc_handle_eratt(struct lpfc_hba *phba)
2169{
2170 (*phba->lpfc_handle_eratt)(phba);
2171}
2172
2173/**
James Smart3621a712009-04-06 18:47:14 -04002174 * lpfc_handle_latt - The HBA link event handler
James Smarte59058c2008-08-24 21:49:00 -04002175 * @phba: pointer to lpfc hba data structure.
2176 *
2177 * This routine is invoked from the worker thread to handle a HBA host
James Smart895427b2017-02-12 13:52:30 -08002178 * attention link event. SLI3 only.
James Smarte59058c2008-08-24 21:49:00 -04002179 **/
dea31012005-04-17 16:05:31 -05002180void
James Smart2e0fef82007-06-17 19:56:36 -05002181lpfc_handle_latt(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002182{
James Smart2e0fef82007-06-17 19:56:36 -05002183 struct lpfc_vport *vport = phba->pport;
2184 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05002185 LPFC_MBOXQ_t *pmb;
2186 volatile uint32_t control;
2187 struct lpfc_dmabuf *mp;
James Smart09372822008-01-11 01:52:54 -05002188 int rc = 0;
dea31012005-04-17 16:05:31 -05002189
2190 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart09372822008-01-11 01:52:54 -05002191 if (!pmb) {
2192 rc = 1;
dea31012005-04-17 16:05:31 -05002193 goto lpfc_handle_latt_err_exit;
James Smart09372822008-01-11 01:52:54 -05002194 }
dea31012005-04-17 16:05:31 -05002195
2196 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
James Smart09372822008-01-11 01:52:54 -05002197 if (!mp) {
2198 rc = 2;
dea31012005-04-17 16:05:31 -05002199 goto lpfc_handle_latt_free_pmb;
James Smart09372822008-01-11 01:52:54 -05002200 }
dea31012005-04-17 16:05:31 -05002201
2202 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
James Smart09372822008-01-11 01:52:54 -05002203 if (!mp->virt) {
2204 rc = 3;
dea31012005-04-17 16:05:31 -05002205 goto lpfc_handle_latt_free_mp;
James Smart09372822008-01-11 01:52:54 -05002206 }
dea31012005-04-17 16:05:31 -05002207
James.Smart@Emulex.Com6281bfe2005-11-28 11:41:33 -05002208 /* Cleanup any outstanding ELS commands */
James Smart549e55c2007-08-02 11:09:51 -04002209 lpfc_els_flush_all_cmd(phba);
dea31012005-04-17 16:05:31 -05002210
2211 psli->slistat.link_event++;
James Smart76a95d72010-11-20 23:11:48 -05002212 lpfc_read_topology(phba, pmb, mp);
2213 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
James Smart2e0fef82007-06-17 19:56:36 -05002214 pmb->vport = vport;
James Smart0d2b6b82008-06-14 22:52:47 -04002215 /* Block ELS IOCBs until we have processed this mbox command */
James Smart895427b2017-02-12 13:52:30 -08002216 phba->sli.sli3_ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
James Smart0b727fe2007-10-27 13:37:25 -04002217 rc = lpfc_sli_issue_mbox (phba, pmb, MBX_NOWAIT);
James Smart09372822008-01-11 01:52:54 -05002218 if (rc == MBX_NOT_FINISHED) {
2219 rc = 4;
James Smart14691152006-12-02 13:34:28 -05002220 goto lpfc_handle_latt_free_mbuf;
James Smart09372822008-01-11 01:52:54 -05002221 }
dea31012005-04-17 16:05:31 -05002222
2223 /* Clear Link Attention in HA REG */
James Smart2e0fef82007-06-17 19:56:36 -05002224 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002225 writel(HA_LATT, phba->HAregaddr);
2226 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05002227 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002228
2229 return;
2230
James Smart14691152006-12-02 13:34:28 -05002231lpfc_handle_latt_free_mbuf:
James Smart895427b2017-02-12 13:52:30 -08002232 phba->sli.sli3_ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart14691152006-12-02 13:34:28 -05002233 lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea31012005-04-17 16:05:31 -05002234lpfc_handle_latt_free_mp:
2235 kfree(mp);
2236lpfc_handle_latt_free_pmb:
James Smart1dcb58e2007-04-25 09:51:30 -04002237 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002238lpfc_handle_latt_err_exit:
2239 /* Enable Link attention interrupts */
James Smart2e0fef82007-06-17 19:56:36 -05002240 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002241 psli->sli_flag |= LPFC_PROCESS_LA;
2242 control = readl(phba->HCregaddr);
2243 control |= HC_LAINT_ENA;
2244 writel(control, phba->HCregaddr);
2245 readl(phba->HCregaddr); /* flush */
2246
2247 /* Clear Link Attention in HA REG */
2248 writel(HA_LATT, phba->HAregaddr);
2249 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05002250 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002251 lpfc_linkdown(phba);
James Smart2e0fef82007-06-17 19:56:36 -05002252 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002253
Dick Kennedy372c1872020-06-30 14:50:00 -07002254 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2255 "0300 LATT: Cannot issue READ_LA: Data:%d\n", rc);
dea31012005-04-17 16:05:31 -05002256
2257 return;
2258}
2259
James Smarte59058c2008-08-24 21:49:00 -04002260/**
James Smart3621a712009-04-06 18:47:14 -04002261 * lpfc_parse_vpd - Parse VPD (Vital Product Data)
James Smarte59058c2008-08-24 21:49:00 -04002262 * @phba: pointer to lpfc hba data structure.
2263 * @vpd: pointer to the vital product data.
2264 * @len: length of the vital product data in bytes.
2265 *
2266 * This routine parses the Vital Product Data (VPD). The VPD is treated as
2267 * an array of characters. In this routine, the ModelName, ProgramType, and
2268 * ModelDesc, etc. fields of the phba data structure will be populated.
2269 *
2270 * Return codes
2271 * 0 - pointer to the VPD passed in is NULL
2272 * 1 - success
2273 **/
James Smart3772a992009-05-22 14:50:54 -04002274int
James Smart2e0fef82007-06-17 19:56:36 -05002275lpfc_parse_vpd(struct lpfc_hba *phba, uint8_t *vpd, int len)
dea31012005-04-17 16:05:31 -05002276{
2277 uint8_t lenlo, lenhi;
Anton Blanchard07da60c2007-03-21 08:41:47 -05002278 int Length;
dea31012005-04-17 16:05:31 -05002279 int i, j;
2280 int finished = 0;
2281 int index = 0;
2282
2283 if (!vpd)
2284 return 0;
2285
2286 /* Vital Product */
James Smarted957682007-06-17 19:56:37 -05002287 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002288 "0455 Vital Product Data: x%x x%x x%x x%x\n",
dea31012005-04-17 16:05:31 -05002289 (uint32_t) vpd[0], (uint32_t) vpd[1], (uint32_t) vpd[2],
2290 (uint32_t) vpd[3]);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002291 while (!finished && (index < (len - 4))) {
dea31012005-04-17 16:05:31 -05002292 switch (vpd[index]) {
2293 case 0x82:
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002294 case 0x91:
dea31012005-04-17 16:05:31 -05002295 index += 1;
2296 lenlo = vpd[index];
2297 index += 1;
2298 lenhi = vpd[index];
2299 index += 1;
2300 i = ((((unsigned short)lenhi) << 8) + lenlo);
2301 index += i;
2302 break;
2303 case 0x90:
2304 index += 1;
2305 lenlo = vpd[index];
2306 index += 1;
2307 lenhi = vpd[index];
2308 index += 1;
2309 Length = ((((unsigned short)lenhi) << 8) + lenlo);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002310 if (Length > len - index)
2311 Length = len - index;
dea31012005-04-17 16:05:31 -05002312 while (Length > 0) {
2313 /* Look for Serial Number */
2314 if ((vpd[index] == 'S') && (vpd[index+1] == 'N')) {
2315 index += 2;
2316 i = vpd[index];
2317 index += 1;
2318 j = 0;
2319 Length -= (3+i);
2320 while(i--) {
2321 phba->SerialNumber[j++] = vpd[index++];
2322 if (j == 31)
2323 break;
2324 }
2325 phba->SerialNumber[j] = 0;
2326 continue;
2327 }
2328 else if ((vpd[index] == 'V') && (vpd[index+1] == '1')) {
2329 phba->vpd_flag |= VPD_MODEL_DESC;
2330 index += 2;
2331 i = vpd[index];
2332 index += 1;
2333 j = 0;
2334 Length -= (3+i);
2335 while(i--) {
2336 phba->ModelDesc[j++] = vpd[index++];
2337 if (j == 255)
2338 break;
2339 }
2340 phba->ModelDesc[j] = 0;
2341 continue;
2342 }
2343 else if ((vpd[index] == 'V') && (vpd[index+1] == '2')) {
2344 phba->vpd_flag |= VPD_MODEL_NAME;
2345 index += 2;
2346 i = vpd[index];
2347 index += 1;
2348 j = 0;
2349 Length -= (3+i);
2350 while(i--) {
2351 phba->ModelName[j++] = vpd[index++];
2352 if (j == 79)
2353 break;
2354 }
2355 phba->ModelName[j] = 0;
2356 continue;
2357 }
2358 else if ((vpd[index] == 'V') && (vpd[index+1] == '3')) {
2359 phba->vpd_flag |= VPD_PROGRAM_TYPE;
2360 index += 2;
2361 i = vpd[index];
2362 index += 1;
2363 j = 0;
2364 Length -= (3+i);
2365 while(i--) {
2366 phba->ProgramType[j++] = vpd[index++];
2367 if (j == 255)
2368 break;
2369 }
2370 phba->ProgramType[j] = 0;
2371 continue;
2372 }
2373 else if ((vpd[index] == 'V') && (vpd[index+1] == '4')) {
2374 phba->vpd_flag |= VPD_PORT;
2375 index += 2;
2376 i = vpd[index];
2377 index += 1;
2378 j = 0;
2379 Length -= (3+i);
2380 while(i--) {
James Smartcd1c8302011-10-10 21:33:25 -04002381 if ((phba->sli_rev == LPFC_SLI_REV4) &&
2382 (phba->sli4_hba.pport_name_sta ==
2383 LPFC_SLI4_PPNAME_GET)) {
2384 j++;
2385 index++;
2386 } else
2387 phba->Port[j++] = vpd[index++];
2388 if (j == 19)
2389 break;
dea31012005-04-17 16:05:31 -05002390 }
James Smartcd1c8302011-10-10 21:33:25 -04002391 if ((phba->sli_rev != LPFC_SLI_REV4) ||
2392 (phba->sli4_hba.pport_name_sta ==
2393 LPFC_SLI4_PPNAME_NON))
2394 phba->Port[j] = 0;
dea31012005-04-17 16:05:31 -05002395 continue;
2396 }
2397 else {
2398 index += 2;
2399 i = vpd[index];
2400 index += 1;
2401 index += i;
2402 Length -= (3 + i);
2403 }
2404 }
2405 finished = 0;
2406 break;
2407 case 0x78:
2408 finished = 1;
2409 break;
2410 default:
2411 index ++;
2412 break;
2413 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002414 }
dea31012005-04-17 16:05:31 -05002415
2416 return(1);
2417}
2418
James Smarte59058c2008-08-24 21:49:00 -04002419/**
James Smart3621a712009-04-06 18:47:14 -04002420 * lpfc_get_hba_model_desc - Retrieve HBA device model name and description
James Smarte59058c2008-08-24 21:49:00 -04002421 * @phba: pointer to lpfc hba data structure.
2422 * @mdp: pointer to the data structure to hold the derived model name.
2423 * @descp: pointer to the data structure to hold the derived description.
2424 *
2425 * This routine retrieves HBA's description based on its registered PCI device
2426 * ID. The @descp passed into this function points to an array of 256 chars. It
2427 * shall be returned with the model name, maximum speed, and the host bus type.
2428 * The @mdp passed into this function points to an array of 80 chars. When the
2429 * function returns, the @mdp will be filled with the model name.
2430 **/
dea31012005-04-17 16:05:31 -05002431static void
James Smart2e0fef82007-06-17 19:56:36 -05002432lpfc_get_hba_model_desc(struct lpfc_hba *phba, uint8_t *mdp, uint8_t *descp)
dea31012005-04-17 16:05:31 -05002433{
2434 lpfc_vpd_t *vp;
James.Smart@Emulex.Comfefcb2b2005-11-28 15:08:56 -05002435 uint16_t dev_id = phba->pcidev->device;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002436 int max_speed;
James Smart84774a42008-08-24 21:50:06 -04002437 int GE = 0;
James Smartda0436e2009-05-22 14:51:39 -04002438 int oneConnect = 0; /* default is not a oneConnect */
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002439 struct {
James Smarta747c9c2009-11-18 15:41:10 -05002440 char *name;
2441 char *bus;
2442 char *function;
2443 } m = {"<Unknown>", "", ""};
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002444
2445 if (mdp && mdp[0] != '\0'
2446 && descp && descp[0] != '\0')
2447 return;
2448
James Smartfbd8a6b2018-02-22 08:18:45 -08002449 if (phba->lmt & LMT_64Gb)
2450 max_speed = 64;
2451 else if (phba->lmt & LMT_32Gb)
James Smartd38dd522015-08-31 16:48:17 -04002452 max_speed = 32;
2453 else if (phba->lmt & LMT_16Gb)
James Smartc0c11512011-05-24 11:41:34 -04002454 max_speed = 16;
2455 else if (phba->lmt & LMT_10Gb)
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002456 max_speed = 10;
2457 else if (phba->lmt & LMT_8Gb)
2458 max_speed = 8;
2459 else if (phba->lmt & LMT_4Gb)
2460 max_speed = 4;
2461 else if (phba->lmt & LMT_2Gb)
2462 max_speed = 2;
James Smart4169d862012-09-29 11:32:09 -04002463 else if (phba->lmt & LMT_1Gb)
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002464 max_speed = 1;
James Smart4169d862012-09-29 11:32:09 -04002465 else
2466 max_speed = 0;
dea31012005-04-17 16:05:31 -05002467
2468 vp = &phba->vpd;
dea31012005-04-17 16:05:31 -05002469
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002470 switch (dev_id) {
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04002471 case PCI_DEVICE_ID_FIREFLY:
James Smart12222f42014-05-21 08:05:19 -04002472 m = (typeof(m)){"LP6000", "PCI",
2473 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04002474 break;
dea31012005-04-17 16:05:31 -05002475 case PCI_DEVICE_ID_SUPERFLY:
2476 if (vp->rev.biuRev >= 1 && vp->rev.biuRev <= 3)
James Smart12222f42014-05-21 08:05:19 -04002477 m = (typeof(m)){"LP7000", "PCI", ""};
dea31012005-04-17 16:05:31 -05002478 else
James Smart12222f42014-05-21 08:05:19 -04002479 m = (typeof(m)){"LP7000E", "PCI", ""};
2480 m.function = "Obsolete, Unsupported Fibre Channel Adapter";
dea31012005-04-17 16:05:31 -05002481 break;
2482 case PCI_DEVICE_ID_DRAGONFLY:
James Smarta747c9c2009-11-18 15:41:10 -05002483 m = (typeof(m)){"LP8000", "PCI",
James Smart12222f42014-05-21 08:05:19 -04002484 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002485 break;
2486 case PCI_DEVICE_ID_CENTAUR:
2487 if (FC_JEDEC_ID(vp->rev.biuRev) == CENTAUR_2G_JEDEC_ID)
James Smart12222f42014-05-21 08:05:19 -04002488 m = (typeof(m)){"LP9002", "PCI", ""};
dea31012005-04-17 16:05:31 -05002489 else
James Smart12222f42014-05-21 08:05:19 -04002490 m = (typeof(m)){"LP9000", "PCI", ""};
2491 m.function = "Obsolete, Unsupported Fibre Channel Adapter";
dea31012005-04-17 16:05:31 -05002492 break;
2493 case PCI_DEVICE_ID_RFLY:
James Smarta747c9c2009-11-18 15:41:10 -05002494 m = (typeof(m)){"LP952", "PCI",
James Smart12222f42014-05-21 08:05:19 -04002495 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002496 break;
2497 case PCI_DEVICE_ID_PEGASUS:
James Smarta747c9c2009-11-18 15:41:10 -05002498 m = (typeof(m)){"LP9802", "PCI-X",
James Smart12222f42014-05-21 08:05:19 -04002499 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002500 break;
2501 case PCI_DEVICE_ID_THOR:
James Smarta747c9c2009-11-18 15:41:10 -05002502 m = (typeof(m)){"LP10000", "PCI-X",
James Smart12222f42014-05-21 08:05:19 -04002503 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002504 break;
2505 case PCI_DEVICE_ID_VIPER:
James Smarta747c9c2009-11-18 15:41:10 -05002506 m = (typeof(m)){"LPX1000", "PCI-X",
James Smart12222f42014-05-21 08:05:19 -04002507 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002508 break;
2509 case PCI_DEVICE_ID_PFLY:
James Smarta747c9c2009-11-18 15:41:10 -05002510 m = (typeof(m)){"LP982", "PCI-X",
James Smart12222f42014-05-21 08:05:19 -04002511 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002512 break;
2513 case PCI_DEVICE_ID_TFLY:
James Smarta747c9c2009-11-18 15:41:10 -05002514 m = (typeof(m)){"LP1050", "PCI-X",
James Smart12222f42014-05-21 08:05:19 -04002515 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002516 break;
2517 case PCI_DEVICE_ID_HELIOS:
James Smarta747c9c2009-11-18 15:41:10 -05002518 m = (typeof(m)){"LP11000", "PCI-X2",
James Smart12222f42014-05-21 08:05:19 -04002519 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002520 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002521 case PCI_DEVICE_ID_HELIOS_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002522 m = (typeof(m)){"LP11000-SP", "PCI-X2",
James Smart12222f42014-05-21 08:05:19 -04002523 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002524 break;
2525 case PCI_DEVICE_ID_HELIOS_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002526 m = (typeof(m)){"LP11002-SP", "PCI-X2",
James Smart12222f42014-05-21 08:05:19 -04002527 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002528 break;
2529 case PCI_DEVICE_ID_NEPTUNE:
James Smart12222f42014-05-21 08:05:19 -04002530 m = (typeof(m)){"LPe1000", "PCIe",
2531 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002532 break;
2533 case PCI_DEVICE_ID_NEPTUNE_SCSP:
James Smart12222f42014-05-21 08:05:19 -04002534 m = (typeof(m)){"LPe1000-SP", "PCIe",
2535 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002536 break;
2537 case PCI_DEVICE_ID_NEPTUNE_DCSP:
James Smart12222f42014-05-21 08:05:19 -04002538 m = (typeof(m)){"LPe1002-SP", "PCIe",
2539 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002540 break;
dea31012005-04-17 16:05:31 -05002541 case PCI_DEVICE_ID_BMID:
James Smarta747c9c2009-11-18 15:41:10 -05002542 m = (typeof(m)){"LP1150", "PCI-X2", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002543 break;
2544 case PCI_DEVICE_ID_BSMB:
James Smart12222f42014-05-21 08:05:19 -04002545 m = (typeof(m)){"LP111", "PCI-X2",
2546 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002547 break;
2548 case PCI_DEVICE_ID_ZEPHYR:
James Smarta747c9c2009-11-18 15:41:10 -05002549 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002550 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002551 case PCI_DEVICE_ID_ZEPHYR_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002552 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002553 break;
2554 case PCI_DEVICE_ID_ZEPHYR_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002555 m = (typeof(m)){"LP2105", "PCIe", "FCoE Adapter"};
James Smarta257bf92009-04-06 18:48:10 -04002556 GE = 1;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002557 break;
dea31012005-04-17 16:05:31 -05002558 case PCI_DEVICE_ID_ZMID:
James Smarta747c9c2009-11-18 15:41:10 -05002559 m = (typeof(m)){"LPe1150", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002560 break;
2561 case PCI_DEVICE_ID_ZSMB:
James Smarta747c9c2009-11-18 15:41:10 -05002562 m = (typeof(m)){"LPe111", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002563 break;
2564 case PCI_DEVICE_ID_LP101:
James Smart12222f42014-05-21 08:05:19 -04002565 m = (typeof(m)){"LP101", "PCI-X",
2566 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002567 break;
2568 case PCI_DEVICE_ID_LP10000S:
James Smart12222f42014-05-21 08:05:19 -04002569 m = (typeof(m)){"LP10000-S", "PCI",
2570 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04002571 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002572 case PCI_DEVICE_ID_LP11000S:
James Smart12222f42014-05-21 08:05:19 -04002573 m = (typeof(m)){"LP11000-S", "PCI-X2",
2574 "Obsolete, Unsupported Fibre Channel Adapter"};
James Smart18a3b592006-12-02 13:35:08 -05002575 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002576 case PCI_DEVICE_ID_LPE11000S:
James Smart12222f42014-05-21 08:05:19 -04002577 m = (typeof(m)){"LPe11000-S", "PCIe",
2578 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Com5cc36b32005-11-28 11:42:19 -05002579 break;
James Smartb87eab32007-04-25 09:53:28 -04002580 case PCI_DEVICE_ID_SAT:
James Smarta747c9c2009-11-18 15:41:10 -05002581 m = (typeof(m)){"LPe12000", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002582 break;
2583 case PCI_DEVICE_ID_SAT_MID:
James Smarta747c9c2009-11-18 15:41:10 -05002584 m = (typeof(m)){"LPe1250", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002585 break;
2586 case PCI_DEVICE_ID_SAT_SMB:
James Smarta747c9c2009-11-18 15:41:10 -05002587 m = (typeof(m)){"LPe121", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002588 break;
2589 case PCI_DEVICE_ID_SAT_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002590 m = (typeof(m)){"LPe12002-SP", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002591 break;
2592 case PCI_DEVICE_ID_SAT_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002593 m = (typeof(m)){"LPe12000-SP", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002594 break;
2595 case PCI_DEVICE_ID_SAT_S:
James Smarta747c9c2009-11-18 15:41:10 -05002596 m = (typeof(m)){"LPe12000-S", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002597 break;
James Smart84774a42008-08-24 21:50:06 -04002598 case PCI_DEVICE_ID_HORNET:
James Smart12222f42014-05-21 08:05:19 -04002599 m = (typeof(m)){"LP21000", "PCIe",
2600 "Obsolete, Unsupported FCoE Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002601 GE = 1;
2602 break;
2603 case PCI_DEVICE_ID_PROTEUS_VF:
James Smarta747c9c2009-11-18 15:41:10 -05002604 m = (typeof(m)){"LPev12000", "PCIe IOV",
James Smart12222f42014-05-21 08:05:19 -04002605 "Obsolete, Unsupported Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002606 break;
2607 case PCI_DEVICE_ID_PROTEUS_PF:
James Smarta747c9c2009-11-18 15:41:10 -05002608 m = (typeof(m)){"LPev12000", "PCIe IOV",
James Smart12222f42014-05-21 08:05:19 -04002609 "Obsolete, Unsupported Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002610 break;
2611 case PCI_DEVICE_ID_PROTEUS_S:
James Smarta747c9c2009-11-18 15:41:10 -05002612 m = (typeof(m)){"LPemv12002-S", "PCIe IOV",
James Smart12222f42014-05-21 08:05:19 -04002613 "Obsolete, Unsupported Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002614 break;
James Smartda0436e2009-05-22 14:51:39 -04002615 case PCI_DEVICE_ID_TIGERSHARK:
2616 oneConnect = 1;
James Smarta747c9c2009-11-18 15:41:10 -05002617 m = (typeof(m)){"OCe10100", "PCIe", "FCoE"};
James Smartda0436e2009-05-22 14:51:39 -04002618 break;
James Smarta747c9c2009-11-18 15:41:10 -05002619 case PCI_DEVICE_ID_TOMCAT:
James Smart6669f9b2009-10-02 15:16:45 -04002620 oneConnect = 1;
James Smarta747c9c2009-11-18 15:41:10 -05002621 m = (typeof(m)){"OCe11100", "PCIe", "FCoE"};
2622 break;
2623 case PCI_DEVICE_ID_FALCON:
2624 m = (typeof(m)){"LPSe12002-ML1-E", "PCIe",
2625 "EmulexSecure Fibre"};
James Smart6669f9b2009-10-02 15:16:45 -04002626 break;
James Smart98fc5dd2010-06-07 15:24:29 -04002627 case PCI_DEVICE_ID_BALIUS:
2628 m = (typeof(m)){"LPVe12002", "PCIe Shared I/O",
James Smart12222f42014-05-21 08:05:19 -04002629 "Obsolete, Unsupported Fibre Channel Adapter"};
James Smart98fc5dd2010-06-07 15:24:29 -04002630 break;
James Smart085c6472010-11-20 23:11:37 -05002631 case PCI_DEVICE_ID_LANCER_FC:
James Smartc0c11512011-05-24 11:41:34 -04002632 m = (typeof(m)){"LPe16000", "PCIe", "Fibre Channel Adapter"};
James Smart085c6472010-11-20 23:11:37 -05002633 break;
James Smart12222f42014-05-21 08:05:19 -04002634 case PCI_DEVICE_ID_LANCER_FC_VF:
2635 m = (typeof(m)){"LPe16000", "PCIe",
2636 "Obsolete, Unsupported Fibre Channel Adapter"};
2637 break;
James Smart085c6472010-11-20 23:11:37 -05002638 case PCI_DEVICE_ID_LANCER_FCOE:
2639 oneConnect = 1;
James Smart079b5c92011-08-21 21:48:49 -04002640 m = (typeof(m)){"OCe15100", "PCIe", "FCoE"};
James Smart085c6472010-11-20 23:11:37 -05002641 break;
James Smart12222f42014-05-21 08:05:19 -04002642 case PCI_DEVICE_ID_LANCER_FCOE_VF:
2643 oneConnect = 1;
2644 m = (typeof(m)){"OCe15100", "PCIe",
2645 "Obsolete, Unsupported FCoE"};
2646 break;
James Smartd38dd522015-08-31 16:48:17 -04002647 case PCI_DEVICE_ID_LANCER_G6_FC:
2648 m = (typeof(m)){"LPe32000", "PCIe", "Fibre Channel Adapter"};
2649 break;
James Smartc238b9b2018-02-22 08:18:44 -08002650 case PCI_DEVICE_ID_LANCER_G7_FC:
2651 m = (typeof(m)){"LPe36000", "PCIe", "Fibre Channel Adapter"};
2652 break;
James Smartf449a3d2021-07-22 15:17:16 -07002653 case PCI_DEVICE_ID_LANCER_G7P_FC:
2654 m = (typeof(m)){"LPe38000", "PCIe", "Fibre Channel Adapter"};
2655 break;
James Smartf8cafd32012-08-03 12:42:51 -04002656 case PCI_DEVICE_ID_SKYHAWK:
2657 case PCI_DEVICE_ID_SKYHAWK_VF:
2658 oneConnect = 1;
2659 m = (typeof(m)){"OCe14000", "PCIe", "FCoE"};
2660 break;
James.Smart@Emulex.Com5cc36b32005-11-28 11:42:19 -05002661 default:
James Smarta747c9c2009-11-18 15:41:10 -05002662 m = (typeof(m)){"Unknown", "", ""};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002663 break;
dea31012005-04-17 16:05:31 -05002664 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002665
2666 if (mdp && mdp[0] == '\0')
2667 snprintf(mdp, 79,"%s", m.name);
James Smartc0c11512011-05-24 11:41:34 -04002668 /*
2669 * oneConnect hba requires special processing, they are all initiators
James Smartda0436e2009-05-22 14:51:39 -04002670 * and we put the port number on the end
2671 */
2672 if (descp && descp[0] == '\0') {
2673 if (oneConnect)
2674 snprintf(descp, 255,
James Smart4169d862012-09-29 11:32:09 -04002675 "Emulex OneConnect %s, %s Initiator %s",
James Smarta747c9c2009-11-18 15:41:10 -05002676 m.name, m.function,
James Smartda0436e2009-05-22 14:51:39 -04002677 phba->Port);
James Smart4169d862012-09-29 11:32:09 -04002678 else if (max_speed == 0)
2679 snprintf(descp, 255,
Sebastian Herbszt290237d2015-08-31 16:48:08 -04002680 "Emulex %s %s %s",
James Smart4169d862012-09-29 11:32:09 -04002681 m.name, m.bus, m.function);
James Smartda0436e2009-05-22 14:51:39 -04002682 else
2683 snprintf(descp, 255,
2684 "Emulex %s %d%s %s %s",
James Smarta747c9c2009-11-18 15:41:10 -05002685 m.name, max_speed, (GE) ? "GE" : "Gb",
2686 m.bus, m.function);
James Smartda0436e2009-05-22 14:51:39 -04002687 }
dea31012005-04-17 16:05:31 -05002688}
2689
James Smarte59058c2008-08-24 21:49:00 -04002690/**
James Smart3621a712009-04-06 18:47:14 -04002691 * lpfc_post_buffer - Post IOCB(s) with DMA buffer descriptor(s) to a IOCB ring
James Smarte59058c2008-08-24 21:49:00 -04002692 * @phba: pointer to lpfc hba data structure.
2693 * @pring: pointer to a IOCB ring.
2694 * @cnt: the number of IOCBs to be posted to the IOCB ring.
2695 *
2696 * This routine posts a given number of IOCBs with the associated DMA buffer
2697 * descriptors specified by the cnt argument to the given IOCB ring.
2698 *
2699 * Return codes
2700 * The number of IOCBs NOT able to be posted to the IOCB ring.
2701 **/
dea31012005-04-17 16:05:31 -05002702int
James Smart495a7142008-06-14 22:52:59 -04002703lpfc_post_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring, int cnt)
dea31012005-04-17 16:05:31 -05002704{
2705 IOCB_t *icmd;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002706 struct lpfc_iocbq *iocb;
dea31012005-04-17 16:05:31 -05002707 struct lpfc_dmabuf *mp1, *mp2;
2708
2709 cnt += pring->missbufcnt;
2710
2711 /* While there are buffers to post */
2712 while (cnt > 0) {
2713 /* Allocate buffer for command iocb */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002714 iocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05002715 if (iocb == NULL) {
2716 pring->missbufcnt = cnt;
2717 return cnt;
2718 }
dea31012005-04-17 16:05:31 -05002719 icmd = &iocb->iocb;
2720
2721 /* 2 buffers can be posted per command */
2722 /* Allocate buffer to post */
2723 mp1 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
2724 if (mp1)
James Smart98c9ea52007-10-27 13:37:33 -04002725 mp1->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &mp1->phys);
2726 if (!mp1 || !mp1->virt) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002727 kfree(mp1);
James Bottomley604a3e32005-10-29 10:28:33 -05002728 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002729 pring->missbufcnt = cnt;
2730 return cnt;
2731 }
2732
2733 INIT_LIST_HEAD(&mp1->list);
2734 /* Allocate buffer to post */
2735 if (cnt > 1) {
2736 mp2 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
2737 if (mp2)
2738 mp2->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
2739 &mp2->phys);
James Smart98c9ea52007-10-27 13:37:33 -04002740 if (!mp2 || !mp2->virt) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002741 kfree(mp2);
dea31012005-04-17 16:05:31 -05002742 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
2743 kfree(mp1);
James Bottomley604a3e32005-10-29 10:28:33 -05002744 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002745 pring->missbufcnt = cnt;
2746 return cnt;
2747 }
2748
2749 INIT_LIST_HEAD(&mp2->list);
2750 } else {
2751 mp2 = NULL;
2752 }
2753
2754 icmd->un.cont64[0].addrHigh = putPaddrHigh(mp1->phys);
2755 icmd->un.cont64[0].addrLow = putPaddrLow(mp1->phys);
2756 icmd->un.cont64[0].tus.f.bdeSize = FCELSSIZE;
2757 icmd->ulpBdeCount = 1;
2758 cnt--;
2759 if (mp2) {
2760 icmd->un.cont64[1].addrHigh = putPaddrHigh(mp2->phys);
2761 icmd->un.cont64[1].addrLow = putPaddrLow(mp2->phys);
2762 icmd->un.cont64[1].tus.f.bdeSize = FCELSSIZE;
2763 cnt--;
2764 icmd->ulpBdeCount = 2;
2765 }
2766
2767 icmd->ulpCommand = CMD_QUE_RING_BUF64_CN;
2768 icmd->ulpLe = 1;
2769
James Smart3772a992009-05-22 14:50:54 -04002770 if (lpfc_sli_issue_iocb(phba, pring->ringno, iocb, 0) ==
2771 IOCB_ERROR) {
dea31012005-04-17 16:05:31 -05002772 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
2773 kfree(mp1);
2774 cnt++;
2775 if (mp2) {
2776 lpfc_mbuf_free(phba, mp2->virt, mp2->phys);
2777 kfree(mp2);
2778 cnt++;
2779 }
James Bottomley604a3e32005-10-29 10:28:33 -05002780 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002781 pring->missbufcnt = cnt;
dea31012005-04-17 16:05:31 -05002782 return cnt;
2783 }
dea31012005-04-17 16:05:31 -05002784 lpfc_sli_ringpostbuf_put(phba, pring, mp1);
James Smart92d7f7b2007-06-17 19:56:38 -05002785 if (mp2)
dea31012005-04-17 16:05:31 -05002786 lpfc_sli_ringpostbuf_put(phba, pring, mp2);
dea31012005-04-17 16:05:31 -05002787 }
2788 pring->missbufcnt = 0;
2789 return 0;
2790}
2791
James Smarte59058c2008-08-24 21:49:00 -04002792/**
James Smart3621a712009-04-06 18:47:14 -04002793 * lpfc_post_rcv_buf - Post the initial receive IOCB buffers to ELS ring
James Smarte59058c2008-08-24 21:49:00 -04002794 * @phba: pointer to lpfc hba data structure.
2795 *
2796 * This routine posts initial receive IOCB buffers to the ELS ring. The
2797 * current number of initial IOCB buffers specified by LPFC_BUF_RING0 is
James Smart895427b2017-02-12 13:52:30 -08002798 * set to 64 IOCBs. SLI3 only.
James Smarte59058c2008-08-24 21:49:00 -04002799 *
2800 * Return codes
2801 * 0 - success (currently always success)
2802 **/
dea31012005-04-17 16:05:31 -05002803static int
James Smart2e0fef82007-06-17 19:56:36 -05002804lpfc_post_rcv_buf(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002805{
2806 struct lpfc_sli *psli = &phba->sli;
2807
2808 /* Ring 0, ELS / CT buffers */
James Smart895427b2017-02-12 13:52:30 -08002809 lpfc_post_buffer(phba, &psli->sli3_ring[LPFC_ELS_RING], LPFC_BUF_RING0);
dea31012005-04-17 16:05:31 -05002810 /* Ring 2 - FCP no buffers needed */
2811
2812 return 0;
2813}
2814
2815#define S(N,V) (((V)<<(N))|((V)>>(32-(N))))
2816
James Smarte59058c2008-08-24 21:49:00 -04002817/**
James Smart3621a712009-04-06 18:47:14 -04002818 * lpfc_sha_init - Set up initial array of hash table entries
James Smarte59058c2008-08-24 21:49:00 -04002819 * @HashResultPointer: pointer to an array as hash table.
2820 *
2821 * This routine sets up the initial values to the array of hash table entries
2822 * for the LC HBAs.
2823 **/
dea31012005-04-17 16:05:31 -05002824static void
2825lpfc_sha_init(uint32_t * HashResultPointer)
2826{
2827 HashResultPointer[0] = 0x67452301;
2828 HashResultPointer[1] = 0xEFCDAB89;
2829 HashResultPointer[2] = 0x98BADCFE;
2830 HashResultPointer[3] = 0x10325476;
2831 HashResultPointer[4] = 0xC3D2E1F0;
2832}
2833
James Smarte59058c2008-08-24 21:49:00 -04002834/**
James Smart3621a712009-04-06 18:47:14 -04002835 * lpfc_sha_iterate - Iterate initial hash table with the working hash table
James Smarte59058c2008-08-24 21:49:00 -04002836 * @HashResultPointer: pointer to an initial/result hash table.
2837 * @HashWorkingPointer: pointer to an working hash table.
2838 *
2839 * This routine iterates an initial hash table pointed by @HashResultPointer
2840 * with the values from the working hash table pointeed by @HashWorkingPointer.
2841 * The results are putting back to the initial hash table, returned through
2842 * the @HashResultPointer as the result hash table.
2843 **/
dea31012005-04-17 16:05:31 -05002844static void
2845lpfc_sha_iterate(uint32_t * HashResultPointer, uint32_t * HashWorkingPointer)
2846{
2847 int t;
2848 uint32_t TEMP;
2849 uint32_t A, B, C, D, E;
2850 t = 16;
2851 do {
2852 HashWorkingPointer[t] =
2853 S(1,
2854 HashWorkingPointer[t - 3] ^ HashWorkingPointer[t -
2855 8] ^
2856 HashWorkingPointer[t - 14] ^ HashWorkingPointer[t - 16]);
2857 } while (++t <= 79);
2858 t = 0;
2859 A = HashResultPointer[0];
2860 B = HashResultPointer[1];
2861 C = HashResultPointer[2];
2862 D = HashResultPointer[3];
2863 E = HashResultPointer[4];
2864
2865 do {
2866 if (t < 20) {
2867 TEMP = ((B & C) | ((~B) & D)) + 0x5A827999;
2868 } else if (t < 40) {
2869 TEMP = (B ^ C ^ D) + 0x6ED9EBA1;
2870 } else if (t < 60) {
2871 TEMP = ((B & C) | (B & D) | (C & D)) + 0x8F1BBCDC;
2872 } else {
2873 TEMP = (B ^ C ^ D) + 0xCA62C1D6;
2874 }
2875 TEMP += S(5, A) + E + HashWorkingPointer[t];
2876 E = D;
2877 D = C;
2878 C = S(30, B);
2879 B = A;
2880 A = TEMP;
2881 } while (++t <= 79);
2882
2883 HashResultPointer[0] += A;
2884 HashResultPointer[1] += B;
2885 HashResultPointer[2] += C;
2886 HashResultPointer[3] += D;
2887 HashResultPointer[4] += E;
2888
2889}
2890
James Smarte59058c2008-08-24 21:49:00 -04002891/**
James Smart3621a712009-04-06 18:47:14 -04002892 * lpfc_challenge_key - Create challenge key based on WWPN of the HBA
James Smarte59058c2008-08-24 21:49:00 -04002893 * @RandomChallenge: pointer to the entry of host challenge random number array.
2894 * @HashWorking: pointer to the entry of the working hash array.
2895 *
2896 * This routine calculates the working hash array referred by @HashWorking
2897 * from the challenge random numbers associated with the host, referred by
2898 * @RandomChallenge. The result is put into the entry of the working hash
2899 * array and returned by reference through @HashWorking.
2900 **/
dea31012005-04-17 16:05:31 -05002901static void
2902lpfc_challenge_key(uint32_t * RandomChallenge, uint32_t * HashWorking)
2903{
2904 *HashWorking = (*RandomChallenge ^ *HashWorking);
2905}
2906
James Smarte59058c2008-08-24 21:49:00 -04002907/**
James Smart3621a712009-04-06 18:47:14 -04002908 * lpfc_hba_init - Perform special handling for LC HBA initialization
James Smarte59058c2008-08-24 21:49:00 -04002909 * @phba: pointer to lpfc hba data structure.
2910 * @hbainit: pointer to an array of unsigned 32-bit integers.
2911 *
2912 * This routine performs the special handling for LC HBA initialization.
2913 **/
dea31012005-04-17 16:05:31 -05002914void
2915lpfc_hba_init(struct lpfc_hba *phba, uint32_t *hbainit)
2916{
2917 int t;
2918 uint32_t *HashWorking;
James Smart2e0fef82007-06-17 19:56:36 -05002919 uint32_t *pwwnn = (uint32_t *) phba->wwnn;
dea31012005-04-17 16:05:31 -05002920
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +02002921 HashWorking = kcalloc(80, sizeof(uint32_t), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05002922 if (!HashWorking)
2923 return;
2924
dea31012005-04-17 16:05:31 -05002925 HashWorking[0] = HashWorking[78] = *pwwnn++;
2926 HashWorking[1] = HashWorking[79] = *pwwnn;
2927
2928 for (t = 0; t < 7; t++)
2929 lpfc_challenge_key(phba->RandomData + t, HashWorking + t);
2930
2931 lpfc_sha_init(hbainit);
2932 lpfc_sha_iterate(hbainit, HashWorking);
2933 kfree(HashWorking);
2934}
2935
James Smarte59058c2008-08-24 21:49:00 -04002936/**
James Smart3621a712009-04-06 18:47:14 -04002937 * lpfc_cleanup - Performs vport cleanups before deleting a vport
James Smarte59058c2008-08-24 21:49:00 -04002938 * @vport: pointer to a virtual N_Port data structure.
2939 *
2940 * This routine performs the necessary cleanups before deleting the @vport.
2941 * It invokes the discovery state machine to perform necessary state
2942 * transitions and to release the ndlps associated with the @vport. Note,
2943 * the physical port is treated as @vport 0.
2944 **/
James Smart87af33f2007-10-27 13:37:43 -04002945void
James Smart2e0fef82007-06-17 19:56:36 -05002946lpfc_cleanup(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002947{
James Smart87af33f2007-10-27 13:37:43 -04002948 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002949 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smarta8adb832007-10-27 13:37:53 -04002950 int i = 0;
dea31012005-04-17 16:05:31 -05002951
James Smart87af33f2007-10-27 13:37:43 -04002952 if (phba->link_state > LPFC_LINK_DOWN)
2953 lpfc_port_link_failure(vport);
2954
Gaurav Srivastava5e633302021-06-08 10:05:49 +05302955 /* Clean up VMID resources */
2956 if (lpfc_is_vmid_enabled(phba))
2957 lpfc_vmid_vport_cleanup(vport);
2958
James Smart87af33f2007-10-27 13:37:43 -04002959 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
James Smart58da1ff2008-04-07 10:15:56 -04002960 if (vport->port_type != LPFC_PHYSICAL_PORT &&
2961 ndlp->nlp_DID == Fabric_DID) {
2962 /* Just free up ndlp with Fabric_DID for vports */
2963 lpfc_nlp_put(ndlp);
2964 continue;
2965 }
2966
James Smarta70e63e2020-11-15 11:26:38 -08002967 if (ndlp->nlp_DID == Fabric_Cntl_DID &&
2968 ndlp->nlp_state == NLP_STE_UNUSED_NODE) {
James Smarteff4a012012-01-18 16:25:25 -05002969 lpfc_nlp_put(ndlp);
2970 continue;
2971 }
2972
James Smarte9b11082020-11-15 11:26:33 -08002973 /* Fabric Ports not in UNMAPPED state are cleaned up in the
2974 * DEVICE_RM event.
2975 */
2976 if (ndlp->nlp_type & NLP_FABRIC &&
2977 ndlp->nlp_state == NLP_STE_UNMAPPED_NODE)
James Smart87af33f2007-10-27 13:37:43 -04002978 lpfc_disc_state_machine(vport, ndlp, NULL,
2979 NLP_EVT_DEVICE_RECOVERY);
James Smarte47c9092008-02-08 18:49:26 -05002980
James Smarte9b11082020-11-15 11:26:33 -08002981 if (!(ndlp->fc4_xpt_flags & (NVME_XPT_REGD|SCSI_XPT_REGD)))
2982 lpfc_disc_state_machine(vport, ndlp, NULL,
2983 NLP_EVT_DEVICE_RM);
James Smart87af33f2007-10-27 13:37:43 -04002984 }
2985
James Smarta8adb832007-10-27 13:37:53 -04002986 /* At this point, ALL ndlp's should be gone
2987 * because of the previous NLP_EVT_DEVICE_RM.
2988 * Lets wait for this to happen, if needed.
2989 */
James Smart87af33f2007-10-27 13:37:43 -04002990 while (!list_empty(&vport->fc_nodes)) {
James Smarta8adb832007-10-27 13:37:53 -04002991 if (i++ > 3000) {
Dick Kennedy372c1872020-06-30 14:50:00 -07002992 lpfc_printf_vlog(vport, KERN_ERR,
2993 LOG_TRACE_EVENT,
James Smarta8adb832007-10-27 13:37:53 -04002994 "0233 Nodelist not empty\n");
James Smarte47c9092008-02-08 18:49:26 -05002995 list_for_each_entry_safe(ndlp, next_ndlp,
2996 &vport->fc_nodes, nlp_listp) {
2997 lpfc_printf_vlog(ndlp->vport, KERN_ERR,
James Smarte9b11082020-11-15 11:26:33 -08002998 LOG_TRACE_EVENT,
2999 "0282 did:x%x ndlp:x%px "
3000 "refcnt:%d xflags x%x nflag x%x\n",
3001 ndlp->nlp_DID, (void *)ndlp,
3002 kref_read(&ndlp->kref),
3003 ndlp->fc4_xpt_flags,
3004 ndlp->nlp_flag);
James Smarte47c9092008-02-08 18:49:26 -05003005 }
James Smarta8adb832007-10-27 13:37:53 -04003006 break;
James Smart87af33f2007-10-27 13:37:43 -04003007 }
James Smarta8adb832007-10-27 13:37:53 -04003008
3009 /* Wait for any activity on ndlps to settle */
3010 msleep(10);
James Smart87af33f2007-10-27 13:37:43 -04003011 }
James Smart1151e3e2011-02-16 12:39:35 -05003012 lpfc_cleanup_vports_rrqs(vport, NULL);
dea31012005-04-17 16:05:31 -05003013}
3014
James Smarte59058c2008-08-24 21:49:00 -04003015/**
James Smart3621a712009-04-06 18:47:14 -04003016 * lpfc_stop_vport_timers - Stop all the timers associated with a vport
James Smarte59058c2008-08-24 21:49:00 -04003017 * @vport: pointer to a virtual N_Port data structure.
3018 *
3019 * This routine stops all the timers associated with a @vport. This function
3020 * is invoked before disabling or deleting a @vport. Note that the physical
3021 * port is treated as @vport 0.
3022 **/
James Smart92d7f7b2007-06-17 19:56:38 -05003023void
3024lpfc_stop_vport_timers(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05003025{
James Smart92d7f7b2007-06-17 19:56:38 -05003026 del_timer_sync(&vport->els_tmofunc);
James Smart92494142011-02-16 12:39:44 -05003027 del_timer_sync(&vport->delayed_disc_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05003028 lpfc_can_disctmo(vport);
3029 return;
dea31012005-04-17 16:05:31 -05003030}
3031
James Smarte59058c2008-08-24 21:49:00 -04003032/**
James Smartecfd03c2010-02-12 14:41:27 -05003033 * __lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
3034 * @phba: pointer to lpfc hba data structure.
3035 *
3036 * This routine stops the SLI4 FCF rediscover wait timer if it's on. The
3037 * caller of this routine should already hold the host lock.
3038 **/
3039void
3040__lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
3041{
James Smart5ac6b302010-10-22 11:05:36 -04003042 /* Clear pending FCF rediscovery wait flag */
3043 phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
3044
James Smartecfd03c2010-02-12 14:41:27 -05003045 /* Now, try to stop the timer */
3046 del_timer(&phba->fcf.redisc_wait);
3047}
3048
3049/**
3050 * lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
3051 * @phba: pointer to lpfc hba data structure.
3052 *
3053 * This routine stops the SLI4 FCF rediscover wait timer if it's on. It
3054 * checks whether the FCF rediscovery wait timer is pending with the host
3055 * lock held before proceeding with disabling the timer and clearing the
3056 * wait timer pendig flag.
3057 **/
3058void
3059lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
3060{
3061 spin_lock_irq(&phba->hbalock);
3062 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
3063 /* FCF rediscovery timer already fired or stopped */
3064 spin_unlock_irq(&phba->hbalock);
3065 return;
3066 }
3067 __lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
James Smart5ac6b302010-10-22 11:05:36 -04003068 /* Clear failover in progress flags */
3069 phba->fcf.fcf_flag &= ~(FCF_DEAD_DISC | FCF_ACVL_DISC);
James Smartecfd03c2010-02-12 14:41:27 -05003070 spin_unlock_irq(&phba->hbalock);
3071}
3072
3073/**
James Smart02243832021-08-16 09:28:54 -07003074 * lpfc_cmf_stop - Stop CMF processing
3075 * @phba: pointer to lpfc hba data structure.
3076 *
3077 * This is called when the link goes down or if CMF mode is turned OFF.
3078 * It is also called when going offline or unloaded just before the
3079 * congestion info buffer is unregistered.
3080 **/
3081void
3082lpfc_cmf_stop(struct lpfc_hba *phba)
3083{
3084 int cpu;
3085 struct lpfc_cgn_stat *cgs;
3086
3087 /* We only do something if CMF is enabled */
3088 if (!phba->sli4_hba.pc_sli4_params.cmf)
3089 return;
3090
3091 lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
3092 "6221 Stop CMF / Cancel Timer\n");
3093
3094 /* Cancel the CMF timer */
3095 hrtimer_cancel(&phba->cmf_timer);
3096
3097 /* Zero CMF counters */
3098 atomic_set(&phba->cmf_busy, 0);
3099 for_each_present_cpu(cpu) {
3100 cgs = per_cpu_ptr(phba->cmf_stat, cpu);
3101 atomic64_set(&cgs->total_bytes, 0);
3102 atomic64_set(&cgs->rcv_bytes, 0);
3103 atomic_set(&cgs->rx_io_cnt, 0);
3104 atomic64_set(&cgs->rx_latency, 0);
3105 }
3106 atomic_set(&phba->cmf_bw_wait, 0);
3107
3108 /* Resume any blocked IO - Queue unblock on workqueue */
3109 queue_work(phba->wq, &phba->unblock_request_work);
3110}
3111
3112static inline uint64_t
3113lpfc_get_max_line_rate(struct lpfc_hba *phba)
3114{
3115 uint64_t rate = lpfc_sli_port_speed_get(phba);
3116
3117 return ((((unsigned long)rate) * 1024 * 1024) / 10);
3118}
3119
3120void
3121lpfc_cmf_signal_init(struct lpfc_hba *phba)
3122{
3123 lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
3124 "6223 Signal CMF init\n");
3125
3126 /* Use the new fc_linkspeed to recalculate */
3127 phba->cmf_interval_rate = LPFC_CMF_INTERVAL;
3128 phba->cmf_max_line_rate = lpfc_get_max_line_rate(phba);
3129 phba->cmf_link_byte_count = div_u64(phba->cmf_max_line_rate *
3130 phba->cmf_interval_rate, 1000);
3131 phba->cmf_max_bytes_per_interval = phba->cmf_link_byte_count;
3132
3133 /* This is a signal to firmware to sync up CMF BW with link speed */
3134 lpfc_issue_cmf_sync_wqe(phba, 0, 0);
3135}
3136
3137/**
3138 * lpfc_cmf_start - Start CMF processing
3139 * @phba: pointer to lpfc hba data structure.
3140 *
3141 * This is called when the link comes up or if CMF mode is turned OFF
3142 * to Monitor or Managed.
3143 **/
3144void
3145lpfc_cmf_start(struct lpfc_hba *phba)
3146{
3147 struct lpfc_cgn_stat *cgs;
3148 int cpu;
3149
3150 /* We only do something if CMF is enabled */
3151 if (!phba->sli4_hba.pc_sli4_params.cmf ||
3152 phba->cmf_active_mode == LPFC_CFG_OFF)
3153 return;
3154
3155 /* Reinitialize congestion buffer info */
3156 lpfc_init_congestion_buf(phba);
3157
3158 atomic_set(&phba->cgn_fabric_warn_cnt, 0);
3159 atomic_set(&phba->cgn_fabric_alarm_cnt, 0);
3160 atomic_set(&phba->cgn_sync_alarm_cnt, 0);
3161 atomic_set(&phba->cgn_sync_warn_cnt, 0);
3162
3163 atomic_set(&phba->cmf_busy, 0);
3164 for_each_present_cpu(cpu) {
3165 cgs = per_cpu_ptr(phba->cmf_stat, cpu);
3166 atomic64_set(&cgs->total_bytes, 0);
3167 atomic64_set(&cgs->rcv_bytes, 0);
3168 atomic_set(&cgs->rx_io_cnt, 0);
3169 atomic64_set(&cgs->rx_latency, 0);
3170 }
3171 phba->cmf_latency.tv_sec = 0;
3172 phba->cmf_latency.tv_nsec = 0;
3173
3174 lpfc_cmf_signal_init(phba);
3175
3176 lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
3177 "6222 Start CMF / Timer\n");
3178
3179 phba->cmf_timer_cnt = 0;
3180 hrtimer_start(&phba->cmf_timer,
3181 ktime_set(0, LPFC_CMF_INTERVAL * 1000000),
3182 HRTIMER_MODE_REL);
3183 /* Setup for latency check in IO cmpl routines */
3184 ktime_get_real_ts64(&phba->cmf_latency);
3185
3186 atomic_set(&phba->cmf_bw_wait, 0);
3187 atomic_set(&phba->cmf_stop_io, 0);
3188}
3189
3190/**
James Smart3772a992009-05-22 14:50:54 -04003191 * lpfc_stop_hba_timers - Stop all the timers associated with an HBA
James Smarte59058c2008-08-24 21:49:00 -04003192 * @phba: pointer to lpfc hba data structure.
3193 *
3194 * This routine stops all the timers associated with a HBA. This function is
3195 * invoked before either putting a HBA offline or unloading the driver.
3196 **/
James Smart3772a992009-05-22 14:50:54 -04003197void
3198lpfc_stop_hba_timers(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05003199{
James Smartcdb42be2019-01-28 11:14:21 -08003200 if (phba->pport)
3201 lpfc_stop_vport_timers(phba->pport);
James Smart32517fc2019-01-28 11:14:33 -08003202 cancel_delayed_work_sync(&phba->eq_delay_work);
Dick Kennedy317aeb82020-06-30 14:49:59 -07003203 cancel_delayed_work_sync(&phba->idle_stat_delay_work);
James Smart2e0fef82007-06-17 19:56:36 -05003204 del_timer_sync(&phba->sli.mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05003205 del_timer_sync(&phba->fabric_block_timer);
James Smart93996272008-08-24 21:50:30 -04003206 del_timer_sync(&phba->eratt_poll);
James Smart3772a992009-05-22 14:50:54 -04003207 del_timer_sync(&phba->hb_tmofunc);
James Smart1151e3e2011-02-16 12:39:35 -05003208 if (phba->sli_rev == LPFC_SLI_REV4) {
3209 del_timer_sync(&phba->rrq_tmr);
3210 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
3211 }
James Smarta22d73b2021-01-04 10:02:38 -08003212 phba->hba_flag &= ~(HBA_HBEAT_INP | HBA_HBEAT_TMO);
James Smart3772a992009-05-22 14:50:54 -04003213
3214 switch (phba->pci_dev_grp) {
3215 case LPFC_PCI_DEV_LP:
3216 /* Stop any LightPulse device specific driver timers */
3217 del_timer_sync(&phba->fcp_poll_timer);
3218 break;
3219 case LPFC_PCI_DEV_OC:
Paul Walmsleycc0e5f12019-07-11 20:52:33 -07003220 /* Stop any OneConnect device specific driver timers */
James Smartecfd03c2010-02-12 14:41:27 -05003221 lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
James Smart3772a992009-05-22 14:50:54 -04003222 break;
3223 default:
Dick Kennedy372c1872020-06-30 14:50:00 -07003224 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart3772a992009-05-22 14:50:54 -04003225 "0297 Invalid device group (x%x)\n",
3226 phba->pci_dev_grp);
3227 break;
3228 }
James Smart2e0fef82007-06-17 19:56:36 -05003229 return;
dea31012005-04-17 16:05:31 -05003230}
3231
James Smarte59058c2008-08-24 21:49:00 -04003232/**
James Smart3621a712009-04-06 18:47:14 -04003233 * lpfc_block_mgmt_io - Mark a HBA's management interface as blocked
James Smarte59058c2008-08-24 21:49:00 -04003234 * @phba: pointer to lpfc hba data structure.
Lee Jonesfe614ac2020-07-23 13:24:22 +01003235 * @mbx_action: flag for mailbox no wait action.
James Smarte59058c2008-08-24 21:49:00 -04003236 *
3237 * This routine marks a HBA's management interface as blocked. Once the HBA's
3238 * management interface is marked as blocked, all the user space access to
3239 * the HBA, whether they are from sysfs interface or libdfc interface will
3240 * all be blocked. The HBA is set to block the management interface when the
3241 * driver prepares the HBA interface for online or offline.
3242 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01003243static void
James Smart618a5232012-06-12 13:54:36 -04003244lpfc_block_mgmt_io(struct lpfc_hba *phba, int mbx_action)
Adrian Bunka6ababd2007-11-05 18:07:33 +01003245{
3246 unsigned long iflag;
James Smart6e7288d2010-06-07 15:23:35 -04003247 uint8_t actcmd = MBX_HEARTBEAT;
3248 unsigned long timeout;
3249
Adrian Bunka6ababd2007-11-05 18:07:33 +01003250 spin_lock_irqsave(&phba->hbalock, iflag);
3251 phba->sli.sli_flag |= LPFC_BLOCK_MGMT_IO;
James Smart618a5232012-06-12 13:54:36 -04003252 spin_unlock_irqrestore(&phba->hbalock, iflag);
3253 if (mbx_action == LPFC_MBX_NO_WAIT)
3254 return;
3255 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
3256 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta183a152011-10-10 21:32:43 -04003257 if (phba->sli.mbox_active) {
James Smart6e7288d2010-06-07 15:23:35 -04003258 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
James Smarta183a152011-10-10 21:32:43 -04003259 /* Determine how long we might wait for the active mailbox
3260 * command to be gracefully completed by firmware.
3261 */
3262 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
3263 phba->sli.mbox_active) * 1000) + jiffies;
3264 }
Adrian Bunka6ababd2007-11-05 18:07:33 +01003265 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta183a152011-10-10 21:32:43 -04003266
James Smart6e7288d2010-06-07 15:23:35 -04003267 /* Wait for the outstnading mailbox command to complete */
3268 while (phba->sli.mbox_active) {
3269 /* Check active mailbox complete status every 2ms */
3270 msleep(2);
3271 if (time_after(jiffies, timeout)) {
Dick Kennedy372c1872020-06-30 14:50:00 -07003272 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3273 "2813 Mgmt IO is Blocked %x "
3274 "- mbox cmd %x still active\n",
3275 phba->sli.sli_flag, actcmd);
James Smart6e7288d2010-06-07 15:23:35 -04003276 break;
3277 }
3278 }
Adrian Bunka6ababd2007-11-05 18:07:33 +01003279}
3280
James Smarte59058c2008-08-24 21:49:00 -04003281/**
James Smart6b5151f2012-01-18 16:24:06 -05003282 * lpfc_sli4_node_prep - Assign RPIs for active nodes.
3283 * @phba: pointer to lpfc hba data structure.
3284 *
3285 * Allocate RPIs for all active remote nodes. This is needed whenever
3286 * an SLI4 adapter is reset and the driver is not unloading. Its purpose
3287 * is to fixup the temporary rpi assignments.
3288 **/
3289void
3290lpfc_sli4_node_prep(struct lpfc_hba *phba)
3291{
3292 struct lpfc_nodelist *ndlp, *next_ndlp;
3293 struct lpfc_vport **vports;
James Smart9d3d3402017-04-21 16:05:00 -07003294 int i, rpi;
James Smart6b5151f2012-01-18 16:24:06 -05003295
3296 if (phba->sli_rev != LPFC_SLI_REV4)
3297 return;
3298
3299 vports = lpfc_create_vport_work_array(phba);
James Smart9d3d3402017-04-21 16:05:00 -07003300 if (vports == NULL)
3301 return;
James Smart6b5151f2012-01-18 16:24:06 -05003302
James Smart9d3d3402017-04-21 16:05:00 -07003303 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
3304 if (vports[i]->load_flag & FC_UNLOADING)
3305 continue;
3306
3307 list_for_each_entry_safe(ndlp, next_ndlp,
3308 &vports[i]->fc_nodes,
3309 nlp_listp) {
James Smart9d3d3402017-04-21 16:05:00 -07003310 rpi = lpfc_sli4_alloc_rpi(phba);
3311 if (rpi == LPFC_RPI_ALLOC_ERROR) {
James Smart307e3382020-11-15 11:26:30 -08003312 /* TODO print log? */
James Smart9d3d3402017-04-21 16:05:00 -07003313 continue;
James Smart6b5151f2012-01-18 16:24:06 -05003314 }
James Smart9d3d3402017-04-21 16:05:00 -07003315 ndlp->nlp_rpi = rpi;
James Smart0f154222019-09-21 20:58:52 -07003316 lpfc_printf_vlog(ndlp->vport, KERN_INFO,
3317 LOG_NODE | LOG_DISCOVERY,
3318 "0009 Assign RPI x%x to ndlp x%px "
James Smart307e3382020-11-15 11:26:30 -08003319 "DID:x%06x flg:x%x\n",
James Smart0f154222019-09-21 20:58:52 -07003320 ndlp->nlp_rpi, ndlp, ndlp->nlp_DID,
James Smart307e3382020-11-15 11:26:30 -08003321 ndlp->nlp_flag);
James Smart6b5151f2012-01-18 16:24:06 -05003322 }
3323 }
3324 lpfc_destroy_vport_work_array(phba, vports);
3325}
3326
3327/**
James Smartc4908502019-01-28 11:14:28 -08003328 * lpfc_create_expedite_pool - create expedite pool
3329 * @phba: pointer to lpfc hba data structure.
3330 *
3331 * This routine moves a batch of XRIs from lpfc_io_buf_list_put of HWQ 0
3332 * to expedite pool. Mark them as expedite.
3333 **/
Bart Van Assche3999df72019-03-28 11:06:16 -07003334static void lpfc_create_expedite_pool(struct lpfc_hba *phba)
James Smartc4908502019-01-28 11:14:28 -08003335{
3336 struct lpfc_sli4_hdw_queue *qp;
3337 struct lpfc_io_buf *lpfc_ncmd;
3338 struct lpfc_io_buf *lpfc_ncmd_next;
3339 struct lpfc_epd_pool *epd_pool;
3340 unsigned long iflag;
3341
3342 epd_pool = &phba->epd_pool;
3343 qp = &phba->sli4_hba.hdwq[0];
3344
3345 spin_lock_init(&epd_pool->lock);
3346 spin_lock_irqsave(&qp->io_buf_list_put_lock, iflag);
3347 spin_lock(&epd_pool->lock);
3348 INIT_LIST_HEAD(&epd_pool->list);
3349 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
3350 &qp->lpfc_io_buf_list_put, list) {
3351 list_move_tail(&lpfc_ncmd->list, &epd_pool->list);
3352 lpfc_ncmd->expedite = true;
3353 qp->put_io_bufs--;
3354 epd_pool->count++;
3355 if (epd_pool->count >= XRI_BATCH)
3356 break;
3357 }
3358 spin_unlock(&epd_pool->lock);
3359 spin_unlock_irqrestore(&qp->io_buf_list_put_lock, iflag);
3360}
3361
3362/**
3363 * lpfc_destroy_expedite_pool - destroy expedite pool
3364 * @phba: pointer to lpfc hba data structure.
3365 *
3366 * This routine returns XRIs from expedite pool to lpfc_io_buf_list_put
3367 * of HWQ 0. Clear the mark.
3368 **/
Bart Van Assche3999df72019-03-28 11:06:16 -07003369static void lpfc_destroy_expedite_pool(struct lpfc_hba *phba)
James Smartc4908502019-01-28 11:14:28 -08003370{
3371 struct lpfc_sli4_hdw_queue *qp;
3372 struct lpfc_io_buf *lpfc_ncmd;
3373 struct lpfc_io_buf *lpfc_ncmd_next;
3374 struct lpfc_epd_pool *epd_pool;
3375 unsigned long iflag;
3376
3377 epd_pool = &phba->epd_pool;
3378 qp = &phba->sli4_hba.hdwq[0];
3379
3380 spin_lock_irqsave(&qp->io_buf_list_put_lock, iflag);
3381 spin_lock(&epd_pool->lock);
3382 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
3383 &epd_pool->list, list) {
3384 list_move_tail(&lpfc_ncmd->list,
3385 &qp->lpfc_io_buf_list_put);
3386 lpfc_ncmd->flags = false;
3387 qp->put_io_bufs++;
3388 epd_pool->count--;
3389 }
3390 spin_unlock(&epd_pool->lock);
3391 spin_unlock_irqrestore(&qp->io_buf_list_put_lock, iflag);
3392}
3393
3394/**
3395 * lpfc_create_multixri_pools - create multi-XRI pools
3396 * @phba: pointer to lpfc hba data structure.
3397 *
3398 * This routine initialize public, private per HWQ. Then, move XRIs from
3399 * lpfc_io_buf_list_put to public pool. High and low watermark are also
3400 * Initialized.
3401 **/
3402void lpfc_create_multixri_pools(struct lpfc_hba *phba)
3403{
3404 u32 i, j;
3405 u32 hwq_count;
3406 u32 count_per_hwq;
3407 struct lpfc_io_buf *lpfc_ncmd;
3408 struct lpfc_io_buf *lpfc_ncmd_next;
3409 unsigned long iflag;
3410 struct lpfc_sli4_hdw_queue *qp;
3411 struct lpfc_multixri_pool *multixri_pool;
3412 struct lpfc_pbl_pool *pbl_pool;
3413 struct lpfc_pvt_pool *pvt_pool;
3414
3415 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3416 "1234 num_hdw_queue=%d num_present_cpu=%d common_xri_cnt=%d\n",
3417 phba->cfg_hdw_queue, phba->sli4_hba.num_present_cpu,
3418 phba->sli4_hba.io_xri_cnt);
3419
3420 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
3421 lpfc_create_expedite_pool(phba);
3422
3423 hwq_count = phba->cfg_hdw_queue;
3424 count_per_hwq = phba->sli4_hba.io_xri_cnt / hwq_count;
3425
3426 for (i = 0; i < hwq_count; i++) {
3427 multixri_pool = kzalloc(sizeof(*multixri_pool), GFP_KERNEL);
3428
3429 if (!multixri_pool) {
3430 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3431 "1238 Failed to allocate memory for "
3432 "multixri_pool\n");
3433
3434 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
3435 lpfc_destroy_expedite_pool(phba);
3436
3437 j = 0;
3438 while (j < i) {
3439 qp = &phba->sli4_hba.hdwq[j];
3440 kfree(qp->p_multixri_pool);
3441 j++;
3442 }
3443 phba->cfg_xri_rebalancing = 0;
3444 return;
3445 }
3446
3447 qp = &phba->sli4_hba.hdwq[i];
3448 qp->p_multixri_pool = multixri_pool;
3449
3450 multixri_pool->xri_limit = count_per_hwq;
3451 multixri_pool->rrb_next_hwqid = i;
3452
3453 /* Deal with public free xri pool */
3454 pbl_pool = &multixri_pool->pbl_pool;
3455 spin_lock_init(&pbl_pool->lock);
3456 spin_lock_irqsave(&qp->io_buf_list_put_lock, iflag);
3457 spin_lock(&pbl_pool->lock);
3458 INIT_LIST_HEAD(&pbl_pool->list);
3459 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
3460 &qp->lpfc_io_buf_list_put, list) {
3461 list_move_tail(&lpfc_ncmd->list, &pbl_pool->list);
3462 qp->put_io_bufs--;
3463 pbl_pool->count++;
3464 }
3465 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3466 "1235 Moved %d buffers from PUT list over to pbl_pool[%d]\n",
3467 pbl_pool->count, i);
3468 spin_unlock(&pbl_pool->lock);
3469 spin_unlock_irqrestore(&qp->io_buf_list_put_lock, iflag);
3470
3471 /* Deal with private free xri pool */
3472 pvt_pool = &multixri_pool->pvt_pool;
3473 pvt_pool->high_watermark = multixri_pool->xri_limit / 2;
3474 pvt_pool->low_watermark = XRI_BATCH;
3475 spin_lock_init(&pvt_pool->lock);
3476 spin_lock_irqsave(&pvt_pool->lock, iflag);
3477 INIT_LIST_HEAD(&pvt_pool->list);
3478 pvt_pool->count = 0;
3479 spin_unlock_irqrestore(&pvt_pool->lock, iflag);
3480 }
3481}
3482
3483/**
3484 * lpfc_destroy_multixri_pools - destroy multi-XRI pools
3485 * @phba: pointer to lpfc hba data structure.
3486 *
3487 * This routine returns XRIs from public/private to lpfc_io_buf_list_put.
3488 **/
Bart Van Assche3999df72019-03-28 11:06:16 -07003489static void lpfc_destroy_multixri_pools(struct lpfc_hba *phba)
James Smartc4908502019-01-28 11:14:28 -08003490{
3491 u32 i;
3492 u32 hwq_count;
3493 struct lpfc_io_buf *lpfc_ncmd;
3494 struct lpfc_io_buf *lpfc_ncmd_next;
3495 unsigned long iflag;
3496 struct lpfc_sli4_hdw_queue *qp;
3497 struct lpfc_multixri_pool *multixri_pool;
3498 struct lpfc_pbl_pool *pbl_pool;
3499 struct lpfc_pvt_pool *pvt_pool;
3500
3501 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
3502 lpfc_destroy_expedite_pool(phba);
3503
James Smartc00f62e2019-08-14 16:57:11 -07003504 if (!(phba->pport->load_flag & FC_UNLOADING))
3505 lpfc_sli_flush_io_rings(phba);
James Smartc66a9192019-03-12 16:30:19 -07003506
James Smartc4908502019-01-28 11:14:28 -08003507 hwq_count = phba->cfg_hdw_queue;
3508
3509 for (i = 0; i < hwq_count; i++) {
3510 qp = &phba->sli4_hba.hdwq[i];
3511 multixri_pool = qp->p_multixri_pool;
3512 if (!multixri_pool)
3513 continue;
3514
3515 qp->p_multixri_pool = NULL;
3516
3517 spin_lock_irqsave(&qp->io_buf_list_put_lock, iflag);
3518
3519 /* Deal with public free xri pool */
3520 pbl_pool = &multixri_pool->pbl_pool;
3521 spin_lock(&pbl_pool->lock);
3522
3523 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3524 "1236 Moving %d buffers from pbl_pool[%d] TO PUT list\n",
3525 pbl_pool->count, i);
3526
3527 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
3528 &pbl_pool->list, list) {
3529 list_move_tail(&lpfc_ncmd->list,
3530 &qp->lpfc_io_buf_list_put);
3531 qp->put_io_bufs++;
3532 pbl_pool->count--;
3533 }
3534
3535 INIT_LIST_HEAD(&pbl_pool->list);
3536 pbl_pool->count = 0;
3537
3538 spin_unlock(&pbl_pool->lock);
3539
3540 /* Deal with private free xri pool */
3541 pvt_pool = &multixri_pool->pvt_pool;
3542 spin_lock(&pvt_pool->lock);
3543
3544 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3545 "1237 Moving %d buffers from pvt_pool[%d] TO PUT list\n",
3546 pvt_pool->count, i);
3547
3548 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
3549 &pvt_pool->list, list) {
3550 list_move_tail(&lpfc_ncmd->list,
3551 &qp->lpfc_io_buf_list_put);
3552 qp->put_io_bufs++;
3553 pvt_pool->count--;
3554 }
3555
3556 INIT_LIST_HEAD(&pvt_pool->list);
3557 pvt_pool->count = 0;
3558
3559 spin_unlock(&pvt_pool->lock);
3560 spin_unlock_irqrestore(&qp->io_buf_list_put_lock, iflag);
3561
3562 kfree(multixri_pool);
3563 }
3564}
3565
3566/**
James Smart3621a712009-04-06 18:47:14 -04003567 * lpfc_online - Initialize and bring a HBA online
James Smarte59058c2008-08-24 21:49:00 -04003568 * @phba: pointer to lpfc hba data structure.
3569 *
3570 * This routine initializes the HBA and brings a HBA online. During this
3571 * process, the management interface is blocked to prevent user space access
3572 * to the HBA interfering with the driver initialization.
3573 *
3574 * Return codes
3575 * 0 - successful
3576 * 1 - failed
3577 **/
dea31012005-04-17 16:05:31 -05003578int
James Smart2e0fef82007-06-17 19:56:36 -05003579lpfc_online(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05003580{
Julia Lawall372bd282008-12-16 16:15:08 +01003581 struct lpfc_vport *vport;
James Smart549e55c2007-08-02 11:09:51 -04003582 struct lpfc_vport **vports;
Dick Kennedya145fda2017-08-23 16:55:44 -07003583 int i, error = 0;
James Smart16a3a202013-04-17 20:14:38 -04003584 bool vpis_cleared = false;
James Smart2e0fef82007-06-17 19:56:36 -05003585
dea31012005-04-17 16:05:31 -05003586 if (!phba)
3587 return 0;
Julia Lawall372bd282008-12-16 16:15:08 +01003588 vport = phba->pport;
dea31012005-04-17 16:05:31 -05003589
James Smart2e0fef82007-06-17 19:56:36 -05003590 if (!(vport->fc_flag & FC_OFFLINE_MODE))
dea31012005-04-17 16:05:31 -05003591 return 0;
3592
James Smarted957682007-06-17 19:56:37 -05003593 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003594 "0458 Bring Adapter online\n");
dea31012005-04-17 16:05:31 -05003595
James Smart618a5232012-06-12 13:54:36 -04003596 lpfc_block_mgmt_io(phba, LPFC_MBX_WAIT);
dea31012005-04-17 16:05:31 -05003597
James Smartda0436e2009-05-22 14:51:39 -04003598 if (phba->sli_rev == LPFC_SLI_REV4) {
3599 if (lpfc_sli4_hba_setup(phba)) { /* Initialize SLI4 HBA */
3600 lpfc_unblock_mgmt_io(phba);
3601 return 1;
3602 }
James Smart16a3a202013-04-17 20:14:38 -04003603 spin_lock_irq(&phba->hbalock);
3604 if (!phba->sli4_hba.max_cfg_param.vpi_used)
3605 vpis_cleared = true;
3606 spin_unlock_irq(&phba->hbalock);
Dick Kennedya145fda2017-08-23 16:55:44 -07003607
3608 /* Reestablish the local initiator port.
3609 * The offline process destroyed the previous lport.
3610 */
3611 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME &&
3612 !phba->nvmet_support) {
3613 error = lpfc_nvme_create_localport(phba->pport);
3614 if (error)
Dick Kennedy372c1872020-06-30 14:50:00 -07003615 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
Dick Kennedya145fda2017-08-23 16:55:44 -07003616 "6132 NVME restore reg failed "
3617 "on nvmei error x%x\n", error);
3618 }
James Smartda0436e2009-05-22 14:51:39 -04003619 } else {
James Smart895427b2017-02-12 13:52:30 -08003620 lpfc_sli_queue_init(phba);
James Smartda0436e2009-05-22 14:51:39 -04003621 if (lpfc_sli_hba_setup(phba)) { /* Initialize SLI2/SLI3 HBA */
3622 lpfc_unblock_mgmt_io(phba);
3623 return 1;
3624 }
James Smart46fa3112007-04-25 09:51:45 -04003625 }
dea31012005-04-17 16:05:31 -05003626
James Smart549e55c2007-08-02 11:09:51 -04003627 vports = lpfc_create_vport_work_array(phba);
Arnd Bergmannaeb66412016-03-14 15:29:44 +01003628 if (vports != NULL) {
James Smartda0436e2009-05-22 14:51:39 -04003629 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -04003630 struct Scsi_Host *shost;
3631 shost = lpfc_shost_from_vport(vports[i]);
3632 spin_lock_irq(shost->host_lock);
3633 vports[i]->fc_flag &= ~FC_OFFLINE_MODE;
3634 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
3635 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart16a3a202013-04-17 20:14:38 -04003636 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart1c6834a2009-07-19 10:01:26 -04003637 vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
James Smart16a3a202013-04-17 20:14:38 -04003638 if ((vpis_cleared) &&
3639 (vports[i]->port_type !=
3640 LPFC_PHYSICAL_PORT))
3641 vports[i]->vpi = 0;
3642 }
James Smart549e55c2007-08-02 11:09:51 -04003643 spin_unlock_irq(shost->host_lock);
3644 }
Arnd Bergmannaeb66412016-03-14 15:29:44 +01003645 }
3646 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05003647
James Smartc4908502019-01-28 11:14:28 -08003648 if (phba->cfg_xri_rebalancing)
3649 lpfc_create_multixri_pools(phba);
3650
James Smart93a4d6f2019-11-04 16:57:05 -08003651 lpfc_cpuhp_add(phba);
3652
James Smart46fa3112007-04-25 09:51:45 -04003653 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05003654 return 0;
3655}
3656
James Smarte59058c2008-08-24 21:49:00 -04003657/**
James Smart3621a712009-04-06 18:47:14 -04003658 * lpfc_unblock_mgmt_io - Mark a HBA's management interface to be not blocked
James Smarte59058c2008-08-24 21:49:00 -04003659 * @phba: pointer to lpfc hba data structure.
3660 *
3661 * This routine marks a HBA's management interface as not blocked. Once the
3662 * HBA's management interface is marked as not blocked, all the user space
3663 * access to the HBA, whether they are from sysfs interface or libdfc
3664 * interface will be allowed. The HBA is set to block the management interface
3665 * when the driver prepares the HBA interface for online or offline and then
3666 * set to unblock the management interface afterwards.
3667 **/
James Smart46fa3112007-04-25 09:51:45 -04003668void
James Smart46fa3112007-04-25 09:51:45 -04003669lpfc_unblock_mgmt_io(struct lpfc_hba * phba)
3670{
3671 unsigned long iflag;
3672
James Smart2e0fef82007-06-17 19:56:36 -05003673 spin_lock_irqsave(&phba->hbalock, iflag);
3674 phba->sli.sli_flag &= ~LPFC_BLOCK_MGMT_IO;
3675 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart46fa3112007-04-25 09:51:45 -04003676}
3677
James Smarte59058c2008-08-24 21:49:00 -04003678/**
James Smart3621a712009-04-06 18:47:14 -04003679 * lpfc_offline_prep - Prepare a HBA to be brought offline
James Smarte59058c2008-08-24 21:49:00 -04003680 * @phba: pointer to lpfc hba data structure.
Lee Jonesfe614ac2020-07-23 13:24:22 +01003681 * @mbx_action: flag for mailbox shutdown action.
James Smarte59058c2008-08-24 21:49:00 -04003682 *
3683 * This routine is invoked to prepare a HBA to be brought offline. It performs
3684 * unregistration login to all the nodes on all vports and flushes the mailbox
3685 * queue to make it ready to be brought offline.
3686 **/
James Smart46fa3112007-04-25 09:51:45 -04003687void
James Smart618a5232012-06-12 13:54:36 -04003688lpfc_offline_prep(struct lpfc_hba *phba, int mbx_action)
James Smart46fa3112007-04-25 09:51:45 -04003689{
James Smart2e0fef82007-06-17 19:56:36 -05003690 struct lpfc_vport *vport = phba->pport;
James Smart46fa3112007-04-25 09:51:45 -04003691 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart87af33f2007-10-27 13:37:43 -04003692 struct lpfc_vport **vports;
James Smart72100cc2010-02-12 14:43:01 -05003693 struct Scsi_Host *shost;
James Smart87af33f2007-10-27 13:37:43 -04003694 int i;
James Smart25ac2c972021-09-10 16:31:54 -07003695 int offline = 0;
dea31012005-04-17 16:05:31 -05003696
James Smart2e0fef82007-06-17 19:56:36 -05003697 if (vport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04003698 return;
dea31012005-04-17 16:05:31 -05003699
James Smart618a5232012-06-12 13:54:36 -04003700 lpfc_block_mgmt_io(phba, mbx_action);
dea31012005-04-17 16:05:31 -05003701
3702 lpfc_linkdown(phba);
3703
James Smart25ac2c972021-09-10 16:31:54 -07003704 offline = pci_channel_offline(phba->pcidev);
3705
James Smart87af33f2007-10-27 13:37:43 -04003706 /* Issue an unreg_login to all nodes on all vports */
3707 vports = lpfc_create_vport_work_array(phba);
3708 if (vports != NULL) {
James Smartda0436e2009-05-22 14:51:39 -04003709 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smarta8adb832007-10-27 13:37:53 -04003710 if (vports[i]->load_flag & FC_UNLOADING)
3711 continue;
James Smart72100cc2010-02-12 14:43:01 -05003712 shost = lpfc_shost_from_vport(vports[i]);
3713 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05003714 vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05003715 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
3716 vports[i]->fc_flag &= ~FC_VFI_REGISTERED;
James Smart72100cc2010-02-12 14:43:01 -05003717 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05003718
James Smart87af33f2007-10-27 13:37:43 -04003719 shost = lpfc_shost_from_vport(vports[i]);
3720 list_for_each_entry_safe(ndlp, next_ndlp,
3721 &vports[i]->fc_nodes,
3722 nlp_listp) {
James Smart0f154222019-09-21 20:58:52 -07003723
James Smartc6adba12020-11-15 11:26:34 -08003724 spin_lock_irq(&ndlp->lock);
James Smart87af33f2007-10-27 13:37:43 -04003725 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
James Smartc6adba12020-11-15 11:26:34 -08003726 spin_unlock_irq(&ndlp->lock);
James Smartaffbe242021-07-07 11:43:42 -07003727
James Smart25ac2c972021-09-10 16:31:54 -07003728 if (offline) {
3729 spin_lock_irq(&ndlp->lock);
3730 ndlp->nlp_flag &= ~(NLP_UNREG_INP |
3731 NLP_RPI_REGISTERED);
3732 spin_unlock_irq(&ndlp->lock);
3733 } else {
3734 lpfc_unreg_rpi(vports[i], ndlp);
3735 }
James Smart6b5151f2012-01-18 16:24:06 -05003736 /*
3737 * Whenever an SLI4 port goes offline, free the
James Smart401ee0c2012-03-01 22:35:34 -05003738 * RPI. Get a new RPI when the adapter port
3739 * comes back online.
James Smart6b5151f2012-01-18 16:24:06 -05003740 */
James Smartbe6bb942015-04-07 15:07:22 -04003741 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smarte9b11082020-11-15 11:26:33 -08003742 lpfc_printf_vlog(vports[i], KERN_INFO,
James Smart0f154222019-09-21 20:58:52 -07003743 LOG_NODE | LOG_DISCOVERY,
3744 "0011 Free RPI x%x on "
James Smartf1156122021-04-11 18:31:24 -07003745 "ndlp: x%px did x%x\n",
James Smart0f154222019-09-21 20:58:52 -07003746 ndlp->nlp_rpi, ndlp,
James Smart307e3382020-11-15 11:26:30 -08003747 ndlp->nlp_DID);
James Smart6b5151f2012-01-18 16:24:06 -05003748 lpfc_sli4_free_rpi(phba, ndlp->nlp_rpi);
James Smart0f154222019-09-21 20:58:52 -07003749 ndlp->nlp_rpi = LPFC_RPI_ALLOC_ERROR;
James Smartbe6bb942015-04-07 15:07:22 -04003750 }
James Smart307e3382020-11-15 11:26:30 -08003751
3752 if (ndlp->nlp_type & NLP_FABRIC) {
3753 lpfc_disc_state_machine(vports[i], ndlp,
3754 NULL, NLP_EVT_DEVICE_RECOVERY);
James Smarte9b11082020-11-15 11:26:33 -08003755
James Smartaf984c82021-10-20 14:14:16 -07003756 /* Don't remove the node unless the node
James Smarte9b11082020-11-15 11:26:33 -08003757 * has been unregistered with the
James Smartaf984c82021-10-20 14:14:16 -07003758 * transport, and we're not in recovery
3759 * before dev_loss_tmo triggered.
3760 * Otherwise, let dev_loss take care of
3761 * the node.
James Smarte9b11082020-11-15 11:26:33 -08003762 */
James Smartaf984c82021-10-20 14:14:16 -07003763 if (!(ndlp->save_flags &
3764 NLP_IN_RECOV_POST_DEV_LOSS) &&
3765 !(ndlp->fc4_xpt_flags &
James Smarte9b11082020-11-15 11:26:33 -08003766 (NVME_XPT_REGD | SCSI_XPT_REGD)))
3767 lpfc_disc_state_machine
3768 (vports[i], ndlp,
3769 NULL,
3770 NLP_EVT_DEVICE_RM);
James Smart307e3382020-11-15 11:26:30 -08003771 }
James Smart87af33f2007-10-27 13:37:43 -04003772 }
3773 }
3774 }
James Smart09372822008-01-11 01:52:54 -05003775 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05003776
James Smart618a5232012-06-12 13:54:36 -04003777 lpfc_sli_mbox_sys_shutdown(phba, mbx_action);
Dick Kennedyf485c182017-09-29 17:34:34 -07003778
3779 if (phba->wq)
3780 flush_workqueue(phba->wq);
James Smart46fa3112007-04-25 09:51:45 -04003781}
3782
James Smarte59058c2008-08-24 21:49:00 -04003783/**
James Smart3621a712009-04-06 18:47:14 -04003784 * lpfc_offline - Bring a HBA offline
James Smarte59058c2008-08-24 21:49:00 -04003785 * @phba: pointer to lpfc hba data structure.
3786 *
3787 * This routine actually brings a HBA offline. It stops all the timers
3788 * associated with the HBA, brings down the SLI layer, and eventually
3789 * marks the HBA as in offline state for the upper layer protocol.
3790 **/
James Smart46fa3112007-04-25 09:51:45 -04003791void
James Smart2e0fef82007-06-17 19:56:36 -05003792lpfc_offline(struct lpfc_hba *phba)
James Smart46fa3112007-04-25 09:51:45 -04003793{
James Smart549e55c2007-08-02 11:09:51 -04003794 struct Scsi_Host *shost;
3795 struct lpfc_vport **vports;
3796 int i;
James Smart46fa3112007-04-25 09:51:45 -04003797
James Smart549e55c2007-08-02 11:09:51 -04003798 if (phba->pport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04003799 return;
James Smart688a8862006-07-06 15:49:56 -04003800
James Smartda0436e2009-05-22 14:51:39 -04003801 /* stop port and all timers associated with this hba */
3802 lpfc_stop_port(phba);
Dick Kennedy4b40d022017-08-23 16:55:38 -07003803
3804 /* Tear down the local and target port registrations. The
3805 * nvme transports need to cleanup.
3806 */
3807 lpfc_nvmet_destroy_targetport(phba);
3808 lpfc_nvme_destroy_localport(phba->pport);
3809
James Smart51ef4c22007-08-02 11:10:31 -04003810 vports = lpfc_create_vport_work_array(phba);
3811 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04003812 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
James Smart51ef4c22007-08-02 11:10:31 -04003813 lpfc_stop_vport_timers(vports[i]);
James Smart09372822008-01-11 01:52:54 -05003814 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -05003815 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003816 "0460 Bring Adapter offline\n");
dea31012005-04-17 16:05:31 -05003817 /* Bring down the SLI Layer and cleanup. The HBA is offline
3818 now. */
3819 lpfc_sli_hba_down(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05003820 spin_lock_irq(&phba->hbalock);
James Smart7054a602007-04-25 09:52:34 -04003821 phba->work_ha = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05003822 spin_unlock_irq(&phba->hbalock);
James Smart549e55c2007-08-02 11:09:51 -04003823 vports = lpfc_create_vport_work_array(phba);
3824 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04003825 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -04003826 shost = lpfc_shost_from_vport(vports[i]);
James Smart549e55c2007-08-02 11:09:51 -04003827 spin_lock_irq(shost->host_lock);
3828 vports[i]->work_port_events = 0;
3829 vports[i]->fc_flag |= FC_OFFLINE_MODE;
3830 spin_unlock_irq(shost->host_lock);
3831 }
James Smart09372822008-01-11 01:52:54 -05003832 lpfc_destroy_vport_work_array(phba, vports);
James Smartf0871ab2021-01-04 10:02:32 -08003833 /* If OFFLINE flag is clear (i.e. unloading), cpuhp removal is handled
3834 * in hba_unset
3835 */
3836 if (phba->pport->fc_flag & FC_OFFLINE_MODE)
3837 __lpfc_cpuhp_remove(phba);
James Smartc4908502019-01-28 11:14:28 -08003838
3839 if (phba->cfg_xri_rebalancing)
3840 lpfc_destroy_multixri_pools(phba);
dea31012005-04-17 16:05:31 -05003841}
3842
James Smarte59058c2008-08-24 21:49:00 -04003843/**
James Smart3621a712009-04-06 18:47:14 -04003844 * lpfc_scsi_free - Free all the SCSI buffers and IOCBs from driver lists
James Smarte59058c2008-08-24 21:49:00 -04003845 * @phba: pointer to lpfc hba data structure.
3846 *
3847 * This routine is to free all the SCSI buffers and IOCBs from the driver
3848 * list back to kernel. It is called from lpfc_pci_remove_one to free
3849 * the internal resources before the device is removed from the system.
James Smarte59058c2008-08-24 21:49:00 -04003850 **/
James Smart8a9d2e82012-05-09 21:16:12 -04003851static void
James Smart2e0fef82007-06-17 19:56:36 -05003852lpfc_scsi_free(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05003853{
James Smartc4908502019-01-28 11:14:28 -08003854 struct lpfc_io_buf *sb, *sb_next;
dea31012005-04-17 16:05:31 -05003855
James Smart895427b2017-02-12 13:52:30 -08003856 if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP))
3857 return;
3858
James Smart2e0fef82007-06-17 19:56:36 -05003859 spin_lock_irq(&phba->hbalock);
James Smarta40fc5f2013-04-17 20:17:40 -04003860
dea31012005-04-17 16:05:31 -05003861 /* Release all the lpfc_scsi_bufs maintained by this host. */
James Smarta40fc5f2013-04-17 20:17:40 -04003862
3863 spin_lock(&phba->scsi_buf_list_put_lock);
3864 list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list_put,
3865 list) {
dea31012005-04-17 16:05:31 -05003866 list_del(&sb->list);
Romain Perier771db5c2017-07-06 10:13:05 +02003867 dma_pool_free(phba->lpfc_sg_dma_buf_pool, sb->data,
James Smart92d7f7b2007-06-17 19:56:38 -05003868 sb->dma_handle);
dea31012005-04-17 16:05:31 -05003869 kfree(sb);
3870 phba->total_scsi_bufs--;
3871 }
James Smarta40fc5f2013-04-17 20:17:40 -04003872 spin_unlock(&phba->scsi_buf_list_put_lock);
3873
3874 spin_lock(&phba->scsi_buf_list_get_lock);
3875 list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list_get,
3876 list) {
3877 list_del(&sb->list);
Romain Perier771db5c2017-07-06 10:13:05 +02003878 dma_pool_free(phba->lpfc_sg_dma_buf_pool, sb->data,
James Smarta40fc5f2013-04-17 20:17:40 -04003879 sb->dma_handle);
3880 kfree(sb);
3881 phba->total_scsi_bufs--;
3882 }
3883 spin_unlock(&phba->scsi_buf_list_get_lock);
James Smart2e0fef82007-06-17 19:56:36 -05003884 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04003885}
James Smart0794d602019-01-28 11:14:19 -08003886
James Smart8a9d2e82012-05-09 21:16:12 -04003887/**
James Smart5e5b5112019-01-28 11:14:22 -08003888 * lpfc_io_free - Free all the IO buffers and IOCBs from driver lists
James Smart895427b2017-02-12 13:52:30 -08003889 * @phba: pointer to lpfc hba data structure.
3890 *
James Smart0794d602019-01-28 11:14:19 -08003891 * This routine is to free all the IO buffers and IOCBs from the driver
James Smart895427b2017-02-12 13:52:30 -08003892 * list back to kernel. It is called from lpfc_pci_remove_one to free
3893 * the internal resources before the device is removed from the system.
3894 **/
James Smartc4908502019-01-28 11:14:28 -08003895void
James Smart5e5b5112019-01-28 11:14:22 -08003896lpfc_io_free(struct lpfc_hba *phba)
James Smart895427b2017-02-12 13:52:30 -08003897{
James Smartc4908502019-01-28 11:14:28 -08003898 struct lpfc_io_buf *lpfc_ncmd, *lpfc_ncmd_next;
James Smart5e5b5112019-01-28 11:14:22 -08003899 struct lpfc_sli4_hdw_queue *qp;
3900 int idx;
James Smart895427b2017-02-12 13:52:30 -08003901
James Smart5e5b5112019-01-28 11:14:22 -08003902 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
3903 qp = &phba->sli4_hba.hdwq[idx];
3904 /* Release all the lpfc_nvme_bufs maintained by this host. */
3905 spin_lock(&qp->io_buf_list_put_lock);
3906 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
3907 &qp->lpfc_io_buf_list_put,
3908 list) {
3909 list_del(&lpfc_ncmd->list);
3910 qp->put_io_bufs--;
3911 dma_pool_free(phba->lpfc_sg_dma_buf_pool,
3912 lpfc_ncmd->data, lpfc_ncmd->dma_handle);
James Smartd79c9e92019-08-14 16:57:09 -07003913 if (phba->cfg_xpsgl && !phba->nvmet_support)
3914 lpfc_put_sgl_per_hdwq(phba, lpfc_ncmd);
3915 lpfc_put_cmd_rsp_buf_per_hdwq(phba, lpfc_ncmd);
James Smart5e5b5112019-01-28 11:14:22 -08003916 kfree(lpfc_ncmd);
3917 qp->total_io_bufs--;
3918 }
3919 spin_unlock(&qp->io_buf_list_put_lock);
James Smart895427b2017-02-12 13:52:30 -08003920
James Smart5e5b5112019-01-28 11:14:22 -08003921 spin_lock(&qp->io_buf_list_get_lock);
3922 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
3923 &qp->lpfc_io_buf_list_get,
3924 list) {
3925 list_del(&lpfc_ncmd->list);
3926 qp->get_io_bufs--;
3927 dma_pool_free(phba->lpfc_sg_dma_buf_pool,
3928 lpfc_ncmd->data, lpfc_ncmd->dma_handle);
James Smartd79c9e92019-08-14 16:57:09 -07003929 if (phba->cfg_xpsgl && !phba->nvmet_support)
3930 lpfc_put_sgl_per_hdwq(phba, lpfc_ncmd);
3931 lpfc_put_cmd_rsp_buf_per_hdwq(phba, lpfc_ncmd);
James Smart5e5b5112019-01-28 11:14:22 -08003932 kfree(lpfc_ncmd);
3933 qp->total_io_bufs--;
3934 }
3935 spin_unlock(&qp->io_buf_list_get_lock);
James Smart895427b2017-02-12 13:52:30 -08003936 }
James Smart895427b2017-02-12 13:52:30 -08003937}
James Smart0794d602019-01-28 11:14:19 -08003938
James Smart895427b2017-02-12 13:52:30 -08003939/**
3940 * lpfc_sli4_els_sgl_update - update ELS xri-sgl sizing and mapping
James Smart8a9d2e82012-05-09 21:16:12 -04003941 * @phba: pointer to lpfc hba data structure.
3942 *
3943 * This routine first calculates the sizes of the current els and allocated
3944 * scsi sgl lists, and then goes through all sgls to updates the physical
3945 * XRIs assigned due to port function reset. During port initialization, the
3946 * current els and allocated scsi sgl lists are 0s.
3947 *
3948 * Return codes
3949 * 0 - successful (for now, it always returns 0)
3950 **/
3951int
James Smart895427b2017-02-12 13:52:30 -08003952lpfc_sli4_els_sgl_update(struct lpfc_hba *phba)
James Smart8a9d2e82012-05-09 21:16:12 -04003953{
3954 struct lpfc_sglq *sglq_entry = NULL, *sglq_entry_next = NULL;
James Smart895427b2017-02-12 13:52:30 -08003955 uint16_t i, lxri, xri_cnt, els_xri_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04003956 LIST_HEAD(els_sgl_list);
James Smart8a9d2e82012-05-09 21:16:12 -04003957 int rc;
3958
3959 /*
3960 * update on pci function's els xri-sgl list
3961 */
3962 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
James Smart895427b2017-02-12 13:52:30 -08003963
James Smart8a9d2e82012-05-09 21:16:12 -04003964 if (els_xri_cnt > phba->sli4_hba.els_xri_cnt) {
3965 /* els xri-sgl expanded */
3966 xri_cnt = els_xri_cnt - phba->sli4_hba.els_xri_cnt;
3967 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3968 "3157 ELS xri-sgl count increased from "
3969 "%d to %d\n", phba->sli4_hba.els_xri_cnt,
3970 els_xri_cnt);
3971 /* allocate the additional els sgls */
3972 for (i = 0; i < xri_cnt; i++) {
3973 sglq_entry = kzalloc(sizeof(struct lpfc_sglq),
3974 GFP_KERNEL);
3975 if (sglq_entry == NULL) {
Dick Kennedy372c1872020-06-30 14:50:00 -07003976 lpfc_printf_log(phba, KERN_ERR,
3977 LOG_TRACE_EVENT,
James Smart8a9d2e82012-05-09 21:16:12 -04003978 "2562 Failure to allocate an "
3979 "ELS sgl entry:%d\n", i);
3980 rc = -ENOMEM;
3981 goto out_free_mem;
3982 }
3983 sglq_entry->buff_type = GEN_BUFF_TYPE;
3984 sglq_entry->virt = lpfc_mbuf_alloc(phba, 0,
3985 &sglq_entry->phys);
3986 if (sglq_entry->virt == NULL) {
3987 kfree(sglq_entry);
Dick Kennedy372c1872020-06-30 14:50:00 -07003988 lpfc_printf_log(phba, KERN_ERR,
3989 LOG_TRACE_EVENT,
James Smart8a9d2e82012-05-09 21:16:12 -04003990 "2563 Failure to allocate an "
3991 "ELS mbuf:%d\n", i);
3992 rc = -ENOMEM;
3993 goto out_free_mem;
3994 }
3995 sglq_entry->sgl = sglq_entry->virt;
3996 memset(sglq_entry->sgl, 0, LPFC_BPL_SIZE);
3997 sglq_entry->state = SGL_FREED;
3998 list_add_tail(&sglq_entry->list, &els_sgl_list);
3999 }
James Smarta7892412021-04-11 18:31:15 -07004000 spin_lock_irq(&phba->sli4_hba.sgl_list_lock);
James Smart895427b2017-02-12 13:52:30 -08004001 list_splice_init(&els_sgl_list,
4002 &phba->sli4_hba.lpfc_els_sgl_list);
James Smarta7892412021-04-11 18:31:15 -07004003 spin_unlock_irq(&phba->sli4_hba.sgl_list_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04004004 } else if (els_xri_cnt < phba->sli4_hba.els_xri_cnt) {
4005 /* els xri-sgl shrinked */
4006 xri_cnt = phba->sli4_hba.els_xri_cnt - els_xri_cnt;
4007 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4008 "3158 ELS xri-sgl count decreased from "
4009 "%d to %d\n", phba->sli4_hba.els_xri_cnt,
4010 els_xri_cnt);
James Smarta7892412021-04-11 18:31:15 -07004011 spin_lock_irq(&phba->sli4_hba.sgl_list_lock);
James Smart895427b2017-02-12 13:52:30 -08004012 list_splice_init(&phba->sli4_hba.lpfc_els_sgl_list,
4013 &els_sgl_list);
James Smart8a9d2e82012-05-09 21:16:12 -04004014 /* release extra els sgls from list */
4015 for (i = 0; i < xri_cnt; i++) {
4016 list_remove_head(&els_sgl_list,
4017 sglq_entry, struct lpfc_sglq, list);
4018 if (sglq_entry) {
James Smart895427b2017-02-12 13:52:30 -08004019 __lpfc_mbuf_free(phba, sglq_entry->virt,
4020 sglq_entry->phys);
James Smart8a9d2e82012-05-09 21:16:12 -04004021 kfree(sglq_entry);
4022 }
4023 }
James Smart895427b2017-02-12 13:52:30 -08004024 list_splice_init(&els_sgl_list,
4025 &phba->sli4_hba.lpfc_els_sgl_list);
James Smarta7892412021-04-11 18:31:15 -07004026 spin_unlock_irq(&phba->sli4_hba.sgl_list_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04004027 } else
4028 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4029 "3163 ELS xri-sgl count unchanged: %d\n",
4030 els_xri_cnt);
4031 phba->sli4_hba.els_xri_cnt = els_xri_cnt;
4032
4033 /* update xris to els sgls on the list */
4034 sglq_entry = NULL;
4035 sglq_entry_next = NULL;
4036 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
James Smart895427b2017-02-12 13:52:30 -08004037 &phba->sli4_hba.lpfc_els_sgl_list, list) {
James Smart8a9d2e82012-05-09 21:16:12 -04004038 lxri = lpfc_sli4_next_xritag(phba);
4039 if (lxri == NO_XRI) {
Dick Kennedy372c1872020-06-30 14:50:00 -07004040 lpfc_printf_log(phba, KERN_ERR,
4041 LOG_TRACE_EVENT,
James Smart8a9d2e82012-05-09 21:16:12 -04004042 "2400 Failed to allocate xri for "
4043 "ELS sgl\n");
4044 rc = -ENOMEM;
4045 goto out_free_mem;
4046 }
4047 sglq_entry->sli4_lxritag = lxri;
4048 sglq_entry->sli4_xritag = phba->sli4_hba.xri_ids[lxri];
4049 }
James Smart895427b2017-02-12 13:52:30 -08004050 return 0;
4051
4052out_free_mem:
4053 lpfc_free_els_sgl_list(phba);
4054 return rc;
4055}
4056
4057/**
James Smartf358dd02017-02-12 13:52:34 -08004058 * lpfc_sli4_nvmet_sgl_update - update xri-sgl sizing and mapping
4059 * @phba: pointer to lpfc hba data structure.
4060 *
4061 * This routine first calculates the sizes of the current els and allocated
4062 * scsi sgl lists, and then goes through all sgls to updates the physical
4063 * XRIs assigned due to port function reset. During port initialization, the
4064 * current els and allocated scsi sgl lists are 0s.
4065 *
4066 * Return codes
4067 * 0 - successful (for now, it always returns 0)
4068 **/
4069int
4070lpfc_sli4_nvmet_sgl_update(struct lpfc_hba *phba)
4071{
4072 struct lpfc_sglq *sglq_entry = NULL, *sglq_entry_next = NULL;
4073 uint16_t i, lxri, xri_cnt, els_xri_cnt;
James Smart6c621a22017-05-15 15:20:45 -07004074 uint16_t nvmet_xri_cnt;
James Smartf358dd02017-02-12 13:52:34 -08004075 LIST_HEAD(nvmet_sgl_list);
4076 int rc;
4077
4078 /*
4079 * update on pci function's nvmet xri-sgl list
4080 */
4081 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
James Smart61f3d4b2017-05-15 15:20:41 -07004082
James Smart6c621a22017-05-15 15:20:45 -07004083 /* For NVMET, ALL remaining XRIs are dedicated for IO processing */
4084 nvmet_xri_cnt = phba->sli4_hba.max_cfg_param.max_xri - els_xri_cnt;
James Smartf358dd02017-02-12 13:52:34 -08004085 if (nvmet_xri_cnt > phba->sli4_hba.nvmet_xri_cnt) {
4086 /* els xri-sgl expanded */
4087 xri_cnt = nvmet_xri_cnt - phba->sli4_hba.nvmet_xri_cnt;
4088 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4089 "6302 NVMET xri-sgl cnt grew from %d to %d\n",
4090 phba->sli4_hba.nvmet_xri_cnt, nvmet_xri_cnt);
4091 /* allocate the additional nvmet sgls */
4092 for (i = 0; i < xri_cnt; i++) {
4093 sglq_entry = kzalloc(sizeof(struct lpfc_sglq),
4094 GFP_KERNEL);
4095 if (sglq_entry == NULL) {
Dick Kennedy372c1872020-06-30 14:50:00 -07004096 lpfc_printf_log(phba, KERN_ERR,
4097 LOG_TRACE_EVENT,
James Smartf358dd02017-02-12 13:52:34 -08004098 "6303 Failure to allocate an "
4099 "NVMET sgl entry:%d\n", i);
4100 rc = -ENOMEM;
4101 goto out_free_mem;
4102 }
4103 sglq_entry->buff_type = NVMET_BUFF_TYPE;
4104 sglq_entry->virt = lpfc_nvmet_buf_alloc(phba, 0,
4105 &sglq_entry->phys);
4106 if (sglq_entry->virt == NULL) {
4107 kfree(sglq_entry);
Dick Kennedy372c1872020-06-30 14:50:00 -07004108 lpfc_printf_log(phba, KERN_ERR,
4109 LOG_TRACE_EVENT,
James Smartf358dd02017-02-12 13:52:34 -08004110 "6304 Failure to allocate an "
4111 "NVMET buf:%d\n", i);
4112 rc = -ENOMEM;
4113 goto out_free_mem;
4114 }
4115 sglq_entry->sgl = sglq_entry->virt;
4116 memset(sglq_entry->sgl, 0,
4117 phba->cfg_sg_dma_buf_size);
4118 sglq_entry->state = SGL_FREED;
4119 list_add_tail(&sglq_entry->list, &nvmet_sgl_list);
4120 }
4121 spin_lock_irq(&phba->hbalock);
4122 spin_lock(&phba->sli4_hba.sgl_list_lock);
4123 list_splice_init(&nvmet_sgl_list,
4124 &phba->sli4_hba.lpfc_nvmet_sgl_list);
4125 spin_unlock(&phba->sli4_hba.sgl_list_lock);
4126 spin_unlock_irq(&phba->hbalock);
4127 } else if (nvmet_xri_cnt < phba->sli4_hba.nvmet_xri_cnt) {
4128 /* nvmet xri-sgl shrunk */
4129 xri_cnt = phba->sli4_hba.nvmet_xri_cnt - nvmet_xri_cnt;
4130 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4131 "6305 NVMET xri-sgl count decreased from "
4132 "%d to %d\n", phba->sli4_hba.nvmet_xri_cnt,
4133 nvmet_xri_cnt);
4134 spin_lock_irq(&phba->hbalock);
4135 spin_lock(&phba->sli4_hba.sgl_list_lock);
4136 list_splice_init(&phba->sli4_hba.lpfc_nvmet_sgl_list,
4137 &nvmet_sgl_list);
4138 /* release extra nvmet sgls from list */
4139 for (i = 0; i < xri_cnt; i++) {
4140 list_remove_head(&nvmet_sgl_list,
4141 sglq_entry, struct lpfc_sglq, list);
4142 if (sglq_entry) {
4143 lpfc_nvmet_buf_free(phba, sglq_entry->virt,
4144 sglq_entry->phys);
4145 kfree(sglq_entry);
4146 }
4147 }
4148 list_splice_init(&nvmet_sgl_list,
4149 &phba->sli4_hba.lpfc_nvmet_sgl_list);
4150 spin_unlock(&phba->sli4_hba.sgl_list_lock);
4151 spin_unlock_irq(&phba->hbalock);
4152 } else
4153 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4154 "6306 NVMET xri-sgl count unchanged: %d\n",
4155 nvmet_xri_cnt);
4156 phba->sli4_hba.nvmet_xri_cnt = nvmet_xri_cnt;
4157
4158 /* update xris to nvmet sgls on the list */
4159 sglq_entry = NULL;
4160 sglq_entry_next = NULL;
4161 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
4162 &phba->sli4_hba.lpfc_nvmet_sgl_list, list) {
4163 lxri = lpfc_sli4_next_xritag(phba);
4164 if (lxri == NO_XRI) {
Dick Kennedy372c1872020-06-30 14:50:00 -07004165 lpfc_printf_log(phba, KERN_ERR,
4166 LOG_TRACE_EVENT,
James Smartf358dd02017-02-12 13:52:34 -08004167 "6307 Failed to allocate xri for "
4168 "NVMET sgl\n");
4169 rc = -ENOMEM;
4170 goto out_free_mem;
4171 }
4172 sglq_entry->sli4_lxritag = lxri;
4173 sglq_entry->sli4_xritag = phba->sli4_hba.xri_ids[lxri];
4174 }
4175 return 0;
4176
4177out_free_mem:
4178 lpfc_free_nvmet_sgl_list(phba);
4179 return rc;
4180}
4181
James Smart5e5b5112019-01-28 11:14:22 -08004182int
4183lpfc_io_buf_flush(struct lpfc_hba *phba, struct list_head *cbuf)
4184{
4185 LIST_HEAD(blist);
4186 struct lpfc_sli4_hdw_queue *qp;
James Smartc4908502019-01-28 11:14:28 -08004187 struct lpfc_io_buf *lpfc_cmd;
4188 struct lpfc_io_buf *iobufp, *prev_iobufp;
James Smart5e5b5112019-01-28 11:14:22 -08004189 int idx, cnt, xri, inserted;
4190
4191 cnt = 0;
4192 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
4193 qp = &phba->sli4_hba.hdwq[idx];
4194 spin_lock_irq(&qp->io_buf_list_get_lock);
4195 spin_lock(&qp->io_buf_list_put_lock);
4196
4197 /* Take everything off the get and put lists */
4198 list_splice_init(&qp->lpfc_io_buf_list_get, &blist);
4199 list_splice(&qp->lpfc_io_buf_list_put, &blist);
4200 INIT_LIST_HEAD(&qp->lpfc_io_buf_list_get);
4201 INIT_LIST_HEAD(&qp->lpfc_io_buf_list_put);
4202 cnt += qp->get_io_bufs + qp->put_io_bufs;
4203 qp->get_io_bufs = 0;
4204 qp->put_io_bufs = 0;
4205 qp->total_io_bufs = 0;
4206 spin_unlock(&qp->io_buf_list_put_lock);
4207 spin_unlock_irq(&qp->io_buf_list_get_lock);
4208 }
4209
4210 /*
4211 * Take IO buffers off blist and put on cbuf sorted by XRI.
4212 * This is because POST_SGL takes a sequential range of XRIs
4213 * to post to the firmware.
4214 */
4215 for (idx = 0; idx < cnt; idx++) {
James Smartc4908502019-01-28 11:14:28 -08004216 list_remove_head(&blist, lpfc_cmd, struct lpfc_io_buf, list);
James Smart5e5b5112019-01-28 11:14:22 -08004217 if (!lpfc_cmd)
4218 return cnt;
4219 if (idx == 0) {
4220 list_add_tail(&lpfc_cmd->list, cbuf);
4221 continue;
4222 }
4223 xri = lpfc_cmd->cur_iocbq.sli4_xritag;
4224 inserted = 0;
4225 prev_iobufp = NULL;
4226 list_for_each_entry(iobufp, cbuf, list) {
4227 if (xri < iobufp->cur_iocbq.sli4_xritag) {
4228 if (prev_iobufp)
4229 list_add(&lpfc_cmd->list,
4230 &prev_iobufp->list);
4231 else
4232 list_add(&lpfc_cmd->list, cbuf);
4233 inserted = 1;
4234 break;
4235 }
4236 prev_iobufp = iobufp;
4237 }
4238 if (!inserted)
4239 list_add_tail(&lpfc_cmd->list, cbuf);
4240 }
4241 return cnt;
4242}
4243
4244int
4245lpfc_io_buf_replenish(struct lpfc_hba *phba, struct list_head *cbuf)
4246{
4247 struct lpfc_sli4_hdw_queue *qp;
James Smartc4908502019-01-28 11:14:28 -08004248 struct lpfc_io_buf *lpfc_cmd;
James Smart5e5b5112019-01-28 11:14:22 -08004249 int idx, cnt;
4250
4251 qp = phba->sli4_hba.hdwq;
4252 cnt = 0;
4253 while (!list_empty(cbuf)) {
4254 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
4255 list_remove_head(cbuf, lpfc_cmd,
James Smartc4908502019-01-28 11:14:28 -08004256 struct lpfc_io_buf, list);
James Smart5e5b5112019-01-28 11:14:22 -08004257 if (!lpfc_cmd)
4258 return cnt;
4259 cnt++;
4260 qp = &phba->sli4_hba.hdwq[idx];
James Smart1fbf9742019-01-28 11:14:26 -08004261 lpfc_cmd->hdwq_no = idx;
4262 lpfc_cmd->hdwq = qp;
James Smart5e5b5112019-01-28 11:14:22 -08004263 lpfc_cmd->cur_iocbq.wqe_cmpl = NULL;
4264 lpfc_cmd->cur_iocbq.iocb_cmpl = NULL;
4265 spin_lock(&qp->io_buf_list_put_lock);
4266 list_add_tail(&lpfc_cmd->list,
4267 &qp->lpfc_io_buf_list_put);
4268 qp->put_io_bufs++;
4269 qp->total_io_bufs++;
4270 spin_unlock(&qp->io_buf_list_put_lock);
4271 }
4272 }
4273 return cnt;
4274}
4275
James Smartf358dd02017-02-12 13:52:34 -08004276/**
James Smart5e5b5112019-01-28 11:14:22 -08004277 * lpfc_sli4_io_sgl_update - update xri-sgl sizing and mapping
James Smart895427b2017-02-12 13:52:30 -08004278 * @phba: pointer to lpfc hba data structure.
4279 *
4280 * This routine first calculates the sizes of the current els and allocated
4281 * scsi sgl lists, and then goes through all sgls to updates the physical
4282 * XRIs assigned due to port function reset. During port initialization, the
4283 * current els and allocated scsi sgl lists are 0s.
4284 *
4285 * Return codes
4286 * 0 - successful (for now, it always returns 0)
4287 **/
4288int
James Smart5e5b5112019-01-28 11:14:22 -08004289lpfc_sli4_io_sgl_update(struct lpfc_hba *phba)
James Smart895427b2017-02-12 13:52:30 -08004290{
James Smartc4908502019-01-28 11:14:28 -08004291 struct lpfc_io_buf *lpfc_ncmd = NULL, *lpfc_ncmd_next = NULL;
James Smart0794d602019-01-28 11:14:19 -08004292 uint16_t i, lxri, els_xri_cnt;
James Smart5e5b5112019-01-28 11:14:22 -08004293 uint16_t io_xri_cnt, io_xri_max;
4294 LIST_HEAD(io_sgl_list);
James Smart0794d602019-01-28 11:14:19 -08004295 int rc, cnt;
James Smart895427b2017-02-12 13:52:30 -08004296
4297 /*
James Smart0794d602019-01-28 11:14:19 -08004298 * update on pci function's allocated nvme xri-sgl list
James Smart895427b2017-02-12 13:52:30 -08004299 */
James Smart0794d602019-01-28 11:14:19 -08004300
4301 /* maximum number of xris available for nvme buffers */
James Smart895427b2017-02-12 13:52:30 -08004302 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
James Smart5e5b5112019-01-28 11:14:22 -08004303 io_xri_max = phba->sli4_hba.max_cfg_param.max_xri - els_xri_cnt;
4304 phba->sli4_hba.io_xri_max = io_xri_max;
James Smart8a9d2e82012-05-09 21:16:12 -04004305
James Smarte8c0a772017-04-21 16:04:49 -07004306 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart0794d602019-01-28 11:14:19 -08004307 "6074 Current allocated XRI sgl count:%d, "
4308 "maximum XRI count:%d\n",
James Smart5e5b5112019-01-28 11:14:22 -08004309 phba->sli4_hba.io_xri_cnt,
4310 phba->sli4_hba.io_xri_max);
James Smarte8c0a772017-04-21 16:04:49 -07004311
James Smart5e5b5112019-01-28 11:14:22 -08004312 cnt = lpfc_io_buf_flush(phba, &io_sgl_list);
James Smart0794d602019-01-28 11:14:19 -08004313
James Smart5e5b5112019-01-28 11:14:22 -08004314 if (phba->sli4_hba.io_xri_cnt > phba->sli4_hba.io_xri_max) {
James Smart0794d602019-01-28 11:14:19 -08004315 /* max nvme xri shrunk below the allocated nvme buffers */
James Smart5e5b5112019-01-28 11:14:22 -08004316 io_xri_cnt = phba->sli4_hba.io_xri_cnt -
4317 phba->sli4_hba.io_xri_max;
James Smart0794d602019-01-28 11:14:19 -08004318 /* release the extra allocated nvme buffers */
James Smart5e5b5112019-01-28 11:14:22 -08004319 for (i = 0; i < io_xri_cnt; i++) {
4320 list_remove_head(&io_sgl_list, lpfc_ncmd,
James Smartc4908502019-01-28 11:14:28 -08004321 struct lpfc_io_buf, list);
James Smart0794d602019-01-28 11:14:19 -08004322 if (lpfc_ncmd) {
Romain Perier771db5c2017-07-06 10:13:05 +02004323 dma_pool_free(phba->lpfc_sg_dma_buf_pool,
James Smart0794d602019-01-28 11:14:19 -08004324 lpfc_ncmd->data,
4325 lpfc_ncmd->dma_handle);
4326 kfree(lpfc_ncmd);
James Smarta2fc4aef2014-09-03 12:57:55 -04004327 }
James Smart8a9d2e82012-05-09 21:16:12 -04004328 }
James Smart5e5b5112019-01-28 11:14:22 -08004329 phba->sli4_hba.io_xri_cnt -= io_xri_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04004330 }
4331
James Smart0794d602019-01-28 11:14:19 -08004332 /* update xris associated to remaining allocated nvme buffers */
4333 lpfc_ncmd = NULL;
4334 lpfc_ncmd_next = NULL;
James Smart5e5b5112019-01-28 11:14:22 -08004335 phba->sli4_hba.io_xri_cnt = cnt;
James Smart0794d602019-01-28 11:14:19 -08004336 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
James Smart5e5b5112019-01-28 11:14:22 -08004337 &io_sgl_list, list) {
James Smart8a9d2e82012-05-09 21:16:12 -04004338 lxri = lpfc_sli4_next_xritag(phba);
4339 if (lxri == NO_XRI) {
Dick Kennedy372c1872020-06-30 14:50:00 -07004340 lpfc_printf_log(phba, KERN_ERR,
4341 LOG_TRACE_EVENT,
James Smart0794d602019-01-28 11:14:19 -08004342 "6075 Failed to allocate xri for "
4343 "nvme buffer\n");
James Smart8a9d2e82012-05-09 21:16:12 -04004344 rc = -ENOMEM;
4345 goto out_free_mem;
4346 }
James Smart0794d602019-01-28 11:14:19 -08004347 lpfc_ncmd->cur_iocbq.sli4_lxritag = lxri;
4348 lpfc_ncmd->cur_iocbq.sli4_xritag = phba->sli4_hba.xri_ids[lxri];
James Smart8a9d2e82012-05-09 21:16:12 -04004349 }
James Smart5e5b5112019-01-28 11:14:22 -08004350 cnt = lpfc_io_buf_replenish(phba, &io_sgl_list);
dea31012005-04-17 16:05:31 -05004351 return 0;
James Smart8a9d2e82012-05-09 21:16:12 -04004352
4353out_free_mem:
James Smart5e5b5112019-01-28 11:14:22 -08004354 lpfc_io_free(phba);
James Smart8a9d2e82012-05-09 21:16:12 -04004355 return rc;
dea31012005-04-17 16:05:31 -05004356}
4357
James Smart0794d602019-01-28 11:14:19 -08004358/**
James Smart5e5b5112019-01-28 11:14:22 -08004359 * lpfc_new_io_buf - IO buffer allocator for HBA with SLI4 IF spec
Lee Jonesfe614ac2020-07-23 13:24:22 +01004360 * @phba: Pointer to lpfc hba data structure.
4361 * @num_to_alloc: The requested number of buffers to allocate.
James Smart0794d602019-01-28 11:14:19 -08004362 *
4363 * This routine allocates nvme buffers for device with SLI-4 interface spec,
4364 * the nvme buffer contains all the necessary information needed to initiate
4365 * an I/O. After allocating up to @num_to_allocate IO buffers and put
4366 * them on a list, it post them to the port by using SGL block post.
4367 *
4368 * Return codes:
James Smart5e5b5112019-01-28 11:14:22 -08004369 * int - number of IO buffers that were allocated and posted.
James Smart0794d602019-01-28 11:14:19 -08004370 * 0 = failure, less than num_to_alloc is a partial failure.
4371 **/
4372int
James Smart5e5b5112019-01-28 11:14:22 -08004373lpfc_new_io_buf(struct lpfc_hba *phba, int num_to_alloc)
James Smart0794d602019-01-28 11:14:19 -08004374{
James Smartc4908502019-01-28 11:14:28 -08004375 struct lpfc_io_buf *lpfc_ncmd;
James Smart0794d602019-01-28 11:14:19 -08004376 struct lpfc_iocbq *pwqeq;
4377 uint16_t iotag, lxri = 0;
4378 int bcnt, num_posted;
4379 LIST_HEAD(prep_nblist);
4380 LIST_HEAD(post_nblist);
4381 LIST_HEAD(nvme_nblist);
4382
James Smart5e5b5112019-01-28 11:14:22 -08004383 phba->sli4_hba.io_xri_cnt = 0;
James Smart0794d602019-01-28 11:14:19 -08004384 for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
James Smart7f9989b2019-08-27 14:27:46 -07004385 lpfc_ncmd = kzalloc(sizeof(*lpfc_ncmd), GFP_KERNEL);
James Smart0794d602019-01-28 11:14:19 -08004386 if (!lpfc_ncmd)
4387 break;
4388 /*
4389 * Get memory from the pci pool to map the virt space to
4390 * pci bus space for an I/O. The DMA buffer includes the
4391 * number of SGE's necessary to support the sg_tablesize.
4392 */
Thomas Meyera5c990e2019-05-29 22:21:36 +02004393 lpfc_ncmd->data = dma_pool_zalloc(phba->lpfc_sg_dma_buf_pool,
4394 GFP_KERNEL,
4395 &lpfc_ncmd->dma_handle);
James Smart0794d602019-01-28 11:14:19 -08004396 if (!lpfc_ncmd->data) {
4397 kfree(lpfc_ncmd);
4398 break;
4399 }
James Smart0794d602019-01-28 11:14:19 -08004400
James Smartd79c9e92019-08-14 16:57:09 -07004401 if (phba->cfg_xpsgl && !phba->nvmet_support) {
4402 INIT_LIST_HEAD(&lpfc_ncmd->dma_sgl_xtra_list);
4403 } else {
4404 /*
4405 * 4K Page alignment is CRITICAL to BlockGuard, double
4406 * check to be sure.
4407 */
4408 if ((phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
4409 (((unsigned long)(lpfc_ncmd->data) &
4410 (unsigned long)(SLI4_PAGE_SIZE - 1)) != 0)) {
Dick Kennedy372c1872020-06-30 14:50:00 -07004411 lpfc_printf_log(phba, KERN_ERR,
4412 LOG_TRACE_EVENT,
James Smartd79c9e92019-08-14 16:57:09 -07004413 "3369 Memory alignment err: "
4414 "addr=%lx\n",
4415 (unsigned long)lpfc_ncmd->data);
4416 dma_pool_free(phba->lpfc_sg_dma_buf_pool,
4417 lpfc_ncmd->data,
4418 lpfc_ncmd->dma_handle);
4419 kfree(lpfc_ncmd);
4420 break;
4421 }
James Smart0794d602019-01-28 11:14:19 -08004422 }
4423
James Smartd79c9e92019-08-14 16:57:09 -07004424 INIT_LIST_HEAD(&lpfc_ncmd->dma_cmd_rsp_list);
4425
James Smart0794d602019-01-28 11:14:19 -08004426 lxri = lpfc_sli4_next_xritag(phba);
4427 if (lxri == NO_XRI) {
4428 dma_pool_free(phba->lpfc_sg_dma_buf_pool,
4429 lpfc_ncmd->data, lpfc_ncmd->dma_handle);
4430 kfree(lpfc_ncmd);
4431 break;
4432 }
4433 pwqeq = &lpfc_ncmd->cur_iocbq;
4434
4435 /* Allocate iotag for lpfc_ncmd->cur_iocbq. */
4436 iotag = lpfc_sli_next_iotag(phba, pwqeq);
4437 if (iotag == 0) {
4438 dma_pool_free(phba->lpfc_sg_dma_buf_pool,
4439 lpfc_ncmd->data, lpfc_ncmd->dma_handle);
4440 kfree(lpfc_ncmd);
Dick Kennedy372c1872020-06-30 14:50:00 -07004441 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart0794d602019-01-28 11:14:19 -08004442 "6121 Failed to allocate IOTAG for"
4443 " XRI:0x%x\n", lxri);
4444 lpfc_sli4_free_xri(phba, lxri);
4445 break;
4446 }
4447 pwqeq->sli4_lxritag = lxri;
4448 pwqeq->sli4_xritag = phba->sli4_hba.xri_ids[lxri];
4449 pwqeq->context1 = lpfc_ncmd;
4450
4451 /* Initialize local short-hand pointers. */
4452 lpfc_ncmd->dma_sgl = lpfc_ncmd->data;
4453 lpfc_ncmd->dma_phys_sgl = lpfc_ncmd->dma_handle;
4454 lpfc_ncmd->cur_iocbq.context1 = lpfc_ncmd;
James Smartc2017262019-01-28 11:14:37 -08004455 spin_lock_init(&lpfc_ncmd->buf_lock);
James Smart0794d602019-01-28 11:14:19 -08004456
4457 /* add the nvme buffer to a post list */
4458 list_add_tail(&lpfc_ncmd->list, &post_nblist);
James Smart5e5b5112019-01-28 11:14:22 -08004459 phba->sli4_hba.io_xri_cnt++;
James Smart0794d602019-01-28 11:14:19 -08004460 }
4461 lpfc_printf_log(phba, KERN_INFO, LOG_NVME,
4462 "6114 Allocate %d out of %d requested new NVME "
4463 "buffers\n", bcnt, num_to_alloc);
4464
4465 /* post the list of nvme buffer sgls to port if available */
4466 if (!list_empty(&post_nblist))
James Smart5e5b5112019-01-28 11:14:22 -08004467 num_posted = lpfc_sli4_post_io_sgl_list(
James Smart0794d602019-01-28 11:14:19 -08004468 phba, &post_nblist, bcnt);
4469 else
4470 num_posted = 0;
4471
4472 return num_posted;
4473}
4474
James Smart96418b52017-03-04 09:30:31 -08004475static uint64_t
4476lpfc_get_wwpn(struct lpfc_hba *phba)
4477{
4478 uint64_t wwn;
4479 int rc;
4480 LPFC_MBOXQ_t *mboxq;
4481 MAILBOX_t *mb;
4482
James Smart96418b52017-03-04 09:30:31 -08004483 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
4484 GFP_KERNEL);
4485 if (!mboxq)
4486 return (uint64_t)-1;
4487
4488 /* First get WWN of HBA instance */
4489 lpfc_read_nv(phba, mboxq);
4490 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4491 if (rc != MBX_SUCCESS) {
Dick Kennedy372c1872020-06-30 14:50:00 -07004492 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart96418b52017-03-04 09:30:31 -08004493 "6019 Mailbox failed , mbxCmd x%x "
4494 "READ_NV, mbxStatus x%x\n",
4495 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
4496 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
4497 mempool_free(mboxq, phba->mbox_mem_pool);
4498 return (uint64_t) -1;
4499 }
4500 mb = &mboxq->u.mb;
4501 memcpy(&wwn, (char *)mb->un.varRDnvp.portname, sizeof(uint64_t));
4502 /* wwn is WWPN of HBA instance */
4503 mempool_free(mboxq, phba->mbox_mem_pool);
4504 if (phba->sli_rev == LPFC_SLI_REV4)
4505 return be64_to_cpu(wwn);
4506 else
Maurizio Lombardi286871a2017-08-23 16:55:48 -07004507 return rol64(wwn, 32);
James Smart96418b52017-03-04 09:30:31 -08004508}
4509
James Smarte59058c2008-08-24 21:49:00 -04004510/**
Gaurav Srivastava5e633302021-06-08 10:05:49 +05304511 * lpfc_vmid_res_alloc - Allocates resources for VMID
4512 * @phba: pointer to lpfc hba data structure.
4513 * @vport: pointer to vport data structure
4514 *
4515 * This routine allocated the resources needed for the VMID.
4516 *
4517 * Return codes
4518 * 0 on Success
4519 * Non-0 on Failure
4520 */
4521static int
4522lpfc_vmid_res_alloc(struct lpfc_hba *phba, struct lpfc_vport *vport)
4523{
4524 /* VMID feature is supported only on SLI4 */
4525 if (phba->sli_rev == LPFC_SLI_REV3) {
4526 phba->cfg_vmid_app_header = 0;
4527 phba->cfg_vmid_priority_tagging = 0;
4528 }
4529
4530 if (lpfc_is_vmid_enabled(phba)) {
4531 vport->vmid =
4532 kcalloc(phba->cfg_max_vmid, sizeof(struct lpfc_vmid),
4533 GFP_KERNEL);
4534 if (!vport->vmid)
4535 return -ENOMEM;
4536
4537 rwlock_init(&vport->vmid_lock);
4538
4539 /* Set the VMID parameters for the vport */
4540 vport->vmid_priority_tagging = phba->cfg_vmid_priority_tagging;
4541 vport->vmid_inactivity_timeout =
4542 phba->cfg_vmid_inactivity_timeout;
4543 vport->max_vmid = phba->cfg_max_vmid;
4544 vport->cur_vmid_cnt = 0;
4545
4546 vport->vmid_priority_range = bitmap_zalloc
4547 (LPFC_VMID_MAX_PRIORITY_RANGE, GFP_KERNEL);
4548
4549 if (!vport->vmid_priority_range) {
4550 kfree(vport->vmid);
4551 return -ENOMEM;
4552 }
4553
4554 hash_init(vport->hash_table);
4555 }
4556 return 0;
4557}
4558
4559/**
James Smart3621a712009-04-06 18:47:14 -04004560 * lpfc_create_port - Create an FC port
James Smarte59058c2008-08-24 21:49:00 -04004561 * @phba: pointer to lpfc hba data structure.
4562 * @instance: a unique integer ID to this FC port.
4563 * @dev: pointer to the device data structure.
4564 *
4565 * This routine creates a FC port for the upper layer protocol. The FC port
4566 * can be created on top of either a physical port or a virtual port provided
4567 * by the HBA. This routine also allocates a SCSI host data structure (shost)
4568 * and associates the FC port created before adding the shost into the SCSI
4569 * layer.
4570 *
4571 * Return codes
4572 * @vport - pointer to the virtual N_Port data structure.
4573 * NULL - port create failed.
4574 **/
James Smart2e0fef82007-06-17 19:56:36 -05004575struct lpfc_vport *
James Smart3de2a652007-08-02 11:09:59 -04004576lpfc_create_port(struct lpfc_hba *phba, int instance, struct device *dev)
James Smart47a86172007-04-25 09:53:22 -04004577{
James Smart2e0fef82007-06-17 19:56:36 -05004578 struct lpfc_vport *vport;
James Smart895427b2017-02-12 13:52:30 -08004579 struct Scsi_Host *shost = NULL;
James Smartc90b4482020-03-22 11:12:56 -07004580 struct scsi_host_template *template;
James Smart2e0fef82007-06-17 19:56:36 -05004581 int error = 0;
James Smart96418b52017-03-04 09:30:31 -08004582 int i;
4583 uint64_t wwn;
4584 bool use_no_reset_hba = false;
James Smart56bc8022017-06-15 22:56:43 -07004585 int rc;
James Smart96418b52017-03-04 09:30:31 -08004586
James Smart56bc8022017-06-15 22:56:43 -07004587 if (lpfc_no_hba_reset_cnt) {
4588 if (phba->sli_rev < LPFC_SLI_REV4 &&
4589 dev == &phba->pcidev->dev) {
4590 /* Reset the port first */
4591 lpfc_sli_brdrestart(phba);
4592 rc = lpfc_sli_chipset_init(phba);
4593 if (rc)
4594 return NULL;
4595 }
4596 wwn = lpfc_get_wwpn(phba);
4597 }
James Smart96418b52017-03-04 09:30:31 -08004598
4599 for (i = 0; i < lpfc_no_hba_reset_cnt; i++) {
4600 if (wwn == lpfc_no_hba_reset[i]) {
Dick Kennedy372c1872020-06-30 14:50:00 -07004601 lpfc_printf_log(phba, KERN_ERR,
4602 LOG_TRACE_EVENT,
James Smart96418b52017-03-04 09:30:31 -08004603 "6020 Setting use_no_reset port=%llx\n",
4604 wwn);
4605 use_no_reset_hba = true;
4606 break;
4607 }
4608 }
James Smart47a86172007-04-25 09:53:22 -04004609
James Smartc90b4482020-03-22 11:12:56 -07004610 /* Seed template for SCSI host registration */
4611 if (dev == &phba->pcidev->dev) {
4612 template = &phba->port_template;
4613
4614 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) {
4615 /* Seed physical port template */
4616 memcpy(template, &lpfc_template, sizeof(*template));
4617
James Smart7c30bb62020-10-20 13:27:16 -07004618 if (use_no_reset_hba)
James Smartc90b4482020-03-22 11:12:56 -07004619 /* template is for a no reset SCSI Host */
James Smartc90b4482020-03-22 11:12:56 -07004620 template->eh_host_reset_handler = NULL;
James Smartc90b4482020-03-22 11:12:56 -07004621
4622 /* Template for all vports this physical port creates */
4623 memcpy(&phba->vport_template, &lpfc_template,
4624 sizeof(*template));
Bart Van Assche08adfa72021-10-12 16:35:39 -07004625 phba->vport_template.shost_groups = lpfc_vport_groups;
James Smartc90b4482020-03-22 11:12:56 -07004626 phba->vport_template.eh_bus_reset_handler = NULL;
4627 phba->vport_template.eh_host_reset_handler = NULL;
4628 phba->vport_template.vendor_id = 0;
4629
4630 /* Initialize the host templates with updated value */
4631 if (phba->sli_rev == LPFC_SLI_REV4) {
4632 template->sg_tablesize = phba->cfg_scsi_seg_cnt;
4633 phba->vport_template.sg_tablesize =
4634 phba->cfg_scsi_seg_cnt;
4635 } else {
4636 template->sg_tablesize = phba->cfg_sg_seg_cnt;
4637 phba->vport_template.sg_tablesize =
4638 phba->cfg_sg_seg_cnt;
4639 }
4640
James Smart895427b2017-02-12 13:52:30 -08004641 } else {
James Smartc90b4482020-03-22 11:12:56 -07004642 /* NVMET is for physical port only */
4643 memcpy(template, &lpfc_template_nvme,
4644 sizeof(*template));
James Smart895427b2017-02-12 13:52:30 -08004645 }
James Smartc90b4482020-03-22 11:12:56 -07004646 } else {
4647 template = &phba->vport_template;
James Smartea4142f2015-04-07 15:07:13 -04004648 }
James Smartc90b4482020-03-22 11:12:56 -07004649
4650 shost = scsi_host_alloc(template, sizeof(struct lpfc_vport));
James Smart2e0fef82007-06-17 19:56:36 -05004651 if (!shost)
4652 goto out;
James Smart47a86172007-04-25 09:53:22 -04004653
James Smart2e0fef82007-06-17 19:56:36 -05004654 vport = (struct lpfc_vport *) shost->hostdata;
4655 vport->phba = phba;
James Smart2e0fef82007-06-17 19:56:36 -05004656 vport->load_flag |= FC_LOADING;
James Smart92d7f7b2007-06-17 19:56:38 -05004657 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart7f5f3d02008-02-08 18:50:14 -05004658 vport->fc_rscn_flush = 0;
James Smart3de2a652007-08-02 11:09:59 -04004659 lpfc_get_vport_cfgparam(vport);
James Smart895427b2017-02-12 13:52:30 -08004660
James Smartf6e84792019-01-28 11:14:38 -08004661 /* Adjust value in vport */
4662 vport->cfg_enable_fc4_type = phba->cfg_enable_fc4_type;
4663
James Smart2e0fef82007-06-17 19:56:36 -05004664 shost->unique_id = instance;
4665 shost->max_id = LPFC_MAX_TARGET;
James Smart3de2a652007-08-02 11:09:59 -04004666 shost->max_lun = vport->cfg_max_luns;
James Smart2e0fef82007-06-17 19:56:36 -05004667 shost->this_id = -1;
4668 shost->max_cmd_len = 16;
James Smart6a828b02019-01-28 11:14:31 -08004669
James Smartda0436e2009-05-22 14:51:39 -04004670 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart77ffd342019-08-15 19:36:49 -07004671 if (!phba->cfg_fcp_mq_threshold ||
4672 phba->cfg_fcp_mq_threshold > phba->cfg_hdw_queue)
4673 phba->cfg_fcp_mq_threshold = phba->cfg_hdw_queue;
4674
4675 shost->nr_hw_queues = min_t(int, 2 * num_possible_nodes(),
4676 phba->cfg_fcp_mq_threshold);
James Smart6a828b02019-01-28 11:14:31 -08004677
James Smart28baac72010-02-12 14:42:03 -05004678 shost->dma_boundary =
James Smartcb5172e2010-03-15 11:25:07 -04004679 phba->sli4_hba.pc_sli4_params.sge_supp_len-1;
James Smartd79c9e92019-08-14 16:57:09 -07004680
4681 if (phba->cfg_xpsgl && !phba->nvmet_support)
4682 shost->sg_tablesize = LPFC_MAX_SG_TABLESIZE;
4683 else
4684 shost->sg_tablesize = phba->cfg_scsi_seg_cnt;
James Smartace44e42019-01-28 11:14:27 -08004685 } else
4686 /* SLI-3 has a limited number of hardware queues (3),
4687 * thus there is only one for FCP processing.
4688 */
4689 shost->nr_hw_queues = 1;
James Smart81301a92008-12-04 22:39:46 -05004690
James Smart47a86172007-04-25 09:53:22 -04004691 /*
James Smart2e0fef82007-06-17 19:56:36 -05004692 * Set initial can_queue value since 0 is no longer supported and
4693 * scsi_add_host will fail. This will be adjusted later based on the
4694 * max xri value determined in hba setup.
James Smart47a86172007-04-25 09:53:22 -04004695 */
James Smart2e0fef82007-06-17 19:56:36 -05004696 shost->can_queue = phba->cfg_hba_queue_depth - 10;
James Smart3de2a652007-08-02 11:09:59 -04004697 if (dev != &phba->pcidev->dev) {
James Smart92d7f7b2007-06-17 19:56:38 -05004698 shost->transportt = lpfc_vport_transport_template;
4699 vport->port_type = LPFC_NPIV_PORT;
4700 } else {
4701 shost->transportt = lpfc_transport_template;
4702 vport->port_type = LPFC_PHYSICAL_PORT;
4703 }
James Smart47a86172007-04-25 09:53:22 -04004704
James Smartc90b4482020-03-22 11:12:56 -07004705 lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_FCP,
4706 "9081 CreatePort TMPLATE type %x TBLsize %d "
4707 "SEGcnt %d/%d\n",
4708 vport->port_type, shost->sg_tablesize,
4709 phba->cfg_scsi_seg_cnt, phba->cfg_sg_seg_cnt);
4710
Gaurav Srivastava5e633302021-06-08 10:05:49 +05304711 /* Allocate the resources for VMID */
4712 rc = lpfc_vmid_res_alloc(phba, vport);
4713
4714 if (rc)
4715 goto out;
4716
James Smart2e0fef82007-06-17 19:56:36 -05004717 /* Initialize all internally managed lists. */
4718 INIT_LIST_HEAD(&vport->fc_nodes);
James Smartda0436e2009-05-22 14:51:39 -04004719 INIT_LIST_HEAD(&vport->rcv_buffer_list);
James Smart2e0fef82007-06-17 19:56:36 -05004720 spin_lock_init(&vport->work_port_lock);
James Smart47a86172007-04-25 09:53:22 -04004721
Kees Cookf22eb4d2017-09-06 20:24:26 -07004722 timer_setup(&vport->fc_disctmo, lpfc_disc_timeout, 0);
James Smart47a86172007-04-25 09:53:22 -04004723
Kees Cookf22eb4d2017-09-06 20:24:26 -07004724 timer_setup(&vport->els_tmofunc, lpfc_els_timeout, 0);
James Smart92494142011-02-16 12:39:44 -05004725
Kees Cookf22eb4d2017-09-06 20:24:26 -07004726 timer_setup(&vport->delayed_disc_tmo, lpfc_delayed_disc_tmo, 0);
James Smart92494142011-02-16 12:39:44 -05004727
James Smartaa6ff302019-05-21 17:49:09 -07004728 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED)
4729 lpfc_setup_bg(phba, shost);
4730
James Bottomleyd139b9b2009-11-05 13:33:12 -06004731 error = scsi_add_host_with_dma(shost, dev, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05004732 if (error)
4733 goto out_put_shost;
James Smart47a86172007-04-25 09:53:22 -04004734
James Smart523128e2018-09-10 10:30:46 -07004735 spin_lock_irq(&phba->port_list_lock);
James Smart2e0fef82007-06-17 19:56:36 -05004736 list_add_tail(&vport->listentry, &phba->port_list);
James Smart523128e2018-09-10 10:30:46 -07004737 spin_unlock_irq(&phba->port_list_lock);
James Smart2e0fef82007-06-17 19:56:36 -05004738 return vport;
James Smart47a86172007-04-25 09:53:22 -04004739
James Smart2e0fef82007-06-17 19:56:36 -05004740out_put_shost:
Gaurav Srivastava5e633302021-06-08 10:05:49 +05304741 kfree(vport->vmid);
4742 bitmap_free(vport->vmid_priority_range);
James Smart2e0fef82007-06-17 19:56:36 -05004743 scsi_host_put(shost);
4744out:
4745 return NULL;
James Smart47a86172007-04-25 09:53:22 -04004746}
4747
James Smarte59058c2008-08-24 21:49:00 -04004748/**
James Smart3621a712009-04-06 18:47:14 -04004749 * destroy_port - destroy an FC port
James Smarte59058c2008-08-24 21:49:00 -04004750 * @vport: pointer to an lpfc virtual N_Port data structure.
4751 *
4752 * This routine destroys a FC port from the upper layer protocol. All the
4753 * resources associated with the port are released.
4754 **/
James Smart2e0fef82007-06-17 19:56:36 -05004755void
4756destroy_port(struct lpfc_vport *vport)
James Smart47a86172007-04-25 09:53:22 -04004757{
James Smart92d7f7b2007-06-17 19:56:38 -05004758 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4759 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04004760
James Smart858c9f62007-06-17 19:56:39 -05004761 lpfc_debugfs_terminate(vport);
James Smart92d7f7b2007-06-17 19:56:38 -05004762 fc_remove_host(shost);
4763 scsi_remove_host(shost);
James Smart47a86172007-04-25 09:53:22 -04004764
James Smart523128e2018-09-10 10:30:46 -07004765 spin_lock_irq(&phba->port_list_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05004766 list_del_init(&vport->listentry);
James Smart523128e2018-09-10 10:30:46 -07004767 spin_unlock_irq(&phba->port_list_lock);
James Smart47a86172007-04-25 09:53:22 -04004768
James Smart92d7f7b2007-06-17 19:56:38 -05004769 lpfc_cleanup(vport);
James Smart47a86172007-04-25 09:53:22 -04004770 return;
James Smart47a86172007-04-25 09:53:22 -04004771}
4772
James Smarte59058c2008-08-24 21:49:00 -04004773/**
James Smart3621a712009-04-06 18:47:14 -04004774 * lpfc_get_instance - Get a unique integer ID
James Smarte59058c2008-08-24 21:49:00 -04004775 *
4776 * This routine allocates a unique integer ID from lpfc_hba_index pool. It
4777 * uses the kernel idr facility to perform the task.
4778 *
4779 * Return codes:
4780 * instance - a unique integer ID allocated as the new instance.
4781 * -1 - lpfc get instance failed.
4782 **/
James Smart92d7f7b2007-06-17 19:56:38 -05004783int
4784lpfc_get_instance(void)
4785{
Tejun Heoab516032013-02-27 17:04:44 -08004786 int ret;
James Smart92d7f7b2007-06-17 19:56:38 -05004787
Tejun Heoab516032013-02-27 17:04:44 -08004788 ret = idr_alloc(&lpfc_hba_index, NULL, 0, 0, GFP_KERNEL);
4789 return ret < 0 ? -1 : ret;
James Smart92d7f7b2007-06-17 19:56:38 -05004790}
4791
James Smarte59058c2008-08-24 21:49:00 -04004792/**
James Smart3621a712009-04-06 18:47:14 -04004793 * lpfc_scan_finished - method for SCSI layer to detect whether scan is done
James Smarte59058c2008-08-24 21:49:00 -04004794 * @shost: pointer to SCSI host data structure.
4795 * @time: elapsed time of the scan in jiffies.
4796 *
4797 * This routine is called by the SCSI layer with a SCSI host to determine
4798 * whether the scan host is finished.
4799 *
4800 * Note: there is no scan_start function as adapter initialization will have
4801 * asynchronously kicked off the link initialization.
4802 *
4803 * Return codes
4804 * 0 - SCSI host scan is not over yet.
4805 * 1 - SCSI host scan is over.
4806 **/
James Smart47a86172007-04-25 09:53:22 -04004807int lpfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
4808{
James Smart2e0fef82007-06-17 19:56:36 -05004809 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4810 struct lpfc_hba *phba = vport->phba;
James Smart858c9f62007-06-17 19:56:39 -05004811 int stat = 0;
James Smart47a86172007-04-25 09:53:22 -04004812
James Smart858c9f62007-06-17 19:56:39 -05004813 spin_lock_irq(shost->host_lock);
4814
James Smart51ef4c22007-08-02 11:10:31 -04004815 if (vport->load_flag & FC_UNLOADING) {
James Smart858c9f62007-06-17 19:56:39 -05004816 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04004817 goto finished;
James Smart858c9f62007-06-17 19:56:39 -05004818 }
James Smart256ec0d2013-04-17 20:14:58 -04004819 if (time >= msecs_to_jiffies(30 * 1000)) {
James Smart2e0fef82007-06-17 19:56:36 -05004820 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004821 "0461 Scanning longer than 30 "
4822 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05004823 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04004824 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05004825 }
James Smart256ec0d2013-04-17 20:14:58 -04004826 if (time >= msecs_to_jiffies(15 * 1000) &&
4827 phba->link_state <= LPFC_LINK_DOWN) {
James Smart2e0fef82007-06-17 19:56:36 -05004828 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004829 "0465 Link down longer than 15 "
4830 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05004831 stat = 1;
James Smart2e0fef82007-06-17 19:56:36 -05004832 goto finished;
James Smart47a86172007-04-25 09:53:22 -04004833 }
4834
James Smart2e0fef82007-06-17 19:56:36 -05004835 if (vport->port_state != LPFC_VPORT_READY)
James Smart858c9f62007-06-17 19:56:39 -05004836 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05004837 if (vport->num_disc_nodes || vport->fc_prli_sent)
James Smart858c9f62007-06-17 19:56:39 -05004838 goto finished;
James Smart256ec0d2013-04-17 20:14:58 -04004839 if (vport->fc_map_cnt == 0 && time < msecs_to_jiffies(2 * 1000))
James Smart858c9f62007-06-17 19:56:39 -05004840 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05004841 if ((phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) != 0)
James Smart858c9f62007-06-17 19:56:39 -05004842 goto finished;
4843
4844 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04004845
4846finished:
James Smart858c9f62007-06-17 19:56:39 -05004847 spin_unlock_irq(shost->host_lock);
4848 return stat;
James Smart92d7f7b2007-06-17 19:56:38 -05004849}
4850
Bart Van Assche3999df72019-03-28 11:06:16 -07004851static void lpfc_host_supported_speeds_set(struct Scsi_Host *shost)
James Smartcd713482018-10-23 13:41:01 -07004852{
4853 struct lpfc_vport *vport = (struct lpfc_vport *)shost->hostdata;
4854 struct lpfc_hba *phba = vport->phba;
4855
4856 fc_host_supported_speeds(shost) = 0;
Dick Kennedya1e4d3d2020-08-03 14:02:22 -07004857 /*
4858 * Avoid reporting supported link speed for FCoE as it can't be
4859 * controlled via FCoE.
4860 */
4861 if (phba->hba_flag & HBA_FCOE_MODE)
4862 return;
4863
James Smartbfc47782021-07-22 15:17:19 -07004864 if (phba->lmt & LMT_256Gb)
4865 fc_host_supported_speeds(shost) |= FC_PORTSPEED_256GBIT;
James Smart1dc5ec22018-10-23 13:41:11 -07004866 if (phba->lmt & LMT_128Gb)
4867 fc_host_supported_speeds(shost) |= FC_PORTSPEED_128GBIT;
James Smartcd713482018-10-23 13:41:01 -07004868 if (phba->lmt & LMT_64Gb)
4869 fc_host_supported_speeds(shost) |= FC_PORTSPEED_64GBIT;
4870 if (phba->lmt & LMT_32Gb)
4871 fc_host_supported_speeds(shost) |= FC_PORTSPEED_32GBIT;
4872 if (phba->lmt & LMT_16Gb)
4873 fc_host_supported_speeds(shost) |= FC_PORTSPEED_16GBIT;
4874 if (phba->lmt & LMT_10Gb)
4875 fc_host_supported_speeds(shost) |= FC_PORTSPEED_10GBIT;
4876 if (phba->lmt & LMT_8Gb)
4877 fc_host_supported_speeds(shost) |= FC_PORTSPEED_8GBIT;
4878 if (phba->lmt & LMT_4Gb)
4879 fc_host_supported_speeds(shost) |= FC_PORTSPEED_4GBIT;
4880 if (phba->lmt & LMT_2Gb)
4881 fc_host_supported_speeds(shost) |= FC_PORTSPEED_2GBIT;
4882 if (phba->lmt & LMT_1Gb)
4883 fc_host_supported_speeds(shost) |= FC_PORTSPEED_1GBIT;
4884}
4885
James Smarte59058c2008-08-24 21:49:00 -04004886/**
James Smart3621a712009-04-06 18:47:14 -04004887 * lpfc_host_attrib_init - Initialize SCSI host attributes on a FC port
James Smarte59058c2008-08-24 21:49:00 -04004888 * @shost: pointer to SCSI host data structure.
4889 *
4890 * This routine initializes a given SCSI host attributes on a FC port. The
4891 * SCSI host can be either on top of a physical port or a virtual port.
4892 **/
James Smart92d7f7b2007-06-17 19:56:38 -05004893void lpfc_host_attrib_init(struct Scsi_Host *shost)
4894{
4895 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4896 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04004897 /*
James Smart2e0fef82007-06-17 19:56:36 -05004898 * Set fixed host attributes. Must done after lpfc_sli_hba_setup().
James Smart47a86172007-04-25 09:53:22 -04004899 */
4900
James Smart2e0fef82007-06-17 19:56:36 -05004901 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
4902 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
James Smart47a86172007-04-25 09:53:22 -04004903 fc_host_supported_classes(shost) = FC_COS_CLASS3;
4904
4905 memset(fc_host_supported_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05004906 sizeof(fc_host_supported_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04004907 fc_host_supported_fc4s(shost)[2] = 1;
4908 fc_host_supported_fc4s(shost)[7] = 1;
4909
James Smart92d7f7b2007-06-17 19:56:38 -05004910 lpfc_vport_symbolic_node_name(vport, fc_host_symbolic_name(shost),
4911 sizeof fc_host_symbolic_name(shost));
James Smart47a86172007-04-25 09:53:22 -04004912
James Smartcd713482018-10-23 13:41:01 -07004913 lpfc_host_supported_speeds_set(shost);
James Smart47a86172007-04-25 09:53:22 -04004914
4915 fc_host_maxframe_size(shost) =
James Smart2e0fef82007-06-17 19:56:36 -05004916 (((uint32_t) vport->fc_sparam.cmn.bbRcvSizeMsb & 0x0F) << 8) |
4917 (uint32_t) vport->fc_sparam.cmn.bbRcvSizeLsb;
James Smart47a86172007-04-25 09:53:22 -04004918
Mike Christie0af5d702010-09-15 16:52:31 -05004919 fc_host_dev_loss_tmo(shost) = vport->cfg_devloss_tmo;
4920
James Smart47a86172007-04-25 09:53:22 -04004921 /* This value is also unchanging */
4922 memset(fc_host_active_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05004923 sizeof(fc_host_active_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04004924 fc_host_active_fc4s(shost)[2] = 1;
4925 fc_host_active_fc4s(shost)[7] = 1;
4926
James Smart92d7f7b2007-06-17 19:56:38 -05004927 fc_host_max_npiv_vports(shost) = phba->max_vpi;
James Smart47a86172007-04-25 09:53:22 -04004928 spin_lock_irq(shost->host_lock);
James Smart51ef4c22007-08-02 11:10:31 -04004929 vport->load_flag &= ~FC_LOADING;
James Smart47a86172007-04-25 09:53:22 -04004930 spin_unlock_irq(shost->host_lock);
James Smart47a86172007-04-25 09:53:22 -04004931}
dea31012005-04-17 16:05:31 -05004932
James Smarte59058c2008-08-24 21:49:00 -04004933/**
James Smartda0436e2009-05-22 14:51:39 -04004934 * lpfc_stop_port_s3 - Stop SLI3 device port
James Smarte59058c2008-08-24 21:49:00 -04004935 * @phba: pointer to lpfc hba data structure.
4936 *
James Smartda0436e2009-05-22 14:51:39 -04004937 * This routine is invoked to stop an SLI3 device port, it stops the device
4938 * from generating interrupts and stops the device driver's timers for the
4939 * device.
James Smarte59058c2008-08-24 21:49:00 -04004940 **/
James Smartdb2378e2008-02-08 18:49:51 -05004941static void
James Smartda0436e2009-05-22 14:51:39 -04004942lpfc_stop_port_s3(struct lpfc_hba *phba)
James Smartdb2378e2008-02-08 18:49:51 -05004943{
James Smartda0436e2009-05-22 14:51:39 -04004944 /* Clear all interrupt enable conditions */
4945 writel(0, phba->HCregaddr);
4946 readl(phba->HCregaddr); /* flush */
4947 /* Clear all pending interrupts */
4948 writel(0xffffffff, phba->HAregaddr);
4949 readl(phba->HAregaddr); /* flush */
James Smart93996272008-08-24 21:50:30 -04004950
James Smartda0436e2009-05-22 14:51:39 -04004951 /* Reset some HBA SLI setup states */
4952 lpfc_stop_hba_timers(phba);
4953 phba->pport->work_port_events = 0;
James Smartdb2378e2008-02-08 18:49:51 -05004954}
4955
James Smarte59058c2008-08-24 21:49:00 -04004956/**
James Smartda0436e2009-05-22 14:51:39 -04004957 * lpfc_stop_port_s4 - Stop SLI4 device port
James Smart5b75da22008-12-04 22:39:35 -05004958 * @phba: pointer to lpfc hba data structure.
4959 *
James Smartda0436e2009-05-22 14:51:39 -04004960 * This routine is invoked to stop an SLI4 device port, it stops the device
4961 * from generating interrupts and stops the device driver's timers for the
4962 * device.
4963 **/
4964static void
4965lpfc_stop_port_s4(struct lpfc_hba *phba)
4966{
4967 /* Reset some HBA SLI4 setup states */
4968 lpfc_stop_hba_timers(phba);
James Smartcdb42be2019-01-28 11:14:21 -08004969 if (phba->pport)
4970 phba->pport->work_port_events = 0;
James Smartda0436e2009-05-22 14:51:39 -04004971 phba->sli4_hba.intr_enable = 0;
James Smartda0436e2009-05-22 14:51:39 -04004972}
4973
4974/**
4975 * lpfc_stop_port - Wrapper function for stopping hba port
4976 * @phba: Pointer to HBA context object.
James Smart5b75da22008-12-04 22:39:35 -05004977 *
James Smartda0436e2009-05-22 14:51:39 -04004978 * This routine wraps the actual SLI3 or SLI4 hba stop port routine from
4979 * the API jump table function pointer from the lpfc_hba struct.
4980 **/
4981void
4982lpfc_stop_port(struct lpfc_hba *phba)
4983{
4984 phba->lpfc_stop_port(phba);
Dick Kennedyf485c182017-09-29 17:34:34 -07004985
4986 if (phba->wq)
4987 flush_workqueue(phba->wq);
James Smartda0436e2009-05-22 14:51:39 -04004988}
4989
4990/**
James Smartecfd03c2010-02-12 14:41:27 -05004991 * lpfc_fcf_redisc_wait_start_timer - Start fcf rediscover wait timer
4992 * @phba: Pointer to hba for which this call is being executed.
4993 *
4994 * This routine starts the timer waiting for the FCF rediscovery to complete.
4995 **/
4996void
4997lpfc_fcf_redisc_wait_start_timer(struct lpfc_hba *phba)
4998{
4999 unsigned long fcf_redisc_wait_tmo =
5000 (jiffies + msecs_to_jiffies(LPFC_FCF_REDISCOVER_WAIT_TMO));
5001 /* Start fcf rediscovery wait period timer */
5002 mod_timer(&phba->fcf.redisc_wait, fcf_redisc_wait_tmo);
5003 spin_lock_irq(&phba->hbalock);
5004 /* Allow action to new fcf asynchronous event */
5005 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
5006 /* Mark the FCF rediscovery pending state */
5007 phba->fcf.fcf_flag |= FCF_REDISC_PEND;
5008 spin_unlock_irq(&phba->hbalock);
5009}
5010
5011/**
5012 * lpfc_sli4_fcf_redisc_wait_tmo - FCF table rediscover wait timeout
Lee Jonesfe614ac2020-07-23 13:24:22 +01005013 * @t: Timer context used to obtain the pointer to lpfc hba data structure.
James Smartecfd03c2010-02-12 14:41:27 -05005014 *
5015 * This routine is invoked when waiting for FCF table rediscover has been
5016 * timed out. If new FCF record(s) has (have) been discovered during the
5017 * wait period, a new FCF event shall be added to the FCOE async event
5018 * list, and then worker thread shall be waked up for processing from the
5019 * worker thread context.
5020 **/
Rashika Kheriae399b222014-09-03 12:55:28 -04005021static void
Kees Cookf22eb4d2017-09-06 20:24:26 -07005022lpfc_sli4_fcf_redisc_wait_tmo(struct timer_list *t)
James Smartecfd03c2010-02-12 14:41:27 -05005023{
Kees Cookf22eb4d2017-09-06 20:24:26 -07005024 struct lpfc_hba *phba = from_timer(phba, t, fcf.redisc_wait);
James Smartecfd03c2010-02-12 14:41:27 -05005025
5026 /* Don't send FCF rediscovery event if timer cancelled */
5027 spin_lock_irq(&phba->hbalock);
5028 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
5029 spin_unlock_irq(&phba->hbalock);
5030 return;
5031 }
5032 /* Clear FCF rediscovery timer pending flag */
5033 phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
5034 /* FCF rediscovery event to worker thread */
5035 phba->fcf.fcf_flag |= FCF_REDISC_EVT;
5036 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05005037 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04005038 "2776 FCF rediscover quiescent timer expired\n");
James Smartecfd03c2010-02-12 14:41:27 -05005039 /* wake up worker thread */
5040 lpfc_worker_wake_up(phba);
5041}
5042
5043/**
Gaurav Srivastava20397172021-06-08 10:05:54 +05305044 * lpfc_vmid_poll - VMID timeout detection
James Smart50baa152021-07-07 11:43:35 -07005045 * @t: Timer context used to obtain the pointer to lpfc hba data structure.
Gaurav Srivastava20397172021-06-08 10:05:54 +05305046 *
5047 * This routine is invoked when there is no I/O on by a VM for the specified
5048 * amount of time. When this situation is detected, the VMID has to be
5049 * deregistered from the switch and all the local resources freed. The VMID
5050 * will be reassigned to the VM once the I/O begins.
5051 **/
5052static void
5053lpfc_vmid_poll(struct timer_list *t)
5054{
5055 struct lpfc_hba *phba = from_timer(phba, t, inactive_vmid_poll);
5056 u32 wake_up = 0;
5057
5058 /* check if there is a need to issue QFPA */
5059 if (phba->pport->vmid_priority_tagging) {
5060 wake_up = 1;
5061 phba->pport->work_port_events |= WORKER_CHECK_VMID_ISSUE_QFPA;
5062 }
5063
5064 /* Is the vmid inactivity timer enabled */
5065 if (phba->pport->vmid_inactivity_timeout ||
5066 phba->pport->load_flag & FC_DEREGISTER_ALL_APP_ID) {
5067 wake_up = 1;
5068 phba->pport->work_port_events |= WORKER_CHECK_INACTIVE_VMID;
5069 }
5070
5071 if (wake_up)
5072 lpfc_worker_wake_up(phba);
5073
5074 /* restart the timer for the next iteration */
5075 mod_timer(&phba->inactive_vmid_poll, jiffies + msecs_to_jiffies(1000 *
5076 LPFC_VMID_TIMER));
5077}
5078
5079/**
James Smartda0436e2009-05-22 14:51:39 -04005080 * lpfc_sli4_parse_latt_fault - Parse sli4 link-attention link fault code
5081 * @phba: pointer to lpfc hba data structure.
5082 * @acqe_link: pointer to the async link completion queue entry.
5083 *
James Smart23288b72018-05-04 20:37:53 -07005084 * This routine is to parse the SLI4 link-attention link fault code.
James Smartda0436e2009-05-22 14:51:39 -04005085 **/
James Smart23288b72018-05-04 20:37:53 -07005086static void
James Smartda0436e2009-05-22 14:51:39 -04005087lpfc_sli4_parse_latt_fault(struct lpfc_hba *phba,
5088 struct lpfc_acqe_link *acqe_link)
5089{
James Smartda0436e2009-05-22 14:51:39 -04005090 switch (bf_get(lpfc_acqe_link_fault, acqe_link)) {
5091 case LPFC_ASYNC_LINK_FAULT_NONE:
5092 case LPFC_ASYNC_LINK_FAULT_LOCAL:
5093 case LPFC_ASYNC_LINK_FAULT_REMOTE:
James Smart23288b72018-05-04 20:37:53 -07005094 case LPFC_ASYNC_LINK_FAULT_LR_LRR:
James Smartda0436e2009-05-22 14:51:39 -04005095 break;
5096 default:
Dick Kennedy372c1872020-06-30 14:50:00 -07005097 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart23288b72018-05-04 20:37:53 -07005098 "0398 Unknown link fault code: x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04005099 bf_get(lpfc_acqe_link_fault, acqe_link));
James Smartda0436e2009-05-22 14:51:39 -04005100 break;
5101 }
James Smartda0436e2009-05-22 14:51:39 -04005102}
5103
5104/**
5105 * lpfc_sli4_parse_latt_type - Parse sli4 link attention type
5106 * @phba: pointer to lpfc hba data structure.
5107 * @acqe_link: pointer to the async link completion queue entry.
5108 *
5109 * This routine is to parse the SLI4 link attention type and translate it
5110 * into the base driver's link attention type coding.
5111 *
5112 * Return: Link attention type in terms of base driver's coding.
5113 **/
5114static uint8_t
5115lpfc_sli4_parse_latt_type(struct lpfc_hba *phba,
5116 struct lpfc_acqe_link *acqe_link)
5117{
5118 uint8_t att_type;
5119
5120 switch (bf_get(lpfc_acqe_link_status, acqe_link)) {
5121 case LPFC_ASYNC_LINK_STATUS_DOWN:
5122 case LPFC_ASYNC_LINK_STATUS_LOGICAL_DOWN:
James Smart76a95d72010-11-20 23:11:48 -05005123 att_type = LPFC_ATT_LINK_DOWN;
James Smartda0436e2009-05-22 14:51:39 -04005124 break;
5125 case LPFC_ASYNC_LINK_STATUS_UP:
5126 /* Ignore physical link up events - wait for logical link up */
James Smart76a95d72010-11-20 23:11:48 -05005127 att_type = LPFC_ATT_RESERVED;
James Smartda0436e2009-05-22 14:51:39 -04005128 break;
5129 case LPFC_ASYNC_LINK_STATUS_LOGICAL_UP:
James Smart76a95d72010-11-20 23:11:48 -05005130 att_type = LPFC_ATT_LINK_UP;
James Smartda0436e2009-05-22 14:51:39 -04005131 break;
5132 default:
Dick Kennedy372c1872020-06-30 14:50:00 -07005133 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartda0436e2009-05-22 14:51:39 -04005134 "0399 Invalid link attention type: x%x\n",
5135 bf_get(lpfc_acqe_link_status, acqe_link));
James Smart76a95d72010-11-20 23:11:48 -05005136 att_type = LPFC_ATT_RESERVED;
James Smartda0436e2009-05-22 14:51:39 -04005137 break;
5138 }
5139 return att_type;
5140}
5141
5142/**
James Smart8b68cd52012-09-29 11:32:37 -04005143 * lpfc_sli_port_speed_get - Get sli3 link speed code to link speed
5144 * @phba: pointer to lpfc hba data structure.
5145 *
5146 * This routine is to get an SLI3 FC port's link speed in Mbps.
5147 *
5148 * Return: link speed in terms of Mbps.
5149 **/
5150uint32_t
5151lpfc_sli_port_speed_get(struct lpfc_hba *phba)
5152{
5153 uint32_t link_speed;
5154
5155 if (!lpfc_is_link_up(phba))
5156 return 0;
5157
James Smarta085e872015-12-16 18:12:02 -05005158 if (phba->sli_rev <= LPFC_SLI_REV3) {
5159 switch (phba->fc_linkspeed) {
5160 case LPFC_LINK_SPEED_1GHZ:
5161 link_speed = 1000;
5162 break;
5163 case LPFC_LINK_SPEED_2GHZ:
5164 link_speed = 2000;
5165 break;
5166 case LPFC_LINK_SPEED_4GHZ:
5167 link_speed = 4000;
5168 break;
5169 case LPFC_LINK_SPEED_8GHZ:
5170 link_speed = 8000;
5171 break;
5172 case LPFC_LINK_SPEED_10GHZ:
5173 link_speed = 10000;
5174 break;
5175 case LPFC_LINK_SPEED_16GHZ:
5176 link_speed = 16000;
5177 break;
5178 default:
5179 link_speed = 0;
5180 }
5181 } else {
5182 if (phba->sli4_hba.link_state.logical_speed)
5183 link_speed =
5184 phba->sli4_hba.link_state.logical_speed;
5185 else
5186 link_speed = phba->sli4_hba.link_state.speed;
James Smart8b68cd52012-09-29 11:32:37 -04005187 }
5188 return link_speed;
5189}
5190
5191/**
5192 * lpfc_sli4_port_speed_parse - Parse async evt link speed code to link speed
5193 * @phba: pointer to lpfc hba data structure.
5194 * @evt_code: asynchronous event code.
5195 * @speed_code: asynchronous event link speed code.
5196 *
5197 * This routine is to parse the giving SLI4 async event link speed code into
5198 * value of Mbps for the link speed.
5199 *
5200 * Return: link speed in terms of Mbps.
5201 **/
5202static uint32_t
5203lpfc_sli4_port_speed_parse(struct lpfc_hba *phba, uint32_t evt_code,
5204 uint8_t speed_code)
5205{
5206 uint32_t port_speed;
5207
5208 switch (evt_code) {
5209 case LPFC_TRAILER_CODE_LINK:
5210 switch (speed_code) {
James Smart26d830e2015-04-07 15:07:17 -04005211 case LPFC_ASYNC_LINK_SPEED_ZERO:
James Smart8b68cd52012-09-29 11:32:37 -04005212 port_speed = 0;
5213 break;
James Smart26d830e2015-04-07 15:07:17 -04005214 case LPFC_ASYNC_LINK_SPEED_10MBPS:
James Smart8b68cd52012-09-29 11:32:37 -04005215 port_speed = 10;
5216 break;
James Smart26d830e2015-04-07 15:07:17 -04005217 case LPFC_ASYNC_LINK_SPEED_100MBPS:
James Smart8b68cd52012-09-29 11:32:37 -04005218 port_speed = 100;
5219 break;
James Smart26d830e2015-04-07 15:07:17 -04005220 case LPFC_ASYNC_LINK_SPEED_1GBPS:
James Smart8b68cd52012-09-29 11:32:37 -04005221 port_speed = 1000;
5222 break;
James Smart26d830e2015-04-07 15:07:17 -04005223 case LPFC_ASYNC_LINK_SPEED_10GBPS:
James Smart8b68cd52012-09-29 11:32:37 -04005224 port_speed = 10000;
5225 break;
James Smart26d830e2015-04-07 15:07:17 -04005226 case LPFC_ASYNC_LINK_SPEED_20GBPS:
5227 port_speed = 20000;
5228 break;
5229 case LPFC_ASYNC_LINK_SPEED_25GBPS:
5230 port_speed = 25000;
5231 break;
5232 case LPFC_ASYNC_LINK_SPEED_40GBPS:
5233 port_speed = 40000;
5234 break;
Dick Kennedya1e4d3d2020-08-03 14:02:22 -07005235 case LPFC_ASYNC_LINK_SPEED_100GBPS:
5236 port_speed = 100000;
5237 break;
James Smart8b68cd52012-09-29 11:32:37 -04005238 default:
5239 port_speed = 0;
5240 }
5241 break;
5242 case LPFC_TRAILER_CODE_FC:
5243 switch (speed_code) {
James Smart26d830e2015-04-07 15:07:17 -04005244 case LPFC_FC_LA_SPEED_UNKNOWN:
James Smart8b68cd52012-09-29 11:32:37 -04005245 port_speed = 0;
5246 break;
James Smart26d830e2015-04-07 15:07:17 -04005247 case LPFC_FC_LA_SPEED_1G:
James Smart8b68cd52012-09-29 11:32:37 -04005248 port_speed = 1000;
5249 break;
James Smart26d830e2015-04-07 15:07:17 -04005250 case LPFC_FC_LA_SPEED_2G:
James Smart8b68cd52012-09-29 11:32:37 -04005251 port_speed = 2000;
5252 break;
James Smart26d830e2015-04-07 15:07:17 -04005253 case LPFC_FC_LA_SPEED_4G:
James Smart8b68cd52012-09-29 11:32:37 -04005254 port_speed = 4000;
5255 break;
James Smart26d830e2015-04-07 15:07:17 -04005256 case LPFC_FC_LA_SPEED_8G:
James Smart8b68cd52012-09-29 11:32:37 -04005257 port_speed = 8000;
5258 break;
James Smart26d830e2015-04-07 15:07:17 -04005259 case LPFC_FC_LA_SPEED_10G:
James Smart8b68cd52012-09-29 11:32:37 -04005260 port_speed = 10000;
5261 break;
James Smart26d830e2015-04-07 15:07:17 -04005262 case LPFC_FC_LA_SPEED_16G:
James Smart8b68cd52012-09-29 11:32:37 -04005263 port_speed = 16000;
5264 break;
James Smartd38dd522015-08-31 16:48:17 -04005265 case LPFC_FC_LA_SPEED_32G:
5266 port_speed = 32000;
5267 break;
James Smartfbd8a6b2018-02-22 08:18:45 -08005268 case LPFC_FC_LA_SPEED_64G:
5269 port_speed = 64000;
5270 break;
James Smart1dc5ec22018-10-23 13:41:11 -07005271 case LPFC_FC_LA_SPEED_128G:
5272 port_speed = 128000;
5273 break;
James Smartbfc47782021-07-22 15:17:19 -07005274 case LPFC_FC_LA_SPEED_256G:
5275 port_speed = 256000;
5276 break;
James Smart8b68cd52012-09-29 11:32:37 -04005277 default:
5278 port_speed = 0;
5279 }
5280 break;
5281 default:
5282 port_speed = 0;
5283 }
5284 return port_speed;
5285}
5286
5287/**
James Smart70f3c072010-12-15 17:57:33 -05005288 * lpfc_sli4_async_link_evt - Process the asynchronous FCoE link event
James Smartda0436e2009-05-22 14:51:39 -04005289 * @phba: pointer to lpfc hba data structure.
5290 * @acqe_link: pointer to the async link completion queue entry.
5291 *
James Smart70f3c072010-12-15 17:57:33 -05005292 * This routine is to handle the SLI4 asynchronous FCoE link event.
James Smartda0436e2009-05-22 14:51:39 -04005293 **/
5294static void
5295lpfc_sli4_async_link_evt(struct lpfc_hba *phba,
5296 struct lpfc_acqe_link *acqe_link)
5297{
5298 struct lpfc_dmabuf *mp;
5299 LPFC_MBOXQ_t *pmb;
5300 MAILBOX_t *mb;
James Smart76a95d72010-11-20 23:11:48 -05005301 struct lpfc_mbx_read_top *la;
James Smartda0436e2009-05-22 14:51:39 -04005302 uint8_t att_type;
James Smart76a95d72010-11-20 23:11:48 -05005303 int rc;
James Smartda0436e2009-05-22 14:51:39 -04005304
5305 att_type = lpfc_sli4_parse_latt_type(phba, acqe_link);
James Smart76a95d72010-11-20 23:11:48 -05005306 if (att_type != LPFC_ATT_LINK_DOWN && att_type != LPFC_ATT_LINK_UP)
James Smartda0436e2009-05-22 14:51:39 -04005307 return;
James Smart32b97932009-07-19 10:01:21 -04005308 phba->fcoe_eventtag = acqe_link->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04005309 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5310 if (!pmb) {
Dick Kennedy372c1872020-06-30 14:50:00 -07005311 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartda0436e2009-05-22 14:51:39 -04005312 "0395 The mboxq allocation failed\n");
5313 return;
5314 }
5315 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5316 if (!mp) {
Dick Kennedy372c1872020-06-30 14:50:00 -07005317 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartda0436e2009-05-22 14:51:39 -04005318 "0396 The lpfc_dmabuf allocation failed\n");
5319 goto out_free_pmb;
5320 }
5321 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
5322 if (!mp->virt) {
Dick Kennedy372c1872020-06-30 14:50:00 -07005323 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartda0436e2009-05-22 14:51:39 -04005324 "0397 The mbuf allocation failed\n");
5325 goto out_free_dmabuf;
5326 }
5327
5328 /* Cleanup any outstanding ELS commands */
5329 lpfc_els_flush_all_cmd(phba);
5330
5331 /* Block ELS IOCBs until we have done process link event */
James Smart895427b2017-02-12 13:52:30 -08005332 phba->sli4_hba.els_wq->pring->flag |= LPFC_STOP_IOCB_EVENT;
James Smartda0436e2009-05-22 14:51:39 -04005333
5334 /* Update link event statistics */
5335 phba->sli.slistat.link_event++;
5336
James Smart76a95d72010-11-20 23:11:48 -05005337 /* Create lpfc_handle_latt mailbox command from link ACQE */
5338 lpfc_read_topology(phba, pmb, mp);
5339 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
James Smartda0436e2009-05-22 14:51:39 -04005340 pmb->vport = phba->pport;
5341
James Smartda0436e2009-05-22 14:51:39 -04005342 /* Keep the link status for extra SLI4 state machine reference */
5343 phba->sli4_hba.link_state.speed =
James Smart8b68cd52012-09-29 11:32:37 -04005344 lpfc_sli4_port_speed_parse(phba, LPFC_TRAILER_CODE_LINK,
5345 bf_get(lpfc_acqe_link_speed, acqe_link));
James Smartda0436e2009-05-22 14:51:39 -04005346 phba->sli4_hba.link_state.duplex =
5347 bf_get(lpfc_acqe_link_duplex, acqe_link);
5348 phba->sli4_hba.link_state.status =
5349 bf_get(lpfc_acqe_link_status, acqe_link);
James Smart70f3c072010-12-15 17:57:33 -05005350 phba->sli4_hba.link_state.type =
5351 bf_get(lpfc_acqe_link_type, acqe_link);
5352 phba->sli4_hba.link_state.number =
5353 bf_get(lpfc_acqe_link_number, acqe_link);
James Smartda0436e2009-05-22 14:51:39 -04005354 phba->sli4_hba.link_state.fault =
5355 bf_get(lpfc_acqe_link_fault, acqe_link);
James Smart65467b62010-01-26 23:08:29 -05005356 phba->sli4_hba.link_state.logical_speed =
James Smart8b68cd52012-09-29 11:32:37 -04005357 bf_get(lpfc_acqe_logical_link_speed, acqe_link) * 10;
5358
James Smart70f3c072010-12-15 17:57:33 -05005359 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartc31098c2011-04-16 11:03:33 -04005360 "2900 Async FC/FCoE Link event - Speed:%dGBit "
5361 "duplex:x%x LA Type:x%x Port Type:%d Port Number:%d "
5362 "Logical speed:%dMbps Fault:%d\n",
James Smart70f3c072010-12-15 17:57:33 -05005363 phba->sli4_hba.link_state.speed,
5364 phba->sli4_hba.link_state.topology,
5365 phba->sli4_hba.link_state.status,
5366 phba->sli4_hba.link_state.type,
5367 phba->sli4_hba.link_state.number,
James Smart8b68cd52012-09-29 11:32:37 -04005368 phba->sli4_hba.link_state.logical_speed,
James Smart70f3c072010-12-15 17:57:33 -05005369 phba->sli4_hba.link_state.fault);
James Smart76a95d72010-11-20 23:11:48 -05005370 /*
5371 * For FC Mode: issue the READ_TOPOLOGY mailbox command to fetch
5372 * topology info. Note: Optional for non FC-AL ports.
5373 */
5374 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
5375 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
James Smartf0d39192021-12-03 16:26:36 -08005376 if (rc == MBX_NOT_FINISHED) {
5377 lpfc_mbuf_free(phba, mp->virt, mp->phys);
James Smart76a95d72010-11-20 23:11:48 -05005378 goto out_free_dmabuf;
James Smartf0d39192021-12-03 16:26:36 -08005379 }
James Smart76a95d72010-11-20 23:11:48 -05005380 return;
5381 }
5382 /*
5383 * For FCoE Mode: fill in all the topology information we need and call
5384 * the READ_TOPOLOGY completion routine to continue without actually
5385 * sending the READ_TOPOLOGY mailbox command to the port.
5386 */
James Smart23288b72018-05-04 20:37:53 -07005387 /* Initialize completion status */
James Smart76a95d72010-11-20 23:11:48 -05005388 mb = &pmb->u.mb;
James Smart23288b72018-05-04 20:37:53 -07005389 mb->mbxStatus = MBX_SUCCESS;
5390
5391 /* Parse port fault information field */
5392 lpfc_sli4_parse_latt_fault(phba, acqe_link);
James Smart76a95d72010-11-20 23:11:48 -05005393
5394 /* Parse and translate link attention fields */
5395 la = (struct lpfc_mbx_read_top *) &pmb->u.mb.un.varReadTop;
5396 la->eventTag = acqe_link->event_tag;
5397 bf_set(lpfc_mbx_read_top_att_type, la, att_type);
5398 bf_set(lpfc_mbx_read_top_link_spd, la,
James Smarta085e872015-12-16 18:12:02 -05005399 (bf_get(lpfc_acqe_link_speed, acqe_link)));
James Smart76a95d72010-11-20 23:11:48 -05005400
5401 /* Fake the the following irrelvant fields */
5402 bf_set(lpfc_mbx_read_top_topology, la, LPFC_TOPOLOGY_PT_PT);
5403 bf_set(lpfc_mbx_read_top_alpa_granted, la, 0);
5404 bf_set(lpfc_mbx_read_top_il, la, 0);
5405 bf_set(lpfc_mbx_read_top_pb, la, 0);
5406 bf_set(lpfc_mbx_read_top_fa, la, 0);
5407 bf_set(lpfc_mbx_read_top_mm, la, 0);
James Smartda0436e2009-05-22 14:51:39 -04005408
5409 /* Invoke the lpfc_handle_latt mailbox command callback function */
James Smart76a95d72010-11-20 23:11:48 -05005410 lpfc_mbx_cmpl_read_topology(phba, pmb);
James Smartda0436e2009-05-22 14:51:39 -04005411
5412 return;
5413
5414out_free_dmabuf:
5415 kfree(mp);
5416out_free_pmb:
5417 mempool_free(pmb, phba->mbox_mem_pool);
5418}
5419
5420/**
James Smart1dc5ec22018-10-23 13:41:11 -07005421 * lpfc_async_link_speed_to_read_top - Parse async evt link speed code to read
5422 * topology.
5423 * @phba: pointer to lpfc hba data structure.
James Smart1dc5ec22018-10-23 13:41:11 -07005424 * @speed_code: asynchronous event link speed code.
5425 *
5426 * This routine is to parse the giving SLI4 async event link speed code into
5427 * value of Read topology link speed.
5428 *
5429 * Return: link speed in terms of Read topology.
5430 **/
5431static uint8_t
5432lpfc_async_link_speed_to_read_top(struct lpfc_hba *phba, uint8_t speed_code)
5433{
5434 uint8_t port_speed;
5435
5436 switch (speed_code) {
5437 case LPFC_FC_LA_SPEED_1G:
5438 port_speed = LPFC_LINK_SPEED_1GHZ;
5439 break;
5440 case LPFC_FC_LA_SPEED_2G:
5441 port_speed = LPFC_LINK_SPEED_2GHZ;
5442 break;
5443 case LPFC_FC_LA_SPEED_4G:
5444 port_speed = LPFC_LINK_SPEED_4GHZ;
5445 break;
5446 case LPFC_FC_LA_SPEED_8G:
5447 port_speed = LPFC_LINK_SPEED_8GHZ;
5448 break;
5449 case LPFC_FC_LA_SPEED_16G:
5450 port_speed = LPFC_LINK_SPEED_16GHZ;
5451 break;
5452 case LPFC_FC_LA_SPEED_32G:
5453 port_speed = LPFC_LINK_SPEED_32GHZ;
5454 break;
5455 case LPFC_FC_LA_SPEED_64G:
5456 port_speed = LPFC_LINK_SPEED_64GHZ;
5457 break;
5458 case LPFC_FC_LA_SPEED_128G:
5459 port_speed = LPFC_LINK_SPEED_128GHZ;
5460 break;
5461 case LPFC_FC_LA_SPEED_256G:
5462 port_speed = LPFC_LINK_SPEED_256GHZ;
5463 break;
5464 default:
5465 port_speed = 0;
5466 break;
5467 }
5468
5469 return port_speed;
5470}
5471
James Smart74a7baa2021-08-16 09:28:58 -07005472void
5473lpfc_cgn_dump_rxmonitor(struct lpfc_hba *phba)
5474{
5475 struct rxtable_entry *entry;
5476 int cnt = 0, head, tail, last, start;
5477
5478 head = atomic_read(&phba->rxtable_idx_head);
5479 tail = atomic_read(&phba->rxtable_idx_tail);
5480 if (!phba->rxtable || head == tail) {
5481 lpfc_printf_log(phba, KERN_ERR, LOG_CGN_MGMT,
5482 "4411 Rxtable is empty\n");
5483 return;
5484 }
5485 last = tail;
5486 start = head;
5487
5488 /* Display the last LPFC_MAX_RXMONITOR_DUMP entries from the rxtable */
5489 while (start != last) {
5490 if (start)
5491 start--;
5492 else
5493 start = LPFC_MAX_RXMONITOR_ENTRY - 1;
5494 entry = &phba->rxtable[start];
5495 lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
5496 "4410 %02d: MBPI %lld Xmit %lld Cmpl %lld "
5497 "Lat %lld ASz %lld Info %02d BWUtil %d "
5498 "Int %d slot %d\n",
5499 cnt, entry->max_bytes_per_interval,
5500 entry->total_bytes, entry->rcv_bytes,
5501 entry->avg_io_latency, entry->avg_io_size,
5502 entry->cmf_info, entry->timer_utilization,
5503 entry->timer_interval, start);
5504 cnt++;
5505 if (cnt >= LPFC_MAX_RXMONITOR_DUMP)
5506 return;
5507 }
5508}
5509
James Smart02243832021-08-16 09:28:54 -07005510/**
James Smart7481811c2021-08-16 09:28:56 -07005511 * lpfc_cgn_update_stat - Save data into congestion stats buffer
5512 * @phba: pointer to lpfc hba data structure.
5513 * @dtag: FPIN descriptor received
5514 *
5515 * Increment the FPIN received counter/time when it happens.
5516 */
5517void
5518lpfc_cgn_update_stat(struct lpfc_hba *phba, uint32_t dtag)
5519{
5520 struct lpfc_cgn_info *cp;
5521 struct tm broken;
5522 struct timespec64 cur_time;
5523 u32 cnt;
5524 u16 value;
5525
5526 /* Make sure we have a congestion info buffer */
5527 if (!phba->cgn_i)
5528 return;
5529 cp = (struct lpfc_cgn_info *)phba->cgn_i->virt;
5530 ktime_get_real_ts64(&cur_time);
5531 time64_to_tm(cur_time.tv_sec, 0, &broken);
5532
5533 /* Update congestion statistics */
5534 switch (dtag) {
5535 case ELS_DTAG_LNK_INTEGRITY:
5536 cnt = le32_to_cpu(cp->link_integ_notification);
5537 cnt++;
5538 cp->link_integ_notification = cpu_to_le32(cnt);
5539
5540 cp->cgn_stat_lnk_month = broken.tm_mon + 1;
5541 cp->cgn_stat_lnk_day = broken.tm_mday;
5542 cp->cgn_stat_lnk_year = broken.tm_year - 100;
5543 cp->cgn_stat_lnk_hour = broken.tm_hour;
5544 cp->cgn_stat_lnk_min = broken.tm_min;
5545 cp->cgn_stat_lnk_sec = broken.tm_sec;
5546 break;
5547 case ELS_DTAG_DELIVERY:
5548 cnt = le32_to_cpu(cp->delivery_notification);
5549 cnt++;
5550 cp->delivery_notification = cpu_to_le32(cnt);
5551
5552 cp->cgn_stat_del_month = broken.tm_mon + 1;
5553 cp->cgn_stat_del_day = broken.tm_mday;
5554 cp->cgn_stat_del_year = broken.tm_year - 100;
5555 cp->cgn_stat_del_hour = broken.tm_hour;
5556 cp->cgn_stat_del_min = broken.tm_min;
5557 cp->cgn_stat_del_sec = broken.tm_sec;
5558 break;
5559 case ELS_DTAG_PEER_CONGEST:
5560 cnt = le32_to_cpu(cp->cgn_peer_notification);
5561 cnt++;
5562 cp->cgn_peer_notification = cpu_to_le32(cnt);
5563
5564 cp->cgn_stat_peer_month = broken.tm_mon + 1;
5565 cp->cgn_stat_peer_day = broken.tm_mday;
5566 cp->cgn_stat_peer_year = broken.tm_year - 100;
5567 cp->cgn_stat_peer_hour = broken.tm_hour;
5568 cp->cgn_stat_peer_min = broken.tm_min;
5569 cp->cgn_stat_peer_sec = broken.tm_sec;
5570 break;
5571 case ELS_DTAG_CONGESTION:
5572 cnt = le32_to_cpu(cp->cgn_notification);
5573 cnt++;
5574 cp->cgn_notification = cpu_to_le32(cnt);
5575
5576 cp->cgn_stat_cgn_month = broken.tm_mon + 1;
5577 cp->cgn_stat_cgn_day = broken.tm_mday;
5578 cp->cgn_stat_cgn_year = broken.tm_year - 100;
5579 cp->cgn_stat_cgn_hour = broken.tm_hour;
5580 cp->cgn_stat_cgn_min = broken.tm_min;
5581 cp->cgn_stat_cgn_sec = broken.tm_sec;
5582 }
5583 if (phba->cgn_fpin_frequency &&
5584 phba->cgn_fpin_frequency != LPFC_FPIN_INIT_FREQ) {
5585 value = LPFC_CGN_TIMER_TO_MIN / phba->cgn_fpin_frequency;
James Smart59936432021-08-30 16:12:43 -07005586 cp->cgn_stat_npm = value;
James Smart7481811c2021-08-16 09:28:56 -07005587 }
5588 value = lpfc_cgn_calc_crc32(cp, LPFC_CGN_INFO_SZ,
5589 LPFC_CGN_CRC32_SEED);
5590 cp->cgn_info_crc = cpu_to_le32(value);
5591}
5592
5593/**
5594 * lpfc_cgn_save_evt_cnt - Save data into registered congestion buffer
5595 * @phba: pointer to lpfc hba data structure.
5596 *
5597 * Save the congestion event data every minute.
5598 * On the hour collapse all the minute data into hour data. Every day
5599 * collapse all the hour data into daily data. Separate driver
5600 * and fabrc congestion event counters that will be saved out
5601 * to the registered congestion buffer every minute.
5602 */
5603static void
5604lpfc_cgn_save_evt_cnt(struct lpfc_hba *phba)
5605{
5606 struct lpfc_cgn_info *cp;
5607 struct tm broken;
5608 struct timespec64 cur_time;
5609 uint32_t i, index;
5610 uint16_t value, mvalue;
5611 uint64_t bps;
5612 uint32_t mbps;
5613 uint32_t dvalue, wvalue, lvalue, avalue;
5614 uint64_t latsum;
James Smart59936432021-08-30 16:12:43 -07005615 __le16 *ptr;
5616 __le32 *lptr;
5617 __le16 *mptr;
James Smart7481811c2021-08-16 09:28:56 -07005618
5619 /* Make sure we have a congestion info buffer */
5620 if (!phba->cgn_i)
5621 return;
5622 cp = (struct lpfc_cgn_info *)phba->cgn_i->virt;
5623
5624 if (time_before(jiffies, phba->cgn_evt_timestamp))
5625 return;
5626 phba->cgn_evt_timestamp = jiffies +
5627 msecs_to_jiffies(LPFC_CGN_TIMER_TO_MIN);
5628 phba->cgn_evt_minute++;
5629
5630 /* We should get to this point in the routine on 1 minute intervals */
5631
5632 ktime_get_real_ts64(&cur_time);
5633 time64_to_tm(cur_time.tv_sec, 0, &broken);
5634
5635 if (phba->cgn_fpin_frequency &&
5636 phba->cgn_fpin_frequency != LPFC_FPIN_INIT_FREQ) {
5637 value = LPFC_CGN_TIMER_TO_MIN / phba->cgn_fpin_frequency;
James Smart59936432021-08-30 16:12:43 -07005638 cp->cgn_stat_npm = value;
James Smart7481811c2021-08-16 09:28:56 -07005639 }
5640
5641 /* Read and clear the latency counters for this minute */
5642 lvalue = atomic_read(&phba->cgn_latency_evt_cnt);
5643 latsum = atomic64_read(&phba->cgn_latency_evt);
5644 atomic_set(&phba->cgn_latency_evt_cnt, 0);
5645 atomic64_set(&phba->cgn_latency_evt, 0);
5646
5647 /* We need to store MB/sec bandwidth in the congestion information.
5648 * block_cnt is count of 512 byte blocks for the entire minute,
5649 * bps will get bytes per sec before finally converting to MB/sec.
5650 */
5651 bps = div_u64(phba->rx_block_cnt, LPFC_SEC_MIN) * 512;
5652 phba->rx_block_cnt = 0;
5653 mvalue = bps / (1024 * 1024); /* convert to MB/sec */
5654
5655 /* Every minute */
5656 /* cgn parameters */
5657 cp->cgn_info_mode = phba->cgn_p.cgn_param_mode;
5658 cp->cgn_info_level0 = phba->cgn_p.cgn_param_level0;
5659 cp->cgn_info_level1 = phba->cgn_p.cgn_param_level1;
5660 cp->cgn_info_level2 = phba->cgn_p.cgn_param_level2;
5661
5662 /* Fill in default LUN qdepth */
5663 value = (uint16_t)(phba->pport->cfg_lun_queue_depth);
5664 cp->cgn_lunq = cpu_to_le16(value);
5665
5666 /* Record congestion buffer info - every minute
5667 * cgn_driver_evt_cnt (Driver events)
5668 * cgn_fabric_warn_cnt (Congestion Warnings)
5669 * cgn_latency_evt_cnt / cgn_latency_evt (IO Latency)
5670 * cgn_fabric_alarm_cnt (Congestion Alarms)
5671 */
5672 index = ++cp->cgn_index_minute;
5673 if (cp->cgn_index_minute == LPFC_MIN_HOUR) {
5674 cp->cgn_index_minute = 0;
5675 index = 0;
5676 }
5677
5678 /* Get the number of driver events in this sample and reset counter */
5679 dvalue = atomic_read(&phba->cgn_driver_evt_cnt);
5680 atomic_set(&phba->cgn_driver_evt_cnt, 0);
5681
5682 /* Get the number of warning events - FPIN and Signal for this minute */
5683 wvalue = 0;
5684 if ((phba->cgn_reg_fpin & LPFC_CGN_FPIN_WARN) ||
5685 phba->cgn_reg_signal == EDC_CG_SIG_WARN_ONLY ||
5686 phba->cgn_reg_signal == EDC_CG_SIG_WARN_ALARM)
5687 wvalue = atomic_read(&phba->cgn_fabric_warn_cnt);
5688 atomic_set(&phba->cgn_fabric_warn_cnt, 0);
5689
5690 /* Get the number of alarm events - FPIN and Signal for this minute */
5691 avalue = 0;
5692 if ((phba->cgn_reg_fpin & LPFC_CGN_FPIN_ALARM) ||
5693 phba->cgn_reg_signal == EDC_CG_SIG_WARN_ALARM)
5694 avalue = atomic_read(&phba->cgn_fabric_alarm_cnt);
5695 atomic_set(&phba->cgn_fabric_alarm_cnt, 0);
5696
5697 /* Collect the driver, warning, alarm and latency counts for this
5698 * minute into the driver congestion buffer.
5699 */
5700 ptr = &cp->cgn_drvr_min[index];
5701 value = (uint16_t)dvalue;
5702 *ptr = cpu_to_le16(value);
5703
5704 ptr = &cp->cgn_warn_min[index];
5705 value = (uint16_t)wvalue;
5706 *ptr = cpu_to_le16(value);
5707
5708 ptr = &cp->cgn_alarm_min[index];
5709 value = (uint16_t)avalue;
5710 *ptr = cpu_to_le16(value);
5711
5712 lptr = &cp->cgn_latency_min[index];
5713 if (lvalue) {
5714 lvalue = (uint32_t)div_u64(latsum, lvalue);
5715 *lptr = cpu_to_le32(lvalue);
5716 } else {
5717 *lptr = 0;
5718 }
5719
5720 /* Collect the bandwidth value into the driver's congesion buffer. */
5721 mptr = &cp->cgn_bw_min[index];
5722 *mptr = cpu_to_le16(mvalue);
5723
5724 lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
5725 "2418 Congestion Info - minute (%d): %d %d %d %d %d\n",
5726 index, dvalue, wvalue, *lptr, mvalue, avalue);
5727
5728 /* Every hour */
5729 if ((phba->cgn_evt_minute % LPFC_MIN_HOUR) == 0) {
5730 /* Record congestion buffer info - every hour
5731 * Collapse all minutes into an hour
5732 */
5733 index = ++cp->cgn_index_hour;
5734 if (cp->cgn_index_hour == LPFC_HOUR_DAY) {
5735 cp->cgn_index_hour = 0;
5736 index = 0;
5737 }
5738
5739 dvalue = 0;
5740 wvalue = 0;
5741 lvalue = 0;
5742 avalue = 0;
5743 mvalue = 0;
5744 mbps = 0;
5745 for (i = 0; i < LPFC_MIN_HOUR; i++) {
5746 dvalue += le16_to_cpu(cp->cgn_drvr_min[i]);
5747 wvalue += le16_to_cpu(cp->cgn_warn_min[i]);
5748 lvalue += le32_to_cpu(cp->cgn_latency_min[i]);
5749 mbps += le16_to_cpu(cp->cgn_bw_min[i]);
5750 avalue += le16_to_cpu(cp->cgn_alarm_min[i]);
5751 }
5752 if (lvalue) /* Avg of latency averages */
5753 lvalue /= LPFC_MIN_HOUR;
5754 if (mbps) /* Avg of Bandwidth averages */
5755 mvalue = mbps / LPFC_MIN_HOUR;
5756
5757 lptr = &cp->cgn_drvr_hr[index];
5758 *lptr = cpu_to_le32(dvalue);
5759 lptr = &cp->cgn_warn_hr[index];
5760 *lptr = cpu_to_le32(wvalue);
5761 lptr = &cp->cgn_latency_hr[index];
5762 *lptr = cpu_to_le32(lvalue);
5763 mptr = &cp->cgn_bw_hr[index];
5764 *mptr = cpu_to_le16(mvalue);
5765 lptr = &cp->cgn_alarm_hr[index];
5766 *lptr = cpu_to_le32(avalue);
5767
5768 lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
5769 "2419 Congestion Info - hour "
5770 "(%d): %d %d %d %d %d\n",
5771 index, dvalue, wvalue, lvalue, mvalue, avalue);
5772 }
5773
5774 /* Every day */
5775 if ((phba->cgn_evt_minute % LPFC_MIN_DAY) == 0) {
5776 /* Record congestion buffer info - every hour
5777 * Collapse all hours into a day. Rotate days
5778 * after LPFC_MAX_CGN_DAYS.
5779 */
5780 index = ++cp->cgn_index_day;
5781 if (cp->cgn_index_day == LPFC_MAX_CGN_DAYS) {
5782 cp->cgn_index_day = 0;
5783 index = 0;
5784 }
5785
5786 /* Anytime we overwrite daily index 0, after we wrap,
5787 * we will be overwriting the oldest day, so we must
5788 * update the congestion data start time for that day.
5789 * That start time should have previously been saved after
5790 * we wrote the last days worth of data.
5791 */
5792 if ((phba->hba_flag & HBA_CGN_DAY_WRAP) && index == 0) {
5793 time64_to_tm(phba->cgn_daily_ts.tv_sec, 0, &broken);
5794
5795 cp->cgn_info_month = broken.tm_mon + 1;
5796 cp->cgn_info_day = broken.tm_mday;
5797 cp->cgn_info_year = broken.tm_year - 100;
5798 cp->cgn_info_hour = broken.tm_hour;
5799 cp->cgn_info_minute = broken.tm_min;
5800 cp->cgn_info_second = broken.tm_sec;
5801
5802 lpfc_printf_log
5803 (phba, KERN_INFO, LOG_CGN_MGMT,
5804 "2646 CGNInfo idx0 Start Time: "
5805 "%d/%d/%d %d:%d:%d\n",
5806 cp->cgn_info_day, cp->cgn_info_month,
5807 cp->cgn_info_year, cp->cgn_info_hour,
5808 cp->cgn_info_minute, cp->cgn_info_second);
5809 }
5810
5811 dvalue = 0;
5812 wvalue = 0;
5813 lvalue = 0;
5814 mvalue = 0;
5815 mbps = 0;
5816 avalue = 0;
5817 for (i = 0; i < LPFC_HOUR_DAY; i++) {
5818 dvalue += le32_to_cpu(cp->cgn_drvr_hr[i]);
5819 wvalue += le32_to_cpu(cp->cgn_warn_hr[i]);
5820 lvalue += le32_to_cpu(cp->cgn_latency_hr[i]);
James Smart59936432021-08-30 16:12:43 -07005821 mbps += le16_to_cpu(cp->cgn_bw_hr[i]);
James Smart7481811c2021-08-16 09:28:56 -07005822 avalue += le32_to_cpu(cp->cgn_alarm_hr[i]);
5823 }
5824 if (lvalue) /* Avg of latency averages */
5825 lvalue /= LPFC_HOUR_DAY;
5826 if (mbps) /* Avg of Bandwidth averages */
5827 mvalue = mbps / LPFC_HOUR_DAY;
5828
5829 lptr = &cp->cgn_drvr_day[index];
5830 *lptr = cpu_to_le32(dvalue);
5831 lptr = &cp->cgn_warn_day[index];
5832 *lptr = cpu_to_le32(wvalue);
5833 lptr = &cp->cgn_latency_day[index];
5834 *lptr = cpu_to_le32(lvalue);
5835 mptr = &cp->cgn_bw_day[index];
5836 *mptr = cpu_to_le16(mvalue);
5837 lptr = &cp->cgn_alarm_day[index];
5838 *lptr = cpu_to_le32(avalue);
5839
5840 lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
5841 "2420 Congestion Info - daily (%d): "
5842 "%d %d %d %d %d\n",
5843 index, dvalue, wvalue, lvalue, mvalue, avalue);
5844
5845 /* We just wrote LPFC_MAX_CGN_DAYS of data,
5846 * so we are wrapped on any data after this.
5847 * Save this as the start time for the next day.
5848 */
5849 if (index == (LPFC_MAX_CGN_DAYS - 1)) {
5850 phba->hba_flag |= HBA_CGN_DAY_WRAP;
5851 ktime_get_real_ts64(&phba->cgn_daily_ts);
5852 }
5853 }
5854
5855 /* Use the frequency found in the last rcv'ed FPIN */
5856 value = phba->cgn_fpin_frequency;
5857 if (phba->cgn_reg_fpin & LPFC_CGN_FPIN_WARN)
5858 cp->cgn_warn_freq = cpu_to_le16(value);
5859 if (phba->cgn_reg_fpin & LPFC_CGN_FPIN_ALARM)
5860 cp->cgn_alarm_freq = cpu_to_le16(value);
5861
5862 /* Frequency (in ms) Signal Warning/Signal Congestion Notifications
5863 * are received by the HBA
5864 */
5865 value = phba->cgn_sig_freq;
5866
5867 if (phba->cgn_reg_signal == EDC_CG_SIG_WARN_ONLY ||
5868 phba->cgn_reg_signal == EDC_CG_SIG_WARN_ALARM)
5869 cp->cgn_warn_freq = cpu_to_le16(value);
5870 if (phba->cgn_reg_signal == EDC_CG_SIG_WARN_ALARM)
5871 cp->cgn_alarm_freq = cpu_to_le16(value);
5872
5873 lvalue = lpfc_cgn_calc_crc32(cp, LPFC_CGN_INFO_SZ,
5874 LPFC_CGN_CRC32_SEED);
5875 cp->cgn_info_crc = cpu_to_le32(lvalue);
5876}
5877
5878/**
James Smart02243832021-08-16 09:28:54 -07005879 * lpfc_calc_cmf_latency - latency from start of rxate timer interval
5880 * @phba: The Hba for which this call is being executed.
5881 *
5882 * The routine calculates the latency from the beginning of the CMF timer
5883 * interval to the current point in time. It is called from IO completion
5884 * when we exceed our Bandwidth limitation for the time interval.
5885 */
5886uint32_t
5887lpfc_calc_cmf_latency(struct lpfc_hba *phba)
5888{
5889 struct timespec64 cmpl_time;
5890 uint32_t msec = 0;
5891
5892 ktime_get_real_ts64(&cmpl_time);
5893
5894 /* This routine works on a ms granularity so sec and usec are
5895 * converted accordingly.
5896 */
5897 if (cmpl_time.tv_sec == phba->cmf_latency.tv_sec) {
5898 msec = (cmpl_time.tv_nsec - phba->cmf_latency.tv_nsec) /
5899 NSEC_PER_MSEC;
5900 } else {
5901 if (cmpl_time.tv_nsec >= phba->cmf_latency.tv_nsec) {
5902 msec = (cmpl_time.tv_sec -
5903 phba->cmf_latency.tv_sec) * MSEC_PER_SEC;
5904 msec += ((cmpl_time.tv_nsec -
5905 phba->cmf_latency.tv_nsec) / NSEC_PER_MSEC);
5906 } else {
5907 msec = (cmpl_time.tv_sec - phba->cmf_latency.tv_sec -
5908 1) * MSEC_PER_SEC;
5909 msec += (((NSEC_PER_SEC - phba->cmf_latency.tv_nsec) +
5910 cmpl_time.tv_nsec) / NSEC_PER_MSEC);
5911 }
5912 }
5913 return msec;
5914}
5915
5916/**
5917 * lpfc_cmf_timer - This is the timer function for one congestion
5918 * rate interval.
5919 * @timer: Pointer to the high resolution timer that expired
5920 */
5921static enum hrtimer_restart
5922lpfc_cmf_timer(struct hrtimer *timer)
5923{
5924 struct lpfc_hba *phba = container_of(timer, struct lpfc_hba,
5925 cmf_timer);
James Smart17b27ac2021-08-16 09:28:55 -07005926 struct rxtable_entry *entry;
James Smart02243832021-08-16 09:28:54 -07005927 uint32_t io_cnt;
James Smart17b27ac2021-08-16 09:28:55 -07005928 uint32_t head, tail;
5929 uint32_t busy, max_read;
James Smarta6269f82021-12-03 16:26:41 -08005930 uint64_t total, rcv, lat, mbpi, extra, cnt;
James Smart02243832021-08-16 09:28:54 -07005931 int timer_interval = LPFC_CMF_INTERVAL;
James Smart17b27ac2021-08-16 09:28:55 -07005932 uint32_t ms;
James Smart02243832021-08-16 09:28:54 -07005933 struct lpfc_cgn_stat *cgs;
5934 int cpu;
5935
5936 /* Only restart the timer if congestion mgmt is on */
5937 if (phba->cmf_active_mode == LPFC_CFG_OFF ||
5938 !phba->cmf_latency.tv_sec) {
5939 lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
5940 "6224 CMF timer exit: %d %lld\n",
5941 phba->cmf_active_mode,
5942 (uint64_t)phba->cmf_latency.tv_sec);
5943 return HRTIMER_NORESTART;
5944 }
5945
5946 /* If pport is not ready yet, just exit and wait for
5947 * the next timer cycle to hit.
5948 */
5949 if (!phba->pport)
5950 goto skip;
5951
5952 /* Do not block SCSI IO while in the timer routine since
5953 * total_bytes will be cleared
5954 */
5955 atomic_set(&phba->cmf_stop_io, 1);
5956
James Smart17b27ac2021-08-16 09:28:55 -07005957 /* First we need to calculate the actual ms between
5958 * the last timer interrupt and this one. We ask for
5959 * LPFC_CMF_INTERVAL, however the actual time may
5960 * vary depending on system overhead.
5961 */
5962 ms = lpfc_calc_cmf_latency(phba);
5963
5964
James Smart02243832021-08-16 09:28:54 -07005965 /* Immediately after we calculate the time since the last
5966 * timer interrupt, set the start time for the next
5967 * interrupt
5968 */
5969 ktime_get_real_ts64(&phba->cmf_latency);
5970
5971 phba->cmf_link_byte_count =
5972 div_u64(phba->cmf_max_line_rate * LPFC_CMF_INTERVAL, 1000);
5973
5974 /* Collect all the stats from the prior timer interval */
5975 total = 0;
5976 io_cnt = 0;
5977 lat = 0;
5978 rcv = 0;
5979 for_each_present_cpu(cpu) {
5980 cgs = per_cpu_ptr(phba->cmf_stat, cpu);
5981 total += atomic64_xchg(&cgs->total_bytes, 0);
5982 io_cnt += atomic_xchg(&cgs->rx_io_cnt, 0);
5983 lat += atomic64_xchg(&cgs->rx_latency, 0);
5984 rcv += atomic64_xchg(&cgs->rcv_bytes, 0);
5985 }
5986
5987 /* Before we issue another CMF_SYNC_WQE, retrieve the BW
5988 * returned from the last CMF_SYNC_WQE issued, from
5989 * cmf_last_sync_bw. This will be the target BW for
5990 * this next timer interval.
5991 */
5992 if (phba->cmf_active_mode == LPFC_CFG_MANAGED &&
5993 phba->link_state != LPFC_LINK_DOWN &&
5994 phba->hba_flag & HBA_SETUP) {
5995 mbpi = phba->cmf_last_sync_bw;
5996 phba->cmf_last_sync_bw = 0;
James Smartd5ac69b2021-09-10 16:31:55 -07005997 extra = 0;
5998
5999 /* Calculate any extra bytes needed to account for the
6000 * timer accuracy. If we are less than LPFC_CMF_INTERVAL
James Smarta6269f82021-12-03 16:26:41 -08006001 * calculate the adjustment needed for total to reflect
6002 * a full LPFC_CMF_INTERVAL.
James Smartd5ac69b2021-09-10 16:31:55 -07006003 */
James Smarta6269f82021-12-03 16:26:41 -08006004 if (ms && ms < LPFC_CMF_INTERVAL) {
6005 cnt = div_u64(total, ms); /* bytes per ms */
6006 cnt *= LPFC_CMF_INTERVAL; /* what total should be */
6007 if (cnt > mbpi)
6008 cnt = mbpi;
6009 extra = cnt - total;
6010 }
James Smartd5ac69b2021-09-10 16:31:55 -07006011 lpfc_issue_cmf_sync_wqe(phba, LPFC_CMF_INTERVAL, total + extra);
James Smart02243832021-08-16 09:28:54 -07006012 } else {
6013 /* For Monitor mode or link down we want mbpi
6014 * to be the full link speed
6015 */
6016 mbpi = phba->cmf_link_byte_count;
James Smarta6269f82021-12-03 16:26:41 -08006017 extra = 0;
James Smart02243832021-08-16 09:28:54 -07006018 }
6019 phba->cmf_timer_cnt++;
6020
6021 if (io_cnt) {
6022 /* Update congestion info buffer latency in us */
6023 atomic_add(io_cnt, &phba->cgn_latency_evt_cnt);
6024 atomic64_add(lat, &phba->cgn_latency_evt);
6025 }
James Smart17b27ac2021-08-16 09:28:55 -07006026 busy = atomic_xchg(&phba->cmf_busy, 0);
6027 max_read = atomic_xchg(&phba->rx_max_read_cnt, 0);
James Smart02243832021-08-16 09:28:54 -07006028
6029 /* Calculate MBPI for the next timer interval */
6030 if (mbpi) {
6031 if (mbpi > phba->cmf_link_byte_count ||
6032 phba->cmf_active_mode == LPFC_CFG_MONITOR)
6033 mbpi = phba->cmf_link_byte_count;
6034
6035 /* Change max_bytes_per_interval to what the prior
6036 * CMF_SYNC_WQE cmpl indicated.
6037 */
6038 if (mbpi != phba->cmf_max_bytes_per_interval)
6039 phba->cmf_max_bytes_per_interval = mbpi;
6040 }
6041
James Smart17b27ac2021-08-16 09:28:55 -07006042 /* Save rxmonitor information for debug */
6043 if (phba->rxtable) {
6044 head = atomic_xchg(&phba->rxtable_idx_head,
6045 LPFC_RXMONITOR_TABLE_IN_USE);
6046 entry = &phba->rxtable[head];
6047 entry->total_bytes = total;
James Smarta6269f82021-12-03 16:26:41 -08006048 entry->cmf_bytes = total + extra;
James Smart17b27ac2021-08-16 09:28:55 -07006049 entry->rcv_bytes = rcv;
6050 entry->cmf_busy = busy;
6051 entry->cmf_info = phba->cmf_active_info;
6052 if (io_cnt) {
6053 entry->avg_io_latency = div_u64(lat, io_cnt);
6054 entry->avg_io_size = div_u64(rcv, io_cnt);
6055 } else {
6056 entry->avg_io_latency = 0;
6057 entry->avg_io_size = 0;
6058 }
6059 entry->max_read_cnt = max_read;
6060 entry->io_cnt = io_cnt;
6061 entry->max_bytes_per_interval = mbpi;
6062 if (phba->cmf_active_mode == LPFC_CFG_MANAGED)
6063 entry->timer_utilization = phba->cmf_last_ts;
6064 else
6065 entry->timer_utilization = ms;
6066 entry->timer_interval = ms;
6067 phba->cmf_last_ts = 0;
6068
6069 /* Increment rxtable index */
6070 head = (head + 1) % LPFC_MAX_RXMONITOR_ENTRY;
6071 tail = atomic_read(&phba->rxtable_idx_tail);
6072 if (head == tail) {
6073 tail = (tail + 1) % LPFC_MAX_RXMONITOR_ENTRY;
6074 atomic_set(&phba->rxtable_idx_tail, tail);
6075 }
6076 atomic_set(&phba->rxtable_idx_head, head);
6077 }
6078
James Smart02243832021-08-16 09:28:54 -07006079 if (phba->cmf_active_mode == LPFC_CFG_MONITOR) {
6080 /* If Monitor mode, check if we are oversubscribed
6081 * against the full line rate.
6082 */
6083 if (mbpi && total > mbpi)
6084 atomic_inc(&phba->cgn_driver_evt_cnt);
6085 }
6086 phba->rx_block_cnt += div_u64(rcv, 512); /* save 512 byte block cnt */
6087
James Smart7481811c2021-08-16 09:28:56 -07006088 /* Each minute save Fabric and Driver congestion information */
6089 lpfc_cgn_save_evt_cnt(phba);
6090
6091 /* Since we need to call lpfc_cgn_save_evt_cnt every minute, on the
6092 * minute, adjust our next timer interval, if needed, to ensure a
6093 * 1 minute granularity when we get the next timer interrupt.
6094 */
6095 if (time_after(jiffies + msecs_to_jiffies(LPFC_CMF_INTERVAL),
6096 phba->cgn_evt_timestamp)) {
6097 timer_interval = jiffies_to_msecs(phba->cgn_evt_timestamp -
6098 jiffies);
6099 if (timer_interval <= 0)
6100 timer_interval = LPFC_CMF_INTERVAL;
6101
6102 /* If we adjust timer_interval, max_bytes_per_interval
6103 * needs to be adjusted as well.
6104 */
6105 phba->cmf_link_byte_count = div_u64(phba->cmf_max_line_rate *
6106 timer_interval, 1000);
6107 if (phba->cmf_active_mode == LPFC_CFG_MONITOR)
6108 phba->cmf_max_bytes_per_interval =
6109 phba->cmf_link_byte_count;
6110 }
6111
James Smart02243832021-08-16 09:28:54 -07006112 /* Since total_bytes has already been zero'ed, its okay to unblock
6113 * after max_bytes_per_interval is setup.
6114 */
6115 if (atomic_xchg(&phba->cmf_bw_wait, 0))
6116 queue_work(phba->wq, &phba->unblock_request_work);
6117
6118 /* SCSI IO is now unblocked */
6119 atomic_set(&phba->cmf_stop_io, 0);
6120
6121skip:
6122 hrtimer_forward_now(timer,
6123 ktime_set(0, timer_interval * NSEC_PER_MSEC));
6124 return HRTIMER_RESTART;
6125}
6126
James Smart1dc5ec22018-10-23 13:41:11 -07006127#define trunk_link_status(__idx)\
6128 bf_get(lpfc_acqe_fc_la_trunk_config_port##__idx, acqe_fc) ?\
6129 ((phba->trunk_link.link##__idx.state == LPFC_LINK_UP) ?\
6130 "Link up" : "Link down") : "NA"
6131/* Did port __idx reported an error */
6132#define trunk_port_fault(__idx)\
6133 bf_get(lpfc_acqe_fc_la_trunk_config_port##__idx, acqe_fc) ?\
6134 (port_fault & (1 << __idx) ? "YES" : "NO") : "NA"
6135
6136static void
6137lpfc_update_trunk_link_status(struct lpfc_hba *phba,
6138 struct lpfc_acqe_fc_la *acqe_fc)
6139{
6140 uint8_t port_fault = bf_get(lpfc_acqe_fc_la_trunk_linkmask, acqe_fc);
6141 uint8_t err = bf_get(lpfc_acqe_fc_la_trunk_fault, acqe_fc);
6142
6143 phba->sli4_hba.link_state.speed =
6144 lpfc_sli4_port_speed_parse(phba, LPFC_TRAILER_CODE_FC,
6145 bf_get(lpfc_acqe_fc_la_speed, acqe_fc));
6146
6147 phba->sli4_hba.link_state.logical_speed =
James Smartb8e6f132019-05-21 17:49:04 -07006148 bf_get(lpfc_acqe_fc_la_llink_spd, acqe_fc) * 10;
James Smart1dc5ec22018-10-23 13:41:11 -07006149 /* We got FC link speed, convert to fc_linkspeed (READ_TOPOLOGY) */
6150 phba->fc_linkspeed =
6151 lpfc_async_link_speed_to_read_top(
6152 phba,
6153 bf_get(lpfc_acqe_fc_la_speed, acqe_fc));
6154
6155 if (bf_get(lpfc_acqe_fc_la_trunk_config_port0, acqe_fc)) {
6156 phba->trunk_link.link0.state =
6157 bf_get(lpfc_acqe_fc_la_trunk_link_status_port0, acqe_fc)
6158 ? LPFC_LINK_UP : LPFC_LINK_DOWN;
James Smart529b3dd2018-12-13 15:17:54 -08006159 phba->trunk_link.link0.fault = port_fault & 0x1 ? err : 0;
James Smart1dc5ec22018-10-23 13:41:11 -07006160 }
6161 if (bf_get(lpfc_acqe_fc_la_trunk_config_port1, acqe_fc)) {
6162 phba->trunk_link.link1.state =
6163 bf_get(lpfc_acqe_fc_la_trunk_link_status_port1, acqe_fc)
6164 ? LPFC_LINK_UP : LPFC_LINK_DOWN;
James Smart529b3dd2018-12-13 15:17:54 -08006165 phba->trunk_link.link1.fault = port_fault & 0x2 ? err : 0;
James Smart1dc5ec22018-10-23 13:41:11 -07006166 }
6167 if (bf_get(lpfc_acqe_fc_la_trunk_config_port2, acqe_fc)) {
6168 phba->trunk_link.link2.state =
6169 bf_get(lpfc_acqe_fc_la_trunk_link_status_port2, acqe_fc)
6170 ? LPFC_LINK_UP : LPFC_LINK_DOWN;
James Smart529b3dd2018-12-13 15:17:54 -08006171 phba->trunk_link.link2.fault = port_fault & 0x4 ? err : 0;
James Smart1dc5ec22018-10-23 13:41:11 -07006172 }
6173 if (bf_get(lpfc_acqe_fc_la_trunk_config_port3, acqe_fc)) {
6174 phba->trunk_link.link3.state =
6175 bf_get(lpfc_acqe_fc_la_trunk_link_status_port3, acqe_fc)
6176 ? LPFC_LINK_UP : LPFC_LINK_DOWN;
James Smart529b3dd2018-12-13 15:17:54 -08006177 phba->trunk_link.link3.fault = port_fault & 0x8 ? err : 0;
James Smart1dc5ec22018-10-23 13:41:11 -07006178 }
6179
Dick Kennedy372c1872020-06-30 14:50:00 -07006180 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart1dc5ec22018-10-23 13:41:11 -07006181 "2910 Async FC Trunking Event - Speed:%d\n"
6182 "\tLogical speed:%d "
6183 "port0: %s port1: %s port2: %s port3: %s\n",
6184 phba->sli4_hba.link_state.speed,
6185 phba->sli4_hba.link_state.logical_speed,
6186 trunk_link_status(0), trunk_link_status(1),
6187 trunk_link_status(2), trunk_link_status(3));
6188
James Smart02243832021-08-16 09:28:54 -07006189 if (phba->cmf_active_mode != LPFC_CFG_OFF)
6190 lpfc_cmf_signal_init(phba);
6191
James Smart1dc5ec22018-10-23 13:41:11 -07006192 if (port_fault)
Dick Kennedy372c1872020-06-30 14:50:00 -07006193 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart1dc5ec22018-10-23 13:41:11 -07006194 "3202 trunk error:0x%x (%s) seen on port0:%s "
6195 /*
6196 * SLI-4: We have only 0xA error codes
6197 * defined as of now. print an appropriate
6198 * message in case driver needs to be updated.
6199 */
6200 "port1:%s port2:%s port3:%s\n", err, err > 0xA ?
6201 "UNDEFINED. update driver." : trunk_errmsg[err],
6202 trunk_port_fault(0), trunk_port_fault(1),
6203 trunk_port_fault(2), trunk_port_fault(3));
6204}
6205
6206
6207/**
James Smart70f3c072010-12-15 17:57:33 -05006208 * lpfc_sli4_async_fc_evt - Process the asynchronous FC link event
6209 * @phba: pointer to lpfc hba data structure.
6210 * @acqe_fc: pointer to the async fc completion queue entry.
6211 *
6212 * This routine is to handle the SLI4 asynchronous FC event. It will simply log
6213 * that the event was received and then issue a read_topology mailbox command so
6214 * that the rest of the driver will treat it the same as SLI3.
6215 **/
6216static void
6217lpfc_sli4_async_fc_evt(struct lpfc_hba *phba, struct lpfc_acqe_fc_la *acqe_fc)
6218{
6219 struct lpfc_dmabuf *mp;
6220 LPFC_MBOXQ_t *pmb;
James Smart7bdedb32016-07-06 12:36:00 -07006221 MAILBOX_t *mb;
6222 struct lpfc_mbx_read_top *la;
James Smart70f3c072010-12-15 17:57:33 -05006223 int rc;
6224
6225 if (bf_get(lpfc_trailer_type, acqe_fc) !=
6226 LPFC_FC_LA_EVENT_TYPE_FC_LINK) {
Dick Kennedy372c1872020-06-30 14:50:00 -07006227 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart70f3c072010-12-15 17:57:33 -05006228 "2895 Non FC link Event detected.(%d)\n",
6229 bf_get(lpfc_trailer_type, acqe_fc));
6230 return;
6231 }
James Smart1dc5ec22018-10-23 13:41:11 -07006232
6233 if (bf_get(lpfc_acqe_fc_la_att_type, acqe_fc) ==
6234 LPFC_FC_LA_TYPE_TRUNKING_EVENT) {
6235 lpfc_update_trunk_link_status(phba, acqe_fc);
6236 return;
6237 }
6238
James Smart70f3c072010-12-15 17:57:33 -05006239 /* Keep the link status for extra SLI4 state machine reference */
6240 phba->sli4_hba.link_state.speed =
James Smart8b68cd52012-09-29 11:32:37 -04006241 lpfc_sli4_port_speed_parse(phba, LPFC_TRAILER_CODE_FC,
6242 bf_get(lpfc_acqe_fc_la_speed, acqe_fc));
James Smart70f3c072010-12-15 17:57:33 -05006243 phba->sli4_hba.link_state.duplex = LPFC_ASYNC_LINK_DUPLEX_FULL;
6244 phba->sli4_hba.link_state.topology =
6245 bf_get(lpfc_acqe_fc_la_topology, acqe_fc);
6246 phba->sli4_hba.link_state.status =
6247 bf_get(lpfc_acqe_fc_la_att_type, acqe_fc);
6248 phba->sli4_hba.link_state.type =
6249 bf_get(lpfc_acqe_fc_la_port_type, acqe_fc);
6250 phba->sli4_hba.link_state.number =
6251 bf_get(lpfc_acqe_fc_la_port_number, acqe_fc);
6252 phba->sli4_hba.link_state.fault =
6253 bf_get(lpfc_acqe_link_fault, acqe_fc);
James Smartb8e6f132019-05-21 17:49:04 -07006254
6255 if (bf_get(lpfc_acqe_fc_la_att_type, acqe_fc) ==
6256 LPFC_FC_LA_TYPE_LINK_DOWN)
6257 phba->sli4_hba.link_state.logical_speed = 0;
6258 else if (!phba->sli4_hba.conf_trunk)
6259 phba->sli4_hba.link_state.logical_speed =
James Smart8b68cd52012-09-29 11:32:37 -04006260 bf_get(lpfc_acqe_fc_la_llink_spd, acqe_fc) * 10;
James Smartb8e6f132019-05-21 17:49:04 -07006261
James Smart70f3c072010-12-15 17:57:33 -05006262 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
6263 "2896 Async FC event - Speed:%dGBaud Topology:x%x "
6264 "LA Type:x%x Port Type:%d Port Number:%d Logical speed:"
6265 "%dMbps Fault:%d\n",
6266 phba->sli4_hba.link_state.speed,
6267 phba->sli4_hba.link_state.topology,
6268 phba->sli4_hba.link_state.status,
6269 phba->sli4_hba.link_state.type,
6270 phba->sli4_hba.link_state.number,
James Smart8b68cd52012-09-29 11:32:37 -04006271 phba->sli4_hba.link_state.logical_speed,
James Smart70f3c072010-12-15 17:57:33 -05006272 phba->sli4_hba.link_state.fault);
6273 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6274 if (!pmb) {
Dick Kennedy372c1872020-06-30 14:50:00 -07006275 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart70f3c072010-12-15 17:57:33 -05006276 "2897 The mboxq allocation failed\n");
6277 return;
6278 }
6279 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
6280 if (!mp) {
Dick Kennedy372c1872020-06-30 14:50:00 -07006281 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart70f3c072010-12-15 17:57:33 -05006282 "2898 The lpfc_dmabuf allocation failed\n");
6283 goto out_free_pmb;
6284 }
6285 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
6286 if (!mp->virt) {
Dick Kennedy372c1872020-06-30 14:50:00 -07006287 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart70f3c072010-12-15 17:57:33 -05006288 "2899 The mbuf allocation failed\n");
6289 goto out_free_dmabuf;
6290 }
6291
6292 /* Cleanup any outstanding ELS commands */
6293 lpfc_els_flush_all_cmd(phba);
6294
6295 /* Block ELS IOCBs until we have done process link event */
James Smart895427b2017-02-12 13:52:30 -08006296 phba->sli4_hba.els_wq->pring->flag |= LPFC_STOP_IOCB_EVENT;
James Smart70f3c072010-12-15 17:57:33 -05006297
6298 /* Update link event statistics */
6299 phba->sli.slistat.link_event++;
6300
6301 /* Create lpfc_handle_latt mailbox command from link ACQE */
6302 lpfc_read_topology(phba, pmb, mp);
6303 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
6304 pmb->vport = phba->pport;
6305
James Smart7bdedb32016-07-06 12:36:00 -07006306 if (phba->sli4_hba.link_state.status != LPFC_FC_LA_TYPE_LINK_UP) {
James Smartae9e28f2017-05-15 15:20:51 -07006307 phba->link_flag &= ~(LS_MDS_LINK_DOWN | LS_MDS_LOOPBACK);
6308
6309 switch (phba->sli4_hba.link_state.status) {
6310 case LPFC_FC_LA_TYPE_MDS_LINK_DOWN:
6311 phba->link_flag |= LS_MDS_LINK_DOWN;
6312 break;
6313 case LPFC_FC_LA_TYPE_MDS_LOOPBACK:
6314 phba->link_flag |= LS_MDS_LOOPBACK;
6315 break;
6316 default:
6317 break;
6318 }
6319
James Smart23288b72018-05-04 20:37:53 -07006320 /* Initialize completion status */
James Smart7bdedb32016-07-06 12:36:00 -07006321 mb = &pmb->u.mb;
James Smart23288b72018-05-04 20:37:53 -07006322 mb->mbxStatus = MBX_SUCCESS;
6323
6324 /* Parse port fault information field */
6325 lpfc_sli4_parse_latt_fault(phba, (void *)acqe_fc);
James Smart7bdedb32016-07-06 12:36:00 -07006326
6327 /* Parse and translate link attention fields */
6328 la = (struct lpfc_mbx_read_top *)&pmb->u.mb.un.varReadTop;
6329 la->eventTag = acqe_fc->event_tag;
James Smart7bdedb32016-07-06 12:36:00 -07006330
James Smartaeb3c812017-04-21 16:05:02 -07006331 if (phba->sli4_hba.link_state.status ==
6332 LPFC_FC_LA_TYPE_UNEXP_WWPN) {
6333 bf_set(lpfc_mbx_read_top_att_type, la,
6334 LPFC_FC_LA_TYPE_UNEXP_WWPN);
6335 } else {
6336 bf_set(lpfc_mbx_read_top_att_type, la,
6337 LPFC_FC_LA_TYPE_LINK_DOWN);
6338 }
James Smart7bdedb32016-07-06 12:36:00 -07006339 /* Invoke the mailbox command callback function */
6340 lpfc_mbx_cmpl_read_topology(phba, pmb);
6341
6342 return;
6343 }
6344
James Smart70f3c072010-12-15 17:57:33 -05006345 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
James Smartf0d39192021-12-03 16:26:36 -08006346 if (rc == MBX_NOT_FINISHED) {
6347 lpfc_mbuf_free(phba, mp->virt, mp->phys);
James Smart70f3c072010-12-15 17:57:33 -05006348 goto out_free_dmabuf;
James Smartf0d39192021-12-03 16:26:36 -08006349 }
James Smart70f3c072010-12-15 17:57:33 -05006350 return;
6351
6352out_free_dmabuf:
6353 kfree(mp);
6354out_free_pmb:
6355 mempool_free(pmb, phba->mbox_mem_pool);
6356}
6357
6358/**
6359 * lpfc_sli4_async_sli_evt - Process the asynchronous SLI link event
6360 * @phba: pointer to lpfc hba data structure.
Lee Jonesfe614ac2020-07-23 13:24:22 +01006361 * @acqe_sli: pointer to the async SLI completion queue entry.
James Smart70f3c072010-12-15 17:57:33 -05006362 *
6363 * This routine is to handle the SLI4 asynchronous SLI events.
6364 **/
6365static void
6366lpfc_sli4_async_sli_evt(struct lpfc_hba *phba, struct lpfc_acqe_sli *acqe_sli)
6367{
James Smart4b8bae02012-06-12 13:55:07 -04006368 char port_name;
James Smart8c1312e2012-10-31 14:45:09 -04006369 char message[128];
James Smart4b8bae02012-06-12 13:55:07 -04006370 uint8_t status;
James Smart946727d2015-04-07 15:07:09 -04006371 uint8_t evt_type;
James Smart448193b2015-12-16 18:12:05 -05006372 uint8_t operational = 0;
James Smart946727d2015-04-07 15:07:09 -04006373 struct temp_event temp_event_data;
James Smart4b8bae02012-06-12 13:55:07 -04006374 struct lpfc_acqe_misconfigured_event *misconfigured;
James Smart9064aeb2021-08-16 09:28:50 -07006375 struct lpfc_acqe_cgn_signal *cgn_signal;
James Smart946727d2015-04-07 15:07:09 -04006376 struct Scsi_Host *shost;
James Smartcd713482018-10-23 13:41:01 -07006377 struct lpfc_vport **vports;
James Smart9064aeb2021-08-16 09:28:50 -07006378 int rc, i, cnt;
James Smart4b8bae02012-06-12 13:55:07 -04006379
James Smart946727d2015-04-07 15:07:09 -04006380 evt_type = bf_get(lpfc_trailer_type, acqe_sli);
6381
James Smart448193b2015-12-16 18:12:05 -05006382 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartd11ed162019-09-21 20:59:03 -07006383 "2901 Async SLI event - Type:%d, Event Data: x%08x "
6384 "x%08x x%08x x%08x\n", evt_type,
James Smart448193b2015-12-16 18:12:05 -05006385 acqe_sli->event_data1, acqe_sli->event_data2,
James Smartd11ed162019-09-21 20:59:03 -07006386 acqe_sli->reserved, acqe_sli->trailer);
James Smart4b8bae02012-06-12 13:55:07 -04006387
6388 port_name = phba->Port[0];
6389 if (port_name == 0x00)
6390 port_name = '?'; /* get port name is empty */
6391
James Smart946727d2015-04-07 15:07:09 -04006392 switch (evt_type) {
6393 case LPFC_SLI_EVENT_TYPE_OVER_TEMP:
6394 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
6395 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
6396 temp_event_data.data = (uint32_t)acqe_sli->event_data1;
6397
6398 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
6399 "3190 Over Temperature:%d Celsius- Port Name %c\n",
6400 acqe_sli->event_data1, port_name);
6401
James Smart310429e2016-07-06 12:35:54 -07006402 phba->sfp_warning |= LPFC_TRANSGRESSION_HIGH_TEMPERATURE;
James Smart946727d2015-04-07 15:07:09 -04006403 shost = lpfc_shost_from_vport(phba->pport);
6404 fc_host_post_vendor_event(shost, fc_get_event_number(),
6405 sizeof(temp_event_data),
6406 (char *)&temp_event_data,
6407 SCSI_NL_VID_TYPE_PCI
6408 | PCI_VENDOR_ID_EMULEX);
6409 break;
6410 case LPFC_SLI_EVENT_TYPE_NORM_TEMP:
6411 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
6412 temp_event_data.event_code = LPFC_NORMAL_TEMP;
6413 temp_event_data.data = (uint32_t)acqe_sli->event_data1;
6414
6415 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
6416 "3191 Normal Temperature:%d Celsius - Port Name %c\n",
6417 acqe_sli->event_data1, port_name);
6418
6419 shost = lpfc_shost_from_vport(phba->pport);
6420 fc_host_post_vendor_event(shost, fc_get_event_number(),
6421 sizeof(temp_event_data),
6422 (char *)&temp_event_data,
6423 SCSI_NL_VID_TYPE_PCI
6424 | PCI_VENDOR_ID_EMULEX);
6425 break;
6426 case LPFC_SLI_EVENT_TYPE_MISCONFIGURED:
6427 misconfigured = (struct lpfc_acqe_misconfigured_event *)
James Smart4b8bae02012-06-12 13:55:07 -04006428 &acqe_sli->event_data1;
6429
James Smart946727d2015-04-07 15:07:09 -04006430 /* fetch the status for this port */
6431 switch (phba->sli4_hba.lnk_info.lnk_no) {
6432 case LPFC_LINK_NUMBER_0:
James Smart448193b2015-12-16 18:12:05 -05006433 status = bf_get(lpfc_sli_misconfigured_port0_state,
6434 &misconfigured->theEvent);
6435 operational = bf_get(lpfc_sli_misconfigured_port0_op,
James Smart4b8bae02012-06-12 13:55:07 -04006436 &misconfigured->theEvent);
James Smart946727d2015-04-07 15:07:09 -04006437 break;
6438 case LPFC_LINK_NUMBER_1:
James Smart448193b2015-12-16 18:12:05 -05006439 status = bf_get(lpfc_sli_misconfigured_port1_state,
6440 &misconfigured->theEvent);
6441 operational = bf_get(lpfc_sli_misconfigured_port1_op,
James Smart4b8bae02012-06-12 13:55:07 -04006442 &misconfigured->theEvent);
James Smart946727d2015-04-07 15:07:09 -04006443 break;
6444 case LPFC_LINK_NUMBER_2:
James Smart448193b2015-12-16 18:12:05 -05006445 status = bf_get(lpfc_sli_misconfigured_port2_state,
6446 &misconfigured->theEvent);
6447 operational = bf_get(lpfc_sli_misconfigured_port2_op,
James Smart4b8bae02012-06-12 13:55:07 -04006448 &misconfigured->theEvent);
James Smart946727d2015-04-07 15:07:09 -04006449 break;
6450 case LPFC_LINK_NUMBER_3:
James Smart448193b2015-12-16 18:12:05 -05006451 status = bf_get(lpfc_sli_misconfigured_port3_state,
6452 &misconfigured->theEvent);
6453 operational = bf_get(lpfc_sli_misconfigured_port3_op,
James Smart4b8bae02012-06-12 13:55:07 -04006454 &misconfigured->theEvent);
James Smart946727d2015-04-07 15:07:09 -04006455 break;
6456 default:
Dick Kennedy372c1872020-06-30 14:50:00 -07006457 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart448193b2015-12-16 18:12:05 -05006458 "3296 "
6459 "LPFC_SLI_EVENT_TYPE_MISCONFIGURED "
6460 "event: Invalid link %d",
6461 phba->sli4_hba.lnk_info.lnk_no);
6462 return;
James Smart946727d2015-04-07 15:07:09 -04006463 }
James Smart4b8bae02012-06-12 13:55:07 -04006464
James Smart448193b2015-12-16 18:12:05 -05006465 /* Skip if optic state unchanged */
6466 if (phba->sli4_hba.lnk_info.optic_state == status)
6467 return;
6468
James Smart946727d2015-04-07 15:07:09 -04006469 switch (status) {
6470 case LPFC_SLI_EVENT_STATUS_VALID:
James Smart448193b2015-12-16 18:12:05 -05006471 sprintf(message, "Physical Link is functional");
6472 break;
James Smart946727d2015-04-07 15:07:09 -04006473 case LPFC_SLI_EVENT_STATUS_NOT_PRESENT:
6474 sprintf(message, "Optics faulted/incorrectly "
6475 "installed/not installed - Reseat optics, "
6476 "if issue not resolved, replace.");
6477 break;
6478 case LPFC_SLI_EVENT_STATUS_WRONG_TYPE:
6479 sprintf(message,
6480 "Optics of two types installed - Remove one "
6481 "optic or install matching pair of optics.");
6482 break;
6483 case LPFC_SLI_EVENT_STATUS_UNSUPPORTED:
6484 sprintf(message, "Incompatible optics - Replace with "
James Smart292098b2012-09-29 11:31:41 -04006485 "compatible optics for card to function.");
James Smart946727d2015-04-07 15:07:09 -04006486 break;
James Smart448193b2015-12-16 18:12:05 -05006487 case LPFC_SLI_EVENT_STATUS_UNQUALIFIED:
6488 sprintf(message, "Unqualified optics - Replace with "
6489 "Avago optics for Warranty and Technical "
6490 "Support - Link is%s operational",
James Smart2ea259e2017-02-12 13:52:27 -08006491 (operational) ? " not" : "");
James Smart448193b2015-12-16 18:12:05 -05006492 break;
6493 case LPFC_SLI_EVENT_STATUS_UNCERTIFIED:
6494 sprintf(message, "Uncertified optics - Replace with "
6495 "Avago-certified optics to enable link "
6496 "operation - Link is%s operational",
James Smart2ea259e2017-02-12 13:52:27 -08006497 (operational) ? " not" : "");
James Smart448193b2015-12-16 18:12:05 -05006498 break;
James Smart946727d2015-04-07 15:07:09 -04006499 default:
6500 /* firmware is reporting a status we don't know about */
6501 sprintf(message, "Unknown event status x%02x", status);
6502 break;
6503 }
James Smartcd713482018-10-23 13:41:01 -07006504
6505 /* Issue READ_CONFIG mbox command to refresh supported speeds */
6506 rc = lpfc_sli4_read_config(phba);
James Smart3952e912018-10-23 13:41:02 -07006507 if (rc) {
James Smartcd713482018-10-23 13:41:01 -07006508 phba->lmt = 0;
Dick Kennedy372c1872020-06-30 14:50:00 -07006509 lpfc_printf_log(phba, KERN_ERR,
6510 LOG_TRACE_EVENT,
James Smartcd713482018-10-23 13:41:01 -07006511 "3194 Unable to retrieve supported "
James Smart3952e912018-10-23 13:41:02 -07006512 "speeds, rc = 0x%x\n", rc);
James Smartcd713482018-10-23 13:41:01 -07006513 }
James Smart7a1dda92021-10-20 14:14:12 -07006514 rc = lpfc_sli4_refresh_params(phba);
6515 if (rc) {
6516 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6517 "3174 Unable to update pls support, "
6518 "rc x%x\n", rc);
6519 }
James Smartcd713482018-10-23 13:41:01 -07006520 vports = lpfc_create_vport_work_array(phba);
6521 if (vports != NULL) {
6522 for (i = 0; i <= phba->max_vports && vports[i] != NULL;
6523 i++) {
6524 shost = lpfc_shost_from_vport(vports[i]);
6525 lpfc_host_supported_speeds_set(shost);
6526 }
6527 }
6528 lpfc_destroy_vport_work_array(phba, vports);
6529
James Smart448193b2015-12-16 18:12:05 -05006530 phba->sli4_hba.lnk_info.optic_state = status;
James Smart946727d2015-04-07 15:07:09 -04006531 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart448193b2015-12-16 18:12:05 -05006532 "3176 Port Name %c %s\n", port_name, message);
James Smart946727d2015-04-07 15:07:09 -04006533 break;
6534 case LPFC_SLI_EVENT_TYPE_REMOTE_DPORT:
6535 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
6536 "3192 Remote DPort Test Initiated - "
6537 "Event Data1:x%08x Event Data2: x%08x\n",
6538 acqe_sli->event_data1, acqe_sli->event_data2);
James Smart4b8bae02012-06-12 13:55:07 -04006539 break;
James Smart02243832021-08-16 09:28:54 -07006540 case LPFC_SLI_EVENT_TYPE_PORT_PARAMS_CHG:
6541 /* Call FW to obtain active parms */
6542 lpfc_sli4_cgn_parm_chg_evt(phba);
6543 break;
James Smarte7d85952019-10-18 14:18:29 -07006544 case LPFC_SLI_EVENT_TYPE_MISCONF_FAWWN:
6545 /* Misconfigured WWN. Reports that the SLI Port is configured
6546 * to use FA-WWN, but the attached device doesn’t support it.
6547 * No driver action is required.
6548 * Event Data1 - N.A, Event Data2 - N.A
6549 */
6550 lpfc_log_msg(phba, KERN_WARNING, LOG_SLI,
6551 "2699 Misconfigured FA-WWN - Attached device does "
6552 "not support FA-WWN\n");
6553 break;
James Smartd11ed162019-09-21 20:59:03 -07006554 case LPFC_SLI_EVENT_TYPE_EEPROM_FAILURE:
6555 /* EEPROM failure. No driver action is required */
6556 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
6557 "2518 EEPROM failure - "
6558 "Event Data1: x%08x Event Data2: x%08x\n",
6559 acqe_sli->event_data1, acqe_sli->event_data2);
6560 break;
James Smart9064aeb2021-08-16 09:28:50 -07006561 case LPFC_SLI_EVENT_TYPE_CGN_SIGNAL:
6562 if (phba->cmf_active_mode == LPFC_CFG_OFF)
6563 break;
6564 cgn_signal = (struct lpfc_acqe_cgn_signal *)
6565 &acqe_sli->event_data1;
6566 phba->cgn_acqe_cnt++;
6567
6568 cnt = bf_get(lpfc_warn_acqe, cgn_signal);
6569 atomic64_add(cnt, &phba->cgn_acqe_stat.warn);
6570 atomic64_add(cgn_signal->alarm_cnt, &phba->cgn_acqe_stat.alarm);
6571
6572 /* no threshold for CMF, even 1 signal will trigger an event */
6573
6574 /* Alarm overrides warning, so check that first */
6575 if (cgn_signal->alarm_cnt) {
6576 if (phba->cgn_reg_signal == EDC_CG_SIG_WARN_ALARM) {
6577 /* Keep track of alarm cnt for cgn_info */
6578 atomic_add(cgn_signal->alarm_cnt,
6579 &phba->cgn_fabric_alarm_cnt);
6580 /* Keep track of alarm cnt for CMF_SYNC_WQE */
6581 atomic_add(cgn_signal->alarm_cnt,
6582 &phba->cgn_sync_alarm_cnt);
6583 }
6584 } else if (cnt) {
6585 /* signal action needs to be taken */
6586 if (phba->cgn_reg_signal == EDC_CG_SIG_WARN_ONLY ||
6587 phba->cgn_reg_signal == EDC_CG_SIG_WARN_ALARM) {
6588 /* Keep track of warning cnt for cgn_info */
6589 atomic_add(cnt, &phba->cgn_fabric_warn_cnt);
6590 /* Keep track of warning cnt for CMF_SYNC_WQE */
6591 atomic_add(cnt, &phba->cgn_sync_warn_cnt);
6592 }
6593 }
6594 break;
James Smart4b8bae02012-06-12 13:55:07 -04006595 default:
James Smart946727d2015-04-07 15:07:09 -04006596 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartd11ed162019-09-21 20:59:03 -07006597 "3193 Unrecognized SLI event, type: 0x%x",
James Smart946727d2015-04-07 15:07:09 -04006598 evt_type);
James Smart4b8bae02012-06-12 13:55:07 -04006599 break;
6600 }
James Smart70f3c072010-12-15 17:57:33 -05006601}
6602
6603/**
James Smartfc2b9892010-02-26 14:15:29 -05006604 * lpfc_sli4_perform_vport_cvl - Perform clear virtual link on a vport
6605 * @vport: pointer to vport data structure.
6606 *
6607 * This routine is to perform Clear Virtual Link (CVL) on a vport in
6608 * response to a CVL event.
6609 *
6610 * Return the pointer to the ndlp with the vport if successful, otherwise
6611 * return NULL.
6612 **/
6613static struct lpfc_nodelist *
6614lpfc_sli4_perform_vport_cvl(struct lpfc_vport *vport)
6615{
6616 struct lpfc_nodelist *ndlp;
6617 struct Scsi_Host *shost;
6618 struct lpfc_hba *phba;
6619
6620 if (!vport)
6621 return NULL;
James Smartfc2b9892010-02-26 14:15:29 -05006622 phba = vport->phba;
6623 if (!phba)
6624 return NULL;
James Smart78730cf2010-04-06 15:06:30 -04006625 ndlp = lpfc_findnode_did(vport, Fabric_DID);
6626 if (!ndlp) {
6627 /* Cannot find existing Fabric ndlp, so allocate a new one */
James Smart9d3d3402017-04-21 16:05:00 -07006628 ndlp = lpfc_nlp_init(vport, Fabric_DID);
James Smart78730cf2010-04-06 15:06:30 -04006629 if (!ndlp)
Colin Ian King5860d9f2021-09-25 23:41:13 +01006630 return NULL;
James Smart78730cf2010-04-06 15:06:30 -04006631 /* Set the node type */
6632 ndlp->nlp_type |= NLP_FABRIC;
6633 /* Put ndlp onto node list */
6634 lpfc_enqueue_node(vport, ndlp);
James Smart78730cf2010-04-06 15:06:30 -04006635 }
James Smart63e801c2010-11-20 23:14:19 -05006636 if ((phba->pport->port_state < LPFC_FLOGI) &&
6637 (phba->pport->port_state != LPFC_VPORT_FAILED))
James Smartfc2b9892010-02-26 14:15:29 -05006638 return NULL;
6639 /* If virtual link is not yet instantiated ignore CVL */
James Smart63e801c2010-11-20 23:14:19 -05006640 if ((vport != phba->pport) && (vport->port_state < LPFC_FDISC)
6641 && (vport->port_state != LPFC_VPORT_FAILED))
James Smartfc2b9892010-02-26 14:15:29 -05006642 return NULL;
6643 shost = lpfc_shost_from_vport(vport);
6644 if (!shost)
6645 return NULL;
6646 lpfc_linkdown_port(vport);
6647 lpfc_cleanup_pending_mbox(vport);
6648 spin_lock_irq(shost->host_lock);
6649 vport->fc_flag |= FC_VPORT_CVL_RCVD;
6650 spin_unlock_irq(shost->host_lock);
6651
6652 return ndlp;
6653}
6654
6655/**
6656 * lpfc_sli4_perform_all_vport_cvl - Perform clear virtual link on all vports
Lee Jonesfe614ac2020-07-23 13:24:22 +01006657 * @phba: pointer to lpfc hba data structure.
James Smartfc2b9892010-02-26 14:15:29 -05006658 *
6659 * This routine is to perform Clear Virtual Link (CVL) on all vports in
6660 * response to a FCF dead event.
6661 **/
6662static void
6663lpfc_sli4_perform_all_vport_cvl(struct lpfc_hba *phba)
6664{
6665 struct lpfc_vport **vports;
6666 int i;
6667
6668 vports = lpfc_create_vport_work_array(phba);
6669 if (vports)
6670 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
6671 lpfc_sli4_perform_vport_cvl(vports[i]);
6672 lpfc_destroy_vport_work_array(phba, vports);
6673}
6674
6675/**
James Smart76a95d72010-11-20 23:11:48 -05006676 * lpfc_sli4_async_fip_evt - Process the asynchronous FCoE FIP event
James Smartda0436e2009-05-22 14:51:39 -04006677 * @phba: pointer to lpfc hba data structure.
Lee Jonesfe614ac2020-07-23 13:24:22 +01006678 * @acqe_fip: pointer to the async fcoe completion queue entry.
James Smartda0436e2009-05-22 14:51:39 -04006679 *
6680 * This routine is to handle the SLI4 asynchronous fcoe event.
6681 **/
6682static void
James Smart76a95d72010-11-20 23:11:48 -05006683lpfc_sli4_async_fip_evt(struct lpfc_hba *phba,
James Smart70f3c072010-12-15 17:57:33 -05006684 struct lpfc_acqe_fip *acqe_fip)
James Smartda0436e2009-05-22 14:51:39 -04006685{
James Smart70f3c072010-12-15 17:57:33 -05006686 uint8_t event_type = bf_get(lpfc_trailer_type, acqe_fip);
James Smartda0436e2009-05-22 14:51:39 -04006687 int rc;
James Smart6669f9b2009-10-02 15:16:45 -04006688 struct lpfc_vport *vport;
6689 struct lpfc_nodelist *ndlp;
James Smart695a8142010-01-26 23:08:03 -05006690 int active_vlink_present;
6691 struct lpfc_vport **vports;
6692 int i;
James Smartda0436e2009-05-22 14:51:39 -04006693
James Smart70f3c072010-12-15 17:57:33 -05006694 phba->fc_eventTag = acqe_fip->event_tag;
6695 phba->fcoe_eventtag = acqe_fip->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04006696 switch (event_type) {
James Smart70f3c072010-12-15 17:57:33 -05006697 case LPFC_FIP_EVENT_TYPE_NEW_FCF:
6698 case LPFC_FIP_EVENT_TYPE_FCF_PARAM_MOD:
6699 if (event_type == LPFC_FIP_EVENT_TYPE_NEW_FCF)
Dick Kennedy372c1872020-06-30 14:50:00 -07006700 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarta93ff372010-10-22 11:06:08 -04006701 "2546 New FCF event, evt_tag:x%x, "
6702 "index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05006703 acqe_fip->event_tag,
6704 acqe_fip->index);
James Smart999d8132010-03-15 11:24:56 -04006705 else
6706 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP |
6707 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04006708 "2788 FCF param modified event, "
6709 "evt_tag:x%x, index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05006710 acqe_fip->event_tag,
6711 acqe_fip->index);
James Smart38b92ef2010-08-04 16:11:39 -04006712 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smart0c9ab6f2010-02-26 14:15:57 -05006713 /*
6714 * During period of FCF discovery, read the FCF
6715 * table record indexed by the event to update
James Smarta93ff372010-10-22 11:06:08 -04006716 * FCF roundrobin failover eligible FCF bmask.
James Smart0c9ab6f2010-02-26 14:15:57 -05006717 */
6718 lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
6719 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04006720 "2779 Read FCF (x%x) for updating "
6721 "roundrobin FCF failover bmask\n",
James Smart70f3c072010-12-15 17:57:33 -05006722 acqe_fip->index);
6723 rc = lpfc_sli4_read_fcf_rec(phba, acqe_fip->index);
James Smart0c9ab6f2010-02-26 14:15:57 -05006724 }
James Smart38b92ef2010-08-04 16:11:39 -04006725
6726 /* If the FCF discovery is in progress, do nothing. */
James Smart3804dc82010-07-14 15:31:37 -04006727 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04006728 if (phba->hba_flag & FCF_TS_INPROG) {
James Smart38b92ef2010-08-04 16:11:39 -04006729 spin_unlock_irq(&phba->hbalock);
6730 break;
6731 }
6732 /* If fast FCF failover rescan event is pending, do nothing */
James Smart036cad12018-10-23 13:41:06 -07006733 if (phba->fcf.fcf_flag & (FCF_REDISC_EVT | FCF_REDISC_PEND)) {
James Smart38b92ef2010-08-04 16:11:39 -04006734 spin_unlock_irq(&phba->hbalock);
6735 break;
6736 }
6737
James Smartc2b97122013-05-31 17:05:36 -04006738 /* If the FCF has been in discovered state, do nothing. */
6739 if (phba->fcf.fcf_flag & FCF_SCAN_DONE) {
James Smart3804dc82010-07-14 15:31:37 -04006740 spin_unlock_irq(&phba->hbalock);
6741 break;
6742 }
6743 spin_unlock_irq(&phba->hbalock);
James Smart38b92ef2010-08-04 16:11:39 -04006744
James Smart0c9ab6f2010-02-26 14:15:57 -05006745 /* Otherwise, scan the entire FCF table and re-discover SAN */
6746 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04006747 "2770 Start FCF table scan per async FCF "
6748 "event, evt_tag:x%x, index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05006749 acqe_fip->event_tag, acqe_fip->index);
James Smart0c9ab6f2010-02-26 14:15:57 -05006750 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba,
6751 LPFC_FCOE_FCF_GET_FIRST);
James Smartda0436e2009-05-22 14:51:39 -04006752 if (rc)
Dick Kennedy372c1872020-06-30 14:50:00 -07006753 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart0c9ab6f2010-02-26 14:15:57 -05006754 "2547 Issue FCF scan read FCF mailbox "
James Smarta93ff372010-10-22 11:06:08 -04006755 "command failed (x%x)\n", rc);
James Smartda0436e2009-05-22 14:51:39 -04006756 break;
6757
James Smart70f3c072010-12-15 17:57:33 -05006758 case LPFC_FIP_EVENT_TYPE_FCF_TABLE_FULL:
Dick Kennedy372c1872020-06-30 14:50:00 -07006759 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6760 "2548 FCF Table full count 0x%x tag 0x%x\n",
6761 bf_get(lpfc_acqe_fip_fcf_count, acqe_fip),
6762 acqe_fip->event_tag);
James Smartda0436e2009-05-22 14:51:39 -04006763 break;
6764
James Smart70f3c072010-12-15 17:57:33 -05006765 case LPFC_FIP_EVENT_TYPE_FCF_DEAD:
James Smart80c17842012-03-01 22:35:45 -05006766 phba->fcoe_cvl_eventtag = acqe_fip->event_tag;
Dick Kennedy372c1872020-06-30 14:50:00 -07006767 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6768 "2549 FCF (x%x) disconnected from network, "
6769 "tag:x%x\n", acqe_fip->index,
6770 acqe_fip->event_tag);
James Smart38b92ef2010-08-04 16:11:39 -04006771 /*
6772 * If we are in the middle of FCF failover process, clear
6773 * the corresponding FCF bit in the roundrobin bitmap.
James Smartda0436e2009-05-22 14:51:39 -04006774 */
James Smartfc2b9892010-02-26 14:15:29 -05006775 spin_lock_irq(&phba->hbalock);
James Smarta1cadfe2016-07-06 12:36:02 -07006776 if ((phba->fcf.fcf_flag & FCF_DISCOVERY) &&
6777 (phba->fcf.current_rec.fcf_indx != acqe_fip->index)) {
James Smartfc2b9892010-02-26 14:15:29 -05006778 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05006779 /* Update FLOGI FCF failover eligible FCF bmask */
James Smart70f3c072010-12-15 17:57:33 -05006780 lpfc_sli4_fcf_rr_index_clear(phba, acqe_fip->index);
James Smartfc2b9892010-02-26 14:15:29 -05006781 break;
6782 }
James Smart38b92ef2010-08-04 16:11:39 -04006783 spin_unlock_irq(&phba->hbalock);
6784
6785 /* If the event is not for currently used fcf do nothing */
James Smart70f3c072010-12-15 17:57:33 -05006786 if (phba->fcf.current_rec.fcf_indx != acqe_fip->index)
James Smart38b92ef2010-08-04 16:11:39 -04006787 break;
6788
6789 /*
6790 * Otherwise, request the port to rediscover the entire FCF
6791 * table for a fast recovery from case that the current FCF
6792 * is no longer valid as we are not in the middle of FCF
6793 * failover process already.
6794 */
James Smartc2b97122013-05-31 17:05:36 -04006795 spin_lock_irq(&phba->hbalock);
6796 /* Mark the fast failover process in progress */
6797 phba->fcf.fcf_flag |= FCF_DEAD_DISC;
6798 spin_unlock_irq(&phba->hbalock);
6799
6800 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
6801 "2771 Start FCF fast failover process due to "
6802 "FCF DEAD event: evt_tag:x%x, fcf_index:x%x "
6803 "\n", acqe_fip->event_tag, acqe_fip->index);
6804 rc = lpfc_sli4_redisc_fcf_table(phba);
6805 if (rc) {
6806 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
Dick Kennedy372c1872020-06-30 14:50:00 -07006807 LOG_TRACE_EVENT,
Colin Ian King7afc0ce2018-05-03 10:26:12 +01006808 "2772 Issue FCF rediscover mailbox "
James Smartc2b97122013-05-31 17:05:36 -04006809 "command failed, fail through to FCF "
6810 "dead event\n");
6811 spin_lock_irq(&phba->hbalock);
6812 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
6813 spin_unlock_irq(&phba->hbalock);
6814 /*
6815 * Last resort will fail over by treating this
6816 * as a link down to FCF registration.
6817 */
6818 lpfc_sli4_fcf_dead_failthrough(phba);
6819 } else {
6820 /* Reset FCF roundrobin bmask for new discovery */
6821 lpfc_sli4_clear_fcf_rr_bmask(phba);
6822 /*
6823 * Handling fast FCF failover to a DEAD FCF event is
6824 * considered equalivant to receiving CVL to all vports.
6825 */
6826 lpfc_sli4_perform_all_vport_cvl(phba);
6827 }
James Smartda0436e2009-05-22 14:51:39 -04006828 break;
James Smart70f3c072010-12-15 17:57:33 -05006829 case LPFC_FIP_EVENT_TYPE_CVL:
James Smart80c17842012-03-01 22:35:45 -05006830 phba->fcoe_cvl_eventtag = acqe_fip->event_tag;
Dick Kennedy372c1872020-06-30 14:50:00 -07006831 lpfc_printf_log(phba, KERN_ERR,
6832 LOG_TRACE_EVENT,
James Smart6669f9b2009-10-02 15:16:45 -04006833 "2718 Clear Virtual Link Received for VPI 0x%x"
James Smart70f3c072010-12-15 17:57:33 -05006834 " tag 0x%x\n", acqe_fip->index, acqe_fip->event_tag);
James Smart6d368e52011-05-24 11:44:12 -04006835
James Smart6669f9b2009-10-02 15:16:45 -04006836 vport = lpfc_find_vport_by_vpid(phba,
James Smart5248a742011-07-22 18:37:06 -04006837 acqe_fip->index);
James Smartfc2b9892010-02-26 14:15:29 -05006838 ndlp = lpfc_sli4_perform_vport_cvl(vport);
James Smart6669f9b2009-10-02 15:16:45 -04006839 if (!ndlp)
6840 break;
James Smart695a8142010-01-26 23:08:03 -05006841 active_vlink_present = 0;
6842
6843 vports = lpfc_create_vport_work_array(phba);
6844 if (vports) {
6845 for (i = 0; i <= phba->max_vports && vports[i] != NULL;
6846 i++) {
6847 if ((!(vports[i]->fc_flag &
6848 FC_VPORT_CVL_RCVD)) &&
6849 (vports[i]->port_state > LPFC_FDISC)) {
6850 active_vlink_present = 1;
6851 break;
6852 }
6853 }
6854 lpfc_destroy_vport_work_array(phba, vports);
6855 }
6856
James Smartcc823552015-05-21 13:55:26 -04006857 /*
6858 * Don't re-instantiate if vport is marked for deletion.
6859 * If we are here first then vport_delete is going to wait
6860 * for discovery to complete.
6861 */
6862 if (!(vport->load_flag & FC_UNLOADING) &&
6863 active_vlink_present) {
James Smart695a8142010-01-26 23:08:03 -05006864 /*
6865 * If there are other active VLinks present,
6866 * re-instantiate the Vlink using FDISC.
6867 */
James Smart256ec0d2013-04-17 20:14:58 -04006868 mod_timer(&ndlp->nlp_delayfunc,
6869 jiffies + msecs_to_jiffies(1000));
James Smartc6adba12020-11-15 11:26:34 -08006870 spin_lock_irq(&ndlp->lock);
James Smart6669f9b2009-10-02 15:16:45 -04006871 ndlp->nlp_flag |= NLP_DELAY_TMO;
James Smartc6adba12020-11-15 11:26:34 -08006872 spin_unlock_irq(&ndlp->lock);
James Smart695a8142010-01-26 23:08:03 -05006873 ndlp->nlp_last_elscmd = ELS_CMD_FDISC;
6874 vport->port_state = LPFC_FDISC;
6875 } else {
James Smartecfd03c2010-02-12 14:41:27 -05006876 /*
6877 * Otherwise, we request port to rediscover
6878 * the entire FCF table for a fast recovery
6879 * from possible case that the current FCF
James Smart0c9ab6f2010-02-26 14:15:57 -05006880 * is no longer valid if we are not already
6881 * in the FCF failover process.
James Smartecfd03c2010-02-12 14:41:27 -05006882 */
James Smartfc2b9892010-02-26 14:15:29 -05006883 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05006884 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smartfc2b9892010-02-26 14:15:29 -05006885 spin_unlock_irq(&phba->hbalock);
6886 break;
6887 }
6888 /* Mark the fast failover process in progress */
James Smart0c9ab6f2010-02-26 14:15:57 -05006889 phba->fcf.fcf_flag |= FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05006890 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05006891 lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
6892 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04006893 "2773 Start FCF failover per CVL, "
James Smart70f3c072010-12-15 17:57:33 -05006894 "evt_tag:x%x\n", acqe_fip->event_tag);
James Smartecfd03c2010-02-12 14:41:27 -05006895 rc = lpfc_sli4_redisc_fcf_table(phba);
James Smartfc2b9892010-02-26 14:15:29 -05006896 if (rc) {
James Smart0c9ab6f2010-02-26 14:15:57 -05006897 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
Dick Kennedy372c1872020-06-30 14:50:00 -07006898 LOG_TRACE_EVENT,
James Smart0c9ab6f2010-02-26 14:15:57 -05006899 "2774 Issue FCF rediscover "
Colin Ian King7afc0ce2018-05-03 10:26:12 +01006900 "mailbox command failed, "
James Smart0c9ab6f2010-02-26 14:15:57 -05006901 "through to CVL event\n");
James Smartfc2b9892010-02-26 14:15:29 -05006902 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05006903 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05006904 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05006905 /*
6906 * Last resort will be re-try on the
6907 * the current registered FCF entry.
6908 */
6909 lpfc_retry_pport_discovery(phba);
James Smart38b92ef2010-08-04 16:11:39 -04006910 } else
6911 /*
6912 * Reset FCF roundrobin bmask for new
6913 * discovery.
6914 */
James Smart7d791df2011-07-22 18:37:52 -04006915 lpfc_sli4_clear_fcf_rr_bmask(phba);
James Smart6669f9b2009-10-02 15:16:45 -04006916 }
6917 break;
James Smartda0436e2009-05-22 14:51:39 -04006918 default:
Dick Kennedy372c1872020-06-30 14:50:00 -07006919 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6920 "0288 Unknown FCoE event type 0x%x event tag "
6921 "0x%x\n", event_type, acqe_fip->event_tag);
James Smartda0436e2009-05-22 14:51:39 -04006922 break;
6923 }
6924}
6925
6926/**
6927 * lpfc_sli4_async_dcbx_evt - Process the asynchronous dcbx event
6928 * @phba: pointer to lpfc hba data structure.
Lee Jonesfe614ac2020-07-23 13:24:22 +01006929 * @acqe_dcbx: pointer to the async dcbx completion queue entry.
James Smartda0436e2009-05-22 14:51:39 -04006930 *
6931 * This routine is to handle the SLI4 asynchronous dcbx event.
6932 **/
6933static void
6934lpfc_sli4_async_dcbx_evt(struct lpfc_hba *phba,
6935 struct lpfc_acqe_dcbx *acqe_dcbx)
6936{
James Smart4d9ab992009-10-02 15:16:39 -04006937 phba->fc_eventTag = acqe_dcbx->event_tag;
Dick Kennedy372c1872020-06-30 14:50:00 -07006938 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartda0436e2009-05-22 14:51:39 -04006939 "0290 The SLI4 DCBX asynchronous event is not "
6940 "handled yet\n");
6941}
6942
6943/**
James Smartb19a0612010-04-06 14:48:51 -04006944 * lpfc_sli4_async_grp5_evt - Process the asynchronous group5 event
6945 * @phba: pointer to lpfc hba data structure.
Lee Jonesfe614ac2020-07-23 13:24:22 +01006946 * @acqe_grp5: pointer to the async grp5 completion queue entry.
James Smartb19a0612010-04-06 14:48:51 -04006947 *
6948 * This routine is to handle the SLI4 asynchronous grp5 event. A grp5 event
6949 * is an asynchronous notified of a logical link speed change. The Port
6950 * reports the logical link speed in units of 10Mbps.
6951 **/
6952static void
6953lpfc_sli4_async_grp5_evt(struct lpfc_hba *phba,
6954 struct lpfc_acqe_grp5 *acqe_grp5)
6955{
6956 uint16_t prev_ll_spd;
6957
6958 phba->fc_eventTag = acqe_grp5->event_tag;
6959 phba->fcoe_eventtag = acqe_grp5->event_tag;
6960 prev_ll_spd = phba->sli4_hba.link_state.logical_speed;
6961 phba->sli4_hba.link_state.logical_speed =
James Smart8b68cd52012-09-29 11:32:37 -04006962 (bf_get(lpfc_acqe_grp5_llink_spd, acqe_grp5)) * 10;
James Smartb19a0612010-04-06 14:48:51 -04006963 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
6964 "2789 GRP5 Async Event: Updating logical link speed "
James Smart8b68cd52012-09-29 11:32:37 -04006965 "from %dMbps to %dMbps\n", prev_ll_spd,
6966 phba->sli4_hba.link_state.logical_speed);
James Smartb19a0612010-04-06 14:48:51 -04006967}
6968
6969/**
James Smart02243832021-08-16 09:28:54 -07006970 * lpfc_sli4_async_cmstat_evt - Process the asynchronous cmstat event
6971 * @phba: pointer to lpfc hba data structure.
6972 *
6973 * This routine is to handle the SLI4 asynchronous cmstat event. A cmstat event
6974 * is an asynchronous notification of a request to reset CM stats.
6975 **/
6976static void
6977lpfc_sli4_async_cmstat_evt(struct lpfc_hba *phba)
6978{
6979 if (!phba->cgn_i)
6980 return;
6981 lpfc_init_congestion_stat(phba);
6982}
6983
6984/**
James Smart72df8a452021-08-16 09:28:52 -07006985 * lpfc_cgn_params_val - Validate FW congestion parameters.
6986 * @phba: pointer to lpfc hba data structure.
6987 * @p_cfg_param: pointer to FW provided congestion parameters.
6988 *
6989 * This routine validates the congestion parameters passed
6990 * by the FW to the driver via an ACQE event.
6991 **/
6992static void
6993lpfc_cgn_params_val(struct lpfc_hba *phba, struct lpfc_cgn_param *p_cfg_param)
6994{
6995 spin_lock_irq(&phba->hbalock);
6996
6997 if (!lpfc_rangecheck(p_cfg_param->cgn_param_mode, LPFC_CFG_OFF,
6998 LPFC_CFG_MONITOR)) {
6999 lpfc_printf_log(phba, KERN_ERR, LOG_CGN_MGMT,
7000 "6225 CMF mode param out of range: %d\n",
7001 p_cfg_param->cgn_param_mode);
7002 p_cfg_param->cgn_param_mode = LPFC_CFG_OFF;
7003 }
7004
7005 spin_unlock_irq(&phba->hbalock);
7006}
7007
7008/**
7009 * lpfc_cgn_params_parse - Process a FW cong parm change event
7010 * @phba: pointer to lpfc hba data structure.
7011 * @p_cgn_param: pointer to a data buffer with the FW cong params.
7012 * @len: the size of pdata in bytes.
7013 *
7014 * This routine validates the congestion management buffer signature
7015 * from the FW, validates the contents and makes corrections for
7016 * valid, in-range values. If the signature magic is correct and
7017 * after parameter validation, the contents are copied to the driver's
7018 * @phba structure. If the magic is incorrect, an error message is
7019 * logged.
7020 **/
7021static void
7022lpfc_cgn_params_parse(struct lpfc_hba *phba,
7023 struct lpfc_cgn_param *p_cgn_param, uint32_t len)
7024{
James Smart7481811c2021-08-16 09:28:56 -07007025 struct lpfc_cgn_info *cp;
7026 uint32_t crc, oldmode;
James Smart72df8a452021-08-16 09:28:52 -07007027
7028 /* Make sure the FW has encoded the correct magic number to
7029 * validate the congestion parameter in FW memory.
7030 */
7031 if (p_cgn_param->cgn_param_magic == LPFC_CFG_PARAM_MAGIC_NUM) {
7032 lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT | LOG_INIT,
7033 "4668 FW cgn parm buffer data: "
7034 "magic 0x%x version %d mode %d "
7035 "level0 %d level1 %d "
7036 "level2 %d byte13 %d "
7037 "byte14 %d byte15 %d "
7038 "byte11 %d byte12 %d activeMode %d\n",
7039 p_cgn_param->cgn_param_magic,
7040 p_cgn_param->cgn_param_version,
7041 p_cgn_param->cgn_param_mode,
7042 p_cgn_param->cgn_param_level0,
7043 p_cgn_param->cgn_param_level1,
7044 p_cgn_param->cgn_param_level2,
7045 p_cgn_param->byte13,
7046 p_cgn_param->byte14,
7047 p_cgn_param->byte15,
7048 p_cgn_param->byte11,
7049 p_cgn_param->byte12,
7050 phba->cmf_active_mode);
7051
7052 oldmode = phba->cmf_active_mode;
7053
7054 /* Any parameters out of range are corrected to defaults
7055 * by this routine. No need to fail.
7056 */
7057 lpfc_cgn_params_val(phba, p_cgn_param);
7058
7059 /* Parameters are verified, move them into driver storage */
7060 spin_lock_irq(&phba->hbalock);
7061 memcpy(&phba->cgn_p, p_cgn_param,
7062 sizeof(struct lpfc_cgn_param));
7063
James Smart7481811c2021-08-16 09:28:56 -07007064 /* Update parameters in congestion info buffer now */
7065 if (phba->cgn_i) {
7066 cp = (struct lpfc_cgn_info *)phba->cgn_i->virt;
7067 cp->cgn_info_mode = phba->cgn_p.cgn_param_mode;
7068 cp->cgn_info_level0 = phba->cgn_p.cgn_param_level0;
7069 cp->cgn_info_level1 = phba->cgn_p.cgn_param_level1;
7070 cp->cgn_info_level2 = phba->cgn_p.cgn_param_level2;
7071 crc = lpfc_cgn_calc_crc32(cp, LPFC_CGN_INFO_SZ,
7072 LPFC_CGN_CRC32_SEED);
7073 cp->cgn_info_crc = cpu_to_le32(crc);
7074 }
James Smart72df8a452021-08-16 09:28:52 -07007075 spin_unlock_irq(&phba->hbalock);
7076
7077 phba->cmf_active_mode = phba->cgn_p.cgn_param_mode;
7078
7079 switch (oldmode) {
7080 case LPFC_CFG_OFF:
7081 if (phba->cgn_p.cgn_param_mode != LPFC_CFG_OFF) {
7082 /* Turning CMF on */
James Smart02243832021-08-16 09:28:54 -07007083 lpfc_cmf_start(phba);
James Smart72df8a452021-08-16 09:28:52 -07007084
7085 if (phba->link_state >= LPFC_LINK_UP) {
7086 phba->cgn_reg_fpin =
7087 phba->cgn_init_reg_fpin;
7088 phba->cgn_reg_signal =
7089 phba->cgn_init_reg_signal;
7090 lpfc_issue_els_edc(phba->pport, 0);
7091 }
7092 }
7093 break;
7094 case LPFC_CFG_MANAGED:
7095 switch (phba->cgn_p.cgn_param_mode) {
7096 case LPFC_CFG_OFF:
7097 /* Turning CMF off */
James Smart02243832021-08-16 09:28:54 -07007098 lpfc_cmf_stop(phba);
James Smart72df8a452021-08-16 09:28:52 -07007099 if (phba->link_state >= LPFC_LINK_UP)
7100 lpfc_issue_els_edc(phba->pport, 0);
7101 break;
7102 case LPFC_CFG_MONITOR:
7103 lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
7104 "4661 Switch from MANAGED to "
7105 "`MONITOR mode\n");
James Smart02243832021-08-16 09:28:54 -07007106 phba->cmf_max_bytes_per_interval =
7107 phba->cmf_link_byte_count;
7108
7109 /* Resume blocked IO - unblock on workqueue */
7110 queue_work(phba->wq,
7111 &phba->unblock_request_work);
James Smart72df8a452021-08-16 09:28:52 -07007112 break;
7113 }
7114 break;
7115 case LPFC_CFG_MONITOR:
7116 switch (phba->cgn_p.cgn_param_mode) {
7117 case LPFC_CFG_OFF:
7118 /* Turning CMF off */
James Smart02243832021-08-16 09:28:54 -07007119 lpfc_cmf_stop(phba);
James Smart72df8a452021-08-16 09:28:52 -07007120 if (phba->link_state >= LPFC_LINK_UP)
7121 lpfc_issue_els_edc(phba->pport, 0);
7122 break;
7123 case LPFC_CFG_MANAGED:
7124 lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
7125 "4662 Switch from MONITOR to "
7126 "MANAGED mode\n");
James Smart02243832021-08-16 09:28:54 -07007127 lpfc_cmf_signal_init(phba);
James Smart72df8a452021-08-16 09:28:52 -07007128 break;
7129 }
7130 break;
7131 }
7132 } else {
7133 lpfc_printf_log(phba, KERN_ERR, LOG_CGN_MGMT | LOG_INIT,
7134 "4669 FW cgn parm buf wrong magic 0x%x "
7135 "version %d\n", p_cgn_param->cgn_param_magic,
7136 p_cgn_param->cgn_param_version);
7137 }
7138}
7139
7140/**
7141 * lpfc_sli4_cgn_params_read - Read and Validate FW congestion parameters.
7142 * @phba: pointer to lpfc hba data structure.
7143 *
7144 * This routine issues a read_object mailbox command to
7145 * get the congestion management parameters from the FW
7146 * parses it and updates the driver maintained values.
7147 *
7148 * Returns
7149 * 0 if the object was empty
7150 * -Eval if an error was encountered
7151 * Count if bytes were read from object
7152 **/
7153int
7154lpfc_sli4_cgn_params_read(struct lpfc_hba *phba)
7155{
7156 int ret = 0;
7157 struct lpfc_cgn_param *p_cgn_param = NULL;
7158 u32 *pdata = NULL;
7159 u32 len = 0;
7160
7161 /* Find out if the FW has a new set of congestion parameters. */
7162 len = sizeof(struct lpfc_cgn_param);
7163 pdata = kzalloc(len, GFP_KERNEL);
7164 ret = lpfc_read_object(phba, (char *)LPFC_PORT_CFG_NAME,
7165 pdata, len);
7166
7167 /* 0 means no data. A negative means error. A positive means
7168 * bytes were copied.
7169 */
7170 if (!ret) {
7171 lpfc_printf_log(phba, KERN_ERR, LOG_CGN_MGMT | LOG_INIT,
7172 "4670 CGN RD OBJ returns no data\n");
7173 goto rd_obj_err;
7174 } else if (ret < 0) {
7175 /* Some error. Just exit and return it to the caller.*/
7176 goto rd_obj_err;
7177 }
7178
7179 lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT | LOG_INIT,
7180 "6234 READ CGN PARAMS Successful %d\n", len);
7181
7182 /* Parse data pointer over len and update the phba congestion
7183 * parameters with values passed back. The receive rate values
7184 * may have been altered in FW, but take no action here.
7185 */
7186 p_cgn_param = (struct lpfc_cgn_param *)pdata;
7187 lpfc_cgn_params_parse(phba, p_cgn_param, len);
7188
7189 rd_obj_err:
7190 kfree(pdata);
7191 return ret;
7192}
7193
7194/**
James Smart02243832021-08-16 09:28:54 -07007195 * lpfc_sli4_cgn_parm_chg_evt - Process a FW congestion param change event
7196 * @phba: pointer to lpfc hba data structure.
7197 *
7198 * The FW generated Async ACQE SLI event calls this routine when
7199 * the event type is an SLI Internal Port Event and the Event Code
7200 * indicates a change to the FW maintained congestion parameters.
7201 *
7202 * This routine executes a Read_Object mailbox call to obtain the
7203 * current congestion parameters maintained in FW and corrects
7204 * the driver's active congestion parameters.
7205 *
7206 * The acqe event is not passed because there is no further data
7207 * required.
7208 *
7209 * Returns nonzero error if event processing encountered an error.
7210 * Zero otherwise for success.
7211 **/
7212static int
7213lpfc_sli4_cgn_parm_chg_evt(struct lpfc_hba *phba)
7214{
7215 int ret = 0;
7216
7217 if (!phba->sli4_hba.pc_sli4_params.cmf) {
7218 lpfc_printf_log(phba, KERN_ERR, LOG_CGN_MGMT | LOG_INIT,
7219 "4664 Cgn Evt when E2E off. Drop event\n");
7220 return -EACCES;
7221 }
7222
7223 /* If the event is claiming an empty object, it's ok. A write
7224 * could have cleared it. Only error is a negative return
7225 * status.
7226 */
7227 ret = lpfc_sli4_cgn_params_read(phba);
7228 if (ret < 0) {
7229 lpfc_printf_log(phba, KERN_ERR, LOG_CGN_MGMT | LOG_INIT,
7230 "4667 Error reading Cgn Params (%d)\n",
7231 ret);
7232 } else if (!ret) {
7233 lpfc_printf_log(phba, KERN_ERR, LOG_CGN_MGMT | LOG_INIT,
7234 "4673 CGN Event empty object.\n");
7235 }
7236 return ret;
7237}
7238
7239/**
James Smartda0436e2009-05-22 14:51:39 -04007240 * lpfc_sli4_async_event_proc - Process all the pending asynchronous event
7241 * @phba: pointer to lpfc hba data structure.
7242 *
7243 * This routine is invoked by the worker thread to process all the pending
7244 * SLI4 asynchronous events.
7245 **/
7246void lpfc_sli4_async_event_proc(struct lpfc_hba *phba)
7247{
7248 struct lpfc_cq_event *cq_event;
James Smarte7dab162020-10-20 13:27:12 -07007249 unsigned long iflags;
James Smartda0436e2009-05-22 14:51:39 -04007250
7251 /* First, declare the async event has been handled */
James Smarte7dab162020-10-20 13:27:12 -07007252 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartda0436e2009-05-22 14:51:39 -04007253 phba->hba_flag &= ~ASYNC_EVENT;
James Smarte7dab162020-10-20 13:27:12 -07007254 spin_unlock_irqrestore(&phba->hbalock, iflags);
7255
James Smartda0436e2009-05-22 14:51:39 -04007256 /* Now, handle all the async events */
James Smarte7dab162020-10-20 13:27:12 -07007257 spin_lock_irqsave(&phba->sli4_hba.asynce_list_lock, iflags);
James Smartda0436e2009-05-22 14:51:39 -04007258 while (!list_empty(&phba->sli4_hba.sp_asynce_work_queue)) {
James Smartda0436e2009-05-22 14:51:39 -04007259 list_remove_head(&phba->sli4_hba.sp_asynce_work_queue,
7260 cq_event, struct lpfc_cq_event, list);
James Smarte7dab162020-10-20 13:27:12 -07007261 spin_unlock_irqrestore(&phba->sli4_hba.asynce_list_lock,
7262 iflags);
7263
James Smartda0436e2009-05-22 14:51:39 -04007264 /* Process the asynchronous event */
7265 switch (bf_get(lpfc_trailer_code, &cq_event->cqe.mcqe_cmpl)) {
7266 case LPFC_TRAILER_CODE_LINK:
7267 lpfc_sli4_async_link_evt(phba,
7268 &cq_event->cqe.acqe_link);
7269 break;
7270 case LPFC_TRAILER_CODE_FCOE:
James Smart70f3c072010-12-15 17:57:33 -05007271 lpfc_sli4_async_fip_evt(phba, &cq_event->cqe.acqe_fip);
James Smartda0436e2009-05-22 14:51:39 -04007272 break;
7273 case LPFC_TRAILER_CODE_DCBX:
7274 lpfc_sli4_async_dcbx_evt(phba,
7275 &cq_event->cqe.acqe_dcbx);
7276 break;
James Smartb19a0612010-04-06 14:48:51 -04007277 case LPFC_TRAILER_CODE_GRP5:
7278 lpfc_sli4_async_grp5_evt(phba,
7279 &cq_event->cqe.acqe_grp5);
7280 break;
James Smart70f3c072010-12-15 17:57:33 -05007281 case LPFC_TRAILER_CODE_FC:
7282 lpfc_sli4_async_fc_evt(phba, &cq_event->cqe.acqe_fc);
7283 break;
7284 case LPFC_TRAILER_CODE_SLI:
7285 lpfc_sli4_async_sli_evt(phba, &cq_event->cqe.acqe_sli);
7286 break;
James Smart02243832021-08-16 09:28:54 -07007287 case LPFC_TRAILER_CODE_CMSTAT:
7288 lpfc_sli4_async_cmstat_evt(phba);
7289 break;
James Smartda0436e2009-05-22 14:51:39 -04007290 default:
Dick Kennedy372c1872020-06-30 14:50:00 -07007291 lpfc_printf_log(phba, KERN_ERR,
7292 LOG_TRACE_EVENT,
Colin Ian King291c2542019-12-18 08:43:01 +00007293 "1804 Invalid asynchronous event code: "
James Smartda0436e2009-05-22 14:51:39 -04007294 "x%x\n", bf_get(lpfc_trailer_code,
7295 &cq_event->cqe.mcqe_cmpl));
7296 break;
7297 }
James Smarte7dab162020-10-20 13:27:12 -07007298
James Smartda0436e2009-05-22 14:51:39 -04007299 /* Free the completion event processed to the free pool */
7300 lpfc_sli4_cq_event_release(phba, cq_event);
James Smarte7dab162020-10-20 13:27:12 -07007301 spin_lock_irqsave(&phba->sli4_hba.asynce_list_lock, iflags);
James Smartda0436e2009-05-22 14:51:39 -04007302 }
James Smarte7dab162020-10-20 13:27:12 -07007303 spin_unlock_irqrestore(&phba->sli4_hba.asynce_list_lock, iflags);
James Smartda0436e2009-05-22 14:51:39 -04007304}
7305
7306/**
James Smartecfd03c2010-02-12 14:41:27 -05007307 * lpfc_sli4_fcf_redisc_event_proc - Process fcf table rediscovery event
7308 * @phba: pointer to lpfc hba data structure.
7309 *
7310 * This routine is invoked by the worker thread to process FCF table
7311 * rediscovery pending completion event.
7312 **/
7313void lpfc_sli4_fcf_redisc_event_proc(struct lpfc_hba *phba)
7314{
7315 int rc;
7316
7317 spin_lock_irq(&phba->hbalock);
7318 /* Clear FCF rediscovery timeout event */
7319 phba->fcf.fcf_flag &= ~FCF_REDISC_EVT;
7320 /* Clear driver fast failover FCF record flag */
7321 phba->fcf.failover_rec.flag = 0;
7322 /* Set state for FCF fast failover */
7323 phba->fcf.fcf_flag |= FCF_REDISC_FOV;
7324 spin_unlock_irq(&phba->hbalock);
7325
7326 /* Scan FCF table from the first entry to re-discover SAN */
James Smart0c9ab6f2010-02-26 14:15:57 -05007327 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04007328 "2777 Start post-quiescent FCF table scan\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05007329 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
James Smartecfd03c2010-02-12 14:41:27 -05007330 if (rc)
Dick Kennedy372c1872020-06-30 14:50:00 -07007331 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart0c9ab6f2010-02-26 14:15:57 -05007332 "2747 Issue FCF scan read FCF mailbox "
7333 "command failed 0x%x\n", rc);
James Smartecfd03c2010-02-12 14:41:27 -05007334}
7335
7336/**
James Smartda0436e2009-05-22 14:51:39 -04007337 * lpfc_api_table_setup - Set up per hba pci-device group func api jump table
7338 * @phba: pointer to lpfc hba data structure.
7339 * @dev_grp: The HBA PCI-Device group number.
7340 *
7341 * This routine is invoked to set up the per HBA PCI-Device group function
7342 * API jump table entries.
7343 *
7344 * Return: 0 if success, otherwise -ENODEV
7345 **/
7346int
7347lpfc_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
James Smart5b75da22008-12-04 22:39:35 -05007348{
7349 int rc;
7350
James Smartda0436e2009-05-22 14:51:39 -04007351 /* Set up lpfc PCI-device group */
7352 phba->pci_dev_grp = dev_grp;
James Smart5b75da22008-12-04 22:39:35 -05007353
James Smartda0436e2009-05-22 14:51:39 -04007354 /* The LPFC_PCI_DEV_OC uses SLI4 */
7355 if (dev_grp == LPFC_PCI_DEV_OC)
7356 phba->sli_rev = LPFC_SLI_REV4;
James Smart5b75da22008-12-04 22:39:35 -05007357
James Smartda0436e2009-05-22 14:51:39 -04007358 /* Set up device INIT API function jump table */
7359 rc = lpfc_init_api_table_setup(phba, dev_grp);
7360 if (rc)
7361 return -ENODEV;
7362 /* Set up SCSI API function jump table */
7363 rc = lpfc_scsi_api_table_setup(phba, dev_grp);
7364 if (rc)
7365 return -ENODEV;
7366 /* Set up SLI API function jump table */
7367 rc = lpfc_sli_api_table_setup(phba, dev_grp);
7368 if (rc)
7369 return -ENODEV;
7370 /* Set up MBOX API function jump table */
7371 rc = lpfc_mbox_api_table_setup(phba, dev_grp);
7372 if (rc)
7373 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05007374
James Smartda0436e2009-05-22 14:51:39 -04007375 return 0;
James Smart5b75da22008-12-04 22:39:35 -05007376}
7377
7378/**
James Smart3621a712009-04-06 18:47:14 -04007379 * lpfc_log_intr_mode - Log the active interrupt mode
James Smart5b75da22008-12-04 22:39:35 -05007380 * @phba: pointer to lpfc hba data structure.
7381 * @intr_mode: active interrupt mode adopted.
7382 *
7383 * This routine it invoked to log the currently used active interrupt mode
7384 * to the device.
James Smart3772a992009-05-22 14:50:54 -04007385 **/
7386static void lpfc_log_intr_mode(struct lpfc_hba *phba, uint32_t intr_mode)
James Smart5b75da22008-12-04 22:39:35 -05007387{
7388 switch (intr_mode) {
7389 case 0:
7390 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7391 "0470 Enable INTx interrupt mode.\n");
7392 break;
7393 case 1:
7394 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7395 "0481 Enabled MSI interrupt mode.\n");
7396 break;
7397 case 2:
7398 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7399 "0480 Enabled MSI-X interrupt mode.\n");
7400 break;
7401 default:
Dick Kennedy372c1872020-06-30 14:50:00 -07007402 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart5b75da22008-12-04 22:39:35 -05007403 "0482 Illegal interrupt mode.\n");
7404 break;
7405 }
7406 return;
7407}
7408
James Smart5b75da22008-12-04 22:39:35 -05007409/**
James Smart3772a992009-05-22 14:50:54 -04007410 * lpfc_enable_pci_dev - Enable a generic PCI device.
James Smart5b75da22008-12-04 22:39:35 -05007411 * @phba: pointer to lpfc hba data structure.
7412 *
James Smart3772a992009-05-22 14:50:54 -04007413 * This routine is invoked to enable the PCI device that is common to all
7414 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05007415 *
7416 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007417 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04007418 * other values - error
James Smart5b75da22008-12-04 22:39:35 -05007419 **/
James Smart3772a992009-05-22 14:50:54 -04007420static int
7421lpfc_enable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05007422{
James Smart3772a992009-05-22 14:50:54 -04007423 struct pci_dev *pdev;
James Smart5b75da22008-12-04 22:39:35 -05007424
James Smart3772a992009-05-22 14:50:54 -04007425 /* Obtain PCI device reference */
7426 if (!phba->pcidev)
7427 goto out_error;
7428 else
7429 pdev = phba->pcidev;
James Smart3772a992009-05-22 14:50:54 -04007430 /* Enable PCI device */
7431 if (pci_enable_device_mem(pdev))
7432 goto out_error;
7433 /* Request PCI resource for the device */
Johannes Thumshirne0c04832016-06-07 09:44:03 +02007434 if (pci_request_mem_regions(pdev, LPFC_DRIVER_NAME))
James Smart3772a992009-05-22 14:50:54 -04007435 goto out_disable_device;
7436 /* Set up device as PCI master and save state for EEH */
7437 pci_set_master(pdev);
7438 pci_try_set_mwi(pdev);
7439 pci_save_state(pdev);
James Smart5b75da22008-12-04 22:39:35 -05007440
James Smart05580562011-05-24 11:40:48 -04007441 /* PCIe EEH recovery on powerpc platforms needs fundamental reset */
Jon Mason453193e2013-09-11 15:38:13 -07007442 if (pci_is_pcie(pdev))
James Smart05580562011-05-24 11:40:48 -04007443 pdev->needs_freset = 1;
7444
James Smart3772a992009-05-22 14:50:54 -04007445 return 0;
James Smart5b75da22008-12-04 22:39:35 -05007446
James Smart3772a992009-05-22 14:50:54 -04007447out_disable_device:
7448 pci_disable_device(pdev);
7449out_error:
James Smarta5160742021-10-20 14:14:10 -07007450 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
Johannes Thumshirne0c04832016-06-07 09:44:03 +02007451 "1401 Failed to enable pci device\n");
James Smart3772a992009-05-22 14:50:54 -04007452 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05007453}
7454
7455/**
James Smart3772a992009-05-22 14:50:54 -04007456 * lpfc_disable_pci_dev - Disable a generic PCI device.
James Smart5b75da22008-12-04 22:39:35 -05007457 * @phba: pointer to lpfc hba data structure.
7458 *
James Smart3772a992009-05-22 14:50:54 -04007459 * This routine is invoked to disable the PCI device that is common to all
7460 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05007461 **/
7462static void
James Smart3772a992009-05-22 14:50:54 -04007463lpfc_disable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05007464{
James Smart3772a992009-05-22 14:50:54 -04007465 struct pci_dev *pdev;
James Smart5b75da22008-12-04 22:39:35 -05007466
James Smart3772a992009-05-22 14:50:54 -04007467 /* Obtain PCI device reference */
7468 if (!phba->pcidev)
7469 return;
7470 else
7471 pdev = phba->pcidev;
James Smart3772a992009-05-22 14:50:54 -04007472 /* Release PCI resource and disable PCI device */
Johannes Thumshirne0c04832016-06-07 09:44:03 +02007473 pci_release_mem_regions(pdev);
James Smart3772a992009-05-22 14:50:54 -04007474 pci_disable_device(pdev);
James Smart5b75da22008-12-04 22:39:35 -05007475
7476 return;
7477}
7478
7479/**
James Smart3772a992009-05-22 14:50:54 -04007480 * lpfc_reset_hba - Reset a hba
7481 * @phba: pointer to lpfc hba data structure.
James Smarte59058c2008-08-24 21:49:00 -04007482 *
James Smart3772a992009-05-22 14:50:54 -04007483 * This routine is invoked to reset a hba device. It brings the HBA
7484 * offline, performs a board restart, and then brings the board back
7485 * online. The lpfc_offline calls lpfc_sli_hba_down which will clean up
7486 * on outstanding mailbox commands.
James Smarte59058c2008-08-24 21:49:00 -04007487 **/
James Smart3772a992009-05-22 14:50:54 -04007488void
7489lpfc_reset_hba(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05007490{
James Smart3772a992009-05-22 14:50:54 -04007491 /* If resets are disabled then set error state and return. */
7492 if (!phba->cfg_enable_hba_reset) {
7493 phba->link_state = LPFC_HBA_ERROR;
7494 return;
7495 }
James Smart9ec58ec2021-01-04 10:02:35 -08007496
7497 /* If not LPFC_SLI_ACTIVE, force all IO to be flushed */
7498 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE) {
James Smartee620212014-04-04 13:51:53 -04007499 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart9ec58ec2021-01-04 10:02:35 -08007500 } else {
James Smartee620212014-04-04 13:51:53 -04007501 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
James Smart9ec58ec2021-01-04 10:02:35 -08007502 lpfc_sli_flush_io_rings(phba);
7503 }
James Smart3772a992009-05-22 14:50:54 -04007504 lpfc_offline(phba);
7505 lpfc_sli_brdrestart(phba);
7506 lpfc_online(phba);
7507 lpfc_unblock_mgmt_io(phba);
7508}
dea31012005-04-17 16:05:31 -05007509
James Smart3772a992009-05-22 14:50:54 -04007510/**
James Smart0a96e972011-07-22 18:37:28 -04007511 * lpfc_sli_sriov_nr_virtfn_get - Get the number of sr-iov virtual functions
7512 * @phba: pointer to lpfc hba data structure.
7513 *
7514 * This function enables the PCI SR-IOV virtual functions to a physical
7515 * function. It invokes the PCI SR-IOV api with the @nr_vfn provided to
7516 * enable the number of virtual functions to the physical function. As
7517 * not all devices support SR-IOV, the return code from the pci_enable_sriov()
7518 * API call does not considered as an error condition for most of the device.
7519 **/
7520uint16_t
7521lpfc_sli_sriov_nr_virtfn_get(struct lpfc_hba *phba)
7522{
7523 struct pci_dev *pdev = phba->pcidev;
7524 uint16_t nr_virtfn;
7525 int pos;
7526
James Smart0a96e972011-07-22 18:37:28 -04007527 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
7528 if (pos == 0)
7529 return 0;
7530
7531 pci_read_config_word(pdev, pos + PCI_SRIOV_TOTAL_VF, &nr_virtfn);
7532 return nr_virtfn;
7533}
7534
7535/**
James Smart912e3ac2011-05-24 11:42:11 -04007536 * lpfc_sli_probe_sriov_nr_virtfn - Enable a number of sr-iov virtual functions
7537 * @phba: pointer to lpfc hba data structure.
7538 * @nr_vfn: number of virtual functions to be enabled.
7539 *
7540 * This function enables the PCI SR-IOV virtual functions to a physical
7541 * function. It invokes the PCI SR-IOV api with the @nr_vfn provided to
7542 * enable the number of virtual functions to the physical function. As
7543 * not all devices support SR-IOV, the return code from the pci_enable_sriov()
7544 * API call does not considered as an error condition for most of the device.
7545 **/
7546int
7547lpfc_sli_probe_sriov_nr_virtfn(struct lpfc_hba *phba, int nr_vfn)
7548{
7549 struct pci_dev *pdev = phba->pcidev;
James Smart0a96e972011-07-22 18:37:28 -04007550 uint16_t max_nr_vfn;
James Smart912e3ac2011-05-24 11:42:11 -04007551 int rc;
7552
James Smart0a96e972011-07-22 18:37:28 -04007553 max_nr_vfn = lpfc_sli_sriov_nr_virtfn_get(phba);
7554 if (nr_vfn > max_nr_vfn) {
Dick Kennedy372c1872020-06-30 14:50:00 -07007555 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart0a96e972011-07-22 18:37:28 -04007556 "3057 Requested vfs (%d) greater than "
7557 "supported vfs (%d)", nr_vfn, max_nr_vfn);
7558 return -EINVAL;
7559 }
7560
James Smart912e3ac2011-05-24 11:42:11 -04007561 rc = pci_enable_sriov(pdev, nr_vfn);
7562 if (rc) {
7563 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7564 "2806 Failed to enable sriov on this device "
7565 "with vfn number nr_vf:%d, rc:%d\n",
7566 nr_vfn, rc);
7567 } else
7568 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7569 "2807 Successful enable sriov on this device "
7570 "with vfn number nr_vf:%d\n", nr_vfn);
7571 return rc;
7572}
7573
James Smart02243832021-08-16 09:28:54 -07007574static void
7575lpfc_unblock_requests_work(struct work_struct *work)
7576{
7577 struct lpfc_hba *phba = container_of(work, struct lpfc_hba,
7578 unblock_request_work);
7579
7580 lpfc_unblock_requests(phba);
7581}
7582
James Smart912e3ac2011-05-24 11:42:11 -04007583/**
James Smart895427b2017-02-12 13:52:30 -08007584 * lpfc_setup_driver_resource_phase1 - Phase1 etup driver internal resources.
James Smart3772a992009-05-22 14:50:54 -04007585 * @phba: pointer to lpfc hba data structure.
7586 *
James Smart895427b2017-02-12 13:52:30 -08007587 * This routine is invoked to set up the driver internal resources before the
7588 * device specific resource setup to support the HBA device it attached to.
James Smart3772a992009-05-22 14:50:54 -04007589 *
7590 * Return codes
James Smart895427b2017-02-12 13:52:30 -08007591 * 0 - successful
7592 * other values - error
James Smart3772a992009-05-22 14:50:54 -04007593 **/
7594static int
James Smart895427b2017-02-12 13:52:30 -08007595lpfc_setup_driver_resource_phase1(struct lpfc_hba *phba)
James Smart3772a992009-05-22 14:50:54 -04007596{
James Smart895427b2017-02-12 13:52:30 -08007597 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05007598
James Smart3772a992009-05-22 14:50:54 -04007599 /*
James Smart895427b2017-02-12 13:52:30 -08007600 * Driver resources common to all SLI revisions
James Smart3772a992009-05-22 14:50:54 -04007601 */
James Smart895427b2017-02-12 13:52:30 -08007602 atomic_set(&phba->fast_event_count, 0);
Dick Kennedy372c1872020-06-30 14:50:00 -07007603 atomic_set(&phba->dbg_log_idx, 0);
7604 atomic_set(&phba->dbg_log_cnt, 0);
7605 atomic_set(&phba->dbg_log_dmping, 0);
James Smart895427b2017-02-12 13:52:30 -08007606 spin_lock_init(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007607
James Smart523128e2018-09-10 10:30:46 -07007608 /* Initialize port_list spinlock */
7609 spin_lock_init(&phba->port_list_lock);
James Smart895427b2017-02-12 13:52:30 -08007610 INIT_LIST_HEAD(&phba->port_list);
James Smart523128e2018-09-10 10:30:46 -07007611
James Smart895427b2017-02-12 13:52:30 -08007612 INIT_LIST_HEAD(&phba->work_list);
7613 init_waitqueue_head(&phba->wait_4_mlo_m_q);
7614
7615 /* Initialize the wait queue head for the kernel thread */
7616 init_waitqueue_head(&phba->work_waitq);
7617
7618 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smartf358dd02017-02-12 13:52:34 -08007619 "1403 Protocols supported %s %s %s\n",
James Smart895427b2017-02-12 13:52:30 -08007620 ((phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) ?
7621 "SCSI" : " "),
7622 ((phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) ?
James Smartf358dd02017-02-12 13:52:34 -08007623 "NVME" : " "),
7624 (phba->nvmet_support ? "NVMET" : " "));
James Smart895427b2017-02-12 13:52:30 -08007625
James Smart0794d602019-01-28 11:14:19 -08007626 /* Initialize the IO buffer list used by driver for SLI3 SCSI */
7627 spin_lock_init(&phba->scsi_buf_list_get_lock);
7628 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_get);
7629 spin_lock_init(&phba->scsi_buf_list_put_lock);
7630 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
James Smart895427b2017-02-12 13:52:30 -08007631
7632 /* Initialize the fabric iocb list */
7633 INIT_LIST_HEAD(&phba->fabric_iocb_list);
7634
7635 /* Initialize list to save ELS buffers */
7636 INIT_LIST_HEAD(&phba->elsbuf);
7637
7638 /* Initialize FCF connection rec list */
7639 INIT_LIST_HEAD(&phba->fcf_conn_rec_list);
7640
7641 /* Initialize OAS configuration list */
7642 spin_lock_init(&phba->devicelock);
7643 INIT_LIST_HEAD(&phba->luns);
7644
James Smart3772a992009-05-22 14:50:54 -04007645 /* MBOX heartbeat timer */
Kees Cookf22eb4d2017-09-06 20:24:26 -07007646 timer_setup(&psli->mbox_tmo, lpfc_mbox_timeout, 0);
James Smart3772a992009-05-22 14:50:54 -04007647 /* Fabric block timer */
Kees Cookf22eb4d2017-09-06 20:24:26 -07007648 timer_setup(&phba->fabric_block_timer, lpfc_fabric_block_timeout, 0);
James Smart3772a992009-05-22 14:50:54 -04007649 /* EA polling mode timer */
Kees Cookf22eb4d2017-09-06 20:24:26 -07007650 timer_setup(&phba->eratt_poll, lpfc_poll_eratt, 0);
James Smart895427b2017-02-12 13:52:30 -08007651 /* Heartbeat timer */
Kees Cookf22eb4d2017-09-06 20:24:26 -07007652 timer_setup(&phba->hb_tmofunc, lpfc_hb_timeout, 0);
James Smart895427b2017-02-12 13:52:30 -08007653
James Smart32517fc2019-01-28 11:14:33 -08007654 INIT_DELAYED_WORK(&phba->eq_delay_work, lpfc_hb_eq_delay_work);
7655
Dick Kennedy317aeb82020-06-30 14:49:59 -07007656 INIT_DELAYED_WORK(&phba->idle_stat_delay_work,
7657 lpfc_idle_stat_delay_work);
James Smart02243832021-08-16 09:28:54 -07007658 INIT_WORK(&phba->unblock_request_work, lpfc_unblock_requests_work);
James Smart895427b2017-02-12 13:52:30 -08007659 return 0;
7660}
7661
7662/**
7663 * lpfc_sli_driver_resource_setup - Setup driver internal resources for SLI3 dev
7664 * @phba: pointer to lpfc hba data structure.
7665 *
7666 * This routine is invoked to set up the driver internal resources specific to
7667 * support the SLI-3 HBA device it attached to.
7668 *
7669 * Return codes
7670 * 0 - successful
7671 * other values - error
7672 **/
7673static int
7674lpfc_sli_driver_resource_setup(struct lpfc_hba *phba)
7675{
James Smart0794d602019-01-28 11:14:19 -08007676 int rc, entry_sz;
James Smart895427b2017-02-12 13:52:30 -08007677
7678 /*
7679 * Initialize timers used by driver
7680 */
7681
7682 /* FCP polling mode timer */
Kees Cookf22eb4d2017-09-06 20:24:26 -07007683 timer_setup(&phba->fcp_poll_timer, lpfc_poll_timeout, 0);
James Smart3772a992009-05-22 14:50:54 -04007684
7685 /* Host attention work mask setup */
7686 phba->work_ha_mask = (HA_ERATT | HA_MBATT | HA_LATT);
7687 phba->work_ha_mask |= (HA_RXMASK << (LPFC_ELS_RING * 4));
7688
7689 /* Get all the module params for configuring this host */
7690 lpfc_get_cfgparam(phba);
James Smart895427b2017-02-12 13:52:30 -08007691 /* Set up phase-1 common device driver resources */
7692
7693 rc = lpfc_setup_driver_resource_phase1(phba);
7694 if (rc)
7695 return -ENODEV;
7696
James Smart49198b32010-04-06 15:04:33 -04007697 if (phba->pcidev->device == PCI_DEVICE_ID_HORNET) {
7698 phba->menlo_flag |= HBA_MENLO_SUPPORT;
7699 /* check for menlo minimum sg count */
7700 if (phba->cfg_sg_seg_cnt < LPFC_DEFAULT_MENLO_SG_SEG_CNT)
7701 phba->cfg_sg_seg_cnt = LPFC_DEFAULT_MENLO_SG_SEG_CNT;
7702 }
7703
James Smart895427b2017-02-12 13:52:30 -08007704 if (!phba->sli.sli3_ring)
Kees Cook6396bb22018-06-12 14:03:40 -07007705 phba->sli.sli3_ring = kcalloc(LPFC_SLI3_MAX_RING,
7706 sizeof(struct lpfc_sli_ring),
7707 GFP_KERNEL);
James Smart895427b2017-02-12 13:52:30 -08007708 if (!phba->sli.sli3_ring)
James Smart2a76a282012-08-03 12:35:54 -04007709 return -ENOMEM;
7710
James Smart3772a992009-05-22 14:50:54 -04007711 /*
James Smart96f70772013-04-17 20:16:15 -04007712 * Since lpfc_sg_seg_cnt is module parameter, the sg_dma_buf_size
James Smart3772a992009-05-22 14:50:54 -04007713 * used to create the sg_dma_buf_pool must be dynamically calculated.
James Smart3772a992009-05-22 14:50:54 -04007714 */
James Smart3772a992009-05-22 14:50:54 -04007715
James Smart0794d602019-01-28 11:14:19 -08007716 if (phba->sli_rev == LPFC_SLI_REV4)
7717 entry_sz = sizeof(struct sli4_sge);
7718 else
7719 entry_sz = sizeof(struct ulp_bde64);
7720
James Smart96f70772013-04-17 20:16:15 -04007721 /* There are going to be 2 reserved BDEs: 1 FCP cmnd + 1 FCP rsp */
7722 if (phba->cfg_enable_bg) {
7723 /*
7724 * The scsi_buf for a T10-DIF I/O will hold the FCP cmnd,
7725 * the FCP rsp, and a BDE for each. Sice we have no control
7726 * over how many protection data segments the SCSI Layer
7727 * will hand us (ie: there could be one for every block
7728 * in the IO), we just allocate enough BDEs to accomidate
7729 * our max amount and we need to limit lpfc_sg_seg_cnt to
7730 * minimize the risk of running out.
7731 */
7732 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
7733 sizeof(struct fcp_rsp) +
James Smart0794d602019-01-28 11:14:19 -08007734 (LPFC_MAX_SG_SEG_CNT * entry_sz);
James Smart96f70772013-04-17 20:16:15 -04007735
7736 if (phba->cfg_sg_seg_cnt > LPFC_MAX_SG_SEG_CNT_DIF)
7737 phba->cfg_sg_seg_cnt = LPFC_MAX_SG_SEG_CNT_DIF;
7738
7739 /* Total BDEs in BPL for scsi_sg_list and scsi_sg_prot_list */
7740 phba->cfg_total_seg_cnt = LPFC_MAX_SG_SEG_CNT;
7741 } else {
7742 /*
7743 * The scsi_buf for a regular I/O will hold the FCP cmnd,
7744 * the FCP rsp, a BDE for each, and a BDE for up to
7745 * cfg_sg_seg_cnt data segments.
7746 */
7747 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
7748 sizeof(struct fcp_rsp) +
James Smart0794d602019-01-28 11:14:19 -08007749 ((phba->cfg_sg_seg_cnt + 2) * entry_sz);
James Smart96f70772013-04-17 20:16:15 -04007750
7751 /* Total BDEs in BPL for scsi_sg_list */
7752 phba->cfg_total_seg_cnt = phba->cfg_sg_seg_cnt + 2;
7753 }
7754
7755 lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_FCP,
James Smartc90b4482020-03-22 11:12:56 -07007756 "9088 INIT sg_tablesize:%d dmabuf_size:%d total_bde:%d\n",
James Smart96f70772013-04-17 20:16:15 -04007757 phba->cfg_sg_seg_cnt, phba->cfg_sg_dma_buf_size,
7758 phba->cfg_total_seg_cnt);
7759
James Smart3772a992009-05-22 14:50:54 -04007760 phba->max_vpi = LPFC_MAX_VPI;
7761 /* This will be set to correct value after config_port mbox */
7762 phba->max_vports = 0;
7763
7764 /*
7765 * Initialize the SLI Layer to run with lpfc HBAs.
7766 */
7767 lpfc_sli_setup(phba);
James Smart895427b2017-02-12 13:52:30 -08007768 lpfc_sli_queue_init(phba);
James Smart3772a992009-05-22 14:50:54 -04007769
7770 /* Allocate device driver memory */
7771 if (lpfc_mem_alloc(phba, BPL_ALIGN_SZ))
7772 return -ENOMEM;
7773
James Smartd79c9e92019-08-14 16:57:09 -07007774 phba->lpfc_sg_dma_buf_pool =
7775 dma_pool_create("lpfc_sg_dma_buf_pool",
7776 &phba->pcidev->dev, phba->cfg_sg_dma_buf_size,
7777 BPL_ALIGN_SZ, 0);
7778
7779 if (!phba->lpfc_sg_dma_buf_pool)
7780 goto fail_free_mem;
7781
7782 phba->lpfc_cmd_rsp_buf_pool =
7783 dma_pool_create("lpfc_cmd_rsp_buf_pool",
7784 &phba->pcidev->dev,
7785 sizeof(struct fcp_cmnd) +
7786 sizeof(struct fcp_rsp),
7787 BPL_ALIGN_SZ, 0);
7788
7789 if (!phba->lpfc_cmd_rsp_buf_pool)
7790 goto fail_free_dma_buf_pool;
7791
James Smart912e3ac2011-05-24 11:42:11 -04007792 /*
7793 * Enable sr-iov virtual functions if supported and configured
7794 * through the module parameter.
7795 */
7796 if (phba->cfg_sriov_nr_virtfn > 0) {
7797 rc = lpfc_sli_probe_sriov_nr_virtfn(phba,
7798 phba->cfg_sriov_nr_virtfn);
7799 if (rc) {
7800 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7801 "2808 Requested number of SR-IOV "
7802 "virtual functions (%d) is not "
7803 "supported\n",
7804 phba->cfg_sriov_nr_virtfn);
7805 phba->cfg_sriov_nr_virtfn = 0;
7806 }
7807 }
7808
James Smart3772a992009-05-22 14:50:54 -04007809 return 0;
James Smartd79c9e92019-08-14 16:57:09 -07007810
7811fail_free_dma_buf_pool:
7812 dma_pool_destroy(phba->lpfc_sg_dma_buf_pool);
7813 phba->lpfc_sg_dma_buf_pool = NULL;
7814fail_free_mem:
7815 lpfc_mem_free(phba);
7816 return -ENOMEM;
James Smart3772a992009-05-22 14:50:54 -04007817}
7818
7819/**
7820 * lpfc_sli_driver_resource_unset - Unset drvr internal resources for SLI3 dev
7821 * @phba: pointer to lpfc hba data structure.
7822 *
7823 * This routine is invoked to unset the driver internal resources set up
7824 * specific for supporting the SLI-3 HBA device it attached to.
7825 **/
7826static void
7827lpfc_sli_driver_resource_unset(struct lpfc_hba *phba)
7828{
7829 /* Free device driver memory allocated */
7830 lpfc_mem_free_all(phba);
7831
7832 return;
7833}
7834
7835/**
James Smartda0436e2009-05-22 14:51:39 -04007836 * lpfc_sli4_driver_resource_setup - Setup drvr internal resources for SLI4 dev
7837 * @phba: pointer to lpfc hba data structure.
7838 *
7839 * This routine is invoked to set up the driver internal resources specific to
7840 * support the SLI-4 HBA device it attached to.
7841 *
7842 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007843 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04007844 * other values - error
7845 **/
7846static int
7847lpfc_sli4_driver_resource_setup(struct lpfc_hba *phba)
7848{
James Smart28baac72010-02-12 14:42:03 -05007849 LPFC_MBOXQ_t *mboxq;
James Smartf358dd02017-02-12 13:52:34 -08007850 MAILBOX_t *mb;
James Smart895427b2017-02-12 13:52:30 -08007851 int rc, i, max_buf_size;
James Smart09294d42013-04-17 20:16:05 -04007852 int longs;
James Smart81e6a632017-11-20 16:00:43 -08007853 int extra;
James Smartf358dd02017-02-12 13:52:34 -08007854 uint64_t wwn;
James Smartb92dc722018-05-24 21:09:01 -07007855 u32 if_type;
7856 u32 if_fam;
James Smartda0436e2009-05-22 14:51:39 -04007857
James Smart895427b2017-02-12 13:52:30 -08007858 phba->sli4_hba.num_present_cpu = lpfc_present_cpu;
James Smarteede4972019-11-21 09:55:56 -08007859 phba->sli4_hba.num_possible_cpu = cpumask_last(cpu_possible_mask) + 1;
James Smart895427b2017-02-12 13:52:30 -08007860 phba->sli4_hba.curr_disp_cpu = 0;
7861
James Smart716d3bc2013-09-06 12:18:28 -04007862 /* Get all the module params for configuring this host */
7863 lpfc_get_cfgparam(phba);
7864
James Smart895427b2017-02-12 13:52:30 -08007865 /* Set up phase-1 common device driver resources */
7866 rc = lpfc_setup_driver_resource_phase1(phba);
7867 if (rc)
7868 return -ENODEV;
7869
James Smartda0436e2009-05-22 14:51:39 -04007870 /* Before proceed, wait for POST done and device ready */
7871 rc = lpfc_sli4_post_status_check(phba);
7872 if (rc)
7873 return -ENODEV;
7874
James Smart3cee98d2019-08-14 16:56:34 -07007875 /* Allocate all driver workqueues here */
7876
7877 /* The lpfc_wq workqueue for deferred irq use */
7878 phba->wq = alloc_workqueue("lpfc_wq", WQ_MEM_RECLAIM, 0);
7879
James Smartda0436e2009-05-22 14:51:39 -04007880 /*
7881 * Initialize timers used by driver
7882 */
7883
Kees Cookf22eb4d2017-09-06 20:24:26 -07007884 timer_setup(&phba->rrq_tmr, lpfc_rrq_timeout, 0);
James Smartda0436e2009-05-22 14:51:39 -04007885
James Smartecfd03c2010-02-12 14:41:27 -05007886 /* FCF rediscover timer */
Kees Cookf22eb4d2017-09-06 20:24:26 -07007887 timer_setup(&phba->fcf.redisc_wait, lpfc_sli4_fcf_redisc_wait_tmo, 0);
James Smartecfd03c2010-02-12 14:41:27 -05007888
James Smart02243832021-08-16 09:28:54 -07007889 /* CMF congestion timer */
7890 hrtimer_init(&phba->cmf_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
7891 phba->cmf_timer.function = lpfc_cmf_timer;
7892
James Smartda0436e2009-05-22 14:51:39 -04007893 /*
James Smart7ad20aa2011-05-24 11:44:28 -04007894 * Control structure for handling external multi-buffer mailbox
7895 * command pass-through.
7896 */
7897 memset((uint8_t *)&phba->mbox_ext_buf_ctx, 0,
7898 sizeof(struct lpfc_mbox_ext_buf_ctx));
7899 INIT_LIST_HEAD(&phba->mbox_ext_buf_ctx.ext_dmabuf_list);
7900
James Smartda0436e2009-05-22 14:51:39 -04007901 phba->max_vpi = LPFC_MAX_VPI;
James Smart67d12732012-08-03 12:36:13 -04007902
James Smartda0436e2009-05-22 14:51:39 -04007903 /* This will be set to correct value after the read_config mbox */
7904 phba->max_vports = 0;
7905
7906 /* Program the default value of vlan_id and fc_map */
7907 phba->valid_vlan = 0;
7908 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
7909 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
7910 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
7911
7912 /*
James Smart2a76a282012-08-03 12:35:54 -04007913 * For SLI4, instead of using ring 0 (LPFC_FCP_RING) for FCP commands
James Smart895427b2017-02-12 13:52:30 -08007914 * we will associate a new ring, for each EQ/CQ/WQ tuple.
7915 * The WQ create will allocate the ring.
James Smart2a76a282012-08-03 12:35:54 -04007916 */
James Smart085c6472010-11-20 23:11:37 -05007917
James Smartda0436e2009-05-22 14:51:39 -04007918 /* Initialize buffer queue management fields */
James Smart895427b2017-02-12 13:52:30 -08007919 INIT_LIST_HEAD(&phba->hbqs[LPFC_ELS_HBQ].hbq_buffer_list);
James Smartda0436e2009-05-22 14:51:39 -04007920 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_sli4_rb_alloc;
7921 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_sli4_rb_free;
7922
Gaurav Srivastava20397172021-06-08 10:05:54 +05307923 /* for VMID idle timeout if VMID is enabled */
7924 if (lpfc_is_vmid_enabled(phba))
7925 timer_setup(&phba->inactive_vmid_poll, lpfc_vmid_poll, 0);
7926
James Smartda0436e2009-05-22 14:51:39 -04007927 /*
7928 * Initialize the SLI Layer to run with lpfc SLI4 HBAs.
7929 */
James Smartc00f62e2019-08-14 16:57:11 -07007930 /* Initialize the Abort buffer list used by driver */
7931 spin_lock_init(&phba->sli4_hba.abts_io_buf_list_lock);
7932 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_io_buf_list);
James Smart895427b2017-02-12 13:52:30 -08007933
7934 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
7935 /* Initialize the Abort nvme buffer list used by driver */
James Smart5e5b5112019-01-28 11:14:22 -08007936 spin_lock_init(&phba->sli4_hba.abts_nvmet_buf_list_lock);
James Smart86c67372017-04-21 16:05:04 -07007937 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_nvmet_ctx_list);
James Smarta8cf5df2017-05-15 15:20:46 -07007938 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_nvmet_io_wait_list);
James Smart79d8c4c2019-05-21 17:48:56 -07007939 spin_lock_init(&phba->sli4_hba.t_active_list_lock);
7940 INIT_LIST_HEAD(&phba->sli4_hba.t_active_ctx_list);
James Smart895427b2017-02-12 13:52:30 -08007941 }
7942
James Smartda0436e2009-05-22 14:51:39 -04007943 /* This abort list used by worker thread */
James Smart895427b2017-02-12 13:52:30 -08007944 spin_lock_init(&phba->sli4_hba.sgl_list_lock);
James Smarta8cf5df2017-05-15 15:20:46 -07007945 spin_lock_init(&phba->sli4_hba.nvmet_io_wait_lock);
James Smarte7dab162020-10-20 13:27:12 -07007946 spin_lock_init(&phba->sli4_hba.asynce_list_lock);
7947 spin_lock_init(&phba->sli4_hba.els_xri_abrt_list_lock);
James Smartda0436e2009-05-22 14:51:39 -04007948
7949 /*
James Smart6d368e52011-05-24 11:44:12 -04007950 * Initialize driver internal slow-path work queues
James Smartda0436e2009-05-22 14:51:39 -04007951 */
7952
7953 /* Driver internel slow-path CQ Event pool */
7954 INIT_LIST_HEAD(&phba->sli4_hba.sp_cqe_event_pool);
7955 /* Response IOCB work queue list */
James Smart45ed1192009-10-02 15:17:02 -04007956 INIT_LIST_HEAD(&phba->sli4_hba.sp_queue_event);
James Smartda0436e2009-05-22 14:51:39 -04007957 /* Asynchronous event CQ Event work queue list */
7958 INIT_LIST_HEAD(&phba->sli4_hba.sp_asynce_work_queue);
James Smartda0436e2009-05-22 14:51:39 -04007959 /* Slow-path XRI aborted CQ Event work queue list */
7960 INIT_LIST_HEAD(&phba->sli4_hba.sp_els_xri_aborted_work_queue);
7961 /* Receive queue CQ Event work queue list */
7962 INIT_LIST_HEAD(&phba->sli4_hba.sp_unsol_work_queue);
7963
James Smart6d368e52011-05-24 11:44:12 -04007964 /* Initialize extent block lists. */
7965 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_blk_list);
7966 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_xri_blk_list);
7967 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_vfi_blk_list);
7968 INIT_LIST_HEAD(&phba->lpfc_vpi_blk_list);
7969
James Smartd1f525a2017-04-21 16:04:55 -07007970 /* Initialize mboxq lists. If the early init routines fail
7971 * these lists need to be correctly initialized.
7972 */
7973 INIT_LIST_HEAD(&phba->sli.mboxq);
7974 INIT_LIST_HEAD(&phba->sli.mboxq_cmpl);
7975
James Smart448193b2015-12-16 18:12:05 -05007976 /* initialize optic_state to 0xFF */
7977 phba->sli4_hba.lnk_info.optic_state = 0xff;
7978
James Smartda0436e2009-05-22 14:51:39 -04007979 /* Allocate device driver memory */
7980 rc = lpfc_mem_alloc(phba, SGL_ALIGN_SZ);
7981 if (rc)
7982 return -ENOMEM;
7983
James Smart2fcee4b2010-12-15 17:57:46 -05007984 /* IF Type 2 ports get initialized now. */
James Smart27d6ac02018-02-22 08:18:42 -08007985 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) >=
James Smart2fcee4b2010-12-15 17:57:46 -05007986 LPFC_SLI_INTF_IF_TYPE_2) {
7987 rc = lpfc_pci_function_reset(phba);
James Smart895427b2017-02-12 13:52:30 -08007988 if (unlikely(rc)) {
7989 rc = -ENODEV;
7990 goto out_free_mem;
7991 }
James Smart946727d2015-04-07 15:07:09 -04007992 phba->temp_sensor_support = 1;
James Smart2fcee4b2010-12-15 17:57:46 -05007993 }
7994
James Smartda0436e2009-05-22 14:51:39 -04007995 /* Create the bootstrap mailbox command */
7996 rc = lpfc_create_bootstrap_mbox(phba);
7997 if (unlikely(rc))
7998 goto out_free_mem;
7999
8000 /* Set up the host's endian order with the device. */
8001 rc = lpfc_setup_endian_order(phba);
8002 if (unlikely(rc))
8003 goto out_free_bsmbx;
8004
8005 /* Set up the hba's configuration parameters. */
8006 rc = lpfc_sli4_read_config(phba);
8007 if (unlikely(rc))
8008 goto out_free_bsmbx;
James Smartcff261f2013-12-17 20:29:47 -05008009 rc = lpfc_mem_alloc_active_rrq_pool_s4(phba);
8010 if (unlikely(rc))
8011 goto out_free_bsmbx;
James Smartda0436e2009-05-22 14:51:39 -04008012
James Smart2fcee4b2010-12-15 17:57:46 -05008013 /* IF Type 0 ports get initialized now. */
8014 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
8015 LPFC_SLI_INTF_IF_TYPE_0) {
8016 rc = lpfc_pci_function_reset(phba);
8017 if (unlikely(rc))
8018 goto out_free_bsmbx;
8019 }
James Smartda0436e2009-05-22 14:51:39 -04008020
James Smartcb5172e2010-03-15 11:25:07 -04008021 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
8022 GFP_KERNEL);
8023 if (!mboxq) {
8024 rc = -ENOMEM;
8025 goto out_free_bsmbx;
8026 }
8027
James Smartf358dd02017-02-12 13:52:34 -08008028 /* Check for NVMET being configured */
James Smart895427b2017-02-12 13:52:30 -08008029 phba->nvmet_support = 0;
James Smartf358dd02017-02-12 13:52:34 -08008030 if (lpfc_enable_nvmet_cnt) {
8031
8032 /* First get WWN of HBA instance */
8033 lpfc_read_nv(phba, mboxq);
8034 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8035 if (rc != MBX_SUCCESS) {
Dick Kennedy372c1872020-06-30 14:50:00 -07008036 lpfc_printf_log(phba, KERN_ERR,
8037 LOG_TRACE_EVENT,
James Smartf358dd02017-02-12 13:52:34 -08008038 "6016 Mailbox failed , mbxCmd x%x "
8039 "READ_NV, mbxStatus x%x\n",
8040 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
8041 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
James Smartd1f525a2017-04-21 16:04:55 -07008042 mempool_free(mboxq, phba->mbox_mem_pool);
James Smartf358dd02017-02-12 13:52:34 -08008043 rc = -EIO;
8044 goto out_free_bsmbx;
8045 }
8046 mb = &mboxq->u.mb;
8047 memcpy(&wwn, (char *)mb->un.varRDnvp.nodename,
8048 sizeof(uint64_t));
8049 wwn = cpu_to_be64(wwn);
8050 phba->sli4_hba.wwnn.u.name = wwn;
8051 memcpy(&wwn, (char *)mb->un.varRDnvp.portname,
8052 sizeof(uint64_t));
8053 /* wwn is WWPN of HBA instance */
8054 wwn = cpu_to_be64(wwn);
8055 phba->sli4_hba.wwpn.u.name = wwn;
8056
8057 /* Check to see if it matches any module parameter */
8058 for (i = 0; i < lpfc_enable_nvmet_cnt; i++) {
8059 if (wwn == lpfc_enable_nvmet[i]) {
James Smart7d708032017-03-08 14:36:01 -08008060#if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
James Smart3c603be2017-05-15 15:20:44 -07008061 if (lpfc_nvmet_mem_alloc(phba))
8062 break;
8063
8064 phba->nvmet_support = 1; /* a match */
8065
Dick Kennedy372c1872020-06-30 14:50:00 -07008066 lpfc_printf_log(phba, KERN_ERR,
8067 LOG_TRACE_EVENT,
James Smartf358dd02017-02-12 13:52:34 -08008068 "6017 NVME Target %016llx\n",
8069 wwn);
James Smart7d708032017-03-08 14:36:01 -08008070#else
Dick Kennedy372c1872020-06-30 14:50:00 -07008071 lpfc_printf_log(phba, KERN_ERR,
8072 LOG_TRACE_EVENT,
James Smart7d708032017-03-08 14:36:01 -08008073 "6021 Can't enable NVME Target."
8074 " NVME_TARGET_FC infrastructure"
8075 " is not in kernel\n");
8076#endif
James Smartc4908502019-01-28 11:14:28 -08008077 /* Not supported for NVMET */
8078 phba->cfg_xri_rebalancing = 0;
Dick Kennedy3048e3e2020-05-01 14:43:06 -07008079 if (phba->irq_chann_mode == NHT_MODE) {
8080 phba->cfg_irq_chann =
8081 phba->sli4_hba.num_present_cpu;
8082 phba->cfg_hdw_queue =
8083 phba->sli4_hba.num_present_cpu;
8084 phba->irq_chann_mode = NORMAL_MODE;
8085 }
James Smart3c603be2017-05-15 15:20:44 -07008086 break;
James Smartf358dd02017-02-12 13:52:34 -08008087 }
8088 }
8089 }
James Smart895427b2017-02-12 13:52:30 -08008090
8091 lpfc_nvme_mod_param_dep(phba);
8092
James Smartfedd3b72011-02-16 12:39:24 -05008093 /*
8094 * Get sli4 parameters that override parameters from Port capabilities.
James Smart6d368e52011-05-24 11:44:12 -04008095 * If this call fails, it isn't critical unless the SLI4 parameters come
8096 * back in conflict.
James Smartfedd3b72011-02-16 12:39:24 -05008097 */
James Smart6d368e52011-05-24 11:44:12 -04008098 rc = lpfc_get_sli4_parameters(phba, mboxq);
8099 if (rc) {
James Smartb92dc722018-05-24 21:09:01 -07008100 if_type = bf_get(lpfc_sli_intf_if_type,
8101 &phba->sli4_hba.sli_intf);
8102 if_fam = bf_get(lpfc_sli_intf_sli_family,
8103 &phba->sli4_hba.sli_intf);
James Smart6d368e52011-05-24 11:44:12 -04008104 if (phba->sli4_hba.extents_in_use &&
8105 phba->sli4_hba.rpi_hdrs_in_use) {
Dick Kennedy372c1872020-06-30 14:50:00 -07008106 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8107 "2999 Unsupported SLI4 Parameters "
8108 "Extents and RPI headers enabled.\n");
James Smartb92dc722018-05-24 21:09:01 -07008109 if (if_type == LPFC_SLI_INTF_IF_TYPE_0 &&
8110 if_fam == LPFC_SLI_INTF_FAMILY_BE2) {
8111 mempool_free(mboxq, phba->mbox_mem_pool);
8112 rc = -EIO;
8113 goto out_free_bsmbx;
8114 }
James Smart6d368e52011-05-24 11:44:12 -04008115 }
James Smartb92dc722018-05-24 21:09:01 -07008116 if (!(if_type == LPFC_SLI_INTF_IF_TYPE_0 &&
8117 if_fam == LPFC_SLI_INTF_FAMILY_BE2)) {
8118 mempool_free(mboxq, phba->mbox_mem_pool);
8119 rc = -EIO;
8120 goto out_free_bsmbx;
8121 }
James Smart6d368e52011-05-24 11:44:12 -04008122 }
James Smart895427b2017-02-12 13:52:30 -08008123
James Smartd79c9e92019-08-14 16:57:09 -07008124 /*
8125 * 1 for cmd, 1 for rsp, NVME adds an extra one
8126 * for boundary conditions in its max_sgl_segment template.
8127 */
8128 extra = 2;
8129 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
8130 extra++;
8131
8132 /*
8133 * It doesn't matter what family our adapter is in, we are
8134 * limited to 2 Pages, 512 SGEs, for our SGL.
8135 * There are going to be 2 reserved SGEs: 1 FCP cmnd + 1 FCP rsp
8136 */
8137 max_buf_size = (2 * SLI4_PAGE_SIZE);
8138
8139 /*
8140 * Since lpfc_sg_seg_cnt is module param, the sg_dma_buf_size
8141 * used to create the sg_dma_buf_pool must be calculated.
8142 */
8143 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED) {
8144 /* Both cfg_enable_bg and cfg_external_dif code paths */
8145
8146 /*
8147 * The scsi_buf for a T10-DIF I/O holds the FCP cmnd,
8148 * the FCP rsp, and a SGE. Sice we have no control
8149 * over how many protection segments the SCSI Layer
8150 * will hand us (ie: there could be one for every block
8151 * in the IO), just allocate enough SGEs to accomidate
8152 * our max amount and we need to limit lpfc_sg_seg_cnt
8153 * to minimize the risk of running out.
8154 */
8155 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
8156 sizeof(struct fcp_rsp) + max_buf_size;
8157
8158 /* Total SGEs for scsi_sg_list and scsi_sg_prot_list */
8159 phba->cfg_total_seg_cnt = LPFC_MAX_SGL_SEG_CNT;
8160
8161 /*
8162 * If supporting DIF, reduce the seg count for scsi to
8163 * allow room for the DIF sges.
8164 */
8165 if (phba->cfg_enable_bg &&
8166 phba->cfg_sg_seg_cnt > LPFC_MAX_BG_SLI4_SEG_CNT_DIF)
8167 phba->cfg_scsi_seg_cnt = LPFC_MAX_BG_SLI4_SEG_CNT_DIF;
8168 else
8169 phba->cfg_scsi_seg_cnt = phba->cfg_sg_seg_cnt;
8170
8171 } else {
8172 /*
8173 * The scsi_buf for a regular I/O holds the FCP cmnd,
8174 * the FCP rsp, a SGE for each, and a SGE for up to
8175 * cfg_sg_seg_cnt data segments.
8176 */
8177 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
8178 sizeof(struct fcp_rsp) +
8179 ((phba->cfg_sg_seg_cnt + extra) *
8180 sizeof(struct sli4_sge));
8181
8182 /* Total SGEs for scsi_sg_list */
8183 phba->cfg_total_seg_cnt = phba->cfg_sg_seg_cnt + extra;
8184 phba->cfg_scsi_seg_cnt = phba->cfg_sg_seg_cnt;
8185
8186 /*
8187 * NOTE: if (phba->cfg_sg_seg_cnt + extra) <= 256 we only
8188 * need to post 1 page for the SGL.
8189 */
8190 }
8191
8192 if (phba->cfg_xpsgl && !phba->nvmet_support)
8193 phba->cfg_sg_dma_buf_size = LPFC_DEFAULT_XPSGL_SIZE;
8194 else if (phba->cfg_sg_dma_buf_size <= LPFC_MIN_SG_SLI4_BUF_SZ)
8195 phba->cfg_sg_dma_buf_size = LPFC_MIN_SG_SLI4_BUF_SZ;
8196 else
8197 phba->cfg_sg_dma_buf_size =
8198 SLI4_PAGE_ALIGN(phba->cfg_sg_dma_buf_size);
8199
8200 phba->border_sge_num = phba->cfg_sg_dma_buf_size /
8201 sizeof(struct sli4_sge);
8202
8203 /* Limit to LPFC_MAX_NVME_SEG_CNT for NVME. */
8204 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
8205 if (phba->cfg_sg_seg_cnt > LPFC_MAX_NVME_SEG_CNT) {
8206 lpfc_printf_log(phba, KERN_INFO, LOG_NVME | LOG_INIT,
8207 "6300 Reducing NVME sg segment "
8208 "cnt to %d\n",
8209 LPFC_MAX_NVME_SEG_CNT);
8210 phba->cfg_nvme_seg_cnt = LPFC_MAX_NVME_SEG_CNT;
8211 } else
8212 phba->cfg_nvme_seg_cnt = phba->cfg_sg_seg_cnt;
8213 }
8214
James Smartd79c9e92019-08-14 16:57:09 -07008215 lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_FCP,
8216 "9087 sg_seg_cnt:%d dmabuf_size:%d "
8217 "total:%d scsi:%d nvme:%d\n",
8218 phba->cfg_sg_seg_cnt, phba->cfg_sg_dma_buf_size,
8219 phba->cfg_total_seg_cnt, phba->cfg_scsi_seg_cnt,
8220 phba->cfg_nvme_seg_cnt);
8221
8222 if (phba->cfg_sg_dma_buf_size < SLI4_PAGE_SIZE)
8223 i = phba->cfg_sg_dma_buf_size;
8224 else
8225 i = SLI4_PAGE_SIZE;
8226
8227 phba->lpfc_sg_dma_buf_pool =
8228 dma_pool_create("lpfc_sg_dma_buf_pool",
8229 &phba->pcidev->dev,
8230 phba->cfg_sg_dma_buf_size,
8231 i, 0);
8232 if (!phba->lpfc_sg_dma_buf_pool)
8233 goto out_free_bsmbx;
8234
8235 phba->lpfc_cmd_rsp_buf_pool =
8236 dma_pool_create("lpfc_cmd_rsp_buf_pool",
8237 &phba->pcidev->dev,
8238 sizeof(struct fcp_cmnd) +
8239 sizeof(struct fcp_rsp),
8240 i, 0);
8241 if (!phba->lpfc_cmd_rsp_buf_pool)
8242 goto out_free_sg_dma_buf;
8243
James Smartcb5172e2010-03-15 11:25:07 -04008244 mempool_free(mboxq, phba->mbox_mem_pool);
James Smart1ba981f2014-02-20 09:56:45 -05008245
8246 /* Verify OAS is supported */
8247 lpfc_sli4_oas_verify(phba);
James Smart1ba981f2014-02-20 09:56:45 -05008248
James Smartd2cc9bc2018-09-10 10:30:50 -07008249 /* Verify RAS support on adapter */
8250 lpfc_sli4_ras_init(phba);
8251
James Smart5350d872011-10-10 21:33:49 -04008252 /* Verify all the SLI4 queues */
8253 rc = lpfc_sli4_queue_verify(phba);
James Smartda0436e2009-05-22 14:51:39 -04008254 if (rc)
James Smartd79c9e92019-08-14 16:57:09 -07008255 goto out_free_cmd_rsp_buf;
James Smartda0436e2009-05-22 14:51:39 -04008256
8257 /* Create driver internal CQE event pool */
8258 rc = lpfc_sli4_cq_event_pool_create(phba);
8259 if (rc)
James Smartd79c9e92019-08-14 16:57:09 -07008260 goto out_free_cmd_rsp_buf;
James Smartda0436e2009-05-22 14:51:39 -04008261
James Smart8a9d2e82012-05-09 21:16:12 -04008262 /* Initialize sgl lists per host */
8263 lpfc_init_sgl_list(phba);
8264
8265 /* Allocate and initialize active sgl array */
James Smartda0436e2009-05-22 14:51:39 -04008266 rc = lpfc_init_active_sgl_array(phba);
8267 if (rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -07008268 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartda0436e2009-05-22 14:51:39 -04008269 "1430 Failed to initialize sgl list.\n");
James Smart8a9d2e82012-05-09 21:16:12 -04008270 goto out_destroy_cq_event_pool;
James Smartda0436e2009-05-22 14:51:39 -04008271 }
James Smartda0436e2009-05-22 14:51:39 -04008272 rc = lpfc_sli4_init_rpi_hdrs(phba);
8273 if (rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -07008274 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartda0436e2009-05-22 14:51:39 -04008275 "1432 Failed to initialize rpi headers.\n");
8276 goto out_free_active_sgl;
8277 }
8278
James Smarta93ff372010-10-22 11:06:08 -04008279 /* Allocate eligible FCF bmask memory for FCF roundrobin failover */
James Smart0c9ab6f2010-02-26 14:15:57 -05008280 longs = (LPFC_SLI4_FCF_TBL_INDX_MAX + BITS_PER_LONG - 1)/BITS_PER_LONG;
Kees Cook6396bb22018-06-12 14:03:40 -07008281 phba->fcf.fcf_rr_bmask = kcalloc(longs, sizeof(unsigned long),
James Smart0c9ab6f2010-02-26 14:15:57 -05008282 GFP_KERNEL);
8283 if (!phba->fcf.fcf_rr_bmask) {
Dick Kennedy372c1872020-06-30 14:50:00 -07008284 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart0c9ab6f2010-02-26 14:15:57 -05008285 "2759 Failed allocate memory for FCF round "
8286 "robin failover bmask\n");
James Smart05580562011-05-24 11:40:48 -04008287 rc = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -05008288 goto out_remove_rpi_hdrs;
8289 }
8290
James Smart6a828b02019-01-28 11:14:31 -08008291 phba->sli4_hba.hba_eq_hdl = kcalloc(phba->cfg_irq_chann,
James Smartcdb42be2019-01-28 11:14:21 -08008292 sizeof(struct lpfc_hba_eq_hdl),
8293 GFP_KERNEL);
James Smart895427b2017-02-12 13:52:30 -08008294 if (!phba->sli4_hba.hba_eq_hdl) {
Dick Kennedy372c1872020-06-30 14:50:00 -07008295 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart67d12732012-08-03 12:36:13 -04008296 "2572 Failed allocate memory for "
8297 "fast-path per-EQ handle array\n");
8298 rc = -ENOMEM;
8299 goto out_free_fcf_rr_bmask;
James Smartda0436e2009-05-22 14:51:39 -04008300 }
8301
James Smart222e9232019-01-28 11:14:35 -08008302 phba->sli4_hba.cpu_map = kcalloc(phba->sli4_hba.num_possible_cpu,
James Smart895427b2017-02-12 13:52:30 -08008303 sizeof(struct lpfc_vector_map_info),
8304 GFP_KERNEL);
James Smart7bb03bb2013-04-17 20:19:16 -04008305 if (!phba->sli4_hba.cpu_map) {
Dick Kennedy372c1872020-06-30 14:50:00 -07008306 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart7bb03bb2013-04-17 20:19:16 -04008307 "3327 Failed allocate memory for msi-x "
8308 "interrupt vector mapping\n");
8309 rc = -ENOMEM;
James Smart895427b2017-02-12 13:52:30 -08008310 goto out_free_hba_eq_hdl;
James Smart7bb03bb2013-04-17 20:19:16 -04008311 }
James Smartb246de12013-05-31 17:03:07 -04008312
James Smart32517fc2019-01-28 11:14:33 -08008313 phba->sli4_hba.eq_info = alloc_percpu(struct lpfc_eq_intr_info);
8314 if (!phba->sli4_hba.eq_info) {
Dick Kennedy372c1872020-06-30 14:50:00 -07008315 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart32517fc2019-01-28 11:14:33 -08008316 "3321 Failed allocation for per_cpu stats\n");
8317 rc = -ENOMEM;
8318 goto out_free_hba_cpu_map;
8319 }
James Smart840eda92020-03-22 11:13:00 -07008320
Dick Kennedy317aeb82020-06-30 14:49:59 -07008321 phba->sli4_hba.idle_stat = kcalloc(phba->sli4_hba.num_possible_cpu,
8322 sizeof(*phba->sli4_hba.idle_stat),
8323 GFP_KERNEL);
8324 if (!phba->sli4_hba.idle_stat) {
Dick Kennedy372c1872020-06-30 14:50:00 -07008325 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
Dick Kennedy317aeb82020-06-30 14:49:59 -07008326 "3390 Failed allocation for idle_stat\n");
8327 rc = -ENOMEM;
8328 goto out_free_hba_eq_info;
8329 }
8330
James Smart840eda92020-03-22 11:13:00 -07008331#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
8332 phba->sli4_hba.c_stat = alloc_percpu(struct lpfc_hdwq_stat);
8333 if (!phba->sli4_hba.c_stat) {
Dick Kennedy372c1872020-06-30 14:50:00 -07008334 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart840eda92020-03-22 11:13:00 -07008335 "3332 Failed allocating per cpu hdwq stats\n");
8336 rc = -ENOMEM;
Dick Kennedy317aeb82020-06-30 14:49:59 -07008337 goto out_free_hba_idle_stat;
James Smart840eda92020-03-22 11:13:00 -07008338 }
8339#endif
8340
James Smart02243832021-08-16 09:28:54 -07008341 phba->cmf_stat = alloc_percpu(struct lpfc_cgn_stat);
8342 if (!phba->cmf_stat) {
8343 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8344 "3331 Failed allocating per cpu cgn stats\n");
8345 rc = -ENOMEM;
8346 goto out_free_hba_hdwq_info;
8347 }
8348
James Smart912e3ac2011-05-24 11:42:11 -04008349 /*
8350 * Enable sr-iov virtual functions if supported and configured
8351 * through the module parameter.
8352 */
8353 if (phba->cfg_sriov_nr_virtfn > 0) {
8354 rc = lpfc_sli_probe_sriov_nr_virtfn(phba,
8355 phba->cfg_sriov_nr_virtfn);
8356 if (rc) {
8357 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
8358 "3020 Requested number of SR-IOV "
8359 "virtual functions (%d) is not "
8360 "supported\n",
8361 phba->cfg_sriov_nr_virtfn);
8362 phba->cfg_sriov_nr_virtfn = 0;
8363 }
8364 }
8365
James Smart5248a742011-07-22 18:37:06 -04008366 return 0;
James Smartda0436e2009-05-22 14:51:39 -04008367
James Smart02243832021-08-16 09:28:54 -07008368out_free_hba_hdwq_info:
James Smart840eda92020-03-22 11:13:00 -07008369#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart37e38402021-09-07 22:09:27 -07008370 free_percpu(phba->sli4_hba.c_stat);
Dick Kennedy317aeb82020-06-30 14:49:59 -07008371out_free_hba_idle_stat:
Dick Kennedy317aeb82020-06-30 14:49:59 -07008372#endif
James Smart37e38402021-09-07 22:09:27 -07008373 kfree(phba->sli4_hba.idle_stat);
James Smart840eda92020-03-22 11:13:00 -07008374out_free_hba_eq_info:
8375 free_percpu(phba->sli4_hba.eq_info);
James Smart32517fc2019-01-28 11:14:33 -08008376out_free_hba_cpu_map:
8377 kfree(phba->sli4_hba.cpu_map);
James Smart895427b2017-02-12 13:52:30 -08008378out_free_hba_eq_hdl:
8379 kfree(phba->sli4_hba.hba_eq_hdl);
James Smart0c9ab6f2010-02-26 14:15:57 -05008380out_free_fcf_rr_bmask:
8381 kfree(phba->fcf.fcf_rr_bmask);
James Smartda0436e2009-05-22 14:51:39 -04008382out_remove_rpi_hdrs:
8383 lpfc_sli4_remove_rpi_hdrs(phba);
8384out_free_active_sgl:
8385 lpfc_free_active_sgl(phba);
James Smartda0436e2009-05-22 14:51:39 -04008386out_destroy_cq_event_pool:
8387 lpfc_sli4_cq_event_pool_destroy(phba);
James Smartd79c9e92019-08-14 16:57:09 -07008388out_free_cmd_rsp_buf:
8389 dma_pool_destroy(phba->lpfc_cmd_rsp_buf_pool);
8390 phba->lpfc_cmd_rsp_buf_pool = NULL;
8391out_free_sg_dma_buf:
8392 dma_pool_destroy(phba->lpfc_sg_dma_buf_pool);
8393 phba->lpfc_sg_dma_buf_pool = NULL;
James Smartda0436e2009-05-22 14:51:39 -04008394out_free_bsmbx:
8395 lpfc_destroy_bootstrap_mbox(phba);
8396out_free_mem:
8397 lpfc_mem_free(phba);
8398 return rc;
8399}
8400
8401/**
8402 * lpfc_sli4_driver_resource_unset - Unset drvr internal resources for SLI4 dev
8403 * @phba: pointer to lpfc hba data structure.
8404 *
8405 * This routine is invoked to unset the driver internal resources set up
8406 * specific for supporting the SLI-4 HBA device it attached to.
8407 **/
8408static void
8409lpfc_sli4_driver_resource_unset(struct lpfc_hba *phba)
8410{
8411 struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
8412
James Smart32517fc2019-01-28 11:14:33 -08008413 free_percpu(phba->sli4_hba.eq_info);
James Smart840eda92020-03-22 11:13:00 -07008414#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
8415 free_percpu(phba->sli4_hba.c_stat);
8416#endif
James Smart02243832021-08-16 09:28:54 -07008417 free_percpu(phba->cmf_stat);
Dick Kennedy317aeb82020-06-30 14:49:59 -07008418 kfree(phba->sli4_hba.idle_stat);
James Smart32517fc2019-01-28 11:14:33 -08008419
James Smart7bb03bb2013-04-17 20:19:16 -04008420 /* Free memory allocated for msi-x interrupt vector to CPU mapping */
8421 kfree(phba->sli4_hba.cpu_map);
James Smart222e9232019-01-28 11:14:35 -08008422 phba->sli4_hba.num_possible_cpu = 0;
James Smart7bb03bb2013-04-17 20:19:16 -04008423 phba->sli4_hba.num_present_cpu = 0;
James Smart76fd07a2014-02-20 09:57:18 -05008424 phba->sli4_hba.curr_disp_cpu = 0;
Dick Kennedy3048e3e2020-05-01 14:43:06 -07008425 cpumask_clear(&phba->sli4_hba.irq_aff_mask);
James Smart7bb03bb2013-04-17 20:19:16 -04008426
James Smartda0436e2009-05-22 14:51:39 -04008427 /* Free memory allocated for fast-path work queue handles */
James Smart895427b2017-02-12 13:52:30 -08008428 kfree(phba->sli4_hba.hba_eq_hdl);
James Smartda0436e2009-05-22 14:51:39 -04008429
8430 /* Free the allocated rpi headers. */
8431 lpfc_sli4_remove_rpi_hdrs(phba);
James Smartd11e31d2009-06-10 17:23:06 -04008432 lpfc_sli4_remove_rpis(phba);
James Smartda0436e2009-05-22 14:51:39 -04008433
James Smart0c9ab6f2010-02-26 14:15:57 -05008434 /* Free eligible FCF index bmask */
8435 kfree(phba->fcf.fcf_rr_bmask);
8436
James Smartda0436e2009-05-22 14:51:39 -04008437 /* Free the ELS sgl list */
8438 lpfc_free_active_sgl(phba);
James Smart8a9d2e82012-05-09 21:16:12 -04008439 lpfc_free_els_sgl_list(phba);
James Smartf358dd02017-02-12 13:52:34 -08008440 lpfc_free_nvmet_sgl_list(phba);
James Smartda0436e2009-05-22 14:51:39 -04008441
James Smartda0436e2009-05-22 14:51:39 -04008442 /* Free the completion queue EQ event pool */
8443 lpfc_sli4_cq_event_release_all(phba);
8444 lpfc_sli4_cq_event_pool_destroy(phba);
8445
James Smart6d368e52011-05-24 11:44:12 -04008446 /* Release resource identifiers. */
8447 lpfc_sli4_dealloc_resource_identifiers(phba);
8448
James Smartda0436e2009-05-22 14:51:39 -04008449 /* Free the bsmbx region. */
8450 lpfc_destroy_bootstrap_mbox(phba);
8451
8452 /* Free the SLI Layer memory with SLI4 HBAs */
8453 lpfc_mem_free_all(phba);
8454
8455 /* Free the current connect table */
8456 list_for_each_entry_safe(conn_entry, next_conn_entry,
James Smart4d9ab992009-10-02 15:16:39 -04008457 &phba->fcf_conn_rec_list, list) {
8458 list_del_init(&conn_entry->list);
James Smartda0436e2009-05-22 14:51:39 -04008459 kfree(conn_entry);
James Smart4d9ab992009-10-02 15:16:39 -04008460 }
James Smartda0436e2009-05-22 14:51:39 -04008461
8462 return;
8463}
8464
8465/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008466 * lpfc_init_api_table_setup - Set up init api function jump table
James Smart3772a992009-05-22 14:50:54 -04008467 * @phba: The hba struct for which this call is being executed.
8468 * @dev_grp: The HBA PCI-Device group number.
8469 *
8470 * This routine sets up the device INIT interface API function jump table
8471 * in @phba struct.
8472 *
8473 * Returns: 0 - success, -ENODEV - failure.
8474 **/
8475int
8476lpfc_init_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
8477{
James Smart84d1b002010-02-12 14:42:33 -05008478 phba->lpfc_hba_init_link = lpfc_hba_init_link;
8479 phba->lpfc_hba_down_link = lpfc_hba_down_link;
James Smart7f860592011-03-11 16:05:52 -05008480 phba->lpfc_selective_reset = lpfc_selective_reset;
James Smart3772a992009-05-22 14:50:54 -04008481 switch (dev_grp) {
8482 case LPFC_PCI_DEV_LP:
8483 phba->lpfc_hba_down_post = lpfc_hba_down_post_s3;
8484 phba->lpfc_handle_eratt = lpfc_handle_eratt_s3;
8485 phba->lpfc_stop_port = lpfc_stop_port_s3;
8486 break;
James Smartda0436e2009-05-22 14:51:39 -04008487 case LPFC_PCI_DEV_OC:
8488 phba->lpfc_hba_down_post = lpfc_hba_down_post_s4;
8489 phba->lpfc_handle_eratt = lpfc_handle_eratt_s4;
8490 phba->lpfc_stop_port = lpfc_stop_port_s4;
8491 break;
James Smart3772a992009-05-22 14:50:54 -04008492 default:
James Smarta5160742021-10-20 14:14:10 -07008493 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart3772a992009-05-22 14:50:54 -04008494 "1431 Invalid HBA PCI-device group: 0x%x\n",
8495 dev_grp);
8496 return -ENODEV;
James Smart3772a992009-05-22 14:50:54 -04008497 }
8498 return 0;
8499}
8500
8501/**
James Smart3772a992009-05-22 14:50:54 -04008502 * lpfc_setup_driver_resource_phase2 - Phase2 setup driver internal resources.
8503 * @phba: pointer to lpfc hba data structure.
8504 *
8505 * This routine is invoked to set up the driver internal resources after the
8506 * device specific resource setup to support the HBA device it attached to.
8507 *
8508 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008509 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04008510 * other values - error
8511 **/
8512static int
8513lpfc_setup_driver_resource_phase2(struct lpfc_hba *phba)
8514{
8515 int error;
8516
8517 /* Startup the kernel thread for this host adapter. */
8518 phba->worker_thread = kthread_run(lpfc_do_work, phba,
8519 "lpfc_worker_%d", phba->brd_no);
8520 if (IS_ERR(phba->worker_thread)) {
8521 error = PTR_ERR(phba->worker_thread);
8522 return error;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05008523 }
8524
James Smart3772a992009-05-22 14:50:54 -04008525 return 0;
8526}
8527
8528/**
8529 * lpfc_unset_driver_resource_phase2 - Phase2 unset driver internal resources.
8530 * @phba: pointer to lpfc hba data structure.
8531 *
8532 * This routine is invoked to unset the driver internal resources set up after
8533 * the device specific resource setup for supporting the HBA device it
8534 * attached to.
8535 **/
8536static void
8537lpfc_unset_driver_resource_phase2(struct lpfc_hba *phba)
8538{
Dick Kennedyf485c182017-09-29 17:34:34 -07008539 if (phba->wq) {
8540 flush_workqueue(phba->wq);
8541 destroy_workqueue(phba->wq);
8542 phba->wq = NULL;
8543 }
8544
James Smart3772a992009-05-22 14:50:54 -04008545 /* Stop kernel worker thread */
James Smart0cdb84e2018-04-09 14:24:26 -07008546 if (phba->worker_thread)
8547 kthread_stop(phba->worker_thread);
James Smart3772a992009-05-22 14:50:54 -04008548}
8549
8550/**
8551 * lpfc_free_iocb_list - Free iocb list.
8552 * @phba: pointer to lpfc hba data structure.
8553 *
8554 * This routine is invoked to free the driver's IOCB list and memory.
8555 **/
James Smart6c621a22017-05-15 15:20:45 -07008556void
James Smart3772a992009-05-22 14:50:54 -04008557lpfc_free_iocb_list(struct lpfc_hba *phba)
8558{
8559 struct lpfc_iocbq *iocbq_entry = NULL, *iocbq_next = NULL;
8560
8561 spin_lock_irq(&phba->hbalock);
8562 list_for_each_entry_safe(iocbq_entry, iocbq_next,
8563 &phba->lpfc_iocb_list, list) {
8564 list_del(&iocbq_entry->list);
8565 kfree(iocbq_entry);
8566 phba->total_iocbq_bufs--;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05008567 }
James Smart3772a992009-05-22 14:50:54 -04008568 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05008569
James Smart3772a992009-05-22 14:50:54 -04008570 return;
8571}
dea31012005-04-17 16:05:31 -05008572
James Smart3772a992009-05-22 14:50:54 -04008573/**
8574 * lpfc_init_iocb_list - Allocate and initialize iocb list.
8575 * @phba: pointer to lpfc hba data structure.
Lee Jonesfe614ac2020-07-23 13:24:22 +01008576 * @iocb_count: number of requested iocbs
James Smart3772a992009-05-22 14:50:54 -04008577 *
8578 * This routine is invoked to allocate and initizlize the driver's IOCB
8579 * list and set up the IOCB tag array accordingly.
8580 *
8581 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008582 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04008583 * other values - error
8584 **/
James Smart6c621a22017-05-15 15:20:45 -07008585int
James Smart3772a992009-05-22 14:50:54 -04008586lpfc_init_iocb_list(struct lpfc_hba *phba, int iocb_count)
8587{
8588 struct lpfc_iocbq *iocbq_entry = NULL;
8589 uint16_t iotag;
8590 int i;
dea31012005-04-17 16:05:31 -05008591
8592 /* Initialize and populate the iocb list per host. */
8593 INIT_LIST_HEAD(&phba->lpfc_iocb_list);
James Smart3772a992009-05-22 14:50:54 -04008594 for (i = 0; i < iocb_count; i++) {
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07008595 iocbq_entry = kzalloc(sizeof(struct lpfc_iocbq), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05008596 if (iocbq_entry == NULL) {
8597 printk(KERN_ERR "%s: only allocated %d iocbs of "
8598 "expected %d count. Unloading driver.\n",
James Smarta5f73372019-09-21 20:58:50 -07008599 __func__, i, iocb_count);
dea31012005-04-17 16:05:31 -05008600 goto out_free_iocbq;
8601 }
8602
James Bottomley604a3e32005-10-29 10:28:33 -05008603 iotag = lpfc_sli_next_iotag(phba, iocbq_entry);
8604 if (iotag == 0) {
James Smart3772a992009-05-22 14:50:54 -04008605 kfree(iocbq_entry);
James Bottomley604a3e32005-10-29 10:28:33 -05008606 printk(KERN_ERR "%s: failed to allocate IOTAG. "
James Smart3772a992009-05-22 14:50:54 -04008607 "Unloading driver.\n", __func__);
James Bottomley604a3e32005-10-29 10:28:33 -05008608 goto out_free_iocbq;
8609 }
James Smart6d368e52011-05-24 11:44:12 -04008610 iocbq_entry->sli4_lxritag = NO_XRI;
James Smart3772a992009-05-22 14:50:54 -04008611 iocbq_entry->sli4_xritag = NO_XRI;
James Smart2e0fef82007-06-17 19:56:36 -05008612
8613 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05008614 list_add(&iocbq_entry->list, &phba->lpfc_iocb_list);
8615 phba->total_iocbq_bufs++;
James Smart2e0fef82007-06-17 19:56:36 -05008616 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05008617 }
8618
James Smart3772a992009-05-22 14:50:54 -04008619 return 0;
8620
8621out_free_iocbq:
8622 lpfc_free_iocb_list(phba);
8623
8624 return -ENOMEM;
8625}
8626
8627/**
James Smart8a9d2e82012-05-09 21:16:12 -04008628 * lpfc_free_sgl_list - Free a given sgl list.
James Smartda0436e2009-05-22 14:51:39 -04008629 * @phba: pointer to lpfc hba data structure.
James Smart8a9d2e82012-05-09 21:16:12 -04008630 * @sglq_list: pointer to the head of sgl list.
James Smartda0436e2009-05-22 14:51:39 -04008631 *
James Smart8a9d2e82012-05-09 21:16:12 -04008632 * This routine is invoked to free a give sgl list and memory.
James Smartda0436e2009-05-22 14:51:39 -04008633 **/
James Smart8a9d2e82012-05-09 21:16:12 -04008634void
8635lpfc_free_sgl_list(struct lpfc_hba *phba, struct list_head *sglq_list)
James Smartda0436e2009-05-22 14:51:39 -04008636{
8637 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
James Smart8a9d2e82012-05-09 21:16:12 -04008638
8639 list_for_each_entry_safe(sglq_entry, sglq_next, sglq_list, list) {
8640 list_del(&sglq_entry->list);
8641 lpfc_mbuf_free(phba, sglq_entry->virt, sglq_entry->phys);
8642 kfree(sglq_entry);
8643 }
8644}
8645
8646/**
8647 * lpfc_free_els_sgl_list - Free els sgl list.
8648 * @phba: pointer to lpfc hba data structure.
8649 *
8650 * This routine is invoked to free the driver's els sgl list and memory.
8651 **/
8652static void
8653lpfc_free_els_sgl_list(struct lpfc_hba *phba)
8654{
James Smartda0436e2009-05-22 14:51:39 -04008655 LIST_HEAD(sglq_list);
James Smartda0436e2009-05-22 14:51:39 -04008656
James Smart8a9d2e82012-05-09 21:16:12 -04008657 /* Retrieve all els sgls from driver list */
James Smarta7892412021-04-11 18:31:15 -07008658 spin_lock_irq(&phba->sli4_hba.sgl_list_lock);
James Smart895427b2017-02-12 13:52:30 -08008659 list_splice_init(&phba->sli4_hba.lpfc_els_sgl_list, &sglq_list);
James Smarta7892412021-04-11 18:31:15 -07008660 spin_unlock_irq(&phba->sli4_hba.sgl_list_lock);
James Smartda0436e2009-05-22 14:51:39 -04008661
James Smart8a9d2e82012-05-09 21:16:12 -04008662 /* Now free the sgl list */
8663 lpfc_free_sgl_list(phba, &sglq_list);
James Smartda0436e2009-05-22 14:51:39 -04008664}
8665
8666/**
James Smartf358dd02017-02-12 13:52:34 -08008667 * lpfc_free_nvmet_sgl_list - Free nvmet sgl list.
8668 * @phba: pointer to lpfc hba data structure.
8669 *
8670 * This routine is invoked to free the driver's nvmet sgl list and memory.
8671 **/
8672static void
8673lpfc_free_nvmet_sgl_list(struct lpfc_hba *phba)
8674{
8675 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
8676 LIST_HEAD(sglq_list);
8677
8678 /* Retrieve all nvmet sgls from driver list */
8679 spin_lock_irq(&phba->hbalock);
8680 spin_lock(&phba->sli4_hba.sgl_list_lock);
8681 list_splice_init(&phba->sli4_hba.lpfc_nvmet_sgl_list, &sglq_list);
8682 spin_unlock(&phba->sli4_hba.sgl_list_lock);
8683 spin_unlock_irq(&phba->hbalock);
8684
8685 /* Now free the sgl list */
8686 list_for_each_entry_safe(sglq_entry, sglq_next, &sglq_list, list) {
8687 list_del(&sglq_entry->list);
8688 lpfc_nvmet_buf_free(phba, sglq_entry->virt, sglq_entry->phys);
8689 kfree(sglq_entry);
8690 }
Dick Kennedy4b40d022017-08-23 16:55:38 -07008691
8692 /* Update the nvmet_xri_cnt to reflect no current sgls.
8693 * The next initialization cycle sets the count and allocates
8694 * the sgls over again.
8695 */
8696 phba->sli4_hba.nvmet_xri_cnt = 0;
James Smartf358dd02017-02-12 13:52:34 -08008697}
8698
8699/**
James Smartda0436e2009-05-22 14:51:39 -04008700 * lpfc_init_active_sgl_array - Allocate the buf to track active ELS XRIs.
8701 * @phba: pointer to lpfc hba data structure.
8702 *
8703 * This routine is invoked to allocate the driver's active sgl memory.
8704 * This array will hold the sglq_entry's for active IOs.
8705 **/
8706static int
8707lpfc_init_active_sgl_array(struct lpfc_hba *phba)
8708{
8709 int size;
8710 size = sizeof(struct lpfc_sglq *);
8711 size *= phba->sli4_hba.max_cfg_param.max_xri;
8712
8713 phba->sli4_hba.lpfc_sglq_active_list =
8714 kzalloc(size, GFP_KERNEL);
8715 if (!phba->sli4_hba.lpfc_sglq_active_list)
8716 return -ENOMEM;
8717 return 0;
8718}
8719
8720/**
8721 * lpfc_free_active_sgl - Free the buf that tracks active ELS XRIs.
8722 * @phba: pointer to lpfc hba data structure.
8723 *
8724 * This routine is invoked to walk through the array of active sglq entries
8725 * and free all of the resources.
8726 * This is just a place holder for now.
8727 **/
8728static void
8729lpfc_free_active_sgl(struct lpfc_hba *phba)
8730{
8731 kfree(phba->sli4_hba.lpfc_sglq_active_list);
8732}
8733
8734/**
8735 * lpfc_init_sgl_list - Allocate and initialize sgl list.
8736 * @phba: pointer to lpfc hba data structure.
8737 *
8738 * This routine is invoked to allocate and initizlize the driver's sgl
8739 * list and set up the sgl xritag tag array accordingly.
8740 *
James Smartda0436e2009-05-22 14:51:39 -04008741 **/
James Smart8a9d2e82012-05-09 21:16:12 -04008742static void
James Smartda0436e2009-05-22 14:51:39 -04008743lpfc_init_sgl_list(struct lpfc_hba *phba)
8744{
James Smartda0436e2009-05-22 14:51:39 -04008745 /* Initialize and populate the sglq list per host/VF. */
James Smart895427b2017-02-12 13:52:30 -08008746 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_els_sgl_list);
James Smartda0436e2009-05-22 14:51:39 -04008747 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_els_sgl_list);
James Smartf358dd02017-02-12 13:52:34 -08008748 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_nvmet_sgl_list);
James Smart86c67372017-04-21 16:05:04 -07008749 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_nvmet_ctx_list);
James Smartda0436e2009-05-22 14:51:39 -04008750
James Smart8a9d2e82012-05-09 21:16:12 -04008751 /* els xri-sgl book keeping */
8752 phba->sli4_hba.els_xri_cnt = 0;
James Smartda0436e2009-05-22 14:51:39 -04008753
James Smart895427b2017-02-12 13:52:30 -08008754 /* nvme xri-buffer book keeping */
James Smart5e5b5112019-01-28 11:14:22 -08008755 phba->sli4_hba.io_xri_cnt = 0;
James Smartda0436e2009-05-22 14:51:39 -04008756}
8757
8758/**
8759 * lpfc_sli4_init_rpi_hdrs - Post the rpi header memory region to the port
8760 * @phba: pointer to lpfc hba data structure.
8761 *
8762 * This routine is invoked to post rpi header templates to the
James Smart88a2cfb2011-07-22 18:36:33 -04008763 * port for those SLI4 ports that do not support extents. This routine
James Smartda0436e2009-05-22 14:51:39 -04008764 * posts a PAGE_SIZE memory region to the port to hold up to
James Smart88a2cfb2011-07-22 18:36:33 -04008765 * PAGE_SIZE modulo 64 rpi context headers. This is an initialization routine
8766 * and should be called only when interrupts are disabled.
James Smartda0436e2009-05-22 14:51:39 -04008767 *
8768 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008769 * 0 - successful
James Smart88a2cfb2011-07-22 18:36:33 -04008770 * -ERROR - otherwise.
James Smartda0436e2009-05-22 14:51:39 -04008771 **/
8772int
8773lpfc_sli4_init_rpi_hdrs(struct lpfc_hba *phba)
8774{
8775 int rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04008776 struct lpfc_rpi_hdr *rpi_hdr;
8777
8778 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_hdr_list);
James Smartff78d8f2011-12-13 13:21:35 -05008779 if (!phba->sli4_hba.rpi_hdrs_in_use)
James Smart6d368e52011-05-24 11:44:12 -04008780 return rc;
James Smart6d368e52011-05-24 11:44:12 -04008781 if (phba->sli4_hba.extents_in_use)
8782 return -EIO;
James Smartda0436e2009-05-22 14:51:39 -04008783
8784 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
8785 if (!rpi_hdr) {
Dick Kennedy372c1872020-06-30 14:50:00 -07008786 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartda0436e2009-05-22 14:51:39 -04008787 "0391 Error during rpi post operation\n");
8788 lpfc_sli4_remove_rpis(phba);
8789 rc = -ENODEV;
8790 }
8791
8792 return rc;
8793}
8794
8795/**
8796 * lpfc_sli4_create_rpi_hdr - Allocate an rpi header memory region
8797 * @phba: pointer to lpfc hba data structure.
8798 *
8799 * This routine is invoked to allocate a single 4KB memory region to
8800 * support rpis and stores them in the phba. This single region
8801 * provides support for up to 64 rpis. The region is used globally
8802 * by the device.
8803 *
8804 * Returns:
8805 * A valid rpi hdr on success.
8806 * A NULL pointer on any failure.
8807 **/
8808struct lpfc_rpi_hdr *
8809lpfc_sli4_create_rpi_hdr(struct lpfc_hba *phba)
8810{
8811 uint16_t rpi_limit, curr_rpi_range;
8812 struct lpfc_dmabuf *dmabuf;
8813 struct lpfc_rpi_hdr *rpi_hdr;
8814
James Smart6d368e52011-05-24 11:44:12 -04008815 /*
8816 * If the SLI4 port supports extents, posting the rpi header isn't
8817 * required. Set the expected maximum count and let the actual value
8818 * get set when extents are fully allocated.
8819 */
8820 if (!phba->sli4_hba.rpi_hdrs_in_use)
8821 return NULL;
8822 if (phba->sli4_hba.extents_in_use)
8823 return NULL;
8824
8825 /* The limit on the logical index is just the max_rpi count. */
James Smart845d9e82017-05-15 15:20:38 -07008826 rpi_limit = phba->sli4_hba.max_cfg_param.max_rpi;
James Smartda0436e2009-05-22 14:51:39 -04008827
8828 spin_lock_irq(&phba->hbalock);
James Smart6d368e52011-05-24 11:44:12 -04008829 /*
8830 * Establish the starting RPI in this header block. The starting
8831 * rpi is normalized to a zero base because the physical rpi is
8832 * port based.
8833 */
James Smart97f2ecf2012-03-01 22:35:23 -05008834 curr_rpi_range = phba->sli4_hba.next_rpi;
James Smartda0436e2009-05-22 14:51:39 -04008835 spin_unlock_irq(&phba->hbalock);
8836
James Smart845d9e82017-05-15 15:20:38 -07008837 /* Reached full RPI range */
8838 if (curr_rpi_range == rpi_limit)
James Smart6d368e52011-05-24 11:44:12 -04008839 return NULL;
James Smart845d9e82017-05-15 15:20:38 -07008840
James Smartda0436e2009-05-22 14:51:39 -04008841 /*
8842 * First allocate the protocol header region for the port. The
8843 * port expects a 4KB DMA-mapped memory region that is 4K aligned.
8844 */
8845 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
8846 if (!dmabuf)
8847 return NULL;
8848
Luis Chamberlain750afb02019-01-04 09:23:09 +01008849 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
8850 LPFC_HDR_TEMPLATE_SIZE,
8851 &dmabuf->phys, GFP_KERNEL);
James Smartda0436e2009-05-22 14:51:39 -04008852 if (!dmabuf->virt) {
8853 rpi_hdr = NULL;
8854 goto err_free_dmabuf;
8855 }
8856
James Smartda0436e2009-05-22 14:51:39 -04008857 if (!IS_ALIGNED(dmabuf->phys, LPFC_HDR_TEMPLATE_SIZE)) {
8858 rpi_hdr = NULL;
8859 goto err_free_coherent;
8860 }
8861
8862 /* Save the rpi header data for cleanup later. */
8863 rpi_hdr = kzalloc(sizeof(struct lpfc_rpi_hdr), GFP_KERNEL);
8864 if (!rpi_hdr)
8865 goto err_free_coherent;
8866
8867 rpi_hdr->dmabuf = dmabuf;
8868 rpi_hdr->len = LPFC_HDR_TEMPLATE_SIZE;
8869 rpi_hdr->page_count = 1;
8870 spin_lock_irq(&phba->hbalock);
James Smart6d368e52011-05-24 11:44:12 -04008871
8872 /* The rpi_hdr stores the logical index only. */
8873 rpi_hdr->start_rpi = curr_rpi_range;
James Smart845d9e82017-05-15 15:20:38 -07008874 rpi_hdr->next_rpi = phba->sli4_hba.next_rpi + LPFC_RPI_HDR_COUNT;
James Smartda0436e2009-05-22 14:51:39 -04008875 list_add_tail(&rpi_hdr->list, &phba->sli4_hba.lpfc_rpi_hdr_list);
8876
James Smartda0436e2009-05-22 14:51:39 -04008877 spin_unlock_irq(&phba->hbalock);
8878 return rpi_hdr;
8879
8880 err_free_coherent:
8881 dma_free_coherent(&phba->pcidev->dev, LPFC_HDR_TEMPLATE_SIZE,
8882 dmabuf->virt, dmabuf->phys);
8883 err_free_dmabuf:
8884 kfree(dmabuf);
8885 return NULL;
8886}
8887
8888/**
8889 * lpfc_sli4_remove_rpi_hdrs - Remove all rpi header memory regions
8890 * @phba: pointer to lpfc hba data structure.
8891 *
8892 * This routine is invoked to remove all memory resources allocated
James Smart6d368e52011-05-24 11:44:12 -04008893 * to support rpis for SLI4 ports not supporting extents. This routine
8894 * presumes the caller has released all rpis consumed by fabric or port
8895 * logins and is prepared to have the header pages removed.
James Smartda0436e2009-05-22 14:51:39 -04008896 **/
8897void
8898lpfc_sli4_remove_rpi_hdrs(struct lpfc_hba *phba)
8899{
8900 struct lpfc_rpi_hdr *rpi_hdr, *next_rpi_hdr;
8901
James Smart6d368e52011-05-24 11:44:12 -04008902 if (!phba->sli4_hba.rpi_hdrs_in_use)
8903 goto exit;
8904
James Smartda0436e2009-05-22 14:51:39 -04008905 list_for_each_entry_safe(rpi_hdr, next_rpi_hdr,
8906 &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
8907 list_del(&rpi_hdr->list);
8908 dma_free_coherent(&phba->pcidev->dev, rpi_hdr->len,
8909 rpi_hdr->dmabuf->virt, rpi_hdr->dmabuf->phys);
8910 kfree(rpi_hdr->dmabuf);
8911 kfree(rpi_hdr);
8912 }
James Smart6d368e52011-05-24 11:44:12 -04008913 exit:
8914 /* There are no rpis available to the port now. */
8915 phba->sli4_hba.next_rpi = 0;
James Smartda0436e2009-05-22 14:51:39 -04008916}
8917
8918/**
James Smart3772a992009-05-22 14:50:54 -04008919 * lpfc_hba_alloc - Allocate driver hba data structure for a device.
8920 * @pdev: pointer to pci device data structure.
8921 *
8922 * This routine is invoked to allocate the driver hba data structure for an
8923 * HBA device. If the allocation is successful, the phba reference to the
8924 * PCI device data structure is set.
8925 *
8926 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008927 * pointer to @phba - successful
James Smart3772a992009-05-22 14:50:54 -04008928 * NULL - error
8929 **/
8930static struct lpfc_hba *
8931lpfc_hba_alloc(struct pci_dev *pdev)
8932{
8933 struct lpfc_hba *phba;
8934
8935 /* Allocate memory for HBA structure */
8936 phba = kzalloc(sizeof(struct lpfc_hba), GFP_KERNEL);
8937 if (!phba) {
Jiri Slabye34ccdf2009-07-13 23:25:54 +02008938 dev_err(&pdev->dev, "failed to allocate hba struct\n");
James Smart3772a992009-05-22 14:50:54 -04008939 return NULL;
8940 }
8941
8942 /* Set reference to PCI device in HBA structure */
8943 phba->pcidev = pdev;
8944
8945 /* Assign an unused board number */
8946 phba->brd_no = lpfc_get_instance();
8947 if (phba->brd_no < 0) {
8948 kfree(phba);
8949 return NULL;
8950 }
James Smart65791f12016-07-06 12:35:56 -07008951 phba->eratt_poll_interval = LPFC_ERATT_POLL_INTERVAL;
James Smart3772a992009-05-22 14:50:54 -04008952
James Smart4fede782010-01-26 23:08:55 -05008953 spin_lock_init(&phba->ct_ev_lock);
James Smartf1c3b0f2009-07-19 10:01:32 -04008954 INIT_LIST_HEAD(&phba->ct_ev_waiters);
8955
James Smart3772a992009-05-22 14:50:54 -04008956 return phba;
8957}
8958
8959/**
8960 * lpfc_hba_free - Free driver hba data structure with a device.
8961 * @phba: pointer to lpfc hba data structure.
8962 *
8963 * This routine is invoked to free the driver hba data structure with an
8964 * HBA device.
8965 **/
8966static void
8967lpfc_hba_free(struct lpfc_hba *phba)
8968{
James Smart5e5b5112019-01-28 11:14:22 -08008969 if (phba->sli_rev == LPFC_SLI_REV4)
8970 kfree(phba->sli4_hba.hdwq);
8971
James Smart3772a992009-05-22 14:50:54 -04008972 /* Release the driver assigned board number */
8973 idr_remove(&lpfc_hba_index, phba->brd_no);
8974
James Smart895427b2017-02-12 13:52:30 -08008975 /* Free memory allocated with sli3 rings */
8976 kfree(phba->sli.sli3_ring);
8977 phba->sli.sli3_ring = NULL;
James Smart2a76a282012-08-03 12:35:54 -04008978
James Smart3772a992009-05-22 14:50:54 -04008979 kfree(phba);
8980 return;
8981}
8982
8983/**
8984 * lpfc_create_shost - Create hba physical port with associated scsi host.
8985 * @phba: pointer to lpfc hba data structure.
8986 *
8987 * This routine is invoked to create HBA physical port and associate a SCSI
8988 * host with it.
8989 *
8990 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008991 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04008992 * other values - error
8993 **/
8994static int
8995lpfc_create_shost(struct lpfc_hba *phba)
8996{
8997 struct lpfc_vport *vport;
8998 struct Scsi_Host *shost;
8999
9000 /* Initialize HBA FC structure */
dea31012005-04-17 16:05:31 -05009001 phba->fc_edtov = FF_DEF_EDTOV;
9002 phba->fc_ratov = FF_DEF_RATOV;
9003 phba->fc_altov = FF_DEF_ALTOV;
9004 phba->fc_arbtov = FF_DEF_ARBTOV;
9005
James Smartd7c47992010-06-08 18:31:54 -04009006 atomic_set(&phba->sdev_cnt, 0);
James Smart3de2a652007-08-02 11:09:59 -04009007 vport = lpfc_create_port(phba, phba->brd_no, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05009008 if (!vport)
James Smart3772a992009-05-22 14:50:54 -04009009 return -ENODEV;
James Smart2e0fef82007-06-17 19:56:36 -05009010
9011 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05009012 phba->pport = vport;
James Smart2ea259e2017-02-12 13:52:27 -08009013
James Smartf358dd02017-02-12 13:52:34 -08009014 if (phba->nvmet_support) {
9015 /* Only 1 vport (pport) will support NVME target */
James Smartea85a202019-10-18 14:18:25 -07009016 phba->targetport = NULL;
9017 phba->cfg_enable_fc4_type = LPFC_ENABLE_NVME;
9018 lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_NVME_DISC,
9019 "6076 NVME Target Found\n");
James Smartf358dd02017-02-12 13:52:34 -08009020 }
9021
James Smart858c9f62007-06-17 19:56:39 -05009022 lpfc_debugfs_initialize(vport);
James Smart3772a992009-05-22 14:50:54 -04009023 /* Put reference to SCSI host to driver's device private data */
9024 pci_set_drvdata(phba->pcidev, shost);
James Smart2e0fef82007-06-17 19:56:36 -05009025
James Smart4258e982015-12-16 18:11:58 -05009026 /*
9027 * At this point we are fully registered with PSA. In addition,
9028 * any initial discovery should be completed.
9029 */
9030 vport->load_flag |= FC_ALLOW_FDMI;
James Smart8663cbb2016-03-31 14:12:33 -07009031 if (phba->cfg_enable_SmartSAN ||
9032 (phba->cfg_fdmi_on == LPFC_FDMI_SUPPORT)) {
James Smart4258e982015-12-16 18:11:58 -05009033
9034 /* Setup appropriate attribute masks */
9035 vport->fdmi_hba_mask = LPFC_FDMI2_HBA_ATTR;
James Smart8663cbb2016-03-31 14:12:33 -07009036 if (phba->cfg_enable_SmartSAN)
James Smart4258e982015-12-16 18:11:58 -05009037 vport->fdmi_port_mask = LPFC_FDMI2_SMART_ATTR;
9038 else
9039 vport->fdmi_port_mask = LPFC_FDMI2_PORT_ATTR;
9040 }
James Smart3772a992009-05-22 14:50:54 -04009041 return 0;
9042}
dea31012005-04-17 16:05:31 -05009043
James Smart3772a992009-05-22 14:50:54 -04009044/**
9045 * lpfc_destroy_shost - Destroy hba physical port with associated scsi host.
9046 * @phba: pointer to lpfc hba data structure.
9047 *
9048 * This routine is invoked to destroy HBA physical port and the associated
9049 * SCSI host.
9050 **/
9051static void
9052lpfc_destroy_shost(struct lpfc_hba *phba)
9053{
9054 struct lpfc_vport *vport = phba->pport;
James Smart93996272008-08-24 21:50:30 -04009055
James Smart3772a992009-05-22 14:50:54 -04009056 /* Destroy physical port that associated with the SCSI host */
9057 destroy_port(vport);
9058
9059 return;
9060}
9061
9062/**
9063 * lpfc_setup_bg - Setup Block guard structures and debug areas.
9064 * @phba: pointer to lpfc hba data structure.
9065 * @shost: the shost to be used to detect Block guard settings.
9066 *
9067 * This routine sets up the local Block guard protocol settings for @shost.
9068 * This routine also allocates memory for debugging bg buffers.
9069 **/
9070static void
9071lpfc_setup_bg(struct lpfc_hba *phba, struct Scsi_Host *shost)
9072{
James Smartbbeb79b2012-06-12 13:54:27 -04009073 uint32_t old_mask;
9074 uint32_t old_guard;
9075
James Smartb3b98b72016-10-13 15:06:06 -07009076 if (phba->cfg_prot_mask && phba->cfg_prot_guard) {
James Smart3772a992009-05-22 14:50:54 -04009077 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9078 "1478 Registering BlockGuard with the "
9079 "SCSI layer\n");
James Smartbbeb79b2012-06-12 13:54:27 -04009080
James Smartb3b98b72016-10-13 15:06:06 -07009081 old_mask = phba->cfg_prot_mask;
9082 old_guard = phba->cfg_prot_guard;
James Smartbbeb79b2012-06-12 13:54:27 -04009083
9084 /* Only allow supported values */
James Smartb3b98b72016-10-13 15:06:06 -07009085 phba->cfg_prot_mask &= (SHOST_DIF_TYPE1_PROTECTION |
James Smartbbeb79b2012-06-12 13:54:27 -04009086 SHOST_DIX_TYPE0_PROTECTION |
9087 SHOST_DIX_TYPE1_PROTECTION);
James Smartb3b98b72016-10-13 15:06:06 -07009088 phba->cfg_prot_guard &= (SHOST_DIX_GUARD_IP |
9089 SHOST_DIX_GUARD_CRC);
James Smartbbeb79b2012-06-12 13:54:27 -04009090
9091 /* DIF Type 1 protection for profiles AST1/C1 is end to end */
James Smartb3b98b72016-10-13 15:06:06 -07009092 if (phba->cfg_prot_mask == SHOST_DIX_TYPE1_PROTECTION)
9093 phba->cfg_prot_mask |= SHOST_DIF_TYPE1_PROTECTION;
James Smartbbeb79b2012-06-12 13:54:27 -04009094
James Smartb3b98b72016-10-13 15:06:06 -07009095 if (phba->cfg_prot_mask && phba->cfg_prot_guard) {
9096 if ((old_mask != phba->cfg_prot_mask) ||
9097 (old_guard != phba->cfg_prot_guard))
Dick Kennedy372c1872020-06-30 14:50:00 -07009098 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartbbeb79b2012-06-12 13:54:27 -04009099 "1475 Registering BlockGuard with the "
9100 "SCSI layer: mask %d guard %d\n",
James Smartb3b98b72016-10-13 15:06:06 -07009101 phba->cfg_prot_mask,
9102 phba->cfg_prot_guard);
James Smartbbeb79b2012-06-12 13:54:27 -04009103
James Smartb3b98b72016-10-13 15:06:06 -07009104 scsi_host_set_prot(shost, phba->cfg_prot_mask);
9105 scsi_host_set_guard(shost, phba->cfg_prot_guard);
James Smartbbeb79b2012-06-12 13:54:27 -04009106 } else
Dick Kennedy372c1872020-06-30 14:50:00 -07009107 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartbbeb79b2012-06-12 13:54:27 -04009108 "1479 Not Registering BlockGuard with the SCSI "
9109 "layer, Bad protection parameters: %d %d\n",
9110 old_mask, old_guard);
James Smart98c9ea52007-10-27 13:37:33 -04009111 }
James Smart3772a992009-05-22 14:50:54 -04009112}
9113
9114/**
9115 * lpfc_post_init_setup - Perform necessary device post initialization setup.
9116 * @phba: pointer to lpfc hba data structure.
9117 *
9118 * This routine is invoked to perform all the necessary post initialization
9119 * setup for the device.
9120 **/
9121static void
9122lpfc_post_init_setup(struct lpfc_hba *phba)
9123{
9124 struct Scsi_Host *shost;
9125 struct lpfc_adapter_event_header adapter_event;
9126
9127 /* Get the default values for Model Name and Description */
9128 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
9129
9130 /*
9131 * hba setup may have changed the hba_queue_depth so we need to
9132 * adjust the value of can_queue.
9133 */
9134 shost = pci_get_drvdata(phba->pcidev);
9135 shost->can_queue = phba->cfg_hba_queue_depth - 10;
James Smart858c9f62007-06-17 19:56:39 -05009136
9137 lpfc_host_attrib_init(shost);
9138
James Smart2e0fef82007-06-17 19:56:36 -05009139 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
9140 spin_lock_irq(shost->host_lock);
9141 lpfc_poll_start_timer(phba);
9142 spin_unlock_irq(shost->host_lock);
9143 }
James Smart8f6d98d2006-08-01 07:34:00 -04009144
James Smart93996272008-08-24 21:50:30 -04009145 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9146 "0428 Perform SCSI scan\n");
James Smartea2151b2008-09-07 11:52:10 -04009147 /* Send board arrival event to upper layer */
9148 adapter_event.event_type = FC_REG_ADAPTER_EVENT;
9149 adapter_event.subcategory = LPFC_EVENT_ARRIVAL;
9150 fc_host_post_vendor_event(shost, fc_get_event_number(),
James Smart3772a992009-05-22 14:50:54 -04009151 sizeof(adapter_event),
9152 (char *) &adapter_event,
9153 LPFC_NL_VENDOR_ID);
9154 return;
9155}
9156
9157/**
9158 * lpfc_sli_pci_mem_setup - Setup SLI3 HBA PCI memory space.
9159 * @phba: pointer to lpfc hba data structure.
9160 *
9161 * This routine is invoked to set up the PCI device memory space for device
9162 * with SLI-3 interface spec.
9163 *
9164 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02009165 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04009166 * other values - error
9167 **/
9168static int
9169lpfc_sli_pci_mem_setup(struct lpfc_hba *phba)
9170{
Christoph Hellwigf30e1bf2018-10-18 15:10:21 +02009171 struct pci_dev *pdev = phba->pcidev;
James Smart3772a992009-05-22 14:50:54 -04009172 unsigned long bar0map_len, bar2map_len;
9173 int i, hbq_count;
9174 void *ptr;
Hannes Reinecke56de8352019-02-18 08:34:19 +01009175 int error;
James Smart3772a992009-05-22 14:50:54 -04009176
Christoph Hellwigf30e1bf2018-10-18 15:10:21 +02009177 if (!pdev)
Hannes Reinecke56de8352019-02-18 08:34:19 +01009178 return -ENODEV;
James Smart3772a992009-05-22 14:50:54 -04009179
9180 /* Set the device DMA mask size */
Hannes Reinecke56de8352019-02-18 08:34:19 +01009181 error = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
9182 if (error)
9183 error = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
9184 if (error)
Christoph Hellwigf30e1bf2018-10-18 15:10:21 +02009185 return error;
Hannes Reinecke56de8352019-02-18 08:34:19 +01009186 error = -ENODEV;
James Smart3772a992009-05-22 14:50:54 -04009187
9188 /* Get the bus address of Bar0 and Bar2 and the number of bytes
9189 * required by each mapping.
9190 */
9191 phba->pci_bar0_map = pci_resource_start(pdev, 0);
9192 bar0map_len = pci_resource_len(pdev, 0);
9193
9194 phba->pci_bar2_map = pci_resource_start(pdev, 2);
9195 bar2map_len = pci_resource_len(pdev, 2);
9196
9197 /* Map HBA SLIM to a kernel virtual address. */
9198 phba->slim_memmap_p = ioremap(phba->pci_bar0_map, bar0map_len);
9199 if (!phba->slim_memmap_p) {
9200 dev_printk(KERN_ERR, &pdev->dev,
9201 "ioremap failed for SLIM memory.\n");
9202 goto out;
9203 }
9204
9205 /* Map HBA Control Registers to a kernel virtual address. */
9206 phba->ctrl_regs_memmap_p = ioremap(phba->pci_bar2_map, bar2map_len);
9207 if (!phba->ctrl_regs_memmap_p) {
9208 dev_printk(KERN_ERR, &pdev->dev,
9209 "ioremap failed for HBA control registers.\n");
9210 goto out_iounmap_slim;
9211 }
9212
9213 /* Allocate memory for SLI-2 structures */
Luis Chamberlain750afb02019-01-04 09:23:09 +01009214 phba->slim2p.virt = dma_alloc_coherent(&pdev->dev, SLI2_SLIM_SIZE,
9215 &phba->slim2p.phys, GFP_KERNEL);
James Smart3772a992009-05-22 14:50:54 -04009216 if (!phba->slim2p.virt)
9217 goto out_iounmap;
9218
James Smart3772a992009-05-22 14:50:54 -04009219 phba->mbox = phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, mbx);
James Smart7a470272010-03-15 11:25:20 -04009220 phba->mbox_ext = (phba->slim2p.virt +
9221 offsetof(struct lpfc_sli2_slim, mbx_ext_words));
James Smart3772a992009-05-22 14:50:54 -04009222 phba->pcb = (phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, pcb));
9223 phba->IOCBs = (phba->slim2p.virt +
9224 offsetof(struct lpfc_sli2_slim, IOCBs));
9225
9226 phba->hbqslimp.virt = dma_alloc_coherent(&pdev->dev,
9227 lpfc_sli_hbq_size(),
9228 &phba->hbqslimp.phys,
9229 GFP_KERNEL);
9230 if (!phba->hbqslimp.virt)
9231 goto out_free_slim;
9232
9233 hbq_count = lpfc_sli_hbq_count();
9234 ptr = phba->hbqslimp.virt;
9235 for (i = 0; i < hbq_count; ++i) {
9236 phba->hbqs[i].hbq_virt = ptr;
9237 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
9238 ptr += (lpfc_hbq_defs[i]->entry_count *
9239 sizeof(struct lpfc_hbq_entry));
9240 }
9241 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_els_hbq_alloc;
9242 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_els_hbq_free;
9243
9244 memset(phba->hbqslimp.virt, 0, lpfc_sli_hbq_size());
9245
James Smart3772a992009-05-22 14:50:54 -04009246 phba->MBslimaddr = phba->slim_memmap_p;
9247 phba->HAregaddr = phba->ctrl_regs_memmap_p + HA_REG_OFFSET;
9248 phba->CAregaddr = phba->ctrl_regs_memmap_p + CA_REG_OFFSET;
9249 phba->HSregaddr = phba->ctrl_regs_memmap_p + HS_REG_OFFSET;
9250 phba->HCregaddr = phba->ctrl_regs_memmap_p + HC_REG_OFFSET;
James Smartea2151b2008-09-07 11:52:10 -04009251
dea31012005-04-17 16:05:31 -05009252 return 0;
9253
dea31012005-04-17 16:05:31 -05009254out_free_slim:
James Smart34b02dc2008-08-24 21:49:55 -04009255 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
9256 phba->slim2p.virt, phba->slim2p.phys);
dea31012005-04-17 16:05:31 -05009257out_iounmap:
9258 iounmap(phba->ctrl_regs_memmap_p);
Jamie Wellnitz901a9202006-02-28 19:25:19 -05009259out_iounmap_slim:
dea31012005-04-17 16:05:31 -05009260 iounmap(phba->slim_memmap_p);
dea31012005-04-17 16:05:31 -05009261out:
9262 return error;
9263}
9264
James Smarte59058c2008-08-24 21:49:00 -04009265/**
James Smart3772a992009-05-22 14:50:54 -04009266 * lpfc_sli_pci_mem_unset - Unset SLI3 HBA PCI memory space.
9267 * @phba: pointer to lpfc hba data structure.
9268 *
9269 * This routine is invoked to unset the PCI device memory space for device
9270 * with SLI-3 interface spec.
9271 **/
9272static void
9273lpfc_sli_pci_mem_unset(struct lpfc_hba *phba)
9274{
9275 struct pci_dev *pdev;
9276
9277 /* Obtain PCI device reference */
9278 if (!phba->pcidev)
9279 return;
9280 else
9281 pdev = phba->pcidev;
9282
9283 /* Free coherent DMA memory allocated */
9284 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
9285 phba->hbqslimp.virt, phba->hbqslimp.phys);
9286 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
9287 phba->slim2p.virt, phba->slim2p.phys);
9288
9289 /* I/O memory unmap */
9290 iounmap(phba->ctrl_regs_memmap_p);
9291 iounmap(phba->slim_memmap_p);
9292
9293 return;
9294}
9295
9296/**
James Smartda0436e2009-05-22 14:51:39 -04009297 * lpfc_sli4_post_status_check - Wait for SLI4 POST done and check status
9298 * @phba: pointer to lpfc hba data structure.
9299 *
9300 * This routine is invoked to wait for SLI4 device Power On Self Test (POST)
9301 * done and check status.
9302 *
9303 * Return 0 if successful, otherwise -ENODEV.
9304 **/
9305int
9306lpfc_sli4_post_status_check(struct lpfc_hba *phba)
9307{
James Smart2fcee4b2010-12-15 17:57:46 -05009308 struct lpfc_register portsmphr_reg, uerrlo_reg, uerrhi_reg;
9309 struct lpfc_register reg_data;
9310 int i, port_error = 0;
9311 uint32_t if_type;
James Smartda0436e2009-05-22 14:51:39 -04009312
James Smart9940b972011-03-11 16:06:12 -05009313 memset(&portsmphr_reg, 0, sizeof(portsmphr_reg));
9314 memset(&reg_data, 0, sizeof(reg_data));
James Smart2fcee4b2010-12-15 17:57:46 -05009315 if (!phba->sli4_hba.PSMPHRregaddr)
James Smartda0436e2009-05-22 14:51:39 -04009316 return -ENODEV;
9317
James Smartda0436e2009-05-22 14:51:39 -04009318 /* Wait up to 30 seconds for the SLI Port POST done and ready */
9319 for (i = 0; i < 3000; i++) {
James Smart9940b972011-03-11 16:06:12 -05009320 if (lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
9321 &portsmphr_reg.word0) ||
9322 (bf_get(lpfc_port_smphr_perr, &portsmphr_reg))) {
James Smart2fcee4b2010-12-15 17:57:46 -05009323 /* Port has a fatal POST error, break out */
James Smartda0436e2009-05-22 14:51:39 -04009324 port_error = -ENODEV;
9325 break;
9326 }
James Smart2fcee4b2010-12-15 17:57:46 -05009327 if (LPFC_POST_STAGE_PORT_READY ==
9328 bf_get(lpfc_port_smphr_port_status, &portsmphr_reg))
James Smartda0436e2009-05-22 14:51:39 -04009329 break;
James Smartda0436e2009-05-22 14:51:39 -04009330 msleep(10);
9331 }
9332
James Smart2fcee4b2010-12-15 17:57:46 -05009333 /*
9334 * If there was a port error during POST, then don't proceed with
9335 * other register reads as the data may not be valid. Just exit.
9336 */
9337 if (port_error) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009338 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2fcee4b2010-12-15 17:57:46 -05009339 "1408 Port Failed POST - portsmphr=0x%x, "
9340 "perr=x%x, sfi=x%x, nip=x%x, ipc=x%x, scr1=x%x, "
9341 "scr2=x%x, hscratch=x%x, pstatus=x%x\n",
9342 portsmphr_reg.word0,
9343 bf_get(lpfc_port_smphr_perr, &portsmphr_reg),
9344 bf_get(lpfc_port_smphr_sfi, &portsmphr_reg),
9345 bf_get(lpfc_port_smphr_nip, &portsmphr_reg),
9346 bf_get(lpfc_port_smphr_ipc, &portsmphr_reg),
9347 bf_get(lpfc_port_smphr_scr1, &portsmphr_reg),
9348 bf_get(lpfc_port_smphr_scr2, &portsmphr_reg),
9349 bf_get(lpfc_port_smphr_host_scratch, &portsmphr_reg),
9350 bf_get(lpfc_port_smphr_port_status, &portsmphr_reg));
9351 } else {
James Smart28baac72010-02-12 14:42:03 -05009352 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -05009353 "2534 Device Info: SLIFamily=0x%x, "
9354 "SLIRev=0x%x, IFType=0x%x, SLIHint_1=0x%x, "
9355 "SLIHint_2=0x%x, FT=0x%x\n",
James Smart28baac72010-02-12 14:42:03 -05009356 bf_get(lpfc_sli_intf_sli_family,
9357 &phba->sli4_hba.sli_intf),
9358 bf_get(lpfc_sli_intf_slirev,
9359 &phba->sli4_hba.sli_intf),
James Smart085c6472010-11-20 23:11:37 -05009360 bf_get(lpfc_sli_intf_if_type,
James Smart28baac72010-02-12 14:42:03 -05009361 &phba->sli4_hba.sli_intf),
James Smart085c6472010-11-20 23:11:37 -05009362 bf_get(lpfc_sli_intf_sli_hint1,
9363 &phba->sli4_hba.sli_intf),
9364 bf_get(lpfc_sli_intf_sli_hint2,
9365 &phba->sli4_hba.sli_intf),
9366 bf_get(lpfc_sli_intf_func_type,
James Smart28baac72010-02-12 14:42:03 -05009367 &phba->sli4_hba.sli_intf));
James Smart2fcee4b2010-12-15 17:57:46 -05009368 /*
9369 * Check for other Port errors during the initialization
9370 * process. Fail the load if the port did not come up
9371 * correctly.
9372 */
9373 if_type = bf_get(lpfc_sli_intf_if_type,
9374 &phba->sli4_hba.sli_intf);
9375 switch (if_type) {
9376 case LPFC_SLI_INTF_IF_TYPE_0:
9377 phba->sli4_hba.ue_mask_lo =
9378 readl(phba->sli4_hba.u.if_type0.UEMASKLOregaddr);
9379 phba->sli4_hba.ue_mask_hi =
9380 readl(phba->sli4_hba.u.if_type0.UEMASKHIregaddr);
9381 uerrlo_reg.word0 =
9382 readl(phba->sli4_hba.u.if_type0.UERRLOregaddr);
9383 uerrhi_reg.word0 =
9384 readl(phba->sli4_hba.u.if_type0.UERRHIregaddr);
9385 if ((~phba->sli4_hba.ue_mask_lo & uerrlo_reg.word0) ||
9386 (~phba->sli4_hba.ue_mask_hi & uerrhi_reg.word0)) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009387 lpfc_printf_log(phba, KERN_ERR,
9388 LOG_TRACE_EVENT,
James Smart2fcee4b2010-12-15 17:57:46 -05009389 "1422 Unrecoverable Error "
9390 "Detected during POST "
9391 "uerr_lo_reg=0x%x, "
9392 "uerr_hi_reg=0x%x, "
9393 "ue_mask_lo_reg=0x%x, "
9394 "ue_mask_hi_reg=0x%x\n",
9395 uerrlo_reg.word0,
9396 uerrhi_reg.word0,
9397 phba->sli4_hba.ue_mask_lo,
9398 phba->sli4_hba.ue_mask_hi);
9399 port_error = -ENODEV;
9400 }
9401 break;
9402 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart27d6ac02018-02-22 08:18:42 -08009403 case LPFC_SLI_INTF_IF_TYPE_6:
James Smart2fcee4b2010-12-15 17:57:46 -05009404 /* Final checks. The port status should be clean. */
James Smart9940b972011-03-11 16:06:12 -05009405 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
9406 &reg_data.word0) ||
James Smart05580562011-05-24 11:40:48 -04009407 (bf_get(lpfc_sliport_status_err, &reg_data) &&
9408 !bf_get(lpfc_sliport_status_rn, &reg_data))) {
James Smart2fcee4b2010-12-15 17:57:46 -05009409 phba->work_status[0] =
9410 readl(phba->sli4_hba.u.if_type2.
9411 ERR1regaddr);
9412 phba->work_status[1] =
9413 readl(phba->sli4_hba.u.if_type2.
9414 ERR2regaddr);
Dick Kennedy372c1872020-06-30 14:50:00 -07009415 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart8fcb8ac2012-03-01 22:35:58 -05009416 "2888 Unrecoverable port error "
9417 "following POST: port status reg "
9418 "0x%x, port_smphr reg 0x%x, "
James Smart2fcee4b2010-12-15 17:57:46 -05009419 "error 1=0x%x, error 2=0x%x\n",
9420 reg_data.word0,
9421 portsmphr_reg.word0,
9422 phba->work_status[0],
9423 phba->work_status[1]);
9424 port_error = -ENODEV;
James Smarta5b141a2021-09-27 11:35:18 -07009425 break;
James Smart2fcee4b2010-12-15 17:57:46 -05009426 }
James Smarta5b141a2021-09-27 11:35:18 -07009427
9428 if (lpfc_pldv_detect &&
9429 bf_get(lpfc_sli_intf_sli_family,
9430 &phba->sli4_hba.sli_intf) ==
9431 LPFC_SLI_INTF_FAMILY_G6)
9432 pci_write_config_byte(phba->pcidev,
9433 LPFC_SLI_INTF, CFG_PLD);
James Smart2fcee4b2010-12-15 17:57:46 -05009434 break;
9435 case LPFC_SLI_INTF_IF_TYPE_1:
9436 default:
9437 break;
9438 }
James Smart28baac72010-02-12 14:42:03 -05009439 }
James Smartda0436e2009-05-22 14:51:39 -04009440 return port_error;
9441}
9442
9443/**
9444 * lpfc_sli4_bar0_register_memmap - Set up SLI4 BAR0 register memory map.
9445 * @phba: pointer to lpfc hba data structure.
James Smart2fcee4b2010-12-15 17:57:46 -05009446 * @if_type: The SLI4 interface type getting configured.
James Smartda0436e2009-05-22 14:51:39 -04009447 *
9448 * This routine is invoked to set up SLI4 BAR0 PCI config space register
9449 * memory map.
9450 **/
9451static void
James Smart2fcee4b2010-12-15 17:57:46 -05009452lpfc_sli4_bar0_register_memmap(struct lpfc_hba *phba, uint32_t if_type)
James Smartda0436e2009-05-22 14:51:39 -04009453{
James Smart2fcee4b2010-12-15 17:57:46 -05009454 switch (if_type) {
9455 case LPFC_SLI_INTF_IF_TYPE_0:
9456 phba->sli4_hba.u.if_type0.UERRLOregaddr =
9457 phba->sli4_hba.conf_regs_memmap_p + LPFC_UERR_STATUS_LO;
9458 phba->sli4_hba.u.if_type0.UERRHIregaddr =
9459 phba->sli4_hba.conf_regs_memmap_p + LPFC_UERR_STATUS_HI;
9460 phba->sli4_hba.u.if_type0.UEMASKLOregaddr =
9461 phba->sli4_hba.conf_regs_memmap_p + LPFC_UE_MASK_LO;
9462 phba->sli4_hba.u.if_type0.UEMASKHIregaddr =
9463 phba->sli4_hba.conf_regs_memmap_p + LPFC_UE_MASK_HI;
9464 phba->sli4_hba.SLIINTFregaddr =
9465 phba->sli4_hba.conf_regs_memmap_p + LPFC_SLI_INTF;
9466 break;
9467 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart0cf07f842017-06-01 21:07:10 -07009468 phba->sli4_hba.u.if_type2.EQDregaddr =
9469 phba->sli4_hba.conf_regs_memmap_p +
9470 LPFC_CTL_PORT_EQ_DELAY_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05009471 phba->sli4_hba.u.if_type2.ERR1regaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04009472 phba->sli4_hba.conf_regs_memmap_p +
9473 LPFC_CTL_PORT_ER1_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05009474 phba->sli4_hba.u.if_type2.ERR2regaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04009475 phba->sli4_hba.conf_regs_memmap_p +
9476 LPFC_CTL_PORT_ER2_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05009477 phba->sli4_hba.u.if_type2.CTRLregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04009478 phba->sli4_hba.conf_regs_memmap_p +
9479 LPFC_CTL_PORT_CTL_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05009480 phba->sli4_hba.u.if_type2.STATUSregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04009481 phba->sli4_hba.conf_regs_memmap_p +
9482 LPFC_CTL_PORT_STA_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05009483 phba->sli4_hba.SLIINTFregaddr =
9484 phba->sli4_hba.conf_regs_memmap_p + LPFC_SLI_INTF;
9485 phba->sli4_hba.PSMPHRregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04009486 phba->sli4_hba.conf_regs_memmap_p +
9487 LPFC_CTL_PORT_SEM_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05009488 phba->sli4_hba.RQDBregaddr =
James Smart962bc512013-01-03 15:44:00 -05009489 phba->sli4_hba.conf_regs_memmap_p +
9490 LPFC_ULP0_RQ_DOORBELL;
James Smart2fcee4b2010-12-15 17:57:46 -05009491 phba->sli4_hba.WQDBregaddr =
James Smart962bc512013-01-03 15:44:00 -05009492 phba->sli4_hba.conf_regs_memmap_p +
9493 LPFC_ULP0_WQ_DOORBELL;
James Smart9dd35422018-02-22 08:18:41 -08009494 phba->sli4_hba.CQDBregaddr =
James Smart2fcee4b2010-12-15 17:57:46 -05009495 phba->sli4_hba.conf_regs_memmap_p + LPFC_EQCQ_DOORBELL;
James Smart9dd35422018-02-22 08:18:41 -08009496 phba->sli4_hba.EQDBregaddr = phba->sli4_hba.CQDBregaddr;
James Smart2fcee4b2010-12-15 17:57:46 -05009497 phba->sli4_hba.MQDBregaddr =
9498 phba->sli4_hba.conf_regs_memmap_p + LPFC_MQ_DOORBELL;
9499 phba->sli4_hba.BMBXregaddr =
9500 phba->sli4_hba.conf_regs_memmap_p + LPFC_BMBX;
9501 break;
James Smart27d6ac02018-02-22 08:18:42 -08009502 case LPFC_SLI_INTF_IF_TYPE_6:
9503 phba->sli4_hba.u.if_type2.EQDregaddr =
9504 phba->sli4_hba.conf_regs_memmap_p +
9505 LPFC_CTL_PORT_EQ_DELAY_OFFSET;
9506 phba->sli4_hba.u.if_type2.ERR1regaddr =
9507 phba->sli4_hba.conf_regs_memmap_p +
9508 LPFC_CTL_PORT_ER1_OFFSET;
9509 phba->sli4_hba.u.if_type2.ERR2regaddr =
9510 phba->sli4_hba.conf_regs_memmap_p +
9511 LPFC_CTL_PORT_ER2_OFFSET;
9512 phba->sli4_hba.u.if_type2.CTRLregaddr =
9513 phba->sli4_hba.conf_regs_memmap_p +
9514 LPFC_CTL_PORT_CTL_OFFSET;
9515 phba->sli4_hba.u.if_type2.STATUSregaddr =
9516 phba->sli4_hba.conf_regs_memmap_p +
9517 LPFC_CTL_PORT_STA_OFFSET;
9518 phba->sli4_hba.PSMPHRregaddr =
9519 phba->sli4_hba.conf_regs_memmap_p +
9520 LPFC_CTL_PORT_SEM_OFFSET;
9521 phba->sli4_hba.BMBXregaddr =
9522 phba->sli4_hba.conf_regs_memmap_p + LPFC_BMBX;
9523 break;
James Smart2fcee4b2010-12-15 17:57:46 -05009524 case LPFC_SLI_INTF_IF_TYPE_1:
9525 default:
9526 dev_printk(KERN_ERR, &phba->pcidev->dev,
9527 "FATAL - unsupported SLI4 interface type - %d\n",
9528 if_type);
9529 break;
9530 }
James Smartda0436e2009-05-22 14:51:39 -04009531}
9532
9533/**
9534 * lpfc_sli4_bar1_register_memmap - Set up SLI4 BAR1 register memory map.
9535 * @phba: pointer to lpfc hba data structure.
Lee Jonesfe614ac2020-07-23 13:24:22 +01009536 * @if_type: sli if type to operate on.
James Smartda0436e2009-05-22 14:51:39 -04009537 *
James Smart27d6ac02018-02-22 08:18:42 -08009538 * This routine is invoked to set up SLI4 BAR1 register memory map.
James Smartda0436e2009-05-22 14:51:39 -04009539 **/
9540static void
James Smart27d6ac02018-02-22 08:18:42 -08009541lpfc_sli4_bar1_register_memmap(struct lpfc_hba *phba, uint32_t if_type)
James Smartda0436e2009-05-22 14:51:39 -04009542{
James Smart27d6ac02018-02-22 08:18:42 -08009543 switch (if_type) {
9544 case LPFC_SLI_INTF_IF_TYPE_0:
9545 phba->sli4_hba.PSMPHRregaddr =
9546 phba->sli4_hba.ctrl_regs_memmap_p +
9547 LPFC_SLIPORT_IF0_SMPHR;
9548 phba->sli4_hba.ISRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
9549 LPFC_HST_ISR0;
9550 phba->sli4_hba.IMRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
9551 LPFC_HST_IMR0;
9552 phba->sli4_hba.ISCRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
9553 LPFC_HST_ISCR0;
9554 break;
9555 case LPFC_SLI_INTF_IF_TYPE_6:
9556 phba->sli4_hba.RQDBregaddr = phba->sli4_hba.drbl_regs_memmap_p +
9557 LPFC_IF6_RQ_DOORBELL;
9558 phba->sli4_hba.WQDBregaddr = phba->sli4_hba.drbl_regs_memmap_p +
9559 LPFC_IF6_WQ_DOORBELL;
9560 phba->sli4_hba.CQDBregaddr = phba->sli4_hba.drbl_regs_memmap_p +
9561 LPFC_IF6_CQ_DOORBELL;
9562 phba->sli4_hba.EQDBregaddr = phba->sli4_hba.drbl_regs_memmap_p +
9563 LPFC_IF6_EQ_DOORBELL;
9564 phba->sli4_hba.MQDBregaddr = phba->sli4_hba.drbl_regs_memmap_p +
9565 LPFC_IF6_MQ_DOORBELL;
9566 break;
9567 case LPFC_SLI_INTF_IF_TYPE_2:
9568 case LPFC_SLI_INTF_IF_TYPE_1:
9569 default:
9570 dev_err(&phba->pcidev->dev,
9571 "FATAL - unsupported SLI4 interface type - %d\n",
9572 if_type);
9573 break;
9574 }
James Smartda0436e2009-05-22 14:51:39 -04009575}
9576
9577/**
9578 * lpfc_sli4_bar2_register_memmap - Set up SLI4 BAR2 register memory map.
9579 * @phba: pointer to lpfc hba data structure.
9580 * @vf: virtual function number
9581 *
9582 * This routine is invoked to set up SLI4 BAR2 doorbell register memory map
9583 * based on the given viftual function number, @vf.
9584 *
9585 * Return 0 if successful, otherwise -ENODEV.
9586 **/
9587static int
9588lpfc_sli4_bar2_register_memmap(struct lpfc_hba *phba, uint32_t vf)
9589{
9590 if (vf > LPFC_VIR_FUNC_MAX)
9591 return -ENODEV;
9592
9593 phba->sli4_hba.RQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
James Smart962bc512013-01-03 15:44:00 -05009594 vf * LPFC_VFR_PAGE_SIZE +
9595 LPFC_ULP0_RQ_DOORBELL);
James Smartda0436e2009-05-22 14:51:39 -04009596 phba->sli4_hba.WQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
James Smart962bc512013-01-03 15:44:00 -05009597 vf * LPFC_VFR_PAGE_SIZE +
9598 LPFC_ULP0_WQ_DOORBELL);
James Smart9dd35422018-02-22 08:18:41 -08009599 phba->sli4_hba.CQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
9600 vf * LPFC_VFR_PAGE_SIZE +
9601 LPFC_EQCQ_DOORBELL);
9602 phba->sli4_hba.EQDBregaddr = phba->sli4_hba.CQDBregaddr;
James Smartda0436e2009-05-22 14:51:39 -04009603 phba->sli4_hba.MQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
9604 vf * LPFC_VFR_PAGE_SIZE + LPFC_MQ_DOORBELL);
9605 phba->sli4_hba.BMBXregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
9606 vf * LPFC_VFR_PAGE_SIZE + LPFC_BMBX);
9607 return 0;
9608}
9609
9610/**
9611 * lpfc_create_bootstrap_mbox - Create the bootstrap mailbox
9612 * @phba: pointer to lpfc hba data structure.
9613 *
9614 * This routine is invoked to create the bootstrap mailbox
9615 * region consistent with the SLI-4 interface spec. This
9616 * routine allocates all memory necessary to communicate
9617 * mailbox commands to the port and sets up all alignment
9618 * needs. No locks are expected to be held when calling
9619 * this routine.
9620 *
9621 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02009622 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04009623 * -ENOMEM - could not allocated memory.
James Smartda0436e2009-05-22 14:51:39 -04009624 **/
9625static int
9626lpfc_create_bootstrap_mbox(struct lpfc_hba *phba)
9627{
9628 uint32_t bmbx_size;
9629 struct lpfc_dmabuf *dmabuf;
9630 struct dma_address *dma_address;
9631 uint32_t pa_addr;
9632 uint64_t phys_addr;
9633
9634 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
9635 if (!dmabuf)
9636 return -ENOMEM;
9637
9638 /*
9639 * The bootstrap mailbox region is comprised of 2 parts
9640 * plus an alignment restriction of 16 bytes.
9641 */
9642 bmbx_size = sizeof(struct lpfc_bmbx_create) + (LPFC_ALIGN_16_BYTE - 1);
Luis Chamberlain750afb02019-01-04 09:23:09 +01009643 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev, bmbx_size,
9644 &dmabuf->phys, GFP_KERNEL);
James Smartda0436e2009-05-22 14:51:39 -04009645 if (!dmabuf->virt) {
9646 kfree(dmabuf);
9647 return -ENOMEM;
9648 }
James Smartda0436e2009-05-22 14:51:39 -04009649
9650 /*
9651 * Initialize the bootstrap mailbox pointers now so that the register
9652 * operations are simple later. The mailbox dma address is required
9653 * to be 16-byte aligned. Also align the virtual memory as each
9654 * maibox is copied into the bmbx mailbox region before issuing the
9655 * command to the port.
9656 */
9657 phba->sli4_hba.bmbx.dmabuf = dmabuf;
9658 phba->sli4_hba.bmbx.bmbx_size = bmbx_size;
9659
9660 phba->sli4_hba.bmbx.avirt = PTR_ALIGN(dmabuf->virt,
9661 LPFC_ALIGN_16_BYTE);
9662 phba->sli4_hba.bmbx.aphys = ALIGN(dmabuf->phys,
9663 LPFC_ALIGN_16_BYTE);
9664
9665 /*
9666 * Set the high and low physical addresses now. The SLI4 alignment
9667 * requirement is 16 bytes and the mailbox is posted to the port
9668 * as two 30-bit addresses. The other data is a bit marking whether
9669 * the 30-bit address is the high or low address.
9670 * Upcast bmbx aphys to 64bits so shift instruction compiles
9671 * clean on 32 bit machines.
9672 */
9673 dma_address = &phba->sli4_hba.bmbx.dma_address;
9674 phys_addr = (uint64_t)phba->sli4_hba.bmbx.aphys;
9675 pa_addr = (uint32_t) ((phys_addr >> 34) & 0x3fffffff);
9676 dma_address->addr_hi = (uint32_t) ((pa_addr << 2) |
9677 LPFC_BMBX_BIT1_ADDR_HI);
9678
9679 pa_addr = (uint32_t) ((phba->sli4_hba.bmbx.aphys >> 4) & 0x3fffffff);
9680 dma_address->addr_lo = (uint32_t) ((pa_addr << 2) |
9681 LPFC_BMBX_BIT1_ADDR_LO);
9682 return 0;
9683}
9684
9685/**
9686 * lpfc_destroy_bootstrap_mbox - Destroy all bootstrap mailbox resources
9687 * @phba: pointer to lpfc hba data structure.
9688 *
9689 * This routine is invoked to teardown the bootstrap mailbox
9690 * region and release all host resources. This routine requires
9691 * the caller to ensure all mailbox commands recovered, no
9692 * additional mailbox comands are sent, and interrupts are disabled
9693 * before calling this routine.
9694 *
9695 **/
9696static void
9697lpfc_destroy_bootstrap_mbox(struct lpfc_hba *phba)
9698{
9699 dma_free_coherent(&phba->pcidev->dev,
9700 phba->sli4_hba.bmbx.bmbx_size,
9701 phba->sli4_hba.bmbx.dmabuf->virt,
9702 phba->sli4_hba.bmbx.dmabuf->phys);
9703
9704 kfree(phba->sli4_hba.bmbx.dmabuf);
9705 memset(&phba->sli4_hba.bmbx, 0, sizeof(struct lpfc_bmbx));
9706}
9707
James Smart83c6cb12019-10-18 14:18:30 -07009708static const char * const lpfc_topo_to_str[] = {
9709 "Loop then P2P",
9710 "Loopback",
9711 "P2P Only",
9712 "Unsupported",
9713 "Loop Only",
9714 "Unsupported",
9715 "P2P then Loop",
9716};
9717
Lee Jonesfe614ac2020-07-23 13:24:22 +01009718#define LINK_FLAGS_DEF 0x0
9719#define LINK_FLAGS_P2P 0x1
9720#define LINK_FLAGS_LOOP 0x2
James Smart83c6cb12019-10-18 14:18:30 -07009721/**
9722 * lpfc_map_topology - Map the topology read from READ_CONFIG
9723 * @phba: pointer to lpfc hba data structure.
Lee Jonesfe614ac2020-07-23 13:24:22 +01009724 * @rd_config: pointer to read config data
James Smart83c6cb12019-10-18 14:18:30 -07009725 *
9726 * This routine is invoked to map the topology values as read
9727 * from the read config mailbox command. If the persistent
9728 * topology feature is supported, the firmware will provide the
9729 * saved topology information to be used in INIT_LINK
James Smart83c6cb12019-10-18 14:18:30 -07009730 **/
James Smart83c6cb12019-10-18 14:18:30 -07009731static void
9732lpfc_map_topology(struct lpfc_hba *phba, struct lpfc_mbx_read_config *rd_config)
9733{
9734 u8 ptv, tf, pt;
9735
9736 ptv = bf_get(lpfc_mbx_rd_conf_ptv, rd_config);
9737 tf = bf_get(lpfc_mbx_rd_conf_tf, rd_config);
9738 pt = bf_get(lpfc_mbx_rd_conf_pt, rd_config);
9739
9740 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9741 "2027 Read Config Data : ptv:0x%x, tf:0x%x pt:0x%x",
9742 ptv, tf, pt);
9743 if (!ptv) {
9744 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9745 "2019 FW does not support persistent topology "
9746 "Using driver parameter defined value [%s]",
9747 lpfc_topo_to_str[phba->cfg_topology]);
9748 return;
9749 }
9750 /* FW supports persistent topology - override module parameter value */
9751 phba->hba_flag |= HBA_PERSISTENT_TOPO;
James Smartf6c5e6c2021-07-22 15:17:18 -07009752
9753 /* if ASIC_GEN_NUM >= 0xC) */
9754 if ((bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
9755 LPFC_SLI_INTF_IF_TYPE_6) ||
9756 (bf_get(lpfc_sli_intf_sli_family, &phba->sli4_hba.sli_intf) ==
9757 LPFC_SLI_INTF_FAMILY_G6)) {
James Smart83c6cb12019-10-18 14:18:30 -07009758 if (!tf) {
9759 phba->cfg_topology = ((pt == LINK_FLAGS_LOOP)
9760 ? FLAGS_TOPOLOGY_MODE_LOOP
9761 : FLAGS_TOPOLOGY_MODE_PT_PT);
9762 } else {
9763 phba->hba_flag &= ~HBA_PERSISTENT_TOPO;
9764 }
James Smartf6c5e6c2021-07-22 15:17:18 -07009765 } else { /* G5 */
James Smart83c6cb12019-10-18 14:18:30 -07009766 if (tf) {
9767 /* If topology failover set - pt is '0' or '1' */
9768 phba->cfg_topology = (pt ? FLAGS_TOPOLOGY_MODE_PT_LOOP :
9769 FLAGS_TOPOLOGY_MODE_LOOP_PT);
9770 } else {
9771 phba->cfg_topology = ((pt == LINK_FLAGS_P2P)
9772 ? FLAGS_TOPOLOGY_MODE_PT_PT
9773 : FLAGS_TOPOLOGY_MODE_LOOP);
9774 }
James Smart83c6cb12019-10-18 14:18:30 -07009775 }
9776 if (phba->hba_flag & HBA_PERSISTENT_TOPO) {
9777 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9778 "2020 Using persistent topology value [%s]",
9779 lpfc_topo_to_str[phba->cfg_topology]);
9780 } else {
9781 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9782 "2021 Invalid topology values from FW "
9783 "Using driver parameter defined value [%s]",
9784 lpfc_topo_to_str[phba->cfg_topology]);
9785 }
9786}
9787
James Smartda0436e2009-05-22 14:51:39 -04009788/**
9789 * lpfc_sli4_read_config - Get the config parameters.
9790 * @phba: pointer to lpfc hba data structure.
9791 *
9792 * This routine is invoked to read the configuration parameters from the HBA.
9793 * The configuration parameters are used to set the base and maximum values
9794 * for RPI's XRI's VPI's VFI's and FCFIs. These values also affect the resource
9795 * allocation for the port.
9796 *
9797 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02009798 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009799 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04009800 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04009801 **/
James Smartff78d8f2011-12-13 13:21:35 -05009802int
James Smartda0436e2009-05-22 14:51:39 -04009803lpfc_sli4_read_config(struct lpfc_hba *phba)
9804{
9805 LPFC_MBOXQ_t *pmb;
9806 struct lpfc_mbx_read_config *rd_config;
James Smart912e3ac2011-05-24 11:42:11 -04009807 union lpfc_sli4_cfg_shdr *shdr;
9808 uint32_t shdr_status, shdr_add_status;
9809 struct lpfc_mbx_get_func_cfg *get_func_cfg;
9810 struct lpfc_rsrc_desc_fcfcoe *desc;
James Smart8aa134a2012-08-14 14:25:29 -04009811 char *pdesc_0;
James Smartc6918162016-10-13 15:06:16 -07009812 uint16_t forced_link_speed;
James Smart6a828b02019-01-28 11:14:31 -08009813 uint32_t if_type, qmin;
James Smart8aa134a2012-08-14 14:25:29 -04009814 int length, i, rc = 0, rc2;
James Smartda0436e2009-05-22 14:51:39 -04009815
9816 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9817 if (!pmb) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009818 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartda0436e2009-05-22 14:51:39 -04009819 "2011 Unable to allocate memory for issuing "
9820 "SLI_CONFIG_SPECIAL mailbox command\n");
9821 return -ENOMEM;
9822 }
9823
9824 lpfc_read_config(phba, pmb);
9825
9826 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
9827 if (rc != MBX_SUCCESS) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009828 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
9829 "2012 Mailbox failed , mbxCmd x%x "
9830 "READ_CONFIG, mbxStatus x%x\n",
9831 bf_get(lpfc_mqe_command, &pmb->u.mqe),
9832 bf_get(lpfc_mqe_status, &pmb->u.mqe));
James Smartda0436e2009-05-22 14:51:39 -04009833 rc = -EIO;
9834 } else {
9835 rd_config = &pmb->u.mqe.un.rd_config;
James Smartff78d8f2011-12-13 13:21:35 -05009836 if (bf_get(lpfc_mbx_rd_conf_lnk_ldv, rd_config)) {
9837 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
9838 phba->sli4_hba.lnk_info.lnk_tp =
9839 bf_get(lpfc_mbx_rd_conf_lnk_type, rd_config);
9840 phba->sli4_hba.lnk_info.lnk_no =
9841 bf_get(lpfc_mbx_rd_conf_lnk_numb, rd_config);
9842 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9843 "3081 lnk_type:%d, lnk_numb:%d\n",
9844 phba->sli4_hba.lnk_info.lnk_tp,
9845 phba->sli4_hba.lnk_info.lnk_no);
9846 } else
9847 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9848 "3082 Mailbox (x%x) returned ldv:x0\n",
9849 bf_get(lpfc_mqe_command, &pmb->u.mqe));
James Smart44fd7fe2017-08-23 16:55:47 -07009850 if (bf_get(lpfc_mbx_rd_conf_bbscn_def, rd_config)) {
9851 phba->bbcredit_support = 1;
9852 phba->sli4_hba.bbscn_params.word0 = rd_config->word8;
9853 }
9854
James Smart1dc5ec22018-10-23 13:41:11 -07009855 phba->sli4_hba.conf_trunk =
9856 bf_get(lpfc_mbx_rd_conf_trunk, rd_config);
James Smart6d368e52011-05-24 11:44:12 -04009857 phba->sli4_hba.extents_in_use =
9858 bf_get(lpfc_mbx_rd_conf_extnts_inuse, rd_config);
James Smartda0436e2009-05-22 14:51:39 -04009859 phba->sli4_hba.max_cfg_param.max_xri =
9860 bf_get(lpfc_mbx_rd_conf_xri_count, rd_config);
James Smart31f06d22019-08-14 16:56:31 -07009861 /* Reduce resource usage in kdump environment */
9862 if (is_kdump_kernel() &&
9863 phba->sli4_hba.max_cfg_param.max_xri > 512)
9864 phba->sli4_hba.max_cfg_param.max_xri = 512;
James Smartda0436e2009-05-22 14:51:39 -04009865 phba->sli4_hba.max_cfg_param.xri_base =
9866 bf_get(lpfc_mbx_rd_conf_xri_base, rd_config);
9867 phba->sli4_hba.max_cfg_param.max_vpi =
9868 bf_get(lpfc_mbx_rd_conf_vpi_count, rd_config);
James Smart8b47ae62018-11-29 16:09:33 -08009869 /* Limit the max we support */
9870 if (phba->sli4_hba.max_cfg_param.max_vpi > LPFC_MAX_VPORTS)
9871 phba->sli4_hba.max_cfg_param.max_vpi = LPFC_MAX_VPORTS;
James Smartda0436e2009-05-22 14:51:39 -04009872 phba->sli4_hba.max_cfg_param.vpi_base =
9873 bf_get(lpfc_mbx_rd_conf_vpi_base, rd_config);
9874 phba->sli4_hba.max_cfg_param.max_rpi =
9875 bf_get(lpfc_mbx_rd_conf_rpi_count, rd_config);
9876 phba->sli4_hba.max_cfg_param.rpi_base =
9877 bf_get(lpfc_mbx_rd_conf_rpi_base, rd_config);
9878 phba->sli4_hba.max_cfg_param.max_vfi =
9879 bf_get(lpfc_mbx_rd_conf_vfi_count, rd_config);
9880 phba->sli4_hba.max_cfg_param.vfi_base =
9881 bf_get(lpfc_mbx_rd_conf_vfi_base, rd_config);
9882 phba->sli4_hba.max_cfg_param.max_fcfi =
9883 bf_get(lpfc_mbx_rd_conf_fcfi_count, rd_config);
James Smartda0436e2009-05-22 14:51:39 -04009884 phba->sli4_hba.max_cfg_param.max_eq =
9885 bf_get(lpfc_mbx_rd_conf_eq_count, rd_config);
9886 phba->sli4_hba.max_cfg_param.max_rq =
9887 bf_get(lpfc_mbx_rd_conf_rq_count, rd_config);
9888 phba->sli4_hba.max_cfg_param.max_wq =
9889 bf_get(lpfc_mbx_rd_conf_wq_count, rd_config);
9890 phba->sli4_hba.max_cfg_param.max_cq =
9891 bf_get(lpfc_mbx_rd_conf_cq_count, rd_config);
9892 phba->lmt = bf_get(lpfc_mbx_rd_conf_lmt, rd_config);
9893 phba->sli4_hba.next_xri = phba->sli4_hba.max_cfg_param.xri_base;
9894 phba->vpi_base = phba->sli4_hba.max_cfg_param.vpi_base;
9895 phba->vfi_base = phba->sli4_hba.max_cfg_param.vfi_base;
James Smart5ffc2662009-11-18 15:39:44 -05009896 phba->max_vpi = (phba->sli4_hba.max_cfg_param.max_vpi > 0) ?
9897 (phba->sli4_hba.max_cfg_param.max_vpi - 1) : 0;
James Smartda0436e2009-05-22 14:51:39 -04009898 phba->max_vports = phba->max_vpi;
James Smart9064aeb2021-08-16 09:28:50 -07009899
9900 /* Next decide on FPIN or Signal E2E CGN support
9901 * For congestion alarms and warnings valid combination are:
9902 * 1. FPIN alarms / FPIN warnings
9903 * 2. Signal alarms / Signal warnings
9904 * 3. FPIN alarms / Signal warnings
9905 * 4. Signal alarms / FPIN warnings
9906 *
9907 * Initialize the adapter frequency to 100 mSecs
9908 */
9909 phba->cgn_reg_fpin = LPFC_CGN_FPIN_BOTH;
9910 phba->cgn_reg_signal = EDC_CG_SIG_NOTSUPPORTED;
9911 phba->cgn_sig_freq = lpfc_fabric_cgn_frequency;
9912
9913 if (lpfc_use_cgn_signal) {
9914 if (bf_get(lpfc_mbx_rd_conf_wcs, rd_config)) {
9915 phba->cgn_reg_signal = EDC_CG_SIG_WARN_ONLY;
9916 phba->cgn_reg_fpin &= ~LPFC_CGN_FPIN_WARN;
9917 }
9918 if (bf_get(lpfc_mbx_rd_conf_acs, rd_config)) {
9919 /* MUST support both alarm and warning
9920 * because EDC does not support alarm alone.
9921 */
9922 if (phba->cgn_reg_signal !=
9923 EDC_CG_SIG_WARN_ONLY) {
9924 /* Must support both or none */
9925 phba->cgn_reg_fpin = LPFC_CGN_FPIN_BOTH;
9926 phba->cgn_reg_signal =
9927 EDC_CG_SIG_NOTSUPPORTED;
9928 } else {
9929 phba->cgn_reg_signal =
9930 EDC_CG_SIG_WARN_ALARM;
9931 phba->cgn_reg_fpin =
9932 LPFC_CGN_FPIN_NONE;
9933 }
9934 }
9935 }
9936
9937 /* Set the congestion initial signal and fpin values. */
9938 phba->cgn_init_reg_fpin = phba->cgn_reg_fpin;
9939 phba->cgn_init_reg_signal = phba->cgn_reg_signal;
9940
9941 lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
9942 "6446 READ_CONFIG reg_sig x%x reg_fpin:x%x\n",
9943 phba->cgn_reg_signal, phba->cgn_reg_fpin);
9944
James Smart83c6cb12019-10-18 14:18:30 -07009945 lpfc_map_topology(phba, rd_config);
James Smartda0436e2009-05-22 14:51:39 -04009946 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart6d368e52011-05-24 11:44:12 -04009947 "2003 cfg params Extents? %d "
9948 "XRI(B:%d M:%d), "
James Smartda0436e2009-05-22 14:51:39 -04009949 "VPI(B:%d M:%d) "
9950 "VFI(B:%d M:%d) "
9951 "RPI(B:%d M:%d) "
Dick Kennedya1e4d3d2020-08-03 14:02:22 -07009952 "FCFI:%d EQ:%d CQ:%d WQ:%d RQ:%d lmt:x%x\n",
James Smart6d368e52011-05-24 11:44:12 -04009953 phba->sli4_hba.extents_in_use,
James Smartda0436e2009-05-22 14:51:39 -04009954 phba->sli4_hba.max_cfg_param.xri_base,
9955 phba->sli4_hba.max_cfg_param.max_xri,
9956 phba->sli4_hba.max_cfg_param.vpi_base,
9957 phba->sli4_hba.max_cfg_param.max_vpi,
9958 phba->sli4_hba.max_cfg_param.vfi_base,
9959 phba->sli4_hba.max_cfg_param.max_vfi,
9960 phba->sli4_hba.max_cfg_param.rpi_base,
9961 phba->sli4_hba.max_cfg_param.max_rpi,
James Smart2ea259e2017-02-12 13:52:27 -08009962 phba->sli4_hba.max_cfg_param.max_fcfi,
9963 phba->sli4_hba.max_cfg_param.max_eq,
9964 phba->sli4_hba.max_cfg_param.max_cq,
9965 phba->sli4_hba.max_cfg_param.max_wq,
Dick Kennedya1e4d3d2020-08-03 14:02:22 -07009966 phba->sli4_hba.max_cfg_param.max_rq,
9967 phba->lmt);
James Smart2ea259e2017-02-12 13:52:27 -08009968
James Smartd38f33b2018-05-04 20:37:54 -07009969 /*
James Smart6a828b02019-01-28 11:14:31 -08009970 * Calculate queue resources based on how
9971 * many WQ/CQ/EQs are available.
James Smartd38f33b2018-05-04 20:37:54 -07009972 */
James Smart6a828b02019-01-28 11:14:31 -08009973 qmin = phba->sli4_hba.max_cfg_param.max_wq;
9974 if (phba->sli4_hba.max_cfg_param.max_cq < qmin)
9975 qmin = phba->sli4_hba.max_cfg_param.max_cq;
9976 if (phba->sli4_hba.max_cfg_param.max_eq < qmin)
9977 qmin = phba->sli4_hba.max_cfg_param.max_eq;
9978 /*
9979 * Whats left after this can go toward NVME / FCP.
9980 * The minus 4 accounts for ELS, NVME LS, MBOX
9981 * plus one extra. When configured for
9982 * NVMET, FCP io channel WQs are not created.
9983 */
9984 qmin -= 4;
James Smartd38f33b2018-05-04 20:37:54 -07009985
James Smart6a828b02019-01-28 11:14:31 -08009986 /* Check to see if there is enough for NVME */
9987 if ((phba->cfg_irq_chann > qmin) ||
9988 (phba->cfg_hdw_queue > qmin)) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009989 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
Dick Kennedy9e3e3652020-08-03 14:02:23 -07009990 "2005 Reducing Queues - "
9991 "FW resource limitation: "
James Smart6a828b02019-01-28 11:14:31 -08009992 "WQ %d CQ %d EQ %d: min %d: "
9993 "IRQ %d HDWQ %d\n",
James Smartd38f33b2018-05-04 20:37:54 -07009994 phba->sli4_hba.max_cfg_param.max_wq,
9995 phba->sli4_hba.max_cfg_param.max_cq,
James Smart6a828b02019-01-28 11:14:31 -08009996 phba->sli4_hba.max_cfg_param.max_eq,
9997 qmin, phba->cfg_irq_chann,
James Smartcdb42be2019-01-28 11:14:21 -08009998 phba->cfg_hdw_queue);
James Smartd38f33b2018-05-04 20:37:54 -07009999
James Smart6a828b02019-01-28 11:14:31 -080010000 if (phba->cfg_irq_chann > qmin)
10001 phba->cfg_irq_chann = qmin;
10002 if (phba->cfg_hdw_queue > qmin)
10003 phba->cfg_hdw_queue = qmin;
James Smartd38f33b2018-05-04 20:37:54 -070010004 }
James Smartda0436e2009-05-22 14:51:39 -040010005 }
James Smart912e3ac2011-05-24 11:42:11 -040010006
10007 if (rc)
10008 goto read_cfg_out;
James Smartda0436e2009-05-22 14:51:39 -040010009
James Smartc6918162016-10-13 15:06:16 -070010010 /* Update link speed if forced link speed is supported */
10011 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
James Smart27d6ac02018-02-22 08:18:42 -080010012 if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
James Smartc6918162016-10-13 15:06:16 -070010013 forced_link_speed =
10014 bf_get(lpfc_mbx_rd_conf_link_speed, rd_config);
10015 if (forced_link_speed) {
10016 phba->hba_flag |= HBA_FORCED_LINK_SPEED;
10017
10018 switch (forced_link_speed) {
10019 case LINK_SPEED_1G:
10020 phba->cfg_link_speed =
10021 LPFC_USER_LINK_SPEED_1G;
10022 break;
10023 case LINK_SPEED_2G:
10024 phba->cfg_link_speed =
10025 LPFC_USER_LINK_SPEED_2G;
10026 break;
10027 case LINK_SPEED_4G:
10028 phba->cfg_link_speed =
10029 LPFC_USER_LINK_SPEED_4G;
10030 break;
10031 case LINK_SPEED_8G:
10032 phba->cfg_link_speed =
10033 LPFC_USER_LINK_SPEED_8G;
10034 break;
10035 case LINK_SPEED_10G:
10036 phba->cfg_link_speed =
10037 LPFC_USER_LINK_SPEED_10G;
10038 break;
10039 case LINK_SPEED_16G:
10040 phba->cfg_link_speed =
10041 LPFC_USER_LINK_SPEED_16G;
10042 break;
10043 case LINK_SPEED_32G:
10044 phba->cfg_link_speed =
10045 LPFC_USER_LINK_SPEED_32G;
10046 break;
James Smartfbd8a6b2018-02-22 08:18:45 -080010047 case LINK_SPEED_64G:
10048 phba->cfg_link_speed =
10049 LPFC_USER_LINK_SPEED_64G;
10050 break;
James Smartc6918162016-10-13 15:06:16 -070010051 case 0xffff:
10052 phba->cfg_link_speed =
10053 LPFC_USER_LINK_SPEED_AUTO;
10054 break;
10055 default:
Dick Kennedy372c1872020-06-30 14:50:00 -070010056 lpfc_printf_log(phba, KERN_ERR,
10057 LOG_TRACE_EVENT,
James Smartc6918162016-10-13 15:06:16 -070010058 "0047 Unrecognized link "
10059 "speed : %d\n",
10060 forced_link_speed);
10061 phba->cfg_link_speed =
10062 LPFC_USER_LINK_SPEED_AUTO;
10063 }
10064 }
10065 }
10066
James Smartda0436e2009-05-22 14:51:39 -040010067 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
James Smart572709e2013-07-15 18:32:43 -040010068 length = phba->sli4_hba.max_cfg_param.max_xri -
10069 lpfc_sli4_get_els_iocb_cnt(phba);
10070 if (phba->cfg_hba_queue_depth > length) {
10071 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
10072 "3361 HBA queue depth changed from %d to %d\n",
10073 phba->cfg_hba_queue_depth, length);
10074 phba->cfg_hba_queue_depth = length;
10075 }
James Smart912e3ac2011-05-24 11:42:11 -040010076
James Smart27d6ac02018-02-22 08:18:42 -080010077 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) <
James Smart912e3ac2011-05-24 11:42:11 -040010078 LPFC_SLI_INTF_IF_TYPE_2)
10079 goto read_cfg_out;
10080
10081 /* get the pf# and vf# for SLI4 if_type 2 port */
10082 length = (sizeof(struct lpfc_mbx_get_func_cfg) -
10083 sizeof(struct lpfc_sli4_cfg_mhdr));
10084 lpfc_sli4_config(phba, pmb, LPFC_MBOX_SUBSYSTEM_COMMON,
10085 LPFC_MBOX_OPCODE_GET_FUNCTION_CONFIG,
10086 length, LPFC_SLI4_MBX_EMBED);
10087
James Smart8aa134a2012-08-14 14:25:29 -040010088 rc2 = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart912e3ac2011-05-24 11:42:11 -040010089 shdr = (union lpfc_sli4_cfg_shdr *)
10090 &pmb->u.mqe.un.sli4_config.header.cfg_shdr;
10091 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10092 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
James Smart8aa134a2012-08-14 14:25:29 -040010093 if (rc2 || shdr_status || shdr_add_status) {
Dick Kennedy372c1872020-06-30 14:50:00 -070010094 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart912e3ac2011-05-24 11:42:11 -040010095 "3026 Mailbox failed , mbxCmd x%x "
10096 "GET_FUNCTION_CONFIG, mbxStatus x%x\n",
10097 bf_get(lpfc_mqe_command, &pmb->u.mqe),
10098 bf_get(lpfc_mqe_status, &pmb->u.mqe));
James Smart912e3ac2011-05-24 11:42:11 -040010099 goto read_cfg_out;
10100 }
10101
10102 /* search for fc_fcoe resrouce descriptor */
10103 get_func_cfg = &pmb->u.mqe.un.get_func_cfg;
James Smart912e3ac2011-05-24 11:42:11 -040010104
James Smart8aa134a2012-08-14 14:25:29 -040010105 pdesc_0 = (char *)&get_func_cfg->func_cfg.desc[0];
10106 desc = (struct lpfc_rsrc_desc_fcfcoe *)pdesc_0;
10107 length = bf_get(lpfc_rsrc_desc_fcfcoe_length, desc);
10108 if (length == LPFC_RSRC_DESC_TYPE_FCFCOE_V0_RSVD)
10109 length = LPFC_RSRC_DESC_TYPE_FCFCOE_V0_LENGTH;
10110 else if (length != LPFC_RSRC_DESC_TYPE_FCFCOE_V1_LENGTH)
10111 goto read_cfg_out;
10112
James Smart912e3ac2011-05-24 11:42:11 -040010113 for (i = 0; i < LPFC_RSRC_DESC_MAX_NUM; i++) {
James Smart8aa134a2012-08-14 14:25:29 -040010114 desc = (struct lpfc_rsrc_desc_fcfcoe *)(pdesc_0 + length * i);
James Smart912e3ac2011-05-24 11:42:11 -040010115 if (LPFC_RSRC_DESC_TYPE_FCFCOE ==
James Smart8aa134a2012-08-14 14:25:29 -040010116 bf_get(lpfc_rsrc_desc_fcfcoe_type, desc)) {
James Smart912e3ac2011-05-24 11:42:11 -040010117 phba->sli4_hba.iov.pf_number =
10118 bf_get(lpfc_rsrc_desc_fcfcoe_pfnum, desc);
10119 phba->sli4_hba.iov.vf_number =
10120 bf_get(lpfc_rsrc_desc_fcfcoe_vfnum, desc);
10121 break;
10122 }
10123 }
10124
10125 if (i < LPFC_RSRC_DESC_MAX_NUM)
10126 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
10127 "3027 GET_FUNCTION_CONFIG: pf_number:%d, "
10128 "vf_number:%d\n", phba->sli4_hba.iov.pf_number,
10129 phba->sli4_hba.iov.vf_number);
James Smart8aa134a2012-08-14 14:25:29 -040010130 else
Dick Kennedy372c1872020-06-30 14:50:00 -070010131 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart912e3ac2011-05-24 11:42:11 -040010132 "3028 GET_FUNCTION_CONFIG: failed to find "
Colin Ian Kingc4dba182018-10-16 18:28:53 +010010133 "Resource Descriptor:x%x\n",
James Smart912e3ac2011-05-24 11:42:11 -040010134 LPFC_RSRC_DESC_TYPE_FCFCOE);
James Smart912e3ac2011-05-24 11:42:11 -040010135
10136read_cfg_out:
10137 mempool_free(pmb, phba->mbox_mem_pool);
James Smartda0436e2009-05-22 14:51:39 -040010138 return rc;
10139}
10140
10141/**
James Smart2fcee4b2010-12-15 17:57:46 -050010142 * lpfc_setup_endian_order - Write endian order to an SLI4 if_type 0 port.
James Smartda0436e2009-05-22 14:51:39 -040010143 * @phba: pointer to lpfc hba data structure.
10144 *
James Smart2fcee4b2010-12-15 17:57:46 -050010145 * This routine is invoked to setup the port-side endian order when
10146 * the port if_type is 0. This routine has no function for other
10147 * if_types.
James Smartda0436e2009-05-22 14:51:39 -040010148 *
10149 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020010150 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010151 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -040010152 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -040010153 **/
10154static int
10155lpfc_setup_endian_order(struct lpfc_hba *phba)
10156{
10157 LPFC_MBOXQ_t *mboxq;
James Smart2fcee4b2010-12-15 17:57:46 -050010158 uint32_t if_type, rc = 0;
James Smartda0436e2009-05-22 14:51:39 -040010159 uint32_t endian_mb_data[2] = {HOST_ENDIAN_LOW_WORD0,
10160 HOST_ENDIAN_HIGH_WORD1};
10161
James Smart2fcee4b2010-12-15 17:57:46 -050010162 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
10163 switch (if_type) {
10164 case LPFC_SLI_INTF_IF_TYPE_0:
10165 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
10166 GFP_KERNEL);
10167 if (!mboxq) {
Dick Kennedy372c1872020-06-30 14:50:00 -070010168 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2fcee4b2010-12-15 17:57:46 -050010169 "0492 Unable to allocate memory for "
10170 "issuing SLI_CONFIG_SPECIAL mailbox "
10171 "command\n");
10172 return -ENOMEM;
10173 }
James Smartda0436e2009-05-22 14:51:39 -040010174
James Smart2fcee4b2010-12-15 17:57:46 -050010175 /*
10176 * The SLI4_CONFIG_SPECIAL mailbox command requires the first
10177 * two words to contain special data values and no other data.
10178 */
10179 memset(mboxq, 0, sizeof(LPFC_MBOXQ_t));
10180 memcpy(&mboxq->u.mqe, &endian_mb_data, sizeof(endian_mb_data));
10181 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
10182 if (rc != MBX_SUCCESS) {
Dick Kennedy372c1872020-06-30 14:50:00 -070010183 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2fcee4b2010-12-15 17:57:46 -050010184 "0493 SLI_CONFIG_SPECIAL mailbox "
10185 "failed with status x%x\n",
10186 rc);
10187 rc = -EIO;
10188 }
10189 mempool_free(mboxq, phba->mbox_mem_pool);
10190 break;
James Smart27d6ac02018-02-22 08:18:42 -080010191 case LPFC_SLI_INTF_IF_TYPE_6:
James Smart2fcee4b2010-12-15 17:57:46 -050010192 case LPFC_SLI_INTF_IF_TYPE_2:
10193 case LPFC_SLI_INTF_IF_TYPE_1:
10194 default:
10195 break;
James Smartda0436e2009-05-22 14:51:39 -040010196 }
James Smartda0436e2009-05-22 14:51:39 -040010197 return rc;
10198}
10199
10200/**
James Smart895427b2017-02-12 13:52:30 -080010201 * lpfc_sli4_queue_verify - Verify and update EQ counts
James Smartda0436e2009-05-22 14:51:39 -040010202 * @phba: pointer to lpfc hba data structure.
10203 *
James Smart895427b2017-02-12 13:52:30 -080010204 * This routine is invoked to check the user settable queue counts for EQs.
10205 * After this routine is called the counts will be set to valid values that
James Smart5350d872011-10-10 21:33:49 -040010206 * adhere to the constraints of the system's interrupt vectors and the port's
10207 * queue resources.
James Smartda0436e2009-05-22 14:51:39 -040010208 *
10209 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020010210 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010211 * -ENOMEM - No available memory
James Smartda0436e2009-05-22 14:51:39 -040010212 **/
10213static int
James Smart5350d872011-10-10 21:33:49 -040010214lpfc_sli4_queue_verify(struct lpfc_hba *phba)
James Smartda0436e2009-05-22 14:51:39 -040010215{
James Smartda0436e2009-05-22 14:51:39 -040010216 /*
James Smart67d12732012-08-03 12:36:13 -040010217 * Sanity check for configured queue parameters against the run-time
James Smartda0436e2009-05-22 14:51:39 -040010218 * device parameters
10219 */
10220
James Smartbcb24f62017-11-20 16:00:36 -080010221 if (phba->nvmet_support) {
James Smart97a9ed32019-10-18 14:18:17 -070010222 if (phba->cfg_hdw_queue < phba->cfg_nvmet_mrq)
10223 phba->cfg_nvmet_mrq = phba->cfg_hdw_queue;
James Smart982ab122019-03-12 16:30:10 -070010224 if (phba->cfg_nvmet_mrq > LPFC_NVMET_MRQ_MAX)
10225 phba->cfg_nvmet_mrq = LPFC_NVMET_MRQ_MAX;
James Smartbcb24f62017-11-20 16:00:36 -080010226 }
James Smart895427b2017-02-12 13:52:30 -080010227
10228 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart6a828b02019-01-28 11:14:31 -080010229 "2574 IO channels: hdwQ %d IRQ %d MRQ: %d\n",
10230 phba->cfg_hdw_queue, phba->cfg_irq_chann,
10231 phba->cfg_nvmet_mrq);
James Smartda0436e2009-05-22 14:51:39 -040010232
James Smartda0436e2009-05-22 14:51:39 -040010233 /* Get EQ depth from module parameter, fake the default for now */
10234 phba->sli4_hba.eq_esize = LPFC_EQE_SIZE_4B;
10235 phba->sli4_hba.eq_ecount = LPFC_EQE_DEF_COUNT;
10236
James Smart5350d872011-10-10 21:33:49 -040010237 /* Get CQ depth from module parameter, fake the default for now */
10238 phba->sli4_hba.cq_esize = LPFC_CQE_SIZE;
10239 phba->sli4_hba.cq_ecount = LPFC_CQE_DEF_COUNT;
James Smart5350d872011-10-10 21:33:49 -040010240 return 0;
James Smart895427b2017-02-12 13:52:30 -080010241}
10242
10243static int
James Smartc00f62e2019-08-14 16:57:11 -070010244lpfc_alloc_io_wq_cq(struct lpfc_hba *phba, int idx)
James Smart895427b2017-02-12 13:52:30 -080010245{
10246 struct lpfc_queue *qdesc;
James Smartc00f62e2019-08-14 16:57:11 -070010247 u32 wqesize;
James Smartc1a21eb2019-03-12 16:30:29 -070010248 int cpu;
James Smart895427b2017-02-12 13:52:30 -080010249
James Smartc00f62e2019-08-14 16:57:11 -070010250 cpu = lpfc_find_cpu_handle(phba, idx, LPFC_FIND_BY_HDWQ);
10251 /* Create Fast Path IO CQs */
James Smartc176ffa2018-01-30 15:58:46 -080010252 if (phba->enab_exp_wqcq_pages)
James Smarta51e41b2017-12-08 17:18:06 -080010253 /* Increase the CQ size when WQEs contain an embedded cdb */
10254 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_EXPANDED_PAGE_SIZE,
10255 phba->sli4_hba.cq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -070010256 LPFC_CQE_EXP_COUNT, cpu);
James Smarta51e41b2017-12-08 17:18:06 -080010257
10258 else
10259 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
10260 phba->sli4_hba.cq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -070010261 phba->sli4_hba.cq_ecount, cpu);
James Smart895427b2017-02-12 13:52:30 -080010262 if (!qdesc) {
Dick Kennedy372c1872020-06-30 14:50:00 -070010263 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
10264 "0499 Failed allocate fast-path IO CQ (%d)\n",
10265 idx);
James Smart895427b2017-02-12 13:52:30 -080010266 return 1;
10267 }
James Smart7365f6f2018-02-22 08:18:46 -080010268 qdesc->qe_valid = 1;
James Smartc00f62e2019-08-14 16:57:11 -070010269 qdesc->hdwq = idx;
James Smartc1a21eb2019-03-12 16:30:29 -070010270 qdesc->chann = cpu;
James Smartc00f62e2019-08-14 16:57:11 -070010271 phba->sli4_hba.hdwq[idx].io_cq = qdesc;
James Smart895427b2017-02-12 13:52:30 -080010272
James Smartc00f62e2019-08-14 16:57:11 -070010273 /* Create Fast Path IO WQs */
James Smartc176ffa2018-01-30 15:58:46 -080010274 if (phba->enab_exp_wqcq_pages) {
James Smarta51e41b2017-12-08 17:18:06 -080010275 /* Increase the WQ size when WQEs contain an embedded cdb */
James Smartc176ffa2018-01-30 15:58:46 -080010276 wqesize = (phba->fcp_embed_io) ?
10277 LPFC_WQE128_SIZE : phba->sli4_hba.wq_esize;
James Smarta51e41b2017-12-08 17:18:06 -080010278 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_EXPANDED_PAGE_SIZE,
James Smartc176ffa2018-01-30 15:58:46 -080010279 wqesize,
James Smartc1a21eb2019-03-12 16:30:29 -070010280 LPFC_WQE_EXP_COUNT, cpu);
James Smartc176ffa2018-01-30 15:58:46 -080010281 } else
James Smarta51e41b2017-12-08 17:18:06 -080010282 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
10283 phba->sli4_hba.wq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -070010284 phba->sli4_hba.wq_ecount, cpu);
James Smartc176ffa2018-01-30 15:58:46 -080010285
James Smart895427b2017-02-12 13:52:30 -080010286 if (!qdesc) {
Dick Kennedy372c1872020-06-30 14:50:00 -070010287 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartc00f62e2019-08-14 16:57:11 -070010288 "0503 Failed allocate fast-path IO WQ (%d)\n",
10289 idx);
James Smart895427b2017-02-12 13:52:30 -080010290 return 1;
10291 }
James Smartc00f62e2019-08-14 16:57:11 -070010292 qdesc->hdwq = idx;
10293 qdesc->chann = cpu;
10294 phba->sli4_hba.hdwq[idx].io_wq = qdesc;
James Smart895427b2017-02-12 13:52:30 -080010295 list_add_tail(&qdesc->wq_list, &phba->sli4_hba.lpfc_wq_list);
10296 return 0;
James Smart5350d872011-10-10 21:33:49 -040010297}
10298
10299/**
10300 * lpfc_sli4_queue_create - Create all the SLI4 queues
10301 * @phba: pointer to lpfc hba data structure.
10302 *
10303 * This routine is invoked to allocate all the SLI4 queues for the FCoE HBA
10304 * operation. For each SLI4 queue type, the parameters such as queue entry
10305 * count (queue depth) shall be taken from the module parameter. For now,
10306 * we just use some constant number as place holder.
10307 *
10308 * Return codes
Anatol Pomozov4907cb72012-09-01 10:31:09 -070010309 * 0 - successful
James Smart5350d872011-10-10 21:33:49 -040010310 * -ENOMEM - No availble memory
10311 * -EIO - The mailbox failed to complete successfully.
10312 **/
10313int
10314lpfc_sli4_queue_create(struct lpfc_hba *phba)
10315{
10316 struct lpfc_queue *qdesc;
James Smart657add42019-05-21 17:49:06 -070010317 int idx, cpu, eqcpu;
James Smart5e5b5112019-01-28 11:14:22 -080010318 struct lpfc_sli4_hdw_queue *qp;
James Smart657add42019-05-21 17:49:06 -070010319 struct lpfc_vector_map_info *cpup;
10320 struct lpfc_vector_map_info *eqcpup;
James Smart32517fc2019-01-28 11:14:33 -080010321 struct lpfc_eq_intr_info *eqi;
James Smart5350d872011-10-10 21:33:49 -040010322
10323 /*
James Smart67d12732012-08-03 12:36:13 -040010324 * Create HBA Record arrays.
James Smart895427b2017-02-12 13:52:30 -080010325 * Both NVME and FCP will share that same vectors / EQs
James Smart5350d872011-10-10 21:33:49 -040010326 */
James Smart67d12732012-08-03 12:36:13 -040010327 phba->sli4_hba.mq_esize = LPFC_MQE_SIZE;
10328 phba->sli4_hba.mq_ecount = LPFC_MQE_DEF_COUNT;
10329 phba->sli4_hba.wq_esize = LPFC_WQE_SIZE;
10330 phba->sli4_hba.wq_ecount = LPFC_WQE_DEF_COUNT;
10331 phba->sli4_hba.rq_esize = LPFC_RQE_SIZE;
10332 phba->sli4_hba.rq_ecount = LPFC_RQE_DEF_COUNT;
James Smart895427b2017-02-12 13:52:30 -080010333 phba->sli4_hba.eq_esize = LPFC_EQE_SIZE_4B;
10334 phba->sli4_hba.eq_ecount = LPFC_EQE_DEF_COUNT;
10335 phba->sli4_hba.cq_esize = LPFC_CQE_SIZE;
10336 phba->sli4_hba.cq_ecount = LPFC_CQE_DEF_COUNT;
James Smart67d12732012-08-03 12:36:13 -040010337
James Smartcdb42be2019-01-28 11:14:21 -080010338 if (!phba->sli4_hba.hdwq) {
James Smart5e5b5112019-01-28 11:14:22 -080010339 phba->sli4_hba.hdwq = kcalloc(
10340 phba->cfg_hdw_queue, sizeof(struct lpfc_sli4_hdw_queue),
10341 GFP_KERNEL);
10342 if (!phba->sli4_hba.hdwq) {
Dick Kennedy372c1872020-06-30 14:50:00 -070010343 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart5e5b5112019-01-28 11:14:22 -080010344 "6427 Failed allocate memory for "
10345 "fast-path Hardware Queue array\n");
James Smart895427b2017-02-12 13:52:30 -080010346 goto out_error;
10347 }
James Smart5e5b5112019-01-28 11:14:22 -080010348 /* Prepare hardware queues to take IO buffers */
10349 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
10350 qp = &phba->sli4_hba.hdwq[idx];
10351 spin_lock_init(&qp->io_buf_list_get_lock);
10352 spin_lock_init(&qp->io_buf_list_put_lock);
10353 INIT_LIST_HEAD(&qp->lpfc_io_buf_list_get);
10354 INIT_LIST_HEAD(&qp->lpfc_io_buf_list_put);
10355 qp->get_io_bufs = 0;
10356 qp->put_io_bufs = 0;
10357 qp->total_io_bufs = 0;
James Smartc00f62e2019-08-14 16:57:11 -070010358 spin_lock_init(&qp->abts_io_buf_list_lock);
10359 INIT_LIST_HEAD(&qp->lpfc_abts_io_buf_list);
James Smart5e5b5112019-01-28 11:14:22 -080010360 qp->abts_scsi_io_bufs = 0;
James Smart5e5b5112019-01-28 11:14:22 -080010361 qp->abts_nvme_io_bufs = 0;
James Smartd79c9e92019-08-14 16:57:09 -070010362 INIT_LIST_HEAD(&qp->sgl_list);
10363 INIT_LIST_HEAD(&qp->cmd_rsp_buf_list);
10364 spin_lock_init(&qp->hdwq_lock);
James Smart895427b2017-02-12 13:52:30 -080010365 }
James Smart67d12732012-08-03 12:36:13 -040010366 }
10367
James Smartcdb42be2019-01-28 11:14:21 -080010368 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
James Smart2d7dbc42017-02-12 13:52:35 -080010369 if (phba->nvmet_support) {
10370 phba->sli4_hba.nvmet_cqset = kcalloc(
10371 phba->cfg_nvmet_mrq,
10372 sizeof(struct lpfc_queue *),
10373 GFP_KERNEL);
10374 if (!phba->sli4_hba.nvmet_cqset) {
Dick Kennedy372c1872020-06-30 14:50:00 -070010375 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2d7dbc42017-02-12 13:52:35 -080010376 "3121 Fail allocate memory for "
10377 "fast-path CQ set array\n");
10378 goto out_error;
10379 }
10380 phba->sli4_hba.nvmet_mrq_hdr = kcalloc(
10381 phba->cfg_nvmet_mrq,
10382 sizeof(struct lpfc_queue *),
10383 GFP_KERNEL);
10384 if (!phba->sli4_hba.nvmet_mrq_hdr) {
Dick Kennedy372c1872020-06-30 14:50:00 -070010385 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2d7dbc42017-02-12 13:52:35 -080010386 "3122 Fail allocate memory for "
10387 "fast-path RQ set hdr array\n");
10388 goto out_error;
10389 }
10390 phba->sli4_hba.nvmet_mrq_data = kcalloc(
10391 phba->cfg_nvmet_mrq,
10392 sizeof(struct lpfc_queue *),
10393 GFP_KERNEL);
10394 if (!phba->sli4_hba.nvmet_mrq_data) {
Dick Kennedy372c1872020-06-30 14:50:00 -070010395 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2d7dbc42017-02-12 13:52:35 -080010396 "3124 Fail allocate memory for "
10397 "fast-path RQ set data array\n");
10398 goto out_error;
10399 }
10400 }
James Smart67d12732012-08-03 12:36:13 -040010401 }
James Smartda0436e2009-05-22 14:51:39 -040010402
James Smart895427b2017-02-12 13:52:30 -080010403 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_wq_list);
James Smart67d12732012-08-03 12:36:13 -040010404
James Smart895427b2017-02-12 13:52:30 -080010405 /* Create HBA Event Queues (EQs) */
James Smart657add42019-05-21 17:49:06 -070010406 for_each_present_cpu(cpu) {
10407 /* We only want to create 1 EQ per vector, even though
10408 * multiple CPUs might be using that vector. so only
10409 * selects the CPUs that are LPFC_CPU_FIRST_IRQ.
James Smart6a828b02019-01-28 11:14:31 -080010410 */
James Smart657add42019-05-21 17:49:06 -070010411 cpup = &phba->sli4_hba.cpu_map[cpu];
10412 if (!(cpup->flag & LPFC_CPU_FIRST_IRQ))
James Smart6a828b02019-01-28 11:14:31 -080010413 continue;
James Smart657add42019-05-21 17:49:06 -070010414
10415 /* Get a ptr to the Hardware Queue associated with this CPU */
10416 qp = &phba->sli4_hba.hdwq[cpup->hdwq];
10417
10418 /* Allocate an EQ */
James Smart81b96ed2017-11-20 16:00:29 -080010419 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
10420 phba->sli4_hba.eq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -070010421 phba->sli4_hba.eq_ecount, cpu);
James Smartda0436e2009-05-22 14:51:39 -040010422 if (!qdesc) {
Dick Kennedy372c1872020-06-30 14:50:00 -070010423 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart657add42019-05-21 17:49:06 -070010424 "0497 Failed allocate EQ (%d)\n",
10425 cpup->hdwq);
James Smart67d12732012-08-03 12:36:13 -040010426 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -040010427 }
James Smart7365f6f2018-02-22 08:18:46 -080010428 qdesc->qe_valid = 1;
James Smart657add42019-05-21 17:49:06 -070010429 qdesc->hdwq = cpup->hdwq;
James Smart3ad348d2019-08-14 16:56:43 -070010430 qdesc->chann = cpu; /* First CPU this EQ is affinitized to */
James Smart32517fc2019-01-28 11:14:33 -080010431 qdesc->last_cpu = qdesc->chann;
James Smart657add42019-05-21 17:49:06 -070010432
10433 /* Save the allocated EQ in the Hardware Queue */
10434 qp->hba_eq = qdesc;
10435
James Smart32517fc2019-01-28 11:14:33 -080010436 eqi = per_cpu_ptr(phba->sli4_hba.eq_info, qdesc->last_cpu);
10437 list_add(&qdesc->cpu_list, &eqi->list);
James Smartda0436e2009-05-22 14:51:39 -040010438 }
10439
James Smart657add42019-05-21 17:49:06 -070010440 /* Now we need to populate the other Hardware Queues, that share
10441 * an IRQ vector, with the associated EQ ptr.
10442 */
10443 for_each_present_cpu(cpu) {
10444 cpup = &phba->sli4_hba.cpu_map[cpu];
10445
10446 /* Check for EQ already allocated in previous loop */
10447 if (cpup->flag & LPFC_CPU_FIRST_IRQ)
10448 continue;
10449
10450 /* Check for multiple CPUs per hdwq */
10451 qp = &phba->sli4_hba.hdwq[cpup->hdwq];
10452 if (qp->hba_eq)
10453 continue;
10454
10455 /* We need to share an EQ for this hdwq */
10456 eqcpu = lpfc_find_cpu_handle(phba, cpup->eq, LPFC_FIND_BY_EQ);
10457 eqcpup = &phba->sli4_hba.cpu_map[eqcpu];
10458 qp->hba_eq = phba->sli4_hba.hdwq[eqcpup->hdwq].hba_eq;
10459 }
James Smart895427b2017-02-12 13:52:30 -080010460
James Smartc00f62e2019-08-14 16:57:11 -070010461 /* Allocate IO Path SLI4 CQ/WQs */
James Smart6a828b02019-01-28 11:14:31 -080010462 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
James Smartc00f62e2019-08-14 16:57:11 -070010463 if (lpfc_alloc_io_wq_cq(phba, idx))
James Smart895427b2017-02-12 13:52:30 -080010464 goto out_error;
James Smart6a828b02019-01-28 11:14:31 -080010465 }
James Smart895427b2017-02-12 13:52:30 -080010466
James Smartc00f62e2019-08-14 16:57:11 -070010467 if (phba->nvmet_support) {
10468 for (idx = 0; idx < phba->cfg_nvmet_mrq; idx++) {
10469 cpu = lpfc_find_cpu_handle(phba, idx,
10470 LPFC_FIND_BY_HDWQ);
10471 qdesc = lpfc_sli4_queue_alloc(phba,
James Smart81b96ed2017-11-20 16:00:29 -080010472 LPFC_DEFAULT_PAGE_SIZE,
10473 phba->sli4_hba.cq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -070010474 phba->sli4_hba.cq_ecount,
10475 cpu);
James Smartc00f62e2019-08-14 16:57:11 -070010476 if (!qdesc) {
Dick Kennedy372c1872020-06-30 14:50:00 -070010477 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartcdb42be2019-01-28 11:14:21 -080010478 "3142 Failed allocate NVME "
10479 "CQ Set (%d)\n", idx);
James Smartc00f62e2019-08-14 16:57:11 -070010480 goto out_error;
James Smart2d7dbc42017-02-12 13:52:35 -080010481 }
James Smartc00f62e2019-08-14 16:57:11 -070010482 qdesc->qe_valid = 1;
10483 qdesc->hdwq = idx;
10484 qdesc->chann = cpu;
10485 phba->sli4_hba.nvmet_cqset[idx] = qdesc;
James Smart2d7dbc42017-02-12 13:52:35 -080010486 }
10487 }
10488
James Smartda0436e2009-05-22 14:51:39 -040010489 /*
James Smart67d12732012-08-03 12:36:13 -040010490 * Create Slow Path Completion Queues (CQs)
James Smartda0436e2009-05-22 14:51:39 -040010491 */
10492
James Smartc1a21eb2019-03-12 16:30:29 -070010493 cpu = lpfc_find_cpu_handle(phba, 0, LPFC_FIND_BY_EQ);
James Smartda0436e2009-05-22 14:51:39 -040010494 /* Create slow-path Mailbox Command Complete Queue */
James Smart81b96ed2017-11-20 16:00:29 -080010495 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
10496 phba->sli4_hba.cq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -070010497 phba->sli4_hba.cq_ecount, cpu);
James Smartda0436e2009-05-22 14:51:39 -040010498 if (!qdesc) {
Dick Kennedy372c1872020-06-30 14:50:00 -070010499 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartda0436e2009-05-22 14:51:39 -040010500 "0500 Failed allocate slow-path mailbox CQ\n");
James Smart67d12732012-08-03 12:36:13 -040010501 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -040010502 }
James Smart7365f6f2018-02-22 08:18:46 -080010503 qdesc->qe_valid = 1;
James Smartda0436e2009-05-22 14:51:39 -040010504 phba->sli4_hba.mbx_cq = qdesc;
10505
10506 /* Create slow-path ELS Complete Queue */
James Smart81b96ed2017-11-20 16:00:29 -080010507 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
10508 phba->sli4_hba.cq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -070010509 phba->sli4_hba.cq_ecount, cpu);
James Smartda0436e2009-05-22 14:51:39 -040010510 if (!qdesc) {
Dick Kennedy372c1872020-06-30 14:50:00 -070010511 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartda0436e2009-05-22 14:51:39 -040010512 "0501 Failed allocate slow-path ELS CQ\n");
James Smart67d12732012-08-03 12:36:13 -040010513 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -040010514 }
James Smart7365f6f2018-02-22 08:18:46 -080010515 qdesc->qe_valid = 1;
James Smartc00f62e2019-08-14 16:57:11 -070010516 qdesc->chann = cpu;
James Smartda0436e2009-05-22 14:51:39 -040010517 phba->sli4_hba.els_cq = qdesc;
10518
James Smartda0436e2009-05-22 14:51:39 -040010519
James Smart5350d872011-10-10 21:33:49 -040010520 /*
James Smart67d12732012-08-03 12:36:13 -040010521 * Create Slow Path Work Queues (WQs)
James Smart5350d872011-10-10 21:33:49 -040010522 */
James Smartda0436e2009-05-22 14:51:39 -040010523
10524 /* Create Mailbox Command Queue */
James Smartda0436e2009-05-22 14:51:39 -040010525
James Smart81b96ed2017-11-20 16:00:29 -080010526 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
10527 phba->sli4_hba.mq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -070010528 phba->sli4_hba.mq_ecount, cpu);
James Smartda0436e2009-05-22 14:51:39 -040010529 if (!qdesc) {
Dick Kennedy372c1872020-06-30 14:50:00 -070010530 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartda0436e2009-05-22 14:51:39 -040010531 "0505 Failed allocate slow-path MQ\n");
James Smart67d12732012-08-03 12:36:13 -040010532 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -040010533 }
James Smartc00f62e2019-08-14 16:57:11 -070010534 qdesc->chann = cpu;
James Smartda0436e2009-05-22 14:51:39 -040010535 phba->sli4_hba.mbx_wq = qdesc;
10536
10537 /*
James Smart67d12732012-08-03 12:36:13 -040010538 * Create ELS Work Queues
James Smartda0436e2009-05-22 14:51:39 -040010539 */
James Smartda0436e2009-05-22 14:51:39 -040010540
10541 /* Create slow-path ELS Work Queue */
James Smart81b96ed2017-11-20 16:00:29 -080010542 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
10543 phba->sli4_hba.wq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -070010544 phba->sli4_hba.wq_ecount, cpu);
James Smartda0436e2009-05-22 14:51:39 -040010545 if (!qdesc) {
Dick Kennedy372c1872020-06-30 14:50:00 -070010546 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartda0436e2009-05-22 14:51:39 -040010547 "0504 Failed allocate slow-path ELS WQ\n");
James Smart67d12732012-08-03 12:36:13 -040010548 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -040010549 }
James Smartc00f62e2019-08-14 16:57:11 -070010550 qdesc->chann = cpu;
James Smartda0436e2009-05-22 14:51:39 -040010551 phba->sli4_hba.els_wq = qdesc;
James Smart895427b2017-02-12 13:52:30 -080010552 list_add_tail(&qdesc->wq_list, &phba->sli4_hba.lpfc_wq_list);
10553
10554 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
10555 /* Create NVME LS Complete Queue */
James Smart81b96ed2017-11-20 16:00:29 -080010556 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
10557 phba->sli4_hba.cq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -070010558 phba->sli4_hba.cq_ecount, cpu);
James Smart895427b2017-02-12 13:52:30 -080010559 if (!qdesc) {
Dick Kennedy372c1872020-06-30 14:50:00 -070010560 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart895427b2017-02-12 13:52:30 -080010561 "6079 Failed allocate NVME LS CQ\n");
10562 goto out_error;
10563 }
James Smartc00f62e2019-08-14 16:57:11 -070010564 qdesc->chann = cpu;
James Smart7365f6f2018-02-22 08:18:46 -080010565 qdesc->qe_valid = 1;
James Smart895427b2017-02-12 13:52:30 -080010566 phba->sli4_hba.nvmels_cq = qdesc;
10567
10568 /* Create NVME LS Work Queue */
James Smart81b96ed2017-11-20 16:00:29 -080010569 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
10570 phba->sli4_hba.wq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -070010571 phba->sli4_hba.wq_ecount, cpu);
James Smart895427b2017-02-12 13:52:30 -080010572 if (!qdesc) {
Dick Kennedy372c1872020-06-30 14:50:00 -070010573 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart895427b2017-02-12 13:52:30 -080010574 "6080 Failed allocate NVME LS WQ\n");
10575 goto out_error;
10576 }
James Smartc00f62e2019-08-14 16:57:11 -070010577 qdesc->chann = cpu;
James Smart895427b2017-02-12 13:52:30 -080010578 phba->sli4_hba.nvmels_wq = qdesc;
10579 list_add_tail(&qdesc->wq_list, &phba->sli4_hba.lpfc_wq_list);
10580 }
James Smartda0436e2009-05-22 14:51:39 -040010581
James Smartda0436e2009-05-22 14:51:39 -040010582 /*
10583 * Create Receive Queue (RQ)
10584 */
James Smartda0436e2009-05-22 14:51:39 -040010585
10586 /* Create Receive Queue for header */
James Smart81b96ed2017-11-20 16:00:29 -080010587 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
10588 phba->sli4_hba.rq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -070010589 phba->sli4_hba.rq_ecount, cpu);
James Smartda0436e2009-05-22 14:51:39 -040010590 if (!qdesc) {
Dick Kennedy372c1872020-06-30 14:50:00 -070010591 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartda0436e2009-05-22 14:51:39 -040010592 "0506 Failed allocate receive HRQ\n");
James Smart67d12732012-08-03 12:36:13 -040010593 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -040010594 }
10595 phba->sli4_hba.hdr_rq = qdesc;
10596
10597 /* Create Receive Queue for data */
James Smart81b96ed2017-11-20 16:00:29 -080010598 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
10599 phba->sli4_hba.rq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -070010600 phba->sli4_hba.rq_ecount, cpu);
James Smartda0436e2009-05-22 14:51:39 -040010601 if (!qdesc) {
Dick Kennedy372c1872020-06-30 14:50:00 -070010602 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartda0436e2009-05-22 14:51:39 -040010603 "0507 Failed allocate receive DRQ\n");
James Smart67d12732012-08-03 12:36:13 -040010604 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -040010605 }
10606 phba->sli4_hba.dat_rq = qdesc;
10607
James Smartcdb42be2019-01-28 11:14:21 -080010608 if ((phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) &&
10609 phba->nvmet_support) {
James Smart2d7dbc42017-02-12 13:52:35 -080010610 for (idx = 0; idx < phba->cfg_nvmet_mrq; idx++) {
James Smartc1a21eb2019-03-12 16:30:29 -070010611 cpu = lpfc_find_cpu_handle(phba, idx,
10612 LPFC_FIND_BY_HDWQ);
James Smart2d7dbc42017-02-12 13:52:35 -080010613 /* Create NVMET Receive Queue for header */
10614 qdesc = lpfc_sli4_queue_alloc(phba,
James Smart81b96ed2017-11-20 16:00:29 -080010615 LPFC_DEFAULT_PAGE_SIZE,
James Smart2d7dbc42017-02-12 13:52:35 -080010616 phba->sli4_hba.rq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -070010617 LPFC_NVMET_RQE_DEF_COUNT,
10618 cpu);
James Smart2d7dbc42017-02-12 13:52:35 -080010619 if (!qdesc) {
Dick Kennedy372c1872020-06-30 14:50:00 -070010620 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2d7dbc42017-02-12 13:52:35 -080010621 "3146 Failed allocate "
10622 "receive HRQ\n");
10623 goto out_error;
10624 }
James Smart5e5b5112019-01-28 11:14:22 -080010625 qdesc->hdwq = idx;
James Smart2d7dbc42017-02-12 13:52:35 -080010626 phba->sli4_hba.nvmet_mrq_hdr[idx] = qdesc;
10627
10628 /* Only needed for header of RQ pair */
James Smartc1a21eb2019-03-12 16:30:29 -070010629 qdesc->rqbp = kzalloc_node(sizeof(*qdesc->rqbp),
10630 GFP_KERNEL,
10631 cpu_to_node(cpu));
James Smart2d7dbc42017-02-12 13:52:35 -080010632 if (qdesc->rqbp == NULL) {
Dick Kennedy372c1872020-06-30 14:50:00 -070010633 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2d7dbc42017-02-12 13:52:35 -080010634 "6131 Failed allocate "
10635 "Header RQBP\n");
10636 goto out_error;
10637 }
10638
Dick Kennedy4b40d022017-08-23 16:55:38 -070010639 /* Put list in known state in case driver load fails. */
10640 INIT_LIST_HEAD(&qdesc->rqbp->rqb_buffer_list);
10641
James Smart2d7dbc42017-02-12 13:52:35 -080010642 /* Create NVMET Receive Queue for data */
10643 qdesc = lpfc_sli4_queue_alloc(phba,
James Smart81b96ed2017-11-20 16:00:29 -080010644 LPFC_DEFAULT_PAGE_SIZE,
James Smart2d7dbc42017-02-12 13:52:35 -080010645 phba->sli4_hba.rq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -070010646 LPFC_NVMET_RQE_DEF_COUNT,
10647 cpu);
James Smart2d7dbc42017-02-12 13:52:35 -080010648 if (!qdesc) {
Dick Kennedy372c1872020-06-30 14:50:00 -070010649 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2d7dbc42017-02-12 13:52:35 -080010650 "3156 Failed allocate "
10651 "receive DRQ\n");
10652 goto out_error;
10653 }
James Smart5e5b5112019-01-28 11:14:22 -080010654 qdesc->hdwq = idx;
James Smart2d7dbc42017-02-12 13:52:35 -080010655 phba->sli4_hba.nvmet_mrq_data[idx] = qdesc;
10656 }
10657 }
10658
James Smart4c47efc2019-01-28 11:14:25 -080010659 /* Clear NVME stats */
10660 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
10661 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
10662 memset(&phba->sli4_hba.hdwq[idx].nvme_cstat, 0,
10663 sizeof(phba->sli4_hba.hdwq[idx].nvme_cstat));
10664 }
10665 }
James Smart4c47efc2019-01-28 11:14:25 -080010666
10667 /* Clear SCSI stats */
10668 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) {
10669 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
10670 memset(&phba->sli4_hba.hdwq[idx].scsi_cstat, 0,
10671 sizeof(phba->sli4_hba.hdwq[idx].scsi_cstat));
10672 }
10673 }
10674
James Smartda0436e2009-05-22 14:51:39 -040010675 return 0;
10676
James Smartda0436e2009-05-22 14:51:39 -040010677out_error:
James Smart67d12732012-08-03 12:36:13 -040010678 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -040010679 return -ENOMEM;
10680}
10681
James Smart895427b2017-02-12 13:52:30 -080010682static inline void
10683__lpfc_sli4_release_queue(struct lpfc_queue **qp)
10684{
10685 if (*qp != NULL) {
10686 lpfc_sli4_queue_free(*qp);
10687 *qp = NULL;
10688 }
10689}
10690
10691static inline void
10692lpfc_sli4_release_queues(struct lpfc_queue ***qs, int max)
10693{
10694 int idx;
10695
10696 if (*qs == NULL)
10697 return;
10698
10699 for (idx = 0; idx < max; idx++)
10700 __lpfc_sli4_release_queue(&(*qs)[idx]);
10701
10702 kfree(*qs);
10703 *qs = NULL;
10704}
10705
10706static inline void
James Smart6a828b02019-01-28 11:14:31 -080010707lpfc_sli4_release_hdwq(struct lpfc_hba *phba)
James Smart895427b2017-02-12 13:52:30 -080010708{
James Smart6a828b02019-01-28 11:14:31 -080010709 struct lpfc_sli4_hdw_queue *hdwq;
James Smart657add42019-05-21 17:49:06 -070010710 struct lpfc_queue *eq;
James Smartcdb42be2019-01-28 11:14:21 -080010711 uint32_t idx;
10712
James Smart6a828b02019-01-28 11:14:31 -080010713 hdwq = phba->sli4_hba.hdwq;
James Smart6a828b02019-01-28 11:14:31 -080010714
James Smart657add42019-05-21 17:49:06 -070010715 /* Loop thru all Hardware Queues */
10716 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
10717 /* Free the CQ/WQ corresponding to the Hardware Queue */
James Smartc00f62e2019-08-14 16:57:11 -070010718 lpfc_sli4_queue_free(hdwq[idx].io_cq);
10719 lpfc_sli4_queue_free(hdwq[idx].io_wq);
James Smart821bc882020-01-27 16:23:05 -080010720 hdwq[idx].hba_eq = NULL;
James Smartc00f62e2019-08-14 16:57:11 -070010721 hdwq[idx].io_cq = NULL;
10722 hdwq[idx].io_wq = NULL;
James Smartd79c9e92019-08-14 16:57:09 -070010723 if (phba->cfg_xpsgl && !phba->nvmet_support)
10724 lpfc_free_sgl_per_hdwq(phba, &hdwq[idx]);
10725 lpfc_free_cmd_rsp_buf_per_hdwq(phba, &hdwq[idx]);
James Smart895427b2017-02-12 13:52:30 -080010726 }
James Smart657add42019-05-21 17:49:06 -070010727 /* Loop thru all IRQ vectors */
10728 for (idx = 0; idx < phba->cfg_irq_chann; idx++) {
10729 /* Free the EQ corresponding to the IRQ vector */
10730 eq = phba->sli4_hba.hba_eq_hdl[idx].eq;
10731 lpfc_sli4_queue_free(eq);
10732 phba->sli4_hba.hba_eq_hdl[idx].eq = NULL;
10733 }
James Smart895427b2017-02-12 13:52:30 -080010734}
10735
James Smartda0436e2009-05-22 14:51:39 -040010736/**
10737 * lpfc_sli4_queue_destroy - Destroy all the SLI4 queues
10738 * @phba: pointer to lpfc hba data structure.
10739 *
10740 * This routine is invoked to release all the SLI4 queues with the FCoE HBA
10741 * operation.
10742 *
10743 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020010744 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010745 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -040010746 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -040010747 **/
James Smart5350d872011-10-10 21:33:49 -040010748void
James Smartda0436e2009-05-22 14:51:39 -040010749lpfc_sli4_queue_destroy(struct lpfc_hba *phba)
10750{
James Smart4645f7b2019-03-12 16:30:14 -070010751 /*
10752 * Set FREE_INIT before beginning to free the queues.
10753 * Wait until the users of queues to acknowledge to
10754 * release queues by clearing FREE_WAIT.
10755 */
10756 spin_lock_irq(&phba->hbalock);
10757 phba->sli.sli_flag |= LPFC_QUEUE_FREE_INIT;
10758 while (phba->sli.sli_flag & LPFC_QUEUE_FREE_WAIT) {
10759 spin_unlock_irq(&phba->hbalock);
10760 msleep(20);
10761 spin_lock_irq(&phba->hbalock);
10762 }
10763 spin_unlock_irq(&phba->hbalock);
10764
James Smart93a4d6f2019-11-04 16:57:05 -080010765 lpfc_sli4_cleanup_poll_list(phba);
10766
James Smart895427b2017-02-12 13:52:30 -080010767 /* Release HBA eqs */
James Smartcdb42be2019-01-28 11:14:21 -080010768 if (phba->sli4_hba.hdwq)
James Smart6a828b02019-01-28 11:14:31 -080010769 lpfc_sli4_release_hdwq(phba);
James Smartda0436e2009-05-22 14:51:39 -040010770
James Smartbcb24f62017-11-20 16:00:36 -080010771 if (phba->nvmet_support) {
10772 lpfc_sli4_release_queues(&phba->sli4_hba.nvmet_cqset,
10773 phba->cfg_nvmet_mrq);
James Smart2d7dbc42017-02-12 13:52:35 -080010774
James Smartbcb24f62017-11-20 16:00:36 -080010775 lpfc_sli4_release_queues(&phba->sli4_hba.nvmet_mrq_hdr,
10776 phba->cfg_nvmet_mrq);
10777 lpfc_sli4_release_queues(&phba->sli4_hba.nvmet_mrq_data,
10778 phba->cfg_nvmet_mrq);
10779 }
James Smart2d7dbc42017-02-12 13:52:35 -080010780
James Smartda0436e2009-05-22 14:51:39 -040010781 /* Release mailbox command work queue */
James Smart895427b2017-02-12 13:52:30 -080010782 __lpfc_sli4_release_queue(&phba->sli4_hba.mbx_wq);
James Smartda0436e2009-05-22 14:51:39 -040010783
10784 /* Release ELS work queue */
James Smart895427b2017-02-12 13:52:30 -080010785 __lpfc_sli4_release_queue(&phba->sli4_hba.els_wq);
10786
10787 /* Release ELS work queue */
10788 __lpfc_sli4_release_queue(&phba->sli4_hba.nvmels_wq);
James Smartda0436e2009-05-22 14:51:39 -040010789
10790 /* Release unsolicited receive queue */
James Smart895427b2017-02-12 13:52:30 -080010791 __lpfc_sli4_release_queue(&phba->sli4_hba.hdr_rq);
10792 __lpfc_sli4_release_queue(&phba->sli4_hba.dat_rq);
James Smartda0436e2009-05-22 14:51:39 -040010793
James Smartda0436e2009-05-22 14:51:39 -040010794 /* Release ELS complete queue */
James Smart895427b2017-02-12 13:52:30 -080010795 __lpfc_sli4_release_queue(&phba->sli4_hba.els_cq);
10796
10797 /* Release NVME LS complete queue */
10798 __lpfc_sli4_release_queue(&phba->sli4_hba.nvmels_cq);
James Smartda0436e2009-05-22 14:51:39 -040010799
10800 /* Release mailbox command complete queue */
James Smart895427b2017-02-12 13:52:30 -080010801 __lpfc_sli4_release_queue(&phba->sli4_hba.mbx_cq);
10802
10803 /* Everything on this list has been freed */
10804 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_wq_list);
James Smart4645f7b2019-03-12 16:30:14 -070010805
10806 /* Done with freeing the queues */
10807 spin_lock_irq(&phba->hbalock);
10808 phba->sli.sli_flag &= ~LPFC_QUEUE_FREE_INIT;
10809 spin_unlock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -080010810}
10811
10812int
James Smart895427b2017-02-12 13:52:30 -080010813lpfc_free_rq_buffer(struct lpfc_hba *phba, struct lpfc_queue *rq)
10814{
10815 struct lpfc_rqb *rqbp;
10816 struct lpfc_dmabuf *h_buf;
10817 struct rqb_dmabuf *rqb_buffer;
10818
10819 rqbp = rq->rqbp;
10820 while (!list_empty(&rqbp->rqb_buffer_list)) {
10821 list_remove_head(&rqbp->rqb_buffer_list, h_buf,
10822 struct lpfc_dmabuf, list);
10823
10824 rqb_buffer = container_of(h_buf, struct rqb_dmabuf, hbuf);
10825 (rqbp->rqb_free_buffer)(phba, rqb_buffer);
10826 rqbp->buffer_count--;
10827 }
10828 return 1;
10829}
10830
10831static int
10832lpfc_create_wq_cq(struct lpfc_hba *phba, struct lpfc_queue *eq,
10833 struct lpfc_queue *cq, struct lpfc_queue *wq, uint16_t *cq_map,
10834 int qidx, uint32_t qtype)
10835{
10836 struct lpfc_sli_ring *pring;
10837 int rc;
10838
10839 if (!eq || !cq || !wq) {
Dick Kennedy372c1872020-06-30 14:50:00 -070010840 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart895427b2017-02-12 13:52:30 -080010841 "6085 Fast-path %s (%d) not allocated\n",
10842 ((eq) ? ((cq) ? "WQ" : "CQ") : "EQ"), qidx);
10843 return -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -040010844 }
James Smartda0436e2009-05-22 14:51:39 -040010845
James Smart895427b2017-02-12 13:52:30 -080010846 /* create the Cq first */
10847 rc = lpfc_cq_create(phba, cq, eq,
10848 (qtype == LPFC_MBOX) ? LPFC_MCQ : LPFC_WCQ, qtype);
10849 if (rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -070010850 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
10851 "6086 Failed setup of CQ (%d), rc = 0x%x\n",
10852 qidx, (uint32_t)rc);
James Smart895427b2017-02-12 13:52:30 -080010853 return rc;
10854 }
10855
10856 if (qtype != LPFC_MBOX) {
James Smartcdb42be2019-01-28 11:14:21 -080010857 /* Setup cq_map for fast lookup */
James Smart895427b2017-02-12 13:52:30 -080010858 if (cq_map)
10859 *cq_map = cq->queue_id;
10860
10861 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10862 "6087 CQ setup: cq[%d]-id=%d, parent eq[%d]-id=%d\n",
10863 qidx, cq->queue_id, qidx, eq->queue_id);
10864
10865 /* create the wq */
10866 rc = lpfc_wq_create(phba, wq, cq, qtype);
10867 if (rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -070010868 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartc835c082019-03-12 16:30:30 -070010869 "4618 Fail setup fastpath WQ (%d), rc = 0x%x\n",
James Smart895427b2017-02-12 13:52:30 -080010870 qidx, (uint32_t)rc);
10871 /* no need to tear down cq - caller will do so */
10872 return rc;
10873 }
10874
10875 /* Bind this CQ/WQ to the NVME ring */
10876 pring = wq->pring;
10877 pring->sli.sli4.wqp = (void *)wq;
10878 cq->pring = pring;
10879
10880 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10881 "2593 WQ setup: wq[%d]-id=%d assoc=%d, cq[%d]-id=%d\n",
10882 qidx, wq->queue_id, wq->assoc_qid, qidx, cq->queue_id);
10883 } else {
10884 rc = lpfc_mq_create(phba, wq, cq, LPFC_MBOX);
10885 if (rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -070010886 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
10887 "0539 Failed setup of slow-path MQ: "
10888 "rc = 0x%x\n", rc);
James Smart895427b2017-02-12 13:52:30 -080010889 /* no need to tear down cq - caller will do so */
10890 return rc;
10891 }
10892
10893 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10894 "2589 MBX MQ setup: wq-id=%d, parent cq-id=%d\n",
10895 phba->sli4_hba.mbx_wq->queue_id,
10896 phba->sli4_hba.mbx_cq->queue_id);
10897 }
10898
10899 return 0;
James Smartda0436e2009-05-22 14:51:39 -040010900}
10901
10902/**
James Smart6a828b02019-01-28 11:14:31 -080010903 * lpfc_setup_cq_lookup - Setup the CQ lookup table
10904 * @phba: pointer to lpfc hba data structure.
10905 *
10906 * This routine will populate the cq_lookup table by all
10907 * available CQ queue_id's.
10908 **/
Bart Van Assche3999df72019-03-28 11:06:16 -070010909static void
James Smart6a828b02019-01-28 11:14:31 -080010910lpfc_setup_cq_lookup(struct lpfc_hba *phba)
10911{
10912 struct lpfc_queue *eq, *childq;
James Smart6a828b02019-01-28 11:14:31 -080010913 int qidx;
10914
James Smart6a828b02019-01-28 11:14:31 -080010915 memset(phba->sli4_hba.cq_lookup, 0,
10916 (sizeof(struct lpfc_queue *) * (phba->sli4_hba.cq_max + 1)));
James Smart657add42019-05-21 17:49:06 -070010917 /* Loop thru all IRQ vectors */
James Smart6a828b02019-01-28 11:14:31 -080010918 for (qidx = 0; qidx < phba->cfg_irq_chann; qidx++) {
James Smart657add42019-05-21 17:49:06 -070010919 /* Get the EQ corresponding to the IRQ vector */
10920 eq = phba->sli4_hba.hba_eq_hdl[qidx].eq;
James Smart6a828b02019-01-28 11:14:31 -080010921 if (!eq)
10922 continue;
James Smart657add42019-05-21 17:49:06 -070010923 /* Loop through all CQs associated with that EQ */
James Smart6a828b02019-01-28 11:14:31 -080010924 list_for_each_entry(childq, &eq->child_list, list) {
10925 if (childq->queue_id > phba->sli4_hba.cq_max)
10926 continue;
James Smartc00f62e2019-08-14 16:57:11 -070010927 if (childq->subtype == LPFC_IO)
James Smart6a828b02019-01-28 11:14:31 -080010928 phba->sli4_hba.cq_lookup[childq->queue_id] =
10929 childq;
10930 }
10931 }
10932}
10933
10934/**
James Smartda0436e2009-05-22 14:51:39 -040010935 * lpfc_sli4_queue_setup - Set up all the SLI4 queues
10936 * @phba: pointer to lpfc hba data structure.
10937 *
10938 * This routine is invoked to set up all the SLI4 queues for the FCoE HBA
10939 * operation.
10940 *
10941 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020010942 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010943 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -040010944 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -040010945 **/
10946int
10947lpfc_sli4_queue_setup(struct lpfc_hba *phba)
10948{
James Smart962bc512013-01-03 15:44:00 -050010949 uint32_t shdr_status, shdr_add_status;
10950 union lpfc_sli4_cfg_shdr *shdr;
James Smart657add42019-05-21 17:49:06 -070010951 struct lpfc_vector_map_info *cpup;
James Smartcdb42be2019-01-28 11:14:21 -080010952 struct lpfc_sli4_hdw_queue *qp;
James Smart962bc512013-01-03 15:44:00 -050010953 LPFC_MBOXQ_t *mboxq;
James Smart657add42019-05-21 17:49:06 -070010954 int qidx, cpu;
James Smartcb733e32019-01-28 11:14:32 -080010955 uint32_t length, usdelay;
James Smart895427b2017-02-12 13:52:30 -080010956 int rc = -ENOMEM;
James Smart962bc512013-01-03 15:44:00 -050010957
10958 /* Check for dual-ULP support */
10959 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10960 if (!mboxq) {
Dick Kennedy372c1872020-06-30 14:50:00 -070010961 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart962bc512013-01-03 15:44:00 -050010962 "3249 Unable to allocate memory for "
10963 "QUERY_FW_CFG mailbox command\n");
10964 return -ENOMEM;
10965 }
10966 length = (sizeof(struct lpfc_mbx_query_fw_config) -
10967 sizeof(struct lpfc_sli4_cfg_mhdr));
10968 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
10969 LPFC_MBOX_OPCODE_QUERY_FW_CFG,
10970 length, LPFC_SLI4_MBX_EMBED);
10971
10972 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
10973
10974 shdr = (union lpfc_sli4_cfg_shdr *)
10975 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
10976 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10977 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10978 if (shdr_status || shdr_add_status || rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -070010979 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart962bc512013-01-03 15:44:00 -050010980 "3250 QUERY_FW_CFG mailbox failed with status "
10981 "x%x add_status x%x, mbx status x%x\n",
10982 shdr_status, shdr_add_status, rc);
James Smart304ee432021-04-11 18:31:17 -070010983 mempool_free(mboxq, phba->mbox_mem_pool);
James Smart962bc512013-01-03 15:44:00 -050010984 rc = -ENXIO;
10985 goto out_error;
10986 }
10987
10988 phba->sli4_hba.fw_func_mode =
10989 mboxq->u.mqe.un.query_fw_cfg.rsp.function_mode;
10990 phba->sli4_hba.ulp0_mode = mboxq->u.mqe.un.query_fw_cfg.rsp.ulp0_mode;
10991 phba->sli4_hba.ulp1_mode = mboxq->u.mqe.un.query_fw_cfg.rsp.ulp1_mode;
James Smart8b017a32015-05-21 13:55:18 -040010992 phba->sli4_hba.physical_port =
10993 mboxq->u.mqe.un.query_fw_cfg.rsp.physical_port;
James Smart962bc512013-01-03 15:44:00 -050010994 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10995 "3251 QUERY_FW_CFG: func_mode:x%x, ulp0_mode:x%x, "
10996 "ulp1_mode:x%x\n", phba->sli4_hba.fw_func_mode,
10997 phba->sli4_hba.ulp0_mode, phba->sli4_hba.ulp1_mode);
10998
James Smart304ee432021-04-11 18:31:17 -070010999 mempool_free(mboxq, phba->mbox_mem_pool);
James Smartda0436e2009-05-22 14:51:39 -040011000
11001 /*
James Smart67d12732012-08-03 12:36:13 -040011002 * Set up HBA Event Queues (EQs)
James Smartda0436e2009-05-22 14:51:39 -040011003 */
James Smartcdb42be2019-01-28 11:14:21 -080011004 qp = phba->sli4_hba.hdwq;
James Smartda0436e2009-05-22 14:51:39 -040011005
James Smart67d12732012-08-03 12:36:13 -040011006 /* Set up HBA event queue */
James Smartcdb42be2019-01-28 11:14:21 -080011007 if (!qp) {
Dick Kennedy372c1872020-06-30 14:50:00 -070011008 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2e90f4b2011-12-13 13:22:37 -050011009 "3147 Fast-path EQs not allocated\n");
James Smart1b511972011-12-13 13:23:09 -050011010 rc = -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -040011011 goto out_error;
James Smart2e90f4b2011-12-13 13:22:37 -050011012 }
James Smart657add42019-05-21 17:49:06 -070011013
11014 /* Loop thru all IRQ vectors */
James Smart6a828b02019-01-28 11:14:31 -080011015 for (qidx = 0; qidx < phba->cfg_irq_chann; qidx++) {
James Smart657add42019-05-21 17:49:06 -070011016 /* Create HBA Event Queues (EQs) in order */
11017 for_each_present_cpu(cpu) {
11018 cpup = &phba->sli4_hba.cpu_map[cpu];
11019
11020 /* Look for the CPU thats using that vector with
11021 * LPFC_CPU_FIRST_IRQ set.
11022 */
11023 if (!(cpup->flag & LPFC_CPU_FIRST_IRQ))
11024 continue;
11025 if (qidx != cpup->eq)
11026 continue;
11027
11028 /* Create an EQ for that vector */
11029 rc = lpfc_eq_create(phba, qp[cpup->hdwq].hba_eq,
11030 phba->cfg_fcp_imax);
11031 if (rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -070011032 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart657add42019-05-21 17:49:06 -070011033 "0523 Failed setup of fast-path"
11034 " EQ (%d), rc = 0x%x\n",
11035 cpup->eq, (uint32_t)rc);
11036 goto out_destroy;
11037 }
11038
11039 /* Save the EQ for that vector in the hba_eq_hdl */
11040 phba->sli4_hba.hba_eq_hdl[cpup->eq].eq =
11041 qp[cpup->hdwq].hba_eq;
11042
11043 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
11044 "2584 HBA EQ setup: queue[%d]-id=%d\n",
11045 cpup->eq,
11046 qp[cpup->hdwq].hba_eq->queue_id);
James Smartda0436e2009-05-22 14:51:39 -040011047 }
James Smartda0436e2009-05-22 14:51:39 -040011048 }
11049
James Smart657add42019-05-21 17:49:06 -070011050 /* Loop thru all Hardware Queues */
James Smartcdb42be2019-01-28 11:14:21 -080011051 for (qidx = 0; qidx < phba->cfg_hdw_queue; qidx++) {
James Smart657add42019-05-21 17:49:06 -070011052 cpu = lpfc_find_cpu_handle(phba, qidx, LPFC_FIND_BY_HDWQ);
11053 cpup = &phba->sli4_hba.cpu_map[cpu];
11054
11055 /* Create the CQ/WQ corresponding to the Hardware Queue */
James Smartcdb42be2019-01-28 11:14:21 -080011056 rc = lpfc_create_wq_cq(phba,
James Smart657add42019-05-21 17:49:06 -070011057 phba->sli4_hba.hdwq[cpup->hdwq].hba_eq,
James Smartc00f62e2019-08-14 16:57:11 -070011058 qp[qidx].io_cq,
11059 qp[qidx].io_wq,
11060 &phba->sli4_hba.hdwq[qidx].io_cq_map,
11061 qidx,
11062 LPFC_IO);
James Smartcdb42be2019-01-28 11:14:21 -080011063 if (rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -070011064 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart895427b2017-02-12 13:52:30 -080011065 "0535 Failed to setup fastpath "
James Smartc00f62e2019-08-14 16:57:11 -070011066 "IO WQ/CQ (%d), rc = 0x%x\n",
James Smart895427b2017-02-12 13:52:30 -080011067 qidx, (uint32_t)rc);
James Smartcdb42be2019-01-28 11:14:21 -080011068 goto out_destroy;
James Smart895427b2017-02-12 13:52:30 -080011069 }
James Smart67d12732012-08-03 12:36:13 -040011070 }
James Smartda0436e2009-05-22 14:51:39 -040011071
11072 /*
James Smart895427b2017-02-12 13:52:30 -080011073 * Set up Slow Path Complete Queues (CQs)
James Smartda0436e2009-05-22 14:51:39 -040011074 */
11075
James Smart895427b2017-02-12 13:52:30 -080011076 /* Set up slow-path MBOX CQ/MQ */
11077
11078 if (!phba->sli4_hba.mbx_cq || !phba->sli4_hba.mbx_wq) {
Dick Kennedy372c1872020-06-30 14:50:00 -070011079 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart895427b2017-02-12 13:52:30 -080011080 "0528 %s not allocated\n",
11081 phba->sli4_hba.mbx_cq ?
James Smartd1f525a2017-04-21 16:04:55 -070011082 "Mailbox WQ" : "Mailbox CQ");
James Smart1b511972011-12-13 13:23:09 -050011083 rc = -ENOMEM;
James Smart895427b2017-02-12 13:52:30 -080011084 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -040011085 }
James Smart895427b2017-02-12 13:52:30 -080011086
James Smartcdb42be2019-01-28 11:14:21 -080011087 rc = lpfc_create_wq_cq(phba, qp[0].hba_eq,
James Smartd1f525a2017-04-21 16:04:55 -070011088 phba->sli4_hba.mbx_cq,
11089 phba->sli4_hba.mbx_wq,
11090 NULL, 0, LPFC_MBOX);
James Smartda0436e2009-05-22 14:51:39 -040011091 if (rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -070011092 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart895427b2017-02-12 13:52:30 -080011093 "0529 Failed setup of mailbox WQ/CQ: rc = 0x%x\n",
11094 (uint32_t)rc);
11095 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -040011096 }
James Smart2d7dbc42017-02-12 13:52:35 -080011097 if (phba->nvmet_support) {
11098 if (!phba->sli4_hba.nvmet_cqset) {
Dick Kennedy372c1872020-06-30 14:50:00 -070011099 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2d7dbc42017-02-12 13:52:35 -080011100 "3165 Fast-path NVME CQ Set "
11101 "array not allocated\n");
11102 rc = -ENOMEM;
11103 goto out_destroy;
11104 }
11105 if (phba->cfg_nvmet_mrq > 1) {
11106 rc = lpfc_cq_create_set(phba,
11107 phba->sli4_hba.nvmet_cqset,
James Smartcdb42be2019-01-28 11:14:21 -080011108 qp,
James Smart2d7dbc42017-02-12 13:52:35 -080011109 LPFC_WCQ, LPFC_NVMET);
11110 if (rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -070011111 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2d7dbc42017-02-12 13:52:35 -080011112 "3164 Failed setup of NVME CQ "
11113 "Set, rc = 0x%x\n",
11114 (uint32_t)rc);
11115 goto out_destroy;
11116 }
11117 } else {
11118 /* Set up NVMET Receive Complete Queue */
11119 rc = lpfc_cq_create(phba, phba->sli4_hba.nvmet_cqset[0],
James Smartcdb42be2019-01-28 11:14:21 -080011120 qp[0].hba_eq,
James Smart2d7dbc42017-02-12 13:52:35 -080011121 LPFC_WCQ, LPFC_NVMET);
11122 if (rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -070011123 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2d7dbc42017-02-12 13:52:35 -080011124 "6089 Failed setup NVMET CQ: "
11125 "rc = 0x%x\n", (uint32_t)rc);
11126 goto out_destroy;
11127 }
James Smart81b96ed2017-11-20 16:00:29 -080011128 phba->sli4_hba.nvmet_cqset[0]->chann = 0;
11129
James Smart2d7dbc42017-02-12 13:52:35 -080011130 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
11131 "6090 NVMET CQ setup: cq-id=%d, "
11132 "parent eq-id=%d\n",
11133 phba->sli4_hba.nvmet_cqset[0]->queue_id,
James Smartcdb42be2019-01-28 11:14:21 -080011134 qp[0].hba_eq->queue_id);
James Smart2d7dbc42017-02-12 13:52:35 -080011135 }
11136 }
James Smartda0436e2009-05-22 14:51:39 -040011137
James Smart895427b2017-02-12 13:52:30 -080011138 /* Set up slow-path ELS WQ/CQ */
11139 if (!phba->sli4_hba.els_cq || !phba->sli4_hba.els_wq) {
Dick Kennedy372c1872020-06-30 14:50:00 -070011140 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart895427b2017-02-12 13:52:30 -080011141 "0530 ELS %s not allocated\n",
11142 phba->sli4_hba.els_cq ? "WQ" : "CQ");
James Smart1b511972011-12-13 13:23:09 -050011143 rc = -ENOMEM;
James Smart895427b2017-02-12 13:52:30 -080011144 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -040011145 }
James Smartcdb42be2019-01-28 11:14:21 -080011146 rc = lpfc_create_wq_cq(phba, qp[0].hba_eq,
11147 phba->sli4_hba.els_cq,
11148 phba->sli4_hba.els_wq,
11149 NULL, 0, LPFC_ELS);
James Smartda0436e2009-05-22 14:51:39 -040011150 if (rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -070011151 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartcdb42be2019-01-28 11:14:21 -080011152 "0525 Failed setup of ELS WQ/CQ: rc = 0x%x\n",
11153 (uint32_t)rc);
James Smart895427b2017-02-12 13:52:30 -080011154 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -040011155 }
11156 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
11157 "2590 ELS WQ setup: wq-id=%d, parent cq-id=%d\n",
11158 phba->sli4_hba.els_wq->queue_id,
11159 phba->sli4_hba.els_cq->queue_id);
11160
James Smartcdb42be2019-01-28 11:14:21 -080011161 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
James Smart895427b2017-02-12 13:52:30 -080011162 /* Set up NVME LS Complete Queue */
11163 if (!phba->sli4_hba.nvmels_cq || !phba->sli4_hba.nvmels_wq) {
Dick Kennedy372c1872020-06-30 14:50:00 -070011164 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart895427b2017-02-12 13:52:30 -080011165 "6091 LS %s not allocated\n",
11166 phba->sli4_hba.nvmels_cq ? "WQ" : "CQ");
11167 rc = -ENOMEM;
11168 goto out_destroy;
11169 }
James Smartcdb42be2019-01-28 11:14:21 -080011170 rc = lpfc_create_wq_cq(phba, qp[0].hba_eq,
11171 phba->sli4_hba.nvmels_cq,
11172 phba->sli4_hba.nvmels_wq,
11173 NULL, 0, LPFC_NVME_LS);
James Smart895427b2017-02-12 13:52:30 -080011174 if (rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -070011175 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartcdb42be2019-01-28 11:14:21 -080011176 "0526 Failed setup of NVVME LS WQ/CQ: "
11177 "rc = 0x%x\n", (uint32_t)rc);
James Smart895427b2017-02-12 13:52:30 -080011178 goto out_destroy;
11179 }
11180
11181 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
11182 "6096 ELS WQ setup: wq-id=%d, "
11183 "parent cq-id=%d\n",
11184 phba->sli4_hba.nvmels_wq->queue_id,
11185 phba->sli4_hba.nvmels_cq->queue_id);
11186 }
11187
James Smart2d7dbc42017-02-12 13:52:35 -080011188 /*
11189 * Create NVMET Receive Queue (RQ)
11190 */
11191 if (phba->nvmet_support) {
11192 if ((!phba->sli4_hba.nvmet_cqset) ||
11193 (!phba->sli4_hba.nvmet_mrq_hdr) ||
11194 (!phba->sli4_hba.nvmet_mrq_data)) {
Dick Kennedy372c1872020-06-30 14:50:00 -070011195 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2d7dbc42017-02-12 13:52:35 -080011196 "6130 MRQ CQ Queues not "
11197 "allocated\n");
11198 rc = -ENOMEM;
11199 goto out_destroy;
11200 }
11201 if (phba->cfg_nvmet_mrq > 1) {
11202 rc = lpfc_mrq_create(phba,
11203 phba->sli4_hba.nvmet_mrq_hdr,
11204 phba->sli4_hba.nvmet_mrq_data,
11205 phba->sli4_hba.nvmet_cqset,
11206 LPFC_NVMET);
11207 if (rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -070011208 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2d7dbc42017-02-12 13:52:35 -080011209 "6098 Failed setup of NVMET "
11210 "MRQ: rc = 0x%x\n",
11211 (uint32_t)rc);
11212 goto out_destroy;
11213 }
11214
11215 } else {
11216 rc = lpfc_rq_create(phba,
11217 phba->sli4_hba.nvmet_mrq_hdr[0],
11218 phba->sli4_hba.nvmet_mrq_data[0],
11219 phba->sli4_hba.nvmet_cqset[0],
11220 LPFC_NVMET);
11221 if (rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -070011222 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2d7dbc42017-02-12 13:52:35 -080011223 "6057 Failed setup of NVMET "
11224 "Receive Queue: rc = 0x%x\n",
11225 (uint32_t)rc);
11226 goto out_destroy;
11227 }
11228
11229 lpfc_printf_log(
11230 phba, KERN_INFO, LOG_INIT,
11231 "6099 NVMET RQ setup: hdr-rq-id=%d, "
11232 "dat-rq-id=%d parent cq-id=%d\n",
11233 phba->sli4_hba.nvmet_mrq_hdr[0]->queue_id,
11234 phba->sli4_hba.nvmet_mrq_data[0]->queue_id,
11235 phba->sli4_hba.nvmet_cqset[0]->queue_id);
11236
11237 }
11238 }
11239
James Smartda0436e2009-05-22 14:51:39 -040011240 if (!phba->sli4_hba.hdr_rq || !phba->sli4_hba.dat_rq) {
Dick Kennedy372c1872020-06-30 14:50:00 -070011241 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartda0436e2009-05-22 14:51:39 -040011242 "0540 Receive Queue not allocated\n");
James Smart1b511972011-12-13 13:23:09 -050011243 rc = -ENOMEM;
James Smart895427b2017-02-12 13:52:30 -080011244 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -040011245 }
James Smart73d91e52011-10-10 21:32:10 -040011246
James Smartda0436e2009-05-22 14:51:39 -040011247 rc = lpfc_rq_create(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
James Smart4d9ab992009-10-02 15:16:39 -040011248 phba->sli4_hba.els_cq, LPFC_USOL);
James Smartda0436e2009-05-22 14:51:39 -040011249 if (rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -070011250 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartda0436e2009-05-22 14:51:39 -040011251 "0541 Failed setup of Receive Queue: "
James Smarta2fc4aef2014-09-03 12:57:55 -040011252 "rc = 0x%x\n", (uint32_t)rc);
James Smart895427b2017-02-12 13:52:30 -080011253 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -040011254 }
James Smart73d91e52011-10-10 21:32:10 -040011255
James Smartda0436e2009-05-22 14:51:39 -040011256 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
11257 "2592 USL RQ setup: hdr-rq-id=%d, dat-rq-id=%d "
11258 "parent cq-id=%d\n",
11259 phba->sli4_hba.hdr_rq->queue_id,
11260 phba->sli4_hba.dat_rq->queue_id,
James Smart4d9ab992009-10-02 15:16:39 -040011261 phba->sli4_hba.els_cq->queue_id);
James Smart1ba981f2014-02-20 09:56:45 -050011262
James Smartcb733e32019-01-28 11:14:32 -080011263 if (phba->cfg_fcp_imax)
11264 usdelay = LPFC_SEC_TO_USEC / phba->cfg_fcp_imax;
11265 else
11266 usdelay = 0;
11267
James Smart6a828b02019-01-28 11:14:31 -080011268 for (qidx = 0; qidx < phba->cfg_irq_chann;
James Smartcdb42be2019-01-28 11:14:21 -080011269 qidx += LPFC_MAX_EQ_DELAY_EQID_CNT)
James Smart0cf07f842017-06-01 21:07:10 -070011270 lpfc_modify_hba_eq_delay(phba, qidx, LPFC_MAX_EQ_DELAY_EQID_CNT,
James Smartcb733e32019-01-28 11:14:32 -080011271 usdelay);
James Smart43140ca2017-03-04 09:30:34 -080011272
James Smart6a828b02019-01-28 11:14:31 -080011273 if (phba->sli4_hba.cq_max) {
11274 kfree(phba->sli4_hba.cq_lookup);
11275 phba->sli4_hba.cq_lookup = kcalloc((phba->sli4_hba.cq_max + 1),
11276 sizeof(struct lpfc_queue *), GFP_KERNEL);
11277 if (!phba->sli4_hba.cq_lookup) {
Dick Kennedy372c1872020-06-30 14:50:00 -070011278 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart6a828b02019-01-28 11:14:31 -080011279 "0549 Failed setup of CQ Lookup table: "
11280 "size 0x%x\n", phba->sli4_hba.cq_max);
Dan Carpenterfad28e32019-02-11 21:43:00 +030011281 rc = -ENOMEM;
James Smart1ba981f2014-02-20 09:56:45 -050011282 goto out_destroy;
James Smart895427b2017-02-12 13:52:30 -080011283 }
James Smart6a828b02019-01-28 11:14:31 -080011284 lpfc_setup_cq_lookup(phba);
James Smart1ba981f2014-02-20 09:56:45 -050011285 }
James Smartda0436e2009-05-22 14:51:39 -040011286 return 0;
11287
James Smart895427b2017-02-12 13:52:30 -080011288out_destroy:
11289 lpfc_sli4_queue_unset(phba);
James Smartda0436e2009-05-22 14:51:39 -040011290out_error:
11291 return rc;
11292}
11293
11294/**
11295 * lpfc_sli4_queue_unset - Unset all the SLI4 queues
11296 * @phba: pointer to lpfc hba data structure.
11297 *
11298 * This routine is invoked to unset all the SLI4 queues with the FCoE HBA
11299 * operation.
11300 *
11301 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020011302 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011303 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -040011304 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -040011305 **/
11306void
11307lpfc_sli4_queue_unset(struct lpfc_hba *phba)
11308{
James Smartcdb42be2019-01-28 11:14:21 -080011309 struct lpfc_sli4_hdw_queue *qp;
James Smart657add42019-05-21 17:49:06 -070011310 struct lpfc_queue *eq;
James Smart895427b2017-02-12 13:52:30 -080011311 int qidx;
James Smartda0436e2009-05-22 14:51:39 -040011312
11313 /* Unset mailbox command work queue */
James Smart895427b2017-02-12 13:52:30 -080011314 if (phba->sli4_hba.mbx_wq)
11315 lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
11316
11317 /* Unset NVME LS work queue */
11318 if (phba->sli4_hba.nvmels_wq)
11319 lpfc_wq_destroy(phba, phba->sli4_hba.nvmels_wq);
11320
James Smartda0436e2009-05-22 14:51:39 -040011321 /* Unset ELS work queue */
Colin Ian King019c0d62017-05-06 23:13:55 +010011322 if (phba->sli4_hba.els_wq)
James Smart895427b2017-02-12 13:52:30 -080011323 lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
11324
James Smartda0436e2009-05-22 14:51:39 -040011325 /* Unset unsolicited receive queue */
James Smart895427b2017-02-12 13:52:30 -080011326 if (phba->sli4_hba.hdr_rq)
11327 lpfc_rq_destroy(phba, phba->sli4_hba.hdr_rq,
11328 phba->sli4_hba.dat_rq);
11329
James Smartda0436e2009-05-22 14:51:39 -040011330 /* Unset mailbox command complete queue */
James Smart895427b2017-02-12 13:52:30 -080011331 if (phba->sli4_hba.mbx_cq)
11332 lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
11333
James Smartda0436e2009-05-22 14:51:39 -040011334 /* Unset ELS complete queue */
James Smart895427b2017-02-12 13:52:30 -080011335 if (phba->sli4_hba.els_cq)
11336 lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
11337
11338 /* Unset NVME LS complete queue */
11339 if (phba->sli4_hba.nvmels_cq)
11340 lpfc_cq_destroy(phba, phba->sli4_hba.nvmels_cq);
11341
James Smartbcb24f62017-11-20 16:00:36 -080011342 if (phba->nvmet_support) {
11343 /* Unset NVMET MRQ queue */
11344 if (phba->sli4_hba.nvmet_mrq_hdr) {
11345 for (qidx = 0; qidx < phba->cfg_nvmet_mrq; qidx++)
11346 lpfc_rq_destroy(
11347 phba,
James Smart2d7dbc42017-02-12 13:52:35 -080011348 phba->sli4_hba.nvmet_mrq_hdr[qidx],
11349 phba->sli4_hba.nvmet_mrq_data[qidx]);
James Smartbcb24f62017-11-20 16:00:36 -080011350 }
James Smart2d7dbc42017-02-12 13:52:35 -080011351
James Smartbcb24f62017-11-20 16:00:36 -080011352 /* Unset NVMET CQ Set complete queue */
11353 if (phba->sli4_hba.nvmet_cqset) {
11354 for (qidx = 0; qidx < phba->cfg_nvmet_mrq; qidx++)
11355 lpfc_cq_destroy(
11356 phba, phba->sli4_hba.nvmet_cqset[qidx]);
11357 }
James Smart2d7dbc42017-02-12 13:52:35 -080011358 }
11359
James Smartcdb42be2019-01-28 11:14:21 -080011360 /* Unset fast-path SLI4 queues */
11361 if (phba->sli4_hba.hdwq) {
James Smart657add42019-05-21 17:49:06 -070011362 /* Loop thru all Hardware Queues */
James Smartcdb42be2019-01-28 11:14:21 -080011363 for (qidx = 0; qidx < phba->cfg_hdw_queue; qidx++) {
James Smart657add42019-05-21 17:49:06 -070011364 /* Destroy the CQ/WQ corresponding to Hardware Queue */
James Smartcdb42be2019-01-28 11:14:21 -080011365 qp = &phba->sli4_hba.hdwq[qidx];
James Smartc00f62e2019-08-14 16:57:11 -070011366 lpfc_wq_destroy(phba, qp->io_wq);
11367 lpfc_cq_destroy(phba, qp->io_cq);
James Smart657add42019-05-21 17:49:06 -070011368 }
11369 /* Loop thru all IRQ vectors */
11370 for (qidx = 0; qidx < phba->cfg_irq_chann; qidx++) {
11371 /* Destroy the EQ corresponding to the IRQ vector */
11372 eq = phba->sli4_hba.hba_eq_hdl[qidx].eq;
11373 lpfc_eq_destroy(phba, eq);
James Smartcdb42be2019-01-28 11:14:21 -080011374 }
11375 }
James Smart895427b2017-02-12 13:52:30 -080011376
James Smart6a828b02019-01-28 11:14:31 -080011377 kfree(phba->sli4_hba.cq_lookup);
11378 phba->sli4_hba.cq_lookup = NULL;
11379 phba->sli4_hba.cq_max = 0;
James Smartda0436e2009-05-22 14:51:39 -040011380}
11381
11382/**
11383 * lpfc_sli4_cq_event_pool_create - Create completion-queue event free pool
11384 * @phba: pointer to lpfc hba data structure.
11385 *
11386 * This routine is invoked to allocate and set up a pool of completion queue
11387 * events. The body of the completion queue event is a completion queue entry
11388 * CQE. For now, this pool is used for the interrupt service routine to queue
11389 * the following HBA completion queue events for the worker thread to process:
11390 * - Mailbox asynchronous events
11391 * - Receive queue completion unsolicited events
11392 * Later, this can be used for all the slow-path events.
11393 *
11394 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020011395 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011396 * -ENOMEM - No available memory
James Smartda0436e2009-05-22 14:51:39 -040011397 **/
11398static int
11399lpfc_sli4_cq_event_pool_create(struct lpfc_hba *phba)
11400{
11401 struct lpfc_cq_event *cq_event;
11402 int i;
11403
11404 for (i = 0; i < (4 * phba->sli4_hba.cq_ecount); i++) {
11405 cq_event = kmalloc(sizeof(struct lpfc_cq_event), GFP_KERNEL);
11406 if (!cq_event)
11407 goto out_pool_create_fail;
11408 list_add_tail(&cq_event->list,
11409 &phba->sli4_hba.sp_cqe_event_pool);
11410 }
11411 return 0;
11412
11413out_pool_create_fail:
11414 lpfc_sli4_cq_event_pool_destroy(phba);
11415 return -ENOMEM;
11416}
11417
11418/**
11419 * lpfc_sli4_cq_event_pool_destroy - Free completion-queue event free pool
11420 * @phba: pointer to lpfc hba data structure.
11421 *
11422 * This routine is invoked to free the pool of completion queue events at
11423 * driver unload time. Note that, it is the responsibility of the driver
11424 * cleanup routine to free all the outstanding completion-queue events
11425 * allocated from this pool back into the pool before invoking this routine
11426 * to destroy the pool.
11427 **/
11428static void
11429lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *phba)
11430{
11431 struct lpfc_cq_event *cq_event, *next_cq_event;
11432
11433 list_for_each_entry_safe(cq_event, next_cq_event,
11434 &phba->sli4_hba.sp_cqe_event_pool, list) {
11435 list_del(&cq_event->list);
11436 kfree(cq_event);
11437 }
11438}
11439
11440/**
11441 * __lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
11442 * @phba: pointer to lpfc hba data structure.
11443 *
11444 * This routine is the lock free version of the API invoked to allocate a
11445 * completion-queue event from the free pool.
11446 *
11447 * Return: Pointer to the newly allocated completion-queue event if successful
11448 * NULL otherwise.
11449 **/
11450struct lpfc_cq_event *
11451__lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
11452{
11453 struct lpfc_cq_event *cq_event = NULL;
11454
11455 list_remove_head(&phba->sli4_hba.sp_cqe_event_pool, cq_event,
11456 struct lpfc_cq_event, list);
11457 return cq_event;
11458}
11459
11460/**
11461 * lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
11462 * @phba: pointer to lpfc hba data structure.
11463 *
11464 * This routine is the lock version of the API invoked to allocate a
11465 * completion-queue event from the free pool.
11466 *
11467 * Return: Pointer to the newly allocated completion-queue event if successful
11468 * NULL otherwise.
11469 **/
11470struct lpfc_cq_event *
11471lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
11472{
11473 struct lpfc_cq_event *cq_event;
11474 unsigned long iflags;
11475
11476 spin_lock_irqsave(&phba->hbalock, iflags);
11477 cq_event = __lpfc_sli4_cq_event_alloc(phba);
11478 spin_unlock_irqrestore(&phba->hbalock, iflags);
11479 return cq_event;
11480}
11481
11482/**
11483 * __lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
11484 * @phba: pointer to lpfc hba data structure.
11485 * @cq_event: pointer to the completion queue event to be freed.
11486 *
11487 * This routine is the lock free version of the API invoked to release a
11488 * completion-queue event back into the free pool.
11489 **/
11490void
11491__lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
11492 struct lpfc_cq_event *cq_event)
11493{
11494 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_cqe_event_pool);
11495}
11496
11497/**
11498 * lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
11499 * @phba: pointer to lpfc hba data structure.
11500 * @cq_event: pointer to the completion queue event to be freed.
11501 *
11502 * This routine is the lock version of the API invoked to release a
11503 * completion-queue event back into the free pool.
11504 **/
11505void
11506lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
11507 struct lpfc_cq_event *cq_event)
11508{
11509 unsigned long iflags;
11510 spin_lock_irqsave(&phba->hbalock, iflags);
11511 __lpfc_sli4_cq_event_release(phba, cq_event);
11512 spin_unlock_irqrestore(&phba->hbalock, iflags);
11513}
11514
11515/**
11516 * lpfc_sli4_cq_event_release_all - Release all cq events to the free pool
11517 * @phba: pointer to lpfc hba data structure.
11518 *
11519 * This routine is to free all the pending completion-queue events to the
11520 * back into the free pool for device reset.
11521 **/
11522static void
11523lpfc_sli4_cq_event_release_all(struct lpfc_hba *phba)
11524{
James Smarte7dab162020-10-20 13:27:12 -070011525 LIST_HEAD(cq_event_list);
11526 struct lpfc_cq_event *cq_event;
James Smartda0436e2009-05-22 14:51:39 -040011527 unsigned long iflags;
11528
11529 /* Retrieve all the pending WCQEs from pending WCQE lists */
James Smartda0436e2009-05-22 14:51:39 -040011530
James Smarte7dab162020-10-20 13:27:12 -070011531 /* Pending ELS XRI abort events */
11532 spin_lock_irqsave(&phba->sli4_hba.els_xri_abrt_list_lock, iflags);
11533 list_splice_init(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
11534 &cq_event_list);
11535 spin_unlock_irqrestore(&phba->sli4_hba.els_xri_abrt_list_lock, iflags);
11536
11537 /* Pending asynnc events */
11538 spin_lock_irqsave(&phba->sli4_hba.asynce_list_lock, iflags);
11539 list_splice_init(&phba->sli4_hba.sp_asynce_work_queue,
11540 &cq_event_list);
11541 spin_unlock_irqrestore(&phba->sli4_hba.asynce_list_lock, iflags);
11542
11543 while (!list_empty(&cq_event_list)) {
11544 list_remove_head(&cq_event_list, cq_event,
11545 struct lpfc_cq_event, list);
11546 lpfc_sli4_cq_event_release(phba, cq_event);
James Smartda0436e2009-05-22 14:51:39 -040011547 }
11548}
11549
11550/**
11551 * lpfc_pci_function_reset - Reset pci function.
11552 * @phba: pointer to lpfc hba data structure.
11553 *
11554 * This routine is invoked to request a PCI function reset. It will destroys
11555 * all resources assigned to the PCI function which originates this request.
11556 *
11557 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020011558 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011559 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -040011560 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -040011561 **/
11562int
11563lpfc_pci_function_reset(struct lpfc_hba *phba)
11564{
11565 LPFC_MBOXQ_t *mboxq;
James Smart2fcee4b2010-12-15 17:57:46 -050011566 uint32_t rc = 0, if_type;
James Smartda0436e2009-05-22 14:51:39 -040011567 uint32_t shdr_status, shdr_add_status;
James Smart2f6fa2c2014-09-03 12:57:08 -040011568 uint32_t rdy_chk;
11569 uint32_t port_reset = 0;
James Smartda0436e2009-05-22 14:51:39 -040011570 union lpfc_sli4_cfg_shdr *shdr;
James Smart2fcee4b2010-12-15 17:57:46 -050011571 struct lpfc_register reg_data;
James Smart2b81f942012-03-01 22:37:18 -050011572 uint16_t devid;
James Smartda0436e2009-05-22 14:51:39 -040011573
James Smart2fcee4b2010-12-15 17:57:46 -050011574 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
11575 switch (if_type) {
11576 case LPFC_SLI_INTF_IF_TYPE_0:
11577 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
11578 GFP_KERNEL);
11579 if (!mboxq) {
Dick Kennedy372c1872020-06-30 14:50:00 -070011580 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2fcee4b2010-12-15 17:57:46 -050011581 "0494 Unable to allocate memory for "
11582 "issuing SLI_FUNCTION_RESET mailbox "
11583 "command\n");
11584 return -ENOMEM;
11585 }
11586
11587 /* Setup PCI function reset mailbox-ioctl command */
11588 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
11589 LPFC_MBOX_OPCODE_FUNCTION_RESET, 0,
11590 LPFC_SLI4_MBX_EMBED);
11591 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
11592 shdr = (union lpfc_sli4_cfg_shdr *)
11593 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
11594 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11595 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
11596 &shdr->response);
James Smart304ee432021-04-11 18:31:17 -070011597 mempool_free(mboxq, phba->mbox_mem_pool);
James Smart2fcee4b2010-12-15 17:57:46 -050011598 if (shdr_status || shdr_add_status || rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -070011599 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2fcee4b2010-12-15 17:57:46 -050011600 "0495 SLI_FUNCTION_RESET mailbox "
11601 "failed with status x%x add_status x%x,"
11602 " mbx status x%x\n",
11603 shdr_status, shdr_add_status, rc);
11604 rc = -ENXIO;
11605 }
11606 break;
11607 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart27d6ac02018-02-22 08:18:42 -080011608 case LPFC_SLI_INTF_IF_TYPE_6:
James Smart2f6fa2c2014-09-03 12:57:08 -040011609wait:
11610 /*
11611 * Poll the Port Status Register and wait for RDY for
11612 * up to 30 seconds. If the port doesn't respond, treat
11613 * it as an error.
11614 */
James Smart77d093f2015-04-07 15:07:08 -040011615 for (rdy_chk = 0; rdy_chk < 1500; rdy_chk++) {
James Smart2f6fa2c2014-09-03 12:57:08 -040011616 if (lpfc_readl(phba->sli4_hba.u.if_type2.
11617 STATUSregaddr, &reg_data.word0)) {
11618 rc = -ENODEV;
11619 goto out;
11620 }
11621 if (bf_get(lpfc_sliport_status_rdy, &reg_data))
11622 break;
11623 msleep(20);
11624 }
11625
11626 if (!bf_get(lpfc_sliport_status_rdy, &reg_data)) {
11627 phba->work_status[0] = readl(
11628 phba->sli4_hba.u.if_type2.ERR1regaddr);
11629 phba->work_status[1] = readl(
11630 phba->sli4_hba.u.if_type2.ERR2regaddr);
Dick Kennedy372c1872020-06-30 14:50:00 -070011631 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2f6fa2c2014-09-03 12:57:08 -040011632 "2890 Port not ready, port status reg "
11633 "0x%x error 1=0x%x, error 2=0x%x\n",
11634 reg_data.word0,
11635 phba->work_status[0],
11636 phba->work_status[1]);
11637 rc = -ENODEV;
11638 goto out;
11639 }
11640
James Smarta5b141a2021-09-27 11:35:18 -070011641 if (bf_get(lpfc_sliport_status_pldv, &reg_data))
11642 lpfc_pldv_detect = true;
11643
James Smart2f6fa2c2014-09-03 12:57:08 -040011644 if (!port_reset) {
11645 /*
11646 * Reset the port now
11647 */
James Smart2fcee4b2010-12-15 17:57:46 -050011648 reg_data.word0 = 0;
11649 bf_set(lpfc_sliport_ctrl_end, &reg_data,
11650 LPFC_SLIPORT_LITTLE_ENDIAN);
11651 bf_set(lpfc_sliport_ctrl_ip, &reg_data,
11652 LPFC_SLIPORT_INIT_PORT);
11653 writel(reg_data.word0, phba->sli4_hba.u.if_type2.
11654 CTRLregaddr);
James Smart8fcb8ac2012-03-01 22:35:58 -050011655 /* flush */
James Smart2b81f942012-03-01 22:37:18 -050011656 pci_read_config_word(phba->pcidev,
11657 PCI_DEVICE_ID, &devid);
James Smart2fcee4b2010-12-15 17:57:46 -050011658
James Smart2f6fa2c2014-09-03 12:57:08 -040011659 port_reset = 1;
11660 msleep(20);
11661 goto wait;
11662 } else if (bf_get(lpfc_sliport_status_rn, &reg_data)) {
11663 rc = -ENODEV;
11664 goto out;
James Smart2fcee4b2010-12-15 17:57:46 -050011665 }
11666 break;
James Smart2f6fa2c2014-09-03 12:57:08 -040011667
James Smart2fcee4b2010-12-15 17:57:46 -050011668 case LPFC_SLI_INTF_IF_TYPE_1:
11669 default:
11670 break;
James Smartda0436e2009-05-22 14:51:39 -040011671 }
11672
James Smart73d91e52011-10-10 21:32:10 -040011673out:
James Smart2fcee4b2010-12-15 17:57:46 -050011674 /* Catch the not-ready port failure after a port reset. */
James Smart2f6fa2c2014-09-03 12:57:08 -040011675 if (rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -070011676 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart229adb02013-04-17 20:16:51 -040011677 "3317 HBA not functional: IP Reset Failed "
James Smart2f6fa2c2014-09-03 12:57:08 -040011678 "try: echo fw_reset > board_mode\n");
James Smart2fcee4b2010-12-15 17:57:46 -050011679 rc = -ENODEV;
James Smart229adb02013-04-17 20:16:51 -040011680 }
James Smart2fcee4b2010-12-15 17:57:46 -050011681
James Smartda0436e2009-05-22 14:51:39 -040011682 return rc;
11683}
11684
11685/**
James Smartda0436e2009-05-22 14:51:39 -040011686 * lpfc_sli4_pci_mem_setup - Setup SLI4 HBA PCI memory space.
11687 * @phba: pointer to lpfc hba data structure.
11688 *
11689 * This routine is invoked to set up the PCI device memory space for device
11690 * with SLI-4 interface spec.
11691 *
11692 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020011693 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -040011694 * other values - error
11695 **/
11696static int
11697lpfc_sli4_pci_mem_setup(struct lpfc_hba *phba)
11698{
Christoph Hellwigf30e1bf2018-10-18 15:10:21 +020011699 struct pci_dev *pdev = phba->pcidev;
James Smartda0436e2009-05-22 14:51:39 -040011700 unsigned long bar0map_len, bar1map_len, bar2map_len;
Dan Carpenter3a487ff2019-03-07 08:33:44 +030011701 int error;
James Smart2fcee4b2010-12-15 17:57:46 -050011702 uint32_t if_type;
James Smartda0436e2009-05-22 14:51:39 -040011703
Christoph Hellwigf30e1bf2018-10-18 15:10:21 +020011704 if (!pdev)
Hannes Reinecke56de8352019-02-18 08:34:19 +010011705 return -ENODEV;
James Smartda0436e2009-05-22 14:51:39 -040011706
11707 /* Set the device DMA mask size */
Hannes Reinecke56de8352019-02-18 08:34:19 +010011708 error = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
11709 if (error)
11710 error = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
11711 if (error)
Christoph Hellwigf30e1bf2018-10-18 15:10:21 +020011712 return error;
James Smartda0436e2009-05-22 14:51:39 -040011713
James Smart2fcee4b2010-12-15 17:57:46 -050011714 /*
11715 * The BARs and register set definitions and offset locations are
11716 * dependent on the if_type.
11717 */
11718 if (pci_read_config_dword(pdev, LPFC_SLI_INTF,
11719 &phba->sli4_hba.sli_intf.word0)) {
Dan Carpenter3a487ff2019-03-07 08:33:44 +030011720 return -ENODEV;
James Smart2fcee4b2010-12-15 17:57:46 -050011721 }
11722
11723 /* There is no SLI3 failback for SLI4 devices. */
11724 if (bf_get(lpfc_sli_intf_valid, &phba->sli4_hba.sli_intf) !=
11725 LPFC_SLI_INTF_VALID) {
James Smarta5160742021-10-20 14:14:10 -070011726 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -050011727 "2894 SLI_INTF reg contents invalid "
11728 "sli_intf reg 0x%x\n",
11729 phba->sli4_hba.sli_intf.word0);
Dan Carpenter3a487ff2019-03-07 08:33:44 +030011730 return -ENODEV;
James Smart2fcee4b2010-12-15 17:57:46 -050011731 }
11732
11733 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
11734 /*
11735 * Get the bus address of SLI4 device Bar regions and the
11736 * number of bytes required by each mapping. The mapping of the
11737 * particular PCI BARs regions is dependent on the type of
11738 * SLI4 device.
James Smartda0436e2009-05-22 14:51:39 -040011739 */
James Smartf5ca6f22013-09-06 12:21:09 -040011740 if (pci_resource_start(pdev, PCI_64BIT_BAR0)) {
11741 phba->pci_bar0_map = pci_resource_start(pdev, PCI_64BIT_BAR0);
11742 bar0map_len = pci_resource_len(pdev, PCI_64BIT_BAR0);
James Smart2fcee4b2010-12-15 17:57:46 -050011743
11744 /*
11745 * Map SLI4 PCI Config Space Register base to a kernel virtual
11746 * addr
11747 */
11748 phba->sli4_hba.conf_regs_memmap_p =
11749 ioremap(phba->pci_bar0_map, bar0map_len);
11750 if (!phba->sli4_hba.conf_regs_memmap_p) {
11751 dev_printk(KERN_ERR, &pdev->dev,
11752 "ioremap failed for SLI4 PCI config "
11753 "registers.\n");
Dan Carpenter3a487ff2019-03-07 08:33:44 +030011754 return -ENODEV;
James Smart2fcee4b2010-12-15 17:57:46 -050011755 }
James Smartf5ca6f22013-09-06 12:21:09 -040011756 phba->pci_bar0_memmap_p = phba->sli4_hba.conf_regs_memmap_p;
James Smart2fcee4b2010-12-15 17:57:46 -050011757 /* Set up BAR0 PCI config space register memory map */
11758 lpfc_sli4_bar0_register_memmap(phba, if_type);
James Smart1dfb5a42010-02-12 14:40:50 -050011759 } else {
11760 phba->pci_bar0_map = pci_resource_start(pdev, 1);
11761 bar0map_len = pci_resource_len(pdev, 1);
James Smart27d6ac02018-02-22 08:18:42 -080011762 if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
James Smart2fcee4b2010-12-15 17:57:46 -050011763 dev_printk(KERN_ERR, &pdev->dev,
11764 "FATAL - No BAR0 mapping for SLI4, if_type 2\n");
Dan Carpenter3a487ff2019-03-07 08:33:44 +030011765 return -ENODEV;
James Smart2fcee4b2010-12-15 17:57:46 -050011766 }
11767 phba->sli4_hba.conf_regs_memmap_p =
James Smartda0436e2009-05-22 14:51:39 -040011768 ioremap(phba->pci_bar0_map, bar0map_len);
James Smart2fcee4b2010-12-15 17:57:46 -050011769 if (!phba->sli4_hba.conf_regs_memmap_p) {
11770 dev_printk(KERN_ERR, &pdev->dev,
11771 "ioremap failed for SLI4 PCI config "
11772 "registers.\n");
Dan Carpenter3a487ff2019-03-07 08:33:44 +030011773 return -ENODEV;
James Smart2fcee4b2010-12-15 17:57:46 -050011774 }
11775 lpfc_sli4_bar0_register_memmap(phba, if_type);
James Smartda0436e2009-05-22 14:51:39 -040011776 }
11777
James Smarte4b97942017-11-20 16:00:31 -080011778 if (if_type == LPFC_SLI_INTF_IF_TYPE_0) {
11779 if (pci_resource_start(pdev, PCI_64BIT_BAR2)) {
11780 /*
11781 * Map SLI4 if type 0 HBA Control Register base to a
11782 * kernel virtual address and setup the registers.
11783 */
11784 phba->pci_bar1_map = pci_resource_start(pdev,
11785 PCI_64BIT_BAR2);
11786 bar1map_len = pci_resource_len(pdev, PCI_64BIT_BAR2);
11787 phba->sli4_hba.ctrl_regs_memmap_p =
11788 ioremap(phba->pci_bar1_map,
11789 bar1map_len);
11790 if (!phba->sli4_hba.ctrl_regs_memmap_p) {
11791 dev_err(&pdev->dev,
11792 "ioremap failed for SLI4 HBA "
11793 "control registers.\n");
11794 error = -ENOMEM;
11795 goto out_iounmap_conf;
11796 }
11797 phba->pci_bar2_memmap_p =
11798 phba->sli4_hba.ctrl_regs_memmap_p;
James Smart27d6ac02018-02-22 08:18:42 -080011799 lpfc_sli4_bar1_register_memmap(phba, if_type);
James Smarte4b97942017-11-20 16:00:31 -080011800 } else {
11801 error = -ENOMEM;
James Smart2fcee4b2010-12-15 17:57:46 -050011802 goto out_iounmap_conf;
11803 }
James Smartda0436e2009-05-22 14:51:39 -040011804 }
11805
James Smart27d6ac02018-02-22 08:18:42 -080011806 if ((if_type == LPFC_SLI_INTF_IF_TYPE_6) &&
11807 (pci_resource_start(pdev, PCI_64BIT_BAR2))) {
11808 /*
11809 * Map SLI4 if type 6 HBA Doorbell Register base to a kernel
11810 * virtual address and setup the registers.
11811 */
11812 phba->pci_bar1_map = pci_resource_start(pdev, PCI_64BIT_BAR2);
11813 bar1map_len = pci_resource_len(pdev, PCI_64BIT_BAR2);
11814 phba->sli4_hba.drbl_regs_memmap_p =
11815 ioremap(phba->pci_bar1_map, bar1map_len);
11816 if (!phba->sli4_hba.drbl_regs_memmap_p) {
11817 dev_err(&pdev->dev,
11818 "ioremap failed for SLI4 HBA doorbell registers.\n");
Dan Carpenter3a487ff2019-03-07 08:33:44 +030011819 error = -ENOMEM;
James Smart27d6ac02018-02-22 08:18:42 -080011820 goto out_iounmap_conf;
11821 }
11822 phba->pci_bar2_memmap_p = phba->sli4_hba.drbl_regs_memmap_p;
11823 lpfc_sli4_bar1_register_memmap(phba, if_type);
11824 }
11825
James Smarte4b97942017-11-20 16:00:31 -080011826 if (if_type == LPFC_SLI_INTF_IF_TYPE_0) {
11827 if (pci_resource_start(pdev, PCI_64BIT_BAR4)) {
11828 /*
11829 * Map SLI4 if type 0 HBA Doorbell Register base to
11830 * a kernel virtual address and setup the registers.
11831 */
11832 phba->pci_bar2_map = pci_resource_start(pdev,
11833 PCI_64BIT_BAR4);
11834 bar2map_len = pci_resource_len(pdev, PCI_64BIT_BAR4);
11835 phba->sli4_hba.drbl_regs_memmap_p =
11836 ioremap(phba->pci_bar2_map,
11837 bar2map_len);
11838 if (!phba->sli4_hba.drbl_regs_memmap_p) {
11839 dev_err(&pdev->dev,
11840 "ioremap failed for SLI4 HBA"
11841 " doorbell registers.\n");
11842 error = -ENOMEM;
11843 goto out_iounmap_ctrl;
11844 }
11845 phba->pci_bar4_memmap_p =
11846 phba->sli4_hba.drbl_regs_memmap_p;
11847 error = lpfc_sli4_bar2_register_memmap(phba, LPFC_VF0);
11848 if (error)
11849 goto out_iounmap_all;
11850 } else {
11851 error = -ENOMEM;
James Smart2fcee4b2010-12-15 17:57:46 -050011852 goto out_iounmap_all;
James Smarte4b97942017-11-20 16:00:31 -080011853 }
James Smartda0436e2009-05-22 14:51:39 -040011854 }
11855
James Smart1351e692018-02-22 08:18:43 -080011856 if (if_type == LPFC_SLI_INTF_IF_TYPE_6 &&
11857 pci_resource_start(pdev, PCI_64BIT_BAR4)) {
11858 /*
11859 * Map SLI4 if type 6 HBA DPP Register base to a kernel
11860 * virtual address and setup the registers.
11861 */
11862 phba->pci_bar2_map = pci_resource_start(pdev, PCI_64BIT_BAR4);
11863 bar2map_len = pci_resource_len(pdev, PCI_64BIT_BAR4);
11864 phba->sli4_hba.dpp_regs_memmap_p =
11865 ioremap(phba->pci_bar2_map, bar2map_len);
11866 if (!phba->sli4_hba.dpp_regs_memmap_p) {
11867 dev_err(&pdev->dev,
11868 "ioremap failed for SLI4 HBA dpp registers.\n");
Dan Carpenter3a487ff2019-03-07 08:33:44 +030011869 error = -ENOMEM;
James Smart1351e692018-02-22 08:18:43 -080011870 goto out_iounmap_ctrl;
11871 }
11872 phba->pci_bar4_memmap_p = phba->sli4_hba.dpp_regs_memmap_p;
11873 }
11874
James Smartb71413d2018-02-22 08:18:40 -080011875 /* Set up the EQ/CQ register handeling functions now */
James Smart27d6ac02018-02-22 08:18:42 -080011876 switch (if_type) {
11877 case LPFC_SLI_INTF_IF_TYPE_0:
11878 case LPFC_SLI_INTF_IF_TYPE_2:
James Smartb71413d2018-02-22 08:18:40 -080011879 phba->sli4_hba.sli4_eq_clr_intr = lpfc_sli4_eq_clr_intr;
James Smart32517fc2019-01-28 11:14:33 -080011880 phba->sli4_hba.sli4_write_eq_db = lpfc_sli4_write_eq_db;
11881 phba->sli4_hba.sli4_write_cq_db = lpfc_sli4_write_cq_db;
James Smart27d6ac02018-02-22 08:18:42 -080011882 break;
11883 case LPFC_SLI_INTF_IF_TYPE_6:
11884 phba->sli4_hba.sli4_eq_clr_intr = lpfc_sli4_if6_eq_clr_intr;
James Smart32517fc2019-01-28 11:14:33 -080011885 phba->sli4_hba.sli4_write_eq_db = lpfc_sli4_if6_write_eq_db;
11886 phba->sli4_hba.sli4_write_cq_db = lpfc_sli4_if6_write_cq_db;
James Smart27d6ac02018-02-22 08:18:42 -080011887 break;
11888 default:
11889 break;
James Smartb71413d2018-02-22 08:18:40 -080011890 }
11891
James Smartda0436e2009-05-22 14:51:39 -040011892 return 0;
11893
11894out_iounmap_all:
11895 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
11896out_iounmap_ctrl:
11897 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
11898out_iounmap_conf:
11899 iounmap(phba->sli4_hba.conf_regs_memmap_p);
Dan Carpenter3a487ff2019-03-07 08:33:44 +030011900
James Smartda0436e2009-05-22 14:51:39 -040011901 return error;
11902}
11903
11904/**
11905 * lpfc_sli4_pci_mem_unset - Unset SLI4 HBA PCI memory space.
11906 * @phba: pointer to lpfc hba data structure.
11907 *
11908 * This routine is invoked to unset the PCI device memory space for device
11909 * with SLI-4 interface spec.
11910 **/
11911static void
11912lpfc_sli4_pci_mem_unset(struct lpfc_hba *phba)
11913{
James Smart2e90f4b2011-12-13 13:22:37 -050011914 uint32_t if_type;
11915 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
James Smartda0436e2009-05-22 14:51:39 -040011916
James Smart2e90f4b2011-12-13 13:22:37 -050011917 switch (if_type) {
11918 case LPFC_SLI_INTF_IF_TYPE_0:
11919 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
11920 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
11921 iounmap(phba->sli4_hba.conf_regs_memmap_p);
11922 break;
11923 case LPFC_SLI_INTF_IF_TYPE_2:
11924 iounmap(phba->sli4_hba.conf_regs_memmap_p);
11925 break;
James Smart27d6ac02018-02-22 08:18:42 -080011926 case LPFC_SLI_INTF_IF_TYPE_6:
11927 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
11928 iounmap(phba->sli4_hba.conf_regs_memmap_p);
James Smart0b439192019-12-18 15:58:05 -080011929 if (phba->sli4_hba.dpp_regs_memmap_p)
11930 iounmap(phba->sli4_hba.dpp_regs_memmap_p);
James Smart27d6ac02018-02-22 08:18:42 -080011931 break;
James Smart2e90f4b2011-12-13 13:22:37 -050011932 case LPFC_SLI_INTF_IF_TYPE_1:
11933 default:
11934 dev_printk(KERN_ERR, &phba->pcidev->dev,
11935 "FATAL - unsupported SLI4 interface type - %d\n",
11936 if_type);
11937 break;
11938 }
James Smartda0436e2009-05-22 14:51:39 -040011939}
11940
11941/**
James Smart3772a992009-05-22 14:50:54 -040011942 * lpfc_sli_enable_msix - Enable MSI-X interrupt mode on SLI-3 device
11943 * @phba: pointer to lpfc hba data structure.
11944 *
11945 * This routine is invoked to enable the MSI-X interrupt vectors to device
Christoph Hellwig45ffac12017-02-12 13:52:26 -080011946 * with SLI-3 interface specs.
James Smart3772a992009-05-22 14:50:54 -040011947 *
11948 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020011949 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -040011950 * other values - error
11951 **/
11952static int
11953lpfc_sli_enable_msix(struct lpfc_hba *phba)
11954{
Christoph Hellwig45ffac12017-02-12 13:52:26 -080011955 int rc;
James Smart3772a992009-05-22 14:50:54 -040011956 LPFC_MBOXQ_t *pmb;
11957
11958 /* Set up MSI-X multi-message vectors */
Christoph Hellwig45ffac12017-02-12 13:52:26 -080011959 rc = pci_alloc_irq_vectors(phba->pcidev,
11960 LPFC_MSIX_VECTORS, LPFC_MSIX_VECTORS, PCI_IRQ_MSIX);
11961 if (rc < 0) {
James Smart3772a992009-05-22 14:50:54 -040011962 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
11963 "0420 PCI enable MSI-X failed (%d)\n", rc);
Alexander Gordeev029165a2014-07-16 20:05:15 +020011964 goto vec_fail_out;
James Smart3772a992009-05-22 14:50:54 -040011965 }
Christoph Hellwig45ffac12017-02-12 13:52:26 -080011966
James Smart3772a992009-05-22 14:50:54 -040011967 /*
11968 * Assign MSI-X vectors to interrupt handlers
11969 */
11970
11971 /* vector-0 is associated to slow-path handler */
Christoph Hellwig45ffac12017-02-12 13:52:26 -080011972 rc = request_irq(pci_irq_vector(phba->pcidev, 0),
James Smarted243d32015-05-21 13:55:25 -040011973 &lpfc_sli_sp_intr_handler, 0,
James Smart3772a992009-05-22 14:50:54 -040011974 LPFC_SP_DRIVER_HANDLER_NAME, phba);
11975 if (rc) {
11976 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
11977 "0421 MSI-X slow-path request_irq failed "
11978 "(%d)\n", rc);
11979 goto msi_fail_out;
11980 }
11981
11982 /* vector-1 is associated to fast-path handler */
Christoph Hellwig45ffac12017-02-12 13:52:26 -080011983 rc = request_irq(pci_irq_vector(phba->pcidev, 1),
James Smarted243d32015-05-21 13:55:25 -040011984 &lpfc_sli_fp_intr_handler, 0,
James Smart3772a992009-05-22 14:50:54 -040011985 LPFC_FP_DRIVER_HANDLER_NAME, phba);
11986
11987 if (rc) {
11988 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
11989 "0429 MSI-X fast-path request_irq failed "
11990 "(%d)\n", rc);
11991 goto irq_fail_out;
11992 }
11993
11994 /*
11995 * Configure HBA MSI-X attention conditions to messages
11996 */
11997 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11998
11999 if (!pmb) {
12000 rc = -ENOMEM;
Dick Kennedy372c1872020-06-30 14:50:00 -070012001 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart3772a992009-05-22 14:50:54 -040012002 "0474 Unable to allocate memory for issuing "
12003 "MBOX_CONFIG_MSI command\n");
12004 goto mem_fail_out;
12005 }
12006 rc = lpfc_config_msi(phba, pmb);
12007 if (rc)
12008 goto mbx_fail_out;
12009 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
12010 if (rc != MBX_SUCCESS) {
12011 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
12012 "0351 Config MSI mailbox command failed, "
12013 "mbxCmd x%x, mbxStatus x%x\n",
12014 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus);
12015 goto mbx_fail_out;
12016 }
12017
12018 /* Free memory allocated for mailbox command */
12019 mempool_free(pmb, phba->mbox_mem_pool);
12020 return rc;
12021
12022mbx_fail_out:
12023 /* Free memory allocated for mailbox command */
12024 mempool_free(pmb, phba->mbox_mem_pool);
12025
12026mem_fail_out:
12027 /* free the irq already requested */
Christoph Hellwig45ffac12017-02-12 13:52:26 -080012028 free_irq(pci_irq_vector(phba->pcidev, 1), phba);
James Smart3772a992009-05-22 14:50:54 -040012029
12030irq_fail_out:
12031 /* free the irq already requested */
Christoph Hellwig45ffac12017-02-12 13:52:26 -080012032 free_irq(pci_irq_vector(phba->pcidev, 0), phba);
James Smart3772a992009-05-22 14:50:54 -040012033
12034msi_fail_out:
12035 /* Unconfigure MSI-X capability structure */
Christoph Hellwig45ffac12017-02-12 13:52:26 -080012036 pci_free_irq_vectors(phba->pcidev);
Alexander Gordeev029165a2014-07-16 20:05:15 +020012037
12038vec_fail_out:
James Smart3772a992009-05-22 14:50:54 -040012039 return rc;
12040}
12041
12042/**
James Smart3772a992009-05-22 14:50:54 -040012043 * lpfc_sli_enable_msi - Enable MSI interrupt mode on SLI-3 device.
12044 * @phba: pointer to lpfc hba data structure.
12045 *
12046 * This routine is invoked to enable the MSI interrupt mode to device with
12047 * SLI-3 interface spec. The kernel function pci_enable_msi() is called to
12048 * enable the MSI vector. The device driver is responsible for calling the
12049 * request_irq() to register MSI vector with a interrupt the handler, which
12050 * is done in this function.
12051 *
12052 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020012053 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -040012054 * other values - error
12055 */
12056static int
12057lpfc_sli_enable_msi(struct lpfc_hba *phba)
12058{
12059 int rc;
12060
12061 rc = pci_enable_msi(phba->pcidev);
12062 if (!rc)
12063 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
12064 "0462 PCI enable MSI mode success.\n");
12065 else {
12066 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
12067 "0471 PCI enable MSI mode failed (%d)\n", rc);
12068 return rc;
12069 }
12070
12071 rc = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
James Smarted243d32015-05-21 13:55:25 -040012072 0, LPFC_DRIVER_NAME, phba);
James Smart3772a992009-05-22 14:50:54 -040012073 if (rc) {
12074 pci_disable_msi(phba->pcidev);
12075 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
12076 "0478 MSI request_irq failed (%d)\n", rc);
12077 }
12078 return rc;
12079}
12080
12081/**
James Smart3772a992009-05-22 14:50:54 -040012082 * lpfc_sli_enable_intr - Enable device interrupt to SLI-3 device.
12083 * @phba: pointer to lpfc hba data structure.
Lee Jonesfe614ac2020-07-23 13:24:22 +010012084 * @cfg_mode: Interrupt configuration mode (INTx, MSI or MSI-X).
James Smart3772a992009-05-22 14:50:54 -040012085 *
12086 * This routine is invoked to enable device interrupt and associate driver's
12087 * interrupt handler(s) to interrupt vector(s) to device with SLI-3 interface
12088 * spec. Depends on the interrupt mode configured to the driver, the driver
12089 * will try to fallback from the configured interrupt mode to an interrupt
12090 * mode which is supported by the platform, kernel, and device in the order
12091 * of:
12092 * MSI-X -> MSI -> IRQ.
12093 *
12094 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020012095 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -040012096 * other values - error
12097 **/
12098static uint32_t
12099lpfc_sli_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
12100{
12101 uint32_t intr_mode = LPFC_INTR_ERROR;
12102 int retval;
12103
James Smartd2f25472021-01-04 10:02:27 -080012104 /* Need to issue conf_port mbox cmd before conf_msi mbox cmd */
12105 retval = lpfc_sli_config_port(phba, LPFC_SLI_REV3);
12106 if (retval)
12107 return intr_mode;
12108 phba->hba_flag &= ~HBA_NEEDS_CFG_PORT;
12109
James Smart3772a992009-05-22 14:50:54 -040012110 if (cfg_mode == 2) {
James Smartd2f25472021-01-04 10:02:27 -080012111 /* Now, try to enable MSI-X interrupt mode */
12112 retval = lpfc_sli_enable_msix(phba);
James Smart3772a992009-05-22 14:50:54 -040012113 if (!retval) {
James Smartd2f25472021-01-04 10:02:27 -080012114 /* Indicate initialization to MSI-X mode */
12115 phba->intr_type = MSIX;
12116 intr_mode = 2;
James Smart3772a992009-05-22 14:50:54 -040012117 }
12118 }
12119
12120 /* Fallback to MSI if MSI-X initialization failed */
12121 if (cfg_mode >= 1 && phba->intr_type == NONE) {
12122 retval = lpfc_sli_enable_msi(phba);
12123 if (!retval) {
12124 /* Indicate initialization to MSI mode */
12125 phba->intr_type = MSI;
12126 intr_mode = 1;
12127 }
12128 }
12129
12130 /* Fallback to INTx if both MSI-X/MSI initalization failed */
12131 if (phba->intr_type == NONE) {
12132 retval = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
12133 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
12134 if (!retval) {
12135 /* Indicate initialization to INTx mode */
12136 phba->intr_type = INTx;
12137 intr_mode = 0;
12138 }
12139 }
12140 return intr_mode;
12141}
12142
12143/**
12144 * lpfc_sli_disable_intr - Disable device interrupt to SLI-3 device.
12145 * @phba: pointer to lpfc hba data structure.
12146 *
12147 * This routine is invoked to disable device interrupt and disassociate the
12148 * driver's interrupt handler(s) from interrupt vector(s) to device with
12149 * SLI-3 interface spec. Depending on the interrupt mode, the driver will
12150 * release the interrupt vector(s) for the message signaled interrupt.
12151 **/
12152static void
12153lpfc_sli_disable_intr(struct lpfc_hba *phba)
12154{
Christoph Hellwig45ffac12017-02-12 13:52:26 -080012155 int nr_irqs, i;
12156
James Smart3772a992009-05-22 14:50:54 -040012157 if (phba->intr_type == MSIX)
Christoph Hellwig45ffac12017-02-12 13:52:26 -080012158 nr_irqs = LPFC_MSIX_VECTORS;
12159 else
12160 nr_irqs = 1;
12161
12162 for (i = 0; i < nr_irqs; i++)
12163 free_irq(pci_irq_vector(phba->pcidev, i), phba);
12164 pci_free_irq_vectors(phba->pcidev);
James Smart3772a992009-05-22 14:50:54 -040012165
12166 /* Reset interrupt management states */
12167 phba->intr_type = NONE;
12168 phba->sli.slistat.sli_intr = 0;
James Smart3772a992009-05-22 14:50:54 -040012169}
12170
12171/**
James Smart657add42019-05-21 17:49:06 -070012172 * lpfc_find_cpu_handle - Find the CPU that corresponds to the specified Queue
James Smart6a828b02019-01-28 11:14:31 -080012173 * @phba: pointer to lpfc hba data structure.
12174 * @id: EQ vector index or Hardware Queue index
12175 * @match: LPFC_FIND_BY_EQ = match by EQ
12176 * LPFC_FIND_BY_HDWQ = match by Hardware Queue
James Smart657add42019-05-21 17:49:06 -070012177 * Return the CPU that matches the selection criteria
James Smart6a828b02019-01-28 11:14:31 -080012178 */
12179static uint16_t
12180lpfc_find_cpu_handle(struct lpfc_hba *phba, uint16_t id, int match)
12181{
12182 struct lpfc_vector_map_info *cpup;
12183 int cpu;
12184
James Smart657add42019-05-21 17:49:06 -070012185 /* Loop through all CPUs */
James Smart222e9232019-01-28 11:14:35 -080012186 for_each_present_cpu(cpu) {
12187 cpup = &phba->sli4_hba.cpu_map[cpu];
James Smart657add42019-05-21 17:49:06 -070012188
12189 /* If we are matching by EQ, there may be multiple CPUs using
12190 * using the same vector, so select the one with
12191 * LPFC_CPU_FIRST_IRQ set.
12192 */
James Smart6a828b02019-01-28 11:14:31 -080012193 if ((match == LPFC_FIND_BY_EQ) &&
James Smart657add42019-05-21 17:49:06 -070012194 (cpup->flag & LPFC_CPU_FIRST_IRQ) &&
James Smart6a828b02019-01-28 11:14:31 -080012195 (cpup->eq == id))
12196 return cpu;
James Smart657add42019-05-21 17:49:06 -070012197
12198 /* If matching by HDWQ, select the first CPU that matches */
James Smart6a828b02019-01-28 11:14:31 -080012199 if ((match == LPFC_FIND_BY_HDWQ) && (cpup->hdwq == id))
12200 return cpu;
James Smart6a828b02019-01-28 11:14:31 -080012201 }
12202 return 0;
12203}
12204
James Smart6a828b02019-01-28 11:14:31 -080012205#ifdef CONFIG_X86
12206/**
12207 * lpfc_find_hyper - Determine if the CPU map entry is hyper-threaded
12208 * @phba: pointer to lpfc hba data structure.
12209 * @cpu: CPU map index
12210 * @phys_id: CPU package physical id
12211 * @core_id: CPU core id
12212 */
12213static int
12214lpfc_find_hyper(struct lpfc_hba *phba, int cpu,
12215 uint16_t phys_id, uint16_t core_id)
12216{
12217 struct lpfc_vector_map_info *cpup;
12218 int idx;
12219
James Smart222e9232019-01-28 11:14:35 -080012220 for_each_present_cpu(idx) {
12221 cpup = &phba->sli4_hba.cpu_map[idx];
James Smart6a828b02019-01-28 11:14:31 -080012222 /* Does the cpup match the one we are looking for */
12223 if ((cpup->phys_id == phys_id) &&
12224 (cpup->core_id == core_id) &&
James Smart222e9232019-01-28 11:14:35 -080012225 (cpu != idx))
James Smart6a828b02019-01-28 11:14:31 -080012226 return 1;
James Smart6a828b02019-01-28 11:14:31 -080012227 }
12228 return 0;
12229}
12230#endif
12231
James Smartdcaa2132019-11-04 16:57:06 -080012232/*
12233 * lpfc_assign_eq_map_info - Assigns eq for vector_map structure
12234 * @phba: pointer to lpfc hba data structure.
12235 * @eqidx: index for eq and irq vector
12236 * @flag: flags to set for vector_map structure
12237 * @cpu: cpu used to index vector_map structure
12238 *
12239 * The routine assigns eq info into vector_map structure
12240 */
12241static inline void
12242lpfc_assign_eq_map_info(struct lpfc_hba *phba, uint16_t eqidx, uint16_t flag,
12243 unsigned int cpu)
12244{
12245 struct lpfc_vector_map_info *cpup = &phba->sli4_hba.cpu_map[cpu];
12246 struct lpfc_hba_eq_hdl *eqhdl = lpfc_get_eq_hdl(eqidx);
12247
12248 cpup->eq = eqidx;
12249 cpup->flag |= flag;
12250
12251 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
12252 "3336 Set Affinity: CPU %d irq %d eq %d flag x%x\n",
12253 cpu, eqhdl->irq, cpup->eq, cpup->flag);
12254}
12255
12256/**
12257 * lpfc_cpu_map_array_init - Initialize cpu_map structure
12258 * @phba: pointer to lpfc hba data structure.
12259 *
12260 * The routine initializes the cpu_map array structure
12261 */
12262static void
12263lpfc_cpu_map_array_init(struct lpfc_hba *phba)
12264{
12265 struct lpfc_vector_map_info *cpup;
12266 struct lpfc_eq_intr_info *eqi;
12267 int cpu;
12268
12269 for_each_possible_cpu(cpu) {
12270 cpup = &phba->sli4_hba.cpu_map[cpu];
12271 cpup->phys_id = LPFC_VECTOR_MAP_EMPTY;
12272 cpup->core_id = LPFC_VECTOR_MAP_EMPTY;
12273 cpup->hdwq = LPFC_VECTOR_MAP_EMPTY;
12274 cpup->eq = LPFC_VECTOR_MAP_EMPTY;
12275 cpup->flag = 0;
12276 eqi = per_cpu_ptr(phba->sli4_hba.eq_info, cpu);
12277 INIT_LIST_HEAD(&eqi->list);
12278 eqi->icnt = 0;
12279 }
12280}
12281
12282/**
12283 * lpfc_hba_eq_hdl_array_init - Initialize hba_eq_hdl structure
12284 * @phba: pointer to lpfc hba data structure.
12285 *
12286 * The routine initializes the hba_eq_hdl array structure
12287 */
12288static void
12289lpfc_hba_eq_hdl_array_init(struct lpfc_hba *phba)
12290{
12291 struct lpfc_hba_eq_hdl *eqhdl;
12292 int i;
12293
12294 for (i = 0; i < phba->cfg_irq_chann; i++) {
12295 eqhdl = lpfc_get_eq_hdl(i);
12296 eqhdl->irq = LPFC_VECTOR_MAP_EMPTY;
12297 eqhdl->phba = phba;
12298 }
12299}
12300
James Smart6a828b02019-01-28 11:14:31 -080012301/**
James Smart895427b2017-02-12 13:52:30 -080012302 * lpfc_cpu_affinity_check - Check vector CPU affinity mappings
James Smart7bb03bb2013-04-17 20:19:16 -040012303 * @phba: pointer to lpfc hba data structure.
James Smart895427b2017-02-12 13:52:30 -080012304 * @vectors: number of msix vectors allocated.
James Smart7bb03bb2013-04-17 20:19:16 -040012305 *
James Smart895427b2017-02-12 13:52:30 -080012306 * The routine will figure out the CPU affinity assignment for every
James Smart6a828b02019-01-28 11:14:31 -080012307 * MSI-X vector allocated for the HBA.
James Smart895427b2017-02-12 13:52:30 -080012308 * In addition, the CPU to IO channel mapping will be calculated
12309 * and the phba->sli4_hba.cpu_map array will reflect this.
James Smart7bb03bb2013-04-17 20:19:16 -040012310 */
James Smart895427b2017-02-12 13:52:30 -080012311static void
12312lpfc_cpu_affinity_check(struct lpfc_hba *phba, int vectors)
James Smart7bb03bb2013-04-17 20:19:16 -040012313{
James Smart3ad348d2019-08-14 16:56:43 -070012314 int i, cpu, idx, next_idx, new_cpu, start_cpu, first_cpu;
James Smart6a828b02019-01-28 11:14:31 -080012315 int max_phys_id, min_phys_id;
12316 int max_core_id, min_core_id;
James Smart7bb03bb2013-04-17 20:19:16 -040012317 struct lpfc_vector_map_info *cpup;
James Smartd9954a22019-05-21 17:49:05 -070012318 struct lpfc_vector_map_info *new_cpup;
James Smart7bb03bb2013-04-17 20:19:16 -040012319#ifdef CONFIG_X86
12320 struct cpuinfo_x86 *cpuinfo;
12321#endif
James Smart840eda92020-03-22 11:13:00 -070012322#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
12323 struct lpfc_hdwq_stat *c_stat;
12324#endif
James Smart7bb03bb2013-04-17 20:19:16 -040012325
James Smart6a828b02019-01-28 11:14:31 -080012326 max_phys_id = 0;
James Smartd9954a22019-05-21 17:49:05 -070012327 min_phys_id = LPFC_VECTOR_MAP_EMPTY;
James Smart6a828b02019-01-28 11:14:31 -080012328 max_core_id = 0;
James Smartd9954a22019-05-21 17:49:05 -070012329 min_core_id = LPFC_VECTOR_MAP_EMPTY;
James Smart7bb03bb2013-04-17 20:19:16 -040012330
12331 /* Update CPU map with physical id and core id of each CPU */
James Smart222e9232019-01-28 11:14:35 -080012332 for_each_present_cpu(cpu) {
12333 cpup = &phba->sli4_hba.cpu_map[cpu];
James Smart7bb03bb2013-04-17 20:19:16 -040012334#ifdef CONFIG_X86
12335 cpuinfo = &cpu_data(cpu);
12336 cpup->phys_id = cpuinfo->phys_proc_id;
12337 cpup->core_id = cpuinfo->cpu_core_id;
James Smartd9954a22019-05-21 17:49:05 -070012338 if (lpfc_find_hyper(phba, cpu, cpup->phys_id, cpup->core_id))
12339 cpup->flag |= LPFC_CPU_MAP_HYPER;
James Smart7bb03bb2013-04-17 20:19:16 -040012340#else
12341 /* No distinction between CPUs for other platforms */
12342 cpup->phys_id = 0;
James Smart6a828b02019-01-28 11:14:31 -080012343 cpup->core_id = cpu;
James Smart7bb03bb2013-04-17 20:19:16 -040012344#endif
James Smart7bb03bb2013-04-17 20:19:16 -040012345
James Smartb3295c22019-01-28 11:14:30 -080012346 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart3ad348d2019-08-14 16:56:43 -070012347 "3328 CPU %d physid %d coreid %d flag x%x\n",
12348 cpu, cpup->phys_id, cpup->core_id, cpup->flag);
James Smart6a828b02019-01-28 11:14:31 -080012349
12350 if (cpup->phys_id > max_phys_id)
12351 max_phys_id = cpup->phys_id;
12352 if (cpup->phys_id < min_phys_id)
12353 min_phys_id = cpup->phys_id;
12354
12355 if (cpup->core_id > max_core_id)
12356 max_core_id = cpup->core_id;
12357 if (cpup->core_id < min_core_id)
12358 min_core_id = cpup->core_id;
James Smart7bb03bb2013-04-17 20:19:16 -040012359 }
James Smartb3295c22019-01-28 11:14:30 -080012360
James Smartd9954a22019-05-21 17:49:05 -070012361 /* After looking at each irq vector assigned to this pcidev, its
12362 * possible to see that not ALL CPUs have been accounted for.
James Smart657add42019-05-21 17:49:06 -070012363 * Next we will set any unassigned (unaffinitized) cpu map
12364 * entries to a IRQ on the same phys_id.
James Smartd9954a22019-05-21 17:49:05 -070012365 */
12366 first_cpu = cpumask_first(cpu_present_mask);
12367 start_cpu = first_cpu;
12368
12369 for_each_present_cpu(cpu) {
12370 cpup = &phba->sli4_hba.cpu_map[cpu];
12371
12372 /* Is this CPU entry unassigned */
12373 if (cpup->eq == LPFC_VECTOR_MAP_EMPTY) {
12374 /* Mark CPU as IRQ not assigned by the kernel */
12375 cpup->flag |= LPFC_CPU_MAP_UNASSIGN;
12376
James Smart657add42019-05-21 17:49:06 -070012377 /* If so, find a new_cpup thats on the the SAME
James Smartd9954a22019-05-21 17:49:05 -070012378 * phys_id as cpup. start_cpu will start where we
12379 * left off so all unassigned entries don't get assgined
12380 * the IRQ of the first entry.
12381 */
12382 new_cpu = start_cpu;
12383 for (i = 0; i < phba->sli4_hba.num_present_cpu; i++) {
12384 new_cpup = &phba->sli4_hba.cpu_map[new_cpu];
12385 if (!(new_cpup->flag & LPFC_CPU_MAP_UNASSIGN) &&
James Smartdcaa2132019-11-04 16:57:06 -080012386 (new_cpup->eq != LPFC_VECTOR_MAP_EMPTY) &&
James Smartd9954a22019-05-21 17:49:05 -070012387 (new_cpup->phys_id == cpup->phys_id))
12388 goto found_same;
12389 new_cpu = cpumask_next(
12390 new_cpu, cpu_present_mask);
12391 if (new_cpu == nr_cpumask_bits)
12392 new_cpu = first_cpu;
12393 }
12394 /* At this point, we leave the CPU as unassigned */
12395 continue;
12396found_same:
12397 /* We found a matching phys_id, so copy the IRQ info */
12398 cpup->eq = new_cpup->eq;
James Smartd9954a22019-05-21 17:49:05 -070012399
12400 /* Bump start_cpu to the next slot to minmize the
12401 * chance of having multiple unassigned CPU entries
12402 * selecting the same IRQ.
12403 */
12404 start_cpu = cpumask_next(new_cpu, cpu_present_mask);
12405 if (start_cpu == nr_cpumask_bits)
12406 start_cpu = first_cpu;
12407
James Smart657add42019-05-21 17:49:06 -070012408 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smartd9954a22019-05-21 17:49:05 -070012409 "3337 Set Affinity: CPU %d "
James Smartdcaa2132019-11-04 16:57:06 -080012410 "eq %d from peer cpu %d same "
James Smartd9954a22019-05-21 17:49:05 -070012411 "phys_id (%d)\n",
James Smartdcaa2132019-11-04 16:57:06 -080012412 cpu, cpup->eq, new_cpu,
12413 cpup->phys_id);
James Smartd9954a22019-05-21 17:49:05 -070012414 }
12415 }
12416
12417 /* Set any unassigned cpu map entries to a IRQ on any phys_id */
12418 start_cpu = first_cpu;
12419
12420 for_each_present_cpu(cpu) {
12421 cpup = &phba->sli4_hba.cpu_map[cpu];
12422
12423 /* Is this entry unassigned */
12424 if (cpup->eq == LPFC_VECTOR_MAP_EMPTY) {
12425 /* Mark it as IRQ not assigned by the kernel */
12426 cpup->flag |= LPFC_CPU_MAP_UNASSIGN;
12427
James Smart657add42019-05-21 17:49:06 -070012428 /* If so, find a new_cpup thats on ANY phys_id
James Smartd9954a22019-05-21 17:49:05 -070012429 * as the cpup. start_cpu will start where we
12430 * left off so all unassigned entries don't get
12431 * assigned the IRQ of the first entry.
12432 */
12433 new_cpu = start_cpu;
12434 for (i = 0; i < phba->sli4_hba.num_present_cpu; i++) {
12435 new_cpup = &phba->sli4_hba.cpu_map[new_cpu];
12436 if (!(new_cpup->flag & LPFC_CPU_MAP_UNASSIGN) &&
James Smartdcaa2132019-11-04 16:57:06 -080012437 (new_cpup->eq != LPFC_VECTOR_MAP_EMPTY))
James Smartd9954a22019-05-21 17:49:05 -070012438 goto found_any;
12439 new_cpu = cpumask_next(
12440 new_cpu, cpu_present_mask);
12441 if (new_cpu == nr_cpumask_bits)
12442 new_cpu = first_cpu;
12443 }
12444 /* We should never leave an entry unassigned */
12445 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12446 "3339 Set Affinity: CPU %d "
James Smartdcaa2132019-11-04 16:57:06 -080012447 "eq %d UNASSIGNED\n",
12448 cpup->hdwq, cpup->eq);
James Smartd9954a22019-05-21 17:49:05 -070012449 continue;
12450found_any:
12451 /* We found an available entry, copy the IRQ info */
12452 cpup->eq = new_cpup->eq;
James Smartd9954a22019-05-21 17:49:05 -070012453
12454 /* Bump start_cpu to the next slot to minmize the
12455 * chance of having multiple unassigned CPU entries
12456 * selecting the same IRQ.
12457 */
12458 start_cpu = cpumask_next(new_cpu, cpu_present_mask);
12459 if (start_cpu == nr_cpumask_bits)
12460 start_cpu = first_cpu;
12461
James Smart657add42019-05-21 17:49:06 -070012462 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smartd9954a22019-05-21 17:49:05 -070012463 "3338 Set Affinity: CPU %d "
James Smartdcaa2132019-11-04 16:57:06 -080012464 "eq %d from peer cpu %d (%d/%d)\n",
12465 cpu, cpup->eq, new_cpu,
James Smartd9954a22019-05-21 17:49:05 -070012466 new_cpup->phys_id, new_cpup->core_id);
12467 }
12468 }
James Smart657add42019-05-21 17:49:06 -070012469
James Smart3ad348d2019-08-14 16:56:43 -070012470 /* Assign hdwq indices that are unique across all cpus in the map
12471 * that are also FIRST_CPUs.
12472 */
12473 idx = 0;
12474 for_each_present_cpu(cpu) {
12475 cpup = &phba->sli4_hba.cpu_map[cpu];
12476
12477 /* Only FIRST IRQs get a hdwq index assignment. */
12478 if (!(cpup->flag & LPFC_CPU_FIRST_IRQ))
12479 continue;
12480
12481 /* 1 to 1, the first LPFC_CPU_FIRST_IRQ cpus to a unique hdwq */
12482 cpup->hdwq = idx;
12483 idx++;
Anton Blanchardbc2736e2020-07-13 18:39:08 +100012484 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart3ad348d2019-08-14 16:56:43 -070012485 "3333 Set Affinity: CPU %d (phys %d core %d): "
James Smartdcaa2132019-11-04 16:57:06 -080012486 "hdwq %d eq %d flg x%x\n",
James Smart3ad348d2019-08-14 16:56:43 -070012487 cpu, cpup->phys_id, cpup->core_id,
James Smartdcaa2132019-11-04 16:57:06 -080012488 cpup->hdwq, cpup->eq, cpup->flag);
James Smart3ad348d2019-08-14 16:56:43 -070012489 }
James Smartbc227dd2019-11-11 15:03:59 -080012490 /* Associate a hdwq with each cpu_map entry
James Smart657add42019-05-21 17:49:06 -070012491 * This will be 1 to 1 - hdwq to cpu, unless there are less
12492 * hardware queues then CPUs. For that case we will just round-robin
12493 * the available hardware queues as they get assigned to CPUs.
James Smart3ad348d2019-08-14 16:56:43 -070012494 * The next_idx is the idx from the FIRST_CPU loop above to account
12495 * for irq_chann < hdwq. The idx is used for round-robin assignments
12496 * and needs to start at 0.
James Smart657add42019-05-21 17:49:06 -070012497 */
James Smart3ad348d2019-08-14 16:56:43 -070012498 next_idx = idx;
James Smart657add42019-05-21 17:49:06 -070012499 start_cpu = 0;
James Smart3ad348d2019-08-14 16:56:43 -070012500 idx = 0;
James Smart657add42019-05-21 17:49:06 -070012501 for_each_present_cpu(cpu) {
12502 cpup = &phba->sli4_hba.cpu_map[cpu];
James Smart657add42019-05-21 17:49:06 -070012503
James Smart3ad348d2019-08-14 16:56:43 -070012504 /* FIRST cpus are already mapped. */
12505 if (cpup->flag & LPFC_CPU_FIRST_IRQ)
12506 continue;
James Smart657add42019-05-21 17:49:06 -070012507
James Smart3ad348d2019-08-14 16:56:43 -070012508 /* If the cfg_irq_chann < cfg_hdw_queue, set the hdwq
12509 * of the unassigned cpus to the next idx so that all
12510 * hdw queues are fully utilized.
12511 */
12512 if (next_idx < phba->cfg_hdw_queue) {
12513 cpup->hdwq = next_idx;
12514 next_idx++;
12515 continue;
James Smart657add42019-05-21 17:49:06 -070012516 }
James Smart3ad348d2019-08-14 16:56:43 -070012517
12518 /* Not a First CPU and all hdw_queues are used. Reuse a
12519 * Hardware Queue for another CPU, so be smart about it
12520 * and pick one that has its IRQ/EQ mapped to the same phys_id
12521 * (CPU package) and core_id.
12522 */
12523 new_cpu = start_cpu;
12524 for (i = 0; i < phba->sli4_hba.num_present_cpu; i++) {
12525 new_cpup = &phba->sli4_hba.cpu_map[new_cpu];
12526 if (new_cpup->hdwq != LPFC_VECTOR_MAP_EMPTY &&
12527 new_cpup->phys_id == cpup->phys_id &&
12528 new_cpup->core_id == cpup->core_id) {
12529 goto found_hdwq;
12530 }
12531 new_cpu = cpumask_next(new_cpu, cpu_present_mask);
12532 if (new_cpu == nr_cpumask_bits)
12533 new_cpu = first_cpu;
12534 }
12535
12536 /* If we can't match both phys_id and core_id,
12537 * settle for just a phys_id match.
12538 */
12539 new_cpu = start_cpu;
12540 for (i = 0; i < phba->sli4_hba.num_present_cpu; i++) {
12541 new_cpup = &phba->sli4_hba.cpu_map[new_cpu];
12542 if (new_cpup->hdwq != LPFC_VECTOR_MAP_EMPTY &&
12543 new_cpup->phys_id == cpup->phys_id)
12544 goto found_hdwq;
12545
12546 new_cpu = cpumask_next(new_cpu, cpu_present_mask);
12547 if (new_cpu == nr_cpumask_bits)
12548 new_cpu = first_cpu;
12549 }
12550
12551 /* Otherwise just round robin on cfg_hdw_queue */
12552 cpup->hdwq = idx % phba->cfg_hdw_queue;
12553 idx++;
12554 goto logit;
12555 found_hdwq:
12556 /* We found an available entry, copy the IRQ info */
12557 start_cpu = cpumask_next(new_cpu, cpu_present_mask);
12558 if (start_cpu == nr_cpumask_bits)
12559 start_cpu = first_cpu;
12560 cpup->hdwq = new_cpup->hdwq;
12561 logit:
Anton Blanchardbc2736e2020-07-13 18:39:08 +100012562 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart657add42019-05-21 17:49:06 -070012563 "3335 Set Affinity: CPU %d (phys %d core %d): "
James Smartdcaa2132019-11-04 16:57:06 -080012564 "hdwq %d eq %d flg x%x\n",
James Smart657add42019-05-21 17:49:06 -070012565 cpu, cpup->phys_id, cpup->core_id,
James Smartdcaa2132019-11-04 16:57:06 -080012566 cpup->hdwq, cpup->eq, cpup->flag);
James Smart657add42019-05-21 17:49:06 -070012567 }
12568
James Smartbc227dd2019-11-11 15:03:59 -080012569 /*
12570 * Initialize the cpu_map slots for not-present cpus in case
12571 * a cpu is hot-added. Perform a simple hdwq round robin assignment.
12572 */
12573 idx = 0;
12574 for_each_possible_cpu(cpu) {
12575 cpup = &phba->sli4_hba.cpu_map[cpu];
James Smart840eda92020-03-22 11:13:00 -070012576#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
12577 c_stat = per_cpu_ptr(phba->sli4_hba.c_stat, cpu);
12578 c_stat->hdwq_no = cpup->hdwq;
12579#endif
James Smartbc227dd2019-11-11 15:03:59 -080012580 if (cpup->hdwq != LPFC_VECTOR_MAP_EMPTY)
12581 continue;
12582
12583 cpup->hdwq = idx++ % phba->cfg_hdw_queue;
James Smart840eda92020-03-22 11:13:00 -070012584#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
12585 c_stat->hdwq_no = cpup->hdwq;
12586#endif
James Smartbc227dd2019-11-11 15:03:59 -080012587 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
12588 "3340 Set Affinity: not present "
12589 "CPU %d hdwq %d\n",
12590 cpu, cpup->hdwq);
James Smart657add42019-05-21 17:49:06 -070012591 }
12592
12593 /* The cpu_map array will be used later during initialization
12594 * when EQ / CQ / WQs are allocated and configured.
12595 */
James Smartb3295c22019-01-28 11:14:30 -080012596 return;
James Smart7bb03bb2013-04-17 20:19:16 -040012597}
James Smart7bb03bb2013-04-17 20:19:16 -040012598
12599/**
James Smart93a4d6f2019-11-04 16:57:05 -080012600 * lpfc_cpuhp_get_eq
12601 *
12602 * @phba: pointer to lpfc hba data structure.
12603 * @cpu: cpu going offline
Lee Jonesfe614ac2020-07-23 13:24:22 +010012604 * @eqlist: eq list to append to
James Smart93a4d6f2019-11-04 16:57:05 -080012605 */
James Smarta99c8072020-01-27 16:23:06 -080012606static int
James Smart93a4d6f2019-11-04 16:57:05 -080012607lpfc_cpuhp_get_eq(struct lpfc_hba *phba, unsigned int cpu,
12608 struct list_head *eqlist)
12609{
James Smart93a4d6f2019-11-04 16:57:05 -080012610 const struct cpumask *maskp;
12611 struct lpfc_queue *eq;
James Smarta99c8072020-01-27 16:23:06 -080012612 struct cpumask *tmp;
James Smart93a4d6f2019-11-04 16:57:05 -080012613 u16 idx;
12614
James Smarta99c8072020-01-27 16:23:06 -080012615 tmp = kzalloc(cpumask_size(), GFP_KERNEL);
12616 if (!tmp)
12617 return -ENOMEM;
12618
James Smart93a4d6f2019-11-04 16:57:05 -080012619 for (idx = 0; idx < phba->cfg_irq_chann; idx++) {
12620 maskp = pci_irq_get_affinity(phba->pcidev, idx);
12621 if (!maskp)
12622 continue;
12623 /*
12624 * if irq is not affinitized to the cpu going
12625 * then we don't need to poll the eq attached
12626 * to it.
12627 */
James Smarta99c8072020-01-27 16:23:06 -080012628 if (!cpumask_and(tmp, maskp, cpumask_of(cpu)))
James Smart93a4d6f2019-11-04 16:57:05 -080012629 continue;
12630 /* get the cpus that are online and are affini-
12631 * tized to this irq vector. If the count is
12632 * more than 1 then cpuhp is not going to shut-
12633 * down this vector. Since this cpu has not
12634 * gone offline yet, we need >1.
12635 */
James Smarta99c8072020-01-27 16:23:06 -080012636 cpumask_and(tmp, maskp, cpu_online_mask);
12637 if (cpumask_weight(tmp) > 1)
James Smart93a4d6f2019-11-04 16:57:05 -080012638 continue;
12639
12640 /* Now that we have an irq to shutdown, get the eq
12641 * mapped to this irq. Note: multiple hdwq's in
12642 * the software can share an eq, but eventually
12643 * only eq will be mapped to this vector
12644 */
James Smartdcaa2132019-11-04 16:57:06 -080012645 eq = phba->sli4_hba.hba_eq_hdl[idx].eq;
12646 list_add(&eq->_poll_list, eqlist);
James Smart93a4d6f2019-11-04 16:57:05 -080012647 }
James Smarta99c8072020-01-27 16:23:06 -080012648 kfree(tmp);
12649 return 0;
James Smart93a4d6f2019-11-04 16:57:05 -080012650}
12651
12652static void __lpfc_cpuhp_remove(struct lpfc_hba *phba)
12653{
12654 if (phba->sli_rev != LPFC_SLI_REV4)
12655 return;
12656
12657 cpuhp_state_remove_instance_nocalls(lpfc_cpuhp_state,
12658 &phba->cpuhp);
12659 /*
12660 * unregistering the instance doesn't stop the polling
12661 * timer. Wait for the poll timer to retire.
12662 */
12663 synchronize_rcu();
12664 del_timer_sync(&phba->cpuhp_poll_timer);
12665}
12666
12667static void lpfc_cpuhp_remove(struct lpfc_hba *phba)
12668{
12669 if (phba->pport->fc_flag & FC_OFFLINE_MODE)
12670 return;
12671
12672 __lpfc_cpuhp_remove(phba);
12673}
12674
12675static void lpfc_cpuhp_add(struct lpfc_hba *phba)
12676{
12677 if (phba->sli_rev != LPFC_SLI_REV4)
12678 return;
12679
12680 rcu_read_lock();
12681
James Smartf861f592020-03-22 11:12:54 -070012682 if (!list_empty(&phba->poll_list))
James Smart93a4d6f2019-11-04 16:57:05 -080012683 mod_timer(&phba->cpuhp_poll_timer,
12684 jiffies + msecs_to_jiffies(LPFC_POLL_HB));
James Smart93a4d6f2019-11-04 16:57:05 -080012685
12686 rcu_read_unlock();
12687
12688 cpuhp_state_add_instance_nocalls(lpfc_cpuhp_state,
12689 &phba->cpuhp);
12690}
12691
12692static int __lpfc_cpuhp_checks(struct lpfc_hba *phba, int *retval)
12693{
12694 if (phba->pport->load_flag & FC_UNLOADING) {
12695 *retval = -EAGAIN;
12696 return true;
12697 }
12698
12699 if (phba->sli_rev != LPFC_SLI_REV4) {
12700 *retval = 0;
12701 return true;
12702 }
12703
12704 /* proceed with the hotplug */
12705 return false;
12706}
12707
James Smartdcaa2132019-11-04 16:57:06 -080012708/**
12709 * lpfc_irq_set_aff - set IRQ affinity
12710 * @eqhdl: EQ handle
12711 * @cpu: cpu to set affinity
12712 *
12713 **/
12714static inline void
12715lpfc_irq_set_aff(struct lpfc_hba_eq_hdl *eqhdl, unsigned int cpu)
12716{
12717 cpumask_clear(&eqhdl->aff_mask);
12718 cpumask_set_cpu(cpu, &eqhdl->aff_mask);
12719 irq_set_status_flags(eqhdl->irq, IRQ_NO_BALANCING);
12720 irq_set_affinity_hint(eqhdl->irq, &eqhdl->aff_mask);
12721}
12722
12723/**
12724 * lpfc_irq_clear_aff - clear IRQ affinity
12725 * @eqhdl: EQ handle
12726 *
12727 **/
12728static inline void
12729lpfc_irq_clear_aff(struct lpfc_hba_eq_hdl *eqhdl)
12730{
12731 cpumask_clear(&eqhdl->aff_mask);
12732 irq_clear_status_flags(eqhdl->irq, IRQ_NO_BALANCING);
James Smartdcaa2132019-11-04 16:57:06 -080012733}
12734
12735/**
12736 * lpfc_irq_rebalance - rebalances IRQ affinity according to cpuhp event
12737 * @phba: pointer to HBA context object.
12738 * @cpu: cpu going offline/online
12739 * @offline: true, cpu is going offline. false, cpu is coming online.
12740 *
12741 * If cpu is going offline, we'll try our best effort to find the next
Dick Kennedy3048e3e2020-05-01 14:43:06 -070012742 * online cpu on the phba's original_mask and migrate all offlining IRQ
12743 * affinities.
James Smartdcaa2132019-11-04 16:57:06 -080012744 *
Dick Kennedy3048e3e2020-05-01 14:43:06 -070012745 * If cpu is coming online, reaffinitize the IRQ back to the onlining cpu.
James Smartdcaa2132019-11-04 16:57:06 -080012746 *
Dick Kennedy3048e3e2020-05-01 14:43:06 -070012747 * Note: Call only if NUMA or NHT mode is enabled, otherwise rely on
James Smartdcaa2132019-11-04 16:57:06 -080012748 * PCI_IRQ_AFFINITY to auto-manage IRQ affinity.
12749 *
12750 **/
12751static void
12752lpfc_irq_rebalance(struct lpfc_hba *phba, unsigned int cpu, bool offline)
12753{
12754 struct lpfc_vector_map_info *cpup;
12755 struct cpumask *aff_mask;
12756 unsigned int cpu_select, cpu_next, idx;
Dick Kennedy3048e3e2020-05-01 14:43:06 -070012757 const struct cpumask *orig_mask;
James Smartdcaa2132019-11-04 16:57:06 -080012758
Dick Kennedy3048e3e2020-05-01 14:43:06 -070012759 if (phba->irq_chann_mode == NORMAL_MODE)
James Smartdcaa2132019-11-04 16:57:06 -080012760 return;
12761
Dick Kennedy3048e3e2020-05-01 14:43:06 -070012762 orig_mask = &phba->sli4_hba.irq_aff_mask;
James Smartdcaa2132019-11-04 16:57:06 -080012763
Dick Kennedy3048e3e2020-05-01 14:43:06 -070012764 if (!cpumask_test_cpu(cpu, orig_mask))
James Smartdcaa2132019-11-04 16:57:06 -080012765 return;
12766
12767 cpup = &phba->sli4_hba.cpu_map[cpu];
12768
12769 if (!(cpup->flag & LPFC_CPU_FIRST_IRQ))
12770 return;
12771
12772 if (offline) {
Dick Kennedy3048e3e2020-05-01 14:43:06 -070012773 /* Find next online CPU on original mask */
12774 cpu_next = cpumask_next_wrap(cpu, orig_mask, cpu, true);
12775 cpu_select = lpfc_next_online_cpu(orig_mask, cpu_next);
James Smartdcaa2132019-11-04 16:57:06 -080012776
12777 /* Found a valid CPU */
12778 if ((cpu_select < nr_cpu_ids) && (cpu_select != cpu)) {
12779 /* Go through each eqhdl and ensure offlining
12780 * cpu aff_mask is migrated
12781 */
12782 for (idx = 0; idx < phba->cfg_irq_chann; idx++) {
12783 aff_mask = lpfc_get_aff_mask(idx);
12784
12785 /* Migrate affinity */
12786 if (cpumask_test_cpu(cpu, aff_mask))
12787 lpfc_irq_set_aff(lpfc_get_eq_hdl(idx),
12788 cpu_select);
12789 }
12790 } else {
12791 /* Rely on irqbalance if no online CPUs left on NUMA */
12792 for (idx = 0; idx < phba->cfg_irq_chann; idx++)
12793 lpfc_irq_clear_aff(lpfc_get_eq_hdl(idx));
12794 }
12795 } else {
12796 /* Migrate affinity back to this CPU */
12797 lpfc_irq_set_aff(lpfc_get_eq_hdl(cpup->eq), cpu);
12798 }
12799}
12800
James Smart93a4d6f2019-11-04 16:57:05 -080012801static int lpfc_cpu_offline(unsigned int cpu, struct hlist_node *node)
12802{
12803 struct lpfc_hba *phba = hlist_entry_safe(node, struct lpfc_hba, cpuhp);
12804 struct lpfc_queue *eq, *next;
12805 LIST_HEAD(eqlist);
12806 int retval;
12807
12808 if (!phba) {
12809 WARN_ONCE(!phba, "cpu: %u. phba:NULL", raw_smp_processor_id());
12810 return 0;
12811 }
12812
12813 if (__lpfc_cpuhp_checks(phba, &retval))
12814 return retval;
12815
James Smartdcaa2132019-11-04 16:57:06 -080012816 lpfc_irq_rebalance(phba, cpu, true);
12817
James Smarta99c8072020-01-27 16:23:06 -080012818 retval = lpfc_cpuhp_get_eq(phba, cpu, &eqlist);
12819 if (retval)
12820 return retval;
James Smart93a4d6f2019-11-04 16:57:05 -080012821
12822 /* start polling on these eq's */
12823 list_for_each_entry_safe(eq, next, &eqlist, _poll_list) {
12824 list_del_init(&eq->_poll_list);
12825 lpfc_sli4_start_polling(eq);
12826 }
12827
12828 return 0;
12829}
12830
12831static int lpfc_cpu_online(unsigned int cpu, struct hlist_node *node)
12832{
12833 struct lpfc_hba *phba = hlist_entry_safe(node, struct lpfc_hba, cpuhp);
12834 struct lpfc_queue *eq, *next;
12835 unsigned int n;
12836 int retval;
12837
12838 if (!phba) {
12839 WARN_ONCE(!phba, "cpu: %u. phba:NULL", raw_smp_processor_id());
12840 return 0;
12841 }
12842
12843 if (__lpfc_cpuhp_checks(phba, &retval))
12844 return retval;
12845
James Smartdcaa2132019-11-04 16:57:06 -080012846 lpfc_irq_rebalance(phba, cpu, false);
12847
James Smart93a4d6f2019-11-04 16:57:05 -080012848 list_for_each_entry_safe(eq, next, &phba->poll_list, _poll_list) {
12849 n = lpfc_find_cpu_handle(phba, eq->hdwq, LPFC_FIND_BY_HDWQ);
12850 if (n == cpu)
12851 lpfc_sli4_stop_polling(eq);
12852 }
12853
12854 return 0;
12855}
12856
12857/**
James Smartda0436e2009-05-22 14:51:39 -040012858 * lpfc_sli4_enable_msix - Enable MSI-X interrupt mode to SLI-4 device
12859 * @phba: pointer to lpfc hba data structure.
12860 *
12861 * This routine is invoked to enable the MSI-X interrupt vectors to device
James Smartdcaa2132019-11-04 16:57:06 -080012862 * with SLI-4 interface spec. It also allocates MSI-X vectors and maps them
12863 * to cpus on the system.
12864 *
12865 * When cfg_irq_numa is enabled, the adapter will only allocate vectors for
12866 * the number of cpus on the same numa node as this adapter. The vectors are
12867 * allocated without requesting OS affinity mapping. A vector will be
12868 * allocated and assigned to each online and offline cpu. If the cpu is
12869 * online, then affinity will be set to that cpu. If the cpu is offline, then
12870 * affinity will be set to the nearest peer cpu within the numa node that is
12871 * online. If there are no online cpus within the numa node, affinity is not
12872 * assigned and the OS may do as it pleases. Note: cpu vector affinity mapping
12873 * is consistent with the way cpu online/offline is handled when cfg_irq_numa is
12874 * configured.
12875 *
12876 * If numa mode is not enabled and there is more than 1 vector allocated, then
12877 * the driver relies on the managed irq interface where the OS assigns vector to
12878 * cpu affinity. The driver will then use that affinity mapping to setup its
12879 * cpu mapping table.
James Smartda0436e2009-05-22 14:51:39 -040012880 *
12881 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020012882 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -040012883 * other values - error
12884 **/
12885static int
12886lpfc_sli4_enable_msix(struct lpfc_hba *phba)
12887{
James Smart75baf692010-06-08 18:31:21 -040012888 int vectors, rc, index;
James Smartb83d0052017-06-01 21:07:05 -070012889 char *name;
Dick Kennedy3048e3e2020-05-01 14:43:06 -070012890 const struct cpumask *aff_mask = NULL;
James Smartdcaa2132019-11-04 16:57:06 -080012891 unsigned int cpu = 0, cpu_cnt = 0, cpu_select = nr_cpu_ids;
Dick Kennedy17105d92020-07-06 13:42:30 -070012892 struct lpfc_vector_map_info *cpup;
James Smartdcaa2132019-11-04 16:57:06 -080012893 struct lpfc_hba_eq_hdl *eqhdl;
12894 const struct cpumask *maskp;
James Smartdcaa2132019-11-04 16:57:06 -080012895 unsigned int flags = PCI_IRQ_MSIX;
James Smartda0436e2009-05-22 14:51:39 -040012896
12897 /* Set up MSI-X multi-message vectors */
James Smart6a828b02019-01-28 11:14:31 -080012898 vectors = phba->cfg_irq_chann;
Christoph Hellwig45ffac12017-02-12 13:52:26 -080012899
Dick Kennedy3048e3e2020-05-01 14:43:06 -070012900 if (phba->irq_chann_mode != NORMAL_MODE)
12901 aff_mask = &phba->sli4_hba.irq_aff_mask;
12902
12903 if (aff_mask) {
12904 cpu_cnt = cpumask_weight(aff_mask);
James Smartdcaa2132019-11-04 16:57:06 -080012905 vectors = min(phba->cfg_irq_chann, cpu_cnt);
12906
Dick Kennedy3048e3e2020-05-01 14:43:06 -070012907 /* cpu: iterates over aff_mask including offline or online
12908 * cpu_select: iterates over online aff_mask to set affinity
James Smartdcaa2132019-11-04 16:57:06 -080012909 */
Dick Kennedy3048e3e2020-05-01 14:43:06 -070012910 cpu = cpumask_first(aff_mask);
12911 cpu_select = lpfc_next_online_cpu(aff_mask, cpu);
James Smartdcaa2132019-11-04 16:57:06 -080012912 } else {
12913 flags |= PCI_IRQ_AFFINITY;
12914 }
12915
12916 rc = pci_alloc_irq_vectors(phba->pcidev, 1, vectors, flags);
Alexander Gordeev4f871e12014-09-03 12:56:29 -040012917 if (rc < 0) {
James Smartda0436e2009-05-22 14:51:39 -040012918 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
12919 "0484 PCI enable MSI-X failed (%d)\n", rc);
Alexander Gordeev029165a2014-07-16 20:05:15 +020012920 goto vec_fail_out;
James Smartda0436e2009-05-22 14:51:39 -040012921 }
Alexander Gordeev4f871e12014-09-03 12:56:29 -040012922 vectors = rc;
James Smart75baf692010-06-08 18:31:21 -040012923
James Smart7bb03bb2013-04-17 20:19:16 -040012924 /* Assign MSI-X vectors to interrupt handlers */
James Smart67d12732012-08-03 12:36:13 -040012925 for (index = 0; index < vectors; index++) {
James Smartdcaa2132019-11-04 16:57:06 -080012926 eqhdl = lpfc_get_eq_hdl(index);
12927 name = eqhdl->handler_name;
James Smartb83d0052017-06-01 21:07:05 -070012928 memset(name, 0, LPFC_SLI4_HANDLER_NAME_SZ);
12929 snprintf(name, LPFC_SLI4_HANDLER_NAME_SZ,
James Smart4305f182012-08-03 12:36:33 -040012930 LPFC_DRIVER_HANDLER_NAME"%d", index);
James Smartda0436e2009-05-22 14:51:39 -040012931
James Smartdcaa2132019-11-04 16:57:06 -080012932 eqhdl->idx = index;
James Smart7370d102019-01-28 11:14:20 -080012933 rc = request_irq(pci_irq_vector(phba->pcidev, index),
12934 &lpfc_sli4_hba_intr_handler, 0,
James Smartdcaa2132019-11-04 16:57:06 -080012935 name, eqhdl);
James Smartda0436e2009-05-22 14:51:39 -040012936 if (rc) {
12937 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
12938 "0486 MSI-X fast-path (%d) "
12939 "request_irq failed (%d)\n", index, rc);
12940 goto cfg_fail_out;
12941 }
James Smartdcaa2132019-11-04 16:57:06 -080012942
12943 eqhdl->irq = pci_irq_vector(phba->pcidev, index);
12944
Dick Kennedy3048e3e2020-05-01 14:43:06 -070012945 if (aff_mask) {
James Smartdcaa2132019-11-04 16:57:06 -080012946 /* If found a neighboring online cpu, set affinity */
12947 if (cpu_select < nr_cpu_ids)
12948 lpfc_irq_set_aff(eqhdl, cpu_select);
12949
12950 /* Assign EQ to cpu_map */
12951 lpfc_assign_eq_map_info(phba, index,
12952 LPFC_CPU_FIRST_IRQ,
12953 cpu);
12954
Dick Kennedy3048e3e2020-05-01 14:43:06 -070012955 /* Iterate to next offline or online cpu in aff_mask */
12956 cpu = cpumask_next(cpu, aff_mask);
James Smartdcaa2132019-11-04 16:57:06 -080012957
Dick Kennedy3048e3e2020-05-01 14:43:06 -070012958 /* Find next online cpu in aff_mask to set affinity */
12959 cpu_select = lpfc_next_online_cpu(aff_mask, cpu);
James Smartdcaa2132019-11-04 16:57:06 -080012960 } else if (vectors == 1) {
12961 cpu = cpumask_first(cpu_present_mask);
12962 lpfc_assign_eq_map_info(phba, index, LPFC_CPU_FIRST_IRQ,
12963 cpu);
12964 } else {
12965 maskp = pci_irq_get_affinity(phba->pcidev, index);
12966
James Smartdcaa2132019-11-04 16:57:06 -080012967 /* Loop through all CPUs associated with vector index */
12968 for_each_cpu_and(cpu, maskp, cpu_present_mask) {
Dick Kennedy17105d92020-07-06 13:42:30 -070012969 cpup = &phba->sli4_hba.cpu_map[cpu];
12970
James Smartdcaa2132019-11-04 16:57:06 -080012971 /* If this is the first CPU thats assigned to
12972 * this vector, set LPFC_CPU_FIRST_IRQ.
Dick Kennedy17105d92020-07-06 13:42:30 -070012973 *
12974 * With certain platforms its possible that irq
12975 * vectors are affinitized to all the cpu's.
12976 * This can result in each cpu_map.eq to be set
12977 * to the last vector, resulting in overwrite
12978 * of all the previous cpu_map.eq. Ensure that
12979 * each vector receives a place in cpu_map.
12980 * Later call to lpfc_cpu_affinity_check will
12981 * ensure we are nicely balanced out.
James Smartdcaa2132019-11-04 16:57:06 -080012982 */
Dick Kennedy17105d92020-07-06 13:42:30 -070012983 if (cpup->eq != LPFC_VECTOR_MAP_EMPTY)
12984 continue;
James Smartdcaa2132019-11-04 16:57:06 -080012985 lpfc_assign_eq_map_info(phba, index,
Dick Kennedy17105d92020-07-06 13:42:30 -070012986 LPFC_CPU_FIRST_IRQ,
James Smartdcaa2132019-11-04 16:57:06 -080012987 cpu);
Dick Kennedy17105d92020-07-06 13:42:30 -070012988 break;
James Smartdcaa2132019-11-04 16:57:06 -080012989 }
12990 }
James Smartda0436e2009-05-22 14:51:39 -040012991 }
12992
James Smart6a828b02019-01-28 11:14:31 -080012993 if (vectors != phba->cfg_irq_chann) {
Dick Kennedy372c1872020-06-30 14:50:00 -070012994 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart82c3e9b2012-09-29 11:29:50 -040012995 "3238 Reducing IO channels to match number of "
12996 "MSI-X vectors, requested %d got %d\n",
James Smart6a828b02019-01-28 11:14:31 -080012997 phba->cfg_irq_chann, vectors);
12998 if (phba->cfg_irq_chann > vectors)
12999 phba->cfg_irq_chann = vectors;
James Smart82c3e9b2012-09-29 11:29:50 -040013000 }
James Smart7bb03bb2013-04-17 20:19:16 -040013001
James Smartda0436e2009-05-22 14:51:39 -040013002 return rc;
13003
13004cfg_fail_out:
13005 /* free the irq already requested */
James Smartdcaa2132019-11-04 16:57:06 -080013006 for (--index; index >= 0; index--) {
13007 eqhdl = lpfc_get_eq_hdl(index);
13008 lpfc_irq_clear_aff(eqhdl);
13009 irq_set_affinity_hint(eqhdl->irq, NULL);
13010 free_irq(eqhdl->irq, eqhdl);
13011 }
James Smartda0436e2009-05-22 14:51:39 -040013012
James Smartda0436e2009-05-22 14:51:39 -040013013 /* Unconfigure MSI-X capability structure */
Christoph Hellwig45ffac12017-02-12 13:52:26 -080013014 pci_free_irq_vectors(phba->pcidev);
Alexander Gordeev029165a2014-07-16 20:05:15 +020013015
13016vec_fail_out:
James Smartda0436e2009-05-22 14:51:39 -040013017 return rc;
13018}
13019
13020/**
James Smartda0436e2009-05-22 14:51:39 -040013021 * lpfc_sli4_enable_msi - Enable MSI interrupt mode to SLI-4 device
13022 * @phba: pointer to lpfc hba data structure.
13023 *
13024 * This routine is invoked to enable the MSI interrupt mode to device with
James Smart07b1b912019-08-14 16:56:58 -070013025 * SLI-4 interface spec. The kernel function pci_alloc_irq_vectors() is
13026 * called to enable the MSI vector. The device driver is responsible for
13027 * calling the request_irq() to register MSI vector with a interrupt the
13028 * handler, which is done in this function.
James Smartda0436e2009-05-22 14:51:39 -040013029 *
13030 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020013031 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -040013032 * other values - error
13033 **/
13034static int
13035lpfc_sli4_enable_msi(struct lpfc_hba *phba)
13036{
13037 int rc, index;
James Smartdcaa2132019-11-04 16:57:06 -080013038 unsigned int cpu;
13039 struct lpfc_hba_eq_hdl *eqhdl;
James Smartda0436e2009-05-22 14:51:39 -040013040
James Smart07b1b912019-08-14 16:56:58 -070013041 rc = pci_alloc_irq_vectors(phba->pcidev, 1, 1,
13042 PCI_IRQ_MSI | PCI_IRQ_AFFINITY);
13043 if (rc > 0)
James Smartda0436e2009-05-22 14:51:39 -040013044 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
13045 "0487 PCI enable MSI mode success.\n");
13046 else {
13047 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
13048 "0488 PCI enable MSI mode failed (%d)\n", rc);
James Smart07b1b912019-08-14 16:56:58 -070013049 return rc ? rc : -1;
James Smartda0436e2009-05-22 14:51:39 -040013050 }
13051
13052 rc = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
James Smarted243d32015-05-21 13:55:25 -040013053 0, LPFC_DRIVER_NAME, phba);
James Smartda0436e2009-05-22 14:51:39 -040013054 if (rc) {
James Smart07b1b912019-08-14 16:56:58 -070013055 pci_free_irq_vectors(phba->pcidev);
James Smartda0436e2009-05-22 14:51:39 -040013056 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
13057 "0490 MSI request_irq failed (%d)\n", rc);
James Smart75baf692010-06-08 18:31:21 -040013058 return rc;
James Smartda0436e2009-05-22 14:51:39 -040013059 }
13060
James Smartdcaa2132019-11-04 16:57:06 -080013061 eqhdl = lpfc_get_eq_hdl(0);
13062 eqhdl->irq = pci_irq_vector(phba->pcidev, 0);
13063
13064 cpu = cpumask_first(cpu_present_mask);
13065 lpfc_assign_eq_map_info(phba, 0, LPFC_CPU_FIRST_IRQ, cpu);
13066
James Smart6a828b02019-01-28 11:14:31 -080013067 for (index = 0; index < phba->cfg_irq_chann; index++) {
James Smartdcaa2132019-11-04 16:57:06 -080013068 eqhdl = lpfc_get_eq_hdl(index);
13069 eqhdl->idx = index;
James Smartda0436e2009-05-22 14:51:39 -040013070 }
13071
James Smart75baf692010-06-08 18:31:21 -040013072 return 0;
James Smartda0436e2009-05-22 14:51:39 -040013073}
13074
13075/**
James Smartda0436e2009-05-22 14:51:39 -040013076 * lpfc_sli4_enable_intr - Enable device interrupt to SLI-4 device
13077 * @phba: pointer to lpfc hba data structure.
Lee Jonesfe614ac2020-07-23 13:24:22 +010013078 * @cfg_mode: Interrupt configuration mode (INTx, MSI or MSI-X).
James Smartda0436e2009-05-22 14:51:39 -040013079 *
13080 * This routine is invoked to enable device interrupt and associate driver's
13081 * interrupt handler(s) to interrupt vector(s) to device with SLI-4
13082 * interface spec. Depends on the interrupt mode configured to the driver,
13083 * the driver will try to fallback from the configured interrupt mode to an
13084 * interrupt mode which is supported by the platform, kernel, and device in
13085 * the order of:
13086 * MSI-X -> MSI -> IRQ.
13087 *
13088 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020013089 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -040013090 * other values - error
13091 **/
13092static uint32_t
13093lpfc_sli4_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
13094{
13095 uint32_t intr_mode = LPFC_INTR_ERROR;
James Smart895427b2017-02-12 13:52:30 -080013096 int retval, idx;
James Smartda0436e2009-05-22 14:51:39 -040013097
13098 if (cfg_mode == 2) {
13099 /* Preparation before conf_msi mbox cmd */
13100 retval = 0;
13101 if (!retval) {
13102 /* Now, try to enable MSI-X interrupt mode */
13103 retval = lpfc_sli4_enable_msix(phba);
13104 if (!retval) {
13105 /* Indicate initialization to MSI-X mode */
13106 phba->intr_type = MSIX;
13107 intr_mode = 2;
13108 }
13109 }
13110 }
13111
13112 /* Fallback to MSI if MSI-X initialization failed */
13113 if (cfg_mode >= 1 && phba->intr_type == NONE) {
13114 retval = lpfc_sli4_enable_msi(phba);
13115 if (!retval) {
13116 /* Indicate initialization to MSI mode */
13117 phba->intr_type = MSI;
13118 intr_mode = 1;
13119 }
13120 }
13121
13122 /* Fallback to INTx if both MSI-X/MSI initalization failed */
13123 if (phba->intr_type == NONE) {
13124 retval = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
13125 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
13126 if (!retval) {
James Smart895427b2017-02-12 13:52:30 -080013127 struct lpfc_hba_eq_hdl *eqhdl;
James Smartdcaa2132019-11-04 16:57:06 -080013128 unsigned int cpu;
James Smart895427b2017-02-12 13:52:30 -080013129
James Smartda0436e2009-05-22 14:51:39 -040013130 /* Indicate initialization to INTx mode */
13131 phba->intr_type = INTx;
13132 intr_mode = 0;
James Smart895427b2017-02-12 13:52:30 -080013133
James Smartdcaa2132019-11-04 16:57:06 -080013134 eqhdl = lpfc_get_eq_hdl(0);
13135 eqhdl->irq = pci_irq_vector(phba->pcidev, 0);
13136
13137 cpu = cpumask_first(cpu_present_mask);
13138 lpfc_assign_eq_map_info(phba, 0, LPFC_CPU_FIRST_IRQ,
13139 cpu);
James Smart6a828b02019-01-28 11:14:31 -080013140 for (idx = 0; idx < phba->cfg_irq_chann; idx++) {
James Smartdcaa2132019-11-04 16:57:06 -080013141 eqhdl = lpfc_get_eq_hdl(idx);
James Smart895427b2017-02-12 13:52:30 -080013142 eqhdl->idx = idx;
James Smart1ba981f2014-02-20 09:56:45 -050013143 }
James Smartda0436e2009-05-22 14:51:39 -040013144 }
13145 }
13146 return intr_mode;
13147}
13148
13149/**
13150 * lpfc_sli4_disable_intr - Disable device interrupt to SLI-4 device
13151 * @phba: pointer to lpfc hba data structure.
13152 *
13153 * This routine is invoked to disable device interrupt and disassociate
13154 * the driver's interrupt handler(s) from interrupt vector(s) to device
13155 * with SLI-4 interface spec. Depending on the interrupt mode, the driver
13156 * will release the interrupt vector(s) for the message signaled interrupt.
13157 **/
13158static void
13159lpfc_sli4_disable_intr(struct lpfc_hba *phba)
13160{
13161 /* Disable the currently initialized interrupt mode */
Christoph Hellwig45ffac12017-02-12 13:52:26 -080013162 if (phba->intr_type == MSIX) {
13163 int index;
James Smartdcaa2132019-11-04 16:57:06 -080013164 struct lpfc_hba_eq_hdl *eqhdl;
Christoph Hellwig45ffac12017-02-12 13:52:26 -080013165
13166 /* Free up MSI-X multi-message vectors */
James Smart6a828b02019-01-28 11:14:31 -080013167 for (index = 0; index < phba->cfg_irq_chann; index++) {
James Smartdcaa2132019-11-04 16:57:06 -080013168 eqhdl = lpfc_get_eq_hdl(index);
13169 lpfc_irq_clear_aff(eqhdl);
13170 irq_set_affinity_hint(eqhdl->irq, NULL);
13171 free_irq(eqhdl->irq, eqhdl);
James Smartb3295c22019-01-28 11:14:30 -080013172 }
Christoph Hellwig45ffac12017-02-12 13:52:26 -080013173 } else {
James Smartda0436e2009-05-22 14:51:39 -040013174 free_irq(phba->pcidev->irq, phba);
Christoph Hellwig45ffac12017-02-12 13:52:26 -080013175 }
13176
13177 pci_free_irq_vectors(phba->pcidev);
James Smartda0436e2009-05-22 14:51:39 -040013178
13179 /* Reset interrupt management states */
13180 phba->intr_type = NONE;
13181 phba->sli.slistat.sli_intr = 0;
James Smartda0436e2009-05-22 14:51:39 -040013182}
13183
13184/**
James Smart3772a992009-05-22 14:50:54 -040013185 * lpfc_unset_hba - Unset SLI3 hba device initialization
13186 * @phba: pointer to lpfc hba data structure.
13187 *
13188 * This routine is invoked to unset the HBA device initialization steps to
13189 * a device with SLI-3 interface spec.
13190 **/
13191static void
13192lpfc_unset_hba(struct lpfc_hba *phba)
13193{
13194 struct lpfc_vport *vport = phba->pport;
13195 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
13196
13197 spin_lock_irq(shost->host_lock);
13198 vport->load_flag |= FC_UNLOADING;
13199 spin_unlock_irq(shost->host_lock);
13200
James Smart72859902012-01-18 16:25:38 -050013201 kfree(phba->vpi_bmask);
13202 kfree(phba->vpi_ids);
13203
James Smart3772a992009-05-22 14:50:54 -040013204 lpfc_stop_hba_timers(phba);
13205
13206 phba->pport->work_port_events = 0;
13207
13208 lpfc_sli_hba_down(phba);
13209
13210 lpfc_sli_brdrestart(phba);
13211
13212 lpfc_sli_disable_intr(phba);
13213
13214 return;
13215}
13216
13217/**
James Smart5af5eee2010-10-22 11:06:38 -040013218 * lpfc_sli4_xri_exchange_busy_wait - Wait for device XRI exchange busy
13219 * @phba: Pointer to HBA context object.
13220 *
13221 * This function is called in the SLI4 code path to wait for completion
13222 * of device's XRIs exchange busy. It will check the XRI exchange busy
13223 * on outstanding FCP and ELS I/Os every 10ms for up to 10 seconds; after
13224 * that, it will check the XRI exchange busy on outstanding FCP and ELS
13225 * I/Os every 30 seconds, log error message, and wait forever. Only when
13226 * all XRI exchange busy complete, the driver unload shall proceed with
13227 * invoking the function reset ioctl mailbox command to the CNA and the
13228 * the rest of the driver unload resource release.
13229 **/
13230static void
13231lpfc_sli4_xri_exchange_busy_wait(struct lpfc_hba *phba)
13232{
James Smart5e5b5112019-01-28 11:14:22 -080013233 struct lpfc_sli4_hdw_queue *qp;
James Smartc00f62e2019-08-14 16:57:11 -070013234 int idx, ccnt;
James Smart5af5eee2010-10-22 11:06:38 -040013235 int wait_time = 0;
James Smart5e5b5112019-01-28 11:14:22 -080013236 int io_xri_cmpl = 1;
James Smart86c67372017-04-21 16:05:04 -070013237 int nvmet_xri_cmpl = 1;
James Smart5af5eee2010-10-22 11:06:38 -040013238 int els_xri_cmpl = list_empty(&phba->sli4_hba.lpfc_abts_els_sgl_list);
13239
James Smartc3725bd2017-11-20 16:00:42 -080013240 /* Driver just aborted IOs during the hba_unset process. Pause
13241 * here to give the HBA time to complete the IO and get entries
13242 * into the abts lists.
13243 */
13244 msleep(LPFC_XRI_EXCH_BUSY_WAIT_T1 * 5);
13245
13246 /* Wait for NVME pending IO to flush back to transport. */
13247 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
13248 lpfc_nvme_wait_for_io_drain(phba);
13249
James Smart5e5b5112019-01-28 11:14:22 -080013250 ccnt = 0;
James Smart5e5b5112019-01-28 11:14:22 -080013251 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
13252 qp = &phba->sli4_hba.hdwq[idx];
James Smartc00f62e2019-08-14 16:57:11 -070013253 io_xri_cmpl = list_empty(&qp->lpfc_abts_io_buf_list);
13254 if (!io_xri_cmpl) /* if list is NOT empty */
13255 ccnt++;
James Smart5e5b5112019-01-28 11:14:22 -080013256 }
13257 if (ccnt)
13258 io_xri_cmpl = 0;
James Smart5e5b5112019-01-28 11:14:22 -080013259
James Smart86c67372017-04-21 16:05:04 -070013260 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
James Smart86c67372017-04-21 16:05:04 -070013261 nvmet_xri_cmpl =
13262 list_empty(&phba->sli4_hba.lpfc_abts_nvmet_ctx_list);
13263 }
James Smart895427b2017-02-12 13:52:30 -080013264
James Smartc00f62e2019-08-14 16:57:11 -070013265 while (!els_xri_cmpl || !io_xri_cmpl || !nvmet_xri_cmpl) {
James Smart5af5eee2010-10-22 11:06:38 -040013266 if (wait_time > LPFC_XRI_EXCH_BUSY_WAIT_TMO) {
James Smart68c9b552018-06-26 08:24:25 -070013267 if (!nvmet_xri_cmpl)
Dick Kennedy372c1872020-06-30 14:50:00 -070013268 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart68c9b552018-06-26 08:24:25 -070013269 "6424 NVMET XRI exchange busy "
13270 "wait time: %d seconds.\n",
13271 wait_time/1000);
James Smart5e5b5112019-01-28 11:14:22 -080013272 if (!io_xri_cmpl)
Dick Kennedy372c1872020-06-30 14:50:00 -070013273 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartc00f62e2019-08-14 16:57:11 -070013274 "6100 IO XRI exchange busy "
James Smart5af5eee2010-10-22 11:06:38 -040013275 "wait time: %d seconds.\n",
13276 wait_time/1000);
13277 if (!els_xri_cmpl)
Dick Kennedy372c1872020-06-30 14:50:00 -070013278 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart5af5eee2010-10-22 11:06:38 -040013279 "2878 ELS XRI exchange busy "
13280 "wait time: %d seconds.\n",
13281 wait_time/1000);
13282 msleep(LPFC_XRI_EXCH_BUSY_WAIT_T2);
13283 wait_time += LPFC_XRI_EXCH_BUSY_WAIT_T2;
13284 } else {
13285 msleep(LPFC_XRI_EXCH_BUSY_WAIT_T1);
13286 wait_time += LPFC_XRI_EXCH_BUSY_WAIT_T1;
13287 }
James Smart5e5b5112019-01-28 11:14:22 -080013288
13289 ccnt = 0;
James Smart5e5b5112019-01-28 11:14:22 -080013290 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
13291 qp = &phba->sli4_hba.hdwq[idx];
James Smartc00f62e2019-08-14 16:57:11 -070013292 io_xri_cmpl = list_empty(
13293 &qp->lpfc_abts_io_buf_list);
13294 if (!io_xri_cmpl) /* if list is NOT empty */
13295 ccnt++;
James Smart5e5b5112019-01-28 11:14:22 -080013296 }
13297 if (ccnt)
13298 io_xri_cmpl = 0;
James Smart5e5b5112019-01-28 11:14:22 -080013299
James Smart86c67372017-04-21 16:05:04 -070013300 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
James Smart86c67372017-04-21 16:05:04 -070013301 nvmet_xri_cmpl = list_empty(
13302 &phba->sli4_hba.lpfc_abts_nvmet_ctx_list);
13303 }
James Smart5af5eee2010-10-22 11:06:38 -040013304 els_xri_cmpl =
13305 list_empty(&phba->sli4_hba.lpfc_abts_els_sgl_list);
James Smartf358dd02017-02-12 13:52:34 -080013306
James Smart5af5eee2010-10-22 11:06:38 -040013307 }
13308}
13309
13310/**
James Smartda0436e2009-05-22 14:51:39 -040013311 * lpfc_sli4_hba_unset - Unset the fcoe hba
13312 * @phba: Pointer to HBA context object.
13313 *
13314 * This function is called in the SLI4 code path to reset the HBA's FCoE
13315 * function. The caller is not required to hold any lock. This routine
13316 * issues PCI function reset mailbox command to reset the FCoE function.
13317 * At the end of the function, it calls lpfc_hba_down_post function to
13318 * free any pending commands.
13319 **/
13320static void
13321lpfc_sli4_hba_unset(struct lpfc_hba *phba)
13322{
13323 int wait_cnt = 0;
13324 LPFC_MBOXQ_t *mboxq;
James Smart912e3ac2011-05-24 11:42:11 -040013325 struct pci_dev *pdev = phba->pcidev;
James Smartda0436e2009-05-22 14:51:39 -040013326
13327 lpfc_stop_hba_timers(phba);
James Smart02243832021-08-16 09:28:54 -070013328 hrtimer_cancel(&phba->cmf_timer);
13329
James Smartcdb42be2019-01-28 11:14:21 -080013330 if (phba->pport)
13331 phba->sli4_hba.intr_enable = 0;
James Smartda0436e2009-05-22 14:51:39 -040013332
13333 /*
13334 * Gracefully wait out the potential current outstanding asynchronous
13335 * mailbox command.
13336 */
13337
13338 /* First, block any pending async mailbox command from posted */
13339 spin_lock_irq(&phba->hbalock);
13340 phba->sli.sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
13341 spin_unlock_irq(&phba->hbalock);
13342 /* Now, trying to wait it out if we can */
13343 while (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
13344 msleep(10);
13345 if (++wait_cnt > LPFC_ACTIVE_MBOX_WAIT_CNT)
13346 break;
13347 }
13348 /* Forcefully release the outstanding mailbox command if timed out */
13349 if (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
13350 spin_lock_irq(&phba->hbalock);
13351 mboxq = phba->sli.mbox_active;
13352 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
13353 __lpfc_mbox_cmpl_put(phba, mboxq);
13354 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
13355 phba->sli.mbox_active = NULL;
13356 spin_unlock_irq(&phba->hbalock);
13357 }
13358
James Smart5af5eee2010-10-22 11:06:38 -040013359 /* Abort all iocbs associated with the hba */
13360 lpfc_sli_hba_iocb_abort(phba);
13361
13362 /* Wait for completion of device XRI exchange busy */
13363 lpfc_sli4_xri_exchange_busy_wait(phba);
13364
James Smart93a4d6f2019-11-04 16:57:05 -080013365 /* per-phba callback de-registration for hotplug event */
SeongJae Park46da5472020-06-23 10:41:22 +020013366 if (phba->pport)
13367 lpfc_cpuhp_remove(phba);
James Smart93a4d6f2019-11-04 16:57:05 -080013368
James Smartda0436e2009-05-22 14:51:39 -040013369 /* Disable PCI subsystem interrupt */
13370 lpfc_sli4_disable_intr(phba);
13371
James Smart912e3ac2011-05-24 11:42:11 -040013372 /* Disable SR-IOV if enabled */
13373 if (phba->cfg_sriov_nr_virtfn)
13374 pci_disable_sriov(pdev);
13375
James Smartda0436e2009-05-22 14:51:39 -040013376 /* Stop kthread signal shall trigger work_done one more time */
13377 kthread_stop(phba->worker_thread);
13378
James Smartd2cc9bc2018-09-10 10:30:50 -070013379 /* Disable FW logging to host memory */
James Smart1165a5c2018-11-29 16:09:39 -080013380 lpfc_ras_stop_fwlog(phba);
James Smartd2cc9bc2018-09-10 10:30:50 -070013381
James Smartd1f525a2017-04-21 16:04:55 -070013382 /* Unset the queues shared with the hardware then release all
13383 * allocated resources.
13384 */
13385 lpfc_sli4_queue_unset(phba);
13386 lpfc_sli4_queue_destroy(phba);
13387
James Smart3677a3a2010-09-29 11:19:14 -040013388 /* Reset SLI4 HBA FCoE function */
13389 lpfc_pci_function_reset(phba);
13390
James Smart1165a5c2018-11-29 16:09:39 -080013391 /* Free RAS DMA memory */
13392 if (phba->ras_fwlog.ras_enabled)
13393 lpfc_sli4_ras_dma_free(phba);
13394
James Smartda0436e2009-05-22 14:51:39 -040013395 /* Stop the SLI4 device port */
James Smart1ffdd2c2019-03-04 15:27:51 -080013396 if (phba->pport)
13397 phba->pport->work_port_events = 0;
James Smartda0436e2009-05-22 14:51:39 -040013398}
13399
James Smart7481811c2021-08-16 09:28:56 -070013400static uint32_t
13401lpfc_cgn_crc32(uint32_t crc, u8 byte)
13402{
13403 uint32_t msb = 0;
13404 uint32_t bit;
13405
13406 for (bit = 0; bit < 8; bit++) {
13407 msb = (crc >> 31) & 1;
13408 crc <<= 1;
13409
13410 if (msb ^ (byte & 1)) {
13411 crc ^= LPFC_CGN_CRC32_MAGIC_NUMBER;
13412 crc |= 1;
13413 }
13414 byte >>= 1;
13415 }
13416 return crc;
13417}
13418
13419static uint32_t
13420lpfc_cgn_reverse_bits(uint32_t wd)
13421{
13422 uint32_t result = 0;
13423 uint32_t i;
13424
13425 for (i = 0; i < 32; i++) {
13426 result <<= 1;
13427 result |= (1 & (wd >> i));
13428 }
13429 return result;
13430}
13431
13432/*
13433 * The routine corresponds with the algorithm the HBA firmware
13434 * uses to validate the data integrity.
13435 */
13436uint32_t
13437lpfc_cgn_calc_crc32(void *ptr, uint32_t byteLen, uint32_t crc)
13438{
13439 uint32_t i;
13440 uint32_t result;
13441 uint8_t *data = (uint8_t *)ptr;
13442
13443 for (i = 0; i < byteLen; ++i)
13444 crc = lpfc_cgn_crc32(crc, data[i]);
13445
13446 result = ~lpfc_cgn_reverse_bits(crc);
13447 return result;
13448}
13449
James Smart8c42a652021-08-16 09:28:51 -070013450void
13451lpfc_init_congestion_buf(struct lpfc_hba *phba)
13452{
James Smart7481811c2021-08-16 09:28:56 -070013453 struct lpfc_cgn_info *cp;
13454 struct timespec64 cmpl_time;
13455 struct tm broken;
13456 uint16_t size;
13457 uint32_t crc;
13458
James Smart8c42a652021-08-16 09:28:51 -070013459 lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
13460 "6235 INIT Congestion Buffer %p\n", phba->cgn_i);
13461
13462 if (!phba->cgn_i)
13463 return;
James Smart7481811c2021-08-16 09:28:56 -070013464 cp = (struct lpfc_cgn_info *)phba->cgn_i->virt;
James Smart8c42a652021-08-16 09:28:51 -070013465
13466 atomic_set(&phba->cgn_fabric_warn_cnt, 0);
13467 atomic_set(&phba->cgn_fabric_alarm_cnt, 0);
13468 atomic_set(&phba->cgn_sync_alarm_cnt, 0);
13469 atomic_set(&phba->cgn_sync_warn_cnt, 0);
13470
James Smart8c42a652021-08-16 09:28:51 -070013471 atomic_set(&phba->cgn_driver_evt_cnt, 0);
13472 atomic_set(&phba->cgn_latency_evt_cnt, 0);
13473 atomic64_set(&phba->cgn_latency_evt, 0);
13474 phba->cgn_evt_minute = 0;
James Smart7481811c2021-08-16 09:28:56 -070013475 phba->hba_flag &= ~HBA_CGN_DAY_WRAP;
13476
13477 memset(cp, 0xff, LPFC_CGN_DATA_SIZE);
13478 cp->cgn_info_size = cpu_to_le16(LPFC_CGN_INFO_SZ);
13479 cp->cgn_info_version = LPFC_CGN_INFO_V3;
13480
13481 /* cgn parameters */
13482 cp->cgn_info_mode = phba->cgn_p.cgn_param_mode;
13483 cp->cgn_info_level0 = phba->cgn_p.cgn_param_level0;
13484 cp->cgn_info_level1 = phba->cgn_p.cgn_param_level1;
13485 cp->cgn_info_level2 = phba->cgn_p.cgn_param_level2;
13486
13487 ktime_get_real_ts64(&cmpl_time);
13488 time64_to_tm(cmpl_time.tv_sec, 0, &broken);
13489
13490 cp->cgn_info_month = broken.tm_mon + 1;
13491 cp->cgn_info_day = broken.tm_mday;
13492 cp->cgn_info_year = broken.tm_year - 100; /* relative to 2000 */
13493 cp->cgn_info_hour = broken.tm_hour;
13494 cp->cgn_info_minute = broken.tm_min;
13495 cp->cgn_info_second = broken.tm_sec;
13496
13497 lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT | LOG_INIT,
13498 "2643 CGNInfo Init: Start Time "
13499 "%d/%d/%d %d:%d:%d\n",
13500 cp->cgn_info_day, cp->cgn_info_month,
13501 cp->cgn_info_year, cp->cgn_info_hour,
13502 cp->cgn_info_minute, cp->cgn_info_second);
13503
13504 /* Fill in default LUN qdepth */
13505 if (phba->pport) {
13506 size = (uint16_t)(phba->pport->cfg_lun_queue_depth);
13507 cp->cgn_lunq = cpu_to_le16(size);
13508 }
13509
13510 /* last used Index initialized to 0xff already */
13511
James Smart59936432021-08-30 16:12:43 -070013512 cp->cgn_warn_freq = cpu_to_le16(LPFC_FPIN_INIT_FREQ);
13513 cp->cgn_alarm_freq = cpu_to_le16(LPFC_FPIN_INIT_FREQ);
James Smart7481811c2021-08-16 09:28:56 -070013514 crc = lpfc_cgn_calc_crc32(cp, LPFC_CGN_INFO_SZ, LPFC_CGN_CRC32_SEED);
13515 cp->cgn_info_crc = cpu_to_le32(crc);
James Smart8c42a652021-08-16 09:28:51 -070013516
13517 phba->cgn_evt_timestamp = jiffies +
13518 msecs_to_jiffies(LPFC_CGN_TIMER_TO_MIN);
13519}
13520
13521void
13522lpfc_init_congestion_stat(struct lpfc_hba *phba)
13523{
James Smart7481811c2021-08-16 09:28:56 -070013524 struct lpfc_cgn_info *cp;
13525 struct timespec64 cmpl_time;
13526 struct tm broken;
13527 uint32_t crc;
13528
James Smart8c42a652021-08-16 09:28:51 -070013529 lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
13530 "6236 INIT Congestion Stat %p\n", phba->cgn_i);
13531
13532 if (!phba->cgn_i)
13533 return;
James Smart7481811c2021-08-16 09:28:56 -070013534
13535 cp = (struct lpfc_cgn_info *)phba->cgn_i->virt;
13536 memset(&cp->cgn_stat_npm, 0, LPFC_CGN_STAT_SIZE);
13537
13538 ktime_get_real_ts64(&cmpl_time);
13539 time64_to_tm(cmpl_time.tv_sec, 0, &broken);
13540
13541 cp->cgn_stat_month = broken.tm_mon + 1;
13542 cp->cgn_stat_day = broken.tm_mday;
13543 cp->cgn_stat_year = broken.tm_year - 100; /* relative to 2000 */
13544 cp->cgn_stat_hour = broken.tm_hour;
13545 cp->cgn_stat_minute = broken.tm_min;
13546
13547 lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT | LOG_INIT,
13548 "2647 CGNstat Init: Start Time "
13549 "%d/%d/%d %d:%d\n",
13550 cp->cgn_stat_day, cp->cgn_stat_month,
13551 cp->cgn_stat_year, cp->cgn_stat_hour,
13552 cp->cgn_stat_minute);
13553
13554 crc = lpfc_cgn_calc_crc32(cp, LPFC_CGN_INFO_SZ, LPFC_CGN_CRC32_SEED);
13555 cp->cgn_info_crc = cpu_to_le32(crc);
James Smart8c42a652021-08-16 09:28:51 -070013556}
13557
13558/**
13559 * __lpfc_reg_congestion_buf - register congestion info buffer with HBA
13560 * @phba: Pointer to hba context object.
13561 * @reg: flag to determine register or unregister.
13562 */
13563static int
13564__lpfc_reg_congestion_buf(struct lpfc_hba *phba, int reg)
13565{
13566 struct lpfc_mbx_reg_congestion_buf *reg_congestion_buf;
13567 union lpfc_sli4_cfg_shdr *shdr;
13568 uint32_t shdr_status, shdr_add_status;
13569 LPFC_MBOXQ_t *mboxq;
13570 int length, rc;
13571
13572 if (!phba->cgn_i)
13573 return -ENXIO;
13574
13575 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13576 if (!mboxq) {
13577 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
13578 "2641 REG_CONGESTION_BUF mbox allocation fail: "
13579 "HBA state x%x reg %d\n",
13580 phba->pport->port_state, reg);
13581 return -ENOMEM;
13582 }
13583
13584 length = (sizeof(struct lpfc_mbx_reg_congestion_buf) -
13585 sizeof(struct lpfc_sli4_cfg_mhdr));
13586 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
13587 LPFC_MBOX_OPCODE_REG_CONGESTION_BUF, length,
13588 LPFC_SLI4_MBX_EMBED);
13589 reg_congestion_buf = &mboxq->u.mqe.un.reg_congestion_buf;
13590 bf_set(lpfc_mbx_reg_cgn_buf_type, reg_congestion_buf, 1);
13591 if (reg > 0)
13592 bf_set(lpfc_mbx_reg_cgn_buf_cnt, reg_congestion_buf, 1);
13593 else
13594 bf_set(lpfc_mbx_reg_cgn_buf_cnt, reg_congestion_buf, 0);
13595 reg_congestion_buf->length = sizeof(struct lpfc_cgn_info);
13596 reg_congestion_buf->addr_lo =
13597 putPaddrLow(phba->cgn_i->phys);
13598 reg_congestion_buf->addr_hi =
13599 putPaddrHigh(phba->cgn_i->phys);
13600
13601 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
13602 shdr = (union lpfc_sli4_cfg_shdr *)
13603 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
13604 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13605 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
13606 &shdr->response);
13607 mempool_free(mboxq, phba->mbox_mem_pool);
13608 if (shdr_status || shdr_add_status || rc) {
13609 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13610 "2642 REG_CONGESTION_BUF mailbox "
13611 "failed with status x%x add_status x%x,"
13612 " mbx status x%x reg %d\n",
13613 shdr_status, shdr_add_status, rc, reg);
13614 return -ENXIO;
13615 }
13616 return 0;
13617}
13618
James Smart02243832021-08-16 09:28:54 -070013619int
James Smart8c42a652021-08-16 09:28:51 -070013620lpfc_unreg_congestion_buf(struct lpfc_hba *phba)
13621{
James Smart02243832021-08-16 09:28:54 -070013622 lpfc_cmf_stop(phba);
James Smart8c42a652021-08-16 09:28:51 -070013623 return __lpfc_reg_congestion_buf(phba, 0);
13624}
13625
13626int
13627lpfc_reg_congestion_buf(struct lpfc_hba *phba)
13628{
13629 return __lpfc_reg_congestion_buf(phba, 1);
13630}
13631
James Smartda0436e2009-05-22 14:51:39 -040013632/**
James Smartfedd3b72011-02-16 12:39:24 -050013633 * lpfc_get_sli4_parameters - Get the SLI4 Config PARAMETERS.
13634 * @phba: Pointer to HBA context object.
13635 * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
13636 *
13637 * This function is called in the SLI4 code path to read the port's
13638 * sli4 capabilities.
13639 *
13640 * This function may be be called from any context that can block-wait
13641 * for the completion. The expectation is that this routine is called
13642 * typically from probe_one or from the online routine.
13643 **/
13644int
13645lpfc_get_sli4_parameters(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
13646{
13647 int rc;
13648 struct lpfc_mqe *mqe = &mboxq->u.mqe;
13649 struct lpfc_pc_sli4_params *sli4_params;
James Smarta183a152011-10-10 21:32:43 -040013650 uint32_t mbox_tmo;
James Smartfedd3b72011-02-16 12:39:24 -050013651 int length;
James Smartbf316c72018-04-09 14:24:28 -070013652 bool exp_wqcq_pages = true;
James Smartfedd3b72011-02-16 12:39:24 -050013653 struct lpfc_sli4_parameters *mbx_sli4_parameters;
13654
James Smart6d368e52011-05-24 11:44:12 -040013655 /*
13656 * By default, the driver assumes the SLI4 port requires RPI
13657 * header postings. The SLI4_PARAM response will correct this
13658 * assumption.
13659 */
13660 phba->sli4_hba.rpi_hdrs_in_use = 1;
13661
James Smartfedd3b72011-02-16 12:39:24 -050013662 /* Read the port's SLI4 Config Parameters */
13663 length = (sizeof(struct lpfc_mbx_get_sli4_parameters) -
13664 sizeof(struct lpfc_sli4_cfg_mhdr));
13665 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
13666 LPFC_MBOX_OPCODE_GET_SLI4_PARAMETERS,
13667 length, LPFC_SLI4_MBX_EMBED);
13668 if (!phba->sli4_hba.intr_enable)
13669 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smarta183a152011-10-10 21:32:43 -040013670 else {
13671 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
13672 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
13673 }
James Smartfedd3b72011-02-16 12:39:24 -050013674 if (unlikely(rc))
13675 return rc;
13676 sli4_params = &phba->sli4_hba.pc_sli4_params;
13677 mbx_sli4_parameters = &mqe->un.get_sli4_parameters.sli4_parameters;
13678 sli4_params->if_type = bf_get(cfg_if_type, mbx_sli4_parameters);
13679 sli4_params->sli_rev = bf_get(cfg_sli_rev, mbx_sli4_parameters);
13680 sli4_params->sli_family = bf_get(cfg_sli_family, mbx_sli4_parameters);
13681 sli4_params->featurelevel_1 = bf_get(cfg_sli_hint_1,
13682 mbx_sli4_parameters);
13683 sli4_params->featurelevel_2 = bf_get(cfg_sli_hint_2,
13684 mbx_sli4_parameters);
13685 if (bf_get(cfg_phwq, mbx_sli4_parameters))
13686 phba->sli3_options |= LPFC_SLI4_PHWQ_ENABLED;
13687 else
13688 phba->sli3_options &= ~LPFC_SLI4_PHWQ_ENABLED;
13689 sli4_params->sge_supp_len = mbx_sli4_parameters->sge_supp_len;
James Smartb62232b2021-04-11 18:31:22 -070013690 sli4_params->loopbk_scope = bf_get(cfg_loopbk_scope,
13691 mbx_sli4_parameters);
James Smart1ba981f2014-02-20 09:56:45 -050013692 sli4_params->oas_supported = bf_get(cfg_oas, mbx_sli4_parameters);
James Smartfedd3b72011-02-16 12:39:24 -050013693 sli4_params->cqv = bf_get(cfg_cqv, mbx_sli4_parameters);
13694 sli4_params->mqv = bf_get(cfg_mqv, mbx_sli4_parameters);
13695 sli4_params->wqv = bf_get(cfg_wqv, mbx_sli4_parameters);
13696 sli4_params->rqv = bf_get(cfg_rqv, mbx_sli4_parameters);
James Smart7365f6f2018-02-22 08:18:46 -080013697 sli4_params->eqav = bf_get(cfg_eqav, mbx_sli4_parameters);
13698 sli4_params->cqav = bf_get(cfg_cqav, mbx_sli4_parameters);
James Smart0c651872013-07-15 18:33:23 -040013699 sli4_params->wqsize = bf_get(cfg_wqsize, mbx_sli4_parameters);
James Smart66e9e6b2018-06-26 08:24:27 -070013700 sli4_params->bv1s = bf_get(cfg_bv1s, mbx_sli4_parameters);
James Smart83c6cb12019-10-18 14:18:30 -070013701 sli4_params->pls = bf_get(cfg_pvl, mbx_sli4_parameters);
James Smartfedd3b72011-02-16 12:39:24 -050013702 sli4_params->sgl_pages_max = bf_get(cfg_sgl_page_cnt,
13703 mbx_sli4_parameters);
James Smart895427b2017-02-12 13:52:30 -080013704 sli4_params->wqpcnt = bf_get(cfg_wqpcnt, mbx_sli4_parameters);
James Smartfedd3b72011-02-16 12:39:24 -050013705 sli4_params->sgl_pp_align = bf_get(cfg_sgl_pp_align,
13706 mbx_sli4_parameters);
James Smart6d368e52011-05-24 11:44:12 -040013707 phba->sli4_hba.extents_in_use = bf_get(cfg_ext, mbx_sli4_parameters);
13708 phba->sli4_hba.rpi_hdrs_in_use = bf_get(cfg_hdrr, mbx_sli4_parameters);
James Smart895427b2017-02-12 13:52:30 -080013709
James Smartd79c9e92019-08-14 16:57:09 -070013710 /* Check for Extended Pre-Registered SGL support */
13711 phba->cfg_xpsgl = bf_get(cfg_xpsgl, mbx_sli4_parameters);
13712
James Smartc15e0702019-05-21 17:49:02 -070013713 /* Check for firmware nvme support */
13714 rc = (bf_get(cfg_nvme, mbx_sli4_parameters) &&
13715 bf_get(cfg_xib, mbx_sli4_parameters));
James Smart895427b2017-02-12 13:52:30 -080013716
James Smartc15e0702019-05-21 17:49:02 -070013717 if (rc) {
13718 /* Save this to indicate the Firmware supports NVME */
13719 sli4_params->nvme = 1;
13720
13721 /* Firmware NVME support, check driver FC4 NVME support */
13722 if (phba->cfg_enable_fc4_type == LPFC_ENABLE_FCP) {
13723 lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_NVME,
13724 "6133 Disabling NVME support: "
13725 "FC4 type not supported: x%x\n",
13726 phba->cfg_enable_fc4_type);
13727 goto fcponly;
13728 }
13729 } else {
13730 /* No firmware NVME support, check driver FC4 NVME support */
13731 sli4_params->nvme = 0;
13732 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
13733 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_NVME,
13734 "6101 Disabling NVME support: Not "
13735 "supported by firmware (%d %d) x%x\n",
13736 bf_get(cfg_nvme, mbx_sli4_parameters),
13737 bf_get(cfg_xib, mbx_sli4_parameters),
13738 phba->cfg_enable_fc4_type);
13739fcponly:
James Smartc15e0702019-05-21 17:49:02 -070013740 phba->nvmet_support = 0;
13741 phba->cfg_nvmet_mrq = 0;
James Smart6a224b42019-08-14 16:56:57 -070013742 phba->cfg_nvme_seg_cnt = 0;
James Smartc15e0702019-05-21 17:49:02 -070013743
13744 /* If no FC4 type support, move to just SCSI support */
13745 if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP))
13746 return -ENODEV;
13747 phba->cfg_enable_fc4_type = LPFC_ENABLE_FCP;
13748 }
James Smart895427b2017-02-12 13:52:30 -080013749 }
James Smart05580562011-05-24 11:40:48 -040013750
James Smartc26c2652019-08-14 16:56:49 -070013751 /* If the NVME FC4 type is enabled, scale the sg_seg_cnt to
James Smarta5f73372019-09-21 20:58:50 -070013752 * accommodate 512K and 1M IOs in a single nvme buf.
James Smartc26c2652019-08-14 16:56:49 -070013753 */
James Smarta5f73372019-09-21 20:58:50 -070013754 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
James Smartc26c2652019-08-14 16:56:49 -070013755 phba->cfg_sg_seg_cnt = LPFC_MAX_NVME_SEG_CNT;
James Smartc26c2652019-08-14 16:56:49 -070013756
James Smart137ddf02021-07-07 11:43:44 -070013757 /* Enable embedded Payload BDE if support is indicated */
13758 if (bf_get(cfg_pbde, mbx_sli4_parameters))
13759 phba->cfg_enable_pbde = 1;
13760 else
James Smart414abe02018-06-26 08:24:26 -070013761 phba->cfg_enable_pbde = 0;
James Smart0bc2b7c2018-02-22 08:18:48 -080013762
James Smart20aefac2018-01-30 15:58:58 -080013763 /*
13764 * To support Suppress Response feature we must satisfy 3 conditions.
13765 * lpfc_suppress_rsp module parameter must be set (default).
13766 * In SLI4-Parameters Descriptor:
13767 * Extended Inline Buffers (XIB) must be supported.
13768 * Suppress Response IU Not Supported (SRIUNS) must NOT be supported
13769 * (double negative).
13770 */
13771 if (phba->cfg_suppress_rsp && bf_get(cfg_xib, mbx_sli4_parameters) &&
13772 !(bf_get(cfg_nosr, mbx_sli4_parameters)))
James Smartf358dd02017-02-12 13:52:34 -080013773 phba->sli.sli_flag |= LPFC_SLI_SUPPRESS_RSP;
James Smart20aefac2018-01-30 15:58:58 -080013774 else
13775 phba->cfg_suppress_rsp = 0;
James Smartf358dd02017-02-12 13:52:34 -080013776
James Smart0cf07f842017-06-01 21:07:10 -070013777 if (bf_get(cfg_eqdr, mbx_sli4_parameters))
13778 phba->sli.sli_flag |= LPFC_SLI_USE_EQDR;
13779
James Smart05580562011-05-24 11:40:48 -040013780 /* Make sure that sge_supp_len can be handled by the driver */
13781 if (sli4_params->sge_supp_len > LPFC_MAX_SGE_SIZE)
13782 sli4_params->sge_supp_len = LPFC_MAX_SGE_SIZE;
13783
James Smartb5c53952016-03-31 14:12:30 -070013784 /*
James Smartc176ffa2018-01-30 15:58:46 -080013785 * Check whether the adapter supports an embedded copy of the
13786 * FCP CMD IU within the WQE for FCP_Ixxx commands. In order
13787 * to use this option, 128-byte WQEs must be used.
James Smartb5c53952016-03-31 14:12:30 -070013788 */
13789 if (bf_get(cfg_ext_embed_cb, mbx_sli4_parameters))
13790 phba->fcp_embed_io = 1;
13791 else
13792 phba->fcp_embed_io = 0;
James Smart7bdedb32016-07-06 12:36:00 -070013793
James Smart0bc2b7c2018-02-22 08:18:48 -080013794 lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_NVME,
James Smart414abe02018-06-26 08:24:26 -070013795 "6422 XIB %d PBDE %d: FCP %d NVME %d %d %d\n",
James Smart0bc2b7c2018-02-22 08:18:48 -080013796 bf_get(cfg_xib, mbx_sli4_parameters),
James Smart414abe02018-06-26 08:24:26 -070013797 phba->cfg_enable_pbde,
James Smartae463b62021-07-07 11:43:32 -070013798 phba->fcp_embed_io, sli4_params->nvme,
James Smart4e565cf2018-02-22 08:18:50 -080013799 phba->cfg_nvme_embed_cmd, phba->cfg_suppress_rsp);
James Smart0bc2b7c2018-02-22 08:18:48 -080013800
James Smartbf316c72018-04-09 14:24:28 -070013801 if ((bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
13802 LPFC_SLI_INTF_IF_TYPE_2) &&
13803 (bf_get(lpfc_sli_intf_sli_family, &phba->sli4_hba.sli_intf) ==
James Smartc2217682018-05-24 21:09:00 -070013804 LPFC_SLI_INTF_FAMILY_LNCR_A0))
James Smartbf316c72018-04-09 14:24:28 -070013805 exp_wqcq_pages = false;
13806
James Smartc176ffa2018-01-30 15:58:46 -080013807 if ((bf_get(cfg_cqpsize, mbx_sli4_parameters) & LPFC_CQ_16K_PAGE_SZ) &&
13808 (bf_get(cfg_wqpsize, mbx_sli4_parameters) & LPFC_WQ_16K_PAGE_SZ) &&
James Smartbf316c72018-04-09 14:24:28 -070013809 exp_wqcq_pages &&
James Smartc176ffa2018-01-30 15:58:46 -080013810 (sli4_params->wqsize & LPFC_WQ_SZ128_SUPPORT))
13811 phba->enab_exp_wqcq_pages = 1;
13812 else
13813 phba->enab_exp_wqcq_pages = 0;
James Smart7bdedb32016-07-06 12:36:00 -070013814 /*
13815 * Check if the SLI port supports MDS Diagnostics
13816 */
13817 if (bf_get(cfg_mds_diags, mbx_sli4_parameters))
13818 phba->mds_diags_support = 1;
13819 else
13820 phba->mds_diags_support = 0;
James Smartd2cc9bc2018-09-10 10:30:50 -070013821
James Smart0d8af092019-08-14 16:57:10 -070013822 /*
13823 * Check if the SLI port supports NSLER
13824 */
13825 if (bf_get(cfg_nsler, mbx_sli4_parameters))
13826 phba->nsler = 1;
13827 else
13828 phba->nsler = 0;
13829
James Smartfedd3b72011-02-16 12:39:24 -050013830 return 0;
13831}
13832
13833/**
James Smart3772a992009-05-22 14:50:54 -040013834 * lpfc_pci_probe_one_s3 - PCI probe func to reg SLI-3 device to PCI subsystem.
13835 * @pdev: pointer to PCI device
13836 * @pid: pointer to PCI device identifier
13837 *
13838 * This routine is to be called to attach a device with SLI-3 interface spec
13839 * to the PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
13840 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
13841 * information of the device and driver to see if the driver state that it can
13842 * support this kind of device. If the match is successful, the driver core
13843 * invokes this routine. If this routine determines it can claim the HBA, it
13844 * does all the initialization that it needs to do to handle the HBA properly.
13845 *
13846 * Return code
13847 * 0 - driver can claim the device
13848 * negative value - driver can not claim the device
13849 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080013850static int
James Smart3772a992009-05-22 14:50:54 -040013851lpfc_pci_probe_one_s3(struct pci_dev *pdev, const struct pci_device_id *pid)
13852{
13853 struct lpfc_hba *phba;
13854 struct lpfc_vport *vport = NULL;
James Smart6669f9b2009-10-02 15:16:45 -040013855 struct Scsi_Host *shost = NULL;
James Smart3772a992009-05-22 14:50:54 -040013856 int error;
13857 uint32_t cfg_mode, intr_mode;
13858
13859 /* Allocate memory for HBA structure */
13860 phba = lpfc_hba_alloc(pdev);
13861 if (!phba)
13862 return -ENOMEM;
13863
13864 /* Perform generic PCI device enabling operation */
13865 error = lpfc_enable_pci_dev(phba);
James Smart079b5c92011-08-21 21:48:49 -040013866 if (error)
James Smart3772a992009-05-22 14:50:54 -040013867 goto out_free_phba;
James Smart3772a992009-05-22 14:50:54 -040013868
13869 /* Set up SLI API function jump table for PCI-device group-0 HBAs */
13870 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_LP);
13871 if (error)
13872 goto out_disable_pci_dev;
13873
13874 /* Set up SLI-3 specific device PCI memory space */
13875 error = lpfc_sli_pci_mem_setup(phba);
13876 if (error) {
13877 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13878 "1402 Failed to set up pci memory space.\n");
13879 goto out_disable_pci_dev;
13880 }
13881
James Smart3772a992009-05-22 14:50:54 -040013882 /* Set up SLI-3 specific device driver resources */
13883 error = lpfc_sli_driver_resource_setup(phba);
13884 if (error) {
13885 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13886 "1404 Failed to set up driver resource.\n");
13887 goto out_unset_pci_mem_s3;
13888 }
13889
13890 /* Initialize and populate the iocb list per host */
James Smartd1f525a2017-04-21 16:04:55 -070013891
James Smart3772a992009-05-22 14:50:54 -040013892 error = lpfc_init_iocb_list(phba, LPFC_IOCB_LIST_CNT);
13893 if (error) {
13894 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13895 "1405 Failed to initialize iocb list.\n");
13896 goto out_unset_driver_resource_s3;
13897 }
13898
13899 /* Set up common device driver resources */
13900 error = lpfc_setup_driver_resource_phase2(phba);
13901 if (error) {
13902 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13903 "1406 Failed to set up driver resource.\n");
13904 goto out_free_iocb_list;
13905 }
13906
James Smart079b5c92011-08-21 21:48:49 -040013907 /* Get the default values for Model Name and Description */
13908 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
13909
James Smart3772a992009-05-22 14:50:54 -040013910 /* Create SCSI host to the physical port */
13911 error = lpfc_create_shost(phba);
13912 if (error) {
13913 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13914 "1407 Failed to create scsi host.\n");
13915 goto out_unset_driver_resource;
13916 }
13917
13918 /* Configure sysfs attributes */
13919 vport = phba->pport;
13920 error = lpfc_alloc_sysfs_attr(vport);
13921 if (error) {
13922 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13923 "1476 Failed to allocate sysfs attr\n");
13924 goto out_destroy_shost;
13925 }
13926
James Smart6669f9b2009-10-02 15:16:45 -040013927 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
James Smart3772a992009-05-22 14:50:54 -040013928 /* Now, trying to enable interrupt and bring up the device */
13929 cfg_mode = phba->cfg_use_msi;
13930 while (true) {
13931 /* Put device to a known state before enabling interrupt */
13932 lpfc_stop_port(phba);
13933 /* Configure and enable interrupt */
13934 intr_mode = lpfc_sli_enable_intr(phba, cfg_mode);
13935 if (intr_mode == LPFC_INTR_ERROR) {
Dick Kennedy372c1872020-06-30 14:50:00 -070013936 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart3772a992009-05-22 14:50:54 -040013937 "0431 Failed to enable interrupt.\n");
13938 error = -ENODEV;
13939 goto out_free_sysfs_attr;
13940 }
13941 /* SLI-3 HBA setup */
13942 if (lpfc_sli_hba_setup(phba)) {
Dick Kennedy372c1872020-06-30 14:50:00 -070013943 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart3772a992009-05-22 14:50:54 -040013944 "1477 Failed to set up hba\n");
13945 error = -ENODEV;
13946 goto out_remove_device;
13947 }
13948
13949 /* Wait 50ms for the interrupts of previous mailbox commands */
13950 msleep(50);
13951 /* Check active interrupts on message signaled interrupts */
13952 if (intr_mode == 0 ||
13953 phba->sli.slistat.sli_intr > LPFC_MSIX_VECTORS) {
13954 /* Log the current active interrupt mode */
13955 phba->intr_mode = intr_mode;
13956 lpfc_log_intr_mode(phba, intr_mode);
13957 break;
13958 } else {
13959 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
13960 "0447 Configure interrupt mode (%d) "
13961 "failed active interrupt test.\n",
13962 intr_mode);
13963 /* Disable the current interrupt mode */
13964 lpfc_sli_disable_intr(phba);
13965 /* Try next level of interrupt mode */
13966 cfg_mode = --intr_mode;
13967 }
13968 }
13969
13970 /* Perform post initialization setup */
13971 lpfc_post_init_setup(phba);
13972
13973 /* Check if there are static vports to be created. */
13974 lpfc_create_static_vport(phba);
13975
13976 return 0;
13977
13978out_remove_device:
13979 lpfc_unset_hba(phba);
13980out_free_sysfs_attr:
13981 lpfc_free_sysfs_attr(vport);
13982out_destroy_shost:
13983 lpfc_destroy_shost(phba);
13984out_unset_driver_resource:
13985 lpfc_unset_driver_resource_phase2(phba);
13986out_free_iocb_list:
13987 lpfc_free_iocb_list(phba);
13988out_unset_driver_resource_s3:
13989 lpfc_sli_driver_resource_unset(phba);
13990out_unset_pci_mem_s3:
13991 lpfc_sli_pci_mem_unset(phba);
13992out_disable_pci_dev:
13993 lpfc_disable_pci_dev(phba);
James Smart6669f9b2009-10-02 15:16:45 -040013994 if (shost)
13995 scsi_host_put(shost);
James Smart3772a992009-05-22 14:50:54 -040013996out_free_phba:
13997 lpfc_hba_free(phba);
13998 return error;
13999}
14000
14001/**
14002 * lpfc_pci_remove_one_s3 - PCI func to unreg SLI-3 device from PCI subsystem.
James Smarte59058c2008-08-24 21:49:00 -040014003 * @pdev: pointer to PCI device
14004 *
James Smart3772a992009-05-22 14:50:54 -040014005 * This routine is to be called to disattach a device with SLI-3 interface
14006 * spec from PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
14007 * removed from PCI bus, it performs all the necessary cleanup for the HBA
14008 * device to be removed from the PCI subsystem properly.
James Smarte59058c2008-08-24 21:49:00 -040014009 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080014010static void
James Smart3772a992009-05-22 14:50:54 -040014011lpfc_pci_remove_one_s3(struct pci_dev *pdev)
dea31012005-04-17 16:05:31 -050014012{
James Smart2e0fef82007-06-17 19:56:36 -050014013 struct Scsi_Host *shost = pci_get_drvdata(pdev);
14014 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
James Smarteada2722008-12-04 22:39:13 -050014015 struct lpfc_vport **vports;
James Smart2e0fef82007-06-17 19:56:36 -050014016 struct lpfc_hba *phba = vport->phba;
James Smarteada2722008-12-04 22:39:13 -050014017 int i;
Tomohiro Kusumi8a4df1202008-01-11 01:53:00 -050014018
James Smart549e55c2007-08-02 11:09:51 -040014019 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -040014020 vport->load_flag |= FC_UNLOADING;
James Smart549e55c2007-08-02 11:09:51 -040014021 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050014022
James Smart858c9f62007-06-17 19:56:39 -050014023 lpfc_free_sysfs_attr(vport);
14024
James Smarteada2722008-12-04 22:39:13 -050014025 /* Release all the vports against this physical port */
14026 vports = lpfc_create_vport_work_array(phba);
14027 if (vports != NULL)
James Smart587a37f2012-05-09 21:16:03 -040014028 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
14029 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
14030 continue;
James Smarteada2722008-12-04 22:39:13 -050014031 fc_vport_terminate(vports[i]->fc_vport);
James Smart587a37f2012-05-09 21:16:03 -040014032 }
James Smarteada2722008-12-04 22:39:13 -050014033 lpfc_destroy_vport_work_array(phba, vports);
14034
James Smart95f0ef82020-11-15 11:26:32 -080014035 /* Remove FC host with the physical port */
James Smart858c9f62007-06-17 19:56:39 -050014036 fc_remove_host(shost);
James Smarte9b11082020-11-15 11:26:33 -080014037 scsi_remove_host(shost);
James Smartd613b6a2017-02-12 13:52:37 -080014038
James Smart95f0ef82020-11-15 11:26:32 -080014039 /* Clean up all nodes, mailboxes and IOs. */
James Smart87af33f2007-10-27 13:37:43 -040014040 lpfc_cleanup(vport);
14041
James Smart2e0fef82007-06-17 19:56:36 -050014042 /*
14043 * Bring down the SLI Layer. This step disable all interrupts,
14044 * clears the rings, discards all mailbox commands, and resets
14045 * the HBA.
14046 */
James Smarta257bf92009-04-06 18:48:10 -040014047
Justin P. Mattock48e34d02010-12-30 15:07:58 -080014048 /* HBA interrupt will be disabled after this call */
James Smart2e0fef82007-06-17 19:56:36 -050014049 lpfc_sli_hba_down(phba);
James Smarta257bf92009-04-06 18:48:10 -040014050 /* Stop kthread signal shall trigger work_done one more time */
14051 kthread_stop(phba->worker_thread);
14052 /* Final cleanup of txcmplq and reset the HBA */
James Smart2e0fef82007-06-17 19:56:36 -050014053 lpfc_sli_brdrestart(phba);
14054
James Smart72859902012-01-18 16:25:38 -050014055 kfree(phba->vpi_bmask);
14056 kfree(phba->vpi_ids);
14057
James Smart3772a992009-05-22 14:50:54 -040014058 lpfc_stop_hba_timers(phba);
James Smart523128e2018-09-10 10:30:46 -070014059 spin_lock_irq(&phba->port_list_lock);
James Smart858c9f62007-06-17 19:56:39 -050014060 list_del_init(&vport->listentry);
James Smart523128e2018-09-10 10:30:46 -070014061 spin_unlock_irq(&phba->port_list_lock);
James Smart858c9f62007-06-17 19:56:39 -050014062
James Smart858c9f62007-06-17 19:56:39 -050014063 lpfc_debugfs_terminate(vport);
James Smart2e0fef82007-06-17 19:56:36 -050014064
James Smart912e3ac2011-05-24 11:42:11 -040014065 /* Disable SR-IOV if enabled */
14066 if (phba->cfg_sriov_nr_virtfn)
14067 pci_disable_sriov(pdev);
14068
James Smart5b75da22008-12-04 22:39:35 -050014069 /* Disable interrupt */
James Smart3772a992009-05-22 14:50:54 -040014070 lpfc_sli_disable_intr(phba);
dea31012005-04-17 16:05:31 -050014071
James Smart858c9f62007-06-17 19:56:39 -050014072 scsi_host_put(shost);
James Smart2e0fef82007-06-17 19:56:36 -050014073
14074 /*
14075 * Call scsi_free before mem_free since scsi bufs are released to their
14076 * corresponding pools here.
14077 */
14078 lpfc_scsi_free(phba);
James Smart0794d602019-01-28 11:14:19 -080014079 lpfc_free_iocb_list(phba);
14080
James Smart3772a992009-05-22 14:50:54 -040014081 lpfc_mem_free_all(phba);
James Smart2e0fef82007-06-17 19:56:36 -050014082
James Smart34b02dc2008-08-24 21:49:55 -040014083 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
14084 phba->hbqslimp.virt, phba->hbqslimp.phys);
James Smarted957682007-06-17 19:56:37 -050014085
James Smart2e0fef82007-06-17 19:56:36 -050014086 /* Free resources associated with SLI2 interface */
14087 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
James Smart34b02dc2008-08-24 21:49:55 -040014088 phba->slim2p.virt, phba->slim2p.phys);
James Smart2e0fef82007-06-17 19:56:36 -050014089
14090 /* unmap adapter SLIM and Control Registers */
14091 iounmap(phba->ctrl_regs_memmap_p);
14092 iounmap(phba->slim_memmap_p);
14093
James Smart3772a992009-05-22 14:50:54 -040014094 lpfc_hba_free(phba);
James Smart2e0fef82007-06-17 19:56:36 -050014095
Johannes Thumshirne0c04832016-06-07 09:44:03 +020014096 pci_release_mem_regions(pdev);
James Smart2e0fef82007-06-17 19:56:36 -050014097 pci_disable_device(pdev);
dea31012005-04-17 16:05:31 -050014098}
14099
Linas Vepstas8d63f372007-02-14 14:28:36 -060014100/**
James Smart3772a992009-05-22 14:50:54 -040014101 * lpfc_pci_suspend_one_s3 - PCI func to suspend SLI-3 device for power mgmnt
Vaibhav Guptaef6fa162020-11-02 22:17:18 +053014102 * @dev_d: pointer to device
James Smart3a55b532008-12-04 22:38:54 -050014103 *
James Smart3772a992009-05-22 14:50:54 -040014104 * This routine is to be called from the kernel's PCI subsystem to support
14105 * system Power Management (PM) to device with SLI-3 interface spec. When
14106 * PM invokes this method, it quiesces the device by stopping the driver's
14107 * worker thread for the device, turning off device's interrupt and DMA,
14108 * and bring the device offline. Note that as the driver implements the
14109 * minimum PM requirements to a power-aware driver's PM support for the
14110 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
14111 * to the suspend() method call will be treated as SUSPEND and the driver will
14112 * fully reinitialize its device during resume() method call, the driver will
14113 * set device to PCI_D3hot state in PCI config space instead of setting it
14114 * according to the @msg provided by the PM.
James Smart3a55b532008-12-04 22:38:54 -050014115 *
14116 * Return code
James Smart3772a992009-05-22 14:50:54 -040014117 * 0 - driver suspended the device
14118 * Error otherwise
James Smart3a55b532008-12-04 22:38:54 -050014119 **/
Vaibhav Guptaef6fa162020-11-02 22:17:18 +053014120static int __maybe_unused
14121lpfc_pci_suspend_one_s3(struct device *dev_d)
James Smart3a55b532008-12-04 22:38:54 -050014122{
Vaibhav Guptaef6fa162020-11-02 22:17:18 +053014123 struct Scsi_Host *shost = dev_get_drvdata(dev_d);
James Smart3a55b532008-12-04 22:38:54 -050014124 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
14125
14126 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
14127 "0473 PCI device Power Management suspend.\n");
14128
14129 /* Bring down the device */
James Smart618a5232012-06-12 13:54:36 -040014130 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart3a55b532008-12-04 22:38:54 -050014131 lpfc_offline(phba);
14132 kthread_stop(phba->worker_thread);
14133
14134 /* Disable interrupt from device */
James Smart3772a992009-05-22 14:50:54 -040014135 lpfc_sli_disable_intr(phba);
James Smart3a55b532008-12-04 22:38:54 -050014136
James Smart3a55b532008-12-04 22:38:54 -050014137 return 0;
14138}
14139
14140/**
James Smart3772a992009-05-22 14:50:54 -040014141 * lpfc_pci_resume_one_s3 - PCI func to resume SLI-3 device for power mgmnt
Vaibhav Guptaef6fa162020-11-02 22:17:18 +053014142 * @dev_d: pointer to device
James Smart3a55b532008-12-04 22:38:54 -050014143 *
James Smart3772a992009-05-22 14:50:54 -040014144 * This routine is to be called from the kernel's PCI subsystem to support
14145 * system Power Management (PM) to device with SLI-3 interface spec. When PM
14146 * invokes this method, it restores the device's PCI config space state and
14147 * fully reinitializes the device and brings it online. Note that as the
14148 * driver implements the minimum PM requirements to a power-aware driver's
14149 * PM for suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE,
14150 * FREEZE) to the suspend() method call will be treated as SUSPEND and the
14151 * driver will fully reinitialize its device during resume() method call,
14152 * the device will be set to PCI_D0 directly in PCI config space before
14153 * restoring the state.
James Smart3a55b532008-12-04 22:38:54 -050014154 *
14155 * Return code
James Smart3772a992009-05-22 14:50:54 -040014156 * 0 - driver suspended the device
14157 * Error otherwise
James Smart3a55b532008-12-04 22:38:54 -050014158 **/
Vaibhav Guptaef6fa162020-11-02 22:17:18 +053014159static int __maybe_unused
14160lpfc_pci_resume_one_s3(struct device *dev_d)
James Smart3a55b532008-12-04 22:38:54 -050014161{
Vaibhav Guptaef6fa162020-11-02 22:17:18 +053014162 struct Scsi_Host *shost = dev_get_drvdata(dev_d);
James Smart3a55b532008-12-04 22:38:54 -050014163 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
James Smart5b75da22008-12-04 22:39:35 -050014164 uint32_t intr_mode;
James Smart3a55b532008-12-04 22:38:54 -050014165 int error;
14166
14167 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
14168 "0452 PCI device Power Management resume.\n");
14169
James Smart3a55b532008-12-04 22:38:54 -050014170 /* Startup the kernel thread for this host adapter. */
14171 phba->worker_thread = kthread_run(lpfc_do_work, phba,
14172 "lpfc_worker_%d", phba->brd_no);
14173 if (IS_ERR(phba->worker_thread)) {
14174 error = PTR_ERR(phba->worker_thread);
14175 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14176 "0434 PM resume failed to start worker "
14177 "thread: error=x%x.\n", error);
14178 return error;
14179 }
14180
James Smart25ac2c972021-09-10 16:31:54 -070014181 /* Init cpu_map array */
14182 lpfc_cpu_map_array_init(phba);
14183 /* Init hba_eq_hdl array */
14184 lpfc_hba_eq_hdl_array_init(phba);
James Smart5b75da22008-12-04 22:39:35 -050014185 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -040014186 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -050014187 if (intr_mode == LPFC_INTR_ERROR) {
Dick Kennedy372c1872020-06-30 14:50:00 -070014188 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart5b75da22008-12-04 22:39:35 -050014189 "0430 PM resume Failed to enable interrupt\n");
14190 return -EIO;
14191 } else
14192 phba->intr_mode = intr_mode;
James Smart3a55b532008-12-04 22:38:54 -050014193
14194 /* Restart HBA and bring it online */
14195 lpfc_sli_brdrestart(phba);
14196 lpfc_online(phba);
14197
James Smart5b75da22008-12-04 22:39:35 -050014198 /* Log the current active interrupt mode */
14199 lpfc_log_intr_mode(phba, phba->intr_mode);
14200
James Smart3a55b532008-12-04 22:38:54 -050014201 return 0;
14202}
14203
14204/**
James Smart891478a2009-11-18 15:40:23 -050014205 * lpfc_sli_prep_dev_for_recover - Prepare SLI3 device for pci slot recover
14206 * @phba: pointer to lpfc hba data structure.
14207 *
14208 * This routine is called to prepare the SLI3 device for PCI slot recover. It
James Smarte2af0d22010-03-15 11:25:32 -040014209 * aborts all the outstanding SCSI I/Os to the pci device.
James Smart891478a2009-11-18 15:40:23 -050014210 **/
14211static void
14212lpfc_sli_prep_dev_for_recover(struct lpfc_hba *phba)
14213{
Dick Kennedy372c1872020-06-30 14:50:00 -070014214 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart891478a2009-11-18 15:40:23 -050014215 "2723 PCI channel I/O abort preparing for recovery\n");
James Smarte2af0d22010-03-15 11:25:32 -040014216
14217 /*
14218 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
14219 * and let the SCSI mid-layer to retry them to recover.
14220 */
James Smartdb55fba2014-04-04 13:52:02 -040014221 lpfc_sli_abort_fcp_rings(phba);
James Smart891478a2009-11-18 15:40:23 -050014222}
14223
14224/**
James Smart0d878412009-10-02 15:16:56 -040014225 * lpfc_sli_prep_dev_for_reset - Prepare SLI3 device for pci slot reset
14226 * @phba: pointer to lpfc hba data structure.
14227 *
14228 * This routine is called to prepare the SLI3 device for PCI slot reset. It
14229 * disables the device interrupt and pci device, and aborts the internal FCP
14230 * pending I/Os.
14231 **/
14232static void
14233lpfc_sli_prep_dev_for_reset(struct lpfc_hba *phba)
14234{
Dick Kennedy372c1872020-06-30 14:50:00 -070014235 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart891478a2009-11-18 15:40:23 -050014236 "2710 PCI channel disable preparing for reset\n");
James Smarte2af0d22010-03-15 11:25:32 -040014237
James Smart75baf692010-06-08 18:31:21 -040014238 /* Block any management I/Os to the device */
James Smart618a5232012-06-12 13:54:36 -040014239 lpfc_block_mgmt_io(phba, LPFC_MBX_WAIT);
James Smart75baf692010-06-08 18:31:21 -040014240
James Smarte2af0d22010-03-15 11:25:32 -040014241 /* Block all SCSI devices' I/Os on the host */
14242 lpfc_scsi_dev_block(phba);
14243
James Smartea714f32013-04-17 20:18:47 -040014244 /* Flush all driver's outstanding SCSI I/Os as we are to reset */
James Smartc00f62e2019-08-14 16:57:11 -070014245 lpfc_sli_flush_io_rings(phba);
James Smartea714f32013-04-17 20:18:47 -040014246
James Smarte2af0d22010-03-15 11:25:32 -040014247 /* stop all timers */
14248 lpfc_stop_hba_timers(phba);
14249
James Smart0d878412009-10-02 15:16:56 -040014250 /* Disable interrupt and pci device */
14251 lpfc_sli_disable_intr(phba);
14252 pci_disable_device(phba->pcidev);
James Smart0d878412009-10-02 15:16:56 -040014253}
14254
14255/**
14256 * lpfc_sli_prep_dev_for_perm_failure - Prepare SLI3 dev for pci slot disable
14257 * @phba: pointer to lpfc hba data structure.
14258 *
14259 * This routine is called to prepare the SLI3 device for PCI slot permanently
14260 * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
14261 * pending I/Os.
14262 **/
14263static void
James Smart75baf692010-06-08 18:31:21 -040014264lpfc_sli_prep_dev_for_perm_failure(struct lpfc_hba *phba)
James Smart0d878412009-10-02 15:16:56 -040014265{
Dick Kennedy372c1872020-06-30 14:50:00 -070014266 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart891478a2009-11-18 15:40:23 -050014267 "2711 PCI channel permanent disable for failure\n");
James Smarte2af0d22010-03-15 11:25:32 -040014268 /* Block all SCSI devices' I/Os on the host */
14269 lpfc_scsi_dev_block(phba);
14270
14271 /* stop all timers */
14272 lpfc_stop_hba_timers(phba);
14273
James Smart0d878412009-10-02 15:16:56 -040014274 /* Clean up all driver's outstanding SCSI I/Os */
James Smartc00f62e2019-08-14 16:57:11 -070014275 lpfc_sli_flush_io_rings(phba);
James Smart0d878412009-10-02 15:16:56 -040014276}
14277
14278/**
James Smart3772a992009-05-22 14:50:54 -040014279 * lpfc_io_error_detected_s3 - Method for handling SLI-3 device PCI I/O error
James Smarte59058c2008-08-24 21:49:00 -040014280 * @pdev: pointer to PCI device.
14281 * @state: the current PCI connection state.
Linas Vepstas8d63f372007-02-14 14:28:36 -060014282 *
James Smart3772a992009-05-22 14:50:54 -040014283 * This routine is called from the PCI subsystem for I/O error handling to
14284 * device with SLI-3 interface spec. This function is called by the PCI
14285 * subsystem after a PCI bus error affecting this device has been detected.
14286 * When this function is invoked, it will need to stop all the I/Os and
14287 * interrupt(s) to the device. Once that is done, it will return
14288 * PCI_ERS_RESULT_NEED_RESET for the PCI subsystem to perform proper recovery
14289 * as desired.
James Smarte59058c2008-08-24 21:49:00 -040014290 *
14291 * Return codes
James Smart0d878412009-10-02 15:16:56 -040014292 * PCI_ERS_RESULT_CAN_RECOVER - can be recovered with reset_link
James Smart3772a992009-05-22 14:50:54 -040014293 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
14294 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
James Smarte59058c2008-08-24 21:49:00 -040014295 **/
James Smart3772a992009-05-22 14:50:54 -040014296static pci_ers_result_t
14297lpfc_io_error_detected_s3(struct pci_dev *pdev, pci_channel_state_t state)
Linas Vepstas8d63f372007-02-14 14:28:36 -060014298{
James Smart51ef4c22007-08-02 11:10:31 -040014299 struct Scsi_Host *shost = pci_get_drvdata(pdev);
14300 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -060014301
James Smart0d878412009-10-02 15:16:56 -040014302 switch (state) {
14303 case pci_channel_io_normal:
James Smart891478a2009-11-18 15:40:23 -050014304 /* Non-fatal error, prepare for recovery */
14305 lpfc_sli_prep_dev_for_recover(phba);
James Smart0d878412009-10-02 15:16:56 -040014306 return PCI_ERS_RESULT_CAN_RECOVER;
14307 case pci_channel_io_frozen:
14308 /* Fatal error, prepare for slot reset */
14309 lpfc_sli_prep_dev_for_reset(phba);
14310 return PCI_ERS_RESULT_NEED_RESET;
14311 case pci_channel_io_perm_failure:
14312 /* Permanent failure, prepare for device down */
James Smart75baf692010-06-08 18:31:21 -040014313 lpfc_sli_prep_dev_for_perm_failure(phba);
Linas Vepstas8d63f372007-02-14 14:28:36 -060014314 return PCI_ERS_RESULT_DISCONNECT;
James Smart0d878412009-10-02 15:16:56 -040014315 default:
14316 /* Unknown state, prepare and request slot reset */
Dick Kennedy372c1872020-06-30 14:50:00 -070014317 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart0d878412009-10-02 15:16:56 -040014318 "0472 Unknown PCI error state: x%x\n", state);
14319 lpfc_sli_prep_dev_for_reset(phba);
14320 return PCI_ERS_RESULT_NEED_RESET;
James Smarta8e497d2008-08-24 21:50:11 -040014321 }
Linas Vepstas8d63f372007-02-14 14:28:36 -060014322}
14323
14324/**
James Smart3772a992009-05-22 14:50:54 -040014325 * lpfc_io_slot_reset_s3 - Method for restarting PCI SLI-3 device from scratch.
James Smarte59058c2008-08-24 21:49:00 -040014326 * @pdev: pointer to PCI device.
Linas Vepstas8d63f372007-02-14 14:28:36 -060014327 *
James Smart3772a992009-05-22 14:50:54 -040014328 * This routine is called from the PCI subsystem for error handling to
14329 * device with SLI-3 interface spec. This is called after PCI bus has been
14330 * reset to restart the PCI card from scratch, as if from a cold-boot.
14331 * During the PCI subsystem error recovery, after driver returns
14332 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
14333 * recovery and then call this routine before calling the .resume method
14334 * to recover the device. This function will initialize the HBA device,
14335 * enable the interrupt, but it will just put the HBA to offline state
14336 * without passing any I/O traffic.
James Smarte59058c2008-08-24 21:49:00 -040014337 *
14338 * Return codes
James Smart3772a992009-05-22 14:50:54 -040014339 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
14340 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
Linas Vepstas8d63f372007-02-14 14:28:36 -060014341 */
James Smart3772a992009-05-22 14:50:54 -040014342static pci_ers_result_t
14343lpfc_io_slot_reset_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -060014344{
James Smart51ef4c22007-08-02 11:10:31 -040014345 struct Scsi_Host *shost = pci_get_drvdata(pdev);
14346 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -060014347 struct lpfc_sli *psli = &phba->sli;
James Smart5b75da22008-12-04 22:39:35 -050014348 uint32_t intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -060014349
14350 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
Benjamin Herrenschmidt09483912007-12-20 15:28:09 +110014351 if (pci_enable_device_mem(pdev)) {
Linas Vepstas8d63f372007-02-14 14:28:36 -060014352 printk(KERN_ERR "lpfc: Cannot re-enable "
14353 "PCI device after reset.\n");
14354 return PCI_ERS_RESULT_DISCONNECT;
14355 }
14356
James Smart97207482008-12-04 22:39:19 -050014357 pci_restore_state(pdev);
James Smart1dfb5a42010-02-12 14:40:50 -050014358
14359 /*
14360 * As the new kernel behavior of pci_restore_state() API call clears
14361 * device saved_state flag, need to save the restored state again.
14362 */
14363 pci_save_state(pdev);
14364
James Smart97207482008-12-04 22:39:19 -050014365 if (pdev->is_busmaster)
14366 pci_set_master(pdev);
Linas Vepstas8d63f372007-02-14 14:28:36 -060014367
James Smart92d7f7b2007-06-17 19:56:38 -050014368 spin_lock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040014369 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -050014370 spin_unlock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -060014371
James Smart5b75da22008-12-04 22:39:35 -050014372 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -040014373 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -050014374 if (intr_mode == LPFC_INTR_ERROR) {
Dick Kennedy372c1872020-06-30 14:50:00 -070014375 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart5b75da22008-12-04 22:39:35 -050014376 "0427 Cannot re-enable interrupt after "
14377 "slot reset.\n");
14378 return PCI_ERS_RESULT_DISCONNECT;
14379 } else
14380 phba->intr_mode = intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -060014381
James Smart75baf692010-06-08 18:31:21 -040014382 /* Take device offline, it will perform cleanup */
James Smart618a5232012-06-12 13:54:36 -040014383 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
Linas Vepstas8d63f372007-02-14 14:28:36 -060014384 lpfc_offline(phba);
14385 lpfc_sli_brdrestart(phba);
14386
James Smart5b75da22008-12-04 22:39:35 -050014387 /* Log the current active interrupt mode */
14388 lpfc_log_intr_mode(phba, phba->intr_mode);
14389
Linas Vepstas8d63f372007-02-14 14:28:36 -060014390 return PCI_ERS_RESULT_RECOVERED;
14391}
14392
14393/**
James Smart3772a992009-05-22 14:50:54 -040014394 * lpfc_io_resume_s3 - Method for resuming PCI I/O operation on SLI-3 device.
James Smarte59058c2008-08-24 21:49:00 -040014395 * @pdev: pointer to PCI device
Linas Vepstas8d63f372007-02-14 14:28:36 -060014396 *
James Smart3772a992009-05-22 14:50:54 -040014397 * This routine is called from the PCI subsystem for error handling to device
14398 * with SLI-3 interface spec. It is called when kernel error recovery tells
14399 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
14400 * error recovery. After this call, traffic can start to flow from this device
14401 * again.
Linas Vepstas8d63f372007-02-14 14:28:36 -060014402 */
James Smart3772a992009-05-22 14:50:54 -040014403static void
14404lpfc_io_resume_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -060014405{
James Smart51ef4c22007-08-02 11:10:31 -040014406 struct Scsi_Host *shost = pci_get_drvdata(pdev);
14407 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -060014408
James Smarte2af0d22010-03-15 11:25:32 -040014409 /* Bring device online, it will be no-op for non-fatal error resume */
James Smart58da1ff2008-04-07 10:15:56 -040014410 lpfc_online(phba);
Linas Vepstas8d63f372007-02-14 14:28:36 -060014411}
14412
James Smart3772a992009-05-22 14:50:54 -040014413/**
James Smartda0436e2009-05-22 14:51:39 -040014414 * lpfc_sli4_get_els_iocb_cnt - Calculate the # of ELS IOCBs to reserve
14415 * @phba: pointer to lpfc hba data structure.
14416 *
14417 * returns the number of ELS/CT IOCBs to reserve
14418 **/
14419int
14420lpfc_sli4_get_els_iocb_cnt(struct lpfc_hba *phba)
14421{
14422 int max_xri = phba->sli4_hba.max_cfg_param.max_xri;
14423
James Smartf1126682009-06-10 17:22:44 -040014424 if (phba->sli_rev == LPFC_SLI_REV4) {
14425 if (max_xri <= 100)
James Smart6a9c52c2009-10-02 15:16:51 -040014426 return 10;
James Smartf1126682009-06-10 17:22:44 -040014427 else if (max_xri <= 256)
James Smart6a9c52c2009-10-02 15:16:51 -040014428 return 25;
James Smartf1126682009-06-10 17:22:44 -040014429 else if (max_xri <= 512)
James Smart6a9c52c2009-10-02 15:16:51 -040014430 return 50;
James Smartf1126682009-06-10 17:22:44 -040014431 else if (max_xri <= 1024)
James Smart6a9c52c2009-10-02 15:16:51 -040014432 return 100;
James Smart8a9d2e82012-05-09 21:16:12 -040014433 else if (max_xri <= 1536)
James Smart6a9c52c2009-10-02 15:16:51 -040014434 return 150;
James Smart8a9d2e82012-05-09 21:16:12 -040014435 else if (max_xri <= 2048)
14436 return 200;
14437 else
14438 return 250;
James Smartf1126682009-06-10 17:22:44 -040014439 } else
14440 return 0;
James Smartda0436e2009-05-22 14:51:39 -040014441}
14442
14443/**
James Smart895427b2017-02-12 13:52:30 -080014444 * lpfc_sli4_get_iocb_cnt - Calculate the # of total IOCBs to reserve
14445 * @phba: pointer to lpfc hba data structure.
14446 *
James Smartf358dd02017-02-12 13:52:34 -080014447 * returns the number of ELS/CT + NVMET IOCBs to reserve
James Smart895427b2017-02-12 13:52:30 -080014448 **/
14449int
14450lpfc_sli4_get_iocb_cnt(struct lpfc_hba *phba)
14451{
14452 int max_xri = lpfc_sli4_get_els_iocb_cnt(phba);
14453
James Smartf358dd02017-02-12 13:52:34 -080014454 if (phba->nvmet_support)
14455 max_xri += LPFC_NVMET_BUF_POST;
James Smart895427b2017-02-12 13:52:30 -080014456 return max_xri;
14457}
14458
14459
James Smart0a5ce732019-10-18 14:18:18 -070014460static int
James Smart1feb8202018-02-22 08:18:47 -080014461lpfc_log_write_firmware_error(struct lpfc_hba *phba, uint32_t offset,
14462 uint32_t magic_number, uint32_t ftype, uint32_t fid, uint32_t fsize,
14463 const struct firmware *fw)
14464{
James Smart0a5ce732019-10-18 14:18:18 -070014465 int rc;
James Smartf6c5e6c2021-07-22 15:17:18 -070014466 u8 sli_family;
James Smart1feb8202018-02-22 08:18:47 -080014467
James Smartf6c5e6c2021-07-22 15:17:18 -070014468 sli_family = bf_get(lpfc_sli_intf_sli_family, &phba->sli4_hba.sli_intf);
James Smart0a5ce732019-10-18 14:18:18 -070014469 /* Three cases: (1) FW was not supported on the detected adapter.
14470 * (2) FW update has been locked out administratively.
14471 * (3) Some other error during FW update.
14472 * In each case, an unmaskable message is written to the console
14473 * for admin diagnosis.
14474 */
14475 if (offset == ADD_STATUS_FW_NOT_SUPPORTED ||
James Smartf6c5e6c2021-07-22 15:17:18 -070014476 (sli_family == LPFC_SLI_INTF_FAMILY_G6 &&
James Smart5792a0e2019-10-25 11:43:42 -070014477 magic_number != MAGIC_NUMBER_G6) ||
James Smartf6c5e6c2021-07-22 15:17:18 -070014478 (sli_family == LPFC_SLI_INTF_FAMILY_G7 &&
14479 magic_number != MAGIC_NUMBER_G7) ||
14480 (sli_family == LPFC_SLI_INTF_FAMILY_G7P &&
14481 magic_number != MAGIC_NUMBER_G7P)) {
Dick Kennedy372c1872020-06-30 14:50:00 -070014482 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart0a5ce732019-10-18 14:18:18 -070014483 "3030 This firmware version is not supported on"
14484 " this HBA model. Device:%x Magic:%x Type:%x "
14485 "ID:%x Size %d %zd\n",
14486 phba->pcidev->device, magic_number, ftype, fid,
14487 fsize, fw->size);
14488 rc = -EINVAL;
14489 } else if (offset == ADD_STATUS_FW_DOWNLOAD_HW_DISABLED) {
Dick Kennedy372c1872020-06-30 14:50:00 -070014490 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart0a5ce732019-10-18 14:18:18 -070014491 "3021 Firmware downloads have been prohibited "
14492 "by a system configuration setting on "
14493 "Device:%x Magic:%x Type:%x ID:%x Size %d "
14494 "%zd\n",
14495 phba->pcidev->device, magic_number, ftype, fid,
14496 fsize, fw->size);
14497 rc = -EACCES;
14498 } else {
Dick Kennedy372c1872020-06-30 14:50:00 -070014499 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart0a5ce732019-10-18 14:18:18 -070014500 "3022 FW Download failed. Add Status x%x "
14501 "Device:%x Magic:%x Type:%x ID:%x Size %d "
14502 "%zd\n",
14503 offset, phba->pcidev->device, magic_number,
14504 ftype, fid, fsize, fw->size);
14505 rc = -EIO;
14506 }
14507 return rc;
James Smart1feb8202018-02-22 08:18:47 -080014508}
James Smart1feb8202018-02-22 08:18:47 -080014509
James Smart895427b2017-02-12 13:52:30 -080014510/**
James Smart52d52442011-05-24 11:42:45 -040014511 * lpfc_write_firmware - attempt to write a firmware image to the port
James Smart52d52442011-05-24 11:42:45 -040014512 * @fw: pointer to firmware image returned from request_firmware.
James Smart0a5ce732019-10-18 14:18:18 -070014513 * @context: pointer to firmware image returned from request_firmware.
James Smart52d52442011-05-24 11:42:45 -040014514 *
James Smart52d52442011-05-24 11:42:45 -040014515 **/
James Smartce396282012-09-29 11:30:56 -040014516static void
14517lpfc_write_firmware(const struct firmware *fw, void *context)
James Smart52d52442011-05-24 11:42:45 -040014518{
James Smartce396282012-09-29 11:30:56 -040014519 struct lpfc_hba *phba = (struct lpfc_hba *)context;
James Smart6b5151f2012-01-18 16:24:06 -050014520 char fwrev[FW_REV_STR_SIZE];
James Smartce396282012-09-29 11:30:56 -040014521 struct lpfc_grp_hdr *image;
James Smart52d52442011-05-24 11:42:45 -040014522 struct list_head dma_buffer_list;
14523 int i, rc = 0;
14524 struct lpfc_dmabuf *dmabuf, *next;
14525 uint32_t offset = 0, temp_offset = 0;
James Smart6b6ef5d2016-10-13 15:06:17 -070014526 uint32_t magic_number, ftype, fid, fsize;
James Smart52d52442011-05-24 11:42:45 -040014527
James Smartc71ab862012-10-31 14:44:33 -040014528 /* It can be null in no-wait mode, sanity check */
James Smartce396282012-09-29 11:30:56 -040014529 if (!fw) {
14530 rc = -ENXIO;
14531 goto out;
14532 }
14533 image = (struct lpfc_grp_hdr *)fw->data;
14534
James Smart6b6ef5d2016-10-13 15:06:17 -070014535 magic_number = be32_to_cpu(image->magic_number);
14536 ftype = bf_get_be32(lpfc_grp_hdr_file_type, image);
James Smart1feb8202018-02-22 08:18:47 -080014537 fid = bf_get_be32(lpfc_grp_hdr_id, image);
James Smart6b6ef5d2016-10-13 15:06:17 -070014538 fsize = be32_to_cpu(image->size);
14539
James Smart52d52442011-05-24 11:42:45 -040014540 INIT_LIST_HEAD(&dma_buffer_list);
James Smart52d52442011-05-24 11:42:45 -040014541 lpfc_decode_firmware_rev(phba, fwrev, 1);
James Smart88a2cfb2011-07-22 18:36:33 -040014542 if (strncmp(fwrev, image->revision, strnlen(image->revision, 16))) {
Dick Kennedy372c1872020-06-30 14:50:00 -070014543 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartce396282012-09-29 11:30:56 -040014544 "3023 Updating Firmware, Current Version:%s "
James Smart52d52442011-05-24 11:42:45 -040014545 "New Version:%s\n",
James Smart88a2cfb2011-07-22 18:36:33 -040014546 fwrev, image->revision);
James Smart52d52442011-05-24 11:42:45 -040014547 for (i = 0; i < LPFC_MBX_WR_CONFIG_MAX_BDE; i++) {
14548 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf),
14549 GFP_KERNEL);
14550 if (!dmabuf) {
14551 rc = -ENOMEM;
James Smartce396282012-09-29 11:30:56 -040014552 goto release_out;
James Smart52d52442011-05-24 11:42:45 -040014553 }
14554 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
14555 SLI4_PAGE_SIZE,
14556 &dmabuf->phys,
14557 GFP_KERNEL);
14558 if (!dmabuf->virt) {
14559 kfree(dmabuf);
14560 rc = -ENOMEM;
James Smartce396282012-09-29 11:30:56 -040014561 goto release_out;
James Smart52d52442011-05-24 11:42:45 -040014562 }
14563 list_add_tail(&dmabuf->list, &dma_buffer_list);
14564 }
14565 while (offset < fw->size) {
14566 temp_offset = offset;
14567 list_for_each_entry(dmabuf, &dma_buffer_list, list) {
James Smart079b5c92011-08-21 21:48:49 -040014568 if (temp_offset + SLI4_PAGE_SIZE > fw->size) {
James Smart52d52442011-05-24 11:42:45 -040014569 memcpy(dmabuf->virt,
14570 fw->data + temp_offset,
James Smart079b5c92011-08-21 21:48:49 -040014571 fw->size - temp_offset);
14572 temp_offset = fw->size;
James Smart52d52442011-05-24 11:42:45 -040014573 break;
14574 }
James Smart52d52442011-05-24 11:42:45 -040014575 memcpy(dmabuf->virt, fw->data + temp_offset,
14576 SLI4_PAGE_SIZE);
James Smart88a2cfb2011-07-22 18:36:33 -040014577 temp_offset += SLI4_PAGE_SIZE;
James Smart52d52442011-05-24 11:42:45 -040014578 }
14579 rc = lpfc_wr_object(phba, &dma_buffer_list,
14580 (fw->size - offset), &offset);
James Smart1feb8202018-02-22 08:18:47 -080014581 if (rc) {
James Smart0a5ce732019-10-18 14:18:18 -070014582 rc = lpfc_log_write_firmware_error(phba, offset,
14583 magic_number,
14584 ftype,
14585 fid,
14586 fsize,
14587 fw);
James Smartce396282012-09-29 11:30:56 -040014588 goto release_out;
James Smart1feb8202018-02-22 08:18:47 -080014589 }
James Smart52d52442011-05-24 11:42:45 -040014590 }
14591 rc = offset;
James Smart1feb8202018-02-22 08:18:47 -080014592 } else
Dick Kennedy372c1872020-06-30 14:50:00 -070014593 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart1feb8202018-02-22 08:18:47 -080014594 "3029 Skipped Firmware update, Current "
14595 "Version:%s New Version:%s\n",
14596 fwrev, image->revision);
James Smartce396282012-09-29 11:30:56 -040014597
14598release_out:
James Smart52d52442011-05-24 11:42:45 -040014599 list_for_each_entry_safe(dmabuf, next, &dma_buffer_list, list) {
14600 list_del(&dmabuf->list);
14601 dma_free_coherent(&phba->pcidev->dev, SLI4_PAGE_SIZE,
14602 dmabuf->virt, dmabuf->phys);
14603 kfree(dmabuf);
14604 }
James Smartce396282012-09-29 11:30:56 -040014605 release_firmware(fw);
14606out:
James Smart0a5ce732019-10-18 14:18:18 -070014607 if (rc < 0)
Dick Kennedy372c1872020-06-30 14:50:00 -070014608 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart0a5ce732019-10-18 14:18:18 -070014609 "3062 Firmware update error, status %d.\n", rc);
14610 else
Dick Kennedy372c1872020-06-30 14:50:00 -070014611 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart0a5ce732019-10-18 14:18:18 -070014612 "3024 Firmware update success: size %d.\n", rc);
James Smart52d52442011-05-24 11:42:45 -040014613}
14614
14615/**
James Smartc71ab862012-10-31 14:44:33 -040014616 * lpfc_sli4_request_firmware_update - Request linux generic firmware upgrade
14617 * @phba: pointer to lpfc hba data structure.
Lee Jonesfe614ac2020-07-23 13:24:22 +010014618 * @fw_upgrade: which firmware to update.
James Smartc71ab862012-10-31 14:44:33 -040014619 *
14620 * This routine is called to perform Linux generic firmware upgrade on device
14621 * that supports such feature.
14622 **/
14623int
14624lpfc_sli4_request_firmware_update(struct lpfc_hba *phba, uint8_t fw_upgrade)
14625{
14626 uint8_t file_name[ELX_MODEL_NAME_SIZE];
14627 int ret;
14628 const struct firmware *fw;
14629
14630 /* Only supported on SLI4 interface type 2 for now */
James Smart27d6ac02018-02-22 08:18:42 -080014631 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) <
James Smartc71ab862012-10-31 14:44:33 -040014632 LPFC_SLI_INTF_IF_TYPE_2)
14633 return -EPERM;
14634
14635 snprintf(file_name, ELX_MODEL_NAME_SIZE, "%s.grp", phba->ModelName);
14636
14637 if (fw_upgrade == INT_FW_UPGRADE) {
Shawn Guo0733d832021-04-25 10:00:24 +080014638 ret = request_firmware_nowait(THIS_MODULE, FW_ACTION_UEVENT,
James Smartc71ab862012-10-31 14:44:33 -040014639 file_name, &phba->pcidev->dev,
14640 GFP_KERNEL, (void *)phba,
14641 lpfc_write_firmware);
14642 } else if (fw_upgrade == RUN_FW_UPGRADE) {
14643 ret = request_firmware(&fw, file_name, &phba->pcidev->dev);
14644 if (!ret)
14645 lpfc_write_firmware(fw, (void *)phba);
14646 } else {
14647 ret = -EINVAL;
14648 }
14649
14650 return ret;
14651}
14652
14653/**
James Smartda0436e2009-05-22 14:51:39 -040014654 * lpfc_pci_probe_one_s4 - PCI probe func to reg SLI-4 device to PCI subsys
14655 * @pdev: pointer to PCI device
14656 * @pid: pointer to PCI device identifier
14657 *
14658 * This routine is called from the kernel's PCI subsystem to device with
14659 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
14660 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
14661 * information of the device and driver to see if the driver state that it
14662 * can support this kind of device. If the match is successful, the driver
14663 * core invokes this routine. If this routine determines it can claim the HBA,
14664 * it does all the initialization that it needs to do to handle the HBA
14665 * properly.
14666 *
14667 * Return code
14668 * 0 - driver can claim the device
14669 * negative value - driver can not claim the device
14670 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080014671static int
James Smartda0436e2009-05-22 14:51:39 -040014672lpfc_pci_probe_one_s4(struct pci_dev *pdev, const struct pci_device_id *pid)
14673{
14674 struct lpfc_hba *phba;
14675 struct lpfc_vport *vport = NULL;
James Smart6669f9b2009-10-02 15:16:45 -040014676 struct Scsi_Host *shost = NULL;
James Smart6c621a22017-05-15 15:20:45 -070014677 int error;
James Smartda0436e2009-05-22 14:51:39 -040014678 uint32_t cfg_mode, intr_mode;
James Smartda0436e2009-05-22 14:51:39 -040014679
14680 /* Allocate memory for HBA structure */
14681 phba = lpfc_hba_alloc(pdev);
14682 if (!phba)
14683 return -ENOMEM;
14684
Ewan D. Milne9977d882021-08-09 11:09:47 -040014685 INIT_LIST_HEAD(&phba->poll_list);
14686
James Smartda0436e2009-05-22 14:51:39 -040014687 /* Perform generic PCI device enabling operation */
14688 error = lpfc_enable_pci_dev(phba);
James Smart079b5c92011-08-21 21:48:49 -040014689 if (error)
James Smartda0436e2009-05-22 14:51:39 -040014690 goto out_free_phba;
James Smartda0436e2009-05-22 14:51:39 -040014691
14692 /* Set up SLI API function jump table for PCI-device group-1 HBAs */
14693 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_OC);
14694 if (error)
14695 goto out_disable_pci_dev;
14696
14697 /* Set up SLI-4 specific device PCI memory space */
14698 error = lpfc_sli4_pci_mem_setup(phba);
14699 if (error) {
14700 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14701 "1410 Failed to set up pci memory space.\n");
14702 goto out_disable_pci_dev;
14703 }
14704
James Smartda0436e2009-05-22 14:51:39 -040014705 /* Set up SLI-4 Specific device driver resources */
14706 error = lpfc_sli4_driver_resource_setup(phba);
14707 if (error) {
14708 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14709 "1412 Failed to set up driver resource.\n");
14710 goto out_unset_pci_mem_s4;
14711 }
14712
James Smart19ca7602010-11-20 23:11:55 -050014713 INIT_LIST_HEAD(&phba->active_rrq_list);
James Smart7d791df2011-07-22 18:37:52 -040014714 INIT_LIST_HEAD(&phba->fcf.fcf_pri_list);
James Smart19ca7602010-11-20 23:11:55 -050014715
James Smartda0436e2009-05-22 14:51:39 -040014716 /* Set up common device driver resources */
14717 error = lpfc_setup_driver_resource_phase2(phba);
14718 if (error) {
14719 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14720 "1414 Failed to set up driver resource.\n");
James Smart6c621a22017-05-15 15:20:45 -070014721 goto out_unset_driver_resource_s4;
James Smartda0436e2009-05-22 14:51:39 -040014722 }
14723
James Smart079b5c92011-08-21 21:48:49 -040014724 /* Get the default values for Model Name and Description */
14725 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
14726
James Smartda0436e2009-05-22 14:51:39 -040014727 /* Now, trying to enable interrupt and bring up the device */
14728 cfg_mode = phba->cfg_use_msi;
James Smartda0436e2009-05-22 14:51:39 -040014729
James Smart7b15db32013-01-03 15:43:29 -050014730 /* Put device to a known state before enabling interrupt */
James Smartcdb42be2019-01-28 11:14:21 -080014731 phba->pport = NULL;
James Smart7b15db32013-01-03 15:43:29 -050014732 lpfc_stop_port(phba);
James Smart895427b2017-02-12 13:52:30 -080014733
James Smartdcaa2132019-11-04 16:57:06 -080014734 /* Init cpu_map array */
14735 lpfc_cpu_map_array_init(phba);
14736
14737 /* Init hba_eq_hdl array */
14738 lpfc_hba_eq_hdl_array_init(phba);
14739
James Smart7b15db32013-01-03 15:43:29 -050014740 /* Configure and enable interrupt */
14741 intr_mode = lpfc_sli4_enable_intr(phba, cfg_mode);
14742 if (intr_mode == LPFC_INTR_ERROR) {
Dick Kennedy372c1872020-06-30 14:50:00 -070014743 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart7b15db32013-01-03 15:43:29 -050014744 "0426 Failed to enable interrupt.\n");
14745 error = -ENODEV;
James Smartcdb42be2019-01-28 11:14:21 -080014746 goto out_unset_driver_resource;
James Smartda0436e2009-05-22 14:51:39 -040014747 }
James Smart7b15db32013-01-03 15:43:29 -050014748 /* Default to single EQ for non-MSI-X */
James Smart895427b2017-02-12 13:52:30 -080014749 if (phba->intr_type != MSIX) {
James Smart6a828b02019-01-28 11:14:31 -080014750 phba->cfg_irq_chann = 1;
James Smart2d7dbc42017-02-12 13:52:35 -080014751 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
James Smart2d7dbc42017-02-12 13:52:35 -080014752 if (phba->nvmet_support)
14753 phba->cfg_nvmet_mrq = 1;
14754 }
James Smartcdb42be2019-01-28 11:14:21 -080014755 }
James Smart6a828b02019-01-28 11:14:31 -080014756 lpfc_cpu_affinity_check(phba, phba->cfg_irq_chann);
James Smartcdb42be2019-01-28 11:14:21 -080014757
14758 /* Create SCSI host to the physical port */
14759 error = lpfc_create_shost(phba);
14760 if (error) {
14761 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14762 "1415 Failed to create scsi host.\n");
14763 goto out_disable_intr;
14764 }
14765 vport = phba->pport;
14766 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
14767
14768 /* Configure sysfs attributes */
14769 error = lpfc_alloc_sysfs_attr(vport);
14770 if (error) {
14771 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14772 "1416 Failed to allocate sysfs attr\n");
14773 goto out_destroy_shost;
James Smart895427b2017-02-12 13:52:30 -080014774 }
14775
James Smart7b15db32013-01-03 15:43:29 -050014776 /* Set up SLI-4 HBA */
14777 if (lpfc_sli4_hba_setup(phba)) {
Dick Kennedy372c1872020-06-30 14:50:00 -070014778 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart7b15db32013-01-03 15:43:29 -050014779 "1421 Failed to set up hba\n");
14780 error = -ENODEV;
James Smartcdb42be2019-01-28 11:14:21 -080014781 goto out_free_sysfs_attr;
James Smart7b15db32013-01-03 15:43:29 -050014782 }
14783
14784 /* Log the current active interrupt mode */
14785 phba->intr_mode = intr_mode;
14786 lpfc_log_intr_mode(phba, intr_mode);
James Smartda0436e2009-05-22 14:51:39 -040014787
14788 /* Perform post initialization setup */
14789 lpfc_post_init_setup(phba);
14790
James Smart01649562017-02-12 13:52:32 -080014791 /* NVME support in FW earlier in the driver load corrects the
14792 * FC4 type making a check for nvme_support unnecessary.
14793 */
James Smart0794d602019-01-28 11:14:19 -080014794 if (phba->nvmet_support == 0) {
14795 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
14796 /* Create NVME binding with nvme_fc_transport. This
14797 * ensures the vport is initialized. If the localport
14798 * create fails, it should not unload the driver to
14799 * support field issues.
14800 */
14801 error = lpfc_nvme_create_localport(vport);
14802 if (error) {
Dick Kennedy372c1872020-06-30 14:50:00 -070014803 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart0794d602019-01-28 11:14:19 -080014804 "6004 NVME registration "
14805 "failed, error x%x\n",
14806 error);
14807 }
James Smart01649562017-02-12 13:52:32 -080014808 }
14809 }
James Smart895427b2017-02-12 13:52:30 -080014810
James Smartc71ab862012-10-31 14:44:33 -040014811 /* check for firmware upgrade or downgrade */
14812 if (phba->cfg_request_firmware_upgrade)
Sebastian Herbsztdb6f1c22015-08-31 16:48:14 -040014813 lpfc_sli4_request_firmware_update(phba, INT_FW_UPGRADE);
James Smart52d52442011-05-24 11:42:45 -040014814
James Smart1c6834a2009-07-19 10:01:26 -040014815 /* Check if there are static vports to be created. */
14816 lpfc_create_static_vport(phba);
James Smartd2cc9bc2018-09-10 10:30:50 -070014817
14818 /* Enable RAS FW log support */
14819 lpfc_sli4_ras_setup(phba);
14820
James Smartf861f592020-03-22 11:12:54 -070014821 timer_setup(&phba->cpuhp_poll_timer, lpfc_sli4_poll_hbtimer, 0);
James Smart93a4d6f2019-11-04 16:57:05 -080014822 cpuhp_state_add_instance_nocalls(lpfc_cpuhp_state, &phba->cpuhp);
14823
James Smartda0436e2009-05-22 14:51:39 -040014824 return 0;
14825
James Smartda0436e2009-05-22 14:51:39 -040014826out_free_sysfs_attr:
14827 lpfc_free_sysfs_attr(vport);
14828out_destroy_shost:
14829 lpfc_destroy_shost(phba);
James Smartcdb42be2019-01-28 11:14:21 -080014830out_disable_intr:
14831 lpfc_sli4_disable_intr(phba);
James Smartda0436e2009-05-22 14:51:39 -040014832out_unset_driver_resource:
14833 lpfc_unset_driver_resource_phase2(phba);
James Smartda0436e2009-05-22 14:51:39 -040014834out_unset_driver_resource_s4:
14835 lpfc_sli4_driver_resource_unset(phba);
14836out_unset_pci_mem_s4:
14837 lpfc_sli4_pci_mem_unset(phba);
14838out_disable_pci_dev:
14839 lpfc_disable_pci_dev(phba);
James Smart6669f9b2009-10-02 15:16:45 -040014840 if (shost)
14841 scsi_host_put(shost);
James Smartda0436e2009-05-22 14:51:39 -040014842out_free_phba:
14843 lpfc_hba_free(phba);
14844 return error;
14845}
14846
14847/**
14848 * lpfc_pci_remove_one_s4 - PCI func to unreg SLI-4 device from PCI subsystem
14849 * @pdev: pointer to PCI device
14850 *
14851 * This routine is called from the kernel's PCI subsystem to device with
14852 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
14853 * removed from PCI bus, it performs all the necessary cleanup for the HBA
14854 * device to be removed from the PCI subsystem properly.
14855 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080014856static void
James Smartda0436e2009-05-22 14:51:39 -040014857lpfc_pci_remove_one_s4(struct pci_dev *pdev)
14858{
14859 struct Scsi_Host *shost = pci_get_drvdata(pdev);
14860 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
14861 struct lpfc_vport **vports;
14862 struct lpfc_hba *phba = vport->phba;
14863 int i;
14864
14865 /* Mark the device unloading flag */
14866 spin_lock_irq(&phba->hbalock);
14867 vport->load_flag |= FC_UNLOADING;
14868 spin_unlock_irq(&phba->hbalock);
James Smart02243832021-08-16 09:28:54 -070014869 if (phba->cgn_i)
14870 lpfc_unreg_congestion_buf(phba);
James Smartda0436e2009-05-22 14:51:39 -040014871
James Smartda0436e2009-05-22 14:51:39 -040014872 lpfc_free_sysfs_attr(vport);
14873
14874 /* Release all the vports against this physical port */
14875 vports = lpfc_create_vport_work_array(phba);
14876 if (vports != NULL)
James Smart587a37f2012-05-09 21:16:03 -040014877 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
14878 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
14879 continue;
James Smartda0436e2009-05-22 14:51:39 -040014880 fc_vport_terminate(vports[i]->fc_vport);
James Smart587a37f2012-05-09 21:16:03 -040014881 }
James Smartda0436e2009-05-22 14:51:39 -040014882 lpfc_destroy_vport_work_array(phba, vports);
14883
James Smart95f0ef82020-11-15 11:26:32 -080014884 /* Remove FC host with the physical port */
James Smartda0436e2009-05-22 14:51:39 -040014885 fc_remove_host(shost);
James Smarte9b11082020-11-15 11:26:33 -080014886 scsi_remove_host(shost);
James Smartda0436e2009-05-22 14:51:39 -040014887
James Smartda0436e2009-05-22 14:51:39 -040014888 /* Perform ndlp cleanup on the physical port. The nvme and nvmet
14889 * localports are destroyed after to cleanup all transport memory.
14890 */
14891 lpfc_cleanup(vport);
14892 lpfc_nvmet_destroy_targetport(phba);
14893 lpfc_nvme_destroy_localport(vport);
14894
James Smartc4908502019-01-28 11:14:28 -080014895 /* De-allocate multi-XRI pools */
14896 if (phba->cfg_xri_rebalancing)
14897 lpfc_destroy_multixri_pools(phba);
14898
James Smart281d6192018-01-30 15:58:47 -080014899 /*
14900 * Bring down the SLI Layer. This step disables all interrupts,
14901 * clears the rings, discards all mailbox commands, and resets
14902 * the HBA FCoE function.
14903 */
14904 lpfc_debugfs_terminate(vport);
James Smartda0436e2009-05-22 14:51:39 -040014905
Dick Kennedy19017622017-09-29 17:34:27 -070014906 lpfc_stop_hba_timers(phba);
James Smart523128e2018-09-10 10:30:46 -070014907 spin_lock_irq(&phba->port_list_lock);
James Smartda0436e2009-05-22 14:51:39 -040014908 list_del_init(&vport->listentry);
James Smart523128e2018-09-10 10:30:46 -070014909 spin_unlock_irq(&phba->port_list_lock);
James Smartda0436e2009-05-22 14:51:39 -040014910
James Smart3677a3a2010-09-29 11:19:14 -040014911 /* Perform scsi free before driver resource_unset since scsi
James Smartda0436e2009-05-22 14:51:39 -040014912 * buffers are released to their corresponding pools here.
14913 */
James Smart5e5b5112019-01-28 11:14:22 -080014914 lpfc_io_free(phba);
James Smart01649562017-02-12 13:52:32 -080014915 lpfc_free_iocb_list(phba);
James Smart5e5b5112019-01-28 11:14:22 -080014916 lpfc_sli4_hba_unset(phba);
James Smart67d12732012-08-03 12:36:13 -040014917
James Smart0cdb84e2018-04-09 14:24:26 -070014918 lpfc_unset_driver_resource_phase2(phba);
James Smartda0436e2009-05-22 14:51:39 -040014919 lpfc_sli4_driver_resource_unset(phba);
14920
14921 /* Unmap adapter Control and Doorbell registers */
14922 lpfc_sli4_pci_mem_unset(phba);
14923
14924 /* Release PCI resources and disable device's PCI function */
14925 scsi_host_put(shost);
14926 lpfc_disable_pci_dev(phba);
14927
14928 /* Finally, free the driver's device data structure */
14929 lpfc_hba_free(phba);
14930
14931 return;
14932}
14933
14934/**
14935 * lpfc_pci_suspend_one_s4 - PCI func to suspend SLI-4 device for power mgmnt
Vaibhav Guptaef6fa162020-11-02 22:17:18 +053014936 * @dev_d: pointer to device
James Smartda0436e2009-05-22 14:51:39 -040014937 *
14938 * This routine is called from the kernel's PCI subsystem to support system
14939 * Power Management (PM) to device with SLI-4 interface spec. When PM invokes
14940 * this method, it quiesces the device by stopping the driver's worker
14941 * thread for the device, turning off device's interrupt and DMA, and bring
14942 * the device offline. Note that as the driver implements the minimum PM
14943 * requirements to a power-aware driver's PM support for suspend/resume -- all
14944 * the possible PM messages (SUSPEND, HIBERNATE, FREEZE) to the suspend()
14945 * method call will be treated as SUSPEND and the driver will fully
14946 * reinitialize its device during resume() method call, the driver will set
14947 * device to PCI_D3hot state in PCI config space instead of setting it
14948 * according to the @msg provided by the PM.
14949 *
14950 * Return code
14951 * 0 - driver suspended the device
14952 * Error otherwise
14953 **/
Vaibhav Guptaef6fa162020-11-02 22:17:18 +053014954static int __maybe_unused
14955lpfc_pci_suspend_one_s4(struct device *dev_d)
James Smartda0436e2009-05-22 14:51:39 -040014956{
Vaibhav Guptaef6fa162020-11-02 22:17:18 +053014957 struct Scsi_Host *shost = dev_get_drvdata(dev_d);
James Smartda0436e2009-05-22 14:51:39 -040014958 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
14959
14960 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart75baf692010-06-08 18:31:21 -040014961 "2843 PCI device Power Management suspend.\n");
James Smartda0436e2009-05-22 14:51:39 -040014962
14963 /* Bring down the device */
James Smart618a5232012-06-12 13:54:36 -040014964 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smartda0436e2009-05-22 14:51:39 -040014965 lpfc_offline(phba);
14966 kthread_stop(phba->worker_thread);
14967
14968 /* Disable interrupt from device */
14969 lpfc_sli4_disable_intr(phba);
James Smart5350d872011-10-10 21:33:49 -040014970 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -040014971
James Smartda0436e2009-05-22 14:51:39 -040014972 return 0;
14973}
14974
14975/**
14976 * lpfc_pci_resume_one_s4 - PCI func to resume SLI-4 device for power mgmnt
Vaibhav Guptaef6fa162020-11-02 22:17:18 +053014977 * @dev_d: pointer to device
James Smartda0436e2009-05-22 14:51:39 -040014978 *
14979 * This routine is called from the kernel's PCI subsystem to support system
14980 * Power Management (PM) to device with SLI-4 interface spac. When PM invokes
14981 * this method, it restores the device's PCI config space state and fully
14982 * reinitializes the device and brings it online. Note that as the driver
14983 * implements the minimum PM requirements to a power-aware driver's PM for
14984 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
14985 * to the suspend() method call will be treated as SUSPEND and the driver
14986 * will fully reinitialize its device during resume() method call, the device
14987 * will be set to PCI_D0 directly in PCI config space before restoring the
14988 * state.
14989 *
14990 * Return code
14991 * 0 - driver suspended the device
14992 * Error otherwise
14993 **/
Vaibhav Guptaef6fa162020-11-02 22:17:18 +053014994static int __maybe_unused
14995lpfc_pci_resume_one_s4(struct device *dev_d)
James Smartda0436e2009-05-22 14:51:39 -040014996{
Vaibhav Guptaef6fa162020-11-02 22:17:18 +053014997 struct Scsi_Host *shost = dev_get_drvdata(dev_d);
James Smartda0436e2009-05-22 14:51:39 -040014998 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
14999 uint32_t intr_mode;
15000 int error;
15001
15002 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
15003 "0292 PCI device Power Management resume.\n");
15004
James Smartda0436e2009-05-22 14:51:39 -040015005 /* Startup the kernel thread for this host adapter. */
15006 phba->worker_thread = kthread_run(lpfc_do_work, phba,
15007 "lpfc_worker_%d", phba->brd_no);
15008 if (IS_ERR(phba->worker_thread)) {
15009 error = PTR_ERR(phba->worker_thread);
15010 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15011 "0293 PM resume failed to start worker "
15012 "thread: error=x%x.\n", error);
15013 return error;
15014 }
15015
15016 /* Configure and enable interrupt */
15017 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
15018 if (intr_mode == LPFC_INTR_ERROR) {
Dick Kennedy372c1872020-06-30 14:50:00 -070015019 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartda0436e2009-05-22 14:51:39 -040015020 "0294 PM resume Failed to enable interrupt\n");
15021 return -EIO;
15022 } else
15023 phba->intr_mode = intr_mode;
15024
15025 /* Restart HBA and bring it online */
15026 lpfc_sli_brdrestart(phba);
15027 lpfc_online(phba);
15028
15029 /* Log the current active interrupt mode */
15030 lpfc_log_intr_mode(phba, phba->intr_mode);
15031
15032 return 0;
15033}
15034
15035/**
James Smart75baf692010-06-08 18:31:21 -040015036 * lpfc_sli4_prep_dev_for_recover - Prepare SLI4 device for pci slot recover
15037 * @phba: pointer to lpfc hba data structure.
15038 *
15039 * This routine is called to prepare the SLI4 device for PCI slot recover. It
15040 * aborts all the outstanding SCSI I/Os to the pci device.
15041 **/
15042static void
15043lpfc_sli4_prep_dev_for_recover(struct lpfc_hba *phba)
15044{
Dick Kennedy372c1872020-06-30 14:50:00 -070015045 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart75baf692010-06-08 18:31:21 -040015046 "2828 PCI channel I/O abort preparing for recovery\n");
15047 /*
15048 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
15049 * and let the SCSI mid-layer to retry them to recover.
15050 */
James Smartdb55fba2014-04-04 13:52:02 -040015051 lpfc_sli_abort_fcp_rings(phba);
James Smart75baf692010-06-08 18:31:21 -040015052}
15053
15054/**
15055 * lpfc_sli4_prep_dev_for_reset - Prepare SLI4 device for pci slot reset
15056 * @phba: pointer to lpfc hba data structure.
15057 *
15058 * This routine is called to prepare the SLI4 device for PCI slot reset. It
15059 * disables the device interrupt and pci device, and aborts the internal FCP
15060 * pending I/Os.
15061 **/
15062static void
15063lpfc_sli4_prep_dev_for_reset(struct lpfc_hba *phba)
15064{
Dick Kennedy372c1872020-06-30 14:50:00 -070015065 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart75baf692010-06-08 18:31:21 -040015066 "2826 PCI channel disable preparing for reset\n");
15067
15068 /* Block any management I/Os to the device */
James Smart618a5232012-06-12 13:54:36 -040015069 lpfc_block_mgmt_io(phba, LPFC_MBX_NO_WAIT);
James Smart75baf692010-06-08 18:31:21 -040015070
15071 /* Block all SCSI devices' I/Os on the host */
15072 lpfc_scsi_dev_block(phba);
15073
James Smartc00f62e2019-08-14 16:57:11 -070015074 /* Flush all driver's outstanding I/Os as we are to reset */
15075 lpfc_sli_flush_io_rings(phba);
James Smartc3725bd2017-11-20 16:00:42 -080015076
James Smart75baf692010-06-08 18:31:21 -040015077 /* stop all timers */
15078 lpfc_stop_hba_timers(phba);
15079
15080 /* Disable interrupt and pci device */
15081 lpfc_sli4_disable_intr(phba);
James Smart5350d872011-10-10 21:33:49 -040015082 lpfc_sli4_queue_destroy(phba);
James Smart75baf692010-06-08 18:31:21 -040015083 pci_disable_device(phba->pcidev);
James Smart75baf692010-06-08 18:31:21 -040015084}
15085
15086/**
15087 * lpfc_sli4_prep_dev_for_perm_failure - Prepare SLI4 dev for pci slot disable
15088 * @phba: pointer to lpfc hba data structure.
15089 *
15090 * This routine is called to prepare the SLI4 device for PCI slot permanently
15091 * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
15092 * pending I/Os.
15093 **/
15094static void
15095lpfc_sli4_prep_dev_for_perm_failure(struct lpfc_hba *phba)
15096{
Dick Kennedy372c1872020-06-30 14:50:00 -070015097 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart75baf692010-06-08 18:31:21 -040015098 "2827 PCI channel permanent disable for failure\n");
15099
15100 /* Block all SCSI devices' I/Os on the host */
15101 lpfc_scsi_dev_block(phba);
15102
15103 /* stop all timers */
15104 lpfc_stop_hba_timers(phba);
15105
James Smartc00f62e2019-08-14 16:57:11 -070015106 /* Clean up all driver's outstanding I/Os */
15107 lpfc_sli_flush_io_rings(phba);
James Smart75baf692010-06-08 18:31:21 -040015108}
15109
15110/**
James Smartda0436e2009-05-22 14:51:39 -040015111 * lpfc_io_error_detected_s4 - Method for handling PCI I/O error to SLI-4 device
15112 * @pdev: pointer to PCI device.
15113 * @state: the current PCI connection state.
15114 *
15115 * This routine is called from the PCI subsystem for error handling to device
15116 * with SLI-4 interface spec. This function is called by the PCI subsystem
15117 * after a PCI bus error affecting this device has been detected. When this
15118 * function is invoked, it will need to stop all the I/Os and interrupt(s)
15119 * to the device. Once that is done, it will return PCI_ERS_RESULT_NEED_RESET
15120 * for the PCI subsystem to perform proper recovery as desired.
15121 *
15122 * Return codes
15123 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
15124 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
15125 **/
15126static pci_ers_result_t
15127lpfc_io_error_detected_s4(struct pci_dev *pdev, pci_channel_state_t state)
15128{
James Smart75baf692010-06-08 18:31:21 -040015129 struct Scsi_Host *shost = pci_get_drvdata(pdev);
15130 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
15131
15132 switch (state) {
15133 case pci_channel_io_normal:
15134 /* Non-fatal error, prepare for recovery */
15135 lpfc_sli4_prep_dev_for_recover(phba);
15136 return PCI_ERS_RESULT_CAN_RECOVER;
15137 case pci_channel_io_frozen:
James Smart25ac2c972021-09-10 16:31:54 -070015138 phba->hba_flag |= HBA_PCI_ERR;
James Smart75baf692010-06-08 18:31:21 -040015139 /* Fatal error, prepare for slot reset */
15140 lpfc_sli4_prep_dev_for_reset(phba);
15141 return PCI_ERS_RESULT_NEED_RESET;
15142 case pci_channel_io_perm_failure:
James Smart25ac2c972021-09-10 16:31:54 -070015143 phba->hba_flag |= HBA_PCI_ERR;
James Smart75baf692010-06-08 18:31:21 -040015144 /* Permanent failure, prepare for device down */
15145 lpfc_sli4_prep_dev_for_perm_failure(phba);
15146 return PCI_ERS_RESULT_DISCONNECT;
15147 default:
James Smart25ac2c972021-09-10 16:31:54 -070015148 phba->hba_flag |= HBA_PCI_ERR;
James Smart75baf692010-06-08 18:31:21 -040015149 /* Unknown state, prepare and request slot reset */
Dick Kennedy372c1872020-06-30 14:50:00 -070015150 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart75baf692010-06-08 18:31:21 -040015151 "2825 Unknown PCI error state: x%x\n", state);
15152 lpfc_sli4_prep_dev_for_reset(phba);
15153 return PCI_ERS_RESULT_NEED_RESET;
15154 }
James Smartda0436e2009-05-22 14:51:39 -040015155}
15156
15157/**
15158 * lpfc_io_slot_reset_s4 - Method for restart PCI SLI-4 device from scratch
15159 * @pdev: pointer to PCI device.
15160 *
15161 * This routine is called from the PCI subsystem for error handling to device
15162 * with SLI-4 interface spec. It is called after PCI bus has been reset to
15163 * restart the PCI card from scratch, as if from a cold-boot. During the
15164 * PCI subsystem error recovery, after the driver returns
15165 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
15166 * recovery and then call this routine before calling the .resume method to
15167 * recover the device. This function will initialize the HBA device, enable
15168 * the interrupt, but it will just put the HBA to offline state without
15169 * passing any I/O traffic.
15170 *
15171 * Return codes
15172 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
15173 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
15174 */
15175static pci_ers_result_t
15176lpfc_io_slot_reset_s4(struct pci_dev *pdev)
15177{
James Smart75baf692010-06-08 18:31:21 -040015178 struct Scsi_Host *shost = pci_get_drvdata(pdev);
15179 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
15180 struct lpfc_sli *psli = &phba->sli;
15181 uint32_t intr_mode;
15182
15183 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
15184 if (pci_enable_device_mem(pdev)) {
15185 printk(KERN_ERR "lpfc: Cannot re-enable "
15186 "PCI device after reset.\n");
15187 return PCI_ERS_RESULT_DISCONNECT;
15188 }
15189
15190 pci_restore_state(pdev);
James Smart0a96e972011-07-22 18:37:28 -040015191
James Smart25ac2c972021-09-10 16:31:54 -070015192 phba->hba_flag &= ~HBA_PCI_ERR;
James Smart0a96e972011-07-22 18:37:28 -040015193 /*
15194 * As the new kernel behavior of pci_restore_state() API call clears
15195 * device saved_state flag, need to save the restored state again.
15196 */
15197 pci_save_state(pdev);
15198
James Smart75baf692010-06-08 18:31:21 -040015199 if (pdev->is_busmaster)
15200 pci_set_master(pdev);
15201
15202 spin_lock_irq(&phba->hbalock);
15203 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
15204 spin_unlock_irq(&phba->hbalock);
15205
15206 /* Configure and enable interrupt */
15207 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
15208 if (intr_mode == LPFC_INTR_ERROR) {
Dick Kennedy372c1872020-06-30 14:50:00 -070015209 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart75baf692010-06-08 18:31:21 -040015210 "2824 Cannot re-enable interrupt after "
15211 "slot reset.\n");
15212 return PCI_ERS_RESULT_DISCONNECT;
15213 } else
15214 phba->intr_mode = intr_mode;
James Smart25ac2c972021-09-10 16:31:54 -070015215 lpfc_cpu_affinity_check(phba, phba->cfg_irq_chann);
James Smart75baf692010-06-08 18:31:21 -040015216
15217 /* Log the current active interrupt mode */
15218 lpfc_log_intr_mode(phba, phba->intr_mode);
15219
James Smartda0436e2009-05-22 14:51:39 -040015220 return PCI_ERS_RESULT_RECOVERED;
15221}
15222
15223/**
15224 * lpfc_io_resume_s4 - Method for resuming PCI I/O operation to SLI-4 device
15225 * @pdev: pointer to PCI device
15226 *
15227 * This routine is called from the PCI subsystem for error handling to device
15228 * with SLI-4 interface spec. It is called when kernel error recovery tells
15229 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
15230 * error recovery. After this call, traffic can start to flow from this device
15231 * again.
15232 **/
15233static void
15234lpfc_io_resume_s4(struct pci_dev *pdev)
15235{
James Smart75baf692010-06-08 18:31:21 -040015236 struct Scsi_Host *shost = pci_get_drvdata(pdev);
15237 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
15238
15239 /*
15240 * In case of slot reset, as function reset is performed through
15241 * mailbox command which needs DMA to be enabled, this operation
15242 * has to be moved to the io resume phase. Taking device offline
15243 * will perform the necessary cleanup.
15244 */
15245 if (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE)) {
15246 /* Perform device reset */
James Smart618a5232012-06-12 13:54:36 -040015247 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart75baf692010-06-08 18:31:21 -040015248 lpfc_offline(phba);
15249 lpfc_sli_brdrestart(phba);
15250 /* Bring the device back online */
15251 lpfc_online(phba);
15252 }
James Smartda0436e2009-05-22 14:51:39 -040015253}
15254
15255/**
James Smart3772a992009-05-22 14:50:54 -040015256 * lpfc_pci_probe_one - lpfc PCI probe func to reg dev to PCI subsystem
15257 * @pdev: pointer to PCI device
15258 * @pid: pointer to PCI device identifier
15259 *
15260 * This routine is to be registered to the kernel's PCI subsystem. When an
15261 * Emulex HBA device is presented on PCI bus, the kernel PCI subsystem looks
15262 * at PCI device-specific information of the device and driver to see if the
15263 * driver state that it can support this kind of device. If the match is
15264 * successful, the driver core invokes this routine. This routine dispatches
15265 * the action to the proper SLI-3 or SLI-4 device probing routine, which will
15266 * do all the initialization that it needs to do to handle the HBA device
15267 * properly.
15268 *
15269 * Return code
15270 * 0 - driver can claim the device
15271 * negative value - driver can not claim the device
15272 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080015273static int
James Smart3772a992009-05-22 14:50:54 -040015274lpfc_pci_probe_one(struct pci_dev *pdev, const struct pci_device_id *pid)
15275{
15276 int rc;
James Smart8fa38512009-07-19 10:01:03 -040015277 struct lpfc_sli_intf intf;
James Smart3772a992009-05-22 14:50:54 -040015278
James Smart28baac72010-02-12 14:42:03 -050015279 if (pci_read_config_dword(pdev, LPFC_SLI_INTF, &intf.word0))
James Smart3772a992009-05-22 14:50:54 -040015280 return -ENODEV;
15281
James Smart8fa38512009-07-19 10:01:03 -040015282 if ((bf_get(lpfc_sli_intf_valid, &intf) == LPFC_SLI_INTF_VALID) &&
James Smart28baac72010-02-12 14:42:03 -050015283 (bf_get(lpfc_sli_intf_slirev, &intf) == LPFC_SLI_INTF_REV_SLI4))
James Smartda0436e2009-05-22 14:51:39 -040015284 rc = lpfc_pci_probe_one_s4(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -040015285 else
James Smart3772a992009-05-22 14:50:54 -040015286 rc = lpfc_pci_probe_one_s3(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -040015287
James Smart3772a992009-05-22 14:50:54 -040015288 return rc;
15289}
15290
15291/**
15292 * lpfc_pci_remove_one - lpfc PCI func to unreg dev from PCI subsystem
15293 * @pdev: pointer to PCI device
15294 *
15295 * This routine is to be registered to the kernel's PCI subsystem. When an
15296 * Emulex HBA is removed from PCI bus, the driver core invokes this routine.
15297 * This routine dispatches the action to the proper SLI-3 or SLI-4 device
15298 * remove routine, which will perform all the necessary cleanup for the
15299 * device to be removed from the PCI subsystem properly.
15300 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080015301static void
James Smart3772a992009-05-22 14:50:54 -040015302lpfc_pci_remove_one(struct pci_dev *pdev)
15303{
15304 struct Scsi_Host *shost = pci_get_drvdata(pdev);
15305 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
15306
15307 switch (phba->pci_dev_grp) {
15308 case LPFC_PCI_DEV_LP:
15309 lpfc_pci_remove_one_s3(pdev);
15310 break;
James Smartda0436e2009-05-22 14:51:39 -040015311 case LPFC_PCI_DEV_OC:
15312 lpfc_pci_remove_one_s4(pdev);
15313 break;
James Smart3772a992009-05-22 14:50:54 -040015314 default:
Dick Kennedy372c1872020-06-30 14:50:00 -070015315 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart3772a992009-05-22 14:50:54 -040015316 "1424 Invalid PCI device group: 0x%x\n",
15317 phba->pci_dev_grp);
15318 break;
15319 }
15320 return;
15321}
15322
15323/**
15324 * lpfc_pci_suspend_one - lpfc PCI func to suspend dev for power management
Vaibhav Guptaef6fa162020-11-02 22:17:18 +053015325 * @dev: pointer to device
James Smart3772a992009-05-22 14:50:54 -040015326 *
15327 * This routine is to be registered to the kernel's PCI subsystem to support
15328 * system Power Management (PM). When PM invokes this method, it dispatches
15329 * the action to the proper SLI-3 or SLI-4 device suspend routine, which will
15330 * suspend the device.
15331 *
15332 * Return code
15333 * 0 - driver suspended the device
15334 * Error otherwise
15335 **/
Vaibhav Guptaef6fa162020-11-02 22:17:18 +053015336static int __maybe_unused
15337lpfc_pci_suspend_one(struct device *dev)
James Smart3772a992009-05-22 14:50:54 -040015338{
Vaibhav Guptaef6fa162020-11-02 22:17:18 +053015339 struct Scsi_Host *shost = dev_get_drvdata(dev);
James Smart3772a992009-05-22 14:50:54 -040015340 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
15341 int rc = -ENODEV;
15342
15343 switch (phba->pci_dev_grp) {
15344 case LPFC_PCI_DEV_LP:
Vaibhav Guptaef6fa162020-11-02 22:17:18 +053015345 rc = lpfc_pci_suspend_one_s3(dev);
James Smart3772a992009-05-22 14:50:54 -040015346 break;
James Smartda0436e2009-05-22 14:51:39 -040015347 case LPFC_PCI_DEV_OC:
Vaibhav Guptaef6fa162020-11-02 22:17:18 +053015348 rc = lpfc_pci_suspend_one_s4(dev);
James Smartda0436e2009-05-22 14:51:39 -040015349 break;
James Smart3772a992009-05-22 14:50:54 -040015350 default:
Dick Kennedy372c1872020-06-30 14:50:00 -070015351 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart3772a992009-05-22 14:50:54 -040015352 "1425 Invalid PCI device group: 0x%x\n",
15353 phba->pci_dev_grp);
15354 break;
15355 }
15356 return rc;
15357}
15358
15359/**
15360 * lpfc_pci_resume_one - lpfc PCI func to resume dev for power management
Vaibhav Guptaef6fa162020-11-02 22:17:18 +053015361 * @dev: pointer to device
James Smart3772a992009-05-22 14:50:54 -040015362 *
15363 * This routine is to be registered to the kernel's PCI subsystem to support
15364 * system Power Management (PM). When PM invokes this method, it dispatches
15365 * the action to the proper SLI-3 or SLI-4 device resume routine, which will
15366 * resume the device.
15367 *
15368 * Return code
15369 * 0 - driver suspended the device
15370 * Error otherwise
15371 **/
Vaibhav Guptaef6fa162020-11-02 22:17:18 +053015372static int __maybe_unused
15373lpfc_pci_resume_one(struct device *dev)
James Smart3772a992009-05-22 14:50:54 -040015374{
Vaibhav Guptaef6fa162020-11-02 22:17:18 +053015375 struct Scsi_Host *shost = dev_get_drvdata(dev);
James Smart3772a992009-05-22 14:50:54 -040015376 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
15377 int rc = -ENODEV;
15378
15379 switch (phba->pci_dev_grp) {
15380 case LPFC_PCI_DEV_LP:
Vaibhav Guptaef6fa162020-11-02 22:17:18 +053015381 rc = lpfc_pci_resume_one_s3(dev);
James Smart3772a992009-05-22 14:50:54 -040015382 break;
James Smartda0436e2009-05-22 14:51:39 -040015383 case LPFC_PCI_DEV_OC:
Vaibhav Guptaef6fa162020-11-02 22:17:18 +053015384 rc = lpfc_pci_resume_one_s4(dev);
James Smartda0436e2009-05-22 14:51:39 -040015385 break;
James Smart3772a992009-05-22 14:50:54 -040015386 default:
Dick Kennedy372c1872020-06-30 14:50:00 -070015387 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart3772a992009-05-22 14:50:54 -040015388 "1426 Invalid PCI device group: 0x%x\n",
15389 phba->pci_dev_grp);
15390 break;
15391 }
15392 return rc;
15393}
15394
15395/**
15396 * lpfc_io_error_detected - lpfc method for handling PCI I/O error
15397 * @pdev: pointer to PCI device.
15398 * @state: the current PCI connection state.
15399 *
15400 * This routine is registered to the PCI subsystem for error handling. This
15401 * function is called by the PCI subsystem after a PCI bus error affecting
15402 * this device has been detected. When this routine is invoked, it dispatches
15403 * the action to the proper SLI-3 or SLI-4 device error detected handling
15404 * routine, which will perform the proper error detected operation.
15405 *
15406 * Return codes
15407 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
15408 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
15409 **/
15410static pci_ers_result_t
15411lpfc_io_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
15412{
15413 struct Scsi_Host *shost = pci_get_drvdata(pdev);
15414 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
15415 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
15416
James Smart25ac2c972021-09-10 16:31:54 -070015417 if (phba->link_state == LPFC_HBA_ERROR &&
15418 phba->hba_flag & HBA_IOQ_FLUSH)
15419 return PCI_ERS_RESULT_NEED_RESET;
15420
James Smart3772a992009-05-22 14:50:54 -040015421 switch (phba->pci_dev_grp) {
15422 case LPFC_PCI_DEV_LP:
15423 rc = lpfc_io_error_detected_s3(pdev, state);
15424 break;
James Smartda0436e2009-05-22 14:51:39 -040015425 case LPFC_PCI_DEV_OC:
15426 rc = lpfc_io_error_detected_s4(pdev, state);
15427 break;
James Smart3772a992009-05-22 14:50:54 -040015428 default:
Dick Kennedy372c1872020-06-30 14:50:00 -070015429 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart3772a992009-05-22 14:50:54 -040015430 "1427 Invalid PCI device group: 0x%x\n",
15431 phba->pci_dev_grp);
15432 break;
15433 }
15434 return rc;
15435}
15436
15437/**
15438 * lpfc_io_slot_reset - lpfc method for restart PCI dev from scratch
15439 * @pdev: pointer to PCI device.
15440 *
15441 * This routine is registered to the PCI subsystem for error handling. This
15442 * function is called after PCI bus has been reset to restart the PCI card
15443 * from scratch, as if from a cold-boot. When this routine is invoked, it
15444 * dispatches the action to the proper SLI-3 or SLI-4 device reset handling
15445 * routine, which will perform the proper device reset.
15446 *
15447 * Return codes
15448 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
15449 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
15450 **/
15451static pci_ers_result_t
15452lpfc_io_slot_reset(struct pci_dev *pdev)
15453{
15454 struct Scsi_Host *shost = pci_get_drvdata(pdev);
15455 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
15456 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
15457
15458 switch (phba->pci_dev_grp) {
15459 case LPFC_PCI_DEV_LP:
15460 rc = lpfc_io_slot_reset_s3(pdev);
15461 break;
James Smartda0436e2009-05-22 14:51:39 -040015462 case LPFC_PCI_DEV_OC:
15463 rc = lpfc_io_slot_reset_s4(pdev);
15464 break;
James Smart3772a992009-05-22 14:50:54 -040015465 default:
Dick Kennedy372c1872020-06-30 14:50:00 -070015466 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart3772a992009-05-22 14:50:54 -040015467 "1428 Invalid PCI device group: 0x%x\n",
15468 phba->pci_dev_grp);
15469 break;
15470 }
15471 return rc;
15472}
15473
15474/**
15475 * lpfc_io_resume - lpfc method for resuming PCI I/O operation
15476 * @pdev: pointer to PCI device
15477 *
15478 * This routine is registered to the PCI subsystem for error handling. It
15479 * is called when kernel error recovery tells the lpfc driver that it is
15480 * OK to resume normal PCI operation after PCI bus error recovery. When
15481 * this routine is invoked, it dispatches the action to the proper SLI-3
15482 * or SLI-4 device io_resume routine, which will resume the device operation.
15483 **/
15484static void
15485lpfc_io_resume(struct pci_dev *pdev)
15486{
15487 struct Scsi_Host *shost = pci_get_drvdata(pdev);
15488 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
15489
15490 switch (phba->pci_dev_grp) {
15491 case LPFC_PCI_DEV_LP:
15492 lpfc_io_resume_s3(pdev);
15493 break;
James Smartda0436e2009-05-22 14:51:39 -040015494 case LPFC_PCI_DEV_OC:
15495 lpfc_io_resume_s4(pdev);
15496 break;
James Smart3772a992009-05-22 14:50:54 -040015497 default:
Dick Kennedy372c1872020-06-30 14:50:00 -070015498 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart3772a992009-05-22 14:50:54 -040015499 "1429 Invalid PCI device group: 0x%x\n",
15500 phba->pci_dev_grp);
15501 break;
15502 }
15503 return;
15504}
15505
James Smart1ba981f2014-02-20 09:56:45 -050015506/**
15507 * lpfc_sli4_oas_verify - Verify OAS is supported by this adapter
15508 * @phba: pointer to lpfc hba data structure.
15509 *
15510 * This routine checks to see if OAS is supported for this adapter. If
15511 * supported, the configure Flash Optimized Fabric flag is set. Otherwise,
15512 * the enable oas flag is cleared and the pool created for OAS device data
15513 * is destroyed.
15514 *
15515 **/
YueHaibingc7092972019-04-17 23:05:54 +080015516static void
James Smart1ba981f2014-02-20 09:56:45 -050015517lpfc_sli4_oas_verify(struct lpfc_hba *phba)
15518{
15519
15520 if (!phba->cfg_EnableXLane)
15521 return;
15522
15523 if (phba->sli4_hba.pc_sli4_params.oas_supported) {
15524 phba->cfg_fof = 1;
15525 } else {
James Smartf38fa0b2014-04-04 13:52:21 -040015526 phba->cfg_fof = 0;
Saurav Girepunjec3e5aac2019-10-27 01:17:17 +053015527 mempool_destroy(phba->device_data_mem_pool);
James Smart1ba981f2014-02-20 09:56:45 -050015528 phba->device_data_mem_pool = NULL;
15529 }
15530
15531 return;
15532}
15533
15534/**
James Smartd2cc9bc2018-09-10 10:30:50 -070015535 * lpfc_sli4_ras_init - Verify RAS-FW log is supported by this adapter
15536 * @phba: pointer to lpfc hba data structure.
15537 *
15538 * This routine checks to see if RAS is supported by the adapter. Check the
15539 * function through which RAS support enablement is to be done.
15540 **/
15541void
15542lpfc_sli4_ras_init(struct lpfc_hba *phba)
15543{
James Smartf6c5e6c2021-07-22 15:17:18 -070015544 /* if ASIC_GEN_NUM >= 0xC) */
15545 if ((bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
15546 LPFC_SLI_INTF_IF_TYPE_6) ||
15547 (bf_get(lpfc_sli_intf_sli_family, &phba->sli4_hba.sli_intf) ==
15548 LPFC_SLI_INTF_FAMILY_G6)) {
James Smartd2cc9bc2018-09-10 10:30:50 -070015549 phba->ras_fwlog.ras_hwsupport = true;
James Smartcb349902018-11-29 16:09:27 -080015550 if (phba->cfg_ras_fwlog_func == PCI_FUNC(phba->pcidev->devfn) &&
15551 phba->cfg_ras_fwlog_buffsize)
James Smartd2cc9bc2018-09-10 10:30:50 -070015552 phba->ras_fwlog.ras_enabled = true;
15553 else
15554 phba->ras_fwlog.ras_enabled = false;
James Smartf6c5e6c2021-07-22 15:17:18 -070015555 } else {
James Smartd2cc9bc2018-09-10 10:30:50 -070015556 phba->ras_fwlog.ras_hwsupport = false;
15557 }
15558}
15559
James Smart1ba981f2014-02-20 09:56:45 -050015560
dea31012005-04-17 16:05:31 -050015561MODULE_DEVICE_TABLE(pci, lpfc_id_table);
15562
Stephen Hemmingera55b2d22012-09-07 09:33:16 -070015563static const struct pci_error_handlers lpfc_err_handler = {
Linas Vepstas8d63f372007-02-14 14:28:36 -060015564 .error_detected = lpfc_io_error_detected,
15565 .slot_reset = lpfc_io_slot_reset,
15566 .resume = lpfc_io_resume,
15567};
15568
Vaibhav Guptaef6fa162020-11-02 22:17:18 +053015569static SIMPLE_DEV_PM_OPS(lpfc_pci_pm_ops_one,
15570 lpfc_pci_suspend_one,
15571 lpfc_pci_resume_one);
15572
dea31012005-04-17 16:05:31 -050015573static struct pci_driver lpfc_driver = {
15574 .name = LPFC_DRIVER_NAME,
15575 .id_table = lpfc_id_table,
15576 .probe = lpfc_pci_probe_one,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080015577 .remove = lpfc_pci_remove_one,
Anton Blanchard85e8a232017-02-13 08:49:20 +110015578 .shutdown = lpfc_pci_remove_one,
Vaibhav Guptaef6fa162020-11-02 22:17:18 +053015579 .driver.pm = &lpfc_pci_pm_ops_one,
James Smart2e0fef82007-06-17 19:56:36 -050015580 .err_handler = &lpfc_err_handler,
dea31012005-04-17 16:05:31 -050015581};
15582
James Smart3ef6d242012-01-18 16:23:48 -050015583static const struct file_operations lpfc_mgmt_fop = {
Al Viro858feac2013-04-14 22:39:37 -040015584 .owner = THIS_MODULE,
James Smart3ef6d242012-01-18 16:23:48 -050015585};
15586
15587static struct miscdevice lpfc_mgmt_dev = {
15588 .minor = MISC_DYNAMIC_MINOR,
15589 .name = "lpfcmgmt",
15590 .fops = &lpfc_mgmt_fop,
15591};
15592
James Smarte59058c2008-08-24 21:49:00 -040015593/**
James Smart3621a712009-04-06 18:47:14 -040015594 * lpfc_init - lpfc module initialization routine
James Smarte59058c2008-08-24 21:49:00 -040015595 *
15596 * This routine is to be invoked when the lpfc module is loaded into the
15597 * kernel. The special kernel macro module_init() is used to indicate the
15598 * role of this routine to the kernel as lpfc module entry point.
15599 *
15600 * Return codes
15601 * 0 - successful
15602 * -ENOMEM - FC attach transport failed
15603 * all others - failed
15604 */
dea31012005-04-17 16:05:31 -050015605static int __init
15606lpfc_init(void)
15607{
15608 int error = 0;
15609
Anton Blanchardbc2736e2020-07-13 18:39:08 +100015610 pr_info(LPFC_MODULE_DESC "\n");
15611 pr_info(LPFC_COPYRIGHT "\n");
dea31012005-04-17 16:05:31 -050015612
James Smart3ef6d242012-01-18 16:23:48 -050015613 error = misc_register(&lpfc_mgmt_dev);
15614 if (error)
15615 printk(KERN_ERR "Could not register lpfcmgmt device, "
15616 "misc_register returned with status %d", error);
15617
Jing Xiangfeng1eaff532020-07-31 14:56:39 +080015618 error = -ENOMEM;
James Smart458c0832016-07-06 12:36:07 -070015619 lpfc_transport_functions.vport_create = lpfc_vport_create;
15620 lpfc_transport_functions.vport_delete = lpfc_vport_delete;
dea31012005-04-17 16:05:31 -050015621 lpfc_transport_template =
15622 fc_attach_transport(&lpfc_transport_functions);
James Smart7ee5d432007-10-27 13:37:17 -040015623 if (lpfc_transport_template == NULL)
Jing Xiangfeng1eaff532020-07-31 14:56:39 +080015624 goto unregister;
James Smart458c0832016-07-06 12:36:07 -070015625 lpfc_vport_transport_template =
15626 fc_attach_transport(&lpfc_vport_transport_functions);
15627 if (lpfc_vport_transport_template == NULL) {
15628 fc_release_transport(lpfc_transport_template);
Jing Xiangfeng1eaff532020-07-31 14:56:39 +080015629 goto unregister;
James Smart7ee5d432007-10-27 13:37:17 -040015630 }
James Smart840a4702020-11-15 11:26:40 -080015631 lpfc_wqe_cmd_template();
James Smartbd3061b2018-03-05 12:04:05 -080015632 lpfc_nvmet_cmd_template();
James Smart7bb03bb2013-04-17 20:19:16 -040015633
15634 /* Initialize in case vector mapping is needed */
James Smart2ea259e2017-02-12 13:52:27 -080015635 lpfc_present_cpu = num_present_cpus();
James Smart7bb03bb2013-04-17 20:19:16 -040015636
James Smarta5b141a2021-09-27 11:35:18 -070015637 lpfc_pldv_detect = false;
15638
James Smart93a4d6f2019-11-04 16:57:05 -080015639 error = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN,
15640 "lpfc/sli4:online",
15641 lpfc_cpu_online, lpfc_cpu_offline);
15642 if (error < 0)
15643 goto cpuhp_failure;
15644 lpfc_cpuhp_state = error;
15645
dea31012005-04-17 16:05:31 -050015646 error = pci_register_driver(&lpfc_driver);
James Smart93a4d6f2019-11-04 16:57:05 -080015647 if (error)
15648 goto unwind;
15649
15650 return error;
15651
15652unwind:
15653 cpuhp_remove_multi_state(lpfc_cpuhp_state);
15654cpuhp_failure:
15655 fc_release_transport(lpfc_transport_template);
15656 fc_release_transport(lpfc_vport_transport_template);
Jing Xiangfeng1eaff532020-07-31 14:56:39 +080015657unregister:
15658 misc_deregister(&lpfc_mgmt_dev);
dea31012005-04-17 16:05:31 -050015659
15660 return error;
15661}
15662
Dick Kennedy372c1872020-06-30 14:50:00 -070015663void lpfc_dmp_dbg(struct lpfc_hba *phba)
15664{
15665 unsigned int start_idx;
15666 unsigned int dbg_cnt;
15667 unsigned int temp_idx;
15668 int i;
15669 int j = 0;
James Smarte8613082021-07-07 11:43:33 -070015670 unsigned long rem_nsec, iflags;
15671 bool log_verbose = false;
15672 struct lpfc_vport *port_iterator;
Dick Kennedy372c1872020-06-30 14:50:00 -070015673
James Smart0b3ad322021-01-04 10:02:39 -080015674 /* Don't dump messages if we explicitly set log_verbose for the
15675 * physical port or any vport.
15676 */
Dick Kennedy372c1872020-06-30 14:50:00 -070015677 if (phba->cfg_log_verbose)
15678 return;
15679
James Smarte8613082021-07-07 11:43:33 -070015680 spin_lock_irqsave(&phba->port_list_lock, iflags);
15681 list_for_each_entry(port_iterator, &phba->port_list, listentry) {
15682 if (port_iterator->load_flag & FC_UNLOADING)
15683 continue;
15684 if (scsi_host_get(lpfc_shost_from_vport(port_iterator))) {
15685 if (port_iterator->cfg_log_verbose)
15686 log_verbose = true;
15687
15688 scsi_host_put(lpfc_shost_from_vport(port_iterator));
15689
15690 if (log_verbose) {
15691 spin_unlock_irqrestore(&phba->port_list_lock,
15692 iflags);
James Smart0b3ad322021-01-04 10:02:39 -080015693 return;
15694 }
15695 }
15696 }
James Smarte8613082021-07-07 11:43:33 -070015697 spin_unlock_irqrestore(&phba->port_list_lock, iflags);
James Smart0b3ad322021-01-04 10:02:39 -080015698
Dick Kennedy372c1872020-06-30 14:50:00 -070015699 if (atomic_cmpxchg(&phba->dbg_log_dmping, 0, 1) != 0)
15700 return;
15701
15702 start_idx = (unsigned int)atomic_read(&phba->dbg_log_idx) % DBG_LOG_SZ;
15703 dbg_cnt = (unsigned int)atomic_read(&phba->dbg_log_cnt);
James Smart0b3ad322021-01-04 10:02:39 -080015704 if (!dbg_cnt)
15705 goto out;
Dick Kennedy372c1872020-06-30 14:50:00 -070015706 temp_idx = start_idx;
15707 if (dbg_cnt >= DBG_LOG_SZ) {
15708 dbg_cnt = DBG_LOG_SZ;
15709 temp_idx -= 1;
15710 } else {
15711 if ((start_idx + dbg_cnt) > (DBG_LOG_SZ - 1)) {
15712 temp_idx = (start_idx + dbg_cnt) % DBG_LOG_SZ;
15713 } else {
Dick Kennedy77dd7d72020-07-06 13:42:46 -070015714 if (start_idx < dbg_cnt)
Dick Kennedy372c1872020-06-30 14:50:00 -070015715 start_idx = DBG_LOG_SZ - (dbg_cnt - start_idx);
Dick Kennedy77dd7d72020-07-06 13:42:46 -070015716 else
Dick Kennedy372c1872020-06-30 14:50:00 -070015717 start_idx -= dbg_cnt;
Dick Kennedy372c1872020-06-30 14:50:00 -070015718 }
15719 }
15720 dev_info(&phba->pcidev->dev, "start %d end %d cnt %d\n",
15721 start_idx, temp_idx, dbg_cnt);
15722
15723 for (i = 0; i < dbg_cnt; i++) {
15724 if ((start_idx + i) < DBG_LOG_SZ)
Dick Kennedy77dd7d72020-07-06 13:42:46 -070015725 temp_idx = (start_idx + i) % DBG_LOG_SZ;
Dick Kennedy372c1872020-06-30 14:50:00 -070015726 else
15727 temp_idx = j++;
15728 rem_nsec = do_div(phba->dbg_log[temp_idx].t_ns, NSEC_PER_SEC);
15729 dev_info(&phba->pcidev->dev, "%d: [%5lu.%06lu] %s",
15730 temp_idx,
15731 (unsigned long)phba->dbg_log[temp_idx].t_ns,
15732 rem_nsec / 1000,
15733 phba->dbg_log[temp_idx].log);
15734 }
James Smart0b3ad322021-01-04 10:02:39 -080015735out:
Dick Kennedy372c1872020-06-30 14:50:00 -070015736 atomic_set(&phba->dbg_log_cnt, 0);
15737 atomic_set(&phba->dbg_log_dmping, 0);
15738}
15739
Lee Jones7fa03c72020-07-23 13:24:21 +010015740__printf(2, 3)
Dick Kennedy372c1872020-06-30 14:50:00 -070015741void lpfc_dbg_print(struct lpfc_hba *phba, const char *fmt, ...)
15742{
15743 unsigned int idx;
15744 va_list args;
15745 int dbg_dmping = atomic_read(&phba->dbg_log_dmping);
15746 struct va_format vaf;
15747
15748
15749 va_start(args, fmt);
15750 if (unlikely(dbg_dmping)) {
15751 vaf.fmt = fmt;
15752 vaf.va = &args;
15753 dev_info(&phba->pcidev->dev, "%pV", &vaf);
15754 va_end(args);
15755 return;
15756 }
15757 idx = (unsigned int)atomic_fetch_add(1, &phba->dbg_log_idx) %
15758 DBG_LOG_SZ;
15759
15760 atomic_inc(&phba->dbg_log_cnt);
15761
15762 vscnprintf(phba->dbg_log[idx].log,
15763 sizeof(phba->dbg_log[idx].log), fmt, args);
15764 va_end(args);
15765
15766 phba->dbg_log[idx].t_ns = local_clock();
15767}
15768
James Smarte59058c2008-08-24 21:49:00 -040015769/**
James Smart3621a712009-04-06 18:47:14 -040015770 * lpfc_exit - lpfc module removal routine
James Smarte59058c2008-08-24 21:49:00 -040015771 *
15772 * This routine is invoked when the lpfc module is removed from the kernel.
15773 * The special kernel macro module_exit() is used to indicate the role of
15774 * this routine to the kernel as lpfc module exit point.
15775 */
dea31012005-04-17 16:05:31 -050015776static void __exit
15777lpfc_exit(void)
15778{
James Smart3ef6d242012-01-18 16:23:48 -050015779 misc_deregister(&lpfc_mgmt_dev);
dea31012005-04-17 16:05:31 -050015780 pci_unregister_driver(&lpfc_driver);
James Smart93a4d6f2019-11-04 16:57:05 -080015781 cpuhp_remove_multi_state(lpfc_cpuhp_state);
dea31012005-04-17 16:05:31 -050015782 fc_release_transport(lpfc_transport_template);
James Smart458c0832016-07-06 12:36:07 -070015783 fc_release_transport(lpfc_vport_transport_template);
Johannes Thumshirn79739672015-08-31 16:48:11 -040015784 idr_destroy(&lpfc_hba_index);
dea31012005-04-17 16:05:31 -050015785}
15786
15787module_init(lpfc_init);
15788module_exit(lpfc_exit);
15789MODULE_LICENSE("GPL");
15790MODULE_DESCRIPTION(LPFC_MODULE_DESC);
James Smartd080abe2017-02-12 13:52:39 -080015791MODULE_AUTHOR("Broadcom");
dea31012005-04-17 16:05:31 -050015792MODULE_VERSION("0:" LPFC_DRIVER_VERSION);