blob: c549212a2b49f5c009e0541f044cc76136fb0eac [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>
dea31012005-04-17 16:05:31 -050043
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -040044#include <scsi/scsi.h>
dea31012005-04-17 16:05:31 -050045#include <scsi/scsi_device.h>
46#include <scsi/scsi_host.h>
47#include <scsi/scsi_transport_fc.h>
James Smart86c67372017-04-21 16:05:04 -070048#include <scsi/scsi_tcq.h>
49#include <scsi/fc/fc_fs.h>
50
51#include <linux/nvme-fc-driver.h>
dea31012005-04-17 16:05:31 -050052
James Smartda0436e2009-05-22 14:51:39 -040053#include "lpfc_hw4.h"
dea31012005-04-17 16:05:31 -050054#include "lpfc_hw.h"
55#include "lpfc_sli.h"
James Smartda0436e2009-05-22 14:51:39 -040056#include "lpfc_sli4.h"
James Smartea2151b2008-09-07 11:52:10 -040057#include "lpfc_nl.h"
dea31012005-04-17 16:05:31 -050058#include "lpfc_disc.h"
dea31012005-04-17 16:05:31 -050059#include "lpfc.h"
James Smart895427b2017-02-12 13:52:30 -080060#include "lpfc_scsi.h"
61#include "lpfc_nvme.h"
James Smart86c67372017-04-21 16:05:04 -070062#include "lpfc_nvmet.h"
dea31012005-04-17 16:05:31 -050063#include "lpfc_logmsg.h"
64#include "lpfc_crtn.h"
James Smart92d7f7b2007-06-17 19:56:38 -050065#include "lpfc_vport.h"
dea31012005-04-17 16:05:31 -050066#include "lpfc_version.h"
James Smart12f44452016-07-06 12:36:03 -070067#include "lpfc_ids.h"
dea31012005-04-17 16:05:31 -050068
James Smart81301a92008-12-04 22:39:46 -050069char *_dump_buf_data;
70unsigned long _dump_buf_data_order;
71char *_dump_buf_dif;
72unsigned long _dump_buf_dif_order;
73spinlock_t _dump_buf_lock;
74
James Smart7bb03bb2013-04-17 20:19:16 -040075/* Used when mapping IRQ vectors in a driver centric manner */
YueHaibingd7b761b2019-05-31 23:28:41 +080076static uint32_t lpfc_present_cpu;
James Smart7bb03bb2013-04-17 20:19:16 -040077
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
1085 spin_lock(&qp->abts_scsi_buf_list_lock);
1086 list_splice_init(&qp->lpfc_abts_scsi_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;
1097 qp->abts_scsi_io_bufs = 0;
1098 spin_unlock(&qp->io_buf_list_put_lock);
1099 spin_unlock(&qp->abts_scsi_buf_list_lock);
James Smart86c67372017-04-21 16:05:04 -07001100
James Smart5e5b5112019-01-28 11:14:22 -08001101 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
1102 spin_lock(&qp->abts_nvme_buf_list_lock);
1103 list_splice_init(&qp->lpfc_abts_nvme_buf_list,
1104 &nvme_aborts);
1105 list_for_each_entry_safe(psb, psb_next, &nvme_aborts,
1106 list) {
1107 psb->pCmd = NULL;
1108 psb->status = IOSTAT_SUCCESS;
1109 cnt++;
1110 }
1111 spin_lock(&qp->io_buf_list_put_lock);
1112 qp->put_io_bufs += qp->abts_nvme_io_bufs;
1113 qp->abts_nvme_io_bufs = 0;
1114 list_splice_init(&nvme_aborts,
1115 &qp->lpfc_io_buf_list_put);
1116 spin_unlock(&qp->io_buf_list_put_lock);
1117 spin_unlock(&qp->abts_nvme_buf_list_lock);
1118
1119 }
1120 }
James Smart731eedc2019-03-12 16:30:12 -07001121 spin_unlock_irq(&phba->hbalock);
James Smart5e5b5112019-01-28 11:14:22 -08001122
1123 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
James Smart731eedc2019-03-12 16:30:12 -07001124 spin_lock_irq(&phba->sli4_hba.abts_nvmet_buf_list_lock);
James Smart5e5b5112019-01-28 11:14:22 -08001125 list_splice_init(&phba->sli4_hba.lpfc_abts_nvmet_ctx_list,
1126 &nvmet_aborts);
James Smart731eedc2019-03-12 16:30:12 -07001127 spin_unlock_irq(&phba->sli4_hba.abts_nvmet_buf_list_lock);
James Smart86c67372017-04-21 16:05:04 -07001128 list_for_each_entry_safe(ctxp, ctxp_next, &nvmet_aborts, list) {
1129 ctxp->flag &= ~(LPFC_NVMET_XBUSY | LPFC_NVMET_ABORT_OP);
James Smart6c621a22017-05-15 15:20:45 -07001130 lpfc_nvmet_ctxbuf_post(phba, ctxp->ctxbuf);
James Smart86c67372017-04-21 16:05:04 -07001131 }
James Smart895427b2017-02-12 13:52:30 -08001132 }
James Smart895427b2017-02-12 13:52:30 -08001133
James Smart68e814f2014-05-21 08:04:59 -04001134 lpfc_sli4_free_sp_events(phba);
James Smart5e5b5112019-01-28 11:14:22 -08001135 return cnt;
James Smartda0436e2009-05-22 14:51:39 -04001136}
1137
1138/**
1139 * lpfc_hba_down_post - Wrapper func for hba down post routine
1140 * @phba: pointer to lpfc HBA data structure.
1141 *
1142 * This routine wraps the actual SLI3 or SLI4 routine for performing
1143 * uninitialization after the HBA is reset when bring down the SLI Layer.
1144 *
1145 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001146 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -04001147 * Any other value - error.
1148 **/
1149int
1150lpfc_hba_down_post(struct lpfc_hba *phba)
1151{
1152 return (*phba->lpfc_hba_down_post)(phba);
1153}
Jamie Wellnitz41415862006-02-28 19:25:27 -05001154
James Smarte59058c2008-08-24 21:49:00 -04001155/**
James Smart3621a712009-04-06 18:47:14 -04001156 * lpfc_hb_timeout - The HBA-timer timeout handler
James Smarte59058c2008-08-24 21:49:00 -04001157 * @ptr: unsigned long holds the pointer to lpfc hba data structure.
1158 *
1159 * This is the HBA-timer timeout handler registered to the lpfc driver. When
1160 * this timer fires, a HBA timeout event shall be posted to the lpfc driver
1161 * work-port-events bitmap and the worker thread is notified. This timeout
1162 * event will be used by the worker thread to invoke the actual timeout
1163 * handler routine, lpfc_hb_timeout_handler. Any periodical operations will
1164 * be performed in the timeout handler and the HBA timeout event bit shall
1165 * be cleared by the worker thread after it has taken the event bitmap out.
1166 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001167static void
Kees Cookf22eb4d2017-09-06 20:24:26 -07001168lpfc_hb_timeout(struct timer_list *t)
James Smart858c9f62007-06-17 19:56:39 -05001169{
1170 struct lpfc_hba *phba;
James Smart5e9d9b82008-06-14 22:52:53 -04001171 uint32_t tmo_posted;
James Smart858c9f62007-06-17 19:56:39 -05001172 unsigned long iflag;
1173
Kees Cookf22eb4d2017-09-06 20:24:26 -07001174 phba = from_timer(phba, t, hb_tmofunc);
James Smart93996272008-08-24 21:50:30 -04001175
1176 /* Check for heart beat timeout conditions */
James Smart858c9f62007-06-17 19:56:39 -05001177 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -04001178 tmo_posted = phba->pport->work_port_events & WORKER_HB_TMO;
1179 if (!tmo_posted)
James Smart858c9f62007-06-17 19:56:39 -05001180 phba->pport->work_port_events |= WORKER_HB_TMO;
1181 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
1182
James Smart93996272008-08-24 21:50:30 -04001183 /* Tell the worker thread there is work to do */
James Smart5e9d9b82008-06-14 22:52:53 -04001184 if (!tmo_posted)
1185 lpfc_worker_wake_up(phba);
James Smart858c9f62007-06-17 19:56:39 -05001186 return;
1187}
1188
James Smarte59058c2008-08-24 21:49:00 -04001189/**
James Smart19ca7602010-11-20 23:11:55 -05001190 * lpfc_rrq_timeout - The RRQ-timer timeout handler
1191 * @ptr: unsigned long holds the pointer to lpfc hba data structure.
1192 *
1193 * This is the RRQ-timer timeout handler registered to the lpfc driver. When
1194 * this timer fires, a RRQ timeout event shall be posted to the lpfc driver
1195 * work-port-events bitmap and the worker thread is notified. This timeout
1196 * event will be used by the worker thread to invoke the actual timeout
1197 * handler routine, lpfc_rrq_handler. Any periodical operations will
1198 * be performed in the timeout handler and the RRQ timeout event bit shall
1199 * be cleared by the worker thread after it has taken the event bitmap out.
1200 **/
1201static void
Kees Cookf22eb4d2017-09-06 20:24:26 -07001202lpfc_rrq_timeout(struct timer_list *t)
James Smart19ca7602010-11-20 23:11:55 -05001203{
1204 struct lpfc_hba *phba;
James Smart19ca7602010-11-20 23:11:55 -05001205 unsigned long iflag;
1206
Kees Cookf22eb4d2017-09-06 20:24:26 -07001207 phba = from_timer(phba, t, rrq_tmr);
James Smart19ca7602010-11-20 23:11:55 -05001208 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart06918ac2014-02-20 09:57:57 -05001209 if (!(phba->pport->load_flag & FC_UNLOADING))
1210 phba->hba_flag |= HBA_RRQ_ACTIVE;
1211 else
1212 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
James Smart19ca7602010-11-20 23:11:55 -05001213 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
James Smart06918ac2014-02-20 09:57:57 -05001214
1215 if (!(phba->pport->load_flag & FC_UNLOADING))
1216 lpfc_worker_wake_up(phba);
James Smart19ca7602010-11-20 23:11:55 -05001217}
1218
1219/**
James Smart3621a712009-04-06 18:47:14 -04001220 * lpfc_hb_mbox_cmpl - The lpfc heart-beat mailbox command callback function
James Smarte59058c2008-08-24 21:49:00 -04001221 * @phba: pointer to lpfc hba data structure.
1222 * @pmboxq: pointer to the driver internal queue element for mailbox command.
1223 *
1224 * This is the callback function to the lpfc heart-beat mailbox command.
1225 * If configured, the lpfc driver issues the heart-beat mailbox command to
1226 * the HBA every LPFC_HB_MBOX_INTERVAL (current 5) seconds. At the time the
1227 * heart-beat mailbox command is issued, the driver shall set up heart-beat
1228 * timeout timer to LPFC_HB_MBOX_TIMEOUT (current 30) seconds and marks
1229 * heart-beat outstanding state. Once the mailbox command comes back and
1230 * no error conditions detected, the heart-beat mailbox command timer is
1231 * reset to LPFC_HB_MBOX_INTERVAL seconds and the heart-beat outstanding
1232 * state is cleared for the next heart-beat. If the timer expired with the
1233 * heart-beat outstanding state set, the driver will put the HBA offline.
1234 **/
James Smart858c9f62007-06-17 19:56:39 -05001235static void
1236lpfc_hb_mbox_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
1237{
1238 unsigned long drvr_flag;
1239
1240 spin_lock_irqsave(&phba->hbalock, drvr_flag);
1241 phba->hb_outstanding = 0;
1242 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
1243
James Smart93996272008-08-24 21:50:30 -04001244 /* Check and reset heart-beat timer is necessary */
James Smart858c9f62007-06-17 19:56:39 -05001245 mempool_free(pmboxq, phba->mbox_mem_pool);
1246 if (!(phba->pport->fc_flag & FC_OFFLINE_MODE) &&
1247 !(phba->link_state == LPFC_HBA_ERROR) &&
James Smart51ef4c22007-08-02 11:10:31 -04001248 !(phba->pport->load_flag & FC_UNLOADING))
James Smart858c9f62007-06-17 19:56:39 -05001249 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001250 jiffies +
1251 msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smart858c9f62007-06-17 19:56:39 -05001252 return;
1253}
1254
James Smart32517fc2019-01-28 11:14:33 -08001255static void
1256lpfc_hb_eq_delay_work(struct work_struct *work)
1257{
1258 struct lpfc_hba *phba = container_of(to_delayed_work(work),
1259 struct lpfc_hba, eq_delay_work);
1260 struct lpfc_eq_intr_info *eqi, *eqi_new;
1261 struct lpfc_queue *eq, *eq_next;
1262 unsigned char *eqcnt = NULL;
1263 uint32_t usdelay;
1264 int i;
James Smart8d34a592019-08-14 16:56:35 -07001265 bool update = false;
James Smart32517fc2019-01-28 11:14:33 -08001266
1267 if (!phba->cfg_auto_imax || phba->pport->load_flag & FC_UNLOADING)
1268 return;
1269
1270 if (phba->link_state == LPFC_HBA_ERROR ||
1271 phba->pport->fc_flag & FC_OFFLINE_MODE)
1272 goto requeue;
1273
1274 eqcnt = kcalloc(num_possible_cpus(), sizeof(unsigned char),
1275 GFP_KERNEL);
1276 if (!eqcnt)
1277 goto requeue;
1278
James Smart8d34a592019-08-14 16:56:35 -07001279 if (phba->cfg_irq_chann > 1) {
1280 /* Loop thru all IRQ vectors */
1281 for (i = 0; i < phba->cfg_irq_chann; i++) {
1282 /* Get the EQ corresponding to the IRQ vector */
1283 eq = phba->sli4_hba.hba_eq_hdl[i].eq;
1284 if (!eq)
1285 continue;
1286 if (eq->q_mode) {
1287 update = true;
1288 break;
1289 }
1290 if (eqcnt[eq->last_cpu] < 2)
1291 eqcnt[eq->last_cpu]++;
1292 }
1293 } else
1294 update = true;
James Smart32517fc2019-01-28 11:14:33 -08001295
1296 for_each_present_cpu(i) {
James Smart32517fc2019-01-28 11:14:33 -08001297 eqi = per_cpu_ptr(phba->sli4_hba.eq_info, i);
James Smart8d34a592019-08-14 16:56:35 -07001298 if (!update && eqcnt[i] < 2) {
1299 eqi->icnt = 0;
1300 continue;
1301 }
James Smart32517fc2019-01-28 11:14:33 -08001302
1303 usdelay = (eqi->icnt / LPFC_IMAX_THRESHOLD) *
1304 LPFC_EQ_DELAY_STEP;
1305 if (usdelay > LPFC_MAX_AUTO_EQ_DELAY)
1306 usdelay = LPFC_MAX_AUTO_EQ_DELAY;
1307
1308 eqi->icnt = 0;
1309
1310 list_for_each_entry_safe(eq, eq_next, &eqi->list, cpu_list) {
1311 if (eq->last_cpu != i) {
1312 eqi_new = per_cpu_ptr(phba->sli4_hba.eq_info,
1313 eq->last_cpu);
1314 list_move_tail(&eq->cpu_list, &eqi_new->list);
1315 continue;
1316 }
1317 if (usdelay != eq->q_mode)
1318 lpfc_modify_hba_eq_delay(phba, eq->hdwq, 1,
1319 usdelay);
1320 }
1321 }
1322
1323 kfree(eqcnt);
1324
1325requeue:
1326 queue_delayed_work(phba->wq, &phba->eq_delay_work,
1327 msecs_to_jiffies(LPFC_EQ_DELAY_MSECS));
1328}
1329
James Smarte59058c2008-08-24 21:49:00 -04001330/**
James Smartc4908502019-01-28 11:14:28 -08001331 * lpfc_hb_mxp_handler - Multi-XRI pools handler to adjust XRI distribution
1332 * @phba: pointer to lpfc hba data structure.
1333 *
1334 * For each heartbeat, this routine does some heuristic methods to adjust
1335 * XRI distribution. The goal is to fully utilize free XRIs.
1336 **/
1337static void lpfc_hb_mxp_handler(struct lpfc_hba *phba)
1338{
1339 u32 i;
1340 u32 hwq_count;
1341
1342 hwq_count = phba->cfg_hdw_queue;
1343 for (i = 0; i < hwq_count; i++) {
1344 /* Adjust XRIs in private pool */
1345 lpfc_adjust_pvt_pool_count(phba, i);
1346
1347 /* Adjust high watermark */
1348 lpfc_adjust_high_watermark(phba, i);
1349
1350#ifdef LPFC_MXP_STAT
1351 /* Snapshot pbl, pvt and busy count */
1352 lpfc_snapshot_mxp(phba, i);
1353#endif
1354 }
1355}
1356
James Smarte59058c2008-08-24 21:49:00 -04001357/**
James Smart3621a712009-04-06 18:47:14 -04001358 * lpfc_hb_timeout_handler - The HBA-timer timeout handler
James Smarte59058c2008-08-24 21:49:00 -04001359 * @phba: pointer to lpfc hba data structure.
1360 *
1361 * This is the actual HBA-timer timeout handler to be invoked by the worker
1362 * thread whenever the HBA timer fired and HBA-timeout event posted. This
1363 * handler performs any periodic operations needed for the device. If such
1364 * periodic event has already been attended to either in the interrupt handler
1365 * or by processing slow-ring or fast-ring events within the HBA-timer
1366 * timeout window (LPFC_HB_MBOX_INTERVAL), this handler just simply resets
1367 * the timer for the next timeout period. If lpfc heart-beat mailbox command
1368 * is configured and there is no heart-beat mailbox command outstanding, a
1369 * heart-beat mailbox is issued and timer set properly. Otherwise, if there
1370 * has been a heart-beat mailbox command outstanding, the HBA shall be put
1371 * to offline.
1372 **/
James Smart858c9f62007-06-17 19:56:39 -05001373void
1374lpfc_hb_timeout_handler(struct lpfc_hba *phba)
1375{
James Smart45ed1192009-10-02 15:17:02 -04001376 struct lpfc_vport **vports;
James Smart858c9f62007-06-17 19:56:39 -05001377 LPFC_MBOXQ_t *pmboxq;
James Smart0ff10d42008-01-11 01:52:36 -05001378 struct lpfc_dmabuf *buf_ptr;
James Smart45ed1192009-10-02 15:17:02 -04001379 int retval, i;
James Smart858c9f62007-06-17 19:56:39 -05001380 struct lpfc_sli *psli = &phba->sli;
James Smart0ff10d42008-01-11 01:52:36 -05001381 LIST_HEAD(completions);
James Smart858c9f62007-06-17 19:56:39 -05001382
James Smartc4908502019-01-28 11:14:28 -08001383 if (phba->cfg_xri_rebalancing) {
1384 /* Multi-XRI pools handler */
1385 lpfc_hb_mxp_handler(phba);
1386 }
James Smart858c9f62007-06-17 19:56:39 -05001387
James Smart45ed1192009-10-02 15:17:02 -04001388 vports = lpfc_create_vport_work_array(phba);
1389 if (vports != NULL)
James Smart4258e982015-12-16 18:11:58 -05001390 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart45ed1192009-10-02 15:17:02 -04001391 lpfc_rcv_seq_check_edtov(vports[i]);
James Smart4258e982015-12-16 18:11:58 -05001392 lpfc_fdmi_num_disc_check(vports[i]);
1393 }
James Smart45ed1192009-10-02 15:17:02 -04001394 lpfc_destroy_vport_work_array(phba, vports);
1395
James Smart858c9f62007-06-17 19:56:39 -05001396 if ((phba->link_state == LPFC_HBA_ERROR) ||
James Smart51ef4c22007-08-02 11:10:31 -04001397 (phba->pport->load_flag & FC_UNLOADING) ||
James Smart858c9f62007-06-17 19:56:39 -05001398 (phba->pport->fc_flag & FC_OFFLINE_MODE))
1399 return;
1400
1401 spin_lock_irq(&phba->pport->work_port_lock);
James Smart858c9f62007-06-17 19:56:39 -05001402
James Smart256ec0d2013-04-17 20:14:58 -04001403 if (time_after(phba->last_completion_time +
1404 msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL),
1405 jiffies)) {
James Smart858c9f62007-06-17 19:56:39 -05001406 spin_unlock_irq(&phba->pport->work_port_lock);
1407 if (!phba->hb_outstanding)
1408 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001409 jiffies +
1410 msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smart858c9f62007-06-17 19:56:39 -05001411 else
1412 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001413 jiffies +
1414 msecs_to_jiffies(1000 * LPFC_HB_MBOX_TIMEOUT));
James Smart858c9f62007-06-17 19:56:39 -05001415 return;
1416 }
1417 spin_unlock_irq(&phba->pport->work_port_lock);
1418
James Smart0ff10d42008-01-11 01:52:36 -05001419 if (phba->elsbuf_cnt &&
1420 (phba->elsbuf_cnt == phba->elsbuf_prev_cnt)) {
1421 spin_lock_irq(&phba->hbalock);
1422 list_splice_init(&phba->elsbuf, &completions);
1423 phba->elsbuf_cnt = 0;
1424 phba->elsbuf_prev_cnt = 0;
1425 spin_unlock_irq(&phba->hbalock);
1426
1427 while (!list_empty(&completions)) {
1428 list_remove_head(&completions, buf_ptr,
1429 struct lpfc_dmabuf, list);
1430 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
1431 kfree(buf_ptr);
1432 }
1433 }
1434 phba->elsbuf_prev_cnt = phba->elsbuf_cnt;
1435
James Smart858c9f62007-06-17 19:56:39 -05001436 /* If there is no heart beat outstanding, issue a heartbeat command */
James Smart13815c82008-01-11 01:52:48 -05001437 if (phba->cfg_enable_hba_heartbeat) {
1438 if (!phba->hb_outstanding) {
James Smartbc739052010-08-04 16:11:18 -04001439 if ((!(psli->sli_flag & LPFC_SLI_MBOX_ACTIVE)) &&
1440 (list_empty(&psli->mboxq))) {
1441 pmboxq = mempool_alloc(phba->mbox_mem_pool,
1442 GFP_KERNEL);
1443 if (!pmboxq) {
1444 mod_timer(&phba->hb_tmofunc,
1445 jiffies +
James Smart256ec0d2013-04-17 20:14:58 -04001446 msecs_to_jiffies(1000 *
1447 LPFC_HB_MBOX_INTERVAL));
James Smartbc739052010-08-04 16:11:18 -04001448 return;
1449 }
James Smart13815c82008-01-11 01:52:48 -05001450
James Smartbc739052010-08-04 16:11:18 -04001451 lpfc_heart_beat(phba, pmboxq);
1452 pmboxq->mbox_cmpl = lpfc_hb_mbox_cmpl;
1453 pmboxq->vport = phba->pport;
1454 retval = lpfc_sli_issue_mbox(phba, pmboxq,
1455 MBX_NOWAIT);
James Smart13815c82008-01-11 01:52:48 -05001456
James Smartbc739052010-08-04 16:11:18 -04001457 if (retval != MBX_BUSY &&
1458 retval != MBX_SUCCESS) {
1459 mempool_free(pmboxq,
1460 phba->mbox_mem_pool);
1461 mod_timer(&phba->hb_tmofunc,
1462 jiffies +
James Smart256ec0d2013-04-17 20:14:58 -04001463 msecs_to_jiffies(1000 *
1464 LPFC_HB_MBOX_INTERVAL));
James Smartbc739052010-08-04 16:11:18 -04001465 return;
1466 }
1467 phba->skipped_hb = 0;
1468 phba->hb_outstanding = 1;
1469 } else if (time_before_eq(phba->last_completion_time,
1470 phba->skipped_hb)) {
1471 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
1472 "2857 Last completion time not "
1473 " updated in %d ms\n",
1474 jiffies_to_msecs(jiffies
1475 - phba->last_completion_time));
1476 } else
1477 phba->skipped_hb = jiffies;
1478
James Smart858c9f62007-06-17 19:56:39 -05001479 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001480 jiffies +
1481 msecs_to_jiffies(1000 * LPFC_HB_MBOX_TIMEOUT));
James Smart858c9f62007-06-17 19:56:39 -05001482 return;
James Smart13815c82008-01-11 01:52:48 -05001483 } else {
1484 /*
1485 * If heart beat timeout called with hb_outstanding set
James Smartdcf2a4e2010-09-29 11:18:53 -04001486 * we need to give the hb mailbox cmd a chance to
1487 * complete or TMO.
James Smart13815c82008-01-11 01:52:48 -05001488 */
James Smartdcf2a4e2010-09-29 11:18:53 -04001489 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
1490 "0459 Adapter heartbeat still out"
1491 "standing:last compl time was %d ms.\n",
1492 jiffies_to_msecs(jiffies
1493 - phba->last_completion_time));
1494 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001495 jiffies +
1496 msecs_to_jiffies(1000 * LPFC_HB_MBOX_TIMEOUT));
James Smart858c9f62007-06-17 19:56:39 -05001497 }
James Smart4258e982015-12-16 18:11:58 -05001498 } else {
1499 mod_timer(&phba->hb_tmofunc,
1500 jiffies +
1501 msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smart858c9f62007-06-17 19:56:39 -05001502 }
1503}
1504
James Smarte59058c2008-08-24 21:49:00 -04001505/**
James Smart3621a712009-04-06 18:47:14 -04001506 * lpfc_offline_eratt - Bring lpfc offline on hardware error attention
James Smarte59058c2008-08-24 21:49:00 -04001507 * @phba: pointer to lpfc hba data structure.
1508 *
1509 * This routine is called to bring the HBA offline when HBA hardware error
1510 * other than Port Error 6 has been detected.
1511 **/
James Smart09372822008-01-11 01:52:54 -05001512static void
1513lpfc_offline_eratt(struct lpfc_hba *phba)
1514{
1515 struct lpfc_sli *psli = &phba->sli;
1516
1517 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001518 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart09372822008-01-11 01:52:54 -05001519 spin_unlock_irq(&phba->hbalock);
James Smart618a5232012-06-12 13:54:36 -04001520 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
James Smart09372822008-01-11 01:52:54 -05001521
1522 lpfc_offline(phba);
1523 lpfc_reset_barrier(phba);
James Smartf4b4c682009-05-22 14:53:12 -04001524 spin_lock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001525 lpfc_sli_brdreset(phba);
James Smartf4b4c682009-05-22 14:53:12 -04001526 spin_unlock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001527 lpfc_hba_down_post(phba);
1528 lpfc_sli_brdready(phba, HS_MBRDY);
1529 lpfc_unblock_mgmt_io(phba);
1530 phba->link_state = LPFC_HBA_ERROR;
1531 return;
1532}
1533
James Smarte59058c2008-08-24 21:49:00 -04001534/**
James Smartda0436e2009-05-22 14:51:39 -04001535 * lpfc_sli4_offline_eratt - Bring lpfc offline on SLI4 hardware error attention
1536 * @phba: pointer to lpfc hba data structure.
1537 *
1538 * This routine is called to bring a SLI4 HBA offline when HBA hardware error
1539 * other than Port Error 6 has been detected.
1540 **/
James Smarta88dbb62013-04-17 20:18:39 -04001541void
James Smartda0436e2009-05-22 14:51:39 -04001542lpfc_sli4_offline_eratt(struct lpfc_hba *phba)
1543{
James Smart946727d2015-04-07 15:07:09 -04001544 spin_lock_irq(&phba->hbalock);
1545 phba->link_state = LPFC_HBA_ERROR;
1546 spin_unlock_irq(&phba->hbalock);
1547
James Smart618a5232012-06-12 13:54:36 -04001548 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
James Smartda0436e2009-05-22 14:51:39 -04001549 lpfc_offline(phba);
James Smartda0436e2009-05-22 14:51:39 -04001550 lpfc_hba_down_post(phba);
James Smartda0436e2009-05-22 14:51:39 -04001551 lpfc_unblock_mgmt_io(phba);
James Smartda0436e2009-05-22 14:51:39 -04001552}
1553
1554/**
James Smarta257bf92009-04-06 18:48:10 -04001555 * lpfc_handle_deferred_eratt - The HBA hardware deferred error handler
1556 * @phba: pointer to lpfc hba data structure.
1557 *
1558 * This routine is invoked to handle the deferred HBA hardware error
1559 * conditions. This type of error is indicated by HBA by setting ER1
1560 * and another ER bit in the host status register. The driver will
1561 * wait until the ER1 bit clears before handling the error condition.
1562 **/
1563static void
1564lpfc_handle_deferred_eratt(struct lpfc_hba *phba)
1565{
1566 uint32_t old_host_status = phba->work_hs;
James Smarta257bf92009-04-06 18:48:10 -04001567 struct lpfc_sli *psli = &phba->sli;
1568
James Smartf4b4c682009-05-22 14:53:12 -04001569 /* If the pci channel is offline, ignore possible errors,
1570 * since we cannot communicate with the pci card anyway.
1571 */
1572 if (pci_channel_offline(phba->pcidev)) {
1573 spin_lock_irq(&phba->hbalock);
1574 phba->hba_flag &= ~DEFER_ERATT;
1575 spin_unlock_irq(&phba->hbalock);
1576 return;
1577 }
1578
James Smarta257bf92009-04-06 18:48:10 -04001579 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1580 "0479 Deferred Adapter Hardware Error "
1581 "Data: x%x x%x x%x\n",
1582 phba->work_hs,
1583 phba->work_status[0], phba->work_status[1]);
1584
1585 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001586 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smarta257bf92009-04-06 18:48:10 -04001587 spin_unlock_irq(&phba->hbalock);
1588
1589
1590 /*
1591 * Firmware stops when it triggred erratt. That could cause the I/Os
1592 * dropped by the firmware. Error iocb (I/O) on txcmplq and let the
1593 * SCSI layer retry it after re-establishing link.
1594 */
James Smartdb55fba2014-04-04 13:52:02 -04001595 lpfc_sli_abort_fcp_rings(phba);
James Smarta257bf92009-04-06 18:48:10 -04001596
1597 /*
1598 * There was a firmware error. Take the hba offline and then
1599 * attempt to restart it.
1600 */
James Smart618a5232012-06-12 13:54:36 -04001601 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smarta257bf92009-04-06 18:48:10 -04001602 lpfc_offline(phba);
1603
1604 /* Wait for the ER1 bit to clear.*/
1605 while (phba->work_hs & HS_FFER1) {
1606 msleep(100);
James Smart9940b972011-03-11 16:06:12 -05001607 if (lpfc_readl(phba->HSregaddr, &phba->work_hs)) {
1608 phba->work_hs = UNPLUG_ERR ;
1609 break;
1610 }
James Smarta257bf92009-04-06 18:48:10 -04001611 /* If driver is unloading let the worker thread continue */
1612 if (phba->pport->load_flag & FC_UNLOADING) {
1613 phba->work_hs = 0;
1614 break;
1615 }
1616 }
1617
1618 /*
1619 * This is to ptrotect against a race condition in which
1620 * first write to the host attention register clear the
1621 * host status register.
1622 */
1623 if ((!phba->work_hs) && (!(phba->pport->load_flag & FC_UNLOADING)))
1624 phba->work_hs = old_host_status & ~HS_FFER1;
1625
James Smart3772a992009-05-22 14:50:54 -04001626 spin_lock_irq(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -04001627 phba->hba_flag &= ~DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -04001628 spin_unlock_irq(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -04001629 phba->work_status[0] = readl(phba->MBslimaddr + 0xa8);
1630 phba->work_status[1] = readl(phba->MBslimaddr + 0xac);
1631}
1632
James Smart3772a992009-05-22 14:50:54 -04001633static void
1634lpfc_board_errevt_to_mgmt(struct lpfc_hba *phba)
1635{
1636 struct lpfc_board_event_header board_event;
1637 struct Scsi_Host *shost;
1638
1639 board_event.event_type = FC_REG_BOARD_EVENT;
1640 board_event.subcategory = LPFC_EVENT_PORTINTERR;
1641 shost = lpfc_shost_from_vport(phba->pport);
1642 fc_host_post_vendor_event(shost, fc_get_event_number(),
1643 sizeof(board_event),
1644 (char *) &board_event,
1645 LPFC_NL_VENDOR_ID);
1646}
1647
James Smarta257bf92009-04-06 18:48:10 -04001648/**
James Smart3772a992009-05-22 14:50:54 -04001649 * lpfc_handle_eratt_s3 - The SLI3 HBA hardware error handler
James Smarte59058c2008-08-24 21:49:00 -04001650 * @phba: pointer to lpfc hba data structure.
1651 *
1652 * This routine is invoked to handle the following HBA hardware error
1653 * conditions:
1654 * 1 - HBA error attention interrupt
1655 * 2 - DMA ring index out of range
1656 * 3 - Mailbox command came back as unknown
1657 **/
James Smart3772a992009-05-22 14:50:54 -04001658static void
1659lpfc_handle_eratt_s3(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001660{
James Smart2e0fef82007-06-17 19:56:36 -05001661 struct lpfc_vport *vport = phba->pport;
James Smart2e0fef82007-06-17 19:56:36 -05001662 struct lpfc_sli *psli = &phba->sli;
James Smartd2873e42006-08-18 17:46:43 -04001663 uint32_t event_data;
James Smart57127f12007-10-27 13:37:05 -04001664 unsigned long temperature;
1665 struct temp_event temp_event_data;
James Smart92d7f7b2007-06-17 19:56:38 -05001666 struct Scsi_Host *shost;
James Smart2e0fef82007-06-17 19:56:36 -05001667
Linas Vepstas8d63f372007-02-14 14:28:36 -06001668 /* If the pci channel is offline, ignore possible errors,
James Smart3772a992009-05-22 14:50:54 -04001669 * since we cannot communicate with the pci card anyway.
1670 */
1671 if (pci_channel_offline(phba->pcidev)) {
1672 spin_lock_irq(&phba->hbalock);
1673 phba->hba_flag &= ~DEFER_ERATT;
1674 spin_unlock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -06001675 return;
James Smart3772a992009-05-22 14:50:54 -04001676 }
1677
James Smart13815c82008-01-11 01:52:48 -05001678 /* If resets are disabled then leave the HBA alone and return */
1679 if (!phba->cfg_enable_hba_reset)
1680 return;
dea31012005-04-17 16:05:31 -05001681
James Smartea2151b2008-09-07 11:52:10 -04001682 /* Send an internal error event to mgmt application */
James Smart3772a992009-05-22 14:50:54 -04001683 lpfc_board_errevt_to_mgmt(phba);
James Smartea2151b2008-09-07 11:52:10 -04001684
James Smarta257bf92009-04-06 18:48:10 -04001685 if (phba->hba_flag & DEFER_ERATT)
1686 lpfc_handle_deferred_eratt(phba);
1687
James Smartdcf2a4e2010-09-29 11:18:53 -04001688 if ((phba->work_hs & HS_FFER6) || (phba->work_hs & HS_FFER8)) {
1689 if (phba->work_hs & HS_FFER6)
1690 /* Re-establishing Link */
1691 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1692 "1301 Re-establishing Link "
1693 "Data: x%x x%x x%x\n",
1694 phba->work_hs, phba->work_status[0],
1695 phba->work_status[1]);
1696 if (phba->work_hs & HS_FFER8)
1697 /* Device Zeroization */
1698 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1699 "2861 Host Authentication device "
1700 "zeroization Data:x%x x%x x%x\n",
1701 phba->work_hs, phba->work_status[0],
1702 phba->work_status[1]);
James Smart58da1ff2008-04-07 10:15:56 -04001703
James Smart92d7f7b2007-06-17 19:56:38 -05001704 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001705 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -05001706 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001707
1708 /*
1709 * Firmware stops when it triggled erratt with HS_FFER6.
1710 * That could cause the I/Os dropped by the firmware.
1711 * Error iocb (I/O) on txcmplq and let the SCSI layer
1712 * retry it after re-establishing link.
1713 */
James Smartdb55fba2014-04-04 13:52:02 -04001714 lpfc_sli_abort_fcp_rings(phba);
dea31012005-04-17 16:05:31 -05001715
dea31012005-04-17 16:05:31 -05001716 /*
1717 * There was a firmware error. Take the hba offline and then
1718 * attempt to restart it.
1719 */
James Smart618a5232012-06-12 13:54:36 -04001720 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
dea31012005-04-17 16:05:31 -05001721 lpfc_offline(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -05001722 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05001723 if (lpfc_online(phba) == 0) { /* Initialize the HBA */
James Smart46fa3112007-04-25 09:51:45 -04001724 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05001725 return;
1726 }
James Smart46fa3112007-04-25 09:51:45 -04001727 lpfc_unblock_mgmt_io(phba);
James Smart57127f12007-10-27 13:37:05 -04001728 } else if (phba->work_hs & HS_CRIT_TEMP) {
1729 temperature = readl(phba->MBslimaddr + TEMPERATURE_OFFSET);
1730 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
1731 temp_event_data.event_code = LPFC_CRIT_TEMP;
1732 temp_event_data.data = (uint32_t)temperature;
1733
1734 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -04001735 "0406 Adapter maximum temperature exceeded "
James Smart57127f12007-10-27 13:37:05 -04001736 "(%ld), taking this port offline "
1737 "Data: x%x x%x x%x\n",
1738 temperature, phba->work_hs,
1739 phba->work_status[0], phba->work_status[1]);
1740
1741 shost = lpfc_shost_from_vport(phba->pport);
1742 fc_host_post_vendor_event(shost, fc_get_event_number(),
1743 sizeof(temp_event_data),
1744 (char *) &temp_event_data,
1745 SCSI_NL_VID_TYPE_PCI
1746 | PCI_VENDOR_ID_EMULEX);
1747
James Smart7af67052007-10-27 13:38:11 -04001748 spin_lock_irq(&phba->hbalock);
James Smart7af67052007-10-27 13:38:11 -04001749 phba->over_temp_state = HBA_OVER_TEMP;
1750 spin_unlock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001751 lpfc_offline_eratt(phba);
James Smart57127f12007-10-27 13:37:05 -04001752
dea31012005-04-17 16:05:31 -05001753 } else {
1754 /* The if clause above forces this code path when the status
James Smart93996272008-08-24 21:50:30 -04001755 * failure is a value other than FFER6. Do not call the offline
1756 * twice. This is the adapter hardware error path.
dea31012005-04-17 16:05:31 -05001757 */
1758 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001759 "0457 Adapter Hardware Error "
dea31012005-04-17 16:05:31 -05001760 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001761 phba->work_hs,
dea31012005-04-17 16:05:31 -05001762 phba->work_status[0], phba->work_status[1]);
1763
James Smartd2873e42006-08-18 17:46:43 -04001764 event_data = FC_REG_DUMP_EVENT;
James Smart92d7f7b2007-06-17 19:56:38 -05001765 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05001766 fc_host_post_vendor_event(shost, fc_get_event_number(),
James Smartd2873e42006-08-18 17:46:43 -04001767 sizeof(event_data), (char *) &event_data,
1768 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
1769
James Smart09372822008-01-11 01:52:54 -05001770 lpfc_offline_eratt(phba);
dea31012005-04-17 16:05:31 -05001771 }
James Smart93996272008-08-24 21:50:30 -04001772 return;
dea31012005-04-17 16:05:31 -05001773}
1774
James Smarte59058c2008-08-24 21:49:00 -04001775/**
James Smart618a5232012-06-12 13:54:36 -04001776 * lpfc_sli4_port_sta_fn_reset - The SLI4 function reset due to port status reg
1777 * @phba: pointer to lpfc hba data structure.
1778 * @mbx_action: flag for mailbox shutdown action.
1779 *
1780 * This routine is invoked to perform an SLI4 port PCI function reset in
1781 * response to port status register polling attention. It waits for port
1782 * status register (ERR, RDY, RN) bits before proceeding with function reset.
1783 * During this process, interrupt vectors are freed and later requested
1784 * for handling possible port resource change.
1785 **/
1786static int
James Smarte10b2022014-02-20 09:57:43 -05001787lpfc_sli4_port_sta_fn_reset(struct lpfc_hba *phba, int mbx_action,
1788 bool en_rn_msg)
James Smart618a5232012-06-12 13:54:36 -04001789{
1790 int rc;
1791 uint32_t intr_mode;
1792
James Smart27d6ac02018-02-22 08:18:42 -08001793 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) >=
James Smart65791f12016-07-06 12:35:56 -07001794 LPFC_SLI_INTF_IF_TYPE_2) {
1795 /*
1796 * On error status condition, driver need to wait for port
1797 * ready before performing reset.
1798 */
1799 rc = lpfc_sli4_pdev_status_reg_wait(phba);
James Smart0e916ee2016-07-06 12:36:06 -07001800 if (rc)
James Smart65791f12016-07-06 12:35:56 -07001801 return rc;
James Smart618a5232012-06-12 13:54:36 -04001802 }
James Smart0e916ee2016-07-06 12:36:06 -07001803
James Smart65791f12016-07-06 12:35:56 -07001804 /* need reset: attempt for port recovery */
1805 if (en_rn_msg)
1806 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1807 "2887 Reset Needed: Attempting Port "
1808 "Recovery...\n");
1809 lpfc_offline_prep(phba, mbx_action);
1810 lpfc_offline(phba);
1811 /* release interrupt for possible resource change */
1812 lpfc_sli4_disable_intr(phba);
James Smart5a9eeff2018-11-29 16:09:32 -08001813 rc = lpfc_sli_brdrestart(phba);
1814 if (rc) {
1815 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1816 "6309 Failed to restart board\n");
1817 return rc;
1818 }
James Smart65791f12016-07-06 12:35:56 -07001819 /* request and enable interrupt */
1820 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
1821 if (intr_mode == LPFC_INTR_ERROR) {
1822 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1823 "3175 Failed to enable interrupt\n");
1824 return -EIO;
1825 }
1826 phba->intr_mode = intr_mode;
1827 rc = lpfc_online(phba);
1828 if (rc == 0)
1829 lpfc_unblock_mgmt_io(phba);
1830
James Smart618a5232012-06-12 13:54:36 -04001831 return rc;
1832}
1833
1834/**
James Smartda0436e2009-05-22 14:51:39 -04001835 * lpfc_handle_eratt_s4 - The SLI4 HBA hardware error handler
1836 * @phba: pointer to lpfc hba data structure.
1837 *
1838 * This routine is invoked to handle the SLI4 HBA hardware error attention
1839 * conditions.
1840 **/
1841static void
1842lpfc_handle_eratt_s4(struct lpfc_hba *phba)
1843{
1844 struct lpfc_vport *vport = phba->pport;
1845 uint32_t event_data;
1846 struct Scsi_Host *shost;
James Smart2fcee4b2010-12-15 17:57:46 -05001847 uint32_t if_type;
James Smart2e90f4b2011-12-13 13:22:37 -05001848 struct lpfc_register portstat_reg = {0};
1849 uint32_t reg_err1, reg_err2;
1850 uint32_t uerrlo_reg, uemasklo_reg;
James Smart65791f12016-07-06 12:35:56 -07001851 uint32_t smphr_port_status = 0, pci_rd_rc1, pci_rd_rc2;
James Smarte10b2022014-02-20 09:57:43 -05001852 bool en_rn_msg = true;
James Smart946727d2015-04-07 15:07:09 -04001853 struct temp_event temp_event_data;
James Smart65791f12016-07-06 12:35:56 -07001854 struct lpfc_register portsmphr_reg;
1855 int rc, i;
James Smartda0436e2009-05-22 14:51:39 -04001856
1857 /* If the pci channel is offline, ignore possible errors, since
1858 * we cannot communicate with the pci card anyway.
1859 */
James Smart32a93102019-03-12 16:30:13 -07001860 if (pci_channel_offline(phba->pcidev)) {
1861 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1862 "3166 pci channel is offline\n");
1863 lpfc_sli4_offline_eratt(phba);
James Smartda0436e2009-05-22 14:51:39 -04001864 return;
James Smart32a93102019-03-12 16:30:13 -07001865 }
James Smartda0436e2009-05-22 14:51:39 -04001866
James Smart65791f12016-07-06 12:35:56 -07001867 memset(&portsmphr_reg, 0, sizeof(portsmphr_reg));
James Smart2fcee4b2010-12-15 17:57:46 -05001868 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
1869 switch (if_type) {
1870 case LPFC_SLI_INTF_IF_TYPE_0:
James Smart2e90f4b2011-12-13 13:22:37 -05001871 pci_rd_rc1 = lpfc_readl(
1872 phba->sli4_hba.u.if_type0.UERRLOregaddr,
1873 &uerrlo_reg);
1874 pci_rd_rc2 = lpfc_readl(
1875 phba->sli4_hba.u.if_type0.UEMASKLOregaddr,
1876 &uemasklo_reg);
1877 /* consider PCI bus read error as pci_channel_offline */
1878 if (pci_rd_rc1 == -EIO && pci_rd_rc2 == -EIO)
1879 return;
James Smart65791f12016-07-06 12:35:56 -07001880 if (!(phba->hba_flag & HBA_RECOVERABLE_UE)) {
1881 lpfc_sli4_offline_eratt(phba);
1882 return;
1883 }
1884 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1885 "7623 Checking UE recoverable");
1886
1887 for (i = 0; i < phba->sli4_hba.ue_to_sr / 1000; i++) {
1888 if (lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
1889 &portsmphr_reg.word0))
1890 continue;
1891
1892 smphr_port_status = bf_get(lpfc_port_smphr_port_status,
1893 &portsmphr_reg);
1894 if ((smphr_port_status & LPFC_PORT_SEM_MASK) ==
1895 LPFC_PORT_SEM_UE_RECOVERABLE)
1896 break;
1897 /*Sleep for 1Sec, before checking SEMAPHORE */
1898 msleep(1000);
1899 }
1900
1901 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1902 "4827 smphr_port_status x%x : Waited %dSec",
1903 smphr_port_status, i);
1904
1905 /* Recoverable UE, reset the HBA device */
1906 if ((smphr_port_status & LPFC_PORT_SEM_MASK) ==
1907 LPFC_PORT_SEM_UE_RECOVERABLE) {
1908 for (i = 0; i < 20; i++) {
1909 msleep(1000);
1910 if (!lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
1911 &portsmphr_reg.word0) &&
1912 (LPFC_POST_STAGE_PORT_READY ==
1913 bf_get(lpfc_port_smphr_port_status,
1914 &portsmphr_reg))) {
1915 rc = lpfc_sli4_port_sta_fn_reset(phba,
1916 LPFC_MBX_NO_WAIT, en_rn_msg);
1917 if (rc == 0)
1918 return;
1919 lpfc_printf_log(phba,
1920 KERN_ERR, LOG_INIT,
1921 "4215 Failed to recover UE");
1922 break;
1923 }
1924 }
1925 }
1926 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1927 "7624 Firmware not ready: Failing UE recovery,"
1928 " waited %dSec", i);
James Smart8c24a4f2019-08-14 16:56:53 -07001929 phba->link_state = LPFC_HBA_ERROR;
James Smart2fcee4b2010-12-15 17:57:46 -05001930 break;
James Smart946727d2015-04-07 15:07:09 -04001931
James Smart2fcee4b2010-12-15 17:57:46 -05001932 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart27d6ac02018-02-22 08:18:42 -08001933 case LPFC_SLI_INTF_IF_TYPE_6:
James Smart2e90f4b2011-12-13 13:22:37 -05001934 pci_rd_rc1 = lpfc_readl(
1935 phba->sli4_hba.u.if_type2.STATUSregaddr,
1936 &portstat_reg.word0);
1937 /* consider PCI bus read error as pci_channel_offline */
James Smart6b5151f2012-01-18 16:24:06 -05001938 if (pci_rd_rc1 == -EIO) {
1939 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1940 "3151 PCI bus read access failure: x%x\n",
1941 readl(phba->sli4_hba.u.if_type2.STATUSregaddr));
James Smart32a93102019-03-12 16:30:13 -07001942 lpfc_sli4_offline_eratt(phba);
James Smart2e90f4b2011-12-13 13:22:37 -05001943 return;
James Smart6b5151f2012-01-18 16:24:06 -05001944 }
James Smart2e90f4b2011-12-13 13:22:37 -05001945 reg_err1 = readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
1946 reg_err2 = readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
James Smart2fcee4b2010-12-15 17:57:46 -05001947 if (bf_get(lpfc_sliport_status_oti, &portstat_reg)) {
James Smart2fcee4b2010-12-15 17:57:46 -05001948 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1949 "2889 Port Overtemperature event, "
James Smart946727d2015-04-07 15:07:09 -04001950 "taking port offline Data: x%x x%x\n",
1951 reg_err1, reg_err2);
1952
James Smart310429e2016-07-06 12:35:54 -07001953 phba->sfp_alarm |= LPFC_TRANSGRESSION_HIGH_TEMPERATURE;
James Smart946727d2015-04-07 15:07:09 -04001954 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
1955 temp_event_data.event_code = LPFC_CRIT_TEMP;
1956 temp_event_data.data = 0xFFFFFFFF;
1957
1958 shost = lpfc_shost_from_vport(phba->pport);
1959 fc_host_post_vendor_event(shost, fc_get_event_number(),
1960 sizeof(temp_event_data),
1961 (char *)&temp_event_data,
1962 SCSI_NL_VID_TYPE_PCI
1963 | PCI_VENDOR_ID_EMULEX);
1964
James Smart2fcee4b2010-12-15 17:57:46 -05001965 spin_lock_irq(&phba->hbalock);
1966 phba->over_temp_state = HBA_OVER_TEMP;
1967 spin_unlock_irq(&phba->hbalock);
1968 lpfc_sli4_offline_eratt(phba);
James Smart946727d2015-04-07 15:07:09 -04001969 return;
James Smart2fcee4b2010-12-15 17:57:46 -05001970 }
James Smart2e90f4b2011-12-13 13:22:37 -05001971 if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
James Smarte10b2022014-02-20 09:57:43 -05001972 reg_err2 == SLIPORT_ERR2_REG_FW_RESTART) {
James Smart2e90f4b2011-12-13 13:22:37 -05001973 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte10b2022014-02-20 09:57:43 -05001974 "3143 Port Down: Firmware Update "
1975 "Detected\n");
1976 en_rn_msg = false;
1977 } else if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
James Smart2e90f4b2011-12-13 13:22:37 -05001978 reg_err2 == SLIPORT_ERR2_REG_FORCED_DUMP)
1979 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1980 "3144 Port Down: Debug Dump\n");
1981 else if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1982 reg_err2 == SLIPORT_ERR2_REG_FUNC_PROVISON)
1983 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1984 "3145 Port Down: Provisioning\n");
James Smart618a5232012-06-12 13:54:36 -04001985
James Smart946727d2015-04-07 15:07:09 -04001986 /* If resets are disabled then leave the HBA alone and return */
1987 if (!phba->cfg_enable_hba_reset)
1988 return;
1989
James Smart618a5232012-06-12 13:54:36 -04001990 /* Check port status register for function reset */
James Smarte10b2022014-02-20 09:57:43 -05001991 rc = lpfc_sli4_port_sta_fn_reset(phba, LPFC_MBX_NO_WAIT,
1992 en_rn_msg);
James Smart618a5232012-06-12 13:54:36 -04001993 if (rc == 0) {
1994 /* don't report event on forced debug dump */
1995 if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1996 reg_err2 == SLIPORT_ERR2_REG_FORCED_DUMP)
1997 return;
1998 else
1999 break;
James Smart2fcee4b2010-12-15 17:57:46 -05002000 }
James Smart618a5232012-06-12 13:54:36 -04002001 /* fall through for not able to recover */
James Smart6b5151f2012-01-18 16:24:06 -05002002 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart8c24a4f2019-08-14 16:56:53 -07002003 "3152 Unrecoverable error\n");
2004 phba->link_state = LPFC_HBA_ERROR;
James Smart2fcee4b2010-12-15 17:57:46 -05002005 break;
2006 case LPFC_SLI_INTF_IF_TYPE_1:
2007 default:
2008 break;
2009 }
James Smart2e90f4b2011-12-13 13:22:37 -05002010 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
2011 "3123 Report dump event to upper layer\n");
2012 /* Send an internal error event to mgmt application */
2013 lpfc_board_errevt_to_mgmt(phba);
2014
2015 event_data = FC_REG_DUMP_EVENT;
2016 shost = lpfc_shost_from_vport(vport);
2017 fc_host_post_vendor_event(shost, fc_get_event_number(),
2018 sizeof(event_data), (char *) &event_data,
2019 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
James Smartda0436e2009-05-22 14:51:39 -04002020}
2021
2022/**
2023 * lpfc_handle_eratt - Wrapper func for handling hba error attention
2024 * @phba: pointer to lpfc HBA data structure.
2025 *
2026 * This routine wraps the actual SLI3 or SLI4 hba error attention handling
2027 * routine from the API jump table function pointer from the lpfc_hba struct.
2028 *
2029 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002030 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -04002031 * Any other value - error.
2032 **/
2033void
2034lpfc_handle_eratt(struct lpfc_hba *phba)
2035{
2036 (*phba->lpfc_handle_eratt)(phba);
2037}
2038
2039/**
James Smart3621a712009-04-06 18:47:14 -04002040 * lpfc_handle_latt - The HBA link event handler
James Smarte59058c2008-08-24 21:49:00 -04002041 * @phba: pointer to lpfc hba data structure.
2042 *
2043 * This routine is invoked from the worker thread to handle a HBA host
James Smart895427b2017-02-12 13:52:30 -08002044 * attention link event. SLI3 only.
James Smarte59058c2008-08-24 21:49:00 -04002045 **/
dea31012005-04-17 16:05:31 -05002046void
James Smart2e0fef82007-06-17 19:56:36 -05002047lpfc_handle_latt(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002048{
James Smart2e0fef82007-06-17 19:56:36 -05002049 struct lpfc_vport *vport = phba->pport;
2050 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05002051 LPFC_MBOXQ_t *pmb;
2052 volatile uint32_t control;
2053 struct lpfc_dmabuf *mp;
James Smart09372822008-01-11 01:52:54 -05002054 int rc = 0;
dea31012005-04-17 16:05:31 -05002055
2056 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart09372822008-01-11 01:52:54 -05002057 if (!pmb) {
2058 rc = 1;
dea31012005-04-17 16:05:31 -05002059 goto lpfc_handle_latt_err_exit;
James Smart09372822008-01-11 01:52:54 -05002060 }
dea31012005-04-17 16:05:31 -05002061
2062 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
James Smart09372822008-01-11 01:52:54 -05002063 if (!mp) {
2064 rc = 2;
dea31012005-04-17 16:05:31 -05002065 goto lpfc_handle_latt_free_pmb;
James Smart09372822008-01-11 01:52:54 -05002066 }
dea31012005-04-17 16:05:31 -05002067
2068 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
James Smart09372822008-01-11 01:52:54 -05002069 if (!mp->virt) {
2070 rc = 3;
dea31012005-04-17 16:05:31 -05002071 goto lpfc_handle_latt_free_mp;
James Smart09372822008-01-11 01:52:54 -05002072 }
dea31012005-04-17 16:05:31 -05002073
James.Smart@Emulex.Com6281bfe2005-11-28 11:41:33 -05002074 /* Cleanup any outstanding ELS commands */
James Smart549e55c2007-08-02 11:09:51 -04002075 lpfc_els_flush_all_cmd(phba);
dea31012005-04-17 16:05:31 -05002076
2077 psli->slistat.link_event++;
James Smart76a95d72010-11-20 23:11:48 -05002078 lpfc_read_topology(phba, pmb, mp);
2079 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
James Smart2e0fef82007-06-17 19:56:36 -05002080 pmb->vport = vport;
James Smart0d2b6b82008-06-14 22:52:47 -04002081 /* Block ELS IOCBs until we have processed this mbox command */
James Smart895427b2017-02-12 13:52:30 -08002082 phba->sli.sli3_ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
James Smart0b727fe2007-10-27 13:37:25 -04002083 rc = lpfc_sli_issue_mbox (phba, pmb, MBX_NOWAIT);
James Smart09372822008-01-11 01:52:54 -05002084 if (rc == MBX_NOT_FINISHED) {
2085 rc = 4;
James Smart14691152006-12-02 13:34:28 -05002086 goto lpfc_handle_latt_free_mbuf;
James Smart09372822008-01-11 01:52:54 -05002087 }
dea31012005-04-17 16:05:31 -05002088
2089 /* Clear Link Attention in HA REG */
James Smart2e0fef82007-06-17 19:56:36 -05002090 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002091 writel(HA_LATT, phba->HAregaddr);
2092 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05002093 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002094
2095 return;
2096
James Smart14691152006-12-02 13:34:28 -05002097lpfc_handle_latt_free_mbuf:
James Smart895427b2017-02-12 13:52:30 -08002098 phba->sli.sli3_ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart14691152006-12-02 13:34:28 -05002099 lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea31012005-04-17 16:05:31 -05002100lpfc_handle_latt_free_mp:
2101 kfree(mp);
2102lpfc_handle_latt_free_pmb:
James Smart1dcb58e2007-04-25 09:51:30 -04002103 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002104lpfc_handle_latt_err_exit:
2105 /* Enable Link attention interrupts */
James Smart2e0fef82007-06-17 19:56:36 -05002106 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002107 psli->sli_flag |= LPFC_PROCESS_LA;
2108 control = readl(phba->HCregaddr);
2109 control |= HC_LAINT_ENA;
2110 writel(control, phba->HCregaddr);
2111 readl(phba->HCregaddr); /* flush */
2112
2113 /* Clear Link Attention in HA REG */
2114 writel(HA_LATT, phba->HAregaddr);
2115 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05002116 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002117 lpfc_linkdown(phba);
James Smart2e0fef82007-06-17 19:56:36 -05002118 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002119
James Smart09372822008-01-11 01:52:54 -05002120 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
2121 "0300 LATT: Cannot issue READ_LA: Data:%d\n", rc);
dea31012005-04-17 16:05:31 -05002122
2123 return;
2124}
2125
James Smarte59058c2008-08-24 21:49:00 -04002126/**
James Smart3621a712009-04-06 18:47:14 -04002127 * lpfc_parse_vpd - Parse VPD (Vital Product Data)
James Smarte59058c2008-08-24 21:49:00 -04002128 * @phba: pointer to lpfc hba data structure.
2129 * @vpd: pointer to the vital product data.
2130 * @len: length of the vital product data in bytes.
2131 *
2132 * This routine parses the Vital Product Data (VPD). The VPD is treated as
2133 * an array of characters. In this routine, the ModelName, ProgramType, and
2134 * ModelDesc, etc. fields of the phba data structure will be populated.
2135 *
2136 * Return codes
2137 * 0 - pointer to the VPD passed in is NULL
2138 * 1 - success
2139 **/
James Smart3772a992009-05-22 14:50:54 -04002140int
James Smart2e0fef82007-06-17 19:56:36 -05002141lpfc_parse_vpd(struct lpfc_hba *phba, uint8_t *vpd, int len)
dea31012005-04-17 16:05:31 -05002142{
2143 uint8_t lenlo, lenhi;
Anton Blanchard07da60c2007-03-21 08:41:47 -05002144 int Length;
dea31012005-04-17 16:05:31 -05002145 int i, j;
2146 int finished = 0;
2147 int index = 0;
2148
2149 if (!vpd)
2150 return 0;
2151
2152 /* Vital Product */
James Smarted957682007-06-17 19:56:37 -05002153 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002154 "0455 Vital Product Data: x%x x%x x%x x%x\n",
dea31012005-04-17 16:05:31 -05002155 (uint32_t) vpd[0], (uint32_t) vpd[1], (uint32_t) vpd[2],
2156 (uint32_t) vpd[3]);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002157 while (!finished && (index < (len - 4))) {
dea31012005-04-17 16:05:31 -05002158 switch (vpd[index]) {
2159 case 0x82:
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002160 case 0x91:
dea31012005-04-17 16:05:31 -05002161 index += 1;
2162 lenlo = vpd[index];
2163 index += 1;
2164 lenhi = vpd[index];
2165 index += 1;
2166 i = ((((unsigned short)lenhi) << 8) + lenlo);
2167 index += i;
2168 break;
2169 case 0x90:
2170 index += 1;
2171 lenlo = vpd[index];
2172 index += 1;
2173 lenhi = vpd[index];
2174 index += 1;
2175 Length = ((((unsigned short)lenhi) << 8) + lenlo);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002176 if (Length > len - index)
2177 Length = len - index;
dea31012005-04-17 16:05:31 -05002178 while (Length > 0) {
2179 /* Look for Serial Number */
2180 if ((vpd[index] == 'S') && (vpd[index+1] == 'N')) {
2181 index += 2;
2182 i = vpd[index];
2183 index += 1;
2184 j = 0;
2185 Length -= (3+i);
2186 while(i--) {
2187 phba->SerialNumber[j++] = vpd[index++];
2188 if (j == 31)
2189 break;
2190 }
2191 phba->SerialNumber[j] = 0;
2192 continue;
2193 }
2194 else if ((vpd[index] == 'V') && (vpd[index+1] == '1')) {
2195 phba->vpd_flag |= VPD_MODEL_DESC;
2196 index += 2;
2197 i = vpd[index];
2198 index += 1;
2199 j = 0;
2200 Length -= (3+i);
2201 while(i--) {
2202 phba->ModelDesc[j++] = vpd[index++];
2203 if (j == 255)
2204 break;
2205 }
2206 phba->ModelDesc[j] = 0;
2207 continue;
2208 }
2209 else if ((vpd[index] == 'V') && (vpd[index+1] == '2')) {
2210 phba->vpd_flag |= VPD_MODEL_NAME;
2211 index += 2;
2212 i = vpd[index];
2213 index += 1;
2214 j = 0;
2215 Length -= (3+i);
2216 while(i--) {
2217 phba->ModelName[j++] = vpd[index++];
2218 if (j == 79)
2219 break;
2220 }
2221 phba->ModelName[j] = 0;
2222 continue;
2223 }
2224 else if ((vpd[index] == 'V') && (vpd[index+1] == '3')) {
2225 phba->vpd_flag |= VPD_PROGRAM_TYPE;
2226 index += 2;
2227 i = vpd[index];
2228 index += 1;
2229 j = 0;
2230 Length -= (3+i);
2231 while(i--) {
2232 phba->ProgramType[j++] = vpd[index++];
2233 if (j == 255)
2234 break;
2235 }
2236 phba->ProgramType[j] = 0;
2237 continue;
2238 }
2239 else if ((vpd[index] == 'V') && (vpd[index+1] == '4')) {
2240 phba->vpd_flag |= VPD_PORT;
2241 index += 2;
2242 i = vpd[index];
2243 index += 1;
2244 j = 0;
2245 Length -= (3+i);
2246 while(i--) {
James Smartcd1c8302011-10-10 21:33:25 -04002247 if ((phba->sli_rev == LPFC_SLI_REV4) &&
2248 (phba->sli4_hba.pport_name_sta ==
2249 LPFC_SLI4_PPNAME_GET)) {
2250 j++;
2251 index++;
2252 } else
2253 phba->Port[j++] = vpd[index++];
2254 if (j == 19)
2255 break;
dea31012005-04-17 16:05:31 -05002256 }
James Smartcd1c8302011-10-10 21:33:25 -04002257 if ((phba->sli_rev != LPFC_SLI_REV4) ||
2258 (phba->sli4_hba.pport_name_sta ==
2259 LPFC_SLI4_PPNAME_NON))
2260 phba->Port[j] = 0;
dea31012005-04-17 16:05:31 -05002261 continue;
2262 }
2263 else {
2264 index += 2;
2265 i = vpd[index];
2266 index += 1;
2267 index += i;
2268 Length -= (3 + i);
2269 }
2270 }
2271 finished = 0;
2272 break;
2273 case 0x78:
2274 finished = 1;
2275 break;
2276 default:
2277 index ++;
2278 break;
2279 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002280 }
dea31012005-04-17 16:05:31 -05002281
2282 return(1);
2283}
2284
James Smarte59058c2008-08-24 21:49:00 -04002285/**
James Smart3621a712009-04-06 18:47:14 -04002286 * lpfc_get_hba_model_desc - Retrieve HBA device model name and description
James Smarte59058c2008-08-24 21:49:00 -04002287 * @phba: pointer to lpfc hba data structure.
2288 * @mdp: pointer to the data structure to hold the derived model name.
2289 * @descp: pointer to the data structure to hold the derived description.
2290 *
2291 * This routine retrieves HBA's description based on its registered PCI device
2292 * ID. The @descp passed into this function points to an array of 256 chars. It
2293 * shall be returned with the model name, maximum speed, and the host bus type.
2294 * The @mdp passed into this function points to an array of 80 chars. When the
2295 * function returns, the @mdp will be filled with the model name.
2296 **/
dea31012005-04-17 16:05:31 -05002297static void
James Smart2e0fef82007-06-17 19:56:36 -05002298lpfc_get_hba_model_desc(struct lpfc_hba *phba, uint8_t *mdp, uint8_t *descp)
dea31012005-04-17 16:05:31 -05002299{
2300 lpfc_vpd_t *vp;
James.Smart@Emulex.Comfefcb2b2005-11-28 15:08:56 -05002301 uint16_t dev_id = phba->pcidev->device;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002302 int max_speed;
James Smart84774a42008-08-24 21:50:06 -04002303 int GE = 0;
James Smartda0436e2009-05-22 14:51:39 -04002304 int oneConnect = 0; /* default is not a oneConnect */
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002305 struct {
James Smarta747c9c2009-11-18 15:41:10 -05002306 char *name;
2307 char *bus;
2308 char *function;
2309 } m = {"<Unknown>", "", ""};
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002310
2311 if (mdp && mdp[0] != '\0'
2312 && descp && descp[0] != '\0')
2313 return;
2314
James Smartfbd8a6b2018-02-22 08:18:45 -08002315 if (phba->lmt & LMT_64Gb)
2316 max_speed = 64;
2317 else if (phba->lmt & LMT_32Gb)
James Smartd38dd522015-08-31 16:48:17 -04002318 max_speed = 32;
2319 else if (phba->lmt & LMT_16Gb)
James Smartc0c11512011-05-24 11:41:34 -04002320 max_speed = 16;
2321 else if (phba->lmt & LMT_10Gb)
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002322 max_speed = 10;
2323 else if (phba->lmt & LMT_8Gb)
2324 max_speed = 8;
2325 else if (phba->lmt & LMT_4Gb)
2326 max_speed = 4;
2327 else if (phba->lmt & LMT_2Gb)
2328 max_speed = 2;
James Smart4169d862012-09-29 11:32:09 -04002329 else if (phba->lmt & LMT_1Gb)
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002330 max_speed = 1;
James Smart4169d862012-09-29 11:32:09 -04002331 else
2332 max_speed = 0;
dea31012005-04-17 16:05:31 -05002333
2334 vp = &phba->vpd;
dea31012005-04-17 16:05:31 -05002335
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002336 switch (dev_id) {
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04002337 case PCI_DEVICE_ID_FIREFLY:
James Smart12222f42014-05-21 08:05:19 -04002338 m = (typeof(m)){"LP6000", "PCI",
2339 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04002340 break;
dea31012005-04-17 16:05:31 -05002341 case PCI_DEVICE_ID_SUPERFLY:
2342 if (vp->rev.biuRev >= 1 && vp->rev.biuRev <= 3)
James Smart12222f42014-05-21 08:05:19 -04002343 m = (typeof(m)){"LP7000", "PCI", ""};
dea31012005-04-17 16:05:31 -05002344 else
James Smart12222f42014-05-21 08:05:19 -04002345 m = (typeof(m)){"LP7000E", "PCI", ""};
2346 m.function = "Obsolete, Unsupported Fibre Channel Adapter";
dea31012005-04-17 16:05:31 -05002347 break;
2348 case PCI_DEVICE_ID_DRAGONFLY:
James Smarta747c9c2009-11-18 15:41:10 -05002349 m = (typeof(m)){"LP8000", "PCI",
James Smart12222f42014-05-21 08:05:19 -04002350 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002351 break;
2352 case PCI_DEVICE_ID_CENTAUR:
2353 if (FC_JEDEC_ID(vp->rev.biuRev) == CENTAUR_2G_JEDEC_ID)
James Smart12222f42014-05-21 08:05:19 -04002354 m = (typeof(m)){"LP9002", "PCI", ""};
dea31012005-04-17 16:05:31 -05002355 else
James Smart12222f42014-05-21 08:05:19 -04002356 m = (typeof(m)){"LP9000", "PCI", ""};
2357 m.function = "Obsolete, Unsupported Fibre Channel Adapter";
dea31012005-04-17 16:05:31 -05002358 break;
2359 case PCI_DEVICE_ID_RFLY:
James Smarta747c9c2009-11-18 15:41:10 -05002360 m = (typeof(m)){"LP952", "PCI",
James Smart12222f42014-05-21 08:05:19 -04002361 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002362 break;
2363 case PCI_DEVICE_ID_PEGASUS:
James Smarta747c9c2009-11-18 15:41:10 -05002364 m = (typeof(m)){"LP9802", "PCI-X",
James Smart12222f42014-05-21 08:05:19 -04002365 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002366 break;
2367 case PCI_DEVICE_ID_THOR:
James Smarta747c9c2009-11-18 15:41:10 -05002368 m = (typeof(m)){"LP10000", "PCI-X",
James Smart12222f42014-05-21 08:05:19 -04002369 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002370 break;
2371 case PCI_DEVICE_ID_VIPER:
James Smarta747c9c2009-11-18 15:41:10 -05002372 m = (typeof(m)){"LPX1000", "PCI-X",
James Smart12222f42014-05-21 08:05:19 -04002373 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002374 break;
2375 case PCI_DEVICE_ID_PFLY:
James Smarta747c9c2009-11-18 15:41:10 -05002376 m = (typeof(m)){"LP982", "PCI-X",
James Smart12222f42014-05-21 08:05:19 -04002377 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002378 break;
2379 case PCI_DEVICE_ID_TFLY:
James Smarta747c9c2009-11-18 15:41:10 -05002380 m = (typeof(m)){"LP1050", "PCI-X",
James Smart12222f42014-05-21 08:05:19 -04002381 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002382 break;
2383 case PCI_DEVICE_ID_HELIOS:
James Smarta747c9c2009-11-18 15:41:10 -05002384 m = (typeof(m)){"LP11000", "PCI-X2",
James Smart12222f42014-05-21 08:05:19 -04002385 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002386 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002387 case PCI_DEVICE_ID_HELIOS_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002388 m = (typeof(m)){"LP11000-SP", "PCI-X2",
James Smart12222f42014-05-21 08:05:19 -04002389 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002390 break;
2391 case PCI_DEVICE_ID_HELIOS_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002392 m = (typeof(m)){"LP11002-SP", "PCI-X2",
James Smart12222f42014-05-21 08:05:19 -04002393 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002394 break;
2395 case PCI_DEVICE_ID_NEPTUNE:
James Smart12222f42014-05-21 08:05:19 -04002396 m = (typeof(m)){"LPe1000", "PCIe",
2397 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002398 break;
2399 case PCI_DEVICE_ID_NEPTUNE_SCSP:
James Smart12222f42014-05-21 08:05:19 -04002400 m = (typeof(m)){"LPe1000-SP", "PCIe",
2401 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002402 break;
2403 case PCI_DEVICE_ID_NEPTUNE_DCSP:
James Smart12222f42014-05-21 08:05:19 -04002404 m = (typeof(m)){"LPe1002-SP", "PCIe",
2405 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002406 break;
dea31012005-04-17 16:05:31 -05002407 case PCI_DEVICE_ID_BMID:
James Smarta747c9c2009-11-18 15:41:10 -05002408 m = (typeof(m)){"LP1150", "PCI-X2", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002409 break;
2410 case PCI_DEVICE_ID_BSMB:
James Smart12222f42014-05-21 08:05:19 -04002411 m = (typeof(m)){"LP111", "PCI-X2",
2412 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002413 break;
2414 case PCI_DEVICE_ID_ZEPHYR:
James Smarta747c9c2009-11-18 15:41:10 -05002415 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002416 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002417 case PCI_DEVICE_ID_ZEPHYR_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002418 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002419 break;
2420 case PCI_DEVICE_ID_ZEPHYR_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002421 m = (typeof(m)){"LP2105", "PCIe", "FCoE Adapter"};
James Smarta257bf92009-04-06 18:48:10 -04002422 GE = 1;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002423 break;
dea31012005-04-17 16:05:31 -05002424 case PCI_DEVICE_ID_ZMID:
James Smarta747c9c2009-11-18 15:41:10 -05002425 m = (typeof(m)){"LPe1150", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002426 break;
2427 case PCI_DEVICE_ID_ZSMB:
James Smarta747c9c2009-11-18 15:41:10 -05002428 m = (typeof(m)){"LPe111", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002429 break;
2430 case PCI_DEVICE_ID_LP101:
James Smart12222f42014-05-21 08:05:19 -04002431 m = (typeof(m)){"LP101", "PCI-X",
2432 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002433 break;
2434 case PCI_DEVICE_ID_LP10000S:
James Smart12222f42014-05-21 08:05:19 -04002435 m = (typeof(m)){"LP10000-S", "PCI",
2436 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04002437 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002438 case PCI_DEVICE_ID_LP11000S:
James Smart12222f42014-05-21 08:05:19 -04002439 m = (typeof(m)){"LP11000-S", "PCI-X2",
2440 "Obsolete, Unsupported Fibre Channel Adapter"};
James Smart18a3b592006-12-02 13:35:08 -05002441 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002442 case PCI_DEVICE_ID_LPE11000S:
James Smart12222f42014-05-21 08:05:19 -04002443 m = (typeof(m)){"LPe11000-S", "PCIe",
2444 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Com5cc36b32005-11-28 11:42:19 -05002445 break;
James Smartb87eab32007-04-25 09:53:28 -04002446 case PCI_DEVICE_ID_SAT:
James Smarta747c9c2009-11-18 15:41:10 -05002447 m = (typeof(m)){"LPe12000", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002448 break;
2449 case PCI_DEVICE_ID_SAT_MID:
James Smarta747c9c2009-11-18 15:41:10 -05002450 m = (typeof(m)){"LPe1250", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002451 break;
2452 case PCI_DEVICE_ID_SAT_SMB:
James Smarta747c9c2009-11-18 15:41:10 -05002453 m = (typeof(m)){"LPe121", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002454 break;
2455 case PCI_DEVICE_ID_SAT_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002456 m = (typeof(m)){"LPe12002-SP", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002457 break;
2458 case PCI_DEVICE_ID_SAT_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002459 m = (typeof(m)){"LPe12000-SP", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002460 break;
2461 case PCI_DEVICE_ID_SAT_S:
James Smarta747c9c2009-11-18 15:41:10 -05002462 m = (typeof(m)){"LPe12000-S", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002463 break;
James Smart84774a42008-08-24 21:50:06 -04002464 case PCI_DEVICE_ID_HORNET:
James Smart12222f42014-05-21 08:05:19 -04002465 m = (typeof(m)){"LP21000", "PCIe",
2466 "Obsolete, Unsupported FCoE Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002467 GE = 1;
2468 break;
2469 case PCI_DEVICE_ID_PROTEUS_VF:
James Smarta747c9c2009-11-18 15:41:10 -05002470 m = (typeof(m)){"LPev12000", "PCIe IOV",
James Smart12222f42014-05-21 08:05:19 -04002471 "Obsolete, Unsupported Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002472 break;
2473 case PCI_DEVICE_ID_PROTEUS_PF:
James Smarta747c9c2009-11-18 15:41:10 -05002474 m = (typeof(m)){"LPev12000", "PCIe IOV",
James Smart12222f42014-05-21 08:05:19 -04002475 "Obsolete, Unsupported Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002476 break;
2477 case PCI_DEVICE_ID_PROTEUS_S:
James Smarta747c9c2009-11-18 15:41:10 -05002478 m = (typeof(m)){"LPemv12002-S", "PCIe IOV",
James Smart12222f42014-05-21 08:05:19 -04002479 "Obsolete, Unsupported Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002480 break;
James Smartda0436e2009-05-22 14:51:39 -04002481 case PCI_DEVICE_ID_TIGERSHARK:
2482 oneConnect = 1;
James Smarta747c9c2009-11-18 15:41:10 -05002483 m = (typeof(m)){"OCe10100", "PCIe", "FCoE"};
James Smartda0436e2009-05-22 14:51:39 -04002484 break;
James Smarta747c9c2009-11-18 15:41:10 -05002485 case PCI_DEVICE_ID_TOMCAT:
James Smart6669f9b2009-10-02 15:16:45 -04002486 oneConnect = 1;
James Smarta747c9c2009-11-18 15:41:10 -05002487 m = (typeof(m)){"OCe11100", "PCIe", "FCoE"};
2488 break;
2489 case PCI_DEVICE_ID_FALCON:
2490 m = (typeof(m)){"LPSe12002-ML1-E", "PCIe",
2491 "EmulexSecure Fibre"};
James Smart6669f9b2009-10-02 15:16:45 -04002492 break;
James Smart98fc5dd2010-06-07 15:24:29 -04002493 case PCI_DEVICE_ID_BALIUS:
2494 m = (typeof(m)){"LPVe12002", "PCIe Shared I/O",
James Smart12222f42014-05-21 08:05:19 -04002495 "Obsolete, Unsupported Fibre Channel Adapter"};
James Smart98fc5dd2010-06-07 15:24:29 -04002496 break;
James Smart085c6472010-11-20 23:11:37 -05002497 case PCI_DEVICE_ID_LANCER_FC:
James Smartc0c11512011-05-24 11:41:34 -04002498 m = (typeof(m)){"LPe16000", "PCIe", "Fibre Channel Adapter"};
James Smart085c6472010-11-20 23:11:37 -05002499 break;
James Smart12222f42014-05-21 08:05:19 -04002500 case PCI_DEVICE_ID_LANCER_FC_VF:
2501 m = (typeof(m)){"LPe16000", "PCIe",
2502 "Obsolete, Unsupported Fibre Channel Adapter"};
2503 break;
James Smart085c6472010-11-20 23:11:37 -05002504 case PCI_DEVICE_ID_LANCER_FCOE:
2505 oneConnect = 1;
James Smart079b5c92011-08-21 21:48:49 -04002506 m = (typeof(m)){"OCe15100", "PCIe", "FCoE"};
James Smart085c6472010-11-20 23:11:37 -05002507 break;
James Smart12222f42014-05-21 08:05:19 -04002508 case PCI_DEVICE_ID_LANCER_FCOE_VF:
2509 oneConnect = 1;
2510 m = (typeof(m)){"OCe15100", "PCIe",
2511 "Obsolete, Unsupported FCoE"};
2512 break;
James Smartd38dd522015-08-31 16:48:17 -04002513 case PCI_DEVICE_ID_LANCER_G6_FC:
2514 m = (typeof(m)){"LPe32000", "PCIe", "Fibre Channel Adapter"};
2515 break;
James Smartc238b9b2018-02-22 08:18:44 -08002516 case PCI_DEVICE_ID_LANCER_G7_FC:
2517 m = (typeof(m)){"LPe36000", "PCIe", "Fibre Channel Adapter"};
2518 break;
James Smartf8cafd32012-08-03 12:42:51 -04002519 case PCI_DEVICE_ID_SKYHAWK:
2520 case PCI_DEVICE_ID_SKYHAWK_VF:
2521 oneConnect = 1;
2522 m = (typeof(m)){"OCe14000", "PCIe", "FCoE"};
2523 break;
James.Smart@Emulex.Com5cc36b32005-11-28 11:42:19 -05002524 default:
James Smarta747c9c2009-11-18 15:41:10 -05002525 m = (typeof(m)){"Unknown", "", ""};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002526 break;
dea31012005-04-17 16:05:31 -05002527 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002528
2529 if (mdp && mdp[0] == '\0')
2530 snprintf(mdp, 79,"%s", m.name);
James Smartc0c11512011-05-24 11:41:34 -04002531 /*
2532 * oneConnect hba requires special processing, they are all initiators
James Smartda0436e2009-05-22 14:51:39 -04002533 * and we put the port number on the end
2534 */
2535 if (descp && descp[0] == '\0') {
2536 if (oneConnect)
2537 snprintf(descp, 255,
James Smart4169d862012-09-29 11:32:09 -04002538 "Emulex OneConnect %s, %s Initiator %s",
James Smarta747c9c2009-11-18 15:41:10 -05002539 m.name, m.function,
James Smartda0436e2009-05-22 14:51:39 -04002540 phba->Port);
James Smart4169d862012-09-29 11:32:09 -04002541 else if (max_speed == 0)
2542 snprintf(descp, 255,
Sebastian Herbszt290237d2015-08-31 16:48:08 -04002543 "Emulex %s %s %s",
James Smart4169d862012-09-29 11:32:09 -04002544 m.name, m.bus, m.function);
James Smartda0436e2009-05-22 14:51:39 -04002545 else
2546 snprintf(descp, 255,
2547 "Emulex %s %d%s %s %s",
James Smarta747c9c2009-11-18 15:41:10 -05002548 m.name, max_speed, (GE) ? "GE" : "Gb",
2549 m.bus, m.function);
James Smartda0436e2009-05-22 14:51:39 -04002550 }
dea31012005-04-17 16:05:31 -05002551}
2552
James Smarte59058c2008-08-24 21:49:00 -04002553/**
James Smart3621a712009-04-06 18:47:14 -04002554 * lpfc_post_buffer - Post IOCB(s) with DMA buffer descriptor(s) to a IOCB ring
James Smarte59058c2008-08-24 21:49:00 -04002555 * @phba: pointer to lpfc hba data structure.
2556 * @pring: pointer to a IOCB ring.
2557 * @cnt: the number of IOCBs to be posted to the IOCB ring.
2558 *
2559 * This routine posts a given number of IOCBs with the associated DMA buffer
2560 * descriptors specified by the cnt argument to the given IOCB ring.
2561 *
2562 * Return codes
2563 * The number of IOCBs NOT able to be posted to the IOCB ring.
2564 **/
dea31012005-04-17 16:05:31 -05002565int
James Smart495a7142008-06-14 22:52:59 -04002566lpfc_post_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring, int cnt)
dea31012005-04-17 16:05:31 -05002567{
2568 IOCB_t *icmd;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002569 struct lpfc_iocbq *iocb;
dea31012005-04-17 16:05:31 -05002570 struct lpfc_dmabuf *mp1, *mp2;
2571
2572 cnt += pring->missbufcnt;
2573
2574 /* While there are buffers to post */
2575 while (cnt > 0) {
2576 /* Allocate buffer for command iocb */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002577 iocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05002578 if (iocb == NULL) {
2579 pring->missbufcnt = cnt;
2580 return cnt;
2581 }
dea31012005-04-17 16:05:31 -05002582 icmd = &iocb->iocb;
2583
2584 /* 2 buffers can be posted per command */
2585 /* Allocate buffer to post */
2586 mp1 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
2587 if (mp1)
James Smart98c9ea52007-10-27 13:37:33 -04002588 mp1->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &mp1->phys);
2589 if (!mp1 || !mp1->virt) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002590 kfree(mp1);
James Bottomley604a3e32005-10-29 10:28:33 -05002591 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002592 pring->missbufcnt = cnt;
2593 return cnt;
2594 }
2595
2596 INIT_LIST_HEAD(&mp1->list);
2597 /* Allocate buffer to post */
2598 if (cnt > 1) {
2599 mp2 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
2600 if (mp2)
2601 mp2->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
2602 &mp2->phys);
James Smart98c9ea52007-10-27 13:37:33 -04002603 if (!mp2 || !mp2->virt) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002604 kfree(mp2);
dea31012005-04-17 16:05:31 -05002605 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
2606 kfree(mp1);
James Bottomley604a3e32005-10-29 10:28:33 -05002607 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002608 pring->missbufcnt = cnt;
2609 return cnt;
2610 }
2611
2612 INIT_LIST_HEAD(&mp2->list);
2613 } else {
2614 mp2 = NULL;
2615 }
2616
2617 icmd->un.cont64[0].addrHigh = putPaddrHigh(mp1->phys);
2618 icmd->un.cont64[0].addrLow = putPaddrLow(mp1->phys);
2619 icmd->un.cont64[0].tus.f.bdeSize = FCELSSIZE;
2620 icmd->ulpBdeCount = 1;
2621 cnt--;
2622 if (mp2) {
2623 icmd->un.cont64[1].addrHigh = putPaddrHigh(mp2->phys);
2624 icmd->un.cont64[1].addrLow = putPaddrLow(mp2->phys);
2625 icmd->un.cont64[1].tus.f.bdeSize = FCELSSIZE;
2626 cnt--;
2627 icmd->ulpBdeCount = 2;
2628 }
2629
2630 icmd->ulpCommand = CMD_QUE_RING_BUF64_CN;
2631 icmd->ulpLe = 1;
2632
James Smart3772a992009-05-22 14:50:54 -04002633 if (lpfc_sli_issue_iocb(phba, pring->ringno, iocb, 0) ==
2634 IOCB_ERROR) {
dea31012005-04-17 16:05:31 -05002635 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
2636 kfree(mp1);
2637 cnt++;
2638 if (mp2) {
2639 lpfc_mbuf_free(phba, mp2->virt, mp2->phys);
2640 kfree(mp2);
2641 cnt++;
2642 }
James Bottomley604a3e32005-10-29 10:28:33 -05002643 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002644 pring->missbufcnt = cnt;
dea31012005-04-17 16:05:31 -05002645 return cnt;
2646 }
dea31012005-04-17 16:05:31 -05002647 lpfc_sli_ringpostbuf_put(phba, pring, mp1);
James Smart92d7f7b2007-06-17 19:56:38 -05002648 if (mp2)
dea31012005-04-17 16:05:31 -05002649 lpfc_sli_ringpostbuf_put(phba, pring, mp2);
dea31012005-04-17 16:05:31 -05002650 }
2651 pring->missbufcnt = 0;
2652 return 0;
2653}
2654
James Smarte59058c2008-08-24 21:49:00 -04002655/**
James Smart3621a712009-04-06 18:47:14 -04002656 * lpfc_post_rcv_buf - Post the initial receive IOCB buffers to ELS ring
James Smarte59058c2008-08-24 21:49:00 -04002657 * @phba: pointer to lpfc hba data structure.
2658 *
2659 * This routine posts initial receive IOCB buffers to the ELS ring. The
2660 * current number of initial IOCB buffers specified by LPFC_BUF_RING0 is
James Smart895427b2017-02-12 13:52:30 -08002661 * set to 64 IOCBs. SLI3 only.
James Smarte59058c2008-08-24 21:49:00 -04002662 *
2663 * Return codes
2664 * 0 - success (currently always success)
2665 **/
dea31012005-04-17 16:05:31 -05002666static int
James Smart2e0fef82007-06-17 19:56:36 -05002667lpfc_post_rcv_buf(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002668{
2669 struct lpfc_sli *psli = &phba->sli;
2670
2671 /* Ring 0, ELS / CT buffers */
James Smart895427b2017-02-12 13:52:30 -08002672 lpfc_post_buffer(phba, &psli->sli3_ring[LPFC_ELS_RING], LPFC_BUF_RING0);
dea31012005-04-17 16:05:31 -05002673 /* Ring 2 - FCP no buffers needed */
2674
2675 return 0;
2676}
2677
2678#define S(N,V) (((V)<<(N))|((V)>>(32-(N))))
2679
James Smarte59058c2008-08-24 21:49:00 -04002680/**
James Smart3621a712009-04-06 18:47:14 -04002681 * lpfc_sha_init - Set up initial array of hash table entries
James Smarte59058c2008-08-24 21:49:00 -04002682 * @HashResultPointer: pointer to an array as hash table.
2683 *
2684 * This routine sets up the initial values to the array of hash table entries
2685 * for the LC HBAs.
2686 **/
dea31012005-04-17 16:05:31 -05002687static void
2688lpfc_sha_init(uint32_t * HashResultPointer)
2689{
2690 HashResultPointer[0] = 0x67452301;
2691 HashResultPointer[1] = 0xEFCDAB89;
2692 HashResultPointer[2] = 0x98BADCFE;
2693 HashResultPointer[3] = 0x10325476;
2694 HashResultPointer[4] = 0xC3D2E1F0;
2695}
2696
James Smarte59058c2008-08-24 21:49:00 -04002697/**
James Smart3621a712009-04-06 18:47:14 -04002698 * lpfc_sha_iterate - Iterate initial hash table with the working hash table
James Smarte59058c2008-08-24 21:49:00 -04002699 * @HashResultPointer: pointer to an initial/result hash table.
2700 * @HashWorkingPointer: pointer to an working hash table.
2701 *
2702 * This routine iterates an initial hash table pointed by @HashResultPointer
2703 * with the values from the working hash table pointeed by @HashWorkingPointer.
2704 * The results are putting back to the initial hash table, returned through
2705 * the @HashResultPointer as the result hash table.
2706 **/
dea31012005-04-17 16:05:31 -05002707static void
2708lpfc_sha_iterate(uint32_t * HashResultPointer, uint32_t * HashWorkingPointer)
2709{
2710 int t;
2711 uint32_t TEMP;
2712 uint32_t A, B, C, D, E;
2713 t = 16;
2714 do {
2715 HashWorkingPointer[t] =
2716 S(1,
2717 HashWorkingPointer[t - 3] ^ HashWorkingPointer[t -
2718 8] ^
2719 HashWorkingPointer[t - 14] ^ HashWorkingPointer[t - 16]);
2720 } while (++t <= 79);
2721 t = 0;
2722 A = HashResultPointer[0];
2723 B = HashResultPointer[1];
2724 C = HashResultPointer[2];
2725 D = HashResultPointer[3];
2726 E = HashResultPointer[4];
2727
2728 do {
2729 if (t < 20) {
2730 TEMP = ((B & C) | ((~B) & D)) + 0x5A827999;
2731 } else if (t < 40) {
2732 TEMP = (B ^ C ^ D) + 0x6ED9EBA1;
2733 } else if (t < 60) {
2734 TEMP = ((B & C) | (B & D) | (C & D)) + 0x8F1BBCDC;
2735 } else {
2736 TEMP = (B ^ C ^ D) + 0xCA62C1D6;
2737 }
2738 TEMP += S(5, A) + E + HashWorkingPointer[t];
2739 E = D;
2740 D = C;
2741 C = S(30, B);
2742 B = A;
2743 A = TEMP;
2744 } while (++t <= 79);
2745
2746 HashResultPointer[0] += A;
2747 HashResultPointer[1] += B;
2748 HashResultPointer[2] += C;
2749 HashResultPointer[3] += D;
2750 HashResultPointer[4] += E;
2751
2752}
2753
James Smarte59058c2008-08-24 21:49:00 -04002754/**
James Smart3621a712009-04-06 18:47:14 -04002755 * lpfc_challenge_key - Create challenge key based on WWPN of the HBA
James Smarte59058c2008-08-24 21:49:00 -04002756 * @RandomChallenge: pointer to the entry of host challenge random number array.
2757 * @HashWorking: pointer to the entry of the working hash array.
2758 *
2759 * This routine calculates the working hash array referred by @HashWorking
2760 * from the challenge random numbers associated with the host, referred by
2761 * @RandomChallenge. The result is put into the entry of the working hash
2762 * array and returned by reference through @HashWorking.
2763 **/
dea31012005-04-17 16:05:31 -05002764static void
2765lpfc_challenge_key(uint32_t * RandomChallenge, uint32_t * HashWorking)
2766{
2767 *HashWorking = (*RandomChallenge ^ *HashWorking);
2768}
2769
James Smarte59058c2008-08-24 21:49:00 -04002770/**
James Smart3621a712009-04-06 18:47:14 -04002771 * lpfc_hba_init - Perform special handling for LC HBA initialization
James Smarte59058c2008-08-24 21:49:00 -04002772 * @phba: pointer to lpfc hba data structure.
2773 * @hbainit: pointer to an array of unsigned 32-bit integers.
2774 *
2775 * This routine performs the special handling for LC HBA initialization.
2776 **/
dea31012005-04-17 16:05:31 -05002777void
2778lpfc_hba_init(struct lpfc_hba *phba, uint32_t *hbainit)
2779{
2780 int t;
2781 uint32_t *HashWorking;
James Smart2e0fef82007-06-17 19:56:36 -05002782 uint32_t *pwwnn = (uint32_t *) phba->wwnn;
dea31012005-04-17 16:05:31 -05002783
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +02002784 HashWorking = kcalloc(80, sizeof(uint32_t), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05002785 if (!HashWorking)
2786 return;
2787
dea31012005-04-17 16:05:31 -05002788 HashWorking[0] = HashWorking[78] = *pwwnn++;
2789 HashWorking[1] = HashWorking[79] = *pwwnn;
2790
2791 for (t = 0; t < 7; t++)
2792 lpfc_challenge_key(phba->RandomData + t, HashWorking + t);
2793
2794 lpfc_sha_init(hbainit);
2795 lpfc_sha_iterate(hbainit, HashWorking);
2796 kfree(HashWorking);
2797}
2798
James Smarte59058c2008-08-24 21:49:00 -04002799/**
James Smart3621a712009-04-06 18:47:14 -04002800 * lpfc_cleanup - Performs vport cleanups before deleting a vport
James Smarte59058c2008-08-24 21:49:00 -04002801 * @vport: pointer to a virtual N_Port data structure.
2802 *
2803 * This routine performs the necessary cleanups before deleting the @vport.
2804 * It invokes the discovery state machine to perform necessary state
2805 * transitions and to release the ndlps associated with the @vport. Note,
2806 * the physical port is treated as @vport 0.
2807 **/
James Smart87af33f2007-10-27 13:37:43 -04002808void
James Smart2e0fef82007-06-17 19:56:36 -05002809lpfc_cleanup(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002810{
James Smart87af33f2007-10-27 13:37:43 -04002811 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002812 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smarta8adb832007-10-27 13:37:53 -04002813 int i = 0;
dea31012005-04-17 16:05:31 -05002814
James Smart87af33f2007-10-27 13:37:43 -04002815 if (phba->link_state > LPFC_LINK_DOWN)
2816 lpfc_port_link_failure(vport);
2817
2818 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05002819 if (!NLP_CHK_NODE_ACT(ndlp)) {
2820 ndlp = lpfc_enable_node(vport, ndlp,
2821 NLP_STE_UNUSED_NODE);
2822 if (!ndlp)
2823 continue;
2824 spin_lock_irq(&phba->ndlp_lock);
2825 NLP_SET_FREE_REQ(ndlp);
2826 spin_unlock_irq(&phba->ndlp_lock);
2827 /* Trigger the release of the ndlp memory */
2828 lpfc_nlp_put(ndlp);
2829 continue;
2830 }
2831 spin_lock_irq(&phba->ndlp_lock);
2832 if (NLP_CHK_FREE_REQ(ndlp)) {
2833 /* The ndlp should not be in memory free mode already */
2834 spin_unlock_irq(&phba->ndlp_lock);
2835 continue;
2836 } else
2837 /* Indicate request for freeing ndlp memory */
2838 NLP_SET_FREE_REQ(ndlp);
2839 spin_unlock_irq(&phba->ndlp_lock);
2840
James Smart58da1ff2008-04-07 10:15:56 -04002841 if (vport->port_type != LPFC_PHYSICAL_PORT &&
2842 ndlp->nlp_DID == Fabric_DID) {
2843 /* Just free up ndlp with Fabric_DID for vports */
2844 lpfc_nlp_put(ndlp);
2845 continue;
2846 }
2847
James Smarteff4a012012-01-18 16:25:25 -05002848 /* take care of nodes in unused state before the state
2849 * machine taking action.
2850 */
2851 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE) {
2852 lpfc_nlp_put(ndlp);
2853 continue;
2854 }
2855
James Smart87af33f2007-10-27 13:37:43 -04002856 if (ndlp->nlp_type & NLP_FABRIC)
2857 lpfc_disc_state_machine(vport, ndlp, NULL,
2858 NLP_EVT_DEVICE_RECOVERY);
James Smarte47c9092008-02-08 18:49:26 -05002859
James Smart87af33f2007-10-27 13:37:43 -04002860 lpfc_disc_state_machine(vport, ndlp, NULL,
2861 NLP_EVT_DEVICE_RM);
2862 }
2863
James Smarta8adb832007-10-27 13:37:53 -04002864 /* At this point, ALL ndlp's should be gone
2865 * because of the previous NLP_EVT_DEVICE_RM.
2866 * Lets wait for this to happen, if needed.
2867 */
James Smart87af33f2007-10-27 13:37:43 -04002868 while (!list_empty(&vport->fc_nodes)) {
James Smarta8adb832007-10-27 13:37:53 -04002869 if (i++ > 3000) {
James Smart87af33f2007-10-27 13:37:43 -04002870 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
James Smarta8adb832007-10-27 13:37:53 -04002871 "0233 Nodelist not empty\n");
James Smarte47c9092008-02-08 18:49:26 -05002872 list_for_each_entry_safe(ndlp, next_ndlp,
2873 &vport->fc_nodes, nlp_listp) {
2874 lpfc_printf_vlog(ndlp->vport, KERN_ERR,
2875 LOG_NODE,
James Smartd7c255b2008-08-24 21:50:00 -04002876 "0282 did:x%x ndlp:x%p "
James Smarte47c9092008-02-08 18:49:26 -05002877 "usgmap:x%x refcnt:%d\n",
2878 ndlp->nlp_DID, (void *)ndlp,
2879 ndlp->nlp_usg_map,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01002880 kref_read(&ndlp->kref));
James Smarte47c9092008-02-08 18:49:26 -05002881 }
James Smarta8adb832007-10-27 13:37:53 -04002882 break;
James Smart87af33f2007-10-27 13:37:43 -04002883 }
James Smarta8adb832007-10-27 13:37:53 -04002884
2885 /* Wait for any activity on ndlps to settle */
2886 msleep(10);
James Smart87af33f2007-10-27 13:37:43 -04002887 }
James Smart1151e3e2011-02-16 12:39:35 -05002888 lpfc_cleanup_vports_rrqs(vport, NULL);
dea31012005-04-17 16:05:31 -05002889}
2890
James Smarte59058c2008-08-24 21:49:00 -04002891/**
James Smart3621a712009-04-06 18:47:14 -04002892 * lpfc_stop_vport_timers - Stop all the timers associated with a vport
James Smarte59058c2008-08-24 21:49:00 -04002893 * @vport: pointer to a virtual N_Port data structure.
2894 *
2895 * This routine stops all the timers associated with a @vport. This function
2896 * is invoked before disabling or deleting a @vport. Note that the physical
2897 * port is treated as @vport 0.
2898 **/
James Smart92d7f7b2007-06-17 19:56:38 -05002899void
2900lpfc_stop_vport_timers(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002901{
James Smart92d7f7b2007-06-17 19:56:38 -05002902 del_timer_sync(&vport->els_tmofunc);
James Smart92494142011-02-16 12:39:44 -05002903 del_timer_sync(&vport->delayed_disc_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05002904 lpfc_can_disctmo(vport);
2905 return;
dea31012005-04-17 16:05:31 -05002906}
2907
James Smarte59058c2008-08-24 21:49:00 -04002908/**
James Smartecfd03c2010-02-12 14:41:27 -05002909 * __lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2910 * @phba: pointer to lpfc hba data structure.
2911 *
2912 * This routine stops the SLI4 FCF rediscover wait timer if it's on. The
2913 * caller of this routine should already hold the host lock.
2914 **/
2915void
2916__lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2917{
James Smart5ac6b302010-10-22 11:05:36 -04002918 /* Clear pending FCF rediscovery wait flag */
2919 phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
2920
James Smartecfd03c2010-02-12 14:41:27 -05002921 /* Now, try to stop the timer */
2922 del_timer(&phba->fcf.redisc_wait);
2923}
2924
2925/**
2926 * lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2927 * @phba: pointer to lpfc hba data structure.
2928 *
2929 * This routine stops the SLI4 FCF rediscover wait timer if it's on. It
2930 * checks whether the FCF rediscovery wait timer is pending with the host
2931 * lock held before proceeding with disabling the timer and clearing the
2932 * wait timer pendig flag.
2933 **/
2934void
2935lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2936{
2937 spin_lock_irq(&phba->hbalock);
2938 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
2939 /* FCF rediscovery timer already fired or stopped */
2940 spin_unlock_irq(&phba->hbalock);
2941 return;
2942 }
2943 __lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
James Smart5ac6b302010-10-22 11:05:36 -04002944 /* Clear failover in progress flags */
2945 phba->fcf.fcf_flag &= ~(FCF_DEAD_DISC | FCF_ACVL_DISC);
James Smartecfd03c2010-02-12 14:41:27 -05002946 spin_unlock_irq(&phba->hbalock);
2947}
2948
2949/**
James Smart3772a992009-05-22 14:50:54 -04002950 * lpfc_stop_hba_timers - Stop all the timers associated with an HBA
James Smarte59058c2008-08-24 21:49:00 -04002951 * @phba: pointer to lpfc hba data structure.
2952 *
2953 * This routine stops all the timers associated with a HBA. This function is
2954 * invoked before either putting a HBA offline or unloading the driver.
2955 **/
James Smart3772a992009-05-22 14:50:54 -04002956void
2957lpfc_stop_hba_timers(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002958{
James Smartcdb42be2019-01-28 11:14:21 -08002959 if (phba->pport)
2960 lpfc_stop_vport_timers(phba->pport);
James Smart32517fc2019-01-28 11:14:33 -08002961 cancel_delayed_work_sync(&phba->eq_delay_work);
James Smart2e0fef82007-06-17 19:56:36 -05002962 del_timer_sync(&phba->sli.mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05002963 del_timer_sync(&phba->fabric_block_timer);
James Smart93996272008-08-24 21:50:30 -04002964 del_timer_sync(&phba->eratt_poll);
James Smart3772a992009-05-22 14:50:54 -04002965 del_timer_sync(&phba->hb_tmofunc);
James Smart1151e3e2011-02-16 12:39:35 -05002966 if (phba->sli_rev == LPFC_SLI_REV4) {
2967 del_timer_sync(&phba->rrq_tmr);
2968 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
2969 }
James Smart3772a992009-05-22 14:50:54 -04002970 phba->hb_outstanding = 0;
2971
2972 switch (phba->pci_dev_grp) {
2973 case LPFC_PCI_DEV_LP:
2974 /* Stop any LightPulse device specific driver timers */
2975 del_timer_sync(&phba->fcp_poll_timer);
2976 break;
2977 case LPFC_PCI_DEV_OC:
Paul Walmsleycc0e5f12019-07-11 20:52:33 -07002978 /* Stop any OneConnect device specific driver timers */
James Smartecfd03c2010-02-12 14:41:27 -05002979 lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
James Smart3772a992009-05-22 14:50:54 -04002980 break;
2981 default:
2982 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2983 "0297 Invalid device group (x%x)\n",
2984 phba->pci_dev_grp);
2985 break;
2986 }
James Smart2e0fef82007-06-17 19:56:36 -05002987 return;
dea31012005-04-17 16:05:31 -05002988}
2989
James Smarte59058c2008-08-24 21:49:00 -04002990/**
James Smart3621a712009-04-06 18:47:14 -04002991 * lpfc_block_mgmt_io - Mark a HBA's management interface as blocked
James Smarte59058c2008-08-24 21:49:00 -04002992 * @phba: pointer to lpfc hba data structure.
2993 *
2994 * This routine marks a HBA's management interface as blocked. Once the HBA's
2995 * management interface is marked as blocked, all the user space access to
2996 * the HBA, whether they are from sysfs interface or libdfc interface will
2997 * all be blocked. The HBA is set to block the management interface when the
2998 * driver prepares the HBA interface for online or offline.
2999 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01003000static void
James Smart618a5232012-06-12 13:54:36 -04003001lpfc_block_mgmt_io(struct lpfc_hba *phba, int mbx_action)
Adrian Bunka6ababd2007-11-05 18:07:33 +01003002{
3003 unsigned long iflag;
James Smart6e7288d2010-06-07 15:23:35 -04003004 uint8_t actcmd = MBX_HEARTBEAT;
3005 unsigned long timeout;
3006
Adrian Bunka6ababd2007-11-05 18:07:33 +01003007 spin_lock_irqsave(&phba->hbalock, iflag);
3008 phba->sli.sli_flag |= LPFC_BLOCK_MGMT_IO;
James Smart618a5232012-06-12 13:54:36 -04003009 spin_unlock_irqrestore(&phba->hbalock, iflag);
3010 if (mbx_action == LPFC_MBX_NO_WAIT)
3011 return;
3012 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
3013 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta183a152011-10-10 21:32:43 -04003014 if (phba->sli.mbox_active) {
James Smart6e7288d2010-06-07 15:23:35 -04003015 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
James Smarta183a152011-10-10 21:32:43 -04003016 /* Determine how long we might wait for the active mailbox
3017 * command to be gracefully completed by firmware.
3018 */
3019 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
3020 phba->sli.mbox_active) * 1000) + jiffies;
3021 }
Adrian Bunka6ababd2007-11-05 18:07:33 +01003022 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta183a152011-10-10 21:32:43 -04003023
James Smart6e7288d2010-06-07 15:23:35 -04003024 /* Wait for the outstnading mailbox command to complete */
3025 while (phba->sli.mbox_active) {
3026 /* Check active mailbox complete status every 2ms */
3027 msleep(2);
3028 if (time_after(jiffies, timeout)) {
3029 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3030 "2813 Mgmt IO is Blocked %x "
3031 "- mbox cmd %x still active\n",
3032 phba->sli.sli_flag, actcmd);
3033 break;
3034 }
3035 }
Adrian Bunka6ababd2007-11-05 18:07:33 +01003036}
3037
James Smarte59058c2008-08-24 21:49:00 -04003038/**
James Smart6b5151f2012-01-18 16:24:06 -05003039 * lpfc_sli4_node_prep - Assign RPIs for active nodes.
3040 * @phba: pointer to lpfc hba data structure.
3041 *
3042 * Allocate RPIs for all active remote nodes. This is needed whenever
3043 * an SLI4 adapter is reset and the driver is not unloading. Its purpose
3044 * is to fixup the temporary rpi assignments.
3045 **/
3046void
3047lpfc_sli4_node_prep(struct lpfc_hba *phba)
3048{
3049 struct lpfc_nodelist *ndlp, *next_ndlp;
3050 struct lpfc_vport **vports;
James Smart9d3d3402017-04-21 16:05:00 -07003051 int i, rpi;
3052 unsigned long flags;
James Smart6b5151f2012-01-18 16:24:06 -05003053
3054 if (phba->sli_rev != LPFC_SLI_REV4)
3055 return;
3056
3057 vports = lpfc_create_vport_work_array(phba);
James Smart9d3d3402017-04-21 16:05:00 -07003058 if (vports == NULL)
3059 return;
James Smart6b5151f2012-01-18 16:24:06 -05003060
James Smart9d3d3402017-04-21 16:05:00 -07003061 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
3062 if (vports[i]->load_flag & FC_UNLOADING)
3063 continue;
3064
3065 list_for_each_entry_safe(ndlp, next_ndlp,
3066 &vports[i]->fc_nodes,
3067 nlp_listp) {
3068 if (!NLP_CHK_NODE_ACT(ndlp))
3069 continue;
3070 rpi = lpfc_sli4_alloc_rpi(phba);
3071 if (rpi == LPFC_RPI_ALLOC_ERROR) {
3072 spin_lock_irqsave(&phba->ndlp_lock, flags);
3073 NLP_CLR_NODE_ACT(ndlp);
3074 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
3075 continue;
James Smart6b5151f2012-01-18 16:24:06 -05003076 }
James Smart9d3d3402017-04-21 16:05:00 -07003077 ndlp->nlp_rpi = rpi;
3078 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
3079 "0009 rpi:%x DID:%x "
3080 "flg:%x map:%x %p\n", ndlp->nlp_rpi,
3081 ndlp->nlp_DID, ndlp->nlp_flag,
3082 ndlp->nlp_usg_map, ndlp);
James Smart6b5151f2012-01-18 16:24:06 -05003083 }
3084 }
3085 lpfc_destroy_vport_work_array(phba, vports);
3086}
3087
3088/**
James Smartc4908502019-01-28 11:14:28 -08003089 * lpfc_create_expedite_pool - create expedite pool
3090 * @phba: pointer to lpfc hba data structure.
3091 *
3092 * This routine moves a batch of XRIs from lpfc_io_buf_list_put of HWQ 0
3093 * to expedite pool. Mark them as expedite.
3094 **/
Bart Van Assche3999df72019-03-28 11:06:16 -07003095static void lpfc_create_expedite_pool(struct lpfc_hba *phba)
James Smartc4908502019-01-28 11:14:28 -08003096{
3097 struct lpfc_sli4_hdw_queue *qp;
3098 struct lpfc_io_buf *lpfc_ncmd;
3099 struct lpfc_io_buf *lpfc_ncmd_next;
3100 struct lpfc_epd_pool *epd_pool;
3101 unsigned long iflag;
3102
3103 epd_pool = &phba->epd_pool;
3104 qp = &phba->sli4_hba.hdwq[0];
3105
3106 spin_lock_init(&epd_pool->lock);
3107 spin_lock_irqsave(&qp->io_buf_list_put_lock, iflag);
3108 spin_lock(&epd_pool->lock);
3109 INIT_LIST_HEAD(&epd_pool->list);
3110 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
3111 &qp->lpfc_io_buf_list_put, list) {
3112 list_move_tail(&lpfc_ncmd->list, &epd_pool->list);
3113 lpfc_ncmd->expedite = true;
3114 qp->put_io_bufs--;
3115 epd_pool->count++;
3116 if (epd_pool->count >= XRI_BATCH)
3117 break;
3118 }
3119 spin_unlock(&epd_pool->lock);
3120 spin_unlock_irqrestore(&qp->io_buf_list_put_lock, iflag);
3121}
3122
3123/**
3124 * lpfc_destroy_expedite_pool - destroy expedite pool
3125 * @phba: pointer to lpfc hba data structure.
3126 *
3127 * This routine returns XRIs from expedite pool to lpfc_io_buf_list_put
3128 * of HWQ 0. Clear the mark.
3129 **/
Bart Van Assche3999df72019-03-28 11:06:16 -07003130static void lpfc_destroy_expedite_pool(struct lpfc_hba *phba)
James Smartc4908502019-01-28 11:14:28 -08003131{
3132 struct lpfc_sli4_hdw_queue *qp;
3133 struct lpfc_io_buf *lpfc_ncmd;
3134 struct lpfc_io_buf *lpfc_ncmd_next;
3135 struct lpfc_epd_pool *epd_pool;
3136 unsigned long iflag;
3137
3138 epd_pool = &phba->epd_pool;
3139 qp = &phba->sli4_hba.hdwq[0];
3140
3141 spin_lock_irqsave(&qp->io_buf_list_put_lock, iflag);
3142 spin_lock(&epd_pool->lock);
3143 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
3144 &epd_pool->list, list) {
3145 list_move_tail(&lpfc_ncmd->list,
3146 &qp->lpfc_io_buf_list_put);
3147 lpfc_ncmd->flags = false;
3148 qp->put_io_bufs++;
3149 epd_pool->count--;
3150 }
3151 spin_unlock(&epd_pool->lock);
3152 spin_unlock_irqrestore(&qp->io_buf_list_put_lock, iflag);
3153}
3154
3155/**
3156 * lpfc_create_multixri_pools - create multi-XRI pools
3157 * @phba: pointer to lpfc hba data structure.
3158 *
3159 * This routine initialize public, private per HWQ. Then, move XRIs from
3160 * lpfc_io_buf_list_put to public pool. High and low watermark are also
3161 * Initialized.
3162 **/
3163void lpfc_create_multixri_pools(struct lpfc_hba *phba)
3164{
3165 u32 i, j;
3166 u32 hwq_count;
3167 u32 count_per_hwq;
3168 struct lpfc_io_buf *lpfc_ncmd;
3169 struct lpfc_io_buf *lpfc_ncmd_next;
3170 unsigned long iflag;
3171 struct lpfc_sli4_hdw_queue *qp;
3172 struct lpfc_multixri_pool *multixri_pool;
3173 struct lpfc_pbl_pool *pbl_pool;
3174 struct lpfc_pvt_pool *pvt_pool;
3175
3176 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3177 "1234 num_hdw_queue=%d num_present_cpu=%d common_xri_cnt=%d\n",
3178 phba->cfg_hdw_queue, phba->sli4_hba.num_present_cpu,
3179 phba->sli4_hba.io_xri_cnt);
3180
3181 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
3182 lpfc_create_expedite_pool(phba);
3183
3184 hwq_count = phba->cfg_hdw_queue;
3185 count_per_hwq = phba->sli4_hba.io_xri_cnt / hwq_count;
3186
3187 for (i = 0; i < hwq_count; i++) {
3188 multixri_pool = kzalloc(sizeof(*multixri_pool), GFP_KERNEL);
3189
3190 if (!multixri_pool) {
3191 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3192 "1238 Failed to allocate memory for "
3193 "multixri_pool\n");
3194
3195 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
3196 lpfc_destroy_expedite_pool(phba);
3197
3198 j = 0;
3199 while (j < i) {
3200 qp = &phba->sli4_hba.hdwq[j];
3201 kfree(qp->p_multixri_pool);
3202 j++;
3203 }
3204 phba->cfg_xri_rebalancing = 0;
3205 return;
3206 }
3207
3208 qp = &phba->sli4_hba.hdwq[i];
3209 qp->p_multixri_pool = multixri_pool;
3210
3211 multixri_pool->xri_limit = count_per_hwq;
3212 multixri_pool->rrb_next_hwqid = i;
3213
3214 /* Deal with public free xri pool */
3215 pbl_pool = &multixri_pool->pbl_pool;
3216 spin_lock_init(&pbl_pool->lock);
3217 spin_lock_irqsave(&qp->io_buf_list_put_lock, iflag);
3218 spin_lock(&pbl_pool->lock);
3219 INIT_LIST_HEAD(&pbl_pool->list);
3220 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
3221 &qp->lpfc_io_buf_list_put, list) {
3222 list_move_tail(&lpfc_ncmd->list, &pbl_pool->list);
3223 qp->put_io_bufs--;
3224 pbl_pool->count++;
3225 }
3226 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3227 "1235 Moved %d buffers from PUT list over to pbl_pool[%d]\n",
3228 pbl_pool->count, i);
3229 spin_unlock(&pbl_pool->lock);
3230 spin_unlock_irqrestore(&qp->io_buf_list_put_lock, iflag);
3231
3232 /* Deal with private free xri pool */
3233 pvt_pool = &multixri_pool->pvt_pool;
3234 pvt_pool->high_watermark = multixri_pool->xri_limit / 2;
3235 pvt_pool->low_watermark = XRI_BATCH;
3236 spin_lock_init(&pvt_pool->lock);
3237 spin_lock_irqsave(&pvt_pool->lock, iflag);
3238 INIT_LIST_HEAD(&pvt_pool->list);
3239 pvt_pool->count = 0;
3240 spin_unlock_irqrestore(&pvt_pool->lock, iflag);
3241 }
3242}
3243
3244/**
3245 * lpfc_destroy_multixri_pools - destroy multi-XRI pools
3246 * @phba: pointer to lpfc hba data structure.
3247 *
3248 * This routine returns XRIs from public/private to lpfc_io_buf_list_put.
3249 **/
Bart Van Assche3999df72019-03-28 11:06:16 -07003250static void lpfc_destroy_multixri_pools(struct lpfc_hba *phba)
James Smartc4908502019-01-28 11:14:28 -08003251{
3252 u32 i;
3253 u32 hwq_count;
3254 struct lpfc_io_buf *lpfc_ncmd;
3255 struct lpfc_io_buf *lpfc_ncmd_next;
3256 unsigned long iflag;
3257 struct lpfc_sli4_hdw_queue *qp;
3258 struct lpfc_multixri_pool *multixri_pool;
3259 struct lpfc_pbl_pool *pbl_pool;
3260 struct lpfc_pvt_pool *pvt_pool;
3261
3262 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
3263 lpfc_destroy_expedite_pool(phba);
3264
James Smartc66a9192019-03-12 16:30:19 -07003265 if (!(phba->pport->load_flag & FC_UNLOADING)) {
3266 lpfc_sli_flush_fcp_rings(phba);
3267
3268 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
3269 lpfc_sli_flush_nvme_rings(phba);
3270 }
3271
James Smartc4908502019-01-28 11:14:28 -08003272 hwq_count = phba->cfg_hdw_queue;
3273
3274 for (i = 0; i < hwq_count; i++) {
3275 qp = &phba->sli4_hba.hdwq[i];
3276 multixri_pool = qp->p_multixri_pool;
3277 if (!multixri_pool)
3278 continue;
3279
3280 qp->p_multixri_pool = NULL;
3281
3282 spin_lock_irqsave(&qp->io_buf_list_put_lock, iflag);
3283
3284 /* Deal with public free xri pool */
3285 pbl_pool = &multixri_pool->pbl_pool;
3286 spin_lock(&pbl_pool->lock);
3287
3288 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3289 "1236 Moving %d buffers from pbl_pool[%d] TO PUT list\n",
3290 pbl_pool->count, i);
3291
3292 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
3293 &pbl_pool->list, list) {
3294 list_move_tail(&lpfc_ncmd->list,
3295 &qp->lpfc_io_buf_list_put);
3296 qp->put_io_bufs++;
3297 pbl_pool->count--;
3298 }
3299
3300 INIT_LIST_HEAD(&pbl_pool->list);
3301 pbl_pool->count = 0;
3302
3303 spin_unlock(&pbl_pool->lock);
3304
3305 /* Deal with private free xri pool */
3306 pvt_pool = &multixri_pool->pvt_pool;
3307 spin_lock(&pvt_pool->lock);
3308
3309 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3310 "1237 Moving %d buffers from pvt_pool[%d] TO PUT list\n",
3311 pvt_pool->count, i);
3312
3313 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
3314 &pvt_pool->list, list) {
3315 list_move_tail(&lpfc_ncmd->list,
3316 &qp->lpfc_io_buf_list_put);
3317 qp->put_io_bufs++;
3318 pvt_pool->count--;
3319 }
3320
3321 INIT_LIST_HEAD(&pvt_pool->list);
3322 pvt_pool->count = 0;
3323
3324 spin_unlock(&pvt_pool->lock);
3325 spin_unlock_irqrestore(&qp->io_buf_list_put_lock, iflag);
3326
3327 kfree(multixri_pool);
3328 }
3329}
3330
3331/**
James Smart3621a712009-04-06 18:47:14 -04003332 * lpfc_online - Initialize and bring a HBA online
James Smarte59058c2008-08-24 21:49:00 -04003333 * @phba: pointer to lpfc hba data structure.
3334 *
3335 * This routine initializes the HBA and brings a HBA online. During this
3336 * process, the management interface is blocked to prevent user space access
3337 * to the HBA interfering with the driver initialization.
3338 *
3339 * Return codes
3340 * 0 - successful
3341 * 1 - failed
3342 **/
dea31012005-04-17 16:05:31 -05003343int
James Smart2e0fef82007-06-17 19:56:36 -05003344lpfc_online(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05003345{
Julia Lawall372bd282008-12-16 16:15:08 +01003346 struct lpfc_vport *vport;
James Smart549e55c2007-08-02 11:09:51 -04003347 struct lpfc_vport **vports;
Dick Kennedya145fda2017-08-23 16:55:44 -07003348 int i, error = 0;
James Smart16a3a202013-04-17 20:14:38 -04003349 bool vpis_cleared = false;
James Smart2e0fef82007-06-17 19:56:36 -05003350
dea31012005-04-17 16:05:31 -05003351 if (!phba)
3352 return 0;
Julia Lawall372bd282008-12-16 16:15:08 +01003353 vport = phba->pport;
dea31012005-04-17 16:05:31 -05003354
James Smart2e0fef82007-06-17 19:56:36 -05003355 if (!(vport->fc_flag & FC_OFFLINE_MODE))
dea31012005-04-17 16:05:31 -05003356 return 0;
3357
James Smarted957682007-06-17 19:56:37 -05003358 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003359 "0458 Bring Adapter online\n");
dea31012005-04-17 16:05:31 -05003360
James Smart618a5232012-06-12 13:54:36 -04003361 lpfc_block_mgmt_io(phba, LPFC_MBX_WAIT);
dea31012005-04-17 16:05:31 -05003362
James Smartda0436e2009-05-22 14:51:39 -04003363 if (phba->sli_rev == LPFC_SLI_REV4) {
3364 if (lpfc_sli4_hba_setup(phba)) { /* Initialize SLI4 HBA */
3365 lpfc_unblock_mgmt_io(phba);
3366 return 1;
3367 }
James Smart16a3a202013-04-17 20:14:38 -04003368 spin_lock_irq(&phba->hbalock);
3369 if (!phba->sli4_hba.max_cfg_param.vpi_used)
3370 vpis_cleared = true;
3371 spin_unlock_irq(&phba->hbalock);
Dick Kennedya145fda2017-08-23 16:55:44 -07003372
3373 /* Reestablish the local initiator port.
3374 * The offline process destroyed the previous lport.
3375 */
3376 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME &&
3377 !phba->nvmet_support) {
3378 error = lpfc_nvme_create_localport(phba->pport);
3379 if (error)
3380 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3381 "6132 NVME restore reg failed "
3382 "on nvmei error x%x\n", error);
3383 }
James Smartda0436e2009-05-22 14:51:39 -04003384 } else {
James Smart895427b2017-02-12 13:52:30 -08003385 lpfc_sli_queue_init(phba);
James Smartda0436e2009-05-22 14:51:39 -04003386 if (lpfc_sli_hba_setup(phba)) { /* Initialize SLI2/SLI3 HBA */
3387 lpfc_unblock_mgmt_io(phba);
3388 return 1;
3389 }
James Smart46fa3112007-04-25 09:51:45 -04003390 }
dea31012005-04-17 16:05:31 -05003391
James Smart549e55c2007-08-02 11:09:51 -04003392 vports = lpfc_create_vport_work_array(phba);
Arnd Bergmannaeb66412016-03-14 15:29:44 +01003393 if (vports != NULL) {
James Smartda0436e2009-05-22 14:51:39 -04003394 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -04003395 struct Scsi_Host *shost;
3396 shost = lpfc_shost_from_vport(vports[i]);
3397 spin_lock_irq(shost->host_lock);
3398 vports[i]->fc_flag &= ~FC_OFFLINE_MODE;
3399 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
3400 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart16a3a202013-04-17 20:14:38 -04003401 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart1c6834a2009-07-19 10:01:26 -04003402 vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
James Smart16a3a202013-04-17 20:14:38 -04003403 if ((vpis_cleared) &&
3404 (vports[i]->port_type !=
3405 LPFC_PHYSICAL_PORT))
3406 vports[i]->vpi = 0;
3407 }
James Smart549e55c2007-08-02 11:09:51 -04003408 spin_unlock_irq(shost->host_lock);
3409 }
Arnd Bergmannaeb66412016-03-14 15:29:44 +01003410 }
3411 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05003412
James Smartc4908502019-01-28 11:14:28 -08003413 if (phba->cfg_xri_rebalancing)
3414 lpfc_create_multixri_pools(phba);
3415
James Smart46fa3112007-04-25 09:51:45 -04003416 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05003417 return 0;
3418}
3419
James Smarte59058c2008-08-24 21:49:00 -04003420/**
James Smart3621a712009-04-06 18:47:14 -04003421 * lpfc_unblock_mgmt_io - Mark a HBA's management interface to be not blocked
James Smarte59058c2008-08-24 21:49:00 -04003422 * @phba: pointer to lpfc hba data structure.
3423 *
3424 * This routine marks a HBA's management interface as not blocked. Once the
3425 * HBA's management interface is marked as not blocked, all the user space
3426 * access to the HBA, whether they are from sysfs interface or libdfc
3427 * interface will be allowed. The HBA is set to block the management interface
3428 * when the driver prepares the HBA interface for online or offline and then
3429 * set to unblock the management interface afterwards.
3430 **/
James Smart46fa3112007-04-25 09:51:45 -04003431void
James Smart46fa3112007-04-25 09:51:45 -04003432lpfc_unblock_mgmt_io(struct lpfc_hba * phba)
3433{
3434 unsigned long iflag;
3435
James Smart2e0fef82007-06-17 19:56:36 -05003436 spin_lock_irqsave(&phba->hbalock, iflag);
3437 phba->sli.sli_flag &= ~LPFC_BLOCK_MGMT_IO;
3438 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart46fa3112007-04-25 09:51:45 -04003439}
3440
James Smarte59058c2008-08-24 21:49:00 -04003441/**
James Smart3621a712009-04-06 18:47:14 -04003442 * lpfc_offline_prep - Prepare a HBA to be brought offline
James Smarte59058c2008-08-24 21:49:00 -04003443 * @phba: pointer to lpfc hba data structure.
3444 *
3445 * This routine is invoked to prepare a HBA to be brought offline. It performs
3446 * unregistration login to all the nodes on all vports and flushes the mailbox
3447 * queue to make it ready to be brought offline.
3448 **/
James Smart46fa3112007-04-25 09:51:45 -04003449void
James Smart618a5232012-06-12 13:54:36 -04003450lpfc_offline_prep(struct lpfc_hba *phba, int mbx_action)
James Smart46fa3112007-04-25 09:51:45 -04003451{
James Smart2e0fef82007-06-17 19:56:36 -05003452 struct lpfc_vport *vport = phba->pport;
James Smart46fa3112007-04-25 09:51:45 -04003453 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart87af33f2007-10-27 13:37:43 -04003454 struct lpfc_vport **vports;
James Smart72100cc2010-02-12 14:43:01 -05003455 struct Scsi_Host *shost;
James Smart87af33f2007-10-27 13:37:43 -04003456 int i;
dea31012005-04-17 16:05:31 -05003457
James Smart2e0fef82007-06-17 19:56:36 -05003458 if (vport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04003459 return;
dea31012005-04-17 16:05:31 -05003460
James Smart618a5232012-06-12 13:54:36 -04003461 lpfc_block_mgmt_io(phba, mbx_action);
dea31012005-04-17 16:05:31 -05003462
3463 lpfc_linkdown(phba);
3464
James Smart87af33f2007-10-27 13:37:43 -04003465 /* Issue an unreg_login to all nodes on all vports */
3466 vports = lpfc_create_vport_work_array(phba);
3467 if (vports != NULL) {
James Smartda0436e2009-05-22 14:51:39 -04003468 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smarta8adb832007-10-27 13:37:53 -04003469 if (vports[i]->load_flag & FC_UNLOADING)
3470 continue;
James Smart72100cc2010-02-12 14:43:01 -05003471 shost = lpfc_shost_from_vport(vports[i]);
3472 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05003473 vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05003474 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
3475 vports[i]->fc_flag &= ~FC_VFI_REGISTERED;
James Smart72100cc2010-02-12 14:43:01 -05003476 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05003477
James Smart87af33f2007-10-27 13:37:43 -04003478 shost = lpfc_shost_from_vport(vports[i]);
3479 list_for_each_entry_safe(ndlp, next_ndlp,
3480 &vports[i]->fc_nodes,
3481 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05003482 if (!NLP_CHK_NODE_ACT(ndlp))
3483 continue;
James Smart87af33f2007-10-27 13:37:43 -04003484 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
3485 continue;
3486 if (ndlp->nlp_type & NLP_FABRIC) {
3487 lpfc_disc_state_machine(vports[i], ndlp,
3488 NULL, NLP_EVT_DEVICE_RECOVERY);
3489 lpfc_disc_state_machine(vports[i], ndlp,
3490 NULL, NLP_EVT_DEVICE_RM);
3491 }
3492 spin_lock_irq(shost->host_lock);
3493 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
James Smart401ee0c2012-03-01 22:35:34 -05003494 spin_unlock_irq(shost->host_lock);
James Smart6b5151f2012-01-18 16:24:06 -05003495 /*
3496 * Whenever an SLI4 port goes offline, free the
James Smart401ee0c2012-03-01 22:35:34 -05003497 * RPI. Get a new RPI when the adapter port
3498 * comes back online.
James Smart6b5151f2012-01-18 16:24:06 -05003499 */
James Smartbe6bb942015-04-07 15:07:22 -04003500 if (phba->sli_rev == LPFC_SLI_REV4) {
3501 lpfc_printf_vlog(ndlp->vport,
3502 KERN_INFO, LOG_NODE,
3503 "0011 lpfc_offline: "
3504 "ndlp:x%p did %x "
3505 "usgmap:x%x rpi:%x\n",
3506 ndlp, ndlp->nlp_DID,
3507 ndlp->nlp_usg_map,
3508 ndlp->nlp_rpi);
3509
James Smart6b5151f2012-01-18 16:24:06 -05003510 lpfc_sli4_free_rpi(phba, ndlp->nlp_rpi);
James Smartbe6bb942015-04-07 15:07:22 -04003511 }
James Smart87af33f2007-10-27 13:37:43 -04003512 lpfc_unreg_rpi(vports[i], ndlp);
3513 }
3514 }
3515 }
James Smart09372822008-01-11 01:52:54 -05003516 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05003517
James Smart618a5232012-06-12 13:54:36 -04003518 lpfc_sli_mbox_sys_shutdown(phba, mbx_action);
Dick Kennedyf485c182017-09-29 17:34:34 -07003519
3520 if (phba->wq)
3521 flush_workqueue(phba->wq);
James Smart46fa3112007-04-25 09:51:45 -04003522}
3523
James Smarte59058c2008-08-24 21:49:00 -04003524/**
James Smart3621a712009-04-06 18:47:14 -04003525 * lpfc_offline - Bring a HBA offline
James Smarte59058c2008-08-24 21:49:00 -04003526 * @phba: pointer to lpfc hba data structure.
3527 *
3528 * This routine actually brings a HBA offline. It stops all the timers
3529 * associated with the HBA, brings down the SLI layer, and eventually
3530 * marks the HBA as in offline state for the upper layer protocol.
3531 **/
James Smart46fa3112007-04-25 09:51:45 -04003532void
James Smart2e0fef82007-06-17 19:56:36 -05003533lpfc_offline(struct lpfc_hba *phba)
James Smart46fa3112007-04-25 09:51:45 -04003534{
James Smart549e55c2007-08-02 11:09:51 -04003535 struct Scsi_Host *shost;
3536 struct lpfc_vport **vports;
3537 int i;
James Smart46fa3112007-04-25 09:51:45 -04003538
James Smart549e55c2007-08-02 11:09:51 -04003539 if (phba->pport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04003540 return;
James Smart688a8862006-07-06 15:49:56 -04003541
James Smartda0436e2009-05-22 14:51:39 -04003542 /* stop port and all timers associated with this hba */
3543 lpfc_stop_port(phba);
Dick Kennedy4b40d022017-08-23 16:55:38 -07003544
3545 /* Tear down the local and target port registrations. The
3546 * nvme transports need to cleanup.
3547 */
3548 lpfc_nvmet_destroy_targetport(phba);
3549 lpfc_nvme_destroy_localport(phba->pport);
3550
James Smart51ef4c22007-08-02 11:10:31 -04003551 vports = lpfc_create_vport_work_array(phba);
3552 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04003553 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
James Smart51ef4c22007-08-02 11:10:31 -04003554 lpfc_stop_vport_timers(vports[i]);
James Smart09372822008-01-11 01:52:54 -05003555 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -05003556 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003557 "0460 Bring Adapter offline\n");
dea31012005-04-17 16:05:31 -05003558 /* Bring down the SLI Layer and cleanup. The HBA is offline
3559 now. */
3560 lpfc_sli_hba_down(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05003561 spin_lock_irq(&phba->hbalock);
James Smart7054a602007-04-25 09:52:34 -04003562 phba->work_ha = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05003563 spin_unlock_irq(&phba->hbalock);
James Smart549e55c2007-08-02 11:09:51 -04003564 vports = lpfc_create_vport_work_array(phba);
3565 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04003566 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -04003567 shost = lpfc_shost_from_vport(vports[i]);
James Smart549e55c2007-08-02 11:09:51 -04003568 spin_lock_irq(shost->host_lock);
3569 vports[i]->work_port_events = 0;
3570 vports[i]->fc_flag |= FC_OFFLINE_MODE;
3571 spin_unlock_irq(shost->host_lock);
3572 }
James Smart09372822008-01-11 01:52:54 -05003573 lpfc_destroy_vport_work_array(phba, vports);
James Smartc4908502019-01-28 11:14:28 -08003574
3575 if (phba->cfg_xri_rebalancing)
3576 lpfc_destroy_multixri_pools(phba);
dea31012005-04-17 16:05:31 -05003577}
3578
James Smarte59058c2008-08-24 21:49:00 -04003579/**
James Smart3621a712009-04-06 18:47:14 -04003580 * lpfc_scsi_free - Free all the SCSI buffers and IOCBs from driver lists
James Smarte59058c2008-08-24 21:49:00 -04003581 * @phba: pointer to lpfc hba data structure.
3582 *
3583 * This routine is to free all the SCSI buffers and IOCBs from the driver
3584 * list back to kernel. It is called from lpfc_pci_remove_one to free
3585 * the internal resources before the device is removed from the system.
James Smarte59058c2008-08-24 21:49:00 -04003586 **/
James Smart8a9d2e82012-05-09 21:16:12 -04003587static void
James Smart2e0fef82007-06-17 19:56:36 -05003588lpfc_scsi_free(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05003589{
James Smartc4908502019-01-28 11:14:28 -08003590 struct lpfc_io_buf *sb, *sb_next;
dea31012005-04-17 16:05:31 -05003591
James Smart895427b2017-02-12 13:52:30 -08003592 if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP))
3593 return;
3594
James Smart2e0fef82007-06-17 19:56:36 -05003595 spin_lock_irq(&phba->hbalock);
James Smarta40fc5f2013-04-17 20:17:40 -04003596
dea31012005-04-17 16:05:31 -05003597 /* Release all the lpfc_scsi_bufs maintained by this host. */
James Smarta40fc5f2013-04-17 20:17:40 -04003598
3599 spin_lock(&phba->scsi_buf_list_put_lock);
3600 list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list_put,
3601 list) {
dea31012005-04-17 16:05:31 -05003602 list_del(&sb->list);
Romain Perier771db5c2017-07-06 10:13:05 +02003603 dma_pool_free(phba->lpfc_sg_dma_buf_pool, sb->data,
James Smart92d7f7b2007-06-17 19:56:38 -05003604 sb->dma_handle);
dea31012005-04-17 16:05:31 -05003605 kfree(sb);
3606 phba->total_scsi_bufs--;
3607 }
James Smarta40fc5f2013-04-17 20:17:40 -04003608 spin_unlock(&phba->scsi_buf_list_put_lock);
3609
3610 spin_lock(&phba->scsi_buf_list_get_lock);
3611 list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list_get,
3612 list) {
3613 list_del(&sb->list);
Romain Perier771db5c2017-07-06 10:13:05 +02003614 dma_pool_free(phba->lpfc_sg_dma_buf_pool, sb->data,
James Smarta40fc5f2013-04-17 20:17:40 -04003615 sb->dma_handle);
3616 kfree(sb);
3617 phba->total_scsi_bufs--;
3618 }
3619 spin_unlock(&phba->scsi_buf_list_get_lock);
James Smart2e0fef82007-06-17 19:56:36 -05003620 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04003621}
James Smart0794d602019-01-28 11:14:19 -08003622
James Smart8a9d2e82012-05-09 21:16:12 -04003623/**
James Smart5e5b5112019-01-28 11:14:22 -08003624 * lpfc_io_free - Free all the IO buffers and IOCBs from driver lists
James Smart895427b2017-02-12 13:52:30 -08003625 * @phba: pointer to lpfc hba data structure.
3626 *
James Smart0794d602019-01-28 11:14:19 -08003627 * This routine is to free all the IO buffers and IOCBs from the driver
James Smart895427b2017-02-12 13:52:30 -08003628 * list back to kernel. It is called from lpfc_pci_remove_one to free
3629 * the internal resources before the device is removed from the system.
3630 **/
James Smartc4908502019-01-28 11:14:28 -08003631void
James Smart5e5b5112019-01-28 11:14:22 -08003632lpfc_io_free(struct lpfc_hba *phba)
James Smart895427b2017-02-12 13:52:30 -08003633{
James Smartc4908502019-01-28 11:14:28 -08003634 struct lpfc_io_buf *lpfc_ncmd, *lpfc_ncmd_next;
James Smart5e5b5112019-01-28 11:14:22 -08003635 struct lpfc_sli4_hdw_queue *qp;
3636 int idx;
James Smart895427b2017-02-12 13:52:30 -08003637
James Smart5e5b5112019-01-28 11:14:22 -08003638 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
3639 qp = &phba->sli4_hba.hdwq[idx];
3640 /* Release all the lpfc_nvme_bufs maintained by this host. */
3641 spin_lock(&qp->io_buf_list_put_lock);
3642 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
3643 &qp->lpfc_io_buf_list_put,
3644 list) {
3645 list_del(&lpfc_ncmd->list);
3646 qp->put_io_bufs--;
3647 dma_pool_free(phba->lpfc_sg_dma_buf_pool,
3648 lpfc_ncmd->data, lpfc_ncmd->dma_handle);
3649 kfree(lpfc_ncmd);
3650 qp->total_io_bufs--;
3651 }
3652 spin_unlock(&qp->io_buf_list_put_lock);
James Smart895427b2017-02-12 13:52:30 -08003653
James Smart5e5b5112019-01-28 11:14:22 -08003654 spin_lock(&qp->io_buf_list_get_lock);
3655 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
3656 &qp->lpfc_io_buf_list_get,
3657 list) {
3658 list_del(&lpfc_ncmd->list);
3659 qp->get_io_bufs--;
3660 dma_pool_free(phba->lpfc_sg_dma_buf_pool,
3661 lpfc_ncmd->data, lpfc_ncmd->dma_handle);
3662 kfree(lpfc_ncmd);
3663 qp->total_io_bufs--;
3664 }
3665 spin_unlock(&qp->io_buf_list_get_lock);
James Smart895427b2017-02-12 13:52:30 -08003666 }
James Smart895427b2017-02-12 13:52:30 -08003667}
James Smart0794d602019-01-28 11:14:19 -08003668
James Smart895427b2017-02-12 13:52:30 -08003669/**
3670 * lpfc_sli4_els_sgl_update - update ELS xri-sgl sizing and mapping
James Smart8a9d2e82012-05-09 21:16:12 -04003671 * @phba: pointer to lpfc hba data structure.
3672 *
3673 * This routine first calculates the sizes of the current els and allocated
3674 * scsi sgl lists, and then goes through all sgls to updates the physical
3675 * XRIs assigned due to port function reset. During port initialization, the
3676 * current els and allocated scsi sgl lists are 0s.
3677 *
3678 * Return codes
3679 * 0 - successful (for now, it always returns 0)
3680 **/
3681int
James Smart895427b2017-02-12 13:52:30 -08003682lpfc_sli4_els_sgl_update(struct lpfc_hba *phba)
James Smart8a9d2e82012-05-09 21:16:12 -04003683{
3684 struct lpfc_sglq *sglq_entry = NULL, *sglq_entry_next = NULL;
James Smart895427b2017-02-12 13:52:30 -08003685 uint16_t i, lxri, xri_cnt, els_xri_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04003686 LIST_HEAD(els_sgl_list);
James Smart8a9d2e82012-05-09 21:16:12 -04003687 int rc;
3688
3689 /*
3690 * update on pci function's els xri-sgl list
3691 */
3692 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
James Smart895427b2017-02-12 13:52:30 -08003693
James Smart8a9d2e82012-05-09 21:16:12 -04003694 if (els_xri_cnt > phba->sli4_hba.els_xri_cnt) {
3695 /* els xri-sgl expanded */
3696 xri_cnt = els_xri_cnt - phba->sli4_hba.els_xri_cnt;
3697 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3698 "3157 ELS xri-sgl count increased from "
3699 "%d to %d\n", phba->sli4_hba.els_xri_cnt,
3700 els_xri_cnt);
3701 /* allocate the additional els sgls */
3702 for (i = 0; i < xri_cnt; i++) {
3703 sglq_entry = kzalloc(sizeof(struct lpfc_sglq),
3704 GFP_KERNEL);
3705 if (sglq_entry == NULL) {
3706 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3707 "2562 Failure to allocate an "
3708 "ELS sgl entry:%d\n", i);
3709 rc = -ENOMEM;
3710 goto out_free_mem;
3711 }
3712 sglq_entry->buff_type = GEN_BUFF_TYPE;
3713 sglq_entry->virt = lpfc_mbuf_alloc(phba, 0,
3714 &sglq_entry->phys);
3715 if (sglq_entry->virt == NULL) {
3716 kfree(sglq_entry);
3717 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3718 "2563 Failure to allocate an "
3719 "ELS mbuf:%d\n", i);
3720 rc = -ENOMEM;
3721 goto out_free_mem;
3722 }
3723 sglq_entry->sgl = sglq_entry->virt;
3724 memset(sglq_entry->sgl, 0, LPFC_BPL_SIZE);
3725 sglq_entry->state = SGL_FREED;
3726 list_add_tail(&sglq_entry->list, &els_sgl_list);
3727 }
James Smart38c20672013-03-01 16:37:44 -05003728 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -08003729 spin_lock(&phba->sli4_hba.sgl_list_lock);
3730 list_splice_init(&els_sgl_list,
3731 &phba->sli4_hba.lpfc_els_sgl_list);
3732 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smart38c20672013-03-01 16:37:44 -05003733 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04003734 } else if (els_xri_cnt < phba->sli4_hba.els_xri_cnt) {
3735 /* els xri-sgl shrinked */
3736 xri_cnt = phba->sli4_hba.els_xri_cnt - els_xri_cnt;
3737 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3738 "3158 ELS xri-sgl count decreased from "
3739 "%d to %d\n", phba->sli4_hba.els_xri_cnt,
3740 els_xri_cnt);
3741 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -08003742 spin_lock(&phba->sli4_hba.sgl_list_lock);
3743 list_splice_init(&phba->sli4_hba.lpfc_els_sgl_list,
3744 &els_sgl_list);
James Smart8a9d2e82012-05-09 21:16:12 -04003745 /* release extra els sgls from list */
3746 for (i = 0; i < xri_cnt; i++) {
3747 list_remove_head(&els_sgl_list,
3748 sglq_entry, struct lpfc_sglq, list);
3749 if (sglq_entry) {
James Smart895427b2017-02-12 13:52:30 -08003750 __lpfc_mbuf_free(phba, sglq_entry->virt,
3751 sglq_entry->phys);
James Smart8a9d2e82012-05-09 21:16:12 -04003752 kfree(sglq_entry);
3753 }
3754 }
James Smart895427b2017-02-12 13:52:30 -08003755 list_splice_init(&els_sgl_list,
3756 &phba->sli4_hba.lpfc_els_sgl_list);
3757 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04003758 spin_unlock_irq(&phba->hbalock);
3759 } else
3760 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3761 "3163 ELS xri-sgl count unchanged: %d\n",
3762 els_xri_cnt);
3763 phba->sli4_hba.els_xri_cnt = els_xri_cnt;
3764
3765 /* update xris to els sgls on the list */
3766 sglq_entry = NULL;
3767 sglq_entry_next = NULL;
3768 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
James Smart895427b2017-02-12 13:52:30 -08003769 &phba->sli4_hba.lpfc_els_sgl_list, list) {
James Smart8a9d2e82012-05-09 21:16:12 -04003770 lxri = lpfc_sli4_next_xritag(phba);
3771 if (lxri == NO_XRI) {
3772 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3773 "2400 Failed to allocate xri for "
3774 "ELS sgl\n");
3775 rc = -ENOMEM;
3776 goto out_free_mem;
3777 }
3778 sglq_entry->sli4_lxritag = lxri;
3779 sglq_entry->sli4_xritag = phba->sli4_hba.xri_ids[lxri];
3780 }
James Smart895427b2017-02-12 13:52:30 -08003781 return 0;
3782
3783out_free_mem:
3784 lpfc_free_els_sgl_list(phba);
3785 return rc;
3786}
3787
3788/**
James Smartf358dd02017-02-12 13:52:34 -08003789 * lpfc_sli4_nvmet_sgl_update - update xri-sgl sizing and mapping
3790 * @phba: pointer to lpfc hba data structure.
3791 *
3792 * This routine first calculates the sizes of the current els and allocated
3793 * scsi sgl lists, and then goes through all sgls to updates the physical
3794 * XRIs assigned due to port function reset. During port initialization, the
3795 * current els and allocated scsi sgl lists are 0s.
3796 *
3797 * Return codes
3798 * 0 - successful (for now, it always returns 0)
3799 **/
3800int
3801lpfc_sli4_nvmet_sgl_update(struct lpfc_hba *phba)
3802{
3803 struct lpfc_sglq *sglq_entry = NULL, *sglq_entry_next = NULL;
3804 uint16_t i, lxri, xri_cnt, els_xri_cnt;
James Smart6c621a22017-05-15 15:20:45 -07003805 uint16_t nvmet_xri_cnt;
James Smartf358dd02017-02-12 13:52:34 -08003806 LIST_HEAD(nvmet_sgl_list);
3807 int rc;
3808
3809 /*
3810 * update on pci function's nvmet xri-sgl list
3811 */
3812 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
James Smart61f3d4b2017-05-15 15:20:41 -07003813
James Smart6c621a22017-05-15 15:20:45 -07003814 /* For NVMET, ALL remaining XRIs are dedicated for IO processing */
3815 nvmet_xri_cnt = phba->sli4_hba.max_cfg_param.max_xri - els_xri_cnt;
James Smartf358dd02017-02-12 13:52:34 -08003816 if (nvmet_xri_cnt > phba->sli4_hba.nvmet_xri_cnt) {
3817 /* els xri-sgl expanded */
3818 xri_cnt = nvmet_xri_cnt - phba->sli4_hba.nvmet_xri_cnt;
3819 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3820 "6302 NVMET xri-sgl cnt grew from %d to %d\n",
3821 phba->sli4_hba.nvmet_xri_cnt, nvmet_xri_cnt);
3822 /* allocate the additional nvmet sgls */
3823 for (i = 0; i < xri_cnt; i++) {
3824 sglq_entry = kzalloc(sizeof(struct lpfc_sglq),
3825 GFP_KERNEL);
3826 if (sglq_entry == NULL) {
3827 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3828 "6303 Failure to allocate an "
3829 "NVMET sgl entry:%d\n", i);
3830 rc = -ENOMEM;
3831 goto out_free_mem;
3832 }
3833 sglq_entry->buff_type = NVMET_BUFF_TYPE;
3834 sglq_entry->virt = lpfc_nvmet_buf_alloc(phba, 0,
3835 &sglq_entry->phys);
3836 if (sglq_entry->virt == NULL) {
3837 kfree(sglq_entry);
3838 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3839 "6304 Failure to allocate an "
3840 "NVMET buf:%d\n", i);
3841 rc = -ENOMEM;
3842 goto out_free_mem;
3843 }
3844 sglq_entry->sgl = sglq_entry->virt;
3845 memset(sglq_entry->sgl, 0,
3846 phba->cfg_sg_dma_buf_size);
3847 sglq_entry->state = SGL_FREED;
3848 list_add_tail(&sglq_entry->list, &nvmet_sgl_list);
3849 }
3850 spin_lock_irq(&phba->hbalock);
3851 spin_lock(&phba->sli4_hba.sgl_list_lock);
3852 list_splice_init(&nvmet_sgl_list,
3853 &phba->sli4_hba.lpfc_nvmet_sgl_list);
3854 spin_unlock(&phba->sli4_hba.sgl_list_lock);
3855 spin_unlock_irq(&phba->hbalock);
3856 } else if (nvmet_xri_cnt < phba->sli4_hba.nvmet_xri_cnt) {
3857 /* nvmet xri-sgl shrunk */
3858 xri_cnt = phba->sli4_hba.nvmet_xri_cnt - nvmet_xri_cnt;
3859 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3860 "6305 NVMET xri-sgl count decreased from "
3861 "%d to %d\n", phba->sli4_hba.nvmet_xri_cnt,
3862 nvmet_xri_cnt);
3863 spin_lock_irq(&phba->hbalock);
3864 spin_lock(&phba->sli4_hba.sgl_list_lock);
3865 list_splice_init(&phba->sli4_hba.lpfc_nvmet_sgl_list,
3866 &nvmet_sgl_list);
3867 /* release extra nvmet sgls from list */
3868 for (i = 0; i < xri_cnt; i++) {
3869 list_remove_head(&nvmet_sgl_list,
3870 sglq_entry, struct lpfc_sglq, list);
3871 if (sglq_entry) {
3872 lpfc_nvmet_buf_free(phba, sglq_entry->virt,
3873 sglq_entry->phys);
3874 kfree(sglq_entry);
3875 }
3876 }
3877 list_splice_init(&nvmet_sgl_list,
3878 &phba->sli4_hba.lpfc_nvmet_sgl_list);
3879 spin_unlock(&phba->sli4_hba.sgl_list_lock);
3880 spin_unlock_irq(&phba->hbalock);
3881 } else
3882 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3883 "6306 NVMET xri-sgl count unchanged: %d\n",
3884 nvmet_xri_cnt);
3885 phba->sli4_hba.nvmet_xri_cnt = nvmet_xri_cnt;
3886
3887 /* update xris to nvmet sgls on the list */
3888 sglq_entry = NULL;
3889 sglq_entry_next = NULL;
3890 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
3891 &phba->sli4_hba.lpfc_nvmet_sgl_list, list) {
3892 lxri = lpfc_sli4_next_xritag(phba);
3893 if (lxri == NO_XRI) {
3894 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3895 "6307 Failed to allocate xri for "
3896 "NVMET sgl\n");
3897 rc = -ENOMEM;
3898 goto out_free_mem;
3899 }
3900 sglq_entry->sli4_lxritag = lxri;
3901 sglq_entry->sli4_xritag = phba->sli4_hba.xri_ids[lxri];
3902 }
3903 return 0;
3904
3905out_free_mem:
3906 lpfc_free_nvmet_sgl_list(phba);
3907 return rc;
3908}
3909
James Smart5e5b5112019-01-28 11:14:22 -08003910int
3911lpfc_io_buf_flush(struct lpfc_hba *phba, struct list_head *cbuf)
3912{
3913 LIST_HEAD(blist);
3914 struct lpfc_sli4_hdw_queue *qp;
James Smartc4908502019-01-28 11:14:28 -08003915 struct lpfc_io_buf *lpfc_cmd;
3916 struct lpfc_io_buf *iobufp, *prev_iobufp;
James Smart5e5b5112019-01-28 11:14:22 -08003917 int idx, cnt, xri, inserted;
3918
3919 cnt = 0;
3920 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
3921 qp = &phba->sli4_hba.hdwq[idx];
3922 spin_lock_irq(&qp->io_buf_list_get_lock);
3923 spin_lock(&qp->io_buf_list_put_lock);
3924
3925 /* Take everything off the get and put lists */
3926 list_splice_init(&qp->lpfc_io_buf_list_get, &blist);
3927 list_splice(&qp->lpfc_io_buf_list_put, &blist);
3928 INIT_LIST_HEAD(&qp->lpfc_io_buf_list_get);
3929 INIT_LIST_HEAD(&qp->lpfc_io_buf_list_put);
3930 cnt += qp->get_io_bufs + qp->put_io_bufs;
3931 qp->get_io_bufs = 0;
3932 qp->put_io_bufs = 0;
3933 qp->total_io_bufs = 0;
3934 spin_unlock(&qp->io_buf_list_put_lock);
3935 spin_unlock_irq(&qp->io_buf_list_get_lock);
3936 }
3937
3938 /*
3939 * Take IO buffers off blist and put on cbuf sorted by XRI.
3940 * This is because POST_SGL takes a sequential range of XRIs
3941 * to post to the firmware.
3942 */
3943 for (idx = 0; idx < cnt; idx++) {
James Smartc4908502019-01-28 11:14:28 -08003944 list_remove_head(&blist, lpfc_cmd, struct lpfc_io_buf, list);
James Smart5e5b5112019-01-28 11:14:22 -08003945 if (!lpfc_cmd)
3946 return cnt;
3947 if (idx == 0) {
3948 list_add_tail(&lpfc_cmd->list, cbuf);
3949 continue;
3950 }
3951 xri = lpfc_cmd->cur_iocbq.sli4_xritag;
3952 inserted = 0;
3953 prev_iobufp = NULL;
3954 list_for_each_entry(iobufp, cbuf, list) {
3955 if (xri < iobufp->cur_iocbq.sli4_xritag) {
3956 if (prev_iobufp)
3957 list_add(&lpfc_cmd->list,
3958 &prev_iobufp->list);
3959 else
3960 list_add(&lpfc_cmd->list, cbuf);
3961 inserted = 1;
3962 break;
3963 }
3964 prev_iobufp = iobufp;
3965 }
3966 if (!inserted)
3967 list_add_tail(&lpfc_cmd->list, cbuf);
3968 }
3969 return cnt;
3970}
3971
3972int
3973lpfc_io_buf_replenish(struct lpfc_hba *phba, struct list_head *cbuf)
3974{
3975 struct lpfc_sli4_hdw_queue *qp;
James Smartc4908502019-01-28 11:14:28 -08003976 struct lpfc_io_buf *lpfc_cmd;
James Smart5e5b5112019-01-28 11:14:22 -08003977 int idx, cnt;
3978
3979 qp = phba->sli4_hba.hdwq;
3980 cnt = 0;
3981 while (!list_empty(cbuf)) {
3982 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
3983 list_remove_head(cbuf, lpfc_cmd,
James Smartc4908502019-01-28 11:14:28 -08003984 struct lpfc_io_buf, list);
James Smart5e5b5112019-01-28 11:14:22 -08003985 if (!lpfc_cmd)
3986 return cnt;
3987 cnt++;
3988 qp = &phba->sli4_hba.hdwq[idx];
James Smart1fbf9742019-01-28 11:14:26 -08003989 lpfc_cmd->hdwq_no = idx;
3990 lpfc_cmd->hdwq = qp;
James Smart5e5b5112019-01-28 11:14:22 -08003991 lpfc_cmd->cur_iocbq.wqe_cmpl = NULL;
3992 lpfc_cmd->cur_iocbq.iocb_cmpl = NULL;
3993 spin_lock(&qp->io_buf_list_put_lock);
3994 list_add_tail(&lpfc_cmd->list,
3995 &qp->lpfc_io_buf_list_put);
3996 qp->put_io_bufs++;
3997 qp->total_io_bufs++;
3998 spin_unlock(&qp->io_buf_list_put_lock);
3999 }
4000 }
4001 return cnt;
4002}
4003
James Smartf358dd02017-02-12 13:52:34 -08004004/**
James Smart5e5b5112019-01-28 11:14:22 -08004005 * lpfc_sli4_io_sgl_update - update xri-sgl sizing and mapping
James Smart895427b2017-02-12 13:52:30 -08004006 * @phba: pointer to lpfc hba data structure.
4007 *
4008 * This routine first calculates the sizes of the current els and allocated
4009 * scsi sgl lists, and then goes through all sgls to updates the physical
4010 * XRIs assigned due to port function reset. During port initialization, the
4011 * current els and allocated scsi sgl lists are 0s.
4012 *
4013 * Return codes
4014 * 0 - successful (for now, it always returns 0)
4015 **/
4016int
James Smart5e5b5112019-01-28 11:14:22 -08004017lpfc_sli4_io_sgl_update(struct lpfc_hba *phba)
James Smart895427b2017-02-12 13:52:30 -08004018{
James Smartc4908502019-01-28 11:14:28 -08004019 struct lpfc_io_buf *lpfc_ncmd = NULL, *lpfc_ncmd_next = NULL;
James Smart0794d602019-01-28 11:14:19 -08004020 uint16_t i, lxri, els_xri_cnt;
James Smart5e5b5112019-01-28 11:14:22 -08004021 uint16_t io_xri_cnt, io_xri_max;
4022 LIST_HEAD(io_sgl_list);
James Smart0794d602019-01-28 11:14:19 -08004023 int rc, cnt;
James Smart895427b2017-02-12 13:52:30 -08004024
4025 /*
James Smart0794d602019-01-28 11:14:19 -08004026 * update on pci function's allocated nvme xri-sgl list
James Smart895427b2017-02-12 13:52:30 -08004027 */
James Smart0794d602019-01-28 11:14:19 -08004028
4029 /* maximum number of xris available for nvme buffers */
James Smart895427b2017-02-12 13:52:30 -08004030 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
James Smart5e5b5112019-01-28 11:14:22 -08004031 io_xri_max = phba->sli4_hba.max_cfg_param.max_xri - els_xri_cnt;
4032 phba->sli4_hba.io_xri_max = io_xri_max;
James Smart8a9d2e82012-05-09 21:16:12 -04004033
James Smarte8c0a772017-04-21 16:04:49 -07004034 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart0794d602019-01-28 11:14:19 -08004035 "6074 Current allocated XRI sgl count:%d, "
4036 "maximum XRI count:%d\n",
James Smart5e5b5112019-01-28 11:14:22 -08004037 phba->sli4_hba.io_xri_cnt,
4038 phba->sli4_hba.io_xri_max);
James Smarte8c0a772017-04-21 16:04:49 -07004039
James Smart5e5b5112019-01-28 11:14:22 -08004040 cnt = lpfc_io_buf_flush(phba, &io_sgl_list);
James Smart0794d602019-01-28 11:14:19 -08004041
James Smart5e5b5112019-01-28 11:14:22 -08004042 if (phba->sli4_hba.io_xri_cnt > phba->sli4_hba.io_xri_max) {
James Smart0794d602019-01-28 11:14:19 -08004043 /* max nvme xri shrunk below the allocated nvme buffers */
James Smart5e5b5112019-01-28 11:14:22 -08004044 io_xri_cnt = phba->sli4_hba.io_xri_cnt -
4045 phba->sli4_hba.io_xri_max;
James Smart0794d602019-01-28 11:14:19 -08004046 /* release the extra allocated nvme buffers */
James Smart5e5b5112019-01-28 11:14:22 -08004047 for (i = 0; i < io_xri_cnt; i++) {
4048 list_remove_head(&io_sgl_list, lpfc_ncmd,
James Smartc4908502019-01-28 11:14:28 -08004049 struct lpfc_io_buf, list);
James Smart0794d602019-01-28 11:14:19 -08004050 if (lpfc_ncmd) {
Romain Perier771db5c2017-07-06 10:13:05 +02004051 dma_pool_free(phba->lpfc_sg_dma_buf_pool,
James Smart0794d602019-01-28 11:14:19 -08004052 lpfc_ncmd->data,
4053 lpfc_ncmd->dma_handle);
4054 kfree(lpfc_ncmd);
James Smarta2fc4aef2014-09-03 12:57:55 -04004055 }
James Smart8a9d2e82012-05-09 21:16:12 -04004056 }
James Smart5e5b5112019-01-28 11:14:22 -08004057 phba->sli4_hba.io_xri_cnt -= io_xri_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04004058 }
4059
James Smart0794d602019-01-28 11:14:19 -08004060 /* update xris associated to remaining allocated nvme buffers */
4061 lpfc_ncmd = NULL;
4062 lpfc_ncmd_next = NULL;
James Smart5e5b5112019-01-28 11:14:22 -08004063 phba->sli4_hba.io_xri_cnt = cnt;
James Smart0794d602019-01-28 11:14:19 -08004064 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
James Smart5e5b5112019-01-28 11:14:22 -08004065 &io_sgl_list, list) {
James Smart8a9d2e82012-05-09 21:16:12 -04004066 lxri = lpfc_sli4_next_xritag(phba);
4067 if (lxri == NO_XRI) {
4068 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart0794d602019-01-28 11:14:19 -08004069 "6075 Failed to allocate xri for "
4070 "nvme buffer\n");
James Smart8a9d2e82012-05-09 21:16:12 -04004071 rc = -ENOMEM;
4072 goto out_free_mem;
4073 }
James Smart0794d602019-01-28 11:14:19 -08004074 lpfc_ncmd->cur_iocbq.sli4_lxritag = lxri;
4075 lpfc_ncmd->cur_iocbq.sli4_xritag = phba->sli4_hba.xri_ids[lxri];
James Smart8a9d2e82012-05-09 21:16:12 -04004076 }
James Smart5e5b5112019-01-28 11:14:22 -08004077 cnt = lpfc_io_buf_replenish(phba, &io_sgl_list);
dea31012005-04-17 16:05:31 -05004078 return 0;
James Smart8a9d2e82012-05-09 21:16:12 -04004079
4080out_free_mem:
James Smart5e5b5112019-01-28 11:14:22 -08004081 lpfc_io_free(phba);
James Smart8a9d2e82012-05-09 21:16:12 -04004082 return rc;
dea31012005-04-17 16:05:31 -05004083}
4084
James Smart0794d602019-01-28 11:14:19 -08004085/**
James Smart5e5b5112019-01-28 11:14:22 -08004086 * lpfc_new_io_buf - IO buffer allocator for HBA with SLI4 IF spec
James Smart0794d602019-01-28 11:14:19 -08004087 * @vport: The virtual port for which this call being executed.
4088 * @num_to_allocate: The requested number of buffers to allocate.
4089 *
4090 * This routine allocates nvme buffers for device with SLI-4 interface spec,
4091 * the nvme buffer contains all the necessary information needed to initiate
4092 * an I/O. After allocating up to @num_to_allocate IO buffers and put
4093 * them on a list, it post them to the port by using SGL block post.
4094 *
4095 * Return codes:
James Smart5e5b5112019-01-28 11:14:22 -08004096 * int - number of IO buffers that were allocated and posted.
James Smart0794d602019-01-28 11:14:19 -08004097 * 0 = failure, less than num_to_alloc is a partial failure.
4098 **/
4099int
James Smart5e5b5112019-01-28 11:14:22 -08004100lpfc_new_io_buf(struct lpfc_hba *phba, int num_to_alloc)
James Smart0794d602019-01-28 11:14:19 -08004101{
James Smartc4908502019-01-28 11:14:28 -08004102 struct lpfc_io_buf *lpfc_ncmd;
James Smart0794d602019-01-28 11:14:19 -08004103 struct lpfc_iocbq *pwqeq;
4104 uint16_t iotag, lxri = 0;
4105 int bcnt, num_posted;
4106 LIST_HEAD(prep_nblist);
4107 LIST_HEAD(post_nblist);
4108 LIST_HEAD(nvme_nblist);
4109
4110 /* Sanity check to ensure our sizing is right for both SCSI and NVME */
James Smartc4908502019-01-28 11:14:28 -08004111 if (sizeof(struct lpfc_io_buf) > LPFC_COMMON_IO_BUF_SZ) {
James Smart0794d602019-01-28 11:14:19 -08004112 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
Arnd Bergmannf9968612019-03-04 20:39:09 +01004113 "6426 Common buffer size %zd exceeds %d\n",
James Smartc4908502019-01-28 11:14:28 -08004114 sizeof(struct lpfc_io_buf),
4115 LPFC_COMMON_IO_BUF_SZ);
James Smart0794d602019-01-28 11:14:19 -08004116 return 0;
4117 }
4118
James Smart5e5b5112019-01-28 11:14:22 -08004119 phba->sli4_hba.io_xri_cnt = 0;
James Smart0794d602019-01-28 11:14:19 -08004120 for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
4121 lpfc_ncmd = kzalloc(LPFC_COMMON_IO_BUF_SZ, GFP_KERNEL);
4122 if (!lpfc_ncmd)
4123 break;
4124 /*
4125 * Get memory from the pci pool to map the virt space to
4126 * pci bus space for an I/O. The DMA buffer includes the
4127 * number of SGE's necessary to support the sg_tablesize.
4128 */
Thomas Meyera5c990e2019-05-29 22:21:36 +02004129 lpfc_ncmd->data = dma_pool_zalloc(phba->lpfc_sg_dma_buf_pool,
4130 GFP_KERNEL,
4131 &lpfc_ncmd->dma_handle);
James Smart0794d602019-01-28 11:14:19 -08004132 if (!lpfc_ncmd->data) {
4133 kfree(lpfc_ncmd);
4134 break;
4135 }
James Smart0794d602019-01-28 11:14:19 -08004136
4137 /*
4138 * 4K Page alignment is CRITICAL to BlockGuard, double check
4139 * to be sure.
4140 */
4141 if ((phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
4142 (((unsigned long)(lpfc_ncmd->data) &
4143 (unsigned long)(SLI4_PAGE_SIZE - 1)) != 0)) {
4144 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
4145 "3369 Memory alignment err: addr=%lx\n",
4146 (unsigned long)lpfc_ncmd->data);
4147 dma_pool_free(phba->lpfc_sg_dma_buf_pool,
4148 lpfc_ncmd->data, lpfc_ncmd->dma_handle);
4149 kfree(lpfc_ncmd);
4150 break;
4151 }
4152
4153 lxri = lpfc_sli4_next_xritag(phba);
4154 if (lxri == NO_XRI) {
4155 dma_pool_free(phba->lpfc_sg_dma_buf_pool,
4156 lpfc_ncmd->data, lpfc_ncmd->dma_handle);
4157 kfree(lpfc_ncmd);
4158 break;
4159 }
4160 pwqeq = &lpfc_ncmd->cur_iocbq;
4161
4162 /* Allocate iotag for lpfc_ncmd->cur_iocbq. */
4163 iotag = lpfc_sli_next_iotag(phba, pwqeq);
4164 if (iotag == 0) {
4165 dma_pool_free(phba->lpfc_sg_dma_buf_pool,
4166 lpfc_ncmd->data, lpfc_ncmd->dma_handle);
4167 kfree(lpfc_ncmd);
4168 lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
4169 "6121 Failed to allocate IOTAG for"
4170 " XRI:0x%x\n", lxri);
4171 lpfc_sli4_free_xri(phba, lxri);
4172 break;
4173 }
4174 pwqeq->sli4_lxritag = lxri;
4175 pwqeq->sli4_xritag = phba->sli4_hba.xri_ids[lxri];
4176 pwqeq->context1 = lpfc_ncmd;
4177
4178 /* Initialize local short-hand pointers. */
4179 lpfc_ncmd->dma_sgl = lpfc_ncmd->data;
4180 lpfc_ncmd->dma_phys_sgl = lpfc_ncmd->dma_handle;
4181 lpfc_ncmd->cur_iocbq.context1 = lpfc_ncmd;
James Smartc2017262019-01-28 11:14:37 -08004182 spin_lock_init(&lpfc_ncmd->buf_lock);
James Smart0794d602019-01-28 11:14:19 -08004183
4184 /* add the nvme buffer to a post list */
4185 list_add_tail(&lpfc_ncmd->list, &post_nblist);
James Smart5e5b5112019-01-28 11:14:22 -08004186 phba->sli4_hba.io_xri_cnt++;
James Smart0794d602019-01-28 11:14:19 -08004187 }
4188 lpfc_printf_log(phba, KERN_INFO, LOG_NVME,
4189 "6114 Allocate %d out of %d requested new NVME "
4190 "buffers\n", bcnt, num_to_alloc);
4191
4192 /* post the list of nvme buffer sgls to port if available */
4193 if (!list_empty(&post_nblist))
James Smart5e5b5112019-01-28 11:14:22 -08004194 num_posted = lpfc_sli4_post_io_sgl_list(
James Smart0794d602019-01-28 11:14:19 -08004195 phba, &post_nblist, bcnt);
4196 else
4197 num_posted = 0;
4198
4199 return num_posted;
4200}
4201
James Smart96418b52017-03-04 09:30:31 -08004202static uint64_t
4203lpfc_get_wwpn(struct lpfc_hba *phba)
4204{
4205 uint64_t wwn;
4206 int rc;
4207 LPFC_MBOXQ_t *mboxq;
4208 MAILBOX_t *mb;
4209
James Smart96418b52017-03-04 09:30:31 -08004210 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
4211 GFP_KERNEL);
4212 if (!mboxq)
4213 return (uint64_t)-1;
4214
4215 /* First get WWN of HBA instance */
4216 lpfc_read_nv(phba, mboxq);
4217 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4218 if (rc != MBX_SUCCESS) {
4219 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4220 "6019 Mailbox failed , mbxCmd x%x "
4221 "READ_NV, mbxStatus x%x\n",
4222 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
4223 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
4224 mempool_free(mboxq, phba->mbox_mem_pool);
4225 return (uint64_t) -1;
4226 }
4227 mb = &mboxq->u.mb;
4228 memcpy(&wwn, (char *)mb->un.varRDnvp.portname, sizeof(uint64_t));
4229 /* wwn is WWPN of HBA instance */
4230 mempool_free(mboxq, phba->mbox_mem_pool);
4231 if (phba->sli_rev == LPFC_SLI_REV4)
4232 return be64_to_cpu(wwn);
4233 else
Maurizio Lombardi286871a2017-08-23 16:55:48 -07004234 return rol64(wwn, 32);
James Smart96418b52017-03-04 09:30:31 -08004235}
4236
James Smarte59058c2008-08-24 21:49:00 -04004237/**
James Smart3621a712009-04-06 18:47:14 -04004238 * lpfc_create_port - Create an FC port
James Smarte59058c2008-08-24 21:49:00 -04004239 * @phba: pointer to lpfc hba data structure.
4240 * @instance: a unique integer ID to this FC port.
4241 * @dev: pointer to the device data structure.
4242 *
4243 * This routine creates a FC port for the upper layer protocol. The FC port
4244 * can be created on top of either a physical port or a virtual port provided
4245 * by the HBA. This routine also allocates a SCSI host data structure (shost)
4246 * and associates the FC port created before adding the shost into the SCSI
4247 * layer.
4248 *
4249 * Return codes
4250 * @vport - pointer to the virtual N_Port data structure.
4251 * NULL - port create failed.
4252 **/
James Smart2e0fef82007-06-17 19:56:36 -05004253struct lpfc_vport *
James Smart3de2a652007-08-02 11:09:59 -04004254lpfc_create_port(struct lpfc_hba *phba, int instance, struct device *dev)
James Smart47a86172007-04-25 09:53:22 -04004255{
James Smart2e0fef82007-06-17 19:56:36 -05004256 struct lpfc_vport *vport;
James Smart895427b2017-02-12 13:52:30 -08004257 struct Scsi_Host *shost = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05004258 int error = 0;
James Smart96418b52017-03-04 09:30:31 -08004259 int i;
4260 uint64_t wwn;
4261 bool use_no_reset_hba = false;
James Smart56bc8022017-06-15 22:56:43 -07004262 int rc;
James Smart96418b52017-03-04 09:30:31 -08004263
James Smart56bc8022017-06-15 22:56:43 -07004264 if (lpfc_no_hba_reset_cnt) {
4265 if (phba->sli_rev < LPFC_SLI_REV4 &&
4266 dev == &phba->pcidev->dev) {
4267 /* Reset the port first */
4268 lpfc_sli_brdrestart(phba);
4269 rc = lpfc_sli_chipset_init(phba);
4270 if (rc)
4271 return NULL;
4272 }
4273 wwn = lpfc_get_wwpn(phba);
4274 }
James Smart96418b52017-03-04 09:30:31 -08004275
4276 for (i = 0; i < lpfc_no_hba_reset_cnt; i++) {
4277 if (wwn == lpfc_no_hba_reset[i]) {
4278 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4279 "6020 Setting use_no_reset port=%llx\n",
4280 wwn);
4281 use_no_reset_hba = true;
4282 break;
4283 }
4284 }
James Smart47a86172007-04-25 09:53:22 -04004285
James Smart895427b2017-02-12 13:52:30 -08004286 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) {
4287 if (dev != &phba->pcidev->dev) {
4288 shost = scsi_host_alloc(&lpfc_vport_template,
4289 sizeof(struct lpfc_vport));
4290 } else {
James Smart96418b52017-03-04 09:30:31 -08004291 if (!use_no_reset_hba)
James Smart895427b2017-02-12 13:52:30 -08004292 shost = scsi_host_alloc(&lpfc_template,
4293 sizeof(struct lpfc_vport));
4294 else
James Smart96418b52017-03-04 09:30:31 -08004295 shost = scsi_host_alloc(&lpfc_template_no_hr,
James Smart895427b2017-02-12 13:52:30 -08004296 sizeof(struct lpfc_vport));
4297 }
4298 } else if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
4299 shost = scsi_host_alloc(&lpfc_template_nvme,
James Smartea4142f2015-04-07 15:07:13 -04004300 sizeof(struct lpfc_vport));
4301 }
James Smart2e0fef82007-06-17 19:56:36 -05004302 if (!shost)
4303 goto out;
James Smart47a86172007-04-25 09:53:22 -04004304
James Smart2e0fef82007-06-17 19:56:36 -05004305 vport = (struct lpfc_vport *) shost->hostdata;
4306 vport->phba = phba;
James Smart2e0fef82007-06-17 19:56:36 -05004307 vport->load_flag |= FC_LOADING;
James Smart92d7f7b2007-06-17 19:56:38 -05004308 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart7f5f3d02008-02-08 18:50:14 -05004309 vport->fc_rscn_flush = 0;
James Smart3de2a652007-08-02 11:09:59 -04004310 lpfc_get_vport_cfgparam(vport);
James Smart895427b2017-02-12 13:52:30 -08004311
James Smartf6e84792019-01-28 11:14:38 -08004312 /* Adjust value in vport */
4313 vport->cfg_enable_fc4_type = phba->cfg_enable_fc4_type;
4314
James Smart2e0fef82007-06-17 19:56:36 -05004315 shost->unique_id = instance;
4316 shost->max_id = LPFC_MAX_TARGET;
James Smart3de2a652007-08-02 11:09:59 -04004317 shost->max_lun = vport->cfg_max_luns;
James Smart2e0fef82007-06-17 19:56:36 -05004318 shost->this_id = -1;
4319 shost->max_cmd_len = 16;
James Smart6a828b02019-01-28 11:14:31 -08004320
James Smartda0436e2009-05-22 14:51:39 -04004321 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart6a828b02019-01-28 11:14:31 -08004322 if (phba->cfg_fcp_io_sched == LPFC_FCP_SCHED_BY_HDWQ)
4323 shost->nr_hw_queues = phba->cfg_hdw_queue;
4324 else
4325 shost->nr_hw_queues = phba->sli4_hba.num_present_cpu;
4326
James Smart28baac72010-02-12 14:42:03 -05004327 shost->dma_boundary =
James Smartcb5172e2010-03-15 11:25:07 -04004328 phba->sli4_hba.pc_sli4_params.sge_supp_len-1;
James Smart5b9e70b2018-09-10 10:30:42 -07004329 shost->sg_tablesize = phba->cfg_scsi_seg_cnt;
James Smartace44e42019-01-28 11:14:27 -08004330 } else
4331 /* SLI-3 has a limited number of hardware queues (3),
4332 * thus there is only one for FCP processing.
4333 */
4334 shost->nr_hw_queues = 1;
James Smart81301a92008-12-04 22:39:46 -05004335
James Smart47a86172007-04-25 09:53:22 -04004336 /*
James Smart2e0fef82007-06-17 19:56:36 -05004337 * Set initial can_queue value since 0 is no longer supported and
4338 * scsi_add_host will fail. This will be adjusted later based on the
4339 * max xri value determined in hba setup.
James Smart47a86172007-04-25 09:53:22 -04004340 */
James Smart2e0fef82007-06-17 19:56:36 -05004341 shost->can_queue = phba->cfg_hba_queue_depth - 10;
James Smart3de2a652007-08-02 11:09:59 -04004342 if (dev != &phba->pcidev->dev) {
James Smart92d7f7b2007-06-17 19:56:38 -05004343 shost->transportt = lpfc_vport_transport_template;
4344 vport->port_type = LPFC_NPIV_PORT;
4345 } else {
4346 shost->transportt = lpfc_transport_template;
4347 vport->port_type = LPFC_PHYSICAL_PORT;
4348 }
James Smart47a86172007-04-25 09:53:22 -04004349
James Smart2e0fef82007-06-17 19:56:36 -05004350 /* Initialize all internally managed lists. */
4351 INIT_LIST_HEAD(&vport->fc_nodes);
James Smartda0436e2009-05-22 14:51:39 -04004352 INIT_LIST_HEAD(&vport->rcv_buffer_list);
James Smart2e0fef82007-06-17 19:56:36 -05004353 spin_lock_init(&vport->work_port_lock);
James Smart47a86172007-04-25 09:53:22 -04004354
Kees Cookf22eb4d2017-09-06 20:24:26 -07004355 timer_setup(&vport->fc_disctmo, lpfc_disc_timeout, 0);
James Smart47a86172007-04-25 09:53:22 -04004356
Kees Cookf22eb4d2017-09-06 20:24:26 -07004357 timer_setup(&vport->els_tmofunc, lpfc_els_timeout, 0);
James Smart92494142011-02-16 12:39:44 -05004358
Kees Cookf22eb4d2017-09-06 20:24:26 -07004359 timer_setup(&vport->delayed_disc_tmo, lpfc_delayed_disc_tmo, 0);
James Smart92494142011-02-16 12:39:44 -05004360
James Smartaa6ff302019-05-21 17:49:09 -07004361 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED)
4362 lpfc_setup_bg(phba, shost);
4363
James Bottomleyd139b9b2009-11-05 13:33:12 -06004364 error = scsi_add_host_with_dma(shost, dev, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05004365 if (error)
4366 goto out_put_shost;
James Smart47a86172007-04-25 09:53:22 -04004367
James Smart523128e2018-09-10 10:30:46 -07004368 spin_lock_irq(&phba->port_list_lock);
James Smart2e0fef82007-06-17 19:56:36 -05004369 list_add_tail(&vport->listentry, &phba->port_list);
James Smart523128e2018-09-10 10:30:46 -07004370 spin_unlock_irq(&phba->port_list_lock);
James Smart2e0fef82007-06-17 19:56:36 -05004371 return vport;
James Smart47a86172007-04-25 09:53:22 -04004372
James Smart2e0fef82007-06-17 19:56:36 -05004373out_put_shost:
4374 scsi_host_put(shost);
4375out:
4376 return NULL;
James Smart47a86172007-04-25 09:53:22 -04004377}
4378
James Smarte59058c2008-08-24 21:49:00 -04004379/**
James Smart3621a712009-04-06 18:47:14 -04004380 * destroy_port - destroy an FC port
James Smarte59058c2008-08-24 21:49:00 -04004381 * @vport: pointer to an lpfc virtual N_Port data structure.
4382 *
4383 * This routine destroys a FC port from the upper layer protocol. All the
4384 * resources associated with the port are released.
4385 **/
James Smart2e0fef82007-06-17 19:56:36 -05004386void
4387destroy_port(struct lpfc_vport *vport)
James Smart47a86172007-04-25 09:53:22 -04004388{
James Smart92d7f7b2007-06-17 19:56:38 -05004389 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4390 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04004391
James Smart858c9f62007-06-17 19:56:39 -05004392 lpfc_debugfs_terminate(vport);
James Smart92d7f7b2007-06-17 19:56:38 -05004393 fc_remove_host(shost);
4394 scsi_remove_host(shost);
James Smart47a86172007-04-25 09:53:22 -04004395
James Smart523128e2018-09-10 10:30:46 -07004396 spin_lock_irq(&phba->port_list_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05004397 list_del_init(&vport->listentry);
James Smart523128e2018-09-10 10:30:46 -07004398 spin_unlock_irq(&phba->port_list_lock);
James Smart47a86172007-04-25 09:53:22 -04004399
James Smart92d7f7b2007-06-17 19:56:38 -05004400 lpfc_cleanup(vport);
James Smart47a86172007-04-25 09:53:22 -04004401 return;
James Smart47a86172007-04-25 09:53:22 -04004402}
4403
James Smarte59058c2008-08-24 21:49:00 -04004404/**
James Smart3621a712009-04-06 18:47:14 -04004405 * lpfc_get_instance - Get a unique integer ID
James Smarte59058c2008-08-24 21:49:00 -04004406 *
4407 * This routine allocates a unique integer ID from lpfc_hba_index pool. It
4408 * uses the kernel idr facility to perform the task.
4409 *
4410 * Return codes:
4411 * instance - a unique integer ID allocated as the new instance.
4412 * -1 - lpfc get instance failed.
4413 **/
James Smart92d7f7b2007-06-17 19:56:38 -05004414int
4415lpfc_get_instance(void)
4416{
Tejun Heoab516032013-02-27 17:04:44 -08004417 int ret;
James Smart92d7f7b2007-06-17 19:56:38 -05004418
Tejun Heoab516032013-02-27 17:04:44 -08004419 ret = idr_alloc(&lpfc_hba_index, NULL, 0, 0, GFP_KERNEL);
4420 return ret < 0 ? -1 : ret;
James Smart92d7f7b2007-06-17 19:56:38 -05004421}
4422
James Smarte59058c2008-08-24 21:49:00 -04004423/**
James Smart3621a712009-04-06 18:47:14 -04004424 * lpfc_scan_finished - method for SCSI layer to detect whether scan is done
James Smarte59058c2008-08-24 21:49:00 -04004425 * @shost: pointer to SCSI host data structure.
4426 * @time: elapsed time of the scan in jiffies.
4427 *
4428 * This routine is called by the SCSI layer with a SCSI host to determine
4429 * whether the scan host is finished.
4430 *
4431 * Note: there is no scan_start function as adapter initialization will have
4432 * asynchronously kicked off the link initialization.
4433 *
4434 * Return codes
4435 * 0 - SCSI host scan is not over yet.
4436 * 1 - SCSI host scan is over.
4437 **/
James Smart47a86172007-04-25 09:53:22 -04004438int lpfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
4439{
James Smart2e0fef82007-06-17 19:56:36 -05004440 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4441 struct lpfc_hba *phba = vport->phba;
James Smart858c9f62007-06-17 19:56:39 -05004442 int stat = 0;
James Smart47a86172007-04-25 09:53:22 -04004443
James Smart858c9f62007-06-17 19:56:39 -05004444 spin_lock_irq(shost->host_lock);
4445
James Smart51ef4c22007-08-02 11:10:31 -04004446 if (vport->load_flag & FC_UNLOADING) {
James Smart858c9f62007-06-17 19:56:39 -05004447 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04004448 goto finished;
James Smart858c9f62007-06-17 19:56:39 -05004449 }
James Smart256ec0d2013-04-17 20:14:58 -04004450 if (time >= msecs_to_jiffies(30 * 1000)) {
James Smart2e0fef82007-06-17 19:56:36 -05004451 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004452 "0461 Scanning longer than 30 "
4453 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05004454 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04004455 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05004456 }
James Smart256ec0d2013-04-17 20:14:58 -04004457 if (time >= msecs_to_jiffies(15 * 1000) &&
4458 phba->link_state <= LPFC_LINK_DOWN) {
James Smart2e0fef82007-06-17 19:56:36 -05004459 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004460 "0465 Link down longer than 15 "
4461 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05004462 stat = 1;
James Smart2e0fef82007-06-17 19:56:36 -05004463 goto finished;
James Smart47a86172007-04-25 09:53:22 -04004464 }
4465
James Smart2e0fef82007-06-17 19:56:36 -05004466 if (vport->port_state != LPFC_VPORT_READY)
James Smart858c9f62007-06-17 19:56:39 -05004467 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05004468 if (vport->num_disc_nodes || vport->fc_prli_sent)
James Smart858c9f62007-06-17 19:56:39 -05004469 goto finished;
James Smart256ec0d2013-04-17 20:14:58 -04004470 if (vport->fc_map_cnt == 0 && time < msecs_to_jiffies(2 * 1000))
James Smart858c9f62007-06-17 19:56:39 -05004471 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05004472 if ((phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) != 0)
James Smart858c9f62007-06-17 19:56:39 -05004473 goto finished;
4474
4475 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04004476
4477finished:
James Smart858c9f62007-06-17 19:56:39 -05004478 spin_unlock_irq(shost->host_lock);
4479 return stat;
James Smart92d7f7b2007-06-17 19:56:38 -05004480}
4481
Bart Van Assche3999df72019-03-28 11:06:16 -07004482static void lpfc_host_supported_speeds_set(struct Scsi_Host *shost)
James Smartcd713482018-10-23 13:41:01 -07004483{
4484 struct lpfc_vport *vport = (struct lpfc_vport *)shost->hostdata;
4485 struct lpfc_hba *phba = vport->phba;
4486
4487 fc_host_supported_speeds(shost) = 0;
James Smart1dc5ec22018-10-23 13:41:11 -07004488 if (phba->lmt & LMT_128Gb)
4489 fc_host_supported_speeds(shost) |= FC_PORTSPEED_128GBIT;
James Smartcd713482018-10-23 13:41:01 -07004490 if (phba->lmt & LMT_64Gb)
4491 fc_host_supported_speeds(shost) |= FC_PORTSPEED_64GBIT;
4492 if (phba->lmt & LMT_32Gb)
4493 fc_host_supported_speeds(shost) |= FC_PORTSPEED_32GBIT;
4494 if (phba->lmt & LMT_16Gb)
4495 fc_host_supported_speeds(shost) |= FC_PORTSPEED_16GBIT;
4496 if (phba->lmt & LMT_10Gb)
4497 fc_host_supported_speeds(shost) |= FC_PORTSPEED_10GBIT;
4498 if (phba->lmt & LMT_8Gb)
4499 fc_host_supported_speeds(shost) |= FC_PORTSPEED_8GBIT;
4500 if (phba->lmt & LMT_4Gb)
4501 fc_host_supported_speeds(shost) |= FC_PORTSPEED_4GBIT;
4502 if (phba->lmt & LMT_2Gb)
4503 fc_host_supported_speeds(shost) |= FC_PORTSPEED_2GBIT;
4504 if (phba->lmt & LMT_1Gb)
4505 fc_host_supported_speeds(shost) |= FC_PORTSPEED_1GBIT;
4506}
4507
James Smarte59058c2008-08-24 21:49:00 -04004508/**
James Smart3621a712009-04-06 18:47:14 -04004509 * lpfc_host_attrib_init - Initialize SCSI host attributes on a FC port
James Smarte59058c2008-08-24 21:49:00 -04004510 * @shost: pointer to SCSI host data structure.
4511 *
4512 * This routine initializes a given SCSI host attributes on a FC port. The
4513 * SCSI host can be either on top of a physical port or a virtual port.
4514 **/
James Smart92d7f7b2007-06-17 19:56:38 -05004515void lpfc_host_attrib_init(struct Scsi_Host *shost)
4516{
4517 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4518 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04004519 /*
James Smart2e0fef82007-06-17 19:56:36 -05004520 * Set fixed host attributes. Must done after lpfc_sli_hba_setup().
James Smart47a86172007-04-25 09:53:22 -04004521 */
4522
James Smart2e0fef82007-06-17 19:56:36 -05004523 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
4524 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
James Smart47a86172007-04-25 09:53:22 -04004525 fc_host_supported_classes(shost) = FC_COS_CLASS3;
4526
4527 memset(fc_host_supported_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05004528 sizeof(fc_host_supported_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04004529 fc_host_supported_fc4s(shost)[2] = 1;
4530 fc_host_supported_fc4s(shost)[7] = 1;
4531
James Smart92d7f7b2007-06-17 19:56:38 -05004532 lpfc_vport_symbolic_node_name(vport, fc_host_symbolic_name(shost),
4533 sizeof fc_host_symbolic_name(shost));
James Smart47a86172007-04-25 09:53:22 -04004534
James Smartcd713482018-10-23 13:41:01 -07004535 lpfc_host_supported_speeds_set(shost);
James Smart47a86172007-04-25 09:53:22 -04004536
4537 fc_host_maxframe_size(shost) =
James Smart2e0fef82007-06-17 19:56:36 -05004538 (((uint32_t) vport->fc_sparam.cmn.bbRcvSizeMsb & 0x0F) << 8) |
4539 (uint32_t) vport->fc_sparam.cmn.bbRcvSizeLsb;
James Smart47a86172007-04-25 09:53:22 -04004540
Mike Christie0af5d702010-09-15 16:52:31 -05004541 fc_host_dev_loss_tmo(shost) = vport->cfg_devloss_tmo;
4542
James Smart47a86172007-04-25 09:53:22 -04004543 /* This value is also unchanging */
4544 memset(fc_host_active_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05004545 sizeof(fc_host_active_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04004546 fc_host_active_fc4s(shost)[2] = 1;
4547 fc_host_active_fc4s(shost)[7] = 1;
4548
James Smart92d7f7b2007-06-17 19:56:38 -05004549 fc_host_max_npiv_vports(shost) = phba->max_vpi;
James Smart47a86172007-04-25 09:53:22 -04004550 spin_lock_irq(shost->host_lock);
James Smart51ef4c22007-08-02 11:10:31 -04004551 vport->load_flag &= ~FC_LOADING;
James Smart47a86172007-04-25 09:53:22 -04004552 spin_unlock_irq(shost->host_lock);
James Smart47a86172007-04-25 09:53:22 -04004553}
dea31012005-04-17 16:05:31 -05004554
James Smarte59058c2008-08-24 21:49:00 -04004555/**
James Smartda0436e2009-05-22 14:51:39 -04004556 * lpfc_stop_port_s3 - Stop SLI3 device port
James Smarte59058c2008-08-24 21:49:00 -04004557 * @phba: pointer to lpfc hba data structure.
4558 *
James Smartda0436e2009-05-22 14:51:39 -04004559 * This routine is invoked to stop an SLI3 device port, it stops the device
4560 * from generating interrupts and stops the device driver's timers for the
4561 * device.
James Smarte59058c2008-08-24 21:49:00 -04004562 **/
James Smartdb2378e2008-02-08 18:49:51 -05004563static void
James Smartda0436e2009-05-22 14:51:39 -04004564lpfc_stop_port_s3(struct lpfc_hba *phba)
James Smartdb2378e2008-02-08 18:49:51 -05004565{
James Smartda0436e2009-05-22 14:51:39 -04004566 /* Clear all interrupt enable conditions */
4567 writel(0, phba->HCregaddr);
4568 readl(phba->HCregaddr); /* flush */
4569 /* Clear all pending interrupts */
4570 writel(0xffffffff, phba->HAregaddr);
4571 readl(phba->HAregaddr); /* flush */
James Smart93996272008-08-24 21:50:30 -04004572
James Smartda0436e2009-05-22 14:51:39 -04004573 /* Reset some HBA SLI setup states */
4574 lpfc_stop_hba_timers(phba);
4575 phba->pport->work_port_events = 0;
James Smartdb2378e2008-02-08 18:49:51 -05004576}
4577
James Smarte59058c2008-08-24 21:49:00 -04004578/**
James Smartda0436e2009-05-22 14:51:39 -04004579 * lpfc_stop_port_s4 - Stop SLI4 device port
James Smart5b75da22008-12-04 22:39:35 -05004580 * @phba: pointer to lpfc hba data structure.
4581 *
James Smartda0436e2009-05-22 14:51:39 -04004582 * This routine is invoked to stop an SLI4 device port, it stops the device
4583 * from generating interrupts and stops the device driver's timers for the
4584 * device.
4585 **/
4586static void
4587lpfc_stop_port_s4(struct lpfc_hba *phba)
4588{
4589 /* Reset some HBA SLI4 setup states */
4590 lpfc_stop_hba_timers(phba);
James Smartcdb42be2019-01-28 11:14:21 -08004591 if (phba->pport)
4592 phba->pport->work_port_events = 0;
James Smartda0436e2009-05-22 14:51:39 -04004593 phba->sli4_hba.intr_enable = 0;
James Smartda0436e2009-05-22 14:51:39 -04004594}
4595
4596/**
4597 * lpfc_stop_port - Wrapper function for stopping hba port
4598 * @phba: Pointer to HBA context object.
James Smart5b75da22008-12-04 22:39:35 -05004599 *
James Smartda0436e2009-05-22 14:51:39 -04004600 * This routine wraps the actual SLI3 or SLI4 hba stop port routine from
4601 * the API jump table function pointer from the lpfc_hba struct.
4602 **/
4603void
4604lpfc_stop_port(struct lpfc_hba *phba)
4605{
4606 phba->lpfc_stop_port(phba);
Dick Kennedyf485c182017-09-29 17:34:34 -07004607
4608 if (phba->wq)
4609 flush_workqueue(phba->wq);
James Smartda0436e2009-05-22 14:51:39 -04004610}
4611
4612/**
James Smartecfd03c2010-02-12 14:41:27 -05004613 * lpfc_fcf_redisc_wait_start_timer - Start fcf rediscover wait timer
4614 * @phba: Pointer to hba for which this call is being executed.
4615 *
4616 * This routine starts the timer waiting for the FCF rediscovery to complete.
4617 **/
4618void
4619lpfc_fcf_redisc_wait_start_timer(struct lpfc_hba *phba)
4620{
4621 unsigned long fcf_redisc_wait_tmo =
4622 (jiffies + msecs_to_jiffies(LPFC_FCF_REDISCOVER_WAIT_TMO));
4623 /* Start fcf rediscovery wait period timer */
4624 mod_timer(&phba->fcf.redisc_wait, fcf_redisc_wait_tmo);
4625 spin_lock_irq(&phba->hbalock);
4626 /* Allow action to new fcf asynchronous event */
4627 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
4628 /* Mark the FCF rediscovery pending state */
4629 phba->fcf.fcf_flag |= FCF_REDISC_PEND;
4630 spin_unlock_irq(&phba->hbalock);
4631}
4632
4633/**
4634 * lpfc_sli4_fcf_redisc_wait_tmo - FCF table rediscover wait timeout
4635 * @ptr: Map to lpfc_hba data structure pointer.
4636 *
4637 * This routine is invoked when waiting for FCF table rediscover has been
4638 * timed out. If new FCF record(s) has (have) been discovered during the
4639 * wait period, a new FCF event shall be added to the FCOE async event
4640 * list, and then worker thread shall be waked up for processing from the
4641 * worker thread context.
4642 **/
Rashika Kheriae399b222014-09-03 12:55:28 -04004643static void
Kees Cookf22eb4d2017-09-06 20:24:26 -07004644lpfc_sli4_fcf_redisc_wait_tmo(struct timer_list *t)
James Smartecfd03c2010-02-12 14:41:27 -05004645{
Kees Cookf22eb4d2017-09-06 20:24:26 -07004646 struct lpfc_hba *phba = from_timer(phba, t, fcf.redisc_wait);
James Smartecfd03c2010-02-12 14:41:27 -05004647
4648 /* Don't send FCF rediscovery event if timer cancelled */
4649 spin_lock_irq(&phba->hbalock);
4650 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
4651 spin_unlock_irq(&phba->hbalock);
4652 return;
4653 }
4654 /* Clear FCF rediscovery timer pending flag */
4655 phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
4656 /* FCF rediscovery event to worker thread */
4657 phba->fcf.fcf_flag |= FCF_REDISC_EVT;
4658 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004659 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04004660 "2776 FCF rediscover quiescent timer expired\n");
James Smartecfd03c2010-02-12 14:41:27 -05004661 /* wake up worker thread */
4662 lpfc_worker_wake_up(phba);
4663}
4664
4665/**
James Smartda0436e2009-05-22 14:51:39 -04004666 * lpfc_sli4_parse_latt_fault - Parse sli4 link-attention link fault code
4667 * @phba: pointer to lpfc hba data structure.
4668 * @acqe_link: pointer to the async link completion queue entry.
4669 *
James Smart23288b72018-05-04 20:37:53 -07004670 * This routine is to parse the SLI4 link-attention link fault code.
James Smartda0436e2009-05-22 14:51:39 -04004671 **/
James Smart23288b72018-05-04 20:37:53 -07004672static void
James Smartda0436e2009-05-22 14:51:39 -04004673lpfc_sli4_parse_latt_fault(struct lpfc_hba *phba,
4674 struct lpfc_acqe_link *acqe_link)
4675{
James Smartda0436e2009-05-22 14:51:39 -04004676 switch (bf_get(lpfc_acqe_link_fault, acqe_link)) {
4677 case LPFC_ASYNC_LINK_FAULT_NONE:
4678 case LPFC_ASYNC_LINK_FAULT_LOCAL:
4679 case LPFC_ASYNC_LINK_FAULT_REMOTE:
James Smart23288b72018-05-04 20:37:53 -07004680 case LPFC_ASYNC_LINK_FAULT_LR_LRR:
James Smartda0436e2009-05-22 14:51:39 -04004681 break;
4682 default:
4683 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart23288b72018-05-04 20:37:53 -07004684 "0398 Unknown link fault code: x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04004685 bf_get(lpfc_acqe_link_fault, acqe_link));
James Smartda0436e2009-05-22 14:51:39 -04004686 break;
4687 }
James Smartda0436e2009-05-22 14:51:39 -04004688}
4689
4690/**
4691 * lpfc_sli4_parse_latt_type - Parse sli4 link attention type
4692 * @phba: pointer to lpfc hba data structure.
4693 * @acqe_link: pointer to the async link completion queue entry.
4694 *
4695 * This routine is to parse the SLI4 link attention type and translate it
4696 * into the base driver's link attention type coding.
4697 *
4698 * Return: Link attention type in terms of base driver's coding.
4699 **/
4700static uint8_t
4701lpfc_sli4_parse_latt_type(struct lpfc_hba *phba,
4702 struct lpfc_acqe_link *acqe_link)
4703{
4704 uint8_t att_type;
4705
4706 switch (bf_get(lpfc_acqe_link_status, acqe_link)) {
4707 case LPFC_ASYNC_LINK_STATUS_DOWN:
4708 case LPFC_ASYNC_LINK_STATUS_LOGICAL_DOWN:
James Smart76a95d72010-11-20 23:11:48 -05004709 att_type = LPFC_ATT_LINK_DOWN;
James Smartda0436e2009-05-22 14:51:39 -04004710 break;
4711 case LPFC_ASYNC_LINK_STATUS_UP:
4712 /* Ignore physical link up events - wait for logical link up */
James Smart76a95d72010-11-20 23:11:48 -05004713 att_type = LPFC_ATT_RESERVED;
James Smartda0436e2009-05-22 14:51:39 -04004714 break;
4715 case LPFC_ASYNC_LINK_STATUS_LOGICAL_UP:
James Smart76a95d72010-11-20 23:11:48 -05004716 att_type = LPFC_ATT_LINK_UP;
James Smartda0436e2009-05-22 14:51:39 -04004717 break;
4718 default:
4719 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4720 "0399 Invalid link attention type: x%x\n",
4721 bf_get(lpfc_acqe_link_status, acqe_link));
James Smart76a95d72010-11-20 23:11:48 -05004722 att_type = LPFC_ATT_RESERVED;
James Smartda0436e2009-05-22 14:51:39 -04004723 break;
4724 }
4725 return att_type;
4726}
4727
4728/**
James Smart8b68cd52012-09-29 11:32:37 -04004729 * lpfc_sli_port_speed_get - Get sli3 link speed code to link speed
4730 * @phba: pointer to lpfc hba data structure.
4731 *
4732 * This routine is to get an SLI3 FC port's link speed in Mbps.
4733 *
4734 * Return: link speed in terms of Mbps.
4735 **/
4736uint32_t
4737lpfc_sli_port_speed_get(struct lpfc_hba *phba)
4738{
4739 uint32_t link_speed;
4740
4741 if (!lpfc_is_link_up(phba))
4742 return 0;
4743
James Smarta085e872015-12-16 18:12:02 -05004744 if (phba->sli_rev <= LPFC_SLI_REV3) {
4745 switch (phba->fc_linkspeed) {
4746 case LPFC_LINK_SPEED_1GHZ:
4747 link_speed = 1000;
4748 break;
4749 case LPFC_LINK_SPEED_2GHZ:
4750 link_speed = 2000;
4751 break;
4752 case LPFC_LINK_SPEED_4GHZ:
4753 link_speed = 4000;
4754 break;
4755 case LPFC_LINK_SPEED_8GHZ:
4756 link_speed = 8000;
4757 break;
4758 case LPFC_LINK_SPEED_10GHZ:
4759 link_speed = 10000;
4760 break;
4761 case LPFC_LINK_SPEED_16GHZ:
4762 link_speed = 16000;
4763 break;
4764 default:
4765 link_speed = 0;
4766 }
4767 } else {
4768 if (phba->sli4_hba.link_state.logical_speed)
4769 link_speed =
4770 phba->sli4_hba.link_state.logical_speed;
4771 else
4772 link_speed = phba->sli4_hba.link_state.speed;
James Smart8b68cd52012-09-29 11:32:37 -04004773 }
4774 return link_speed;
4775}
4776
4777/**
4778 * lpfc_sli4_port_speed_parse - Parse async evt link speed code to link speed
4779 * @phba: pointer to lpfc hba data structure.
4780 * @evt_code: asynchronous event code.
4781 * @speed_code: asynchronous event link speed code.
4782 *
4783 * This routine is to parse the giving SLI4 async event link speed code into
4784 * value of Mbps for the link speed.
4785 *
4786 * Return: link speed in terms of Mbps.
4787 **/
4788static uint32_t
4789lpfc_sli4_port_speed_parse(struct lpfc_hba *phba, uint32_t evt_code,
4790 uint8_t speed_code)
4791{
4792 uint32_t port_speed;
4793
4794 switch (evt_code) {
4795 case LPFC_TRAILER_CODE_LINK:
4796 switch (speed_code) {
James Smart26d830e2015-04-07 15:07:17 -04004797 case LPFC_ASYNC_LINK_SPEED_ZERO:
James Smart8b68cd52012-09-29 11:32:37 -04004798 port_speed = 0;
4799 break;
James Smart26d830e2015-04-07 15:07:17 -04004800 case LPFC_ASYNC_LINK_SPEED_10MBPS:
James Smart8b68cd52012-09-29 11:32:37 -04004801 port_speed = 10;
4802 break;
James Smart26d830e2015-04-07 15:07:17 -04004803 case LPFC_ASYNC_LINK_SPEED_100MBPS:
James Smart8b68cd52012-09-29 11:32:37 -04004804 port_speed = 100;
4805 break;
James Smart26d830e2015-04-07 15:07:17 -04004806 case LPFC_ASYNC_LINK_SPEED_1GBPS:
James Smart8b68cd52012-09-29 11:32:37 -04004807 port_speed = 1000;
4808 break;
James Smart26d830e2015-04-07 15:07:17 -04004809 case LPFC_ASYNC_LINK_SPEED_10GBPS:
James Smart8b68cd52012-09-29 11:32:37 -04004810 port_speed = 10000;
4811 break;
James Smart26d830e2015-04-07 15:07:17 -04004812 case LPFC_ASYNC_LINK_SPEED_20GBPS:
4813 port_speed = 20000;
4814 break;
4815 case LPFC_ASYNC_LINK_SPEED_25GBPS:
4816 port_speed = 25000;
4817 break;
4818 case LPFC_ASYNC_LINK_SPEED_40GBPS:
4819 port_speed = 40000;
4820 break;
James Smart8b68cd52012-09-29 11:32:37 -04004821 default:
4822 port_speed = 0;
4823 }
4824 break;
4825 case LPFC_TRAILER_CODE_FC:
4826 switch (speed_code) {
James Smart26d830e2015-04-07 15:07:17 -04004827 case LPFC_FC_LA_SPEED_UNKNOWN:
James Smart8b68cd52012-09-29 11:32:37 -04004828 port_speed = 0;
4829 break;
James Smart26d830e2015-04-07 15:07:17 -04004830 case LPFC_FC_LA_SPEED_1G:
James Smart8b68cd52012-09-29 11:32:37 -04004831 port_speed = 1000;
4832 break;
James Smart26d830e2015-04-07 15:07:17 -04004833 case LPFC_FC_LA_SPEED_2G:
James Smart8b68cd52012-09-29 11:32:37 -04004834 port_speed = 2000;
4835 break;
James Smart26d830e2015-04-07 15:07:17 -04004836 case LPFC_FC_LA_SPEED_4G:
James Smart8b68cd52012-09-29 11:32:37 -04004837 port_speed = 4000;
4838 break;
James Smart26d830e2015-04-07 15:07:17 -04004839 case LPFC_FC_LA_SPEED_8G:
James Smart8b68cd52012-09-29 11:32:37 -04004840 port_speed = 8000;
4841 break;
James Smart26d830e2015-04-07 15:07:17 -04004842 case LPFC_FC_LA_SPEED_10G:
James Smart8b68cd52012-09-29 11:32:37 -04004843 port_speed = 10000;
4844 break;
James Smart26d830e2015-04-07 15:07:17 -04004845 case LPFC_FC_LA_SPEED_16G:
James Smart8b68cd52012-09-29 11:32:37 -04004846 port_speed = 16000;
4847 break;
James Smartd38dd522015-08-31 16:48:17 -04004848 case LPFC_FC_LA_SPEED_32G:
4849 port_speed = 32000;
4850 break;
James Smartfbd8a6b2018-02-22 08:18:45 -08004851 case LPFC_FC_LA_SPEED_64G:
4852 port_speed = 64000;
4853 break;
James Smart1dc5ec22018-10-23 13:41:11 -07004854 case LPFC_FC_LA_SPEED_128G:
4855 port_speed = 128000;
4856 break;
James Smart8b68cd52012-09-29 11:32:37 -04004857 default:
4858 port_speed = 0;
4859 }
4860 break;
4861 default:
4862 port_speed = 0;
4863 }
4864 return port_speed;
4865}
4866
4867/**
James Smart70f3c072010-12-15 17:57:33 -05004868 * lpfc_sli4_async_link_evt - Process the asynchronous FCoE link event
James Smartda0436e2009-05-22 14:51:39 -04004869 * @phba: pointer to lpfc hba data structure.
4870 * @acqe_link: pointer to the async link completion queue entry.
4871 *
James Smart70f3c072010-12-15 17:57:33 -05004872 * This routine is to handle the SLI4 asynchronous FCoE link event.
James Smartda0436e2009-05-22 14:51:39 -04004873 **/
4874static void
4875lpfc_sli4_async_link_evt(struct lpfc_hba *phba,
4876 struct lpfc_acqe_link *acqe_link)
4877{
4878 struct lpfc_dmabuf *mp;
4879 LPFC_MBOXQ_t *pmb;
4880 MAILBOX_t *mb;
James Smart76a95d72010-11-20 23:11:48 -05004881 struct lpfc_mbx_read_top *la;
James Smartda0436e2009-05-22 14:51:39 -04004882 uint8_t att_type;
James Smart76a95d72010-11-20 23:11:48 -05004883 int rc;
James Smartda0436e2009-05-22 14:51:39 -04004884
4885 att_type = lpfc_sli4_parse_latt_type(phba, acqe_link);
James Smart76a95d72010-11-20 23:11:48 -05004886 if (att_type != LPFC_ATT_LINK_DOWN && att_type != LPFC_ATT_LINK_UP)
James Smartda0436e2009-05-22 14:51:39 -04004887 return;
James Smart32b97932009-07-19 10:01:21 -04004888 phba->fcoe_eventtag = acqe_link->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04004889 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4890 if (!pmb) {
4891 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4892 "0395 The mboxq allocation failed\n");
4893 return;
4894 }
4895 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4896 if (!mp) {
4897 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4898 "0396 The lpfc_dmabuf allocation failed\n");
4899 goto out_free_pmb;
4900 }
4901 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
4902 if (!mp->virt) {
4903 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4904 "0397 The mbuf allocation failed\n");
4905 goto out_free_dmabuf;
4906 }
4907
4908 /* Cleanup any outstanding ELS commands */
4909 lpfc_els_flush_all_cmd(phba);
4910
4911 /* Block ELS IOCBs until we have done process link event */
James Smart895427b2017-02-12 13:52:30 -08004912 phba->sli4_hba.els_wq->pring->flag |= LPFC_STOP_IOCB_EVENT;
James Smartda0436e2009-05-22 14:51:39 -04004913
4914 /* Update link event statistics */
4915 phba->sli.slistat.link_event++;
4916
James Smart76a95d72010-11-20 23:11:48 -05004917 /* Create lpfc_handle_latt mailbox command from link ACQE */
4918 lpfc_read_topology(phba, pmb, mp);
4919 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
James Smartda0436e2009-05-22 14:51:39 -04004920 pmb->vport = phba->pport;
4921
James Smartda0436e2009-05-22 14:51:39 -04004922 /* Keep the link status for extra SLI4 state machine reference */
4923 phba->sli4_hba.link_state.speed =
James Smart8b68cd52012-09-29 11:32:37 -04004924 lpfc_sli4_port_speed_parse(phba, LPFC_TRAILER_CODE_LINK,
4925 bf_get(lpfc_acqe_link_speed, acqe_link));
James Smartda0436e2009-05-22 14:51:39 -04004926 phba->sli4_hba.link_state.duplex =
4927 bf_get(lpfc_acqe_link_duplex, acqe_link);
4928 phba->sli4_hba.link_state.status =
4929 bf_get(lpfc_acqe_link_status, acqe_link);
James Smart70f3c072010-12-15 17:57:33 -05004930 phba->sli4_hba.link_state.type =
4931 bf_get(lpfc_acqe_link_type, acqe_link);
4932 phba->sli4_hba.link_state.number =
4933 bf_get(lpfc_acqe_link_number, acqe_link);
James Smartda0436e2009-05-22 14:51:39 -04004934 phba->sli4_hba.link_state.fault =
4935 bf_get(lpfc_acqe_link_fault, acqe_link);
James Smart65467b62010-01-26 23:08:29 -05004936 phba->sli4_hba.link_state.logical_speed =
James Smart8b68cd52012-09-29 11:32:37 -04004937 bf_get(lpfc_acqe_logical_link_speed, acqe_link) * 10;
4938
James Smart70f3c072010-12-15 17:57:33 -05004939 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartc31098c2011-04-16 11:03:33 -04004940 "2900 Async FC/FCoE Link event - Speed:%dGBit "
4941 "duplex:x%x LA Type:x%x Port Type:%d Port Number:%d "
4942 "Logical speed:%dMbps Fault:%d\n",
James Smart70f3c072010-12-15 17:57:33 -05004943 phba->sli4_hba.link_state.speed,
4944 phba->sli4_hba.link_state.topology,
4945 phba->sli4_hba.link_state.status,
4946 phba->sli4_hba.link_state.type,
4947 phba->sli4_hba.link_state.number,
James Smart8b68cd52012-09-29 11:32:37 -04004948 phba->sli4_hba.link_state.logical_speed,
James Smart70f3c072010-12-15 17:57:33 -05004949 phba->sli4_hba.link_state.fault);
James Smart76a95d72010-11-20 23:11:48 -05004950 /*
4951 * For FC Mode: issue the READ_TOPOLOGY mailbox command to fetch
4952 * topology info. Note: Optional for non FC-AL ports.
4953 */
4954 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
4955 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
4956 if (rc == MBX_NOT_FINISHED)
4957 goto out_free_dmabuf;
4958 return;
4959 }
4960 /*
4961 * For FCoE Mode: fill in all the topology information we need and call
4962 * the READ_TOPOLOGY completion routine to continue without actually
4963 * sending the READ_TOPOLOGY mailbox command to the port.
4964 */
James Smart23288b72018-05-04 20:37:53 -07004965 /* Initialize completion status */
James Smart76a95d72010-11-20 23:11:48 -05004966 mb = &pmb->u.mb;
James Smart23288b72018-05-04 20:37:53 -07004967 mb->mbxStatus = MBX_SUCCESS;
4968
4969 /* Parse port fault information field */
4970 lpfc_sli4_parse_latt_fault(phba, acqe_link);
James Smart76a95d72010-11-20 23:11:48 -05004971
4972 /* Parse and translate link attention fields */
4973 la = (struct lpfc_mbx_read_top *) &pmb->u.mb.un.varReadTop;
4974 la->eventTag = acqe_link->event_tag;
4975 bf_set(lpfc_mbx_read_top_att_type, la, att_type);
4976 bf_set(lpfc_mbx_read_top_link_spd, la,
James Smarta085e872015-12-16 18:12:02 -05004977 (bf_get(lpfc_acqe_link_speed, acqe_link)));
James Smart76a95d72010-11-20 23:11:48 -05004978
4979 /* Fake the the following irrelvant fields */
4980 bf_set(lpfc_mbx_read_top_topology, la, LPFC_TOPOLOGY_PT_PT);
4981 bf_set(lpfc_mbx_read_top_alpa_granted, la, 0);
4982 bf_set(lpfc_mbx_read_top_il, la, 0);
4983 bf_set(lpfc_mbx_read_top_pb, la, 0);
4984 bf_set(lpfc_mbx_read_top_fa, la, 0);
4985 bf_set(lpfc_mbx_read_top_mm, la, 0);
James Smartda0436e2009-05-22 14:51:39 -04004986
4987 /* Invoke the lpfc_handle_latt mailbox command callback function */
James Smart76a95d72010-11-20 23:11:48 -05004988 lpfc_mbx_cmpl_read_topology(phba, pmb);
James Smartda0436e2009-05-22 14:51:39 -04004989
4990 return;
4991
4992out_free_dmabuf:
4993 kfree(mp);
4994out_free_pmb:
4995 mempool_free(pmb, phba->mbox_mem_pool);
4996}
4997
4998/**
James Smart1dc5ec22018-10-23 13:41:11 -07004999 * lpfc_async_link_speed_to_read_top - Parse async evt link speed code to read
5000 * topology.
5001 * @phba: pointer to lpfc hba data structure.
5002 * @evt_code: asynchronous event code.
5003 * @speed_code: asynchronous event link speed code.
5004 *
5005 * This routine is to parse the giving SLI4 async event link speed code into
5006 * value of Read topology link speed.
5007 *
5008 * Return: link speed in terms of Read topology.
5009 **/
5010static uint8_t
5011lpfc_async_link_speed_to_read_top(struct lpfc_hba *phba, uint8_t speed_code)
5012{
5013 uint8_t port_speed;
5014
5015 switch (speed_code) {
5016 case LPFC_FC_LA_SPEED_1G:
5017 port_speed = LPFC_LINK_SPEED_1GHZ;
5018 break;
5019 case LPFC_FC_LA_SPEED_2G:
5020 port_speed = LPFC_LINK_SPEED_2GHZ;
5021 break;
5022 case LPFC_FC_LA_SPEED_4G:
5023 port_speed = LPFC_LINK_SPEED_4GHZ;
5024 break;
5025 case LPFC_FC_LA_SPEED_8G:
5026 port_speed = LPFC_LINK_SPEED_8GHZ;
5027 break;
5028 case LPFC_FC_LA_SPEED_16G:
5029 port_speed = LPFC_LINK_SPEED_16GHZ;
5030 break;
5031 case LPFC_FC_LA_SPEED_32G:
5032 port_speed = LPFC_LINK_SPEED_32GHZ;
5033 break;
5034 case LPFC_FC_LA_SPEED_64G:
5035 port_speed = LPFC_LINK_SPEED_64GHZ;
5036 break;
5037 case LPFC_FC_LA_SPEED_128G:
5038 port_speed = LPFC_LINK_SPEED_128GHZ;
5039 break;
5040 case LPFC_FC_LA_SPEED_256G:
5041 port_speed = LPFC_LINK_SPEED_256GHZ;
5042 break;
5043 default:
5044 port_speed = 0;
5045 break;
5046 }
5047
5048 return port_speed;
5049}
5050
5051#define trunk_link_status(__idx)\
5052 bf_get(lpfc_acqe_fc_la_trunk_config_port##__idx, acqe_fc) ?\
5053 ((phba->trunk_link.link##__idx.state == LPFC_LINK_UP) ?\
5054 "Link up" : "Link down") : "NA"
5055/* Did port __idx reported an error */
5056#define trunk_port_fault(__idx)\
5057 bf_get(lpfc_acqe_fc_la_trunk_config_port##__idx, acqe_fc) ?\
5058 (port_fault & (1 << __idx) ? "YES" : "NO") : "NA"
5059
5060static void
5061lpfc_update_trunk_link_status(struct lpfc_hba *phba,
5062 struct lpfc_acqe_fc_la *acqe_fc)
5063{
5064 uint8_t port_fault = bf_get(lpfc_acqe_fc_la_trunk_linkmask, acqe_fc);
5065 uint8_t err = bf_get(lpfc_acqe_fc_la_trunk_fault, acqe_fc);
5066
5067 phba->sli4_hba.link_state.speed =
5068 lpfc_sli4_port_speed_parse(phba, LPFC_TRAILER_CODE_FC,
5069 bf_get(lpfc_acqe_fc_la_speed, acqe_fc));
5070
5071 phba->sli4_hba.link_state.logical_speed =
James Smartb8e6f132019-05-21 17:49:04 -07005072 bf_get(lpfc_acqe_fc_la_llink_spd, acqe_fc) * 10;
James Smart1dc5ec22018-10-23 13:41:11 -07005073 /* We got FC link speed, convert to fc_linkspeed (READ_TOPOLOGY) */
5074 phba->fc_linkspeed =
5075 lpfc_async_link_speed_to_read_top(
5076 phba,
5077 bf_get(lpfc_acqe_fc_la_speed, acqe_fc));
5078
5079 if (bf_get(lpfc_acqe_fc_la_trunk_config_port0, acqe_fc)) {
5080 phba->trunk_link.link0.state =
5081 bf_get(lpfc_acqe_fc_la_trunk_link_status_port0, acqe_fc)
5082 ? LPFC_LINK_UP : LPFC_LINK_DOWN;
James Smart529b3dd2018-12-13 15:17:54 -08005083 phba->trunk_link.link0.fault = port_fault & 0x1 ? err : 0;
James Smart1dc5ec22018-10-23 13:41:11 -07005084 }
5085 if (bf_get(lpfc_acqe_fc_la_trunk_config_port1, acqe_fc)) {
5086 phba->trunk_link.link1.state =
5087 bf_get(lpfc_acqe_fc_la_trunk_link_status_port1, acqe_fc)
5088 ? LPFC_LINK_UP : LPFC_LINK_DOWN;
James Smart529b3dd2018-12-13 15:17:54 -08005089 phba->trunk_link.link1.fault = port_fault & 0x2 ? err : 0;
James Smart1dc5ec22018-10-23 13:41:11 -07005090 }
5091 if (bf_get(lpfc_acqe_fc_la_trunk_config_port2, acqe_fc)) {
5092 phba->trunk_link.link2.state =
5093 bf_get(lpfc_acqe_fc_la_trunk_link_status_port2, acqe_fc)
5094 ? LPFC_LINK_UP : LPFC_LINK_DOWN;
James Smart529b3dd2018-12-13 15:17:54 -08005095 phba->trunk_link.link2.fault = port_fault & 0x4 ? err : 0;
James Smart1dc5ec22018-10-23 13:41:11 -07005096 }
5097 if (bf_get(lpfc_acqe_fc_la_trunk_config_port3, acqe_fc)) {
5098 phba->trunk_link.link3.state =
5099 bf_get(lpfc_acqe_fc_la_trunk_link_status_port3, acqe_fc)
5100 ? LPFC_LINK_UP : LPFC_LINK_DOWN;
James Smart529b3dd2018-12-13 15:17:54 -08005101 phba->trunk_link.link3.fault = port_fault & 0x8 ? err : 0;
James Smart1dc5ec22018-10-23 13:41:11 -07005102 }
5103
5104 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5105 "2910 Async FC Trunking Event - Speed:%d\n"
5106 "\tLogical speed:%d "
5107 "port0: %s port1: %s port2: %s port3: %s\n",
5108 phba->sli4_hba.link_state.speed,
5109 phba->sli4_hba.link_state.logical_speed,
5110 trunk_link_status(0), trunk_link_status(1),
5111 trunk_link_status(2), trunk_link_status(3));
5112
5113 if (port_fault)
5114 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5115 "3202 trunk error:0x%x (%s) seen on port0:%s "
5116 /*
5117 * SLI-4: We have only 0xA error codes
5118 * defined as of now. print an appropriate
5119 * message in case driver needs to be updated.
5120 */
5121 "port1:%s port2:%s port3:%s\n", err, err > 0xA ?
5122 "UNDEFINED. update driver." : trunk_errmsg[err],
5123 trunk_port_fault(0), trunk_port_fault(1),
5124 trunk_port_fault(2), trunk_port_fault(3));
5125}
5126
5127
5128/**
James Smart70f3c072010-12-15 17:57:33 -05005129 * lpfc_sli4_async_fc_evt - Process the asynchronous FC link event
5130 * @phba: pointer to lpfc hba data structure.
5131 * @acqe_fc: pointer to the async fc completion queue entry.
5132 *
5133 * This routine is to handle the SLI4 asynchronous FC event. It will simply log
5134 * that the event was received and then issue a read_topology mailbox command so
5135 * that the rest of the driver will treat it the same as SLI3.
5136 **/
5137static void
5138lpfc_sli4_async_fc_evt(struct lpfc_hba *phba, struct lpfc_acqe_fc_la *acqe_fc)
5139{
5140 struct lpfc_dmabuf *mp;
5141 LPFC_MBOXQ_t *pmb;
James Smart7bdedb32016-07-06 12:36:00 -07005142 MAILBOX_t *mb;
5143 struct lpfc_mbx_read_top *la;
James Smart70f3c072010-12-15 17:57:33 -05005144 int rc;
5145
5146 if (bf_get(lpfc_trailer_type, acqe_fc) !=
5147 LPFC_FC_LA_EVENT_TYPE_FC_LINK) {
5148 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5149 "2895 Non FC link Event detected.(%d)\n",
5150 bf_get(lpfc_trailer_type, acqe_fc));
5151 return;
5152 }
James Smart1dc5ec22018-10-23 13:41:11 -07005153
5154 if (bf_get(lpfc_acqe_fc_la_att_type, acqe_fc) ==
5155 LPFC_FC_LA_TYPE_TRUNKING_EVENT) {
5156 lpfc_update_trunk_link_status(phba, acqe_fc);
5157 return;
5158 }
5159
James Smart70f3c072010-12-15 17:57:33 -05005160 /* Keep the link status for extra SLI4 state machine reference */
5161 phba->sli4_hba.link_state.speed =
James Smart8b68cd52012-09-29 11:32:37 -04005162 lpfc_sli4_port_speed_parse(phba, LPFC_TRAILER_CODE_FC,
5163 bf_get(lpfc_acqe_fc_la_speed, acqe_fc));
James Smart70f3c072010-12-15 17:57:33 -05005164 phba->sli4_hba.link_state.duplex = LPFC_ASYNC_LINK_DUPLEX_FULL;
5165 phba->sli4_hba.link_state.topology =
5166 bf_get(lpfc_acqe_fc_la_topology, acqe_fc);
5167 phba->sli4_hba.link_state.status =
5168 bf_get(lpfc_acqe_fc_la_att_type, acqe_fc);
5169 phba->sli4_hba.link_state.type =
5170 bf_get(lpfc_acqe_fc_la_port_type, acqe_fc);
5171 phba->sli4_hba.link_state.number =
5172 bf_get(lpfc_acqe_fc_la_port_number, acqe_fc);
5173 phba->sli4_hba.link_state.fault =
5174 bf_get(lpfc_acqe_link_fault, acqe_fc);
James Smartb8e6f132019-05-21 17:49:04 -07005175
5176 if (bf_get(lpfc_acqe_fc_la_att_type, acqe_fc) ==
5177 LPFC_FC_LA_TYPE_LINK_DOWN)
5178 phba->sli4_hba.link_state.logical_speed = 0;
5179 else if (!phba->sli4_hba.conf_trunk)
5180 phba->sli4_hba.link_state.logical_speed =
James Smart8b68cd52012-09-29 11:32:37 -04005181 bf_get(lpfc_acqe_fc_la_llink_spd, acqe_fc) * 10;
James Smartb8e6f132019-05-21 17:49:04 -07005182
James Smart70f3c072010-12-15 17:57:33 -05005183 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5184 "2896 Async FC event - Speed:%dGBaud Topology:x%x "
5185 "LA Type:x%x Port Type:%d Port Number:%d Logical speed:"
5186 "%dMbps Fault:%d\n",
5187 phba->sli4_hba.link_state.speed,
5188 phba->sli4_hba.link_state.topology,
5189 phba->sli4_hba.link_state.status,
5190 phba->sli4_hba.link_state.type,
5191 phba->sli4_hba.link_state.number,
James Smart8b68cd52012-09-29 11:32:37 -04005192 phba->sli4_hba.link_state.logical_speed,
James Smart70f3c072010-12-15 17:57:33 -05005193 phba->sli4_hba.link_state.fault);
5194 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5195 if (!pmb) {
5196 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5197 "2897 The mboxq allocation failed\n");
5198 return;
5199 }
5200 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5201 if (!mp) {
5202 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5203 "2898 The lpfc_dmabuf allocation failed\n");
5204 goto out_free_pmb;
5205 }
5206 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
5207 if (!mp->virt) {
5208 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5209 "2899 The mbuf allocation failed\n");
5210 goto out_free_dmabuf;
5211 }
5212
5213 /* Cleanup any outstanding ELS commands */
5214 lpfc_els_flush_all_cmd(phba);
5215
5216 /* Block ELS IOCBs until we have done process link event */
James Smart895427b2017-02-12 13:52:30 -08005217 phba->sli4_hba.els_wq->pring->flag |= LPFC_STOP_IOCB_EVENT;
James Smart70f3c072010-12-15 17:57:33 -05005218
5219 /* Update link event statistics */
5220 phba->sli.slistat.link_event++;
5221
5222 /* Create lpfc_handle_latt mailbox command from link ACQE */
5223 lpfc_read_topology(phba, pmb, mp);
5224 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
5225 pmb->vport = phba->pport;
5226
James Smart7bdedb32016-07-06 12:36:00 -07005227 if (phba->sli4_hba.link_state.status != LPFC_FC_LA_TYPE_LINK_UP) {
James Smartae9e28f2017-05-15 15:20:51 -07005228 phba->link_flag &= ~(LS_MDS_LINK_DOWN | LS_MDS_LOOPBACK);
5229
5230 switch (phba->sli4_hba.link_state.status) {
5231 case LPFC_FC_LA_TYPE_MDS_LINK_DOWN:
5232 phba->link_flag |= LS_MDS_LINK_DOWN;
5233 break;
5234 case LPFC_FC_LA_TYPE_MDS_LOOPBACK:
5235 phba->link_flag |= LS_MDS_LOOPBACK;
5236 break;
5237 default:
5238 break;
5239 }
5240
James Smart23288b72018-05-04 20:37:53 -07005241 /* Initialize completion status */
James Smart7bdedb32016-07-06 12:36:00 -07005242 mb = &pmb->u.mb;
James Smart23288b72018-05-04 20:37:53 -07005243 mb->mbxStatus = MBX_SUCCESS;
5244
5245 /* Parse port fault information field */
5246 lpfc_sli4_parse_latt_fault(phba, (void *)acqe_fc);
James Smart7bdedb32016-07-06 12:36:00 -07005247
5248 /* Parse and translate link attention fields */
5249 la = (struct lpfc_mbx_read_top *)&pmb->u.mb.un.varReadTop;
5250 la->eventTag = acqe_fc->event_tag;
James Smart7bdedb32016-07-06 12:36:00 -07005251
James Smartaeb3c812017-04-21 16:05:02 -07005252 if (phba->sli4_hba.link_state.status ==
5253 LPFC_FC_LA_TYPE_UNEXP_WWPN) {
5254 bf_set(lpfc_mbx_read_top_att_type, la,
5255 LPFC_FC_LA_TYPE_UNEXP_WWPN);
5256 } else {
5257 bf_set(lpfc_mbx_read_top_att_type, la,
5258 LPFC_FC_LA_TYPE_LINK_DOWN);
5259 }
James Smart7bdedb32016-07-06 12:36:00 -07005260 /* Invoke the mailbox command callback function */
5261 lpfc_mbx_cmpl_read_topology(phba, pmb);
5262
5263 return;
5264 }
5265
James Smart70f3c072010-12-15 17:57:33 -05005266 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
5267 if (rc == MBX_NOT_FINISHED)
5268 goto out_free_dmabuf;
5269 return;
5270
5271out_free_dmabuf:
5272 kfree(mp);
5273out_free_pmb:
5274 mempool_free(pmb, phba->mbox_mem_pool);
5275}
5276
5277/**
5278 * lpfc_sli4_async_sli_evt - Process the asynchronous SLI link event
5279 * @phba: pointer to lpfc hba data structure.
5280 * @acqe_fc: pointer to the async SLI completion queue entry.
5281 *
5282 * This routine is to handle the SLI4 asynchronous SLI events.
5283 **/
5284static void
5285lpfc_sli4_async_sli_evt(struct lpfc_hba *phba, struct lpfc_acqe_sli *acqe_sli)
5286{
James Smart4b8bae02012-06-12 13:55:07 -04005287 char port_name;
James Smart8c1312e2012-10-31 14:45:09 -04005288 char message[128];
James Smart4b8bae02012-06-12 13:55:07 -04005289 uint8_t status;
James Smart946727d2015-04-07 15:07:09 -04005290 uint8_t evt_type;
James Smart448193b2015-12-16 18:12:05 -05005291 uint8_t operational = 0;
James Smart946727d2015-04-07 15:07:09 -04005292 struct temp_event temp_event_data;
James Smart4b8bae02012-06-12 13:55:07 -04005293 struct lpfc_acqe_misconfigured_event *misconfigured;
James Smart946727d2015-04-07 15:07:09 -04005294 struct Scsi_Host *shost;
James Smartcd713482018-10-23 13:41:01 -07005295 struct lpfc_vport **vports;
5296 int rc, i;
James Smart4b8bae02012-06-12 13:55:07 -04005297
James Smart946727d2015-04-07 15:07:09 -04005298 evt_type = bf_get(lpfc_trailer_type, acqe_sli);
5299
James Smart448193b2015-12-16 18:12:05 -05005300 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5301 "2901 Async SLI event - Event Data1:x%08x Event Data2:"
5302 "x%08x SLI Event Type:%d\n",
5303 acqe_sli->event_data1, acqe_sli->event_data2,
5304 evt_type);
James Smart4b8bae02012-06-12 13:55:07 -04005305
5306 port_name = phba->Port[0];
5307 if (port_name == 0x00)
5308 port_name = '?'; /* get port name is empty */
5309
James Smart946727d2015-04-07 15:07:09 -04005310 switch (evt_type) {
5311 case LPFC_SLI_EVENT_TYPE_OVER_TEMP:
5312 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
5313 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
5314 temp_event_data.data = (uint32_t)acqe_sli->event_data1;
5315
5316 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5317 "3190 Over Temperature:%d Celsius- Port Name %c\n",
5318 acqe_sli->event_data1, port_name);
5319
James Smart310429e2016-07-06 12:35:54 -07005320 phba->sfp_warning |= LPFC_TRANSGRESSION_HIGH_TEMPERATURE;
James Smart946727d2015-04-07 15:07:09 -04005321 shost = lpfc_shost_from_vport(phba->pport);
5322 fc_host_post_vendor_event(shost, fc_get_event_number(),
5323 sizeof(temp_event_data),
5324 (char *)&temp_event_data,
5325 SCSI_NL_VID_TYPE_PCI
5326 | PCI_VENDOR_ID_EMULEX);
5327 break;
5328 case LPFC_SLI_EVENT_TYPE_NORM_TEMP:
5329 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
5330 temp_event_data.event_code = LPFC_NORMAL_TEMP;
5331 temp_event_data.data = (uint32_t)acqe_sli->event_data1;
5332
5333 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5334 "3191 Normal Temperature:%d Celsius - Port Name %c\n",
5335 acqe_sli->event_data1, port_name);
5336
5337 shost = lpfc_shost_from_vport(phba->pport);
5338 fc_host_post_vendor_event(shost, fc_get_event_number(),
5339 sizeof(temp_event_data),
5340 (char *)&temp_event_data,
5341 SCSI_NL_VID_TYPE_PCI
5342 | PCI_VENDOR_ID_EMULEX);
5343 break;
5344 case LPFC_SLI_EVENT_TYPE_MISCONFIGURED:
5345 misconfigured = (struct lpfc_acqe_misconfigured_event *)
James Smart4b8bae02012-06-12 13:55:07 -04005346 &acqe_sli->event_data1;
5347
James Smart946727d2015-04-07 15:07:09 -04005348 /* fetch the status for this port */
5349 switch (phba->sli4_hba.lnk_info.lnk_no) {
5350 case LPFC_LINK_NUMBER_0:
James Smart448193b2015-12-16 18:12:05 -05005351 status = bf_get(lpfc_sli_misconfigured_port0_state,
5352 &misconfigured->theEvent);
5353 operational = bf_get(lpfc_sli_misconfigured_port0_op,
James Smart4b8bae02012-06-12 13:55:07 -04005354 &misconfigured->theEvent);
James Smart946727d2015-04-07 15:07:09 -04005355 break;
5356 case LPFC_LINK_NUMBER_1:
James Smart448193b2015-12-16 18:12:05 -05005357 status = bf_get(lpfc_sli_misconfigured_port1_state,
5358 &misconfigured->theEvent);
5359 operational = bf_get(lpfc_sli_misconfigured_port1_op,
James Smart4b8bae02012-06-12 13:55:07 -04005360 &misconfigured->theEvent);
James Smart946727d2015-04-07 15:07:09 -04005361 break;
5362 case LPFC_LINK_NUMBER_2:
James Smart448193b2015-12-16 18:12:05 -05005363 status = bf_get(lpfc_sli_misconfigured_port2_state,
5364 &misconfigured->theEvent);
5365 operational = bf_get(lpfc_sli_misconfigured_port2_op,
James Smart4b8bae02012-06-12 13:55:07 -04005366 &misconfigured->theEvent);
James Smart946727d2015-04-07 15:07:09 -04005367 break;
5368 case LPFC_LINK_NUMBER_3:
James Smart448193b2015-12-16 18:12:05 -05005369 status = bf_get(lpfc_sli_misconfigured_port3_state,
5370 &misconfigured->theEvent);
5371 operational = bf_get(lpfc_sli_misconfigured_port3_op,
James Smart4b8bae02012-06-12 13:55:07 -04005372 &misconfigured->theEvent);
James Smart946727d2015-04-07 15:07:09 -04005373 break;
5374 default:
James Smart448193b2015-12-16 18:12:05 -05005375 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5376 "3296 "
5377 "LPFC_SLI_EVENT_TYPE_MISCONFIGURED "
5378 "event: Invalid link %d",
5379 phba->sli4_hba.lnk_info.lnk_no);
5380 return;
James Smart946727d2015-04-07 15:07:09 -04005381 }
James Smart4b8bae02012-06-12 13:55:07 -04005382
James Smart448193b2015-12-16 18:12:05 -05005383 /* Skip if optic state unchanged */
5384 if (phba->sli4_hba.lnk_info.optic_state == status)
5385 return;
5386
James Smart946727d2015-04-07 15:07:09 -04005387 switch (status) {
5388 case LPFC_SLI_EVENT_STATUS_VALID:
James Smart448193b2015-12-16 18:12:05 -05005389 sprintf(message, "Physical Link is functional");
5390 break;
James Smart946727d2015-04-07 15:07:09 -04005391 case LPFC_SLI_EVENT_STATUS_NOT_PRESENT:
5392 sprintf(message, "Optics faulted/incorrectly "
5393 "installed/not installed - Reseat optics, "
5394 "if issue not resolved, replace.");
5395 break;
5396 case LPFC_SLI_EVENT_STATUS_WRONG_TYPE:
5397 sprintf(message,
5398 "Optics of two types installed - Remove one "
5399 "optic or install matching pair of optics.");
5400 break;
5401 case LPFC_SLI_EVENT_STATUS_UNSUPPORTED:
5402 sprintf(message, "Incompatible optics - Replace with "
James Smart292098b2012-09-29 11:31:41 -04005403 "compatible optics for card to function.");
James Smart946727d2015-04-07 15:07:09 -04005404 break;
James Smart448193b2015-12-16 18:12:05 -05005405 case LPFC_SLI_EVENT_STATUS_UNQUALIFIED:
5406 sprintf(message, "Unqualified optics - Replace with "
5407 "Avago optics for Warranty and Technical "
5408 "Support - Link is%s operational",
James Smart2ea259e2017-02-12 13:52:27 -08005409 (operational) ? " not" : "");
James Smart448193b2015-12-16 18:12:05 -05005410 break;
5411 case LPFC_SLI_EVENT_STATUS_UNCERTIFIED:
5412 sprintf(message, "Uncertified optics - Replace with "
5413 "Avago-certified optics to enable link "
5414 "operation - Link is%s operational",
James Smart2ea259e2017-02-12 13:52:27 -08005415 (operational) ? " not" : "");
James Smart448193b2015-12-16 18:12:05 -05005416 break;
James Smart946727d2015-04-07 15:07:09 -04005417 default:
5418 /* firmware is reporting a status we don't know about */
5419 sprintf(message, "Unknown event status x%02x", status);
5420 break;
5421 }
James Smartcd713482018-10-23 13:41:01 -07005422
5423 /* Issue READ_CONFIG mbox command to refresh supported speeds */
5424 rc = lpfc_sli4_read_config(phba);
James Smart3952e912018-10-23 13:41:02 -07005425 if (rc) {
James Smartcd713482018-10-23 13:41:01 -07005426 phba->lmt = 0;
5427 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5428 "3194 Unable to retrieve supported "
James Smart3952e912018-10-23 13:41:02 -07005429 "speeds, rc = 0x%x\n", rc);
James Smartcd713482018-10-23 13:41:01 -07005430 }
5431 vports = lpfc_create_vport_work_array(phba);
5432 if (vports != NULL) {
5433 for (i = 0; i <= phba->max_vports && vports[i] != NULL;
5434 i++) {
5435 shost = lpfc_shost_from_vport(vports[i]);
5436 lpfc_host_supported_speeds_set(shost);
5437 }
5438 }
5439 lpfc_destroy_vport_work_array(phba, vports);
5440
James Smart448193b2015-12-16 18:12:05 -05005441 phba->sli4_hba.lnk_info.optic_state = status;
James Smart946727d2015-04-07 15:07:09 -04005442 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart448193b2015-12-16 18:12:05 -05005443 "3176 Port Name %c %s\n", port_name, message);
James Smart946727d2015-04-07 15:07:09 -04005444 break;
5445 case LPFC_SLI_EVENT_TYPE_REMOTE_DPORT:
5446 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5447 "3192 Remote DPort Test Initiated - "
5448 "Event Data1:x%08x Event Data2: x%08x\n",
5449 acqe_sli->event_data1, acqe_sli->event_data2);
James Smart4b8bae02012-06-12 13:55:07 -04005450 break;
5451 default:
James Smart946727d2015-04-07 15:07:09 -04005452 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5453 "3193 Async SLI event - Event Data1:x%08x Event Data2:"
5454 "x%08x SLI Event Type:%d\n",
5455 acqe_sli->event_data1, acqe_sli->event_data2,
5456 evt_type);
James Smart4b8bae02012-06-12 13:55:07 -04005457 break;
5458 }
James Smart70f3c072010-12-15 17:57:33 -05005459}
5460
5461/**
James Smartfc2b9892010-02-26 14:15:29 -05005462 * lpfc_sli4_perform_vport_cvl - Perform clear virtual link on a vport
5463 * @vport: pointer to vport data structure.
5464 *
5465 * This routine is to perform Clear Virtual Link (CVL) on a vport in
5466 * response to a CVL event.
5467 *
5468 * Return the pointer to the ndlp with the vport if successful, otherwise
5469 * return NULL.
5470 **/
5471static struct lpfc_nodelist *
5472lpfc_sli4_perform_vport_cvl(struct lpfc_vport *vport)
5473{
5474 struct lpfc_nodelist *ndlp;
5475 struct Scsi_Host *shost;
5476 struct lpfc_hba *phba;
5477
5478 if (!vport)
5479 return NULL;
James Smartfc2b9892010-02-26 14:15:29 -05005480 phba = vport->phba;
5481 if (!phba)
5482 return NULL;
James Smart78730cf2010-04-06 15:06:30 -04005483 ndlp = lpfc_findnode_did(vport, Fabric_DID);
5484 if (!ndlp) {
5485 /* Cannot find existing Fabric ndlp, so allocate a new one */
James Smart9d3d3402017-04-21 16:05:00 -07005486 ndlp = lpfc_nlp_init(vport, Fabric_DID);
James Smart78730cf2010-04-06 15:06:30 -04005487 if (!ndlp)
5488 return 0;
James Smart78730cf2010-04-06 15:06:30 -04005489 /* Set the node type */
5490 ndlp->nlp_type |= NLP_FABRIC;
5491 /* Put ndlp onto node list */
5492 lpfc_enqueue_node(vport, ndlp);
5493 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
5494 /* re-setup ndlp without removing from node list */
5495 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
5496 if (!ndlp)
5497 return 0;
5498 }
James Smart63e801c2010-11-20 23:14:19 -05005499 if ((phba->pport->port_state < LPFC_FLOGI) &&
5500 (phba->pport->port_state != LPFC_VPORT_FAILED))
James Smartfc2b9892010-02-26 14:15:29 -05005501 return NULL;
5502 /* If virtual link is not yet instantiated ignore CVL */
James Smart63e801c2010-11-20 23:14:19 -05005503 if ((vport != phba->pport) && (vport->port_state < LPFC_FDISC)
5504 && (vport->port_state != LPFC_VPORT_FAILED))
James Smartfc2b9892010-02-26 14:15:29 -05005505 return NULL;
5506 shost = lpfc_shost_from_vport(vport);
5507 if (!shost)
5508 return NULL;
5509 lpfc_linkdown_port(vport);
5510 lpfc_cleanup_pending_mbox(vport);
5511 spin_lock_irq(shost->host_lock);
5512 vport->fc_flag |= FC_VPORT_CVL_RCVD;
5513 spin_unlock_irq(shost->host_lock);
5514
5515 return ndlp;
5516}
5517
5518/**
5519 * lpfc_sli4_perform_all_vport_cvl - Perform clear virtual link on all vports
5520 * @vport: pointer to lpfc hba data structure.
5521 *
5522 * This routine is to perform Clear Virtual Link (CVL) on all vports in
5523 * response to a FCF dead event.
5524 **/
5525static void
5526lpfc_sli4_perform_all_vport_cvl(struct lpfc_hba *phba)
5527{
5528 struct lpfc_vport **vports;
5529 int i;
5530
5531 vports = lpfc_create_vport_work_array(phba);
5532 if (vports)
5533 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
5534 lpfc_sli4_perform_vport_cvl(vports[i]);
5535 lpfc_destroy_vport_work_array(phba, vports);
5536}
5537
5538/**
James Smart76a95d72010-11-20 23:11:48 -05005539 * lpfc_sli4_async_fip_evt - Process the asynchronous FCoE FIP event
James Smartda0436e2009-05-22 14:51:39 -04005540 * @phba: pointer to lpfc hba data structure.
5541 * @acqe_link: pointer to the async fcoe completion queue entry.
5542 *
5543 * This routine is to handle the SLI4 asynchronous fcoe event.
5544 **/
5545static void
James Smart76a95d72010-11-20 23:11:48 -05005546lpfc_sli4_async_fip_evt(struct lpfc_hba *phba,
James Smart70f3c072010-12-15 17:57:33 -05005547 struct lpfc_acqe_fip *acqe_fip)
James Smartda0436e2009-05-22 14:51:39 -04005548{
James Smart70f3c072010-12-15 17:57:33 -05005549 uint8_t event_type = bf_get(lpfc_trailer_type, acqe_fip);
James Smartda0436e2009-05-22 14:51:39 -04005550 int rc;
James Smart6669f9b2009-10-02 15:16:45 -04005551 struct lpfc_vport *vport;
5552 struct lpfc_nodelist *ndlp;
5553 struct Scsi_Host *shost;
James Smart695a8142010-01-26 23:08:03 -05005554 int active_vlink_present;
5555 struct lpfc_vport **vports;
5556 int i;
James Smartda0436e2009-05-22 14:51:39 -04005557
James Smart70f3c072010-12-15 17:57:33 -05005558 phba->fc_eventTag = acqe_fip->event_tag;
5559 phba->fcoe_eventtag = acqe_fip->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04005560 switch (event_type) {
James Smart70f3c072010-12-15 17:57:33 -05005561 case LPFC_FIP_EVENT_TYPE_NEW_FCF:
5562 case LPFC_FIP_EVENT_TYPE_FCF_PARAM_MOD:
5563 if (event_type == LPFC_FIP_EVENT_TYPE_NEW_FCF)
James Smart999d8132010-03-15 11:24:56 -04005564 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
5565 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04005566 "2546 New FCF event, evt_tag:x%x, "
5567 "index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05005568 acqe_fip->event_tag,
5569 acqe_fip->index);
James Smart999d8132010-03-15 11:24:56 -04005570 else
5571 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP |
5572 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04005573 "2788 FCF param modified event, "
5574 "evt_tag:x%x, index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05005575 acqe_fip->event_tag,
5576 acqe_fip->index);
James Smart38b92ef2010-08-04 16:11:39 -04005577 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smart0c9ab6f2010-02-26 14:15:57 -05005578 /*
5579 * During period of FCF discovery, read the FCF
5580 * table record indexed by the event to update
James Smarta93ff372010-10-22 11:06:08 -04005581 * FCF roundrobin failover eligible FCF bmask.
James Smart0c9ab6f2010-02-26 14:15:57 -05005582 */
5583 lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
5584 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04005585 "2779 Read FCF (x%x) for updating "
5586 "roundrobin FCF failover bmask\n",
James Smart70f3c072010-12-15 17:57:33 -05005587 acqe_fip->index);
5588 rc = lpfc_sli4_read_fcf_rec(phba, acqe_fip->index);
James Smart0c9ab6f2010-02-26 14:15:57 -05005589 }
James Smart38b92ef2010-08-04 16:11:39 -04005590
5591 /* If the FCF discovery is in progress, do nothing. */
James Smart3804dc82010-07-14 15:31:37 -04005592 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04005593 if (phba->hba_flag & FCF_TS_INPROG) {
James Smart38b92ef2010-08-04 16:11:39 -04005594 spin_unlock_irq(&phba->hbalock);
5595 break;
5596 }
5597 /* If fast FCF failover rescan event is pending, do nothing */
James Smart036cad12018-10-23 13:41:06 -07005598 if (phba->fcf.fcf_flag & (FCF_REDISC_EVT | FCF_REDISC_PEND)) {
James Smart38b92ef2010-08-04 16:11:39 -04005599 spin_unlock_irq(&phba->hbalock);
5600 break;
5601 }
5602
James Smartc2b97122013-05-31 17:05:36 -04005603 /* If the FCF has been in discovered state, do nothing. */
5604 if (phba->fcf.fcf_flag & FCF_SCAN_DONE) {
James Smart3804dc82010-07-14 15:31:37 -04005605 spin_unlock_irq(&phba->hbalock);
5606 break;
5607 }
5608 spin_unlock_irq(&phba->hbalock);
James Smart38b92ef2010-08-04 16:11:39 -04005609
James Smart0c9ab6f2010-02-26 14:15:57 -05005610 /* Otherwise, scan the entire FCF table and re-discover SAN */
5611 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04005612 "2770 Start FCF table scan per async FCF "
5613 "event, evt_tag:x%x, index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05005614 acqe_fip->event_tag, acqe_fip->index);
James Smart0c9ab6f2010-02-26 14:15:57 -05005615 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba,
5616 LPFC_FCOE_FCF_GET_FIRST);
James Smartda0436e2009-05-22 14:51:39 -04005617 if (rc)
James Smart0c9ab6f2010-02-26 14:15:57 -05005618 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
5619 "2547 Issue FCF scan read FCF mailbox "
James Smarta93ff372010-10-22 11:06:08 -04005620 "command failed (x%x)\n", rc);
James Smartda0436e2009-05-22 14:51:39 -04005621 break;
5622
James Smart70f3c072010-12-15 17:57:33 -05005623 case LPFC_FIP_EVENT_TYPE_FCF_TABLE_FULL:
James Smartda0436e2009-05-22 14:51:39 -04005624 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte4e74272009-07-19 10:01:38 -04005625 "2548 FCF Table full count 0x%x tag 0x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05005626 bf_get(lpfc_acqe_fip_fcf_count, acqe_fip),
5627 acqe_fip->event_tag);
James Smartda0436e2009-05-22 14:51:39 -04005628 break;
5629
James Smart70f3c072010-12-15 17:57:33 -05005630 case LPFC_FIP_EVENT_TYPE_FCF_DEAD:
James Smart80c17842012-03-01 22:35:45 -05005631 phba->fcoe_cvl_eventtag = acqe_fip->event_tag;
James Smart0c9ab6f2010-02-26 14:15:57 -05005632 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04005633 "2549 FCF (x%x) disconnected from network, "
James Smart70f3c072010-12-15 17:57:33 -05005634 "tag:x%x\n", acqe_fip->index, acqe_fip->event_tag);
James Smart38b92ef2010-08-04 16:11:39 -04005635 /*
5636 * If we are in the middle of FCF failover process, clear
5637 * the corresponding FCF bit in the roundrobin bitmap.
James Smartda0436e2009-05-22 14:51:39 -04005638 */
James Smartfc2b9892010-02-26 14:15:29 -05005639 spin_lock_irq(&phba->hbalock);
James Smarta1cadfe2016-07-06 12:36:02 -07005640 if ((phba->fcf.fcf_flag & FCF_DISCOVERY) &&
5641 (phba->fcf.current_rec.fcf_indx != acqe_fip->index)) {
James Smartfc2b9892010-02-26 14:15:29 -05005642 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05005643 /* Update FLOGI FCF failover eligible FCF bmask */
James Smart70f3c072010-12-15 17:57:33 -05005644 lpfc_sli4_fcf_rr_index_clear(phba, acqe_fip->index);
James Smartfc2b9892010-02-26 14:15:29 -05005645 break;
5646 }
James Smart38b92ef2010-08-04 16:11:39 -04005647 spin_unlock_irq(&phba->hbalock);
5648
5649 /* If the event is not for currently used fcf do nothing */
James Smart70f3c072010-12-15 17:57:33 -05005650 if (phba->fcf.current_rec.fcf_indx != acqe_fip->index)
James Smart38b92ef2010-08-04 16:11:39 -04005651 break;
5652
5653 /*
5654 * Otherwise, request the port to rediscover the entire FCF
5655 * table for a fast recovery from case that the current FCF
5656 * is no longer valid as we are not in the middle of FCF
5657 * failover process already.
5658 */
James Smartc2b97122013-05-31 17:05:36 -04005659 spin_lock_irq(&phba->hbalock);
5660 /* Mark the fast failover process in progress */
5661 phba->fcf.fcf_flag |= FCF_DEAD_DISC;
5662 spin_unlock_irq(&phba->hbalock);
5663
5664 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
5665 "2771 Start FCF fast failover process due to "
5666 "FCF DEAD event: evt_tag:x%x, fcf_index:x%x "
5667 "\n", acqe_fip->event_tag, acqe_fip->index);
5668 rc = lpfc_sli4_redisc_fcf_table(phba);
5669 if (rc) {
5670 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
5671 LOG_DISCOVERY,
Colin Ian King7afc0ce2018-05-03 10:26:12 +01005672 "2772 Issue FCF rediscover mailbox "
James Smartc2b97122013-05-31 17:05:36 -04005673 "command failed, fail through to FCF "
5674 "dead event\n");
5675 spin_lock_irq(&phba->hbalock);
5676 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
5677 spin_unlock_irq(&phba->hbalock);
5678 /*
5679 * Last resort will fail over by treating this
5680 * as a link down to FCF registration.
5681 */
5682 lpfc_sli4_fcf_dead_failthrough(phba);
5683 } else {
5684 /* Reset FCF roundrobin bmask for new discovery */
5685 lpfc_sli4_clear_fcf_rr_bmask(phba);
5686 /*
5687 * Handling fast FCF failover to a DEAD FCF event is
5688 * considered equalivant to receiving CVL to all vports.
5689 */
5690 lpfc_sli4_perform_all_vport_cvl(phba);
5691 }
James Smartda0436e2009-05-22 14:51:39 -04005692 break;
James Smart70f3c072010-12-15 17:57:33 -05005693 case LPFC_FIP_EVENT_TYPE_CVL:
James Smart80c17842012-03-01 22:35:45 -05005694 phba->fcoe_cvl_eventtag = acqe_fip->event_tag;
James Smart0c9ab6f2010-02-26 14:15:57 -05005695 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
James Smart6669f9b2009-10-02 15:16:45 -04005696 "2718 Clear Virtual Link Received for VPI 0x%x"
James Smart70f3c072010-12-15 17:57:33 -05005697 " tag 0x%x\n", acqe_fip->index, acqe_fip->event_tag);
James Smart6d368e52011-05-24 11:44:12 -04005698
James Smart6669f9b2009-10-02 15:16:45 -04005699 vport = lpfc_find_vport_by_vpid(phba,
James Smart5248a742011-07-22 18:37:06 -04005700 acqe_fip->index);
James Smartfc2b9892010-02-26 14:15:29 -05005701 ndlp = lpfc_sli4_perform_vport_cvl(vport);
James Smart6669f9b2009-10-02 15:16:45 -04005702 if (!ndlp)
5703 break;
James Smart695a8142010-01-26 23:08:03 -05005704 active_vlink_present = 0;
5705
5706 vports = lpfc_create_vport_work_array(phba);
5707 if (vports) {
5708 for (i = 0; i <= phba->max_vports && vports[i] != NULL;
5709 i++) {
5710 if ((!(vports[i]->fc_flag &
5711 FC_VPORT_CVL_RCVD)) &&
5712 (vports[i]->port_state > LPFC_FDISC)) {
5713 active_vlink_present = 1;
5714 break;
5715 }
5716 }
5717 lpfc_destroy_vport_work_array(phba, vports);
5718 }
5719
James Smartcc823552015-05-21 13:55:26 -04005720 /*
5721 * Don't re-instantiate if vport is marked for deletion.
5722 * If we are here first then vport_delete is going to wait
5723 * for discovery to complete.
5724 */
5725 if (!(vport->load_flag & FC_UNLOADING) &&
5726 active_vlink_present) {
James Smart695a8142010-01-26 23:08:03 -05005727 /*
5728 * If there are other active VLinks present,
5729 * re-instantiate the Vlink using FDISC.
5730 */
James Smart256ec0d2013-04-17 20:14:58 -04005731 mod_timer(&ndlp->nlp_delayfunc,
5732 jiffies + msecs_to_jiffies(1000));
James Smartfc2b9892010-02-26 14:15:29 -05005733 shost = lpfc_shost_from_vport(vport);
James Smart6669f9b2009-10-02 15:16:45 -04005734 spin_lock_irq(shost->host_lock);
5735 ndlp->nlp_flag |= NLP_DELAY_TMO;
5736 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05005737 ndlp->nlp_last_elscmd = ELS_CMD_FDISC;
5738 vport->port_state = LPFC_FDISC;
5739 } else {
James Smartecfd03c2010-02-12 14:41:27 -05005740 /*
5741 * Otherwise, we request port to rediscover
5742 * the entire FCF table for a fast recovery
5743 * from possible case that the current FCF
James Smart0c9ab6f2010-02-26 14:15:57 -05005744 * is no longer valid if we are not already
5745 * in the FCF failover process.
James Smartecfd03c2010-02-12 14:41:27 -05005746 */
James Smartfc2b9892010-02-26 14:15:29 -05005747 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05005748 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smartfc2b9892010-02-26 14:15:29 -05005749 spin_unlock_irq(&phba->hbalock);
5750 break;
5751 }
5752 /* Mark the fast failover process in progress */
James Smart0c9ab6f2010-02-26 14:15:57 -05005753 phba->fcf.fcf_flag |= FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05005754 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05005755 lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
5756 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04005757 "2773 Start FCF failover per CVL, "
James Smart70f3c072010-12-15 17:57:33 -05005758 "evt_tag:x%x\n", acqe_fip->event_tag);
James Smartecfd03c2010-02-12 14:41:27 -05005759 rc = lpfc_sli4_redisc_fcf_table(phba);
James Smartfc2b9892010-02-26 14:15:29 -05005760 if (rc) {
James Smart0c9ab6f2010-02-26 14:15:57 -05005761 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
5762 LOG_DISCOVERY,
5763 "2774 Issue FCF rediscover "
Colin Ian King7afc0ce2018-05-03 10:26:12 +01005764 "mailbox command failed, "
James Smart0c9ab6f2010-02-26 14:15:57 -05005765 "through to CVL event\n");
James Smartfc2b9892010-02-26 14:15:29 -05005766 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05005767 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05005768 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05005769 /*
5770 * Last resort will be re-try on the
5771 * the current registered FCF entry.
5772 */
5773 lpfc_retry_pport_discovery(phba);
James Smart38b92ef2010-08-04 16:11:39 -04005774 } else
5775 /*
5776 * Reset FCF roundrobin bmask for new
5777 * discovery.
5778 */
James Smart7d791df2011-07-22 18:37:52 -04005779 lpfc_sli4_clear_fcf_rr_bmask(phba);
James Smart6669f9b2009-10-02 15:16:45 -04005780 }
5781 break;
James Smartda0436e2009-05-22 14:51:39 -04005782 default:
5783 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5784 "0288 Unknown FCoE event type 0x%x event tag "
James Smart70f3c072010-12-15 17:57:33 -05005785 "0x%x\n", event_type, acqe_fip->event_tag);
James Smartda0436e2009-05-22 14:51:39 -04005786 break;
5787 }
5788}
5789
5790/**
5791 * lpfc_sli4_async_dcbx_evt - Process the asynchronous dcbx event
5792 * @phba: pointer to lpfc hba data structure.
5793 * @acqe_link: pointer to the async dcbx completion queue entry.
5794 *
5795 * This routine is to handle the SLI4 asynchronous dcbx event.
5796 **/
5797static void
5798lpfc_sli4_async_dcbx_evt(struct lpfc_hba *phba,
5799 struct lpfc_acqe_dcbx *acqe_dcbx)
5800{
James Smart4d9ab992009-10-02 15:16:39 -04005801 phba->fc_eventTag = acqe_dcbx->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04005802 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5803 "0290 The SLI4 DCBX asynchronous event is not "
5804 "handled yet\n");
5805}
5806
5807/**
James Smartb19a0612010-04-06 14:48:51 -04005808 * lpfc_sli4_async_grp5_evt - Process the asynchronous group5 event
5809 * @phba: pointer to lpfc hba data structure.
5810 * @acqe_link: pointer to the async grp5 completion queue entry.
5811 *
5812 * This routine is to handle the SLI4 asynchronous grp5 event. A grp5 event
5813 * is an asynchronous notified of a logical link speed change. The Port
5814 * reports the logical link speed in units of 10Mbps.
5815 **/
5816static void
5817lpfc_sli4_async_grp5_evt(struct lpfc_hba *phba,
5818 struct lpfc_acqe_grp5 *acqe_grp5)
5819{
5820 uint16_t prev_ll_spd;
5821
5822 phba->fc_eventTag = acqe_grp5->event_tag;
5823 phba->fcoe_eventtag = acqe_grp5->event_tag;
5824 prev_ll_spd = phba->sli4_hba.link_state.logical_speed;
5825 phba->sli4_hba.link_state.logical_speed =
James Smart8b68cd52012-09-29 11:32:37 -04005826 (bf_get(lpfc_acqe_grp5_llink_spd, acqe_grp5)) * 10;
James Smartb19a0612010-04-06 14:48:51 -04005827 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5828 "2789 GRP5 Async Event: Updating logical link speed "
James Smart8b68cd52012-09-29 11:32:37 -04005829 "from %dMbps to %dMbps\n", prev_ll_spd,
5830 phba->sli4_hba.link_state.logical_speed);
James Smartb19a0612010-04-06 14:48:51 -04005831}
5832
5833/**
James Smartda0436e2009-05-22 14:51:39 -04005834 * lpfc_sli4_async_event_proc - Process all the pending asynchronous event
5835 * @phba: pointer to lpfc hba data structure.
5836 *
5837 * This routine is invoked by the worker thread to process all the pending
5838 * SLI4 asynchronous events.
5839 **/
5840void lpfc_sli4_async_event_proc(struct lpfc_hba *phba)
5841{
5842 struct lpfc_cq_event *cq_event;
5843
5844 /* First, declare the async event has been handled */
5845 spin_lock_irq(&phba->hbalock);
5846 phba->hba_flag &= ~ASYNC_EVENT;
5847 spin_unlock_irq(&phba->hbalock);
5848 /* Now, handle all the async events */
5849 while (!list_empty(&phba->sli4_hba.sp_asynce_work_queue)) {
5850 /* Get the first event from the head of the event queue */
5851 spin_lock_irq(&phba->hbalock);
5852 list_remove_head(&phba->sli4_hba.sp_asynce_work_queue,
5853 cq_event, struct lpfc_cq_event, list);
5854 spin_unlock_irq(&phba->hbalock);
5855 /* Process the asynchronous event */
5856 switch (bf_get(lpfc_trailer_code, &cq_event->cqe.mcqe_cmpl)) {
5857 case LPFC_TRAILER_CODE_LINK:
5858 lpfc_sli4_async_link_evt(phba,
5859 &cq_event->cqe.acqe_link);
5860 break;
5861 case LPFC_TRAILER_CODE_FCOE:
James Smart70f3c072010-12-15 17:57:33 -05005862 lpfc_sli4_async_fip_evt(phba, &cq_event->cqe.acqe_fip);
James Smartda0436e2009-05-22 14:51:39 -04005863 break;
5864 case LPFC_TRAILER_CODE_DCBX:
5865 lpfc_sli4_async_dcbx_evt(phba,
5866 &cq_event->cqe.acqe_dcbx);
5867 break;
James Smartb19a0612010-04-06 14:48:51 -04005868 case LPFC_TRAILER_CODE_GRP5:
5869 lpfc_sli4_async_grp5_evt(phba,
5870 &cq_event->cqe.acqe_grp5);
5871 break;
James Smart70f3c072010-12-15 17:57:33 -05005872 case LPFC_TRAILER_CODE_FC:
5873 lpfc_sli4_async_fc_evt(phba, &cq_event->cqe.acqe_fc);
5874 break;
5875 case LPFC_TRAILER_CODE_SLI:
5876 lpfc_sli4_async_sli_evt(phba, &cq_event->cqe.acqe_sli);
5877 break;
James Smartda0436e2009-05-22 14:51:39 -04005878 default:
5879 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5880 "1804 Invalid asynchrous event code: "
5881 "x%x\n", bf_get(lpfc_trailer_code,
5882 &cq_event->cqe.mcqe_cmpl));
5883 break;
5884 }
5885 /* Free the completion event processed to the free pool */
5886 lpfc_sli4_cq_event_release(phba, cq_event);
5887 }
5888}
5889
5890/**
James Smartecfd03c2010-02-12 14:41:27 -05005891 * lpfc_sli4_fcf_redisc_event_proc - Process fcf table rediscovery event
5892 * @phba: pointer to lpfc hba data structure.
5893 *
5894 * This routine is invoked by the worker thread to process FCF table
5895 * rediscovery pending completion event.
5896 **/
5897void lpfc_sli4_fcf_redisc_event_proc(struct lpfc_hba *phba)
5898{
5899 int rc;
5900
5901 spin_lock_irq(&phba->hbalock);
5902 /* Clear FCF rediscovery timeout event */
5903 phba->fcf.fcf_flag &= ~FCF_REDISC_EVT;
5904 /* Clear driver fast failover FCF record flag */
5905 phba->fcf.failover_rec.flag = 0;
5906 /* Set state for FCF fast failover */
5907 phba->fcf.fcf_flag |= FCF_REDISC_FOV;
5908 spin_unlock_irq(&phba->hbalock);
5909
5910 /* Scan FCF table from the first entry to re-discover SAN */
James Smart0c9ab6f2010-02-26 14:15:57 -05005911 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04005912 "2777 Start post-quiescent FCF table scan\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05005913 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
James Smartecfd03c2010-02-12 14:41:27 -05005914 if (rc)
James Smart0c9ab6f2010-02-26 14:15:57 -05005915 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
5916 "2747 Issue FCF scan read FCF mailbox "
5917 "command failed 0x%x\n", rc);
James Smartecfd03c2010-02-12 14:41:27 -05005918}
5919
5920/**
James Smartda0436e2009-05-22 14:51:39 -04005921 * lpfc_api_table_setup - Set up per hba pci-device group func api jump table
5922 * @phba: pointer to lpfc hba data structure.
5923 * @dev_grp: The HBA PCI-Device group number.
5924 *
5925 * This routine is invoked to set up the per HBA PCI-Device group function
5926 * API jump table entries.
5927 *
5928 * Return: 0 if success, otherwise -ENODEV
5929 **/
5930int
5931lpfc_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
James Smart5b75da22008-12-04 22:39:35 -05005932{
5933 int rc;
5934
James Smartda0436e2009-05-22 14:51:39 -04005935 /* Set up lpfc PCI-device group */
5936 phba->pci_dev_grp = dev_grp;
James Smart5b75da22008-12-04 22:39:35 -05005937
James Smartda0436e2009-05-22 14:51:39 -04005938 /* The LPFC_PCI_DEV_OC uses SLI4 */
5939 if (dev_grp == LPFC_PCI_DEV_OC)
5940 phba->sli_rev = LPFC_SLI_REV4;
James Smart5b75da22008-12-04 22:39:35 -05005941
James Smartda0436e2009-05-22 14:51:39 -04005942 /* Set up device INIT API function jump table */
5943 rc = lpfc_init_api_table_setup(phba, dev_grp);
5944 if (rc)
5945 return -ENODEV;
5946 /* Set up SCSI API function jump table */
5947 rc = lpfc_scsi_api_table_setup(phba, dev_grp);
5948 if (rc)
5949 return -ENODEV;
5950 /* Set up SLI API function jump table */
5951 rc = lpfc_sli_api_table_setup(phba, dev_grp);
5952 if (rc)
5953 return -ENODEV;
5954 /* Set up MBOX API function jump table */
5955 rc = lpfc_mbox_api_table_setup(phba, dev_grp);
5956 if (rc)
5957 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05005958
James Smartda0436e2009-05-22 14:51:39 -04005959 return 0;
James Smart5b75da22008-12-04 22:39:35 -05005960}
5961
5962/**
James Smart3621a712009-04-06 18:47:14 -04005963 * lpfc_log_intr_mode - Log the active interrupt mode
James Smart5b75da22008-12-04 22:39:35 -05005964 * @phba: pointer to lpfc hba data structure.
5965 * @intr_mode: active interrupt mode adopted.
5966 *
5967 * This routine it invoked to log the currently used active interrupt mode
5968 * to the device.
James Smart3772a992009-05-22 14:50:54 -04005969 **/
5970static void lpfc_log_intr_mode(struct lpfc_hba *phba, uint32_t intr_mode)
James Smart5b75da22008-12-04 22:39:35 -05005971{
5972 switch (intr_mode) {
5973 case 0:
5974 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5975 "0470 Enable INTx interrupt mode.\n");
5976 break;
5977 case 1:
5978 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5979 "0481 Enabled MSI interrupt mode.\n");
5980 break;
5981 case 2:
5982 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5983 "0480 Enabled MSI-X interrupt mode.\n");
5984 break;
5985 default:
5986 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5987 "0482 Illegal interrupt mode.\n");
5988 break;
5989 }
5990 return;
5991}
5992
James Smart5b75da22008-12-04 22:39:35 -05005993/**
James Smart3772a992009-05-22 14:50:54 -04005994 * lpfc_enable_pci_dev - Enable a generic PCI device.
James Smart5b75da22008-12-04 22:39:35 -05005995 * @phba: pointer to lpfc hba data structure.
5996 *
James Smart3772a992009-05-22 14:50:54 -04005997 * This routine is invoked to enable the PCI device that is common to all
5998 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05005999 *
6000 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006001 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04006002 * other values - error
James Smart5b75da22008-12-04 22:39:35 -05006003 **/
James Smart3772a992009-05-22 14:50:54 -04006004static int
6005lpfc_enable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05006006{
James Smart3772a992009-05-22 14:50:54 -04006007 struct pci_dev *pdev;
James Smart5b75da22008-12-04 22:39:35 -05006008
James Smart3772a992009-05-22 14:50:54 -04006009 /* Obtain PCI device reference */
6010 if (!phba->pcidev)
6011 goto out_error;
6012 else
6013 pdev = phba->pcidev;
James Smart3772a992009-05-22 14:50:54 -04006014 /* Enable PCI device */
6015 if (pci_enable_device_mem(pdev))
6016 goto out_error;
6017 /* Request PCI resource for the device */
Johannes Thumshirne0c04832016-06-07 09:44:03 +02006018 if (pci_request_mem_regions(pdev, LPFC_DRIVER_NAME))
James Smart3772a992009-05-22 14:50:54 -04006019 goto out_disable_device;
6020 /* Set up device as PCI master and save state for EEH */
6021 pci_set_master(pdev);
6022 pci_try_set_mwi(pdev);
6023 pci_save_state(pdev);
James Smart5b75da22008-12-04 22:39:35 -05006024
James Smart05580562011-05-24 11:40:48 -04006025 /* PCIe EEH recovery on powerpc platforms needs fundamental reset */
Jon Mason453193e2013-09-11 15:38:13 -07006026 if (pci_is_pcie(pdev))
James Smart05580562011-05-24 11:40:48 -04006027 pdev->needs_freset = 1;
6028
James Smart3772a992009-05-22 14:50:54 -04006029 return 0;
James Smart5b75da22008-12-04 22:39:35 -05006030
James Smart3772a992009-05-22 14:50:54 -04006031out_disable_device:
6032 pci_disable_device(pdev);
6033out_error:
James Smart079b5c92011-08-21 21:48:49 -04006034 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
Johannes Thumshirne0c04832016-06-07 09:44:03 +02006035 "1401 Failed to enable pci device\n");
James Smart3772a992009-05-22 14:50:54 -04006036 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05006037}
6038
6039/**
James Smart3772a992009-05-22 14:50:54 -04006040 * lpfc_disable_pci_dev - Disable a generic PCI device.
James Smart5b75da22008-12-04 22:39:35 -05006041 * @phba: pointer to lpfc hba data structure.
6042 *
James Smart3772a992009-05-22 14:50:54 -04006043 * This routine is invoked to disable the PCI device that is common to all
6044 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05006045 **/
6046static void
James Smart3772a992009-05-22 14:50:54 -04006047lpfc_disable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05006048{
James Smart3772a992009-05-22 14:50:54 -04006049 struct pci_dev *pdev;
James Smart5b75da22008-12-04 22:39:35 -05006050
James Smart3772a992009-05-22 14:50:54 -04006051 /* Obtain PCI device reference */
6052 if (!phba->pcidev)
6053 return;
6054 else
6055 pdev = phba->pcidev;
James Smart3772a992009-05-22 14:50:54 -04006056 /* Release PCI resource and disable PCI device */
Johannes Thumshirne0c04832016-06-07 09:44:03 +02006057 pci_release_mem_regions(pdev);
James Smart3772a992009-05-22 14:50:54 -04006058 pci_disable_device(pdev);
James Smart5b75da22008-12-04 22:39:35 -05006059
6060 return;
6061}
6062
6063/**
James Smart3772a992009-05-22 14:50:54 -04006064 * lpfc_reset_hba - Reset a hba
6065 * @phba: pointer to lpfc hba data structure.
James Smarte59058c2008-08-24 21:49:00 -04006066 *
James Smart3772a992009-05-22 14:50:54 -04006067 * This routine is invoked to reset a hba device. It brings the HBA
6068 * offline, performs a board restart, and then brings the board back
6069 * online. The lpfc_offline calls lpfc_sli_hba_down which will clean up
6070 * on outstanding mailbox commands.
James Smarte59058c2008-08-24 21:49:00 -04006071 **/
James Smart3772a992009-05-22 14:50:54 -04006072void
6073lpfc_reset_hba(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05006074{
James Smart3772a992009-05-22 14:50:54 -04006075 /* If resets are disabled then set error state and return. */
6076 if (!phba->cfg_enable_hba_reset) {
6077 phba->link_state = LPFC_HBA_ERROR;
6078 return;
6079 }
James Smartee620212014-04-04 13:51:53 -04006080 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
6081 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
6082 else
6083 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
James Smart3772a992009-05-22 14:50:54 -04006084 lpfc_offline(phba);
6085 lpfc_sli_brdrestart(phba);
6086 lpfc_online(phba);
6087 lpfc_unblock_mgmt_io(phba);
6088}
dea31012005-04-17 16:05:31 -05006089
James Smart3772a992009-05-22 14:50:54 -04006090/**
James Smart0a96e972011-07-22 18:37:28 -04006091 * lpfc_sli_sriov_nr_virtfn_get - Get the number of sr-iov virtual functions
6092 * @phba: pointer to lpfc hba data structure.
6093 *
6094 * This function enables the PCI SR-IOV virtual functions to a physical
6095 * function. It invokes the PCI SR-IOV api with the @nr_vfn provided to
6096 * enable the number of virtual functions to the physical function. As
6097 * not all devices support SR-IOV, the return code from the pci_enable_sriov()
6098 * API call does not considered as an error condition for most of the device.
6099 **/
6100uint16_t
6101lpfc_sli_sriov_nr_virtfn_get(struct lpfc_hba *phba)
6102{
6103 struct pci_dev *pdev = phba->pcidev;
6104 uint16_t nr_virtfn;
6105 int pos;
6106
James Smart0a96e972011-07-22 18:37:28 -04006107 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
6108 if (pos == 0)
6109 return 0;
6110
6111 pci_read_config_word(pdev, pos + PCI_SRIOV_TOTAL_VF, &nr_virtfn);
6112 return nr_virtfn;
6113}
6114
6115/**
James Smart912e3ac2011-05-24 11:42:11 -04006116 * lpfc_sli_probe_sriov_nr_virtfn - Enable a number of sr-iov virtual functions
6117 * @phba: pointer to lpfc hba data structure.
6118 * @nr_vfn: number of virtual functions to be enabled.
6119 *
6120 * This function enables the PCI SR-IOV virtual functions to a physical
6121 * function. It invokes the PCI SR-IOV api with the @nr_vfn provided to
6122 * enable the number of virtual functions to the physical function. As
6123 * not all devices support SR-IOV, the return code from the pci_enable_sriov()
6124 * API call does not considered as an error condition for most of the device.
6125 **/
6126int
6127lpfc_sli_probe_sriov_nr_virtfn(struct lpfc_hba *phba, int nr_vfn)
6128{
6129 struct pci_dev *pdev = phba->pcidev;
James Smart0a96e972011-07-22 18:37:28 -04006130 uint16_t max_nr_vfn;
James Smart912e3ac2011-05-24 11:42:11 -04006131 int rc;
6132
James Smart0a96e972011-07-22 18:37:28 -04006133 max_nr_vfn = lpfc_sli_sriov_nr_virtfn_get(phba);
6134 if (nr_vfn > max_nr_vfn) {
6135 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6136 "3057 Requested vfs (%d) greater than "
6137 "supported vfs (%d)", nr_vfn, max_nr_vfn);
6138 return -EINVAL;
6139 }
6140
James Smart912e3ac2011-05-24 11:42:11 -04006141 rc = pci_enable_sriov(pdev, nr_vfn);
6142 if (rc) {
6143 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6144 "2806 Failed to enable sriov on this device "
6145 "with vfn number nr_vf:%d, rc:%d\n",
6146 nr_vfn, rc);
6147 } else
6148 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6149 "2807 Successful enable sriov on this device "
6150 "with vfn number nr_vf:%d\n", nr_vfn);
6151 return rc;
6152}
6153
6154/**
James Smart895427b2017-02-12 13:52:30 -08006155 * lpfc_setup_driver_resource_phase1 - Phase1 etup driver internal resources.
James Smart3772a992009-05-22 14:50:54 -04006156 * @phba: pointer to lpfc hba data structure.
6157 *
James Smart895427b2017-02-12 13:52:30 -08006158 * This routine is invoked to set up the driver internal resources before the
6159 * device specific resource setup to support the HBA device it attached to.
James Smart3772a992009-05-22 14:50:54 -04006160 *
6161 * Return codes
James Smart895427b2017-02-12 13:52:30 -08006162 * 0 - successful
6163 * other values - error
James Smart3772a992009-05-22 14:50:54 -04006164 **/
6165static int
James Smart895427b2017-02-12 13:52:30 -08006166lpfc_setup_driver_resource_phase1(struct lpfc_hba *phba)
James Smart3772a992009-05-22 14:50:54 -04006167{
James Smart895427b2017-02-12 13:52:30 -08006168 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05006169
James Smart3772a992009-05-22 14:50:54 -04006170 /*
James Smart895427b2017-02-12 13:52:30 -08006171 * Driver resources common to all SLI revisions
James Smart3772a992009-05-22 14:50:54 -04006172 */
James Smart895427b2017-02-12 13:52:30 -08006173 atomic_set(&phba->fast_event_count, 0);
6174 spin_lock_init(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006175
James Smart895427b2017-02-12 13:52:30 -08006176 /* Initialize ndlp management spinlock */
6177 spin_lock_init(&phba->ndlp_lock);
James Smart3772a992009-05-22 14:50:54 -04006178
James Smart523128e2018-09-10 10:30:46 -07006179 /* Initialize port_list spinlock */
6180 spin_lock_init(&phba->port_list_lock);
James Smart895427b2017-02-12 13:52:30 -08006181 INIT_LIST_HEAD(&phba->port_list);
James Smart523128e2018-09-10 10:30:46 -07006182
James Smart895427b2017-02-12 13:52:30 -08006183 INIT_LIST_HEAD(&phba->work_list);
6184 init_waitqueue_head(&phba->wait_4_mlo_m_q);
6185
6186 /* Initialize the wait queue head for the kernel thread */
6187 init_waitqueue_head(&phba->work_waitq);
6188
6189 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smartf358dd02017-02-12 13:52:34 -08006190 "1403 Protocols supported %s %s %s\n",
James Smart895427b2017-02-12 13:52:30 -08006191 ((phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) ?
6192 "SCSI" : " "),
6193 ((phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) ?
James Smartf358dd02017-02-12 13:52:34 -08006194 "NVME" : " "),
6195 (phba->nvmet_support ? "NVMET" : " "));
James Smart895427b2017-02-12 13:52:30 -08006196
James Smart0794d602019-01-28 11:14:19 -08006197 /* Initialize the IO buffer list used by driver for SLI3 SCSI */
6198 spin_lock_init(&phba->scsi_buf_list_get_lock);
6199 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_get);
6200 spin_lock_init(&phba->scsi_buf_list_put_lock);
6201 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
James Smart895427b2017-02-12 13:52:30 -08006202
6203 /* Initialize the fabric iocb list */
6204 INIT_LIST_HEAD(&phba->fabric_iocb_list);
6205
6206 /* Initialize list to save ELS buffers */
6207 INIT_LIST_HEAD(&phba->elsbuf);
6208
6209 /* Initialize FCF connection rec list */
6210 INIT_LIST_HEAD(&phba->fcf_conn_rec_list);
6211
6212 /* Initialize OAS configuration list */
6213 spin_lock_init(&phba->devicelock);
6214 INIT_LIST_HEAD(&phba->luns);
6215
James Smart3772a992009-05-22 14:50:54 -04006216 /* MBOX heartbeat timer */
Kees Cookf22eb4d2017-09-06 20:24:26 -07006217 timer_setup(&psli->mbox_tmo, lpfc_mbox_timeout, 0);
James Smart3772a992009-05-22 14:50:54 -04006218 /* Fabric block timer */
Kees Cookf22eb4d2017-09-06 20:24:26 -07006219 timer_setup(&phba->fabric_block_timer, lpfc_fabric_block_timeout, 0);
James Smart3772a992009-05-22 14:50:54 -04006220 /* EA polling mode timer */
Kees Cookf22eb4d2017-09-06 20:24:26 -07006221 timer_setup(&phba->eratt_poll, lpfc_poll_eratt, 0);
James Smart895427b2017-02-12 13:52:30 -08006222 /* Heartbeat timer */
Kees Cookf22eb4d2017-09-06 20:24:26 -07006223 timer_setup(&phba->hb_tmofunc, lpfc_hb_timeout, 0);
James Smart895427b2017-02-12 13:52:30 -08006224
James Smart32517fc2019-01-28 11:14:33 -08006225 INIT_DELAYED_WORK(&phba->eq_delay_work, lpfc_hb_eq_delay_work);
6226
James Smart895427b2017-02-12 13:52:30 -08006227 return 0;
6228}
6229
6230/**
6231 * lpfc_sli_driver_resource_setup - Setup driver internal resources for SLI3 dev
6232 * @phba: pointer to lpfc hba data structure.
6233 *
6234 * This routine is invoked to set up the driver internal resources specific to
6235 * support the SLI-3 HBA device it attached to.
6236 *
6237 * Return codes
6238 * 0 - successful
6239 * other values - error
6240 **/
6241static int
6242lpfc_sli_driver_resource_setup(struct lpfc_hba *phba)
6243{
James Smart0794d602019-01-28 11:14:19 -08006244 int rc, entry_sz;
James Smart895427b2017-02-12 13:52:30 -08006245
6246 /*
6247 * Initialize timers used by driver
6248 */
6249
6250 /* FCP polling mode timer */
Kees Cookf22eb4d2017-09-06 20:24:26 -07006251 timer_setup(&phba->fcp_poll_timer, lpfc_poll_timeout, 0);
James Smart3772a992009-05-22 14:50:54 -04006252
6253 /* Host attention work mask setup */
6254 phba->work_ha_mask = (HA_ERATT | HA_MBATT | HA_LATT);
6255 phba->work_ha_mask |= (HA_RXMASK << (LPFC_ELS_RING * 4));
6256
6257 /* Get all the module params for configuring this host */
6258 lpfc_get_cfgparam(phba);
James Smart895427b2017-02-12 13:52:30 -08006259 /* Set up phase-1 common device driver resources */
6260
6261 rc = lpfc_setup_driver_resource_phase1(phba);
6262 if (rc)
6263 return -ENODEV;
6264
James Smart49198b32010-04-06 15:04:33 -04006265 if (phba->pcidev->device == PCI_DEVICE_ID_HORNET) {
6266 phba->menlo_flag |= HBA_MENLO_SUPPORT;
6267 /* check for menlo minimum sg count */
6268 if (phba->cfg_sg_seg_cnt < LPFC_DEFAULT_MENLO_SG_SEG_CNT)
6269 phba->cfg_sg_seg_cnt = LPFC_DEFAULT_MENLO_SG_SEG_CNT;
6270 }
6271
James Smart895427b2017-02-12 13:52:30 -08006272 if (!phba->sli.sli3_ring)
Kees Cook6396bb22018-06-12 14:03:40 -07006273 phba->sli.sli3_ring = kcalloc(LPFC_SLI3_MAX_RING,
6274 sizeof(struct lpfc_sli_ring),
6275 GFP_KERNEL);
James Smart895427b2017-02-12 13:52:30 -08006276 if (!phba->sli.sli3_ring)
James Smart2a76a282012-08-03 12:35:54 -04006277 return -ENOMEM;
6278
James Smart3772a992009-05-22 14:50:54 -04006279 /*
James Smart96f70772013-04-17 20:16:15 -04006280 * Since lpfc_sg_seg_cnt is module parameter, the sg_dma_buf_size
James Smart3772a992009-05-22 14:50:54 -04006281 * used to create the sg_dma_buf_pool must be dynamically calculated.
James Smart3772a992009-05-22 14:50:54 -04006282 */
James Smart3772a992009-05-22 14:50:54 -04006283
James Smart96f70772013-04-17 20:16:15 -04006284 /* Initialize the host templates the configured values. */
James Smart3772a992009-05-22 14:50:54 -04006285 lpfc_vport_template.sg_tablesize = phba->cfg_sg_seg_cnt;
James Smart96418b52017-03-04 09:30:31 -08006286 lpfc_template_no_hr.sg_tablesize = phba->cfg_sg_seg_cnt;
6287 lpfc_template.sg_tablesize = phba->cfg_sg_seg_cnt;
James Smart3772a992009-05-22 14:50:54 -04006288
James Smart0794d602019-01-28 11:14:19 -08006289 if (phba->sli_rev == LPFC_SLI_REV4)
6290 entry_sz = sizeof(struct sli4_sge);
6291 else
6292 entry_sz = sizeof(struct ulp_bde64);
6293
James Smart96f70772013-04-17 20:16:15 -04006294 /* There are going to be 2 reserved BDEs: 1 FCP cmnd + 1 FCP rsp */
6295 if (phba->cfg_enable_bg) {
6296 /*
6297 * The scsi_buf for a T10-DIF I/O will hold the FCP cmnd,
6298 * the FCP rsp, and a BDE for each. Sice we have no control
6299 * over how many protection data segments the SCSI Layer
6300 * will hand us (ie: there could be one for every block
6301 * in the IO), we just allocate enough BDEs to accomidate
6302 * our max amount and we need to limit lpfc_sg_seg_cnt to
6303 * minimize the risk of running out.
6304 */
6305 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
6306 sizeof(struct fcp_rsp) +
James Smart0794d602019-01-28 11:14:19 -08006307 (LPFC_MAX_SG_SEG_CNT * entry_sz);
James Smart96f70772013-04-17 20:16:15 -04006308
6309 if (phba->cfg_sg_seg_cnt > LPFC_MAX_SG_SEG_CNT_DIF)
6310 phba->cfg_sg_seg_cnt = LPFC_MAX_SG_SEG_CNT_DIF;
6311
6312 /* Total BDEs in BPL for scsi_sg_list and scsi_sg_prot_list */
6313 phba->cfg_total_seg_cnt = LPFC_MAX_SG_SEG_CNT;
6314 } else {
6315 /*
6316 * The scsi_buf for a regular I/O will hold the FCP cmnd,
6317 * the FCP rsp, a BDE for each, and a BDE for up to
6318 * cfg_sg_seg_cnt data segments.
6319 */
6320 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
6321 sizeof(struct fcp_rsp) +
James Smart0794d602019-01-28 11:14:19 -08006322 ((phba->cfg_sg_seg_cnt + 2) * entry_sz);
James Smart96f70772013-04-17 20:16:15 -04006323
6324 /* Total BDEs in BPL for scsi_sg_list */
6325 phba->cfg_total_seg_cnt = phba->cfg_sg_seg_cnt + 2;
6326 }
6327
6328 lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_FCP,
6329 "9088 sg_tablesize:%d dmabuf_size:%d total_bde:%d\n",
6330 phba->cfg_sg_seg_cnt, phba->cfg_sg_dma_buf_size,
6331 phba->cfg_total_seg_cnt);
6332
James Smart3772a992009-05-22 14:50:54 -04006333 phba->max_vpi = LPFC_MAX_VPI;
6334 /* This will be set to correct value after config_port mbox */
6335 phba->max_vports = 0;
6336
6337 /*
6338 * Initialize the SLI Layer to run with lpfc HBAs.
6339 */
6340 lpfc_sli_setup(phba);
James Smart895427b2017-02-12 13:52:30 -08006341 lpfc_sli_queue_init(phba);
James Smart3772a992009-05-22 14:50:54 -04006342
6343 /* Allocate device driver memory */
6344 if (lpfc_mem_alloc(phba, BPL_ALIGN_SZ))
6345 return -ENOMEM;
6346
James Smart912e3ac2011-05-24 11:42:11 -04006347 /*
6348 * Enable sr-iov virtual functions if supported and configured
6349 * through the module parameter.
6350 */
6351 if (phba->cfg_sriov_nr_virtfn > 0) {
6352 rc = lpfc_sli_probe_sriov_nr_virtfn(phba,
6353 phba->cfg_sriov_nr_virtfn);
6354 if (rc) {
6355 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6356 "2808 Requested number of SR-IOV "
6357 "virtual functions (%d) is not "
6358 "supported\n",
6359 phba->cfg_sriov_nr_virtfn);
6360 phba->cfg_sriov_nr_virtfn = 0;
6361 }
6362 }
6363
James Smart3772a992009-05-22 14:50:54 -04006364 return 0;
6365}
6366
6367/**
6368 * lpfc_sli_driver_resource_unset - Unset drvr internal resources for SLI3 dev
6369 * @phba: pointer to lpfc hba data structure.
6370 *
6371 * This routine is invoked to unset the driver internal resources set up
6372 * specific for supporting the SLI-3 HBA device it attached to.
6373 **/
6374static void
6375lpfc_sli_driver_resource_unset(struct lpfc_hba *phba)
6376{
6377 /* Free device driver memory allocated */
6378 lpfc_mem_free_all(phba);
6379
6380 return;
6381}
6382
6383/**
James Smartda0436e2009-05-22 14:51:39 -04006384 * lpfc_sli4_driver_resource_setup - Setup drvr internal resources for SLI4 dev
6385 * @phba: pointer to lpfc hba data structure.
6386 *
6387 * This routine is invoked to set up the driver internal resources specific to
6388 * support the SLI-4 HBA device it attached to.
6389 *
6390 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006391 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04006392 * other values - error
6393 **/
6394static int
6395lpfc_sli4_driver_resource_setup(struct lpfc_hba *phba)
6396{
James Smart28baac72010-02-12 14:42:03 -05006397 LPFC_MBOXQ_t *mboxq;
James Smartf358dd02017-02-12 13:52:34 -08006398 MAILBOX_t *mb;
James Smart895427b2017-02-12 13:52:30 -08006399 int rc, i, max_buf_size;
James Smart28baac72010-02-12 14:42:03 -05006400 uint8_t pn_page[LPFC_MAX_SUPPORTED_PAGES] = {0};
6401 struct lpfc_mqe *mqe;
James Smart09294d42013-04-17 20:16:05 -04006402 int longs;
James Smart81e6a632017-11-20 16:00:43 -08006403 int extra;
James Smartf358dd02017-02-12 13:52:34 -08006404 uint64_t wwn;
James Smartb92dc722018-05-24 21:09:01 -07006405 u32 if_type;
6406 u32 if_fam;
James Smartda0436e2009-05-22 14:51:39 -04006407
James Smart895427b2017-02-12 13:52:30 -08006408 phba->sli4_hba.num_present_cpu = lpfc_present_cpu;
James Smart222e9232019-01-28 11:14:35 -08006409 phba->sli4_hba.num_possible_cpu = num_possible_cpus();
James Smart895427b2017-02-12 13:52:30 -08006410 phba->sli4_hba.curr_disp_cpu = 0;
6411
James Smart716d3bc2013-09-06 12:18:28 -04006412 /* Get all the module params for configuring this host */
6413 lpfc_get_cfgparam(phba);
6414
James Smart895427b2017-02-12 13:52:30 -08006415 /* Set up phase-1 common device driver resources */
6416 rc = lpfc_setup_driver_resource_phase1(phba);
6417 if (rc)
6418 return -ENODEV;
6419
James Smartda0436e2009-05-22 14:51:39 -04006420 /* Before proceed, wait for POST done and device ready */
6421 rc = lpfc_sli4_post_status_check(phba);
6422 if (rc)
6423 return -ENODEV;
6424
James Smart3cee98d2019-08-14 16:56:34 -07006425 /* Allocate all driver workqueues here */
6426
6427 /* The lpfc_wq workqueue for deferred irq use */
6428 phba->wq = alloc_workqueue("lpfc_wq", WQ_MEM_RECLAIM, 0);
6429
James Smartda0436e2009-05-22 14:51:39 -04006430 /*
6431 * Initialize timers used by driver
6432 */
6433
Kees Cookf22eb4d2017-09-06 20:24:26 -07006434 timer_setup(&phba->rrq_tmr, lpfc_rrq_timeout, 0);
James Smartda0436e2009-05-22 14:51:39 -04006435
James Smartecfd03c2010-02-12 14:41:27 -05006436 /* FCF rediscover timer */
Kees Cookf22eb4d2017-09-06 20:24:26 -07006437 timer_setup(&phba->fcf.redisc_wait, lpfc_sli4_fcf_redisc_wait_tmo, 0);
James Smartecfd03c2010-02-12 14:41:27 -05006438
James Smartda0436e2009-05-22 14:51:39 -04006439 /*
James Smart7ad20aa2011-05-24 11:44:28 -04006440 * Control structure for handling external multi-buffer mailbox
6441 * command pass-through.
6442 */
6443 memset((uint8_t *)&phba->mbox_ext_buf_ctx, 0,
6444 sizeof(struct lpfc_mbox_ext_buf_ctx));
6445 INIT_LIST_HEAD(&phba->mbox_ext_buf_ctx.ext_dmabuf_list);
6446
James Smartda0436e2009-05-22 14:51:39 -04006447 phba->max_vpi = LPFC_MAX_VPI;
James Smart67d12732012-08-03 12:36:13 -04006448
James Smartda0436e2009-05-22 14:51:39 -04006449 /* This will be set to correct value after the read_config mbox */
6450 phba->max_vports = 0;
6451
6452 /* Program the default value of vlan_id and fc_map */
6453 phba->valid_vlan = 0;
6454 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
6455 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
6456 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
6457
6458 /*
James Smart2a76a282012-08-03 12:35:54 -04006459 * For SLI4, instead of using ring 0 (LPFC_FCP_RING) for FCP commands
James Smart895427b2017-02-12 13:52:30 -08006460 * we will associate a new ring, for each EQ/CQ/WQ tuple.
6461 * The WQ create will allocate the ring.
James Smart2a76a282012-08-03 12:35:54 -04006462 */
James Smart085c6472010-11-20 23:11:37 -05006463
James Smart09294d42013-04-17 20:16:05 -04006464 /*
James Smart81e6a632017-11-20 16:00:43 -08006465 * 1 for cmd, 1 for rsp, NVME adds an extra one
6466 * for boundary conditions in its max_sgl_segment template.
6467 */
6468 extra = 2;
6469 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
6470 extra++;
6471
6472 /*
James Smart09294d42013-04-17 20:16:05 -04006473 * It doesn't matter what family our adapter is in, we are
6474 * limited to 2 Pages, 512 SGEs, for our SGL.
6475 * There are going to be 2 reserved SGEs: 1 FCP cmnd + 1 FCP rsp
6476 */
6477 max_buf_size = (2 * SLI4_PAGE_SIZE);
James Smart09294d42013-04-17 20:16:05 -04006478
James Smartda0436e2009-05-22 14:51:39 -04006479 /*
James Smart895427b2017-02-12 13:52:30 -08006480 * Since lpfc_sg_seg_cnt is module param, the sg_dma_buf_size
6481 * used to create the sg_dma_buf_pool must be calculated.
James Smart28baac72010-02-12 14:42:03 -05006482 */
James Smartf44ac122018-03-05 12:04:08 -08006483 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED) {
James Smart96f70772013-04-17 20:16:15 -04006484 /*
James Smart895427b2017-02-12 13:52:30 -08006485 * The scsi_buf for a T10-DIF I/O holds the FCP cmnd,
6486 * the FCP rsp, and a SGE. Sice we have no control
6487 * over how many protection segments the SCSI Layer
James Smart96f70772013-04-17 20:16:15 -04006488 * will hand us (ie: there could be one for every block
James Smart895427b2017-02-12 13:52:30 -08006489 * in the IO), just allocate enough SGEs to accomidate
6490 * our max amount and we need to limit lpfc_sg_seg_cnt
6491 * to minimize the risk of running out.
James Smart96f70772013-04-17 20:16:15 -04006492 */
6493 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
James Smart895427b2017-02-12 13:52:30 -08006494 sizeof(struct fcp_rsp) + max_buf_size;
James Smart96f70772013-04-17 20:16:15 -04006495
6496 /* Total SGEs for scsi_sg_list and scsi_sg_prot_list */
6497 phba->cfg_total_seg_cnt = LPFC_MAX_SGL_SEG_CNT;
6498
James Smart5b9e70b2018-09-10 10:30:42 -07006499 /*
6500 * If supporting DIF, reduce the seg count for scsi to
6501 * allow room for the DIF sges.
6502 */
6503 if (phba->cfg_enable_bg &&
6504 phba->cfg_sg_seg_cnt > LPFC_MAX_BG_SLI4_SEG_CNT_DIF)
6505 phba->cfg_scsi_seg_cnt = LPFC_MAX_BG_SLI4_SEG_CNT_DIF;
6506 else
6507 phba->cfg_scsi_seg_cnt = phba->cfg_sg_seg_cnt;
6508
James Smart96f70772013-04-17 20:16:15 -04006509 } else {
6510 /*
James Smart895427b2017-02-12 13:52:30 -08006511 * The scsi_buf for a regular I/O holds the FCP cmnd,
James Smart96f70772013-04-17 20:16:15 -04006512 * the FCP rsp, a SGE for each, and a SGE for up to
6513 * cfg_sg_seg_cnt data segments.
6514 */
6515 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
James Smart895427b2017-02-12 13:52:30 -08006516 sizeof(struct fcp_rsp) +
James Smart81e6a632017-11-20 16:00:43 -08006517 ((phba->cfg_sg_seg_cnt + extra) *
James Smart895427b2017-02-12 13:52:30 -08006518 sizeof(struct sli4_sge));
James Smart96f70772013-04-17 20:16:15 -04006519
6520 /* Total SGEs for scsi_sg_list */
James Smart81e6a632017-11-20 16:00:43 -08006521 phba->cfg_total_seg_cnt = phba->cfg_sg_seg_cnt + extra;
James Smart5b9e70b2018-09-10 10:30:42 -07006522 phba->cfg_scsi_seg_cnt = phba->cfg_sg_seg_cnt;
James Smart895427b2017-02-12 13:52:30 -08006523
James Smart96f70772013-04-17 20:16:15 -04006524 /*
James Smart81e6a632017-11-20 16:00:43 -08006525 * NOTE: if (phba->cfg_sg_seg_cnt + extra) <= 256 we only
James Smart895427b2017-02-12 13:52:30 -08006526 * need to post 1 page for the SGL.
James Smart96f70772013-04-17 20:16:15 -04006527 */
James Smart085c6472010-11-20 23:11:37 -05006528 }
James Smartacd68592012-01-18 16:25:09 -05006529
James Smart5b9e70b2018-09-10 10:30:42 -07006530 /* Limit to LPFC_MAX_NVME_SEG_CNT for NVME. */
6531 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
6532 if (phba->cfg_sg_seg_cnt > LPFC_MAX_NVME_SEG_CNT) {
6533 lpfc_printf_log(phba, KERN_INFO, LOG_NVME | LOG_INIT,
6534 "6300 Reducing NVME sg segment "
6535 "cnt to %d\n",
6536 LPFC_MAX_NVME_SEG_CNT);
6537 phba->cfg_nvme_seg_cnt = LPFC_MAX_NVME_SEG_CNT;
6538 } else
6539 phba->cfg_nvme_seg_cnt = phba->cfg_sg_seg_cnt;
6540 }
6541
James Smart96f70772013-04-17 20:16:15 -04006542 /* Initialize the host templates with the updated values. */
James Smart5b9e70b2018-09-10 10:30:42 -07006543 lpfc_vport_template.sg_tablesize = phba->cfg_scsi_seg_cnt;
6544 lpfc_template.sg_tablesize = phba->cfg_scsi_seg_cnt;
6545 lpfc_template_no_hr.sg_tablesize = phba->cfg_scsi_seg_cnt;
James Smart96f70772013-04-17 20:16:15 -04006546
6547 if (phba->cfg_sg_dma_buf_size <= LPFC_MIN_SG_SLI4_BUF_SZ)
6548 phba->cfg_sg_dma_buf_size = LPFC_MIN_SG_SLI4_BUF_SZ;
6549 else
6550 phba->cfg_sg_dma_buf_size =
6551 SLI4_PAGE_ALIGN(phba->cfg_sg_dma_buf_size);
6552
6553 lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_FCP,
James Smart5b9e70b2018-09-10 10:30:42 -07006554 "9087 sg_seg_cnt:%d dmabuf_size:%d "
6555 "total:%d scsi:%d nvme:%d\n",
James Smart96f70772013-04-17 20:16:15 -04006556 phba->cfg_sg_seg_cnt, phba->cfg_sg_dma_buf_size,
James Smart5b9e70b2018-09-10 10:30:42 -07006557 phba->cfg_total_seg_cnt, phba->cfg_scsi_seg_cnt,
6558 phba->cfg_nvme_seg_cnt);
James Smartda0436e2009-05-22 14:51:39 -04006559
6560 /* Initialize buffer queue management fields */
James Smart895427b2017-02-12 13:52:30 -08006561 INIT_LIST_HEAD(&phba->hbqs[LPFC_ELS_HBQ].hbq_buffer_list);
James Smartda0436e2009-05-22 14:51:39 -04006562 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_sli4_rb_alloc;
6563 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_sli4_rb_free;
6564
6565 /*
6566 * Initialize the SLI Layer to run with lpfc SLI4 HBAs.
6567 */
James Smart895427b2017-02-12 13:52:30 -08006568 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) {
6569 /* Initialize the Abort scsi buffer list used by driver */
6570 spin_lock_init(&phba->sli4_hba.abts_scsi_buf_list_lock);
6571 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
6572 }
6573
6574 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
6575 /* Initialize the Abort nvme buffer list used by driver */
James Smart5e5b5112019-01-28 11:14:22 -08006576 spin_lock_init(&phba->sli4_hba.abts_nvmet_buf_list_lock);
James Smart86c67372017-04-21 16:05:04 -07006577 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_nvmet_ctx_list);
James Smarta8cf5df2017-05-15 15:20:46 -07006578 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_nvmet_io_wait_list);
James Smart79d8c4c2019-05-21 17:48:56 -07006579 spin_lock_init(&phba->sli4_hba.t_active_list_lock);
6580 INIT_LIST_HEAD(&phba->sli4_hba.t_active_ctx_list);
James Smart895427b2017-02-12 13:52:30 -08006581 }
6582
James Smartda0436e2009-05-22 14:51:39 -04006583 /* This abort list used by worker thread */
James Smart895427b2017-02-12 13:52:30 -08006584 spin_lock_init(&phba->sli4_hba.sgl_list_lock);
James Smarta8cf5df2017-05-15 15:20:46 -07006585 spin_lock_init(&phba->sli4_hba.nvmet_io_wait_lock);
James Smartda0436e2009-05-22 14:51:39 -04006586
6587 /*
James Smart6d368e52011-05-24 11:44:12 -04006588 * Initialize driver internal slow-path work queues
James Smartda0436e2009-05-22 14:51:39 -04006589 */
6590
6591 /* Driver internel slow-path CQ Event pool */
6592 INIT_LIST_HEAD(&phba->sli4_hba.sp_cqe_event_pool);
6593 /* Response IOCB work queue list */
James Smart45ed1192009-10-02 15:17:02 -04006594 INIT_LIST_HEAD(&phba->sli4_hba.sp_queue_event);
James Smartda0436e2009-05-22 14:51:39 -04006595 /* Asynchronous event CQ Event work queue list */
6596 INIT_LIST_HEAD(&phba->sli4_hba.sp_asynce_work_queue);
6597 /* Fast-path XRI aborted CQ Event work queue list */
6598 INIT_LIST_HEAD(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
6599 /* Slow-path XRI aborted CQ Event work queue list */
6600 INIT_LIST_HEAD(&phba->sli4_hba.sp_els_xri_aborted_work_queue);
6601 /* Receive queue CQ Event work queue list */
6602 INIT_LIST_HEAD(&phba->sli4_hba.sp_unsol_work_queue);
6603
James Smart6d368e52011-05-24 11:44:12 -04006604 /* Initialize extent block lists. */
6605 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_blk_list);
6606 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_xri_blk_list);
6607 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_vfi_blk_list);
6608 INIT_LIST_HEAD(&phba->lpfc_vpi_blk_list);
6609
James Smartd1f525a2017-04-21 16:04:55 -07006610 /* Initialize mboxq lists. If the early init routines fail
6611 * these lists need to be correctly initialized.
6612 */
6613 INIT_LIST_HEAD(&phba->sli.mboxq);
6614 INIT_LIST_HEAD(&phba->sli.mboxq_cmpl);
6615
James Smart448193b2015-12-16 18:12:05 -05006616 /* initialize optic_state to 0xFF */
6617 phba->sli4_hba.lnk_info.optic_state = 0xff;
6618
James Smartda0436e2009-05-22 14:51:39 -04006619 /* Allocate device driver memory */
6620 rc = lpfc_mem_alloc(phba, SGL_ALIGN_SZ);
6621 if (rc)
6622 return -ENOMEM;
6623
James Smart2fcee4b2010-12-15 17:57:46 -05006624 /* IF Type 2 ports get initialized now. */
James Smart27d6ac02018-02-22 08:18:42 -08006625 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) >=
James Smart2fcee4b2010-12-15 17:57:46 -05006626 LPFC_SLI_INTF_IF_TYPE_2) {
6627 rc = lpfc_pci_function_reset(phba);
James Smart895427b2017-02-12 13:52:30 -08006628 if (unlikely(rc)) {
6629 rc = -ENODEV;
6630 goto out_free_mem;
6631 }
James Smart946727d2015-04-07 15:07:09 -04006632 phba->temp_sensor_support = 1;
James Smart2fcee4b2010-12-15 17:57:46 -05006633 }
6634
James Smartda0436e2009-05-22 14:51:39 -04006635 /* Create the bootstrap mailbox command */
6636 rc = lpfc_create_bootstrap_mbox(phba);
6637 if (unlikely(rc))
6638 goto out_free_mem;
6639
6640 /* Set up the host's endian order with the device. */
6641 rc = lpfc_setup_endian_order(phba);
6642 if (unlikely(rc))
6643 goto out_free_bsmbx;
6644
6645 /* Set up the hba's configuration parameters. */
6646 rc = lpfc_sli4_read_config(phba);
6647 if (unlikely(rc))
6648 goto out_free_bsmbx;
James Smartcff261f2013-12-17 20:29:47 -05006649 rc = lpfc_mem_alloc_active_rrq_pool_s4(phba);
6650 if (unlikely(rc))
6651 goto out_free_bsmbx;
James Smartda0436e2009-05-22 14:51:39 -04006652
James Smart2fcee4b2010-12-15 17:57:46 -05006653 /* IF Type 0 ports get initialized now. */
6654 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
6655 LPFC_SLI_INTF_IF_TYPE_0) {
6656 rc = lpfc_pci_function_reset(phba);
6657 if (unlikely(rc))
6658 goto out_free_bsmbx;
6659 }
James Smartda0436e2009-05-22 14:51:39 -04006660
James Smartcb5172e2010-03-15 11:25:07 -04006661 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
6662 GFP_KERNEL);
6663 if (!mboxq) {
6664 rc = -ENOMEM;
6665 goto out_free_bsmbx;
6666 }
6667
James Smartf358dd02017-02-12 13:52:34 -08006668 /* Check for NVMET being configured */
James Smart895427b2017-02-12 13:52:30 -08006669 phba->nvmet_support = 0;
James Smartf358dd02017-02-12 13:52:34 -08006670 if (lpfc_enable_nvmet_cnt) {
6671
6672 /* First get WWN of HBA instance */
6673 lpfc_read_nv(phba, mboxq);
6674 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6675 if (rc != MBX_SUCCESS) {
6676 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6677 "6016 Mailbox failed , mbxCmd x%x "
6678 "READ_NV, mbxStatus x%x\n",
6679 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
6680 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
James Smartd1f525a2017-04-21 16:04:55 -07006681 mempool_free(mboxq, phba->mbox_mem_pool);
James Smartf358dd02017-02-12 13:52:34 -08006682 rc = -EIO;
6683 goto out_free_bsmbx;
6684 }
6685 mb = &mboxq->u.mb;
6686 memcpy(&wwn, (char *)mb->un.varRDnvp.nodename,
6687 sizeof(uint64_t));
6688 wwn = cpu_to_be64(wwn);
6689 phba->sli4_hba.wwnn.u.name = wwn;
6690 memcpy(&wwn, (char *)mb->un.varRDnvp.portname,
6691 sizeof(uint64_t));
6692 /* wwn is WWPN of HBA instance */
6693 wwn = cpu_to_be64(wwn);
6694 phba->sli4_hba.wwpn.u.name = wwn;
6695
6696 /* Check to see if it matches any module parameter */
6697 for (i = 0; i < lpfc_enable_nvmet_cnt; i++) {
6698 if (wwn == lpfc_enable_nvmet[i]) {
James Smart7d708032017-03-08 14:36:01 -08006699#if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
James Smart3c603be2017-05-15 15:20:44 -07006700 if (lpfc_nvmet_mem_alloc(phba))
6701 break;
6702
6703 phba->nvmet_support = 1; /* a match */
6704
James Smartf358dd02017-02-12 13:52:34 -08006705 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6706 "6017 NVME Target %016llx\n",
6707 wwn);
James Smart7d708032017-03-08 14:36:01 -08006708#else
6709 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6710 "6021 Can't enable NVME Target."
6711 " NVME_TARGET_FC infrastructure"
6712 " is not in kernel\n");
6713#endif
James Smartc4908502019-01-28 11:14:28 -08006714 /* Not supported for NVMET */
6715 phba->cfg_xri_rebalancing = 0;
James Smart3c603be2017-05-15 15:20:44 -07006716 break;
James Smartf358dd02017-02-12 13:52:34 -08006717 }
6718 }
6719 }
James Smart895427b2017-02-12 13:52:30 -08006720
6721 lpfc_nvme_mod_param_dep(phba);
6722
James Smartfedd3b72011-02-16 12:39:24 -05006723 /* Get the Supported Pages if PORT_CAPABILITIES is supported by port. */
James Smartcb5172e2010-03-15 11:25:07 -04006724 lpfc_supported_pages(mboxq);
6725 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smartfedd3b72011-02-16 12:39:24 -05006726 if (!rc) {
6727 mqe = &mboxq->u.mqe;
6728 memcpy(&pn_page[0], ((uint8_t *)&mqe->un.supp_pages.word3),
6729 LPFC_MAX_SUPPORTED_PAGES);
6730 for (i = 0; i < LPFC_MAX_SUPPORTED_PAGES; i++) {
6731 switch (pn_page[i]) {
6732 case LPFC_SLI4_PARAMETERS:
6733 phba->sli4_hba.pc_sli4_params.supported = 1;
6734 break;
6735 default:
6736 break;
6737 }
6738 }
6739 /* Read the port's SLI4 Parameters capabilities if supported. */
6740 if (phba->sli4_hba.pc_sli4_params.supported)
6741 rc = lpfc_pc_sli4_params_get(phba, mboxq);
6742 if (rc) {
6743 mempool_free(mboxq, phba->mbox_mem_pool);
6744 rc = -EIO;
6745 goto out_free_bsmbx;
James Smartcb5172e2010-03-15 11:25:07 -04006746 }
6747 }
James Smart65791f12016-07-06 12:35:56 -07006748
James Smartfedd3b72011-02-16 12:39:24 -05006749 /*
6750 * Get sli4 parameters that override parameters from Port capabilities.
James Smart6d368e52011-05-24 11:44:12 -04006751 * If this call fails, it isn't critical unless the SLI4 parameters come
6752 * back in conflict.
James Smartfedd3b72011-02-16 12:39:24 -05006753 */
James Smart6d368e52011-05-24 11:44:12 -04006754 rc = lpfc_get_sli4_parameters(phba, mboxq);
6755 if (rc) {
James Smartb92dc722018-05-24 21:09:01 -07006756 if_type = bf_get(lpfc_sli_intf_if_type,
6757 &phba->sli4_hba.sli_intf);
6758 if_fam = bf_get(lpfc_sli_intf_sli_family,
6759 &phba->sli4_hba.sli_intf);
James Smart6d368e52011-05-24 11:44:12 -04006760 if (phba->sli4_hba.extents_in_use &&
6761 phba->sli4_hba.rpi_hdrs_in_use) {
6762 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6763 "2999 Unsupported SLI4 Parameters "
6764 "Extents and RPI headers enabled.\n");
James Smartb92dc722018-05-24 21:09:01 -07006765 if (if_type == LPFC_SLI_INTF_IF_TYPE_0 &&
6766 if_fam == LPFC_SLI_INTF_FAMILY_BE2) {
6767 mempool_free(mboxq, phba->mbox_mem_pool);
6768 rc = -EIO;
6769 goto out_free_bsmbx;
6770 }
James Smart6d368e52011-05-24 11:44:12 -04006771 }
James Smartb92dc722018-05-24 21:09:01 -07006772 if (!(if_type == LPFC_SLI_INTF_IF_TYPE_0 &&
6773 if_fam == LPFC_SLI_INTF_FAMILY_BE2)) {
6774 mempool_free(mboxq, phba->mbox_mem_pool);
6775 rc = -EIO;
6776 goto out_free_bsmbx;
6777 }
James Smart6d368e52011-05-24 11:44:12 -04006778 }
James Smart895427b2017-02-12 13:52:30 -08006779
James Smartcb5172e2010-03-15 11:25:07 -04006780 mempool_free(mboxq, phba->mbox_mem_pool);
James Smart1ba981f2014-02-20 09:56:45 -05006781
6782 /* Verify OAS is supported */
6783 lpfc_sli4_oas_verify(phba);
James Smart1ba981f2014-02-20 09:56:45 -05006784
James Smartd2cc9bc2018-09-10 10:30:50 -07006785 /* Verify RAS support on adapter */
6786 lpfc_sli4_ras_init(phba);
6787
James Smart5350d872011-10-10 21:33:49 -04006788 /* Verify all the SLI4 queues */
6789 rc = lpfc_sli4_queue_verify(phba);
James Smartda0436e2009-05-22 14:51:39 -04006790 if (rc)
6791 goto out_free_bsmbx;
6792
6793 /* Create driver internal CQE event pool */
6794 rc = lpfc_sli4_cq_event_pool_create(phba);
6795 if (rc)
James Smart5350d872011-10-10 21:33:49 -04006796 goto out_free_bsmbx;
James Smartda0436e2009-05-22 14:51:39 -04006797
James Smart8a9d2e82012-05-09 21:16:12 -04006798 /* Initialize sgl lists per host */
6799 lpfc_init_sgl_list(phba);
6800
6801 /* Allocate and initialize active sgl array */
James Smartda0436e2009-05-22 14:51:39 -04006802 rc = lpfc_init_active_sgl_array(phba);
6803 if (rc) {
6804 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6805 "1430 Failed to initialize sgl list.\n");
James Smart8a9d2e82012-05-09 21:16:12 -04006806 goto out_destroy_cq_event_pool;
James Smartda0436e2009-05-22 14:51:39 -04006807 }
James Smartda0436e2009-05-22 14:51:39 -04006808 rc = lpfc_sli4_init_rpi_hdrs(phba);
6809 if (rc) {
6810 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6811 "1432 Failed to initialize rpi headers.\n");
6812 goto out_free_active_sgl;
6813 }
6814
James Smarta93ff372010-10-22 11:06:08 -04006815 /* Allocate eligible FCF bmask memory for FCF roundrobin failover */
James Smart0c9ab6f2010-02-26 14:15:57 -05006816 longs = (LPFC_SLI4_FCF_TBL_INDX_MAX + BITS_PER_LONG - 1)/BITS_PER_LONG;
Kees Cook6396bb22018-06-12 14:03:40 -07006817 phba->fcf.fcf_rr_bmask = kcalloc(longs, sizeof(unsigned long),
James Smart0c9ab6f2010-02-26 14:15:57 -05006818 GFP_KERNEL);
6819 if (!phba->fcf.fcf_rr_bmask) {
6820 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6821 "2759 Failed allocate memory for FCF round "
6822 "robin failover bmask\n");
James Smart05580562011-05-24 11:40:48 -04006823 rc = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -05006824 goto out_remove_rpi_hdrs;
6825 }
6826
James Smart6a828b02019-01-28 11:14:31 -08006827 phba->sli4_hba.hba_eq_hdl = kcalloc(phba->cfg_irq_chann,
James Smartcdb42be2019-01-28 11:14:21 -08006828 sizeof(struct lpfc_hba_eq_hdl),
6829 GFP_KERNEL);
James Smart895427b2017-02-12 13:52:30 -08006830 if (!phba->sli4_hba.hba_eq_hdl) {
James Smart67d12732012-08-03 12:36:13 -04006831 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6832 "2572 Failed allocate memory for "
6833 "fast-path per-EQ handle array\n");
6834 rc = -ENOMEM;
6835 goto out_free_fcf_rr_bmask;
James Smartda0436e2009-05-22 14:51:39 -04006836 }
6837
James Smart222e9232019-01-28 11:14:35 -08006838 phba->sli4_hba.cpu_map = kcalloc(phba->sli4_hba.num_possible_cpu,
James Smart895427b2017-02-12 13:52:30 -08006839 sizeof(struct lpfc_vector_map_info),
6840 GFP_KERNEL);
James Smart7bb03bb2013-04-17 20:19:16 -04006841 if (!phba->sli4_hba.cpu_map) {
6842 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6843 "3327 Failed allocate memory for msi-x "
6844 "interrupt vector mapping\n");
6845 rc = -ENOMEM;
James Smart895427b2017-02-12 13:52:30 -08006846 goto out_free_hba_eq_hdl;
James Smart7bb03bb2013-04-17 20:19:16 -04006847 }
James Smartb246de12013-05-31 17:03:07 -04006848
James Smart32517fc2019-01-28 11:14:33 -08006849 phba->sli4_hba.eq_info = alloc_percpu(struct lpfc_eq_intr_info);
6850 if (!phba->sli4_hba.eq_info) {
6851 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6852 "3321 Failed allocation for per_cpu stats\n");
6853 rc = -ENOMEM;
6854 goto out_free_hba_cpu_map;
6855 }
James Smart912e3ac2011-05-24 11:42:11 -04006856 /*
6857 * Enable sr-iov virtual functions if supported and configured
6858 * through the module parameter.
6859 */
6860 if (phba->cfg_sriov_nr_virtfn > 0) {
6861 rc = lpfc_sli_probe_sriov_nr_virtfn(phba,
6862 phba->cfg_sriov_nr_virtfn);
6863 if (rc) {
6864 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6865 "3020 Requested number of SR-IOV "
6866 "virtual functions (%d) is not "
6867 "supported\n",
6868 phba->cfg_sriov_nr_virtfn);
6869 phba->cfg_sriov_nr_virtfn = 0;
6870 }
6871 }
6872
James Smart5248a742011-07-22 18:37:06 -04006873 return 0;
James Smartda0436e2009-05-22 14:51:39 -04006874
James Smart32517fc2019-01-28 11:14:33 -08006875out_free_hba_cpu_map:
6876 kfree(phba->sli4_hba.cpu_map);
James Smart895427b2017-02-12 13:52:30 -08006877out_free_hba_eq_hdl:
6878 kfree(phba->sli4_hba.hba_eq_hdl);
James Smart0c9ab6f2010-02-26 14:15:57 -05006879out_free_fcf_rr_bmask:
6880 kfree(phba->fcf.fcf_rr_bmask);
James Smartda0436e2009-05-22 14:51:39 -04006881out_remove_rpi_hdrs:
6882 lpfc_sli4_remove_rpi_hdrs(phba);
6883out_free_active_sgl:
6884 lpfc_free_active_sgl(phba);
James Smartda0436e2009-05-22 14:51:39 -04006885out_destroy_cq_event_pool:
6886 lpfc_sli4_cq_event_pool_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04006887out_free_bsmbx:
6888 lpfc_destroy_bootstrap_mbox(phba);
6889out_free_mem:
6890 lpfc_mem_free(phba);
6891 return rc;
6892}
6893
6894/**
6895 * lpfc_sli4_driver_resource_unset - Unset drvr internal resources for SLI4 dev
6896 * @phba: pointer to lpfc hba data structure.
6897 *
6898 * This routine is invoked to unset the driver internal resources set up
6899 * specific for supporting the SLI-4 HBA device it attached to.
6900 **/
6901static void
6902lpfc_sli4_driver_resource_unset(struct lpfc_hba *phba)
6903{
6904 struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
6905
James Smart32517fc2019-01-28 11:14:33 -08006906 free_percpu(phba->sli4_hba.eq_info);
6907
James Smart7bb03bb2013-04-17 20:19:16 -04006908 /* Free memory allocated for msi-x interrupt vector to CPU mapping */
6909 kfree(phba->sli4_hba.cpu_map);
James Smart222e9232019-01-28 11:14:35 -08006910 phba->sli4_hba.num_possible_cpu = 0;
James Smart7bb03bb2013-04-17 20:19:16 -04006911 phba->sli4_hba.num_present_cpu = 0;
James Smart76fd07a2014-02-20 09:57:18 -05006912 phba->sli4_hba.curr_disp_cpu = 0;
James Smart7bb03bb2013-04-17 20:19:16 -04006913
James Smartda0436e2009-05-22 14:51:39 -04006914 /* Free memory allocated for fast-path work queue handles */
James Smart895427b2017-02-12 13:52:30 -08006915 kfree(phba->sli4_hba.hba_eq_hdl);
James Smartda0436e2009-05-22 14:51:39 -04006916
6917 /* Free the allocated rpi headers. */
6918 lpfc_sli4_remove_rpi_hdrs(phba);
James Smartd11e31d2009-06-10 17:23:06 -04006919 lpfc_sli4_remove_rpis(phba);
James Smartda0436e2009-05-22 14:51:39 -04006920
James Smart0c9ab6f2010-02-26 14:15:57 -05006921 /* Free eligible FCF index bmask */
6922 kfree(phba->fcf.fcf_rr_bmask);
6923
James Smartda0436e2009-05-22 14:51:39 -04006924 /* Free the ELS sgl list */
6925 lpfc_free_active_sgl(phba);
James Smart8a9d2e82012-05-09 21:16:12 -04006926 lpfc_free_els_sgl_list(phba);
James Smartf358dd02017-02-12 13:52:34 -08006927 lpfc_free_nvmet_sgl_list(phba);
James Smartda0436e2009-05-22 14:51:39 -04006928
James Smartda0436e2009-05-22 14:51:39 -04006929 /* Free the completion queue EQ event pool */
6930 lpfc_sli4_cq_event_release_all(phba);
6931 lpfc_sli4_cq_event_pool_destroy(phba);
6932
James Smart6d368e52011-05-24 11:44:12 -04006933 /* Release resource identifiers. */
6934 lpfc_sli4_dealloc_resource_identifiers(phba);
6935
James Smartda0436e2009-05-22 14:51:39 -04006936 /* Free the bsmbx region. */
6937 lpfc_destroy_bootstrap_mbox(phba);
6938
6939 /* Free the SLI Layer memory with SLI4 HBAs */
6940 lpfc_mem_free_all(phba);
6941
6942 /* Free the current connect table */
6943 list_for_each_entry_safe(conn_entry, next_conn_entry,
James Smart4d9ab992009-10-02 15:16:39 -04006944 &phba->fcf_conn_rec_list, list) {
6945 list_del_init(&conn_entry->list);
James Smartda0436e2009-05-22 14:51:39 -04006946 kfree(conn_entry);
James Smart4d9ab992009-10-02 15:16:39 -04006947 }
James Smartda0436e2009-05-22 14:51:39 -04006948
6949 return;
6950}
6951
6952/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006953 * lpfc_init_api_table_setup - Set up init api function jump table
James Smart3772a992009-05-22 14:50:54 -04006954 * @phba: The hba struct for which this call is being executed.
6955 * @dev_grp: The HBA PCI-Device group number.
6956 *
6957 * This routine sets up the device INIT interface API function jump table
6958 * in @phba struct.
6959 *
6960 * Returns: 0 - success, -ENODEV - failure.
6961 **/
6962int
6963lpfc_init_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
6964{
James Smart84d1b002010-02-12 14:42:33 -05006965 phba->lpfc_hba_init_link = lpfc_hba_init_link;
6966 phba->lpfc_hba_down_link = lpfc_hba_down_link;
James Smart7f860592011-03-11 16:05:52 -05006967 phba->lpfc_selective_reset = lpfc_selective_reset;
James Smart3772a992009-05-22 14:50:54 -04006968 switch (dev_grp) {
6969 case LPFC_PCI_DEV_LP:
6970 phba->lpfc_hba_down_post = lpfc_hba_down_post_s3;
6971 phba->lpfc_handle_eratt = lpfc_handle_eratt_s3;
6972 phba->lpfc_stop_port = lpfc_stop_port_s3;
6973 break;
James Smartda0436e2009-05-22 14:51:39 -04006974 case LPFC_PCI_DEV_OC:
6975 phba->lpfc_hba_down_post = lpfc_hba_down_post_s4;
6976 phba->lpfc_handle_eratt = lpfc_handle_eratt_s4;
6977 phba->lpfc_stop_port = lpfc_stop_port_s4;
6978 break;
James Smart3772a992009-05-22 14:50:54 -04006979 default:
6980 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6981 "1431 Invalid HBA PCI-device group: 0x%x\n",
6982 dev_grp);
6983 return -ENODEV;
6984 break;
6985 }
6986 return 0;
6987}
6988
6989/**
James Smart3772a992009-05-22 14:50:54 -04006990 * lpfc_setup_driver_resource_phase2 - Phase2 setup driver internal resources.
6991 * @phba: pointer to lpfc hba data structure.
6992 *
6993 * This routine is invoked to set up the driver internal resources after the
6994 * device specific resource setup to support the HBA device it attached to.
6995 *
6996 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006997 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04006998 * other values - error
6999 **/
7000static int
7001lpfc_setup_driver_resource_phase2(struct lpfc_hba *phba)
7002{
7003 int error;
7004
7005 /* Startup the kernel thread for this host adapter. */
7006 phba->worker_thread = kthread_run(lpfc_do_work, phba,
7007 "lpfc_worker_%d", phba->brd_no);
7008 if (IS_ERR(phba->worker_thread)) {
7009 error = PTR_ERR(phba->worker_thread);
7010 return error;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05007011 }
7012
James Smart3772a992009-05-22 14:50:54 -04007013 return 0;
7014}
7015
7016/**
7017 * lpfc_unset_driver_resource_phase2 - Phase2 unset driver internal resources.
7018 * @phba: pointer to lpfc hba data structure.
7019 *
7020 * This routine is invoked to unset the driver internal resources set up after
7021 * the device specific resource setup for supporting the HBA device it
7022 * attached to.
7023 **/
7024static void
7025lpfc_unset_driver_resource_phase2(struct lpfc_hba *phba)
7026{
Dick Kennedyf485c182017-09-29 17:34:34 -07007027 if (phba->wq) {
7028 flush_workqueue(phba->wq);
7029 destroy_workqueue(phba->wq);
7030 phba->wq = NULL;
7031 }
7032
James Smart3772a992009-05-22 14:50:54 -04007033 /* Stop kernel worker thread */
James Smart0cdb84e2018-04-09 14:24:26 -07007034 if (phba->worker_thread)
7035 kthread_stop(phba->worker_thread);
James Smart3772a992009-05-22 14:50:54 -04007036}
7037
7038/**
7039 * lpfc_free_iocb_list - Free iocb list.
7040 * @phba: pointer to lpfc hba data structure.
7041 *
7042 * This routine is invoked to free the driver's IOCB list and memory.
7043 **/
James Smart6c621a22017-05-15 15:20:45 -07007044void
James Smart3772a992009-05-22 14:50:54 -04007045lpfc_free_iocb_list(struct lpfc_hba *phba)
7046{
7047 struct lpfc_iocbq *iocbq_entry = NULL, *iocbq_next = NULL;
7048
7049 spin_lock_irq(&phba->hbalock);
7050 list_for_each_entry_safe(iocbq_entry, iocbq_next,
7051 &phba->lpfc_iocb_list, list) {
7052 list_del(&iocbq_entry->list);
7053 kfree(iocbq_entry);
7054 phba->total_iocbq_bufs--;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05007055 }
James Smart3772a992009-05-22 14:50:54 -04007056 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007057
James Smart3772a992009-05-22 14:50:54 -04007058 return;
7059}
dea31012005-04-17 16:05:31 -05007060
James Smart3772a992009-05-22 14:50:54 -04007061/**
7062 * lpfc_init_iocb_list - Allocate and initialize iocb list.
7063 * @phba: pointer to lpfc hba data structure.
7064 *
7065 * This routine is invoked to allocate and initizlize the driver's IOCB
7066 * list and set up the IOCB tag array accordingly.
7067 *
7068 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007069 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04007070 * other values - error
7071 **/
James Smart6c621a22017-05-15 15:20:45 -07007072int
James Smart3772a992009-05-22 14:50:54 -04007073lpfc_init_iocb_list(struct lpfc_hba *phba, int iocb_count)
7074{
7075 struct lpfc_iocbq *iocbq_entry = NULL;
7076 uint16_t iotag;
7077 int i;
dea31012005-04-17 16:05:31 -05007078
7079 /* Initialize and populate the iocb list per host. */
7080 INIT_LIST_HEAD(&phba->lpfc_iocb_list);
James Smart3772a992009-05-22 14:50:54 -04007081 for (i = 0; i < iocb_count; i++) {
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07007082 iocbq_entry = kzalloc(sizeof(struct lpfc_iocbq), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05007083 if (iocbq_entry == NULL) {
7084 printk(KERN_ERR "%s: only allocated %d iocbs of "
7085 "expected %d count. Unloading driver.\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07007086 __func__, i, LPFC_IOCB_LIST_CNT);
dea31012005-04-17 16:05:31 -05007087 goto out_free_iocbq;
7088 }
7089
James Bottomley604a3e32005-10-29 10:28:33 -05007090 iotag = lpfc_sli_next_iotag(phba, iocbq_entry);
7091 if (iotag == 0) {
James Smart3772a992009-05-22 14:50:54 -04007092 kfree(iocbq_entry);
James Bottomley604a3e32005-10-29 10:28:33 -05007093 printk(KERN_ERR "%s: failed to allocate IOTAG. "
James Smart3772a992009-05-22 14:50:54 -04007094 "Unloading driver.\n", __func__);
James Bottomley604a3e32005-10-29 10:28:33 -05007095 goto out_free_iocbq;
7096 }
James Smart6d368e52011-05-24 11:44:12 -04007097 iocbq_entry->sli4_lxritag = NO_XRI;
James Smart3772a992009-05-22 14:50:54 -04007098 iocbq_entry->sli4_xritag = NO_XRI;
James Smart2e0fef82007-06-17 19:56:36 -05007099
7100 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007101 list_add(&iocbq_entry->list, &phba->lpfc_iocb_list);
7102 phba->total_iocbq_bufs++;
James Smart2e0fef82007-06-17 19:56:36 -05007103 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007104 }
7105
James Smart3772a992009-05-22 14:50:54 -04007106 return 0;
7107
7108out_free_iocbq:
7109 lpfc_free_iocb_list(phba);
7110
7111 return -ENOMEM;
7112}
7113
7114/**
James Smart8a9d2e82012-05-09 21:16:12 -04007115 * lpfc_free_sgl_list - Free a given sgl list.
James Smartda0436e2009-05-22 14:51:39 -04007116 * @phba: pointer to lpfc hba data structure.
James Smart8a9d2e82012-05-09 21:16:12 -04007117 * @sglq_list: pointer to the head of sgl list.
James Smartda0436e2009-05-22 14:51:39 -04007118 *
James Smart8a9d2e82012-05-09 21:16:12 -04007119 * This routine is invoked to free a give sgl list and memory.
James Smartda0436e2009-05-22 14:51:39 -04007120 **/
James Smart8a9d2e82012-05-09 21:16:12 -04007121void
7122lpfc_free_sgl_list(struct lpfc_hba *phba, struct list_head *sglq_list)
James Smartda0436e2009-05-22 14:51:39 -04007123{
7124 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
James Smart8a9d2e82012-05-09 21:16:12 -04007125
7126 list_for_each_entry_safe(sglq_entry, sglq_next, sglq_list, list) {
7127 list_del(&sglq_entry->list);
7128 lpfc_mbuf_free(phba, sglq_entry->virt, sglq_entry->phys);
7129 kfree(sglq_entry);
7130 }
7131}
7132
7133/**
7134 * lpfc_free_els_sgl_list - Free els sgl list.
7135 * @phba: pointer to lpfc hba data structure.
7136 *
7137 * This routine is invoked to free the driver's els sgl list and memory.
7138 **/
7139static void
7140lpfc_free_els_sgl_list(struct lpfc_hba *phba)
7141{
James Smartda0436e2009-05-22 14:51:39 -04007142 LIST_HEAD(sglq_list);
James Smartda0436e2009-05-22 14:51:39 -04007143
James Smart8a9d2e82012-05-09 21:16:12 -04007144 /* Retrieve all els sgls from driver list */
James Smartda0436e2009-05-22 14:51:39 -04007145 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -08007146 spin_lock(&phba->sli4_hba.sgl_list_lock);
7147 list_splice_init(&phba->sli4_hba.lpfc_els_sgl_list, &sglq_list);
7148 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smartda0436e2009-05-22 14:51:39 -04007149 spin_unlock_irq(&phba->hbalock);
7150
James Smart8a9d2e82012-05-09 21:16:12 -04007151 /* Now free the sgl list */
7152 lpfc_free_sgl_list(phba, &sglq_list);
James Smartda0436e2009-05-22 14:51:39 -04007153}
7154
7155/**
James Smartf358dd02017-02-12 13:52:34 -08007156 * lpfc_free_nvmet_sgl_list - Free nvmet sgl list.
7157 * @phba: pointer to lpfc hba data structure.
7158 *
7159 * This routine is invoked to free the driver's nvmet sgl list and memory.
7160 **/
7161static void
7162lpfc_free_nvmet_sgl_list(struct lpfc_hba *phba)
7163{
7164 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
7165 LIST_HEAD(sglq_list);
7166
7167 /* Retrieve all nvmet sgls from driver list */
7168 spin_lock_irq(&phba->hbalock);
7169 spin_lock(&phba->sli4_hba.sgl_list_lock);
7170 list_splice_init(&phba->sli4_hba.lpfc_nvmet_sgl_list, &sglq_list);
7171 spin_unlock(&phba->sli4_hba.sgl_list_lock);
7172 spin_unlock_irq(&phba->hbalock);
7173
7174 /* Now free the sgl list */
7175 list_for_each_entry_safe(sglq_entry, sglq_next, &sglq_list, list) {
7176 list_del(&sglq_entry->list);
7177 lpfc_nvmet_buf_free(phba, sglq_entry->virt, sglq_entry->phys);
7178 kfree(sglq_entry);
7179 }
Dick Kennedy4b40d022017-08-23 16:55:38 -07007180
7181 /* Update the nvmet_xri_cnt to reflect no current sgls.
7182 * The next initialization cycle sets the count and allocates
7183 * the sgls over again.
7184 */
7185 phba->sli4_hba.nvmet_xri_cnt = 0;
James Smartf358dd02017-02-12 13:52:34 -08007186}
7187
7188/**
James Smartda0436e2009-05-22 14:51:39 -04007189 * lpfc_init_active_sgl_array - Allocate the buf to track active ELS XRIs.
7190 * @phba: pointer to lpfc hba data structure.
7191 *
7192 * This routine is invoked to allocate the driver's active sgl memory.
7193 * This array will hold the sglq_entry's for active IOs.
7194 **/
7195static int
7196lpfc_init_active_sgl_array(struct lpfc_hba *phba)
7197{
7198 int size;
7199 size = sizeof(struct lpfc_sglq *);
7200 size *= phba->sli4_hba.max_cfg_param.max_xri;
7201
7202 phba->sli4_hba.lpfc_sglq_active_list =
7203 kzalloc(size, GFP_KERNEL);
7204 if (!phba->sli4_hba.lpfc_sglq_active_list)
7205 return -ENOMEM;
7206 return 0;
7207}
7208
7209/**
7210 * lpfc_free_active_sgl - Free the buf that tracks active ELS XRIs.
7211 * @phba: pointer to lpfc hba data structure.
7212 *
7213 * This routine is invoked to walk through the array of active sglq entries
7214 * and free all of the resources.
7215 * This is just a place holder for now.
7216 **/
7217static void
7218lpfc_free_active_sgl(struct lpfc_hba *phba)
7219{
7220 kfree(phba->sli4_hba.lpfc_sglq_active_list);
7221}
7222
7223/**
7224 * lpfc_init_sgl_list - Allocate and initialize sgl list.
7225 * @phba: pointer to lpfc hba data structure.
7226 *
7227 * This routine is invoked to allocate and initizlize the driver's sgl
7228 * list and set up the sgl xritag tag array accordingly.
7229 *
James Smartda0436e2009-05-22 14:51:39 -04007230 **/
James Smart8a9d2e82012-05-09 21:16:12 -04007231static void
James Smartda0436e2009-05-22 14:51:39 -04007232lpfc_init_sgl_list(struct lpfc_hba *phba)
7233{
James Smartda0436e2009-05-22 14:51:39 -04007234 /* Initialize and populate the sglq list per host/VF. */
James Smart895427b2017-02-12 13:52:30 -08007235 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_els_sgl_list);
James Smartda0436e2009-05-22 14:51:39 -04007236 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_els_sgl_list);
James Smartf358dd02017-02-12 13:52:34 -08007237 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_nvmet_sgl_list);
James Smart86c67372017-04-21 16:05:04 -07007238 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_nvmet_ctx_list);
James Smartda0436e2009-05-22 14:51:39 -04007239
James Smart8a9d2e82012-05-09 21:16:12 -04007240 /* els xri-sgl book keeping */
7241 phba->sli4_hba.els_xri_cnt = 0;
James Smartda0436e2009-05-22 14:51:39 -04007242
James Smart895427b2017-02-12 13:52:30 -08007243 /* nvme xri-buffer book keeping */
James Smart5e5b5112019-01-28 11:14:22 -08007244 phba->sli4_hba.io_xri_cnt = 0;
James Smartda0436e2009-05-22 14:51:39 -04007245}
7246
7247/**
7248 * lpfc_sli4_init_rpi_hdrs - Post the rpi header memory region to the port
7249 * @phba: pointer to lpfc hba data structure.
7250 *
7251 * This routine is invoked to post rpi header templates to the
James Smart88a2cfb2011-07-22 18:36:33 -04007252 * port for those SLI4 ports that do not support extents. This routine
James Smartda0436e2009-05-22 14:51:39 -04007253 * posts a PAGE_SIZE memory region to the port to hold up to
James Smart88a2cfb2011-07-22 18:36:33 -04007254 * PAGE_SIZE modulo 64 rpi context headers. This is an initialization routine
7255 * and should be called only when interrupts are disabled.
James Smartda0436e2009-05-22 14:51:39 -04007256 *
7257 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007258 * 0 - successful
James Smart88a2cfb2011-07-22 18:36:33 -04007259 * -ERROR - otherwise.
James Smartda0436e2009-05-22 14:51:39 -04007260 **/
7261int
7262lpfc_sli4_init_rpi_hdrs(struct lpfc_hba *phba)
7263{
7264 int rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04007265 struct lpfc_rpi_hdr *rpi_hdr;
7266
7267 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_hdr_list);
James Smartff78d8f2011-12-13 13:21:35 -05007268 if (!phba->sli4_hba.rpi_hdrs_in_use)
James Smart6d368e52011-05-24 11:44:12 -04007269 return rc;
James Smart6d368e52011-05-24 11:44:12 -04007270 if (phba->sli4_hba.extents_in_use)
7271 return -EIO;
James Smartda0436e2009-05-22 14:51:39 -04007272
7273 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
7274 if (!rpi_hdr) {
7275 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7276 "0391 Error during rpi post operation\n");
7277 lpfc_sli4_remove_rpis(phba);
7278 rc = -ENODEV;
7279 }
7280
7281 return rc;
7282}
7283
7284/**
7285 * lpfc_sli4_create_rpi_hdr - Allocate an rpi header memory region
7286 * @phba: pointer to lpfc hba data structure.
7287 *
7288 * This routine is invoked to allocate a single 4KB memory region to
7289 * support rpis and stores them in the phba. This single region
7290 * provides support for up to 64 rpis. The region is used globally
7291 * by the device.
7292 *
7293 * Returns:
7294 * A valid rpi hdr on success.
7295 * A NULL pointer on any failure.
7296 **/
7297struct lpfc_rpi_hdr *
7298lpfc_sli4_create_rpi_hdr(struct lpfc_hba *phba)
7299{
7300 uint16_t rpi_limit, curr_rpi_range;
7301 struct lpfc_dmabuf *dmabuf;
7302 struct lpfc_rpi_hdr *rpi_hdr;
7303
James Smart6d368e52011-05-24 11:44:12 -04007304 /*
7305 * If the SLI4 port supports extents, posting the rpi header isn't
7306 * required. Set the expected maximum count and let the actual value
7307 * get set when extents are fully allocated.
7308 */
7309 if (!phba->sli4_hba.rpi_hdrs_in_use)
7310 return NULL;
7311 if (phba->sli4_hba.extents_in_use)
7312 return NULL;
7313
7314 /* The limit on the logical index is just the max_rpi count. */
James Smart845d9e82017-05-15 15:20:38 -07007315 rpi_limit = phba->sli4_hba.max_cfg_param.max_rpi;
James Smartda0436e2009-05-22 14:51:39 -04007316
7317 spin_lock_irq(&phba->hbalock);
James Smart6d368e52011-05-24 11:44:12 -04007318 /*
7319 * Establish the starting RPI in this header block. The starting
7320 * rpi is normalized to a zero base because the physical rpi is
7321 * port based.
7322 */
James Smart97f2ecf2012-03-01 22:35:23 -05007323 curr_rpi_range = phba->sli4_hba.next_rpi;
James Smartda0436e2009-05-22 14:51:39 -04007324 spin_unlock_irq(&phba->hbalock);
7325
James Smart845d9e82017-05-15 15:20:38 -07007326 /* Reached full RPI range */
7327 if (curr_rpi_range == rpi_limit)
James Smart6d368e52011-05-24 11:44:12 -04007328 return NULL;
James Smart845d9e82017-05-15 15:20:38 -07007329
James Smartda0436e2009-05-22 14:51:39 -04007330 /*
7331 * First allocate the protocol header region for the port. The
7332 * port expects a 4KB DMA-mapped memory region that is 4K aligned.
7333 */
7334 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
7335 if (!dmabuf)
7336 return NULL;
7337
Luis Chamberlain750afb02019-01-04 09:23:09 +01007338 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
7339 LPFC_HDR_TEMPLATE_SIZE,
7340 &dmabuf->phys, GFP_KERNEL);
James Smartda0436e2009-05-22 14:51:39 -04007341 if (!dmabuf->virt) {
7342 rpi_hdr = NULL;
7343 goto err_free_dmabuf;
7344 }
7345
James Smartda0436e2009-05-22 14:51:39 -04007346 if (!IS_ALIGNED(dmabuf->phys, LPFC_HDR_TEMPLATE_SIZE)) {
7347 rpi_hdr = NULL;
7348 goto err_free_coherent;
7349 }
7350
7351 /* Save the rpi header data for cleanup later. */
7352 rpi_hdr = kzalloc(sizeof(struct lpfc_rpi_hdr), GFP_KERNEL);
7353 if (!rpi_hdr)
7354 goto err_free_coherent;
7355
7356 rpi_hdr->dmabuf = dmabuf;
7357 rpi_hdr->len = LPFC_HDR_TEMPLATE_SIZE;
7358 rpi_hdr->page_count = 1;
7359 spin_lock_irq(&phba->hbalock);
James Smart6d368e52011-05-24 11:44:12 -04007360
7361 /* The rpi_hdr stores the logical index only. */
7362 rpi_hdr->start_rpi = curr_rpi_range;
James Smart845d9e82017-05-15 15:20:38 -07007363 rpi_hdr->next_rpi = phba->sli4_hba.next_rpi + LPFC_RPI_HDR_COUNT;
James Smartda0436e2009-05-22 14:51:39 -04007364 list_add_tail(&rpi_hdr->list, &phba->sli4_hba.lpfc_rpi_hdr_list);
7365
James Smartda0436e2009-05-22 14:51:39 -04007366 spin_unlock_irq(&phba->hbalock);
7367 return rpi_hdr;
7368
7369 err_free_coherent:
7370 dma_free_coherent(&phba->pcidev->dev, LPFC_HDR_TEMPLATE_SIZE,
7371 dmabuf->virt, dmabuf->phys);
7372 err_free_dmabuf:
7373 kfree(dmabuf);
7374 return NULL;
7375}
7376
7377/**
7378 * lpfc_sli4_remove_rpi_hdrs - Remove all rpi header memory regions
7379 * @phba: pointer to lpfc hba data structure.
7380 *
7381 * This routine is invoked to remove all memory resources allocated
James Smart6d368e52011-05-24 11:44:12 -04007382 * to support rpis for SLI4 ports not supporting extents. This routine
7383 * presumes the caller has released all rpis consumed by fabric or port
7384 * logins and is prepared to have the header pages removed.
James Smartda0436e2009-05-22 14:51:39 -04007385 **/
7386void
7387lpfc_sli4_remove_rpi_hdrs(struct lpfc_hba *phba)
7388{
7389 struct lpfc_rpi_hdr *rpi_hdr, *next_rpi_hdr;
7390
James Smart6d368e52011-05-24 11:44:12 -04007391 if (!phba->sli4_hba.rpi_hdrs_in_use)
7392 goto exit;
7393
James Smartda0436e2009-05-22 14:51:39 -04007394 list_for_each_entry_safe(rpi_hdr, next_rpi_hdr,
7395 &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
7396 list_del(&rpi_hdr->list);
7397 dma_free_coherent(&phba->pcidev->dev, rpi_hdr->len,
7398 rpi_hdr->dmabuf->virt, rpi_hdr->dmabuf->phys);
7399 kfree(rpi_hdr->dmabuf);
7400 kfree(rpi_hdr);
7401 }
James Smart6d368e52011-05-24 11:44:12 -04007402 exit:
7403 /* There are no rpis available to the port now. */
7404 phba->sli4_hba.next_rpi = 0;
James Smartda0436e2009-05-22 14:51:39 -04007405}
7406
7407/**
James Smart3772a992009-05-22 14:50:54 -04007408 * lpfc_hba_alloc - Allocate driver hba data structure for a device.
7409 * @pdev: pointer to pci device data structure.
7410 *
7411 * This routine is invoked to allocate the driver hba data structure for an
7412 * HBA device. If the allocation is successful, the phba reference to the
7413 * PCI device data structure is set.
7414 *
7415 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007416 * pointer to @phba - successful
James Smart3772a992009-05-22 14:50:54 -04007417 * NULL - error
7418 **/
7419static struct lpfc_hba *
7420lpfc_hba_alloc(struct pci_dev *pdev)
7421{
7422 struct lpfc_hba *phba;
7423
7424 /* Allocate memory for HBA structure */
7425 phba = kzalloc(sizeof(struct lpfc_hba), GFP_KERNEL);
7426 if (!phba) {
Jiri Slabye34ccdf2009-07-13 23:25:54 +02007427 dev_err(&pdev->dev, "failed to allocate hba struct\n");
James Smart3772a992009-05-22 14:50:54 -04007428 return NULL;
7429 }
7430
7431 /* Set reference to PCI device in HBA structure */
7432 phba->pcidev = pdev;
7433
7434 /* Assign an unused board number */
7435 phba->brd_no = lpfc_get_instance();
7436 if (phba->brd_no < 0) {
7437 kfree(phba);
7438 return NULL;
7439 }
James Smart65791f12016-07-06 12:35:56 -07007440 phba->eratt_poll_interval = LPFC_ERATT_POLL_INTERVAL;
James Smart3772a992009-05-22 14:50:54 -04007441
James Smart4fede782010-01-26 23:08:55 -05007442 spin_lock_init(&phba->ct_ev_lock);
James Smartf1c3b0f2009-07-19 10:01:32 -04007443 INIT_LIST_HEAD(&phba->ct_ev_waiters);
7444
James Smart3772a992009-05-22 14:50:54 -04007445 return phba;
7446}
7447
7448/**
7449 * lpfc_hba_free - Free driver hba data structure with a device.
7450 * @phba: pointer to lpfc hba data structure.
7451 *
7452 * This routine is invoked to free the driver hba data structure with an
7453 * HBA device.
7454 **/
7455static void
7456lpfc_hba_free(struct lpfc_hba *phba)
7457{
James Smart5e5b5112019-01-28 11:14:22 -08007458 if (phba->sli_rev == LPFC_SLI_REV4)
7459 kfree(phba->sli4_hba.hdwq);
7460
James Smart3772a992009-05-22 14:50:54 -04007461 /* Release the driver assigned board number */
7462 idr_remove(&lpfc_hba_index, phba->brd_no);
7463
James Smart895427b2017-02-12 13:52:30 -08007464 /* Free memory allocated with sli3 rings */
7465 kfree(phba->sli.sli3_ring);
7466 phba->sli.sli3_ring = NULL;
James Smart2a76a282012-08-03 12:35:54 -04007467
James Smart3772a992009-05-22 14:50:54 -04007468 kfree(phba);
7469 return;
7470}
7471
7472/**
7473 * lpfc_create_shost - Create hba physical port with associated scsi host.
7474 * @phba: pointer to lpfc hba data structure.
7475 *
7476 * This routine is invoked to create HBA physical port and associate a SCSI
7477 * host with it.
7478 *
7479 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007480 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04007481 * other values - error
7482 **/
7483static int
7484lpfc_create_shost(struct lpfc_hba *phba)
7485{
7486 struct lpfc_vport *vport;
7487 struct Scsi_Host *shost;
7488
7489 /* Initialize HBA FC structure */
dea31012005-04-17 16:05:31 -05007490 phba->fc_edtov = FF_DEF_EDTOV;
7491 phba->fc_ratov = FF_DEF_RATOV;
7492 phba->fc_altov = FF_DEF_ALTOV;
7493 phba->fc_arbtov = FF_DEF_ARBTOV;
7494
James Smartd7c47992010-06-08 18:31:54 -04007495 atomic_set(&phba->sdev_cnt, 0);
James Smart3de2a652007-08-02 11:09:59 -04007496 vport = lpfc_create_port(phba, phba->brd_no, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05007497 if (!vport)
James Smart3772a992009-05-22 14:50:54 -04007498 return -ENODEV;
James Smart2e0fef82007-06-17 19:56:36 -05007499
7500 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05007501 phba->pport = vport;
James Smart2ea259e2017-02-12 13:52:27 -08007502
James Smartf358dd02017-02-12 13:52:34 -08007503 if (phba->nvmet_support) {
7504 /* Only 1 vport (pport) will support NVME target */
7505 if (phba->txrdy_payload_pool == NULL) {
Romain Perier771db5c2017-07-06 10:13:05 +02007506 phba->txrdy_payload_pool = dma_pool_create(
7507 "txrdy_pool", &phba->pcidev->dev,
James Smartf358dd02017-02-12 13:52:34 -08007508 TXRDY_PAYLOAD_LEN, 16, 0);
7509 if (phba->txrdy_payload_pool) {
7510 phba->targetport = NULL;
7511 phba->cfg_enable_fc4_type = LPFC_ENABLE_NVME;
7512 lpfc_printf_log(phba, KERN_INFO,
7513 LOG_INIT | LOG_NVME_DISC,
7514 "6076 NVME Target Found\n");
7515 }
7516 }
7517 }
7518
James Smart858c9f62007-06-17 19:56:39 -05007519 lpfc_debugfs_initialize(vport);
James Smart3772a992009-05-22 14:50:54 -04007520 /* Put reference to SCSI host to driver's device private data */
7521 pci_set_drvdata(phba->pcidev, shost);
James Smart2e0fef82007-06-17 19:56:36 -05007522
James Smart4258e982015-12-16 18:11:58 -05007523 /*
7524 * At this point we are fully registered with PSA. In addition,
7525 * any initial discovery should be completed.
7526 */
7527 vport->load_flag |= FC_ALLOW_FDMI;
James Smart8663cbb2016-03-31 14:12:33 -07007528 if (phba->cfg_enable_SmartSAN ||
7529 (phba->cfg_fdmi_on == LPFC_FDMI_SUPPORT)) {
James Smart4258e982015-12-16 18:11:58 -05007530
7531 /* Setup appropriate attribute masks */
7532 vport->fdmi_hba_mask = LPFC_FDMI2_HBA_ATTR;
James Smart8663cbb2016-03-31 14:12:33 -07007533 if (phba->cfg_enable_SmartSAN)
James Smart4258e982015-12-16 18:11:58 -05007534 vport->fdmi_port_mask = LPFC_FDMI2_SMART_ATTR;
7535 else
7536 vport->fdmi_port_mask = LPFC_FDMI2_PORT_ATTR;
7537 }
James Smart3772a992009-05-22 14:50:54 -04007538 return 0;
7539}
dea31012005-04-17 16:05:31 -05007540
James Smart3772a992009-05-22 14:50:54 -04007541/**
7542 * lpfc_destroy_shost - Destroy hba physical port with associated scsi host.
7543 * @phba: pointer to lpfc hba data structure.
7544 *
7545 * This routine is invoked to destroy HBA physical port and the associated
7546 * SCSI host.
7547 **/
7548static void
7549lpfc_destroy_shost(struct lpfc_hba *phba)
7550{
7551 struct lpfc_vport *vport = phba->pport;
James Smart93996272008-08-24 21:50:30 -04007552
James Smart3772a992009-05-22 14:50:54 -04007553 /* Destroy physical port that associated with the SCSI host */
7554 destroy_port(vport);
7555
7556 return;
7557}
7558
7559/**
7560 * lpfc_setup_bg - Setup Block guard structures and debug areas.
7561 * @phba: pointer to lpfc hba data structure.
7562 * @shost: the shost to be used to detect Block guard settings.
7563 *
7564 * This routine sets up the local Block guard protocol settings for @shost.
7565 * This routine also allocates memory for debugging bg buffers.
7566 **/
7567static void
7568lpfc_setup_bg(struct lpfc_hba *phba, struct Scsi_Host *shost)
7569{
James Smartbbeb79b2012-06-12 13:54:27 -04007570 uint32_t old_mask;
7571 uint32_t old_guard;
7572
James Smart3772a992009-05-22 14:50:54 -04007573 int pagecnt = 10;
James Smartb3b98b72016-10-13 15:06:06 -07007574 if (phba->cfg_prot_mask && phba->cfg_prot_guard) {
James Smart3772a992009-05-22 14:50:54 -04007575 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7576 "1478 Registering BlockGuard with the "
7577 "SCSI layer\n");
James Smartbbeb79b2012-06-12 13:54:27 -04007578
James Smartb3b98b72016-10-13 15:06:06 -07007579 old_mask = phba->cfg_prot_mask;
7580 old_guard = phba->cfg_prot_guard;
James Smartbbeb79b2012-06-12 13:54:27 -04007581
7582 /* Only allow supported values */
James Smartb3b98b72016-10-13 15:06:06 -07007583 phba->cfg_prot_mask &= (SHOST_DIF_TYPE1_PROTECTION |
James Smartbbeb79b2012-06-12 13:54:27 -04007584 SHOST_DIX_TYPE0_PROTECTION |
7585 SHOST_DIX_TYPE1_PROTECTION);
James Smartb3b98b72016-10-13 15:06:06 -07007586 phba->cfg_prot_guard &= (SHOST_DIX_GUARD_IP |
7587 SHOST_DIX_GUARD_CRC);
James Smartbbeb79b2012-06-12 13:54:27 -04007588
7589 /* DIF Type 1 protection for profiles AST1/C1 is end to end */
James Smartb3b98b72016-10-13 15:06:06 -07007590 if (phba->cfg_prot_mask == SHOST_DIX_TYPE1_PROTECTION)
7591 phba->cfg_prot_mask |= SHOST_DIF_TYPE1_PROTECTION;
James Smartbbeb79b2012-06-12 13:54:27 -04007592
James Smartb3b98b72016-10-13 15:06:06 -07007593 if (phba->cfg_prot_mask && phba->cfg_prot_guard) {
7594 if ((old_mask != phba->cfg_prot_mask) ||
7595 (old_guard != phba->cfg_prot_guard))
James Smartbbeb79b2012-06-12 13:54:27 -04007596 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7597 "1475 Registering BlockGuard with the "
7598 "SCSI layer: mask %d guard %d\n",
James Smartb3b98b72016-10-13 15:06:06 -07007599 phba->cfg_prot_mask,
7600 phba->cfg_prot_guard);
James Smartbbeb79b2012-06-12 13:54:27 -04007601
James Smartb3b98b72016-10-13 15:06:06 -07007602 scsi_host_set_prot(shost, phba->cfg_prot_mask);
7603 scsi_host_set_guard(shost, phba->cfg_prot_guard);
James Smartbbeb79b2012-06-12 13:54:27 -04007604 } else
7605 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7606 "1479 Not Registering BlockGuard with the SCSI "
7607 "layer, Bad protection parameters: %d %d\n",
7608 old_mask, old_guard);
James Smart98c9ea52007-10-27 13:37:33 -04007609 }
James Smartbbeb79b2012-06-12 13:54:27 -04007610
James Smart81301a92008-12-04 22:39:46 -05007611 if (!_dump_buf_data) {
James Smart81301a92008-12-04 22:39:46 -05007612 while (pagecnt) {
7613 spin_lock_init(&_dump_buf_lock);
7614 _dump_buf_data =
7615 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
7616 if (_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -04007617 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
7618 "9043 BLKGRD: allocated %d pages for "
James Smart3772a992009-05-22 14:50:54 -04007619 "_dump_buf_data at 0x%p\n",
7620 (1 << pagecnt), _dump_buf_data);
James Smart81301a92008-12-04 22:39:46 -05007621 _dump_buf_data_order = pagecnt;
James Smart3772a992009-05-22 14:50:54 -04007622 memset(_dump_buf_data, 0,
7623 ((1 << PAGE_SHIFT) << pagecnt));
James Smart81301a92008-12-04 22:39:46 -05007624 break;
James Smart3772a992009-05-22 14:50:54 -04007625 } else
James Smart81301a92008-12-04 22:39:46 -05007626 --pagecnt;
James Smart81301a92008-12-04 22:39:46 -05007627 }
James Smart81301a92008-12-04 22:39:46 -05007628 if (!_dump_buf_data_order)
James Smart6a9c52c2009-10-02 15:16:51 -04007629 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
7630 "9044 BLKGRD: ERROR unable to allocate "
James Smart3772a992009-05-22 14:50:54 -04007631 "memory for hexdump\n");
7632 } else
James Smart6a9c52c2009-10-02 15:16:51 -04007633 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
7634 "9045 BLKGRD: already allocated _dump_buf_data=0x%p"
James Smart81301a92008-12-04 22:39:46 -05007635 "\n", _dump_buf_data);
James Smart81301a92008-12-04 22:39:46 -05007636 if (!_dump_buf_dif) {
James Smart81301a92008-12-04 22:39:46 -05007637 while (pagecnt) {
7638 _dump_buf_dif =
7639 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
7640 if (_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -04007641 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
7642 "9046 BLKGRD: allocated %d pages for "
James Smart3772a992009-05-22 14:50:54 -04007643 "_dump_buf_dif at 0x%p\n",
7644 (1 << pagecnt), _dump_buf_dif);
James Smart81301a92008-12-04 22:39:46 -05007645 _dump_buf_dif_order = pagecnt;
James Smart3772a992009-05-22 14:50:54 -04007646 memset(_dump_buf_dif, 0,
7647 ((1 << PAGE_SHIFT) << pagecnt));
James Smart81301a92008-12-04 22:39:46 -05007648 break;
James Smart3772a992009-05-22 14:50:54 -04007649 } else
James Smart81301a92008-12-04 22:39:46 -05007650 --pagecnt;
James Smart81301a92008-12-04 22:39:46 -05007651 }
James Smart81301a92008-12-04 22:39:46 -05007652 if (!_dump_buf_dif_order)
James Smart6a9c52c2009-10-02 15:16:51 -04007653 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
7654 "9047 BLKGRD: ERROR unable to allocate "
James Smart3772a992009-05-22 14:50:54 -04007655 "memory for hexdump\n");
7656 } else
James Smart6a9c52c2009-10-02 15:16:51 -04007657 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
7658 "9048 BLKGRD: already allocated _dump_buf_dif=0x%p\n",
James Smart3772a992009-05-22 14:50:54 -04007659 _dump_buf_dif);
7660}
7661
7662/**
7663 * lpfc_post_init_setup - Perform necessary device post initialization setup.
7664 * @phba: pointer to lpfc hba data structure.
7665 *
7666 * This routine is invoked to perform all the necessary post initialization
7667 * setup for the device.
7668 **/
7669static void
7670lpfc_post_init_setup(struct lpfc_hba *phba)
7671{
7672 struct Scsi_Host *shost;
7673 struct lpfc_adapter_event_header adapter_event;
7674
7675 /* Get the default values for Model Name and Description */
7676 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
7677
7678 /*
7679 * hba setup may have changed the hba_queue_depth so we need to
7680 * adjust the value of can_queue.
7681 */
7682 shost = pci_get_drvdata(phba->pcidev);
7683 shost->can_queue = phba->cfg_hba_queue_depth - 10;
James Smart858c9f62007-06-17 19:56:39 -05007684
7685 lpfc_host_attrib_init(shost);
7686
James Smart2e0fef82007-06-17 19:56:36 -05007687 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
7688 spin_lock_irq(shost->host_lock);
7689 lpfc_poll_start_timer(phba);
7690 spin_unlock_irq(shost->host_lock);
7691 }
James Smart8f6d98d2006-08-01 07:34:00 -04007692
James Smart93996272008-08-24 21:50:30 -04007693 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7694 "0428 Perform SCSI scan\n");
James Smartea2151b2008-09-07 11:52:10 -04007695 /* Send board arrival event to upper layer */
7696 adapter_event.event_type = FC_REG_ADAPTER_EVENT;
7697 adapter_event.subcategory = LPFC_EVENT_ARRIVAL;
7698 fc_host_post_vendor_event(shost, fc_get_event_number(),
James Smart3772a992009-05-22 14:50:54 -04007699 sizeof(adapter_event),
7700 (char *) &adapter_event,
7701 LPFC_NL_VENDOR_ID);
7702 return;
7703}
7704
7705/**
7706 * lpfc_sli_pci_mem_setup - Setup SLI3 HBA PCI memory space.
7707 * @phba: pointer to lpfc hba data structure.
7708 *
7709 * This routine is invoked to set up the PCI device memory space for device
7710 * with SLI-3 interface spec.
7711 *
7712 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007713 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04007714 * other values - error
7715 **/
7716static int
7717lpfc_sli_pci_mem_setup(struct lpfc_hba *phba)
7718{
Christoph Hellwigf30e1bf2018-10-18 15:10:21 +02007719 struct pci_dev *pdev = phba->pcidev;
James Smart3772a992009-05-22 14:50:54 -04007720 unsigned long bar0map_len, bar2map_len;
7721 int i, hbq_count;
7722 void *ptr;
Hannes Reinecke56de8352019-02-18 08:34:19 +01007723 int error;
James Smart3772a992009-05-22 14:50:54 -04007724
Christoph Hellwigf30e1bf2018-10-18 15:10:21 +02007725 if (!pdev)
Hannes Reinecke56de8352019-02-18 08:34:19 +01007726 return -ENODEV;
James Smart3772a992009-05-22 14:50:54 -04007727
7728 /* Set the device DMA mask size */
Hannes Reinecke56de8352019-02-18 08:34:19 +01007729 error = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
7730 if (error)
7731 error = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
7732 if (error)
Christoph Hellwigf30e1bf2018-10-18 15:10:21 +02007733 return error;
Hannes Reinecke56de8352019-02-18 08:34:19 +01007734 error = -ENODEV;
James Smart3772a992009-05-22 14:50:54 -04007735
7736 /* Get the bus address of Bar0 and Bar2 and the number of bytes
7737 * required by each mapping.
7738 */
7739 phba->pci_bar0_map = pci_resource_start(pdev, 0);
7740 bar0map_len = pci_resource_len(pdev, 0);
7741
7742 phba->pci_bar2_map = pci_resource_start(pdev, 2);
7743 bar2map_len = pci_resource_len(pdev, 2);
7744
7745 /* Map HBA SLIM to a kernel virtual address. */
7746 phba->slim_memmap_p = ioremap(phba->pci_bar0_map, bar0map_len);
7747 if (!phba->slim_memmap_p) {
7748 dev_printk(KERN_ERR, &pdev->dev,
7749 "ioremap failed for SLIM memory.\n");
7750 goto out;
7751 }
7752
7753 /* Map HBA Control Registers to a kernel virtual address. */
7754 phba->ctrl_regs_memmap_p = ioremap(phba->pci_bar2_map, bar2map_len);
7755 if (!phba->ctrl_regs_memmap_p) {
7756 dev_printk(KERN_ERR, &pdev->dev,
7757 "ioremap failed for HBA control registers.\n");
7758 goto out_iounmap_slim;
7759 }
7760
7761 /* Allocate memory for SLI-2 structures */
Luis Chamberlain750afb02019-01-04 09:23:09 +01007762 phba->slim2p.virt = dma_alloc_coherent(&pdev->dev, SLI2_SLIM_SIZE,
7763 &phba->slim2p.phys, GFP_KERNEL);
James Smart3772a992009-05-22 14:50:54 -04007764 if (!phba->slim2p.virt)
7765 goto out_iounmap;
7766
James Smart3772a992009-05-22 14:50:54 -04007767 phba->mbox = phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, mbx);
James Smart7a470272010-03-15 11:25:20 -04007768 phba->mbox_ext = (phba->slim2p.virt +
7769 offsetof(struct lpfc_sli2_slim, mbx_ext_words));
James Smart3772a992009-05-22 14:50:54 -04007770 phba->pcb = (phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, pcb));
7771 phba->IOCBs = (phba->slim2p.virt +
7772 offsetof(struct lpfc_sli2_slim, IOCBs));
7773
7774 phba->hbqslimp.virt = dma_alloc_coherent(&pdev->dev,
7775 lpfc_sli_hbq_size(),
7776 &phba->hbqslimp.phys,
7777 GFP_KERNEL);
7778 if (!phba->hbqslimp.virt)
7779 goto out_free_slim;
7780
7781 hbq_count = lpfc_sli_hbq_count();
7782 ptr = phba->hbqslimp.virt;
7783 for (i = 0; i < hbq_count; ++i) {
7784 phba->hbqs[i].hbq_virt = ptr;
7785 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
7786 ptr += (lpfc_hbq_defs[i]->entry_count *
7787 sizeof(struct lpfc_hbq_entry));
7788 }
7789 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_els_hbq_alloc;
7790 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_els_hbq_free;
7791
7792 memset(phba->hbqslimp.virt, 0, lpfc_sli_hbq_size());
7793
James Smart3772a992009-05-22 14:50:54 -04007794 phba->MBslimaddr = phba->slim_memmap_p;
7795 phba->HAregaddr = phba->ctrl_regs_memmap_p + HA_REG_OFFSET;
7796 phba->CAregaddr = phba->ctrl_regs_memmap_p + CA_REG_OFFSET;
7797 phba->HSregaddr = phba->ctrl_regs_memmap_p + HS_REG_OFFSET;
7798 phba->HCregaddr = phba->ctrl_regs_memmap_p + HC_REG_OFFSET;
James Smartea2151b2008-09-07 11:52:10 -04007799
dea31012005-04-17 16:05:31 -05007800 return 0;
7801
dea31012005-04-17 16:05:31 -05007802out_free_slim:
James Smart34b02dc2008-08-24 21:49:55 -04007803 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
7804 phba->slim2p.virt, phba->slim2p.phys);
dea31012005-04-17 16:05:31 -05007805out_iounmap:
7806 iounmap(phba->ctrl_regs_memmap_p);
Jamie Wellnitz901a9202006-02-28 19:25:19 -05007807out_iounmap_slim:
dea31012005-04-17 16:05:31 -05007808 iounmap(phba->slim_memmap_p);
dea31012005-04-17 16:05:31 -05007809out:
7810 return error;
7811}
7812
James Smarte59058c2008-08-24 21:49:00 -04007813/**
James Smart3772a992009-05-22 14:50:54 -04007814 * lpfc_sli_pci_mem_unset - Unset SLI3 HBA PCI memory space.
7815 * @phba: pointer to lpfc hba data structure.
7816 *
7817 * This routine is invoked to unset the PCI device memory space for device
7818 * with SLI-3 interface spec.
7819 **/
7820static void
7821lpfc_sli_pci_mem_unset(struct lpfc_hba *phba)
7822{
7823 struct pci_dev *pdev;
7824
7825 /* Obtain PCI device reference */
7826 if (!phba->pcidev)
7827 return;
7828 else
7829 pdev = phba->pcidev;
7830
7831 /* Free coherent DMA memory allocated */
7832 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
7833 phba->hbqslimp.virt, phba->hbqslimp.phys);
7834 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
7835 phba->slim2p.virt, phba->slim2p.phys);
7836
7837 /* I/O memory unmap */
7838 iounmap(phba->ctrl_regs_memmap_p);
7839 iounmap(phba->slim_memmap_p);
7840
7841 return;
7842}
7843
7844/**
James Smartda0436e2009-05-22 14:51:39 -04007845 * lpfc_sli4_post_status_check - Wait for SLI4 POST done and check status
7846 * @phba: pointer to lpfc hba data structure.
7847 *
7848 * This routine is invoked to wait for SLI4 device Power On Self Test (POST)
7849 * done and check status.
7850 *
7851 * Return 0 if successful, otherwise -ENODEV.
7852 **/
7853int
7854lpfc_sli4_post_status_check(struct lpfc_hba *phba)
7855{
James Smart2fcee4b2010-12-15 17:57:46 -05007856 struct lpfc_register portsmphr_reg, uerrlo_reg, uerrhi_reg;
7857 struct lpfc_register reg_data;
7858 int i, port_error = 0;
7859 uint32_t if_type;
James Smartda0436e2009-05-22 14:51:39 -04007860
James Smart9940b972011-03-11 16:06:12 -05007861 memset(&portsmphr_reg, 0, sizeof(portsmphr_reg));
7862 memset(&reg_data, 0, sizeof(reg_data));
James Smart2fcee4b2010-12-15 17:57:46 -05007863 if (!phba->sli4_hba.PSMPHRregaddr)
James Smartda0436e2009-05-22 14:51:39 -04007864 return -ENODEV;
7865
James Smartda0436e2009-05-22 14:51:39 -04007866 /* Wait up to 30 seconds for the SLI Port POST done and ready */
7867 for (i = 0; i < 3000; i++) {
James Smart9940b972011-03-11 16:06:12 -05007868 if (lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
7869 &portsmphr_reg.word0) ||
7870 (bf_get(lpfc_port_smphr_perr, &portsmphr_reg))) {
James Smart2fcee4b2010-12-15 17:57:46 -05007871 /* Port has a fatal POST error, break out */
James Smartda0436e2009-05-22 14:51:39 -04007872 port_error = -ENODEV;
7873 break;
7874 }
James Smart2fcee4b2010-12-15 17:57:46 -05007875 if (LPFC_POST_STAGE_PORT_READY ==
7876 bf_get(lpfc_port_smphr_port_status, &portsmphr_reg))
James Smartda0436e2009-05-22 14:51:39 -04007877 break;
James Smartda0436e2009-05-22 14:51:39 -04007878 msleep(10);
7879 }
7880
James Smart2fcee4b2010-12-15 17:57:46 -05007881 /*
7882 * If there was a port error during POST, then don't proceed with
7883 * other register reads as the data may not be valid. Just exit.
7884 */
7885 if (port_error) {
James Smartda0436e2009-05-22 14:51:39 -04007886 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -05007887 "1408 Port Failed POST - portsmphr=0x%x, "
7888 "perr=x%x, sfi=x%x, nip=x%x, ipc=x%x, scr1=x%x, "
7889 "scr2=x%x, hscratch=x%x, pstatus=x%x\n",
7890 portsmphr_reg.word0,
7891 bf_get(lpfc_port_smphr_perr, &portsmphr_reg),
7892 bf_get(lpfc_port_smphr_sfi, &portsmphr_reg),
7893 bf_get(lpfc_port_smphr_nip, &portsmphr_reg),
7894 bf_get(lpfc_port_smphr_ipc, &portsmphr_reg),
7895 bf_get(lpfc_port_smphr_scr1, &portsmphr_reg),
7896 bf_get(lpfc_port_smphr_scr2, &portsmphr_reg),
7897 bf_get(lpfc_port_smphr_host_scratch, &portsmphr_reg),
7898 bf_get(lpfc_port_smphr_port_status, &portsmphr_reg));
7899 } else {
James Smart28baac72010-02-12 14:42:03 -05007900 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -05007901 "2534 Device Info: SLIFamily=0x%x, "
7902 "SLIRev=0x%x, IFType=0x%x, SLIHint_1=0x%x, "
7903 "SLIHint_2=0x%x, FT=0x%x\n",
James Smart28baac72010-02-12 14:42:03 -05007904 bf_get(lpfc_sli_intf_sli_family,
7905 &phba->sli4_hba.sli_intf),
7906 bf_get(lpfc_sli_intf_slirev,
7907 &phba->sli4_hba.sli_intf),
James Smart085c6472010-11-20 23:11:37 -05007908 bf_get(lpfc_sli_intf_if_type,
James Smart28baac72010-02-12 14:42:03 -05007909 &phba->sli4_hba.sli_intf),
James Smart085c6472010-11-20 23:11:37 -05007910 bf_get(lpfc_sli_intf_sli_hint1,
7911 &phba->sli4_hba.sli_intf),
7912 bf_get(lpfc_sli_intf_sli_hint2,
7913 &phba->sli4_hba.sli_intf),
7914 bf_get(lpfc_sli_intf_func_type,
James Smart28baac72010-02-12 14:42:03 -05007915 &phba->sli4_hba.sli_intf));
James Smart2fcee4b2010-12-15 17:57:46 -05007916 /*
7917 * Check for other Port errors during the initialization
7918 * process. Fail the load if the port did not come up
7919 * correctly.
7920 */
7921 if_type = bf_get(lpfc_sli_intf_if_type,
7922 &phba->sli4_hba.sli_intf);
7923 switch (if_type) {
7924 case LPFC_SLI_INTF_IF_TYPE_0:
7925 phba->sli4_hba.ue_mask_lo =
7926 readl(phba->sli4_hba.u.if_type0.UEMASKLOregaddr);
7927 phba->sli4_hba.ue_mask_hi =
7928 readl(phba->sli4_hba.u.if_type0.UEMASKHIregaddr);
7929 uerrlo_reg.word0 =
7930 readl(phba->sli4_hba.u.if_type0.UERRLOregaddr);
7931 uerrhi_reg.word0 =
7932 readl(phba->sli4_hba.u.if_type0.UERRHIregaddr);
7933 if ((~phba->sli4_hba.ue_mask_lo & uerrlo_reg.word0) ||
7934 (~phba->sli4_hba.ue_mask_hi & uerrhi_reg.word0)) {
7935 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7936 "1422 Unrecoverable Error "
7937 "Detected during POST "
7938 "uerr_lo_reg=0x%x, "
7939 "uerr_hi_reg=0x%x, "
7940 "ue_mask_lo_reg=0x%x, "
7941 "ue_mask_hi_reg=0x%x\n",
7942 uerrlo_reg.word0,
7943 uerrhi_reg.word0,
7944 phba->sli4_hba.ue_mask_lo,
7945 phba->sli4_hba.ue_mask_hi);
7946 port_error = -ENODEV;
7947 }
7948 break;
7949 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart27d6ac02018-02-22 08:18:42 -08007950 case LPFC_SLI_INTF_IF_TYPE_6:
James Smart2fcee4b2010-12-15 17:57:46 -05007951 /* Final checks. The port status should be clean. */
James Smart9940b972011-03-11 16:06:12 -05007952 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
7953 &reg_data.word0) ||
James Smart05580562011-05-24 11:40:48 -04007954 (bf_get(lpfc_sliport_status_err, &reg_data) &&
7955 !bf_get(lpfc_sliport_status_rn, &reg_data))) {
James Smart2fcee4b2010-12-15 17:57:46 -05007956 phba->work_status[0] =
7957 readl(phba->sli4_hba.u.if_type2.
7958 ERR1regaddr);
7959 phba->work_status[1] =
7960 readl(phba->sli4_hba.u.if_type2.
7961 ERR2regaddr);
7962 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart8fcb8ac2012-03-01 22:35:58 -05007963 "2888 Unrecoverable port error "
7964 "following POST: port status reg "
7965 "0x%x, port_smphr reg 0x%x, "
James Smart2fcee4b2010-12-15 17:57:46 -05007966 "error 1=0x%x, error 2=0x%x\n",
7967 reg_data.word0,
7968 portsmphr_reg.word0,
7969 phba->work_status[0],
7970 phba->work_status[1]);
7971 port_error = -ENODEV;
7972 }
7973 break;
7974 case LPFC_SLI_INTF_IF_TYPE_1:
7975 default:
7976 break;
7977 }
James Smart28baac72010-02-12 14:42:03 -05007978 }
James Smartda0436e2009-05-22 14:51:39 -04007979 return port_error;
7980}
7981
7982/**
7983 * lpfc_sli4_bar0_register_memmap - Set up SLI4 BAR0 register memory map.
7984 * @phba: pointer to lpfc hba data structure.
James Smart2fcee4b2010-12-15 17:57:46 -05007985 * @if_type: The SLI4 interface type getting configured.
James Smartda0436e2009-05-22 14:51:39 -04007986 *
7987 * This routine is invoked to set up SLI4 BAR0 PCI config space register
7988 * memory map.
7989 **/
7990static void
James Smart2fcee4b2010-12-15 17:57:46 -05007991lpfc_sli4_bar0_register_memmap(struct lpfc_hba *phba, uint32_t if_type)
James Smartda0436e2009-05-22 14:51:39 -04007992{
James Smart2fcee4b2010-12-15 17:57:46 -05007993 switch (if_type) {
7994 case LPFC_SLI_INTF_IF_TYPE_0:
7995 phba->sli4_hba.u.if_type0.UERRLOregaddr =
7996 phba->sli4_hba.conf_regs_memmap_p + LPFC_UERR_STATUS_LO;
7997 phba->sli4_hba.u.if_type0.UERRHIregaddr =
7998 phba->sli4_hba.conf_regs_memmap_p + LPFC_UERR_STATUS_HI;
7999 phba->sli4_hba.u.if_type0.UEMASKLOregaddr =
8000 phba->sli4_hba.conf_regs_memmap_p + LPFC_UE_MASK_LO;
8001 phba->sli4_hba.u.if_type0.UEMASKHIregaddr =
8002 phba->sli4_hba.conf_regs_memmap_p + LPFC_UE_MASK_HI;
8003 phba->sli4_hba.SLIINTFregaddr =
8004 phba->sli4_hba.conf_regs_memmap_p + LPFC_SLI_INTF;
8005 break;
8006 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart0cf07f842017-06-01 21:07:10 -07008007 phba->sli4_hba.u.if_type2.EQDregaddr =
8008 phba->sli4_hba.conf_regs_memmap_p +
8009 LPFC_CTL_PORT_EQ_DELAY_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05008010 phba->sli4_hba.u.if_type2.ERR1regaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04008011 phba->sli4_hba.conf_regs_memmap_p +
8012 LPFC_CTL_PORT_ER1_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05008013 phba->sli4_hba.u.if_type2.ERR2regaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04008014 phba->sli4_hba.conf_regs_memmap_p +
8015 LPFC_CTL_PORT_ER2_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05008016 phba->sli4_hba.u.if_type2.CTRLregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04008017 phba->sli4_hba.conf_regs_memmap_p +
8018 LPFC_CTL_PORT_CTL_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05008019 phba->sli4_hba.u.if_type2.STATUSregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04008020 phba->sli4_hba.conf_regs_memmap_p +
8021 LPFC_CTL_PORT_STA_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05008022 phba->sli4_hba.SLIINTFregaddr =
8023 phba->sli4_hba.conf_regs_memmap_p + LPFC_SLI_INTF;
8024 phba->sli4_hba.PSMPHRregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04008025 phba->sli4_hba.conf_regs_memmap_p +
8026 LPFC_CTL_PORT_SEM_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05008027 phba->sli4_hba.RQDBregaddr =
James Smart962bc512013-01-03 15:44:00 -05008028 phba->sli4_hba.conf_regs_memmap_p +
8029 LPFC_ULP0_RQ_DOORBELL;
James Smart2fcee4b2010-12-15 17:57:46 -05008030 phba->sli4_hba.WQDBregaddr =
James Smart962bc512013-01-03 15:44:00 -05008031 phba->sli4_hba.conf_regs_memmap_p +
8032 LPFC_ULP0_WQ_DOORBELL;
James Smart9dd35422018-02-22 08:18:41 -08008033 phba->sli4_hba.CQDBregaddr =
James Smart2fcee4b2010-12-15 17:57:46 -05008034 phba->sli4_hba.conf_regs_memmap_p + LPFC_EQCQ_DOORBELL;
James Smart9dd35422018-02-22 08:18:41 -08008035 phba->sli4_hba.EQDBregaddr = phba->sli4_hba.CQDBregaddr;
James Smart2fcee4b2010-12-15 17:57:46 -05008036 phba->sli4_hba.MQDBregaddr =
8037 phba->sli4_hba.conf_regs_memmap_p + LPFC_MQ_DOORBELL;
8038 phba->sli4_hba.BMBXregaddr =
8039 phba->sli4_hba.conf_regs_memmap_p + LPFC_BMBX;
8040 break;
James Smart27d6ac02018-02-22 08:18:42 -08008041 case LPFC_SLI_INTF_IF_TYPE_6:
8042 phba->sli4_hba.u.if_type2.EQDregaddr =
8043 phba->sli4_hba.conf_regs_memmap_p +
8044 LPFC_CTL_PORT_EQ_DELAY_OFFSET;
8045 phba->sli4_hba.u.if_type2.ERR1regaddr =
8046 phba->sli4_hba.conf_regs_memmap_p +
8047 LPFC_CTL_PORT_ER1_OFFSET;
8048 phba->sli4_hba.u.if_type2.ERR2regaddr =
8049 phba->sli4_hba.conf_regs_memmap_p +
8050 LPFC_CTL_PORT_ER2_OFFSET;
8051 phba->sli4_hba.u.if_type2.CTRLregaddr =
8052 phba->sli4_hba.conf_regs_memmap_p +
8053 LPFC_CTL_PORT_CTL_OFFSET;
8054 phba->sli4_hba.u.if_type2.STATUSregaddr =
8055 phba->sli4_hba.conf_regs_memmap_p +
8056 LPFC_CTL_PORT_STA_OFFSET;
8057 phba->sli4_hba.PSMPHRregaddr =
8058 phba->sli4_hba.conf_regs_memmap_p +
8059 LPFC_CTL_PORT_SEM_OFFSET;
8060 phba->sli4_hba.BMBXregaddr =
8061 phba->sli4_hba.conf_regs_memmap_p + LPFC_BMBX;
8062 break;
James Smart2fcee4b2010-12-15 17:57:46 -05008063 case LPFC_SLI_INTF_IF_TYPE_1:
8064 default:
8065 dev_printk(KERN_ERR, &phba->pcidev->dev,
8066 "FATAL - unsupported SLI4 interface type - %d\n",
8067 if_type);
8068 break;
8069 }
James Smartda0436e2009-05-22 14:51:39 -04008070}
8071
8072/**
8073 * lpfc_sli4_bar1_register_memmap - Set up SLI4 BAR1 register memory map.
8074 * @phba: pointer to lpfc hba data structure.
8075 *
James Smart27d6ac02018-02-22 08:18:42 -08008076 * This routine is invoked to set up SLI4 BAR1 register memory map.
James Smartda0436e2009-05-22 14:51:39 -04008077 **/
8078static void
James Smart27d6ac02018-02-22 08:18:42 -08008079lpfc_sli4_bar1_register_memmap(struct lpfc_hba *phba, uint32_t if_type)
James Smartda0436e2009-05-22 14:51:39 -04008080{
James Smart27d6ac02018-02-22 08:18:42 -08008081 switch (if_type) {
8082 case LPFC_SLI_INTF_IF_TYPE_0:
8083 phba->sli4_hba.PSMPHRregaddr =
8084 phba->sli4_hba.ctrl_regs_memmap_p +
8085 LPFC_SLIPORT_IF0_SMPHR;
8086 phba->sli4_hba.ISRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
8087 LPFC_HST_ISR0;
8088 phba->sli4_hba.IMRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
8089 LPFC_HST_IMR0;
8090 phba->sli4_hba.ISCRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
8091 LPFC_HST_ISCR0;
8092 break;
8093 case LPFC_SLI_INTF_IF_TYPE_6:
8094 phba->sli4_hba.RQDBregaddr = phba->sli4_hba.drbl_regs_memmap_p +
8095 LPFC_IF6_RQ_DOORBELL;
8096 phba->sli4_hba.WQDBregaddr = phba->sli4_hba.drbl_regs_memmap_p +
8097 LPFC_IF6_WQ_DOORBELL;
8098 phba->sli4_hba.CQDBregaddr = phba->sli4_hba.drbl_regs_memmap_p +
8099 LPFC_IF6_CQ_DOORBELL;
8100 phba->sli4_hba.EQDBregaddr = phba->sli4_hba.drbl_regs_memmap_p +
8101 LPFC_IF6_EQ_DOORBELL;
8102 phba->sli4_hba.MQDBregaddr = phba->sli4_hba.drbl_regs_memmap_p +
8103 LPFC_IF6_MQ_DOORBELL;
8104 break;
8105 case LPFC_SLI_INTF_IF_TYPE_2:
8106 case LPFC_SLI_INTF_IF_TYPE_1:
8107 default:
8108 dev_err(&phba->pcidev->dev,
8109 "FATAL - unsupported SLI4 interface type - %d\n",
8110 if_type);
8111 break;
8112 }
James Smartda0436e2009-05-22 14:51:39 -04008113}
8114
8115/**
8116 * lpfc_sli4_bar2_register_memmap - Set up SLI4 BAR2 register memory map.
8117 * @phba: pointer to lpfc hba data structure.
8118 * @vf: virtual function number
8119 *
8120 * This routine is invoked to set up SLI4 BAR2 doorbell register memory map
8121 * based on the given viftual function number, @vf.
8122 *
8123 * Return 0 if successful, otherwise -ENODEV.
8124 **/
8125static int
8126lpfc_sli4_bar2_register_memmap(struct lpfc_hba *phba, uint32_t vf)
8127{
8128 if (vf > LPFC_VIR_FUNC_MAX)
8129 return -ENODEV;
8130
8131 phba->sli4_hba.RQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
James Smart962bc512013-01-03 15:44:00 -05008132 vf * LPFC_VFR_PAGE_SIZE +
8133 LPFC_ULP0_RQ_DOORBELL);
James Smartda0436e2009-05-22 14:51:39 -04008134 phba->sli4_hba.WQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
James Smart962bc512013-01-03 15:44:00 -05008135 vf * LPFC_VFR_PAGE_SIZE +
8136 LPFC_ULP0_WQ_DOORBELL);
James Smart9dd35422018-02-22 08:18:41 -08008137 phba->sli4_hba.CQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
8138 vf * LPFC_VFR_PAGE_SIZE +
8139 LPFC_EQCQ_DOORBELL);
8140 phba->sli4_hba.EQDBregaddr = phba->sli4_hba.CQDBregaddr;
James Smartda0436e2009-05-22 14:51:39 -04008141 phba->sli4_hba.MQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
8142 vf * LPFC_VFR_PAGE_SIZE + LPFC_MQ_DOORBELL);
8143 phba->sli4_hba.BMBXregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
8144 vf * LPFC_VFR_PAGE_SIZE + LPFC_BMBX);
8145 return 0;
8146}
8147
8148/**
8149 * lpfc_create_bootstrap_mbox - Create the bootstrap mailbox
8150 * @phba: pointer to lpfc hba data structure.
8151 *
8152 * This routine is invoked to create the bootstrap mailbox
8153 * region consistent with the SLI-4 interface spec. This
8154 * routine allocates all memory necessary to communicate
8155 * mailbox commands to the port and sets up all alignment
8156 * needs. No locks are expected to be held when calling
8157 * this routine.
8158 *
8159 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008160 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04008161 * -ENOMEM - could not allocated memory.
James Smartda0436e2009-05-22 14:51:39 -04008162 **/
8163static int
8164lpfc_create_bootstrap_mbox(struct lpfc_hba *phba)
8165{
8166 uint32_t bmbx_size;
8167 struct lpfc_dmabuf *dmabuf;
8168 struct dma_address *dma_address;
8169 uint32_t pa_addr;
8170 uint64_t phys_addr;
8171
8172 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
8173 if (!dmabuf)
8174 return -ENOMEM;
8175
8176 /*
8177 * The bootstrap mailbox region is comprised of 2 parts
8178 * plus an alignment restriction of 16 bytes.
8179 */
8180 bmbx_size = sizeof(struct lpfc_bmbx_create) + (LPFC_ALIGN_16_BYTE - 1);
Luis Chamberlain750afb02019-01-04 09:23:09 +01008181 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev, bmbx_size,
8182 &dmabuf->phys, GFP_KERNEL);
James Smartda0436e2009-05-22 14:51:39 -04008183 if (!dmabuf->virt) {
8184 kfree(dmabuf);
8185 return -ENOMEM;
8186 }
James Smartda0436e2009-05-22 14:51:39 -04008187
8188 /*
8189 * Initialize the bootstrap mailbox pointers now so that the register
8190 * operations are simple later. The mailbox dma address is required
8191 * to be 16-byte aligned. Also align the virtual memory as each
8192 * maibox is copied into the bmbx mailbox region before issuing the
8193 * command to the port.
8194 */
8195 phba->sli4_hba.bmbx.dmabuf = dmabuf;
8196 phba->sli4_hba.bmbx.bmbx_size = bmbx_size;
8197
8198 phba->sli4_hba.bmbx.avirt = PTR_ALIGN(dmabuf->virt,
8199 LPFC_ALIGN_16_BYTE);
8200 phba->sli4_hba.bmbx.aphys = ALIGN(dmabuf->phys,
8201 LPFC_ALIGN_16_BYTE);
8202
8203 /*
8204 * Set the high and low physical addresses now. The SLI4 alignment
8205 * requirement is 16 bytes and the mailbox is posted to the port
8206 * as two 30-bit addresses. The other data is a bit marking whether
8207 * the 30-bit address is the high or low address.
8208 * Upcast bmbx aphys to 64bits so shift instruction compiles
8209 * clean on 32 bit machines.
8210 */
8211 dma_address = &phba->sli4_hba.bmbx.dma_address;
8212 phys_addr = (uint64_t)phba->sli4_hba.bmbx.aphys;
8213 pa_addr = (uint32_t) ((phys_addr >> 34) & 0x3fffffff);
8214 dma_address->addr_hi = (uint32_t) ((pa_addr << 2) |
8215 LPFC_BMBX_BIT1_ADDR_HI);
8216
8217 pa_addr = (uint32_t) ((phba->sli4_hba.bmbx.aphys >> 4) & 0x3fffffff);
8218 dma_address->addr_lo = (uint32_t) ((pa_addr << 2) |
8219 LPFC_BMBX_BIT1_ADDR_LO);
8220 return 0;
8221}
8222
8223/**
8224 * lpfc_destroy_bootstrap_mbox - Destroy all bootstrap mailbox resources
8225 * @phba: pointer to lpfc hba data structure.
8226 *
8227 * This routine is invoked to teardown the bootstrap mailbox
8228 * region and release all host resources. This routine requires
8229 * the caller to ensure all mailbox commands recovered, no
8230 * additional mailbox comands are sent, and interrupts are disabled
8231 * before calling this routine.
8232 *
8233 **/
8234static void
8235lpfc_destroy_bootstrap_mbox(struct lpfc_hba *phba)
8236{
8237 dma_free_coherent(&phba->pcidev->dev,
8238 phba->sli4_hba.bmbx.bmbx_size,
8239 phba->sli4_hba.bmbx.dmabuf->virt,
8240 phba->sli4_hba.bmbx.dmabuf->phys);
8241
8242 kfree(phba->sli4_hba.bmbx.dmabuf);
8243 memset(&phba->sli4_hba.bmbx, 0, sizeof(struct lpfc_bmbx));
8244}
8245
8246/**
8247 * lpfc_sli4_read_config - Get the config parameters.
8248 * @phba: pointer to lpfc hba data structure.
8249 *
8250 * This routine is invoked to read the configuration parameters from the HBA.
8251 * The configuration parameters are used to set the base and maximum values
8252 * for RPI's XRI's VPI's VFI's and FCFIs. These values also affect the resource
8253 * allocation for the port.
8254 *
8255 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008256 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008257 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04008258 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04008259 **/
James Smartff78d8f2011-12-13 13:21:35 -05008260int
James Smartda0436e2009-05-22 14:51:39 -04008261lpfc_sli4_read_config(struct lpfc_hba *phba)
8262{
8263 LPFC_MBOXQ_t *pmb;
8264 struct lpfc_mbx_read_config *rd_config;
James Smart912e3ac2011-05-24 11:42:11 -04008265 union lpfc_sli4_cfg_shdr *shdr;
8266 uint32_t shdr_status, shdr_add_status;
8267 struct lpfc_mbx_get_func_cfg *get_func_cfg;
8268 struct lpfc_rsrc_desc_fcfcoe *desc;
James Smart8aa134a2012-08-14 14:25:29 -04008269 char *pdesc_0;
James Smartc6918162016-10-13 15:06:16 -07008270 uint16_t forced_link_speed;
James Smart6a828b02019-01-28 11:14:31 -08008271 uint32_t if_type, qmin;
James Smart8aa134a2012-08-14 14:25:29 -04008272 int length, i, rc = 0, rc2;
James Smartda0436e2009-05-22 14:51:39 -04008273
8274 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
8275 if (!pmb) {
8276 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8277 "2011 Unable to allocate memory for issuing "
8278 "SLI_CONFIG_SPECIAL mailbox command\n");
8279 return -ENOMEM;
8280 }
8281
8282 lpfc_read_config(phba, pmb);
8283
8284 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
8285 if (rc != MBX_SUCCESS) {
8286 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8287 "2012 Mailbox failed , mbxCmd x%x "
8288 "READ_CONFIG, mbxStatus x%x\n",
8289 bf_get(lpfc_mqe_command, &pmb->u.mqe),
8290 bf_get(lpfc_mqe_status, &pmb->u.mqe));
8291 rc = -EIO;
8292 } else {
8293 rd_config = &pmb->u.mqe.un.rd_config;
James Smartff78d8f2011-12-13 13:21:35 -05008294 if (bf_get(lpfc_mbx_rd_conf_lnk_ldv, rd_config)) {
8295 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
8296 phba->sli4_hba.lnk_info.lnk_tp =
8297 bf_get(lpfc_mbx_rd_conf_lnk_type, rd_config);
8298 phba->sli4_hba.lnk_info.lnk_no =
8299 bf_get(lpfc_mbx_rd_conf_lnk_numb, rd_config);
8300 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8301 "3081 lnk_type:%d, lnk_numb:%d\n",
8302 phba->sli4_hba.lnk_info.lnk_tp,
8303 phba->sli4_hba.lnk_info.lnk_no);
8304 } else
8305 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8306 "3082 Mailbox (x%x) returned ldv:x0\n",
8307 bf_get(lpfc_mqe_command, &pmb->u.mqe));
James Smart44fd7fe2017-08-23 16:55:47 -07008308 if (bf_get(lpfc_mbx_rd_conf_bbscn_def, rd_config)) {
8309 phba->bbcredit_support = 1;
8310 phba->sli4_hba.bbscn_params.word0 = rd_config->word8;
8311 }
8312
James Smart1dc5ec22018-10-23 13:41:11 -07008313 phba->sli4_hba.conf_trunk =
8314 bf_get(lpfc_mbx_rd_conf_trunk, rd_config);
James Smart6d368e52011-05-24 11:44:12 -04008315 phba->sli4_hba.extents_in_use =
8316 bf_get(lpfc_mbx_rd_conf_extnts_inuse, rd_config);
James Smartda0436e2009-05-22 14:51:39 -04008317 phba->sli4_hba.max_cfg_param.max_xri =
8318 bf_get(lpfc_mbx_rd_conf_xri_count, rd_config);
James Smart31f06d22019-08-14 16:56:31 -07008319 /* Reduce resource usage in kdump environment */
8320 if (is_kdump_kernel() &&
8321 phba->sli4_hba.max_cfg_param.max_xri > 512)
8322 phba->sli4_hba.max_cfg_param.max_xri = 512;
James Smartda0436e2009-05-22 14:51:39 -04008323 phba->sli4_hba.max_cfg_param.xri_base =
8324 bf_get(lpfc_mbx_rd_conf_xri_base, rd_config);
8325 phba->sli4_hba.max_cfg_param.max_vpi =
8326 bf_get(lpfc_mbx_rd_conf_vpi_count, rd_config);
James Smart8b47ae62018-11-29 16:09:33 -08008327 /* Limit the max we support */
8328 if (phba->sli4_hba.max_cfg_param.max_vpi > LPFC_MAX_VPORTS)
8329 phba->sli4_hba.max_cfg_param.max_vpi = LPFC_MAX_VPORTS;
James Smartda0436e2009-05-22 14:51:39 -04008330 phba->sli4_hba.max_cfg_param.vpi_base =
8331 bf_get(lpfc_mbx_rd_conf_vpi_base, rd_config);
8332 phba->sli4_hba.max_cfg_param.max_rpi =
8333 bf_get(lpfc_mbx_rd_conf_rpi_count, rd_config);
8334 phba->sli4_hba.max_cfg_param.rpi_base =
8335 bf_get(lpfc_mbx_rd_conf_rpi_base, rd_config);
8336 phba->sli4_hba.max_cfg_param.max_vfi =
8337 bf_get(lpfc_mbx_rd_conf_vfi_count, rd_config);
8338 phba->sli4_hba.max_cfg_param.vfi_base =
8339 bf_get(lpfc_mbx_rd_conf_vfi_base, rd_config);
8340 phba->sli4_hba.max_cfg_param.max_fcfi =
8341 bf_get(lpfc_mbx_rd_conf_fcfi_count, rd_config);
James Smartda0436e2009-05-22 14:51:39 -04008342 phba->sli4_hba.max_cfg_param.max_eq =
8343 bf_get(lpfc_mbx_rd_conf_eq_count, rd_config);
8344 phba->sli4_hba.max_cfg_param.max_rq =
8345 bf_get(lpfc_mbx_rd_conf_rq_count, rd_config);
8346 phba->sli4_hba.max_cfg_param.max_wq =
8347 bf_get(lpfc_mbx_rd_conf_wq_count, rd_config);
8348 phba->sli4_hba.max_cfg_param.max_cq =
8349 bf_get(lpfc_mbx_rd_conf_cq_count, rd_config);
8350 phba->lmt = bf_get(lpfc_mbx_rd_conf_lmt, rd_config);
8351 phba->sli4_hba.next_xri = phba->sli4_hba.max_cfg_param.xri_base;
8352 phba->vpi_base = phba->sli4_hba.max_cfg_param.vpi_base;
8353 phba->vfi_base = phba->sli4_hba.max_cfg_param.vfi_base;
James Smart5ffc2662009-11-18 15:39:44 -05008354 phba->max_vpi = (phba->sli4_hba.max_cfg_param.max_vpi > 0) ?
8355 (phba->sli4_hba.max_cfg_param.max_vpi - 1) : 0;
James Smartda0436e2009-05-22 14:51:39 -04008356 phba->max_vports = phba->max_vpi;
8357 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart6d368e52011-05-24 11:44:12 -04008358 "2003 cfg params Extents? %d "
8359 "XRI(B:%d M:%d), "
James Smartda0436e2009-05-22 14:51:39 -04008360 "VPI(B:%d M:%d) "
8361 "VFI(B:%d M:%d) "
8362 "RPI(B:%d M:%d) "
James Smart2ea259e2017-02-12 13:52:27 -08008363 "FCFI:%d EQ:%d CQ:%d WQ:%d RQ:%d\n",
James Smart6d368e52011-05-24 11:44:12 -04008364 phba->sli4_hba.extents_in_use,
James Smartda0436e2009-05-22 14:51:39 -04008365 phba->sli4_hba.max_cfg_param.xri_base,
8366 phba->sli4_hba.max_cfg_param.max_xri,
8367 phba->sli4_hba.max_cfg_param.vpi_base,
8368 phba->sli4_hba.max_cfg_param.max_vpi,
8369 phba->sli4_hba.max_cfg_param.vfi_base,
8370 phba->sli4_hba.max_cfg_param.max_vfi,
8371 phba->sli4_hba.max_cfg_param.rpi_base,
8372 phba->sli4_hba.max_cfg_param.max_rpi,
James Smart2ea259e2017-02-12 13:52:27 -08008373 phba->sli4_hba.max_cfg_param.max_fcfi,
8374 phba->sli4_hba.max_cfg_param.max_eq,
8375 phba->sli4_hba.max_cfg_param.max_cq,
8376 phba->sli4_hba.max_cfg_param.max_wq,
8377 phba->sli4_hba.max_cfg_param.max_rq);
8378
James Smartd38f33b2018-05-04 20:37:54 -07008379 /*
James Smart6a828b02019-01-28 11:14:31 -08008380 * Calculate queue resources based on how
8381 * many WQ/CQ/EQs are available.
James Smartd38f33b2018-05-04 20:37:54 -07008382 */
James Smart6a828b02019-01-28 11:14:31 -08008383 qmin = phba->sli4_hba.max_cfg_param.max_wq;
8384 if (phba->sli4_hba.max_cfg_param.max_cq < qmin)
8385 qmin = phba->sli4_hba.max_cfg_param.max_cq;
8386 if (phba->sli4_hba.max_cfg_param.max_eq < qmin)
8387 qmin = phba->sli4_hba.max_cfg_param.max_eq;
8388 /*
8389 * Whats left after this can go toward NVME / FCP.
8390 * The minus 4 accounts for ELS, NVME LS, MBOX
8391 * plus one extra. When configured for
8392 * NVMET, FCP io channel WQs are not created.
8393 */
8394 qmin -= 4;
James Smartd38f33b2018-05-04 20:37:54 -07008395
James Smart6a828b02019-01-28 11:14:31 -08008396 /* If NVME is configured, double the number of CQ/WQs needed */
8397 if ((phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) &&
8398 !phba->nvmet_support)
8399 qmin /= 2;
James Smartd38f33b2018-05-04 20:37:54 -07008400
James Smart6a828b02019-01-28 11:14:31 -08008401 /* Check to see if there is enough for NVME */
8402 if ((phba->cfg_irq_chann > qmin) ||
8403 (phba->cfg_hdw_queue > qmin)) {
8404 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8405 "2005 Reducing Queues: "
8406 "WQ %d CQ %d EQ %d: min %d: "
8407 "IRQ %d HDWQ %d\n",
James Smartd38f33b2018-05-04 20:37:54 -07008408 phba->sli4_hba.max_cfg_param.max_wq,
8409 phba->sli4_hba.max_cfg_param.max_cq,
James Smart6a828b02019-01-28 11:14:31 -08008410 phba->sli4_hba.max_cfg_param.max_eq,
8411 qmin, phba->cfg_irq_chann,
James Smartcdb42be2019-01-28 11:14:21 -08008412 phba->cfg_hdw_queue);
James Smartd38f33b2018-05-04 20:37:54 -07008413
James Smart6a828b02019-01-28 11:14:31 -08008414 if (phba->cfg_irq_chann > qmin)
8415 phba->cfg_irq_chann = qmin;
8416 if (phba->cfg_hdw_queue > qmin)
8417 phba->cfg_hdw_queue = qmin;
James Smartd38f33b2018-05-04 20:37:54 -07008418 }
James Smartda0436e2009-05-22 14:51:39 -04008419 }
James Smart912e3ac2011-05-24 11:42:11 -04008420
8421 if (rc)
8422 goto read_cfg_out;
James Smartda0436e2009-05-22 14:51:39 -04008423
James Smartc6918162016-10-13 15:06:16 -07008424 /* Update link speed if forced link speed is supported */
8425 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
James Smart27d6ac02018-02-22 08:18:42 -08008426 if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
James Smartc6918162016-10-13 15:06:16 -07008427 forced_link_speed =
8428 bf_get(lpfc_mbx_rd_conf_link_speed, rd_config);
8429 if (forced_link_speed) {
8430 phba->hba_flag |= HBA_FORCED_LINK_SPEED;
8431
8432 switch (forced_link_speed) {
8433 case LINK_SPEED_1G:
8434 phba->cfg_link_speed =
8435 LPFC_USER_LINK_SPEED_1G;
8436 break;
8437 case LINK_SPEED_2G:
8438 phba->cfg_link_speed =
8439 LPFC_USER_LINK_SPEED_2G;
8440 break;
8441 case LINK_SPEED_4G:
8442 phba->cfg_link_speed =
8443 LPFC_USER_LINK_SPEED_4G;
8444 break;
8445 case LINK_SPEED_8G:
8446 phba->cfg_link_speed =
8447 LPFC_USER_LINK_SPEED_8G;
8448 break;
8449 case LINK_SPEED_10G:
8450 phba->cfg_link_speed =
8451 LPFC_USER_LINK_SPEED_10G;
8452 break;
8453 case LINK_SPEED_16G:
8454 phba->cfg_link_speed =
8455 LPFC_USER_LINK_SPEED_16G;
8456 break;
8457 case LINK_SPEED_32G:
8458 phba->cfg_link_speed =
8459 LPFC_USER_LINK_SPEED_32G;
8460 break;
James Smartfbd8a6b2018-02-22 08:18:45 -08008461 case LINK_SPEED_64G:
8462 phba->cfg_link_speed =
8463 LPFC_USER_LINK_SPEED_64G;
8464 break;
James Smartc6918162016-10-13 15:06:16 -07008465 case 0xffff:
8466 phba->cfg_link_speed =
8467 LPFC_USER_LINK_SPEED_AUTO;
8468 break;
8469 default:
8470 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8471 "0047 Unrecognized link "
8472 "speed : %d\n",
8473 forced_link_speed);
8474 phba->cfg_link_speed =
8475 LPFC_USER_LINK_SPEED_AUTO;
8476 }
8477 }
8478 }
8479
James Smartda0436e2009-05-22 14:51:39 -04008480 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
James Smart572709e2013-07-15 18:32:43 -04008481 length = phba->sli4_hba.max_cfg_param.max_xri -
8482 lpfc_sli4_get_els_iocb_cnt(phba);
8483 if (phba->cfg_hba_queue_depth > length) {
8484 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
8485 "3361 HBA queue depth changed from %d to %d\n",
8486 phba->cfg_hba_queue_depth, length);
8487 phba->cfg_hba_queue_depth = length;
8488 }
James Smart912e3ac2011-05-24 11:42:11 -04008489
James Smart27d6ac02018-02-22 08:18:42 -08008490 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) <
James Smart912e3ac2011-05-24 11:42:11 -04008491 LPFC_SLI_INTF_IF_TYPE_2)
8492 goto read_cfg_out;
8493
8494 /* get the pf# and vf# for SLI4 if_type 2 port */
8495 length = (sizeof(struct lpfc_mbx_get_func_cfg) -
8496 sizeof(struct lpfc_sli4_cfg_mhdr));
8497 lpfc_sli4_config(phba, pmb, LPFC_MBOX_SUBSYSTEM_COMMON,
8498 LPFC_MBOX_OPCODE_GET_FUNCTION_CONFIG,
8499 length, LPFC_SLI4_MBX_EMBED);
8500
James Smart8aa134a2012-08-14 14:25:29 -04008501 rc2 = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart912e3ac2011-05-24 11:42:11 -04008502 shdr = (union lpfc_sli4_cfg_shdr *)
8503 &pmb->u.mqe.un.sli4_config.header.cfg_shdr;
8504 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
8505 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
James Smart8aa134a2012-08-14 14:25:29 -04008506 if (rc2 || shdr_status || shdr_add_status) {
James Smart912e3ac2011-05-24 11:42:11 -04008507 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8508 "3026 Mailbox failed , mbxCmd x%x "
8509 "GET_FUNCTION_CONFIG, mbxStatus x%x\n",
8510 bf_get(lpfc_mqe_command, &pmb->u.mqe),
8511 bf_get(lpfc_mqe_status, &pmb->u.mqe));
James Smart912e3ac2011-05-24 11:42:11 -04008512 goto read_cfg_out;
8513 }
8514
8515 /* search for fc_fcoe resrouce descriptor */
8516 get_func_cfg = &pmb->u.mqe.un.get_func_cfg;
James Smart912e3ac2011-05-24 11:42:11 -04008517
James Smart8aa134a2012-08-14 14:25:29 -04008518 pdesc_0 = (char *)&get_func_cfg->func_cfg.desc[0];
8519 desc = (struct lpfc_rsrc_desc_fcfcoe *)pdesc_0;
8520 length = bf_get(lpfc_rsrc_desc_fcfcoe_length, desc);
8521 if (length == LPFC_RSRC_DESC_TYPE_FCFCOE_V0_RSVD)
8522 length = LPFC_RSRC_DESC_TYPE_FCFCOE_V0_LENGTH;
8523 else if (length != LPFC_RSRC_DESC_TYPE_FCFCOE_V1_LENGTH)
8524 goto read_cfg_out;
8525
James Smart912e3ac2011-05-24 11:42:11 -04008526 for (i = 0; i < LPFC_RSRC_DESC_MAX_NUM; i++) {
James Smart8aa134a2012-08-14 14:25:29 -04008527 desc = (struct lpfc_rsrc_desc_fcfcoe *)(pdesc_0 + length * i);
James Smart912e3ac2011-05-24 11:42:11 -04008528 if (LPFC_RSRC_DESC_TYPE_FCFCOE ==
James Smart8aa134a2012-08-14 14:25:29 -04008529 bf_get(lpfc_rsrc_desc_fcfcoe_type, desc)) {
James Smart912e3ac2011-05-24 11:42:11 -04008530 phba->sli4_hba.iov.pf_number =
8531 bf_get(lpfc_rsrc_desc_fcfcoe_pfnum, desc);
8532 phba->sli4_hba.iov.vf_number =
8533 bf_get(lpfc_rsrc_desc_fcfcoe_vfnum, desc);
8534 break;
8535 }
8536 }
8537
8538 if (i < LPFC_RSRC_DESC_MAX_NUM)
8539 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8540 "3027 GET_FUNCTION_CONFIG: pf_number:%d, "
8541 "vf_number:%d\n", phba->sli4_hba.iov.pf_number,
8542 phba->sli4_hba.iov.vf_number);
James Smart8aa134a2012-08-14 14:25:29 -04008543 else
James Smart912e3ac2011-05-24 11:42:11 -04008544 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8545 "3028 GET_FUNCTION_CONFIG: failed to find "
Colin Ian Kingc4dba182018-10-16 18:28:53 +01008546 "Resource Descriptor:x%x\n",
James Smart912e3ac2011-05-24 11:42:11 -04008547 LPFC_RSRC_DESC_TYPE_FCFCOE);
James Smart912e3ac2011-05-24 11:42:11 -04008548
8549read_cfg_out:
8550 mempool_free(pmb, phba->mbox_mem_pool);
James Smartda0436e2009-05-22 14:51:39 -04008551 return rc;
8552}
8553
8554/**
James Smart2fcee4b2010-12-15 17:57:46 -05008555 * lpfc_setup_endian_order - Write endian order to an SLI4 if_type 0 port.
James Smartda0436e2009-05-22 14:51:39 -04008556 * @phba: pointer to lpfc hba data structure.
8557 *
James Smart2fcee4b2010-12-15 17:57:46 -05008558 * This routine is invoked to setup the port-side endian order when
8559 * the port if_type is 0. This routine has no function for other
8560 * if_types.
James Smartda0436e2009-05-22 14:51:39 -04008561 *
8562 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008563 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008564 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04008565 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04008566 **/
8567static int
8568lpfc_setup_endian_order(struct lpfc_hba *phba)
8569{
8570 LPFC_MBOXQ_t *mboxq;
James Smart2fcee4b2010-12-15 17:57:46 -05008571 uint32_t if_type, rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04008572 uint32_t endian_mb_data[2] = {HOST_ENDIAN_LOW_WORD0,
8573 HOST_ENDIAN_HIGH_WORD1};
8574
James Smart2fcee4b2010-12-15 17:57:46 -05008575 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
8576 switch (if_type) {
8577 case LPFC_SLI_INTF_IF_TYPE_0:
8578 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
8579 GFP_KERNEL);
8580 if (!mboxq) {
8581 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8582 "0492 Unable to allocate memory for "
8583 "issuing SLI_CONFIG_SPECIAL mailbox "
8584 "command\n");
8585 return -ENOMEM;
8586 }
James Smartda0436e2009-05-22 14:51:39 -04008587
James Smart2fcee4b2010-12-15 17:57:46 -05008588 /*
8589 * The SLI4_CONFIG_SPECIAL mailbox command requires the first
8590 * two words to contain special data values and no other data.
8591 */
8592 memset(mboxq, 0, sizeof(LPFC_MBOXQ_t));
8593 memcpy(&mboxq->u.mqe, &endian_mb_data, sizeof(endian_mb_data));
8594 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8595 if (rc != MBX_SUCCESS) {
8596 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8597 "0493 SLI_CONFIG_SPECIAL mailbox "
8598 "failed with status x%x\n",
8599 rc);
8600 rc = -EIO;
8601 }
8602 mempool_free(mboxq, phba->mbox_mem_pool);
8603 break;
James Smart27d6ac02018-02-22 08:18:42 -08008604 case LPFC_SLI_INTF_IF_TYPE_6:
James Smart2fcee4b2010-12-15 17:57:46 -05008605 case LPFC_SLI_INTF_IF_TYPE_2:
8606 case LPFC_SLI_INTF_IF_TYPE_1:
8607 default:
8608 break;
James Smartda0436e2009-05-22 14:51:39 -04008609 }
James Smartda0436e2009-05-22 14:51:39 -04008610 return rc;
8611}
8612
8613/**
James Smart895427b2017-02-12 13:52:30 -08008614 * lpfc_sli4_queue_verify - Verify and update EQ counts
James Smartda0436e2009-05-22 14:51:39 -04008615 * @phba: pointer to lpfc hba data structure.
8616 *
James Smart895427b2017-02-12 13:52:30 -08008617 * This routine is invoked to check the user settable queue counts for EQs.
8618 * After this routine is called the counts will be set to valid values that
James Smart5350d872011-10-10 21:33:49 -04008619 * adhere to the constraints of the system's interrupt vectors and the port's
8620 * queue resources.
James Smartda0436e2009-05-22 14:51:39 -04008621 *
8622 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008623 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008624 * -ENOMEM - No available memory
James Smartda0436e2009-05-22 14:51:39 -04008625 **/
8626static int
James Smart5350d872011-10-10 21:33:49 -04008627lpfc_sli4_queue_verify(struct lpfc_hba *phba)
James Smartda0436e2009-05-22 14:51:39 -04008628{
James Smartda0436e2009-05-22 14:51:39 -04008629 /*
James Smart67d12732012-08-03 12:36:13 -04008630 * Sanity check for configured queue parameters against the run-time
James Smartda0436e2009-05-22 14:51:39 -04008631 * device parameters
8632 */
8633
James Smartbcb24f62017-11-20 16:00:36 -08008634 if (phba->nvmet_support) {
James Smart6a828b02019-01-28 11:14:31 -08008635 if (phba->cfg_irq_chann < phba->cfg_nvmet_mrq)
8636 phba->cfg_nvmet_mrq = phba->cfg_irq_chann;
James Smart982ab122019-03-12 16:30:10 -07008637 if (phba->cfg_nvmet_mrq > LPFC_NVMET_MRQ_MAX)
8638 phba->cfg_nvmet_mrq = LPFC_NVMET_MRQ_MAX;
James Smartbcb24f62017-11-20 16:00:36 -08008639 }
James Smart895427b2017-02-12 13:52:30 -08008640
8641 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart6a828b02019-01-28 11:14:31 -08008642 "2574 IO channels: hdwQ %d IRQ %d MRQ: %d\n",
8643 phba->cfg_hdw_queue, phba->cfg_irq_chann,
8644 phba->cfg_nvmet_mrq);
James Smartda0436e2009-05-22 14:51:39 -04008645
James Smartda0436e2009-05-22 14:51:39 -04008646 /* Get EQ depth from module parameter, fake the default for now */
8647 phba->sli4_hba.eq_esize = LPFC_EQE_SIZE_4B;
8648 phba->sli4_hba.eq_ecount = LPFC_EQE_DEF_COUNT;
8649
James Smart5350d872011-10-10 21:33:49 -04008650 /* Get CQ depth from module parameter, fake the default for now */
8651 phba->sli4_hba.cq_esize = LPFC_CQE_SIZE;
8652 phba->sli4_hba.cq_ecount = LPFC_CQE_DEF_COUNT;
James Smart5350d872011-10-10 21:33:49 -04008653 return 0;
James Smart895427b2017-02-12 13:52:30 -08008654}
8655
8656static int
8657lpfc_alloc_nvme_wq_cq(struct lpfc_hba *phba, int wqidx)
8658{
8659 struct lpfc_queue *qdesc;
James Smartc1a21eb2019-03-12 16:30:29 -07008660 int cpu;
James Smart895427b2017-02-12 13:52:30 -08008661
James Smartc1a21eb2019-03-12 16:30:29 -07008662 cpu = lpfc_find_cpu_handle(phba, wqidx, LPFC_FIND_BY_HDWQ);
James Smarta51e41b2017-12-08 17:18:06 -08008663 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_EXPANDED_PAGE_SIZE,
James Smart81b96ed2017-11-20 16:00:29 -08008664 phba->sli4_hba.cq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -07008665 LPFC_CQE_EXP_COUNT, cpu);
James Smart895427b2017-02-12 13:52:30 -08008666 if (!qdesc) {
8667 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8668 "0508 Failed allocate fast-path NVME CQ (%d)\n",
8669 wqidx);
8670 return 1;
8671 }
James Smart7365f6f2018-02-22 08:18:46 -08008672 qdesc->qe_valid = 1;
James Smart5e5b5112019-01-28 11:14:22 -08008673 qdesc->hdwq = wqidx;
James Smartc1a21eb2019-03-12 16:30:29 -07008674 qdesc->chann = cpu;
James Smartcdb42be2019-01-28 11:14:21 -08008675 phba->sli4_hba.hdwq[wqidx].nvme_cq = qdesc;
James Smart895427b2017-02-12 13:52:30 -08008676
James Smarta51e41b2017-12-08 17:18:06 -08008677 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_EXPANDED_PAGE_SIZE,
James Smartc1a21eb2019-03-12 16:30:29 -07008678 LPFC_WQE128_SIZE, LPFC_WQE_EXP_COUNT,
8679 cpu);
James Smart895427b2017-02-12 13:52:30 -08008680 if (!qdesc) {
8681 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8682 "0509 Failed allocate fast-path NVME WQ (%d)\n",
8683 wqidx);
8684 return 1;
8685 }
James Smart5e5b5112019-01-28 11:14:22 -08008686 qdesc->hdwq = wqidx;
James Smart6a828b02019-01-28 11:14:31 -08008687 qdesc->chann = wqidx;
James Smartcdb42be2019-01-28 11:14:21 -08008688 phba->sli4_hba.hdwq[wqidx].nvme_wq = qdesc;
James Smart895427b2017-02-12 13:52:30 -08008689 list_add_tail(&qdesc->wq_list, &phba->sli4_hba.lpfc_wq_list);
8690 return 0;
8691}
8692
8693static int
8694lpfc_alloc_fcp_wq_cq(struct lpfc_hba *phba, int wqidx)
8695{
8696 struct lpfc_queue *qdesc;
James Smartc176ffa2018-01-30 15:58:46 -08008697 uint32_t wqesize;
James Smartc1a21eb2019-03-12 16:30:29 -07008698 int cpu;
James Smart895427b2017-02-12 13:52:30 -08008699
James Smartc1a21eb2019-03-12 16:30:29 -07008700 cpu = lpfc_find_cpu_handle(phba, wqidx, LPFC_FIND_BY_HDWQ);
James Smart895427b2017-02-12 13:52:30 -08008701 /* Create Fast Path FCP CQs */
James Smartc176ffa2018-01-30 15:58:46 -08008702 if (phba->enab_exp_wqcq_pages)
James Smarta51e41b2017-12-08 17:18:06 -08008703 /* Increase the CQ size when WQEs contain an embedded cdb */
8704 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_EXPANDED_PAGE_SIZE,
8705 phba->sli4_hba.cq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -07008706 LPFC_CQE_EXP_COUNT, cpu);
James Smarta51e41b2017-12-08 17:18:06 -08008707
8708 else
8709 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
8710 phba->sli4_hba.cq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -07008711 phba->sli4_hba.cq_ecount, cpu);
James Smart895427b2017-02-12 13:52:30 -08008712 if (!qdesc) {
8713 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8714 "0499 Failed allocate fast-path FCP CQ (%d)\n", wqidx);
8715 return 1;
8716 }
James Smart7365f6f2018-02-22 08:18:46 -08008717 qdesc->qe_valid = 1;
James Smart5e5b5112019-01-28 11:14:22 -08008718 qdesc->hdwq = wqidx;
James Smartc1a21eb2019-03-12 16:30:29 -07008719 qdesc->chann = cpu;
James Smartcdb42be2019-01-28 11:14:21 -08008720 phba->sli4_hba.hdwq[wqidx].fcp_cq = qdesc;
James Smart895427b2017-02-12 13:52:30 -08008721
8722 /* Create Fast Path FCP WQs */
James Smartc176ffa2018-01-30 15:58:46 -08008723 if (phba->enab_exp_wqcq_pages) {
James Smarta51e41b2017-12-08 17:18:06 -08008724 /* Increase the WQ size when WQEs contain an embedded cdb */
James Smartc176ffa2018-01-30 15:58:46 -08008725 wqesize = (phba->fcp_embed_io) ?
8726 LPFC_WQE128_SIZE : phba->sli4_hba.wq_esize;
James Smarta51e41b2017-12-08 17:18:06 -08008727 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_EXPANDED_PAGE_SIZE,
James Smartc176ffa2018-01-30 15:58:46 -08008728 wqesize,
James Smartc1a21eb2019-03-12 16:30:29 -07008729 LPFC_WQE_EXP_COUNT, cpu);
James Smartc176ffa2018-01-30 15:58:46 -08008730 } else
James Smarta51e41b2017-12-08 17:18:06 -08008731 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
8732 phba->sli4_hba.wq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -07008733 phba->sli4_hba.wq_ecount, cpu);
James Smartc176ffa2018-01-30 15:58:46 -08008734
James Smart895427b2017-02-12 13:52:30 -08008735 if (!qdesc) {
8736 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8737 "0503 Failed allocate fast-path FCP WQ (%d)\n",
8738 wqidx);
8739 return 1;
8740 }
James Smart5e5b5112019-01-28 11:14:22 -08008741 qdesc->hdwq = wqidx;
James Smart6a828b02019-01-28 11:14:31 -08008742 qdesc->chann = wqidx;
James Smartcdb42be2019-01-28 11:14:21 -08008743 phba->sli4_hba.hdwq[wqidx].fcp_wq = qdesc;
James Smart895427b2017-02-12 13:52:30 -08008744 list_add_tail(&qdesc->wq_list, &phba->sli4_hba.lpfc_wq_list);
8745 return 0;
James Smart5350d872011-10-10 21:33:49 -04008746}
8747
8748/**
8749 * lpfc_sli4_queue_create - Create all the SLI4 queues
8750 * @phba: pointer to lpfc hba data structure.
8751 *
8752 * This routine is invoked to allocate all the SLI4 queues for the FCoE HBA
8753 * operation. For each SLI4 queue type, the parameters such as queue entry
8754 * count (queue depth) shall be taken from the module parameter. For now,
8755 * we just use some constant number as place holder.
8756 *
8757 * Return codes
Anatol Pomozov4907cb72012-09-01 10:31:09 -07008758 * 0 - successful
James Smart5350d872011-10-10 21:33:49 -04008759 * -ENOMEM - No availble memory
8760 * -EIO - The mailbox failed to complete successfully.
8761 **/
8762int
8763lpfc_sli4_queue_create(struct lpfc_hba *phba)
8764{
8765 struct lpfc_queue *qdesc;
James Smart657add42019-05-21 17:49:06 -07008766 int idx, cpu, eqcpu;
James Smart5e5b5112019-01-28 11:14:22 -08008767 struct lpfc_sli4_hdw_queue *qp;
James Smart657add42019-05-21 17:49:06 -07008768 struct lpfc_vector_map_info *cpup;
8769 struct lpfc_vector_map_info *eqcpup;
James Smart32517fc2019-01-28 11:14:33 -08008770 struct lpfc_eq_intr_info *eqi;
James Smart5350d872011-10-10 21:33:49 -04008771
8772 /*
James Smart67d12732012-08-03 12:36:13 -04008773 * Create HBA Record arrays.
James Smart895427b2017-02-12 13:52:30 -08008774 * Both NVME and FCP will share that same vectors / EQs
James Smart5350d872011-10-10 21:33:49 -04008775 */
James Smart67d12732012-08-03 12:36:13 -04008776 phba->sli4_hba.mq_esize = LPFC_MQE_SIZE;
8777 phba->sli4_hba.mq_ecount = LPFC_MQE_DEF_COUNT;
8778 phba->sli4_hba.wq_esize = LPFC_WQE_SIZE;
8779 phba->sli4_hba.wq_ecount = LPFC_WQE_DEF_COUNT;
8780 phba->sli4_hba.rq_esize = LPFC_RQE_SIZE;
8781 phba->sli4_hba.rq_ecount = LPFC_RQE_DEF_COUNT;
James Smart895427b2017-02-12 13:52:30 -08008782 phba->sli4_hba.eq_esize = LPFC_EQE_SIZE_4B;
8783 phba->sli4_hba.eq_ecount = LPFC_EQE_DEF_COUNT;
8784 phba->sli4_hba.cq_esize = LPFC_CQE_SIZE;
8785 phba->sli4_hba.cq_ecount = LPFC_CQE_DEF_COUNT;
James Smart67d12732012-08-03 12:36:13 -04008786
James Smartcdb42be2019-01-28 11:14:21 -08008787 if (!phba->sli4_hba.hdwq) {
James Smart5e5b5112019-01-28 11:14:22 -08008788 phba->sli4_hba.hdwq = kcalloc(
8789 phba->cfg_hdw_queue, sizeof(struct lpfc_sli4_hdw_queue),
8790 GFP_KERNEL);
8791 if (!phba->sli4_hba.hdwq) {
James Smart895427b2017-02-12 13:52:30 -08008792 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart5e5b5112019-01-28 11:14:22 -08008793 "6427 Failed allocate memory for "
8794 "fast-path Hardware Queue array\n");
James Smart895427b2017-02-12 13:52:30 -08008795 goto out_error;
8796 }
James Smart5e5b5112019-01-28 11:14:22 -08008797 /* Prepare hardware queues to take IO buffers */
8798 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
8799 qp = &phba->sli4_hba.hdwq[idx];
8800 spin_lock_init(&qp->io_buf_list_get_lock);
8801 spin_lock_init(&qp->io_buf_list_put_lock);
8802 INIT_LIST_HEAD(&qp->lpfc_io_buf_list_get);
8803 INIT_LIST_HEAD(&qp->lpfc_io_buf_list_put);
8804 qp->get_io_bufs = 0;
8805 qp->put_io_bufs = 0;
8806 qp->total_io_bufs = 0;
8807 spin_lock_init(&qp->abts_scsi_buf_list_lock);
8808 INIT_LIST_HEAD(&qp->lpfc_abts_scsi_buf_list);
8809 qp->abts_scsi_io_bufs = 0;
8810 spin_lock_init(&qp->abts_nvme_buf_list_lock);
8811 INIT_LIST_HEAD(&qp->lpfc_abts_nvme_buf_list);
8812 qp->abts_nvme_io_bufs = 0;
James Smart895427b2017-02-12 13:52:30 -08008813 }
James Smart67d12732012-08-03 12:36:13 -04008814 }
8815
James Smartcdb42be2019-01-28 11:14:21 -08008816 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
James Smart2d7dbc42017-02-12 13:52:35 -08008817 if (phba->nvmet_support) {
8818 phba->sli4_hba.nvmet_cqset = kcalloc(
8819 phba->cfg_nvmet_mrq,
8820 sizeof(struct lpfc_queue *),
8821 GFP_KERNEL);
8822 if (!phba->sli4_hba.nvmet_cqset) {
8823 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8824 "3121 Fail allocate memory for "
8825 "fast-path CQ set array\n");
8826 goto out_error;
8827 }
8828 phba->sli4_hba.nvmet_mrq_hdr = kcalloc(
8829 phba->cfg_nvmet_mrq,
8830 sizeof(struct lpfc_queue *),
8831 GFP_KERNEL);
8832 if (!phba->sli4_hba.nvmet_mrq_hdr) {
8833 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8834 "3122 Fail allocate memory for "
8835 "fast-path RQ set hdr array\n");
8836 goto out_error;
8837 }
8838 phba->sli4_hba.nvmet_mrq_data = kcalloc(
8839 phba->cfg_nvmet_mrq,
8840 sizeof(struct lpfc_queue *),
8841 GFP_KERNEL);
8842 if (!phba->sli4_hba.nvmet_mrq_data) {
8843 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8844 "3124 Fail allocate memory for "
8845 "fast-path RQ set data array\n");
8846 goto out_error;
8847 }
8848 }
James Smart67d12732012-08-03 12:36:13 -04008849 }
James Smartda0436e2009-05-22 14:51:39 -04008850
James Smart895427b2017-02-12 13:52:30 -08008851 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_wq_list);
James Smart67d12732012-08-03 12:36:13 -04008852
James Smart895427b2017-02-12 13:52:30 -08008853 /* Create HBA Event Queues (EQs) */
James Smart657add42019-05-21 17:49:06 -07008854 for_each_present_cpu(cpu) {
8855 /* We only want to create 1 EQ per vector, even though
8856 * multiple CPUs might be using that vector. so only
8857 * selects the CPUs that are LPFC_CPU_FIRST_IRQ.
James Smart6a828b02019-01-28 11:14:31 -08008858 */
James Smart657add42019-05-21 17:49:06 -07008859 cpup = &phba->sli4_hba.cpu_map[cpu];
8860 if (!(cpup->flag & LPFC_CPU_FIRST_IRQ))
James Smart6a828b02019-01-28 11:14:31 -08008861 continue;
James Smart657add42019-05-21 17:49:06 -07008862
8863 /* Get a ptr to the Hardware Queue associated with this CPU */
8864 qp = &phba->sli4_hba.hdwq[cpup->hdwq];
8865
8866 /* Allocate an EQ */
James Smart81b96ed2017-11-20 16:00:29 -08008867 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
8868 phba->sli4_hba.eq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -07008869 phba->sli4_hba.eq_ecount, cpu);
James Smartda0436e2009-05-22 14:51:39 -04008870 if (!qdesc) {
8871 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart657add42019-05-21 17:49:06 -07008872 "0497 Failed allocate EQ (%d)\n",
8873 cpup->hdwq);
James Smart67d12732012-08-03 12:36:13 -04008874 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04008875 }
James Smart7365f6f2018-02-22 08:18:46 -08008876 qdesc->qe_valid = 1;
James Smart657add42019-05-21 17:49:06 -07008877 qdesc->hdwq = cpup->hdwq;
James Smart3ad348d2019-08-14 16:56:43 -07008878 qdesc->chann = cpu; /* First CPU this EQ is affinitized to */
James Smart32517fc2019-01-28 11:14:33 -08008879 qdesc->last_cpu = qdesc->chann;
James Smart657add42019-05-21 17:49:06 -07008880
8881 /* Save the allocated EQ in the Hardware Queue */
8882 qp->hba_eq = qdesc;
8883
James Smart32517fc2019-01-28 11:14:33 -08008884 eqi = per_cpu_ptr(phba->sli4_hba.eq_info, qdesc->last_cpu);
8885 list_add(&qdesc->cpu_list, &eqi->list);
James Smartda0436e2009-05-22 14:51:39 -04008886 }
8887
James Smart657add42019-05-21 17:49:06 -07008888 /* Now we need to populate the other Hardware Queues, that share
8889 * an IRQ vector, with the associated EQ ptr.
8890 */
8891 for_each_present_cpu(cpu) {
8892 cpup = &phba->sli4_hba.cpu_map[cpu];
8893
8894 /* Check for EQ already allocated in previous loop */
8895 if (cpup->flag & LPFC_CPU_FIRST_IRQ)
8896 continue;
8897
8898 /* Check for multiple CPUs per hdwq */
8899 qp = &phba->sli4_hba.hdwq[cpup->hdwq];
8900 if (qp->hba_eq)
8901 continue;
8902
8903 /* We need to share an EQ for this hdwq */
8904 eqcpu = lpfc_find_cpu_handle(phba, cpup->eq, LPFC_FIND_BY_EQ);
8905 eqcpup = &phba->sli4_hba.cpu_map[eqcpu];
8906 qp->hba_eq = phba->sli4_hba.hdwq[eqcpup->hdwq].hba_eq;
8907 }
James Smart895427b2017-02-12 13:52:30 -08008908
James Smartcdb42be2019-01-28 11:14:21 -08008909 /* Allocate SCSI SLI4 CQ/WQs */
James Smart6a828b02019-01-28 11:14:31 -08008910 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
James Smart895427b2017-02-12 13:52:30 -08008911 if (lpfc_alloc_fcp_wq_cq(phba, idx))
8912 goto out_error;
James Smart6a828b02019-01-28 11:14:31 -08008913 }
James Smart895427b2017-02-12 13:52:30 -08008914
James Smartcdb42be2019-01-28 11:14:21 -08008915 /* Allocate NVME SLI4 CQ/WQs */
8916 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
James Smart6a828b02019-01-28 11:14:31 -08008917 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
James Smartcdb42be2019-01-28 11:14:21 -08008918 if (lpfc_alloc_nvme_wq_cq(phba, idx))
8919 goto out_error;
James Smart6a828b02019-01-28 11:14:31 -08008920 }
James Smart67d12732012-08-03 12:36:13 -04008921
James Smartcdb42be2019-01-28 11:14:21 -08008922 if (phba->nvmet_support) {
8923 for (idx = 0; idx < phba->cfg_nvmet_mrq; idx++) {
James Smartc1a21eb2019-03-12 16:30:29 -07008924 cpu = lpfc_find_cpu_handle(phba, idx,
8925 LPFC_FIND_BY_HDWQ);
James Smartcdb42be2019-01-28 11:14:21 -08008926 qdesc = lpfc_sli4_queue_alloc(
8927 phba,
James Smart81b96ed2017-11-20 16:00:29 -08008928 LPFC_DEFAULT_PAGE_SIZE,
8929 phba->sli4_hba.cq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -07008930 phba->sli4_hba.cq_ecount,
8931 cpu);
James Smartcdb42be2019-01-28 11:14:21 -08008932 if (!qdesc) {
8933 lpfc_printf_log(
8934 phba, KERN_ERR, LOG_INIT,
8935 "3142 Failed allocate NVME "
8936 "CQ Set (%d)\n", idx);
8937 goto out_error;
8938 }
8939 qdesc->qe_valid = 1;
James Smart5e5b5112019-01-28 11:14:22 -08008940 qdesc->hdwq = idx;
James Smartc1a21eb2019-03-12 16:30:29 -07008941 qdesc->chann = cpu;
James Smartcdb42be2019-01-28 11:14:21 -08008942 phba->sli4_hba.nvmet_cqset[idx] = qdesc;
James Smart2d7dbc42017-02-12 13:52:35 -08008943 }
James Smart2d7dbc42017-02-12 13:52:35 -08008944 }
8945 }
8946
James Smartda0436e2009-05-22 14:51:39 -04008947 /*
James Smart67d12732012-08-03 12:36:13 -04008948 * Create Slow Path Completion Queues (CQs)
James Smartda0436e2009-05-22 14:51:39 -04008949 */
8950
James Smartc1a21eb2019-03-12 16:30:29 -07008951 cpu = lpfc_find_cpu_handle(phba, 0, LPFC_FIND_BY_EQ);
James Smartda0436e2009-05-22 14:51:39 -04008952 /* Create slow-path Mailbox Command Complete Queue */
James Smart81b96ed2017-11-20 16:00:29 -08008953 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
8954 phba->sli4_hba.cq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -07008955 phba->sli4_hba.cq_ecount, cpu);
James Smartda0436e2009-05-22 14:51:39 -04008956 if (!qdesc) {
8957 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8958 "0500 Failed allocate slow-path mailbox CQ\n");
James Smart67d12732012-08-03 12:36:13 -04008959 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04008960 }
James Smart7365f6f2018-02-22 08:18:46 -08008961 qdesc->qe_valid = 1;
James Smartda0436e2009-05-22 14:51:39 -04008962 phba->sli4_hba.mbx_cq = qdesc;
8963
8964 /* Create slow-path ELS Complete Queue */
James Smart81b96ed2017-11-20 16:00:29 -08008965 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
8966 phba->sli4_hba.cq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -07008967 phba->sli4_hba.cq_ecount, cpu);
James Smartda0436e2009-05-22 14:51:39 -04008968 if (!qdesc) {
8969 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8970 "0501 Failed allocate slow-path ELS CQ\n");
James Smart67d12732012-08-03 12:36:13 -04008971 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04008972 }
James Smart7365f6f2018-02-22 08:18:46 -08008973 qdesc->qe_valid = 1;
James Smart6a828b02019-01-28 11:14:31 -08008974 qdesc->chann = 0;
James Smartda0436e2009-05-22 14:51:39 -04008975 phba->sli4_hba.els_cq = qdesc;
8976
James Smartda0436e2009-05-22 14:51:39 -04008977
James Smart5350d872011-10-10 21:33:49 -04008978 /*
James Smart67d12732012-08-03 12:36:13 -04008979 * Create Slow Path Work Queues (WQs)
James Smart5350d872011-10-10 21:33:49 -04008980 */
James Smartda0436e2009-05-22 14:51:39 -04008981
8982 /* Create Mailbox Command Queue */
James Smartda0436e2009-05-22 14:51:39 -04008983
James Smart81b96ed2017-11-20 16:00:29 -08008984 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
8985 phba->sli4_hba.mq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -07008986 phba->sli4_hba.mq_ecount, cpu);
James Smartda0436e2009-05-22 14:51:39 -04008987 if (!qdesc) {
8988 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8989 "0505 Failed allocate slow-path MQ\n");
James Smart67d12732012-08-03 12:36:13 -04008990 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04008991 }
James Smart6a828b02019-01-28 11:14:31 -08008992 qdesc->chann = 0;
James Smartda0436e2009-05-22 14:51:39 -04008993 phba->sli4_hba.mbx_wq = qdesc;
8994
8995 /*
James Smart67d12732012-08-03 12:36:13 -04008996 * Create ELS Work Queues
James Smartda0436e2009-05-22 14:51:39 -04008997 */
James Smartda0436e2009-05-22 14:51:39 -04008998
8999 /* Create slow-path ELS Work Queue */
James Smart81b96ed2017-11-20 16:00:29 -08009000 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
9001 phba->sli4_hba.wq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -07009002 phba->sli4_hba.wq_ecount, cpu);
James Smartda0436e2009-05-22 14:51:39 -04009003 if (!qdesc) {
9004 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9005 "0504 Failed allocate slow-path ELS WQ\n");
James Smart67d12732012-08-03 12:36:13 -04009006 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04009007 }
James Smart6a828b02019-01-28 11:14:31 -08009008 qdesc->chann = 0;
James Smartda0436e2009-05-22 14:51:39 -04009009 phba->sli4_hba.els_wq = qdesc;
James Smart895427b2017-02-12 13:52:30 -08009010 list_add_tail(&qdesc->wq_list, &phba->sli4_hba.lpfc_wq_list);
9011
9012 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
9013 /* Create NVME LS Complete Queue */
James Smart81b96ed2017-11-20 16:00:29 -08009014 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
9015 phba->sli4_hba.cq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -07009016 phba->sli4_hba.cq_ecount, cpu);
James Smart895427b2017-02-12 13:52:30 -08009017 if (!qdesc) {
9018 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9019 "6079 Failed allocate NVME LS CQ\n");
9020 goto out_error;
9021 }
James Smart6a828b02019-01-28 11:14:31 -08009022 qdesc->chann = 0;
James Smart7365f6f2018-02-22 08:18:46 -08009023 qdesc->qe_valid = 1;
James Smart895427b2017-02-12 13:52:30 -08009024 phba->sli4_hba.nvmels_cq = qdesc;
9025
9026 /* Create NVME LS Work Queue */
James Smart81b96ed2017-11-20 16:00:29 -08009027 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
9028 phba->sli4_hba.wq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -07009029 phba->sli4_hba.wq_ecount, cpu);
James Smart895427b2017-02-12 13:52:30 -08009030 if (!qdesc) {
9031 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9032 "6080 Failed allocate NVME LS WQ\n");
9033 goto out_error;
9034 }
James Smart6a828b02019-01-28 11:14:31 -08009035 qdesc->chann = 0;
James Smart895427b2017-02-12 13:52:30 -08009036 phba->sli4_hba.nvmels_wq = qdesc;
9037 list_add_tail(&qdesc->wq_list, &phba->sli4_hba.lpfc_wq_list);
9038 }
James Smartda0436e2009-05-22 14:51:39 -04009039
James Smartda0436e2009-05-22 14:51:39 -04009040 /*
9041 * Create Receive Queue (RQ)
9042 */
James Smartda0436e2009-05-22 14:51:39 -04009043
9044 /* Create Receive Queue for header */
James Smart81b96ed2017-11-20 16:00:29 -08009045 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
9046 phba->sli4_hba.rq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -07009047 phba->sli4_hba.rq_ecount, cpu);
James Smartda0436e2009-05-22 14:51:39 -04009048 if (!qdesc) {
9049 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9050 "0506 Failed allocate receive HRQ\n");
James Smart67d12732012-08-03 12:36:13 -04009051 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04009052 }
9053 phba->sli4_hba.hdr_rq = qdesc;
9054
9055 /* Create Receive Queue for data */
James Smart81b96ed2017-11-20 16:00:29 -08009056 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
9057 phba->sli4_hba.rq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -07009058 phba->sli4_hba.rq_ecount, cpu);
James Smartda0436e2009-05-22 14:51:39 -04009059 if (!qdesc) {
9060 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9061 "0507 Failed allocate receive DRQ\n");
James Smart67d12732012-08-03 12:36:13 -04009062 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04009063 }
9064 phba->sli4_hba.dat_rq = qdesc;
9065
James Smartcdb42be2019-01-28 11:14:21 -08009066 if ((phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) &&
9067 phba->nvmet_support) {
James Smart2d7dbc42017-02-12 13:52:35 -08009068 for (idx = 0; idx < phba->cfg_nvmet_mrq; idx++) {
James Smartc1a21eb2019-03-12 16:30:29 -07009069 cpu = lpfc_find_cpu_handle(phba, idx,
9070 LPFC_FIND_BY_HDWQ);
James Smart2d7dbc42017-02-12 13:52:35 -08009071 /* Create NVMET Receive Queue for header */
9072 qdesc = lpfc_sli4_queue_alloc(phba,
James Smart81b96ed2017-11-20 16:00:29 -08009073 LPFC_DEFAULT_PAGE_SIZE,
James Smart2d7dbc42017-02-12 13:52:35 -08009074 phba->sli4_hba.rq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -07009075 LPFC_NVMET_RQE_DEF_COUNT,
9076 cpu);
James Smart2d7dbc42017-02-12 13:52:35 -08009077 if (!qdesc) {
9078 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9079 "3146 Failed allocate "
9080 "receive HRQ\n");
9081 goto out_error;
9082 }
James Smart5e5b5112019-01-28 11:14:22 -08009083 qdesc->hdwq = idx;
James Smart2d7dbc42017-02-12 13:52:35 -08009084 phba->sli4_hba.nvmet_mrq_hdr[idx] = qdesc;
9085
9086 /* Only needed for header of RQ pair */
James Smartc1a21eb2019-03-12 16:30:29 -07009087 qdesc->rqbp = kzalloc_node(sizeof(*qdesc->rqbp),
9088 GFP_KERNEL,
9089 cpu_to_node(cpu));
James Smart2d7dbc42017-02-12 13:52:35 -08009090 if (qdesc->rqbp == NULL) {
9091 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9092 "6131 Failed allocate "
9093 "Header RQBP\n");
9094 goto out_error;
9095 }
9096
Dick Kennedy4b40d022017-08-23 16:55:38 -07009097 /* Put list in known state in case driver load fails. */
9098 INIT_LIST_HEAD(&qdesc->rqbp->rqb_buffer_list);
9099
James Smart2d7dbc42017-02-12 13:52:35 -08009100 /* Create NVMET Receive Queue for data */
9101 qdesc = lpfc_sli4_queue_alloc(phba,
James Smart81b96ed2017-11-20 16:00:29 -08009102 LPFC_DEFAULT_PAGE_SIZE,
James Smart2d7dbc42017-02-12 13:52:35 -08009103 phba->sli4_hba.rq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -07009104 LPFC_NVMET_RQE_DEF_COUNT,
9105 cpu);
James Smart2d7dbc42017-02-12 13:52:35 -08009106 if (!qdesc) {
9107 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9108 "3156 Failed allocate "
9109 "receive DRQ\n");
9110 goto out_error;
9111 }
James Smart5e5b5112019-01-28 11:14:22 -08009112 qdesc->hdwq = idx;
James Smart2d7dbc42017-02-12 13:52:35 -08009113 phba->sli4_hba.nvmet_mrq_data[idx] = qdesc;
9114 }
9115 }
9116
James Smart4c47efc2019-01-28 11:14:25 -08009117#if defined(BUILD_NVME)
9118 /* Clear NVME stats */
9119 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
9120 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
9121 memset(&phba->sli4_hba.hdwq[idx].nvme_cstat, 0,
9122 sizeof(phba->sli4_hba.hdwq[idx].nvme_cstat));
9123 }
9124 }
9125#endif
9126
9127 /* Clear SCSI stats */
9128 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) {
9129 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
9130 memset(&phba->sli4_hba.hdwq[idx].scsi_cstat, 0,
9131 sizeof(phba->sli4_hba.hdwq[idx].scsi_cstat));
9132 }
9133 }
9134
James Smartda0436e2009-05-22 14:51:39 -04009135 return 0;
9136
James Smartda0436e2009-05-22 14:51:39 -04009137out_error:
James Smart67d12732012-08-03 12:36:13 -04009138 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04009139 return -ENOMEM;
9140}
9141
James Smart895427b2017-02-12 13:52:30 -08009142static inline void
9143__lpfc_sli4_release_queue(struct lpfc_queue **qp)
9144{
9145 if (*qp != NULL) {
9146 lpfc_sli4_queue_free(*qp);
9147 *qp = NULL;
9148 }
9149}
9150
9151static inline void
9152lpfc_sli4_release_queues(struct lpfc_queue ***qs, int max)
9153{
9154 int idx;
9155
9156 if (*qs == NULL)
9157 return;
9158
9159 for (idx = 0; idx < max; idx++)
9160 __lpfc_sli4_release_queue(&(*qs)[idx]);
9161
9162 kfree(*qs);
9163 *qs = NULL;
9164}
9165
9166static inline void
James Smart6a828b02019-01-28 11:14:31 -08009167lpfc_sli4_release_hdwq(struct lpfc_hba *phba)
James Smart895427b2017-02-12 13:52:30 -08009168{
James Smart6a828b02019-01-28 11:14:31 -08009169 struct lpfc_sli4_hdw_queue *hdwq;
James Smart657add42019-05-21 17:49:06 -07009170 struct lpfc_queue *eq;
James Smartcdb42be2019-01-28 11:14:21 -08009171 uint32_t idx;
9172
James Smart6a828b02019-01-28 11:14:31 -08009173 hdwq = phba->sli4_hba.hdwq;
James Smart6a828b02019-01-28 11:14:31 -08009174
James Smart657add42019-05-21 17:49:06 -07009175 /* Loop thru all Hardware Queues */
9176 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
9177 /* Free the CQ/WQ corresponding to the Hardware Queue */
James Smartcdb42be2019-01-28 11:14:21 -08009178 lpfc_sli4_queue_free(hdwq[idx].fcp_cq);
9179 lpfc_sli4_queue_free(hdwq[idx].nvme_cq);
9180 lpfc_sli4_queue_free(hdwq[idx].fcp_wq);
9181 lpfc_sli4_queue_free(hdwq[idx].nvme_wq);
James Smart657add42019-05-21 17:49:06 -07009182 hdwq[idx].hba_eq = NULL;
James Smartcdb42be2019-01-28 11:14:21 -08009183 hdwq[idx].fcp_cq = NULL;
9184 hdwq[idx].nvme_cq = NULL;
9185 hdwq[idx].fcp_wq = NULL;
9186 hdwq[idx].nvme_wq = NULL;
James Smart895427b2017-02-12 13:52:30 -08009187 }
James Smart657add42019-05-21 17:49:06 -07009188 /* Loop thru all IRQ vectors */
9189 for (idx = 0; idx < phba->cfg_irq_chann; idx++) {
9190 /* Free the EQ corresponding to the IRQ vector */
9191 eq = phba->sli4_hba.hba_eq_hdl[idx].eq;
9192 lpfc_sli4_queue_free(eq);
9193 phba->sli4_hba.hba_eq_hdl[idx].eq = NULL;
9194 }
James Smart895427b2017-02-12 13:52:30 -08009195}
9196
James Smartda0436e2009-05-22 14:51:39 -04009197/**
9198 * lpfc_sli4_queue_destroy - Destroy all the SLI4 queues
9199 * @phba: pointer to lpfc hba data structure.
9200 *
9201 * This routine is invoked to release all the SLI4 queues with the FCoE HBA
9202 * operation.
9203 *
9204 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02009205 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009206 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04009207 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04009208 **/
James Smart5350d872011-10-10 21:33:49 -04009209void
James Smartda0436e2009-05-22 14:51:39 -04009210lpfc_sli4_queue_destroy(struct lpfc_hba *phba)
9211{
James Smart4645f7b2019-03-12 16:30:14 -07009212 /*
9213 * Set FREE_INIT before beginning to free the queues.
9214 * Wait until the users of queues to acknowledge to
9215 * release queues by clearing FREE_WAIT.
9216 */
9217 spin_lock_irq(&phba->hbalock);
9218 phba->sli.sli_flag |= LPFC_QUEUE_FREE_INIT;
9219 while (phba->sli.sli_flag & LPFC_QUEUE_FREE_WAIT) {
9220 spin_unlock_irq(&phba->hbalock);
9221 msleep(20);
9222 spin_lock_irq(&phba->hbalock);
9223 }
9224 spin_unlock_irq(&phba->hbalock);
9225
James Smart895427b2017-02-12 13:52:30 -08009226 /* Release HBA eqs */
James Smartcdb42be2019-01-28 11:14:21 -08009227 if (phba->sli4_hba.hdwq)
James Smart6a828b02019-01-28 11:14:31 -08009228 lpfc_sli4_release_hdwq(phba);
James Smartda0436e2009-05-22 14:51:39 -04009229
James Smartbcb24f62017-11-20 16:00:36 -08009230 if (phba->nvmet_support) {
9231 lpfc_sli4_release_queues(&phba->sli4_hba.nvmet_cqset,
9232 phba->cfg_nvmet_mrq);
James Smart2d7dbc42017-02-12 13:52:35 -08009233
James Smartbcb24f62017-11-20 16:00:36 -08009234 lpfc_sli4_release_queues(&phba->sli4_hba.nvmet_mrq_hdr,
9235 phba->cfg_nvmet_mrq);
9236 lpfc_sli4_release_queues(&phba->sli4_hba.nvmet_mrq_data,
9237 phba->cfg_nvmet_mrq);
9238 }
James Smart2d7dbc42017-02-12 13:52:35 -08009239
James Smartda0436e2009-05-22 14:51:39 -04009240 /* Release mailbox command work queue */
James Smart895427b2017-02-12 13:52:30 -08009241 __lpfc_sli4_release_queue(&phba->sli4_hba.mbx_wq);
James Smartda0436e2009-05-22 14:51:39 -04009242
9243 /* Release ELS work queue */
James Smart895427b2017-02-12 13:52:30 -08009244 __lpfc_sli4_release_queue(&phba->sli4_hba.els_wq);
9245
9246 /* Release ELS work queue */
9247 __lpfc_sli4_release_queue(&phba->sli4_hba.nvmels_wq);
James Smartda0436e2009-05-22 14:51:39 -04009248
9249 /* Release unsolicited receive queue */
James Smart895427b2017-02-12 13:52:30 -08009250 __lpfc_sli4_release_queue(&phba->sli4_hba.hdr_rq);
9251 __lpfc_sli4_release_queue(&phba->sli4_hba.dat_rq);
James Smartda0436e2009-05-22 14:51:39 -04009252
James Smartda0436e2009-05-22 14:51:39 -04009253 /* Release ELS complete queue */
James Smart895427b2017-02-12 13:52:30 -08009254 __lpfc_sli4_release_queue(&phba->sli4_hba.els_cq);
9255
9256 /* Release NVME LS complete queue */
9257 __lpfc_sli4_release_queue(&phba->sli4_hba.nvmels_cq);
James Smartda0436e2009-05-22 14:51:39 -04009258
9259 /* Release mailbox command complete queue */
James Smart895427b2017-02-12 13:52:30 -08009260 __lpfc_sli4_release_queue(&phba->sli4_hba.mbx_cq);
9261
9262 /* Everything on this list has been freed */
9263 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_wq_list);
James Smart4645f7b2019-03-12 16:30:14 -07009264
9265 /* Done with freeing the queues */
9266 spin_lock_irq(&phba->hbalock);
9267 phba->sli.sli_flag &= ~LPFC_QUEUE_FREE_INIT;
9268 spin_unlock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -08009269}
9270
9271int
James Smart895427b2017-02-12 13:52:30 -08009272lpfc_free_rq_buffer(struct lpfc_hba *phba, struct lpfc_queue *rq)
9273{
9274 struct lpfc_rqb *rqbp;
9275 struct lpfc_dmabuf *h_buf;
9276 struct rqb_dmabuf *rqb_buffer;
9277
9278 rqbp = rq->rqbp;
9279 while (!list_empty(&rqbp->rqb_buffer_list)) {
9280 list_remove_head(&rqbp->rqb_buffer_list, h_buf,
9281 struct lpfc_dmabuf, list);
9282
9283 rqb_buffer = container_of(h_buf, struct rqb_dmabuf, hbuf);
9284 (rqbp->rqb_free_buffer)(phba, rqb_buffer);
9285 rqbp->buffer_count--;
9286 }
9287 return 1;
9288}
9289
9290static int
9291lpfc_create_wq_cq(struct lpfc_hba *phba, struct lpfc_queue *eq,
9292 struct lpfc_queue *cq, struct lpfc_queue *wq, uint16_t *cq_map,
9293 int qidx, uint32_t qtype)
9294{
9295 struct lpfc_sli_ring *pring;
9296 int rc;
9297
9298 if (!eq || !cq || !wq) {
9299 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9300 "6085 Fast-path %s (%d) not allocated\n",
9301 ((eq) ? ((cq) ? "WQ" : "CQ") : "EQ"), qidx);
9302 return -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04009303 }
James Smartda0436e2009-05-22 14:51:39 -04009304
James Smart895427b2017-02-12 13:52:30 -08009305 /* create the Cq first */
9306 rc = lpfc_cq_create(phba, cq, eq,
9307 (qtype == LPFC_MBOX) ? LPFC_MCQ : LPFC_WCQ, qtype);
9308 if (rc) {
9309 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9310 "6086 Failed setup of CQ (%d), rc = 0x%x\n",
9311 qidx, (uint32_t)rc);
9312 return rc;
9313 }
9314
9315 if (qtype != LPFC_MBOX) {
James Smartcdb42be2019-01-28 11:14:21 -08009316 /* Setup cq_map for fast lookup */
James Smart895427b2017-02-12 13:52:30 -08009317 if (cq_map)
9318 *cq_map = cq->queue_id;
9319
9320 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9321 "6087 CQ setup: cq[%d]-id=%d, parent eq[%d]-id=%d\n",
9322 qidx, cq->queue_id, qidx, eq->queue_id);
9323
9324 /* create the wq */
9325 rc = lpfc_wq_create(phba, wq, cq, qtype);
9326 if (rc) {
9327 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartc835c082019-03-12 16:30:30 -07009328 "4618 Fail setup fastpath WQ (%d), rc = 0x%x\n",
James Smart895427b2017-02-12 13:52:30 -08009329 qidx, (uint32_t)rc);
9330 /* no need to tear down cq - caller will do so */
9331 return rc;
9332 }
9333
9334 /* Bind this CQ/WQ to the NVME ring */
9335 pring = wq->pring;
9336 pring->sli.sli4.wqp = (void *)wq;
9337 cq->pring = pring;
9338
9339 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9340 "2593 WQ setup: wq[%d]-id=%d assoc=%d, cq[%d]-id=%d\n",
9341 qidx, wq->queue_id, wq->assoc_qid, qidx, cq->queue_id);
9342 } else {
9343 rc = lpfc_mq_create(phba, wq, cq, LPFC_MBOX);
9344 if (rc) {
9345 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9346 "0539 Failed setup of slow-path MQ: "
9347 "rc = 0x%x\n", rc);
9348 /* no need to tear down cq - caller will do so */
9349 return rc;
9350 }
9351
9352 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9353 "2589 MBX MQ setup: wq-id=%d, parent cq-id=%d\n",
9354 phba->sli4_hba.mbx_wq->queue_id,
9355 phba->sli4_hba.mbx_cq->queue_id);
9356 }
9357
9358 return 0;
James Smartda0436e2009-05-22 14:51:39 -04009359}
9360
9361/**
James Smart6a828b02019-01-28 11:14:31 -08009362 * lpfc_setup_cq_lookup - Setup the CQ lookup table
9363 * @phba: pointer to lpfc hba data structure.
9364 *
9365 * This routine will populate the cq_lookup table by all
9366 * available CQ queue_id's.
9367 **/
Bart Van Assche3999df72019-03-28 11:06:16 -07009368static void
James Smart6a828b02019-01-28 11:14:31 -08009369lpfc_setup_cq_lookup(struct lpfc_hba *phba)
9370{
9371 struct lpfc_queue *eq, *childq;
James Smart6a828b02019-01-28 11:14:31 -08009372 int qidx;
9373
James Smart6a828b02019-01-28 11:14:31 -08009374 memset(phba->sli4_hba.cq_lookup, 0,
9375 (sizeof(struct lpfc_queue *) * (phba->sli4_hba.cq_max + 1)));
James Smart657add42019-05-21 17:49:06 -07009376 /* Loop thru all IRQ vectors */
James Smart6a828b02019-01-28 11:14:31 -08009377 for (qidx = 0; qidx < phba->cfg_irq_chann; qidx++) {
James Smart657add42019-05-21 17:49:06 -07009378 /* Get the EQ corresponding to the IRQ vector */
9379 eq = phba->sli4_hba.hba_eq_hdl[qidx].eq;
James Smart6a828b02019-01-28 11:14:31 -08009380 if (!eq)
9381 continue;
James Smart657add42019-05-21 17:49:06 -07009382 /* Loop through all CQs associated with that EQ */
James Smart6a828b02019-01-28 11:14:31 -08009383 list_for_each_entry(childq, &eq->child_list, list) {
9384 if (childq->queue_id > phba->sli4_hba.cq_max)
9385 continue;
9386 if ((childq->subtype == LPFC_FCP) ||
9387 (childq->subtype == LPFC_NVME))
9388 phba->sli4_hba.cq_lookup[childq->queue_id] =
9389 childq;
9390 }
9391 }
9392}
9393
9394/**
James Smartda0436e2009-05-22 14:51:39 -04009395 * lpfc_sli4_queue_setup - Set up all the SLI4 queues
9396 * @phba: pointer to lpfc hba data structure.
9397 *
9398 * This routine is invoked to set up all the SLI4 queues for the FCoE HBA
9399 * operation.
9400 *
9401 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02009402 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009403 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04009404 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04009405 **/
9406int
9407lpfc_sli4_queue_setup(struct lpfc_hba *phba)
9408{
James Smart962bc512013-01-03 15:44:00 -05009409 uint32_t shdr_status, shdr_add_status;
9410 union lpfc_sli4_cfg_shdr *shdr;
James Smart657add42019-05-21 17:49:06 -07009411 struct lpfc_vector_map_info *cpup;
James Smartcdb42be2019-01-28 11:14:21 -08009412 struct lpfc_sli4_hdw_queue *qp;
James Smart962bc512013-01-03 15:44:00 -05009413 LPFC_MBOXQ_t *mboxq;
James Smart657add42019-05-21 17:49:06 -07009414 int qidx, cpu;
James Smartcb733e32019-01-28 11:14:32 -08009415 uint32_t length, usdelay;
James Smart895427b2017-02-12 13:52:30 -08009416 int rc = -ENOMEM;
James Smart962bc512013-01-03 15:44:00 -05009417
9418 /* Check for dual-ULP support */
9419 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9420 if (!mboxq) {
9421 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9422 "3249 Unable to allocate memory for "
9423 "QUERY_FW_CFG mailbox command\n");
9424 return -ENOMEM;
9425 }
9426 length = (sizeof(struct lpfc_mbx_query_fw_config) -
9427 sizeof(struct lpfc_sli4_cfg_mhdr));
9428 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
9429 LPFC_MBOX_OPCODE_QUERY_FW_CFG,
9430 length, LPFC_SLI4_MBX_EMBED);
9431
9432 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
9433
9434 shdr = (union lpfc_sli4_cfg_shdr *)
9435 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
9436 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9437 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9438 if (shdr_status || shdr_add_status || rc) {
9439 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9440 "3250 QUERY_FW_CFG mailbox failed with status "
9441 "x%x add_status x%x, mbx status x%x\n",
9442 shdr_status, shdr_add_status, rc);
9443 if (rc != MBX_TIMEOUT)
9444 mempool_free(mboxq, phba->mbox_mem_pool);
9445 rc = -ENXIO;
9446 goto out_error;
9447 }
9448
9449 phba->sli4_hba.fw_func_mode =
9450 mboxq->u.mqe.un.query_fw_cfg.rsp.function_mode;
9451 phba->sli4_hba.ulp0_mode = mboxq->u.mqe.un.query_fw_cfg.rsp.ulp0_mode;
9452 phba->sli4_hba.ulp1_mode = mboxq->u.mqe.un.query_fw_cfg.rsp.ulp1_mode;
James Smart8b017a32015-05-21 13:55:18 -04009453 phba->sli4_hba.physical_port =
9454 mboxq->u.mqe.un.query_fw_cfg.rsp.physical_port;
James Smart962bc512013-01-03 15:44:00 -05009455 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9456 "3251 QUERY_FW_CFG: func_mode:x%x, ulp0_mode:x%x, "
9457 "ulp1_mode:x%x\n", phba->sli4_hba.fw_func_mode,
9458 phba->sli4_hba.ulp0_mode, phba->sli4_hba.ulp1_mode);
9459
9460 if (rc != MBX_TIMEOUT)
9461 mempool_free(mboxq, phba->mbox_mem_pool);
James Smartda0436e2009-05-22 14:51:39 -04009462
9463 /*
James Smart67d12732012-08-03 12:36:13 -04009464 * Set up HBA Event Queues (EQs)
James Smartda0436e2009-05-22 14:51:39 -04009465 */
James Smartcdb42be2019-01-28 11:14:21 -08009466 qp = phba->sli4_hba.hdwq;
James Smartda0436e2009-05-22 14:51:39 -04009467
James Smart67d12732012-08-03 12:36:13 -04009468 /* Set up HBA event queue */
James Smartcdb42be2019-01-28 11:14:21 -08009469 if (!qp) {
James Smart2e90f4b2011-12-13 13:22:37 -05009470 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9471 "3147 Fast-path EQs not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05009472 rc = -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04009473 goto out_error;
James Smart2e90f4b2011-12-13 13:22:37 -05009474 }
James Smart657add42019-05-21 17:49:06 -07009475
9476 /* Loop thru all IRQ vectors */
James Smart6a828b02019-01-28 11:14:31 -08009477 for (qidx = 0; qidx < phba->cfg_irq_chann; qidx++) {
James Smart657add42019-05-21 17:49:06 -07009478 /* Create HBA Event Queues (EQs) in order */
9479 for_each_present_cpu(cpu) {
9480 cpup = &phba->sli4_hba.cpu_map[cpu];
9481
9482 /* Look for the CPU thats using that vector with
9483 * LPFC_CPU_FIRST_IRQ set.
9484 */
9485 if (!(cpup->flag & LPFC_CPU_FIRST_IRQ))
9486 continue;
9487 if (qidx != cpup->eq)
9488 continue;
9489
9490 /* Create an EQ for that vector */
9491 rc = lpfc_eq_create(phba, qp[cpup->hdwq].hba_eq,
9492 phba->cfg_fcp_imax);
9493 if (rc) {
9494 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9495 "0523 Failed setup of fast-path"
9496 " EQ (%d), rc = 0x%x\n",
9497 cpup->eq, (uint32_t)rc);
9498 goto out_destroy;
9499 }
9500
9501 /* Save the EQ for that vector in the hba_eq_hdl */
9502 phba->sli4_hba.hba_eq_hdl[cpup->eq].eq =
9503 qp[cpup->hdwq].hba_eq;
9504
9505 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9506 "2584 HBA EQ setup: queue[%d]-id=%d\n",
9507 cpup->eq,
9508 qp[cpup->hdwq].hba_eq->queue_id);
James Smartda0436e2009-05-22 14:51:39 -04009509 }
James Smartda0436e2009-05-22 14:51:39 -04009510 }
9511
James Smart657add42019-05-21 17:49:06 -07009512 /* Loop thru all Hardware Queues */
James Smartcdb42be2019-01-28 11:14:21 -08009513 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
9514 for (qidx = 0; qidx < phba->cfg_hdw_queue; qidx++) {
James Smart657add42019-05-21 17:49:06 -07009515 cpu = lpfc_find_cpu_handle(phba, qidx,
9516 LPFC_FIND_BY_HDWQ);
9517 cpup = &phba->sli4_hba.cpu_map[cpu];
9518
9519 /* Create the CQ/WQ corresponding to the
9520 * Hardware Queue
9521 */
James Smart895427b2017-02-12 13:52:30 -08009522 rc = lpfc_create_wq_cq(phba,
James Smart657add42019-05-21 17:49:06 -07009523 phba->sli4_hba.hdwq[cpup->hdwq].hba_eq,
James Smartcdb42be2019-01-28 11:14:21 -08009524 qp[qidx].nvme_cq,
9525 qp[qidx].nvme_wq,
9526 &phba->sli4_hba.hdwq[qidx].nvme_cq_map,
James Smart895427b2017-02-12 13:52:30 -08009527 qidx, LPFC_NVME);
9528 if (rc) {
9529 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9530 "6123 Failed to setup fastpath "
9531 "NVME WQ/CQ (%d), rc = 0x%x\n",
9532 qidx, (uint32_t)rc);
9533 goto out_destroy;
9534 }
9535 }
James Smart67d12732012-08-03 12:36:13 -04009536 }
9537
James Smartcdb42be2019-01-28 11:14:21 -08009538 for (qidx = 0; qidx < phba->cfg_hdw_queue; qidx++) {
James Smart657add42019-05-21 17:49:06 -07009539 cpu = lpfc_find_cpu_handle(phba, qidx, LPFC_FIND_BY_HDWQ);
9540 cpup = &phba->sli4_hba.cpu_map[cpu];
9541
9542 /* Create the CQ/WQ corresponding to the Hardware Queue */
James Smartcdb42be2019-01-28 11:14:21 -08009543 rc = lpfc_create_wq_cq(phba,
James Smart657add42019-05-21 17:49:06 -07009544 phba->sli4_hba.hdwq[cpup->hdwq].hba_eq,
James Smartcdb42be2019-01-28 11:14:21 -08009545 qp[qidx].fcp_cq,
9546 qp[qidx].fcp_wq,
9547 &phba->sli4_hba.hdwq[qidx].fcp_cq_map,
9548 qidx, LPFC_FCP);
9549 if (rc) {
James Smart67d12732012-08-03 12:36:13 -04009550 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart895427b2017-02-12 13:52:30 -08009551 "0535 Failed to setup fastpath "
9552 "FCP WQ/CQ (%d), rc = 0x%x\n",
9553 qidx, (uint32_t)rc);
James Smartcdb42be2019-01-28 11:14:21 -08009554 goto out_destroy;
James Smart895427b2017-02-12 13:52:30 -08009555 }
James Smart67d12732012-08-03 12:36:13 -04009556 }
James Smartda0436e2009-05-22 14:51:39 -04009557
9558 /*
James Smart895427b2017-02-12 13:52:30 -08009559 * Set up Slow Path Complete Queues (CQs)
James Smartda0436e2009-05-22 14:51:39 -04009560 */
9561
James Smart895427b2017-02-12 13:52:30 -08009562 /* Set up slow-path MBOX CQ/MQ */
9563
9564 if (!phba->sli4_hba.mbx_cq || !phba->sli4_hba.mbx_wq) {
James Smartda0436e2009-05-22 14:51:39 -04009565 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart895427b2017-02-12 13:52:30 -08009566 "0528 %s not allocated\n",
9567 phba->sli4_hba.mbx_cq ?
James Smartd1f525a2017-04-21 16:04:55 -07009568 "Mailbox WQ" : "Mailbox CQ");
James Smart1b511972011-12-13 13:23:09 -05009569 rc = -ENOMEM;
James Smart895427b2017-02-12 13:52:30 -08009570 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -04009571 }
James Smart895427b2017-02-12 13:52:30 -08009572
James Smartcdb42be2019-01-28 11:14:21 -08009573 rc = lpfc_create_wq_cq(phba, qp[0].hba_eq,
James Smartd1f525a2017-04-21 16:04:55 -07009574 phba->sli4_hba.mbx_cq,
9575 phba->sli4_hba.mbx_wq,
9576 NULL, 0, LPFC_MBOX);
James Smartda0436e2009-05-22 14:51:39 -04009577 if (rc) {
9578 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart895427b2017-02-12 13:52:30 -08009579 "0529 Failed setup of mailbox WQ/CQ: rc = 0x%x\n",
9580 (uint32_t)rc);
9581 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -04009582 }
James Smart2d7dbc42017-02-12 13:52:35 -08009583 if (phba->nvmet_support) {
9584 if (!phba->sli4_hba.nvmet_cqset) {
9585 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9586 "3165 Fast-path NVME CQ Set "
9587 "array not allocated\n");
9588 rc = -ENOMEM;
9589 goto out_destroy;
9590 }
9591 if (phba->cfg_nvmet_mrq > 1) {
9592 rc = lpfc_cq_create_set(phba,
9593 phba->sli4_hba.nvmet_cqset,
James Smartcdb42be2019-01-28 11:14:21 -08009594 qp,
James Smart2d7dbc42017-02-12 13:52:35 -08009595 LPFC_WCQ, LPFC_NVMET);
9596 if (rc) {
9597 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9598 "3164 Failed setup of NVME CQ "
9599 "Set, rc = 0x%x\n",
9600 (uint32_t)rc);
9601 goto out_destroy;
9602 }
9603 } else {
9604 /* Set up NVMET Receive Complete Queue */
9605 rc = lpfc_cq_create(phba, phba->sli4_hba.nvmet_cqset[0],
James Smartcdb42be2019-01-28 11:14:21 -08009606 qp[0].hba_eq,
James Smart2d7dbc42017-02-12 13:52:35 -08009607 LPFC_WCQ, LPFC_NVMET);
9608 if (rc) {
9609 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9610 "6089 Failed setup NVMET CQ: "
9611 "rc = 0x%x\n", (uint32_t)rc);
9612 goto out_destroy;
9613 }
James Smart81b96ed2017-11-20 16:00:29 -08009614 phba->sli4_hba.nvmet_cqset[0]->chann = 0;
9615
James Smart2d7dbc42017-02-12 13:52:35 -08009616 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9617 "6090 NVMET CQ setup: cq-id=%d, "
9618 "parent eq-id=%d\n",
9619 phba->sli4_hba.nvmet_cqset[0]->queue_id,
James Smartcdb42be2019-01-28 11:14:21 -08009620 qp[0].hba_eq->queue_id);
James Smart2d7dbc42017-02-12 13:52:35 -08009621 }
9622 }
James Smartda0436e2009-05-22 14:51:39 -04009623
James Smart895427b2017-02-12 13:52:30 -08009624 /* Set up slow-path ELS WQ/CQ */
9625 if (!phba->sli4_hba.els_cq || !phba->sli4_hba.els_wq) {
James Smartda0436e2009-05-22 14:51:39 -04009626 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart895427b2017-02-12 13:52:30 -08009627 "0530 ELS %s not allocated\n",
9628 phba->sli4_hba.els_cq ? "WQ" : "CQ");
James Smart1b511972011-12-13 13:23:09 -05009629 rc = -ENOMEM;
James Smart895427b2017-02-12 13:52:30 -08009630 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -04009631 }
James Smartcdb42be2019-01-28 11:14:21 -08009632 rc = lpfc_create_wq_cq(phba, qp[0].hba_eq,
9633 phba->sli4_hba.els_cq,
9634 phba->sli4_hba.els_wq,
9635 NULL, 0, LPFC_ELS);
James Smartda0436e2009-05-22 14:51:39 -04009636 if (rc) {
9637 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartcdb42be2019-01-28 11:14:21 -08009638 "0525 Failed setup of ELS WQ/CQ: rc = 0x%x\n",
9639 (uint32_t)rc);
James Smart895427b2017-02-12 13:52:30 -08009640 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -04009641 }
9642 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9643 "2590 ELS WQ setup: wq-id=%d, parent cq-id=%d\n",
9644 phba->sli4_hba.els_wq->queue_id,
9645 phba->sli4_hba.els_cq->queue_id);
9646
James Smartcdb42be2019-01-28 11:14:21 -08009647 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
James Smart895427b2017-02-12 13:52:30 -08009648 /* Set up NVME LS Complete Queue */
9649 if (!phba->sli4_hba.nvmels_cq || !phba->sli4_hba.nvmels_wq) {
9650 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9651 "6091 LS %s not allocated\n",
9652 phba->sli4_hba.nvmels_cq ? "WQ" : "CQ");
9653 rc = -ENOMEM;
9654 goto out_destroy;
9655 }
James Smartcdb42be2019-01-28 11:14:21 -08009656 rc = lpfc_create_wq_cq(phba, qp[0].hba_eq,
9657 phba->sli4_hba.nvmels_cq,
9658 phba->sli4_hba.nvmels_wq,
9659 NULL, 0, LPFC_NVME_LS);
James Smart895427b2017-02-12 13:52:30 -08009660 if (rc) {
9661 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartcdb42be2019-01-28 11:14:21 -08009662 "0526 Failed setup of NVVME LS WQ/CQ: "
9663 "rc = 0x%x\n", (uint32_t)rc);
James Smart895427b2017-02-12 13:52:30 -08009664 goto out_destroy;
9665 }
9666
9667 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9668 "6096 ELS WQ setup: wq-id=%d, "
9669 "parent cq-id=%d\n",
9670 phba->sli4_hba.nvmels_wq->queue_id,
9671 phba->sli4_hba.nvmels_cq->queue_id);
9672 }
9673
James Smart2d7dbc42017-02-12 13:52:35 -08009674 /*
9675 * Create NVMET Receive Queue (RQ)
9676 */
9677 if (phba->nvmet_support) {
9678 if ((!phba->sli4_hba.nvmet_cqset) ||
9679 (!phba->sli4_hba.nvmet_mrq_hdr) ||
9680 (!phba->sli4_hba.nvmet_mrq_data)) {
9681 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9682 "6130 MRQ CQ Queues not "
9683 "allocated\n");
9684 rc = -ENOMEM;
9685 goto out_destroy;
9686 }
9687 if (phba->cfg_nvmet_mrq > 1) {
9688 rc = lpfc_mrq_create(phba,
9689 phba->sli4_hba.nvmet_mrq_hdr,
9690 phba->sli4_hba.nvmet_mrq_data,
9691 phba->sli4_hba.nvmet_cqset,
9692 LPFC_NVMET);
9693 if (rc) {
9694 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9695 "6098 Failed setup of NVMET "
9696 "MRQ: rc = 0x%x\n",
9697 (uint32_t)rc);
9698 goto out_destroy;
9699 }
9700
9701 } else {
9702 rc = lpfc_rq_create(phba,
9703 phba->sli4_hba.nvmet_mrq_hdr[0],
9704 phba->sli4_hba.nvmet_mrq_data[0],
9705 phba->sli4_hba.nvmet_cqset[0],
9706 LPFC_NVMET);
9707 if (rc) {
9708 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9709 "6057 Failed setup of NVMET "
9710 "Receive Queue: rc = 0x%x\n",
9711 (uint32_t)rc);
9712 goto out_destroy;
9713 }
9714
9715 lpfc_printf_log(
9716 phba, KERN_INFO, LOG_INIT,
9717 "6099 NVMET RQ setup: hdr-rq-id=%d, "
9718 "dat-rq-id=%d parent cq-id=%d\n",
9719 phba->sli4_hba.nvmet_mrq_hdr[0]->queue_id,
9720 phba->sli4_hba.nvmet_mrq_data[0]->queue_id,
9721 phba->sli4_hba.nvmet_cqset[0]->queue_id);
9722
9723 }
9724 }
9725
James Smartda0436e2009-05-22 14:51:39 -04009726 if (!phba->sli4_hba.hdr_rq || !phba->sli4_hba.dat_rq) {
9727 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9728 "0540 Receive Queue not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05009729 rc = -ENOMEM;
James Smart895427b2017-02-12 13:52:30 -08009730 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -04009731 }
James Smart73d91e52011-10-10 21:32:10 -04009732
James Smartda0436e2009-05-22 14:51:39 -04009733 rc = lpfc_rq_create(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
James Smart4d9ab992009-10-02 15:16:39 -04009734 phba->sli4_hba.els_cq, LPFC_USOL);
James Smartda0436e2009-05-22 14:51:39 -04009735 if (rc) {
9736 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9737 "0541 Failed setup of Receive Queue: "
James Smarta2fc4aef2014-09-03 12:57:55 -04009738 "rc = 0x%x\n", (uint32_t)rc);
James Smart895427b2017-02-12 13:52:30 -08009739 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -04009740 }
James Smart73d91e52011-10-10 21:32:10 -04009741
James Smartda0436e2009-05-22 14:51:39 -04009742 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9743 "2592 USL RQ setup: hdr-rq-id=%d, dat-rq-id=%d "
9744 "parent cq-id=%d\n",
9745 phba->sli4_hba.hdr_rq->queue_id,
9746 phba->sli4_hba.dat_rq->queue_id,
James Smart4d9ab992009-10-02 15:16:39 -04009747 phba->sli4_hba.els_cq->queue_id);
James Smart1ba981f2014-02-20 09:56:45 -05009748
James Smartcb733e32019-01-28 11:14:32 -08009749 if (phba->cfg_fcp_imax)
9750 usdelay = LPFC_SEC_TO_USEC / phba->cfg_fcp_imax;
9751 else
9752 usdelay = 0;
9753
James Smart6a828b02019-01-28 11:14:31 -08009754 for (qidx = 0; qidx < phba->cfg_irq_chann;
James Smartcdb42be2019-01-28 11:14:21 -08009755 qidx += LPFC_MAX_EQ_DELAY_EQID_CNT)
James Smart0cf07f842017-06-01 21:07:10 -07009756 lpfc_modify_hba_eq_delay(phba, qidx, LPFC_MAX_EQ_DELAY_EQID_CNT,
James Smartcb733e32019-01-28 11:14:32 -08009757 usdelay);
James Smart43140ca2017-03-04 09:30:34 -08009758
James Smart6a828b02019-01-28 11:14:31 -08009759 if (phba->sli4_hba.cq_max) {
9760 kfree(phba->sli4_hba.cq_lookup);
9761 phba->sli4_hba.cq_lookup = kcalloc((phba->sli4_hba.cq_max + 1),
9762 sizeof(struct lpfc_queue *), GFP_KERNEL);
9763 if (!phba->sli4_hba.cq_lookup) {
James Smart1ba981f2014-02-20 09:56:45 -05009764 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart6a828b02019-01-28 11:14:31 -08009765 "0549 Failed setup of CQ Lookup table: "
9766 "size 0x%x\n", phba->sli4_hba.cq_max);
Dan Carpenterfad28e32019-02-11 21:43:00 +03009767 rc = -ENOMEM;
James Smart1ba981f2014-02-20 09:56:45 -05009768 goto out_destroy;
James Smart895427b2017-02-12 13:52:30 -08009769 }
James Smart6a828b02019-01-28 11:14:31 -08009770 lpfc_setup_cq_lookup(phba);
James Smart1ba981f2014-02-20 09:56:45 -05009771 }
James Smartda0436e2009-05-22 14:51:39 -04009772 return 0;
9773
James Smart895427b2017-02-12 13:52:30 -08009774out_destroy:
9775 lpfc_sli4_queue_unset(phba);
James Smartda0436e2009-05-22 14:51:39 -04009776out_error:
9777 return rc;
9778}
9779
9780/**
9781 * lpfc_sli4_queue_unset - Unset all the SLI4 queues
9782 * @phba: pointer to lpfc hba data structure.
9783 *
9784 * This routine is invoked to unset all the SLI4 queues with the FCoE HBA
9785 * operation.
9786 *
9787 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02009788 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009789 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04009790 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04009791 **/
9792void
9793lpfc_sli4_queue_unset(struct lpfc_hba *phba)
9794{
James Smartcdb42be2019-01-28 11:14:21 -08009795 struct lpfc_sli4_hdw_queue *qp;
James Smart657add42019-05-21 17:49:06 -07009796 struct lpfc_queue *eq;
James Smart895427b2017-02-12 13:52:30 -08009797 int qidx;
James Smartda0436e2009-05-22 14:51:39 -04009798
9799 /* Unset mailbox command work queue */
James Smart895427b2017-02-12 13:52:30 -08009800 if (phba->sli4_hba.mbx_wq)
9801 lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
9802
9803 /* Unset NVME LS work queue */
9804 if (phba->sli4_hba.nvmels_wq)
9805 lpfc_wq_destroy(phba, phba->sli4_hba.nvmels_wq);
9806
James Smartda0436e2009-05-22 14:51:39 -04009807 /* Unset ELS work queue */
Colin Ian King019c0d62017-05-06 23:13:55 +01009808 if (phba->sli4_hba.els_wq)
James Smart895427b2017-02-12 13:52:30 -08009809 lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
9810
James Smartda0436e2009-05-22 14:51:39 -04009811 /* Unset unsolicited receive queue */
James Smart895427b2017-02-12 13:52:30 -08009812 if (phba->sli4_hba.hdr_rq)
9813 lpfc_rq_destroy(phba, phba->sli4_hba.hdr_rq,
9814 phba->sli4_hba.dat_rq);
9815
James Smartda0436e2009-05-22 14:51:39 -04009816 /* Unset mailbox command complete queue */
James Smart895427b2017-02-12 13:52:30 -08009817 if (phba->sli4_hba.mbx_cq)
9818 lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
9819
James Smartda0436e2009-05-22 14:51:39 -04009820 /* Unset ELS complete queue */
James Smart895427b2017-02-12 13:52:30 -08009821 if (phba->sli4_hba.els_cq)
9822 lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
9823
9824 /* Unset NVME LS complete queue */
9825 if (phba->sli4_hba.nvmels_cq)
9826 lpfc_cq_destroy(phba, phba->sli4_hba.nvmels_cq);
9827
James Smartbcb24f62017-11-20 16:00:36 -08009828 if (phba->nvmet_support) {
9829 /* Unset NVMET MRQ queue */
9830 if (phba->sli4_hba.nvmet_mrq_hdr) {
9831 for (qidx = 0; qidx < phba->cfg_nvmet_mrq; qidx++)
9832 lpfc_rq_destroy(
9833 phba,
James Smart2d7dbc42017-02-12 13:52:35 -08009834 phba->sli4_hba.nvmet_mrq_hdr[qidx],
9835 phba->sli4_hba.nvmet_mrq_data[qidx]);
James Smartbcb24f62017-11-20 16:00:36 -08009836 }
James Smart2d7dbc42017-02-12 13:52:35 -08009837
James Smartbcb24f62017-11-20 16:00:36 -08009838 /* Unset NVMET CQ Set complete queue */
9839 if (phba->sli4_hba.nvmet_cqset) {
9840 for (qidx = 0; qidx < phba->cfg_nvmet_mrq; qidx++)
9841 lpfc_cq_destroy(
9842 phba, phba->sli4_hba.nvmet_cqset[qidx]);
9843 }
James Smart2d7dbc42017-02-12 13:52:35 -08009844 }
9845
James Smartcdb42be2019-01-28 11:14:21 -08009846 /* Unset fast-path SLI4 queues */
9847 if (phba->sli4_hba.hdwq) {
James Smart657add42019-05-21 17:49:06 -07009848 /* Loop thru all Hardware Queues */
James Smartcdb42be2019-01-28 11:14:21 -08009849 for (qidx = 0; qidx < phba->cfg_hdw_queue; qidx++) {
James Smart657add42019-05-21 17:49:06 -07009850 /* Destroy the CQ/WQ corresponding to Hardware Queue */
James Smartcdb42be2019-01-28 11:14:21 -08009851 qp = &phba->sli4_hba.hdwq[qidx];
9852 lpfc_wq_destroy(phba, qp->fcp_wq);
9853 lpfc_wq_destroy(phba, qp->nvme_wq);
9854 lpfc_cq_destroy(phba, qp->fcp_cq);
9855 lpfc_cq_destroy(phba, qp->nvme_cq);
James Smart657add42019-05-21 17:49:06 -07009856 }
9857 /* Loop thru all IRQ vectors */
9858 for (qidx = 0; qidx < phba->cfg_irq_chann; qidx++) {
9859 /* Destroy the EQ corresponding to the IRQ vector */
9860 eq = phba->sli4_hba.hba_eq_hdl[qidx].eq;
9861 lpfc_eq_destroy(phba, eq);
James Smartcdb42be2019-01-28 11:14:21 -08009862 }
9863 }
James Smart895427b2017-02-12 13:52:30 -08009864
James Smart6a828b02019-01-28 11:14:31 -08009865 kfree(phba->sli4_hba.cq_lookup);
9866 phba->sli4_hba.cq_lookup = NULL;
9867 phba->sli4_hba.cq_max = 0;
James Smartda0436e2009-05-22 14:51:39 -04009868}
9869
9870/**
9871 * lpfc_sli4_cq_event_pool_create - Create completion-queue event free pool
9872 * @phba: pointer to lpfc hba data structure.
9873 *
9874 * This routine is invoked to allocate and set up a pool of completion queue
9875 * events. The body of the completion queue event is a completion queue entry
9876 * CQE. For now, this pool is used for the interrupt service routine to queue
9877 * the following HBA completion queue events for the worker thread to process:
9878 * - Mailbox asynchronous events
9879 * - Receive queue completion unsolicited events
9880 * Later, this can be used for all the slow-path events.
9881 *
9882 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02009883 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009884 * -ENOMEM - No available memory
James Smartda0436e2009-05-22 14:51:39 -04009885 **/
9886static int
9887lpfc_sli4_cq_event_pool_create(struct lpfc_hba *phba)
9888{
9889 struct lpfc_cq_event *cq_event;
9890 int i;
9891
9892 for (i = 0; i < (4 * phba->sli4_hba.cq_ecount); i++) {
9893 cq_event = kmalloc(sizeof(struct lpfc_cq_event), GFP_KERNEL);
9894 if (!cq_event)
9895 goto out_pool_create_fail;
9896 list_add_tail(&cq_event->list,
9897 &phba->sli4_hba.sp_cqe_event_pool);
9898 }
9899 return 0;
9900
9901out_pool_create_fail:
9902 lpfc_sli4_cq_event_pool_destroy(phba);
9903 return -ENOMEM;
9904}
9905
9906/**
9907 * lpfc_sli4_cq_event_pool_destroy - Free completion-queue event free pool
9908 * @phba: pointer to lpfc hba data structure.
9909 *
9910 * This routine is invoked to free the pool of completion queue events at
9911 * driver unload time. Note that, it is the responsibility of the driver
9912 * cleanup routine to free all the outstanding completion-queue events
9913 * allocated from this pool back into the pool before invoking this routine
9914 * to destroy the pool.
9915 **/
9916static void
9917lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *phba)
9918{
9919 struct lpfc_cq_event *cq_event, *next_cq_event;
9920
9921 list_for_each_entry_safe(cq_event, next_cq_event,
9922 &phba->sli4_hba.sp_cqe_event_pool, list) {
9923 list_del(&cq_event->list);
9924 kfree(cq_event);
9925 }
9926}
9927
9928/**
9929 * __lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
9930 * @phba: pointer to lpfc hba data structure.
9931 *
9932 * This routine is the lock free version of the API invoked to allocate a
9933 * completion-queue event from the free pool.
9934 *
9935 * Return: Pointer to the newly allocated completion-queue event if successful
9936 * NULL otherwise.
9937 **/
9938struct lpfc_cq_event *
9939__lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
9940{
9941 struct lpfc_cq_event *cq_event = NULL;
9942
9943 list_remove_head(&phba->sli4_hba.sp_cqe_event_pool, cq_event,
9944 struct lpfc_cq_event, list);
9945 return cq_event;
9946}
9947
9948/**
9949 * lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
9950 * @phba: pointer to lpfc hba data structure.
9951 *
9952 * This routine is the lock version of the API invoked to allocate a
9953 * completion-queue event from the free pool.
9954 *
9955 * Return: Pointer to the newly allocated completion-queue event if successful
9956 * NULL otherwise.
9957 **/
9958struct lpfc_cq_event *
9959lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
9960{
9961 struct lpfc_cq_event *cq_event;
9962 unsigned long iflags;
9963
9964 spin_lock_irqsave(&phba->hbalock, iflags);
9965 cq_event = __lpfc_sli4_cq_event_alloc(phba);
9966 spin_unlock_irqrestore(&phba->hbalock, iflags);
9967 return cq_event;
9968}
9969
9970/**
9971 * __lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
9972 * @phba: pointer to lpfc hba data structure.
9973 * @cq_event: pointer to the completion queue event to be freed.
9974 *
9975 * This routine is the lock free version of the API invoked to release a
9976 * completion-queue event back into the free pool.
9977 **/
9978void
9979__lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
9980 struct lpfc_cq_event *cq_event)
9981{
9982 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_cqe_event_pool);
9983}
9984
9985/**
9986 * lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
9987 * @phba: pointer to lpfc hba data structure.
9988 * @cq_event: pointer to the completion queue event to be freed.
9989 *
9990 * This routine is the lock version of the API invoked to release a
9991 * completion-queue event back into the free pool.
9992 **/
9993void
9994lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
9995 struct lpfc_cq_event *cq_event)
9996{
9997 unsigned long iflags;
9998 spin_lock_irqsave(&phba->hbalock, iflags);
9999 __lpfc_sli4_cq_event_release(phba, cq_event);
10000 spin_unlock_irqrestore(&phba->hbalock, iflags);
10001}
10002
10003/**
10004 * lpfc_sli4_cq_event_release_all - Release all cq events to the free pool
10005 * @phba: pointer to lpfc hba data structure.
10006 *
10007 * This routine is to free all the pending completion-queue events to the
10008 * back into the free pool for device reset.
10009 **/
10010static void
10011lpfc_sli4_cq_event_release_all(struct lpfc_hba *phba)
10012{
10013 LIST_HEAD(cqelist);
10014 struct lpfc_cq_event *cqe;
10015 unsigned long iflags;
10016
10017 /* Retrieve all the pending WCQEs from pending WCQE lists */
10018 spin_lock_irqsave(&phba->hbalock, iflags);
10019 /* Pending FCP XRI abort events */
10020 list_splice_init(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
10021 &cqelist);
10022 /* Pending ELS XRI abort events */
10023 list_splice_init(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
10024 &cqelist);
10025 /* Pending asynnc events */
10026 list_splice_init(&phba->sli4_hba.sp_asynce_work_queue,
10027 &cqelist);
10028 spin_unlock_irqrestore(&phba->hbalock, iflags);
10029
10030 while (!list_empty(&cqelist)) {
10031 list_remove_head(&cqelist, cqe, struct lpfc_cq_event, list);
10032 lpfc_sli4_cq_event_release(phba, cqe);
10033 }
10034}
10035
10036/**
10037 * lpfc_pci_function_reset - Reset pci function.
10038 * @phba: pointer to lpfc hba data structure.
10039 *
10040 * This routine is invoked to request a PCI function reset. It will destroys
10041 * all resources assigned to the PCI function which originates this request.
10042 *
10043 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020010044 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010045 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -040010046 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -040010047 **/
10048int
10049lpfc_pci_function_reset(struct lpfc_hba *phba)
10050{
10051 LPFC_MBOXQ_t *mboxq;
James Smart2fcee4b2010-12-15 17:57:46 -050010052 uint32_t rc = 0, if_type;
James Smartda0436e2009-05-22 14:51:39 -040010053 uint32_t shdr_status, shdr_add_status;
James Smart2f6fa2c2014-09-03 12:57:08 -040010054 uint32_t rdy_chk;
10055 uint32_t port_reset = 0;
James Smartda0436e2009-05-22 14:51:39 -040010056 union lpfc_sli4_cfg_shdr *shdr;
James Smart2fcee4b2010-12-15 17:57:46 -050010057 struct lpfc_register reg_data;
James Smart2b81f942012-03-01 22:37:18 -050010058 uint16_t devid;
James Smartda0436e2009-05-22 14:51:39 -040010059
James Smart2fcee4b2010-12-15 17:57:46 -050010060 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
10061 switch (if_type) {
10062 case LPFC_SLI_INTF_IF_TYPE_0:
10063 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
10064 GFP_KERNEL);
10065 if (!mboxq) {
10066 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10067 "0494 Unable to allocate memory for "
10068 "issuing SLI_FUNCTION_RESET mailbox "
10069 "command\n");
10070 return -ENOMEM;
10071 }
10072
10073 /* Setup PCI function reset mailbox-ioctl command */
10074 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
10075 LPFC_MBOX_OPCODE_FUNCTION_RESET, 0,
10076 LPFC_SLI4_MBX_EMBED);
10077 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
10078 shdr = (union lpfc_sli4_cfg_shdr *)
10079 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
10080 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10081 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
10082 &shdr->response);
10083 if (rc != MBX_TIMEOUT)
10084 mempool_free(mboxq, phba->mbox_mem_pool);
10085 if (shdr_status || shdr_add_status || rc) {
10086 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10087 "0495 SLI_FUNCTION_RESET mailbox "
10088 "failed with status x%x add_status x%x,"
10089 " mbx status x%x\n",
10090 shdr_status, shdr_add_status, rc);
10091 rc = -ENXIO;
10092 }
10093 break;
10094 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart27d6ac02018-02-22 08:18:42 -080010095 case LPFC_SLI_INTF_IF_TYPE_6:
James Smart2f6fa2c2014-09-03 12:57:08 -040010096wait:
10097 /*
10098 * Poll the Port Status Register and wait for RDY for
10099 * up to 30 seconds. If the port doesn't respond, treat
10100 * it as an error.
10101 */
James Smart77d093f2015-04-07 15:07:08 -040010102 for (rdy_chk = 0; rdy_chk < 1500; rdy_chk++) {
James Smart2f6fa2c2014-09-03 12:57:08 -040010103 if (lpfc_readl(phba->sli4_hba.u.if_type2.
10104 STATUSregaddr, &reg_data.word0)) {
10105 rc = -ENODEV;
10106 goto out;
10107 }
10108 if (bf_get(lpfc_sliport_status_rdy, &reg_data))
10109 break;
10110 msleep(20);
10111 }
10112
10113 if (!bf_get(lpfc_sliport_status_rdy, &reg_data)) {
10114 phba->work_status[0] = readl(
10115 phba->sli4_hba.u.if_type2.ERR1regaddr);
10116 phba->work_status[1] = readl(
10117 phba->sli4_hba.u.if_type2.ERR2regaddr);
10118 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10119 "2890 Port not ready, port status reg "
10120 "0x%x error 1=0x%x, error 2=0x%x\n",
10121 reg_data.word0,
10122 phba->work_status[0],
10123 phba->work_status[1]);
10124 rc = -ENODEV;
10125 goto out;
10126 }
10127
10128 if (!port_reset) {
10129 /*
10130 * Reset the port now
10131 */
James Smart2fcee4b2010-12-15 17:57:46 -050010132 reg_data.word0 = 0;
10133 bf_set(lpfc_sliport_ctrl_end, &reg_data,
10134 LPFC_SLIPORT_LITTLE_ENDIAN);
10135 bf_set(lpfc_sliport_ctrl_ip, &reg_data,
10136 LPFC_SLIPORT_INIT_PORT);
10137 writel(reg_data.word0, phba->sli4_hba.u.if_type2.
10138 CTRLregaddr);
James Smart8fcb8ac2012-03-01 22:35:58 -050010139 /* flush */
James Smart2b81f942012-03-01 22:37:18 -050010140 pci_read_config_word(phba->pcidev,
10141 PCI_DEVICE_ID, &devid);
James Smart2fcee4b2010-12-15 17:57:46 -050010142
James Smart2f6fa2c2014-09-03 12:57:08 -040010143 port_reset = 1;
10144 msleep(20);
10145 goto wait;
10146 } else if (bf_get(lpfc_sliport_status_rn, &reg_data)) {
10147 rc = -ENODEV;
10148 goto out;
James Smart2fcee4b2010-12-15 17:57:46 -050010149 }
10150 break;
James Smart2f6fa2c2014-09-03 12:57:08 -040010151
James Smart2fcee4b2010-12-15 17:57:46 -050010152 case LPFC_SLI_INTF_IF_TYPE_1:
10153 default:
10154 break;
James Smartda0436e2009-05-22 14:51:39 -040010155 }
10156
James Smart73d91e52011-10-10 21:32:10 -040010157out:
James Smart2fcee4b2010-12-15 17:57:46 -050010158 /* Catch the not-ready port failure after a port reset. */
James Smart2f6fa2c2014-09-03 12:57:08 -040010159 if (rc) {
James Smart229adb02013-04-17 20:16:51 -040010160 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10161 "3317 HBA not functional: IP Reset Failed "
James Smart2f6fa2c2014-09-03 12:57:08 -040010162 "try: echo fw_reset > board_mode\n");
James Smart2fcee4b2010-12-15 17:57:46 -050010163 rc = -ENODEV;
James Smart229adb02013-04-17 20:16:51 -040010164 }
James Smart2fcee4b2010-12-15 17:57:46 -050010165
James Smartda0436e2009-05-22 14:51:39 -040010166 return rc;
10167}
10168
10169/**
James Smartda0436e2009-05-22 14:51:39 -040010170 * lpfc_sli4_pci_mem_setup - Setup SLI4 HBA PCI memory space.
10171 * @phba: pointer to lpfc hba data structure.
10172 *
10173 * This routine is invoked to set up the PCI device memory space for device
10174 * with SLI-4 interface spec.
10175 *
10176 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020010177 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -040010178 * other values - error
10179 **/
10180static int
10181lpfc_sli4_pci_mem_setup(struct lpfc_hba *phba)
10182{
Christoph Hellwigf30e1bf2018-10-18 15:10:21 +020010183 struct pci_dev *pdev = phba->pcidev;
James Smartda0436e2009-05-22 14:51:39 -040010184 unsigned long bar0map_len, bar1map_len, bar2map_len;
Dan Carpenter3a487ff2019-03-07 08:33:44 +030010185 int error;
James Smart2fcee4b2010-12-15 17:57:46 -050010186 uint32_t if_type;
James Smartda0436e2009-05-22 14:51:39 -040010187
Christoph Hellwigf30e1bf2018-10-18 15:10:21 +020010188 if (!pdev)
Hannes Reinecke56de8352019-02-18 08:34:19 +010010189 return -ENODEV;
James Smartda0436e2009-05-22 14:51:39 -040010190
10191 /* Set the device DMA mask size */
Hannes Reinecke56de8352019-02-18 08:34:19 +010010192 error = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
10193 if (error)
10194 error = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
10195 if (error)
Christoph Hellwigf30e1bf2018-10-18 15:10:21 +020010196 return error;
James Smartda0436e2009-05-22 14:51:39 -040010197
James Smart2fcee4b2010-12-15 17:57:46 -050010198 /*
10199 * The BARs and register set definitions and offset locations are
10200 * dependent on the if_type.
10201 */
10202 if (pci_read_config_dword(pdev, LPFC_SLI_INTF,
10203 &phba->sli4_hba.sli_intf.word0)) {
Dan Carpenter3a487ff2019-03-07 08:33:44 +030010204 return -ENODEV;
James Smart2fcee4b2010-12-15 17:57:46 -050010205 }
10206
10207 /* There is no SLI3 failback for SLI4 devices. */
10208 if (bf_get(lpfc_sli_intf_valid, &phba->sli4_hba.sli_intf) !=
10209 LPFC_SLI_INTF_VALID) {
10210 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10211 "2894 SLI_INTF reg contents invalid "
10212 "sli_intf reg 0x%x\n",
10213 phba->sli4_hba.sli_intf.word0);
Dan Carpenter3a487ff2019-03-07 08:33:44 +030010214 return -ENODEV;
James Smart2fcee4b2010-12-15 17:57:46 -050010215 }
10216
10217 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
10218 /*
10219 * Get the bus address of SLI4 device Bar regions and the
10220 * number of bytes required by each mapping. The mapping of the
10221 * particular PCI BARs regions is dependent on the type of
10222 * SLI4 device.
James Smartda0436e2009-05-22 14:51:39 -040010223 */
James Smartf5ca6f22013-09-06 12:21:09 -040010224 if (pci_resource_start(pdev, PCI_64BIT_BAR0)) {
10225 phba->pci_bar0_map = pci_resource_start(pdev, PCI_64BIT_BAR0);
10226 bar0map_len = pci_resource_len(pdev, PCI_64BIT_BAR0);
James Smart2fcee4b2010-12-15 17:57:46 -050010227
10228 /*
10229 * Map SLI4 PCI Config Space Register base to a kernel virtual
10230 * addr
10231 */
10232 phba->sli4_hba.conf_regs_memmap_p =
10233 ioremap(phba->pci_bar0_map, bar0map_len);
10234 if (!phba->sli4_hba.conf_regs_memmap_p) {
10235 dev_printk(KERN_ERR, &pdev->dev,
10236 "ioremap failed for SLI4 PCI config "
10237 "registers.\n");
Dan Carpenter3a487ff2019-03-07 08:33:44 +030010238 return -ENODEV;
James Smart2fcee4b2010-12-15 17:57:46 -050010239 }
James Smartf5ca6f22013-09-06 12:21:09 -040010240 phba->pci_bar0_memmap_p = phba->sli4_hba.conf_regs_memmap_p;
James Smart2fcee4b2010-12-15 17:57:46 -050010241 /* Set up BAR0 PCI config space register memory map */
10242 lpfc_sli4_bar0_register_memmap(phba, if_type);
James Smart1dfb5a42010-02-12 14:40:50 -050010243 } else {
10244 phba->pci_bar0_map = pci_resource_start(pdev, 1);
10245 bar0map_len = pci_resource_len(pdev, 1);
James Smart27d6ac02018-02-22 08:18:42 -080010246 if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
James Smart2fcee4b2010-12-15 17:57:46 -050010247 dev_printk(KERN_ERR, &pdev->dev,
10248 "FATAL - No BAR0 mapping for SLI4, if_type 2\n");
Dan Carpenter3a487ff2019-03-07 08:33:44 +030010249 return -ENODEV;
James Smart2fcee4b2010-12-15 17:57:46 -050010250 }
10251 phba->sli4_hba.conf_regs_memmap_p =
James Smartda0436e2009-05-22 14:51:39 -040010252 ioremap(phba->pci_bar0_map, bar0map_len);
James Smart2fcee4b2010-12-15 17:57:46 -050010253 if (!phba->sli4_hba.conf_regs_memmap_p) {
10254 dev_printk(KERN_ERR, &pdev->dev,
10255 "ioremap failed for SLI4 PCI config "
10256 "registers.\n");
Dan Carpenter3a487ff2019-03-07 08:33:44 +030010257 return -ENODEV;
James Smart2fcee4b2010-12-15 17:57:46 -050010258 }
10259 lpfc_sli4_bar0_register_memmap(phba, if_type);
James Smartda0436e2009-05-22 14:51:39 -040010260 }
10261
James Smarte4b97942017-11-20 16:00:31 -080010262 if (if_type == LPFC_SLI_INTF_IF_TYPE_0) {
10263 if (pci_resource_start(pdev, PCI_64BIT_BAR2)) {
10264 /*
10265 * Map SLI4 if type 0 HBA Control Register base to a
10266 * kernel virtual address and setup the registers.
10267 */
10268 phba->pci_bar1_map = pci_resource_start(pdev,
10269 PCI_64BIT_BAR2);
10270 bar1map_len = pci_resource_len(pdev, PCI_64BIT_BAR2);
10271 phba->sli4_hba.ctrl_regs_memmap_p =
10272 ioremap(phba->pci_bar1_map,
10273 bar1map_len);
10274 if (!phba->sli4_hba.ctrl_regs_memmap_p) {
10275 dev_err(&pdev->dev,
10276 "ioremap failed for SLI4 HBA "
10277 "control registers.\n");
10278 error = -ENOMEM;
10279 goto out_iounmap_conf;
10280 }
10281 phba->pci_bar2_memmap_p =
10282 phba->sli4_hba.ctrl_regs_memmap_p;
James Smart27d6ac02018-02-22 08:18:42 -080010283 lpfc_sli4_bar1_register_memmap(phba, if_type);
James Smarte4b97942017-11-20 16:00:31 -080010284 } else {
10285 error = -ENOMEM;
James Smart2fcee4b2010-12-15 17:57:46 -050010286 goto out_iounmap_conf;
10287 }
James Smartda0436e2009-05-22 14:51:39 -040010288 }
10289
James Smart27d6ac02018-02-22 08:18:42 -080010290 if ((if_type == LPFC_SLI_INTF_IF_TYPE_6) &&
10291 (pci_resource_start(pdev, PCI_64BIT_BAR2))) {
10292 /*
10293 * Map SLI4 if type 6 HBA Doorbell Register base to a kernel
10294 * virtual address and setup the registers.
10295 */
10296 phba->pci_bar1_map = pci_resource_start(pdev, PCI_64BIT_BAR2);
10297 bar1map_len = pci_resource_len(pdev, PCI_64BIT_BAR2);
10298 phba->sli4_hba.drbl_regs_memmap_p =
10299 ioremap(phba->pci_bar1_map, bar1map_len);
10300 if (!phba->sli4_hba.drbl_regs_memmap_p) {
10301 dev_err(&pdev->dev,
10302 "ioremap failed for SLI4 HBA doorbell registers.\n");
Dan Carpenter3a487ff2019-03-07 08:33:44 +030010303 error = -ENOMEM;
James Smart27d6ac02018-02-22 08:18:42 -080010304 goto out_iounmap_conf;
10305 }
10306 phba->pci_bar2_memmap_p = phba->sli4_hba.drbl_regs_memmap_p;
10307 lpfc_sli4_bar1_register_memmap(phba, if_type);
10308 }
10309
James Smarte4b97942017-11-20 16:00:31 -080010310 if (if_type == LPFC_SLI_INTF_IF_TYPE_0) {
10311 if (pci_resource_start(pdev, PCI_64BIT_BAR4)) {
10312 /*
10313 * Map SLI4 if type 0 HBA Doorbell Register base to
10314 * a kernel virtual address and setup the registers.
10315 */
10316 phba->pci_bar2_map = pci_resource_start(pdev,
10317 PCI_64BIT_BAR4);
10318 bar2map_len = pci_resource_len(pdev, PCI_64BIT_BAR4);
10319 phba->sli4_hba.drbl_regs_memmap_p =
10320 ioremap(phba->pci_bar2_map,
10321 bar2map_len);
10322 if (!phba->sli4_hba.drbl_regs_memmap_p) {
10323 dev_err(&pdev->dev,
10324 "ioremap failed for SLI4 HBA"
10325 " doorbell registers.\n");
10326 error = -ENOMEM;
10327 goto out_iounmap_ctrl;
10328 }
10329 phba->pci_bar4_memmap_p =
10330 phba->sli4_hba.drbl_regs_memmap_p;
10331 error = lpfc_sli4_bar2_register_memmap(phba, LPFC_VF0);
10332 if (error)
10333 goto out_iounmap_all;
10334 } else {
10335 error = -ENOMEM;
James Smart2fcee4b2010-12-15 17:57:46 -050010336 goto out_iounmap_all;
James Smarte4b97942017-11-20 16:00:31 -080010337 }
James Smartda0436e2009-05-22 14:51:39 -040010338 }
10339
James Smart1351e692018-02-22 08:18:43 -080010340 if (if_type == LPFC_SLI_INTF_IF_TYPE_6 &&
10341 pci_resource_start(pdev, PCI_64BIT_BAR4)) {
10342 /*
10343 * Map SLI4 if type 6 HBA DPP Register base to a kernel
10344 * virtual address and setup the registers.
10345 */
10346 phba->pci_bar2_map = pci_resource_start(pdev, PCI_64BIT_BAR4);
10347 bar2map_len = pci_resource_len(pdev, PCI_64BIT_BAR4);
10348 phba->sli4_hba.dpp_regs_memmap_p =
10349 ioremap(phba->pci_bar2_map, bar2map_len);
10350 if (!phba->sli4_hba.dpp_regs_memmap_p) {
10351 dev_err(&pdev->dev,
10352 "ioremap failed for SLI4 HBA dpp registers.\n");
Dan Carpenter3a487ff2019-03-07 08:33:44 +030010353 error = -ENOMEM;
James Smart1351e692018-02-22 08:18:43 -080010354 goto out_iounmap_ctrl;
10355 }
10356 phba->pci_bar4_memmap_p = phba->sli4_hba.dpp_regs_memmap_p;
10357 }
10358
James Smartb71413d2018-02-22 08:18:40 -080010359 /* Set up the EQ/CQ register handeling functions now */
James Smart27d6ac02018-02-22 08:18:42 -080010360 switch (if_type) {
10361 case LPFC_SLI_INTF_IF_TYPE_0:
10362 case LPFC_SLI_INTF_IF_TYPE_2:
James Smartb71413d2018-02-22 08:18:40 -080010363 phba->sli4_hba.sli4_eq_clr_intr = lpfc_sli4_eq_clr_intr;
James Smart32517fc2019-01-28 11:14:33 -080010364 phba->sli4_hba.sli4_write_eq_db = lpfc_sli4_write_eq_db;
10365 phba->sli4_hba.sli4_write_cq_db = lpfc_sli4_write_cq_db;
James Smart27d6ac02018-02-22 08:18:42 -080010366 break;
10367 case LPFC_SLI_INTF_IF_TYPE_6:
10368 phba->sli4_hba.sli4_eq_clr_intr = lpfc_sli4_if6_eq_clr_intr;
James Smart32517fc2019-01-28 11:14:33 -080010369 phba->sli4_hba.sli4_write_eq_db = lpfc_sli4_if6_write_eq_db;
10370 phba->sli4_hba.sli4_write_cq_db = lpfc_sli4_if6_write_cq_db;
James Smart27d6ac02018-02-22 08:18:42 -080010371 break;
10372 default:
10373 break;
James Smartb71413d2018-02-22 08:18:40 -080010374 }
10375
James Smartda0436e2009-05-22 14:51:39 -040010376 return 0;
10377
10378out_iounmap_all:
10379 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
10380out_iounmap_ctrl:
10381 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
10382out_iounmap_conf:
10383 iounmap(phba->sli4_hba.conf_regs_memmap_p);
Dan Carpenter3a487ff2019-03-07 08:33:44 +030010384
James Smartda0436e2009-05-22 14:51:39 -040010385 return error;
10386}
10387
10388/**
10389 * lpfc_sli4_pci_mem_unset - Unset SLI4 HBA PCI memory space.
10390 * @phba: pointer to lpfc hba data structure.
10391 *
10392 * This routine is invoked to unset the PCI device memory space for device
10393 * with SLI-4 interface spec.
10394 **/
10395static void
10396lpfc_sli4_pci_mem_unset(struct lpfc_hba *phba)
10397{
James Smart2e90f4b2011-12-13 13:22:37 -050010398 uint32_t if_type;
10399 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
James Smartda0436e2009-05-22 14:51:39 -040010400
James Smart2e90f4b2011-12-13 13:22:37 -050010401 switch (if_type) {
10402 case LPFC_SLI_INTF_IF_TYPE_0:
10403 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
10404 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
10405 iounmap(phba->sli4_hba.conf_regs_memmap_p);
10406 break;
10407 case LPFC_SLI_INTF_IF_TYPE_2:
10408 iounmap(phba->sli4_hba.conf_regs_memmap_p);
10409 break;
James Smart27d6ac02018-02-22 08:18:42 -080010410 case LPFC_SLI_INTF_IF_TYPE_6:
10411 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
10412 iounmap(phba->sli4_hba.conf_regs_memmap_p);
10413 break;
James Smart2e90f4b2011-12-13 13:22:37 -050010414 case LPFC_SLI_INTF_IF_TYPE_1:
10415 default:
10416 dev_printk(KERN_ERR, &phba->pcidev->dev,
10417 "FATAL - unsupported SLI4 interface type - %d\n",
10418 if_type);
10419 break;
10420 }
James Smartda0436e2009-05-22 14:51:39 -040010421}
10422
10423/**
James Smart3772a992009-05-22 14:50:54 -040010424 * lpfc_sli_enable_msix - Enable MSI-X interrupt mode on SLI-3 device
10425 * @phba: pointer to lpfc hba data structure.
10426 *
10427 * This routine is invoked to enable the MSI-X interrupt vectors to device
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010428 * with SLI-3 interface specs.
James Smart3772a992009-05-22 14:50:54 -040010429 *
10430 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020010431 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -040010432 * other values - error
10433 **/
10434static int
10435lpfc_sli_enable_msix(struct lpfc_hba *phba)
10436{
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010437 int rc;
James Smart3772a992009-05-22 14:50:54 -040010438 LPFC_MBOXQ_t *pmb;
10439
10440 /* Set up MSI-X multi-message vectors */
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010441 rc = pci_alloc_irq_vectors(phba->pcidev,
10442 LPFC_MSIX_VECTORS, LPFC_MSIX_VECTORS, PCI_IRQ_MSIX);
10443 if (rc < 0) {
James Smart3772a992009-05-22 14:50:54 -040010444 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10445 "0420 PCI enable MSI-X failed (%d)\n", rc);
Alexander Gordeev029165a2014-07-16 20:05:15 +020010446 goto vec_fail_out;
James Smart3772a992009-05-22 14:50:54 -040010447 }
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010448
James Smart3772a992009-05-22 14:50:54 -040010449 /*
10450 * Assign MSI-X vectors to interrupt handlers
10451 */
10452
10453 /* vector-0 is associated to slow-path handler */
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010454 rc = request_irq(pci_irq_vector(phba->pcidev, 0),
James Smarted243d32015-05-21 13:55:25 -040010455 &lpfc_sli_sp_intr_handler, 0,
James Smart3772a992009-05-22 14:50:54 -040010456 LPFC_SP_DRIVER_HANDLER_NAME, phba);
10457 if (rc) {
10458 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
10459 "0421 MSI-X slow-path request_irq failed "
10460 "(%d)\n", rc);
10461 goto msi_fail_out;
10462 }
10463
10464 /* vector-1 is associated to fast-path handler */
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010465 rc = request_irq(pci_irq_vector(phba->pcidev, 1),
James Smarted243d32015-05-21 13:55:25 -040010466 &lpfc_sli_fp_intr_handler, 0,
James Smart3772a992009-05-22 14:50:54 -040010467 LPFC_FP_DRIVER_HANDLER_NAME, phba);
10468
10469 if (rc) {
10470 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
10471 "0429 MSI-X fast-path request_irq failed "
10472 "(%d)\n", rc);
10473 goto irq_fail_out;
10474 }
10475
10476 /*
10477 * Configure HBA MSI-X attention conditions to messages
10478 */
10479 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10480
10481 if (!pmb) {
10482 rc = -ENOMEM;
10483 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10484 "0474 Unable to allocate memory for issuing "
10485 "MBOX_CONFIG_MSI command\n");
10486 goto mem_fail_out;
10487 }
10488 rc = lpfc_config_msi(phba, pmb);
10489 if (rc)
10490 goto mbx_fail_out;
10491 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
10492 if (rc != MBX_SUCCESS) {
10493 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
10494 "0351 Config MSI mailbox command failed, "
10495 "mbxCmd x%x, mbxStatus x%x\n",
10496 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus);
10497 goto mbx_fail_out;
10498 }
10499
10500 /* Free memory allocated for mailbox command */
10501 mempool_free(pmb, phba->mbox_mem_pool);
10502 return rc;
10503
10504mbx_fail_out:
10505 /* Free memory allocated for mailbox command */
10506 mempool_free(pmb, phba->mbox_mem_pool);
10507
10508mem_fail_out:
10509 /* free the irq already requested */
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010510 free_irq(pci_irq_vector(phba->pcidev, 1), phba);
James Smart3772a992009-05-22 14:50:54 -040010511
10512irq_fail_out:
10513 /* free the irq already requested */
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010514 free_irq(pci_irq_vector(phba->pcidev, 0), phba);
James Smart3772a992009-05-22 14:50:54 -040010515
10516msi_fail_out:
10517 /* Unconfigure MSI-X capability structure */
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010518 pci_free_irq_vectors(phba->pcidev);
Alexander Gordeev029165a2014-07-16 20:05:15 +020010519
10520vec_fail_out:
James Smart3772a992009-05-22 14:50:54 -040010521 return rc;
10522}
10523
10524/**
James Smart3772a992009-05-22 14:50:54 -040010525 * lpfc_sli_enable_msi - Enable MSI interrupt mode on SLI-3 device.
10526 * @phba: pointer to lpfc hba data structure.
10527 *
10528 * This routine is invoked to enable the MSI interrupt mode to device with
10529 * SLI-3 interface spec. The kernel function pci_enable_msi() is called to
10530 * enable the MSI vector. The device driver is responsible for calling the
10531 * request_irq() to register MSI vector with a interrupt the handler, which
10532 * is done in this function.
10533 *
10534 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020010535 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -040010536 * other values - error
10537 */
10538static int
10539lpfc_sli_enable_msi(struct lpfc_hba *phba)
10540{
10541 int rc;
10542
10543 rc = pci_enable_msi(phba->pcidev);
10544 if (!rc)
10545 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10546 "0462 PCI enable MSI mode success.\n");
10547 else {
10548 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10549 "0471 PCI enable MSI mode failed (%d)\n", rc);
10550 return rc;
10551 }
10552
10553 rc = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
James Smarted243d32015-05-21 13:55:25 -040010554 0, LPFC_DRIVER_NAME, phba);
James Smart3772a992009-05-22 14:50:54 -040010555 if (rc) {
10556 pci_disable_msi(phba->pcidev);
10557 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
10558 "0478 MSI request_irq failed (%d)\n", rc);
10559 }
10560 return rc;
10561}
10562
10563/**
James Smart3772a992009-05-22 14:50:54 -040010564 * lpfc_sli_enable_intr - Enable device interrupt to SLI-3 device.
10565 * @phba: pointer to lpfc hba data structure.
10566 *
10567 * This routine is invoked to enable device interrupt and associate driver's
10568 * interrupt handler(s) to interrupt vector(s) to device with SLI-3 interface
10569 * spec. Depends on the interrupt mode configured to the driver, the driver
10570 * will try to fallback from the configured interrupt mode to an interrupt
10571 * mode which is supported by the platform, kernel, and device in the order
10572 * of:
10573 * MSI-X -> MSI -> IRQ.
10574 *
10575 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020010576 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -040010577 * other values - error
10578 **/
10579static uint32_t
10580lpfc_sli_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
10581{
10582 uint32_t intr_mode = LPFC_INTR_ERROR;
10583 int retval;
10584
10585 if (cfg_mode == 2) {
10586 /* Need to issue conf_port mbox cmd before conf_msi mbox cmd */
10587 retval = lpfc_sli_config_port(phba, LPFC_SLI_REV3);
10588 if (!retval) {
10589 /* Now, try to enable MSI-X interrupt mode */
10590 retval = lpfc_sli_enable_msix(phba);
10591 if (!retval) {
10592 /* Indicate initialization to MSI-X mode */
10593 phba->intr_type = MSIX;
10594 intr_mode = 2;
10595 }
10596 }
10597 }
10598
10599 /* Fallback to MSI if MSI-X initialization failed */
10600 if (cfg_mode >= 1 && phba->intr_type == NONE) {
10601 retval = lpfc_sli_enable_msi(phba);
10602 if (!retval) {
10603 /* Indicate initialization to MSI mode */
10604 phba->intr_type = MSI;
10605 intr_mode = 1;
10606 }
10607 }
10608
10609 /* Fallback to INTx if both MSI-X/MSI initalization failed */
10610 if (phba->intr_type == NONE) {
10611 retval = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
10612 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
10613 if (!retval) {
10614 /* Indicate initialization to INTx mode */
10615 phba->intr_type = INTx;
10616 intr_mode = 0;
10617 }
10618 }
10619 return intr_mode;
10620}
10621
10622/**
10623 * lpfc_sli_disable_intr - Disable device interrupt to SLI-3 device.
10624 * @phba: pointer to lpfc hba data structure.
10625 *
10626 * This routine is invoked to disable device interrupt and disassociate the
10627 * driver's interrupt handler(s) from interrupt vector(s) to device with
10628 * SLI-3 interface spec. Depending on the interrupt mode, the driver will
10629 * release the interrupt vector(s) for the message signaled interrupt.
10630 **/
10631static void
10632lpfc_sli_disable_intr(struct lpfc_hba *phba)
10633{
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010634 int nr_irqs, i;
10635
James Smart3772a992009-05-22 14:50:54 -040010636 if (phba->intr_type == MSIX)
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010637 nr_irqs = LPFC_MSIX_VECTORS;
10638 else
10639 nr_irqs = 1;
10640
10641 for (i = 0; i < nr_irqs; i++)
10642 free_irq(pci_irq_vector(phba->pcidev, i), phba);
10643 pci_free_irq_vectors(phba->pcidev);
James Smart3772a992009-05-22 14:50:54 -040010644
10645 /* Reset interrupt management states */
10646 phba->intr_type = NONE;
10647 phba->sli.slistat.sli_intr = 0;
James Smart3772a992009-05-22 14:50:54 -040010648}
10649
10650/**
James Smart657add42019-05-21 17:49:06 -070010651 * lpfc_find_cpu_handle - Find the CPU that corresponds to the specified Queue
James Smart6a828b02019-01-28 11:14:31 -080010652 * @phba: pointer to lpfc hba data structure.
10653 * @id: EQ vector index or Hardware Queue index
10654 * @match: LPFC_FIND_BY_EQ = match by EQ
10655 * LPFC_FIND_BY_HDWQ = match by Hardware Queue
James Smart657add42019-05-21 17:49:06 -070010656 * Return the CPU that matches the selection criteria
James Smart6a828b02019-01-28 11:14:31 -080010657 */
10658static uint16_t
10659lpfc_find_cpu_handle(struct lpfc_hba *phba, uint16_t id, int match)
10660{
10661 struct lpfc_vector_map_info *cpup;
10662 int cpu;
10663
James Smart657add42019-05-21 17:49:06 -070010664 /* Loop through all CPUs */
James Smart222e9232019-01-28 11:14:35 -080010665 for_each_present_cpu(cpu) {
10666 cpup = &phba->sli4_hba.cpu_map[cpu];
James Smart657add42019-05-21 17:49:06 -070010667
10668 /* If we are matching by EQ, there may be multiple CPUs using
10669 * using the same vector, so select the one with
10670 * LPFC_CPU_FIRST_IRQ set.
10671 */
James Smart6a828b02019-01-28 11:14:31 -080010672 if ((match == LPFC_FIND_BY_EQ) &&
James Smart657add42019-05-21 17:49:06 -070010673 (cpup->flag & LPFC_CPU_FIRST_IRQ) &&
James Smart6a828b02019-01-28 11:14:31 -080010674 (cpup->irq != LPFC_VECTOR_MAP_EMPTY) &&
10675 (cpup->eq == id))
10676 return cpu;
James Smart657add42019-05-21 17:49:06 -070010677
10678 /* If matching by HDWQ, select the first CPU that matches */
James Smart6a828b02019-01-28 11:14:31 -080010679 if ((match == LPFC_FIND_BY_HDWQ) && (cpup->hdwq == id))
10680 return cpu;
James Smart6a828b02019-01-28 11:14:31 -080010681 }
10682 return 0;
10683}
10684
James Smart6a828b02019-01-28 11:14:31 -080010685#ifdef CONFIG_X86
10686/**
10687 * lpfc_find_hyper - Determine if the CPU map entry is hyper-threaded
10688 * @phba: pointer to lpfc hba data structure.
10689 * @cpu: CPU map index
10690 * @phys_id: CPU package physical id
10691 * @core_id: CPU core id
10692 */
10693static int
10694lpfc_find_hyper(struct lpfc_hba *phba, int cpu,
10695 uint16_t phys_id, uint16_t core_id)
10696{
10697 struct lpfc_vector_map_info *cpup;
10698 int idx;
10699
James Smart222e9232019-01-28 11:14:35 -080010700 for_each_present_cpu(idx) {
10701 cpup = &phba->sli4_hba.cpu_map[idx];
James Smart6a828b02019-01-28 11:14:31 -080010702 /* Does the cpup match the one we are looking for */
10703 if ((cpup->phys_id == phys_id) &&
10704 (cpup->core_id == core_id) &&
James Smart222e9232019-01-28 11:14:35 -080010705 (cpu != idx))
James Smart6a828b02019-01-28 11:14:31 -080010706 return 1;
James Smart6a828b02019-01-28 11:14:31 -080010707 }
10708 return 0;
10709}
10710#endif
10711
10712/**
James Smart895427b2017-02-12 13:52:30 -080010713 * lpfc_cpu_affinity_check - Check vector CPU affinity mappings
James Smart7bb03bb2013-04-17 20:19:16 -040010714 * @phba: pointer to lpfc hba data structure.
James Smart895427b2017-02-12 13:52:30 -080010715 * @vectors: number of msix vectors allocated.
James Smart7bb03bb2013-04-17 20:19:16 -040010716 *
James Smart895427b2017-02-12 13:52:30 -080010717 * The routine will figure out the CPU affinity assignment for every
James Smart6a828b02019-01-28 11:14:31 -080010718 * MSI-X vector allocated for the HBA.
James Smart895427b2017-02-12 13:52:30 -080010719 * In addition, the CPU to IO channel mapping will be calculated
10720 * and the phba->sli4_hba.cpu_map array will reflect this.
James Smart7bb03bb2013-04-17 20:19:16 -040010721 */
James Smart895427b2017-02-12 13:52:30 -080010722static void
10723lpfc_cpu_affinity_check(struct lpfc_hba *phba, int vectors)
James Smart7bb03bb2013-04-17 20:19:16 -040010724{
James Smart3ad348d2019-08-14 16:56:43 -070010725 int i, cpu, idx, next_idx, new_cpu, start_cpu, first_cpu;
James Smart6a828b02019-01-28 11:14:31 -080010726 int max_phys_id, min_phys_id;
10727 int max_core_id, min_core_id;
James Smart7bb03bb2013-04-17 20:19:16 -040010728 struct lpfc_vector_map_info *cpup;
James Smartd9954a22019-05-21 17:49:05 -070010729 struct lpfc_vector_map_info *new_cpup;
James Smart75508a82019-01-28 11:14:34 -080010730 const struct cpumask *maskp;
James Smart7bb03bb2013-04-17 20:19:16 -040010731#ifdef CONFIG_X86
10732 struct cpuinfo_x86 *cpuinfo;
10733#endif
James Smart7bb03bb2013-04-17 20:19:16 -040010734
10735 /* Init cpu_map array */
James Smartd9954a22019-05-21 17:49:05 -070010736 for_each_possible_cpu(cpu) {
10737 cpup = &phba->sli4_hba.cpu_map[cpu];
10738 cpup->phys_id = LPFC_VECTOR_MAP_EMPTY;
10739 cpup->core_id = LPFC_VECTOR_MAP_EMPTY;
10740 cpup->hdwq = LPFC_VECTOR_MAP_EMPTY;
10741 cpup->eq = LPFC_VECTOR_MAP_EMPTY;
10742 cpup->irq = LPFC_VECTOR_MAP_EMPTY;
10743 cpup->flag = 0;
10744 }
James Smart7bb03bb2013-04-17 20:19:16 -040010745
James Smart6a828b02019-01-28 11:14:31 -080010746 max_phys_id = 0;
James Smartd9954a22019-05-21 17:49:05 -070010747 min_phys_id = LPFC_VECTOR_MAP_EMPTY;
James Smart6a828b02019-01-28 11:14:31 -080010748 max_core_id = 0;
James Smartd9954a22019-05-21 17:49:05 -070010749 min_core_id = LPFC_VECTOR_MAP_EMPTY;
James Smart7bb03bb2013-04-17 20:19:16 -040010750
10751 /* Update CPU map with physical id and core id of each CPU */
James Smart222e9232019-01-28 11:14:35 -080010752 for_each_present_cpu(cpu) {
10753 cpup = &phba->sli4_hba.cpu_map[cpu];
James Smart7bb03bb2013-04-17 20:19:16 -040010754#ifdef CONFIG_X86
10755 cpuinfo = &cpu_data(cpu);
10756 cpup->phys_id = cpuinfo->phys_proc_id;
10757 cpup->core_id = cpuinfo->cpu_core_id;
James Smartd9954a22019-05-21 17:49:05 -070010758 if (lpfc_find_hyper(phba, cpu, cpup->phys_id, cpup->core_id))
10759 cpup->flag |= LPFC_CPU_MAP_HYPER;
James Smart7bb03bb2013-04-17 20:19:16 -040010760#else
10761 /* No distinction between CPUs for other platforms */
10762 cpup->phys_id = 0;
James Smart6a828b02019-01-28 11:14:31 -080010763 cpup->core_id = cpu;
James Smart7bb03bb2013-04-17 20:19:16 -040010764#endif
James Smart7bb03bb2013-04-17 20:19:16 -040010765
James Smartb3295c22019-01-28 11:14:30 -080010766 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart3ad348d2019-08-14 16:56:43 -070010767 "3328 CPU %d physid %d coreid %d flag x%x\n",
10768 cpu, cpup->phys_id, cpup->core_id, cpup->flag);
James Smart6a828b02019-01-28 11:14:31 -080010769
10770 if (cpup->phys_id > max_phys_id)
10771 max_phys_id = cpup->phys_id;
10772 if (cpup->phys_id < min_phys_id)
10773 min_phys_id = cpup->phys_id;
10774
10775 if (cpup->core_id > max_core_id)
10776 max_core_id = cpup->core_id;
10777 if (cpup->core_id < min_core_id)
10778 min_core_id = cpup->core_id;
James Smart7bb03bb2013-04-17 20:19:16 -040010779 }
James Smartb3295c22019-01-28 11:14:30 -080010780
James Smart32517fc2019-01-28 11:14:33 -080010781 for_each_possible_cpu(i) {
10782 struct lpfc_eq_intr_info *eqi =
10783 per_cpu_ptr(phba->sli4_hba.eq_info, i);
10784
10785 INIT_LIST_HEAD(&eqi->list);
10786 eqi->icnt = 0;
10787 }
10788
James Smartd9954a22019-05-21 17:49:05 -070010789 /* This loop sets up all CPUs that are affinitized with a
10790 * irq vector assigned to the driver. All affinitized CPUs
James Smart657add42019-05-21 17:49:06 -070010791 * will get a link to that vectors IRQ and EQ.
James Smartd9954a22019-05-21 17:49:05 -070010792 */
James Smart75508a82019-01-28 11:14:34 -080010793 for (idx = 0; idx < phba->cfg_irq_chann; idx++) {
James Smart657add42019-05-21 17:49:06 -070010794 /* Get a CPU mask for all CPUs affinitized to this vector */
James Smart75508a82019-01-28 11:14:34 -080010795 maskp = pci_irq_get_affinity(phba->pcidev, idx);
10796 if (!maskp)
10797 continue;
10798
James Smart657add42019-05-21 17:49:06 -070010799 i = 0;
10800 /* Loop through all CPUs associated with vector idx */
James Smart75508a82019-01-28 11:14:34 -080010801 for_each_cpu_and(cpu, maskp, cpu_present_mask) {
James Smart657add42019-05-21 17:49:06 -070010802 /* Set the EQ index and IRQ for that vector */
James Smart75508a82019-01-28 11:14:34 -080010803 cpup = &phba->sli4_hba.cpu_map[cpu];
James Smart6a828b02019-01-28 11:14:31 -080010804 cpup->eq = idx;
James Smart6a828b02019-01-28 11:14:31 -080010805 cpup->irq = pci_irq_vector(phba->pcidev, idx);
James Smartb3295c22019-01-28 11:14:30 -080010806
James Smart657add42019-05-21 17:49:06 -070010807 /* If this is the first CPU thats assigned to this
10808 * vector, set LPFC_CPU_FIRST_IRQ.
10809 */
10810 if (!i)
10811 cpup->flag |= LPFC_CPU_FIRST_IRQ;
10812 i++;
James Smart3ad348d2019-08-14 16:56:43 -070010813
10814 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10815 "3336 Set Affinity: CPU %d "
10816 "irq %d eq %d flag x%x\n",
10817 cpu, cpup->irq, cpup->eq, cpup->flag);
James Smart6a828b02019-01-28 11:14:31 -080010818 }
James Smartb3295c22019-01-28 11:14:30 -080010819 }
James Smartd9954a22019-05-21 17:49:05 -070010820
10821 /* After looking at each irq vector assigned to this pcidev, its
10822 * possible to see that not ALL CPUs have been accounted for.
James Smart657add42019-05-21 17:49:06 -070010823 * Next we will set any unassigned (unaffinitized) cpu map
10824 * entries to a IRQ on the same phys_id.
James Smartd9954a22019-05-21 17:49:05 -070010825 */
10826 first_cpu = cpumask_first(cpu_present_mask);
10827 start_cpu = first_cpu;
10828
10829 for_each_present_cpu(cpu) {
10830 cpup = &phba->sli4_hba.cpu_map[cpu];
10831
10832 /* Is this CPU entry unassigned */
10833 if (cpup->eq == LPFC_VECTOR_MAP_EMPTY) {
10834 /* Mark CPU as IRQ not assigned by the kernel */
10835 cpup->flag |= LPFC_CPU_MAP_UNASSIGN;
10836
James Smart657add42019-05-21 17:49:06 -070010837 /* If so, find a new_cpup thats on the the SAME
James Smartd9954a22019-05-21 17:49:05 -070010838 * phys_id as cpup. start_cpu will start where we
10839 * left off so all unassigned entries don't get assgined
10840 * the IRQ of the first entry.
10841 */
10842 new_cpu = start_cpu;
10843 for (i = 0; i < phba->sli4_hba.num_present_cpu; i++) {
10844 new_cpup = &phba->sli4_hba.cpu_map[new_cpu];
10845 if (!(new_cpup->flag & LPFC_CPU_MAP_UNASSIGN) &&
10846 (new_cpup->irq != LPFC_VECTOR_MAP_EMPTY) &&
10847 (new_cpup->phys_id == cpup->phys_id))
10848 goto found_same;
10849 new_cpu = cpumask_next(
10850 new_cpu, cpu_present_mask);
10851 if (new_cpu == nr_cpumask_bits)
10852 new_cpu = first_cpu;
10853 }
10854 /* At this point, we leave the CPU as unassigned */
10855 continue;
10856found_same:
10857 /* We found a matching phys_id, so copy the IRQ info */
10858 cpup->eq = new_cpup->eq;
James Smartd9954a22019-05-21 17:49:05 -070010859 cpup->irq = new_cpup->irq;
10860
10861 /* Bump start_cpu to the next slot to minmize the
10862 * chance of having multiple unassigned CPU entries
10863 * selecting the same IRQ.
10864 */
10865 start_cpu = cpumask_next(new_cpu, cpu_present_mask);
10866 if (start_cpu == nr_cpumask_bits)
10867 start_cpu = first_cpu;
10868
James Smart657add42019-05-21 17:49:06 -070010869 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smartd9954a22019-05-21 17:49:05 -070010870 "3337 Set Affinity: CPU %d "
James Smart657add42019-05-21 17:49:06 -070010871 "irq %d from id %d same "
James Smartd9954a22019-05-21 17:49:05 -070010872 "phys_id (%d)\n",
James Smart657add42019-05-21 17:49:06 -070010873 cpu, cpup->irq, new_cpu, cpup->phys_id);
James Smartd9954a22019-05-21 17:49:05 -070010874 }
10875 }
10876
10877 /* Set any unassigned cpu map entries to a IRQ on any phys_id */
10878 start_cpu = first_cpu;
10879
10880 for_each_present_cpu(cpu) {
10881 cpup = &phba->sli4_hba.cpu_map[cpu];
10882
10883 /* Is this entry unassigned */
10884 if (cpup->eq == LPFC_VECTOR_MAP_EMPTY) {
10885 /* Mark it as IRQ not assigned by the kernel */
10886 cpup->flag |= LPFC_CPU_MAP_UNASSIGN;
10887
James Smart657add42019-05-21 17:49:06 -070010888 /* If so, find a new_cpup thats on ANY phys_id
James Smartd9954a22019-05-21 17:49:05 -070010889 * as the cpup. start_cpu will start where we
10890 * left off so all unassigned entries don't get
10891 * assigned the IRQ of the first entry.
10892 */
10893 new_cpu = start_cpu;
10894 for (i = 0; i < phba->sli4_hba.num_present_cpu; i++) {
10895 new_cpup = &phba->sli4_hba.cpu_map[new_cpu];
10896 if (!(new_cpup->flag & LPFC_CPU_MAP_UNASSIGN) &&
10897 (new_cpup->irq != LPFC_VECTOR_MAP_EMPTY))
10898 goto found_any;
10899 new_cpu = cpumask_next(
10900 new_cpu, cpu_present_mask);
10901 if (new_cpu == nr_cpumask_bits)
10902 new_cpu = first_cpu;
10903 }
10904 /* We should never leave an entry unassigned */
10905 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10906 "3339 Set Affinity: CPU %d "
James Smart657add42019-05-21 17:49:06 -070010907 "irq %d UNASSIGNED\n",
10908 cpup->hdwq, cpup->irq);
James Smartd9954a22019-05-21 17:49:05 -070010909 continue;
10910found_any:
10911 /* We found an available entry, copy the IRQ info */
10912 cpup->eq = new_cpup->eq;
James Smartd9954a22019-05-21 17:49:05 -070010913 cpup->irq = new_cpup->irq;
10914
10915 /* Bump start_cpu to the next slot to minmize the
10916 * chance of having multiple unassigned CPU entries
10917 * selecting the same IRQ.
10918 */
10919 start_cpu = cpumask_next(new_cpu, cpu_present_mask);
10920 if (start_cpu == nr_cpumask_bits)
10921 start_cpu = first_cpu;
10922
James Smart657add42019-05-21 17:49:06 -070010923 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smartd9954a22019-05-21 17:49:05 -070010924 "3338 Set Affinity: CPU %d "
James Smart657add42019-05-21 17:49:06 -070010925 "irq %d from id %d (%d/%d)\n",
10926 cpu, cpup->irq, new_cpu,
James Smartd9954a22019-05-21 17:49:05 -070010927 new_cpup->phys_id, new_cpup->core_id);
10928 }
10929 }
James Smart657add42019-05-21 17:49:06 -070010930
James Smart3ad348d2019-08-14 16:56:43 -070010931 /* Assign hdwq indices that are unique across all cpus in the map
10932 * that are also FIRST_CPUs.
10933 */
10934 idx = 0;
10935 for_each_present_cpu(cpu) {
10936 cpup = &phba->sli4_hba.cpu_map[cpu];
10937
10938 /* Only FIRST IRQs get a hdwq index assignment. */
10939 if (!(cpup->flag & LPFC_CPU_FIRST_IRQ))
10940 continue;
10941
10942 /* 1 to 1, the first LPFC_CPU_FIRST_IRQ cpus to a unique hdwq */
10943 cpup->hdwq = idx;
10944 idx++;
10945 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10946 "3333 Set Affinity: CPU %d (phys %d core %d): "
10947 "hdwq %d eq %d irq %d flg x%x\n",
10948 cpu, cpup->phys_id, cpup->core_id,
10949 cpup->hdwq, cpup->eq, cpup->irq, cpup->flag);
10950 }
James Smart657add42019-05-21 17:49:06 -070010951 /* Finally we need to associate a hdwq with each cpu_map entry
10952 * This will be 1 to 1 - hdwq to cpu, unless there are less
10953 * hardware queues then CPUs. For that case we will just round-robin
10954 * the available hardware queues as they get assigned to CPUs.
James Smart3ad348d2019-08-14 16:56:43 -070010955 * The next_idx is the idx from the FIRST_CPU loop above to account
10956 * for irq_chann < hdwq. The idx is used for round-robin assignments
10957 * and needs to start at 0.
James Smart657add42019-05-21 17:49:06 -070010958 */
James Smart3ad348d2019-08-14 16:56:43 -070010959 next_idx = idx;
James Smart657add42019-05-21 17:49:06 -070010960 start_cpu = 0;
James Smart3ad348d2019-08-14 16:56:43 -070010961 idx = 0;
James Smart657add42019-05-21 17:49:06 -070010962 for_each_present_cpu(cpu) {
10963 cpup = &phba->sli4_hba.cpu_map[cpu];
James Smart657add42019-05-21 17:49:06 -070010964
James Smart3ad348d2019-08-14 16:56:43 -070010965 /* FIRST cpus are already mapped. */
10966 if (cpup->flag & LPFC_CPU_FIRST_IRQ)
10967 continue;
James Smart657add42019-05-21 17:49:06 -070010968
James Smart3ad348d2019-08-14 16:56:43 -070010969 /* If the cfg_irq_chann < cfg_hdw_queue, set the hdwq
10970 * of the unassigned cpus to the next idx so that all
10971 * hdw queues are fully utilized.
10972 */
10973 if (next_idx < phba->cfg_hdw_queue) {
10974 cpup->hdwq = next_idx;
10975 next_idx++;
10976 continue;
James Smart657add42019-05-21 17:49:06 -070010977 }
James Smart3ad348d2019-08-14 16:56:43 -070010978
10979 /* Not a First CPU and all hdw_queues are used. Reuse a
10980 * Hardware Queue for another CPU, so be smart about it
10981 * and pick one that has its IRQ/EQ mapped to the same phys_id
10982 * (CPU package) and core_id.
10983 */
10984 new_cpu = start_cpu;
10985 for (i = 0; i < phba->sli4_hba.num_present_cpu; i++) {
10986 new_cpup = &phba->sli4_hba.cpu_map[new_cpu];
10987 if (new_cpup->hdwq != LPFC_VECTOR_MAP_EMPTY &&
10988 new_cpup->phys_id == cpup->phys_id &&
10989 new_cpup->core_id == cpup->core_id) {
10990 goto found_hdwq;
10991 }
10992 new_cpu = cpumask_next(new_cpu, cpu_present_mask);
10993 if (new_cpu == nr_cpumask_bits)
10994 new_cpu = first_cpu;
10995 }
10996
10997 /* If we can't match both phys_id and core_id,
10998 * settle for just a phys_id match.
10999 */
11000 new_cpu = start_cpu;
11001 for (i = 0; i < phba->sli4_hba.num_present_cpu; i++) {
11002 new_cpup = &phba->sli4_hba.cpu_map[new_cpu];
11003 if (new_cpup->hdwq != LPFC_VECTOR_MAP_EMPTY &&
11004 new_cpup->phys_id == cpup->phys_id)
11005 goto found_hdwq;
11006
11007 new_cpu = cpumask_next(new_cpu, cpu_present_mask);
11008 if (new_cpu == nr_cpumask_bits)
11009 new_cpu = first_cpu;
11010 }
11011
11012 /* Otherwise just round robin on cfg_hdw_queue */
11013 cpup->hdwq = idx % phba->cfg_hdw_queue;
11014 idx++;
11015 goto logit;
11016 found_hdwq:
11017 /* We found an available entry, copy the IRQ info */
11018 start_cpu = cpumask_next(new_cpu, cpu_present_mask);
11019 if (start_cpu == nr_cpumask_bits)
11020 start_cpu = first_cpu;
11021 cpup->hdwq = new_cpup->hdwq;
11022 logit:
James Smart657add42019-05-21 17:49:06 -070011023 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11024 "3335 Set Affinity: CPU %d (phys %d core %d): "
11025 "hdwq %d eq %d irq %d flg x%x\n",
11026 cpu, cpup->phys_id, cpup->core_id,
11027 cpup->hdwq, cpup->eq, cpup->irq, cpup->flag);
James Smart657add42019-05-21 17:49:06 -070011028 }
11029
11030 /* The cpu_map array will be used later during initialization
11031 * when EQ / CQ / WQs are allocated and configured.
11032 */
James Smartb3295c22019-01-28 11:14:30 -080011033 return;
James Smart7bb03bb2013-04-17 20:19:16 -040011034}
James Smart7bb03bb2013-04-17 20:19:16 -040011035
11036/**
James Smartda0436e2009-05-22 14:51:39 -040011037 * lpfc_sli4_enable_msix - Enable MSI-X interrupt mode to SLI-4 device
11038 * @phba: pointer to lpfc hba data structure.
11039 *
11040 * This routine is invoked to enable the MSI-X interrupt vectors to device
Christoph Hellwig45ffac12017-02-12 13:52:26 -080011041 * with SLI-4 interface spec.
James Smartda0436e2009-05-22 14:51:39 -040011042 *
11043 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020011044 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -040011045 * other values - error
11046 **/
11047static int
11048lpfc_sli4_enable_msix(struct lpfc_hba *phba)
11049{
James Smart75baf692010-06-08 18:31:21 -040011050 int vectors, rc, index;
James Smartb83d0052017-06-01 21:07:05 -070011051 char *name;
James Smartda0436e2009-05-22 14:51:39 -040011052
11053 /* Set up MSI-X multi-message vectors */
James Smart6a828b02019-01-28 11:14:31 -080011054 vectors = phba->cfg_irq_chann;
Christoph Hellwig45ffac12017-02-12 13:52:26 -080011055
James Smartf358dd02017-02-12 13:52:34 -080011056 rc = pci_alloc_irq_vectors(phba->pcidev,
James Smart75508a82019-01-28 11:14:34 -080011057 1,
James Smartf358dd02017-02-12 13:52:34 -080011058 vectors, PCI_IRQ_MSIX | PCI_IRQ_AFFINITY);
Alexander Gordeev4f871e12014-09-03 12:56:29 -040011059 if (rc < 0) {
James Smartda0436e2009-05-22 14:51:39 -040011060 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
11061 "0484 PCI enable MSI-X failed (%d)\n", rc);
Alexander Gordeev029165a2014-07-16 20:05:15 +020011062 goto vec_fail_out;
James Smartda0436e2009-05-22 14:51:39 -040011063 }
Alexander Gordeev4f871e12014-09-03 12:56:29 -040011064 vectors = rc;
James Smart75baf692010-06-08 18:31:21 -040011065
James Smart7bb03bb2013-04-17 20:19:16 -040011066 /* Assign MSI-X vectors to interrupt handlers */
James Smart67d12732012-08-03 12:36:13 -040011067 for (index = 0; index < vectors; index++) {
James Smartb83d0052017-06-01 21:07:05 -070011068 name = phba->sli4_hba.hba_eq_hdl[index].handler_name;
11069 memset(name, 0, LPFC_SLI4_HANDLER_NAME_SZ);
11070 snprintf(name, LPFC_SLI4_HANDLER_NAME_SZ,
James Smart4305f182012-08-03 12:36:33 -040011071 LPFC_DRIVER_HANDLER_NAME"%d", index);
James Smartda0436e2009-05-22 14:51:39 -040011072
James Smart895427b2017-02-12 13:52:30 -080011073 phba->sli4_hba.hba_eq_hdl[index].idx = index;
11074 phba->sli4_hba.hba_eq_hdl[index].phba = phba;
James Smart7370d102019-01-28 11:14:20 -080011075 rc = request_irq(pci_irq_vector(phba->pcidev, index),
11076 &lpfc_sli4_hba_intr_handler, 0,
11077 name,
11078 &phba->sli4_hba.hba_eq_hdl[index]);
James Smartda0436e2009-05-22 14:51:39 -040011079 if (rc) {
11080 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
11081 "0486 MSI-X fast-path (%d) "
11082 "request_irq failed (%d)\n", index, rc);
11083 goto cfg_fail_out;
11084 }
11085 }
11086
James Smart6a828b02019-01-28 11:14:31 -080011087 if (vectors != phba->cfg_irq_chann) {
James Smart82c3e9b2012-09-29 11:29:50 -040011088 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11089 "3238 Reducing IO channels to match number of "
11090 "MSI-X vectors, requested %d got %d\n",
James Smart6a828b02019-01-28 11:14:31 -080011091 phba->cfg_irq_chann, vectors);
11092 if (phba->cfg_irq_chann > vectors)
11093 phba->cfg_irq_chann = vectors;
James Smart982ab122019-03-12 16:30:10 -070011094 if (phba->nvmet_support && (phba->cfg_nvmet_mrq > vectors))
James Smartcdb42be2019-01-28 11:14:21 -080011095 phba->cfg_nvmet_mrq = vectors;
James Smart82c3e9b2012-09-29 11:29:50 -040011096 }
James Smart7bb03bb2013-04-17 20:19:16 -040011097
James Smartda0436e2009-05-22 14:51:39 -040011098 return rc;
11099
11100cfg_fail_out:
11101 /* free the irq already requested */
James Smart895427b2017-02-12 13:52:30 -080011102 for (--index; index >= 0; index--)
11103 free_irq(pci_irq_vector(phba->pcidev, index),
11104 &phba->sli4_hba.hba_eq_hdl[index]);
James Smartda0436e2009-05-22 14:51:39 -040011105
James Smartda0436e2009-05-22 14:51:39 -040011106 /* Unconfigure MSI-X capability structure */
Christoph Hellwig45ffac12017-02-12 13:52:26 -080011107 pci_free_irq_vectors(phba->pcidev);
Alexander Gordeev029165a2014-07-16 20:05:15 +020011108
11109vec_fail_out:
James Smartda0436e2009-05-22 14:51:39 -040011110 return rc;
11111}
11112
11113/**
James Smartda0436e2009-05-22 14:51:39 -040011114 * lpfc_sli4_enable_msi - Enable MSI interrupt mode to SLI-4 device
11115 * @phba: pointer to lpfc hba data structure.
11116 *
11117 * This routine is invoked to enable the MSI interrupt mode to device with
11118 * SLI-4 interface spec. The kernel function pci_enable_msi() is called
11119 * to enable the MSI vector. The device driver is responsible for calling
11120 * the request_irq() to register MSI vector with a interrupt the handler,
11121 * which is done in this function.
11122 *
11123 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020011124 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -040011125 * other values - error
11126 **/
11127static int
11128lpfc_sli4_enable_msi(struct lpfc_hba *phba)
11129{
11130 int rc, index;
11131
11132 rc = pci_enable_msi(phba->pcidev);
11133 if (!rc)
11134 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
11135 "0487 PCI enable MSI mode success.\n");
11136 else {
11137 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
11138 "0488 PCI enable MSI mode failed (%d)\n", rc);
11139 return rc;
11140 }
11141
11142 rc = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
James Smarted243d32015-05-21 13:55:25 -040011143 0, LPFC_DRIVER_NAME, phba);
James Smartda0436e2009-05-22 14:51:39 -040011144 if (rc) {
11145 pci_disable_msi(phba->pcidev);
11146 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
11147 "0490 MSI request_irq failed (%d)\n", rc);
James Smart75baf692010-06-08 18:31:21 -040011148 return rc;
James Smartda0436e2009-05-22 14:51:39 -040011149 }
11150
James Smart6a828b02019-01-28 11:14:31 -080011151 for (index = 0; index < phba->cfg_irq_chann; index++) {
James Smart895427b2017-02-12 13:52:30 -080011152 phba->sli4_hba.hba_eq_hdl[index].idx = index;
11153 phba->sli4_hba.hba_eq_hdl[index].phba = phba;
James Smartda0436e2009-05-22 14:51:39 -040011154 }
11155
James Smart75baf692010-06-08 18:31:21 -040011156 return 0;
James Smartda0436e2009-05-22 14:51:39 -040011157}
11158
11159/**
James Smartda0436e2009-05-22 14:51:39 -040011160 * lpfc_sli4_enable_intr - Enable device interrupt to SLI-4 device
11161 * @phba: pointer to lpfc hba data structure.
11162 *
11163 * This routine is invoked to enable device interrupt and associate driver's
11164 * interrupt handler(s) to interrupt vector(s) to device with SLI-4
11165 * interface spec. Depends on the interrupt mode configured to the driver,
11166 * the driver will try to fallback from the configured interrupt mode to an
11167 * interrupt mode which is supported by the platform, kernel, and device in
11168 * the order of:
11169 * MSI-X -> MSI -> IRQ.
11170 *
11171 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020011172 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -040011173 * other values - error
11174 **/
11175static uint32_t
11176lpfc_sli4_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
11177{
11178 uint32_t intr_mode = LPFC_INTR_ERROR;
James Smart895427b2017-02-12 13:52:30 -080011179 int retval, idx;
James Smartda0436e2009-05-22 14:51:39 -040011180
11181 if (cfg_mode == 2) {
11182 /* Preparation before conf_msi mbox cmd */
11183 retval = 0;
11184 if (!retval) {
11185 /* Now, try to enable MSI-X interrupt mode */
11186 retval = lpfc_sli4_enable_msix(phba);
11187 if (!retval) {
11188 /* Indicate initialization to MSI-X mode */
11189 phba->intr_type = MSIX;
11190 intr_mode = 2;
11191 }
11192 }
11193 }
11194
11195 /* Fallback to MSI if MSI-X initialization failed */
11196 if (cfg_mode >= 1 && phba->intr_type == NONE) {
11197 retval = lpfc_sli4_enable_msi(phba);
11198 if (!retval) {
11199 /* Indicate initialization to MSI mode */
11200 phba->intr_type = MSI;
11201 intr_mode = 1;
11202 }
11203 }
11204
11205 /* Fallback to INTx if both MSI-X/MSI initalization failed */
11206 if (phba->intr_type == NONE) {
11207 retval = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
11208 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
11209 if (!retval) {
James Smart895427b2017-02-12 13:52:30 -080011210 struct lpfc_hba_eq_hdl *eqhdl;
11211
James Smartda0436e2009-05-22 14:51:39 -040011212 /* Indicate initialization to INTx mode */
11213 phba->intr_type = INTx;
11214 intr_mode = 0;
James Smart895427b2017-02-12 13:52:30 -080011215
James Smart6a828b02019-01-28 11:14:31 -080011216 for (idx = 0; idx < phba->cfg_irq_chann; idx++) {
James Smart895427b2017-02-12 13:52:30 -080011217 eqhdl = &phba->sli4_hba.hba_eq_hdl[idx];
11218 eqhdl->idx = idx;
11219 eqhdl->phba = phba;
James Smart1ba981f2014-02-20 09:56:45 -050011220 }
James Smartda0436e2009-05-22 14:51:39 -040011221 }
11222 }
11223 return intr_mode;
11224}
11225
11226/**
11227 * lpfc_sli4_disable_intr - Disable device interrupt to SLI-4 device
11228 * @phba: pointer to lpfc hba data structure.
11229 *
11230 * This routine is invoked to disable device interrupt and disassociate
11231 * the driver's interrupt handler(s) from interrupt vector(s) to device
11232 * with SLI-4 interface spec. Depending on the interrupt mode, the driver
11233 * will release the interrupt vector(s) for the message signaled interrupt.
11234 **/
11235static void
11236lpfc_sli4_disable_intr(struct lpfc_hba *phba)
11237{
11238 /* Disable the currently initialized interrupt mode */
Christoph Hellwig45ffac12017-02-12 13:52:26 -080011239 if (phba->intr_type == MSIX) {
11240 int index;
11241
11242 /* Free up MSI-X multi-message vectors */
James Smart6a828b02019-01-28 11:14:31 -080011243 for (index = 0; index < phba->cfg_irq_chann; index++) {
James Smartb3295c22019-01-28 11:14:30 -080011244 irq_set_affinity_hint(
11245 pci_irq_vector(phba->pcidev, index),
11246 NULL);
James Smart895427b2017-02-12 13:52:30 -080011247 free_irq(pci_irq_vector(phba->pcidev, index),
11248 &phba->sli4_hba.hba_eq_hdl[index]);
James Smartb3295c22019-01-28 11:14:30 -080011249 }
Christoph Hellwig45ffac12017-02-12 13:52:26 -080011250 } else {
James Smartda0436e2009-05-22 14:51:39 -040011251 free_irq(phba->pcidev->irq, phba);
Christoph Hellwig45ffac12017-02-12 13:52:26 -080011252 }
11253
11254 pci_free_irq_vectors(phba->pcidev);
James Smartda0436e2009-05-22 14:51:39 -040011255
11256 /* Reset interrupt management states */
11257 phba->intr_type = NONE;
11258 phba->sli.slistat.sli_intr = 0;
James Smartda0436e2009-05-22 14:51:39 -040011259}
11260
11261/**
James Smart3772a992009-05-22 14:50:54 -040011262 * lpfc_unset_hba - Unset SLI3 hba device initialization
11263 * @phba: pointer to lpfc hba data structure.
11264 *
11265 * This routine is invoked to unset the HBA device initialization steps to
11266 * a device with SLI-3 interface spec.
11267 **/
11268static void
11269lpfc_unset_hba(struct lpfc_hba *phba)
11270{
11271 struct lpfc_vport *vport = phba->pport;
11272 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
11273
11274 spin_lock_irq(shost->host_lock);
11275 vport->load_flag |= FC_UNLOADING;
11276 spin_unlock_irq(shost->host_lock);
11277
James Smart72859902012-01-18 16:25:38 -050011278 kfree(phba->vpi_bmask);
11279 kfree(phba->vpi_ids);
11280
James Smart3772a992009-05-22 14:50:54 -040011281 lpfc_stop_hba_timers(phba);
11282
11283 phba->pport->work_port_events = 0;
11284
11285 lpfc_sli_hba_down(phba);
11286
11287 lpfc_sli_brdrestart(phba);
11288
11289 lpfc_sli_disable_intr(phba);
11290
11291 return;
11292}
11293
11294/**
James Smart5af5eee2010-10-22 11:06:38 -040011295 * lpfc_sli4_xri_exchange_busy_wait - Wait for device XRI exchange busy
11296 * @phba: Pointer to HBA context object.
11297 *
11298 * This function is called in the SLI4 code path to wait for completion
11299 * of device's XRIs exchange busy. It will check the XRI exchange busy
11300 * on outstanding FCP and ELS I/Os every 10ms for up to 10 seconds; after
11301 * that, it will check the XRI exchange busy on outstanding FCP and ELS
11302 * I/Os every 30 seconds, log error message, and wait forever. Only when
11303 * all XRI exchange busy complete, the driver unload shall proceed with
11304 * invoking the function reset ioctl mailbox command to the CNA and the
11305 * the rest of the driver unload resource release.
11306 **/
11307static void
11308lpfc_sli4_xri_exchange_busy_wait(struct lpfc_hba *phba)
11309{
James Smart5e5b5112019-01-28 11:14:22 -080011310 struct lpfc_sli4_hdw_queue *qp;
11311 int idx, ccnt, fcnt;
James Smart5af5eee2010-10-22 11:06:38 -040011312 int wait_time = 0;
James Smart5e5b5112019-01-28 11:14:22 -080011313 int io_xri_cmpl = 1;
James Smart86c67372017-04-21 16:05:04 -070011314 int nvmet_xri_cmpl = 1;
James Smart895427b2017-02-12 13:52:30 -080011315 int fcp_xri_cmpl = 1;
James Smart5af5eee2010-10-22 11:06:38 -040011316 int els_xri_cmpl = list_empty(&phba->sli4_hba.lpfc_abts_els_sgl_list);
11317
James Smartc3725bd2017-11-20 16:00:42 -080011318 /* Driver just aborted IOs during the hba_unset process. Pause
11319 * here to give the HBA time to complete the IO and get entries
11320 * into the abts lists.
11321 */
11322 msleep(LPFC_XRI_EXCH_BUSY_WAIT_T1 * 5);
11323
11324 /* Wait for NVME pending IO to flush back to transport. */
11325 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
11326 lpfc_nvme_wait_for_io_drain(phba);
11327
James Smart5e5b5112019-01-28 11:14:22 -080011328 ccnt = 0;
11329 fcnt = 0;
11330 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
11331 qp = &phba->sli4_hba.hdwq[idx];
11332 fcp_xri_cmpl = list_empty(
11333 &qp->lpfc_abts_scsi_buf_list);
11334 if (!fcp_xri_cmpl) /* if list is NOT empty */
11335 fcnt++;
11336 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
11337 io_xri_cmpl = list_empty(
11338 &qp->lpfc_abts_nvme_buf_list);
11339 if (!io_xri_cmpl) /* if list is NOT empty */
11340 ccnt++;
11341 }
11342 }
11343 if (ccnt)
11344 io_xri_cmpl = 0;
11345 if (fcnt)
11346 fcp_xri_cmpl = 0;
11347
James Smart86c67372017-04-21 16:05:04 -070011348 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
James Smart86c67372017-04-21 16:05:04 -070011349 nvmet_xri_cmpl =
11350 list_empty(&phba->sli4_hba.lpfc_abts_nvmet_ctx_list);
11351 }
James Smart895427b2017-02-12 13:52:30 -080011352
James Smart5e5b5112019-01-28 11:14:22 -080011353 while (!fcp_xri_cmpl || !els_xri_cmpl || !io_xri_cmpl ||
James Smartf358dd02017-02-12 13:52:34 -080011354 !nvmet_xri_cmpl) {
James Smart5af5eee2010-10-22 11:06:38 -040011355 if (wait_time > LPFC_XRI_EXCH_BUSY_WAIT_TMO) {
James Smart68c9b552018-06-26 08:24:25 -070011356 if (!nvmet_xri_cmpl)
11357 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11358 "6424 NVMET XRI exchange busy "
11359 "wait time: %d seconds.\n",
11360 wait_time/1000);
James Smart5e5b5112019-01-28 11:14:22 -080011361 if (!io_xri_cmpl)
James Smart895427b2017-02-12 13:52:30 -080011362 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11363 "6100 NVME XRI exchange busy "
11364 "wait time: %d seconds.\n",
11365 wait_time/1000);
James Smart5af5eee2010-10-22 11:06:38 -040011366 if (!fcp_xri_cmpl)
11367 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11368 "2877 FCP XRI exchange busy "
11369 "wait time: %d seconds.\n",
11370 wait_time/1000);
11371 if (!els_xri_cmpl)
11372 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11373 "2878 ELS XRI exchange busy "
11374 "wait time: %d seconds.\n",
11375 wait_time/1000);
11376 msleep(LPFC_XRI_EXCH_BUSY_WAIT_T2);
11377 wait_time += LPFC_XRI_EXCH_BUSY_WAIT_T2;
11378 } else {
11379 msleep(LPFC_XRI_EXCH_BUSY_WAIT_T1);
11380 wait_time += LPFC_XRI_EXCH_BUSY_WAIT_T1;
11381 }
James Smart5e5b5112019-01-28 11:14:22 -080011382
11383 ccnt = 0;
11384 fcnt = 0;
11385 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
11386 qp = &phba->sli4_hba.hdwq[idx];
11387 fcp_xri_cmpl = list_empty(
11388 &qp->lpfc_abts_scsi_buf_list);
11389 if (!fcp_xri_cmpl) /* if list is NOT empty */
11390 fcnt++;
11391 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
11392 io_xri_cmpl = list_empty(
11393 &qp->lpfc_abts_nvme_buf_list);
11394 if (!io_xri_cmpl) /* if list is NOT empty */
11395 ccnt++;
11396 }
11397 }
11398 if (ccnt)
11399 io_xri_cmpl = 0;
11400 if (fcnt)
11401 fcp_xri_cmpl = 0;
11402
James Smart86c67372017-04-21 16:05:04 -070011403 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
James Smart86c67372017-04-21 16:05:04 -070011404 nvmet_xri_cmpl = list_empty(
11405 &phba->sli4_hba.lpfc_abts_nvmet_ctx_list);
11406 }
James Smart5af5eee2010-10-22 11:06:38 -040011407 els_xri_cmpl =
11408 list_empty(&phba->sli4_hba.lpfc_abts_els_sgl_list);
James Smartf358dd02017-02-12 13:52:34 -080011409
James Smart5af5eee2010-10-22 11:06:38 -040011410 }
11411}
11412
11413/**
James Smartda0436e2009-05-22 14:51:39 -040011414 * lpfc_sli4_hba_unset - Unset the fcoe hba
11415 * @phba: Pointer to HBA context object.
11416 *
11417 * This function is called in the SLI4 code path to reset the HBA's FCoE
11418 * function. The caller is not required to hold any lock. This routine
11419 * issues PCI function reset mailbox command to reset the FCoE function.
11420 * At the end of the function, it calls lpfc_hba_down_post function to
11421 * free any pending commands.
11422 **/
11423static void
11424lpfc_sli4_hba_unset(struct lpfc_hba *phba)
11425{
11426 int wait_cnt = 0;
11427 LPFC_MBOXQ_t *mboxq;
James Smart912e3ac2011-05-24 11:42:11 -040011428 struct pci_dev *pdev = phba->pcidev;
James Smartda0436e2009-05-22 14:51:39 -040011429
11430 lpfc_stop_hba_timers(phba);
James Smartcdb42be2019-01-28 11:14:21 -080011431 if (phba->pport)
11432 phba->sli4_hba.intr_enable = 0;
James Smartda0436e2009-05-22 14:51:39 -040011433
11434 /*
11435 * Gracefully wait out the potential current outstanding asynchronous
11436 * mailbox command.
11437 */
11438
11439 /* First, block any pending async mailbox command from posted */
11440 spin_lock_irq(&phba->hbalock);
11441 phba->sli.sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
11442 spin_unlock_irq(&phba->hbalock);
11443 /* Now, trying to wait it out if we can */
11444 while (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
11445 msleep(10);
11446 if (++wait_cnt > LPFC_ACTIVE_MBOX_WAIT_CNT)
11447 break;
11448 }
11449 /* Forcefully release the outstanding mailbox command if timed out */
11450 if (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
11451 spin_lock_irq(&phba->hbalock);
11452 mboxq = phba->sli.mbox_active;
11453 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
11454 __lpfc_mbox_cmpl_put(phba, mboxq);
11455 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
11456 phba->sli.mbox_active = NULL;
11457 spin_unlock_irq(&phba->hbalock);
11458 }
11459
James Smart5af5eee2010-10-22 11:06:38 -040011460 /* Abort all iocbs associated with the hba */
11461 lpfc_sli_hba_iocb_abort(phba);
11462
11463 /* Wait for completion of device XRI exchange busy */
11464 lpfc_sli4_xri_exchange_busy_wait(phba);
11465
James Smartda0436e2009-05-22 14:51:39 -040011466 /* Disable PCI subsystem interrupt */
11467 lpfc_sli4_disable_intr(phba);
11468
James Smart912e3ac2011-05-24 11:42:11 -040011469 /* Disable SR-IOV if enabled */
11470 if (phba->cfg_sriov_nr_virtfn)
11471 pci_disable_sriov(pdev);
11472
James Smartda0436e2009-05-22 14:51:39 -040011473 /* Stop kthread signal shall trigger work_done one more time */
11474 kthread_stop(phba->worker_thread);
11475
James Smartd2cc9bc2018-09-10 10:30:50 -070011476 /* Disable FW logging to host memory */
James Smart1165a5c2018-11-29 16:09:39 -080011477 lpfc_ras_stop_fwlog(phba);
James Smartd2cc9bc2018-09-10 10:30:50 -070011478
James Smartd1f525a2017-04-21 16:04:55 -070011479 /* Unset the queues shared with the hardware then release all
11480 * allocated resources.
11481 */
11482 lpfc_sli4_queue_unset(phba);
11483 lpfc_sli4_queue_destroy(phba);
11484
James Smart3677a3a2010-09-29 11:19:14 -040011485 /* Reset SLI4 HBA FCoE function */
11486 lpfc_pci_function_reset(phba);
11487
James Smart1165a5c2018-11-29 16:09:39 -080011488 /* Free RAS DMA memory */
11489 if (phba->ras_fwlog.ras_enabled)
11490 lpfc_sli4_ras_dma_free(phba);
11491
James Smartda0436e2009-05-22 14:51:39 -040011492 /* Stop the SLI4 device port */
James Smart1ffdd2c2019-03-04 15:27:51 -080011493 if (phba->pport)
11494 phba->pport->work_port_events = 0;
James Smartda0436e2009-05-22 14:51:39 -040011495}
11496
James Smart28baac72010-02-12 14:42:03 -050011497 /**
11498 * lpfc_pc_sli4_params_get - Get the SLI4_PARAMS port capabilities.
11499 * @phba: Pointer to HBA context object.
11500 * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
11501 *
11502 * This function is called in the SLI4 code path to read the port's
11503 * sli4 capabilities.
11504 *
11505 * This function may be be called from any context that can block-wait
11506 * for the completion. The expectation is that this routine is called
11507 * typically from probe_one or from the online routine.
11508 **/
11509int
11510lpfc_pc_sli4_params_get(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
11511{
11512 int rc;
11513 struct lpfc_mqe *mqe;
11514 struct lpfc_pc_sli4_params *sli4_params;
11515 uint32_t mbox_tmo;
11516
11517 rc = 0;
11518 mqe = &mboxq->u.mqe;
11519
11520 /* Read the port's SLI4 Parameters port capabilities */
James Smartfedd3b72011-02-16 12:39:24 -050011521 lpfc_pc_sli4_params(mboxq);
James Smart28baac72010-02-12 14:42:03 -050011522 if (!phba->sli4_hba.intr_enable)
11523 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
11524 else {
James Smarta183a152011-10-10 21:32:43 -040011525 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
James Smart28baac72010-02-12 14:42:03 -050011526 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
11527 }
11528
11529 if (unlikely(rc))
11530 return 1;
11531
11532 sli4_params = &phba->sli4_hba.pc_sli4_params;
11533 sli4_params->if_type = bf_get(if_type, &mqe->un.sli4_params);
11534 sli4_params->sli_rev = bf_get(sli_rev, &mqe->un.sli4_params);
11535 sli4_params->sli_family = bf_get(sli_family, &mqe->un.sli4_params);
11536 sli4_params->featurelevel_1 = bf_get(featurelevel_1,
11537 &mqe->un.sli4_params);
11538 sli4_params->featurelevel_2 = bf_get(featurelevel_2,
11539 &mqe->un.sli4_params);
11540 sli4_params->proto_types = mqe->un.sli4_params.word3;
11541 sli4_params->sge_supp_len = mqe->un.sli4_params.sge_supp_len;
11542 sli4_params->if_page_sz = bf_get(if_page_sz, &mqe->un.sli4_params);
11543 sli4_params->rq_db_window = bf_get(rq_db_window, &mqe->un.sli4_params);
11544 sli4_params->loopbk_scope = bf_get(loopbk_scope, &mqe->un.sli4_params);
11545 sli4_params->eq_pages_max = bf_get(eq_pages, &mqe->un.sli4_params);
11546 sli4_params->eqe_size = bf_get(eqe_size, &mqe->un.sli4_params);
11547 sli4_params->cq_pages_max = bf_get(cq_pages, &mqe->un.sli4_params);
11548 sli4_params->cqe_size = bf_get(cqe_size, &mqe->un.sli4_params);
11549 sli4_params->mq_pages_max = bf_get(mq_pages, &mqe->un.sli4_params);
11550 sli4_params->mqe_size = bf_get(mqe_size, &mqe->un.sli4_params);
11551 sli4_params->mq_elem_cnt = bf_get(mq_elem_cnt, &mqe->un.sli4_params);
11552 sli4_params->wq_pages_max = bf_get(wq_pages, &mqe->un.sli4_params);
11553 sli4_params->wqe_size = bf_get(wqe_size, &mqe->un.sli4_params);
11554 sli4_params->rq_pages_max = bf_get(rq_pages, &mqe->un.sli4_params);
11555 sli4_params->rqe_size = bf_get(rqe_size, &mqe->un.sli4_params);
11556 sli4_params->hdr_pages_max = bf_get(hdr_pages, &mqe->un.sli4_params);
11557 sli4_params->hdr_size = bf_get(hdr_size, &mqe->un.sli4_params);
11558 sli4_params->hdr_pp_align = bf_get(hdr_pp_align, &mqe->un.sli4_params);
11559 sli4_params->sgl_pages_max = bf_get(sgl_pages, &mqe->un.sli4_params);
11560 sli4_params->sgl_pp_align = bf_get(sgl_pp_align, &mqe->un.sli4_params);
James Smart05580562011-05-24 11:40:48 -040011561
11562 /* Make sure that sge_supp_len can be handled by the driver */
11563 if (sli4_params->sge_supp_len > LPFC_MAX_SGE_SIZE)
11564 sli4_params->sge_supp_len = LPFC_MAX_SGE_SIZE;
11565
James Smart28baac72010-02-12 14:42:03 -050011566 return rc;
11567}
11568
James Smartda0436e2009-05-22 14:51:39 -040011569/**
James Smartfedd3b72011-02-16 12:39:24 -050011570 * lpfc_get_sli4_parameters - Get the SLI4 Config PARAMETERS.
11571 * @phba: Pointer to HBA context object.
11572 * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
11573 *
11574 * This function is called in the SLI4 code path to read the port's
11575 * sli4 capabilities.
11576 *
11577 * This function may be be called from any context that can block-wait
11578 * for the completion. The expectation is that this routine is called
11579 * typically from probe_one or from the online routine.
11580 **/
11581int
11582lpfc_get_sli4_parameters(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
11583{
11584 int rc;
11585 struct lpfc_mqe *mqe = &mboxq->u.mqe;
11586 struct lpfc_pc_sli4_params *sli4_params;
James Smarta183a152011-10-10 21:32:43 -040011587 uint32_t mbox_tmo;
James Smartfedd3b72011-02-16 12:39:24 -050011588 int length;
James Smartbf316c72018-04-09 14:24:28 -070011589 bool exp_wqcq_pages = true;
James Smartfedd3b72011-02-16 12:39:24 -050011590 struct lpfc_sli4_parameters *mbx_sli4_parameters;
11591
James Smart6d368e52011-05-24 11:44:12 -040011592 /*
11593 * By default, the driver assumes the SLI4 port requires RPI
11594 * header postings. The SLI4_PARAM response will correct this
11595 * assumption.
11596 */
11597 phba->sli4_hba.rpi_hdrs_in_use = 1;
11598
James Smartfedd3b72011-02-16 12:39:24 -050011599 /* Read the port's SLI4 Config Parameters */
11600 length = (sizeof(struct lpfc_mbx_get_sli4_parameters) -
11601 sizeof(struct lpfc_sli4_cfg_mhdr));
11602 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
11603 LPFC_MBOX_OPCODE_GET_SLI4_PARAMETERS,
11604 length, LPFC_SLI4_MBX_EMBED);
11605 if (!phba->sli4_hba.intr_enable)
11606 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smarta183a152011-10-10 21:32:43 -040011607 else {
11608 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
11609 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
11610 }
James Smartfedd3b72011-02-16 12:39:24 -050011611 if (unlikely(rc))
11612 return rc;
11613 sli4_params = &phba->sli4_hba.pc_sli4_params;
11614 mbx_sli4_parameters = &mqe->un.get_sli4_parameters.sli4_parameters;
11615 sli4_params->if_type = bf_get(cfg_if_type, mbx_sli4_parameters);
11616 sli4_params->sli_rev = bf_get(cfg_sli_rev, mbx_sli4_parameters);
11617 sli4_params->sli_family = bf_get(cfg_sli_family, mbx_sli4_parameters);
11618 sli4_params->featurelevel_1 = bf_get(cfg_sli_hint_1,
11619 mbx_sli4_parameters);
11620 sli4_params->featurelevel_2 = bf_get(cfg_sli_hint_2,
11621 mbx_sli4_parameters);
11622 if (bf_get(cfg_phwq, mbx_sli4_parameters))
11623 phba->sli3_options |= LPFC_SLI4_PHWQ_ENABLED;
11624 else
11625 phba->sli3_options &= ~LPFC_SLI4_PHWQ_ENABLED;
11626 sli4_params->sge_supp_len = mbx_sli4_parameters->sge_supp_len;
11627 sli4_params->loopbk_scope = bf_get(loopbk_scope, mbx_sli4_parameters);
James Smart1ba981f2014-02-20 09:56:45 -050011628 sli4_params->oas_supported = bf_get(cfg_oas, mbx_sli4_parameters);
James Smartfedd3b72011-02-16 12:39:24 -050011629 sli4_params->cqv = bf_get(cfg_cqv, mbx_sli4_parameters);
11630 sli4_params->mqv = bf_get(cfg_mqv, mbx_sli4_parameters);
11631 sli4_params->wqv = bf_get(cfg_wqv, mbx_sli4_parameters);
11632 sli4_params->rqv = bf_get(cfg_rqv, mbx_sli4_parameters);
James Smart7365f6f2018-02-22 08:18:46 -080011633 sli4_params->eqav = bf_get(cfg_eqav, mbx_sli4_parameters);
11634 sli4_params->cqav = bf_get(cfg_cqav, mbx_sli4_parameters);
James Smart0c651872013-07-15 18:33:23 -040011635 sli4_params->wqsize = bf_get(cfg_wqsize, mbx_sli4_parameters);
James Smart66e9e6b2018-06-26 08:24:27 -070011636 sli4_params->bv1s = bf_get(cfg_bv1s, mbx_sli4_parameters);
James Smartfedd3b72011-02-16 12:39:24 -050011637 sli4_params->sgl_pages_max = bf_get(cfg_sgl_page_cnt,
11638 mbx_sli4_parameters);
James Smart895427b2017-02-12 13:52:30 -080011639 sli4_params->wqpcnt = bf_get(cfg_wqpcnt, mbx_sli4_parameters);
James Smartfedd3b72011-02-16 12:39:24 -050011640 sli4_params->sgl_pp_align = bf_get(cfg_sgl_pp_align,
11641 mbx_sli4_parameters);
James Smart6d368e52011-05-24 11:44:12 -040011642 phba->sli4_hba.extents_in_use = bf_get(cfg_ext, mbx_sli4_parameters);
11643 phba->sli4_hba.rpi_hdrs_in_use = bf_get(cfg_hdrr, mbx_sli4_parameters);
James Smart895427b2017-02-12 13:52:30 -080011644
James Smartc15e0702019-05-21 17:49:02 -070011645 /* Check for firmware nvme support */
11646 rc = (bf_get(cfg_nvme, mbx_sli4_parameters) &&
11647 bf_get(cfg_xib, mbx_sli4_parameters));
James Smart895427b2017-02-12 13:52:30 -080011648
James Smartc15e0702019-05-21 17:49:02 -070011649 if (rc) {
11650 /* Save this to indicate the Firmware supports NVME */
11651 sli4_params->nvme = 1;
11652
11653 /* Firmware NVME support, check driver FC4 NVME support */
11654 if (phba->cfg_enable_fc4_type == LPFC_ENABLE_FCP) {
11655 lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_NVME,
11656 "6133 Disabling NVME support: "
11657 "FC4 type not supported: x%x\n",
11658 phba->cfg_enable_fc4_type);
11659 goto fcponly;
11660 }
11661 } else {
11662 /* No firmware NVME support, check driver FC4 NVME support */
11663 sli4_params->nvme = 0;
11664 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
11665 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_NVME,
11666 "6101 Disabling NVME support: Not "
11667 "supported by firmware (%d %d) x%x\n",
11668 bf_get(cfg_nvme, mbx_sli4_parameters),
11669 bf_get(cfg_xib, mbx_sli4_parameters),
11670 phba->cfg_enable_fc4_type);
11671fcponly:
11672 phba->nvme_support = 0;
11673 phba->nvmet_support = 0;
11674 phba->cfg_nvmet_mrq = 0;
11675
11676 /* If no FC4 type support, move to just SCSI support */
11677 if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP))
11678 return -ENODEV;
11679 phba->cfg_enable_fc4_type = LPFC_ENABLE_FCP;
11680 }
James Smart895427b2017-02-12 13:52:30 -080011681 }
James Smart05580562011-05-24 11:40:48 -040011682
James Smartc26c2652019-08-14 16:56:49 -070011683 /* If the NVME FC4 type is enabled, scale the sg_seg_cnt to
11684 * accommodate 512K and 1M IOs in a single nvme buf and supply
11685 * enough NVME LS iocb buffers for larger connectivity counts.
11686 */
11687 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
11688 phba->cfg_sg_seg_cnt = LPFC_MAX_NVME_SEG_CNT;
11689 phba->cfg_iocb_cnt = 5;
11690 }
11691
James Smart414abe02018-06-26 08:24:26 -070011692 /* Only embed PBDE for if_type 6, PBDE support requires xib be set */
11693 if ((bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
11694 LPFC_SLI_INTF_IF_TYPE_6) || (!bf_get(cfg_xib, mbx_sli4_parameters)))
11695 phba->cfg_enable_pbde = 0;
James Smart0bc2b7c2018-02-22 08:18:48 -080011696
James Smart20aefac2018-01-30 15:58:58 -080011697 /*
11698 * To support Suppress Response feature we must satisfy 3 conditions.
11699 * lpfc_suppress_rsp module parameter must be set (default).
11700 * In SLI4-Parameters Descriptor:
11701 * Extended Inline Buffers (XIB) must be supported.
11702 * Suppress Response IU Not Supported (SRIUNS) must NOT be supported
11703 * (double negative).
11704 */
11705 if (phba->cfg_suppress_rsp && bf_get(cfg_xib, mbx_sli4_parameters) &&
11706 !(bf_get(cfg_nosr, mbx_sli4_parameters)))
James Smartf358dd02017-02-12 13:52:34 -080011707 phba->sli.sli_flag |= LPFC_SLI_SUPPRESS_RSP;
James Smart20aefac2018-01-30 15:58:58 -080011708 else
11709 phba->cfg_suppress_rsp = 0;
James Smartf358dd02017-02-12 13:52:34 -080011710
James Smart0cf07f842017-06-01 21:07:10 -070011711 if (bf_get(cfg_eqdr, mbx_sli4_parameters))
11712 phba->sli.sli_flag |= LPFC_SLI_USE_EQDR;
11713
James Smart05580562011-05-24 11:40:48 -040011714 /* Make sure that sge_supp_len can be handled by the driver */
11715 if (sli4_params->sge_supp_len > LPFC_MAX_SGE_SIZE)
11716 sli4_params->sge_supp_len = LPFC_MAX_SGE_SIZE;
11717
James Smartb5c53952016-03-31 14:12:30 -070011718 /*
James Smartc176ffa2018-01-30 15:58:46 -080011719 * Check whether the adapter supports an embedded copy of the
11720 * FCP CMD IU within the WQE for FCP_Ixxx commands. In order
11721 * to use this option, 128-byte WQEs must be used.
James Smartb5c53952016-03-31 14:12:30 -070011722 */
11723 if (bf_get(cfg_ext_embed_cb, mbx_sli4_parameters))
11724 phba->fcp_embed_io = 1;
11725 else
11726 phba->fcp_embed_io = 0;
James Smart7bdedb32016-07-06 12:36:00 -070011727
James Smart0bc2b7c2018-02-22 08:18:48 -080011728 lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_NVME,
James Smart414abe02018-06-26 08:24:26 -070011729 "6422 XIB %d PBDE %d: FCP %d NVME %d %d %d\n",
James Smart0bc2b7c2018-02-22 08:18:48 -080011730 bf_get(cfg_xib, mbx_sli4_parameters),
James Smart414abe02018-06-26 08:24:26 -070011731 phba->cfg_enable_pbde,
11732 phba->fcp_embed_io, phba->nvme_support,
James Smart4e565cf2018-02-22 08:18:50 -080011733 phba->cfg_nvme_embed_cmd, phba->cfg_suppress_rsp);
James Smart0bc2b7c2018-02-22 08:18:48 -080011734
James Smartbf316c72018-04-09 14:24:28 -070011735 if ((bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
11736 LPFC_SLI_INTF_IF_TYPE_2) &&
11737 (bf_get(lpfc_sli_intf_sli_family, &phba->sli4_hba.sli_intf) ==
James Smartc2217682018-05-24 21:09:00 -070011738 LPFC_SLI_INTF_FAMILY_LNCR_A0))
James Smartbf316c72018-04-09 14:24:28 -070011739 exp_wqcq_pages = false;
11740
James Smartc176ffa2018-01-30 15:58:46 -080011741 if ((bf_get(cfg_cqpsize, mbx_sli4_parameters) & LPFC_CQ_16K_PAGE_SZ) &&
11742 (bf_get(cfg_wqpsize, mbx_sli4_parameters) & LPFC_WQ_16K_PAGE_SZ) &&
James Smartbf316c72018-04-09 14:24:28 -070011743 exp_wqcq_pages &&
James Smartc176ffa2018-01-30 15:58:46 -080011744 (sli4_params->wqsize & LPFC_WQ_SZ128_SUPPORT))
11745 phba->enab_exp_wqcq_pages = 1;
11746 else
11747 phba->enab_exp_wqcq_pages = 0;
James Smart7bdedb32016-07-06 12:36:00 -070011748 /*
11749 * Check if the SLI port supports MDS Diagnostics
11750 */
11751 if (bf_get(cfg_mds_diags, mbx_sli4_parameters))
11752 phba->mds_diags_support = 1;
11753 else
11754 phba->mds_diags_support = 0;
James Smartd2cc9bc2018-09-10 10:30:50 -070011755
James Smartfedd3b72011-02-16 12:39:24 -050011756 return 0;
11757}
11758
11759/**
James Smart3772a992009-05-22 14:50:54 -040011760 * lpfc_pci_probe_one_s3 - PCI probe func to reg SLI-3 device to PCI subsystem.
11761 * @pdev: pointer to PCI device
11762 * @pid: pointer to PCI device identifier
11763 *
11764 * This routine is to be called to attach a device with SLI-3 interface spec
11765 * to the PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
11766 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
11767 * information of the device and driver to see if the driver state that it can
11768 * support this kind of device. If the match is successful, the driver core
11769 * invokes this routine. If this routine determines it can claim the HBA, it
11770 * does all the initialization that it needs to do to handle the HBA properly.
11771 *
11772 * Return code
11773 * 0 - driver can claim the device
11774 * negative value - driver can not claim the device
11775 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080011776static int
James Smart3772a992009-05-22 14:50:54 -040011777lpfc_pci_probe_one_s3(struct pci_dev *pdev, const struct pci_device_id *pid)
11778{
11779 struct lpfc_hba *phba;
11780 struct lpfc_vport *vport = NULL;
James Smart6669f9b2009-10-02 15:16:45 -040011781 struct Scsi_Host *shost = NULL;
James Smart3772a992009-05-22 14:50:54 -040011782 int error;
11783 uint32_t cfg_mode, intr_mode;
11784
11785 /* Allocate memory for HBA structure */
11786 phba = lpfc_hba_alloc(pdev);
11787 if (!phba)
11788 return -ENOMEM;
11789
11790 /* Perform generic PCI device enabling operation */
11791 error = lpfc_enable_pci_dev(phba);
James Smart079b5c92011-08-21 21:48:49 -040011792 if (error)
James Smart3772a992009-05-22 14:50:54 -040011793 goto out_free_phba;
James Smart3772a992009-05-22 14:50:54 -040011794
11795 /* Set up SLI API function jump table for PCI-device group-0 HBAs */
11796 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_LP);
11797 if (error)
11798 goto out_disable_pci_dev;
11799
11800 /* Set up SLI-3 specific device PCI memory space */
11801 error = lpfc_sli_pci_mem_setup(phba);
11802 if (error) {
11803 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11804 "1402 Failed to set up pci memory space.\n");
11805 goto out_disable_pci_dev;
11806 }
11807
James Smart3772a992009-05-22 14:50:54 -040011808 /* Set up SLI-3 specific device driver resources */
11809 error = lpfc_sli_driver_resource_setup(phba);
11810 if (error) {
11811 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11812 "1404 Failed to set up driver resource.\n");
11813 goto out_unset_pci_mem_s3;
11814 }
11815
11816 /* Initialize and populate the iocb list per host */
James Smartd1f525a2017-04-21 16:04:55 -070011817
James Smart3772a992009-05-22 14:50:54 -040011818 error = lpfc_init_iocb_list(phba, LPFC_IOCB_LIST_CNT);
11819 if (error) {
11820 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11821 "1405 Failed to initialize iocb list.\n");
11822 goto out_unset_driver_resource_s3;
11823 }
11824
11825 /* Set up common device driver resources */
11826 error = lpfc_setup_driver_resource_phase2(phba);
11827 if (error) {
11828 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11829 "1406 Failed to set up driver resource.\n");
11830 goto out_free_iocb_list;
11831 }
11832
James Smart079b5c92011-08-21 21:48:49 -040011833 /* Get the default values for Model Name and Description */
11834 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
11835
James Smart3772a992009-05-22 14:50:54 -040011836 /* Create SCSI host to the physical port */
11837 error = lpfc_create_shost(phba);
11838 if (error) {
11839 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11840 "1407 Failed to create scsi host.\n");
11841 goto out_unset_driver_resource;
11842 }
11843
11844 /* Configure sysfs attributes */
11845 vport = phba->pport;
11846 error = lpfc_alloc_sysfs_attr(vport);
11847 if (error) {
11848 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11849 "1476 Failed to allocate sysfs attr\n");
11850 goto out_destroy_shost;
11851 }
11852
James Smart6669f9b2009-10-02 15:16:45 -040011853 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
James Smart3772a992009-05-22 14:50:54 -040011854 /* Now, trying to enable interrupt and bring up the device */
11855 cfg_mode = phba->cfg_use_msi;
11856 while (true) {
11857 /* Put device to a known state before enabling interrupt */
11858 lpfc_stop_port(phba);
11859 /* Configure and enable interrupt */
11860 intr_mode = lpfc_sli_enable_intr(phba, cfg_mode);
11861 if (intr_mode == LPFC_INTR_ERROR) {
11862 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11863 "0431 Failed to enable interrupt.\n");
11864 error = -ENODEV;
11865 goto out_free_sysfs_attr;
11866 }
11867 /* SLI-3 HBA setup */
11868 if (lpfc_sli_hba_setup(phba)) {
11869 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11870 "1477 Failed to set up hba\n");
11871 error = -ENODEV;
11872 goto out_remove_device;
11873 }
11874
11875 /* Wait 50ms for the interrupts of previous mailbox commands */
11876 msleep(50);
11877 /* Check active interrupts on message signaled interrupts */
11878 if (intr_mode == 0 ||
11879 phba->sli.slistat.sli_intr > LPFC_MSIX_VECTORS) {
11880 /* Log the current active interrupt mode */
11881 phba->intr_mode = intr_mode;
11882 lpfc_log_intr_mode(phba, intr_mode);
11883 break;
11884 } else {
11885 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
11886 "0447 Configure interrupt mode (%d) "
11887 "failed active interrupt test.\n",
11888 intr_mode);
11889 /* Disable the current interrupt mode */
11890 lpfc_sli_disable_intr(phba);
11891 /* Try next level of interrupt mode */
11892 cfg_mode = --intr_mode;
11893 }
11894 }
11895
11896 /* Perform post initialization setup */
11897 lpfc_post_init_setup(phba);
11898
11899 /* Check if there are static vports to be created. */
11900 lpfc_create_static_vport(phba);
11901
11902 return 0;
11903
11904out_remove_device:
11905 lpfc_unset_hba(phba);
11906out_free_sysfs_attr:
11907 lpfc_free_sysfs_attr(vport);
11908out_destroy_shost:
11909 lpfc_destroy_shost(phba);
11910out_unset_driver_resource:
11911 lpfc_unset_driver_resource_phase2(phba);
11912out_free_iocb_list:
11913 lpfc_free_iocb_list(phba);
11914out_unset_driver_resource_s3:
11915 lpfc_sli_driver_resource_unset(phba);
11916out_unset_pci_mem_s3:
11917 lpfc_sli_pci_mem_unset(phba);
11918out_disable_pci_dev:
11919 lpfc_disable_pci_dev(phba);
James Smart6669f9b2009-10-02 15:16:45 -040011920 if (shost)
11921 scsi_host_put(shost);
James Smart3772a992009-05-22 14:50:54 -040011922out_free_phba:
11923 lpfc_hba_free(phba);
11924 return error;
11925}
11926
11927/**
11928 * lpfc_pci_remove_one_s3 - PCI func to unreg SLI-3 device from PCI subsystem.
James Smarte59058c2008-08-24 21:49:00 -040011929 * @pdev: pointer to PCI device
11930 *
James Smart3772a992009-05-22 14:50:54 -040011931 * This routine is to be called to disattach a device with SLI-3 interface
11932 * spec from PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
11933 * removed from PCI bus, it performs all the necessary cleanup for the HBA
11934 * device to be removed from the PCI subsystem properly.
James Smarte59058c2008-08-24 21:49:00 -040011935 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080011936static void
James Smart3772a992009-05-22 14:50:54 -040011937lpfc_pci_remove_one_s3(struct pci_dev *pdev)
dea31012005-04-17 16:05:31 -050011938{
James Smart2e0fef82007-06-17 19:56:36 -050011939 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11940 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
James Smarteada2722008-12-04 22:39:13 -050011941 struct lpfc_vport **vports;
James Smart2e0fef82007-06-17 19:56:36 -050011942 struct lpfc_hba *phba = vport->phba;
James Smarteada2722008-12-04 22:39:13 -050011943 int i;
Tomohiro Kusumi8a4df1202008-01-11 01:53:00 -050011944
James Smart549e55c2007-08-02 11:09:51 -040011945 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -040011946 vport->load_flag |= FC_UNLOADING;
James Smart549e55c2007-08-02 11:09:51 -040011947 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050011948
James Smart858c9f62007-06-17 19:56:39 -050011949 lpfc_free_sysfs_attr(vport);
11950
James Smarteada2722008-12-04 22:39:13 -050011951 /* Release all the vports against this physical port */
11952 vports = lpfc_create_vport_work_array(phba);
11953 if (vports != NULL)
James Smart587a37f2012-05-09 21:16:03 -040011954 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
11955 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
11956 continue;
James Smarteada2722008-12-04 22:39:13 -050011957 fc_vport_terminate(vports[i]->fc_vport);
James Smart587a37f2012-05-09 21:16:03 -040011958 }
James Smarteada2722008-12-04 22:39:13 -050011959 lpfc_destroy_vport_work_array(phba, vports);
11960
11961 /* Remove FC host and then SCSI host with the physical port */
James Smart858c9f62007-06-17 19:56:39 -050011962 fc_remove_host(shost);
11963 scsi_remove_host(shost);
James Smartd613b6a2017-02-12 13:52:37 -080011964
James Smart87af33f2007-10-27 13:37:43 -040011965 lpfc_cleanup(vport);
11966
James Smart2e0fef82007-06-17 19:56:36 -050011967 /*
11968 * Bring down the SLI Layer. This step disable all interrupts,
11969 * clears the rings, discards all mailbox commands, and resets
11970 * the HBA.
11971 */
James Smarta257bf92009-04-06 18:48:10 -040011972
Justin P. Mattock48e34d02010-12-30 15:07:58 -080011973 /* HBA interrupt will be disabled after this call */
James Smart2e0fef82007-06-17 19:56:36 -050011974 lpfc_sli_hba_down(phba);
James Smarta257bf92009-04-06 18:48:10 -040011975 /* Stop kthread signal shall trigger work_done one more time */
11976 kthread_stop(phba->worker_thread);
11977 /* Final cleanup of txcmplq and reset the HBA */
James Smart2e0fef82007-06-17 19:56:36 -050011978 lpfc_sli_brdrestart(phba);
11979
James Smart72859902012-01-18 16:25:38 -050011980 kfree(phba->vpi_bmask);
11981 kfree(phba->vpi_ids);
11982
James Smart3772a992009-05-22 14:50:54 -040011983 lpfc_stop_hba_timers(phba);
James Smart523128e2018-09-10 10:30:46 -070011984 spin_lock_irq(&phba->port_list_lock);
James Smart858c9f62007-06-17 19:56:39 -050011985 list_del_init(&vport->listentry);
James Smart523128e2018-09-10 10:30:46 -070011986 spin_unlock_irq(&phba->port_list_lock);
James Smart858c9f62007-06-17 19:56:39 -050011987
James Smart858c9f62007-06-17 19:56:39 -050011988 lpfc_debugfs_terminate(vport);
James Smart2e0fef82007-06-17 19:56:36 -050011989
James Smart912e3ac2011-05-24 11:42:11 -040011990 /* Disable SR-IOV if enabled */
11991 if (phba->cfg_sriov_nr_virtfn)
11992 pci_disable_sriov(pdev);
11993
James Smart5b75da22008-12-04 22:39:35 -050011994 /* Disable interrupt */
James Smart3772a992009-05-22 14:50:54 -040011995 lpfc_sli_disable_intr(phba);
dea31012005-04-17 16:05:31 -050011996
James Smart858c9f62007-06-17 19:56:39 -050011997 scsi_host_put(shost);
James Smart2e0fef82007-06-17 19:56:36 -050011998
11999 /*
12000 * Call scsi_free before mem_free since scsi bufs are released to their
12001 * corresponding pools here.
12002 */
12003 lpfc_scsi_free(phba);
James Smart0794d602019-01-28 11:14:19 -080012004 lpfc_free_iocb_list(phba);
12005
James Smart3772a992009-05-22 14:50:54 -040012006 lpfc_mem_free_all(phba);
James Smart2e0fef82007-06-17 19:56:36 -050012007
James Smart34b02dc2008-08-24 21:49:55 -040012008 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
12009 phba->hbqslimp.virt, phba->hbqslimp.phys);
James Smarted957682007-06-17 19:56:37 -050012010
James Smart2e0fef82007-06-17 19:56:36 -050012011 /* Free resources associated with SLI2 interface */
12012 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
James Smart34b02dc2008-08-24 21:49:55 -040012013 phba->slim2p.virt, phba->slim2p.phys);
James Smart2e0fef82007-06-17 19:56:36 -050012014
12015 /* unmap adapter SLIM and Control Registers */
12016 iounmap(phba->ctrl_regs_memmap_p);
12017 iounmap(phba->slim_memmap_p);
12018
James Smart3772a992009-05-22 14:50:54 -040012019 lpfc_hba_free(phba);
James Smart2e0fef82007-06-17 19:56:36 -050012020
Johannes Thumshirne0c04832016-06-07 09:44:03 +020012021 pci_release_mem_regions(pdev);
James Smart2e0fef82007-06-17 19:56:36 -050012022 pci_disable_device(pdev);
dea31012005-04-17 16:05:31 -050012023}
12024
Linas Vepstas8d63f372007-02-14 14:28:36 -060012025/**
James Smart3772a992009-05-22 14:50:54 -040012026 * lpfc_pci_suspend_one_s3 - PCI func to suspend SLI-3 device for power mgmnt
James Smart3a55b532008-12-04 22:38:54 -050012027 * @pdev: pointer to PCI device
12028 * @msg: power management message
12029 *
James Smart3772a992009-05-22 14:50:54 -040012030 * This routine is to be called from the kernel's PCI subsystem to support
12031 * system Power Management (PM) to device with SLI-3 interface spec. When
12032 * PM invokes this method, it quiesces the device by stopping the driver's
12033 * worker thread for the device, turning off device's interrupt and DMA,
12034 * and bring the device offline. Note that as the driver implements the
12035 * minimum PM requirements to a power-aware driver's PM support for the
12036 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
12037 * to the suspend() method call will be treated as SUSPEND and the driver will
12038 * fully reinitialize its device during resume() method call, the driver will
12039 * set device to PCI_D3hot state in PCI config space instead of setting it
12040 * according to the @msg provided by the PM.
James Smart3a55b532008-12-04 22:38:54 -050012041 *
12042 * Return code
James Smart3772a992009-05-22 14:50:54 -040012043 * 0 - driver suspended the device
12044 * Error otherwise
James Smart3a55b532008-12-04 22:38:54 -050012045 **/
12046static int
James Smart3772a992009-05-22 14:50:54 -040012047lpfc_pci_suspend_one_s3(struct pci_dev *pdev, pm_message_t msg)
James Smart3a55b532008-12-04 22:38:54 -050012048{
12049 struct Scsi_Host *shost = pci_get_drvdata(pdev);
12050 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
12051
12052 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
12053 "0473 PCI device Power Management suspend.\n");
12054
12055 /* Bring down the device */
James Smart618a5232012-06-12 13:54:36 -040012056 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart3a55b532008-12-04 22:38:54 -050012057 lpfc_offline(phba);
12058 kthread_stop(phba->worker_thread);
12059
12060 /* Disable interrupt from device */
James Smart3772a992009-05-22 14:50:54 -040012061 lpfc_sli_disable_intr(phba);
James Smart3a55b532008-12-04 22:38:54 -050012062
12063 /* Save device state to PCI config space */
12064 pci_save_state(pdev);
12065 pci_set_power_state(pdev, PCI_D3hot);
12066
12067 return 0;
12068}
12069
12070/**
James Smart3772a992009-05-22 14:50:54 -040012071 * lpfc_pci_resume_one_s3 - PCI func to resume SLI-3 device for power mgmnt
James Smart3a55b532008-12-04 22:38:54 -050012072 * @pdev: pointer to PCI device
12073 *
James Smart3772a992009-05-22 14:50:54 -040012074 * This routine is to be called from the kernel's PCI subsystem to support
12075 * system Power Management (PM) to device with SLI-3 interface spec. When PM
12076 * invokes this method, it restores the device's PCI config space state and
12077 * fully reinitializes the device and brings it online. Note that as the
12078 * driver implements the minimum PM requirements to a power-aware driver's
12079 * PM for suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE,
12080 * FREEZE) to the suspend() method call will be treated as SUSPEND and the
12081 * driver will fully reinitialize its device during resume() method call,
12082 * the device will be set to PCI_D0 directly in PCI config space before
12083 * restoring the state.
James Smart3a55b532008-12-04 22:38:54 -050012084 *
12085 * Return code
James Smart3772a992009-05-22 14:50:54 -040012086 * 0 - driver suspended the device
12087 * Error otherwise
James Smart3a55b532008-12-04 22:38:54 -050012088 **/
12089static int
James Smart3772a992009-05-22 14:50:54 -040012090lpfc_pci_resume_one_s3(struct pci_dev *pdev)
James Smart3a55b532008-12-04 22:38:54 -050012091{
12092 struct Scsi_Host *shost = pci_get_drvdata(pdev);
12093 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
James Smart5b75da22008-12-04 22:39:35 -050012094 uint32_t intr_mode;
James Smart3a55b532008-12-04 22:38:54 -050012095 int error;
12096
12097 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
12098 "0452 PCI device Power Management resume.\n");
12099
12100 /* Restore device state from PCI config space */
12101 pci_set_power_state(pdev, PCI_D0);
12102 pci_restore_state(pdev);
James Smart0d878412009-10-02 15:16:56 -040012103
James Smart1dfb5a42010-02-12 14:40:50 -050012104 /*
12105 * As the new kernel behavior of pci_restore_state() API call clears
12106 * device saved_state flag, need to save the restored state again.
12107 */
12108 pci_save_state(pdev);
12109
James Smart3a55b532008-12-04 22:38:54 -050012110 if (pdev->is_busmaster)
12111 pci_set_master(pdev);
12112
12113 /* Startup the kernel thread for this host adapter. */
12114 phba->worker_thread = kthread_run(lpfc_do_work, phba,
12115 "lpfc_worker_%d", phba->brd_no);
12116 if (IS_ERR(phba->worker_thread)) {
12117 error = PTR_ERR(phba->worker_thread);
12118 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12119 "0434 PM resume failed to start worker "
12120 "thread: error=x%x.\n", error);
12121 return error;
12122 }
12123
James Smart5b75da22008-12-04 22:39:35 -050012124 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -040012125 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -050012126 if (intr_mode == LPFC_INTR_ERROR) {
James Smart3a55b532008-12-04 22:38:54 -050012127 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart5b75da22008-12-04 22:39:35 -050012128 "0430 PM resume Failed to enable interrupt\n");
12129 return -EIO;
12130 } else
12131 phba->intr_mode = intr_mode;
James Smart3a55b532008-12-04 22:38:54 -050012132
12133 /* Restart HBA and bring it online */
12134 lpfc_sli_brdrestart(phba);
12135 lpfc_online(phba);
12136
James Smart5b75da22008-12-04 22:39:35 -050012137 /* Log the current active interrupt mode */
12138 lpfc_log_intr_mode(phba, phba->intr_mode);
12139
James Smart3a55b532008-12-04 22:38:54 -050012140 return 0;
12141}
12142
12143/**
James Smart891478a2009-11-18 15:40:23 -050012144 * lpfc_sli_prep_dev_for_recover - Prepare SLI3 device for pci slot recover
12145 * @phba: pointer to lpfc hba data structure.
12146 *
12147 * This routine is called to prepare the SLI3 device for PCI slot recover. It
James Smarte2af0d22010-03-15 11:25:32 -040012148 * aborts all the outstanding SCSI I/Os to the pci device.
James Smart891478a2009-11-18 15:40:23 -050012149 **/
12150static void
12151lpfc_sli_prep_dev_for_recover(struct lpfc_hba *phba)
12152{
12153 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12154 "2723 PCI channel I/O abort preparing for recovery\n");
James Smarte2af0d22010-03-15 11:25:32 -040012155
12156 /*
12157 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
12158 * and let the SCSI mid-layer to retry them to recover.
12159 */
James Smartdb55fba2014-04-04 13:52:02 -040012160 lpfc_sli_abort_fcp_rings(phba);
James Smart891478a2009-11-18 15:40:23 -050012161}
12162
12163/**
James Smart0d878412009-10-02 15:16:56 -040012164 * lpfc_sli_prep_dev_for_reset - Prepare SLI3 device for pci slot reset
12165 * @phba: pointer to lpfc hba data structure.
12166 *
12167 * This routine is called to prepare the SLI3 device for PCI slot reset. It
12168 * disables the device interrupt and pci device, and aborts the internal FCP
12169 * pending I/Os.
12170 **/
12171static void
12172lpfc_sli_prep_dev_for_reset(struct lpfc_hba *phba)
12173{
James Smart0d878412009-10-02 15:16:56 -040012174 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart891478a2009-11-18 15:40:23 -050012175 "2710 PCI channel disable preparing for reset\n");
James Smarte2af0d22010-03-15 11:25:32 -040012176
James Smart75baf692010-06-08 18:31:21 -040012177 /* Block any management I/Os to the device */
James Smart618a5232012-06-12 13:54:36 -040012178 lpfc_block_mgmt_io(phba, LPFC_MBX_WAIT);
James Smart75baf692010-06-08 18:31:21 -040012179
James Smarte2af0d22010-03-15 11:25:32 -040012180 /* Block all SCSI devices' I/Os on the host */
12181 lpfc_scsi_dev_block(phba);
12182
James Smartea714f32013-04-17 20:18:47 -040012183 /* Flush all driver's outstanding SCSI I/Os as we are to reset */
12184 lpfc_sli_flush_fcp_rings(phba);
12185
James Smarte2af0d22010-03-15 11:25:32 -040012186 /* stop all timers */
12187 lpfc_stop_hba_timers(phba);
12188
James Smart0d878412009-10-02 15:16:56 -040012189 /* Disable interrupt and pci device */
12190 lpfc_sli_disable_intr(phba);
12191 pci_disable_device(phba->pcidev);
James Smart0d878412009-10-02 15:16:56 -040012192}
12193
12194/**
12195 * lpfc_sli_prep_dev_for_perm_failure - Prepare SLI3 dev for pci slot disable
12196 * @phba: pointer to lpfc hba data structure.
12197 *
12198 * This routine is called to prepare the SLI3 device for PCI slot permanently
12199 * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
12200 * pending I/Os.
12201 **/
12202static void
James Smart75baf692010-06-08 18:31:21 -040012203lpfc_sli_prep_dev_for_perm_failure(struct lpfc_hba *phba)
James Smart0d878412009-10-02 15:16:56 -040012204{
12205 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart891478a2009-11-18 15:40:23 -050012206 "2711 PCI channel permanent disable for failure\n");
James Smarte2af0d22010-03-15 11:25:32 -040012207 /* Block all SCSI devices' I/Os on the host */
12208 lpfc_scsi_dev_block(phba);
12209
12210 /* stop all timers */
12211 lpfc_stop_hba_timers(phba);
12212
James Smart0d878412009-10-02 15:16:56 -040012213 /* Clean up all driver's outstanding SCSI I/Os */
12214 lpfc_sli_flush_fcp_rings(phba);
12215}
12216
12217/**
James Smart3772a992009-05-22 14:50:54 -040012218 * lpfc_io_error_detected_s3 - Method for handling SLI-3 device PCI I/O error
James Smarte59058c2008-08-24 21:49:00 -040012219 * @pdev: pointer to PCI device.
12220 * @state: the current PCI connection state.
Linas Vepstas8d63f372007-02-14 14:28:36 -060012221 *
James Smart3772a992009-05-22 14:50:54 -040012222 * This routine is called from the PCI subsystem for I/O error handling to
12223 * device with SLI-3 interface spec. This function is called by the PCI
12224 * subsystem after a PCI bus error affecting this device has been detected.
12225 * When this function is invoked, it will need to stop all the I/Os and
12226 * interrupt(s) to the device. Once that is done, it will return
12227 * PCI_ERS_RESULT_NEED_RESET for the PCI subsystem to perform proper recovery
12228 * as desired.
James Smarte59058c2008-08-24 21:49:00 -040012229 *
12230 * Return codes
James Smart0d878412009-10-02 15:16:56 -040012231 * PCI_ERS_RESULT_CAN_RECOVER - can be recovered with reset_link
James Smart3772a992009-05-22 14:50:54 -040012232 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
12233 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
James Smarte59058c2008-08-24 21:49:00 -040012234 **/
James Smart3772a992009-05-22 14:50:54 -040012235static pci_ers_result_t
12236lpfc_io_error_detected_s3(struct pci_dev *pdev, pci_channel_state_t state)
Linas Vepstas8d63f372007-02-14 14:28:36 -060012237{
James Smart51ef4c22007-08-02 11:10:31 -040012238 struct Scsi_Host *shost = pci_get_drvdata(pdev);
12239 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -060012240
James Smart0d878412009-10-02 15:16:56 -040012241 switch (state) {
12242 case pci_channel_io_normal:
James Smart891478a2009-11-18 15:40:23 -050012243 /* Non-fatal error, prepare for recovery */
12244 lpfc_sli_prep_dev_for_recover(phba);
James Smart0d878412009-10-02 15:16:56 -040012245 return PCI_ERS_RESULT_CAN_RECOVER;
12246 case pci_channel_io_frozen:
12247 /* Fatal error, prepare for slot reset */
12248 lpfc_sli_prep_dev_for_reset(phba);
12249 return PCI_ERS_RESULT_NEED_RESET;
12250 case pci_channel_io_perm_failure:
12251 /* Permanent failure, prepare for device down */
James Smart75baf692010-06-08 18:31:21 -040012252 lpfc_sli_prep_dev_for_perm_failure(phba);
Linas Vepstas8d63f372007-02-14 14:28:36 -060012253 return PCI_ERS_RESULT_DISCONNECT;
James Smart0d878412009-10-02 15:16:56 -040012254 default:
12255 /* Unknown state, prepare and request slot reset */
12256 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12257 "0472 Unknown PCI error state: x%x\n", state);
12258 lpfc_sli_prep_dev_for_reset(phba);
12259 return PCI_ERS_RESULT_NEED_RESET;
James Smarta8e497d2008-08-24 21:50:11 -040012260 }
Linas Vepstas8d63f372007-02-14 14:28:36 -060012261}
12262
12263/**
James Smart3772a992009-05-22 14:50:54 -040012264 * lpfc_io_slot_reset_s3 - Method for restarting PCI SLI-3 device from scratch.
James Smarte59058c2008-08-24 21:49:00 -040012265 * @pdev: pointer to PCI device.
Linas Vepstas8d63f372007-02-14 14:28:36 -060012266 *
James Smart3772a992009-05-22 14:50:54 -040012267 * This routine is called from the PCI subsystem for error handling to
12268 * device with SLI-3 interface spec. This is called after PCI bus has been
12269 * reset to restart the PCI card from scratch, as if from a cold-boot.
12270 * During the PCI subsystem error recovery, after driver returns
12271 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
12272 * recovery and then call this routine before calling the .resume method
12273 * to recover the device. This function will initialize the HBA device,
12274 * enable the interrupt, but it will just put the HBA to offline state
12275 * without passing any I/O traffic.
James Smarte59058c2008-08-24 21:49:00 -040012276 *
12277 * Return codes
James Smart3772a992009-05-22 14:50:54 -040012278 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
12279 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
Linas Vepstas8d63f372007-02-14 14:28:36 -060012280 */
James Smart3772a992009-05-22 14:50:54 -040012281static pci_ers_result_t
12282lpfc_io_slot_reset_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -060012283{
James Smart51ef4c22007-08-02 11:10:31 -040012284 struct Scsi_Host *shost = pci_get_drvdata(pdev);
12285 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -060012286 struct lpfc_sli *psli = &phba->sli;
James Smart5b75da22008-12-04 22:39:35 -050012287 uint32_t intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -060012288
12289 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
Benjamin Herrenschmidt09483912007-12-20 15:28:09 +110012290 if (pci_enable_device_mem(pdev)) {
Linas Vepstas8d63f372007-02-14 14:28:36 -060012291 printk(KERN_ERR "lpfc: Cannot re-enable "
12292 "PCI device after reset.\n");
12293 return PCI_ERS_RESULT_DISCONNECT;
12294 }
12295
James Smart97207482008-12-04 22:39:19 -050012296 pci_restore_state(pdev);
James Smart1dfb5a42010-02-12 14:40:50 -050012297
12298 /*
12299 * As the new kernel behavior of pci_restore_state() API call clears
12300 * device saved_state flag, need to save the restored state again.
12301 */
12302 pci_save_state(pdev);
12303
James Smart97207482008-12-04 22:39:19 -050012304 if (pdev->is_busmaster)
12305 pci_set_master(pdev);
Linas Vepstas8d63f372007-02-14 14:28:36 -060012306
James Smart92d7f7b2007-06-17 19:56:38 -050012307 spin_lock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040012308 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -050012309 spin_unlock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -060012310
James Smart5b75da22008-12-04 22:39:35 -050012311 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -040012312 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -050012313 if (intr_mode == LPFC_INTR_ERROR) {
12314 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12315 "0427 Cannot re-enable interrupt after "
12316 "slot reset.\n");
12317 return PCI_ERS_RESULT_DISCONNECT;
12318 } else
12319 phba->intr_mode = intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -060012320
James Smart75baf692010-06-08 18:31:21 -040012321 /* Take device offline, it will perform cleanup */
James Smart618a5232012-06-12 13:54:36 -040012322 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
Linas Vepstas8d63f372007-02-14 14:28:36 -060012323 lpfc_offline(phba);
12324 lpfc_sli_brdrestart(phba);
12325
James Smart5b75da22008-12-04 22:39:35 -050012326 /* Log the current active interrupt mode */
12327 lpfc_log_intr_mode(phba, phba->intr_mode);
12328
Linas Vepstas8d63f372007-02-14 14:28:36 -060012329 return PCI_ERS_RESULT_RECOVERED;
12330}
12331
12332/**
James Smart3772a992009-05-22 14:50:54 -040012333 * lpfc_io_resume_s3 - Method for resuming PCI I/O operation on SLI-3 device.
James Smarte59058c2008-08-24 21:49:00 -040012334 * @pdev: pointer to PCI device
Linas Vepstas8d63f372007-02-14 14:28:36 -060012335 *
James Smart3772a992009-05-22 14:50:54 -040012336 * This routine is called from the PCI subsystem for error handling to device
12337 * with SLI-3 interface spec. It is called when kernel error recovery tells
12338 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
12339 * error recovery. After this call, traffic can start to flow from this device
12340 * again.
Linas Vepstas8d63f372007-02-14 14:28:36 -060012341 */
James Smart3772a992009-05-22 14:50:54 -040012342static void
12343lpfc_io_resume_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -060012344{
James Smart51ef4c22007-08-02 11:10:31 -040012345 struct Scsi_Host *shost = pci_get_drvdata(pdev);
12346 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -060012347
James Smarte2af0d22010-03-15 11:25:32 -040012348 /* Bring device online, it will be no-op for non-fatal error resume */
James Smart58da1ff2008-04-07 10:15:56 -040012349 lpfc_online(phba);
Linas Vepstas8d63f372007-02-14 14:28:36 -060012350}
12351
James Smart3772a992009-05-22 14:50:54 -040012352/**
James Smartda0436e2009-05-22 14:51:39 -040012353 * lpfc_sli4_get_els_iocb_cnt - Calculate the # of ELS IOCBs to reserve
12354 * @phba: pointer to lpfc hba data structure.
12355 *
12356 * returns the number of ELS/CT IOCBs to reserve
12357 **/
12358int
12359lpfc_sli4_get_els_iocb_cnt(struct lpfc_hba *phba)
12360{
12361 int max_xri = phba->sli4_hba.max_cfg_param.max_xri;
12362
James Smartf1126682009-06-10 17:22:44 -040012363 if (phba->sli_rev == LPFC_SLI_REV4) {
12364 if (max_xri <= 100)
James Smart6a9c52c2009-10-02 15:16:51 -040012365 return 10;
James Smartf1126682009-06-10 17:22:44 -040012366 else if (max_xri <= 256)
James Smart6a9c52c2009-10-02 15:16:51 -040012367 return 25;
James Smartf1126682009-06-10 17:22:44 -040012368 else if (max_xri <= 512)
James Smart6a9c52c2009-10-02 15:16:51 -040012369 return 50;
James Smartf1126682009-06-10 17:22:44 -040012370 else if (max_xri <= 1024)
James Smart6a9c52c2009-10-02 15:16:51 -040012371 return 100;
James Smart8a9d2e82012-05-09 21:16:12 -040012372 else if (max_xri <= 1536)
James Smart6a9c52c2009-10-02 15:16:51 -040012373 return 150;
James Smart8a9d2e82012-05-09 21:16:12 -040012374 else if (max_xri <= 2048)
12375 return 200;
12376 else
12377 return 250;
James Smartf1126682009-06-10 17:22:44 -040012378 } else
12379 return 0;
James Smartda0436e2009-05-22 14:51:39 -040012380}
12381
12382/**
James Smart895427b2017-02-12 13:52:30 -080012383 * lpfc_sli4_get_iocb_cnt - Calculate the # of total IOCBs to reserve
12384 * @phba: pointer to lpfc hba data structure.
12385 *
James Smartf358dd02017-02-12 13:52:34 -080012386 * returns the number of ELS/CT + NVMET IOCBs to reserve
James Smart895427b2017-02-12 13:52:30 -080012387 **/
12388int
12389lpfc_sli4_get_iocb_cnt(struct lpfc_hba *phba)
12390{
12391 int max_xri = lpfc_sli4_get_els_iocb_cnt(phba);
12392
James Smartf358dd02017-02-12 13:52:34 -080012393 if (phba->nvmet_support)
12394 max_xri += LPFC_NVMET_BUF_POST;
James Smart895427b2017-02-12 13:52:30 -080012395 return max_xri;
12396}
12397
12398
James Smart1feb8202018-02-22 08:18:47 -080012399static void
12400lpfc_log_write_firmware_error(struct lpfc_hba *phba, uint32_t offset,
12401 uint32_t magic_number, uint32_t ftype, uint32_t fid, uint32_t fsize,
12402 const struct firmware *fw)
12403{
James Smarta72d56b2018-05-04 20:37:52 -070012404 if ((offset == ADD_STATUS_FW_NOT_SUPPORTED) ||
12405 (phba->pcidev->device == PCI_DEVICE_ID_LANCER_G6_FC &&
12406 magic_number != MAGIC_NUMER_G6) ||
12407 (phba->pcidev->device == PCI_DEVICE_ID_LANCER_G7_FC &&
12408 magic_number != MAGIC_NUMER_G7))
James Smart1feb8202018-02-22 08:18:47 -080012409 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12410 "3030 This firmware version is not supported on "
12411 "this HBA model. Device:%x Magic:%x Type:%x "
12412 "ID:%x Size %d %zd\n",
12413 phba->pcidev->device, magic_number, ftype, fid,
12414 fsize, fw->size);
12415 else
12416 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12417 "3022 FW Download failed. Device:%x Magic:%x Type:%x "
12418 "ID:%x Size %d %zd\n",
12419 phba->pcidev->device, magic_number, ftype, fid,
12420 fsize, fw->size);
12421}
12422
12423
James Smart895427b2017-02-12 13:52:30 -080012424/**
James Smart52d52442011-05-24 11:42:45 -040012425 * lpfc_write_firmware - attempt to write a firmware image to the port
James Smart52d52442011-05-24 11:42:45 -040012426 * @fw: pointer to firmware image returned from request_firmware.
James Smartce396282012-09-29 11:30:56 -040012427 * @phba: pointer to lpfc hba data structure.
James Smart52d52442011-05-24 11:42:45 -040012428 *
James Smart52d52442011-05-24 11:42:45 -040012429 **/
James Smartce396282012-09-29 11:30:56 -040012430static void
12431lpfc_write_firmware(const struct firmware *fw, void *context)
James Smart52d52442011-05-24 11:42:45 -040012432{
James Smartce396282012-09-29 11:30:56 -040012433 struct lpfc_hba *phba = (struct lpfc_hba *)context;
James Smart6b5151f2012-01-18 16:24:06 -050012434 char fwrev[FW_REV_STR_SIZE];
James Smartce396282012-09-29 11:30:56 -040012435 struct lpfc_grp_hdr *image;
James Smart52d52442011-05-24 11:42:45 -040012436 struct list_head dma_buffer_list;
12437 int i, rc = 0;
12438 struct lpfc_dmabuf *dmabuf, *next;
12439 uint32_t offset = 0, temp_offset = 0;
James Smart6b6ef5d2016-10-13 15:06:17 -070012440 uint32_t magic_number, ftype, fid, fsize;
James Smart52d52442011-05-24 11:42:45 -040012441
James Smartc71ab862012-10-31 14:44:33 -040012442 /* It can be null in no-wait mode, sanity check */
James Smartce396282012-09-29 11:30:56 -040012443 if (!fw) {
12444 rc = -ENXIO;
12445 goto out;
12446 }
12447 image = (struct lpfc_grp_hdr *)fw->data;
12448
James Smart6b6ef5d2016-10-13 15:06:17 -070012449 magic_number = be32_to_cpu(image->magic_number);
12450 ftype = bf_get_be32(lpfc_grp_hdr_file_type, image);
James Smart1feb8202018-02-22 08:18:47 -080012451 fid = bf_get_be32(lpfc_grp_hdr_id, image);
James Smart6b6ef5d2016-10-13 15:06:17 -070012452 fsize = be32_to_cpu(image->size);
12453
James Smart52d52442011-05-24 11:42:45 -040012454 INIT_LIST_HEAD(&dma_buffer_list);
James Smart52d52442011-05-24 11:42:45 -040012455 lpfc_decode_firmware_rev(phba, fwrev, 1);
James Smart88a2cfb2011-07-22 18:36:33 -040012456 if (strncmp(fwrev, image->revision, strnlen(image->revision, 16))) {
James Smart52d52442011-05-24 11:42:45 -040012457 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartce396282012-09-29 11:30:56 -040012458 "3023 Updating Firmware, Current Version:%s "
James Smart52d52442011-05-24 11:42:45 -040012459 "New Version:%s\n",
James Smart88a2cfb2011-07-22 18:36:33 -040012460 fwrev, image->revision);
James Smart52d52442011-05-24 11:42:45 -040012461 for (i = 0; i < LPFC_MBX_WR_CONFIG_MAX_BDE; i++) {
12462 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf),
12463 GFP_KERNEL);
12464 if (!dmabuf) {
12465 rc = -ENOMEM;
James Smartce396282012-09-29 11:30:56 -040012466 goto release_out;
James Smart52d52442011-05-24 11:42:45 -040012467 }
12468 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
12469 SLI4_PAGE_SIZE,
12470 &dmabuf->phys,
12471 GFP_KERNEL);
12472 if (!dmabuf->virt) {
12473 kfree(dmabuf);
12474 rc = -ENOMEM;
James Smartce396282012-09-29 11:30:56 -040012475 goto release_out;
James Smart52d52442011-05-24 11:42:45 -040012476 }
12477 list_add_tail(&dmabuf->list, &dma_buffer_list);
12478 }
12479 while (offset < fw->size) {
12480 temp_offset = offset;
12481 list_for_each_entry(dmabuf, &dma_buffer_list, list) {
James Smart079b5c92011-08-21 21:48:49 -040012482 if (temp_offset + SLI4_PAGE_SIZE > fw->size) {
James Smart52d52442011-05-24 11:42:45 -040012483 memcpy(dmabuf->virt,
12484 fw->data + temp_offset,
James Smart079b5c92011-08-21 21:48:49 -040012485 fw->size - temp_offset);
12486 temp_offset = fw->size;
James Smart52d52442011-05-24 11:42:45 -040012487 break;
12488 }
James Smart52d52442011-05-24 11:42:45 -040012489 memcpy(dmabuf->virt, fw->data + temp_offset,
12490 SLI4_PAGE_SIZE);
James Smart88a2cfb2011-07-22 18:36:33 -040012491 temp_offset += SLI4_PAGE_SIZE;
James Smart52d52442011-05-24 11:42:45 -040012492 }
12493 rc = lpfc_wr_object(phba, &dma_buffer_list,
12494 (fw->size - offset), &offset);
James Smart1feb8202018-02-22 08:18:47 -080012495 if (rc) {
12496 lpfc_log_write_firmware_error(phba, offset,
12497 magic_number, ftype, fid, fsize, fw);
James Smartce396282012-09-29 11:30:56 -040012498 goto release_out;
James Smart1feb8202018-02-22 08:18:47 -080012499 }
James Smart52d52442011-05-24 11:42:45 -040012500 }
12501 rc = offset;
James Smart1feb8202018-02-22 08:18:47 -080012502 } else
12503 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12504 "3029 Skipped Firmware update, Current "
12505 "Version:%s New Version:%s\n",
12506 fwrev, image->revision);
James Smartce396282012-09-29 11:30:56 -040012507
12508release_out:
James Smart52d52442011-05-24 11:42:45 -040012509 list_for_each_entry_safe(dmabuf, next, &dma_buffer_list, list) {
12510 list_del(&dmabuf->list);
12511 dma_free_coherent(&phba->pcidev->dev, SLI4_PAGE_SIZE,
12512 dmabuf->virt, dmabuf->phys);
12513 kfree(dmabuf);
12514 }
James Smartce396282012-09-29 11:30:56 -040012515 release_firmware(fw);
12516out:
12517 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartc71ab862012-10-31 14:44:33 -040012518 "3024 Firmware update done: %d.\n", rc);
James Smartce396282012-09-29 11:30:56 -040012519 return;
James Smart52d52442011-05-24 11:42:45 -040012520}
12521
12522/**
James Smartc71ab862012-10-31 14:44:33 -040012523 * lpfc_sli4_request_firmware_update - Request linux generic firmware upgrade
12524 * @phba: pointer to lpfc hba data structure.
12525 *
12526 * This routine is called to perform Linux generic firmware upgrade on device
12527 * that supports such feature.
12528 **/
12529int
12530lpfc_sli4_request_firmware_update(struct lpfc_hba *phba, uint8_t fw_upgrade)
12531{
12532 uint8_t file_name[ELX_MODEL_NAME_SIZE];
12533 int ret;
12534 const struct firmware *fw;
12535
12536 /* Only supported on SLI4 interface type 2 for now */
James Smart27d6ac02018-02-22 08:18:42 -080012537 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) <
James Smartc71ab862012-10-31 14:44:33 -040012538 LPFC_SLI_INTF_IF_TYPE_2)
12539 return -EPERM;
12540
12541 snprintf(file_name, ELX_MODEL_NAME_SIZE, "%s.grp", phba->ModelName);
12542
12543 if (fw_upgrade == INT_FW_UPGRADE) {
12544 ret = request_firmware_nowait(THIS_MODULE, FW_ACTION_HOTPLUG,
12545 file_name, &phba->pcidev->dev,
12546 GFP_KERNEL, (void *)phba,
12547 lpfc_write_firmware);
12548 } else if (fw_upgrade == RUN_FW_UPGRADE) {
12549 ret = request_firmware(&fw, file_name, &phba->pcidev->dev);
12550 if (!ret)
12551 lpfc_write_firmware(fw, (void *)phba);
12552 } else {
12553 ret = -EINVAL;
12554 }
12555
12556 return ret;
12557}
12558
12559/**
James Smartda0436e2009-05-22 14:51:39 -040012560 * lpfc_pci_probe_one_s4 - PCI probe func to reg SLI-4 device to PCI subsys
12561 * @pdev: pointer to PCI device
12562 * @pid: pointer to PCI device identifier
12563 *
12564 * This routine is called from the kernel's PCI subsystem to device with
12565 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
12566 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
12567 * information of the device and driver to see if the driver state that it
12568 * can support this kind of device. If the match is successful, the driver
12569 * core invokes this routine. If this routine determines it can claim the HBA,
12570 * it does all the initialization that it needs to do to handle the HBA
12571 * properly.
12572 *
12573 * Return code
12574 * 0 - driver can claim the device
12575 * negative value - driver can not claim the device
12576 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080012577static int
James Smartda0436e2009-05-22 14:51:39 -040012578lpfc_pci_probe_one_s4(struct pci_dev *pdev, const struct pci_device_id *pid)
12579{
12580 struct lpfc_hba *phba;
12581 struct lpfc_vport *vport = NULL;
James Smart6669f9b2009-10-02 15:16:45 -040012582 struct Scsi_Host *shost = NULL;
James Smart6c621a22017-05-15 15:20:45 -070012583 int error;
James Smartda0436e2009-05-22 14:51:39 -040012584 uint32_t cfg_mode, intr_mode;
James Smartda0436e2009-05-22 14:51:39 -040012585
12586 /* Allocate memory for HBA structure */
12587 phba = lpfc_hba_alloc(pdev);
12588 if (!phba)
12589 return -ENOMEM;
12590
12591 /* Perform generic PCI device enabling operation */
12592 error = lpfc_enable_pci_dev(phba);
James Smart079b5c92011-08-21 21:48:49 -040012593 if (error)
James Smartda0436e2009-05-22 14:51:39 -040012594 goto out_free_phba;
James Smartda0436e2009-05-22 14:51:39 -040012595
12596 /* Set up SLI API function jump table for PCI-device group-1 HBAs */
12597 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_OC);
12598 if (error)
12599 goto out_disable_pci_dev;
12600
12601 /* Set up SLI-4 specific device PCI memory space */
12602 error = lpfc_sli4_pci_mem_setup(phba);
12603 if (error) {
12604 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12605 "1410 Failed to set up pci memory space.\n");
12606 goto out_disable_pci_dev;
12607 }
12608
James Smartda0436e2009-05-22 14:51:39 -040012609 /* Set up SLI-4 Specific device driver resources */
12610 error = lpfc_sli4_driver_resource_setup(phba);
12611 if (error) {
12612 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12613 "1412 Failed to set up driver resource.\n");
12614 goto out_unset_pci_mem_s4;
12615 }
12616
James Smart19ca7602010-11-20 23:11:55 -050012617 INIT_LIST_HEAD(&phba->active_rrq_list);
James Smart7d791df2011-07-22 18:37:52 -040012618 INIT_LIST_HEAD(&phba->fcf.fcf_pri_list);
James Smart19ca7602010-11-20 23:11:55 -050012619
James Smartda0436e2009-05-22 14:51:39 -040012620 /* Set up common device driver resources */
12621 error = lpfc_setup_driver_resource_phase2(phba);
12622 if (error) {
12623 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12624 "1414 Failed to set up driver resource.\n");
James Smart6c621a22017-05-15 15:20:45 -070012625 goto out_unset_driver_resource_s4;
James Smartda0436e2009-05-22 14:51:39 -040012626 }
12627
James Smart079b5c92011-08-21 21:48:49 -040012628 /* Get the default values for Model Name and Description */
12629 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
12630
James Smartda0436e2009-05-22 14:51:39 -040012631 /* Now, trying to enable interrupt and bring up the device */
12632 cfg_mode = phba->cfg_use_msi;
James Smartda0436e2009-05-22 14:51:39 -040012633
James Smart7b15db32013-01-03 15:43:29 -050012634 /* Put device to a known state before enabling interrupt */
James Smartcdb42be2019-01-28 11:14:21 -080012635 phba->pport = NULL;
James Smart7b15db32013-01-03 15:43:29 -050012636 lpfc_stop_port(phba);
James Smart895427b2017-02-12 13:52:30 -080012637
James Smart7b15db32013-01-03 15:43:29 -050012638 /* Configure and enable interrupt */
12639 intr_mode = lpfc_sli4_enable_intr(phba, cfg_mode);
12640 if (intr_mode == LPFC_INTR_ERROR) {
12641 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12642 "0426 Failed to enable interrupt.\n");
12643 error = -ENODEV;
James Smartcdb42be2019-01-28 11:14:21 -080012644 goto out_unset_driver_resource;
James Smartda0436e2009-05-22 14:51:39 -040012645 }
James Smart7b15db32013-01-03 15:43:29 -050012646 /* Default to single EQ for non-MSI-X */
James Smart895427b2017-02-12 13:52:30 -080012647 if (phba->intr_type != MSIX) {
James Smart6a828b02019-01-28 11:14:31 -080012648 phba->cfg_irq_chann = 1;
James Smart2d7dbc42017-02-12 13:52:35 -080012649 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
James Smart2d7dbc42017-02-12 13:52:35 -080012650 if (phba->nvmet_support)
12651 phba->cfg_nvmet_mrq = 1;
12652 }
James Smartcdb42be2019-01-28 11:14:21 -080012653 }
James Smart6a828b02019-01-28 11:14:31 -080012654 lpfc_cpu_affinity_check(phba, phba->cfg_irq_chann);
James Smartcdb42be2019-01-28 11:14:21 -080012655
12656 /* Create SCSI host to the physical port */
12657 error = lpfc_create_shost(phba);
12658 if (error) {
12659 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12660 "1415 Failed to create scsi host.\n");
12661 goto out_disable_intr;
12662 }
12663 vport = phba->pport;
12664 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
12665
12666 /* Configure sysfs attributes */
12667 error = lpfc_alloc_sysfs_attr(vport);
12668 if (error) {
12669 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12670 "1416 Failed to allocate sysfs attr\n");
12671 goto out_destroy_shost;
James Smart895427b2017-02-12 13:52:30 -080012672 }
12673
James Smart7b15db32013-01-03 15:43:29 -050012674 /* Set up SLI-4 HBA */
12675 if (lpfc_sli4_hba_setup(phba)) {
12676 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12677 "1421 Failed to set up hba\n");
12678 error = -ENODEV;
James Smartcdb42be2019-01-28 11:14:21 -080012679 goto out_free_sysfs_attr;
James Smart7b15db32013-01-03 15:43:29 -050012680 }
12681
12682 /* Log the current active interrupt mode */
12683 phba->intr_mode = intr_mode;
12684 lpfc_log_intr_mode(phba, intr_mode);
James Smartda0436e2009-05-22 14:51:39 -040012685
12686 /* Perform post initialization setup */
12687 lpfc_post_init_setup(phba);
12688
James Smart01649562017-02-12 13:52:32 -080012689 /* NVME support in FW earlier in the driver load corrects the
12690 * FC4 type making a check for nvme_support unnecessary.
12691 */
James Smart0794d602019-01-28 11:14:19 -080012692 if (phba->nvmet_support == 0) {
12693 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
12694 /* Create NVME binding with nvme_fc_transport. This
12695 * ensures the vport is initialized. If the localport
12696 * create fails, it should not unload the driver to
12697 * support field issues.
12698 */
12699 error = lpfc_nvme_create_localport(vport);
12700 if (error) {
12701 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12702 "6004 NVME registration "
12703 "failed, error x%x\n",
12704 error);
12705 }
James Smart01649562017-02-12 13:52:32 -080012706 }
12707 }
James Smart895427b2017-02-12 13:52:30 -080012708
James Smartc71ab862012-10-31 14:44:33 -040012709 /* check for firmware upgrade or downgrade */
12710 if (phba->cfg_request_firmware_upgrade)
Sebastian Herbsztdb6f1c22015-08-31 16:48:14 -040012711 lpfc_sli4_request_firmware_update(phba, INT_FW_UPGRADE);
James Smart52d52442011-05-24 11:42:45 -040012712
James Smart1c6834a2009-07-19 10:01:26 -040012713 /* Check if there are static vports to be created. */
12714 lpfc_create_static_vport(phba);
James Smartd2cc9bc2018-09-10 10:30:50 -070012715
12716 /* Enable RAS FW log support */
12717 lpfc_sli4_ras_setup(phba);
12718
James Smartda0436e2009-05-22 14:51:39 -040012719 return 0;
12720
James Smartda0436e2009-05-22 14:51:39 -040012721out_free_sysfs_attr:
12722 lpfc_free_sysfs_attr(vport);
12723out_destroy_shost:
12724 lpfc_destroy_shost(phba);
James Smartcdb42be2019-01-28 11:14:21 -080012725out_disable_intr:
12726 lpfc_sli4_disable_intr(phba);
James Smartda0436e2009-05-22 14:51:39 -040012727out_unset_driver_resource:
12728 lpfc_unset_driver_resource_phase2(phba);
James Smartda0436e2009-05-22 14:51:39 -040012729out_unset_driver_resource_s4:
12730 lpfc_sli4_driver_resource_unset(phba);
12731out_unset_pci_mem_s4:
12732 lpfc_sli4_pci_mem_unset(phba);
12733out_disable_pci_dev:
12734 lpfc_disable_pci_dev(phba);
James Smart6669f9b2009-10-02 15:16:45 -040012735 if (shost)
12736 scsi_host_put(shost);
James Smartda0436e2009-05-22 14:51:39 -040012737out_free_phba:
12738 lpfc_hba_free(phba);
12739 return error;
12740}
12741
12742/**
12743 * lpfc_pci_remove_one_s4 - PCI func to unreg SLI-4 device from PCI subsystem
12744 * @pdev: pointer to PCI device
12745 *
12746 * This routine is called from the kernel's PCI subsystem to device with
12747 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
12748 * removed from PCI bus, it performs all the necessary cleanup for the HBA
12749 * device to be removed from the PCI subsystem properly.
12750 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080012751static void
James Smartda0436e2009-05-22 14:51:39 -040012752lpfc_pci_remove_one_s4(struct pci_dev *pdev)
12753{
12754 struct Scsi_Host *shost = pci_get_drvdata(pdev);
12755 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
12756 struct lpfc_vport **vports;
12757 struct lpfc_hba *phba = vport->phba;
12758 int i;
12759
12760 /* Mark the device unloading flag */
12761 spin_lock_irq(&phba->hbalock);
12762 vport->load_flag |= FC_UNLOADING;
12763 spin_unlock_irq(&phba->hbalock);
12764
12765 /* Free the HBA sysfs attributes */
12766 lpfc_free_sysfs_attr(vport);
12767
12768 /* Release all the vports against this physical port */
12769 vports = lpfc_create_vport_work_array(phba);
12770 if (vports != NULL)
James Smart587a37f2012-05-09 21:16:03 -040012771 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
12772 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
12773 continue;
James Smartda0436e2009-05-22 14:51:39 -040012774 fc_vport_terminate(vports[i]->fc_vport);
James Smart587a37f2012-05-09 21:16:03 -040012775 }
James Smartda0436e2009-05-22 14:51:39 -040012776 lpfc_destroy_vport_work_array(phba, vports);
12777
12778 /* Remove FC host and then SCSI host with the physical port */
12779 fc_remove_host(shost);
12780 scsi_remove_host(shost);
James Smartda0436e2009-05-22 14:51:39 -040012781
James Smartda0436e2009-05-22 14:51:39 -040012782 /* Perform ndlp cleanup on the physical port. The nvme and nvmet
12783 * localports are destroyed after to cleanup all transport memory.
12784 */
12785 lpfc_cleanup(vport);
12786 lpfc_nvmet_destroy_targetport(phba);
12787 lpfc_nvme_destroy_localport(vport);
12788
James Smartc4908502019-01-28 11:14:28 -080012789 /* De-allocate multi-XRI pools */
12790 if (phba->cfg_xri_rebalancing)
12791 lpfc_destroy_multixri_pools(phba);
12792
James Smart281d6192018-01-30 15:58:47 -080012793 /*
12794 * Bring down the SLI Layer. This step disables all interrupts,
12795 * clears the rings, discards all mailbox commands, and resets
12796 * the HBA FCoE function.
12797 */
12798 lpfc_debugfs_terminate(vport);
James Smartda0436e2009-05-22 14:51:39 -040012799
Dick Kennedy19017622017-09-29 17:34:27 -070012800 lpfc_stop_hba_timers(phba);
James Smart523128e2018-09-10 10:30:46 -070012801 spin_lock_irq(&phba->port_list_lock);
James Smartda0436e2009-05-22 14:51:39 -040012802 list_del_init(&vport->listentry);
James Smart523128e2018-09-10 10:30:46 -070012803 spin_unlock_irq(&phba->port_list_lock);
James Smartda0436e2009-05-22 14:51:39 -040012804
James Smart3677a3a2010-09-29 11:19:14 -040012805 /* Perform scsi free before driver resource_unset since scsi
James Smartda0436e2009-05-22 14:51:39 -040012806 * buffers are released to their corresponding pools here.
12807 */
James Smart5e5b5112019-01-28 11:14:22 -080012808 lpfc_io_free(phba);
James Smart01649562017-02-12 13:52:32 -080012809 lpfc_free_iocb_list(phba);
James Smart5e5b5112019-01-28 11:14:22 -080012810 lpfc_sli4_hba_unset(phba);
James Smart67d12732012-08-03 12:36:13 -040012811
James Smart0cdb84e2018-04-09 14:24:26 -070012812 lpfc_unset_driver_resource_phase2(phba);
James Smartda0436e2009-05-22 14:51:39 -040012813 lpfc_sli4_driver_resource_unset(phba);
12814
12815 /* Unmap adapter Control and Doorbell registers */
12816 lpfc_sli4_pci_mem_unset(phba);
12817
12818 /* Release PCI resources and disable device's PCI function */
12819 scsi_host_put(shost);
12820 lpfc_disable_pci_dev(phba);
12821
12822 /* Finally, free the driver's device data structure */
12823 lpfc_hba_free(phba);
12824
12825 return;
12826}
12827
12828/**
12829 * lpfc_pci_suspend_one_s4 - PCI func to suspend SLI-4 device for power mgmnt
12830 * @pdev: pointer to PCI device
12831 * @msg: power management message
12832 *
12833 * This routine is called from the kernel's PCI subsystem to support system
12834 * Power Management (PM) to device with SLI-4 interface spec. When PM invokes
12835 * this method, it quiesces the device by stopping the driver's worker
12836 * thread for the device, turning off device's interrupt and DMA, and bring
12837 * the device offline. Note that as the driver implements the minimum PM
12838 * requirements to a power-aware driver's PM support for suspend/resume -- all
12839 * the possible PM messages (SUSPEND, HIBERNATE, FREEZE) to the suspend()
12840 * method call will be treated as SUSPEND and the driver will fully
12841 * reinitialize its device during resume() method call, the driver will set
12842 * device to PCI_D3hot state in PCI config space instead of setting it
12843 * according to the @msg provided by the PM.
12844 *
12845 * Return code
12846 * 0 - driver suspended the device
12847 * Error otherwise
12848 **/
12849static int
12850lpfc_pci_suspend_one_s4(struct pci_dev *pdev, pm_message_t msg)
12851{
12852 struct Scsi_Host *shost = pci_get_drvdata(pdev);
12853 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
12854
12855 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart75baf692010-06-08 18:31:21 -040012856 "2843 PCI device Power Management suspend.\n");
James Smartda0436e2009-05-22 14:51:39 -040012857
12858 /* Bring down the device */
James Smart618a5232012-06-12 13:54:36 -040012859 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smartda0436e2009-05-22 14:51:39 -040012860 lpfc_offline(phba);
12861 kthread_stop(phba->worker_thread);
12862
12863 /* Disable interrupt from device */
12864 lpfc_sli4_disable_intr(phba);
James Smart5350d872011-10-10 21:33:49 -040012865 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -040012866
12867 /* Save device state to PCI config space */
12868 pci_save_state(pdev);
12869 pci_set_power_state(pdev, PCI_D3hot);
12870
12871 return 0;
12872}
12873
12874/**
12875 * lpfc_pci_resume_one_s4 - PCI func to resume SLI-4 device for power mgmnt
12876 * @pdev: pointer to PCI device
12877 *
12878 * This routine is called from the kernel's PCI subsystem to support system
12879 * Power Management (PM) to device with SLI-4 interface spac. When PM invokes
12880 * this method, it restores the device's PCI config space state and fully
12881 * reinitializes the device and brings it online. Note that as the driver
12882 * implements the minimum PM requirements to a power-aware driver's PM for
12883 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
12884 * to the suspend() method call will be treated as SUSPEND and the driver
12885 * will fully reinitialize its device during resume() method call, the device
12886 * will be set to PCI_D0 directly in PCI config space before restoring the
12887 * state.
12888 *
12889 * Return code
12890 * 0 - driver suspended the device
12891 * Error otherwise
12892 **/
12893static int
12894lpfc_pci_resume_one_s4(struct pci_dev *pdev)
12895{
12896 struct Scsi_Host *shost = pci_get_drvdata(pdev);
12897 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
12898 uint32_t intr_mode;
12899 int error;
12900
12901 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
12902 "0292 PCI device Power Management resume.\n");
12903
12904 /* Restore device state from PCI config space */
12905 pci_set_power_state(pdev, PCI_D0);
12906 pci_restore_state(pdev);
James Smart1dfb5a42010-02-12 14:40:50 -050012907
12908 /*
12909 * As the new kernel behavior of pci_restore_state() API call clears
12910 * device saved_state flag, need to save the restored state again.
12911 */
12912 pci_save_state(pdev);
12913
James Smartda0436e2009-05-22 14:51:39 -040012914 if (pdev->is_busmaster)
12915 pci_set_master(pdev);
12916
12917 /* Startup the kernel thread for this host adapter. */
12918 phba->worker_thread = kthread_run(lpfc_do_work, phba,
12919 "lpfc_worker_%d", phba->brd_no);
12920 if (IS_ERR(phba->worker_thread)) {
12921 error = PTR_ERR(phba->worker_thread);
12922 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12923 "0293 PM resume failed to start worker "
12924 "thread: error=x%x.\n", error);
12925 return error;
12926 }
12927
12928 /* Configure and enable interrupt */
12929 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
12930 if (intr_mode == LPFC_INTR_ERROR) {
12931 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12932 "0294 PM resume Failed to enable interrupt\n");
12933 return -EIO;
12934 } else
12935 phba->intr_mode = intr_mode;
12936
12937 /* Restart HBA and bring it online */
12938 lpfc_sli_brdrestart(phba);
12939 lpfc_online(phba);
12940
12941 /* Log the current active interrupt mode */
12942 lpfc_log_intr_mode(phba, phba->intr_mode);
12943
12944 return 0;
12945}
12946
12947/**
James Smart75baf692010-06-08 18:31:21 -040012948 * lpfc_sli4_prep_dev_for_recover - Prepare SLI4 device for pci slot recover
12949 * @phba: pointer to lpfc hba data structure.
12950 *
12951 * This routine is called to prepare the SLI4 device for PCI slot recover. It
12952 * aborts all the outstanding SCSI I/Os to the pci device.
12953 **/
12954static void
12955lpfc_sli4_prep_dev_for_recover(struct lpfc_hba *phba)
12956{
James Smart75baf692010-06-08 18:31:21 -040012957 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12958 "2828 PCI channel I/O abort preparing for recovery\n");
12959 /*
12960 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
12961 * and let the SCSI mid-layer to retry them to recover.
12962 */
James Smartdb55fba2014-04-04 13:52:02 -040012963 lpfc_sli_abort_fcp_rings(phba);
James Smart75baf692010-06-08 18:31:21 -040012964}
12965
12966/**
12967 * lpfc_sli4_prep_dev_for_reset - Prepare SLI4 device for pci slot reset
12968 * @phba: pointer to lpfc hba data structure.
12969 *
12970 * This routine is called to prepare the SLI4 device for PCI slot reset. It
12971 * disables the device interrupt and pci device, and aborts the internal FCP
12972 * pending I/Os.
12973 **/
12974static void
12975lpfc_sli4_prep_dev_for_reset(struct lpfc_hba *phba)
12976{
12977 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12978 "2826 PCI channel disable preparing for reset\n");
12979
12980 /* Block any management I/Os to the device */
James Smart618a5232012-06-12 13:54:36 -040012981 lpfc_block_mgmt_io(phba, LPFC_MBX_NO_WAIT);
James Smart75baf692010-06-08 18:31:21 -040012982
12983 /* Block all SCSI devices' I/Os on the host */
12984 lpfc_scsi_dev_block(phba);
12985
James Smartea714f32013-04-17 20:18:47 -040012986 /* Flush all driver's outstanding SCSI I/Os as we are to reset */
12987 lpfc_sli_flush_fcp_rings(phba);
12988
James Smartc3725bd2017-11-20 16:00:42 -080012989 /* Flush the outstanding NVME IOs if fc4 type enabled. */
12990 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
12991 lpfc_sli_flush_nvme_rings(phba);
12992
James Smart75baf692010-06-08 18:31:21 -040012993 /* stop all timers */
12994 lpfc_stop_hba_timers(phba);
12995
12996 /* Disable interrupt and pci device */
12997 lpfc_sli4_disable_intr(phba);
James Smart5350d872011-10-10 21:33:49 -040012998 lpfc_sli4_queue_destroy(phba);
James Smart75baf692010-06-08 18:31:21 -040012999 pci_disable_device(phba->pcidev);
James Smart75baf692010-06-08 18:31:21 -040013000}
13001
13002/**
13003 * lpfc_sli4_prep_dev_for_perm_failure - Prepare SLI4 dev for pci slot disable
13004 * @phba: pointer to lpfc hba data structure.
13005 *
13006 * This routine is called to prepare the SLI4 device for PCI slot permanently
13007 * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
13008 * pending I/Os.
13009 **/
13010static void
13011lpfc_sli4_prep_dev_for_perm_failure(struct lpfc_hba *phba)
13012{
13013 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13014 "2827 PCI channel permanent disable for failure\n");
13015
13016 /* Block all SCSI devices' I/Os on the host */
13017 lpfc_scsi_dev_block(phba);
13018
13019 /* stop all timers */
13020 lpfc_stop_hba_timers(phba);
13021
13022 /* Clean up all driver's outstanding SCSI I/Os */
13023 lpfc_sli_flush_fcp_rings(phba);
James Smartc3725bd2017-11-20 16:00:42 -080013024
13025 /* Flush the outstanding NVME IOs if fc4 type enabled. */
13026 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
13027 lpfc_sli_flush_nvme_rings(phba);
James Smart75baf692010-06-08 18:31:21 -040013028}
13029
13030/**
James Smartda0436e2009-05-22 14:51:39 -040013031 * lpfc_io_error_detected_s4 - Method for handling PCI I/O error to SLI-4 device
13032 * @pdev: pointer to PCI device.
13033 * @state: the current PCI connection state.
13034 *
13035 * This routine is called from the PCI subsystem for error handling to device
13036 * with SLI-4 interface spec. This function is called by the PCI subsystem
13037 * after a PCI bus error affecting this device has been detected. When this
13038 * function is invoked, it will need to stop all the I/Os and interrupt(s)
13039 * to the device. Once that is done, it will return PCI_ERS_RESULT_NEED_RESET
13040 * for the PCI subsystem to perform proper recovery as desired.
13041 *
13042 * Return codes
13043 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
13044 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
13045 **/
13046static pci_ers_result_t
13047lpfc_io_error_detected_s4(struct pci_dev *pdev, pci_channel_state_t state)
13048{
James Smart75baf692010-06-08 18:31:21 -040013049 struct Scsi_Host *shost = pci_get_drvdata(pdev);
13050 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
13051
13052 switch (state) {
13053 case pci_channel_io_normal:
13054 /* Non-fatal error, prepare for recovery */
13055 lpfc_sli4_prep_dev_for_recover(phba);
13056 return PCI_ERS_RESULT_CAN_RECOVER;
13057 case pci_channel_io_frozen:
13058 /* Fatal error, prepare for slot reset */
13059 lpfc_sli4_prep_dev_for_reset(phba);
13060 return PCI_ERS_RESULT_NEED_RESET;
13061 case pci_channel_io_perm_failure:
13062 /* Permanent failure, prepare for device down */
13063 lpfc_sli4_prep_dev_for_perm_failure(phba);
13064 return PCI_ERS_RESULT_DISCONNECT;
13065 default:
13066 /* Unknown state, prepare and request slot reset */
13067 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13068 "2825 Unknown PCI error state: x%x\n", state);
13069 lpfc_sli4_prep_dev_for_reset(phba);
13070 return PCI_ERS_RESULT_NEED_RESET;
13071 }
James Smartda0436e2009-05-22 14:51:39 -040013072}
13073
13074/**
13075 * lpfc_io_slot_reset_s4 - Method for restart PCI SLI-4 device from scratch
13076 * @pdev: pointer to PCI device.
13077 *
13078 * This routine is called from the PCI subsystem for error handling to device
13079 * with SLI-4 interface spec. It is called after PCI bus has been reset to
13080 * restart the PCI card from scratch, as if from a cold-boot. During the
13081 * PCI subsystem error recovery, after the driver returns
13082 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
13083 * recovery and then call this routine before calling the .resume method to
13084 * recover the device. This function will initialize the HBA device, enable
13085 * the interrupt, but it will just put the HBA to offline state without
13086 * passing any I/O traffic.
13087 *
13088 * Return codes
13089 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
13090 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
13091 */
13092static pci_ers_result_t
13093lpfc_io_slot_reset_s4(struct pci_dev *pdev)
13094{
James Smart75baf692010-06-08 18:31:21 -040013095 struct Scsi_Host *shost = pci_get_drvdata(pdev);
13096 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
13097 struct lpfc_sli *psli = &phba->sli;
13098 uint32_t intr_mode;
13099
13100 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
13101 if (pci_enable_device_mem(pdev)) {
13102 printk(KERN_ERR "lpfc: Cannot re-enable "
13103 "PCI device after reset.\n");
13104 return PCI_ERS_RESULT_DISCONNECT;
13105 }
13106
13107 pci_restore_state(pdev);
James Smart0a96e972011-07-22 18:37:28 -040013108
13109 /*
13110 * As the new kernel behavior of pci_restore_state() API call clears
13111 * device saved_state flag, need to save the restored state again.
13112 */
13113 pci_save_state(pdev);
13114
James Smart75baf692010-06-08 18:31:21 -040013115 if (pdev->is_busmaster)
13116 pci_set_master(pdev);
13117
13118 spin_lock_irq(&phba->hbalock);
13119 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
13120 spin_unlock_irq(&phba->hbalock);
13121
13122 /* Configure and enable interrupt */
13123 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
13124 if (intr_mode == LPFC_INTR_ERROR) {
13125 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13126 "2824 Cannot re-enable interrupt after "
13127 "slot reset.\n");
13128 return PCI_ERS_RESULT_DISCONNECT;
13129 } else
13130 phba->intr_mode = intr_mode;
13131
13132 /* Log the current active interrupt mode */
13133 lpfc_log_intr_mode(phba, phba->intr_mode);
13134
James Smartda0436e2009-05-22 14:51:39 -040013135 return PCI_ERS_RESULT_RECOVERED;
13136}
13137
13138/**
13139 * lpfc_io_resume_s4 - Method for resuming PCI I/O operation to SLI-4 device
13140 * @pdev: pointer to PCI device
13141 *
13142 * This routine is called from the PCI subsystem for error handling to device
13143 * with SLI-4 interface spec. It is called when kernel error recovery tells
13144 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
13145 * error recovery. After this call, traffic can start to flow from this device
13146 * again.
13147 **/
13148static void
13149lpfc_io_resume_s4(struct pci_dev *pdev)
13150{
James Smart75baf692010-06-08 18:31:21 -040013151 struct Scsi_Host *shost = pci_get_drvdata(pdev);
13152 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
13153
13154 /*
13155 * In case of slot reset, as function reset is performed through
13156 * mailbox command which needs DMA to be enabled, this operation
13157 * has to be moved to the io resume phase. Taking device offline
13158 * will perform the necessary cleanup.
13159 */
13160 if (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE)) {
13161 /* Perform device reset */
James Smart618a5232012-06-12 13:54:36 -040013162 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart75baf692010-06-08 18:31:21 -040013163 lpfc_offline(phba);
13164 lpfc_sli_brdrestart(phba);
13165 /* Bring the device back online */
13166 lpfc_online(phba);
13167 }
James Smartda0436e2009-05-22 14:51:39 -040013168}
13169
13170/**
James Smart3772a992009-05-22 14:50:54 -040013171 * lpfc_pci_probe_one - lpfc PCI probe func to reg dev to PCI subsystem
13172 * @pdev: pointer to PCI device
13173 * @pid: pointer to PCI device identifier
13174 *
13175 * This routine is to be registered to the kernel's PCI subsystem. When an
13176 * Emulex HBA device is presented on PCI bus, the kernel PCI subsystem looks
13177 * at PCI device-specific information of the device and driver to see if the
13178 * driver state that it can support this kind of device. If the match is
13179 * successful, the driver core invokes this routine. This routine dispatches
13180 * the action to the proper SLI-3 or SLI-4 device probing routine, which will
13181 * do all the initialization that it needs to do to handle the HBA device
13182 * properly.
13183 *
13184 * Return code
13185 * 0 - driver can claim the device
13186 * negative value - driver can not claim the device
13187 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080013188static int
James Smart3772a992009-05-22 14:50:54 -040013189lpfc_pci_probe_one(struct pci_dev *pdev, const struct pci_device_id *pid)
13190{
13191 int rc;
James Smart8fa38512009-07-19 10:01:03 -040013192 struct lpfc_sli_intf intf;
James Smart3772a992009-05-22 14:50:54 -040013193
James Smart28baac72010-02-12 14:42:03 -050013194 if (pci_read_config_dword(pdev, LPFC_SLI_INTF, &intf.word0))
James Smart3772a992009-05-22 14:50:54 -040013195 return -ENODEV;
13196
James Smart8fa38512009-07-19 10:01:03 -040013197 if ((bf_get(lpfc_sli_intf_valid, &intf) == LPFC_SLI_INTF_VALID) &&
James Smart28baac72010-02-12 14:42:03 -050013198 (bf_get(lpfc_sli_intf_slirev, &intf) == LPFC_SLI_INTF_REV_SLI4))
James Smartda0436e2009-05-22 14:51:39 -040013199 rc = lpfc_pci_probe_one_s4(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -040013200 else
James Smart3772a992009-05-22 14:50:54 -040013201 rc = lpfc_pci_probe_one_s3(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -040013202
James Smart3772a992009-05-22 14:50:54 -040013203 return rc;
13204}
13205
13206/**
13207 * lpfc_pci_remove_one - lpfc PCI func to unreg dev from PCI subsystem
13208 * @pdev: pointer to PCI device
13209 *
13210 * This routine is to be registered to the kernel's PCI subsystem. When an
13211 * Emulex HBA is removed from PCI bus, the driver core invokes this routine.
13212 * This routine dispatches the action to the proper SLI-3 or SLI-4 device
13213 * remove routine, which will perform all the necessary cleanup for the
13214 * device to be removed from the PCI subsystem properly.
13215 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080013216static void
James Smart3772a992009-05-22 14:50:54 -040013217lpfc_pci_remove_one(struct pci_dev *pdev)
13218{
13219 struct Scsi_Host *shost = pci_get_drvdata(pdev);
13220 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
13221
13222 switch (phba->pci_dev_grp) {
13223 case LPFC_PCI_DEV_LP:
13224 lpfc_pci_remove_one_s3(pdev);
13225 break;
James Smartda0436e2009-05-22 14:51:39 -040013226 case LPFC_PCI_DEV_OC:
13227 lpfc_pci_remove_one_s4(pdev);
13228 break;
James Smart3772a992009-05-22 14:50:54 -040013229 default:
13230 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13231 "1424 Invalid PCI device group: 0x%x\n",
13232 phba->pci_dev_grp);
13233 break;
13234 }
13235 return;
13236}
13237
13238/**
13239 * lpfc_pci_suspend_one - lpfc PCI func to suspend dev for power management
13240 * @pdev: pointer to PCI device
13241 * @msg: power management message
13242 *
13243 * This routine is to be registered to the kernel's PCI subsystem to support
13244 * system Power Management (PM). When PM invokes this method, it dispatches
13245 * the action to the proper SLI-3 or SLI-4 device suspend routine, which will
13246 * suspend the device.
13247 *
13248 * Return code
13249 * 0 - driver suspended the device
13250 * Error otherwise
13251 **/
13252static int
13253lpfc_pci_suspend_one(struct pci_dev *pdev, pm_message_t msg)
13254{
13255 struct Scsi_Host *shost = pci_get_drvdata(pdev);
13256 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
13257 int rc = -ENODEV;
13258
13259 switch (phba->pci_dev_grp) {
13260 case LPFC_PCI_DEV_LP:
13261 rc = lpfc_pci_suspend_one_s3(pdev, msg);
13262 break;
James Smartda0436e2009-05-22 14:51:39 -040013263 case LPFC_PCI_DEV_OC:
13264 rc = lpfc_pci_suspend_one_s4(pdev, msg);
13265 break;
James Smart3772a992009-05-22 14:50:54 -040013266 default:
13267 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13268 "1425 Invalid PCI device group: 0x%x\n",
13269 phba->pci_dev_grp);
13270 break;
13271 }
13272 return rc;
13273}
13274
13275/**
13276 * lpfc_pci_resume_one - lpfc PCI func to resume dev for power management
13277 * @pdev: pointer to PCI device
13278 *
13279 * This routine is to be registered to the kernel's PCI subsystem to support
13280 * system Power Management (PM). When PM invokes this method, it dispatches
13281 * the action to the proper SLI-3 or SLI-4 device resume routine, which will
13282 * resume the device.
13283 *
13284 * Return code
13285 * 0 - driver suspended the device
13286 * Error otherwise
13287 **/
13288static int
13289lpfc_pci_resume_one(struct pci_dev *pdev)
13290{
13291 struct Scsi_Host *shost = pci_get_drvdata(pdev);
13292 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
13293 int rc = -ENODEV;
13294
13295 switch (phba->pci_dev_grp) {
13296 case LPFC_PCI_DEV_LP:
13297 rc = lpfc_pci_resume_one_s3(pdev);
13298 break;
James Smartda0436e2009-05-22 14:51:39 -040013299 case LPFC_PCI_DEV_OC:
13300 rc = lpfc_pci_resume_one_s4(pdev);
13301 break;
James Smart3772a992009-05-22 14:50:54 -040013302 default:
13303 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13304 "1426 Invalid PCI device group: 0x%x\n",
13305 phba->pci_dev_grp);
13306 break;
13307 }
13308 return rc;
13309}
13310
13311/**
13312 * lpfc_io_error_detected - lpfc method for handling PCI I/O error
13313 * @pdev: pointer to PCI device.
13314 * @state: the current PCI connection state.
13315 *
13316 * This routine is registered to the PCI subsystem for error handling. This
13317 * function is called by the PCI subsystem after a PCI bus error affecting
13318 * this device has been detected. When this routine is invoked, it dispatches
13319 * the action to the proper SLI-3 or SLI-4 device error detected handling
13320 * routine, which will perform the proper error detected operation.
13321 *
13322 * Return codes
13323 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
13324 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
13325 **/
13326static pci_ers_result_t
13327lpfc_io_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
13328{
13329 struct Scsi_Host *shost = pci_get_drvdata(pdev);
13330 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
13331 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
13332
13333 switch (phba->pci_dev_grp) {
13334 case LPFC_PCI_DEV_LP:
13335 rc = lpfc_io_error_detected_s3(pdev, state);
13336 break;
James Smartda0436e2009-05-22 14:51:39 -040013337 case LPFC_PCI_DEV_OC:
13338 rc = lpfc_io_error_detected_s4(pdev, state);
13339 break;
James Smart3772a992009-05-22 14:50:54 -040013340 default:
13341 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13342 "1427 Invalid PCI device group: 0x%x\n",
13343 phba->pci_dev_grp);
13344 break;
13345 }
13346 return rc;
13347}
13348
13349/**
13350 * lpfc_io_slot_reset - lpfc method for restart PCI dev from scratch
13351 * @pdev: pointer to PCI device.
13352 *
13353 * This routine is registered to the PCI subsystem for error handling. This
13354 * function is called after PCI bus has been reset to restart the PCI card
13355 * from scratch, as if from a cold-boot. When this routine is invoked, it
13356 * dispatches the action to the proper SLI-3 or SLI-4 device reset handling
13357 * routine, which will perform the proper device reset.
13358 *
13359 * Return codes
13360 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
13361 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
13362 **/
13363static pci_ers_result_t
13364lpfc_io_slot_reset(struct pci_dev *pdev)
13365{
13366 struct Scsi_Host *shost = pci_get_drvdata(pdev);
13367 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
13368 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
13369
13370 switch (phba->pci_dev_grp) {
13371 case LPFC_PCI_DEV_LP:
13372 rc = lpfc_io_slot_reset_s3(pdev);
13373 break;
James Smartda0436e2009-05-22 14:51:39 -040013374 case LPFC_PCI_DEV_OC:
13375 rc = lpfc_io_slot_reset_s4(pdev);
13376 break;
James Smart3772a992009-05-22 14:50:54 -040013377 default:
13378 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13379 "1428 Invalid PCI device group: 0x%x\n",
13380 phba->pci_dev_grp);
13381 break;
13382 }
13383 return rc;
13384}
13385
13386/**
13387 * lpfc_io_resume - lpfc method for resuming PCI I/O operation
13388 * @pdev: pointer to PCI device
13389 *
13390 * This routine is registered to the PCI subsystem for error handling. It
13391 * is called when kernel error recovery tells the lpfc driver that it is
13392 * OK to resume normal PCI operation after PCI bus error recovery. When
13393 * this routine is invoked, it dispatches the action to the proper SLI-3
13394 * or SLI-4 device io_resume routine, which will resume the device operation.
13395 **/
13396static void
13397lpfc_io_resume(struct pci_dev *pdev)
13398{
13399 struct Scsi_Host *shost = pci_get_drvdata(pdev);
13400 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
13401
13402 switch (phba->pci_dev_grp) {
13403 case LPFC_PCI_DEV_LP:
13404 lpfc_io_resume_s3(pdev);
13405 break;
James Smartda0436e2009-05-22 14:51:39 -040013406 case LPFC_PCI_DEV_OC:
13407 lpfc_io_resume_s4(pdev);
13408 break;
James Smart3772a992009-05-22 14:50:54 -040013409 default:
13410 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13411 "1429 Invalid PCI device group: 0x%x\n",
13412 phba->pci_dev_grp);
13413 break;
13414 }
13415 return;
13416}
13417
James Smart1ba981f2014-02-20 09:56:45 -050013418/**
13419 * lpfc_sli4_oas_verify - Verify OAS is supported by this adapter
13420 * @phba: pointer to lpfc hba data structure.
13421 *
13422 * This routine checks to see if OAS is supported for this adapter. If
13423 * supported, the configure Flash Optimized Fabric flag is set. Otherwise,
13424 * the enable oas flag is cleared and the pool created for OAS device data
13425 * is destroyed.
13426 *
13427 **/
YueHaibingc7092972019-04-17 23:05:54 +080013428static void
James Smart1ba981f2014-02-20 09:56:45 -050013429lpfc_sli4_oas_verify(struct lpfc_hba *phba)
13430{
13431
13432 if (!phba->cfg_EnableXLane)
13433 return;
13434
13435 if (phba->sli4_hba.pc_sli4_params.oas_supported) {
13436 phba->cfg_fof = 1;
13437 } else {
James Smartf38fa0b2014-04-04 13:52:21 -040013438 phba->cfg_fof = 0;
James Smart1ba981f2014-02-20 09:56:45 -050013439 if (phba->device_data_mem_pool)
13440 mempool_destroy(phba->device_data_mem_pool);
13441 phba->device_data_mem_pool = NULL;
13442 }
13443
13444 return;
13445}
13446
13447/**
James Smartd2cc9bc2018-09-10 10:30:50 -070013448 * lpfc_sli4_ras_init - Verify RAS-FW log is supported by this adapter
13449 * @phba: pointer to lpfc hba data structure.
13450 *
13451 * This routine checks to see if RAS is supported by the adapter. Check the
13452 * function through which RAS support enablement is to be done.
13453 **/
13454void
13455lpfc_sli4_ras_init(struct lpfc_hba *phba)
13456{
13457 switch (phba->pcidev->device) {
13458 case PCI_DEVICE_ID_LANCER_G6_FC:
13459 case PCI_DEVICE_ID_LANCER_G7_FC:
13460 phba->ras_fwlog.ras_hwsupport = true;
James Smartcb349902018-11-29 16:09:27 -080013461 if (phba->cfg_ras_fwlog_func == PCI_FUNC(phba->pcidev->devfn) &&
13462 phba->cfg_ras_fwlog_buffsize)
James Smartd2cc9bc2018-09-10 10:30:50 -070013463 phba->ras_fwlog.ras_enabled = true;
13464 else
13465 phba->ras_fwlog.ras_enabled = false;
13466 break;
13467 default:
13468 phba->ras_fwlog.ras_hwsupport = false;
13469 }
13470}
13471
James Smart1ba981f2014-02-20 09:56:45 -050013472
dea31012005-04-17 16:05:31 -050013473MODULE_DEVICE_TABLE(pci, lpfc_id_table);
13474
Stephen Hemmingera55b2d22012-09-07 09:33:16 -070013475static const struct pci_error_handlers lpfc_err_handler = {
Linas Vepstas8d63f372007-02-14 14:28:36 -060013476 .error_detected = lpfc_io_error_detected,
13477 .slot_reset = lpfc_io_slot_reset,
13478 .resume = lpfc_io_resume,
13479};
13480
dea31012005-04-17 16:05:31 -050013481static struct pci_driver lpfc_driver = {
13482 .name = LPFC_DRIVER_NAME,
13483 .id_table = lpfc_id_table,
13484 .probe = lpfc_pci_probe_one,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080013485 .remove = lpfc_pci_remove_one,
Anton Blanchard85e8a232017-02-13 08:49:20 +110013486 .shutdown = lpfc_pci_remove_one,
James Smart3a55b532008-12-04 22:38:54 -050013487 .suspend = lpfc_pci_suspend_one,
James Smart3772a992009-05-22 14:50:54 -040013488 .resume = lpfc_pci_resume_one,
James Smart2e0fef82007-06-17 19:56:36 -050013489 .err_handler = &lpfc_err_handler,
dea31012005-04-17 16:05:31 -050013490};
13491
James Smart3ef6d242012-01-18 16:23:48 -050013492static const struct file_operations lpfc_mgmt_fop = {
Al Viro858feac2013-04-14 22:39:37 -040013493 .owner = THIS_MODULE,
James Smart3ef6d242012-01-18 16:23:48 -050013494};
13495
13496static struct miscdevice lpfc_mgmt_dev = {
13497 .minor = MISC_DYNAMIC_MINOR,
13498 .name = "lpfcmgmt",
13499 .fops = &lpfc_mgmt_fop,
13500};
13501
James Smarte59058c2008-08-24 21:49:00 -040013502/**
James Smart3621a712009-04-06 18:47:14 -040013503 * lpfc_init - lpfc module initialization routine
James Smarte59058c2008-08-24 21:49:00 -040013504 *
13505 * This routine is to be invoked when the lpfc module is loaded into the
13506 * kernel. The special kernel macro module_init() is used to indicate the
13507 * role of this routine to the kernel as lpfc module entry point.
13508 *
13509 * Return codes
13510 * 0 - successful
13511 * -ENOMEM - FC attach transport failed
13512 * all others - failed
13513 */
dea31012005-04-17 16:05:31 -050013514static int __init
13515lpfc_init(void)
13516{
13517 int error = 0;
13518
13519 printk(LPFC_MODULE_DESC "\n");
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040013520 printk(LPFC_COPYRIGHT "\n");
dea31012005-04-17 16:05:31 -050013521
James Smart3ef6d242012-01-18 16:23:48 -050013522 error = misc_register(&lpfc_mgmt_dev);
13523 if (error)
13524 printk(KERN_ERR "Could not register lpfcmgmt device, "
13525 "misc_register returned with status %d", error);
13526
James Smart458c0832016-07-06 12:36:07 -070013527 lpfc_transport_functions.vport_create = lpfc_vport_create;
13528 lpfc_transport_functions.vport_delete = lpfc_vport_delete;
dea31012005-04-17 16:05:31 -050013529 lpfc_transport_template =
13530 fc_attach_transport(&lpfc_transport_functions);
James Smart7ee5d432007-10-27 13:37:17 -040013531 if (lpfc_transport_template == NULL)
dea31012005-04-17 16:05:31 -050013532 return -ENOMEM;
James Smart458c0832016-07-06 12:36:07 -070013533 lpfc_vport_transport_template =
13534 fc_attach_transport(&lpfc_vport_transport_functions);
13535 if (lpfc_vport_transport_template == NULL) {
13536 fc_release_transport(lpfc_transport_template);
13537 return -ENOMEM;
James Smart7ee5d432007-10-27 13:37:17 -040013538 }
James Smart5fd11082018-03-05 12:04:04 -080013539 lpfc_nvme_cmd_template();
James Smartbd3061b2018-03-05 12:04:05 -080013540 lpfc_nvmet_cmd_template();
James Smart7bb03bb2013-04-17 20:19:16 -040013541
13542 /* Initialize in case vector mapping is needed */
James Smart2ea259e2017-02-12 13:52:27 -080013543 lpfc_present_cpu = num_present_cpus();
James Smart7bb03bb2013-04-17 20:19:16 -040013544
dea31012005-04-17 16:05:31 -050013545 error = pci_register_driver(&lpfc_driver);
James Smart92d7f7b2007-06-17 19:56:38 -050013546 if (error) {
dea31012005-04-17 16:05:31 -050013547 fc_release_transport(lpfc_transport_template);
James Smart458c0832016-07-06 12:36:07 -070013548 fc_release_transport(lpfc_vport_transport_template);
James Smart92d7f7b2007-06-17 19:56:38 -050013549 }
dea31012005-04-17 16:05:31 -050013550
13551 return error;
13552}
13553
James Smarte59058c2008-08-24 21:49:00 -040013554/**
James Smart3621a712009-04-06 18:47:14 -040013555 * lpfc_exit - lpfc module removal routine
James Smarte59058c2008-08-24 21:49:00 -040013556 *
13557 * This routine is invoked when the lpfc module is removed from the kernel.
13558 * The special kernel macro module_exit() is used to indicate the role of
13559 * this routine to the kernel as lpfc module exit point.
13560 */
dea31012005-04-17 16:05:31 -050013561static void __exit
13562lpfc_exit(void)
13563{
James Smart3ef6d242012-01-18 16:23:48 -050013564 misc_deregister(&lpfc_mgmt_dev);
dea31012005-04-17 16:05:31 -050013565 pci_unregister_driver(&lpfc_driver);
13566 fc_release_transport(lpfc_transport_template);
James Smart458c0832016-07-06 12:36:07 -070013567 fc_release_transport(lpfc_vport_transport_template);
James Smart81301a92008-12-04 22:39:46 -050013568 if (_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -040013569 printk(KERN_ERR "9062 BLKGRD: freeing %lu pages for "
13570 "_dump_buf_data at 0x%p\n",
James Smart81301a92008-12-04 22:39:46 -050013571 (1L << _dump_buf_data_order), _dump_buf_data);
13572 free_pages((unsigned long)_dump_buf_data, _dump_buf_data_order);
13573 }
13574
13575 if (_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -040013576 printk(KERN_ERR "9049 BLKGRD: freeing %lu pages for "
13577 "_dump_buf_dif at 0x%p\n",
James Smart81301a92008-12-04 22:39:46 -050013578 (1L << _dump_buf_dif_order), _dump_buf_dif);
13579 free_pages((unsigned long)_dump_buf_dif, _dump_buf_dif_order);
13580 }
Johannes Thumshirn79739672015-08-31 16:48:11 -040013581 idr_destroy(&lpfc_hba_index);
dea31012005-04-17 16:05:31 -050013582}
13583
13584module_init(lpfc_init);
13585module_exit(lpfc_exit);
13586MODULE_LICENSE("GPL");
13587MODULE_DESCRIPTION(LPFC_MODULE_DESC);
James Smartd080abe2017-02-12 13:52:39 -080013588MODULE_AUTHOR("Broadcom");
dea31012005-04-17 16:05:31 -050013589MODULE_VERSION("0:" LPFC_DRIVER_VERSION);