blob: 28e6a763f1065fa98a8f6ce78172f269f390c38f [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 Smart0d041212019-01-28 11:14:41 -08004 * Copyright (C) 2017-2019 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
53#include <linux/nvme-fc-driver.h>
dea31012005-04-17 16:05:31 -050054
James Smartda0436e2009-05-22 14:51:39 -040055#include "lpfc_hw4.h"
dea31012005-04-17 16:05:31 -050056#include "lpfc_hw.h"
57#include "lpfc_sli.h"
James Smartda0436e2009-05-22 14:51:39 -040058#include "lpfc_sli4.h"
James Smartea2151b2008-09-07 11:52:10 -040059#include "lpfc_nl.h"
dea31012005-04-17 16:05:31 -050060#include "lpfc_disc.h"
dea31012005-04-17 16:05:31 -050061#include "lpfc.h"
James Smart895427b2017-02-12 13:52:30 -080062#include "lpfc_scsi.h"
63#include "lpfc_nvme.h"
James Smart86c67372017-04-21 16:05:04 -070064#include "lpfc_nvmet.h"
dea31012005-04-17 16:05:31 -050065#include "lpfc_logmsg.h"
66#include "lpfc_crtn.h"
James Smart92d7f7b2007-06-17 19:56:38 -050067#include "lpfc_vport.h"
dea31012005-04-17 16:05:31 -050068#include "lpfc_version.h"
James Smart12f44452016-07-06 12:36:03 -070069#include "lpfc_ids.h"
dea31012005-04-17 16:05:31 -050070
James Smart93a4d6f2019-11-04 16:57:05 -080071static enum cpuhp_state lpfc_cpuhp_state;
James Smart7bb03bb2013-04-17 20:19:16 -040072/* Used when mapping IRQ vectors in a driver centric manner */
YueHaibingd7b761b2019-05-31 23:28:41 +080073static uint32_t lpfc_present_cpu;
James Smart7bb03bb2013-04-17 20:19:16 -040074
James Smart93a4d6f2019-11-04 16:57:05 -080075static void __lpfc_cpuhp_remove(struct lpfc_hba *phba);
76static void lpfc_cpuhp_remove(struct lpfc_hba *phba);
77static void lpfc_cpuhp_add(struct lpfc_hba *phba);
dea31012005-04-17 16:05:31 -050078static void lpfc_get_hba_model_desc(struct lpfc_hba *, uint8_t *, uint8_t *);
79static int lpfc_post_rcv_buf(struct lpfc_hba *);
James Smart5350d872011-10-10 21:33:49 -040080static int lpfc_sli4_queue_verify(struct lpfc_hba *);
James Smartda0436e2009-05-22 14:51:39 -040081static int lpfc_create_bootstrap_mbox(struct lpfc_hba *);
82static int lpfc_setup_endian_order(struct lpfc_hba *);
James Smartda0436e2009-05-22 14:51:39 -040083static void lpfc_destroy_bootstrap_mbox(struct lpfc_hba *);
James Smart8a9d2e82012-05-09 21:16:12 -040084static void lpfc_free_els_sgl_list(struct lpfc_hba *);
James Smartf358dd02017-02-12 13:52:34 -080085static void lpfc_free_nvmet_sgl_list(struct lpfc_hba *);
James Smart8a9d2e82012-05-09 21:16:12 -040086static void lpfc_init_sgl_list(struct lpfc_hba *);
James Smartda0436e2009-05-22 14:51:39 -040087static int lpfc_init_active_sgl_array(struct lpfc_hba *);
88static void lpfc_free_active_sgl(struct lpfc_hba *);
89static int lpfc_hba_down_post_s3(struct lpfc_hba *phba);
90static int lpfc_hba_down_post_s4(struct lpfc_hba *phba);
91static int lpfc_sli4_cq_event_pool_create(struct lpfc_hba *);
92static void lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *);
93static void lpfc_sli4_cq_event_release_all(struct lpfc_hba *);
James Smart618a5232012-06-12 13:54:36 -040094static void lpfc_sli4_disable_intr(struct lpfc_hba *);
95static uint32_t lpfc_sli4_enable_intr(struct lpfc_hba *, uint32_t);
James Smart1ba981f2014-02-20 09:56:45 -050096static void lpfc_sli4_oas_verify(struct lpfc_hba *phba);
James Smart6a828b02019-01-28 11:14:31 -080097static uint16_t lpfc_find_cpu_handle(struct lpfc_hba *, uint16_t, int);
James Smartaa6ff302019-05-21 17:49:09 -070098static void lpfc_setup_bg(struct lpfc_hba *, struct Scsi_Host *);
dea31012005-04-17 16:05:31 -050099
100static struct scsi_transport_template *lpfc_transport_template = NULL;
James Smart92d7f7b2007-06-17 19:56:38 -0500101static struct scsi_transport_template *lpfc_vport_transport_template = NULL;
dea31012005-04-17 16:05:31 -0500102static DEFINE_IDR(lpfc_hba_index);
James Smartf358dd02017-02-12 13:52:34 -0800103#define LPFC_NVMET_BUF_POST 254
dea31012005-04-17 16:05:31 -0500104
James Smarte59058c2008-08-24 21:49:00 -0400105/**
James Smart3621a712009-04-06 18:47:14 -0400106 * lpfc_config_port_prep - Perform lpfc initialization prior to config port
James Smarte59058c2008-08-24 21:49:00 -0400107 * @phba: pointer to lpfc hba data structure.
108 *
109 * This routine will do LPFC initialization prior to issuing the CONFIG_PORT
110 * mailbox command. It retrieves the revision information from the HBA and
111 * collects the Vital Product Data (VPD) about the HBA for preparing the
112 * configuration of the HBA.
113 *
114 * Return codes:
115 * 0 - success.
116 * -ERESTART - requests the SLI layer to reset the HBA and try again.
117 * Any other value - indicates an error.
118 **/
dea31012005-04-17 16:05:31 -0500119int
James Smart2e0fef82007-06-17 19:56:36 -0500120lpfc_config_port_prep(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500121{
122 lpfc_vpd_t *vp = &phba->vpd;
123 int i = 0, rc;
124 LPFC_MBOXQ_t *pmb;
125 MAILBOX_t *mb;
126 char *lpfc_vpd_data = NULL;
127 uint16_t offset = 0;
128 static char licensed[56] =
129 "key unlock for use with gnu public licensed code only\0";
James Smart65a29c12006-07-06 15:50:50 -0400130 static int init_key = 1;
dea31012005-04-17 16:05:31 -0500131
132 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
133 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -0500134 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500135 return -ENOMEM;
136 }
137
James Smart04c68492009-05-22 14:52:52 -0400138 mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -0500139 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -0500140
141 if (lpfc_is_LC_HBA(phba->pcidev->device)) {
James Smart65a29c12006-07-06 15:50:50 -0400142 if (init_key) {
143 uint32_t *ptext = (uint32_t *) licensed;
dea31012005-04-17 16:05:31 -0500144
James Smart65a29c12006-07-06 15:50:50 -0400145 for (i = 0; i < 56; i += sizeof (uint32_t), ptext++)
146 *ptext = cpu_to_be32(*ptext);
147 init_key = 0;
148 }
dea31012005-04-17 16:05:31 -0500149
150 lpfc_read_nv(phba, pmb);
151 memset((char*)mb->un.varRDnvp.rsvd3, 0,
152 sizeof (mb->un.varRDnvp.rsvd3));
153 memcpy((char*)mb->un.varRDnvp.rsvd3, licensed,
154 sizeof (licensed));
155
156 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
157
158 if (rc != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500159 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
James Smarte8b62012007-08-02 11:10:09 -0400160 "0324 Config Port initialization "
dea31012005-04-17 16:05:31 -0500161 "error, mbxCmd x%x READ_NVPARM, "
162 "mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500163 mb->mbxCommand, mb->mbxStatus);
164 mempool_free(pmb, phba->mbox_mem_pool);
165 return -ERESTART;
166 }
167 memcpy(phba->wwnn, (char *)mb->un.varRDnvp.nodename,
James Smart2e0fef82007-06-17 19:56:36 -0500168 sizeof(phba->wwnn));
169 memcpy(phba->wwpn, (char *)mb->un.varRDnvp.portname,
170 sizeof(phba->wwpn));
dea31012005-04-17 16:05:31 -0500171 }
172
Martin Wilckdfb75132018-11-12 09:58:37 +0100173 /*
174 * Clear all option bits except LPFC_SLI3_BG_ENABLED,
175 * which was already set in lpfc_get_cfgparam()
176 */
177 phba->sli3_options &= (uint32_t)LPFC_SLI3_BG_ENABLED;
James Smart92d7f7b2007-06-17 19:56:38 -0500178
dea31012005-04-17 16:05:31 -0500179 /* Setup and issue mailbox READ REV command */
180 lpfc_read_rev(phba, pmb);
181 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
182 if (rc != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500183 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400184 "0439 Adapter failed to init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500185 "READ_REV, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500186 mb->mbxCommand, mb->mbxStatus);
187 mempool_free( pmb, phba->mbox_mem_pool);
188 return -ERESTART;
189 }
190
James Smart92d7f7b2007-06-17 19:56:38 -0500191
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500192 /*
193 * The value of rr must be 1 since the driver set the cv field to 1.
194 * This setting requires the FW to set all revision fields.
dea31012005-04-17 16:05:31 -0500195 */
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500196 if (mb->un.varRdRev.rr == 0) {
dea31012005-04-17 16:05:31 -0500197 vp->rev.rBit = 0;
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500198 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400199 "0440 Adapter failed to init, READ_REV has "
200 "missing revision information.\n");
dea31012005-04-17 16:05:31 -0500201 mempool_free(pmb, phba->mbox_mem_pool);
202 return -ERESTART;
dea31012005-04-17 16:05:31 -0500203 }
204
James Smart495a7142008-06-14 22:52:59 -0400205 if (phba->sli_rev == 3 && !mb->un.varRdRev.v3rsp) {
206 mempool_free(pmb, phba->mbox_mem_pool);
James Smarted957682007-06-17 19:56:37 -0500207 return -EINVAL;
James Smart495a7142008-06-14 22:52:59 -0400208 }
James Smarted957682007-06-17 19:56:37 -0500209
dea31012005-04-17 16:05:31 -0500210 /* Save information as VPD data */
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500211 vp->rev.rBit = 1;
James Smart92d7f7b2007-06-17 19:56:38 -0500212 memcpy(&vp->sli3Feat, &mb->un.varRdRev.sli3Feat, sizeof(uint32_t));
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500213 vp->rev.sli1FwRev = mb->un.varRdRev.sli1FwRev;
214 memcpy(vp->rev.sli1FwName, (char*) mb->un.varRdRev.sli1FwName, 16);
215 vp->rev.sli2FwRev = mb->un.varRdRev.sli2FwRev;
216 memcpy(vp->rev.sli2FwName, (char *) mb->un.varRdRev.sli2FwName, 16);
dea31012005-04-17 16:05:31 -0500217 vp->rev.biuRev = mb->un.varRdRev.biuRev;
218 vp->rev.smRev = mb->un.varRdRev.smRev;
219 vp->rev.smFwRev = mb->un.varRdRev.un.smFwRev;
220 vp->rev.endecRev = mb->un.varRdRev.endecRev;
221 vp->rev.fcphHigh = mb->un.varRdRev.fcphHigh;
222 vp->rev.fcphLow = mb->un.varRdRev.fcphLow;
223 vp->rev.feaLevelHigh = mb->un.varRdRev.feaLevelHigh;
224 vp->rev.feaLevelLow = mb->un.varRdRev.feaLevelLow;
225 vp->rev.postKernRev = mb->un.varRdRev.postKernRev;
226 vp->rev.opFwRev = mb->un.varRdRev.opFwRev;
227
James Smart92d7f7b2007-06-17 19:56:38 -0500228 /* If the sli feature level is less then 9, we must
229 * tear down all RPIs and VPIs on link down if NPIV
230 * is enabled.
231 */
232 if (vp->rev.feaLevelHigh < 9)
233 phba->sli3_options |= LPFC_SLI3_VPORT_TEARDOWN;
234
dea31012005-04-17 16:05:31 -0500235 if (lpfc_is_LC_HBA(phba->pcidev->device))
236 memcpy(phba->RandomData, (char *)&mb->un.varWords[24],
237 sizeof (phba->RandomData));
238
dea31012005-04-17 16:05:31 -0500239 /* Get adapter VPD information */
dea31012005-04-17 16:05:31 -0500240 lpfc_vpd_data = kmalloc(DMP_VPD_SIZE, GFP_KERNEL);
241 if (!lpfc_vpd_data)
James Smartd7c255b2008-08-24 21:50:00 -0400242 goto out_free_mbox;
dea31012005-04-17 16:05:31 -0500243 do {
James Smarta0c87cb2009-07-19 10:01:10 -0400244 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_VPD);
dea31012005-04-17 16:05:31 -0500245 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
246
247 if (rc != MBX_SUCCESS) {
248 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400249 "0441 VPD not present on adapter, "
dea31012005-04-17 16:05:31 -0500250 "mbxCmd x%x DUMP VPD, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500251 mb->mbxCommand, mb->mbxStatus);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500252 mb->un.varDmp.word_cnt = 0;
dea31012005-04-17 16:05:31 -0500253 }
James Smart04c68492009-05-22 14:52:52 -0400254 /* dump mem may return a zero when finished or we got a
255 * mailbox error, either way we are done.
256 */
257 if (mb->un.varDmp.word_cnt == 0)
258 break;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500259 if (mb->un.varDmp.word_cnt > DMP_VPD_SIZE - offset)
260 mb->un.varDmp.word_cnt = DMP_VPD_SIZE - offset;
James Smartd7c255b2008-08-24 21:50:00 -0400261 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
262 lpfc_vpd_data + offset,
James Smart92d7f7b2007-06-17 19:56:38 -0500263 mb->un.varDmp.word_cnt);
dea31012005-04-17 16:05:31 -0500264 offset += mb->un.varDmp.word_cnt;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500265 } while (mb->un.varDmp.word_cnt && offset < DMP_VPD_SIZE);
266 lpfc_parse_vpd(phba, lpfc_vpd_data, offset);
dea31012005-04-17 16:05:31 -0500267
268 kfree(lpfc_vpd_data);
dea31012005-04-17 16:05:31 -0500269out_free_mbox:
270 mempool_free(pmb, phba->mbox_mem_pool);
271 return 0;
272}
273
James Smarte59058c2008-08-24 21:49:00 -0400274/**
James Smart3621a712009-04-06 18:47:14 -0400275 * lpfc_config_async_cmpl - Completion handler for config async event mbox cmd
James Smarte59058c2008-08-24 21:49:00 -0400276 * @phba: pointer to lpfc hba data structure.
277 * @pmboxq: pointer to the driver internal queue element for mailbox command.
278 *
279 * This is the completion handler for driver's configuring asynchronous event
280 * mailbox command to the device. If the mailbox command returns successfully,
281 * it will set internal async event support flag to 1; otherwise, it will
282 * set internal async event support flag to 0.
283 **/
James Smart57127f12007-10-27 13:37:05 -0400284static void
285lpfc_config_async_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
286{
James Smart04c68492009-05-22 14:52:52 -0400287 if (pmboxq->u.mb.mbxStatus == MBX_SUCCESS)
James Smart57127f12007-10-27 13:37:05 -0400288 phba->temp_sensor_support = 1;
289 else
290 phba->temp_sensor_support = 0;
291 mempool_free(pmboxq, phba->mbox_mem_pool);
292 return;
293}
294
James Smarte59058c2008-08-24 21:49:00 -0400295/**
James Smart3621a712009-04-06 18:47:14 -0400296 * lpfc_dump_wakeup_param_cmpl - dump memory mailbox command completion handler
James Smart97207482008-12-04 22:39:19 -0500297 * @phba: pointer to lpfc hba data structure.
298 * @pmboxq: pointer to the driver internal queue element for mailbox command.
299 *
300 * This is the completion handler for dump mailbox command for getting
301 * wake up parameters. When this command complete, the response contain
302 * Option rom version of the HBA. This function translate the version number
303 * into a human readable string and store it in OptionROMVersion.
304 **/
305static void
306lpfc_dump_wakeup_param_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
307{
308 struct prog_id *prg;
309 uint32_t prog_id_word;
310 char dist = ' ';
311 /* character array used for decoding dist type. */
312 char dist_char[] = "nabx";
313
James Smart04c68492009-05-22 14:52:52 -0400314 if (pmboxq->u.mb.mbxStatus != MBX_SUCCESS) {
James Smart9f1e1b52008-12-04 22:39:40 -0500315 mempool_free(pmboxq, phba->mbox_mem_pool);
James Smart97207482008-12-04 22:39:19 -0500316 return;
James Smart9f1e1b52008-12-04 22:39:40 -0500317 }
James Smart97207482008-12-04 22:39:19 -0500318
319 prg = (struct prog_id *) &prog_id_word;
320
321 /* word 7 contain option rom version */
James Smart04c68492009-05-22 14:52:52 -0400322 prog_id_word = pmboxq->u.mb.un.varWords[7];
James Smart97207482008-12-04 22:39:19 -0500323
324 /* Decode the Option rom version word to a readable string */
325 if (prg->dist < 4)
326 dist = dist_char[prg->dist];
327
328 if ((prg->dist == 3) && (prg->num == 0))
James Smarta2fc4aef2014-09-03 12:57:55 -0400329 snprintf(phba->OptionROMVersion, 32, "%d.%d%d",
James Smart97207482008-12-04 22:39:19 -0500330 prg->ver, prg->rev, prg->lev);
331 else
James Smarta2fc4aef2014-09-03 12:57:55 -0400332 snprintf(phba->OptionROMVersion, 32, "%d.%d%d%c%d",
James Smart97207482008-12-04 22:39:19 -0500333 prg->ver, prg->rev, prg->lev,
334 dist, prg->num);
James Smart9f1e1b52008-12-04 22:39:40 -0500335 mempool_free(pmboxq, phba->mbox_mem_pool);
James Smart97207482008-12-04 22:39:19 -0500336 return;
337}
338
339/**
James Smart05580562011-05-24 11:40:48 -0400340 * lpfc_update_vport_wwn - Updates the fc_nodename, fc_portname,
341 * cfg_soft_wwnn, cfg_soft_wwpn
342 * @vport: pointer to lpfc vport data structure.
343 *
344 *
345 * Return codes
346 * None.
347 **/
348void
349lpfc_update_vport_wwn(struct lpfc_vport *vport)
350{
James Smartaeb3c812017-04-21 16:05:02 -0700351 uint8_t vvvl = vport->fc_sparam.cmn.valid_vendor_ver_level;
352 u32 *fawwpn_key = (u32 *)&vport->fc_sparam.un.vendorVersion[0];
353
James Smart05580562011-05-24 11:40:48 -0400354 /* If the soft name exists then update it using the service params */
355 if (vport->phba->cfg_soft_wwnn)
356 u64_to_wwn(vport->phba->cfg_soft_wwnn,
357 vport->fc_sparam.nodeName.u.wwn);
358 if (vport->phba->cfg_soft_wwpn)
359 u64_to_wwn(vport->phba->cfg_soft_wwpn,
360 vport->fc_sparam.portName.u.wwn);
361
362 /*
363 * If the name is empty or there exists a soft name
364 * then copy the service params name, otherwise use the fc name
365 */
366 if (vport->fc_nodename.u.wwn[0] == 0 || vport->phba->cfg_soft_wwnn)
367 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
368 sizeof(struct lpfc_name));
369 else
370 memcpy(&vport->fc_sparam.nodeName, &vport->fc_nodename,
371 sizeof(struct lpfc_name));
372
James Smartaeb3c812017-04-21 16:05:02 -0700373 /*
374 * If the port name has changed, then set the Param changes flag
375 * to unreg the login
376 */
377 if (vport->fc_portname.u.wwn[0] != 0 &&
378 memcmp(&vport->fc_portname, &vport->fc_sparam.portName,
379 sizeof(struct lpfc_name)))
380 vport->vport_flag |= FAWWPN_PARAM_CHG;
381
382 if (vport->fc_portname.u.wwn[0] == 0 ||
383 vport->phba->cfg_soft_wwpn ||
384 (vvvl == 1 && cpu_to_be32(*fawwpn_key) == FAPWWN_KEY_VENDOR) ||
385 vport->vport_flag & FAWWPN_SET) {
James Smart05580562011-05-24 11:40:48 -0400386 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
387 sizeof(struct lpfc_name));
James Smartaeb3c812017-04-21 16:05:02 -0700388 vport->vport_flag &= ~FAWWPN_SET;
389 if (vvvl == 1 && cpu_to_be32(*fawwpn_key) == FAPWWN_KEY_VENDOR)
390 vport->vport_flag |= FAWWPN_SET;
391 }
James Smart05580562011-05-24 11:40:48 -0400392 else
393 memcpy(&vport->fc_sparam.portName, &vport->fc_portname,
394 sizeof(struct lpfc_name));
395}
396
397/**
James Smart3621a712009-04-06 18:47:14 -0400398 * lpfc_config_port_post - Perform lpfc initialization after config port
James Smarte59058c2008-08-24 21:49:00 -0400399 * @phba: pointer to lpfc hba data structure.
400 *
401 * This routine will do LPFC initialization after the CONFIG_PORT mailbox
402 * command call. It performs all internal resource and state setups on the
403 * port: post IOCB buffers, enable appropriate host interrupt attentions,
404 * ELS ring timers, etc.
405 *
406 * Return codes
407 * 0 - success.
408 * Any other value - error.
409 **/
dea31012005-04-17 16:05:31 -0500410int
James Smart2e0fef82007-06-17 19:56:36 -0500411lpfc_config_port_post(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500412{
James Smart2e0fef82007-06-17 19:56:36 -0500413 struct lpfc_vport *vport = phba->pport;
James Smarta257bf92009-04-06 18:48:10 -0400414 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -0500415 LPFC_MBOXQ_t *pmb;
416 MAILBOX_t *mb;
417 struct lpfc_dmabuf *mp;
418 struct lpfc_sli *psli = &phba->sli;
419 uint32_t status, timeout;
James Smart2e0fef82007-06-17 19:56:36 -0500420 int i, j;
421 int rc;
dea31012005-04-17 16:05:31 -0500422
James Smart7af67052007-10-27 13:38:11 -0400423 spin_lock_irq(&phba->hbalock);
424 /*
425 * If the Config port completed correctly the HBA is not
426 * over heated any more.
427 */
428 if (phba->over_temp_state == HBA_OVER_TEMP)
429 phba->over_temp_state = HBA_NORMAL_TEMP;
430 spin_unlock_irq(&phba->hbalock);
431
dea31012005-04-17 16:05:31 -0500432 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
433 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -0500434 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500435 return -ENOMEM;
436 }
James Smart04c68492009-05-22 14:52:52 -0400437 mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -0500438
dea31012005-04-17 16:05:31 -0500439 /* Get login parameters for NID. */
James Smart9f1177a2010-02-26 14:12:57 -0500440 rc = lpfc_read_sparam(phba, pmb, 0);
441 if (rc) {
442 mempool_free(pmb, phba->mbox_mem_pool);
443 return -ENOMEM;
444 }
445
James Smarted957682007-06-17 19:56:37 -0500446 pmb->vport = vport;
dea31012005-04-17 16:05:31 -0500447 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500448 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400449 "0448 Adapter failed init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500450 "READ_SPARM mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500451 mb->mbxCommand, mb->mbxStatus);
James Smart2e0fef82007-06-17 19:56:36 -0500452 phba->link_state = LPFC_HBA_ERROR;
James Smart3e1f0712018-11-29 16:09:29 -0800453 mp = (struct lpfc_dmabuf *)pmb->ctx_buf;
James Smart9f1177a2010-02-26 14:12:57 -0500454 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500455 lpfc_mbuf_free(phba, mp->virt, mp->phys);
456 kfree(mp);
457 return -EIO;
458 }
459
James Smart3e1f0712018-11-29 16:09:29 -0800460 mp = (struct lpfc_dmabuf *)pmb->ctx_buf;
dea31012005-04-17 16:05:31 -0500461
James Smart2e0fef82007-06-17 19:56:36 -0500462 memcpy(&vport->fc_sparam, mp->virt, sizeof (struct serv_parm));
dea31012005-04-17 16:05:31 -0500463 lpfc_mbuf_free(phba, mp->virt, mp->phys);
464 kfree(mp);
James Smart3e1f0712018-11-29 16:09:29 -0800465 pmb->ctx_buf = NULL;
James Smart05580562011-05-24 11:40:48 -0400466 lpfc_update_vport_wwn(vport);
James Smarta257bf92009-04-06 18:48:10 -0400467
468 /* Update the fc_host data structures with new wwn. */
469 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
470 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
James Smart21e9a0a2009-05-22 14:53:21 -0400471 fc_host_max_npiv_vports(shost) = phba->max_vpi;
James Smarta257bf92009-04-06 18:48:10 -0400472
dea31012005-04-17 16:05:31 -0500473 /* If no serial number in VPD data, use low 6 bytes of WWNN */
474 /* This should be consolidated into parse_vpd ? - mr */
475 if (phba->SerialNumber[0] == 0) {
476 uint8_t *outptr;
477
James Smart2e0fef82007-06-17 19:56:36 -0500478 outptr = &vport->fc_nodename.u.s.IEEE[0];
dea31012005-04-17 16:05:31 -0500479 for (i = 0; i < 12; i++) {
480 status = *outptr++;
481 j = ((status & 0xf0) >> 4);
482 if (j <= 9)
483 phba->SerialNumber[i] =
484 (char)((uint8_t) 0x30 + (uint8_t) j);
485 else
486 phba->SerialNumber[i] =
487 (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
488 i++;
489 j = (status & 0xf);
490 if (j <= 9)
491 phba->SerialNumber[i] =
492 (char)((uint8_t) 0x30 + (uint8_t) j);
493 else
494 phba->SerialNumber[i] =
495 (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
496 }
497 }
498
dea31012005-04-17 16:05:31 -0500499 lpfc_read_config(phba, pmb);
James Smarted957682007-06-17 19:56:37 -0500500 pmb->vport = vport;
dea31012005-04-17 16:05:31 -0500501 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500502 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400503 "0453 Adapter failed to init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500504 "READ_CONFIG, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500505 mb->mbxCommand, mb->mbxStatus);
James Smart2e0fef82007-06-17 19:56:36 -0500506 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500507 mempool_free( pmb, phba->mbox_mem_pool);
508 return -EIO;
509 }
510
James Smarta0c87cb2009-07-19 10:01:10 -0400511 /* Check if the port is disabled */
512 lpfc_sli_read_link_ste(phba);
513
dea31012005-04-17 16:05:31 -0500514 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
James Smart572709e2013-07-15 18:32:43 -0400515 i = (mb->un.varRdConfig.max_xri + 1);
516 if (phba->cfg_hba_queue_depth > i) {
517 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
518 "3359 HBA queue depth changed from %d to %d\n",
519 phba->cfg_hba_queue_depth, i);
520 phba->cfg_hba_queue_depth = i;
521 }
522
523 /* Reset the DFT_LUN_Q_DEPTH to (max xri >> 3) */
524 i = (mb->un.varRdConfig.max_xri >> 3);
525 if (phba->pport->cfg_lun_queue_depth > i) {
526 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
527 "3360 LUN queue depth changed from %d to %d\n",
528 phba->pport->cfg_lun_queue_depth, i);
529 phba->pport->cfg_lun_queue_depth = i;
530 }
dea31012005-04-17 16:05:31 -0500531
532 phba->lmt = mb->un.varRdConfig.lmt;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500533
534 /* Get the default values for Model Name and Description */
535 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
536
James Smart2e0fef82007-06-17 19:56:36 -0500537 phba->link_state = LPFC_LINK_DOWN;
dea31012005-04-17 16:05:31 -0500538
James Smart0b727fe2007-10-27 13:37:25 -0400539 /* Only process IOCBs on ELS ring till hba_state is READY */
James Smart895427b2017-02-12 13:52:30 -0800540 if (psli->sli3_ring[LPFC_EXTRA_RING].sli.sli3.cmdringaddr)
541 psli->sli3_ring[LPFC_EXTRA_RING].flag |= LPFC_STOP_IOCB_EVENT;
542 if (psli->sli3_ring[LPFC_FCP_RING].sli.sli3.cmdringaddr)
543 psli->sli3_ring[LPFC_FCP_RING].flag |= LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -0500544
545 /* Post receive buffers for desired rings */
James Smarted957682007-06-17 19:56:37 -0500546 if (phba->sli_rev != 3)
547 lpfc_post_rcv_buf(phba);
dea31012005-04-17 16:05:31 -0500548
James Smart93996272008-08-24 21:50:30 -0400549 /*
550 * Configure HBA MSI-X attention conditions to messages if MSI-X mode
551 */
552 if (phba->intr_type == MSIX) {
553 rc = lpfc_config_msi(phba, pmb);
554 if (rc) {
555 mempool_free(pmb, phba->mbox_mem_pool);
556 return -EIO;
557 }
558 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
559 if (rc != MBX_SUCCESS) {
560 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
561 "0352 Config MSI mailbox command "
562 "failed, mbxCmd x%x, mbxStatus x%x\n",
James Smart04c68492009-05-22 14:52:52 -0400563 pmb->u.mb.mbxCommand,
564 pmb->u.mb.mbxStatus);
James Smart93996272008-08-24 21:50:30 -0400565 mempool_free(pmb, phba->mbox_mem_pool);
566 return -EIO;
567 }
568 }
569
James Smart04c68492009-05-22 14:52:52 -0400570 spin_lock_irq(&phba->hbalock);
James Smart93996272008-08-24 21:50:30 -0400571 /* Initialize ERATT handling flag */
572 phba->hba_flag &= ~HBA_ERATT_HANDLED;
573
dea31012005-04-17 16:05:31 -0500574 /* Enable appropriate host interrupts */
James Smart9940b972011-03-11 16:06:12 -0500575 if (lpfc_readl(phba->HCregaddr, &status)) {
576 spin_unlock_irq(&phba->hbalock);
577 return -EIO;
578 }
dea31012005-04-17 16:05:31 -0500579 status |= HC_MBINT_ENA | HC_ERINT_ENA | HC_LAINT_ENA;
580 if (psli->num_rings > 0)
581 status |= HC_R0INT_ENA;
582 if (psli->num_rings > 1)
583 status |= HC_R1INT_ENA;
584 if (psli->num_rings > 2)
585 status |= HC_R2INT_ENA;
586 if (psli->num_rings > 3)
587 status |= HC_R3INT_ENA;
588
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500589 if ((phba->cfg_poll & ENABLE_FCP_RING_POLLING) &&
590 (phba->cfg_poll & DISABLE_FCP_RING_INT))
James Smart93996272008-08-24 21:50:30 -0400591 status &= ~(HC_R0INT_ENA);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500592
dea31012005-04-17 16:05:31 -0500593 writel(status, phba->HCregaddr);
594 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -0500595 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500596
James Smart93996272008-08-24 21:50:30 -0400597 /* Set up ring-0 (ELS) timer */
598 timeout = phba->fc_ratov * 2;
James Smart256ec0d2013-04-17 20:14:58 -0400599 mod_timer(&vport->els_tmofunc,
600 jiffies + msecs_to_jiffies(1000 * timeout));
James Smart93996272008-08-24 21:50:30 -0400601 /* Set up heart beat (HB) timer */
James Smart256ec0d2013-04-17 20:14:58 -0400602 mod_timer(&phba->hb_tmofunc,
603 jiffies + msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smart858c9f62007-06-17 19:56:39 -0500604 phba->hb_outstanding = 0;
605 phba->last_completion_time = jiffies;
James Smart93996272008-08-24 21:50:30 -0400606 /* Set up error attention (ERATT) polling timer */
James Smart256ec0d2013-04-17 20:14:58 -0400607 mod_timer(&phba->eratt_poll,
James Smart65791f12016-07-06 12:35:56 -0700608 jiffies + msecs_to_jiffies(1000 * phba->eratt_poll_interval));
dea31012005-04-17 16:05:31 -0500609
James Smarta0c87cb2009-07-19 10:01:10 -0400610 if (phba->hba_flag & LINK_DISABLED) {
611 lpfc_printf_log(phba,
612 KERN_ERR, LOG_INIT,
613 "2598 Adapter Link is disabled.\n");
614 lpfc_down_link(phba, pmb);
615 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
616 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
617 if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
618 lpfc_printf_log(phba,
619 KERN_ERR, LOG_INIT,
620 "2599 Adapter failed to issue DOWN_LINK"
621 " mbox command rc 0x%x\n", rc);
622
623 mempool_free(pmb, phba->mbox_mem_pool);
624 return -EIO;
625 }
James Smarte40a02c2010-02-26 14:13:54 -0500626 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
James Smart026abb82011-12-13 13:20:45 -0500627 mempool_free(pmb, phba->mbox_mem_pool);
628 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
629 if (rc)
630 return rc;
dea31012005-04-17 16:05:31 -0500631 }
632 /* MBOX buffer will be freed in mbox compl */
James Smart57127f12007-10-27 13:37:05 -0400633 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart9f1177a2010-02-26 14:12:57 -0500634 if (!pmb) {
635 phba->link_state = LPFC_HBA_ERROR;
636 return -ENOMEM;
637 }
638
James Smart57127f12007-10-27 13:37:05 -0400639 lpfc_config_async(phba, pmb, LPFC_ELS_RING);
640 pmb->mbox_cmpl = lpfc_config_async_cmpl;
641 pmb->vport = phba->pport;
642 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -0500643
James Smart57127f12007-10-27 13:37:05 -0400644 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
645 lpfc_printf_log(phba,
646 KERN_ERR,
647 LOG_INIT,
648 "0456 Adapter failed to issue "
James Smarte4e74272009-07-19 10:01:38 -0400649 "ASYNCEVT_ENABLE mbox status x%x\n",
James Smart57127f12007-10-27 13:37:05 -0400650 rc);
651 mempool_free(pmb, phba->mbox_mem_pool);
652 }
James Smart97207482008-12-04 22:39:19 -0500653
654 /* Get Option rom version */
655 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart9f1177a2010-02-26 14:12:57 -0500656 if (!pmb) {
657 phba->link_state = LPFC_HBA_ERROR;
658 return -ENOMEM;
659 }
660
James Smart97207482008-12-04 22:39:19 -0500661 lpfc_dump_wakeup_param(phba, pmb);
662 pmb->mbox_cmpl = lpfc_dump_wakeup_param_cmpl;
663 pmb->vport = phba->pport;
664 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
665
666 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
667 lpfc_printf_log(phba, KERN_ERR, LOG_INIT, "0435 Adapter failed "
James Smarte4e74272009-07-19 10:01:38 -0400668 "to get Option ROM version status x%x\n", rc);
James Smart97207482008-12-04 22:39:19 -0500669 mempool_free(pmb, phba->mbox_mem_pool);
670 }
671
James Smartd7c255b2008-08-24 21:50:00 -0400672 return 0;
James Smartce8b3ce2006-07-06 15:50:36 -0400673}
674
James Smarte59058c2008-08-24 21:49:00 -0400675/**
James Smart84d1b002010-02-12 14:42:33 -0500676 * lpfc_hba_init_link - Initialize the FC link
677 * @phba: pointer to lpfc hba data structure.
James Smart6e7288d2010-06-07 15:23:35 -0400678 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
James Smart84d1b002010-02-12 14:42:33 -0500679 *
680 * This routine will issue the INIT_LINK mailbox command call.
681 * It is available to other drivers through the lpfc_hba data
682 * structure for use as a delayed link up mechanism with the
683 * module parameter lpfc_suppress_link_up.
684 *
685 * Return code
686 * 0 - success
687 * Any other value - error
688 **/
Rashika Kheriae399b222014-09-03 12:55:28 -0400689static int
James Smart6e7288d2010-06-07 15:23:35 -0400690lpfc_hba_init_link(struct lpfc_hba *phba, uint32_t flag)
James Smart84d1b002010-02-12 14:42:33 -0500691{
James Smart1b511972011-12-13 13:23:09 -0500692 return lpfc_hba_init_link_fc_topology(phba, phba->cfg_topology, flag);
693}
694
695/**
696 * lpfc_hba_init_link_fc_topology - Initialize FC link with desired topology
697 * @phba: pointer to lpfc hba data structure.
698 * @fc_topology: desired fc topology.
699 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
700 *
701 * This routine will issue the INIT_LINK mailbox command call.
702 * It is available to other drivers through the lpfc_hba data
703 * structure for use as a delayed link up mechanism with the
704 * module parameter lpfc_suppress_link_up.
705 *
706 * Return code
707 * 0 - success
708 * Any other value - error
709 **/
710int
711lpfc_hba_init_link_fc_topology(struct lpfc_hba *phba, uint32_t fc_topology,
712 uint32_t flag)
713{
James Smart84d1b002010-02-12 14:42:33 -0500714 struct lpfc_vport *vport = phba->pport;
715 LPFC_MBOXQ_t *pmb;
716 MAILBOX_t *mb;
717 int rc;
718
719 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
720 if (!pmb) {
721 phba->link_state = LPFC_HBA_ERROR;
722 return -ENOMEM;
723 }
724 mb = &pmb->u.mb;
725 pmb->vport = vport;
726
James Smart026abb82011-12-13 13:20:45 -0500727 if ((phba->cfg_link_speed > LPFC_USER_LINK_SPEED_MAX) ||
728 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_1G) &&
729 !(phba->lmt & LMT_1Gb)) ||
730 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_2G) &&
731 !(phba->lmt & LMT_2Gb)) ||
732 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_4G) &&
733 !(phba->lmt & LMT_4Gb)) ||
734 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_8G) &&
735 !(phba->lmt & LMT_8Gb)) ||
736 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_10G) &&
737 !(phba->lmt & LMT_10Gb)) ||
738 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_16G) &&
James Smartd38dd522015-08-31 16:48:17 -0400739 !(phba->lmt & LMT_16Gb)) ||
740 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_32G) &&
James Smartfbd8a6b2018-02-22 08:18:45 -0800741 !(phba->lmt & LMT_32Gb)) ||
742 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_64G) &&
743 !(phba->lmt & LMT_64Gb))) {
James Smart026abb82011-12-13 13:20:45 -0500744 /* Reset link speed to auto */
745 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
746 "1302 Invalid speed for this board:%d "
747 "Reset link speed to auto.\n",
748 phba->cfg_link_speed);
749 phba->cfg_link_speed = LPFC_USER_LINK_SPEED_AUTO;
750 }
James Smart1b511972011-12-13 13:23:09 -0500751 lpfc_init_link(phba, pmb, fc_topology, phba->cfg_link_speed);
James Smart84d1b002010-02-12 14:42:33 -0500752 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart1b511972011-12-13 13:23:09 -0500753 if (phba->sli_rev < LPFC_SLI_REV4)
754 lpfc_set_loopback_flag(phba);
James Smart6e7288d2010-06-07 15:23:35 -0400755 rc = lpfc_sli_issue_mbox(phba, pmb, flag);
James Smart76a95d72010-11-20 23:11:48 -0500756 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
James Smart84d1b002010-02-12 14:42:33 -0500757 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
758 "0498 Adapter failed to init, mbxCmd x%x "
759 "INIT_LINK, mbxStatus x%x\n",
760 mb->mbxCommand, mb->mbxStatus);
James Smart76a95d72010-11-20 23:11:48 -0500761 if (phba->sli_rev <= LPFC_SLI_REV3) {
762 /* Clear all interrupt enable conditions */
763 writel(0, phba->HCregaddr);
764 readl(phba->HCregaddr); /* flush */
765 /* Clear all pending interrupts */
766 writel(0xffffffff, phba->HAregaddr);
767 readl(phba->HAregaddr); /* flush */
768 }
James Smart84d1b002010-02-12 14:42:33 -0500769 phba->link_state = LPFC_HBA_ERROR;
James Smart6e7288d2010-06-07 15:23:35 -0400770 if (rc != MBX_BUSY || flag == MBX_POLL)
James Smart84d1b002010-02-12 14:42:33 -0500771 mempool_free(pmb, phba->mbox_mem_pool);
772 return -EIO;
773 }
James Smarte40a02c2010-02-26 14:13:54 -0500774 phba->cfg_suppress_link_up = LPFC_INITIALIZE_LINK;
James Smart6e7288d2010-06-07 15:23:35 -0400775 if (flag == MBX_POLL)
776 mempool_free(pmb, phba->mbox_mem_pool);
James Smart84d1b002010-02-12 14:42:33 -0500777
778 return 0;
779}
780
781/**
782 * lpfc_hba_down_link - this routine downs the FC link
James Smart6e7288d2010-06-07 15:23:35 -0400783 * @phba: pointer to lpfc hba data structure.
784 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
James Smart84d1b002010-02-12 14:42:33 -0500785 *
786 * This routine will issue the DOWN_LINK mailbox command call.
787 * It is available to other drivers through the lpfc_hba data
788 * structure for use to stop the link.
789 *
790 * Return code
791 * 0 - success
792 * Any other value - error
793 **/
Rashika Kheriae399b222014-09-03 12:55:28 -0400794static int
James Smart6e7288d2010-06-07 15:23:35 -0400795lpfc_hba_down_link(struct lpfc_hba *phba, uint32_t flag)
James Smart84d1b002010-02-12 14:42:33 -0500796{
797 LPFC_MBOXQ_t *pmb;
798 int rc;
799
800 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
801 if (!pmb) {
802 phba->link_state = LPFC_HBA_ERROR;
803 return -ENOMEM;
804 }
805
806 lpfc_printf_log(phba,
807 KERN_ERR, LOG_INIT,
808 "0491 Adapter Link is disabled.\n");
809 lpfc_down_link(phba, pmb);
810 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart6e7288d2010-06-07 15:23:35 -0400811 rc = lpfc_sli_issue_mbox(phba, pmb, flag);
James Smart84d1b002010-02-12 14:42:33 -0500812 if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
813 lpfc_printf_log(phba,
814 KERN_ERR, LOG_INIT,
815 "2522 Adapter failed to issue DOWN_LINK"
816 " mbox command rc 0x%x\n", rc);
817
818 mempool_free(pmb, phba->mbox_mem_pool);
819 return -EIO;
820 }
James Smart6e7288d2010-06-07 15:23:35 -0400821 if (flag == MBX_POLL)
822 mempool_free(pmb, phba->mbox_mem_pool);
823
James Smart84d1b002010-02-12 14:42:33 -0500824 return 0;
825}
826
827/**
James Smart3621a712009-04-06 18:47:14 -0400828 * lpfc_hba_down_prep - Perform lpfc uninitialization prior to HBA reset
James Smarte59058c2008-08-24 21:49:00 -0400829 * @phba: pointer to lpfc HBA data structure.
830 *
831 * This routine will do LPFC uninitialization before the HBA is reset when
832 * bringing down the SLI Layer.
833 *
834 * Return codes
835 * 0 - success.
836 * Any other value - error.
837 **/
dea31012005-04-17 16:05:31 -0500838int
James Smart2e0fef82007-06-17 19:56:36 -0500839lpfc_hba_down_prep(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500840{
James Smart1b32f6a2008-02-08 18:49:39 -0500841 struct lpfc_vport **vports;
842 int i;
James Smart3772a992009-05-22 14:50:54 -0400843
844 if (phba->sli_rev <= LPFC_SLI_REV3) {
845 /* Disable interrupts */
846 writel(0, phba->HCregaddr);
847 readl(phba->HCregaddr); /* flush */
848 }
dea31012005-04-17 16:05:31 -0500849
James Smart1b32f6a2008-02-08 18:49:39 -0500850 if (phba->pport->load_flag & FC_UNLOADING)
851 lpfc_cleanup_discovery_resources(phba->pport);
852 else {
853 vports = lpfc_create_vport_work_array(phba);
854 if (vports != NULL)
James Smart3772a992009-05-22 14:50:54 -0400855 for (i = 0; i <= phba->max_vports &&
856 vports[i] != NULL; i++)
James Smart1b32f6a2008-02-08 18:49:39 -0500857 lpfc_cleanup_discovery_resources(vports[i]);
858 lpfc_destroy_vport_work_array(phba, vports);
James Smart7f5f3d02008-02-08 18:50:14 -0500859 }
860 return 0;
dea31012005-04-17 16:05:31 -0500861}
862
James Smarte59058c2008-08-24 21:49:00 -0400863/**
James Smart68e814f2014-05-21 08:04:59 -0400864 * lpfc_sli4_free_sp_events - Cleanup sp_queue_events to free
865 * rspiocb which got deferred
866 *
867 * @phba: pointer to lpfc HBA data structure.
868 *
869 * This routine will cleanup completed slow path events after HBA is reset
870 * when bringing down the SLI Layer.
871 *
872 *
873 * Return codes
874 * void.
875 **/
876static void
877lpfc_sli4_free_sp_events(struct lpfc_hba *phba)
878{
879 struct lpfc_iocbq *rspiocbq;
880 struct hbq_dmabuf *dmabuf;
881 struct lpfc_cq_event *cq_event;
882
883 spin_lock_irq(&phba->hbalock);
884 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
885 spin_unlock_irq(&phba->hbalock);
886
887 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
888 /* Get the response iocb from the head of work queue */
889 spin_lock_irq(&phba->hbalock);
890 list_remove_head(&phba->sli4_hba.sp_queue_event,
891 cq_event, struct lpfc_cq_event, list);
892 spin_unlock_irq(&phba->hbalock);
893
894 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
895 case CQE_CODE_COMPL_WQE:
896 rspiocbq = container_of(cq_event, struct lpfc_iocbq,
897 cq_event);
898 lpfc_sli_release_iocbq(phba, rspiocbq);
899 break;
900 case CQE_CODE_RECEIVE:
901 case CQE_CODE_RECEIVE_V1:
902 dmabuf = container_of(cq_event, struct hbq_dmabuf,
903 cq_event);
904 lpfc_in_buf_free(phba, &dmabuf->dbuf);
905 }
906 }
907}
908
909/**
James Smartbcece5f2014-04-04 13:51:34 -0400910 * lpfc_hba_free_post_buf - Perform lpfc uninitialization after HBA reset
911 * @phba: pointer to lpfc HBA data structure.
912 *
913 * This routine will cleanup posted ELS buffers after the HBA is reset
914 * when bringing down the SLI Layer.
915 *
916 *
917 * Return codes
918 * void.
919 **/
920static void
921lpfc_hba_free_post_buf(struct lpfc_hba *phba)
922{
923 struct lpfc_sli *psli = &phba->sli;
924 struct lpfc_sli_ring *pring;
925 struct lpfc_dmabuf *mp, *next_mp;
James Smart07eab622014-04-04 13:51:44 -0400926 LIST_HEAD(buflist);
927 int count;
James Smartbcece5f2014-04-04 13:51:34 -0400928
929 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
930 lpfc_sli_hbqbuf_free_all(phba);
931 else {
932 /* Cleanup preposted buffers on the ELS ring */
James Smart895427b2017-02-12 13:52:30 -0800933 pring = &psli->sli3_ring[LPFC_ELS_RING];
James Smart07eab622014-04-04 13:51:44 -0400934 spin_lock_irq(&phba->hbalock);
935 list_splice_init(&pring->postbufq, &buflist);
936 spin_unlock_irq(&phba->hbalock);
937
938 count = 0;
939 list_for_each_entry_safe(mp, next_mp, &buflist, list) {
James Smartbcece5f2014-04-04 13:51:34 -0400940 list_del(&mp->list);
James Smart07eab622014-04-04 13:51:44 -0400941 count++;
James Smartbcece5f2014-04-04 13:51:34 -0400942 lpfc_mbuf_free(phba, mp->virt, mp->phys);
943 kfree(mp);
944 }
James Smart07eab622014-04-04 13:51:44 -0400945
946 spin_lock_irq(&phba->hbalock);
947 pring->postbufq_cnt -= count;
James Smartbcece5f2014-04-04 13:51:34 -0400948 spin_unlock_irq(&phba->hbalock);
949 }
950}
951
952/**
953 * lpfc_hba_clean_txcmplq - Perform lpfc uninitialization after HBA reset
954 * @phba: pointer to lpfc HBA data structure.
955 *
956 * This routine will cleanup the txcmplq after the HBA is reset when bringing
957 * down the SLI Layer.
958 *
959 * Return codes
960 * void
961 **/
962static void
963lpfc_hba_clean_txcmplq(struct lpfc_hba *phba)
964{
965 struct lpfc_sli *psli = &phba->sli;
James Smart895427b2017-02-12 13:52:30 -0800966 struct lpfc_queue *qp = NULL;
James Smartbcece5f2014-04-04 13:51:34 -0400967 struct lpfc_sli_ring *pring;
968 LIST_HEAD(completions);
969 int i;
James Smartc1dd9112018-01-30 15:58:57 -0800970 struct lpfc_iocbq *piocb, *next_iocb;
James Smartbcece5f2014-04-04 13:51:34 -0400971
James Smart895427b2017-02-12 13:52:30 -0800972 if (phba->sli_rev != LPFC_SLI_REV4) {
973 for (i = 0; i < psli->num_rings; i++) {
974 pring = &psli->sli3_ring[i];
James Smartbcece5f2014-04-04 13:51:34 -0400975 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -0800976 /* At this point in time the HBA is either reset or DOA
977 * Nothing should be on txcmplq as it will
978 * NEVER complete.
979 */
980 list_splice_init(&pring->txcmplq, &completions);
981 pring->txcmplq_cnt = 0;
James Smartbcece5f2014-04-04 13:51:34 -0400982 spin_unlock_irq(&phba->hbalock);
983
James Smart895427b2017-02-12 13:52:30 -0800984 lpfc_sli_abort_iocb_ring(phba, pring);
985 }
James Smartbcece5f2014-04-04 13:51:34 -0400986 /* Cancel all the IOCBs from the completions list */
James Smart895427b2017-02-12 13:52:30 -0800987 lpfc_sli_cancel_iocbs(phba, &completions,
988 IOSTAT_LOCAL_REJECT, IOERR_SLI_ABORTED);
989 return;
990 }
991 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
992 pring = qp->pring;
993 if (!pring)
994 continue;
995 spin_lock_irq(&pring->ring_lock);
James Smartc1dd9112018-01-30 15:58:57 -0800996 list_for_each_entry_safe(piocb, next_iocb,
997 &pring->txcmplq, list)
998 piocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smart895427b2017-02-12 13:52:30 -0800999 list_splice_init(&pring->txcmplq, &completions);
1000 pring->txcmplq_cnt = 0;
1001 spin_unlock_irq(&pring->ring_lock);
James Smartbcece5f2014-04-04 13:51:34 -04001002 lpfc_sli_abort_iocb_ring(phba, pring);
1003 }
James Smart895427b2017-02-12 13:52:30 -08001004 /* Cancel all the IOCBs from the completions list */
1005 lpfc_sli_cancel_iocbs(phba, &completions,
1006 IOSTAT_LOCAL_REJECT, IOERR_SLI_ABORTED);
James Smartbcece5f2014-04-04 13:51:34 -04001007}
1008
1009/**
James Smart3772a992009-05-22 14:50:54 -04001010 * lpfc_hba_down_post_s3 - Perform lpfc uninitialization after HBA reset
James Smartbcece5f2014-04-04 13:51:34 -04001011 int i;
James Smarte59058c2008-08-24 21:49:00 -04001012 * @phba: pointer to lpfc HBA data structure.
1013 *
1014 * This routine will do uninitialization after the HBA is reset when bring
1015 * down the SLI Layer.
1016 *
1017 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001018 * 0 - success.
James Smarte59058c2008-08-24 21:49:00 -04001019 * Any other value - error.
1020 **/
James Smart3772a992009-05-22 14:50:54 -04001021static int
1022lpfc_hba_down_post_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05001023{
James Smartbcece5f2014-04-04 13:51:34 -04001024 lpfc_hba_free_post_buf(phba);
1025 lpfc_hba_clean_txcmplq(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -05001026 return 0;
1027}
James Smart5af5eee2010-10-22 11:06:38 -04001028
James Smartda0436e2009-05-22 14:51:39 -04001029/**
1030 * lpfc_hba_down_post_s4 - Perform lpfc uninitialization after HBA reset
1031 * @phba: pointer to lpfc HBA data structure.
1032 *
1033 * This routine will do uninitialization after the HBA is reset when bring
1034 * down the SLI Layer.
1035 *
1036 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001037 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -04001038 * Any other value - error.
1039 **/
1040static int
1041lpfc_hba_down_post_s4(struct lpfc_hba *phba)
1042{
James Smartc4908502019-01-28 11:14:28 -08001043 struct lpfc_io_buf *psb, *psb_next;
James Smart86c67372017-04-21 16:05:04 -07001044 struct lpfc_nvmet_rcv_ctx *ctxp, *ctxp_next;
James Smart5e5b5112019-01-28 11:14:22 -08001045 struct lpfc_sli4_hdw_queue *qp;
James Smartda0436e2009-05-22 14:51:39 -04001046 LIST_HEAD(aborts);
James Smart895427b2017-02-12 13:52:30 -08001047 LIST_HEAD(nvme_aborts);
James Smart86c67372017-04-21 16:05:04 -07001048 LIST_HEAD(nvmet_aborts);
James Smart0f65ff62010-02-26 14:14:23 -05001049 struct lpfc_sglq *sglq_entry = NULL;
James Smart5e5b5112019-01-28 11:14:22 -08001050 int cnt, idx;
James Smart0f65ff62010-02-26 14:14:23 -05001051
James Smart895427b2017-02-12 13:52:30 -08001052
1053 lpfc_sli_hbqbuf_free_all(phba);
James Smartbcece5f2014-04-04 13:51:34 -04001054 lpfc_hba_clean_txcmplq(phba);
1055
James Smartda0436e2009-05-22 14:51:39 -04001056 /* At this point in time the HBA is either reset or DOA. Either
1057 * way, nothing should be on lpfc_abts_els_sgl_list, it needs to be
James Smart895427b2017-02-12 13:52:30 -08001058 * on the lpfc_els_sgl_list so that it can either be freed if the
James Smartda0436e2009-05-22 14:51:39 -04001059 * driver is unloading or reposted if the driver is restarting
1060 * the port.
1061 */
James Smart895427b2017-02-12 13:52:30 -08001062 spin_lock_irq(&phba->hbalock); /* required for lpfc_els_sgl_list and */
James Smartda0436e2009-05-22 14:51:39 -04001063 /* scsl_buf_list */
James Smart895427b2017-02-12 13:52:30 -08001064 /* sgl_list_lock required because worker thread uses this
James Smartda0436e2009-05-22 14:51:39 -04001065 * list.
1066 */
James Smart895427b2017-02-12 13:52:30 -08001067 spin_lock(&phba->sli4_hba.sgl_list_lock);
James Smart0f65ff62010-02-26 14:14:23 -05001068 list_for_each_entry(sglq_entry,
1069 &phba->sli4_hba.lpfc_abts_els_sgl_list, list)
1070 sglq_entry->state = SGL_FREED;
1071
James Smartda0436e2009-05-22 14:51:39 -04001072 list_splice_init(&phba->sli4_hba.lpfc_abts_els_sgl_list,
James Smart895427b2017-02-12 13:52:30 -08001073 &phba->sli4_hba.lpfc_els_sgl_list);
1074
James Smartf358dd02017-02-12 13:52:34 -08001075
James Smart895427b2017-02-12 13:52:30 -08001076 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smart5e5b5112019-01-28 11:14:22 -08001077
1078 /* abts_xxxx_buf_list_lock required because worker thread uses this
James Smartda0436e2009-05-22 14:51:39 -04001079 * list.
1080 */
James Smart5e5b5112019-01-28 11:14:22 -08001081 cnt = 0;
1082 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
1083 qp = &phba->sli4_hba.hdwq[idx];
1084
James Smartc00f62e2019-08-14 16:57:11 -07001085 spin_lock(&qp->abts_io_buf_list_lock);
1086 list_splice_init(&qp->lpfc_abts_io_buf_list,
James Smart895427b2017-02-12 13:52:30 -08001087 &aborts);
James Smart895427b2017-02-12 13:52:30 -08001088
James Smart0794d602019-01-28 11:14:19 -08001089 list_for_each_entry_safe(psb, psb_next, &aborts, list) {
James Smart86c67372017-04-21 16:05:04 -07001090 psb->pCmd = NULL;
1091 psb->status = IOSTAT_SUCCESS;
James Smartcf1a1d32017-12-08 17:18:03 -08001092 cnt++;
James Smart86c67372017-04-21 16:05:04 -07001093 }
James Smart5e5b5112019-01-28 11:14:22 -08001094 spin_lock(&qp->io_buf_list_put_lock);
1095 list_splice_init(&aborts, &qp->lpfc_io_buf_list_put);
1096 qp->put_io_bufs += qp->abts_scsi_io_bufs;
James Smartc00f62e2019-08-14 16:57:11 -07001097 qp->put_io_bufs += qp->abts_nvme_io_bufs;
James Smart5e5b5112019-01-28 11:14:22 -08001098 qp->abts_scsi_io_bufs = 0;
James Smartc00f62e2019-08-14 16:57:11 -07001099 qp->abts_nvme_io_bufs = 0;
James Smart5e5b5112019-01-28 11:14:22 -08001100 spin_unlock(&qp->io_buf_list_put_lock);
James Smartc00f62e2019-08-14 16:57:11 -07001101 spin_unlock(&qp->abts_io_buf_list_lock);
James Smart5e5b5112019-01-28 11:14:22 -08001102 }
James Smart731eedc2019-03-12 16:30:12 -07001103 spin_unlock_irq(&phba->hbalock);
James Smart5e5b5112019-01-28 11:14:22 -08001104
1105 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
James Smart731eedc2019-03-12 16:30:12 -07001106 spin_lock_irq(&phba->sli4_hba.abts_nvmet_buf_list_lock);
James Smart5e5b5112019-01-28 11:14:22 -08001107 list_splice_init(&phba->sli4_hba.lpfc_abts_nvmet_ctx_list,
1108 &nvmet_aborts);
James Smart731eedc2019-03-12 16:30:12 -07001109 spin_unlock_irq(&phba->sli4_hba.abts_nvmet_buf_list_lock);
James Smart86c67372017-04-21 16:05:04 -07001110 list_for_each_entry_safe(ctxp, ctxp_next, &nvmet_aborts, list) {
1111 ctxp->flag &= ~(LPFC_NVMET_XBUSY | LPFC_NVMET_ABORT_OP);
James Smart6c621a22017-05-15 15:20:45 -07001112 lpfc_nvmet_ctxbuf_post(phba, ctxp->ctxbuf);
James Smart86c67372017-04-21 16:05:04 -07001113 }
James Smart895427b2017-02-12 13:52:30 -08001114 }
James Smart895427b2017-02-12 13:52:30 -08001115
James Smart68e814f2014-05-21 08:04:59 -04001116 lpfc_sli4_free_sp_events(phba);
James Smart5e5b5112019-01-28 11:14:22 -08001117 return cnt;
James Smartda0436e2009-05-22 14:51:39 -04001118}
1119
1120/**
1121 * lpfc_hba_down_post - Wrapper func for hba down post routine
1122 * @phba: pointer to lpfc HBA data structure.
1123 *
1124 * This routine wraps the actual SLI3 or SLI4 routine for performing
1125 * uninitialization after the HBA is reset when bring down the SLI Layer.
1126 *
1127 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001128 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -04001129 * Any other value - error.
1130 **/
1131int
1132lpfc_hba_down_post(struct lpfc_hba *phba)
1133{
1134 return (*phba->lpfc_hba_down_post)(phba);
1135}
Jamie Wellnitz41415862006-02-28 19:25:27 -05001136
James Smarte59058c2008-08-24 21:49:00 -04001137/**
James Smart3621a712009-04-06 18:47:14 -04001138 * lpfc_hb_timeout - The HBA-timer timeout handler
James Smarte59058c2008-08-24 21:49:00 -04001139 * @ptr: unsigned long holds the pointer to lpfc hba data structure.
1140 *
1141 * This is the HBA-timer timeout handler registered to the lpfc driver. When
1142 * this timer fires, a HBA timeout event shall be posted to the lpfc driver
1143 * work-port-events bitmap and the worker thread is notified. This timeout
1144 * event will be used by the worker thread to invoke the actual timeout
1145 * handler routine, lpfc_hb_timeout_handler. Any periodical operations will
1146 * be performed in the timeout handler and the HBA timeout event bit shall
1147 * be cleared by the worker thread after it has taken the event bitmap out.
1148 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001149static void
Kees Cookf22eb4d2017-09-06 20:24:26 -07001150lpfc_hb_timeout(struct timer_list *t)
James Smart858c9f62007-06-17 19:56:39 -05001151{
1152 struct lpfc_hba *phba;
James Smart5e9d9b82008-06-14 22:52:53 -04001153 uint32_t tmo_posted;
James Smart858c9f62007-06-17 19:56:39 -05001154 unsigned long iflag;
1155
Kees Cookf22eb4d2017-09-06 20:24:26 -07001156 phba = from_timer(phba, t, hb_tmofunc);
James Smart93996272008-08-24 21:50:30 -04001157
1158 /* Check for heart beat timeout conditions */
James Smart858c9f62007-06-17 19:56:39 -05001159 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -04001160 tmo_posted = phba->pport->work_port_events & WORKER_HB_TMO;
1161 if (!tmo_posted)
James Smart858c9f62007-06-17 19:56:39 -05001162 phba->pport->work_port_events |= WORKER_HB_TMO;
1163 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
1164
James Smart93996272008-08-24 21:50:30 -04001165 /* Tell the worker thread there is work to do */
James Smart5e9d9b82008-06-14 22:52:53 -04001166 if (!tmo_posted)
1167 lpfc_worker_wake_up(phba);
James Smart858c9f62007-06-17 19:56:39 -05001168 return;
1169}
1170
James Smarte59058c2008-08-24 21:49:00 -04001171/**
James Smart19ca7602010-11-20 23:11:55 -05001172 * lpfc_rrq_timeout - The RRQ-timer timeout handler
1173 * @ptr: unsigned long holds the pointer to lpfc hba data structure.
1174 *
1175 * This is the RRQ-timer timeout handler registered to the lpfc driver. When
1176 * this timer fires, a RRQ timeout event shall be posted to the lpfc driver
1177 * work-port-events bitmap and the worker thread is notified. This timeout
1178 * event will be used by the worker thread to invoke the actual timeout
1179 * handler routine, lpfc_rrq_handler. Any periodical operations will
1180 * be performed in the timeout handler and the RRQ timeout event bit shall
1181 * be cleared by the worker thread after it has taken the event bitmap out.
1182 **/
1183static void
Kees Cookf22eb4d2017-09-06 20:24:26 -07001184lpfc_rrq_timeout(struct timer_list *t)
James Smart19ca7602010-11-20 23:11:55 -05001185{
1186 struct lpfc_hba *phba;
James Smart19ca7602010-11-20 23:11:55 -05001187 unsigned long iflag;
1188
Kees Cookf22eb4d2017-09-06 20:24:26 -07001189 phba = from_timer(phba, t, rrq_tmr);
James Smart19ca7602010-11-20 23:11:55 -05001190 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart06918ac2014-02-20 09:57:57 -05001191 if (!(phba->pport->load_flag & FC_UNLOADING))
1192 phba->hba_flag |= HBA_RRQ_ACTIVE;
1193 else
1194 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
James Smart19ca7602010-11-20 23:11:55 -05001195 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
James Smart06918ac2014-02-20 09:57:57 -05001196
1197 if (!(phba->pport->load_flag & FC_UNLOADING))
1198 lpfc_worker_wake_up(phba);
James Smart19ca7602010-11-20 23:11:55 -05001199}
1200
1201/**
James Smart3621a712009-04-06 18:47:14 -04001202 * lpfc_hb_mbox_cmpl - The lpfc heart-beat mailbox command callback function
James Smarte59058c2008-08-24 21:49:00 -04001203 * @phba: pointer to lpfc hba data structure.
1204 * @pmboxq: pointer to the driver internal queue element for mailbox command.
1205 *
1206 * This is the callback function to the lpfc heart-beat mailbox command.
1207 * If configured, the lpfc driver issues the heart-beat mailbox command to
1208 * the HBA every LPFC_HB_MBOX_INTERVAL (current 5) seconds. At the time the
1209 * heart-beat mailbox command is issued, the driver shall set up heart-beat
1210 * timeout timer to LPFC_HB_MBOX_TIMEOUT (current 30) seconds and marks
1211 * heart-beat outstanding state. Once the mailbox command comes back and
1212 * no error conditions detected, the heart-beat mailbox command timer is
1213 * reset to LPFC_HB_MBOX_INTERVAL seconds and the heart-beat outstanding
1214 * state is cleared for the next heart-beat. If the timer expired with the
1215 * heart-beat outstanding state set, the driver will put the HBA offline.
1216 **/
James Smart858c9f62007-06-17 19:56:39 -05001217static void
1218lpfc_hb_mbox_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
1219{
1220 unsigned long drvr_flag;
1221
1222 spin_lock_irqsave(&phba->hbalock, drvr_flag);
1223 phba->hb_outstanding = 0;
1224 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
1225
James Smart93996272008-08-24 21:50:30 -04001226 /* Check and reset heart-beat timer is necessary */
James Smart858c9f62007-06-17 19:56:39 -05001227 mempool_free(pmboxq, phba->mbox_mem_pool);
1228 if (!(phba->pport->fc_flag & FC_OFFLINE_MODE) &&
1229 !(phba->link_state == LPFC_HBA_ERROR) &&
James Smart51ef4c22007-08-02 11:10:31 -04001230 !(phba->pport->load_flag & FC_UNLOADING))
James Smart858c9f62007-06-17 19:56:39 -05001231 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001232 jiffies +
1233 msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smart858c9f62007-06-17 19:56:39 -05001234 return;
1235}
1236
James Smart32517fc2019-01-28 11:14:33 -08001237static void
1238lpfc_hb_eq_delay_work(struct work_struct *work)
1239{
1240 struct lpfc_hba *phba = container_of(to_delayed_work(work),
1241 struct lpfc_hba, eq_delay_work);
1242 struct lpfc_eq_intr_info *eqi, *eqi_new;
1243 struct lpfc_queue *eq, *eq_next;
James Smart8156d372019-10-18 14:18:26 -07001244 unsigned char *ena_delay = NULL;
James Smart32517fc2019-01-28 11:14:33 -08001245 uint32_t usdelay;
1246 int i;
1247
1248 if (!phba->cfg_auto_imax || phba->pport->load_flag & FC_UNLOADING)
1249 return;
1250
1251 if (phba->link_state == LPFC_HBA_ERROR ||
1252 phba->pport->fc_flag & FC_OFFLINE_MODE)
1253 goto requeue;
1254
James Smart8156d372019-10-18 14:18:26 -07001255 ena_delay = kcalloc(phba->sli4_hba.num_possible_cpu, sizeof(*ena_delay),
1256 GFP_KERNEL);
1257 if (!ena_delay)
James Smart32517fc2019-01-28 11:14:33 -08001258 goto requeue;
1259
James Smart8156d372019-10-18 14:18:26 -07001260 for (i = 0; i < phba->cfg_irq_chann; i++) {
1261 /* Get the EQ corresponding to the IRQ vector */
1262 eq = phba->sli4_hba.hba_eq_hdl[i].eq;
1263 if (!eq)
1264 continue;
1265 if (eq->q_mode || eq->q_flag & HBA_EQ_DELAY_CHK) {
1266 eq->q_flag &= ~HBA_EQ_DELAY_CHK;
1267 ena_delay[eq->last_cpu] = 1;
James Smart8d34a592019-08-14 16:56:35 -07001268 }
James Smart8156d372019-10-18 14:18:26 -07001269 }
James Smart32517fc2019-01-28 11:14:33 -08001270
1271 for_each_present_cpu(i) {
James Smart32517fc2019-01-28 11:14:33 -08001272 eqi = per_cpu_ptr(phba->sli4_hba.eq_info, i);
James Smart8156d372019-10-18 14:18:26 -07001273 if (ena_delay[i]) {
1274 usdelay = (eqi->icnt >> 10) * LPFC_EQ_DELAY_STEP;
1275 if (usdelay > LPFC_MAX_AUTO_EQ_DELAY)
1276 usdelay = LPFC_MAX_AUTO_EQ_DELAY;
1277 } else {
1278 usdelay = 0;
James Smart8d34a592019-08-14 16:56:35 -07001279 }
James Smart32517fc2019-01-28 11:14:33 -08001280
James Smart32517fc2019-01-28 11:14:33 -08001281 eqi->icnt = 0;
1282
1283 list_for_each_entry_safe(eq, eq_next, &eqi->list, cpu_list) {
James Smart8156d372019-10-18 14:18:26 -07001284 if (unlikely(eq->last_cpu != i)) {
James Smart32517fc2019-01-28 11:14:33 -08001285 eqi_new = per_cpu_ptr(phba->sli4_hba.eq_info,
1286 eq->last_cpu);
1287 list_move_tail(&eq->cpu_list, &eqi_new->list);
1288 continue;
1289 }
1290 if (usdelay != eq->q_mode)
1291 lpfc_modify_hba_eq_delay(phba, eq->hdwq, 1,
1292 usdelay);
1293 }
1294 }
1295
James Smart8156d372019-10-18 14:18:26 -07001296 kfree(ena_delay);
James Smart32517fc2019-01-28 11:14:33 -08001297
1298requeue:
1299 queue_delayed_work(phba->wq, &phba->eq_delay_work,
1300 msecs_to_jiffies(LPFC_EQ_DELAY_MSECS));
1301}
1302
James Smarte59058c2008-08-24 21:49:00 -04001303/**
James Smartc4908502019-01-28 11:14:28 -08001304 * lpfc_hb_mxp_handler - Multi-XRI pools handler to adjust XRI distribution
1305 * @phba: pointer to lpfc hba data structure.
1306 *
1307 * For each heartbeat, this routine does some heuristic methods to adjust
1308 * XRI distribution. The goal is to fully utilize free XRIs.
1309 **/
1310static void lpfc_hb_mxp_handler(struct lpfc_hba *phba)
1311{
1312 u32 i;
1313 u32 hwq_count;
1314
1315 hwq_count = phba->cfg_hdw_queue;
1316 for (i = 0; i < hwq_count; i++) {
1317 /* Adjust XRIs in private pool */
1318 lpfc_adjust_pvt_pool_count(phba, i);
1319
1320 /* Adjust high watermark */
1321 lpfc_adjust_high_watermark(phba, i);
1322
1323#ifdef LPFC_MXP_STAT
1324 /* Snapshot pbl, pvt and busy count */
1325 lpfc_snapshot_mxp(phba, i);
1326#endif
1327 }
1328}
1329
James Smarte59058c2008-08-24 21:49:00 -04001330/**
James Smart3621a712009-04-06 18:47:14 -04001331 * lpfc_hb_timeout_handler - The HBA-timer timeout handler
James Smarte59058c2008-08-24 21:49:00 -04001332 * @phba: pointer to lpfc hba data structure.
1333 *
1334 * This is the actual HBA-timer timeout handler to be invoked by the worker
1335 * thread whenever the HBA timer fired and HBA-timeout event posted. This
1336 * handler performs any periodic operations needed for the device. If such
1337 * periodic event has already been attended to either in the interrupt handler
1338 * or by processing slow-ring or fast-ring events within the HBA-timer
1339 * timeout window (LPFC_HB_MBOX_INTERVAL), this handler just simply resets
1340 * the timer for the next timeout period. If lpfc heart-beat mailbox command
1341 * is configured and there is no heart-beat mailbox command outstanding, a
1342 * heart-beat mailbox is issued and timer set properly. Otherwise, if there
1343 * has been a heart-beat mailbox command outstanding, the HBA shall be put
1344 * to offline.
1345 **/
James Smart858c9f62007-06-17 19:56:39 -05001346void
1347lpfc_hb_timeout_handler(struct lpfc_hba *phba)
1348{
James Smart45ed1192009-10-02 15:17:02 -04001349 struct lpfc_vport **vports;
James Smart858c9f62007-06-17 19:56:39 -05001350 LPFC_MBOXQ_t *pmboxq;
James Smart0ff10d42008-01-11 01:52:36 -05001351 struct lpfc_dmabuf *buf_ptr;
James Smart45ed1192009-10-02 15:17:02 -04001352 int retval, i;
James Smart858c9f62007-06-17 19:56:39 -05001353 struct lpfc_sli *psli = &phba->sli;
James Smart0ff10d42008-01-11 01:52:36 -05001354 LIST_HEAD(completions);
James Smart858c9f62007-06-17 19:56:39 -05001355
James Smartc4908502019-01-28 11:14:28 -08001356 if (phba->cfg_xri_rebalancing) {
1357 /* Multi-XRI pools handler */
1358 lpfc_hb_mxp_handler(phba);
1359 }
James Smart858c9f62007-06-17 19:56:39 -05001360
James Smart45ed1192009-10-02 15:17:02 -04001361 vports = lpfc_create_vport_work_array(phba);
1362 if (vports != NULL)
James Smart4258e982015-12-16 18:11:58 -05001363 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart45ed1192009-10-02 15:17:02 -04001364 lpfc_rcv_seq_check_edtov(vports[i]);
James Smart4258e982015-12-16 18:11:58 -05001365 lpfc_fdmi_num_disc_check(vports[i]);
1366 }
James Smart45ed1192009-10-02 15:17:02 -04001367 lpfc_destroy_vport_work_array(phba, vports);
1368
James Smart858c9f62007-06-17 19:56:39 -05001369 if ((phba->link_state == LPFC_HBA_ERROR) ||
James Smart51ef4c22007-08-02 11:10:31 -04001370 (phba->pport->load_flag & FC_UNLOADING) ||
James Smart858c9f62007-06-17 19:56:39 -05001371 (phba->pport->fc_flag & FC_OFFLINE_MODE))
1372 return;
1373
1374 spin_lock_irq(&phba->pport->work_port_lock);
James Smart858c9f62007-06-17 19:56:39 -05001375
James Smart256ec0d2013-04-17 20:14:58 -04001376 if (time_after(phba->last_completion_time +
1377 msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL),
1378 jiffies)) {
James Smart858c9f62007-06-17 19:56:39 -05001379 spin_unlock_irq(&phba->pport->work_port_lock);
1380 if (!phba->hb_outstanding)
1381 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001382 jiffies +
1383 msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smart858c9f62007-06-17 19:56:39 -05001384 else
1385 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001386 jiffies +
1387 msecs_to_jiffies(1000 * LPFC_HB_MBOX_TIMEOUT));
James Smart858c9f62007-06-17 19:56:39 -05001388 return;
1389 }
1390 spin_unlock_irq(&phba->pport->work_port_lock);
1391
James Smart0ff10d42008-01-11 01:52:36 -05001392 if (phba->elsbuf_cnt &&
1393 (phba->elsbuf_cnt == phba->elsbuf_prev_cnt)) {
1394 spin_lock_irq(&phba->hbalock);
1395 list_splice_init(&phba->elsbuf, &completions);
1396 phba->elsbuf_cnt = 0;
1397 phba->elsbuf_prev_cnt = 0;
1398 spin_unlock_irq(&phba->hbalock);
1399
1400 while (!list_empty(&completions)) {
1401 list_remove_head(&completions, buf_ptr,
1402 struct lpfc_dmabuf, list);
1403 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
1404 kfree(buf_ptr);
1405 }
1406 }
1407 phba->elsbuf_prev_cnt = phba->elsbuf_cnt;
1408
James Smart858c9f62007-06-17 19:56:39 -05001409 /* If there is no heart beat outstanding, issue a heartbeat command */
James Smart13815c82008-01-11 01:52:48 -05001410 if (phba->cfg_enable_hba_heartbeat) {
1411 if (!phba->hb_outstanding) {
James Smartbc739052010-08-04 16:11:18 -04001412 if ((!(psli->sli_flag & LPFC_SLI_MBOX_ACTIVE)) &&
1413 (list_empty(&psli->mboxq))) {
1414 pmboxq = mempool_alloc(phba->mbox_mem_pool,
1415 GFP_KERNEL);
1416 if (!pmboxq) {
1417 mod_timer(&phba->hb_tmofunc,
1418 jiffies +
James Smart256ec0d2013-04-17 20:14:58 -04001419 msecs_to_jiffies(1000 *
1420 LPFC_HB_MBOX_INTERVAL));
James Smartbc739052010-08-04 16:11:18 -04001421 return;
1422 }
James Smart13815c82008-01-11 01:52:48 -05001423
James Smartbc739052010-08-04 16:11:18 -04001424 lpfc_heart_beat(phba, pmboxq);
1425 pmboxq->mbox_cmpl = lpfc_hb_mbox_cmpl;
1426 pmboxq->vport = phba->pport;
1427 retval = lpfc_sli_issue_mbox(phba, pmboxq,
1428 MBX_NOWAIT);
James Smart13815c82008-01-11 01:52:48 -05001429
James Smartbc739052010-08-04 16:11:18 -04001430 if (retval != MBX_BUSY &&
1431 retval != MBX_SUCCESS) {
1432 mempool_free(pmboxq,
1433 phba->mbox_mem_pool);
1434 mod_timer(&phba->hb_tmofunc,
1435 jiffies +
James Smart256ec0d2013-04-17 20:14:58 -04001436 msecs_to_jiffies(1000 *
1437 LPFC_HB_MBOX_INTERVAL));
James Smartbc739052010-08-04 16:11:18 -04001438 return;
1439 }
1440 phba->skipped_hb = 0;
1441 phba->hb_outstanding = 1;
1442 } else if (time_before_eq(phba->last_completion_time,
1443 phba->skipped_hb)) {
1444 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
1445 "2857 Last completion time not "
1446 " updated in %d ms\n",
1447 jiffies_to_msecs(jiffies
1448 - phba->last_completion_time));
1449 } else
1450 phba->skipped_hb = jiffies;
1451
James Smart858c9f62007-06-17 19:56:39 -05001452 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001453 jiffies +
1454 msecs_to_jiffies(1000 * LPFC_HB_MBOX_TIMEOUT));
James Smart858c9f62007-06-17 19:56:39 -05001455 return;
James Smart13815c82008-01-11 01:52:48 -05001456 } else {
1457 /*
1458 * If heart beat timeout called with hb_outstanding set
James Smartdcf2a4e2010-09-29 11:18:53 -04001459 * we need to give the hb mailbox cmd a chance to
1460 * complete or TMO.
James Smart13815c82008-01-11 01:52:48 -05001461 */
James Smartdcf2a4e2010-09-29 11:18:53 -04001462 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
1463 "0459 Adapter heartbeat still out"
1464 "standing:last compl time was %d ms.\n",
1465 jiffies_to_msecs(jiffies
1466 - phba->last_completion_time));
1467 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001468 jiffies +
1469 msecs_to_jiffies(1000 * LPFC_HB_MBOX_TIMEOUT));
James Smart858c9f62007-06-17 19:56:39 -05001470 }
James Smart4258e982015-12-16 18:11:58 -05001471 } else {
1472 mod_timer(&phba->hb_tmofunc,
1473 jiffies +
1474 msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smart858c9f62007-06-17 19:56:39 -05001475 }
1476}
1477
James Smarte59058c2008-08-24 21:49:00 -04001478/**
James Smart3621a712009-04-06 18:47:14 -04001479 * lpfc_offline_eratt - Bring lpfc offline on hardware error attention
James Smarte59058c2008-08-24 21:49:00 -04001480 * @phba: pointer to lpfc hba data structure.
1481 *
1482 * This routine is called to bring the HBA offline when HBA hardware error
1483 * other than Port Error 6 has been detected.
1484 **/
James Smart09372822008-01-11 01:52:54 -05001485static void
1486lpfc_offline_eratt(struct lpfc_hba *phba)
1487{
1488 struct lpfc_sli *psli = &phba->sli;
1489
1490 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001491 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart09372822008-01-11 01:52:54 -05001492 spin_unlock_irq(&phba->hbalock);
James Smart618a5232012-06-12 13:54:36 -04001493 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
James Smart09372822008-01-11 01:52:54 -05001494
1495 lpfc_offline(phba);
1496 lpfc_reset_barrier(phba);
James Smartf4b4c682009-05-22 14:53:12 -04001497 spin_lock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001498 lpfc_sli_brdreset(phba);
James Smartf4b4c682009-05-22 14:53:12 -04001499 spin_unlock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001500 lpfc_hba_down_post(phba);
1501 lpfc_sli_brdready(phba, HS_MBRDY);
1502 lpfc_unblock_mgmt_io(phba);
1503 phba->link_state = LPFC_HBA_ERROR;
1504 return;
1505}
1506
James Smarte59058c2008-08-24 21:49:00 -04001507/**
James Smartda0436e2009-05-22 14:51:39 -04001508 * lpfc_sli4_offline_eratt - Bring lpfc offline on SLI4 hardware error attention
1509 * @phba: pointer to lpfc hba data structure.
1510 *
1511 * This routine is called to bring a SLI4 HBA offline when HBA hardware error
1512 * other than Port Error 6 has been detected.
1513 **/
James Smarta88dbb62013-04-17 20:18:39 -04001514void
James Smartda0436e2009-05-22 14:51:39 -04001515lpfc_sli4_offline_eratt(struct lpfc_hba *phba)
1516{
James Smart946727d2015-04-07 15:07:09 -04001517 spin_lock_irq(&phba->hbalock);
1518 phba->link_state = LPFC_HBA_ERROR;
1519 spin_unlock_irq(&phba->hbalock);
1520
James Smart618a5232012-06-12 13:54:36 -04001521 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
James Smartc00f62e2019-08-14 16:57:11 -07001522 lpfc_sli_flush_io_rings(phba);
James Smartda0436e2009-05-22 14:51:39 -04001523 lpfc_offline(phba);
James Smartda0436e2009-05-22 14:51:39 -04001524 lpfc_hba_down_post(phba);
James Smartda0436e2009-05-22 14:51:39 -04001525 lpfc_unblock_mgmt_io(phba);
James Smartda0436e2009-05-22 14:51:39 -04001526}
1527
1528/**
James Smarta257bf92009-04-06 18:48:10 -04001529 * lpfc_handle_deferred_eratt - The HBA hardware deferred error handler
1530 * @phba: pointer to lpfc hba data structure.
1531 *
1532 * This routine is invoked to handle the deferred HBA hardware error
1533 * conditions. This type of error is indicated by HBA by setting ER1
1534 * and another ER bit in the host status register. The driver will
1535 * wait until the ER1 bit clears before handling the error condition.
1536 **/
1537static void
1538lpfc_handle_deferred_eratt(struct lpfc_hba *phba)
1539{
1540 uint32_t old_host_status = phba->work_hs;
James Smarta257bf92009-04-06 18:48:10 -04001541 struct lpfc_sli *psli = &phba->sli;
1542
James Smartf4b4c682009-05-22 14:53:12 -04001543 /* If the pci channel is offline, ignore possible errors,
1544 * since we cannot communicate with the pci card anyway.
1545 */
1546 if (pci_channel_offline(phba->pcidev)) {
1547 spin_lock_irq(&phba->hbalock);
1548 phba->hba_flag &= ~DEFER_ERATT;
1549 spin_unlock_irq(&phba->hbalock);
1550 return;
1551 }
1552
James Smarta257bf92009-04-06 18:48:10 -04001553 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1554 "0479 Deferred Adapter Hardware Error "
1555 "Data: x%x x%x x%x\n",
1556 phba->work_hs,
1557 phba->work_status[0], phba->work_status[1]);
1558
1559 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001560 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smarta257bf92009-04-06 18:48:10 -04001561 spin_unlock_irq(&phba->hbalock);
1562
1563
1564 /*
1565 * Firmware stops when it triggred erratt. That could cause the I/Os
1566 * dropped by the firmware. Error iocb (I/O) on txcmplq and let the
1567 * SCSI layer retry it after re-establishing link.
1568 */
James Smartdb55fba2014-04-04 13:52:02 -04001569 lpfc_sli_abort_fcp_rings(phba);
James Smarta257bf92009-04-06 18:48:10 -04001570
1571 /*
1572 * There was a firmware error. Take the hba offline and then
1573 * attempt to restart it.
1574 */
James Smart618a5232012-06-12 13:54:36 -04001575 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smarta257bf92009-04-06 18:48:10 -04001576 lpfc_offline(phba);
1577
1578 /* Wait for the ER1 bit to clear.*/
1579 while (phba->work_hs & HS_FFER1) {
1580 msleep(100);
James Smart9940b972011-03-11 16:06:12 -05001581 if (lpfc_readl(phba->HSregaddr, &phba->work_hs)) {
1582 phba->work_hs = UNPLUG_ERR ;
1583 break;
1584 }
James Smarta257bf92009-04-06 18:48:10 -04001585 /* If driver is unloading let the worker thread continue */
1586 if (phba->pport->load_flag & FC_UNLOADING) {
1587 phba->work_hs = 0;
1588 break;
1589 }
1590 }
1591
1592 /*
1593 * This is to ptrotect against a race condition in which
1594 * first write to the host attention register clear the
1595 * host status register.
1596 */
1597 if ((!phba->work_hs) && (!(phba->pport->load_flag & FC_UNLOADING)))
1598 phba->work_hs = old_host_status & ~HS_FFER1;
1599
James Smart3772a992009-05-22 14:50:54 -04001600 spin_lock_irq(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -04001601 phba->hba_flag &= ~DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -04001602 spin_unlock_irq(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -04001603 phba->work_status[0] = readl(phba->MBslimaddr + 0xa8);
1604 phba->work_status[1] = readl(phba->MBslimaddr + 0xac);
1605}
1606
James Smart3772a992009-05-22 14:50:54 -04001607static void
1608lpfc_board_errevt_to_mgmt(struct lpfc_hba *phba)
1609{
1610 struct lpfc_board_event_header board_event;
1611 struct Scsi_Host *shost;
1612
1613 board_event.event_type = FC_REG_BOARD_EVENT;
1614 board_event.subcategory = LPFC_EVENT_PORTINTERR;
1615 shost = lpfc_shost_from_vport(phba->pport);
1616 fc_host_post_vendor_event(shost, fc_get_event_number(),
1617 sizeof(board_event),
1618 (char *) &board_event,
1619 LPFC_NL_VENDOR_ID);
1620}
1621
James Smarta257bf92009-04-06 18:48:10 -04001622/**
James Smart3772a992009-05-22 14:50:54 -04001623 * lpfc_handle_eratt_s3 - The SLI3 HBA hardware error handler
James Smarte59058c2008-08-24 21:49:00 -04001624 * @phba: pointer to lpfc hba data structure.
1625 *
1626 * This routine is invoked to handle the following HBA hardware error
1627 * conditions:
1628 * 1 - HBA error attention interrupt
1629 * 2 - DMA ring index out of range
1630 * 3 - Mailbox command came back as unknown
1631 **/
James Smart3772a992009-05-22 14:50:54 -04001632static void
1633lpfc_handle_eratt_s3(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001634{
James Smart2e0fef82007-06-17 19:56:36 -05001635 struct lpfc_vport *vport = phba->pport;
James Smart2e0fef82007-06-17 19:56:36 -05001636 struct lpfc_sli *psli = &phba->sli;
James Smartd2873e42006-08-18 17:46:43 -04001637 uint32_t event_data;
James Smart57127f12007-10-27 13:37:05 -04001638 unsigned long temperature;
1639 struct temp_event temp_event_data;
James Smart92d7f7b2007-06-17 19:56:38 -05001640 struct Scsi_Host *shost;
James Smart2e0fef82007-06-17 19:56:36 -05001641
Linas Vepstas8d63f372007-02-14 14:28:36 -06001642 /* If the pci channel is offline, ignore possible errors,
James Smart3772a992009-05-22 14:50:54 -04001643 * since we cannot communicate with the pci card anyway.
1644 */
1645 if (pci_channel_offline(phba->pcidev)) {
1646 spin_lock_irq(&phba->hbalock);
1647 phba->hba_flag &= ~DEFER_ERATT;
1648 spin_unlock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -06001649 return;
James Smart3772a992009-05-22 14:50:54 -04001650 }
1651
James Smart13815c82008-01-11 01:52:48 -05001652 /* If resets are disabled then leave the HBA alone and return */
1653 if (!phba->cfg_enable_hba_reset)
1654 return;
dea31012005-04-17 16:05:31 -05001655
James Smartea2151b2008-09-07 11:52:10 -04001656 /* Send an internal error event to mgmt application */
James Smart3772a992009-05-22 14:50:54 -04001657 lpfc_board_errevt_to_mgmt(phba);
James Smartea2151b2008-09-07 11:52:10 -04001658
James Smarta257bf92009-04-06 18:48:10 -04001659 if (phba->hba_flag & DEFER_ERATT)
1660 lpfc_handle_deferred_eratt(phba);
1661
James Smartdcf2a4e2010-09-29 11:18:53 -04001662 if ((phba->work_hs & HS_FFER6) || (phba->work_hs & HS_FFER8)) {
1663 if (phba->work_hs & HS_FFER6)
1664 /* Re-establishing Link */
1665 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1666 "1301 Re-establishing Link "
1667 "Data: x%x x%x x%x\n",
1668 phba->work_hs, phba->work_status[0],
1669 phba->work_status[1]);
1670 if (phba->work_hs & HS_FFER8)
1671 /* Device Zeroization */
1672 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1673 "2861 Host Authentication device "
1674 "zeroization Data:x%x x%x x%x\n",
1675 phba->work_hs, phba->work_status[0],
1676 phba->work_status[1]);
James Smart58da1ff2008-04-07 10:15:56 -04001677
James Smart92d7f7b2007-06-17 19:56:38 -05001678 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001679 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -05001680 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001681
1682 /*
1683 * Firmware stops when it triggled erratt with HS_FFER6.
1684 * That could cause the I/Os dropped by the firmware.
1685 * Error iocb (I/O) on txcmplq and let the SCSI layer
1686 * retry it after re-establishing link.
1687 */
James Smartdb55fba2014-04-04 13:52:02 -04001688 lpfc_sli_abort_fcp_rings(phba);
dea31012005-04-17 16:05:31 -05001689
dea31012005-04-17 16:05:31 -05001690 /*
1691 * There was a firmware error. Take the hba offline and then
1692 * attempt to restart it.
1693 */
James Smart618a5232012-06-12 13:54:36 -04001694 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
dea31012005-04-17 16:05:31 -05001695 lpfc_offline(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -05001696 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05001697 if (lpfc_online(phba) == 0) { /* Initialize the HBA */
James Smart46fa3112007-04-25 09:51:45 -04001698 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05001699 return;
1700 }
James Smart46fa3112007-04-25 09:51:45 -04001701 lpfc_unblock_mgmt_io(phba);
James Smart57127f12007-10-27 13:37:05 -04001702 } else if (phba->work_hs & HS_CRIT_TEMP) {
1703 temperature = readl(phba->MBslimaddr + TEMPERATURE_OFFSET);
1704 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
1705 temp_event_data.event_code = LPFC_CRIT_TEMP;
1706 temp_event_data.data = (uint32_t)temperature;
1707
1708 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -04001709 "0406 Adapter maximum temperature exceeded "
James Smart57127f12007-10-27 13:37:05 -04001710 "(%ld), taking this port offline "
1711 "Data: x%x x%x x%x\n",
1712 temperature, phba->work_hs,
1713 phba->work_status[0], phba->work_status[1]);
1714
1715 shost = lpfc_shost_from_vport(phba->pport);
1716 fc_host_post_vendor_event(shost, fc_get_event_number(),
1717 sizeof(temp_event_data),
1718 (char *) &temp_event_data,
1719 SCSI_NL_VID_TYPE_PCI
1720 | PCI_VENDOR_ID_EMULEX);
1721
James Smart7af67052007-10-27 13:38:11 -04001722 spin_lock_irq(&phba->hbalock);
James Smart7af67052007-10-27 13:38:11 -04001723 phba->over_temp_state = HBA_OVER_TEMP;
1724 spin_unlock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001725 lpfc_offline_eratt(phba);
James Smart57127f12007-10-27 13:37:05 -04001726
dea31012005-04-17 16:05:31 -05001727 } else {
1728 /* The if clause above forces this code path when the status
James Smart93996272008-08-24 21:50:30 -04001729 * failure is a value other than FFER6. Do not call the offline
1730 * twice. This is the adapter hardware error path.
dea31012005-04-17 16:05:31 -05001731 */
1732 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001733 "0457 Adapter Hardware Error "
dea31012005-04-17 16:05:31 -05001734 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001735 phba->work_hs,
dea31012005-04-17 16:05:31 -05001736 phba->work_status[0], phba->work_status[1]);
1737
James Smartd2873e42006-08-18 17:46:43 -04001738 event_data = FC_REG_DUMP_EVENT;
James Smart92d7f7b2007-06-17 19:56:38 -05001739 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05001740 fc_host_post_vendor_event(shost, fc_get_event_number(),
James Smartd2873e42006-08-18 17:46:43 -04001741 sizeof(event_data), (char *) &event_data,
1742 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
1743
James Smart09372822008-01-11 01:52:54 -05001744 lpfc_offline_eratt(phba);
dea31012005-04-17 16:05:31 -05001745 }
James Smart93996272008-08-24 21:50:30 -04001746 return;
dea31012005-04-17 16:05:31 -05001747}
1748
James Smarte59058c2008-08-24 21:49:00 -04001749/**
James Smart618a5232012-06-12 13:54:36 -04001750 * lpfc_sli4_port_sta_fn_reset - The SLI4 function reset due to port status reg
1751 * @phba: pointer to lpfc hba data structure.
1752 * @mbx_action: flag for mailbox shutdown action.
1753 *
1754 * This routine is invoked to perform an SLI4 port PCI function reset in
1755 * response to port status register polling attention. It waits for port
1756 * status register (ERR, RDY, RN) bits before proceeding with function reset.
1757 * During this process, interrupt vectors are freed and later requested
1758 * for handling possible port resource change.
1759 **/
1760static int
James Smarte10b2022014-02-20 09:57:43 -05001761lpfc_sli4_port_sta_fn_reset(struct lpfc_hba *phba, int mbx_action,
1762 bool en_rn_msg)
James Smart618a5232012-06-12 13:54:36 -04001763{
1764 int rc;
1765 uint32_t intr_mode;
1766
James Smart27d6ac02018-02-22 08:18:42 -08001767 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) >=
James Smart65791f12016-07-06 12:35:56 -07001768 LPFC_SLI_INTF_IF_TYPE_2) {
1769 /*
1770 * On error status condition, driver need to wait for port
1771 * ready before performing reset.
1772 */
1773 rc = lpfc_sli4_pdev_status_reg_wait(phba);
James Smart0e916ee2016-07-06 12:36:06 -07001774 if (rc)
James Smart65791f12016-07-06 12:35:56 -07001775 return rc;
James Smart618a5232012-06-12 13:54:36 -04001776 }
James Smart0e916ee2016-07-06 12:36:06 -07001777
James Smart65791f12016-07-06 12:35:56 -07001778 /* need reset: attempt for port recovery */
1779 if (en_rn_msg)
1780 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1781 "2887 Reset Needed: Attempting Port "
1782 "Recovery...\n");
1783 lpfc_offline_prep(phba, mbx_action);
James Smartc00f62e2019-08-14 16:57:11 -07001784 lpfc_sli_flush_io_rings(phba);
James Smart65791f12016-07-06 12:35:56 -07001785 lpfc_offline(phba);
1786 /* release interrupt for possible resource change */
1787 lpfc_sli4_disable_intr(phba);
James Smart5a9eeff2018-11-29 16:09:32 -08001788 rc = lpfc_sli_brdrestart(phba);
1789 if (rc) {
1790 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1791 "6309 Failed to restart board\n");
1792 return rc;
1793 }
James Smart65791f12016-07-06 12:35:56 -07001794 /* request and enable interrupt */
1795 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
1796 if (intr_mode == LPFC_INTR_ERROR) {
1797 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1798 "3175 Failed to enable interrupt\n");
1799 return -EIO;
1800 }
1801 phba->intr_mode = intr_mode;
1802 rc = lpfc_online(phba);
1803 if (rc == 0)
1804 lpfc_unblock_mgmt_io(phba);
1805
James Smart618a5232012-06-12 13:54:36 -04001806 return rc;
1807}
1808
1809/**
James Smartda0436e2009-05-22 14:51:39 -04001810 * lpfc_handle_eratt_s4 - The SLI4 HBA hardware error handler
1811 * @phba: pointer to lpfc hba data structure.
1812 *
1813 * This routine is invoked to handle the SLI4 HBA hardware error attention
1814 * conditions.
1815 **/
1816static void
1817lpfc_handle_eratt_s4(struct lpfc_hba *phba)
1818{
1819 struct lpfc_vport *vport = phba->pport;
1820 uint32_t event_data;
1821 struct Scsi_Host *shost;
James Smart2fcee4b2010-12-15 17:57:46 -05001822 uint32_t if_type;
James Smart2e90f4b2011-12-13 13:22:37 -05001823 struct lpfc_register portstat_reg = {0};
1824 uint32_t reg_err1, reg_err2;
1825 uint32_t uerrlo_reg, uemasklo_reg;
James Smart65791f12016-07-06 12:35:56 -07001826 uint32_t smphr_port_status = 0, pci_rd_rc1, pci_rd_rc2;
James Smarte10b2022014-02-20 09:57:43 -05001827 bool en_rn_msg = true;
James Smart946727d2015-04-07 15:07:09 -04001828 struct temp_event temp_event_data;
James Smart65791f12016-07-06 12:35:56 -07001829 struct lpfc_register portsmphr_reg;
1830 int rc, i;
James Smartda0436e2009-05-22 14:51:39 -04001831
1832 /* If the pci channel is offline, ignore possible errors, since
1833 * we cannot communicate with the pci card anyway.
1834 */
James Smart32a93102019-03-12 16:30:13 -07001835 if (pci_channel_offline(phba->pcidev)) {
1836 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1837 "3166 pci channel is offline\n");
1838 lpfc_sli4_offline_eratt(phba);
James Smartda0436e2009-05-22 14:51:39 -04001839 return;
James Smart32a93102019-03-12 16:30:13 -07001840 }
James Smartda0436e2009-05-22 14:51:39 -04001841
James Smart65791f12016-07-06 12:35:56 -07001842 memset(&portsmphr_reg, 0, sizeof(portsmphr_reg));
James Smart2fcee4b2010-12-15 17:57:46 -05001843 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
1844 switch (if_type) {
1845 case LPFC_SLI_INTF_IF_TYPE_0:
James Smart2e90f4b2011-12-13 13:22:37 -05001846 pci_rd_rc1 = lpfc_readl(
1847 phba->sli4_hba.u.if_type0.UERRLOregaddr,
1848 &uerrlo_reg);
1849 pci_rd_rc2 = lpfc_readl(
1850 phba->sli4_hba.u.if_type0.UEMASKLOregaddr,
1851 &uemasklo_reg);
1852 /* consider PCI bus read error as pci_channel_offline */
1853 if (pci_rd_rc1 == -EIO && pci_rd_rc2 == -EIO)
1854 return;
James Smart65791f12016-07-06 12:35:56 -07001855 if (!(phba->hba_flag & HBA_RECOVERABLE_UE)) {
1856 lpfc_sli4_offline_eratt(phba);
1857 return;
1858 }
1859 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1860 "7623 Checking UE recoverable");
1861
1862 for (i = 0; i < phba->sli4_hba.ue_to_sr / 1000; i++) {
1863 if (lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
1864 &portsmphr_reg.word0))
1865 continue;
1866
1867 smphr_port_status = bf_get(lpfc_port_smphr_port_status,
1868 &portsmphr_reg);
1869 if ((smphr_port_status & LPFC_PORT_SEM_MASK) ==
1870 LPFC_PORT_SEM_UE_RECOVERABLE)
1871 break;
1872 /*Sleep for 1Sec, before checking SEMAPHORE */
1873 msleep(1000);
1874 }
1875
1876 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1877 "4827 smphr_port_status x%x : Waited %dSec",
1878 smphr_port_status, i);
1879
1880 /* Recoverable UE, reset the HBA device */
1881 if ((smphr_port_status & LPFC_PORT_SEM_MASK) ==
1882 LPFC_PORT_SEM_UE_RECOVERABLE) {
1883 for (i = 0; i < 20; i++) {
1884 msleep(1000);
1885 if (!lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
1886 &portsmphr_reg.word0) &&
1887 (LPFC_POST_STAGE_PORT_READY ==
1888 bf_get(lpfc_port_smphr_port_status,
1889 &portsmphr_reg))) {
1890 rc = lpfc_sli4_port_sta_fn_reset(phba,
1891 LPFC_MBX_NO_WAIT, en_rn_msg);
1892 if (rc == 0)
1893 return;
1894 lpfc_printf_log(phba,
1895 KERN_ERR, LOG_INIT,
1896 "4215 Failed to recover UE");
1897 break;
1898 }
1899 }
1900 }
1901 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1902 "7624 Firmware not ready: Failing UE recovery,"
1903 " waited %dSec", i);
James Smart8c24a4f2019-08-14 16:56:53 -07001904 phba->link_state = LPFC_HBA_ERROR;
James Smart2fcee4b2010-12-15 17:57:46 -05001905 break;
James Smart946727d2015-04-07 15:07:09 -04001906
James Smart2fcee4b2010-12-15 17:57:46 -05001907 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart27d6ac02018-02-22 08:18:42 -08001908 case LPFC_SLI_INTF_IF_TYPE_6:
James Smart2e90f4b2011-12-13 13:22:37 -05001909 pci_rd_rc1 = lpfc_readl(
1910 phba->sli4_hba.u.if_type2.STATUSregaddr,
1911 &portstat_reg.word0);
1912 /* consider PCI bus read error as pci_channel_offline */
James Smart6b5151f2012-01-18 16:24:06 -05001913 if (pci_rd_rc1 == -EIO) {
1914 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1915 "3151 PCI bus read access failure: x%x\n",
1916 readl(phba->sli4_hba.u.if_type2.STATUSregaddr));
James Smart32a93102019-03-12 16:30:13 -07001917 lpfc_sli4_offline_eratt(phba);
James Smart2e90f4b2011-12-13 13:22:37 -05001918 return;
James Smart6b5151f2012-01-18 16:24:06 -05001919 }
James Smart2e90f4b2011-12-13 13:22:37 -05001920 reg_err1 = readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
1921 reg_err2 = readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
James Smart2fcee4b2010-12-15 17:57:46 -05001922 if (bf_get(lpfc_sliport_status_oti, &portstat_reg)) {
James Smart2fcee4b2010-12-15 17:57:46 -05001923 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1924 "2889 Port Overtemperature event, "
James Smart946727d2015-04-07 15:07:09 -04001925 "taking port offline Data: x%x x%x\n",
1926 reg_err1, reg_err2);
1927
James Smart310429e2016-07-06 12:35:54 -07001928 phba->sfp_alarm |= LPFC_TRANSGRESSION_HIGH_TEMPERATURE;
James Smart946727d2015-04-07 15:07:09 -04001929 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
1930 temp_event_data.event_code = LPFC_CRIT_TEMP;
1931 temp_event_data.data = 0xFFFFFFFF;
1932
1933 shost = lpfc_shost_from_vport(phba->pport);
1934 fc_host_post_vendor_event(shost, fc_get_event_number(),
1935 sizeof(temp_event_data),
1936 (char *)&temp_event_data,
1937 SCSI_NL_VID_TYPE_PCI
1938 | PCI_VENDOR_ID_EMULEX);
1939
James Smart2fcee4b2010-12-15 17:57:46 -05001940 spin_lock_irq(&phba->hbalock);
1941 phba->over_temp_state = HBA_OVER_TEMP;
1942 spin_unlock_irq(&phba->hbalock);
1943 lpfc_sli4_offline_eratt(phba);
James Smart946727d2015-04-07 15:07:09 -04001944 return;
James Smart2fcee4b2010-12-15 17:57:46 -05001945 }
James Smart2e90f4b2011-12-13 13:22:37 -05001946 if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
James Smarte10b2022014-02-20 09:57:43 -05001947 reg_err2 == SLIPORT_ERR2_REG_FW_RESTART) {
James Smart2e90f4b2011-12-13 13:22:37 -05001948 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte10b2022014-02-20 09:57:43 -05001949 "3143 Port Down: Firmware Update "
1950 "Detected\n");
1951 en_rn_msg = false;
1952 } else if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
James Smart2e90f4b2011-12-13 13:22:37 -05001953 reg_err2 == SLIPORT_ERR2_REG_FORCED_DUMP)
1954 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1955 "3144 Port Down: Debug Dump\n");
1956 else if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1957 reg_err2 == SLIPORT_ERR2_REG_FUNC_PROVISON)
1958 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1959 "3145 Port Down: Provisioning\n");
James Smart618a5232012-06-12 13:54:36 -04001960
James Smart946727d2015-04-07 15:07:09 -04001961 /* If resets are disabled then leave the HBA alone and return */
1962 if (!phba->cfg_enable_hba_reset)
1963 return;
1964
James Smart618a5232012-06-12 13:54:36 -04001965 /* Check port status register for function reset */
James Smarte10b2022014-02-20 09:57:43 -05001966 rc = lpfc_sli4_port_sta_fn_reset(phba, LPFC_MBX_NO_WAIT,
1967 en_rn_msg);
James Smart618a5232012-06-12 13:54:36 -04001968 if (rc == 0) {
1969 /* don't report event on forced debug dump */
1970 if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1971 reg_err2 == SLIPORT_ERR2_REG_FORCED_DUMP)
1972 return;
1973 else
1974 break;
James Smart2fcee4b2010-12-15 17:57:46 -05001975 }
James Smart618a5232012-06-12 13:54:36 -04001976 /* fall through for not able to recover */
James Smart6b5151f2012-01-18 16:24:06 -05001977 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart8c24a4f2019-08-14 16:56:53 -07001978 "3152 Unrecoverable error\n");
1979 phba->link_state = LPFC_HBA_ERROR;
James Smart2fcee4b2010-12-15 17:57:46 -05001980 break;
1981 case LPFC_SLI_INTF_IF_TYPE_1:
1982 default:
1983 break;
1984 }
James Smart2e90f4b2011-12-13 13:22:37 -05001985 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
1986 "3123 Report dump event to upper layer\n");
1987 /* Send an internal error event to mgmt application */
1988 lpfc_board_errevt_to_mgmt(phba);
1989
1990 event_data = FC_REG_DUMP_EVENT;
1991 shost = lpfc_shost_from_vport(vport);
1992 fc_host_post_vendor_event(shost, fc_get_event_number(),
1993 sizeof(event_data), (char *) &event_data,
1994 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
James Smartda0436e2009-05-22 14:51:39 -04001995}
1996
1997/**
1998 * lpfc_handle_eratt - Wrapper func for handling hba error attention
1999 * @phba: pointer to lpfc HBA data structure.
2000 *
2001 * This routine wraps the actual SLI3 or SLI4 hba error attention handling
2002 * routine from the API jump table function pointer from the lpfc_hba struct.
2003 *
2004 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002005 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -04002006 * Any other value - error.
2007 **/
2008void
2009lpfc_handle_eratt(struct lpfc_hba *phba)
2010{
2011 (*phba->lpfc_handle_eratt)(phba);
2012}
2013
2014/**
James Smart3621a712009-04-06 18:47:14 -04002015 * lpfc_handle_latt - The HBA link event handler
James Smarte59058c2008-08-24 21:49:00 -04002016 * @phba: pointer to lpfc hba data structure.
2017 *
2018 * This routine is invoked from the worker thread to handle a HBA host
James Smart895427b2017-02-12 13:52:30 -08002019 * attention link event. SLI3 only.
James Smarte59058c2008-08-24 21:49:00 -04002020 **/
dea31012005-04-17 16:05:31 -05002021void
James Smart2e0fef82007-06-17 19:56:36 -05002022lpfc_handle_latt(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002023{
James Smart2e0fef82007-06-17 19:56:36 -05002024 struct lpfc_vport *vport = phba->pport;
2025 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05002026 LPFC_MBOXQ_t *pmb;
2027 volatile uint32_t control;
2028 struct lpfc_dmabuf *mp;
James Smart09372822008-01-11 01:52:54 -05002029 int rc = 0;
dea31012005-04-17 16:05:31 -05002030
2031 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart09372822008-01-11 01:52:54 -05002032 if (!pmb) {
2033 rc = 1;
dea31012005-04-17 16:05:31 -05002034 goto lpfc_handle_latt_err_exit;
James Smart09372822008-01-11 01:52:54 -05002035 }
dea31012005-04-17 16:05:31 -05002036
2037 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
James Smart09372822008-01-11 01:52:54 -05002038 if (!mp) {
2039 rc = 2;
dea31012005-04-17 16:05:31 -05002040 goto lpfc_handle_latt_free_pmb;
James Smart09372822008-01-11 01:52:54 -05002041 }
dea31012005-04-17 16:05:31 -05002042
2043 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
James Smart09372822008-01-11 01:52:54 -05002044 if (!mp->virt) {
2045 rc = 3;
dea31012005-04-17 16:05:31 -05002046 goto lpfc_handle_latt_free_mp;
James Smart09372822008-01-11 01:52:54 -05002047 }
dea31012005-04-17 16:05:31 -05002048
James.Smart@Emulex.Com6281bfe2005-11-28 11:41:33 -05002049 /* Cleanup any outstanding ELS commands */
James Smart549e55c2007-08-02 11:09:51 -04002050 lpfc_els_flush_all_cmd(phba);
dea31012005-04-17 16:05:31 -05002051
2052 psli->slistat.link_event++;
James Smart76a95d72010-11-20 23:11:48 -05002053 lpfc_read_topology(phba, pmb, mp);
2054 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
James Smart2e0fef82007-06-17 19:56:36 -05002055 pmb->vport = vport;
James Smart0d2b6b82008-06-14 22:52:47 -04002056 /* Block ELS IOCBs until we have processed this mbox command */
James Smart895427b2017-02-12 13:52:30 -08002057 phba->sli.sli3_ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
James Smart0b727fe2007-10-27 13:37:25 -04002058 rc = lpfc_sli_issue_mbox (phba, pmb, MBX_NOWAIT);
James Smart09372822008-01-11 01:52:54 -05002059 if (rc == MBX_NOT_FINISHED) {
2060 rc = 4;
James Smart14691152006-12-02 13:34:28 -05002061 goto lpfc_handle_latt_free_mbuf;
James Smart09372822008-01-11 01:52:54 -05002062 }
dea31012005-04-17 16:05:31 -05002063
2064 /* Clear Link Attention in HA REG */
James Smart2e0fef82007-06-17 19:56:36 -05002065 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002066 writel(HA_LATT, phba->HAregaddr);
2067 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05002068 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002069
2070 return;
2071
James Smart14691152006-12-02 13:34:28 -05002072lpfc_handle_latt_free_mbuf:
James Smart895427b2017-02-12 13:52:30 -08002073 phba->sli.sli3_ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart14691152006-12-02 13:34:28 -05002074 lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea31012005-04-17 16:05:31 -05002075lpfc_handle_latt_free_mp:
2076 kfree(mp);
2077lpfc_handle_latt_free_pmb:
James Smart1dcb58e2007-04-25 09:51:30 -04002078 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002079lpfc_handle_latt_err_exit:
2080 /* Enable Link attention interrupts */
James Smart2e0fef82007-06-17 19:56:36 -05002081 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002082 psli->sli_flag |= LPFC_PROCESS_LA;
2083 control = readl(phba->HCregaddr);
2084 control |= HC_LAINT_ENA;
2085 writel(control, phba->HCregaddr);
2086 readl(phba->HCregaddr); /* flush */
2087
2088 /* Clear Link Attention in HA REG */
2089 writel(HA_LATT, phba->HAregaddr);
2090 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05002091 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002092 lpfc_linkdown(phba);
James Smart2e0fef82007-06-17 19:56:36 -05002093 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002094
James Smart09372822008-01-11 01:52:54 -05002095 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
2096 "0300 LATT: Cannot issue READ_LA: Data:%d\n", rc);
dea31012005-04-17 16:05:31 -05002097
2098 return;
2099}
2100
James Smarte59058c2008-08-24 21:49:00 -04002101/**
James Smart3621a712009-04-06 18:47:14 -04002102 * lpfc_parse_vpd - Parse VPD (Vital Product Data)
James Smarte59058c2008-08-24 21:49:00 -04002103 * @phba: pointer to lpfc hba data structure.
2104 * @vpd: pointer to the vital product data.
2105 * @len: length of the vital product data in bytes.
2106 *
2107 * This routine parses the Vital Product Data (VPD). The VPD is treated as
2108 * an array of characters. In this routine, the ModelName, ProgramType, and
2109 * ModelDesc, etc. fields of the phba data structure will be populated.
2110 *
2111 * Return codes
2112 * 0 - pointer to the VPD passed in is NULL
2113 * 1 - success
2114 **/
James Smart3772a992009-05-22 14:50:54 -04002115int
James Smart2e0fef82007-06-17 19:56:36 -05002116lpfc_parse_vpd(struct lpfc_hba *phba, uint8_t *vpd, int len)
dea31012005-04-17 16:05:31 -05002117{
2118 uint8_t lenlo, lenhi;
Anton Blanchard07da60c2007-03-21 08:41:47 -05002119 int Length;
dea31012005-04-17 16:05:31 -05002120 int i, j;
2121 int finished = 0;
2122 int index = 0;
2123
2124 if (!vpd)
2125 return 0;
2126
2127 /* Vital Product */
James Smarted957682007-06-17 19:56:37 -05002128 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002129 "0455 Vital Product Data: x%x x%x x%x x%x\n",
dea31012005-04-17 16:05:31 -05002130 (uint32_t) vpd[0], (uint32_t) vpd[1], (uint32_t) vpd[2],
2131 (uint32_t) vpd[3]);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002132 while (!finished && (index < (len - 4))) {
dea31012005-04-17 16:05:31 -05002133 switch (vpd[index]) {
2134 case 0x82:
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002135 case 0x91:
dea31012005-04-17 16:05:31 -05002136 index += 1;
2137 lenlo = vpd[index];
2138 index += 1;
2139 lenhi = vpd[index];
2140 index += 1;
2141 i = ((((unsigned short)lenhi) << 8) + lenlo);
2142 index += i;
2143 break;
2144 case 0x90:
2145 index += 1;
2146 lenlo = vpd[index];
2147 index += 1;
2148 lenhi = vpd[index];
2149 index += 1;
2150 Length = ((((unsigned short)lenhi) << 8) + lenlo);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002151 if (Length > len - index)
2152 Length = len - index;
dea31012005-04-17 16:05:31 -05002153 while (Length > 0) {
2154 /* Look for Serial Number */
2155 if ((vpd[index] == 'S') && (vpd[index+1] == 'N')) {
2156 index += 2;
2157 i = vpd[index];
2158 index += 1;
2159 j = 0;
2160 Length -= (3+i);
2161 while(i--) {
2162 phba->SerialNumber[j++] = vpd[index++];
2163 if (j == 31)
2164 break;
2165 }
2166 phba->SerialNumber[j] = 0;
2167 continue;
2168 }
2169 else if ((vpd[index] == 'V') && (vpd[index+1] == '1')) {
2170 phba->vpd_flag |= VPD_MODEL_DESC;
2171 index += 2;
2172 i = vpd[index];
2173 index += 1;
2174 j = 0;
2175 Length -= (3+i);
2176 while(i--) {
2177 phba->ModelDesc[j++] = vpd[index++];
2178 if (j == 255)
2179 break;
2180 }
2181 phba->ModelDesc[j] = 0;
2182 continue;
2183 }
2184 else if ((vpd[index] == 'V') && (vpd[index+1] == '2')) {
2185 phba->vpd_flag |= VPD_MODEL_NAME;
2186 index += 2;
2187 i = vpd[index];
2188 index += 1;
2189 j = 0;
2190 Length -= (3+i);
2191 while(i--) {
2192 phba->ModelName[j++] = vpd[index++];
2193 if (j == 79)
2194 break;
2195 }
2196 phba->ModelName[j] = 0;
2197 continue;
2198 }
2199 else if ((vpd[index] == 'V') && (vpd[index+1] == '3')) {
2200 phba->vpd_flag |= VPD_PROGRAM_TYPE;
2201 index += 2;
2202 i = vpd[index];
2203 index += 1;
2204 j = 0;
2205 Length -= (3+i);
2206 while(i--) {
2207 phba->ProgramType[j++] = vpd[index++];
2208 if (j == 255)
2209 break;
2210 }
2211 phba->ProgramType[j] = 0;
2212 continue;
2213 }
2214 else if ((vpd[index] == 'V') && (vpd[index+1] == '4')) {
2215 phba->vpd_flag |= VPD_PORT;
2216 index += 2;
2217 i = vpd[index];
2218 index += 1;
2219 j = 0;
2220 Length -= (3+i);
2221 while(i--) {
James Smartcd1c8302011-10-10 21:33:25 -04002222 if ((phba->sli_rev == LPFC_SLI_REV4) &&
2223 (phba->sli4_hba.pport_name_sta ==
2224 LPFC_SLI4_PPNAME_GET)) {
2225 j++;
2226 index++;
2227 } else
2228 phba->Port[j++] = vpd[index++];
2229 if (j == 19)
2230 break;
dea31012005-04-17 16:05:31 -05002231 }
James Smartcd1c8302011-10-10 21:33:25 -04002232 if ((phba->sli_rev != LPFC_SLI_REV4) ||
2233 (phba->sli4_hba.pport_name_sta ==
2234 LPFC_SLI4_PPNAME_NON))
2235 phba->Port[j] = 0;
dea31012005-04-17 16:05:31 -05002236 continue;
2237 }
2238 else {
2239 index += 2;
2240 i = vpd[index];
2241 index += 1;
2242 index += i;
2243 Length -= (3 + i);
2244 }
2245 }
2246 finished = 0;
2247 break;
2248 case 0x78:
2249 finished = 1;
2250 break;
2251 default:
2252 index ++;
2253 break;
2254 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002255 }
dea31012005-04-17 16:05:31 -05002256
2257 return(1);
2258}
2259
James Smarte59058c2008-08-24 21:49:00 -04002260/**
James Smart3621a712009-04-06 18:47:14 -04002261 * lpfc_get_hba_model_desc - Retrieve HBA device model name and description
James Smarte59058c2008-08-24 21:49:00 -04002262 * @phba: pointer to lpfc hba data structure.
2263 * @mdp: pointer to the data structure to hold the derived model name.
2264 * @descp: pointer to the data structure to hold the derived description.
2265 *
2266 * This routine retrieves HBA's description based on its registered PCI device
2267 * ID. The @descp passed into this function points to an array of 256 chars. It
2268 * shall be returned with the model name, maximum speed, and the host bus type.
2269 * The @mdp passed into this function points to an array of 80 chars. When the
2270 * function returns, the @mdp will be filled with the model name.
2271 **/
dea31012005-04-17 16:05:31 -05002272static void
James Smart2e0fef82007-06-17 19:56:36 -05002273lpfc_get_hba_model_desc(struct lpfc_hba *phba, uint8_t *mdp, uint8_t *descp)
dea31012005-04-17 16:05:31 -05002274{
2275 lpfc_vpd_t *vp;
James.Smart@Emulex.Comfefcb2b2005-11-28 15:08:56 -05002276 uint16_t dev_id = phba->pcidev->device;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002277 int max_speed;
James Smart84774a42008-08-24 21:50:06 -04002278 int GE = 0;
James Smartda0436e2009-05-22 14:51:39 -04002279 int oneConnect = 0; /* default is not a oneConnect */
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002280 struct {
James Smarta747c9c2009-11-18 15:41:10 -05002281 char *name;
2282 char *bus;
2283 char *function;
2284 } m = {"<Unknown>", "", ""};
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002285
2286 if (mdp && mdp[0] != '\0'
2287 && descp && descp[0] != '\0')
2288 return;
2289
James Smartfbd8a6b2018-02-22 08:18:45 -08002290 if (phba->lmt & LMT_64Gb)
2291 max_speed = 64;
2292 else if (phba->lmt & LMT_32Gb)
James Smartd38dd522015-08-31 16:48:17 -04002293 max_speed = 32;
2294 else if (phba->lmt & LMT_16Gb)
James Smartc0c11512011-05-24 11:41:34 -04002295 max_speed = 16;
2296 else if (phba->lmt & LMT_10Gb)
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002297 max_speed = 10;
2298 else if (phba->lmt & LMT_8Gb)
2299 max_speed = 8;
2300 else if (phba->lmt & LMT_4Gb)
2301 max_speed = 4;
2302 else if (phba->lmt & LMT_2Gb)
2303 max_speed = 2;
James Smart4169d862012-09-29 11:32:09 -04002304 else if (phba->lmt & LMT_1Gb)
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002305 max_speed = 1;
James Smart4169d862012-09-29 11:32:09 -04002306 else
2307 max_speed = 0;
dea31012005-04-17 16:05:31 -05002308
2309 vp = &phba->vpd;
dea31012005-04-17 16:05:31 -05002310
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002311 switch (dev_id) {
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04002312 case PCI_DEVICE_ID_FIREFLY:
James Smart12222f42014-05-21 08:05:19 -04002313 m = (typeof(m)){"LP6000", "PCI",
2314 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04002315 break;
dea31012005-04-17 16:05:31 -05002316 case PCI_DEVICE_ID_SUPERFLY:
2317 if (vp->rev.biuRev >= 1 && vp->rev.biuRev <= 3)
James Smart12222f42014-05-21 08:05:19 -04002318 m = (typeof(m)){"LP7000", "PCI", ""};
dea31012005-04-17 16:05:31 -05002319 else
James Smart12222f42014-05-21 08:05:19 -04002320 m = (typeof(m)){"LP7000E", "PCI", ""};
2321 m.function = "Obsolete, Unsupported Fibre Channel Adapter";
dea31012005-04-17 16:05:31 -05002322 break;
2323 case PCI_DEVICE_ID_DRAGONFLY:
James Smarta747c9c2009-11-18 15:41:10 -05002324 m = (typeof(m)){"LP8000", "PCI",
James Smart12222f42014-05-21 08:05:19 -04002325 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002326 break;
2327 case PCI_DEVICE_ID_CENTAUR:
2328 if (FC_JEDEC_ID(vp->rev.biuRev) == CENTAUR_2G_JEDEC_ID)
James Smart12222f42014-05-21 08:05:19 -04002329 m = (typeof(m)){"LP9002", "PCI", ""};
dea31012005-04-17 16:05:31 -05002330 else
James Smart12222f42014-05-21 08:05:19 -04002331 m = (typeof(m)){"LP9000", "PCI", ""};
2332 m.function = "Obsolete, Unsupported Fibre Channel Adapter";
dea31012005-04-17 16:05:31 -05002333 break;
2334 case PCI_DEVICE_ID_RFLY:
James Smarta747c9c2009-11-18 15:41:10 -05002335 m = (typeof(m)){"LP952", "PCI",
James Smart12222f42014-05-21 08:05:19 -04002336 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002337 break;
2338 case PCI_DEVICE_ID_PEGASUS:
James Smarta747c9c2009-11-18 15:41:10 -05002339 m = (typeof(m)){"LP9802", "PCI-X",
James Smart12222f42014-05-21 08:05:19 -04002340 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002341 break;
2342 case PCI_DEVICE_ID_THOR:
James Smarta747c9c2009-11-18 15:41:10 -05002343 m = (typeof(m)){"LP10000", "PCI-X",
James Smart12222f42014-05-21 08:05:19 -04002344 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002345 break;
2346 case PCI_DEVICE_ID_VIPER:
James Smarta747c9c2009-11-18 15:41:10 -05002347 m = (typeof(m)){"LPX1000", "PCI-X",
James Smart12222f42014-05-21 08:05:19 -04002348 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002349 break;
2350 case PCI_DEVICE_ID_PFLY:
James Smarta747c9c2009-11-18 15:41:10 -05002351 m = (typeof(m)){"LP982", "PCI-X",
James Smart12222f42014-05-21 08:05:19 -04002352 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002353 break;
2354 case PCI_DEVICE_ID_TFLY:
James Smarta747c9c2009-11-18 15:41:10 -05002355 m = (typeof(m)){"LP1050", "PCI-X",
James Smart12222f42014-05-21 08:05:19 -04002356 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002357 break;
2358 case PCI_DEVICE_ID_HELIOS:
James Smarta747c9c2009-11-18 15:41:10 -05002359 m = (typeof(m)){"LP11000", "PCI-X2",
James Smart12222f42014-05-21 08:05:19 -04002360 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002361 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002362 case PCI_DEVICE_ID_HELIOS_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002363 m = (typeof(m)){"LP11000-SP", "PCI-X2",
James Smart12222f42014-05-21 08:05:19 -04002364 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002365 break;
2366 case PCI_DEVICE_ID_HELIOS_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002367 m = (typeof(m)){"LP11002-SP", "PCI-X2",
James Smart12222f42014-05-21 08:05:19 -04002368 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002369 break;
2370 case PCI_DEVICE_ID_NEPTUNE:
James Smart12222f42014-05-21 08:05:19 -04002371 m = (typeof(m)){"LPe1000", "PCIe",
2372 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002373 break;
2374 case PCI_DEVICE_ID_NEPTUNE_SCSP:
James Smart12222f42014-05-21 08:05:19 -04002375 m = (typeof(m)){"LPe1000-SP", "PCIe",
2376 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002377 break;
2378 case PCI_DEVICE_ID_NEPTUNE_DCSP:
James Smart12222f42014-05-21 08:05:19 -04002379 m = (typeof(m)){"LPe1002-SP", "PCIe",
2380 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002381 break;
dea31012005-04-17 16:05:31 -05002382 case PCI_DEVICE_ID_BMID:
James Smarta747c9c2009-11-18 15:41:10 -05002383 m = (typeof(m)){"LP1150", "PCI-X2", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002384 break;
2385 case PCI_DEVICE_ID_BSMB:
James Smart12222f42014-05-21 08:05:19 -04002386 m = (typeof(m)){"LP111", "PCI-X2",
2387 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002388 break;
2389 case PCI_DEVICE_ID_ZEPHYR:
James Smarta747c9c2009-11-18 15:41:10 -05002390 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002391 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002392 case PCI_DEVICE_ID_ZEPHYR_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002393 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002394 break;
2395 case PCI_DEVICE_ID_ZEPHYR_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002396 m = (typeof(m)){"LP2105", "PCIe", "FCoE Adapter"};
James Smarta257bf92009-04-06 18:48:10 -04002397 GE = 1;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002398 break;
dea31012005-04-17 16:05:31 -05002399 case PCI_DEVICE_ID_ZMID:
James Smarta747c9c2009-11-18 15:41:10 -05002400 m = (typeof(m)){"LPe1150", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002401 break;
2402 case PCI_DEVICE_ID_ZSMB:
James Smarta747c9c2009-11-18 15:41:10 -05002403 m = (typeof(m)){"LPe111", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002404 break;
2405 case PCI_DEVICE_ID_LP101:
James Smart12222f42014-05-21 08:05:19 -04002406 m = (typeof(m)){"LP101", "PCI-X",
2407 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002408 break;
2409 case PCI_DEVICE_ID_LP10000S:
James Smart12222f42014-05-21 08:05:19 -04002410 m = (typeof(m)){"LP10000-S", "PCI",
2411 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04002412 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002413 case PCI_DEVICE_ID_LP11000S:
James Smart12222f42014-05-21 08:05:19 -04002414 m = (typeof(m)){"LP11000-S", "PCI-X2",
2415 "Obsolete, Unsupported Fibre Channel Adapter"};
James Smart18a3b592006-12-02 13:35:08 -05002416 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002417 case PCI_DEVICE_ID_LPE11000S:
James Smart12222f42014-05-21 08:05:19 -04002418 m = (typeof(m)){"LPe11000-S", "PCIe",
2419 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Com5cc36b32005-11-28 11:42:19 -05002420 break;
James Smartb87eab32007-04-25 09:53:28 -04002421 case PCI_DEVICE_ID_SAT:
James Smarta747c9c2009-11-18 15:41:10 -05002422 m = (typeof(m)){"LPe12000", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002423 break;
2424 case PCI_DEVICE_ID_SAT_MID:
James Smarta747c9c2009-11-18 15:41:10 -05002425 m = (typeof(m)){"LPe1250", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002426 break;
2427 case PCI_DEVICE_ID_SAT_SMB:
James Smarta747c9c2009-11-18 15:41:10 -05002428 m = (typeof(m)){"LPe121", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002429 break;
2430 case PCI_DEVICE_ID_SAT_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002431 m = (typeof(m)){"LPe12002-SP", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002432 break;
2433 case PCI_DEVICE_ID_SAT_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002434 m = (typeof(m)){"LPe12000-SP", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002435 break;
2436 case PCI_DEVICE_ID_SAT_S:
James Smarta747c9c2009-11-18 15:41:10 -05002437 m = (typeof(m)){"LPe12000-S", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002438 break;
James Smart84774a42008-08-24 21:50:06 -04002439 case PCI_DEVICE_ID_HORNET:
James Smart12222f42014-05-21 08:05:19 -04002440 m = (typeof(m)){"LP21000", "PCIe",
2441 "Obsolete, Unsupported FCoE Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002442 GE = 1;
2443 break;
2444 case PCI_DEVICE_ID_PROTEUS_VF:
James Smarta747c9c2009-11-18 15:41:10 -05002445 m = (typeof(m)){"LPev12000", "PCIe IOV",
James Smart12222f42014-05-21 08:05:19 -04002446 "Obsolete, Unsupported Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002447 break;
2448 case PCI_DEVICE_ID_PROTEUS_PF:
James Smarta747c9c2009-11-18 15:41:10 -05002449 m = (typeof(m)){"LPev12000", "PCIe IOV",
James Smart12222f42014-05-21 08:05:19 -04002450 "Obsolete, Unsupported Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002451 break;
2452 case PCI_DEVICE_ID_PROTEUS_S:
James Smarta747c9c2009-11-18 15:41:10 -05002453 m = (typeof(m)){"LPemv12002-S", "PCIe IOV",
James Smart12222f42014-05-21 08:05:19 -04002454 "Obsolete, Unsupported Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002455 break;
James Smartda0436e2009-05-22 14:51:39 -04002456 case PCI_DEVICE_ID_TIGERSHARK:
2457 oneConnect = 1;
James Smarta747c9c2009-11-18 15:41:10 -05002458 m = (typeof(m)){"OCe10100", "PCIe", "FCoE"};
James Smartda0436e2009-05-22 14:51:39 -04002459 break;
James Smarta747c9c2009-11-18 15:41:10 -05002460 case PCI_DEVICE_ID_TOMCAT:
James Smart6669f9b2009-10-02 15:16:45 -04002461 oneConnect = 1;
James Smarta747c9c2009-11-18 15:41:10 -05002462 m = (typeof(m)){"OCe11100", "PCIe", "FCoE"};
2463 break;
2464 case PCI_DEVICE_ID_FALCON:
2465 m = (typeof(m)){"LPSe12002-ML1-E", "PCIe",
2466 "EmulexSecure Fibre"};
James Smart6669f9b2009-10-02 15:16:45 -04002467 break;
James Smart98fc5dd2010-06-07 15:24:29 -04002468 case PCI_DEVICE_ID_BALIUS:
2469 m = (typeof(m)){"LPVe12002", "PCIe Shared I/O",
James Smart12222f42014-05-21 08:05:19 -04002470 "Obsolete, Unsupported Fibre Channel Adapter"};
James Smart98fc5dd2010-06-07 15:24:29 -04002471 break;
James Smart085c6472010-11-20 23:11:37 -05002472 case PCI_DEVICE_ID_LANCER_FC:
James Smartc0c11512011-05-24 11:41:34 -04002473 m = (typeof(m)){"LPe16000", "PCIe", "Fibre Channel Adapter"};
James Smart085c6472010-11-20 23:11:37 -05002474 break;
James Smart12222f42014-05-21 08:05:19 -04002475 case PCI_DEVICE_ID_LANCER_FC_VF:
2476 m = (typeof(m)){"LPe16000", "PCIe",
2477 "Obsolete, Unsupported Fibre Channel Adapter"};
2478 break;
James Smart085c6472010-11-20 23:11:37 -05002479 case PCI_DEVICE_ID_LANCER_FCOE:
2480 oneConnect = 1;
James Smart079b5c92011-08-21 21:48:49 -04002481 m = (typeof(m)){"OCe15100", "PCIe", "FCoE"};
James Smart085c6472010-11-20 23:11:37 -05002482 break;
James Smart12222f42014-05-21 08:05:19 -04002483 case PCI_DEVICE_ID_LANCER_FCOE_VF:
2484 oneConnect = 1;
2485 m = (typeof(m)){"OCe15100", "PCIe",
2486 "Obsolete, Unsupported FCoE"};
2487 break;
James Smartd38dd522015-08-31 16:48:17 -04002488 case PCI_DEVICE_ID_LANCER_G6_FC:
2489 m = (typeof(m)){"LPe32000", "PCIe", "Fibre Channel Adapter"};
2490 break;
James Smartc238b9b2018-02-22 08:18:44 -08002491 case PCI_DEVICE_ID_LANCER_G7_FC:
2492 m = (typeof(m)){"LPe36000", "PCIe", "Fibre Channel Adapter"};
2493 break;
James Smartf8cafd32012-08-03 12:42:51 -04002494 case PCI_DEVICE_ID_SKYHAWK:
2495 case PCI_DEVICE_ID_SKYHAWK_VF:
2496 oneConnect = 1;
2497 m = (typeof(m)){"OCe14000", "PCIe", "FCoE"};
2498 break;
James.Smart@Emulex.Com5cc36b32005-11-28 11:42:19 -05002499 default:
James Smarta747c9c2009-11-18 15:41:10 -05002500 m = (typeof(m)){"Unknown", "", ""};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002501 break;
dea31012005-04-17 16:05:31 -05002502 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002503
2504 if (mdp && mdp[0] == '\0')
2505 snprintf(mdp, 79,"%s", m.name);
James Smartc0c11512011-05-24 11:41:34 -04002506 /*
2507 * oneConnect hba requires special processing, they are all initiators
James Smartda0436e2009-05-22 14:51:39 -04002508 * and we put the port number on the end
2509 */
2510 if (descp && descp[0] == '\0') {
2511 if (oneConnect)
2512 snprintf(descp, 255,
James Smart4169d862012-09-29 11:32:09 -04002513 "Emulex OneConnect %s, %s Initiator %s",
James Smarta747c9c2009-11-18 15:41:10 -05002514 m.name, m.function,
James Smartda0436e2009-05-22 14:51:39 -04002515 phba->Port);
James Smart4169d862012-09-29 11:32:09 -04002516 else if (max_speed == 0)
2517 snprintf(descp, 255,
Sebastian Herbszt290237d2015-08-31 16:48:08 -04002518 "Emulex %s %s %s",
James Smart4169d862012-09-29 11:32:09 -04002519 m.name, m.bus, m.function);
James Smartda0436e2009-05-22 14:51:39 -04002520 else
2521 snprintf(descp, 255,
2522 "Emulex %s %d%s %s %s",
James Smarta747c9c2009-11-18 15:41:10 -05002523 m.name, max_speed, (GE) ? "GE" : "Gb",
2524 m.bus, m.function);
James Smartda0436e2009-05-22 14:51:39 -04002525 }
dea31012005-04-17 16:05:31 -05002526}
2527
James Smarte59058c2008-08-24 21:49:00 -04002528/**
James Smart3621a712009-04-06 18:47:14 -04002529 * lpfc_post_buffer - Post IOCB(s) with DMA buffer descriptor(s) to a IOCB ring
James Smarte59058c2008-08-24 21:49:00 -04002530 * @phba: pointer to lpfc hba data structure.
2531 * @pring: pointer to a IOCB ring.
2532 * @cnt: the number of IOCBs to be posted to the IOCB ring.
2533 *
2534 * This routine posts a given number of IOCBs with the associated DMA buffer
2535 * descriptors specified by the cnt argument to the given IOCB ring.
2536 *
2537 * Return codes
2538 * The number of IOCBs NOT able to be posted to the IOCB ring.
2539 **/
dea31012005-04-17 16:05:31 -05002540int
James Smart495a7142008-06-14 22:52:59 -04002541lpfc_post_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring, int cnt)
dea31012005-04-17 16:05:31 -05002542{
2543 IOCB_t *icmd;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002544 struct lpfc_iocbq *iocb;
dea31012005-04-17 16:05:31 -05002545 struct lpfc_dmabuf *mp1, *mp2;
2546
2547 cnt += pring->missbufcnt;
2548
2549 /* While there are buffers to post */
2550 while (cnt > 0) {
2551 /* Allocate buffer for command iocb */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002552 iocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05002553 if (iocb == NULL) {
2554 pring->missbufcnt = cnt;
2555 return cnt;
2556 }
dea31012005-04-17 16:05:31 -05002557 icmd = &iocb->iocb;
2558
2559 /* 2 buffers can be posted per command */
2560 /* Allocate buffer to post */
2561 mp1 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
2562 if (mp1)
James Smart98c9ea52007-10-27 13:37:33 -04002563 mp1->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &mp1->phys);
2564 if (!mp1 || !mp1->virt) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002565 kfree(mp1);
James Bottomley604a3e32005-10-29 10:28:33 -05002566 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002567 pring->missbufcnt = cnt;
2568 return cnt;
2569 }
2570
2571 INIT_LIST_HEAD(&mp1->list);
2572 /* Allocate buffer to post */
2573 if (cnt > 1) {
2574 mp2 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
2575 if (mp2)
2576 mp2->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
2577 &mp2->phys);
James Smart98c9ea52007-10-27 13:37:33 -04002578 if (!mp2 || !mp2->virt) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002579 kfree(mp2);
dea31012005-04-17 16:05:31 -05002580 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
2581 kfree(mp1);
James Bottomley604a3e32005-10-29 10:28:33 -05002582 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002583 pring->missbufcnt = cnt;
2584 return cnt;
2585 }
2586
2587 INIT_LIST_HEAD(&mp2->list);
2588 } else {
2589 mp2 = NULL;
2590 }
2591
2592 icmd->un.cont64[0].addrHigh = putPaddrHigh(mp1->phys);
2593 icmd->un.cont64[0].addrLow = putPaddrLow(mp1->phys);
2594 icmd->un.cont64[0].tus.f.bdeSize = FCELSSIZE;
2595 icmd->ulpBdeCount = 1;
2596 cnt--;
2597 if (mp2) {
2598 icmd->un.cont64[1].addrHigh = putPaddrHigh(mp2->phys);
2599 icmd->un.cont64[1].addrLow = putPaddrLow(mp2->phys);
2600 icmd->un.cont64[1].tus.f.bdeSize = FCELSSIZE;
2601 cnt--;
2602 icmd->ulpBdeCount = 2;
2603 }
2604
2605 icmd->ulpCommand = CMD_QUE_RING_BUF64_CN;
2606 icmd->ulpLe = 1;
2607
James Smart3772a992009-05-22 14:50:54 -04002608 if (lpfc_sli_issue_iocb(phba, pring->ringno, iocb, 0) ==
2609 IOCB_ERROR) {
dea31012005-04-17 16:05:31 -05002610 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
2611 kfree(mp1);
2612 cnt++;
2613 if (mp2) {
2614 lpfc_mbuf_free(phba, mp2->virt, mp2->phys);
2615 kfree(mp2);
2616 cnt++;
2617 }
James Bottomley604a3e32005-10-29 10:28:33 -05002618 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002619 pring->missbufcnt = cnt;
dea31012005-04-17 16:05:31 -05002620 return cnt;
2621 }
dea31012005-04-17 16:05:31 -05002622 lpfc_sli_ringpostbuf_put(phba, pring, mp1);
James Smart92d7f7b2007-06-17 19:56:38 -05002623 if (mp2)
dea31012005-04-17 16:05:31 -05002624 lpfc_sli_ringpostbuf_put(phba, pring, mp2);
dea31012005-04-17 16:05:31 -05002625 }
2626 pring->missbufcnt = 0;
2627 return 0;
2628}
2629
James Smarte59058c2008-08-24 21:49:00 -04002630/**
James Smart3621a712009-04-06 18:47:14 -04002631 * lpfc_post_rcv_buf - Post the initial receive IOCB buffers to ELS ring
James Smarte59058c2008-08-24 21:49:00 -04002632 * @phba: pointer to lpfc hba data structure.
2633 *
2634 * This routine posts initial receive IOCB buffers to the ELS ring. The
2635 * current number of initial IOCB buffers specified by LPFC_BUF_RING0 is
James Smart895427b2017-02-12 13:52:30 -08002636 * set to 64 IOCBs. SLI3 only.
James Smarte59058c2008-08-24 21:49:00 -04002637 *
2638 * Return codes
2639 * 0 - success (currently always success)
2640 **/
dea31012005-04-17 16:05:31 -05002641static int
James Smart2e0fef82007-06-17 19:56:36 -05002642lpfc_post_rcv_buf(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002643{
2644 struct lpfc_sli *psli = &phba->sli;
2645
2646 /* Ring 0, ELS / CT buffers */
James Smart895427b2017-02-12 13:52:30 -08002647 lpfc_post_buffer(phba, &psli->sli3_ring[LPFC_ELS_RING], LPFC_BUF_RING0);
dea31012005-04-17 16:05:31 -05002648 /* Ring 2 - FCP no buffers needed */
2649
2650 return 0;
2651}
2652
2653#define S(N,V) (((V)<<(N))|((V)>>(32-(N))))
2654
James Smarte59058c2008-08-24 21:49:00 -04002655/**
James Smart3621a712009-04-06 18:47:14 -04002656 * lpfc_sha_init - Set up initial array of hash table entries
James Smarte59058c2008-08-24 21:49:00 -04002657 * @HashResultPointer: pointer to an array as hash table.
2658 *
2659 * This routine sets up the initial values to the array of hash table entries
2660 * for the LC HBAs.
2661 **/
dea31012005-04-17 16:05:31 -05002662static void
2663lpfc_sha_init(uint32_t * HashResultPointer)
2664{
2665 HashResultPointer[0] = 0x67452301;
2666 HashResultPointer[1] = 0xEFCDAB89;
2667 HashResultPointer[2] = 0x98BADCFE;
2668 HashResultPointer[3] = 0x10325476;
2669 HashResultPointer[4] = 0xC3D2E1F0;
2670}
2671
James Smarte59058c2008-08-24 21:49:00 -04002672/**
James Smart3621a712009-04-06 18:47:14 -04002673 * lpfc_sha_iterate - Iterate initial hash table with the working hash table
James Smarte59058c2008-08-24 21:49:00 -04002674 * @HashResultPointer: pointer to an initial/result hash table.
2675 * @HashWorkingPointer: pointer to an working hash table.
2676 *
2677 * This routine iterates an initial hash table pointed by @HashResultPointer
2678 * with the values from the working hash table pointeed by @HashWorkingPointer.
2679 * The results are putting back to the initial hash table, returned through
2680 * the @HashResultPointer as the result hash table.
2681 **/
dea31012005-04-17 16:05:31 -05002682static void
2683lpfc_sha_iterate(uint32_t * HashResultPointer, uint32_t * HashWorkingPointer)
2684{
2685 int t;
2686 uint32_t TEMP;
2687 uint32_t A, B, C, D, E;
2688 t = 16;
2689 do {
2690 HashWorkingPointer[t] =
2691 S(1,
2692 HashWorkingPointer[t - 3] ^ HashWorkingPointer[t -
2693 8] ^
2694 HashWorkingPointer[t - 14] ^ HashWorkingPointer[t - 16]);
2695 } while (++t <= 79);
2696 t = 0;
2697 A = HashResultPointer[0];
2698 B = HashResultPointer[1];
2699 C = HashResultPointer[2];
2700 D = HashResultPointer[3];
2701 E = HashResultPointer[4];
2702
2703 do {
2704 if (t < 20) {
2705 TEMP = ((B & C) | ((~B) & D)) + 0x5A827999;
2706 } else if (t < 40) {
2707 TEMP = (B ^ C ^ D) + 0x6ED9EBA1;
2708 } else if (t < 60) {
2709 TEMP = ((B & C) | (B & D) | (C & D)) + 0x8F1BBCDC;
2710 } else {
2711 TEMP = (B ^ C ^ D) + 0xCA62C1D6;
2712 }
2713 TEMP += S(5, A) + E + HashWorkingPointer[t];
2714 E = D;
2715 D = C;
2716 C = S(30, B);
2717 B = A;
2718 A = TEMP;
2719 } while (++t <= 79);
2720
2721 HashResultPointer[0] += A;
2722 HashResultPointer[1] += B;
2723 HashResultPointer[2] += C;
2724 HashResultPointer[3] += D;
2725 HashResultPointer[4] += E;
2726
2727}
2728
James Smarte59058c2008-08-24 21:49:00 -04002729/**
James Smart3621a712009-04-06 18:47:14 -04002730 * lpfc_challenge_key - Create challenge key based on WWPN of the HBA
James Smarte59058c2008-08-24 21:49:00 -04002731 * @RandomChallenge: pointer to the entry of host challenge random number array.
2732 * @HashWorking: pointer to the entry of the working hash array.
2733 *
2734 * This routine calculates the working hash array referred by @HashWorking
2735 * from the challenge random numbers associated with the host, referred by
2736 * @RandomChallenge. The result is put into the entry of the working hash
2737 * array and returned by reference through @HashWorking.
2738 **/
dea31012005-04-17 16:05:31 -05002739static void
2740lpfc_challenge_key(uint32_t * RandomChallenge, uint32_t * HashWorking)
2741{
2742 *HashWorking = (*RandomChallenge ^ *HashWorking);
2743}
2744
James Smarte59058c2008-08-24 21:49:00 -04002745/**
James Smart3621a712009-04-06 18:47:14 -04002746 * lpfc_hba_init - Perform special handling for LC HBA initialization
James Smarte59058c2008-08-24 21:49:00 -04002747 * @phba: pointer to lpfc hba data structure.
2748 * @hbainit: pointer to an array of unsigned 32-bit integers.
2749 *
2750 * This routine performs the special handling for LC HBA initialization.
2751 **/
dea31012005-04-17 16:05:31 -05002752void
2753lpfc_hba_init(struct lpfc_hba *phba, uint32_t *hbainit)
2754{
2755 int t;
2756 uint32_t *HashWorking;
James Smart2e0fef82007-06-17 19:56:36 -05002757 uint32_t *pwwnn = (uint32_t *) phba->wwnn;
dea31012005-04-17 16:05:31 -05002758
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +02002759 HashWorking = kcalloc(80, sizeof(uint32_t), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05002760 if (!HashWorking)
2761 return;
2762
dea31012005-04-17 16:05:31 -05002763 HashWorking[0] = HashWorking[78] = *pwwnn++;
2764 HashWorking[1] = HashWorking[79] = *pwwnn;
2765
2766 for (t = 0; t < 7; t++)
2767 lpfc_challenge_key(phba->RandomData + t, HashWorking + t);
2768
2769 lpfc_sha_init(hbainit);
2770 lpfc_sha_iterate(hbainit, HashWorking);
2771 kfree(HashWorking);
2772}
2773
James Smarte59058c2008-08-24 21:49:00 -04002774/**
James Smart3621a712009-04-06 18:47:14 -04002775 * lpfc_cleanup - Performs vport cleanups before deleting a vport
James Smarte59058c2008-08-24 21:49:00 -04002776 * @vport: pointer to a virtual N_Port data structure.
2777 *
2778 * This routine performs the necessary cleanups before deleting the @vport.
2779 * It invokes the discovery state machine to perform necessary state
2780 * transitions and to release the ndlps associated with the @vport. Note,
2781 * the physical port is treated as @vport 0.
2782 **/
James Smart87af33f2007-10-27 13:37:43 -04002783void
James Smart2e0fef82007-06-17 19:56:36 -05002784lpfc_cleanup(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002785{
James Smart87af33f2007-10-27 13:37:43 -04002786 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002787 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smarta8adb832007-10-27 13:37:53 -04002788 int i = 0;
dea31012005-04-17 16:05:31 -05002789
James Smart87af33f2007-10-27 13:37:43 -04002790 if (phba->link_state > LPFC_LINK_DOWN)
2791 lpfc_port_link_failure(vport);
2792
2793 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05002794 if (!NLP_CHK_NODE_ACT(ndlp)) {
2795 ndlp = lpfc_enable_node(vport, ndlp,
2796 NLP_STE_UNUSED_NODE);
2797 if (!ndlp)
2798 continue;
2799 spin_lock_irq(&phba->ndlp_lock);
2800 NLP_SET_FREE_REQ(ndlp);
2801 spin_unlock_irq(&phba->ndlp_lock);
2802 /* Trigger the release of the ndlp memory */
2803 lpfc_nlp_put(ndlp);
2804 continue;
2805 }
2806 spin_lock_irq(&phba->ndlp_lock);
2807 if (NLP_CHK_FREE_REQ(ndlp)) {
2808 /* The ndlp should not be in memory free mode already */
2809 spin_unlock_irq(&phba->ndlp_lock);
2810 continue;
2811 } else
2812 /* Indicate request for freeing ndlp memory */
2813 NLP_SET_FREE_REQ(ndlp);
2814 spin_unlock_irq(&phba->ndlp_lock);
2815
James Smart58da1ff2008-04-07 10:15:56 -04002816 if (vport->port_type != LPFC_PHYSICAL_PORT &&
2817 ndlp->nlp_DID == Fabric_DID) {
2818 /* Just free up ndlp with Fabric_DID for vports */
2819 lpfc_nlp_put(ndlp);
2820 continue;
2821 }
2822
James Smarteff4a012012-01-18 16:25:25 -05002823 /* take care of nodes in unused state before the state
2824 * machine taking action.
2825 */
2826 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE) {
2827 lpfc_nlp_put(ndlp);
2828 continue;
2829 }
2830
James Smart87af33f2007-10-27 13:37:43 -04002831 if (ndlp->nlp_type & NLP_FABRIC)
2832 lpfc_disc_state_machine(vport, ndlp, NULL,
2833 NLP_EVT_DEVICE_RECOVERY);
James Smarte47c9092008-02-08 18:49:26 -05002834
James Smart87af33f2007-10-27 13:37:43 -04002835 lpfc_disc_state_machine(vport, ndlp, NULL,
2836 NLP_EVT_DEVICE_RM);
2837 }
2838
James Smarta8adb832007-10-27 13:37:53 -04002839 /* At this point, ALL ndlp's should be gone
2840 * because of the previous NLP_EVT_DEVICE_RM.
2841 * Lets wait for this to happen, if needed.
2842 */
James Smart87af33f2007-10-27 13:37:43 -04002843 while (!list_empty(&vport->fc_nodes)) {
James Smarta8adb832007-10-27 13:37:53 -04002844 if (i++ > 3000) {
James Smart87af33f2007-10-27 13:37:43 -04002845 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
James Smarta8adb832007-10-27 13:37:53 -04002846 "0233 Nodelist not empty\n");
James Smarte47c9092008-02-08 18:49:26 -05002847 list_for_each_entry_safe(ndlp, next_ndlp,
2848 &vport->fc_nodes, nlp_listp) {
2849 lpfc_printf_vlog(ndlp->vport, KERN_ERR,
2850 LOG_NODE,
James Smart32350662019-08-14 16:57:06 -07002851 "0282 did:x%x ndlp:x%px "
James Smarte47c9092008-02-08 18:49:26 -05002852 "usgmap:x%x refcnt:%d\n",
2853 ndlp->nlp_DID, (void *)ndlp,
2854 ndlp->nlp_usg_map,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01002855 kref_read(&ndlp->kref));
James Smarte47c9092008-02-08 18:49:26 -05002856 }
James Smarta8adb832007-10-27 13:37:53 -04002857 break;
James Smart87af33f2007-10-27 13:37:43 -04002858 }
James Smarta8adb832007-10-27 13:37:53 -04002859
2860 /* Wait for any activity on ndlps to settle */
2861 msleep(10);
James Smart87af33f2007-10-27 13:37:43 -04002862 }
James Smart1151e3e2011-02-16 12:39:35 -05002863 lpfc_cleanup_vports_rrqs(vport, NULL);
dea31012005-04-17 16:05:31 -05002864}
2865
James Smarte59058c2008-08-24 21:49:00 -04002866/**
James Smart3621a712009-04-06 18:47:14 -04002867 * lpfc_stop_vport_timers - Stop all the timers associated with a vport
James Smarte59058c2008-08-24 21:49:00 -04002868 * @vport: pointer to a virtual N_Port data structure.
2869 *
2870 * This routine stops all the timers associated with a @vport. This function
2871 * is invoked before disabling or deleting a @vport. Note that the physical
2872 * port is treated as @vport 0.
2873 **/
James Smart92d7f7b2007-06-17 19:56:38 -05002874void
2875lpfc_stop_vport_timers(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002876{
James Smart92d7f7b2007-06-17 19:56:38 -05002877 del_timer_sync(&vport->els_tmofunc);
James Smart92494142011-02-16 12:39:44 -05002878 del_timer_sync(&vport->delayed_disc_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05002879 lpfc_can_disctmo(vport);
2880 return;
dea31012005-04-17 16:05:31 -05002881}
2882
James Smarte59058c2008-08-24 21:49:00 -04002883/**
James Smartecfd03c2010-02-12 14:41:27 -05002884 * __lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2885 * @phba: pointer to lpfc hba data structure.
2886 *
2887 * This routine stops the SLI4 FCF rediscover wait timer if it's on. The
2888 * caller of this routine should already hold the host lock.
2889 **/
2890void
2891__lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2892{
James Smart5ac6b302010-10-22 11:05:36 -04002893 /* Clear pending FCF rediscovery wait flag */
2894 phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
2895
James Smartecfd03c2010-02-12 14:41:27 -05002896 /* Now, try to stop the timer */
2897 del_timer(&phba->fcf.redisc_wait);
2898}
2899
2900/**
2901 * lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2902 * @phba: pointer to lpfc hba data structure.
2903 *
2904 * This routine stops the SLI4 FCF rediscover wait timer if it's on. It
2905 * checks whether the FCF rediscovery wait timer is pending with the host
2906 * lock held before proceeding with disabling the timer and clearing the
2907 * wait timer pendig flag.
2908 **/
2909void
2910lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2911{
2912 spin_lock_irq(&phba->hbalock);
2913 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
2914 /* FCF rediscovery timer already fired or stopped */
2915 spin_unlock_irq(&phba->hbalock);
2916 return;
2917 }
2918 __lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
James Smart5ac6b302010-10-22 11:05:36 -04002919 /* Clear failover in progress flags */
2920 phba->fcf.fcf_flag &= ~(FCF_DEAD_DISC | FCF_ACVL_DISC);
James Smartecfd03c2010-02-12 14:41:27 -05002921 spin_unlock_irq(&phba->hbalock);
2922}
2923
2924/**
James Smart3772a992009-05-22 14:50:54 -04002925 * lpfc_stop_hba_timers - Stop all the timers associated with an HBA
James Smarte59058c2008-08-24 21:49:00 -04002926 * @phba: pointer to lpfc hba data structure.
2927 *
2928 * This routine stops all the timers associated with a HBA. This function is
2929 * invoked before either putting a HBA offline or unloading the driver.
2930 **/
James Smart3772a992009-05-22 14:50:54 -04002931void
2932lpfc_stop_hba_timers(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002933{
James Smartcdb42be2019-01-28 11:14:21 -08002934 if (phba->pport)
2935 lpfc_stop_vport_timers(phba->pport);
James Smart32517fc2019-01-28 11:14:33 -08002936 cancel_delayed_work_sync(&phba->eq_delay_work);
James Smart2e0fef82007-06-17 19:56:36 -05002937 del_timer_sync(&phba->sli.mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05002938 del_timer_sync(&phba->fabric_block_timer);
James Smart93996272008-08-24 21:50:30 -04002939 del_timer_sync(&phba->eratt_poll);
James Smart3772a992009-05-22 14:50:54 -04002940 del_timer_sync(&phba->hb_tmofunc);
James Smart1151e3e2011-02-16 12:39:35 -05002941 if (phba->sli_rev == LPFC_SLI_REV4) {
2942 del_timer_sync(&phba->rrq_tmr);
2943 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
2944 }
James Smart3772a992009-05-22 14:50:54 -04002945 phba->hb_outstanding = 0;
2946
2947 switch (phba->pci_dev_grp) {
2948 case LPFC_PCI_DEV_LP:
2949 /* Stop any LightPulse device specific driver timers */
2950 del_timer_sync(&phba->fcp_poll_timer);
2951 break;
2952 case LPFC_PCI_DEV_OC:
Paul Walmsleycc0e5f12019-07-11 20:52:33 -07002953 /* Stop any OneConnect device specific driver timers */
James Smartecfd03c2010-02-12 14:41:27 -05002954 lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
James Smart3772a992009-05-22 14:50:54 -04002955 break;
2956 default:
2957 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2958 "0297 Invalid device group (x%x)\n",
2959 phba->pci_dev_grp);
2960 break;
2961 }
James Smart2e0fef82007-06-17 19:56:36 -05002962 return;
dea31012005-04-17 16:05:31 -05002963}
2964
James Smarte59058c2008-08-24 21:49:00 -04002965/**
James Smart3621a712009-04-06 18:47:14 -04002966 * lpfc_block_mgmt_io - Mark a HBA's management interface as blocked
James Smarte59058c2008-08-24 21:49:00 -04002967 * @phba: pointer to lpfc hba data structure.
2968 *
2969 * This routine marks a HBA's management interface as blocked. Once the HBA's
2970 * management interface is marked as blocked, all the user space access to
2971 * the HBA, whether they are from sysfs interface or libdfc interface will
2972 * all be blocked. The HBA is set to block the management interface when the
2973 * driver prepares the HBA interface for online or offline.
2974 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01002975static void
James Smart618a5232012-06-12 13:54:36 -04002976lpfc_block_mgmt_io(struct lpfc_hba *phba, int mbx_action)
Adrian Bunka6ababd2007-11-05 18:07:33 +01002977{
2978 unsigned long iflag;
James Smart6e7288d2010-06-07 15:23:35 -04002979 uint8_t actcmd = MBX_HEARTBEAT;
2980 unsigned long timeout;
2981
Adrian Bunka6ababd2007-11-05 18:07:33 +01002982 spin_lock_irqsave(&phba->hbalock, iflag);
2983 phba->sli.sli_flag |= LPFC_BLOCK_MGMT_IO;
James Smart618a5232012-06-12 13:54:36 -04002984 spin_unlock_irqrestore(&phba->hbalock, iflag);
2985 if (mbx_action == LPFC_MBX_NO_WAIT)
2986 return;
2987 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
2988 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta183a152011-10-10 21:32:43 -04002989 if (phba->sli.mbox_active) {
James Smart6e7288d2010-06-07 15:23:35 -04002990 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
James Smarta183a152011-10-10 21:32:43 -04002991 /* Determine how long we might wait for the active mailbox
2992 * command to be gracefully completed by firmware.
2993 */
2994 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
2995 phba->sli.mbox_active) * 1000) + jiffies;
2996 }
Adrian Bunka6ababd2007-11-05 18:07:33 +01002997 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta183a152011-10-10 21:32:43 -04002998
James Smart6e7288d2010-06-07 15:23:35 -04002999 /* Wait for the outstnading mailbox command to complete */
3000 while (phba->sli.mbox_active) {
3001 /* Check active mailbox complete status every 2ms */
3002 msleep(2);
3003 if (time_after(jiffies, timeout)) {
3004 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3005 "2813 Mgmt IO is Blocked %x "
3006 "- mbox cmd %x still active\n",
3007 phba->sli.sli_flag, actcmd);
3008 break;
3009 }
3010 }
Adrian Bunka6ababd2007-11-05 18:07:33 +01003011}
3012
James Smarte59058c2008-08-24 21:49:00 -04003013/**
James Smart6b5151f2012-01-18 16:24:06 -05003014 * lpfc_sli4_node_prep - Assign RPIs for active nodes.
3015 * @phba: pointer to lpfc hba data structure.
3016 *
3017 * Allocate RPIs for all active remote nodes. This is needed whenever
3018 * an SLI4 adapter is reset and the driver is not unloading. Its purpose
3019 * is to fixup the temporary rpi assignments.
3020 **/
3021void
3022lpfc_sli4_node_prep(struct lpfc_hba *phba)
3023{
3024 struct lpfc_nodelist *ndlp, *next_ndlp;
3025 struct lpfc_vport **vports;
James Smart9d3d3402017-04-21 16:05:00 -07003026 int i, rpi;
3027 unsigned long flags;
James Smart6b5151f2012-01-18 16:24:06 -05003028
3029 if (phba->sli_rev != LPFC_SLI_REV4)
3030 return;
3031
3032 vports = lpfc_create_vport_work_array(phba);
James Smart9d3d3402017-04-21 16:05:00 -07003033 if (vports == NULL)
3034 return;
James Smart6b5151f2012-01-18 16:24:06 -05003035
James Smart9d3d3402017-04-21 16:05:00 -07003036 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
3037 if (vports[i]->load_flag & FC_UNLOADING)
3038 continue;
3039
3040 list_for_each_entry_safe(ndlp, next_ndlp,
3041 &vports[i]->fc_nodes,
3042 nlp_listp) {
3043 if (!NLP_CHK_NODE_ACT(ndlp))
3044 continue;
3045 rpi = lpfc_sli4_alloc_rpi(phba);
3046 if (rpi == LPFC_RPI_ALLOC_ERROR) {
3047 spin_lock_irqsave(&phba->ndlp_lock, flags);
3048 NLP_CLR_NODE_ACT(ndlp);
3049 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
3050 continue;
James Smart6b5151f2012-01-18 16:24:06 -05003051 }
James Smart9d3d3402017-04-21 16:05:00 -07003052 ndlp->nlp_rpi = rpi;
James Smart0f154222019-09-21 20:58:52 -07003053 lpfc_printf_vlog(ndlp->vport, KERN_INFO,
3054 LOG_NODE | LOG_DISCOVERY,
3055 "0009 Assign RPI x%x to ndlp x%px "
3056 "DID:x%06x flg:x%x map:x%x\n",
3057 ndlp->nlp_rpi, ndlp, ndlp->nlp_DID,
3058 ndlp->nlp_flag, ndlp->nlp_usg_map);
James Smart6b5151f2012-01-18 16:24:06 -05003059 }
3060 }
3061 lpfc_destroy_vport_work_array(phba, vports);
3062}
3063
3064/**
James Smartc4908502019-01-28 11:14:28 -08003065 * lpfc_create_expedite_pool - create expedite pool
3066 * @phba: pointer to lpfc hba data structure.
3067 *
3068 * This routine moves a batch of XRIs from lpfc_io_buf_list_put of HWQ 0
3069 * to expedite pool. Mark them as expedite.
3070 **/
Bart Van Assche3999df72019-03-28 11:06:16 -07003071static void lpfc_create_expedite_pool(struct lpfc_hba *phba)
James Smartc4908502019-01-28 11:14:28 -08003072{
3073 struct lpfc_sli4_hdw_queue *qp;
3074 struct lpfc_io_buf *lpfc_ncmd;
3075 struct lpfc_io_buf *lpfc_ncmd_next;
3076 struct lpfc_epd_pool *epd_pool;
3077 unsigned long iflag;
3078
3079 epd_pool = &phba->epd_pool;
3080 qp = &phba->sli4_hba.hdwq[0];
3081
3082 spin_lock_init(&epd_pool->lock);
3083 spin_lock_irqsave(&qp->io_buf_list_put_lock, iflag);
3084 spin_lock(&epd_pool->lock);
3085 INIT_LIST_HEAD(&epd_pool->list);
3086 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
3087 &qp->lpfc_io_buf_list_put, list) {
3088 list_move_tail(&lpfc_ncmd->list, &epd_pool->list);
3089 lpfc_ncmd->expedite = true;
3090 qp->put_io_bufs--;
3091 epd_pool->count++;
3092 if (epd_pool->count >= XRI_BATCH)
3093 break;
3094 }
3095 spin_unlock(&epd_pool->lock);
3096 spin_unlock_irqrestore(&qp->io_buf_list_put_lock, iflag);
3097}
3098
3099/**
3100 * lpfc_destroy_expedite_pool - destroy expedite pool
3101 * @phba: pointer to lpfc hba data structure.
3102 *
3103 * This routine returns XRIs from expedite pool to lpfc_io_buf_list_put
3104 * of HWQ 0. Clear the mark.
3105 **/
Bart Van Assche3999df72019-03-28 11:06:16 -07003106static void lpfc_destroy_expedite_pool(struct lpfc_hba *phba)
James Smartc4908502019-01-28 11:14:28 -08003107{
3108 struct lpfc_sli4_hdw_queue *qp;
3109 struct lpfc_io_buf *lpfc_ncmd;
3110 struct lpfc_io_buf *lpfc_ncmd_next;
3111 struct lpfc_epd_pool *epd_pool;
3112 unsigned long iflag;
3113
3114 epd_pool = &phba->epd_pool;
3115 qp = &phba->sli4_hba.hdwq[0];
3116
3117 spin_lock_irqsave(&qp->io_buf_list_put_lock, iflag);
3118 spin_lock(&epd_pool->lock);
3119 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
3120 &epd_pool->list, list) {
3121 list_move_tail(&lpfc_ncmd->list,
3122 &qp->lpfc_io_buf_list_put);
3123 lpfc_ncmd->flags = false;
3124 qp->put_io_bufs++;
3125 epd_pool->count--;
3126 }
3127 spin_unlock(&epd_pool->lock);
3128 spin_unlock_irqrestore(&qp->io_buf_list_put_lock, iflag);
3129}
3130
3131/**
3132 * lpfc_create_multixri_pools - create multi-XRI pools
3133 * @phba: pointer to lpfc hba data structure.
3134 *
3135 * This routine initialize public, private per HWQ. Then, move XRIs from
3136 * lpfc_io_buf_list_put to public pool. High and low watermark are also
3137 * Initialized.
3138 **/
3139void lpfc_create_multixri_pools(struct lpfc_hba *phba)
3140{
3141 u32 i, j;
3142 u32 hwq_count;
3143 u32 count_per_hwq;
3144 struct lpfc_io_buf *lpfc_ncmd;
3145 struct lpfc_io_buf *lpfc_ncmd_next;
3146 unsigned long iflag;
3147 struct lpfc_sli4_hdw_queue *qp;
3148 struct lpfc_multixri_pool *multixri_pool;
3149 struct lpfc_pbl_pool *pbl_pool;
3150 struct lpfc_pvt_pool *pvt_pool;
3151
3152 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3153 "1234 num_hdw_queue=%d num_present_cpu=%d common_xri_cnt=%d\n",
3154 phba->cfg_hdw_queue, phba->sli4_hba.num_present_cpu,
3155 phba->sli4_hba.io_xri_cnt);
3156
3157 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
3158 lpfc_create_expedite_pool(phba);
3159
3160 hwq_count = phba->cfg_hdw_queue;
3161 count_per_hwq = phba->sli4_hba.io_xri_cnt / hwq_count;
3162
3163 for (i = 0; i < hwq_count; i++) {
3164 multixri_pool = kzalloc(sizeof(*multixri_pool), GFP_KERNEL);
3165
3166 if (!multixri_pool) {
3167 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3168 "1238 Failed to allocate memory for "
3169 "multixri_pool\n");
3170
3171 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
3172 lpfc_destroy_expedite_pool(phba);
3173
3174 j = 0;
3175 while (j < i) {
3176 qp = &phba->sli4_hba.hdwq[j];
3177 kfree(qp->p_multixri_pool);
3178 j++;
3179 }
3180 phba->cfg_xri_rebalancing = 0;
3181 return;
3182 }
3183
3184 qp = &phba->sli4_hba.hdwq[i];
3185 qp->p_multixri_pool = multixri_pool;
3186
3187 multixri_pool->xri_limit = count_per_hwq;
3188 multixri_pool->rrb_next_hwqid = i;
3189
3190 /* Deal with public free xri pool */
3191 pbl_pool = &multixri_pool->pbl_pool;
3192 spin_lock_init(&pbl_pool->lock);
3193 spin_lock_irqsave(&qp->io_buf_list_put_lock, iflag);
3194 spin_lock(&pbl_pool->lock);
3195 INIT_LIST_HEAD(&pbl_pool->list);
3196 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
3197 &qp->lpfc_io_buf_list_put, list) {
3198 list_move_tail(&lpfc_ncmd->list, &pbl_pool->list);
3199 qp->put_io_bufs--;
3200 pbl_pool->count++;
3201 }
3202 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3203 "1235 Moved %d buffers from PUT list over to pbl_pool[%d]\n",
3204 pbl_pool->count, i);
3205 spin_unlock(&pbl_pool->lock);
3206 spin_unlock_irqrestore(&qp->io_buf_list_put_lock, iflag);
3207
3208 /* Deal with private free xri pool */
3209 pvt_pool = &multixri_pool->pvt_pool;
3210 pvt_pool->high_watermark = multixri_pool->xri_limit / 2;
3211 pvt_pool->low_watermark = XRI_BATCH;
3212 spin_lock_init(&pvt_pool->lock);
3213 spin_lock_irqsave(&pvt_pool->lock, iflag);
3214 INIT_LIST_HEAD(&pvt_pool->list);
3215 pvt_pool->count = 0;
3216 spin_unlock_irqrestore(&pvt_pool->lock, iflag);
3217 }
3218}
3219
3220/**
3221 * lpfc_destroy_multixri_pools - destroy multi-XRI pools
3222 * @phba: pointer to lpfc hba data structure.
3223 *
3224 * This routine returns XRIs from public/private to lpfc_io_buf_list_put.
3225 **/
Bart Van Assche3999df72019-03-28 11:06:16 -07003226static void lpfc_destroy_multixri_pools(struct lpfc_hba *phba)
James Smartc4908502019-01-28 11:14:28 -08003227{
3228 u32 i;
3229 u32 hwq_count;
3230 struct lpfc_io_buf *lpfc_ncmd;
3231 struct lpfc_io_buf *lpfc_ncmd_next;
3232 unsigned long iflag;
3233 struct lpfc_sli4_hdw_queue *qp;
3234 struct lpfc_multixri_pool *multixri_pool;
3235 struct lpfc_pbl_pool *pbl_pool;
3236 struct lpfc_pvt_pool *pvt_pool;
3237
3238 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
3239 lpfc_destroy_expedite_pool(phba);
3240
James Smartc00f62e2019-08-14 16:57:11 -07003241 if (!(phba->pport->load_flag & FC_UNLOADING))
3242 lpfc_sli_flush_io_rings(phba);
James Smartc66a9192019-03-12 16:30:19 -07003243
James Smartc4908502019-01-28 11:14:28 -08003244 hwq_count = phba->cfg_hdw_queue;
3245
3246 for (i = 0; i < hwq_count; i++) {
3247 qp = &phba->sli4_hba.hdwq[i];
3248 multixri_pool = qp->p_multixri_pool;
3249 if (!multixri_pool)
3250 continue;
3251
3252 qp->p_multixri_pool = NULL;
3253
3254 spin_lock_irqsave(&qp->io_buf_list_put_lock, iflag);
3255
3256 /* Deal with public free xri pool */
3257 pbl_pool = &multixri_pool->pbl_pool;
3258 spin_lock(&pbl_pool->lock);
3259
3260 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3261 "1236 Moving %d buffers from pbl_pool[%d] TO PUT list\n",
3262 pbl_pool->count, i);
3263
3264 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
3265 &pbl_pool->list, list) {
3266 list_move_tail(&lpfc_ncmd->list,
3267 &qp->lpfc_io_buf_list_put);
3268 qp->put_io_bufs++;
3269 pbl_pool->count--;
3270 }
3271
3272 INIT_LIST_HEAD(&pbl_pool->list);
3273 pbl_pool->count = 0;
3274
3275 spin_unlock(&pbl_pool->lock);
3276
3277 /* Deal with private free xri pool */
3278 pvt_pool = &multixri_pool->pvt_pool;
3279 spin_lock(&pvt_pool->lock);
3280
3281 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3282 "1237 Moving %d buffers from pvt_pool[%d] TO PUT list\n",
3283 pvt_pool->count, i);
3284
3285 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
3286 &pvt_pool->list, list) {
3287 list_move_tail(&lpfc_ncmd->list,
3288 &qp->lpfc_io_buf_list_put);
3289 qp->put_io_bufs++;
3290 pvt_pool->count--;
3291 }
3292
3293 INIT_LIST_HEAD(&pvt_pool->list);
3294 pvt_pool->count = 0;
3295
3296 spin_unlock(&pvt_pool->lock);
3297 spin_unlock_irqrestore(&qp->io_buf_list_put_lock, iflag);
3298
3299 kfree(multixri_pool);
3300 }
3301}
3302
3303/**
James Smart3621a712009-04-06 18:47:14 -04003304 * lpfc_online - Initialize and bring a HBA online
James Smarte59058c2008-08-24 21:49:00 -04003305 * @phba: pointer to lpfc hba data structure.
3306 *
3307 * This routine initializes the HBA and brings a HBA online. During this
3308 * process, the management interface is blocked to prevent user space access
3309 * to the HBA interfering with the driver initialization.
3310 *
3311 * Return codes
3312 * 0 - successful
3313 * 1 - failed
3314 **/
dea31012005-04-17 16:05:31 -05003315int
James Smart2e0fef82007-06-17 19:56:36 -05003316lpfc_online(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05003317{
Julia Lawall372bd282008-12-16 16:15:08 +01003318 struct lpfc_vport *vport;
James Smart549e55c2007-08-02 11:09:51 -04003319 struct lpfc_vport **vports;
Dick Kennedya145fda2017-08-23 16:55:44 -07003320 int i, error = 0;
James Smart16a3a202013-04-17 20:14:38 -04003321 bool vpis_cleared = false;
James Smart2e0fef82007-06-17 19:56:36 -05003322
dea31012005-04-17 16:05:31 -05003323 if (!phba)
3324 return 0;
Julia Lawall372bd282008-12-16 16:15:08 +01003325 vport = phba->pport;
dea31012005-04-17 16:05:31 -05003326
James Smart2e0fef82007-06-17 19:56:36 -05003327 if (!(vport->fc_flag & FC_OFFLINE_MODE))
dea31012005-04-17 16:05:31 -05003328 return 0;
3329
James Smarted957682007-06-17 19:56:37 -05003330 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003331 "0458 Bring Adapter online\n");
dea31012005-04-17 16:05:31 -05003332
James Smart618a5232012-06-12 13:54:36 -04003333 lpfc_block_mgmt_io(phba, LPFC_MBX_WAIT);
dea31012005-04-17 16:05:31 -05003334
James Smartda0436e2009-05-22 14:51:39 -04003335 if (phba->sli_rev == LPFC_SLI_REV4) {
3336 if (lpfc_sli4_hba_setup(phba)) { /* Initialize SLI4 HBA */
3337 lpfc_unblock_mgmt_io(phba);
3338 return 1;
3339 }
James Smart16a3a202013-04-17 20:14:38 -04003340 spin_lock_irq(&phba->hbalock);
3341 if (!phba->sli4_hba.max_cfg_param.vpi_used)
3342 vpis_cleared = true;
3343 spin_unlock_irq(&phba->hbalock);
Dick Kennedya145fda2017-08-23 16:55:44 -07003344
3345 /* Reestablish the local initiator port.
3346 * The offline process destroyed the previous lport.
3347 */
3348 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME &&
3349 !phba->nvmet_support) {
3350 error = lpfc_nvme_create_localport(phba->pport);
3351 if (error)
3352 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3353 "6132 NVME restore reg failed "
3354 "on nvmei error x%x\n", error);
3355 }
James Smartda0436e2009-05-22 14:51:39 -04003356 } else {
James Smart895427b2017-02-12 13:52:30 -08003357 lpfc_sli_queue_init(phba);
James Smartda0436e2009-05-22 14:51:39 -04003358 if (lpfc_sli_hba_setup(phba)) { /* Initialize SLI2/SLI3 HBA */
3359 lpfc_unblock_mgmt_io(phba);
3360 return 1;
3361 }
James Smart46fa3112007-04-25 09:51:45 -04003362 }
dea31012005-04-17 16:05:31 -05003363
James Smart549e55c2007-08-02 11:09:51 -04003364 vports = lpfc_create_vport_work_array(phba);
Arnd Bergmannaeb66412016-03-14 15:29:44 +01003365 if (vports != NULL) {
James Smartda0436e2009-05-22 14:51:39 -04003366 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -04003367 struct Scsi_Host *shost;
3368 shost = lpfc_shost_from_vport(vports[i]);
3369 spin_lock_irq(shost->host_lock);
3370 vports[i]->fc_flag &= ~FC_OFFLINE_MODE;
3371 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
3372 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart16a3a202013-04-17 20:14:38 -04003373 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart1c6834a2009-07-19 10:01:26 -04003374 vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
James Smart16a3a202013-04-17 20:14:38 -04003375 if ((vpis_cleared) &&
3376 (vports[i]->port_type !=
3377 LPFC_PHYSICAL_PORT))
3378 vports[i]->vpi = 0;
3379 }
James Smart549e55c2007-08-02 11:09:51 -04003380 spin_unlock_irq(shost->host_lock);
3381 }
Arnd Bergmannaeb66412016-03-14 15:29:44 +01003382 }
3383 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05003384
James Smartc4908502019-01-28 11:14:28 -08003385 if (phba->cfg_xri_rebalancing)
3386 lpfc_create_multixri_pools(phba);
3387
James Smart93a4d6f2019-11-04 16:57:05 -08003388 lpfc_cpuhp_add(phba);
3389
James Smart46fa3112007-04-25 09:51:45 -04003390 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05003391 return 0;
3392}
3393
James Smarte59058c2008-08-24 21:49:00 -04003394/**
James Smart3621a712009-04-06 18:47:14 -04003395 * lpfc_unblock_mgmt_io - Mark a HBA's management interface to be not blocked
James Smarte59058c2008-08-24 21:49:00 -04003396 * @phba: pointer to lpfc hba data structure.
3397 *
3398 * This routine marks a HBA's management interface as not blocked. Once the
3399 * HBA's management interface is marked as not blocked, all the user space
3400 * access to the HBA, whether they are from sysfs interface or libdfc
3401 * interface will be allowed. The HBA is set to block the management interface
3402 * when the driver prepares the HBA interface for online or offline and then
3403 * set to unblock the management interface afterwards.
3404 **/
James Smart46fa3112007-04-25 09:51:45 -04003405void
James Smart46fa3112007-04-25 09:51:45 -04003406lpfc_unblock_mgmt_io(struct lpfc_hba * phba)
3407{
3408 unsigned long iflag;
3409
James Smart2e0fef82007-06-17 19:56:36 -05003410 spin_lock_irqsave(&phba->hbalock, iflag);
3411 phba->sli.sli_flag &= ~LPFC_BLOCK_MGMT_IO;
3412 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart46fa3112007-04-25 09:51:45 -04003413}
3414
James Smarte59058c2008-08-24 21:49:00 -04003415/**
James Smart3621a712009-04-06 18:47:14 -04003416 * lpfc_offline_prep - Prepare a HBA to be brought offline
James Smarte59058c2008-08-24 21:49:00 -04003417 * @phba: pointer to lpfc hba data structure.
3418 *
3419 * This routine is invoked to prepare a HBA to be brought offline. It performs
3420 * unregistration login to all the nodes on all vports and flushes the mailbox
3421 * queue to make it ready to be brought offline.
3422 **/
James Smart46fa3112007-04-25 09:51:45 -04003423void
James Smart618a5232012-06-12 13:54:36 -04003424lpfc_offline_prep(struct lpfc_hba *phba, int mbx_action)
James Smart46fa3112007-04-25 09:51:45 -04003425{
James Smart2e0fef82007-06-17 19:56:36 -05003426 struct lpfc_vport *vport = phba->pport;
James Smart46fa3112007-04-25 09:51:45 -04003427 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart87af33f2007-10-27 13:37:43 -04003428 struct lpfc_vport **vports;
James Smart72100cc2010-02-12 14:43:01 -05003429 struct Scsi_Host *shost;
James Smart87af33f2007-10-27 13:37:43 -04003430 int i;
dea31012005-04-17 16:05:31 -05003431
James Smart2e0fef82007-06-17 19:56:36 -05003432 if (vport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04003433 return;
dea31012005-04-17 16:05:31 -05003434
James Smart618a5232012-06-12 13:54:36 -04003435 lpfc_block_mgmt_io(phba, mbx_action);
dea31012005-04-17 16:05:31 -05003436
3437 lpfc_linkdown(phba);
3438
James Smart87af33f2007-10-27 13:37:43 -04003439 /* Issue an unreg_login to all nodes on all vports */
3440 vports = lpfc_create_vport_work_array(phba);
3441 if (vports != NULL) {
James Smartda0436e2009-05-22 14:51:39 -04003442 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smarta8adb832007-10-27 13:37:53 -04003443 if (vports[i]->load_flag & FC_UNLOADING)
3444 continue;
James Smart72100cc2010-02-12 14:43:01 -05003445 shost = lpfc_shost_from_vport(vports[i]);
3446 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05003447 vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05003448 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
3449 vports[i]->fc_flag &= ~FC_VFI_REGISTERED;
James Smart72100cc2010-02-12 14:43:01 -05003450 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05003451
James Smart87af33f2007-10-27 13:37:43 -04003452 shost = lpfc_shost_from_vport(vports[i]);
3453 list_for_each_entry_safe(ndlp, next_ndlp,
3454 &vports[i]->fc_nodes,
3455 nlp_listp) {
James Smart0f154222019-09-21 20:58:52 -07003456 if ((!NLP_CHK_NODE_ACT(ndlp)) ||
3457 ndlp->nlp_state == NLP_STE_UNUSED_NODE) {
3458 /* Driver must assume RPI is invalid for
3459 * any unused or inactive node.
3460 */
3461 ndlp->nlp_rpi = LPFC_RPI_ALLOC_ERROR;
James Smarte47c9092008-02-08 18:49:26 -05003462 continue;
James Smart0f154222019-09-21 20:58:52 -07003463 }
3464
James Smart87af33f2007-10-27 13:37:43 -04003465 if (ndlp->nlp_type & NLP_FABRIC) {
3466 lpfc_disc_state_machine(vports[i], ndlp,
3467 NULL, NLP_EVT_DEVICE_RECOVERY);
3468 lpfc_disc_state_machine(vports[i], ndlp,
3469 NULL, NLP_EVT_DEVICE_RM);
3470 }
3471 spin_lock_irq(shost->host_lock);
3472 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
James Smart401ee0c2012-03-01 22:35:34 -05003473 spin_unlock_irq(shost->host_lock);
James Smart6b5151f2012-01-18 16:24:06 -05003474 /*
3475 * Whenever an SLI4 port goes offline, free the
James Smart401ee0c2012-03-01 22:35:34 -05003476 * RPI. Get a new RPI when the adapter port
3477 * comes back online.
James Smart6b5151f2012-01-18 16:24:06 -05003478 */
James Smartbe6bb942015-04-07 15:07:22 -04003479 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart0f154222019-09-21 20:58:52 -07003480 lpfc_printf_vlog(ndlp->vport, KERN_INFO,
3481 LOG_NODE | LOG_DISCOVERY,
3482 "0011 Free RPI x%x on "
3483 "ndlp:x%px did x%x "
3484 "usgmap:x%x\n",
3485 ndlp->nlp_rpi, ndlp,
3486 ndlp->nlp_DID,
3487 ndlp->nlp_usg_map);
James Smart6b5151f2012-01-18 16:24:06 -05003488 lpfc_sli4_free_rpi(phba, ndlp->nlp_rpi);
James Smart0f154222019-09-21 20:58:52 -07003489 ndlp->nlp_rpi = LPFC_RPI_ALLOC_ERROR;
James Smartbe6bb942015-04-07 15:07:22 -04003490 }
James Smart87af33f2007-10-27 13:37:43 -04003491 lpfc_unreg_rpi(vports[i], ndlp);
3492 }
3493 }
3494 }
James Smart09372822008-01-11 01:52:54 -05003495 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05003496
James Smart618a5232012-06-12 13:54:36 -04003497 lpfc_sli_mbox_sys_shutdown(phba, mbx_action);
Dick Kennedyf485c182017-09-29 17:34:34 -07003498
3499 if (phba->wq)
3500 flush_workqueue(phba->wq);
James Smart46fa3112007-04-25 09:51:45 -04003501}
3502
James Smarte59058c2008-08-24 21:49:00 -04003503/**
James Smart3621a712009-04-06 18:47:14 -04003504 * lpfc_offline - Bring a HBA offline
James Smarte59058c2008-08-24 21:49:00 -04003505 * @phba: pointer to lpfc hba data structure.
3506 *
3507 * This routine actually brings a HBA offline. It stops all the timers
3508 * associated with the HBA, brings down the SLI layer, and eventually
3509 * marks the HBA as in offline state for the upper layer protocol.
3510 **/
James Smart46fa3112007-04-25 09:51:45 -04003511void
James Smart2e0fef82007-06-17 19:56:36 -05003512lpfc_offline(struct lpfc_hba *phba)
James Smart46fa3112007-04-25 09:51:45 -04003513{
James Smart549e55c2007-08-02 11:09:51 -04003514 struct Scsi_Host *shost;
3515 struct lpfc_vport **vports;
3516 int i;
James Smart46fa3112007-04-25 09:51:45 -04003517
James Smart549e55c2007-08-02 11:09:51 -04003518 if (phba->pport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04003519 return;
James Smart688a8862006-07-06 15:49:56 -04003520
James Smartda0436e2009-05-22 14:51:39 -04003521 /* stop port and all timers associated with this hba */
3522 lpfc_stop_port(phba);
Dick Kennedy4b40d022017-08-23 16:55:38 -07003523
3524 /* Tear down the local and target port registrations. The
3525 * nvme transports need to cleanup.
3526 */
3527 lpfc_nvmet_destroy_targetport(phba);
3528 lpfc_nvme_destroy_localport(phba->pport);
3529
James Smart51ef4c22007-08-02 11:10:31 -04003530 vports = lpfc_create_vport_work_array(phba);
3531 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04003532 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
James Smart51ef4c22007-08-02 11:10:31 -04003533 lpfc_stop_vport_timers(vports[i]);
James Smart09372822008-01-11 01:52:54 -05003534 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -05003535 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003536 "0460 Bring Adapter offline\n");
dea31012005-04-17 16:05:31 -05003537 /* Bring down the SLI Layer and cleanup. The HBA is offline
3538 now. */
3539 lpfc_sli_hba_down(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05003540 spin_lock_irq(&phba->hbalock);
James Smart7054a602007-04-25 09:52:34 -04003541 phba->work_ha = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05003542 spin_unlock_irq(&phba->hbalock);
James Smart549e55c2007-08-02 11:09:51 -04003543 vports = lpfc_create_vport_work_array(phba);
3544 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04003545 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -04003546 shost = lpfc_shost_from_vport(vports[i]);
James Smart549e55c2007-08-02 11:09:51 -04003547 spin_lock_irq(shost->host_lock);
3548 vports[i]->work_port_events = 0;
3549 vports[i]->fc_flag |= FC_OFFLINE_MODE;
3550 spin_unlock_irq(shost->host_lock);
3551 }
James Smart09372822008-01-11 01:52:54 -05003552 lpfc_destroy_vport_work_array(phba, vports);
James Smart93a4d6f2019-11-04 16:57:05 -08003553 __lpfc_cpuhp_remove(phba);
James Smartc4908502019-01-28 11:14:28 -08003554
3555 if (phba->cfg_xri_rebalancing)
3556 lpfc_destroy_multixri_pools(phba);
dea31012005-04-17 16:05:31 -05003557}
3558
James Smarte59058c2008-08-24 21:49:00 -04003559/**
James Smart3621a712009-04-06 18:47:14 -04003560 * lpfc_scsi_free - Free all the SCSI buffers and IOCBs from driver lists
James Smarte59058c2008-08-24 21:49:00 -04003561 * @phba: pointer to lpfc hba data structure.
3562 *
3563 * This routine is to free all the SCSI buffers and IOCBs from the driver
3564 * list back to kernel. It is called from lpfc_pci_remove_one to free
3565 * the internal resources before the device is removed from the system.
James Smarte59058c2008-08-24 21:49:00 -04003566 **/
James Smart8a9d2e82012-05-09 21:16:12 -04003567static void
James Smart2e0fef82007-06-17 19:56:36 -05003568lpfc_scsi_free(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05003569{
James Smartc4908502019-01-28 11:14:28 -08003570 struct lpfc_io_buf *sb, *sb_next;
dea31012005-04-17 16:05:31 -05003571
James Smart895427b2017-02-12 13:52:30 -08003572 if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP))
3573 return;
3574
James Smart2e0fef82007-06-17 19:56:36 -05003575 spin_lock_irq(&phba->hbalock);
James Smarta40fc5f2013-04-17 20:17:40 -04003576
dea31012005-04-17 16:05:31 -05003577 /* Release all the lpfc_scsi_bufs maintained by this host. */
James Smarta40fc5f2013-04-17 20:17:40 -04003578
3579 spin_lock(&phba->scsi_buf_list_put_lock);
3580 list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list_put,
3581 list) {
dea31012005-04-17 16:05:31 -05003582 list_del(&sb->list);
Romain Perier771db5c2017-07-06 10:13:05 +02003583 dma_pool_free(phba->lpfc_sg_dma_buf_pool, sb->data,
James Smart92d7f7b2007-06-17 19:56:38 -05003584 sb->dma_handle);
dea31012005-04-17 16:05:31 -05003585 kfree(sb);
3586 phba->total_scsi_bufs--;
3587 }
James Smarta40fc5f2013-04-17 20:17:40 -04003588 spin_unlock(&phba->scsi_buf_list_put_lock);
3589
3590 spin_lock(&phba->scsi_buf_list_get_lock);
3591 list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list_get,
3592 list) {
3593 list_del(&sb->list);
Romain Perier771db5c2017-07-06 10:13:05 +02003594 dma_pool_free(phba->lpfc_sg_dma_buf_pool, sb->data,
James Smarta40fc5f2013-04-17 20:17:40 -04003595 sb->dma_handle);
3596 kfree(sb);
3597 phba->total_scsi_bufs--;
3598 }
3599 spin_unlock(&phba->scsi_buf_list_get_lock);
James Smart2e0fef82007-06-17 19:56:36 -05003600 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04003601}
James Smart0794d602019-01-28 11:14:19 -08003602
James Smart8a9d2e82012-05-09 21:16:12 -04003603/**
James Smart5e5b5112019-01-28 11:14:22 -08003604 * lpfc_io_free - Free all the IO buffers and IOCBs from driver lists
James Smart895427b2017-02-12 13:52:30 -08003605 * @phba: pointer to lpfc hba data structure.
3606 *
James Smart0794d602019-01-28 11:14:19 -08003607 * This routine is to free all the IO buffers and IOCBs from the driver
James Smart895427b2017-02-12 13:52:30 -08003608 * list back to kernel. It is called from lpfc_pci_remove_one to free
3609 * the internal resources before the device is removed from the system.
3610 **/
James Smartc4908502019-01-28 11:14:28 -08003611void
James Smart5e5b5112019-01-28 11:14:22 -08003612lpfc_io_free(struct lpfc_hba *phba)
James Smart895427b2017-02-12 13:52:30 -08003613{
James Smartc4908502019-01-28 11:14:28 -08003614 struct lpfc_io_buf *lpfc_ncmd, *lpfc_ncmd_next;
James Smart5e5b5112019-01-28 11:14:22 -08003615 struct lpfc_sli4_hdw_queue *qp;
3616 int idx;
James Smart895427b2017-02-12 13:52:30 -08003617
James Smart5e5b5112019-01-28 11:14:22 -08003618 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
3619 qp = &phba->sli4_hba.hdwq[idx];
3620 /* Release all the lpfc_nvme_bufs maintained by this host. */
3621 spin_lock(&qp->io_buf_list_put_lock);
3622 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
3623 &qp->lpfc_io_buf_list_put,
3624 list) {
3625 list_del(&lpfc_ncmd->list);
3626 qp->put_io_bufs--;
3627 dma_pool_free(phba->lpfc_sg_dma_buf_pool,
3628 lpfc_ncmd->data, lpfc_ncmd->dma_handle);
James Smartd79c9e92019-08-14 16:57:09 -07003629 if (phba->cfg_xpsgl && !phba->nvmet_support)
3630 lpfc_put_sgl_per_hdwq(phba, lpfc_ncmd);
3631 lpfc_put_cmd_rsp_buf_per_hdwq(phba, lpfc_ncmd);
James Smart5e5b5112019-01-28 11:14:22 -08003632 kfree(lpfc_ncmd);
3633 qp->total_io_bufs--;
3634 }
3635 spin_unlock(&qp->io_buf_list_put_lock);
James Smart895427b2017-02-12 13:52:30 -08003636
James Smart5e5b5112019-01-28 11:14:22 -08003637 spin_lock(&qp->io_buf_list_get_lock);
3638 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
3639 &qp->lpfc_io_buf_list_get,
3640 list) {
3641 list_del(&lpfc_ncmd->list);
3642 qp->get_io_bufs--;
3643 dma_pool_free(phba->lpfc_sg_dma_buf_pool,
3644 lpfc_ncmd->data, lpfc_ncmd->dma_handle);
James Smartd79c9e92019-08-14 16:57:09 -07003645 if (phba->cfg_xpsgl && !phba->nvmet_support)
3646 lpfc_put_sgl_per_hdwq(phba, lpfc_ncmd);
3647 lpfc_put_cmd_rsp_buf_per_hdwq(phba, lpfc_ncmd);
James Smart5e5b5112019-01-28 11:14:22 -08003648 kfree(lpfc_ncmd);
3649 qp->total_io_bufs--;
3650 }
3651 spin_unlock(&qp->io_buf_list_get_lock);
James Smart895427b2017-02-12 13:52:30 -08003652 }
James Smart895427b2017-02-12 13:52:30 -08003653}
James Smart0794d602019-01-28 11:14:19 -08003654
James Smart895427b2017-02-12 13:52:30 -08003655/**
3656 * lpfc_sli4_els_sgl_update - update ELS xri-sgl sizing and mapping
James Smart8a9d2e82012-05-09 21:16:12 -04003657 * @phba: pointer to lpfc hba data structure.
3658 *
3659 * This routine first calculates the sizes of the current els and allocated
3660 * scsi sgl lists, and then goes through all sgls to updates the physical
3661 * XRIs assigned due to port function reset. During port initialization, the
3662 * current els and allocated scsi sgl lists are 0s.
3663 *
3664 * Return codes
3665 * 0 - successful (for now, it always returns 0)
3666 **/
3667int
James Smart895427b2017-02-12 13:52:30 -08003668lpfc_sli4_els_sgl_update(struct lpfc_hba *phba)
James Smart8a9d2e82012-05-09 21:16:12 -04003669{
3670 struct lpfc_sglq *sglq_entry = NULL, *sglq_entry_next = NULL;
James Smart895427b2017-02-12 13:52:30 -08003671 uint16_t i, lxri, xri_cnt, els_xri_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04003672 LIST_HEAD(els_sgl_list);
James Smart8a9d2e82012-05-09 21:16:12 -04003673 int rc;
3674
3675 /*
3676 * update on pci function's els xri-sgl list
3677 */
3678 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
James Smart895427b2017-02-12 13:52:30 -08003679
James Smart8a9d2e82012-05-09 21:16:12 -04003680 if (els_xri_cnt > phba->sli4_hba.els_xri_cnt) {
3681 /* els xri-sgl expanded */
3682 xri_cnt = els_xri_cnt - phba->sli4_hba.els_xri_cnt;
3683 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3684 "3157 ELS xri-sgl count increased from "
3685 "%d to %d\n", phba->sli4_hba.els_xri_cnt,
3686 els_xri_cnt);
3687 /* allocate the additional els sgls */
3688 for (i = 0; i < xri_cnt; i++) {
3689 sglq_entry = kzalloc(sizeof(struct lpfc_sglq),
3690 GFP_KERNEL);
3691 if (sglq_entry == NULL) {
3692 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3693 "2562 Failure to allocate an "
3694 "ELS sgl entry:%d\n", i);
3695 rc = -ENOMEM;
3696 goto out_free_mem;
3697 }
3698 sglq_entry->buff_type = GEN_BUFF_TYPE;
3699 sglq_entry->virt = lpfc_mbuf_alloc(phba, 0,
3700 &sglq_entry->phys);
3701 if (sglq_entry->virt == NULL) {
3702 kfree(sglq_entry);
3703 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3704 "2563 Failure to allocate an "
3705 "ELS mbuf:%d\n", i);
3706 rc = -ENOMEM;
3707 goto out_free_mem;
3708 }
3709 sglq_entry->sgl = sglq_entry->virt;
3710 memset(sglq_entry->sgl, 0, LPFC_BPL_SIZE);
3711 sglq_entry->state = SGL_FREED;
3712 list_add_tail(&sglq_entry->list, &els_sgl_list);
3713 }
James Smart38c20672013-03-01 16:37:44 -05003714 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -08003715 spin_lock(&phba->sli4_hba.sgl_list_lock);
3716 list_splice_init(&els_sgl_list,
3717 &phba->sli4_hba.lpfc_els_sgl_list);
3718 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smart38c20672013-03-01 16:37:44 -05003719 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04003720 } else if (els_xri_cnt < phba->sli4_hba.els_xri_cnt) {
3721 /* els xri-sgl shrinked */
3722 xri_cnt = phba->sli4_hba.els_xri_cnt - els_xri_cnt;
3723 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3724 "3158 ELS xri-sgl count decreased from "
3725 "%d to %d\n", phba->sli4_hba.els_xri_cnt,
3726 els_xri_cnt);
3727 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -08003728 spin_lock(&phba->sli4_hba.sgl_list_lock);
3729 list_splice_init(&phba->sli4_hba.lpfc_els_sgl_list,
3730 &els_sgl_list);
James Smart8a9d2e82012-05-09 21:16:12 -04003731 /* release extra els sgls from list */
3732 for (i = 0; i < xri_cnt; i++) {
3733 list_remove_head(&els_sgl_list,
3734 sglq_entry, struct lpfc_sglq, list);
3735 if (sglq_entry) {
James Smart895427b2017-02-12 13:52:30 -08003736 __lpfc_mbuf_free(phba, sglq_entry->virt,
3737 sglq_entry->phys);
James Smart8a9d2e82012-05-09 21:16:12 -04003738 kfree(sglq_entry);
3739 }
3740 }
James Smart895427b2017-02-12 13:52:30 -08003741 list_splice_init(&els_sgl_list,
3742 &phba->sli4_hba.lpfc_els_sgl_list);
3743 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04003744 spin_unlock_irq(&phba->hbalock);
3745 } else
3746 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3747 "3163 ELS xri-sgl count unchanged: %d\n",
3748 els_xri_cnt);
3749 phba->sli4_hba.els_xri_cnt = els_xri_cnt;
3750
3751 /* update xris to els sgls on the list */
3752 sglq_entry = NULL;
3753 sglq_entry_next = NULL;
3754 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
James Smart895427b2017-02-12 13:52:30 -08003755 &phba->sli4_hba.lpfc_els_sgl_list, list) {
James Smart8a9d2e82012-05-09 21:16:12 -04003756 lxri = lpfc_sli4_next_xritag(phba);
3757 if (lxri == NO_XRI) {
3758 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3759 "2400 Failed to allocate xri for "
3760 "ELS sgl\n");
3761 rc = -ENOMEM;
3762 goto out_free_mem;
3763 }
3764 sglq_entry->sli4_lxritag = lxri;
3765 sglq_entry->sli4_xritag = phba->sli4_hba.xri_ids[lxri];
3766 }
James Smart895427b2017-02-12 13:52:30 -08003767 return 0;
3768
3769out_free_mem:
3770 lpfc_free_els_sgl_list(phba);
3771 return rc;
3772}
3773
3774/**
James Smartf358dd02017-02-12 13:52:34 -08003775 * lpfc_sli4_nvmet_sgl_update - update xri-sgl sizing and mapping
3776 * @phba: pointer to lpfc hba data structure.
3777 *
3778 * This routine first calculates the sizes of the current els and allocated
3779 * scsi sgl lists, and then goes through all sgls to updates the physical
3780 * XRIs assigned due to port function reset. During port initialization, the
3781 * current els and allocated scsi sgl lists are 0s.
3782 *
3783 * Return codes
3784 * 0 - successful (for now, it always returns 0)
3785 **/
3786int
3787lpfc_sli4_nvmet_sgl_update(struct lpfc_hba *phba)
3788{
3789 struct lpfc_sglq *sglq_entry = NULL, *sglq_entry_next = NULL;
3790 uint16_t i, lxri, xri_cnt, els_xri_cnt;
James Smart6c621a22017-05-15 15:20:45 -07003791 uint16_t nvmet_xri_cnt;
James Smartf358dd02017-02-12 13:52:34 -08003792 LIST_HEAD(nvmet_sgl_list);
3793 int rc;
3794
3795 /*
3796 * update on pci function's nvmet xri-sgl list
3797 */
3798 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
James Smart61f3d4b2017-05-15 15:20:41 -07003799
James Smart6c621a22017-05-15 15:20:45 -07003800 /* For NVMET, ALL remaining XRIs are dedicated for IO processing */
3801 nvmet_xri_cnt = phba->sli4_hba.max_cfg_param.max_xri - els_xri_cnt;
James Smartf358dd02017-02-12 13:52:34 -08003802 if (nvmet_xri_cnt > phba->sli4_hba.nvmet_xri_cnt) {
3803 /* els xri-sgl expanded */
3804 xri_cnt = nvmet_xri_cnt - phba->sli4_hba.nvmet_xri_cnt;
3805 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3806 "6302 NVMET xri-sgl cnt grew from %d to %d\n",
3807 phba->sli4_hba.nvmet_xri_cnt, nvmet_xri_cnt);
3808 /* allocate the additional nvmet sgls */
3809 for (i = 0; i < xri_cnt; i++) {
3810 sglq_entry = kzalloc(sizeof(struct lpfc_sglq),
3811 GFP_KERNEL);
3812 if (sglq_entry == NULL) {
3813 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3814 "6303 Failure to allocate an "
3815 "NVMET sgl entry:%d\n", i);
3816 rc = -ENOMEM;
3817 goto out_free_mem;
3818 }
3819 sglq_entry->buff_type = NVMET_BUFF_TYPE;
3820 sglq_entry->virt = lpfc_nvmet_buf_alloc(phba, 0,
3821 &sglq_entry->phys);
3822 if (sglq_entry->virt == NULL) {
3823 kfree(sglq_entry);
3824 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3825 "6304 Failure to allocate an "
3826 "NVMET buf:%d\n", i);
3827 rc = -ENOMEM;
3828 goto out_free_mem;
3829 }
3830 sglq_entry->sgl = sglq_entry->virt;
3831 memset(sglq_entry->sgl, 0,
3832 phba->cfg_sg_dma_buf_size);
3833 sglq_entry->state = SGL_FREED;
3834 list_add_tail(&sglq_entry->list, &nvmet_sgl_list);
3835 }
3836 spin_lock_irq(&phba->hbalock);
3837 spin_lock(&phba->sli4_hba.sgl_list_lock);
3838 list_splice_init(&nvmet_sgl_list,
3839 &phba->sli4_hba.lpfc_nvmet_sgl_list);
3840 spin_unlock(&phba->sli4_hba.sgl_list_lock);
3841 spin_unlock_irq(&phba->hbalock);
3842 } else if (nvmet_xri_cnt < phba->sli4_hba.nvmet_xri_cnt) {
3843 /* nvmet xri-sgl shrunk */
3844 xri_cnt = phba->sli4_hba.nvmet_xri_cnt - nvmet_xri_cnt;
3845 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3846 "6305 NVMET xri-sgl count decreased from "
3847 "%d to %d\n", phba->sli4_hba.nvmet_xri_cnt,
3848 nvmet_xri_cnt);
3849 spin_lock_irq(&phba->hbalock);
3850 spin_lock(&phba->sli4_hba.sgl_list_lock);
3851 list_splice_init(&phba->sli4_hba.lpfc_nvmet_sgl_list,
3852 &nvmet_sgl_list);
3853 /* release extra nvmet sgls from list */
3854 for (i = 0; i < xri_cnt; i++) {
3855 list_remove_head(&nvmet_sgl_list,
3856 sglq_entry, struct lpfc_sglq, list);
3857 if (sglq_entry) {
3858 lpfc_nvmet_buf_free(phba, sglq_entry->virt,
3859 sglq_entry->phys);
3860 kfree(sglq_entry);
3861 }
3862 }
3863 list_splice_init(&nvmet_sgl_list,
3864 &phba->sli4_hba.lpfc_nvmet_sgl_list);
3865 spin_unlock(&phba->sli4_hba.sgl_list_lock);
3866 spin_unlock_irq(&phba->hbalock);
3867 } else
3868 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3869 "6306 NVMET xri-sgl count unchanged: %d\n",
3870 nvmet_xri_cnt);
3871 phba->sli4_hba.nvmet_xri_cnt = nvmet_xri_cnt;
3872
3873 /* update xris to nvmet sgls on the list */
3874 sglq_entry = NULL;
3875 sglq_entry_next = NULL;
3876 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
3877 &phba->sli4_hba.lpfc_nvmet_sgl_list, list) {
3878 lxri = lpfc_sli4_next_xritag(phba);
3879 if (lxri == NO_XRI) {
3880 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3881 "6307 Failed to allocate xri for "
3882 "NVMET sgl\n");
3883 rc = -ENOMEM;
3884 goto out_free_mem;
3885 }
3886 sglq_entry->sli4_lxritag = lxri;
3887 sglq_entry->sli4_xritag = phba->sli4_hba.xri_ids[lxri];
3888 }
3889 return 0;
3890
3891out_free_mem:
3892 lpfc_free_nvmet_sgl_list(phba);
3893 return rc;
3894}
3895
James Smart5e5b5112019-01-28 11:14:22 -08003896int
3897lpfc_io_buf_flush(struct lpfc_hba *phba, struct list_head *cbuf)
3898{
3899 LIST_HEAD(blist);
3900 struct lpfc_sli4_hdw_queue *qp;
James Smartc4908502019-01-28 11:14:28 -08003901 struct lpfc_io_buf *lpfc_cmd;
3902 struct lpfc_io_buf *iobufp, *prev_iobufp;
James Smart5e5b5112019-01-28 11:14:22 -08003903 int idx, cnt, xri, inserted;
3904
3905 cnt = 0;
3906 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
3907 qp = &phba->sli4_hba.hdwq[idx];
3908 spin_lock_irq(&qp->io_buf_list_get_lock);
3909 spin_lock(&qp->io_buf_list_put_lock);
3910
3911 /* Take everything off the get and put lists */
3912 list_splice_init(&qp->lpfc_io_buf_list_get, &blist);
3913 list_splice(&qp->lpfc_io_buf_list_put, &blist);
3914 INIT_LIST_HEAD(&qp->lpfc_io_buf_list_get);
3915 INIT_LIST_HEAD(&qp->lpfc_io_buf_list_put);
3916 cnt += qp->get_io_bufs + qp->put_io_bufs;
3917 qp->get_io_bufs = 0;
3918 qp->put_io_bufs = 0;
3919 qp->total_io_bufs = 0;
3920 spin_unlock(&qp->io_buf_list_put_lock);
3921 spin_unlock_irq(&qp->io_buf_list_get_lock);
3922 }
3923
3924 /*
3925 * Take IO buffers off blist and put on cbuf sorted by XRI.
3926 * This is because POST_SGL takes a sequential range of XRIs
3927 * to post to the firmware.
3928 */
3929 for (idx = 0; idx < cnt; idx++) {
James Smartc4908502019-01-28 11:14:28 -08003930 list_remove_head(&blist, lpfc_cmd, struct lpfc_io_buf, list);
James Smart5e5b5112019-01-28 11:14:22 -08003931 if (!lpfc_cmd)
3932 return cnt;
3933 if (idx == 0) {
3934 list_add_tail(&lpfc_cmd->list, cbuf);
3935 continue;
3936 }
3937 xri = lpfc_cmd->cur_iocbq.sli4_xritag;
3938 inserted = 0;
3939 prev_iobufp = NULL;
3940 list_for_each_entry(iobufp, cbuf, list) {
3941 if (xri < iobufp->cur_iocbq.sli4_xritag) {
3942 if (prev_iobufp)
3943 list_add(&lpfc_cmd->list,
3944 &prev_iobufp->list);
3945 else
3946 list_add(&lpfc_cmd->list, cbuf);
3947 inserted = 1;
3948 break;
3949 }
3950 prev_iobufp = iobufp;
3951 }
3952 if (!inserted)
3953 list_add_tail(&lpfc_cmd->list, cbuf);
3954 }
3955 return cnt;
3956}
3957
3958int
3959lpfc_io_buf_replenish(struct lpfc_hba *phba, struct list_head *cbuf)
3960{
3961 struct lpfc_sli4_hdw_queue *qp;
James Smartc4908502019-01-28 11:14:28 -08003962 struct lpfc_io_buf *lpfc_cmd;
James Smart5e5b5112019-01-28 11:14:22 -08003963 int idx, cnt;
3964
3965 qp = phba->sli4_hba.hdwq;
3966 cnt = 0;
3967 while (!list_empty(cbuf)) {
3968 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
3969 list_remove_head(cbuf, lpfc_cmd,
James Smartc4908502019-01-28 11:14:28 -08003970 struct lpfc_io_buf, list);
James Smart5e5b5112019-01-28 11:14:22 -08003971 if (!lpfc_cmd)
3972 return cnt;
3973 cnt++;
3974 qp = &phba->sli4_hba.hdwq[idx];
James Smart1fbf9742019-01-28 11:14:26 -08003975 lpfc_cmd->hdwq_no = idx;
3976 lpfc_cmd->hdwq = qp;
James Smart5e5b5112019-01-28 11:14:22 -08003977 lpfc_cmd->cur_iocbq.wqe_cmpl = NULL;
3978 lpfc_cmd->cur_iocbq.iocb_cmpl = NULL;
3979 spin_lock(&qp->io_buf_list_put_lock);
3980 list_add_tail(&lpfc_cmd->list,
3981 &qp->lpfc_io_buf_list_put);
3982 qp->put_io_bufs++;
3983 qp->total_io_bufs++;
3984 spin_unlock(&qp->io_buf_list_put_lock);
3985 }
3986 }
3987 return cnt;
3988}
3989
James Smartf358dd02017-02-12 13:52:34 -08003990/**
James Smart5e5b5112019-01-28 11:14:22 -08003991 * lpfc_sli4_io_sgl_update - update xri-sgl sizing and mapping
James Smart895427b2017-02-12 13:52:30 -08003992 * @phba: pointer to lpfc hba data structure.
3993 *
3994 * This routine first calculates the sizes of the current els and allocated
3995 * scsi sgl lists, and then goes through all sgls to updates the physical
3996 * XRIs assigned due to port function reset. During port initialization, the
3997 * current els and allocated scsi sgl lists are 0s.
3998 *
3999 * Return codes
4000 * 0 - successful (for now, it always returns 0)
4001 **/
4002int
James Smart5e5b5112019-01-28 11:14:22 -08004003lpfc_sli4_io_sgl_update(struct lpfc_hba *phba)
James Smart895427b2017-02-12 13:52:30 -08004004{
James Smartc4908502019-01-28 11:14:28 -08004005 struct lpfc_io_buf *lpfc_ncmd = NULL, *lpfc_ncmd_next = NULL;
James Smart0794d602019-01-28 11:14:19 -08004006 uint16_t i, lxri, els_xri_cnt;
James Smart5e5b5112019-01-28 11:14:22 -08004007 uint16_t io_xri_cnt, io_xri_max;
4008 LIST_HEAD(io_sgl_list);
James Smart0794d602019-01-28 11:14:19 -08004009 int rc, cnt;
James Smart895427b2017-02-12 13:52:30 -08004010
4011 /*
James Smart0794d602019-01-28 11:14:19 -08004012 * update on pci function's allocated nvme xri-sgl list
James Smart895427b2017-02-12 13:52:30 -08004013 */
James Smart0794d602019-01-28 11:14:19 -08004014
4015 /* maximum number of xris available for nvme buffers */
James Smart895427b2017-02-12 13:52:30 -08004016 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
James Smart5e5b5112019-01-28 11:14:22 -08004017 io_xri_max = phba->sli4_hba.max_cfg_param.max_xri - els_xri_cnt;
4018 phba->sli4_hba.io_xri_max = io_xri_max;
James Smart8a9d2e82012-05-09 21:16:12 -04004019
James Smarte8c0a772017-04-21 16:04:49 -07004020 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart0794d602019-01-28 11:14:19 -08004021 "6074 Current allocated XRI sgl count:%d, "
4022 "maximum XRI count:%d\n",
James Smart5e5b5112019-01-28 11:14:22 -08004023 phba->sli4_hba.io_xri_cnt,
4024 phba->sli4_hba.io_xri_max);
James Smarte8c0a772017-04-21 16:04:49 -07004025
James Smart5e5b5112019-01-28 11:14:22 -08004026 cnt = lpfc_io_buf_flush(phba, &io_sgl_list);
James Smart0794d602019-01-28 11:14:19 -08004027
James Smart5e5b5112019-01-28 11:14:22 -08004028 if (phba->sli4_hba.io_xri_cnt > phba->sli4_hba.io_xri_max) {
James Smart0794d602019-01-28 11:14:19 -08004029 /* max nvme xri shrunk below the allocated nvme buffers */
James Smart5e5b5112019-01-28 11:14:22 -08004030 io_xri_cnt = phba->sli4_hba.io_xri_cnt -
4031 phba->sli4_hba.io_xri_max;
James Smart0794d602019-01-28 11:14:19 -08004032 /* release the extra allocated nvme buffers */
James Smart5e5b5112019-01-28 11:14:22 -08004033 for (i = 0; i < io_xri_cnt; i++) {
4034 list_remove_head(&io_sgl_list, lpfc_ncmd,
James Smartc4908502019-01-28 11:14:28 -08004035 struct lpfc_io_buf, list);
James Smart0794d602019-01-28 11:14:19 -08004036 if (lpfc_ncmd) {
Romain Perier771db5c2017-07-06 10:13:05 +02004037 dma_pool_free(phba->lpfc_sg_dma_buf_pool,
James Smart0794d602019-01-28 11:14:19 -08004038 lpfc_ncmd->data,
4039 lpfc_ncmd->dma_handle);
4040 kfree(lpfc_ncmd);
James Smarta2fc4aef2014-09-03 12:57:55 -04004041 }
James Smart8a9d2e82012-05-09 21:16:12 -04004042 }
James Smart5e5b5112019-01-28 11:14:22 -08004043 phba->sli4_hba.io_xri_cnt -= io_xri_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04004044 }
4045
James Smart0794d602019-01-28 11:14:19 -08004046 /* update xris associated to remaining allocated nvme buffers */
4047 lpfc_ncmd = NULL;
4048 lpfc_ncmd_next = NULL;
James Smart5e5b5112019-01-28 11:14:22 -08004049 phba->sli4_hba.io_xri_cnt = cnt;
James Smart0794d602019-01-28 11:14:19 -08004050 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
James Smart5e5b5112019-01-28 11:14:22 -08004051 &io_sgl_list, list) {
James Smart8a9d2e82012-05-09 21:16:12 -04004052 lxri = lpfc_sli4_next_xritag(phba);
4053 if (lxri == NO_XRI) {
4054 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart0794d602019-01-28 11:14:19 -08004055 "6075 Failed to allocate xri for "
4056 "nvme buffer\n");
James Smart8a9d2e82012-05-09 21:16:12 -04004057 rc = -ENOMEM;
4058 goto out_free_mem;
4059 }
James Smart0794d602019-01-28 11:14:19 -08004060 lpfc_ncmd->cur_iocbq.sli4_lxritag = lxri;
4061 lpfc_ncmd->cur_iocbq.sli4_xritag = phba->sli4_hba.xri_ids[lxri];
James Smart8a9d2e82012-05-09 21:16:12 -04004062 }
James Smart5e5b5112019-01-28 11:14:22 -08004063 cnt = lpfc_io_buf_replenish(phba, &io_sgl_list);
dea31012005-04-17 16:05:31 -05004064 return 0;
James Smart8a9d2e82012-05-09 21:16:12 -04004065
4066out_free_mem:
James Smart5e5b5112019-01-28 11:14:22 -08004067 lpfc_io_free(phba);
James Smart8a9d2e82012-05-09 21:16:12 -04004068 return rc;
dea31012005-04-17 16:05:31 -05004069}
4070
James Smart0794d602019-01-28 11:14:19 -08004071/**
James Smart5e5b5112019-01-28 11:14:22 -08004072 * lpfc_new_io_buf - IO buffer allocator for HBA with SLI4 IF spec
James Smart0794d602019-01-28 11:14:19 -08004073 * @vport: The virtual port for which this call being executed.
4074 * @num_to_allocate: The requested number of buffers to allocate.
4075 *
4076 * This routine allocates nvme buffers for device with SLI-4 interface spec,
4077 * the nvme buffer contains all the necessary information needed to initiate
4078 * an I/O. After allocating up to @num_to_allocate IO buffers and put
4079 * them on a list, it post them to the port by using SGL block post.
4080 *
4081 * Return codes:
James Smart5e5b5112019-01-28 11:14:22 -08004082 * int - number of IO buffers that were allocated and posted.
James Smart0794d602019-01-28 11:14:19 -08004083 * 0 = failure, less than num_to_alloc is a partial failure.
4084 **/
4085int
James Smart5e5b5112019-01-28 11:14:22 -08004086lpfc_new_io_buf(struct lpfc_hba *phba, int num_to_alloc)
James Smart0794d602019-01-28 11:14:19 -08004087{
James Smartc4908502019-01-28 11:14:28 -08004088 struct lpfc_io_buf *lpfc_ncmd;
James Smart0794d602019-01-28 11:14:19 -08004089 struct lpfc_iocbq *pwqeq;
4090 uint16_t iotag, lxri = 0;
4091 int bcnt, num_posted;
4092 LIST_HEAD(prep_nblist);
4093 LIST_HEAD(post_nblist);
4094 LIST_HEAD(nvme_nblist);
4095
James Smart5e5b5112019-01-28 11:14:22 -08004096 phba->sli4_hba.io_xri_cnt = 0;
James Smart0794d602019-01-28 11:14:19 -08004097 for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
James Smart7f9989b2019-08-27 14:27:46 -07004098 lpfc_ncmd = kzalloc(sizeof(*lpfc_ncmd), GFP_KERNEL);
James Smart0794d602019-01-28 11:14:19 -08004099 if (!lpfc_ncmd)
4100 break;
4101 /*
4102 * Get memory from the pci pool to map the virt space to
4103 * pci bus space for an I/O. The DMA buffer includes the
4104 * number of SGE's necessary to support the sg_tablesize.
4105 */
Thomas Meyera5c990e2019-05-29 22:21:36 +02004106 lpfc_ncmd->data = dma_pool_zalloc(phba->lpfc_sg_dma_buf_pool,
4107 GFP_KERNEL,
4108 &lpfc_ncmd->dma_handle);
James Smart0794d602019-01-28 11:14:19 -08004109 if (!lpfc_ncmd->data) {
4110 kfree(lpfc_ncmd);
4111 break;
4112 }
James Smart0794d602019-01-28 11:14:19 -08004113
James Smartd79c9e92019-08-14 16:57:09 -07004114 if (phba->cfg_xpsgl && !phba->nvmet_support) {
4115 INIT_LIST_HEAD(&lpfc_ncmd->dma_sgl_xtra_list);
4116 } else {
4117 /*
4118 * 4K Page alignment is CRITICAL to BlockGuard, double
4119 * check to be sure.
4120 */
4121 if ((phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
4122 (((unsigned long)(lpfc_ncmd->data) &
4123 (unsigned long)(SLI4_PAGE_SIZE - 1)) != 0)) {
4124 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
4125 "3369 Memory alignment err: "
4126 "addr=%lx\n",
4127 (unsigned long)lpfc_ncmd->data);
4128 dma_pool_free(phba->lpfc_sg_dma_buf_pool,
4129 lpfc_ncmd->data,
4130 lpfc_ncmd->dma_handle);
4131 kfree(lpfc_ncmd);
4132 break;
4133 }
James Smart0794d602019-01-28 11:14:19 -08004134 }
4135
James Smartd79c9e92019-08-14 16:57:09 -07004136 INIT_LIST_HEAD(&lpfc_ncmd->dma_cmd_rsp_list);
4137
James Smart0794d602019-01-28 11:14:19 -08004138 lxri = lpfc_sli4_next_xritag(phba);
4139 if (lxri == NO_XRI) {
4140 dma_pool_free(phba->lpfc_sg_dma_buf_pool,
4141 lpfc_ncmd->data, lpfc_ncmd->dma_handle);
4142 kfree(lpfc_ncmd);
4143 break;
4144 }
4145 pwqeq = &lpfc_ncmd->cur_iocbq;
4146
4147 /* Allocate iotag for lpfc_ncmd->cur_iocbq. */
4148 iotag = lpfc_sli_next_iotag(phba, pwqeq);
4149 if (iotag == 0) {
4150 dma_pool_free(phba->lpfc_sg_dma_buf_pool,
4151 lpfc_ncmd->data, lpfc_ncmd->dma_handle);
4152 kfree(lpfc_ncmd);
4153 lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
4154 "6121 Failed to allocate IOTAG for"
4155 " XRI:0x%x\n", lxri);
4156 lpfc_sli4_free_xri(phba, lxri);
4157 break;
4158 }
4159 pwqeq->sli4_lxritag = lxri;
4160 pwqeq->sli4_xritag = phba->sli4_hba.xri_ids[lxri];
4161 pwqeq->context1 = lpfc_ncmd;
4162
4163 /* Initialize local short-hand pointers. */
4164 lpfc_ncmd->dma_sgl = lpfc_ncmd->data;
4165 lpfc_ncmd->dma_phys_sgl = lpfc_ncmd->dma_handle;
4166 lpfc_ncmd->cur_iocbq.context1 = lpfc_ncmd;
James Smartc2017262019-01-28 11:14:37 -08004167 spin_lock_init(&lpfc_ncmd->buf_lock);
James Smart0794d602019-01-28 11:14:19 -08004168
4169 /* add the nvme buffer to a post list */
4170 list_add_tail(&lpfc_ncmd->list, &post_nblist);
James Smart5e5b5112019-01-28 11:14:22 -08004171 phba->sli4_hba.io_xri_cnt++;
James Smart0794d602019-01-28 11:14:19 -08004172 }
4173 lpfc_printf_log(phba, KERN_INFO, LOG_NVME,
4174 "6114 Allocate %d out of %d requested new NVME "
4175 "buffers\n", bcnt, num_to_alloc);
4176
4177 /* post the list of nvme buffer sgls to port if available */
4178 if (!list_empty(&post_nblist))
James Smart5e5b5112019-01-28 11:14:22 -08004179 num_posted = lpfc_sli4_post_io_sgl_list(
James Smart0794d602019-01-28 11:14:19 -08004180 phba, &post_nblist, bcnt);
4181 else
4182 num_posted = 0;
4183
4184 return num_posted;
4185}
4186
James Smart96418b52017-03-04 09:30:31 -08004187static uint64_t
4188lpfc_get_wwpn(struct lpfc_hba *phba)
4189{
4190 uint64_t wwn;
4191 int rc;
4192 LPFC_MBOXQ_t *mboxq;
4193 MAILBOX_t *mb;
4194
James Smart96418b52017-03-04 09:30:31 -08004195 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
4196 GFP_KERNEL);
4197 if (!mboxq)
4198 return (uint64_t)-1;
4199
4200 /* First get WWN of HBA instance */
4201 lpfc_read_nv(phba, mboxq);
4202 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4203 if (rc != MBX_SUCCESS) {
4204 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4205 "6019 Mailbox failed , mbxCmd x%x "
4206 "READ_NV, mbxStatus x%x\n",
4207 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
4208 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
4209 mempool_free(mboxq, phba->mbox_mem_pool);
4210 return (uint64_t) -1;
4211 }
4212 mb = &mboxq->u.mb;
4213 memcpy(&wwn, (char *)mb->un.varRDnvp.portname, sizeof(uint64_t));
4214 /* wwn is WWPN of HBA instance */
4215 mempool_free(mboxq, phba->mbox_mem_pool);
4216 if (phba->sli_rev == LPFC_SLI_REV4)
4217 return be64_to_cpu(wwn);
4218 else
Maurizio Lombardi286871a2017-08-23 16:55:48 -07004219 return rol64(wwn, 32);
James Smart96418b52017-03-04 09:30:31 -08004220}
4221
James Smarte59058c2008-08-24 21:49:00 -04004222/**
James Smart3621a712009-04-06 18:47:14 -04004223 * lpfc_create_port - Create an FC port
James Smarte59058c2008-08-24 21:49:00 -04004224 * @phba: pointer to lpfc hba data structure.
4225 * @instance: a unique integer ID to this FC port.
4226 * @dev: pointer to the device data structure.
4227 *
4228 * This routine creates a FC port for the upper layer protocol. The FC port
4229 * can be created on top of either a physical port or a virtual port provided
4230 * by the HBA. This routine also allocates a SCSI host data structure (shost)
4231 * and associates the FC port created before adding the shost into the SCSI
4232 * layer.
4233 *
4234 * Return codes
4235 * @vport - pointer to the virtual N_Port data structure.
4236 * NULL - port create failed.
4237 **/
James Smart2e0fef82007-06-17 19:56:36 -05004238struct lpfc_vport *
James Smart3de2a652007-08-02 11:09:59 -04004239lpfc_create_port(struct lpfc_hba *phba, int instance, struct device *dev)
James Smart47a86172007-04-25 09:53:22 -04004240{
James Smart2e0fef82007-06-17 19:56:36 -05004241 struct lpfc_vport *vport;
James Smart895427b2017-02-12 13:52:30 -08004242 struct Scsi_Host *shost = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05004243 int error = 0;
James Smart96418b52017-03-04 09:30:31 -08004244 int i;
4245 uint64_t wwn;
4246 bool use_no_reset_hba = false;
James Smart56bc8022017-06-15 22:56:43 -07004247 int rc;
James Smart96418b52017-03-04 09:30:31 -08004248
James Smart56bc8022017-06-15 22:56:43 -07004249 if (lpfc_no_hba_reset_cnt) {
4250 if (phba->sli_rev < LPFC_SLI_REV4 &&
4251 dev == &phba->pcidev->dev) {
4252 /* Reset the port first */
4253 lpfc_sli_brdrestart(phba);
4254 rc = lpfc_sli_chipset_init(phba);
4255 if (rc)
4256 return NULL;
4257 }
4258 wwn = lpfc_get_wwpn(phba);
4259 }
James Smart96418b52017-03-04 09:30:31 -08004260
4261 for (i = 0; i < lpfc_no_hba_reset_cnt; i++) {
4262 if (wwn == lpfc_no_hba_reset[i]) {
4263 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4264 "6020 Setting use_no_reset port=%llx\n",
4265 wwn);
4266 use_no_reset_hba = true;
4267 break;
4268 }
4269 }
James Smart47a86172007-04-25 09:53:22 -04004270
James Smart895427b2017-02-12 13:52:30 -08004271 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) {
4272 if (dev != &phba->pcidev->dev) {
4273 shost = scsi_host_alloc(&lpfc_vport_template,
4274 sizeof(struct lpfc_vport));
4275 } else {
James Smart96418b52017-03-04 09:30:31 -08004276 if (!use_no_reset_hba)
James Smart895427b2017-02-12 13:52:30 -08004277 shost = scsi_host_alloc(&lpfc_template,
4278 sizeof(struct lpfc_vport));
4279 else
James Smart96418b52017-03-04 09:30:31 -08004280 shost = scsi_host_alloc(&lpfc_template_no_hr,
James Smart895427b2017-02-12 13:52:30 -08004281 sizeof(struct lpfc_vport));
4282 }
4283 } else if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
4284 shost = scsi_host_alloc(&lpfc_template_nvme,
James Smartea4142f2015-04-07 15:07:13 -04004285 sizeof(struct lpfc_vport));
4286 }
James Smart2e0fef82007-06-17 19:56:36 -05004287 if (!shost)
4288 goto out;
James Smart47a86172007-04-25 09:53:22 -04004289
James Smart2e0fef82007-06-17 19:56:36 -05004290 vport = (struct lpfc_vport *) shost->hostdata;
4291 vport->phba = phba;
James Smart2e0fef82007-06-17 19:56:36 -05004292 vport->load_flag |= FC_LOADING;
James Smart92d7f7b2007-06-17 19:56:38 -05004293 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart7f5f3d02008-02-08 18:50:14 -05004294 vport->fc_rscn_flush = 0;
James Smart3de2a652007-08-02 11:09:59 -04004295 lpfc_get_vport_cfgparam(vport);
James Smart895427b2017-02-12 13:52:30 -08004296
James Smartf6e84792019-01-28 11:14:38 -08004297 /* Adjust value in vport */
4298 vport->cfg_enable_fc4_type = phba->cfg_enable_fc4_type;
4299
James Smart2e0fef82007-06-17 19:56:36 -05004300 shost->unique_id = instance;
4301 shost->max_id = LPFC_MAX_TARGET;
James Smart3de2a652007-08-02 11:09:59 -04004302 shost->max_lun = vport->cfg_max_luns;
James Smart2e0fef82007-06-17 19:56:36 -05004303 shost->this_id = -1;
4304 shost->max_cmd_len = 16;
James Smart6a828b02019-01-28 11:14:31 -08004305
James Smartda0436e2009-05-22 14:51:39 -04004306 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart77ffd342019-08-15 19:36:49 -07004307 if (!phba->cfg_fcp_mq_threshold ||
4308 phba->cfg_fcp_mq_threshold > phba->cfg_hdw_queue)
4309 phba->cfg_fcp_mq_threshold = phba->cfg_hdw_queue;
4310
4311 shost->nr_hw_queues = min_t(int, 2 * num_possible_nodes(),
4312 phba->cfg_fcp_mq_threshold);
James Smart6a828b02019-01-28 11:14:31 -08004313
James Smart28baac72010-02-12 14:42:03 -05004314 shost->dma_boundary =
James Smartcb5172e2010-03-15 11:25:07 -04004315 phba->sli4_hba.pc_sli4_params.sge_supp_len-1;
James Smartd79c9e92019-08-14 16:57:09 -07004316
4317 if (phba->cfg_xpsgl && !phba->nvmet_support)
4318 shost->sg_tablesize = LPFC_MAX_SG_TABLESIZE;
4319 else
4320 shost->sg_tablesize = phba->cfg_scsi_seg_cnt;
James Smartace44e42019-01-28 11:14:27 -08004321 } else
4322 /* SLI-3 has a limited number of hardware queues (3),
4323 * thus there is only one for FCP processing.
4324 */
4325 shost->nr_hw_queues = 1;
James Smart81301a92008-12-04 22:39:46 -05004326
James Smart47a86172007-04-25 09:53:22 -04004327 /*
James Smart2e0fef82007-06-17 19:56:36 -05004328 * Set initial can_queue value since 0 is no longer supported and
4329 * scsi_add_host will fail. This will be adjusted later based on the
4330 * max xri value determined in hba setup.
James Smart47a86172007-04-25 09:53:22 -04004331 */
James Smart2e0fef82007-06-17 19:56:36 -05004332 shost->can_queue = phba->cfg_hba_queue_depth - 10;
James Smart3de2a652007-08-02 11:09:59 -04004333 if (dev != &phba->pcidev->dev) {
James Smart92d7f7b2007-06-17 19:56:38 -05004334 shost->transportt = lpfc_vport_transport_template;
4335 vport->port_type = LPFC_NPIV_PORT;
4336 } else {
4337 shost->transportt = lpfc_transport_template;
4338 vport->port_type = LPFC_PHYSICAL_PORT;
4339 }
James Smart47a86172007-04-25 09:53:22 -04004340
James Smart2e0fef82007-06-17 19:56:36 -05004341 /* Initialize all internally managed lists. */
4342 INIT_LIST_HEAD(&vport->fc_nodes);
James Smartda0436e2009-05-22 14:51:39 -04004343 INIT_LIST_HEAD(&vport->rcv_buffer_list);
James Smart2e0fef82007-06-17 19:56:36 -05004344 spin_lock_init(&vport->work_port_lock);
James Smart47a86172007-04-25 09:53:22 -04004345
Kees Cookf22eb4d2017-09-06 20:24:26 -07004346 timer_setup(&vport->fc_disctmo, lpfc_disc_timeout, 0);
James Smart47a86172007-04-25 09:53:22 -04004347
Kees Cookf22eb4d2017-09-06 20:24:26 -07004348 timer_setup(&vport->els_tmofunc, lpfc_els_timeout, 0);
James Smart92494142011-02-16 12:39:44 -05004349
Kees Cookf22eb4d2017-09-06 20:24:26 -07004350 timer_setup(&vport->delayed_disc_tmo, lpfc_delayed_disc_tmo, 0);
James Smart92494142011-02-16 12:39:44 -05004351
James Smartaa6ff302019-05-21 17:49:09 -07004352 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED)
4353 lpfc_setup_bg(phba, shost);
4354
James Bottomleyd139b9b2009-11-05 13:33:12 -06004355 error = scsi_add_host_with_dma(shost, dev, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05004356 if (error)
4357 goto out_put_shost;
James Smart47a86172007-04-25 09:53:22 -04004358
James Smart523128e2018-09-10 10:30:46 -07004359 spin_lock_irq(&phba->port_list_lock);
James Smart2e0fef82007-06-17 19:56:36 -05004360 list_add_tail(&vport->listentry, &phba->port_list);
James Smart523128e2018-09-10 10:30:46 -07004361 spin_unlock_irq(&phba->port_list_lock);
James Smart2e0fef82007-06-17 19:56:36 -05004362 return vport;
James Smart47a86172007-04-25 09:53:22 -04004363
James Smart2e0fef82007-06-17 19:56:36 -05004364out_put_shost:
4365 scsi_host_put(shost);
4366out:
4367 return NULL;
James Smart47a86172007-04-25 09:53:22 -04004368}
4369
James Smarte59058c2008-08-24 21:49:00 -04004370/**
James Smart3621a712009-04-06 18:47:14 -04004371 * destroy_port - destroy an FC port
James Smarte59058c2008-08-24 21:49:00 -04004372 * @vport: pointer to an lpfc virtual N_Port data structure.
4373 *
4374 * This routine destroys a FC port from the upper layer protocol. All the
4375 * resources associated with the port are released.
4376 **/
James Smart2e0fef82007-06-17 19:56:36 -05004377void
4378destroy_port(struct lpfc_vport *vport)
James Smart47a86172007-04-25 09:53:22 -04004379{
James Smart92d7f7b2007-06-17 19:56:38 -05004380 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4381 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04004382
James Smart858c9f62007-06-17 19:56:39 -05004383 lpfc_debugfs_terminate(vport);
James Smart92d7f7b2007-06-17 19:56:38 -05004384 fc_remove_host(shost);
4385 scsi_remove_host(shost);
James Smart47a86172007-04-25 09:53:22 -04004386
James Smart523128e2018-09-10 10:30:46 -07004387 spin_lock_irq(&phba->port_list_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05004388 list_del_init(&vport->listentry);
James Smart523128e2018-09-10 10:30:46 -07004389 spin_unlock_irq(&phba->port_list_lock);
James Smart47a86172007-04-25 09:53:22 -04004390
James Smart92d7f7b2007-06-17 19:56:38 -05004391 lpfc_cleanup(vport);
James Smart47a86172007-04-25 09:53:22 -04004392 return;
James Smart47a86172007-04-25 09:53:22 -04004393}
4394
James Smarte59058c2008-08-24 21:49:00 -04004395/**
James Smart3621a712009-04-06 18:47:14 -04004396 * lpfc_get_instance - Get a unique integer ID
James Smarte59058c2008-08-24 21:49:00 -04004397 *
4398 * This routine allocates a unique integer ID from lpfc_hba_index pool. It
4399 * uses the kernel idr facility to perform the task.
4400 *
4401 * Return codes:
4402 * instance - a unique integer ID allocated as the new instance.
4403 * -1 - lpfc get instance failed.
4404 **/
James Smart92d7f7b2007-06-17 19:56:38 -05004405int
4406lpfc_get_instance(void)
4407{
Tejun Heoab516032013-02-27 17:04:44 -08004408 int ret;
James Smart92d7f7b2007-06-17 19:56:38 -05004409
Tejun Heoab516032013-02-27 17:04:44 -08004410 ret = idr_alloc(&lpfc_hba_index, NULL, 0, 0, GFP_KERNEL);
4411 return ret < 0 ? -1 : ret;
James Smart92d7f7b2007-06-17 19:56:38 -05004412}
4413
James Smarte59058c2008-08-24 21:49:00 -04004414/**
James Smart3621a712009-04-06 18:47:14 -04004415 * lpfc_scan_finished - method for SCSI layer to detect whether scan is done
James Smarte59058c2008-08-24 21:49:00 -04004416 * @shost: pointer to SCSI host data structure.
4417 * @time: elapsed time of the scan in jiffies.
4418 *
4419 * This routine is called by the SCSI layer with a SCSI host to determine
4420 * whether the scan host is finished.
4421 *
4422 * Note: there is no scan_start function as adapter initialization will have
4423 * asynchronously kicked off the link initialization.
4424 *
4425 * Return codes
4426 * 0 - SCSI host scan is not over yet.
4427 * 1 - SCSI host scan is over.
4428 **/
James Smart47a86172007-04-25 09:53:22 -04004429int lpfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
4430{
James Smart2e0fef82007-06-17 19:56:36 -05004431 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4432 struct lpfc_hba *phba = vport->phba;
James Smart858c9f62007-06-17 19:56:39 -05004433 int stat = 0;
James Smart47a86172007-04-25 09:53:22 -04004434
James Smart858c9f62007-06-17 19:56:39 -05004435 spin_lock_irq(shost->host_lock);
4436
James Smart51ef4c22007-08-02 11:10:31 -04004437 if (vport->load_flag & FC_UNLOADING) {
James Smart858c9f62007-06-17 19:56:39 -05004438 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04004439 goto finished;
James Smart858c9f62007-06-17 19:56:39 -05004440 }
James Smart256ec0d2013-04-17 20:14:58 -04004441 if (time >= msecs_to_jiffies(30 * 1000)) {
James Smart2e0fef82007-06-17 19:56:36 -05004442 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004443 "0461 Scanning longer than 30 "
4444 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05004445 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04004446 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05004447 }
James Smart256ec0d2013-04-17 20:14:58 -04004448 if (time >= msecs_to_jiffies(15 * 1000) &&
4449 phba->link_state <= LPFC_LINK_DOWN) {
James Smart2e0fef82007-06-17 19:56:36 -05004450 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004451 "0465 Link down longer than 15 "
4452 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05004453 stat = 1;
James Smart2e0fef82007-06-17 19:56:36 -05004454 goto finished;
James Smart47a86172007-04-25 09:53:22 -04004455 }
4456
James Smart2e0fef82007-06-17 19:56:36 -05004457 if (vport->port_state != LPFC_VPORT_READY)
James Smart858c9f62007-06-17 19:56:39 -05004458 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05004459 if (vport->num_disc_nodes || vport->fc_prli_sent)
James Smart858c9f62007-06-17 19:56:39 -05004460 goto finished;
James Smart256ec0d2013-04-17 20:14:58 -04004461 if (vport->fc_map_cnt == 0 && time < msecs_to_jiffies(2 * 1000))
James Smart858c9f62007-06-17 19:56:39 -05004462 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05004463 if ((phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) != 0)
James Smart858c9f62007-06-17 19:56:39 -05004464 goto finished;
4465
4466 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04004467
4468finished:
James Smart858c9f62007-06-17 19:56:39 -05004469 spin_unlock_irq(shost->host_lock);
4470 return stat;
James Smart92d7f7b2007-06-17 19:56:38 -05004471}
4472
Bart Van Assche3999df72019-03-28 11:06:16 -07004473static void lpfc_host_supported_speeds_set(struct Scsi_Host *shost)
James Smartcd713482018-10-23 13:41:01 -07004474{
4475 struct lpfc_vport *vport = (struct lpfc_vport *)shost->hostdata;
4476 struct lpfc_hba *phba = vport->phba;
4477
4478 fc_host_supported_speeds(shost) = 0;
James Smart1dc5ec22018-10-23 13:41:11 -07004479 if (phba->lmt & LMT_128Gb)
4480 fc_host_supported_speeds(shost) |= FC_PORTSPEED_128GBIT;
James Smartcd713482018-10-23 13:41:01 -07004481 if (phba->lmt & LMT_64Gb)
4482 fc_host_supported_speeds(shost) |= FC_PORTSPEED_64GBIT;
4483 if (phba->lmt & LMT_32Gb)
4484 fc_host_supported_speeds(shost) |= FC_PORTSPEED_32GBIT;
4485 if (phba->lmt & LMT_16Gb)
4486 fc_host_supported_speeds(shost) |= FC_PORTSPEED_16GBIT;
4487 if (phba->lmt & LMT_10Gb)
4488 fc_host_supported_speeds(shost) |= FC_PORTSPEED_10GBIT;
4489 if (phba->lmt & LMT_8Gb)
4490 fc_host_supported_speeds(shost) |= FC_PORTSPEED_8GBIT;
4491 if (phba->lmt & LMT_4Gb)
4492 fc_host_supported_speeds(shost) |= FC_PORTSPEED_4GBIT;
4493 if (phba->lmt & LMT_2Gb)
4494 fc_host_supported_speeds(shost) |= FC_PORTSPEED_2GBIT;
4495 if (phba->lmt & LMT_1Gb)
4496 fc_host_supported_speeds(shost) |= FC_PORTSPEED_1GBIT;
4497}
4498
James Smarte59058c2008-08-24 21:49:00 -04004499/**
James Smart3621a712009-04-06 18:47:14 -04004500 * lpfc_host_attrib_init - Initialize SCSI host attributes on a FC port
James Smarte59058c2008-08-24 21:49:00 -04004501 * @shost: pointer to SCSI host data structure.
4502 *
4503 * This routine initializes a given SCSI host attributes on a FC port. The
4504 * SCSI host can be either on top of a physical port or a virtual port.
4505 **/
James Smart92d7f7b2007-06-17 19:56:38 -05004506void lpfc_host_attrib_init(struct Scsi_Host *shost)
4507{
4508 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4509 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04004510 /*
James Smart2e0fef82007-06-17 19:56:36 -05004511 * Set fixed host attributes. Must done after lpfc_sli_hba_setup().
James Smart47a86172007-04-25 09:53:22 -04004512 */
4513
James Smart2e0fef82007-06-17 19:56:36 -05004514 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
4515 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
James Smart47a86172007-04-25 09:53:22 -04004516 fc_host_supported_classes(shost) = FC_COS_CLASS3;
4517
4518 memset(fc_host_supported_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05004519 sizeof(fc_host_supported_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04004520 fc_host_supported_fc4s(shost)[2] = 1;
4521 fc_host_supported_fc4s(shost)[7] = 1;
4522
James Smart92d7f7b2007-06-17 19:56:38 -05004523 lpfc_vport_symbolic_node_name(vport, fc_host_symbolic_name(shost),
4524 sizeof fc_host_symbolic_name(shost));
James Smart47a86172007-04-25 09:53:22 -04004525
James Smartcd713482018-10-23 13:41:01 -07004526 lpfc_host_supported_speeds_set(shost);
James Smart47a86172007-04-25 09:53:22 -04004527
4528 fc_host_maxframe_size(shost) =
James Smart2e0fef82007-06-17 19:56:36 -05004529 (((uint32_t) vport->fc_sparam.cmn.bbRcvSizeMsb & 0x0F) << 8) |
4530 (uint32_t) vport->fc_sparam.cmn.bbRcvSizeLsb;
James Smart47a86172007-04-25 09:53:22 -04004531
Mike Christie0af5d702010-09-15 16:52:31 -05004532 fc_host_dev_loss_tmo(shost) = vport->cfg_devloss_tmo;
4533
James Smart47a86172007-04-25 09:53:22 -04004534 /* This value is also unchanging */
4535 memset(fc_host_active_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05004536 sizeof(fc_host_active_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04004537 fc_host_active_fc4s(shost)[2] = 1;
4538 fc_host_active_fc4s(shost)[7] = 1;
4539
James Smart92d7f7b2007-06-17 19:56:38 -05004540 fc_host_max_npiv_vports(shost) = phba->max_vpi;
James Smart47a86172007-04-25 09:53:22 -04004541 spin_lock_irq(shost->host_lock);
James Smart51ef4c22007-08-02 11:10:31 -04004542 vport->load_flag &= ~FC_LOADING;
James Smart47a86172007-04-25 09:53:22 -04004543 spin_unlock_irq(shost->host_lock);
James Smart47a86172007-04-25 09:53:22 -04004544}
dea31012005-04-17 16:05:31 -05004545
James Smarte59058c2008-08-24 21:49:00 -04004546/**
James Smartda0436e2009-05-22 14:51:39 -04004547 * lpfc_stop_port_s3 - Stop SLI3 device port
James Smarte59058c2008-08-24 21:49:00 -04004548 * @phba: pointer to lpfc hba data structure.
4549 *
James Smartda0436e2009-05-22 14:51:39 -04004550 * This routine is invoked to stop an SLI3 device port, it stops the device
4551 * from generating interrupts and stops the device driver's timers for the
4552 * device.
James Smarte59058c2008-08-24 21:49:00 -04004553 **/
James Smartdb2378e2008-02-08 18:49:51 -05004554static void
James Smartda0436e2009-05-22 14:51:39 -04004555lpfc_stop_port_s3(struct lpfc_hba *phba)
James Smartdb2378e2008-02-08 18:49:51 -05004556{
James Smartda0436e2009-05-22 14:51:39 -04004557 /* Clear all interrupt enable conditions */
4558 writel(0, phba->HCregaddr);
4559 readl(phba->HCregaddr); /* flush */
4560 /* Clear all pending interrupts */
4561 writel(0xffffffff, phba->HAregaddr);
4562 readl(phba->HAregaddr); /* flush */
James Smart93996272008-08-24 21:50:30 -04004563
James Smartda0436e2009-05-22 14:51:39 -04004564 /* Reset some HBA SLI setup states */
4565 lpfc_stop_hba_timers(phba);
4566 phba->pport->work_port_events = 0;
James Smartdb2378e2008-02-08 18:49:51 -05004567}
4568
James Smarte59058c2008-08-24 21:49:00 -04004569/**
James Smartda0436e2009-05-22 14:51:39 -04004570 * lpfc_stop_port_s4 - Stop SLI4 device port
James Smart5b75da22008-12-04 22:39:35 -05004571 * @phba: pointer to lpfc hba data structure.
4572 *
James Smartda0436e2009-05-22 14:51:39 -04004573 * This routine is invoked to stop an SLI4 device port, it stops the device
4574 * from generating interrupts and stops the device driver's timers for the
4575 * device.
4576 **/
4577static void
4578lpfc_stop_port_s4(struct lpfc_hba *phba)
4579{
4580 /* Reset some HBA SLI4 setup states */
4581 lpfc_stop_hba_timers(phba);
James Smartcdb42be2019-01-28 11:14:21 -08004582 if (phba->pport)
4583 phba->pport->work_port_events = 0;
James Smartda0436e2009-05-22 14:51:39 -04004584 phba->sli4_hba.intr_enable = 0;
James Smartda0436e2009-05-22 14:51:39 -04004585}
4586
4587/**
4588 * lpfc_stop_port - Wrapper function for stopping hba port
4589 * @phba: Pointer to HBA context object.
James Smart5b75da22008-12-04 22:39:35 -05004590 *
James Smartda0436e2009-05-22 14:51:39 -04004591 * This routine wraps the actual SLI3 or SLI4 hba stop port routine from
4592 * the API jump table function pointer from the lpfc_hba struct.
4593 **/
4594void
4595lpfc_stop_port(struct lpfc_hba *phba)
4596{
4597 phba->lpfc_stop_port(phba);
Dick Kennedyf485c182017-09-29 17:34:34 -07004598
4599 if (phba->wq)
4600 flush_workqueue(phba->wq);
James Smartda0436e2009-05-22 14:51:39 -04004601}
4602
4603/**
James Smartecfd03c2010-02-12 14:41:27 -05004604 * lpfc_fcf_redisc_wait_start_timer - Start fcf rediscover wait timer
4605 * @phba: Pointer to hba for which this call is being executed.
4606 *
4607 * This routine starts the timer waiting for the FCF rediscovery to complete.
4608 **/
4609void
4610lpfc_fcf_redisc_wait_start_timer(struct lpfc_hba *phba)
4611{
4612 unsigned long fcf_redisc_wait_tmo =
4613 (jiffies + msecs_to_jiffies(LPFC_FCF_REDISCOVER_WAIT_TMO));
4614 /* Start fcf rediscovery wait period timer */
4615 mod_timer(&phba->fcf.redisc_wait, fcf_redisc_wait_tmo);
4616 spin_lock_irq(&phba->hbalock);
4617 /* Allow action to new fcf asynchronous event */
4618 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
4619 /* Mark the FCF rediscovery pending state */
4620 phba->fcf.fcf_flag |= FCF_REDISC_PEND;
4621 spin_unlock_irq(&phba->hbalock);
4622}
4623
4624/**
4625 * lpfc_sli4_fcf_redisc_wait_tmo - FCF table rediscover wait timeout
4626 * @ptr: Map to lpfc_hba data structure pointer.
4627 *
4628 * This routine is invoked when waiting for FCF table rediscover has been
4629 * timed out. If new FCF record(s) has (have) been discovered during the
4630 * wait period, a new FCF event shall be added to the FCOE async event
4631 * list, and then worker thread shall be waked up for processing from the
4632 * worker thread context.
4633 **/
Rashika Kheriae399b222014-09-03 12:55:28 -04004634static void
Kees Cookf22eb4d2017-09-06 20:24:26 -07004635lpfc_sli4_fcf_redisc_wait_tmo(struct timer_list *t)
James Smartecfd03c2010-02-12 14:41:27 -05004636{
Kees Cookf22eb4d2017-09-06 20:24:26 -07004637 struct lpfc_hba *phba = from_timer(phba, t, fcf.redisc_wait);
James Smartecfd03c2010-02-12 14:41:27 -05004638
4639 /* Don't send FCF rediscovery event if timer cancelled */
4640 spin_lock_irq(&phba->hbalock);
4641 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
4642 spin_unlock_irq(&phba->hbalock);
4643 return;
4644 }
4645 /* Clear FCF rediscovery timer pending flag */
4646 phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
4647 /* FCF rediscovery event to worker thread */
4648 phba->fcf.fcf_flag |= FCF_REDISC_EVT;
4649 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004650 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04004651 "2776 FCF rediscover quiescent timer expired\n");
James Smartecfd03c2010-02-12 14:41:27 -05004652 /* wake up worker thread */
4653 lpfc_worker_wake_up(phba);
4654}
4655
4656/**
James Smartda0436e2009-05-22 14:51:39 -04004657 * lpfc_sli4_parse_latt_fault - Parse sli4 link-attention link fault code
4658 * @phba: pointer to lpfc hba data structure.
4659 * @acqe_link: pointer to the async link completion queue entry.
4660 *
James Smart23288b72018-05-04 20:37:53 -07004661 * This routine is to parse the SLI4 link-attention link fault code.
James Smartda0436e2009-05-22 14:51:39 -04004662 **/
James Smart23288b72018-05-04 20:37:53 -07004663static void
James Smartda0436e2009-05-22 14:51:39 -04004664lpfc_sli4_parse_latt_fault(struct lpfc_hba *phba,
4665 struct lpfc_acqe_link *acqe_link)
4666{
James Smartda0436e2009-05-22 14:51:39 -04004667 switch (bf_get(lpfc_acqe_link_fault, acqe_link)) {
4668 case LPFC_ASYNC_LINK_FAULT_NONE:
4669 case LPFC_ASYNC_LINK_FAULT_LOCAL:
4670 case LPFC_ASYNC_LINK_FAULT_REMOTE:
James Smart23288b72018-05-04 20:37:53 -07004671 case LPFC_ASYNC_LINK_FAULT_LR_LRR:
James Smartda0436e2009-05-22 14:51:39 -04004672 break;
4673 default:
4674 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart23288b72018-05-04 20:37:53 -07004675 "0398 Unknown link fault code: x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04004676 bf_get(lpfc_acqe_link_fault, acqe_link));
James Smartda0436e2009-05-22 14:51:39 -04004677 break;
4678 }
James Smartda0436e2009-05-22 14:51:39 -04004679}
4680
4681/**
4682 * lpfc_sli4_parse_latt_type - Parse sli4 link attention type
4683 * @phba: pointer to lpfc hba data structure.
4684 * @acqe_link: pointer to the async link completion queue entry.
4685 *
4686 * This routine is to parse the SLI4 link attention type and translate it
4687 * into the base driver's link attention type coding.
4688 *
4689 * Return: Link attention type in terms of base driver's coding.
4690 **/
4691static uint8_t
4692lpfc_sli4_parse_latt_type(struct lpfc_hba *phba,
4693 struct lpfc_acqe_link *acqe_link)
4694{
4695 uint8_t att_type;
4696
4697 switch (bf_get(lpfc_acqe_link_status, acqe_link)) {
4698 case LPFC_ASYNC_LINK_STATUS_DOWN:
4699 case LPFC_ASYNC_LINK_STATUS_LOGICAL_DOWN:
James Smart76a95d72010-11-20 23:11:48 -05004700 att_type = LPFC_ATT_LINK_DOWN;
James Smartda0436e2009-05-22 14:51:39 -04004701 break;
4702 case LPFC_ASYNC_LINK_STATUS_UP:
4703 /* Ignore physical link up events - wait for logical link up */
James Smart76a95d72010-11-20 23:11:48 -05004704 att_type = LPFC_ATT_RESERVED;
James Smartda0436e2009-05-22 14:51:39 -04004705 break;
4706 case LPFC_ASYNC_LINK_STATUS_LOGICAL_UP:
James Smart76a95d72010-11-20 23:11:48 -05004707 att_type = LPFC_ATT_LINK_UP;
James Smartda0436e2009-05-22 14:51:39 -04004708 break;
4709 default:
4710 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4711 "0399 Invalid link attention type: x%x\n",
4712 bf_get(lpfc_acqe_link_status, acqe_link));
James Smart76a95d72010-11-20 23:11:48 -05004713 att_type = LPFC_ATT_RESERVED;
James Smartda0436e2009-05-22 14:51:39 -04004714 break;
4715 }
4716 return att_type;
4717}
4718
4719/**
James Smart8b68cd52012-09-29 11:32:37 -04004720 * lpfc_sli_port_speed_get - Get sli3 link speed code to link speed
4721 * @phba: pointer to lpfc hba data structure.
4722 *
4723 * This routine is to get an SLI3 FC port's link speed in Mbps.
4724 *
4725 * Return: link speed in terms of Mbps.
4726 **/
4727uint32_t
4728lpfc_sli_port_speed_get(struct lpfc_hba *phba)
4729{
4730 uint32_t link_speed;
4731
4732 if (!lpfc_is_link_up(phba))
4733 return 0;
4734
James Smarta085e872015-12-16 18:12:02 -05004735 if (phba->sli_rev <= LPFC_SLI_REV3) {
4736 switch (phba->fc_linkspeed) {
4737 case LPFC_LINK_SPEED_1GHZ:
4738 link_speed = 1000;
4739 break;
4740 case LPFC_LINK_SPEED_2GHZ:
4741 link_speed = 2000;
4742 break;
4743 case LPFC_LINK_SPEED_4GHZ:
4744 link_speed = 4000;
4745 break;
4746 case LPFC_LINK_SPEED_8GHZ:
4747 link_speed = 8000;
4748 break;
4749 case LPFC_LINK_SPEED_10GHZ:
4750 link_speed = 10000;
4751 break;
4752 case LPFC_LINK_SPEED_16GHZ:
4753 link_speed = 16000;
4754 break;
4755 default:
4756 link_speed = 0;
4757 }
4758 } else {
4759 if (phba->sli4_hba.link_state.logical_speed)
4760 link_speed =
4761 phba->sli4_hba.link_state.logical_speed;
4762 else
4763 link_speed = phba->sli4_hba.link_state.speed;
James Smart8b68cd52012-09-29 11:32:37 -04004764 }
4765 return link_speed;
4766}
4767
4768/**
4769 * lpfc_sli4_port_speed_parse - Parse async evt link speed code to link speed
4770 * @phba: pointer to lpfc hba data structure.
4771 * @evt_code: asynchronous event code.
4772 * @speed_code: asynchronous event link speed code.
4773 *
4774 * This routine is to parse the giving SLI4 async event link speed code into
4775 * value of Mbps for the link speed.
4776 *
4777 * Return: link speed in terms of Mbps.
4778 **/
4779static uint32_t
4780lpfc_sli4_port_speed_parse(struct lpfc_hba *phba, uint32_t evt_code,
4781 uint8_t speed_code)
4782{
4783 uint32_t port_speed;
4784
4785 switch (evt_code) {
4786 case LPFC_TRAILER_CODE_LINK:
4787 switch (speed_code) {
James Smart26d830e2015-04-07 15:07:17 -04004788 case LPFC_ASYNC_LINK_SPEED_ZERO:
James Smart8b68cd52012-09-29 11:32:37 -04004789 port_speed = 0;
4790 break;
James Smart26d830e2015-04-07 15:07:17 -04004791 case LPFC_ASYNC_LINK_SPEED_10MBPS:
James Smart8b68cd52012-09-29 11:32:37 -04004792 port_speed = 10;
4793 break;
James Smart26d830e2015-04-07 15:07:17 -04004794 case LPFC_ASYNC_LINK_SPEED_100MBPS:
James Smart8b68cd52012-09-29 11:32:37 -04004795 port_speed = 100;
4796 break;
James Smart26d830e2015-04-07 15:07:17 -04004797 case LPFC_ASYNC_LINK_SPEED_1GBPS:
James Smart8b68cd52012-09-29 11:32:37 -04004798 port_speed = 1000;
4799 break;
James Smart26d830e2015-04-07 15:07:17 -04004800 case LPFC_ASYNC_LINK_SPEED_10GBPS:
James Smart8b68cd52012-09-29 11:32:37 -04004801 port_speed = 10000;
4802 break;
James Smart26d830e2015-04-07 15:07:17 -04004803 case LPFC_ASYNC_LINK_SPEED_20GBPS:
4804 port_speed = 20000;
4805 break;
4806 case LPFC_ASYNC_LINK_SPEED_25GBPS:
4807 port_speed = 25000;
4808 break;
4809 case LPFC_ASYNC_LINK_SPEED_40GBPS:
4810 port_speed = 40000;
4811 break;
James Smart8b68cd52012-09-29 11:32:37 -04004812 default:
4813 port_speed = 0;
4814 }
4815 break;
4816 case LPFC_TRAILER_CODE_FC:
4817 switch (speed_code) {
James Smart26d830e2015-04-07 15:07:17 -04004818 case LPFC_FC_LA_SPEED_UNKNOWN:
James Smart8b68cd52012-09-29 11:32:37 -04004819 port_speed = 0;
4820 break;
James Smart26d830e2015-04-07 15:07:17 -04004821 case LPFC_FC_LA_SPEED_1G:
James Smart8b68cd52012-09-29 11:32:37 -04004822 port_speed = 1000;
4823 break;
James Smart26d830e2015-04-07 15:07:17 -04004824 case LPFC_FC_LA_SPEED_2G:
James Smart8b68cd52012-09-29 11:32:37 -04004825 port_speed = 2000;
4826 break;
James Smart26d830e2015-04-07 15:07:17 -04004827 case LPFC_FC_LA_SPEED_4G:
James Smart8b68cd52012-09-29 11:32:37 -04004828 port_speed = 4000;
4829 break;
James Smart26d830e2015-04-07 15:07:17 -04004830 case LPFC_FC_LA_SPEED_8G:
James Smart8b68cd52012-09-29 11:32:37 -04004831 port_speed = 8000;
4832 break;
James Smart26d830e2015-04-07 15:07:17 -04004833 case LPFC_FC_LA_SPEED_10G:
James Smart8b68cd52012-09-29 11:32:37 -04004834 port_speed = 10000;
4835 break;
James Smart26d830e2015-04-07 15:07:17 -04004836 case LPFC_FC_LA_SPEED_16G:
James Smart8b68cd52012-09-29 11:32:37 -04004837 port_speed = 16000;
4838 break;
James Smartd38dd522015-08-31 16:48:17 -04004839 case LPFC_FC_LA_SPEED_32G:
4840 port_speed = 32000;
4841 break;
James Smartfbd8a6b2018-02-22 08:18:45 -08004842 case LPFC_FC_LA_SPEED_64G:
4843 port_speed = 64000;
4844 break;
James Smart1dc5ec22018-10-23 13:41:11 -07004845 case LPFC_FC_LA_SPEED_128G:
4846 port_speed = 128000;
4847 break;
James Smart8b68cd52012-09-29 11:32:37 -04004848 default:
4849 port_speed = 0;
4850 }
4851 break;
4852 default:
4853 port_speed = 0;
4854 }
4855 return port_speed;
4856}
4857
4858/**
James Smart70f3c072010-12-15 17:57:33 -05004859 * lpfc_sli4_async_link_evt - Process the asynchronous FCoE link event
James Smartda0436e2009-05-22 14:51:39 -04004860 * @phba: pointer to lpfc hba data structure.
4861 * @acqe_link: pointer to the async link completion queue entry.
4862 *
James Smart70f3c072010-12-15 17:57:33 -05004863 * This routine is to handle the SLI4 asynchronous FCoE link event.
James Smartda0436e2009-05-22 14:51:39 -04004864 **/
4865static void
4866lpfc_sli4_async_link_evt(struct lpfc_hba *phba,
4867 struct lpfc_acqe_link *acqe_link)
4868{
4869 struct lpfc_dmabuf *mp;
4870 LPFC_MBOXQ_t *pmb;
4871 MAILBOX_t *mb;
James Smart76a95d72010-11-20 23:11:48 -05004872 struct lpfc_mbx_read_top *la;
James Smartda0436e2009-05-22 14:51:39 -04004873 uint8_t att_type;
James Smart76a95d72010-11-20 23:11:48 -05004874 int rc;
James Smartda0436e2009-05-22 14:51:39 -04004875
4876 att_type = lpfc_sli4_parse_latt_type(phba, acqe_link);
James Smart76a95d72010-11-20 23:11:48 -05004877 if (att_type != LPFC_ATT_LINK_DOWN && att_type != LPFC_ATT_LINK_UP)
James Smartda0436e2009-05-22 14:51:39 -04004878 return;
James Smart32b97932009-07-19 10:01:21 -04004879 phba->fcoe_eventtag = acqe_link->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04004880 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4881 if (!pmb) {
4882 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4883 "0395 The mboxq allocation failed\n");
4884 return;
4885 }
4886 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4887 if (!mp) {
4888 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4889 "0396 The lpfc_dmabuf allocation failed\n");
4890 goto out_free_pmb;
4891 }
4892 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
4893 if (!mp->virt) {
4894 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4895 "0397 The mbuf allocation failed\n");
4896 goto out_free_dmabuf;
4897 }
4898
4899 /* Cleanup any outstanding ELS commands */
4900 lpfc_els_flush_all_cmd(phba);
4901
4902 /* Block ELS IOCBs until we have done process link event */
James Smart895427b2017-02-12 13:52:30 -08004903 phba->sli4_hba.els_wq->pring->flag |= LPFC_STOP_IOCB_EVENT;
James Smartda0436e2009-05-22 14:51:39 -04004904
4905 /* Update link event statistics */
4906 phba->sli.slistat.link_event++;
4907
James Smart76a95d72010-11-20 23:11:48 -05004908 /* Create lpfc_handle_latt mailbox command from link ACQE */
4909 lpfc_read_topology(phba, pmb, mp);
4910 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
James Smartda0436e2009-05-22 14:51:39 -04004911 pmb->vport = phba->pport;
4912
James Smartda0436e2009-05-22 14:51:39 -04004913 /* Keep the link status for extra SLI4 state machine reference */
4914 phba->sli4_hba.link_state.speed =
James Smart8b68cd52012-09-29 11:32:37 -04004915 lpfc_sli4_port_speed_parse(phba, LPFC_TRAILER_CODE_LINK,
4916 bf_get(lpfc_acqe_link_speed, acqe_link));
James Smartda0436e2009-05-22 14:51:39 -04004917 phba->sli4_hba.link_state.duplex =
4918 bf_get(lpfc_acqe_link_duplex, acqe_link);
4919 phba->sli4_hba.link_state.status =
4920 bf_get(lpfc_acqe_link_status, acqe_link);
James Smart70f3c072010-12-15 17:57:33 -05004921 phba->sli4_hba.link_state.type =
4922 bf_get(lpfc_acqe_link_type, acqe_link);
4923 phba->sli4_hba.link_state.number =
4924 bf_get(lpfc_acqe_link_number, acqe_link);
James Smartda0436e2009-05-22 14:51:39 -04004925 phba->sli4_hba.link_state.fault =
4926 bf_get(lpfc_acqe_link_fault, acqe_link);
James Smart65467b62010-01-26 23:08:29 -05004927 phba->sli4_hba.link_state.logical_speed =
James Smart8b68cd52012-09-29 11:32:37 -04004928 bf_get(lpfc_acqe_logical_link_speed, acqe_link) * 10;
4929
James Smart70f3c072010-12-15 17:57:33 -05004930 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartc31098c2011-04-16 11:03:33 -04004931 "2900 Async FC/FCoE Link event - Speed:%dGBit "
4932 "duplex:x%x LA Type:x%x Port Type:%d Port Number:%d "
4933 "Logical speed:%dMbps Fault:%d\n",
James Smart70f3c072010-12-15 17:57:33 -05004934 phba->sli4_hba.link_state.speed,
4935 phba->sli4_hba.link_state.topology,
4936 phba->sli4_hba.link_state.status,
4937 phba->sli4_hba.link_state.type,
4938 phba->sli4_hba.link_state.number,
James Smart8b68cd52012-09-29 11:32:37 -04004939 phba->sli4_hba.link_state.logical_speed,
James Smart70f3c072010-12-15 17:57:33 -05004940 phba->sli4_hba.link_state.fault);
James Smart76a95d72010-11-20 23:11:48 -05004941 /*
4942 * For FC Mode: issue the READ_TOPOLOGY mailbox command to fetch
4943 * topology info. Note: Optional for non FC-AL ports.
4944 */
4945 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
4946 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
4947 if (rc == MBX_NOT_FINISHED)
4948 goto out_free_dmabuf;
4949 return;
4950 }
4951 /*
4952 * For FCoE Mode: fill in all the topology information we need and call
4953 * the READ_TOPOLOGY completion routine to continue without actually
4954 * sending the READ_TOPOLOGY mailbox command to the port.
4955 */
James Smart23288b72018-05-04 20:37:53 -07004956 /* Initialize completion status */
James Smart76a95d72010-11-20 23:11:48 -05004957 mb = &pmb->u.mb;
James Smart23288b72018-05-04 20:37:53 -07004958 mb->mbxStatus = MBX_SUCCESS;
4959
4960 /* Parse port fault information field */
4961 lpfc_sli4_parse_latt_fault(phba, acqe_link);
James Smart76a95d72010-11-20 23:11:48 -05004962
4963 /* Parse and translate link attention fields */
4964 la = (struct lpfc_mbx_read_top *) &pmb->u.mb.un.varReadTop;
4965 la->eventTag = acqe_link->event_tag;
4966 bf_set(lpfc_mbx_read_top_att_type, la, att_type);
4967 bf_set(lpfc_mbx_read_top_link_spd, la,
James Smarta085e872015-12-16 18:12:02 -05004968 (bf_get(lpfc_acqe_link_speed, acqe_link)));
James Smart76a95d72010-11-20 23:11:48 -05004969
4970 /* Fake the the following irrelvant fields */
4971 bf_set(lpfc_mbx_read_top_topology, la, LPFC_TOPOLOGY_PT_PT);
4972 bf_set(lpfc_mbx_read_top_alpa_granted, la, 0);
4973 bf_set(lpfc_mbx_read_top_il, la, 0);
4974 bf_set(lpfc_mbx_read_top_pb, la, 0);
4975 bf_set(lpfc_mbx_read_top_fa, la, 0);
4976 bf_set(lpfc_mbx_read_top_mm, la, 0);
James Smartda0436e2009-05-22 14:51:39 -04004977
4978 /* Invoke the lpfc_handle_latt mailbox command callback function */
James Smart76a95d72010-11-20 23:11:48 -05004979 lpfc_mbx_cmpl_read_topology(phba, pmb);
James Smartda0436e2009-05-22 14:51:39 -04004980
4981 return;
4982
4983out_free_dmabuf:
4984 kfree(mp);
4985out_free_pmb:
4986 mempool_free(pmb, phba->mbox_mem_pool);
4987}
4988
4989/**
James Smart1dc5ec22018-10-23 13:41:11 -07004990 * lpfc_async_link_speed_to_read_top - Parse async evt link speed code to read
4991 * topology.
4992 * @phba: pointer to lpfc hba data structure.
4993 * @evt_code: asynchronous event code.
4994 * @speed_code: asynchronous event link speed code.
4995 *
4996 * This routine is to parse the giving SLI4 async event link speed code into
4997 * value of Read topology link speed.
4998 *
4999 * Return: link speed in terms of Read topology.
5000 **/
5001static uint8_t
5002lpfc_async_link_speed_to_read_top(struct lpfc_hba *phba, uint8_t speed_code)
5003{
5004 uint8_t port_speed;
5005
5006 switch (speed_code) {
5007 case LPFC_FC_LA_SPEED_1G:
5008 port_speed = LPFC_LINK_SPEED_1GHZ;
5009 break;
5010 case LPFC_FC_LA_SPEED_2G:
5011 port_speed = LPFC_LINK_SPEED_2GHZ;
5012 break;
5013 case LPFC_FC_LA_SPEED_4G:
5014 port_speed = LPFC_LINK_SPEED_4GHZ;
5015 break;
5016 case LPFC_FC_LA_SPEED_8G:
5017 port_speed = LPFC_LINK_SPEED_8GHZ;
5018 break;
5019 case LPFC_FC_LA_SPEED_16G:
5020 port_speed = LPFC_LINK_SPEED_16GHZ;
5021 break;
5022 case LPFC_FC_LA_SPEED_32G:
5023 port_speed = LPFC_LINK_SPEED_32GHZ;
5024 break;
5025 case LPFC_FC_LA_SPEED_64G:
5026 port_speed = LPFC_LINK_SPEED_64GHZ;
5027 break;
5028 case LPFC_FC_LA_SPEED_128G:
5029 port_speed = LPFC_LINK_SPEED_128GHZ;
5030 break;
5031 case LPFC_FC_LA_SPEED_256G:
5032 port_speed = LPFC_LINK_SPEED_256GHZ;
5033 break;
5034 default:
5035 port_speed = 0;
5036 break;
5037 }
5038
5039 return port_speed;
5040}
5041
5042#define trunk_link_status(__idx)\
5043 bf_get(lpfc_acqe_fc_la_trunk_config_port##__idx, acqe_fc) ?\
5044 ((phba->trunk_link.link##__idx.state == LPFC_LINK_UP) ?\
5045 "Link up" : "Link down") : "NA"
5046/* Did port __idx reported an error */
5047#define trunk_port_fault(__idx)\
5048 bf_get(lpfc_acqe_fc_la_trunk_config_port##__idx, acqe_fc) ?\
5049 (port_fault & (1 << __idx) ? "YES" : "NO") : "NA"
5050
5051static void
5052lpfc_update_trunk_link_status(struct lpfc_hba *phba,
5053 struct lpfc_acqe_fc_la *acqe_fc)
5054{
5055 uint8_t port_fault = bf_get(lpfc_acqe_fc_la_trunk_linkmask, acqe_fc);
5056 uint8_t err = bf_get(lpfc_acqe_fc_la_trunk_fault, acqe_fc);
5057
5058 phba->sli4_hba.link_state.speed =
5059 lpfc_sli4_port_speed_parse(phba, LPFC_TRAILER_CODE_FC,
5060 bf_get(lpfc_acqe_fc_la_speed, acqe_fc));
5061
5062 phba->sli4_hba.link_state.logical_speed =
James Smartb8e6f132019-05-21 17:49:04 -07005063 bf_get(lpfc_acqe_fc_la_llink_spd, acqe_fc) * 10;
James Smart1dc5ec22018-10-23 13:41:11 -07005064 /* We got FC link speed, convert to fc_linkspeed (READ_TOPOLOGY) */
5065 phba->fc_linkspeed =
5066 lpfc_async_link_speed_to_read_top(
5067 phba,
5068 bf_get(lpfc_acqe_fc_la_speed, acqe_fc));
5069
5070 if (bf_get(lpfc_acqe_fc_la_trunk_config_port0, acqe_fc)) {
5071 phba->trunk_link.link0.state =
5072 bf_get(lpfc_acqe_fc_la_trunk_link_status_port0, acqe_fc)
5073 ? LPFC_LINK_UP : LPFC_LINK_DOWN;
James Smart529b3dd2018-12-13 15:17:54 -08005074 phba->trunk_link.link0.fault = port_fault & 0x1 ? err : 0;
James Smart1dc5ec22018-10-23 13:41:11 -07005075 }
5076 if (bf_get(lpfc_acqe_fc_la_trunk_config_port1, acqe_fc)) {
5077 phba->trunk_link.link1.state =
5078 bf_get(lpfc_acqe_fc_la_trunk_link_status_port1, acqe_fc)
5079 ? LPFC_LINK_UP : LPFC_LINK_DOWN;
James Smart529b3dd2018-12-13 15:17:54 -08005080 phba->trunk_link.link1.fault = port_fault & 0x2 ? err : 0;
James Smart1dc5ec22018-10-23 13:41:11 -07005081 }
5082 if (bf_get(lpfc_acqe_fc_la_trunk_config_port2, acqe_fc)) {
5083 phba->trunk_link.link2.state =
5084 bf_get(lpfc_acqe_fc_la_trunk_link_status_port2, acqe_fc)
5085 ? LPFC_LINK_UP : LPFC_LINK_DOWN;
James Smart529b3dd2018-12-13 15:17:54 -08005086 phba->trunk_link.link2.fault = port_fault & 0x4 ? err : 0;
James Smart1dc5ec22018-10-23 13:41:11 -07005087 }
5088 if (bf_get(lpfc_acqe_fc_la_trunk_config_port3, acqe_fc)) {
5089 phba->trunk_link.link3.state =
5090 bf_get(lpfc_acqe_fc_la_trunk_link_status_port3, acqe_fc)
5091 ? LPFC_LINK_UP : LPFC_LINK_DOWN;
James Smart529b3dd2018-12-13 15:17:54 -08005092 phba->trunk_link.link3.fault = port_fault & 0x8 ? err : 0;
James Smart1dc5ec22018-10-23 13:41:11 -07005093 }
5094
5095 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5096 "2910 Async FC Trunking Event - Speed:%d\n"
5097 "\tLogical speed:%d "
5098 "port0: %s port1: %s port2: %s port3: %s\n",
5099 phba->sli4_hba.link_state.speed,
5100 phba->sli4_hba.link_state.logical_speed,
5101 trunk_link_status(0), trunk_link_status(1),
5102 trunk_link_status(2), trunk_link_status(3));
5103
5104 if (port_fault)
5105 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5106 "3202 trunk error:0x%x (%s) seen on port0:%s "
5107 /*
5108 * SLI-4: We have only 0xA error codes
5109 * defined as of now. print an appropriate
5110 * message in case driver needs to be updated.
5111 */
5112 "port1:%s port2:%s port3:%s\n", err, err > 0xA ?
5113 "UNDEFINED. update driver." : trunk_errmsg[err],
5114 trunk_port_fault(0), trunk_port_fault(1),
5115 trunk_port_fault(2), trunk_port_fault(3));
5116}
5117
5118
5119/**
James Smart70f3c072010-12-15 17:57:33 -05005120 * lpfc_sli4_async_fc_evt - Process the asynchronous FC link event
5121 * @phba: pointer to lpfc hba data structure.
5122 * @acqe_fc: pointer to the async fc completion queue entry.
5123 *
5124 * This routine is to handle the SLI4 asynchronous FC event. It will simply log
5125 * that the event was received and then issue a read_topology mailbox command so
5126 * that the rest of the driver will treat it the same as SLI3.
5127 **/
5128static void
5129lpfc_sli4_async_fc_evt(struct lpfc_hba *phba, struct lpfc_acqe_fc_la *acqe_fc)
5130{
5131 struct lpfc_dmabuf *mp;
5132 LPFC_MBOXQ_t *pmb;
James Smart7bdedb32016-07-06 12:36:00 -07005133 MAILBOX_t *mb;
5134 struct lpfc_mbx_read_top *la;
James Smart70f3c072010-12-15 17:57:33 -05005135 int rc;
5136
5137 if (bf_get(lpfc_trailer_type, acqe_fc) !=
5138 LPFC_FC_LA_EVENT_TYPE_FC_LINK) {
5139 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5140 "2895 Non FC link Event detected.(%d)\n",
5141 bf_get(lpfc_trailer_type, acqe_fc));
5142 return;
5143 }
James Smart1dc5ec22018-10-23 13:41:11 -07005144
5145 if (bf_get(lpfc_acqe_fc_la_att_type, acqe_fc) ==
5146 LPFC_FC_LA_TYPE_TRUNKING_EVENT) {
5147 lpfc_update_trunk_link_status(phba, acqe_fc);
5148 return;
5149 }
5150
James Smart70f3c072010-12-15 17:57:33 -05005151 /* Keep the link status for extra SLI4 state machine reference */
5152 phba->sli4_hba.link_state.speed =
James Smart8b68cd52012-09-29 11:32:37 -04005153 lpfc_sli4_port_speed_parse(phba, LPFC_TRAILER_CODE_FC,
5154 bf_get(lpfc_acqe_fc_la_speed, acqe_fc));
James Smart70f3c072010-12-15 17:57:33 -05005155 phba->sli4_hba.link_state.duplex = LPFC_ASYNC_LINK_DUPLEX_FULL;
5156 phba->sli4_hba.link_state.topology =
5157 bf_get(lpfc_acqe_fc_la_topology, acqe_fc);
5158 phba->sli4_hba.link_state.status =
5159 bf_get(lpfc_acqe_fc_la_att_type, acqe_fc);
5160 phba->sli4_hba.link_state.type =
5161 bf_get(lpfc_acqe_fc_la_port_type, acqe_fc);
5162 phba->sli4_hba.link_state.number =
5163 bf_get(lpfc_acqe_fc_la_port_number, acqe_fc);
5164 phba->sli4_hba.link_state.fault =
5165 bf_get(lpfc_acqe_link_fault, acqe_fc);
James Smartb8e6f132019-05-21 17:49:04 -07005166
5167 if (bf_get(lpfc_acqe_fc_la_att_type, acqe_fc) ==
5168 LPFC_FC_LA_TYPE_LINK_DOWN)
5169 phba->sli4_hba.link_state.logical_speed = 0;
5170 else if (!phba->sli4_hba.conf_trunk)
5171 phba->sli4_hba.link_state.logical_speed =
James Smart8b68cd52012-09-29 11:32:37 -04005172 bf_get(lpfc_acqe_fc_la_llink_spd, acqe_fc) * 10;
James Smartb8e6f132019-05-21 17:49:04 -07005173
James Smart70f3c072010-12-15 17:57:33 -05005174 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5175 "2896 Async FC event - Speed:%dGBaud Topology:x%x "
5176 "LA Type:x%x Port Type:%d Port Number:%d Logical speed:"
5177 "%dMbps Fault:%d\n",
5178 phba->sli4_hba.link_state.speed,
5179 phba->sli4_hba.link_state.topology,
5180 phba->sli4_hba.link_state.status,
5181 phba->sli4_hba.link_state.type,
5182 phba->sli4_hba.link_state.number,
James Smart8b68cd52012-09-29 11:32:37 -04005183 phba->sli4_hba.link_state.logical_speed,
James Smart70f3c072010-12-15 17:57:33 -05005184 phba->sli4_hba.link_state.fault);
5185 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5186 if (!pmb) {
5187 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5188 "2897 The mboxq allocation failed\n");
5189 return;
5190 }
5191 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5192 if (!mp) {
5193 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5194 "2898 The lpfc_dmabuf allocation failed\n");
5195 goto out_free_pmb;
5196 }
5197 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
5198 if (!mp->virt) {
5199 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5200 "2899 The mbuf allocation failed\n");
5201 goto out_free_dmabuf;
5202 }
5203
5204 /* Cleanup any outstanding ELS commands */
5205 lpfc_els_flush_all_cmd(phba);
5206
5207 /* Block ELS IOCBs until we have done process link event */
James Smart895427b2017-02-12 13:52:30 -08005208 phba->sli4_hba.els_wq->pring->flag |= LPFC_STOP_IOCB_EVENT;
James Smart70f3c072010-12-15 17:57:33 -05005209
5210 /* Update link event statistics */
5211 phba->sli.slistat.link_event++;
5212
5213 /* Create lpfc_handle_latt mailbox command from link ACQE */
5214 lpfc_read_topology(phba, pmb, mp);
5215 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
5216 pmb->vport = phba->pport;
5217
James Smart7bdedb32016-07-06 12:36:00 -07005218 if (phba->sli4_hba.link_state.status != LPFC_FC_LA_TYPE_LINK_UP) {
James Smartae9e28f2017-05-15 15:20:51 -07005219 phba->link_flag &= ~(LS_MDS_LINK_DOWN | LS_MDS_LOOPBACK);
5220
5221 switch (phba->sli4_hba.link_state.status) {
5222 case LPFC_FC_LA_TYPE_MDS_LINK_DOWN:
5223 phba->link_flag |= LS_MDS_LINK_DOWN;
5224 break;
5225 case LPFC_FC_LA_TYPE_MDS_LOOPBACK:
5226 phba->link_flag |= LS_MDS_LOOPBACK;
5227 break;
5228 default:
5229 break;
5230 }
5231
James Smart23288b72018-05-04 20:37:53 -07005232 /* Initialize completion status */
James Smart7bdedb32016-07-06 12:36:00 -07005233 mb = &pmb->u.mb;
James Smart23288b72018-05-04 20:37:53 -07005234 mb->mbxStatus = MBX_SUCCESS;
5235
5236 /* Parse port fault information field */
5237 lpfc_sli4_parse_latt_fault(phba, (void *)acqe_fc);
James Smart7bdedb32016-07-06 12:36:00 -07005238
5239 /* Parse and translate link attention fields */
5240 la = (struct lpfc_mbx_read_top *)&pmb->u.mb.un.varReadTop;
5241 la->eventTag = acqe_fc->event_tag;
James Smart7bdedb32016-07-06 12:36:00 -07005242
James Smartaeb3c812017-04-21 16:05:02 -07005243 if (phba->sli4_hba.link_state.status ==
5244 LPFC_FC_LA_TYPE_UNEXP_WWPN) {
5245 bf_set(lpfc_mbx_read_top_att_type, la,
5246 LPFC_FC_LA_TYPE_UNEXP_WWPN);
5247 } else {
5248 bf_set(lpfc_mbx_read_top_att_type, la,
5249 LPFC_FC_LA_TYPE_LINK_DOWN);
5250 }
James Smart7bdedb32016-07-06 12:36:00 -07005251 /* Invoke the mailbox command callback function */
5252 lpfc_mbx_cmpl_read_topology(phba, pmb);
5253
5254 return;
5255 }
5256
James Smart70f3c072010-12-15 17:57:33 -05005257 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
5258 if (rc == MBX_NOT_FINISHED)
5259 goto out_free_dmabuf;
5260 return;
5261
5262out_free_dmabuf:
5263 kfree(mp);
5264out_free_pmb:
5265 mempool_free(pmb, phba->mbox_mem_pool);
5266}
5267
5268/**
5269 * lpfc_sli4_async_sli_evt - Process the asynchronous SLI link event
5270 * @phba: pointer to lpfc hba data structure.
5271 * @acqe_fc: pointer to the async SLI completion queue entry.
5272 *
5273 * This routine is to handle the SLI4 asynchronous SLI events.
5274 **/
5275static void
5276lpfc_sli4_async_sli_evt(struct lpfc_hba *phba, struct lpfc_acqe_sli *acqe_sli)
5277{
James Smart4b8bae02012-06-12 13:55:07 -04005278 char port_name;
James Smart8c1312e2012-10-31 14:45:09 -04005279 char message[128];
James Smart4b8bae02012-06-12 13:55:07 -04005280 uint8_t status;
James Smart946727d2015-04-07 15:07:09 -04005281 uint8_t evt_type;
James Smart448193b2015-12-16 18:12:05 -05005282 uint8_t operational = 0;
James Smart946727d2015-04-07 15:07:09 -04005283 struct temp_event temp_event_data;
James Smart4b8bae02012-06-12 13:55:07 -04005284 struct lpfc_acqe_misconfigured_event *misconfigured;
James Smart946727d2015-04-07 15:07:09 -04005285 struct Scsi_Host *shost;
James Smartcd713482018-10-23 13:41:01 -07005286 struct lpfc_vport **vports;
5287 int rc, i;
James Smart4b8bae02012-06-12 13:55:07 -04005288
James Smart946727d2015-04-07 15:07:09 -04005289 evt_type = bf_get(lpfc_trailer_type, acqe_sli);
5290
James Smart448193b2015-12-16 18:12:05 -05005291 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartd11ed162019-09-21 20:59:03 -07005292 "2901 Async SLI event - Type:%d, Event Data: x%08x "
5293 "x%08x x%08x x%08x\n", evt_type,
James Smart448193b2015-12-16 18:12:05 -05005294 acqe_sli->event_data1, acqe_sli->event_data2,
James Smartd11ed162019-09-21 20:59:03 -07005295 acqe_sli->reserved, acqe_sli->trailer);
James Smart4b8bae02012-06-12 13:55:07 -04005296
5297 port_name = phba->Port[0];
5298 if (port_name == 0x00)
5299 port_name = '?'; /* get port name is empty */
5300
James Smart946727d2015-04-07 15:07:09 -04005301 switch (evt_type) {
5302 case LPFC_SLI_EVENT_TYPE_OVER_TEMP:
5303 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
5304 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
5305 temp_event_data.data = (uint32_t)acqe_sli->event_data1;
5306
5307 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5308 "3190 Over Temperature:%d Celsius- Port Name %c\n",
5309 acqe_sli->event_data1, port_name);
5310
James Smart310429e2016-07-06 12:35:54 -07005311 phba->sfp_warning |= LPFC_TRANSGRESSION_HIGH_TEMPERATURE;
James Smart946727d2015-04-07 15:07:09 -04005312 shost = lpfc_shost_from_vport(phba->pport);
5313 fc_host_post_vendor_event(shost, fc_get_event_number(),
5314 sizeof(temp_event_data),
5315 (char *)&temp_event_data,
5316 SCSI_NL_VID_TYPE_PCI
5317 | PCI_VENDOR_ID_EMULEX);
5318 break;
5319 case LPFC_SLI_EVENT_TYPE_NORM_TEMP:
5320 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
5321 temp_event_data.event_code = LPFC_NORMAL_TEMP;
5322 temp_event_data.data = (uint32_t)acqe_sli->event_data1;
5323
5324 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5325 "3191 Normal Temperature:%d Celsius - Port Name %c\n",
5326 acqe_sli->event_data1, port_name);
5327
5328 shost = lpfc_shost_from_vport(phba->pport);
5329 fc_host_post_vendor_event(shost, fc_get_event_number(),
5330 sizeof(temp_event_data),
5331 (char *)&temp_event_data,
5332 SCSI_NL_VID_TYPE_PCI
5333 | PCI_VENDOR_ID_EMULEX);
5334 break;
5335 case LPFC_SLI_EVENT_TYPE_MISCONFIGURED:
5336 misconfigured = (struct lpfc_acqe_misconfigured_event *)
James Smart4b8bae02012-06-12 13:55:07 -04005337 &acqe_sli->event_data1;
5338
James Smart946727d2015-04-07 15:07:09 -04005339 /* fetch the status for this port */
5340 switch (phba->sli4_hba.lnk_info.lnk_no) {
5341 case LPFC_LINK_NUMBER_0:
James Smart448193b2015-12-16 18:12:05 -05005342 status = bf_get(lpfc_sli_misconfigured_port0_state,
5343 &misconfigured->theEvent);
5344 operational = bf_get(lpfc_sli_misconfigured_port0_op,
James Smart4b8bae02012-06-12 13:55:07 -04005345 &misconfigured->theEvent);
James Smart946727d2015-04-07 15:07:09 -04005346 break;
5347 case LPFC_LINK_NUMBER_1:
James Smart448193b2015-12-16 18:12:05 -05005348 status = bf_get(lpfc_sli_misconfigured_port1_state,
5349 &misconfigured->theEvent);
5350 operational = bf_get(lpfc_sli_misconfigured_port1_op,
James Smart4b8bae02012-06-12 13:55:07 -04005351 &misconfigured->theEvent);
James Smart946727d2015-04-07 15:07:09 -04005352 break;
5353 case LPFC_LINK_NUMBER_2:
James Smart448193b2015-12-16 18:12:05 -05005354 status = bf_get(lpfc_sli_misconfigured_port2_state,
5355 &misconfigured->theEvent);
5356 operational = bf_get(lpfc_sli_misconfigured_port2_op,
James Smart4b8bae02012-06-12 13:55:07 -04005357 &misconfigured->theEvent);
James Smart946727d2015-04-07 15:07:09 -04005358 break;
5359 case LPFC_LINK_NUMBER_3:
James Smart448193b2015-12-16 18:12:05 -05005360 status = bf_get(lpfc_sli_misconfigured_port3_state,
5361 &misconfigured->theEvent);
5362 operational = bf_get(lpfc_sli_misconfigured_port3_op,
James Smart4b8bae02012-06-12 13:55:07 -04005363 &misconfigured->theEvent);
James Smart946727d2015-04-07 15:07:09 -04005364 break;
5365 default:
James Smart448193b2015-12-16 18:12:05 -05005366 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5367 "3296 "
5368 "LPFC_SLI_EVENT_TYPE_MISCONFIGURED "
5369 "event: Invalid link %d",
5370 phba->sli4_hba.lnk_info.lnk_no);
5371 return;
James Smart946727d2015-04-07 15:07:09 -04005372 }
James Smart4b8bae02012-06-12 13:55:07 -04005373
James Smart448193b2015-12-16 18:12:05 -05005374 /* Skip if optic state unchanged */
5375 if (phba->sli4_hba.lnk_info.optic_state == status)
5376 return;
5377
James Smart946727d2015-04-07 15:07:09 -04005378 switch (status) {
5379 case LPFC_SLI_EVENT_STATUS_VALID:
James Smart448193b2015-12-16 18:12:05 -05005380 sprintf(message, "Physical Link is functional");
5381 break;
James Smart946727d2015-04-07 15:07:09 -04005382 case LPFC_SLI_EVENT_STATUS_NOT_PRESENT:
5383 sprintf(message, "Optics faulted/incorrectly "
5384 "installed/not installed - Reseat optics, "
5385 "if issue not resolved, replace.");
5386 break;
5387 case LPFC_SLI_EVENT_STATUS_WRONG_TYPE:
5388 sprintf(message,
5389 "Optics of two types installed - Remove one "
5390 "optic or install matching pair of optics.");
5391 break;
5392 case LPFC_SLI_EVENT_STATUS_UNSUPPORTED:
5393 sprintf(message, "Incompatible optics - Replace with "
James Smart292098b2012-09-29 11:31:41 -04005394 "compatible optics for card to function.");
James Smart946727d2015-04-07 15:07:09 -04005395 break;
James Smart448193b2015-12-16 18:12:05 -05005396 case LPFC_SLI_EVENT_STATUS_UNQUALIFIED:
5397 sprintf(message, "Unqualified optics - Replace with "
5398 "Avago optics for Warranty and Technical "
5399 "Support - Link is%s operational",
James Smart2ea259e2017-02-12 13:52:27 -08005400 (operational) ? " not" : "");
James Smart448193b2015-12-16 18:12:05 -05005401 break;
5402 case LPFC_SLI_EVENT_STATUS_UNCERTIFIED:
5403 sprintf(message, "Uncertified optics - Replace with "
5404 "Avago-certified optics to enable link "
5405 "operation - Link is%s operational",
James Smart2ea259e2017-02-12 13:52:27 -08005406 (operational) ? " not" : "");
James Smart448193b2015-12-16 18:12:05 -05005407 break;
James Smart946727d2015-04-07 15:07:09 -04005408 default:
5409 /* firmware is reporting a status we don't know about */
5410 sprintf(message, "Unknown event status x%02x", status);
5411 break;
5412 }
James Smartcd713482018-10-23 13:41:01 -07005413
5414 /* Issue READ_CONFIG mbox command to refresh supported speeds */
5415 rc = lpfc_sli4_read_config(phba);
James Smart3952e912018-10-23 13:41:02 -07005416 if (rc) {
James Smartcd713482018-10-23 13:41:01 -07005417 phba->lmt = 0;
5418 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5419 "3194 Unable to retrieve supported "
James Smart3952e912018-10-23 13:41:02 -07005420 "speeds, rc = 0x%x\n", rc);
James Smartcd713482018-10-23 13:41:01 -07005421 }
5422 vports = lpfc_create_vport_work_array(phba);
5423 if (vports != NULL) {
5424 for (i = 0; i <= phba->max_vports && vports[i] != NULL;
5425 i++) {
5426 shost = lpfc_shost_from_vport(vports[i]);
5427 lpfc_host_supported_speeds_set(shost);
5428 }
5429 }
5430 lpfc_destroy_vport_work_array(phba, vports);
5431
James Smart448193b2015-12-16 18:12:05 -05005432 phba->sli4_hba.lnk_info.optic_state = status;
James Smart946727d2015-04-07 15:07:09 -04005433 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart448193b2015-12-16 18:12:05 -05005434 "3176 Port Name %c %s\n", port_name, message);
James Smart946727d2015-04-07 15:07:09 -04005435 break;
5436 case LPFC_SLI_EVENT_TYPE_REMOTE_DPORT:
5437 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5438 "3192 Remote DPort Test Initiated - "
5439 "Event Data1:x%08x Event Data2: x%08x\n",
5440 acqe_sli->event_data1, acqe_sli->event_data2);
James Smart4b8bae02012-06-12 13:55:07 -04005441 break;
James Smarte7d85952019-10-18 14:18:29 -07005442 case LPFC_SLI_EVENT_TYPE_MISCONF_FAWWN:
5443 /* Misconfigured WWN. Reports that the SLI Port is configured
5444 * to use FA-WWN, but the attached device doesn’t support it.
5445 * No driver action is required.
5446 * Event Data1 - N.A, Event Data2 - N.A
5447 */
5448 lpfc_log_msg(phba, KERN_WARNING, LOG_SLI,
5449 "2699 Misconfigured FA-WWN - Attached device does "
5450 "not support FA-WWN\n");
5451 break;
James Smartd11ed162019-09-21 20:59:03 -07005452 case LPFC_SLI_EVENT_TYPE_EEPROM_FAILURE:
5453 /* EEPROM failure. No driver action is required */
5454 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5455 "2518 EEPROM failure - "
5456 "Event Data1: x%08x Event Data2: x%08x\n",
5457 acqe_sli->event_data1, acqe_sli->event_data2);
5458 break;
James Smart4b8bae02012-06-12 13:55:07 -04005459 default:
James Smart946727d2015-04-07 15:07:09 -04005460 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartd11ed162019-09-21 20:59:03 -07005461 "3193 Unrecognized SLI event, type: 0x%x",
James Smart946727d2015-04-07 15:07:09 -04005462 evt_type);
James Smart4b8bae02012-06-12 13:55:07 -04005463 break;
5464 }
James Smart70f3c072010-12-15 17:57:33 -05005465}
5466
5467/**
James Smartfc2b9892010-02-26 14:15:29 -05005468 * lpfc_sli4_perform_vport_cvl - Perform clear virtual link on a vport
5469 * @vport: pointer to vport data structure.
5470 *
5471 * This routine is to perform Clear Virtual Link (CVL) on a vport in
5472 * response to a CVL event.
5473 *
5474 * Return the pointer to the ndlp with the vport if successful, otherwise
5475 * return NULL.
5476 **/
5477static struct lpfc_nodelist *
5478lpfc_sli4_perform_vport_cvl(struct lpfc_vport *vport)
5479{
5480 struct lpfc_nodelist *ndlp;
5481 struct Scsi_Host *shost;
5482 struct lpfc_hba *phba;
5483
5484 if (!vport)
5485 return NULL;
James Smartfc2b9892010-02-26 14:15:29 -05005486 phba = vport->phba;
5487 if (!phba)
5488 return NULL;
James Smart78730cf2010-04-06 15:06:30 -04005489 ndlp = lpfc_findnode_did(vport, Fabric_DID);
5490 if (!ndlp) {
5491 /* Cannot find existing Fabric ndlp, so allocate a new one */
James Smart9d3d3402017-04-21 16:05:00 -07005492 ndlp = lpfc_nlp_init(vport, Fabric_DID);
James Smart78730cf2010-04-06 15:06:30 -04005493 if (!ndlp)
5494 return 0;
James Smart78730cf2010-04-06 15:06:30 -04005495 /* Set the node type */
5496 ndlp->nlp_type |= NLP_FABRIC;
5497 /* Put ndlp onto node list */
5498 lpfc_enqueue_node(vport, ndlp);
5499 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
5500 /* re-setup ndlp without removing from node list */
5501 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
5502 if (!ndlp)
5503 return 0;
5504 }
James Smart63e801c2010-11-20 23:14:19 -05005505 if ((phba->pport->port_state < LPFC_FLOGI) &&
5506 (phba->pport->port_state != LPFC_VPORT_FAILED))
James Smartfc2b9892010-02-26 14:15:29 -05005507 return NULL;
5508 /* If virtual link is not yet instantiated ignore CVL */
James Smart63e801c2010-11-20 23:14:19 -05005509 if ((vport != phba->pport) && (vport->port_state < LPFC_FDISC)
5510 && (vport->port_state != LPFC_VPORT_FAILED))
James Smartfc2b9892010-02-26 14:15:29 -05005511 return NULL;
5512 shost = lpfc_shost_from_vport(vport);
5513 if (!shost)
5514 return NULL;
5515 lpfc_linkdown_port(vport);
5516 lpfc_cleanup_pending_mbox(vport);
5517 spin_lock_irq(shost->host_lock);
5518 vport->fc_flag |= FC_VPORT_CVL_RCVD;
5519 spin_unlock_irq(shost->host_lock);
5520
5521 return ndlp;
5522}
5523
5524/**
5525 * lpfc_sli4_perform_all_vport_cvl - Perform clear virtual link on all vports
5526 * @vport: pointer to lpfc hba data structure.
5527 *
5528 * This routine is to perform Clear Virtual Link (CVL) on all vports in
5529 * response to a FCF dead event.
5530 **/
5531static void
5532lpfc_sli4_perform_all_vport_cvl(struct lpfc_hba *phba)
5533{
5534 struct lpfc_vport **vports;
5535 int i;
5536
5537 vports = lpfc_create_vport_work_array(phba);
5538 if (vports)
5539 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
5540 lpfc_sli4_perform_vport_cvl(vports[i]);
5541 lpfc_destroy_vport_work_array(phba, vports);
5542}
5543
5544/**
James Smart76a95d72010-11-20 23:11:48 -05005545 * lpfc_sli4_async_fip_evt - Process the asynchronous FCoE FIP event
James Smartda0436e2009-05-22 14:51:39 -04005546 * @phba: pointer to lpfc hba data structure.
5547 * @acqe_link: pointer to the async fcoe completion queue entry.
5548 *
5549 * This routine is to handle the SLI4 asynchronous fcoe event.
5550 **/
5551static void
James Smart76a95d72010-11-20 23:11:48 -05005552lpfc_sli4_async_fip_evt(struct lpfc_hba *phba,
James Smart70f3c072010-12-15 17:57:33 -05005553 struct lpfc_acqe_fip *acqe_fip)
James Smartda0436e2009-05-22 14:51:39 -04005554{
James Smart70f3c072010-12-15 17:57:33 -05005555 uint8_t event_type = bf_get(lpfc_trailer_type, acqe_fip);
James Smartda0436e2009-05-22 14:51:39 -04005556 int rc;
James Smart6669f9b2009-10-02 15:16:45 -04005557 struct lpfc_vport *vport;
5558 struct lpfc_nodelist *ndlp;
5559 struct Scsi_Host *shost;
James Smart695a8142010-01-26 23:08:03 -05005560 int active_vlink_present;
5561 struct lpfc_vport **vports;
5562 int i;
James Smartda0436e2009-05-22 14:51:39 -04005563
James Smart70f3c072010-12-15 17:57:33 -05005564 phba->fc_eventTag = acqe_fip->event_tag;
5565 phba->fcoe_eventtag = acqe_fip->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04005566 switch (event_type) {
James Smart70f3c072010-12-15 17:57:33 -05005567 case LPFC_FIP_EVENT_TYPE_NEW_FCF:
5568 case LPFC_FIP_EVENT_TYPE_FCF_PARAM_MOD:
5569 if (event_type == LPFC_FIP_EVENT_TYPE_NEW_FCF)
James Smart999d8132010-03-15 11:24:56 -04005570 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
5571 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04005572 "2546 New FCF event, evt_tag:x%x, "
5573 "index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05005574 acqe_fip->event_tag,
5575 acqe_fip->index);
James Smart999d8132010-03-15 11:24:56 -04005576 else
5577 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP |
5578 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04005579 "2788 FCF param modified event, "
5580 "evt_tag:x%x, index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05005581 acqe_fip->event_tag,
5582 acqe_fip->index);
James Smart38b92ef2010-08-04 16:11:39 -04005583 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smart0c9ab6f2010-02-26 14:15:57 -05005584 /*
5585 * During period of FCF discovery, read the FCF
5586 * table record indexed by the event to update
James Smarta93ff372010-10-22 11:06:08 -04005587 * FCF roundrobin failover eligible FCF bmask.
James Smart0c9ab6f2010-02-26 14:15:57 -05005588 */
5589 lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
5590 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04005591 "2779 Read FCF (x%x) for updating "
5592 "roundrobin FCF failover bmask\n",
James Smart70f3c072010-12-15 17:57:33 -05005593 acqe_fip->index);
5594 rc = lpfc_sli4_read_fcf_rec(phba, acqe_fip->index);
James Smart0c9ab6f2010-02-26 14:15:57 -05005595 }
James Smart38b92ef2010-08-04 16:11:39 -04005596
5597 /* If the FCF discovery is in progress, do nothing. */
James Smart3804dc82010-07-14 15:31:37 -04005598 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04005599 if (phba->hba_flag & FCF_TS_INPROG) {
James Smart38b92ef2010-08-04 16:11:39 -04005600 spin_unlock_irq(&phba->hbalock);
5601 break;
5602 }
5603 /* If fast FCF failover rescan event is pending, do nothing */
James Smart036cad12018-10-23 13:41:06 -07005604 if (phba->fcf.fcf_flag & (FCF_REDISC_EVT | FCF_REDISC_PEND)) {
James Smart38b92ef2010-08-04 16:11:39 -04005605 spin_unlock_irq(&phba->hbalock);
5606 break;
5607 }
5608
James Smartc2b97122013-05-31 17:05:36 -04005609 /* If the FCF has been in discovered state, do nothing. */
5610 if (phba->fcf.fcf_flag & FCF_SCAN_DONE) {
James Smart3804dc82010-07-14 15:31:37 -04005611 spin_unlock_irq(&phba->hbalock);
5612 break;
5613 }
5614 spin_unlock_irq(&phba->hbalock);
James Smart38b92ef2010-08-04 16:11:39 -04005615
James Smart0c9ab6f2010-02-26 14:15:57 -05005616 /* Otherwise, scan the entire FCF table and re-discover SAN */
5617 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04005618 "2770 Start FCF table scan per async FCF "
5619 "event, evt_tag:x%x, index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05005620 acqe_fip->event_tag, acqe_fip->index);
James Smart0c9ab6f2010-02-26 14:15:57 -05005621 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba,
5622 LPFC_FCOE_FCF_GET_FIRST);
James Smartda0436e2009-05-22 14:51:39 -04005623 if (rc)
James Smart0c9ab6f2010-02-26 14:15:57 -05005624 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
5625 "2547 Issue FCF scan read FCF mailbox "
James Smarta93ff372010-10-22 11:06:08 -04005626 "command failed (x%x)\n", rc);
James Smartda0436e2009-05-22 14:51:39 -04005627 break;
5628
James Smart70f3c072010-12-15 17:57:33 -05005629 case LPFC_FIP_EVENT_TYPE_FCF_TABLE_FULL:
James Smartda0436e2009-05-22 14:51:39 -04005630 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte4e74272009-07-19 10:01:38 -04005631 "2548 FCF Table full count 0x%x tag 0x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05005632 bf_get(lpfc_acqe_fip_fcf_count, acqe_fip),
5633 acqe_fip->event_tag);
James Smartda0436e2009-05-22 14:51:39 -04005634 break;
5635
James Smart70f3c072010-12-15 17:57:33 -05005636 case LPFC_FIP_EVENT_TYPE_FCF_DEAD:
James Smart80c17842012-03-01 22:35:45 -05005637 phba->fcoe_cvl_eventtag = acqe_fip->event_tag;
James Smart0c9ab6f2010-02-26 14:15:57 -05005638 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04005639 "2549 FCF (x%x) disconnected from network, "
James Smart70f3c072010-12-15 17:57:33 -05005640 "tag:x%x\n", acqe_fip->index, acqe_fip->event_tag);
James Smart38b92ef2010-08-04 16:11:39 -04005641 /*
5642 * If we are in the middle of FCF failover process, clear
5643 * the corresponding FCF bit in the roundrobin bitmap.
James Smartda0436e2009-05-22 14:51:39 -04005644 */
James Smartfc2b9892010-02-26 14:15:29 -05005645 spin_lock_irq(&phba->hbalock);
James Smarta1cadfe2016-07-06 12:36:02 -07005646 if ((phba->fcf.fcf_flag & FCF_DISCOVERY) &&
5647 (phba->fcf.current_rec.fcf_indx != acqe_fip->index)) {
James Smartfc2b9892010-02-26 14:15:29 -05005648 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05005649 /* Update FLOGI FCF failover eligible FCF bmask */
James Smart70f3c072010-12-15 17:57:33 -05005650 lpfc_sli4_fcf_rr_index_clear(phba, acqe_fip->index);
James Smartfc2b9892010-02-26 14:15:29 -05005651 break;
5652 }
James Smart38b92ef2010-08-04 16:11:39 -04005653 spin_unlock_irq(&phba->hbalock);
5654
5655 /* If the event is not for currently used fcf do nothing */
James Smart70f3c072010-12-15 17:57:33 -05005656 if (phba->fcf.current_rec.fcf_indx != acqe_fip->index)
James Smart38b92ef2010-08-04 16:11:39 -04005657 break;
5658
5659 /*
5660 * Otherwise, request the port to rediscover the entire FCF
5661 * table for a fast recovery from case that the current FCF
5662 * is no longer valid as we are not in the middle of FCF
5663 * failover process already.
5664 */
James Smartc2b97122013-05-31 17:05:36 -04005665 spin_lock_irq(&phba->hbalock);
5666 /* Mark the fast failover process in progress */
5667 phba->fcf.fcf_flag |= FCF_DEAD_DISC;
5668 spin_unlock_irq(&phba->hbalock);
5669
5670 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
5671 "2771 Start FCF fast failover process due to "
5672 "FCF DEAD event: evt_tag:x%x, fcf_index:x%x "
5673 "\n", acqe_fip->event_tag, acqe_fip->index);
5674 rc = lpfc_sli4_redisc_fcf_table(phba);
5675 if (rc) {
5676 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
5677 LOG_DISCOVERY,
Colin Ian King7afc0ce2018-05-03 10:26:12 +01005678 "2772 Issue FCF rediscover mailbox "
James Smartc2b97122013-05-31 17:05:36 -04005679 "command failed, fail through to FCF "
5680 "dead event\n");
5681 spin_lock_irq(&phba->hbalock);
5682 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
5683 spin_unlock_irq(&phba->hbalock);
5684 /*
5685 * Last resort will fail over by treating this
5686 * as a link down to FCF registration.
5687 */
5688 lpfc_sli4_fcf_dead_failthrough(phba);
5689 } else {
5690 /* Reset FCF roundrobin bmask for new discovery */
5691 lpfc_sli4_clear_fcf_rr_bmask(phba);
5692 /*
5693 * Handling fast FCF failover to a DEAD FCF event is
5694 * considered equalivant to receiving CVL to all vports.
5695 */
5696 lpfc_sli4_perform_all_vport_cvl(phba);
5697 }
James Smartda0436e2009-05-22 14:51:39 -04005698 break;
James Smart70f3c072010-12-15 17:57:33 -05005699 case LPFC_FIP_EVENT_TYPE_CVL:
James Smart80c17842012-03-01 22:35:45 -05005700 phba->fcoe_cvl_eventtag = acqe_fip->event_tag;
James Smart0c9ab6f2010-02-26 14:15:57 -05005701 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
James Smart6669f9b2009-10-02 15:16:45 -04005702 "2718 Clear Virtual Link Received for VPI 0x%x"
James Smart70f3c072010-12-15 17:57:33 -05005703 " tag 0x%x\n", acqe_fip->index, acqe_fip->event_tag);
James Smart6d368e52011-05-24 11:44:12 -04005704
James Smart6669f9b2009-10-02 15:16:45 -04005705 vport = lpfc_find_vport_by_vpid(phba,
James Smart5248a742011-07-22 18:37:06 -04005706 acqe_fip->index);
James Smartfc2b9892010-02-26 14:15:29 -05005707 ndlp = lpfc_sli4_perform_vport_cvl(vport);
James Smart6669f9b2009-10-02 15:16:45 -04005708 if (!ndlp)
5709 break;
James Smart695a8142010-01-26 23:08:03 -05005710 active_vlink_present = 0;
5711
5712 vports = lpfc_create_vport_work_array(phba);
5713 if (vports) {
5714 for (i = 0; i <= phba->max_vports && vports[i] != NULL;
5715 i++) {
5716 if ((!(vports[i]->fc_flag &
5717 FC_VPORT_CVL_RCVD)) &&
5718 (vports[i]->port_state > LPFC_FDISC)) {
5719 active_vlink_present = 1;
5720 break;
5721 }
5722 }
5723 lpfc_destroy_vport_work_array(phba, vports);
5724 }
5725
James Smartcc823552015-05-21 13:55:26 -04005726 /*
5727 * Don't re-instantiate if vport is marked for deletion.
5728 * If we are here first then vport_delete is going to wait
5729 * for discovery to complete.
5730 */
5731 if (!(vport->load_flag & FC_UNLOADING) &&
5732 active_vlink_present) {
James Smart695a8142010-01-26 23:08:03 -05005733 /*
5734 * If there are other active VLinks present,
5735 * re-instantiate the Vlink using FDISC.
5736 */
James Smart256ec0d2013-04-17 20:14:58 -04005737 mod_timer(&ndlp->nlp_delayfunc,
5738 jiffies + msecs_to_jiffies(1000));
James Smartfc2b9892010-02-26 14:15:29 -05005739 shost = lpfc_shost_from_vport(vport);
James Smart6669f9b2009-10-02 15:16:45 -04005740 spin_lock_irq(shost->host_lock);
5741 ndlp->nlp_flag |= NLP_DELAY_TMO;
5742 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05005743 ndlp->nlp_last_elscmd = ELS_CMD_FDISC;
5744 vport->port_state = LPFC_FDISC;
5745 } else {
James Smartecfd03c2010-02-12 14:41:27 -05005746 /*
5747 * Otherwise, we request port to rediscover
5748 * the entire FCF table for a fast recovery
5749 * from possible case that the current FCF
James Smart0c9ab6f2010-02-26 14:15:57 -05005750 * is no longer valid if we are not already
5751 * in the FCF failover process.
James Smartecfd03c2010-02-12 14:41:27 -05005752 */
James Smartfc2b9892010-02-26 14:15:29 -05005753 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05005754 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smartfc2b9892010-02-26 14:15:29 -05005755 spin_unlock_irq(&phba->hbalock);
5756 break;
5757 }
5758 /* Mark the fast failover process in progress */
James Smart0c9ab6f2010-02-26 14:15:57 -05005759 phba->fcf.fcf_flag |= FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05005760 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05005761 lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
5762 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04005763 "2773 Start FCF failover per CVL, "
James Smart70f3c072010-12-15 17:57:33 -05005764 "evt_tag:x%x\n", acqe_fip->event_tag);
James Smartecfd03c2010-02-12 14:41:27 -05005765 rc = lpfc_sli4_redisc_fcf_table(phba);
James Smartfc2b9892010-02-26 14:15:29 -05005766 if (rc) {
James Smart0c9ab6f2010-02-26 14:15:57 -05005767 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
5768 LOG_DISCOVERY,
5769 "2774 Issue FCF rediscover "
Colin Ian King7afc0ce2018-05-03 10:26:12 +01005770 "mailbox command failed, "
James Smart0c9ab6f2010-02-26 14:15:57 -05005771 "through to CVL event\n");
James Smartfc2b9892010-02-26 14:15:29 -05005772 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05005773 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05005774 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05005775 /*
5776 * Last resort will be re-try on the
5777 * the current registered FCF entry.
5778 */
5779 lpfc_retry_pport_discovery(phba);
James Smart38b92ef2010-08-04 16:11:39 -04005780 } else
5781 /*
5782 * Reset FCF roundrobin bmask for new
5783 * discovery.
5784 */
James Smart7d791df2011-07-22 18:37:52 -04005785 lpfc_sli4_clear_fcf_rr_bmask(phba);
James Smart6669f9b2009-10-02 15:16:45 -04005786 }
5787 break;
James Smartda0436e2009-05-22 14:51:39 -04005788 default:
5789 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5790 "0288 Unknown FCoE event type 0x%x event tag "
James Smart70f3c072010-12-15 17:57:33 -05005791 "0x%x\n", event_type, acqe_fip->event_tag);
James Smartda0436e2009-05-22 14:51:39 -04005792 break;
5793 }
5794}
5795
5796/**
5797 * lpfc_sli4_async_dcbx_evt - Process the asynchronous dcbx event
5798 * @phba: pointer to lpfc hba data structure.
5799 * @acqe_link: pointer to the async dcbx completion queue entry.
5800 *
5801 * This routine is to handle the SLI4 asynchronous dcbx event.
5802 **/
5803static void
5804lpfc_sli4_async_dcbx_evt(struct lpfc_hba *phba,
5805 struct lpfc_acqe_dcbx *acqe_dcbx)
5806{
James Smart4d9ab992009-10-02 15:16:39 -04005807 phba->fc_eventTag = acqe_dcbx->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04005808 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5809 "0290 The SLI4 DCBX asynchronous event is not "
5810 "handled yet\n");
5811}
5812
5813/**
James Smartb19a0612010-04-06 14:48:51 -04005814 * lpfc_sli4_async_grp5_evt - Process the asynchronous group5 event
5815 * @phba: pointer to lpfc hba data structure.
5816 * @acqe_link: pointer to the async grp5 completion queue entry.
5817 *
5818 * This routine is to handle the SLI4 asynchronous grp5 event. A grp5 event
5819 * is an asynchronous notified of a logical link speed change. The Port
5820 * reports the logical link speed in units of 10Mbps.
5821 **/
5822static void
5823lpfc_sli4_async_grp5_evt(struct lpfc_hba *phba,
5824 struct lpfc_acqe_grp5 *acqe_grp5)
5825{
5826 uint16_t prev_ll_spd;
5827
5828 phba->fc_eventTag = acqe_grp5->event_tag;
5829 phba->fcoe_eventtag = acqe_grp5->event_tag;
5830 prev_ll_spd = phba->sli4_hba.link_state.logical_speed;
5831 phba->sli4_hba.link_state.logical_speed =
James Smart8b68cd52012-09-29 11:32:37 -04005832 (bf_get(lpfc_acqe_grp5_llink_spd, acqe_grp5)) * 10;
James Smartb19a0612010-04-06 14:48:51 -04005833 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5834 "2789 GRP5 Async Event: Updating logical link speed "
James Smart8b68cd52012-09-29 11:32:37 -04005835 "from %dMbps to %dMbps\n", prev_ll_spd,
5836 phba->sli4_hba.link_state.logical_speed);
James Smartb19a0612010-04-06 14:48:51 -04005837}
5838
5839/**
James Smartda0436e2009-05-22 14:51:39 -04005840 * lpfc_sli4_async_event_proc - Process all the pending asynchronous event
5841 * @phba: pointer to lpfc hba data structure.
5842 *
5843 * This routine is invoked by the worker thread to process all the pending
5844 * SLI4 asynchronous events.
5845 **/
5846void lpfc_sli4_async_event_proc(struct lpfc_hba *phba)
5847{
5848 struct lpfc_cq_event *cq_event;
5849
5850 /* First, declare the async event has been handled */
5851 spin_lock_irq(&phba->hbalock);
5852 phba->hba_flag &= ~ASYNC_EVENT;
5853 spin_unlock_irq(&phba->hbalock);
5854 /* Now, handle all the async events */
5855 while (!list_empty(&phba->sli4_hba.sp_asynce_work_queue)) {
5856 /* Get the first event from the head of the event queue */
5857 spin_lock_irq(&phba->hbalock);
5858 list_remove_head(&phba->sli4_hba.sp_asynce_work_queue,
5859 cq_event, struct lpfc_cq_event, list);
5860 spin_unlock_irq(&phba->hbalock);
5861 /* Process the asynchronous event */
5862 switch (bf_get(lpfc_trailer_code, &cq_event->cqe.mcqe_cmpl)) {
5863 case LPFC_TRAILER_CODE_LINK:
5864 lpfc_sli4_async_link_evt(phba,
5865 &cq_event->cqe.acqe_link);
5866 break;
5867 case LPFC_TRAILER_CODE_FCOE:
James Smart70f3c072010-12-15 17:57:33 -05005868 lpfc_sli4_async_fip_evt(phba, &cq_event->cqe.acqe_fip);
James Smartda0436e2009-05-22 14:51:39 -04005869 break;
5870 case LPFC_TRAILER_CODE_DCBX:
5871 lpfc_sli4_async_dcbx_evt(phba,
5872 &cq_event->cqe.acqe_dcbx);
5873 break;
James Smartb19a0612010-04-06 14:48:51 -04005874 case LPFC_TRAILER_CODE_GRP5:
5875 lpfc_sli4_async_grp5_evt(phba,
5876 &cq_event->cqe.acqe_grp5);
5877 break;
James Smart70f3c072010-12-15 17:57:33 -05005878 case LPFC_TRAILER_CODE_FC:
5879 lpfc_sli4_async_fc_evt(phba, &cq_event->cqe.acqe_fc);
5880 break;
5881 case LPFC_TRAILER_CODE_SLI:
5882 lpfc_sli4_async_sli_evt(phba, &cq_event->cqe.acqe_sli);
5883 break;
James Smartda0436e2009-05-22 14:51:39 -04005884 default:
5885 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5886 "1804 Invalid asynchrous event code: "
5887 "x%x\n", bf_get(lpfc_trailer_code,
5888 &cq_event->cqe.mcqe_cmpl));
5889 break;
5890 }
5891 /* Free the completion event processed to the free pool */
5892 lpfc_sli4_cq_event_release(phba, cq_event);
5893 }
5894}
5895
5896/**
James Smartecfd03c2010-02-12 14:41:27 -05005897 * lpfc_sli4_fcf_redisc_event_proc - Process fcf table rediscovery event
5898 * @phba: pointer to lpfc hba data structure.
5899 *
5900 * This routine is invoked by the worker thread to process FCF table
5901 * rediscovery pending completion event.
5902 **/
5903void lpfc_sli4_fcf_redisc_event_proc(struct lpfc_hba *phba)
5904{
5905 int rc;
5906
5907 spin_lock_irq(&phba->hbalock);
5908 /* Clear FCF rediscovery timeout event */
5909 phba->fcf.fcf_flag &= ~FCF_REDISC_EVT;
5910 /* Clear driver fast failover FCF record flag */
5911 phba->fcf.failover_rec.flag = 0;
5912 /* Set state for FCF fast failover */
5913 phba->fcf.fcf_flag |= FCF_REDISC_FOV;
5914 spin_unlock_irq(&phba->hbalock);
5915
5916 /* Scan FCF table from the first entry to re-discover SAN */
James Smart0c9ab6f2010-02-26 14:15:57 -05005917 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04005918 "2777 Start post-quiescent FCF table scan\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05005919 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
James Smartecfd03c2010-02-12 14:41:27 -05005920 if (rc)
James Smart0c9ab6f2010-02-26 14:15:57 -05005921 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
5922 "2747 Issue FCF scan read FCF mailbox "
5923 "command failed 0x%x\n", rc);
James Smartecfd03c2010-02-12 14:41:27 -05005924}
5925
5926/**
James Smartda0436e2009-05-22 14:51:39 -04005927 * lpfc_api_table_setup - Set up per hba pci-device group func api jump table
5928 * @phba: pointer to lpfc hba data structure.
5929 * @dev_grp: The HBA PCI-Device group number.
5930 *
5931 * This routine is invoked to set up the per HBA PCI-Device group function
5932 * API jump table entries.
5933 *
5934 * Return: 0 if success, otherwise -ENODEV
5935 **/
5936int
5937lpfc_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
James Smart5b75da22008-12-04 22:39:35 -05005938{
5939 int rc;
5940
James Smartda0436e2009-05-22 14:51:39 -04005941 /* Set up lpfc PCI-device group */
5942 phba->pci_dev_grp = dev_grp;
James Smart5b75da22008-12-04 22:39:35 -05005943
James Smartda0436e2009-05-22 14:51:39 -04005944 /* The LPFC_PCI_DEV_OC uses SLI4 */
5945 if (dev_grp == LPFC_PCI_DEV_OC)
5946 phba->sli_rev = LPFC_SLI_REV4;
James Smart5b75da22008-12-04 22:39:35 -05005947
James Smartda0436e2009-05-22 14:51:39 -04005948 /* Set up device INIT API function jump table */
5949 rc = lpfc_init_api_table_setup(phba, dev_grp);
5950 if (rc)
5951 return -ENODEV;
5952 /* Set up SCSI API function jump table */
5953 rc = lpfc_scsi_api_table_setup(phba, dev_grp);
5954 if (rc)
5955 return -ENODEV;
5956 /* Set up SLI API function jump table */
5957 rc = lpfc_sli_api_table_setup(phba, dev_grp);
5958 if (rc)
5959 return -ENODEV;
5960 /* Set up MBOX API function jump table */
5961 rc = lpfc_mbox_api_table_setup(phba, dev_grp);
5962 if (rc)
5963 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05005964
James Smartda0436e2009-05-22 14:51:39 -04005965 return 0;
James Smart5b75da22008-12-04 22:39:35 -05005966}
5967
5968/**
James Smart3621a712009-04-06 18:47:14 -04005969 * lpfc_log_intr_mode - Log the active interrupt mode
James Smart5b75da22008-12-04 22:39:35 -05005970 * @phba: pointer to lpfc hba data structure.
5971 * @intr_mode: active interrupt mode adopted.
5972 *
5973 * This routine it invoked to log the currently used active interrupt mode
5974 * to the device.
James Smart3772a992009-05-22 14:50:54 -04005975 **/
5976static void lpfc_log_intr_mode(struct lpfc_hba *phba, uint32_t intr_mode)
James Smart5b75da22008-12-04 22:39:35 -05005977{
5978 switch (intr_mode) {
5979 case 0:
5980 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5981 "0470 Enable INTx interrupt mode.\n");
5982 break;
5983 case 1:
5984 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5985 "0481 Enabled MSI interrupt mode.\n");
5986 break;
5987 case 2:
5988 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5989 "0480 Enabled MSI-X interrupt mode.\n");
5990 break;
5991 default:
5992 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5993 "0482 Illegal interrupt mode.\n");
5994 break;
5995 }
5996 return;
5997}
5998
James Smart5b75da22008-12-04 22:39:35 -05005999/**
James Smartdcaa2132019-11-04 16:57:06 -08006000 * lpfc_cpumask_of_node_init - initalizes cpumask of phba's NUMA node
6001 * @phba: Pointer to HBA context object.
6002 *
6003 **/
6004static void
6005lpfc_cpumask_of_node_init(struct lpfc_hba *phba)
6006{
6007 unsigned int cpu, numa_node;
6008 struct cpumask *numa_mask = NULL;
6009
6010#ifdef CONFIG_NUMA
6011 numa_node = phba->pcidev->dev.numa_node;
6012#else
6013 numa_node = NUMA_NO_NODE;
6014#endif
6015 numa_mask = &phba->sli4_hba.numa_mask;
6016
6017 cpumask_clear(numa_mask);
6018
6019 /* Check if we're a NUMA architecture */
6020 if (!cpumask_of_node(numa_node))
6021 return;
6022
6023 for_each_possible_cpu(cpu)
6024 if (cpu_to_node(cpu) == numa_node)
6025 cpumask_set_cpu(cpu, numa_mask);
6026}
6027
6028/**
James Smart3772a992009-05-22 14:50:54 -04006029 * lpfc_enable_pci_dev - Enable a generic PCI device.
James Smart5b75da22008-12-04 22:39:35 -05006030 * @phba: pointer to lpfc hba data structure.
6031 *
James Smart3772a992009-05-22 14:50:54 -04006032 * This routine is invoked to enable the PCI device that is common to all
6033 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05006034 *
6035 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006036 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04006037 * other values - error
James Smart5b75da22008-12-04 22:39:35 -05006038 **/
James Smart3772a992009-05-22 14:50:54 -04006039static int
6040lpfc_enable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05006041{
James Smart3772a992009-05-22 14:50:54 -04006042 struct pci_dev *pdev;
James Smart5b75da22008-12-04 22:39:35 -05006043
James Smart3772a992009-05-22 14:50:54 -04006044 /* Obtain PCI device reference */
6045 if (!phba->pcidev)
6046 goto out_error;
6047 else
6048 pdev = phba->pcidev;
James Smart3772a992009-05-22 14:50:54 -04006049 /* Enable PCI device */
6050 if (pci_enable_device_mem(pdev))
6051 goto out_error;
6052 /* Request PCI resource for the device */
Johannes Thumshirne0c04832016-06-07 09:44:03 +02006053 if (pci_request_mem_regions(pdev, LPFC_DRIVER_NAME))
James Smart3772a992009-05-22 14:50:54 -04006054 goto out_disable_device;
6055 /* Set up device as PCI master and save state for EEH */
6056 pci_set_master(pdev);
6057 pci_try_set_mwi(pdev);
6058 pci_save_state(pdev);
James Smart5b75da22008-12-04 22:39:35 -05006059
James Smart05580562011-05-24 11:40:48 -04006060 /* PCIe EEH recovery on powerpc platforms needs fundamental reset */
Jon Mason453193e2013-09-11 15:38:13 -07006061 if (pci_is_pcie(pdev))
James Smart05580562011-05-24 11:40:48 -04006062 pdev->needs_freset = 1;
6063
James Smart3772a992009-05-22 14:50:54 -04006064 return 0;
James Smart5b75da22008-12-04 22:39:35 -05006065
James Smart3772a992009-05-22 14:50:54 -04006066out_disable_device:
6067 pci_disable_device(pdev);
6068out_error:
James Smart079b5c92011-08-21 21:48:49 -04006069 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
Johannes Thumshirne0c04832016-06-07 09:44:03 +02006070 "1401 Failed to enable pci device\n");
James Smart3772a992009-05-22 14:50:54 -04006071 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05006072}
6073
6074/**
James Smart3772a992009-05-22 14:50:54 -04006075 * lpfc_disable_pci_dev - Disable a generic PCI device.
James Smart5b75da22008-12-04 22:39:35 -05006076 * @phba: pointer to lpfc hba data structure.
6077 *
James Smart3772a992009-05-22 14:50:54 -04006078 * This routine is invoked to disable the PCI device that is common to all
6079 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05006080 **/
6081static void
James Smart3772a992009-05-22 14:50:54 -04006082lpfc_disable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05006083{
James Smart3772a992009-05-22 14:50:54 -04006084 struct pci_dev *pdev;
James Smart5b75da22008-12-04 22:39:35 -05006085
James Smart3772a992009-05-22 14:50:54 -04006086 /* Obtain PCI device reference */
6087 if (!phba->pcidev)
6088 return;
6089 else
6090 pdev = phba->pcidev;
James Smart3772a992009-05-22 14:50:54 -04006091 /* Release PCI resource and disable PCI device */
Johannes Thumshirne0c04832016-06-07 09:44:03 +02006092 pci_release_mem_regions(pdev);
James Smart3772a992009-05-22 14:50:54 -04006093 pci_disable_device(pdev);
James Smart5b75da22008-12-04 22:39:35 -05006094
6095 return;
6096}
6097
6098/**
James Smart3772a992009-05-22 14:50:54 -04006099 * lpfc_reset_hba - Reset a hba
6100 * @phba: pointer to lpfc hba data structure.
James Smarte59058c2008-08-24 21:49:00 -04006101 *
James Smart3772a992009-05-22 14:50:54 -04006102 * This routine is invoked to reset a hba device. It brings the HBA
6103 * offline, performs a board restart, and then brings the board back
6104 * online. The lpfc_offline calls lpfc_sli_hba_down which will clean up
6105 * on outstanding mailbox commands.
James Smarte59058c2008-08-24 21:49:00 -04006106 **/
James Smart3772a992009-05-22 14:50:54 -04006107void
6108lpfc_reset_hba(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05006109{
James Smart3772a992009-05-22 14:50:54 -04006110 /* If resets are disabled then set error state and return. */
6111 if (!phba->cfg_enable_hba_reset) {
6112 phba->link_state = LPFC_HBA_ERROR;
6113 return;
6114 }
James Smartee620212014-04-04 13:51:53 -04006115 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
6116 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
6117 else
6118 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
James Smart3772a992009-05-22 14:50:54 -04006119 lpfc_offline(phba);
6120 lpfc_sli_brdrestart(phba);
6121 lpfc_online(phba);
6122 lpfc_unblock_mgmt_io(phba);
6123}
dea31012005-04-17 16:05:31 -05006124
James Smart3772a992009-05-22 14:50:54 -04006125/**
James Smart0a96e972011-07-22 18:37:28 -04006126 * lpfc_sli_sriov_nr_virtfn_get - Get the number of sr-iov virtual functions
6127 * @phba: pointer to lpfc hba data structure.
6128 *
6129 * This function enables the PCI SR-IOV virtual functions to a physical
6130 * function. It invokes the PCI SR-IOV api with the @nr_vfn provided to
6131 * enable the number of virtual functions to the physical function. As
6132 * not all devices support SR-IOV, the return code from the pci_enable_sriov()
6133 * API call does not considered as an error condition for most of the device.
6134 **/
6135uint16_t
6136lpfc_sli_sriov_nr_virtfn_get(struct lpfc_hba *phba)
6137{
6138 struct pci_dev *pdev = phba->pcidev;
6139 uint16_t nr_virtfn;
6140 int pos;
6141
James Smart0a96e972011-07-22 18:37:28 -04006142 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
6143 if (pos == 0)
6144 return 0;
6145
6146 pci_read_config_word(pdev, pos + PCI_SRIOV_TOTAL_VF, &nr_virtfn);
6147 return nr_virtfn;
6148}
6149
6150/**
James Smart912e3ac2011-05-24 11:42:11 -04006151 * lpfc_sli_probe_sriov_nr_virtfn - Enable a number of sr-iov virtual functions
6152 * @phba: pointer to lpfc hba data structure.
6153 * @nr_vfn: number of virtual functions to be enabled.
6154 *
6155 * This function enables the PCI SR-IOV virtual functions to a physical
6156 * function. It invokes the PCI SR-IOV api with the @nr_vfn provided to
6157 * enable the number of virtual functions to the physical function. As
6158 * not all devices support SR-IOV, the return code from the pci_enable_sriov()
6159 * API call does not considered as an error condition for most of the device.
6160 **/
6161int
6162lpfc_sli_probe_sriov_nr_virtfn(struct lpfc_hba *phba, int nr_vfn)
6163{
6164 struct pci_dev *pdev = phba->pcidev;
James Smart0a96e972011-07-22 18:37:28 -04006165 uint16_t max_nr_vfn;
James Smart912e3ac2011-05-24 11:42:11 -04006166 int rc;
6167
James Smart0a96e972011-07-22 18:37:28 -04006168 max_nr_vfn = lpfc_sli_sriov_nr_virtfn_get(phba);
6169 if (nr_vfn > max_nr_vfn) {
6170 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6171 "3057 Requested vfs (%d) greater than "
6172 "supported vfs (%d)", nr_vfn, max_nr_vfn);
6173 return -EINVAL;
6174 }
6175
James Smart912e3ac2011-05-24 11:42:11 -04006176 rc = pci_enable_sriov(pdev, nr_vfn);
6177 if (rc) {
6178 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6179 "2806 Failed to enable sriov on this device "
6180 "with vfn number nr_vf:%d, rc:%d\n",
6181 nr_vfn, rc);
6182 } else
6183 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6184 "2807 Successful enable sriov on this device "
6185 "with vfn number nr_vf:%d\n", nr_vfn);
6186 return rc;
6187}
6188
6189/**
James Smart895427b2017-02-12 13:52:30 -08006190 * lpfc_setup_driver_resource_phase1 - Phase1 etup driver internal resources.
James Smart3772a992009-05-22 14:50:54 -04006191 * @phba: pointer to lpfc hba data structure.
6192 *
James Smart895427b2017-02-12 13:52:30 -08006193 * This routine is invoked to set up the driver internal resources before the
6194 * device specific resource setup to support the HBA device it attached to.
James Smart3772a992009-05-22 14:50:54 -04006195 *
6196 * Return codes
James Smart895427b2017-02-12 13:52:30 -08006197 * 0 - successful
6198 * other values - error
James Smart3772a992009-05-22 14:50:54 -04006199 **/
6200static int
James Smart895427b2017-02-12 13:52:30 -08006201lpfc_setup_driver_resource_phase1(struct lpfc_hba *phba)
James Smart3772a992009-05-22 14:50:54 -04006202{
James Smart895427b2017-02-12 13:52:30 -08006203 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05006204
James Smart3772a992009-05-22 14:50:54 -04006205 /*
James Smart895427b2017-02-12 13:52:30 -08006206 * Driver resources common to all SLI revisions
James Smart3772a992009-05-22 14:50:54 -04006207 */
James Smart895427b2017-02-12 13:52:30 -08006208 atomic_set(&phba->fast_event_count, 0);
6209 spin_lock_init(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006210
James Smart895427b2017-02-12 13:52:30 -08006211 /* Initialize ndlp management spinlock */
6212 spin_lock_init(&phba->ndlp_lock);
James Smart3772a992009-05-22 14:50:54 -04006213
James Smart523128e2018-09-10 10:30:46 -07006214 /* Initialize port_list spinlock */
6215 spin_lock_init(&phba->port_list_lock);
James Smart895427b2017-02-12 13:52:30 -08006216 INIT_LIST_HEAD(&phba->port_list);
James Smart523128e2018-09-10 10:30:46 -07006217
James Smart895427b2017-02-12 13:52:30 -08006218 INIT_LIST_HEAD(&phba->work_list);
6219 init_waitqueue_head(&phba->wait_4_mlo_m_q);
6220
6221 /* Initialize the wait queue head for the kernel thread */
6222 init_waitqueue_head(&phba->work_waitq);
6223
6224 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smartf358dd02017-02-12 13:52:34 -08006225 "1403 Protocols supported %s %s %s\n",
James Smart895427b2017-02-12 13:52:30 -08006226 ((phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) ?
6227 "SCSI" : " "),
6228 ((phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) ?
James Smartf358dd02017-02-12 13:52:34 -08006229 "NVME" : " "),
6230 (phba->nvmet_support ? "NVMET" : " "));
James Smart895427b2017-02-12 13:52:30 -08006231
James Smart0794d602019-01-28 11:14:19 -08006232 /* Initialize the IO buffer list used by driver for SLI3 SCSI */
6233 spin_lock_init(&phba->scsi_buf_list_get_lock);
6234 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_get);
6235 spin_lock_init(&phba->scsi_buf_list_put_lock);
6236 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
James Smart895427b2017-02-12 13:52:30 -08006237
6238 /* Initialize the fabric iocb list */
6239 INIT_LIST_HEAD(&phba->fabric_iocb_list);
6240
6241 /* Initialize list to save ELS buffers */
6242 INIT_LIST_HEAD(&phba->elsbuf);
6243
6244 /* Initialize FCF connection rec list */
6245 INIT_LIST_HEAD(&phba->fcf_conn_rec_list);
6246
6247 /* Initialize OAS configuration list */
6248 spin_lock_init(&phba->devicelock);
6249 INIT_LIST_HEAD(&phba->luns);
6250
James Smart3772a992009-05-22 14:50:54 -04006251 /* MBOX heartbeat timer */
Kees Cookf22eb4d2017-09-06 20:24:26 -07006252 timer_setup(&psli->mbox_tmo, lpfc_mbox_timeout, 0);
James Smart3772a992009-05-22 14:50:54 -04006253 /* Fabric block timer */
Kees Cookf22eb4d2017-09-06 20:24:26 -07006254 timer_setup(&phba->fabric_block_timer, lpfc_fabric_block_timeout, 0);
James Smart3772a992009-05-22 14:50:54 -04006255 /* EA polling mode timer */
Kees Cookf22eb4d2017-09-06 20:24:26 -07006256 timer_setup(&phba->eratt_poll, lpfc_poll_eratt, 0);
James Smart895427b2017-02-12 13:52:30 -08006257 /* Heartbeat timer */
Kees Cookf22eb4d2017-09-06 20:24:26 -07006258 timer_setup(&phba->hb_tmofunc, lpfc_hb_timeout, 0);
James Smart895427b2017-02-12 13:52:30 -08006259
James Smart32517fc2019-01-28 11:14:33 -08006260 INIT_DELAYED_WORK(&phba->eq_delay_work, lpfc_hb_eq_delay_work);
6261
James Smart895427b2017-02-12 13:52:30 -08006262 return 0;
6263}
6264
6265/**
6266 * lpfc_sli_driver_resource_setup - Setup driver internal resources for SLI3 dev
6267 * @phba: pointer to lpfc hba data structure.
6268 *
6269 * This routine is invoked to set up the driver internal resources specific to
6270 * support the SLI-3 HBA device it attached to.
6271 *
6272 * Return codes
6273 * 0 - successful
6274 * other values - error
6275 **/
6276static int
6277lpfc_sli_driver_resource_setup(struct lpfc_hba *phba)
6278{
James Smart0794d602019-01-28 11:14:19 -08006279 int rc, entry_sz;
James Smart895427b2017-02-12 13:52:30 -08006280
6281 /*
6282 * Initialize timers used by driver
6283 */
6284
6285 /* FCP polling mode timer */
Kees Cookf22eb4d2017-09-06 20:24:26 -07006286 timer_setup(&phba->fcp_poll_timer, lpfc_poll_timeout, 0);
James Smart3772a992009-05-22 14:50:54 -04006287
6288 /* Host attention work mask setup */
6289 phba->work_ha_mask = (HA_ERATT | HA_MBATT | HA_LATT);
6290 phba->work_ha_mask |= (HA_RXMASK << (LPFC_ELS_RING * 4));
6291
6292 /* Get all the module params for configuring this host */
6293 lpfc_get_cfgparam(phba);
James Smart895427b2017-02-12 13:52:30 -08006294 /* Set up phase-1 common device driver resources */
6295
6296 rc = lpfc_setup_driver_resource_phase1(phba);
6297 if (rc)
6298 return -ENODEV;
6299
James Smart49198b32010-04-06 15:04:33 -04006300 if (phba->pcidev->device == PCI_DEVICE_ID_HORNET) {
6301 phba->menlo_flag |= HBA_MENLO_SUPPORT;
6302 /* check for menlo minimum sg count */
6303 if (phba->cfg_sg_seg_cnt < LPFC_DEFAULT_MENLO_SG_SEG_CNT)
6304 phba->cfg_sg_seg_cnt = LPFC_DEFAULT_MENLO_SG_SEG_CNT;
6305 }
6306
James Smart895427b2017-02-12 13:52:30 -08006307 if (!phba->sli.sli3_ring)
Kees Cook6396bb22018-06-12 14:03:40 -07006308 phba->sli.sli3_ring = kcalloc(LPFC_SLI3_MAX_RING,
6309 sizeof(struct lpfc_sli_ring),
6310 GFP_KERNEL);
James Smart895427b2017-02-12 13:52:30 -08006311 if (!phba->sli.sli3_ring)
James Smart2a76a282012-08-03 12:35:54 -04006312 return -ENOMEM;
6313
James Smart3772a992009-05-22 14:50:54 -04006314 /*
James Smart96f70772013-04-17 20:16:15 -04006315 * Since lpfc_sg_seg_cnt is module parameter, the sg_dma_buf_size
James Smart3772a992009-05-22 14:50:54 -04006316 * used to create the sg_dma_buf_pool must be dynamically calculated.
James Smart3772a992009-05-22 14:50:54 -04006317 */
James Smart3772a992009-05-22 14:50:54 -04006318
James Smart96f70772013-04-17 20:16:15 -04006319 /* Initialize the host templates the configured values. */
James Smart3772a992009-05-22 14:50:54 -04006320 lpfc_vport_template.sg_tablesize = phba->cfg_sg_seg_cnt;
James Smart96418b52017-03-04 09:30:31 -08006321 lpfc_template_no_hr.sg_tablesize = phba->cfg_sg_seg_cnt;
6322 lpfc_template.sg_tablesize = phba->cfg_sg_seg_cnt;
James Smart3772a992009-05-22 14:50:54 -04006323
James Smart0794d602019-01-28 11:14:19 -08006324 if (phba->sli_rev == LPFC_SLI_REV4)
6325 entry_sz = sizeof(struct sli4_sge);
6326 else
6327 entry_sz = sizeof(struct ulp_bde64);
6328
James Smart96f70772013-04-17 20:16:15 -04006329 /* There are going to be 2 reserved BDEs: 1 FCP cmnd + 1 FCP rsp */
6330 if (phba->cfg_enable_bg) {
6331 /*
6332 * The scsi_buf for a T10-DIF I/O will hold the FCP cmnd,
6333 * the FCP rsp, and a BDE for each. Sice we have no control
6334 * over how many protection data segments the SCSI Layer
6335 * will hand us (ie: there could be one for every block
6336 * in the IO), we just allocate enough BDEs to accomidate
6337 * our max amount and we need to limit lpfc_sg_seg_cnt to
6338 * minimize the risk of running out.
6339 */
6340 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
6341 sizeof(struct fcp_rsp) +
James Smart0794d602019-01-28 11:14:19 -08006342 (LPFC_MAX_SG_SEG_CNT * entry_sz);
James Smart96f70772013-04-17 20:16:15 -04006343
6344 if (phba->cfg_sg_seg_cnt > LPFC_MAX_SG_SEG_CNT_DIF)
6345 phba->cfg_sg_seg_cnt = LPFC_MAX_SG_SEG_CNT_DIF;
6346
6347 /* Total BDEs in BPL for scsi_sg_list and scsi_sg_prot_list */
6348 phba->cfg_total_seg_cnt = LPFC_MAX_SG_SEG_CNT;
6349 } else {
6350 /*
6351 * The scsi_buf for a regular I/O will hold the FCP cmnd,
6352 * the FCP rsp, a BDE for each, and a BDE for up to
6353 * cfg_sg_seg_cnt data segments.
6354 */
6355 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
6356 sizeof(struct fcp_rsp) +
James Smart0794d602019-01-28 11:14:19 -08006357 ((phba->cfg_sg_seg_cnt + 2) * entry_sz);
James Smart96f70772013-04-17 20:16:15 -04006358
6359 /* Total BDEs in BPL for scsi_sg_list */
6360 phba->cfg_total_seg_cnt = phba->cfg_sg_seg_cnt + 2;
6361 }
6362
6363 lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_FCP,
6364 "9088 sg_tablesize:%d dmabuf_size:%d total_bde:%d\n",
6365 phba->cfg_sg_seg_cnt, phba->cfg_sg_dma_buf_size,
6366 phba->cfg_total_seg_cnt);
6367
James Smart3772a992009-05-22 14:50:54 -04006368 phba->max_vpi = LPFC_MAX_VPI;
6369 /* This will be set to correct value after config_port mbox */
6370 phba->max_vports = 0;
6371
6372 /*
6373 * Initialize the SLI Layer to run with lpfc HBAs.
6374 */
6375 lpfc_sli_setup(phba);
James Smart895427b2017-02-12 13:52:30 -08006376 lpfc_sli_queue_init(phba);
James Smart3772a992009-05-22 14:50:54 -04006377
6378 /* Allocate device driver memory */
6379 if (lpfc_mem_alloc(phba, BPL_ALIGN_SZ))
6380 return -ENOMEM;
6381
James Smartd79c9e92019-08-14 16:57:09 -07006382 phba->lpfc_sg_dma_buf_pool =
6383 dma_pool_create("lpfc_sg_dma_buf_pool",
6384 &phba->pcidev->dev, phba->cfg_sg_dma_buf_size,
6385 BPL_ALIGN_SZ, 0);
6386
6387 if (!phba->lpfc_sg_dma_buf_pool)
6388 goto fail_free_mem;
6389
6390 phba->lpfc_cmd_rsp_buf_pool =
6391 dma_pool_create("lpfc_cmd_rsp_buf_pool",
6392 &phba->pcidev->dev,
6393 sizeof(struct fcp_cmnd) +
6394 sizeof(struct fcp_rsp),
6395 BPL_ALIGN_SZ, 0);
6396
6397 if (!phba->lpfc_cmd_rsp_buf_pool)
6398 goto fail_free_dma_buf_pool;
6399
James Smart912e3ac2011-05-24 11:42:11 -04006400 /*
6401 * Enable sr-iov virtual functions if supported and configured
6402 * through the module parameter.
6403 */
6404 if (phba->cfg_sriov_nr_virtfn > 0) {
6405 rc = lpfc_sli_probe_sriov_nr_virtfn(phba,
6406 phba->cfg_sriov_nr_virtfn);
6407 if (rc) {
6408 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6409 "2808 Requested number of SR-IOV "
6410 "virtual functions (%d) is not "
6411 "supported\n",
6412 phba->cfg_sriov_nr_virtfn);
6413 phba->cfg_sriov_nr_virtfn = 0;
6414 }
6415 }
6416
James Smart3772a992009-05-22 14:50:54 -04006417 return 0;
James Smartd79c9e92019-08-14 16:57:09 -07006418
6419fail_free_dma_buf_pool:
6420 dma_pool_destroy(phba->lpfc_sg_dma_buf_pool);
6421 phba->lpfc_sg_dma_buf_pool = NULL;
6422fail_free_mem:
6423 lpfc_mem_free(phba);
6424 return -ENOMEM;
James Smart3772a992009-05-22 14:50:54 -04006425}
6426
6427/**
6428 * lpfc_sli_driver_resource_unset - Unset drvr internal resources for SLI3 dev
6429 * @phba: pointer to lpfc hba data structure.
6430 *
6431 * This routine is invoked to unset the driver internal resources set up
6432 * specific for supporting the SLI-3 HBA device it attached to.
6433 **/
6434static void
6435lpfc_sli_driver_resource_unset(struct lpfc_hba *phba)
6436{
6437 /* Free device driver memory allocated */
6438 lpfc_mem_free_all(phba);
6439
6440 return;
6441}
6442
6443/**
James Smartda0436e2009-05-22 14:51:39 -04006444 * lpfc_sli4_driver_resource_setup - Setup drvr internal resources for SLI4 dev
6445 * @phba: pointer to lpfc hba data structure.
6446 *
6447 * This routine is invoked to set up the driver internal resources specific to
6448 * support the SLI-4 HBA device it attached to.
6449 *
6450 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006451 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04006452 * other values - error
6453 **/
6454static int
6455lpfc_sli4_driver_resource_setup(struct lpfc_hba *phba)
6456{
James Smart28baac72010-02-12 14:42:03 -05006457 LPFC_MBOXQ_t *mboxq;
James Smartf358dd02017-02-12 13:52:34 -08006458 MAILBOX_t *mb;
James Smart895427b2017-02-12 13:52:30 -08006459 int rc, i, max_buf_size;
James Smart28baac72010-02-12 14:42:03 -05006460 uint8_t pn_page[LPFC_MAX_SUPPORTED_PAGES] = {0};
6461 struct lpfc_mqe *mqe;
James Smart09294d42013-04-17 20:16:05 -04006462 int longs;
James Smart81e6a632017-11-20 16:00:43 -08006463 int extra;
James Smartf358dd02017-02-12 13:52:34 -08006464 uint64_t wwn;
James Smartb92dc722018-05-24 21:09:01 -07006465 u32 if_type;
6466 u32 if_fam;
James Smartda0436e2009-05-22 14:51:39 -04006467
James Smart895427b2017-02-12 13:52:30 -08006468 phba->sli4_hba.num_present_cpu = lpfc_present_cpu;
James Smart222e9232019-01-28 11:14:35 -08006469 phba->sli4_hba.num_possible_cpu = num_possible_cpus();
James Smart895427b2017-02-12 13:52:30 -08006470 phba->sli4_hba.curr_disp_cpu = 0;
James Smartdcaa2132019-11-04 16:57:06 -08006471 lpfc_cpumask_of_node_init(phba);
James Smart895427b2017-02-12 13:52:30 -08006472
James Smart716d3bc2013-09-06 12:18:28 -04006473 /* Get all the module params for configuring this host */
6474 lpfc_get_cfgparam(phba);
6475
James Smart895427b2017-02-12 13:52:30 -08006476 /* Set up phase-1 common device driver resources */
6477 rc = lpfc_setup_driver_resource_phase1(phba);
6478 if (rc)
6479 return -ENODEV;
6480
James Smartda0436e2009-05-22 14:51:39 -04006481 /* Before proceed, wait for POST done and device ready */
6482 rc = lpfc_sli4_post_status_check(phba);
6483 if (rc)
6484 return -ENODEV;
6485
James Smart3cee98d2019-08-14 16:56:34 -07006486 /* Allocate all driver workqueues here */
6487
6488 /* The lpfc_wq workqueue for deferred irq use */
6489 phba->wq = alloc_workqueue("lpfc_wq", WQ_MEM_RECLAIM, 0);
6490
James Smartda0436e2009-05-22 14:51:39 -04006491 /*
6492 * Initialize timers used by driver
6493 */
6494
Kees Cookf22eb4d2017-09-06 20:24:26 -07006495 timer_setup(&phba->rrq_tmr, lpfc_rrq_timeout, 0);
James Smartda0436e2009-05-22 14:51:39 -04006496
James Smartecfd03c2010-02-12 14:41:27 -05006497 /* FCF rediscover timer */
Kees Cookf22eb4d2017-09-06 20:24:26 -07006498 timer_setup(&phba->fcf.redisc_wait, lpfc_sli4_fcf_redisc_wait_tmo, 0);
James Smartecfd03c2010-02-12 14:41:27 -05006499
James Smartda0436e2009-05-22 14:51:39 -04006500 /*
James Smart7ad20aa2011-05-24 11:44:28 -04006501 * Control structure for handling external multi-buffer mailbox
6502 * command pass-through.
6503 */
6504 memset((uint8_t *)&phba->mbox_ext_buf_ctx, 0,
6505 sizeof(struct lpfc_mbox_ext_buf_ctx));
6506 INIT_LIST_HEAD(&phba->mbox_ext_buf_ctx.ext_dmabuf_list);
6507
James Smartda0436e2009-05-22 14:51:39 -04006508 phba->max_vpi = LPFC_MAX_VPI;
James Smart67d12732012-08-03 12:36:13 -04006509
James Smartda0436e2009-05-22 14:51:39 -04006510 /* This will be set to correct value after the read_config mbox */
6511 phba->max_vports = 0;
6512
6513 /* Program the default value of vlan_id and fc_map */
6514 phba->valid_vlan = 0;
6515 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
6516 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
6517 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
6518
6519 /*
James Smart2a76a282012-08-03 12:35:54 -04006520 * For SLI4, instead of using ring 0 (LPFC_FCP_RING) for FCP commands
James Smart895427b2017-02-12 13:52:30 -08006521 * we will associate a new ring, for each EQ/CQ/WQ tuple.
6522 * The WQ create will allocate the ring.
James Smart2a76a282012-08-03 12:35:54 -04006523 */
James Smart085c6472010-11-20 23:11:37 -05006524
James Smartda0436e2009-05-22 14:51:39 -04006525 /* Initialize buffer queue management fields */
James Smart895427b2017-02-12 13:52:30 -08006526 INIT_LIST_HEAD(&phba->hbqs[LPFC_ELS_HBQ].hbq_buffer_list);
James Smartda0436e2009-05-22 14:51:39 -04006527 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_sli4_rb_alloc;
6528 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_sli4_rb_free;
6529
6530 /*
6531 * Initialize the SLI Layer to run with lpfc SLI4 HBAs.
6532 */
James Smartc00f62e2019-08-14 16:57:11 -07006533 /* Initialize the Abort buffer list used by driver */
6534 spin_lock_init(&phba->sli4_hba.abts_io_buf_list_lock);
6535 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_io_buf_list);
James Smart895427b2017-02-12 13:52:30 -08006536
6537 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
6538 /* Initialize the Abort nvme buffer list used by driver */
James Smart5e5b5112019-01-28 11:14:22 -08006539 spin_lock_init(&phba->sli4_hba.abts_nvmet_buf_list_lock);
James Smart86c67372017-04-21 16:05:04 -07006540 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_nvmet_ctx_list);
James Smarta8cf5df2017-05-15 15:20:46 -07006541 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_nvmet_io_wait_list);
James Smart79d8c4c2019-05-21 17:48:56 -07006542 spin_lock_init(&phba->sli4_hba.t_active_list_lock);
6543 INIT_LIST_HEAD(&phba->sli4_hba.t_active_ctx_list);
James Smart895427b2017-02-12 13:52:30 -08006544 }
6545
James Smartda0436e2009-05-22 14:51:39 -04006546 /* This abort list used by worker thread */
James Smart895427b2017-02-12 13:52:30 -08006547 spin_lock_init(&phba->sli4_hba.sgl_list_lock);
James Smarta8cf5df2017-05-15 15:20:46 -07006548 spin_lock_init(&phba->sli4_hba.nvmet_io_wait_lock);
James Smartda0436e2009-05-22 14:51:39 -04006549
6550 /*
James Smart6d368e52011-05-24 11:44:12 -04006551 * Initialize driver internal slow-path work queues
James Smartda0436e2009-05-22 14:51:39 -04006552 */
6553
6554 /* Driver internel slow-path CQ Event pool */
6555 INIT_LIST_HEAD(&phba->sli4_hba.sp_cqe_event_pool);
6556 /* Response IOCB work queue list */
James Smart45ed1192009-10-02 15:17:02 -04006557 INIT_LIST_HEAD(&phba->sli4_hba.sp_queue_event);
James Smartda0436e2009-05-22 14:51:39 -04006558 /* Asynchronous event CQ Event work queue list */
6559 INIT_LIST_HEAD(&phba->sli4_hba.sp_asynce_work_queue);
6560 /* Fast-path XRI aborted CQ Event work queue list */
6561 INIT_LIST_HEAD(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
6562 /* Slow-path XRI aborted CQ Event work queue list */
6563 INIT_LIST_HEAD(&phba->sli4_hba.sp_els_xri_aborted_work_queue);
6564 /* Receive queue CQ Event work queue list */
6565 INIT_LIST_HEAD(&phba->sli4_hba.sp_unsol_work_queue);
6566
James Smart6d368e52011-05-24 11:44:12 -04006567 /* Initialize extent block lists. */
6568 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_blk_list);
6569 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_xri_blk_list);
6570 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_vfi_blk_list);
6571 INIT_LIST_HEAD(&phba->lpfc_vpi_blk_list);
6572
James Smartd1f525a2017-04-21 16:04:55 -07006573 /* Initialize mboxq lists. If the early init routines fail
6574 * these lists need to be correctly initialized.
6575 */
6576 INIT_LIST_HEAD(&phba->sli.mboxq);
6577 INIT_LIST_HEAD(&phba->sli.mboxq_cmpl);
6578
James Smart448193b2015-12-16 18:12:05 -05006579 /* initialize optic_state to 0xFF */
6580 phba->sli4_hba.lnk_info.optic_state = 0xff;
6581
James Smartda0436e2009-05-22 14:51:39 -04006582 /* Allocate device driver memory */
6583 rc = lpfc_mem_alloc(phba, SGL_ALIGN_SZ);
6584 if (rc)
6585 return -ENOMEM;
6586
James Smart2fcee4b2010-12-15 17:57:46 -05006587 /* IF Type 2 ports get initialized now. */
James Smart27d6ac02018-02-22 08:18:42 -08006588 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) >=
James Smart2fcee4b2010-12-15 17:57:46 -05006589 LPFC_SLI_INTF_IF_TYPE_2) {
6590 rc = lpfc_pci_function_reset(phba);
James Smart895427b2017-02-12 13:52:30 -08006591 if (unlikely(rc)) {
6592 rc = -ENODEV;
6593 goto out_free_mem;
6594 }
James Smart946727d2015-04-07 15:07:09 -04006595 phba->temp_sensor_support = 1;
James Smart2fcee4b2010-12-15 17:57:46 -05006596 }
6597
James Smartda0436e2009-05-22 14:51:39 -04006598 /* Create the bootstrap mailbox command */
6599 rc = lpfc_create_bootstrap_mbox(phba);
6600 if (unlikely(rc))
6601 goto out_free_mem;
6602
6603 /* Set up the host's endian order with the device. */
6604 rc = lpfc_setup_endian_order(phba);
6605 if (unlikely(rc))
6606 goto out_free_bsmbx;
6607
6608 /* Set up the hba's configuration parameters. */
6609 rc = lpfc_sli4_read_config(phba);
6610 if (unlikely(rc))
6611 goto out_free_bsmbx;
James Smartcff261f2013-12-17 20:29:47 -05006612 rc = lpfc_mem_alloc_active_rrq_pool_s4(phba);
6613 if (unlikely(rc))
6614 goto out_free_bsmbx;
James Smartda0436e2009-05-22 14:51:39 -04006615
James Smart2fcee4b2010-12-15 17:57:46 -05006616 /* IF Type 0 ports get initialized now. */
6617 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
6618 LPFC_SLI_INTF_IF_TYPE_0) {
6619 rc = lpfc_pci_function_reset(phba);
6620 if (unlikely(rc))
6621 goto out_free_bsmbx;
6622 }
James Smartda0436e2009-05-22 14:51:39 -04006623
James Smartcb5172e2010-03-15 11:25:07 -04006624 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
6625 GFP_KERNEL);
6626 if (!mboxq) {
6627 rc = -ENOMEM;
6628 goto out_free_bsmbx;
6629 }
6630
James Smartf358dd02017-02-12 13:52:34 -08006631 /* Check for NVMET being configured */
James Smart895427b2017-02-12 13:52:30 -08006632 phba->nvmet_support = 0;
James Smartf358dd02017-02-12 13:52:34 -08006633 if (lpfc_enable_nvmet_cnt) {
6634
6635 /* First get WWN of HBA instance */
6636 lpfc_read_nv(phba, mboxq);
6637 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6638 if (rc != MBX_SUCCESS) {
6639 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6640 "6016 Mailbox failed , mbxCmd x%x "
6641 "READ_NV, mbxStatus x%x\n",
6642 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
6643 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
James Smartd1f525a2017-04-21 16:04:55 -07006644 mempool_free(mboxq, phba->mbox_mem_pool);
James Smartf358dd02017-02-12 13:52:34 -08006645 rc = -EIO;
6646 goto out_free_bsmbx;
6647 }
6648 mb = &mboxq->u.mb;
6649 memcpy(&wwn, (char *)mb->un.varRDnvp.nodename,
6650 sizeof(uint64_t));
6651 wwn = cpu_to_be64(wwn);
6652 phba->sli4_hba.wwnn.u.name = wwn;
6653 memcpy(&wwn, (char *)mb->un.varRDnvp.portname,
6654 sizeof(uint64_t));
6655 /* wwn is WWPN of HBA instance */
6656 wwn = cpu_to_be64(wwn);
6657 phba->sli4_hba.wwpn.u.name = wwn;
6658
6659 /* Check to see if it matches any module parameter */
6660 for (i = 0; i < lpfc_enable_nvmet_cnt; i++) {
6661 if (wwn == lpfc_enable_nvmet[i]) {
James Smart7d708032017-03-08 14:36:01 -08006662#if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
James Smart3c603be2017-05-15 15:20:44 -07006663 if (lpfc_nvmet_mem_alloc(phba))
6664 break;
6665
6666 phba->nvmet_support = 1; /* a match */
6667
James Smartf358dd02017-02-12 13:52:34 -08006668 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6669 "6017 NVME Target %016llx\n",
6670 wwn);
James Smart7d708032017-03-08 14:36:01 -08006671#else
6672 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6673 "6021 Can't enable NVME Target."
6674 " NVME_TARGET_FC infrastructure"
6675 " is not in kernel\n");
6676#endif
James Smartc4908502019-01-28 11:14:28 -08006677 /* Not supported for NVMET */
6678 phba->cfg_xri_rebalancing = 0;
James Smart3c603be2017-05-15 15:20:44 -07006679 break;
James Smartf358dd02017-02-12 13:52:34 -08006680 }
6681 }
6682 }
James Smart895427b2017-02-12 13:52:30 -08006683
6684 lpfc_nvme_mod_param_dep(phba);
6685
James Smartfedd3b72011-02-16 12:39:24 -05006686 /* Get the Supported Pages if PORT_CAPABILITIES is supported by port. */
James Smartcb5172e2010-03-15 11:25:07 -04006687 lpfc_supported_pages(mboxq);
6688 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smartfedd3b72011-02-16 12:39:24 -05006689 if (!rc) {
6690 mqe = &mboxq->u.mqe;
6691 memcpy(&pn_page[0], ((uint8_t *)&mqe->un.supp_pages.word3),
6692 LPFC_MAX_SUPPORTED_PAGES);
6693 for (i = 0; i < LPFC_MAX_SUPPORTED_PAGES; i++) {
6694 switch (pn_page[i]) {
6695 case LPFC_SLI4_PARAMETERS:
6696 phba->sli4_hba.pc_sli4_params.supported = 1;
6697 break;
6698 default:
6699 break;
6700 }
6701 }
6702 /* Read the port's SLI4 Parameters capabilities if supported. */
6703 if (phba->sli4_hba.pc_sli4_params.supported)
6704 rc = lpfc_pc_sli4_params_get(phba, mboxq);
6705 if (rc) {
6706 mempool_free(mboxq, phba->mbox_mem_pool);
6707 rc = -EIO;
6708 goto out_free_bsmbx;
James Smartcb5172e2010-03-15 11:25:07 -04006709 }
6710 }
James Smart65791f12016-07-06 12:35:56 -07006711
James Smartfedd3b72011-02-16 12:39:24 -05006712 /*
6713 * Get sli4 parameters that override parameters from Port capabilities.
James Smart6d368e52011-05-24 11:44:12 -04006714 * If this call fails, it isn't critical unless the SLI4 parameters come
6715 * back in conflict.
James Smartfedd3b72011-02-16 12:39:24 -05006716 */
James Smart6d368e52011-05-24 11:44:12 -04006717 rc = lpfc_get_sli4_parameters(phba, mboxq);
6718 if (rc) {
James Smartb92dc722018-05-24 21:09:01 -07006719 if_type = bf_get(lpfc_sli_intf_if_type,
6720 &phba->sli4_hba.sli_intf);
6721 if_fam = bf_get(lpfc_sli_intf_sli_family,
6722 &phba->sli4_hba.sli_intf);
James Smart6d368e52011-05-24 11:44:12 -04006723 if (phba->sli4_hba.extents_in_use &&
6724 phba->sli4_hba.rpi_hdrs_in_use) {
6725 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6726 "2999 Unsupported SLI4 Parameters "
6727 "Extents and RPI headers enabled.\n");
James Smartb92dc722018-05-24 21:09:01 -07006728 if (if_type == LPFC_SLI_INTF_IF_TYPE_0 &&
6729 if_fam == LPFC_SLI_INTF_FAMILY_BE2) {
6730 mempool_free(mboxq, phba->mbox_mem_pool);
6731 rc = -EIO;
6732 goto out_free_bsmbx;
6733 }
James Smart6d368e52011-05-24 11:44:12 -04006734 }
James Smartb92dc722018-05-24 21:09:01 -07006735 if (!(if_type == LPFC_SLI_INTF_IF_TYPE_0 &&
6736 if_fam == LPFC_SLI_INTF_FAMILY_BE2)) {
6737 mempool_free(mboxq, phba->mbox_mem_pool);
6738 rc = -EIO;
6739 goto out_free_bsmbx;
6740 }
James Smart6d368e52011-05-24 11:44:12 -04006741 }
James Smart895427b2017-02-12 13:52:30 -08006742
James Smartd79c9e92019-08-14 16:57:09 -07006743 /*
6744 * 1 for cmd, 1 for rsp, NVME adds an extra one
6745 * for boundary conditions in its max_sgl_segment template.
6746 */
6747 extra = 2;
6748 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
6749 extra++;
6750
6751 /*
6752 * It doesn't matter what family our adapter is in, we are
6753 * limited to 2 Pages, 512 SGEs, for our SGL.
6754 * There are going to be 2 reserved SGEs: 1 FCP cmnd + 1 FCP rsp
6755 */
6756 max_buf_size = (2 * SLI4_PAGE_SIZE);
6757
6758 /*
6759 * Since lpfc_sg_seg_cnt is module param, the sg_dma_buf_size
6760 * used to create the sg_dma_buf_pool must be calculated.
6761 */
6762 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED) {
6763 /* Both cfg_enable_bg and cfg_external_dif code paths */
6764
6765 /*
6766 * The scsi_buf for a T10-DIF I/O holds the FCP cmnd,
6767 * the FCP rsp, and a SGE. Sice we have no control
6768 * over how many protection segments the SCSI Layer
6769 * will hand us (ie: there could be one for every block
6770 * in the IO), just allocate enough SGEs to accomidate
6771 * our max amount and we need to limit lpfc_sg_seg_cnt
6772 * to minimize the risk of running out.
6773 */
6774 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
6775 sizeof(struct fcp_rsp) + max_buf_size;
6776
6777 /* Total SGEs for scsi_sg_list and scsi_sg_prot_list */
6778 phba->cfg_total_seg_cnt = LPFC_MAX_SGL_SEG_CNT;
6779
6780 /*
6781 * If supporting DIF, reduce the seg count for scsi to
6782 * allow room for the DIF sges.
6783 */
6784 if (phba->cfg_enable_bg &&
6785 phba->cfg_sg_seg_cnt > LPFC_MAX_BG_SLI4_SEG_CNT_DIF)
6786 phba->cfg_scsi_seg_cnt = LPFC_MAX_BG_SLI4_SEG_CNT_DIF;
6787 else
6788 phba->cfg_scsi_seg_cnt = phba->cfg_sg_seg_cnt;
6789
6790 } else {
6791 /*
6792 * The scsi_buf for a regular I/O holds the FCP cmnd,
6793 * the FCP rsp, a SGE for each, and a SGE for up to
6794 * cfg_sg_seg_cnt data segments.
6795 */
6796 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
6797 sizeof(struct fcp_rsp) +
6798 ((phba->cfg_sg_seg_cnt + extra) *
6799 sizeof(struct sli4_sge));
6800
6801 /* Total SGEs for scsi_sg_list */
6802 phba->cfg_total_seg_cnt = phba->cfg_sg_seg_cnt + extra;
6803 phba->cfg_scsi_seg_cnt = phba->cfg_sg_seg_cnt;
6804
6805 /*
6806 * NOTE: if (phba->cfg_sg_seg_cnt + extra) <= 256 we only
6807 * need to post 1 page for the SGL.
6808 */
6809 }
6810
6811 if (phba->cfg_xpsgl && !phba->nvmet_support)
6812 phba->cfg_sg_dma_buf_size = LPFC_DEFAULT_XPSGL_SIZE;
6813 else if (phba->cfg_sg_dma_buf_size <= LPFC_MIN_SG_SLI4_BUF_SZ)
6814 phba->cfg_sg_dma_buf_size = LPFC_MIN_SG_SLI4_BUF_SZ;
6815 else
6816 phba->cfg_sg_dma_buf_size =
6817 SLI4_PAGE_ALIGN(phba->cfg_sg_dma_buf_size);
6818
6819 phba->border_sge_num = phba->cfg_sg_dma_buf_size /
6820 sizeof(struct sli4_sge);
6821
6822 /* Limit to LPFC_MAX_NVME_SEG_CNT for NVME. */
6823 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
6824 if (phba->cfg_sg_seg_cnt > LPFC_MAX_NVME_SEG_CNT) {
6825 lpfc_printf_log(phba, KERN_INFO, LOG_NVME | LOG_INIT,
6826 "6300 Reducing NVME sg segment "
6827 "cnt to %d\n",
6828 LPFC_MAX_NVME_SEG_CNT);
6829 phba->cfg_nvme_seg_cnt = LPFC_MAX_NVME_SEG_CNT;
6830 } else
6831 phba->cfg_nvme_seg_cnt = phba->cfg_sg_seg_cnt;
6832 }
6833
6834 /* Initialize the host templates with the updated values. */
6835 lpfc_vport_template.sg_tablesize = phba->cfg_scsi_seg_cnt;
6836 lpfc_template.sg_tablesize = phba->cfg_scsi_seg_cnt;
6837 lpfc_template_no_hr.sg_tablesize = phba->cfg_scsi_seg_cnt;
6838
6839 lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_FCP,
6840 "9087 sg_seg_cnt:%d dmabuf_size:%d "
6841 "total:%d scsi:%d nvme:%d\n",
6842 phba->cfg_sg_seg_cnt, phba->cfg_sg_dma_buf_size,
6843 phba->cfg_total_seg_cnt, phba->cfg_scsi_seg_cnt,
6844 phba->cfg_nvme_seg_cnt);
6845
6846 if (phba->cfg_sg_dma_buf_size < SLI4_PAGE_SIZE)
6847 i = phba->cfg_sg_dma_buf_size;
6848 else
6849 i = SLI4_PAGE_SIZE;
6850
6851 phba->lpfc_sg_dma_buf_pool =
6852 dma_pool_create("lpfc_sg_dma_buf_pool",
6853 &phba->pcidev->dev,
6854 phba->cfg_sg_dma_buf_size,
6855 i, 0);
6856 if (!phba->lpfc_sg_dma_buf_pool)
6857 goto out_free_bsmbx;
6858
6859 phba->lpfc_cmd_rsp_buf_pool =
6860 dma_pool_create("lpfc_cmd_rsp_buf_pool",
6861 &phba->pcidev->dev,
6862 sizeof(struct fcp_cmnd) +
6863 sizeof(struct fcp_rsp),
6864 i, 0);
6865 if (!phba->lpfc_cmd_rsp_buf_pool)
6866 goto out_free_sg_dma_buf;
6867
James Smartcb5172e2010-03-15 11:25:07 -04006868 mempool_free(mboxq, phba->mbox_mem_pool);
James Smart1ba981f2014-02-20 09:56:45 -05006869
6870 /* Verify OAS is supported */
6871 lpfc_sli4_oas_verify(phba);
James Smart1ba981f2014-02-20 09:56:45 -05006872
James Smartd2cc9bc2018-09-10 10:30:50 -07006873 /* Verify RAS support on adapter */
6874 lpfc_sli4_ras_init(phba);
6875
James Smart5350d872011-10-10 21:33:49 -04006876 /* Verify all the SLI4 queues */
6877 rc = lpfc_sli4_queue_verify(phba);
James Smartda0436e2009-05-22 14:51:39 -04006878 if (rc)
James Smartd79c9e92019-08-14 16:57:09 -07006879 goto out_free_cmd_rsp_buf;
James Smartda0436e2009-05-22 14:51:39 -04006880
6881 /* Create driver internal CQE event pool */
6882 rc = lpfc_sli4_cq_event_pool_create(phba);
6883 if (rc)
James Smartd79c9e92019-08-14 16:57:09 -07006884 goto out_free_cmd_rsp_buf;
James Smartda0436e2009-05-22 14:51:39 -04006885
James Smart8a9d2e82012-05-09 21:16:12 -04006886 /* Initialize sgl lists per host */
6887 lpfc_init_sgl_list(phba);
6888
6889 /* Allocate and initialize active sgl array */
James Smartda0436e2009-05-22 14:51:39 -04006890 rc = lpfc_init_active_sgl_array(phba);
6891 if (rc) {
6892 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6893 "1430 Failed to initialize sgl list.\n");
James Smart8a9d2e82012-05-09 21:16:12 -04006894 goto out_destroy_cq_event_pool;
James Smartda0436e2009-05-22 14:51:39 -04006895 }
James Smartda0436e2009-05-22 14:51:39 -04006896 rc = lpfc_sli4_init_rpi_hdrs(phba);
6897 if (rc) {
6898 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6899 "1432 Failed to initialize rpi headers.\n");
6900 goto out_free_active_sgl;
6901 }
6902
James Smarta93ff372010-10-22 11:06:08 -04006903 /* Allocate eligible FCF bmask memory for FCF roundrobin failover */
James Smart0c9ab6f2010-02-26 14:15:57 -05006904 longs = (LPFC_SLI4_FCF_TBL_INDX_MAX + BITS_PER_LONG - 1)/BITS_PER_LONG;
Kees Cook6396bb22018-06-12 14:03:40 -07006905 phba->fcf.fcf_rr_bmask = kcalloc(longs, sizeof(unsigned long),
James Smart0c9ab6f2010-02-26 14:15:57 -05006906 GFP_KERNEL);
6907 if (!phba->fcf.fcf_rr_bmask) {
6908 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6909 "2759 Failed allocate memory for FCF round "
6910 "robin failover bmask\n");
James Smart05580562011-05-24 11:40:48 -04006911 rc = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -05006912 goto out_remove_rpi_hdrs;
6913 }
6914
James Smart6a828b02019-01-28 11:14:31 -08006915 phba->sli4_hba.hba_eq_hdl = kcalloc(phba->cfg_irq_chann,
James Smartcdb42be2019-01-28 11:14:21 -08006916 sizeof(struct lpfc_hba_eq_hdl),
6917 GFP_KERNEL);
James Smart895427b2017-02-12 13:52:30 -08006918 if (!phba->sli4_hba.hba_eq_hdl) {
James Smart67d12732012-08-03 12:36:13 -04006919 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6920 "2572 Failed allocate memory for "
6921 "fast-path per-EQ handle array\n");
6922 rc = -ENOMEM;
6923 goto out_free_fcf_rr_bmask;
James Smartda0436e2009-05-22 14:51:39 -04006924 }
6925
James Smart222e9232019-01-28 11:14:35 -08006926 phba->sli4_hba.cpu_map = kcalloc(phba->sli4_hba.num_possible_cpu,
James Smart895427b2017-02-12 13:52:30 -08006927 sizeof(struct lpfc_vector_map_info),
6928 GFP_KERNEL);
James Smart7bb03bb2013-04-17 20:19:16 -04006929 if (!phba->sli4_hba.cpu_map) {
6930 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6931 "3327 Failed allocate memory for msi-x "
6932 "interrupt vector mapping\n");
6933 rc = -ENOMEM;
James Smart895427b2017-02-12 13:52:30 -08006934 goto out_free_hba_eq_hdl;
James Smart7bb03bb2013-04-17 20:19:16 -04006935 }
James Smartb246de12013-05-31 17:03:07 -04006936
James Smart32517fc2019-01-28 11:14:33 -08006937 phba->sli4_hba.eq_info = alloc_percpu(struct lpfc_eq_intr_info);
6938 if (!phba->sli4_hba.eq_info) {
6939 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6940 "3321 Failed allocation for per_cpu stats\n");
6941 rc = -ENOMEM;
6942 goto out_free_hba_cpu_map;
6943 }
James Smart912e3ac2011-05-24 11:42:11 -04006944 /*
6945 * Enable sr-iov virtual functions if supported and configured
6946 * through the module parameter.
6947 */
6948 if (phba->cfg_sriov_nr_virtfn > 0) {
6949 rc = lpfc_sli_probe_sriov_nr_virtfn(phba,
6950 phba->cfg_sriov_nr_virtfn);
6951 if (rc) {
6952 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6953 "3020 Requested number of SR-IOV "
6954 "virtual functions (%d) is not "
6955 "supported\n",
6956 phba->cfg_sriov_nr_virtfn);
6957 phba->cfg_sriov_nr_virtfn = 0;
6958 }
6959 }
6960
James Smart5248a742011-07-22 18:37:06 -04006961 return 0;
James Smartda0436e2009-05-22 14:51:39 -04006962
James Smart32517fc2019-01-28 11:14:33 -08006963out_free_hba_cpu_map:
6964 kfree(phba->sli4_hba.cpu_map);
James Smart895427b2017-02-12 13:52:30 -08006965out_free_hba_eq_hdl:
6966 kfree(phba->sli4_hba.hba_eq_hdl);
James Smart0c9ab6f2010-02-26 14:15:57 -05006967out_free_fcf_rr_bmask:
6968 kfree(phba->fcf.fcf_rr_bmask);
James Smartda0436e2009-05-22 14:51:39 -04006969out_remove_rpi_hdrs:
6970 lpfc_sli4_remove_rpi_hdrs(phba);
6971out_free_active_sgl:
6972 lpfc_free_active_sgl(phba);
James Smartda0436e2009-05-22 14:51:39 -04006973out_destroy_cq_event_pool:
6974 lpfc_sli4_cq_event_pool_destroy(phba);
James Smartd79c9e92019-08-14 16:57:09 -07006975out_free_cmd_rsp_buf:
6976 dma_pool_destroy(phba->lpfc_cmd_rsp_buf_pool);
6977 phba->lpfc_cmd_rsp_buf_pool = NULL;
6978out_free_sg_dma_buf:
6979 dma_pool_destroy(phba->lpfc_sg_dma_buf_pool);
6980 phba->lpfc_sg_dma_buf_pool = NULL;
James Smartda0436e2009-05-22 14:51:39 -04006981out_free_bsmbx:
6982 lpfc_destroy_bootstrap_mbox(phba);
6983out_free_mem:
6984 lpfc_mem_free(phba);
6985 return rc;
6986}
6987
6988/**
6989 * lpfc_sli4_driver_resource_unset - Unset drvr internal resources for SLI4 dev
6990 * @phba: pointer to lpfc hba data structure.
6991 *
6992 * This routine is invoked to unset the driver internal resources set up
6993 * specific for supporting the SLI-4 HBA device it attached to.
6994 **/
6995static void
6996lpfc_sli4_driver_resource_unset(struct lpfc_hba *phba)
6997{
6998 struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
6999
James Smart32517fc2019-01-28 11:14:33 -08007000 free_percpu(phba->sli4_hba.eq_info);
7001
James Smart7bb03bb2013-04-17 20:19:16 -04007002 /* Free memory allocated for msi-x interrupt vector to CPU mapping */
7003 kfree(phba->sli4_hba.cpu_map);
James Smart222e9232019-01-28 11:14:35 -08007004 phba->sli4_hba.num_possible_cpu = 0;
James Smart7bb03bb2013-04-17 20:19:16 -04007005 phba->sli4_hba.num_present_cpu = 0;
James Smart76fd07a2014-02-20 09:57:18 -05007006 phba->sli4_hba.curr_disp_cpu = 0;
James Smartdcaa2132019-11-04 16:57:06 -08007007 cpumask_clear(&phba->sli4_hba.numa_mask);
James Smart7bb03bb2013-04-17 20:19:16 -04007008
James Smartda0436e2009-05-22 14:51:39 -04007009 /* Free memory allocated for fast-path work queue handles */
James Smart895427b2017-02-12 13:52:30 -08007010 kfree(phba->sli4_hba.hba_eq_hdl);
James Smartda0436e2009-05-22 14:51:39 -04007011
7012 /* Free the allocated rpi headers. */
7013 lpfc_sli4_remove_rpi_hdrs(phba);
James Smartd11e31d2009-06-10 17:23:06 -04007014 lpfc_sli4_remove_rpis(phba);
James Smartda0436e2009-05-22 14:51:39 -04007015
James Smart0c9ab6f2010-02-26 14:15:57 -05007016 /* Free eligible FCF index bmask */
7017 kfree(phba->fcf.fcf_rr_bmask);
7018
James Smartda0436e2009-05-22 14:51:39 -04007019 /* Free the ELS sgl list */
7020 lpfc_free_active_sgl(phba);
James Smart8a9d2e82012-05-09 21:16:12 -04007021 lpfc_free_els_sgl_list(phba);
James Smartf358dd02017-02-12 13:52:34 -08007022 lpfc_free_nvmet_sgl_list(phba);
James Smartda0436e2009-05-22 14:51:39 -04007023
James Smartda0436e2009-05-22 14:51:39 -04007024 /* Free the completion queue EQ event pool */
7025 lpfc_sli4_cq_event_release_all(phba);
7026 lpfc_sli4_cq_event_pool_destroy(phba);
7027
James Smart6d368e52011-05-24 11:44:12 -04007028 /* Release resource identifiers. */
7029 lpfc_sli4_dealloc_resource_identifiers(phba);
7030
James Smartda0436e2009-05-22 14:51:39 -04007031 /* Free the bsmbx region. */
7032 lpfc_destroy_bootstrap_mbox(phba);
7033
7034 /* Free the SLI Layer memory with SLI4 HBAs */
7035 lpfc_mem_free_all(phba);
7036
7037 /* Free the current connect table */
7038 list_for_each_entry_safe(conn_entry, next_conn_entry,
James Smart4d9ab992009-10-02 15:16:39 -04007039 &phba->fcf_conn_rec_list, list) {
7040 list_del_init(&conn_entry->list);
James Smartda0436e2009-05-22 14:51:39 -04007041 kfree(conn_entry);
James Smart4d9ab992009-10-02 15:16:39 -04007042 }
James Smartda0436e2009-05-22 14:51:39 -04007043
7044 return;
7045}
7046
7047/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007048 * lpfc_init_api_table_setup - Set up init api function jump table
James Smart3772a992009-05-22 14:50:54 -04007049 * @phba: The hba struct for which this call is being executed.
7050 * @dev_grp: The HBA PCI-Device group number.
7051 *
7052 * This routine sets up the device INIT interface API function jump table
7053 * in @phba struct.
7054 *
7055 * Returns: 0 - success, -ENODEV - failure.
7056 **/
7057int
7058lpfc_init_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
7059{
James Smart84d1b002010-02-12 14:42:33 -05007060 phba->lpfc_hba_init_link = lpfc_hba_init_link;
7061 phba->lpfc_hba_down_link = lpfc_hba_down_link;
James Smart7f860592011-03-11 16:05:52 -05007062 phba->lpfc_selective_reset = lpfc_selective_reset;
James Smart3772a992009-05-22 14:50:54 -04007063 switch (dev_grp) {
7064 case LPFC_PCI_DEV_LP:
7065 phba->lpfc_hba_down_post = lpfc_hba_down_post_s3;
7066 phba->lpfc_handle_eratt = lpfc_handle_eratt_s3;
7067 phba->lpfc_stop_port = lpfc_stop_port_s3;
7068 break;
James Smartda0436e2009-05-22 14:51:39 -04007069 case LPFC_PCI_DEV_OC:
7070 phba->lpfc_hba_down_post = lpfc_hba_down_post_s4;
7071 phba->lpfc_handle_eratt = lpfc_handle_eratt_s4;
7072 phba->lpfc_stop_port = lpfc_stop_port_s4;
7073 break;
James Smart3772a992009-05-22 14:50:54 -04007074 default:
7075 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7076 "1431 Invalid HBA PCI-device group: 0x%x\n",
7077 dev_grp);
7078 return -ENODEV;
7079 break;
7080 }
7081 return 0;
7082}
7083
7084/**
James Smart3772a992009-05-22 14:50:54 -04007085 * lpfc_setup_driver_resource_phase2 - Phase2 setup driver internal resources.
7086 * @phba: pointer to lpfc hba data structure.
7087 *
7088 * This routine is invoked to set up the driver internal resources after the
7089 * device specific resource setup to support the HBA device it attached to.
7090 *
7091 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007092 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04007093 * other values - error
7094 **/
7095static int
7096lpfc_setup_driver_resource_phase2(struct lpfc_hba *phba)
7097{
7098 int error;
7099
7100 /* Startup the kernel thread for this host adapter. */
7101 phba->worker_thread = kthread_run(lpfc_do_work, phba,
7102 "lpfc_worker_%d", phba->brd_no);
7103 if (IS_ERR(phba->worker_thread)) {
7104 error = PTR_ERR(phba->worker_thread);
7105 return error;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05007106 }
7107
James Smart3772a992009-05-22 14:50:54 -04007108 return 0;
7109}
7110
7111/**
7112 * lpfc_unset_driver_resource_phase2 - Phase2 unset driver internal resources.
7113 * @phba: pointer to lpfc hba data structure.
7114 *
7115 * This routine is invoked to unset the driver internal resources set up after
7116 * the device specific resource setup for supporting the HBA device it
7117 * attached to.
7118 **/
7119static void
7120lpfc_unset_driver_resource_phase2(struct lpfc_hba *phba)
7121{
Dick Kennedyf485c182017-09-29 17:34:34 -07007122 if (phba->wq) {
7123 flush_workqueue(phba->wq);
7124 destroy_workqueue(phba->wq);
7125 phba->wq = NULL;
7126 }
7127
James Smart3772a992009-05-22 14:50:54 -04007128 /* Stop kernel worker thread */
James Smart0cdb84e2018-04-09 14:24:26 -07007129 if (phba->worker_thread)
7130 kthread_stop(phba->worker_thread);
James Smart3772a992009-05-22 14:50:54 -04007131}
7132
7133/**
7134 * lpfc_free_iocb_list - Free iocb list.
7135 * @phba: pointer to lpfc hba data structure.
7136 *
7137 * This routine is invoked to free the driver's IOCB list and memory.
7138 **/
James Smart6c621a22017-05-15 15:20:45 -07007139void
James Smart3772a992009-05-22 14:50:54 -04007140lpfc_free_iocb_list(struct lpfc_hba *phba)
7141{
7142 struct lpfc_iocbq *iocbq_entry = NULL, *iocbq_next = NULL;
7143
7144 spin_lock_irq(&phba->hbalock);
7145 list_for_each_entry_safe(iocbq_entry, iocbq_next,
7146 &phba->lpfc_iocb_list, list) {
7147 list_del(&iocbq_entry->list);
7148 kfree(iocbq_entry);
7149 phba->total_iocbq_bufs--;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05007150 }
James Smart3772a992009-05-22 14:50:54 -04007151 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007152
James Smart3772a992009-05-22 14:50:54 -04007153 return;
7154}
dea31012005-04-17 16:05:31 -05007155
James Smart3772a992009-05-22 14:50:54 -04007156/**
7157 * lpfc_init_iocb_list - Allocate and initialize iocb list.
7158 * @phba: pointer to lpfc hba data structure.
7159 *
7160 * This routine is invoked to allocate and initizlize the driver's IOCB
7161 * list and set up the IOCB tag array accordingly.
7162 *
7163 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007164 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04007165 * other values - error
7166 **/
James Smart6c621a22017-05-15 15:20:45 -07007167int
James Smart3772a992009-05-22 14:50:54 -04007168lpfc_init_iocb_list(struct lpfc_hba *phba, int iocb_count)
7169{
7170 struct lpfc_iocbq *iocbq_entry = NULL;
7171 uint16_t iotag;
7172 int i;
dea31012005-04-17 16:05:31 -05007173
7174 /* Initialize and populate the iocb list per host. */
7175 INIT_LIST_HEAD(&phba->lpfc_iocb_list);
James Smart3772a992009-05-22 14:50:54 -04007176 for (i = 0; i < iocb_count; i++) {
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07007177 iocbq_entry = kzalloc(sizeof(struct lpfc_iocbq), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05007178 if (iocbq_entry == NULL) {
7179 printk(KERN_ERR "%s: only allocated %d iocbs of "
7180 "expected %d count. Unloading driver.\n",
James Smarta5f73372019-09-21 20:58:50 -07007181 __func__, i, iocb_count);
dea31012005-04-17 16:05:31 -05007182 goto out_free_iocbq;
7183 }
7184
James Bottomley604a3e32005-10-29 10:28:33 -05007185 iotag = lpfc_sli_next_iotag(phba, iocbq_entry);
7186 if (iotag == 0) {
James Smart3772a992009-05-22 14:50:54 -04007187 kfree(iocbq_entry);
James Bottomley604a3e32005-10-29 10:28:33 -05007188 printk(KERN_ERR "%s: failed to allocate IOTAG. "
James Smart3772a992009-05-22 14:50:54 -04007189 "Unloading driver.\n", __func__);
James Bottomley604a3e32005-10-29 10:28:33 -05007190 goto out_free_iocbq;
7191 }
James Smart6d368e52011-05-24 11:44:12 -04007192 iocbq_entry->sli4_lxritag = NO_XRI;
James Smart3772a992009-05-22 14:50:54 -04007193 iocbq_entry->sli4_xritag = NO_XRI;
James Smart2e0fef82007-06-17 19:56:36 -05007194
7195 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007196 list_add(&iocbq_entry->list, &phba->lpfc_iocb_list);
7197 phba->total_iocbq_bufs++;
James Smart2e0fef82007-06-17 19:56:36 -05007198 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007199 }
7200
James Smart3772a992009-05-22 14:50:54 -04007201 return 0;
7202
7203out_free_iocbq:
7204 lpfc_free_iocb_list(phba);
7205
7206 return -ENOMEM;
7207}
7208
7209/**
James Smart8a9d2e82012-05-09 21:16:12 -04007210 * lpfc_free_sgl_list - Free a given sgl list.
James Smartda0436e2009-05-22 14:51:39 -04007211 * @phba: pointer to lpfc hba data structure.
James Smart8a9d2e82012-05-09 21:16:12 -04007212 * @sglq_list: pointer to the head of sgl list.
James Smartda0436e2009-05-22 14:51:39 -04007213 *
James Smart8a9d2e82012-05-09 21:16:12 -04007214 * This routine is invoked to free a give sgl list and memory.
James Smartda0436e2009-05-22 14:51:39 -04007215 **/
James Smart8a9d2e82012-05-09 21:16:12 -04007216void
7217lpfc_free_sgl_list(struct lpfc_hba *phba, struct list_head *sglq_list)
James Smartda0436e2009-05-22 14:51:39 -04007218{
7219 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
James Smart8a9d2e82012-05-09 21:16:12 -04007220
7221 list_for_each_entry_safe(sglq_entry, sglq_next, sglq_list, list) {
7222 list_del(&sglq_entry->list);
7223 lpfc_mbuf_free(phba, sglq_entry->virt, sglq_entry->phys);
7224 kfree(sglq_entry);
7225 }
7226}
7227
7228/**
7229 * lpfc_free_els_sgl_list - Free els sgl list.
7230 * @phba: pointer to lpfc hba data structure.
7231 *
7232 * This routine is invoked to free the driver's els sgl list and memory.
7233 **/
7234static void
7235lpfc_free_els_sgl_list(struct lpfc_hba *phba)
7236{
James Smartda0436e2009-05-22 14:51:39 -04007237 LIST_HEAD(sglq_list);
James Smartda0436e2009-05-22 14:51:39 -04007238
James Smart8a9d2e82012-05-09 21:16:12 -04007239 /* Retrieve all els sgls from driver list */
James Smartda0436e2009-05-22 14:51:39 -04007240 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -08007241 spin_lock(&phba->sli4_hba.sgl_list_lock);
7242 list_splice_init(&phba->sli4_hba.lpfc_els_sgl_list, &sglq_list);
7243 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smartda0436e2009-05-22 14:51:39 -04007244 spin_unlock_irq(&phba->hbalock);
7245
James Smart8a9d2e82012-05-09 21:16:12 -04007246 /* Now free the sgl list */
7247 lpfc_free_sgl_list(phba, &sglq_list);
James Smartda0436e2009-05-22 14:51:39 -04007248}
7249
7250/**
James Smartf358dd02017-02-12 13:52:34 -08007251 * lpfc_free_nvmet_sgl_list - Free nvmet sgl list.
7252 * @phba: pointer to lpfc hba data structure.
7253 *
7254 * This routine is invoked to free the driver's nvmet sgl list and memory.
7255 **/
7256static void
7257lpfc_free_nvmet_sgl_list(struct lpfc_hba *phba)
7258{
7259 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
7260 LIST_HEAD(sglq_list);
7261
7262 /* Retrieve all nvmet sgls from driver list */
7263 spin_lock_irq(&phba->hbalock);
7264 spin_lock(&phba->sli4_hba.sgl_list_lock);
7265 list_splice_init(&phba->sli4_hba.lpfc_nvmet_sgl_list, &sglq_list);
7266 spin_unlock(&phba->sli4_hba.sgl_list_lock);
7267 spin_unlock_irq(&phba->hbalock);
7268
7269 /* Now free the sgl list */
7270 list_for_each_entry_safe(sglq_entry, sglq_next, &sglq_list, list) {
7271 list_del(&sglq_entry->list);
7272 lpfc_nvmet_buf_free(phba, sglq_entry->virt, sglq_entry->phys);
7273 kfree(sglq_entry);
7274 }
Dick Kennedy4b40d022017-08-23 16:55:38 -07007275
7276 /* Update the nvmet_xri_cnt to reflect no current sgls.
7277 * The next initialization cycle sets the count and allocates
7278 * the sgls over again.
7279 */
7280 phba->sli4_hba.nvmet_xri_cnt = 0;
James Smartf358dd02017-02-12 13:52:34 -08007281}
7282
7283/**
James Smartda0436e2009-05-22 14:51:39 -04007284 * lpfc_init_active_sgl_array - Allocate the buf to track active ELS XRIs.
7285 * @phba: pointer to lpfc hba data structure.
7286 *
7287 * This routine is invoked to allocate the driver's active sgl memory.
7288 * This array will hold the sglq_entry's for active IOs.
7289 **/
7290static int
7291lpfc_init_active_sgl_array(struct lpfc_hba *phba)
7292{
7293 int size;
7294 size = sizeof(struct lpfc_sglq *);
7295 size *= phba->sli4_hba.max_cfg_param.max_xri;
7296
7297 phba->sli4_hba.lpfc_sglq_active_list =
7298 kzalloc(size, GFP_KERNEL);
7299 if (!phba->sli4_hba.lpfc_sglq_active_list)
7300 return -ENOMEM;
7301 return 0;
7302}
7303
7304/**
7305 * lpfc_free_active_sgl - Free the buf that tracks active ELS XRIs.
7306 * @phba: pointer to lpfc hba data structure.
7307 *
7308 * This routine is invoked to walk through the array of active sglq entries
7309 * and free all of the resources.
7310 * This is just a place holder for now.
7311 **/
7312static void
7313lpfc_free_active_sgl(struct lpfc_hba *phba)
7314{
7315 kfree(phba->sli4_hba.lpfc_sglq_active_list);
7316}
7317
7318/**
7319 * lpfc_init_sgl_list - Allocate and initialize sgl list.
7320 * @phba: pointer to lpfc hba data structure.
7321 *
7322 * This routine is invoked to allocate and initizlize the driver's sgl
7323 * list and set up the sgl xritag tag array accordingly.
7324 *
James Smartda0436e2009-05-22 14:51:39 -04007325 **/
James Smart8a9d2e82012-05-09 21:16:12 -04007326static void
James Smartda0436e2009-05-22 14:51:39 -04007327lpfc_init_sgl_list(struct lpfc_hba *phba)
7328{
James Smartda0436e2009-05-22 14:51:39 -04007329 /* Initialize and populate the sglq list per host/VF. */
James Smart895427b2017-02-12 13:52:30 -08007330 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_els_sgl_list);
James Smartda0436e2009-05-22 14:51:39 -04007331 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_els_sgl_list);
James Smartf358dd02017-02-12 13:52:34 -08007332 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_nvmet_sgl_list);
James Smart86c67372017-04-21 16:05:04 -07007333 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_nvmet_ctx_list);
James Smartda0436e2009-05-22 14:51:39 -04007334
James Smart8a9d2e82012-05-09 21:16:12 -04007335 /* els xri-sgl book keeping */
7336 phba->sli4_hba.els_xri_cnt = 0;
James Smartda0436e2009-05-22 14:51:39 -04007337
James Smart895427b2017-02-12 13:52:30 -08007338 /* nvme xri-buffer book keeping */
James Smart5e5b5112019-01-28 11:14:22 -08007339 phba->sli4_hba.io_xri_cnt = 0;
James Smartda0436e2009-05-22 14:51:39 -04007340}
7341
7342/**
7343 * lpfc_sli4_init_rpi_hdrs - Post the rpi header memory region to the port
7344 * @phba: pointer to lpfc hba data structure.
7345 *
7346 * This routine is invoked to post rpi header templates to the
James Smart88a2cfb2011-07-22 18:36:33 -04007347 * port for those SLI4 ports that do not support extents. This routine
James Smartda0436e2009-05-22 14:51:39 -04007348 * posts a PAGE_SIZE memory region to the port to hold up to
James Smart88a2cfb2011-07-22 18:36:33 -04007349 * PAGE_SIZE modulo 64 rpi context headers. This is an initialization routine
7350 * and should be called only when interrupts are disabled.
James Smartda0436e2009-05-22 14:51:39 -04007351 *
7352 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007353 * 0 - successful
James Smart88a2cfb2011-07-22 18:36:33 -04007354 * -ERROR - otherwise.
James Smartda0436e2009-05-22 14:51:39 -04007355 **/
7356int
7357lpfc_sli4_init_rpi_hdrs(struct lpfc_hba *phba)
7358{
7359 int rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04007360 struct lpfc_rpi_hdr *rpi_hdr;
7361
7362 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_hdr_list);
James Smartff78d8f2011-12-13 13:21:35 -05007363 if (!phba->sli4_hba.rpi_hdrs_in_use)
James Smart6d368e52011-05-24 11:44:12 -04007364 return rc;
James Smart6d368e52011-05-24 11:44:12 -04007365 if (phba->sli4_hba.extents_in_use)
7366 return -EIO;
James Smartda0436e2009-05-22 14:51:39 -04007367
7368 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
7369 if (!rpi_hdr) {
7370 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7371 "0391 Error during rpi post operation\n");
7372 lpfc_sli4_remove_rpis(phba);
7373 rc = -ENODEV;
7374 }
7375
7376 return rc;
7377}
7378
7379/**
7380 * lpfc_sli4_create_rpi_hdr - Allocate an rpi header memory region
7381 * @phba: pointer to lpfc hba data structure.
7382 *
7383 * This routine is invoked to allocate a single 4KB memory region to
7384 * support rpis and stores them in the phba. This single region
7385 * provides support for up to 64 rpis. The region is used globally
7386 * by the device.
7387 *
7388 * Returns:
7389 * A valid rpi hdr on success.
7390 * A NULL pointer on any failure.
7391 **/
7392struct lpfc_rpi_hdr *
7393lpfc_sli4_create_rpi_hdr(struct lpfc_hba *phba)
7394{
7395 uint16_t rpi_limit, curr_rpi_range;
7396 struct lpfc_dmabuf *dmabuf;
7397 struct lpfc_rpi_hdr *rpi_hdr;
7398
James Smart6d368e52011-05-24 11:44:12 -04007399 /*
7400 * If the SLI4 port supports extents, posting the rpi header isn't
7401 * required. Set the expected maximum count and let the actual value
7402 * get set when extents are fully allocated.
7403 */
7404 if (!phba->sli4_hba.rpi_hdrs_in_use)
7405 return NULL;
7406 if (phba->sli4_hba.extents_in_use)
7407 return NULL;
7408
7409 /* The limit on the logical index is just the max_rpi count. */
James Smart845d9e82017-05-15 15:20:38 -07007410 rpi_limit = phba->sli4_hba.max_cfg_param.max_rpi;
James Smartda0436e2009-05-22 14:51:39 -04007411
7412 spin_lock_irq(&phba->hbalock);
James Smart6d368e52011-05-24 11:44:12 -04007413 /*
7414 * Establish the starting RPI in this header block. The starting
7415 * rpi is normalized to a zero base because the physical rpi is
7416 * port based.
7417 */
James Smart97f2ecf2012-03-01 22:35:23 -05007418 curr_rpi_range = phba->sli4_hba.next_rpi;
James Smartda0436e2009-05-22 14:51:39 -04007419 spin_unlock_irq(&phba->hbalock);
7420
James Smart845d9e82017-05-15 15:20:38 -07007421 /* Reached full RPI range */
7422 if (curr_rpi_range == rpi_limit)
James Smart6d368e52011-05-24 11:44:12 -04007423 return NULL;
James Smart845d9e82017-05-15 15:20:38 -07007424
James Smartda0436e2009-05-22 14:51:39 -04007425 /*
7426 * First allocate the protocol header region for the port. The
7427 * port expects a 4KB DMA-mapped memory region that is 4K aligned.
7428 */
7429 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
7430 if (!dmabuf)
7431 return NULL;
7432
Luis Chamberlain750afb02019-01-04 09:23:09 +01007433 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
7434 LPFC_HDR_TEMPLATE_SIZE,
7435 &dmabuf->phys, GFP_KERNEL);
James Smartda0436e2009-05-22 14:51:39 -04007436 if (!dmabuf->virt) {
7437 rpi_hdr = NULL;
7438 goto err_free_dmabuf;
7439 }
7440
James Smartda0436e2009-05-22 14:51:39 -04007441 if (!IS_ALIGNED(dmabuf->phys, LPFC_HDR_TEMPLATE_SIZE)) {
7442 rpi_hdr = NULL;
7443 goto err_free_coherent;
7444 }
7445
7446 /* Save the rpi header data for cleanup later. */
7447 rpi_hdr = kzalloc(sizeof(struct lpfc_rpi_hdr), GFP_KERNEL);
7448 if (!rpi_hdr)
7449 goto err_free_coherent;
7450
7451 rpi_hdr->dmabuf = dmabuf;
7452 rpi_hdr->len = LPFC_HDR_TEMPLATE_SIZE;
7453 rpi_hdr->page_count = 1;
7454 spin_lock_irq(&phba->hbalock);
James Smart6d368e52011-05-24 11:44:12 -04007455
7456 /* The rpi_hdr stores the logical index only. */
7457 rpi_hdr->start_rpi = curr_rpi_range;
James Smart845d9e82017-05-15 15:20:38 -07007458 rpi_hdr->next_rpi = phba->sli4_hba.next_rpi + LPFC_RPI_HDR_COUNT;
James Smartda0436e2009-05-22 14:51:39 -04007459 list_add_tail(&rpi_hdr->list, &phba->sli4_hba.lpfc_rpi_hdr_list);
7460
James Smartda0436e2009-05-22 14:51:39 -04007461 spin_unlock_irq(&phba->hbalock);
7462 return rpi_hdr;
7463
7464 err_free_coherent:
7465 dma_free_coherent(&phba->pcidev->dev, LPFC_HDR_TEMPLATE_SIZE,
7466 dmabuf->virt, dmabuf->phys);
7467 err_free_dmabuf:
7468 kfree(dmabuf);
7469 return NULL;
7470}
7471
7472/**
7473 * lpfc_sli4_remove_rpi_hdrs - Remove all rpi header memory regions
7474 * @phba: pointer to lpfc hba data structure.
7475 *
7476 * This routine is invoked to remove all memory resources allocated
James Smart6d368e52011-05-24 11:44:12 -04007477 * to support rpis for SLI4 ports not supporting extents. This routine
7478 * presumes the caller has released all rpis consumed by fabric or port
7479 * logins and is prepared to have the header pages removed.
James Smartda0436e2009-05-22 14:51:39 -04007480 **/
7481void
7482lpfc_sli4_remove_rpi_hdrs(struct lpfc_hba *phba)
7483{
7484 struct lpfc_rpi_hdr *rpi_hdr, *next_rpi_hdr;
7485
James Smart6d368e52011-05-24 11:44:12 -04007486 if (!phba->sli4_hba.rpi_hdrs_in_use)
7487 goto exit;
7488
James Smartda0436e2009-05-22 14:51:39 -04007489 list_for_each_entry_safe(rpi_hdr, next_rpi_hdr,
7490 &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
7491 list_del(&rpi_hdr->list);
7492 dma_free_coherent(&phba->pcidev->dev, rpi_hdr->len,
7493 rpi_hdr->dmabuf->virt, rpi_hdr->dmabuf->phys);
7494 kfree(rpi_hdr->dmabuf);
7495 kfree(rpi_hdr);
7496 }
James Smart6d368e52011-05-24 11:44:12 -04007497 exit:
7498 /* There are no rpis available to the port now. */
7499 phba->sli4_hba.next_rpi = 0;
James Smartda0436e2009-05-22 14:51:39 -04007500}
7501
7502/**
James Smart3772a992009-05-22 14:50:54 -04007503 * lpfc_hba_alloc - Allocate driver hba data structure for a device.
7504 * @pdev: pointer to pci device data structure.
7505 *
7506 * This routine is invoked to allocate the driver hba data structure for an
7507 * HBA device. If the allocation is successful, the phba reference to the
7508 * PCI device data structure is set.
7509 *
7510 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007511 * pointer to @phba - successful
James Smart3772a992009-05-22 14:50:54 -04007512 * NULL - error
7513 **/
7514static struct lpfc_hba *
7515lpfc_hba_alloc(struct pci_dev *pdev)
7516{
7517 struct lpfc_hba *phba;
7518
7519 /* Allocate memory for HBA structure */
7520 phba = kzalloc(sizeof(struct lpfc_hba), GFP_KERNEL);
7521 if (!phba) {
Jiri Slabye34ccdf2009-07-13 23:25:54 +02007522 dev_err(&pdev->dev, "failed to allocate hba struct\n");
James Smart3772a992009-05-22 14:50:54 -04007523 return NULL;
7524 }
7525
7526 /* Set reference to PCI device in HBA structure */
7527 phba->pcidev = pdev;
7528
7529 /* Assign an unused board number */
7530 phba->brd_no = lpfc_get_instance();
7531 if (phba->brd_no < 0) {
7532 kfree(phba);
7533 return NULL;
7534 }
James Smart65791f12016-07-06 12:35:56 -07007535 phba->eratt_poll_interval = LPFC_ERATT_POLL_INTERVAL;
James Smart3772a992009-05-22 14:50:54 -04007536
James Smart4fede782010-01-26 23:08:55 -05007537 spin_lock_init(&phba->ct_ev_lock);
James Smartf1c3b0f2009-07-19 10:01:32 -04007538 INIT_LIST_HEAD(&phba->ct_ev_waiters);
7539
James Smart3772a992009-05-22 14:50:54 -04007540 return phba;
7541}
7542
7543/**
7544 * lpfc_hba_free - Free driver hba data structure with a device.
7545 * @phba: pointer to lpfc hba data structure.
7546 *
7547 * This routine is invoked to free the driver hba data structure with an
7548 * HBA device.
7549 **/
7550static void
7551lpfc_hba_free(struct lpfc_hba *phba)
7552{
James Smart5e5b5112019-01-28 11:14:22 -08007553 if (phba->sli_rev == LPFC_SLI_REV4)
7554 kfree(phba->sli4_hba.hdwq);
7555
James Smart3772a992009-05-22 14:50:54 -04007556 /* Release the driver assigned board number */
7557 idr_remove(&lpfc_hba_index, phba->brd_no);
7558
James Smart895427b2017-02-12 13:52:30 -08007559 /* Free memory allocated with sli3 rings */
7560 kfree(phba->sli.sli3_ring);
7561 phba->sli.sli3_ring = NULL;
James Smart2a76a282012-08-03 12:35:54 -04007562
James Smart3772a992009-05-22 14:50:54 -04007563 kfree(phba);
7564 return;
7565}
7566
7567/**
7568 * lpfc_create_shost - Create hba physical port with associated scsi host.
7569 * @phba: pointer to lpfc hba data structure.
7570 *
7571 * This routine is invoked to create HBA physical port and associate a SCSI
7572 * host with it.
7573 *
7574 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007575 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04007576 * other values - error
7577 **/
7578static int
7579lpfc_create_shost(struct lpfc_hba *phba)
7580{
7581 struct lpfc_vport *vport;
7582 struct Scsi_Host *shost;
7583
7584 /* Initialize HBA FC structure */
dea31012005-04-17 16:05:31 -05007585 phba->fc_edtov = FF_DEF_EDTOV;
7586 phba->fc_ratov = FF_DEF_RATOV;
7587 phba->fc_altov = FF_DEF_ALTOV;
7588 phba->fc_arbtov = FF_DEF_ARBTOV;
7589
James Smartd7c47992010-06-08 18:31:54 -04007590 atomic_set(&phba->sdev_cnt, 0);
James Smart3de2a652007-08-02 11:09:59 -04007591 vport = lpfc_create_port(phba, phba->brd_no, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05007592 if (!vport)
James Smart3772a992009-05-22 14:50:54 -04007593 return -ENODEV;
James Smart2e0fef82007-06-17 19:56:36 -05007594
7595 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05007596 phba->pport = vport;
James Smart2ea259e2017-02-12 13:52:27 -08007597
James Smartf358dd02017-02-12 13:52:34 -08007598 if (phba->nvmet_support) {
7599 /* Only 1 vport (pport) will support NVME target */
James Smartea85a202019-10-18 14:18:25 -07007600 phba->targetport = NULL;
7601 phba->cfg_enable_fc4_type = LPFC_ENABLE_NVME;
7602 lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_NVME_DISC,
7603 "6076 NVME Target Found\n");
James Smartf358dd02017-02-12 13:52:34 -08007604 }
7605
James Smart858c9f62007-06-17 19:56:39 -05007606 lpfc_debugfs_initialize(vport);
James Smart3772a992009-05-22 14:50:54 -04007607 /* Put reference to SCSI host to driver's device private data */
7608 pci_set_drvdata(phba->pcidev, shost);
James Smart2e0fef82007-06-17 19:56:36 -05007609
James Smart4258e982015-12-16 18:11:58 -05007610 /*
7611 * At this point we are fully registered with PSA. In addition,
7612 * any initial discovery should be completed.
7613 */
7614 vport->load_flag |= FC_ALLOW_FDMI;
James Smart8663cbb2016-03-31 14:12:33 -07007615 if (phba->cfg_enable_SmartSAN ||
7616 (phba->cfg_fdmi_on == LPFC_FDMI_SUPPORT)) {
James Smart4258e982015-12-16 18:11:58 -05007617
7618 /* Setup appropriate attribute masks */
7619 vport->fdmi_hba_mask = LPFC_FDMI2_HBA_ATTR;
James Smart8663cbb2016-03-31 14:12:33 -07007620 if (phba->cfg_enable_SmartSAN)
James Smart4258e982015-12-16 18:11:58 -05007621 vport->fdmi_port_mask = LPFC_FDMI2_SMART_ATTR;
7622 else
7623 vport->fdmi_port_mask = LPFC_FDMI2_PORT_ATTR;
7624 }
James Smart3772a992009-05-22 14:50:54 -04007625 return 0;
7626}
dea31012005-04-17 16:05:31 -05007627
James Smart3772a992009-05-22 14:50:54 -04007628/**
7629 * lpfc_destroy_shost - Destroy hba physical port with associated scsi host.
7630 * @phba: pointer to lpfc hba data structure.
7631 *
7632 * This routine is invoked to destroy HBA physical port and the associated
7633 * SCSI host.
7634 **/
7635static void
7636lpfc_destroy_shost(struct lpfc_hba *phba)
7637{
7638 struct lpfc_vport *vport = phba->pport;
James Smart93996272008-08-24 21:50:30 -04007639
James Smart3772a992009-05-22 14:50:54 -04007640 /* Destroy physical port that associated with the SCSI host */
7641 destroy_port(vport);
7642
7643 return;
7644}
7645
7646/**
7647 * lpfc_setup_bg - Setup Block guard structures and debug areas.
7648 * @phba: pointer to lpfc hba data structure.
7649 * @shost: the shost to be used to detect Block guard settings.
7650 *
7651 * This routine sets up the local Block guard protocol settings for @shost.
7652 * This routine also allocates memory for debugging bg buffers.
7653 **/
7654static void
7655lpfc_setup_bg(struct lpfc_hba *phba, struct Scsi_Host *shost)
7656{
James Smartbbeb79b2012-06-12 13:54:27 -04007657 uint32_t old_mask;
7658 uint32_t old_guard;
7659
James Smartb3b98b72016-10-13 15:06:06 -07007660 if (phba->cfg_prot_mask && phba->cfg_prot_guard) {
James Smart3772a992009-05-22 14:50:54 -04007661 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7662 "1478 Registering BlockGuard with the "
7663 "SCSI layer\n");
James Smartbbeb79b2012-06-12 13:54:27 -04007664
James Smartb3b98b72016-10-13 15:06:06 -07007665 old_mask = phba->cfg_prot_mask;
7666 old_guard = phba->cfg_prot_guard;
James Smartbbeb79b2012-06-12 13:54:27 -04007667
7668 /* Only allow supported values */
James Smartb3b98b72016-10-13 15:06:06 -07007669 phba->cfg_prot_mask &= (SHOST_DIF_TYPE1_PROTECTION |
James Smartbbeb79b2012-06-12 13:54:27 -04007670 SHOST_DIX_TYPE0_PROTECTION |
7671 SHOST_DIX_TYPE1_PROTECTION);
James Smartb3b98b72016-10-13 15:06:06 -07007672 phba->cfg_prot_guard &= (SHOST_DIX_GUARD_IP |
7673 SHOST_DIX_GUARD_CRC);
James Smartbbeb79b2012-06-12 13:54:27 -04007674
7675 /* DIF Type 1 protection for profiles AST1/C1 is end to end */
James Smartb3b98b72016-10-13 15:06:06 -07007676 if (phba->cfg_prot_mask == SHOST_DIX_TYPE1_PROTECTION)
7677 phba->cfg_prot_mask |= SHOST_DIF_TYPE1_PROTECTION;
James Smartbbeb79b2012-06-12 13:54:27 -04007678
James Smartb3b98b72016-10-13 15:06:06 -07007679 if (phba->cfg_prot_mask && phba->cfg_prot_guard) {
7680 if ((old_mask != phba->cfg_prot_mask) ||
7681 (old_guard != phba->cfg_prot_guard))
James Smartbbeb79b2012-06-12 13:54:27 -04007682 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7683 "1475 Registering BlockGuard with the "
7684 "SCSI layer: mask %d guard %d\n",
James Smartb3b98b72016-10-13 15:06:06 -07007685 phba->cfg_prot_mask,
7686 phba->cfg_prot_guard);
James Smartbbeb79b2012-06-12 13:54:27 -04007687
James Smartb3b98b72016-10-13 15:06:06 -07007688 scsi_host_set_prot(shost, phba->cfg_prot_mask);
7689 scsi_host_set_guard(shost, phba->cfg_prot_guard);
James Smartbbeb79b2012-06-12 13:54:27 -04007690 } else
7691 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7692 "1479 Not Registering BlockGuard with the SCSI "
7693 "layer, Bad protection parameters: %d %d\n",
7694 old_mask, old_guard);
James Smart98c9ea52007-10-27 13:37:33 -04007695 }
James Smart3772a992009-05-22 14:50:54 -04007696}
7697
7698/**
7699 * lpfc_post_init_setup - Perform necessary device post initialization setup.
7700 * @phba: pointer to lpfc hba data structure.
7701 *
7702 * This routine is invoked to perform all the necessary post initialization
7703 * setup for the device.
7704 **/
7705static void
7706lpfc_post_init_setup(struct lpfc_hba *phba)
7707{
7708 struct Scsi_Host *shost;
7709 struct lpfc_adapter_event_header adapter_event;
7710
7711 /* Get the default values for Model Name and Description */
7712 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
7713
7714 /*
7715 * hba setup may have changed the hba_queue_depth so we need to
7716 * adjust the value of can_queue.
7717 */
7718 shost = pci_get_drvdata(phba->pcidev);
7719 shost->can_queue = phba->cfg_hba_queue_depth - 10;
James Smart858c9f62007-06-17 19:56:39 -05007720
7721 lpfc_host_attrib_init(shost);
7722
James Smart2e0fef82007-06-17 19:56:36 -05007723 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
7724 spin_lock_irq(shost->host_lock);
7725 lpfc_poll_start_timer(phba);
7726 spin_unlock_irq(shost->host_lock);
7727 }
James Smart8f6d98d2006-08-01 07:34:00 -04007728
James Smart93996272008-08-24 21:50:30 -04007729 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7730 "0428 Perform SCSI scan\n");
James Smartea2151b2008-09-07 11:52:10 -04007731 /* Send board arrival event to upper layer */
7732 adapter_event.event_type = FC_REG_ADAPTER_EVENT;
7733 adapter_event.subcategory = LPFC_EVENT_ARRIVAL;
7734 fc_host_post_vendor_event(shost, fc_get_event_number(),
James Smart3772a992009-05-22 14:50:54 -04007735 sizeof(adapter_event),
7736 (char *) &adapter_event,
7737 LPFC_NL_VENDOR_ID);
7738 return;
7739}
7740
7741/**
7742 * lpfc_sli_pci_mem_setup - Setup SLI3 HBA PCI memory space.
7743 * @phba: pointer to lpfc hba data structure.
7744 *
7745 * This routine is invoked to set up the PCI device memory space for device
7746 * with SLI-3 interface spec.
7747 *
7748 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007749 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04007750 * other values - error
7751 **/
7752static int
7753lpfc_sli_pci_mem_setup(struct lpfc_hba *phba)
7754{
Christoph Hellwigf30e1bf2018-10-18 15:10:21 +02007755 struct pci_dev *pdev = phba->pcidev;
James Smart3772a992009-05-22 14:50:54 -04007756 unsigned long bar0map_len, bar2map_len;
7757 int i, hbq_count;
7758 void *ptr;
Hannes Reinecke56de8352019-02-18 08:34:19 +01007759 int error;
James Smart3772a992009-05-22 14:50:54 -04007760
Christoph Hellwigf30e1bf2018-10-18 15:10:21 +02007761 if (!pdev)
Hannes Reinecke56de8352019-02-18 08:34:19 +01007762 return -ENODEV;
James Smart3772a992009-05-22 14:50:54 -04007763
7764 /* Set the device DMA mask size */
Hannes Reinecke56de8352019-02-18 08:34:19 +01007765 error = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
7766 if (error)
7767 error = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
7768 if (error)
Christoph Hellwigf30e1bf2018-10-18 15:10:21 +02007769 return error;
Hannes Reinecke56de8352019-02-18 08:34:19 +01007770 error = -ENODEV;
James Smart3772a992009-05-22 14:50:54 -04007771
7772 /* Get the bus address of Bar0 and Bar2 and the number of bytes
7773 * required by each mapping.
7774 */
7775 phba->pci_bar0_map = pci_resource_start(pdev, 0);
7776 bar0map_len = pci_resource_len(pdev, 0);
7777
7778 phba->pci_bar2_map = pci_resource_start(pdev, 2);
7779 bar2map_len = pci_resource_len(pdev, 2);
7780
7781 /* Map HBA SLIM to a kernel virtual address. */
7782 phba->slim_memmap_p = ioremap(phba->pci_bar0_map, bar0map_len);
7783 if (!phba->slim_memmap_p) {
7784 dev_printk(KERN_ERR, &pdev->dev,
7785 "ioremap failed for SLIM memory.\n");
7786 goto out;
7787 }
7788
7789 /* Map HBA Control Registers to a kernel virtual address. */
7790 phba->ctrl_regs_memmap_p = ioremap(phba->pci_bar2_map, bar2map_len);
7791 if (!phba->ctrl_regs_memmap_p) {
7792 dev_printk(KERN_ERR, &pdev->dev,
7793 "ioremap failed for HBA control registers.\n");
7794 goto out_iounmap_slim;
7795 }
7796
7797 /* Allocate memory for SLI-2 structures */
Luis Chamberlain750afb02019-01-04 09:23:09 +01007798 phba->slim2p.virt = dma_alloc_coherent(&pdev->dev, SLI2_SLIM_SIZE,
7799 &phba->slim2p.phys, GFP_KERNEL);
James Smart3772a992009-05-22 14:50:54 -04007800 if (!phba->slim2p.virt)
7801 goto out_iounmap;
7802
James Smart3772a992009-05-22 14:50:54 -04007803 phba->mbox = phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, mbx);
James Smart7a470272010-03-15 11:25:20 -04007804 phba->mbox_ext = (phba->slim2p.virt +
7805 offsetof(struct lpfc_sli2_slim, mbx_ext_words));
James Smart3772a992009-05-22 14:50:54 -04007806 phba->pcb = (phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, pcb));
7807 phba->IOCBs = (phba->slim2p.virt +
7808 offsetof(struct lpfc_sli2_slim, IOCBs));
7809
7810 phba->hbqslimp.virt = dma_alloc_coherent(&pdev->dev,
7811 lpfc_sli_hbq_size(),
7812 &phba->hbqslimp.phys,
7813 GFP_KERNEL);
7814 if (!phba->hbqslimp.virt)
7815 goto out_free_slim;
7816
7817 hbq_count = lpfc_sli_hbq_count();
7818 ptr = phba->hbqslimp.virt;
7819 for (i = 0; i < hbq_count; ++i) {
7820 phba->hbqs[i].hbq_virt = ptr;
7821 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
7822 ptr += (lpfc_hbq_defs[i]->entry_count *
7823 sizeof(struct lpfc_hbq_entry));
7824 }
7825 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_els_hbq_alloc;
7826 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_els_hbq_free;
7827
7828 memset(phba->hbqslimp.virt, 0, lpfc_sli_hbq_size());
7829
James Smart3772a992009-05-22 14:50:54 -04007830 phba->MBslimaddr = phba->slim_memmap_p;
7831 phba->HAregaddr = phba->ctrl_regs_memmap_p + HA_REG_OFFSET;
7832 phba->CAregaddr = phba->ctrl_regs_memmap_p + CA_REG_OFFSET;
7833 phba->HSregaddr = phba->ctrl_regs_memmap_p + HS_REG_OFFSET;
7834 phba->HCregaddr = phba->ctrl_regs_memmap_p + HC_REG_OFFSET;
James Smartea2151b2008-09-07 11:52:10 -04007835
dea31012005-04-17 16:05:31 -05007836 return 0;
7837
dea31012005-04-17 16:05:31 -05007838out_free_slim:
James Smart34b02dc2008-08-24 21:49:55 -04007839 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
7840 phba->slim2p.virt, phba->slim2p.phys);
dea31012005-04-17 16:05:31 -05007841out_iounmap:
7842 iounmap(phba->ctrl_regs_memmap_p);
Jamie Wellnitz901a9202006-02-28 19:25:19 -05007843out_iounmap_slim:
dea31012005-04-17 16:05:31 -05007844 iounmap(phba->slim_memmap_p);
dea31012005-04-17 16:05:31 -05007845out:
7846 return error;
7847}
7848
James Smarte59058c2008-08-24 21:49:00 -04007849/**
James Smart3772a992009-05-22 14:50:54 -04007850 * lpfc_sli_pci_mem_unset - Unset SLI3 HBA PCI memory space.
7851 * @phba: pointer to lpfc hba data structure.
7852 *
7853 * This routine is invoked to unset the PCI device memory space for device
7854 * with SLI-3 interface spec.
7855 **/
7856static void
7857lpfc_sli_pci_mem_unset(struct lpfc_hba *phba)
7858{
7859 struct pci_dev *pdev;
7860
7861 /* Obtain PCI device reference */
7862 if (!phba->pcidev)
7863 return;
7864 else
7865 pdev = phba->pcidev;
7866
7867 /* Free coherent DMA memory allocated */
7868 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
7869 phba->hbqslimp.virt, phba->hbqslimp.phys);
7870 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
7871 phba->slim2p.virt, phba->slim2p.phys);
7872
7873 /* I/O memory unmap */
7874 iounmap(phba->ctrl_regs_memmap_p);
7875 iounmap(phba->slim_memmap_p);
7876
7877 return;
7878}
7879
7880/**
James Smartda0436e2009-05-22 14:51:39 -04007881 * lpfc_sli4_post_status_check - Wait for SLI4 POST done and check status
7882 * @phba: pointer to lpfc hba data structure.
7883 *
7884 * This routine is invoked to wait for SLI4 device Power On Self Test (POST)
7885 * done and check status.
7886 *
7887 * Return 0 if successful, otherwise -ENODEV.
7888 **/
7889int
7890lpfc_sli4_post_status_check(struct lpfc_hba *phba)
7891{
James Smart2fcee4b2010-12-15 17:57:46 -05007892 struct lpfc_register portsmphr_reg, uerrlo_reg, uerrhi_reg;
7893 struct lpfc_register reg_data;
7894 int i, port_error = 0;
7895 uint32_t if_type;
James Smartda0436e2009-05-22 14:51:39 -04007896
James Smart9940b972011-03-11 16:06:12 -05007897 memset(&portsmphr_reg, 0, sizeof(portsmphr_reg));
7898 memset(&reg_data, 0, sizeof(reg_data));
James Smart2fcee4b2010-12-15 17:57:46 -05007899 if (!phba->sli4_hba.PSMPHRregaddr)
James Smartda0436e2009-05-22 14:51:39 -04007900 return -ENODEV;
7901
James Smartda0436e2009-05-22 14:51:39 -04007902 /* Wait up to 30 seconds for the SLI Port POST done and ready */
7903 for (i = 0; i < 3000; i++) {
James Smart9940b972011-03-11 16:06:12 -05007904 if (lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
7905 &portsmphr_reg.word0) ||
7906 (bf_get(lpfc_port_smphr_perr, &portsmphr_reg))) {
James Smart2fcee4b2010-12-15 17:57:46 -05007907 /* Port has a fatal POST error, break out */
James Smartda0436e2009-05-22 14:51:39 -04007908 port_error = -ENODEV;
7909 break;
7910 }
James Smart2fcee4b2010-12-15 17:57:46 -05007911 if (LPFC_POST_STAGE_PORT_READY ==
7912 bf_get(lpfc_port_smphr_port_status, &portsmphr_reg))
James Smartda0436e2009-05-22 14:51:39 -04007913 break;
James Smartda0436e2009-05-22 14:51:39 -04007914 msleep(10);
7915 }
7916
James Smart2fcee4b2010-12-15 17:57:46 -05007917 /*
7918 * If there was a port error during POST, then don't proceed with
7919 * other register reads as the data may not be valid. Just exit.
7920 */
7921 if (port_error) {
James Smartda0436e2009-05-22 14:51:39 -04007922 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -05007923 "1408 Port Failed POST - portsmphr=0x%x, "
7924 "perr=x%x, sfi=x%x, nip=x%x, ipc=x%x, scr1=x%x, "
7925 "scr2=x%x, hscratch=x%x, pstatus=x%x\n",
7926 portsmphr_reg.word0,
7927 bf_get(lpfc_port_smphr_perr, &portsmphr_reg),
7928 bf_get(lpfc_port_smphr_sfi, &portsmphr_reg),
7929 bf_get(lpfc_port_smphr_nip, &portsmphr_reg),
7930 bf_get(lpfc_port_smphr_ipc, &portsmphr_reg),
7931 bf_get(lpfc_port_smphr_scr1, &portsmphr_reg),
7932 bf_get(lpfc_port_smphr_scr2, &portsmphr_reg),
7933 bf_get(lpfc_port_smphr_host_scratch, &portsmphr_reg),
7934 bf_get(lpfc_port_smphr_port_status, &portsmphr_reg));
7935 } else {
James Smart28baac72010-02-12 14:42:03 -05007936 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -05007937 "2534 Device Info: SLIFamily=0x%x, "
7938 "SLIRev=0x%x, IFType=0x%x, SLIHint_1=0x%x, "
7939 "SLIHint_2=0x%x, FT=0x%x\n",
James Smart28baac72010-02-12 14:42:03 -05007940 bf_get(lpfc_sli_intf_sli_family,
7941 &phba->sli4_hba.sli_intf),
7942 bf_get(lpfc_sli_intf_slirev,
7943 &phba->sli4_hba.sli_intf),
James Smart085c6472010-11-20 23:11:37 -05007944 bf_get(lpfc_sli_intf_if_type,
James Smart28baac72010-02-12 14:42:03 -05007945 &phba->sli4_hba.sli_intf),
James Smart085c6472010-11-20 23:11:37 -05007946 bf_get(lpfc_sli_intf_sli_hint1,
7947 &phba->sli4_hba.sli_intf),
7948 bf_get(lpfc_sli_intf_sli_hint2,
7949 &phba->sli4_hba.sli_intf),
7950 bf_get(lpfc_sli_intf_func_type,
James Smart28baac72010-02-12 14:42:03 -05007951 &phba->sli4_hba.sli_intf));
James Smart2fcee4b2010-12-15 17:57:46 -05007952 /*
7953 * Check for other Port errors during the initialization
7954 * process. Fail the load if the port did not come up
7955 * correctly.
7956 */
7957 if_type = bf_get(lpfc_sli_intf_if_type,
7958 &phba->sli4_hba.sli_intf);
7959 switch (if_type) {
7960 case LPFC_SLI_INTF_IF_TYPE_0:
7961 phba->sli4_hba.ue_mask_lo =
7962 readl(phba->sli4_hba.u.if_type0.UEMASKLOregaddr);
7963 phba->sli4_hba.ue_mask_hi =
7964 readl(phba->sli4_hba.u.if_type0.UEMASKHIregaddr);
7965 uerrlo_reg.word0 =
7966 readl(phba->sli4_hba.u.if_type0.UERRLOregaddr);
7967 uerrhi_reg.word0 =
7968 readl(phba->sli4_hba.u.if_type0.UERRHIregaddr);
7969 if ((~phba->sli4_hba.ue_mask_lo & uerrlo_reg.word0) ||
7970 (~phba->sli4_hba.ue_mask_hi & uerrhi_reg.word0)) {
7971 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7972 "1422 Unrecoverable Error "
7973 "Detected during POST "
7974 "uerr_lo_reg=0x%x, "
7975 "uerr_hi_reg=0x%x, "
7976 "ue_mask_lo_reg=0x%x, "
7977 "ue_mask_hi_reg=0x%x\n",
7978 uerrlo_reg.word0,
7979 uerrhi_reg.word0,
7980 phba->sli4_hba.ue_mask_lo,
7981 phba->sli4_hba.ue_mask_hi);
7982 port_error = -ENODEV;
7983 }
7984 break;
7985 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart27d6ac02018-02-22 08:18:42 -08007986 case LPFC_SLI_INTF_IF_TYPE_6:
James Smart2fcee4b2010-12-15 17:57:46 -05007987 /* Final checks. The port status should be clean. */
James Smart9940b972011-03-11 16:06:12 -05007988 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
7989 &reg_data.word0) ||
James Smart05580562011-05-24 11:40:48 -04007990 (bf_get(lpfc_sliport_status_err, &reg_data) &&
7991 !bf_get(lpfc_sliport_status_rn, &reg_data))) {
James Smart2fcee4b2010-12-15 17:57:46 -05007992 phba->work_status[0] =
7993 readl(phba->sli4_hba.u.if_type2.
7994 ERR1regaddr);
7995 phba->work_status[1] =
7996 readl(phba->sli4_hba.u.if_type2.
7997 ERR2regaddr);
7998 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart8fcb8ac2012-03-01 22:35:58 -05007999 "2888 Unrecoverable port error "
8000 "following POST: port status reg "
8001 "0x%x, port_smphr reg 0x%x, "
James Smart2fcee4b2010-12-15 17:57:46 -05008002 "error 1=0x%x, error 2=0x%x\n",
8003 reg_data.word0,
8004 portsmphr_reg.word0,
8005 phba->work_status[0],
8006 phba->work_status[1]);
8007 port_error = -ENODEV;
8008 }
8009 break;
8010 case LPFC_SLI_INTF_IF_TYPE_1:
8011 default:
8012 break;
8013 }
James Smart28baac72010-02-12 14:42:03 -05008014 }
James Smartda0436e2009-05-22 14:51:39 -04008015 return port_error;
8016}
8017
8018/**
8019 * lpfc_sli4_bar0_register_memmap - Set up SLI4 BAR0 register memory map.
8020 * @phba: pointer to lpfc hba data structure.
James Smart2fcee4b2010-12-15 17:57:46 -05008021 * @if_type: The SLI4 interface type getting configured.
James Smartda0436e2009-05-22 14:51:39 -04008022 *
8023 * This routine is invoked to set up SLI4 BAR0 PCI config space register
8024 * memory map.
8025 **/
8026static void
James Smart2fcee4b2010-12-15 17:57:46 -05008027lpfc_sli4_bar0_register_memmap(struct lpfc_hba *phba, uint32_t if_type)
James Smartda0436e2009-05-22 14:51:39 -04008028{
James Smart2fcee4b2010-12-15 17:57:46 -05008029 switch (if_type) {
8030 case LPFC_SLI_INTF_IF_TYPE_0:
8031 phba->sli4_hba.u.if_type0.UERRLOregaddr =
8032 phba->sli4_hba.conf_regs_memmap_p + LPFC_UERR_STATUS_LO;
8033 phba->sli4_hba.u.if_type0.UERRHIregaddr =
8034 phba->sli4_hba.conf_regs_memmap_p + LPFC_UERR_STATUS_HI;
8035 phba->sli4_hba.u.if_type0.UEMASKLOregaddr =
8036 phba->sli4_hba.conf_regs_memmap_p + LPFC_UE_MASK_LO;
8037 phba->sli4_hba.u.if_type0.UEMASKHIregaddr =
8038 phba->sli4_hba.conf_regs_memmap_p + LPFC_UE_MASK_HI;
8039 phba->sli4_hba.SLIINTFregaddr =
8040 phba->sli4_hba.conf_regs_memmap_p + LPFC_SLI_INTF;
8041 break;
8042 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart0cf07f842017-06-01 21:07:10 -07008043 phba->sli4_hba.u.if_type2.EQDregaddr =
8044 phba->sli4_hba.conf_regs_memmap_p +
8045 LPFC_CTL_PORT_EQ_DELAY_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05008046 phba->sli4_hba.u.if_type2.ERR1regaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04008047 phba->sli4_hba.conf_regs_memmap_p +
8048 LPFC_CTL_PORT_ER1_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05008049 phba->sli4_hba.u.if_type2.ERR2regaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04008050 phba->sli4_hba.conf_regs_memmap_p +
8051 LPFC_CTL_PORT_ER2_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05008052 phba->sli4_hba.u.if_type2.CTRLregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04008053 phba->sli4_hba.conf_regs_memmap_p +
8054 LPFC_CTL_PORT_CTL_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05008055 phba->sli4_hba.u.if_type2.STATUSregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04008056 phba->sli4_hba.conf_regs_memmap_p +
8057 LPFC_CTL_PORT_STA_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05008058 phba->sli4_hba.SLIINTFregaddr =
8059 phba->sli4_hba.conf_regs_memmap_p + LPFC_SLI_INTF;
8060 phba->sli4_hba.PSMPHRregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04008061 phba->sli4_hba.conf_regs_memmap_p +
8062 LPFC_CTL_PORT_SEM_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05008063 phba->sli4_hba.RQDBregaddr =
James Smart962bc512013-01-03 15:44:00 -05008064 phba->sli4_hba.conf_regs_memmap_p +
8065 LPFC_ULP0_RQ_DOORBELL;
James Smart2fcee4b2010-12-15 17:57:46 -05008066 phba->sli4_hba.WQDBregaddr =
James Smart962bc512013-01-03 15:44:00 -05008067 phba->sli4_hba.conf_regs_memmap_p +
8068 LPFC_ULP0_WQ_DOORBELL;
James Smart9dd35422018-02-22 08:18:41 -08008069 phba->sli4_hba.CQDBregaddr =
James Smart2fcee4b2010-12-15 17:57:46 -05008070 phba->sli4_hba.conf_regs_memmap_p + LPFC_EQCQ_DOORBELL;
James Smart9dd35422018-02-22 08:18:41 -08008071 phba->sli4_hba.EQDBregaddr = phba->sli4_hba.CQDBregaddr;
James Smart2fcee4b2010-12-15 17:57:46 -05008072 phba->sli4_hba.MQDBregaddr =
8073 phba->sli4_hba.conf_regs_memmap_p + LPFC_MQ_DOORBELL;
8074 phba->sli4_hba.BMBXregaddr =
8075 phba->sli4_hba.conf_regs_memmap_p + LPFC_BMBX;
8076 break;
James Smart27d6ac02018-02-22 08:18:42 -08008077 case LPFC_SLI_INTF_IF_TYPE_6:
8078 phba->sli4_hba.u.if_type2.EQDregaddr =
8079 phba->sli4_hba.conf_regs_memmap_p +
8080 LPFC_CTL_PORT_EQ_DELAY_OFFSET;
8081 phba->sli4_hba.u.if_type2.ERR1regaddr =
8082 phba->sli4_hba.conf_regs_memmap_p +
8083 LPFC_CTL_PORT_ER1_OFFSET;
8084 phba->sli4_hba.u.if_type2.ERR2regaddr =
8085 phba->sli4_hba.conf_regs_memmap_p +
8086 LPFC_CTL_PORT_ER2_OFFSET;
8087 phba->sli4_hba.u.if_type2.CTRLregaddr =
8088 phba->sli4_hba.conf_regs_memmap_p +
8089 LPFC_CTL_PORT_CTL_OFFSET;
8090 phba->sli4_hba.u.if_type2.STATUSregaddr =
8091 phba->sli4_hba.conf_regs_memmap_p +
8092 LPFC_CTL_PORT_STA_OFFSET;
8093 phba->sli4_hba.PSMPHRregaddr =
8094 phba->sli4_hba.conf_regs_memmap_p +
8095 LPFC_CTL_PORT_SEM_OFFSET;
8096 phba->sli4_hba.BMBXregaddr =
8097 phba->sli4_hba.conf_regs_memmap_p + LPFC_BMBX;
8098 break;
James Smart2fcee4b2010-12-15 17:57:46 -05008099 case LPFC_SLI_INTF_IF_TYPE_1:
8100 default:
8101 dev_printk(KERN_ERR, &phba->pcidev->dev,
8102 "FATAL - unsupported SLI4 interface type - %d\n",
8103 if_type);
8104 break;
8105 }
James Smartda0436e2009-05-22 14:51:39 -04008106}
8107
8108/**
8109 * lpfc_sli4_bar1_register_memmap - Set up SLI4 BAR1 register memory map.
8110 * @phba: pointer to lpfc hba data structure.
8111 *
James Smart27d6ac02018-02-22 08:18:42 -08008112 * This routine is invoked to set up SLI4 BAR1 register memory map.
James Smartda0436e2009-05-22 14:51:39 -04008113 **/
8114static void
James Smart27d6ac02018-02-22 08:18:42 -08008115lpfc_sli4_bar1_register_memmap(struct lpfc_hba *phba, uint32_t if_type)
James Smartda0436e2009-05-22 14:51:39 -04008116{
James Smart27d6ac02018-02-22 08:18:42 -08008117 switch (if_type) {
8118 case LPFC_SLI_INTF_IF_TYPE_0:
8119 phba->sli4_hba.PSMPHRregaddr =
8120 phba->sli4_hba.ctrl_regs_memmap_p +
8121 LPFC_SLIPORT_IF0_SMPHR;
8122 phba->sli4_hba.ISRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
8123 LPFC_HST_ISR0;
8124 phba->sli4_hba.IMRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
8125 LPFC_HST_IMR0;
8126 phba->sli4_hba.ISCRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
8127 LPFC_HST_ISCR0;
8128 break;
8129 case LPFC_SLI_INTF_IF_TYPE_6:
8130 phba->sli4_hba.RQDBregaddr = phba->sli4_hba.drbl_regs_memmap_p +
8131 LPFC_IF6_RQ_DOORBELL;
8132 phba->sli4_hba.WQDBregaddr = phba->sli4_hba.drbl_regs_memmap_p +
8133 LPFC_IF6_WQ_DOORBELL;
8134 phba->sli4_hba.CQDBregaddr = phba->sli4_hba.drbl_regs_memmap_p +
8135 LPFC_IF6_CQ_DOORBELL;
8136 phba->sli4_hba.EQDBregaddr = phba->sli4_hba.drbl_regs_memmap_p +
8137 LPFC_IF6_EQ_DOORBELL;
8138 phba->sli4_hba.MQDBregaddr = phba->sli4_hba.drbl_regs_memmap_p +
8139 LPFC_IF6_MQ_DOORBELL;
8140 break;
8141 case LPFC_SLI_INTF_IF_TYPE_2:
8142 case LPFC_SLI_INTF_IF_TYPE_1:
8143 default:
8144 dev_err(&phba->pcidev->dev,
8145 "FATAL - unsupported SLI4 interface type - %d\n",
8146 if_type);
8147 break;
8148 }
James Smartda0436e2009-05-22 14:51:39 -04008149}
8150
8151/**
8152 * lpfc_sli4_bar2_register_memmap - Set up SLI4 BAR2 register memory map.
8153 * @phba: pointer to lpfc hba data structure.
8154 * @vf: virtual function number
8155 *
8156 * This routine is invoked to set up SLI4 BAR2 doorbell register memory map
8157 * based on the given viftual function number, @vf.
8158 *
8159 * Return 0 if successful, otherwise -ENODEV.
8160 **/
8161static int
8162lpfc_sli4_bar2_register_memmap(struct lpfc_hba *phba, uint32_t vf)
8163{
8164 if (vf > LPFC_VIR_FUNC_MAX)
8165 return -ENODEV;
8166
8167 phba->sli4_hba.RQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
James Smart962bc512013-01-03 15:44:00 -05008168 vf * LPFC_VFR_PAGE_SIZE +
8169 LPFC_ULP0_RQ_DOORBELL);
James Smartda0436e2009-05-22 14:51:39 -04008170 phba->sli4_hba.WQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
James Smart962bc512013-01-03 15:44:00 -05008171 vf * LPFC_VFR_PAGE_SIZE +
8172 LPFC_ULP0_WQ_DOORBELL);
James Smart9dd35422018-02-22 08:18:41 -08008173 phba->sli4_hba.CQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
8174 vf * LPFC_VFR_PAGE_SIZE +
8175 LPFC_EQCQ_DOORBELL);
8176 phba->sli4_hba.EQDBregaddr = phba->sli4_hba.CQDBregaddr;
James Smartda0436e2009-05-22 14:51:39 -04008177 phba->sli4_hba.MQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
8178 vf * LPFC_VFR_PAGE_SIZE + LPFC_MQ_DOORBELL);
8179 phba->sli4_hba.BMBXregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
8180 vf * LPFC_VFR_PAGE_SIZE + LPFC_BMBX);
8181 return 0;
8182}
8183
8184/**
8185 * lpfc_create_bootstrap_mbox - Create the bootstrap mailbox
8186 * @phba: pointer to lpfc hba data structure.
8187 *
8188 * This routine is invoked to create the bootstrap mailbox
8189 * region consistent with the SLI-4 interface spec. This
8190 * routine allocates all memory necessary to communicate
8191 * mailbox commands to the port and sets up all alignment
8192 * needs. No locks are expected to be held when calling
8193 * this routine.
8194 *
8195 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008196 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04008197 * -ENOMEM - could not allocated memory.
James Smartda0436e2009-05-22 14:51:39 -04008198 **/
8199static int
8200lpfc_create_bootstrap_mbox(struct lpfc_hba *phba)
8201{
8202 uint32_t bmbx_size;
8203 struct lpfc_dmabuf *dmabuf;
8204 struct dma_address *dma_address;
8205 uint32_t pa_addr;
8206 uint64_t phys_addr;
8207
8208 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
8209 if (!dmabuf)
8210 return -ENOMEM;
8211
8212 /*
8213 * The bootstrap mailbox region is comprised of 2 parts
8214 * plus an alignment restriction of 16 bytes.
8215 */
8216 bmbx_size = sizeof(struct lpfc_bmbx_create) + (LPFC_ALIGN_16_BYTE - 1);
Luis Chamberlain750afb02019-01-04 09:23:09 +01008217 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev, bmbx_size,
8218 &dmabuf->phys, GFP_KERNEL);
James Smartda0436e2009-05-22 14:51:39 -04008219 if (!dmabuf->virt) {
8220 kfree(dmabuf);
8221 return -ENOMEM;
8222 }
James Smartda0436e2009-05-22 14:51:39 -04008223
8224 /*
8225 * Initialize the bootstrap mailbox pointers now so that the register
8226 * operations are simple later. The mailbox dma address is required
8227 * to be 16-byte aligned. Also align the virtual memory as each
8228 * maibox is copied into the bmbx mailbox region before issuing the
8229 * command to the port.
8230 */
8231 phba->sli4_hba.bmbx.dmabuf = dmabuf;
8232 phba->sli4_hba.bmbx.bmbx_size = bmbx_size;
8233
8234 phba->sli4_hba.bmbx.avirt = PTR_ALIGN(dmabuf->virt,
8235 LPFC_ALIGN_16_BYTE);
8236 phba->sli4_hba.bmbx.aphys = ALIGN(dmabuf->phys,
8237 LPFC_ALIGN_16_BYTE);
8238
8239 /*
8240 * Set the high and low physical addresses now. The SLI4 alignment
8241 * requirement is 16 bytes and the mailbox is posted to the port
8242 * as two 30-bit addresses. The other data is a bit marking whether
8243 * the 30-bit address is the high or low address.
8244 * Upcast bmbx aphys to 64bits so shift instruction compiles
8245 * clean on 32 bit machines.
8246 */
8247 dma_address = &phba->sli4_hba.bmbx.dma_address;
8248 phys_addr = (uint64_t)phba->sli4_hba.bmbx.aphys;
8249 pa_addr = (uint32_t) ((phys_addr >> 34) & 0x3fffffff);
8250 dma_address->addr_hi = (uint32_t) ((pa_addr << 2) |
8251 LPFC_BMBX_BIT1_ADDR_HI);
8252
8253 pa_addr = (uint32_t) ((phba->sli4_hba.bmbx.aphys >> 4) & 0x3fffffff);
8254 dma_address->addr_lo = (uint32_t) ((pa_addr << 2) |
8255 LPFC_BMBX_BIT1_ADDR_LO);
8256 return 0;
8257}
8258
8259/**
8260 * lpfc_destroy_bootstrap_mbox - Destroy all bootstrap mailbox resources
8261 * @phba: pointer to lpfc hba data structure.
8262 *
8263 * This routine is invoked to teardown the bootstrap mailbox
8264 * region and release all host resources. This routine requires
8265 * the caller to ensure all mailbox commands recovered, no
8266 * additional mailbox comands are sent, and interrupts are disabled
8267 * before calling this routine.
8268 *
8269 **/
8270static void
8271lpfc_destroy_bootstrap_mbox(struct lpfc_hba *phba)
8272{
8273 dma_free_coherent(&phba->pcidev->dev,
8274 phba->sli4_hba.bmbx.bmbx_size,
8275 phba->sli4_hba.bmbx.dmabuf->virt,
8276 phba->sli4_hba.bmbx.dmabuf->phys);
8277
8278 kfree(phba->sli4_hba.bmbx.dmabuf);
8279 memset(&phba->sli4_hba.bmbx, 0, sizeof(struct lpfc_bmbx));
8280}
8281
James Smart83c6cb12019-10-18 14:18:30 -07008282static const char * const lpfc_topo_to_str[] = {
8283 "Loop then P2P",
8284 "Loopback",
8285 "P2P Only",
8286 "Unsupported",
8287 "Loop Only",
8288 "Unsupported",
8289 "P2P then Loop",
8290};
8291
8292/**
8293 * lpfc_map_topology - Map the topology read from READ_CONFIG
8294 * @phba: pointer to lpfc hba data structure.
8295 * @rdconf: pointer to read config data
8296 *
8297 * This routine is invoked to map the topology values as read
8298 * from the read config mailbox command. If the persistent
8299 * topology feature is supported, the firmware will provide the
8300 * saved topology information to be used in INIT_LINK
8301 *
8302 **/
8303#define LINK_FLAGS_DEF 0x0
8304#define LINK_FLAGS_P2P 0x1
8305#define LINK_FLAGS_LOOP 0x2
8306static void
8307lpfc_map_topology(struct lpfc_hba *phba, struct lpfc_mbx_read_config *rd_config)
8308{
8309 u8 ptv, tf, pt;
8310
8311 ptv = bf_get(lpfc_mbx_rd_conf_ptv, rd_config);
8312 tf = bf_get(lpfc_mbx_rd_conf_tf, rd_config);
8313 pt = bf_get(lpfc_mbx_rd_conf_pt, rd_config);
8314
8315 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8316 "2027 Read Config Data : ptv:0x%x, tf:0x%x pt:0x%x",
8317 ptv, tf, pt);
8318 if (!ptv) {
8319 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8320 "2019 FW does not support persistent topology "
8321 "Using driver parameter defined value [%s]",
8322 lpfc_topo_to_str[phba->cfg_topology]);
8323 return;
8324 }
8325 /* FW supports persistent topology - override module parameter value */
8326 phba->hba_flag |= HBA_PERSISTENT_TOPO;
8327 switch (phba->pcidev->device) {
8328 case PCI_DEVICE_ID_LANCER_G7_FC:
8329 if (tf || (pt == LINK_FLAGS_LOOP)) {
8330 /* Invalid values from FW - use driver params */
8331 phba->hba_flag &= ~HBA_PERSISTENT_TOPO;
8332 } else {
8333 /* Prism only supports PT2PT topology */
8334 phba->cfg_topology = FLAGS_TOPOLOGY_MODE_PT_PT;
8335 }
8336 break;
8337 case PCI_DEVICE_ID_LANCER_G6_FC:
8338 if (!tf) {
8339 phba->cfg_topology = ((pt == LINK_FLAGS_LOOP)
8340 ? FLAGS_TOPOLOGY_MODE_LOOP
8341 : FLAGS_TOPOLOGY_MODE_PT_PT);
8342 } else {
8343 phba->hba_flag &= ~HBA_PERSISTENT_TOPO;
8344 }
8345 break;
8346 default: /* G5 */
8347 if (tf) {
8348 /* If topology failover set - pt is '0' or '1' */
8349 phba->cfg_topology = (pt ? FLAGS_TOPOLOGY_MODE_PT_LOOP :
8350 FLAGS_TOPOLOGY_MODE_LOOP_PT);
8351 } else {
8352 phba->cfg_topology = ((pt == LINK_FLAGS_P2P)
8353 ? FLAGS_TOPOLOGY_MODE_PT_PT
8354 : FLAGS_TOPOLOGY_MODE_LOOP);
8355 }
8356 break;
8357 }
8358 if (phba->hba_flag & HBA_PERSISTENT_TOPO) {
8359 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8360 "2020 Using persistent topology value [%s]",
8361 lpfc_topo_to_str[phba->cfg_topology]);
8362 } else {
8363 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8364 "2021 Invalid topology values from FW "
8365 "Using driver parameter defined value [%s]",
8366 lpfc_topo_to_str[phba->cfg_topology]);
8367 }
8368}
8369
James Smartda0436e2009-05-22 14:51:39 -04008370/**
8371 * lpfc_sli4_read_config - Get the config parameters.
8372 * @phba: pointer to lpfc hba data structure.
8373 *
8374 * This routine is invoked to read the configuration parameters from the HBA.
8375 * The configuration parameters are used to set the base and maximum values
8376 * for RPI's XRI's VPI's VFI's and FCFIs. These values also affect the resource
8377 * allocation for the port.
8378 *
8379 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008380 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008381 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04008382 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04008383 **/
James Smartff78d8f2011-12-13 13:21:35 -05008384int
James Smartda0436e2009-05-22 14:51:39 -04008385lpfc_sli4_read_config(struct lpfc_hba *phba)
8386{
8387 LPFC_MBOXQ_t *pmb;
8388 struct lpfc_mbx_read_config *rd_config;
James Smart912e3ac2011-05-24 11:42:11 -04008389 union lpfc_sli4_cfg_shdr *shdr;
8390 uint32_t shdr_status, shdr_add_status;
8391 struct lpfc_mbx_get_func_cfg *get_func_cfg;
8392 struct lpfc_rsrc_desc_fcfcoe *desc;
James Smart8aa134a2012-08-14 14:25:29 -04008393 char *pdesc_0;
James Smartc6918162016-10-13 15:06:16 -07008394 uint16_t forced_link_speed;
James Smart6a828b02019-01-28 11:14:31 -08008395 uint32_t if_type, qmin;
James Smart8aa134a2012-08-14 14:25:29 -04008396 int length, i, rc = 0, rc2;
James Smartda0436e2009-05-22 14:51:39 -04008397
8398 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
8399 if (!pmb) {
8400 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8401 "2011 Unable to allocate memory for issuing "
8402 "SLI_CONFIG_SPECIAL mailbox command\n");
8403 return -ENOMEM;
8404 }
8405
8406 lpfc_read_config(phba, pmb);
8407
8408 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
8409 if (rc != MBX_SUCCESS) {
8410 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8411 "2012 Mailbox failed , mbxCmd x%x "
8412 "READ_CONFIG, mbxStatus x%x\n",
8413 bf_get(lpfc_mqe_command, &pmb->u.mqe),
8414 bf_get(lpfc_mqe_status, &pmb->u.mqe));
8415 rc = -EIO;
8416 } else {
8417 rd_config = &pmb->u.mqe.un.rd_config;
James Smartff78d8f2011-12-13 13:21:35 -05008418 if (bf_get(lpfc_mbx_rd_conf_lnk_ldv, rd_config)) {
8419 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
8420 phba->sli4_hba.lnk_info.lnk_tp =
8421 bf_get(lpfc_mbx_rd_conf_lnk_type, rd_config);
8422 phba->sli4_hba.lnk_info.lnk_no =
8423 bf_get(lpfc_mbx_rd_conf_lnk_numb, rd_config);
8424 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8425 "3081 lnk_type:%d, lnk_numb:%d\n",
8426 phba->sli4_hba.lnk_info.lnk_tp,
8427 phba->sli4_hba.lnk_info.lnk_no);
8428 } else
8429 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8430 "3082 Mailbox (x%x) returned ldv:x0\n",
8431 bf_get(lpfc_mqe_command, &pmb->u.mqe));
James Smart44fd7fe2017-08-23 16:55:47 -07008432 if (bf_get(lpfc_mbx_rd_conf_bbscn_def, rd_config)) {
8433 phba->bbcredit_support = 1;
8434 phba->sli4_hba.bbscn_params.word0 = rd_config->word8;
8435 }
8436
James Smart1dc5ec22018-10-23 13:41:11 -07008437 phba->sli4_hba.conf_trunk =
8438 bf_get(lpfc_mbx_rd_conf_trunk, rd_config);
James Smart6d368e52011-05-24 11:44:12 -04008439 phba->sli4_hba.extents_in_use =
8440 bf_get(lpfc_mbx_rd_conf_extnts_inuse, rd_config);
James Smartda0436e2009-05-22 14:51:39 -04008441 phba->sli4_hba.max_cfg_param.max_xri =
8442 bf_get(lpfc_mbx_rd_conf_xri_count, rd_config);
James Smart31f06d22019-08-14 16:56:31 -07008443 /* Reduce resource usage in kdump environment */
8444 if (is_kdump_kernel() &&
8445 phba->sli4_hba.max_cfg_param.max_xri > 512)
8446 phba->sli4_hba.max_cfg_param.max_xri = 512;
James Smartda0436e2009-05-22 14:51:39 -04008447 phba->sli4_hba.max_cfg_param.xri_base =
8448 bf_get(lpfc_mbx_rd_conf_xri_base, rd_config);
8449 phba->sli4_hba.max_cfg_param.max_vpi =
8450 bf_get(lpfc_mbx_rd_conf_vpi_count, rd_config);
James Smart8b47ae62018-11-29 16:09:33 -08008451 /* Limit the max we support */
8452 if (phba->sli4_hba.max_cfg_param.max_vpi > LPFC_MAX_VPORTS)
8453 phba->sli4_hba.max_cfg_param.max_vpi = LPFC_MAX_VPORTS;
James Smartda0436e2009-05-22 14:51:39 -04008454 phba->sli4_hba.max_cfg_param.vpi_base =
8455 bf_get(lpfc_mbx_rd_conf_vpi_base, rd_config);
8456 phba->sli4_hba.max_cfg_param.max_rpi =
8457 bf_get(lpfc_mbx_rd_conf_rpi_count, rd_config);
8458 phba->sli4_hba.max_cfg_param.rpi_base =
8459 bf_get(lpfc_mbx_rd_conf_rpi_base, rd_config);
8460 phba->sli4_hba.max_cfg_param.max_vfi =
8461 bf_get(lpfc_mbx_rd_conf_vfi_count, rd_config);
8462 phba->sli4_hba.max_cfg_param.vfi_base =
8463 bf_get(lpfc_mbx_rd_conf_vfi_base, rd_config);
8464 phba->sli4_hba.max_cfg_param.max_fcfi =
8465 bf_get(lpfc_mbx_rd_conf_fcfi_count, rd_config);
James Smartda0436e2009-05-22 14:51:39 -04008466 phba->sli4_hba.max_cfg_param.max_eq =
8467 bf_get(lpfc_mbx_rd_conf_eq_count, rd_config);
8468 phba->sli4_hba.max_cfg_param.max_rq =
8469 bf_get(lpfc_mbx_rd_conf_rq_count, rd_config);
8470 phba->sli4_hba.max_cfg_param.max_wq =
8471 bf_get(lpfc_mbx_rd_conf_wq_count, rd_config);
8472 phba->sli4_hba.max_cfg_param.max_cq =
8473 bf_get(lpfc_mbx_rd_conf_cq_count, rd_config);
8474 phba->lmt = bf_get(lpfc_mbx_rd_conf_lmt, rd_config);
8475 phba->sli4_hba.next_xri = phba->sli4_hba.max_cfg_param.xri_base;
8476 phba->vpi_base = phba->sli4_hba.max_cfg_param.vpi_base;
8477 phba->vfi_base = phba->sli4_hba.max_cfg_param.vfi_base;
James Smart5ffc2662009-11-18 15:39:44 -05008478 phba->max_vpi = (phba->sli4_hba.max_cfg_param.max_vpi > 0) ?
8479 (phba->sli4_hba.max_cfg_param.max_vpi - 1) : 0;
James Smartda0436e2009-05-22 14:51:39 -04008480 phba->max_vports = phba->max_vpi;
James Smart83c6cb12019-10-18 14:18:30 -07008481 lpfc_map_topology(phba, rd_config);
James Smartda0436e2009-05-22 14:51:39 -04008482 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart6d368e52011-05-24 11:44:12 -04008483 "2003 cfg params Extents? %d "
8484 "XRI(B:%d M:%d), "
James Smartda0436e2009-05-22 14:51:39 -04008485 "VPI(B:%d M:%d) "
8486 "VFI(B:%d M:%d) "
8487 "RPI(B:%d M:%d) "
James Smart2ea259e2017-02-12 13:52:27 -08008488 "FCFI:%d EQ:%d CQ:%d WQ:%d RQ:%d\n",
James Smart6d368e52011-05-24 11:44:12 -04008489 phba->sli4_hba.extents_in_use,
James Smartda0436e2009-05-22 14:51:39 -04008490 phba->sli4_hba.max_cfg_param.xri_base,
8491 phba->sli4_hba.max_cfg_param.max_xri,
8492 phba->sli4_hba.max_cfg_param.vpi_base,
8493 phba->sli4_hba.max_cfg_param.max_vpi,
8494 phba->sli4_hba.max_cfg_param.vfi_base,
8495 phba->sli4_hba.max_cfg_param.max_vfi,
8496 phba->sli4_hba.max_cfg_param.rpi_base,
8497 phba->sli4_hba.max_cfg_param.max_rpi,
James Smart2ea259e2017-02-12 13:52:27 -08008498 phba->sli4_hba.max_cfg_param.max_fcfi,
8499 phba->sli4_hba.max_cfg_param.max_eq,
8500 phba->sli4_hba.max_cfg_param.max_cq,
8501 phba->sli4_hba.max_cfg_param.max_wq,
8502 phba->sli4_hba.max_cfg_param.max_rq);
8503
James Smartd38f33b2018-05-04 20:37:54 -07008504 /*
James Smart6a828b02019-01-28 11:14:31 -08008505 * Calculate queue resources based on how
8506 * many WQ/CQ/EQs are available.
James Smartd38f33b2018-05-04 20:37:54 -07008507 */
James Smart6a828b02019-01-28 11:14:31 -08008508 qmin = phba->sli4_hba.max_cfg_param.max_wq;
8509 if (phba->sli4_hba.max_cfg_param.max_cq < qmin)
8510 qmin = phba->sli4_hba.max_cfg_param.max_cq;
8511 if (phba->sli4_hba.max_cfg_param.max_eq < qmin)
8512 qmin = phba->sli4_hba.max_cfg_param.max_eq;
8513 /*
8514 * Whats left after this can go toward NVME / FCP.
8515 * The minus 4 accounts for ELS, NVME LS, MBOX
8516 * plus one extra. When configured for
8517 * NVMET, FCP io channel WQs are not created.
8518 */
8519 qmin -= 4;
James Smartd38f33b2018-05-04 20:37:54 -07008520
James Smart6a828b02019-01-28 11:14:31 -08008521 /* Check to see if there is enough for NVME */
8522 if ((phba->cfg_irq_chann > qmin) ||
8523 (phba->cfg_hdw_queue > qmin)) {
8524 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8525 "2005 Reducing Queues: "
8526 "WQ %d CQ %d EQ %d: min %d: "
8527 "IRQ %d HDWQ %d\n",
James Smartd38f33b2018-05-04 20:37:54 -07008528 phba->sli4_hba.max_cfg_param.max_wq,
8529 phba->sli4_hba.max_cfg_param.max_cq,
James Smart6a828b02019-01-28 11:14:31 -08008530 phba->sli4_hba.max_cfg_param.max_eq,
8531 qmin, phba->cfg_irq_chann,
James Smartcdb42be2019-01-28 11:14:21 -08008532 phba->cfg_hdw_queue);
James Smartd38f33b2018-05-04 20:37:54 -07008533
James Smart6a828b02019-01-28 11:14:31 -08008534 if (phba->cfg_irq_chann > qmin)
8535 phba->cfg_irq_chann = qmin;
8536 if (phba->cfg_hdw_queue > qmin)
8537 phba->cfg_hdw_queue = qmin;
James Smartd38f33b2018-05-04 20:37:54 -07008538 }
James Smartda0436e2009-05-22 14:51:39 -04008539 }
James Smart912e3ac2011-05-24 11:42:11 -04008540
8541 if (rc)
8542 goto read_cfg_out;
James Smartda0436e2009-05-22 14:51:39 -04008543
James Smartc6918162016-10-13 15:06:16 -07008544 /* Update link speed if forced link speed is supported */
8545 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
James Smart27d6ac02018-02-22 08:18:42 -08008546 if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
James Smartc6918162016-10-13 15:06:16 -07008547 forced_link_speed =
8548 bf_get(lpfc_mbx_rd_conf_link_speed, rd_config);
8549 if (forced_link_speed) {
8550 phba->hba_flag |= HBA_FORCED_LINK_SPEED;
8551
8552 switch (forced_link_speed) {
8553 case LINK_SPEED_1G:
8554 phba->cfg_link_speed =
8555 LPFC_USER_LINK_SPEED_1G;
8556 break;
8557 case LINK_SPEED_2G:
8558 phba->cfg_link_speed =
8559 LPFC_USER_LINK_SPEED_2G;
8560 break;
8561 case LINK_SPEED_4G:
8562 phba->cfg_link_speed =
8563 LPFC_USER_LINK_SPEED_4G;
8564 break;
8565 case LINK_SPEED_8G:
8566 phba->cfg_link_speed =
8567 LPFC_USER_LINK_SPEED_8G;
8568 break;
8569 case LINK_SPEED_10G:
8570 phba->cfg_link_speed =
8571 LPFC_USER_LINK_SPEED_10G;
8572 break;
8573 case LINK_SPEED_16G:
8574 phba->cfg_link_speed =
8575 LPFC_USER_LINK_SPEED_16G;
8576 break;
8577 case LINK_SPEED_32G:
8578 phba->cfg_link_speed =
8579 LPFC_USER_LINK_SPEED_32G;
8580 break;
James Smartfbd8a6b2018-02-22 08:18:45 -08008581 case LINK_SPEED_64G:
8582 phba->cfg_link_speed =
8583 LPFC_USER_LINK_SPEED_64G;
8584 break;
James Smartc6918162016-10-13 15:06:16 -07008585 case 0xffff:
8586 phba->cfg_link_speed =
8587 LPFC_USER_LINK_SPEED_AUTO;
8588 break;
8589 default:
8590 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8591 "0047 Unrecognized link "
8592 "speed : %d\n",
8593 forced_link_speed);
8594 phba->cfg_link_speed =
8595 LPFC_USER_LINK_SPEED_AUTO;
8596 }
8597 }
8598 }
8599
James Smartda0436e2009-05-22 14:51:39 -04008600 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
James Smart572709e2013-07-15 18:32:43 -04008601 length = phba->sli4_hba.max_cfg_param.max_xri -
8602 lpfc_sli4_get_els_iocb_cnt(phba);
8603 if (phba->cfg_hba_queue_depth > length) {
8604 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
8605 "3361 HBA queue depth changed from %d to %d\n",
8606 phba->cfg_hba_queue_depth, length);
8607 phba->cfg_hba_queue_depth = length;
8608 }
James Smart912e3ac2011-05-24 11:42:11 -04008609
James Smart27d6ac02018-02-22 08:18:42 -08008610 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) <
James Smart912e3ac2011-05-24 11:42:11 -04008611 LPFC_SLI_INTF_IF_TYPE_2)
8612 goto read_cfg_out;
8613
8614 /* get the pf# and vf# for SLI4 if_type 2 port */
8615 length = (sizeof(struct lpfc_mbx_get_func_cfg) -
8616 sizeof(struct lpfc_sli4_cfg_mhdr));
8617 lpfc_sli4_config(phba, pmb, LPFC_MBOX_SUBSYSTEM_COMMON,
8618 LPFC_MBOX_OPCODE_GET_FUNCTION_CONFIG,
8619 length, LPFC_SLI4_MBX_EMBED);
8620
James Smart8aa134a2012-08-14 14:25:29 -04008621 rc2 = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart912e3ac2011-05-24 11:42:11 -04008622 shdr = (union lpfc_sli4_cfg_shdr *)
8623 &pmb->u.mqe.un.sli4_config.header.cfg_shdr;
8624 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
8625 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
James Smart8aa134a2012-08-14 14:25:29 -04008626 if (rc2 || shdr_status || shdr_add_status) {
James Smart912e3ac2011-05-24 11:42:11 -04008627 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8628 "3026 Mailbox failed , mbxCmd x%x "
8629 "GET_FUNCTION_CONFIG, mbxStatus x%x\n",
8630 bf_get(lpfc_mqe_command, &pmb->u.mqe),
8631 bf_get(lpfc_mqe_status, &pmb->u.mqe));
James Smart912e3ac2011-05-24 11:42:11 -04008632 goto read_cfg_out;
8633 }
8634
8635 /* search for fc_fcoe resrouce descriptor */
8636 get_func_cfg = &pmb->u.mqe.un.get_func_cfg;
James Smart912e3ac2011-05-24 11:42:11 -04008637
James Smart8aa134a2012-08-14 14:25:29 -04008638 pdesc_0 = (char *)&get_func_cfg->func_cfg.desc[0];
8639 desc = (struct lpfc_rsrc_desc_fcfcoe *)pdesc_0;
8640 length = bf_get(lpfc_rsrc_desc_fcfcoe_length, desc);
8641 if (length == LPFC_RSRC_DESC_TYPE_FCFCOE_V0_RSVD)
8642 length = LPFC_RSRC_DESC_TYPE_FCFCOE_V0_LENGTH;
8643 else if (length != LPFC_RSRC_DESC_TYPE_FCFCOE_V1_LENGTH)
8644 goto read_cfg_out;
8645
James Smart912e3ac2011-05-24 11:42:11 -04008646 for (i = 0; i < LPFC_RSRC_DESC_MAX_NUM; i++) {
James Smart8aa134a2012-08-14 14:25:29 -04008647 desc = (struct lpfc_rsrc_desc_fcfcoe *)(pdesc_0 + length * i);
James Smart912e3ac2011-05-24 11:42:11 -04008648 if (LPFC_RSRC_DESC_TYPE_FCFCOE ==
James Smart8aa134a2012-08-14 14:25:29 -04008649 bf_get(lpfc_rsrc_desc_fcfcoe_type, desc)) {
James Smart912e3ac2011-05-24 11:42:11 -04008650 phba->sli4_hba.iov.pf_number =
8651 bf_get(lpfc_rsrc_desc_fcfcoe_pfnum, desc);
8652 phba->sli4_hba.iov.vf_number =
8653 bf_get(lpfc_rsrc_desc_fcfcoe_vfnum, desc);
8654 break;
8655 }
8656 }
8657
8658 if (i < LPFC_RSRC_DESC_MAX_NUM)
8659 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8660 "3027 GET_FUNCTION_CONFIG: pf_number:%d, "
8661 "vf_number:%d\n", phba->sli4_hba.iov.pf_number,
8662 phba->sli4_hba.iov.vf_number);
James Smart8aa134a2012-08-14 14:25:29 -04008663 else
James Smart912e3ac2011-05-24 11:42:11 -04008664 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8665 "3028 GET_FUNCTION_CONFIG: failed to find "
Colin Ian Kingc4dba182018-10-16 18:28:53 +01008666 "Resource Descriptor:x%x\n",
James Smart912e3ac2011-05-24 11:42:11 -04008667 LPFC_RSRC_DESC_TYPE_FCFCOE);
James Smart912e3ac2011-05-24 11:42:11 -04008668
8669read_cfg_out:
8670 mempool_free(pmb, phba->mbox_mem_pool);
James Smartda0436e2009-05-22 14:51:39 -04008671 return rc;
8672}
8673
8674/**
James Smart2fcee4b2010-12-15 17:57:46 -05008675 * lpfc_setup_endian_order - Write endian order to an SLI4 if_type 0 port.
James Smartda0436e2009-05-22 14:51:39 -04008676 * @phba: pointer to lpfc hba data structure.
8677 *
James Smart2fcee4b2010-12-15 17:57:46 -05008678 * This routine is invoked to setup the port-side endian order when
8679 * the port if_type is 0. This routine has no function for other
8680 * if_types.
James Smartda0436e2009-05-22 14:51:39 -04008681 *
8682 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008683 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008684 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04008685 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04008686 **/
8687static int
8688lpfc_setup_endian_order(struct lpfc_hba *phba)
8689{
8690 LPFC_MBOXQ_t *mboxq;
James Smart2fcee4b2010-12-15 17:57:46 -05008691 uint32_t if_type, rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04008692 uint32_t endian_mb_data[2] = {HOST_ENDIAN_LOW_WORD0,
8693 HOST_ENDIAN_HIGH_WORD1};
8694
James Smart2fcee4b2010-12-15 17:57:46 -05008695 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
8696 switch (if_type) {
8697 case LPFC_SLI_INTF_IF_TYPE_0:
8698 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
8699 GFP_KERNEL);
8700 if (!mboxq) {
8701 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8702 "0492 Unable to allocate memory for "
8703 "issuing SLI_CONFIG_SPECIAL mailbox "
8704 "command\n");
8705 return -ENOMEM;
8706 }
James Smartda0436e2009-05-22 14:51:39 -04008707
James Smart2fcee4b2010-12-15 17:57:46 -05008708 /*
8709 * The SLI4_CONFIG_SPECIAL mailbox command requires the first
8710 * two words to contain special data values and no other data.
8711 */
8712 memset(mboxq, 0, sizeof(LPFC_MBOXQ_t));
8713 memcpy(&mboxq->u.mqe, &endian_mb_data, sizeof(endian_mb_data));
8714 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8715 if (rc != MBX_SUCCESS) {
8716 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8717 "0493 SLI_CONFIG_SPECIAL mailbox "
8718 "failed with status x%x\n",
8719 rc);
8720 rc = -EIO;
8721 }
8722 mempool_free(mboxq, phba->mbox_mem_pool);
8723 break;
James Smart27d6ac02018-02-22 08:18:42 -08008724 case LPFC_SLI_INTF_IF_TYPE_6:
James Smart2fcee4b2010-12-15 17:57:46 -05008725 case LPFC_SLI_INTF_IF_TYPE_2:
8726 case LPFC_SLI_INTF_IF_TYPE_1:
8727 default:
8728 break;
James Smartda0436e2009-05-22 14:51:39 -04008729 }
James Smartda0436e2009-05-22 14:51:39 -04008730 return rc;
8731}
8732
8733/**
James Smart895427b2017-02-12 13:52:30 -08008734 * lpfc_sli4_queue_verify - Verify and update EQ counts
James Smartda0436e2009-05-22 14:51:39 -04008735 * @phba: pointer to lpfc hba data structure.
8736 *
James Smart895427b2017-02-12 13:52:30 -08008737 * This routine is invoked to check the user settable queue counts for EQs.
8738 * After this routine is called the counts will be set to valid values that
James Smart5350d872011-10-10 21:33:49 -04008739 * adhere to the constraints of the system's interrupt vectors and the port's
8740 * queue resources.
James Smartda0436e2009-05-22 14:51:39 -04008741 *
8742 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008743 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008744 * -ENOMEM - No available memory
James Smartda0436e2009-05-22 14:51:39 -04008745 **/
8746static int
James Smart5350d872011-10-10 21:33:49 -04008747lpfc_sli4_queue_verify(struct lpfc_hba *phba)
James Smartda0436e2009-05-22 14:51:39 -04008748{
James Smartda0436e2009-05-22 14:51:39 -04008749 /*
James Smart67d12732012-08-03 12:36:13 -04008750 * Sanity check for configured queue parameters against the run-time
James Smartda0436e2009-05-22 14:51:39 -04008751 * device parameters
8752 */
8753
James Smartbcb24f62017-11-20 16:00:36 -08008754 if (phba->nvmet_support) {
James Smart97a9ed32019-10-18 14:18:17 -07008755 if (phba->cfg_hdw_queue < phba->cfg_nvmet_mrq)
8756 phba->cfg_nvmet_mrq = phba->cfg_hdw_queue;
James Smart982ab122019-03-12 16:30:10 -07008757 if (phba->cfg_nvmet_mrq > LPFC_NVMET_MRQ_MAX)
8758 phba->cfg_nvmet_mrq = LPFC_NVMET_MRQ_MAX;
James Smartbcb24f62017-11-20 16:00:36 -08008759 }
James Smart895427b2017-02-12 13:52:30 -08008760
8761 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart6a828b02019-01-28 11:14:31 -08008762 "2574 IO channels: hdwQ %d IRQ %d MRQ: %d\n",
8763 phba->cfg_hdw_queue, phba->cfg_irq_chann,
8764 phba->cfg_nvmet_mrq);
James Smartda0436e2009-05-22 14:51:39 -04008765
James Smartda0436e2009-05-22 14:51:39 -04008766 /* Get EQ depth from module parameter, fake the default for now */
8767 phba->sli4_hba.eq_esize = LPFC_EQE_SIZE_4B;
8768 phba->sli4_hba.eq_ecount = LPFC_EQE_DEF_COUNT;
8769
James Smart5350d872011-10-10 21:33:49 -04008770 /* Get CQ depth from module parameter, fake the default for now */
8771 phba->sli4_hba.cq_esize = LPFC_CQE_SIZE;
8772 phba->sli4_hba.cq_ecount = LPFC_CQE_DEF_COUNT;
James Smart5350d872011-10-10 21:33:49 -04008773 return 0;
James Smart895427b2017-02-12 13:52:30 -08008774}
8775
8776static int
James Smartc00f62e2019-08-14 16:57:11 -07008777lpfc_alloc_io_wq_cq(struct lpfc_hba *phba, int idx)
James Smart895427b2017-02-12 13:52:30 -08008778{
8779 struct lpfc_queue *qdesc;
James Smartc00f62e2019-08-14 16:57:11 -07008780 u32 wqesize;
James Smartc1a21eb2019-03-12 16:30:29 -07008781 int cpu;
James Smart895427b2017-02-12 13:52:30 -08008782
James Smartc00f62e2019-08-14 16:57:11 -07008783 cpu = lpfc_find_cpu_handle(phba, idx, LPFC_FIND_BY_HDWQ);
8784 /* Create Fast Path IO CQs */
James Smartc176ffa2018-01-30 15:58:46 -08008785 if (phba->enab_exp_wqcq_pages)
James Smarta51e41b2017-12-08 17:18:06 -08008786 /* Increase the CQ size when WQEs contain an embedded cdb */
8787 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_EXPANDED_PAGE_SIZE,
8788 phba->sli4_hba.cq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -07008789 LPFC_CQE_EXP_COUNT, cpu);
James Smarta51e41b2017-12-08 17:18:06 -08008790
8791 else
8792 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
8793 phba->sli4_hba.cq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -07008794 phba->sli4_hba.cq_ecount, cpu);
James Smart895427b2017-02-12 13:52:30 -08008795 if (!qdesc) {
8796 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartc00f62e2019-08-14 16:57:11 -07008797 "0499 Failed allocate fast-path IO CQ (%d)\n", idx);
James Smart895427b2017-02-12 13:52:30 -08008798 return 1;
8799 }
James Smart7365f6f2018-02-22 08:18:46 -08008800 qdesc->qe_valid = 1;
James Smartc00f62e2019-08-14 16:57:11 -07008801 qdesc->hdwq = idx;
James Smartc1a21eb2019-03-12 16:30:29 -07008802 qdesc->chann = cpu;
James Smartc00f62e2019-08-14 16:57:11 -07008803 phba->sli4_hba.hdwq[idx].io_cq = qdesc;
James Smart895427b2017-02-12 13:52:30 -08008804
James Smartc00f62e2019-08-14 16:57:11 -07008805 /* Create Fast Path IO WQs */
James Smartc176ffa2018-01-30 15:58:46 -08008806 if (phba->enab_exp_wqcq_pages) {
James Smarta51e41b2017-12-08 17:18:06 -08008807 /* Increase the WQ size when WQEs contain an embedded cdb */
James Smartc176ffa2018-01-30 15:58:46 -08008808 wqesize = (phba->fcp_embed_io) ?
8809 LPFC_WQE128_SIZE : phba->sli4_hba.wq_esize;
James Smarta51e41b2017-12-08 17:18:06 -08008810 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_EXPANDED_PAGE_SIZE,
James Smartc176ffa2018-01-30 15:58:46 -08008811 wqesize,
James Smartc1a21eb2019-03-12 16:30:29 -07008812 LPFC_WQE_EXP_COUNT, cpu);
James Smartc176ffa2018-01-30 15:58:46 -08008813 } else
James Smarta51e41b2017-12-08 17:18:06 -08008814 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
8815 phba->sli4_hba.wq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -07008816 phba->sli4_hba.wq_ecount, cpu);
James Smartc176ffa2018-01-30 15:58:46 -08008817
James Smart895427b2017-02-12 13:52:30 -08008818 if (!qdesc) {
8819 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartc00f62e2019-08-14 16:57:11 -07008820 "0503 Failed allocate fast-path IO WQ (%d)\n",
8821 idx);
James Smart895427b2017-02-12 13:52:30 -08008822 return 1;
8823 }
James Smartc00f62e2019-08-14 16:57:11 -07008824 qdesc->hdwq = idx;
8825 qdesc->chann = cpu;
8826 phba->sli4_hba.hdwq[idx].io_wq = qdesc;
James Smart895427b2017-02-12 13:52:30 -08008827 list_add_tail(&qdesc->wq_list, &phba->sli4_hba.lpfc_wq_list);
8828 return 0;
James Smart5350d872011-10-10 21:33:49 -04008829}
8830
8831/**
8832 * lpfc_sli4_queue_create - Create all the SLI4 queues
8833 * @phba: pointer to lpfc hba data structure.
8834 *
8835 * This routine is invoked to allocate all the SLI4 queues for the FCoE HBA
8836 * operation. For each SLI4 queue type, the parameters such as queue entry
8837 * count (queue depth) shall be taken from the module parameter. For now,
8838 * we just use some constant number as place holder.
8839 *
8840 * Return codes
Anatol Pomozov4907cb72012-09-01 10:31:09 -07008841 * 0 - successful
James Smart5350d872011-10-10 21:33:49 -04008842 * -ENOMEM - No availble memory
8843 * -EIO - The mailbox failed to complete successfully.
8844 **/
8845int
8846lpfc_sli4_queue_create(struct lpfc_hba *phba)
8847{
8848 struct lpfc_queue *qdesc;
James Smart657add42019-05-21 17:49:06 -07008849 int idx, cpu, eqcpu;
James Smart5e5b5112019-01-28 11:14:22 -08008850 struct lpfc_sli4_hdw_queue *qp;
James Smart657add42019-05-21 17:49:06 -07008851 struct lpfc_vector_map_info *cpup;
8852 struct lpfc_vector_map_info *eqcpup;
James Smart32517fc2019-01-28 11:14:33 -08008853 struct lpfc_eq_intr_info *eqi;
James Smart5350d872011-10-10 21:33:49 -04008854
8855 /*
James Smart67d12732012-08-03 12:36:13 -04008856 * Create HBA Record arrays.
James Smart895427b2017-02-12 13:52:30 -08008857 * Both NVME and FCP will share that same vectors / EQs
James Smart5350d872011-10-10 21:33:49 -04008858 */
James Smart67d12732012-08-03 12:36:13 -04008859 phba->sli4_hba.mq_esize = LPFC_MQE_SIZE;
8860 phba->sli4_hba.mq_ecount = LPFC_MQE_DEF_COUNT;
8861 phba->sli4_hba.wq_esize = LPFC_WQE_SIZE;
8862 phba->sli4_hba.wq_ecount = LPFC_WQE_DEF_COUNT;
8863 phba->sli4_hba.rq_esize = LPFC_RQE_SIZE;
8864 phba->sli4_hba.rq_ecount = LPFC_RQE_DEF_COUNT;
James Smart895427b2017-02-12 13:52:30 -08008865 phba->sli4_hba.eq_esize = LPFC_EQE_SIZE_4B;
8866 phba->sli4_hba.eq_ecount = LPFC_EQE_DEF_COUNT;
8867 phba->sli4_hba.cq_esize = LPFC_CQE_SIZE;
8868 phba->sli4_hba.cq_ecount = LPFC_CQE_DEF_COUNT;
James Smart67d12732012-08-03 12:36:13 -04008869
James Smartcdb42be2019-01-28 11:14:21 -08008870 if (!phba->sli4_hba.hdwq) {
James Smart5e5b5112019-01-28 11:14:22 -08008871 phba->sli4_hba.hdwq = kcalloc(
8872 phba->cfg_hdw_queue, sizeof(struct lpfc_sli4_hdw_queue),
8873 GFP_KERNEL);
8874 if (!phba->sli4_hba.hdwq) {
James Smart895427b2017-02-12 13:52:30 -08008875 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart5e5b5112019-01-28 11:14:22 -08008876 "6427 Failed allocate memory for "
8877 "fast-path Hardware Queue array\n");
James Smart895427b2017-02-12 13:52:30 -08008878 goto out_error;
8879 }
James Smart5e5b5112019-01-28 11:14:22 -08008880 /* Prepare hardware queues to take IO buffers */
8881 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
8882 qp = &phba->sli4_hba.hdwq[idx];
8883 spin_lock_init(&qp->io_buf_list_get_lock);
8884 spin_lock_init(&qp->io_buf_list_put_lock);
8885 INIT_LIST_HEAD(&qp->lpfc_io_buf_list_get);
8886 INIT_LIST_HEAD(&qp->lpfc_io_buf_list_put);
8887 qp->get_io_bufs = 0;
8888 qp->put_io_bufs = 0;
8889 qp->total_io_bufs = 0;
James Smartc00f62e2019-08-14 16:57:11 -07008890 spin_lock_init(&qp->abts_io_buf_list_lock);
8891 INIT_LIST_HEAD(&qp->lpfc_abts_io_buf_list);
James Smart5e5b5112019-01-28 11:14:22 -08008892 qp->abts_scsi_io_bufs = 0;
James Smart5e5b5112019-01-28 11:14:22 -08008893 qp->abts_nvme_io_bufs = 0;
James Smartd79c9e92019-08-14 16:57:09 -07008894 INIT_LIST_HEAD(&qp->sgl_list);
8895 INIT_LIST_HEAD(&qp->cmd_rsp_buf_list);
8896 spin_lock_init(&qp->hdwq_lock);
James Smart895427b2017-02-12 13:52:30 -08008897 }
James Smart67d12732012-08-03 12:36:13 -04008898 }
8899
James Smartcdb42be2019-01-28 11:14:21 -08008900 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
James Smart2d7dbc42017-02-12 13:52:35 -08008901 if (phba->nvmet_support) {
8902 phba->sli4_hba.nvmet_cqset = kcalloc(
8903 phba->cfg_nvmet_mrq,
8904 sizeof(struct lpfc_queue *),
8905 GFP_KERNEL);
8906 if (!phba->sli4_hba.nvmet_cqset) {
8907 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8908 "3121 Fail allocate memory for "
8909 "fast-path CQ set array\n");
8910 goto out_error;
8911 }
8912 phba->sli4_hba.nvmet_mrq_hdr = kcalloc(
8913 phba->cfg_nvmet_mrq,
8914 sizeof(struct lpfc_queue *),
8915 GFP_KERNEL);
8916 if (!phba->sli4_hba.nvmet_mrq_hdr) {
8917 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8918 "3122 Fail allocate memory for "
8919 "fast-path RQ set hdr array\n");
8920 goto out_error;
8921 }
8922 phba->sli4_hba.nvmet_mrq_data = kcalloc(
8923 phba->cfg_nvmet_mrq,
8924 sizeof(struct lpfc_queue *),
8925 GFP_KERNEL);
8926 if (!phba->sli4_hba.nvmet_mrq_data) {
8927 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8928 "3124 Fail allocate memory for "
8929 "fast-path RQ set data array\n");
8930 goto out_error;
8931 }
8932 }
James Smart67d12732012-08-03 12:36:13 -04008933 }
James Smartda0436e2009-05-22 14:51:39 -04008934
James Smart895427b2017-02-12 13:52:30 -08008935 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_wq_list);
James Smart67d12732012-08-03 12:36:13 -04008936
James Smart895427b2017-02-12 13:52:30 -08008937 /* Create HBA Event Queues (EQs) */
James Smart657add42019-05-21 17:49:06 -07008938 for_each_present_cpu(cpu) {
8939 /* We only want to create 1 EQ per vector, even though
8940 * multiple CPUs might be using that vector. so only
8941 * selects the CPUs that are LPFC_CPU_FIRST_IRQ.
James Smart6a828b02019-01-28 11:14:31 -08008942 */
James Smart657add42019-05-21 17:49:06 -07008943 cpup = &phba->sli4_hba.cpu_map[cpu];
8944 if (!(cpup->flag & LPFC_CPU_FIRST_IRQ))
James Smart6a828b02019-01-28 11:14:31 -08008945 continue;
James Smart657add42019-05-21 17:49:06 -07008946
8947 /* Get a ptr to the Hardware Queue associated with this CPU */
8948 qp = &phba->sli4_hba.hdwq[cpup->hdwq];
8949
8950 /* Allocate an EQ */
James Smart81b96ed2017-11-20 16:00:29 -08008951 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
8952 phba->sli4_hba.eq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -07008953 phba->sli4_hba.eq_ecount, cpu);
James Smartda0436e2009-05-22 14:51:39 -04008954 if (!qdesc) {
8955 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart657add42019-05-21 17:49:06 -07008956 "0497 Failed allocate EQ (%d)\n",
8957 cpup->hdwq);
James Smart67d12732012-08-03 12:36:13 -04008958 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04008959 }
James Smart7365f6f2018-02-22 08:18:46 -08008960 qdesc->qe_valid = 1;
James Smart657add42019-05-21 17:49:06 -07008961 qdesc->hdwq = cpup->hdwq;
James Smart3ad348d2019-08-14 16:56:43 -07008962 qdesc->chann = cpu; /* First CPU this EQ is affinitized to */
James Smart32517fc2019-01-28 11:14:33 -08008963 qdesc->last_cpu = qdesc->chann;
James Smart657add42019-05-21 17:49:06 -07008964
8965 /* Save the allocated EQ in the Hardware Queue */
8966 qp->hba_eq = qdesc;
8967
James Smart32517fc2019-01-28 11:14:33 -08008968 eqi = per_cpu_ptr(phba->sli4_hba.eq_info, qdesc->last_cpu);
8969 list_add(&qdesc->cpu_list, &eqi->list);
James Smartda0436e2009-05-22 14:51:39 -04008970 }
8971
James Smart657add42019-05-21 17:49:06 -07008972 /* Now we need to populate the other Hardware Queues, that share
8973 * an IRQ vector, with the associated EQ ptr.
8974 */
8975 for_each_present_cpu(cpu) {
8976 cpup = &phba->sli4_hba.cpu_map[cpu];
8977
8978 /* Check for EQ already allocated in previous loop */
8979 if (cpup->flag & LPFC_CPU_FIRST_IRQ)
8980 continue;
8981
8982 /* Check for multiple CPUs per hdwq */
8983 qp = &phba->sli4_hba.hdwq[cpup->hdwq];
8984 if (qp->hba_eq)
8985 continue;
8986
8987 /* We need to share an EQ for this hdwq */
8988 eqcpu = lpfc_find_cpu_handle(phba, cpup->eq, LPFC_FIND_BY_EQ);
8989 eqcpup = &phba->sli4_hba.cpu_map[eqcpu];
8990 qp->hba_eq = phba->sli4_hba.hdwq[eqcpup->hdwq].hba_eq;
8991 }
James Smart895427b2017-02-12 13:52:30 -08008992
James Smartc00f62e2019-08-14 16:57:11 -07008993 /* Allocate IO Path SLI4 CQ/WQs */
James Smart6a828b02019-01-28 11:14:31 -08008994 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
James Smartc00f62e2019-08-14 16:57:11 -07008995 if (lpfc_alloc_io_wq_cq(phba, idx))
James Smart895427b2017-02-12 13:52:30 -08008996 goto out_error;
James Smart6a828b02019-01-28 11:14:31 -08008997 }
James Smart895427b2017-02-12 13:52:30 -08008998
James Smartc00f62e2019-08-14 16:57:11 -07008999 if (phba->nvmet_support) {
9000 for (idx = 0; idx < phba->cfg_nvmet_mrq; idx++) {
9001 cpu = lpfc_find_cpu_handle(phba, idx,
9002 LPFC_FIND_BY_HDWQ);
9003 qdesc = lpfc_sli4_queue_alloc(phba,
James Smart81b96ed2017-11-20 16:00:29 -08009004 LPFC_DEFAULT_PAGE_SIZE,
9005 phba->sli4_hba.cq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -07009006 phba->sli4_hba.cq_ecount,
9007 cpu);
James Smartc00f62e2019-08-14 16:57:11 -07009008 if (!qdesc) {
9009 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartcdb42be2019-01-28 11:14:21 -08009010 "3142 Failed allocate NVME "
9011 "CQ Set (%d)\n", idx);
James Smartc00f62e2019-08-14 16:57:11 -07009012 goto out_error;
James Smart2d7dbc42017-02-12 13:52:35 -08009013 }
James Smartc00f62e2019-08-14 16:57:11 -07009014 qdesc->qe_valid = 1;
9015 qdesc->hdwq = idx;
9016 qdesc->chann = cpu;
9017 phba->sli4_hba.nvmet_cqset[idx] = qdesc;
James Smart2d7dbc42017-02-12 13:52:35 -08009018 }
9019 }
9020
James Smartda0436e2009-05-22 14:51:39 -04009021 /*
James Smart67d12732012-08-03 12:36:13 -04009022 * Create Slow Path Completion Queues (CQs)
James Smartda0436e2009-05-22 14:51:39 -04009023 */
9024
James Smartc1a21eb2019-03-12 16:30:29 -07009025 cpu = lpfc_find_cpu_handle(phba, 0, LPFC_FIND_BY_EQ);
James Smartda0436e2009-05-22 14:51:39 -04009026 /* Create slow-path Mailbox Command Complete Queue */
James Smart81b96ed2017-11-20 16:00:29 -08009027 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
9028 phba->sli4_hba.cq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -07009029 phba->sli4_hba.cq_ecount, cpu);
James Smartda0436e2009-05-22 14:51:39 -04009030 if (!qdesc) {
9031 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9032 "0500 Failed allocate slow-path mailbox CQ\n");
James Smart67d12732012-08-03 12:36:13 -04009033 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04009034 }
James Smart7365f6f2018-02-22 08:18:46 -08009035 qdesc->qe_valid = 1;
James Smartda0436e2009-05-22 14:51:39 -04009036 phba->sli4_hba.mbx_cq = qdesc;
9037
9038 /* Create slow-path ELS Complete Queue */
James Smart81b96ed2017-11-20 16:00:29 -08009039 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
9040 phba->sli4_hba.cq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -07009041 phba->sli4_hba.cq_ecount, cpu);
James Smartda0436e2009-05-22 14:51:39 -04009042 if (!qdesc) {
9043 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9044 "0501 Failed allocate slow-path ELS CQ\n");
James Smart67d12732012-08-03 12:36:13 -04009045 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04009046 }
James Smart7365f6f2018-02-22 08:18:46 -08009047 qdesc->qe_valid = 1;
James Smartc00f62e2019-08-14 16:57:11 -07009048 qdesc->chann = cpu;
James Smartda0436e2009-05-22 14:51:39 -04009049 phba->sli4_hba.els_cq = qdesc;
9050
James Smartda0436e2009-05-22 14:51:39 -04009051
James Smart5350d872011-10-10 21:33:49 -04009052 /*
James Smart67d12732012-08-03 12:36:13 -04009053 * Create Slow Path Work Queues (WQs)
James Smart5350d872011-10-10 21:33:49 -04009054 */
James Smartda0436e2009-05-22 14:51:39 -04009055
9056 /* Create Mailbox Command Queue */
James Smartda0436e2009-05-22 14:51:39 -04009057
James Smart81b96ed2017-11-20 16:00:29 -08009058 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
9059 phba->sli4_hba.mq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -07009060 phba->sli4_hba.mq_ecount, cpu);
James Smartda0436e2009-05-22 14:51:39 -04009061 if (!qdesc) {
9062 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9063 "0505 Failed allocate slow-path MQ\n");
James Smart67d12732012-08-03 12:36:13 -04009064 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04009065 }
James Smartc00f62e2019-08-14 16:57:11 -07009066 qdesc->chann = cpu;
James Smartda0436e2009-05-22 14:51:39 -04009067 phba->sli4_hba.mbx_wq = qdesc;
9068
9069 /*
James Smart67d12732012-08-03 12:36:13 -04009070 * Create ELS Work Queues
James Smartda0436e2009-05-22 14:51:39 -04009071 */
James Smartda0436e2009-05-22 14:51:39 -04009072
9073 /* Create slow-path ELS Work Queue */
James Smart81b96ed2017-11-20 16:00:29 -08009074 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
9075 phba->sli4_hba.wq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -07009076 phba->sli4_hba.wq_ecount, cpu);
James Smartda0436e2009-05-22 14:51:39 -04009077 if (!qdesc) {
9078 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9079 "0504 Failed allocate slow-path ELS WQ\n");
James Smart67d12732012-08-03 12:36:13 -04009080 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04009081 }
James Smartc00f62e2019-08-14 16:57:11 -07009082 qdesc->chann = cpu;
James Smartda0436e2009-05-22 14:51:39 -04009083 phba->sli4_hba.els_wq = qdesc;
James Smart895427b2017-02-12 13:52:30 -08009084 list_add_tail(&qdesc->wq_list, &phba->sli4_hba.lpfc_wq_list);
9085
9086 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
9087 /* Create NVME LS Complete Queue */
James Smart81b96ed2017-11-20 16:00:29 -08009088 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
9089 phba->sli4_hba.cq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -07009090 phba->sli4_hba.cq_ecount, cpu);
James Smart895427b2017-02-12 13:52:30 -08009091 if (!qdesc) {
9092 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9093 "6079 Failed allocate NVME LS CQ\n");
9094 goto out_error;
9095 }
James Smartc00f62e2019-08-14 16:57:11 -07009096 qdesc->chann = cpu;
James Smart7365f6f2018-02-22 08:18:46 -08009097 qdesc->qe_valid = 1;
James Smart895427b2017-02-12 13:52:30 -08009098 phba->sli4_hba.nvmels_cq = qdesc;
9099
9100 /* Create NVME LS Work Queue */
James Smart81b96ed2017-11-20 16:00:29 -08009101 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
9102 phba->sli4_hba.wq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -07009103 phba->sli4_hba.wq_ecount, cpu);
James Smart895427b2017-02-12 13:52:30 -08009104 if (!qdesc) {
9105 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9106 "6080 Failed allocate NVME LS WQ\n");
9107 goto out_error;
9108 }
James Smartc00f62e2019-08-14 16:57:11 -07009109 qdesc->chann = cpu;
James Smart895427b2017-02-12 13:52:30 -08009110 phba->sli4_hba.nvmels_wq = qdesc;
9111 list_add_tail(&qdesc->wq_list, &phba->sli4_hba.lpfc_wq_list);
9112 }
James Smartda0436e2009-05-22 14:51:39 -04009113
James Smartda0436e2009-05-22 14:51:39 -04009114 /*
9115 * Create Receive Queue (RQ)
9116 */
James Smartda0436e2009-05-22 14:51:39 -04009117
9118 /* Create Receive Queue for header */
James Smart81b96ed2017-11-20 16:00:29 -08009119 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
9120 phba->sli4_hba.rq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -07009121 phba->sli4_hba.rq_ecount, cpu);
James Smartda0436e2009-05-22 14:51:39 -04009122 if (!qdesc) {
9123 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9124 "0506 Failed allocate receive HRQ\n");
James Smart67d12732012-08-03 12:36:13 -04009125 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04009126 }
9127 phba->sli4_hba.hdr_rq = qdesc;
9128
9129 /* Create Receive Queue for data */
James Smart81b96ed2017-11-20 16:00:29 -08009130 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
9131 phba->sli4_hba.rq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -07009132 phba->sli4_hba.rq_ecount, cpu);
James Smartda0436e2009-05-22 14:51:39 -04009133 if (!qdesc) {
9134 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9135 "0507 Failed allocate receive DRQ\n");
James Smart67d12732012-08-03 12:36:13 -04009136 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04009137 }
9138 phba->sli4_hba.dat_rq = qdesc;
9139
James Smartcdb42be2019-01-28 11:14:21 -08009140 if ((phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) &&
9141 phba->nvmet_support) {
James Smart2d7dbc42017-02-12 13:52:35 -08009142 for (idx = 0; idx < phba->cfg_nvmet_mrq; idx++) {
James Smartc1a21eb2019-03-12 16:30:29 -07009143 cpu = lpfc_find_cpu_handle(phba, idx,
9144 LPFC_FIND_BY_HDWQ);
James Smart2d7dbc42017-02-12 13:52:35 -08009145 /* Create NVMET Receive Queue for header */
9146 qdesc = lpfc_sli4_queue_alloc(phba,
James Smart81b96ed2017-11-20 16:00:29 -08009147 LPFC_DEFAULT_PAGE_SIZE,
James Smart2d7dbc42017-02-12 13:52:35 -08009148 phba->sli4_hba.rq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -07009149 LPFC_NVMET_RQE_DEF_COUNT,
9150 cpu);
James Smart2d7dbc42017-02-12 13:52:35 -08009151 if (!qdesc) {
9152 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9153 "3146 Failed allocate "
9154 "receive HRQ\n");
9155 goto out_error;
9156 }
James Smart5e5b5112019-01-28 11:14:22 -08009157 qdesc->hdwq = idx;
James Smart2d7dbc42017-02-12 13:52:35 -08009158 phba->sli4_hba.nvmet_mrq_hdr[idx] = qdesc;
9159
9160 /* Only needed for header of RQ pair */
James Smartc1a21eb2019-03-12 16:30:29 -07009161 qdesc->rqbp = kzalloc_node(sizeof(*qdesc->rqbp),
9162 GFP_KERNEL,
9163 cpu_to_node(cpu));
James Smart2d7dbc42017-02-12 13:52:35 -08009164 if (qdesc->rqbp == NULL) {
9165 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9166 "6131 Failed allocate "
9167 "Header RQBP\n");
9168 goto out_error;
9169 }
9170
Dick Kennedy4b40d022017-08-23 16:55:38 -07009171 /* Put list in known state in case driver load fails. */
9172 INIT_LIST_HEAD(&qdesc->rqbp->rqb_buffer_list);
9173
James Smart2d7dbc42017-02-12 13:52:35 -08009174 /* Create NVMET Receive Queue for data */
9175 qdesc = lpfc_sli4_queue_alloc(phba,
James Smart81b96ed2017-11-20 16:00:29 -08009176 LPFC_DEFAULT_PAGE_SIZE,
James Smart2d7dbc42017-02-12 13:52:35 -08009177 phba->sli4_hba.rq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -07009178 LPFC_NVMET_RQE_DEF_COUNT,
9179 cpu);
James Smart2d7dbc42017-02-12 13:52:35 -08009180 if (!qdesc) {
9181 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9182 "3156 Failed allocate "
9183 "receive DRQ\n");
9184 goto out_error;
9185 }
James Smart5e5b5112019-01-28 11:14:22 -08009186 qdesc->hdwq = idx;
James Smart2d7dbc42017-02-12 13:52:35 -08009187 phba->sli4_hba.nvmet_mrq_data[idx] = qdesc;
9188 }
9189 }
9190
James Smart4c47efc2019-01-28 11:14:25 -08009191#if defined(BUILD_NVME)
9192 /* Clear NVME stats */
9193 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
9194 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
9195 memset(&phba->sli4_hba.hdwq[idx].nvme_cstat, 0,
9196 sizeof(phba->sli4_hba.hdwq[idx].nvme_cstat));
9197 }
9198 }
9199#endif
9200
9201 /* Clear SCSI stats */
9202 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) {
9203 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
9204 memset(&phba->sli4_hba.hdwq[idx].scsi_cstat, 0,
9205 sizeof(phba->sli4_hba.hdwq[idx].scsi_cstat));
9206 }
9207 }
9208
James Smartda0436e2009-05-22 14:51:39 -04009209 return 0;
9210
James Smartda0436e2009-05-22 14:51:39 -04009211out_error:
James Smart67d12732012-08-03 12:36:13 -04009212 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04009213 return -ENOMEM;
9214}
9215
James Smart895427b2017-02-12 13:52:30 -08009216static inline void
9217__lpfc_sli4_release_queue(struct lpfc_queue **qp)
9218{
9219 if (*qp != NULL) {
9220 lpfc_sli4_queue_free(*qp);
9221 *qp = NULL;
9222 }
9223}
9224
9225static inline void
9226lpfc_sli4_release_queues(struct lpfc_queue ***qs, int max)
9227{
9228 int idx;
9229
9230 if (*qs == NULL)
9231 return;
9232
9233 for (idx = 0; idx < max; idx++)
9234 __lpfc_sli4_release_queue(&(*qs)[idx]);
9235
9236 kfree(*qs);
9237 *qs = NULL;
9238}
9239
9240static inline void
James Smart6a828b02019-01-28 11:14:31 -08009241lpfc_sli4_release_hdwq(struct lpfc_hba *phba)
James Smart895427b2017-02-12 13:52:30 -08009242{
James Smart6a828b02019-01-28 11:14:31 -08009243 struct lpfc_sli4_hdw_queue *hdwq;
James Smart657add42019-05-21 17:49:06 -07009244 struct lpfc_queue *eq;
James Smartcdb42be2019-01-28 11:14:21 -08009245 uint32_t idx;
9246
James Smart6a828b02019-01-28 11:14:31 -08009247 hdwq = phba->sli4_hba.hdwq;
James Smart6a828b02019-01-28 11:14:31 -08009248
James Smart657add42019-05-21 17:49:06 -07009249 /* Loop thru all Hardware Queues */
9250 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
9251 /* Free the CQ/WQ corresponding to the Hardware Queue */
James Smartc00f62e2019-08-14 16:57:11 -07009252 lpfc_sli4_queue_free(hdwq[idx].io_cq);
9253 lpfc_sli4_queue_free(hdwq[idx].io_wq);
9254 hdwq[idx].io_cq = NULL;
9255 hdwq[idx].io_wq = NULL;
James Smartd79c9e92019-08-14 16:57:09 -07009256 if (phba->cfg_xpsgl && !phba->nvmet_support)
9257 lpfc_free_sgl_per_hdwq(phba, &hdwq[idx]);
9258 lpfc_free_cmd_rsp_buf_per_hdwq(phba, &hdwq[idx]);
James Smart895427b2017-02-12 13:52:30 -08009259 }
James Smart657add42019-05-21 17:49:06 -07009260 /* Loop thru all IRQ vectors */
9261 for (idx = 0; idx < phba->cfg_irq_chann; idx++) {
9262 /* Free the EQ corresponding to the IRQ vector */
9263 eq = phba->sli4_hba.hba_eq_hdl[idx].eq;
9264 lpfc_sli4_queue_free(eq);
9265 phba->sli4_hba.hba_eq_hdl[idx].eq = NULL;
9266 }
James Smart895427b2017-02-12 13:52:30 -08009267}
9268
James Smartda0436e2009-05-22 14:51:39 -04009269/**
9270 * lpfc_sli4_queue_destroy - Destroy all the SLI4 queues
9271 * @phba: pointer to lpfc hba data structure.
9272 *
9273 * This routine is invoked to release all the SLI4 queues with the FCoE HBA
9274 * operation.
9275 *
9276 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02009277 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009278 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04009279 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04009280 **/
James Smart5350d872011-10-10 21:33:49 -04009281void
James Smartda0436e2009-05-22 14:51:39 -04009282lpfc_sli4_queue_destroy(struct lpfc_hba *phba)
9283{
James Smart4645f7b2019-03-12 16:30:14 -07009284 /*
9285 * Set FREE_INIT before beginning to free the queues.
9286 * Wait until the users of queues to acknowledge to
9287 * release queues by clearing FREE_WAIT.
9288 */
9289 spin_lock_irq(&phba->hbalock);
9290 phba->sli.sli_flag |= LPFC_QUEUE_FREE_INIT;
9291 while (phba->sli.sli_flag & LPFC_QUEUE_FREE_WAIT) {
9292 spin_unlock_irq(&phba->hbalock);
9293 msleep(20);
9294 spin_lock_irq(&phba->hbalock);
9295 }
9296 spin_unlock_irq(&phba->hbalock);
9297
James Smart93a4d6f2019-11-04 16:57:05 -08009298 lpfc_sli4_cleanup_poll_list(phba);
9299
James Smart895427b2017-02-12 13:52:30 -08009300 /* Release HBA eqs */
James Smartcdb42be2019-01-28 11:14:21 -08009301 if (phba->sli4_hba.hdwq)
James Smart6a828b02019-01-28 11:14:31 -08009302 lpfc_sli4_release_hdwq(phba);
James Smartda0436e2009-05-22 14:51:39 -04009303
James Smartbcb24f62017-11-20 16:00:36 -08009304 if (phba->nvmet_support) {
9305 lpfc_sli4_release_queues(&phba->sli4_hba.nvmet_cqset,
9306 phba->cfg_nvmet_mrq);
James Smart2d7dbc42017-02-12 13:52:35 -08009307
James Smartbcb24f62017-11-20 16:00:36 -08009308 lpfc_sli4_release_queues(&phba->sli4_hba.nvmet_mrq_hdr,
9309 phba->cfg_nvmet_mrq);
9310 lpfc_sli4_release_queues(&phba->sli4_hba.nvmet_mrq_data,
9311 phba->cfg_nvmet_mrq);
9312 }
James Smart2d7dbc42017-02-12 13:52:35 -08009313
James Smartda0436e2009-05-22 14:51:39 -04009314 /* Release mailbox command work queue */
James Smart895427b2017-02-12 13:52:30 -08009315 __lpfc_sli4_release_queue(&phba->sli4_hba.mbx_wq);
James Smartda0436e2009-05-22 14:51:39 -04009316
9317 /* Release ELS work queue */
James Smart895427b2017-02-12 13:52:30 -08009318 __lpfc_sli4_release_queue(&phba->sli4_hba.els_wq);
9319
9320 /* Release ELS work queue */
9321 __lpfc_sli4_release_queue(&phba->sli4_hba.nvmels_wq);
James Smartda0436e2009-05-22 14:51:39 -04009322
9323 /* Release unsolicited receive queue */
James Smart895427b2017-02-12 13:52:30 -08009324 __lpfc_sli4_release_queue(&phba->sli4_hba.hdr_rq);
9325 __lpfc_sli4_release_queue(&phba->sli4_hba.dat_rq);
James Smartda0436e2009-05-22 14:51:39 -04009326
James Smartda0436e2009-05-22 14:51:39 -04009327 /* Release ELS complete queue */
James Smart895427b2017-02-12 13:52:30 -08009328 __lpfc_sli4_release_queue(&phba->sli4_hba.els_cq);
9329
9330 /* Release NVME LS complete queue */
9331 __lpfc_sli4_release_queue(&phba->sli4_hba.nvmels_cq);
James Smartda0436e2009-05-22 14:51:39 -04009332
9333 /* Release mailbox command complete queue */
James Smart895427b2017-02-12 13:52:30 -08009334 __lpfc_sli4_release_queue(&phba->sli4_hba.mbx_cq);
9335
9336 /* Everything on this list has been freed */
9337 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_wq_list);
James Smart4645f7b2019-03-12 16:30:14 -07009338
9339 /* Done with freeing the queues */
9340 spin_lock_irq(&phba->hbalock);
9341 phba->sli.sli_flag &= ~LPFC_QUEUE_FREE_INIT;
9342 spin_unlock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -08009343}
9344
9345int
James Smart895427b2017-02-12 13:52:30 -08009346lpfc_free_rq_buffer(struct lpfc_hba *phba, struct lpfc_queue *rq)
9347{
9348 struct lpfc_rqb *rqbp;
9349 struct lpfc_dmabuf *h_buf;
9350 struct rqb_dmabuf *rqb_buffer;
9351
9352 rqbp = rq->rqbp;
9353 while (!list_empty(&rqbp->rqb_buffer_list)) {
9354 list_remove_head(&rqbp->rqb_buffer_list, h_buf,
9355 struct lpfc_dmabuf, list);
9356
9357 rqb_buffer = container_of(h_buf, struct rqb_dmabuf, hbuf);
9358 (rqbp->rqb_free_buffer)(phba, rqb_buffer);
9359 rqbp->buffer_count--;
9360 }
9361 return 1;
9362}
9363
9364static int
9365lpfc_create_wq_cq(struct lpfc_hba *phba, struct lpfc_queue *eq,
9366 struct lpfc_queue *cq, struct lpfc_queue *wq, uint16_t *cq_map,
9367 int qidx, uint32_t qtype)
9368{
9369 struct lpfc_sli_ring *pring;
9370 int rc;
9371
9372 if (!eq || !cq || !wq) {
9373 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9374 "6085 Fast-path %s (%d) not allocated\n",
9375 ((eq) ? ((cq) ? "WQ" : "CQ") : "EQ"), qidx);
9376 return -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04009377 }
James Smartda0436e2009-05-22 14:51:39 -04009378
James Smart895427b2017-02-12 13:52:30 -08009379 /* create the Cq first */
9380 rc = lpfc_cq_create(phba, cq, eq,
9381 (qtype == LPFC_MBOX) ? LPFC_MCQ : LPFC_WCQ, qtype);
9382 if (rc) {
9383 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9384 "6086 Failed setup of CQ (%d), rc = 0x%x\n",
9385 qidx, (uint32_t)rc);
9386 return rc;
9387 }
9388
9389 if (qtype != LPFC_MBOX) {
James Smartcdb42be2019-01-28 11:14:21 -08009390 /* Setup cq_map for fast lookup */
James Smart895427b2017-02-12 13:52:30 -08009391 if (cq_map)
9392 *cq_map = cq->queue_id;
9393
9394 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9395 "6087 CQ setup: cq[%d]-id=%d, parent eq[%d]-id=%d\n",
9396 qidx, cq->queue_id, qidx, eq->queue_id);
9397
9398 /* create the wq */
9399 rc = lpfc_wq_create(phba, wq, cq, qtype);
9400 if (rc) {
9401 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartc835c082019-03-12 16:30:30 -07009402 "4618 Fail setup fastpath WQ (%d), rc = 0x%x\n",
James Smart895427b2017-02-12 13:52:30 -08009403 qidx, (uint32_t)rc);
9404 /* no need to tear down cq - caller will do so */
9405 return rc;
9406 }
9407
9408 /* Bind this CQ/WQ to the NVME ring */
9409 pring = wq->pring;
9410 pring->sli.sli4.wqp = (void *)wq;
9411 cq->pring = pring;
9412
9413 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9414 "2593 WQ setup: wq[%d]-id=%d assoc=%d, cq[%d]-id=%d\n",
9415 qidx, wq->queue_id, wq->assoc_qid, qidx, cq->queue_id);
9416 } else {
9417 rc = lpfc_mq_create(phba, wq, cq, LPFC_MBOX);
9418 if (rc) {
9419 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9420 "0539 Failed setup of slow-path MQ: "
9421 "rc = 0x%x\n", rc);
9422 /* no need to tear down cq - caller will do so */
9423 return rc;
9424 }
9425
9426 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9427 "2589 MBX MQ setup: wq-id=%d, parent cq-id=%d\n",
9428 phba->sli4_hba.mbx_wq->queue_id,
9429 phba->sli4_hba.mbx_cq->queue_id);
9430 }
9431
9432 return 0;
James Smartda0436e2009-05-22 14:51:39 -04009433}
9434
9435/**
James Smart6a828b02019-01-28 11:14:31 -08009436 * lpfc_setup_cq_lookup - Setup the CQ lookup table
9437 * @phba: pointer to lpfc hba data structure.
9438 *
9439 * This routine will populate the cq_lookup table by all
9440 * available CQ queue_id's.
9441 **/
Bart Van Assche3999df72019-03-28 11:06:16 -07009442static void
James Smart6a828b02019-01-28 11:14:31 -08009443lpfc_setup_cq_lookup(struct lpfc_hba *phba)
9444{
9445 struct lpfc_queue *eq, *childq;
James Smart6a828b02019-01-28 11:14:31 -08009446 int qidx;
9447
James Smart6a828b02019-01-28 11:14:31 -08009448 memset(phba->sli4_hba.cq_lookup, 0,
9449 (sizeof(struct lpfc_queue *) * (phba->sli4_hba.cq_max + 1)));
James Smart657add42019-05-21 17:49:06 -07009450 /* Loop thru all IRQ vectors */
James Smart6a828b02019-01-28 11:14:31 -08009451 for (qidx = 0; qidx < phba->cfg_irq_chann; qidx++) {
James Smart657add42019-05-21 17:49:06 -07009452 /* Get the EQ corresponding to the IRQ vector */
9453 eq = phba->sli4_hba.hba_eq_hdl[qidx].eq;
James Smart6a828b02019-01-28 11:14:31 -08009454 if (!eq)
9455 continue;
James Smart657add42019-05-21 17:49:06 -07009456 /* Loop through all CQs associated with that EQ */
James Smart6a828b02019-01-28 11:14:31 -08009457 list_for_each_entry(childq, &eq->child_list, list) {
9458 if (childq->queue_id > phba->sli4_hba.cq_max)
9459 continue;
James Smartc00f62e2019-08-14 16:57:11 -07009460 if (childq->subtype == LPFC_IO)
James Smart6a828b02019-01-28 11:14:31 -08009461 phba->sli4_hba.cq_lookup[childq->queue_id] =
9462 childq;
9463 }
9464 }
9465}
9466
9467/**
James Smartda0436e2009-05-22 14:51:39 -04009468 * lpfc_sli4_queue_setup - Set up all the SLI4 queues
9469 * @phba: pointer to lpfc hba data structure.
9470 *
9471 * This routine is invoked to set up all the SLI4 queues for the FCoE HBA
9472 * operation.
9473 *
9474 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02009475 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009476 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04009477 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04009478 **/
9479int
9480lpfc_sli4_queue_setup(struct lpfc_hba *phba)
9481{
James Smart962bc512013-01-03 15:44:00 -05009482 uint32_t shdr_status, shdr_add_status;
9483 union lpfc_sli4_cfg_shdr *shdr;
James Smart657add42019-05-21 17:49:06 -07009484 struct lpfc_vector_map_info *cpup;
James Smartcdb42be2019-01-28 11:14:21 -08009485 struct lpfc_sli4_hdw_queue *qp;
James Smart962bc512013-01-03 15:44:00 -05009486 LPFC_MBOXQ_t *mboxq;
James Smart657add42019-05-21 17:49:06 -07009487 int qidx, cpu;
James Smartcb733e32019-01-28 11:14:32 -08009488 uint32_t length, usdelay;
James Smart895427b2017-02-12 13:52:30 -08009489 int rc = -ENOMEM;
James Smart962bc512013-01-03 15:44:00 -05009490
9491 /* Check for dual-ULP support */
9492 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9493 if (!mboxq) {
9494 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9495 "3249 Unable to allocate memory for "
9496 "QUERY_FW_CFG mailbox command\n");
9497 return -ENOMEM;
9498 }
9499 length = (sizeof(struct lpfc_mbx_query_fw_config) -
9500 sizeof(struct lpfc_sli4_cfg_mhdr));
9501 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
9502 LPFC_MBOX_OPCODE_QUERY_FW_CFG,
9503 length, LPFC_SLI4_MBX_EMBED);
9504
9505 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
9506
9507 shdr = (union lpfc_sli4_cfg_shdr *)
9508 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
9509 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9510 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9511 if (shdr_status || shdr_add_status || rc) {
9512 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9513 "3250 QUERY_FW_CFG mailbox failed with status "
9514 "x%x add_status x%x, mbx status x%x\n",
9515 shdr_status, shdr_add_status, rc);
9516 if (rc != MBX_TIMEOUT)
9517 mempool_free(mboxq, phba->mbox_mem_pool);
9518 rc = -ENXIO;
9519 goto out_error;
9520 }
9521
9522 phba->sli4_hba.fw_func_mode =
9523 mboxq->u.mqe.un.query_fw_cfg.rsp.function_mode;
9524 phba->sli4_hba.ulp0_mode = mboxq->u.mqe.un.query_fw_cfg.rsp.ulp0_mode;
9525 phba->sli4_hba.ulp1_mode = mboxq->u.mqe.un.query_fw_cfg.rsp.ulp1_mode;
James Smart8b017a32015-05-21 13:55:18 -04009526 phba->sli4_hba.physical_port =
9527 mboxq->u.mqe.un.query_fw_cfg.rsp.physical_port;
James Smart962bc512013-01-03 15:44:00 -05009528 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9529 "3251 QUERY_FW_CFG: func_mode:x%x, ulp0_mode:x%x, "
9530 "ulp1_mode:x%x\n", phba->sli4_hba.fw_func_mode,
9531 phba->sli4_hba.ulp0_mode, phba->sli4_hba.ulp1_mode);
9532
9533 if (rc != MBX_TIMEOUT)
9534 mempool_free(mboxq, phba->mbox_mem_pool);
James Smartda0436e2009-05-22 14:51:39 -04009535
9536 /*
James Smart67d12732012-08-03 12:36:13 -04009537 * Set up HBA Event Queues (EQs)
James Smartda0436e2009-05-22 14:51:39 -04009538 */
James Smartcdb42be2019-01-28 11:14:21 -08009539 qp = phba->sli4_hba.hdwq;
James Smartda0436e2009-05-22 14:51:39 -04009540
James Smart67d12732012-08-03 12:36:13 -04009541 /* Set up HBA event queue */
James Smartcdb42be2019-01-28 11:14:21 -08009542 if (!qp) {
James Smart2e90f4b2011-12-13 13:22:37 -05009543 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9544 "3147 Fast-path EQs not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05009545 rc = -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04009546 goto out_error;
James Smart2e90f4b2011-12-13 13:22:37 -05009547 }
James Smart657add42019-05-21 17:49:06 -07009548
9549 /* Loop thru all IRQ vectors */
James Smart6a828b02019-01-28 11:14:31 -08009550 for (qidx = 0; qidx < phba->cfg_irq_chann; qidx++) {
James Smart657add42019-05-21 17:49:06 -07009551 /* Create HBA Event Queues (EQs) in order */
9552 for_each_present_cpu(cpu) {
9553 cpup = &phba->sli4_hba.cpu_map[cpu];
9554
9555 /* Look for the CPU thats using that vector with
9556 * LPFC_CPU_FIRST_IRQ set.
9557 */
9558 if (!(cpup->flag & LPFC_CPU_FIRST_IRQ))
9559 continue;
9560 if (qidx != cpup->eq)
9561 continue;
9562
9563 /* Create an EQ for that vector */
9564 rc = lpfc_eq_create(phba, qp[cpup->hdwq].hba_eq,
9565 phba->cfg_fcp_imax);
9566 if (rc) {
9567 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9568 "0523 Failed setup of fast-path"
9569 " EQ (%d), rc = 0x%x\n",
9570 cpup->eq, (uint32_t)rc);
9571 goto out_destroy;
9572 }
9573
9574 /* Save the EQ for that vector in the hba_eq_hdl */
9575 phba->sli4_hba.hba_eq_hdl[cpup->eq].eq =
9576 qp[cpup->hdwq].hba_eq;
9577
9578 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9579 "2584 HBA EQ setup: queue[%d]-id=%d\n",
9580 cpup->eq,
9581 qp[cpup->hdwq].hba_eq->queue_id);
James Smartda0436e2009-05-22 14:51:39 -04009582 }
James Smartda0436e2009-05-22 14:51:39 -04009583 }
9584
James Smart657add42019-05-21 17:49:06 -07009585 /* Loop thru all Hardware Queues */
James Smartcdb42be2019-01-28 11:14:21 -08009586 for (qidx = 0; qidx < phba->cfg_hdw_queue; qidx++) {
James Smart657add42019-05-21 17:49:06 -07009587 cpu = lpfc_find_cpu_handle(phba, qidx, LPFC_FIND_BY_HDWQ);
9588 cpup = &phba->sli4_hba.cpu_map[cpu];
9589
9590 /* Create the CQ/WQ corresponding to the Hardware Queue */
James Smartcdb42be2019-01-28 11:14:21 -08009591 rc = lpfc_create_wq_cq(phba,
James Smart657add42019-05-21 17:49:06 -07009592 phba->sli4_hba.hdwq[cpup->hdwq].hba_eq,
James Smartc00f62e2019-08-14 16:57:11 -07009593 qp[qidx].io_cq,
9594 qp[qidx].io_wq,
9595 &phba->sli4_hba.hdwq[qidx].io_cq_map,
9596 qidx,
9597 LPFC_IO);
James Smartcdb42be2019-01-28 11:14:21 -08009598 if (rc) {
James Smart67d12732012-08-03 12:36:13 -04009599 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart895427b2017-02-12 13:52:30 -08009600 "0535 Failed to setup fastpath "
James Smartc00f62e2019-08-14 16:57:11 -07009601 "IO WQ/CQ (%d), rc = 0x%x\n",
James Smart895427b2017-02-12 13:52:30 -08009602 qidx, (uint32_t)rc);
James Smartcdb42be2019-01-28 11:14:21 -08009603 goto out_destroy;
James Smart895427b2017-02-12 13:52:30 -08009604 }
James Smart67d12732012-08-03 12:36:13 -04009605 }
James Smartda0436e2009-05-22 14:51:39 -04009606
9607 /*
James Smart895427b2017-02-12 13:52:30 -08009608 * Set up Slow Path Complete Queues (CQs)
James Smartda0436e2009-05-22 14:51:39 -04009609 */
9610
James Smart895427b2017-02-12 13:52:30 -08009611 /* Set up slow-path MBOX CQ/MQ */
9612
9613 if (!phba->sli4_hba.mbx_cq || !phba->sli4_hba.mbx_wq) {
James Smartda0436e2009-05-22 14:51:39 -04009614 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart895427b2017-02-12 13:52:30 -08009615 "0528 %s not allocated\n",
9616 phba->sli4_hba.mbx_cq ?
James Smartd1f525a2017-04-21 16:04:55 -07009617 "Mailbox WQ" : "Mailbox CQ");
James Smart1b511972011-12-13 13:23:09 -05009618 rc = -ENOMEM;
James Smart895427b2017-02-12 13:52:30 -08009619 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -04009620 }
James Smart895427b2017-02-12 13:52:30 -08009621
James Smartcdb42be2019-01-28 11:14:21 -08009622 rc = lpfc_create_wq_cq(phba, qp[0].hba_eq,
James Smartd1f525a2017-04-21 16:04:55 -07009623 phba->sli4_hba.mbx_cq,
9624 phba->sli4_hba.mbx_wq,
9625 NULL, 0, LPFC_MBOX);
James Smartda0436e2009-05-22 14:51:39 -04009626 if (rc) {
9627 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart895427b2017-02-12 13:52:30 -08009628 "0529 Failed setup of mailbox WQ/CQ: rc = 0x%x\n",
9629 (uint32_t)rc);
9630 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -04009631 }
James Smart2d7dbc42017-02-12 13:52:35 -08009632 if (phba->nvmet_support) {
9633 if (!phba->sli4_hba.nvmet_cqset) {
9634 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9635 "3165 Fast-path NVME CQ Set "
9636 "array not allocated\n");
9637 rc = -ENOMEM;
9638 goto out_destroy;
9639 }
9640 if (phba->cfg_nvmet_mrq > 1) {
9641 rc = lpfc_cq_create_set(phba,
9642 phba->sli4_hba.nvmet_cqset,
James Smartcdb42be2019-01-28 11:14:21 -08009643 qp,
James Smart2d7dbc42017-02-12 13:52:35 -08009644 LPFC_WCQ, LPFC_NVMET);
9645 if (rc) {
9646 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9647 "3164 Failed setup of NVME CQ "
9648 "Set, rc = 0x%x\n",
9649 (uint32_t)rc);
9650 goto out_destroy;
9651 }
9652 } else {
9653 /* Set up NVMET Receive Complete Queue */
9654 rc = lpfc_cq_create(phba, phba->sli4_hba.nvmet_cqset[0],
James Smartcdb42be2019-01-28 11:14:21 -08009655 qp[0].hba_eq,
James Smart2d7dbc42017-02-12 13:52:35 -08009656 LPFC_WCQ, LPFC_NVMET);
9657 if (rc) {
9658 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9659 "6089 Failed setup NVMET CQ: "
9660 "rc = 0x%x\n", (uint32_t)rc);
9661 goto out_destroy;
9662 }
James Smart81b96ed2017-11-20 16:00:29 -08009663 phba->sli4_hba.nvmet_cqset[0]->chann = 0;
9664
James Smart2d7dbc42017-02-12 13:52:35 -08009665 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9666 "6090 NVMET CQ setup: cq-id=%d, "
9667 "parent eq-id=%d\n",
9668 phba->sli4_hba.nvmet_cqset[0]->queue_id,
James Smartcdb42be2019-01-28 11:14:21 -08009669 qp[0].hba_eq->queue_id);
James Smart2d7dbc42017-02-12 13:52:35 -08009670 }
9671 }
James Smartda0436e2009-05-22 14:51:39 -04009672
James Smart895427b2017-02-12 13:52:30 -08009673 /* Set up slow-path ELS WQ/CQ */
9674 if (!phba->sli4_hba.els_cq || !phba->sli4_hba.els_wq) {
James Smartda0436e2009-05-22 14:51:39 -04009675 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart895427b2017-02-12 13:52:30 -08009676 "0530 ELS %s not allocated\n",
9677 phba->sli4_hba.els_cq ? "WQ" : "CQ");
James Smart1b511972011-12-13 13:23:09 -05009678 rc = -ENOMEM;
James Smart895427b2017-02-12 13:52:30 -08009679 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -04009680 }
James Smartcdb42be2019-01-28 11:14:21 -08009681 rc = lpfc_create_wq_cq(phba, qp[0].hba_eq,
9682 phba->sli4_hba.els_cq,
9683 phba->sli4_hba.els_wq,
9684 NULL, 0, LPFC_ELS);
James Smartda0436e2009-05-22 14:51:39 -04009685 if (rc) {
9686 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartcdb42be2019-01-28 11:14:21 -08009687 "0525 Failed setup of ELS WQ/CQ: rc = 0x%x\n",
9688 (uint32_t)rc);
James Smart895427b2017-02-12 13:52:30 -08009689 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -04009690 }
9691 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9692 "2590 ELS WQ setup: wq-id=%d, parent cq-id=%d\n",
9693 phba->sli4_hba.els_wq->queue_id,
9694 phba->sli4_hba.els_cq->queue_id);
9695
James Smartcdb42be2019-01-28 11:14:21 -08009696 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
James Smart895427b2017-02-12 13:52:30 -08009697 /* Set up NVME LS Complete Queue */
9698 if (!phba->sli4_hba.nvmels_cq || !phba->sli4_hba.nvmels_wq) {
9699 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9700 "6091 LS %s not allocated\n",
9701 phba->sli4_hba.nvmels_cq ? "WQ" : "CQ");
9702 rc = -ENOMEM;
9703 goto out_destroy;
9704 }
James Smartcdb42be2019-01-28 11:14:21 -08009705 rc = lpfc_create_wq_cq(phba, qp[0].hba_eq,
9706 phba->sli4_hba.nvmels_cq,
9707 phba->sli4_hba.nvmels_wq,
9708 NULL, 0, LPFC_NVME_LS);
James Smart895427b2017-02-12 13:52:30 -08009709 if (rc) {
9710 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartcdb42be2019-01-28 11:14:21 -08009711 "0526 Failed setup of NVVME LS WQ/CQ: "
9712 "rc = 0x%x\n", (uint32_t)rc);
James Smart895427b2017-02-12 13:52:30 -08009713 goto out_destroy;
9714 }
9715
9716 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9717 "6096 ELS WQ setup: wq-id=%d, "
9718 "parent cq-id=%d\n",
9719 phba->sli4_hba.nvmels_wq->queue_id,
9720 phba->sli4_hba.nvmels_cq->queue_id);
9721 }
9722
James Smart2d7dbc42017-02-12 13:52:35 -08009723 /*
9724 * Create NVMET Receive Queue (RQ)
9725 */
9726 if (phba->nvmet_support) {
9727 if ((!phba->sli4_hba.nvmet_cqset) ||
9728 (!phba->sli4_hba.nvmet_mrq_hdr) ||
9729 (!phba->sli4_hba.nvmet_mrq_data)) {
9730 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9731 "6130 MRQ CQ Queues not "
9732 "allocated\n");
9733 rc = -ENOMEM;
9734 goto out_destroy;
9735 }
9736 if (phba->cfg_nvmet_mrq > 1) {
9737 rc = lpfc_mrq_create(phba,
9738 phba->sli4_hba.nvmet_mrq_hdr,
9739 phba->sli4_hba.nvmet_mrq_data,
9740 phba->sli4_hba.nvmet_cqset,
9741 LPFC_NVMET);
9742 if (rc) {
9743 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9744 "6098 Failed setup of NVMET "
9745 "MRQ: rc = 0x%x\n",
9746 (uint32_t)rc);
9747 goto out_destroy;
9748 }
9749
9750 } else {
9751 rc = lpfc_rq_create(phba,
9752 phba->sli4_hba.nvmet_mrq_hdr[0],
9753 phba->sli4_hba.nvmet_mrq_data[0],
9754 phba->sli4_hba.nvmet_cqset[0],
9755 LPFC_NVMET);
9756 if (rc) {
9757 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9758 "6057 Failed setup of NVMET "
9759 "Receive Queue: rc = 0x%x\n",
9760 (uint32_t)rc);
9761 goto out_destroy;
9762 }
9763
9764 lpfc_printf_log(
9765 phba, KERN_INFO, LOG_INIT,
9766 "6099 NVMET RQ setup: hdr-rq-id=%d, "
9767 "dat-rq-id=%d parent cq-id=%d\n",
9768 phba->sli4_hba.nvmet_mrq_hdr[0]->queue_id,
9769 phba->sli4_hba.nvmet_mrq_data[0]->queue_id,
9770 phba->sli4_hba.nvmet_cqset[0]->queue_id);
9771
9772 }
9773 }
9774
James Smartda0436e2009-05-22 14:51:39 -04009775 if (!phba->sli4_hba.hdr_rq || !phba->sli4_hba.dat_rq) {
9776 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9777 "0540 Receive Queue not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05009778 rc = -ENOMEM;
James Smart895427b2017-02-12 13:52:30 -08009779 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -04009780 }
James Smart73d91e52011-10-10 21:32:10 -04009781
James Smartda0436e2009-05-22 14:51:39 -04009782 rc = lpfc_rq_create(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
James Smart4d9ab992009-10-02 15:16:39 -04009783 phba->sli4_hba.els_cq, LPFC_USOL);
James Smartda0436e2009-05-22 14:51:39 -04009784 if (rc) {
9785 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9786 "0541 Failed setup of Receive Queue: "
James Smarta2fc4aef2014-09-03 12:57:55 -04009787 "rc = 0x%x\n", (uint32_t)rc);
James Smart895427b2017-02-12 13:52:30 -08009788 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -04009789 }
James Smart73d91e52011-10-10 21:32:10 -04009790
James Smartda0436e2009-05-22 14:51:39 -04009791 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9792 "2592 USL RQ setup: hdr-rq-id=%d, dat-rq-id=%d "
9793 "parent cq-id=%d\n",
9794 phba->sli4_hba.hdr_rq->queue_id,
9795 phba->sli4_hba.dat_rq->queue_id,
James Smart4d9ab992009-10-02 15:16:39 -04009796 phba->sli4_hba.els_cq->queue_id);
James Smart1ba981f2014-02-20 09:56:45 -05009797
James Smartcb733e32019-01-28 11:14:32 -08009798 if (phba->cfg_fcp_imax)
9799 usdelay = LPFC_SEC_TO_USEC / phba->cfg_fcp_imax;
9800 else
9801 usdelay = 0;
9802
James Smart6a828b02019-01-28 11:14:31 -08009803 for (qidx = 0; qidx < phba->cfg_irq_chann;
James Smartcdb42be2019-01-28 11:14:21 -08009804 qidx += LPFC_MAX_EQ_DELAY_EQID_CNT)
James Smart0cf07f842017-06-01 21:07:10 -07009805 lpfc_modify_hba_eq_delay(phba, qidx, LPFC_MAX_EQ_DELAY_EQID_CNT,
James Smartcb733e32019-01-28 11:14:32 -08009806 usdelay);
James Smart43140ca2017-03-04 09:30:34 -08009807
James Smart6a828b02019-01-28 11:14:31 -08009808 if (phba->sli4_hba.cq_max) {
9809 kfree(phba->sli4_hba.cq_lookup);
9810 phba->sli4_hba.cq_lookup = kcalloc((phba->sli4_hba.cq_max + 1),
9811 sizeof(struct lpfc_queue *), GFP_KERNEL);
9812 if (!phba->sli4_hba.cq_lookup) {
James Smart1ba981f2014-02-20 09:56:45 -05009813 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart6a828b02019-01-28 11:14:31 -08009814 "0549 Failed setup of CQ Lookup table: "
9815 "size 0x%x\n", phba->sli4_hba.cq_max);
Dan Carpenterfad28e32019-02-11 21:43:00 +03009816 rc = -ENOMEM;
James Smart1ba981f2014-02-20 09:56:45 -05009817 goto out_destroy;
James Smart895427b2017-02-12 13:52:30 -08009818 }
James Smart6a828b02019-01-28 11:14:31 -08009819 lpfc_setup_cq_lookup(phba);
James Smart1ba981f2014-02-20 09:56:45 -05009820 }
James Smartda0436e2009-05-22 14:51:39 -04009821 return 0;
9822
James Smart895427b2017-02-12 13:52:30 -08009823out_destroy:
9824 lpfc_sli4_queue_unset(phba);
James Smartda0436e2009-05-22 14:51:39 -04009825out_error:
9826 return rc;
9827}
9828
9829/**
9830 * lpfc_sli4_queue_unset - Unset all the SLI4 queues
9831 * @phba: pointer to lpfc hba data structure.
9832 *
9833 * This routine is invoked to unset all the SLI4 queues with the FCoE HBA
9834 * operation.
9835 *
9836 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02009837 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009838 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04009839 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04009840 **/
9841void
9842lpfc_sli4_queue_unset(struct lpfc_hba *phba)
9843{
James Smartcdb42be2019-01-28 11:14:21 -08009844 struct lpfc_sli4_hdw_queue *qp;
James Smart657add42019-05-21 17:49:06 -07009845 struct lpfc_queue *eq;
James Smart895427b2017-02-12 13:52:30 -08009846 int qidx;
James Smartda0436e2009-05-22 14:51:39 -04009847
9848 /* Unset mailbox command work queue */
James Smart895427b2017-02-12 13:52:30 -08009849 if (phba->sli4_hba.mbx_wq)
9850 lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
9851
9852 /* Unset NVME LS work queue */
9853 if (phba->sli4_hba.nvmels_wq)
9854 lpfc_wq_destroy(phba, phba->sli4_hba.nvmels_wq);
9855
James Smartda0436e2009-05-22 14:51:39 -04009856 /* Unset ELS work queue */
Colin Ian King019c0d62017-05-06 23:13:55 +01009857 if (phba->sli4_hba.els_wq)
James Smart895427b2017-02-12 13:52:30 -08009858 lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
9859
James Smartda0436e2009-05-22 14:51:39 -04009860 /* Unset unsolicited receive queue */
James Smart895427b2017-02-12 13:52:30 -08009861 if (phba->sli4_hba.hdr_rq)
9862 lpfc_rq_destroy(phba, phba->sli4_hba.hdr_rq,
9863 phba->sli4_hba.dat_rq);
9864
James Smartda0436e2009-05-22 14:51:39 -04009865 /* Unset mailbox command complete queue */
James Smart895427b2017-02-12 13:52:30 -08009866 if (phba->sli4_hba.mbx_cq)
9867 lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
9868
James Smartda0436e2009-05-22 14:51:39 -04009869 /* Unset ELS complete queue */
James Smart895427b2017-02-12 13:52:30 -08009870 if (phba->sli4_hba.els_cq)
9871 lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
9872
9873 /* Unset NVME LS complete queue */
9874 if (phba->sli4_hba.nvmels_cq)
9875 lpfc_cq_destroy(phba, phba->sli4_hba.nvmels_cq);
9876
James Smartbcb24f62017-11-20 16:00:36 -08009877 if (phba->nvmet_support) {
9878 /* Unset NVMET MRQ queue */
9879 if (phba->sli4_hba.nvmet_mrq_hdr) {
9880 for (qidx = 0; qidx < phba->cfg_nvmet_mrq; qidx++)
9881 lpfc_rq_destroy(
9882 phba,
James Smart2d7dbc42017-02-12 13:52:35 -08009883 phba->sli4_hba.nvmet_mrq_hdr[qidx],
9884 phba->sli4_hba.nvmet_mrq_data[qidx]);
James Smartbcb24f62017-11-20 16:00:36 -08009885 }
James Smart2d7dbc42017-02-12 13:52:35 -08009886
James Smartbcb24f62017-11-20 16:00:36 -08009887 /* Unset NVMET CQ Set complete queue */
9888 if (phba->sli4_hba.nvmet_cqset) {
9889 for (qidx = 0; qidx < phba->cfg_nvmet_mrq; qidx++)
9890 lpfc_cq_destroy(
9891 phba, phba->sli4_hba.nvmet_cqset[qidx]);
9892 }
James Smart2d7dbc42017-02-12 13:52:35 -08009893 }
9894
James Smartcdb42be2019-01-28 11:14:21 -08009895 /* Unset fast-path SLI4 queues */
9896 if (phba->sli4_hba.hdwq) {
James Smart657add42019-05-21 17:49:06 -07009897 /* Loop thru all Hardware Queues */
James Smartcdb42be2019-01-28 11:14:21 -08009898 for (qidx = 0; qidx < phba->cfg_hdw_queue; qidx++) {
James Smart657add42019-05-21 17:49:06 -07009899 /* Destroy the CQ/WQ corresponding to Hardware Queue */
James Smartcdb42be2019-01-28 11:14:21 -08009900 qp = &phba->sli4_hba.hdwq[qidx];
James Smartc00f62e2019-08-14 16:57:11 -07009901 lpfc_wq_destroy(phba, qp->io_wq);
9902 lpfc_cq_destroy(phba, qp->io_cq);
James Smart657add42019-05-21 17:49:06 -07009903 }
9904 /* Loop thru all IRQ vectors */
9905 for (qidx = 0; qidx < phba->cfg_irq_chann; qidx++) {
9906 /* Destroy the EQ corresponding to the IRQ vector */
9907 eq = phba->sli4_hba.hba_eq_hdl[qidx].eq;
9908 lpfc_eq_destroy(phba, eq);
James Smartcdb42be2019-01-28 11:14:21 -08009909 }
9910 }
James Smart895427b2017-02-12 13:52:30 -08009911
James Smart6a828b02019-01-28 11:14:31 -08009912 kfree(phba->sli4_hba.cq_lookup);
9913 phba->sli4_hba.cq_lookup = NULL;
9914 phba->sli4_hba.cq_max = 0;
James Smartda0436e2009-05-22 14:51:39 -04009915}
9916
9917/**
9918 * lpfc_sli4_cq_event_pool_create - Create completion-queue event free pool
9919 * @phba: pointer to lpfc hba data structure.
9920 *
9921 * This routine is invoked to allocate and set up a pool of completion queue
9922 * events. The body of the completion queue event is a completion queue entry
9923 * CQE. For now, this pool is used for the interrupt service routine to queue
9924 * the following HBA completion queue events for the worker thread to process:
9925 * - Mailbox asynchronous events
9926 * - Receive queue completion unsolicited events
9927 * Later, this can be used for all the slow-path events.
9928 *
9929 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02009930 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009931 * -ENOMEM - No available memory
James Smartda0436e2009-05-22 14:51:39 -04009932 **/
9933static int
9934lpfc_sli4_cq_event_pool_create(struct lpfc_hba *phba)
9935{
9936 struct lpfc_cq_event *cq_event;
9937 int i;
9938
9939 for (i = 0; i < (4 * phba->sli4_hba.cq_ecount); i++) {
9940 cq_event = kmalloc(sizeof(struct lpfc_cq_event), GFP_KERNEL);
9941 if (!cq_event)
9942 goto out_pool_create_fail;
9943 list_add_tail(&cq_event->list,
9944 &phba->sli4_hba.sp_cqe_event_pool);
9945 }
9946 return 0;
9947
9948out_pool_create_fail:
9949 lpfc_sli4_cq_event_pool_destroy(phba);
9950 return -ENOMEM;
9951}
9952
9953/**
9954 * lpfc_sli4_cq_event_pool_destroy - Free completion-queue event free pool
9955 * @phba: pointer to lpfc hba data structure.
9956 *
9957 * This routine is invoked to free the pool of completion queue events at
9958 * driver unload time. Note that, it is the responsibility of the driver
9959 * cleanup routine to free all the outstanding completion-queue events
9960 * allocated from this pool back into the pool before invoking this routine
9961 * to destroy the pool.
9962 **/
9963static void
9964lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *phba)
9965{
9966 struct lpfc_cq_event *cq_event, *next_cq_event;
9967
9968 list_for_each_entry_safe(cq_event, next_cq_event,
9969 &phba->sli4_hba.sp_cqe_event_pool, list) {
9970 list_del(&cq_event->list);
9971 kfree(cq_event);
9972 }
9973}
9974
9975/**
9976 * __lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
9977 * @phba: pointer to lpfc hba data structure.
9978 *
9979 * This routine is the lock free version of the API invoked to allocate a
9980 * completion-queue event from the free pool.
9981 *
9982 * Return: Pointer to the newly allocated completion-queue event if successful
9983 * NULL otherwise.
9984 **/
9985struct lpfc_cq_event *
9986__lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
9987{
9988 struct lpfc_cq_event *cq_event = NULL;
9989
9990 list_remove_head(&phba->sli4_hba.sp_cqe_event_pool, cq_event,
9991 struct lpfc_cq_event, list);
9992 return cq_event;
9993}
9994
9995/**
9996 * lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
9997 * @phba: pointer to lpfc hba data structure.
9998 *
9999 * This routine is the lock version of the API invoked to allocate a
10000 * completion-queue event from the free pool.
10001 *
10002 * Return: Pointer to the newly allocated completion-queue event if successful
10003 * NULL otherwise.
10004 **/
10005struct lpfc_cq_event *
10006lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
10007{
10008 struct lpfc_cq_event *cq_event;
10009 unsigned long iflags;
10010
10011 spin_lock_irqsave(&phba->hbalock, iflags);
10012 cq_event = __lpfc_sli4_cq_event_alloc(phba);
10013 spin_unlock_irqrestore(&phba->hbalock, iflags);
10014 return cq_event;
10015}
10016
10017/**
10018 * __lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
10019 * @phba: pointer to lpfc hba data structure.
10020 * @cq_event: pointer to the completion queue event to be freed.
10021 *
10022 * This routine is the lock free version of the API invoked to release a
10023 * completion-queue event back into the free pool.
10024 **/
10025void
10026__lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
10027 struct lpfc_cq_event *cq_event)
10028{
10029 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_cqe_event_pool);
10030}
10031
10032/**
10033 * lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
10034 * @phba: pointer to lpfc hba data structure.
10035 * @cq_event: pointer to the completion queue event to be freed.
10036 *
10037 * This routine is the lock version of the API invoked to release a
10038 * completion-queue event back into the free pool.
10039 **/
10040void
10041lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
10042 struct lpfc_cq_event *cq_event)
10043{
10044 unsigned long iflags;
10045 spin_lock_irqsave(&phba->hbalock, iflags);
10046 __lpfc_sli4_cq_event_release(phba, cq_event);
10047 spin_unlock_irqrestore(&phba->hbalock, iflags);
10048}
10049
10050/**
10051 * lpfc_sli4_cq_event_release_all - Release all cq events to the free pool
10052 * @phba: pointer to lpfc hba data structure.
10053 *
10054 * This routine is to free all the pending completion-queue events to the
10055 * back into the free pool for device reset.
10056 **/
10057static void
10058lpfc_sli4_cq_event_release_all(struct lpfc_hba *phba)
10059{
10060 LIST_HEAD(cqelist);
10061 struct lpfc_cq_event *cqe;
10062 unsigned long iflags;
10063
10064 /* Retrieve all the pending WCQEs from pending WCQE lists */
10065 spin_lock_irqsave(&phba->hbalock, iflags);
10066 /* Pending FCP XRI abort events */
10067 list_splice_init(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
10068 &cqelist);
10069 /* Pending ELS XRI abort events */
10070 list_splice_init(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
10071 &cqelist);
10072 /* Pending asynnc events */
10073 list_splice_init(&phba->sli4_hba.sp_asynce_work_queue,
10074 &cqelist);
10075 spin_unlock_irqrestore(&phba->hbalock, iflags);
10076
10077 while (!list_empty(&cqelist)) {
10078 list_remove_head(&cqelist, cqe, struct lpfc_cq_event, list);
10079 lpfc_sli4_cq_event_release(phba, cqe);
10080 }
10081}
10082
10083/**
10084 * lpfc_pci_function_reset - Reset pci function.
10085 * @phba: pointer to lpfc hba data structure.
10086 *
10087 * This routine is invoked to request a PCI function reset. It will destroys
10088 * all resources assigned to the PCI function which originates this request.
10089 *
10090 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020010091 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010092 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -040010093 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -040010094 **/
10095int
10096lpfc_pci_function_reset(struct lpfc_hba *phba)
10097{
10098 LPFC_MBOXQ_t *mboxq;
James Smart2fcee4b2010-12-15 17:57:46 -050010099 uint32_t rc = 0, if_type;
James Smartda0436e2009-05-22 14:51:39 -040010100 uint32_t shdr_status, shdr_add_status;
James Smart2f6fa2c2014-09-03 12:57:08 -040010101 uint32_t rdy_chk;
10102 uint32_t port_reset = 0;
James Smartda0436e2009-05-22 14:51:39 -040010103 union lpfc_sli4_cfg_shdr *shdr;
James Smart2fcee4b2010-12-15 17:57:46 -050010104 struct lpfc_register reg_data;
James Smart2b81f942012-03-01 22:37:18 -050010105 uint16_t devid;
James Smartda0436e2009-05-22 14:51:39 -040010106
James Smart2fcee4b2010-12-15 17:57:46 -050010107 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
10108 switch (if_type) {
10109 case LPFC_SLI_INTF_IF_TYPE_0:
10110 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
10111 GFP_KERNEL);
10112 if (!mboxq) {
10113 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10114 "0494 Unable to allocate memory for "
10115 "issuing SLI_FUNCTION_RESET mailbox "
10116 "command\n");
10117 return -ENOMEM;
10118 }
10119
10120 /* Setup PCI function reset mailbox-ioctl command */
10121 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
10122 LPFC_MBOX_OPCODE_FUNCTION_RESET, 0,
10123 LPFC_SLI4_MBX_EMBED);
10124 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
10125 shdr = (union lpfc_sli4_cfg_shdr *)
10126 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
10127 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10128 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
10129 &shdr->response);
10130 if (rc != MBX_TIMEOUT)
10131 mempool_free(mboxq, phba->mbox_mem_pool);
10132 if (shdr_status || shdr_add_status || rc) {
10133 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10134 "0495 SLI_FUNCTION_RESET mailbox "
10135 "failed with status x%x add_status x%x,"
10136 " mbx status x%x\n",
10137 shdr_status, shdr_add_status, rc);
10138 rc = -ENXIO;
10139 }
10140 break;
10141 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart27d6ac02018-02-22 08:18:42 -080010142 case LPFC_SLI_INTF_IF_TYPE_6:
James Smart2f6fa2c2014-09-03 12:57:08 -040010143wait:
10144 /*
10145 * Poll the Port Status Register and wait for RDY for
10146 * up to 30 seconds. If the port doesn't respond, treat
10147 * it as an error.
10148 */
James Smart77d093f2015-04-07 15:07:08 -040010149 for (rdy_chk = 0; rdy_chk < 1500; rdy_chk++) {
James Smart2f6fa2c2014-09-03 12:57:08 -040010150 if (lpfc_readl(phba->sli4_hba.u.if_type2.
10151 STATUSregaddr, &reg_data.word0)) {
10152 rc = -ENODEV;
10153 goto out;
10154 }
10155 if (bf_get(lpfc_sliport_status_rdy, &reg_data))
10156 break;
10157 msleep(20);
10158 }
10159
10160 if (!bf_get(lpfc_sliport_status_rdy, &reg_data)) {
10161 phba->work_status[0] = readl(
10162 phba->sli4_hba.u.if_type2.ERR1regaddr);
10163 phba->work_status[1] = readl(
10164 phba->sli4_hba.u.if_type2.ERR2regaddr);
10165 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10166 "2890 Port not ready, port status reg "
10167 "0x%x error 1=0x%x, error 2=0x%x\n",
10168 reg_data.word0,
10169 phba->work_status[0],
10170 phba->work_status[1]);
10171 rc = -ENODEV;
10172 goto out;
10173 }
10174
10175 if (!port_reset) {
10176 /*
10177 * Reset the port now
10178 */
James Smart2fcee4b2010-12-15 17:57:46 -050010179 reg_data.word0 = 0;
10180 bf_set(lpfc_sliport_ctrl_end, &reg_data,
10181 LPFC_SLIPORT_LITTLE_ENDIAN);
10182 bf_set(lpfc_sliport_ctrl_ip, &reg_data,
10183 LPFC_SLIPORT_INIT_PORT);
10184 writel(reg_data.word0, phba->sli4_hba.u.if_type2.
10185 CTRLregaddr);
James Smart8fcb8ac2012-03-01 22:35:58 -050010186 /* flush */
James Smart2b81f942012-03-01 22:37:18 -050010187 pci_read_config_word(phba->pcidev,
10188 PCI_DEVICE_ID, &devid);
James Smart2fcee4b2010-12-15 17:57:46 -050010189
James Smart2f6fa2c2014-09-03 12:57:08 -040010190 port_reset = 1;
10191 msleep(20);
10192 goto wait;
10193 } else if (bf_get(lpfc_sliport_status_rn, &reg_data)) {
10194 rc = -ENODEV;
10195 goto out;
James Smart2fcee4b2010-12-15 17:57:46 -050010196 }
10197 break;
James Smart2f6fa2c2014-09-03 12:57:08 -040010198
James Smart2fcee4b2010-12-15 17:57:46 -050010199 case LPFC_SLI_INTF_IF_TYPE_1:
10200 default:
10201 break;
James Smartda0436e2009-05-22 14:51:39 -040010202 }
10203
James Smart73d91e52011-10-10 21:32:10 -040010204out:
James Smart2fcee4b2010-12-15 17:57:46 -050010205 /* Catch the not-ready port failure after a port reset. */
James Smart2f6fa2c2014-09-03 12:57:08 -040010206 if (rc) {
James Smart229adb02013-04-17 20:16:51 -040010207 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10208 "3317 HBA not functional: IP Reset Failed "
James Smart2f6fa2c2014-09-03 12:57:08 -040010209 "try: echo fw_reset > board_mode\n");
James Smart2fcee4b2010-12-15 17:57:46 -050010210 rc = -ENODEV;
James Smart229adb02013-04-17 20:16:51 -040010211 }
James Smart2fcee4b2010-12-15 17:57:46 -050010212
James Smartda0436e2009-05-22 14:51:39 -040010213 return rc;
10214}
10215
10216/**
James Smartda0436e2009-05-22 14:51:39 -040010217 * lpfc_sli4_pci_mem_setup - Setup SLI4 HBA PCI memory space.
10218 * @phba: pointer to lpfc hba data structure.
10219 *
10220 * This routine is invoked to set up the PCI device memory space for device
10221 * with SLI-4 interface spec.
10222 *
10223 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020010224 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -040010225 * other values - error
10226 **/
10227static int
10228lpfc_sli4_pci_mem_setup(struct lpfc_hba *phba)
10229{
Christoph Hellwigf30e1bf2018-10-18 15:10:21 +020010230 struct pci_dev *pdev = phba->pcidev;
James Smartda0436e2009-05-22 14:51:39 -040010231 unsigned long bar0map_len, bar1map_len, bar2map_len;
Dan Carpenter3a487ff2019-03-07 08:33:44 +030010232 int error;
James Smart2fcee4b2010-12-15 17:57:46 -050010233 uint32_t if_type;
James Smartda0436e2009-05-22 14:51:39 -040010234
Christoph Hellwigf30e1bf2018-10-18 15:10:21 +020010235 if (!pdev)
Hannes Reinecke56de8352019-02-18 08:34:19 +010010236 return -ENODEV;
James Smartda0436e2009-05-22 14:51:39 -040010237
10238 /* Set the device DMA mask size */
Hannes Reinecke56de8352019-02-18 08:34:19 +010010239 error = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
10240 if (error)
10241 error = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
10242 if (error)
Christoph Hellwigf30e1bf2018-10-18 15:10:21 +020010243 return error;
James Smartda0436e2009-05-22 14:51:39 -040010244
James Smart2fcee4b2010-12-15 17:57:46 -050010245 /*
10246 * The BARs and register set definitions and offset locations are
10247 * dependent on the if_type.
10248 */
10249 if (pci_read_config_dword(pdev, LPFC_SLI_INTF,
10250 &phba->sli4_hba.sli_intf.word0)) {
Dan Carpenter3a487ff2019-03-07 08:33:44 +030010251 return -ENODEV;
James Smart2fcee4b2010-12-15 17:57:46 -050010252 }
10253
10254 /* There is no SLI3 failback for SLI4 devices. */
10255 if (bf_get(lpfc_sli_intf_valid, &phba->sli4_hba.sli_intf) !=
10256 LPFC_SLI_INTF_VALID) {
10257 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10258 "2894 SLI_INTF reg contents invalid "
10259 "sli_intf reg 0x%x\n",
10260 phba->sli4_hba.sli_intf.word0);
Dan Carpenter3a487ff2019-03-07 08:33:44 +030010261 return -ENODEV;
James Smart2fcee4b2010-12-15 17:57:46 -050010262 }
10263
10264 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
10265 /*
10266 * Get the bus address of SLI4 device Bar regions and the
10267 * number of bytes required by each mapping. The mapping of the
10268 * particular PCI BARs regions is dependent on the type of
10269 * SLI4 device.
James Smartda0436e2009-05-22 14:51:39 -040010270 */
James Smartf5ca6f22013-09-06 12:21:09 -040010271 if (pci_resource_start(pdev, PCI_64BIT_BAR0)) {
10272 phba->pci_bar0_map = pci_resource_start(pdev, PCI_64BIT_BAR0);
10273 bar0map_len = pci_resource_len(pdev, PCI_64BIT_BAR0);
James Smart2fcee4b2010-12-15 17:57:46 -050010274
10275 /*
10276 * Map SLI4 PCI Config Space Register base to a kernel virtual
10277 * addr
10278 */
10279 phba->sli4_hba.conf_regs_memmap_p =
10280 ioremap(phba->pci_bar0_map, bar0map_len);
10281 if (!phba->sli4_hba.conf_regs_memmap_p) {
10282 dev_printk(KERN_ERR, &pdev->dev,
10283 "ioremap failed for SLI4 PCI config "
10284 "registers.\n");
Dan Carpenter3a487ff2019-03-07 08:33:44 +030010285 return -ENODEV;
James Smart2fcee4b2010-12-15 17:57:46 -050010286 }
James Smartf5ca6f22013-09-06 12:21:09 -040010287 phba->pci_bar0_memmap_p = phba->sli4_hba.conf_regs_memmap_p;
James Smart2fcee4b2010-12-15 17:57:46 -050010288 /* Set up BAR0 PCI config space register memory map */
10289 lpfc_sli4_bar0_register_memmap(phba, if_type);
James Smart1dfb5a42010-02-12 14:40:50 -050010290 } else {
10291 phba->pci_bar0_map = pci_resource_start(pdev, 1);
10292 bar0map_len = pci_resource_len(pdev, 1);
James Smart27d6ac02018-02-22 08:18:42 -080010293 if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
James Smart2fcee4b2010-12-15 17:57:46 -050010294 dev_printk(KERN_ERR, &pdev->dev,
10295 "FATAL - No BAR0 mapping for SLI4, if_type 2\n");
Dan Carpenter3a487ff2019-03-07 08:33:44 +030010296 return -ENODEV;
James Smart2fcee4b2010-12-15 17:57:46 -050010297 }
10298 phba->sli4_hba.conf_regs_memmap_p =
James Smartda0436e2009-05-22 14:51:39 -040010299 ioremap(phba->pci_bar0_map, bar0map_len);
James Smart2fcee4b2010-12-15 17:57:46 -050010300 if (!phba->sli4_hba.conf_regs_memmap_p) {
10301 dev_printk(KERN_ERR, &pdev->dev,
10302 "ioremap failed for SLI4 PCI config "
10303 "registers.\n");
Dan Carpenter3a487ff2019-03-07 08:33:44 +030010304 return -ENODEV;
James Smart2fcee4b2010-12-15 17:57:46 -050010305 }
10306 lpfc_sli4_bar0_register_memmap(phba, if_type);
James Smartda0436e2009-05-22 14:51:39 -040010307 }
10308
James Smarte4b97942017-11-20 16:00:31 -080010309 if (if_type == LPFC_SLI_INTF_IF_TYPE_0) {
10310 if (pci_resource_start(pdev, PCI_64BIT_BAR2)) {
10311 /*
10312 * Map SLI4 if type 0 HBA Control Register base to a
10313 * kernel virtual address and setup the registers.
10314 */
10315 phba->pci_bar1_map = pci_resource_start(pdev,
10316 PCI_64BIT_BAR2);
10317 bar1map_len = pci_resource_len(pdev, PCI_64BIT_BAR2);
10318 phba->sli4_hba.ctrl_regs_memmap_p =
10319 ioremap(phba->pci_bar1_map,
10320 bar1map_len);
10321 if (!phba->sli4_hba.ctrl_regs_memmap_p) {
10322 dev_err(&pdev->dev,
10323 "ioremap failed for SLI4 HBA "
10324 "control registers.\n");
10325 error = -ENOMEM;
10326 goto out_iounmap_conf;
10327 }
10328 phba->pci_bar2_memmap_p =
10329 phba->sli4_hba.ctrl_regs_memmap_p;
James Smart27d6ac02018-02-22 08:18:42 -080010330 lpfc_sli4_bar1_register_memmap(phba, if_type);
James Smarte4b97942017-11-20 16:00:31 -080010331 } else {
10332 error = -ENOMEM;
James Smart2fcee4b2010-12-15 17:57:46 -050010333 goto out_iounmap_conf;
10334 }
James Smartda0436e2009-05-22 14:51:39 -040010335 }
10336
James Smart27d6ac02018-02-22 08:18:42 -080010337 if ((if_type == LPFC_SLI_INTF_IF_TYPE_6) &&
10338 (pci_resource_start(pdev, PCI_64BIT_BAR2))) {
10339 /*
10340 * Map SLI4 if type 6 HBA Doorbell Register base to a kernel
10341 * virtual address and setup the registers.
10342 */
10343 phba->pci_bar1_map = pci_resource_start(pdev, PCI_64BIT_BAR2);
10344 bar1map_len = pci_resource_len(pdev, PCI_64BIT_BAR2);
10345 phba->sli4_hba.drbl_regs_memmap_p =
10346 ioremap(phba->pci_bar1_map, bar1map_len);
10347 if (!phba->sli4_hba.drbl_regs_memmap_p) {
10348 dev_err(&pdev->dev,
10349 "ioremap failed for SLI4 HBA doorbell registers.\n");
Dan Carpenter3a487ff2019-03-07 08:33:44 +030010350 error = -ENOMEM;
James Smart27d6ac02018-02-22 08:18:42 -080010351 goto out_iounmap_conf;
10352 }
10353 phba->pci_bar2_memmap_p = phba->sli4_hba.drbl_regs_memmap_p;
10354 lpfc_sli4_bar1_register_memmap(phba, if_type);
10355 }
10356
James Smarte4b97942017-11-20 16:00:31 -080010357 if (if_type == LPFC_SLI_INTF_IF_TYPE_0) {
10358 if (pci_resource_start(pdev, PCI_64BIT_BAR4)) {
10359 /*
10360 * Map SLI4 if type 0 HBA Doorbell Register base to
10361 * a kernel virtual address and setup the registers.
10362 */
10363 phba->pci_bar2_map = pci_resource_start(pdev,
10364 PCI_64BIT_BAR4);
10365 bar2map_len = pci_resource_len(pdev, PCI_64BIT_BAR4);
10366 phba->sli4_hba.drbl_regs_memmap_p =
10367 ioremap(phba->pci_bar2_map,
10368 bar2map_len);
10369 if (!phba->sli4_hba.drbl_regs_memmap_p) {
10370 dev_err(&pdev->dev,
10371 "ioremap failed for SLI4 HBA"
10372 " doorbell registers.\n");
10373 error = -ENOMEM;
10374 goto out_iounmap_ctrl;
10375 }
10376 phba->pci_bar4_memmap_p =
10377 phba->sli4_hba.drbl_regs_memmap_p;
10378 error = lpfc_sli4_bar2_register_memmap(phba, LPFC_VF0);
10379 if (error)
10380 goto out_iounmap_all;
10381 } else {
10382 error = -ENOMEM;
James Smart2fcee4b2010-12-15 17:57:46 -050010383 goto out_iounmap_all;
James Smarte4b97942017-11-20 16:00:31 -080010384 }
James Smartda0436e2009-05-22 14:51:39 -040010385 }
10386
James Smart1351e692018-02-22 08:18:43 -080010387 if (if_type == LPFC_SLI_INTF_IF_TYPE_6 &&
10388 pci_resource_start(pdev, PCI_64BIT_BAR4)) {
10389 /*
10390 * Map SLI4 if type 6 HBA DPP Register base to a kernel
10391 * virtual address and setup the registers.
10392 */
10393 phba->pci_bar2_map = pci_resource_start(pdev, PCI_64BIT_BAR4);
10394 bar2map_len = pci_resource_len(pdev, PCI_64BIT_BAR4);
10395 phba->sli4_hba.dpp_regs_memmap_p =
10396 ioremap(phba->pci_bar2_map, bar2map_len);
10397 if (!phba->sli4_hba.dpp_regs_memmap_p) {
10398 dev_err(&pdev->dev,
10399 "ioremap failed for SLI4 HBA dpp registers.\n");
Dan Carpenter3a487ff2019-03-07 08:33:44 +030010400 error = -ENOMEM;
James Smart1351e692018-02-22 08:18:43 -080010401 goto out_iounmap_ctrl;
10402 }
10403 phba->pci_bar4_memmap_p = phba->sli4_hba.dpp_regs_memmap_p;
10404 }
10405
James Smartb71413d2018-02-22 08:18:40 -080010406 /* Set up the EQ/CQ register handeling functions now */
James Smart27d6ac02018-02-22 08:18:42 -080010407 switch (if_type) {
10408 case LPFC_SLI_INTF_IF_TYPE_0:
10409 case LPFC_SLI_INTF_IF_TYPE_2:
James Smartb71413d2018-02-22 08:18:40 -080010410 phba->sli4_hba.sli4_eq_clr_intr = lpfc_sli4_eq_clr_intr;
James Smart32517fc2019-01-28 11:14:33 -080010411 phba->sli4_hba.sli4_write_eq_db = lpfc_sli4_write_eq_db;
10412 phba->sli4_hba.sli4_write_cq_db = lpfc_sli4_write_cq_db;
James Smart27d6ac02018-02-22 08:18:42 -080010413 break;
10414 case LPFC_SLI_INTF_IF_TYPE_6:
10415 phba->sli4_hba.sli4_eq_clr_intr = lpfc_sli4_if6_eq_clr_intr;
James Smart32517fc2019-01-28 11:14:33 -080010416 phba->sli4_hba.sli4_write_eq_db = lpfc_sli4_if6_write_eq_db;
10417 phba->sli4_hba.sli4_write_cq_db = lpfc_sli4_if6_write_cq_db;
James Smart27d6ac02018-02-22 08:18:42 -080010418 break;
10419 default:
10420 break;
James Smartb71413d2018-02-22 08:18:40 -080010421 }
10422
James Smartda0436e2009-05-22 14:51:39 -040010423 return 0;
10424
10425out_iounmap_all:
10426 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
10427out_iounmap_ctrl:
10428 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
10429out_iounmap_conf:
10430 iounmap(phba->sli4_hba.conf_regs_memmap_p);
Dan Carpenter3a487ff2019-03-07 08:33:44 +030010431
James Smartda0436e2009-05-22 14:51:39 -040010432 return error;
10433}
10434
10435/**
10436 * lpfc_sli4_pci_mem_unset - Unset SLI4 HBA PCI memory space.
10437 * @phba: pointer to lpfc hba data structure.
10438 *
10439 * This routine is invoked to unset the PCI device memory space for device
10440 * with SLI-4 interface spec.
10441 **/
10442static void
10443lpfc_sli4_pci_mem_unset(struct lpfc_hba *phba)
10444{
James Smart2e90f4b2011-12-13 13:22:37 -050010445 uint32_t if_type;
10446 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
James Smartda0436e2009-05-22 14:51:39 -040010447
James Smart2e90f4b2011-12-13 13:22:37 -050010448 switch (if_type) {
10449 case LPFC_SLI_INTF_IF_TYPE_0:
10450 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
10451 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
10452 iounmap(phba->sli4_hba.conf_regs_memmap_p);
10453 break;
10454 case LPFC_SLI_INTF_IF_TYPE_2:
10455 iounmap(phba->sli4_hba.conf_regs_memmap_p);
10456 break;
James Smart27d6ac02018-02-22 08:18:42 -080010457 case LPFC_SLI_INTF_IF_TYPE_6:
10458 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
10459 iounmap(phba->sli4_hba.conf_regs_memmap_p);
10460 break;
James Smart2e90f4b2011-12-13 13:22:37 -050010461 case LPFC_SLI_INTF_IF_TYPE_1:
10462 default:
10463 dev_printk(KERN_ERR, &phba->pcidev->dev,
10464 "FATAL - unsupported SLI4 interface type - %d\n",
10465 if_type);
10466 break;
10467 }
James Smartda0436e2009-05-22 14:51:39 -040010468}
10469
10470/**
James Smart3772a992009-05-22 14:50:54 -040010471 * lpfc_sli_enable_msix - Enable MSI-X interrupt mode on SLI-3 device
10472 * @phba: pointer to lpfc hba data structure.
10473 *
10474 * This routine is invoked to enable the MSI-X interrupt vectors to device
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010475 * with SLI-3 interface specs.
James Smart3772a992009-05-22 14:50:54 -040010476 *
10477 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020010478 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -040010479 * other values - error
10480 **/
10481static int
10482lpfc_sli_enable_msix(struct lpfc_hba *phba)
10483{
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010484 int rc;
James Smart3772a992009-05-22 14:50:54 -040010485 LPFC_MBOXQ_t *pmb;
10486
10487 /* Set up MSI-X multi-message vectors */
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010488 rc = pci_alloc_irq_vectors(phba->pcidev,
10489 LPFC_MSIX_VECTORS, LPFC_MSIX_VECTORS, PCI_IRQ_MSIX);
10490 if (rc < 0) {
James Smart3772a992009-05-22 14:50:54 -040010491 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10492 "0420 PCI enable MSI-X failed (%d)\n", rc);
Alexander Gordeev029165a2014-07-16 20:05:15 +020010493 goto vec_fail_out;
James Smart3772a992009-05-22 14:50:54 -040010494 }
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010495
James Smart3772a992009-05-22 14:50:54 -040010496 /*
10497 * Assign MSI-X vectors to interrupt handlers
10498 */
10499
10500 /* vector-0 is associated to slow-path handler */
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010501 rc = request_irq(pci_irq_vector(phba->pcidev, 0),
James Smarted243d32015-05-21 13:55:25 -040010502 &lpfc_sli_sp_intr_handler, 0,
James Smart3772a992009-05-22 14:50:54 -040010503 LPFC_SP_DRIVER_HANDLER_NAME, phba);
10504 if (rc) {
10505 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
10506 "0421 MSI-X slow-path request_irq failed "
10507 "(%d)\n", rc);
10508 goto msi_fail_out;
10509 }
10510
10511 /* vector-1 is associated to fast-path handler */
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010512 rc = request_irq(pci_irq_vector(phba->pcidev, 1),
James Smarted243d32015-05-21 13:55:25 -040010513 &lpfc_sli_fp_intr_handler, 0,
James Smart3772a992009-05-22 14:50:54 -040010514 LPFC_FP_DRIVER_HANDLER_NAME, phba);
10515
10516 if (rc) {
10517 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
10518 "0429 MSI-X fast-path request_irq failed "
10519 "(%d)\n", rc);
10520 goto irq_fail_out;
10521 }
10522
10523 /*
10524 * Configure HBA MSI-X attention conditions to messages
10525 */
10526 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10527
10528 if (!pmb) {
10529 rc = -ENOMEM;
10530 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10531 "0474 Unable to allocate memory for issuing "
10532 "MBOX_CONFIG_MSI command\n");
10533 goto mem_fail_out;
10534 }
10535 rc = lpfc_config_msi(phba, pmb);
10536 if (rc)
10537 goto mbx_fail_out;
10538 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
10539 if (rc != MBX_SUCCESS) {
10540 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
10541 "0351 Config MSI mailbox command failed, "
10542 "mbxCmd x%x, mbxStatus x%x\n",
10543 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus);
10544 goto mbx_fail_out;
10545 }
10546
10547 /* Free memory allocated for mailbox command */
10548 mempool_free(pmb, phba->mbox_mem_pool);
10549 return rc;
10550
10551mbx_fail_out:
10552 /* Free memory allocated for mailbox command */
10553 mempool_free(pmb, phba->mbox_mem_pool);
10554
10555mem_fail_out:
10556 /* free the irq already requested */
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010557 free_irq(pci_irq_vector(phba->pcidev, 1), phba);
James Smart3772a992009-05-22 14:50:54 -040010558
10559irq_fail_out:
10560 /* free the irq already requested */
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010561 free_irq(pci_irq_vector(phba->pcidev, 0), phba);
James Smart3772a992009-05-22 14:50:54 -040010562
10563msi_fail_out:
10564 /* Unconfigure MSI-X capability structure */
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010565 pci_free_irq_vectors(phba->pcidev);
Alexander Gordeev029165a2014-07-16 20:05:15 +020010566
10567vec_fail_out:
James Smart3772a992009-05-22 14:50:54 -040010568 return rc;
10569}
10570
10571/**
James Smart3772a992009-05-22 14:50:54 -040010572 * lpfc_sli_enable_msi - Enable MSI interrupt mode on SLI-3 device.
10573 * @phba: pointer to lpfc hba data structure.
10574 *
10575 * This routine is invoked to enable the MSI interrupt mode to device with
10576 * SLI-3 interface spec. The kernel function pci_enable_msi() is called to
10577 * enable the MSI vector. The device driver is responsible for calling the
10578 * request_irq() to register MSI vector with a interrupt the handler, which
10579 * is done in this function.
10580 *
10581 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020010582 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -040010583 * other values - error
10584 */
10585static int
10586lpfc_sli_enable_msi(struct lpfc_hba *phba)
10587{
10588 int rc;
10589
10590 rc = pci_enable_msi(phba->pcidev);
10591 if (!rc)
10592 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10593 "0462 PCI enable MSI mode success.\n");
10594 else {
10595 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10596 "0471 PCI enable MSI mode failed (%d)\n", rc);
10597 return rc;
10598 }
10599
10600 rc = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
James Smarted243d32015-05-21 13:55:25 -040010601 0, LPFC_DRIVER_NAME, phba);
James Smart3772a992009-05-22 14:50:54 -040010602 if (rc) {
10603 pci_disable_msi(phba->pcidev);
10604 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
10605 "0478 MSI request_irq failed (%d)\n", rc);
10606 }
10607 return rc;
10608}
10609
10610/**
James Smart3772a992009-05-22 14:50:54 -040010611 * lpfc_sli_enable_intr - Enable device interrupt to SLI-3 device.
10612 * @phba: pointer to lpfc hba data structure.
10613 *
10614 * This routine is invoked to enable device interrupt and associate driver's
10615 * interrupt handler(s) to interrupt vector(s) to device with SLI-3 interface
10616 * spec. Depends on the interrupt mode configured to the driver, the driver
10617 * will try to fallback from the configured interrupt mode to an interrupt
10618 * mode which is supported by the platform, kernel, and device in the order
10619 * of:
10620 * MSI-X -> MSI -> IRQ.
10621 *
10622 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020010623 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -040010624 * other values - error
10625 **/
10626static uint32_t
10627lpfc_sli_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
10628{
10629 uint32_t intr_mode = LPFC_INTR_ERROR;
10630 int retval;
10631
10632 if (cfg_mode == 2) {
10633 /* Need to issue conf_port mbox cmd before conf_msi mbox cmd */
10634 retval = lpfc_sli_config_port(phba, LPFC_SLI_REV3);
10635 if (!retval) {
10636 /* Now, try to enable MSI-X interrupt mode */
10637 retval = lpfc_sli_enable_msix(phba);
10638 if (!retval) {
10639 /* Indicate initialization to MSI-X mode */
10640 phba->intr_type = MSIX;
10641 intr_mode = 2;
10642 }
10643 }
10644 }
10645
10646 /* Fallback to MSI if MSI-X initialization failed */
10647 if (cfg_mode >= 1 && phba->intr_type == NONE) {
10648 retval = lpfc_sli_enable_msi(phba);
10649 if (!retval) {
10650 /* Indicate initialization to MSI mode */
10651 phba->intr_type = MSI;
10652 intr_mode = 1;
10653 }
10654 }
10655
10656 /* Fallback to INTx if both MSI-X/MSI initalization failed */
10657 if (phba->intr_type == NONE) {
10658 retval = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
10659 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
10660 if (!retval) {
10661 /* Indicate initialization to INTx mode */
10662 phba->intr_type = INTx;
10663 intr_mode = 0;
10664 }
10665 }
10666 return intr_mode;
10667}
10668
10669/**
10670 * lpfc_sli_disable_intr - Disable device interrupt to SLI-3 device.
10671 * @phba: pointer to lpfc hba data structure.
10672 *
10673 * This routine is invoked to disable device interrupt and disassociate the
10674 * driver's interrupt handler(s) from interrupt vector(s) to device with
10675 * SLI-3 interface spec. Depending on the interrupt mode, the driver will
10676 * release the interrupt vector(s) for the message signaled interrupt.
10677 **/
10678static void
10679lpfc_sli_disable_intr(struct lpfc_hba *phba)
10680{
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010681 int nr_irqs, i;
10682
James Smart3772a992009-05-22 14:50:54 -040010683 if (phba->intr_type == MSIX)
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010684 nr_irqs = LPFC_MSIX_VECTORS;
10685 else
10686 nr_irqs = 1;
10687
10688 for (i = 0; i < nr_irqs; i++)
10689 free_irq(pci_irq_vector(phba->pcidev, i), phba);
10690 pci_free_irq_vectors(phba->pcidev);
James Smart3772a992009-05-22 14:50:54 -040010691
10692 /* Reset interrupt management states */
10693 phba->intr_type = NONE;
10694 phba->sli.slistat.sli_intr = 0;
James Smart3772a992009-05-22 14:50:54 -040010695}
10696
10697/**
James Smart657add42019-05-21 17:49:06 -070010698 * lpfc_find_cpu_handle - Find the CPU that corresponds to the specified Queue
James Smart6a828b02019-01-28 11:14:31 -080010699 * @phba: pointer to lpfc hba data structure.
10700 * @id: EQ vector index or Hardware Queue index
10701 * @match: LPFC_FIND_BY_EQ = match by EQ
10702 * LPFC_FIND_BY_HDWQ = match by Hardware Queue
James Smart657add42019-05-21 17:49:06 -070010703 * Return the CPU that matches the selection criteria
James Smart6a828b02019-01-28 11:14:31 -080010704 */
10705static uint16_t
10706lpfc_find_cpu_handle(struct lpfc_hba *phba, uint16_t id, int match)
10707{
10708 struct lpfc_vector_map_info *cpup;
10709 int cpu;
10710
James Smart657add42019-05-21 17:49:06 -070010711 /* Loop through all CPUs */
James Smart222e9232019-01-28 11:14:35 -080010712 for_each_present_cpu(cpu) {
10713 cpup = &phba->sli4_hba.cpu_map[cpu];
James Smart657add42019-05-21 17:49:06 -070010714
10715 /* If we are matching by EQ, there may be multiple CPUs using
10716 * using the same vector, so select the one with
10717 * LPFC_CPU_FIRST_IRQ set.
10718 */
James Smart6a828b02019-01-28 11:14:31 -080010719 if ((match == LPFC_FIND_BY_EQ) &&
James Smart657add42019-05-21 17:49:06 -070010720 (cpup->flag & LPFC_CPU_FIRST_IRQ) &&
James Smart6a828b02019-01-28 11:14:31 -080010721 (cpup->eq == id))
10722 return cpu;
James Smart657add42019-05-21 17:49:06 -070010723
10724 /* If matching by HDWQ, select the first CPU that matches */
James Smart6a828b02019-01-28 11:14:31 -080010725 if ((match == LPFC_FIND_BY_HDWQ) && (cpup->hdwq == id))
10726 return cpu;
James Smart6a828b02019-01-28 11:14:31 -080010727 }
10728 return 0;
10729}
10730
James Smart6a828b02019-01-28 11:14:31 -080010731#ifdef CONFIG_X86
10732/**
10733 * lpfc_find_hyper - Determine if the CPU map entry is hyper-threaded
10734 * @phba: pointer to lpfc hba data structure.
10735 * @cpu: CPU map index
10736 * @phys_id: CPU package physical id
10737 * @core_id: CPU core id
10738 */
10739static int
10740lpfc_find_hyper(struct lpfc_hba *phba, int cpu,
10741 uint16_t phys_id, uint16_t core_id)
10742{
10743 struct lpfc_vector_map_info *cpup;
10744 int idx;
10745
James Smart222e9232019-01-28 11:14:35 -080010746 for_each_present_cpu(idx) {
10747 cpup = &phba->sli4_hba.cpu_map[idx];
James Smart6a828b02019-01-28 11:14:31 -080010748 /* Does the cpup match the one we are looking for */
10749 if ((cpup->phys_id == phys_id) &&
10750 (cpup->core_id == core_id) &&
James Smart222e9232019-01-28 11:14:35 -080010751 (cpu != idx))
James Smart6a828b02019-01-28 11:14:31 -080010752 return 1;
James Smart6a828b02019-01-28 11:14:31 -080010753 }
10754 return 0;
10755}
10756#endif
10757
James Smartdcaa2132019-11-04 16:57:06 -080010758/*
10759 * lpfc_assign_eq_map_info - Assigns eq for vector_map structure
10760 * @phba: pointer to lpfc hba data structure.
10761 * @eqidx: index for eq and irq vector
10762 * @flag: flags to set for vector_map structure
10763 * @cpu: cpu used to index vector_map structure
10764 *
10765 * The routine assigns eq info into vector_map structure
10766 */
10767static inline void
10768lpfc_assign_eq_map_info(struct lpfc_hba *phba, uint16_t eqidx, uint16_t flag,
10769 unsigned int cpu)
10770{
10771 struct lpfc_vector_map_info *cpup = &phba->sli4_hba.cpu_map[cpu];
10772 struct lpfc_hba_eq_hdl *eqhdl = lpfc_get_eq_hdl(eqidx);
10773
10774 cpup->eq = eqidx;
10775 cpup->flag |= flag;
10776
10777 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10778 "3336 Set Affinity: CPU %d irq %d eq %d flag x%x\n",
10779 cpu, eqhdl->irq, cpup->eq, cpup->flag);
10780}
10781
10782/**
10783 * lpfc_cpu_map_array_init - Initialize cpu_map structure
10784 * @phba: pointer to lpfc hba data structure.
10785 *
10786 * The routine initializes the cpu_map array structure
10787 */
10788static void
10789lpfc_cpu_map_array_init(struct lpfc_hba *phba)
10790{
10791 struct lpfc_vector_map_info *cpup;
10792 struct lpfc_eq_intr_info *eqi;
10793 int cpu;
10794
10795 for_each_possible_cpu(cpu) {
10796 cpup = &phba->sli4_hba.cpu_map[cpu];
10797 cpup->phys_id = LPFC_VECTOR_MAP_EMPTY;
10798 cpup->core_id = LPFC_VECTOR_MAP_EMPTY;
10799 cpup->hdwq = LPFC_VECTOR_MAP_EMPTY;
10800 cpup->eq = LPFC_VECTOR_MAP_EMPTY;
10801 cpup->flag = 0;
10802 eqi = per_cpu_ptr(phba->sli4_hba.eq_info, cpu);
10803 INIT_LIST_HEAD(&eqi->list);
10804 eqi->icnt = 0;
10805 }
10806}
10807
10808/**
10809 * lpfc_hba_eq_hdl_array_init - Initialize hba_eq_hdl structure
10810 * @phba: pointer to lpfc hba data structure.
10811 *
10812 * The routine initializes the hba_eq_hdl array structure
10813 */
10814static void
10815lpfc_hba_eq_hdl_array_init(struct lpfc_hba *phba)
10816{
10817 struct lpfc_hba_eq_hdl *eqhdl;
10818 int i;
10819
10820 for (i = 0; i < phba->cfg_irq_chann; i++) {
10821 eqhdl = lpfc_get_eq_hdl(i);
10822 eqhdl->irq = LPFC_VECTOR_MAP_EMPTY;
10823 eqhdl->phba = phba;
10824 }
10825}
10826
James Smart6a828b02019-01-28 11:14:31 -080010827/**
James Smart895427b2017-02-12 13:52:30 -080010828 * lpfc_cpu_affinity_check - Check vector CPU affinity mappings
James Smart7bb03bb2013-04-17 20:19:16 -040010829 * @phba: pointer to lpfc hba data structure.
James Smart895427b2017-02-12 13:52:30 -080010830 * @vectors: number of msix vectors allocated.
James Smart7bb03bb2013-04-17 20:19:16 -040010831 *
James Smart895427b2017-02-12 13:52:30 -080010832 * The routine will figure out the CPU affinity assignment for every
James Smart6a828b02019-01-28 11:14:31 -080010833 * MSI-X vector allocated for the HBA.
James Smart895427b2017-02-12 13:52:30 -080010834 * In addition, the CPU to IO channel mapping will be calculated
10835 * and the phba->sli4_hba.cpu_map array will reflect this.
James Smart7bb03bb2013-04-17 20:19:16 -040010836 */
James Smart895427b2017-02-12 13:52:30 -080010837static void
10838lpfc_cpu_affinity_check(struct lpfc_hba *phba, int vectors)
James Smart7bb03bb2013-04-17 20:19:16 -040010839{
James Smart3ad348d2019-08-14 16:56:43 -070010840 int i, cpu, idx, next_idx, new_cpu, start_cpu, first_cpu;
James Smart6a828b02019-01-28 11:14:31 -080010841 int max_phys_id, min_phys_id;
10842 int max_core_id, min_core_id;
James Smart7bb03bb2013-04-17 20:19:16 -040010843 struct lpfc_vector_map_info *cpup;
James Smartd9954a22019-05-21 17:49:05 -070010844 struct lpfc_vector_map_info *new_cpup;
James Smart7bb03bb2013-04-17 20:19:16 -040010845#ifdef CONFIG_X86
10846 struct cpuinfo_x86 *cpuinfo;
10847#endif
James Smart7bb03bb2013-04-17 20:19:16 -040010848
James Smart6a828b02019-01-28 11:14:31 -080010849 max_phys_id = 0;
James Smartd9954a22019-05-21 17:49:05 -070010850 min_phys_id = LPFC_VECTOR_MAP_EMPTY;
James Smart6a828b02019-01-28 11:14:31 -080010851 max_core_id = 0;
James Smartd9954a22019-05-21 17:49:05 -070010852 min_core_id = LPFC_VECTOR_MAP_EMPTY;
James Smart7bb03bb2013-04-17 20:19:16 -040010853
10854 /* Update CPU map with physical id and core id of each CPU */
James Smart222e9232019-01-28 11:14:35 -080010855 for_each_present_cpu(cpu) {
10856 cpup = &phba->sli4_hba.cpu_map[cpu];
James Smart7bb03bb2013-04-17 20:19:16 -040010857#ifdef CONFIG_X86
10858 cpuinfo = &cpu_data(cpu);
10859 cpup->phys_id = cpuinfo->phys_proc_id;
10860 cpup->core_id = cpuinfo->cpu_core_id;
James Smartd9954a22019-05-21 17:49:05 -070010861 if (lpfc_find_hyper(phba, cpu, cpup->phys_id, cpup->core_id))
10862 cpup->flag |= LPFC_CPU_MAP_HYPER;
James Smart7bb03bb2013-04-17 20:19:16 -040010863#else
10864 /* No distinction between CPUs for other platforms */
10865 cpup->phys_id = 0;
James Smart6a828b02019-01-28 11:14:31 -080010866 cpup->core_id = cpu;
James Smart7bb03bb2013-04-17 20:19:16 -040010867#endif
James Smart7bb03bb2013-04-17 20:19:16 -040010868
James Smartb3295c22019-01-28 11:14:30 -080010869 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart3ad348d2019-08-14 16:56:43 -070010870 "3328 CPU %d physid %d coreid %d flag x%x\n",
10871 cpu, cpup->phys_id, cpup->core_id, cpup->flag);
James Smart6a828b02019-01-28 11:14:31 -080010872
10873 if (cpup->phys_id > max_phys_id)
10874 max_phys_id = cpup->phys_id;
10875 if (cpup->phys_id < min_phys_id)
10876 min_phys_id = cpup->phys_id;
10877
10878 if (cpup->core_id > max_core_id)
10879 max_core_id = cpup->core_id;
10880 if (cpup->core_id < min_core_id)
10881 min_core_id = cpup->core_id;
James Smart7bb03bb2013-04-17 20:19:16 -040010882 }
James Smartb3295c22019-01-28 11:14:30 -080010883
James Smartd9954a22019-05-21 17:49:05 -070010884 /* After looking at each irq vector assigned to this pcidev, its
10885 * possible to see that not ALL CPUs have been accounted for.
James Smart657add42019-05-21 17:49:06 -070010886 * Next we will set any unassigned (unaffinitized) cpu map
10887 * entries to a IRQ on the same phys_id.
James Smartd9954a22019-05-21 17:49:05 -070010888 */
10889 first_cpu = cpumask_first(cpu_present_mask);
10890 start_cpu = first_cpu;
10891
10892 for_each_present_cpu(cpu) {
10893 cpup = &phba->sli4_hba.cpu_map[cpu];
10894
10895 /* Is this CPU entry unassigned */
10896 if (cpup->eq == LPFC_VECTOR_MAP_EMPTY) {
10897 /* Mark CPU as IRQ not assigned by the kernel */
10898 cpup->flag |= LPFC_CPU_MAP_UNASSIGN;
10899
James Smart657add42019-05-21 17:49:06 -070010900 /* If so, find a new_cpup thats on the the SAME
James Smartd9954a22019-05-21 17:49:05 -070010901 * phys_id as cpup. start_cpu will start where we
10902 * left off so all unassigned entries don't get assgined
10903 * the IRQ of the first entry.
10904 */
10905 new_cpu = start_cpu;
10906 for (i = 0; i < phba->sli4_hba.num_present_cpu; i++) {
10907 new_cpup = &phba->sli4_hba.cpu_map[new_cpu];
10908 if (!(new_cpup->flag & LPFC_CPU_MAP_UNASSIGN) &&
James Smartdcaa2132019-11-04 16:57:06 -080010909 (new_cpup->eq != LPFC_VECTOR_MAP_EMPTY) &&
James Smartd9954a22019-05-21 17:49:05 -070010910 (new_cpup->phys_id == cpup->phys_id))
10911 goto found_same;
10912 new_cpu = cpumask_next(
10913 new_cpu, cpu_present_mask);
10914 if (new_cpu == nr_cpumask_bits)
10915 new_cpu = first_cpu;
10916 }
10917 /* At this point, we leave the CPU as unassigned */
10918 continue;
10919found_same:
10920 /* We found a matching phys_id, so copy the IRQ info */
10921 cpup->eq = new_cpup->eq;
James Smartd9954a22019-05-21 17:49:05 -070010922
10923 /* Bump start_cpu to the next slot to minmize the
10924 * chance of having multiple unassigned CPU entries
10925 * selecting the same IRQ.
10926 */
10927 start_cpu = cpumask_next(new_cpu, cpu_present_mask);
10928 if (start_cpu == nr_cpumask_bits)
10929 start_cpu = first_cpu;
10930
James Smart657add42019-05-21 17:49:06 -070010931 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smartd9954a22019-05-21 17:49:05 -070010932 "3337 Set Affinity: CPU %d "
James Smartdcaa2132019-11-04 16:57:06 -080010933 "eq %d from peer cpu %d same "
James Smartd9954a22019-05-21 17:49:05 -070010934 "phys_id (%d)\n",
James Smartdcaa2132019-11-04 16:57:06 -080010935 cpu, cpup->eq, new_cpu,
10936 cpup->phys_id);
James Smartd9954a22019-05-21 17:49:05 -070010937 }
10938 }
10939
10940 /* Set any unassigned cpu map entries to a IRQ on any phys_id */
10941 start_cpu = first_cpu;
10942
10943 for_each_present_cpu(cpu) {
10944 cpup = &phba->sli4_hba.cpu_map[cpu];
10945
10946 /* Is this entry unassigned */
10947 if (cpup->eq == LPFC_VECTOR_MAP_EMPTY) {
10948 /* Mark it as IRQ not assigned by the kernel */
10949 cpup->flag |= LPFC_CPU_MAP_UNASSIGN;
10950
James Smart657add42019-05-21 17:49:06 -070010951 /* If so, find a new_cpup thats on ANY phys_id
James Smartd9954a22019-05-21 17:49:05 -070010952 * as the cpup. start_cpu will start where we
10953 * left off so all unassigned entries don't get
10954 * assigned the IRQ of the first entry.
10955 */
10956 new_cpu = start_cpu;
10957 for (i = 0; i < phba->sli4_hba.num_present_cpu; i++) {
10958 new_cpup = &phba->sli4_hba.cpu_map[new_cpu];
10959 if (!(new_cpup->flag & LPFC_CPU_MAP_UNASSIGN) &&
James Smartdcaa2132019-11-04 16:57:06 -080010960 (new_cpup->eq != LPFC_VECTOR_MAP_EMPTY))
James Smartd9954a22019-05-21 17:49:05 -070010961 goto found_any;
10962 new_cpu = cpumask_next(
10963 new_cpu, cpu_present_mask);
10964 if (new_cpu == nr_cpumask_bits)
10965 new_cpu = first_cpu;
10966 }
10967 /* We should never leave an entry unassigned */
10968 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10969 "3339 Set Affinity: CPU %d "
James Smartdcaa2132019-11-04 16:57:06 -080010970 "eq %d UNASSIGNED\n",
10971 cpup->hdwq, cpup->eq);
James Smartd9954a22019-05-21 17:49:05 -070010972 continue;
10973found_any:
10974 /* We found an available entry, copy the IRQ info */
10975 cpup->eq = new_cpup->eq;
James Smartd9954a22019-05-21 17:49:05 -070010976
10977 /* Bump start_cpu to the next slot to minmize the
10978 * chance of having multiple unassigned CPU entries
10979 * selecting the same IRQ.
10980 */
10981 start_cpu = cpumask_next(new_cpu, cpu_present_mask);
10982 if (start_cpu == nr_cpumask_bits)
10983 start_cpu = first_cpu;
10984
James Smart657add42019-05-21 17:49:06 -070010985 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smartd9954a22019-05-21 17:49:05 -070010986 "3338 Set Affinity: CPU %d "
James Smartdcaa2132019-11-04 16:57:06 -080010987 "eq %d from peer cpu %d (%d/%d)\n",
10988 cpu, cpup->eq, new_cpu,
James Smartd9954a22019-05-21 17:49:05 -070010989 new_cpup->phys_id, new_cpup->core_id);
10990 }
10991 }
James Smart657add42019-05-21 17:49:06 -070010992
James Smart3ad348d2019-08-14 16:56:43 -070010993 /* Assign hdwq indices that are unique across all cpus in the map
10994 * that are also FIRST_CPUs.
10995 */
10996 idx = 0;
10997 for_each_present_cpu(cpu) {
10998 cpup = &phba->sli4_hba.cpu_map[cpu];
10999
11000 /* Only FIRST IRQs get a hdwq index assignment. */
11001 if (!(cpup->flag & LPFC_CPU_FIRST_IRQ))
11002 continue;
11003
11004 /* 1 to 1, the first LPFC_CPU_FIRST_IRQ cpus to a unique hdwq */
11005 cpup->hdwq = idx;
11006 idx++;
11007 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11008 "3333 Set Affinity: CPU %d (phys %d core %d): "
James Smartdcaa2132019-11-04 16:57:06 -080011009 "hdwq %d eq %d flg x%x\n",
James Smart3ad348d2019-08-14 16:56:43 -070011010 cpu, cpup->phys_id, cpup->core_id,
James Smartdcaa2132019-11-04 16:57:06 -080011011 cpup->hdwq, cpup->eq, cpup->flag);
James Smart3ad348d2019-08-14 16:56:43 -070011012 }
James Smart657add42019-05-21 17:49:06 -070011013 /* Finally we need to associate a hdwq with each cpu_map entry
11014 * This will be 1 to 1 - hdwq to cpu, unless there are less
11015 * hardware queues then CPUs. For that case we will just round-robin
11016 * the available hardware queues as they get assigned to CPUs.
James Smart3ad348d2019-08-14 16:56:43 -070011017 * The next_idx is the idx from the FIRST_CPU loop above to account
11018 * for irq_chann < hdwq. The idx is used for round-robin assignments
11019 * and needs to start at 0.
James Smart657add42019-05-21 17:49:06 -070011020 */
James Smart3ad348d2019-08-14 16:56:43 -070011021 next_idx = idx;
James Smart657add42019-05-21 17:49:06 -070011022 start_cpu = 0;
James Smart3ad348d2019-08-14 16:56:43 -070011023 idx = 0;
James Smart657add42019-05-21 17:49:06 -070011024 for_each_present_cpu(cpu) {
11025 cpup = &phba->sli4_hba.cpu_map[cpu];
James Smart657add42019-05-21 17:49:06 -070011026
James Smart3ad348d2019-08-14 16:56:43 -070011027 /* FIRST cpus are already mapped. */
11028 if (cpup->flag & LPFC_CPU_FIRST_IRQ)
11029 continue;
James Smart657add42019-05-21 17:49:06 -070011030
James Smart3ad348d2019-08-14 16:56:43 -070011031 /* If the cfg_irq_chann < cfg_hdw_queue, set the hdwq
11032 * of the unassigned cpus to the next idx so that all
11033 * hdw queues are fully utilized.
11034 */
11035 if (next_idx < phba->cfg_hdw_queue) {
11036 cpup->hdwq = next_idx;
11037 next_idx++;
11038 continue;
James Smart657add42019-05-21 17:49:06 -070011039 }
James Smart3ad348d2019-08-14 16:56:43 -070011040
11041 /* Not a First CPU and all hdw_queues are used. Reuse a
11042 * Hardware Queue for another CPU, so be smart about it
11043 * and pick one that has its IRQ/EQ mapped to the same phys_id
11044 * (CPU package) and core_id.
11045 */
11046 new_cpu = start_cpu;
11047 for (i = 0; i < phba->sli4_hba.num_present_cpu; i++) {
11048 new_cpup = &phba->sli4_hba.cpu_map[new_cpu];
11049 if (new_cpup->hdwq != LPFC_VECTOR_MAP_EMPTY &&
11050 new_cpup->phys_id == cpup->phys_id &&
11051 new_cpup->core_id == cpup->core_id) {
11052 goto found_hdwq;
11053 }
11054 new_cpu = cpumask_next(new_cpu, cpu_present_mask);
11055 if (new_cpu == nr_cpumask_bits)
11056 new_cpu = first_cpu;
11057 }
11058
11059 /* If we can't match both phys_id and core_id,
11060 * settle for just a phys_id match.
11061 */
11062 new_cpu = start_cpu;
11063 for (i = 0; i < phba->sli4_hba.num_present_cpu; i++) {
11064 new_cpup = &phba->sli4_hba.cpu_map[new_cpu];
11065 if (new_cpup->hdwq != LPFC_VECTOR_MAP_EMPTY &&
11066 new_cpup->phys_id == cpup->phys_id)
11067 goto found_hdwq;
11068
11069 new_cpu = cpumask_next(new_cpu, cpu_present_mask);
11070 if (new_cpu == nr_cpumask_bits)
11071 new_cpu = first_cpu;
11072 }
11073
11074 /* Otherwise just round robin on cfg_hdw_queue */
11075 cpup->hdwq = idx % phba->cfg_hdw_queue;
11076 idx++;
11077 goto logit;
11078 found_hdwq:
11079 /* We found an available entry, copy the IRQ info */
11080 start_cpu = cpumask_next(new_cpu, cpu_present_mask);
11081 if (start_cpu == nr_cpumask_bits)
11082 start_cpu = first_cpu;
11083 cpup->hdwq = new_cpup->hdwq;
11084 logit:
James Smart657add42019-05-21 17:49:06 -070011085 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11086 "3335 Set Affinity: CPU %d (phys %d core %d): "
James Smartdcaa2132019-11-04 16:57:06 -080011087 "hdwq %d eq %d flg x%x\n",
James Smart657add42019-05-21 17:49:06 -070011088 cpu, cpup->phys_id, cpup->core_id,
James Smartdcaa2132019-11-04 16:57:06 -080011089 cpup->hdwq, cpup->eq, cpup->flag);
James Smart657add42019-05-21 17:49:06 -070011090 }
11091
11092 /* The cpu_map array will be used later during initialization
11093 * when EQ / CQ / WQs are allocated and configured.
11094 */
James Smartb3295c22019-01-28 11:14:30 -080011095 return;
James Smart7bb03bb2013-04-17 20:19:16 -040011096}
James Smart7bb03bb2013-04-17 20:19:16 -040011097
11098/**
James Smart93a4d6f2019-11-04 16:57:05 -080011099 * lpfc_cpuhp_get_eq
11100 *
11101 * @phba: pointer to lpfc hba data structure.
11102 * @cpu: cpu going offline
11103 * @eqlist:
11104 */
11105static void
11106lpfc_cpuhp_get_eq(struct lpfc_hba *phba, unsigned int cpu,
11107 struct list_head *eqlist)
11108{
James Smart93a4d6f2019-11-04 16:57:05 -080011109 const struct cpumask *maskp;
11110 struct lpfc_queue *eq;
James Smart93a4d6f2019-11-04 16:57:05 -080011111 cpumask_t tmp;
11112 u16 idx;
11113
11114 for (idx = 0; idx < phba->cfg_irq_chann; idx++) {
11115 maskp = pci_irq_get_affinity(phba->pcidev, idx);
11116 if (!maskp)
11117 continue;
11118 /*
11119 * if irq is not affinitized to the cpu going
11120 * then we don't need to poll the eq attached
11121 * to it.
11122 */
11123 if (!cpumask_and(&tmp, maskp, cpumask_of(cpu)))
11124 continue;
11125 /* get the cpus that are online and are affini-
11126 * tized to this irq vector. If the count is
11127 * more than 1 then cpuhp is not going to shut-
11128 * down this vector. Since this cpu has not
11129 * gone offline yet, we need >1.
11130 */
11131 cpumask_and(&tmp, maskp, cpu_online_mask);
11132 if (cpumask_weight(&tmp) > 1)
11133 continue;
11134
11135 /* Now that we have an irq to shutdown, get the eq
11136 * mapped to this irq. Note: multiple hdwq's in
11137 * the software can share an eq, but eventually
11138 * only eq will be mapped to this vector
11139 */
James Smartdcaa2132019-11-04 16:57:06 -080011140 eq = phba->sli4_hba.hba_eq_hdl[idx].eq;
11141 list_add(&eq->_poll_list, eqlist);
James Smart93a4d6f2019-11-04 16:57:05 -080011142 }
11143}
11144
11145static void __lpfc_cpuhp_remove(struct lpfc_hba *phba)
11146{
11147 if (phba->sli_rev != LPFC_SLI_REV4)
11148 return;
11149
11150 cpuhp_state_remove_instance_nocalls(lpfc_cpuhp_state,
11151 &phba->cpuhp);
11152 /*
11153 * unregistering the instance doesn't stop the polling
11154 * timer. Wait for the poll timer to retire.
11155 */
11156 synchronize_rcu();
11157 del_timer_sync(&phba->cpuhp_poll_timer);
11158}
11159
11160static void lpfc_cpuhp_remove(struct lpfc_hba *phba)
11161{
11162 if (phba->pport->fc_flag & FC_OFFLINE_MODE)
11163 return;
11164
11165 __lpfc_cpuhp_remove(phba);
11166}
11167
11168static void lpfc_cpuhp_add(struct lpfc_hba *phba)
11169{
11170 if (phba->sli_rev != LPFC_SLI_REV4)
11171 return;
11172
11173 rcu_read_lock();
11174
11175 if (!list_empty(&phba->poll_list)) {
11176 timer_setup(&phba->cpuhp_poll_timer, lpfc_sli4_poll_hbtimer, 0);
11177 mod_timer(&phba->cpuhp_poll_timer,
11178 jiffies + msecs_to_jiffies(LPFC_POLL_HB));
11179 }
11180
11181 rcu_read_unlock();
11182
11183 cpuhp_state_add_instance_nocalls(lpfc_cpuhp_state,
11184 &phba->cpuhp);
11185}
11186
11187static int __lpfc_cpuhp_checks(struct lpfc_hba *phba, int *retval)
11188{
11189 if (phba->pport->load_flag & FC_UNLOADING) {
11190 *retval = -EAGAIN;
11191 return true;
11192 }
11193
11194 if (phba->sli_rev != LPFC_SLI_REV4) {
11195 *retval = 0;
11196 return true;
11197 }
11198
11199 /* proceed with the hotplug */
11200 return false;
11201}
11202
James Smartdcaa2132019-11-04 16:57:06 -080011203/**
11204 * lpfc_irq_set_aff - set IRQ affinity
11205 * @eqhdl: EQ handle
11206 * @cpu: cpu to set affinity
11207 *
11208 **/
11209static inline void
11210lpfc_irq_set_aff(struct lpfc_hba_eq_hdl *eqhdl, unsigned int cpu)
11211{
11212 cpumask_clear(&eqhdl->aff_mask);
11213 cpumask_set_cpu(cpu, &eqhdl->aff_mask);
11214 irq_set_status_flags(eqhdl->irq, IRQ_NO_BALANCING);
11215 irq_set_affinity_hint(eqhdl->irq, &eqhdl->aff_mask);
11216}
11217
11218/**
11219 * lpfc_irq_clear_aff - clear IRQ affinity
11220 * @eqhdl: EQ handle
11221 *
11222 **/
11223static inline void
11224lpfc_irq_clear_aff(struct lpfc_hba_eq_hdl *eqhdl)
11225{
11226 cpumask_clear(&eqhdl->aff_mask);
11227 irq_clear_status_flags(eqhdl->irq, IRQ_NO_BALANCING);
11228 irq_set_affinity_hint(eqhdl->irq, &eqhdl->aff_mask);
11229}
11230
11231/**
11232 * lpfc_irq_rebalance - rebalances IRQ affinity according to cpuhp event
11233 * @phba: pointer to HBA context object.
11234 * @cpu: cpu going offline/online
11235 * @offline: true, cpu is going offline. false, cpu is coming online.
11236 *
11237 * If cpu is going offline, we'll try our best effort to find the next
11238 * online cpu on the phba's NUMA node and migrate all offlining IRQ affinities.
11239 *
11240 * If cpu is coming online, reaffinitize the IRQ back to the onlineng cpu.
11241 *
11242 * Note: Call only if cfg_irq_numa is enabled, otherwise rely on
11243 * PCI_IRQ_AFFINITY to auto-manage IRQ affinity.
11244 *
11245 **/
11246static void
11247lpfc_irq_rebalance(struct lpfc_hba *phba, unsigned int cpu, bool offline)
11248{
11249 struct lpfc_vector_map_info *cpup;
11250 struct cpumask *aff_mask;
11251 unsigned int cpu_select, cpu_next, idx;
11252 const struct cpumask *numa_mask;
11253
11254 if (!phba->cfg_irq_numa)
11255 return;
11256
11257 numa_mask = &phba->sli4_hba.numa_mask;
11258
11259 if (!cpumask_test_cpu(cpu, numa_mask))
11260 return;
11261
11262 cpup = &phba->sli4_hba.cpu_map[cpu];
11263
11264 if (!(cpup->flag & LPFC_CPU_FIRST_IRQ))
11265 return;
11266
11267 if (offline) {
11268 /* Find next online CPU on NUMA node */
11269 cpu_next = cpumask_next_wrap(cpu, numa_mask, cpu, true);
11270 cpu_select = lpfc_next_online_numa_cpu(numa_mask, cpu_next);
11271
11272 /* Found a valid CPU */
11273 if ((cpu_select < nr_cpu_ids) && (cpu_select != cpu)) {
11274 /* Go through each eqhdl and ensure offlining
11275 * cpu aff_mask is migrated
11276 */
11277 for (idx = 0; idx < phba->cfg_irq_chann; idx++) {
11278 aff_mask = lpfc_get_aff_mask(idx);
11279
11280 /* Migrate affinity */
11281 if (cpumask_test_cpu(cpu, aff_mask))
11282 lpfc_irq_set_aff(lpfc_get_eq_hdl(idx),
11283 cpu_select);
11284 }
11285 } else {
11286 /* Rely on irqbalance if no online CPUs left on NUMA */
11287 for (idx = 0; idx < phba->cfg_irq_chann; idx++)
11288 lpfc_irq_clear_aff(lpfc_get_eq_hdl(idx));
11289 }
11290 } else {
11291 /* Migrate affinity back to this CPU */
11292 lpfc_irq_set_aff(lpfc_get_eq_hdl(cpup->eq), cpu);
11293 }
11294}
11295
James Smart93a4d6f2019-11-04 16:57:05 -080011296static int lpfc_cpu_offline(unsigned int cpu, struct hlist_node *node)
11297{
11298 struct lpfc_hba *phba = hlist_entry_safe(node, struct lpfc_hba, cpuhp);
11299 struct lpfc_queue *eq, *next;
11300 LIST_HEAD(eqlist);
11301 int retval;
11302
11303 if (!phba) {
11304 WARN_ONCE(!phba, "cpu: %u. phba:NULL", raw_smp_processor_id());
11305 return 0;
11306 }
11307
11308 if (__lpfc_cpuhp_checks(phba, &retval))
11309 return retval;
11310
James Smartdcaa2132019-11-04 16:57:06 -080011311 lpfc_irq_rebalance(phba, cpu, true);
11312
James Smart93a4d6f2019-11-04 16:57:05 -080011313 lpfc_cpuhp_get_eq(phba, cpu, &eqlist);
11314
11315 /* start polling on these eq's */
11316 list_for_each_entry_safe(eq, next, &eqlist, _poll_list) {
11317 list_del_init(&eq->_poll_list);
11318 lpfc_sli4_start_polling(eq);
11319 }
11320
11321 return 0;
11322}
11323
11324static int lpfc_cpu_online(unsigned int cpu, struct hlist_node *node)
11325{
11326 struct lpfc_hba *phba = hlist_entry_safe(node, struct lpfc_hba, cpuhp);
11327 struct lpfc_queue *eq, *next;
11328 unsigned int n;
11329 int retval;
11330
11331 if (!phba) {
11332 WARN_ONCE(!phba, "cpu: %u. phba:NULL", raw_smp_processor_id());
11333 return 0;
11334 }
11335
11336 if (__lpfc_cpuhp_checks(phba, &retval))
11337 return retval;
11338
James Smartdcaa2132019-11-04 16:57:06 -080011339 lpfc_irq_rebalance(phba, cpu, false);
11340
James Smart93a4d6f2019-11-04 16:57:05 -080011341 list_for_each_entry_safe(eq, next, &phba->poll_list, _poll_list) {
11342 n = lpfc_find_cpu_handle(phba, eq->hdwq, LPFC_FIND_BY_HDWQ);
11343 if (n == cpu)
11344 lpfc_sli4_stop_polling(eq);
11345 }
11346
11347 return 0;
11348}
11349
11350/**
James Smartda0436e2009-05-22 14:51:39 -040011351 * lpfc_sli4_enable_msix - Enable MSI-X interrupt mode to SLI-4 device
11352 * @phba: pointer to lpfc hba data structure.
11353 *
11354 * This routine is invoked to enable the MSI-X interrupt vectors to device
James Smartdcaa2132019-11-04 16:57:06 -080011355 * with SLI-4 interface spec. It also allocates MSI-X vectors and maps them
11356 * to cpus on the system.
11357 *
11358 * When cfg_irq_numa is enabled, the adapter will only allocate vectors for
11359 * the number of cpus on the same numa node as this adapter. The vectors are
11360 * allocated without requesting OS affinity mapping. A vector will be
11361 * allocated and assigned to each online and offline cpu. If the cpu is
11362 * online, then affinity will be set to that cpu. If the cpu is offline, then
11363 * affinity will be set to the nearest peer cpu within the numa node that is
11364 * online. If there are no online cpus within the numa node, affinity is not
11365 * assigned and the OS may do as it pleases. Note: cpu vector affinity mapping
11366 * is consistent with the way cpu online/offline is handled when cfg_irq_numa is
11367 * configured.
11368 *
11369 * If numa mode is not enabled and there is more than 1 vector allocated, then
11370 * the driver relies on the managed irq interface where the OS assigns vector to
11371 * cpu affinity. The driver will then use that affinity mapping to setup its
11372 * cpu mapping table.
James Smartda0436e2009-05-22 14:51:39 -040011373 *
11374 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020011375 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -040011376 * other values - error
11377 **/
11378static int
11379lpfc_sli4_enable_msix(struct lpfc_hba *phba)
11380{
James Smart75baf692010-06-08 18:31:21 -040011381 int vectors, rc, index;
James Smartb83d0052017-06-01 21:07:05 -070011382 char *name;
James Smartdcaa2132019-11-04 16:57:06 -080011383 const struct cpumask *numa_mask = NULL;
11384 unsigned int cpu = 0, cpu_cnt = 0, cpu_select = nr_cpu_ids;
11385 struct lpfc_hba_eq_hdl *eqhdl;
11386 const struct cpumask *maskp;
11387 bool first;
11388 unsigned int flags = PCI_IRQ_MSIX;
James Smartda0436e2009-05-22 14:51:39 -040011389
11390 /* Set up MSI-X multi-message vectors */
James Smart6a828b02019-01-28 11:14:31 -080011391 vectors = phba->cfg_irq_chann;
Christoph Hellwig45ffac12017-02-12 13:52:26 -080011392
James Smartdcaa2132019-11-04 16:57:06 -080011393 if (phba->cfg_irq_numa) {
11394 numa_mask = &phba->sli4_hba.numa_mask;
11395 cpu_cnt = cpumask_weight(numa_mask);
11396 vectors = min(phba->cfg_irq_chann, cpu_cnt);
11397
11398 /* cpu: iterates over numa_mask including offline or online
11399 * cpu_select: iterates over online numa_mask to set affinity
11400 */
11401 cpu = cpumask_first(numa_mask);
11402 cpu_select = lpfc_next_online_numa_cpu(numa_mask, cpu);
11403 } else {
11404 flags |= PCI_IRQ_AFFINITY;
11405 }
11406
11407 rc = pci_alloc_irq_vectors(phba->pcidev, 1, vectors, flags);
Alexander Gordeev4f871e12014-09-03 12:56:29 -040011408 if (rc < 0) {
James Smartda0436e2009-05-22 14:51:39 -040011409 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
11410 "0484 PCI enable MSI-X failed (%d)\n", rc);
Alexander Gordeev029165a2014-07-16 20:05:15 +020011411 goto vec_fail_out;
James Smartda0436e2009-05-22 14:51:39 -040011412 }
Alexander Gordeev4f871e12014-09-03 12:56:29 -040011413 vectors = rc;
James Smart75baf692010-06-08 18:31:21 -040011414
James Smart7bb03bb2013-04-17 20:19:16 -040011415 /* Assign MSI-X vectors to interrupt handlers */
James Smart67d12732012-08-03 12:36:13 -040011416 for (index = 0; index < vectors; index++) {
James Smartdcaa2132019-11-04 16:57:06 -080011417 eqhdl = lpfc_get_eq_hdl(index);
11418 name = eqhdl->handler_name;
James Smartb83d0052017-06-01 21:07:05 -070011419 memset(name, 0, LPFC_SLI4_HANDLER_NAME_SZ);
11420 snprintf(name, LPFC_SLI4_HANDLER_NAME_SZ,
James Smart4305f182012-08-03 12:36:33 -040011421 LPFC_DRIVER_HANDLER_NAME"%d", index);
James Smartda0436e2009-05-22 14:51:39 -040011422
James Smartdcaa2132019-11-04 16:57:06 -080011423 eqhdl->idx = index;
James Smart7370d102019-01-28 11:14:20 -080011424 rc = request_irq(pci_irq_vector(phba->pcidev, index),
11425 &lpfc_sli4_hba_intr_handler, 0,
James Smartdcaa2132019-11-04 16:57:06 -080011426 name, eqhdl);
James Smartda0436e2009-05-22 14:51:39 -040011427 if (rc) {
11428 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
11429 "0486 MSI-X fast-path (%d) "
11430 "request_irq failed (%d)\n", index, rc);
11431 goto cfg_fail_out;
11432 }
James Smartdcaa2132019-11-04 16:57:06 -080011433
11434 eqhdl->irq = pci_irq_vector(phba->pcidev, index);
11435
11436 if (phba->cfg_irq_numa) {
11437 /* If found a neighboring online cpu, set affinity */
11438 if (cpu_select < nr_cpu_ids)
11439 lpfc_irq_set_aff(eqhdl, cpu_select);
11440
11441 /* Assign EQ to cpu_map */
11442 lpfc_assign_eq_map_info(phba, index,
11443 LPFC_CPU_FIRST_IRQ,
11444 cpu);
11445
11446 /* Iterate to next offline or online cpu in numa_mask */
11447 cpu = cpumask_next(cpu, numa_mask);
11448
11449 /* Find next online cpu in numa_mask to set affinity */
11450 cpu_select = lpfc_next_online_numa_cpu(numa_mask, cpu);
11451 } else if (vectors == 1) {
11452 cpu = cpumask_first(cpu_present_mask);
11453 lpfc_assign_eq_map_info(phba, index, LPFC_CPU_FIRST_IRQ,
11454 cpu);
11455 } else {
11456 maskp = pci_irq_get_affinity(phba->pcidev, index);
11457
11458 first = true;
11459 /* Loop through all CPUs associated with vector index */
11460 for_each_cpu_and(cpu, maskp, cpu_present_mask) {
11461 /* If this is the first CPU thats assigned to
11462 * this vector, set LPFC_CPU_FIRST_IRQ.
11463 */
11464 lpfc_assign_eq_map_info(phba, index,
11465 first ?
11466 LPFC_CPU_FIRST_IRQ : 0,
11467 cpu);
11468 if (first)
11469 first = false;
11470 }
11471 }
James Smartda0436e2009-05-22 14:51:39 -040011472 }
11473
James Smart6a828b02019-01-28 11:14:31 -080011474 if (vectors != phba->cfg_irq_chann) {
James Smart82c3e9b2012-09-29 11:29:50 -040011475 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11476 "3238 Reducing IO channels to match number of "
11477 "MSI-X vectors, requested %d got %d\n",
James Smart6a828b02019-01-28 11:14:31 -080011478 phba->cfg_irq_chann, vectors);
11479 if (phba->cfg_irq_chann > vectors)
11480 phba->cfg_irq_chann = vectors;
James Smart82c3e9b2012-09-29 11:29:50 -040011481 }
James Smart7bb03bb2013-04-17 20:19:16 -040011482
James Smartda0436e2009-05-22 14:51:39 -040011483 return rc;
11484
11485cfg_fail_out:
11486 /* free the irq already requested */
James Smartdcaa2132019-11-04 16:57:06 -080011487 for (--index; index >= 0; index--) {
11488 eqhdl = lpfc_get_eq_hdl(index);
11489 lpfc_irq_clear_aff(eqhdl);
11490 irq_set_affinity_hint(eqhdl->irq, NULL);
11491 free_irq(eqhdl->irq, eqhdl);
11492 }
James Smartda0436e2009-05-22 14:51:39 -040011493
James Smartda0436e2009-05-22 14:51:39 -040011494 /* Unconfigure MSI-X capability structure */
Christoph Hellwig45ffac12017-02-12 13:52:26 -080011495 pci_free_irq_vectors(phba->pcidev);
Alexander Gordeev029165a2014-07-16 20:05:15 +020011496
11497vec_fail_out:
James Smartda0436e2009-05-22 14:51:39 -040011498 return rc;
11499}
11500
11501/**
James Smartda0436e2009-05-22 14:51:39 -040011502 * lpfc_sli4_enable_msi - Enable MSI interrupt mode to SLI-4 device
11503 * @phba: pointer to lpfc hba data structure.
11504 *
11505 * This routine is invoked to enable the MSI interrupt mode to device with
James Smart07b1b912019-08-14 16:56:58 -070011506 * SLI-4 interface spec. The kernel function pci_alloc_irq_vectors() is
11507 * called to enable the MSI vector. The device driver is responsible for
11508 * calling the request_irq() to register MSI vector with a interrupt the
11509 * handler, which is done in this function.
James Smartda0436e2009-05-22 14:51:39 -040011510 *
11511 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020011512 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -040011513 * other values - error
11514 **/
11515static int
11516lpfc_sli4_enable_msi(struct lpfc_hba *phba)
11517{
11518 int rc, index;
James Smartdcaa2132019-11-04 16:57:06 -080011519 unsigned int cpu;
11520 struct lpfc_hba_eq_hdl *eqhdl;
James Smartda0436e2009-05-22 14:51:39 -040011521
James Smart07b1b912019-08-14 16:56:58 -070011522 rc = pci_alloc_irq_vectors(phba->pcidev, 1, 1,
11523 PCI_IRQ_MSI | PCI_IRQ_AFFINITY);
11524 if (rc > 0)
James Smartda0436e2009-05-22 14:51:39 -040011525 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
11526 "0487 PCI enable MSI mode success.\n");
11527 else {
11528 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
11529 "0488 PCI enable MSI mode failed (%d)\n", rc);
James Smart07b1b912019-08-14 16:56:58 -070011530 return rc ? rc : -1;
James Smartda0436e2009-05-22 14:51:39 -040011531 }
11532
11533 rc = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
James Smarted243d32015-05-21 13:55:25 -040011534 0, LPFC_DRIVER_NAME, phba);
James Smartda0436e2009-05-22 14:51:39 -040011535 if (rc) {
James Smart07b1b912019-08-14 16:56:58 -070011536 pci_free_irq_vectors(phba->pcidev);
James Smartda0436e2009-05-22 14:51:39 -040011537 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
11538 "0490 MSI request_irq failed (%d)\n", rc);
James Smart75baf692010-06-08 18:31:21 -040011539 return rc;
James Smartda0436e2009-05-22 14:51:39 -040011540 }
11541
James Smartdcaa2132019-11-04 16:57:06 -080011542 eqhdl = lpfc_get_eq_hdl(0);
11543 eqhdl->irq = pci_irq_vector(phba->pcidev, 0);
11544
11545 cpu = cpumask_first(cpu_present_mask);
11546 lpfc_assign_eq_map_info(phba, 0, LPFC_CPU_FIRST_IRQ, cpu);
11547
James Smart6a828b02019-01-28 11:14:31 -080011548 for (index = 0; index < phba->cfg_irq_chann; index++) {
James Smartdcaa2132019-11-04 16:57:06 -080011549 eqhdl = lpfc_get_eq_hdl(index);
11550 eqhdl->idx = index;
James Smartda0436e2009-05-22 14:51:39 -040011551 }
11552
James Smart75baf692010-06-08 18:31:21 -040011553 return 0;
James Smartda0436e2009-05-22 14:51:39 -040011554}
11555
11556/**
James Smartda0436e2009-05-22 14:51:39 -040011557 * lpfc_sli4_enable_intr - Enable device interrupt to SLI-4 device
11558 * @phba: pointer to lpfc hba data structure.
11559 *
11560 * This routine is invoked to enable device interrupt and associate driver's
11561 * interrupt handler(s) to interrupt vector(s) to device with SLI-4
11562 * interface spec. Depends on the interrupt mode configured to the driver,
11563 * the driver will try to fallback from the configured interrupt mode to an
11564 * interrupt mode which is supported by the platform, kernel, and device in
11565 * the order of:
11566 * MSI-X -> MSI -> IRQ.
11567 *
11568 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020011569 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -040011570 * other values - error
11571 **/
11572static uint32_t
11573lpfc_sli4_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
11574{
11575 uint32_t intr_mode = LPFC_INTR_ERROR;
James Smart895427b2017-02-12 13:52:30 -080011576 int retval, idx;
James Smartda0436e2009-05-22 14:51:39 -040011577
11578 if (cfg_mode == 2) {
11579 /* Preparation before conf_msi mbox cmd */
11580 retval = 0;
11581 if (!retval) {
11582 /* Now, try to enable MSI-X interrupt mode */
James Smartdcaa2132019-11-04 16:57:06 -080011583 get_online_cpus();
James Smartda0436e2009-05-22 14:51:39 -040011584 retval = lpfc_sli4_enable_msix(phba);
James Smartdcaa2132019-11-04 16:57:06 -080011585 put_online_cpus();
James Smartda0436e2009-05-22 14:51:39 -040011586 if (!retval) {
11587 /* Indicate initialization to MSI-X mode */
11588 phba->intr_type = MSIX;
11589 intr_mode = 2;
11590 }
11591 }
11592 }
11593
11594 /* Fallback to MSI if MSI-X initialization failed */
11595 if (cfg_mode >= 1 && phba->intr_type == NONE) {
11596 retval = lpfc_sli4_enable_msi(phba);
11597 if (!retval) {
11598 /* Indicate initialization to MSI mode */
11599 phba->intr_type = MSI;
11600 intr_mode = 1;
11601 }
11602 }
11603
11604 /* Fallback to INTx if both MSI-X/MSI initalization failed */
11605 if (phba->intr_type == NONE) {
11606 retval = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
11607 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
11608 if (!retval) {
James Smart895427b2017-02-12 13:52:30 -080011609 struct lpfc_hba_eq_hdl *eqhdl;
James Smartdcaa2132019-11-04 16:57:06 -080011610 unsigned int cpu;
James Smart895427b2017-02-12 13:52:30 -080011611
James Smartda0436e2009-05-22 14:51:39 -040011612 /* Indicate initialization to INTx mode */
11613 phba->intr_type = INTx;
11614 intr_mode = 0;
James Smart895427b2017-02-12 13:52:30 -080011615
James Smartdcaa2132019-11-04 16:57:06 -080011616 eqhdl = lpfc_get_eq_hdl(0);
11617 eqhdl->irq = pci_irq_vector(phba->pcidev, 0);
11618
11619 cpu = cpumask_first(cpu_present_mask);
11620 lpfc_assign_eq_map_info(phba, 0, LPFC_CPU_FIRST_IRQ,
11621 cpu);
James Smart6a828b02019-01-28 11:14:31 -080011622 for (idx = 0; idx < phba->cfg_irq_chann; idx++) {
James Smartdcaa2132019-11-04 16:57:06 -080011623 eqhdl = lpfc_get_eq_hdl(idx);
James Smart895427b2017-02-12 13:52:30 -080011624 eqhdl->idx = idx;
James Smart1ba981f2014-02-20 09:56:45 -050011625 }
James Smartda0436e2009-05-22 14:51:39 -040011626 }
11627 }
11628 return intr_mode;
11629}
11630
11631/**
11632 * lpfc_sli4_disable_intr - Disable device interrupt to SLI-4 device
11633 * @phba: pointer to lpfc hba data structure.
11634 *
11635 * This routine is invoked to disable device interrupt and disassociate
11636 * the driver's interrupt handler(s) from interrupt vector(s) to device
11637 * with SLI-4 interface spec. Depending on the interrupt mode, the driver
11638 * will release the interrupt vector(s) for the message signaled interrupt.
11639 **/
11640static void
11641lpfc_sli4_disable_intr(struct lpfc_hba *phba)
11642{
11643 /* Disable the currently initialized interrupt mode */
Christoph Hellwig45ffac12017-02-12 13:52:26 -080011644 if (phba->intr_type == MSIX) {
11645 int index;
James Smartdcaa2132019-11-04 16:57:06 -080011646 struct lpfc_hba_eq_hdl *eqhdl;
Christoph Hellwig45ffac12017-02-12 13:52:26 -080011647
11648 /* Free up MSI-X multi-message vectors */
James Smart6a828b02019-01-28 11:14:31 -080011649 for (index = 0; index < phba->cfg_irq_chann; index++) {
James Smartdcaa2132019-11-04 16:57:06 -080011650 eqhdl = lpfc_get_eq_hdl(index);
11651 lpfc_irq_clear_aff(eqhdl);
11652 irq_set_affinity_hint(eqhdl->irq, NULL);
11653 free_irq(eqhdl->irq, eqhdl);
James Smartb3295c22019-01-28 11:14:30 -080011654 }
Christoph Hellwig45ffac12017-02-12 13:52:26 -080011655 } else {
James Smartda0436e2009-05-22 14:51:39 -040011656 free_irq(phba->pcidev->irq, phba);
Christoph Hellwig45ffac12017-02-12 13:52:26 -080011657 }
11658
11659 pci_free_irq_vectors(phba->pcidev);
James Smartda0436e2009-05-22 14:51:39 -040011660
11661 /* Reset interrupt management states */
11662 phba->intr_type = NONE;
11663 phba->sli.slistat.sli_intr = 0;
James Smartda0436e2009-05-22 14:51:39 -040011664}
11665
11666/**
James Smart3772a992009-05-22 14:50:54 -040011667 * lpfc_unset_hba - Unset SLI3 hba device initialization
11668 * @phba: pointer to lpfc hba data structure.
11669 *
11670 * This routine is invoked to unset the HBA device initialization steps to
11671 * a device with SLI-3 interface spec.
11672 **/
11673static void
11674lpfc_unset_hba(struct lpfc_hba *phba)
11675{
11676 struct lpfc_vport *vport = phba->pport;
11677 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
11678
11679 spin_lock_irq(shost->host_lock);
11680 vport->load_flag |= FC_UNLOADING;
11681 spin_unlock_irq(shost->host_lock);
11682
James Smart72859902012-01-18 16:25:38 -050011683 kfree(phba->vpi_bmask);
11684 kfree(phba->vpi_ids);
11685
James Smart3772a992009-05-22 14:50:54 -040011686 lpfc_stop_hba_timers(phba);
11687
11688 phba->pport->work_port_events = 0;
11689
11690 lpfc_sli_hba_down(phba);
11691
11692 lpfc_sli_brdrestart(phba);
11693
11694 lpfc_sli_disable_intr(phba);
11695
11696 return;
11697}
11698
11699/**
James Smart5af5eee2010-10-22 11:06:38 -040011700 * lpfc_sli4_xri_exchange_busy_wait - Wait for device XRI exchange busy
11701 * @phba: Pointer to HBA context object.
11702 *
11703 * This function is called in the SLI4 code path to wait for completion
11704 * of device's XRIs exchange busy. It will check the XRI exchange busy
11705 * on outstanding FCP and ELS I/Os every 10ms for up to 10 seconds; after
11706 * that, it will check the XRI exchange busy on outstanding FCP and ELS
11707 * I/Os every 30 seconds, log error message, and wait forever. Only when
11708 * all XRI exchange busy complete, the driver unload shall proceed with
11709 * invoking the function reset ioctl mailbox command to the CNA and the
11710 * the rest of the driver unload resource release.
11711 **/
11712static void
11713lpfc_sli4_xri_exchange_busy_wait(struct lpfc_hba *phba)
11714{
James Smart5e5b5112019-01-28 11:14:22 -080011715 struct lpfc_sli4_hdw_queue *qp;
James Smartc00f62e2019-08-14 16:57:11 -070011716 int idx, ccnt;
James Smart5af5eee2010-10-22 11:06:38 -040011717 int wait_time = 0;
James Smart5e5b5112019-01-28 11:14:22 -080011718 int io_xri_cmpl = 1;
James Smart86c67372017-04-21 16:05:04 -070011719 int nvmet_xri_cmpl = 1;
James Smart5af5eee2010-10-22 11:06:38 -040011720 int els_xri_cmpl = list_empty(&phba->sli4_hba.lpfc_abts_els_sgl_list);
11721
James Smartc3725bd2017-11-20 16:00:42 -080011722 /* Driver just aborted IOs during the hba_unset process. Pause
11723 * here to give the HBA time to complete the IO and get entries
11724 * into the abts lists.
11725 */
11726 msleep(LPFC_XRI_EXCH_BUSY_WAIT_T1 * 5);
11727
11728 /* Wait for NVME pending IO to flush back to transport. */
11729 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
11730 lpfc_nvme_wait_for_io_drain(phba);
11731
James Smart5e5b5112019-01-28 11:14:22 -080011732 ccnt = 0;
James Smart5e5b5112019-01-28 11:14:22 -080011733 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
11734 qp = &phba->sli4_hba.hdwq[idx];
James Smartc00f62e2019-08-14 16:57:11 -070011735 io_xri_cmpl = list_empty(&qp->lpfc_abts_io_buf_list);
11736 if (!io_xri_cmpl) /* if list is NOT empty */
11737 ccnt++;
James Smart5e5b5112019-01-28 11:14:22 -080011738 }
11739 if (ccnt)
11740 io_xri_cmpl = 0;
James Smart5e5b5112019-01-28 11:14:22 -080011741
James Smart86c67372017-04-21 16:05:04 -070011742 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
James Smart86c67372017-04-21 16:05:04 -070011743 nvmet_xri_cmpl =
11744 list_empty(&phba->sli4_hba.lpfc_abts_nvmet_ctx_list);
11745 }
James Smart895427b2017-02-12 13:52:30 -080011746
James Smartc00f62e2019-08-14 16:57:11 -070011747 while (!els_xri_cmpl || !io_xri_cmpl || !nvmet_xri_cmpl) {
James Smart5af5eee2010-10-22 11:06:38 -040011748 if (wait_time > LPFC_XRI_EXCH_BUSY_WAIT_TMO) {
James Smart68c9b552018-06-26 08:24:25 -070011749 if (!nvmet_xri_cmpl)
11750 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11751 "6424 NVMET XRI exchange busy "
11752 "wait time: %d seconds.\n",
11753 wait_time/1000);
James Smart5e5b5112019-01-28 11:14:22 -080011754 if (!io_xri_cmpl)
James Smart895427b2017-02-12 13:52:30 -080011755 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartc00f62e2019-08-14 16:57:11 -070011756 "6100 IO XRI exchange busy "
James Smart5af5eee2010-10-22 11:06:38 -040011757 "wait time: %d seconds.\n",
11758 wait_time/1000);
11759 if (!els_xri_cmpl)
11760 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11761 "2878 ELS XRI exchange busy "
11762 "wait time: %d seconds.\n",
11763 wait_time/1000);
11764 msleep(LPFC_XRI_EXCH_BUSY_WAIT_T2);
11765 wait_time += LPFC_XRI_EXCH_BUSY_WAIT_T2;
11766 } else {
11767 msleep(LPFC_XRI_EXCH_BUSY_WAIT_T1);
11768 wait_time += LPFC_XRI_EXCH_BUSY_WAIT_T1;
11769 }
James Smart5e5b5112019-01-28 11:14:22 -080011770
11771 ccnt = 0;
James Smart5e5b5112019-01-28 11:14:22 -080011772 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
11773 qp = &phba->sli4_hba.hdwq[idx];
James Smartc00f62e2019-08-14 16:57:11 -070011774 io_xri_cmpl = list_empty(
11775 &qp->lpfc_abts_io_buf_list);
11776 if (!io_xri_cmpl) /* if list is NOT empty */
11777 ccnt++;
James Smart5e5b5112019-01-28 11:14:22 -080011778 }
11779 if (ccnt)
11780 io_xri_cmpl = 0;
James Smart5e5b5112019-01-28 11:14:22 -080011781
James Smart86c67372017-04-21 16:05:04 -070011782 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
James Smart86c67372017-04-21 16:05:04 -070011783 nvmet_xri_cmpl = list_empty(
11784 &phba->sli4_hba.lpfc_abts_nvmet_ctx_list);
11785 }
James Smart5af5eee2010-10-22 11:06:38 -040011786 els_xri_cmpl =
11787 list_empty(&phba->sli4_hba.lpfc_abts_els_sgl_list);
James Smartf358dd02017-02-12 13:52:34 -080011788
James Smart5af5eee2010-10-22 11:06:38 -040011789 }
11790}
11791
11792/**
James Smartda0436e2009-05-22 14:51:39 -040011793 * lpfc_sli4_hba_unset - Unset the fcoe hba
11794 * @phba: Pointer to HBA context object.
11795 *
11796 * This function is called in the SLI4 code path to reset the HBA's FCoE
11797 * function. The caller is not required to hold any lock. This routine
11798 * issues PCI function reset mailbox command to reset the FCoE function.
11799 * At the end of the function, it calls lpfc_hba_down_post function to
11800 * free any pending commands.
11801 **/
11802static void
11803lpfc_sli4_hba_unset(struct lpfc_hba *phba)
11804{
11805 int wait_cnt = 0;
11806 LPFC_MBOXQ_t *mboxq;
James Smart912e3ac2011-05-24 11:42:11 -040011807 struct pci_dev *pdev = phba->pcidev;
James Smartda0436e2009-05-22 14:51:39 -040011808
11809 lpfc_stop_hba_timers(phba);
James Smartcdb42be2019-01-28 11:14:21 -080011810 if (phba->pport)
11811 phba->sli4_hba.intr_enable = 0;
James Smartda0436e2009-05-22 14:51:39 -040011812
11813 /*
11814 * Gracefully wait out the potential current outstanding asynchronous
11815 * mailbox command.
11816 */
11817
11818 /* First, block any pending async mailbox command from posted */
11819 spin_lock_irq(&phba->hbalock);
11820 phba->sli.sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
11821 spin_unlock_irq(&phba->hbalock);
11822 /* Now, trying to wait it out if we can */
11823 while (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
11824 msleep(10);
11825 if (++wait_cnt > LPFC_ACTIVE_MBOX_WAIT_CNT)
11826 break;
11827 }
11828 /* Forcefully release the outstanding mailbox command if timed out */
11829 if (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
11830 spin_lock_irq(&phba->hbalock);
11831 mboxq = phba->sli.mbox_active;
11832 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
11833 __lpfc_mbox_cmpl_put(phba, mboxq);
11834 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
11835 phba->sli.mbox_active = NULL;
11836 spin_unlock_irq(&phba->hbalock);
11837 }
11838
James Smart5af5eee2010-10-22 11:06:38 -040011839 /* Abort all iocbs associated with the hba */
11840 lpfc_sli_hba_iocb_abort(phba);
11841
11842 /* Wait for completion of device XRI exchange busy */
11843 lpfc_sli4_xri_exchange_busy_wait(phba);
11844
James Smart93a4d6f2019-11-04 16:57:05 -080011845 /* per-phba callback de-registration for hotplug event */
11846 lpfc_cpuhp_remove(phba);
11847
James Smartda0436e2009-05-22 14:51:39 -040011848 /* Disable PCI subsystem interrupt */
11849 lpfc_sli4_disable_intr(phba);
11850
James Smart912e3ac2011-05-24 11:42:11 -040011851 /* Disable SR-IOV if enabled */
11852 if (phba->cfg_sriov_nr_virtfn)
11853 pci_disable_sriov(pdev);
11854
James Smartda0436e2009-05-22 14:51:39 -040011855 /* Stop kthread signal shall trigger work_done one more time */
11856 kthread_stop(phba->worker_thread);
11857
James Smartd2cc9bc2018-09-10 10:30:50 -070011858 /* Disable FW logging to host memory */
James Smart1165a5c2018-11-29 16:09:39 -080011859 lpfc_ras_stop_fwlog(phba);
James Smartd2cc9bc2018-09-10 10:30:50 -070011860
James Smartd1f525a2017-04-21 16:04:55 -070011861 /* Unset the queues shared with the hardware then release all
11862 * allocated resources.
11863 */
11864 lpfc_sli4_queue_unset(phba);
11865 lpfc_sli4_queue_destroy(phba);
11866
James Smart3677a3a2010-09-29 11:19:14 -040011867 /* Reset SLI4 HBA FCoE function */
11868 lpfc_pci_function_reset(phba);
11869
James Smart1165a5c2018-11-29 16:09:39 -080011870 /* Free RAS DMA memory */
11871 if (phba->ras_fwlog.ras_enabled)
11872 lpfc_sli4_ras_dma_free(phba);
11873
James Smartda0436e2009-05-22 14:51:39 -040011874 /* Stop the SLI4 device port */
James Smart1ffdd2c2019-03-04 15:27:51 -080011875 if (phba->pport)
11876 phba->pport->work_port_events = 0;
James Smartda0436e2009-05-22 14:51:39 -040011877}
11878
James Smart28baac72010-02-12 14:42:03 -050011879 /**
11880 * lpfc_pc_sli4_params_get - Get the SLI4_PARAMS port capabilities.
11881 * @phba: Pointer to HBA context object.
11882 * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
11883 *
11884 * This function is called in the SLI4 code path to read the port's
11885 * sli4 capabilities.
11886 *
11887 * This function may be be called from any context that can block-wait
11888 * for the completion. The expectation is that this routine is called
11889 * typically from probe_one or from the online routine.
11890 **/
11891int
11892lpfc_pc_sli4_params_get(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
11893{
11894 int rc;
11895 struct lpfc_mqe *mqe;
11896 struct lpfc_pc_sli4_params *sli4_params;
11897 uint32_t mbox_tmo;
11898
11899 rc = 0;
11900 mqe = &mboxq->u.mqe;
11901
11902 /* Read the port's SLI4 Parameters port capabilities */
James Smartfedd3b72011-02-16 12:39:24 -050011903 lpfc_pc_sli4_params(mboxq);
James Smart28baac72010-02-12 14:42:03 -050011904 if (!phba->sli4_hba.intr_enable)
11905 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
11906 else {
James Smarta183a152011-10-10 21:32:43 -040011907 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
James Smart28baac72010-02-12 14:42:03 -050011908 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
11909 }
11910
11911 if (unlikely(rc))
11912 return 1;
11913
11914 sli4_params = &phba->sli4_hba.pc_sli4_params;
11915 sli4_params->if_type = bf_get(if_type, &mqe->un.sli4_params);
11916 sli4_params->sli_rev = bf_get(sli_rev, &mqe->un.sli4_params);
11917 sli4_params->sli_family = bf_get(sli_family, &mqe->un.sli4_params);
11918 sli4_params->featurelevel_1 = bf_get(featurelevel_1,
11919 &mqe->un.sli4_params);
11920 sli4_params->featurelevel_2 = bf_get(featurelevel_2,
11921 &mqe->un.sli4_params);
11922 sli4_params->proto_types = mqe->un.sli4_params.word3;
11923 sli4_params->sge_supp_len = mqe->un.sli4_params.sge_supp_len;
11924 sli4_params->if_page_sz = bf_get(if_page_sz, &mqe->un.sli4_params);
11925 sli4_params->rq_db_window = bf_get(rq_db_window, &mqe->un.sli4_params);
11926 sli4_params->loopbk_scope = bf_get(loopbk_scope, &mqe->un.sli4_params);
11927 sli4_params->eq_pages_max = bf_get(eq_pages, &mqe->un.sli4_params);
11928 sli4_params->eqe_size = bf_get(eqe_size, &mqe->un.sli4_params);
11929 sli4_params->cq_pages_max = bf_get(cq_pages, &mqe->un.sli4_params);
11930 sli4_params->cqe_size = bf_get(cqe_size, &mqe->un.sli4_params);
11931 sli4_params->mq_pages_max = bf_get(mq_pages, &mqe->un.sli4_params);
11932 sli4_params->mqe_size = bf_get(mqe_size, &mqe->un.sli4_params);
11933 sli4_params->mq_elem_cnt = bf_get(mq_elem_cnt, &mqe->un.sli4_params);
11934 sli4_params->wq_pages_max = bf_get(wq_pages, &mqe->un.sli4_params);
11935 sli4_params->wqe_size = bf_get(wqe_size, &mqe->un.sli4_params);
11936 sli4_params->rq_pages_max = bf_get(rq_pages, &mqe->un.sli4_params);
11937 sli4_params->rqe_size = bf_get(rqe_size, &mqe->un.sli4_params);
11938 sli4_params->hdr_pages_max = bf_get(hdr_pages, &mqe->un.sli4_params);
11939 sli4_params->hdr_size = bf_get(hdr_size, &mqe->un.sli4_params);
11940 sli4_params->hdr_pp_align = bf_get(hdr_pp_align, &mqe->un.sli4_params);
11941 sli4_params->sgl_pages_max = bf_get(sgl_pages, &mqe->un.sli4_params);
11942 sli4_params->sgl_pp_align = bf_get(sgl_pp_align, &mqe->un.sli4_params);
James Smart05580562011-05-24 11:40:48 -040011943
11944 /* Make sure that sge_supp_len can be handled by the driver */
11945 if (sli4_params->sge_supp_len > LPFC_MAX_SGE_SIZE)
11946 sli4_params->sge_supp_len = LPFC_MAX_SGE_SIZE;
11947
James Smart28baac72010-02-12 14:42:03 -050011948 return rc;
11949}
11950
James Smartda0436e2009-05-22 14:51:39 -040011951/**
James Smartfedd3b72011-02-16 12:39:24 -050011952 * lpfc_get_sli4_parameters - Get the SLI4 Config PARAMETERS.
11953 * @phba: Pointer to HBA context object.
11954 * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
11955 *
11956 * This function is called in the SLI4 code path to read the port's
11957 * sli4 capabilities.
11958 *
11959 * This function may be be called from any context that can block-wait
11960 * for the completion. The expectation is that this routine is called
11961 * typically from probe_one or from the online routine.
11962 **/
11963int
11964lpfc_get_sli4_parameters(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
11965{
11966 int rc;
11967 struct lpfc_mqe *mqe = &mboxq->u.mqe;
11968 struct lpfc_pc_sli4_params *sli4_params;
James Smarta183a152011-10-10 21:32:43 -040011969 uint32_t mbox_tmo;
James Smartfedd3b72011-02-16 12:39:24 -050011970 int length;
James Smartbf316c72018-04-09 14:24:28 -070011971 bool exp_wqcq_pages = true;
James Smartfedd3b72011-02-16 12:39:24 -050011972 struct lpfc_sli4_parameters *mbx_sli4_parameters;
11973
James Smart6d368e52011-05-24 11:44:12 -040011974 /*
11975 * By default, the driver assumes the SLI4 port requires RPI
11976 * header postings. The SLI4_PARAM response will correct this
11977 * assumption.
11978 */
11979 phba->sli4_hba.rpi_hdrs_in_use = 1;
11980
James Smartfedd3b72011-02-16 12:39:24 -050011981 /* Read the port's SLI4 Config Parameters */
11982 length = (sizeof(struct lpfc_mbx_get_sli4_parameters) -
11983 sizeof(struct lpfc_sli4_cfg_mhdr));
11984 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
11985 LPFC_MBOX_OPCODE_GET_SLI4_PARAMETERS,
11986 length, LPFC_SLI4_MBX_EMBED);
11987 if (!phba->sli4_hba.intr_enable)
11988 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smarta183a152011-10-10 21:32:43 -040011989 else {
11990 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
11991 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
11992 }
James Smartfedd3b72011-02-16 12:39:24 -050011993 if (unlikely(rc))
11994 return rc;
11995 sli4_params = &phba->sli4_hba.pc_sli4_params;
11996 mbx_sli4_parameters = &mqe->un.get_sli4_parameters.sli4_parameters;
11997 sli4_params->if_type = bf_get(cfg_if_type, mbx_sli4_parameters);
11998 sli4_params->sli_rev = bf_get(cfg_sli_rev, mbx_sli4_parameters);
11999 sli4_params->sli_family = bf_get(cfg_sli_family, mbx_sli4_parameters);
12000 sli4_params->featurelevel_1 = bf_get(cfg_sli_hint_1,
12001 mbx_sli4_parameters);
12002 sli4_params->featurelevel_2 = bf_get(cfg_sli_hint_2,
12003 mbx_sli4_parameters);
12004 if (bf_get(cfg_phwq, mbx_sli4_parameters))
12005 phba->sli3_options |= LPFC_SLI4_PHWQ_ENABLED;
12006 else
12007 phba->sli3_options &= ~LPFC_SLI4_PHWQ_ENABLED;
12008 sli4_params->sge_supp_len = mbx_sli4_parameters->sge_supp_len;
12009 sli4_params->loopbk_scope = bf_get(loopbk_scope, mbx_sli4_parameters);
James Smart1ba981f2014-02-20 09:56:45 -050012010 sli4_params->oas_supported = bf_get(cfg_oas, mbx_sli4_parameters);
James Smartfedd3b72011-02-16 12:39:24 -050012011 sli4_params->cqv = bf_get(cfg_cqv, mbx_sli4_parameters);
12012 sli4_params->mqv = bf_get(cfg_mqv, mbx_sli4_parameters);
12013 sli4_params->wqv = bf_get(cfg_wqv, mbx_sli4_parameters);
12014 sli4_params->rqv = bf_get(cfg_rqv, mbx_sli4_parameters);
James Smart7365f6f2018-02-22 08:18:46 -080012015 sli4_params->eqav = bf_get(cfg_eqav, mbx_sli4_parameters);
12016 sli4_params->cqav = bf_get(cfg_cqav, mbx_sli4_parameters);
James Smart0c651872013-07-15 18:33:23 -040012017 sli4_params->wqsize = bf_get(cfg_wqsize, mbx_sli4_parameters);
James Smart66e9e6b2018-06-26 08:24:27 -070012018 sli4_params->bv1s = bf_get(cfg_bv1s, mbx_sli4_parameters);
James Smart83c6cb12019-10-18 14:18:30 -070012019 sli4_params->pls = bf_get(cfg_pvl, mbx_sli4_parameters);
James Smartfedd3b72011-02-16 12:39:24 -050012020 sli4_params->sgl_pages_max = bf_get(cfg_sgl_page_cnt,
12021 mbx_sli4_parameters);
James Smart895427b2017-02-12 13:52:30 -080012022 sli4_params->wqpcnt = bf_get(cfg_wqpcnt, mbx_sli4_parameters);
James Smartfedd3b72011-02-16 12:39:24 -050012023 sli4_params->sgl_pp_align = bf_get(cfg_sgl_pp_align,
12024 mbx_sli4_parameters);
James Smart6d368e52011-05-24 11:44:12 -040012025 phba->sli4_hba.extents_in_use = bf_get(cfg_ext, mbx_sli4_parameters);
12026 phba->sli4_hba.rpi_hdrs_in_use = bf_get(cfg_hdrr, mbx_sli4_parameters);
James Smart895427b2017-02-12 13:52:30 -080012027
James Smartd79c9e92019-08-14 16:57:09 -070012028 /* Check for Extended Pre-Registered SGL support */
12029 phba->cfg_xpsgl = bf_get(cfg_xpsgl, mbx_sli4_parameters);
12030
James Smartc15e0702019-05-21 17:49:02 -070012031 /* Check for firmware nvme support */
12032 rc = (bf_get(cfg_nvme, mbx_sli4_parameters) &&
12033 bf_get(cfg_xib, mbx_sli4_parameters));
James Smart895427b2017-02-12 13:52:30 -080012034
James Smartc15e0702019-05-21 17:49:02 -070012035 if (rc) {
12036 /* Save this to indicate the Firmware supports NVME */
12037 sli4_params->nvme = 1;
12038
12039 /* Firmware NVME support, check driver FC4 NVME support */
12040 if (phba->cfg_enable_fc4_type == LPFC_ENABLE_FCP) {
12041 lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_NVME,
12042 "6133 Disabling NVME support: "
12043 "FC4 type not supported: x%x\n",
12044 phba->cfg_enable_fc4_type);
12045 goto fcponly;
12046 }
12047 } else {
12048 /* No firmware NVME support, check driver FC4 NVME support */
12049 sli4_params->nvme = 0;
12050 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
12051 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_NVME,
12052 "6101 Disabling NVME support: Not "
12053 "supported by firmware (%d %d) x%x\n",
12054 bf_get(cfg_nvme, mbx_sli4_parameters),
12055 bf_get(cfg_xib, mbx_sli4_parameters),
12056 phba->cfg_enable_fc4_type);
12057fcponly:
12058 phba->nvme_support = 0;
12059 phba->nvmet_support = 0;
12060 phba->cfg_nvmet_mrq = 0;
James Smart6a224b42019-08-14 16:56:57 -070012061 phba->cfg_nvme_seg_cnt = 0;
James Smartc15e0702019-05-21 17:49:02 -070012062
12063 /* If no FC4 type support, move to just SCSI support */
12064 if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP))
12065 return -ENODEV;
12066 phba->cfg_enable_fc4_type = LPFC_ENABLE_FCP;
12067 }
James Smart895427b2017-02-12 13:52:30 -080012068 }
James Smart05580562011-05-24 11:40:48 -040012069
James Smartc26c2652019-08-14 16:56:49 -070012070 /* If the NVME FC4 type is enabled, scale the sg_seg_cnt to
James Smarta5f73372019-09-21 20:58:50 -070012071 * accommodate 512K and 1M IOs in a single nvme buf.
James Smartc26c2652019-08-14 16:56:49 -070012072 */
James Smarta5f73372019-09-21 20:58:50 -070012073 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
James Smartc26c2652019-08-14 16:56:49 -070012074 phba->cfg_sg_seg_cnt = LPFC_MAX_NVME_SEG_CNT;
James Smartc26c2652019-08-14 16:56:49 -070012075
James Smart414abe02018-06-26 08:24:26 -070012076 /* Only embed PBDE for if_type 6, PBDE support requires xib be set */
12077 if ((bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
12078 LPFC_SLI_INTF_IF_TYPE_6) || (!bf_get(cfg_xib, mbx_sli4_parameters)))
12079 phba->cfg_enable_pbde = 0;
James Smart0bc2b7c2018-02-22 08:18:48 -080012080
James Smart20aefac2018-01-30 15:58:58 -080012081 /*
12082 * To support Suppress Response feature we must satisfy 3 conditions.
12083 * lpfc_suppress_rsp module parameter must be set (default).
12084 * In SLI4-Parameters Descriptor:
12085 * Extended Inline Buffers (XIB) must be supported.
12086 * Suppress Response IU Not Supported (SRIUNS) must NOT be supported
12087 * (double negative).
12088 */
12089 if (phba->cfg_suppress_rsp && bf_get(cfg_xib, mbx_sli4_parameters) &&
12090 !(bf_get(cfg_nosr, mbx_sli4_parameters)))
James Smartf358dd02017-02-12 13:52:34 -080012091 phba->sli.sli_flag |= LPFC_SLI_SUPPRESS_RSP;
James Smart20aefac2018-01-30 15:58:58 -080012092 else
12093 phba->cfg_suppress_rsp = 0;
James Smartf358dd02017-02-12 13:52:34 -080012094
James Smart0cf07f842017-06-01 21:07:10 -070012095 if (bf_get(cfg_eqdr, mbx_sli4_parameters))
12096 phba->sli.sli_flag |= LPFC_SLI_USE_EQDR;
12097
James Smart05580562011-05-24 11:40:48 -040012098 /* Make sure that sge_supp_len can be handled by the driver */
12099 if (sli4_params->sge_supp_len > LPFC_MAX_SGE_SIZE)
12100 sli4_params->sge_supp_len = LPFC_MAX_SGE_SIZE;
12101
James Smartb5c53952016-03-31 14:12:30 -070012102 /*
James Smartc176ffa2018-01-30 15:58:46 -080012103 * Check whether the adapter supports an embedded copy of the
12104 * FCP CMD IU within the WQE for FCP_Ixxx commands. In order
12105 * to use this option, 128-byte WQEs must be used.
James Smartb5c53952016-03-31 14:12:30 -070012106 */
12107 if (bf_get(cfg_ext_embed_cb, mbx_sli4_parameters))
12108 phba->fcp_embed_io = 1;
12109 else
12110 phba->fcp_embed_io = 0;
James Smart7bdedb32016-07-06 12:36:00 -070012111
James Smart0bc2b7c2018-02-22 08:18:48 -080012112 lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_NVME,
James Smart414abe02018-06-26 08:24:26 -070012113 "6422 XIB %d PBDE %d: FCP %d NVME %d %d %d\n",
James Smart0bc2b7c2018-02-22 08:18:48 -080012114 bf_get(cfg_xib, mbx_sli4_parameters),
James Smart414abe02018-06-26 08:24:26 -070012115 phba->cfg_enable_pbde,
12116 phba->fcp_embed_io, phba->nvme_support,
James Smart4e565cf2018-02-22 08:18:50 -080012117 phba->cfg_nvme_embed_cmd, phba->cfg_suppress_rsp);
James Smart0bc2b7c2018-02-22 08:18:48 -080012118
James Smartbf316c72018-04-09 14:24:28 -070012119 if ((bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
12120 LPFC_SLI_INTF_IF_TYPE_2) &&
12121 (bf_get(lpfc_sli_intf_sli_family, &phba->sli4_hba.sli_intf) ==
James Smartc2217682018-05-24 21:09:00 -070012122 LPFC_SLI_INTF_FAMILY_LNCR_A0))
James Smartbf316c72018-04-09 14:24:28 -070012123 exp_wqcq_pages = false;
12124
James Smartc176ffa2018-01-30 15:58:46 -080012125 if ((bf_get(cfg_cqpsize, mbx_sli4_parameters) & LPFC_CQ_16K_PAGE_SZ) &&
12126 (bf_get(cfg_wqpsize, mbx_sli4_parameters) & LPFC_WQ_16K_PAGE_SZ) &&
James Smartbf316c72018-04-09 14:24:28 -070012127 exp_wqcq_pages &&
James Smartc176ffa2018-01-30 15:58:46 -080012128 (sli4_params->wqsize & LPFC_WQ_SZ128_SUPPORT))
12129 phba->enab_exp_wqcq_pages = 1;
12130 else
12131 phba->enab_exp_wqcq_pages = 0;
James Smart7bdedb32016-07-06 12:36:00 -070012132 /*
12133 * Check if the SLI port supports MDS Diagnostics
12134 */
12135 if (bf_get(cfg_mds_diags, mbx_sli4_parameters))
12136 phba->mds_diags_support = 1;
12137 else
12138 phba->mds_diags_support = 0;
James Smartd2cc9bc2018-09-10 10:30:50 -070012139
James Smart0d8af092019-08-14 16:57:10 -070012140 /*
12141 * Check if the SLI port supports NSLER
12142 */
12143 if (bf_get(cfg_nsler, mbx_sli4_parameters))
12144 phba->nsler = 1;
12145 else
12146 phba->nsler = 0;
12147
James Smartfedd3b72011-02-16 12:39:24 -050012148 return 0;
12149}
12150
12151/**
James Smart3772a992009-05-22 14:50:54 -040012152 * lpfc_pci_probe_one_s3 - PCI probe func to reg SLI-3 device to PCI subsystem.
12153 * @pdev: pointer to PCI device
12154 * @pid: pointer to PCI device identifier
12155 *
12156 * This routine is to be called to attach a device with SLI-3 interface spec
12157 * to the PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
12158 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
12159 * information of the device and driver to see if the driver state that it can
12160 * support this kind of device. If the match is successful, the driver core
12161 * invokes this routine. If this routine determines it can claim the HBA, it
12162 * does all the initialization that it needs to do to handle the HBA properly.
12163 *
12164 * Return code
12165 * 0 - driver can claim the device
12166 * negative value - driver can not claim the device
12167 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080012168static int
James Smart3772a992009-05-22 14:50:54 -040012169lpfc_pci_probe_one_s3(struct pci_dev *pdev, const struct pci_device_id *pid)
12170{
12171 struct lpfc_hba *phba;
12172 struct lpfc_vport *vport = NULL;
James Smart6669f9b2009-10-02 15:16:45 -040012173 struct Scsi_Host *shost = NULL;
James Smart3772a992009-05-22 14:50:54 -040012174 int error;
12175 uint32_t cfg_mode, intr_mode;
12176
12177 /* Allocate memory for HBA structure */
12178 phba = lpfc_hba_alloc(pdev);
12179 if (!phba)
12180 return -ENOMEM;
12181
12182 /* Perform generic PCI device enabling operation */
12183 error = lpfc_enable_pci_dev(phba);
James Smart079b5c92011-08-21 21:48:49 -040012184 if (error)
James Smart3772a992009-05-22 14:50:54 -040012185 goto out_free_phba;
James Smart3772a992009-05-22 14:50:54 -040012186
12187 /* Set up SLI API function jump table for PCI-device group-0 HBAs */
12188 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_LP);
12189 if (error)
12190 goto out_disable_pci_dev;
12191
12192 /* Set up SLI-3 specific device PCI memory space */
12193 error = lpfc_sli_pci_mem_setup(phba);
12194 if (error) {
12195 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12196 "1402 Failed to set up pci memory space.\n");
12197 goto out_disable_pci_dev;
12198 }
12199
James Smart3772a992009-05-22 14:50:54 -040012200 /* Set up SLI-3 specific device driver resources */
12201 error = lpfc_sli_driver_resource_setup(phba);
12202 if (error) {
12203 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12204 "1404 Failed to set up driver resource.\n");
12205 goto out_unset_pci_mem_s3;
12206 }
12207
12208 /* Initialize and populate the iocb list per host */
James Smartd1f525a2017-04-21 16:04:55 -070012209
James Smart3772a992009-05-22 14:50:54 -040012210 error = lpfc_init_iocb_list(phba, LPFC_IOCB_LIST_CNT);
12211 if (error) {
12212 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12213 "1405 Failed to initialize iocb list.\n");
12214 goto out_unset_driver_resource_s3;
12215 }
12216
12217 /* Set up common device driver resources */
12218 error = lpfc_setup_driver_resource_phase2(phba);
12219 if (error) {
12220 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12221 "1406 Failed to set up driver resource.\n");
12222 goto out_free_iocb_list;
12223 }
12224
James Smart079b5c92011-08-21 21:48:49 -040012225 /* Get the default values for Model Name and Description */
12226 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
12227
James Smart3772a992009-05-22 14:50:54 -040012228 /* Create SCSI host to the physical port */
12229 error = lpfc_create_shost(phba);
12230 if (error) {
12231 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12232 "1407 Failed to create scsi host.\n");
12233 goto out_unset_driver_resource;
12234 }
12235
12236 /* Configure sysfs attributes */
12237 vport = phba->pport;
12238 error = lpfc_alloc_sysfs_attr(vport);
12239 if (error) {
12240 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12241 "1476 Failed to allocate sysfs attr\n");
12242 goto out_destroy_shost;
12243 }
12244
James Smart6669f9b2009-10-02 15:16:45 -040012245 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
James Smart3772a992009-05-22 14:50:54 -040012246 /* Now, trying to enable interrupt and bring up the device */
12247 cfg_mode = phba->cfg_use_msi;
12248 while (true) {
12249 /* Put device to a known state before enabling interrupt */
12250 lpfc_stop_port(phba);
12251 /* Configure and enable interrupt */
12252 intr_mode = lpfc_sli_enable_intr(phba, cfg_mode);
12253 if (intr_mode == LPFC_INTR_ERROR) {
12254 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12255 "0431 Failed to enable interrupt.\n");
12256 error = -ENODEV;
12257 goto out_free_sysfs_attr;
12258 }
12259 /* SLI-3 HBA setup */
12260 if (lpfc_sli_hba_setup(phba)) {
12261 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12262 "1477 Failed to set up hba\n");
12263 error = -ENODEV;
12264 goto out_remove_device;
12265 }
12266
12267 /* Wait 50ms for the interrupts of previous mailbox commands */
12268 msleep(50);
12269 /* Check active interrupts on message signaled interrupts */
12270 if (intr_mode == 0 ||
12271 phba->sli.slistat.sli_intr > LPFC_MSIX_VECTORS) {
12272 /* Log the current active interrupt mode */
12273 phba->intr_mode = intr_mode;
12274 lpfc_log_intr_mode(phba, intr_mode);
12275 break;
12276 } else {
12277 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
12278 "0447 Configure interrupt mode (%d) "
12279 "failed active interrupt test.\n",
12280 intr_mode);
12281 /* Disable the current interrupt mode */
12282 lpfc_sli_disable_intr(phba);
12283 /* Try next level of interrupt mode */
12284 cfg_mode = --intr_mode;
12285 }
12286 }
12287
12288 /* Perform post initialization setup */
12289 lpfc_post_init_setup(phba);
12290
12291 /* Check if there are static vports to be created. */
12292 lpfc_create_static_vport(phba);
12293
12294 return 0;
12295
12296out_remove_device:
12297 lpfc_unset_hba(phba);
12298out_free_sysfs_attr:
12299 lpfc_free_sysfs_attr(vport);
12300out_destroy_shost:
12301 lpfc_destroy_shost(phba);
12302out_unset_driver_resource:
12303 lpfc_unset_driver_resource_phase2(phba);
12304out_free_iocb_list:
12305 lpfc_free_iocb_list(phba);
12306out_unset_driver_resource_s3:
12307 lpfc_sli_driver_resource_unset(phba);
12308out_unset_pci_mem_s3:
12309 lpfc_sli_pci_mem_unset(phba);
12310out_disable_pci_dev:
12311 lpfc_disable_pci_dev(phba);
James Smart6669f9b2009-10-02 15:16:45 -040012312 if (shost)
12313 scsi_host_put(shost);
James Smart3772a992009-05-22 14:50:54 -040012314out_free_phba:
12315 lpfc_hba_free(phba);
12316 return error;
12317}
12318
12319/**
12320 * lpfc_pci_remove_one_s3 - PCI func to unreg SLI-3 device from PCI subsystem.
James Smarte59058c2008-08-24 21:49:00 -040012321 * @pdev: pointer to PCI device
12322 *
James Smart3772a992009-05-22 14:50:54 -040012323 * This routine is to be called to disattach a device with SLI-3 interface
12324 * spec from PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
12325 * removed from PCI bus, it performs all the necessary cleanup for the HBA
12326 * device to be removed from the PCI subsystem properly.
James Smarte59058c2008-08-24 21:49:00 -040012327 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080012328static void
James Smart3772a992009-05-22 14:50:54 -040012329lpfc_pci_remove_one_s3(struct pci_dev *pdev)
dea31012005-04-17 16:05:31 -050012330{
James Smart2e0fef82007-06-17 19:56:36 -050012331 struct Scsi_Host *shost = pci_get_drvdata(pdev);
12332 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
James Smarteada2722008-12-04 22:39:13 -050012333 struct lpfc_vport **vports;
James Smart2e0fef82007-06-17 19:56:36 -050012334 struct lpfc_hba *phba = vport->phba;
James Smarteada2722008-12-04 22:39:13 -050012335 int i;
Tomohiro Kusumi8a4df1202008-01-11 01:53:00 -050012336
James Smart549e55c2007-08-02 11:09:51 -040012337 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -040012338 vport->load_flag |= FC_UNLOADING;
James Smart549e55c2007-08-02 11:09:51 -040012339 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050012340
James Smart858c9f62007-06-17 19:56:39 -050012341 lpfc_free_sysfs_attr(vport);
12342
James Smarteada2722008-12-04 22:39:13 -050012343 /* Release all the vports against this physical port */
12344 vports = lpfc_create_vport_work_array(phba);
12345 if (vports != NULL)
James Smart587a37f2012-05-09 21:16:03 -040012346 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
12347 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
12348 continue;
James Smarteada2722008-12-04 22:39:13 -050012349 fc_vport_terminate(vports[i]->fc_vport);
James Smart587a37f2012-05-09 21:16:03 -040012350 }
James Smarteada2722008-12-04 22:39:13 -050012351 lpfc_destroy_vport_work_array(phba, vports);
12352
12353 /* Remove FC host and then SCSI host with the physical port */
James Smart858c9f62007-06-17 19:56:39 -050012354 fc_remove_host(shost);
12355 scsi_remove_host(shost);
James Smartd613b6a2017-02-12 13:52:37 -080012356
James Smart87af33f2007-10-27 13:37:43 -040012357 lpfc_cleanup(vport);
12358
James Smart2e0fef82007-06-17 19:56:36 -050012359 /*
12360 * Bring down the SLI Layer. This step disable all interrupts,
12361 * clears the rings, discards all mailbox commands, and resets
12362 * the HBA.
12363 */
James Smarta257bf92009-04-06 18:48:10 -040012364
Justin P. Mattock48e34d02010-12-30 15:07:58 -080012365 /* HBA interrupt will be disabled after this call */
James Smart2e0fef82007-06-17 19:56:36 -050012366 lpfc_sli_hba_down(phba);
James Smarta257bf92009-04-06 18:48:10 -040012367 /* Stop kthread signal shall trigger work_done one more time */
12368 kthread_stop(phba->worker_thread);
12369 /* Final cleanup of txcmplq and reset the HBA */
James Smart2e0fef82007-06-17 19:56:36 -050012370 lpfc_sli_brdrestart(phba);
12371
James Smart72859902012-01-18 16:25:38 -050012372 kfree(phba->vpi_bmask);
12373 kfree(phba->vpi_ids);
12374
James Smart3772a992009-05-22 14:50:54 -040012375 lpfc_stop_hba_timers(phba);
James Smart523128e2018-09-10 10:30:46 -070012376 spin_lock_irq(&phba->port_list_lock);
James Smart858c9f62007-06-17 19:56:39 -050012377 list_del_init(&vport->listentry);
James Smart523128e2018-09-10 10:30:46 -070012378 spin_unlock_irq(&phba->port_list_lock);
James Smart858c9f62007-06-17 19:56:39 -050012379
James Smart858c9f62007-06-17 19:56:39 -050012380 lpfc_debugfs_terminate(vport);
James Smart2e0fef82007-06-17 19:56:36 -050012381
James Smart912e3ac2011-05-24 11:42:11 -040012382 /* Disable SR-IOV if enabled */
12383 if (phba->cfg_sriov_nr_virtfn)
12384 pci_disable_sriov(pdev);
12385
James Smart5b75da22008-12-04 22:39:35 -050012386 /* Disable interrupt */
James Smart3772a992009-05-22 14:50:54 -040012387 lpfc_sli_disable_intr(phba);
dea31012005-04-17 16:05:31 -050012388
James Smart858c9f62007-06-17 19:56:39 -050012389 scsi_host_put(shost);
James Smart2e0fef82007-06-17 19:56:36 -050012390
12391 /*
12392 * Call scsi_free before mem_free since scsi bufs are released to their
12393 * corresponding pools here.
12394 */
12395 lpfc_scsi_free(phba);
James Smart0794d602019-01-28 11:14:19 -080012396 lpfc_free_iocb_list(phba);
12397
James Smart3772a992009-05-22 14:50:54 -040012398 lpfc_mem_free_all(phba);
James Smart2e0fef82007-06-17 19:56:36 -050012399
James Smart34b02dc2008-08-24 21:49:55 -040012400 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
12401 phba->hbqslimp.virt, phba->hbqslimp.phys);
James Smarted957682007-06-17 19:56:37 -050012402
James Smart2e0fef82007-06-17 19:56:36 -050012403 /* Free resources associated with SLI2 interface */
12404 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
James Smart34b02dc2008-08-24 21:49:55 -040012405 phba->slim2p.virt, phba->slim2p.phys);
James Smart2e0fef82007-06-17 19:56:36 -050012406
12407 /* unmap adapter SLIM and Control Registers */
12408 iounmap(phba->ctrl_regs_memmap_p);
12409 iounmap(phba->slim_memmap_p);
12410
James Smart3772a992009-05-22 14:50:54 -040012411 lpfc_hba_free(phba);
James Smart2e0fef82007-06-17 19:56:36 -050012412
Johannes Thumshirne0c04832016-06-07 09:44:03 +020012413 pci_release_mem_regions(pdev);
James Smart2e0fef82007-06-17 19:56:36 -050012414 pci_disable_device(pdev);
dea31012005-04-17 16:05:31 -050012415}
12416
Linas Vepstas8d63f372007-02-14 14:28:36 -060012417/**
James Smart3772a992009-05-22 14:50:54 -040012418 * lpfc_pci_suspend_one_s3 - PCI func to suspend SLI-3 device for power mgmnt
James Smart3a55b532008-12-04 22:38:54 -050012419 * @pdev: pointer to PCI device
12420 * @msg: power management message
12421 *
James Smart3772a992009-05-22 14:50:54 -040012422 * This routine is to be called from the kernel's PCI subsystem to support
12423 * system Power Management (PM) to device with SLI-3 interface spec. When
12424 * PM invokes this method, it quiesces the device by stopping the driver's
12425 * worker thread for the device, turning off device's interrupt and DMA,
12426 * and bring the device offline. Note that as the driver implements the
12427 * minimum PM requirements to a power-aware driver's PM support for the
12428 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
12429 * to the suspend() method call will be treated as SUSPEND and the driver will
12430 * fully reinitialize its device during resume() method call, the driver will
12431 * set device to PCI_D3hot state in PCI config space instead of setting it
12432 * according to the @msg provided by the PM.
James Smart3a55b532008-12-04 22:38:54 -050012433 *
12434 * Return code
James Smart3772a992009-05-22 14:50:54 -040012435 * 0 - driver suspended the device
12436 * Error otherwise
James Smart3a55b532008-12-04 22:38:54 -050012437 **/
12438static int
James Smart3772a992009-05-22 14:50:54 -040012439lpfc_pci_suspend_one_s3(struct pci_dev *pdev, pm_message_t msg)
James Smart3a55b532008-12-04 22:38:54 -050012440{
12441 struct Scsi_Host *shost = pci_get_drvdata(pdev);
12442 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
12443
12444 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
12445 "0473 PCI device Power Management suspend.\n");
12446
12447 /* Bring down the device */
James Smart618a5232012-06-12 13:54:36 -040012448 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart3a55b532008-12-04 22:38:54 -050012449 lpfc_offline(phba);
12450 kthread_stop(phba->worker_thread);
12451
12452 /* Disable interrupt from device */
James Smart3772a992009-05-22 14:50:54 -040012453 lpfc_sli_disable_intr(phba);
James Smart3a55b532008-12-04 22:38:54 -050012454
12455 /* Save device state to PCI config space */
12456 pci_save_state(pdev);
12457 pci_set_power_state(pdev, PCI_D3hot);
12458
12459 return 0;
12460}
12461
12462/**
James Smart3772a992009-05-22 14:50:54 -040012463 * lpfc_pci_resume_one_s3 - PCI func to resume SLI-3 device for power mgmnt
James Smart3a55b532008-12-04 22:38:54 -050012464 * @pdev: pointer to PCI device
12465 *
James Smart3772a992009-05-22 14:50:54 -040012466 * This routine is to be called from the kernel's PCI subsystem to support
12467 * system Power Management (PM) to device with SLI-3 interface spec. When PM
12468 * invokes this method, it restores the device's PCI config space state and
12469 * fully reinitializes the device and brings it online. Note that as the
12470 * driver implements the minimum PM requirements to a power-aware driver's
12471 * PM for suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE,
12472 * FREEZE) to the suspend() method call will be treated as SUSPEND and the
12473 * driver will fully reinitialize its device during resume() method call,
12474 * the device will be set to PCI_D0 directly in PCI config space before
12475 * restoring the state.
James Smart3a55b532008-12-04 22:38:54 -050012476 *
12477 * Return code
James Smart3772a992009-05-22 14:50:54 -040012478 * 0 - driver suspended the device
12479 * Error otherwise
James Smart3a55b532008-12-04 22:38:54 -050012480 **/
12481static int
James Smart3772a992009-05-22 14:50:54 -040012482lpfc_pci_resume_one_s3(struct pci_dev *pdev)
James Smart3a55b532008-12-04 22:38:54 -050012483{
12484 struct Scsi_Host *shost = pci_get_drvdata(pdev);
12485 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
James Smart5b75da22008-12-04 22:39:35 -050012486 uint32_t intr_mode;
James Smart3a55b532008-12-04 22:38:54 -050012487 int error;
12488
12489 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
12490 "0452 PCI device Power Management resume.\n");
12491
12492 /* Restore device state from PCI config space */
12493 pci_set_power_state(pdev, PCI_D0);
12494 pci_restore_state(pdev);
James Smart0d878412009-10-02 15:16:56 -040012495
James Smart1dfb5a42010-02-12 14:40:50 -050012496 /*
12497 * As the new kernel behavior of pci_restore_state() API call clears
12498 * device saved_state flag, need to save the restored state again.
12499 */
12500 pci_save_state(pdev);
12501
James Smart3a55b532008-12-04 22:38:54 -050012502 if (pdev->is_busmaster)
12503 pci_set_master(pdev);
12504
12505 /* Startup the kernel thread for this host adapter. */
12506 phba->worker_thread = kthread_run(lpfc_do_work, phba,
12507 "lpfc_worker_%d", phba->brd_no);
12508 if (IS_ERR(phba->worker_thread)) {
12509 error = PTR_ERR(phba->worker_thread);
12510 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12511 "0434 PM resume failed to start worker "
12512 "thread: error=x%x.\n", error);
12513 return error;
12514 }
12515
James Smart5b75da22008-12-04 22:39:35 -050012516 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -040012517 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -050012518 if (intr_mode == LPFC_INTR_ERROR) {
James Smart3a55b532008-12-04 22:38:54 -050012519 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart5b75da22008-12-04 22:39:35 -050012520 "0430 PM resume Failed to enable interrupt\n");
12521 return -EIO;
12522 } else
12523 phba->intr_mode = intr_mode;
James Smart3a55b532008-12-04 22:38:54 -050012524
12525 /* Restart HBA and bring it online */
12526 lpfc_sli_brdrestart(phba);
12527 lpfc_online(phba);
12528
James Smart5b75da22008-12-04 22:39:35 -050012529 /* Log the current active interrupt mode */
12530 lpfc_log_intr_mode(phba, phba->intr_mode);
12531
James Smart3a55b532008-12-04 22:38:54 -050012532 return 0;
12533}
12534
12535/**
James Smart891478a2009-11-18 15:40:23 -050012536 * lpfc_sli_prep_dev_for_recover - Prepare SLI3 device for pci slot recover
12537 * @phba: pointer to lpfc hba data structure.
12538 *
12539 * This routine is called to prepare the SLI3 device for PCI slot recover. It
James Smarte2af0d22010-03-15 11:25:32 -040012540 * aborts all the outstanding SCSI I/Os to the pci device.
James Smart891478a2009-11-18 15:40:23 -050012541 **/
12542static void
12543lpfc_sli_prep_dev_for_recover(struct lpfc_hba *phba)
12544{
12545 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12546 "2723 PCI channel I/O abort preparing for recovery\n");
James Smarte2af0d22010-03-15 11:25:32 -040012547
12548 /*
12549 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
12550 * and let the SCSI mid-layer to retry them to recover.
12551 */
James Smartdb55fba2014-04-04 13:52:02 -040012552 lpfc_sli_abort_fcp_rings(phba);
James Smart891478a2009-11-18 15:40:23 -050012553}
12554
12555/**
James Smart0d878412009-10-02 15:16:56 -040012556 * lpfc_sli_prep_dev_for_reset - Prepare SLI3 device for pci slot reset
12557 * @phba: pointer to lpfc hba data structure.
12558 *
12559 * This routine is called to prepare the SLI3 device for PCI slot reset. It
12560 * disables the device interrupt and pci device, and aborts the internal FCP
12561 * pending I/Os.
12562 **/
12563static void
12564lpfc_sli_prep_dev_for_reset(struct lpfc_hba *phba)
12565{
James Smart0d878412009-10-02 15:16:56 -040012566 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart891478a2009-11-18 15:40:23 -050012567 "2710 PCI channel disable preparing for reset\n");
James Smarte2af0d22010-03-15 11:25:32 -040012568
James Smart75baf692010-06-08 18:31:21 -040012569 /* Block any management I/Os to the device */
James Smart618a5232012-06-12 13:54:36 -040012570 lpfc_block_mgmt_io(phba, LPFC_MBX_WAIT);
James Smart75baf692010-06-08 18:31:21 -040012571
James Smarte2af0d22010-03-15 11:25:32 -040012572 /* Block all SCSI devices' I/Os on the host */
12573 lpfc_scsi_dev_block(phba);
12574
James Smartea714f32013-04-17 20:18:47 -040012575 /* Flush all driver's outstanding SCSI I/Os as we are to reset */
James Smartc00f62e2019-08-14 16:57:11 -070012576 lpfc_sli_flush_io_rings(phba);
James Smartea714f32013-04-17 20:18:47 -040012577
James Smarte2af0d22010-03-15 11:25:32 -040012578 /* stop all timers */
12579 lpfc_stop_hba_timers(phba);
12580
James Smart0d878412009-10-02 15:16:56 -040012581 /* Disable interrupt and pci device */
12582 lpfc_sli_disable_intr(phba);
12583 pci_disable_device(phba->pcidev);
James Smart0d878412009-10-02 15:16:56 -040012584}
12585
12586/**
12587 * lpfc_sli_prep_dev_for_perm_failure - Prepare SLI3 dev for pci slot disable
12588 * @phba: pointer to lpfc hba data structure.
12589 *
12590 * This routine is called to prepare the SLI3 device for PCI slot permanently
12591 * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
12592 * pending I/Os.
12593 **/
12594static void
James Smart75baf692010-06-08 18:31:21 -040012595lpfc_sli_prep_dev_for_perm_failure(struct lpfc_hba *phba)
James Smart0d878412009-10-02 15:16:56 -040012596{
12597 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart891478a2009-11-18 15:40:23 -050012598 "2711 PCI channel permanent disable for failure\n");
James Smarte2af0d22010-03-15 11:25:32 -040012599 /* Block all SCSI devices' I/Os on the host */
12600 lpfc_scsi_dev_block(phba);
12601
12602 /* stop all timers */
12603 lpfc_stop_hba_timers(phba);
12604
James Smart0d878412009-10-02 15:16:56 -040012605 /* Clean up all driver's outstanding SCSI I/Os */
James Smartc00f62e2019-08-14 16:57:11 -070012606 lpfc_sli_flush_io_rings(phba);
James Smart0d878412009-10-02 15:16:56 -040012607}
12608
12609/**
James Smart3772a992009-05-22 14:50:54 -040012610 * lpfc_io_error_detected_s3 - Method for handling SLI-3 device PCI I/O error
James Smarte59058c2008-08-24 21:49:00 -040012611 * @pdev: pointer to PCI device.
12612 * @state: the current PCI connection state.
Linas Vepstas8d63f372007-02-14 14:28:36 -060012613 *
James Smart3772a992009-05-22 14:50:54 -040012614 * This routine is called from the PCI subsystem for I/O error handling to
12615 * device with SLI-3 interface spec. This function is called by the PCI
12616 * subsystem after a PCI bus error affecting this device has been detected.
12617 * When this function is invoked, it will need to stop all the I/Os and
12618 * interrupt(s) to the device. Once that is done, it will return
12619 * PCI_ERS_RESULT_NEED_RESET for the PCI subsystem to perform proper recovery
12620 * as desired.
James Smarte59058c2008-08-24 21:49:00 -040012621 *
12622 * Return codes
James Smart0d878412009-10-02 15:16:56 -040012623 * PCI_ERS_RESULT_CAN_RECOVER - can be recovered with reset_link
James Smart3772a992009-05-22 14:50:54 -040012624 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
12625 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
James Smarte59058c2008-08-24 21:49:00 -040012626 **/
James Smart3772a992009-05-22 14:50:54 -040012627static pci_ers_result_t
12628lpfc_io_error_detected_s3(struct pci_dev *pdev, pci_channel_state_t state)
Linas Vepstas8d63f372007-02-14 14:28:36 -060012629{
James Smart51ef4c22007-08-02 11:10:31 -040012630 struct Scsi_Host *shost = pci_get_drvdata(pdev);
12631 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -060012632
James Smart0d878412009-10-02 15:16:56 -040012633 switch (state) {
12634 case pci_channel_io_normal:
James Smart891478a2009-11-18 15:40:23 -050012635 /* Non-fatal error, prepare for recovery */
12636 lpfc_sli_prep_dev_for_recover(phba);
James Smart0d878412009-10-02 15:16:56 -040012637 return PCI_ERS_RESULT_CAN_RECOVER;
12638 case pci_channel_io_frozen:
12639 /* Fatal error, prepare for slot reset */
12640 lpfc_sli_prep_dev_for_reset(phba);
12641 return PCI_ERS_RESULT_NEED_RESET;
12642 case pci_channel_io_perm_failure:
12643 /* Permanent failure, prepare for device down */
James Smart75baf692010-06-08 18:31:21 -040012644 lpfc_sli_prep_dev_for_perm_failure(phba);
Linas Vepstas8d63f372007-02-14 14:28:36 -060012645 return PCI_ERS_RESULT_DISCONNECT;
James Smart0d878412009-10-02 15:16:56 -040012646 default:
12647 /* Unknown state, prepare and request slot reset */
12648 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12649 "0472 Unknown PCI error state: x%x\n", state);
12650 lpfc_sli_prep_dev_for_reset(phba);
12651 return PCI_ERS_RESULT_NEED_RESET;
James Smarta8e497d2008-08-24 21:50:11 -040012652 }
Linas Vepstas8d63f372007-02-14 14:28:36 -060012653}
12654
12655/**
James Smart3772a992009-05-22 14:50:54 -040012656 * lpfc_io_slot_reset_s3 - Method for restarting PCI SLI-3 device from scratch.
James Smarte59058c2008-08-24 21:49:00 -040012657 * @pdev: pointer to PCI device.
Linas Vepstas8d63f372007-02-14 14:28:36 -060012658 *
James Smart3772a992009-05-22 14:50:54 -040012659 * This routine is called from the PCI subsystem for error handling to
12660 * device with SLI-3 interface spec. This is called after PCI bus has been
12661 * reset to restart the PCI card from scratch, as if from a cold-boot.
12662 * During the PCI subsystem error recovery, after driver returns
12663 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
12664 * recovery and then call this routine before calling the .resume method
12665 * to recover the device. This function will initialize the HBA device,
12666 * enable the interrupt, but it will just put the HBA to offline state
12667 * without passing any I/O traffic.
James Smarte59058c2008-08-24 21:49:00 -040012668 *
12669 * Return codes
James Smart3772a992009-05-22 14:50:54 -040012670 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
12671 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
Linas Vepstas8d63f372007-02-14 14:28:36 -060012672 */
James Smart3772a992009-05-22 14:50:54 -040012673static pci_ers_result_t
12674lpfc_io_slot_reset_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -060012675{
James Smart51ef4c22007-08-02 11:10:31 -040012676 struct Scsi_Host *shost = pci_get_drvdata(pdev);
12677 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -060012678 struct lpfc_sli *psli = &phba->sli;
James Smart5b75da22008-12-04 22:39:35 -050012679 uint32_t intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -060012680
12681 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
Benjamin Herrenschmidt09483912007-12-20 15:28:09 +110012682 if (pci_enable_device_mem(pdev)) {
Linas Vepstas8d63f372007-02-14 14:28:36 -060012683 printk(KERN_ERR "lpfc: Cannot re-enable "
12684 "PCI device after reset.\n");
12685 return PCI_ERS_RESULT_DISCONNECT;
12686 }
12687
James Smart97207482008-12-04 22:39:19 -050012688 pci_restore_state(pdev);
James Smart1dfb5a42010-02-12 14:40:50 -050012689
12690 /*
12691 * As the new kernel behavior of pci_restore_state() API call clears
12692 * device saved_state flag, need to save the restored state again.
12693 */
12694 pci_save_state(pdev);
12695
James Smart97207482008-12-04 22:39:19 -050012696 if (pdev->is_busmaster)
12697 pci_set_master(pdev);
Linas Vepstas8d63f372007-02-14 14:28:36 -060012698
James Smart92d7f7b2007-06-17 19:56:38 -050012699 spin_lock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040012700 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -050012701 spin_unlock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -060012702
James Smart5b75da22008-12-04 22:39:35 -050012703 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -040012704 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -050012705 if (intr_mode == LPFC_INTR_ERROR) {
12706 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12707 "0427 Cannot re-enable interrupt after "
12708 "slot reset.\n");
12709 return PCI_ERS_RESULT_DISCONNECT;
12710 } else
12711 phba->intr_mode = intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -060012712
James Smart75baf692010-06-08 18:31:21 -040012713 /* Take device offline, it will perform cleanup */
James Smart618a5232012-06-12 13:54:36 -040012714 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
Linas Vepstas8d63f372007-02-14 14:28:36 -060012715 lpfc_offline(phba);
12716 lpfc_sli_brdrestart(phba);
12717
James Smart5b75da22008-12-04 22:39:35 -050012718 /* Log the current active interrupt mode */
12719 lpfc_log_intr_mode(phba, phba->intr_mode);
12720
Linas Vepstas8d63f372007-02-14 14:28:36 -060012721 return PCI_ERS_RESULT_RECOVERED;
12722}
12723
12724/**
James Smart3772a992009-05-22 14:50:54 -040012725 * lpfc_io_resume_s3 - Method for resuming PCI I/O operation on SLI-3 device.
James Smarte59058c2008-08-24 21:49:00 -040012726 * @pdev: pointer to PCI device
Linas Vepstas8d63f372007-02-14 14:28:36 -060012727 *
James Smart3772a992009-05-22 14:50:54 -040012728 * This routine is called from the PCI subsystem for error handling to device
12729 * with SLI-3 interface spec. It is called when kernel error recovery tells
12730 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
12731 * error recovery. After this call, traffic can start to flow from this device
12732 * again.
Linas Vepstas8d63f372007-02-14 14:28:36 -060012733 */
James Smart3772a992009-05-22 14:50:54 -040012734static void
12735lpfc_io_resume_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -060012736{
James Smart51ef4c22007-08-02 11:10:31 -040012737 struct Scsi_Host *shost = pci_get_drvdata(pdev);
12738 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -060012739
James Smarte2af0d22010-03-15 11:25:32 -040012740 /* Bring device online, it will be no-op for non-fatal error resume */
James Smart58da1ff2008-04-07 10:15:56 -040012741 lpfc_online(phba);
Linas Vepstas8d63f372007-02-14 14:28:36 -060012742}
12743
James Smart3772a992009-05-22 14:50:54 -040012744/**
James Smartda0436e2009-05-22 14:51:39 -040012745 * lpfc_sli4_get_els_iocb_cnt - Calculate the # of ELS IOCBs to reserve
12746 * @phba: pointer to lpfc hba data structure.
12747 *
12748 * returns the number of ELS/CT IOCBs to reserve
12749 **/
12750int
12751lpfc_sli4_get_els_iocb_cnt(struct lpfc_hba *phba)
12752{
12753 int max_xri = phba->sli4_hba.max_cfg_param.max_xri;
12754
James Smartf1126682009-06-10 17:22:44 -040012755 if (phba->sli_rev == LPFC_SLI_REV4) {
12756 if (max_xri <= 100)
James Smart6a9c52c2009-10-02 15:16:51 -040012757 return 10;
James Smartf1126682009-06-10 17:22:44 -040012758 else if (max_xri <= 256)
James Smart6a9c52c2009-10-02 15:16:51 -040012759 return 25;
James Smartf1126682009-06-10 17:22:44 -040012760 else if (max_xri <= 512)
James Smart6a9c52c2009-10-02 15:16:51 -040012761 return 50;
James Smartf1126682009-06-10 17:22:44 -040012762 else if (max_xri <= 1024)
James Smart6a9c52c2009-10-02 15:16:51 -040012763 return 100;
James Smart8a9d2e82012-05-09 21:16:12 -040012764 else if (max_xri <= 1536)
James Smart6a9c52c2009-10-02 15:16:51 -040012765 return 150;
James Smart8a9d2e82012-05-09 21:16:12 -040012766 else if (max_xri <= 2048)
12767 return 200;
12768 else
12769 return 250;
James Smartf1126682009-06-10 17:22:44 -040012770 } else
12771 return 0;
James Smartda0436e2009-05-22 14:51:39 -040012772}
12773
12774/**
James Smart895427b2017-02-12 13:52:30 -080012775 * lpfc_sli4_get_iocb_cnt - Calculate the # of total IOCBs to reserve
12776 * @phba: pointer to lpfc hba data structure.
12777 *
James Smartf358dd02017-02-12 13:52:34 -080012778 * returns the number of ELS/CT + NVMET IOCBs to reserve
James Smart895427b2017-02-12 13:52:30 -080012779 **/
12780int
12781lpfc_sli4_get_iocb_cnt(struct lpfc_hba *phba)
12782{
12783 int max_xri = lpfc_sli4_get_els_iocb_cnt(phba);
12784
James Smartf358dd02017-02-12 13:52:34 -080012785 if (phba->nvmet_support)
12786 max_xri += LPFC_NVMET_BUF_POST;
James Smart895427b2017-02-12 13:52:30 -080012787 return max_xri;
12788}
12789
12790
James Smart0a5ce732019-10-18 14:18:18 -070012791static int
James Smart1feb8202018-02-22 08:18:47 -080012792lpfc_log_write_firmware_error(struct lpfc_hba *phba, uint32_t offset,
12793 uint32_t magic_number, uint32_t ftype, uint32_t fid, uint32_t fsize,
12794 const struct firmware *fw)
12795{
James Smart0a5ce732019-10-18 14:18:18 -070012796 int rc;
12797
12798 /* Three cases: (1) FW was not supported on the detected adapter.
12799 * (2) FW update has been locked out administratively.
12800 * (3) Some other error during FW update.
12801 * In each case, an unmaskable message is written to the console
12802 * for admin diagnosis.
12803 */
12804 if (offset == ADD_STATUS_FW_NOT_SUPPORTED ||
James Smarta72d56b2018-05-04 20:37:52 -070012805 (phba->pcidev->device == PCI_DEVICE_ID_LANCER_G6_FC &&
James Smart5792a0e2019-10-25 11:43:42 -070012806 magic_number != MAGIC_NUMBER_G6) ||
James Smarta72d56b2018-05-04 20:37:52 -070012807 (phba->pcidev->device == PCI_DEVICE_ID_LANCER_G7_FC &&
James Smart5792a0e2019-10-25 11:43:42 -070012808 magic_number != MAGIC_NUMBER_G7)) {
James Smart1feb8202018-02-22 08:18:47 -080012809 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart0a5ce732019-10-18 14:18:18 -070012810 "3030 This firmware version is not supported on"
12811 " this HBA model. Device:%x Magic:%x Type:%x "
12812 "ID:%x Size %d %zd\n",
12813 phba->pcidev->device, magic_number, ftype, fid,
12814 fsize, fw->size);
12815 rc = -EINVAL;
12816 } else if (offset == ADD_STATUS_FW_DOWNLOAD_HW_DISABLED) {
James Smart1feb8202018-02-22 08:18:47 -080012817 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart0a5ce732019-10-18 14:18:18 -070012818 "3021 Firmware downloads have been prohibited "
12819 "by a system configuration setting on "
12820 "Device:%x Magic:%x Type:%x ID:%x Size %d "
12821 "%zd\n",
12822 phba->pcidev->device, magic_number, ftype, fid,
12823 fsize, fw->size);
12824 rc = -EACCES;
12825 } else {
12826 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12827 "3022 FW Download failed. Add Status x%x "
12828 "Device:%x Magic:%x Type:%x ID:%x Size %d "
12829 "%zd\n",
12830 offset, phba->pcidev->device, magic_number,
12831 ftype, fid, fsize, fw->size);
12832 rc = -EIO;
12833 }
12834 return rc;
James Smart1feb8202018-02-22 08:18:47 -080012835}
12836
James Smart895427b2017-02-12 13:52:30 -080012837/**
James Smart52d52442011-05-24 11:42:45 -040012838 * lpfc_write_firmware - attempt to write a firmware image to the port
James Smart52d52442011-05-24 11:42:45 -040012839 * @fw: pointer to firmware image returned from request_firmware.
James Smart0a5ce732019-10-18 14:18:18 -070012840 * @context: pointer to firmware image returned from request_firmware.
12841 * @ret: return value this routine provides to the caller.
James Smart52d52442011-05-24 11:42:45 -040012842 *
James Smart52d52442011-05-24 11:42:45 -040012843 **/
James Smartce396282012-09-29 11:30:56 -040012844static void
12845lpfc_write_firmware(const struct firmware *fw, void *context)
James Smart52d52442011-05-24 11:42:45 -040012846{
James Smartce396282012-09-29 11:30:56 -040012847 struct lpfc_hba *phba = (struct lpfc_hba *)context;
James Smart6b5151f2012-01-18 16:24:06 -050012848 char fwrev[FW_REV_STR_SIZE];
James Smartce396282012-09-29 11:30:56 -040012849 struct lpfc_grp_hdr *image;
James Smart52d52442011-05-24 11:42:45 -040012850 struct list_head dma_buffer_list;
12851 int i, rc = 0;
12852 struct lpfc_dmabuf *dmabuf, *next;
12853 uint32_t offset = 0, temp_offset = 0;
James Smart6b6ef5d2016-10-13 15:06:17 -070012854 uint32_t magic_number, ftype, fid, fsize;
James Smart52d52442011-05-24 11:42:45 -040012855
James Smartc71ab862012-10-31 14:44:33 -040012856 /* It can be null in no-wait mode, sanity check */
James Smartce396282012-09-29 11:30:56 -040012857 if (!fw) {
12858 rc = -ENXIO;
12859 goto out;
12860 }
12861 image = (struct lpfc_grp_hdr *)fw->data;
12862
James Smart6b6ef5d2016-10-13 15:06:17 -070012863 magic_number = be32_to_cpu(image->magic_number);
12864 ftype = bf_get_be32(lpfc_grp_hdr_file_type, image);
James Smart1feb8202018-02-22 08:18:47 -080012865 fid = bf_get_be32(lpfc_grp_hdr_id, image);
James Smart6b6ef5d2016-10-13 15:06:17 -070012866 fsize = be32_to_cpu(image->size);
12867
James Smart52d52442011-05-24 11:42:45 -040012868 INIT_LIST_HEAD(&dma_buffer_list);
James Smart52d52442011-05-24 11:42:45 -040012869 lpfc_decode_firmware_rev(phba, fwrev, 1);
James Smart88a2cfb2011-07-22 18:36:33 -040012870 if (strncmp(fwrev, image->revision, strnlen(image->revision, 16))) {
James Smart52d52442011-05-24 11:42:45 -040012871 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartce396282012-09-29 11:30:56 -040012872 "3023 Updating Firmware, Current Version:%s "
James Smart52d52442011-05-24 11:42:45 -040012873 "New Version:%s\n",
James Smart88a2cfb2011-07-22 18:36:33 -040012874 fwrev, image->revision);
James Smart52d52442011-05-24 11:42:45 -040012875 for (i = 0; i < LPFC_MBX_WR_CONFIG_MAX_BDE; i++) {
12876 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf),
12877 GFP_KERNEL);
12878 if (!dmabuf) {
12879 rc = -ENOMEM;
James Smartce396282012-09-29 11:30:56 -040012880 goto release_out;
James Smart52d52442011-05-24 11:42:45 -040012881 }
12882 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
12883 SLI4_PAGE_SIZE,
12884 &dmabuf->phys,
12885 GFP_KERNEL);
12886 if (!dmabuf->virt) {
12887 kfree(dmabuf);
12888 rc = -ENOMEM;
James Smartce396282012-09-29 11:30:56 -040012889 goto release_out;
James Smart52d52442011-05-24 11:42:45 -040012890 }
12891 list_add_tail(&dmabuf->list, &dma_buffer_list);
12892 }
12893 while (offset < fw->size) {
12894 temp_offset = offset;
12895 list_for_each_entry(dmabuf, &dma_buffer_list, list) {
James Smart079b5c92011-08-21 21:48:49 -040012896 if (temp_offset + SLI4_PAGE_SIZE > fw->size) {
James Smart52d52442011-05-24 11:42:45 -040012897 memcpy(dmabuf->virt,
12898 fw->data + temp_offset,
James Smart079b5c92011-08-21 21:48:49 -040012899 fw->size - temp_offset);
12900 temp_offset = fw->size;
James Smart52d52442011-05-24 11:42:45 -040012901 break;
12902 }
James Smart52d52442011-05-24 11:42:45 -040012903 memcpy(dmabuf->virt, fw->data + temp_offset,
12904 SLI4_PAGE_SIZE);
James Smart88a2cfb2011-07-22 18:36:33 -040012905 temp_offset += SLI4_PAGE_SIZE;
James Smart52d52442011-05-24 11:42:45 -040012906 }
12907 rc = lpfc_wr_object(phba, &dma_buffer_list,
12908 (fw->size - offset), &offset);
James Smart1feb8202018-02-22 08:18:47 -080012909 if (rc) {
James Smart0a5ce732019-10-18 14:18:18 -070012910 rc = lpfc_log_write_firmware_error(phba, offset,
12911 magic_number,
12912 ftype,
12913 fid,
12914 fsize,
12915 fw);
James Smartce396282012-09-29 11:30:56 -040012916 goto release_out;
James Smart1feb8202018-02-22 08:18:47 -080012917 }
James Smart52d52442011-05-24 11:42:45 -040012918 }
12919 rc = offset;
James Smart1feb8202018-02-22 08:18:47 -080012920 } else
12921 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12922 "3029 Skipped Firmware update, Current "
12923 "Version:%s New Version:%s\n",
12924 fwrev, image->revision);
James Smartce396282012-09-29 11:30:56 -040012925
12926release_out:
James Smart52d52442011-05-24 11:42:45 -040012927 list_for_each_entry_safe(dmabuf, next, &dma_buffer_list, list) {
12928 list_del(&dmabuf->list);
12929 dma_free_coherent(&phba->pcidev->dev, SLI4_PAGE_SIZE,
12930 dmabuf->virt, dmabuf->phys);
12931 kfree(dmabuf);
12932 }
James Smartce396282012-09-29 11:30:56 -040012933 release_firmware(fw);
12934out:
James Smart0a5ce732019-10-18 14:18:18 -070012935 if (rc < 0)
12936 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12937 "3062 Firmware update error, status %d.\n", rc);
12938 else
12939 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12940 "3024 Firmware update success: size %d.\n", rc);
James Smart52d52442011-05-24 11:42:45 -040012941}
12942
12943/**
James Smartc71ab862012-10-31 14:44:33 -040012944 * lpfc_sli4_request_firmware_update - Request linux generic firmware upgrade
12945 * @phba: pointer to lpfc hba data structure.
12946 *
12947 * This routine is called to perform Linux generic firmware upgrade on device
12948 * that supports such feature.
12949 **/
12950int
12951lpfc_sli4_request_firmware_update(struct lpfc_hba *phba, uint8_t fw_upgrade)
12952{
12953 uint8_t file_name[ELX_MODEL_NAME_SIZE];
12954 int ret;
12955 const struct firmware *fw;
12956
12957 /* Only supported on SLI4 interface type 2 for now */
James Smart27d6ac02018-02-22 08:18:42 -080012958 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) <
James Smartc71ab862012-10-31 14:44:33 -040012959 LPFC_SLI_INTF_IF_TYPE_2)
12960 return -EPERM;
12961
12962 snprintf(file_name, ELX_MODEL_NAME_SIZE, "%s.grp", phba->ModelName);
12963
12964 if (fw_upgrade == INT_FW_UPGRADE) {
12965 ret = request_firmware_nowait(THIS_MODULE, FW_ACTION_HOTPLUG,
12966 file_name, &phba->pcidev->dev,
12967 GFP_KERNEL, (void *)phba,
12968 lpfc_write_firmware);
12969 } else if (fw_upgrade == RUN_FW_UPGRADE) {
12970 ret = request_firmware(&fw, file_name, &phba->pcidev->dev);
12971 if (!ret)
12972 lpfc_write_firmware(fw, (void *)phba);
12973 } else {
12974 ret = -EINVAL;
12975 }
12976
12977 return ret;
12978}
12979
12980/**
James Smartda0436e2009-05-22 14:51:39 -040012981 * lpfc_pci_probe_one_s4 - PCI probe func to reg SLI-4 device to PCI subsys
12982 * @pdev: pointer to PCI device
12983 * @pid: pointer to PCI device identifier
12984 *
12985 * This routine is called from the kernel's PCI subsystem to device with
12986 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
12987 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
12988 * information of the device and driver to see if the driver state that it
12989 * can support this kind of device. If the match is successful, the driver
12990 * core invokes this routine. If this routine determines it can claim the HBA,
12991 * it does all the initialization that it needs to do to handle the HBA
12992 * properly.
12993 *
12994 * Return code
12995 * 0 - driver can claim the device
12996 * negative value - driver can not claim the device
12997 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080012998static int
James Smartda0436e2009-05-22 14:51:39 -040012999lpfc_pci_probe_one_s4(struct pci_dev *pdev, const struct pci_device_id *pid)
13000{
13001 struct lpfc_hba *phba;
13002 struct lpfc_vport *vport = NULL;
James Smart6669f9b2009-10-02 15:16:45 -040013003 struct Scsi_Host *shost = NULL;
James Smart6c621a22017-05-15 15:20:45 -070013004 int error;
James Smartda0436e2009-05-22 14:51:39 -040013005 uint32_t cfg_mode, intr_mode;
James Smartda0436e2009-05-22 14:51:39 -040013006
13007 /* Allocate memory for HBA structure */
13008 phba = lpfc_hba_alloc(pdev);
13009 if (!phba)
13010 return -ENOMEM;
13011
13012 /* Perform generic PCI device enabling operation */
13013 error = lpfc_enable_pci_dev(phba);
James Smart079b5c92011-08-21 21:48:49 -040013014 if (error)
James Smartda0436e2009-05-22 14:51:39 -040013015 goto out_free_phba;
James Smartda0436e2009-05-22 14:51:39 -040013016
13017 /* Set up SLI API function jump table for PCI-device group-1 HBAs */
13018 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_OC);
13019 if (error)
13020 goto out_disable_pci_dev;
13021
13022 /* Set up SLI-4 specific device PCI memory space */
13023 error = lpfc_sli4_pci_mem_setup(phba);
13024 if (error) {
13025 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13026 "1410 Failed to set up pci memory space.\n");
13027 goto out_disable_pci_dev;
13028 }
13029
James Smartda0436e2009-05-22 14:51:39 -040013030 /* Set up SLI-4 Specific device driver resources */
13031 error = lpfc_sli4_driver_resource_setup(phba);
13032 if (error) {
13033 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13034 "1412 Failed to set up driver resource.\n");
13035 goto out_unset_pci_mem_s4;
13036 }
13037
James Smart19ca7602010-11-20 23:11:55 -050013038 INIT_LIST_HEAD(&phba->active_rrq_list);
James Smart7d791df2011-07-22 18:37:52 -040013039 INIT_LIST_HEAD(&phba->fcf.fcf_pri_list);
James Smart19ca7602010-11-20 23:11:55 -050013040
James Smartda0436e2009-05-22 14:51:39 -040013041 /* Set up common device driver resources */
13042 error = lpfc_setup_driver_resource_phase2(phba);
13043 if (error) {
13044 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13045 "1414 Failed to set up driver resource.\n");
James Smart6c621a22017-05-15 15:20:45 -070013046 goto out_unset_driver_resource_s4;
James Smartda0436e2009-05-22 14:51:39 -040013047 }
13048
James Smart079b5c92011-08-21 21:48:49 -040013049 /* Get the default values for Model Name and Description */
13050 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
13051
James Smartda0436e2009-05-22 14:51:39 -040013052 /* Now, trying to enable interrupt and bring up the device */
13053 cfg_mode = phba->cfg_use_msi;
James Smartda0436e2009-05-22 14:51:39 -040013054
James Smart7b15db32013-01-03 15:43:29 -050013055 /* Put device to a known state before enabling interrupt */
James Smartcdb42be2019-01-28 11:14:21 -080013056 phba->pport = NULL;
James Smart7b15db32013-01-03 15:43:29 -050013057 lpfc_stop_port(phba);
James Smart895427b2017-02-12 13:52:30 -080013058
James Smartdcaa2132019-11-04 16:57:06 -080013059 /* Init cpu_map array */
13060 lpfc_cpu_map_array_init(phba);
13061
13062 /* Init hba_eq_hdl array */
13063 lpfc_hba_eq_hdl_array_init(phba);
13064
James Smart7b15db32013-01-03 15:43:29 -050013065 /* Configure and enable interrupt */
13066 intr_mode = lpfc_sli4_enable_intr(phba, cfg_mode);
13067 if (intr_mode == LPFC_INTR_ERROR) {
13068 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13069 "0426 Failed to enable interrupt.\n");
13070 error = -ENODEV;
James Smartcdb42be2019-01-28 11:14:21 -080013071 goto out_unset_driver_resource;
James Smartda0436e2009-05-22 14:51:39 -040013072 }
James Smart7b15db32013-01-03 15:43:29 -050013073 /* Default to single EQ for non-MSI-X */
James Smart895427b2017-02-12 13:52:30 -080013074 if (phba->intr_type != MSIX) {
James Smart6a828b02019-01-28 11:14:31 -080013075 phba->cfg_irq_chann = 1;
James Smart2d7dbc42017-02-12 13:52:35 -080013076 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
James Smart2d7dbc42017-02-12 13:52:35 -080013077 if (phba->nvmet_support)
13078 phba->cfg_nvmet_mrq = 1;
13079 }
James Smartcdb42be2019-01-28 11:14:21 -080013080 }
James Smart6a828b02019-01-28 11:14:31 -080013081 lpfc_cpu_affinity_check(phba, phba->cfg_irq_chann);
James Smartcdb42be2019-01-28 11:14:21 -080013082
13083 /* Create SCSI host to the physical port */
13084 error = lpfc_create_shost(phba);
13085 if (error) {
13086 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13087 "1415 Failed to create scsi host.\n");
13088 goto out_disable_intr;
13089 }
13090 vport = phba->pport;
13091 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
13092
13093 /* Configure sysfs attributes */
13094 error = lpfc_alloc_sysfs_attr(vport);
13095 if (error) {
13096 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13097 "1416 Failed to allocate sysfs attr\n");
13098 goto out_destroy_shost;
James Smart895427b2017-02-12 13:52:30 -080013099 }
13100
James Smart7b15db32013-01-03 15:43:29 -050013101 /* Set up SLI-4 HBA */
13102 if (lpfc_sli4_hba_setup(phba)) {
13103 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13104 "1421 Failed to set up hba\n");
13105 error = -ENODEV;
James Smartcdb42be2019-01-28 11:14:21 -080013106 goto out_free_sysfs_attr;
James Smart7b15db32013-01-03 15:43:29 -050013107 }
13108
13109 /* Log the current active interrupt mode */
13110 phba->intr_mode = intr_mode;
13111 lpfc_log_intr_mode(phba, intr_mode);
James Smartda0436e2009-05-22 14:51:39 -040013112
13113 /* Perform post initialization setup */
13114 lpfc_post_init_setup(phba);
13115
James Smart01649562017-02-12 13:52:32 -080013116 /* NVME support in FW earlier in the driver load corrects the
13117 * FC4 type making a check for nvme_support unnecessary.
13118 */
James Smart0794d602019-01-28 11:14:19 -080013119 if (phba->nvmet_support == 0) {
13120 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
13121 /* Create NVME binding with nvme_fc_transport. This
13122 * ensures the vport is initialized. If the localport
13123 * create fails, it should not unload the driver to
13124 * support field issues.
13125 */
13126 error = lpfc_nvme_create_localport(vport);
13127 if (error) {
13128 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13129 "6004 NVME registration "
13130 "failed, error x%x\n",
13131 error);
13132 }
James Smart01649562017-02-12 13:52:32 -080013133 }
13134 }
James Smart895427b2017-02-12 13:52:30 -080013135
James Smartc71ab862012-10-31 14:44:33 -040013136 /* check for firmware upgrade or downgrade */
13137 if (phba->cfg_request_firmware_upgrade)
Sebastian Herbsztdb6f1c22015-08-31 16:48:14 -040013138 lpfc_sli4_request_firmware_update(phba, INT_FW_UPGRADE);
James Smart52d52442011-05-24 11:42:45 -040013139
James Smart1c6834a2009-07-19 10:01:26 -040013140 /* Check if there are static vports to be created. */
13141 lpfc_create_static_vport(phba);
James Smartd2cc9bc2018-09-10 10:30:50 -070013142
13143 /* Enable RAS FW log support */
13144 lpfc_sli4_ras_setup(phba);
13145
James Smart93a4d6f2019-11-04 16:57:05 -080013146 INIT_LIST_HEAD(&phba->poll_list);
13147 cpuhp_state_add_instance_nocalls(lpfc_cpuhp_state, &phba->cpuhp);
13148
James Smartda0436e2009-05-22 14:51:39 -040013149 return 0;
13150
James Smartda0436e2009-05-22 14:51:39 -040013151out_free_sysfs_attr:
13152 lpfc_free_sysfs_attr(vport);
13153out_destroy_shost:
13154 lpfc_destroy_shost(phba);
James Smartcdb42be2019-01-28 11:14:21 -080013155out_disable_intr:
13156 lpfc_sli4_disable_intr(phba);
James Smartda0436e2009-05-22 14:51:39 -040013157out_unset_driver_resource:
13158 lpfc_unset_driver_resource_phase2(phba);
James Smartda0436e2009-05-22 14:51:39 -040013159out_unset_driver_resource_s4:
13160 lpfc_sli4_driver_resource_unset(phba);
13161out_unset_pci_mem_s4:
13162 lpfc_sli4_pci_mem_unset(phba);
13163out_disable_pci_dev:
13164 lpfc_disable_pci_dev(phba);
James Smart6669f9b2009-10-02 15:16:45 -040013165 if (shost)
13166 scsi_host_put(shost);
James Smartda0436e2009-05-22 14:51:39 -040013167out_free_phba:
13168 lpfc_hba_free(phba);
13169 return error;
13170}
13171
13172/**
13173 * lpfc_pci_remove_one_s4 - PCI func to unreg SLI-4 device from PCI subsystem
13174 * @pdev: pointer to PCI device
13175 *
13176 * This routine is called from the kernel's PCI subsystem to device with
13177 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
13178 * removed from PCI bus, it performs all the necessary cleanup for the HBA
13179 * device to be removed from the PCI subsystem properly.
13180 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080013181static void
James Smartda0436e2009-05-22 14:51:39 -040013182lpfc_pci_remove_one_s4(struct pci_dev *pdev)
13183{
13184 struct Scsi_Host *shost = pci_get_drvdata(pdev);
13185 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
13186 struct lpfc_vport **vports;
13187 struct lpfc_hba *phba = vport->phba;
13188 int i;
13189
13190 /* Mark the device unloading flag */
13191 spin_lock_irq(&phba->hbalock);
13192 vport->load_flag |= FC_UNLOADING;
13193 spin_unlock_irq(&phba->hbalock);
13194
13195 /* Free the HBA sysfs attributes */
13196 lpfc_free_sysfs_attr(vport);
13197
13198 /* Release all the vports against this physical port */
13199 vports = lpfc_create_vport_work_array(phba);
13200 if (vports != NULL)
James Smart587a37f2012-05-09 21:16:03 -040013201 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
13202 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
13203 continue;
James Smartda0436e2009-05-22 14:51:39 -040013204 fc_vport_terminate(vports[i]->fc_vport);
James Smart587a37f2012-05-09 21:16:03 -040013205 }
James Smartda0436e2009-05-22 14:51:39 -040013206 lpfc_destroy_vport_work_array(phba, vports);
13207
13208 /* Remove FC host and then SCSI host with the physical port */
13209 fc_remove_host(shost);
13210 scsi_remove_host(shost);
James Smartda0436e2009-05-22 14:51:39 -040013211
James Smartda0436e2009-05-22 14:51:39 -040013212 /* Perform ndlp cleanup on the physical port. The nvme and nvmet
13213 * localports are destroyed after to cleanup all transport memory.
13214 */
13215 lpfc_cleanup(vport);
13216 lpfc_nvmet_destroy_targetport(phba);
13217 lpfc_nvme_destroy_localport(vport);
13218
James Smartc4908502019-01-28 11:14:28 -080013219 /* De-allocate multi-XRI pools */
13220 if (phba->cfg_xri_rebalancing)
13221 lpfc_destroy_multixri_pools(phba);
13222
James Smart281d6192018-01-30 15:58:47 -080013223 /*
13224 * Bring down the SLI Layer. This step disables all interrupts,
13225 * clears the rings, discards all mailbox commands, and resets
13226 * the HBA FCoE function.
13227 */
13228 lpfc_debugfs_terminate(vport);
James Smartda0436e2009-05-22 14:51:39 -040013229
Dick Kennedy19017622017-09-29 17:34:27 -070013230 lpfc_stop_hba_timers(phba);
James Smart523128e2018-09-10 10:30:46 -070013231 spin_lock_irq(&phba->port_list_lock);
James Smartda0436e2009-05-22 14:51:39 -040013232 list_del_init(&vport->listentry);
James Smart523128e2018-09-10 10:30:46 -070013233 spin_unlock_irq(&phba->port_list_lock);
James Smartda0436e2009-05-22 14:51:39 -040013234
James Smart3677a3a2010-09-29 11:19:14 -040013235 /* Perform scsi free before driver resource_unset since scsi
James Smartda0436e2009-05-22 14:51:39 -040013236 * buffers are released to their corresponding pools here.
13237 */
James Smart5e5b5112019-01-28 11:14:22 -080013238 lpfc_io_free(phba);
James Smart01649562017-02-12 13:52:32 -080013239 lpfc_free_iocb_list(phba);
James Smart5e5b5112019-01-28 11:14:22 -080013240 lpfc_sli4_hba_unset(phba);
James Smart67d12732012-08-03 12:36:13 -040013241
James Smart0cdb84e2018-04-09 14:24:26 -070013242 lpfc_unset_driver_resource_phase2(phba);
James Smartda0436e2009-05-22 14:51:39 -040013243 lpfc_sli4_driver_resource_unset(phba);
13244
13245 /* Unmap adapter Control and Doorbell registers */
13246 lpfc_sli4_pci_mem_unset(phba);
13247
13248 /* Release PCI resources and disable device's PCI function */
13249 scsi_host_put(shost);
13250 lpfc_disable_pci_dev(phba);
13251
13252 /* Finally, free the driver's device data structure */
13253 lpfc_hba_free(phba);
13254
13255 return;
13256}
13257
13258/**
13259 * lpfc_pci_suspend_one_s4 - PCI func to suspend SLI-4 device for power mgmnt
13260 * @pdev: pointer to PCI device
13261 * @msg: power management message
13262 *
13263 * This routine is called from the kernel's PCI subsystem to support system
13264 * Power Management (PM) to device with SLI-4 interface spec. When PM invokes
13265 * this method, it quiesces the device by stopping the driver's worker
13266 * thread for the device, turning off device's interrupt and DMA, and bring
13267 * the device offline. Note that as the driver implements the minimum PM
13268 * requirements to a power-aware driver's PM support for suspend/resume -- all
13269 * the possible PM messages (SUSPEND, HIBERNATE, FREEZE) to the suspend()
13270 * method call will be treated as SUSPEND and the driver will fully
13271 * reinitialize its device during resume() method call, the driver will set
13272 * device to PCI_D3hot state in PCI config space instead of setting it
13273 * according to the @msg provided by the PM.
13274 *
13275 * Return code
13276 * 0 - driver suspended the device
13277 * Error otherwise
13278 **/
13279static int
13280lpfc_pci_suspend_one_s4(struct pci_dev *pdev, pm_message_t msg)
13281{
13282 struct Scsi_Host *shost = pci_get_drvdata(pdev);
13283 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
13284
13285 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart75baf692010-06-08 18:31:21 -040013286 "2843 PCI device Power Management suspend.\n");
James Smartda0436e2009-05-22 14:51:39 -040013287
13288 /* Bring down the device */
James Smart618a5232012-06-12 13:54:36 -040013289 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smartda0436e2009-05-22 14:51:39 -040013290 lpfc_offline(phba);
13291 kthread_stop(phba->worker_thread);
13292
13293 /* Disable interrupt from device */
13294 lpfc_sli4_disable_intr(phba);
James Smart5350d872011-10-10 21:33:49 -040013295 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -040013296
13297 /* Save device state to PCI config space */
13298 pci_save_state(pdev);
13299 pci_set_power_state(pdev, PCI_D3hot);
13300
13301 return 0;
13302}
13303
13304/**
13305 * lpfc_pci_resume_one_s4 - PCI func to resume SLI-4 device for power mgmnt
13306 * @pdev: pointer to PCI device
13307 *
13308 * This routine is called from the kernel's PCI subsystem to support system
13309 * Power Management (PM) to device with SLI-4 interface spac. When PM invokes
13310 * this method, it restores the device's PCI config space state and fully
13311 * reinitializes the device and brings it online. Note that as the driver
13312 * implements the minimum PM requirements to a power-aware driver's PM for
13313 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
13314 * to the suspend() method call will be treated as SUSPEND and the driver
13315 * will fully reinitialize its device during resume() method call, the device
13316 * will be set to PCI_D0 directly in PCI config space before restoring the
13317 * state.
13318 *
13319 * Return code
13320 * 0 - driver suspended the device
13321 * Error otherwise
13322 **/
13323static int
13324lpfc_pci_resume_one_s4(struct pci_dev *pdev)
13325{
13326 struct Scsi_Host *shost = pci_get_drvdata(pdev);
13327 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
13328 uint32_t intr_mode;
13329 int error;
13330
13331 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
13332 "0292 PCI device Power Management resume.\n");
13333
13334 /* Restore device state from PCI config space */
13335 pci_set_power_state(pdev, PCI_D0);
13336 pci_restore_state(pdev);
James Smart1dfb5a42010-02-12 14:40:50 -050013337
13338 /*
13339 * As the new kernel behavior of pci_restore_state() API call clears
13340 * device saved_state flag, need to save the restored state again.
13341 */
13342 pci_save_state(pdev);
13343
James Smartda0436e2009-05-22 14:51:39 -040013344 if (pdev->is_busmaster)
13345 pci_set_master(pdev);
13346
13347 /* Startup the kernel thread for this host adapter. */
13348 phba->worker_thread = kthread_run(lpfc_do_work, phba,
13349 "lpfc_worker_%d", phba->brd_no);
13350 if (IS_ERR(phba->worker_thread)) {
13351 error = PTR_ERR(phba->worker_thread);
13352 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13353 "0293 PM resume failed to start worker "
13354 "thread: error=x%x.\n", error);
13355 return error;
13356 }
13357
13358 /* Configure and enable interrupt */
13359 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
13360 if (intr_mode == LPFC_INTR_ERROR) {
13361 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13362 "0294 PM resume Failed to enable interrupt\n");
13363 return -EIO;
13364 } else
13365 phba->intr_mode = intr_mode;
13366
13367 /* Restart HBA and bring it online */
13368 lpfc_sli_brdrestart(phba);
13369 lpfc_online(phba);
13370
13371 /* Log the current active interrupt mode */
13372 lpfc_log_intr_mode(phba, phba->intr_mode);
13373
13374 return 0;
13375}
13376
13377/**
James Smart75baf692010-06-08 18:31:21 -040013378 * lpfc_sli4_prep_dev_for_recover - Prepare SLI4 device for pci slot recover
13379 * @phba: pointer to lpfc hba data structure.
13380 *
13381 * This routine is called to prepare the SLI4 device for PCI slot recover. It
13382 * aborts all the outstanding SCSI I/Os to the pci device.
13383 **/
13384static void
13385lpfc_sli4_prep_dev_for_recover(struct lpfc_hba *phba)
13386{
James Smart75baf692010-06-08 18:31:21 -040013387 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13388 "2828 PCI channel I/O abort preparing for recovery\n");
13389 /*
13390 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
13391 * and let the SCSI mid-layer to retry them to recover.
13392 */
James Smartdb55fba2014-04-04 13:52:02 -040013393 lpfc_sli_abort_fcp_rings(phba);
James Smart75baf692010-06-08 18:31:21 -040013394}
13395
13396/**
13397 * lpfc_sli4_prep_dev_for_reset - Prepare SLI4 device for pci slot reset
13398 * @phba: pointer to lpfc hba data structure.
13399 *
13400 * This routine is called to prepare the SLI4 device for PCI slot reset. It
13401 * disables the device interrupt and pci device, and aborts the internal FCP
13402 * pending I/Os.
13403 **/
13404static void
13405lpfc_sli4_prep_dev_for_reset(struct lpfc_hba *phba)
13406{
13407 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13408 "2826 PCI channel disable preparing for reset\n");
13409
13410 /* Block any management I/Os to the device */
James Smart618a5232012-06-12 13:54:36 -040013411 lpfc_block_mgmt_io(phba, LPFC_MBX_NO_WAIT);
James Smart75baf692010-06-08 18:31:21 -040013412
13413 /* Block all SCSI devices' I/Os on the host */
13414 lpfc_scsi_dev_block(phba);
13415
James Smartc00f62e2019-08-14 16:57:11 -070013416 /* Flush all driver's outstanding I/Os as we are to reset */
13417 lpfc_sli_flush_io_rings(phba);
James Smartc3725bd2017-11-20 16:00:42 -080013418
James Smart75baf692010-06-08 18:31:21 -040013419 /* stop all timers */
13420 lpfc_stop_hba_timers(phba);
13421
13422 /* Disable interrupt and pci device */
13423 lpfc_sli4_disable_intr(phba);
James Smart5350d872011-10-10 21:33:49 -040013424 lpfc_sli4_queue_destroy(phba);
James Smart75baf692010-06-08 18:31:21 -040013425 pci_disable_device(phba->pcidev);
James Smart75baf692010-06-08 18:31:21 -040013426}
13427
13428/**
13429 * lpfc_sli4_prep_dev_for_perm_failure - Prepare SLI4 dev for pci slot disable
13430 * @phba: pointer to lpfc hba data structure.
13431 *
13432 * This routine is called to prepare the SLI4 device for PCI slot permanently
13433 * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
13434 * pending I/Os.
13435 **/
13436static void
13437lpfc_sli4_prep_dev_for_perm_failure(struct lpfc_hba *phba)
13438{
13439 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13440 "2827 PCI channel permanent disable for failure\n");
13441
13442 /* Block all SCSI devices' I/Os on the host */
13443 lpfc_scsi_dev_block(phba);
13444
13445 /* stop all timers */
13446 lpfc_stop_hba_timers(phba);
13447
James Smartc00f62e2019-08-14 16:57:11 -070013448 /* Clean up all driver's outstanding I/Os */
13449 lpfc_sli_flush_io_rings(phba);
James Smart75baf692010-06-08 18:31:21 -040013450}
13451
13452/**
James Smartda0436e2009-05-22 14:51:39 -040013453 * lpfc_io_error_detected_s4 - Method for handling PCI I/O error to SLI-4 device
13454 * @pdev: pointer to PCI device.
13455 * @state: the current PCI connection state.
13456 *
13457 * This routine is called from the PCI subsystem for error handling to device
13458 * with SLI-4 interface spec. This function is called by the PCI subsystem
13459 * after a PCI bus error affecting this device has been detected. When this
13460 * function is invoked, it will need to stop all the I/Os and interrupt(s)
13461 * to the device. Once that is done, it will return PCI_ERS_RESULT_NEED_RESET
13462 * for the PCI subsystem to perform proper recovery as desired.
13463 *
13464 * Return codes
13465 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
13466 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
13467 **/
13468static pci_ers_result_t
13469lpfc_io_error_detected_s4(struct pci_dev *pdev, pci_channel_state_t state)
13470{
James Smart75baf692010-06-08 18:31:21 -040013471 struct Scsi_Host *shost = pci_get_drvdata(pdev);
13472 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
13473
13474 switch (state) {
13475 case pci_channel_io_normal:
13476 /* Non-fatal error, prepare for recovery */
13477 lpfc_sli4_prep_dev_for_recover(phba);
13478 return PCI_ERS_RESULT_CAN_RECOVER;
13479 case pci_channel_io_frozen:
13480 /* Fatal error, prepare for slot reset */
13481 lpfc_sli4_prep_dev_for_reset(phba);
13482 return PCI_ERS_RESULT_NEED_RESET;
13483 case pci_channel_io_perm_failure:
13484 /* Permanent failure, prepare for device down */
13485 lpfc_sli4_prep_dev_for_perm_failure(phba);
13486 return PCI_ERS_RESULT_DISCONNECT;
13487 default:
13488 /* Unknown state, prepare and request slot reset */
13489 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13490 "2825 Unknown PCI error state: x%x\n", state);
13491 lpfc_sli4_prep_dev_for_reset(phba);
13492 return PCI_ERS_RESULT_NEED_RESET;
13493 }
James Smartda0436e2009-05-22 14:51:39 -040013494}
13495
13496/**
13497 * lpfc_io_slot_reset_s4 - Method for restart PCI SLI-4 device from scratch
13498 * @pdev: pointer to PCI device.
13499 *
13500 * This routine is called from the PCI subsystem for error handling to device
13501 * with SLI-4 interface spec. It is called after PCI bus has been reset to
13502 * restart the PCI card from scratch, as if from a cold-boot. During the
13503 * PCI subsystem error recovery, after the driver returns
13504 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
13505 * recovery and then call this routine before calling the .resume method to
13506 * recover the device. This function will initialize the HBA device, enable
13507 * the interrupt, but it will just put the HBA to offline state without
13508 * passing any I/O traffic.
13509 *
13510 * Return codes
13511 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
13512 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
13513 */
13514static pci_ers_result_t
13515lpfc_io_slot_reset_s4(struct pci_dev *pdev)
13516{
James Smart75baf692010-06-08 18:31:21 -040013517 struct Scsi_Host *shost = pci_get_drvdata(pdev);
13518 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
13519 struct lpfc_sli *psli = &phba->sli;
13520 uint32_t intr_mode;
13521
13522 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
13523 if (pci_enable_device_mem(pdev)) {
13524 printk(KERN_ERR "lpfc: Cannot re-enable "
13525 "PCI device after reset.\n");
13526 return PCI_ERS_RESULT_DISCONNECT;
13527 }
13528
13529 pci_restore_state(pdev);
James Smart0a96e972011-07-22 18:37:28 -040013530
13531 /*
13532 * As the new kernel behavior of pci_restore_state() API call clears
13533 * device saved_state flag, need to save the restored state again.
13534 */
13535 pci_save_state(pdev);
13536
James Smart75baf692010-06-08 18:31:21 -040013537 if (pdev->is_busmaster)
13538 pci_set_master(pdev);
13539
13540 spin_lock_irq(&phba->hbalock);
13541 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
13542 spin_unlock_irq(&phba->hbalock);
13543
13544 /* Configure and enable interrupt */
13545 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
13546 if (intr_mode == LPFC_INTR_ERROR) {
13547 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13548 "2824 Cannot re-enable interrupt after "
13549 "slot reset.\n");
13550 return PCI_ERS_RESULT_DISCONNECT;
13551 } else
13552 phba->intr_mode = intr_mode;
13553
13554 /* Log the current active interrupt mode */
13555 lpfc_log_intr_mode(phba, phba->intr_mode);
13556
James Smartda0436e2009-05-22 14:51:39 -040013557 return PCI_ERS_RESULT_RECOVERED;
13558}
13559
13560/**
13561 * lpfc_io_resume_s4 - Method for resuming PCI I/O operation to SLI-4 device
13562 * @pdev: pointer to PCI device
13563 *
13564 * This routine is called from the PCI subsystem for error handling to device
13565 * with SLI-4 interface spec. It is called when kernel error recovery tells
13566 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
13567 * error recovery. After this call, traffic can start to flow from this device
13568 * again.
13569 **/
13570static void
13571lpfc_io_resume_s4(struct pci_dev *pdev)
13572{
James Smart75baf692010-06-08 18:31:21 -040013573 struct Scsi_Host *shost = pci_get_drvdata(pdev);
13574 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
13575
13576 /*
13577 * In case of slot reset, as function reset is performed through
13578 * mailbox command which needs DMA to be enabled, this operation
13579 * has to be moved to the io resume phase. Taking device offline
13580 * will perform the necessary cleanup.
13581 */
13582 if (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE)) {
13583 /* Perform device reset */
James Smart618a5232012-06-12 13:54:36 -040013584 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart75baf692010-06-08 18:31:21 -040013585 lpfc_offline(phba);
13586 lpfc_sli_brdrestart(phba);
13587 /* Bring the device back online */
13588 lpfc_online(phba);
13589 }
James Smartda0436e2009-05-22 14:51:39 -040013590}
13591
13592/**
James Smart3772a992009-05-22 14:50:54 -040013593 * lpfc_pci_probe_one - lpfc PCI probe func to reg dev to PCI subsystem
13594 * @pdev: pointer to PCI device
13595 * @pid: pointer to PCI device identifier
13596 *
13597 * This routine is to be registered to the kernel's PCI subsystem. When an
13598 * Emulex HBA device is presented on PCI bus, the kernel PCI subsystem looks
13599 * at PCI device-specific information of the device and driver to see if the
13600 * driver state that it can support this kind of device. If the match is
13601 * successful, the driver core invokes this routine. This routine dispatches
13602 * the action to the proper SLI-3 or SLI-4 device probing routine, which will
13603 * do all the initialization that it needs to do to handle the HBA device
13604 * properly.
13605 *
13606 * Return code
13607 * 0 - driver can claim the device
13608 * negative value - driver can not claim the device
13609 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080013610static int
James Smart3772a992009-05-22 14:50:54 -040013611lpfc_pci_probe_one(struct pci_dev *pdev, const struct pci_device_id *pid)
13612{
13613 int rc;
James Smart8fa38512009-07-19 10:01:03 -040013614 struct lpfc_sli_intf intf;
James Smart3772a992009-05-22 14:50:54 -040013615
James Smart28baac72010-02-12 14:42:03 -050013616 if (pci_read_config_dword(pdev, LPFC_SLI_INTF, &intf.word0))
James Smart3772a992009-05-22 14:50:54 -040013617 return -ENODEV;
13618
James Smart8fa38512009-07-19 10:01:03 -040013619 if ((bf_get(lpfc_sli_intf_valid, &intf) == LPFC_SLI_INTF_VALID) &&
James Smart28baac72010-02-12 14:42:03 -050013620 (bf_get(lpfc_sli_intf_slirev, &intf) == LPFC_SLI_INTF_REV_SLI4))
James Smartda0436e2009-05-22 14:51:39 -040013621 rc = lpfc_pci_probe_one_s4(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -040013622 else
James Smart3772a992009-05-22 14:50:54 -040013623 rc = lpfc_pci_probe_one_s3(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -040013624
James Smart3772a992009-05-22 14:50:54 -040013625 return rc;
13626}
13627
13628/**
13629 * lpfc_pci_remove_one - lpfc PCI func to unreg dev from PCI subsystem
13630 * @pdev: pointer to PCI device
13631 *
13632 * This routine is to be registered to the kernel's PCI subsystem. When an
13633 * Emulex HBA is removed from PCI bus, the driver core invokes this routine.
13634 * This routine dispatches the action to the proper SLI-3 or SLI-4 device
13635 * remove routine, which will perform all the necessary cleanup for the
13636 * device to be removed from the PCI subsystem properly.
13637 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080013638static void
James Smart3772a992009-05-22 14:50:54 -040013639lpfc_pci_remove_one(struct pci_dev *pdev)
13640{
13641 struct Scsi_Host *shost = pci_get_drvdata(pdev);
13642 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
13643
13644 switch (phba->pci_dev_grp) {
13645 case LPFC_PCI_DEV_LP:
13646 lpfc_pci_remove_one_s3(pdev);
13647 break;
James Smartda0436e2009-05-22 14:51:39 -040013648 case LPFC_PCI_DEV_OC:
13649 lpfc_pci_remove_one_s4(pdev);
13650 break;
James Smart3772a992009-05-22 14:50:54 -040013651 default:
13652 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13653 "1424 Invalid PCI device group: 0x%x\n",
13654 phba->pci_dev_grp);
13655 break;
13656 }
13657 return;
13658}
13659
13660/**
13661 * lpfc_pci_suspend_one - lpfc PCI func to suspend dev for power management
13662 * @pdev: pointer to PCI device
13663 * @msg: power management message
13664 *
13665 * This routine is to be registered to the kernel's PCI subsystem to support
13666 * system Power Management (PM). When PM invokes this method, it dispatches
13667 * the action to the proper SLI-3 or SLI-4 device suspend routine, which will
13668 * suspend the device.
13669 *
13670 * Return code
13671 * 0 - driver suspended the device
13672 * Error otherwise
13673 **/
13674static int
13675lpfc_pci_suspend_one(struct pci_dev *pdev, pm_message_t msg)
13676{
13677 struct Scsi_Host *shost = pci_get_drvdata(pdev);
13678 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
13679 int rc = -ENODEV;
13680
13681 switch (phba->pci_dev_grp) {
13682 case LPFC_PCI_DEV_LP:
13683 rc = lpfc_pci_suspend_one_s3(pdev, msg);
13684 break;
James Smartda0436e2009-05-22 14:51:39 -040013685 case LPFC_PCI_DEV_OC:
13686 rc = lpfc_pci_suspend_one_s4(pdev, msg);
13687 break;
James Smart3772a992009-05-22 14:50:54 -040013688 default:
13689 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13690 "1425 Invalid PCI device group: 0x%x\n",
13691 phba->pci_dev_grp);
13692 break;
13693 }
13694 return rc;
13695}
13696
13697/**
13698 * lpfc_pci_resume_one - lpfc PCI func to resume dev for power management
13699 * @pdev: pointer to PCI device
13700 *
13701 * This routine is to be registered to the kernel's PCI subsystem to support
13702 * system Power Management (PM). When PM invokes this method, it dispatches
13703 * the action to the proper SLI-3 or SLI-4 device resume routine, which will
13704 * resume the device.
13705 *
13706 * Return code
13707 * 0 - driver suspended the device
13708 * Error otherwise
13709 **/
13710static int
13711lpfc_pci_resume_one(struct pci_dev *pdev)
13712{
13713 struct Scsi_Host *shost = pci_get_drvdata(pdev);
13714 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
13715 int rc = -ENODEV;
13716
13717 switch (phba->pci_dev_grp) {
13718 case LPFC_PCI_DEV_LP:
13719 rc = lpfc_pci_resume_one_s3(pdev);
13720 break;
James Smartda0436e2009-05-22 14:51:39 -040013721 case LPFC_PCI_DEV_OC:
13722 rc = lpfc_pci_resume_one_s4(pdev);
13723 break;
James Smart3772a992009-05-22 14:50:54 -040013724 default:
13725 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13726 "1426 Invalid PCI device group: 0x%x\n",
13727 phba->pci_dev_grp);
13728 break;
13729 }
13730 return rc;
13731}
13732
13733/**
13734 * lpfc_io_error_detected - lpfc method for handling PCI I/O error
13735 * @pdev: pointer to PCI device.
13736 * @state: the current PCI connection state.
13737 *
13738 * This routine is registered to the PCI subsystem for error handling. This
13739 * function is called by the PCI subsystem after a PCI bus error affecting
13740 * this device has been detected. When this routine is invoked, it dispatches
13741 * the action to the proper SLI-3 or SLI-4 device error detected handling
13742 * routine, which will perform the proper error detected operation.
13743 *
13744 * Return codes
13745 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
13746 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
13747 **/
13748static pci_ers_result_t
13749lpfc_io_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
13750{
13751 struct Scsi_Host *shost = pci_get_drvdata(pdev);
13752 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
13753 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
13754
13755 switch (phba->pci_dev_grp) {
13756 case LPFC_PCI_DEV_LP:
13757 rc = lpfc_io_error_detected_s3(pdev, state);
13758 break;
James Smartda0436e2009-05-22 14:51:39 -040013759 case LPFC_PCI_DEV_OC:
13760 rc = lpfc_io_error_detected_s4(pdev, state);
13761 break;
James Smart3772a992009-05-22 14:50:54 -040013762 default:
13763 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13764 "1427 Invalid PCI device group: 0x%x\n",
13765 phba->pci_dev_grp);
13766 break;
13767 }
13768 return rc;
13769}
13770
13771/**
13772 * lpfc_io_slot_reset - lpfc method for restart PCI dev from scratch
13773 * @pdev: pointer to PCI device.
13774 *
13775 * This routine is registered to the PCI subsystem for error handling. This
13776 * function is called after PCI bus has been reset to restart the PCI card
13777 * from scratch, as if from a cold-boot. When this routine is invoked, it
13778 * dispatches the action to the proper SLI-3 or SLI-4 device reset handling
13779 * routine, which will perform the proper device reset.
13780 *
13781 * Return codes
13782 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
13783 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
13784 **/
13785static pci_ers_result_t
13786lpfc_io_slot_reset(struct pci_dev *pdev)
13787{
13788 struct Scsi_Host *shost = pci_get_drvdata(pdev);
13789 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
13790 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
13791
13792 switch (phba->pci_dev_grp) {
13793 case LPFC_PCI_DEV_LP:
13794 rc = lpfc_io_slot_reset_s3(pdev);
13795 break;
James Smartda0436e2009-05-22 14:51:39 -040013796 case LPFC_PCI_DEV_OC:
13797 rc = lpfc_io_slot_reset_s4(pdev);
13798 break;
James Smart3772a992009-05-22 14:50:54 -040013799 default:
13800 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13801 "1428 Invalid PCI device group: 0x%x\n",
13802 phba->pci_dev_grp);
13803 break;
13804 }
13805 return rc;
13806}
13807
13808/**
13809 * lpfc_io_resume - lpfc method for resuming PCI I/O operation
13810 * @pdev: pointer to PCI device
13811 *
13812 * This routine is registered to the PCI subsystem for error handling. It
13813 * is called when kernel error recovery tells the lpfc driver that it is
13814 * OK to resume normal PCI operation after PCI bus error recovery. When
13815 * this routine is invoked, it dispatches the action to the proper SLI-3
13816 * or SLI-4 device io_resume routine, which will resume the device operation.
13817 **/
13818static void
13819lpfc_io_resume(struct pci_dev *pdev)
13820{
13821 struct Scsi_Host *shost = pci_get_drvdata(pdev);
13822 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
13823
13824 switch (phba->pci_dev_grp) {
13825 case LPFC_PCI_DEV_LP:
13826 lpfc_io_resume_s3(pdev);
13827 break;
James Smartda0436e2009-05-22 14:51:39 -040013828 case LPFC_PCI_DEV_OC:
13829 lpfc_io_resume_s4(pdev);
13830 break;
James Smart3772a992009-05-22 14:50:54 -040013831 default:
13832 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13833 "1429 Invalid PCI device group: 0x%x\n",
13834 phba->pci_dev_grp);
13835 break;
13836 }
13837 return;
13838}
13839
James Smart1ba981f2014-02-20 09:56:45 -050013840/**
13841 * lpfc_sli4_oas_verify - Verify OAS is supported by this adapter
13842 * @phba: pointer to lpfc hba data structure.
13843 *
13844 * This routine checks to see if OAS is supported for this adapter. If
13845 * supported, the configure Flash Optimized Fabric flag is set. Otherwise,
13846 * the enable oas flag is cleared and the pool created for OAS device data
13847 * is destroyed.
13848 *
13849 **/
YueHaibingc7092972019-04-17 23:05:54 +080013850static void
James Smart1ba981f2014-02-20 09:56:45 -050013851lpfc_sli4_oas_verify(struct lpfc_hba *phba)
13852{
13853
13854 if (!phba->cfg_EnableXLane)
13855 return;
13856
13857 if (phba->sli4_hba.pc_sli4_params.oas_supported) {
13858 phba->cfg_fof = 1;
13859 } else {
James Smartf38fa0b2014-04-04 13:52:21 -040013860 phba->cfg_fof = 0;
Saurav Girepunjec3e5aac2019-10-27 01:17:17 +053013861 mempool_destroy(phba->device_data_mem_pool);
James Smart1ba981f2014-02-20 09:56:45 -050013862 phba->device_data_mem_pool = NULL;
13863 }
13864
13865 return;
13866}
13867
13868/**
James Smartd2cc9bc2018-09-10 10:30:50 -070013869 * lpfc_sli4_ras_init - Verify RAS-FW log is supported by this adapter
13870 * @phba: pointer to lpfc hba data structure.
13871 *
13872 * This routine checks to see if RAS is supported by the adapter. Check the
13873 * function through which RAS support enablement is to be done.
13874 **/
13875void
13876lpfc_sli4_ras_init(struct lpfc_hba *phba)
13877{
13878 switch (phba->pcidev->device) {
13879 case PCI_DEVICE_ID_LANCER_G6_FC:
13880 case PCI_DEVICE_ID_LANCER_G7_FC:
13881 phba->ras_fwlog.ras_hwsupport = true;
James Smartcb349902018-11-29 16:09:27 -080013882 if (phba->cfg_ras_fwlog_func == PCI_FUNC(phba->pcidev->devfn) &&
13883 phba->cfg_ras_fwlog_buffsize)
James Smartd2cc9bc2018-09-10 10:30:50 -070013884 phba->ras_fwlog.ras_enabled = true;
13885 else
13886 phba->ras_fwlog.ras_enabled = false;
13887 break;
13888 default:
13889 phba->ras_fwlog.ras_hwsupport = false;
13890 }
13891}
13892
James Smart1ba981f2014-02-20 09:56:45 -050013893
dea31012005-04-17 16:05:31 -050013894MODULE_DEVICE_TABLE(pci, lpfc_id_table);
13895
Stephen Hemmingera55b2d22012-09-07 09:33:16 -070013896static const struct pci_error_handlers lpfc_err_handler = {
Linas Vepstas8d63f372007-02-14 14:28:36 -060013897 .error_detected = lpfc_io_error_detected,
13898 .slot_reset = lpfc_io_slot_reset,
13899 .resume = lpfc_io_resume,
13900};
13901
dea31012005-04-17 16:05:31 -050013902static struct pci_driver lpfc_driver = {
13903 .name = LPFC_DRIVER_NAME,
13904 .id_table = lpfc_id_table,
13905 .probe = lpfc_pci_probe_one,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080013906 .remove = lpfc_pci_remove_one,
Anton Blanchard85e8a232017-02-13 08:49:20 +110013907 .shutdown = lpfc_pci_remove_one,
James Smart3a55b532008-12-04 22:38:54 -050013908 .suspend = lpfc_pci_suspend_one,
James Smart3772a992009-05-22 14:50:54 -040013909 .resume = lpfc_pci_resume_one,
James Smart2e0fef82007-06-17 19:56:36 -050013910 .err_handler = &lpfc_err_handler,
dea31012005-04-17 16:05:31 -050013911};
13912
James Smart3ef6d242012-01-18 16:23:48 -050013913static const struct file_operations lpfc_mgmt_fop = {
Al Viro858feac2013-04-14 22:39:37 -040013914 .owner = THIS_MODULE,
James Smart3ef6d242012-01-18 16:23:48 -050013915};
13916
13917static struct miscdevice lpfc_mgmt_dev = {
13918 .minor = MISC_DYNAMIC_MINOR,
13919 .name = "lpfcmgmt",
13920 .fops = &lpfc_mgmt_fop,
13921};
13922
James Smarte59058c2008-08-24 21:49:00 -040013923/**
James Smart3621a712009-04-06 18:47:14 -040013924 * lpfc_init - lpfc module initialization routine
James Smarte59058c2008-08-24 21:49:00 -040013925 *
13926 * This routine is to be invoked when the lpfc module is loaded into the
13927 * kernel. The special kernel macro module_init() is used to indicate the
13928 * role of this routine to the kernel as lpfc module entry point.
13929 *
13930 * Return codes
13931 * 0 - successful
13932 * -ENOMEM - FC attach transport failed
13933 * all others - failed
13934 */
dea31012005-04-17 16:05:31 -050013935static int __init
13936lpfc_init(void)
13937{
13938 int error = 0;
13939
13940 printk(LPFC_MODULE_DESC "\n");
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040013941 printk(LPFC_COPYRIGHT "\n");
dea31012005-04-17 16:05:31 -050013942
James Smart3ef6d242012-01-18 16:23:48 -050013943 error = misc_register(&lpfc_mgmt_dev);
13944 if (error)
13945 printk(KERN_ERR "Could not register lpfcmgmt device, "
13946 "misc_register returned with status %d", error);
13947
James Smart458c0832016-07-06 12:36:07 -070013948 lpfc_transport_functions.vport_create = lpfc_vport_create;
13949 lpfc_transport_functions.vport_delete = lpfc_vport_delete;
dea31012005-04-17 16:05:31 -050013950 lpfc_transport_template =
13951 fc_attach_transport(&lpfc_transport_functions);
James Smart7ee5d432007-10-27 13:37:17 -040013952 if (lpfc_transport_template == NULL)
dea31012005-04-17 16:05:31 -050013953 return -ENOMEM;
James Smart458c0832016-07-06 12:36:07 -070013954 lpfc_vport_transport_template =
13955 fc_attach_transport(&lpfc_vport_transport_functions);
13956 if (lpfc_vport_transport_template == NULL) {
13957 fc_release_transport(lpfc_transport_template);
13958 return -ENOMEM;
James Smart7ee5d432007-10-27 13:37:17 -040013959 }
James Smart5fd11082018-03-05 12:04:04 -080013960 lpfc_nvme_cmd_template();
James Smartbd3061b2018-03-05 12:04:05 -080013961 lpfc_nvmet_cmd_template();
James Smart7bb03bb2013-04-17 20:19:16 -040013962
13963 /* Initialize in case vector mapping is needed */
James Smart2ea259e2017-02-12 13:52:27 -080013964 lpfc_present_cpu = num_present_cpus();
James Smart7bb03bb2013-04-17 20:19:16 -040013965
James Smart93a4d6f2019-11-04 16:57:05 -080013966 error = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN,
13967 "lpfc/sli4:online",
13968 lpfc_cpu_online, lpfc_cpu_offline);
13969 if (error < 0)
13970 goto cpuhp_failure;
13971 lpfc_cpuhp_state = error;
13972
dea31012005-04-17 16:05:31 -050013973 error = pci_register_driver(&lpfc_driver);
James Smart93a4d6f2019-11-04 16:57:05 -080013974 if (error)
13975 goto unwind;
13976
13977 return error;
13978
13979unwind:
13980 cpuhp_remove_multi_state(lpfc_cpuhp_state);
13981cpuhp_failure:
13982 fc_release_transport(lpfc_transport_template);
13983 fc_release_transport(lpfc_vport_transport_template);
dea31012005-04-17 16:05:31 -050013984
13985 return error;
13986}
13987
James Smarte59058c2008-08-24 21:49:00 -040013988/**
James Smart3621a712009-04-06 18:47:14 -040013989 * lpfc_exit - lpfc module removal routine
James Smarte59058c2008-08-24 21:49:00 -040013990 *
13991 * This routine is invoked when the lpfc module is removed from the kernel.
13992 * The special kernel macro module_exit() is used to indicate the role of
13993 * this routine to the kernel as lpfc module exit point.
13994 */
dea31012005-04-17 16:05:31 -050013995static void __exit
13996lpfc_exit(void)
13997{
James Smart3ef6d242012-01-18 16:23:48 -050013998 misc_deregister(&lpfc_mgmt_dev);
dea31012005-04-17 16:05:31 -050013999 pci_unregister_driver(&lpfc_driver);
James Smart93a4d6f2019-11-04 16:57:05 -080014000 cpuhp_remove_multi_state(lpfc_cpuhp_state);
dea31012005-04-17 16:05:31 -050014001 fc_release_transport(lpfc_transport_template);
James Smart458c0832016-07-06 12:36:07 -070014002 fc_release_transport(lpfc_vport_transport_template);
Johannes Thumshirn79739672015-08-31 16:48:11 -040014003 idr_destroy(&lpfc_hba_index);
dea31012005-04-17 16:05:31 -050014004}
14005
14006module_init(lpfc_init);
14007module_exit(lpfc_exit);
14008MODULE_LICENSE("GPL");
14009MODULE_DESCRIPTION(LPFC_MODULE_DESC);
James Smartd080abe2017-02-12 13:52:39 -080014010MODULE_AUTHOR("Broadcom");
dea31012005-04-17 16:05:31 -050014011MODULE_VERSION("0:" LPFC_DRIVER_VERSION);