blob: 03dadc4fa7cb4e2e5aa3bf860190922244eb7463 [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 Smart145e5a82020-01-27 16:23:12 -08004 * Copyright (C) 2017-2020 Broadcom. All Rights Reserved. The term *
James Smart3e21d1c2018-05-04 20:37:59 -07005 * “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. *
James Smart50611572016-03-31 14:12:34 -07006 * Copyright (C) 2004-2016 Emulex. All rights reserved. *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04007 * EMULEX and SLI are trademarks of Emulex. *
James Smartd080abe2017-02-12 13:52:39 -08008 * www.broadcom.com *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04009 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea31012005-04-17 16:05:31 -050010 * *
11 * This program is free software; you can redistribute it and/or *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040012 * modify it under the terms of version 2 of the GNU General *
13 * Public License as published by the Free Software Foundation. *
14 * This program is distributed in the hope that it will be useful. *
15 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
16 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
17 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
18 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
19 * TO BE LEGALLY INVALID. See the GNU General Public License for *
20 * more details, a copy of which can be found in the file COPYING *
21 * included with this package. *
dea31012005-04-17 16:05:31 -050022 *******************************************************************/
23
dea31012005-04-17 16:05:31 -050024#include <linux/blkdev.h>
25#include <linux/delay.h>
26#include <linux/dma-mapping.h>
27#include <linux/idr.h>
28#include <linux/interrupt.h>
Paul Gortmakeracf3368f2011-05-27 09:47:43 -040029#include <linux/module.h>
dea31012005-04-17 16:05:31 -050030#include <linux/kthread.h>
31#include <linux/pci.h>
32#include <linux/spinlock.h>
James Smart92d7f7b2007-06-17 19:56:38 -050033#include <linux/ctype.h>
James Smart0d878412009-10-02 15:16:56 -040034#include <linux/aer.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090035#include <linux/slab.h>
James Smart52d52442011-05-24 11:42:45 -040036#include <linux/firmware.h>
James Smart3ef6d242012-01-18 16:23:48 -050037#include <linux/miscdevice.h>
James Smart7bb03bb2013-04-17 20:19:16 -040038#include <linux/percpu.h>
James Smart895427b2017-02-12 13:52:30 -080039#include <linux/msi.h>
James Smart6a828b02019-01-28 11:14:31 -080040#include <linux/irq.h>
Maurizio Lombardi286871a2017-08-23 16:55:48 -070041#include <linux/bitops.h>
James Smart31f06d22019-08-14 16:56:31 -070042#include <linux/crash_dump.h>
James Smartdcaa2132019-11-04 16:57:06 -080043#include <linux/cpu.h>
James Smart93a4d6f2019-11-04 16:57:05 -080044#include <linux/cpuhotplug.h>
dea31012005-04-17 16:05:31 -050045
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -040046#include <scsi/scsi.h>
dea31012005-04-17 16:05:31 -050047#include <scsi/scsi_device.h>
48#include <scsi/scsi_host.h>
49#include <scsi/scsi_transport_fc.h>
James Smart86c67372017-04-21 16:05:04 -070050#include <scsi/scsi_tcq.h>
51#include <scsi/fc/fc_fs.h>
52
James Smartda0436e2009-05-22 14:51:39 -040053#include "lpfc_hw4.h"
dea31012005-04-17 16:05:31 -050054#include "lpfc_hw.h"
55#include "lpfc_sli.h"
James Smartda0436e2009-05-22 14:51:39 -040056#include "lpfc_sli4.h"
James Smartea2151b2008-09-07 11:52:10 -040057#include "lpfc_nl.h"
dea31012005-04-17 16:05:31 -050058#include "lpfc_disc.h"
dea31012005-04-17 16:05:31 -050059#include "lpfc.h"
James Smart895427b2017-02-12 13:52:30 -080060#include "lpfc_scsi.h"
61#include "lpfc_nvme.h"
dea31012005-04-17 16:05:31 -050062#include "lpfc_logmsg.h"
63#include "lpfc_crtn.h"
James Smart92d7f7b2007-06-17 19:56:38 -050064#include "lpfc_vport.h"
dea31012005-04-17 16:05:31 -050065#include "lpfc_version.h"
James Smart12f44452016-07-06 12:36:03 -070066#include "lpfc_ids.h"
dea31012005-04-17 16:05:31 -050067
James Smart93a4d6f2019-11-04 16:57:05 -080068static enum cpuhp_state lpfc_cpuhp_state;
James Smart7bb03bb2013-04-17 20:19:16 -040069/* Used when mapping IRQ vectors in a driver centric manner */
YueHaibingd7b761b2019-05-31 23:28:41 +080070static uint32_t lpfc_present_cpu;
James Smart7bb03bb2013-04-17 20:19:16 -040071
James Smart93a4d6f2019-11-04 16:57:05 -080072static void __lpfc_cpuhp_remove(struct lpfc_hba *phba);
73static void lpfc_cpuhp_remove(struct lpfc_hba *phba);
74static void lpfc_cpuhp_add(struct lpfc_hba *phba);
dea31012005-04-17 16:05:31 -050075static void lpfc_get_hba_model_desc(struct lpfc_hba *, uint8_t *, uint8_t *);
76static int lpfc_post_rcv_buf(struct lpfc_hba *);
James Smart5350d872011-10-10 21:33:49 -040077static int lpfc_sli4_queue_verify(struct lpfc_hba *);
James Smartda0436e2009-05-22 14:51:39 -040078static int lpfc_create_bootstrap_mbox(struct lpfc_hba *);
79static int lpfc_setup_endian_order(struct lpfc_hba *);
James Smartda0436e2009-05-22 14:51:39 -040080static void lpfc_destroy_bootstrap_mbox(struct lpfc_hba *);
James Smart8a9d2e82012-05-09 21:16:12 -040081static void lpfc_free_els_sgl_list(struct lpfc_hba *);
James Smartf358dd02017-02-12 13:52:34 -080082static void lpfc_free_nvmet_sgl_list(struct lpfc_hba *);
James Smart8a9d2e82012-05-09 21:16:12 -040083static void lpfc_init_sgl_list(struct lpfc_hba *);
James Smartda0436e2009-05-22 14:51:39 -040084static int lpfc_init_active_sgl_array(struct lpfc_hba *);
85static void lpfc_free_active_sgl(struct lpfc_hba *);
86static int lpfc_hba_down_post_s3(struct lpfc_hba *phba);
87static int lpfc_hba_down_post_s4(struct lpfc_hba *phba);
88static int lpfc_sli4_cq_event_pool_create(struct lpfc_hba *);
89static void lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *);
90static void lpfc_sli4_cq_event_release_all(struct lpfc_hba *);
James Smart618a5232012-06-12 13:54:36 -040091static void lpfc_sli4_disable_intr(struct lpfc_hba *);
92static uint32_t lpfc_sli4_enable_intr(struct lpfc_hba *, uint32_t);
James Smart1ba981f2014-02-20 09:56:45 -050093static void lpfc_sli4_oas_verify(struct lpfc_hba *phba);
James Smart6a828b02019-01-28 11:14:31 -080094static uint16_t lpfc_find_cpu_handle(struct lpfc_hba *, uint16_t, int);
James Smartaa6ff302019-05-21 17:49:09 -070095static void lpfc_setup_bg(struct lpfc_hba *, struct Scsi_Host *);
dea31012005-04-17 16:05:31 -050096
97static struct scsi_transport_template *lpfc_transport_template = NULL;
James Smart92d7f7b2007-06-17 19:56:38 -050098static struct scsi_transport_template *lpfc_vport_transport_template = NULL;
dea31012005-04-17 16:05:31 -050099static DEFINE_IDR(lpfc_hba_index);
James Smartf358dd02017-02-12 13:52:34 -0800100#define LPFC_NVMET_BUF_POST 254
dea31012005-04-17 16:05:31 -0500101
James Smarte59058c2008-08-24 21:49:00 -0400102/**
James Smart3621a712009-04-06 18:47:14 -0400103 * lpfc_config_port_prep - Perform lpfc initialization prior to config port
James Smarte59058c2008-08-24 21:49:00 -0400104 * @phba: pointer to lpfc hba data structure.
105 *
106 * This routine will do LPFC initialization prior to issuing the CONFIG_PORT
107 * mailbox command. It retrieves the revision information from the HBA and
108 * collects the Vital Product Data (VPD) about the HBA for preparing the
109 * configuration of the HBA.
110 *
111 * Return codes:
112 * 0 - success.
113 * -ERESTART - requests the SLI layer to reset the HBA and try again.
114 * Any other value - indicates an error.
115 **/
dea31012005-04-17 16:05:31 -0500116int
James Smart2e0fef82007-06-17 19:56:36 -0500117lpfc_config_port_prep(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500118{
119 lpfc_vpd_t *vp = &phba->vpd;
120 int i = 0, rc;
121 LPFC_MBOXQ_t *pmb;
122 MAILBOX_t *mb;
123 char *lpfc_vpd_data = NULL;
124 uint16_t offset = 0;
125 static char licensed[56] =
126 "key unlock for use with gnu public licensed code only\0";
James Smart65a29c12006-07-06 15:50:50 -0400127 static int init_key = 1;
dea31012005-04-17 16:05:31 -0500128
129 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
130 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -0500131 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500132 return -ENOMEM;
133 }
134
James Smart04c68492009-05-22 14:52:52 -0400135 mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -0500136 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -0500137
138 if (lpfc_is_LC_HBA(phba->pcidev->device)) {
James Smart65a29c12006-07-06 15:50:50 -0400139 if (init_key) {
140 uint32_t *ptext = (uint32_t *) licensed;
dea31012005-04-17 16:05:31 -0500141
James Smart65a29c12006-07-06 15:50:50 -0400142 for (i = 0; i < 56; i += sizeof (uint32_t), ptext++)
143 *ptext = cpu_to_be32(*ptext);
144 init_key = 0;
145 }
dea31012005-04-17 16:05:31 -0500146
147 lpfc_read_nv(phba, pmb);
148 memset((char*)mb->un.varRDnvp.rsvd3, 0,
149 sizeof (mb->un.varRDnvp.rsvd3));
150 memcpy((char*)mb->un.varRDnvp.rsvd3, licensed,
151 sizeof (licensed));
152
153 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
154
155 if (rc != MBX_SUCCESS) {
Dick Kennedy372c1872020-06-30 14:50:00 -0700156 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarte8b62012007-08-02 11:10:09 -0400157 "0324 Config Port initialization "
dea31012005-04-17 16:05:31 -0500158 "error, mbxCmd x%x READ_NVPARM, "
159 "mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500160 mb->mbxCommand, mb->mbxStatus);
161 mempool_free(pmb, phba->mbox_mem_pool);
162 return -ERESTART;
163 }
164 memcpy(phba->wwnn, (char *)mb->un.varRDnvp.nodename,
James Smart2e0fef82007-06-17 19:56:36 -0500165 sizeof(phba->wwnn));
166 memcpy(phba->wwpn, (char *)mb->un.varRDnvp.portname,
167 sizeof(phba->wwpn));
dea31012005-04-17 16:05:31 -0500168 }
169
Martin Wilckdfb75132018-11-12 09:58:37 +0100170 /*
171 * Clear all option bits except LPFC_SLI3_BG_ENABLED,
172 * which was already set in lpfc_get_cfgparam()
173 */
174 phba->sli3_options &= (uint32_t)LPFC_SLI3_BG_ENABLED;
James Smart92d7f7b2007-06-17 19:56:38 -0500175
dea31012005-04-17 16:05:31 -0500176 /* Setup and issue mailbox READ REV command */
177 lpfc_read_rev(phba, pmb);
178 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
179 if (rc != MBX_SUCCESS) {
Dick Kennedy372c1872020-06-30 14:50:00 -0700180 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarte8b62012007-08-02 11:10:09 -0400181 "0439 Adapter failed to init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500182 "READ_REV, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500183 mb->mbxCommand, mb->mbxStatus);
184 mempool_free( pmb, phba->mbox_mem_pool);
185 return -ERESTART;
186 }
187
James Smart92d7f7b2007-06-17 19:56:38 -0500188
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500189 /*
190 * The value of rr must be 1 since the driver set the cv field to 1.
191 * This setting requires the FW to set all revision fields.
dea31012005-04-17 16:05:31 -0500192 */
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500193 if (mb->un.varRdRev.rr == 0) {
dea31012005-04-17 16:05:31 -0500194 vp->rev.rBit = 0;
Dick Kennedy372c1872020-06-30 14:50:00 -0700195 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarte8b62012007-08-02 11:10:09 -0400196 "0440 Adapter failed to init, READ_REV has "
197 "missing revision information.\n");
dea31012005-04-17 16:05:31 -0500198 mempool_free(pmb, phba->mbox_mem_pool);
199 return -ERESTART;
dea31012005-04-17 16:05:31 -0500200 }
201
James Smart495a7142008-06-14 22:52:59 -0400202 if (phba->sli_rev == 3 && !mb->un.varRdRev.v3rsp) {
203 mempool_free(pmb, phba->mbox_mem_pool);
James Smarted957682007-06-17 19:56:37 -0500204 return -EINVAL;
James Smart495a7142008-06-14 22:52:59 -0400205 }
James Smarted957682007-06-17 19:56:37 -0500206
dea31012005-04-17 16:05:31 -0500207 /* Save information as VPD data */
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500208 vp->rev.rBit = 1;
James Smart92d7f7b2007-06-17 19:56:38 -0500209 memcpy(&vp->sli3Feat, &mb->un.varRdRev.sli3Feat, sizeof(uint32_t));
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500210 vp->rev.sli1FwRev = mb->un.varRdRev.sli1FwRev;
211 memcpy(vp->rev.sli1FwName, (char*) mb->un.varRdRev.sli1FwName, 16);
212 vp->rev.sli2FwRev = mb->un.varRdRev.sli2FwRev;
213 memcpy(vp->rev.sli2FwName, (char *) mb->un.varRdRev.sli2FwName, 16);
dea31012005-04-17 16:05:31 -0500214 vp->rev.biuRev = mb->un.varRdRev.biuRev;
215 vp->rev.smRev = mb->un.varRdRev.smRev;
216 vp->rev.smFwRev = mb->un.varRdRev.un.smFwRev;
217 vp->rev.endecRev = mb->un.varRdRev.endecRev;
218 vp->rev.fcphHigh = mb->un.varRdRev.fcphHigh;
219 vp->rev.fcphLow = mb->un.varRdRev.fcphLow;
220 vp->rev.feaLevelHigh = mb->un.varRdRev.feaLevelHigh;
221 vp->rev.feaLevelLow = mb->un.varRdRev.feaLevelLow;
222 vp->rev.postKernRev = mb->un.varRdRev.postKernRev;
223 vp->rev.opFwRev = mb->un.varRdRev.opFwRev;
224
James Smart92d7f7b2007-06-17 19:56:38 -0500225 /* If the sli feature level is less then 9, we must
226 * tear down all RPIs and VPIs on link down if NPIV
227 * is enabled.
228 */
229 if (vp->rev.feaLevelHigh < 9)
230 phba->sli3_options |= LPFC_SLI3_VPORT_TEARDOWN;
231
dea31012005-04-17 16:05:31 -0500232 if (lpfc_is_LC_HBA(phba->pcidev->device))
233 memcpy(phba->RandomData, (char *)&mb->un.varWords[24],
234 sizeof (phba->RandomData));
235
dea31012005-04-17 16:05:31 -0500236 /* Get adapter VPD information */
dea31012005-04-17 16:05:31 -0500237 lpfc_vpd_data = kmalloc(DMP_VPD_SIZE, GFP_KERNEL);
238 if (!lpfc_vpd_data)
James Smartd7c255b2008-08-24 21:50:00 -0400239 goto out_free_mbox;
dea31012005-04-17 16:05:31 -0500240 do {
James Smarta0c87cb2009-07-19 10:01:10 -0400241 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_VPD);
dea31012005-04-17 16:05:31 -0500242 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
243
244 if (rc != MBX_SUCCESS) {
245 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400246 "0441 VPD not present on adapter, "
dea31012005-04-17 16:05:31 -0500247 "mbxCmd x%x DUMP VPD, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500248 mb->mbxCommand, mb->mbxStatus);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500249 mb->un.varDmp.word_cnt = 0;
dea31012005-04-17 16:05:31 -0500250 }
James Smart04c68492009-05-22 14:52:52 -0400251 /* dump mem may return a zero when finished or we got a
252 * mailbox error, either way we are done.
253 */
254 if (mb->un.varDmp.word_cnt == 0)
255 break;
Dick Kennedyd91e3ab2020-06-30 14:49:52 -0700256
257 i = mb->un.varDmp.word_cnt * sizeof(uint32_t);
258 if (offset + i > DMP_VPD_SIZE)
259 i = DMP_VPD_SIZE - offset;
James Smartd7c255b2008-08-24 21:50:00 -0400260 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
Dick Kennedyd91e3ab2020-06-30 14:49:52 -0700261 lpfc_vpd_data + offset, i);
262 offset += i;
263 } while (offset < DMP_VPD_SIZE);
264
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500265 lpfc_parse_vpd(phba, lpfc_vpd_data, offset);
dea31012005-04-17 16:05:31 -0500266
267 kfree(lpfc_vpd_data);
dea31012005-04-17 16:05:31 -0500268out_free_mbox:
269 mempool_free(pmb, phba->mbox_mem_pool);
270 return 0;
271}
272
James Smarte59058c2008-08-24 21:49:00 -0400273/**
James Smart3621a712009-04-06 18:47:14 -0400274 * lpfc_config_async_cmpl - Completion handler for config async event mbox cmd
James Smarte59058c2008-08-24 21:49:00 -0400275 * @phba: pointer to lpfc hba data structure.
276 * @pmboxq: pointer to the driver internal queue element for mailbox command.
277 *
278 * This is the completion handler for driver's configuring asynchronous event
279 * mailbox command to the device. If the mailbox command returns successfully,
280 * it will set internal async event support flag to 1; otherwise, it will
281 * set internal async event support flag to 0.
282 **/
James Smart57127f12007-10-27 13:37:05 -0400283static void
284lpfc_config_async_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
285{
James Smart04c68492009-05-22 14:52:52 -0400286 if (pmboxq->u.mb.mbxStatus == MBX_SUCCESS)
James Smart57127f12007-10-27 13:37:05 -0400287 phba->temp_sensor_support = 1;
288 else
289 phba->temp_sensor_support = 0;
290 mempool_free(pmboxq, phba->mbox_mem_pool);
291 return;
292}
293
James Smarte59058c2008-08-24 21:49:00 -0400294/**
James Smart3621a712009-04-06 18:47:14 -0400295 * lpfc_dump_wakeup_param_cmpl - dump memory mailbox command completion handler
James Smart97207482008-12-04 22:39:19 -0500296 * @phba: pointer to lpfc hba data structure.
297 * @pmboxq: pointer to the driver internal queue element for mailbox command.
298 *
299 * This is the completion handler for dump mailbox command for getting
300 * wake up parameters. When this command complete, the response contain
301 * Option rom version of the HBA. This function translate the version number
302 * into a human readable string and store it in OptionROMVersion.
303 **/
304static void
305lpfc_dump_wakeup_param_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
306{
307 struct prog_id *prg;
308 uint32_t prog_id_word;
309 char dist = ' ';
310 /* character array used for decoding dist type. */
311 char dist_char[] = "nabx";
312
James Smart04c68492009-05-22 14:52:52 -0400313 if (pmboxq->u.mb.mbxStatus != MBX_SUCCESS) {
James Smart9f1e1b52008-12-04 22:39:40 -0500314 mempool_free(pmboxq, phba->mbox_mem_pool);
James Smart97207482008-12-04 22:39:19 -0500315 return;
James Smart9f1e1b52008-12-04 22:39:40 -0500316 }
James Smart97207482008-12-04 22:39:19 -0500317
318 prg = (struct prog_id *) &prog_id_word;
319
320 /* word 7 contain option rom version */
James Smart04c68492009-05-22 14:52:52 -0400321 prog_id_word = pmboxq->u.mb.un.varWords[7];
James Smart97207482008-12-04 22:39:19 -0500322
323 /* Decode the Option rom version word to a readable string */
324 if (prg->dist < 4)
325 dist = dist_char[prg->dist];
326
327 if ((prg->dist == 3) && (prg->num == 0))
James Smarta2fc4aef2014-09-03 12:57:55 -0400328 snprintf(phba->OptionROMVersion, 32, "%d.%d%d",
James Smart97207482008-12-04 22:39:19 -0500329 prg->ver, prg->rev, prg->lev);
330 else
James Smarta2fc4aef2014-09-03 12:57:55 -0400331 snprintf(phba->OptionROMVersion, 32, "%d.%d%d%c%d",
James Smart97207482008-12-04 22:39:19 -0500332 prg->ver, prg->rev, prg->lev,
333 dist, prg->num);
James Smart9f1e1b52008-12-04 22:39:40 -0500334 mempool_free(pmboxq, phba->mbox_mem_pool);
James Smart97207482008-12-04 22:39:19 -0500335 return;
336}
337
338/**
James Smart05580562011-05-24 11:40:48 -0400339 * lpfc_update_vport_wwn - Updates the fc_nodename, fc_portname,
340 * cfg_soft_wwnn, cfg_soft_wwpn
341 * @vport: pointer to lpfc vport data structure.
342 *
343 *
344 * Return codes
345 * None.
346 **/
347void
348lpfc_update_vport_wwn(struct lpfc_vport *vport)
349{
James Smartaeb3c812017-04-21 16:05:02 -0700350 uint8_t vvvl = vport->fc_sparam.cmn.valid_vendor_ver_level;
351 u32 *fawwpn_key = (u32 *)&vport->fc_sparam.un.vendorVersion[0];
352
James Smart05580562011-05-24 11:40:48 -0400353 /* If the soft name exists then update it using the service params */
354 if (vport->phba->cfg_soft_wwnn)
355 u64_to_wwn(vport->phba->cfg_soft_wwnn,
356 vport->fc_sparam.nodeName.u.wwn);
357 if (vport->phba->cfg_soft_wwpn)
358 u64_to_wwn(vport->phba->cfg_soft_wwpn,
359 vport->fc_sparam.portName.u.wwn);
360
361 /*
362 * If the name is empty or there exists a soft name
363 * then copy the service params name, otherwise use the fc name
364 */
365 if (vport->fc_nodename.u.wwn[0] == 0 || vport->phba->cfg_soft_wwnn)
366 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
367 sizeof(struct lpfc_name));
368 else
369 memcpy(&vport->fc_sparam.nodeName, &vport->fc_nodename,
370 sizeof(struct lpfc_name));
371
James Smartaeb3c812017-04-21 16:05:02 -0700372 /*
373 * If the port name has changed, then set the Param changes flag
374 * to unreg the login
375 */
376 if (vport->fc_portname.u.wwn[0] != 0 &&
377 memcmp(&vport->fc_portname, &vport->fc_sparam.portName,
378 sizeof(struct lpfc_name)))
379 vport->vport_flag |= FAWWPN_PARAM_CHG;
380
381 if (vport->fc_portname.u.wwn[0] == 0 ||
382 vport->phba->cfg_soft_wwpn ||
383 (vvvl == 1 && cpu_to_be32(*fawwpn_key) == FAPWWN_KEY_VENDOR) ||
384 vport->vport_flag & FAWWPN_SET) {
James Smart05580562011-05-24 11:40:48 -0400385 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
386 sizeof(struct lpfc_name));
James Smartaeb3c812017-04-21 16:05:02 -0700387 vport->vport_flag &= ~FAWWPN_SET;
388 if (vvvl == 1 && cpu_to_be32(*fawwpn_key) == FAPWWN_KEY_VENDOR)
389 vport->vport_flag |= FAWWPN_SET;
390 }
James Smart05580562011-05-24 11:40:48 -0400391 else
392 memcpy(&vport->fc_sparam.portName, &vport->fc_portname,
393 sizeof(struct lpfc_name));
394}
395
396/**
James Smart3621a712009-04-06 18:47:14 -0400397 * lpfc_config_port_post - Perform lpfc initialization after config port
James Smarte59058c2008-08-24 21:49:00 -0400398 * @phba: pointer to lpfc hba data structure.
399 *
400 * This routine will do LPFC initialization after the CONFIG_PORT mailbox
401 * command call. It performs all internal resource and state setups on the
402 * port: post IOCB buffers, enable appropriate host interrupt attentions,
403 * ELS ring timers, etc.
404 *
405 * Return codes
406 * 0 - success.
407 * Any other value - error.
408 **/
dea31012005-04-17 16:05:31 -0500409int
James Smart2e0fef82007-06-17 19:56:36 -0500410lpfc_config_port_post(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500411{
James Smart2e0fef82007-06-17 19:56:36 -0500412 struct lpfc_vport *vport = phba->pport;
James Smarta257bf92009-04-06 18:48:10 -0400413 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -0500414 LPFC_MBOXQ_t *pmb;
415 MAILBOX_t *mb;
416 struct lpfc_dmabuf *mp;
417 struct lpfc_sli *psli = &phba->sli;
418 uint32_t status, timeout;
James Smart2e0fef82007-06-17 19:56:36 -0500419 int i, j;
420 int rc;
dea31012005-04-17 16:05:31 -0500421
James Smart7af67052007-10-27 13:38:11 -0400422 spin_lock_irq(&phba->hbalock);
423 /*
424 * If the Config port completed correctly the HBA is not
425 * over heated any more.
426 */
427 if (phba->over_temp_state == HBA_OVER_TEMP)
428 phba->over_temp_state = HBA_NORMAL_TEMP;
429 spin_unlock_irq(&phba->hbalock);
430
dea31012005-04-17 16:05:31 -0500431 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
432 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -0500433 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500434 return -ENOMEM;
435 }
James Smart04c68492009-05-22 14:52:52 -0400436 mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -0500437
dea31012005-04-17 16:05:31 -0500438 /* Get login parameters for NID. */
James Smart9f1177a2010-02-26 14:12:57 -0500439 rc = lpfc_read_sparam(phba, pmb, 0);
440 if (rc) {
441 mempool_free(pmb, phba->mbox_mem_pool);
442 return -ENOMEM;
443 }
444
James Smarted957682007-06-17 19:56:37 -0500445 pmb->vport = vport;
dea31012005-04-17 16:05:31 -0500446 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
Dick Kennedy372c1872020-06-30 14:50:00 -0700447 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarte8b62012007-08-02 11:10:09 -0400448 "0448 Adapter failed init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500449 "READ_SPARM mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500450 mb->mbxCommand, mb->mbxStatus);
James Smart2e0fef82007-06-17 19:56:36 -0500451 phba->link_state = LPFC_HBA_ERROR;
James Smart3e1f0712018-11-29 16:09:29 -0800452 mp = (struct lpfc_dmabuf *)pmb->ctx_buf;
James Smart9f1177a2010-02-26 14:12:57 -0500453 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500454 lpfc_mbuf_free(phba, mp->virt, mp->phys);
455 kfree(mp);
456 return -EIO;
457 }
458
James Smart3e1f0712018-11-29 16:09:29 -0800459 mp = (struct lpfc_dmabuf *)pmb->ctx_buf;
dea31012005-04-17 16:05:31 -0500460
James Smart2e0fef82007-06-17 19:56:36 -0500461 memcpy(&vport->fc_sparam, mp->virt, sizeof (struct serv_parm));
dea31012005-04-17 16:05:31 -0500462 lpfc_mbuf_free(phba, mp->virt, mp->phys);
463 kfree(mp);
James Smart3e1f0712018-11-29 16:09:29 -0800464 pmb->ctx_buf = NULL;
James Smart05580562011-05-24 11:40:48 -0400465 lpfc_update_vport_wwn(vport);
James Smarta257bf92009-04-06 18:48:10 -0400466
467 /* Update the fc_host data structures with new wwn. */
468 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
469 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
James Smart21e9a0a2009-05-22 14:53:21 -0400470 fc_host_max_npiv_vports(shost) = phba->max_vpi;
James Smarta257bf92009-04-06 18:48:10 -0400471
dea31012005-04-17 16:05:31 -0500472 /* If no serial number in VPD data, use low 6 bytes of WWNN */
473 /* This should be consolidated into parse_vpd ? - mr */
474 if (phba->SerialNumber[0] == 0) {
475 uint8_t *outptr;
476
James Smart2e0fef82007-06-17 19:56:36 -0500477 outptr = &vport->fc_nodename.u.s.IEEE[0];
dea31012005-04-17 16:05:31 -0500478 for (i = 0; i < 12; i++) {
479 status = *outptr++;
480 j = ((status & 0xf0) >> 4);
481 if (j <= 9)
482 phba->SerialNumber[i] =
483 (char)((uint8_t) 0x30 + (uint8_t) j);
484 else
485 phba->SerialNumber[i] =
486 (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
487 i++;
488 j = (status & 0xf);
489 if (j <= 9)
490 phba->SerialNumber[i] =
491 (char)((uint8_t) 0x30 + (uint8_t) j);
492 else
493 phba->SerialNumber[i] =
494 (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
495 }
496 }
497
dea31012005-04-17 16:05:31 -0500498 lpfc_read_config(phba, pmb);
James Smarted957682007-06-17 19:56:37 -0500499 pmb->vport = vport;
dea31012005-04-17 16:05:31 -0500500 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
Dick Kennedy372c1872020-06-30 14:50:00 -0700501 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarte8b62012007-08-02 11:10:09 -0400502 "0453 Adapter failed to init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500503 "READ_CONFIG, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500504 mb->mbxCommand, mb->mbxStatus);
James Smart2e0fef82007-06-17 19:56:36 -0500505 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500506 mempool_free( pmb, phba->mbox_mem_pool);
507 return -EIO;
508 }
509
James Smarta0c87cb2009-07-19 10:01:10 -0400510 /* Check if the port is disabled */
511 lpfc_sli_read_link_ste(phba);
512
dea31012005-04-17 16:05:31 -0500513 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
James Smartf6770e72020-01-27 16:23:09 -0800514 if (phba->cfg_hba_queue_depth > mb->un.varRdConfig.max_xri) {
James Smart572709e2013-07-15 18:32:43 -0400515 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
516 "3359 HBA queue depth changed from %d to %d\n",
James Smartf6770e72020-01-27 16:23:09 -0800517 phba->cfg_hba_queue_depth,
518 mb->un.varRdConfig.max_xri);
519 phba->cfg_hba_queue_depth = mb->un.varRdConfig.max_xri;
James Smart572709e2013-07-15 18:32:43 -0400520 }
dea31012005-04-17 16:05:31 -0500521
522 phba->lmt = mb->un.varRdConfig.lmt;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500523
524 /* Get the default values for Model Name and Description */
525 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
526
James Smart2e0fef82007-06-17 19:56:36 -0500527 phba->link_state = LPFC_LINK_DOWN;
dea31012005-04-17 16:05:31 -0500528
James Smart0b727fe2007-10-27 13:37:25 -0400529 /* Only process IOCBs on ELS ring till hba_state is READY */
James Smart895427b2017-02-12 13:52:30 -0800530 if (psli->sli3_ring[LPFC_EXTRA_RING].sli.sli3.cmdringaddr)
531 psli->sli3_ring[LPFC_EXTRA_RING].flag |= LPFC_STOP_IOCB_EVENT;
532 if (psli->sli3_ring[LPFC_FCP_RING].sli.sli3.cmdringaddr)
533 psli->sli3_ring[LPFC_FCP_RING].flag |= LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -0500534
535 /* Post receive buffers for desired rings */
James Smarted957682007-06-17 19:56:37 -0500536 if (phba->sli_rev != 3)
537 lpfc_post_rcv_buf(phba);
dea31012005-04-17 16:05:31 -0500538
James Smart93996272008-08-24 21:50:30 -0400539 /*
540 * Configure HBA MSI-X attention conditions to messages if MSI-X mode
541 */
542 if (phba->intr_type == MSIX) {
543 rc = lpfc_config_msi(phba, pmb);
544 if (rc) {
545 mempool_free(pmb, phba->mbox_mem_pool);
546 return -EIO;
547 }
548 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
549 if (rc != MBX_SUCCESS) {
Dick Kennedy372c1872020-06-30 14:50:00 -0700550 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart93996272008-08-24 21:50:30 -0400551 "0352 Config MSI mailbox command "
552 "failed, mbxCmd x%x, mbxStatus x%x\n",
James Smart04c68492009-05-22 14:52:52 -0400553 pmb->u.mb.mbxCommand,
554 pmb->u.mb.mbxStatus);
James Smart93996272008-08-24 21:50:30 -0400555 mempool_free(pmb, phba->mbox_mem_pool);
556 return -EIO;
557 }
558 }
559
James Smart04c68492009-05-22 14:52:52 -0400560 spin_lock_irq(&phba->hbalock);
James Smart93996272008-08-24 21:50:30 -0400561 /* Initialize ERATT handling flag */
562 phba->hba_flag &= ~HBA_ERATT_HANDLED;
563
dea31012005-04-17 16:05:31 -0500564 /* Enable appropriate host interrupts */
James Smart9940b972011-03-11 16:06:12 -0500565 if (lpfc_readl(phba->HCregaddr, &status)) {
566 spin_unlock_irq(&phba->hbalock);
567 return -EIO;
568 }
dea31012005-04-17 16:05:31 -0500569 status |= HC_MBINT_ENA | HC_ERINT_ENA | HC_LAINT_ENA;
570 if (psli->num_rings > 0)
571 status |= HC_R0INT_ENA;
572 if (psli->num_rings > 1)
573 status |= HC_R1INT_ENA;
574 if (psli->num_rings > 2)
575 status |= HC_R2INT_ENA;
576 if (psli->num_rings > 3)
577 status |= HC_R3INT_ENA;
578
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500579 if ((phba->cfg_poll & ENABLE_FCP_RING_POLLING) &&
580 (phba->cfg_poll & DISABLE_FCP_RING_INT))
James Smart93996272008-08-24 21:50:30 -0400581 status &= ~(HC_R0INT_ENA);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500582
dea31012005-04-17 16:05:31 -0500583 writel(status, phba->HCregaddr);
584 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -0500585 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500586
James Smart93996272008-08-24 21:50:30 -0400587 /* Set up ring-0 (ELS) timer */
588 timeout = phba->fc_ratov * 2;
James Smart256ec0d2013-04-17 20:14:58 -0400589 mod_timer(&vport->els_tmofunc,
590 jiffies + msecs_to_jiffies(1000 * timeout));
James Smart93996272008-08-24 21:50:30 -0400591 /* Set up heart beat (HB) timer */
James Smart256ec0d2013-04-17 20:14:58 -0400592 mod_timer(&phba->hb_tmofunc,
593 jiffies + msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smart858c9f62007-06-17 19:56:39 -0500594 phba->hb_outstanding = 0;
595 phba->last_completion_time = jiffies;
James Smart93996272008-08-24 21:50:30 -0400596 /* Set up error attention (ERATT) polling timer */
James Smart256ec0d2013-04-17 20:14:58 -0400597 mod_timer(&phba->eratt_poll,
James Smart65791f12016-07-06 12:35:56 -0700598 jiffies + msecs_to_jiffies(1000 * phba->eratt_poll_interval));
dea31012005-04-17 16:05:31 -0500599
James Smarta0c87cb2009-07-19 10:01:10 -0400600 if (phba->hba_flag & LINK_DISABLED) {
Dick Kennedy372c1872020-06-30 14:50:00 -0700601 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
602 "2598 Adapter Link is disabled.\n");
James Smarta0c87cb2009-07-19 10:01:10 -0400603 lpfc_down_link(phba, pmb);
604 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
605 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
606 if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
Dick Kennedy372c1872020-06-30 14:50:00 -0700607 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
608 "2599 Adapter failed to issue DOWN_LINK"
609 " mbox command rc 0x%x\n", rc);
James Smarta0c87cb2009-07-19 10:01:10 -0400610
611 mempool_free(pmb, phba->mbox_mem_pool);
612 return -EIO;
613 }
James Smarte40a02c2010-02-26 14:13:54 -0500614 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
James Smart026abb82011-12-13 13:20:45 -0500615 mempool_free(pmb, phba->mbox_mem_pool);
616 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
617 if (rc)
618 return rc;
dea31012005-04-17 16:05:31 -0500619 }
620 /* MBOX buffer will be freed in mbox compl */
James Smart57127f12007-10-27 13:37:05 -0400621 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart9f1177a2010-02-26 14:12:57 -0500622 if (!pmb) {
623 phba->link_state = LPFC_HBA_ERROR;
624 return -ENOMEM;
625 }
626
James Smart57127f12007-10-27 13:37:05 -0400627 lpfc_config_async(phba, pmb, LPFC_ELS_RING);
628 pmb->mbox_cmpl = lpfc_config_async_cmpl;
629 pmb->vport = phba->pport;
630 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -0500631
James Smart57127f12007-10-27 13:37:05 -0400632 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
Dick Kennedy372c1872020-06-30 14:50:00 -0700633 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart57127f12007-10-27 13:37:05 -0400634 "0456 Adapter failed to issue "
James Smarte4e74272009-07-19 10:01:38 -0400635 "ASYNCEVT_ENABLE mbox status x%x\n",
James Smart57127f12007-10-27 13:37:05 -0400636 rc);
637 mempool_free(pmb, phba->mbox_mem_pool);
638 }
James Smart97207482008-12-04 22:39:19 -0500639
640 /* Get Option rom version */
641 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart9f1177a2010-02-26 14:12:57 -0500642 if (!pmb) {
643 phba->link_state = LPFC_HBA_ERROR;
644 return -ENOMEM;
645 }
646
James Smart97207482008-12-04 22:39:19 -0500647 lpfc_dump_wakeup_param(phba, pmb);
648 pmb->mbox_cmpl = lpfc_dump_wakeup_param_cmpl;
649 pmb->vport = phba->pport;
650 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
651
652 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
Dick Kennedy372c1872020-06-30 14:50:00 -0700653 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
654 "0435 Adapter failed "
James Smarte4e74272009-07-19 10:01:38 -0400655 "to get Option ROM version status x%x\n", rc);
James Smart97207482008-12-04 22:39:19 -0500656 mempool_free(pmb, phba->mbox_mem_pool);
657 }
658
James Smartd7c255b2008-08-24 21:50:00 -0400659 return 0;
James Smartce8b3ce2006-07-06 15:50:36 -0400660}
661
James Smarte59058c2008-08-24 21:49:00 -0400662/**
James Smart84d1b002010-02-12 14:42:33 -0500663 * lpfc_hba_init_link - Initialize the FC link
664 * @phba: pointer to lpfc hba data structure.
James Smart6e7288d2010-06-07 15:23:35 -0400665 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
James Smart84d1b002010-02-12 14:42:33 -0500666 *
667 * This routine will issue the INIT_LINK mailbox command call.
668 * It is available to other drivers through the lpfc_hba data
669 * structure for use as a delayed link up mechanism with the
670 * module parameter lpfc_suppress_link_up.
671 *
672 * Return code
673 * 0 - success
674 * Any other value - error
675 **/
Rashika Kheriae399b222014-09-03 12:55:28 -0400676static int
James Smart6e7288d2010-06-07 15:23:35 -0400677lpfc_hba_init_link(struct lpfc_hba *phba, uint32_t flag)
James Smart84d1b002010-02-12 14:42:33 -0500678{
James Smart1b511972011-12-13 13:23:09 -0500679 return lpfc_hba_init_link_fc_topology(phba, phba->cfg_topology, flag);
680}
681
682/**
683 * lpfc_hba_init_link_fc_topology - Initialize FC link with desired topology
684 * @phba: pointer to lpfc hba data structure.
685 * @fc_topology: desired fc topology.
686 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
687 *
688 * This routine will issue the INIT_LINK mailbox command call.
689 * It is available to other drivers through the lpfc_hba data
690 * structure for use as a delayed link up mechanism with the
691 * module parameter lpfc_suppress_link_up.
692 *
693 * Return code
694 * 0 - success
695 * Any other value - error
696 **/
697int
698lpfc_hba_init_link_fc_topology(struct lpfc_hba *phba, uint32_t fc_topology,
699 uint32_t flag)
700{
James Smart84d1b002010-02-12 14:42:33 -0500701 struct lpfc_vport *vport = phba->pport;
702 LPFC_MBOXQ_t *pmb;
703 MAILBOX_t *mb;
704 int rc;
705
706 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
707 if (!pmb) {
708 phba->link_state = LPFC_HBA_ERROR;
709 return -ENOMEM;
710 }
711 mb = &pmb->u.mb;
712 pmb->vport = vport;
713
James Smart026abb82011-12-13 13:20:45 -0500714 if ((phba->cfg_link_speed > LPFC_USER_LINK_SPEED_MAX) ||
715 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_1G) &&
716 !(phba->lmt & LMT_1Gb)) ||
717 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_2G) &&
718 !(phba->lmt & LMT_2Gb)) ||
719 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_4G) &&
720 !(phba->lmt & LMT_4Gb)) ||
721 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_8G) &&
722 !(phba->lmt & LMT_8Gb)) ||
723 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_10G) &&
724 !(phba->lmt & LMT_10Gb)) ||
725 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_16G) &&
James Smartd38dd522015-08-31 16:48:17 -0400726 !(phba->lmt & LMT_16Gb)) ||
727 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_32G) &&
James Smartfbd8a6b2018-02-22 08:18:45 -0800728 !(phba->lmt & LMT_32Gb)) ||
729 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_64G) &&
730 !(phba->lmt & LMT_64Gb))) {
James Smart026abb82011-12-13 13:20:45 -0500731 /* Reset link speed to auto */
Dick Kennedy372c1872020-06-30 14:50:00 -0700732 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
733 "1302 Invalid speed for this board:%d "
734 "Reset link speed to auto.\n",
735 phba->cfg_link_speed);
James Smart026abb82011-12-13 13:20:45 -0500736 phba->cfg_link_speed = LPFC_USER_LINK_SPEED_AUTO;
737 }
James Smart1b511972011-12-13 13:23:09 -0500738 lpfc_init_link(phba, pmb, fc_topology, phba->cfg_link_speed);
James Smart84d1b002010-02-12 14:42:33 -0500739 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart1b511972011-12-13 13:23:09 -0500740 if (phba->sli_rev < LPFC_SLI_REV4)
741 lpfc_set_loopback_flag(phba);
James Smart6e7288d2010-06-07 15:23:35 -0400742 rc = lpfc_sli_issue_mbox(phba, pmb, flag);
James Smart76a95d72010-11-20 23:11:48 -0500743 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
Dick Kennedy372c1872020-06-30 14:50:00 -0700744 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
745 "0498 Adapter failed to init, mbxCmd x%x "
746 "INIT_LINK, mbxStatus x%x\n",
747 mb->mbxCommand, mb->mbxStatus);
James Smart76a95d72010-11-20 23:11:48 -0500748 if (phba->sli_rev <= LPFC_SLI_REV3) {
749 /* Clear all interrupt enable conditions */
750 writel(0, phba->HCregaddr);
751 readl(phba->HCregaddr); /* flush */
752 /* Clear all pending interrupts */
753 writel(0xffffffff, phba->HAregaddr);
754 readl(phba->HAregaddr); /* flush */
755 }
James Smart84d1b002010-02-12 14:42:33 -0500756 phba->link_state = LPFC_HBA_ERROR;
James Smart6e7288d2010-06-07 15:23:35 -0400757 if (rc != MBX_BUSY || flag == MBX_POLL)
James Smart84d1b002010-02-12 14:42:33 -0500758 mempool_free(pmb, phba->mbox_mem_pool);
759 return -EIO;
760 }
James Smarte40a02c2010-02-26 14:13:54 -0500761 phba->cfg_suppress_link_up = LPFC_INITIALIZE_LINK;
James Smart6e7288d2010-06-07 15:23:35 -0400762 if (flag == MBX_POLL)
763 mempool_free(pmb, phba->mbox_mem_pool);
James Smart84d1b002010-02-12 14:42:33 -0500764
765 return 0;
766}
767
768/**
769 * lpfc_hba_down_link - this routine downs the FC link
James Smart6e7288d2010-06-07 15:23:35 -0400770 * @phba: pointer to lpfc hba data structure.
771 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
James Smart84d1b002010-02-12 14:42:33 -0500772 *
773 * This routine will issue the DOWN_LINK mailbox command call.
774 * It is available to other drivers through the lpfc_hba data
775 * structure for use to stop the link.
776 *
777 * Return code
778 * 0 - success
779 * Any other value - error
780 **/
Rashika Kheriae399b222014-09-03 12:55:28 -0400781static int
James Smart6e7288d2010-06-07 15:23:35 -0400782lpfc_hba_down_link(struct lpfc_hba *phba, uint32_t flag)
James Smart84d1b002010-02-12 14:42:33 -0500783{
784 LPFC_MBOXQ_t *pmb;
785 int rc;
786
787 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
788 if (!pmb) {
789 phba->link_state = LPFC_HBA_ERROR;
790 return -ENOMEM;
791 }
792
Dick Kennedy372c1872020-06-30 14:50:00 -0700793 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
794 "0491 Adapter Link is disabled.\n");
James Smart84d1b002010-02-12 14:42:33 -0500795 lpfc_down_link(phba, pmb);
796 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart6e7288d2010-06-07 15:23:35 -0400797 rc = lpfc_sli_issue_mbox(phba, pmb, flag);
James Smart84d1b002010-02-12 14:42:33 -0500798 if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
Dick Kennedy372c1872020-06-30 14:50:00 -0700799 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
800 "2522 Adapter failed to issue DOWN_LINK"
801 " mbox command rc 0x%x\n", rc);
James Smart84d1b002010-02-12 14:42:33 -0500802
803 mempool_free(pmb, phba->mbox_mem_pool);
804 return -EIO;
805 }
James Smart6e7288d2010-06-07 15:23:35 -0400806 if (flag == MBX_POLL)
807 mempool_free(pmb, phba->mbox_mem_pool);
808
James Smart84d1b002010-02-12 14:42:33 -0500809 return 0;
810}
811
812/**
James Smart3621a712009-04-06 18:47:14 -0400813 * lpfc_hba_down_prep - Perform lpfc uninitialization prior to HBA reset
James Smarte59058c2008-08-24 21:49:00 -0400814 * @phba: pointer to lpfc HBA data structure.
815 *
816 * This routine will do LPFC uninitialization before the HBA is reset when
817 * bringing down the SLI Layer.
818 *
819 * Return codes
820 * 0 - success.
821 * Any other value - error.
822 **/
dea31012005-04-17 16:05:31 -0500823int
James Smart2e0fef82007-06-17 19:56:36 -0500824lpfc_hba_down_prep(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500825{
James Smart1b32f6a2008-02-08 18:49:39 -0500826 struct lpfc_vport **vports;
827 int i;
James Smart3772a992009-05-22 14:50:54 -0400828
829 if (phba->sli_rev <= LPFC_SLI_REV3) {
830 /* Disable interrupts */
831 writel(0, phba->HCregaddr);
832 readl(phba->HCregaddr); /* flush */
833 }
dea31012005-04-17 16:05:31 -0500834
James Smart1b32f6a2008-02-08 18:49:39 -0500835 if (phba->pport->load_flag & FC_UNLOADING)
836 lpfc_cleanup_discovery_resources(phba->pport);
837 else {
838 vports = lpfc_create_vport_work_array(phba);
839 if (vports != NULL)
James Smart3772a992009-05-22 14:50:54 -0400840 for (i = 0; i <= phba->max_vports &&
841 vports[i] != NULL; i++)
James Smart1b32f6a2008-02-08 18:49:39 -0500842 lpfc_cleanup_discovery_resources(vports[i]);
843 lpfc_destroy_vport_work_array(phba, vports);
James Smart7f5f3d02008-02-08 18:50:14 -0500844 }
845 return 0;
dea31012005-04-17 16:05:31 -0500846}
847
James Smarte59058c2008-08-24 21:49:00 -0400848/**
James Smart68e814f2014-05-21 08:04:59 -0400849 * lpfc_sli4_free_sp_events - Cleanup sp_queue_events to free
850 * rspiocb which got deferred
851 *
852 * @phba: pointer to lpfc HBA data structure.
853 *
854 * This routine will cleanup completed slow path events after HBA is reset
855 * when bringing down the SLI Layer.
856 *
857 *
858 * Return codes
859 * void.
860 **/
861static void
862lpfc_sli4_free_sp_events(struct lpfc_hba *phba)
863{
864 struct lpfc_iocbq *rspiocbq;
865 struct hbq_dmabuf *dmabuf;
866 struct lpfc_cq_event *cq_event;
867
868 spin_lock_irq(&phba->hbalock);
869 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
870 spin_unlock_irq(&phba->hbalock);
871
872 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
873 /* Get the response iocb from the head of work queue */
874 spin_lock_irq(&phba->hbalock);
875 list_remove_head(&phba->sli4_hba.sp_queue_event,
876 cq_event, struct lpfc_cq_event, list);
877 spin_unlock_irq(&phba->hbalock);
878
879 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
880 case CQE_CODE_COMPL_WQE:
881 rspiocbq = container_of(cq_event, struct lpfc_iocbq,
882 cq_event);
883 lpfc_sli_release_iocbq(phba, rspiocbq);
884 break;
885 case CQE_CODE_RECEIVE:
886 case CQE_CODE_RECEIVE_V1:
887 dmabuf = container_of(cq_event, struct hbq_dmabuf,
888 cq_event);
889 lpfc_in_buf_free(phba, &dmabuf->dbuf);
890 }
891 }
892}
893
894/**
James Smartbcece5f2014-04-04 13:51:34 -0400895 * lpfc_hba_free_post_buf - Perform lpfc uninitialization after HBA reset
896 * @phba: pointer to lpfc HBA data structure.
897 *
898 * This routine will cleanup posted ELS buffers after the HBA is reset
899 * when bringing down the SLI Layer.
900 *
901 *
902 * Return codes
903 * void.
904 **/
905static void
906lpfc_hba_free_post_buf(struct lpfc_hba *phba)
907{
908 struct lpfc_sli *psli = &phba->sli;
909 struct lpfc_sli_ring *pring;
910 struct lpfc_dmabuf *mp, *next_mp;
James Smart07eab622014-04-04 13:51:44 -0400911 LIST_HEAD(buflist);
912 int count;
James Smartbcece5f2014-04-04 13:51:34 -0400913
914 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
915 lpfc_sli_hbqbuf_free_all(phba);
916 else {
917 /* Cleanup preposted buffers on the ELS ring */
James Smart895427b2017-02-12 13:52:30 -0800918 pring = &psli->sli3_ring[LPFC_ELS_RING];
James Smart07eab622014-04-04 13:51:44 -0400919 spin_lock_irq(&phba->hbalock);
920 list_splice_init(&pring->postbufq, &buflist);
921 spin_unlock_irq(&phba->hbalock);
922
923 count = 0;
924 list_for_each_entry_safe(mp, next_mp, &buflist, list) {
James Smartbcece5f2014-04-04 13:51:34 -0400925 list_del(&mp->list);
James Smart07eab622014-04-04 13:51:44 -0400926 count++;
James Smartbcece5f2014-04-04 13:51:34 -0400927 lpfc_mbuf_free(phba, mp->virt, mp->phys);
928 kfree(mp);
929 }
James Smart07eab622014-04-04 13:51:44 -0400930
931 spin_lock_irq(&phba->hbalock);
932 pring->postbufq_cnt -= count;
James Smartbcece5f2014-04-04 13:51:34 -0400933 spin_unlock_irq(&phba->hbalock);
934 }
935}
936
937/**
938 * lpfc_hba_clean_txcmplq - Perform lpfc uninitialization after HBA reset
939 * @phba: pointer to lpfc HBA data structure.
940 *
941 * This routine will cleanup the txcmplq after the HBA is reset when bringing
942 * down the SLI Layer.
943 *
944 * Return codes
945 * void
946 **/
947static void
948lpfc_hba_clean_txcmplq(struct lpfc_hba *phba)
949{
950 struct lpfc_sli *psli = &phba->sli;
James Smart895427b2017-02-12 13:52:30 -0800951 struct lpfc_queue *qp = NULL;
James Smartbcece5f2014-04-04 13:51:34 -0400952 struct lpfc_sli_ring *pring;
953 LIST_HEAD(completions);
954 int i;
James Smartc1dd9112018-01-30 15:58:57 -0800955 struct lpfc_iocbq *piocb, *next_iocb;
James Smartbcece5f2014-04-04 13:51:34 -0400956
James Smart895427b2017-02-12 13:52:30 -0800957 if (phba->sli_rev != LPFC_SLI_REV4) {
958 for (i = 0; i < psli->num_rings; i++) {
959 pring = &psli->sli3_ring[i];
James Smartbcece5f2014-04-04 13:51:34 -0400960 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -0800961 /* At this point in time the HBA is either reset or DOA
962 * Nothing should be on txcmplq as it will
963 * NEVER complete.
964 */
965 list_splice_init(&pring->txcmplq, &completions);
966 pring->txcmplq_cnt = 0;
James Smartbcece5f2014-04-04 13:51:34 -0400967 spin_unlock_irq(&phba->hbalock);
968
James Smart895427b2017-02-12 13:52:30 -0800969 lpfc_sli_abort_iocb_ring(phba, pring);
970 }
James Smartbcece5f2014-04-04 13:51:34 -0400971 /* Cancel all the IOCBs from the completions list */
James Smart895427b2017-02-12 13:52:30 -0800972 lpfc_sli_cancel_iocbs(phba, &completions,
973 IOSTAT_LOCAL_REJECT, IOERR_SLI_ABORTED);
974 return;
975 }
976 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
977 pring = qp->pring;
978 if (!pring)
979 continue;
980 spin_lock_irq(&pring->ring_lock);
James Smartc1dd9112018-01-30 15:58:57 -0800981 list_for_each_entry_safe(piocb, next_iocb,
982 &pring->txcmplq, list)
983 piocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smart895427b2017-02-12 13:52:30 -0800984 list_splice_init(&pring->txcmplq, &completions);
985 pring->txcmplq_cnt = 0;
986 spin_unlock_irq(&pring->ring_lock);
James Smartbcece5f2014-04-04 13:51:34 -0400987 lpfc_sli_abort_iocb_ring(phba, pring);
988 }
James Smart895427b2017-02-12 13:52:30 -0800989 /* Cancel all the IOCBs from the completions list */
990 lpfc_sli_cancel_iocbs(phba, &completions,
991 IOSTAT_LOCAL_REJECT, IOERR_SLI_ABORTED);
James Smartbcece5f2014-04-04 13:51:34 -0400992}
993
994/**
James Smart3772a992009-05-22 14:50:54 -0400995 * lpfc_hba_down_post_s3 - Perform lpfc uninitialization after HBA reset
James Smarte59058c2008-08-24 21:49:00 -0400996 * @phba: pointer to lpfc HBA data structure.
997 *
998 * This routine will do uninitialization after the HBA is reset when bring
999 * down the SLI Layer.
1000 *
1001 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001002 * 0 - success.
James Smarte59058c2008-08-24 21:49:00 -04001003 * Any other value - error.
1004 **/
James Smart3772a992009-05-22 14:50:54 -04001005static int
1006lpfc_hba_down_post_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05001007{
James Smartbcece5f2014-04-04 13:51:34 -04001008 lpfc_hba_free_post_buf(phba);
1009 lpfc_hba_clean_txcmplq(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -05001010 return 0;
1011}
James Smart5af5eee2010-10-22 11:06:38 -04001012
James Smartda0436e2009-05-22 14:51:39 -04001013/**
1014 * lpfc_hba_down_post_s4 - Perform lpfc uninitialization after HBA reset
1015 * @phba: pointer to lpfc HBA data structure.
1016 *
1017 * This routine will do uninitialization after the HBA is reset when bring
1018 * down the SLI Layer.
1019 *
1020 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001021 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -04001022 * Any other value - error.
1023 **/
1024static int
1025lpfc_hba_down_post_s4(struct lpfc_hba *phba)
1026{
James Smartc4908502019-01-28 11:14:28 -08001027 struct lpfc_io_buf *psb, *psb_next;
James Smart7cacae22020-03-31 09:50:03 -07001028 struct lpfc_async_xchg_ctx *ctxp, *ctxp_next;
James Smart5e5b5112019-01-28 11:14:22 -08001029 struct lpfc_sli4_hdw_queue *qp;
James Smartda0436e2009-05-22 14:51:39 -04001030 LIST_HEAD(aborts);
James Smart895427b2017-02-12 13:52:30 -08001031 LIST_HEAD(nvme_aborts);
James Smart86c67372017-04-21 16:05:04 -07001032 LIST_HEAD(nvmet_aborts);
James Smart0f65ff62010-02-26 14:14:23 -05001033 struct lpfc_sglq *sglq_entry = NULL;
James Smart5e5b5112019-01-28 11:14:22 -08001034 int cnt, idx;
James Smart0f65ff62010-02-26 14:14:23 -05001035
James Smart895427b2017-02-12 13:52:30 -08001036
1037 lpfc_sli_hbqbuf_free_all(phba);
James Smartbcece5f2014-04-04 13:51:34 -04001038 lpfc_hba_clean_txcmplq(phba);
1039
James Smartda0436e2009-05-22 14:51:39 -04001040 /* At this point in time the HBA is either reset or DOA. Either
1041 * way, nothing should be on lpfc_abts_els_sgl_list, it needs to be
James Smart895427b2017-02-12 13:52:30 -08001042 * on the lpfc_els_sgl_list so that it can either be freed if the
James Smartda0436e2009-05-22 14:51:39 -04001043 * driver is unloading or reposted if the driver is restarting
1044 * the port.
1045 */
James Smart895427b2017-02-12 13:52:30 -08001046 spin_lock_irq(&phba->hbalock); /* required for lpfc_els_sgl_list and */
James Smartda0436e2009-05-22 14:51:39 -04001047 /* scsl_buf_list */
James Smart895427b2017-02-12 13:52:30 -08001048 /* sgl_list_lock required because worker thread uses this
James Smartda0436e2009-05-22 14:51:39 -04001049 * list.
1050 */
James Smart895427b2017-02-12 13:52:30 -08001051 spin_lock(&phba->sli4_hba.sgl_list_lock);
James Smart0f65ff62010-02-26 14:14:23 -05001052 list_for_each_entry(sglq_entry,
1053 &phba->sli4_hba.lpfc_abts_els_sgl_list, list)
1054 sglq_entry->state = SGL_FREED;
1055
James Smartda0436e2009-05-22 14:51:39 -04001056 list_splice_init(&phba->sli4_hba.lpfc_abts_els_sgl_list,
James Smart895427b2017-02-12 13:52:30 -08001057 &phba->sli4_hba.lpfc_els_sgl_list);
1058
James Smartf358dd02017-02-12 13:52:34 -08001059
James Smart895427b2017-02-12 13:52:30 -08001060 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smart5e5b5112019-01-28 11:14:22 -08001061
1062 /* abts_xxxx_buf_list_lock required because worker thread uses this
James Smartda0436e2009-05-22 14:51:39 -04001063 * list.
1064 */
James Smart5e5b5112019-01-28 11:14:22 -08001065 cnt = 0;
1066 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
1067 qp = &phba->sli4_hba.hdwq[idx];
1068
James Smartc00f62e2019-08-14 16:57:11 -07001069 spin_lock(&qp->abts_io_buf_list_lock);
1070 list_splice_init(&qp->lpfc_abts_io_buf_list,
James Smart895427b2017-02-12 13:52:30 -08001071 &aborts);
James Smart895427b2017-02-12 13:52:30 -08001072
James Smart0794d602019-01-28 11:14:19 -08001073 list_for_each_entry_safe(psb, psb_next, &aborts, list) {
James Smart86c67372017-04-21 16:05:04 -07001074 psb->pCmd = NULL;
1075 psb->status = IOSTAT_SUCCESS;
James Smartcf1a1d32017-12-08 17:18:03 -08001076 cnt++;
James Smart86c67372017-04-21 16:05:04 -07001077 }
James Smart5e5b5112019-01-28 11:14:22 -08001078 spin_lock(&qp->io_buf_list_put_lock);
1079 list_splice_init(&aborts, &qp->lpfc_io_buf_list_put);
1080 qp->put_io_bufs += qp->abts_scsi_io_bufs;
James Smartc00f62e2019-08-14 16:57:11 -07001081 qp->put_io_bufs += qp->abts_nvme_io_bufs;
James Smart5e5b5112019-01-28 11:14:22 -08001082 qp->abts_scsi_io_bufs = 0;
James Smartc00f62e2019-08-14 16:57:11 -07001083 qp->abts_nvme_io_bufs = 0;
James Smart5e5b5112019-01-28 11:14:22 -08001084 spin_unlock(&qp->io_buf_list_put_lock);
James Smartc00f62e2019-08-14 16:57:11 -07001085 spin_unlock(&qp->abts_io_buf_list_lock);
James Smart5e5b5112019-01-28 11:14:22 -08001086 }
James Smart731eedc2019-03-12 16:30:12 -07001087 spin_unlock_irq(&phba->hbalock);
James Smart5e5b5112019-01-28 11:14:22 -08001088
1089 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
James Smart731eedc2019-03-12 16:30:12 -07001090 spin_lock_irq(&phba->sli4_hba.abts_nvmet_buf_list_lock);
James Smart5e5b5112019-01-28 11:14:22 -08001091 list_splice_init(&phba->sli4_hba.lpfc_abts_nvmet_ctx_list,
1092 &nvmet_aborts);
James Smart731eedc2019-03-12 16:30:12 -07001093 spin_unlock_irq(&phba->sli4_hba.abts_nvmet_buf_list_lock);
James Smart86c67372017-04-21 16:05:04 -07001094 list_for_each_entry_safe(ctxp, ctxp_next, &nvmet_aborts, list) {
James Smart7b7f5512020-03-31 09:50:04 -07001095 ctxp->flag &= ~(LPFC_NVME_XBUSY | LPFC_NVME_ABORT_OP);
James Smart6c621a22017-05-15 15:20:45 -07001096 lpfc_nvmet_ctxbuf_post(phba, ctxp->ctxbuf);
James Smart86c67372017-04-21 16:05:04 -07001097 }
James Smart895427b2017-02-12 13:52:30 -08001098 }
James Smart895427b2017-02-12 13:52:30 -08001099
James Smart68e814f2014-05-21 08:04:59 -04001100 lpfc_sli4_free_sp_events(phba);
James Smart5e5b5112019-01-28 11:14:22 -08001101 return cnt;
James Smartda0436e2009-05-22 14:51:39 -04001102}
1103
1104/**
1105 * lpfc_hba_down_post - Wrapper func for hba down post routine
1106 * @phba: pointer to lpfc HBA data structure.
1107 *
1108 * This routine wraps the actual SLI3 or SLI4 routine for performing
1109 * uninitialization after the HBA is reset when bring down the SLI Layer.
1110 *
1111 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001112 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -04001113 * Any other value - error.
1114 **/
1115int
1116lpfc_hba_down_post(struct lpfc_hba *phba)
1117{
1118 return (*phba->lpfc_hba_down_post)(phba);
1119}
Jamie Wellnitz41415862006-02-28 19:25:27 -05001120
James Smarte59058c2008-08-24 21:49:00 -04001121/**
James Smart3621a712009-04-06 18:47:14 -04001122 * lpfc_hb_timeout - The HBA-timer timeout handler
Lee Jonesfe614ac2020-07-23 13:24:22 +01001123 * @t: timer context used to obtain the pointer to lpfc hba data structure.
James Smarte59058c2008-08-24 21:49:00 -04001124 *
1125 * This is the HBA-timer timeout handler registered to the lpfc driver. When
1126 * this timer fires, a HBA timeout event shall be posted to the lpfc driver
1127 * work-port-events bitmap and the worker thread is notified. This timeout
1128 * event will be used by the worker thread to invoke the actual timeout
1129 * handler routine, lpfc_hb_timeout_handler. Any periodical operations will
1130 * be performed in the timeout handler and the HBA timeout event bit shall
1131 * be cleared by the worker thread after it has taken the event bitmap out.
1132 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001133static void
Kees Cookf22eb4d2017-09-06 20:24:26 -07001134lpfc_hb_timeout(struct timer_list *t)
James Smart858c9f62007-06-17 19:56:39 -05001135{
1136 struct lpfc_hba *phba;
James Smart5e9d9b82008-06-14 22:52:53 -04001137 uint32_t tmo_posted;
James Smart858c9f62007-06-17 19:56:39 -05001138 unsigned long iflag;
1139
Kees Cookf22eb4d2017-09-06 20:24:26 -07001140 phba = from_timer(phba, t, hb_tmofunc);
James Smart93996272008-08-24 21:50:30 -04001141
1142 /* Check for heart beat timeout conditions */
James Smart858c9f62007-06-17 19:56:39 -05001143 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -04001144 tmo_posted = phba->pport->work_port_events & WORKER_HB_TMO;
1145 if (!tmo_posted)
James Smart858c9f62007-06-17 19:56:39 -05001146 phba->pport->work_port_events |= WORKER_HB_TMO;
1147 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
1148
James Smart93996272008-08-24 21:50:30 -04001149 /* Tell the worker thread there is work to do */
James Smart5e9d9b82008-06-14 22:52:53 -04001150 if (!tmo_posted)
1151 lpfc_worker_wake_up(phba);
James Smart858c9f62007-06-17 19:56:39 -05001152 return;
1153}
1154
James Smarte59058c2008-08-24 21:49:00 -04001155/**
James Smart19ca7602010-11-20 23:11:55 -05001156 * lpfc_rrq_timeout - The RRQ-timer timeout handler
Lee Jonesfe614ac2020-07-23 13:24:22 +01001157 * @t: timer context used to obtain the pointer to lpfc hba data structure.
James Smart19ca7602010-11-20 23:11:55 -05001158 *
1159 * This is the RRQ-timer timeout handler registered to the lpfc driver. When
1160 * this timer fires, a RRQ timeout event shall be posted to the lpfc driver
1161 * work-port-events bitmap and the worker thread is notified. This timeout
1162 * event will be used by the worker thread to invoke the actual timeout
1163 * handler routine, lpfc_rrq_handler. Any periodical operations will
1164 * be performed in the timeout handler and the RRQ timeout event bit shall
1165 * be cleared by the worker thread after it has taken the event bitmap out.
1166 **/
1167static void
Kees Cookf22eb4d2017-09-06 20:24:26 -07001168lpfc_rrq_timeout(struct timer_list *t)
James Smart19ca7602010-11-20 23:11:55 -05001169{
1170 struct lpfc_hba *phba;
James Smart19ca7602010-11-20 23:11:55 -05001171 unsigned long iflag;
1172
Kees Cookf22eb4d2017-09-06 20:24:26 -07001173 phba = from_timer(phba, t, rrq_tmr);
James Smart19ca7602010-11-20 23:11:55 -05001174 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart06918ac2014-02-20 09:57:57 -05001175 if (!(phba->pport->load_flag & FC_UNLOADING))
1176 phba->hba_flag |= HBA_RRQ_ACTIVE;
1177 else
1178 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
James Smart19ca7602010-11-20 23:11:55 -05001179 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
James Smart06918ac2014-02-20 09:57:57 -05001180
1181 if (!(phba->pport->load_flag & FC_UNLOADING))
1182 lpfc_worker_wake_up(phba);
James Smart19ca7602010-11-20 23:11:55 -05001183}
1184
1185/**
James Smart3621a712009-04-06 18:47:14 -04001186 * lpfc_hb_mbox_cmpl - The lpfc heart-beat mailbox command callback function
James Smarte59058c2008-08-24 21:49:00 -04001187 * @phba: pointer to lpfc hba data structure.
1188 * @pmboxq: pointer to the driver internal queue element for mailbox command.
1189 *
1190 * This is the callback function to the lpfc heart-beat mailbox command.
1191 * If configured, the lpfc driver issues the heart-beat mailbox command to
1192 * the HBA every LPFC_HB_MBOX_INTERVAL (current 5) seconds. At the time the
1193 * heart-beat mailbox command is issued, the driver shall set up heart-beat
1194 * timeout timer to LPFC_HB_MBOX_TIMEOUT (current 30) seconds and marks
1195 * heart-beat outstanding state. Once the mailbox command comes back and
1196 * no error conditions detected, the heart-beat mailbox command timer is
1197 * reset to LPFC_HB_MBOX_INTERVAL seconds and the heart-beat outstanding
1198 * state is cleared for the next heart-beat. If the timer expired with the
1199 * heart-beat outstanding state set, the driver will put the HBA offline.
1200 **/
James Smart858c9f62007-06-17 19:56:39 -05001201static void
1202lpfc_hb_mbox_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
1203{
1204 unsigned long drvr_flag;
1205
1206 spin_lock_irqsave(&phba->hbalock, drvr_flag);
1207 phba->hb_outstanding = 0;
1208 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
1209
James Smart93996272008-08-24 21:50:30 -04001210 /* Check and reset heart-beat timer is necessary */
James Smart858c9f62007-06-17 19:56:39 -05001211 mempool_free(pmboxq, phba->mbox_mem_pool);
1212 if (!(phba->pport->fc_flag & FC_OFFLINE_MODE) &&
1213 !(phba->link_state == LPFC_HBA_ERROR) &&
James Smart51ef4c22007-08-02 11:10:31 -04001214 !(phba->pport->load_flag & FC_UNLOADING))
James Smart858c9f62007-06-17 19:56:39 -05001215 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001216 jiffies +
1217 msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smart858c9f62007-06-17 19:56:39 -05001218 return;
1219}
1220
Lee Jonesfe614ac2020-07-23 13:24:22 +01001221/*
Dick Kennedy317aeb82020-06-30 14:49:59 -07001222 * lpfc_idle_stat_delay_work - idle_stat tracking
1223 *
1224 * This routine tracks per-cq idle_stat and determines polling decisions.
1225 *
1226 * Return codes:
1227 * None
1228 **/
1229static void
1230lpfc_idle_stat_delay_work(struct work_struct *work)
1231{
1232 struct lpfc_hba *phba = container_of(to_delayed_work(work),
1233 struct lpfc_hba,
1234 idle_stat_delay_work);
1235 struct lpfc_queue *cq;
1236 struct lpfc_sli4_hdw_queue *hdwq;
1237 struct lpfc_idle_stat *idle_stat;
1238 u32 i, idle_percent;
1239 u64 wall, wall_idle, diff_wall, diff_idle, busy_time;
1240
1241 if (phba->pport->load_flag & FC_UNLOADING)
1242 return;
1243
1244 if (phba->link_state == LPFC_HBA_ERROR ||
1245 phba->pport->fc_flag & FC_OFFLINE_MODE)
1246 goto requeue;
1247
1248 for_each_present_cpu(i) {
1249 hdwq = &phba->sli4_hba.hdwq[phba->sli4_hba.cpu_map[i].hdwq];
1250 cq = hdwq->io_cq;
1251
1252 /* Skip if we've already handled this cq's primary CPU */
1253 if (cq->chann != i)
1254 continue;
1255
1256 idle_stat = &phba->sli4_hba.idle_stat[i];
1257
1258 /* get_cpu_idle_time returns values as running counters. Thus,
1259 * to know the amount for this period, the prior counter values
1260 * need to be subtracted from the current counter values.
1261 * From there, the idle time stat can be calculated as a
1262 * percentage of 100 - the sum of the other consumption times.
1263 */
1264 wall_idle = get_cpu_idle_time(i, &wall, 1);
1265 diff_idle = wall_idle - idle_stat->prev_idle;
1266 diff_wall = wall - idle_stat->prev_wall;
1267
1268 if (diff_wall <= diff_idle)
1269 busy_time = 0;
1270 else
1271 busy_time = diff_wall - diff_idle;
1272
1273 idle_percent = div64_u64(100 * busy_time, diff_wall);
1274 idle_percent = 100 - idle_percent;
1275
1276 if (idle_percent < 15)
1277 cq->poll_mode = LPFC_QUEUE_WORK;
1278 else
1279 cq->poll_mode = LPFC_IRQ_POLL;
1280
1281 idle_stat->prev_idle = wall_idle;
1282 idle_stat->prev_wall = wall;
1283 }
1284
1285requeue:
1286 schedule_delayed_work(&phba->idle_stat_delay_work,
1287 msecs_to_jiffies(LPFC_IDLE_STAT_DELAY));
1288}
1289
James Smart32517fc2019-01-28 11:14:33 -08001290static void
1291lpfc_hb_eq_delay_work(struct work_struct *work)
1292{
1293 struct lpfc_hba *phba = container_of(to_delayed_work(work),
1294 struct lpfc_hba, eq_delay_work);
1295 struct lpfc_eq_intr_info *eqi, *eqi_new;
1296 struct lpfc_queue *eq, *eq_next;
James Smart8156d372019-10-18 14:18:26 -07001297 unsigned char *ena_delay = NULL;
James Smart32517fc2019-01-28 11:14:33 -08001298 uint32_t usdelay;
1299 int i;
1300
1301 if (!phba->cfg_auto_imax || phba->pport->load_flag & FC_UNLOADING)
1302 return;
1303
1304 if (phba->link_state == LPFC_HBA_ERROR ||
1305 phba->pport->fc_flag & FC_OFFLINE_MODE)
1306 goto requeue;
1307
James Smart8156d372019-10-18 14:18:26 -07001308 ena_delay = kcalloc(phba->sli4_hba.num_possible_cpu, sizeof(*ena_delay),
1309 GFP_KERNEL);
1310 if (!ena_delay)
James Smart32517fc2019-01-28 11:14:33 -08001311 goto requeue;
1312
James Smart8156d372019-10-18 14:18:26 -07001313 for (i = 0; i < phba->cfg_irq_chann; i++) {
1314 /* Get the EQ corresponding to the IRQ vector */
1315 eq = phba->sli4_hba.hba_eq_hdl[i].eq;
1316 if (!eq)
1317 continue;
1318 if (eq->q_mode || eq->q_flag & HBA_EQ_DELAY_CHK) {
1319 eq->q_flag &= ~HBA_EQ_DELAY_CHK;
1320 ena_delay[eq->last_cpu] = 1;
James Smart8d34a592019-08-14 16:56:35 -07001321 }
James Smart8156d372019-10-18 14:18:26 -07001322 }
James Smart32517fc2019-01-28 11:14:33 -08001323
1324 for_each_present_cpu(i) {
James Smart32517fc2019-01-28 11:14:33 -08001325 eqi = per_cpu_ptr(phba->sli4_hba.eq_info, i);
James Smart8156d372019-10-18 14:18:26 -07001326 if (ena_delay[i]) {
1327 usdelay = (eqi->icnt >> 10) * LPFC_EQ_DELAY_STEP;
1328 if (usdelay > LPFC_MAX_AUTO_EQ_DELAY)
1329 usdelay = LPFC_MAX_AUTO_EQ_DELAY;
1330 } else {
1331 usdelay = 0;
James Smart8d34a592019-08-14 16:56:35 -07001332 }
James Smart32517fc2019-01-28 11:14:33 -08001333
James Smart32517fc2019-01-28 11:14:33 -08001334 eqi->icnt = 0;
1335
1336 list_for_each_entry_safe(eq, eq_next, &eqi->list, cpu_list) {
James Smart8156d372019-10-18 14:18:26 -07001337 if (unlikely(eq->last_cpu != i)) {
James Smart32517fc2019-01-28 11:14:33 -08001338 eqi_new = per_cpu_ptr(phba->sli4_hba.eq_info,
1339 eq->last_cpu);
1340 list_move_tail(&eq->cpu_list, &eqi_new->list);
1341 continue;
1342 }
1343 if (usdelay != eq->q_mode)
1344 lpfc_modify_hba_eq_delay(phba, eq->hdwq, 1,
1345 usdelay);
1346 }
1347 }
1348
James Smart8156d372019-10-18 14:18:26 -07001349 kfree(ena_delay);
James Smart32517fc2019-01-28 11:14:33 -08001350
1351requeue:
1352 queue_delayed_work(phba->wq, &phba->eq_delay_work,
1353 msecs_to_jiffies(LPFC_EQ_DELAY_MSECS));
1354}
1355
James Smarte59058c2008-08-24 21:49:00 -04001356/**
James Smartc4908502019-01-28 11:14:28 -08001357 * lpfc_hb_mxp_handler - Multi-XRI pools handler to adjust XRI distribution
1358 * @phba: pointer to lpfc hba data structure.
1359 *
1360 * For each heartbeat, this routine does some heuristic methods to adjust
1361 * XRI distribution. The goal is to fully utilize free XRIs.
1362 **/
1363static void lpfc_hb_mxp_handler(struct lpfc_hba *phba)
1364{
1365 u32 i;
1366 u32 hwq_count;
1367
1368 hwq_count = phba->cfg_hdw_queue;
1369 for (i = 0; i < hwq_count; i++) {
1370 /* Adjust XRIs in private pool */
1371 lpfc_adjust_pvt_pool_count(phba, i);
1372
1373 /* Adjust high watermark */
1374 lpfc_adjust_high_watermark(phba, i);
1375
1376#ifdef LPFC_MXP_STAT
1377 /* Snapshot pbl, pvt and busy count */
1378 lpfc_snapshot_mxp(phba, i);
1379#endif
1380 }
1381}
1382
James Smarte59058c2008-08-24 21:49:00 -04001383/**
James Smart3621a712009-04-06 18:47:14 -04001384 * lpfc_hb_timeout_handler - The HBA-timer timeout handler
James Smarte59058c2008-08-24 21:49:00 -04001385 * @phba: pointer to lpfc hba data structure.
1386 *
1387 * This is the actual HBA-timer timeout handler to be invoked by the worker
1388 * thread whenever the HBA timer fired and HBA-timeout event posted. This
1389 * handler performs any periodic operations needed for the device. If such
1390 * periodic event has already been attended to either in the interrupt handler
1391 * or by processing slow-ring or fast-ring events within the HBA-timer
1392 * timeout window (LPFC_HB_MBOX_INTERVAL), this handler just simply resets
1393 * the timer for the next timeout period. If lpfc heart-beat mailbox command
1394 * is configured and there is no heart-beat mailbox command outstanding, a
1395 * heart-beat mailbox is issued and timer set properly. Otherwise, if there
1396 * has been a heart-beat mailbox command outstanding, the HBA shall be put
1397 * to offline.
1398 **/
James Smart858c9f62007-06-17 19:56:39 -05001399void
1400lpfc_hb_timeout_handler(struct lpfc_hba *phba)
1401{
James Smart45ed1192009-10-02 15:17:02 -04001402 struct lpfc_vport **vports;
James Smart858c9f62007-06-17 19:56:39 -05001403 LPFC_MBOXQ_t *pmboxq;
James Smart0ff10d42008-01-11 01:52:36 -05001404 struct lpfc_dmabuf *buf_ptr;
James Smart45ed1192009-10-02 15:17:02 -04001405 int retval, i;
James Smart858c9f62007-06-17 19:56:39 -05001406 struct lpfc_sli *psli = &phba->sli;
James Smart0ff10d42008-01-11 01:52:36 -05001407 LIST_HEAD(completions);
James Smart858c9f62007-06-17 19:56:39 -05001408
James Smartc4908502019-01-28 11:14:28 -08001409 if (phba->cfg_xri_rebalancing) {
1410 /* Multi-XRI pools handler */
1411 lpfc_hb_mxp_handler(phba);
1412 }
James Smart858c9f62007-06-17 19:56:39 -05001413
James Smart45ed1192009-10-02 15:17:02 -04001414 vports = lpfc_create_vport_work_array(phba);
1415 if (vports != NULL)
James Smart4258e982015-12-16 18:11:58 -05001416 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart45ed1192009-10-02 15:17:02 -04001417 lpfc_rcv_seq_check_edtov(vports[i]);
James Smarte3ba04c2019-12-18 15:58:02 -08001418 lpfc_fdmi_change_check(vports[i]);
James Smart4258e982015-12-16 18:11:58 -05001419 }
James Smart45ed1192009-10-02 15:17:02 -04001420 lpfc_destroy_vport_work_array(phba, vports);
1421
James Smart858c9f62007-06-17 19:56:39 -05001422 if ((phba->link_state == LPFC_HBA_ERROR) ||
James Smart51ef4c22007-08-02 11:10:31 -04001423 (phba->pport->load_flag & FC_UNLOADING) ||
James Smart858c9f62007-06-17 19:56:39 -05001424 (phba->pport->fc_flag & FC_OFFLINE_MODE))
1425 return;
1426
1427 spin_lock_irq(&phba->pport->work_port_lock);
James Smart858c9f62007-06-17 19:56:39 -05001428
James Smart256ec0d2013-04-17 20:14:58 -04001429 if (time_after(phba->last_completion_time +
1430 msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL),
1431 jiffies)) {
James Smart858c9f62007-06-17 19:56:39 -05001432 spin_unlock_irq(&phba->pport->work_port_lock);
1433 if (!phba->hb_outstanding)
1434 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001435 jiffies +
1436 msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smart858c9f62007-06-17 19:56:39 -05001437 else
1438 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001439 jiffies +
1440 msecs_to_jiffies(1000 * LPFC_HB_MBOX_TIMEOUT));
James Smart858c9f62007-06-17 19:56:39 -05001441 return;
1442 }
1443 spin_unlock_irq(&phba->pport->work_port_lock);
1444
James Smart0ff10d42008-01-11 01:52:36 -05001445 if (phba->elsbuf_cnt &&
1446 (phba->elsbuf_cnt == phba->elsbuf_prev_cnt)) {
1447 spin_lock_irq(&phba->hbalock);
1448 list_splice_init(&phba->elsbuf, &completions);
1449 phba->elsbuf_cnt = 0;
1450 phba->elsbuf_prev_cnt = 0;
1451 spin_unlock_irq(&phba->hbalock);
1452
1453 while (!list_empty(&completions)) {
1454 list_remove_head(&completions, buf_ptr,
1455 struct lpfc_dmabuf, list);
1456 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
1457 kfree(buf_ptr);
1458 }
1459 }
1460 phba->elsbuf_prev_cnt = phba->elsbuf_cnt;
1461
James Smart858c9f62007-06-17 19:56:39 -05001462 /* If there is no heart beat outstanding, issue a heartbeat command */
James Smart13815c82008-01-11 01:52:48 -05001463 if (phba->cfg_enable_hba_heartbeat) {
1464 if (!phba->hb_outstanding) {
James Smartbc739052010-08-04 16:11:18 -04001465 if ((!(psli->sli_flag & LPFC_SLI_MBOX_ACTIVE)) &&
1466 (list_empty(&psli->mboxq))) {
1467 pmboxq = mempool_alloc(phba->mbox_mem_pool,
1468 GFP_KERNEL);
1469 if (!pmboxq) {
1470 mod_timer(&phba->hb_tmofunc,
1471 jiffies +
James Smart256ec0d2013-04-17 20:14:58 -04001472 msecs_to_jiffies(1000 *
1473 LPFC_HB_MBOX_INTERVAL));
James Smartbc739052010-08-04 16:11:18 -04001474 return;
1475 }
James Smart13815c82008-01-11 01:52:48 -05001476
James Smartbc739052010-08-04 16:11:18 -04001477 lpfc_heart_beat(phba, pmboxq);
1478 pmboxq->mbox_cmpl = lpfc_hb_mbox_cmpl;
1479 pmboxq->vport = phba->pport;
1480 retval = lpfc_sli_issue_mbox(phba, pmboxq,
1481 MBX_NOWAIT);
James Smart13815c82008-01-11 01:52:48 -05001482
James Smartbc739052010-08-04 16:11:18 -04001483 if (retval != MBX_BUSY &&
1484 retval != MBX_SUCCESS) {
1485 mempool_free(pmboxq,
1486 phba->mbox_mem_pool);
1487 mod_timer(&phba->hb_tmofunc,
1488 jiffies +
James Smart256ec0d2013-04-17 20:14:58 -04001489 msecs_to_jiffies(1000 *
1490 LPFC_HB_MBOX_INTERVAL));
James Smartbc739052010-08-04 16:11:18 -04001491 return;
1492 }
1493 phba->skipped_hb = 0;
1494 phba->hb_outstanding = 1;
1495 } else if (time_before_eq(phba->last_completion_time,
1496 phba->skipped_hb)) {
1497 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
1498 "2857 Last completion time not "
1499 " updated in %d ms\n",
1500 jiffies_to_msecs(jiffies
1501 - phba->last_completion_time));
1502 } else
1503 phba->skipped_hb = jiffies;
1504
James Smart858c9f62007-06-17 19:56:39 -05001505 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001506 jiffies +
1507 msecs_to_jiffies(1000 * LPFC_HB_MBOX_TIMEOUT));
James Smart858c9f62007-06-17 19:56:39 -05001508 return;
James Smart13815c82008-01-11 01:52:48 -05001509 } else {
1510 /*
1511 * If heart beat timeout called with hb_outstanding set
James Smartdcf2a4e2010-09-29 11:18:53 -04001512 * we need to give the hb mailbox cmd a chance to
1513 * complete or TMO.
James Smart13815c82008-01-11 01:52:48 -05001514 */
James Smartdcf2a4e2010-09-29 11:18:53 -04001515 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
1516 "0459 Adapter heartbeat still out"
1517 "standing:last compl time was %d ms.\n",
1518 jiffies_to_msecs(jiffies
1519 - phba->last_completion_time));
1520 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001521 jiffies +
1522 msecs_to_jiffies(1000 * LPFC_HB_MBOX_TIMEOUT));
James Smart858c9f62007-06-17 19:56:39 -05001523 }
James Smart4258e982015-12-16 18:11:58 -05001524 } else {
1525 mod_timer(&phba->hb_tmofunc,
1526 jiffies +
1527 msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smart858c9f62007-06-17 19:56:39 -05001528 }
1529}
1530
James Smarte59058c2008-08-24 21:49:00 -04001531/**
James Smart3621a712009-04-06 18:47:14 -04001532 * lpfc_offline_eratt - Bring lpfc offline on hardware error attention
James Smarte59058c2008-08-24 21:49:00 -04001533 * @phba: pointer to lpfc hba data structure.
1534 *
1535 * This routine is called to bring the HBA offline when HBA hardware error
1536 * other than Port Error 6 has been detected.
1537 **/
James Smart09372822008-01-11 01:52:54 -05001538static void
1539lpfc_offline_eratt(struct lpfc_hba *phba)
1540{
1541 struct lpfc_sli *psli = &phba->sli;
1542
1543 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001544 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart09372822008-01-11 01:52:54 -05001545 spin_unlock_irq(&phba->hbalock);
James Smart618a5232012-06-12 13:54:36 -04001546 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
James Smart09372822008-01-11 01:52:54 -05001547
1548 lpfc_offline(phba);
1549 lpfc_reset_barrier(phba);
James Smartf4b4c682009-05-22 14:53:12 -04001550 spin_lock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001551 lpfc_sli_brdreset(phba);
James Smartf4b4c682009-05-22 14:53:12 -04001552 spin_unlock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001553 lpfc_hba_down_post(phba);
1554 lpfc_sli_brdready(phba, HS_MBRDY);
1555 lpfc_unblock_mgmt_io(phba);
1556 phba->link_state = LPFC_HBA_ERROR;
1557 return;
1558}
1559
James Smarte59058c2008-08-24 21:49:00 -04001560/**
James Smartda0436e2009-05-22 14:51:39 -04001561 * lpfc_sli4_offline_eratt - Bring lpfc offline on SLI4 hardware error attention
1562 * @phba: pointer to lpfc hba data structure.
1563 *
1564 * This routine is called to bring a SLI4 HBA offline when HBA hardware error
1565 * other than Port Error 6 has been detected.
1566 **/
James Smarta88dbb62013-04-17 20:18:39 -04001567void
James Smartda0436e2009-05-22 14:51:39 -04001568lpfc_sli4_offline_eratt(struct lpfc_hba *phba)
1569{
James Smart946727d2015-04-07 15:07:09 -04001570 spin_lock_irq(&phba->hbalock);
1571 phba->link_state = LPFC_HBA_ERROR;
1572 spin_unlock_irq(&phba->hbalock);
1573
James Smart618a5232012-06-12 13:54:36 -04001574 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
James Smartc00f62e2019-08-14 16:57:11 -07001575 lpfc_sli_flush_io_rings(phba);
James Smartda0436e2009-05-22 14:51:39 -04001576 lpfc_offline(phba);
James Smartda0436e2009-05-22 14:51:39 -04001577 lpfc_hba_down_post(phba);
James Smartda0436e2009-05-22 14:51:39 -04001578 lpfc_unblock_mgmt_io(phba);
James Smartda0436e2009-05-22 14:51:39 -04001579}
1580
1581/**
James Smarta257bf92009-04-06 18:48:10 -04001582 * lpfc_handle_deferred_eratt - The HBA hardware deferred error handler
1583 * @phba: pointer to lpfc hba data structure.
1584 *
1585 * This routine is invoked to handle the deferred HBA hardware error
1586 * conditions. This type of error is indicated by HBA by setting ER1
1587 * and another ER bit in the host status register. The driver will
1588 * wait until the ER1 bit clears before handling the error condition.
1589 **/
1590static void
1591lpfc_handle_deferred_eratt(struct lpfc_hba *phba)
1592{
1593 uint32_t old_host_status = phba->work_hs;
James Smarta257bf92009-04-06 18:48:10 -04001594 struct lpfc_sli *psli = &phba->sli;
1595
James Smartf4b4c682009-05-22 14:53:12 -04001596 /* If the pci channel is offline, ignore possible errors,
1597 * since we cannot communicate with the pci card anyway.
1598 */
1599 if (pci_channel_offline(phba->pcidev)) {
1600 spin_lock_irq(&phba->hbalock);
1601 phba->hba_flag &= ~DEFER_ERATT;
1602 spin_unlock_irq(&phba->hbalock);
1603 return;
1604 }
1605
Dick Kennedy372c1872020-06-30 14:50:00 -07001606 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
1607 "0479 Deferred Adapter Hardware Error "
1608 "Data: x%x x%x x%x\n",
1609 phba->work_hs, phba->work_status[0],
1610 phba->work_status[1]);
James Smarta257bf92009-04-06 18:48:10 -04001611
1612 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001613 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smarta257bf92009-04-06 18:48:10 -04001614 spin_unlock_irq(&phba->hbalock);
1615
1616
1617 /*
1618 * Firmware stops when it triggred erratt. That could cause the I/Os
1619 * dropped by the firmware. Error iocb (I/O) on txcmplq and let the
1620 * SCSI layer retry it after re-establishing link.
1621 */
James Smartdb55fba2014-04-04 13:52:02 -04001622 lpfc_sli_abort_fcp_rings(phba);
James Smarta257bf92009-04-06 18:48:10 -04001623
1624 /*
1625 * There was a firmware error. Take the hba offline and then
1626 * attempt to restart it.
1627 */
James Smart618a5232012-06-12 13:54:36 -04001628 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smarta257bf92009-04-06 18:48:10 -04001629 lpfc_offline(phba);
1630
1631 /* Wait for the ER1 bit to clear.*/
1632 while (phba->work_hs & HS_FFER1) {
1633 msleep(100);
James Smart9940b972011-03-11 16:06:12 -05001634 if (lpfc_readl(phba->HSregaddr, &phba->work_hs)) {
1635 phba->work_hs = UNPLUG_ERR ;
1636 break;
1637 }
James Smarta257bf92009-04-06 18:48:10 -04001638 /* If driver is unloading let the worker thread continue */
1639 if (phba->pport->load_flag & FC_UNLOADING) {
1640 phba->work_hs = 0;
1641 break;
1642 }
1643 }
1644
1645 /*
1646 * This is to ptrotect against a race condition in which
1647 * first write to the host attention register clear the
1648 * host status register.
1649 */
1650 if ((!phba->work_hs) && (!(phba->pport->load_flag & FC_UNLOADING)))
1651 phba->work_hs = old_host_status & ~HS_FFER1;
1652
James Smart3772a992009-05-22 14:50:54 -04001653 spin_lock_irq(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -04001654 phba->hba_flag &= ~DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -04001655 spin_unlock_irq(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -04001656 phba->work_status[0] = readl(phba->MBslimaddr + 0xa8);
1657 phba->work_status[1] = readl(phba->MBslimaddr + 0xac);
1658}
1659
James Smart3772a992009-05-22 14:50:54 -04001660static void
1661lpfc_board_errevt_to_mgmt(struct lpfc_hba *phba)
1662{
1663 struct lpfc_board_event_header board_event;
1664 struct Scsi_Host *shost;
1665
1666 board_event.event_type = FC_REG_BOARD_EVENT;
1667 board_event.subcategory = LPFC_EVENT_PORTINTERR;
1668 shost = lpfc_shost_from_vport(phba->pport);
1669 fc_host_post_vendor_event(shost, fc_get_event_number(),
1670 sizeof(board_event),
1671 (char *) &board_event,
1672 LPFC_NL_VENDOR_ID);
1673}
1674
James Smarta257bf92009-04-06 18:48:10 -04001675/**
James Smart3772a992009-05-22 14:50:54 -04001676 * lpfc_handle_eratt_s3 - The SLI3 HBA hardware error handler
James Smarte59058c2008-08-24 21:49:00 -04001677 * @phba: pointer to lpfc hba data structure.
1678 *
1679 * This routine is invoked to handle the following HBA hardware error
1680 * conditions:
1681 * 1 - HBA error attention interrupt
1682 * 2 - DMA ring index out of range
1683 * 3 - Mailbox command came back as unknown
1684 **/
James Smart3772a992009-05-22 14:50:54 -04001685static void
1686lpfc_handle_eratt_s3(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001687{
James Smart2e0fef82007-06-17 19:56:36 -05001688 struct lpfc_vport *vport = phba->pport;
James Smart2e0fef82007-06-17 19:56:36 -05001689 struct lpfc_sli *psli = &phba->sli;
James Smartd2873e42006-08-18 17:46:43 -04001690 uint32_t event_data;
James Smart57127f12007-10-27 13:37:05 -04001691 unsigned long temperature;
1692 struct temp_event temp_event_data;
James Smart92d7f7b2007-06-17 19:56:38 -05001693 struct Scsi_Host *shost;
James Smart2e0fef82007-06-17 19:56:36 -05001694
Linas Vepstas8d63f372007-02-14 14:28:36 -06001695 /* If the pci channel is offline, ignore possible errors,
James Smart3772a992009-05-22 14:50:54 -04001696 * since we cannot communicate with the pci card anyway.
1697 */
1698 if (pci_channel_offline(phba->pcidev)) {
1699 spin_lock_irq(&phba->hbalock);
1700 phba->hba_flag &= ~DEFER_ERATT;
1701 spin_unlock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -06001702 return;
James Smart3772a992009-05-22 14:50:54 -04001703 }
1704
James Smart13815c82008-01-11 01:52:48 -05001705 /* If resets are disabled then leave the HBA alone and return */
1706 if (!phba->cfg_enable_hba_reset)
1707 return;
dea31012005-04-17 16:05:31 -05001708
James Smartea2151b2008-09-07 11:52:10 -04001709 /* Send an internal error event to mgmt application */
James Smart3772a992009-05-22 14:50:54 -04001710 lpfc_board_errevt_to_mgmt(phba);
James Smartea2151b2008-09-07 11:52:10 -04001711
James Smarta257bf92009-04-06 18:48:10 -04001712 if (phba->hba_flag & DEFER_ERATT)
1713 lpfc_handle_deferred_eratt(phba);
1714
James Smartdcf2a4e2010-09-29 11:18:53 -04001715 if ((phba->work_hs & HS_FFER6) || (phba->work_hs & HS_FFER8)) {
1716 if (phba->work_hs & HS_FFER6)
1717 /* Re-establishing Link */
1718 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1719 "1301 Re-establishing Link "
1720 "Data: x%x x%x x%x\n",
1721 phba->work_hs, phba->work_status[0],
1722 phba->work_status[1]);
1723 if (phba->work_hs & HS_FFER8)
1724 /* Device Zeroization */
1725 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1726 "2861 Host Authentication device "
1727 "zeroization Data:x%x x%x x%x\n",
1728 phba->work_hs, phba->work_status[0],
1729 phba->work_status[1]);
James Smart58da1ff2008-04-07 10:15:56 -04001730
James Smart92d7f7b2007-06-17 19:56:38 -05001731 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001732 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -05001733 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001734
1735 /*
1736 * Firmware stops when it triggled erratt with HS_FFER6.
1737 * That could cause the I/Os dropped by the firmware.
1738 * Error iocb (I/O) on txcmplq and let the SCSI layer
1739 * retry it after re-establishing link.
1740 */
James Smartdb55fba2014-04-04 13:52:02 -04001741 lpfc_sli_abort_fcp_rings(phba);
dea31012005-04-17 16:05:31 -05001742
dea31012005-04-17 16:05:31 -05001743 /*
1744 * There was a firmware error. Take the hba offline and then
1745 * attempt to restart it.
1746 */
James Smart618a5232012-06-12 13:54:36 -04001747 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
dea31012005-04-17 16:05:31 -05001748 lpfc_offline(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -05001749 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05001750 if (lpfc_online(phba) == 0) { /* Initialize the HBA */
James Smart46fa3112007-04-25 09:51:45 -04001751 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05001752 return;
1753 }
James Smart46fa3112007-04-25 09:51:45 -04001754 lpfc_unblock_mgmt_io(phba);
James Smart57127f12007-10-27 13:37:05 -04001755 } else if (phba->work_hs & HS_CRIT_TEMP) {
1756 temperature = readl(phba->MBslimaddr + TEMPERATURE_OFFSET);
1757 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
1758 temp_event_data.event_code = LPFC_CRIT_TEMP;
1759 temp_event_data.data = (uint32_t)temperature;
1760
Dick Kennedy372c1872020-06-30 14:50:00 -07001761 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartd7c255b2008-08-24 21:50:00 -04001762 "0406 Adapter maximum temperature exceeded "
James Smart57127f12007-10-27 13:37:05 -04001763 "(%ld), taking this port offline "
1764 "Data: x%x x%x x%x\n",
1765 temperature, phba->work_hs,
1766 phba->work_status[0], phba->work_status[1]);
1767
1768 shost = lpfc_shost_from_vport(phba->pport);
1769 fc_host_post_vendor_event(shost, fc_get_event_number(),
1770 sizeof(temp_event_data),
1771 (char *) &temp_event_data,
1772 SCSI_NL_VID_TYPE_PCI
1773 | PCI_VENDOR_ID_EMULEX);
1774
James Smart7af67052007-10-27 13:38:11 -04001775 spin_lock_irq(&phba->hbalock);
James Smart7af67052007-10-27 13:38:11 -04001776 phba->over_temp_state = HBA_OVER_TEMP;
1777 spin_unlock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001778 lpfc_offline_eratt(phba);
James Smart57127f12007-10-27 13:37:05 -04001779
dea31012005-04-17 16:05:31 -05001780 } else {
1781 /* The if clause above forces this code path when the status
James Smart93996272008-08-24 21:50:30 -04001782 * failure is a value other than FFER6. Do not call the offline
1783 * twice. This is the adapter hardware error path.
dea31012005-04-17 16:05:31 -05001784 */
Dick Kennedy372c1872020-06-30 14:50:00 -07001785 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04001786 "0457 Adapter Hardware Error "
dea31012005-04-17 16:05:31 -05001787 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001788 phba->work_hs,
dea31012005-04-17 16:05:31 -05001789 phba->work_status[0], phba->work_status[1]);
1790
James Smartd2873e42006-08-18 17:46:43 -04001791 event_data = FC_REG_DUMP_EVENT;
James Smart92d7f7b2007-06-17 19:56:38 -05001792 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05001793 fc_host_post_vendor_event(shost, fc_get_event_number(),
James Smartd2873e42006-08-18 17:46:43 -04001794 sizeof(event_data), (char *) &event_data,
1795 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
1796
James Smart09372822008-01-11 01:52:54 -05001797 lpfc_offline_eratt(phba);
dea31012005-04-17 16:05:31 -05001798 }
James Smart93996272008-08-24 21:50:30 -04001799 return;
dea31012005-04-17 16:05:31 -05001800}
1801
James Smarte59058c2008-08-24 21:49:00 -04001802/**
James Smart618a5232012-06-12 13:54:36 -04001803 * lpfc_sli4_port_sta_fn_reset - The SLI4 function reset due to port status reg
1804 * @phba: pointer to lpfc hba data structure.
1805 * @mbx_action: flag for mailbox shutdown action.
Lee Jonesfe614ac2020-07-23 13:24:22 +01001806 * @en_rn_msg: send reset/port recovery message.
James Smart618a5232012-06-12 13:54:36 -04001807 * This routine is invoked to perform an SLI4 port PCI function reset in
1808 * response to port status register polling attention. It waits for port
1809 * status register (ERR, RDY, RN) bits before proceeding with function reset.
1810 * During this process, interrupt vectors are freed and later requested
1811 * for handling possible port resource change.
1812 **/
1813static int
James Smarte10b2022014-02-20 09:57:43 -05001814lpfc_sli4_port_sta_fn_reset(struct lpfc_hba *phba, int mbx_action,
1815 bool en_rn_msg)
James Smart618a5232012-06-12 13:54:36 -04001816{
1817 int rc;
1818 uint32_t intr_mode;
1819
James Smart27d6ac02018-02-22 08:18:42 -08001820 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) >=
James Smart65791f12016-07-06 12:35:56 -07001821 LPFC_SLI_INTF_IF_TYPE_2) {
1822 /*
1823 * On error status condition, driver need to wait for port
1824 * ready before performing reset.
1825 */
1826 rc = lpfc_sli4_pdev_status_reg_wait(phba);
James Smart0e916ee2016-07-06 12:36:06 -07001827 if (rc)
James Smart65791f12016-07-06 12:35:56 -07001828 return rc;
James Smart618a5232012-06-12 13:54:36 -04001829 }
James Smart0e916ee2016-07-06 12:36:06 -07001830
James Smart65791f12016-07-06 12:35:56 -07001831 /* need reset: attempt for port recovery */
1832 if (en_rn_msg)
Dick Kennedy372c1872020-06-30 14:50:00 -07001833 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart65791f12016-07-06 12:35:56 -07001834 "2887 Reset Needed: Attempting Port "
1835 "Recovery...\n");
1836 lpfc_offline_prep(phba, mbx_action);
James Smartc00f62e2019-08-14 16:57:11 -07001837 lpfc_sli_flush_io_rings(phba);
James Smart65791f12016-07-06 12:35:56 -07001838 lpfc_offline(phba);
1839 /* release interrupt for possible resource change */
1840 lpfc_sli4_disable_intr(phba);
James Smart5a9eeff2018-11-29 16:09:32 -08001841 rc = lpfc_sli_brdrestart(phba);
1842 if (rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -07001843 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart5a9eeff2018-11-29 16:09:32 -08001844 "6309 Failed to restart board\n");
1845 return rc;
1846 }
James Smart65791f12016-07-06 12:35:56 -07001847 /* request and enable interrupt */
1848 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
1849 if (intr_mode == LPFC_INTR_ERROR) {
Dick Kennedy372c1872020-06-30 14:50:00 -07001850 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart65791f12016-07-06 12:35:56 -07001851 "3175 Failed to enable interrupt\n");
1852 return -EIO;
1853 }
1854 phba->intr_mode = intr_mode;
1855 rc = lpfc_online(phba);
1856 if (rc == 0)
1857 lpfc_unblock_mgmt_io(phba);
1858
James Smart618a5232012-06-12 13:54:36 -04001859 return rc;
1860}
1861
1862/**
James Smartda0436e2009-05-22 14:51:39 -04001863 * lpfc_handle_eratt_s4 - The SLI4 HBA hardware error handler
1864 * @phba: pointer to lpfc hba data structure.
1865 *
1866 * This routine is invoked to handle the SLI4 HBA hardware error attention
1867 * conditions.
1868 **/
1869static void
1870lpfc_handle_eratt_s4(struct lpfc_hba *phba)
1871{
1872 struct lpfc_vport *vport = phba->pport;
1873 uint32_t event_data;
1874 struct Scsi_Host *shost;
James Smart2fcee4b2010-12-15 17:57:46 -05001875 uint32_t if_type;
James Smart2e90f4b2011-12-13 13:22:37 -05001876 struct lpfc_register portstat_reg = {0};
1877 uint32_t reg_err1, reg_err2;
1878 uint32_t uerrlo_reg, uemasklo_reg;
James Smart65791f12016-07-06 12:35:56 -07001879 uint32_t smphr_port_status = 0, pci_rd_rc1, pci_rd_rc2;
James Smarte10b2022014-02-20 09:57:43 -05001880 bool en_rn_msg = true;
James Smart946727d2015-04-07 15:07:09 -04001881 struct temp_event temp_event_data;
James Smart65791f12016-07-06 12:35:56 -07001882 struct lpfc_register portsmphr_reg;
1883 int rc, i;
James Smartda0436e2009-05-22 14:51:39 -04001884
1885 /* If the pci channel is offline, ignore possible errors, since
1886 * we cannot communicate with the pci card anyway.
1887 */
James Smart32a93102019-03-12 16:30:13 -07001888 if (pci_channel_offline(phba->pcidev)) {
Dick Kennedy372c1872020-06-30 14:50:00 -07001889 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart32a93102019-03-12 16:30:13 -07001890 "3166 pci channel is offline\n");
1891 lpfc_sli4_offline_eratt(phba);
James Smartda0436e2009-05-22 14:51:39 -04001892 return;
James Smart32a93102019-03-12 16:30:13 -07001893 }
James Smartda0436e2009-05-22 14:51:39 -04001894
James Smart65791f12016-07-06 12:35:56 -07001895 memset(&portsmphr_reg, 0, sizeof(portsmphr_reg));
James Smart2fcee4b2010-12-15 17:57:46 -05001896 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
1897 switch (if_type) {
1898 case LPFC_SLI_INTF_IF_TYPE_0:
James Smart2e90f4b2011-12-13 13:22:37 -05001899 pci_rd_rc1 = lpfc_readl(
1900 phba->sli4_hba.u.if_type0.UERRLOregaddr,
1901 &uerrlo_reg);
1902 pci_rd_rc2 = lpfc_readl(
1903 phba->sli4_hba.u.if_type0.UEMASKLOregaddr,
1904 &uemasklo_reg);
1905 /* consider PCI bus read error as pci_channel_offline */
1906 if (pci_rd_rc1 == -EIO && pci_rd_rc2 == -EIO)
1907 return;
James Smart65791f12016-07-06 12:35:56 -07001908 if (!(phba->hba_flag & HBA_RECOVERABLE_UE)) {
1909 lpfc_sli4_offline_eratt(phba);
1910 return;
1911 }
Dick Kennedy372c1872020-06-30 14:50:00 -07001912 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart65791f12016-07-06 12:35:56 -07001913 "7623 Checking UE recoverable");
1914
1915 for (i = 0; i < phba->sli4_hba.ue_to_sr / 1000; i++) {
1916 if (lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
1917 &portsmphr_reg.word0))
1918 continue;
1919
1920 smphr_port_status = bf_get(lpfc_port_smphr_port_status,
1921 &portsmphr_reg);
1922 if ((smphr_port_status & LPFC_PORT_SEM_MASK) ==
1923 LPFC_PORT_SEM_UE_RECOVERABLE)
1924 break;
1925 /*Sleep for 1Sec, before checking SEMAPHORE */
1926 msleep(1000);
1927 }
1928
Dick Kennedy372c1872020-06-30 14:50:00 -07001929 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart65791f12016-07-06 12:35:56 -07001930 "4827 smphr_port_status x%x : Waited %dSec",
1931 smphr_port_status, i);
1932
1933 /* Recoverable UE, reset the HBA device */
1934 if ((smphr_port_status & LPFC_PORT_SEM_MASK) ==
1935 LPFC_PORT_SEM_UE_RECOVERABLE) {
1936 for (i = 0; i < 20; i++) {
1937 msleep(1000);
1938 if (!lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
1939 &portsmphr_reg.word0) &&
1940 (LPFC_POST_STAGE_PORT_READY ==
1941 bf_get(lpfc_port_smphr_port_status,
1942 &portsmphr_reg))) {
1943 rc = lpfc_sli4_port_sta_fn_reset(phba,
1944 LPFC_MBX_NO_WAIT, en_rn_msg);
1945 if (rc == 0)
1946 return;
Dick Kennedy372c1872020-06-30 14:50:00 -07001947 lpfc_printf_log(phba, KERN_ERR,
1948 LOG_TRACE_EVENT,
James Smart65791f12016-07-06 12:35:56 -07001949 "4215 Failed to recover UE");
1950 break;
1951 }
1952 }
1953 }
Dick Kennedy372c1872020-06-30 14:50:00 -07001954 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart65791f12016-07-06 12:35:56 -07001955 "7624 Firmware not ready: Failing UE recovery,"
1956 " waited %dSec", i);
James Smart8c24a4f2019-08-14 16:56:53 -07001957 phba->link_state = LPFC_HBA_ERROR;
James Smart2fcee4b2010-12-15 17:57:46 -05001958 break;
James Smart946727d2015-04-07 15:07:09 -04001959
James Smart2fcee4b2010-12-15 17:57:46 -05001960 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart27d6ac02018-02-22 08:18:42 -08001961 case LPFC_SLI_INTF_IF_TYPE_6:
James Smart2e90f4b2011-12-13 13:22:37 -05001962 pci_rd_rc1 = lpfc_readl(
1963 phba->sli4_hba.u.if_type2.STATUSregaddr,
1964 &portstat_reg.word0);
1965 /* consider PCI bus read error as pci_channel_offline */
James Smart6b5151f2012-01-18 16:24:06 -05001966 if (pci_rd_rc1 == -EIO) {
Dick Kennedy372c1872020-06-30 14:50:00 -07001967 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart6b5151f2012-01-18 16:24:06 -05001968 "3151 PCI bus read access failure: x%x\n",
1969 readl(phba->sli4_hba.u.if_type2.STATUSregaddr));
James Smart32a93102019-03-12 16:30:13 -07001970 lpfc_sli4_offline_eratt(phba);
James Smart2e90f4b2011-12-13 13:22:37 -05001971 return;
James Smart6b5151f2012-01-18 16:24:06 -05001972 }
James Smart2e90f4b2011-12-13 13:22:37 -05001973 reg_err1 = readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
1974 reg_err2 = readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
James Smart2fcee4b2010-12-15 17:57:46 -05001975 if (bf_get(lpfc_sliport_status_oti, &portstat_reg)) {
Dick Kennedy372c1872020-06-30 14:50:00 -07001976 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
1977 "2889 Port Overtemperature event, "
1978 "taking port offline Data: x%x x%x\n",
1979 reg_err1, reg_err2);
James Smart946727d2015-04-07 15:07:09 -04001980
James Smart310429e2016-07-06 12:35:54 -07001981 phba->sfp_alarm |= LPFC_TRANSGRESSION_HIGH_TEMPERATURE;
James Smart946727d2015-04-07 15:07:09 -04001982 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
1983 temp_event_data.event_code = LPFC_CRIT_TEMP;
1984 temp_event_data.data = 0xFFFFFFFF;
1985
1986 shost = lpfc_shost_from_vport(phba->pport);
1987 fc_host_post_vendor_event(shost, fc_get_event_number(),
1988 sizeof(temp_event_data),
1989 (char *)&temp_event_data,
1990 SCSI_NL_VID_TYPE_PCI
1991 | PCI_VENDOR_ID_EMULEX);
1992
James Smart2fcee4b2010-12-15 17:57:46 -05001993 spin_lock_irq(&phba->hbalock);
1994 phba->over_temp_state = HBA_OVER_TEMP;
1995 spin_unlock_irq(&phba->hbalock);
1996 lpfc_sli4_offline_eratt(phba);
James Smart946727d2015-04-07 15:07:09 -04001997 return;
James Smart2fcee4b2010-12-15 17:57:46 -05001998 }
James Smart2e90f4b2011-12-13 13:22:37 -05001999 if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
James Smarte10b2022014-02-20 09:57:43 -05002000 reg_err2 == SLIPORT_ERR2_REG_FW_RESTART) {
Dick Kennedy372c1872020-06-30 14:50:00 -07002001 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarte10b2022014-02-20 09:57:43 -05002002 "3143 Port Down: Firmware Update "
2003 "Detected\n");
2004 en_rn_msg = false;
2005 } else if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
James Smart2e90f4b2011-12-13 13:22:37 -05002006 reg_err2 == SLIPORT_ERR2_REG_FORCED_DUMP)
Dick Kennedy372c1872020-06-30 14:50:00 -07002007 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2e90f4b2011-12-13 13:22:37 -05002008 "3144 Port Down: Debug Dump\n");
2009 else if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
2010 reg_err2 == SLIPORT_ERR2_REG_FUNC_PROVISON)
Dick Kennedy372c1872020-06-30 14:50:00 -07002011 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2e90f4b2011-12-13 13:22:37 -05002012 "3145 Port Down: Provisioning\n");
James Smart618a5232012-06-12 13:54:36 -04002013
James Smart946727d2015-04-07 15:07:09 -04002014 /* If resets are disabled then leave the HBA alone and return */
2015 if (!phba->cfg_enable_hba_reset)
2016 return;
2017
James Smart618a5232012-06-12 13:54:36 -04002018 /* Check port status register for function reset */
James Smarte10b2022014-02-20 09:57:43 -05002019 rc = lpfc_sli4_port_sta_fn_reset(phba, LPFC_MBX_NO_WAIT,
2020 en_rn_msg);
James Smart618a5232012-06-12 13:54:36 -04002021 if (rc == 0) {
2022 /* don't report event on forced debug dump */
2023 if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
2024 reg_err2 == SLIPORT_ERR2_REG_FORCED_DUMP)
2025 return;
2026 else
2027 break;
James Smart2fcee4b2010-12-15 17:57:46 -05002028 }
James Smart618a5232012-06-12 13:54:36 -04002029 /* fall through for not able to recover */
Dick Kennedy372c1872020-06-30 14:50:00 -07002030 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart8c24a4f2019-08-14 16:56:53 -07002031 "3152 Unrecoverable error\n");
2032 phba->link_state = LPFC_HBA_ERROR;
James Smart2fcee4b2010-12-15 17:57:46 -05002033 break;
2034 case LPFC_SLI_INTF_IF_TYPE_1:
2035 default:
2036 break;
2037 }
James Smart2e90f4b2011-12-13 13:22:37 -05002038 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
2039 "3123 Report dump event to upper layer\n");
2040 /* Send an internal error event to mgmt application */
2041 lpfc_board_errevt_to_mgmt(phba);
2042
2043 event_data = FC_REG_DUMP_EVENT;
2044 shost = lpfc_shost_from_vport(vport);
2045 fc_host_post_vendor_event(shost, fc_get_event_number(),
2046 sizeof(event_data), (char *) &event_data,
2047 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
James Smartda0436e2009-05-22 14:51:39 -04002048}
2049
2050/**
2051 * lpfc_handle_eratt - Wrapper func for handling hba error attention
2052 * @phba: pointer to lpfc HBA data structure.
2053 *
2054 * This routine wraps the actual SLI3 or SLI4 hba error attention handling
2055 * routine from the API jump table function pointer from the lpfc_hba struct.
2056 *
2057 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002058 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -04002059 * Any other value - error.
2060 **/
2061void
2062lpfc_handle_eratt(struct lpfc_hba *phba)
2063{
2064 (*phba->lpfc_handle_eratt)(phba);
2065}
2066
2067/**
James Smart3621a712009-04-06 18:47:14 -04002068 * lpfc_handle_latt - The HBA link event handler
James Smarte59058c2008-08-24 21:49:00 -04002069 * @phba: pointer to lpfc hba data structure.
2070 *
2071 * This routine is invoked from the worker thread to handle a HBA host
James Smart895427b2017-02-12 13:52:30 -08002072 * attention link event. SLI3 only.
James Smarte59058c2008-08-24 21:49:00 -04002073 **/
dea31012005-04-17 16:05:31 -05002074void
James Smart2e0fef82007-06-17 19:56:36 -05002075lpfc_handle_latt(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002076{
James Smart2e0fef82007-06-17 19:56:36 -05002077 struct lpfc_vport *vport = phba->pport;
2078 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05002079 LPFC_MBOXQ_t *pmb;
2080 volatile uint32_t control;
2081 struct lpfc_dmabuf *mp;
James Smart09372822008-01-11 01:52:54 -05002082 int rc = 0;
dea31012005-04-17 16:05:31 -05002083
2084 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart09372822008-01-11 01:52:54 -05002085 if (!pmb) {
2086 rc = 1;
dea31012005-04-17 16:05:31 -05002087 goto lpfc_handle_latt_err_exit;
James Smart09372822008-01-11 01:52:54 -05002088 }
dea31012005-04-17 16:05:31 -05002089
2090 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
James Smart09372822008-01-11 01:52:54 -05002091 if (!mp) {
2092 rc = 2;
dea31012005-04-17 16:05:31 -05002093 goto lpfc_handle_latt_free_pmb;
James Smart09372822008-01-11 01:52:54 -05002094 }
dea31012005-04-17 16:05:31 -05002095
2096 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
James Smart09372822008-01-11 01:52:54 -05002097 if (!mp->virt) {
2098 rc = 3;
dea31012005-04-17 16:05:31 -05002099 goto lpfc_handle_latt_free_mp;
James Smart09372822008-01-11 01:52:54 -05002100 }
dea31012005-04-17 16:05:31 -05002101
James.Smart@Emulex.Com6281bfe2005-11-28 11:41:33 -05002102 /* Cleanup any outstanding ELS commands */
James Smart549e55c2007-08-02 11:09:51 -04002103 lpfc_els_flush_all_cmd(phba);
dea31012005-04-17 16:05:31 -05002104
2105 psli->slistat.link_event++;
James Smart76a95d72010-11-20 23:11:48 -05002106 lpfc_read_topology(phba, pmb, mp);
2107 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
James Smart2e0fef82007-06-17 19:56:36 -05002108 pmb->vport = vport;
James Smart0d2b6b82008-06-14 22:52:47 -04002109 /* Block ELS IOCBs until we have processed this mbox command */
James Smart895427b2017-02-12 13:52:30 -08002110 phba->sli.sli3_ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
James Smart0b727fe2007-10-27 13:37:25 -04002111 rc = lpfc_sli_issue_mbox (phba, pmb, MBX_NOWAIT);
James Smart09372822008-01-11 01:52:54 -05002112 if (rc == MBX_NOT_FINISHED) {
2113 rc = 4;
James Smart14691152006-12-02 13:34:28 -05002114 goto lpfc_handle_latt_free_mbuf;
James Smart09372822008-01-11 01:52:54 -05002115 }
dea31012005-04-17 16:05:31 -05002116
2117 /* Clear Link Attention in HA REG */
James Smart2e0fef82007-06-17 19:56:36 -05002118 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002119 writel(HA_LATT, phba->HAregaddr);
2120 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05002121 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002122
2123 return;
2124
James Smart14691152006-12-02 13:34:28 -05002125lpfc_handle_latt_free_mbuf:
James Smart895427b2017-02-12 13:52:30 -08002126 phba->sli.sli3_ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart14691152006-12-02 13:34:28 -05002127 lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea31012005-04-17 16:05:31 -05002128lpfc_handle_latt_free_mp:
2129 kfree(mp);
2130lpfc_handle_latt_free_pmb:
James Smart1dcb58e2007-04-25 09:51:30 -04002131 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002132lpfc_handle_latt_err_exit:
2133 /* Enable Link attention interrupts */
James Smart2e0fef82007-06-17 19:56:36 -05002134 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002135 psli->sli_flag |= LPFC_PROCESS_LA;
2136 control = readl(phba->HCregaddr);
2137 control |= HC_LAINT_ENA;
2138 writel(control, phba->HCregaddr);
2139 readl(phba->HCregaddr); /* flush */
2140
2141 /* Clear Link Attention in HA REG */
2142 writel(HA_LATT, phba->HAregaddr);
2143 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05002144 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002145 lpfc_linkdown(phba);
James Smart2e0fef82007-06-17 19:56:36 -05002146 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002147
Dick Kennedy372c1872020-06-30 14:50:00 -07002148 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2149 "0300 LATT: Cannot issue READ_LA: Data:%d\n", rc);
dea31012005-04-17 16:05:31 -05002150
2151 return;
2152}
2153
James Smarte59058c2008-08-24 21:49:00 -04002154/**
James Smart3621a712009-04-06 18:47:14 -04002155 * lpfc_parse_vpd - Parse VPD (Vital Product Data)
James Smarte59058c2008-08-24 21:49:00 -04002156 * @phba: pointer to lpfc hba data structure.
2157 * @vpd: pointer to the vital product data.
2158 * @len: length of the vital product data in bytes.
2159 *
2160 * This routine parses the Vital Product Data (VPD). The VPD is treated as
2161 * an array of characters. In this routine, the ModelName, ProgramType, and
2162 * ModelDesc, etc. fields of the phba data structure will be populated.
2163 *
2164 * Return codes
2165 * 0 - pointer to the VPD passed in is NULL
2166 * 1 - success
2167 **/
James Smart3772a992009-05-22 14:50:54 -04002168int
James Smart2e0fef82007-06-17 19:56:36 -05002169lpfc_parse_vpd(struct lpfc_hba *phba, uint8_t *vpd, int len)
dea31012005-04-17 16:05:31 -05002170{
2171 uint8_t lenlo, lenhi;
Anton Blanchard07da60c2007-03-21 08:41:47 -05002172 int Length;
dea31012005-04-17 16:05:31 -05002173 int i, j;
2174 int finished = 0;
2175 int index = 0;
2176
2177 if (!vpd)
2178 return 0;
2179
2180 /* Vital Product */
James Smarted957682007-06-17 19:56:37 -05002181 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002182 "0455 Vital Product Data: x%x x%x x%x x%x\n",
dea31012005-04-17 16:05:31 -05002183 (uint32_t) vpd[0], (uint32_t) vpd[1], (uint32_t) vpd[2],
2184 (uint32_t) vpd[3]);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002185 while (!finished && (index < (len - 4))) {
dea31012005-04-17 16:05:31 -05002186 switch (vpd[index]) {
2187 case 0x82:
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002188 case 0x91:
dea31012005-04-17 16:05:31 -05002189 index += 1;
2190 lenlo = vpd[index];
2191 index += 1;
2192 lenhi = vpd[index];
2193 index += 1;
2194 i = ((((unsigned short)lenhi) << 8) + lenlo);
2195 index += i;
2196 break;
2197 case 0x90:
2198 index += 1;
2199 lenlo = vpd[index];
2200 index += 1;
2201 lenhi = vpd[index];
2202 index += 1;
2203 Length = ((((unsigned short)lenhi) << 8) + lenlo);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002204 if (Length > len - index)
2205 Length = len - index;
dea31012005-04-17 16:05:31 -05002206 while (Length > 0) {
2207 /* Look for Serial Number */
2208 if ((vpd[index] == 'S') && (vpd[index+1] == 'N')) {
2209 index += 2;
2210 i = vpd[index];
2211 index += 1;
2212 j = 0;
2213 Length -= (3+i);
2214 while(i--) {
2215 phba->SerialNumber[j++] = vpd[index++];
2216 if (j == 31)
2217 break;
2218 }
2219 phba->SerialNumber[j] = 0;
2220 continue;
2221 }
2222 else if ((vpd[index] == 'V') && (vpd[index+1] == '1')) {
2223 phba->vpd_flag |= VPD_MODEL_DESC;
2224 index += 2;
2225 i = vpd[index];
2226 index += 1;
2227 j = 0;
2228 Length -= (3+i);
2229 while(i--) {
2230 phba->ModelDesc[j++] = vpd[index++];
2231 if (j == 255)
2232 break;
2233 }
2234 phba->ModelDesc[j] = 0;
2235 continue;
2236 }
2237 else if ((vpd[index] == 'V') && (vpd[index+1] == '2')) {
2238 phba->vpd_flag |= VPD_MODEL_NAME;
2239 index += 2;
2240 i = vpd[index];
2241 index += 1;
2242 j = 0;
2243 Length -= (3+i);
2244 while(i--) {
2245 phba->ModelName[j++] = vpd[index++];
2246 if (j == 79)
2247 break;
2248 }
2249 phba->ModelName[j] = 0;
2250 continue;
2251 }
2252 else if ((vpd[index] == 'V') && (vpd[index+1] == '3')) {
2253 phba->vpd_flag |= VPD_PROGRAM_TYPE;
2254 index += 2;
2255 i = vpd[index];
2256 index += 1;
2257 j = 0;
2258 Length -= (3+i);
2259 while(i--) {
2260 phba->ProgramType[j++] = vpd[index++];
2261 if (j == 255)
2262 break;
2263 }
2264 phba->ProgramType[j] = 0;
2265 continue;
2266 }
2267 else if ((vpd[index] == 'V') && (vpd[index+1] == '4')) {
2268 phba->vpd_flag |= VPD_PORT;
2269 index += 2;
2270 i = vpd[index];
2271 index += 1;
2272 j = 0;
2273 Length -= (3+i);
2274 while(i--) {
James Smartcd1c8302011-10-10 21:33:25 -04002275 if ((phba->sli_rev == LPFC_SLI_REV4) &&
2276 (phba->sli4_hba.pport_name_sta ==
2277 LPFC_SLI4_PPNAME_GET)) {
2278 j++;
2279 index++;
2280 } else
2281 phba->Port[j++] = vpd[index++];
2282 if (j == 19)
2283 break;
dea31012005-04-17 16:05:31 -05002284 }
James Smartcd1c8302011-10-10 21:33:25 -04002285 if ((phba->sli_rev != LPFC_SLI_REV4) ||
2286 (phba->sli4_hba.pport_name_sta ==
2287 LPFC_SLI4_PPNAME_NON))
2288 phba->Port[j] = 0;
dea31012005-04-17 16:05:31 -05002289 continue;
2290 }
2291 else {
2292 index += 2;
2293 i = vpd[index];
2294 index += 1;
2295 index += i;
2296 Length -= (3 + i);
2297 }
2298 }
2299 finished = 0;
2300 break;
2301 case 0x78:
2302 finished = 1;
2303 break;
2304 default:
2305 index ++;
2306 break;
2307 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002308 }
dea31012005-04-17 16:05:31 -05002309
2310 return(1);
2311}
2312
James Smarte59058c2008-08-24 21:49:00 -04002313/**
James Smart3621a712009-04-06 18:47:14 -04002314 * lpfc_get_hba_model_desc - Retrieve HBA device model name and description
James Smarte59058c2008-08-24 21:49:00 -04002315 * @phba: pointer to lpfc hba data structure.
2316 * @mdp: pointer to the data structure to hold the derived model name.
2317 * @descp: pointer to the data structure to hold the derived description.
2318 *
2319 * This routine retrieves HBA's description based on its registered PCI device
2320 * ID. The @descp passed into this function points to an array of 256 chars. It
2321 * shall be returned with the model name, maximum speed, and the host bus type.
2322 * The @mdp passed into this function points to an array of 80 chars. When the
2323 * function returns, the @mdp will be filled with the model name.
2324 **/
dea31012005-04-17 16:05:31 -05002325static void
James Smart2e0fef82007-06-17 19:56:36 -05002326lpfc_get_hba_model_desc(struct lpfc_hba *phba, uint8_t *mdp, uint8_t *descp)
dea31012005-04-17 16:05:31 -05002327{
2328 lpfc_vpd_t *vp;
James.Smart@Emulex.Comfefcb2b2005-11-28 15:08:56 -05002329 uint16_t dev_id = phba->pcidev->device;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002330 int max_speed;
James Smart84774a42008-08-24 21:50:06 -04002331 int GE = 0;
James Smartda0436e2009-05-22 14:51:39 -04002332 int oneConnect = 0; /* default is not a oneConnect */
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002333 struct {
James Smarta747c9c2009-11-18 15:41:10 -05002334 char *name;
2335 char *bus;
2336 char *function;
2337 } m = {"<Unknown>", "", ""};
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002338
2339 if (mdp && mdp[0] != '\0'
2340 && descp && descp[0] != '\0')
2341 return;
2342
James Smartfbd8a6b2018-02-22 08:18:45 -08002343 if (phba->lmt & LMT_64Gb)
2344 max_speed = 64;
2345 else if (phba->lmt & LMT_32Gb)
James Smartd38dd522015-08-31 16:48:17 -04002346 max_speed = 32;
2347 else if (phba->lmt & LMT_16Gb)
James Smartc0c11512011-05-24 11:41:34 -04002348 max_speed = 16;
2349 else if (phba->lmt & LMT_10Gb)
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002350 max_speed = 10;
2351 else if (phba->lmt & LMT_8Gb)
2352 max_speed = 8;
2353 else if (phba->lmt & LMT_4Gb)
2354 max_speed = 4;
2355 else if (phba->lmt & LMT_2Gb)
2356 max_speed = 2;
James Smart4169d862012-09-29 11:32:09 -04002357 else if (phba->lmt & LMT_1Gb)
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002358 max_speed = 1;
James Smart4169d862012-09-29 11:32:09 -04002359 else
2360 max_speed = 0;
dea31012005-04-17 16:05:31 -05002361
2362 vp = &phba->vpd;
dea31012005-04-17 16:05:31 -05002363
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002364 switch (dev_id) {
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04002365 case PCI_DEVICE_ID_FIREFLY:
James Smart12222f42014-05-21 08:05:19 -04002366 m = (typeof(m)){"LP6000", "PCI",
2367 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04002368 break;
dea31012005-04-17 16:05:31 -05002369 case PCI_DEVICE_ID_SUPERFLY:
2370 if (vp->rev.biuRev >= 1 && vp->rev.biuRev <= 3)
James Smart12222f42014-05-21 08:05:19 -04002371 m = (typeof(m)){"LP7000", "PCI", ""};
dea31012005-04-17 16:05:31 -05002372 else
James Smart12222f42014-05-21 08:05:19 -04002373 m = (typeof(m)){"LP7000E", "PCI", ""};
2374 m.function = "Obsolete, Unsupported Fibre Channel Adapter";
dea31012005-04-17 16:05:31 -05002375 break;
2376 case PCI_DEVICE_ID_DRAGONFLY:
James Smarta747c9c2009-11-18 15:41:10 -05002377 m = (typeof(m)){"LP8000", "PCI",
James Smart12222f42014-05-21 08:05:19 -04002378 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002379 break;
2380 case PCI_DEVICE_ID_CENTAUR:
2381 if (FC_JEDEC_ID(vp->rev.biuRev) == CENTAUR_2G_JEDEC_ID)
James Smart12222f42014-05-21 08:05:19 -04002382 m = (typeof(m)){"LP9002", "PCI", ""};
dea31012005-04-17 16:05:31 -05002383 else
James Smart12222f42014-05-21 08:05:19 -04002384 m = (typeof(m)){"LP9000", "PCI", ""};
2385 m.function = "Obsolete, Unsupported Fibre Channel Adapter";
dea31012005-04-17 16:05:31 -05002386 break;
2387 case PCI_DEVICE_ID_RFLY:
James Smarta747c9c2009-11-18 15:41:10 -05002388 m = (typeof(m)){"LP952", "PCI",
James Smart12222f42014-05-21 08:05:19 -04002389 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002390 break;
2391 case PCI_DEVICE_ID_PEGASUS:
James Smarta747c9c2009-11-18 15:41:10 -05002392 m = (typeof(m)){"LP9802", "PCI-X",
James Smart12222f42014-05-21 08:05:19 -04002393 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002394 break;
2395 case PCI_DEVICE_ID_THOR:
James Smarta747c9c2009-11-18 15:41:10 -05002396 m = (typeof(m)){"LP10000", "PCI-X",
James Smart12222f42014-05-21 08:05:19 -04002397 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002398 break;
2399 case PCI_DEVICE_ID_VIPER:
James Smarta747c9c2009-11-18 15:41:10 -05002400 m = (typeof(m)){"LPX1000", "PCI-X",
James Smart12222f42014-05-21 08:05:19 -04002401 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002402 break;
2403 case PCI_DEVICE_ID_PFLY:
James Smarta747c9c2009-11-18 15:41:10 -05002404 m = (typeof(m)){"LP982", "PCI-X",
James Smart12222f42014-05-21 08:05:19 -04002405 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002406 break;
2407 case PCI_DEVICE_ID_TFLY:
James Smarta747c9c2009-11-18 15:41:10 -05002408 m = (typeof(m)){"LP1050", "PCI-X",
James Smart12222f42014-05-21 08:05:19 -04002409 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002410 break;
2411 case PCI_DEVICE_ID_HELIOS:
James Smarta747c9c2009-11-18 15:41:10 -05002412 m = (typeof(m)){"LP11000", "PCI-X2",
James Smart12222f42014-05-21 08:05:19 -04002413 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002414 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002415 case PCI_DEVICE_ID_HELIOS_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002416 m = (typeof(m)){"LP11000-SP", "PCI-X2",
James Smart12222f42014-05-21 08:05:19 -04002417 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002418 break;
2419 case PCI_DEVICE_ID_HELIOS_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002420 m = (typeof(m)){"LP11002-SP", "PCI-X2",
James Smart12222f42014-05-21 08:05:19 -04002421 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002422 break;
2423 case PCI_DEVICE_ID_NEPTUNE:
James Smart12222f42014-05-21 08:05:19 -04002424 m = (typeof(m)){"LPe1000", "PCIe",
2425 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002426 break;
2427 case PCI_DEVICE_ID_NEPTUNE_SCSP:
James Smart12222f42014-05-21 08:05:19 -04002428 m = (typeof(m)){"LPe1000-SP", "PCIe",
2429 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002430 break;
2431 case PCI_DEVICE_ID_NEPTUNE_DCSP:
James Smart12222f42014-05-21 08:05:19 -04002432 m = (typeof(m)){"LPe1002-SP", "PCIe",
2433 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002434 break;
dea31012005-04-17 16:05:31 -05002435 case PCI_DEVICE_ID_BMID:
James Smarta747c9c2009-11-18 15:41:10 -05002436 m = (typeof(m)){"LP1150", "PCI-X2", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002437 break;
2438 case PCI_DEVICE_ID_BSMB:
James Smart12222f42014-05-21 08:05:19 -04002439 m = (typeof(m)){"LP111", "PCI-X2",
2440 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002441 break;
2442 case PCI_DEVICE_ID_ZEPHYR:
James Smarta747c9c2009-11-18 15:41:10 -05002443 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002444 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002445 case PCI_DEVICE_ID_ZEPHYR_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002446 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002447 break;
2448 case PCI_DEVICE_ID_ZEPHYR_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002449 m = (typeof(m)){"LP2105", "PCIe", "FCoE Adapter"};
James Smarta257bf92009-04-06 18:48:10 -04002450 GE = 1;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002451 break;
dea31012005-04-17 16:05:31 -05002452 case PCI_DEVICE_ID_ZMID:
James Smarta747c9c2009-11-18 15:41:10 -05002453 m = (typeof(m)){"LPe1150", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002454 break;
2455 case PCI_DEVICE_ID_ZSMB:
James Smarta747c9c2009-11-18 15:41:10 -05002456 m = (typeof(m)){"LPe111", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002457 break;
2458 case PCI_DEVICE_ID_LP101:
James Smart12222f42014-05-21 08:05:19 -04002459 m = (typeof(m)){"LP101", "PCI-X",
2460 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002461 break;
2462 case PCI_DEVICE_ID_LP10000S:
James Smart12222f42014-05-21 08:05:19 -04002463 m = (typeof(m)){"LP10000-S", "PCI",
2464 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04002465 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002466 case PCI_DEVICE_ID_LP11000S:
James Smart12222f42014-05-21 08:05:19 -04002467 m = (typeof(m)){"LP11000-S", "PCI-X2",
2468 "Obsolete, Unsupported Fibre Channel Adapter"};
James Smart18a3b592006-12-02 13:35:08 -05002469 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002470 case PCI_DEVICE_ID_LPE11000S:
James Smart12222f42014-05-21 08:05:19 -04002471 m = (typeof(m)){"LPe11000-S", "PCIe",
2472 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Com5cc36b32005-11-28 11:42:19 -05002473 break;
James Smartb87eab32007-04-25 09:53:28 -04002474 case PCI_DEVICE_ID_SAT:
James Smarta747c9c2009-11-18 15:41:10 -05002475 m = (typeof(m)){"LPe12000", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002476 break;
2477 case PCI_DEVICE_ID_SAT_MID:
James Smarta747c9c2009-11-18 15:41:10 -05002478 m = (typeof(m)){"LPe1250", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002479 break;
2480 case PCI_DEVICE_ID_SAT_SMB:
James Smarta747c9c2009-11-18 15:41:10 -05002481 m = (typeof(m)){"LPe121", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002482 break;
2483 case PCI_DEVICE_ID_SAT_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002484 m = (typeof(m)){"LPe12002-SP", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002485 break;
2486 case PCI_DEVICE_ID_SAT_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002487 m = (typeof(m)){"LPe12000-SP", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002488 break;
2489 case PCI_DEVICE_ID_SAT_S:
James Smarta747c9c2009-11-18 15:41:10 -05002490 m = (typeof(m)){"LPe12000-S", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002491 break;
James Smart84774a42008-08-24 21:50:06 -04002492 case PCI_DEVICE_ID_HORNET:
James Smart12222f42014-05-21 08:05:19 -04002493 m = (typeof(m)){"LP21000", "PCIe",
2494 "Obsolete, Unsupported FCoE Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002495 GE = 1;
2496 break;
2497 case PCI_DEVICE_ID_PROTEUS_VF:
James Smarta747c9c2009-11-18 15:41:10 -05002498 m = (typeof(m)){"LPev12000", "PCIe IOV",
James Smart12222f42014-05-21 08:05:19 -04002499 "Obsolete, Unsupported Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002500 break;
2501 case PCI_DEVICE_ID_PROTEUS_PF:
James Smarta747c9c2009-11-18 15:41:10 -05002502 m = (typeof(m)){"LPev12000", "PCIe IOV",
James Smart12222f42014-05-21 08:05:19 -04002503 "Obsolete, Unsupported Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002504 break;
2505 case PCI_DEVICE_ID_PROTEUS_S:
James Smarta747c9c2009-11-18 15:41:10 -05002506 m = (typeof(m)){"LPemv12002-S", "PCIe IOV",
James Smart12222f42014-05-21 08:05:19 -04002507 "Obsolete, Unsupported Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002508 break;
James Smartda0436e2009-05-22 14:51:39 -04002509 case PCI_DEVICE_ID_TIGERSHARK:
2510 oneConnect = 1;
James Smarta747c9c2009-11-18 15:41:10 -05002511 m = (typeof(m)){"OCe10100", "PCIe", "FCoE"};
James Smartda0436e2009-05-22 14:51:39 -04002512 break;
James Smarta747c9c2009-11-18 15:41:10 -05002513 case PCI_DEVICE_ID_TOMCAT:
James Smart6669f9b2009-10-02 15:16:45 -04002514 oneConnect = 1;
James Smarta747c9c2009-11-18 15:41:10 -05002515 m = (typeof(m)){"OCe11100", "PCIe", "FCoE"};
2516 break;
2517 case PCI_DEVICE_ID_FALCON:
2518 m = (typeof(m)){"LPSe12002-ML1-E", "PCIe",
2519 "EmulexSecure Fibre"};
James Smart6669f9b2009-10-02 15:16:45 -04002520 break;
James Smart98fc5dd2010-06-07 15:24:29 -04002521 case PCI_DEVICE_ID_BALIUS:
2522 m = (typeof(m)){"LPVe12002", "PCIe Shared I/O",
James Smart12222f42014-05-21 08:05:19 -04002523 "Obsolete, Unsupported Fibre Channel Adapter"};
James Smart98fc5dd2010-06-07 15:24:29 -04002524 break;
James Smart085c6472010-11-20 23:11:37 -05002525 case PCI_DEVICE_ID_LANCER_FC:
James Smartc0c11512011-05-24 11:41:34 -04002526 m = (typeof(m)){"LPe16000", "PCIe", "Fibre Channel Adapter"};
James Smart085c6472010-11-20 23:11:37 -05002527 break;
James Smart12222f42014-05-21 08:05:19 -04002528 case PCI_DEVICE_ID_LANCER_FC_VF:
2529 m = (typeof(m)){"LPe16000", "PCIe",
2530 "Obsolete, Unsupported Fibre Channel Adapter"};
2531 break;
James Smart085c6472010-11-20 23:11:37 -05002532 case PCI_DEVICE_ID_LANCER_FCOE:
2533 oneConnect = 1;
James Smart079b5c92011-08-21 21:48:49 -04002534 m = (typeof(m)){"OCe15100", "PCIe", "FCoE"};
James Smart085c6472010-11-20 23:11:37 -05002535 break;
James Smart12222f42014-05-21 08:05:19 -04002536 case PCI_DEVICE_ID_LANCER_FCOE_VF:
2537 oneConnect = 1;
2538 m = (typeof(m)){"OCe15100", "PCIe",
2539 "Obsolete, Unsupported FCoE"};
2540 break;
James Smartd38dd522015-08-31 16:48:17 -04002541 case PCI_DEVICE_ID_LANCER_G6_FC:
2542 m = (typeof(m)){"LPe32000", "PCIe", "Fibre Channel Adapter"};
2543 break;
James Smartc238b9b2018-02-22 08:18:44 -08002544 case PCI_DEVICE_ID_LANCER_G7_FC:
2545 m = (typeof(m)){"LPe36000", "PCIe", "Fibre Channel Adapter"};
2546 break;
James Smartf8cafd32012-08-03 12:42:51 -04002547 case PCI_DEVICE_ID_SKYHAWK:
2548 case PCI_DEVICE_ID_SKYHAWK_VF:
2549 oneConnect = 1;
2550 m = (typeof(m)){"OCe14000", "PCIe", "FCoE"};
2551 break;
James.Smart@Emulex.Com5cc36b32005-11-28 11:42:19 -05002552 default:
James Smarta747c9c2009-11-18 15:41:10 -05002553 m = (typeof(m)){"Unknown", "", ""};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002554 break;
dea31012005-04-17 16:05:31 -05002555 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002556
2557 if (mdp && mdp[0] == '\0')
2558 snprintf(mdp, 79,"%s", m.name);
James Smartc0c11512011-05-24 11:41:34 -04002559 /*
2560 * oneConnect hba requires special processing, they are all initiators
James Smartda0436e2009-05-22 14:51:39 -04002561 * and we put the port number on the end
2562 */
2563 if (descp && descp[0] == '\0') {
2564 if (oneConnect)
2565 snprintf(descp, 255,
James Smart4169d862012-09-29 11:32:09 -04002566 "Emulex OneConnect %s, %s Initiator %s",
James Smarta747c9c2009-11-18 15:41:10 -05002567 m.name, m.function,
James Smartda0436e2009-05-22 14:51:39 -04002568 phba->Port);
James Smart4169d862012-09-29 11:32:09 -04002569 else if (max_speed == 0)
2570 snprintf(descp, 255,
Sebastian Herbszt290237d2015-08-31 16:48:08 -04002571 "Emulex %s %s %s",
James Smart4169d862012-09-29 11:32:09 -04002572 m.name, m.bus, m.function);
James Smartda0436e2009-05-22 14:51:39 -04002573 else
2574 snprintf(descp, 255,
2575 "Emulex %s %d%s %s %s",
James Smarta747c9c2009-11-18 15:41:10 -05002576 m.name, max_speed, (GE) ? "GE" : "Gb",
2577 m.bus, m.function);
James Smartda0436e2009-05-22 14:51:39 -04002578 }
dea31012005-04-17 16:05:31 -05002579}
2580
James Smarte59058c2008-08-24 21:49:00 -04002581/**
James Smart3621a712009-04-06 18:47:14 -04002582 * lpfc_post_buffer - Post IOCB(s) with DMA buffer descriptor(s) to a IOCB ring
James Smarte59058c2008-08-24 21:49:00 -04002583 * @phba: pointer to lpfc hba data structure.
2584 * @pring: pointer to a IOCB ring.
2585 * @cnt: the number of IOCBs to be posted to the IOCB ring.
2586 *
2587 * This routine posts a given number of IOCBs with the associated DMA buffer
2588 * descriptors specified by the cnt argument to the given IOCB ring.
2589 *
2590 * Return codes
2591 * The number of IOCBs NOT able to be posted to the IOCB ring.
2592 **/
dea31012005-04-17 16:05:31 -05002593int
James Smart495a7142008-06-14 22:52:59 -04002594lpfc_post_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring, int cnt)
dea31012005-04-17 16:05:31 -05002595{
2596 IOCB_t *icmd;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002597 struct lpfc_iocbq *iocb;
dea31012005-04-17 16:05:31 -05002598 struct lpfc_dmabuf *mp1, *mp2;
2599
2600 cnt += pring->missbufcnt;
2601
2602 /* While there are buffers to post */
2603 while (cnt > 0) {
2604 /* Allocate buffer for command iocb */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002605 iocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05002606 if (iocb == NULL) {
2607 pring->missbufcnt = cnt;
2608 return cnt;
2609 }
dea31012005-04-17 16:05:31 -05002610 icmd = &iocb->iocb;
2611
2612 /* 2 buffers can be posted per command */
2613 /* Allocate buffer to post */
2614 mp1 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
2615 if (mp1)
James Smart98c9ea52007-10-27 13:37:33 -04002616 mp1->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &mp1->phys);
2617 if (!mp1 || !mp1->virt) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002618 kfree(mp1);
James Bottomley604a3e32005-10-29 10:28:33 -05002619 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002620 pring->missbufcnt = cnt;
2621 return cnt;
2622 }
2623
2624 INIT_LIST_HEAD(&mp1->list);
2625 /* Allocate buffer to post */
2626 if (cnt > 1) {
2627 mp2 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
2628 if (mp2)
2629 mp2->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
2630 &mp2->phys);
James Smart98c9ea52007-10-27 13:37:33 -04002631 if (!mp2 || !mp2->virt) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002632 kfree(mp2);
dea31012005-04-17 16:05:31 -05002633 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
2634 kfree(mp1);
James Bottomley604a3e32005-10-29 10:28:33 -05002635 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002636 pring->missbufcnt = cnt;
2637 return cnt;
2638 }
2639
2640 INIT_LIST_HEAD(&mp2->list);
2641 } else {
2642 mp2 = NULL;
2643 }
2644
2645 icmd->un.cont64[0].addrHigh = putPaddrHigh(mp1->phys);
2646 icmd->un.cont64[0].addrLow = putPaddrLow(mp1->phys);
2647 icmd->un.cont64[0].tus.f.bdeSize = FCELSSIZE;
2648 icmd->ulpBdeCount = 1;
2649 cnt--;
2650 if (mp2) {
2651 icmd->un.cont64[1].addrHigh = putPaddrHigh(mp2->phys);
2652 icmd->un.cont64[1].addrLow = putPaddrLow(mp2->phys);
2653 icmd->un.cont64[1].tus.f.bdeSize = FCELSSIZE;
2654 cnt--;
2655 icmd->ulpBdeCount = 2;
2656 }
2657
2658 icmd->ulpCommand = CMD_QUE_RING_BUF64_CN;
2659 icmd->ulpLe = 1;
2660
James Smart3772a992009-05-22 14:50:54 -04002661 if (lpfc_sli_issue_iocb(phba, pring->ringno, iocb, 0) ==
2662 IOCB_ERROR) {
dea31012005-04-17 16:05:31 -05002663 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
2664 kfree(mp1);
2665 cnt++;
2666 if (mp2) {
2667 lpfc_mbuf_free(phba, mp2->virt, mp2->phys);
2668 kfree(mp2);
2669 cnt++;
2670 }
James Bottomley604a3e32005-10-29 10:28:33 -05002671 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002672 pring->missbufcnt = cnt;
dea31012005-04-17 16:05:31 -05002673 return cnt;
2674 }
dea31012005-04-17 16:05:31 -05002675 lpfc_sli_ringpostbuf_put(phba, pring, mp1);
James Smart92d7f7b2007-06-17 19:56:38 -05002676 if (mp2)
dea31012005-04-17 16:05:31 -05002677 lpfc_sli_ringpostbuf_put(phba, pring, mp2);
dea31012005-04-17 16:05:31 -05002678 }
2679 pring->missbufcnt = 0;
2680 return 0;
2681}
2682
James Smarte59058c2008-08-24 21:49:00 -04002683/**
James Smart3621a712009-04-06 18:47:14 -04002684 * lpfc_post_rcv_buf - Post the initial receive IOCB buffers to ELS ring
James Smarte59058c2008-08-24 21:49:00 -04002685 * @phba: pointer to lpfc hba data structure.
2686 *
2687 * This routine posts initial receive IOCB buffers to the ELS ring. The
2688 * current number of initial IOCB buffers specified by LPFC_BUF_RING0 is
James Smart895427b2017-02-12 13:52:30 -08002689 * set to 64 IOCBs. SLI3 only.
James Smarte59058c2008-08-24 21:49:00 -04002690 *
2691 * Return codes
2692 * 0 - success (currently always success)
2693 **/
dea31012005-04-17 16:05:31 -05002694static int
James Smart2e0fef82007-06-17 19:56:36 -05002695lpfc_post_rcv_buf(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002696{
2697 struct lpfc_sli *psli = &phba->sli;
2698
2699 /* Ring 0, ELS / CT buffers */
James Smart895427b2017-02-12 13:52:30 -08002700 lpfc_post_buffer(phba, &psli->sli3_ring[LPFC_ELS_RING], LPFC_BUF_RING0);
dea31012005-04-17 16:05:31 -05002701 /* Ring 2 - FCP no buffers needed */
2702
2703 return 0;
2704}
2705
2706#define S(N,V) (((V)<<(N))|((V)>>(32-(N))))
2707
James Smarte59058c2008-08-24 21:49:00 -04002708/**
James Smart3621a712009-04-06 18:47:14 -04002709 * lpfc_sha_init - Set up initial array of hash table entries
James Smarte59058c2008-08-24 21:49:00 -04002710 * @HashResultPointer: pointer to an array as hash table.
2711 *
2712 * This routine sets up the initial values to the array of hash table entries
2713 * for the LC HBAs.
2714 **/
dea31012005-04-17 16:05:31 -05002715static void
2716lpfc_sha_init(uint32_t * HashResultPointer)
2717{
2718 HashResultPointer[0] = 0x67452301;
2719 HashResultPointer[1] = 0xEFCDAB89;
2720 HashResultPointer[2] = 0x98BADCFE;
2721 HashResultPointer[3] = 0x10325476;
2722 HashResultPointer[4] = 0xC3D2E1F0;
2723}
2724
James Smarte59058c2008-08-24 21:49:00 -04002725/**
James Smart3621a712009-04-06 18:47:14 -04002726 * lpfc_sha_iterate - Iterate initial hash table with the working hash table
James Smarte59058c2008-08-24 21:49:00 -04002727 * @HashResultPointer: pointer to an initial/result hash table.
2728 * @HashWorkingPointer: pointer to an working hash table.
2729 *
2730 * This routine iterates an initial hash table pointed by @HashResultPointer
2731 * with the values from the working hash table pointeed by @HashWorkingPointer.
2732 * The results are putting back to the initial hash table, returned through
2733 * the @HashResultPointer as the result hash table.
2734 **/
dea31012005-04-17 16:05:31 -05002735static void
2736lpfc_sha_iterate(uint32_t * HashResultPointer, uint32_t * HashWorkingPointer)
2737{
2738 int t;
2739 uint32_t TEMP;
2740 uint32_t A, B, C, D, E;
2741 t = 16;
2742 do {
2743 HashWorkingPointer[t] =
2744 S(1,
2745 HashWorkingPointer[t - 3] ^ HashWorkingPointer[t -
2746 8] ^
2747 HashWorkingPointer[t - 14] ^ HashWorkingPointer[t - 16]);
2748 } while (++t <= 79);
2749 t = 0;
2750 A = HashResultPointer[0];
2751 B = HashResultPointer[1];
2752 C = HashResultPointer[2];
2753 D = HashResultPointer[3];
2754 E = HashResultPointer[4];
2755
2756 do {
2757 if (t < 20) {
2758 TEMP = ((B & C) | ((~B) & D)) + 0x5A827999;
2759 } else if (t < 40) {
2760 TEMP = (B ^ C ^ D) + 0x6ED9EBA1;
2761 } else if (t < 60) {
2762 TEMP = ((B & C) | (B & D) | (C & D)) + 0x8F1BBCDC;
2763 } else {
2764 TEMP = (B ^ C ^ D) + 0xCA62C1D6;
2765 }
2766 TEMP += S(5, A) + E + HashWorkingPointer[t];
2767 E = D;
2768 D = C;
2769 C = S(30, B);
2770 B = A;
2771 A = TEMP;
2772 } while (++t <= 79);
2773
2774 HashResultPointer[0] += A;
2775 HashResultPointer[1] += B;
2776 HashResultPointer[2] += C;
2777 HashResultPointer[3] += D;
2778 HashResultPointer[4] += E;
2779
2780}
2781
James Smarte59058c2008-08-24 21:49:00 -04002782/**
James Smart3621a712009-04-06 18:47:14 -04002783 * lpfc_challenge_key - Create challenge key based on WWPN of the HBA
James Smarte59058c2008-08-24 21:49:00 -04002784 * @RandomChallenge: pointer to the entry of host challenge random number array.
2785 * @HashWorking: pointer to the entry of the working hash array.
2786 *
2787 * This routine calculates the working hash array referred by @HashWorking
2788 * from the challenge random numbers associated with the host, referred by
2789 * @RandomChallenge. The result is put into the entry of the working hash
2790 * array and returned by reference through @HashWorking.
2791 **/
dea31012005-04-17 16:05:31 -05002792static void
2793lpfc_challenge_key(uint32_t * RandomChallenge, uint32_t * HashWorking)
2794{
2795 *HashWorking = (*RandomChallenge ^ *HashWorking);
2796}
2797
James Smarte59058c2008-08-24 21:49:00 -04002798/**
James Smart3621a712009-04-06 18:47:14 -04002799 * lpfc_hba_init - Perform special handling for LC HBA initialization
James Smarte59058c2008-08-24 21:49:00 -04002800 * @phba: pointer to lpfc hba data structure.
2801 * @hbainit: pointer to an array of unsigned 32-bit integers.
2802 *
2803 * This routine performs the special handling for LC HBA initialization.
2804 **/
dea31012005-04-17 16:05:31 -05002805void
2806lpfc_hba_init(struct lpfc_hba *phba, uint32_t *hbainit)
2807{
2808 int t;
2809 uint32_t *HashWorking;
James Smart2e0fef82007-06-17 19:56:36 -05002810 uint32_t *pwwnn = (uint32_t *) phba->wwnn;
dea31012005-04-17 16:05:31 -05002811
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +02002812 HashWorking = kcalloc(80, sizeof(uint32_t), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05002813 if (!HashWorking)
2814 return;
2815
dea31012005-04-17 16:05:31 -05002816 HashWorking[0] = HashWorking[78] = *pwwnn++;
2817 HashWorking[1] = HashWorking[79] = *pwwnn;
2818
2819 for (t = 0; t < 7; t++)
2820 lpfc_challenge_key(phba->RandomData + t, HashWorking + t);
2821
2822 lpfc_sha_init(hbainit);
2823 lpfc_sha_iterate(hbainit, HashWorking);
2824 kfree(HashWorking);
2825}
2826
James Smarte59058c2008-08-24 21:49:00 -04002827/**
James Smart3621a712009-04-06 18:47:14 -04002828 * lpfc_cleanup - Performs vport cleanups before deleting a vport
James Smarte59058c2008-08-24 21:49:00 -04002829 * @vport: pointer to a virtual N_Port data structure.
2830 *
2831 * This routine performs the necessary cleanups before deleting the @vport.
2832 * It invokes the discovery state machine to perform necessary state
2833 * transitions and to release the ndlps associated with the @vport. Note,
2834 * the physical port is treated as @vport 0.
2835 **/
James Smart87af33f2007-10-27 13:37:43 -04002836void
James Smart2e0fef82007-06-17 19:56:36 -05002837lpfc_cleanup(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002838{
James Smart87af33f2007-10-27 13:37:43 -04002839 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002840 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smarta8adb832007-10-27 13:37:53 -04002841 int i = 0;
dea31012005-04-17 16:05:31 -05002842
James Smart87af33f2007-10-27 13:37:43 -04002843 if (phba->link_state > LPFC_LINK_DOWN)
2844 lpfc_port_link_failure(vport);
2845
2846 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05002847 if (!NLP_CHK_NODE_ACT(ndlp)) {
2848 ndlp = lpfc_enable_node(vport, ndlp,
2849 NLP_STE_UNUSED_NODE);
2850 if (!ndlp)
2851 continue;
2852 spin_lock_irq(&phba->ndlp_lock);
2853 NLP_SET_FREE_REQ(ndlp);
2854 spin_unlock_irq(&phba->ndlp_lock);
2855 /* Trigger the release of the ndlp memory */
2856 lpfc_nlp_put(ndlp);
2857 continue;
2858 }
2859 spin_lock_irq(&phba->ndlp_lock);
2860 if (NLP_CHK_FREE_REQ(ndlp)) {
2861 /* The ndlp should not be in memory free mode already */
2862 spin_unlock_irq(&phba->ndlp_lock);
2863 continue;
2864 } else
2865 /* Indicate request for freeing ndlp memory */
2866 NLP_SET_FREE_REQ(ndlp);
2867 spin_unlock_irq(&phba->ndlp_lock);
2868
James Smart58da1ff2008-04-07 10:15:56 -04002869 if (vport->port_type != LPFC_PHYSICAL_PORT &&
2870 ndlp->nlp_DID == Fabric_DID) {
2871 /* Just free up ndlp with Fabric_DID for vports */
2872 lpfc_nlp_put(ndlp);
2873 continue;
2874 }
2875
James Smarteff4a012012-01-18 16:25:25 -05002876 /* take care of nodes in unused state before the state
2877 * machine taking action.
2878 */
2879 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE) {
2880 lpfc_nlp_put(ndlp);
2881 continue;
2882 }
2883
James Smart87af33f2007-10-27 13:37:43 -04002884 if (ndlp->nlp_type & NLP_FABRIC)
2885 lpfc_disc_state_machine(vport, ndlp, NULL,
2886 NLP_EVT_DEVICE_RECOVERY);
James Smarte47c9092008-02-08 18:49:26 -05002887
James Smart87af33f2007-10-27 13:37:43 -04002888 lpfc_disc_state_machine(vport, ndlp, NULL,
2889 NLP_EVT_DEVICE_RM);
2890 }
2891
James Smarta8adb832007-10-27 13:37:53 -04002892 /* At this point, ALL ndlp's should be gone
2893 * because of the previous NLP_EVT_DEVICE_RM.
2894 * Lets wait for this to happen, if needed.
2895 */
James Smart87af33f2007-10-27 13:37:43 -04002896 while (!list_empty(&vport->fc_nodes)) {
James Smarta8adb832007-10-27 13:37:53 -04002897 if (i++ > 3000) {
Dick Kennedy372c1872020-06-30 14:50:00 -07002898 lpfc_printf_vlog(vport, KERN_ERR,
2899 LOG_TRACE_EVENT,
James Smarta8adb832007-10-27 13:37:53 -04002900 "0233 Nodelist not empty\n");
James Smarte47c9092008-02-08 18:49:26 -05002901 list_for_each_entry_safe(ndlp, next_ndlp,
2902 &vport->fc_nodes, nlp_listp) {
2903 lpfc_printf_vlog(ndlp->vport, KERN_ERR,
Dick Kennedy372c1872020-06-30 14:50:00 -07002904 LOG_TRACE_EVENT,
James Smart32350662019-08-14 16:57:06 -07002905 "0282 did:x%x ndlp:x%px "
James Smarte47c9092008-02-08 18:49:26 -05002906 "usgmap:x%x refcnt:%d\n",
2907 ndlp->nlp_DID, (void *)ndlp,
2908 ndlp->nlp_usg_map,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01002909 kref_read(&ndlp->kref));
James Smarte47c9092008-02-08 18:49:26 -05002910 }
James Smarta8adb832007-10-27 13:37:53 -04002911 break;
James Smart87af33f2007-10-27 13:37:43 -04002912 }
James Smarta8adb832007-10-27 13:37:53 -04002913
2914 /* Wait for any activity on ndlps to settle */
2915 msleep(10);
James Smart87af33f2007-10-27 13:37:43 -04002916 }
James Smart1151e3e2011-02-16 12:39:35 -05002917 lpfc_cleanup_vports_rrqs(vport, NULL);
dea31012005-04-17 16:05:31 -05002918}
2919
James Smarte59058c2008-08-24 21:49:00 -04002920/**
James Smart3621a712009-04-06 18:47:14 -04002921 * lpfc_stop_vport_timers - Stop all the timers associated with a vport
James Smarte59058c2008-08-24 21:49:00 -04002922 * @vport: pointer to a virtual N_Port data structure.
2923 *
2924 * This routine stops all the timers associated with a @vport. This function
2925 * is invoked before disabling or deleting a @vport. Note that the physical
2926 * port is treated as @vport 0.
2927 **/
James Smart92d7f7b2007-06-17 19:56:38 -05002928void
2929lpfc_stop_vport_timers(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002930{
James Smart92d7f7b2007-06-17 19:56:38 -05002931 del_timer_sync(&vport->els_tmofunc);
James Smart92494142011-02-16 12:39:44 -05002932 del_timer_sync(&vport->delayed_disc_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05002933 lpfc_can_disctmo(vport);
2934 return;
dea31012005-04-17 16:05:31 -05002935}
2936
James Smarte59058c2008-08-24 21:49:00 -04002937/**
James Smartecfd03c2010-02-12 14:41:27 -05002938 * __lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2939 * @phba: pointer to lpfc hba data structure.
2940 *
2941 * This routine stops the SLI4 FCF rediscover wait timer if it's on. The
2942 * caller of this routine should already hold the host lock.
2943 **/
2944void
2945__lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2946{
James Smart5ac6b302010-10-22 11:05:36 -04002947 /* Clear pending FCF rediscovery wait flag */
2948 phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
2949
James Smartecfd03c2010-02-12 14:41:27 -05002950 /* Now, try to stop the timer */
2951 del_timer(&phba->fcf.redisc_wait);
2952}
2953
2954/**
2955 * lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2956 * @phba: pointer to lpfc hba data structure.
2957 *
2958 * This routine stops the SLI4 FCF rediscover wait timer if it's on. It
2959 * checks whether the FCF rediscovery wait timer is pending with the host
2960 * lock held before proceeding with disabling the timer and clearing the
2961 * wait timer pendig flag.
2962 **/
2963void
2964lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2965{
2966 spin_lock_irq(&phba->hbalock);
2967 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
2968 /* FCF rediscovery timer already fired or stopped */
2969 spin_unlock_irq(&phba->hbalock);
2970 return;
2971 }
2972 __lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
James Smart5ac6b302010-10-22 11:05:36 -04002973 /* Clear failover in progress flags */
2974 phba->fcf.fcf_flag &= ~(FCF_DEAD_DISC | FCF_ACVL_DISC);
James Smartecfd03c2010-02-12 14:41:27 -05002975 spin_unlock_irq(&phba->hbalock);
2976}
2977
2978/**
James Smart3772a992009-05-22 14:50:54 -04002979 * lpfc_stop_hba_timers - Stop all the timers associated with an HBA
James Smarte59058c2008-08-24 21:49:00 -04002980 * @phba: pointer to lpfc hba data structure.
2981 *
2982 * This routine stops all the timers associated with a HBA. This function is
2983 * invoked before either putting a HBA offline or unloading the driver.
2984 **/
James Smart3772a992009-05-22 14:50:54 -04002985void
2986lpfc_stop_hba_timers(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002987{
James Smartcdb42be2019-01-28 11:14:21 -08002988 if (phba->pport)
2989 lpfc_stop_vport_timers(phba->pport);
James Smart32517fc2019-01-28 11:14:33 -08002990 cancel_delayed_work_sync(&phba->eq_delay_work);
Dick Kennedy317aeb82020-06-30 14:49:59 -07002991 cancel_delayed_work_sync(&phba->idle_stat_delay_work);
James Smart2e0fef82007-06-17 19:56:36 -05002992 del_timer_sync(&phba->sli.mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05002993 del_timer_sync(&phba->fabric_block_timer);
James Smart93996272008-08-24 21:50:30 -04002994 del_timer_sync(&phba->eratt_poll);
James Smart3772a992009-05-22 14:50:54 -04002995 del_timer_sync(&phba->hb_tmofunc);
James Smart1151e3e2011-02-16 12:39:35 -05002996 if (phba->sli_rev == LPFC_SLI_REV4) {
2997 del_timer_sync(&phba->rrq_tmr);
2998 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
2999 }
James Smart3772a992009-05-22 14:50:54 -04003000 phba->hb_outstanding = 0;
3001
3002 switch (phba->pci_dev_grp) {
3003 case LPFC_PCI_DEV_LP:
3004 /* Stop any LightPulse device specific driver timers */
3005 del_timer_sync(&phba->fcp_poll_timer);
3006 break;
3007 case LPFC_PCI_DEV_OC:
Paul Walmsleycc0e5f12019-07-11 20:52:33 -07003008 /* Stop any OneConnect device specific driver timers */
James Smartecfd03c2010-02-12 14:41:27 -05003009 lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
James Smart3772a992009-05-22 14:50:54 -04003010 break;
3011 default:
Dick Kennedy372c1872020-06-30 14:50:00 -07003012 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart3772a992009-05-22 14:50:54 -04003013 "0297 Invalid device group (x%x)\n",
3014 phba->pci_dev_grp);
3015 break;
3016 }
James Smart2e0fef82007-06-17 19:56:36 -05003017 return;
dea31012005-04-17 16:05:31 -05003018}
3019
James Smarte59058c2008-08-24 21:49:00 -04003020/**
James Smart3621a712009-04-06 18:47:14 -04003021 * lpfc_block_mgmt_io - Mark a HBA's management interface as blocked
James Smarte59058c2008-08-24 21:49:00 -04003022 * @phba: pointer to lpfc hba data structure.
Lee Jonesfe614ac2020-07-23 13:24:22 +01003023 * @mbx_action: flag for mailbox no wait action.
James Smarte59058c2008-08-24 21:49:00 -04003024 *
3025 * This routine marks a HBA's management interface as blocked. Once the HBA's
3026 * management interface is marked as blocked, all the user space access to
3027 * the HBA, whether they are from sysfs interface or libdfc interface will
3028 * all be blocked. The HBA is set to block the management interface when the
3029 * driver prepares the HBA interface for online or offline.
3030 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01003031static void
James Smart618a5232012-06-12 13:54:36 -04003032lpfc_block_mgmt_io(struct lpfc_hba *phba, int mbx_action)
Adrian Bunka6ababd2007-11-05 18:07:33 +01003033{
3034 unsigned long iflag;
James Smart6e7288d2010-06-07 15:23:35 -04003035 uint8_t actcmd = MBX_HEARTBEAT;
3036 unsigned long timeout;
3037
Adrian Bunka6ababd2007-11-05 18:07:33 +01003038 spin_lock_irqsave(&phba->hbalock, iflag);
3039 phba->sli.sli_flag |= LPFC_BLOCK_MGMT_IO;
James Smart618a5232012-06-12 13:54:36 -04003040 spin_unlock_irqrestore(&phba->hbalock, iflag);
3041 if (mbx_action == LPFC_MBX_NO_WAIT)
3042 return;
3043 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
3044 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta183a152011-10-10 21:32:43 -04003045 if (phba->sli.mbox_active) {
James Smart6e7288d2010-06-07 15:23:35 -04003046 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
James Smarta183a152011-10-10 21:32:43 -04003047 /* Determine how long we might wait for the active mailbox
3048 * command to be gracefully completed by firmware.
3049 */
3050 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
3051 phba->sli.mbox_active) * 1000) + jiffies;
3052 }
Adrian Bunka6ababd2007-11-05 18:07:33 +01003053 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta183a152011-10-10 21:32:43 -04003054
James Smart6e7288d2010-06-07 15:23:35 -04003055 /* Wait for the outstnading mailbox command to complete */
3056 while (phba->sli.mbox_active) {
3057 /* Check active mailbox complete status every 2ms */
3058 msleep(2);
3059 if (time_after(jiffies, timeout)) {
Dick Kennedy372c1872020-06-30 14:50:00 -07003060 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3061 "2813 Mgmt IO is Blocked %x "
3062 "- mbox cmd %x still active\n",
3063 phba->sli.sli_flag, actcmd);
James Smart6e7288d2010-06-07 15:23:35 -04003064 break;
3065 }
3066 }
Adrian Bunka6ababd2007-11-05 18:07:33 +01003067}
3068
James Smarte59058c2008-08-24 21:49:00 -04003069/**
James Smart6b5151f2012-01-18 16:24:06 -05003070 * lpfc_sli4_node_prep - Assign RPIs for active nodes.
3071 * @phba: pointer to lpfc hba data structure.
3072 *
3073 * Allocate RPIs for all active remote nodes. This is needed whenever
3074 * an SLI4 adapter is reset and the driver is not unloading. Its purpose
3075 * is to fixup the temporary rpi assignments.
3076 **/
3077void
3078lpfc_sli4_node_prep(struct lpfc_hba *phba)
3079{
3080 struct lpfc_nodelist *ndlp, *next_ndlp;
3081 struct lpfc_vport **vports;
James Smart9d3d3402017-04-21 16:05:00 -07003082 int i, rpi;
3083 unsigned long flags;
James Smart6b5151f2012-01-18 16:24:06 -05003084
3085 if (phba->sli_rev != LPFC_SLI_REV4)
3086 return;
3087
3088 vports = lpfc_create_vport_work_array(phba);
James Smart9d3d3402017-04-21 16:05:00 -07003089 if (vports == NULL)
3090 return;
James Smart6b5151f2012-01-18 16:24:06 -05003091
James Smart9d3d3402017-04-21 16:05:00 -07003092 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
3093 if (vports[i]->load_flag & FC_UNLOADING)
3094 continue;
3095
3096 list_for_each_entry_safe(ndlp, next_ndlp,
3097 &vports[i]->fc_nodes,
3098 nlp_listp) {
3099 if (!NLP_CHK_NODE_ACT(ndlp))
3100 continue;
3101 rpi = lpfc_sli4_alloc_rpi(phba);
3102 if (rpi == LPFC_RPI_ALLOC_ERROR) {
3103 spin_lock_irqsave(&phba->ndlp_lock, flags);
3104 NLP_CLR_NODE_ACT(ndlp);
3105 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
3106 continue;
James Smart6b5151f2012-01-18 16:24:06 -05003107 }
James Smart9d3d3402017-04-21 16:05:00 -07003108 ndlp->nlp_rpi = rpi;
James Smart0f154222019-09-21 20:58:52 -07003109 lpfc_printf_vlog(ndlp->vport, KERN_INFO,
3110 LOG_NODE | LOG_DISCOVERY,
3111 "0009 Assign RPI x%x to ndlp x%px "
3112 "DID:x%06x flg:x%x map:x%x\n",
3113 ndlp->nlp_rpi, ndlp, ndlp->nlp_DID,
3114 ndlp->nlp_flag, ndlp->nlp_usg_map);
James Smart6b5151f2012-01-18 16:24:06 -05003115 }
3116 }
3117 lpfc_destroy_vport_work_array(phba, vports);
3118}
3119
3120/**
James Smartc4908502019-01-28 11:14:28 -08003121 * lpfc_create_expedite_pool - create expedite pool
3122 * @phba: pointer to lpfc hba data structure.
3123 *
3124 * This routine moves a batch of XRIs from lpfc_io_buf_list_put of HWQ 0
3125 * to expedite pool. Mark them as expedite.
3126 **/
Bart Van Assche3999df72019-03-28 11:06:16 -07003127static void lpfc_create_expedite_pool(struct lpfc_hba *phba)
James Smartc4908502019-01-28 11:14:28 -08003128{
3129 struct lpfc_sli4_hdw_queue *qp;
3130 struct lpfc_io_buf *lpfc_ncmd;
3131 struct lpfc_io_buf *lpfc_ncmd_next;
3132 struct lpfc_epd_pool *epd_pool;
3133 unsigned long iflag;
3134
3135 epd_pool = &phba->epd_pool;
3136 qp = &phba->sli4_hba.hdwq[0];
3137
3138 spin_lock_init(&epd_pool->lock);
3139 spin_lock_irqsave(&qp->io_buf_list_put_lock, iflag);
3140 spin_lock(&epd_pool->lock);
3141 INIT_LIST_HEAD(&epd_pool->list);
3142 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
3143 &qp->lpfc_io_buf_list_put, list) {
3144 list_move_tail(&lpfc_ncmd->list, &epd_pool->list);
3145 lpfc_ncmd->expedite = true;
3146 qp->put_io_bufs--;
3147 epd_pool->count++;
3148 if (epd_pool->count >= XRI_BATCH)
3149 break;
3150 }
3151 spin_unlock(&epd_pool->lock);
3152 spin_unlock_irqrestore(&qp->io_buf_list_put_lock, iflag);
3153}
3154
3155/**
3156 * lpfc_destroy_expedite_pool - destroy expedite pool
3157 * @phba: pointer to lpfc hba data structure.
3158 *
3159 * This routine returns XRIs from expedite pool to lpfc_io_buf_list_put
3160 * of HWQ 0. Clear the mark.
3161 **/
Bart Van Assche3999df72019-03-28 11:06:16 -07003162static void lpfc_destroy_expedite_pool(struct lpfc_hba *phba)
James Smartc4908502019-01-28 11:14:28 -08003163{
3164 struct lpfc_sli4_hdw_queue *qp;
3165 struct lpfc_io_buf *lpfc_ncmd;
3166 struct lpfc_io_buf *lpfc_ncmd_next;
3167 struct lpfc_epd_pool *epd_pool;
3168 unsigned long iflag;
3169
3170 epd_pool = &phba->epd_pool;
3171 qp = &phba->sli4_hba.hdwq[0];
3172
3173 spin_lock_irqsave(&qp->io_buf_list_put_lock, iflag);
3174 spin_lock(&epd_pool->lock);
3175 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
3176 &epd_pool->list, list) {
3177 list_move_tail(&lpfc_ncmd->list,
3178 &qp->lpfc_io_buf_list_put);
3179 lpfc_ncmd->flags = false;
3180 qp->put_io_bufs++;
3181 epd_pool->count--;
3182 }
3183 spin_unlock(&epd_pool->lock);
3184 spin_unlock_irqrestore(&qp->io_buf_list_put_lock, iflag);
3185}
3186
3187/**
3188 * lpfc_create_multixri_pools - create multi-XRI pools
3189 * @phba: pointer to lpfc hba data structure.
3190 *
3191 * This routine initialize public, private per HWQ. Then, move XRIs from
3192 * lpfc_io_buf_list_put to public pool. High and low watermark are also
3193 * Initialized.
3194 **/
3195void lpfc_create_multixri_pools(struct lpfc_hba *phba)
3196{
3197 u32 i, j;
3198 u32 hwq_count;
3199 u32 count_per_hwq;
3200 struct lpfc_io_buf *lpfc_ncmd;
3201 struct lpfc_io_buf *lpfc_ncmd_next;
3202 unsigned long iflag;
3203 struct lpfc_sli4_hdw_queue *qp;
3204 struct lpfc_multixri_pool *multixri_pool;
3205 struct lpfc_pbl_pool *pbl_pool;
3206 struct lpfc_pvt_pool *pvt_pool;
3207
3208 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3209 "1234 num_hdw_queue=%d num_present_cpu=%d common_xri_cnt=%d\n",
3210 phba->cfg_hdw_queue, phba->sli4_hba.num_present_cpu,
3211 phba->sli4_hba.io_xri_cnt);
3212
3213 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
3214 lpfc_create_expedite_pool(phba);
3215
3216 hwq_count = phba->cfg_hdw_queue;
3217 count_per_hwq = phba->sli4_hba.io_xri_cnt / hwq_count;
3218
3219 for (i = 0; i < hwq_count; i++) {
3220 multixri_pool = kzalloc(sizeof(*multixri_pool), GFP_KERNEL);
3221
3222 if (!multixri_pool) {
3223 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3224 "1238 Failed to allocate memory for "
3225 "multixri_pool\n");
3226
3227 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
3228 lpfc_destroy_expedite_pool(phba);
3229
3230 j = 0;
3231 while (j < i) {
3232 qp = &phba->sli4_hba.hdwq[j];
3233 kfree(qp->p_multixri_pool);
3234 j++;
3235 }
3236 phba->cfg_xri_rebalancing = 0;
3237 return;
3238 }
3239
3240 qp = &phba->sli4_hba.hdwq[i];
3241 qp->p_multixri_pool = multixri_pool;
3242
3243 multixri_pool->xri_limit = count_per_hwq;
3244 multixri_pool->rrb_next_hwqid = i;
3245
3246 /* Deal with public free xri pool */
3247 pbl_pool = &multixri_pool->pbl_pool;
3248 spin_lock_init(&pbl_pool->lock);
3249 spin_lock_irqsave(&qp->io_buf_list_put_lock, iflag);
3250 spin_lock(&pbl_pool->lock);
3251 INIT_LIST_HEAD(&pbl_pool->list);
3252 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
3253 &qp->lpfc_io_buf_list_put, list) {
3254 list_move_tail(&lpfc_ncmd->list, &pbl_pool->list);
3255 qp->put_io_bufs--;
3256 pbl_pool->count++;
3257 }
3258 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3259 "1235 Moved %d buffers from PUT list over to pbl_pool[%d]\n",
3260 pbl_pool->count, i);
3261 spin_unlock(&pbl_pool->lock);
3262 spin_unlock_irqrestore(&qp->io_buf_list_put_lock, iflag);
3263
3264 /* Deal with private free xri pool */
3265 pvt_pool = &multixri_pool->pvt_pool;
3266 pvt_pool->high_watermark = multixri_pool->xri_limit / 2;
3267 pvt_pool->low_watermark = XRI_BATCH;
3268 spin_lock_init(&pvt_pool->lock);
3269 spin_lock_irqsave(&pvt_pool->lock, iflag);
3270 INIT_LIST_HEAD(&pvt_pool->list);
3271 pvt_pool->count = 0;
3272 spin_unlock_irqrestore(&pvt_pool->lock, iflag);
3273 }
3274}
3275
3276/**
3277 * lpfc_destroy_multixri_pools - destroy multi-XRI pools
3278 * @phba: pointer to lpfc hba data structure.
3279 *
3280 * This routine returns XRIs from public/private to lpfc_io_buf_list_put.
3281 **/
Bart Van Assche3999df72019-03-28 11:06:16 -07003282static void lpfc_destroy_multixri_pools(struct lpfc_hba *phba)
James Smartc4908502019-01-28 11:14:28 -08003283{
3284 u32 i;
3285 u32 hwq_count;
3286 struct lpfc_io_buf *lpfc_ncmd;
3287 struct lpfc_io_buf *lpfc_ncmd_next;
3288 unsigned long iflag;
3289 struct lpfc_sli4_hdw_queue *qp;
3290 struct lpfc_multixri_pool *multixri_pool;
3291 struct lpfc_pbl_pool *pbl_pool;
3292 struct lpfc_pvt_pool *pvt_pool;
3293
3294 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
3295 lpfc_destroy_expedite_pool(phba);
3296
James Smartc00f62e2019-08-14 16:57:11 -07003297 if (!(phba->pport->load_flag & FC_UNLOADING))
3298 lpfc_sli_flush_io_rings(phba);
James Smartc66a9192019-03-12 16:30:19 -07003299
James Smartc4908502019-01-28 11:14:28 -08003300 hwq_count = phba->cfg_hdw_queue;
3301
3302 for (i = 0; i < hwq_count; i++) {
3303 qp = &phba->sli4_hba.hdwq[i];
3304 multixri_pool = qp->p_multixri_pool;
3305 if (!multixri_pool)
3306 continue;
3307
3308 qp->p_multixri_pool = NULL;
3309
3310 spin_lock_irqsave(&qp->io_buf_list_put_lock, iflag);
3311
3312 /* Deal with public free xri pool */
3313 pbl_pool = &multixri_pool->pbl_pool;
3314 spin_lock(&pbl_pool->lock);
3315
3316 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3317 "1236 Moving %d buffers from pbl_pool[%d] TO PUT list\n",
3318 pbl_pool->count, i);
3319
3320 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
3321 &pbl_pool->list, list) {
3322 list_move_tail(&lpfc_ncmd->list,
3323 &qp->lpfc_io_buf_list_put);
3324 qp->put_io_bufs++;
3325 pbl_pool->count--;
3326 }
3327
3328 INIT_LIST_HEAD(&pbl_pool->list);
3329 pbl_pool->count = 0;
3330
3331 spin_unlock(&pbl_pool->lock);
3332
3333 /* Deal with private free xri pool */
3334 pvt_pool = &multixri_pool->pvt_pool;
3335 spin_lock(&pvt_pool->lock);
3336
3337 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3338 "1237 Moving %d buffers from pvt_pool[%d] TO PUT list\n",
3339 pvt_pool->count, i);
3340
3341 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
3342 &pvt_pool->list, list) {
3343 list_move_tail(&lpfc_ncmd->list,
3344 &qp->lpfc_io_buf_list_put);
3345 qp->put_io_bufs++;
3346 pvt_pool->count--;
3347 }
3348
3349 INIT_LIST_HEAD(&pvt_pool->list);
3350 pvt_pool->count = 0;
3351
3352 spin_unlock(&pvt_pool->lock);
3353 spin_unlock_irqrestore(&qp->io_buf_list_put_lock, iflag);
3354
3355 kfree(multixri_pool);
3356 }
3357}
3358
3359/**
James Smart3621a712009-04-06 18:47:14 -04003360 * lpfc_online - Initialize and bring a HBA online
James Smarte59058c2008-08-24 21:49:00 -04003361 * @phba: pointer to lpfc hba data structure.
3362 *
3363 * This routine initializes the HBA and brings a HBA online. During this
3364 * process, the management interface is blocked to prevent user space access
3365 * to the HBA interfering with the driver initialization.
3366 *
3367 * Return codes
3368 * 0 - successful
3369 * 1 - failed
3370 **/
dea31012005-04-17 16:05:31 -05003371int
James Smart2e0fef82007-06-17 19:56:36 -05003372lpfc_online(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05003373{
Julia Lawall372bd282008-12-16 16:15:08 +01003374 struct lpfc_vport *vport;
James Smart549e55c2007-08-02 11:09:51 -04003375 struct lpfc_vport **vports;
Dick Kennedya145fda2017-08-23 16:55:44 -07003376 int i, error = 0;
James Smart16a3a202013-04-17 20:14:38 -04003377 bool vpis_cleared = false;
James Smart2e0fef82007-06-17 19:56:36 -05003378
dea31012005-04-17 16:05:31 -05003379 if (!phba)
3380 return 0;
Julia Lawall372bd282008-12-16 16:15:08 +01003381 vport = phba->pport;
dea31012005-04-17 16:05:31 -05003382
James Smart2e0fef82007-06-17 19:56:36 -05003383 if (!(vport->fc_flag & FC_OFFLINE_MODE))
dea31012005-04-17 16:05:31 -05003384 return 0;
3385
James Smarted957682007-06-17 19:56:37 -05003386 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003387 "0458 Bring Adapter online\n");
dea31012005-04-17 16:05:31 -05003388
James Smart618a5232012-06-12 13:54:36 -04003389 lpfc_block_mgmt_io(phba, LPFC_MBX_WAIT);
dea31012005-04-17 16:05:31 -05003390
James Smartda0436e2009-05-22 14:51:39 -04003391 if (phba->sli_rev == LPFC_SLI_REV4) {
3392 if (lpfc_sli4_hba_setup(phba)) { /* Initialize SLI4 HBA */
3393 lpfc_unblock_mgmt_io(phba);
3394 return 1;
3395 }
James Smart16a3a202013-04-17 20:14:38 -04003396 spin_lock_irq(&phba->hbalock);
3397 if (!phba->sli4_hba.max_cfg_param.vpi_used)
3398 vpis_cleared = true;
3399 spin_unlock_irq(&phba->hbalock);
Dick Kennedya145fda2017-08-23 16:55:44 -07003400
3401 /* Reestablish the local initiator port.
3402 * The offline process destroyed the previous lport.
3403 */
3404 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME &&
3405 !phba->nvmet_support) {
3406 error = lpfc_nvme_create_localport(phba->pport);
3407 if (error)
Dick Kennedy372c1872020-06-30 14:50:00 -07003408 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
Dick Kennedya145fda2017-08-23 16:55:44 -07003409 "6132 NVME restore reg failed "
3410 "on nvmei error x%x\n", error);
3411 }
James Smartda0436e2009-05-22 14:51:39 -04003412 } else {
James Smart895427b2017-02-12 13:52:30 -08003413 lpfc_sli_queue_init(phba);
James Smartda0436e2009-05-22 14:51:39 -04003414 if (lpfc_sli_hba_setup(phba)) { /* Initialize SLI2/SLI3 HBA */
3415 lpfc_unblock_mgmt_io(phba);
3416 return 1;
3417 }
James Smart46fa3112007-04-25 09:51:45 -04003418 }
dea31012005-04-17 16:05:31 -05003419
James Smart549e55c2007-08-02 11:09:51 -04003420 vports = lpfc_create_vport_work_array(phba);
Arnd Bergmannaeb66412016-03-14 15:29:44 +01003421 if (vports != NULL) {
James Smartda0436e2009-05-22 14:51:39 -04003422 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -04003423 struct Scsi_Host *shost;
3424 shost = lpfc_shost_from_vport(vports[i]);
3425 spin_lock_irq(shost->host_lock);
3426 vports[i]->fc_flag &= ~FC_OFFLINE_MODE;
3427 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
3428 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart16a3a202013-04-17 20:14:38 -04003429 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart1c6834a2009-07-19 10:01:26 -04003430 vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
James Smart16a3a202013-04-17 20:14:38 -04003431 if ((vpis_cleared) &&
3432 (vports[i]->port_type !=
3433 LPFC_PHYSICAL_PORT))
3434 vports[i]->vpi = 0;
3435 }
James Smart549e55c2007-08-02 11:09:51 -04003436 spin_unlock_irq(shost->host_lock);
3437 }
Arnd Bergmannaeb66412016-03-14 15:29:44 +01003438 }
3439 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05003440
James Smartc4908502019-01-28 11:14:28 -08003441 if (phba->cfg_xri_rebalancing)
3442 lpfc_create_multixri_pools(phba);
3443
James Smart93a4d6f2019-11-04 16:57:05 -08003444 lpfc_cpuhp_add(phba);
3445
James Smart46fa3112007-04-25 09:51:45 -04003446 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05003447 return 0;
3448}
3449
James Smarte59058c2008-08-24 21:49:00 -04003450/**
James Smart3621a712009-04-06 18:47:14 -04003451 * lpfc_unblock_mgmt_io - Mark a HBA's management interface to be not blocked
James Smarte59058c2008-08-24 21:49:00 -04003452 * @phba: pointer to lpfc hba data structure.
3453 *
3454 * This routine marks a HBA's management interface as not blocked. Once the
3455 * HBA's management interface is marked as not blocked, all the user space
3456 * access to the HBA, whether they are from sysfs interface or libdfc
3457 * interface will be allowed. The HBA is set to block the management interface
3458 * when the driver prepares the HBA interface for online or offline and then
3459 * set to unblock the management interface afterwards.
3460 **/
James Smart46fa3112007-04-25 09:51:45 -04003461void
James Smart46fa3112007-04-25 09:51:45 -04003462lpfc_unblock_mgmt_io(struct lpfc_hba * phba)
3463{
3464 unsigned long iflag;
3465
James Smart2e0fef82007-06-17 19:56:36 -05003466 spin_lock_irqsave(&phba->hbalock, iflag);
3467 phba->sli.sli_flag &= ~LPFC_BLOCK_MGMT_IO;
3468 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart46fa3112007-04-25 09:51:45 -04003469}
3470
James Smarte59058c2008-08-24 21:49:00 -04003471/**
James Smart3621a712009-04-06 18:47:14 -04003472 * lpfc_offline_prep - Prepare a HBA to be brought offline
James Smarte59058c2008-08-24 21:49:00 -04003473 * @phba: pointer to lpfc hba data structure.
Lee Jonesfe614ac2020-07-23 13:24:22 +01003474 * @mbx_action: flag for mailbox shutdown action.
James Smarte59058c2008-08-24 21:49:00 -04003475 *
3476 * This routine is invoked to prepare a HBA to be brought offline. It performs
3477 * unregistration login to all the nodes on all vports and flushes the mailbox
3478 * queue to make it ready to be brought offline.
3479 **/
James Smart46fa3112007-04-25 09:51:45 -04003480void
James Smart618a5232012-06-12 13:54:36 -04003481lpfc_offline_prep(struct lpfc_hba *phba, int mbx_action)
James Smart46fa3112007-04-25 09:51:45 -04003482{
James Smart2e0fef82007-06-17 19:56:36 -05003483 struct lpfc_vport *vport = phba->pport;
James Smart46fa3112007-04-25 09:51:45 -04003484 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart87af33f2007-10-27 13:37:43 -04003485 struct lpfc_vport **vports;
James Smart72100cc2010-02-12 14:43:01 -05003486 struct Scsi_Host *shost;
James Smart87af33f2007-10-27 13:37:43 -04003487 int i;
dea31012005-04-17 16:05:31 -05003488
James Smart2e0fef82007-06-17 19:56:36 -05003489 if (vport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04003490 return;
dea31012005-04-17 16:05:31 -05003491
James Smart618a5232012-06-12 13:54:36 -04003492 lpfc_block_mgmt_io(phba, mbx_action);
dea31012005-04-17 16:05:31 -05003493
3494 lpfc_linkdown(phba);
3495
James Smart87af33f2007-10-27 13:37:43 -04003496 /* Issue an unreg_login to all nodes on all vports */
3497 vports = lpfc_create_vport_work_array(phba);
3498 if (vports != NULL) {
James Smartda0436e2009-05-22 14:51:39 -04003499 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smarta8adb832007-10-27 13:37:53 -04003500 if (vports[i]->load_flag & FC_UNLOADING)
3501 continue;
James Smart72100cc2010-02-12 14:43:01 -05003502 shost = lpfc_shost_from_vport(vports[i]);
3503 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05003504 vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05003505 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
3506 vports[i]->fc_flag &= ~FC_VFI_REGISTERED;
James Smart72100cc2010-02-12 14:43:01 -05003507 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05003508
James Smart87af33f2007-10-27 13:37:43 -04003509 shost = lpfc_shost_from_vport(vports[i]);
3510 list_for_each_entry_safe(ndlp, next_ndlp,
3511 &vports[i]->fc_nodes,
3512 nlp_listp) {
James Smart0f154222019-09-21 20:58:52 -07003513 if ((!NLP_CHK_NODE_ACT(ndlp)) ||
3514 ndlp->nlp_state == NLP_STE_UNUSED_NODE) {
3515 /* Driver must assume RPI is invalid for
3516 * any unused or inactive node.
3517 */
3518 ndlp->nlp_rpi = LPFC_RPI_ALLOC_ERROR;
James Smarte47c9092008-02-08 18:49:26 -05003519 continue;
James Smart0f154222019-09-21 20:58:52 -07003520 }
3521
James Smart87af33f2007-10-27 13:37:43 -04003522 if (ndlp->nlp_type & NLP_FABRIC) {
3523 lpfc_disc_state_machine(vports[i], ndlp,
3524 NULL, NLP_EVT_DEVICE_RECOVERY);
3525 lpfc_disc_state_machine(vports[i], ndlp,
3526 NULL, NLP_EVT_DEVICE_RM);
3527 }
3528 spin_lock_irq(shost->host_lock);
3529 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
James Smart401ee0c2012-03-01 22:35:34 -05003530 spin_unlock_irq(shost->host_lock);
James Smart6b5151f2012-01-18 16:24:06 -05003531 /*
3532 * Whenever an SLI4 port goes offline, free the
James Smart401ee0c2012-03-01 22:35:34 -05003533 * RPI. Get a new RPI when the adapter port
3534 * comes back online.
James Smart6b5151f2012-01-18 16:24:06 -05003535 */
James Smartbe6bb942015-04-07 15:07:22 -04003536 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart0f154222019-09-21 20:58:52 -07003537 lpfc_printf_vlog(ndlp->vport, KERN_INFO,
3538 LOG_NODE | LOG_DISCOVERY,
3539 "0011 Free RPI x%x on "
3540 "ndlp:x%px did x%x "
3541 "usgmap:x%x\n",
3542 ndlp->nlp_rpi, ndlp,
3543 ndlp->nlp_DID,
3544 ndlp->nlp_usg_map);
James Smart6b5151f2012-01-18 16:24:06 -05003545 lpfc_sli4_free_rpi(phba, ndlp->nlp_rpi);
James Smart0f154222019-09-21 20:58:52 -07003546 ndlp->nlp_rpi = LPFC_RPI_ALLOC_ERROR;
James Smartbe6bb942015-04-07 15:07:22 -04003547 }
James Smart87af33f2007-10-27 13:37:43 -04003548 lpfc_unreg_rpi(vports[i], ndlp);
3549 }
3550 }
3551 }
James Smart09372822008-01-11 01:52:54 -05003552 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05003553
James Smart618a5232012-06-12 13:54:36 -04003554 lpfc_sli_mbox_sys_shutdown(phba, mbx_action);
Dick Kennedyf485c182017-09-29 17:34:34 -07003555
3556 if (phba->wq)
3557 flush_workqueue(phba->wq);
James Smart46fa3112007-04-25 09:51:45 -04003558}
3559
James Smarte59058c2008-08-24 21:49:00 -04003560/**
James Smart3621a712009-04-06 18:47:14 -04003561 * lpfc_offline - Bring a HBA offline
James Smarte59058c2008-08-24 21:49:00 -04003562 * @phba: pointer to lpfc hba data structure.
3563 *
3564 * This routine actually brings a HBA offline. It stops all the timers
3565 * associated with the HBA, brings down the SLI layer, and eventually
3566 * marks the HBA as in offline state for the upper layer protocol.
3567 **/
James Smart46fa3112007-04-25 09:51:45 -04003568void
James Smart2e0fef82007-06-17 19:56:36 -05003569lpfc_offline(struct lpfc_hba *phba)
James Smart46fa3112007-04-25 09:51:45 -04003570{
James Smart549e55c2007-08-02 11:09:51 -04003571 struct Scsi_Host *shost;
3572 struct lpfc_vport **vports;
3573 int i;
James Smart46fa3112007-04-25 09:51:45 -04003574
James Smart549e55c2007-08-02 11:09:51 -04003575 if (phba->pport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04003576 return;
James Smart688a8862006-07-06 15:49:56 -04003577
James Smartda0436e2009-05-22 14:51:39 -04003578 /* stop port and all timers associated with this hba */
3579 lpfc_stop_port(phba);
Dick Kennedy4b40d022017-08-23 16:55:38 -07003580
3581 /* Tear down the local and target port registrations. The
3582 * nvme transports need to cleanup.
3583 */
3584 lpfc_nvmet_destroy_targetport(phba);
3585 lpfc_nvme_destroy_localport(phba->pport);
3586
James Smart51ef4c22007-08-02 11:10:31 -04003587 vports = lpfc_create_vport_work_array(phba);
3588 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04003589 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
James Smart51ef4c22007-08-02 11:10:31 -04003590 lpfc_stop_vport_timers(vports[i]);
James Smart09372822008-01-11 01:52:54 -05003591 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -05003592 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003593 "0460 Bring Adapter offline\n");
dea31012005-04-17 16:05:31 -05003594 /* Bring down the SLI Layer and cleanup. The HBA is offline
3595 now. */
3596 lpfc_sli_hba_down(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05003597 spin_lock_irq(&phba->hbalock);
James Smart7054a602007-04-25 09:52:34 -04003598 phba->work_ha = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05003599 spin_unlock_irq(&phba->hbalock);
James Smart549e55c2007-08-02 11:09:51 -04003600 vports = lpfc_create_vport_work_array(phba);
3601 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04003602 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -04003603 shost = lpfc_shost_from_vport(vports[i]);
James Smart549e55c2007-08-02 11:09:51 -04003604 spin_lock_irq(shost->host_lock);
3605 vports[i]->work_port_events = 0;
3606 vports[i]->fc_flag |= FC_OFFLINE_MODE;
3607 spin_unlock_irq(shost->host_lock);
3608 }
James Smart09372822008-01-11 01:52:54 -05003609 lpfc_destroy_vport_work_array(phba, vports);
James Smart93a4d6f2019-11-04 16:57:05 -08003610 __lpfc_cpuhp_remove(phba);
James Smartc4908502019-01-28 11:14:28 -08003611
3612 if (phba->cfg_xri_rebalancing)
3613 lpfc_destroy_multixri_pools(phba);
dea31012005-04-17 16:05:31 -05003614}
3615
James Smarte59058c2008-08-24 21:49:00 -04003616/**
James Smart3621a712009-04-06 18:47:14 -04003617 * lpfc_scsi_free - Free all the SCSI buffers and IOCBs from driver lists
James Smarte59058c2008-08-24 21:49:00 -04003618 * @phba: pointer to lpfc hba data structure.
3619 *
3620 * This routine is to free all the SCSI buffers and IOCBs from the driver
3621 * list back to kernel. It is called from lpfc_pci_remove_one to free
3622 * the internal resources before the device is removed from the system.
James Smarte59058c2008-08-24 21:49:00 -04003623 **/
James Smart8a9d2e82012-05-09 21:16:12 -04003624static void
James Smart2e0fef82007-06-17 19:56:36 -05003625lpfc_scsi_free(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05003626{
James Smartc4908502019-01-28 11:14:28 -08003627 struct lpfc_io_buf *sb, *sb_next;
dea31012005-04-17 16:05:31 -05003628
James Smart895427b2017-02-12 13:52:30 -08003629 if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP))
3630 return;
3631
James Smart2e0fef82007-06-17 19:56:36 -05003632 spin_lock_irq(&phba->hbalock);
James Smarta40fc5f2013-04-17 20:17:40 -04003633
dea31012005-04-17 16:05:31 -05003634 /* Release all the lpfc_scsi_bufs maintained by this host. */
James Smarta40fc5f2013-04-17 20:17:40 -04003635
3636 spin_lock(&phba->scsi_buf_list_put_lock);
3637 list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list_put,
3638 list) {
dea31012005-04-17 16:05:31 -05003639 list_del(&sb->list);
Romain Perier771db5c2017-07-06 10:13:05 +02003640 dma_pool_free(phba->lpfc_sg_dma_buf_pool, sb->data,
James Smart92d7f7b2007-06-17 19:56:38 -05003641 sb->dma_handle);
dea31012005-04-17 16:05:31 -05003642 kfree(sb);
3643 phba->total_scsi_bufs--;
3644 }
James Smarta40fc5f2013-04-17 20:17:40 -04003645 spin_unlock(&phba->scsi_buf_list_put_lock);
3646
3647 spin_lock(&phba->scsi_buf_list_get_lock);
3648 list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list_get,
3649 list) {
3650 list_del(&sb->list);
Romain Perier771db5c2017-07-06 10:13:05 +02003651 dma_pool_free(phba->lpfc_sg_dma_buf_pool, sb->data,
James Smarta40fc5f2013-04-17 20:17:40 -04003652 sb->dma_handle);
3653 kfree(sb);
3654 phba->total_scsi_bufs--;
3655 }
3656 spin_unlock(&phba->scsi_buf_list_get_lock);
James Smart2e0fef82007-06-17 19:56:36 -05003657 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04003658}
James Smart0794d602019-01-28 11:14:19 -08003659
James Smart8a9d2e82012-05-09 21:16:12 -04003660/**
James Smart5e5b5112019-01-28 11:14:22 -08003661 * lpfc_io_free - Free all the IO buffers and IOCBs from driver lists
James Smart895427b2017-02-12 13:52:30 -08003662 * @phba: pointer to lpfc hba data structure.
3663 *
James Smart0794d602019-01-28 11:14:19 -08003664 * This routine is to free all the IO buffers and IOCBs from the driver
James Smart895427b2017-02-12 13:52:30 -08003665 * list back to kernel. It is called from lpfc_pci_remove_one to free
3666 * the internal resources before the device is removed from the system.
3667 **/
James Smartc4908502019-01-28 11:14:28 -08003668void
James Smart5e5b5112019-01-28 11:14:22 -08003669lpfc_io_free(struct lpfc_hba *phba)
James Smart895427b2017-02-12 13:52:30 -08003670{
James Smartc4908502019-01-28 11:14:28 -08003671 struct lpfc_io_buf *lpfc_ncmd, *lpfc_ncmd_next;
James Smart5e5b5112019-01-28 11:14:22 -08003672 struct lpfc_sli4_hdw_queue *qp;
3673 int idx;
James Smart895427b2017-02-12 13:52:30 -08003674
James Smart5e5b5112019-01-28 11:14:22 -08003675 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
3676 qp = &phba->sli4_hba.hdwq[idx];
3677 /* Release all the lpfc_nvme_bufs maintained by this host. */
3678 spin_lock(&qp->io_buf_list_put_lock);
3679 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
3680 &qp->lpfc_io_buf_list_put,
3681 list) {
3682 list_del(&lpfc_ncmd->list);
3683 qp->put_io_bufs--;
3684 dma_pool_free(phba->lpfc_sg_dma_buf_pool,
3685 lpfc_ncmd->data, lpfc_ncmd->dma_handle);
James Smartd79c9e92019-08-14 16:57:09 -07003686 if (phba->cfg_xpsgl && !phba->nvmet_support)
3687 lpfc_put_sgl_per_hdwq(phba, lpfc_ncmd);
3688 lpfc_put_cmd_rsp_buf_per_hdwq(phba, lpfc_ncmd);
James Smart5e5b5112019-01-28 11:14:22 -08003689 kfree(lpfc_ncmd);
3690 qp->total_io_bufs--;
3691 }
3692 spin_unlock(&qp->io_buf_list_put_lock);
James Smart895427b2017-02-12 13:52:30 -08003693
James Smart5e5b5112019-01-28 11:14:22 -08003694 spin_lock(&qp->io_buf_list_get_lock);
3695 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
3696 &qp->lpfc_io_buf_list_get,
3697 list) {
3698 list_del(&lpfc_ncmd->list);
3699 qp->get_io_bufs--;
3700 dma_pool_free(phba->lpfc_sg_dma_buf_pool,
3701 lpfc_ncmd->data, lpfc_ncmd->dma_handle);
James Smartd79c9e92019-08-14 16:57:09 -07003702 if (phba->cfg_xpsgl && !phba->nvmet_support)
3703 lpfc_put_sgl_per_hdwq(phba, lpfc_ncmd);
3704 lpfc_put_cmd_rsp_buf_per_hdwq(phba, lpfc_ncmd);
James Smart5e5b5112019-01-28 11:14:22 -08003705 kfree(lpfc_ncmd);
3706 qp->total_io_bufs--;
3707 }
3708 spin_unlock(&qp->io_buf_list_get_lock);
James Smart895427b2017-02-12 13:52:30 -08003709 }
James Smart895427b2017-02-12 13:52:30 -08003710}
James Smart0794d602019-01-28 11:14:19 -08003711
James Smart895427b2017-02-12 13:52:30 -08003712/**
3713 * lpfc_sli4_els_sgl_update - update ELS xri-sgl sizing and mapping
James Smart8a9d2e82012-05-09 21:16:12 -04003714 * @phba: pointer to lpfc hba data structure.
3715 *
3716 * This routine first calculates the sizes of the current els and allocated
3717 * scsi sgl lists, and then goes through all sgls to updates the physical
3718 * XRIs assigned due to port function reset. During port initialization, the
3719 * current els and allocated scsi sgl lists are 0s.
3720 *
3721 * Return codes
3722 * 0 - successful (for now, it always returns 0)
3723 **/
3724int
James Smart895427b2017-02-12 13:52:30 -08003725lpfc_sli4_els_sgl_update(struct lpfc_hba *phba)
James Smart8a9d2e82012-05-09 21:16:12 -04003726{
3727 struct lpfc_sglq *sglq_entry = NULL, *sglq_entry_next = NULL;
James Smart895427b2017-02-12 13:52:30 -08003728 uint16_t i, lxri, xri_cnt, els_xri_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04003729 LIST_HEAD(els_sgl_list);
James Smart8a9d2e82012-05-09 21:16:12 -04003730 int rc;
3731
3732 /*
3733 * update on pci function's els xri-sgl list
3734 */
3735 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
James Smart895427b2017-02-12 13:52:30 -08003736
James Smart8a9d2e82012-05-09 21:16:12 -04003737 if (els_xri_cnt > phba->sli4_hba.els_xri_cnt) {
3738 /* els xri-sgl expanded */
3739 xri_cnt = els_xri_cnt - phba->sli4_hba.els_xri_cnt;
3740 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3741 "3157 ELS xri-sgl count increased from "
3742 "%d to %d\n", phba->sli4_hba.els_xri_cnt,
3743 els_xri_cnt);
3744 /* allocate the additional els sgls */
3745 for (i = 0; i < xri_cnt; i++) {
3746 sglq_entry = kzalloc(sizeof(struct lpfc_sglq),
3747 GFP_KERNEL);
3748 if (sglq_entry == NULL) {
Dick Kennedy372c1872020-06-30 14:50:00 -07003749 lpfc_printf_log(phba, KERN_ERR,
3750 LOG_TRACE_EVENT,
James Smart8a9d2e82012-05-09 21:16:12 -04003751 "2562 Failure to allocate an "
3752 "ELS sgl entry:%d\n", i);
3753 rc = -ENOMEM;
3754 goto out_free_mem;
3755 }
3756 sglq_entry->buff_type = GEN_BUFF_TYPE;
3757 sglq_entry->virt = lpfc_mbuf_alloc(phba, 0,
3758 &sglq_entry->phys);
3759 if (sglq_entry->virt == NULL) {
3760 kfree(sglq_entry);
Dick Kennedy372c1872020-06-30 14:50:00 -07003761 lpfc_printf_log(phba, KERN_ERR,
3762 LOG_TRACE_EVENT,
James Smart8a9d2e82012-05-09 21:16:12 -04003763 "2563 Failure to allocate an "
3764 "ELS mbuf:%d\n", i);
3765 rc = -ENOMEM;
3766 goto out_free_mem;
3767 }
3768 sglq_entry->sgl = sglq_entry->virt;
3769 memset(sglq_entry->sgl, 0, LPFC_BPL_SIZE);
3770 sglq_entry->state = SGL_FREED;
3771 list_add_tail(&sglq_entry->list, &els_sgl_list);
3772 }
James Smart38c20672013-03-01 16:37:44 -05003773 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -08003774 spin_lock(&phba->sli4_hba.sgl_list_lock);
3775 list_splice_init(&els_sgl_list,
3776 &phba->sli4_hba.lpfc_els_sgl_list);
3777 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smart38c20672013-03-01 16:37:44 -05003778 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04003779 } else if (els_xri_cnt < phba->sli4_hba.els_xri_cnt) {
3780 /* els xri-sgl shrinked */
3781 xri_cnt = phba->sli4_hba.els_xri_cnt - els_xri_cnt;
3782 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3783 "3158 ELS xri-sgl count decreased from "
3784 "%d to %d\n", phba->sli4_hba.els_xri_cnt,
3785 els_xri_cnt);
3786 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -08003787 spin_lock(&phba->sli4_hba.sgl_list_lock);
3788 list_splice_init(&phba->sli4_hba.lpfc_els_sgl_list,
3789 &els_sgl_list);
James Smart8a9d2e82012-05-09 21:16:12 -04003790 /* release extra els sgls from list */
3791 for (i = 0; i < xri_cnt; i++) {
3792 list_remove_head(&els_sgl_list,
3793 sglq_entry, struct lpfc_sglq, list);
3794 if (sglq_entry) {
James Smart895427b2017-02-12 13:52:30 -08003795 __lpfc_mbuf_free(phba, sglq_entry->virt,
3796 sglq_entry->phys);
James Smart8a9d2e82012-05-09 21:16:12 -04003797 kfree(sglq_entry);
3798 }
3799 }
James Smart895427b2017-02-12 13:52:30 -08003800 list_splice_init(&els_sgl_list,
3801 &phba->sli4_hba.lpfc_els_sgl_list);
3802 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04003803 spin_unlock_irq(&phba->hbalock);
3804 } else
3805 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3806 "3163 ELS xri-sgl count unchanged: %d\n",
3807 els_xri_cnt);
3808 phba->sli4_hba.els_xri_cnt = els_xri_cnt;
3809
3810 /* update xris to els sgls on the list */
3811 sglq_entry = NULL;
3812 sglq_entry_next = NULL;
3813 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
James Smart895427b2017-02-12 13:52:30 -08003814 &phba->sli4_hba.lpfc_els_sgl_list, list) {
James Smart8a9d2e82012-05-09 21:16:12 -04003815 lxri = lpfc_sli4_next_xritag(phba);
3816 if (lxri == NO_XRI) {
Dick Kennedy372c1872020-06-30 14:50:00 -07003817 lpfc_printf_log(phba, KERN_ERR,
3818 LOG_TRACE_EVENT,
James Smart8a9d2e82012-05-09 21:16:12 -04003819 "2400 Failed to allocate xri for "
3820 "ELS sgl\n");
3821 rc = -ENOMEM;
3822 goto out_free_mem;
3823 }
3824 sglq_entry->sli4_lxritag = lxri;
3825 sglq_entry->sli4_xritag = phba->sli4_hba.xri_ids[lxri];
3826 }
James Smart895427b2017-02-12 13:52:30 -08003827 return 0;
3828
3829out_free_mem:
3830 lpfc_free_els_sgl_list(phba);
3831 return rc;
3832}
3833
3834/**
James Smartf358dd02017-02-12 13:52:34 -08003835 * lpfc_sli4_nvmet_sgl_update - update xri-sgl sizing and mapping
3836 * @phba: pointer to lpfc hba data structure.
3837 *
3838 * This routine first calculates the sizes of the current els and allocated
3839 * scsi sgl lists, and then goes through all sgls to updates the physical
3840 * XRIs assigned due to port function reset. During port initialization, the
3841 * current els and allocated scsi sgl lists are 0s.
3842 *
3843 * Return codes
3844 * 0 - successful (for now, it always returns 0)
3845 **/
3846int
3847lpfc_sli4_nvmet_sgl_update(struct lpfc_hba *phba)
3848{
3849 struct lpfc_sglq *sglq_entry = NULL, *sglq_entry_next = NULL;
3850 uint16_t i, lxri, xri_cnt, els_xri_cnt;
James Smart6c621a22017-05-15 15:20:45 -07003851 uint16_t nvmet_xri_cnt;
James Smartf358dd02017-02-12 13:52:34 -08003852 LIST_HEAD(nvmet_sgl_list);
3853 int rc;
3854
3855 /*
3856 * update on pci function's nvmet xri-sgl list
3857 */
3858 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
James Smart61f3d4b2017-05-15 15:20:41 -07003859
James Smart6c621a22017-05-15 15:20:45 -07003860 /* For NVMET, ALL remaining XRIs are dedicated for IO processing */
3861 nvmet_xri_cnt = phba->sli4_hba.max_cfg_param.max_xri - els_xri_cnt;
James Smartf358dd02017-02-12 13:52:34 -08003862 if (nvmet_xri_cnt > phba->sli4_hba.nvmet_xri_cnt) {
3863 /* els xri-sgl expanded */
3864 xri_cnt = nvmet_xri_cnt - phba->sli4_hba.nvmet_xri_cnt;
3865 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3866 "6302 NVMET xri-sgl cnt grew from %d to %d\n",
3867 phba->sli4_hba.nvmet_xri_cnt, nvmet_xri_cnt);
3868 /* allocate the additional nvmet sgls */
3869 for (i = 0; i < xri_cnt; i++) {
3870 sglq_entry = kzalloc(sizeof(struct lpfc_sglq),
3871 GFP_KERNEL);
3872 if (sglq_entry == NULL) {
Dick Kennedy372c1872020-06-30 14:50:00 -07003873 lpfc_printf_log(phba, KERN_ERR,
3874 LOG_TRACE_EVENT,
James Smartf358dd02017-02-12 13:52:34 -08003875 "6303 Failure to allocate an "
3876 "NVMET sgl entry:%d\n", i);
3877 rc = -ENOMEM;
3878 goto out_free_mem;
3879 }
3880 sglq_entry->buff_type = NVMET_BUFF_TYPE;
3881 sglq_entry->virt = lpfc_nvmet_buf_alloc(phba, 0,
3882 &sglq_entry->phys);
3883 if (sglq_entry->virt == NULL) {
3884 kfree(sglq_entry);
Dick Kennedy372c1872020-06-30 14:50:00 -07003885 lpfc_printf_log(phba, KERN_ERR,
3886 LOG_TRACE_EVENT,
James Smartf358dd02017-02-12 13:52:34 -08003887 "6304 Failure to allocate an "
3888 "NVMET buf:%d\n", i);
3889 rc = -ENOMEM;
3890 goto out_free_mem;
3891 }
3892 sglq_entry->sgl = sglq_entry->virt;
3893 memset(sglq_entry->sgl, 0,
3894 phba->cfg_sg_dma_buf_size);
3895 sglq_entry->state = SGL_FREED;
3896 list_add_tail(&sglq_entry->list, &nvmet_sgl_list);
3897 }
3898 spin_lock_irq(&phba->hbalock);
3899 spin_lock(&phba->sli4_hba.sgl_list_lock);
3900 list_splice_init(&nvmet_sgl_list,
3901 &phba->sli4_hba.lpfc_nvmet_sgl_list);
3902 spin_unlock(&phba->sli4_hba.sgl_list_lock);
3903 spin_unlock_irq(&phba->hbalock);
3904 } else if (nvmet_xri_cnt < phba->sli4_hba.nvmet_xri_cnt) {
3905 /* nvmet xri-sgl shrunk */
3906 xri_cnt = phba->sli4_hba.nvmet_xri_cnt - nvmet_xri_cnt;
3907 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3908 "6305 NVMET xri-sgl count decreased from "
3909 "%d to %d\n", phba->sli4_hba.nvmet_xri_cnt,
3910 nvmet_xri_cnt);
3911 spin_lock_irq(&phba->hbalock);
3912 spin_lock(&phba->sli4_hba.sgl_list_lock);
3913 list_splice_init(&phba->sli4_hba.lpfc_nvmet_sgl_list,
3914 &nvmet_sgl_list);
3915 /* release extra nvmet sgls from list */
3916 for (i = 0; i < xri_cnt; i++) {
3917 list_remove_head(&nvmet_sgl_list,
3918 sglq_entry, struct lpfc_sglq, list);
3919 if (sglq_entry) {
3920 lpfc_nvmet_buf_free(phba, sglq_entry->virt,
3921 sglq_entry->phys);
3922 kfree(sglq_entry);
3923 }
3924 }
3925 list_splice_init(&nvmet_sgl_list,
3926 &phba->sli4_hba.lpfc_nvmet_sgl_list);
3927 spin_unlock(&phba->sli4_hba.sgl_list_lock);
3928 spin_unlock_irq(&phba->hbalock);
3929 } else
3930 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3931 "6306 NVMET xri-sgl count unchanged: %d\n",
3932 nvmet_xri_cnt);
3933 phba->sli4_hba.nvmet_xri_cnt = nvmet_xri_cnt;
3934
3935 /* update xris to nvmet sgls on the list */
3936 sglq_entry = NULL;
3937 sglq_entry_next = NULL;
3938 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
3939 &phba->sli4_hba.lpfc_nvmet_sgl_list, list) {
3940 lxri = lpfc_sli4_next_xritag(phba);
3941 if (lxri == NO_XRI) {
Dick Kennedy372c1872020-06-30 14:50:00 -07003942 lpfc_printf_log(phba, KERN_ERR,
3943 LOG_TRACE_EVENT,
James Smartf358dd02017-02-12 13:52:34 -08003944 "6307 Failed to allocate xri for "
3945 "NVMET sgl\n");
3946 rc = -ENOMEM;
3947 goto out_free_mem;
3948 }
3949 sglq_entry->sli4_lxritag = lxri;
3950 sglq_entry->sli4_xritag = phba->sli4_hba.xri_ids[lxri];
3951 }
3952 return 0;
3953
3954out_free_mem:
3955 lpfc_free_nvmet_sgl_list(phba);
3956 return rc;
3957}
3958
James Smart5e5b5112019-01-28 11:14:22 -08003959int
3960lpfc_io_buf_flush(struct lpfc_hba *phba, struct list_head *cbuf)
3961{
3962 LIST_HEAD(blist);
3963 struct lpfc_sli4_hdw_queue *qp;
James Smartc4908502019-01-28 11:14:28 -08003964 struct lpfc_io_buf *lpfc_cmd;
3965 struct lpfc_io_buf *iobufp, *prev_iobufp;
James Smart5e5b5112019-01-28 11:14:22 -08003966 int idx, cnt, xri, inserted;
3967
3968 cnt = 0;
3969 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
3970 qp = &phba->sli4_hba.hdwq[idx];
3971 spin_lock_irq(&qp->io_buf_list_get_lock);
3972 spin_lock(&qp->io_buf_list_put_lock);
3973
3974 /* Take everything off the get and put lists */
3975 list_splice_init(&qp->lpfc_io_buf_list_get, &blist);
3976 list_splice(&qp->lpfc_io_buf_list_put, &blist);
3977 INIT_LIST_HEAD(&qp->lpfc_io_buf_list_get);
3978 INIT_LIST_HEAD(&qp->lpfc_io_buf_list_put);
3979 cnt += qp->get_io_bufs + qp->put_io_bufs;
3980 qp->get_io_bufs = 0;
3981 qp->put_io_bufs = 0;
3982 qp->total_io_bufs = 0;
3983 spin_unlock(&qp->io_buf_list_put_lock);
3984 spin_unlock_irq(&qp->io_buf_list_get_lock);
3985 }
3986
3987 /*
3988 * Take IO buffers off blist and put on cbuf sorted by XRI.
3989 * This is because POST_SGL takes a sequential range of XRIs
3990 * to post to the firmware.
3991 */
3992 for (idx = 0; idx < cnt; idx++) {
James Smartc4908502019-01-28 11:14:28 -08003993 list_remove_head(&blist, lpfc_cmd, struct lpfc_io_buf, list);
James Smart5e5b5112019-01-28 11:14:22 -08003994 if (!lpfc_cmd)
3995 return cnt;
3996 if (idx == 0) {
3997 list_add_tail(&lpfc_cmd->list, cbuf);
3998 continue;
3999 }
4000 xri = lpfc_cmd->cur_iocbq.sli4_xritag;
4001 inserted = 0;
4002 prev_iobufp = NULL;
4003 list_for_each_entry(iobufp, cbuf, list) {
4004 if (xri < iobufp->cur_iocbq.sli4_xritag) {
4005 if (prev_iobufp)
4006 list_add(&lpfc_cmd->list,
4007 &prev_iobufp->list);
4008 else
4009 list_add(&lpfc_cmd->list, cbuf);
4010 inserted = 1;
4011 break;
4012 }
4013 prev_iobufp = iobufp;
4014 }
4015 if (!inserted)
4016 list_add_tail(&lpfc_cmd->list, cbuf);
4017 }
4018 return cnt;
4019}
4020
4021int
4022lpfc_io_buf_replenish(struct lpfc_hba *phba, struct list_head *cbuf)
4023{
4024 struct lpfc_sli4_hdw_queue *qp;
James Smartc4908502019-01-28 11:14:28 -08004025 struct lpfc_io_buf *lpfc_cmd;
James Smart5e5b5112019-01-28 11:14:22 -08004026 int idx, cnt;
4027
4028 qp = phba->sli4_hba.hdwq;
4029 cnt = 0;
4030 while (!list_empty(cbuf)) {
4031 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
4032 list_remove_head(cbuf, lpfc_cmd,
James Smartc4908502019-01-28 11:14:28 -08004033 struct lpfc_io_buf, list);
James Smart5e5b5112019-01-28 11:14:22 -08004034 if (!lpfc_cmd)
4035 return cnt;
4036 cnt++;
4037 qp = &phba->sli4_hba.hdwq[idx];
James Smart1fbf9742019-01-28 11:14:26 -08004038 lpfc_cmd->hdwq_no = idx;
4039 lpfc_cmd->hdwq = qp;
James Smart5e5b5112019-01-28 11:14:22 -08004040 lpfc_cmd->cur_iocbq.wqe_cmpl = NULL;
4041 lpfc_cmd->cur_iocbq.iocb_cmpl = NULL;
4042 spin_lock(&qp->io_buf_list_put_lock);
4043 list_add_tail(&lpfc_cmd->list,
4044 &qp->lpfc_io_buf_list_put);
4045 qp->put_io_bufs++;
4046 qp->total_io_bufs++;
4047 spin_unlock(&qp->io_buf_list_put_lock);
4048 }
4049 }
4050 return cnt;
4051}
4052
James Smartf358dd02017-02-12 13:52:34 -08004053/**
James Smart5e5b5112019-01-28 11:14:22 -08004054 * lpfc_sli4_io_sgl_update - update xri-sgl sizing and mapping
James Smart895427b2017-02-12 13:52:30 -08004055 * @phba: pointer to lpfc hba data structure.
4056 *
4057 * This routine first calculates the sizes of the current els and allocated
4058 * scsi sgl lists, and then goes through all sgls to updates the physical
4059 * XRIs assigned due to port function reset. During port initialization, the
4060 * current els and allocated scsi sgl lists are 0s.
4061 *
4062 * Return codes
4063 * 0 - successful (for now, it always returns 0)
4064 **/
4065int
James Smart5e5b5112019-01-28 11:14:22 -08004066lpfc_sli4_io_sgl_update(struct lpfc_hba *phba)
James Smart895427b2017-02-12 13:52:30 -08004067{
James Smartc4908502019-01-28 11:14:28 -08004068 struct lpfc_io_buf *lpfc_ncmd = NULL, *lpfc_ncmd_next = NULL;
James Smart0794d602019-01-28 11:14:19 -08004069 uint16_t i, lxri, els_xri_cnt;
James Smart5e5b5112019-01-28 11:14:22 -08004070 uint16_t io_xri_cnt, io_xri_max;
4071 LIST_HEAD(io_sgl_list);
James Smart0794d602019-01-28 11:14:19 -08004072 int rc, cnt;
James Smart895427b2017-02-12 13:52:30 -08004073
4074 /*
James Smart0794d602019-01-28 11:14:19 -08004075 * update on pci function's allocated nvme xri-sgl list
James Smart895427b2017-02-12 13:52:30 -08004076 */
James Smart0794d602019-01-28 11:14:19 -08004077
4078 /* maximum number of xris available for nvme buffers */
James Smart895427b2017-02-12 13:52:30 -08004079 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
James Smart5e5b5112019-01-28 11:14:22 -08004080 io_xri_max = phba->sli4_hba.max_cfg_param.max_xri - els_xri_cnt;
4081 phba->sli4_hba.io_xri_max = io_xri_max;
James Smart8a9d2e82012-05-09 21:16:12 -04004082
James Smarte8c0a772017-04-21 16:04:49 -07004083 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart0794d602019-01-28 11:14:19 -08004084 "6074 Current allocated XRI sgl count:%d, "
4085 "maximum XRI count:%d\n",
James Smart5e5b5112019-01-28 11:14:22 -08004086 phba->sli4_hba.io_xri_cnt,
4087 phba->sli4_hba.io_xri_max);
James Smarte8c0a772017-04-21 16:04:49 -07004088
James Smart5e5b5112019-01-28 11:14:22 -08004089 cnt = lpfc_io_buf_flush(phba, &io_sgl_list);
James Smart0794d602019-01-28 11:14:19 -08004090
James Smart5e5b5112019-01-28 11:14:22 -08004091 if (phba->sli4_hba.io_xri_cnt > phba->sli4_hba.io_xri_max) {
James Smart0794d602019-01-28 11:14:19 -08004092 /* max nvme xri shrunk below the allocated nvme buffers */
James Smart5e5b5112019-01-28 11:14:22 -08004093 io_xri_cnt = phba->sli4_hba.io_xri_cnt -
4094 phba->sli4_hba.io_xri_max;
James Smart0794d602019-01-28 11:14:19 -08004095 /* release the extra allocated nvme buffers */
James Smart5e5b5112019-01-28 11:14:22 -08004096 for (i = 0; i < io_xri_cnt; i++) {
4097 list_remove_head(&io_sgl_list, lpfc_ncmd,
James Smartc4908502019-01-28 11:14:28 -08004098 struct lpfc_io_buf, list);
James Smart0794d602019-01-28 11:14:19 -08004099 if (lpfc_ncmd) {
Romain Perier771db5c2017-07-06 10:13:05 +02004100 dma_pool_free(phba->lpfc_sg_dma_buf_pool,
James Smart0794d602019-01-28 11:14:19 -08004101 lpfc_ncmd->data,
4102 lpfc_ncmd->dma_handle);
4103 kfree(lpfc_ncmd);
James Smarta2fc4aef2014-09-03 12:57:55 -04004104 }
James Smart8a9d2e82012-05-09 21:16:12 -04004105 }
James Smart5e5b5112019-01-28 11:14:22 -08004106 phba->sli4_hba.io_xri_cnt -= io_xri_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04004107 }
4108
James Smart0794d602019-01-28 11:14:19 -08004109 /* update xris associated to remaining allocated nvme buffers */
4110 lpfc_ncmd = NULL;
4111 lpfc_ncmd_next = NULL;
James Smart5e5b5112019-01-28 11:14:22 -08004112 phba->sli4_hba.io_xri_cnt = cnt;
James Smart0794d602019-01-28 11:14:19 -08004113 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
James Smart5e5b5112019-01-28 11:14:22 -08004114 &io_sgl_list, list) {
James Smart8a9d2e82012-05-09 21:16:12 -04004115 lxri = lpfc_sli4_next_xritag(phba);
4116 if (lxri == NO_XRI) {
Dick Kennedy372c1872020-06-30 14:50:00 -07004117 lpfc_printf_log(phba, KERN_ERR,
4118 LOG_TRACE_EVENT,
James Smart0794d602019-01-28 11:14:19 -08004119 "6075 Failed to allocate xri for "
4120 "nvme buffer\n");
James Smart8a9d2e82012-05-09 21:16:12 -04004121 rc = -ENOMEM;
4122 goto out_free_mem;
4123 }
James Smart0794d602019-01-28 11:14:19 -08004124 lpfc_ncmd->cur_iocbq.sli4_lxritag = lxri;
4125 lpfc_ncmd->cur_iocbq.sli4_xritag = phba->sli4_hba.xri_ids[lxri];
James Smart8a9d2e82012-05-09 21:16:12 -04004126 }
James Smart5e5b5112019-01-28 11:14:22 -08004127 cnt = lpfc_io_buf_replenish(phba, &io_sgl_list);
dea31012005-04-17 16:05:31 -05004128 return 0;
James Smart8a9d2e82012-05-09 21:16:12 -04004129
4130out_free_mem:
James Smart5e5b5112019-01-28 11:14:22 -08004131 lpfc_io_free(phba);
James Smart8a9d2e82012-05-09 21:16:12 -04004132 return rc;
dea31012005-04-17 16:05:31 -05004133}
4134
James Smart0794d602019-01-28 11:14:19 -08004135/**
James Smart5e5b5112019-01-28 11:14:22 -08004136 * lpfc_new_io_buf - IO buffer allocator for HBA with SLI4 IF spec
Lee Jonesfe614ac2020-07-23 13:24:22 +01004137 * @phba: Pointer to lpfc hba data structure.
4138 * @num_to_alloc: The requested number of buffers to allocate.
James Smart0794d602019-01-28 11:14:19 -08004139 *
4140 * This routine allocates nvme buffers for device with SLI-4 interface spec,
4141 * the nvme buffer contains all the necessary information needed to initiate
4142 * an I/O. After allocating up to @num_to_allocate IO buffers and put
4143 * them on a list, it post them to the port by using SGL block post.
4144 *
4145 * Return codes:
James Smart5e5b5112019-01-28 11:14:22 -08004146 * int - number of IO buffers that were allocated and posted.
James Smart0794d602019-01-28 11:14:19 -08004147 * 0 = failure, less than num_to_alloc is a partial failure.
4148 **/
4149int
James Smart5e5b5112019-01-28 11:14:22 -08004150lpfc_new_io_buf(struct lpfc_hba *phba, int num_to_alloc)
James Smart0794d602019-01-28 11:14:19 -08004151{
James Smartc4908502019-01-28 11:14:28 -08004152 struct lpfc_io_buf *lpfc_ncmd;
James Smart0794d602019-01-28 11:14:19 -08004153 struct lpfc_iocbq *pwqeq;
4154 uint16_t iotag, lxri = 0;
4155 int bcnt, num_posted;
4156 LIST_HEAD(prep_nblist);
4157 LIST_HEAD(post_nblist);
4158 LIST_HEAD(nvme_nblist);
4159
James Smart5e5b5112019-01-28 11:14:22 -08004160 phba->sli4_hba.io_xri_cnt = 0;
James Smart0794d602019-01-28 11:14:19 -08004161 for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
James Smart7f9989b2019-08-27 14:27:46 -07004162 lpfc_ncmd = kzalloc(sizeof(*lpfc_ncmd), GFP_KERNEL);
James Smart0794d602019-01-28 11:14:19 -08004163 if (!lpfc_ncmd)
4164 break;
4165 /*
4166 * Get memory from the pci pool to map the virt space to
4167 * pci bus space for an I/O. The DMA buffer includes the
4168 * number of SGE's necessary to support the sg_tablesize.
4169 */
Thomas Meyera5c990e2019-05-29 22:21:36 +02004170 lpfc_ncmd->data = dma_pool_zalloc(phba->lpfc_sg_dma_buf_pool,
4171 GFP_KERNEL,
4172 &lpfc_ncmd->dma_handle);
James Smart0794d602019-01-28 11:14:19 -08004173 if (!lpfc_ncmd->data) {
4174 kfree(lpfc_ncmd);
4175 break;
4176 }
James Smart0794d602019-01-28 11:14:19 -08004177
James Smartd79c9e92019-08-14 16:57:09 -07004178 if (phba->cfg_xpsgl && !phba->nvmet_support) {
4179 INIT_LIST_HEAD(&lpfc_ncmd->dma_sgl_xtra_list);
4180 } else {
4181 /*
4182 * 4K Page alignment is CRITICAL to BlockGuard, double
4183 * check to be sure.
4184 */
4185 if ((phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
4186 (((unsigned long)(lpfc_ncmd->data) &
4187 (unsigned long)(SLI4_PAGE_SIZE - 1)) != 0)) {
Dick Kennedy372c1872020-06-30 14:50:00 -07004188 lpfc_printf_log(phba, KERN_ERR,
4189 LOG_TRACE_EVENT,
James Smartd79c9e92019-08-14 16:57:09 -07004190 "3369 Memory alignment err: "
4191 "addr=%lx\n",
4192 (unsigned long)lpfc_ncmd->data);
4193 dma_pool_free(phba->lpfc_sg_dma_buf_pool,
4194 lpfc_ncmd->data,
4195 lpfc_ncmd->dma_handle);
4196 kfree(lpfc_ncmd);
4197 break;
4198 }
James Smart0794d602019-01-28 11:14:19 -08004199 }
4200
James Smartd79c9e92019-08-14 16:57:09 -07004201 INIT_LIST_HEAD(&lpfc_ncmd->dma_cmd_rsp_list);
4202
James Smart0794d602019-01-28 11:14:19 -08004203 lxri = lpfc_sli4_next_xritag(phba);
4204 if (lxri == NO_XRI) {
4205 dma_pool_free(phba->lpfc_sg_dma_buf_pool,
4206 lpfc_ncmd->data, lpfc_ncmd->dma_handle);
4207 kfree(lpfc_ncmd);
4208 break;
4209 }
4210 pwqeq = &lpfc_ncmd->cur_iocbq;
4211
4212 /* Allocate iotag for lpfc_ncmd->cur_iocbq. */
4213 iotag = lpfc_sli_next_iotag(phba, pwqeq);
4214 if (iotag == 0) {
4215 dma_pool_free(phba->lpfc_sg_dma_buf_pool,
4216 lpfc_ncmd->data, lpfc_ncmd->dma_handle);
4217 kfree(lpfc_ncmd);
Dick Kennedy372c1872020-06-30 14:50:00 -07004218 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart0794d602019-01-28 11:14:19 -08004219 "6121 Failed to allocate IOTAG for"
4220 " XRI:0x%x\n", lxri);
4221 lpfc_sli4_free_xri(phba, lxri);
4222 break;
4223 }
4224 pwqeq->sli4_lxritag = lxri;
4225 pwqeq->sli4_xritag = phba->sli4_hba.xri_ids[lxri];
4226 pwqeq->context1 = lpfc_ncmd;
4227
4228 /* Initialize local short-hand pointers. */
4229 lpfc_ncmd->dma_sgl = lpfc_ncmd->data;
4230 lpfc_ncmd->dma_phys_sgl = lpfc_ncmd->dma_handle;
4231 lpfc_ncmd->cur_iocbq.context1 = lpfc_ncmd;
James Smartc2017262019-01-28 11:14:37 -08004232 spin_lock_init(&lpfc_ncmd->buf_lock);
James Smart0794d602019-01-28 11:14:19 -08004233
4234 /* add the nvme buffer to a post list */
4235 list_add_tail(&lpfc_ncmd->list, &post_nblist);
James Smart5e5b5112019-01-28 11:14:22 -08004236 phba->sli4_hba.io_xri_cnt++;
James Smart0794d602019-01-28 11:14:19 -08004237 }
4238 lpfc_printf_log(phba, KERN_INFO, LOG_NVME,
4239 "6114 Allocate %d out of %d requested new NVME "
4240 "buffers\n", bcnt, num_to_alloc);
4241
4242 /* post the list of nvme buffer sgls to port if available */
4243 if (!list_empty(&post_nblist))
James Smart5e5b5112019-01-28 11:14:22 -08004244 num_posted = lpfc_sli4_post_io_sgl_list(
James Smart0794d602019-01-28 11:14:19 -08004245 phba, &post_nblist, bcnt);
4246 else
4247 num_posted = 0;
4248
4249 return num_posted;
4250}
4251
James Smart96418b52017-03-04 09:30:31 -08004252static uint64_t
4253lpfc_get_wwpn(struct lpfc_hba *phba)
4254{
4255 uint64_t wwn;
4256 int rc;
4257 LPFC_MBOXQ_t *mboxq;
4258 MAILBOX_t *mb;
4259
James Smart96418b52017-03-04 09:30:31 -08004260 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
4261 GFP_KERNEL);
4262 if (!mboxq)
4263 return (uint64_t)-1;
4264
4265 /* First get WWN of HBA instance */
4266 lpfc_read_nv(phba, mboxq);
4267 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4268 if (rc != MBX_SUCCESS) {
Dick Kennedy372c1872020-06-30 14:50:00 -07004269 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart96418b52017-03-04 09:30:31 -08004270 "6019 Mailbox failed , mbxCmd x%x "
4271 "READ_NV, mbxStatus x%x\n",
4272 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
4273 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
4274 mempool_free(mboxq, phba->mbox_mem_pool);
4275 return (uint64_t) -1;
4276 }
4277 mb = &mboxq->u.mb;
4278 memcpy(&wwn, (char *)mb->un.varRDnvp.portname, sizeof(uint64_t));
4279 /* wwn is WWPN of HBA instance */
4280 mempool_free(mboxq, phba->mbox_mem_pool);
4281 if (phba->sli_rev == LPFC_SLI_REV4)
4282 return be64_to_cpu(wwn);
4283 else
Maurizio Lombardi286871a2017-08-23 16:55:48 -07004284 return rol64(wwn, 32);
James Smart96418b52017-03-04 09:30:31 -08004285}
4286
James Smarte59058c2008-08-24 21:49:00 -04004287/**
James Smart3621a712009-04-06 18:47:14 -04004288 * lpfc_create_port - Create an FC port
James Smarte59058c2008-08-24 21:49:00 -04004289 * @phba: pointer to lpfc hba data structure.
4290 * @instance: a unique integer ID to this FC port.
4291 * @dev: pointer to the device data structure.
4292 *
4293 * This routine creates a FC port for the upper layer protocol. The FC port
4294 * can be created on top of either a physical port or a virtual port provided
4295 * by the HBA. This routine also allocates a SCSI host data structure (shost)
4296 * and associates the FC port created before adding the shost into the SCSI
4297 * layer.
4298 *
4299 * Return codes
4300 * @vport - pointer to the virtual N_Port data structure.
4301 * NULL - port create failed.
4302 **/
James Smart2e0fef82007-06-17 19:56:36 -05004303struct lpfc_vport *
James Smart3de2a652007-08-02 11:09:59 -04004304lpfc_create_port(struct lpfc_hba *phba, int instance, struct device *dev)
James Smart47a86172007-04-25 09:53:22 -04004305{
James Smart2e0fef82007-06-17 19:56:36 -05004306 struct lpfc_vport *vport;
James Smart895427b2017-02-12 13:52:30 -08004307 struct Scsi_Host *shost = NULL;
James Smartc90b4482020-03-22 11:12:56 -07004308 struct scsi_host_template *template;
James Smart2e0fef82007-06-17 19:56:36 -05004309 int error = 0;
James Smart96418b52017-03-04 09:30:31 -08004310 int i;
4311 uint64_t wwn;
4312 bool use_no_reset_hba = false;
James Smart56bc8022017-06-15 22:56:43 -07004313 int rc;
James Smart96418b52017-03-04 09:30:31 -08004314
James Smart56bc8022017-06-15 22:56:43 -07004315 if (lpfc_no_hba_reset_cnt) {
4316 if (phba->sli_rev < LPFC_SLI_REV4 &&
4317 dev == &phba->pcidev->dev) {
4318 /* Reset the port first */
4319 lpfc_sli_brdrestart(phba);
4320 rc = lpfc_sli_chipset_init(phba);
4321 if (rc)
4322 return NULL;
4323 }
4324 wwn = lpfc_get_wwpn(phba);
4325 }
James Smart96418b52017-03-04 09:30:31 -08004326
4327 for (i = 0; i < lpfc_no_hba_reset_cnt; i++) {
4328 if (wwn == lpfc_no_hba_reset[i]) {
Dick Kennedy372c1872020-06-30 14:50:00 -07004329 lpfc_printf_log(phba, KERN_ERR,
4330 LOG_TRACE_EVENT,
James Smart96418b52017-03-04 09:30:31 -08004331 "6020 Setting use_no_reset port=%llx\n",
4332 wwn);
4333 use_no_reset_hba = true;
4334 break;
4335 }
4336 }
James Smart47a86172007-04-25 09:53:22 -04004337
James Smartc90b4482020-03-22 11:12:56 -07004338 /* Seed template for SCSI host registration */
4339 if (dev == &phba->pcidev->dev) {
4340 template = &phba->port_template;
4341
4342 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) {
4343 /* Seed physical port template */
4344 memcpy(template, &lpfc_template, sizeof(*template));
4345
4346 if (use_no_reset_hba) {
4347 /* template is for a no reset SCSI Host */
4348 template->max_sectors = 0xffff;
4349 template->eh_host_reset_handler = NULL;
4350 }
4351
4352 /* Template for all vports this physical port creates */
4353 memcpy(&phba->vport_template, &lpfc_template,
4354 sizeof(*template));
4355 phba->vport_template.max_sectors = 0xffff;
4356 phba->vport_template.shost_attrs = lpfc_vport_attrs;
4357 phba->vport_template.eh_bus_reset_handler = NULL;
4358 phba->vport_template.eh_host_reset_handler = NULL;
4359 phba->vport_template.vendor_id = 0;
4360
4361 /* Initialize the host templates with updated value */
4362 if (phba->sli_rev == LPFC_SLI_REV4) {
4363 template->sg_tablesize = phba->cfg_scsi_seg_cnt;
4364 phba->vport_template.sg_tablesize =
4365 phba->cfg_scsi_seg_cnt;
4366 } else {
4367 template->sg_tablesize = phba->cfg_sg_seg_cnt;
4368 phba->vport_template.sg_tablesize =
4369 phba->cfg_sg_seg_cnt;
4370 }
4371
James Smart895427b2017-02-12 13:52:30 -08004372 } else {
James Smartc90b4482020-03-22 11:12:56 -07004373 /* NVMET is for physical port only */
4374 memcpy(template, &lpfc_template_nvme,
4375 sizeof(*template));
James Smart895427b2017-02-12 13:52:30 -08004376 }
James Smartc90b4482020-03-22 11:12:56 -07004377 } else {
4378 template = &phba->vport_template;
James Smartea4142f2015-04-07 15:07:13 -04004379 }
James Smartc90b4482020-03-22 11:12:56 -07004380
4381 shost = scsi_host_alloc(template, sizeof(struct lpfc_vport));
James Smart2e0fef82007-06-17 19:56:36 -05004382 if (!shost)
4383 goto out;
James Smart47a86172007-04-25 09:53:22 -04004384
James Smart2e0fef82007-06-17 19:56:36 -05004385 vport = (struct lpfc_vport *) shost->hostdata;
4386 vport->phba = phba;
James Smart2e0fef82007-06-17 19:56:36 -05004387 vport->load_flag |= FC_LOADING;
James Smart92d7f7b2007-06-17 19:56:38 -05004388 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart7f5f3d02008-02-08 18:50:14 -05004389 vport->fc_rscn_flush = 0;
James Smart3de2a652007-08-02 11:09:59 -04004390 lpfc_get_vport_cfgparam(vport);
James Smart895427b2017-02-12 13:52:30 -08004391
James Smartf6e84792019-01-28 11:14:38 -08004392 /* Adjust value in vport */
4393 vport->cfg_enable_fc4_type = phba->cfg_enable_fc4_type;
4394
James Smart2e0fef82007-06-17 19:56:36 -05004395 shost->unique_id = instance;
4396 shost->max_id = LPFC_MAX_TARGET;
James Smart3de2a652007-08-02 11:09:59 -04004397 shost->max_lun = vport->cfg_max_luns;
James Smart2e0fef82007-06-17 19:56:36 -05004398 shost->this_id = -1;
4399 shost->max_cmd_len = 16;
James Smart6a828b02019-01-28 11:14:31 -08004400
James Smartda0436e2009-05-22 14:51:39 -04004401 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart77ffd342019-08-15 19:36:49 -07004402 if (!phba->cfg_fcp_mq_threshold ||
4403 phba->cfg_fcp_mq_threshold > phba->cfg_hdw_queue)
4404 phba->cfg_fcp_mq_threshold = phba->cfg_hdw_queue;
4405
4406 shost->nr_hw_queues = min_t(int, 2 * num_possible_nodes(),
4407 phba->cfg_fcp_mq_threshold);
James Smart6a828b02019-01-28 11:14:31 -08004408
James Smart28baac72010-02-12 14:42:03 -05004409 shost->dma_boundary =
James Smartcb5172e2010-03-15 11:25:07 -04004410 phba->sli4_hba.pc_sli4_params.sge_supp_len-1;
James Smartd79c9e92019-08-14 16:57:09 -07004411
4412 if (phba->cfg_xpsgl && !phba->nvmet_support)
4413 shost->sg_tablesize = LPFC_MAX_SG_TABLESIZE;
4414 else
4415 shost->sg_tablesize = phba->cfg_scsi_seg_cnt;
James Smartace44e42019-01-28 11:14:27 -08004416 } else
4417 /* SLI-3 has a limited number of hardware queues (3),
4418 * thus there is only one for FCP processing.
4419 */
4420 shost->nr_hw_queues = 1;
James Smart81301a92008-12-04 22:39:46 -05004421
James Smart47a86172007-04-25 09:53:22 -04004422 /*
James Smart2e0fef82007-06-17 19:56:36 -05004423 * Set initial can_queue value since 0 is no longer supported and
4424 * scsi_add_host will fail. This will be adjusted later based on the
4425 * max xri value determined in hba setup.
James Smart47a86172007-04-25 09:53:22 -04004426 */
James Smart2e0fef82007-06-17 19:56:36 -05004427 shost->can_queue = phba->cfg_hba_queue_depth - 10;
James Smart3de2a652007-08-02 11:09:59 -04004428 if (dev != &phba->pcidev->dev) {
James Smart92d7f7b2007-06-17 19:56:38 -05004429 shost->transportt = lpfc_vport_transport_template;
4430 vport->port_type = LPFC_NPIV_PORT;
4431 } else {
4432 shost->transportt = lpfc_transport_template;
4433 vport->port_type = LPFC_PHYSICAL_PORT;
4434 }
James Smart47a86172007-04-25 09:53:22 -04004435
James Smartc90b4482020-03-22 11:12:56 -07004436 lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_FCP,
4437 "9081 CreatePort TMPLATE type %x TBLsize %d "
4438 "SEGcnt %d/%d\n",
4439 vport->port_type, shost->sg_tablesize,
4440 phba->cfg_scsi_seg_cnt, phba->cfg_sg_seg_cnt);
4441
James Smart2e0fef82007-06-17 19:56:36 -05004442 /* Initialize all internally managed lists. */
4443 INIT_LIST_HEAD(&vport->fc_nodes);
James Smartda0436e2009-05-22 14:51:39 -04004444 INIT_LIST_HEAD(&vport->rcv_buffer_list);
James Smart2e0fef82007-06-17 19:56:36 -05004445 spin_lock_init(&vport->work_port_lock);
James Smart47a86172007-04-25 09:53:22 -04004446
Kees Cookf22eb4d2017-09-06 20:24:26 -07004447 timer_setup(&vport->fc_disctmo, lpfc_disc_timeout, 0);
James Smart47a86172007-04-25 09:53:22 -04004448
Kees Cookf22eb4d2017-09-06 20:24:26 -07004449 timer_setup(&vport->els_tmofunc, lpfc_els_timeout, 0);
James Smart92494142011-02-16 12:39:44 -05004450
Kees Cookf22eb4d2017-09-06 20:24:26 -07004451 timer_setup(&vport->delayed_disc_tmo, lpfc_delayed_disc_tmo, 0);
James Smart92494142011-02-16 12:39:44 -05004452
James Smartaa6ff302019-05-21 17:49:09 -07004453 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED)
4454 lpfc_setup_bg(phba, shost);
4455
James Bottomleyd139b9b2009-11-05 13:33:12 -06004456 error = scsi_add_host_with_dma(shost, dev, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05004457 if (error)
4458 goto out_put_shost;
James Smart47a86172007-04-25 09:53:22 -04004459
James Smart523128e2018-09-10 10:30:46 -07004460 spin_lock_irq(&phba->port_list_lock);
James Smart2e0fef82007-06-17 19:56:36 -05004461 list_add_tail(&vport->listentry, &phba->port_list);
James Smart523128e2018-09-10 10:30:46 -07004462 spin_unlock_irq(&phba->port_list_lock);
James Smart2e0fef82007-06-17 19:56:36 -05004463 return vport;
James Smart47a86172007-04-25 09:53:22 -04004464
James Smart2e0fef82007-06-17 19:56:36 -05004465out_put_shost:
4466 scsi_host_put(shost);
4467out:
4468 return NULL;
James Smart47a86172007-04-25 09:53:22 -04004469}
4470
James Smarte59058c2008-08-24 21:49:00 -04004471/**
James Smart3621a712009-04-06 18:47:14 -04004472 * destroy_port - destroy an FC port
James Smarte59058c2008-08-24 21:49:00 -04004473 * @vport: pointer to an lpfc virtual N_Port data structure.
4474 *
4475 * This routine destroys a FC port from the upper layer protocol. All the
4476 * resources associated with the port are released.
4477 **/
James Smart2e0fef82007-06-17 19:56:36 -05004478void
4479destroy_port(struct lpfc_vport *vport)
James Smart47a86172007-04-25 09:53:22 -04004480{
James Smart92d7f7b2007-06-17 19:56:38 -05004481 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4482 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04004483
James Smart858c9f62007-06-17 19:56:39 -05004484 lpfc_debugfs_terminate(vport);
James Smart92d7f7b2007-06-17 19:56:38 -05004485 fc_remove_host(shost);
4486 scsi_remove_host(shost);
James Smart47a86172007-04-25 09:53:22 -04004487
James Smart523128e2018-09-10 10:30:46 -07004488 spin_lock_irq(&phba->port_list_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05004489 list_del_init(&vport->listentry);
James Smart523128e2018-09-10 10:30:46 -07004490 spin_unlock_irq(&phba->port_list_lock);
James Smart47a86172007-04-25 09:53:22 -04004491
James Smart92d7f7b2007-06-17 19:56:38 -05004492 lpfc_cleanup(vport);
James Smart47a86172007-04-25 09:53:22 -04004493 return;
James Smart47a86172007-04-25 09:53:22 -04004494}
4495
James Smarte59058c2008-08-24 21:49:00 -04004496/**
James Smart3621a712009-04-06 18:47:14 -04004497 * lpfc_get_instance - Get a unique integer ID
James Smarte59058c2008-08-24 21:49:00 -04004498 *
4499 * This routine allocates a unique integer ID from lpfc_hba_index pool. It
4500 * uses the kernel idr facility to perform the task.
4501 *
4502 * Return codes:
4503 * instance - a unique integer ID allocated as the new instance.
4504 * -1 - lpfc get instance failed.
4505 **/
James Smart92d7f7b2007-06-17 19:56:38 -05004506int
4507lpfc_get_instance(void)
4508{
Tejun Heoab516032013-02-27 17:04:44 -08004509 int ret;
James Smart92d7f7b2007-06-17 19:56:38 -05004510
Tejun Heoab516032013-02-27 17:04:44 -08004511 ret = idr_alloc(&lpfc_hba_index, NULL, 0, 0, GFP_KERNEL);
4512 return ret < 0 ? -1 : ret;
James Smart92d7f7b2007-06-17 19:56:38 -05004513}
4514
James Smarte59058c2008-08-24 21:49:00 -04004515/**
James Smart3621a712009-04-06 18:47:14 -04004516 * lpfc_scan_finished - method for SCSI layer to detect whether scan is done
James Smarte59058c2008-08-24 21:49:00 -04004517 * @shost: pointer to SCSI host data structure.
4518 * @time: elapsed time of the scan in jiffies.
4519 *
4520 * This routine is called by the SCSI layer with a SCSI host to determine
4521 * whether the scan host is finished.
4522 *
4523 * Note: there is no scan_start function as adapter initialization will have
4524 * asynchronously kicked off the link initialization.
4525 *
4526 * Return codes
4527 * 0 - SCSI host scan is not over yet.
4528 * 1 - SCSI host scan is over.
4529 **/
James Smart47a86172007-04-25 09:53:22 -04004530int lpfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
4531{
James Smart2e0fef82007-06-17 19:56:36 -05004532 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4533 struct lpfc_hba *phba = vport->phba;
James Smart858c9f62007-06-17 19:56:39 -05004534 int stat = 0;
James Smart47a86172007-04-25 09:53:22 -04004535
James Smart858c9f62007-06-17 19:56:39 -05004536 spin_lock_irq(shost->host_lock);
4537
James Smart51ef4c22007-08-02 11:10:31 -04004538 if (vport->load_flag & FC_UNLOADING) {
James Smart858c9f62007-06-17 19:56:39 -05004539 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04004540 goto finished;
James Smart858c9f62007-06-17 19:56:39 -05004541 }
James Smart256ec0d2013-04-17 20:14:58 -04004542 if (time >= msecs_to_jiffies(30 * 1000)) {
James Smart2e0fef82007-06-17 19:56:36 -05004543 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004544 "0461 Scanning longer than 30 "
4545 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05004546 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04004547 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05004548 }
James Smart256ec0d2013-04-17 20:14:58 -04004549 if (time >= msecs_to_jiffies(15 * 1000) &&
4550 phba->link_state <= LPFC_LINK_DOWN) {
James Smart2e0fef82007-06-17 19:56:36 -05004551 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004552 "0465 Link down longer than 15 "
4553 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05004554 stat = 1;
James Smart2e0fef82007-06-17 19:56:36 -05004555 goto finished;
James Smart47a86172007-04-25 09:53:22 -04004556 }
4557
James Smart2e0fef82007-06-17 19:56:36 -05004558 if (vport->port_state != LPFC_VPORT_READY)
James Smart858c9f62007-06-17 19:56:39 -05004559 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05004560 if (vport->num_disc_nodes || vport->fc_prli_sent)
James Smart858c9f62007-06-17 19:56:39 -05004561 goto finished;
James Smart256ec0d2013-04-17 20:14:58 -04004562 if (vport->fc_map_cnt == 0 && time < msecs_to_jiffies(2 * 1000))
James Smart858c9f62007-06-17 19:56:39 -05004563 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05004564 if ((phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) != 0)
James Smart858c9f62007-06-17 19:56:39 -05004565 goto finished;
4566
4567 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04004568
4569finished:
James Smart858c9f62007-06-17 19:56:39 -05004570 spin_unlock_irq(shost->host_lock);
4571 return stat;
James Smart92d7f7b2007-06-17 19:56:38 -05004572}
4573
Bart Van Assche3999df72019-03-28 11:06:16 -07004574static void lpfc_host_supported_speeds_set(struct Scsi_Host *shost)
James Smartcd713482018-10-23 13:41:01 -07004575{
4576 struct lpfc_vport *vport = (struct lpfc_vport *)shost->hostdata;
4577 struct lpfc_hba *phba = vport->phba;
4578
4579 fc_host_supported_speeds(shost) = 0;
James Smart1dc5ec22018-10-23 13:41:11 -07004580 if (phba->lmt & LMT_128Gb)
4581 fc_host_supported_speeds(shost) |= FC_PORTSPEED_128GBIT;
James Smartcd713482018-10-23 13:41:01 -07004582 if (phba->lmt & LMT_64Gb)
4583 fc_host_supported_speeds(shost) |= FC_PORTSPEED_64GBIT;
4584 if (phba->lmt & LMT_32Gb)
4585 fc_host_supported_speeds(shost) |= FC_PORTSPEED_32GBIT;
4586 if (phba->lmt & LMT_16Gb)
4587 fc_host_supported_speeds(shost) |= FC_PORTSPEED_16GBIT;
4588 if (phba->lmt & LMT_10Gb)
4589 fc_host_supported_speeds(shost) |= FC_PORTSPEED_10GBIT;
4590 if (phba->lmt & LMT_8Gb)
4591 fc_host_supported_speeds(shost) |= FC_PORTSPEED_8GBIT;
4592 if (phba->lmt & LMT_4Gb)
4593 fc_host_supported_speeds(shost) |= FC_PORTSPEED_4GBIT;
4594 if (phba->lmt & LMT_2Gb)
4595 fc_host_supported_speeds(shost) |= FC_PORTSPEED_2GBIT;
4596 if (phba->lmt & LMT_1Gb)
4597 fc_host_supported_speeds(shost) |= FC_PORTSPEED_1GBIT;
4598}
4599
James Smarte59058c2008-08-24 21:49:00 -04004600/**
James Smart3621a712009-04-06 18:47:14 -04004601 * lpfc_host_attrib_init - Initialize SCSI host attributes on a FC port
James Smarte59058c2008-08-24 21:49:00 -04004602 * @shost: pointer to SCSI host data structure.
4603 *
4604 * This routine initializes a given SCSI host attributes on a FC port. The
4605 * SCSI host can be either on top of a physical port or a virtual port.
4606 **/
James Smart92d7f7b2007-06-17 19:56:38 -05004607void lpfc_host_attrib_init(struct Scsi_Host *shost)
4608{
4609 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4610 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04004611 /*
James Smart2e0fef82007-06-17 19:56:36 -05004612 * Set fixed host attributes. Must done after lpfc_sli_hba_setup().
James Smart47a86172007-04-25 09:53:22 -04004613 */
4614
James Smart2e0fef82007-06-17 19:56:36 -05004615 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
4616 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
James Smart47a86172007-04-25 09:53:22 -04004617 fc_host_supported_classes(shost) = FC_COS_CLASS3;
4618
4619 memset(fc_host_supported_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05004620 sizeof(fc_host_supported_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04004621 fc_host_supported_fc4s(shost)[2] = 1;
4622 fc_host_supported_fc4s(shost)[7] = 1;
4623
James Smart92d7f7b2007-06-17 19:56:38 -05004624 lpfc_vport_symbolic_node_name(vport, fc_host_symbolic_name(shost),
4625 sizeof fc_host_symbolic_name(shost));
James Smart47a86172007-04-25 09:53:22 -04004626
James Smartcd713482018-10-23 13:41:01 -07004627 lpfc_host_supported_speeds_set(shost);
James Smart47a86172007-04-25 09:53:22 -04004628
4629 fc_host_maxframe_size(shost) =
James Smart2e0fef82007-06-17 19:56:36 -05004630 (((uint32_t) vport->fc_sparam.cmn.bbRcvSizeMsb & 0x0F) << 8) |
4631 (uint32_t) vport->fc_sparam.cmn.bbRcvSizeLsb;
James Smart47a86172007-04-25 09:53:22 -04004632
Mike Christie0af5d702010-09-15 16:52:31 -05004633 fc_host_dev_loss_tmo(shost) = vport->cfg_devloss_tmo;
4634
James Smart47a86172007-04-25 09:53:22 -04004635 /* This value is also unchanging */
4636 memset(fc_host_active_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05004637 sizeof(fc_host_active_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04004638 fc_host_active_fc4s(shost)[2] = 1;
4639 fc_host_active_fc4s(shost)[7] = 1;
4640
James Smart92d7f7b2007-06-17 19:56:38 -05004641 fc_host_max_npiv_vports(shost) = phba->max_vpi;
James Smart47a86172007-04-25 09:53:22 -04004642 spin_lock_irq(shost->host_lock);
James Smart51ef4c22007-08-02 11:10:31 -04004643 vport->load_flag &= ~FC_LOADING;
James Smart47a86172007-04-25 09:53:22 -04004644 spin_unlock_irq(shost->host_lock);
James Smart47a86172007-04-25 09:53:22 -04004645}
dea31012005-04-17 16:05:31 -05004646
James Smarte59058c2008-08-24 21:49:00 -04004647/**
James Smartda0436e2009-05-22 14:51:39 -04004648 * lpfc_stop_port_s3 - Stop SLI3 device port
James Smarte59058c2008-08-24 21:49:00 -04004649 * @phba: pointer to lpfc hba data structure.
4650 *
James Smartda0436e2009-05-22 14:51:39 -04004651 * This routine is invoked to stop an SLI3 device port, it stops the device
4652 * from generating interrupts and stops the device driver's timers for the
4653 * device.
James Smarte59058c2008-08-24 21:49:00 -04004654 **/
James Smartdb2378e2008-02-08 18:49:51 -05004655static void
James Smartda0436e2009-05-22 14:51:39 -04004656lpfc_stop_port_s3(struct lpfc_hba *phba)
James Smartdb2378e2008-02-08 18:49:51 -05004657{
James Smartda0436e2009-05-22 14:51:39 -04004658 /* Clear all interrupt enable conditions */
4659 writel(0, phba->HCregaddr);
4660 readl(phba->HCregaddr); /* flush */
4661 /* Clear all pending interrupts */
4662 writel(0xffffffff, phba->HAregaddr);
4663 readl(phba->HAregaddr); /* flush */
James Smart93996272008-08-24 21:50:30 -04004664
James Smartda0436e2009-05-22 14:51:39 -04004665 /* Reset some HBA SLI setup states */
4666 lpfc_stop_hba_timers(phba);
4667 phba->pport->work_port_events = 0;
James Smartdb2378e2008-02-08 18:49:51 -05004668}
4669
James Smarte59058c2008-08-24 21:49:00 -04004670/**
James Smartda0436e2009-05-22 14:51:39 -04004671 * lpfc_stop_port_s4 - Stop SLI4 device port
James Smart5b75da22008-12-04 22:39:35 -05004672 * @phba: pointer to lpfc hba data structure.
4673 *
James Smartda0436e2009-05-22 14:51:39 -04004674 * This routine is invoked to stop an SLI4 device port, it stops the device
4675 * from generating interrupts and stops the device driver's timers for the
4676 * device.
4677 **/
4678static void
4679lpfc_stop_port_s4(struct lpfc_hba *phba)
4680{
4681 /* Reset some HBA SLI4 setup states */
4682 lpfc_stop_hba_timers(phba);
James Smartcdb42be2019-01-28 11:14:21 -08004683 if (phba->pport)
4684 phba->pport->work_port_events = 0;
James Smartda0436e2009-05-22 14:51:39 -04004685 phba->sli4_hba.intr_enable = 0;
James Smartda0436e2009-05-22 14:51:39 -04004686}
4687
4688/**
4689 * lpfc_stop_port - Wrapper function for stopping hba port
4690 * @phba: Pointer to HBA context object.
James Smart5b75da22008-12-04 22:39:35 -05004691 *
James Smartda0436e2009-05-22 14:51:39 -04004692 * This routine wraps the actual SLI3 or SLI4 hba stop port routine from
4693 * the API jump table function pointer from the lpfc_hba struct.
4694 **/
4695void
4696lpfc_stop_port(struct lpfc_hba *phba)
4697{
4698 phba->lpfc_stop_port(phba);
Dick Kennedyf485c182017-09-29 17:34:34 -07004699
4700 if (phba->wq)
4701 flush_workqueue(phba->wq);
James Smartda0436e2009-05-22 14:51:39 -04004702}
4703
4704/**
James Smartecfd03c2010-02-12 14:41:27 -05004705 * lpfc_fcf_redisc_wait_start_timer - Start fcf rediscover wait timer
4706 * @phba: Pointer to hba for which this call is being executed.
4707 *
4708 * This routine starts the timer waiting for the FCF rediscovery to complete.
4709 **/
4710void
4711lpfc_fcf_redisc_wait_start_timer(struct lpfc_hba *phba)
4712{
4713 unsigned long fcf_redisc_wait_tmo =
4714 (jiffies + msecs_to_jiffies(LPFC_FCF_REDISCOVER_WAIT_TMO));
4715 /* Start fcf rediscovery wait period timer */
4716 mod_timer(&phba->fcf.redisc_wait, fcf_redisc_wait_tmo);
4717 spin_lock_irq(&phba->hbalock);
4718 /* Allow action to new fcf asynchronous event */
4719 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
4720 /* Mark the FCF rediscovery pending state */
4721 phba->fcf.fcf_flag |= FCF_REDISC_PEND;
4722 spin_unlock_irq(&phba->hbalock);
4723}
4724
4725/**
4726 * lpfc_sli4_fcf_redisc_wait_tmo - FCF table rediscover wait timeout
Lee Jonesfe614ac2020-07-23 13:24:22 +01004727 * @t: Timer context used to obtain the pointer to lpfc hba data structure.
James Smartecfd03c2010-02-12 14:41:27 -05004728 *
4729 * This routine is invoked when waiting for FCF table rediscover has been
4730 * timed out. If new FCF record(s) has (have) been discovered during the
4731 * wait period, a new FCF event shall be added to the FCOE async event
4732 * list, and then worker thread shall be waked up for processing from the
4733 * worker thread context.
4734 **/
Rashika Kheriae399b222014-09-03 12:55:28 -04004735static void
Kees Cookf22eb4d2017-09-06 20:24:26 -07004736lpfc_sli4_fcf_redisc_wait_tmo(struct timer_list *t)
James Smartecfd03c2010-02-12 14:41:27 -05004737{
Kees Cookf22eb4d2017-09-06 20:24:26 -07004738 struct lpfc_hba *phba = from_timer(phba, t, fcf.redisc_wait);
James Smartecfd03c2010-02-12 14:41:27 -05004739
4740 /* Don't send FCF rediscovery event if timer cancelled */
4741 spin_lock_irq(&phba->hbalock);
4742 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
4743 spin_unlock_irq(&phba->hbalock);
4744 return;
4745 }
4746 /* Clear FCF rediscovery timer pending flag */
4747 phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
4748 /* FCF rediscovery event to worker thread */
4749 phba->fcf.fcf_flag |= FCF_REDISC_EVT;
4750 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004751 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04004752 "2776 FCF rediscover quiescent timer expired\n");
James Smartecfd03c2010-02-12 14:41:27 -05004753 /* wake up worker thread */
4754 lpfc_worker_wake_up(phba);
4755}
4756
4757/**
James Smartda0436e2009-05-22 14:51:39 -04004758 * lpfc_sli4_parse_latt_fault - Parse sli4 link-attention link fault code
4759 * @phba: pointer to lpfc hba data structure.
4760 * @acqe_link: pointer to the async link completion queue entry.
4761 *
James Smart23288b72018-05-04 20:37:53 -07004762 * This routine is to parse the SLI4 link-attention link fault code.
James Smartda0436e2009-05-22 14:51:39 -04004763 **/
James Smart23288b72018-05-04 20:37:53 -07004764static void
James Smartda0436e2009-05-22 14:51:39 -04004765lpfc_sli4_parse_latt_fault(struct lpfc_hba *phba,
4766 struct lpfc_acqe_link *acqe_link)
4767{
James Smartda0436e2009-05-22 14:51:39 -04004768 switch (bf_get(lpfc_acqe_link_fault, acqe_link)) {
4769 case LPFC_ASYNC_LINK_FAULT_NONE:
4770 case LPFC_ASYNC_LINK_FAULT_LOCAL:
4771 case LPFC_ASYNC_LINK_FAULT_REMOTE:
James Smart23288b72018-05-04 20:37:53 -07004772 case LPFC_ASYNC_LINK_FAULT_LR_LRR:
James Smartda0436e2009-05-22 14:51:39 -04004773 break;
4774 default:
Dick Kennedy372c1872020-06-30 14:50:00 -07004775 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart23288b72018-05-04 20:37:53 -07004776 "0398 Unknown link fault code: x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04004777 bf_get(lpfc_acqe_link_fault, acqe_link));
James Smartda0436e2009-05-22 14:51:39 -04004778 break;
4779 }
James Smartda0436e2009-05-22 14:51:39 -04004780}
4781
4782/**
4783 * lpfc_sli4_parse_latt_type - Parse sli4 link attention type
4784 * @phba: pointer to lpfc hba data structure.
4785 * @acqe_link: pointer to the async link completion queue entry.
4786 *
4787 * This routine is to parse the SLI4 link attention type and translate it
4788 * into the base driver's link attention type coding.
4789 *
4790 * Return: Link attention type in terms of base driver's coding.
4791 **/
4792static uint8_t
4793lpfc_sli4_parse_latt_type(struct lpfc_hba *phba,
4794 struct lpfc_acqe_link *acqe_link)
4795{
4796 uint8_t att_type;
4797
4798 switch (bf_get(lpfc_acqe_link_status, acqe_link)) {
4799 case LPFC_ASYNC_LINK_STATUS_DOWN:
4800 case LPFC_ASYNC_LINK_STATUS_LOGICAL_DOWN:
James Smart76a95d72010-11-20 23:11:48 -05004801 att_type = LPFC_ATT_LINK_DOWN;
James Smartda0436e2009-05-22 14:51:39 -04004802 break;
4803 case LPFC_ASYNC_LINK_STATUS_UP:
4804 /* Ignore physical link up events - wait for logical link up */
James Smart76a95d72010-11-20 23:11:48 -05004805 att_type = LPFC_ATT_RESERVED;
James Smartda0436e2009-05-22 14:51:39 -04004806 break;
4807 case LPFC_ASYNC_LINK_STATUS_LOGICAL_UP:
James Smart76a95d72010-11-20 23:11:48 -05004808 att_type = LPFC_ATT_LINK_UP;
James Smartda0436e2009-05-22 14:51:39 -04004809 break;
4810 default:
Dick Kennedy372c1872020-06-30 14:50:00 -07004811 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartda0436e2009-05-22 14:51:39 -04004812 "0399 Invalid link attention type: x%x\n",
4813 bf_get(lpfc_acqe_link_status, acqe_link));
James Smart76a95d72010-11-20 23:11:48 -05004814 att_type = LPFC_ATT_RESERVED;
James Smartda0436e2009-05-22 14:51:39 -04004815 break;
4816 }
4817 return att_type;
4818}
4819
4820/**
James Smart8b68cd52012-09-29 11:32:37 -04004821 * lpfc_sli_port_speed_get - Get sli3 link speed code to link speed
4822 * @phba: pointer to lpfc hba data structure.
4823 *
4824 * This routine is to get an SLI3 FC port's link speed in Mbps.
4825 *
4826 * Return: link speed in terms of Mbps.
4827 **/
4828uint32_t
4829lpfc_sli_port_speed_get(struct lpfc_hba *phba)
4830{
4831 uint32_t link_speed;
4832
4833 if (!lpfc_is_link_up(phba))
4834 return 0;
4835
James Smarta085e872015-12-16 18:12:02 -05004836 if (phba->sli_rev <= LPFC_SLI_REV3) {
4837 switch (phba->fc_linkspeed) {
4838 case LPFC_LINK_SPEED_1GHZ:
4839 link_speed = 1000;
4840 break;
4841 case LPFC_LINK_SPEED_2GHZ:
4842 link_speed = 2000;
4843 break;
4844 case LPFC_LINK_SPEED_4GHZ:
4845 link_speed = 4000;
4846 break;
4847 case LPFC_LINK_SPEED_8GHZ:
4848 link_speed = 8000;
4849 break;
4850 case LPFC_LINK_SPEED_10GHZ:
4851 link_speed = 10000;
4852 break;
4853 case LPFC_LINK_SPEED_16GHZ:
4854 link_speed = 16000;
4855 break;
4856 default:
4857 link_speed = 0;
4858 }
4859 } else {
4860 if (phba->sli4_hba.link_state.logical_speed)
4861 link_speed =
4862 phba->sli4_hba.link_state.logical_speed;
4863 else
4864 link_speed = phba->sli4_hba.link_state.speed;
James Smart8b68cd52012-09-29 11:32:37 -04004865 }
4866 return link_speed;
4867}
4868
4869/**
4870 * lpfc_sli4_port_speed_parse - Parse async evt link speed code to link speed
4871 * @phba: pointer to lpfc hba data structure.
4872 * @evt_code: asynchronous event code.
4873 * @speed_code: asynchronous event link speed code.
4874 *
4875 * This routine is to parse the giving SLI4 async event link speed code into
4876 * value of Mbps for the link speed.
4877 *
4878 * Return: link speed in terms of Mbps.
4879 **/
4880static uint32_t
4881lpfc_sli4_port_speed_parse(struct lpfc_hba *phba, uint32_t evt_code,
4882 uint8_t speed_code)
4883{
4884 uint32_t port_speed;
4885
4886 switch (evt_code) {
4887 case LPFC_TRAILER_CODE_LINK:
4888 switch (speed_code) {
James Smart26d830e2015-04-07 15:07:17 -04004889 case LPFC_ASYNC_LINK_SPEED_ZERO:
James Smart8b68cd52012-09-29 11:32:37 -04004890 port_speed = 0;
4891 break;
James Smart26d830e2015-04-07 15:07:17 -04004892 case LPFC_ASYNC_LINK_SPEED_10MBPS:
James Smart8b68cd52012-09-29 11:32:37 -04004893 port_speed = 10;
4894 break;
James Smart26d830e2015-04-07 15:07:17 -04004895 case LPFC_ASYNC_LINK_SPEED_100MBPS:
James Smart8b68cd52012-09-29 11:32:37 -04004896 port_speed = 100;
4897 break;
James Smart26d830e2015-04-07 15:07:17 -04004898 case LPFC_ASYNC_LINK_SPEED_1GBPS:
James Smart8b68cd52012-09-29 11:32:37 -04004899 port_speed = 1000;
4900 break;
James Smart26d830e2015-04-07 15:07:17 -04004901 case LPFC_ASYNC_LINK_SPEED_10GBPS:
James Smart8b68cd52012-09-29 11:32:37 -04004902 port_speed = 10000;
4903 break;
James Smart26d830e2015-04-07 15:07:17 -04004904 case LPFC_ASYNC_LINK_SPEED_20GBPS:
4905 port_speed = 20000;
4906 break;
4907 case LPFC_ASYNC_LINK_SPEED_25GBPS:
4908 port_speed = 25000;
4909 break;
4910 case LPFC_ASYNC_LINK_SPEED_40GBPS:
4911 port_speed = 40000;
4912 break;
James Smart8b68cd52012-09-29 11:32:37 -04004913 default:
4914 port_speed = 0;
4915 }
4916 break;
4917 case LPFC_TRAILER_CODE_FC:
4918 switch (speed_code) {
James Smart26d830e2015-04-07 15:07:17 -04004919 case LPFC_FC_LA_SPEED_UNKNOWN:
James Smart8b68cd52012-09-29 11:32:37 -04004920 port_speed = 0;
4921 break;
James Smart26d830e2015-04-07 15:07:17 -04004922 case LPFC_FC_LA_SPEED_1G:
James Smart8b68cd52012-09-29 11:32:37 -04004923 port_speed = 1000;
4924 break;
James Smart26d830e2015-04-07 15:07:17 -04004925 case LPFC_FC_LA_SPEED_2G:
James Smart8b68cd52012-09-29 11:32:37 -04004926 port_speed = 2000;
4927 break;
James Smart26d830e2015-04-07 15:07:17 -04004928 case LPFC_FC_LA_SPEED_4G:
James Smart8b68cd52012-09-29 11:32:37 -04004929 port_speed = 4000;
4930 break;
James Smart26d830e2015-04-07 15:07:17 -04004931 case LPFC_FC_LA_SPEED_8G:
James Smart8b68cd52012-09-29 11:32:37 -04004932 port_speed = 8000;
4933 break;
James Smart26d830e2015-04-07 15:07:17 -04004934 case LPFC_FC_LA_SPEED_10G:
James Smart8b68cd52012-09-29 11:32:37 -04004935 port_speed = 10000;
4936 break;
James Smart26d830e2015-04-07 15:07:17 -04004937 case LPFC_FC_LA_SPEED_16G:
James Smart8b68cd52012-09-29 11:32:37 -04004938 port_speed = 16000;
4939 break;
James Smartd38dd522015-08-31 16:48:17 -04004940 case LPFC_FC_LA_SPEED_32G:
4941 port_speed = 32000;
4942 break;
James Smartfbd8a6b2018-02-22 08:18:45 -08004943 case LPFC_FC_LA_SPEED_64G:
4944 port_speed = 64000;
4945 break;
James Smart1dc5ec22018-10-23 13:41:11 -07004946 case LPFC_FC_LA_SPEED_128G:
4947 port_speed = 128000;
4948 break;
James Smart8b68cd52012-09-29 11:32:37 -04004949 default:
4950 port_speed = 0;
4951 }
4952 break;
4953 default:
4954 port_speed = 0;
4955 }
4956 return port_speed;
4957}
4958
4959/**
James Smart70f3c072010-12-15 17:57:33 -05004960 * lpfc_sli4_async_link_evt - Process the asynchronous FCoE link event
James Smartda0436e2009-05-22 14:51:39 -04004961 * @phba: pointer to lpfc hba data structure.
4962 * @acqe_link: pointer to the async link completion queue entry.
4963 *
James Smart70f3c072010-12-15 17:57:33 -05004964 * This routine is to handle the SLI4 asynchronous FCoE link event.
James Smartda0436e2009-05-22 14:51:39 -04004965 **/
4966static void
4967lpfc_sli4_async_link_evt(struct lpfc_hba *phba,
4968 struct lpfc_acqe_link *acqe_link)
4969{
4970 struct lpfc_dmabuf *mp;
4971 LPFC_MBOXQ_t *pmb;
4972 MAILBOX_t *mb;
James Smart76a95d72010-11-20 23:11:48 -05004973 struct lpfc_mbx_read_top *la;
James Smartda0436e2009-05-22 14:51:39 -04004974 uint8_t att_type;
James Smart76a95d72010-11-20 23:11:48 -05004975 int rc;
James Smartda0436e2009-05-22 14:51:39 -04004976
4977 att_type = lpfc_sli4_parse_latt_type(phba, acqe_link);
James Smart76a95d72010-11-20 23:11:48 -05004978 if (att_type != LPFC_ATT_LINK_DOWN && att_type != LPFC_ATT_LINK_UP)
James Smartda0436e2009-05-22 14:51:39 -04004979 return;
James Smart32b97932009-07-19 10:01:21 -04004980 phba->fcoe_eventtag = acqe_link->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04004981 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4982 if (!pmb) {
Dick Kennedy372c1872020-06-30 14:50:00 -07004983 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartda0436e2009-05-22 14:51:39 -04004984 "0395 The mboxq allocation failed\n");
4985 return;
4986 }
4987 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4988 if (!mp) {
Dick Kennedy372c1872020-06-30 14:50:00 -07004989 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartda0436e2009-05-22 14:51:39 -04004990 "0396 The lpfc_dmabuf allocation failed\n");
4991 goto out_free_pmb;
4992 }
4993 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
4994 if (!mp->virt) {
Dick Kennedy372c1872020-06-30 14:50:00 -07004995 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartda0436e2009-05-22 14:51:39 -04004996 "0397 The mbuf allocation failed\n");
4997 goto out_free_dmabuf;
4998 }
4999
5000 /* Cleanup any outstanding ELS commands */
5001 lpfc_els_flush_all_cmd(phba);
5002
5003 /* Block ELS IOCBs until we have done process link event */
James Smart895427b2017-02-12 13:52:30 -08005004 phba->sli4_hba.els_wq->pring->flag |= LPFC_STOP_IOCB_EVENT;
James Smartda0436e2009-05-22 14:51:39 -04005005
5006 /* Update link event statistics */
5007 phba->sli.slistat.link_event++;
5008
James Smart76a95d72010-11-20 23:11:48 -05005009 /* Create lpfc_handle_latt mailbox command from link ACQE */
5010 lpfc_read_topology(phba, pmb, mp);
5011 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
James Smartda0436e2009-05-22 14:51:39 -04005012 pmb->vport = phba->pport;
5013
James Smartda0436e2009-05-22 14:51:39 -04005014 /* Keep the link status for extra SLI4 state machine reference */
5015 phba->sli4_hba.link_state.speed =
James Smart8b68cd52012-09-29 11:32:37 -04005016 lpfc_sli4_port_speed_parse(phba, LPFC_TRAILER_CODE_LINK,
5017 bf_get(lpfc_acqe_link_speed, acqe_link));
James Smartda0436e2009-05-22 14:51:39 -04005018 phba->sli4_hba.link_state.duplex =
5019 bf_get(lpfc_acqe_link_duplex, acqe_link);
5020 phba->sli4_hba.link_state.status =
5021 bf_get(lpfc_acqe_link_status, acqe_link);
James Smart70f3c072010-12-15 17:57:33 -05005022 phba->sli4_hba.link_state.type =
5023 bf_get(lpfc_acqe_link_type, acqe_link);
5024 phba->sli4_hba.link_state.number =
5025 bf_get(lpfc_acqe_link_number, acqe_link);
James Smartda0436e2009-05-22 14:51:39 -04005026 phba->sli4_hba.link_state.fault =
5027 bf_get(lpfc_acqe_link_fault, acqe_link);
James Smart65467b62010-01-26 23:08:29 -05005028 phba->sli4_hba.link_state.logical_speed =
James Smart8b68cd52012-09-29 11:32:37 -04005029 bf_get(lpfc_acqe_logical_link_speed, acqe_link) * 10;
5030
James Smart70f3c072010-12-15 17:57:33 -05005031 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartc31098c2011-04-16 11:03:33 -04005032 "2900 Async FC/FCoE Link event - Speed:%dGBit "
5033 "duplex:x%x LA Type:x%x Port Type:%d Port Number:%d "
5034 "Logical speed:%dMbps Fault:%d\n",
James Smart70f3c072010-12-15 17:57:33 -05005035 phba->sli4_hba.link_state.speed,
5036 phba->sli4_hba.link_state.topology,
5037 phba->sli4_hba.link_state.status,
5038 phba->sli4_hba.link_state.type,
5039 phba->sli4_hba.link_state.number,
James Smart8b68cd52012-09-29 11:32:37 -04005040 phba->sli4_hba.link_state.logical_speed,
James Smart70f3c072010-12-15 17:57:33 -05005041 phba->sli4_hba.link_state.fault);
James Smart76a95d72010-11-20 23:11:48 -05005042 /*
5043 * For FC Mode: issue the READ_TOPOLOGY mailbox command to fetch
5044 * topology info. Note: Optional for non FC-AL ports.
5045 */
5046 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
5047 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
5048 if (rc == MBX_NOT_FINISHED)
5049 goto out_free_dmabuf;
5050 return;
5051 }
5052 /*
5053 * For FCoE Mode: fill in all the topology information we need and call
5054 * the READ_TOPOLOGY completion routine to continue without actually
5055 * sending the READ_TOPOLOGY mailbox command to the port.
5056 */
James Smart23288b72018-05-04 20:37:53 -07005057 /* Initialize completion status */
James Smart76a95d72010-11-20 23:11:48 -05005058 mb = &pmb->u.mb;
James Smart23288b72018-05-04 20:37:53 -07005059 mb->mbxStatus = MBX_SUCCESS;
5060
5061 /* Parse port fault information field */
5062 lpfc_sli4_parse_latt_fault(phba, acqe_link);
James Smart76a95d72010-11-20 23:11:48 -05005063
5064 /* Parse and translate link attention fields */
5065 la = (struct lpfc_mbx_read_top *) &pmb->u.mb.un.varReadTop;
5066 la->eventTag = acqe_link->event_tag;
5067 bf_set(lpfc_mbx_read_top_att_type, la, att_type);
5068 bf_set(lpfc_mbx_read_top_link_spd, la,
James Smarta085e872015-12-16 18:12:02 -05005069 (bf_get(lpfc_acqe_link_speed, acqe_link)));
James Smart76a95d72010-11-20 23:11:48 -05005070
5071 /* Fake the the following irrelvant fields */
5072 bf_set(lpfc_mbx_read_top_topology, la, LPFC_TOPOLOGY_PT_PT);
5073 bf_set(lpfc_mbx_read_top_alpa_granted, la, 0);
5074 bf_set(lpfc_mbx_read_top_il, la, 0);
5075 bf_set(lpfc_mbx_read_top_pb, la, 0);
5076 bf_set(lpfc_mbx_read_top_fa, la, 0);
5077 bf_set(lpfc_mbx_read_top_mm, la, 0);
James Smartda0436e2009-05-22 14:51:39 -04005078
5079 /* Invoke the lpfc_handle_latt mailbox command callback function */
James Smart76a95d72010-11-20 23:11:48 -05005080 lpfc_mbx_cmpl_read_topology(phba, pmb);
James Smartda0436e2009-05-22 14:51:39 -04005081
5082 return;
5083
5084out_free_dmabuf:
5085 kfree(mp);
5086out_free_pmb:
5087 mempool_free(pmb, phba->mbox_mem_pool);
5088}
5089
5090/**
James Smart1dc5ec22018-10-23 13:41:11 -07005091 * lpfc_async_link_speed_to_read_top - Parse async evt link speed code to read
5092 * topology.
5093 * @phba: pointer to lpfc hba data structure.
James Smart1dc5ec22018-10-23 13:41:11 -07005094 * @speed_code: asynchronous event link speed code.
5095 *
5096 * This routine is to parse the giving SLI4 async event link speed code into
5097 * value of Read topology link speed.
5098 *
5099 * Return: link speed in terms of Read topology.
5100 **/
5101static uint8_t
5102lpfc_async_link_speed_to_read_top(struct lpfc_hba *phba, uint8_t speed_code)
5103{
5104 uint8_t port_speed;
5105
5106 switch (speed_code) {
5107 case LPFC_FC_LA_SPEED_1G:
5108 port_speed = LPFC_LINK_SPEED_1GHZ;
5109 break;
5110 case LPFC_FC_LA_SPEED_2G:
5111 port_speed = LPFC_LINK_SPEED_2GHZ;
5112 break;
5113 case LPFC_FC_LA_SPEED_4G:
5114 port_speed = LPFC_LINK_SPEED_4GHZ;
5115 break;
5116 case LPFC_FC_LA_SPEED_8G:
5117 port_speed = LPFC_LINK_SPEED_8GHZ;
5118 break;
5119 case LPFC_FC_LA_SPEED_16G:
5120 port_speed = LPFC_LINK_SPEED_16GHZ;
5121 break;
5122 case LPFC_FC_LA_SPEED_32G:
5123 port_speed = LPFC_LINK_SPEED_32GHZ;
5124 break;
5125 case LPFC_FC_LA_SPEED_64G:
5126 port_speed = LPFC_LINK_SPEED_64GHZ;
5127 break;
5128 case LPFC_FC_LA_SPEED_128G:
5129 port_speed = LPFC_LINK_SPEED_128GHZ;
5130 break;
5131 case LPFC_FC_LA_SPEED_256G:
5132 port_speed = LPFC_LINK_SPEED_256GHZ;
5133 break;
5134 default:
5135 port_speed = 0;
5136 break;
5137 }
5138
5139 return port_speed;
5140}
5141
5142#define trunk_link_status(__idx)\
5143 bf_get(lpfc_acqe_fc_la_trunk_config_port##__idx, acqe_fc) ?\
5144 ((phba->trunk_link.link##__idx.state == LPFC_LINK_UP) ?\
5145 "Link up" : "Link down") : "NA"
5146/* Did port __idx reported an error */
5147#define trunk_port_fault(__idx)\
5148 bf_get(lpfc_acqe_fc_la_trunk_config_port##__idx, acqe_fc) ?\
5149 (port_fault & (1 << __idx) ? "YES" : "NO") : "NA"
5150
5151static void
5152lpfc_update_trunk_link_status(struct lpfc_hba *phba,
5153 struct lpfc_acqe_fc_la *acqe_fc)
5154{
5155 uint8_t port_fault = bf_get(lpfc_acqe_fc_la_trunk_linkmask, acqe_fc);
5156 uint8_t err = bf_get(lpfc_acqe_fc_la_trunk_fault, acqe_fc);
5157
5158 phba->sli4_hba.link_state.speed =
5159 lpfc_sli4_port_speed_parse(phba, LPFC_TRAILER_CODE_FC,
5160 bf_get(lpfc_acqe_fc_la_speed, acqe_fc));
5161
5162 phba->sli4_hba.link_state.logical_speed =
James Smartb8e6f132019-05-21 17:49:04 -07005163 bf_get(lpfc_acqe_fc_la_llink_spd, acqe_fc) * 10;
James Smart1dc5ec22018-10-23 13:41:11 -07005164 /* We got FC link speed, convert to fc_linkspeed (READ_TOPOLOGY) */
5165 phba->fc_linkspeed =
5166 lpfc_async_link_speed_to_read_top(
5167 phba,
5168 bf_get(lpfc_acqe_fc_la_speed, acqe_fc));
5169
5170 if (bf_get(lpfc_acqe_fc_la_trunk_config_port0, acqe_fc)) {
5171 phba->trunk_link.link0.state =
5172 bf_get(lpfc_acqe_fc_la_trunk_link_status_port0, acqe_fc)
5173 ? LPFC_LINK_UP : LPFC_LINK_DOWN;
James Smart529b3dd2018-12-13 15:17:54 -08005174 phba->trunk_link.link0.fault = port_fault & 0x1 ? err : 0;
James Smart1dc5ec22018-10-23 13:41:11 -07005175 }
5176 if (bf_get(lpfc_acqe_fc_la_trunk_config_port1, acqe_fc)) {
5177 phba->trunk_link.link1.state =
5178 bf_get(lpfc_acqe_fc_la_trunk_link_status_port1, acqe_fc)
5179 ? LPFC_LINK_UP : LPFC_LINK_DOWN;
James Smart529b3dd2018-12-13 15:17:54 -08005180 phba->trunk_link.link1.fault = port_fault & 0x2 ? err : 0;
James Smart1dc5ec22018-10-23 13:41:11 -07005181 }
5182 if (bf_get(lpfc_acqe_fc_la_trunk_config_port2, acqe_fc)) {
5183 phba->trunk_link.link2.state =
5184 bf_get(lpfc_acqe_fc_la_trunk_link_status_port2, acqe_fc)
5185 ? LPFC_LINK_UP : LPFC_LINK_DOWN;
James Smart529b3dd2018-12-13 15:17:54 -08005186 phba->trunk_link.link2.fault = port_fault & 0x4 ? err : 0;
James Smart1dc5ec22018-10-23 13:41:11 -07005187 }
5188 if (bf_get(lpfc_acqe_fc_la_trunk_config_port3, acqe_fc)) {
5189 phba->trunk_link.link3.state =
5190 bf_get(lpfc_acqe_fc_la_trunk_link_status_port3, acqe_fc)
5191 ? LPFC_LINK_UP : LPFC_LINK_DOWN;
James Smart529b3dd2018-12-13 15:17:54 -08005192 phba->trunk_link.link3.fault = port_fault & 0x8 ? err : 0;
James Smart1dc5ec22018-10-23 13:41:11 -07005193 }
5194
Dick Kennedy372c1872020-06-30 14:50:00 -07005195 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart1dc5ec22018-10-23 13:41:11 -07005196 "2910 Async FC Trunking Event - Speed:%d\n"
5197 "\tLogical speed:%d "
5198 "port0: %s port1: %s port2: %s port3: %s\n",
5199 phba->sli4_hba.link_state.speed,
5200 phba->sli4_hba.link_state.logical_speed,
5201 trunk_link_status(0), trunk_link_status(1),
5202 trunk_link_status(2), trunk_link_status(3));
5203
5204 if (port_fault)
Dick Kennedy372c1872020-06-30 14:50:00 -07005205 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart1dc5ec22018-10-23 13:41:11 -07005206 "3202 trunk error:0x%x (%s) seen on port0:%s "
5207 /*
5208 * SLI-4: We have only 0xA error codes
5209 * defined as of now. print an appropriate
5210 * message in case driver needs to be updated.
5211 */
5212 "port1:%s port2:%s port3:%s\n", err, err > 0xA ?
5213 "UNDEFINED. update driver." : trunk_errmsg[err],
5214 trunk_port_fault(0), trunk_port_fault(1),
5215 trunk_port_fault(2), trunk_port_fault(3));
5216}
5217
5218
5219/**
James Smart70f3c072010-12-15 17:57:33 -05005220 * lpfc_sli4_async_fc_evt - Process the asynchronous FC link event
5221 * @phba: pointer to lpfc hba data structure.
5222 * @acqe_fc: pointer to the async fc completion queue entry.
5223 *
5224 * This routine is to handle the SLI4 asynchronous FC event. It will simply log
5225 * that the event was received and then issue a read_topology mailbox command so
5226 * that the rest of the driver will treat it the same as SLI3.
5227 **/
5228static void
5229lpfc_sli4_async_fc_evt(struct lpfc_hba *phba, struct lpfc_acqe_fc_la *acqe_fc)
5230{
5231 struct lpfc_dmabuf *mp;
5232 LPFC_MBOXQ_t *pmb;
James Smart7bdedb32016-07-06 12:36:00 -07005233 MAILBOX_t *mb;
5234 struct lpfc_mbx_read_top *la;
James Smart70f3c072010-12-15 17:57:33 -05005235 int rc;
5236
5237 if (bf_get(lpfc_trailer_type, acqe_fc) !=
5238 LPFC_FC_LA_EVENT_TYPE_FC_LINK) {
Dick Kennedy372c1872020-06-30 14:50:00 -07005239 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart70f3c072010-12-15 17:57:33 -05005240 "2895 Non FC link Event detected.(%d)\n",
5241 bf_get(lpfc_trailer_type, acqe_fc));
5242 return;
5243 }
James Smart1dc5ec22018-10-23 13:41:11 -07005244
5245 if (bf_get(lpfc_acqe_fc_la_att_type, acqe_fc) ==
5246 LPFC_FC_LA_TYPE_TRUNKING_EVENT) {
5247 lpfc_update_trunk_link_status(phba, acqe_fc);
5248 return;
5249 }
5250
James Smart70f3c072010-12-15 17:57:33 -05005251 /* Keep the link status for extra SLI4 state machine reference */
5252 phba->sli4_hba.link_state.speed =
James Smart8b68cd52012-09-29 11:32:37 -04005253 lpfc_sli4_port_speed_parse(phba, LPFC_TRAILER_CODE_FC,
5254 bf_get(lpfc_acqe_fc_la_speed, acqe_fc));
James Smart70f3c072010-12-15 17:57:33 -05005255 phba->sli4_hba.link_state.duplex = LPFC_ASYNC_LINK_DUPLEX_FULL;
5256 phba->sli4_hba.link_state.topology =
5257 bf_get(lpfc_acqe_fc_la_topology, acqe_fc);
5258 phba->sli4_hba.link_state.status =
5259 bf_get(lpfc_acqe_fc_la_att_type, acqe_fc);
5260 phba->sli4_hba.link_state.type =
5261 bf_get(lpfc_acqe_fc_la_port_type, acqe_fc);
5262 phba->sli4_hba.link_state.number =
5263 bf_get(lpfc_acqe_fc_la_port_number, acqe_fc);
5264 phba->sli4_hba.link_state.fault =
5265 bf_get(lpfc_acqe_link_fault, acqe_fc);
James Smartb8e6f132019-05-21 17:49:04 -07005266
5267 if (bf_get(lpfc_acqe_fc_la_att_type, acqe_fc) ==
5268 LPFC_FC_LA_TYPE_LINK_DOWN)
5269 phba->sli4_hba.link_state.logical_speed = 0;
5270 else if (!phba->sli4_hba.conf_trunk)
5271 phba->sli4_hba.link_state.logical_speed =
James Smart8b68cd52012-09-29 11:32:37 -04005272 bf_get(lpfc_acqe_fc_la_llink_spd, acqe_fc) * 10;
James Smartb8e6f132019-05-21 17:49:04 -07005273
James Smart70f3c072010-12-15 17:57:33 -05005274 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5275 "2896 Async FC event - Speed:%dGBaud Topology:x%x "
5276 "LA Type:x%x Port Type:%d Port Number:%d Logical speed:"
5277 "%dMbps Fault:%d\n",
5278 phba->sli4_hba.link_state.speed,
5279 phba->sli4_hba.link_state.topology,
5280 phba->sli4_hba.link_state.status,
5281 phba->sli4_hba.link_state.type,
5282 phba->sli4_hba.link_state.number,
James Smart8b68cd52012-09-29 11:32:37 -04005283 phba->sli4_hba.link_state.logical_speed,
James Smart70f3c072010-12-15 17:57:33 -05005284 phba->sli4_hba.link_state.fault);
5285 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5286 if (!pmb) {
Dick Kennedy372c1872020-06-30 14:50:00 -07005287 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart70f3c072010-12-15 17:57:33 -05005288 "2897 The mboxq allocation failed\n");
5289 return;
5290 }
5291 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5292 if (!mp) {
Dick Kennedy372c1872020-06-30 14:50:00 -07005293 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart70f3c072010-12-15 17:57:33 -05005294 "2898 The lpfc_dmabuf allocation failed\n");
5295 goto out_free_pmb;
5296 }
5297 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
5298 if (!mp->virt) {
Dick Kennedy372c1872020-06-30 14:50:00 -07005299 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart70f3c072010-12-15 17:57:33 -05005300 "2899 The mbuf allocation failed\n");
5301 goto out_free_dmabuf;
5302 }
5303
5304 /* Cleanup any outstanding ELS commands */
5305 lpfc_els_flush_all_cmd(phba);
5306
5307 /* Block ELS IOCBs until we have done process link event */
James Smart895427b2017-02-12 13:52:30 -08005308 phba->sli4_hba.els_wq->pring->flag |= LPFC_STOP_IOCB_EVENT;
James Smart70f3c072010-12-15 17:57:33 -05005309
5310 /* Update link event statistics */
5311 phba->sli.slistat.link_event++;
5312
5313 /* Create lpfc_handle_latt mailbox command from link ACQE */
5314 lpfc_read_topology(phba, pmb, mp);
5315 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
5316 pmb->vport = phba->pport;
5317
James Smart7bdedb32016-07-06 12:36:00 -07005318 if (phba->sli4_hba.link_state.status != LPFC_FC_LA_TYPE_LINK_UP) {
James Smartae9e28f2017-05-15 15:20:51 -07005319 phba->link_flag &= ~(LS_MDS_LINK_DOWN | LS_MDS_LOOPBACK);
5320
5321 switch (phba->sli4_hba.link_state.status) {
5322 case LPFC_FC_LA_TYPE_MDS_LINK_DOWN:
5323 phba->link_flag |= LS_MDS_LINK_DOWN;
5324 break;
5325 case LPFC_FC_LA_TYPE_MDS_LOOPBACK:
5326 phba->link_flag |= LS_MDS_LOOPBACK;
5327 break;
5328 default:
5329 break;
5330 }
5331
James Smart23288b72018-05-04 20:37:53 -07005332 /* Initialize completion status */
James Smart7bdedb32016-07-06 12:36:00 -07005333 mb = &pmb->u.mb;
James Smart23288b72018-05-04 20:37:53 -07005334 mb->mbxStatus = MBX_SUCCESS;
5335
5336 /* Parse port fault information field */
5337 lpfc_sli4_parse_latt_fault(phba, (void *)acqe_fc);
James Smart7bdedb32016-07-06 12:36:00 -07005338
5339 /* Parse and translate link attention fields */
5340 la = (struct lpfc_mbx_read_top *)&pmb->u.mb.un.varReadTop;
5341 la->eventTag = acqe_fc->event_tag;
James Smart7bdedb32016-07-06 12:36:00 -07005342
James Smartaeb3c812017-04-21 16:05:02 -07005343 if (phba->sli4_hba.link_state.status ==
5344 LPFC_FC_LA_TYPE_UNEXP_WWPN) {
5345 bf_set(lpfc_mbx_read_top_att_type, la,
5346 LPFC_FC_LA_TYPE_UNEXP_WWPN);
5347 } else {
5348 bf_set(lpfc_mbx_read_top_att_type, la,
5349 LPFC_FC_LA_TYPE_LINK_DOWN);
5350 }
James Smart7bdedb32016-07-06 12:36:00 -07005351 /* Invoke the mailbox command callback function */
5352 lpfc_mbx_cmpl_read_topology(phba, pmb);
5353
5354 return;
5355 }
5356
James Smart70f3c072010-12-15 17:57:33 -05005357 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
5358 if (rc == MBX_NOT_FINISHED)
5359 goto out_free_dmabuf;
5360 return;
5361
5362out_free_dmabuf:
5363 kfree(mp);
5364out_free_pmb:
5365 mempool_free(pmb, phba->mbox_mem_pool);
5366}
5367
5368/**
5369 * lpfc_sli4_async_sli_evt - Process the asynchronous SLI link event
5370 * @phba: pointer to lpfc hba data structure.
Lee Jonesfe614ac2020-07-23 13:24:22 +01005371 * @acqe_sli: pointer to the async SLI completion queue entry.
James Smart70f3c072010-12-15 17:57:33 -05005372 *
5373 * This routine is to handle the SLI4 asynchronous SLI events.
5374 **/
5375static void
5376lpfc_sli4_async_sli_evt(struct lpfc_hba *phba, struct lpfc_acqe_sli *acqe_sli)
5377{
James Smart4b8bae02012-06-12 13:55:07 -04005378 char port_name;
James Smart8c1312e2012-10-31 14:45:09 -04005379 char message[128];
James Smart4b8bae02012-06-12 13:55:07 -04005380 uint8_t status;
James Smart946727d2015-04-07 15:07:09 -04005381 uint8_t evt_type;
James Smart448193b2015-12-16 18:12:05 -05005382 uint8_t operational = 0;
James Smart946727d2015-04-07 15:07:09 -04005383 struct temp_event temp_event_data;
James Smart4b8bae02012-06-12 13:55:07 -04005384 struct lpfc_acqe_misconfigured_event *misconfigured;
James Smart946727d2015-04-07 15:07:09 -04005385 struct Scsi_Host *shost;
James Smartcd713482018-10-23 13:41:01 -07005386 struct lpfc_vport **vports;
5387 int rc, i;
James Smart4b8bae02012-06-12 13:55:07 -04005388
James Smart946727d2015-04-07 15:07:09 -04005389 evt_type = bf_get(lpfc_trailer_type, acqe_sli);
5390
James Smart448193b2015-12-16 18:12:05 -05005391 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartd11ed162019-09-21 20:59:03 -07005392 "2901 Async SLI event - Type:%d, Event Data: x%08x "
5393 "x%08x x%08x x%08x\n", evt_type,
James Smart448193b2015-12-16 18:12:05 -05005394 acqe_sli->event_data1, acqe_sli->event_data2,
James Smartd11ed162019-09-21 20:59:03 -07005395 acqe_sli->reserved, acqe_sli->trailer);
James Smart4b8bae02012-06-12 13:55:07 -04005396
5397 port_name = phba->Port[0];
5398 if (port_name == 0x00)
5399 port_name = '?'; /* get port name is empty */
5400
James Smart946727d2015-04-07 15:07:09 -04005401 switch (evt_type) {
5402 case LPFC_SLI_EVENT_TYPE_OVER_TEMP:
5403 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
5404 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
5405 temp_event_data.data = (uint32_t)acqe_sli->event_data1;
5406
5407 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5408 "3190 Over Temperature:%d Celsius- Port Name %c\n",
5409 acqe_sli->event_data1, port_name);
5410
James Smart310429e2016-07-06 12:35:54 -07005411 phba->sfp_warning |= LPFC_TRANSGRESSION_HIGH_TEMPERATURE;
James Smart946727d2015-04-07 15:07:09 -04005412 shost = lpfc_shost_from_vport(phba->pport);
5413 fc_host_post_vendor_event(shost, fc_get_event_number(),
5414 sizeof(temp_event_data),
5415 (char *)&temp_event_data,
5416 SCSI_NL_VID_TYPE_PCI
5417 | PCI_VENDOR_ID_EMULEX);
5418 break;
5419 case LPFC_SLI_EVENT_TYPE_NORM_TEMP:
5420 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
5421 temp_event_data.event_code = LPFC_NORMAL_TEMP;
5422 temp_event_data.data = (uint32_t)acqe_sli->event_data1;
5423
5424 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5425 "3191 Normal Temperature:%d Celsius - Port Name %c\n",
5426 acqe_sli->event_data1, port_name);
5427
5428 shost = lpfc_shost_from_vport(phba->pport);
5429 fc_host_post_vendor_event(shost, fc_get_event_number(),
5430 sizeof(temp_event_data),
5431 (char *)&temp_event_data,
5432 SCSI_NL_VID_TYPE_PCI
5433 | PCI_VENDOR_ID_EMULEX);
5434 break;
5435 case LPFC_SLI_EVENT_TYPE_MISCONFIGURED:
5436 misconfigured = (struct lpfc_acqe_misconfigured_event *)
James Smart4b8bae02012-06-12 13:55:07 -04005437 &acqe_sli->event_data1;
5438
James Smart946727d2015-04-07 15:07:09 -04005439 /* fetch the status for this port */
5440 switch (phba->sli4_hba.lnk_info.lnk_no) {
5441 case LPFC_LINK_NUMBER_0:
James Smart448193b2015-12-16 18:12:05 -05005442 status = bf_get(lpfc_sli_misconfigured_port0_state,
5443 &misconfigured->theEvent);
5444 operational = bf_get(lpfc_sli_misconfigured_port0_op,
James Smart4b8bae02012-06-12 13:55:07 -04005445 &misconfigured->theEvent);
James Smart946727d2015-04-07 15:07:09 -04005446 break;
5447 case LPFC_LINK_NUMBER_1:
James Smart448193b2015-12-16 18:12:05 -05005448 status = bf_get(lpfc_sli_misconfigured_port1_state,
5449 &misconfigured->theEvent);
5450 operational = bf_get(lpfc_sli_misconfigured_port1_op,
James Smart4b8bae02012-06-12 13:55:07 -04005451 &misconfigured->theEvent);
James Smart946727d2015-04-07 15:07:09 -04005452 break;
5453 case LPFC_LINK_NUMBER_2:
James Smart448193b2015-12-16 18:12:05 -05005454 status = bf_get(lpfc_sli_misconfigured_port2_state,
5455 &misconfigured->theEvent);
5456 operational = bf_get(lpfc_sli_misconfigured_port2_op,
James Smart4b8bae02012-06-12 13:55:07 -04005457 &misconfigured->theEvent);
James Smart946727d2015-04-07 15:07:09 -04005458 break;
5459 case LPFC_LINK_NUMBER_3:
James Smart448193b2015-12-16 18:12:05 -05005460 status = bf_get(lpfc_sli_misconfigured_port3_state,
5461 &misconfigured->theEvent);
5462 operational = bf_get(lpfc_sli_misconfigured_port3_op,
James Smart4b8bae02012-06-12 13:55:07 -04005463 &misconfigured->theEvent);
James Smart946727d2015-04-07 15:07:09 -04005464 break;
5465 default:
Dick Kennedy372c1872020-06-30 14:50:00 -07005466 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart448193b2015-12-16 18:12:05 -05005467 "3296 "
5468 "LPFC_SLI_EVENT_TYPE_MISCONFIGURED "
5469 "event: Invalid link %d",
5470 phba->sli4_hba.lnk_info.lnk_no);
5471 return;
James Smart946727d2015-04-07 15:07:09 -04005472 }
James Smart4b8bae02012-06-12 13:55:07 -04005473
James Smart448193b2015-12-16 18:12:05 -05005474 /* Skip if optic state unchanged */
5475 if (phba->sli4_hba.lnk_info.optic_state == status)
5476 return;
5477
James Smart946727d2015-04-07 15:07:09 -04005478 switch (status) {
5479 case LPFC_SLI_EVENT_STATUS_VALID:
James Smart448193b2015-12-16 18:12:05 -05005480 sprintf(message, "Physical Link is functional");
5481 break;
James Smart946727d2015-04-07 15:07:09 -04005482 case LPFC_SLI_EVENT_STATUS_NOT_PRESENT:
5483 sprintf(message, "Optics faulted/incorrectly "
5484 "installed/not installed - Reseat optics, "
5485 "if issue not resolved, replace.");
5486 break;
5487 case LPFC_SLI_EVENT_STATUS_WRONG_TYPE:
5488 sprintf(message,
5489 "Optics of two types installed - Remove one "
5490 "optic or install matching pair of optics.");
5491 break;
5492 case LPFC_SLI_EVENT_STATUS_UNSUPPORTED:
5493 sprintf(message, "Incompatible optics - Replace with "
James Smart292098b2012-09-29 11:31:41 -04005494 "compatible optics for card to function.");
James Smart946727d2015-04-07 15:07:09 -04005495 break;
James Smart448193b2015-12-16 18:12:05 -05005496 case LPFC_SLI_EVENT_STATUS_UNQUALIFIED:
5497 sprintf(message, "Unqualified optics - Replace with "
5498 "Avago optics for Warranty and Technical "
5499 "Support - Link is%s operational",
James Smart2ea259e2017-02-12 13:52:27 -08005500 (operational) ? " not" : "");
James Smart448193b2015-12-16 18:12:05 -05005501 break;
5502 case LPFC_SLI_EVENT_STATUS_UNCERTIFIED:
5503 sprintf(message, "Uncertified optics - Replace with "
5504 "Avago-certified optics to enable link "
5505 "operation - Link is%s operational",
James Smart2ea259e2017-02-12 13:52:27 -08005506 (operational) ? " not" : "");
James Smart448193b2015-12-16 18:12:05 -05005507 break;
James Smart946727d2015-04-07 15:07:09 -04005508 default:
5509 /* firmware is reporting a status we don't know about */
5510 sprintf(message, "Unknown event status x%02x", status);
5511 break;
5512 }
James Smartcd713482018-10-23 13:41:01 -07005513
5514 /* Issue READ_CONFIG mbox command to refresh supported speeds */
5515 rc = lpfc_sli4_read_config(phba);
James Smart3952e912018-10-23 13:41:02 -07005516 if (rc) {
James Smartcd713482018-10-23 13:41:01 -07005517 phba->lmt = 0;
Dick Kennedy372c1872020-06-30 14:50:00 -07005518 lpfc_printf_log(phba, KERN_ERR,
5519 LOG_TRACE_EVENT,
James Smartcd713482018-10-23 13:41:01 -07005520 "3194 Unable to retrieve supported "
James Smart3952e912018-10-23 13:41:02 -07005521 "speeds, rc = 0x%x\n", rc);
James Smartcd713482018-10-23 13:41:01 -07005522 }
5523 vports = lpfc_create_vport_work_array(phba);
5524 if (vports != NULL) {
5525 for (i = 0; i <= phba->max_vports && vports[i] != NULL;
5526 i++) {
5527 shost = lpfc_shost_from_vport(vports[i]);
5528 lpfc_host_supported_speeds_set(shost);
5529 }
5530 }
5531 lpfc_destroy_vport_work_array(phba, vports);
5532
James Smart448193b2015-12-16 18:12:05 -05005533 phba->sli4_hba.lnk_info.optic_state = status;
James Smart946727d2015-04-07 15:07:09 -04005534 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart448193b2015-12-16 18:12:05 -05005535 "3176 Port Name %c %s\n", port_name, message);
James Smart946727d2015-04-07 15:07:09 -04005536 break;
5537 case LPFC_SLI_EVENT_TYPE_REMOTE_DPORT:
5538 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5539 "3192 Remote DPort Test Initiated - "
5540 "Event Data1:x%08x Event Data2: x%08x\n",
5541 acqe_sli->event_data1, acqe_sli->event_data2);
James Smart4b8bae02012-06-12 13:55:07 -04005542 break;
James Smarte7d85952019-10-18 14:18:29 -07005543 case LPFC_SLI_EVENT_TYPE_MISCONF_FAWWN:
5544 /* Misconfigured WWN. Reports that the SLI Port is configured
5545 * to use FA-WWN, but the attached device doesn’t support it.
5546 * No driver action is required.
5547 * Event Data1 - N.A, Event Data2 - N.A
5548 */
5549 lpfc_log_msg(phba, KERN_WARNING, LOG_SLI,
5550 "2699 Misconfigured FA-WWN - Attached device does "
5551 "not support FA-WWN\n");
5552 break;
James Smartd11ed162019-09-21 20:59:03 -07005553 case LPFC_SLI_EVENT_TYPE_EEPROM_FAILURE:
5554 /* EEPROM failure. No driver action is required */
5555 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5556 "2518 EEPROM failure - "
5557 "Event Data1: x%08x Event Data2: x%08x\n",
5558 acqe_sli->event_data1, acqe_sli->event_data2);
5559 break;
James Smart4b8bae02012-06-12 13:55:07 -04005560 default:
James Smart946727d2015-04-07 15:07:09 -04005561 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartd11ed162019-09-21 20:59:03 -07005562 "3193 Unrecognized SLI event, type: 0x%x",
James Smart946727d2015-04-07 15:07:09 -04005563 evt_type);
James Smart4b8bae02012-06-12 13:55:07 -04005564 break;
5565 }
James Smart70f3c072010-12-15 17:57:33 -05005566}
5567
5568/**
James Smartfc2b9892010-02-26 14:15:29 -05005569 * lpfc_sli4_perform_vport_cvl - Perform clear virtual link on a vport
5570 * @vport: pointer to vport data structure.
5571 *
5572 * This routine is to perform Clear Virtual Link (CVL) on a vport in
5573 * response to a CVL event.
5574 *
5575 * Return the pointer to the ndlp with the vport if successful, otherwise
5576 * return NULL.
5577 **/
5578static struct lpfc_nodelist *
5579lpfc_sli4_perform_vport_cvl(struct lpfc_vport *vport)
5580{
5581 struct lpfc_nodelist *ndlp;
5582 struct Scsi_Host *shost;
5583 struct lpfc_hba *phba;
5584
5585 if (!vport)
5586 return NULL;
James Smartfc2b9892010-02-26 14:15:29 -05005587 phba = vport->phba;
5588 if (!phba)
5589 return NULL;
James Smart78730cf2010-04-06 15:06:30 -04005590 ndlp = lpfc_findnode_did(vport, Fabric_DID);
5591 if (!ndlp) {
5592 /* Cannot find existing Fabric ndlp, so allocate a new one */
James Smart9d3d3402017-04-21 16:05:00 -07005593 ndlp = lpfc_nlp_init(vport, Fabric_DID);
James Smart78730cf2010-04-06 15:06:30 -04005594 if (!ndlp)
5595 return 0;
James Smart78730cf2010-04-06 15:06:30 -04005596 /* Set the node type */
5597 ndlp->nlp_type |= NLP_FABRIC;
5598 /* Put ndlp onto node list */
5599 lpfc_enqueue_node(vport, ndlp);
5600 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
5601 /* re-setup ndlp without removing from node list */
5602 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
5603 if (!ndlp)
5604 return 0;
5605 }
James Smart63e801c2010-11-20 23:14:19 -05005606 if ((phba->pport->port_state < LPFC_FLOGI) &&
5607 (phba->pport->port_state != LPFC_VPORT_FAILED))
James Smartfc2b9892010-02-26 14:15:29 -05005608 return NULL;
5609 /* If virtual link is not yet instantiated ignore CVL */
James Smart63e801c2010-11-20 23:14:19 -05005610 if ((vport != phba->pport) && (vport->port_state < LPFC_FDISC)
5611 && (vport->port_state != LPFC_VPORT_FAILED))
James Smartfc2b9892010-02-26 14:15:29 -05005612 return NULL;
5613 shost = lpfc_shost_from_vport(vport);
5614 if (!shost)
5615 return NULL;
5616 lpfc_linkdown_port(vport);
5617 lpfc_cleanup_pending_mbox(vport);
5618 spin_lock_irq(shost->host_lock);
5619 vport->fc_flag |= FC_VPORT_CVL_RCVD;
5620 spin_unlock_irq(shost->host_lock);
5621
5622 return ndlp;
5623}
5624
5625/**
5626 * lpfc_sli4_perform_all_vport_cvl - Perform clear virtual link on all vports
Lee Jonesfe614ac2020-07-23 13:24:22 +01005627 * @phba: pointer to lpfc hba data structure.
James Smartfc2b9892010-02-26 14:15:29 -05005628 *
5629 * This routine is to perform Clear Virtual Link (CVL) on all vports in
5630 * response to a FCF dead event.
5631 **/
5632static void
5633lpfc_sli4_perform_all_vport_cvl(struct lpfc_hba *phba)
5634{
5635 struct lpfc_vport **vports;
5636 int i;
5637
5638 vports = lpfc_create_vport_work_array(phba);
5639 if (vports)
5640 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
5641 lpfc_sli4_perform_vport_cvl(vports[i]);
5642 lpfc_destroy_vport_work_array(phba, vports);
5643}
5644
5645/**
James Smart76a95d72010-11-20 23:11:48 -05005646 * lpfc_sli4_async_fip_evt - Process the asynchronous FCoE FIP event
James Smartda0436e2009-05-22 14:51:39 -04005647 * @phba: pointer to lpfc hba data structure.
Lee Jonesfe614ac2020-07-23 13:24:22 +01005648 * @acqe_fip: pointer to the async fcoe completion queue entry.
James Smartda0436e2009-05-22 14:51:39 -04005649 *
5650 * This routine is to handle the SLI4 asynchronous fcoe event.
5651 **/
5652static void
James Smart76a95d72010-11-20 23:11:48 -05005653lpfc_sli4_async_fip_evt(struct lpfc_hba *phba,
James Smart70f3c072010-12-15 17:57:33 -05005654 struct lpfc_acqe_fip *acqe_fip)
James Smartda0436e2009-05-22 14:51:39 -04005655{
James Smart70f3c072010-12-15 17:57:33 -05005656 uint8_t event_type = bf_get(lpfc_trailer_type, acqe_fip);
James Smartda0436e2009-05-22 14:51:39 -04005657 int rc;
James Smart6669f9b2009-10-02 15:16:45 -04005658 struct lpfc_vport *vport;
5659 struct lpfc_nodelist *ndlp;
5660 struct Scsi_Host *shost;
James Smart695a8142010-01-26 23:08:03 -05005661 int active_vlink_present;
5662 struct lpfc_vport **vports;
5663 int i;
James Smartda0436e2009-05-22 14:51:39 -04005664
James Smart70f3c072010-12-15 17:57:33 -05005665 phba->fc_eventTag = acqe_fip->event_tag;
5666 phba->fcoe_eventtag = acqe_fip->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04005667 switch (event_type) {
James Smart70f3c072010-12-15 17:57:33 -05005668 case LPFC_FIP_EVENT_TYPE_NEW_FCF:
5669 case LPFC_FIP_EVENT_TYPE_FCF_PARAM_MOD:
5670 if (event_type == LPFC_FIP_EVENT_TYPE_NEW_FCF)
Dick Kennedy372c1872020-06-30 14:50:00 -07005671 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarta93ff372010-10-22 11:06:08 -04005672 "2546 New FCF event, evt_tag:x%x, "
5673 "index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05005674 acqe_fip->event_tag,
5675 acqe_fip->index);
James Smart999d8132010-03-15 11:24:56 -04005676 else
5677 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP |
5678 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04005679 "2788 FCF param modified event, "
5680 "evt_tag:x%x, index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05005681 acqe_fip->event_tag,
5682 acqe_fip->index);
James Smart38b92ef2010-08-04 16:11:39 -04005683 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smart0c9ab6f2010-02-26 14:15:57 -05005684 /*
5685 * During period of FCF discovery, read the FCF
5686 * table record indexed by the event to update
James Smarta93ff372010-10-22 11:06:08 -04005687 * FCF roundrobin failover eligible FCF bmask.
James Smart0c9ab6f2010-02-26 14:15:57 -05005688 */
5689 lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
5690 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04005691 "2779 Read FCF (x%x) for updating "
5692 "roundrobin FCF failover bmask\n",
James Smart70f3c072010-12-15 17:57:33 -05005693 acqe_fip->index);
5694 rc = lpfc_sli4_read_fcf_rec(phba, acqe_fip->index);
James Smart0c9ab6f2010-02-26 14:15:57 -05005695 }
James Smart38b92ef2010-08-04 16:11:39 -04005696
5697 /* If the FCF discovery is in progress, do nothing. */
James Smart3804dc82010-07-14 15:31:37 -04005698 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04005699 if (phba->hba_flag & FCF_TS_INPROG) {
James Smart38b92ef2010-08-04 16:11:39 -04005700 spin_unlock_irq(&phba->hbalock);
5701 break;
5702 }
5703 /* If fast FCF failover rescan event is pending, do nothing */
James Smart036cad12018-10-23 13:41:06 -07005704 if (phba->fcf.fcf_flag & (FCF_REDISC_EVT | FCF_REDISC_PEND)) {
James Smart38b92ef2010-08-04 16:11:39 -04005705 spin_unlock_irq(&phba->hbalock);
5706 break;
5707 }
5708
James Smartc2b97122013-05-31 17:05:36 -04005709 /* If the FCF has been in discovered state, do nothing. */
5710 if (phba->fcf.fcf_flag & FCF_SCAN_DONE) {
James Smart3804dc82010-07-14 15:31:37 -04005711 spin_unlock_irq(&phba->hbalock);
5712 break;
5713 }
5714 spin_unlock_irq(&phba->hbalock);
James Smart38b92ef2010-08-04 16:11:39 -04005715
James Smart0c9ab6f2010-02-26 14:15:57 -05005716 /* Otherwise, scan the entire FCF table and re-discover SAN */
5717 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04005718 "2770 Start FCF table scan per async FCF "
5719 "event, evt_tag:x%x, index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05005720 acqe_fip->event_tag, acqe_fip->index);
James Smart0c9ab6f2010-02-26 14:15:57 -05005721 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba,
5722 LPFC_FCOE_FCF_GET_FIRST);
James Smartda0436e2009-05-22 14:51:39 -04005723 if (rc)
Dick Kennedy372c1872020-06-30 14:50:00 -07005724 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart0c9ab6f2010-02-26 14:15:57 -05005725 "2547 Issue FCF scan read FCF mailbox "
James Smarta93ff372010-10-22 11:06:08 -04005726 "command failed (x%x)\n", rc);
James Smartda0436e2009-05-22 14:51:39 -04005727 break;
5728
James Smart70f3c072010-12-15 17:57:33 -05005729 case LPFC_FIP_EVENT_TYPE_FCF_TABLE_FULL:
Dick Kennedy372c1872020-06-30 14:50:00 -07005730 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
5731 "2548 FCF Table full count 0x%x tag 0x%x\n",
5732 bf_get(lpfc_acqe_fip_fcf_count, acqe_fip),
5733 acqe_fip->event_tag);
James Smartda0436e2009-05-22 14:51:39 -04005734 break;
5735
James Smart70f3c072010-12-15 17:57:33 -05005736 case LPFC_FIP_EVENT_TYPE_FCF_DEAD:
James Smart80c17842012-03-01 22:35:45 -05005737 phba->fcoe_cvl_eventtag = acqe_fip->event_tag;
Dick Kennedy372c1872020-06-30 14:50:00 -07005738 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
5739 "2549 FCF (x%x) disconnected from network, "
5740 "tag:x%x\n", acqe_fip->index,
5741 acqe_fip->event_tag);
James Smart38b92ef2010-08-04 16:11:39 -04005742 /*
5743 * If we are in the middle of FCF failover process, clear
5744 * the corresponding FCF bit in the roundrobin bitmap.
James Smartda0436e2009-05-22 14:51:39 -04005745 */
James Smartfc2b9892010-02-26 14:15:29 -05005746 spin_lock_irq(&phba->hbalock);
James Smarta1cadfe2016-07-06 12:36:02 -07005747 if ((phba->fcf.fcf_flag & FCF_DISCOVERY) &&
5748 (phba->fcf.current_rec.fcf_indx != acqe_fip->index)) {
James Smartfc2b9892010-02-26 14:15:29 -05005749 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05005750 /* Update FLOGI FCF failover eligible FCF bmask */
James Smart70f3c072010-12-15 17:57:33 -05005751 lpfc_sli4_fcf_rr_index_clear(phba, acqe_fip->index);
James Smartfc2b9892010-02-26 14:15:29 -05005752 break;
5753 }
James Smart38b92ef2010-08-04 16:11:39 -04005754 spin_unlock_irq(&phba->hbalock);
5755
5756 /* If the event is not for currently used fcf do nothing */
James Smart70f3c072010-12-15 17:57:33 -05005757 if (phba->fcf.current_rec.fcf_indx != acqe_fip->index)
James Smart38b92ef2010-08-04 16:11:39 -04005758 break;
5759
5760 /*
5761 * Otherwise, request the port to rediscover the entire FCF
5762 * table for a fast recovery from case that the current FCF
5763 * is no longer valid as we are not in the middle of FCF
5764 * failover process already.
5765 */
James Smartc2b97122013-05-31 17:05:36 -04005766 spin_lock_irq(&phba->hbalock);
5767 /* Mark the fast failover process in progress */
5768 phba->fcf.fcf_flag |= FCF_DEAD_DISC;
5769 spin_unlock_irq(&phba->hbalock);
5770
5771 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
5772 "2771 Start FCF fast failover process due to "
5773 "FCF DEAD event: evt_tag:x%x, fcf_index:x%x "
5774 "\n", acqe_fip->event_tag, acqe_fip->index);
5775 rc = lpfc_sli4_redisc_fcf_table(phba);
5776 if (rc) {
5777 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
Dick Kennedy372c1872020-06-30 14:50:00 -07005778 LOG_TRACE_EVENT,
Colin Ian King7afc0ce2018-05-03 10:26:12 +01005779 "2772 Issue FCF rediscover mailbox "
James Smartc2b97122013-05-31 17:05:36 -04005780 "command failed, fail through to FCF "
5781 "dead event\n");
5782 spin_lock_irq(&phba->hbalock);
5783 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
5784 spin_unlock_irq(&phba->hbalock);
5785 /*
5786 * Last resort will fail over by treating this
5787 * as a link down to FCF registration.
5788 */
5789 lpfc_sli4_fcf_dead_failthrough(phba);
5790 } else {
5791 /* Reset FCF roundrobin bmask for new discovery */
5792 lpfc_sli4_clear_fcf_rr_bmask(phba);
5793 /*
5794 * Handling fast FCF failover to a DEAD FCF event is
5795 * considered equalivant to receiving CVL to all vports.
5796 */
5797 lpfc_sli4_perform_all_vport_cvl(phba);
5798 }
James Smartda0436e2009-05-22 14:51:39 -04005799 break;
James Smart70f3c072010-12-15 17:57:33 -05005800 case LPFC_FIP_EVENT_TYPE_CVL:
James Smart80c17842012-03-01 22:35:45 -05005801 phba->fcoe_cvl_eventtag = acqe_fip->event_tag;
Dick Kennedy372c1872020-06-30 14:50:00 -07005802 lpfc_printf_log(phba, KERN_ERR,
5803 LOG_TRACE_EVENT,
James Smart6669f9b2009-10-02 15:16:45 -04005804 "2718 Clear Virtual Link Received for VPI 0x%x"
James Smart70f3c072010-12-15 17:57:33 -05005805 " tag 0x%x\n", acqe_fip->index, acqe_fip->event_tag);
James Smart6d368e52011-05-24 11:44:12 -04005806
James Smart6669f9b2009-10-02 15:16:45 -04005807 vport = lpfc_find_vport_by_vpid(phba,
James Smart5248a742011-07-22 18:37:06 -04005808 acqe_fip->index);
James Smartfc2b9892010-02-26 14:15:29 -05005809 ndlp = lpfc_sli4_perform_vport_cvl(vport);
James Smart6669f9b2009-10-02 15:16:45 -04005810 if (!ndlp)
5811 break;
James Smart695a8142010-01-26 23:08:03 -05005812 active_vlink_present = 0;
5813
5814 vports = lpfc_create_vport_work_array(phba);
5815 if (vports) {
5816 for (i = 0; i <= phba->max_vports && vports[i] != NULL;
5817 i++) {
5818 if ((!(vports[i]->fc_flag &
5819 FC_VPORT_CVL_RCVD)) &&
5820 (vports[i]->port_state > LPFC_FDISC)) {
5821 active_vlink_present = 1;
5822 break;
5823 }
5824 }
5825 lpfc_destroy_vport_work_array(phba, vports);
5826 }
5827
James Smartcc823552015-05-21 13:55:26 -04005828 /*
5829 * Don't re-instantiate if vport is marked for deletion.
5830 * If we are here first then vport_delete is going to wait
5831 * for discovery to complete.
5832 */
5833 if (!(vport->load_flag & FC_UNLOADING) &&
5834 active_vlink_present) {
James Smart695a8142010-01-26 23:08:03 -05005835 /*
5836 * If there are other active VLinks present,
5837 * re-instantiate the Vlink using FDISC.
5838 */
James Smart256ec0d2013-04-17 20:14:58 -04005839 mod_timer(&ndlp->nlp_delayfunc,
5840 jiffies + msecs_to_jiffies(1000));
James Smartfc2b9892010-02-26 14:15:29 -05005841 shost = lpfc_shost_from_vport(vport);
James Smart6669f9b2009-10-02 15:16:45 -04005842 spin_lock_irq(shost->host_lock);
5843 ndlp->nlp_flag |= NLP_DELAY_TMO;
5844 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05005845 ndlp->nlp_last_elscmd = ELS_CMD_FDISC;
5846 vport->port_state = LPFC_FDISC;
5847 } else {
James Smartecfd03c2010-02-12 14:41:27 -05005848 /*
5849 * Otherwise, we request port to rediscover
5850 * the entire FCF table for a fast recovery
5851 * from possible case that the current FCF
James Smart0c9ab6f2010-02-26 14:15:57 -05005852 * is no longer valid if we are not already
5853 * in the FCF failover process.
James Smartecfd03c2010-02-12 14:41:27 -05005854 */
James Smartfc2b9892010-02-26 14:15:29 -05005855 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05005856 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smartfc2b9892010-02-26 14:15:29 -05005857 spin_unlock_irq(&phba->hbalock);
5858 break;
5859 }
5860 /* Mark the fast failover process in progress */
James Smart0c9ab6f2010-02-26 14:15:57 -05005861 phba->fcf.fcf_flag |= FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05005862 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05005863 lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
5864 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04005865 "2773 Start FCF failover per CVL, "
James Smart70f3c072010-12-15 17:57:33 -05005866 "evt_tag:x%x\n", acqe_fip->event_tag);
James Smartecfd03c2010-02-12 14:41:27 -05005867 rc = lpfc_sli4_redisc_fcf_table(phba);
James Smartfc2b9892010-02-26 14:15:29 -05005868 if (rc) {
James Smart0c9ab6f2010-02-26 14:15:57 -05005869 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
Dick Kennedy372c1872020-06-30 14:50:00 -07005870 LOG_TRACE_EVENT,
James Smart0c9ab6f2010-02-26 14:15:57 -05005871 "2774 Issue FCF rediscover "
Colin Ian King7afc0ce2018-05-03 10:26:12 +01005872 "mailbox command failed, "
James Smart0c9ab6f2010-02-26 14:15:57 -05005873 "through to CVL event\n");
James Smartfc2b9892010-02-26 14:15:29 -05005874 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05005875 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05005876 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05005877 /*
5878 * Last resort will be re-try on the
5879 * the current registered FCF entry.
5880 */
5881 lpfc_retry_pport_discovery(phba);
James Smart38b92ef2010-08-04 16:11:39 -04005882 } else
5883 /*
5884 * Reset FCF roundrobin bmask for new
5885 * discovery.
5886 */
James Smart7d791df2011-07-22 18:37:52 -04005887 lpfc_sli4_clear_fcf_rr_bmask(phba);
James Smart6669f9b2009-10-02 15:16:45 -04005888 }
5889 break;
James Smartda0436e2009-05-22 14:51:39 -04005890 default:
Dick Kennedy372c1872020-06-30 14:50:00 -07005891 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
5892 "0288 Unknown FCoE event type 0x%x event tag "
5893 "0x%x\n", event_type, acqe_fip->event_tag);
James Smartda0436e2009-05-22 14:51:39 -04005894 break;
5895 }
5896}
5897
5898/**
5899 * lpfc_sli4_async_dcbx_evt - Process the asynchronous dcbx event
5900 * @phba: pointer to lpfc hba data structure.
Lee Jonesfe614ac2020-07-23 13:24:22 +01005901 * @acqe_dcbx: pointer to the async dcbx completion queue entry.
James Smartda0436e2009-05-22 14:51:39 -04005902 *
5903 * This routine is to handle the SLI4 asynchronous dcbx event.
5904 **/
5905static void
5906lpfc_sli4_async_dcbx_evt(struct lpfc_hba *phba,
5907 struct lpfc_acqe_dcbx *acqe_dcbx)
5908{
James Smart4d9ab992009-10-02 15:16:39 -04005909 phba->fc_eventTag = acqe_dcbx->event_tag;
Dick Kennedy372c1872020-06-30 14:50:00 -07005910 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartda0436e2009-05-22 14:51:39 -04005911 "0290 The SLI4 DCBX asynchronous event is not "
5912 "handled yet\n");
5913}
5914
5915/**
James Smartb19a0612010-04-06 14:48:51 -04005916 * lpfc_sli4_async_grp5_evt - Process the asynchronous group5 event
5917 * @phba: pointer to lpfc hba data structure.
Lee Jonesfe614ac2020-07-23 13:24:22 +01005918 * @acqe_grp5: pointer to the async grp5 completion queue entry.
James Smartb19a0612010-04-06 14:48:51 -04005919 *
5920 * This routine is to handle the SLI4 asynchronous grp5 event. A grp5 event
5921 * is an asynchronous notified of a logical link speed change. The Port
5922 * reports the logical link speed in units of 10Mbps.
5923 **/
5924static void
5925lpfc_sli4_async_grp5_evt(struct lpfc_hba *phba,
5926 struct lpfc_acqe_grp5 *acqe_grp5)
5927{
5928 uint16_t prev_ll_spd;
5929
5930 phba->fc_eventTag = acqe_grp5->event_tag;
5931 phba->fcoe_eventtag = acqe_grp5->event_tag;
5932 prev_ll_spd = phba->sli4_hba.link_state.logical_speed;
5933 phba->sli4_hba.link_state.logical_speed =
James Smart8b68cd52012-09-29 11:32:37 -04005934 (bf_get(lpfc_acqe_grp5_llink_spd, acqe_grp5)) * 10;
James Smartb19a0612010-04-06 14:48:51 -04005935 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5936 "2789 GRP5 Async Event: Updating logical link speed "
James Smart8b68cd52012-09-29 11:32:37 -04005937 "from %dMbps to %dMbps\n", prev_ll_spd,
5938 phba->sli4_hba.link_state.logical_speed);
James Smartb19a0612010-04-06 14:48:51 -04005939}
5940
5941/**
James Smartda0436e2009-05-22 14:51:39 -04005942 * lpfc_sli4_async_event_proc - Process all the pending asynchronous event
5943 * @phba: pointer to lpfc hba data structure.
5944 *
5945 * This routine is invoked by the worker thread to process all the pending
5946 * SLI4 asynchronous events.
5947 **/
5948void lpfc_sli4_async_event_proc(struct lpfc_hba *phba)
5949{
5950 struct lpfc_cq_event *cq_event;
5951
5952 /* First, declare the async event has been handled */
5953 spin_lock_irq(&phba->hbalock);
5954 phba->hba_flag &= ~ASYNC_EVENT;
5955 spin_unlock_irq(&phba->hbalock);
5956 /* Now, handle all the async events */
5957 while (!list_empty(&phba->sli4_hba.sp_asynce_work_queue)) {
5958 /* Get the first event from the head of the event queue */
5959 spin_lock_irq(&phba->hbalock);
5960 list_remove_head(&phba->sli4_hba.sp_asynce_work_queue,
5961 cq_event, struct lpfc_cq_event, list);
5962 spin_unlock_irq(&phba->hbalock);
5963 /* Process the asynchronous event */
5964 switch (bf_get(lpfc_trailer_code, &cq_event->cqe.mcqe_cmpl)) {
5965 case LPFC_TRAILER_CODE_LINK:
5966 lpfc_sli4_async_link_evt(phba,
5967 &cq_event->cqe.acqe_link);
5968 break;
5969 case LPFC_TRAILER_CODE_FCOE:
James Smart70f3c072010-12-15 17:57:33 -05005970 lpfc_sli4_async_fip_evt(phba, &cq_event->cqe.acqe_fip);
James Smartda0436e2009-05-22 14:51:39 -04005971 break;
5972 case LPFC_TRAILER_CODE_DCBX:
5973 lpfc_sli4_async_dcbx_evt(phba,
5974 &cq_event->cqe.acqe_dcbx);
5975 break;
James Smartb19a0612010-04-06 14:48:51 -04005976 case LPFC_TRAILER_CODE_GRP5:
5977 lpfc_sli4_async_grp5_evt(phba,
5978 &cq_event->cqe.acqe_grp5);
5979 break;
James Smart70f3c072010-12-15 17:57:33 -05005980 case LPFC_TRAILER_CODE_FC:
5981 lpfc_sli4_async_fc_evt(phba, &cq_event->cqe.acqe_fc);
5982 break;
5983 case LPFC_TRAILER_CODE_SLI:
5984 lpfc_sli4_async_sli_evt(phba, &cq_event->cqe.acqe_sli);
5985 break;
James Smartda0436e2009-05-22 14:51:39 -04005986 default:
Dick Kennedy372c1872020-06-30 14:50:00 -07005987 lpfc_printf_log(phba, KERN_ERR,
5988 LOG_TRACE_EVENT,
Colin Ian King291c2542019-12-18 08:43:01 +00005989 "1804 Invalid asynchronous event code: "
James Smartda0436e2009-05-22 14:51:39 -04005990 "x%x\n", bf_get(lpfc_trailer_code,
5991 &cq_event->cqe.mcqe_cmpl));
5992 break;
5993 }
5994 /* Free the completion event processed to the free pool */
5995 lpfc_sli4_cq_event_release(phba, cq_event);
5996 }
5997}
5998
5999/**
James Smartecfd03c2010-02-12 14:41:27 -05006000 * lpfc_sli4_fcf_redisc_event_proc - Process fcf table rediscovery event
6001 * @phba: pointer to lpfc hba data structure.
6002 *
6003 * This routine is invoked by the worker thread to process FCF table
6004 * rediscovery pending completion event.
6005 **/
6006void lpfc_sli4_fcf_redisc_event_proc(struct lpfc_hba *phba)
6007{
6008 int rc;
6009
6010 spin_lock_irq(&phba->hbalock);
6011 /* Clear FCF rediscovery timeout event */
6012 phba->fcf.fcf_flag &= ~FCF_REDISC_EVT;
6013 /* Clear driver fast failover FCF record flag */
6014 phba->fcf.failover_rec.flag = 0;
6015 /* Set state for FCF fast failover */
6016 phba->fcf.fcf_flag |= FCF_REDISC_FOV;
6017 spin_unlock_irq(&phba->hbalock);
6018
6019 /* Scan FCF table from the first entry to re-discover SAN */
James Smart0c9ab6f2010-02-26 14:15:57 -05006020 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04006021 "2777 Start post-quiescent FCF table scan\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05006022 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
James Smartecfd03c2010-02-12 14:41:27 -05006023 if (rc)
Dick Kennedy372c1872020-06-30 14:50:00 -07006024 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart0c9ab6f2010-02-26 14:15:57 -05006025 "2747 Issue FCF scan read FCF mailbox "
6026 "command failed 0x%x\n", rc);
James Smartecfd03c2010-02-12 14:41:27 -05006027}
6028
6029/**
James Smartda0436e2009-05-22 14:51:39 -04006030 * lpfc_api_table_setup - Set up per hba pci-device group func api jump table
6031 * @phba: pointer to lpfc hba data structure.
6032 * @dev_grp: The HBA PCI-Device group number.
6033 *
6034 * This routine is invoked to set up the per HBA PCI-Device group function
6035 * API jump table entries.
6036 *
6037 * Return: 0 if success, otherwise -ENODEV
6038 **/
6039int
6040lpfc_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
James Smart5b75da22008-12-04 22:39:35 -05006041{
6042 int rc;
6043
James Smartda0436e2009-05-22 14:51:39 -04006044 /* Set up lpfc PCI-device group */
6045 phba->pci_dev_grp = dev_grp;
James Smart5b75da22008-12-04 22:39:35 -05006046
James Smartda0436e2009-05-22 14:51:39 -04006047 /* The LPFC_PCI_DEV_OC uses SLI4 */
6048 if (dev_grp == LPFC_PCI_DEV_OC)
6049 phba->sli_rev = LPFC_SLI_REV4;
James Smart5b75da22008-12-04 22:39:35 -05006050
James Smartda0436e2009-05-22 14:51:39 -04006051 /* Set up device INIT API function jump table */
6052 rc = lpfc_init_api_table_setup(phba, dev_grp);
6053 if (rc)
6054 return -ENODEV;
6055 /* Set up SCSI API function jump table */
6056 rc = lpfc_scsi_api_table_setup(phba, dev_grp);
6057 if (rc)
6058 return -ENODEV;
6059 /* Set up SLI API function jump table */
6060 rc = lpfc_sli_api_table_setup(phba, dev_grp);
6061 if (rc)
6062 return -ENODEV;
6063 /* Set up MBOX API function jump table */
6064 rc = lpfc_mbox_api_table_setup(phba, dev_grp);
6065 if (rc)
6066 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05006067
James Smartda0436e2009-05-22 14:51:39 -04006068 return 0;
James Smart5b75da22008-12-04 22:39:35 -05006069}
6070
6071/**
James Smart3621a712009-04-06 18:47:14 -04006072 * lpfc_log_intr_mode - Log the active interrupt mode
James Smart5b75da22008-12-04 22:39:35 -05006073 * @phba: pointer to lpfc hba data structure.
6074 * @intr_mode: active interrupt mode adopted.
6075 *
6076 * This routine it invoked to log the currently used active interrupt mode
6077 * to the device.
James Smart3772a992009-05-22 14:50:54 -04006078 **/
6079static void lpfc_log_intr_mode(struct lpfc_hba *phba, uint32_t intr_mode)
James Smart5b75da22008-12-04 22:39:35 -05006080{
6081 switch (intr_mode) {
6082 case 0:
6083 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6084 "0470 Enable INTx interrupt mode.\n");
6085 break;
6086 case 1:
6087 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6088 "0481 Enabled MSI interrupt mode.\n");
6089 break;
6090 case 2:
6091 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6092 "0480 Enabled MSI-X interrupt mode.\n");
6093 break;
6094 default:
Dick Kennedy372c1872020-06-30 14:50:00 -07006095 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart5b75da22008-12-04 22:39:35 -05006096 "0482 Illegal interrupt mode.\n");
6097 break;
6098 }
6099 return;
6100}
6101
James Smart5b75da22008-12-04 22:39:35 -05006102/**
James Smart3772a992009-05-22 14:50:54 -04006103 * lpfc_enable_pci_dev - Enable a generic PCI device.
James Smart5b75da22008-12-04 22:39:35 -05006104 * @phba: pointer to lpfc hba data structure.
6105 *
James Smart3772a992009-05-22 14:50:54 -04006106 * This routine is invoked to enable the PCI device that is common to all
6107 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05006108 *
6109 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006110 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04006111 * other values - error
James Smart5b75da22008-12-04 22:39:35 -05006112 **/
James Smart3772a992009-05-22 14:50:54 -04006113static int
6114lpfc_enable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05006115{
James Smart3772a992009-05-22 14:50:54 -04006116 struct pci_dev *pdev;
James Smart5b75da22008-12-04 22:39:35 -05006117
James Smart3772a992009-05-22 14:50:54 -04006118 /* Obtain PCI device reference */
6119 if (!phba->pcidev)
6120 goto out_error;
6121 else
6122 pdev = phba->pcidev;
James Smart3772a992009-05-22 14:50:54 -04006123 /* Enable PCI device */
6124 if (pci_enable_device_mem(pdev))
6125 goto out_error;
6126 /* Request PCI resource for the device */
Johannes Thumshirne0c04832016-06-07 09:44:03 +02006127 if (pci_request_mem_regions(pdev, LPFC_DRIVER_NAME))
James Smart3772a992009-05-22 14:50:54 -04006128 goto out_disable_device;
6129 /* Set up device as PCI master and save state for EEH */
6130 pci_set_master(pdev);
6131 pci_try_set_mwi(pdev);
6132 pci_save_state(pdev);
James Smart5b75da22008-12-04 22:39:35 -05006133
James Smart05580562011-05-24 11:40:48 -04006134 /* PCIe EEH recovery on powerpc platforms needs fundamental reset */
Jon Mason453193e2013-09-11 15:38:13 -07006135 if (pci_is_pcie(pdev))
James Smart05580562011-05-24 11:40:48 -04006136 pdev->needs_freset = 1;
6137
James Smart3772a992009-05-22 14:50:54 -04006138 return 0;
James Smart5b75da22008-12-04 22:39:35 -05006139
James Smart3772a992009-05-22 14:50:54 -04006140out_disable_device:
6141 pci_disable_device(pdev);
6142out_error:
Dick Kennedy372c1872020-06-30 14:50:00 -07006143 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
Johannes Thumshirne0c04832016-06-07 09:44:03 +02006144 "1401 Failed to enable pci device\n");
James Smart3772a992009-05-22 14:50:54 -04006145 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05006146}
6147
6148/**
James Smart3772a992009-05-22 14:50:54 -04006149 * lpfc_disable_pci_dev - Disable a generic PCI device.
James Smart5b75da22008-12-04 22:39:35 -05006150 * @phba: pointer to lpfc hba data structure.
6151 *
James Smart3772a992009-05-22 14:50:54 -04006152 * This routine is invoked to disable the PCI device that is common to all
6153 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05006154 **/
6155static void
James Smart3772a992009-05-22 14:50:54 -04006156lpfc_disable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05006157{
James Smart3772a992009-05-22 14:50:54 -04006158 struct pci_dev *pdev;
James Smart5b75da22008-12-04 22:39:35 -05006159
James Smart3772a992009-05-22 14:50:54 -04006160 /* Obtain PCI device reference */
6161 if (!phba->pcidev)
6162 return;
6163 else
6164 pdev = phba->pcidev;
James Smart3772a992009-05-22 14:50:54 -04006165 /* Release PCI resource and disable PCI device */
Johannes Thumshirne0c04832016-06-07 09:44:03 +02006166 pci_release_mem_regions(pdev);
James Smart3772a992009-05-22 14:50:54 -04006167 pci_disable_device(pdev);
James Smart5b75da22008-12-04 22:39:35 -05006168
6169 return;
6170}
6171
6172/**
James Smart3772a992009-05-22 14:50:54 -04006173 * lpfc_reset_hba - Reset a hba
6174 * @phba: pointer to lpfc hba data structure.
James Smarte59058c2008-08-24 21:49:00 -04006175 *
James Smart3772a992009-05-22 14:50:54 -04006176 * This routine is invoked to reset a hba device. It brings the HBA
6177 * offline, performs a board restart, and then brings the board back
6178 * online. The lpfc_offline calls lpfc_sli_hba_down which will clean up
6179 * on outstanding mailbox commands.
James Smarte59058c2008-08-24 21:49:00 -04006180 **/
James Smart3772a992009-05-22 14:50:54 -04006181void
6182lpfc_reset_hba(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05006183{
James Smart3772a992009-05-22 14:50:54 -04006184 /* If resets are disabled then set error state and return. */
6185 if (!phba->cfg_enable_hba_reset) {
6186 phba->link_state = LPFC_HBA_ERROR;
6187 return;
6188 }
James Smartee620212014-04-04 13:51:53 -04006189 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
6190 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
6191 else
6192 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
James Smart3772a992009-05-22 14:50:54 -04006193 lpfc_offline(phba);
6194 lpfc_sli_brdrestart(phba);
6195 lpfc_online(phba);
6196 lpfc_unblock_mgmt_io(phba);
6197}
dea31012005-04-17 16:05:31 -05006198
James Smart3772a992009-05-22 14:50:54 -04006199/**
James Smart0a96e972011-07-22 18:37:28 -04006200 * lpfc_sli_sriov_nr_virtfn_get - Get the number of sr-iov virtual functions
6201 * @phba: pointer to lpfc hba data structure.
6202 *
6203 * This function enables the PCI SR-IOV virtual functions to a physical
6204 * function. It invokes the PCI SR-IOV api with the @nr_vfn provided to
6205 * enable the number of virtual functions to the physical function. As
6206 * not all devices support SR-IOV, the return code from the pci_enable_sriov()
6207 * API call does not considered as an error condition for most of the device.
6208 **/
6209uint16_t
6210lpfc_sli_sriov_nr_virtfn_get(struct lpfc_hba *phba)
6211{
6212 struct pci_dev *pdev = phba->pcidev;
6213 uint16_t nr_virtfn;
6214 int pos;
6215
James Smart0a96e972011-07-22 18:37:28 -04006216 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
6217 if (pos == 0)
6218 return 0;
6219
6220 pci_read_config_word(pdev, pos + PCI_SRIOV_TOTAL_VF, &nr_virtfn);
6221 return nr_virtfn;
6222}
6223
6224/**
James Smart912e3ac2011-05-24 11:42:11 -04006225 * lpfc_sli_probe_sriov_nr_virtfn - Enable a number of sr-iov virtual functions
6226 * @phba: pointer to lpfc hba data structure.
6227 * @nr_vfn: number of virtual functions to be enabled.
6228 *
6229 * This function enables the PCI SR-IOV virtual functions to a physical
6230 * function. It invokes the PCI SR-IOV api with the @nr_vfn provided to
6231 * enable the number of virtual functions to the physical function. As
6232 * not all devices support SR-IOV, the return code from the pci_enable_sriov()
6233 * API call does not considered as an error condition for most of the device.
6234 **/
6235int
6236lpfc_sli_probe_sriov_nr_virtfn(struct lpfc_hba *phba, int nr_vfn)
6237{
6238 struct pci_dev *pdev = phba->pcidev;
James Smart0a96e972011-07-22 18:37:28 -04006239 uint16_t max_nr_vfn;
James Smart912e3ac2011-05-24 11:42:11 -04006240 int rc;
6241
James Smart0a96e972011-07-22 18:37:28 -04006242 max_nr_vfn = lpfc_sli_sriov_nr_virtfn_get(phba);
6243 if (nr_vfn > max_nr_vfn) {
Dick Kennedy372c1872020-06-30 14:50:00 -07006244 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart0a96e972011-07-22 18:37:28 -04006245 "3057 Requested vfs (%d) greater than "
6246 "supported vfs (%d)", nr_vfn, max_nr_vfn);
6247 return -EINVAL;
6248 }
6249
James Smart912e3ac2011-05-24 11:42:11 -04006250 rc = pci_enable_sriov(pdev, nr_vfn);
6251 if (rc) {
6252 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6253 "2806 Failed to enable sriov on this device "
6254 "with vfn number nr_vf:%d, rc:%d\n",
6255 nr_vfn, rc);
6256 } else
6257 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6258 "2807 Successful enable sriov on this device "
6259 "with vfn number nr_vf:%d\n", nr_vfn);
6260 return rc;
6261}
6262
6263/**
James Smart895427b2017-02-12 13:52:30 -08006264 * lpfc_setup_driver_resource_phase1 - Phase1 etup driver internal resources.
James Smart3772a992009-05-22 14:50:54 -04006265 * @phba: pointer to lpfc hba data structure.
6266 *
James Smart895427b2017-02-12 13:52:30 -08006267 * This routine is invoked to set up the driver internal resources before the
6268 * device specific resource setup to support the HBA device it attached to.
James Smart3772a992009-05-22 14:50:54 -04006269 *
6270 * Return codes
James Smart895427b2017-02-12 13:52:30 -08006271 * 0 - successful
6272 * other values - error
James Smart3772a992009-05-22 14:50:54 -04006273 **/
6274static int
James Smart895427b2017-02-12 13:52:30 -08006275lpfc_setup_driver_resource_phase1(struct lpfc_hba *phba)
James Smart3772a992009-05-22 14:50:54 -04006276{
James Smart895427b2017-02-12 13:52:30 -08006277 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05006278
James Smart3772a992009-05-22 14:50:54 -04006279 /*
James Smart895427b2017-02-12 13:52:30 -08006280 * Driver resources common to all SLI revisions
James Smart3772a992009-05-22 14:50:54 -04006281 */
James Smart895427b2017-02-12 13:52:30 -08006282 atomic_set(&phba->fast_event_count, 0);
Dick Kennedy372c1872020-06-30 14:50:00 -07006283 atomic_set(&phba->dbg_log_idx, 0);
6284 atomic_set(&phba->dbg_log_cnt, 0);
6285 atomic_set(&phba->dbg_log_dmping, 0);
James Smart895427b2017-02-12 13:52:30 -08006286 spin_lock_init(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006287
James Smart895427b2017-02-12 13:52:30 -08006288 /* Initialize ndlp management spinlock */
6289 spin_lock_init(&phba->ndlp_lock);
James Smart3772a992009-05-22 14:50:54 -04006290
James Smart523128e2018-09-10 10:30:46 -07006291 /* Initialize port_list spinlock */
6292 spin_lock_init(&phba->port_list_lock);
James Smart895427b2017-02-12 13:52:30 -08006293 INIT_LIST_HEAD(&phba->port_list);
James Smart523128e2018-09-10 10:30:46 -07006294
James Smart895427b2017-02-12 13:52:30 -08006295 INIT_LIST_HEAD(&phba->work_list);
6296 init_waitqueue_head(&phba->wait_4_mlo_m_q);
6297
6298 /* Initialize the wait queue head for the kernel thread */
6299 init_waitqueue_head(&phba->work_waitq);
6300
6301 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smartf358dd02017-02-12 13:52:34 -08006302 "1403 Protocols supported %s %s %s\n",
James Smart895427b2017-02-12 13:52:30 -08006303 ((phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) ?
6304 "SCSI" : " "),
6305 ((phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) ?
James Smartf358dd02017-02-12 13:52:34 -08006306 "NVME" : " "),
6307 (phba->nvmet_support ? "NVMET" : " "));
James Smart895427b2017-02-12 13:52:30 -08006308
James Smart0794d602019-01-28 11:14:19 -08006309 /* Initialize the IO buffer list used by driver for SLI3 SCSI */
6310 spin_lock_init(&phba->scsi_buf_list_get_lock);
6311 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_get);
6312 spin_lock_init(&phba->scsi_buf_list_put_lock);
6313 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
James Smart895427b2017-02-12 13:52:30 -08006314
6315 /* Initialize the fabric iocb list */
6316 INIT_LIST_HEAD(&phba->fabric_iocb_list);
6317
6318 /* Initialize list to save ELS buffers */
6319 INIT_LIST_HEAD(&phba->elsbuf);
6320
6321 /* Initialize FCF connection rec list */
6322 INIT_LIST_HEAD(&phba->fcf_conn_rec_list);
6323
6324 /* Initialize OAS configuration list */
6325 spin_lock_init(&phba->devicelock);
6326 INIT_LIST_HEAD(&phba->luns);
6327
James Smart3772a992009-05-22 14:50:54 -04006328 /* MBOX heartbeat timer */
Kees Cookf22eb4d2017-09-06 20:24:26 -07006329 timer_setup(&psli->mbox_tmo, lpfc_mbox_timeout, 0);
James Smart3772a992009-05-22 14:50:54 -04006330 /* Fabric block timer */
Kees Cookf22eb4d2017-09-06 20:24:26 -07006331 timer_setup(&phba->fabric_block_timer, lpfc_fabric_block_timeout, 0);
James Smart3772a992009-05-22 14:50:54 -04006332 /* EA polling mode timer */
Kees Cookf22eb4d2017-09-06 20:24:26 -07006333 timer_setup(&phba->eratt_poll, lpfc_poll_eratt, 0);
James Smart895427b2017-02-12 13:52:30 -08006334 /* Heartbeat timer */
Kees Cookf22eb4d2017-09-06 20:24:26 -07006335 timer_setup(&phba->hb_tmofunc, lpfc_hb_timeout, 0);
James Smart895427b2017-02-12 13:52:30 -08006336
James Smart32517fc2019-01-28 11:14:33 -08006337 INIT_DELAYED_WORK(&phba->eq_delay_work, lpfc_hb_eq_delay_work);
6338
Dick Kennedy317aeb82020-06-30 14:49:59 -07006339 INIT_DELAYED_WORK(&phba->idle_stat_delay_work,
6340 lpfc_idle_stat_delay_work);
6341
James Smart895427b2017-02-12 13:52:30 -08006342 return 0;
6343}
6344
6345/**
6346 * lpfc_sli_driver_resource_setup - Setup driver internal resources for SLI3 dev
6347 * @phba: pointer to lpfc hba data structure.
6348 *
6349 * This routine is invoked to set up the driver internal resources specific to
6350 * support the SLI-3 HBA device it attached to.
6351 *
6352 * Return codes
6353 * 0 - successful
6354 * other values - error
6355 **/
6356static int
6357lpfc_sli_driver_resource_setup(struct lpfc_hba *phba)
6358{
James Smart0794d602019-01-28 11:14:19 -08006359 int rc, entry_sz;
James Smart895427b2017-02-12 13:52:30 -08006360
6361 /*
6362 * Initialize timers used by driver
6363 */
6364
6365 /* FCP polling mode timer */
Kees Cookf22eb4d2017-09-06 20:24:26 -07006366 timer_setup(&phba->fcp_poll_timer, lpfc_poll_timeout, 0);
James Smart3772a992009-05-22 14:50:54 -04006367
6368 /* Host attention work mask setup */
6369 phba->work_ha_mask = (HA_ERATT | HA_MBATT | HA_LATT);
6370 phba->work_ha_mask |= (HA_RXMASK << (LPFC_ELS_RING * 4));
6371
6372 /* Get all the module params for configuring this host */
6373 lpfc_get_cfgparam(phba);
James Smart895427b2017-02-12 13:52:30 -08006374 /* Set up phase-1 common device driver resources */
6375
6376 rc = lpfc_setup_driver_resource_phase1(phba);
6377 if (rc)
6378 return -ENODEV;
6379
James Smart49198b32010-04-06 15:04:33 -04006380 if (phba->pcidev->device == PCI_DEVICE_ID_HORNET) {
6381 phba->menlo_flag |= HBA_MENLO_SUPPORT;
6382 /* check for menlo minimum sg count */
6383 if (phba->cfg_sg_seg_cnt < LPFC_DEFAULT_MENLO_SG_SEG_CNT)
6384 phba->cfg_sg_seg_cnt = LPFC_DEFAULT_MENLO_SG_SEG_CNT;
6385 }
6386
James Smart895427b2017-02-12 13:52:30 -08006387 if (!phba->sli.sli3_ring)
Kees Cook6396bb22018-06-12 14:03:40 -07006388 phba->sli.sli3_ring = kcalloc(LPFC_SLI3_MAX_RING,
6389 sizeof(struct lpfc_sli_ring),
6390 GFP_KERNEL);
James Smart895427b2017-02-12 13:52:30 -08006391 if (!phba->sli.sli3_ring)
James Smart2a76a282012-08-03 12:35:54 -04006392 return -ENOMEM;
6393
James Smart3772a992009-05-22 14:50:54 -04006394 /*
James Smart96f70772013-04-17 20:16:15 -04006395 * Since lpfc_sg_seg_cnt is module parameter, the sg_dma_buf_size
James Smart3772a992009-05-22 14:50:54 -04006396 * used to create the sg_dma_buf_pool must be dynamically calculated.
James Smart3772a992009-05-22 14:50:54 -04006397 */
James Smart3772a992009-05-22 14:50:54 -04006398
James Smart0794d602019-01-28 11:14:19 -08006399 if (phba->sli_rev == LPFC_SLI_REV4)
6400 entry_sz = sizeof(struct sli4_sge);
6401 else
6402 entry_sz = sizeof(struct ulp_bde64);
6403
James Smart96f70772013-04-17 20:16:15 -04006404 /* There are going to be 2 reserved BDEs: 1 FCP cmnd + 1 FCP rsp */
6405 if (phba->cfg_enable_bg) {
6406 /*
6407 * The scsi_buf for a T10-DIF I/O will hold the FCP cmnd,
6408 * the FCP rsp, and a BDE for each. Sice we have no control
6409 * over how many protection data segments the SCSI Layer
6410 * will hand us (ie: there could be one for every block
6411 * in the IO), we just allocate enough BDEs to accomidate
6412 * our max amount and we need to limit lpfc_sg_seg_cnt to
6413 * minimize the risk of running out.
6414 */
6415 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
6416 sizeof(struct fcp_rsp) +
James Smart0794d602019-01-28 11:14:19 -08006417 (LPFC_MAX_SG_SEG_CNT * entry_sz);
James Smart96f70772013-04-17 20:16:15 -04006418
6419 if (phba->cfg_sg_seg_cnt > LPFC_MAX_SG_SEG_CNT_DIF)
6420 phba->cfg_sg_seg_cnt = LPFC_MAX_SG_SEG_CNT_DIF;
6421
6422 /* Total BDEs in BPL for scsi_sg_list and scsi_sg_prot_list */
6423 phba->cfg_total_seg_cnt = LPFC_MAX_SG_SEG_CNT;
6424 } else {
6425 /*
6426 * The scsi_buf for a regular I/O will hold the FCP cmnd,
6427 * the FCP rsp, a BDE for each, and a BDE for up to
6428 * cfg_sg_seg_cnt data segments.
6429 */
6430 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
6431 sizeof(struct fcp_rsp) +
James Smart0794d602019-01-28 11:14:19 -08006432 ((phba->cfg_sg_seg_cnt + 2) * entry_sz);
James Smart96f70772013-04-17 20:16:15 -04006433
6434 /* Total BDEs in BPL for scsi_sg_list */
6435 phba->cfg_total_seg_cnt = phba->cfg_sg_seg_cnt + 2;
6436 }
6437
6438 lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_FCP,
James Smartc90b4482020-03-22 11:12:56 -07006439 "9088 INIT sg_tablesize:%d dmabuf_size:%d total_bde:%d\n",
James Smart96f70772013-04-17 20:16:15 -04006440 phba->cfg_sg_seg_cnt, phba->cfg_sg_dma_buf_size,
6441 phba->cfg_total_seg_cnt);
6442
James Smart3772a992009-05-22 14:50:54 -04006443 phba->max_vpi = LPFC_MAX_VPI;
6444 /* This will be set to correct value after config_port mbox */
6445 phba->max_vports = 0;
6446
6447 /*
6448 * Initialize the SLI Layer to run with lpfc HBAs.
6449 */
6450 lpfc_sli_setup(phba);
James Smart895427b2017-02-12 13:52:30 -08006451 lpfc_sli_queue_init(phba);
James Smart3772a992009-05-22 14:50:54 -04006452
6453 /* Allocate device driver memory */
6454 if (lpfc_mem_alloc(phba, BPL_ALIGN_SZ))
6455 return -ENOMEM;
6456
James Smartd79c9e92019-08-14 16:57:09 -07006457 phba->lpfc_sg_dma_buf_pool =
6458 dma_pool_create("lpfc_sg_dma_buf_pool",
6459 &phba->pcidev->dev, phba->cfg_sg_dma_buf_size,
6460 BPL_ALIGN_SZ, 0);
6461
6462 if (!phba->lpfc_sg_dma_buf_pool)
6463 goto fail_free_mem;
6464
6465 phba->lpfc_cmd_rsp_buf_pool =
6466 dma_pool_create("lpfc_cmd_rsp_buf_pool",
6467 &phba->pcidev->dev,
6468 sizeof(struct fcp_cmnd) +
6469 sizeof(struct fcp_rsp),
6470 BPL_ALIGN_SZ, 0);
6471
6472 if (!phba->lpfc_cmd_rsp_buf_pool)
6473 goto fail_free_dma_buf_pool;
6474
James Smart912e3ac2011-05-24 11:42:11 -04006475 /*
6476 * Enable sr-iov virtual functions if supported and configured
6477 * through the module parameter.
6478 */
6479 if (phba->cfg_sriov_nr_virtfn > 0) {
6480 rc = lpfc_sli_probe_sriov_nr_virtfn(phba,
6481 phba->cfg_sriov_nr_virtfn);
6482 if (rc) {
6483 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6484 "2808 Requested number of SR-IOV "
6485 "virtual functions (%d) is not "
6486 "supported\n",
6487 phba->cfg_sriov_nr_virtfn);
6488 phba->cfg_sriov_nr_virtfn = 0;
6489 }
6490 }
6491
James Smart3772a992009-05-22 14:50:54 -04006492 return 0;
James Smartd79c9e92019-08-14 16:57:09 -07006493
6494fail_free_dma_buf_pool:
6495 dma_pool_destroy(phba->lpfc_sg_dma_buf_pool);
6496 phba->lpfc_sg_dma_buf_pool = NULL;
6497fail_free_mem:
6498 lpfc_mem_free(phba);
6499 return -ENOMEM;
James Smart3772a992009-05-22 14:50:54 -04006500}
6501
6502/**
6503 * lpfc_sli_driver_resource_unset - Unset drvr internal resources for SLI3 dev
6504 * @phba: pointer to lpfc hba data structure.
6505 *
6506 * This routine is invoked to unset the driver internal resources set up
6507 * specific for supporting the SLI-3 HBA device it attached to.
6508 **/
6509static void
6510lpfc_sli_driver_resource_unset(struct lpfc_hba *phba)
6511{
6512 /* Free device driver memory allocated */
6513 lpfc_mem_free_all(phba);
6514
6515 return;
6516}
6517
6518/**
James Smartda0436e2009-05-22 14:51:39 -04006519 * lpfc_sli4_driver_resource_setup - Setup drvr internal resources for SLI4 dev
6520 * @phba: pointer to lpfc hba data structure.
6521 *
6522 * This routine is invoked to set up the driver internal resources specific to
6523 * support the SLI-4 HBA device it attached to.
6524 *
6525 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006526 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04006527 * other values - error
6528 **/
6529static int
6530lpfc_sli4_driver_resource_setup(struct lpfc_hba *phba)
6531{
James Smart28baac72010-02-12 14:42:03 -05006532 LPFC_MBOXQ_t *mboxq;
James Smartf358dd02017-02-12 13:52:34 -08006533 MAILBOX_t *mb;
James Smart895427b2017-02-12 13:52:30 -08006534 int rc, i, max_buf_size;
James Smart28baac72010-02-12 14:42:03 -05006535 uint8_t pn_page[LPFC_MAX_SUPPORTED_PAGES] = {0};
6536 struct lpfc_mqe *mqe;
James Smart09294d42013-04-17 20:16:05 -04006537 int longs;
James Smart81e6a632017-11-20 16:00:43 -08006538 int extra;
James Smartf358dd02017-02-12 13:52:34 -08006539 uint64_t wwn;
James Smartb92dc722018-05-24 21:09:01 -07006540 u32 if_type;
6541 u32 if_fam;
James Smartda0436e2009-05-22 14:51:39 -04006542
James Smart895427b2017-02-12 13:52:30 -08006543 phba->sli4_hba.num_present_cpu = lpfc_present_cpu;
James Smarteede4972019-11-21 09:55:56 -08006544 phba->sli4_hba.num_possible_cpu = cpumask_last(cpu_possible_mask) + 1;
James Smart895427b2017-02-12 13:52:30 -08006545 phba->sli4_hba.curr_disp_cpu = 0;
6546
James Smart716d3bc2013-09-06 12:18:28 -04006547 /* Get all the module params for configuring this host */
6548 lpfc_get_cfgparam(phba);
6549
James Smart895427b2017-02-12 13:52:30 -08006550 /* Set up phase-1 common device driver resources */
6551 rc = lpfc_setup_driver_resource_phase1(phba);
6552 if (rc)
6553 return -ENODEV;
6554
James Smartda0436e2009-05-22 14:51:39 -04006555 /* Before proceed, wait for POST done and device ready */
6556 rc = lpfc_sli4_post_status_check(phba);
6557 if (rc)
6558 return -ENODEV;
6559
James Smart3cee98d2019-08-14 16:56:34 -07006560 /* Allocate all driver workqueues here */
6561
6562 /* The lpfc_wq workqueue for deferred irq use */
6563 phba->wq = alloc_workqueue("lpfc_wq", WQ_MEM_RECLAIM, 0);
6564
James Smartda0436e2009-05-22 14:51:39 -04006565 /*
6566 * Initialize timers used by driver
6567 */
6568
Kees Cookf22eb4d2017-09-06 20:24:26 -07006569 timer_setup(&phba->rrq_tmr, lpfc_rrq_timeout, 0);
James Smartda0436e2009-05-22 14:51:39 -04006570
James Smartecfd03c2010-02-12 14:41:27 -05006571 /* FCF rediscover timer */
Kees Cookf22eb4d2017-09-06 20:24:26 -07006572 timer_setup(&phba->fcf.redisc_wait, lpfc_sli4_fcf_redisc_wait_tmo, 0);
James Smartecfd03c2010-02-12 14:41:27 -05006573
James Smartda0436e2009-05-22 14:51:39 -04006574 /*
James Smart7ad20aa2011-05-24 11:44:28 -04006575 * Control structure for handling external multi-buffer mailbox
6576 * command pass-through.
6577 */
6578 memset((uint8_t *)&phba->mbox_ext_buf_ctx, 0,
6579 sizeof(struct lpfc_mbox_ext_buf_ctx));
6580 INIT_LIST_HEAD(&phba->mbox_ext_buf_ctx.ext_dmabuf_list);
6581
James Smartda0436e2009-05-22 14:51:39 -04006582 phba->max_vpi = LPFC_MAX_VPI;
James Smart67d12732012-08-03 12:36:13 -04006583
James Smartda0436e2009-05-22 14:51:39 -04006584 /* This will be set to correct value after the read_config mbox */
6585 phba->max_vports = 0;
6586
6587 /* Program the default value of vlan_id and fc_map */
6588 phba->valid_vlan = 0;
6589 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
6590 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
6591 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
6592
6593 /*
James Smart2a76a282012-08-03 12:35:54 -04006594 * For SLI4, instead of using ring 0 (LPFC_FCP_RING) for FCP commands
James Smart895427b2017-02-12 13:52:30 -08006595 * we will associate a new ring, for each EQ/CQ/WQ tuple.
6596 * The WQ create will allocate the ring.
James Smart2a76a282012-08-03 12:35:54 -04006597 */
James Smart085c6472010-11-20 23:11:37 -05006598
James Smartda0436e2009-05-22 14:51:39 -04006599 /* Initialize buffer queue management fields */
James Smart895427b2017-02-12 13:52:30 -08006600 INIT_LIST_HEAD(&phba->hbqs[LPFC_ELS_HBQ].hbq_buffer_list);
James Smartda0436e2009-05-22 14:51:39 -04006601 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_sli4_rb_alloc;
6602 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_sli4_rb_free;
6603
6604 /*
6605 * Initialize the SLI Layer to run with lpfc SLI4 HBAs.
6606 */
James Smartc00f62e2019-08-14 16:57:11 -07006607 /* Initialize the Abort buffer list used by driver */
6608 spin_lock_init(&phba->sli4_hba.abts_io_buf_list_lock);
6609 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_io_buf_list);
James Smart895427b2017-02-12 13:52:30 -08006610
6611 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
6612 /* Initialize the Abort nvme buffer list used by driver */
James Smart5e5b5112019-01-28 11:14:22 -08006613 spin_lock_init(&phba->sli4_hba.abts_nvmet_buf_list_lock);
James Smart86c67372017-04-21 16:05:04 -07006614 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_nvmet_ctx_list);
James Smarta8cf5df2017-05-15 15:20:46 -07006615 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_nvmet_io_wait_list);
James Smart79d8c4c2019-05-21 17:48:56 -07006616 spin_lock_init(&phba->sli4_hba.t_active_list_lock);
6617 INIT_LIST_HEAD(&phba->sli4_hba.t_active_ctx_list);
James Smart895427b2017-02-12 13:52:30 -08006618 }
6619
James Smartda0436e2009-05-22 14:51:39 -04006620 /* This abort list used by worker thread */
James Smart895427b2017-02-12 13:52:30 -08006621 spin_lock_init(&phba->sli4_hba.sgl_list_lock);
James Smarta8cf5df2017-05-15 15:20:46 -07006622 spin_lock_init(&phba->sli4_hba.nvmet_io_wait_lock);
James Smartda0436e2009-05-22 14:51:39 -04006623
6624 /*
James Smart6d368e52011-05-24 11:44:12 -04006625 * Initialize driver internal slow-path work queues
James Smartda0436e2009-05-22 14:51:39 -04006626 */
6627
6628 /* Driver internel slow-path CQ Event pool */
6629 INIT_LIST_HEAD(&phba->sli4_hba.sp_cqe_event_pool);
6630 /* Response IOCB work queue list */
James Smart45ed1192009-10-02 15:17:02 -04006631 INIT_LIST_HEAD(&phba->sli4_hba.sp_queue_event);
James Smartda0436e2009-05-22 14:51:39 -04006632 /* Asynchronous event CQ Event work queue list */
6633 INIT_LIST_HEAD(&phba->sli4_hba.sp_asynce_work_queue);
6634 /* Fast-path XRI aborted CQ Event work queue list */
6635 INIT_LIST_HEAD(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
6636 /* Slow-path XRI aborted CQ Event work queue list */
6637 INIT_LIST_HEAD(&phba->sli4_hba.sp_els_xri_aborted_work_queue);
6638 /* Receive queue CQ Event work queue list */
6639 INIT_LIST_HEAD(&phba->sli4_hba.sp_unsol_work_queue);
6640
James Smart6d368e52011-05-24 11:44:12 -04006641 /* Initialize extent block lists. */
6642 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_blk_list);
6643 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_xri_blk_list);
6644 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_vfi_blk_list);
6645 INIT_LIST_HEAD(&phba->lpfc_vpi_blk_list);
6646
James Smartd1f525a2017-04-21 16:04:55 -07006647 /* Initialize mboxq lists. If the early init routines fail
6648 * these lists need to be correctly initialized.
6649 */
6650 INIT_LIST_HEAD(&phba->sli.mboxq);
6651 INIT_LIST_HEAD(&phba->sli.mboxq_cmpl);
6652
James Smart448193b2015-12-16 18:12:05 -05006653 /* initialize optic_state to 0xFF */
6654 phba->sli4_hba.lnk_info.optic_state = 0xff;
6655
James Smartda0436e2009-05-22 14:51:39 -04006656 /* Allocate device driver memory */
6657 rc = lpfc_mem_alloc(phba, SGL_ALIGN_SZ);
6658 if (rc)
6659 return -ENOMEM;
6660
James Smart2fcee4b2010-12-15 17:57:46 -05006661 /* IF Type 2 ports get initialized now. */
James Smart27d6ac02018-02-22 08:18:42 -08006662 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) >=
James Smart2fcee4b2010-12-15 17:57:46 -05006663 LPFC_SLI_INTF_IF_TYPE_2) {
6664 rc = lpfc_pci_function_reset(phba);
James Smart895427b2017-02-12 13:52:30 -08006665 if (unlikely(rc)) {
6666 rc = -ENODEV;
6667 goto out_free_mem;
6668 }
James Smart946727d2015-04-07 15:07:09 -04006669 phba->temp_sensor_support = 1;
James Smart2fcee4b2010-12-15 17:57:46 -05006670 }
6671
James Smartda0436e2009-05-22 14:51:39 -04006672 /* Create the bootstrap mailbox command */
6673 rc = lpfc_create_bootstrap_mbox(phba);
6674 if (unlikely(rc))
6675 goto out_free_mem;
6676
6677 /* Set up the host's endian order with the device. */
6678 rc = lpfc_setup_endian_order(phba);
6679 if (unlikely(rc))
6680 goto out_free_bsmbx;
6681
6682 /* Set up the hba's configuration parameters. */
6683 rc = lpfc_sli4_read_config(phba);
6684 if (unlikely(rc))
6685 goto out_free_bsmbx;
James Smartcff261f2013-12-17 20:29:47 -05006686 rc = lpfc_mem_alloc_active_rrq_pool_s4(phba);
6687 if (unlikely(rc))
6688 goto out_free_bsmbx;
James Smartda0436e2009-05-22 14:51:39 -04006689
James Smart2fcee4b2010-12-15 17:57:46 -05006690 /* IF Type 0 ports get initialized now. */
6691 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
6692 LPFC_SLI_INTF_IF_TYPE_0) {
6693 rc = lpfc_pci_function_reset(phba);
6694 if (unlikely(rc))
6695 goto out_free_bsmbx;
6696 }
James Smartda0436e2009-05-22 14:51:39 -04006697
James Smartcb5172e2010-03-15 11:25:07 -04006698 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
6699 GFP_KERNEL);
6700 if (!mboxq) {
6701 rc = -ENOMEM;
6702 goto out_free_bsmbx;
6703 }
6704
James Smartf358dd02017-02-12 13:52:34 -08006705 /* Check for NVMET being configured */
James Smart895427b2017-02-12 13:52:30 -08006706 phba->nvmet_support = 0;
James Smartf358dd02017-02-12 13:52:34 -08006707 if (lpfc_enable_nvmet_cnt) {
6708
6709 /* First get WWN of HBA instance */
6710 lpfc_read_nv(phba, mboxq);
6711 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6712 if (rc != MBX_SUCCESS) {
Dick Kennedy372c1872020-06-30 14:50:00 -07006713 lpfc_printf_log(phba, KERN_ERR,
6714 LOG_TRACE_EVENT,
James Smartf358dd02017-02-12 13:52:34 -08006715 "6016 Mailbox failed , mbxCmd x%x "
6716 "READ_NV, mbxStatus x%x\n",
6717 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
6718 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
James Smartd1f525a2017-04-21 16:04:55 -07006719 mempool_free(mboxq, phba->mbox_mem_pool);
James Smartf358dd02017-02-12 13:52:34 -08006720 rc = -EIO;
6721 goto out_free_bsmbx;
6722 }
6723 mb = &mboxq->u.mb;
6724 memcpy(&wwn, (char *)mb->un.varRDnvp.nodename,
6725 sizeof(uint64_t));
6726 wwn = cpu_to_be64(wwn);
6727 phba->sli4_hba.wwnn.u.name = wwn;
6728 memcpy(&wwn, (char *)mb->un.varRDnvp.portname,
6729 sizeof(uint64_t));
6730 /* wwn is WWPN of HBA instance */
6731 wwn = cpu_to_be64(wwn);
6732 phba->sli4_hba.wwpn.u.name = wwn;
6733
6734 /* Check to see if it matches any module parameter */
6735 for (i = 0; i < lpfc_enable_nvmet_cnt; i++) {
6736 if (wwn == lpfc_enable_nvmet[i]) {
James Smart7d708032017-03-08 14:36:01 -08006737#if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
James Smart3c603be2017-05-15 15:20:44 -07006738 if (lpfc_nvmet_mem_alloc(phba))
6739 break;
6740
6741 phba->nvmet_support = 1; /* a match */
6742
Dick Kennedy372c1872020-06-30 14:50:00 -07006743 lpfc_printf_log(phba, KERN_ERR,
6744 LOG_TRACE_EVENT,
James Smartf358dd02017-02-12 13:52:34 -08006745 "6017 NVME Target %016llx\n",
6746 wwn);
James Smart7d708032017-03-08 14:36:01 -08006747#else
Dick Kennedy372c1872020-06-30 14:50:00 -07006748 lpfc_printf_log(phba, KERN_ERR,
6749 LOG_TRACE_EVENT,
James Smart7d708032017-03-08 14:36:01 -08006750 "6021 Can't enable NVME Target."
6751 " NVME_TARGET_FC infrastructure"
6752 " is not in kernel\n");
6753#endif
James Smartc4908502019-01-28 11:14:28 -08006754 /* Not supported for NVMET */
6755 phba->cfg_xri_rebalancing = 0;
Dick Kennedy3048e3e2020-05-01 14:43:06 -07006756 if (phba->irq_chann_mode == NHT_MODE) {
6757 phba->cfg_irq_chann =
6758 phba->sli4_hba.num_present_cpu;
6759 phba->cfg_hdw_queue =
6760 phba->sli4_hba.num_present_cpu;
6761 phba->irq_chann_mode = NORMAL_MODE;
6762 }
James Smart3c603be2017-05-15 15:20:44 -07006763 break;
James Smartf358dd02017-02-12 13:52:34 -08006764 }
6765 }
6766 }
James Smart895427b2017-02-12 13:52:30 -08006767
6768 lpfc_nvme_mod_param_dep(phba);
6769
James Smartfedd3b72011-02-16 12:39:24 -05006770 /* Get the Supported Pages if PORT_CAPABILITIES is supported by port. */
James Smartcb5172e2010-03-15 11:25:07 -04006771 lpfc_supported_pages(mboxq);
6772 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smartfedd3b72011-02-16 12:39:24 -05006773 if (!rc) {
6774 mqe = &mboxq->u.mqe;
6775 memcpy(&pn_page[0], ((uint8_t *)&mqe->un.supp_pages.word3),
6776 LPFC_MAX_SUPPORTED_PAGES);
6777 for (i = 0; i < LPFC_MAX_SUPPORTED_PAGES; i++) {
6778 switch (pn_page[i]) {
6779 case LPFC_SLI4_PARAMETERS:
6780 phba->sli4_hba.pc_sli4_params.supported = 1;
6781 break;
6782 default:
6783 break;
6784 }
6785 }
6786 /* Read the port's SLI4 Parameters capabilities if supported. */
6787 if (phba->sli4_hba.pc_sli4_params.supported)
6788 rc = lpfc_pc_sli4_params_get(phba, mboxq);
6789 if (rc) {
6790 mempool_free(mboxq, phba->mbox_mem_pool);
6791 rc = -EIO;
6792 goto out_free_bsmbx;
James Smartcb5172e2010-03-15 11:25:07 -04006793 }
6794 }
James Smart65791f12016-07-06 12:35:56 -07006795
James Smartfedd3b72011-02-16 12:39:24 -05006796 /*
6797 * Get sli4 parameters that override parameters from Port capabilities.
James Smart6d368e52011-05-24 11:44:12 -04006798 * If this call fails, it isn't critical unless the SLI4 parameters come
6799 * back in conflict.
James Smartfedd3b72011-02-16 12:39:24 -05006800 */
James Smart6d368e52011-05-24 11:44:12 -04006801 rc = lpfc_get_sli4_parameters(phba, mboxq);
6802 if (rc) {
James Smartb92dc722018-05-24 21:09:01 -07006803 if_type = bf_get(lpfc_sli_intf_if_type,
6804 &phba->sli4_hba.sli_intf);
6805 if_fam = bf_get(lpfc_sli_intf_sli_family,
6806 &phba->sli4_hba.sli_intf);
James Smart6d368e52011-05-24 11:44:12 -04006807 if (phba->sli4_hba.extents_in_use &&
6808 phba->sli4_hba.rpi_hdrs_in_use) {
Dick Kennedy372c1872020-06-30 14:50:00 -07006809 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6810 "2999 Unsupported SLI4 Parameters "
6811 "Extents and RPI headers enabled.\n");
James Smartb92dc722018-05-24 21:09:01 -07006812 if (if_type == LPFC_SLI_INTF_IF_TYPE_0 &&
6813 if_fam == LPFC_SLI_INTF_FAMILY_BE2) {
6814 mempool_free(mboxq, phba->mbox_mem_pool);
6815 rc = -EIO;
6816 goto out_free_bsmbx;
6817 }
James Smart6d368e52011-05-24 11:44:12 -04006818 }
James Smartb92dc722018-05-24 21:09:01 -07006819 if (!(if_type == LPFC_SLI_INTF_IF_TYPE_0 &&
6820 if_fam == LPFC_SLI_INTF_FAMILY_BE2)) {
6821 mempool_free(mboxq, phba->mbox_mem_pool);
6822 rc = -EIO;
6823 goto out_free_bsmbx;
6824 }
James Smart6d368e52011-05-24 11:44:12 -04006825 }
James Smart895427b2017-02-12 13:52:30 -08006826
James Smartd79c9e92019-08-14 16:57:09 -07006827 /*
6828 * 1 for cmd, 1 for rsp, NVME adds an extra one
6829 * for boundary conditions in its max_sgl_segment template.
6830 */
6831 extra = 2;
6832 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
6833 extra++;
6834
6835 /*
6836 * It doesn't matter what family our adapter is in, we are
6837 * limited to 2 Pages, 512 SGEs, for our SGL.
6838 * There are going to be 2 reserved SGEs: 1 FCP cmnd + 1 FCP rsp
6839 */
6840 max_buf_size = (2 * SLI4_PAGE_SIZE);
6841
6842 /*
6843 * Since lpfc_sg_seg_cnt is module param, the sg_dma_buf_size
6844 * used to create the sg_dma_buf_pool must be calculated.
6845 */
6846 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED) {
6847 /* Both cfg_enable_bg and cfg_external_dif code paths */
6848
6849 /*
6850 * The scsi_buf for a T10-DIF I/O holds the FCP cmnd,
6851 * the FCP rsp, and a SGE. Sice we have no control
6852 * over how many protection segments the SCSI Layer
6853 * will hand us (ie: there could be one for every block
6854 * in the IO), just allocate enough SGEs to accomidate
6855 * our max amount and we need to limit lpfc_sg_seg_cnt
6856 * to minimize the risk of running out.
6857 */
6858 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
6859 sizeof(struct fcp_rsp) + max_buf_size;
6860
6861 /* Total SGEs for scsi_sg_list and scsi_sg_prot_list */
6862 phba->cfg_total_seg_cnt = LPFC_MAX_SGL_SEG_CNT;
6863
6864 /*
6865 * If supporting DIF, reduce the seg count for scsi to
6866 * allow room for the DIF sges.
6867 */
6868 if (phba->cfg_enable_bg &&
6869 phba->cfg_sg_seg_cnt > LPFC_MAX_BG_SLI4_SEG_CNT_DIF)
6870 phba->cfg_scsi_seg_cnt = LPFC_MAX_BG_SLI4_SEG_CNT_DIF;
6871 else
6872 phba->cfg_scsi_seg_cnt = phba->cfg_sg_seg_cnt;
6873
6874 } else {
6875 /*
6876 * The scsi_buf for a regular I/O holds the FCP cmnd,
6877 * the FCP rsp, a SGE for each, and a SGE for up to
6878 * cfg_sg_seg_cnt data segments.
6879 */
6880 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
6881 sizeof(struct fcp_rsp) +
6882 ((phba->cfg_sg_seg_cnt + extra) *
6883 sizeof(struct sli4_sge));
6884
6885 /* Total SGEs for scsi_sg_list */
6886 phba->cfg_total_seg_cnt = phba->cfg_sg_seg_cnt + extra;
6887 phba->cfg_scsi_seg_cnt = phba->cfg_sg_seg_cnt;
6888
6889 /*
6890 * NOTE: if (phba->cfg_sg_seg_cnt + extra) <= 256 we only
6891 * need to post 1 page for the SGL.
6892 */
6893 }
6894
6895 if (phba->cfg_xpsgl && !phba->nvmet_support)
6896 phba->cfg_sg_dma_buf_size = LPFC_DEFAULT_XPSGL_SIZE;
6897 else if (phba->cfg_sg_dma_buf_size <= LPFC_MIN_SG_SLI4_BUF_SZ)
6898 phba->cfg_sg_dma_buf_size = LPFC_MIN_SG_SLI4_BUF_SZ;
6899 else
6900 phba->cfg_sg_dma_buf_size =
6901 SLI4_PAGE_ALIGN(phba->cfg_sg_dma_buf_size);
6902
6903 phba->border_sge_num = phba->cfg_sg_dma_buf_size /
6904 sizeof(struct sli4_sge);
6905
6906 /* Limit to LPFC_MAX_NVME_SEG_CNT for NVME. */
6907 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
6908 if (phba->cfg_sg_seg_cnt > LPFC_MAX_NVME_SEG_CNT) {
6909 lpfc_printf_log(phba, KERN_INFO, LOG_NVME | LOG_INIT,
6910 "6300 Reducing NVME sg segment "
6911 "cnt to %d\n",
6912 LPFC_MAX_NVME_SEG_CNT);
6913 phba->cfg_nvme_seg_cnt = LPFC_MAX_NVME_SEG_CNT;
6914 } else
6915 phba->cfg_nvme_seg_cnt = phba->cfg_sg_seg_cnt;
6916 }
6917
James Smartd79c9e92019-08-14 16:57:09 -07006918 lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_FCP,
6919 "9087 sg_seg_cnt:%d dmabuf_size:%d "
6920 "total:%d scsi:%d nvme:%d\n",
6921 phba->cfg_sg_seg_cnt, phba->cfg_sg_dma_buf_size,
6922 phba->cfg_total_seg_cnt, phba->cfg_scsi_seg_cnt,
6923 phba->cfg_nvme_seg_cnt);
6924
6925 if (phba->cfg_sg_dma_buf_size < SLI4_PAGE_SIZE)
6926 i = phba->cfg_sg_dma_buf_size;
6927 else
6928 i = SLI4_PAGE_SIZE;
6929
6930 phba->lpfc_sg_dma_buf_pool =
6931 dma_pool_create("lpfc_sg_dma_buf_pool",
6932 &phba->pcidev->dev,
6933 phba->cfg_sg_dma_buf_size,
6934 i, 0);
6935 if (!phba->lpfc_sg_dma_buf_pool)
6936 goto out_free_bsmbx;
6937
6938 phba->lpfc_cmd_rsp_buf_pool =
6939 dma_pool_create("lpfc_cmd_rsp_buf_pool",
6940 &phba->pcidev->dev,
6941 sizeof(struct fcp_cmnd) +
6942 sizeof(struct fcp_rsp),
6943 i, 0);
6944 if (!phba->lpfc_cmd_rsp_buf_pool)
6945 goto out_free_sg_dma_buf;
6946
James Smartcb5172e2010-03-15 11:25:07 -04006947 mempool_free(mboxq, phba->mbox_mem_pool);
James Smart1ba981f2014-02-20 09:56:45 -05006948
6949 /* Verify OAS is supported */
6950 lpfc_sli4_oas_verify(phba);
James Smart1ba981f2014-02-20 09:56:45 -05006951
James Smartd2cc9bc2018-09-10 10:30:50 -07006952 /* Verify RAS support on adapter */
6953 lpfc_sli4_ras_init(phba);
6954
James Smart5350d872011-10-10 21:33:49 -04006955 /* Verify all the SLI4 queues */
6956 rc = lpfc_sli4_queue_verify(phba);
James Smartda0436e2009-05-22 14:51:39 -04006957 if (rc)
James Smartd79c9e92019-08-14 16:57:09 -07006958 goto out_free_cmd_rsp_buf;
James Smartda0436e2009-05-22 14:51:39 -04006959
6960 /* Create driver internal CQE event pool */
6961 rc = lpfc_sli4_cq_event_pool_create(phba);
6962 if (rc)
James Smartd79c9e92019-08-14 16:57:09 -07006963 goto out_free_cmd_rsp_buf;
James Smartda0436e2009-05-22 14:51:39 -04006964
James Smart8a9d2e82012-05-09 21:16:12 -04006965 /* Initialize sgl lists per host */
6966 lpfc_init_sgl_list(phba);
6967
6968 /* Allocate and initialize active sgl array */
James Smartda0436e2009-05-22 14:51:39 -04006969 rc = lpfc_init_active_sgl_array(phba);
6970 if (rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -07006971 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartda0436e2009-05-22 14:51:39 -04006972 "1430 Failed to initialize sgl list.\n");
James Smart8a9d2e82012-05-09 21:16:12 -04006973 goto out_destroy_cq_event_pool;
James Smartda0436e2009-05-22 14:51:39 -04006974 }
James Smartda0436e2009-05-22 14:51:39 -04006975 rc = lpfc_sli4_init_rpi_hdrs(phba);
6976 if (rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -07006977 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartda0436e2009-05-22 14:51:39 -04006978 "1432 Failed to initialize rpi headers.\n");
6979 goto out_free_active_sgl;
6980 }
6981
James Smarta93ff372010-10-22 11:06:08 -04006982 /* Allocate eligible FCF bmask memory for FCF roundrobin failover */
James Smart0c9ab6f2010-02-26 14:15:57 -05006983 longs = (LPFC_SLI4_FCF_TBL_INDX_MAX + BITS_PER_LONG - 1)/BITS_PER_LONG;
Kees Cook6396bb22018-06-12 14:03:40 -07006984 phba->fcf.fcf_rr_bmask = kcalloc(longs, sizeof(unsigned long),
James Smart0c9ab6f2010-02-26 14:15:57 -05006985 GFP_KERNEL);
6986 if (!phba->fcf.fcf_rr_bmask) {
Dick Kennedy372c1872020-06-30 14:50:00 -07006987 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart0c9ab6f2010-02-26 14:15:57 -05006988 "2759 Failed allocate memory for FCF round "
6989 "robin failover bmask\n");
James Smart05580562011-05-24 11:40:48 -04006990 rc = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -05006991 goto out_remove_rpi_hdrs;
6992 }
6993
James Smart6a828b02019-01-28 11:14:31 -08006994 phba->sli4_hba.hba_eq_hdl = kcalloc(phba->cfg_irq_chann,
James Smartcdb42be2019-01-28 11:14:21 -08006995 sizeof(struct lpfc_hba_eq_hdl),
6996 GFP_KERNEL);
James Smart895427b2017-02-12 13:52:30 -08006997 if (!phba->sli4_hba.hba_eq_hdl) {
Dick Kennedy372c1872020-06-30 14:50:00 -07006998 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart67d12732012-08-03 12:36:13 -04006999 "2572 Failed allocate memory for "
7000 "fast-path per-EQ handle array\n");
7001 rc = -ENOMEM;
7002 goto out_free_fcf_rr_bmask;
James Smartda0436e2009-05-22 14:51:39 -04007003 }
7004
James Smart222e9232019-01-28 11:14:35 -08007005 phba->sli4_hba.cpu_map = kcalloc(phba->sli4_hba.num_possible_cpu,
James Smart895427b2017-02-12 13:52:30 -08007006 sizeof(struct lpfc_vector_map_info),
7007 GFP_KERNEL);
James Smart7bb03bb2013-04-17 20:19:16 -04007008 if (!phba->sli4_hba.cpu_map) {
Dick Kennedy372c1872020-06-30 14:50:00 -07007009 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart7bb03bb2013-04-17 20:19:16 -04007010 "3327 Failed allocate memory for msi-x "
7011 "interrupt vector mapping\n");
7012 rc = -ENOMEM;
James Smart895427b2017-02-12 13:52:30 -08007013 goto out_free_hba_eq_hdl;
James Smart7bb03bb2013-04-17 20:19:16 -04007014 }
James Smartb246de12013-05-31 17:03:07 -04007015
James Smart32517fc2019-01-28 11:14:33 -08007016 phba->sli4_hba.eq_info = alloc_percpu(struct lpfc_eq_intr_info);
7017 if (!phba->sli4_hba.eq_info) {
Dick Kennedy372c1872020-06-30 14:50:00 -07007018 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart32517fc2019-01-28 11:14:33 -08007019 "3321 Failed allocation for per_cpu stats\n");
7020 rc = -ENOMEM;
7021 goto out_free_hba_cpu_map;
7022 }
James Smart840eda92020-03-22 11:13:00 -07007023
Dick Kennedy317aeb82020-06-30 14:49:59 -07007024 phba->sli4_hba.idle_stat = kcalloc(phba->sli4_hba.num_possible_cpu,
7025 sizeof(*phba->sli4_hba.idle_stat),
7026 GFP_KERNEL);
7027 if (!phba->sli4_hba.idle_stat) {
Dick Kennedy372c1872020-06-30 14:50:00 -07007028 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
Dick Kennedy317aeb82020-06-30 14:49:59 -07007029 "3390 Failed allocation for idle_stat\n");
7030 rc = -ENOMEM;
7031 goto out_free_hba_eq_info;
7032 }
7033
James Smart840eda92020-03-22 11:13:00 -07007034#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
7035 phba->sli4_hba.c_stat = alloc_percpu(struct lpfc_hdwq_stat);
7036 if (!phba->sli4_hba.c_stat) {
Dick Kennedy372c1872020-06-30 14:50:00 -07007037 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart840eda92020-03-22 11:13:00 -07007038 "3332 Failed allocating per cpu hdwq stats\n");
7039 rc = -ENOMEM;
Dick Kennedy317aeb82020-06-30 14:49:59 -07007040 goto out_free_hba_idle_stat;
James Smart840eda92020-03-22 11:13:00 -07007041 }
7042#endif
7043
James Smart912e3ac2011-05-24 11:42:11 -04007044 /*
7045 * Enable sr-iov virtual functions if supported and configured
7046 * through the module parameter.
7047 */
7048 if (phba->cfg_sriov_nr_virtfn > 0) {
7049 rc = lpfc_sli_probe_sriov_nr_virtfn(phba,
7050 phba->cfg_sriov_nr_virtfn);
7051 if (rc) {
7052 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7053 "3020 Requested number of SR-IOV "
7054 "virtual functions (%d) is not "
7055 "supported\n",
7056 phba->cfg_sriov_nr_virtfn);
7057 phba->cfg_sriov_nr_virtfn = 0;
7058 }
7059 }
7060
James Smart5248a742011-07-22 18:37:06 -04007061 return 0;
James Smartda0436e2009-05-22 14:51:39 -04007062
James Smart840eda92020-03-22 11:13:00 -07007063#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
Dick Kennedy317aeb82020-06-30 14:49:59 -07007064out_free_hba_idle_stat:
7065 kfree(phba->sli4_hba.idle_stat);
7066#endif
James Smart840eda92020-03-22 11:13:00 -07007067out_free_hba_eq_info:
7068 free_percpu(phba->sli4_hba.eq_info);
James Smart32517fc2019-01-28 11:14:33 -08007069out_free_hba_cpu_map:
7070 kfree(phba->sli4_hba.cpu_map);
James Smart895427b2017-02-12 13:52:30 -08007071out_free_hba_eq_hdl:
7072 kfree(phba->sli4_hba.hba_eq_hdl);
James Smart0c9ab6f2010-02-26 14:15:57 -05007073out_free_fcf_rr_bmask:
7074 kfree(phba->fcf.fcf_rr_bmask);
James Smartda0436e2009-05-22 14:51:39 -04007075out_remove_rpi_hdrs:
7076 lpfc_sli4_remove_rpi_hdrs(phba);
7077out_free_active_sgl:
7078 lpfc_free_active_sgl(phba);
James Smartda0436e2009-05-22 14:51:39 -04007079out_destroy_cq_event_pool:
7080 lpfc_sli4_cq_event_pool_destroy(phba);
James Smartd79c9e92019-08-14 16:57:09 -07007081out_free_cmd_rsp_buf:
7082 dma_pool_destroy(phba->lpfc_cmd_rsp_buf_pool);
7083 phba->lpfc_cmd_rsp_buf_pool = NULL;
7084out_free_sg_dma_buf:
7085 dma_pool_destroy(phba->lpfc_sg_dma_buf_pool);
7086 phba->lpfc_sg_dma_buf_pool = NULL;
James Smartda0436e2009-05-22 14:51:39 -04007087out_free_bsmbx:
7088 lpfc_destroy_bootstrap_mbox(phba);
7089out_free_mem:
7090 lpfc_mem_free(phba);
7091 return rc;
7092}
7093
7094/**
7095 * lpfc_sli4_driver_resource_unset - Unset drvr internal resources for SLI4 dev
7096 * @phba: pointer to lpfc hba data structure.
7097 *
7098 * This routine is invoked to unset the driver internal resources set up
7099 * specific for supporting the SLI-4 HBA device it attached to.
7100 **/
7101static void
7102lpfc_sli4_driver_resource_unset(struct lpfc_hba *phba)
7103{
7104 struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
7105
James Smart32517fc2019-01-28 11:14:33 -08007106 free_percpu(phba->sli4_hba.eq_info);
James Smart840eda92020-03-22 11:13:00 -07007107#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
7108 free_percpu(phba->sli4_hba.c_stat);
7109#endif
Dick Kennedy317aeb82020-06-30 14:49:59 -07007110 kfree(phba->sli4_hba.idle_stat);
James Smart32517fc2019-01-28 11:14:33 -08007111
James Smart7bb03bb2013-04-17 20:19:16 -04007112 /* Free memory allocated for msi-x interrupt vector to CPU mapping */
7113 kfree(phba->sli4_hba.cpu_map);
James Smart222e9232019-01-28 11:14:35 -08007114 phba->sli4_hba.num_possible_cpu = 0;
James Smart7bb03bb2013-04-17 20:19:16 -04007115 phba->sli4_hba.num_present_cpu = 0;
James Smart76fd07a2014-02-20 09:57:18 -05007116 phba->sli4_hba.curr_disp_cpu = 0;
Dick Kennedy3048e3e2020-05-01 14:43:06 -07007117 cpumask_clear(&phba->sli4_hba.irq_aff_mask);
James Smart7bb03bb2013-04-17 20:19:16 -04007118
James Smartda0436e2009-05-22 14:51:39 -04007119 /* Free memory allocated for fast-path work queue handles */
James Smart895427b2017-02-12 13:52:30 -08007120 kfree(phba->sli4_hba.hba_eq_hdl);
James Smartda0436e2009-05-22 14:51:39 -04007121
7122 /* Free the allocated rpi headers. */
7123 lpfc_sli4_remove_rpi_hdrs(phba);
James Smartd11e31d2009-06-10 17:23:06 -04007124 lpfc_sli4_remove_rpis(phba);
James Smartda0436e2009-05-22 14:51:39 -04007125
James Smart0c9ab6f2010-02-26 14:15:57 -05007126 /* Free eligible FCF index bmask */
7127 kfree(phba->fcf.fcf_rr_bmask);
7128
James Smartda0436e2009-05-22 14:51:39 -04007129 /* Free the ELS sgl list */
7130 lpfc_free_active_sgl(phba);
James Smart8a9d2e82012-05-09 21:16:12 -04007131 lpfc_free_els_sgl_list(phba);
James Smartf358dd02017-02-12 13:52:34 -08007132 lpfc_free_nvmet_sgl_list(phba);
James Smartda0436e2009-05-22 14:51:39 -04007133
James Smartda0436e2009-05-22 14:51:39 -04007134 /* Free the completion queue EQ event pool */
7135 lpfc_sli4_cq_event_release_all(phba);
7136 lpfc_sli4_cq_event_pool_destroy(phba);
7137
James Smart6d368e52011-05-24 11:44:12 -04007138 /* Release resource identifiers. */
7139 lpfc_sli4_dealloc_resource_identifiers(phba);
7140
James Smartda0436e2009-05-22 14:51:39 -04007141 /* Free the bsmbx region. */
7142 lpfc_destroy_bootstrap_mbox(phba);
7143
7144 /* Free the SLI Layer memory with SLI4 HBAs */
7145 lpfc_mem_free_all(phba);
7146
7147 /* Free the current connect table */
7148 list_for_each_entry_safe(conn_entry, next_conn_entry,
James Smart4d9ab992009-10-02 15:16:39 -04007149 &phba->fcf_conn_rec_list, list) {
7150 list_del_init(&conn_entry->list);
James Smartda0436e2009-05-22 14:51:39 -04007151 kfree(conn_entry);
James Smart4d9ab992009-10-02 15:16:39 -04007152 }
James Smartda0436e2009-05-22 14:51:39 -04007153
7154 return;
7155}
7156
7157/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007158 * lpfc_init_api_table_setup - Set up init api function jump table
James Smart3772a992009-05-22 14:50:54 -04007159 * @phba: The hba struct for which this call is being executed.
7160 * @dev_grp: The HBA PCI-Device group number.
7161 *
7162 * This routine sets up the device INIT interface API function jump table
7163 * in @phba struct.
7164 *
7165 * Returns: 0 - success, -ENODEV - failure.
7166 **/
7167int
7168lpfc_init_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
7169{
James Smart84d1b002010-02-12 14:42:33 -05007170 phba->lpfc_hba_init_link = lpfc_hba_init_link;
7171 phba->lpfc_hba_down_link = lpfc_hba_down_link;
James Smart7f860592011-03-11 16:05:52 -05007172 phba->lpfc_selective_reset = lpfc_selective_reset;
James Smart3772a992009-05-22 14:50:54 -04007173 switch (dev_grp) {
7174 case LPFC_PCI_DEV_LP:
7175 phba->lpfc_hba_down_post = lpfc_hba_down_post_s3;
7176 phba->lpfc_handle_eratt = lpfc_handle_eratt_s3;
7177 phba->lpfc_stop_port = lpfc_stop_port_s3;
7178 break;
James Smartda0436e2009-05-22 14:51:39 -04007179 case LPFC_PCI_DEV_OC:
7180 phba->lpfc_hba_down_post = lpfc_hba_down_post_s4;
7181 phba->lpfc_handle_eratt = lpfc_handle_eratt_s4;
7182 phba->lpfc_stop_port = lpfc_stop_port_s4;
7183 break;
James Smart3772a992009-05-22 14:50:54 -04007184 default:
Dick Kennedy372c1872020-06-30 14:50:00 -07007185 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart3772a992009-05-22 14:50:54 -04007186 "1431 Invalid HBA PCI-device group: 0x%x\n",
7187 dev_grp);
7188 return -ENODEV;
7189 break;
7190 }
7191 return 0;
7192}
7193
7194/**
James Smart3772a992009-05-22 14:50:54 -04007195 * lpfc_setup_driver_resource_phase2 - Phase2 setup driver internal resources.
7196 * @phba: pointer to lpfc hba data structure.
7197 *
7198 * This routine is invoked to set up the driver internal resources after the
7199 * device specific resource setup to support the HBA device it attached to.
7200 *
7201 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007202 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04007203 * other values - error
7204 **/
7205static int
7206lpfc_setup_driver_resource_phase2(struct lpfc_hba *phba)
7207{
7208 int error;
7209
7210 /* Startup the kernel thread for this host adapter. */
7211 phba->worker_thread = kthread_run(lpfc_do_work, phba,
7212 "lpfc_worker_%d", phba->brd_no);
7213 if (IS_ERR(phba->worker_thread)) {
7214 error = PTR_ERR(phba->worker_thread);
7215 return error;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05007216 }
7217
James Smart3772a992009-05-22 14:50:54 -04007218 return 0;
7219}
7220
7221/**
7222 * lpfc_unset_driver_resource_phase2 - Phase2 unset driver internal resources.
7223 * @phba: pointer to lpfc hba data structure.
7224 *
7225 * This routine is invoked to unset the driver internal resources set up after
7226 * the device specific resource setup for supporting the HBA device it
7227 * attached to.
7228 **/
7229static void
7230lpfc_unset_driver_resource_phase2(struct lpfc_hba *phba)
7231{
Dick Kennedyf485c182017-09-29 17:34:34 -07007232 if (phba->wq) {
7233 flush_workqueue(phba->wq);
7234 destroy_workqueue(phba->wq);
7235 phba->wq = NULL;
7236 }
7237
James Smart3772a992009-05-22 14:50:54 -04007238 /* Stop kernel worker thread */
James Smart0cdb84e2018-04-09 14:24:26 -07007239 if (phba->worker_thread)
7240 kthread_stop(phba->worker_thread);
James Smart3772a992009-05-22 14:50:54 -04007241}
7242
7243/**
7244 * lpfc_free_iocb_list - Free iocb list.
7245 * @phba: pointer to lpfc hba data structure.
7246 *
7247 * This routine is invoked to free the driver's IOCB list and memory.
7248 **/
James Smart6c621a22017-05-15 15:20:45 -07007249void
James Smart3772a992009-05-22 14:50:54 -04007250lpfc_free_iocb_list(struct lpfc_hba *phba)
7251{
7252 struct lpfc_iocbq *iocbq_entry = NULL, *iocbq_next = NULL;
7253
7254 spin_lock_irq(&phba->hbalock);
7255 list_for_each_entry_safe(iocbq_entry, iocbq_next,
7256 &phba->lpfc_iocb_list, list) {
7257 list_del(&iocbq_entry->list);
7258 kfree(iocbq_entry);
7259 phba->total_iocbq_bufs--;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05007260 }
James Smart3772a992009-05-22 14:50:54 -04007261 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007262
James Smart3772a992009-05-22 14:50:54 -04007263 return;
7264}
dea31012005-04-17 16:05:31 -05007265
James Smart3772a992009-05-22 14:50:54 -04007266/**
7267 * lpfc_init_iocb_list - Allocate and initialize iocb list.
7268 * @phba: pointer to lpfc hba data structure.
Lee Jonesfe614ac2020-07-23 13:24:22 +01007269 * @iocb_count: number of requested iocbs
James Smart3772a992009-05-22 14:50:54 -04007270 *
7271 * This routine is invoked to allocate and initizlize the driver's IOCB
7272 * list and set up the IOCB tag array accordingly.
7273 *
7274 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007275 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04007276 * other values - error
7277 **/
James Smart6c621a22017-05-15 15:20:45 -07007278int
James Smart3772a992009-05-22 14:50:54 -04007279lpfc_init_iocb_list(struct lpfc_hba *phba, int iocb_count)
7280{
7281 struct lpfc_iocbq *iocbq_entry = NULL;
7282 uint16_t iotag;
7283 int i;
dea31012005-04-17 16:05:31 -05007284
7285 /* Initialize and populate the iocb list per host. */
7286 INIT_LIST_HEAD(&phba->lpfc_iocb_list);
James Smart3772a992009-05-22 14:50:54 -04007287 for (i = 0; i < iocb_count; i++) {
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07007288 iocbq_entry = kzalloc(sizeof(struct lpfc_iocbq), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05007289 if (iocbq_entry == NULL) {
7290 printk(KERN_ERR "%s: only allocated %d iocbs of "
7291 "expected %d count. Unloading driver.\n",
James Smarta5f73372019-09-21 20:58:50 -07007292 __func__, i, iocb_count);
dea31012005-04-17 16:05:31 -05007293 goto out_free_iocbq;
7294 }
7295
James Bottomley604a3e32005-10-29 10:28:33 -05007296 iotag = lpfc_sli_next_iotag(phba, iocbq_entry);
7297 if (iotag == 0) {
James Smart3772a992009-05-22 14:50:54 -04007298 kfree(iocbq_entry);
James Bottomley604a3e32005-10-29 10:28:33 -05007299 printk(KERN_ERR "%s: failed to allocate IOTAG. "
James Smart3772a992009-05-22 14:50:54 -04007300 "Unloading driver.\n", __func__);
James Bottomley604a3e32005-10-29 10:28:33 -05007301 goto out_free_iocbq;
7302 }
James Smart6d368e52011-05-24 11:44:12 -04007303 iocbq_entry->sli4_lxritag = NO_XRI;
James Smart3772a992009-05-22 14:50:54 -04007304 iocbq_entry->sli4_xritag = NO_XRI;
James Smart2e0fef82007-06-17 19:56:36 -05007305
7306 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007307 list_add(&iocbq_entry->list, &phba->lpfc_iocb_list);
7308 phba->total_iocbq_bufs++;
James Smart2e0fef82007-06-17 19:56:36 -05007309 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007310 }
7311
James Smart3772a992009-05-22 14:50:54 -04007312 return 0;
7313
7314out_free_iocbq:
7315 lpfc_free_iocb_list(phba);
7316
7317 return -ENOMEM;
7318}
7319
7320/**
James Smart8a9d2e82012-05-09 21:16:12 -04007321 * lpfc_free_sgl_list - Free a given sgl list.
James Smartda0436e2009-05-22 14:51:39 -04007322 * @phba: pointer to lpfc hba data structure.
James Smart8a9d2e82012-05-09 21:16:12 -04007323 * @sglq_list: pointer to the head of sgl list.
James Smartda0436e2009-05-22 14:51:39 -04007324 *
James Smart8a9d2e82012-05-09 21:16:12 -04007325 * This routine is invoked to free a give sgl list and memory.
James Smartda0436e2009-05-22 14:51:39 -04007326 **/
James Smart8a9d2e82012-05-09 21:16:12 -04007327void
7328lpfc_free_sgl_list(struct lpfc_hba *phba, struct list_head *sglq_list)
James Smartda0436e2009-05-22 14:51:39 -04007329{
7330 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
James Smart8a9d2e82012-05-09 21:16:12 -04007331
7332 list_for_each_entry_safe(sglq_entry, sglq_next, sglq_list, list) {
7333 list_del(&sglq_entry->list);
7334 lpfc_mbuf_free(phba, sglq_entry->virt, sglq_entry->phys);
7335 kfree(sglq_entry);
7336 }
7337}
7338
7339/**
7340 * lpfc_free_els_sgl_list - Free els sgl list.
7341 * @phba: pointer to lpfc hba data structure.
7342 *
7343 * This routine is invoked to free the driver's els sgl list and memory.
7344 **/
7345static void
7346lpfc_free_els_sgl_list(struct lpfc_hba *phba)
7347{
James Smartda0436e2009-05-22 14:51:39 -04007348 LIST_HEAD(sglq_list);
James Smartda0436e2009-05-22 14:51:39 -04007349
James Smart8a9d2e82012-05-09 21:16:12 -04007350 /* Retrieve all els sgls from driver list */
James Smartda0436e2009-05-22 14:51:39 -04007351 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -08007352 spin_lock(&phba->sli4_hba.sgl_list_lock);
7353 list_splice_init(&phba->sli4_hba.lpfc_els_sgl_list, &sglq_list);
7354 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smartda0436e2009-05-22 14:51:39 -04007355 spin_unlock_irq(&phba->hbalock);
7356
James Smart8a9d2e82012-05-09 21:16:12 -04007357 /* Now free the sgl list */
7358 lpfc_free_sgl_list(phba, &sglq_list);
James Smartda0436e2009-05-22 14:51:39 -04007359}
7360
7361/**
James Smartf358dd02017-02-12 13:52:34 -08007362 * lpfc_free_nvmet_sgl_list - Free nvmet sgl list.
7363 * @phba: pointer to lpfc hba data structure.
7364 *
7365 * This routine is invoked to free the driver's nvmet sgl list and memory.
7366 **/
7367static void
7368lpfc_free_nvmet_sgl_list(struct lpfc_hba *phba)
7369{
7370 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
7371 LIST_HEAD(sglq_list);
7372
7373 /* Retrieve all nvmet sgls from driver list */
7374 spin_lock_irq(&phba->hbalock);
7375 spin_lock(&phba->sli4_hba.sgl_list_lock);
7376 list_splice_init(&phba->sli4_hba.lpfc_nvmet_sgl_list, &sglq_list);
7377 spin_unlock(&phba->sli4_hba.sgl_list_lock);
7378 spin_unlock_irq(&phba->hbalock);
7379
7380 /* Now free the sgl list */
7381 list_for_each_entry_safe(sglq_entry, sglq_next, &sglq_list, list) {
7382 list_del(&sglq_entry->list);
7383 lpfc_nvmet_buf_free(phba, sglq_entry->virt, sglq_entry->phys);
7384 kfree(sglq_entry);
7385 }
Dick Kennedy4b40d022017-08-23 16:55:38 -07007386
7387 /* Update the nvmet_xri_cnt to reflect no current sgls.
7388 * The next initialization cycle sets the count and allocates
7389 * the sgls over again.
7390 */
7391 phba->sli4_hba.nvmet_xri_cnt = 0;
James Smartf358dd02017-02-12 13:52:34 -08007392}
7393
7394/**
James Smartda0436e2009-05-22 14:51:39 -04007395 * lpfc_init_active_sgl_array - Allocate the buf to track active ELS XRIs.
7396 * @phba: pointer to lpfc hba data structure.
7397 *
7398 * This routine is invoked to allocate the driver's active sgl memory.
7399 * This array will hold the sglq_entry's for active IOs.
7400 **/
7401static int
7402lpfc_init_active_sgl_array(struct lpfc_hba *phba)
7403{
7404 int size;
7405 size = sizeof(struct lpfc_sglq *);
7406 size *= phba->sli4_hba.max_cfg_param.max_xri;
7407
7408 phba->sli4_hba.lpfc_sglq_active_list =
7409 kzalloc(size, GFP_KERNEL);
7410 if (!phba->sli4_hba.lpfc_sglq_active_list)
7411 return -ENOMEM;
7412 return 0;
7413}
7414
7415/**
7416 * lpfc_free_active_sgl - Free the buf that tracks active ELS XRIs.
7417 * @phba: pointer to lpfc hba data structure.
7418 *
7419 * This routine is invoked to walk through the array of active sglq entries
7420 * and free all of the resources.
7421 * This is just a place holder for now.
7422 **/
7423static void
7424lpfc_free_active_sgl(struct lpfc_hba *phba)
7425{
7426 kfree(phba->sli4_hba.lpfc_sglq_active_list);
7427}
7428
7429/**
7430 * lpfc_init_sgl_list - Allocate and initialize sgl list.
7431 * @phba: pointer to lpfc hba data structure.
7432 *
7433 * This routine is invoked to allocate and initizlize the driver's sgl
7434 * list and set up the sgl xritag tag array accordingly.
7435 *
James Smartda0436e2009-05-22 14:51:39 -04007436 **/
James Smart8a9d2e82012-05-09 21:16:12 -04007437static void
James Smartda0436e2009-05-22 14:51:39 -04007438lpfc_init_sgl_list(struct lpfc_hba *phba)
7439{
James Smartda0436e2009-05-22 14:51:39 -04007440 /* Initialize and populate the sglq list per host/VF. */
James Smart895427b2017-02-12 13:52:30 -08007441 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_els_sgl_list);
James Smartda0436e2009-05-22 14:51:39 -04007442 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_els_sgl_list);
James Smartf358dd02017-02-12 13:52:34 -08007443 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_nvmet_sgl_list);
James Smart86c67372017-04-21 16:05:04 -07007444 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_nvmet_ctx_list);
James Smartda0436e2009-05-22 14:51:39 -04007445
James Smart8a9d2e82012-05-09 21:16:12 -04007446 /* els xri-sgl book keeping */
7447 phba->sli4_hba.els_xri_cnt = 0;
James Smartda0436e2009-05-22 14:51:39 -04007448
James Smart895427b2017-02-12 13:52:30 -08007449 /* nvme xri-buffer book keeping */
James Smart5e5b5112019-01-28 11:14:22 -08007450 phba->sli4_hba.io_xri_cnt = 0;
James Smartda0436e2009-05-22 14:51:39 -04007451}
7452
7453/**
7454 * lpfc_sli4_init_rpi_hdrs - Post the rpi header memory region to the port
7455 * @phba: pointer to lpfc hba data structure.
7456 *
7457 * This routine is invoked to post rpi header templates to the
James Smart88a2cfb2011-07-22 18:36:33 -04007458 * port for those SLI4 ports that do not support extents. This routine
James Smartda0436e2009-05-22 14:51:39 -04007459 * posts a PAGE_SIZE memory region to the port to hold up to
James Smart88a2cfb2011-07-22 18:36:33 -04007460 * PAGE_SIZE modulo 64 rpi context headers. This is an initialization routine
7461 * and should be called only when interrupts are disabled.
James Smartda0436e2009-05-22 14:51:39 -04007462 *
7463 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007464 * 0 - successful
James Smart88a2cfb2011-07-22 18:36:33 -04007465 * -ERROR - otherwise.
James Smartda0436e2009-05-22 14:51:39 -04007466 **/
7467int
7468lpfc_sli4_init_rpi_hdrs(struct lpfc_hba *phba)
7469{
7470 int rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04007471 struct lpfc_rpi_hdr *rpi_hdr;
7472
7473 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_hdr_list);
James Smartff78d8f2011-12-13 13:21:35 -05007474 if (!phba->sli4_hba.rpi_hdrs_in_use)
James Smart6d368e52011-05-24 11:44:12 -04007475 return rc;
James Smart6d368e52011-05-24 11:44:12 -04007476 if (phba->sli4_hba.extents_in_use)
7477 return -EIO;
James Smartda0436e2009-05-22 14:51:39 -04007478
7479 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
7480 if (!rpi_hdr) {
Dick Kennedy372c1872020-06-30 14:50:00 -07007481 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartda0436e2009-05-22 14:51:39 -04007482 "0391 Error during rpi post operation\n");
7483 lpfc_sli4_remove_rpis(phba);
7484 rc = -ENODEV;
7485 }
7486
7487 return rc;
7488}
7489
7490/**
7491 * lpfc_sli4_create_rpi_hdr - Allocate an rpi header memory region
7492 * @phba: pointer to lpfc hba data structure.
7493 *
7494 * This routine is invoked to allocate a single 4KB memory region to
7495 * support rpis and stores them in the phba. This single region
7496 * provides support for up to 64 rpis. The region is used globally
7497 * by the device.
7498 *
7499 * Returns:
7500 * A valid rpi hdr on success.
7501 * A NULL pointer on any failure.
7502 **/
7503struct lpfc_rpi_hdr *
7504lpfc_sli4_create_rpi_hdr(struct lpfc_hba *phba)
7505{
7506 uint16_t rpi_limit, curr_rpi_range;
7507 struct lpfc_dmabuf *dmabuf;
7508 struct lpfc_rpi_hdr *rpi_hdr;
7509
James Smart6d368e52011-05-24 11:44:12 -04007510 /*
7511 * If the SLI4 port supports extents, posting the rpi header isn't
7512 * required. Set the expected maximum count and let the actual value
7513 * get set when extents are fully allocated.
7514 */
7515 if (!phba->sli4_hba.rpi_hdrs_in_use)
7516 return NULL;
7517 if (phba->sli4_hba.extents_in_use)
7518 return NULL;
7519
7520 /* The limit on the logical index is just the max_rpi count. */
James Smart845d9e82017-05-15 15:20:38 -07007521 rpi_limit = phba->sli4_hba.max_cfg_param.max_rpi;
James Smartda0436e2009-05-22 14:51:39 -04007522
7523 spin_lock_irq(&phba->hbalock);
James Smart6d368e52011-05-24 11:44:12 -04007524 /*
7525 * Establish the starting RPI in this header block. The starting
7526 * rpi is normalized to a zero base because the physical rpi is
7527 * port based.
7528 */
James Smart97f2ecf2012-03-01 22:35:23 -05007529 curr_rpi_range = phba->sli4_hba.next_rpi;
James Smartda0436e2009-05-22 14:51:39 -04007530 spin_unlock_irq(&phba->hbalock);
7531
James Smart845d9e82017-05-15 15:20:38 -07007532 /* Reached full RPI range */
7533 if (curr_rpi_range == rpi_limit)
James Smart6d368e52011-05-24 11:44:12 -04007534 return NULL;
James Smart845d9e82017-05-15 15:20:38 -07007535
James Smartda0436e2009-05-22 14:51:39 -04007536 /*
7537 * First allocate the protocol header region for the port. The
7538 * port expects a 4KB DMA-mapped memory region that is 4K aligned.
7539 */
7540 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
7541 if (!dmabuf)
7542 return NULL;
7543
Luis Chamberlain750afb02019-01-04 09:23:09 +01007544 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
7545 LPFC_HDR_TEMPLATE_SIZE,
7546 &dmabuf->phys, GFP_KERNEL);
James Smartda0436e2009-05-22 14:51:39 -04007547 if (!dmabuf->virt) {
7548 rpi_hdr = NULL;
7549 goto err_free_dmabuf;
7550 }
7551
James Smartda0436e2009-05-22 14:51:39 -04007552 if (!IS_ALIGNED(dmabuf->phys, LPFC_HDR_TEMPLATE_SIZE)) {
7553 rpi_hdr = NULL;
7554 goto err_free_coherent;
7555 }
7556
7557 /* Save the rpi header data for cleanup later. */
7558 rpi_hdr = kzalloc(sizeof(struct lpfc_rpi_hdr), GFP_KERNEL);
7559 if (!rpi_hdr)
7560 goto err_free_coherent;
7561
7562 rpi_hdr->dmabuf = dmabuf;
7563 rpi_hdr->len = LPFC_HDR_TEMPLATE_SIZE;
7564 rpi_hdr->page_count = 1;
7565 spin_lock_irq(&phba->hbalock);
James Smart6d368e52011-05-24 11:44:12 -04007566
7567 /* The rpi_hdr stores the logical index only. */
7568 rpi_hdr->start_rpi = curr_rpi_range;
James Smart845d9e82017-05-15 15:20:38 -07007569 rpi_hdr->next_rpi = phba->sli4_hba.next_rpi + LPFC_RPI_HDR_COUNT;
James Smartda0436e2009-05-22 14:51:39 -04007570 list_add_tail(&rpi_hdr->list, &phba->sli4_hba.lpfc_rpi_hdr_list);
7571
James Smartda0436e2009-05-22 14:51:39 -04007572 spin_unlock_irq(&phba->hbalock);
7573 return rpi_hdr;
7574
7575 err_free_coherent:
7576 dma_free_coherent(&phba->pcidev->dev, LPFC_HDR_TEMPLATE_SIZE,
7577 dmabuf->virt, dmabuf->phys);
7578 err_free_dmabuf:
7579 kfree(dmabuf);
7580 return NULL;
7581}
7582
7583/**
7584 * lpfc_sli4_remove_rpi_hdrs - Remove all rpi header memory regions
7585 * @phba: pointer to lpfc hba data structure.
7586 *
7587 * This routine is invoked to remove all memory resources allocated
James Smart6d368e52011-05-24 11:44:12 -04007588 * to support rpis for SLI4 ports not supporting extents. This routine
7589 * presumes the caller has released all rpis consumed by fabric or port
7590 * logins and is prepared to have the header pages removed.
James Smartda0436e2009-05-22 14:51:39 -04007591 **/
7592void
7593lpfc_sli4_remove_rpi_hdrs(struct lpfc_hba *phba)
7594{
7595 struct lpfc_rpi_hdr *rpi_hdr, *next_rpi_hdr;
7596
James Smart6d368e52011-05-24 11:44:12 -04007597 if (!phba->sli4_hba.rpi_hdrs_in_use)
7598 goto exit;
7599
James Smartda0436e2009-05-22 14:51:39 -04007600 list_for_each_entry_safe(rpi_hdr, next_rpi_hdr,
7601 &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
7602 list_del(&rpi_hdr->list);
7603 dma_free_coherent(&phba->pcidev->dev, rpi_hdr->len,
7604 rpi_hdr->dmabuf->virt, rpi_hdr->dmabuf->phys);
7605 kfree(rpi_hdr->dmabuf);
7606 kfree(rpi_hdr);
7607 }
James Smart6d368e52011-05-24 11:44:12 -04007608 exit:
7609 /* There are no rpis available to the port now. */
7610 phba->sli4_hba.next_rpi = 0;
James Smartda0436e2009-05-22 14:51:39 -04007611}
7612
7613/**
James Smart3772a992009-05-22 14:50:54 -04007614 * lpfc_hba_alloc - Allocate driver hba data structure for a device.
7615 * @pdev: pointer to pci device data structure.
7616 *
7617 * This routine is invoked to allocate the driver hba data structure for an
7618 * HBA device. If the allocation is successful, the phba reference to the
7619 * PCI device data structure is set.
7620 *
7621 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007622 * pointer to @phba - successful
James Smart3772a992009-05-22 14:50:54 -04007623 * NULL - error
7624 **/
7625static struct lpfc_hba *
7626lpfc_hba_alloc(struct pci_dev *pdev)
7627{
7628 struct lpfc_hba *phba;
7629
7630 /* Allocate memory for HBA structure */
7631 phba = kzalloc(sizeof(struct lpfc_hba), GFP_KERNEL);
7632 if (!phba) {
Jiri Slabye34ccdf2009-07-13 23:25:54 +02007633 dev_err(&pdev->dev, "failed to allocate hba struct\n");
James Smart3772a992009-05-22 14:50:54 -04007634 return NULL;
7635 }
7636
7637 /* Set reference to PCI device in HBA structure */
7638 phba->pcidev = pdev;
7639
7640 /* Assign an unused board number */
7641 phba->brd_no = lpfc_get_instance();
7642 if (phba->brd_no < 0) {
7643 kfree(phba);
7644 return NULL;
7645 }
James Smart65791f12016-07-06 12:35:56 -07007646 phba->eratt_poll_interval = LPFC_ERATT_POLL_INTERVAL;
James Smart3772a992009-05-22 14:50:54 -04007647
James Smart4fede782010-01-26 23:08:55 -05007648 spin_lock_init(&phba->ct_ev_lock);
James Smartf1c3b0f2009-07-19 10:01:32 -04007649 INIT_LIST_HEAD(&phba->ct_ev_waiters);
7650
James Smart3772a992009-05-22 14:50:54 -04007651 return phba;
7652}
7653
7654/**
7655 * lpfc_hba_free - Free driver hba data structure with a device.
7656 * @phba: pointer to lpfc hba data structure.
7657 *
7658 * This routine is invoked to free the driver hba data structure with an
7659 * HBA device.
7660 **/
7661static void
7662lpfc_hba_free(struct lpfc_hba *phba)
7663{
James Smart5e5b5112019-01-28 11:14:22 -08007664 if (phba->sli_rev == LPFC_SLI_REV4)
7665 kfree(phba->sli4_hba.hdwq);
7666
James Smart3772a992009-05-22 14:50:54 -04007667 /* Release the driver assigned board number */
7668 idr_remove(&lpfc_hba_index, phba->brd_no);
7669
James Smart895427b2017-02-12 13:52:30 -08007670 /* Free memory allocated with sli3 rings */
7671 kfree(phba->sli.sli3_ring);
7672 phba->sli.sli3_ring = NULL;
James Smart2a76a282012-08-03 12:35:54 -04007673
James Smart3772a992009-05-22 14:50:54 -04007674 kfree(phba);
7675 return;
7676}
7677
7678/**
7679 * lpfc_create_shost - Create hba physical port with associated scsi host.
7680 * @phba: pointer to lpfc hba data structure.
7681 *
7682 * This routine is invoked to create HBA physical port and associate a SCSI
7683 * host with it.
7684 *
7685 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007686 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04007687 * other values - error
7688 **/
7689static int
7690lpfc_create_shost(struct lpfc_hba *phba)
7691{
7692 struct lpfc_vport *vport;
7693 struct Scsi_Host *shost;
7694
7695 /* Initialize HBA FC structure */
dea31012005-04-17 16:05:31 -05007696 phba->fc_edtov = FF_DEF_EDTOV;
7697 phba->fc_ratov = FF_DEF_RATOV;
7698 phba->fc_altov = FF_DEF_ALTOV;
7699 phba->fc_arbtov = FF_DEF_ARBTOV;
7700
James Smartd7c47992010-06-08 18:31:54 -04007701 atomic_set(&phba->sdev_cnt, 0);
James Smart3de2a652007-08-02 11:09:59 -04007702 vport = lpfc_create_port(phba, phba->brd_no, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05007703 if (!vport)
James Smart3772a992009-05-22 14:50:54 -04007704 return -ENODEV;
James Smart2e0fef82007-06-17 19:56:36 -05007705
7706 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05007707 phba->pport = vport;
James Smart2ea259e2017-02-12 13:52:27 -08007708
James Smartf358dd02017-02-12 13:52:34 -08007709 if (phba->nvmet_support) {
7710 /* Only 1 vport (pport) will support NVME target */
James Smartea85a202019-10-18 14:18:25 -07007711 phba->targetport = NULL;
7712 phba->cfg_enable_fc4_type = LPFC_ENABLE_NVME;
7713 lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_NVME_DISC,
7714 "6076 NVME Target Found\n");
James Smartf358dd02017-02-12 13:52:34 -08007715 }
7716
James Smart858c9f62007-06-17 19:56:39 -05007717 lpfc_debugfs_initialize(vport);
James Smart3772a992009-05-22 14:50:54 -04007718 /* Put reference to SCSI host to driver's device private data */
7719 pci_set_drvdata(phba->pcidev, shost);
James Smart2e0fef82007-06-17 19:56:36 -05007720
James Smart4258e982015-12-16 18:11:58 -05007721 /*
7722 * At this point we are fully registered with PSA. In addition,
7723 * any initial discovery should be completed.
7724 */
7725 vport->load_flag |= FC_ALLOW_FDMI;
James Smart8663cbb2016-03-31 14:12:33 -07007726 if (phba->cfg_enable_SmartSAN ||
7727 (phba->cfg_fdmi_on == LPFC_FDMI_SUPPORT)) {
James Smart4258e982015-12-16 18:11:58 -05007728
7729 /* Setup appropriate attribute masks */
7730 vport->fdmi_hba_mask = LPFC_FDMI2_HBA_ATTR;
James Smart8663cbb2016-03-31 14:12:33 -07007731 if (phba->cfg_enable_SmartSAN)
James Smart4258e982015-12-16 18:11:58 -05007732 vport->fdmi_port_mask = LPFC_FDMI2_SMART_ATTR;
7733 else
7734 vport->fdmi_port_mask = LPFC_FDMI2_PORT_ATTR;
7735 }
James Smart3772a992009-05-22 14:50:54 -04007736 return 0;
7737}
dea31012005-04-17 16:05:31 -05007738
James Smart3772a992009-05-22 14:50:54 -04007739/**
7740 * lpfc_destroy_shost - Destroy hba physical port with associated scsi host.
7741 * @phba: pointer to lpfc hba data structure.
7742 *
7743 * This routine is invoked to destroy HBA physical port and the associated
7744 * SCSI host.
7745 **/
7746static void
7747lpfc_destroy_shost(struct lpfc_hba *phba)
7748{
7749 struct lpfc_vport *vport = phba->pport;
James Smart93996272008-08-24 21:50:30 -04007750
James Smart3772a992009-05-22 14:50:54 -04007751 /* Destroy physical port that associated with the SCSI host */
7752 destroy_port(vport);
7753
7754 return;
7755}
7756
7757/**
7758 * lpfc_setup_bg - Setup Block guard structures and debug areas.
7759 * @phba: pointer to lpfc hba data structure.
7760 * @shost: the shost to be used to detect Block guard settings.
7761 *
7762 * This routine sets up the local Block guard protocol settings for @shost.
7763 * This routine also allocates memory for debugging bg buffers.
7764 **/
7765static void
7766lpfc_setup_bg(struct lpfc_hba *phba, struct Scsi_Host *shost)
7767{
James Smartbbeb79b2012-06-12 13:54:27 -04007768 uint32_t old_mask;
7769 uint32_t old_guard;
7770
James Smartb3b98b72016-10-13 15:06:06 -07007771 if (phba->cfg_prot_mask && phba->cfg_prot_guard) {
James Smart3772a992009-05-22 14:50:54 -04007772 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7773 "1478 Registering BlockGuard with the "
7774 "SCSI layer\n");
James Smartbbeb79b2012-06-12 13:54:27 -04007775
James Smartb3b98b72016-10-13 15:06:06 -07007776 old_mask = phba->cfg_prot_mask;
7777 old_guard = phba->cfg_prot_guard;
James Smartbbeb79b2012-06-12 13:54:27 -04007778
7779 /* Only allow supported values */
James Smartb3b98b72016-10-13 15:06:06 -07007780 phba->cfg_prot_mask &= (SHOST_DIF_TYPE1_PROTECTION |
James Smartbbeb79b2012-06-12 13:54:27 -04007781 SHOST_DIX_TYPE0_PROTECTION |
7782 SHOST_DIX_TYPE1_PROTECTION);
James Smartb3b98b72016-10-13 15:06:06 -07007783 phba->cfg_prot_guard &= (SHOST_DIX_GUARD_IP |
7784 SHOST_DIX_GUARD_CRC);
James Smartbbeb79b2012-06-12 13:54:27 -04007785
7786 /* DIF Type 1 protection for profiles AST1/C1 is end to end */
James Smartb3b98b72016-10-13 15:06:06 -07007787 if (phba->cfg_prot_mask == SHOST_DIX_TYPE1_PROTECTION)
7788 phba->cfg_prot_mask |= SHOST_DIF_TYPE1_PROTECTION;
James Smartbbeb79b2012-06-12 13:54:27 -04007789
James Smartb3b98b72016-10-13 15:06:06 -07007790 if (phba->cfg_prot_mask && phba->cfg_prot_guard) {
7791 if ((old_mask != phba->cfg_prot_mask) ||
7792 (old_guard != phba->cfg_prot_guard))
Dick Kennedy372c1872020-06-30 14:50:00 -07007793 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartbbeb79b2012-06-12 13:54:27 -04007794 "1475 Registering BlockGuard with the "
7795 "SCSI layer: mask %d guard %d\n",
James Smartb3b98b72016-10-13 15:06:06 -07007796 phba->cfg_prot_mask,
7797 phba->cfg_prot_guard);
James Smartbbeb79b2012-06-12 13:54:27 -04007798
James Smartb3b98b72016-10-13 15:06:06 -07007799 scsi_host_set_prot(shost, phba->cfg_prot_mask);
7800 scsi_host_set_guard(shost, phba->cfg_prot_guard);
James Smartbbeb79b2012-06-12 13:54:27 -04007801 } else
Dick Kennedy372c1872020-06-30 14:50:00 -07007802 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartbbeb79b2012-06-12 13:54:27 -04007803 "1479 Not Registering BlockGuard with the SCSI "
7804 "layer, Bad protection parameters: %d %d\n",
7805 old_mask, old_guard);
James Smart98c9ea52007-10-27 13:37:33 -04007806 }
James Smart3772a992009-05-22 14:50:54 -04007807}
7808
7809/**
7810 * lpfc_post_init_setup - Perform necessary device post initialization setup.
7811 * @phba: pointer to lpfc hba data structure.
7812 *
7813 * This routine is invoked to perform all the necessary post initialization
7814 * setup for the device.
7815 **/
7816static void
7817lpfc_post_init_setup(struct lpfc_hba *phba)
7818{
7819 struct Scsi_Host *shost;
7820 struct lpfc_adapter_event_header adapter_event;
7821
7822 /* Get the default values for Model Name and Description */
7823 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
7824
7825 /*
7826 * hba setup may have changed the hba_queue_depth so we need to
7827 * adjust the value of can_queue.
7828 */
7829 shost = pci_get_drvdata(phba->pcidev);
7830 shost->can_queue = phba->cfg_hba_queue_depth - 10;
James Smart858c9f62007-06-17 19:56:39 -05007831
7832 lpfc_host_attrib_init(shost);
7833
James Smart2e0fef82007-06-17 19:56:36 -05007834 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
7835 spin_lock_irq(shost->host_lock);
7836 lpfc_poll_start_timer(phba);
7837 spin_unlock_irq(shost->host_lock);
7838 }
James Smart8f6d98d2006-08-01 07:34:00 -04007839
James Smart93996272008-08-24 21:50:30 -04007840 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7841 "0428 Perform SCSI scan\n");
James Smartea2151b2008-09-07 11:52:10 -04007842 /* Send board arrival event to upper layer */
7843 adapter_event.event_type = FC_REG_ADAPTER_EVENT;
7844 adapter_event.subcategory = LPFC_EVENT_ARRIVAL;
7845 fc_host_post_vendor_event(shost, fc_get_event_number(),
James Smart3772a992009-05-22 14:50:54 -04007846 sizeof(adapter_event),
7847 (char *) &adapter_event,
7848 LPFC_NL_VENDOR_ID);
7849 return;
7850}
7851
7852/**
7853 * lpfc_sli_pci_mem_setup - Setup SLI3 HBA PCI memory space.
7854 * @phba: pointer to lpfc hba data structure.
7855 *
7856 * This routine is invoked to set up the PCI device memory space for device
7857 * with SLI-3 interface spec.
7858 *
7859 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007860 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04007861 * other values - error
7862 **/
7863static int
7864lpfc_sli_pci_mem_setup(struct lpfc_hba *phba)
7865{
Christoph Hellwigf30e1bf2018-10-18 15:10:21 +02007866 struct pci_dev *pdev = phba->pcidev;
James Smart3772a992009-05-22 14:50:54 -04007867 unsigned long bar0map_len, bar2map_len;
7868 int i, hbq_count;
7869 void *ptr;
Hannes Reinecke56de8352019-02-18 08:34:19 +01007870 int error;
James Smart3772a992009-05-22 14:50:54 -04007871
Christoph Hellwigf30e1bf2018-10-18 15:10:21 +02007872 if (!pdev)
Hannes Reinecke56de8352019-02-18 08:34:19 +01007873 return -ENODEV;
James Smart3772a992009-05-22 14:50:54 -04007874
7875 /* Set the device DMA mask size */
Hannes Reinecke56de8352019-02-18 08:34:19 +01007876 error = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
7877 if (error)
7878 error = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
7879 if (error)
Christoph Hellwigf30e1bf2018-10-18 15:10:21 +02007880 return error;
Hannes Reinecke56de8352019-02-18 08:34:19 +01007881 error = -ENODEV;
James Smart3772a992009-05-22 14:50:54 -04007882
7883 /* Get the bus address of Bar0 and Bar2 and the number of bytes
7884 * required by each mapping.
7885 */
7886 phba->pci_bar0_map = pci_resource_start(pdev, 0);
7887 bar0map_len = pci_resource_len(pdev, 0);
7888
7889 phba->pci_bar2_map = pci_resource_start(pdev, 2);
7890 bar2map_len = pci_resource_len(pdev, 2);
7891
7892 /* Map HBA SLIM to a kernel virtual address. */
7893 phba->slim_memmap_p = ioremap(phba->pci_bar0_map, bar0map_len);
7894 if (!phba->slim_memmap_p) {
7895 dev_printk(KERN_ERR, &pdev->dev,
7896 "ioremap failed for SLIM memory.\n");
7897 goto out;
7898 }
7899
7900 /* Map HBA Control Registers to a kernel virtual address. */
7901 phba->ctrl_regs_memmap_p = ioremap(phba->pci_bar2_map, bar2map_len);
7902 if (!phba->ctrl_regs_memmap_p) {
7903 dev_printk(KERN_ERR, &pdev->dev,
7904 "ioremap failed for HBA control registers.\n");
7905 goto out_iounmap_slim;
7906 }
7907
7908 /* Allocate memory for SLI-2 structures */
Luis Chamberlain750afb02019-01-04 09:23:09 +01007909 phba->slim2p.virt = dma_alloc_coherent(&pdev->dev, SLI2_SLIM_SIZE,
7910 &phba->slim2p.phys, GFP_KERNEL);
James Smart3772a992009-05-22 14:50:54 -04007911 if (!phba->slim2p.virt)
7912 goto out_iounmap;
7913
James Smart3772a992009-05-22 14:50:54 -04007914 phba->mbox = phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, mbx);
James Smart7a470272010-03-15 11:25:20 -04007915 phba->mbox_ext = (phba->slim2p.virt +
7916 offsetof(struct lpfc_sli2_slim, mbx_ext_words));
James Smart3772a992009-05-22 14:50:54 -04007917 phba->pcb = (phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, pcb));
7918 phba->IOCBs = (phba->slim2p.virt +
7919 offsetof(struct lpfc_sli2_slim, IOCBs));
7920
7921 phba->hbqslimp.virt = dma_alloc_coherent(&pdev->dev,
7922 lpfc_sli_hbq_size(),
7923 &phba->hbqslimp.phys,
7924 GFP_KERNEL);
7925 if (!phba->hbqslimp.virt)
7926 goto out_free_slim;
7927
7928 hbq_count = lpfc_sli_hbq_count();
7929 ptr = phba->hbqslimp.virt;
7930 for (i = 0; i < hbq_count; ++i) {
7931 phba->hbqs[i].hbq_virt = ptr;
7932 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
7933 ptr += (lpfc_hbq_defs[i]->entry_count *
7934 sizeof(struct lpfc_hbq_entry));
7935 }
7936 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_els_hbq_alloc;
7937 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_els_hbq_free;
7938
7939 memset(phba->hbqslimp.virt, 0, lpfc_sli_hbq_size());
7940
James Smart3772a992009-05-22 14:50:54 -04007941 phba->MBslimaddr = phba->slim_memmap_p;
7942 phba->HAregaddr = phba->ctrl_regs_memmap_p + HA_REG_OFFSET;
7943 phba->CAregaddr = phba->ctrl_regs_memmap_p + CA_REG_OFFSET;
7944 phba->HSregaddr = phba->ctrl_regs_memmap_p + HS_REG_OFFSET;
7945 phba->HCregaddr = phba->ctrl_regs_memmap_p + HC_REG_OFFSET;
James Smartea2151b2008-09-07 11:52:10 -04007946
dea31012005-04-17 16:05:31 -05007947 return 0;
7948
dea31012005-04-17 16:05:31 -05007949out_free_slim:
James Smart34b02dc2008-08-24 21:49:55 -04007950 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
7951 phba->slim2p.virt, phba->slim2p.phys);
dea31012005-04-17 16:05:31 -05007952out_iounmap:
7953 iounmap(phba->ctrl_regs_memmap_p);
Jamie Wellnitz901a9202006-02-28 19:25:19 -05007954out_iounmap_slim:
dea31012005-04-17 16:05:31 -05007955 iounmap(phba->slim_memmap_p);
dea31012005-04-17 16:05:31 -05007956out:
7957 return error;
7958}
7959
James Smarte59058c2008-08-24 21:49:00 -04007960/**
James Smart3772a992009-05-22 14:50:54 -04007961 * lpfc_sli_pci_mem_unset - Unset SLI3 HBA PCI memory space.
7962 * @phba: pointer to lpfc hba data structure.
7963 *
7964 * This routine is invoked to unset the PCI device memory space for device
7965 * with SLI-3 interface spec.
7966 **/
7967static void
7968lpfc_sli_pci_mem_unset(struct lpfc_hba *phba)
7969{
7970 struct pci_dev *pdev;
7971
7972 /* Obtain PCI device reference */
7973 if (!phba->pcidev)
7974 return;
7975 else
7976 pdev = phba->pcidev;
7977
7978 /* Free coherent DMA memory allocated */
7979 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
7980 phba->hbqslimp.virt, phba->hbqslimp.phys);
7981 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
7982 phba->slim2p.virt, phba->slim2p.phys);
7983
7984 /* I/O memory unmap */
7985 iounmap(phba->ctrl_regs_memmap_p);
7986 iounmap(phba->slim_memmap_p);
7987
7988 return;
7989}
7990
7991/**
James Smartda0436e2009-05-22 14:51:39 -04007992 * lpfc_sli4_post_status_check - Wait for SLI4 POST done and check status
7993 * @phba: pointer to lpfc hba data structure.
7994 *
7995 * This routine is invoked to wait for SLI4 device Power On Self Test (POST)
7996 * done and check status.
7997 *
7998 * Return 0 if successful, otherwise -ENODEV.
7999 **/
8000int
8001lpfc_sli4_post_status_check(struct lpfc_hba *phba)
8002{
James Smart2fcee4b2010-12-15 17:57:46 -05008003 struct lpfc_register portsmphr_reg, uerrlo_reg, uerrhi_reg;
8004 struct lpfc_register reg_data;
8005 int i, port_error = 0;
8006 uint32_t if_type;
James Smartda0436e2009-05-22 14:51:39 -04008007
James Smart9940b972011-03-11 16:06:12 -05008008 memset(&portsmphr_reg, 0, sizeof(portsmphr_reg));
8009 memset(&reg_data, 0, sizeof(reg_data));
James Smart2fcee4b2010-12-15 17:57:46 -05008010 if (!phba->sli4_hba.PSMPHRregaddr)
James Smartda0436e2009-05-22 14:51:39 -04008011 return -ENODEV;
8012
James Smartda0436e2009-05-22 14:51:39 -04008013 /* Wait up to 30 seconds for the SLI Port POST done and ready */
8014 for (i = 0; i < 3000; i++) {
James Smart9940b972011-03-11 16:06:12 -05008015 if (lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
8016 &portsmphr_reg.word0) ||
8017 (bf_get(lpfc_port_smphr_perr, &portsmphr_reg))) {
James Smart2fcee4b2010-12-15 17:57:46 -05008018 /* Port has a fatal POST error, break out */
James Smartda0436e2009-05-22 14:51:39 -04008019 port_error = -ENODEV;
8020 break;
8021 }
James Smart2fcee4b2010-12-15 17:57:46 -05008022 if (LPFC_POST_STAGE_PORT_READY ==
8023 bf_get(lpfc_port_smphr_port_status, &portsmphr_reg))
James Smartda0436e2009-05-22 14:51:39 -04008024 break;
James Smartda0436e2009-05-22 14:51:39 -04008025 msleep(10);
8026 }
8027
James Smart2fcee4b2010-12-15 17:57:46 -05008028 /*
8029 * If there was a port error during POST, then don't proceed with
8030 * other register reads as the data may not be valid. Just exit.
8031 */
8032 if (port_error) {
Dick Kennedy372c1872020-06-30 14:50:00 -07008033 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2fcee4b2010-12-15 17:57:46 -05008034 "1408 Port Failed POST - portsmphr=0x%x, "
8035 "perr=x%x, sfi=x%x, nip=x%x, ipc=x%x, scr1=x%x, "
8036 "scr2=x%x, hscratch=x%x, pstatus=x%x\n",
8037 portsmphr_reg.word0,
8038 bf_get(lpfc_port_smphr_perr, &portsmphr_reg),
8039 bf_get(lpfc_port_smphr_sfi, &portsmphr_reg),
8040 bf_get(lpfc_port_smphr_nip, &portsmphr_reg),
8041 bf_get(lpfc_port_smphr_ipc, &portsmphr_reg),
8042 bf_get(lpfc_port_smphr_scr1, &portsmphr_reg),
8043 bf_get(lpfc_port_smphr_scr2, &portsmphr_reg),
8044 bf_get(lpfc_port_smphr_host_scratch, &portsmphr_reg),
8045 bf_get(lpfc_port_smphr_port_status, &portsmphr_reg));
8046 } else {
James Smart28baac72010-02-12 14:42:03 -05008047 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -05008048 "2534 Device Info: SLIFamily=0x%x, "
8049 "SLIRev=0x%x, IFType=0x%x, SLIHint_1=0x%x, "
8050 "SLIHint_2=0x%x, FT=0x%x\n",
James Smart28baac72010-02-12 14:42:03 -05008051 bf_get(lpfc_sli_intf_sli_family,
8052 &phba->sli4_hba.sli_intf),
8053 bf_get(lpfc_sli_intf_slirev,
8054 &phba->sli4_hba.sli_intf),
James Smart085c6472010-11-20 23:11:37 -05008055 bf_get(lpfc_sli_intf_if_type,
James Smart28baac72010-02-12 14:42:03 -05008056 &phba->sli4_hba.sli_intf),
James Smart085c6472010-11-20 23:11:37 -05008057 bf_get(lpfc_sli_intf_sli_hint1,
8058 &phba->sli4_hba.sli_intf),
8059 bf_get(lpfc_sli_intf_sli_hint2,
8060 &phba->sli4_hba.sli_intf),
8061 bf_get(lpfc_sli_intf_func_type,
James Smart28baac72010-02-12 14:42:03 -05008062 &phba->sli4_hba.sli_intf));
James Smart2fcee4b2010-12-15 17:57:46 -05008063 /*
8064 * Check for other Port errors during the initialization
8065 * process. Fail the load if the port did not come up
8066 * correctly.
8067 */
8068 if_type = bf_get(lpfc_sli_intf_if_type,
8069 &phba->sli4_hba.sli_intf);
8070 switch (if_type) {
8071 case LPFC_SLI_INTF_IF_TYPE_0:
8072 phba->sli4_hba.ue_mask_lo =
8073 readl(phba->sli4_hba.u.if_type0.UEMASKLOregaddr);
8074 phba->sli4_hba.ue_mask_hi =
8075 readl(phba->sli4_hba.u.if_type0.UEMASKHIregaddr);
8076 uerrlo_reg.word0 =
8077 readl(phba->sli4_hba.u.if_type0.UERRLOregaddr);
8078 uerrhi_reg.word0 =
8079 readl(phba->sli4_hba.u.if_type0.UERRHIregaddr);
8080 if ((~phba->sli4_hba.ue_mask_lo & uerrlo_reg.word0) ||
8081 (~phba->sli4_hba.ue_mask_hi & uerrhi_reg.word0)) {
Dick Kennedy372c1872020-06-30 14:50:00 -07008082 lpfc_printf_log(phba, KERN_ERR,
8083 LOG_TRACE_EVENT,
James Smart2fcee4b2010-12-15 17:57:46 -05008084 "1422 Unrecoverable Error "
8085 "Detected during POST "
8086 "uerr_lo_reg=0x%x, "
8087 "uerr_hi_reg=0x%x, "
8088 "ue_mask_lo_reg=0x%x, "
8089 "ue_mask_hi_reg=0x%x\n",
8090 uerrlo_reg.word0,
8091 uerrhi_reg.word0,
8092 phba->sli4_hba.ue_mask_lo,
8093 phba->sli4_hba.ue_mask_hi);
8094 port_error = -ENODEV;
8095 }
8096 break;
8097 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart27d6ac02018-02-22 08:18:42 -08008098 case LPFC_SLI_INTF_IF_TYPE_6:
James Smart2fcee4b2010-12-15 17:57:46 -05008099 /* Final checks. The port status should be clean. */
James Smart9940b972011-03-11 16:06:12 -05008100 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
8101 &reg_data.word0) ||
James Smart05580562011-05-24 11:40:48 -04008102 (bf_get(lpfc_sliport_status_err, &reg_data) &&
8103 !bf_get(lpfc_sliport_status_rn, &reg_data))) {
James Smart2fcee4b2010-12-15 17:57:46 -05008104 phba->work_status[0] =
8105 readl(phba->sli4_hba.u.if_type2.
8106 ERR1regaddr);
8107 phba->work_status[1] =
8108 readl(phba->sli4_hba.u.if_type2.
8109 ERR2regaddr);
Dick Kennedy372c1872020-06-30 14:50:00 -07008110 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart8fcb8ac2012-03-01 22:35:58 -05008111 "2888 Unrecoverable port error "
8112 "following POST: port status reg "
8113 "0x%x, port_smphr reg 0x%x, "
James Smart2fcee4b2010-12-15 17:57:46 -05008114 "error 1=0x%x, error 2=0x%x\n",
8115 reg_data.word0,
8116 portsmphr_reg.word0,
8117 phba->work_status[0],
8118 phba->work_status[1]);
8119 port_error = -ENODEV;
8120 }
8121 break;
8122 case LPFC_SLI_INTF_IF_TYPE_1:
8123 default:
8124 break;
8125 }
James Smart28baac72010-02-12 14:42:03 -05008126 }
James Smartda0436e2009-05-22 14:51:39 -04008127 return port_error;
8128}
8129
8130/**
8131 * lpfc_sli4_bar0_register_memmap - Set up SLI4 BAR0 register memory map.
8132 * @phba: pointer to lpfc hba data structure.
James Smart2fcee4b2010-12-15 17:57:46 -05008133 * @if_type: The SLI4 interface type getting configured.
James Smartda0436e2009-05-22 14:51:39 -04008134 *
8135 * This routine is invoked to set up SLI4 BAR0 PCI config space register
8136 * memory map.
8137 **/
8138static void
James Smart2fcee4b2010-12-15 17:57:46 -05008139lpfc_sli4_bar0_register_memmap(struct lpfc_hba *phba, uint32_t if_type)
James Smartda0436e2009-05-22 14:51:39 -04008140{
James Smart2fcee4b2010-12-15 17:57:46 -05008141 switch (if_type) {
8142 case LPFC_SLI_INTF_IF_TYPE_0:
8143 phba->sli4_hba.u.if_type0.UERRLOregaddr =
8144 phba->sli4_hba.conf_regs_memmap_p + LPFC_UERR_STATUS_LO;
8145 phba->sli4_hba.u.if_type0.UERRHIregaddr =
8146 phba->sli4_hba.conf_regs_memmap_p + LPFC_UERR_STATUS_HI;
8147 phba->sli4_hba.u.if_type0.UEMASKLOregaddr =
8148 phba->sli4_hba.conf_regs_memmap_p + LPFC_UE_MASK_LO;
8149 phba->sli4_hba.u.if_type0.UEMASKHIregaddr =
8150 phba->sli4_hba.conf_regs_memmap_p + LPFC_UE_MASK_HI;
8151 phba->sli4_hba.SLIINTFregaddr =
8152 phba->sli4_hba.conf_regs_memmap_p + LPFC_SLI_INTF;
8153 break;
8154 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart0cf07f842017-06-01 21:07:10 -07008155 phba->sli4_hba.u.if_type2.EQDregaddr =
8156 phba->sli4_hba.conf_regs_memmap_p +
8157 LPFC_CTL_PORT_EQ_DELAY_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05008158 phba->sli4_hba.u.if_type2.ERR1regaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04008159 phba->sli4_hba.conf_regs_memmap_p +
8160 LPFC_CTL_PORT_ER1_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05008161 phba->sli4_hba.u.if_type2.ERR2regaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04008162 phba->sli4_hba.conf_regs_memmap_p +
8163 LPFC_CTL_PORT_ER2_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05008164 phba->sli4_hba.u.if_type2.CTRLregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04008165 phba->sli4_hba.conf_regs_memmap_p +
8166 LPFC_CTL_PORT_CTL_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05008167 phba->sli4_hba.u.if_type2.STATUSregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04008168 phba->sli4_hba.conf_regs_memmap_p +
8169 LPFC_CTL_PORT_STA_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05008170 phba->sli4_hba.SLIINTFregaddr =
8171 phba->sli4_hba.conf_regs_memmap_p + LPFC_SLI_INTF;
8172 phba->sli4_hba.PSMPHRregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04008173 phba->sli4_hba.conf_regs_memmap_p +
8174 LPFC_CTL_PORT_SEM_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05008175 phba->sli4_hba.RQDBregaddr =
James Smart962bc512013-01-03 15:44:00 -05008176 phba->sli4_hba.conf_regs_memmap_p +
8177 LPFC_ULP0_RQ_DOORBELL;
James Smart2fcee4b2010-12-15 17:57:46 -05008178 phba->sli4_hba.WQDBregaddr =
James Smart962bc512013-01-03 15:44:00 -05008179 phba->sli4_hba.conf_regs_memmap_p +
8180 LPFC_ULP0_WQ_DOORBELL;
James Smart9dd35422018-02-22 08:18:41 -08008181 phba->sli4_hba.CQDBregaddr =
James Smart2fcee4b2010-12-15 17:57:46 -05008182 phba->sli4_hba.conf_regs_memmap_p + LPFC_EQCQ_DOORBELL;
James Smart9dd35422018-02-22 08:18:41 -08008183 phba->sli4_hba.EQDBregaddr = phba->sli4_hba.CQDBregaddr;
James Smart2fcee4b2010-12-15 17:57:46 -05008184 phba->sli4_hba.MQDBregaddr =
8185 phba->sli4_hba.conf_regs_memmap_p + LPFC_MQ_DOORBELL;
8186 phba->sli4_hba.BMBXregaddr =
8187 phba->sli4_hba.conf_regs_memmap_p + LPFC_BMBX;
8188 break;
James Smart27d6ac02018-02-22 08:18:42 -08008189 case LPFC_SLI_INTF_IF_TYPE_6:
8190 phba->sli4_hba.u.if_type2.EQDregaddr =
8191 phba->sli4_hba.conf_regs_memmap_p +
8192 LPFC_CTL_PORT_EQ_DELAY_OFFSET;
8193 phba->sli4_hba.u.if_type2.ERR1regaddr =
8194 phba->sli4_hba.conf_regs_memmap_p +
8195 LPFC_CTL_PORT_ER1_OFFSET;
8196 phba->sli4_hba.u.if_type2.ERR2regaddr =
8197 phba->sli4_hba.conf_regs_memmap_p +
8198 LPFC_CTL_PORT_ER2_OFFSET;
8199 phba->sli4_hba.u.if_type2.CTRLregaddr =
8200 phba->sli4_hba.conf_regs_memmap_p +
8201 LPFC_CTL_PORT_CTL_OFFSET;
8202 phba->sli4_hba.u.if_type2.STATUSregaddr =
8203 phba->sli4_hba.conf_regs_memmap_p +
8204 LPFC_CTL_PORT_STA_OFFSET;
8205 phba->sli4_hba.PSMPHRregaddr =
8206 phba->sli4_hba.conf_regs_memmap_p +
8207 LPFC_CTL_PORT_SEM_OFFSET;
8208 phba->sli4_hba.BMBXregaddr =
8209 phba->sli4_hba.conf_regs_memmap_p + LPFC_BMBX;
8210 break;
James Smart2fcee4b2010-12-15 17:57:46 -05008211 case LPFC_SLI_INTF_IF_TYPE_1:
8212 default:
8213 dev_printk(KERN_ERR, &phba->pcidev->dev,
8214 "FATAL - unsupported SLI4 interface type - %d\n",
8215 if_type);
8216 break;
8217 }
James Smartda0436e2009-05-22 14:51:39 -04008218}
8219
8220/**
8221 * lpfc_sli4_bar1_register_memmap - Set up SLI4 BAR1 register memory map.
8222 * @phba: pointer to lpfc hba data structure.
Lee Jonesfe614ac2020-07-23 13:24:22 +01008223 * @if_type: sli if type to operate on.
James Smartda0436e2009-05-22 14:51:39 -04008224 *
James Smart27d6ac02018-02-22 08:18:42 -08008225 * This routine is invoked to set up SLI4 BAR1 register memory map.
James Smartda0436e2009-05-22 14:51:39 -04008226 **/
8227static void
James Smart27d6ac02018-02-22 08:18:42 -08008228lpfc_sli4_bar1_register_memmap(struct lpfc_hba *phba, uint32_t if_type)
James Smartda0436e2009-05-22 14:51:39 -04008229{
James Smart27d6ac02018-02-22 08:18:42 -08008230 switch (if_type) {
8231 case LPFC_SLI_INTF_IF_TYPE_0:
8232 phba->sli4_hba.PSMPHRregaddr =
8233 phba->sli4_hba.ctrl_regs_memmap_p +
8234 LPFC_SLIPORT_IF0_SMPHR;
8235 phba->sli4_hba.ISRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
8236 LPFC_HST_ISR0;
8237 phba->sli4_hba.IMRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
8238 LPFC_HST_IMR0;
8239 phba->sli4_hba.ISCRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
8240 LPFC_HST_ISCR0;
8241 break;
8242 case LPFC_SLI_INTF_IF_TYPE_6:
8243 phba->sli4_hba.RQDBregaddr = phba->sli4_hba.drbl_regs_memmap_p +
8244 LPFC_IF6_RQ_DOORBELL;
8245 phba->sli4_hba.WQDBregaddr = phba->sli4_hba.drbl_regs_memmap_p +
8246 LPFC_IF6_WQ_DOORBELL;
8247 phba->sli4_hba.CQDBregaddr = phba->sli4_hba.drbl_regs_memmap_p +
8248 LPFC_IF6_CQ_DOORBELL;
8249 phba->sli4_hba.EQDBregaddr = phba->sli4_hba.drbl_regs_memmap_p +
8250 LPFC_IF6_EQ_DOORBELL;
8251 phba->sli4_hba.MQDBregaddr = phba->sli4_hba.drbl_regs_memmap_p +
8252 LPFC_IF6_MQ_DOORBELL;
8253 break;
8254 case LPFC_SLI_INTF_IF_TYPE_2:
8255 case LPFC_SLI_INTF_IF_TYPE_1:
8256 default:
8257 dev_err(&phba->pcidev->dev,
8258 "FATAL - unsupported SLI4 interface type - %d\n",
8259 if_type);
8260 break;
8261 }
James Smartda0436e2009-05-22 14:51:39 -04008262}
8263
8264/**
8265 * lpfc_sli4_bar2_register_memmap - Set up SLI4 BAR2 register memory map.
8266 * @phba: pointer to lpfc hba data structure.
8267 * @vf: virtual function number
8268 *
8269 * This routine is invoked to set up SLI4 BAR2 doorbell register memory map
8270 * based on the given viftual function number, @vf.
8271 *
8272 * Return 0 if successful, otherwise -ENODEV.
8273 **/
8274static int
8275lpfc_sli4_bar2_register_memmap(struct lpfc_hba *phba, uint32_t vf)
8276{
8277 if (vf > LPFC_VIR_FUNC_MAX)
8278 return -ENODEV;
8279
8280 phba->sli4_hba.RQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
James Smart962bc512013-01-03 15:44:00 -05008281 vf * LPFC_VFR_PAGE_SIZE +
8282 LPFC_ULP0_RQ_DOORBELL);
James Smartda0436e2009-05-22 14:51:39 -04008283 phba->sli4_hba.WQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
James Smart962bc512013-01-03 15:44:00 -05008284 vf * LPFC_VFR_PAGE_SIZE +
8285 LPFC_ULP0_WQ_DOORBELL);
James Smart9dd35422018-02-22 08:18:41 -08008286 phba->sli4_hba.CQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
8287 vf * LPFC_VFR_PAGE_SIZE +
8288 LPFC_EQCQ_DOORBELL);
8289 phba->sli4_hba.EQDBregaddr = phba->sli4_hba.CQDBregaddr;
James Smartda0436e2009-05-22 14:51:39 -04008290 phba->sli4_hba.MQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
8291 vf * LPFC_VFR_PAGE_SIZE + LPFC_MQ_DOORBELL);
8292 phba->sli4_hba.BMBXregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
8293 vf * LPFC_VFR_PAGE_SIZE + LPFC_BMBX);
8294 return 0;
8295}
8296
8297/**
8298 * lpfc_create_bootstrap_mbox - Create the bootstrap mailbox
8299 * @phba: pointer to lpfc hba data structure.
8300 *
8301 * This routine is invoked to create the bootstrap mailbox
8302 * region consistent with the SLI-4 interface spec. This
8303 * routine allocates all memory necessary to communicate
8304 * mailbox commands to the port and sets up all alignment
8305 * needs. No locks are expected to be held when calling
8306 * this routine.
8307 *
8308 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008309 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04008310 * -ENOMEM - could not allocated memory.
James Smartda0436e2009-05-22 14:51:39 -04008311 **/
8312static int
8313lpfc_create_bootstrap_mbox(struct lpfc_hba *phba)
8314{
8315 uint32_t bmbx_size;
8316 struct lpfc_dmabuf *dmabuf;
8317 struct dma_address *dma_address;
8318 uint32_t pa_addr;
8319 uint64_t phys_addr;
8320
8321 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
8322 if (!dmabuf)
8323 return -ENOMEM;
8324
8325 /*
8326 * The bootstrap mailbox region is comprised of 2 parts
8327 * plus an alignment restriction of 16 bytes.
8328 */
8329 bmbx_size = sizeof(struct lpfc_bmbx_create) + (LPFC_ALIGN_16_BYTE - 1);
Luis Chamberlain750afb02019-01-04 09:23:09 +01008330 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev, bmbx_size,
8331 &dmabuf->phys, GFP_KERNEL);
James Smartda0436e2009-05-22 14:51:39 -04008332 if (!dmabuf->virt) {
8333 kfree(dmabuf);
8334 return -ENOMEM;
8335 }
James Smartda0436e2009-05-22 14:51:39 -04008336
8337 /*
8338 * Initialize the bootstrap mailbox pointers now so that the register
8339 * operations are simple later. The mailbox dma address is required
8340 * to be 16-byte aligned. Also align the virtual memory as each
8341 * maibox is copied into the bmbx mailbox region before issuing the
8342 * command to the port.
8343 */
8344 phba->sli4_hba.bmbx.dmabuf = dmabuf;
8345 phba->sli4_hba.bmbx.bmbx_size = bmbx_size;
8346
8347 phba->sli4_hba.bmbx.avirt = PTR_ALIGN(dmabuf->virt,
8348 LPFC_ALIGN_16_BYTE);
8349 phba->sli4_hba.bmbx.aphys = ALIGN(dmabuf->phys,
8350 LPFC_ALIGN_16_BYTE);
8351
8352 /*
8353 * Set the high and low physical addresses now. The SLI4 alignment
8354 * requirement is 16 bytes and the mailbox is posted to the port
8355 * as two 30-bit addresses. The other data is a bit marking whether
8356 * the 30-bit address is the high or low address.
8357 * Upcast bmbx aphys to 64bits so shift instruction compiles
8358 * clean on 32 bit machines.
8359 */
8360 dma_address = &phba->sli4_hba.bmbx.dma_address;
8361 phys_addr = (uint64_t)phba->sli4_hba.bmbx.aphys;
8362 pa_addr = (uint32_t) ((phys_addr >> 34) & 0x3fffffff);
8363 dma_address->addr_hi = (uint32_t) ((pa_addr << 2) |
8364 LPFC_BMBX_BIT1_ADDR_HI);
8365
8366 pa_addr = (uint32_t) ((phba->sli4_hba.bmbx.aphys >> 4) & 0x3fffffff);
8367 dma_address->addr_lo = (uint32_t) ((pa_addr << 2) |
8368 LPFC_BMBX_BIT1_ADDR_LO);
8369 return 0;
8370}
8371
8372/**
8373 * lpfc_destroy_bootstrap_mbox - Destroy all bootstrap mailbox resources
8374 * @phba: pointer to lpfc hba data structure.
8375 *
8376 * This routine is invoked to teardown the bootstrap mailbox
8377 * region and release all host resources. This routine requires
8378 * the caller to ensure all mailbox commands recovered, no
8379 * additional mailbox comands are sent, and interrupts are disabled
8380 * before calling this routine.
8381 *
8382 **/
8383static void
8384lpfc_destroy_bootstrap_mbox(struct lpfc_hba *phba)
8385{
8386 dma_free_coherent(&phba->pcidev->dev,
8387 phba->sli4_hba.bmbx.bmbx_size,
8388 phba->sli4_hba.bmbx.dmabuf->virt,
8389 phba->sli4_hba.bmbx.dmabuf->phys);
8390
8391 kfree(phba->sli4_hba.bmbx.dmabuf);
8392 memset(&phba->sli4_hba.bmbx, 0, sizeof(struct lpfc_bmbx));
8393}
8394
James Smart83c6cb12019-10-18 14:18:30 -07008395static const char * const lpfc_topo_to_str[] = {
8396 "Loop then P2P",
8397 "Loopback",
8398 "P2P Only",
8399 "Unsupported",
8400 "Loop Only",
8401 "Unsupported",
8402 "P2P then Loop",
8403};
8404
Lee Jonesfe614ac2020-07-23 13:24:22 +01008405#define LINK_FLAGS_DEF 0x0
8406#define LINK_FLAGS_P2P 0x1
8407#define LINK_FLAGS_LOOP 0x2
James Smart83c6cb12019-10-18 14:18:30 -07008408/**
8409 * lpfc_map_topology - Map the topology read from READ_CONFIG
8410 * @phba: pointer to lpfc hba data structure.
Lee Jonesfe614ac2020-07-23 13:24:22 +01008411 * @rd_config: pointer to read config data
James Smart83c6cb12019-10-18 14:18:30 -07008412 *
8413 * This routine is invoked to map the topology values as read
8414 * from the read config mailbox command. If the persistent
8415 * topology feature is supported, the firmware will provide the
8416 * saved topology information to be used in INIT_LINK
James Smart83c6cb12019-10-18 14:18:30 -07008417 **/
James Smart83c6cb12019-10-18 14:18:30 -07008418static void
8419lpfc_map_topology(struct lpfc_hba *phba, struct lpfc_mbx_read_config *rd_config)
8420{
8421 u8 ptv, tf, pt;
8422
8423 ptv = bf_get(lpfc_mbx_rd_conf_ptv, rd_config);
8424 tf = bf_get(lpfc_mbx_rd_conf_tf, rd_config);
8425 pt = bf_get(lpfc_mbx_rd_conf_pt, rd_config);
8426
8427 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8428 "2027 Read Config Data : ptv:0x%x, tf:0x%x pt:0x%x",
8429 ptv, tf, pt);
8430 if (!ptv) {
8431 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8432 "2019 FW does not support persistent topology "
8433 "Using driver parameter defined value [%s]",
8434 lpfc_topo_to_str[phba->cfg_topology]);
8435 return;
8436 }
8437 /* FW supports persistent topology - override module parameter value */
8438 phba->hba_flag |= HBA_PERSISTENT_TOPO;
8439 switch (phba->pcidev->device) {
8440 case PCI_DEVICE_ID_LANCER_G7_FC:
James Smart83c6cb12019-10-18 14:18:30 -07008441 case PCI_DEVICE_ID_LANCER_G6_FC:
8442 if (!tf) {
8443 phba->cfg_topology = ((pt == LINK_FLAGS_LOOP)
8444 ? FLAGS_TOPOLOGY_MODE_LOOP
8445 : FLAGS_TOPOLOGY_MODE_PT_PT);
8446 } else {
8447 phba->hba_flag &= ~HBA_PERSISTENT_TOPO;
8448 }
8449 break;
8450 default: /* G5 */
8451 if (tf) {
8452 /* If topology failover set - pt is '0' or '1' */
8453 phba->cfg_topology = (pt ? FLAGS_TOPOLOGY_MODE_PT_LOOP :
8454 FLAGS_TOPOLOGY_MODE_LOOP_PT);
8455 } else {
8456 phba->cfg_topology = ((pt == LINK_FLAGS_P2P)
8457 ? FLAGS_TOPOLOGY_MODE_PT_PT
8458 : FLAGS_TOPOLOGY_MODE_LOOP);
8459 }
8460 break;
8461 }
8462 if (phba->hba_flag & HBA_PERSISTENT_TOPO) {
8463 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8464 "2020 Using persistent topology value [%s]",
8465 lpfc_topo_to_str[phba->cfg_topology]);
8466 } else {
8467 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8468 "2021 Invalid topology values from FW "
8469 "Using driver parameter defined value [%s]",
8470 lpfc_topo_to_str[phba->cfg_topology]);
8471 }
8472}
8473
James Smartda0436e2009-05-22 14:51:39 -04008474/**
8475 * lpfc_sli4_read_config - Get the config parameters.
8476 * @phba: pointer to lpfc hba data structure.
8477 *
8478 * This routine is invoked to read the configuration parameters from the HBA.
8479 * The configuration parameters are used to set the base and maximum values
8480 * for RPI's XRI's VPI's VFI's and FCFIs. These values also affect the resource
8481 * allocation for the port.
8482 *
8483 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008484 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008485 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04008486 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04008487 **/
James Smartff78d8f2011-12-13 13:21:35 -05008488int
James Smartda0436e2009-05-22 14:51:39 -04008489lpfc_sli4_read_config(struct lpfc_hba *phba)
8490{
8491 LPFC_MBOXQ_t *pmb;
8492 struct lpfc_mbx_read_config *rd_config;
James Smart912e3ac2011-05-24 11:42:11 -04008493 union lpfc_sli4_cfg_shdr *shdr;
8494 uint32_t shdr_status, shdr_add_status;
8495 struct lpfc_mbx_get_func_cfg *get_func_cfg;
8496 struct lpfc_rsrc_desc_fcfcoe *desc;
James Smart8aa134a2012-08-14 14:25:29 -04008497 char *pdesc_0;
James Smartc6918162016-10-13 15:06:16 -07008498 uint16_t forced_link_speed;
James Smart6a828b02019-01-28 11:14:31 -08008499 uint32_t if_type, qmin;
James Smart8aa134a2012-08-14 14:25:29 -04008500 int length, i, rc = 0, rc2;
James Smartda0436e2009-05-22 14:51:39 -04008501
8502 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
8503 if (!pmb) {
Dick Kennedy372c1872020-06-30 14:50:00 -07008504 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartda0436e2009-05-22 14:51:39 -04008505 "2011 Unable to allocate memory for issuing "
8506 "SLI_CONFIG_SPECIAL mailbox command\n");
8507 return -ENOMEM;
8508 }
8509
8510 lpfc_read_config(phba, pmb);
8511
8512 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
8513 if (rc != MBX_SUCCESS) {
Dick Kennedy372c1872020-06-30 14:50:00 -07008514 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8515 "2012 Mailbox failed , mbxCmd x%x "
8516 "READ_CONFIG, mbxStatus x%x\n",
8517 bf_get(lpfc_mqe_command, &pmb->u.mqe),
8518 bf_get(lpfc_mqe_status, &pmb->u.mqe));
James Smartda0436e2009-05-22 14:51:39 -04008519 rc = -EIO;
8520 } else {
8521 rd_config = &pmb->u.mqe.un.rd_config;
James Smartff78d8f2011-12-13 13:21:35 -05008522 if (bf_get(lpfc_mbx_rd_conf_lnk_ldv, rd_config)) {
8523 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
8524 phba->sli4_hba.lnk_info.lnk_tp =
8525 bf_get(lpfc_mbx_rd_conf_lnk_type, rd_config);
8526 phba->sli4_hba.lnk_info.lnk_no =
8527 bf_get(lpfc_mbx_rd_conf_lnk_numb, rd_config);
8528 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8529 "3081 lnk_type:%d, lnk_numb:%d\n",
8530 phba->sli4_hba.lnk_info.lnk_tp,
8531 phba->sli4_hba.lnk_info.lnk_no);
8532 } else
8533 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8534 "3082 Mailbox (x%x) returned ldv:x0\n",
8535 bf_get(lpfc_mqe_command, &pmb->u.mqe));
James Smart44fd7fe2017-08-23 16:55:47 -07008536 if (bf_get(lpfc_mbx_rd_conf_bbscn_def, rd_config)) {
8537 phba->bbcredit_support = 1;
8538 phba->sli4_hba.bbscn_params.word0 = rd_config->word8;
8539 }
8540
James Smart1dc5ec22018-10-23 13:41:11 -07008541 phba->sli4_hba.conf_trunk =
8542 bf_get(lpfc_mbx_rd_conf_trunk, rd_config);
James Smart6d368e52011-05-24 11:44:12 -04008543 phba->sli4_hba.extents_in_use =
8544 bf_get(lpfc_mbx_rd_conf_extnts_inuse, rd_config);
James Smartda0436e2009-05-22 14:51:39 -04008545 phba->sli4_hba.max_cfg_param.max_xri =
8546 bf_get(lpfc_mbx_rd_conf_xri_count, rd_config);
James Smart31f06d22019-08-14 16:56:31 -07008547 /* Reduce resource usage in kdump environment */
8548 if (is_kdump_kernel() &&
8549 phba->sli4_hba.max_cfg_param.max_xri > 512)
8550 phba->sli4_hba.max_cfg_param.max_xri = 512;
James Smartda0436e2009-05-22 14:51:39 -04008551 phba->sli4_hba.max_cfg_param.xri_base =
8552 bf_get(lpfc_mbx_rd_conf_xri_base, rd_config);
8553 phba->sli4_hba.max_cfg_param.max_vpi =
8554 bf_get(lpfc_mbx_rd_conf_vpi_count, rd_config);
James Smart8b47ae62018-11-29 16:09:33 -08008555 /* Limit the max we support */
8556 if (phba->sli4_hba.max_cfg_param.max_vpi > LPFC_MAX_VPORTS)
8557 phba->sli4_hba.max_cfg_param.max_vpi = LPFC_MAX_VPORTS;
James Smartda0436e2009-05-22 14:51:39 -04008558 phba->sli4_hba.max_cfg_param.vpi_base =
8559 bf_get(lpfc_mbx_rd_conf_vpi_base, rd_config);
8560 phba->sli4_hba.max_cfg_param.max_rpi =
8561 bf_get(lpfc_mbx_rd_conf_rpi_count, rd_config);
8562 phba->sli4_hba.max_cfg_param.rpi_base =
8563 bf_get(lpfc_mbx_rd_conf_rpi_base, rd_config);
8564 phba->sli4_hba.max_cfg_param.max_vfi =
8565 bf_get(lpfc_mbx_rd_conf_vfi_count, rd_config);
8566 phba->sli4_hba.max_cfg_param.vfi_base =
8567 bf_get(lpfc_mbx_rd_conf_vfi_base, rd_config);
8568 phba->sli4_hba.max_cfg_param.max_fcfi =
8569 bf_get(lpfc_mbx_rd_conf_fcfi_count, rd_config);
James Smartda0436e2009-05-22 14:51:39 -04008570 phba->sli4_hba.max_cfg_param.max_eq =
8571 bf_get(lpfc_mbx_rd_conf_eq_count, rd_config);
8572 phba->sli4_hba.max_cfg_param.max_rq =
8573 bf_get(lpfc_mbx_rd_conf_rq_count, rd_config);
8574 phba->sli4_hba.max_cfg_param.max_wq =
8575 bf_get(lpfc_mbx_rd_conf_wq_count, rd_config);
8576 phba->sli4_hba.max_cfg_param.max_cq =
8577 bf_get(lpfc_mbx_rd_conf_cq_count, rd_config);
8578 phba->lmt = bf_get(lpfc_mbx_rd_conf_lmt, rd_config);
8579 phba->sli4_hba.next_xri = phba->sli4_hba.max_cfg_param.xri_base;
8580 phba->vpi_base = phba->sli4_hba.max_cfg_param.vpi_base;
8581 phba->vfi_base = phba->sli4_hba.max_cfg_param.vfi_base;
James Smart5ffc2662009-11-18 15:39:44 -05008582 phba->max_vpi = (phba->sli4_hba.max_cfg_param.max_vpi > 0) ?
8583 (phba->sli4_hba.max_cfg_param.max_vpi - 1) : 0;
James Smartda0436e2009-05-22 14:51:39 -04008584 phba->max_vports = phba->max_vpi;
James Smart83c6cb12019-10-18 14:18:30 -07008585 lpfc_map_topology(phba, rd_config);
James Smartda0436e2009-05-22 14:51:39 -04008586 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart6d368e52011-05-24 11:44:12 -04008587 "2003 cfg params Extents? %d "
8588 "XRI(B:%d M:%d), "
James Smartda0436e2009-05-22 14:51:39 -04008589 "VPI(B:%d M:%d) "
8590 "VFI(B:%d M:%d) "
8591 "RPI(B:%d M:%d) "
James Smart2ea259e2017-02-12 13:52:27 -08008592 "FCFI:%d EQ:%d CQ:%d WQ:%d RQ:%d\n",
James Smart6d368e52011-05-24 11:44:12 -04008593 phba->sli4_hba.extents_in_use,
James Smartda0436e2009-05-22 14:51:39 -04008594 phba->sli4_hba.max_cfg_param.xri_base,
8595 phba->sli4_hba.max_cfg_param.max_xri,
8596 phba->sli4_hba.max_cfg_param.vpi_base,
8597 phba->sli4_hba.max_cfg_param.max_vpi,
8598 phba->sli4_hba.max_cfg_param.vfi_base,
8599 phba->sli4_hba.max_cfg_param.max_vfi,
8600 phba->sli4_hba.max_cfg_param.rpi_base,
8601 phba->sli4_hba.max_cfg_param.max_rpi,
James Smart2ea259e2017-02-12 13:52:27 -08008602 phba->sli4_hba.max_cfg_param.max_fcfi,
8603 phba->sli4_hba.max_cfg_param.max_eq,
8604 phba->sli4_hba.max_cfg_param.max_cq,
8605 phba->sli4_hba.max_cfg_param.max_wq,
8606 phba->sli4_hba.max_cfg_param.max_rq);
8607
James Smartd38f33b2018-05-04 20:37:54 -07008608 /*
James Smart6a828b02019-01-28 11:14:31 -08008609 * Calculate queue resources based on how
8610 * many WQ/CQ/EQs are available.
James Smartd38f33b2018-05-04 20:37:54 -07008611 */
James Smart6a828b02019-01-28 11:14:31 -08008612 qmin = phba->sli4_hba.max_cfg_param.max_wq;
8613 if (phba->sli4_hba.max_cfg_param.max_cq < qmin)
8614 qmin = phba->sli4_hba.max_cfg_param.max_cq;
8615 if (phba->sli4_hba.max_cfg_param.max_eq < qmin)
8616 qmin = phba->sli4_hba.max_cfg_param.max_eq;
8617 /*
8618 * Whats left after this can go toward NVME / FCP.
8619 * The minus 4 accounts for ELS, NVME LS, MBOX
8620 * plus one extra. When configured for
8621 * NVMET, FCP io channel WQs are not created.
8622 */
8623 qmin -= 4;
James Smartd38f33b2018-05-04 20:37:54 -07008624
James Smart6a828b02019-01-28 11:14:31 -08008625 /* Check to see if there is enough for NVME */
8626 if ((phba->cfg_irq_chann > qmin) ||
8627 (phba->cfg_hdw_queue > qmin)) {
Dick Kennedy372c1872020-06-30 14:50:00 -07008628 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart6a828b02019-01-28 11:14:31 -08008629 "2005 Reducing Queues: "
8630 "WQ %d CQ %d EQ %d: min %d: "
8631 "IRQ %d HDWQ %d\n",
James Smartd38f33b2018-05-04 20:37:54 -07008632 phba->sli4_hba.max_cfg_param.max_wq,
8633 phba->sli4_hba.max_cfg_param.max_cq,
James Smart6a828b02019-01-28 11:14:31 -08008634 phba->sli4_hba.max_cfg_param.max_eq,
8635 qmin, phba->cfg_irq_chann,
James Smartcdb42be2019-01-28 11:14:21 -08008636 phba->cfg_hdw_queue);
James Smartd38f33b2018-05-04 20:37:54 -07008637
James Smart6a828b02019-01-28 11:14:31 -08008638 if (phba->cfg_irq_chann > qmin)
8639 phba->cfg_irq_chann = qmin;
8640 if (phba->cfg_hdw_queue > qmin)
8641 phba->cfg_hdw_queue = qmin;
James Smartd38f33b2018-05-04 20:37:54 -07008642 }
James Smartda0436e2009-05-22 14:51:39 -04008643 }
James Smart912e3ac2011-05-24 11:42:11 -04008644
8645 if (rc)
8646 goto read_cfg_out;
James Smartda0436e2009-05-22 14:51:39 -04008647
James Smartc6918162016-10-13 15:06:16 -07008648 /* Update link speed if forced link speed is supported */
8649 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
James Smart27d6ac02018-02-22 08:18:42 -08008650 if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
James Smartc6918162016-10-13 15:06:16 -07008651 forced_link_speed =
8652 bf_get(lpfc_mbx_rd_conf_link_speed, rd_config);
8653 if (forced_link_speed) {
8654 phba->hba_flag |= HBA_FORCED_LINK_SPEED;
8655
8656 switch (forced_link_speed) {
8657 case LINK_SPEED_1G:
8658 phba->cfg_link_speed =
8659 LPFC_USER_LINK_SPEED_1G;
8660 break;
8661 case LINK_SPEED_2G:
8662 phba->cfg_link_speed =
8663 LPFC_USER_LINK_SPEED_2G;
8664 break;
8665 case LINK_SPEED_4G:
8666 phba->cfg_link_speed =
8667 LPFC_USER_LINK_SPEED_4G;
8668 break;
8669 case LINK_SPEED_8G:
8670 phba->cfg_link_speed =
8671 LPFC_USER_LINK_SPEED_8G;
8672 break;
8673 case LINK_SPEED_10G:
8674 phba->cfg_link_speed =
8675 LPFC_USER_LINK_SPEED_10G;
8676 break;
8677 case LINK_SPEED_16G:
8678 phba->cfg_link_speed =
8679 LPFC_USER_LINK_SPEED_16G;
8680 break;
8681 case LINK_SPEED_32G:
8682 phba->cfg_link_speed =
8683 LPFC_USER_LINK_SPEED_32G;
8684 break;
James Smartfbd8a6b2018-02-22 08:18:45 -08008685 case LINK_SPEED_64G:
8686 phba->cfg_link_speed =
8687 LPFC_USER_LINK_SPEED_64G;
8688 break;
James Smartc6918162016-10-13 15:06:16 -07008689 case 0xffff:
8690 phba->cfg_link_speed =
8691 LPFC_USER_LINK_SPEED_AUTO;
8692 break;
8693 default:
Dick Kennedy372c1872020-06-30 14:50:00 -07008694 lpfc_printf_log(phba, KERN_ERR,
8695 LOG_TRACE_EVENT,
James Smartc6918162016-10-13 15:06:16 -07008696 "0047 Unrecognized link "
8697 "speed : %d\n",
8698 forced_link_speed);
8699 phba->cfg_link_speed =
8700 LPFC_USER_LINK_SPEED_AUTO;
8701 }
8702 }
8703 }
8704
James Smartda0436e2009-05-22 14:51:39 -04008705 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
James Smart572709e2013-07-15 18:32:43 -04008706 length = phba->sli4_hba.max_cfg_param.max_xri -
8707 lpfc_sli4_get_els_iocb_cnt(phba);
8708 if (phba->cfg_hba_queue_depth > length) {
8709 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
8710 "3361 HBA queue depth changed from %d to %d\n",
8711 phba->cfg_hba_queue_depth, length);
8712 phba->cfg_hba_queue_depth = length;
8713 }
James Smart912e3ac2011-05-24 11:42:11 -04008714
James Smart27d6ac02018-02-22 08:18:42 -08008715 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) <
James Smart912e3ac2011-05-24 11:42:11 -04008716 LPFC_SLI_INTF_IF_TYPE_2)
8717 goto read_cfg_out;
8718
8719 /* get the pf# and vf# for SLI4 if_type 2 port */
8720 length = (sizeof(struct lpfc_mbx_get_func_cfg) -
8721 sizeof(struct lpfc_sli4_cfg_mhdr));
8722 lpfc_sli4_config(phba, pmb, LPFC_MBOX_SUBSYSTEM_COMMON,
8723 LPFC_MBOX_OPCODE_GET_FUNCTION_CONFIG,
8724 length, LPFC_SLI4_MBX_EMBED);
8725
James Smart8aa134a2012-08-14 14:25:29 -04008726 rc2 = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart912e3ac2011-05-24 11:42:11 -04008727 shdr = (union lpfc_sli4_cfg_shdr *)
8728 &pmb->u.mqe.un.sli4_config.header.cfg_shdr;
8729 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
8730 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
James Smart8aa134a2012-08-14 14:25:29 -04008731 if (rc2 || shdr_status || shdr_add_status) {
Dick Kennedy372c1872020-06-30 14:50:00 -07008732 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart912e3ac2011-05-24 11:42:11 -04008733 "3026 Mailbox failed , mbxCmd x%x "
8734 "GET_FUNCTION_CONFIG, mbxStatus x%x\n",
8735 bf_get(lpfc_mqe_command, &pmb->u.mqe),
8736 bf_get(lpfc_mqe_status, &pmb->u.mqe));
James Smart912e3ac2011-05-24 11:42:11 -04008737 goto read_cfg_out;
8738 }
8739
8740 /* search for fc_fcoe resrouce descriptor */
8741 get_func_cfg = &pmb->u.mqe.un.get_func_cfg;
James Smart912e3ac2011-05-24 11:42:11 -04008742
James Smart8aa134a2012-08-14 14:25:29 -04008743 pdesc_0 = (char *)&get_func_cfg->func_cfg.desc[0];
8744 desc = (struct lpfc_rsrc_desc_fcfcoe *)pdesc_0;
8745 length = bf_get(lpfc_rsrc_desc_fcfcoe_length, desc);
8746 if (length == LPFC_RSRC_DESC_TYPE_FCFCOE_V0_RSVD)
8747 length = LPFC_RSRC_DESC_TYPE_FCFCOE_V0_LENGTH;
8748 else if (length != LPFC_RSRC_DESC_TYPE_FCFCOE_V1_LENGTH)
8749 goto read_cfg_out;
8750
James Smart912e3ac2011-05-24 11:42:11 -04008751 for (i = 0; i < LPFC_RSRC_DESC_MAX_NUM; i++) {
James Smart8aa134a2012-08-14 14:25:29 -04008752 desc = (struct lpfc_rsrc_desc_fcfcoe *)(pdesc_0 + length * i);
James Smart912e3ac2011-05-24 11:42:11 -04008753 if (LPFC_RSRC_DESC_TYPE_FCFCOE ==
James Smart8aa134a2012-08-14 14:25:29 -04008754 bf_get(lpfc_rsrc_desc_fcfcoe_type, desc)) {
James Smart912e3ac2011-05-24 11:42:11 -04008755 phba->sli4_hba.iov.pf_number =
8756 bf_get(lpfc_rsrc_desc_fcfcoe_pfnum, desc);
8757 phba->sli4_hba.iov.vf_number =
8758 bf_get(lpfc_rsrc_desc_fcfcoe_vfnum, desc);
8759 break;
8760 }
8761 }
8762
8763 if (i < LPFC_RSRC_DESC_MAX_NUM)
8764 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8765 "3027 GET_FUNCTION_CONFIG: pf_number:%d, "
8766 "vf_number:%d\n", phba->sli4_hba.iov.pf_number,
8767 phba->sli4_hba.iov.vf_number);
James Smart8aa134a2012-08-14 14:25:29 -04008768 else
Dick Kennedy372c1872020-06-30 14:50:00 -07008769 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart912e3ac2011-05-24 11:42:11 -04008770 "3028 GET_FUNCTION_CONFIG: failed to find "
Colin Ian Kingc4dba182018-10-16 18:28:53 +01008771 "Resource Descriptor:x%x\n",
James Smart912e3ac2011-05-24 11:42:11 -04008772 LPFC_RSRC_DESC_TYPE_FCFCOE);
James Smart912e3ac2011-05-24 11:42:11 -04008773
8774read_cfg_out:
8775 mempool_free(pmb, phba->mbox_mem_pool);
James Smartda0436e2009-05-22 14:51:39 -04008776 return rc;
8777}
8778
8779/**
James Smart2fcee4b2010-12-15 17:57:46 -05008780 * lpfc_setup_endian_order - Write endian order to an SLI4 if_type 0 port.
James Smartda0436e2009-05-22 14:51:39 -04008781 * @phba: pointer to lpfc hba data structure.
8782 *
James Smart2fcee4b2010-12-15 17:57:46 -05008783 * This routine is invoked to setup the port-side endian order when
8784 * the port if_type is 0. This routine has no function for other
8785 * if_types.
James Smartda0436e2009-05-22 14:51:39 -04008786 *
8787 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008788 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008789 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04008790 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04008791 **/
8792static int
8793lpfc_setup_endian_order(struct lpfc_hba *phba)
8794{
8795 LPFC_MBOXQ_t *mboxq;
James Smart2fcee4b2010-12-15 17:57:46 -05008796 uint32_t if_type, rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04008797 uint32_t endian_mb_data[2] = {HOST_ENDIAN_LOW_WORD0,
8798 HOST_ENDIAN_HIGH_WORD1};
8799
James Smart2fcee4b2010-12-15 17:57:46 -05008800 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
8801 switch (if_type) {
8802 case LPFC_SLI_INTF_IF_TYPE_0:
8803 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
8804 GFP_KERNEL);
8805 if (!mboxq) {
Dick Kennedy372c1872020-06-30 14:50:00 -07008806 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2fcee4b2010-12-15 17:57:46 -05008807 "0492 Unable to allocate memory for "
8808 "issuing SLI_CONFIG_SPECIAL mailbox "
8809 "command\n");
8810 return -ENOMEM;
8811 }
James Smartda0436e2009-05-22 14:51:39 -04008812
James Smart2fcee4b2010-12-15 17:57:46 -05008813 /*
8814 * The SLI4_CONFIG_SPECIAL mailbox command requires the first
8815 * two words to contain special data values and no other data.
8816 */
8817 memset(mboxq, 0, sizeof(LPFC_MBOXQ_t));
8818 memcpy(&mboxq->u.mqe, &endian_mb_data, sizeof(endian_mb_data));
8819 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8820 if (rc != MBX_SUCCESS) {
Dick Kennedy372c1872020-06-30 14:50:00 -07008821 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2fcee4b2010-12-15 17:57:46 -05008822 "0493 SLI_CONFIG_SPECIAL mailbox "
8823 "failed with status x%x\n",
8824 rc);
8825 rc = -EIO;
8826 }
8827 mempool_free(mboxq, phba->mbox_mem_pool);
8828 break;
James Smart27d6ac02018-02-22 08:18:42 -08008829 case LPFC_SLI_INTF_IF_TYPE_6:
James Smart2fcee4b2010-12-15 17:57:46 -05008830 case LPFC_SLI_INTF_IF_TYPE_2:
8831 case LPFC_SLI_INTF_IF_TYPE_1:
8832 default:
8833 break;
James Smartda0436e2009-05-22 14:51:39 -04008834 }
James Smartda0436e2009-05-22 14:51:39 -04008835 return rc;
8836}
8837
8838/**
James Smart895427b2017-02-12 13:52:30 -08008839 * lpfc_sli4_queue_verify - Verify and update EQ counts
James Smartda0436e2009-05-22 14:51:39 -04008840 * @phba: pointer to lpfc hba data structure.
8841 *
James Smart895427b2017-02-12 13:52:30 -08008842 * This routine is invoked to check the user settable queue counts for EQs.
8843 * After this routine is called the counts will be set to valid values that
James Smart5350d872011-10-10 21:33:49 -04008844 * adhere to the constraints of the system's interrupt vectors and the port's
8845 * queue resources.
James Smartda0436e2009-05-22 14:51:39 -04008846 *
8847 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008848 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008849 * -ENOMEM - No available memory
James Smartda0436e2009-05-22 14:51:39 -04008850 **/
8851static int
James Smart5350d872011-10-10 21:33:49 -04008852lpfc_sli4_queue_verify(struct lpfc_hba *phba)
James Smartda0436e2009-05-22 14:51:39 -04008853{
James Smartda0436e2009-05-22 14:51:39 -04008854 /*
James Smart67d12732012-08-03 12:36:13 -04008855 * Sanity check for configured queue parameters against the run-time
James Smartda0436e2009-05-22 14:51:39 -04008856 * device parameters
8857 */
8858
James Smartbcb24f62017-11-20 16:00:36 -08008859 if (phba->nvmet_support) {
James Smart97a9ed32019-10-18 14:18:17 -07008860 if (phba->cfg_hdw_queue < phba->cfg_nvmet_mrq)
8861 phba->cfg_nvmet_mrq = phba->cfg_hdw_queue;
James Smart982ab122019-03-12 16:30:10 -07008862 if (phba->cfg_nvmet_mrq > LPFC_NVMET_MRQ_MAX)
8863 phba->cfg_nvmet_mrq = LPFC_NVMET_MRQ_MAX;
James Smartbcb24f62017-11-20 16:00:36 -08008864 }
James Smart895427b2017-02-12 13:52:30 -08008865
8866 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart6a828b02019-01-28 11:14:31 -08008867 "2574 IO channels: hdwQ %d IRQ %d MRQ: %d\n",
8868 phba->cfg_hdw_queue, phba->cfg_irq_chann,
8869 phba->cfg_nvmet_mrq);
James Smartda0436e2009-05-22 14:51:39 -04008870
James Smartda0436e2009-05-22 14:51:39 -04008871 /* Get EQ depth from module parameter, fake the default for now */
8872 phba->sli4_hba.eq_esize = LPFC_EQE_SIZE_4B;
8873 phba->sli4_hba.eq_ecount = LPFC_EQE_DEF_COUNT;
8874
James Smart5350d872011-10-10 21:33:49 -04008875 /* Get CQ depth from module parameter, fake the default for now */
8876 phba->sli4_hba.cq_esize = LPFC_CQE_SIZE;
8877 phba->sli4_hba.cq_ecount = LPFC_CQE_DEF_COUNT;
James Smart5350d872011-10-10 21:33:49 -04008878 return 0;
James Smart895427b2017-02-12 13:52:30 -08008879}
8880
8881static int
James Smartc00f62e2019-08-14 16:57:11 -07008882lpfc_alloc_io_wq_cq(struct lpfc_hba *phba, int idx)
James Smart895427b2017-02-12 13:52:30 -08008883{
8884 struct lpfc_queue *qdesc;
James Smartc00f62e2019-08-14 16:57:11 -07008885 u32 wqesize;
James Smartc1a21eb2019-03-12 16:30:29 -07008886 int cpu;
James Smart895427b2017-02-12 13:52:30 -08008887
James Smartc00f62e2019-08-14 16:57:11 -07008888 cpu = lpfc_find_cpu_handle(phba, idx, LPFC_FIND_BY_HDWQ);
8889 /* Create Fast Path IO CQs */
James Smartc176ffa2018-01-30 15:58:46 -08008890 if (phba->enab_exp_wqcq_pages)
James Smarta51e41b2017-12-08 17:18:06 -08008891 /* Increase the CQ size when WQEs contain an embedded cdb */
8892 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_EXPANDED_PAGE_SIZE,
8893 phba->sli4_hba.cq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -07008894 LPFC_CQE_EXP_COUNT, cpu);
James Smarta51e41b2017-12-08 17:18:06 -08008895
8896 else
8897 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
8898 phba->sli4_hba.cq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -07008899 phba->sli4_hba.cq_ecount, cpu);
James Smart895427b2017-02-12 13:52:30 -08008900 if (!qdesc) {
Dick Kennedy372c1872020-06-30 14:50:00 -07008901 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
8902 "0499 Failed allocate fast-path IO CQ (%d)\n",
8903 idx);
James Smart895427b2017-02-12 13:52:30 -08008904 return 1;
8905 }
James Smart7365f6f2018-02-22 08:18:46 -08008906 qdesc->qe_valid = 1;
James Smartc00f62e2019-08-14 16:57:11 -07008907 qdesc->hdwq = idx;
James Smartc1a21eb2019-03-12 16:30:29 -07008908 qdesc->chann = cpu;
James Smartc00f62e2019-08-14 16:57:11 -07008909 phba->sli4_hba.hdwq[idx].io_cq = qdesc;
James Smart895427b2017-02-12 13:52:30 -08008910
James Smartc00f62e2019-08-14 16:57:11 -07008911 /* Create Fast Path IO WQs */
James Smartc176ffa2018-01-30 15:58:46 -08008912 if (phba->enab_exp_wqcq_pages) {
James Smarta51e41b2017-12-08 17:18:06 -08008913 /* Increase the WQ size when WQEs contain an embedded cdb */
James Smartc176ffa2018-01-30 15:58:46 -08008914 wqesize = (phba->fcp_embed_io) ?
8915 LPFC_WQE128_SIZE : phba->sli4_hba.wq_esize;
James Smarta51e41b2017-12-08 17:18:06 -08008916 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_EXPANDED_PAGE_SIZE,
James Smartc176ffa2018-01-30 15:58:46 -08008917 wqesize,
James Smartc1a21eb2019-03-12 16:30:29 -07008918 LPFC_WQE_EXP_COUNT, cpu);
James Smartc176ffa2018-01-30 15:58:46 -08008919 } else
James Smarta51e41b2017-12-08 17:18:06 -08008920 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
8921 phba->sli4_hba.wq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -07008922 phba->sli4_hba.wq_ecount, cpu);
James Smartc176ffa2018-01-30 15:58:46 -08008923
James Smart895427b2017-02-12 13:52:30 -08008924 if (!qdesc) {
Dick Kennedy372c1872020-06-30 14:50:00 -07008925 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartc00f62e2019-08-14 16:57:11 -07008926 "0503 Failed allocate fast-path IO WQ (%d)\n",
8927 idx);
James Smart895427b2017-02-12 13:52:30 -08008928 return 1;
8929 }
James Smartc00f62e2019-08-14 16:57:11 -07008930 qdesc->hdwq = idx;
8931 qdesc->chann = cpu;
8932 phba->sli4_hba.hdwq[idx].io_wq = qdesc;
James Smart895427b2017-02-12 13:52:30 -08008933 list_add_tail(&qdesc->wq_list, &phba->sli4_hba.lpfc_wq_list);
8934 return 0;
James Smart5350d872011-10-10 21:33:49 -04008935}
8936
8937/**
8938 * lpfc_sli4_queue_create - Create all the SLI4 queues
8939 * @phba: pointer to lpfc hba data structure.
8940 *
8941 * This routine is invoked to allocate all the SLI4 queues for the FCoE HBA
8942 * operation. For each SLI4 queue type, the parameters such as queue entry
8943 * count (queue depth) shall be taken from the module parameter. For now,
8944 * we just use some constant number as place holder.
8945 *
8946 * Return codes
Anatol Pomozov4907cb72012-09-01 10:31:09 -07008947 * 0 - successful
James Smart5350d872011-10-10 21:33:49 -04008948 * -ENOMEM - No availble memory
8949 * -EIO - The mailbox failed to complete successfully.
8950 **/
8951int
8952lpfc_sli4_queue_create(struct lpfc_hba *phba)
8953{
8954 struct lpfc_queue *qdesc;
James Smart657add42019-05-21 17:49:06 -07008955 int idx, cpu, eqcpu;
James Smart5e5b5112019-01-28 11:14:22 -08008956 struct lpfc_sli4_hdw_queue *qp;
James Smart657add42019-05-21 17:49:06 -07008957 struct lpfc_vector_map_info *cpup;
8958 struct lpfc_vector_map_info *eqcpup;
James Smart32517fc2019-01-28 11:14:33 -08008959 struct lpfc_eq_intr_info *eqi;
James Smart5350d872011-10-10 21:33:49 -04008960
8961 /*
James Smart67d12732012-08-03 12:36:13 -04008962 * Create HBA Record arrays.
James Smart895427b2017-02-12 13:52:30 -08008963 * Both NVME and FCP will share that same vectors / EQs
James Smart5350d872011-10-10 21:33:49 -04008964 */
James Smart67d12732012-08-03 12:36:13 -04008965 phba->sli4_hba.mq_esize = LPFC_MQE_SIZE;
8966 phba->sli4_hba.mq_ecount = LPFC_MQE_DEF_COUNT;
8967 phba->sli4_hba.wq_esize = LPFC_WQE_SIZE;
8968 phba->sli4_hba.wq_ecount = LPFC_WQE_DEF_COUNT;
8969 phba->sli4_hba.rq_esize = LPFC_RQE_SIZE;
8970 phba->sli4_hba.rq_ecount = LPFC_RQE_DEF_COUNT;
James Smart895427b2017-02-12 13:52:30 -08008971 phba->sli4_hba.eq_esize = LPFC_EQE_SIZE_4B;
8972 phba->sli4_hba.eq_ecount = LPFC_EQE_DEF_COUNT;
8973 phba->sli4_hba.cq_esize = LPFC_CQE_SIZE;
8974 phba->sli4_hba.cq_ecount = LPFC_CQE_DEF_COUNT;
James Smart67d12732012-08-03 12:36:13 -04008975
James Smartcdb42be2019-01-28 11:14:21 -08008976 if (!phba->sli4_hba.hdwq) {
James Smart5e5b5112019-01-28 11:14:22 -08008977 phba->sli4_hba.hdwq = kcalloc(
8978 phba->cfg_hdw_queue, sizeof(struct lpfc_sli4_hdw_queue),
8979 GFP_KERNEL);
8980 if (!phba->sli4_hba.hdwq) {
Dick Kennedy372c1872020-06-30 14:50:00 -07008981 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart5e5b5112019-01-28 11:14:22 -08008982 "6427 Failed allocate memory for "
8983 "fast-path Hardware Queue array\n");
James Smart895427b2017-02-12 13:52:30 -08008984 goto out_error;
8985 }
James Smart5e5b5112019-01-28 11:14:22 -08008986 /* Prepare hardware queues to take IO buffers */
8987 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
8988 qp = &phba->sli4_hba.hdwq[idx];
8989 spin_lock_init(&qp->io_buf_list_get_lock);
8990 spin_lock_init(&qp->io_buf_list_put_lock);
8991 INIT_LIST_HEAD(&qp->lpfc_io_buf_list_get);
8992 INIT_LIST_HEAD(&qp->lpfc_io_buf_list_put);
8993 qp->get_io_bufs = 0;
8994 qp->put_io_bufs = 0;
8995 qp->total_io_bufs = 0;
James Smartc00f62e2019-08-14 16:57:11 -07008996 spin_lock_init(&qp->abts_io_buf_list_lock);
8997 INIT_LIST_HEAD(&qp->lpfc_abts_io_buf_list);
James Smart5e5b5112019-01-28 11:14:22 -08008998 qp->abts_scsi_io_bufs = 0;
James Smart5e5b5112019-01-28 11:14:22 -08008999 qp->abts_nvme_io_bufs = 0;
James Smartd79c9e92019-08-14 16:57:09 -07009000 INIT_LIST_HEAD(&qp->sgl_list);
9001 INIT_LIST_HEAD(&qp->cmd_rsp_buf_list);
9002 spin_lock_init(&qp->hdwq_lock);
James Smart895427b2017-02-12 13:52:30 -08009003 }
James Smart67d12732012-08-03 12:36:13 -04009004 }
9005
James Smartcdb42be2019-01-28 11:14:21 -08009006 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
James Smart2d7dbc42017-02-12 13:52:35 -08009007 if (phba->nvmet_support) {
9008 phba->sli4_hba.nvmet_cqset = kcalloc(
9009 phba->cfg_nvmet_mrq,
9010 sizeof(struct lpfc_queue *),
9011 GFP_KERNEL);
9012 if (!phba->sli4_hba.nvmet_cqset) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009013 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2d7dbc42017-02-12 13:52:35 -08009014 "3121 Fail allocate memory for "
9015 "fast-path CQ set array\n");
9016 goto out_error;
9017 }
9018 phba->sli4_hba.nvmet_mrq_hdr = kcalloc(
9019 phba->cfg_nvmet_mrq,
9020 sizeof(struct lpfc_queue *),
9021 GFP_KERNEL);
9022 if (!phba->sli4_hba.nvmet_mrq_hdr) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009023 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2d7dbc42017-02-12 13:52:35 -08009024 "3122 Fail allocate memory for "
9025 "fast-path RQ set hdr array\n");
9026 goto out_error;
9027 }
9028 phba->sli4_hba.nvmet_mrq_data = kcalloc(
9029 phba->cfg_nvmet_mrq,
9030 sizeof(struct lpfc_queue *),
9031 GFP_KERNEL);
9032 if (!phba->sli4_hba.nvmet_mrq_data) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009033 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2d7dbc42017-02-12 13:52:35 -08009034 "3124 Fail allocate memory for "
9035 "fast-path RQ set data array\n");
9036 goto out_error;
9037 }
9038 }
James Smart67d12732012-08-03 12:36:13 -04009039 }
James Smartda0436e2009-05-22 14:51:39 -04009040
James Smart895427b2017-02-12 13:52:30 -08009041 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_wq_list);
James Smart67d12732012-08-03 12:36:13 -04009042
James Smart895427b2017-02-12 13:52:30 -08009043 /* Create HBA Event Queues (EQs) */
James Smart657add42019-05-21 17:49:06 -07009044 for_each_present_cpu(cpu) {
9045 /* We only want to create 1 EQ per vector, even though
9046 * multiple CPUs might be using that vector. so only
9047 * selects the CPUs that are LPFC_CPU_FIRST_IRQ.
James Smart6a828b02019-01-28 11:14:31 -08009048 */
James Smart657add42019-05-21 17:49:06 -07009049 cpup = &phba->sli4_hba.cpu_map[cpu];
9050 if (!(cpup->flag & LPFC_CPU_FIRST_IRQ))
James Smart6a828b02019-01-28 11:14:31 -08009051 continue;
James Smart657add42019-05-21 17:49:06 -07009052
9053 /* Get a ptr to the Hardware Queue associated with this CPU */
9054 qp = &phba->sli4_hba.hdwq[cpup->hdwq];
9055
9056 /* Allocate an EQ */
James Smart81b96ed2017-11-20 16:00:29 -08009057 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
9058 phba->sli4_hba.eq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -07009059 phba->sli4_hba.eq_ecount, cpu);
James Smartda0436e2009-05-22 14:51:39 -04009060 if (!qdesc) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009061 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart657add42019-05-21 17:49:06 -07009062 "0497 Failed allocate EQ (%d)\n",
9063 cpup->hdwq);
James Smart67d12732012-08-03 12:36:13 -04009064 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04009065 }
James Smart7365f6f2018-02-22 08:18:46 -08009066 qdesc->qe_valid = 1;
James Smart657add42019-05-21 17:49:06 -07009067 qdesc->hdwq = cpup->hdwq;
James Smart3ad348d2019-08-14 16:56:43 -07009068 qdesc->chann = cpu; /* First CPU this EQ is affinitized to */
James Smart32517fc2019-01-28 11:14:33 -08009069 qdesc->last_cpu = qdesc->chann;
James Smart657add42019-05-21 17:49:06 -07009070
9071 /* Save the allocated EQ in the Hardware Queue */
9072 qp->hba_eq = qdesc;
9073
James Smart32517fc2019-01-28 11:14:33 -08009074 eqi = per_cpu_ptr(phba->sli4_hba.eq_info, qdesc->last_cpu);
9075 list_add(&qdesc->cpu_list, &eqi->list);
James Smartda0436e2009-05-22 14:51:39 -04009076 }
9077
James Smart657add42019-05-21 17:49:06 -07009078 /* Now we need to populate the other Hardware Queues, that share
9079 * an IRQ vector, with the associated EQ ptr.
9080 */
9081 for_each_present_cpu(cpu) {
9082 cpup = &phba->sli4_hba.cpu_map[cpu];
9083
9084 /* Check for EQ already allocated in previous loop */
9085 if (cpup->flag & LPFC_CPU_FIRST_IRQ)
9086 continue;
9087
9088 /* Check for multiple CPUs per hdwq */
9089 qp = &phba->sli4_hba.hdwq[cpup->hdwq];
9090 if (qp->hba_eq)
9091 continue;
9092
9093 /* We need to share an EQ for this hdwq */
9094 eqcpu = lpfc_find_cpu_handle(phba, cpup->eq, LPFC_FIND_BY_EQ);
9095 eqcpup = &phba->sli4_hba.cpu_map[eqcpu];
9096 qp->hba_eq = phba->sli4_hba.hdwq[eqcpup->hdwq].hba_eq;
9097 }
James Smart895427b2017-02-12 13:52:30 -08009098
James Smartc00f62e2019-08-14 16:57:11 -07009099 /* Allocate IO Path SLI4 CQ/WQs */
James Smart6a828b02019-01-28 11:14:31 -08009100 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
James Smartc00f62e2019-08-14 16:57:11 -07009101 if (lpfc_alloc_io_wq_cq(phba, idx))
James Smart895427b2017-02-12 13:52:30 -08009102 goto out_error;
James Smart6a828b02019-01-28 11:14:31 -08009103 }
James Smart895427b2017-02-12 13:52:30 -08009104
James Smartc00f62e2019-08-14 16:57:11 -07009105 if (phba->nvmet_support) {
9106 for (idx = 0; idx < phba->cfg_nvmet_mrq; idx++) {
9107 cpu = lpfc_find_cpu_handle(phba, idx,
9108 LPFC_FIND_BY_HDWQ);
9109 qdesc = lpfc_sli4_queue_alloc(phba,
James Smart81b96ed2017-11-20 16:00:29 -08009110 LPFC_DEFAULT_PAGE_SIZE,
9111 phba->sli4_hba.cq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -07009112 phba->sli4_hba.cq_ecount,
9113 cpu);
James Smartc00f62e2019-08-14 16:57:11 -07009114 if (!qdesc) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009115 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartcdb42be2019-01-28 11:14:21 -08009116 "3142 Failed allocate NVME "
9117 "CQ Set (%d)\n", idx);
James Smartc00f62e2019-08-14 16:57:11 -07009118 goto out_error;
James Smart2d7dbc42017-02-12 13:52:35 -08009119 }
James Smartc00f62e2019-08-14 16:57:11 -07009120 qdesc->qe_valid = 1;
9121 qdesc->hdwq = idx;
9122 qdesc->chann = cpu;
9123 phba->sli4_hba.nvmet_cqset[idx] = qdesc;
James Smart2d7dbc42017-02-12 13:52:35 -08009124 }
9125 }
9126
James Smartda0436e2009-05-22 14:51:39 -04009127 /*
James Smart67d12732012-08-03 12:36:13 -04009128 * Create Slow Path Completion Queues (CQs)
James Smartda0436e2009-05-22 14:51:39 -04009129 */
9130
James Smartc1a21eb2019-03-12 16:30:29 -07009131 cpu = lpfc_find_cpu_handle(phba, 0, LPFC_FIND_BY_EQ);
James Smartda0436e2009-05-22 14:51:39 -04009132 /* Create slow-path Mailbox Command Complete Queue */
James Smart81b96ed2017-11-20 16:00:29 -08009133 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
9134 phba->sli4_hba.cq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -07009135 phba->sli4_hba.cq_ecount, cpu);
James Smartda0436e2009-05-22 14:51:39 -04009136 if (!qdesc) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009137 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartda0436e2009-05-22 14:51:39 -04009138 "0500 Failed allocate slow-path mailbox CQ\n");
James Smart67d12732012-08-03 12:36:13 -04009139 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04009140 }
James Smart7365f6f2018-02-22 08:18:46 -08009141 qdesc->qe_valid = 1;
James Smartda0436e2009-05-22 14:51:39 -04009142 phba->sli4_hba.mbx_cq = qdesc;
9143
9144 /* Create slow-path ELS Complete Queue */
James Smart81b96ed2017-11-20 16:00:29 -08009145 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
9146 phba->sli4_hba.cq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -07009147 phba->sli4_hba.cq_ecount, cpu);
James Smartda0436e2009-05-22 14:51:39 -04009148 if (!qdesc) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009149 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartda0436e2009-05-22 14:51:39 -04009150 "0501 Failed allocate slow-path ELS CQ\n");
James Smart67d12732012-08-03 12:36:13 -04009151 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04009152 }
James Smart7365f6f2018-02-22 08:18:46 -08009153 qdesc->qe_valid = 1;
James Smartc00f62e2019-08-14 16:57:11 -07009154 qdesc->chann = cpu;
James Smartda0436e2009-05-22 14:51:39 -04009155 phba->sli4_hba.els_cq = qdesc;
9156
James Smartda0436e2009-05-22 14:51:39 -04009157
James Smart5350d872011-10-10 21:33:49 -04009158 /*
James Smart67d12732012-08-03 12:36:13 -04009159 * Create Slow Path Work Queues (WQs)
James Smart5350d872011-10-10 21:33:49 -04009160 */
James Smartda0436e2009-05-22 14:51:39 -04009161
9162 /* Create Mailbox Command Queue */
James Smartda0436e2009-05-22 14:51:39 -04009163
James Smart81b96ed2017-11-20 16:00:29 -08009164 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
9165 phba->sli4_hba.mq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -07009166 phba->sli4_hba.mq_ecount, cpu);
James Smartda0436e2009-05-22 14:51:39 -04009167 if (!qdesc) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009168 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartda0436e2009-05-22 14:51:39 -04009169 "0505 Failed allocate slow-path MQ\n");
James Smart67d12732012-08-03 12:36:13 -04009170 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04009171 }
James Smartc00f62e2019-08-14 16:57:11 -07009172 qdesc->chann = cpu;
James Smartda0436e2009-05-22 14:51:39 -04009173 phba->sli4_hba.mbx_wq = qdesc;
9174
9175 /*
James Smart67d12732012-08-03 12:36:13 -04009176 * Create ELS Work Queues
James Smartda0436e2009-05-22 14:51:39 -04009177 */
James Smartda0436e2009-05-22 14:51:39 -04009178
9179 /* Create slow-path ELS Work Queue */
James Smart81b96ed2017-11-20 16:00:29 -08009180 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
9181 phba->sli4_hba.wq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -07009182 phba->sli4_hba.wq_ecount, cpu);
James Smartda0436e2009-05-22 14:51:39 -04009183 if (!qdesc) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009184 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartda0436e2009-05-22 14:51:39 -04009185 "0504 Failed allocate slow-path ELS WQ\n");
James Smart67d12732012-08-03 12:36:13 -04009186 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04009187 }
James Smartc00f62e2019-08-14 16:57:11 -07009188 qdesc->chann = cpu;
James Smartda0436e2009-05-22 14:51:39 -04009189 phba->sli4_hba.els_wq = qdesc;
James Smart895427b2017-02-12 13:52:30 -08009190 list_add_tail(&qdesc->wq_list, &phba->sli4_hba.lpfc_wq_list);
9191
9192 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
9193 /* Create NVME LS Complete Queue */
James Smart81b96ed2017-11-20 16:00:29 -08009194 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
9195 phba->sli4_hba.cq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -07009196 phba->sli4_hba.cq_ecount, cpu);
James Smart895427b2017-02-12 13:52:30 -08009197 if (!qdesc) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009198 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart895427b2017-02-12 13:52:30 -08009199 "6079 Failed allocate NVME LS CQ\n");
9200 goto out_error;
9201 }
James Smartc00f62e2019-08-14 16:57:11 -07009202 qdesc->chann = cpu;
James Smart7365f6f2018-02-22 08:18:46 -08009203 qdesc->qe_valid = 1;
James Smart895427b2017-02-12 13:52:30 -08009204 phba->sli4_hba.nvmels_cq = qdesc;
9205
9206 /* Create NVME LS Work Queue */
James Smart81b96ed2017-11-20 16:00:29 -08009207 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
9208 phba->sli4_hba.wq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -07009209 phba->sli4_hba.wq_ecount, cpu);
James Smart895427b2017-02-12 13:52:30 -08009210 if (!qdesc) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009211 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart895427b2017-02-12 13:52:30 -08009212 "6080 Failed allocate NVME LS WQ\n");
9213 goto out_error;
9214 }
James Smartc00f62e2019-08-14 16:57:11 -07009215 qdesc->chann = cpu;
James Smart895427b2017-02-12 13:52:30 -08009216 phba->sli4_hba.nvmels_wq = qdesc;
9217 list_add_tail(&qdesc->wq_list, &phba->sli4_hba.lpfc_wq_list);
9218 }
James Smartda0436e2009-05-22 14:51:39 -04009219
James Smartda0436e2009-05-22 14:51:39 -04009220 /*
9221 * Create Receive Queue (RQ)
9222 */
James Smartda0436e2009-05-22 14:51:39 -04009223
9224 /* Create Receive Queue for header */
James Smart81b96ed2017-11-20 16:00:29 -08009225 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
9226 phba->sli4_hba.rq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -07009227 phba->sli4_hba.rq_ecount, cpu);
James Smartda0436e2009-05-22 14:51:39 -04009228 if (!qdesc) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009229 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartda0436e2009-05-22 14:51:39 -04009230 "0506 Failed allocate receive HRQ\n");
James Smart67d12732012-08-03 12:36:13 -04009231 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04009232 }
9233 phba->sli4_hba.hdr_rq = qdesc;
9234
9235 /* Create Receive Queue for data */
James Smart81b96ed2017-11-20 16:00:29 -08009236 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
9237 phba->sli4_hba.rq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -07009238 phba->sli4_hba.rq_ecount, cpu);
James Smartda0436e2009-05-22 14:51:39 -04009239 if (!qdesc) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009240 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartda0436e2009-05-22 14:51:39 -04009241 "0507 Failed allocate receive DRQ\n");
James Smart67d12732012-08-03 12:36:13 -04009242 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04009243 }
9244 phba->sli4_hba.dat_rq = qdesc;
9245
James Smartcdb42be2019-01-28 11:14:21 -08009246 if ((phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) &&
9247 phba->nvmet_support) {
James Smart2d7dbc42017-02-12 13:52:35 -08009248 for (idx = 0; idx < phba->cfg_nvmet_mrq; idx++) {
James Smartc1a21eb2019-03-12 16:30:29 -07009249 cpu = lpfc_find_cpu_handle(phba, idx,
9250 LPFC_FIND_BY_HDWQ);
James Smart2d7dbc42017-02-12 13:52:35 -08009251 /* Create NVMET Receive Queue for header */
9252 qdesc = lpfc_sli4_queue_alloc(phba,
James Smart81b96ed2017-11-20 16:00:29 -08009253 LPFC_DEFAULT_PAGE_SIZE,
James Smart2d7dbc42017-02-12 13:52:35 -08009254 phba->sli4_hba.rq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -07009255 LPFC_NVMET_RQE_DEF_COUNT,
9256 cpu);
James Smart2d7dbc42017-02-12 13:52:35 -08009257 if (!qdesc) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009258 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2d7dbc42017-02-12 13:52:35 -08009259 "3146 Failed allocate "
9260 "receive HRQ\n");
9261 goto out_error;
9262 }
James Smart5e5b5112019-01-28 11:14:22 -08009263 qdesc->hdwq = idx;
James Smart2d7dbc42017-02-12 13:52:35 -08009264 phba->sli4_hba.nvmet_mrq_hdr[idx] = qdesc;
9265
9266 /* Only needed for header of RQ pair */
James Smartc1a21eb2019-03-12 16:30:29 -07009267 qdesc->rqbp = kzalloc_node(sizeof(*qdesc->rqbp),
9268 GFP_KERNEL,
9269 cpu_to_node(cpu));
James Smart2d7dbc42017-02-12 13:52:35 -08009270 if (qdesc->rqbp == NULL) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009271 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2d7dbc42017-02-12 13:52:35 -08009272 "6131 Failed allocate "
9273 "Header RQBP\n");
9274 goto out_error;
9275 }
9276
Dick Kennedy4b40d022017-08-23 16:55:38 -07009277 /* Put list in known state in case driver load fails. */
9278 INIT_LIST_HEAD(&qdesc->rqbp->rqb_buffer_list);
9279
James Smart2d7dbc42017-02-12 13:52:35 -08009280 /* Create NVMET Receive Queue for data */
9281 qdesc = lpfc_sli4_queue_alloc(phba,
James Smart81b96ed2017-11-20 16:00:29 -08009282 LPFC_DEFAULT_PAGE_SIZE,
James Smart2d7dbc42017-02-12 13:52:35 -08009283 phba->sli4_hba.rq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -07009284 LPFC_NVMET_RQE_DEF_COUNT,
9285 cpu);
James Smart2d7dbc42017-02-12 13:52:35 -08009286 if (!qdesc) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009287 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2d7dbc42017-02-12 13:52:35 -08009288 "3156 Failed allocate "
9289 "receive DRQ\n");
9290 goto out_error;
9291 }
James Smart5e5b5112019-01-28 11:14:22 -08009292 qdesc->hdwq = idx;
James Smart2d7dbc42017-02-12 13:52:35 -08009293 phba->sli4_hba.nvmet_mrq_data[idx] = qdesc;
9294 }
9295 }
9296
James Smart4c47efc2019-01-28 11:14:25 -08009297 /* Clear NVME stats */
9298 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
9299 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
9300 memset(&phba->sli4_hba.hdwq[idx].nvme_cstat, 0,
9301 sizeof(phba->sli4_hba.hdwq[idx].nvme_cstat));
9302 }
9303 }
James Smart4c47efc2019-01-28 11:14:25 -08009304
9305 /* Clear SCSI stats */
9306 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) {
9307 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
9308 memset(&phba->sli4_hba.hdwq[idx].scsi_cstat, 0,
9309 sizeof(phba->sli4_hba.hdwq[idx].scsi_cstat));
9310 }
9311 }
9312
James Smartda0436e2009-05-22 14:51:39 -04009313 return 0;
9314
James Smartda0436e2009-05-22 14:51:39 -04009315out_error:
James Smart67d12732012-08-03 12:36:13 -04009316 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04009317 return -ENOMEM;
9318}
9319
James Smart895427b2017-02-12 13:52:30 -08009320static inline void
9321__lpfc_sli4_release_queue(struct lpfc_queue **qp)
9322{
9323 if (*qp != NULL) {
9324 lpfc_sli4_queue_free(*qp);
9325 *qp = NULL;
9326 }
9327}
9328
9329static inline void
9330lpfc_sli4_release_queues(struct lpfc_queue ***qs, int max)
9331{
9332 int idx;
9333
9334 if (*qs == NULL)
9335 return;
9336
9337 for (idx = 0; idx < max; idx++)
9338 __lpfc_sli4_release_queue(&(*qs)[idx]);
9339
9340 kfree(*qs);
9341 *qs = NULL;
9342}
9343
9344static inline void
James Smart6a828b02019-01-28 11:14:31 -08009345lpfc_sli4_release_hdwq(struct lpfc_hba *phba)
James Smart895427b2017-02-12 13:52:30 -08009346{
James Smart6a828b02019-01-28 11:14:31 -08009347 struct lpfc_sli4_hdw_queue *hdwq;
James Smart657add42019-05-21 17:49:06 -07009348 struct lpfc_queue *eq;
James Smartcdb42be2019-01-28 11:14:21 -08009349 uint32_t idx;
9350
James Smart6a828b02019-01-28 11:14:31 -08009351 hdwq = phba->sli4_hba.hdwq;
James Smart6a828b02019-01-28 11:14:31 -08009352
James Smart657add42019-05-21 17:49:06 -07009353 /* Loop thru all Hardware Queues */
9354 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
9355 /* Free the CQ/WQ corresponding to the Hardware Queue */
James Smartc00f62e2019-08-14 16:57:11 -07009356 lpfc_sli4_queue_free(hdwq[idx].io_cq);
9357 lpfc_sli4_queue_free(hdwq[idx].io_wq);
James Smart821bc882020-01-27 16:23:05 -08009358 hdwq[idx].hba_eq = NULL;
James Smartc00f62e2019-08-14 16:57:11 -07009359 hdwq[idx].io_cq = NULL;
9360 hdwq[idx].io_wq = NULL;
James Smartd79c9e92019-08-14 16:57:09 -07009361 if (phba->cfg_xpsgl && !phba->nvmet_support)
9362 lpfc_free_sgl_per_hdwq(phba, &hdwq[idx]);
9363 lpfc_free_cmd_rsp_buf_per_hdwq(phba, &hdwq[idx]);
James Smart895427b2017-02-12 13:52:30 -08009364 }
James Smart657add42019-05-21 17:49:06 -07009365 /* Loop thru all IRQ vectors */
9366 for (idx = 0; idx < phba->cfg_irq_chann; idx++) {
9367 /* Free the EQ corresponding to the IRQ vector */
9368 eq = phba->sli4_hba.hba_eq_hdl[idx].eq;
9369 lpfc_sli4_queue_free(eq);
9370 phba->sli4_hba.hba_eq_hdl[idx].eq = NULL;
9371 }
James Smart895427b2017-02-12 13:52:30 -08009372}
9373
James Smartda0436e2009-05-22 14:51:39 -04009374/**
9375 * lpfc_sli4_queue_destroy - Destroy all the SLI4 queues
9376 * @phba: pointer to lpfc hba data structure.
9377 *
9378 * This routine is invoked to release all the SLI4 queues with the FCoE HBA
9379 * operation.
9380 *
9381 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02009382 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009383 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04009384 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04009385 **/
James Smart5350d872011-10-10 21:33:49 -04009386void
James Smartda0436e2009-05-22 14:51:39 -04009387lpfc_sli4_queue_destroy(struct lpfc_hba *phba)
9388{
James Smart4645f7b2019-03-12 16:30:14 -07009389 /*
9390 * Set FREE_INIT before beginning to free the queues.
9391 * Wait until the users of queues to acknowledge to
9392 * release queues by clearing FREE_WAIT.
9393 */
9394 spin_lock_irq(&phba->hbalock);
9395 phba->sli.sli_flag |= LPFC_QUEUE_FREE_INIT;
9396 while (phba->sli.sli_flag & LPFC_QUEUE_FREE_WAIT) {
9397 spin_unlock_irq(&phba->hbalock);
9398 msleep(20);
9399 spin_lock_irq(&phba->hbalock);
9400 }
9401 spin_unlock_irq(&phba->hbalock);
9402
James Smart93a4d6f2019-11-04 16:57:05 -08009403 lpfc_sli4_cleanup_poll_list(phba);
9404
James Smart895427b2017-02-12 13:52:30 -08009405 /* Release HBA eqs */
James Smartcdb42be2019-01-28 11:14:21 -08009406 if (phba->sli4_hba.hdwq)
James Smart6a828b02019-01-28 11:14:31 -08009407 lpfc_sli4_release_hdwq(phba);
James Smartda0436e2009-05-22 14:51:39 -04009408
James Smartbcb24f62017-11-20 16:00:36 -08009409 if (phba->nvmet_support) {
9410 lpfc_sli4_release_queues(&phba->sli4_hba.nvmet_cqset,
9411 phba->cfg_nvmet_mrq);
James Smart2d7dbc42017-02-12 13:52:35 -08009412
James Smartbcb24f62017-11-20 16:00:36 -08009413 lpfc_sli4_release_queues(&phba->sli4_hba.nvmet_mrq_hdr,
9414 phba->cfg_nvmet_mrq);
9415 lpfc_sli4_release_queues(&phba->sli4_hba.nvmet_mrq_data,
9416 phba->cfg_nvmet_mrq);
9417 }
James Smart2d7dbc42017-02-12 13:52:35 -08009418
James Smartda0436e2009-05-22 14:51:39 -04009419 /* Release mailbox command work queue */
James Smart895427b2017-02-12 13:52:30 -08009420 __lpfc_sli4_release_queue(&phba->sli4_hba.mbx_wq);
James Smartda0436e2009-05-22 14:51:39 -04009421
9422 /* Release ELS work queue */
James Smart895427b2017-02-12 13:52:30 -08009423 __lpfc_sli4_release_queue(&phba->sli4_hba.els_wq);
9424
9425 /* Release ELS work queue */
9426 __lpfc_sli4_release_queue(&phba->sli4_hba.nvmels_wq);
James Smartda0436e2009-05-22 14:51:39 -04009427
9428 /* Release unsolicited receive queue */
James Smart895427b2017-02-12 13:52:30 -08009429 __lpfc_sli4_release_queue(&phba->sli4_hba.hdr_rq);
9430 __lpfc_sli4_release_queue(&phba->sli4_hba.dat_rq);
James Smartda0436e2009-05-22 14:51:39 -04009431
James Smartda0436e2009-05-22 14:51:39 -04009432 /* Release ELS complete queue */
James Smart895427b2017-02-12 13:52:30 -08009433 __lpfc_sli4_release_queue(&phba->sli4_hba.els_cq);
9434
9435 /* Release NVME LS complete queue */
9436 __lpfc_sli4_release_queue(&phba->sli4_hba.nvmels_cq);
James Smartda0436e2009-05-22 14:51:39 -04009437
9438 /* Release mailbox command complete queue */
James Smart895427b2017-02-12 13:52:30 -08009439 __lpfc_sli4_release_queue(&phba->sli4_hba.mbx_cq);
9440
9441 /* Everything on this list has been freed */
9442 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_wq_list);
James Smart4645f7b2019-03-12 16:30:14 -07009443
9444 /* Done with freeing the queues */
9445 spin_lock_irq(&phba->hbalock);
9446 phba->sli.sli_flag &= ~LPFC_QUEUE_FREE_INIT;
9447 spin_unlock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -08009448}
9449
9450int
James Smart895427b2017-02-12 13:52:30 -08009451lpfc_free_rq_buffer(struct lpfc_hba *phba, struct lpfc_queue *rq)
9452{
9453 struct lpfc_rqb *rqbp;
9454 struct lpfc_dmabuf *h_buf;
9455 struct rqb_dmabuf *rqb_buffer;
9456
9457 rqbp = rq->rqbp;
9458 while (!list_empty(&rqbp->rqb_buffer_list)) {
9459 list_remove_head(&rqbp->rqb_buffer_list, h_buf,
9460 struct lpfc_dmabuf, list);
9461
9462 rqb_buffer = container_of(h_buf, struct rqb_dmabuf, hbuf);
9463 (rqbp->rqb_free_buffer)(phba, rqb_buffer);
9464 rqbp->buffer_count--;
9465 }
9466 return 1;
9467}
9468
9469static int
9470lpfc_create_wq_cq(struct lpfc_hba *phba, struct lpfc_queue *eq,
9471 struct lpfc_queue *cq, struct lpfc_queue *wq, uint16_t *cq_map,
9472 int qidx, uint32_t qtype)
9473{
9474 struct lpfc_sli_ring *pring;
9475 int rc;
9476
9477 if (!eq || !cq || !wq) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009478 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart895427b2017-02-12 13:52:30 -08009479 "6085 Fast-path %s (%d) not allocated\n",
9480 ((eq) ? ((cq) ? "WQ" : "CQ") : "EQ"), qidx);
9481 return -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04009482 }
James Smartda0436e2009-05-22 14:51:39 -04009483
James Smart895427b2017-02-12 13:52:30 -08009484 /* create the Cq first */
9485 rc = lpfc_cq_create(phba, cq, eq,
9486 (qtype == LPFC_MBOX) ? LPFC_MCQ : LPFC_WCQ, qtype);
9487 if (rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009488 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
9489 "6086 Failed setup of CQ (%d), rc = 0x%x\n",
9490 qidx, (uint32_t)rc);
James Smart895427b2017-02-12 13:52:30 -08009491 return rc;
9492 }
9493
9494 if (qtype != LPFC_MBOX) {
James Smartcdb42be2019-01-28 11:14:21 -08009495 /* Setup cq_map for fast lookup */
James Smart895427b2017-02-12 13:52:30 -08009496 if (cq_map)
9497 *cq_map = cq->queue_id;
9498
9499 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9500 "6087 CQ setup: cq[%d]-id=%d, parent eq[%d]-id=%d\n",
9501 qidx, cq->queue_id, qidx, eq->queue_id);
9502
9503 /* create the wq */
9504 rc = lpfc_wq_create(phba, wq, cq, qtype);
9505 if (rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009506 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartc835c082019-03-12 16:30:30 -07009507 "4618 Fail setup fastpath WQ (%d), rc = 0x%x\n",
James Smart895427b2017-02-12 13:52:30 -08009508 qidx, (uint32_t)rc);
9509 /* no need to tear down cq - caller will do so */
9510 return rc;
9511 }
9512
9513 /* Bind this CQ/WQ to the NVME ring */
9514 pring = wq->pring;
9515 pring->sli.sli4.wqp = (void *)wq;
9516 cq->pring = pring;
9517
9518 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9519 "2593 WQ setup: wq[%d]-id=%d assoc=%d, cq[%d]-id=%d\n",
9520 qidx, wq->queue_id, wq->assoc_qid, qidx, cq->queue_id);
9521 } else {
9522 rc = lpfc_mq_create(phba, wq, cq, LPFC_MBOX);
9523 if (rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009524 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
9525 "0539 Failed setup of slow-path MQ: "
9526 "rc = 0x%x\n", rc);
James Smart895427b2017-02-12 13:52:30 -08009527 /* no need to tear down cq - caller will do so */
9528 return rc;
9529 }
9530
9531 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9532 "2589 MBX MQ setup: wq-id=%d, parent cq-id=%d\n",
9533 phba->sli4_hba.mbx_wq->queue_id,
9534 phba->sli4_hba.mbx_cq->queue_id);
9535 }
9536
9537 return 0;
James Smartda0436e2009-05-22 14:51:39 -04009538}
9539
9540/**
James Smart6a828b02019-01-28 11:14:31 -08009541 * lpfc_setup_cq_lookup - Setup the CQ lookup table
9542 * @phba: pointer to lpfc hba data structure.
9543 *
9544 * This routine will populate the cq_lookup table by all
9545 * available CQ queue_id's.
9546 **/
Bart Van Assche3999df72019-03-28 11:06:16 -07009547static void
James Smart6a828b02019-01-28 11:14:31 -08009548lpfc_setup_cq_lookup(struct lpfc_hba *phba)
9549{
9550 struct lpfc_queue *eq, *childq;
James Smart6a828b02019-01-28 11:14:31 -08009551 int qidx;
9552
James Smart6a828b02019-01-28 11:14:31 -08009553 memset(phba->sli4_hba.cq_lookup, 0,
9554 (sizeof(struct lpfc_queue *) * (phba->sli4_hba.cq_max + 1)));
James Smart657add42019-05-21 17:49:06 -07009555 /* Loop thru all IRQ vectors */
James Smart6a828b02019-01-28 11:14:31 -08009556 for (qidx = 0; qidx < phba->cfg_irq_chann; qidx++) {
James Smart657add42019-05-21 17:49:06 -07009557 /* Get the EQ corresponding to the IRQ vector */
9558 eq = phba->sli4_hba.hba_eq_hdl[qidx].eq;
James Smart6a828b02019-01-28 11:14:31 -08009559 if (!eq)
9560 continue;
James Smart657add42019-05-21 17:49:06 -07009561 /* Loop through all CQs associated with that EQ */
James Smart6a828b02019-01-28 11:14:31 -08009562 list_for_each_entry(childq, &eq->child_list, list) {
9563 if (childq->queue_id > phba->sli4_hba.cq_max)
9564 continue;
James Smartc00f62e2019-08-14 16:57:11 -07009565 if (childq->subtype == LPFC_IO)
James Smart6a828b02019-01-28 11:14:31 -08009566 phba->sli4_hba.cq_lookup[childq->queue_id] =
9567 childq;
9568 }
9569 }
9570}
9571
9572/**
James Smartda0436e2009-05-22 14:51:39 -04009573 * lpfc_sli4_queue_setup - Set up all the SLI4 queues
9574 * @phba: pointer to lpfc hba data structure.
9575 *
9576 * This routine is invoked to set up all the SLI4 queues for the FCoE HBA
9577 * operation.
9578 *
9579 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02009580 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009581 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04009582 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04009583 **/
9584int
9585lpfc_sli4_queue_setup(struct lpfc_hba *phba)
9586{
James Smart962bc512013-01-03 15:44:00 -05009587 uint32_t shdr_status, shdr_add_status;
9588 union lpfc_sli4_cfg_shdr *shdr;
James Smart657add42019-05-21 17:49:06 -07009589 struct lpfc_vector_map_info *cpup;
James Smartcdb42be2019-01-28 11:14:21 -08009590 struct lpfc_sli4_hdw_queue *qp;
James Smart962bc512013-01-03 15:44:00 -05009591 LPFC_MBOXQ_t *mboxq;
James Smart657add42019-05-21 17:49:06 -07009592 int qidx, cpu;
James Smartcb733e32019-01-28 11:14:32 -08009593 uint32_t length, usdelay;
James Smart895427b2017-02-12 13:52:30 -08009594 int rc = -ENOMEM;
James Smart962bc512013-01-03 15:44:00 -05009595
9596 /* Check for dual-ULP support */
9597 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9598 if (!mboxq) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009599 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart962bc512013-01-03 15:44:00 -05009600 "3249 Unable to allocate memory for "
9601 "QUERY_FW_CFG mailbox command\n");
9602 return -ENOMEM;
9603 }
9604 length = (sizeof(struct lpfc_mbx_query_fw_config) -
9605 sizeof(struct lpfc_sli4_cfg_mhdr));
9606 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
9607 LPFC_MBOX_OPCODE_QUERY_FW_CFG,
9608 length, LPFC_SLI4_MBX_EMBED);
9609
9610 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
9611
9612 shdr = (union lpfc_sli4_cfg_shdr *)
9613 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
9614 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9615 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9616 if (shdr_status || shdr_add_status || rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009617 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart962bc512013-01-03 15:44:00 -05009618 "3250 QUERY_FW_CFG mailbox failed with status "
9619 "x%x add_status x%x, mbx status x%x\n",
9620 shdr_status, shdr_add_status, rc);
9621 if (rc != MBX_TIMEOUT)
9622 mempool_free(mboxq, phba->mbox_mem_pool);
9623 rc = -ENXIO;
9624 goto out_error;
9625 }
9626
9627 phba->sli4_hba.fw_func_mode =
9628 mboxq->u.mqe.un.query_fw_cfg.rsp.function_mode;
9629 phba->sli4_hba.ulp0_mode = mboxq->u.mqe.un.query_fw_cfg.rsp.ulp0_mode;
9630 phba->sli4_hba.ulp1_mode = mboxq->u.mqe.un.query_fw_cfg.rsp.ulp1_mode;
James Smart8b017a32015-05-21 13:55:18 -04009631 phba->sli4_hba.physical_port =
9632 mboxq->u.mqe.un.query_fw_cfg.rsp.physical_port;
James Smart962bc512013-01-03 15:44:00 -05009633 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9634 "3251 QUERY_FW_CFG: func_mode:x%x, ulp0_mode:x%x, "
9635 "ulp1_mode:x%x\n", phba->sli4_hba.fw_func_mode,
9636 phba->sli4_hba.ulp0_mode, phba->sli4_hba.ulp1_mode);
9637
9638 if (rc != MBX_TIMEOUT)
9639 mempool_free(mboxq, phba->mbox_mem_pool);
James Smartda0436e2009-05-22 14:51:39 -04009640
9641 /*
James Smart67d12732012-08-03 12:36:13 -04009642 * Set up HBA Event Queues (EQs)
James Smartda0436e2009-05-22 14:51:39 -04009643 */
James Smartcdb42be2019-01-28 11:14:21 -08009644 qp = phba->sli4_hba.hdwq;
James Smartda0436e2009-05-22 14:51:39 -04009645
James Smart67d12732012-08-03 12:36:13 -04009646 /* Set up HBA event queue */
James Smartcdb42be2019-01-28 11:14:21 -08009647 if (!qp) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009648 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2e90f4b2011-12-13 13:22:37 -05009649 "3147 Fast-path EQs not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05009650 rc = -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04009651 goto out_error;
James Smart2e90f4b2011-12-13 13:22:37 -05009652 }
James Smart657add42019-05-21 17:49:06 -07009653
9654 /* Loop thru all IRQ vectors */
James Smart6a828b02019-01-28 11:14:31 -08009655 for (qidx = 0; qidx < phba->cfg_irq_chann; qidx++) {
James Smart657add42019-05-21 17:49:06 -07009656 /* Create HBA Event Queues (EQs) in order */
9657 for_each_present_cpu(cpu) {
9658 cpup = &phba->sli4_hba.cpu_map[cpu];
9659
9660 /* Look for the CPU thats using that vector with
9661 * LPFC_CPU_FIRST_IRQ set.
9662 */
9663 if (!(cpup->flag & LPFC_CPU_FIRST_IRQ))
9664 continue;
9665 if (qidx != cpup->eq)
9666 continue;
9667
9668 /* Create an EQ for that vector */
9669 rc = lpfc_eq_create(phba, qp[cpup->hdwq].hba_eq,
9670 phba->cfg_fcp_imax);
9671 if (rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009672 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart657add42019-05-21 17:49:06 -07009673 "0523 Failed setup of fast-path"
9674 " EQ (%d), rc = 0x%x\n",
9675 cpup->eq, (uint32_t)rc);
9676 goto out_destroy;
9677 }
9678
9679 /* Save the EQ for that vector in the hba_eq_hdl */
9680 phba->sli4_hba.hba_eq_hdl[cpup->eq].eq =
9681 qp[cpup->hdwq].hba_eq;
9682
9683 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9684 "2584 HBA EQ setup: queue[%d]-id=%d\n",
9685 cpup->eq,
9686 qp[cpup->hdwq].hba_eq->queue_id);
James Smartda0436e2009-05-22 14:51:39 -04009687 }
James Smartda0436e2009-05-22 14:51:39 -04009688 }
9689
James Smart657add42019-05-21 17:49:06 -07009690 /* Loop thru all Hardware Queues */
James Smartcdb42be2019-01-28 11:14:21 -08009691 for (qidx = 0; qidx < phba->cfg_hdw_queue; qidx++) {
James Smart657add42019-05-21 17:49:06 -07009692 cpu = lpfc_find_cpu_handle(phba, qidx, LPFC_FIND_BY_HDWQ);
9693 cpup = &phba->sli4_hba.cpu_map[cpu];
9694
9695 /* Create the CQ/WQ corresponding to the Hardware Queue */
James Smartcdb42be2019-01-28 11:14:21 -08009696 rc = lpfc_create_wq_cq(phba,
James Smart657add42019-05-21 17:49:06 -07009697 phba->sli4_hba.hdwq[cpup->hdwq].hba_eq,
James Smartc00f62e2019-08-14 16:57:11 -07009698 qp[qidx].io_cq,
9699 qp[qidx].io_wq,
9700 &phba->sli4_hba.hdwq[qidx].io_cq_map,
9701 qidx,
9702 LPFC_IO);
James Smartcdb42be2019-01-28 11:14:21 -08009703 if (rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009704 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart895427b2017-02-12 13:52:30 -08009705 "0535 Failed to setup fastpath "
James Smartc00f62e2019-08-14 16:57:11 -07009706 "IO WQ/CQ (%d), rc = 0x%x\n",
James Smart895427b2017-02-12 13:52:30 -08009707 qidx, (uint32_t)rc);
James Smartcdb42be2019-01-28 11:14:21 -08009708 goto out_destroy;
James Smart895427b2017-02-12 13:52:30 -08009709 }
James Smart67d12732012-08-03 12:36:13 -04009710 }
James Smartda0436e2009-05-22 14:51:39 -04009711
9712 /*
James Smart895427b2017-02-12 13:52:30 -08009713 * Set up Slow Path Complete Queues (CQs)
James Smartda0436e2009-05-22 14:51:39 -04009714 */
9715
James Smart895427b2017-02-12 13:52:30 -08009716 /* Set up slow-path MBOX CQ/MQ */
9717
9718 if (!phba->sli4_hba.mbx_cq || !phba->sli4_hba.mbx_wq) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009719 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart895427b2017-02-12 13:52:30 -08009720 "0528 %s not allocated\n",
9721 phba->sli4_hba.mbx_cq ?
James Smartd1f525a2017-04-21 16:04:55 -07009722 "Mailbox WQ" : "Mailbox CQ");
James Smart1b511972011-12-13 13:23:09 -05009723 rc = -ENOMEM;
James Smart895427b2017-02-12 13:52:30 -08009724 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -04009725 }
James Smart895427b2017-02-12 13:52:30 -08009726
James Smartcdb42be2019-01-28 11:14:21 -08009727 rc = lpfc_create_wq_cq(phba, qp[0].hba_eq,
James Smartd1f525a2017-04-21 16:04:55 -07009728 phba->sli4_hba.mbx_cq,
9729 phba->sli4_hba.mbx_wq,
9730 NULL, 0, LPFC_MBOX);
James Smartda0436e2009-05-22 14:51:39 -04009731 if (rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009732 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart895427b2017-02-12 13:52:30 -08009733 "0529 Failed setup of mailbox WQ/CQ: rc = 0x%x\n",
9734 (uint32_t)rc);
9735 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -04009736 }
James Smart2d7dbc42017-02-12 13:52:35 -08009737 if (phba->nvmet_support) {
9738 if (!phba->sli4_hba.nvmet_cqset) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009739 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2d7dbc42017-02-12 13:52:35 -08009740 "3165 Fast-path NVME CQ Set "
9741 "array not allocated\n");
9742 rc = -ENOMEM;
9743 goto out_destroy;
9744 }
9745 if (phba->cfg_nvmet_mrq > 1) {
9746 rc = lpfc_cq_create_set(phba,
9747 phba->sli4_hba.nvmet_cqset,
James Smartcdb42be2019-01-28 11:14:21 -08009748 qp,
James Smart2d7dbc42017-02-12 13:52:35 -08009749 LPFC_WCQ, LPFC_NVMET);
9750 if (rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009751 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2d7dbc42017-02-12 13:52:35 -08009752 "3164 Failed setup of NVME CQ "
9753 "Set, rc = 0x%x\n",
9754 (uint32_t)rc);
9755 goto out_destroy;
9756 }
9757 } else {
9758 /* Set up NVMET Receive Complete Queue */
9759 rc = lpfc_cq_create(phba, phba->sli4_hba.nvmet_cqset[0],
James Smartcdb42be2019-01-28 11:14:21 -08009760 qp[0].hba_eq,
James Smart2d7dbc42017-02-12 13:52:35 -08009761 LPFC_WCQ, LPFC_NVMET);
9762 if (rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009763 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2d7dbc42017-02-12 13:52:35 -08009764 "6089 Failed setup NVMET CQ: "
9765 "rc = 0x%x\n", (uint32_t)rc);
9766 goto out_destroy;
9767 }
James Smart81b96ed2017-11-20 16:00:29 -08009768 phba->sli4_hba.nvmet_cqset[0]->chann = 0;
9769
James Smart2d7dbc42017-02-12 13:52:35 -08009770 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9771 "6090 NVMET CQ setup: cq-id=%d, "
9772 "parent eq-id=%d\n",
9773 phba->sli4_hba.nvmet_cqset[0]->queue_id,
James Smartcdb42be2019-01-28 11:14:21 -08009774 qp[0].hba_eq->queue_id);
James Smart2d7dbc42017-02-12 13:52:35 -08009775 }
9776 }
James Smartda0436e2009-05-22 14:51:39 -04009777
James Smart895427b2017-02-12 13:52:30 -08009778 /* Set up slow-path ELS WQ/CQ */
9779 if (!phba->sli4_hba.els_cq || !phba->sli4_hba.els_wq) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009780 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart895427b2017-02-12 13:52:30 -08009781 "0530 ELS %s not allocated\n",
9782 phba->sli4_hba.els_cq ? "WQ" : "CQ");
James Smart1b511972011-12-13 13:23:09 -05009783 rc = -ENOMEM;
James Smart895427b2017-02-12 13:52:30 -08009784 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -04009785 }
James Smartcdb42be2019-01-28 11:14:21 -08009786 rc = lpfc_create_wq_cq(phba, qp[0].hba_eq,
9787 phba->sli4_hba.els_cq,
9788 phba->sli4_hba.els_wq,
9789 NULL, 0, LPFC_ELS);
James Smartda0436e2009-05-22 14:51:39 -04009790 if (rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009791 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartcdb42be2019-01-28 11:14:21 -08009792 "0525 Failed setup of ELS WQ/CQ: rc = 0x%x\n",
9793 (uint32_t)rc);
James Smart895427b2017-02-12 13:52:30 -08009794 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -04009795 }
9796 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9797 "2590 ELS WQ setup: wq-id=%d, parent cq-id=%d\n",
9798 phba->sli4_hba.els_wq->queue_id,
9799 phba->sli4_hba.els_cq->queue_id);
9800
James Smartcdb42be2019-01-28 11:14:21 -08009801 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
James Smart895427b2017-02-12 13:52:30 -08009802 /* Set up NVME LS Complete Queue */
9803 if (!phba->sli4_hba.nvmels_cq || !phba->sli4_hba.nvmels_wq) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009804 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart895427b2017-02-12 13:52:30 -08009805 "6091 LS %s not allocated\n",
9806 phba->sli4_hba.nvmels_cq ? "WQ" : "CQ");
9807 rc = -ENOMEM;
9808 goto out_destroy;
9809 }
James Smartcdb42be2019-01-28 11:14:21 -08009810 rc = lpfc_create_wq_cq(phba, qp[0].hba_eq,
9811 phba->sli4_hba.nvmels_cq,
9812 phba->sli4_hba.nvmels_wq,
9813 NULL, 0, LPFC_NVME_LS);
James Smart895427b2017-02-12 13:52:30 -08009814 if (rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009815 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartcdb42be2019-01-28 11:14:21 -08009816 "0526 Failed setup of NVVME LS WQ/CQ: "
9817 "rc = 0x%x\n", (uint32_t)rc);
James Smart895427b2017-02-12 13:52:30 -08009818 goto out_destroy;
9819 }
9820
9821 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9822 "6096 ELS WQ setup: wq-id=%d, "
9823 "parent cq-id=%d\n",
9824 phba->sli4_hba.nvmels_wq->queue_id,
9825 phba->sli4_hba.nvmels_cq->queue_id);
9826 }
9827
James Smart2d7dbc42017-02-12 13:52:35 -08009828 /*
9829 * Create NVMET Receive Queue (RQ)
9830 */
9831 if (phba->nvmet_support) {
9832 if ((!phba->sli4_hba.nvmet_cqset) ||
9833 (!phba->sli4_hba.nvmet_mrq_hdr) ||
9834 (!phba->sli4_hba.nvmet_mrq_data)) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009835 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2d7dbc42017-02-12 13:52:35 -08009836 "6130 MRQ CQ Queues not "
9837 "allocated\n");
9838 rc = -ENOMEM;
9839 goto out_destroy;
9840 }
9841 if (phba->cfg_nvmet_mrq > 1) {
9842 rc = lpfc_mrq_create(phba,
9843 phba->sli4_hba.nvmet_mrq_hdr,
9844 phba->sli4_hba.nvmet_mrq_data,
9845 phba->sli4_hba.nvmet_cqset,
9846 LPFC_NVMET);
9847 if (rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009848 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2d7dbc42017-02-12 13:52:35 -08009849 "6098 Failed setup of NVMET "
9850 "MRQ: rc = 0x%x\n",
9851 (uint32_t)rc);
9852 goto out_destroy;
9853 }
9854
9855 } else {
9856 rc = lpfc_rq_create(phba,
9857 phba->sli4_hba.nvmet_mrq_hdr[0],
9858 phba->sli4_hba.nvmet_mrq_data[0],
9859 phba->sli4_hba.nvmet_cqset[0],
9860 LPFC_NVMET);
9861 if (rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009862 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2d7dbc42017-02-12 13:52:35 -08009863 "6057 Failed setup of NVMET "
9864 "Receive Queue: rc = 0x%x\n",
9865 (uint32_t)rc);
9866 goto out_destroy;
9867 }
9868
9869 lpfc_printf_log(
9870 phba, KERN_INFO, LOG_INIT,
9871 "6099 NVMET RQ setup: hdr-rq-id=%d, "
9872 "dat-rq-id=%d parent cq-id=%d\n",
9873 phba->sli4_hba.nvmet_mrq_hdr[0]->queue_id,
9874 phba->sli4_hba.nvmet_mrq_data[0]->queue_id,
9875 phba->sli4_hba.nvmet_cqset[0]->queue_id);
9876
9877 }
9878 }
9879
James Smartda0436e2009-05-22 14:51:39 -04009880 if (!phba->sli4_hba.hdr_rq || !phba->sli4_hba.dat_rq) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009881 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartda0436e2009-05-22 14:51:39 -04009882 "0540 Receive Queue not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05009883 rc = -ENOMEM;
James Smart895427b2017-02-12 13:52:30 -08009884 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -04009885 }
James Smart73d91e52011-10-10 21:32:10 -04009886
James Smartda0436e2009-05-22 14:51:39 -04009887 rc = lpfc_rq_create(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
James Smart4d9ab992009-10-02 15:16:39 -04009888 phba->sli4_hba.els_cq, LPFC_USOL);
James Smartda0436e2009-05-22 14:51:39 -04009889 if (rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009890 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartda0436e2009-05-22 14:51:39 -04009891 "0541 Failed setup of Receive Queue: "
James Smarta2fc4aef2014-09-03 12:57:55 -04009892 "rc = 0x%x\n", (uint32_t)rc);
James Smart895427b2017-02-12 13:52:30 -08009893 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -04009894 }
James Smart73d91e52011-10-10 21:32:10 -04009895
James Smartda0436e2009-05-22 14:51:39 -04009896 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9897 "2592 USL RQ setup: hdr-rq-id=%d, dat-rq-id=%d "
9898 "parent cq-id=%d\n",
9899 phba->sli4_hba.hdr_rq->queue_id,
9900 phba->sli4_hba.dat_rq->queue_id,
James Smart4d9ab992009-10-02 15:16:39 -04009901 phba->sli4_hba.els_cq->queue_id);
James Smart1ba981f2014-02-20 09:56:45 -05009902
James Smartcb733e32019-01-28 11:14:32 -08009903 if (phba->cfg_fcp_imax)
9904 usdelay = LPFC_SEC_TO_USEC / phba->cfg_fcp_imax;
9905 else
9906 usdelay = 0;
9907
James Smart6a828b02019-01-28 11:14:31 -08009908 for (qidx = 0; qidx < phba->cfg_irq_chann;
James Smartcdb42be2019-01-28 11:14:21 -08009909 qidx += LPFC_MAX_EQ_DELAY_EQID_CNT)
James Smart0cf07f842017-06-01 21:07:10 -07009910 lpfc_modify_hba_eq_delay(phba, qidx, LPFC_MAX_EQ_DELAY_EQID_CNT,
James Smartcb733e32019-01-28 11:14:32 -08009911 usdelay);
James Smart43140ca2017-03-04 09:30:34 -08009912
James Smart6a828b02019-01-28 11:14:31 -08009913 if (phba->sli4_hba.cq_max) {
9914 kfree(phba->sli4_hba.cq_lookup);
9915 phba->sli4_hba.cq_lookup = kcalloc((phba->sli4_hba.cq_max + 1),
9916 sizeof(struct lpfc_queue *), GFP_KERNEL);
9917 if (!phba->sli4_hba.cq_lookup) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009918 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart6a828b02019-01-28 11:14:31 -08009919 "0549 Failed setup of CQ Lookup table: "
9920 "size 0x%x\n", phba->sli4_hba.cq_max);
Dan Carpenterfad28e32019-02-11 21:43:00 +03009921 rc = -ENOMEM;
James Smart1ba981f2014-02-20 09:56:45 -05009922 goto out_destroy;
James Smart895427b2017-02-12 13:52:30 -08009923 }
James Smart6a828b02019-01-28 11:14:31 -08009924 lpfc_setup_cq_lookup(phba);
James Smart1ba981f2014-02-20 09:56:45 -05009925 }
James Smartda0436e2009-05-22 14:51:39 -04009926 return 0;
9927
James Smart895427b2017-02-12 13:52:30 -08009928out_destroy:
9929 lpfc_sli4_queue_unset(phba);
James Smartda0436e2009-05-22 14:51:39 -04009930out_error:
9931 return rc;
9932}
9933
9934/**
9935 * lpfc_sli4_queue_unset - Unset all the SLI4 queues
9936 * @phba: pointer to lpfc hba data structure.
9937 *
9938 * This routine is invoked to unset all the SLI4 queues with the FCoE HBA
9939 * operation.
9940 *
9941 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02009942 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009943 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04009944 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04009945 **/
9946void
9947lpfc_sli4_queue_unset(struct lpfc_hba *phba)
9948{
James Smartcdb42be2019-01-28 11:14:21 -08009949 struct lpfc_sli4_hdw_queue *qp;
James Smart657add42019-05-21 17:49:06 -07009950 struct lpfc_queue *eq;
James Smart895427b2017-02-12 13:52:30 -08009951 int qidx;
James Smartda0436e2009-05-22 14:51:39 -04009952
9953 /* Unset mailbox command work queue */
James Smart895427b2017-02-12 13:52:30 -08009954 if (phba->sli4_hba.mbx_wq)
9955 lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
9956
9957 /* Unset NVME LS work queue */
9958 if (phba->sli4_hba.nvmels_wq)
9959 lpfc_wq_destroy(phba, phba->sli4_hba.nvmels_wq);
9960
James Smartda0436e2009-05-22 14:51:39 -04009961 /* Unset ELS work queue */
Colin Ian King019c0d62017-05-06 23:13:55 +01009962 if (phba->sli4_hba.els_wq)
James Smart895427b2017-02-12 13:52:30 -08009963 lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
9964
James Smartda0436e2009-05-22 14:51:39 -04009965 /* Unset unsolicited receive queue */
James Smart895427b2017-02-12 13:52:30 -08009966 if (phba->sli4_hba.hdr_rq)
9967 lpfc_rq_destroy(phba, phba->sli4_hba.hdr_rq,
9968 phba->sli4_hba.dat_rq);
9969
James Smartda0436e2009-05-22 14:51:39 -04009970 /* Unset mailbox command complete queue */
James Smart895427b2017-02-12 13:52:30 -08009971 if (phba->sli4_hba.mbx_cq)
9972 lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
9973
James Smartda0436e2009-05-22 14:51:39 -04009974 /* Unset ELS complete queue */
James Smart895427b2017-02-12 13:52:30 -08009975 if (phba->sli4_hba.els_cq)
9976 lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
9977
9978 /* Unset NVME LS complete queue */
9979 if (phba->sli4_hba.nvmels_cq)
9980 lpfc_cq_destroy(phba, phba->sli4_hba.nvmels_cq);
9981
James Smartbcb24f62017-11-20 16:00:36 -08009982 if (phba->nvmet_support) {
9983 /* Unset NVMET MRQ queue */
9984 if (phba->sli4_hba.nvmet_mrq_hdr) {
9985 for (qidx = 0; qidx < phba->cfg_nvmet_mrq; qidx++)
9986 lpfc_rq_destroy(
9987 phba,
James Smart2d7dbc42017-02-12 13:52:35 -08009988 phba->sli4_hba.nvmet_mrq_hdr[qidx],
9989 phba->sli4_hba.nvmet_mrq_data[qidx]);
James Smartbcb24f62017-11-20 16:00:36 -08009990 }
James Smart2d7dbc42017-02-12 13:52:35 -08009991
James Smartbcb24f62017-11-20 16:00:36 -08009992 /* Unset NVMET CQ Set complete queue */
9993 if (phba->sli4_hba.nvmet_cqset) {
9994 for (qidx = 0; qidx < phba->cfg_nvmet_mrq; qidx++)
9995 lpfc_cq_destroy(
9996 phba, phba->sli4_hba.nvmet_cqset[qidx]);
9997 }
James Smart2d7dbc42017-02-12 13:52:35 -08009998 }
9999
James Smartcdb42be2019-01-28 11:14:21 -080010000 /* Unset fast-path SLI4 queues */
10001 if (phba->sli4_hba.hdwq) {
James Smart657add42019-05-21 17:49:06 -070010002 /* Loop thru all Hardware Queues */
James Smartcdb42be2019-01-28 11:14:21 -080010003 for (qidx = 0; qidx < phba->cfg_hdw_queue; qidx++) {
James Smart657add42019-05-21 17:49:06 -070010004 /* Destroy the CQ/WQ corresponding to Hardware Queue */
James Smartcdb42be2019-01-28 11:14:21 -080010005 qp = &phba->sli4_hba.hdwq[qidx];
James Smartc00f62e2019-08-14 16:57:11 -070010006 lpfc_wq_destroy(phba, qp->io_wq);
10007 lpfc_cq_destroy(phba, qp->io_cq);
James Smart657add42019-05-21 17:49:06 -070010008 }
10009 /* Loop thru all IRQ vectors */
10010 for (qidx = 0; qidx < phba->cfg_irq_chann; qidx++) {
10011 /* Destroy the EQ corresponding to the IRQ vector */
10012 eq = phba->sli4_hba.hba_eq_hdl[qidx].eq;
10013 lpfc_eq_destroy(phba, eq);
James Smartcdb42be2019-01-28 11:14:21 -080010014 }
10015 }
James Smart895427b2017-02-12 13:52:30 -080010016
James Smart6a828b02019-01-28 11:14:31 -080010017 kfree(phba->sli4_hba.cq_lookup);
10018 phba->sli4_hba.cq_lookup = NULL;
10019 phba->sli4_hba.cq_max = 0;
James Smartda0436e2009-05-22 14:51:39 -040010020}
10021
10022/**
10023 * lpfc_sli4_cq_event_pool_create - Create completion-queue event free pool
10024 * @phba: pointer to lpfc hba data structure.
10025 *
10026 * This routine is invoked to allocate and set up a pool of completion queue
10027 * events. The body of the completion queue event is a completion queue entry
10028 * CQE. For now, this pool is used for the interrupt service routine to queue
10029 * the following HBA completion queue events for the worker thread to process:
10030 * - Mailbox asynchronous events
10031 * - Receive queue completion unsolicited events
10032 * Later, this can be used for all the slow-path events.
10033 *
10034 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020010035 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010036 * -ENOMEM - No available memory
James Smartda0436e2009-05-22 14:51:39 -040010037 **/
10038static int
10039lpfc_sli4_cq_event_pool_create(struct lpfc_hba *phba)
10040{
10041 struct lpfc_cq_event *cq_event;
10042 int i;
10043
10044 for (i = 0; i < (4 * phba->sli4_hba.cq_ecount); i++) {
10045 cq_event = kmalloc(sizeof(struct lpfc_cq_event), GFP_KERNEL);
10046 if (!cq_event)
10047 goto out_pool_create_fail;
10048 list_add_tail(&cq_event->list,
10049 &phba->sli4_hba.sp_cqe_event_pool);
10050 }
10051 return 0;
10052
10053out_pool_create_fail:
10054 lpfc_sli4_cq_event_pool_destroy(phba);
10055 return -ENOMEM;
10056}
10057
10058/**
10059 * lpfc_sli4_cq_event_pool_destroy - Free completion-queue event free pool
10060 * @phba: pointer to lpfc hba data structure.
10061 *
10062 * This routine is invoked to free the pool of completion queue events at
10063 * driver unload time. Note that, it is the responsibility of the driver
10064 * cleanup routine to free all the outstanding completion-queue events
10065 * allocated from this pool back into the pool before invoking this routine
10066 * to destroy the pool.
10067 **/
10068static void
10069lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *phba)
10070{
10071 struct lpfc_cq_event *cq_event, *next_cq_event;
10072
10073 list_for_each_entry_safe(cq_event, next_cq_event,
10074 &phba->sli4_hba.sp_cqe_event_pool, list) {
10075 list_del(&cq_event->list);
10076 kfree(cq_event);
10077 }
10078}
10079
10080/**
10081 * __lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
10082 * @phba: pointer to lpfc hba data structure.
10083 *
10084 * This routine is the lock free version of the API invoked to allocate a
10085 * completion-queue event from the free pool.
10086 *
10087 * Return: Pointer to the newly allocated completion-queue event if successful
10088 * NULL otherwise.
10089 **/
10090struct lpfc_cq_event *
10091__lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
10092{
10093 struct lpfc_cq_event *cq_event = NULL;
10094
10095 list_remove_head(&phba->sli4_hba.sp_cqe_event_pool, cq_event,
10096 struct lpfc_cq_event, list);
10097 return cq_event;
10098}
10099
10100/**
10101 * lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
10102 * @phba: pointer to lpfc hba data structure.
10103 *
10104 * This routine is the lock version of the API invoked to allocate a
10105 * completion-queue event from the free pool.
10106 *
10107 * Return: Pointer to the newly allocated completion-queue event if successful
10108 * NULL otherwise.
10109 **/
10110struct lpfc_cq_event *
10111lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
10112{
10113 struct lpfc_cq_event *cq_event;
10114 unsigned long iflags;
10115
10116 spin_lock_irqsave(&phba->hbalock, iflags);
10117 cq_event = __lpfc_sli4_cq_event_alloc(phba);
10118 spin_unlock_irqrestore(&phba->hbalock, iflags);
10119 return cq_event;
10120}
10121
10122/**
10123 * __lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
10124 * @phba: pointer to lpfc hba data structure.
10125 * @cq_event: pointer to the completion queue event to be freed.
10126 *
10127 * This routine is the lock free version of the API invoked to release a
10128 * completion-queue event back into the free pool.
10129 **/
10130void
10131__lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
10132 struct lpfc_cq_event *cq_event)
10133{
10134 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_cqe_event_pool);
10135}
10136
10137/**
10138 * lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
10139 * @phba: pointer to lpfc hba data structure.
10140 * @cq_event: pointer to the completion queue event to be freed.
10141 *
10142 * This routine is the lock version of the API invoked to release a
10143 * completion-queue event back into the free pool.
10144 **/
10145void
10146lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
10147 struct lpfc_cq_event *cq_event)
10148{
10149 unsigned long iflags;
10150 spin_lock_irqsave(&phba->hbalock, iflags);
10151 __lpfc_sli4_cq_event_release(phba, cq_event);
10152 spin_unlock_irqrestore(&phba->hbalock, iflags);
10153}
10154
10155/**
10156 * lpfc_sli4_cq_event_release_all - Release all cq events to the free pool
10157 * @phba: pointer to lpfc hba data structure.
10158 *
10159 * This routine is to free all the pending completion-queue events to the
10160 * back into the free pool for device reset.
10161 **/
10162static void
10163lpfc_sli4_cq_event_release_all(struct lpfc_hba *phba)
10164{
10165 LIST_HEAD(cqelist);
10166 struct lpfc_cq_event *cqe;
10167 unsigned long iflags;
10168
10169 /* Retrieve all the pending WCQEs from pending WCQE lists */
10170 spin_lock_irqsave(&phba->hbalock, iflags);
10171 /* Pending FCP XRI abort events */
10172 list_splice_init(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
10173 &cqelist);
10174 /* Pending ELS XRI abort events */
10175 list_splice_init(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
10176 &cqelist);
10177 /* Pending asynnc events */
10178 list_splice_init(&phba->sli4_hba.sp_asynce_work_queue,
10179 &cqelist);
10180 spin_unlock_irqrestore(&phba->hbalock, iflags);
10181
10182 while (!list_empty(&cqelist)) {
10183 list_remove_head(&cqelist, cqe, struct lpfc_cq_event, list);
10184 lpfc_sli4_cq_event_release(phba, cqe);
10185 }
10186}
10187
10188/**
10189 * lpfc_pci_function_reset - Reset pci function.
10190 * @phba: pointer to lpfc hba data structure.
10191 *
10192 * This routine is invoked to request a PCI function reset. It will destroys
10193 * all resources assigned to the PCI function which originates this request.
10194 *
10195 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020010196 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010197 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -040010198 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -040010199 **/
10200int
10201lpfc_pci_function_reset(struct lpfc_hba *phba)
10202{
10203 LPFC_MBOXQ_t *mboxq;
James Smart2fcee4b2010-12-15 17:57:46 -050010204 uint32_t rc = 0, if_type;
James Smartda0436e2009-05-22 14:51:39 -040010205 uint32_t shdr_status, shdr_add_status;
James Smart2f6fa2c2014-09-03 12:57:08 -040010206 uint32_t rdy_chk;
10207 uint32_t port_reset = 0;
James Smartda0436e2009-05-22 14:51:39 -040010208 union lpfc_sli4_cfg_shdr *shdr;
James Smart2fcee4b2010-12-15 17:57:46 -050010209 struct lpfc_register reg_data;
James Smart2b81f942012-03-01 22:37:18 -050010210 uint16_t devid;
James Smartda0436e2009-05-22 14:51:39 -040010211
James Smart2fcee4b2010-12-15 17:57:46 -050010212 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
10213 switch (if_type) {
10214 case LPFC_SLI_INTF_IF_TYPE_0:
10215 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
10216 GFP_KERNEL);
10217 if (!mboxq) {
Dick Kennedy372c1872020-06-30 14:50:00 -070010218 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2fcee4b2010-12-15 17:57:46 -050010219 "0494 Unable to allocate memory for "
10220 "issuing SLI_FUNCTION_RESET mailbox "
10221 "command\n");
10222 return -ENOMEM;
10223 }
10224
10225 /* Setup PCI function reset mailbox-ioctl command */
10226 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
10227 LPFC_MBOX_OPCODE_FUNCTION_RESET, 0,
10228 LPFC_SLI4_MBX_EMBED);
10229 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
10230 shdr = (union lpfc_sli4_cfg_shdr *)
10231 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
10232 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10233 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
10234 &shdr->response);
10235 if (rc != MBX_TIMEOUT)
10236 mempool_free(mboxq, phba->mbox_mem_pool);
10237 if (shdr_status || shdr_add_status || rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -070010238 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2fcee4b2010-12-15 17:57:46 -050010239 "0495 SLI_FUNCTION_RESET mailbox "
10240 "failed with status x%x add_status x%x,"
10241 " mbx status x%x\n",
10242 shdr_status, shdr_add_status, rc);
10243 rc = -ENXIO;
10244 }
10245 break;
10246 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart27d6ac02018-02-22 08:18:42 -080010247 case LPFC_SLI_INTF_IF_TYPE_6:
James Smart2f6fa2c2014-09-03 12:57:08 -040010248wait:
10249 /*
10250 * Poll the Port Status Register and wait for RDY for
10251 * up to 30 seconds. If the port doesn't respond, treat
10252 * it as an error.
10253 */
James Smart77d093f2015-04-07 15:07:08 -040010254 for (rdy_chk = 0; rdy_chk < 1500; rdy_chk++) {
James Smart2f6fa2c2014-09-03 12:57:08 -040010255 if (lpfc_readl(phba->sli4_hba.u.if_type2.
10256 STATUSregaddr, &reg_data.word0)) {
10257 rc = -ENODEV;
10258 goto out;
10259 }
10260 if (bf_get(lpfc_sliport_status_rdy, &reg_data))
10261 break;
10262 msleep(20);
10263 }
10264
10265 if (!bf_get(lpfc_sliport_status_rdy, &reg_data)) {
10266 phba->work_status[0] = readl(
10267 phba->sli4_hba.u.if_type2.ERR1regaddr);
10268 phba->work_status[1] = readl(
10269 phba->sli4_hba.u.if_type2.ERR2regaddr);
Dick Kennedy372c1872020-06-30 14:50:00 -070010270 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2f6fa2c2014-09-03 12:57:08 -040010271 "2890 Port not ready, port status reg "
10272 "0x%x error 1=0x%x, error 2=0x%x\n",
10273 reg_data.word0,
10274 phba->work_status[0],
10275 phba->work_status[1]);
10276 rc = -ENODEV;
10277 goto out;
10278 }
10279
10280 if (!port_reset) {
10281 /*
10282 * Reset the port now
10283 */
James Smart2fcee4b2010-12-15 17:57:46 -050010284 reg_data.word0 = 0;
10285 bf_set(lpfc_sliport_ctrl_end, &reg_data,
10286 LPFC_SLIPORT_LITTLE_ENDIAN);
10287 bf_set(lpfc_sliport_ctrl_ip, &reg_data,
10288 LPFC_SLIPORT_INIT_PORT);
10289 writel(reg_data.word0, phba->sli4_hba.u.if_type2.
10290 CTRLregaddr);
James Smart8fcb8ac2012-03-01 22:35:58 -050010291 /* flush */
James Smart2b81f942012-03-01 22:37:18 -050010292 pci_read_config_word(phba->pcidev,
10293 PCI_DEVICE_ID, &devid);
James Smart2fcee4b2010-12-15 17:57:46 -050010294
James Smart2f6fa2c2014-09-03 12:57:08 -040010295 port_reset = 1;
10296 msleep(20);
10297 goto wait;
10298 } else if (bf_get(lpfc_sliport_status_rn, &reg_data)) {
10299 rc = -ENODEV;
10300 goto out;
James Smart2fcee4b2010-12-15 17:57:46 -050010301 }
10302 break;
James Smart2f6fa2c2014-09-03 12:57:08 -040010303
James Smart2fcee4b2010-12-15 17:57:46 -050010304 case LPFC_SLI_INTF_IF_TYPE_1:
10305 default:
10306 break;
James Smartda0436e2009-05-22 14:51:39 -040010307 }
10308
James Smart73d91e52011-10-10 21:32:10 -040010309out:
James Smart2fcee4b2010-12-15 17:57:46 -050010310 /* Catch the not-ready port failure after a port reset. */
James Smart2f6fa2c2014-09-03 12:57:08 -040010311 if (rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -070010312 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart229adb02013-04-17 20:16:51 -040010313 "3317 HBA not functional: IP Reset Failed "
James Smart2f6fa2c2014-09-03 12:57:08 -040010314 "try: echo fw_reset > board_mode\n");
James Smart2fcee4b2010-12-15 17:57:46 -050010315 rc = -ENODEV;
James Smart229adb02013-04-17 20:16:51 -040010316 }
James Smart2fcee4b2010-12-15 17:57:46 -050010317
James Smartda0436e2009-05-22 14:51:39 -040010318 return rc;
10319}
10320
10321/**
James Smartda0436e2009-05-22 14:51:39 -040010322 * lpfc_sli4_pci_mem_setup - Setup SLI4 HBA PCI memory space.
10323 * @phba: pointer to lpfc hba data structure.
10324 *
10325 * This routine is invoked to set up the PCI device memory space for device
10326 * with SLI-4 interface spec.
10327 *
10328 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020010329 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -040010330 * other values - error
10331 **/
10332static int
10333lpfc_sli4_pci_mem_setup(struct lpfc_hba *phba)
10334{
Christoph Hellwigf30e1bf2018-10-18 15:10:21 +020010335 struct pci_dev *pdev = phba->pcidev;
James Smartda0436e2009-05-22 14:51:39 -040010336 unsigned long bar0map_len, bar1map_len, bar2map_len;
Dan Carpenter3a487ff2019-03-07 08:33:44 +030010337 int error;
James Smart2fcee4b2010-12-15 17:57:46 -050010338 uint32_t if_type;
James Smartda0436e2009-05-22 14:51:39 -040010339
Christoph Hellwigf30e1bf2018-10-18 15:10:21 +020010340 if (!pdev)
Hannes Reinecke56de8352019-02-18 08:34:19 +010010341 return -ENODEV;
James Smartda0436e2009-05-22 14:51:39 -040010342
10343 /* Set the device DMA mask size */
Hannes Reinecke56de8352019-02-18 08:34:19 +010010344 error = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
10345 if (error)
10346 error = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
10347 if (error)
Christoph Hellwigf30e1bf2018-10-18 15:10:21 +020010348 return error;
James Smartda0436e2009-05-22 14:51:39 -040010349
James Smart2fcee4b2010-12-15 17:57:46 -050010350 /*
10351 * The BARs and register set definitions and offset locations are
10352 * dependent on the if_type.
10353 */
10354 if (pci_read_config_dword(pdev, LPFC_SLI_INTF,
10355 &phba->sli4_hba.sli_intf.word0)) {
Dan Carpenter3a487ff2019-03-07 08:33:44 +030010356 return -ENODEV;
James Smart2fcee4b2010-12-15 17:57:46 -050010357 }
10358
10359 /* There is no SLI3 failback for SLI4 devices. */
10360 if (bf_get(lpfc_sli_intf_valid, &phba->sli4_hba.sli_intf) !=
10361 LPFC_SLI_INTF_VALID) {
Dick Kennedy372c1872020-06-30 14:50:00 -070010362 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2fcee4b2010-12-15 17:57:46 -050010363 "2894 SLI_INTF reg contents invalid "
10364 "sli_intf reg 0x%x\n",
10365 phba->sli4_hba.sli_intf.word0);
Dan Carpenter3a487ff2019-03-07 08:33:44 +030010366 return -ENODEV;
James Smart2fcee4b2010-12-15 17:57:46 -050010367 }
10368
10369 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
10370 /*
10371 * Get the bus address of SLI4 device Bar regions and the
10372 * number of bytes required by each mapping. The mapping of the
10373 * particular PCI BARs regions is dependent on the type of
10374 * SLI4 device.
James Smartda0436e2009-05-22 14:51:39 -040010375 */
James Smartf5ca6f22013-09-06 12:21:09 -040010376 if (pci_resource_start(pdev, PCI_64BIT_BAR0)) {
10377 phba->pci_bar0_map = pci_resource_start(pdev, PCI_64BIT_BAR0);
10378 bar0map_len = pci_resource_len(pdev, PCI_64BIT_BAR0);
James Smart2fcee4b2010-12-15 17:57:46 -050010379
10380 /*
10381 * Map SLI4 PCI Config Space Register base to a kernel virtual
10382 * addr
10383 */
10384 phba->sli4_hba.conf_regs_memmap_p =
10385 ioremap(phba->pci_bar0_map, bar0map_len);
10386 if (!phba->sli4_hba.conf_regs_memmap_p) {
10387 dev_printk(KERN_ERR, &pdev->dev,
10388 "ioremap failed for SLI4 PCI config "
10389 "registers.\n");
Dan Carpenter3a487ff2019-03-07 08:33:44 +030010390 return -ENODEV;
James Smart2fcee4b2010-12-15 17:57:46 -050010391 }
James Smartf5ca6f22013-09-06 12:21:09 -040010392 phba->pci_bar0_memmap_p = phba->sli4_hba.conf_regs_memmap_p;
James Smart2fcee4b2010-12-15 17:57:46 -050010393 /* Set up BAR0 PCI config space register memory map */
10394 lpfc_sli4_bar0_register_memmap(phba, if_type);
James Smart1dfb5a42010-02-12 14:40:50 -050010395 } else {
10396 phba->pci_bar0_map = pci_resource_start(pdev, 1);
10397 bar0map_len = pci_resource_len(pdev, 1);
James Smart27d6ac02018-02-22 08:18:42 -080010398 if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
James Smart2fcee4b2010-12-15 17:57:46 -050010399 dev_printk(KERN_ERR, &pdev->dev,
10400 "FATAL - No BAR0 mapping for SLI4, if_type 2\n");
Dan Carpenter3a487ff2019-03-07 08:33:44 +030010401 return -ENODEV;
James Smart2fcee4b2010-12-15 17:57:46 -050010402 }
10403 phba->sli4_hba.conf_regs_memmap_p =
James Smartda0436e2009-05-22 14:51:39 -040010404 ioremap(phba->pci_bar0_map, bar0map_len);
James Smart2fcee4b2010-12-15 17:57:46 -050010405 if (!phba->sli4_hba.conf_regs_memmap_p) {
10406 dev_printk(KERN_ERR, &pdev->dev,
10407 "ioremap failed for SLI4 PCI config "
10408 "registers.\n");
Dan Carpenter3a487ff2019-03-07 08:33:44 +030010409 return -ENODEV;
James Smart2fcee4b2010-12-15 17:57:46 -050010410 }
10411 lpfc_sli4_bar0_register_memmap(phba, if_type);
James Smartda0436e2009-05-22 14:51:39 -040010412 }
10413
James Smarte4b97942017-11-20 16:00:31 -080010414 if (if_type == LPFC_SLI_INTF_IF_TYPE_0) {
10415 if (pci_resource_start(pdev, PCI_64BIT_BAR2)) {
10416 /*
10417 * Map SLI4 if type 0 HBA Control Register base to a
10418 * kernel virtual address and setup the registers.
10419 */
10420 phba->pci_bar1_map = pci_resource_start(pdev,
10421 PCI_64BIT_BAR2);
10422 bar1map_len = pci_resource_len(pdev, PCI_64BIT_BAR2);
10423 phba->sli4_hba.ctrl_regs_memmap_p =
10424 ioremap(phba->pci_bar1_map,
10425 bar1map_len);
10426 if (!phba->sli4_hba.ctrl_regs_memmap_p) {
10427 dev_err(&pdev->dev,
10428 "ioremap failed for SLI4 HBA "
10429 "control registers.\n");
10430 error = -ENOMEM;
10431 goto out_iounmap_conf;
10432 }
10433 phba->pci_bar2_memmap_p =
10434 phba->sli4_hba.ctrl_regs_memmap_p;
James Smart27d6ac02018-02-22 08:18:42 -080010435 lpfc_sli4_bar1_register_memmap(phba, if_type);
James Smarte4b97942017-11-20 16:00:31 -080010436 } else {
10437 error = -ENOMEM;
James Smart2fcee4b2010-12-15 17:57:46 -050010438 goto out_iounmap_conf;
10439 }
James Smartda0436e2009-05-22 14:51:39 -040010440 }
10441
James Smart27d6ac02018-02-22 08:18:42 -080010442 if ((if_type == LPFC_SLI_INTF_IF_TYPE_6) &&
10443 (pci_resource_start(pdev, PCI_64BIT_BAR2))) {
10444 /*
10445 * Map SLI4 if type 6 HBA Doorbell Register base to a kernel
10446 * virtual address and setup the registers.
10447 */
10448 phba->pci_bar1_map = pci_resource_start(pdev, PCI_64BIT_BAR2);
10449 bar1map_len = pci_resource_len(pdev, PCI_64BIT_BAR2);
10450 phba->sli4_hba.drbl_regs_memmap_p =
10451 ioremap(phba->pci_bar1_map, bar1map_len);
10452 if (!phba->sli4_hba.drbl_regs_memmap_p) {
10453 dev_err(&pdev->dev,
10454 "ioremap failed for SLI4 HBA doorbell registers.\n");
Dan Carpenter3a487ff2019-03-07 08:33:44 +030010455 error = -ENOMEM;
James Smart27d6ac02018-02-22 08:18:42 -080010456 goto out_iounmap_conf;
10457 }
10458 phba->pci_bar2_memmap_p = phba->sli4_hba.drbl_regs_memmap_p;
10459 lpfc_sli4_bar1_register_memmap(phba, if_type);
10460 }
10461
James Smarte4b97942017-11-20 16:00:31 -080010462 if (if_type == LPFC_SLI_INTF_IF_TYPE_0) {
10463 if (pci_resource_start(pdev, PCI_64BIT_BAR4)) {
10464 /*
10465 * Map SLI4 if type 0 HBA Doorbell Register base to
10466 * a kernel virtual address and setup the registers.
10467 */
10468 phba->pci_bar2_map = pci_resource_start(pdev,
10469 PCI_64BIT_BAR4);
10470 bar2map_len = pci_resource_len(pdev, PCI_64BIT_BAR4);
10471 phba->sli4_hba.drbl_regs_memmap_p =
10472 ioremap(phba->pci_bar2_map,
10473 bar2map_len);
10474 if (!phba->sli4_hba.drbl_regs_memmap_p) {
10475 dev_err(&pdev->dev,
10476 "ioremap failed for SLI4 HBA"
10477 " doorbell registers.\n");
10478 error = -ENOMEM;
10479 goto out_iounmap_ctrl;
10480 }
10481 phba->pci_bar4_memmap_p =
10482 phba->sli4_hba.drbl_regs_memmap_p;
10483 error = lpfc_sli4_bar2_register_memmap(phba, LPFC_VF0);
10484 if (error)
10485 goto out_iounmap_all;
10486 } else {
10487 error = -ENOMEM;
James Smart2fcee4b2010-12-15 17:57:46 -050010488 goto out_iounmap_all;
James Smarte4b97942017-11-20 16:00:31 -080010489 }
James Smartda0436e2009-05-22 14:51:39 -040010490 }
10491
James Smart1351e692018-02-22 08:18:43 -080010492 if (if_type == LPFC_SLI_INTF_IF_TYPE_6 &&
10493 pci_resource_start(pdev, PCI_64BIT_BAR4)) {
10494 /*
10495 * Map SLI4 if type 6 HBA DPP Register base to a kernel
10496 * virtual address and setup the registers.
10497 */
10498 phba->pci_bar2_map = pci_resource_start(pdev, PCI_64BIT_BAR4);
10499 bar2map_len = pci_resource_len(pdev, PCI_64BIT_BAR4);
10500 phba->sli4_hba.dpp_regs_memmap_p =
10501 ioremap(phba->pci_bar2_map, bar2map_len);
10502 if (!phba->sli4_hba.dpp_regs_memmap_p) {
10503 dev_err(&pdev->dev,
10504 "ioremap failed for SLI4 HBA dpp registers.\n");
Dan Carpenter3a487ff2019-03-07 08:33:44 +030010505 error = -ENOMEM;
James Smart1351e692018-02-22 08:18:43 -080010506 goto out_iounmap_ctrl;
10507 }
10508 phba->pci_bar4_memmap_p = phba->sli4_hba.dpp_regs_memmap_p;
10509 }
10510
James Smartb71413d2018-02-22 08:18:40 -080010511 /* Set up the EQ/CQ register handeling functions now */
James Smart27d6ac02018-02-22 08:18:42 -080010512 switch (if_type) {
10513 case LPFC_SLI_INTF_IF_TYPE_0:
10514 case LPFC_SLI_INTF_IF_TYPE_2:
James Smartb71413d2018-02-22 08:18:40 -080010515 phba->sli4_hba.sli4_eq_clr_intr = lpfc_sli4_eq_clr_intr;
James Smart32517fc2019-01-28 11:14:33 -080010516 phba->sli4_hba.sli4_write_eq_db = lpfc_sli4_write_eq_db;
10517 phba->sli4_hba.sli4_write_cq_db = lpfc_sli4_write_cq_db;
James Smart27d6ac02018-02-22 08:18:42 -080010518 break;
10519 case LPFC_SLI_INTF_IF_TYPE_6:
10520 phba->sli4_hba.sli4_eq_clr_intr = lpfc_sli4_if6_eq_clr_intr;
James Smart32517fc2019-01-28 11:14:33 -080010521 phba->sli4_hba.sli4_write_eq_db = lpfc_sli4_if6_write_eq_db;
10522 phba->sli4_hba.sli4_write_cq_db = lpfc_sli4_if6_write_cq_db;
James Smart27d6ac02018-02-22 08:18:42 -080010523 break;
10524 default:
10525 break;
James Smartb71413d2018-02-22 08:18:40 -080010526 }
10527
James Smartda0436e2009-05-22 14:51:39 -040010528 return 0;
10529
10530out_iounmap_all:
10531 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
10532out_iounmap_ctrl:
10533 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
10534out_iounmap_conf:
10535 iounmap(phba->sli4_hba.conf_regs_memmap_p);
Dan Carpenter3a487ff2019-03-07 08:33:44 +030010536
James Smartda0436e2009-05-22 14:51:39 -040010537 return error;
10538}
10539
10540/**
10541 * lpfc_sli4_pci_mem_unset - Unset SLI4 HBA PCI memory space.
10542 * @phba: pointer to lpfc hba data structure.
10543 *
10544 * This routine is invoked to unset the PCI device memory space for device
10545 * with SLI-4 interface spec.
10546 **/
10547static void
10548lpfc_sli4_pci_mem_unset(struct lpfc_hba *phba)
10549{
James Smart2e90f4b2011-12-13 13:22:37 -050010550 uint32_t if_type;
10551 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
James Smartda0436e2009-05-22 14:51:39 -040010552
James Smart2e90f4b2011-12-13 13:22:37 -050010553 switch (if_type) {
10554 case LPFC_SLI_INTF_IF_TYPE_0:
10555 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
10556 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
10557 iounmap(phba->sli4_hba.conf_regs_memmap_p);
10558 break;
10559 case LPFC_SLI_INTF_IF_TYPE_2:
10560 iounmap(phba->sli4_hba.conf_regs_memmap_p);
10561 break;
James Smart27d6ac02018-02-22 08:18:42 -080010562 case LPFC_SLI_INTF_IF_TYPE_6:
10563 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
10564 iounmap(phba->sli4_hba.conf_regs_memmap_p);
James Smart0b439192019-12-18 15:58:05 -080010565 if (phba->sli4_hba.dpp_regs_memmap_p)
10566 iounmap(phba->sli4_hba.dpp_regs_memmap_p);
James Smart27d6ac02018-02-22 08:18:42 -080010567 break;
James Smart2e90f4b2011-12-13 13:22:37 -050010568 case LPFC_SLI_INTF_IF_TYPE_1:
10569 default:
10570 dev_printk(KERN_ERR, &phba->pcidev->dev,
10571 "FATAL - unsupported SLI4 interface type - %d\n",
10572 if_type);
10573 break;
10574 }
James Smartda0436e2009-05-22 14:51:39 -040010575}
10576
10577/**
James Smart3772a992009-05-22 14:50:54 -040010578 * lpfc_sli_enable_msix - Enable MSI-X interrupt mode on SLI-3 device
10579 * @phba: pointer to lpfc hba data structure.
10580 *
10581 * This routine is invoked to enable the MSI-X interrupt vectors to device
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010582 * with SLI-3 interface specs.
James Smart3772a992009-05-22 14:50:54 -040010583 *
10584 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020010585 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -040010586 * other values - error
10587 **/
10588static int
10589lpfc_sli_enable_msix(struct lpfc_hba *phba)
10590{
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010591 int rc;
James Smart3772a992009-05-22 14:50:54 -040010592 LPFC_MBOXQ_t *pmb;
10593
10594 /* Set up MSI-X multi-message vectors */
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010595 rc = pci_alloc_irq_vectors(phba->pcidev,
10596 LPFC_MSIX_VECTORS, LPFC_MSIX_VECTORS, PCI_IRQ_MSIX);
10597 if (rc < 0) {
James Smart3772a992009-05-22 14:50:54 -040010598 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10599 "0420 PCI enable MSI-X failed (%d)\n", rc);
Alexander Gordeev029165a2014-07-16 20:05:15 +020010600 goto vec_fail_out;
James Smart3772a992009-05-22 14:50:54 -040010601 }
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010602
James Smart3772a992009-05-22 14:50:54 -040010603 /*
10604 * Assign MSI-X vectors to interrupt handlers
10605 */
10606
10607 /* vector-0 is associated to slow-path handler */
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010608 rc = request_irq(pci_irq_vector(phba->pcidev, 0),
James Smarted243d32015-05-21 13:55:25 -040010609 &lpfc_sli_sp_intr_handler, 0,
James Smart3772a992009-05-22 14:50:54 -040010610 LPFC_SP_DRIVER_HANDLER_NAME, phba);
10611 if (rc) {
10612 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
10613 "0421 MSI-X slow-path request_irq failed "
10614 "(%d)\n", rc);
10615 goto msi_fail_out;
10616 }
10617
10618 /* vector-1 is associated to fast-path handler */
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010619 rc = request_irq(pci_irq_vector(phba->pcidev, 1),
James Smarted243d32015-05-21 13:55:25 -040010620 &lpfc_sli_fp_intr_handler, 0,
James Smart3772a992009-05-22 14:50:54 -040010621 LPFC_FP_DRIVER_HANDLER_NAME, phba);
10622
10623 if (rc) {
10624 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
10625 "0429 MSI-X fast-path request_irq failed "
10626 "(%d)\n", rc);
10627 goto irq_fail_out;
10628 }
10629
10630 /*
10631 * Configure HBA MSI-X attention conditions to messages
10632 */
10633 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10634
10635 if (!pmb) {
10636 rc = -ENOMEM;
Dick Kennedy372c1872020-06-30 14:50:00 -070010637 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart3772a992009-05-22 14:50:54 -040010638 "0474 Unable to allocate memory for issuing "
10639 "MBOX_CONFIG_MSI command\n");
10640 goto mem_fail_out;
10641 }
10642 rc = lpfc_config_msi(phba, pmb);
10643 if (rc)
10644 goto mbx_fail_out;
10645 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
10646 if (rc != MBX_SUCCESS) {
10647 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
10648 "0351 Config MSI mailbox command failed, "
10649 "mbxCmd x%x, mbxStatus x%x\n",
10650 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus);
10651 goto mbx_fail_out;
10652 }
10653
10654 /* Free memory allocated for mailbox command */
10655 mempool_free(pmb, phba->mbox_mem_pool);
10656 return rc;
10657
10658mbx_fail_out:
10659 /* Free memory allocated for mailbox command */
10660 mempool_free(pmb, phba->mbox_mem_pool);
10661
10662mem_fail_out:
10663 /* free the irq already requested */
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010664 free_irq(pci_irq_vector(phba->pcidev, 1), phba);
James Smart3772a992009-05-22 14:50:54 -040010665
10666irq_fail_out:
10667 /* free the irq already requested */
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010668 free_irq(pci_irq_vector(phba->pcidev, 0), phba);
James Smart3772a992009-05-22 14:50:54 -040010669
10670msi_fail_out:
10671 /* Unconfigure MSI-X capability structure */
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010672 pci_free_irq_vectors(phba->pcidev);
Alexander Gordeev029165a2014-07-16 20:05:15 +020010673
10674vec_fail_out:
James Smart3772a992009-05-22 14:50:54 -040010675 return rc;
10676}
10677
10678/**
James Smart3772a992009-05-22 14:50:54 -040010679 * lpfc_sli_enable_msi - Enable MSI interrupt mode on SLI-3 device.
10680 * @phba: pointer to lpfc hba data structure.
10681 *
10682 * This routine is invoked to enable the MSI interrupt mode to device with
10683 * SLI-3 interface spec. The kernel function pci_enable_msi() is called to
10684 * enable the MSI vector. The device driver is responsible for calling the
10685 * request_irq() to register MSI vector with a interrupt the handler, which
10686 * is done in this function.
10687 *
10688 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020010689 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -040010690 * other values - error
10691 */
10692static int
10693lpfc_sli_enable_msi(struct lpfc_hba *phba)
10694{
10695 int rc;
10696
10697 rc = pci_enable_msi(phba->pcidev);
10698 if (!rc)
10699 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10700 "0462 PCI enable MSI mode success.\n");
10701 else {
10702 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10703 "0471 PCI enable MSI mode failed (%d)\n", rc);
10704 return rc;
10705 }
10706
10707 rc = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
James Smarted243d32015-05-21 13:55:25 -040010708 0, LPFC_DRIVER_NAME, phba);
James Smart3772a992009-05-22 14:50:54 -040010709 if (rc) {
10710 pci_disable_msi(phba->pcidev);
10711 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
10712 "0478 MSI request_irq failed (%d)\n", rc);
10713 }
10714 return rc;
10715}
10716
10717/**
James Smart3772a992009-05-22 14:50:54 -040010718 * lpfc_sli_enable_intr - Enable device interrupt to SLI-3 device.
10719 * @phba: pointer to lpfc hba data structure.
Lee Jonesfe614ac2020-07-23 13:24:22 +010010720 * @cfg_mode: Interrupt configuration mode (INTx, MSI or MSI-X).
James Smart3772a992009-05-22 14:50:54 -040010721 *
10722 * This routine is invoked to enable device interrupt and associate driver's
10723 * interrupt handler(s) to interrupt vector(s) to device with SLI-3 interface
10724 * spec. Depends on the interrupt mode configured to the driver, the driver
10725 * will try to fallback from the configured interrupt mode to an interrupt
10726 * mode which is supported by the platform, kernel, and device in the order
10727 * of:
10728 * MSI-X -> MSI -> IRQ.
10729 *
10730 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020010731 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -040010732 * other values - error
10733 **/
10734static uint32_t
10735lpfc_sli_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
10736{
10737 uint32_t intr_mode = LPFC_INTR_ERROR;
10738 int retval;
10739
10740 if (cfg_mode == 2) {
10741 /* Need to issue conf_port mbox cmd before conf_msi mbox cmd */
10742 retval = lpfc_sli_config_port(phba, LPFC_SLI_REV3);
10743 if (!retval) {
10744 /* Now, try to enable MSI-X interrupt mode */
10745 retval = lpfc_sli_enable_msix(phba);
10746 if (!retval) {
10747 /* Indicate initialization to MSI-X mode */
10748 phba->intr_type = MSIX;
10749 intr_mode = 2;
10750 }
10751 }
10752 }
10753
10754 /* Fallback to MSI if MSI-X initialization failed */
10755 if (cfg_mode >= 1 && phba->intr_type == NONE) {
10756 retval = lpfc_sli_enable_msi(phba);
10757 if (!retval) {
10758 /* Indicate initialization to MSI mode */
10759 phba->intr_type = MSI;
10760 intr_mode = 1;
10761 }
10762 }
10763
10764 /* Fallback to INTx if both MSI-X/MSI initalization failed */
10765 if (phba->intr_type == NONE) {
10766 retval = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
10767 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
10768 if (!retval) {
10769 /* Indicate initialization to INTx mode */
10770 phba->intr_type = INTx;
10771 intr_mode = 0;
10772 }
10773 }
10774 return intr_mode;
10775}
10776
10777/**
10778 * lpfc_sli_disable_intr - Disable device interrupt to SLI-3 device.
10779 * @phba: pointer to lpfc hba data structure.
10780 *
10781 * This routine is invoked to disable device interrupt and disassociate the
10782 * driver's interrupt handler(s) from interrupt vector(s) to device with
10783 * SLI-3 interface spec. Depending on the interrupt mode, the driver will
10784 * release the interrupt vector(s) for the message signaled interrupt.
10785 **/
10786static void
10787lpfc_sli_disable_intr(struct lpfc_hba *phba)
10788{
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010789 int nr_irqs, i;
10790
James Smart3772a992009-05-22 14:50:54 -040010791 if (phba->intr_type == MSIX)
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010792 nr_irqs = LPFC_MSIX_VECTORS;
10793 else
10794 nr_irqs = 1;
10795
10796 for (i = 0; i < nr_irqs; i++)
10797 free_irq(pci_irq_vector(phba->pcidev, i), phba);
10798 pci_free_irq_vectors(phba->pcidev);
James Smart3772a992009-05-22 14:50:54 -040010799
10800 /* Reset interrupt management states */
10801 phba->intr_type = NONE;
10802 phba->sli.slistat.sli_intr = 0;
James Smart3772a992009-05-22 14:50:54 -040010803}
10804
10805/**
James Smart657add42019-05-21 17:49:06 -070010806 * lpfc_find_cpu_handle - Find the CPU that corresponds to the specified Queue
James Smart6a828b02019-01-28 11:14:31 -080010807 * @phba: pointer to lpfc hba data structure.
10808 * @id: EQ vector index or Hardware Queue index
10809 * @match: LPFC_FIND_BY_EQ = match by EQ
10810 * LPFC_FIND_BY_HDWQ = match by Hardware Queue
James Smart657add42019-05-21 17:49:06 -070010811 * Return the CPU that matches the selection criteria
James Smart6a828b02019-01-28 11:14:31 -080010812 */
10813static uint16_t
10814lpfc_find_cpu_handle(struct lpfc_hba *phba, uint16_t id, int match)
10815{
10816 struct lpfc_vector_map_info *cpup;
10817 int cpu;
10818
James Smart657add42019-05-21 17:49:06 -070010819 /* Loop through all CPUs */
James Smart222e9232019-01-28 11:14:35 -080010820 for_each_present_cpu(cpu) {
10821 cpup = &phba->sli4_hba.cpu_map[cpu];
James Smart657add42019-05-21 17:49:06 -070010822
10823 /* If we are matching by EQ, there may be multiple CPUs using
10824 * using the same vector, so select the one with
10825 * LPFC_CPU_FIRST_IRQ set.
10826 */
James Smart6a828b02019-01-28 11:14:31 -080010827 if ((match == LPFC_FIND_BY_EQ) &&
James Smart657add42019-05-21 17:49:06 -070010828 (cpup->flag & LPFC_CPU_FIRST_IRQ) &&
James Smart6a828b02019-01-28 11:14:31 -080010829 (cpup->eq == id))
10830 return cpu;
James Smart657add42019-05-21 17:49:06 -070010831
10832 /* If matching by HDWQ, select the first CPU that matches */
James Smart6a828b02019-01-28 11:14:31 -080010833 if ((match == LPFC_FIND_BY_HDWQ) && (cpup->hdwq == id))
10834 return cpu;
James Smart6a828b02019-01-28 11:14:31 -080010835 }
10836 return 0;
10837}
10838
James Smart6a828b02019-01-28 11:14:31 -080010839#ifdef CONFIG_X86
10840/**
10841 * lpfc_find_hyper - Determine if the CPU map entry is hyper-threaded
10842 * @phba: pointer to lpfc hba data structure.
10843 * @cpu: CPU map index
10844 * @phys_id: CPU package physical id
10845 * @core_id: CPU core id
10846 */
10847static int
10848lpfc_find_hyper(struct lpfc_hba *phba, int cpu,
10849 uint16_t phys_id, uint16_t core_id)
10850{
10851 struct lpfc_vector_map_info *cpup;
10852 int idx;
10853
James Smart222e9232019-01-28 11:14:35 -080010854 for_each_present_cpu(idx) {
10855 cpup = &phba->sli4_hba.cpu_map[idx];
James Smart6a828b02019-01-28 11:14:31 -080010856 /* Does the cpup match the one we are looking for */
10857 if ((cpup->phys_id == phys_id) &&
10858 (cpup->core_id == core_id) &&
James Smart222e9232019-01-28 11:14:35 -080010859 (cpu != idx))
James Smart6a828b02019-01-28 11:14:31 -080010860 return 1;
James Smart6a828b02019-01-28 11:14:31 -080010861 }
10862 return 0;
10863}
10864#endif
10865
James Smartdcaa2132019-11-04 16:57:06 -080010866/*
10867 * lpfc_assign_eq_map_info - Assigns eq for vector_map structure
10868 * @phba: pointer to lpfc hba data structure.
10869 * @eqidx: index for eq and irq vector
10870 * @flag: flags to set for vector_map structure
10871 * @cpu: cpu used to index vector_map structure
10872 *
10873 * The routine assigns eq info into vector_map structure
10874 */
10875static inline void
10876lpfc_assign_eq_map_info(struct lpfc_hba *phba, uint16_t eqidx, uint16_t flag,
10877 unsigned int cpu)
10878{
10879 struct lpfc_vector_map_info *cpup = &phba->sli4_hba.cpu_map[cpu];
10880 struct lpfc_hba_eq_hdl *eqhdl = lpfc_get_eq_hdl(eqidx);
10881
10882 cpup->eq = eqidx;
10883 cpup->flag |= flag;
10884
10885 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10886 "3336 Set Affinity: CPU %d irq %d eq %d flag x%x\n",
10887 cpu, eqhdl->irq, cpup->eq, cpup->flag);
10888}
10889
10890/**
10891 * lpfc_cpu_map_array_init - Initialize cpu_map structure
10892 * @phba: pointer to lpfc hba data structure.
10893 *
10894 * The routine initializes the cpu_map array structure
10895 */
10896static void
10897lpfc_cpu_map_array_init(struct lpfc_hba *phba)
10898{
10899 struct lpfc_vector_map_info *cpup;
10900 struct lpfc_eq_intr_info *eqi;
10901 int cpu;
10902
10903 for_each_possible_cpu(cpu) {
10904 cpup = &phba->sli4_hba.cpu_map[cpu];
10905 cpup->phys_id = LPFC_VECTOR_MAP_EMPTY;
10906 cpup->core_id = LPFC_VECTOR_MAP_EMPTY;
10907 cpup->hdwq = LPFC_VECTOR_MAP_EMPTY;
10908 cpup->eq = LPFC_VECTOR_MAP_EMPTY;
10909 cpup->flag = 0;
10910 eqi = per_cpu_ptr(phba->sli4_hba.eq_info, cpu);
10911 INIT_LIST_HEAD(&eqi->list);
10912 eqi->icnt = 0;
10913 }
10914}
10915
10916/**
10917 * lpfc_hba_eq_hdl_array_init - Initialize hba_eq_hdl structure
10918 * @phba: pointer to lpfc hba data structure.
10919 *
10920 * The routine initializes the hba_eq_hdl array structure
10921 */
10922static void
10923lpfc_hba_eq_hdl_array_init(struct lpfc_hba *phba)
10924{
10925 struct lpfc_hba_eq_hdl *eqhdl;
10926 int i;
10927
10928 for (i = 0; i < phba->cfg_irq_chann; i++) {
10929 eqhdl = lpfc_get_eq_hdl(i);
10930 eqhdl->irq = LPFC_VECTOR_MAP_EMPTY;
10931 eqhdl->phba = phba;
10932 }
10933}
10934
James Smart6a828b02019-01-28 11:14:31 -080010935/**
James Smart895427b2017-02-12 13:52:30 -080010936 * lpfc_cpu_affinity_check - Check vector CPU affinity mappings
James Smart7bb03bb2013-04-17 20:19:16 -040010937 * @phba: pointer to lpfc hba data structure.
James Smart895427b2017-02-12 13:52:30 -080010938 * @vectors: number of msix vectors allocated.
James Smart7bb03bb2013-04-17 20:19:16 -040010939 *
James Smart895427b2017-02-12 13:52:30 -080010940 * The routine will figure out the CPU affinity assignment for every
James Smart6a828b02019-01-28 11:14:31 -080010941 * MSI-X vector allocated for the HBA.
James Smart895427b2017-02-12 13:52:30 -080010942 * In addition, the CPU to IO channel mapping will be calculated
10943 * and the phba->sli4_hba.cpu_map array will reflect this.
James Smart7bb03bb2013-04-17 20:19:16 -040010944 */
James Smart895427b2017-02-12 13:52:30 -080010945static void
10946lpfc_cpu_affinity_check(struct lpfc_hba *phba, int vectors)
James Smart7bb03bb2013-04-17 20:19:16 -040010947{
James Smart3ad348d2019-08-14 16:56:43 -070010948 int i, cpu, idx, next_idx, new_cpu, start_cpu, first_cpu;
James Smart6a828b02019-01-28 11:14:31 -080010949 int max_phys_id, min_phys_id;
10950 int max_core_id, min_core_id;
James Smart7bb03bb2013-04-17 20:19:16 -040010951 struct lpfc_vector_map_info *cpup;
James Smartd9954a22019-05-21 17:49:05 -070010952 struct lpfc_vector_map_info *new_cpup;
James Smart7bb03bb2013-04-17 20:19:16 -040010953#ifdef CONFIG_X86
10954 struct cpuinfo_x86 *cpuinfo;
10955#endif
James Smart840eda92020-03-22 11:13:00 -070010956#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
10957 struct lpfc_hdwq_stat *c_stat;
10958#endif
James Smart7bb03bb2013-04-17 20:19:16 -040010959
James Smart6a828b02019-01-28 11:14:31 -080010960 max_phys_id = 0;
James Smartd9954a22019-05-21 17:49:05 -070010961 min_phys_id = LPFC_VECTOR_MAP_EMPTY;
James Smart6a828b02019-01-28 11:14:31 -080010962 max_core_id = 0;
James Smartd9954a22019-05-21 17:49:05 -070010963 min_core_id = LPFC_VECTOR_MAP_EMPTY;
James Smart7bb03bb2013-04-17 20:19:16 -040010964
10965 /* Update CPU map with physical id and core id of each CPU */
James Smart222e9232019-01-28 11:14:35 -080010966 for_each_present_cpu(cpu) {
10967 cpup = &phba->sli4_hba.cpu_map[cpu];
James Smart7bb03bb2013-04-17 20:19:16 -040010968#ifdef CONFIG_X86
10969 cpuinfo = &cpu_data(cpu);
10970 cpup->phys_id = cpuinfo->phys_proc_id;
10971 cpup->core_id = cpuinfo->cpu_core_id;
James Smartd9954a22019-05-21 17:49:05 -070010972 if (lpfc_find_hyper(phba, cpu, cpup->phys_id, cpup->core_id))
10973 cpup->flag |= LPFC_CPU_MAP_HYPER;
James Smart7bb03bb2013-04-17 20:19:16 -040010974#else
10975 /* No distinction between CPUs for other platforms */
10976 cpup->phys_id = 0;
James Smart6a828b02019-01-28 11:14:31 -080010977 cpup->core_id = cpu;
James Smart7bb03bb2013-04-17 20:19:16 -040010978#endif
James Smart7bb03bb2013-04-17 20:19:16 -040010979
James Smartb3295c22019-01-28 11:14:30 -080010980 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart3ad348d2019-08-14 16:56:43 -070010981 "3328 CPU %d physid %d coreid %d flag x%x\n",
10982 cpu, cpup->phys_id, cpup->core_id, cpup->flag);
James Smart6a828b02019-01-28 11:14:31 -080010983
10984 if (cpup->phys_id > max_phys_id)
10985 max_phys_id = cpup->phys_id;
10986 if (cpup->phys_id < min_phys_id)
10987 min_phys_id = cpup->phys_id;
10988
10989 if (cpup->core_id > max_core_id)
10990 max_core_id = cpup->core_id;
10991 if (cpup->core_id < min_core_id)
10992 min_core_id = cpup->core_id;
James Smart7bb03bb2013-04-17 20:19:16 -040010993 }
James Smartb3295c22019-01-28 11:14:30 -080010994
James Smartd9954a22019-05-21 17:49:05 -070010995 /* After looking at each irq vector assigned to this pcidev, its
10996 * possible to see that not ALL CPUs have been accounted for.
James Smart657add42019-05-21 17:49:06 -070010997 * Next we will set any unassigned (unaffinitized) cpu map
10998 * entries to a IRQ on the same phys_id.
James Smartd9954a22019-05-21 17:49:05 -070010999 */
11000 first_cpu = cpumask_first(cpu_present_mask);
11001 start_cpu = first_cpu;
11002
11003 for_each_present_cpu(cpu) {
11004 cpup = &phba->sli4_hba.cpu_map[cpu];
11005
11006 /* Is this CPU entry unassigned */
11007 if (cpup->eq == LPFC_VECTOR_MAP_EMPTY) {
11008 /* Mark CPU as IRQ not assigned by the kernel */
11009 cpup->flag |= LPFC_CPU_MAP_UNASSIGN;
11010
James Smart657add42019-05-21 17:49:06 -070011011 /* If so, find a new_cpup thats on the the SAME
James Smartd9954a22019-05-21 17:49:05 -070011012 * phys_id as cpup. start_cpu will start where we
11013 * left off so all unassigned entries don't get assgined
11014 * the IRQ of the first entry.
11015 */
11016 new_cpu = start_cpu;
11017 for (i = 0; i < phba->sli4_hba.num_present_cpu; i++) {
11018 new_cpup = &phba->sli4_hba.cpu_map[new_cpu];
11019 if (!(new_cpup->flag & LPFC_CPU_MAP_UNASSIGN) &&
James Smartdcaa2132019-11-04 16:57:06 -080011020 (new_cpup->eq != LPFC_VECTOR_MAP_EMPTY) &&
James Smartd9954a22019-05-21 17:49:05 -070011021 (new_cpup->phys_id == cpup->phys_id))
11022 goto found_same;
11023 new_cpu = cpumask_next(
11024 new_cpu, cpu_present_mask);
11025 if (new_cpu == nr_cpumask_bits)
11026 new_cpu = first_cpu;
11027 }
11028 /* At this point, we leave the CPU as unassigned */
11029 continue;
11030found_same:
11031 /* We found a matching phys_id, so copy the IRQ info */
11032 cpup->eq = new_cpup->eq;
James Smartd9954a22019-05-21 17:49:05 -070011033
11034 /* Bump start_cpu to the next slot to minmize the
11035 * chance of having multiple unassigned CPU entries
11036 * selecting the same IRQ.
11037 */
11038 start_cpu = cpumask_next(new_cpu, cpu_present_mask);
11039 if (start_cpu == nr_cpumask_bits)
11040 start_cpu = first_cpu;
11041
James Smart657add42019-05-21 17:49:06 -070011042 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smartd9954a22019-05-21 17:49:05 -070011043 "3337 Set Affinity: CPU %d "
James Smartdcaa2132019-11-04 16:57:06 -080011044 "eq %d from peer cpu %d same "
James Smartd9954a22019-05-21 17:49:05 -070011045 "phys_id (%d)\n",
James Smartdcaa2132019-11-04 16:57:06 -080011046 cpu, cpup->eq, new_cpu,
11047 cpup->phys_id);
James Smartd9954a22019-05-21 17:49:05 -070011048 }
11049 }
11050
11051 /* Set any unassigned cpu map entries to a IRQ on any phys_id */
11052 start_cpu = first_cpu;
11053
11054 for_each_present_cpu(cpu) {
11055 cpup = &phba->sli4_hba.cpu_map[cpu];
11056
11057 /* Is this entry unassigned */
11058 if (cpup->eq == LPFC_VECTOR_MAP_EMPTY) {
11059 /* Mark it as IRQ not assigned by the kernel */
11060 cpup->flag |= LPFC_CPU_MAP_UNASSIGN;
11061
James Smart657add42019-05-21 17:49:06 -070011062 /* If so, find a new_cpup thats on ANY phys_id
James Smartd9954a22019-05-21 17:49:05 -070011063 * as the cpup. start_cpu will start where we
11064 * left off so all unassigned entries don't get
11065 * assigned the IRQ of the first entry.
11066 */
11067 new_cpu = start_cpu;
11068 for (i = 0; i < phba->sli4_hba.num_present_cpu; i++) {
11069 new_cpup = &phba->sli4_hba.cpu_map[new_cpu];
11070 if (!(new_cpup->flag & LPFC_CPU_MAP_UNASSIGN) &&
James Smartdcaa2132019-11-04 16:57:06 -080011071 (new_cpup->eq != LPFC_VECTOR_MAP_EMPTY))
James Smartd9954a22019-05-21 17:49:05 -070011072 goto found_any;
11073 new_cpu = cpumask_next(
11074 new_cpu, cpu_present_mask);
11075 if (new_cpu == nr_cpumask_bits)
11076 new_cpu = first_cpu;
11077 }
11078 /* We should never leave an entry unassigned */
11079 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11080 "3339 Set Affinity: CPU %d "
James Smartdcaa2132019-11-04 16:57:06 -080011081 "eq %d UNASSIGNED\n",
11082 cpup->hdwq, cpup->eq);
James Smartd9954a22019-05-21 17:49:05 -070011083 continue;
11084found_any:
11085 /* We found an available entry, copy the IRQ info */
11086 cpup->eq = new_cpup->eq;
James Smartd9954a22019-05-21 17:49:05 -070011087
11088 /* Bump start_cpu to the next slot to minmize the
11089 * chance of having multiple unassigned CPU entries
11090 * selecting the same IRQ.
11091 */
11092 start_cpu = cpumask_next(new_cpu, cpu_present_mask);
11093 if (start_cpu == nr_cpumask_bits)
11094 start_cpu = first_cpu;
11095
James Smart657add42019-05-21 17:49:06 -070011096 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smartd9954a22019-05-21 17:49:05 -070011097 "3338 Set Affinity: CPU %d "
James Smartdcaa2132019-11-04 16:57:06 -080011098 "eq %d from peer cpu %d (%d/%d)\n",
11099 cpu, cpup->eq, new_cpu,
James Smartd9954a22019-05-21 17:49:05 -070011100 new_cpup->phys_id, new_cpup->core_id);
11101 }
11102 }
James Smart657add42019-05-21 17:49:06 -070011103
James Smart3ad348d2019-08-14 16:56:43 -070011104 /* Assign hdwq indices that are unique across all cpus in the map
11105 * that are also FIRST_CPUs.
11106 */
11107 idx = 0;
11108 for_each_present_cpu(cpu) {
11109 cpup = &phba->sli4_hba.cpu_map[cpu];
11110
11111 /* Only FIRST IRQs get a hdwq index assignment. */
11112 if (!(cpup->flag & LPFC_CPU_FIRST_IRQ))
11113 continue;
11114
11115 /* 1 to 1, the first LPFC_CPU_FIRST_IRQ cpus to a unique hdwq */
11116 cpup->hdwq = idx;
11117 idx++;
Anton Blanchardbc2736e2020-07-13 18:39:08 +100011118 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart3ad348d2019-08-14 16:56:43 -070011119 "3333 Set Affinity: CPU %d (phys %d core %d): "
James Smartdcaa2132019-11-04 16:57:06 -080011120 "hdwq %d eq %d flg x%x\n",
James Smart3ad348d2019-08-14 16:56:43 -070011121 cpu, cpup->phys_id, cpup->core_id,
James Smartdcaa2132019-11-04 16:57:06 -080011122 cpup->hdwq, cpup->eq, cpup->flag);
James Smart3ad348d2019-08-14 16:56:43 -070011123 }
James Smartbc227dd2019-11-11 15:03:59 -080011124 /* Associate a hdwq with each cpu_map entry
James Smart657add42019-05-21 17:49:06 -070011125 * This will be 1 to 1 - hdwq to cpu, unless there are less
11126 * hardware queues then CPUs. For that case we will just round-robin
11127 * the available hardware queues as they get assigned to CPUs.
James Smart3ad348d2019-08-14 16:56:43 -070011128 * The next_idx is the idx from the FIRST_CPU loop above to account
11129 * for irq_chann < hdwq. The idx is used for round-robin assignments
11130 * and needs to start at 0.
James Smart657add42019-05-21 17:49:06 -070011131 */
James Smart3ad348d2019-08-14 16:56:43 -070011132 next_idx = idx;
James Smart657add42019-05-21 17:49:06 -070011133 start_cpu = 0;
James Smart3ad348d2019-08-14 16:56:43 -070011134 idx = 0;
James Smart657add42019-05-21 17:49:06 -070011135 for_each_present_cpu(cpu) {
11136 cpup = &phba->sli4_hba.cpu_map[cpu];
James Smart657add42019-05-21 17:49:06 -070011137
James Smart3ad348d2019-08-14 16:56:43 -070011138 /* FIRST cpus are already mapped. */
11139 if (cpup->flag & LPFC_CPU_FIRST_IRQ)
11140 continue;
James Smart657add42019-05-21 17:49:06 -070011141
James Smart3ad348d2019-08-14 16:56:43 -070011142 /* If the cfg_irq_chann < cfg_hdw_queue, set the hdwq
11143 * of the unassigned cpus to the next idx so that all
11144 * hdw queues are fully utilized.
11145 */
11146 if (next_idx < phba->cfg_hdw_queue) {
11147 cpup->hdwq = next_idx;
11148 next_idx++;
11149 continue;
James Smart657add42019-05-21 17:49:06 -070011150 }
James Smart3ad348d2019-08-14 16:56:43 -070011151
11152 /* Not a First CPU and all hdw_queues are used. Reuse a
11153 * Hardware Queue for another CPU, so be smart about it
11154 * and pick one that has its IRQ/EQ mapped to the same phys_id
11155 * (CPU package) and core_id.
11156 */
11157 new_cpu = start_cpu;
11158 for (i = 0; i < phba->sli4_hba.num_present_cpu; i++) {
11159 new_cpup = &phba->sli4_hba.cpu_map[new_cpu];
11160 if (new_cpup->hdwq != LPFC_VECTOR_MAP_EMPTY &&
11161 new_cpup->phys_id == cpup->phys_id &&
11162 new_cpup->core_id == cpup->core_id) {
11163 goto found_hdwq;
11164 }
11165 new_cpu = cpumask_next(new_cpu, cpu_present_mask);
11166 if (new_cpu == nr_cpumask_bits)
11167 new_cpu = first_cpu;
11168 }
11169
11170 /* If we can't match both phys_id and core_id,
11171 * settle for just a phys_id match.
11172 */
11173 new_cpu = start_cpu;
11174 for (i = 0; i < phba->sli4_hba.num_present_cpu; i++) {
11175 new_cpup = &phba->sli4_hba.cpu_map[new_cpu];
11176 if (new_cpup->hdwq != LPFC_VECTOR_MAP_EMPTY &&
11177 new_cpup->phys_id == cpup->phys_id)
11178 goto found_hdwq;
11179
11180 new_cpu = cpumask_next(new_cpu, cpu_present_mask);
11181 if (new_cpu == nr_cpumask_bits)
11182 new_cpu = first_cpu;
11183 }
11184
11185 /* Otherwise just round robin on cfg_hdw_queue */
11186 cpup->hdwq = idx % phba->cfg_hdw_queue;
11187 idx++;
11188 goto logit;
11189 found_hdwq:
11190 /* We found an available entry, copy the IRQ info */
11191 start_cpu = cpumask_next(new_cpu, cpu_present_mask);
11192 if (start_cpu == nr_cpumask_bits)
11193 start_cpu = first_cpu;
11194 cpup->hdwq = new_cpup->hdwq;
11195 logit:
Anton Blanchardbc2736e2020-07-13 18:39:08 +100011196 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart657add42019-05-21 17:49:06 -070011197 "3335 Set Affinity: CPU %d (phys %d core %d): "
James Smartdcaa2132019-11-04 16:57:06 -080011198 "hdwq %d eq %d flg x%x\n",
James Smart657add42019-05-21 17:49:06 -070011199 cpu, cpup->phys_id, cpup->core_id,
James Smartdcaa2132019-11-04 16:57:06 -080011200 cpup->hdwq, cpup->eq, cpup->flag);
James Smart657add42019-05-21 17:49:06 -070011201 }
11202
James Smartbc227dd2019-11-11 15:03:59 -080011203 /*
11204 * Initialize the cpu_map slots for not-present cpus in case
11205 * a cpu is hot-added. Perform a simple hdwq round robin assignment.
11206 */
11207 idx = 0;
11208 for_each_possible_cpu(cpu) {
11209 cpup = &phba->sli4_hba.cpu_map[cpu];
James Smart840eda92020-03-22 11:13:00 -070011210#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
11211 c_stat = per_cpu_ptr(phba->sli4_hba.c_stat, cpu);
11212 c_stat->hdwq_no = cpup->hdwq;
11213#endif
James Smartbc227dd2019-11-11 15:03:59 -080011214 if (cpup->hdwq != LPFC_VECTOR_MAP_EMPTY)
11215 continue;
11216
11217 cpup->hdwq = idx++ % phba->cfg_hdw_queue;
James Smart840eda92020-03-22 11:13:00 -070011218#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
11219 c_stat->hdwq_no = cpup->hdwq;
11220#endif
James Smartbc227dd2019-11-11 15:03:59 -080011221 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
11222 "3340 Set Affinity: not present "
11223 "CPU %d hdwq %d\n",
11224 cpu, cpup->hdwq);
James Smart657add42019-05-21 17:49:06 -070011225 }
11226
11227 /* The cpu_map array will be used later during initialization
11228 * when EQ / CQ / WQs are allocated and configured.
11229 */
James Smartb3295c22019-01-28 11:14:30 -080011230 return;
James Smart7bb03bb2013-04-17 20:19:16 -040011231}
James Smart7bb03bb2013-04-17 20:19:16 -040011232
11233/**
James Smart93a4d6f2019-11-04 16:57:05 -080011234 * lpfc_cpuhp_get_eq
11235 *
11236 * @phba: pointer to lpfc hba data structure.
11237 * @cpu: cpu going offline
Lee Jonesfe614ac2020-07-23 13:24:22 +010011238 * @eqlist: eq list to append to
James Smart93a4d6f2019-11-04 16:57:05 -080011239 */
James Smarta99c8072020-01-27 16:23:06 -080011240static int
James Smart93a4d6f2019-11-04 16:57:05 -080011241lpfc_cpuhp_get_eq(struct lpfc_hba *phba, unsigned int cpu,
11242 struct list_head *eqlist)
11243{
James Smart93a4d6f2019-11-04 16:57:05 -080011244 const struct cpumask *maskp;
11245 struct lpfc_queue *eq;
James Smarta99c8072020-01-27 16:23:06 -080011246 struct cpumask *tmp;
James Smart93a4d6f2019-11-04 16:57:05 -080011247 u16 idx;
11248
James Smarta99c8072020-01-27 16:23:06 -080011249 tmp = kzalloc(cpumask_size(), GFP_KERNEL);
11250 if (!tmp)
11251 return -ENOMEM;
11252
James Smart93a4d6f2019-11-04 16:57:05 -080011253 for (idx = 0; idx < phba->cfg_irq_chann; idx++) {
11254 maskp = pci_irq_get_affinity(phba->pcidev, idx);
11255 if (!maskp)
11256 continue;
11257 /*
11258 * if irq is not affinitized to the cpu going
11259 * then we don't need to poll the eq attached
11260 * to it.
11261 */
James Smarta99c8072020-01-27 16:23:06 -080011262 if (!cpumask_and(tmp, maskp, cpumask_of(cpu)))
James Smart93a4d6f2019-11-04 16:57:05 -080011263 continue;
11264 /* get the cpus that are online and are affini-
11265 * tized to this irq vector. If the count is
11266 * more than 1 then cpuhp is not going to shut-
11267 * down this vector. Since this cpu has not
11268 * gone offline yet, we need >1.
11269 */
James Smarta99c8072020-01-27 16:23:06 -080011270 cpumask_and(tmp, maskp, cpu_online_mask);
11271 if (cpumask_weight(tmp) > 1)
James Smart93a4d6f2019-11-04 16:57:05 -080011272 continue;
11273
11274 /* Now that we have an irq to shutdown, get the eq
11275 * mapped to this irq. Note: multiple hdwq's in
11276 * the software can share an eq, but eventually
11277 * only eq will be mapped to this vector
11278 */
James Smartdcaa2132019-11-04 16:57:06 -080011279 eq = phba->sli4_hba.hba_eq_hdl[idx].eq;
11280 list_add(&eq->_poll_list, eqlist);
James Smart93a4d6f2019-11-04 16:57:05 -080011281 }
James Smarta99c8072020-01-27 16:23:06 -080011282 kfree(tmp);
11283 return 0;
James Smart93a4d6f2019-11-04 16:57:05 -080011284}
11285
11286static void __lpfc_cpuhp_remove(struct lpfc_hba *phba)
11287{
11288 if (phba->sli_rev != LPFC_SLI_REV4)
11289 return;
11290
11291 cpuhp_state_remove_instance_nocalls(lpfc_cpuhp_state,
11292 &phba->cpuhp);
11293 /*
11294 * unregistering the instance doesn't stop the polling
11295 * timer. Wait for the poll timer to retire.
11296 */
11297 synchronize_rcu();
11298 del_timer_sync(&phba->cpuhp_poll_timer);
11299}
11300
11301static void lpfc_cpuhp_remove(struct lpfc_hba *phba)
11302{
11303 if (phba->pport->fc_flag & FC_OFFLINE_MODE)
11304 return;
11305
11306 __lpfc_cpuhp_remove(phba);
11307}
11308
11309static void lpfc_cpuhp_add(struct lpfc_hba *phba)
11310{
11311 if (phba->sli_rev != LPFC_SLI_REV4)
11312 return;
11313
11314 rcu_read_lock();
11315
James Smartf861f592020-03-22 11:12:54 -070011316 if (!list_empty(&phba->poll_list))
James Smart93a4d6f2019-11-04 16:57:05 -080011317 mod_timer(&phba->cpuhp_poll_timer,
11318 jiffies + msecs_to_jiffies(LPFC_POLL_HB));
James Smart93a4d6f2019-11-04 16:57:05 -080011319
11320 rcu_read_unlock();
11321
11322 cpuhp_state_add_instance_nocalls(lpfc_cpuhp_state,
11323 &phba->cpuhp);
11324}
11325
11326static int __lpfc_cpuhp_checks(struct lpfc_hba *phba, int *retval)
11327{
11328 if (phba->pport->load_flag & FC_UNLOADING) {
11329 *retval = -EAGAIN;
11330 return true;
11331 }
11332
11333 if (phba->sli_rev != LPFC_SLI_REV4) {
11334 *retval = 0;
11335 return true;
11336 }
11337
11338 /* proceed with the hotplug */
11339 return false;
11340}
11341
James Smartdcaa2132019-11-04 16:57:06 -080011342/**
11343 * lpfc_irq_set_aff - set IRQ affinity
11344 * @eqhdl: EQ handle
11345 * @cpu: cpu to set affinity
11346 *
11347 **/
11348static inline void
11349lpfc_irq_set_aff(struct lpfc_hba_eq_hdl *eqhdl, unsigned int cpu)
11350{
11351 cpumask_clear(&eqhdl->aff_mask);
11352 cpumask_set_cpu(cpu, &eqhdl->aff_mask);
11353 irq_set_status_flags(eqhdl->irq, IRQ_NO_BALANCING);
11354 irq_set_affinity_hint(eqhdl->irq, &eqhdl->aff_mask);
11355}
11356
11357/**
11358 * lpfc_irq_clear_aff - clear IRQ affinity
11359 * @eqhdl: EQ handle
11360 *
11361 **/
11362static inline void
11363lpfc_irq_clear_aff(struct lpfc_hba_eq_hdl *eqhdl)
11364{
11365 cpumask_clear(&eqhdl->aff_mask);
11366 irq_clear_status_flags(eqhdl->irq, IRQ_NO_BALANCING);
11367 irq_set_affinity_hint(eqhdl->irq, &eqhdl->aff_mask);
11368}
11369
11370/**
11371 * lpfc_irq_rebalance - rebalances IRQ affinity according to cpuhp event
11372 * @phba: pointer to HBA context object.
11373 * @cpu: cpu going offline/online
11374 * @offline: true, cpu is going offline. false, cpu is coming online.
11375 *
11376 * If cpu is going offline, we'll try our best effort to find the next
Dick Kennedy3048e3e2020-05-01 14:43:06 -070011377 * online cpu on the phba's original_mask and migrate all offlining IRQ
11378 * affinities.
James Smartdcaa2132019-11-04 16:57:06 -080011379 *
Dick Kennedy3048e3e2020-05-01 14:43:06 -070011380 * If cpu is coming online, reaffinitize the IRQ back to the onlining cpu.
James Smartdcaa2132019-11-04 16:57:06 -080011381 *
Dick Kennedy3048e3e2020-05-01 14:43:06 -070011382 * Note: Call only if NUMA or NHT mode is enabled, otherwise rely on
James Smartdcaa2132019-11-04 16:57:06 -080011383 * PCI_IRQ_AFFINITY to auto-manage IRQ affinity.
11384 *
11385 **/
11386static void
11387lpfc_irq_rebalance(struct lpfc_hba *phba, unsigned int cpu, bool offline)
11388{
11389 struct lpfc_vector_map_info *cpup;
11390 struct cpumask *aff_mask;
11391 unsigned int cpu_select, cpu_next, idx;
Dick Kennedy3048e3e2020-05-01 14:43:06 -070011392 const struct cpumask *orig_mask;
James Smartdcaa2132019-11-04 16:57:06 -080011393
Dick Kennedy3048e3e2020-05-01 14:43:06 -070011394 if (phba->irq_chann_mode == NORMAL_MODE)
James Smartdcaa2132019-11-04 16:57:06 -080011395 return;
11396
Dick Kennedy3048e3e2020-05-01 14:43:06 -070011397 orig_mask = &phba->sli4_hba.irq_aff_mask;
James Smartdcaa2132019-11-04 16:57:06 -080011398
Dick Kennedy3048e3e2020-05-01 14:43:06 -070011399 if (!cpumask_test_cpu(cpu, orig_mask))
James Smartdcaa2132019-11-04 16:57:06 -080011400 return;
11401
11402 cpup = &phba->sli4_hba.cpu_map[cpu];
11403
11404 if (!(cpup->flag & LPFC_CPU_FIRST_IRQ))
11405 return;
11406
11407 if (offline) {
Dick Kennedy3048e3e2020-05-01 14:43:06 -070011408 /* Find next online CPU on original mask */
11409 cpu_next = cpumask_next_wrap(cpu, orig_mask, cpu, true);
11410 cpu_select = lpfc_next_online_cpu(orig_mask, cpu_next);
James Smartdcaa2132019-11-04 16:57:06 -080011411
11412 /* Found a valid CPU */
11413 if ((cpu_select < nr_cpu_ids) && (cpu_select != cpu)) {
11414 /* Go through each eqhdl and ensure offlining
11415 * cpu aff_mask is migrated
11416 */
11417 for (idx = 0; idx < phba->cfg_irq_chann; idx++) {
11418 aff_mask = lpfc_get_aff_mask(idx);
11419
11420 /* Migrate affinity */
11421 if (cpumask_test_cpu(cpu, aff_mask))
11422 lpfc_irq_set_aff(lpfc_get_eq_hdl(idx),
11423 cpu_select);
11424 }
11425 } else {
11426 /* Rely on irqbalance if no online CPUs left on NUMA */
11427 for (idx = 0; idx < phba->cfg_irq_chann; idx++)
11428 lpfc_irq_clear_aff(lpfc_get_eq_hdl(idx));
11429 }
11430 } else {
11431 /* Migrate affinity back to this CPU */
11432 lpfc_irq_set_aff(lpfc_get_eq_hdl(cpup->eq), cpu);
11433 }
11434}
11435
James Smart93a4d6f2019-11-04 16:57:05 -080011436static int lpfc_cpu_offline(unsigned int cpu, struct hlist_node *node)
11437{
11438 struct lpfc_hba *phba = hlist_entry_safe(node, struct lpfc_hba, cpuhp);
11439 struct lpfc_queue *eq, *next;
11440 LIST_HEAD(eqlist);
11441 int retval;
11442
11443 if (!phba) {
11444 WARN_ONCE(!phba, "cpu: %u. phba:NULL", raw_smp_processor_id());
11445 return 0;
11446 }
11447
11448 if (__lpfc_cpuhp_checks(phba, &retval))
11449 return retval;
11450
James Smartdcaa2132019-11-04 16:57:06 -080011451 lpfc_irq_rebalance(phba, cpu, true);
11452
James Smarta99c8072020-01-27 16:23:06 -080011453 retval = lpfc_cpuhp_get_eq(phba, cpu, &eqlist);
11454 if (retval)
11455 return retval;
James Smart93a4d6f2019-11-04 16:57:05 -080011456
11457 /* start polling on these eq's */
11458 list_for_each_entry_safe(eq, next, &eqlist, _poll_list) {
11459 list_del_init(&eq->_poll_list);
11460 lpfc_sli4_start_polling(eq);
11461 }
11462
11463 return 0;
11464}
11465
11466static int lpfc_cpu_online(unsigned int cpu, struct hlist_node *node)
11467{
11468 struct lpfc_hba *phba = hlist_entry_safe(node, struct lpfc_hba, cpuhp);
11469 struct lpfc_queue *eq, *next;
11470 unsigned int n;
11471 int retval;
11472
11473 if (!phba) {
11474 WARN_ONCE(!phba, "cpu: %u. phba:NULL", raw_smp_processor_id());
11475 return 0;
11476 }
11477
11478 if (__lpfc_cpuhp_checks(phba, &retval))
11479 return retval;
11480
James Smartdcaa2132019-11-04 16:57:06 -080011481 lpfc_irq_rebalance(phba, cpu, false);
11482
James Smart93a4d6f2019-11-04 16:57:05 -080011483 list_for_each_entry_safe(eq, next, &phba->poll_list, _poll_list) {
11484 n = lpfc_find_cpu_handle(phba, eq->hdwq, LPFC_FIND_BY_HDWQ);
11485 if (n == cpu)
11486 lpfc_sli4_stop_polling(eq);
11487 }
11488
11489 return 0;
11490}
11491
11492/**
James Smartda0436e2009-05-22 14:51:39 -040011493 * lpfc_sli4_enable_msix - Enable MSI-X interrupt mode to SLI-4 device
11494 * @phba: pointer to lpfc hba data structure.
11495 *
11496 * This routine is invoked to enable the MSI-X interrupt vectors to device
James Smartdcaa2132019-11-04 16:57:06 -080011497 * with SLI-4 interface spec. It also allocates MSI-X vectors and maps them
11498 * to cpus on the system.
11499 *
11500 * When cfg_irq_numa is enabled, the adapter will only allocate vectors for
11501 * the number of cpus on the same numa node as this adapter. The vectors are
11502 * allocated without requesting OS affinity mapping. A vector will be
11503 * allocated and assigned to each online and offline cpu. If the cpu is
11504 * online, then affinity will be set to that cpu. If the cpu is offline, then
11505 * affinity will be set to the nearest peer cpu within the numa node that is
11506 * online. If there are no online cpus within the numa node, affinity is not
11507 * assigned and the OS may do as it pleases. Note: cpu vector affinity mapping
11508 * is consistent with the way cpu online/offline is handled when cfg_irq_numa is
11509 * configured.
11510 *
11511 * If numa mode is not enabled and there is more than 1 vector allocated, then
11512 * the driver relies on the managed irq interface where the OS assigns vector to
11513 * cpu affinity. The driver will then use that affinity mapping to setup its
11514 * cpu mapping table.
James Smartda0436e2009-05-22 14:51:39 -040011515 *
11516 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020011517 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -040011518 * other values - error
11519 **/
11520static int
11521lpfc_sli4_enable_msix(struct lpfc_hba *phba)
11522{
James Smart75baf692010-06-08 18:31:21 -040011523 int vectors, rc, index;
James Smartb83d0052017-06-01 21:07:05 -070011524 char *name;
Dick Kennedy3048e3e2020-05-01 14:43:06 -070011525 const struct cpumask *aff_mask = NULL;
James Smartdcaa2132019-11-04 16:57:06 -080011526 unsigned int cpu = 0, cpu_cnt = 0, cpu_select = nr_cpu_ids;
Dick Kennedy17105d92020-07-06 13:42:30 -070011527 struct lpfc_vector_map_info *cpup;
James Smartdcaa2132019-11-04 16:57:06 -080011528 struct lpfc_hba_eq_hdl *eqhdl;
11529 const struct cpumask *maskp;
James Smartdcaa2132019-11-04 16:57:06 -080011530 unsigned int flags = PCI_IRQ_MSIX;
James Smartda0436e2009-05-22 14:51:39 -040011531
11532 /* Set up MSI-X multi-message vectors */
James Smart6a828b02019-01-28 11:14:31 -080011533 vectors = phba->cfg_irq_chann;
Christoph Hellwig45ffac12017-02-12 13:52:26 -080011534
Dick Kennedy3048e3e2020-05-01 14:43:06 -070011535 if (phba->irq_chann_mode != NORMAL_MODE)
11536 aff_mask = &phba->sli4_hba.irq_aff_mask;
11537
11538 if (aff_mask) {
11539 cpu_cnt = cpumask_weight(aff_mask);
James Smartdcaa2132019-11-04 16:57:06 -080011540 vectors = min(phba->cfg_irq_chann, cpu_cnt);
11541
Dick Kennedy3048e3e2020-05-01 14:43:06 -070011542 /* cpu: iterates over aff_mask including offline or online
11543 * cpu_select: iterates over online aff_mask to set affinity
James Smartdcaa2132019-11-04 16:57:06 -080011544 */
Dick Kennedy3048e3e2020-05-01 14:43:06 -070011545 cpu = cpumask_first(aff_mask);
11546 cpu_select = lpfc_next_online_cpu(aff_mask, cpu);
James Smartdcaa2132019-11-04 16:57:06 -080011547 } else {
11548 flags |= PCI_IRQ_AFFINITY;
11549 }
11550
11551 rc = pci_alloc_irq_vectors(phba->pcidev, 1, vectors, flags);
Alexander Gordeev4f871e12014-09-03 12:56:29 -040011552 if (rc < 0) {
James Smartda0436e2009-05-22 14:51:39 -040011553 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
11554 "0484 PCI enable MSI-X failed (%d)\n", rc);
Alexander Gordeev029165a2014-07-16 20:05:15 +020011555 goto vec_fail_out;
James Smartda0436e2009-05-22 14:51:39 -040011556 }
Alexander Gordeev4f871e12014-09-03 12:56:29 -040011557 vectors = rc;
James Smart75baf692010-06-08 18:31:21 -040011558
James Smart7bb03bb2013-04-17 20:19:16 -040011559 /* Assign MSI-X vectors to interrupt handlers */
James Smart67d12732012-08-03 12:36:13 -040011560 for (index = 0; index < vectors; index++) {
James Smartdcaa2132019-11-04 16:57:06 -080011561 eqhdl = lpfc_get_eq_hdl(index);
11562 name = eqhdl->handler_name;
James Smartb83d0052017-06-01 21:07:05 -070011563 memset(name, 0, LPFC_SLI4_HANDLER_NAME_SZ);
11564 snprintf(name, LPFC_SLI4_HANDLER_NAME_SZ,
James Smart4305f182012-08-03 12:36:33 -040011565 LPFC_DRIVER_HANDLER_NAME"%d", index);
James Smartda0436e2009-05-22 14:51:39 -040011566
James Smartdcaa2132019-11-04 16:57:06 -080011567 eqhdl->idx = index;
James Smart7370d102019-01-28 11:14:20 -080011568 rc = request_irq(pci_irq_vector(phba->pcidev, index),
11569 &lpfc_sli4_hba_intr_handler, 0,
James Smartdcaa2132019-11-04 16:57:06 -080011570 name, eqhdl);
James Smartda0436e2009-05-22 14:51:39 -040011571 if (rc) {
11572 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
11573 "0486 MSI-X fast-path (%d) "
11574 "request_irq failed (%d)\n", index, rc);
11575 goto cfg_fail_out;
11576 }
James Smartdcaa2132019-11-04 16:57:06 -080011577
11578 eqhdl->irq = pci_irq_vector(phba->pcidev, index);
11579
Dick Kennedy3048e3e2020-05-01 14:43:06 -070011580 if (aff_mask) {
James Smartdcaa2132019-11-04 16:57:06 -080011581 /* If found a neighboring online cpu, set affinity */
11582 if (cpu_select < nr_cpu_ids)
11583 lpfc_irq_set_aff(eqhdl, cpu_select);
11584
11585 /* Assign EQ to cpu_map */
11586 lpfc_assign_eq_map_info(phba, index,
11587 LPFC_CPU_FIRST_IRQ,
11588 cpu);
11589
Dick Kennedy3048e3e2020-05-01 14:43:06 -070011590 /* Iterate to next offline or online cpu in aff_mask */
11591 cpu = cpumask_next(cpu, aff_mask);
James Smartdcaa2132019-11-04 16:57:06 -080011592
Dick Kennedy3048e3e2020-05-01 14:43:06 -070011593 /* Find next online cpu in aff_mask to set affinity */
11594 cpu_select = lpfc_next_online_cpu(aff_mask, cpu);
James Smartdcaa2132019-11-04 16:57:06 -080011595 } else if (vectors == 1) {
11596 cpu = cpumask_first(cpu_present_mask);
11597 lpfc_assign_eq_map_info(phba, index, LPFC_CPU_FIRST_IRQ,
11598 cpu);
11599 } else {
11600 maskp = pci_irq_get_affinity(phba->pcidev, index);
11601
James Smartdcaa2132019-11-04 16:57:06 -080011602 /* Loop through all CPUs associated with vector index */
11603 for_each_cpu_and(cpu, maskp, cpu_present_mask) {
Dick Kennedy17105d92020-07-06 13:42:30 -070011604 cpup = &phba->sli4_hba.cpu_map[cpu];
11605
James Smartdcaa2132019-11-04 16:57:06 -080011606 /* If this is the first CPU thats assigned to
11607 * this vector, set LPFC_CPU_FIRST_IRQ.
Dick Kennedy17105d92020-07-06 13:42:30 -070011608 *
11609 * With certain platforms its possible that irq
11610 * vectors are affinitized to all the cpu's.
11611 * This can result in each cpu_map.eq to be set
11612 * to the last vector, resulting in overwrite
11613 * of all the previous cpu_map.eq. Ensure that
11614 * each vector receives a place in cpu_map.
11615 * Later call to lpfc_cpu_affinity_check will
11616 * ensure we are nicely balanced out.
James Smartdcaa2132019-11-04 16:57:06 -080011617 */
Dick Kennedy17105d92020-07-06 13:42:30 -070011618 if (cpup->eq != LPFC_VECTOR_MAP_EMPTY)
11619 continue;
James Smartdcaa2132019-11-04 16:57:06 -080011620 lpfc_assign_eq_map_info(phba, index,
Dick Kennedy17105d92020-07-06 13:42:30 -070011621 LPFC_CPU_FIRST_IRQ,
James Smartdcaa2132019-11-04 16:57:06 -080011622 cpu);
Dick Kennedy17105d92020-07-06 13:42:30 -070011623 break;
James Smartdcaa2132019-11-04 16:57:06 -080011624 }
11625 }
James Smartda0436e2009-05-22 14:51:39 -040011626 }
11627
James Smart6a828b02019-01-28 11:14:31 -080011628 if (vectors != phba->cfg_irq_chann) {
Dick Kennedy372c1872020-06-30 14:50:00 -070011629 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart82c3e9b2012-09-29 11:29:50 -040011630 "3238 Reducing IO channels to match number of "
11631 "MSI-X vectors, requested %d got %d\n",
James Smart6a828b02019-01-28 11:14:31 -080011632 phba->cfg_irq_chann, vectors);
11633 if (phba->cfg_irq_chann > vectors)
11634 phba->cfg_irq_chann = vectors;
James Smart82c3e9b2012-09-29 11:29:50 -040011635 }
James Smart7bb03bb2013-04-17 20:19:16 -040011636
James Smartda0436e2009-05-22 14:51:39 -040011637 return rc;
11638
11639cfg_fail_out:
11640 /* free the irq already requested */
James Smartdcaa2132019-11-04 16:57:06 -080011641 for (--index; index >= 0; index--) {
11642 eqhdl = lpfc_get_eq_hdl(index);
11643 lpfc_irq_clear_aff(eqhdl);
11644 irq_set_affinity_hint(eqhdl->irq, NULL);
11645 free_irq(eqhdl->irq, eqhdl);
11646 }
James Smartda0436e2009-05-22 14:51:39 -040011647
James Smartda0436e2009-05-22 14:51:39 -040011648 /* Unconfigure MSI-X capability structure */
Christoph Hellwig45ffac12017-02-12 13:52:26 -080011649 pci_free_irq_vectors(phba->pcidev);
Alexander Gordeev029165a2014-07-16 20:05:15 +020011650
11651vec_fail_out:
James Smartda0436e2009-05-22 14:51:39 -040011652 return rc;
11653}
11654
11655/**
James Smartda0436e2009-05-22 14:51:39 -040011656 * lpfc_sli4_enable_msi - Enable MSI interrupt mode to SLI-4 device
11657 * @phba: pointer to lpfc hba data structure.
11658 *
11659 * This routine is invoked to enable the MSI interrupt mode to device with
James Smart07b1b912019-08-14 16:56:58 -070011660 * SLI-4 interface spec. The kernel function pci_alloc_irq_vectors() is
11661 * called to enable the MSI vector. The device driver is responsible for
11662 * calling the request_irq() to register MSI vector with a interrupt the
11663 * handler, which is done in this function.
James Smartda0436e2009-05-22 14:51:39 -040011664 *
11665 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020011666 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -040011667 * other values - error
11668 **/
11669static int
11670lpfc_sli4_enable_msi(struct lpfc_hba *phba)
11671{
11672 int rc, index;
James Smartdcaa2132019-11-04 16:57:06 -080011673 unsigned int cpu;
11674 struct lpfc_hba_eq_hdl *eqhdl;
James Smartda0436e2009-05-22 14:51:39 -040011675
James Smart07b1b912019-08-14 16:56:58 -070011676 rc = pci_alloc_irq_vectors(phba->pcidev, 1, 1,
11677 PCI_IRQ_MSI | PCI_IRQ_AFFINITY);
11678 if (rc > 0)
James Smartda0436e2009-05-22 14:51:39 -040011679 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
11680 "0487 PCI enable MSI mode success.\n");
11681 else {
11682 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
11683 "0488 PCI enable MSI mode failed (%d)\n", rc);
James Smart07b1b912019-08-14 16:56:58 -070011684 return rc ? rc : -1;
James Smartda0436e2009-05-22 14:51:39 -040011685 }
11686
11687 rc = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
James Smarted243d32015-05-21 13:55:25 -040011688 0, LPFC_DRIVER_NAME, phba);
James Smartda0436e2009-05-22 14:51:39 -040011689 if (rc) {
James Smart07b1b912019-08-14 16:56:58 -070011690 pci_free_irq_vectors(phba->pcidev);
James Smartda0436e2009-05-22 14:51:39 -040011691 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
11692 "0490 MSI request_irq failed (%d)\n", rc);
James Smart75baf692010-06-08 18:31:21 -040011693 return rc;
James Smartda0436e2009-05-22 14:51:39 -040011694 }
11695
James Smartdcaa2132019-11-04 16:57:06 -080011696 eqhdl = lpfc_get_eq_hdl(0);
11697 eqhdl->irq = pci_irq_vector(phba->pcidev, 0);
11698
11699 cpu = cpumask_first(cpu_present_mask);
11700 lpfc_assign_eq_map_info(phba, 0, LPFC_CPU_FIRST_IRQ, cpu);
11701
James Smart6a828b02019-01-28 11:14:31 -080011702 for (index = 0; index < phba->cfg_irq_chann; index++) {
James Smartdcaa2132019-11-04 16:57:06 -080011703 eqhdl = lpfc_get_eq_hdl(index);
11704 eqhdl->idx = index;
James Smartda0436e2009-05-22 14:51:39 -040011705 }
11706
James Smart75baf692010-06-08 18:31:21 -040011707 return 0;
James Smartda0436e2009-05-22 14:51:39 -040011708}
11709
11710/**
James Smartda0436e2009-05-22 14:51:39 -040011711 * lpfc_sli4_enable_intr - Enable device interrupt to SLI-4 device
11712 * @phba: pointer to lpfc hba data structure.
Lee Jonesfe614ac2020-07-23 13:24:22 +010011713 * @cfg_mode: Interrupt configuration mode (INTx, MSI or MSI-X).
James Smartda0436e2009-05-22 14:51:39 -040011714 *
11715 * This routine is invoked to enable device interrupt and associate driver's
11716 * interrupt handler(s) to interrupt vector(s) to device with SLI-4
11717 * interface spec. Depends on the interrupt mode configured to the driver,
11718 * the driver will try to fallback from the configured interrupt mode to an
11719 * interrupt mode which is supported by the platform, kernel, and device in
11720 * the order of:
11721 * MSI-X -> MSI -> IRQ.
11722 *
11723 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020011724 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -040011725 * other values - error
11726 **/
11727static uint32_t
11728lpfc_sli4_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
11729{
11730 uint32_t intr_mode = LPFC_INTR_ERROR;
James Smart895427b2017-02-12 13:52:30 -080011731 int retval, idx;
James Smartda0436e2009-05-22 14:51:39 -040011732
11733 if (cfg_mode == 2) {
11734 /* Preparation before conf_msi mbox cmd */
11735 retval = 0;
11736 if (!retval) {
11737 /* Now, try to enable MSI-X interrupt mode */
11738 retval = lpfc_sli4_enable_msix(phba);
11739 if (!retval) {
11740 /* Indicate initialization to MSI-X mode */
11741 phba->intr_type = MSIX;
11742 intr_mode = 2;
11743 }
11744 }
11745 }
11746
11747 /* Fallback to MSI if MSI-X initialization failed */
11748 if (cfg_mode >= 1 && phba->intr_type == NONE) {
11749 retval = lpfc_sli4_enable_msi(phba);
11750 if (!retval) {
11751 /* Indicate initialization to MSI mode */
11752 phba->intr_type = MSI;
11753 intr_mode = 1;
11754 }
11755 }
11756
11757 /* Fallback to INTx if both MSI-X/MSI initalization failed */
11758 if (phba->intr_type == NONE) {
11759 retval = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
11760 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
11761 if (!retval) {
James Smart895427b2017-02-12 13:52:30 -080011762 struct lpfc_hba_eq_hdl *eqhdl;
James Smartdcaa2132019-11-04 16:57:06 -080011763 unsigned int cpu;
James Smart895427b2017-02-12 13:52:30 -080011764
James Smartda0436e2009-05-22 14:51:39 -040011765 /* Indicate initialization to INTx mode */
11766 phba->intr_type = INTx;
11767 intr_mode = 0;
James Smart895427b2017-02-12 13:52:30 -080011768
James Smartdcaa2132019-11-04 16:57:06 -080011769 eqhdl = lpfc_get_eq_hdl(0);
11770 eqhdl->irq = pci_irq_vector(phba->pcidev, 0);
11771
11772 cpu = cpumask_first(cpu_present_mask);
11773 lpfc_assign_eq_map_info(phba, 0, LPFC_CPU_FIRST_IRQ,
11774 cpu);
James Smart6a828b02019-01-28 11:14:31 -080011775 for (idx = 0; idx < phba->cfg_irq_chann; idx++) {
James Smartdcaa2132019-11-04 16:57:06 -080011776 eqhdl = lpfc_get_eq_hdl(idx);
James Smart895427b2017-02-12 13:52:30 -080011777 eqhdl->idx = idx;
James Smart1ba981f2014-02-20 09:56:45 -050011778 }
James Smartda0436e2009-05-22 14:51:39 -040011779 }
11780 }
11781 return intr_mode;
11782}
11783
11784/**
11785 * lpfc_sli4_disable_intr - Disable device interrupt to SLI-4 device
11786 * @phba: pointer to lpfc hba data structure.
11787 *
11788 * This routine is invoked to disable device interrupt and disassociate
11789 * the driver's interrupt handler(s) from interrupt vector(s) to device
11790 * with SLI-4 interface spec. Depending on the interrupt mode, the driver
11791 * will release the interrupt vector(s) for the message signaled interrupt.
11792 **/
11793static void
11794lpfc_sli4_disable_intr(struct lpfc_hba *phba)
11795{
11796 /* Disable the currently initialized interrupt mode */
Christoph Hellwig45ffac12017-02-12 13:52:26 -080011797 if (phba->intr_type == MSIX) {
11798 int index;
James Smartdcaa2132019-11-04 16:57:06 -080011799 struct lpfc_hba_eq_hdl *eqhdl;
Christoph Hellwig45ffac12017-02-12 13:52:26 -080011800
11801 /* Free up MSI-X multi-message vectors */
James Smart6a828b02019-01-28 11:14:31 -080011802 for (index = 0; index < phba->cfg_irq_chann; index++) {
James Smartdcaa2132019-11-04 16:57:06 -080011803 eqhdl = lpfc_get_eq_hdl(index);
11804 lpfc_irq_clear_aff(eqhdl);
11805 irq_set_affinity_hint(eqhdl->irq, NULL);
11806 free_irq(eqhdl->irq, eqhdl);
James Smartb3295c22019-01-28 11:14:30 -080011807 }
Christoph Hellwig45ffac12017-02-12 13:52:26 -080011808 } else {
James Smartda0436e2009-05-22 14:51:39 -040011809 free_irq(phba->pcidev->irq, phba);
Christoph Hellwig45ffac12017-02-12 13:52:26 -080011810 }
11811
11812 pci_free_irq_vectors(phba->pcidev);
James Smartda0436e2009-05-22 14:51:39 -040011813
11814 /* Reset interrupt management states */
11815 phba->intr_type = NONE;
11816 phba->sli.slistat.sli_intr = 0;
James Smartda0436e2009-05-22 14:51:39 -040011817}
11818
11819/**
James Smart3772a992009-05-22 14:50:54 -040011820 * lpfc_unset_hba - Unset SLI3 hba device initialization
11821 * @phba: pointer to lpfc hba data structure.
11822 *
11823 * This routine is invoked to unset the HBA device initialization steps to
11824 * a device with SLI-3 interface spec.
11825 **/
11826static void
11827lpfc_unset_hba(struct lpfc_hba *phba)
11828{
11829 struct lpfc_vport *vport = phba->pport;
11830 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
11831
11832 spin_lock_irq(shost->host_lock);
11833 vport->load_flag |= FC_UNLOADING;
11834 spin_unlock_irq(shost->host_lock);
11835
James Smart72859902012-01-18 16:25:38 -050011836 kfree(phba->vpi_bmask);
11837 kfree(phba->vpi_ids);
11838
James Smart3772a992009-05-22 14:50:54 -040011839 lpfc_stop_hba_timers(phba);
11840
11841 phba->pport->work_port_events = 0;
11842
11843 lpfc_sli_hba_down(phba);
11844
11845 lpfc_sli_brdrestart(phba);
11846
11847 lpfc_sli_disable_intr(phba);
11848
11849 return;
11850}
11851
11852/**
James Smart5af5eee2010-10-22 11:06:38 -040011853 * lpfc_sli4_xri_exchange_busy_wait - Wait for device XRI exchange busy
11854 * @phba: Pointer to HBA context object.
11855 *
11856 * This function is called in the SLI4 code path to wait for completion
11857 * of device's XRIs exchange busy. It will check the XRI exchange busy
11858 * on outstanding FCP and ELS I/Os every 10ms for up to 10 seconds; after
11859 * that, it will check the XRI exchange busy on outstanding FCP and ELS
11860 * I/Os every 30 seconds, log error message, and wait forever. Only when
11861 * all XRI exchange busy complete, the driver unload shall proceed with
11862 * invoking the function reset ioctl mailbox command to the CNA and the
11863 * the rest of the driver unload resource release.
11864 **/
11865static void
11866lpfc_sli4_xri_exchange_busy_wait(struct lpfc_hba *phba)
11867{
James Smart5e5b5112019-01-28 11:14:22 -080011868 struct lpfc_sli4_hdw_queue *qp;
James Smartc00f62e2019-08-14 16:57:11 -070011869 int idx, ccnt;
James Smart5af5eee2010-10-22 11:06:38 -040011870 int wait_time = 0;
James Smart5e5b5112019-01-28 11:14:22 -080011871 int io_xri_cmpl = 1;
James Smart86c67372017-04-21 16:05:04 -070011872 int nvmet_xri_cmpl = 1;
James Smart5af5eee2010-10-22 11:06:38 -040011873 int els_xri_cmpl = list_empty(&phba->sli4_hba.lpfc_abts_els_sgl_list);
11874
James Smartc3725bd2017-11-20 16:00:42 -080011875 /* Driver just aborted IOs during the hba_unset process. Pause
11876 * here to give the HBA time to complete the IO and get entries
11877 * into the abts lists.
11878 */
11879 msleep(LPFC_XRI_EXCH_BUSY_WAIT_T1 * 5);
11880
11881 /* Wait for NVME pending IO to flush back to transport. */
11882 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
11883 lpfc_nvme_wait_for_io_drain(phba);
11884
James Smart5e5b5112019-01-28 11:14:22 -080011885 ccnt = 0;
James Smart5e5b5112019-01-28 11:14:22 -080011886 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
11887 qp = &phba->sli4_hba.hdwq[idx];
James Smartc00f62e2019-08-14 16:57:11 -070011888 io_xri_cmpl = list_empty(&qp->lpfc_abts_io_buf_list);
11889 if (!io_xri_cmpl) /* if list is NOT empty */
11890 ccnt++;
James Smart5e5b5112019-01-28 11:14:22 -080011891 }
11892 if (ccnt)
11893 io_xri_cmpl = 0;
James Smart5e5b5112019-01-28 11:14:22 -080011894
James Smart86c67372017-04-21 16:05:04 -070011895 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
James Smart86c67372017-04-21 16:05:04 -070011896 nvmet_xri_cmpl =
11897 list_empty(&phba->sli4_hba.lpfc_abts_nvmet_ctx_list);
11898 }
James Smart895427b2017-02-12 13:52:30 -080011899
James Smartc00f62e2019-08-14 16:57:11 -070011900 while (!els_xri_cmpl || !io_xri_cmpl || !nvmet_xri_cmpl) {
James Smart5af5eee2010-10-22 11:06:38 -040011901 if (wait_time > LPFC_XRI_EXCH_BUSY_WAIT_TMO) {
James Smart68c9b552018-06-26 08:24:25 -070011902 if (!nvmet_xri_cmpl)
Dick Kennedy372c1872020-06-30 14:50:00 -070011903 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart68c9b552018-06-26 08:24:25 -070011904 "6424 NVMET XRI exchange busy "
11905 "wait time: %d seconds.\n",
11906 wait_time/1000);
James Smart5e5b5112019-01-28 11:14:22 -080011907 if (!io_xri_cmpl)
Dick Kennedy372c1872020-06-30 14:50:00 -070011908 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartc00f62e2019-08-14 16:57:11 -070011909 "6100 IO XRI exchange busy "
James Smart5af5eee2010-10-22 11:06:38 -040011910 "wait time: %d seconds.\n",
11911 wait_time/1000);
11912 if (!els_xri_cmpl)
Dick Kennedy372c1872020-06-30 14:50:00 -070011913 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart5af5eee2010-10-22 11:06:38 -040011914 "2878 ELS XRI exchange busy "
11915 "wait time: %d seconds.\n",
11916 wait_time/1000);
11917 msleep(LPFC_XRI_EXCH_BUSY_WAIT_T2);
11918 wait_time += LPFC_XRI_EXCH_BUSY_WAIT_T2;
11919 } else {
11920 msleep(LPFC_XRI_EXCH_BUSY_WAIT_T1);
11921 wait_time += LPFC_XRI_EXCH_BUSY_WAIT_T1;
11922 }
James Smart5e5b5112019-01-28 11:14:22 -080011923
11924 ccnt = 0;
James Smart5e5b5112019-01-28 11:14:22 -080011925 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
11926 qp = &phba->sli4_hba.hdwq[idx];
James Smartc00f62e2019-08-14 16:57:11 -070011927 io_xri_cmpl = list_empty(
11928 &qp->lpfc_abts_io_buf_list);
11929 if (!io_xri_cmpl) /* if list is NOT empty */
11930 ccnt++;
James Smart5e5b5112019-01-28 11:14:22 -080011931 }
11932 if (ccnt)
11933 io_xri_cmpl = 0;
James Smart5e5b5112019-01-28 11:14:22 -080011934
James Smart86c67372017-04-21 16:05:04 -070011935 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
James Smart86c67372017-04-21 16:05:04 -070011936 nvmet_xri_cmpl = list_empty(
11937 &phba->sli4_hba.lpfc_abts_nvmet_ctx_list);
11938 }
James Smart5af5eee2010-10-22 11:06:38 -040011939 els_xri_cmpl =
11940 list_empty(&phba->sli4_hba.lpfc_abts_els_sgl_list);
James Smartf358dd02017-02-12 13:52:34 -080011941
James Smart5af5eee2010-10-22 11:06:38 -040011942 }
11943}
11944
11945/**
James Smartda0436e2009-05-22 14:51:39 -040011946 * lpfc_sli4_hba_unset - Unset the fcoe hba
11947 * @phba: Pointer to HBA context object.
11948 *
11949 * This function is called in the SLI4 code path to reset the HBA's FCoE
11950 * function. The caller is not required to hold any lock. This routine
11951 * issues PCI function reset mailbox command to reset the FCoE function.
11952 * At the end of the function, it calls lpfc_hba_down_post function to
11953 * free any pending commands.
11954 **/
11955static void
11956lpfc_sli4_hba_unset(struct lpfc_hba *phba)
11957{
11958 int wait_cnt = 0;
11959 LPFC_MBOXQ_t *mboxq;
James Smart912e3ac2011-05-24 11:42:11 -040011960 struct pci_dev *pdev = phba->pcidev;
James Smartda0436e2009-05-22 14:51:39 -040011961
11962 lpfc_stop_hba_timers(phba);
James Smartcdb42be2019-01-28 11:14:21 -080011963 if (phba->pport)
11964 phba->sli4_hba.intr_enable = 0;
James Smartda0436e2009-05-22 14:51:39 -040011965
11966 /*
11967 * Gracefully wait out the potential current outstanding asynchronous
11968 * mailbox command.
11969 */
11970
11971 /* First, block any pending async mailbox command from posted */
11972 spin_lock_irq(&phba->hbalock);
11973 phba->sli.sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
11974 spin_unlock_irq(&phba->hbalock);
11975 /* Now, trying to wait it out if we can */
11976 while (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
11977 msleep(10);
11978 if (++wait_cnt > LPFC_ACTIVE_MBOX_WAIT_CNT)
11979 break;
11980 }
11981 /* Forcefully release the outstanding mailbox command if timed out */
11982 if (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
11983 spin_lock_irq(&phba->hbalock);
11984 mboxq = phba->sli.mbox_active;
11985 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
11986 __lpfc_mbox_cmpl_put(phba, mboxq);
11987 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
11988 phba->sli.mbox_active = NULL;
11989 spin_unlock_irq(&phba->hbalock);
11990 }
11991
James Smart5af5eee2010-10-22 11:06:38 -040011992 /* Abort all iocbs associated with the hba */
11993 lpfc_sli_hba_iocb_abort(phba);
11994
11995 /* Wait for completion of device XRI exchange busy */
11996 lpfc_sli4_xri_exchange_busy_wait(phba);
11997
James Smart93a4d6f2019-11-04 16:57:05 -080011998 /* per-phba callback de-registration for hotplug event */
11999 lpfc_cpuhp_remove(phba);
12000
James Smartda0436e2009-05-22 14:51:39 -040012001 /* Disable PCI subsystem interrupt */
12002 lpfc_sli4_disable_intr(phba);
12003
James Smart912e3ac2011-05-24 11:42:11 -040012004 /* Disable SR-IOV if enabled */
12005 if (phba->cfg_sriov_nr_virtfn)
12006 pci_disable_sriov(pdev);
12007
James Smartda0436e2009-05-22 14:51:39 -040012008 /* Stop kthread signal shall trigger work_done one more time */
12009 kthread_stop(phba->worker_thread);
12010
James Smartd2cc9bc2018-09-10 10:30:50 -070012011 /* Disable FW logging to host memory */
James Smart1165a5c2018-11-29 16:09:39 -080012012 lpfc_ras_stop_fwlog(phba);
James Smartd2cc9bc2018-09-10 10:30:50 -070012013
James Smartd1f525a2017-04-21 16:04:55 -070012014 /* Unset the queues shared with the hardware then release all
12015 * allocated resources.
12016 */
12017 lpfc_sli4_queue_unset(phba);
12018 lpfc_sli4_queue_destroy(phba);
12019
James Smart3677a3a2010-09-29 11:19:14 -040012020 /* Reset SLI4 HBA FCoE function */
12021 lpfc_pci_function_reset(phba);
12022
James Smart1165a5c2018-11-29 16:09:39 -080012023 /* Free RAS DMA memory */
12024 if (phba->ras_fwlog.ras_enabled)
12025 lpfc_sli4_ras_dma_free(phba);
12026
James Smartda0436e2009-05-22 14:51:39 -040012027 /* Stop the SLI4 device port */
James Smart1ffdd2c2019-03-04 15:27:51 -080012028 if (phba->pport)
12029 phba->pport->work_port_events = 0;
James Smartda0436e2009-05-22 14:51:39 -040012030}
12031
James Smart28baac72010-02-12 14:42:03 -050012032 /**
12033 * lpfc_pc_sli4_params_get - Get the SLI4_PARAMS port capabilities.
12034 * @phba: Pointer to HBA context object.
12035 * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
12036 *
12037 * This function is called in the SLI4 code path to read the port's
12038 * sli4 capabilities.
12039 *
12040 * This function may be be called from any context that can block-wait
12041 * for the completion. The expectation is that this routine is called
12042 * typically from probe_one or from the online routine.
12043 **/
12044int
12045lpfc_pc_sli4_params_get(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
12046{
12047 int rc;
12048 struct lpfc_mqe *mqe;
12049 struct lpfc_pc_sli4_params *sli4_params;
12050 uint32_t mbox_tmo;
12051
12052 rc = 0;
12053 mqe = &mboxq->u.mqe;
12054
12055 /* Read the port's SLI4 Parameters port capabilities */
James Smartfedd3b72011-02-16 12:39:24 -050012056 lpfc_pc_sli4_params(mboxq);
James Smart28baac72010-02-12 14:42:03 -050012057 if (!phba->sli4_hba.intr_enable)
12058 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
12059 else {
James Smarta183a152011-10-10 21:32:43 -040012060 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
James Smart28baac72010-02-12 14:42:03 -050012061 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
12062 }
12063
12064 if (unlikely(rc))
12065 return 1;
12066
12067 sli4_params = &phba->sli4_hba.pc_sli4_params;
12068 sli4_params->if_type = bf_get(if_type, &mqe->un.sli4_params);
12069 sli4_params->sli_rev = bf_get(sli_rev, &mqe->un.sli4_params);
12070 sli4_params->sli_family = bf_get(sli_family, &mqe->un.sli4_params);
12071 sli4_params->featurelevel_1 = bf_get(featurelevel_1,
12072 &mqe->un.sli4_params);
12073 sli4_params->featurelevel_2 = bf_get(featurelevel_2,
12074 &mqe->un.sli4_params);
12075 sli4_params->proto_types = mqe->un.sli4_params.word3;
12076 sli4_params->sge_supp_len = mqe->un.sli4_params.sge_supp_len;
12077 sli4_params->if_page_sz = bf_get(if_page_sz, &mqe->un.sli4_params);
12078 sli4_params->rq_db_window = bf_get(rq_db_window, &mqe->un.sli4_params);
12079 sli4_params->loopbk_scope = bf_get(loopbk_scope, &mqe->un.sli4_params);
12080 sli4_params->eq_pages_max = bf_get(eq_pages, &mqe->un.sli4_params);
12081 sli4_params->eqe_size = bf_get(eqe_size, &mqe->un.sli4_params);
12082 sli4_params->cq_pages_max = bf_get(cq_pages, &mqe->un.sli4_params);
12083 sli4_params->cqe_size = bf_get(cqe_size, &mqe->un.sli4_params);
12084 sli4_params->mq_pages_max = bf_get(mq_pages, &mqe->un.sli4_params);
12085 sli4_params->mqe_size = bf_get(mqe_size, &mqe->un.sli4_params);
12086 sli4_params->mq_elem_cnt = bf_get(mq_elem_cnt, &mqe->un.sli4_params);
12087 sli4_params->wq_pages_max = bf_get(wq_pages, &mqe->un.sli4_params);
12088 sli4_params->wqe_size = bf_get(wqe_size, &mqe->un.sli4_params);
12089 sli4_params->rq_pages_max = bf_get(rq_pages, &mqe->un.sli4_params);
12090 sli4_params->rqe_size = bf_get(rqe_size, &mqe->un.sli4_params);
12091 sli4_params->hdr_pages_max = bf_get(hdr_pages, &mqe->un.sli4_params);
12092 sli4_params->hdr_size = bf_get(hdr_size, &mqe->un.sli4_params);
12093 sli4_params->hdr_pp_align = bf_get(hdr_pp_align, &mqe->un.sli4_params);
12094 sli4_params->sgl_pages_max = bf_get(sgl_pages, &mqe->un.sli4_params);
12095 sli4_params->sgl_pp_align = bf_get(sgl_pp_align, &mqe->un.sli4_params);
James Smart05580562011-05-24 11:40:48 -040012096
12097 /* Make sure that sge_supp_len can be handled by the driver */
12098 if (sli4_params->sge_supp_len > LPFC_MAX_SGE_SIZE)
12099 sli4_params->sge_supp_len = LPFC_MAX_SGE_SIZE;
12100
James Smart28baac72010-02-12 14:42:03 -050012101 return rc;
12102}
12103
James Smartda0436e2009-05-22 14:51:39 -040012104/**
James Smartfedd3b72011-02-16 12:39:24 -050012105 * lpfc_get_sli4_parameters - Get the SLI4 Config PARAMETERS.
12106 * @phba: Pointer to HBA context object.
12107 * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
12108 *
12109 * This function is called in the SLI4 code path to read the port's
12110 * sli4 capabilities.
12111 *
12112 * This function may be be called from any context that can block-wait
12113 * for the completion. The expectation is that this routine is called
12114 * typically from probe_one or from the online routine.
12115 **/
12116int
12117lpfc_get_sli4_parameters(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
12118{
12119 int rc;
12120 struct lpfc_mqe *mqe = &mboxq->u.mqe;
12121 struct lpfc_pc_sli4_params *sli4_params;
James Smarta183a152011-10-10 21:32:43 -040012122 uint32_t mbox_tmo;
James Smartfedd3b72011-02-16 12:39:24 -050012123 int length;
James Smartbf316c72018-04-09 14:24:28 -070012124 bool exp_wqcq_pages = true;
James Smartfedd3b72011-02-16 12:39:24 -050012125 struct lpfc_sli4_parameters *mbx_sli4_parameters;
12126
James Smart6d368e52011-05-24 11:44:12 -040012127 /*
12128 * By default, the driver assumes the SLI4 port requires RPI
12129 * header postings. The SLI4_PARAM response will correct this
12130 * assumption.
12131 */
12132 phba->sli4_hba.rpi_hdrs_in_use = 1;
12133
James Smartfedd3b72011-02-16 12:39:24 -050012134 /* Read the port's SLI4 Config Parameters */
12135 length = (sizeof(struct lpfc_mbx_get_sli4_parameters) -
12136 sizeof(struct lpfc_sli4_cfg_mhdr));
12137 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
12138 LPFC_MBOX_OPCODE_GET_SLI4_PARAMETERS,
12139 length, LPFC_SLI4_MBX_EMBED);
12140 if (!phba->sli4_hba.intr_enable)
12141 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smarta183a152011-10-10 21:32:43 -040012142 else {
12143 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
12144 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
12145 }
James Smartfedd3b72011-02-16 12:39:24 -050012146 if (unlikely(rc))
12147 return rc;
12148 sli4_params = &phba->sli4_hba.pc_sli4_params;
12149 mbx_sli4_parameters = &mqe->un.get_sli4_parameters.sli4_parameters;
12150 sli4_params->if_type = bf_get(cfg_if_type, mbx_sli4_parameters);
12151 sli4_params->sli_rev = bf_get(cfg_sli_rev, mbx_sli4_parameters);
12152 sli4_params->sli_family = bf_get(cfg_sli_family, mbx_sli4_parameters);
12153 sli4_params->featurelevel_1 = bf_get(cfg_sli_hint_1,
12154 mbx_sli4_parameters);
12155 sli4_params->featurelevel_2 = bf_get(cfg_sli_hint_2,
12156 mbx_sli4_parameters);
12157 if (bf_get(cfg_phwq, mbx_sli4_parameters))
12158 phba->sli3_options |= LPFC_SLI4_PHWQ_ENABLED;
12159 else
12160 phba->sli3_options &= ~LPFC_SLI4_PHWQ_ENABLED;
12161 sli4_params->sge_supp_len = mbx_sli4_parameters->sge_supp_len;
12162 sli4_params->loopbk_scope = bf_get(loopbk_scope, mbx_sli4_parameters);
James Smart1ba981f2014-02-20 09:56:45 -050012163 sli4_params->oas_supported = bf_get(cfg_oas, mbx_sli4_parameters);
James Smartfedd3b72011-02-16 12:39:24 -050012164 sli4_params->cqv = bf_get(cfg_cqv, mbx_sli4_parameters);
12165 sli4_params->mqv = bf_get(cfg_mqv, mbx_sli4_parameters);
12166 sli4_params->wqv = bf_get(cfg_wqv, mbx_sli4_parameters);
12167 sli4_params->rqv = bf_get(cfg_rqv, mbx_sli4_parameters);
James Smart7365f6f2018-02-22 08:18:46 -080012168 sli4_params->eqav = bf_get(cfg_eqav, mbx_sli4_parameters);
12169 sli4_params->cqav = bf_get(cfg_cqav, mbx_sli4_parameters);
James Smart0c651872013-07-15 18:33:23 -040012170 sli4_params->wqsize = bf_get(cfg_wqsize, mbx_sli4_parameters);
James Smart66e9e6b2018-06-26 08:24:27 -070012171 sli4_params->bv1s = bf_get(cfg_bv1s, mbx_sli4_parameters);
James Smart83c6cb12019-10-18 14:18:30 -070012172 sli4_params->pls = bf_get(cfg_pvl, mbx_sli4_parameters);
James Smartfedd3b72011-02-16 12:39:24 -050012173 sli4_params->sgl_pages_max = bf_get(cfg_sgl_page_cnt,
12174 mbx_sli4_parameters);
James Smart895427b2017-02-12 13:52:30 -080012175 sli4_params->wqpcnt = bf_get(cfg_wqpcnt, mbx_sli4_parameters);
James Smartfedd3b72011-02-16 12:39:24 -050012176 sli4_params->sgl_pp_align = bf_get(cfg_sgl_pp_align,
12177 mbx_sli4_parameters);
James Smart6d368e52011-05-24 11:44:12 -040012178 phba->sli4_hba.extents_in_use = bf_get(cfg_ext, mbx_sli4_parameters);
12179 phba->sli4_hba.rpi_hdrs_in_use = bf_get(cfg_hdrr, mbx_sli4_parameters);
James Smart895427b2017-02-12 13:52:30 -080012180
James Smartd79c9e92019-08-14 16:57:09 -070012181 /* Check for Extended Pre-Registered SGL support */
12182 phba->cfg_xpsgl = bf_get(cfg_xpsgl, mbx_sli4_parameters);
12183
James Smartc15e0702019-05-21 17:49:02 -070012184 /* Check for firmware nvme support */
12185 rc = (bf_get(cfg_nvme, mbx_sli4_parameters) &&
12186 bf_get(cfg_xib, mbx_sli4_parameters));
James Smart895427b2017-02-12 13:52:30 -080012187
James Smartc15e0702019-05-21 17:49:02 -070012188 if (rc) {
12189 /* Save this to indicate the Firmware supports NVME */
12190 sli4_params->nvme = 1;
12191
12192 /* Firmware NVME support, check driver FC4 NVME support */
12193 if (phba->cfg_enable_fc4_type == LPFC_ENABLE_FCP) {
12194 lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_NVME,
12195 "6133 Disabling NVME support: "
12196 "FC4 type not supported: x%x\n",
12197 phba->cfg_enable_fc4_type);
12198 goto fcponly;
12199 }
12200 } else {
12201 /* No firmware NVME support, check driver FC4 NVME support */
12202 sli4_params->nvme = 0;
12203 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
12204 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_NVME,
12205 "6101 Disabling NVME support: Not "
12206 "supported by firmware (%d %d) x%x\n",
12207 bf_get(cfg_nvme, mbx_sli4_parameters),
12208 bf_get(cfg_xib, mbx_sli4_parameters),
12209 phba->cfg_enable_fc4_type);
12210fcponly:
12211 phba->nvme_support = 0;
12212 phba->nvmet_support = 0;
12213 phba->cfg_nvmet_mrq = 0;
James Smart6a224b42019-08-14 16:56:57 -070012214 phba->cfg_nvme_seg_cnt = 0;
James Smartc15e0702019-05-21 17:49:02 -070012215
12216 /* If no FC4 type support, move to just SCSI support */
12217 if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP))
12218 return -ENODEV;
12219 phba->cfg_enable_fc4_type = LPFC_ENABLE_FCP;
12220 }
James Smart895427b2017-02-12 13:52:30 -080012221 }
James Smart05580562011-05-24 11:40:48 -040012222
James Smartc26c2652019-08-14 16:56:49 -070012223 /* If the NVME FC4 type is enabled, scale the sg_seg_cnt to
James Smarta5f73372019-09-21 20:58:50 -070012224 * accommodate 512K and 1M IOs in a single nvme buf.
James Smartc26c2652019-08-14 16:56:49 -070012225 */
James Smarta5f73372019-09-21 20:58:50 -070012226 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
James Smartc26c2652019-08-14 16:56:49 -070012227 phba->cfg_sg_seg_cnt = LPFC_MAX_NVME_SEG_CNT;
James Smartc26c2652019-08-14 16:56:49 -070012228
James Smart414abe02018-06-26 08:24:26 -070012229 /* Only embed PBDE for if_type 6, PBDE support requires xib be set */
12230 if ((bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
12231 LPFC_SLI_INTF_IF_TYPE_6) || (!bf_get(cfg_xib, mbx_sli4_parameters)))
12232 phba->cfg_enable_pbde = 0;
James Smart0bc2b7c2018-02-22 08:18:48 -080012233
James Smart20aefac2018-01-30 15:58:58 -080012234 /*
12235 * To support Suppress Response feature we must satisfy 3 conditions.
12236 * lpfc_suppress_rsp module parameter must be set (default).
12237 * In SLI4-Parameters Descriptor:
12238 * Extended Inline Buffers (XIB) must be supported.
12239 * Suppress Response IU Not Supported (SRIUNS) must NOT be supported
12240 * (double negative).
12241 */
12242 if (phba->cfg_suppress_rsp && bf_get(cfg_xib, mbx_sli4_parameters) &&
12243 !(bf_get(cfg_nosr, mbx_sli4_parameters)))
James Smartf358dd02017-02-12 13:52:34 -080012244 phba->sli.sli_flag |= LPFC_SLI_SUPPRESS_RSP;
James Smart20aefac2018-01-30 15:58:58 -080012245 else
12246 phba->cfg_suppress_rsp = 0;
James Smartf358dd02017-02-12 13:52:34 -080012247
James Smart0cf07f842017-06-01 21:07:10 -070012248 if (bf_get(cfg_eqdr, mbx_sli4_parameters))
12249 phba->sli.sli_flag |= LPFC_SLI_USE_EQDR;
12250
James Smart05580562011-05-24 11:40:48 -040012251 /* Make sure that sge_supp_len can be handled by the driver */
12252 if (sli4_params->sge_supp_len > LPFC_MAX_SGE_SIZE)
12253 sli4_params->sge_supp_len = LPFC_MAX_SGE_SIZE;
12254
James Smartb5c53952016-03-31 14:12:30 -070012255 /*
James Smartc176ffa2018-01-30 15:58:46 -080012256 * Check whether the adapter supports an embedded copy of the
12257 * FCP CMD IU within the WQE for FCP_Ixxx commands. In order
12258 * to use this option, 128-byte WQEs must be used.
James Smartb5c53952016-03-31 14:12:30 -070012259 */
12260 if (bf_get(cfg_ext_embed_cb, mbx_sli4_parameters))
12261 phba->fcp_embed_io = 1;
12262 else
12263 phba->fcp_embed_io = 0;
James Smart7bdedb32016-07-06 12:36:00 -070012264
James Smart0bc2b7c2018-02-22 08:18:48 -080012265 lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_NVME,
James Smart414abe02018-06-26 08:24:26 -070012266 "6422 XIB %d PBDE %d: FCP %d NVME %d %d %d\n",
James Smart0bc2b7c2018-02-22 08:18:48 -080012267 bf_get(cfg_xib, mbx_sli4_parameters),
James Smart414abe02018-06-26 08:24:26 -070012268 phba->cfg_enable_pbde,
12269 phba->fcp_embed_io, phba->nvme_support,
James Smart4e565cf2018-02-22 08:18:50 -080012270 phba->cfg_nvme_embed_cmd, phba->cfg_suppress_rsp);
James Smart0bc2b7c2018-02-22 08:18:48 -080012271
James Smartbf316c72018-04-09 14:24:28 -070012272 if ((bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
12273 LPFC_SLI_INTF_IF_TYPE_2) &&
12274 (bf_get(lpfc_sli_intf_sli_family, &phba->sli4_hba.sli_intf) ==
James Smartc2217682018-05-24 21:09:00 -070012275 LPFC_SLI_INTF_FAMILY_LNCR_A0))
James Smartbf316c72018-04-09 14:24:28 -070012276 exp_wqcq_pages = false;
12277
James Smartc176ffa2018-01-30 15:58:46 -080012278 if ((bf_get(cfg_cqpsize, mbx_sli4_parameters) & LPFC_CQ_16K_PAGE_SZ) &&
12279 (bf_get(cfg_wqpsize, mbx_sli4_parameters) & LPFC_WQ_16K_PAGE_SZ) &&
James Smartbf316c72018-04-09 14:24:28 -070012280 exp_wqcq_pages &&
James Smartc176ffa2018-01-30 15:58:46 -080012281 (sli4_params->wqsize & LPFC_WQ_SZ128_SUPPORT))
12282 phba->enab_exp_wqcq_pages = 1;
12283 else
12284 phba->enab_exp_wqcq_pages = 0;
James Smart7bdedb32016-07-06 12:36:00 -070012285 /*
12286 * Check if the SLI port supports MDS Diagnostics
12287 */
12288 if (bf_get(cfg_mds_diags, mbx_sli4_parameters))
12289 phba->mds_diags_support = 1;
12290 else
12291 phba->mds_diags_support = 0;
James Smartd2cc9bc2018-09-10 10:30:50 -070012292
James Smart0d8af092019-08-14 16:57:10 -070012293 /*
12294 * Check if the SLI port supports NSLER
12295 */
12296 if (bf_get(cfg_nsler, mbx_sli4_parameters))
12297 phba->nsler = 1;
12298 else
12299 phba->nsler = 0;
12300
James Smartfedd3b72011-02-16 12:39:24 -050012301 return 0;
12302}
12303
12304/**
James Smart3772a992009-05-22 14:50:54 -040012305 * lpfc_pci_probe_one_s3 - PCI probe func to reg SLI-3 device to PCI subsystem.
12306 * @pdev: pointer to PCI device
12307 * @pid: pointer to PCI device identifier
12308 *
12309 * This routine is to be called to attach a device with SLI-3 interface spec
12310 * to the PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
12311 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
12312 * information of the device and driver to see if the driver state that it can
12313 * support this kind of device. If the match is successful, the driver core
12314 * invokes this routine. If this routine determines it can claim the HBA, it
12315 * does all the initialization that it needs to do to handle the HBA properly.
12316 *
12317 * Return code
12318 * 0 - driver can claim the device
12319 * negative value - driver can not claim the device
12320 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080012321static int
James Smart3772a992009-05-22 14:50:54 -040012322lpfc_pci_probe_one_s3(struct pci_dev *pdev, const struct pci_device_id *pid)
12323{
12324 struct lpfc_hba *phba;
12325 struct lpfc_vport *vport = NULL;
James Smart6669f9b2009-10-02 15:16:45 -040012326 struct Scsi_Host *shost = NULL;
James Smart3772a992009-05-22 14:50:54 -040012327 int error;
12328 uint32_t cfg_mode, intr_mode;
12329
12330 /* Allocate memory for HBA structure */
12331 phba = lpfc_hba_alloc(pdev);
12332 if (!phba)
12333 return -ENOMEM;
12334
12335 /* Perform generic PCI device enabling operation */
12336 error = lpfc_enable_pci_dev(phba);
James Smart079b5c92011-08-21 21:48:49 -040012337 if (error)
James Smart3772a992009-05-22 14:50:54 -040012338 goto out_free_phba;
James Smart3772a992009-05-22 14:50:54 -040012339
12340 /* Set up SLI API function jump table for PCI-device group-0 HBAs */
12341 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_LP);
12342 if (error)
12343 goto out_disable_pci_dev;
12344
12345 /* Set up SLI-3 specific device PCI memory space */
12346 error = lpfc_sli_pci_mem_setup(phba);
12347 if (error) {
12348 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12349 "1402 Failed to set up pci memory space.\n");
12350 goto out_disable_pci_dev;
12351 }
12352
James Smart3772a992009-05-22 14:50:54 -040012353 /* Set up SLI-3 specific device driver resources */
12354 error = lpfc_sli_driver_resource_setup(phba);
12355 if (error) {
12356 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12357 "1404 Failed to set up driver resource.\n");
12358 goto out_unset_pci_mem_s3;
12359 }
12360
12361 /* Initialize and populate the iocb list per host */
James Smartd1f525a2017-04-21 16:04:55 -070012362
James Smart3772a992009-05-22 14:50:54 -040012363 error = lpfc_init_iocb_list(phba, LPFC_IOCB_LIST_CNT);
12364 if (error) {
12365 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12366 "1405 Failed to initialize iocb list.\n");
12367 goto out_unset_driver_resource_s3;
12368 }
12369
12370 /* Set up common device driver resources */
12371 error = lpfc_setup_driver_resource_phase2(phba);
12372 if (error) {
12373 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12374 "1406 Failed to set up driver resource.\n");
12375 goto out_free_iocb_list;
12376 }
12377
James Smart079b5c92011-08-21 21:48:49 -040012378 /* Get the default values for Model Name and Description */
12379 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
12380
James Smart3772a992009-05-22 14:50:54 -040012381 /* Create SCSI host to the physical port */
12382 error = lpfc_create_shost(phba);
12383 if (error) {
12384 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12385 "1407 Failed to create scsi host.\n");
12386 goto out_unset_driver_resource;
12387 }
12388
12389 /* Configure sysfs attributes */
12390 vport = phba->pport;
12391 error = lpfc_alloc_sysfs_attr(vport);
12392 if (error) {
12393 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12394 "1476 Failed to allocate sysfs attr\n");
12395 goto out_destroy_shost;
12396 }
12397
James Smart6669f9b2009-10-02 15:16:45 -040012398 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
James Smart3772a992009-05-22 14:50:54 -040012399 /* Now, trying to enable interrupt and bring up the device */
12400 cfg_mode = phba->cfg_use_msi;
12401 while (true) {
12402 /* Put device to a known state before enabling interrupt */
12403 lpfc_stop_port(phba);
12404 /* Configure and enable interrupt */
12405 intr_mode = lpfc_sli_enable_intr(phba, cfg_mode);
12406 if (intr_mode == LPFC_INTR_ERROR) {
Dick Kennedy372c1872020-06-30 14:50:00 -070012407 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart3772a992009-05-22 14:50:54 -040012408 "0431 Failed to enable interrupt.\n");
12409 error = -ENODEV;
12410 goto out_free_sysfs_attr;
12411 }
12412 /* SLI-3 HBA setup */
12413 if (lpfc_sli_hba_setup(phba)) {
Dick Kennedy372c1872020-06-30 14:50:00 -070012414 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart3772a992009-05-22 14:50:54 -040012415 "1477 Failed to set up hba\n");
12416 error = -ENODEV;
12417 goto out_remove_device;
12418 }
12419
12420 /* Wait 50ms for the interrupts of previous mailbox commands */
12421 msleep(50);
12422 /* Check active interrupts on message signaled interrupts */
12423 if (intr_mode == 0 ||
12424 phba->sli.slistat.sli_intr > LPFC_MSIX_VECTORS) {
12425 /* Log the current active interrupt mode */
12426 phba->intr_mode = intr_mode;
12427 lpfc_log_intr_mode(phba, intr_mode);
12428 break;
12429 } else {
12430 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
12431 "0447 Configure interrupt mode (%d) "
12432 "failed active interrupt test.\n",
12433 intr_mode);
12434 /* Disable the current interrupt mode */
12435 lpfc_sli_disable_intr(phba);
12436 /* Try next level of interrupt mode */
12437 cfg_mode = --intr_mode;
12438 }
12439 }
12440
12441 /* Perform post initialization setup */
12442 lpfc_post_init_setup(phba);
12443
12444 /* Check if there are static vports to be created. */
12445 lpfc_create_static_vport(phba);
12446
12447 return 0;
12448
12449out_remove_device:
12450 lpfc_unset_hba(phba);
12451out_free_sysfs_attr:
12452 lpfc_free_sysfs_attr(vport);
12453out_destroy_shost:
12454 lpfc_destroy_shost(phba);
12455out_unset_driver_resource:
12456 lpfc_unset_driver_resource_phase2(phba);
12457out_free_iocb_list:
12458 lpfc_free_iocb_list(phba);
12459out_unset_driver_resource_s3:
12460 lpfc_sli_driver_resource_unset(phba);
12461out_unset_pci_mem_s3:
12462 lpfc_sli_pci_mem_unset(phba);
12463out_disable_pci_dev:
12464 lpfc_disable_pci_dev(phba);
James Smart6669f9b2009-10-02 15:16:45 -040012465 if (shost)
12466 scsi_host_put(shost);
James Smart3772a992009-05-22 14:50:54 -040012467out_free_phba:
12468 lpfc_hba_free(phba);
12469 return error;
12470}
12471
12472/**
12473 * lpfc_pci_remove_one_s3 - PCI func to unreg SLI-3 device from PCI subsystem.
James Smarte59058c2008-08-24 21:49:00 -040012474 * @pdev: pointer to PCI device
12475 *
James Smart3772a992009-05-22 14:50:54 -040012476 * This routine is to be called to disattach a device with SLI-3 interface
12477 * spec from PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
12478 * removed from PCI bus, it performs all the necessary cleanup for the HBA
12479 * device to be removed from the PCI subsystem properly.
James Smarte59058c2008-08-24 21:49:00 -040012480 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080012481static void
James Smart3772a992009-05-22 14:50:54 -040012482lpfc_pci_remove_one_s3(struct pci_dev *pdev)
dea31012005-04-17 16:05:31 -050012483{
James Smart2e0fef82007-06-17 19:56:36 -050012484 struct Scsi_Host *shost = pci_get_drvdata(pdev);
12485 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
James Smarteada2722008-12-04 22:39:13 -050012486 struct lpfc_vport **vports;
James Smart2e0fef82007-06-17 19:56:36 -050012487 struct lpfc_hba *phba = vport->phba;
James Smarteada2722008-12-04 22:39:13 -050012488 int i;
Tomohiro Kusumi8a4df1202008-01-11 01:53:00 -050012489
James Smart549e55c2007-08-02 11:09:51 -040012490 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -040012491 vport->load_flag |= FC_UNLOADING;
James Smart549e55c2007-08-02 11:09:51 -040012492 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050012493
James Smart858c9f62007-06-17 19:56:39 -050012494 lpfc_free_sysfs_attr(vport);
12495
James Smarteada2722008-12-04 22:39:13 -050012496 /* Release all the vports against this physical port */
12497 vports = lpfc_create_vport_work_array(phba);
12498 if (vports != NULL)
James Smart587a37f2012-05-09 21:16:03 -040012499 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
12500 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
12501 continue;
James Smarteada2722008-12-04 22:39:13 -050012502 fc_vport_terminate(vports[i]->fc_vport);
James Smart587a37f2012-05-09 21:16:03 -040012503 }
James Smarteada2722008-12-04 22:39:13 -050012504 lpfc_destroy_vport_work_array(phba, vports);
12505
12506 /* Remove FC host and then SCSI host with the physical port */
James Smart858c9f62007-06-17 19:56:39 -050012507 fc_remove_host(shost);
12508 scsi_remove_host(shost);
James Smartd613b6a2017-02-12 13:52:37 -080012509
James Smart87af33f2007-10-27 13:37:43 -040012510 lpfc_cleanup(vport);
12511
James Smart2e0fef82007-06-17 19:56:36 -050012512 /*
12513 * Bring down the SLI Layer. This step disable all interrupts,
12514 * clears the rings, discards all mailbox commands, and resets
12515 * the HBA.
12516 */
James Smarta257bf92009-04-06 18:48:10 -040012517
Justin P. Mattock48e34d02010-12-30 15:07:58 -080012518 /* HBA interrupt will be disabled after this call */
James Smart2e0fef82007-06-17 19:56:36 -050012519 lpfc_sli_hba_down(phba);
James Smarta257bf92009-04-06 18:48:10 -040012520 /* Stop kthread signal shall trigger work_done one more time */
12521 kthread_stop(phba->worker_thread);
12522 /* Final cleanup of txcmplq and reset the HBA */
James Smart2e0fef82007-06-17 19:56:36 -050012523 lpfc_sli_brdrestart(phba);
12524
James Smart72859902012-01-18 16:25:38 -050012525 kfree(phba->vpi_bmask);
12526 kfree(phba->vpi_ids);
12527
James Smart3772a992009-05-22 14:50:54 -040012528 lpfc_stop_hba_timers(phba);
James Smart523128e2018-09-10 10:30:46 -070012529 spin_lock_irq(&phba->port_list_lock);
James Smart858c9f62007-06-17 19:56:39 -050012530 list_del_init(&vport->listentry);
James Smart523128e2018-09-10 10:30:46 -070012531 spin_unlock_irq(&phba->port_list_lock);
James Smart858c9f62007-06-17 19:56:39 -050012532
James Smart858c9f62007-06-17 19:56:39 -050012533 lpfc_debugfs_terminate(vport);
James Smart2e0fef82007-06-17 19:56:36 -050012534
James Smart912e3ac2011-05-24 11:42:11 -040012535 /* Disable SR-IOV if enabled */
12536 if (phba->cfg_sriov_nr_virtfn)
12537 pci_disable_sriov(pdev);
12538
James Smart5b75da22008-12-04 22:39:35 -050012539 /* Disable interrupt */
James Smart3772a992009-05-22 14:50:54 -040012540 lpfc_sli_disable_intr(phba);
dea31012005-04-17 16:05:31 -050012541
James Smart858c9f62007-06-17 19:56:39 -050012542 scsi_host_put(shost);
James Smart2e0fef82007-06-17 19:56:36 -050012543
12544 /*
12545 * Call scsi_free before mem_free since scsi bufs are released to their
12546 * corresponding pools here.
12547 */
12548 lpfc_scsi_free(phba);
James Smart0794d602019-01-28 11:14:19 -080012549 lpfc_free_iocb_list(phba);
12550
James Smart3772a992009-05-22 14:50:54 -040012551 lpfc_mem_free_all(phba);
James Smart2e0fef82007-06-17 19:56:36 -050012552
James Smart34b02dc2008-08-24 21:49:55 -040012553 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
12554 phba->hbqslimp.virt, phba->hbqslimp.phys);
James Smarted957682007-06-17 19:56:37 -050012555
James Smart2e0fef82007-06-17 19:56:36 -050012556 /* Free resources associated with SLI2 interface */
12557 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
James Smart34b02dc2008-08-24 21:49:55 -040012558 phba->slim2p.virt, phba->slim2p.phys);
James Smart2e0fef82007-06-17 19:56:36 -050012559
12560 /* unmap adapter SLIM and Control Registers */
12561 iounmap(phba->ctrl_regs_memmap_p);
12562 iounmap(phba->slim_memmap_p);
12563
James Smart3772a992009-05-22 14:50:54 -040012564 lpfc_hba_free(phba);
James Smart2e0fef82007-06-17 19:56:36 -050012565
Johannes Thumshirne0c04832016-06-07 09:44:03 +020012566 pci_release_mem_regions(pdev);
James Smart2e0fef82007-06-17 19:56:36 -050012567 pci_disable_device(pdev);
dea31012005-04-17 16:05:31 -050012568}
12569
Linas Vepstas8d63f372007-02-14 14:28:36 -060012570/**
James Smart3772a992009-05-22 14:50:54 -040012571 * lpfc_pci_suspend_one_s3 - PCI func to suspend SLI-3 device for power mgmnt
James Smart3a55b532008-12-04 22:38:54 -050012572 * @pdev: pointer to PCI device
12573 * @msg: power management message
12574 *
James Smart3772a992009-05-22 14:50:54 -040012575 * This routine is to be called from the kernel's PCI subsystem to support
12576 * system Power Management (PM) to device with SLI-3 interface spec. When
12577 * PM invokes this method, it quiesces the device by stopping the driver's
12578 * worker thread for the device, turning off device's interrupt and DMA,
12579 * and bring the device offline. Note that as the driver implements the
12580 * minimum PM requirements to a power-aware driver's PM support for the
12581 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
12582 * to the suspend() method call will be treated as SUSPEND and the driver will
12583 * fully reinitialize its device during resume() method call, the driver will
12584 * set device to PCI_D3hot state in PCI config space instead of setting it
12585 * according to the @msg provided by the PM.
James Smart3a55b532008-12-04 22:38:54 -050012586 *
12587 * Return code
James Smart3772a992009-05-22 14:50:54 -040012588 * 0 - driver suspended the device
12589 * Error otherwise
James Smart3a55b532008-12-04 22:38:54 -050012590 **/
12591static int
James Smart3772a992009-05-22 14:50:54 -040012592lpfc_pci_suspend_one_s3(struct pci_dev *pdev, pm_message_t msg)
James Smart3a55b532008-12-04 22:38:54 -050012593{
12594 struct Scsi_Host *shost = pci_get_drvdata(pdev);
12595 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
12596
12597 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
12598 "0473 PCI device Power Management suspend.\n");
12599
12600 /* Bring down the device */
James Smart618a5232012-06-12 13:54:36 -040012601 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart3a55b532008-12-04 22:38:54 -050012602 lpfc_offline(phba);
12603 kthread_stop(phba->worker_thread);
12604
12605 /* Disable interrupt from device */
James Smart3772a992009-05-22 14:50:54 -040012606 lpfc_sli_disable_intr(phba);
James Smart3a55b532008-12-04 22:38:54 -050012607
12608 /* Save device state to PCI config space */
12609 pci_save_state(pdev);
12610 pci_set_power_state(pdev, PCI_D3hot);
12611
12612 return 0;
12613}
12614
12615/**
James Smart3772a992009-05-22 14:50:54 -040012616 * lpfc_pci_resume_one_s3 - PCI func to resume SLI-3 device for power mgmnt
James Smart3a55b532008-12-04 22:38:54 -050012617 * @pdev: pointer to PCI device
12618 *
James Smart3772a992009-05-22 14:50:54 -040012619 * This routine is to be called from the kernel's PCI subsystem to support
12620 * system Power Management (PM) to device with SLI-3 interface spec. When PM
12621 * invokes this method, it restores the device's PCI config space state and
12622 * fully reinitializes the device and brings it online. Note that as the
12623 * driver implements the minimum PM requirements to a power-aware driver's
12624 * PM for suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE,
12625 * FREEZE) to the suspend() method call will be treated as SUSPEND and the
12626 * driver will fully reinitialize its device during resume() method call,
12627 * the device will be set to PCI_D0 directly in PCI config space before
12628 * restoring the state.
James Smart3a55b532008-12-04 22:38:54 -050012629 *
12630 * Return code
James Smart3772a992009-05-22 14:50:54 -040012631 * 0 - driver suspended the device
12632 * Error otherwise
James Smart3a55b532008-12-04 22:38:54 -050012633 **/
12634static int
James Smart3772a992009-05-22 14:50:54 -040012635lpfc_pci_resume_one_s3(struct pci_dev *pdev)
James Smart3a55b532008-12-04 22:38:54 -050012636{
12637 struct Scsi_Host *shost = pci_get_drvdata(pdev);
12638 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
James Smart5b75da22008-12-04 22:39:35 -050012639 uint32_t intr_mode;
James Smart3a55b532008-12-04 22:38:54 -050012640 int error;
12641
12642 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
12643 "0452 PCI device Power Management resume.\n");
12644
12645 /* Restore device state from PCI config space */
12646 pci_set_power_state(pdev, PCI_D0);
12647 pci_restore_state(pdev);
James Smart0d878412009-10-02 15:16:56 -040012648
James Smart1dfb5a42010-02-12 14:40:50 -050012649 /*
12650 * As the new kernel behavior of pci_restore_state() API call clears
12651 * device saved_state flag, need to save the restored state again.
12652 */
12653 pci_save_state(pdev);
12654
James Smart3a55b532008-12-04 22:38:54 -050012655 if (pdev->is_busmaster)
12656 pci_set_master(pdev);
12657
12658 /* Startup the kernel thread for this host adapter. */
12659 phba->worker_thread = kthread_run(lpfc_do_work, phba,
12660 "lpfc_worker_%d", phba->brd_no);
12661 if (IS_ERR(phba->worker_thread)) {
12662 error = PTR_ERR(phba->worker_thread);
12663 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12664 "0434 PM resume failed to start worker "
12665 "thread: error=x%x.\n", error);
12666 return error;
12667 }
12668
James Smart5b75da22008-12-04 22:39:35 -050012669 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -040012670 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -050012671 if (intr_mode == LPFC_INTR_ERROR) {
Dick Kennedy372c1872020-06-30 14:50:00 -070012672 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart5b75da22008-12-04 22:39:35 -050012673 "0430 PM resume Failed to enable interrupt\n");
12674 return -EIO;
12675 } else
12676 phba->intr_mode = intr_mode;
James Smart3a55b532008-12-04 22:38:54 -050012677
12678 /* Restart HBA and bring it online */
12679 lpfc_sli_brdrestart(phba);
12680 lpfc_online(phba);
12681
James Smart5b75da22008-12-04 22:39:35 -050012682 /* Log the current active interrupt mode */
12683 lpfc_log_intr_mode(phba, phba->intr_mode);
12684
James Smart3a55b532008-12-04 22:38:54 -050012685 return 0;
12686}
12687
12688/**
James Smart891478a2009-11-18 15:40:23 -050012689 * lpfc_sli_prep_dev_for_recover - Prepare SLI3 device for pci slot recover
12690 * @phba: pointer to lpfc hba data structure.
12691 *
12692 * This routine is called to prepare the SLI3 device for PCI slot recover. It
James Smarte2af0d22010-03-15 11:25:32 -040012693 * aborts all the outstanding SCSI I/Os to the pci device.
James Smart891478a2009-11-18 15:40:23 -050012694 **/
12695static void
12696lpfc_sli_prep_dev_for_recover(struct lpfc_hba *phba)
12697{
Dick Kennedy372c1872020-06-30 14:50:00 -070012698 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart891478a2009-11-18 15:40:23 -050012699 "2723 PCI channel I/O abort preparing for recovery\n");
James Smarte2af0d22010-03-15 11:25:32 -040012700
12701 /*
12702 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
12703 * and let the SCSI mid-layer to retry them to recover.
12704 */
James Smartdb55fba2014-04-04 13:52:02 -040012705 lpfc_sli_abort_fcp_rings(phba);
James Smart891478a2009-11-18 15:40:23 -050012706}
12707
12708/**
James Smart0d878412009-10-02 15:16:56 -040012709 * lpfc_sli_prep_dev_for_reset - Prepare SLI3 device for pci slot reset
12710 * @phba: pointer to lpfc hba data structure.
12711 *
12712 * This routine is called to prepare the SLI3 device for PCI slot reset. It
12713 * disables the device interrupt and pci device, and aborts the internal FCP
12714 * pending I/Os.
12715 **/
12716static void
12717lpfc_sli_prep_dev_for_reset(struct lpfc_hba *phba)
12718{
Dick Kennedy372c1872020-06-30 14:50:00 -070012719 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart891478a2009-11-18 15:40:23 -050012720 "2710 PCI channel disable preparing for reset\n");
James Smarte2af0d22010-03-15 11:25:32 -040012721
James Smart75baf692010-06-08 18:31:21 -040012722 /* Block any management I/Os to the device */
James Smart618a5232012-06-12 13:54:36 -040012723 lpfc_block_mgmt_io(phba, LPFC_MBX_WAIT);
James Smart75baf692010-06-08 18:31:21 -040012724
James Smarte2af0d22010-03-15 11:25:32 -040012725 /* Block all SCSI devices' I/Os on the host */
12726 lpfc_scsi_dev_block(phba);
12727
James Smartea714f32013-04-17 20:18:47 -040012728 /* Flush all driver's outstanding SCSI I/Os as we are to reset */
James Smartc00f62e2019-08-14 16:57:11 -070012729 lpfc_sli_flush_io_rings(phba);
James Smartea714f32013-04-17 20:18:47 -040012730
James Smarte2af0d22010-03-15 11:25:32 -040012731 /* stop all timers */
12732 lpfc_stop_hba_timers(phba);
12733
James Smart0d878412009-10-02 15:16:56 -040012734 /* Disable interrupt and pci device */
12735 lpfc_sli_disable_intr(phba);
12736 pci_disable_device(phba->pcidev);
James Smart0d878412009-10-02 15:16:56 -040012737}
12738
12739/**
12740 * lpfc_sli_prep_dev_for_perm_failure - Prepare SLI3 dev for pci slot disable
12741 * @phba: pointer to lpfc hba data structure.
12742 *
12743 * This routine is called to prepare the SLI3 device for PCI slot permanently
12744 * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
12745 * pending I/Os.
12746 **/
12747static void
James Smart75baf692010-06-08 18:31:21 -040012748lpfc_sli_prep_dev_for_perm_failure(struct lpfc_hba *phba)
James Smart0d878412009-10-02 15:16:56 -040012749{
Dick Kennedy372c1872020-06-30 14:50:00 -070012750 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart891478a2009-11-18 15:40:23 -050012751 "2711 PCI channel permanent disable for failure\n");
James Smarte2af0d22010-03-15 11:25:32 -040012752 /* Block all SCSI devices' I/Os on the host */
12753 lpfc_scsi_dev_block(phba);
12754
12755 /* stop all timers */
12756 lpfc_stop_hba_timers(phba);
12757
James Smart0d878412009-10-02 15:16:56 -040012758 /* Clean up all driver's outstanding SCSI I/Os */
James Smartc00f62e2019-08-14 16:57:11 -070012759 lpfc_sli_flush_io_rings(phba);
James Smart0d878412009-10-02 15:16:56 -040012760}
12761
12762/**
James Smart3772a992009-05-22 14:50:54 -040012763 * lpfc_io_error_detected_s3 - Method for handling SLI-3 device PCI I/O error
James Smarte59058c2008-08-24 21:49:00 -040012764 * @pdev: pointer to PCI device.
12765 * @state: the current PCI connection state.
Linas Vepstas8d63f372007-02-14 14:28:36 -060012766 *
James Smart3772a992009-05-22 14:50:54 -040012767 * This routine is called from the PCI subsystem for I/O error handling to
12768 * device with SLI-3 interface spec. This function is called by the PCI
12769 * subsystem after a PCI bus error affecting this device has been detected.
12770 * When this function is invoked, it will need to stop all the I/Os and
12771 * interrupt(s) to the device. Once that is done, it will return
12772 * PCI_ERS_RESULT_NEED_RESET for the PCI subsystem to perform proper recovery
12773 * as desired.
James Smarte59058c2008-08-24 21:49:00 -040012774 *
12775 * Return codes
James Smart0d878412009-10-02 15:16:56 -040012776 * PCI_ERS_RESULT_CAN_RECOVER - can be recovered with reset_link
James Smart3772a992009-05-22 14:50:54 -040012777 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
12778 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
James Smarte59058c2008-08-24 21:49:00 -040012779 **/
James Smart3772a992009-05-22 14:50:54 -040012780static pci_ers_result_t
12781lpfc_io_error_detected_s3(struct pci_dev *pdev, pci_channel_state_t state)
Linas Vepstas8d63f372007-02-14 14:28:36 -060012782{
James Smart51ef4c22007-08-02 11:10:31 -040012783 struct Scsi_Host *shost = pci_get_drvdata(pdev);
12784 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -060012785
James Smart0d878412009-10-02 15:16:56 -040012786 switch (state) {
12787 case pci_channel_io_normal:
James Smart891478a2009-11-18 15:40:23 -050012788 /* Non-fatal error, prepare for recovery */
12789 lpfc_sli_prep_dev_for_recover(phba);
James Smart0d878412009-10-02 15:16:56 -040012790 return PCI_ERS_RESULT_CAN_RECOVER;
12791 case pci_channel_io_frozen:
12792 /* Fatal error, prepare for slot reset */
12793 lpfc_sli_prep_dev_for_reset(phba);
12794 return PCI_ERS_RESULT_NEED_RESET;
12795 case pci_channel_io_perm_failure:
12796 /* Permanent failure, prepare for device down */
James Smart75baf692010-06-08 18:31:21 -040012797 lpfc_sli_prep_dev_for_perm_failure(phba);
Linas Vepstas8d63f372007-02-14 14:28:36 -060012798 return PCI_ERS_RESULT_DISCONNECT;
James Smart0d878412009-10-02 15:16:56 -040012799 default:
12800 /* Unknown state, prepare and request slot reset */
Dick Kennedy372c1872020-06-30 14:50:00 -070012801 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart0d878412009-10-02 15:16:56 -040012802 "0472 Unknown PCI error state: x%x\n", state);
12803 lpfc_sli_prep_dev_for_reset(phba);
12804 return PCI_ERS_RESULT_NEED_RESET;
James Smarta8e497d2008-08-24 21:50:11 -040012805 }
Linas Vepstas8d63f372007-02-14 14:28:36 -060012806}
12807
12808/**
James Smart3772a992009-05-22 14:50:54 -040012809 * lpfc_io_slot_reset_s3 - Method for restarting PCI SLI-3 device from scratch.
James Smarte59058c2008-08-24 21:49:00 -040012810 * @pdev: pointer to PCI device.
Linas Vepstas8d63f372007-02-14 14:28:36 -060012811 *
James Smart3772a992009-05-22 14:50:54 -040012812 * This routine is called from the PCI subsystem for error handling to
12813 * device with SLI-3 interface spec. This is called after PCI bus has been
12814 * reset to restart the PCI card from scratch, as if from a cold-boot.
12815 * During the PCI subsystem error recovery, after driver returns
12816 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
12817 * recovery and then call this routine before calling the .resume method
12818 * to recover the device. This function will initialize the HBA device,
12819 * enable the interrupt, but it will just put the HBA to offline state
12820 * without passing any I/O traffic.
James Smarte59058c2008-08-24 21:49:00 -040012821 *
12822 * Return codes
James Smart3772a992009-05-22 14:50:54 -040012823 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
12824 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
Linas Vepstas8d63f372007-02-14 14:28:36 -060012825 */
James Smart3772a992009-05-22 14:50:54 -040012826static pci_ers_result_t
12827lpfc_io_slot_reset_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -060012828{
James Smart51ef4c22007-08-02 11:10:31 -040012829 struct Scsi_Host *shost = pci_get_drvdata(pdev);
12830 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -060012831 struct lpfc_sli *psli = &phba->sli;
James Smart5b75da22008-12-04 22:39:35 -050012832 uint32_t intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -060012833
12834 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
Benjamin Herrenschmidt09483912007-12-20 15:28:09 +110012835 if (pci_enable_device_mem(pdev)) {
Linas Vepstas8d63f372007-02-14 14:28:36 -060012836 printk(KERN_ERR "lpfc: Cannot re-enable "
12837 "PCI device after reset.\n");
12838 return PCI_ERS_RESULT_DISCONNECT;
12839 }
12840
James Smart97207482008-12-04 22:39:19 -050012841 pci_restore_state(pdev);
James Smart1dfb5a42010-02-12 14:40:50 -050012842
12843 /*
12844 * As the new kernel behavior of pci_restore_state() API call clears
12845 * device saved_state flag, need to save the restored state again.
12846 */
12847 pci_save_state(pdev);
12848
James Smart97207482008-12-04 22:39:19 -050012849 if (pdev->is_busmaster)
12850 pci_set_master(pdev);
Linas Vepstas8d63f372007-02-14 14:28:36 -060012851
James Smart92d7f7b2007-06-17 19:56:38 -050012852 spin_lock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040012853 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -050012854 spin_unlock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -060012855
James Smart5b75da22008-12-04 22:39:35 -050012856 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -040012857 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -050012858 if (intr_mode == LPFC_INTR_ERROR) {
Dick Kennedy372c1872020-06-30 14:50:00 -070012859 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart5b75da22008-12-04 22:39:35 -050012860 "0427 Cannot re-enable interrupt after "
12861 "slot reset.\n");
12862 return PCI_ERS_RESULT_DISCONNECT;
12863 } else
12864 phba->intr_mode = intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -060012865
James Smart75baf692010-06-08 18:31:21 -040012866 /* Take device offline, it will perform cleanup */
James Smart618a5232012-06-12 13:54:36 -040012867 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
Linas Vepstas8d63f372007-02-14 14:28:36 -060012868 lpfc_offline(phba);
12869 lpfc_sli_brdrestart(phba);
12870
James Smart5b75da22008-12-04 22:39:35 -050012871 /* Log the current active interrupt mode */
12872 lpfc_log_intr_mode(phba, phba->intr_mode);
12873
Linas Vepstas8d63f372007-02-14 14:28:36 -060012874 return PCI_ERS_RESULT_RECOVERED;
12875}
12876
12877/**
James Smart3772a992009-05-22 14:50:54 -040012878 * lpfc_io_resume_s3 - Method for resuming PCI I/O operation on SLI-3 device.
James Smarte59058c2008-08-24 21:49:00 -040012879 * @pdev: pointer to PCI device
Linas Vepstas8d63f372007-02-14 14:28:36 -060012880 *
James Smart3772a992009-05-22 14:50:54 -040012881 * This routine is called from the PCI subsystem for error handling to device
12882 * with SLI-3 interface spec. It is called when kernel error recovery tells
12883 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
12884 * error recovery. After this call, traffic can start to flow from this device
12885 * again.
Linas Vepstas8d63f372007-02-14 14:28:36 -060012886 */
James Smart3772a992009-05-22 14:50:54 -040012887static void
12888lpfc_io_resume_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -060012889{
James Smart51ef4c22007-08-02 11:10:31 -040012890 struct Scsi_Host *shost = pci_get_drvdata(pdev);
12891 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -060012892
James Smarte2af0d22010-03-15 11:25:32 -040012893 /* Bring device online, it will be no-op for non-fatal error resume */
James Smart58da1ff2008-04-07 10:15:56 -040012894 lpfc_online(phba);
Linas Vepstas8d63f372007-02-14 14:28:36 -060012895}
12896
James Smart3772a992009-05-22 14:50:54 -040012897/**
James Smartda0436e2009-05-22 14:51:39 -040012898 * lpfc_sli4_get_els_iocb_cnt - Calculate the # of ELS IOCBs to reserve
12899 * @phba: pointer to lpfc hba data structure.
12900 *
12901 * returns the number of ELS/CT IOCBs to reserve
12902 **/
12903int
12904lpfc_sli4_get_els_iocb_cnt(struct lpfc_hba *phba)
12905{
12906 int max_xri = phba->sli4_hba.max_cfg_param.max_xri;
12907
James Smartf1126682009-06-10 17:22:44 -040012908 if (phba->sli_rev == LPFC_SLI_REV4) {
12909 if (max_xri <= 100)
James Smart6a9c52c2009-10-02 15:16:51 -040012910 return 10;
James Smartf1126682009-06-10 17:22:44 -040012911 else if (max_xri <= 256)
James Smart6a9c52c2009-10-02 15:16:51 -040012912 return 25;
James Smartf1126682009-06-10 17:22:44 -040012913 else if (max_xri <= 512)
James Smart6a9c52c2009-10-02 15:16:51 -040012914 return 50;
James Smartf1126682009-06-10 17:22:44 -040012915 else if (max_xri <= 1024)
James Smart6a9c52c2009-10-02 15:16:51 -040012916 return 100;
James Smart8a9d2e82012-05-09 21:16:12 -040012917 else if (max_xri <= 1536)
James Smart6a9c52c2009-10-02 15:16:51 -040012918 return 150;
James Smart8a9d2e82012-05-09 21:16:12 -040012919 else if (max_xri <= 2048)
12920 return 200;
12921 else
12922 return 250;
James Smartf1126682009-06-10 17:22:44 -040012923 } else
12924 return 0;
James Smartda0436e2009-05-22 14:51:39 -040012925}
12926
12927/**
James Smart895427b2017-02-12 13:52:30 -080012928 * lpfc_sli4_get_iocb_cnt - Calculate the # of total IOCBs to reserve
12929 * @phba: pointer to lpfc hba data structure.
12930 *
James Smartf358dd02017-02-12 13:52:34 -080012931 * returns the number of ELS/CT + NVMET IOCBs to reserve
James Smart895427b2017-02-12 13:52:30 -080012932 **/
12933int
12934lpfc_sli4_get_iocb_cnt(struct lpfc_hba *phba)
12935{
12936 int max_xri = lpfc_sli4_get_els_iocb_cnt(phba);
12937
James Smartf358dd02017-02-12 13:52:34 -080012938 if (phba->nvmet_support)
12939 max_xri += LPFC_NVMET_BUF_POST;
James Smart895427b2017-02-12 13:52:30 -080012940 return max_xri;
12941}
12942
12943
James Smart0a5ce732019-10-18 14:18:18 -070012944static int
James Smart1feb8202018-02-22 08:18:47 -080012945lpfc_log_write_firmware_error(struct lpfc_hba *phba, uint32_t offset,
12946 uint32_t magic_number, uint32_t ftype, uint32_t fid, uint32_t fsize,
12947 const struct firmware *fw)
12948{
James Smart0a5ce732019-10-18 14:18:18 -070012949 int rc;
James Smart1feb8202018-02-22 08:18:47 -080012950
James Smart0a5ce732019-10-18 14:18:18 -070012951 /* Three cases: (1) FW was not supported on the detected adapter.
12952 * (2) FW update has been locked out administratively.
12953 * (3) Some other error during FW update.
12954 * In each case, an unmaskable message is written to the console
12955 * for admin diagnosis.
12956 */
12957 if (offset == ADD_STATUS_FW_NOT_SUPPORTED ||
James Smart1feb8202018-02-22 08:18:47 -080012958 (phba->pcidev->device == PCI_DEVICE_ID_LANCER_G6_FC &&
James Smart5792a0e2019-10-25 11:43:42 -070012959 magic_number != MAGIC_NUMBER_G6) ||
James Smart1feb8202018-02-22 08:18:47 -080012960 (phba->pcidev->device == PCI_DEVICE_ID_LANCER_G7_FC &&
James Smart5792a0e2019-10-25 11:43:42 -070012961 magic_number != MAGIC_NUMBER_G7)) {
Dick Kennedy372c1872020-06-30 14:50:00 -070012962 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart0a5ce732019-10-18 14:18:18 -070012963 "3030 This firmware version is not supported on"
12964 " this HBA model. Device:%x Magic:%x Type:%x "
12965 "ID:%x Size %d %zd\n",
12966 phba->pcidev->device, magic_number, ftype, fid,
12967 fsize, fw->size);
12968 rc = -EINVAL;
12969 } else if (offset == ADD_STATUS_FW_DOWNLOAD_HW_DISABLED) {
Dick Kennedy372c1872020-06-30 14:50:00 -070012970 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart0a5ce732019-10-18 14:18:18 -070012971 "3021 Firmware downloads have been prohibited "
12972 "by a system configuration setting on "
12973 "Device:%x Magic:%x Type:%x ID:%x Size %d "
12974 "%zd\n",
12975 phba->pcidev->device, magic_number, ftype, fid,
12976 fsize, fw->size);
12977 rc = -EACCES;
12978 } else {
Dick Kennedy372c1872020-06-30 14:50:00 -070012979 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart0a5ce732019-10-18 14:18:18 -070012980 "3022 FW Download failed. Add Status x%x "
12981 "Device:%x Magic:%x Type:%x ID:%x Size %d "
12982 "%zd\n",
12983 offset, phba->pcidev->device, magic_number,
12984 ftype, fid, fsize, fw->size);
12985 rc = -EIO;
12986 }
12987 return rc;
James Smart1feb8202018-02-22 08:18:47 -080012988}
James Smart1feb8202018-02-22 08:18:47 -080012989
James Smart895427b2017-02-12 13:52:30 -080012990/**
James Smart52d52442011-05-24 11:42:45 -040012991 * lpfc_write_firmware - attempt to write a firmware image to the port
James Smart52d52442011-05-24 11:42:45 -040012992 * @fw: pointer to firmware image returned from request_firmware.
James Smart0a5ce732019-10-18 14:18:18 -070012993 * @context: pointer to firmware image returned from request_firmware.
James Smart52d52442011-05-24 11:42:45 -040012994 *
James Smart52d52442011-05-24 11:42:45 -040012995 **/
James Smartce396282012-09-29 11:30:56 -040012996static void
12997lpfc_write_firmware(const struct firmware *fw, void *context)
James Smart52d52442011-05-24 11:42:45 -040012998{
James Smartce396282012-09-29 11:30:56 -040012999 struct lpfc_hba *phba = (struct lpfc_hba *)context;
James Smart6b5151f2012-01-18 16:24:06 -050013000 char fwrev[FW_REV_STR_SIZE];
James Smartce396282012-09-29 11:30:56 -040013001 struct lpfc_grp_hdr *image;
James Smart52d52442011-05-24 11:42:45 -040013002 struct list_head dma_buffer_list;
13003 int i, rc = 0;
13004 struct lpfc_dmabuf *dmabuf, *next;
13005 uint32_t offset = 0, temp_offset = 0;
James Smart6b6ef5d2016-10-13 15:06:17 -070013006 uint32_t magic_number, ftype, fid, fsize;
James Smart52d52442011-05-24 11:42:45 -040013007
James Smartc71ab862012-10-31 14:44:33 -040013008 /* It can be null in no-wait mode, sanity check */
James Smartce396282012-09-29 11:30:56 -040013009 if (!fw) {
13010 rc = -ENXIO;
13011 goto out;
13012 }
13013 image = (struct lpfc_grp_hdr *)fw->data;
13014
James Smart6b6ef5d2016-10-13 15:06:17 -070013015 magic_number = be32_to_cpu(image->magic_number);
13016 ftype = bf_get_be32(lpfc_grp_hdr_file_type, image);
James Smart1feb8202018-02-22 08:18:47 -080013017 fid = bf_get_be32(lpfc_grp_hdr_id, image);
James Smart6b6ef5d2016-10-13 15:06:17 -070013018 fsize = be32_to_cpu(image->size);
13019
James Smart52d52442011-05-24 11:42:45 -040013020 INIT_LIST_HEAD(&dma_buffer_list);
James Smart52d52442011-05-24 11:42:45 -040013021 lpfc_decode_firmware_rev(phba, fwrev, 1);
James Smart88a2cfb2011-07-22 18:36:33 -040013022 if (strncmp(fwrev, image->revision, strnlen(image->revision, 16))) {
Dick Kennedy372c1872020-06-30 14:50:00 -070013023 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartce396282012-09-29 11:30:56 -040013024 "3023 Updating Firmware, Current Version:%s "
James Smart52d52442011-05-24 11:42:45 -040013025 "New Version:%s\n",
James Smart88a2cfb2011-07-22 18:36:33 -040013026 fwrev, image->revision);
James Smart52d52442011-05-24 11:42:45 -040013027 for (i = 0; i < LPFC_MBX_WR_CONFIG_MAX_BDE; i++) {
13028 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf),
13029 GFP_KERNEL);
13030 if (!dmabuf) {
13031 rc = -ENOMEM;
James Smartce396282012-09-29 11:30:56 -040013032 goto release_out;
James Smart52d52442011-05-24 11:42:45 -040013033 }
13034 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
13035 SLI4_PAGE_SIZE,
13036 &dmabuf->phys,
13037 GFP_KERNEL);
13038 if (!dmabuf->virt) {
13039 kfree(dmabuf);
13040 rc = -ENOMEM;
James Smartce396282012-09-29 11:30:56 -040013041 goto release_out;
James Smart52d52442011-05-24 11:42:45 -040013042 }
13043 list_add_tail(&dmabuf->list, &dma_buffer_list);
13044 }
13045 while (offset < fw->size) {
13046 temp_offset = offset;
13047 list_for_each_entry(dmabuf, &dma_buffer_list, list) {
James Smart079b5c92011-08-21 21:48:49 -040013048 if (temp_offset + SLI4_PAGE_SIZE > fw->size) {
James Smart52d52442011-05-24 11:42:45 -040013049 memcpy(dmabuf->virt,
13050 fw->data + temp_offset,
James Smart079b5c92011-08-21 21:48:49 -040013051 fw->size - temp_offset);
13052 temp_offset = fw->size;
James Smart52d52442011-05-24 11:42:45 -040013053 break;
13054 }
James Smart52d52442011-05-24 11:42:45 -040013055 memcpy(dmabuf->virt, fw->data + temp_offset,
13056 SLI4_PAGE_SIZE);
James Smart88a2cfb2011-07-22 18:36:33 -040013057 temp_offset += SLI4_PAGE_SIZE;
James Smart52d52442011-05-24 11:42:45 -040013058 }
13059 rc = lpfc_wr_object(phba, &dma_buffer_list,
13060 (fw->size - offset), &offset);
James Smart1feb8202018-02-22 08:18:47 -080013061 if (rc) {
James Smart0a5ce732019-10-18 14:18:18 -070013062 rc = lpfc_log_write_firmware_error(phba, offset,
13063 magic_number,
13064 ftype,
13065 fid,
13066 fsize,
13067 fw);
James Smartce396282012-09-29 11:30:56 -040013068 goto release_out;
James Smart1feb8202018-02-22 08:18:47 -080013069 }
James Smart52d52442011-05-24 11:42:45 -040013070 }
13071 rc = offset;
James Smart1feb8202018-02-22 08:18:47 -080013072 } else
Dick Kennedy372c1872020-06-30 14:50:00 -070013073 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart1feb8202018-02-22 08:18:47 -080013074 "3029 Skipped Firmware update, Current "
13075 "Version:%s New Version:%s\n",
13076 fwrev, image->revision);
James Smartce396282012-09-29 11:30:56 -040013077
13078release_out:
James Smart52d52442011-05-24 11:42:45 -040013079 list_for_each_entry_safe(dmabuf, next, &dma_buffer_list, list) {
13080 list_del(&dmabuf->list);
13081 dma_free_coherent(&phba->pcidev->dev, SLI4_PAGE_SIZE,
13082 dmabuf->virt, dmabuf->phys);
13083 kfree(dmabuf);
13084 }
James Smartce396282012-09-29 11:30:56 -040013085 release_firmware(fw);
13086out:
James Smart0a5ce732019-10-18 14:18:18 -070013087 if (rc < 0)
Dick Kennedy372c1872020-06-30 14:50:00 -070013088 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart0a5ce732019-10-18 14:18:18 -070013089 "3062 Firmware update error, status %d.\n", rc);
13090 else
Dick Kennedy372c1872020-06-30 14:50:00 -070013091 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart0a5ce732019-10-18 14:18:18 -070013092 "3024 Firmware update success: size %d.\n", rc);
James Smart52d52442011-05-24 11:42:45 -040013093}
13094
13095/**
James Smartc71ab862012-10-31 14:44:33 -040013096 * lpfc_sli4_request_firmware_update - Request linux generic firmware upgrade
13097 * @phba: pointer to lpfc hba data structure.
Lee Jonesfe614ac2020-07-23 13:24:22 +010013098 * @fw_upgrade: which firmware to update.
James Smartc71ab862012-10-31 14:44:33 -040013099 *
13100 * This routine is called to perform Linux generic firmware upgrade on device
13101 * that supports such feature.
13102 **/
13103int
13104lpfc_sli4_request_firmware_update(struct lpfc_hba *phba, uint8_t fw_upgrade)
13105{
13106 uint8_t file_name[ELX_MODEL_NAME_SIZE];
13107 int ret;
13108 const struct firmware *fw;
13109
13110 /* Only supported on SLI4 interface type 2 for now */
James Smart27d6ac02018-02-22 08:18:42 -080013111 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) <
James Smartc71ab862012-10-31 14:44:33 -040013112 LPFC_SLI_INTF_IF_TYPE_2)
13113 return -EPERM;
13114
13115 snprintf(file_name, ELX_MODEL_NAME_SIZE, "%s.grp", phba->ModelName);
13116
13117 if (fw_upgrade == INT_FW_UPGRADE) {
13118 ret = request_firmware_nowait(THIS_MODULE, FW_ACTION_HOTPLUG,
13119 file_name, &phba->pcidev->dev,
13120 GFP_KERNEL, (void *)phba,
13121 lpfc_write_firmware);
13122 } else if (fw_upgrade == RUN_FW_UPGRADE) {
13123 ret = request_firmware(&fw, file_name, &phba->pcidev->dev);
13124 if (!ret)
13125 lpfc_write_firmware(fw, (void *)phba);
13126 } else {
13127 ret = -EINVAL;
13128 }
13129
13130 return ret;
13131}
13132
13133/**
James Smartda0436e2009-05-22 14:51:39 -040013134 * lpfc_pci_probe_one_s4 - PCI probe func to reg SLI-4 device to PCI subsys
13135 * @pdev: pointer to PCI device
13136 * @pid: pointer to PCI device identifier
13137 *
13138 * This routine is called from the kernel's PCI subsystem to device with
13139 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
13140 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
13141 * information of the device and driver to see if the driver state that it
13142 * can support this kind of device. If the match is successful, the driver
13143 * core invokes this routine. If this routine determines it can claim the HBA,
13144 * it does all the initialization that it needs to do to handle the HBA
13145 * properly.
13146 *
13147 * Return code
13148 * 0 - driver can claim the device
13149 * negative value - driver can not claim the device
13150 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080013151static int
James Smartda0436e2009-05-22 14:51:39 -040013152lpfc_pci_probe_one_s4(struct pci_dev *pdev, const struct pci_device_id *pid)
13153{
13154 struct lpfc_hba *phba;
13155 struct lpfc_vport *vport = NULL;
James Smart6669f9b2009-10-02 15:16:45 -040013156 struct Scsi_Host *shost = NULL;
James Smart6c621a22017-05-15 15:20:45 -070013157 int error;
James Smartda0436e2009-05-22 14:51:39 -040013158 uint32_t cfg_mode, intr_mode;
James Smartda0436e2009-05-22 14:51:39 -040013159
13160 /* Allocate memory for HBA structure */
13161 phba = lpfc_hba_alloc(pdev);
13162 if (!phba)
13163 return -ENOMEM;
13164
13165 /* Perform generic PCI device enabling operation */
13166 error = lpfc_enable_pci_dev(phba);
James Smart079b5c92011-08-21 21:48:49 -040013167 if (error)
James Smartda0436e2009-05-22 14:51:39 -040013168 goto out_free_phba;
James Smartda0436e2009-05-22 14:51:39 -040013169
13170 /* Set up SLI API function jump table for PCI-device group-1 HBAs */
13171 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_OC);
13172 if (error)
13173 goto out_disable_pci_dev;
13174
13175 /* Set up SLI-4 specific device PCI memory space */
13176 error = lpfc_sli4_pci_mem_setup(phba);
13177 if (error) {
13178 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13179 "1410 Failed to set up pci memory space.\n");
13180 goto out_disable_pci_dev;
13181 }
13182
James Smartda0436e2009-05-22 14:51:39 -040013183 /* Set up SLI-4 Specific device driver resources */
13184 error = lpfc_sli4_driver_resource_setup(phba);
13185 if (error) {
13186 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13187 "1412 Failed to set up driver resource.\n");
13188 goto out_unset_pci_mem_s4;
13189 }
13190
James Smart19ca7602010-11-20 23:11:55 -050013191 INIT_LIST_HEAD(&phba->active_rrq_list);
James Smart7d791df2011-07-22 18:37:52 -040013192 INIT_LIST_HEAD(&phba->fcf.fcf_pri_list);
James Smart19ca7602010-11-20 23:11:55 -050013193
James Smartda0436e2009-05-22 14:51:39 -040013194 /* Set up common device driver resources */
13195 error = lpfc_setup_driver_resource_phase2(phba);
13196 if (error) {
13197 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13198 "1414 Failed to set up driver resource.\n");
James Smart6c621a22017-05-15 15:20:45 -070013199 goto out_unset_driver_resource_s4;
James Smartda0436e2009-05-22 14:51:39 -040013200 }
13201
James Smart079b5c92011-08-21 21:48:49 -040013202 /* Get the default values for Model Name and Description */
13203 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
13204
James Smartda0436e2009-05-22 14:51:39 -040013205 /* Now, trying to enable interrupt and bring up the device */
13206 cfg_mode = phba->cfg_use_msi;
James Smartda0436e2009-05-22 14:51:39 -040013207
James Smart7b15db32013-01-03 15:43:29 -050013208 /* Put device to a known state before enabling interrupt */
James Smartcdb42be2019-01-28 11:14:21 -080013209 phba->pport = NULL;
James Smart7b15db32013-01-03 15:43:29 -050013210 lpfc_stop_port(phba);
James Smart895427b2017-02-12 13:52:30 -080013211
James Smartdcaa2132019-11-04 16:57:06 -080013212 /* Init cpu_map array */
13213 lpfc_cpu_map_array_init(phba);
13214
13215 /* Init hba_eq_hdl array */
13216 lpfc_hba_eq_hdl_array_init(phba);
13217
James Smart7b15db32013-01-03 15:43:29 -050013218 /* Configure and enable interrupt */
13219 intr_mode = lpfc_sli4_enable_intr(phba, cfg_mode);
13220 if (intr_mode == LPFC_INTR_ERROR) {
Dick Kennedy372c1872020-06-30 14:50:00 -070013221 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart7b15db32013-01-03 15:43:29 -050013222 "0426 Failed to enable interrupt.\n");
13223 error = -ENODEV;
James Smartcdb42be2019-01-28 11:14:21 -080013224 goto out_unset_driver_resource;
James Smartda0436e2009-05-22 14:51:39 -040013225 }
James Smart7b15db32013-01-03 15:43:29 -050013226 /* Default to single EQ for non-MSI-X */
James Smart895427b2017-02-12 13:52:30 -080013227 if (phba->intr_type != MSIX) {
James Smart6a828b02019-01-28 11:14:31 -080013228 phba->cfg_irq_chann = 1;
James Smart2d7dbc42017-02-12 13:52:35 -080013229 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
James Smart2d7dbc42017-02-12 13:52:35 -080013230 if (phba->nvmet_support)
13231 phba->cfg_nvmet_mrq = 1;
13232 }
James Smartcdb42be2019-01-28 11:14:21 -080013233 }
James Smart6a828b02019-01-28 11:14:31 -080013234 lpfc_cpu_affinity_check(phba, phba->cfg_irq_chann);
James Smartcdb42be2019-01-28 11:14:21 -080013235
13236 /* Create SCSI host to the physical port */
13237 error = lpfc_create_shost(phba);
13238 if (error) {
13239 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13240 "1415 Failed to create scsi host.\n");
13241 goto out_disable_intr;
13242 }
13243 vport = phba->pport;
13244 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
13245
13246 /* Configure sysfs attributes */
13247 error = lpfc_alloc_sysfs_attr(vport);
13248 if (error) {
13249 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13250 "1416 Failed to allocate sysfs attr\n");
13251 goto out_destroy_shost;
James Smart895427b2017-02-12 13:52:30 -080013252 }
13253
James Smart7b15db32013-01-03 15:43:29 -050013254 /* Set up SLI-4 HBA */
13255 if (lpfc_sli4_hba_setup(phba)) {
Dick Kennedy372c1872020-06-30 14:50:00 -070013256 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart7b15db32013-01-03 15:43:29 -050013257 "1421 Failed to set up hba\n");
13258 error = -ENODEV;
James Smartcdb42be2019-01-28 11:14:21 -080013259 goto out_free_sysfs_attr;
James Smart7b15db32013-01-03 15:43:29 -050013260 }
13261
13262 /* Log the current active interrupt mode */
13263 phba->intr_mode = intr_mode;
13264 lpfc_log_intr_mode(phba, intr_mode);
James Smartda0436e2009-05-22 14:51:39 -040013265
13266 /* Perform post initialization setup */
13267 lpfc_post_init_setup(phba);
13268
James Smart01649562017-02-12 13:52:32 -080013269 /* NVME support in FW earlier in the driver load corrects the
13270 * FC4 type making a check for nvme_support unnecessary.
13271 */
James Smart0794d602019-01-28 11:14:19 -080013272 if (phba->nvmet_support == 0) {
13273 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
13274 /* Create NVME binding with nvme_fc_transport. This
13275 * ensures the vport is initialized. If the localport
13276 * create fails, it should not unload the driver to
13277 * support field issues.
13278 */
13279 error = lpfc_nvme_create_localport(vport);
13280 if (error) {
Dick Kennedy372c1872020-06-30 14:50:00 -070013281 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart0794d602019-01-28 11:14:19 -080013282 "6004 NVME registration "
13283 "failed, error x%x\n",
13284 error);
13285 }
James Smart01649562017-02-12 13:52:32 -080013286 }
13287 }
James Smart895427b2017-02-12 13:52:30 -080013288
James Smartc71ab862012-10-31 14:44:33 -040013289 /* check for firmware upgrade or downgrade */
13290 if (phba->cfg_request_firmware_upgrade)
Sebastian Herbsztdb6f1c22015-08-31 16:48:14 -040013291 lpfc_sli4_request_firmware_update(phba, INT_FW_UPGRADE);
James Smart52d52442011-05-24 11:42:45 -040013292
James Smart1c6834a2009-07-19 10:01:26 -040013293 /* Check if there are static vports to be created. */
13294 lpfc_create_static_vport(phba);
James Smartd2cc9bc2018-09-10 10:30:50 -070013295
13296 /* Enable RAS FW log support */
13297 lpfc_sli4_ras_setup(phba);
13298
James Smart93a4d6f2019-11-04 16:57:05 -080013299 INIT_LIST_HEAD(&phba->poll_list);
James Smartf861f592020-03-22 11:12:54 -070013300 timer_setup(&phba->cpuhp_poll_timer, lpfc_sli4_poll_hbtimer, 0);
James Smart93a4d6f2019-11-04 16:57:05 -080013301 cpuhp_state_add_instance_nocalls(lpfc_cpuhp_state, &phba->cpuhp);
13302
James Smartda0436e2009-05-22 14:51:39 -040013303 return 0;
13304
James Smartda0436e2009-05-22 14:51:39 -040013305out_free_sysfs_attr:
13306 lpfc_free_sysfs_attr(vport);
13307out_destroy_shost:
13308 lpfc_destroy_shost(phba);
James Smartcdb42be2019-01-28 11:14:21 -080013309out_disable_intr:
13310 lpfc_sli4_disable_intr(phba);
James Smartda0436e2009-05-22 14:51:39 -040013311out_unset_driver_resource:
13312 lpfc_unset_driver_resource_phase2(phba);
James Smartda0436e2009-05-22 14:51:39 -040013313out_unset_driver_resource_s4:
13314 lpfc_sli4_driver_resource_unset(phba);
13315out_unset_pci_mem_s4:
13316 lpfc_sli4_pci_mem_unset(phba);
13317out_disable_pci_dev:
13318 lpfc_disable_pci_dev(phba);
James Smart6669f9b2009-10-02 15:16:45 -040013319 if (shost)
13320 scsi_host_put(shost);
James Smartda0436e2009-05-22 14:51:39 -040013321out_free_phba:
13322 lpfc_hba_free(phba);
13323 return error;
13324}
13325
13326/**
13327 * lpfc_pci_remove_one_s4 - PCI func to unreg SLI-4 device from PCI subsystem
13328 * @pdev: pointer to PCI device
13329 *
13330 * This routine is called from the kernel's PCI subsystem to device with
13331 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
13332 * removed from PCI bus, it performs all the necessary cleanup for the HBA
13333 * device to be removed from the PCI subsystem properly.
13334 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080013335static void
James Smartda0436e2009-05-22 14:51:39 -040013336lpfc_pci_remove_one_s4(struct pci_dev *pdev)
13337{
13338 struct Scsi_Host *shost = pci_get_drvdata(pdev);
13339 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
13340 struct lpfc_vport **vports;
13341 struct lpfc_hba *phba = vport->phba;
13342 int i;
13343
13344 /* Mark the device unloading flag */
13345 spin_lock_irq(&phba->hbalock);
13346 vport->load_flag |= FC_UNLOADING;
13347 spin_unlock_irq(&phba->hbalock);
13348
13349 /* Free the HBA sysfs attributes */
13350 lpfc_free_sysfs_attr(vport);
13351
13352 /* Release all the vports against this physical port */
13353 vports = lpfc_create_vport_work_array(phba);
13354 if (vports != NULL)
James Smart587a37f2012-05-09 21:16:03 -040013355 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
13356 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
13357 continue;
James Smartda0436e2009-05-22 14:51:39 -040013358 fc_vport_terminate(vports[i]->fc_vport);
James Smart587a37f2012-05-09 21:16:03 -040013359 }
James Smartda0436e2009-05-22 14:51:39 -040013360 lpfc_destroy_vport_work_array(phba, vports);
13361
13362 /* Remove FC host and then SCSI host with the physical port */
13363 fc_remove_host(shost);
13364 scsi_remove_host(shost);
James Smartda0436e2009-05-22 14:51:39 -040013365
James Smartda0436e2009-05-22 14:51:39 -040013366 /* Perform ndlp cleanup on the physical port. The nvme and nvmet
13367 * localports are destroyed after to cleanup all transport memory.
13368 */
13369 lpfc_cleanup(vport);
13370 lpfc_nvmet_destroy_targetport(phba);
13371 lpfc_nvme_destroy_localport(vport);
13372
James Smartc4908502019-01-28 11:14:28 -080013373 /* De-allocate multi-XRI pools */
13374 if (phba->cfg_xri_rebalancing)
13375 lpfc_destroy_multixri_pools(phba);
13376
James Smart281d6192018-01-30 15:58:47 -080013377 /*
13378 * Bring down the SLI Layer. This step disables all interrupts,
13379 * clears the rings, discards all mailbox commands, and resets
13380 * the HBA FCoE function.
13381 */
13382 lpfc_debugfs_terminate(vport);
James Smartda0436e2009-05-22 14:51:39 -040013383
Dick Kennedy19017622017-09-29 17:34:27 -070013384 lpfc_stop_hba_timers(phba);
James Smart523128e2018-09-10 10:30:46 -070013385 spin_lock_irq(&phba->port_list_lock);
James Smartda0436e2009-05-22 14:51:39 -040013386 list_del_init(&vport->listentry);
James Smart523128e2018-09-10 10:30:46 -070013387 spin_unlock_irq(&phba->port_list_lock);
James Smartda0436e2009-05-22 14:51:39 -040013388
James Smart3677a3a2010-09-29 11:19:14 -040013389 /* Perform scsi free before driver resource_unset since scsi
James Smartda0436e2009-05-22 14:51:39 -040013390 * buffers are released to their corresponding pools here.
13391 */
James Smart5e5b5112019-01-28 11:14:22 -080013392 lpfc_io_free(phba);
James Smart01649562017-02-12 13:52:32 -080013393 lpfc_free_iocb_list(phba);
James Smart5e5b5112019-01-28 11:14:22 -080013394 lpfc_sli4_hba_unset(phba);
James Smart67d12732012-08-03 12:36:13 -040013395
James Smart0cdb84e2018-04-09 14:24:26 -070013396 lpfc_unset_driver_resource_phase2(phba);
James Smartda0436e2009-05-22 14:51:39 -040013397 lpfc_sli4_driver_resource_unset(phba);
13398
13399 /* Unmap adapter Control and Doorbell registers */
13400 lpfc_sli4_pci_mem_unset(phba);
13401
13402 /* Release PCI resources and disable device's PCI function */
13403 scsi_host_put(shost);
13404 lpfc_disable_pci_dev(phba);
13405
13406 /* Finally, free the driver's device data structure */
13407 lpfc_hba_free(phba);
13408
13409 return;
13410}
13411
13412/**
13413 * lpfc_pci_suspend_one_s4 - PCI func to suspend SLI-4 device for power mgmnt
13414 * @pdev: pointer to PCI device
13415 * @msg: power management message
13416 *
13417 * This routine is called from the kernel's PCI subsystem to support system
13418 * Power Management (PM) to device with SLI-4 interface spec. When PM invokes
13419 * this method, it quiesces the device by stopping the driver's worker
13420 * thread for the device, turning off device's interrupt and DMA, and bring
13421 * the device offline. Note that as the driver implements the minimum PM
13422 * requirements to a power-aware driver's PM support for suspend/resume -- all
13423 * the possible PM messages (SUSPEND, HIBERNATE, FREEZE) to the suspend()
13424 * method call will be treated as SUSPEND and the driver will fully
13425 * reinitialize its device during resume() method call, the driver will set
13426 * device to PCI_D3hot state in PCI config space instead of setting it
13427 * according to the @msg provided by the PM.
13428 *
13429 * Return code
13430 * 0 - driver suspended the device
13431 * Error otherwise
13432 **/
13433static int
13434lpfc_pci_suspend_one_s4(struct pci_dev *pdev, pm_message_t msg)
13435{
13436 struct Scsi_Host *shost = pci_get_drvdata(pdev);
13437 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
13438
13439 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart75baf692010-06-08 18:31:21 -040013440 "2843 PCI device Power Management suspend.\n");
James Smartda0436e2009-05-22 14:51:39 -040013441
13442 /* Bring down the device */
James Smart618a5232012-06-12 13:54:36 -040013443 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smartda0436e2009-05-22 14:51:39 -040013444 lpfc_offline(phba);
13445 kthread_stop(phba->worker_thread);
13446
13447 /* Disable interrupt from device */
13448 lpfc_sli4_disable_intr(phba);
James Smart5350d872011-10-10 21:33:49 -040013449 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -040013450
13451 /* Save device state to PCI config space */
13452 pci_save_state(pdev);
13453 pci_set_power_state(pdev, PCI_D3hot);
13454
13455 return 0;
13456}
13457
13458/**
13459 * lpfc_pci_resume_one_s4 - PCI func to resume SLI-4 device for power mgmnt
13460 * @pdev: pointer to PCI device
13461 *
13462 * This routine is called from the kernel's PCI subsystem to support system
13463 * Power Management (PM) to device with SLI-4 interface spac. When PM invokes
13464 * this method, it restores the device's PCI config space state and fully
13465 * reinitializes the device and brings it online. Note that as the driver
13466 * implements the minimum PM requirements to a power-aware driver's PM for
13467 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
13468 * to the suspend() method call will be treated as SUSPEND and the driver
13469 * will fully reinitialize its device during resume() method call, the device
13470 * will be set to PCI_D0 directly in PCI config space before restoring the
13471 * state.
13472 *
13473 * Return code
13474 * 0 - driver suspended the device
13475 * Error otherwise
13476 **/
13477static int
13478lpfc_pci_resume_one_s4(struct pci_dev *pdev)
13479{
13480 struct Scsi_Host *shost = pci_get_drvdata(pdev);
13481 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
13482 uint32_t intr_mode;
13483 int error;
13484
13485 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
13486 "0292 PCI device Power Management resume.\n");
13487
13488 /* Restore device state from PCI config space */
13489 pci_set_power_state(pdev, PCI_D0);
13490 pci_restore_state(pdev);
James Smart1dfb5a42010-02-12 14:40:50 -050013491
13492 /*
13493 * As the new kernel behavior of pci_restore_state() API call clears
13494 * device saved_state flag, need to save the restored state again.
13495 */
13496 pci_save_state(pdev);
13497
James Smartda0436e2009-05-22 14:51:39 -040013498 if (pdev->is_busmaster)
13499 pci_set_master(pdev);
13500
13501 /* Startup the kernel thread for this host adapter. */
13502 phba->worker_thread = kthread_run(lpfc_do_work, phba,
13503 "lpfc_worker_%d", phba->brd_no);
13504 if (IS_ERR(phba->worker_thread)) {
13505 error = PTR_ERR(phba->worker_thread);
13506 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13507 "0293 PM resume failed to start worker "
13508 "thread: error=x%x.\n", error);
13509 return error;
13510 }
13511
13512 /* Configure and enable interrupt */
13513 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
13514 if (intr_mode == LPFC_INTR_ERROR) {
Dick Kennedy372c1872020-06-30 14:50:00 -070013515 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartda0436e2009-05-22 14:51:39 -040013516 "0294 PM resume Failed to enable interrupt\n");
13517 return -EIO;
13518 } else
13519 phba->intr_mode = intr_mode;
13520
13521 /* Restart HBA and bring it online */
13522 lpfc_sli_brdrestart(phba);
13523 lpfc_online(phba);
13524
13525 /* Log the current active interrupt mode */
13526 lpfc_log_intr_mode(phba, phba->intr_mode);
13527
13528 return 0;
13529}
13530
13531/**
James Smart75baf692010-06-08 18:31:21 -040013532 * lpfc_sli4_prep_dev_for_recover - Prepare SLI4 device for pci slot recover
13533 * @phba: pointer to lpfc hba data structure.
13534 *
13535 * This routine is called to prepare the SLI4 device for PCI slot recover. It
13536 * aborts all the outstanding SCSI I/Os to the pci device.
13537 **/
13538static void
13539lpfc_sli4_prep_dev_for_recover(struct lpfc_hba *phba)
13540{
Dick Kennedy372c1872020-06-30 14:50:00 -070013541 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart75baf692010-06-08 18:31:21 -040013542 "2828 PCI channel I/O abort preparing for recovery\n");
13543 /*
13544 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
13545 * and let the SCSI mid-layer to retry them to recover.
13546 */
James Smartdb55fba2014-04-04 13:52:02 -040013547 lpfc_sli_abort_fcp_rings(phba);
James Smart75baf692010-06-08 18:31:21 -040013548}
13549
13550/**
13551 * lpfc_sli4_prep_dev_for_reset - Prepare SLI4 device for pci slot reset
13552 * @phba: pointer to lpfc hba data structure.
13553 *
13554 * This routine is called to prepare the SLI4 device for PCI slot reset. It
13555 * disables the device interrupt and pci device, and aborts the internal FCP
13556 * pending I/Os.
13557 **/
13558static void
13559lpfc_sli4_prep_dev_for_reset(struct lpfc_hba *phba)
13560{
Dick Kennedy372c1872020-06-30 14:50:00 -070013561 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart75baf692010-06-08 18:31:21 -040013562 "2826 PCI channel disable preparing for reset\n");
13563
13564 /* Block any management I/Os to the device */
James Smart618a5232012-06-12 13:54:36 -040013565 lpfc_block_mgmt_io(phba, LPFC_MBX_NO_WAIT);
James Smart75baf692010-06-08 18:31:21 -040013566
13567 /* Block all SCSI devices' I/Os on the host */
13568 lpfc_scsi_dev_block(phba);
13569
James Smartc00f62e2019-08-14 16:57:11 -070013570 /* Flush all driver's outstanding I/Os as we are to reset */
13571 lpfc_sli_flush_io_rings(phba);
James Smartc3725bd2017-11-20 16:00:42 -080013572
James Smart75baf692010-06-08 18:31:21 -040013573 /* stop all timers */
13574 lpfc_stop_hba_timers(phba);
13575
13576 /* Disable interrupt and pci device */
13577 lpfc_sli4_disable_intr(phba);
James Smart5350d872011-10-10 21:33:49 -040013578 lpfc_sli4_queue_destroy(phba);
James Smart75baf692010-06-08 18:31:21 -040013579 pci_disable_device(phba->pcidev);
James Smart75baf692010-06-08 18:31:21 -040013580}
13581
13582/**
13583 * lpfc_sli4_prep_dev_for_perm_failure - Prepare SLI4 dev for pci slot disable
13584 * @phba: pointer to lpfc hba data structure.
13585 *
13586 * This routine is called to prepare the SLI4 device for PCI slot permanently
13587 * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
13588 * pending I/Os.
13589 **/
13590static void
13591lpfc_sli4_prep_dev_for_perm_failure(struct lpfc_hba *phba)
13592{
Dick Kennedy372c1872020-06-30 14:50:00 -070013593 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart75baf692010-06-08 18:31:21 -040013594 "2827 PCI channel permanent disable for failure\n");
13595
13596 /* Block all SCSI devices' I/Os on the host */
13597 lpfc_scsi_dev_block(phba);
13598
13599 /* stop all timers */
13600 lpfc_stop_hba_timers(phba);
13601
James Smartc00f62e2019-08-14 16:57:11 -070013602 /* Clean up all driver's outstanding I/Os */
13603 lpfc_sli_flush_io_rings(phba);
James Smart75baf692010-06-08 18:31:21 -040013604}
13605
13606/**
James Smartda0436e2009-05-22 14:51:39 -040013607 * lpfc_io_error_detected_s4 - Method for handling PCI I/O error to SLI-4 device
13608 * @pdev: pointer to PCI device.
13609 * @state: the current PCI connection state.
13610 *
13611 * This routine is called from the PCI subsystem for error handling to device
13612 * with SLI-4 interface spec. This function is called by the PCI subsystem
13613 * after a PCI bus error affecting this device has been detected. When this
13614 * function is invoked, it will need to stop all the I/Os and interrupt(s)
13615 * to the device. Once that is done, it will return PCI_ERS_RESULT_NEED_RESET
13616 * for the PCI subsystem to perform proper recovery as desired.
13617 *
13618 * Return codes
13619 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
13620 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
13621 **/
13622static pci_ers_result_t
13623lpfc_io_error_detected_s4(struct pci_dev *pdev, pci_channel_state_t state)
13624{
James Smart75baf692010-06-08 18:31:21 -040013625 struct Scsi_Host *shost = pci_get_drvdata(pdev);
13626 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
13627
13628 switch (state) {
13629 case pci_channel_io_normal:
13630 /* Non-fatal error, prepare for recovery */
13631 lpfc_sli4_prep_dev_for_recover(phba);
13632 return PCI_ERS_RESULT_CAN_RECOVER;
13633 case pci_channel_io_frozen:
13634 /* Fatal error, prepare for slot reset */
13635 lpfc_sli4_prep_dev_for_reset(phba);
13636 return PCI_ERS_RESULT_NEED_RESET;
13637 case pci_channel_io_perm_failure:
13638 /* Permanent failure, prepare for device down */
13639 lpfc_sli4_prep_dev_for_perm_failure(phba);
13640 return PCI_ERS_RESULT_DISCONNECT;
13641 default:
13642 /* Unknown state, prepare and request slot reset */
Dick Kennedy372c1872020-06-30 14:50:00 -070013643 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart75baf692010-06-08 18:31:21 -040013644 "2825 Unknown PCI error state: x%x\n", state);
13645 lpfc_sli4_prep_dev_for_reset(phba);
13646 return PCI_ERS_RESULT_NEED_RESET;
13647 }
James Smartda0436e2009-05-22 14:51:39 -040013648}
13649
13650/**
13651 * lpfc_io_slot_reset_s4 - Method for restart PCI SLI-4 device from scratch
13652 * @pdev: pointer to PCI device.
13653 *
13654 * This routine is called from the PCI subsystem for error handling to device
13655 * with SLI-4 interface spec. It is called after PCI bus has been reset to
13656 * restart the PCI card from scratch, as if from a cold-boot. During the
13657 * PCI subsystem error recovery, after the driver returns
13658 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
13659 * recovery and then call this routine before calling the .resume method to
13660 * recover the device. This function will initialize the HBA device, enable
13661 * the interrupt, but it will just put the HBA to offline state without
13662 * passing any I/O traffic.
13663 *
13664 * Return codes
13665 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
13666 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
13667 */
13668static pci_ers_result_t
13669lpfc_io_slot_reset_s4(struct pci_dev *pdev)
13670{
James Smart75baf692010-06-08 18:31:21 -040013671 struct Scsi_Host *shost = pci_get_drvdata(pdev);
13672 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
13673 struct lpfc_sli *psli = &phba->sli;
13674 uint32_t intr_mode;
13675
13676 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
13677 if (pci_enable_device_mem(pdev)) {
13678 printk(KERN_ERR "lpfc: Cannot re-enable "
13679 "PCI device after reset.\n");
13680 return PCI_ERS_RESULT_DISCONNECT;
13681 }
13682
13683 pci_restore_state(pdev);
James Smart0a96e972011-07-22 18:37:28 -040013684
13685 /*
13686 * As the new kernel behavior of pci_restore_state() API call clears
13687 * device saved_state flag, need to save the restored state again.
13688 */
13689 pci_save_state(pdev);
13690
James Smart75baf692010-06-08 18:31:21 -040013691 if (pdev->is_busmaster)
13692 pci_set_master(pdev);
13693
13694 spin_lock_irq(&phba->hbalock);
13695 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
13696 spin_unlock_irq(&phba->hbalock);
13697
13698 /* Configure and enable interrupt */
13699 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
13700 if (intr_mode == LPFC_INTR_ERROR) {
Dick Kennedy372c1872020-06-30 14:50:00 -070013701 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart75baf692010-06-08 18:31:21 -040013702 "2824 Cannot re-enable interrupt after "
13703 "slot reset.\n");
13704 return PCI_ERS_RESULT_DISCONNECT;
13705 } else
13706 phba->intr_mode = intr_mode;
13707
13708 /* Log the current active interrupt mode */
13709 lpfc_log_intr_mode(phba, phba->intr_mode);
13710
James Smartda0436e2009-05-22 14:51:39 -040013711 return PCI_ERS_RESULT_RECOVERED;
13712}
13713
13714/**
13715 * lpfc_io_resume_s4 - Method for resuming PCI I/O operation to SLI-4 device
13716 * @pdev: pointer to PCI device
13717 *
13718 * This routine is called from the PCI subsystem for error handling to device
13719 * with SLI-4 interface spec. It is called when kernel error recovery tells
13720 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
13721 * error recovery. After this call, traffic can start to flow from this device
13722 * again.
13723 **/
13724static void
13725lpfc_io_resume_s4(struct pci_dev *pdev)
13726{
James Smart75baf692010-06-08 18:31:21 -040013727 struct Scsi_Host *shost = pci_get_drvdata(pdev);
13728 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
13729
13730 /*
13731 * In case of slot reset, as function reset is performed through
13732 * mailbox command which needs DMA to be enabled, this operation
13733 * has to be moved to the io resume phase. Taking device offline
13734 * will perform the necessary cleanup.
13735 */
13736 if (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE)) {
13737 /* Perform device reset */
James Smart618a5232012-06-12 13:54:36 -040013738 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart75baf692010-06-08 18:31:21 -040013739 lpfc_offline(phba);
13740 lpfc_sli_brdrestart(phba);
13741 /* Bring the device back online */
13742 lpfc_online(phba);
13743 }
James Smartda0436e2009-05-22 14:51:39 -040013744}
13745
13746/**
James Smart3772a992009-05-22 14:50:54 -040013747 * lpfc_pci_probe_one - lpfc PCI probe func to reg dev to PCI subsystem
13748 * @pdev: pointer to PCI device
13749 * @pid: pointer to PCI device identifier
13750 *
13751 * This routine is to be registered to the kernel's PCI subsystem. When an
13752 * Emulex HBA device is presented on PCI bus, the kernel PCI subsystem looks
13753 * at PCI device-specific information of the device and driver to see if the
13754 * driver state that it can support this kind of device. If the match is
13755 * successful, the driver core invokes this routine. This routine dispatches
13756 * the action to the proper SLI-3 or SLI-4 device probing routine, which will
13757 * do all the initialization that it needs to do to handle the HBA device
13758 * properly.
13759 *
13760 * Return code
13761 * 0 - driver can claim the device
13762 * negative value - driver can not claim the device
13763 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080013764static int
James Smart3772a992009-05-22 14:50:54 -040013765lpfc_pci_probe_one(struct pci_dev *pdev, const struct pci_device_id *pid)
13766{
13767 int rc;
James Smart8fa38512009-07-19 10:01:03 -040013768 struct lpfc_sli_intf intf;
James Smart3772a992009-05-22 14:50:54 -040013769
James Smart28baac72010-02-12 14:42:03 -050013770 if (pci_read_config_dword(pdev, LPFC_SLI_INTF, &intf.word0))
James Smart3772a992009-05-22 14:50:54 -040013771 return -ENODEV;
13772
James Smart8fa38512009-07-19 10:01:03 -040013773 if ((bf_get(lpfc_sli_intf_valid, &intf) == LPFC_SLI_INTF_VALID) &&
James Smart28baac72010-02-12 14:42:03 -050013774 (bf_get(lpfc_sli_intf_slirev, &intf) == LPFC_SLI_INTF_REV_SLI4))
James Smartda0436e2009-05-22 14:51:39 -040013775 rc = lpfc_pci_probe_one_s4(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -040013776 else
James Smart3772a992009-05-22 14:50:54 -040013777 rc = lpfc_pci_probe_one_s3(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -040013778
James Smart3772a992009-05-22 14:50:54 -040013779 return rc;
13780}
13781
13782/**
13783 * lpfc_pci_remove_one - lpfc PCI func to unreg dev from PCI subsystem
13784 * @pdev: pointer to PCI device
13785 *
13786 * This routine is to be registered to the kernel's PCI subsystem. When an
13787 * Emulex HBA is removed from PCI bus, the driver core invokes this routine.
13788 * This routine dispatches the action to the proper SLI-3 or SLI-4 device
13789 * remove routine, which will perform all the necessary cleanup for the
13790 * device to be removed from the PCI subsystem properly.
13791 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080013792static void
James Smart3772a992009-05-22 14:50:54 -040013793lpfc_pci_remove_one(struct pci_dev *pdev)
13794{
13795 struct Scsi_Host *shost = pci_get_drvdata(pdev);
13796 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
13797
13798 switch (phba->pci_dev_grp) {
13799 case LPFC_PCI_DEV_LP:
13800 lpfc_pci_remove_one_s3(pdev);
13801 break;
James Smartda0436e2009-05-22 14:51:39 -040013802 case LPFC_PCI_DEV_OC:
13803 lpfc_pci_remove_one_s4(pdev);
13804 break;
James Smart3772a992009-05-22 14:50:54 -040013805 default:
Dick Kennedy372c1872020-06-30 14:50:00 -070013806 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart3772a992009-05-22 14:50:54 -040013807 "1424 Invalid PCI device group: 0x%x\n",
13808 phba->pci_dev_grp);
13809 break;
13810 }
13811 return;
13812}
13813
13814/**
13815 * lpfc_pci_suspend_one - lpfc PCI func to suspend dev for power management
13816 * @pdev: pointer to PCI device
13817 * @msg: power management message
13818 *
13819 * This routine is to be registered to the kernel's PCI subsystem to support
13820 * system Power Management (PM). When PM invokes this method, it dispatches
13821 * the action to the proper SLI-3 or SLI-4 device suspend routine, which will
13822 * suspend the device.
13823 *
13824 * Return code
13825 * 0 - driver suspended the device
13826 * Error otherwise
13827 **/
13828static int
13829lpfc_pci_suspend_one(struct pci_dev *pdev, pm_message_t msg)
13830{
13831 struct Scsi_Host *shost = pci_get_drvdata(pdev);
13832 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
13833 int rc = -ENODEV;
13834
13835 switch (phba->pci_dev_grp) {
13836 case LPFC_PCI_DEV_LP:
13837 rc = lpfc_pci_suspend_one_s3(pdev, msg);
13838 break;
James Smartda0436e2009-05-22 14:51:39 -040013839 case LPFC_PCI_DEV_OC:
13840 rc = lpfc_pci_suspend_one_s4(pdev, msg);
13841 break;
James Smart3772a992009-05-22 14:50:54 -040013842 default:
Dick Kennedy372c1872020-06-30 14:50:00 -070013843 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart3772a992009-05-22 14:50:54 -040013844 "1425 Invalid PCI device group: 0x%x\n",
13845 phba->pci_dev_grp);
13846 break;
13847 }
13848 return rc;
13849}
13850
13851/**
13852 * lpfc_pci_resume_one - lpfc PCI func to resume dev for power management
13853 * @pdev: pointer to PCI device
13854 *
13855 * This routine is to be registered to the kernel's PCI subsystem to support
13856 * system Power Management (PM). When PM invokes this method, it dispatches
13857 * the action to the proper SLI-3 or SLI-4 device resume routine, which will
13858 * resume the device.
13859 *
13860 * Return code
13861 * 0 - driver suspended the device
13862 * Error otherwise
13863 **/
13864static int
13865lpfc_pci_resume_one(struct pci_dev *pdev)
13866{
13867 struct Scsi_Host *shost = pci_get_drvdata(pdev);
13868 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
13869 int rc = -ENODEV;
13870
13871 switch (phba->pci_dev_grp) {
13872 case LPFC_PCI_DEV_LP:
13873 rc = lpfc_pci_resume_one_s3(pdev);
13874 break;
James Smartda0436e2009-05-22 14:51:39 -040013875 case LPFC_PCI_DEV_OC:
13876 rc = lpfc_pci_resume_one_s4(pdev);
13877 break;
James Smart3772a992009-05-22 14:50:54 -040013878 default:
Dick Kennedy372c1872020-06-30 14:50:00 -070013879 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart3772a992009-05-22 14:50:54 -040013880 "1426 Invalid PCI device group: 0x%x\n",
13881 phba->pci_dev_grp);
13882 break;
13883 }
13884 return rc;
13885}
13886
13887/**
13888 * lpfc_io_error_detected - lpfc method for handling PCI I/O error
13889 * @pdev: pointer to PCI device.
13890 * @state: the current PCI connection state.
13891 *
13892 * This routine is registered to the PCI subsystem for error handling. This
13893 * function is called by the PCI subsystem after a PCI bus error affecting
13894 * this device has been detected. When this routine is invoked, it dispatches
13895 * the action to the proper SLI-3 or SLI-4 device error detected handling
13896 * routine, which will perform the proper error detected operation.
13897 *
13898 * Return codes
13899 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
13900 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
13901 **/
13902static pci_ers_result_t
13903lpfc_io_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
13904{
13905 struct Scsi_Host *shost = pci_get_drvdata(pdev);
13906 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
13907 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
13908
13909 switch (phba->pci_dev_grp) {
13910 case LPFC_PCI_DEV_LP:
13911 rc = lpfc_io_error_detected_s3(pdev, state);
13912 break;
James Smartda0436e2009-05-22 14:51:39 -040013913 case LPFC_PCI_DEV_OC:
13914 rc = lpfc_io_error_detected_s4(pdev, state);
13915 break;
James Smart3772a992009-05-22 14:50:54 -040013916 default:
Dick Kennedy372c1872020-06-30 14:50:00 -070013917 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart3772a992009-05-22 14:50:54 -040013918 "1427 Invalid PCI device group: 0x%x\n",
13919 phba->pci_dev_grp);
13920 break;
13921 }
13922 return rc;
13923}
13924
13925/**
13926 * lpfc_io_slot_reset - lpfc method for restart PCI dev from scratch
13927 * @pdev: pointer to PCI device.
13928 *
13929 * This routine is registered to the PCI subsystem for error handling. This
13930 * function is called after PCI bus has been reset to restart the PCI card
13931 * from scratch, as if from a cold-boot. When this routine is invoked, it
13932 * dispatches the action to the proper SLI-3 or SLI-4 device reset handling
13933 * routine, which will perform the proper device reset.
13934 *
13935 * Return codes
13936 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
13937 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
13938 **/
13939static pci_ers_result_t
13940lpfc_io_slot_reset(struct pci_dev *pdev)
13941{
13942 struct Scsi_Host *shost = pci_get_drvdata(pdev);
13943 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
13944 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
13945
13946 switch (phba->pci_dev_grp) {
13947 case LPFC_PCI_DEV_LP:
13948 rc = lpfc_io_slot_reset_s3(pdev);
13949 break;
James Smartda0436e2009-05-22 14:51:39 -040013950 case LPFC_PCI_DEV_OC:
13951 rc = lpfc_io_slot_reset_s4(pdev);
13952 break;
James Smart3772a992009-05-22 14:50:54 -040013953 default:
Dick Kennedy372c1872020-06-30 14:50:00 -070013954 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart3772a992009-05-22 14:50:54 -040013955 "1428 Invalid PCI device group: 0x%x\n",
13956 phba->pci_dev_grp);
13957 break;
13958 }
13959 return rc;
13960}
13961
13962/**
13963 * lpfc_io_resume - lpfc method for resuming PCI I/O operation
13964 * @pdev: pointer to PCI device
13965 *
13966 * This routine is registered to the PCI subsystem for error handling. It
13967 * is called when kernel error recovery tells the lpfc driver that it is
13968 * OK to resume normal PCI operation after PCI bus error recovery. When
13969 * this routine is invoked, it dispatches the action to the proper SLI-3
13970 * or SLI-4 device io_resume routine, which will resume the device operation.
13971 **/
13972static void
13973lpfc_io_resume(struct pci_dev *pdev)
13974{
13975 struct Scsi_Host *shost = pci_get_drvdata(pdev);
13976 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
13977
13978 switch (phba->pci_dev_grp) {
13979 case LPFC_PCI_DEV_LP:
13980 lpfc_io_resume_s3(pdev);
13981 break;
James Smartda0436e2009-05-22 14:51:39 -040013982 case LPFC_PCI_DEV_OC:
13983 lpfc_io_resume_s4(pdev);
13984 break;
James Smart3772a992009-05-22 14:50:54 -040013985 default:
Dick Kennedy372c1872020-06-30 14:50:00 -070013986 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart3772a992009-05-22 14:50:54 -040013987 "1429 Invalid PCI device group: 0x%x\n",
13988 phba->pci_dev_grp);
13989 break;
13990 }
13991 return;
13992}
13993
James Smart1ba981f2014-02-20 09:56:45 -050013994/**
13995 * lpfc_sli4_oas_verify - Verify OAS is supported by this adapter
13996 * @phba: pointer to lpfc hba data structure.
13997 *
13998 * This routine checks to see if OAS is supported for this adapter. If
13999 * supported, the configure Flash Optimized Fabric flag is set. Otherwise,
14000 * the enable oas flag is cleared and the pool created for OAS device data
14001 * is destroyed.
14002 *
14003 **/
YueHaibingc7092972019-04-17 23:05:54 +080014004static void
James Smart1ba981f2014-02-20 09:56:45 -050014005lpfc_sli4_oas_verify(struct lpfc_hba *phba)
14006{
14007
14008 if (!phba->cfg_EnableXLane)
14009 return;
14010
14011 if (phba->sli4_hba.pc_sli4_params.oas_supported) {
14012 phba->cfg_fof = 1;
14013 } else {
James Smartf38fa0b2014-04-04 13:52:21 -040014014 phba->cfg_fof = 0;
Saurav Girepunjec3e5aac2019-10-27 01:17:17 +053014015 mempool_destroy(phba->device_data_mem_pool);
James Smart1ba981f2014-02-20 09:56:45 -050014016 phba->device_data_mem_pool = NULL;
14017 }
14018
14019 return;
14020}
14021
14022/**
James Smartd2cc9bc2018-09-10 10:30:50 -070014023 * lpfc_sli4_ras_init - Verify RAS-FW log is supported by this adapter
14024 * @phba: pointer to lpfc hba data structure.
14025 *
14026 * This routine checks to see if RAS is supported by the adapter. Check the
14027 * function through which RAS support enablement is to be done.
14028 **/
14029void
14030lpfc_sli4_ras_init(struct lpfc_hba *phba)
14031{
14032 switch (phba->pcidev->device) {
14033 case PCI_DEVICE_ID_LANCER_G6_FC:
14034 case PCI_DEVICE_ID_LANCER_G7_FC:
14035 phba->ras_fwlog.ras_hwsupport = true;
James Smartcb349902018-11-29 16:09:27 -080014036 if (phba->cfg_ras_fwlog_func == PCI_FUNC(phba->pcidev->devfn) &&
14037 phba->cfg_ras_fwlog_buffsize)
James Smartd2cc9bc2018-09-10 10:30:50 -070014038 phba->ras_fwlog.ras_enabled = true;
14039 else
14040 phba->ras_fwlog.ras_enabled = false;
14041 break;
14042 default:
14043 phba->ras_fwlog.ras_hwsupport = false;
14044 }
14045}
14046
James Smart1ba981f2014-02-20 09:56:45 -050014047
dea31012005-04-17 16:05:31 -050014048MODULE_DEVICE_TABLE(pci, lpfc_id_table);
14049
Stephen Hemmingera55b2d22012-09-07 09:33:16 -070014050static const struct pci_error_handlers lpfc_err_handler = {
Linas Vepstas8d63f372007-02-14 14:28:36 -060014051 .error_detected = lpfc_io_error_detected,
14052 .slot_reset = lpfc_io_slot_reset,
14053 .resume = lpfc_io_resume,
14054};
14055
dea31012005-04-17 16:05:31 -050014056static struct pci_driver lpfc_driver = {
14057 .name = LPFC_DRIVER_NAME,
14058 .id_table = lpfc_id_table,
14059 .probe = lpfc_pci_probe_one,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080014060 .remove = lpfc_pci_remove_one,
Anton Blanchard85e8a232017-02-13 08:49:20 +110014061 .shutdown = lpfc_pci_remove_one,
James Smart3a55b532008-12-04 22:38:54 -050014062 .suspend = lpfc_pci_suspend_one,
James Smart3772a992009-05-22 14:50:54 -040014063 .resume = lpfc_pci_resume_one,
James Smart2e0fef82007-06-17 19:56:36 -050014064 .err_handler = &lpfc_err_handler,
dea31012005-04-17 16:05:31 -050014065};
14066
James Smart3ef6d242012-01-18 16:23:48 -050014067static const struct file_operations lpfc_mgmt_fop = {
Al Viro858feac2013-04-14 22:39:37 -040014068 .owner = THIS_MODULE,
James Smart3ef6d242012-01-18 16:23:48 -050014069};
14070
14071static struct miscdevice lpfc_mgmt_dev = {
14072 .minor = MISC_DYNAMIC_MINOR,
14073 .name = "lpfcmgmt",
14074 .fops = &lpfc_mgmt_fop,
14075};
14076
James Smarte59058c2008-08-24 21:49:00 -040014077/**
James Smart3621a712009-04-06 18:47:14 -040014078 * lpfc_init - lpfc module initialization routine
James Smarte59058c2008-08-24 21:49:00 -040014079 *
14080 * This routine is to be invoked when the lpfc module is loaded into the
14081 * kernel. The special kernel macro module_init() is used to indicate the
14082 * role of this routine to the kernel as lpfc module entry point.
14083 *
14084 * Return codes
14085 * 0 - successful
14086 * -ENOMEM - FC attach transport failed
14087 * all others - failed
14088 */
dea31012005-04-17 16:05:31 -050014089static int __init
14090lpfc_init(void)
14091{
14092 int error = 0;
14093
Anton Blanchardbc2736e2020-07-13 18:39:08 +100014094 pr_info(LPFC_MODULE_DESC "\n");
14095 pr_info(LPFC_COPYRIGHT "\n");
dea31012005-04-17 16:05:31 -050014096
James Smart3ef6d242012-01-18 16:23:48 -050014097 error = misc_register(&lpfc_mgmt_dev);
14098 if (error)
14099 printk(KERN_ERR "Could not register lpfcmgmt device, "
14100 "misc_register returned with status %d", error);
14101
Jing Xiangfeng1eaff532020-07-31 14:56:39 +080014102 error = -ENOMEM;
James Smart458c0832016-07-06 12:36:07 -070014103 lpfc_transport_functions.vport_create = lpfc_vport_create;
14104 lpfc_transport_functions.vport_delete = lpfc_vport_delete;
dea31012005-04-17 16:05:31 -050014105 lpfc_transport_template =
14106 fc_attach_transport(&lpfc_transport_functions);
James Smart7ee5d432007-10-27 13:37:17 -040014107 if (lpfc_transport_template == NULL)
Jing Xiangfeng1eaff532020-07-31 14:56:39 +080014108 goto unregister;
James Smart458c0832016-07-06 12:36:07 -070014109 lpfc_vport_transport_template =
14110 fc_attach_transport(&lpfc_vport_transport_functions);
14111 if (lpfc_vport_transport_template == NULL) {
14112 fc_release_transport(lpfc_transport_template);
Jing Xiangfeng1eaff532020-07-31 14:56:39 +080014113 goto unregister;
James Smart7ee5d432007-10-27 13:37:17 -040014114 }
James Smart5fd11082018-03-05 12:04:04 -080014115 lpfc_nvme_cmd_template();
James Smartbd3061b2018-03-05 12:04:05 -080014116 lpfc_nvmet_cmd_template();
James Smart7bb03bb2013-04-17 20:19:16 -040014117
14118 /* Initialize in case vector mapping is needed */
James Smart2ea259e2017-02-12 13:52:27 -080014119 lpfc_present_cpu = num_present_cpus();
James Smart7bb03bb2013-04-17 20:19:16 -040014120
James Smart93a4d6f2019-11-04 16:57:05 -080014121 error = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN,
14122 "lpfc/sli4:online",
14123 lpfc_cpu_online, lpfc_cpu_offline);
14124 if (error < 0)
14125 goto cpuhp_failure;
14126 lpfc_cpuhp_state = error;
14127
dea31012005-04-17 16:05:31 -050014128 error = pci_register_driver(&lpfc_driver);
James Smart93a4d6f2019-11-04 16:57:05 -080014129 if (error)
14130 goto unwind;
14131
14132 return error;
14133
14134unwind:
14135 cpuhp_remove_multi_state(lpfc_cpuhp_state);
14136cpuhp_failure:
14137 fc_release_transport(lpfc_transport_template);
14138 fc_release_transport(lpfc_vport_transport_template);
Jing Xiangfeng1eaff532020-07-31 14:56:39 +080014139unregister:
14140 misc_deregister(&lpfc_mgmt_dev);
dea31012005-04-17 16:05:31 -050014141
14142 return error;
14143}
14144
Dick Kennedy372c1872020-06-30 14:50:00 -070014145void lpfc_dmp_dbg(struct lpfc_hba *phba)
14146{
14147 unsigned int start_idx;
14148 unsigned int dbg_cnt;
14149 unsigned int temp_idx;
14150 int i;
14151 int j = 0;
14152 unsigned long rem_nsec;
14153
14154 if (phba->cfg_log_verbose)
14155 return;
14156
14157 if (atomic_cmpxchg(&phba->dbg_log_dmping, 0, 1) != 0)
14158 return;
14159
14160 start_idx = (unsigned int)atomic_read(&phba->dbg_log_idx) % DBG_LOG_SZ;
14161 dbg_cnt = (unsigned int)atomic_read(&phba->dbg_log_cnt);
14162 temp_idx = start_idx;
14163 if (dbg_cnt >= DBG_LOG_SZ) {
14164 dbg_cnt = DBG_LOG_SZ;
14165 temp_idx -= 1;
14166 } else {
14167 if ((start_idx + dbg_cnt) > (DBG_LOG_SZ - 1)) {
14168 temp_idx = (start_idx + dbg_cnt) % DBG_LOG_SZ;
14169 } else {
Dick Kennedy77dd7d72020-07-06 13:42:46 -070014170 if (start_idx < dbg_cnt)
Dick Kennedy372c1872020-06-30 14:50:00 -070014171 start_idx = DBG_LOG_SZ - (dbg_cnt - start_idx);
Dick Kennedy77dd7d72020-07-06 13:42:46 -070014172 else
Dick Kennedy372c1872020-06-30 14:50:00 -070014173 start_idx -= dbg_cnt;
Dick Kennedy372c1872020-06-30 14:50:00 -070014174 }
14175 }
14176 dev_info(&phba->pcidev->dev, "start %d end %d cnt %d\n",
14177 start_idx, temp_idx, dbg_cnt);
14178
14179 for (i = 0; i < dbg_cnt; i++) {
14180 if ((start_idx + i) < DBG_LOG_SZ)
Dick Kennedy77dd7d72020-07-06 13:42:46 -070014181 temp_idx = (start_idx + i) % DBG_LOG_SZ;
Dick Kennedy372c1872020-06-30 14:50:00 -070014182 else
14183 temp_idx = j++;
14184 rem_nsec = do_div(phba->dbg_log[temp_idx].t_ns, NSEC_PER_SEC);
14185 dev_info(&phba->pcidev->dev, "%d: [%5lu.%06lu] %s",
14186 temp_idx,
14187 (unsigned long)phba->dbg_log[temp_idx].t_ns,
14188 rem_nsec / 1000,
14189 phba->dbg_log[temp_idx].log);
14190 }
14191 atomic_set(&phba->dbg_log_cnt, 0);
14192 atomic_set(&phba->dbg_log_dmping, 0);
14193}
14194
Lee Jones7fa03c72020-07-23 13:24:21 +010014195__printf(2, 3)
Dick Kennedy372c1872020-06-30 14:50:00 -070014196void lpfc_dbg_print(struct lpfc_hba *phba, const char *fmt, ...)
14197{
14198 unsigned int idx;
14199 va_list args;
14200 int dbg_dmping = atomic_read(&phba->dbg_log_dmping);
14201 struct va_format vaf;
14202
14203
14204 va_start(args, fmt);
14205 if (unlikely(dbg_dmping)) {
14206 vaf.fmt = fmt;
14207 vaf.va = &args;
14208 dev_info(&phba->pcidev->dev, "%pV", &vaf);
14209 va_end(args);
14210 return;
14211 }
14212 idx = (unsigned int)atomic_fetch_add(1, &phba->dbg_log_idx) %
14213 DBG_LOG_SZ;
14214
14215 atomic_inc(&phba->dbg_log_cnt);
14216
14217 vscnprintf(phba->dbg_log[idx].log,
14218 sizeof(phba->dbg_log[idx].log), fmt, args);
14219 va_end(args);
14220
14221 phba->dbg_log[idx].t_ns = local_clock();
14222}
14223
James Smarte59058c2008-08-24 21:49:00 -040014224/**
James Smart3621a712009-04-06 18:47:14 -040014225 * lpfc_exit - lpfc module removal routine
James Smarte59058c2008-08-24 21:49:00 -040014226 *
14227 * This routine is invoked when the lpfc module is removed from the kernel.
14228 * The special kernel macro module_exit() is used to indicate the role of
14229 * this routine to the kernel as lpfc module exit point.
14230 */
dea31012005-04-17 16:05:31 -050014231static void __exit
14232lpfc_exit(void)
14233{
James Smart3ef6d242012-01-18 16:23:48 -050014234 misc_deregister(&lpfc_mgmt_dev);
dea31012005-04-17 16:05:31 -050014235 pci_unregister_driver(&lpfc_driver);
James Smart93a4d6f2019-11-04 16:57:05 -080014236 cpuhp_remove_multi_state(lpfc_cpuhp_state);
dea31012005-04-17 16:05:31 -050014237 fc_release_transport(lpfc_transport_template);
James Smart458c0832016-07-06 12:36:07 -070014238 fc_release_transport(lpfc_vport_transport_template);
Johannes Thumshirn79739672015-08-31 16:48:11 -040014239 idr_destroy(&lpfc_hba_index);
dea31012005-04-17 16:05:31 -050014240}
14241
14242module_init(lpfc_init);
14243module_exit(lpfc_exit);
14244MODULE_LICENSE("GPL");
14245MODULE_DESCRIPTION(LPFC_MODULE_DESC);
James Smartd080abe2017-02-12 13:52:39 -080014246MODULE_AUTHOR("Broadcom");
dea31012005-04-17 16:05:31 -050014247MODULE_VERSION("0:" LPFC_DRIVER_VERSION);