blob: 416f0fb155f5df45adbd5d6fa95ef9ec758313f4 [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>
dea31012005-04-17 16:05:31 -050042
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -040043#include <scsi/scsi.h>
dea31012005-04-17 16:05:31 -050044#include <scsi/scsi_device.h>
45#include <scsi/scsi_host.h>
46#include <scsi/scsi_transport_fc.h>
James Smart86c67372017-04-21 16:05:04 -070047#include <scsi/scsi_tcq.h>
48#include <scsi/fc/fc_fs.h>
49
50#include <linux/nvme-fc-driver.h>
dea31012005-04-17 16:05:31 -050051
James Smartda0436e2009-05-22 14:51:39 -040052#include "lpfc_hw4.h"
dea31012005-04-17 16:05:31 -050053#include "lpfc_hw.h"
54#include "lpfc_sli.h"
James Smartda0436e2009-05-22 14:51:39 -040055#include "lpfc_sli4.h"
James Smartea2151b2008-09-07 11:52:10 -040056#include "lpfc_nl.h"
dea31012005-04-17 16:05:31 -050057#include "lpfc_disc.h"
dea31012005-04-17 16:05:31 -050058#include "lpfc.h"
James Smart895427b2017-02-12 13:52:30 -080059#include "lpfc_scsi.h"
60#include "lpfc_nvme.h"
James Smart86c67372017-04-21 16:05:04 -070061#include "lpfc_nvmet.h"
dea31012005-04-17 16:05:31 -050062#include "lpfc_logmsg.h"
63#include "lpfc_crtn.h"
James Smart92d7f7b2007-06-17 19:56:38 -050064#include "lpfc_vport.h"
dea31012005-04-17 16:05:31 -050065#include "lpfc_version.h"
James Smart12f44452016-07-06 12:36:03 -070066#include "lpfc_ids.h"
dea31012005-04-17 16:05:31 -050067
James Smart81301a92008-12-04 22:39:46 -050068char *_dump_buf_data;
69unsigned long _dump_buf_data_order;
70char *_dump_buf_dif;
71unsigned long _dump_buf_dif_order;
72spinlock_t _dump_buf_lock;
73
James Smart7bb03bb2013-04-17 20:19:16 -040074/* Used when mapping IRQ vectors in a driver centric manner */
James Smartb246de12013-05-31 17:03:07 -040075uint32_t lpfc_present_cpu;
James Smart7bb03bb2013-04-17 20:19:16 -040076
dea31012005-04-17 16:05:31 -050077static void lpfc_get_hba_model_desc(struct lpfc_hba *, uint8_t *, uint8_t *);
78static int lpfc_post_rcv_buf(struct lpfc_hba *);
James Smart5350d872011-10-10 21:33:49 -040079static int lpfc_sli4_queue_verify(struct lpfc_hba *);
James Smartda0436e2009-05-22 14:51:39 -040080static int lpfc_create_bootstrap_mbox(struct lpfc_hba *);
81static int lpfc_setup_endian_order(struct lpfc_hba *);
James Smartda0436e2009-05-22 14:51:39 -040082static void lpfc_destroy_bootstrap_mbox(struct lpfc_hba *);
James Smart8a9d2e82012-05-09 21:16:12 -040083static void lpfc_free_els_sgl_list(struct lpfc_hba *);
James Smartf358dd02017-02-12 13:52:34 -080084static void lpfc_free_nvmet_sgl_list(struct lpfc_hba *);
James Smart8a9d2e82012-05-09 21:16:12 -040085static void lpfc_init_sgl_list(struct lpfc_hba *);
James Smartda0436e2009-05-22 14:51:39 -040086static int lpfc_init_active_sgl_array(struct lpfc_hba *);
87static void lpfc_free_active_sgl(struct lpfc_hba *);
88static int lpfc_hba_down_post_s3(struct lpfc_hba *phba);
89static int lpfc_hba_down_post_s4(struct lpfc_hba *phba);
90static int lpfc_sli4_cq_event_pool_create(struct lpfc_hba *);
91static void lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *);
92static void lpfc_sli4_cq_event_release_all(struct lpfc_hba *);
James Smart618a5232012-06-12 13:54:36 -040093static void lpfc_sli4_disable_intr(struct lpfc_hba *);
94static uint32_t lpfc_sli4_enable_intr(struct lpfc_hba *, uint32_t);
James Smart1ba981f2014-02-20 09:56:45 -050095static void lpfc_sli4_oas_verify(struct lpfc_hba *phba);
James Smart6a828b02019-01-28 11:14:31 -080096static uint16_t lpfc_find_cpu_handle(struct lpfc_hba *, uint16_t, int);
dea31012005-04-17 16:05:31 -050097
98static struct scsi_transport_template *lpfc_transport_template = NULL;
James Smart92d7f7b2007-06-17 19:56:38 -050099static struct scsi_transport_template *lpfc_vport_transport_template = NULL;
dea31012005-04-17 16:05:31 -0500100static DEFINE_IDR(lpfc_hba_index);
James Smartf358dd02017-02-12 13:52:34 -0800101#define LPFC_NVMET_BUF_POST 254
dea31012005-04-17 16:05:31 -0500102
James Smarte59058c2008-08-24 21:49:00 -0400103/**
James Smart3621a712009-04-06 18:47:14 -0400104 * lpfc_config_port_prep - Perform lpfc initialization prior to config port
James Smarte59058c2008-08-24 21:49:00 -0400105 * @phba: pointer to lpfc hba data structure.
106 *
107 * This routine will do LPFC initialization prior to issuing the CONFIG_PORT
108 * mailbox command. It retrieves the revision information from the HBA and
109 * collects the Vital Product Data (VPD) about the HBA for preparing the
110 * configuration of the HBA.
111 *
112 * Return codes:
113 * 0 - success.
114 * -ERESTART - requests the SLI layer to reset the HBA and try again.
115 * Any other value - indicates an error.
116 **/
dea31012005-04-17 16:05:31 -0500117int
James Smart2e0fef82007-06-17 19:56:36 -0500118lpfc_config_port_prep(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500119{
120 lpfc_vpd_t *vp = &phba->vpd;
121 int i = 0, rc;
122 LPFC_MBOXQ_t *pmb;
123 MAILBOX_t *mb;
124 char *lpfc_vpd_data = NULL;
125 uint16_t offset = 0;
126 static char licensed[56] =
127 "key unlock for use with gnu public licensed code only\0";
James Smart65a29c12006-07-06 15:50:50 -0400128 static int init_key = 1;
dea31012005-04-17 16:05:31 -0500129
130 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
131 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -0500132 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500133 return -ENOMEM;
134 }
135
James Smart04c68492009-05-22 14:52:52 -0400136 mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -0500137 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -0500138
139 if (lpfc_is_LC_HBA(phba->pcidev->device)) {
James Smart65a29c12006-07-06 15:50:50 -0400140 if (init_key) {
141 uint32_t *ptext = (uint32_t *) licensed;
dea31012005-04-17 16:05:31 -0500142
James Smart65a29c12006-07-06 15:50:50 -0400143 for (i = 0; i < 56; i += sizeof (uint32_t), ptext++)
144 *ptext = cpu_to_be32(*ptext);
145 init_key = 0;
146 }
dea31012005-04-17 16:05:31 -0500147
148 lpfc_read_nv(phba, pmb);
149 memset((char*)mb->un.varRDnvp.rsvd3, 0,
150 sizeof (mb->un.varRDnvp.rsvd3));
151 memcpy((char*)mb->un.varRDnvp.rsvd3, licensed,
152 sizeof (licensed));
153
154 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
155
156 if (rc != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500157 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
James Smarte8b62012007-08-02 11:10:09 -0400158 "0324 Config Port initialization "
dea31012005-04-17 16:05:31 -0500159 "error, mbxCmd x%x READ_NVPARM, "
160 "mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500161 mb->mbxCommand, mb->mbxStatus);
162 mempool_free(pmb, phba->mbox_mem_pool);
163 return -ERESTART;
164 }
165 memcpy(phba->wwnn, (char *)mb->un.varRDnvp.nodename,
James Smart2e0fef82007-06-17 19:56:36 -0500166 sizeof(phba->wwnn));
167 memcpy(phba->wwpn, (char *)mb->un.varRDnvp.portname,
168 sizeof(phba->wwpn));
dea31012005-04-17 16:05:31 -0500169 }
170
Martin Wilckdfb75132018-11-12 09:58:37 +0100171 /*
172 * Clear all option bits except LPFC_SLI3_BG_ENABLED,
173 * which was already set in lpfc_get_cfgparam()
174 */
175 phba->sli3_options &= (uint32_t)LPFC_SLI3_BG_ENABLED;
James Smart92d7f7b2007-06-17 19:56:38 -0500176
dea31012005-04-17 16:05:31 -0500177 /* Setup and issue mailbox READ REV command */
178 lpfc_read_rev(phba, pmb);
179 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
180 if (rc != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500181 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400182 "0439 Adapter failed to init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500183 "READ_REV, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500184 mb->mbxCommand, mb->mbxStatus);
185 mempool_free( pmb, phba->mbox_mem_pool);
186 return -ERESTART;
187 }
188
James Smart92d7f7b2007-06-17 19:56:38 -0500189
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500190 /*
191 * The value of rr must be 1 since the driver set the cv field to 1.
192 * This setting requires the FW to set all revision fields.
dea31012005-04-17 16:05:31 -0500193 */
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500194 if (mb->un.varRdRev.rr == 0) {
dea31012005-04-17 16:05:31 -0500195 vp->rev.rBit = 0;
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500196 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400197 "0440 Adapter failed to init, READ_REV has "
198 "missing revision information.\n");
dea31012005-04-17 16:05:31 -0500199 mempool_free(pmb, phba->mbox_mem_pool);
200 return -ERESTART;
dea31012005-04-17 16:05:31 -0500201 }
202
James Smart495a7142008-06-14 22:52:59 -0400203 if (phba->sli_rev == 3 && !mb->un.varRdRev.v3rsp) {
204 mempool_free(pmb, phba->mbox_mem_pool);
James Smarted957682007-06-17 19:56:37 -0500205 return -EINVAL;
James Smart495a7142008-06-14 22:52:59 -0400206 }
James Smarted957682007-06-17 19:56:37 -0500207
dea31012005-04-17 16:05:31 -0500208 /* Save information as VPD data */
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500209 vp->rev.rBit = 1;
James Smart92d7f7b2007-06-17 19:56:38 -0500210 memcpy(&vp->sli3Feat, &mb->un.varRdRev.sli3Feat, sizeof(uint32_t));
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500211 vp->rev.sli1FwRev = mb->un.varRdRev.sli1FwRev;
212 memcpy(vp->rev.sli1FwName, (char*) mb->un.varRdRev.sli1FwName, 16);
213 vp->rev.sli2FwRev = mb->un.varRdRev.sli2FwRev;
214 memcpy(vp->rev.sli2FwName, (char *) mb->un.varRdRev.sli2FwName, 16);
dea31012005-04-17 16:05:31 -0500215 vp->rev.biuRev = mb->un.varRdRev.biuRev;
216 vp->rev.smRev = mb->un.varRdRev.smRev;
217 vp->rev.smFwRev = mb->un.varRdRev.un.smFwRev;
218 vp->rev.endecRev = mb->un.varRdRev.endecRev;
219 vp->rev.fcphHigh = mb->un.varRdRev.fcphHigh;
220 vp->rev.fcphLow = mb->un.varRdRev.fcphLow;
221 vp->rev.feaLevelHigh = mb->un.varRdRev.feaLevelHigh;
222 vp->rev.feaLevelLow = mb->un.varRdRev.feaLevelLow;
223 vp->rev.postKernRev = mb->un.varRdRev.postKernRev;
224 vp->rev.opFwRev = mb->un.varRdRev.opFwRev;
225
James Smart92d7f7b2007-06-17 19:56:38 -0500226 /* If the sli feature level is less then 9, we must
227 * tear down all RPIs and VPIs on link down if NPIV
228 * is enabled.
229 */
230 if (vp->rev.feaLevelHigh < 9)
231 phba->sli3_options |= LPFC_SLI3_VPORT_TEARDOWN;
232
dea31012005-04-17 16:05:31 -0500233 if (lpfc_is_LC_HBA(phba->pcidev->device))
234 memcpy(phba->RandomData, (char *)&mb->un.varWords[24],
235 sizeof (phba->RandomData));
236
dea31012005-04-17 16:05:31 -0500237 /* Get adapter VPD information */
dea31012005-04-17 16:05:31 -0500238 lpfc_vpd_data = kmalloc(DMP_VPD_SIZE, GFP_KERNEL);
239 if (!lpfc_vpd_data)
James Smartd7c255b2008-08-24 21:50:00 -0400240 goto out_free_mbox;
dea31012005-04-17 16:05:31 -0500241 do {
James Smarta0c87cb2009-07-19 10:01:10 -0400242 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_VPD);
dea31012005-04-17 16:05:31 -0500243 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
244
245 if (rc != MBX_SUCCESS) {
246 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400247 "0441 VPD not present on adapter, "
dea31012005-04-17 16:05:31 -0500248 "mbxCmd x%x DUMP VPD, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500249 mb->mbxCommand, mb->mbxStatus);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500250 mb->un.varDmp.word_cnt = 0;
dea31012005-04-17 16:05:31 -0500251 }
James Smart04c68492009-05-22 14:52:52 -0400252 /* dump mem may return a zero when finished or we got a
253 * mailbox error, either way we are done.
254 */
255 if (mb->un.varDmp.word_cnt == 0)
256 break;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500257 if (mb->un.varDmp.word_cnt > DMP_VPD_SIZE - offset)
258 mb->un.varDmp.word_cnt = DMP_VPD_SIZE - offset;
James Smartd7c255b2008-08-24 21:50:00 -0400259 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
260 lpfc_vpd_data + offset,
James Smart92d7f7b2007-06-17 19:56:38 -0500261 mb->un.varDmp.word_cnt);
dea31012005-04-17 16:05:31 -0500262 offset += mb->un.varDmp.word_cnt;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500263 } while (mb->un.varDmp.word_cnt && offset < DMP_VPD_SIZE);
264 lpfc_parse_vpd(phba, lpfc_vpd_data, offset);
dea31012005-04-17 16:05:31 -0500265
266 kfree(lpfc_vpd_data);
dea31012005-04-17 16:05:31 -0500267out_free_mbox:
268 mempool_free(pmb, phba->mbox_mem_pool);
269 return 0;
270}
271
James Smarte59058c2008-08-24 21:49:00 -0400272/**
James Smart3621a712009-04-06 18:47:14 -0400273 * lpfc_config_async_cmpl - Completion handler for config async event mbox cmd
James Smarte59058c2008-08-24 21:49:00 -0400274 * @phba: pointer to lpfc hba data structure.
275 * @pmboxq: pointer to the driver internal queue element for mailbox command.
276 *
277 * This is the completion handler for driver's configuring asynchronous event
278 * mailbox command to the device. If the mailbox command returns successfully,
279 * it will set internal async event support flag to 1; otherwise, it will
280 * set internal async event support flag to 0.
281 **/
James Smart57127f12007-10-27 13:37:05 -0400282static void
283lpfc_config_async_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
284{
James Smart04c68492009-05-22 14:52:52 -0400285 if (pmboxq->u.mb.mbxStatus == MBX_SUCCESS)
James Smart57127f12007-10-27 13:37:05 -0400286 phba->temp_sensor_support = 1;
287 else
288 phba->temp_sensor_support = 0;
289 mempool_free(pmboxq, phba->mbox_mem_pool);
290 return;
291}
292
James Smarte59058c2008-08-24 21:49:00 -0400293/**
James Smart3621a712009-04-06 18:47:14 -0400294 * lpfc_dump_wakeup_param_cmpl - dump memory mailbox command completion handler
James Smart97207482008-12-04 22:39:19 -0500295 * @phba: pointer to lpfc hba data structure.
296 * @pmboxq: pointer to the driver internal queue element for mailbox command.
297 *
298 * This is the completion handler for dump mailbox command for getting
299 * wake up parameters. When this command complete, the response contain
300 * Option rom version of the HBA. This function translate the version number
301 * into a human readable string and store it in OptionROMVersion.
302 **/
303static void
304lpfc_dump_wakeup_param_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
305{
306 struct prog_id *prg;
307 uint32_t prog_id_word;
308 char dist = ' ';
309 /* character array used for decoding dist type. */
310 char dist_char[] = "nabx";
311
James Smart04c68492009-05-22 14:52:52 -0400312 if (pmboxq->u.mb.mbxStatus != MBX_SUCCESS) {
James Smart9f1e1b52008-12-04 22:39:40 -0500313 mempool_free(pmboxq, phba->mbox_mem_pool);
James Smart97207482008-12-04 22:39:19 -0500314 return;
James Smart9f1e1b52008-12-04 22:39:40 -0500315 }
James Smart97207482008-12-04 22:39:19 -0500316
317 prg = (struct prog_id *) &prog_id_word;
318
319 /* word 7 contain option rom version */
James Smart04c68492009-05-22 14:52:52 -0400320 prog_id_word = pmboxq->u.mb.un.varWords[7];
James Smart97207482008-12-04 22:39:19 -0500321
322 /* Decode the Option rom version word to a readable string */
323 if (prg->dist < 4)
324 dist = dist_char[prg->dist];
325
326 if ((prg->dist == 3) && (prg->num == 0))
James Smarta2fc4aef2014-09-03 12:57:55 -0400327 snprintf(phba->OptionROMVersion, 32, "%d.%d%d",
James Smart97207482008-12-04 22:39:19 -0500328 prg->ver, prg->rev, prg->lev);
329 else
James Smarta2fc4aef2014-09-03 12:57:55 -0400330 snprintf(phba->OptionROMVersion, 32, "%d.%d%d%c%d",
James Smart97207482008-12-04 22:39:19 -0500331 prg->ver, prg->rev, prg->lev,
332 dist, prg->num);
James Smart9f1e1b52008-12-04 22:39:40 -0500333 mempool_free(pmboxq, phba->mbox_mem_pool);
James Smart97207482008-12-04 22:39:19 -0500334 return;
335}
336
337/**
James Smart05580562011-05-24 11:40:48 -0400338 * lpfc_update_vport_wwn - Updates the fc_nodename, fc_portname,
339 * cfg_soft_wwnn, cfg_soft_wwpn
340 * @vport: pointer to lpfc vport data structure.
341 *
342 *
343 * Return codes
344 * None.
345 **/
346void
347lpfc_update_vport_wwn(struct lpfc_vport *vport)
348{
James Smartaeb3c812017-04-21 16:05:02 -0700349 uint8_t vvvl = vport->fc_sparam.cmn.valid_vendor_ver_level;
350 u32 *fawwpn_key = (u32 *)&vport->fc_sparam.un.vendorVersion[0];
351
James Smart05580562011-05-24 11:40:48 -0400352 /* If the soft name exists then update it using the service params */
353 if (vport->phba->cfg_soft_wwnn)
354 u64_to_wwn(vport->phba->cfg_soft_wwnn,
355 vport->fc_sparam.nodeName.u.wwn);
356 if (vport->phba->cfg_soft_wwpn)
357 u64_to_wwn(vport->phba->cfg_soft_wwpn,
358 vport->fc_sparam.portName.u.wwn);
359
360 /*
361 * If the name is empty or there exists a soft name
362 * then copy the service params name, otherwise use the fc name
363 */
364 if (vport->fc_nodename.u.wwn[0] == 0 || vport->phba->cfg_soft_wwnn)
365 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
366 sizeof(struct lpfc_name));
367 else
368 memcpy(&vport->fc_sparam.nodeName, &vport->fc_nodename,
369 sizeof(struct lpfc_name));
370
James Smartaeb3c812017-04-21 16:05:02 -0700371 /*
372 * If the port name has changed, then set the Param changes flag
373 * to unreg the login
374 */
375 if (vport->fc_portname.u.wwn[0] != 0 &&
376 memcmp(&vport->fc_portname, &vport->fc_sparam.portName,
377 sizeof(struct lpfc_name)))
378 vport->vport_flag |= FAWWPN_PARAM_CHG;
379
380 if (vport->fc_portname.u.wwn[0] == 0 ||
381 vport->phba->cfg_soft_wwpn ||
382 (vvvl == 1 && cpu_to_be32(*fawwpn_key) == FAPWWN_KEY_VENDOR) ||
383 vport->vport_flag & FAWWPN_SET) {
James Smart05580562011-05-24 11:40:48 -0400384 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
385 sizeof(struct lpfc_name));
James Smartaeb3c812017-04-21 16:05:02 -0700386 vport->vport_flag &= ~FAWWPN_SET;
387 if (vvvl == 1 && cpu_to_be32(*fawwpn_key) == FAPWWN_KEY_VENDOR)
388 vport->vport_flag |= FAWWPN_SET;
389 }
James Smart05580562011-05-24 11:40:48 -0400390 else
391 memcpy(&vport->fc_sparam.portName, &vport->fc_portname,
392 sizeof(struct lpfc_name));
393}
394
395/**
James Smart3621a712009-04-06 18:47:14 -0400396 * lpfc_config_port_post - Perform lpfc initialization after config port
James Smarte59058c2008-08-24 21:49:00 -0400397 * @phba: pointer to lpfc hba data structure.
398 *
399 * This routine will do LPFC initialization after the CONFIG_PORT mailbox
400 * command call. It performs all internal resource and state setups on the
401 * port: post IOCB buffers, enable appropriate host interrupt attentions,
402 * ELS ring timers, etc.
403 *
404 * Return codes
405 * 0 - success.
406 * Any other value - error.
407 **/
dea31012005-04-17 16:05:31 -0500408int
James Smart2e0fef82007-06-17 19:56:36 -0500409lpfc_config_port_post(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500410{
James Smart2e0fef82007-06-17 19:56:36 -0500411 struct lpfc_vport *vport = phba->pport;
James Smarta257bf92009-04-06 18:48:10 -0400412 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -0500413 LPFC_MBOXQ_t *pmb;
414 MAILBOX_t *mb;
415 struct lpfc_dmabuf *mp;
416 struct lpfc_sli *psli = &phba->sli;
417 uint32_t status, timeout;
James Smart2e0fef82007-06-17 19:56:36 -0500418 int i, j;
419 int rc;
dea31012005-04-17 16:05:31 -0500420
James Smart7af67052007-10-27 13:38:11 -0400421 spin_lock_irq(&phba->hbalock);
422 /*
423 * If the Config port completed correctly the HBA is not
424 * over heated any more.
425 */
426 if (phba->over_temp_state == HBA_OVER_TEMP)
427 phba->over_temp_state = HBA_NORMAL_TEMP;
428 spin_unlock_irq(&phba->hbalock);
429
dea31012005-04-17 16:05:31 -0500430 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
431 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -0500432 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500433 return -ENOMEM;
434 }
James Smart04c68492009-05-22 14:52:52 -0400435 mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -0500436
dea31012005-04-17 16:05:31 -0500437 /* Get login parameters for NID. */
James Smart9f1177a2010-02-26 14:12:57 -0500438 rc = lpfc_read_sparam(phba, pmb, 0);
439 if (rc) {
440 mempool_free(pmb, phba->mbox_mem_pool);
441 return -ENOMEM;
442 }
443
James Smarted957682007-06-17 19:56:37 -0500444 pmb->vport = vport;
dea31012005-04-17 16:05:31 -0500445 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500446 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400447 "0448 Adapter failed init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500448 "READ_SPARM mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500449 mb->mbxCommand, mb->mbxStatus);
James Smart2e0fef82007-06-17 19:56:36 -0500450 phba->link_state = LPFC_HBA_ERROR;
James Smart3e1f0712018-11-29 16:09:29 -0800451 mp = (struct lpfc_dmabuf *)pmb->ctx_buf;
James Smart9f1177a2010-02-26 14:12:57 -0500452 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500453 lpfc_mbuf_free(phba, mp->virt, mp->phys);
454 kfree(mp);
455 return -EIO;
456 }
457
James Smart3e1f0712018-11-29 16:09:29 -0800458 mp = (struct lpfc_dmabuf *)pmb->ctx_buf;
dea31012005-04-17 16:05:31 -0500459
James Smart2e0fef82007-06-17 19:56:36 -0500460 memcpy(&vport->fc_sparam, mp->virt, sizeof (struct serv_parm));
dea31012005-04-17 16:05:31 -0500461 lpfc_mbuf_free(phba, mp->virt, mp->phys);
462 kfree(mp);
James Smart3e1f0712018-11-29 16:09:29 -0800463 pmb->ctx_buf = NULL;
James Smart05580562011-05-24 11:40:48 -0400464 lpfc_update_vport_wwn(vport);
James Smarta257bf92009-04-06 18:48:10 -0400465
466 /* Update the fc_host data structures with new wwn. */
467 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
468 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
James Smart21e9a0a2009-05-22 14:53:21 -0400469 fc_host_max_npiv_vports(shost) = phba->max_vpi;
James Smarta257bf92009-04-06 18:48:10 -0400470
dea31012005-04-17 16:05:31 -0500471 /* If no serial number in VPD data, use low 6 bytes of WWNN */
472 /* This should be consolidated into parse_vpd ? - mr */
473 if (phba->SerialNumber[0] == 0) {
474 uint8_t *outptr;
475
James Smart2e0fef82007-06-17 19:56:36 -0500476 outptr = &vport->fc_nodename.u.s.IEEE[0];
dea31012005-04-17 16:05:31 -0500477 for (i = 0; i < 12; i++) {
478 status = *outptr++;
479 j = ((status & 0xf0) >> 4);
480 if (j <= 9)
481 phba->SerialNumber[i] =
482 (char)((uint8_t) 0x30 + (uint8_t) j);
483 else
484 phba->SerialNumber[i] =
485 (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
486 i++;
487 j = (status & 0xf);
488 if (j <= 9)
489 phba->SerialNumber[i] =
490 (char)((uint8_t) 0x30 + (uint8_t) j);
491 else
492 phba->SerialNumber[i] =
493 (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
494 }
495 }
496
dea31012005-04-17 16:05:31 -0500497 lpfc_read_config(phba, pmb);
James Smarted957682007-06-17 19:56:37 -0500498 pmb->vport = vport;
dea31012005-04-17 16:05:31 -0500499 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500500 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400501 "0453 Adapter failed to init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500502 "READ_CONFIG, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500503 mb->mbxCommand, mb->mbxStatus);
James Smart2e0fef82007-06-17 19:56:36 -0500504 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500505 mempool_free( pmb, phba->mbox_mem_pool);
506 return -EIO;
507 }
508
James Smarta0c87cb2009-07-19 10:01:10 -0400509 /* Check if the port is disabled */
510 lpfc_sli_read_link_ste(phba);
511
dea31012005-04-17 16:05:31 -0500512 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
James Smart572709e2013-07-15 18:32:43 -0400513 i = (mb->un.varRdConfig.max_xri + 1);
514 if (phba->cfg_hba_queue_depth > i) {
515 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
516 "3359 HBA queue depth changed from %d to %d\n",
517 phba->cfg_hba_queue_depth, i);
518 phba->cfg_hba_queue_depth = i;
519 }
520
521 /* Reset the DFT_LUN_Q_DEPTH to (max xri >> 3) */
522 i = (mb->un.varRdConfig.max_xri >> 3);
523 if (phba->pport->cfg_lun_queue_depth > i) {
524 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
525 "3360 LUN queue depth changed from %d to %d\n",
526 phba->pport->cfg_lun_queue_depth, i);
527 phba->pport->cfg_lun_queue_depth = i;
528 }
dea31012005-04-17 16:05:31 -0500529
530 phba->lmt = mb->un.varRdConfig.lmt;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500531
532 /* Get the default values for Model Name and Description */
533 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
534
James Smart2e0fef82007-06-17 19:56:36 -0500535 phba->link_state = LPFC_LINK_DOWN;
dea31012005-04-17 16:05:31 -0500536
James Smart0b727fe2007-10-27 13:37:25 -0400537 /* Only process IOCBs on ELS ring till hba_state is READY */
James Smart895427b2017-02-12 13:52:30 -0800538 if (psli->sli3_ring[LPFC_EXTRA_RING].sli.sli3.cmdringaddr)
539 psli->sli3_ring[LPFC_EXTRA_RING].flag |= LPFC_STOP_IOCB_EVENT;
540 if (psli->sli3_ring[LPFC_FCP_RING].sli.sli3.cmdringaddr)
541 psli->sli3_ring[LPFC_FCP_RING].flag |= LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -0500542
543 /* Post receive buffers for desired rings */
James Smarted957682007-06-17 19:56:37 -0500544 if (phba->sli_rev != 3)
545 lpfc_post_rcv_buf(phba);
dea31012005-04-17 16:05:31 -0500546
James Smart93996272008-08-24 21:50:30 -0400547 /*
548 * Configure HBA MSI-X attention conditions to messages if MSI-X mode
549 */
550 if (phba->intr_type == MSIX) {
551 rc = lpfc_config_msi(phba, pmb);
552 if (rc) {
553 mempool_free(pmb, phba->mbox_mem_pool);
554 return -EIO;
555 }
556 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
557 if (rc != MBX_SUCCESS) {
558 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
559 "0352 Config MSI mailbox command "
560 "failed, mbxCmd x%x, mbxStatus x%x\n",
James Smart04c68492009-05-22 14:52:52 -0400561 pmb->u.mb.mbxCommand,
562 pmb->u.mb.mbxStatus);
James Smart93996272008-08-24 21:50:30 -0400563 mempool_free(pmb, phba->mbox_mem_pool);
564 return -EIO;
565 }
566 }
567
James Smart04c68492009-05-22 14:52:52 -0400568 spin_lock_irq(&phba->hbalock);
James Smart93996272008-08-24 21:50:30 -0400569 /* Initialize ERATT handling flag */
570 phba->hba_flag &= ~HBA_ERATT_HANDLED;
571
dea31012005-04-17 16:05:31 -0500572 /* Enable appropriate host interrupts */
James Smart9940b972011-03-11 16:06:12 -0500573 if (lpfc_readl(phba->HCregaddr, &status)) {
574 spin_unlock_irq(&phba->hbalock);
575 return -EIO;
576 }
dea31012005-04-17 16:05:31 -0500577 status |= HC_MBINT_ENA | HC_ERINT_ENA | HC_LAINT_ENA;
578 if (psli->num_rings > 0)
579 status |= HC_R0INT_ENA;
580 if (psli->num_rings > 1)
581 status |= HC_R1INT_ENA;
582 if (psli->num_rings > 2)
583 status |= HC_R2INT_ENA;
584 if (psli->num_rings > 3)
585 status |= HC_R3INT_ENA;
586
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500587 if ((phba->cfg_poll & ENABLE_FCP_RING_POLLING) &&
588 (phba->cfg_poll & DISABLE_FCP_RING_INT))
James Smart93996272008-08-24 21:50:30 -0400589 status &= ~(HC_R0INT_ENA);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500590
dea31012005-04-17 16:05:31 -0500591 writel(status, phba->HCregaddr);
592 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -0500593 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500594
James Smart93996272008-08-24 21:50:30 -0400595 /* Set up ring-0 (ELS) timer */
596 timeout = phba->fc_ratov * 2;
James Smart256ec0d2013-04-17 20:14:58 -0400597 mod_timer(&vport->els_tmofunc,
598 jiffies + msecs_to_jiffies(1000 * timeout));
James Smart93996272008-08-24 21:50:30 -0400599 /* Set up heart beat (HB) timer */
James Smart256ec0d2013-04-17 20:14:58 -0400600 mod_timer(&phba->hb_tmofunc,
601 jiffies + msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smart858c9f62007-06-17 19:56:39 -0500602 phba->hb_outstanding = 0;
603 phba->last_completion_time = jiffies;
James Smart93996272008-08-24 21:50:30 -0400604 /* Set up error attention (ERATT) polling timer */
James Smart256ec0d2013-04-17 20:14:58 -0400605 mod_timer(&phba->eratt_poll,
James Smart65791f12016-07-06 12:35:56 -0700606 jiffies + msecs_to_jiffies(1000 * phba->eratt_poll_interval));
dea31012005-04-17 16:05:31 -0500607
James Smarta0c87cb2009-07-19 10:01:10 -0400608 if (phba->hba_flag & LINK_DISABLED) {
609 lpfc_printf_log(phba,
610 KERN_ERR, LOG_INIT,
611 "2598 Adapter Link is disabled.\n");
612 lpfc_down_link(phba, pmb);
613 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
614 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
615 if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
616 lpfc_printf_log(phba,
617 KERN_ERR, LOG_INIT,
618 "2599 Adapter failed to issue DOWN_LINK"
619 " mbox command rc 0x%x\n", rc);
620
621 mempool_free(pmb, phba->mbox_mem_pool);
622 return -EIO;
623 }
James Smarte40a02c2010-02-26 14:13:54 -0500624 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
James Smart026abb82011-12-13 13:20:45 -0500625 mempool_free(pmb, phba->mbox_mem_pool);
626 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
627 if (rc)
628 return rc;
dea31012005-04-17 16:05:31 -0500629 }
630 /* MBOX buffer will be freed in mbox compl */
James Smart57127f12007-10-27 13:37:05 -0400631 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart9f1177a2010-02-26 14:12:57 -0500632 if (!pmb) {
633 phba->link_state = LPFC_HBA_ERROR;
634 return -ENOMEM;
635 }
636
James Smart57127f12007-10-27 13:37:05 -0400637 lpfc_config_async(phba, pmb, LPFC_ELS_RING);
638 pmb->mbox_cmpl = lpfc_config_async_cmpl;
639 pmb->vport = phba->pport;
640 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -0500641
James Smart57127f12007-10-27 13:37:05 -0400642 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
643 lpfc_printf_log(phba,
644 KERN_ERR,
645 LOG_INIT,
646 "0456 Adapter failed to issue "
James Smarte4e74272009-07-19 10:01:38 -0400647 "ASYNCEVT_ENABLE mbox status x%x\n",
James Smart57127f12007-10-27 13:37:05 -0400648 rc);
649 mempool_free(pmb, phba->mbox_mem_pool);
650 }
James Smart97207482008-12-04 22:39:19 -0500651
652 /* Get Option rom version */
653 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart9f1177a2010-02-26 14:12:57 -0500654 if (!pmb) {
655 phba->link_state = LPFC_HBA_ERROR;
656 return -ENOMEM;
657 }
658
James Smart97207482008-12-04 22:39:19 -0500659 lpfc_dump_wakeup_param(phba, pmb);
660 pmb->mbox_cmpl = lpfc_dump_wakeup_param_cmpl;
661 pmb->vport = phba->pport;
662 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
663
664 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
665 lpfc_printf_log(phba, KERN_ERR, LOG_INIT, "0435 Adapter failed "
James Smarte4e74272009-07-19 10:01:38 -0400666 "to get Option ROM version status x%x\n", rc);
James Smart97207482008-12-04 22:39:19 -0500667 mempool_free(pmb, phba->mbox_mem_pool);
668 }
669
James Smartd7c255b2008-08-24 21:50:00 -0400670 return 0;
James Smartce8b3ce2006-07-06 15:50:36 -0400671}
672
James Smarte59058c2008-08-24 21:49:00 -0400673/**
James Smart84d1b002010-02-12 14:42:33 -0500674 * lpfc_hba_init_link - Initialize the FC link
675 * @phba: pointer to lpfc hba data structure.
James Smart6e7288d2010-06-07 15:23:35 -0400676 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
James Smart84d1b002010-02-12 14:42:33 -0500677 *
678 * This routine will issue the INIT_LINK mailbox command call.
679 * It is available to other drivers through the lpfc_hba data
680 * structure for use as a delayed link up mechanism with the
681 * module parameter lpfc_suppress_link_up.
682 *
683 * Return code
684 * 0 - success
685 * Any other value - error
686 **/
Rashika Kheriae399b222014-09-03 12:55:28 -0400687static int
James Smart6e7288d2010-06-07 15:23:35 -0400688lpfc_hba_init_link(struct lpfc_hba *phba, uint32_t flag)
James Smart84d1b002010-02-12 14:42:33 -0500689{
James Smart1b511972011-12-13 13:23:09 -0500690 return lpfc_hba_init_link_fc_topology(phba, phba->cfg_topology, flag);
691}
692
693/**
694 * lpfc_hba_init_link_fc_topology - Initialize FC link with desired topology
695 * @phba: pointer to lpfc hba data structure.
696 * @fc_topology: desired fc topology.
697 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
698 *
699 * This routine will issue the INIT_LINK mailbox command call.
700 * It is available to other drivers through the lpfc_hba data
701 * structure for use as a delayed link up mechanism with the
702 * module parameter lpfc_suppress_link_up.
703 *
704 * Return code
705 * 0 - success
706 * Any other value - error
707 **/
708int
709lpfc_hba_init_link_fc_topology(struct lpfc_hba *phba, uint32_t fc_topology,
710 uint32_t flag)
711{
James Smart84d1b002010-02-12 14:42:33 -0500712 struct lpfc_vport *vport = phba->pport;
713 LPFC_MBOXQ_t *pmb;
714 MAILBOX_t *mb;
715 int rc;
716
717 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
718 if (!pmb) {
719 phba->link_state = LPFC_HBA_ERROR;
720 return -ENOMEM;
721 }
722 mb = &pmb->u.mb;
723 pmb->vport = vport;
724
James Smart026abb82011-12-13 13:20:45 -0500725 if ((phba->cfg_link_speed > LPFC_USER_LINK_SPEED_MAX) ||
726 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_1G) &&
727 !(phba->lmt & LMT_1Gb)) ||
728 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_2G) &&
729 !(phba->lmt & LMT_2Gb)) ||
730 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_4G) &&
731 !(phba->lmt & LMT_4Gb)) ||
732 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_8G) &&
733 !(phba->lmt & LMT_8Gb)) ||
734 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_10G) &&
735 !(phba->lmt & LMT_10Gb)) ||
736 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_16G) &&
James Smartd38dd522015-08-31 16:48:17 -0400737 !(phba->lmt & LMT_16Gb)) ||
738 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_32G) &&
James Smartfbd8a6b2018-02-22 08:18:45 -0800739 !(phba->lmt & LMT_32Gb)) ||
740 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_64G) &&
741 !(phba->lmt & LMT_64Gb))) {
James Smart026abb82011-12-13 13:20:45 -0500742 /* Reset link speed to auto */
743 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
744 "1302 Invalid speed for this board:%d "
745 "Reset link speed to auto.\n",
746 phba->cfg_link_speed);
747 phba->cfg_link_speed = LPFC_USER_LINK_SPEED_AUTO;
748 }
James Smart1b511972011-12-13 13:23:09 -0500749 lpfc_init_link(phba, pmb, fc_topology, phba->cfg_link_speed);
James Smart84d1b002010-02-12 14:42:33 -0500750 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart1b511972011-12-13 13:23:09 -0500751 if (phba->sli_rev < LPFC_SLI_REV4)
752 lpfc_set_loopback_flag(phba);
James Smart6e7288d2010-06-07 15:23:35 -0400753 rc = lpfc_sli_issue_mbox(phba, pmb, flag);
James Smart76a95d72010-11-20 23:11:48 -0500754 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
James Smart84d1b002010-02-12 14:42:33 -0500755 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
756 "0498 Adapter failed to init, mbxCmd x%x "
757 "INIT_LINK, mbxStatus x%x\n",
758 mb->mbxCommand, mb->mbxStatus);
James Smart76a95d72010-11-20 23:11:48 -0500759 if (phba->sli_rev <= LPFC_SLI_REV3) {
760 /* Clear all interrupt enable conditions */
761 writel(0, phba->HCregaddr);
762 readl(phba->HCregaddr); /* flush */
763 /* Clear all pending interrupts */
764 writel(0xffffffff, phba->HAregaddr);
765 readl(phba->HAregaddr); /* flush */
766 }
James Smart84d1b002010-02-12 14:42:33 -0500767 phba->link_state = LPFC_HBA_ERROR;
James Smart6e7288d2010-06-07 15:23:35 -0400768 if (rc != MBX_BUSY || flag == MBX_POLL)
James Smart84d1b002010-02-12 14:42:33 -0500769 mempool_free(pmb, phba->mbox_mem_pool);
770 return -EIO;
771 }
James Smarte40a02c2010-02-26 14:13:54 -0500772 phba->cfg_suppress_link_up = LPFC_INITIALIZE_LINK;
James Smart6e7288d2010-06-07 15:23:35 -0400773 if (flag == MBX_POLL)
774 mempool_free(pmb, phba->mbox_mem_pool);
James Smart84d1b002010-02-12 14:42:33 -0500775
776 return 0;
777}
778
779/**
780 * lpfc_hba_down_link - this routine downs the FC link
James Smart6e7288d2010-06-07 15:23:35 -0400781 * @phba: pointer to lpfc hba data structure.
782 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
James Smart84d1b002010-02-12 14:42:33 -0500783 *
784 * This routine will issue the DOWN_LINK mailbox command call.
785 * It is available to other drivers through the lpfc_hba data
786 * structure for use to stop the link.
787 *
788 * Return code
789 * 0 - success
790 * Any other value - error
791 **/
Rashika Kheriae399b222014-09-03 12:55:28 -0400792static int
James Smart6e7288d2010-06-07 15:23:35 -0400793lpfc_hba_down_link(struct lpfc_hba *phba, uint32_t flag)
James Smart84d1b002010-02-12 14:42:33 -0500794{
795 LPFC_MBOXQ_t *pmb;
796 int rc;
797
798 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
799 if (!pmb) {
800 phba->link_state = LPFC_HBA_ERROR;
801 return -ENOMEM;
802 }
803
804 lpfc_printf_log(phba,
805 KERN_ERR, LOG_INIT,
806 "0491 Adapter Link is disabled.\n");
807 lpfc_down_link(phba, pmb);
808 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart6e7288d2010-06-07 15:23:35 -0400809 rc = lpfc_sli_issue_mbox(phba, pmb, flag);
James Smart84d1b002010-02-12 14:42:33 -0500810 if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
811 lpfc_printf_log(phba,
812 KERN_ERR, LOG_INIT,
813 "2522 Adapter failed to issue DOWN_LINK"
814 " mbox command rc 0x%x\n", rc);
815
816 mempool_free(pmb, phba->mbox_mem_pool);
817 return -EIO;
818 }
James Smart6e7288d2010-06-07 15:23:35 -0400819 if (flag == MBX_POLL)
820 mempool_free(pmb, phba->mbox_mem_pool);
821
James Smart84d1b002010-02-12 14:42:33 -0500822 return 0;
823}
824
825/**
James Smart3621a712009-04-06 18:47:14 -0400826 * lpfc_hba_down_prep - Perform lpfc uninitialization prior to HBA reset
James Smarte59058c2008-08-24 21:49:00 -0400827 * @phba: pointer to lpfc HBA data structure.
828 *
829 * This routine will do LPFC uninitialization before the HBA is reset when
830 * bringing down the SLI Layer.
831 *
832 * Return codes
833 * 0 - success.
834 * Any other value - error.
835 **/
dea31012005-04-17 16:05:31 -0500836int
James Smart2e0fef82007-06-17 19:56:36 -0500837lpfc_hba_down_prep(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500838{
James Smart1b32f6a2008-02-08 18:49:39 -0500839 struct lpfc_vport **vports;
840 int i;
James Smart3772a992009-05-22 14:50:54 -0400841
842 if (phba->sli_rev <= LPFC_SLI_REV3) {
843 /* Disable interrupts */
844 writel(0, phba->HCregaddr);
845 readl(phba->HCregaddr); /* flush */
846 }
dea31012005-04-17 16:05:31 -0500847
James Smart1b32f6a2008-02-08 18:49:39 -0500848 if (phba->pport->load_flag & FC_UNLOADING)
849 lpfc_cleanup_discovery_resources(phba->pport);
850 else {
851 vports = lpfc_create_vport_work_array(phba);
852 if (vports != NULL)
James Smart3772a992009-05-22 14:50:54 -0400853 for (i = 0; i <= phba->max_vports &&
854 vports[i] != NULL; i++)
James Smart1b32f6a2008-02-08 18:49:39 -0500855 lpfc_cleanup_discovery_resources(vports[i]);
856 lpfc_destroy_vport_work_array(phba, vports);
James Smart7f5f3d02008-02-08 18:50:14 -0500857 }
858 return 0;
dea31012005-04-17 16:05:31 -0500859}
860
James Smarte59058c2008-08-24 21:49:00 -0400861/**
James Smart68e814f2014-05-21 08:04:59 -0400862 * lpfc_sli4_free_sp_events - Cleanup sp_queue_events to free
863 * rspiocb which got deferred
864 *
865 * @phba: pointer to lpfc HBA data structure.
866 *
867 * This routine will cleanup completed slow path events after HBA is reset
868 * when bringing down the SLI Layer.
869 *
870 *
871 * Return codes
872 * void.
873 **/
874static void
875lpfc_sli4_free_sp_events(struct lpfc_hba *phba)
876{
877 struct lpfc_iocbq *rspiocbq;
878 struct hbq_dmabuf *dmabuf;
879 struct lpfc_cq_event *cq_event;
880
881 spin_lock_irq(&phba->hbalock);
882 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
883 spin_unlock_irq(&phba->hbalock);
884
885 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
886 /* Get the response iocb from the head of work queue */
887 spin_lock_irq(&phba->hbalock);
888 list_remove_head(&phba->sli4_hba.sp_queue_event,
889 cq_event, struct lpfc_cq_event, list);
890 spin_unlock_irq(&phba->hbalock);
891
892 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
893 case CQE_CODE_COMPL_WQE:
894 rspiocbq = container_of(cq_event, struct lpfc_iocbq,
895 cq_event);
896 lpfc_sli_release_iocbq(phba, rspiocbq);
897 break;
898 case CQE_CODE_RECEIVE:
899 case CQE_CODE_RECEIVE_V1:
900 dmabuf = container_of(cq_event, struct hbq_dmabuf,
901 cq_event);
902 lpfc_in_buf_free(phba, &dmabuf->dbuf);
903 }
904 }
905}
906
907/**
James Smartbcece5f2014-04-04 13:51:34 -0400908 * lpfc_hba_free_post_buf - Perform lpfc uninitialization after HBA reset
909 * @phba: pointer to lpfc HBA data structure.
910 *
911 * This routine will cleanup posted ELS buffers after the HBA is reset
912 * when bringing down the SLI Layer.
913 *
914 *
915 * Return codes
916 * void.
917 **/
918static void
919lpfc_hba_free_post_buf(struct lpfc_hba *phba)
920{
921 struct lpfc_sli *psli = &phba->sli;
922 struct lpfc_sli_ring *pring;
923 struct lpfc_dmabuf *mp, *next_mp;
James Smart07eab622014-04-04 13:51:44 -0400924 LIST_HEAD(buflist);
925 int count;
James Smartbcece5f2014-04-04 13:51:34 -0400926
927 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
928 lpfc_sli_hbqbuf_free_all(phba);
929 else {
930 /* Cleanup preposted buffers on the ELS ring */
James Smart895427b2017-02-12 13:52:30 -0800931 pring = &psli->sli3_ring[LPFC_ELS_RING];
James Smart07eab622014-04-04 13:51:44 -0400932 spin_lock_irq(&phba->hbalock);
933 list_splice_init(&pring->postbufq, &buflist);
934 spin_unlock_irq(&phba->hbalock);
935
936 count = 0;
937 list_for_each_entry_safe(mp, next_mp, &buflist, list) {
James Smartbcece5f2014-04-04 13:51:34 -0400938 list_del(&mp->list);
James Smart07eab622014-04-04 13:51:44 -0400939 count++;
James Smartbcece5f2014-04-04 13:51:34 -0400940 lpfc_mbuf_free(phba, mp->virt, mp->phys);
941 kfree(mp);
942 }
James Smart07eab622014-04-04 13:51:44 -0400943
944 spin_lock_irq(&phba->hbalock);
945 pring->postbufq_cnt -= count;
James Smartbcece5f2014-04-04 13:51:34 -0400946 spin_unlock_irq(&phba->hbalock);
947 }
948}
949
950/**
951 * lpfc_hba_clean_txcmplq - Perform lpfc uninitialization after HBA reset
952 * @phba: pointer to lpfc HBA data structure.
953 *
954 * This routine will cleanup the txcmplq after the HBA is reset when bringing
955 * down the SLI Layer.
956 *
957 * Return codes
958 * void
959 **/
960static void
961lpfc_hba_clean_txcmplq(struct lpfc_hba *phba)
962{
963 struct lpfc_sli *psli = &phba->sli;
James Smart895427b2017-02-12 13:52:30 -0800964 struct lpfc_queue *qp = NULL;
James Smartbcece5f2014-04-04 13:51:34 -0400965 struct lpfc_sli_ring *pring;
966 LIST_HEAD(completions);
967 int i;
James Smartc1dd9112018-01-30 15:58:57 -0800968 struct lpfc_iocbq *piocb, *next_iocb;
James Smartbcece5f2014-04-04 13:51:34 -0400969
James Smart895427b2017-02-12 13:52:30 -0800970 if (phba->sli_rev != LPFC_SLI_REV4) {
971 for (i = 0; i < psli->num_rings; i++) {
972 pring = &psli->sli3_ring[i];
James Smartbcece5f2014-04-04 13:51:34 -0400973 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -0800974 /* At this point in time the HBA is either reset or DOA
975 * Nothing should be on txcmplq as it will
976 * NEVER complete.
977 */
978 list_splice_init(&pring->txcmplq, &completions);
979 pring->txcmplq_cnt = 0;
James Smartbcece5f2014-04-04 13:51:34 -0400980 spin_unlock_irq(&phba->hbalock);
981
James Smart895427b2017-02-12 13:52:30 -0800982 lpfc_sli_abort_iocb_ring(phba, pring);
983 }
James Smartbcece5f2014-04-04 13:51:34 -0400984 /* Cancel all the IOCBs from the completions list */
James Smart895427b2017-02-12 13:52:30 -0800985 lpfc_sli_cancel_iocbs(phba, &completions,
986 IOSTAT_LOCAL_REJECT, IOERR_SLI_ABORTED);
987 return;
988 }
989 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
990 pring = qp->pring;
991 if (!pring)
992 continue;
993 spin_lock_irq(&pring->ring_lock);
James Smartc1dd9112018-01-30 15:58:57 -0800994 list_for_each_entry_safe(piocb, next_iocb,
995 &pring->txcmplq, list)
996 piocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smart895427b2017-02-12 13:52:30 -0800997 list_splice_init(&pring->txcmplq, &completions);
998 pring->txcmplq_cnt = 0;
999 spin_unlock_irq(&pring->ring_lock);
James Smartbcece5f2014-04-04 13:51:34 -04001000 lpfc_sli_abort_iocb_ring(phba, pring);
1001 }
James Smart895427b2017-02-12 13:52:30 -08001002 /* Cancel all the IOCBs from the completions list */
1003 lpfc_sli_cancel_iocbs(phba, &completions,
1004 IOSTAT_LOCAL_REJECT, IOERR_SLI_ABORTED);
James Smartbcece5f2014-04-04 13:51:34 -04001005}
1006
1007/**
James Smart3772a992009-05-22 14:50:54 -04001008 * lpfc_hba_down_post_s3 - Perform lpfc uninitialization after HBA reset
James Smartbcece5f2014-04-04 13:51:34 -04001009 int i;
James Smarte59058c2008-08-24 21:49:00 -04001010 * @phba: pointer to lpfc HBA data structure.
1011 *
1012 * This routine will do uninitialization after the HBA is reset when bring
1013 * down the SLI Layer.
1014 *
1015 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001016 * 0 - success.
James Smarte59058c2008-08-24 21:49:00 -04001017 * Any other value - error.
1018 **/
James Smart3772a992009-05-22 14:50:54 -04001019static int
1020lpfc_hba_down_post_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05001021{
James Smartbcece5f2014-04-04 13:51:34 -04001022 lpfc_hba_free_post_buf(phba);
1023 lpfc_hba_clean_txcmplq(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -05001024 return 0;
1025}
James Smart5af5eee2010-10-22 11:06:38 -04001026
James Smartda0436e2009-05-22 14:51:39 -04001027/**
1028 * lpfc_hba_down_post_s4 - Perform lpfc uninitialization after HBA reset
1029 * @phba: pointer to lpfc HBA data structure.
1030 *
1031 * This routine will do uninitialization after the HBA is reset when bring
1032 * down the SLI Layer.
1033 *
1034 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001035 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -04001036 * Any other value - error.
1037 **/
1038static int
1039lpfc_hba_down_post_s4(struct lpfc_hba *phba)
1040{
James Smartc4908502019-01-28 11:14:28 -08001041 struct lpfc_io_buf *psb, *psb_next;
James Smart86c67372017-04-21 16:05:04 -07001042 struct lpfc_nvmet_rcv_ctx *ctxp, *ctxp_next;
James Smart5e5b5112019-01-28 11:14:22 -08001043 struct lpfc_sli4_hdw_queue *qp;
James Smartda0436e2009-05-22 14:51:39 -04001044 LIST_HEAD(aborts);
James Smart895427b2017-02-12 13:52:30 -08001045 LIST_HEAD(nvme_aborts);
James Smart86c67372017-04-21 16:05:04 -07001046 LIST_HEAD(nvmet_aborts);
James Smart0f65ff62010-02-26 14:14:23 -05001047 struct lpfc_sglq *sglq_entry = NULL;
James Smart5e5b5112019-01-28 11:14:22 -08001048 int cnt, idx;
James Smart0f65ff62010-02-26 14:14:23 -05001049
James Smart895427b2017-02-12 13:52:30 -08001050
1051 lpfc_sli_hbqbuf_free_all(phba);
James Smartbcece5f2014-04-04 13:51:34 -04001052 lpfc_hba_clean_txcmplq(phba);
1053
James Smartda0436e2009-05-22 14:51:39 -04001054 /* At this point in time the HBA is either reset or DOA. Either
1055 * way, nothing should be on lpfc_abts_els_sgl_list, it needs to be
James Smart895427b2017-02-12 13:52:30 -08001056 * on the lpfc_els_sgl_list so that it can either be freed if the
James Smartda0436e2009-05-22 14:51:39 -04001057 * driver is unloading or reposted if the driver is restarting
1058 * the port.
1059 */
James Smart895427b2017-02-12 13:52:30 -08001060 spin_lock_irq(&phba->hbalock); /* required for lpfc_els_sgl_list and */
James Smartda0436e2009-05-22 14:51:39 -04001061 /* scsl_buf_list */
James Smart895427b2017-02-12 13:52:30 -08001062 /* sgl_list_lock required because worker thread uses this
James Smartda0436e2009-05-22 14:51:39 -04001063 * list.
1064 */
James Smart895427b2017-02-12 13:52:30 -08001065 spin_lock(&phba->sli4_hba.sgl_list_lock);
James Smart0f65ff62010-02-26 14:14:23 -05001066 list_for_each_entry(sglq_entry,
1067 &phba->sli4_hba.lpfc_abts_els_sgl_list, list)
1068 sglq_entry->state = SGL_FREED;
1069
James Smartda0436e2009-05-22 14:51:39 -04001070 list_splice_init(&phba->sli4_hba.lpfc_abts_els_sgl_list,
James Smart895427b2017-02-12 13:52:30 -08001071 &phba->sli4_hba.lpfc_els_sgl_list);
1072
James Smartf358dd02017-02-12 13:52:34 -08001073
James Smart895427b2017-02-12 13:52:30 -08001074 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smart5e5b5112019-01-28 11:14:22 -08001075
1076 /* abts_xxxx_buf_list_lock required because worker thread uses this
James Smartda0436e2009-05-22 14:51:39 -04001077 * list.
1078 */
James Smart5e5b5112019-01-28 11:14:22 -08001079 cnt = 0;
1080 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
1081 qp = &phba->sli4_hba.hdwq[idx];
1082
1083 spin_lock(&qp->abts_scsi_buf_list_lock);
1084 list_splice_init(&qp->lpfc_abts_scsi_buf_list,
James Smart895427b2017-02-12 13:52:30 -08001085 &aborts);
James Smart895427b2017-02-12 13:52:30 -08001086
James Smart0794d602019-01-28 11:14:19 -08001087 list_for_each_entry_safe(psb, psb_next, &aborts, list) {
James Smart86c67372017-04-21 16:05:04 -07001088 psb->pCmd = NULL;
1089 psb->status = IOSTAT_SUCCESS;
James Smartcf1a1d32017-12-08 17:18:03 -08001090 cnt++;
James Smart86c67372017-04-21 16:05:04 -07001091 }
James Smart5e5b5112019-01-28 11:14:22 -08001092 spin_lock(&qp->io_buf_list_put_lock);
1093 list_splice_init(&aborts, &qp->lpfc_io_buf_list_put);
1094 qp->put_io_bufs += qp->abts_scsi_io_bufs;
1095 qp->abts_scsi_io_bufs = 0;
1096 spin_unlock(&qp->io_buf_list_put_lock);
1097 spin_unlock(&qp->abts_scsi_buf_list_lock);
James Smart86c67372017-04-21 16:05:04 -07001098
James Smart5e5b5112019-01-28 11:14:22 -08001099 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
1100 spin_lock(&qp->abts_nvme_buf_list_lock);
1101 list_splice_init(&qp->lpfc_abts_nvme_buf_list,
1102 &nvme_aborts);
1103 list_for_each_entry_safe(psb, psb_next, &nvme_aborts,
1104 list) {
1105 psb->pCmd = NULL;
1106 psb->status = IOSTAT_SUCCESS;
1107 cnt++;
1108 }
1109 spin_lock(&qp->io_buf_list_put_lock);
1110 qp->put_io_bufs += qp->abts_nvme_io_bufs;
1111 qp->abts_nvme_io_bufs = 0;
1112 list_splice_init(&nvme_aborts,
1113 &qp->lpfc_io_buf_list_put);
1114 spin_unlock(&qp->io_buf_list_put_lock);
1115 spin_unlock(&qp->abts_nvme_buf_list_lock);
1116
1117 }
1118 }
James Smart731eedc2019-03-12 16:30:12 -07001119 spin_unlock_irq(&phba->hbalock);
James Smart5e5b5112019-01-28 11:14:22 -08001120
1121 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
James Smart731eedc2019-03-12 16:30:12 -07001122 spin_lock_irq(&phba->sli4_hba.abts_nvmet_buf_list_lock);
James Smart5e5b5112019-01-28 11:14:22 -08001123 list_splice_init(&phba->sli4_hba.lpfc_abts_nvmet_ctx_list,
1124 &nvmet_aborts);
James Smart731eedc2019-03-12 16:30:12 -07001125 spin_unlock_irq(&phba->sli4_hba.abts_nvmet_buf_list_lock);
James Smart86c67372017-04-21 16:05:04 -07001126 list_for_each_entry_safe(ctxp, ctxp_next, &nvmet_aborts, list) {
1127 ctxp->flag &= ~(LPFC_NVMET_XBUSY | LPFC_NVMET_ABORT_OP);
James Smart6c621a22017-05-15 15:20:45 -07001128 lpfc_nvmet_ctxbuf_post(phba, ctxp->ctxbuf);
James Smart86c67372017-04-21 16:05:04 -07001129 }
James Smart895427b2017-02-12 13:52:30 -08001130 }
James Smart895427b2017-02-12 13:52:30 -08001131
James Smart68e814f2014-05-21 08:04:59 -04001132 lpfc_sli4_free_sp_events(phba);
James Smart5e5b5112019-01-28 11:14:22 -08001133 return cnt;
James Smartda0436e2009-05-22 14:51:39 -04001134}
1135
1136/**
1137 * lpfc_hba_down_post - Wrapper func for hba down post routine
1138 * @phba: pointer to lpfc HBA data structure.
1139 *
1140 * This routine wraps the actual SLI3 or SLI4 routine for performing
1141 * uninitialization after the HBA is reset when bring down the SLI Layer.
1142 *
1143 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001144 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -04001145 * Any other value - error.
1146 **/
1147int
1148lpfc_hba_down_post(struct lpfc_hba *phba)
1149{
1150 return (*phba->lpfc_hba_down_post)(phba);
1151}
Jamie Wellnitz41415862006-02-28 19:25:27 -05001152
James Smarte59058c2008-08-24 21:49:00 -04001153/**
James Smart3621a712009-04-06 18:47:14 -04001154 * lpfc_hb_timeout - The HBA-timer timeout handler
James Smarte59058c2008-08-24 21:49:00 -04001155 * @ptr: unsigned long holds the pointer to lpfc hba data structure.
1156 *
1157 * This is the HBA-timer timeout handler registered to the lpfc driver. When
1158 * this timer fires, a HBA timeout event shall be posted to the lpfc driver
1159 * work-port-events bitmap and the worker thread is notified. This timeout
1160 * event will be used by the worker thread to invoke the actual timeout
1161 * handler routine, lpfc_hb_timeout_handler. Any periodical operations will
1162 * be performed in the timeout handler and the HBA timeout event bit shall
1163 * be cleared by the worker thread after it has taken the event bitmap out.
1164 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001165static void
Kees Cookf22eb4d2017-09-06 20:24:26 -07001166lpfc_hb_timeout(struct timer_list *t)
James Smart858c9f62007-06-17 19:56:39 -05001167{
1168 struct lpfc_hba *phba;
James Smart5e9d9b82008-06-14 22:52:53 -04001169 uint32_t tmo_posted;
James Smart858c9f62007-06-17 19:56:39 -05001170 unsigned long iflag;
1171
Kees Cookf22eb4d2017-09-06 20:24:26 -07001172 phba = from_timer(phba, t, hb_tmofunc);
James Smart93996272008-08-24 21:50:30 -04001173
1174 /* Check for heart beat timeout conditions */
James Smart858c9f62007-06-17 19:56:39 -05001175 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -04001176 tmo_posted = phba->pport->work_port_events & WORKER_HB_TMO;
1177 if (!tmo_posted)
James Smart858c9f62007-06-17 19:56:39 -05001178 phba->pport->work_port_events |= WORKER_HB_TMO;
1179 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
1180
James Smart93996272008-08-24 21:50:30 -04001181 /* Tell the worker thread there is work to do */
James Smart5e9d9b82008-06-14 22:52:53 -04001182 if (!tmo_posted)
1183 lpfc_worker_wake_up(phba);
James Smart858c9f62007-06-17 19:56:39 -05001184 return;
1185}
1186
James Smarte59058c2008-08-24 21:49:00 -04001187/**
James Smart19ca7602010-11-20 23:11:55 -05001188 * lpfc_rrq_timeout - The RRQ-timer timeout handler
1189 * @ptr: unsigned long holds the pointer to lpfc hba data structure.
1190 *
1191 * This is the RRQ-timer timeout handler registered to the lpfc driver. When
1192 * this timer fires, a RRQ timeout event shall be posted to the lpfc driver
1193 * work-port-events bitmap and the worker thread is notified. This timeout
1194 * event will be used by the worker thread to invoke the actual timeout
1195 * handler routine, lpfc_rrq_handler. Any periodical operations will
1196 * be performed in the timeout handler and the RRQ timeout event bit shall
1197 * be cleared by the worker thread after it has taken the event bitmap out.
1198 **/
1199static void
Kees Cookf22eb4d2017-09-06 20:24:26 -07001200lpfc_rrq_timeout(struct timer_list *t)
James Smart19ca7602010-11-20 23:11:55 -05001201{
1202 struct lpfc_hba *phba;
James Smart19ca7602010-11-20 23:11:55 -05001203 unsigned long iflag;
1204
Kees Cookf22eb4d2017-09-06 20:24:26 -07001205 phba = from_timer(phba, t, rrq_tmr);
James Smart19ca7602010-11-20 23:11:55 -05001206 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart06918ac2014-02-20 09:57:57 -05001207 if (!(phba->pport->load_flag & FC_UNLOADING))
1208 phba->hba_flag |= HBA_RRQ_ACTIVE;
1209 else
1210 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
James Smart19ca7602010-11-20 23:11:55 -05001211 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
James Smart06918ac2014-02-20 09:57:57 -05001212
1213 if (!(phba->pport->load_flag & FC_UNLOADING))
1214 lpfc_worker_wake_up(phba);
James Smart19ca7602010-11-20 23:11:55 -05001215}
1216
1217/**
James Smart3621a712009-04-06 18:47:14 -04001218 * lpfc_hb_mbox_cmpl - The lpfc heart-beat mailbox command callback function
James Smarte59058c2008-08-24 21:49:00 -04001219 * @phba: pointer to lpfc hba data structure.
1220 * @pmboxq: pointer to the driver internal queue element for mailbox command.
1221 *
1222 * This is the callback function to the lpfc heart-beat mailbox command.
1223 * If configured, the lpfc driver issues the heart-beat mailbox command to
1224 * the HBA every LPFC_HB_MBOX_INTERVAL (current 5) seconds. At the time the
1225 * heart-beat mailbox command is issued, the driver shall set up heart-beat
1226 * timeout timer to LPFC_HB_MBOX_TIMEOUT (current 30) seconds and marks
1227 * heart-beat outstanding state. Once the mailbox command comes back and
1228 * no error conditions detected, the heart-beat mailbox command timer is
1229 * reset to LPFC_HB_MBOX_INTERVAL seconds and the heart-beat outstanding
1230 * state is cleared for the next heart-beat. If the timer expired with the
1231 * heart-beat outstanding state set, the driver will put the HBA offline.
1232 **/
James Smart858c9f62007-06-17 19:56:39 -05001233static void
1234lpfc_hb_mbox_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
1235{
1236 unsigned long drvr_flag;
1237
1238 spin_lock_irqsave(&phba->hbalock, drvr_flag);
1239 phba->hb_outstanding = 0;
1240 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
1241
James Smart93996272008-08-24 21:50:30 -04001242 /* Check and reset heart-beat timer is necessary */
James Smart858c9f62007-06-17 19:56:39 -05001243 mempool_free(pmboxq, phba->mbox_mem_pool);
1244 if (!(phba->pport->fc_flag & FC_OFFLINE_MODE) &&
1245 !(phba->link_state == LPFC_HBA_ERROR) &&
James Smart51ef4c22007-08-02 11:10:31 -04001246 !(phba->pport->load_flag & FC_UNLOADING))
James Smart858c9f62007-06-17 19:56:39 -05001247 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001248 jiffies +
1249 msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smart858c9f62007-06-17 19:56:39 -05001250 return;
1251}
1252
James Smart32517fc2019-01-28 11:14:33 -08001253static void
1254lpfc_hb_eq_delay_work(struct work_struct *work)
1255{
1256 struct lpfc_hba *phba = container_of(to_delayed_work(work),
1257 struct lpfc_hba, eq_delay_work);
1258 struct lpfc_eq_intr_info *eqi, *eqi_new;
1259 struct lpfc_queue *eq, *eq_next;
1260 unsigned char *eqcnt = NULL;
1261 uint32_t usdelay;
1262 int i;
1263
1264 if (!phba->cfg_auto_imax || phba->pport->load_flag & FC_UNLOADING)
1265 return;
1266
1267 if (phba->link_state == LPFC_HBA_ERROR ||
1268 phba->pport->fc_flag & FC_OFFLINE_MODE)
1269 goto requeue;
1270
1271 eqcnt = kcalloc(num_possible_cpus(), sizeof(unsigned char),
1272 GFP_KERNEL);
1273 if (!eqcnt)
1274 goto requeue;
1275
James Smart657add42019-05-21 17:49:06 -07001276 /* Loop thru all IRQ vectors */
James Smart32517fc2019-01-28 11:14:33 -08001277 for (i = 0; i < phba->cfg_irq_chann; i++) {
James Smart657add42019-05-21 17:49:06 -07001278 /* Get the EQ corresponding to the IRQ vector */
1279 eq = phba->sli4_hba.hba_eq_hdl[i].eq;
James Smart32517fc2019-01-28 11:14:33 -08001280 if (eq && eqcnt[eq->last_cpu] < 2)
1281 eqcnt[eq->last_cpu]++;
1282 continue;
1283 }
1284
1285 for_each_present_cpu(i) {
1286 if (phba->cfg_irq_chann > 1 && eqcnt[i] < 2)
1287 continue;
1288
1289 eqi = per_cpu_ptr(phba->sli4_hba.eq_info, i);
1290
1291 usdelay = (eqi->icnt / LPFC_IMAX_THRESHOLD) *
1292 LPFC_EQ_DELAY_STEP;
1293 if (usdelay > LPFC_MAX_AUTO_EQ_DELAY)
1294 usdelay = LPFC_MAX_AUTO_EQ_DELAY;
1295
1296 eqi->icnt = 0;
1297
1298 list_for_each_entry_safe(eq, eq_next, &eqi->list, cpu_list) {
1299 if (eq->last_cpu != i) {
1300 eqi_new = per_cpu_ptr(phba->sli4_hba.eq_info,
1301 eq->last_cpu);
1302 list_move_tail(&eq->cpu_list, &eqi_new->list);
1303 continue;
1304 }
1305 if (usdelay != eq->q_mode)
1306 lpfc_modify_hba_eq_delay(phba, eq->hdwq, 1,
1307 usdelay);
1308 }
1309 }
1310
1311 kfree(eqcnt);
1312
1313requeue:
1314 queue_delayed_work(phba->wq, &phba->eq_delay_work,
1315 msecs_to_jiffies(LPFC_EQ_DELAY_MSECS));
1316}
1317
James Smarte59058c2008-08-24 21:49:00 -04001318/**
James Smartc4908502019-01-28 11:14:28 -08001319 * lpfc_hb_mxp_handler - Multi-XRI pools handler to adjust XRI distribution
1320 * @phba: pointer to lpfc hba data structure.
1321 *
1322 * For each heartbeat, this routine does some heuristic methods to adjust
1323 * XRI distribution. The goal is to fully utilize free XRIs.
1324 **/
1325static void lpfc_hb_mxp_handler(struct lpfc_hba *phba)
1326{
1327 u32 i;
1328 u32 hwq_count;
1329
1330 hwq_count = phba->cfg_hdw_queue;
1331 for (i = 0; i < hwq_count; i++) {
1332 /* Adjust XRIs in private pool */
1333 lpfc_adjust_pvt_pool_count(phba, i);
1334
1335 /* Adjust high watermark */
1336 lpfc_adjust_high_watermark(phba, i);
1337
1338#ifdef LPFC_MXP_STAT
1339 /* Snapshot pbl, pvt and busy count */
1340 lpfc_snapshot_mxp(phba, i);
1341#endif
1342 }
1343}
1344
James Smarte59058c2008-08-24 21:49:00 -04001345/**
James Smart3621a712009-04-06 18:47:14 -04001346 * lpfc_hb_timeout_handler - The HBA-timer timeout handler
James Smarte59058c2008-08-24 21:49:00 -04001347 * @phba: pointer to lpfc hba data structure.
1348 *
1349 * This is the actual HBA-timer timeout handler to be invoked by the worker
1350 * thread whenever the HBA timer fired and HBA-timeout event posted. This
1351 * handler performs any periodic operations needed for the device. If such
1352 * periodic event has already been attended to either in the interrupt handler
1353 * or by processing slow-ring or fast-ring events within the HBA-timer
1354 * timeout window (LPFC_HB_MBOX_INTERVAL), this handler just simply resets
1355 * the timer for the next timeout period. If lpfc heart-beat mailbox command
1356 * is configured and there is no heart-beat mailbox command outstanding, a
1357 * heart-beat mailbox is issued and timer set properly. Otherwise, if there
1358 * has been a heart-beat mailbox command outstanding, the HBA shall be put
1359 * to offline.
1360 **/
James Smart858c9f62007-06-17 19:56:39 -05001361void
1362lpfc_hb_timeout_handler(struct lpfc_hba *phba)
1363{
James Smart45ed1192009-10-02 15:17:02 -04001364 struct lpfc_vport **vports;
James Smart858c9f62007-06-17 19:56:39 -05001365 LPFC_MBOXQ_t *pmboxq;
James Smart0ff10d42008-01-11 01:52:36 -05001366 struct lpfc_dmabuf *buf_ptr;
James Smart45ed1192009-10-02 15:17:02 -04001367 int retval, i;
James Smart858c9f62007-06-17 19:56:39 -05001368 struct lpfc_sli *psli = &phba->sli;
James Smart0ff10d42008-01-11 01:52:36 -05001369 LIST_HEAD(completions);
James Smart858c9f62007-06-17 19:56:39 -05001370
James Smartc4908502019-01-28 11:14:28 -08001371 if (phba->cfg_xri_rebalancing) {
1372 /* Multi-XRI pools handler */
1373 lpfc_hb_mxp_handler(phba);
1374 }
James Smart858c9f62007-06-17 19:56:39 -05001375
James Smart45ed1192009-10-02 15:17:02 -04001376 vports = lpfc_create_vport_work_array(phba);
1377 if (vports != NULL)
James Smart4258e982015-12-16 18:11:58 -05001378 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart45ed1192009-10-02 15:17:02 -04001379 lpfc_rcv_seq_check_edtov(vports[i]);
James Smart4258e982015-12-16 18:11:58 -05001380 lpfc_fdmi_num_disc_check(vports[i]);
1381 }
James Smart45ed1192009-10-02 15:17:02 -04001382 lpfc_destroy_vport_work_array(phba, vports);
1383
James Smart858c9f62007-06-17 19:56:39 -05001384 if ((phba->link_state == LPFC_HBA_ERROR) ||
James Smart51ef4c22007-08-02 11:10:31 -04001385 (phba->pport->load_flag & FC_UNLOADING) ||
James Smart858c9f62007-06-17 19:56:39 -05001386 (phba->pport->fc_flag & FC_OFFLINE_MODE))
1387 return;
1388
1389 spin_lock_irq(&phba->pport->work_port_lock);
James Smart858c9f62007-06-17 19:56:39 -05001390
James Smart256ec0d2013-04-17 20:14:58 -04001391 if (time_after(phba->last_completion_time +
1392 msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL),
1393 jiffies)) {
James Smart858c9f62007-06-17 19:56:39 -05001394 spin_unlock_irq(&phba->pport->work_port_lock);
1395 if (!phba->hb_outstanding)
1396 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001397 jiffies +
1398 msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smart858c9f62007-06-17 19:56:39 -05001399 else
1400 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001401 jiffies +
1402 msecs_to_jiffies(1000 * LPFC_HB_MBOX_TIMEOUT));
James Smart858c9f62007-06-17 19:56:39 -05001403 return;
1404 }
1405 spin_unlock_irq(&phba->pport->work_port_lock);
1406
James Smart0ff10d42008-01-11 01:52:36 -05001407 if (phba->elsbuf_cnt &&
1408 (phba->elsbuf_cnt == phba->elsbuf_prev_cnt)) {
1409 spin_lock_irq(&phba->hbalock);
1410 list_splice_init(&phba->elsbuf, &completions);
1411 phba->elsbuf_cnt = 0;
1412 phba->elsbuf_prev_cnt = 0;
1413 spin_unlock_irq(&phba->hbalock);
1414
1415 while (!list_empty(&completions)) {
1416 list_remove_head(&completions, buf_ptr,
1417 struct lpfc_dmabuf, list);
1418 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
1419 kfree(buf_ptr);
1420 }
1421 }
1422 phba->elsbuf_prev_cnt = phba->elsbuf_cnt;
1423
James Smart858c9f62007-06-17 19:56:39 -05001424 /* If there is no heart beat outstanding, issue a heartbeat command */
James Smart13815c82008-01-11 01:52:48 -05001425 if (phba->cfg_enable_hba_heartbeat) {
1426 if (!phba->hb_outstanding) {
James Smartbc739052010-08-04 16:11:18 -04001427 if ((!(psli->sli_flag & LPFC_SLI_MBOX_ACTIVE)) &&
1428 (list_empty(&psli->mboxq))) {
1429 pmboxq = mempool_alloc(phba->mbox_mem_pool,
1430 GFP_KERNEL);
1431 if (!pmboxq) {
1432 mod_timer(&phba->hb_tmofunc,
1433 jiffies +
James Smart256ec0d2013-04-17 20:14:58 -04001434 msecs_to_jiffies(1000 *
1435 LPFC_HB_MBOX_INTERVAL));
James Smartbc739052010-08-04 16:11:18 -04001436 return;
1437 }
James Smart13815c82008-01-11 01:52:48 -05001438
James Smartbc739052010-08-04 16:11:18 -04001439 lpfc_heart_beat(phba, pmboxq);
1440 pmboxq->mbox_cmpl = lpfc_hb_mbox_cmpl;
1441 pmboxq->vport = phba->pport;
1442 retval = lpfc_sli_issue_mbox(phba, pmboxq,
1443 MBX_NOWAIT);
James Smart13815c82008-01-11 01:52:48 -05001444
James Smartbc739052010-08-04 16:11:18 -04001445 if (retval != MBX_BUSY &&
1446 retval != MBX_SUCCESS) {
1447 mempool_free(pmboxq,
1448 phba->mbox_mem_pool);
1449 mod_timer(&phba->hb_tmofunc,
1450 jiffies +
James Smart256ec0d2013-04-17 20:14:58 -04001451 msecs_to_jiffies(1000 *
1452 LPFC_HB_MBOX_INTERVAL));
James Smartbc739052010-08-04 16:11:18 -04001453 return;
1454 }
1455 phba->skipped_hb = 0;
1456 phba->hb_outstanding = 1;
1457 } else if (time_before_eq(phba->last_completion_time,
1458 phba->skipped_hb)) {
1459 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
1460 "2857 Last completion time not "
1461 " updated in %d ms\n",
1462 jiffies_to_msecs(jiffies
1463 - phba->last_completion_time));
1464 } else
1465 phba->skipped_hb = jiffies;
1466
James Smart858c9f62007-06-17 19:56:39 -05001467 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001468 jiffies +
1469 msecs_to_jiffies(1000 * LPFC_HB_MBOX_TIMEOUT));
James Smart858c9f62007-06-17 19:56:39 -05001470 return;
James Smart13815c82008-01-11 01:52:48 -05001471 } else {
1472 /*
1473 * If heart beat timeout called with hb_outstanding set
James Smartdcf2a4e2010-09-29 11:18:53 -04001474 * we need to give the hb mailbox cmd a chance to
1475 * complete or TMO.
James Smart13815c82008-01-11 01:52:48 -05001476 */
James Smartdcf2a4e2010-09-29 11:18:53 -04001477 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
1478 "0459 Adapter heartbeat still out"
1479 "standing:last compl time was %d ms.\n",
1480 jiffies_to_msecs(jiffies
1481 - phba->last_completion_time));
1482 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001483 jiffies +
1484 msecs_to_jiffies(1000 * LPFC_HB_MBOX_TIMEOUT));
James Smart858c9f62007-06-17 19:56:39 -05001485 }
James Smart4258e982015-12-16 18:11:58 -05001486 } else {
1487 mod_timer(&phba->hb_tmofunc,
1488 jiffies +
1489 msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smart858c9f62007-06-17 19:56:39 -05001490 }
1491}
1492
James Smarte59058c2008-08-24 21:49:00 -04001493/**
James Smart3621a712009-04-06 18:47:14 -04001494 * lpfc_offline_eratt - Bring lpfc offline on hardware error attention
James Smarte59058c2008-08-24 21:49:00 -04001495 * @phba: pointer to lpfc hba data structure.
1496 *
1497 * This routine is called to bring the HBA offline when HBA hardware error
1498 * other than Port Error 6 has been detected.
1499 **/
James Smart09372822008-01-11 01:52:54 -05001500static void
1501lpfc_offline_eratt(struct lpfc_hba *phba)
1502{
1503 struct lpfc_sli *psli = &phba->sli;
1504
1505 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001506 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart09372822008-01-11 01:52:54 -05001507 spin_unlock_irq(&phba->hbalock);
James Smart618a5232012-06-12 13:54:36 -04001508 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
James Smart09372822008-01-11 01:52:54 -05001509
1510 lpfc_offline(phba);
1511 lpfc_reset_barrier(phba);
James Smartf4b4c682009-05-22 14:53:12 -04001512 spin_lock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001513 lpfc_sli_brdreset(phba);
James Smartf4b4c682009-05-22 14:53:12 -04001514 spin_unlock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001515 lpfc_hba_down_post(phba);
1516 lpfc_sli_brdready(phba, HS_MBRDY);
1517 lpfc_unblock_mgmt_io(phba);
1518 phba->link_state = LPFC_HBA_ERROR;
1519 return;
1520}
1521
James Smarte59058c2008-08-24 21:49:00 -04001522/**
James Smartda0436e2009-05-22 14:51:39 -04001523 * lpfc_sli4_offline_eratt - Bring lpfc offline on SLI4 hardware error attention
1524 * @phba: pointer to lpfc hba data structure.
1525 *
1526 * This routine is called to bring a SLI4 HBA offline when HBA hardware error
1527 * other than Port Error 6 has been detected.
1528 **/
James Smarta88dbb62013-04-17 20:18:39 -04001529void
James Smartda0436e2009-05-22 14:51:39 -04001530lpfc_sli4_offline_eratt(struct lpfc_hba *phba)
1531{
James Smart946727d2015-04-07 15:07:09 -04001532 spin_lock_irq(&phba->hbalock);
1533 phba->link_state = LPFC_HBA_ERROR;
1534 spin_unlock_irq(&phba->hbalock);
1535
James Smart618a5232012-06-12 13:54:36 -04001536 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
James Smartda0436e2009-05-22 14:51:39 -04001537 lpfc_offline(phba);
James Smartda0436e2009-05-22 14:51:39 -04001538 lpfc_hba_down_post(phba);
James Smartda0436e2009-05-22 14:51:39 -04001539 lpfc_unblock_mgmt_io(phba);
James Smartda0436e2009-05-22 14:51:39 -04001540}
1541
1542/**
James Smarta257bf92009-04-06 18:48:10 -04001543 * lpfc_handle_deferred_eratt - The HBA hardware deferred error handler
1544 * @phba: pointer to lpfc hba data structure.
1545 *
1546 * This routine is invoked to handle the deferred HBA hardware error
1547 * conditions. This type of error is indicated by HBA by setting ER1
1548 * and another ER bit in the host status register. The driver will
1549 * wait until the ER1 bit clears before handling the error condition.
1550 **/
1551static void
1552lpfc_handle_deferred_eratt(struct lpfc_hba *phba)
1553{
1554 uint32_t old_host_status = phba->work_hs;
James Smarta257bf92009-04-06 18:48:10 -04001555 struct lpfc_sli *psli = &phba->sli;
1556
James Smartf4b4c682009-05-22 14:53:12 -04001557 /* If the pci channel is offline, ignore possible errors,
1558 * since we cannot communicate with the pci card anyway.
1559 */
1560 if (pci_channel_offline(phba->pcidev)) {
1561 spin_lock_irq(&phba->hbalock);
1562 phba->hba_flag &= ~DEFER_ERATT;
1563 spin_unlock_irq(&phba->hbalock);
1564 return;
1565 }
1566
James Smarta257bf92009-04-06 18:48:10 -04001567 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1568 "0479 Deferred Adapter Hardware Error "
1569 "Data: x%x x%x x%x\n",
1570 phba->work_hs,
1571 phba->work_status[0], phba->work_status[1]);
1572
1573 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001574 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smarta257bf92009-04-06 18:48:10 -04001575 spin_unlock_irq(&phba->hbalock);
1576
1577
1578 /*
1579 * Firmware stops when it triggred erratt. That could cause the I/Os
1580 * dropped by the firmware. Error iocb (I/O) on txcmplq and let the
1581 * SCSI layer retry it after re-establishing link.
1582 */
James Smartdb55fba2014-04-04 13:52:02 -04001583 lpfc_sli_abort_fcp_rings(phba);
James Smarta257bf92009-04-06 18:48:10 -04001584
1585 /*
1586 * There was a firmware error. Take the hba offline and then
1587 * attempt to restart it.
1588 */
James Smart618a5232012-06-12 13:54:36 -04001589 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smarta257bf92009-04-06 18:48:10 -04001590 lpfc_offline(phba);
1591
1592 /* Wait for the ER1 bit to clear.*/
1593 while (phba->work_hs & HS_FFER1) {
1594 msleep(100);
James Smart9940b972011-03-11 16:06:12 -05001595 if (lpfc_readl(phba->HSregaddr, &phba->work_hs)) {
1596 phba->work_hs = UNPLUG_ERR ;
1597 break;
1598 }
James Smarta257bf92009-04-06 18:48:10 -04001599 /* If driver is unloading let the worker thread continue */
1600 if (phba->pport->load_flag & FC_UNLOADING) {
1601 phba->work_hs = 0;
1602 break;
1603 }
1604 }
1605
1606 /*
1607 * This is to ptrotect against a race condition in which
1608 * first write to the host attention register clear the
1609 * host status register.
1610 */
1611 if ((!phba->work_hs) && (!(phba->pport->load_flag & FC_UNLOADING)))
1612 phba->work_hs = old_host_status & ~HS_FFER1;
1613
James Smart3772a992009-05-22 14:50:54 -04001614 spin_lock_irq(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -04001615 phba->hba_flag &= ~DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -04001616 spin_unlock_irq(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -04001617 phba->work_status[0] = readl(phba->MBslimaddr + 0xa8);
1618 phba->work_status[1] = readl(phba->MBslimaddr + 0xac);
1619}
1620
James Smart3772a992009-05-22 14:50:54 -04001621static void
1622lpfc_board_errevt_to_mgmt(struct lpfc_hba *phba)
1623{
1624 struct lpfc_board_event_header board_event;
1625 struct Scsi_Host *shost;
1626
1627 board_event.event_type = FC_REG_BOARD_EVENT;
1628 board_event.subcategory = LPFC_EVENT_PORTINTERR;
1629 shost = lpfc_shost_from_vport(phba->pport);
1630 fc_host_post_vendor_event(shost, fc_get_event_number(),
1631 sizeof(board_event),
1632 (char *) &board_event,
1633 LPFC_NL_VENDOR_ID);
1634}
1635
James Smarta257bf92009-04-06 18:48:10 -04001636/**
James Smart3772a992009-05-22 14:50:54 -04001637 * lpfc_handle_eratt_s3 - The SLI3 HBA hardware error handler
James Smarte59058c2008-08-24 21:49:00 -04001638 * @phba: pointer to lpfc hba data structure.
1639 *
1640 * This routine is invoked to handle the following HBA hardware error
1641 * conditions:
1642 * 1 - HBA error attention interrupt
1643 * 2 - DMA ring index out of range
1644 * 3 - Mailbox command came back as unknown
1645 **/
James Smart3772a992009-05-22 14:50:54 -04001646static void
1647lpfc_handle_eratt_s3(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001648{
James Smart2e0fef82007-06-17 19:56:36 -05001649 struct lpfc_vport *vport = phba->pport;
James Smart2e0fef82007-06-17 19:56:36 -05001650 struct lpfc_sli *psli = &phba->sli;
James Smartd2873e42006-08-18 17:46:43 -04001651 uint32_t event_data;
James Smart57127f12007-10-27 13:37:05 -04001652 unsigned long temperature;
1653 struct temp_event temp_event_data;
James Smart92d7f7b2007-06-17 19:56:38 -05001654 struct Scsi_Host *shost;
James Smart2e0fef82007-06-17 19:56:36 -05001655
Linas Vepstas8d63f372007-02-14 14:28:36 -06001656 /* If the pci channel is offline, ignore possible errors,
James Smart3772a992009-05-22 14:50:54 -04001657 * since we cannot communicate with the pci card anyway.
1658 */
1659 if (pci_channel_offline(phba->pcidev)) {
1660 spin_lock_irq(&phba->hbalock);
1661 phba->hba_flag &= ~DEFER_ERATT;
1662 spin_unlock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -06001663 return;
James Smart3772a992009-05-22 14:50:54 -04001664 }
1665
James Smart13815c82008-01-11 01:52:48 -05001666 /* If resets are disabled then leave the HBA alone and return */
1667 if (!phba->cfg_enable_hba_reset)
1668 return;
dea31012005-04-17 16:05:31 -05001669
James Smartea2151b2008-09-07 11:52:10 -04001670 /* Send an internal error event to mgmt application */
James Smart3772a992009-05-22 14:50:54 -04001671 lpfc_board_errevt_to_mgmt(phba);
James Smartea2151b2008-09-07 11:52:10 -04001672
James Smarta257bf92009-04-06 18:48:10 -04001673 if (phba->hba_flag & DEFER_ERATT)
1674 lpfc_handle_deferred_eratt(phba);
1675
James Smartdcf2a4e2010-09-29 11:18:53 -04001676 if ((phba->work_hs & HS_FFER6) || (phba->work_hs & HS_FFER8)) {
1677 if (phba->work_hs & HS_FFER6)
1678 /* Re-establishing Link */
1679 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1680 "1301 Re-establishing Link "
1681 "Data: x%x x%x x%x\n",
1682 phba->work_hs, phba->work_status[0],
1683 phba->work_status[1]);
1684 if (phba->work_hs & HS_FFER8)
1685 /* Device Zeroization */
1686 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1687 "2861 Host Authentication device "
1688 "zeroization Data:x%x x%x x%x\n",
1689 phba->work_hs, phba->work_status[0],
1690 phba->work_status[1]);
James Smart58da1ff2008-04-07 10:15:56 -04001691
James Smart92d7f7b2007-06-17 19:56:38 -05001692 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001693 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -05001694 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001695
1696 /*
1697 * Firmware stops when it triggled erratt with HS_FFER6.
1698 * That could cause the I/Os dropped by the firmware.
1699 * Error iocb (I/O) on txcmplq and let the SCSI layer
1700 * retry it after re-establishing link.
1701 */
James Smartdb55fba2014-04-04 13:52:02 -04001702 lpfc_sli_abort_fcp_rings(phba);
dea31012005-04-17 16:05:31 -05001703
dea31012005-04-17 16:05:31 -05001704 /*
1705 * There was a firmware error. Take the hba offline and then
1706 * attempt to restart it.
1707 */
James Smart618a5232012-06-12 13:54:36 -04001708 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
dea31012005-04-17 16:05:31 -05001709 lpfc_offline(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -05001710 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05001711 if (lpfc_online(phba) == 0) { /* Initialize the HBA */
James Smart46fa3112007-04-25 09:51:45 -04001712 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05001713 return;
1714 }
James Smart46fa3112007-04-25 09:51:45 -04001715 lpfc_unblock_mgmt_io(phba);
James Smart57127f12007-10-27 13:37:05 -04001716 } else if (phba->work_hs & HS_CRIT_TEMP) {
1717 temperature = readl(phba->MBslimaddr + TEMPERATURE_OFFSET);
1718 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
1719 temp_event_data.event_code = LPFC_CRIT_TEMP;
1720 temp_event_data.data = (uint32_t)temperature;
1721
1722 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -04001723 "0406 Adapter maximum temperature exceeded "
James Smart57127f12007-10-27 13:37:05 -04001724 "(%ld), taking this port offline "
1725 "Data: x%x x%x x%x\n",
1726 temperature, phba->work_hs,
1727 phba->work_status[0], phba->work_status[1]);
1728
1729 shost = lpfc_shost_from_vport(phba->pport);
1730 fc_host_post_vendor_event(shost, fc_get_event_number(),
1731 sizeof(temp_event_data),
1732 (char *) &temp_event_data,
1733 SCSI_NL_VID_TYPE_PCI
1734 | PCI_VENDOR_ID_EMULEX);
1735
James Smart7af67052007-10-27 13:38:11 -04001736 spin_lock_irq(&phba->hbalock);
James Smart7af67052007-10-27 13:38:11 -04001737 phba->over_temp_state = HBA_OVER_TEMP;
1738 spin_unlock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001739 lpfc_offline_eratt(phba);
James Smart57127f12007-10-27 13:37:05 -04001740
dea31012005-04-17 16:05:31 -05001741 } else {
1742 /* The if clause above forces this code path when the status
James Smart93996272008-08-24 21:50:30 -04001743 * failure is a value other than FFER6. Do not call the offline
1744 * twice. This is the adapter hardware error path.
dea31012005-04-17 16:05:31 -05001745 */
1746 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001747 "0457 Adapter Hardware Error "
dea31012005-04-17 16:05:31 -05001748 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001749 phba->work_hs,
dea31012005-04-17 16:05:31 -05001750 phba->work_status[0], phba->work_status[1]);
1751
James Smartd2873e42006-08-18 17:46:43 -04001752 event_data = FC_REG_DUMP_EVENT;
James Smart92d7f7b2007-06-17 19:56:38 -05001753 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05001754 fc_host_post_vendor_event(shost, fc_get_event_number(),
James Smartd2873e42006-08-18 17:46:43 -04001755 sizeof(event_data), (char *) &event_data,
1756 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
1757
James Smart09372822008-01-11 01:52:54 -05001758 lpfc_offline_eratt(phba);
dea31012005-04-17 16:05:31 -05001759 }
James Smart93996272008-08-24 21:50:30 -04001760 return;
dea31012005-04-17 16:05:31 -05001761}
1762
James Smarte59058c2008-08-24 21:49:00 -04001763/**
James Smart618a5232012-06-12 13:54:36 -04001764 * lpfc_sli4_port_sta_fn_reset - The SLI4 function reset due to port status reg
1765 * @phba: pointer to lpfc hba data structure.
1766 * @mbx_action: flag for mailbox shutdown action.
1767 *
1768 * This routine is invoked to perform an SLI4 port PCI function reset in
1769 * response to port status register polling attention. It waits for port
1770 * status register (ERR, RDY, RN) bits before proceeding with function reset.
1771 * During this process, interrupt vectors are freed and later requested
1772 * for handling possible port resource change.
1773 **/
1774static int
James Smarte10b2022014-02-20 09:57:43 -05001775lpfc_sli4_port_sta_fn_reset(struct lpfc_hba *phba, int mbx_action,
1776 bool en_rn_msg)
James Smart618a5232012-06-12 13:54:36 -04001777{
1778 int rc;
1779 uint32_t intr_mode;
1780
James Smart27d6ac02018-02-22 08:18:42 -08001781 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) >=
James Smart65791f12016-07-06 12:35:56 -07001782 LPFC_SLI_INTF_IF_TYPE_2) {
1783 /*
1784 * On error status condition, driver need to wait for port
1785 * ready before performing reset.
1786 */
1787 rc = lpfc_sli4_pdev_status_reg_wait(phba);
James Smart0e916ee2016-07-06 12:36:06 -07001788 if (rc)
James Smart65791f12016-07-06 12:35:56 -07001789 return rc;
James Smart618a5232012-06-12 13:54:36 -04001790 }
James Smart0e916ee2016-07-06 12:36:06 -07001791
James Smart65791f12016-07-06 12:35:56 -07001792 /* need reset: attempt for port recovery */
1793 if (en_rn_msg)
1794 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1795 "2887 Reset Needed: Attempting Port "
1796 "Recovery...\n");
1797 lpfc_offline_prep(phba, mbx_action);
1798 lpfc_offline(phba);
1799 /* release interrupt for possible resource change */
1800 lpfc_sli4_disable_intr(phba);
James Smart5a9eeff2018-11-29 16:09:32 -08001801 rc = lpfc_sli_brdrestart(phba);
1802 if (rc) {
1803 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1804 "6309 Failed to restart board\n");
1805 return rc;
1806 }
James Smart65791f12016-07-06 12:35:56 -07001807 /* request and enable interrupt */
1808 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
1809 if (intr_mode == LPFC_INTR_ERROR) {
1810 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1811 "3175 Failed to enable interrupt\n");
1812 return -EIO;
1813 }
1814 phba->intr_mode = intr_mode;
1815 rc = lpfc_online(phba);
1816 if (rc == 0)
1817 lpfc_unblock_mgmt_io(phba);
1818
James Smart618a5232012-06-12 13:54:36 -04001819 return rc;
1820}
1821
1822/**
James Smartda0436e2009-05-22 14:51:39 -04001823 * lpfc_handle_eratt_s4 - The SLI4 HBA hardware error handler
1824 * @phba: pointer to lpfc hba data structure.
1825 *
1826 * This routine is invoked to handle the SLI4 HBA hardware error attention
1827 * conditions.
1828 **/
1829static void
1830lpfc_handle_eratt_s4(struct lpfc_hba *phba)
1831{
1832 struct lpfc_vport *vport = phba->pport;
1833 uint32_t event_data;
1834 struct Scsi_Host *shost;
James Smart2fcee4b2010-12-15 17:57:46 -05001835 uint32_t if_type;
James Smart2e90f4b2011-12-13 13:22:37 -05001836 struct lpfc_register portstat_reg = {0};
1837 uint32_t reg_err1, reg_err2;
1838 uint32_t uerrlo_reg, uemasklo_reg;
James Smart65791f12016-07-06 12:35:56 -07001839 uint32_t smphr_port_status = 0, pci_rd_rc1, pci_rd_rc2;
James Smarte10b2022014-02-20 09:57:43 -05001840 bool en_rn_msg = true;
James Smart946727d2015-04-07 15:07:09 -04001841 struct temp_event temp_event_data;
James Smart65791f12016-07-06 12:35:56 -07001842 struct lpfc_register portsmphr_reg;
1843 int rc, i;
James Smartda0436e2009-05-22 14:51:39 -04001844
1845 /* If the pci channel is offline, ignore possible errors, since
1846 * we cannot communicate with the pci card anyway.
1847 */
James Smart32a93102019-03-12 16:30:13 -07001848 if (pci_channel_offline(phba->pcidev)) {
1849 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1850 "3166 pci channel is offline\n");
1851 lpfc_sli4_offline_eratt(phba);
James Smartda0436e2009-05-22 14:51:39 -04001852 return;
James Smart32a93102019-03-12 16:30:13 -07001853 }
James Smartda0436e2009-05-22 14:51:39 -04001854
James Smart65791f12016-07-06 12:35:56 -07001855 memset(&portsmphr_reg, 0, sizeof(portsmphr_reg));
James Smart2fcee4b2010-12-15 17:57:46 -05001856 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
1857 switch (if_type) {
1858 case LPFC_SLI_INTF_IF_TYPE_0:
James Smart2e90f4b2011-12-13 13:22:37 -05001859 pci_rd_rc1 = lpfc_readl(
1860 phba->sli4_hba.u.if_type0.UERRLOregaddr,
1861 &uerrlo_reg);
1862 pci_rd_rc2 = lpfc_readl(
1863 phba->sli4_hba.u.if_type0.UEMASKLOregaddr,
1864 &uemasklo_reg);
1865 /* consider PCI bus read error as pci_channel_offline */
1866 if (pci_rd_rc1 == -EIO && pci_rd_rc2 == -EIO)
1867 return;
James Smart65791f12016-07-06 12:35:56 -07001868 if (!(phba->hba_flag & HBA_RECOVERABLE_UE)) {
1869 lpfc_sli4_offline_eratt(phba);
1870 return;
1871 }
1872 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1873 "7623 Checking UE recoverable");
1874
1875 for (i = 0; i < phba->sli4_hba.ue_to_sr / 1000; i++) {
1876 if (lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
1877 &portsmphr_reg.word0))
1878 continue;
1879
1880 smphr_port_status = bf_get(lpfc_port_smphr_port_status,
1881 &portsmphr_reg);
1882 if ((smphr_port_status & LPFC_PORT_SEM_MASK) ==
1883 LPFC_PORT_SEM_UE_RECOVERABLE)
1884 break;
1885 /*Sleep for 1Sec, before checking SEMAPHORE */
1886 msleep(1000);
1887 }
1888
1889 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1890 "4827 smphr_port_status x%x : Waited %dSec",
1891 smphr_port_status, i);
1892
1893 /* Recoverable UE, reset the HBA device */
1894 if ((smphr_port_status & LPFC_PORT_SEM_MASK) ==
1895 LPFC_PORT_SEM_UE_RECOVERABLE) {
1896 for (i = 0; i < 20; i++) {
1897 msleep(1000);
1898 if (!lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
1899 &portsmphr_reg.word0) &&
1900 (LPFC_POST_STAGE_PORT_READY ==
1901 bf_get(lpfc_port_smphr_port_status,
1902 &portsmphr_reg))) {
1903 rc = lpfc_sli4_port_sta_fn_reset(phba,
1904 LPFC_MBX_NO_WAIT, en_rn_msg);
1905 if (rc == 0)
1906 return;
1907 lpfc_printf_log(phba,
1908 KERN_ERR, LOG_INIT,
1909 "4215 Failed to recover UE");
1910 break;
1911 }
1912 }
1913 }
1914 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1915 "7624 Firmware not ready: Failing UE recovery,"
1916 " waited %dSec", i);
James Smart2fcee4b2010-12-15 17:57:46 -05001917 lpfc_sli4_offline_eratt(phba);
1918 break;
James Smart946727d2015-04-07 15:07:09 -04001919
James Smart2fcee4b2010-12-15 17:57:46 -05001920 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart27d6ac02018-02-22 08:18:42 -08001921 case LPFC_SLI_INTF_IF_TYPE_6:
James Smart2e90f4b2011-12-13 13:22:37 -05001922 pci_rd_rc1 = lpfc_readl(
1923 phba->sli4_hba.u.if_type2.STATUSregaddr,
1924 &portstat_reg.word0);
1925 /* consider PCI bus read error as pci_channel_offline */
James Smart6b5151f2012-01-18 16:24:06 -05001926 if (pci_rd_rc1 == -EIO) {
1927 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1928 "3151 PCI bus read access failure: x%x\n",
1929 readl(phba->sli4_hba.u.if_type2.STATUSregaddr));
James Smart32a93102019-03-12 16:30:13 -07001930 lpfc_sli4_offline_eratt(phba);
James Smart2e90f4b2011-12-13 13:22:37 -05001931 return;
James Smart6b5151f2012-01-18 16:24:06 -05001932 }
James Smart2e90f4b2011-12-13 13:22:37 -05001933 reg_err1 = readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
1934 reg_err2 = readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
James Smart2fcee4b2010-12-15 17:57:46 -05001935 if (bf_get(lpfc_sliport_status_oti, &portstat_reg)) {
James Smart2fcee4b2010-12-15 17:57:46 -05001936 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1937 "2889 Port Overtemperature event, "
James Smart946727d2015-04-07 15:07:09 -04001938 "taking port offline Data: x%x x%x\n",
1939 reg_err1, reg_err2);
1940
James Smart310429e2016-07-06 12:35:54 -07001941 phba->sfp_alarm |= LPFC_TRANSGRESSION_HIGH_TEMPERATURE;
James Smart946727d2015-04-07 15:07:09 -04001942 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
1943 temp_event_data.event_code = LPFC_CRIT_TEMP;
1944 temp_event_data.data = 0xFFFFFFFF;
1945
1946 shost = lpfc_shost_from_vport(phba->pport);
1947 fc_host_post_vendor_event(shost, fc_get_event_number(),
1948 sizeof(temp_event_data),
1949 (char *)&temp_event_data,
1950 SCSI_NL_VID_TYPE_PCI
1951 | PCI_VENDOR_ID_EMULEX);
1952
James Smart2fcee4b2010-12-15 17:57:46 -05001953 spin_lock_irq(&phba->hbalock);
1954 phba->over_temp_state = HBA_OVER_TEMP;
1955 spin_unlock_irq(&phba->hbalock);
1956 lpfc_sli4_offline_eratt(phba);
James Smart946727d2015-04-07 15:07:09 -04001957 return;
James Smart2fcee4b2010-12-15 17:57:46 -05001958 }
James Smart2e90f4b2011-12-13 13:22:37 -05001959 if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
James Smarte10b2022014-02-20 09:57:43 -05001960 reg_err2 == SLIPORT_ERR2_REG_FW_RESTART) {
James Smart2e90f4b2011-12-13 13:22:37 -05001961 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte10b2022014-02-20 09:57:43 -05001962 "3143 Port Down: Firmware Update "
1963 "Detected\n");
1964 en_rn_msg = false;
1965 } else if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
James Smart2e90f4b2011-12-13 13:22:37 -05001966 reg_err2 == SLIPORT_ERR2_REG_FORCED_DUMP)
1967 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1968 "3144 Port Down: Debug Dump\n");
1969 else if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1970 reg_err2 == SLIPORT_ERR2_REG_FUNC_PROVISON)
1971 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1972 "3145 Port Down: Provisioning\n");
James Smart618a5232012-06-12 13:54:36 -04001973
James Smart946727d2015-04-07 15:07:09 -04001974 /* If resets are disabled then leave the HBA alone and return */
1975 if (!phba->cfg_enable_hba_reset)
1976 return;
1977
James Smart618a5232012-06-12 13:54:36 -04001978 /* Check port status register for function reset */
James Smarte10b2022014-02-20 09:57:43 -05001979 rc = lpfc_sli4_port_sta_fn_reset(phba, LPFC_MBX_NO_WAIT,
1980 en_rn_msg);
James Smart618a5232012-06-12 13:54:36 -04001981 if (rc == 0) {
1982 /* don't report event on forced debug dump */
1983 if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1984 reg_err2 == SLIPORT_ERR2_REG_FORCED_DUMP)
1985 return;
1986 else
1987 break;
James Smart2fcee4b2010-12-15 17:57:46 -05001988 }
James Smart618a5232012-06-12 13:54:36 -04001989 /* fall through for not able to recover */
James Smart6b5151f2012-01-18 16:24:06 -05001990 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1991 "3152 Unrecoverable error, bring the port "
1992 "offline\n");
James Smart2fcee4b2010-12-15 17:57:46 -05001993 lpfc_sli4_offline_eratt(phba);
1994 break;
1995 case LPFC_SLI_INTF_IF_TYPE_1:
1996 default:
1997 break;
1998 }
James Smart2e90f4b2011-12-13 13:22:37 -05001999 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
2000 "3123 Report dump event to upper layer\n");
2001 /* Send an internal error event to mgmt application */
2002 lpfc_board_errevt_to_mgmt(phba);
2003
2004 event_data = FC_REG_DUMP_EVENT;
2005 shost = lpfc_shost_from_vport(vport);
2006 fc_host_post_vendor_event(shost, fc_get_event_number(),
2007 sizeof(event_data), (char *) &event_data,
2008 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
James Smartda0436e2009-05-22 14:51:39 -04002009}
2010
2011/**
2012 * lpfc_handle_eratt - Wrapper func for handling hba error attention
2013 * @phba: pointer to lpfc HBA data structure.
2014 *
2015 * This routine wraps the actual SLI3 or SLI4 hba error attention handling
2016 * routine from the API jump table function pointer from the lpfc_hba struct.
2017 *
2018 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002019 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -04002020 * Any other value - error.
2021 **/
2022void
2023lpfc_handle_eratt(struct lpfc_hba *phba)
2024{
2025 (*phba->lpfc_handle_eratt)(phba);
2026}
2027
2028/**
James Smart3621a712009-04-06 18:47:14 -04002029 * lpfc_handle_latt - The HBA link event handler
James Smarte59058c2008-08-24 21:49:00 -04002030 * @phba: pointer to lpfc hba data structure.
2031 *
2032 * This routine is invoked from the worker thread to handle a HBA host
James Smart895427b2017-02-12 13:52:30 -08002033 * attention link event. SLI3 only.
James Smarte59058c2008-08-24 21:49:00 -04002034 **/
dea31012005-04-17 16:05:31 -05002035void
James Smart2e0fef82007-06-17 19:56:36 -05002036lpfc_handle_latt(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002037{
James Smart2e0fef82007-06-17 19:56:36 -05002038 struct lpfc_vport *vport = phba->pport;
2039 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05002040 LPFC_MBOXQ_t *pmb;
2041 volatile uint32_t control;
2042 struct lpfc_dmabuf *mp;
James Smart09372822008-01-11 01:52:54 -05002043 int rc = 0;
dea31012005-04-17 16:05:31 -05002044
2045 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart09372822008-01-11 01:52:54 -05002046 if (!pmb) {
2047 rc = 1;
dea31012005-04-17 16:05:31 -05002048 goto lpfc_handle_latt_err_exit;
James Smart09372822008-01-11 01:52:54 -05002049 }
dea31012005-04-17 16:05:31 -05002050
2051 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
James Smart09372822008-01-11 01:52:54 -05002052 if (!mp) {
2053 rc = 2;
dea31012005-04-17 16:05:31 -05002054 goto lpfc_handle_latt_free_pmb;
James Smart09372822008-01-11 01:52:54 -05002055 }
dea31012005-04-17 16:05:31 -05002056
2057 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
James Smart09372822008-01-11 01:52:54 -05002058 if (!mp->virt) {
2059 rc = 3;
dea31012005-04-17 16:05:31 -05002060 goto lpfc_handle_latt_free_mp;
James Smart09372822008-01-11 01:52:54 -05002061 }
dea31012005-04-17 16:05:31 -05002062
James.Smart@Emulex.Com6281bfe2005-11-28 11:41:33 -05002063 /* Cleanup any outstanding ELS commands */
James Smart549e55c2007-08-02 11:09:51 -04002064 lpfc_els_flush_all_cmd(phba);
dea31012005-04-17 16:05:31 -05002065
2066 psli->slistat.link_event++;
James Smart76a95d72010-11-20 23:11:48 -05002067 lpfc_read_topology(phba, pmb, mp);
2068 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
James Smart2e0fef82007-06-17 19:56:36 -05002069 pmb->vport = vport;
James Smart0d2b6b82008-06-14 22:52:47 -04002070 /* Block ELS IOCBs until we have processed this mbox command */
James Smart895427b2017-02-12 13:52:30 -08002071 phba->sli.sli3_ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
James Smart0b727fe2007-10-27 13:37:25 -04002072 rc = lpfc_sli_issue_mbox (phba, pmb, MBX_NOWAIT);
James Smart09372822008-01-11 01:52:54 -05002073 if (rc == MBX_NOT_FINISHED) {
2074 rc = 4;
James Smart14691152006-12-02 13:34:28 -05002075 goto lpfc_handle_latt_free_mbuf;
James Smart09372822008-01-11 01:52:54 -05002076 }
dea31012005-04-17 16:05:31 -05002077
2078 /* Clear Link Attention in HA REG */
James Smart2e0fef82007-06-17 19:56:36 -05002079 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002080 writel(HA_LATT, phba->HAregaddr);
2081 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05002082 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002083
2084 return;
2085
James Smart14691152006-12-02 13:34:28 -05002086lpfc_handle_latt_free_mbuf:
James Smart895427b2017-02-12 13:52:30 -08002087 phba->sli.sli3_ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart14691152006-12-02 13:34:28 -05002088 lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea31012005-04-17 16:05:31 -05002089lpfc_handle_latt_free_mp:
2090 kfree(mp);
2091lpfc_handle_latt_free_pmb:
James Smart1dcb58e2007-04-25 09:51:30 -04002092 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002093lpfc_handle_latt_err_exit:
2094 /* Enable Link attention interrupts */
James Smart2e0fef82007-06-17 19:56:36 -05002095 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002096 psli->sli_flag |= LPFC_PROCESS_LA;
2097 control = readl(phba->HCregaddr);
2098 control |= HC_LAINT_ENA;
2099 writel(control, phba->HCregaddr);
2100 readl(phba->HCregaddr); /* flush */
2101
2102 /* Clear Link Attention in HA REG */
2103 writel(HA_LATT, phba->HAregaddr);
2104 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05002105 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002106 lpfc_linkdown(phba);
James Smart2e0fef82007-06-17 19:56:36 -05002107 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002108
James Smart09372822008-01-11 01:52:54 -05002109 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
2110 "0300 LATT: Cannot issue READ_LA: Data:%d\n", rc);
dea31012005-04-17 16:05:31 -05002111
2112 return;
2113}
2114
James Smarte59058c2008-08-24 21:49:00 -04002115/**
James Smart3621a712009-04-06 18:47:14 -04002116 * lpfc_parse_vpd - Parse VPD (Vital Product Data)
James Smarte59058c2008-08-24 21:49:00 -04002117 * @phba: pointer to lpfc hba data structure.
2118 * @vpd: pointer to the vital product data.
2119 * @len: length of the vital product data in bytes.
2120 *
2121 * This routine parses the Vital Product Data (VPD). The VPD is treated as
2122 * an array of characters. In this routine, the ModelName, ProgramType, and
2123 * ModelDesc, etc. fields of the phba data structure will be populated.
2124 *
2125 * Return codes
2126 * 0 - pointer to the VPD passed in is NULL
2127 * 1 - success
2128 **/
James Smart3772a992009-05-22 14:50:54 -04002129int
James Smart2e0fef82007-06-17 19:56:36 -05002130lpfc_parse_vpd(struct lpfc_hba *phba, uint8_t *vpd, int len)
dea31012005-04-17 16:05:31 -05002131{
2132 uint8_t lenlo, lenhi;
Anton Blanchard07da60c2007-03-21 08:41:47 -05002133 int Length;
dea31012005-04-17 16:05:31 -05002134 int i, j;
2135 int finished = 0;
2136 int index = 0;
2137
2138 if (!vpd)
2139 return 0;
2140
2141 /* Vital Product */
James Smarted957682007-06-17 19:56:37 -05002142 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002143 "0455 Vital Product Data: x%x x%x x%x x%x\n",
dea31012005-04-17 16:05:31 -05002144 (uint32_t) vpd[0], (uint32_t) vpd[1], (uint32_t) vpd[2],
2145 (uint32_t) vpd[3]);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002146 while (!finished && (index < (len - 4))) {
dea31012005-04-17 16:05:31 -05002147 switch (vpd[index]) {
2148 case 0x82:
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002149 case 0x91:
dea31012005-04-17 16:05:31 -05002150 index += 1;
2151 lenlo = vpd[index];
2152 index += 1;
2153 lenhi = vpd[index];
2154 index += 1;
2155 i = ((((unsigned short)lenhi) << 8) + lenlo);
2156 index += i;
2157 break;
2158 case 0x90:
2159 index += 1;
2160 lenlo = vpd[index];
2161 index += 1;
2162 lenhi = vpd[index];
2163 index += 1;
2164 Length = ((((unsigned short)lenhi) << 8) + lenlo);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002165 if (Length > len - index)
2166 Length = len - index;
dea31012005-04-17 16:05:31 -05002167 while (Length > 0) {
2168 /* Look for Serial Number */
2169 if ((vpd[index] == 'S') && (vpd[index+1] == 'N')) {
2170 index += 2;
2171 i = vpd[index];
2172 index += 1;
2173 j = 0;
2174 Length -= (3+i);
2175 while(i--) {
2176 phba->SerialNumber[j++] = vpd[index++];
2177 if (j == 31)
2178 break;
2179 }
2180 phba->SerialNumber[j] = 0;
2181 continue;
2182 }
2183 else if ((vpd[index] == 'V') && (vpd[index+1] == '1')) {
2184 phba->vpd_flag |= VPD_MODEL_DESC;
2185 index += 2;
2186 i = vpd[index];
2187 index += 1;
2188 j = 0;
2189 Length -= (3+i);
2190 while(i--) {
2191 phba->ModelDesc[j++] = vpd[index++];
2192 if (j == 255)
2193 break;
2194 }
2195 phba->ModelDesc[j] = 0;
2196 continue;
2197 }
2198 else if ((vpd[index] == 'V') && (vpd[index+1] == '2')) {
2199 phba->vpd_flag |= VPD_MODEL_NAME;
2200 index += 2;
2201 i = vpd[index];
2202 index += 1;
2203 j = 0;
2204 Length -= (3+i);
2205 while(i--) {
2206 phba->ModelName[j++] = vpd[index++];
2207 if (j == 79)
2208 break;
2209 }
2210 phba->ModelName[j] = 0;
2211 continue;
2212 }
2213 else if ((vpd[index] == 'V') && (vpd[index+1] == '3')) {
2214 phba->vpd_flag |= VPD_PROGRAM_TYPE;
2215 index += 2;
2216 i = vpd[index];
2217 index += 1;
2218 j = 0;
2219 Length -= (3+i);
2220 while(i--) {
2221 phba->ProgramType[j++] = vpd[index++];
2222 if (j == 255)
2223 break;
2224 }
2225 phba->ProgramType[j] = 0;
2226 continue;
2227 }
2228 else if ((vpd[index] == 'V') && (vpd[index+1] == '4')) {
2229 phba->vpd_flag |= VPD_PORT;
2230 index += 2;
2231 i = vpd[index];
2232 index += 1;
2233 j = 0;
2234 Length -= (3+i);
2235 while(i--) {
James Smartcd1c8302011-10-10 21:33:25 -04002236 if ((phba->sli_rev == LPFC_SLI_REV4) &&
2237 (phba->sli4_hba.pport_name_sta ==
2238 LPFC_SLI4_PPNAME_GET)) {
2239 j++;
2240 index++;
2241 } else
2242 phba->Port[j++] = vpd[index++];
2243 if (j == 19)
2244 break;
dea31012005-04-17 16:05:31 -05002245 }
James Smartcd1c8302011-10-10 21:33:25 -04002246 if ((phba->sli_rev != LPFC_SLI_REV4) ||
2247 (phba->sli4_hba.pport_name_sta ==
2248 LPFC_SLI4_PPNAME_NON))
2249 phba->Port[j] = 0;
dea31012005-04-17 16:05:31 -05002250 continue;
2251 }
2252 else {
2253 index += 2;
2254 i = vpd[index];
2255 index += 1;
2256 index += i;
2257 Length -= (3 + i);
2258 }
2259 }
2260 finished = 0;
2261 break;
2262 case 0x78:
2263 finished = 1;
2264 break;
2265 default:
2266 index ++;
2267 break;
2268 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002269 }
dea31012005-04-17 16:05:31 -05002270
2271 return(1);
2272}
2273
James Smarte59058c2008-08-24 21:49:00 -04002274/**
James Smart3621a712009-04-06 18:47:14 -04002275 * lpfc_get_hba_model_desc - Retrieve HBA device model name and description
James Smarte59058c2008-08-24 21:49:00 -04002276 * @phba: pointer to lpfc hba data structure.
2277 * @mdp: pointer to the data structure to hold the derived model name.
2278 * @descp: pointer to the data structure to hold the derived description.
2279 *
2280 * This routine retrieves HBA's description based on its registered PCI device
2281 * ID. The @descp passed into this function points to an array of 256 chars. It
2282 * shall be returned with the model name, maximum speed, and the host bus type.
2283 * The @mdp passed into this function points to an array of 80 chars. When the
2284 * function returns, the @mdp will be filled with the model name.
2285 **/
dea31012005-04-17 16:05:31 -05002286static void
James Smart2e0fef82007-06-17 19:56:36 -05002287lpfc_get_hba_model_desc(struct lpfc_hba *phba, uint8_t *mdp, uint8_t *descp)
dea31012005-04-17 16:05:31 -05002288{
2289 lpfc_vpd_t *vp;
James.Smart@Emulex.Comfefcb2b2005-11-28 15:08:56 -05002290 uint16_t dev_id = phba->pcidev->device;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002291 int max_speed;
James Smart84774a42008-08-24 21:50:06 -04002292 int GE = 0;
James Smartda0436e2009-05-22 14:51:39 -04002293 int oneConnect = 0; /* default is not a oneConnect */
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002294 struct {
James Smarta747c9c2009-11-18 15:41:10 -05002295 char *name;
2296 char *bus;
2297 char *function;
2298 } m = {"<Unknown>", "", ""};
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002299
2300 if (mdp && mdp[0] != '\0'
2301 && descp && descp[0] != '\0')
2302 return;
2303
James Smartfbd8a6b2018-02-22 08:18:45 -08002304 if (phba->lmt & LMT_64Gb)
2305 max_speed = 64;
2306 else if (phba->lmt & LMT_32Gb)
James Smartd38dd522015-08-31 16:48:17 -04002307 max_speed = 32;
2308 else if (phba->lmt & LMT_16Gb)
James Smartc0c11512011-05-24 11:41:34 -04002309 max_speed = 16;
2310 else if (phba->lmt & LMT_10Gb)
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002311 max_speed = 10;
2312 else if (phba->lmt & LMT_8Gb)
2313 max_speed = 8;
2314 else if (phba->lmt & LMT_4Gb)
2315 max_speed = 4;
2316 else if (phba->lmt & LMT_2Gb)
2317 max_speed = 2;
James Smart4169d862012-09-29 11:32:09 -04002318 else if (phba->lmt & LMT_1Gb)
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002319 max_speed = 1;
James Smart4169d862012-09-29 11:32:09 -04002320 else
2321 max_speed = 0;
dea31012005-04-17 16:05:31 -05002322
2323 vp = &phba->vpd;
dea31012005-04-17 16:05:31 -05002324
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002325 switch (dev_id) {
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04002326 case PCI_DEVICE_ID_FIREFLY:
James Smart12222f42014-05-21 08:05:19 -04002327 m = (typeof(m)){"LP6000", "PCI",
2328 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04002329 break;
dea31012005-04-17 16:05:31 -05002330 case PCI_DEVICE_ID_SUPERFLY:
2331 if (vp->rev.biuRev >= 1 && vp->rev.biuRev <= 3)
James Smart12222f42014-05-21 08:05:19 -04002332 m = (typeof(m)){"LP7000", "PCI", ""};
dea31012005-04-17 16:05:31 -05002333 else
James Smart12222f42014-05-21 08:05:19 -04002334 m = (typeof(m)){"LP7000E", "PCI", ""};
2335 m.function = "Obsolete, Unsupported Fibre Channel Adapter";
dea31012005-04-17 16:05:31 -05002336 break;
2337 case PCI_DEVICE_ID_DRAGONFLY:
James Smarta747c9c2009-11-18 15:41:10 -05002338 m = (typeof(m)){"LP8000", "PCI",
James Smart12222f42014-05-21 08:05:19 -04002339 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002340 break;
2341 case PCI_DEVICE_ID_CENTAUR:
2342 if (FC_JEDEC_ID(vp->rev.biuRev) == CENTAUR_2G_JEDEC_ID)
James Smart12222f42014-05-21 08:05:19 -04002343 m = (typeof(m)){"LP9002", "PCI", ""};
dea31012005-04-17 16:05:31 -05002344 else
James Smart12222f42014-05-21 08:05:19 -04002345 m = (typeof(m)){"LP9000", "PCI", ""};
2346 m.function = "Obsolete, Unsupported Fibre Channel Adapter";
dea31012005-04-17 16:05:31 -05002347 break;
2348 case PCI_DEVICE_ID_RFLY:
James Smarta747c9c2009-11-18 15:41:10 -05002349 m = (typeof(m)){"LP952", "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_PEGASUS:
James Smarta747c9c2009-11-18 15:41:10 -05002353 m = (typeof(m)){"LP9802", "PCI-X",
James Smart12222f42014-05-21 08:05:19 -04002354 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002355 break;
2356 case PCI_DEVICE_ID_THOR:
James Smarta747c9c2009-11-18 15:41:10 -05002357 m = (typeof(m)){"LP10000", "PCI-X",
James Smart12222f42014-05-21 08:05:19 -04002358 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002359 break;
2360 case PCI_DEVICE_ID_VIPER:
James Smarta747c9c2009-11-18 15:41:10 -05002361 m = (typeof(m)){"LPX1000", "PCI-X",
James Smart12222f42014-05-21 08:05:19 -04002362 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002363 break;
2364 case PCI_DEVICE_ID_PFLY:
James Smarta747c9c2009-11-18 15:41:10 -05002365 m = (typeof(m)){"LP982", "PCI-X",
James Smart12222f42014-05-21 08:05:19 -04002366 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002367 break;
2368 case PCI_DEVICE_ID_TFLY:
James Smarta747c9c2009-11-18 15:41:10 -05002369 m = (typeof(m)){"LP1050", "PCI-X",
James Smart12222f42014-05-21 08:05:19 -04002370 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002371 break;
2372 case PCI_DEVICE_ID_HELIOS:
James Smarta747c9c2009-11-18 15:41:10 -05002373 m = (typeof(m)){"LP11000", "PCI-X2",
James Smart12222f42014-05-21 08:05:19 -04002374 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002375 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002376 case PCI_DEVICE_ID_HELIOS_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002377 m = (typeof(m)){"LP11000-SP", "PCI-X2",
James Smart12222f42014-05-21 08:05:19 -04002378 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002379 break;
2380 case PCI_DEVICE_ID_HELIOS_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002381 m = (typeof(m)){"LP11002-SP", "PCI-X2",
James Smart12222f42014-05-21 08:05:19 -04002382 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002383 break;
2384 case PCI_DEVICE_ID_NEPTUNE:
James Smart12222f42014-05-21 08:05:19 -04002385 m = (typeof(m)){"LPe1000", "PCIe",
2386 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002387 break;
2388 case PCI_DEVICE_ID_NEPTUNE_SCSP:
James Smart12222f42014-05-21 08:05:19 -04002389 m = (typeof(m)){"LPe1000-SP", "PCIe",
2390 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002391 break;
2392 case PCI_DEVICE_ID_NEPTUNE_DCSP:
James Smart12222f42014-05-21 08:05:19 -04002393 m = (typeof(m)){"LPe1002-SP", "PCIe",
2394 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002395 break;
dea31012005-04-17 16:05:31 -05002396 case PCI_DEVICE_ID_BMID:
James Smarta747c9c2009-11-18 15:41:10 -05002397 m = (typeof(m)){"LP1150", "PCI-X2", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002398 break;
2399 case PCI_DEVICE_ID_BSMB:
James Smart12222f42014-05-21 08:05:19 -04002400 m = (typeof(m)){"LP111", "PCI-X2",
2401 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002402 break;
2403 case PCI_DEVICE_ID_ZEPHYR:
James Smarta747c9c2009-11-18 15:41:10 -05002404 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002405 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002406 case PCI_DEVICE_ID_ZEPHYR_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002407 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002408 break;
2409 case PCI_DEVICE_ID_ZEPHYR_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002410 m = (typeof(m)){"LP2105", "PCIe", "FCoE Adapter"};
James Smarta257bf92009-04-06 18:48:10 -04002411 GE = 1;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002412 break;
dea31012005-04-17 16:05:31 -05002413 case PCI_DEVICE_ID_ZMID:
James Smarta747c9c2009-11-18 15:41:10 -05002414 m = (typeof(m)){"LPe1150", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002415 break;
2416 case PCI_DEVICE_ID_ZSMB:
James Smarta747c9c2009-11-18 15:41:10 -05002417 m = (typeof(m)){"LPe111", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002418 break;
2419 case PCI_DEVICE_ID_LP101:
James Smart12222f42014-05-21 08:05:19 -04002420 m = (typeof(m)){"LP101", "PCI-X",
2421 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002422 break;
2423 case PCI_DEVICE_ID_LP10000S:
James Smart12222f42014-05-21 08:05:19 -04002424 m = (typeof(m)){"LP10000-S", "PCI",
2425 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04002426 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002427 case PCI_DEVICE_ID_LP11000S:
James Smart12222f42014-05-21 08:05:19 -04002428 m = (typeof(m)){"LP11000-S", "PCI-X2",
2429 "Obsolete, Unsupported Fibre Channel Adapter"};
James Smart18a3b592006-12-02 13:35:08 -05002430 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002431 case PCI_DEVICE_ID_LPE11000S:
James Smart12222f42014-05-21 08:05:19 -04002432 m = (typeof(m)){"LPe11000-S", "PCIe",
2433 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Com5cc36b32005-11-28 11:42:19 -05002434 break;
James Smartb87eab32007-04-25 09:53:28 -04002435 case PCI_DEVICE_ID_SAT:
James Smarta747c9c2009-11-18 15:41:10 -05002436 m = (typeof(m)){"LPe12000", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002437 break;
2438 case PCI_DEVICE_ID_SAT_MID:
James Smarta747c9c2009-11-18 15:41:10 -05002439 m = (typeof(m)){"LPe1250", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002440 break;
2441 case PCI_DEVICE_ID_SAT_SMB:
James Smarta747c9c2009-11-18 15:41:10 -05002442 m = (typeof(m)){"LPe121", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002443 break;
2444 case PCI_DEVICE_ID_SAT_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002445 m = (typeof(m)){"LPe12002-SP", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002446 break;
2447 case PCI_DEVICE_ID_SAT_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002448 m = (typeof(m)){"LPe12000-SP", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002449 break;
2450 case PCI_DEVICE_ID_SAT_S:
James Smarta747c9c2009-11-18 15:41:10 -05002451 m = (typeof(m)){"LPe12000-S", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002452 break;
James Smart84774a42008-08-24 21:50:06 -04002453 case PCI_DEVICE_ID_HORNET:
James Smart12222f42014-05-21 08:05:19 -04002454 m = (typeof(m)){"LP21000", "PCIe",
2455 "Obsolete, Unsupported FCoE Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002456 GE = 1;
2457 break;
2458 case PCI_DEVICE_ID_PROTEUS_VF:
James Smarta747c9c2009-11-18 15:41:10 -05002459 m = (typeof(m)){"LPev12000", "PCIe IOV",
James Smart12222f42014-05-21 08:05:19 -04002460 "Obsolete, Unsupported Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002461 break;
2462 case PCI_DEVICE_ID_PROTEUS_PF:
James Smarta747c9c2009-11-18 15:41:10 -05002463 m = (typeof(m)){"LPev12000", "PCIe IOV",
James Smart12222f42014-05-21 08:05:19 -04002464 "Obsolete, Unsupported Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002465 break;
2466 case PCI_DEVICE_ID_PROTEUS_S:
James Smarta747c9c2009-11-18 15:41:10 -05002467 m = (typeof(m)){"LPemv12002-S", "PCIe IOV",
James Smart12222f42014-05-21 08:05:19 -04002468 "Obsolete, Unsupported Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002469 break;
James Smartda0436e2009-05-22 14:51:39 -04002470 case PCI_DEVICE_ID_TIGERSHARK:
2471 oneConnect = 1;
James Smarta747c9c2009-11-18 15:41:10 -05002472 m = (typeof(m)){"OCe10100", "PCIe", "FCoE"};
James Smartda0436e2009-05-22 14:51:39 -04002473 break;
James Smarta747c9c2009-11-18 15:41:10 -05002474 case PCI_DEVICE_ID_TOMCAT:
James Smart6669f9b2009-10-02 15:16:45 -04002475 oneConnect = 1;
James Smarta747c9c2009-11-18 15:41:10 -05002476 m = (typeof(m)){"OCe11100", "PCIe", "FCoE"};
2477 break;
2478 case PCI_DEVICE_ID_FALCON:
2479 m = (typeof(m)){"LPSe12002-ML1-E", "PCIe",
2480 "EmulexSecure Fibre"};
James Smart6669f9b2009-10-02 15:16:45 -04002481 break;
James Smart98fc5dd2010-06-07 15:24:29 -04002482 case PCI_DEVICE_ID_BALIUS:
2483 m = (typeof(m)){"LPVe12002", "PCIe Shared I/O",
James Smart12222f42014-05-21 08:05:19 -04002484 "Obsolete, Unsupported Fibre Channel Adapter"};
James Smart98fc5dd2010-06-07 15:24:29 -04002485 break;
James Smart085c6472010-11-20 23:11:37 -05002486 case PCI_DEVICE_ID_LANCER_FC:
James Smartc0c11512011-05-24 11:41:34 -04002487 m = (typeof(m)){"LPe16000", "PCIe", "Fibre Channel Adapter"};
James Smart085c6472010-11-20 23:11:37 -05002488 break;
James Smart12222f42014-05-21 08:05:19 -04002489 case PCI_DEVICE_ID_LANCER_FC_VF:
2490 m = (typeof(m)){"LPe16000", "PCIe",
2491 "Obsolete, Unsupported Fibre Channel Adapter"};
2492 break;
James Smart085c6472010-11-20 23:11:37 -05002493 case PCI_DEVICE_ID_LANCER_FCOE:
2494 oneConnect = 1;
James Smart079b5c92011-08-21 21:48:49 -04002495 m = (typeof(m)){"OCe15100", "PCIe", "FCoE"};
James Smart085c6472010-11-20 23:11:37 -05002496 break;
James Smart12222f42014-05-21 08:05:19 -04002497 case PCI_DEVICE_ID_LANCER_FCOE_VF:
2498 oneConnect = 1;
2499 m = (typeof(m)){"OCe15100", "PCIe",
2500 "Obsolete, Unsupported FCoE"};
2501 break;
James Smartd38dd522015-08-31 16:48:17 -04002502 case PCI_DEVICE_ID_LANCER_G6_FC:
2503 m = (typeof(m)){"LPe32000", "PCIe", "Fibre Channel Adapter"};
2504 break;
James Smartc238b9b2018-02-22 08:18:44 -08002505 case PCI_DEVICE_ID_LANCER_G7_FC:
2506 m = (typeof(m)){"LPe36000", "PCIe", "Fibre Channel Adapter"};
2507 break;
James Smartf8cafd32012-08-03 12:42:51 -04002508 case PCI_DEVICE_ID_SKYHAWK:
2509 case PCI_DEVICE_ID_SKYHAWK_VF:
2510 oneConnect = 1;
2511 m = (typeof(m)){"OCe14000", "PCIe", "FCoE"};
2512 break;
James.Smart@Emulex.Com5cc36b32005-11-28 11:42:19 -05002513 default:
James Smarta747c9c2009-11-18 15:41:10 -05002514 m = (typeof(m)){"Unknown", "", ""};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002515 break;
dea31012005-04-17 16:05:31 -05002516 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002517
2518 if (mdp && mdp[0] == '\0')
2519 snprintf(mdp, 79,"%s", m.name);
James Smartc0c11512011-05-24 11:41:34 -04002520 /*
2521 * oneConnect hba requires special processing, they are all initiators
James Smartda0436e2009-05-22 14:51:39 -04002522 * and we put the port number on the end
2523 */
2524 if (descp && descp[0] == '\0') {
2525 if (oneConnect)
2526 snprintf(descp, 255,
James Smart4169d862012-09-29 11:32:09 -04002527 "Emulex OneConnect %s, %s Initiator %s",
James Smarta747c9c2009-11-18 15:41:10 -05002528 m.name, m.function,
James Smartda0436e2009-05-22 14:51:39 -04002529 phba->Port);
James Smart4169d862012-09-29 11:32:09 -04002530 else if (max_speed == 0)
2531 snprintf(descp, 255,
Sebastian Herbszt290237d2015-08-31 16:48:08 -04002532 "Emulex %s %s %s",
James Smart4169d862012-09-29 11:32:09 -04002533 m.name, m.bus, m.function);
James Smartda0436e2009-05-22 14:51:39 -04002534 else
2535 snprintf(descp, 255,
2536 "Emulex %s %d%s %s %s",
James Smarta747c9c2009-11-18 15:41:10 -05002537 m.name, max_speed, (GE) ? "GE" : "Gb",
2538 m.bus, m.function);
James Smartda0436e2009-05-22 14:51:39 -04002539 }
dea31012005-04-17 16:05:31 -05002540}
2541
James Smarte59058c2008-08-24 21:49:00 -04002542/**
James Smart3621a712009-04-06 18:47:14 -04002543 * lpfc_post_buffer - Post IOCB(s) with DMA buffer descriptor(s) to a IOCB ring
James Smarte59058c2008-08-24 21:49:00 -04002544 * @phba: pointer to lpfc hba data structure.
2545 * @pring: pointer to a IOCB ring.
2546 * @cnt: the number of IOCBs to be posted to the IOCB ring.
2547 *
2548 * This routine posts a given number of IOCBs with the associated DMA buffer
2549 * descriptors specified by the cnt argument to the given IOCB ring.
2550 *
2551 * Return codes
2552 * The number of IOCBs NOT able to be posted to the IOCB ring.
2553 **/
dea31012005-04-17 16:05:31 -05002554int
James Smart495a7142008-06-14 22:52:59 -04002555lpfc_post_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring, int cnt)
dea31012005-04-17 16:05:31 -05002556{
2557 IOCB_t *icmd;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002558 struct lpfc_iocbq *iocb;
dea31012005-04-17 16:05:31 -05002559 struct lpfc_dmabuf *mp1, *mp2;
2560
2561 cnt += pring->missbufcnt;
2562
2563 /* While there are buffers to post */
2564 while (cnt > 0) {
2565 /* Allocate buffer for command iocb */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002566 iocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05002567 if (iocb == NULL) {
2568 pring->missbufcnt = cnt;
2569 return cnt;
2570 }
dea31012005-04-17 16:05:31 -05002571 icmd = &iocb->iocb;
2572
2573 /* 2 buffers can be posted per command */
2574 /* Allocate buffer to post */
2575 mp1 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
2576 if (mp1)
James Smart98c9ea52007-10-27 13:37:33 -04002577 mp1->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &mp1->phys);
2578 if (!mp1 || !mp1->virt) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002579 kfree(mp1);
James Bottomley604a3e32005-10-29 10:28:33 -05002580 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002581 pring->missbufcnt = cnt;
2582 return cnt;
2583 }
2584
2585 INIT_LIST_HEAD(&mp1->list);
2586 /* Allocate buffer to post */
2587 if (cnt > 1) {
2588 mp2 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
2589 if (mp2)
2590 mp2->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
2591 &mp2->phys);
James Smart98c9ea52007-10-27 13:37:33 -04002592 if (!mp2 || !mp2->virt) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002593 kfree(mp2);
dea31012005-04-17 16:05:31 -05002594 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
2595 kfree(mp1);
James Bottomley604a3e32005-10-29 10:28:33 -05002596 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002597 pring->missbufcnt = cnt;
2598 return cnt;
2599 }
2600
2601 INIT_LIST_HEAD(&mp2->list);
2602 } else {
2603 mp2 = NULL;
2604 }
2605
2606 icmd->un.cont64[0].addrHigh = putPaddrHigh(mp1->phys);
2607 icmd->un.cont64[0].addrLow = putPaddrLow(mp1->phys);
2608 icmd->un.cont64[0].tus.f.bdeSize = FCELSSIZE;
2609 icmd->ulpBdeCount = 1;
2610 cnt--;
2611 if (mp2) {
2612 icmd->un.cont64[1].addrHigh = putPaddrHigh(mp2->phys);
2613 icmd->un.cont64[1].addrLow = putPaddrLow(mp2->phys);
2614 icmd->un.cont64[1].tus.f.bdeSize = FCELSSIZE;
2615 cnt--;
2616 icmd->ulpBdeCount = 2;
2617 }
2618
2619 icmd->ulpCommand = CMD_QUE_RING_BUF64_CN;
2620 icmd->ulpLe = 1;
2621
James Smart3772a992009-05-22 14:50:54 -04002622 if (lpfc_sli_issue_iocb(phba, pring->ringno, iocb, 0) ==
2623 IOCB_ERROR) {
dea31012005-04-17 16:05:31 -05002624 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
2625 kfree(mp1);
2626 cnt++;
2627 if (mp2) {
2628 lpfc_mbuf_free(phba, mp2->virt, mp2->phys);
2629 kfree(mp2);
2630 cnt++;
2631 }
James Bottomley604a3e32005-10-29 10:28:33 -05002632 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002633 pring->missbufcnt = cnt;
dea31012005-04-17 16:05:31 -05002634 return cnt;
2635 }
dea31012005-04-17 16:05:31 -05002636 lpfc_sli_ringpostbuf_put(phba, pring, mp1);
James Smart92d7f7b2007-06-17 19:56:38 -05002637 if (mp2)
dea31012005-04-17 16:05:31 -05002638 lpfc_sli_ringpostbuf_put(phba, pring, mp2);
dea31012005-04-17 16:05:31 -05002639 }
2640 pring->missbufcnt = 0;
2641 return 0;
2642}
2643
James Smarte59058c2008-08-24 21:49:00 -04002644/**
James Smart3621a712009-04-06 18:47:14 -04002645 * lpfc_post_rcv_buf - Post the initial receive IOCB buffers to ELS ring
James Smarte59058c2008-08-24 21:49:00 -04002646 * @phba: pointer to lpfc hba data structure.
2647 *
2648 * This routine posts initial receive IOCB buffers to the ELS ring. The
2649 * current number of initial IOCB buffers specified by LPFC_BUF_RING0 is
James Smart895427b2017-02-12 13:52:30 -08002650 * set to 64 IOCBs. SLI3 only.
James Smarte59058c2008-08-24 21:49:00 -04002651 *
2652 * Return codes
2653 * 0 - success (currently always success)
2654 **/
dea31012005-04-17 16:05:31 -05002655static int
James Smart2e0fef82007-06-17 19:56:36 -05002656lpfc_post_rcv_buf(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002657{
2658 struct lpfc_sli *psli = &phba->sli;
2659
2660 /* Ring 0, ELS / CT buffers */
James Smart895427b2017-02-12 13:52:30 -08002661 lpfc_post_buffer(phba, &psli->sli3_ring[LPFC_ELS_RING], LPFC_BUF_RING0);
dea31012005-04-17 16:05:31 -05002662 /* Ring 2 - FCP no buffers needed */
2663
2664 return 0;
2665}
2666
2667#define S(N,V) (((V)<<(N))|((V)>>(32-(N))))
2668
James Smarte59058c2008-08-24 21:49:00 -04002669/**
James Smart3621a712009-04-06 18:47:14 -04002670 * lpfc_sha_init - Set up initial array of hash table entries
James Smarte59058c2008-08-24 21:49:00 -04002671 * @HashResultPointer: pointer to an array as hash table.
2672 *
2673 * This routine sets up the initial values to the array of hash table entries
2674 * for the LC HBAs.
2675 **/
dea31012005-04-17 16:05:31 -05002676static void
2677lpfc_sha_init(uint32_t * HashResultPointer)
2678{
2679 HashResultPointer[0] = 0x67452301;
2680 HashResultPointer[1] = 0xEFCDAB89;
2681 HashResultPointer[2] = 0x98BADCFE;
2682 HashResultPointer[3] = 0x10325476;
2683 HashResultPointer[4] = 0xC3D2E1F0;
2684}
2685
James Smarte59058c2008-08-24 21:49:00 -04002686/**
James Smart3621a712009-04-06 18:47:14 -04002687 * lpfc_sha_iterate - Iterate initial hash table with the working hash table
James Smarte59058c2008-08-24 21:49:00 -04002688 * @HashResultPointer: pointer to an initial/result hash table.
2689 * @HashWorkingPointer: pointer to an working hash table.
2690 *
2691 * This routine iterates an initial hash table pointed by @HashResultPointer
2692 * with the values from the working hash table pointeed by @HashWorkingPointer.
2693 * The results are putting back to the initial hash table, returned through
2694 * the @HashResultPointer as the result hash table.
2695 **/
dea31012005-04-17 16:05:31 -05002696static void
2697lpfc_sha_iterate(uint32_t * HashResultPointer, uint32_t * HashWorkingPointer)
2698{
2699 int t;
2700 uint32_t TEMP;
2701 uint32_t A, B, C, D, E;
2702 t = 16;
2703 do {
2704 HashWorkingPointer[t] =
2705 S(1,
2706 HashWorkingPointer[t - 3] ^ HashWorkingPointer[t -
2707 8] ^
2708 HashWorkingPointer[t - 14] ^ HashWorkingPointer[t - 16]);
2709 } while (++t <= 79);
2710 t = 0;
2711 A = HashResultPointer[0];
2712 B = HashResultPointer[1];
2713 C = HashResultPointer[2];
2714 D = HashResultPointer[3];
2715 E = HashResultPointer[4];
2716
2717 do {
2718 if (t < 20) {
2719 TEMP = ((B & C) | ((~B) & D)) + 0x5A827999;
2720 } else if (t < 40) {
2721 TEMP = (B ^ C ^ D) + 0x6ED9EBA1;
2722 } else if (t < 60) {
2723 TEMP = ((B & C) | (B & D) | (C & D)) + 0x8F1BBCDC;
2724 } else {
2725 TEMP = (B ^ C ^ D) + 0xCA62C1D6;
2726 }
2727 TEMP += S(5, A) + E + HashWorkingPointer[t];
2728 E = D;
2729 D = C;
2730 C = S(30, B);
2731 B = A;
2732 A = TEMP;
2733 } while (++t <= 79);
2734
2735 HashResultPointer[0] += A;
2736 HashResultPointer[1] += B;
2737 HashResultPointer[2] += C;
2738 HashResultPointer[3] += D;
2739 HashResultPointer[4] += E;
2740
2741}
2742
James Smarte59058c2008-08-24 21:49:00 -04002743/**
James Smart3621a712009-04-06 18:47:14 -04002744 * lpfc_challenge_key - Create challenge key based on WWPN of the HBA
James Smarte59058c2008-08-24 21:49:00 -04002745 * @RandomChallenge: pointer to the entry of host challenge random number array.
2746 * @HashWorking: pointer to the entry of the working hash array.
2747 *
2748 * This routine calculates the working hash array referred by @HashWorking
2749 * from the challenge random numbers associated with the host, referred by
2750 * @RandomChallenge. The result is put into the entry of the working hash
2751 * array and returned by reference through @HashWorking.
2752 **/
dea31012005-04-17 16:05:31 -05002753static void
2754lpfc_challenge_key(uint32_t * RandomChallenge, uint32_t * HashWorking)
2755{
2756 *HashWorking = (*RandomChallenge ^ *HashWorking);
2757}
2758
James Smarte59058c2008-08-24 21:49:00 -04002759/**
James Smart3621a712009-04-06 18:47:14 -04002760 * lpfc_hba_init - Perform special handling for LC HBA initialization
James Smarte59058c2008-08-24 21:49:00 -04002761 * @phba: pointer to lpfc hba data structure.
2762 * @hbainit: pointer to an array of unsigned 32-bit integers.
2763 *
2764 * This routine performs the special handling for LC HBA initialization.
2765 **/
dea31012005-04-17 16:05:31 -05002766void
2767lpfc_hba_init(struct lpfc_hba *phba, uint32_t *hbainit)
2768{
2769 int t;
2770 uint32_t *HashWorking;
James Smart2e0fef82007-06-17 19:56:36 -05002771 uint32_t *pwwnn = (uint32_t *) phba->wwnn;
dea31012005-04-17 16:05:31 -05002772
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +02002773 HashWorking = kcalloc(80, sizeof(uint32_t), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05002774 if (!HashWorking)
2775 return;
2776
dea31012005-04-17 16:05:31 -05002777 HashWorking[0] = HashWorking[78] = *pwwnn++;
2778 HashWorking[1] = HashWorking[79] = *pwwnn;
2779
2780 for (t = 0; t < 7; t++)
2781 lpfc_challenge_key(phba->RandomData + t, HashWorking + t);
2782
2783 lpfc_sha_init(hbainit);
2784 lpfc_sha_iterate(hbainit, HashWorking);
2785 kfree(HashWorking);
2786}
2787
James Smarte59058c2008-08-24 21:49:00 -04002788/**
James Smart3621a712009-04-06 18:47:14 -04002789 * lpfc_cleanup - Performs vport cleanups before deleting a vport
James Smarte59058c2008-08-24 21:49:00 -04002790 * @vport: pointer to a virtual N_Port data structure.
2791 *
2792 * This routine performs the necessary cleanups before deleting the @vport.
2793 * It invokes the discovery state machine to perform necessary state
2794 * transitions and to release the ndlps associated with the @vport. Note,
2795 * the physical port is treated as @vport 0.
2796 **/
James Smart87af33f2007-10-27 13:37:43 -04002797void
James Smart2e0fef82007-06-17 19:56:36 -05002798lpfc_cleanup(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002799{
James Smart87af33f2007-10-27 13:37:43 -04002800 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002801 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smarta8adb832007-10-27 13:37:53 -04002802 int i = 0;
dea31012005-04-17 16:05:31 -05002803
James Smart87af33f2007-10-27 13:37:43 -04002804 if (phba->link_state > LPFC_LINK_DOWN)
2805 lpfc_port_link_failure(vport);
2806
2807 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05002808 if (!NLP_CHK_NODE_ACT(ndlp)) {
2809 ndlp = lpfc_enable_node(vport, ndlp,
2810 NLP_STE_UNUSED_NODE);
2811 if (!ndlp)
2812 continue;
2813 spin_lock_irq(&phba->ndlp_lock);
2814 NLP_SET_FREE_REQ(ndlp);
2815 spin_unlock_irq(&phba->ndlp_lock);
2816 /* Trigger the release of the ndlp memory */
2817 lpfc_nlp_put(ndlp);
2818 continue;
2819 }
2820 spin_lock_irq(&phba->ndlp_lock);
2821 if (NLP_CHK_FREE_REQ(ndlp)) {
2822 /* The ndlp should not be in memory free mode already */
2823 spin_unlock_irq(&phba->ndlp_lock);
2824 continue;
2825 } else
2826 /* Indicate request for freeing ndlp memory */
2827 NLP_SET_FREE_REQ(ndlp);
2828 spin_unlock_irq(&phba->ndlp_lock);
2829
James Smart58da1ff2008-04-07 10:15:56 -04002830 if (vport->port_type != LPFC_PHYSICAL_PORT &&
2831 ndlp->nlp_DID == Fabric_DID) {
2832 /* Just free up ndlp with Fabric_DID for vports */
2833 lpfc_nlp_put(ndlp);
2834 continue;
2835 }
2836
James Smarteff4a012012-01-18 16:25:25 -05002837 /* take care of nodes in unused state before the state
2838 * machine taking action.
2839 */
2840 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE) {
2841 lpfc_nlp_put(ndlp);
2842 continue;
2843 }
2844
James Smart87af33f2007-10-27 13:37:43 -04002845 if (ndlp->nlp_type & NLP_FABRIC)
2846 lpfc_disc_state_machine(vport, ndlp, NULL,
2847 NLP_EVT_DEVICE_RECOVERY);
James Smarte47c9092008-02-08 18:49:26 -05002848
James Smart87af33f2007-10-27 13:37:43 -04002849 lpfc_disc_state_machine(vport, ndlp, NULL,
2850 NLP_EVT_DEVICE_RM);
2851 }
2852
James Smarta8adb832007-10-27 13:37:53 -04002853 /* At this point, ALL ndlp's should be gone
2854 * because of the previous NLP_EVT_DEVICE_RM.
2855 * Lets wait for this to happen, if needed.
2856 */
James Smart87af33f2007-10-27 13:37:43 -04002857 while (!list_empty(&vport->fc_nodes)) {
James Smarta8adb832007-10-27 13:37:53 -04002858 if (i++ > 3000) {
James Smart87af33f2007-10-27 13:37:43 -04002859 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
James Smarta8adb832007-10-27 13:37:53 -04002860 "0233 Nodelist not empty\n");
James Smarte47c9092008-02-08 18:49:26 -05002861 list_for_each_entry_safe(ndlp, next_ndlp,
2862 &vport->fc_nodes, nlp_listp) {
2863 lpfc_printf_vlog(ndlp->vport, KERN_ERR,
2864 LOG_NODE,
James Smartd7c255b2008-08-24 21:50:00 -04002865 "0282 did:x%x ndlp:x%p "
James Smarte47c9092008-02-08 18:49:26 -05002866 "usgmap:x%x refcnt:%d\n",
2867 ndlp->nlp_DID, (void *)ndlp,
2868 ndlp->nlp_usg_map,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01002869 kref_read(&ndlp->kref));
James Smarte47c9092008-02-08 18:49:26 -05002870 }
James Smarta8adb832007-10-27 13:37:53 -04002871 break;
James Smart87af33f2007-10-27 13:37:43 -04002872 }
James Smarta8adb832007-10-27 13:37:53 -04002873
2874 /* Wait for any activity on ndlps to settle */
2875 msleep(10);
James Smart87af33f2007-10-27 13:37:43 -04002876 }
James Smart1151e3e2011-02-16 12:39:35 -05002877 lpfc_cleanup_vports_rrqs(vport, NULL);
dea31012005-04-17 16:05:31 -05002878}
2879
James Smarte59058c2008-08-24 21:49:00 -04002880/**
James Smart3621a712009-04-06 18:47:14 -04002881 * lpfc_stop_vport_timers - Stop all the timers associated with a vport
James Smarte59058c2008-08-24 21:49:00 -04002882 * @vport: pointer to a virtual N_Port data structure.
2883 *
2884 * This routine stops all the timers associated with a @vport. This function
2885 * is invoked before disabling or deleting a @vport. Note that the physical
2886 * port is treated as @vport 0.
2887 **/
James Smart92d7f7b2007-06-17 19:56:38 -05002888void
2889lpfc_stop_vport_timers(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002890{
James Smart92d7f7b2007-06-17 19:56:38 -05002891 del_timer_sync(&vport->els_tmofunc);
James Smart92494142011-02-16 12:39:44 -05002892 del_timer_sync(&vport->delayed_disc_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05002893 lpfc_can_disctmo(vport);
2894 return;
dea31012005-04-17 16:05:31 -05002895}
2896
James Smarte59058c2008-08-24 21:49:00 -04002897/**
James Smartecfd03c2010-02-12 14:41:27 -05002898 * __lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2899 * @phba: pointer to lpfc hba data structure.
2900 *
2901 * This routine stops the SLI4 FCF rediscover wait timer if it's on. The
2902 * caller of this routine should already hold the host lock.
2903 **/
2904void
2905__lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2906{
James Smart5ac6b302010-10-22 11:05:36 -04002907 /* Clear pending FCF rediscovery wait flag */
2908 phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
2909
James Smartecfd03c2010-02-12 14:41:27 -05002910 /* Now, try to stop the timer */
2911 del_timer(&phba->fcf.redisc_wait);
2912}
2913
2914/**
2915 * lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2916 * @phba: pointer to lpfc hba data structure.
2917 *
2918 * This routine stops the SLI4 FCF rediscover wait timer if it's on. It
2919 * checks whether the FCF rediscovery wait timer is pending with the host
2920 * lock held before proceeding with disabling the timer and clearing the
2921 * wait timer pendig flag.
2922 **/
2923void
2924lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2925{
2926 spin_lock_irq(&phba->hbalock);
2927 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
2928 /* FCF rediscovery timer already fired or stopped */
2929 spin_unlock_irq(&phba->hbalock);
2930 return;
2931 }
2932 __lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
James Smart5ac6b302010-10-22 11:05:36 -04002933 /* Clear failover in progress flags */
2934 phba->fcf.fcf_flag &= ~(FCF_DEAD_DISC | FCF_ACVL_DISC);
James Smartecfd03c2010-02-12 14:41:27 -05002935 spin_unlock_irq(&phba->hbalock);
2936}
2937
2938/**
James Smart3772a992009-05-22 14:50:54 -04002939 * lpfc_stop_hba_timers - Stop all the timers associated with an HBA
James Smarte59058c2008-08-24 21:49:00 -04002940 * @phba: pointer to lpfc hba data structure.
2941 *
2942 * This routine stops all the timers associated with a HBA. This function is
2943 * invoked before either putting a HBA offline or unloading the driver.
2944 **/
James Smart3772a992009-05-22 14:50:54 -04002945void
2946lpfc_stop_hba_timers(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002947{
James Smartcdb42be2019-01-28 11:14:21 -08002948 if (phba->pport)
2949 lpfc_stop_vport_timers(phba->pport);
James Smart32517fc2019-01-28 11:14:33 -08002950 cancel_delayed_work_sync(&phba->eq_delay_work);
James Smart2e0fef82007-06-17 19:56:36 -05002951 del_timer_sync(&phba->sli.mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05002952 del_timer_sync(&phba->fabric_block_timer);
James Smart93996272008-08-24 21:50:30 -04002953 del_timer_sync(&phba->eratt_poll);
James Smart3772a992009-05-22 14:50:54 -04002954 del_timer_sync(&phba->hb_tmofunc);
James Smart1151e3e2011-02-16 12:39:35 -05002955 if (phba->sli_rev == LPFC_SLI_REV4) {
2956 del_timer_sync(&phba->rrq_tmr);
2957 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
2958 }
James Smart3772a992009-05-22 14:50:54 -04002959 phba->hb_outstanding = 0;
2960
2961 switch (phba->pci_dev_grp) {
2962 case LPFC_PCI_DEV_LP:
2963 /* Stop any LightPulse device specific driver timers */
2964 del_timer_sync(&phba->fcp_poll_timer);
2965 break;
2966 case LPFC_PCI_DEV_OC:
2967 /* Stop any OneConnect device sepcific driver timers */
James Smartecfd03c2010-02-12 14:41:27 -05002968 lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
James Smart3772a992009-05-22 14:50:54 -04002969 break;
2970 default:
2971 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2972 "0297 Invalid device group (x%x)\n",
2973 phba->pci_dev_grp);
2974 break;
2975 }
James Smart2e0fef82007-06-17 19:56:36 -05002976 return;
dea31012005-04-17 16:05:31 -05002977}
2978
James Smarte59058c2008-08-24 21:49:00 -04002979/**
James Smart3621a712009-04-06 18:47:14 -04002980 * lpfc_block_mgmt_io - Mark a HBA's management interface as blocked
James Smarte59058c2008-08-24 21:49:00 -04002981 * @phba: pointer to lpfc hba data structure.
2982 *
2983 * This routine marks a HBA's management interface as blocked. Once the HBA's
2984 * management interface is marked as blocked, all the user space access to
2985 * the HBA, whether they are from sysfs interface or libdfc interface will
2986 * all be blocked. The HBA is set to block the management interface when the
2987 * driver prepares the HBA interface for online or offline.
2988 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01002989static void
James Smart618a5232012-06-12 13:54:36 -04002990lpfc_block_mgmt_io(struct lpfc_hba *phba, int mbx_action)
Adrian Bunka6ababd2007-11-05 18:07:33 +01002991{
2992 unsigned long iflag;
James Smart6e7288d2010-06-07 15:23:35 -04002993 uint8_t actcmd = MBX_HEARTBEAT;
2994 unsigned long timeout;
2995
Adrian Bunka6ababd2007-11-05 18:07:33 +01002996 spin_lock_irqsave(&phba->hbalock, iflag);
2997 phba->sli.sli_flag |= LPFC_BLOCK_MGMT_IO;
James Smart618a5232012-06-12 13:54:36 -04002998 spin_unlock_irqrestore(&phba->hbalock, iflag);
2999 if (mbx_action == LPFC_MBX_NO_WAIT)
3000 return;
3001 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
3002 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta183a152011-10-10 21:32:43 -04003003 if (phba->sli.mbox_active) {
James Smart6e7288d2010-06-07 15:23:35 -04003004 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
James Smarta183a152011-10-10 21:32:43 -04003005 /* Determine how long we might wait for the active mailbox
3006 * command to be gracefully completed by firmware.
3007 */
3008 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
3009 phba->sli.mbox_active) * 1000) + jiffies;
3010 }
Adrian Bunka6ababd2007-11-05 18:07:33 +01003011 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta183a152011-10-10 21:32:43 -04003012
James Smart6e7288d2010-06-07 15:23:35 -04003013 /* Wait for the outstnading mailbox command to complete */
3014 while (phba->sli.mbox_active) {
3015 /* Check active mailbox complete status every 2ms */
3016 msleep(2);
3017 if (time_after(jiffies, timeout)) {
3018 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3019 "2813 Mgmt IO is Blocked %x "
3020 "- mbox cmd %x still active\n",
3021 phba->sli.sli_flag, actcmd);
3022 break;
3023 }
3024 }
Adrian Bunka6ababd2007-11-05 18:07:33 +01003025}
3026
James Smarte59058c2008-08-24 21:49:00 -04003027/**
James Smart6b5151f2012-01-18 16:24:06 -05003028 * lpfc_sli4_node_prep - Assign RPIs for active nodes.
3029 * @phba: pointer to lpfc hba data structure.
3030 *
3031 * Allocate RPIs for all active remote nodes. This is needed whenever
3032 * an SLI4 adapter is reset and the driver is not unloading. Its purpose
3033 * is to fixup the temporary rpi assignments.
3034 **/
3035void
3036lpfc_sli4_node_prep(struct lpfc_hba *phba)
3037{
3038 struct lpfc_nodelist *ndlp, *next_ndlp;
3039 struct lpfc_vport **vports;
James Smart9d3d3402017-04-21 16:05:00 -07003040 int i, rpi;
3041 unsigned long flags;
James Smart6b5151f2012-01-18 16:24:06 -05003042
3043 if (phba->sli_rev != LPFC_SLI_REV4)
3044 return;
3045
3046 vports = lpfc_create_vport_work_array(phba);
James Smart9d3d3402017-04-21 16:05:00 -07003047 if (vports == NULL)
3048 return;
James Smart6b5151f2012-01-18 16:24:06 -05003049
James Smart9d3d3402017-04-21 16:05:00 -07003050 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
3051 if (vports[i]->load_flag & FC_UNLOADING)
3052 continue;
3053
3054 list_for_each_entry_safe(ndlp, next_ndlp,
3055 &vports[i]->fc_nodes,
3056 nlp_listp) {
3057 if (!NLP_CHK_NODE_ACT(ndlp))
3058 continue;
3059 rpi = lpfc_sli4_alloc_rpi(phba);
3060 if (rpi == LPFC_RPI_ALLOC_ERROR) {
3061 spin_lock_irqsave(&phba->ndlp_lock, flags);
3062 NLP_CLR_NODE_ACT(ndlp);
3063 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
3064 continue;
James Smart6b5151f2012-01-18 16:24:06 -05003065 }
James Smart9d3d3402017-04-21 16:05:00 -07003066 ndlp->nlp_rpi = rpi;
3067 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
3068 "0009 rpi:%x DID:%x "
3069 "flg:%x map:%x %p\n", ndlp->nlp_rpi,
3070 ndlp->nlp_DID, ndlp->nlp_flag,
3071 ndlp->nlp_usg_map, ndlp);
James Smart6b5151f2012-01-18 16:24:06 -05003072 }
3073 }
3074 lpfc_destroy_vport_work_array(phba, vports);
3075}
3076
3077/**
James Smartc4908502019-01-28 11:14:28 -08003078 * lpfc_create_expedite_pool - create expedite pool
3079 * @phba: pointer to lpfc hba data structure.
3080 *
3081 * This routine moves a batch of XRIs from lpfc_io_buf_list_put of HWQ 0
3082 * to expedite pool. Mark them as expedite.
3083 **/
Bart Van Assche3999df72019-03-28 11:06:16 -07003084static void lpfc_create_expedite_pool(struct lpfc_hba *phba)
James Smartc4908502019-01-28 11:14:28 -08003085{
3086 struct lpfc_sli4_hdw_queue *qp;
3087 struct lpfc_io_buf *lpfc_ncmd;
3088 struct lpfc_io_buf *lpfc_ncmd_next;
3089 struct lpfc_epd_pool *epd_pool;
3090 unsigned long iflag;
3091
3092 epd_pool = &phba->epd_pool;
3093 qp = &phba->sli4_hba.hdwq[0];
3094
3095 spin_lock_init(&epd_pool->lock);
3096 spin_lock_irqsave(&qp->io_buf_list_put_lock, iflag);
3097 spin_lock(&epd_pool->lock);
3098 INIT_LIST_HEAD(&epd_pool->list);
3099 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
3100 &qp->lpfc_io_buf_list_put, list) {
3101 list_move_tail(&lpfc_ncmd->list, &epd_pool->list);
3102 lpfc_ncmd->expedite = true;
3103 qp->put_io_bufs--;
3104 epd_pool->count++;
3105 if (epd_pool->count >= XRI_BATCH)
3106 break;
3107 }
3108 spin_unlock(&epd_pool->lock);
3109 spin_unlock_irqrestore(&qp->io_buf_list_put_lock, iflag);
3110}
3111
3112/**
3113 * lpfc_destroy_expedite_pool - destroy expedite pool
3114 * @phba: pointer to lpfc hba data structure.
3115 *
3116 * This routine returns XRIs from expedite pool to lpfc_io_buf_list_put
3117 * of HWQ 0. Clear the mark.
3118 **/
Bart Van Assche3999df72019-03-28 11:06:16 -07003119static void lpfc_destroy_expedite_pool(struct lpfc_hba *phba)
James Smartc4908502019-01-28 11:14:28 -08003120{
3121 struct lpfc_sli4_hdw_queue *qp;
3122 struct lpfc_io_buf *lpfc_ncmd;
3123 struct lpfc_io_buf *lpfc_ncmd_next;
3124 struct lpfc_epd_pool *epd_pool;
3125 unsigned long iflag;
3126
3127 epd_pool = &phba->epd_pool;
3128 qp = &phba->sli4_hba.hdwq[0];
3129
3130 spin_lock_irqsave(&qp->io_buf_list_put_lock, iflag);
3131 spin_lock(&epd_pool->lock);
3132 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
3133 &epd_pool->list, list) {
3134 list_move_tail(&lpfc_ncmd->list,
3135 &qp->lpfc_io_buf_list_put);
3136 lpfc_ncmd->flags = false;
3137 qp->put_io_bufs++;
3138 epd_pool->count--;
3139 }
3140 spin_unlock(&epd_pool->lock);
3141 spin_unlock_irqrestore(&qp->io_buf_list_put_lock, iflag);
3142}
3143
3144/**
3145 * lpfc_create_multixri_pools - create multi-XRI pools
3146 * @phba: pointer to lpfc hba data structure.
3147 *
3148 * This routine initialize public, private per HWQ. Then, move XRIs from
3149 * lpfc_io_buf_list_put to public pool. High and low watermark are also
3150 * Initialized.
3151 **/
3152void lpfc_create_multixri_pools(struct lpfc_hba *phba)
3153{
3154 u32 i, j;
3155 u32 hwq_count;
3156 u32 count_per_hwq;
3157 struct lpfc_io_buf *lpfc_ncmd;
3158 struct lpfc_io_buf *lpfc_ncmd_next;
3159 unsigned long iflag;
3160 struct lpfc_sli4_hdw_queue *qp;
3161 struct lpfc_multixri_pool *multixri_pool;
3162 struct lpfc_pbl_pool *pbl_pool;
3163 struct lpfc_pvt_pool *pvt_pool;
3164
3165 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3166 "1234 num_hdw_queue=%d num_present_cpu=%d common_xri_cnt=%d\n",
3167 phba->cfg_hdw_queue, phba->sli4_hba.num_present_cpu,
3168 phba->sli4_hba.io_xri_cnt);
3169
3170 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
3171 lpfc_create_expedite_pool(phba);
3172
3173 hwq_count = phba->cfg_hdw_queue;
3174 count_per_hwq = phba->sli4_hba.io_xri_cnt / hwq_count;
3175
3176 for (i = 0; i < hwq_count; i++) {
3177 multixri_pool = kzalloc(sizeof(*multixri_pool), GFP_KERNEL);
3178
3179 if (!multixri_pool) {
3180 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3181 "1238 Failed to allocate memory for "
3182 "multixri_pool\n");
3183
3184 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
3185 lpfc_destroy_expedite_pool(phba);
3186
3187 j = 0;
3188 while (j < i) {
3189 qp = &phba->sli4_hba.hdwq[j];
3190 kfree(qp->p_multixri_pool);
3191 j++;
3192 }
3193 phba->cfg_xri_rebalancing = 0;
3194 return;
3195 }
3196
3197 qp = &phba->sli4_hba.hdwq[i];
3198 qp->p_multixri_pool = multixri_pool;
3199
3200 multixri_pool->xri_limit = count_per_hwq;
3201 multixri_pool->rrb_next_hwqid = i;
3202
3203 /* Deal with public free xri pool */
3204 pbl_pool = &multixri_pool->pbl_pool;
3205 spin_lock_init(&pbl_pool->lock);
3206 spin_lock_irqsave(&qp->io_buf_list_put_lock, iflag);
3207 spin_lock(&pbl_pool->lock);
3208 INIT_LIST_HEAD(&pbl_pool->list);
3209 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
3210 &qp->lpfc_io_buf_list_put, list) {
3211 list_move_tail(&lpfc_ncmd->list, &pbl_pool->list);
3212 qp->put_io_bufs--;
3213 pbl_pool->count++;
3214 }
3215 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3216 "1235 Moved %d buffers from PUT list over to pbl_pool[%d]\n",
3217 pbl_pool->count, i);
3218 spin_unlock(&pbl_pool->lock);
3219 spin_unlock_irqrestore(&qp->io_buf_list_put_lock, iflag);
3220
3221 /* Deal with private free xri pool */
3222 pvt_pool = &multixri_pool->pvt_pool;
3223 pvt_pool->high_watermark = multixri_pool->xri_limit / 2;
3224 pvt_pool->low_watermark = XRI_BATCH;
3225 spin_lock_init(&pvt_pool->lock);
3226 spin_lock_irqsave(&pvt_pool->lock, iflag);
3227 INIT_LIST_HEAD(&pvt_pool->list);
3228 pvt_pool->count = 0;
3229 spin_unlock_irqrestore(&pvt_pool->lock, iflag);
3230 }
3231}
3232
3233/**
3234 * lpfc_destroy_multixri_pools - destroy multi-XRI pools
3235 * @phba: pointer to lpfc hba data structure.
3236 *
3237 * This routine returns XRIs from public/private to lpfc_io_buf_list_put.
3238 **/
Bart Van Assche3999df72019-03-28 11:06:16 -07003239static void lpfc_destroy_multixri_pools(struct lpfc_hba *phba)
James Smartc4908502019-01-28 11:14:28 -08003240{
3241 u32 i;
3242 u32 hwq_count;
3243 struct lpfc_io_buf *lpfc_ncmd;
3244 struct lpfc_io_buf *lpfc_ncmd_next;
3245 unsigned long iflag;
3246 struct lpfc_sli4_hdw_queue *qp;
3247 struct lpfc_multixri_pool *multixri_pool;
3248 struct lpfc_pbl_pool *pbl_pool;
3249 struct lpfc_pvt_pool *pvt_pool;
3250
3251 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
3252 lpfc_destroy_expedite_pool(phba);
3253
James Smartc66a9192019-03-12 16:30:19 -07003254 if (!(phba->pport->load_flag & FC_UNLOADING)) {
3255 lpfc_sli_flush_fcp_rings(phba);
3256
3257 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
3258 lpfc_sli_flush_nvme_rings(phba);
3259 }
3260
James Smartc4908502019-01-28 11:14:28 -08003261 hwq_count = phba->cfg_hdw_queue;
3262
3263 for (i = 0; i < hwq_count; i++) {
3264 qp = &phba->sli4_hba.hdwq[i];
3265 multixri_pool = qp->p_multixri_pool;
3266 if (!multixri_pool)
3267 continue;
3268
3269 qp->p_multixri_pool = NULL;
3270
3271 spin_lock_irqsave(&qp->io_buf_list_put_lock, iflag);
3272
3273 /* Deal with public free xri pool */
3274 pbl_pool = &multixri_pool->pbl_pool;
3275 spin_lock(&pbl_pool->lock);
3276
3277 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3278 "1236 Moving %d buffers from pbl_pool[%d] TO PUT list\n",
3279 pbl_pool->count, i);
3280
3281 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
3282 &pbl_pool->list, list) {
3283 list_move_tail(&lpfc_ncmd->list,
3284 &qp->lpfc_io_buf_list_put);
3285 qp->put_io_bufs++;
3286 pbl_pool->count--;
3287 }
3288
3289 INIT_LIST_HEAD(&pbl_pool->list);
3290 pbl_pool->count = 0;
3291
3292 spin_unlock(&pbl_pool->lock);
3293
3294 /* Deal with private free xri pool */
3295 pvt_pool = &multixri_pool->pvt_pool;
3296 spin_lock(&pvt_pool->lock);
3297
3298 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3299 "1237 Moving %d buffers from pvt_pool[%d] TO PUT list\n",
3300 pvt_pool->count, i);
3301
3302 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
3303 &pvt_pool->list, list) {
3304 list_move_tail(&lpfc_ncmd->list,
3305 &qp->lpfc_io_buf_list_put);
3306 qp->put_io_bufs++;
3307 pvt_pool->count--;
3308 }
3309
3310 INIT_LIST_HEAD(&pvt_pool->list);
3311 pvt_pool->count = 0;
3312
3313 spin_unlock(&pvt_pool->lock);
3314 spin_unlock_irqrestore(&qp->io_buf_list_put_lock, iflag);
3315
3316 kfree(multixri_pool);
3317 }
3318}
3319
3320/**
James Smart3621a712009-04-06 18:47:14 -04003321 * lpfc_online - Initialize and bring a HBA online
James Smarte59058c2008-08-24 21:49:00 -04003322 * @phba: pointer to lpfc hba data structure.
3323 *
3324 * This routine initializes the HBA and brings a HBA online. During this
3325 * process, the management interface is blocked to prevent user space access
3326 * to the HBA interfering with the driver initialization.
3327 *
3328 * Return codes
3329 * 0 - successful
3330 * 1 - failed
3331 **/
dea31012005-04-17 16:05:31 -05003332int
James Smart2e0fef82007-06-17 19:56:36 -05003333lpfc_online(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05003334{
Julia Lawall372bd282008-12-16 16:15:08 +01003335 struct lpfc_vport *vport;
James Smart549e55c2007-08-02 11:09:51 -04003336 struct lpfc_vport **vports;
Dick Kennedya145fda2017-08-23 16:55:44 -07003337 int i, error = 0;
James Smart16a3a202013-04-17 20:14:38 -04003338 bool vpis_cleared = false;
James Smart2e0fef82007-06-17 19:56:36 -05003339
dea31012005-04-17 16:05:31 -05003340 if (!phba)
3341 return 0;
Julia Lawall372bd282008-12-16 16:15:08 +01003342 vport = phba->pport;
dea31012005-04-17 16:05:31 -05003343
James Smart2e0fef82007-06-17 19:56:36 -05003344 if (!(vport->fc_flag & FC_OFFLINE_MODE))
dea31012005-04-17 16:05:31 -05003345 return 0;
3346
James Smarted957682007-06-17 19:56:37 -05003347 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003348 "0458 Bring Adapter online\n");
dea31012005-04-17 16:05:31 -05003349
James Smart618a5232012-06-12 13:54:36 -04003350 lpfc_block_mgmt_io(phba, LPFC_MBX_WAIT);
dea31012005-04-17 16:05:31 -05003351
James Smartda0436e2009-05-22 14:51:39 -04003352 if (phba->sli_rev == LPFC_SLI_REV4) {
3353 if (lpfc_sli4_hba_setup(phba)) { /* Initialize SLI4 HBA */
3354 lpfc_unblock_mgmt_io(phba);
3355 return 1;
3356 }
James Smart16a3a202013-04-17 20:14:38 -04003357 spin_lock_irq(&phba->hbalock);
3358 if (!phba->sli4_hba.max_cfg_param.vpi_used)
3359 vpis_cleared = true;
3360 spin_unlock_irq(&phba->hbalock);
Dick Kennedya145fda2017-08-23 16:55:44 -07003361
3362 /* Reestablish the local initiator port.
3363 * The offline process destroyed the previous lport.
3364 */
3365 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME &&
3366 !phba->nvmet_support) {
3367 error = lpfc_nvme_create_localport(phba->pport);
3368 if (error)
3369 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3370 "6132 NVME restore reg failed "
3371 "on nvmei error x%x\n", error);
3372 }
James Smartda0436e2009-05-22 14:51:39 -04003373 } else {
James Smart895427b2017-02-12 13:52:30 -08003374 lpfc_sli_queue_init(phba);
James Smartda0436e2009-05-22 14:51:39 -04003375 if (lpfc_sli_hba_setup(phba)) { /* Initialize SLI2/SLI3 HBA */
3376 lpfc_unblock_mgmt_io(phba);
3377 return 1;
3378 }
James Smart46fa3112007-04-25 09:51:45 -04003379 }
dea31012005-04-17 16:05:31 -05003380
James Smart549e55c2007-08-02 11:09:51 -04003381 vports = lpfc_create_vport_work_array(phba);
Arnd Bergmannaeb66412016-03-14 15:29:44 +01003382 if (vports != NULL) {
James Smartda0436e2009-05-22 14:51:39 -04003383 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -04003384 struct Scsi_Host *shost;
3385 shost = lpfc_shost_from_vport(vports[i]);
3386 spin_lock_irq(shost->host_lock);
3387 vports[i]->fc_flag &= ~FC_OFFLINE_MODE;
3388 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
3389 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart16a3a202013-04-17 20:14:38 -04003390 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart1c6834a2009-07-19 10:01:26 -04003391 vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
James Smart16a3a202013-04-17 20:14:38 -04003392 if ((vpis_cleared) &&
3393 (vports[i]->port_type !=
3394 LPFC_PHYSICAL_PORT))
3395 vports[i]->vpi = 0;
3396 }
James Smart549e55c2007-08-02 11:09:51 -04003397 spin_unlock_irq(shost->host_lock);
3398 }
Arnd Bergmannaeb66412016-03-14 15:29:44 +01003399 }
3400 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05003401
James Smartc4908502019-01-28 11:14:28 -08003402 if (phba->cfg_xri_rebalancing)
3403 lpfc_create_multixri_pools(phba);
3404
James Smart46fa3112007-04-25 09:51:45 -04003405 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05003406 return 0;
3407}
3408
James Smarte59058c2008-08-24 21:49:00 -04003409/**
James Smart3621a712009-04-06 18:47:14 -04003410 * lpfc_unblock_mgmt_io - Mark a HBA's management interface to be not blocked
James Smarte59058c2008-08-24 21:49:00 -04003411 * @phba: pointer to lpfc hba data structure.
3412 *
3413 * This routine marks a HBA's management interface as not blocked. Once the
3414 * HBA's management interface is marked as not blocked, all the user space
3415 * access to the HBA, whether they are from sysfs interface or libdfc
3416 * interface will be allowed. The HBA is set to block the management interface
3417 * when the driver prepares the HBA interface for online or offline and then
3418 * set to unblock the management interface afterwards.
3419 **/
James Smart46fa3112007-04-25 09:51:45 -04003420void
James Smart46fa3112007-04-25 09:51:45 -04003421lpfc_unblock_mgmt_io(struct lpfc_hba * phba)
3422{
3423 unsigned long iflag;
3424
James Smart2e0fef82007-06-17 19:56:36 -05003425 spin_lock_irqsave(&phba->hbalock, iflag);
3426 phba->sli.sli_flag &= ~LPFC_BLOCK_MGMT_IO;
3427 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart46fa3112007-04-25 09:51:45 -04003428}
3429
James Smarte59058c2008-08-24 21:49:00 -04003430/**
James Smart3621a712009-04-06 18:47:14 -04003431 * lpfc_offline_prep - Prepare a HBA to be brought offline
James Smarte59058c2008-08-24 21:49:00 -04003432 * @phba: pointer to lpfc hba data structure.
3433 *
3434 * This routine is invoked to prepare a HBA to be brought offline. It performs
3435 * unregistration login to all the nodes on all vports and flushes the mailbox
3436 * queue to make it ready to be brought offline.
3437 **/
James Smart46fa3112007-04-25 09:51:45 -04003438void
James Smart618a5232012-06-12 13:54:36 -04003439lpfc_offline_prep(struct lpfc_hba *phba, int mbx_action)
James Smart46fa3112007-04-25 09:51:45 -04003440{
James Smart2e0fef82007-06-17 19:56:36 -05003441 struct lpfc_vport *vport = phba->pport;
James Smart46fa3112007-04-25 09:51:45 -04003442 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart87af33f2007-10-27 13:37:43 -04003443 struct lpfc_vport **vports;
James Smart72100cc2010-02-12 14:43:01 -05003444 struct Scsi_Host *shost;
James Smart87af33f2007-10-27 13:37:43 -04003445 int i;
dea31012005-04-17 16:05:31 -05003446
James Smart2e0fef82007-06-17 19:56:36 -05003447 if (vport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04003448 return;
dea31012005-04-17 16:05:31 -05003449
James Smart618a5232012-06-12 13:54:36 -04003450 lpfc_block_mgmt_io(phba, mbx_action);
dea31012005-04-17 16:05:31 -05003451
3452 lpfc_linkdown(phba);
3453
James Smart87af33f2007-10-27 13:37:43 -04003454 /* Issue an unreg_login to all nodes on all vports */
3455 vports = lpfc_create_vport_work_array(phba);
3456 if (vports != NULL) {
James Smartda0436e2009-05-22 14:51:39 -04003457 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smarta8adb832007-10-27 13:37:53 -04003458 if (vports[i]->load_flag & FC_UNLOADING)
3459 continue;
James Smart72100cc2010-02-12 14:43:01 -05003460 shost = lpfc_shost_from_vport(vports[i]);
3461 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05003462 vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05003463 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
3464 vports[i]->fc_flag &= ~FC_VFI_REGISTERED;
James Smart72100cc2010-02-12 14:43:01 -05003465 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05003466
James Smart87af33f2007-10-27 13:37:43 -04003467 shost = lpfc_shost_from_vport(vports[i]);
3468 list_for_each_entry_safe(ndlp, next_ndlp,
3469 &vports[i]->fc_nodes,
3470 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05003471 if (!NLP_CHK_NODE_ACT(ndlp))
3472 continue;
James Smart87af33f2007-10-27 13:37:43 -04003473 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
3474 continue;
3475 if (ndlp->nlp_type & NLP_FABRIC) {
3476 lpfc_disc_state_machine(vports[i], ndlp,
3477 NULL, NLP_EVT_DEVICE_RECOVERY);
3478 lpfc_disc_state_machine(vports[i], ndlp,
3479 NULL, NLP_EVT_DEVICE_RM);
3480 }
3481 spin_lock_irq(shost->host_lock);
3482 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
James Smart401ee0c2012-03-01 22:35:34 -05003483 spin_unlock_irq(shost->host_lock);
James Smart6b5151f2012-01-18 16:24:06 -05003484 /*
3485 * Whenever an SLI4 port goes offline, free the
James Smart401ee0c2012-03-01 22:35:34 -05003486 * RPI. Get a new RPI when the adapter port
3487 * comes back online.
James Smart6b5151f2012-01-18 16:24:06 -05003488 */
James Smartbe6bb942015-04-07 15:07:22 -04003489 if (phba->sli_rev == LPFC_SLI_REV4) {
3490 lpfc_printf_vlog(ndlp->vport,
3491 KERN_INFO, LOG_NODE,
3492 "0011 lpfc_offline: "
3493 "ndlp:x%p did %x "
3494 "usgmap:x%x rpi:%x\n",
3495 ndlp, ndlp->nlp_DID,
3496 ndlp->nlp_usg_map,
3497 ndlp->nlp_rpi);
3498
James Smart6b5151f2012-01-18 16:24:06 -05003499 lpfc_sli4_free_rpi(phba, ndlp->nlp_rpi);
James Smartbe6bb942015-04-07 15:07:22 -04003500 }
James Smart87af33f2007-10-27 13:37:43 -04003501 lpfc_unreg_rpi(vports[i], ndlp);
3502 }
3503 }
3504 }
James Smart09372822008-01-11 01:52:54 -05003505 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05003506
James Smart618a5232012-06-12 13:54:36 -04003507 lpfc_sli_mbox_sys_shutdown(phba, mbx_action);
Dick Kennedyf485c182017-09-29 17:34:34 -07003508
3509 if (phba->wq)
3510 flush_workqueue(phba->wq);
James Smart46fa3112007-04-25 09:51:45 -04003511}
3512
James Smarte59058c2008-08-24 21:49:00 -04003513/**
James Smart3621a712009-04-06 18:47:14 -04003514 * lpfc_offline - Bring a HBA offline
James Smarte59058c2008-08-24 21:49:00 -04003515 * @phba: pointer to lpfc hba data structure.
3516 *
3517 * This routine actually brings a HBA offline. It stops all the timers
3518 * associated with the HBA, brings down the SLI layer, and eventually
3519 * marks the HBA as in offline state for the upper layer protocol.
3520 **/
James Smart46fa3112007-04-25 09:51:45 -04003521void
James Smart2e0fef82007-06-17 19:56:36 -05003522lpfc_offline(struct lpfc_hba *phba)
James Smart46fa3112007-04-25 09:51:45 -04003523{
James Smart549e55c2007-08-02 11:09:51 -04003524 struct Scsi_Host *shost;
3525 struct lpfc_vport **vports;
3526 int i;
James Smart46fa3112007-04-25 09:51:45 -04003527
James Smart549e55c2007-08-02 11:09:51 -04003528 if (phba->pport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04003529 return;
James Smart688a8862006-07-06 15:49:56 -04003530
James Smartda0436e2009-05-22 14:51:39 -04003531 /* stop port and all timers associated with this hba */
3532 lpfc_stop_port(phba);
Dick Kennedy4b40d022017-08-23 16:55:38 -07003533
3534 /* Tear down the local and target port registrations. The
3535 * nvme transports need to cleanup.
3536 */
3537 lpfc_nvmet_destroy_targetport(phba);
3538 lpfc_nvme_destroy_localport(phba->pport);
3539
James Smart51ef4c22007-08-02 11:10:31 -04003540 vports = lpfc_create_vport_work_array(phba);
3541 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04003542 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
James Smart51ef4c22007-08-02 11:10:31 -04003543 lpfc_stop_vport_timers(vports[i]);
James Smart09372822008-01-11 01:52:54 -05003544 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -05003545 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003546 "0460 Bring Adapter offline\n");
dea31012005-04-17 16:05:31 -05003547 /* Bring down the SLI Layer and cleanup. The HBA is offline
3548 now. */
3549 lpfc_sli_hba_down(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05003550 spin_lock_irq(&phba->hbalock);
James Smart7054a602007-04-25 09:52:34 -04003551 phba->work_ha = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05003552 spin_unlock_irq(&phba->hbalock);
James Smart549e55c2007-08-02 11:09:51 -04003553 vports = lpfc_create_vport_work_array(phba);
3554 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04003555 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -04003556 shost = lpfc_shost_from_vport(vports[i]);
James Smart549e55c2007-08-02 11:09:51 -04003557 spin_lock_irq(shost->host_lock);
3558 vports[i]->work_port_events = 0;
3559 vports[i]->fc_flag |= FC_OFFLINE_MODE;
3560 spin_unlock_irq(shost->host_lock);
3561 }
James Smart09372822008-01-11 01:52:54 -05003562 lpfc_destroy_vport_work_array(phba, vports);
James Smartc4908502019-01-28 11:14:28 -08003563
3564 if (phba->cfg_xri_rebalancing)
3565 lpfc_destroy_multixri_pools(phba);
dea31012005-04-17 16:05:31 -05003566}
3567
James Smarte59058c2008-08-24 21:49:00 -04003568/**
James Smart3621a712009-04-06 18:47:14 -04003569 * lpfc_scsi_free - Free all the SCSI buffers and IOCBs from driver lists
James Smarte59058c2008-08-24 21:49:00 -04003570 * @phba: pointer to lpfc hba data structure.
3571 *
3572 * This routine is to free all the SCSI buffers and IOCBs from the driver
3573 * list back to kernel. It is called from lpfc_pci_remove_one to free
3574 * the internal resources before the device is removed from the system.
James Smarte59058c2008-08-24 21:49:00 -04003575 **/
James Smart8a9d2e82012-05-09 21:16:12 -04003576static void
James Smart2e0fef82007-06-17 19:56:36 -05003577lpfc_scsi_free(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05003578{
James Smartc4908502019-01-28 11:14:28 -08003579 struct lpfc_io_buf *sb, *sb_next;
dea31012005-04-17 16:05:31 -05003580
James Smart895427b2017-02-12 13:52:30 -08003581 if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP))
3582 return;
3583
James Smart2e0fef82007-06-17 19:56:36 -05003584 spin_lock_irq(&phba->hbalock);
James Smarta40fc5f2013-04-17 20:17:40 -04003585
dea31012005-04-17 16:05:31 -05003586 /* Release all the lpfc_scsi_bufs maintained by this host. */
James Smarta40fc5f2013-04-17 20:17:40 -04003587
3588 spin_lock(&phba->scsi_buf_list_put_lock);
3589 list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list_put,
3590 list) {
dea31012005-04-17 16:05:31 -05003591 list_del(&sb->list);
Romain Perier771db5c2017-07-06 10:13:05 +02003592 dma_pool_free(phba->lpfc_sg_dma_buf_pool, sb->data,
James Smart92d7f7b2007-06-17 19:56:38 -05003593 sb->dma_handle);
dea31012005-04-17 16:05:31 -05003594 kfree(sb);
3595 phba->total_scsi_bufs--;
3596 }
James Smarta40fc5f2013-04-17 20:17:40 -04003597 spin_unlock(&phba->scsi_buf_list_put_lock);
3598
3599 spin_lock(&phba->scsi_buf_list_get_lock);
3600 list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list_get,
3601 list) {
3602 list_del(&sb->list);
Romain Perier771db5c2017-07-06 10:13:05 +02003603 dma_pool_free(phba->lpfc_sg_dma_buf_pool, sb->data,
James Smarta40fc5f2013-04-17 20:17:40 -04003604 sb->dma_handle);
3605 kfree(sb);
3606 phba->total_scsi_bufs--;
3607 }
3608 spin_unlock(&phba->scsi_buf_list_get_lock);
James Smart2e0fef82007-06-17 19:56:36 -05003609 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04003610}
James Smart0794d602019-01-28 11:14:19 -08003611
James Smart8a9d2e82012-05-09 21:16:12 -04003612/**
James Smart5e5b5112019-01-28 11:14:22 -08003613 * lpfc_io_free - Free all the IO buffers and IOCBs from driver lists
James Smart895427b2017-02-12 13:52:30 -08003614 * @phba: pointer to lpfc hba data structure.
3615 *
James Smart0794d602019-01-28 11:14:19 -08003616 * This routine is to free all the IO buffers and IOCBs from the driver
James Smart895427b2017-02-12 13:52:30 -08003617 * list back to kernel. It is called from lpfc_pci_remove_one to free
3618 * the internal resources before the device is removed from the system.
3619 **/
James Smartc4908502019-01-28 11:14:28 -08003620void
James Smart5e5b5112019-01-28 11:14:22 -08003621lpfc_io_free(struct lpfc_hba *phba)
James Smart895427b2017-02-12 13:52:30 -08003622{
James Smartc4908502019-01-28 11:14:28 -08003623 struct lpfc_io_buf *lpfc_ncmd, *lpfc_ncmd_next;
James Smart5e5b5112019-01-28 11:14:22 -08003624 struct lpfc_sli4_hdw_queue *qp;
3625 int idx;
James Smart895427b2017-02-12 13:52:30 -08003626
James Smart5e5b5112019-01-28 11:14:22 -08003627 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
3628 qp = &phba->sli4_hba.hdwq[idx];
3629 /* Release all the lpfc_nvme_bufs maintained by this host. */
3630 spin_lock(&qp->io_buf_list_put_lock);
3631 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
3632 &qp->lpfc_io_buf_list_put,
3633 list) {
3634 list_del(&lpfc_ncmd->list);
3635 qp->put_io_bufs--;
3636 dma_pool_free(phba->lpfc_sg_dma_buf_pool,
3637 lpfc_ncmd->data, lpfc_ncmd->dma_handle);
3638 kfree(lpfc_ncmd);
3639 qp->total_io_bufs--;
3640 }
3641 spin_unlock(&qp->io_buf_list_put_lock);
James Smart895427b2017-02-12 13:52:30 -08003642
James Smart5e5b5112019-01-28 11:14:22 -08003643 spin_lock(&qp->io_buf_list_get_lock);
3644 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
3645 &qp->lpfc_io_buf_list_get,
3646 list) {
3647 list_del(&lpfc_ncmd->list);
3648 qp->get_io_bufs--;
3649 dma_pool_free(phba->lpfc_sg_dma_buf_pool,
3650 lpfc_ncmd->data, lpfc_ncmd->dma_handle);
3651 kfree(lpfc_ncmd);
3652 qp->total_io_bufs--;
3653 }
3654 spin_unlock(&qp->io_buf_list_get_lock);
James Smart895427b2017-02-12 13:52:30 -08003655 }
James Smart895427b2017-02-12 13:52:30 -08003656}
James Smart0794d602019-01-28 11:14:19 -08003657
James Smart895427b2017-02-12 13:52:30 -08003658/**
3659 * lpfc_sli4_els_sgl_update - update ELS xri-sgl sizing and mapping
James Smart8a9d2e82012-05-09 21:16:12 -04003660 * @phba: pointer to lpfc hba data structure.
3661 *
3662 * This routine first calculates the sizes of the current els and allocated
3663 * scsi sgl lists, and then goes through all sgls to updates the physical
3664 * XRIs assigned due to port function reset. During port initialization, the
3665 * current els and allocated scsi sgl lists are 0s.
3666 *
3667 * Return codes
3668 * 0 - successful (for now, it always returns 0)
3669 **/
3670int
James Smart895427b2017-02-12 13:52:30 -08003671lpfc_sli4_els_sgl_update(struct lpfc_hba *phba)
James Smart8a9d2e82012-05-09 21:16:12 -04003672{
3673 struct lpfc_sglq *sglq_entry = NULL, *sglq_entry_next = NULL;
James Smart895427b2017-02-12 13:52:30 -08003674 uint16_t i, lxri, xri_cnt, els_xri_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04003675 LIST_HEAD(els_sgl_list);
James Smart8a9d2e82012-05-09 21:16:12 -04003676 int rc;
3677
3678 /*
3679 * update on pci function's els xri-sgl list
3680 */
3681 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
James Smart895427b2017-02-12 13:52:30 -08003682
James Smart8a9d2e82012-05-09 21:16:12 -04003683 if (els_xri_cnt > phba->sli4_hba.els_xri_cnt) {
3684 /* els xri-sgl expanded */
3685 xri_cnt = els_xri_cnt - phba->sli4_hba.els_xri_cnt;
3686 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3687 "3157 ELS xri-sgl count increased from "
3688 "%d to %d\n", phba->sli4_hba.els_xri_cnt,
3689 els_xri_cnt);
3690 /* allocate the additional els sgls */
3691 for (i = 0; i < xri_cnt; i++) {
3692 sglq_entry = kzalloc(sizeof(struct lpfc_sglq),
3693 GFP_KERNEL);
3694 if (sglq_entry == NULL) {
3695 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3696 "2562 Failure to allocate an "
3697 "ELS sgl entry:%d\n", i);
3698 rc = -ENOMEM;
3699 goto out_free_mem;
3700 }
3701 sglq_entry->buff_type = GEN_BUFF_TYPE;
3702 sglq_entry->virt = lpfc_mbuf_alloc(phba, 0,
3703 &sglq_entry->phys);
3704 if (sglq_entry->virt == NULL) {
3705 kfree(sglq_entry);
3706 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3707 "2563 Failure to allocate an "
3708 "ELS mbuf:%d\n", i);
3709 rc = -ENOMEM;
3710 goto out_free_mem;
3711 }
3712 sglq_entry->sgl = sglq_entry->virt;
3713 memset(sglq_entry->sgl, 0, LPFC_BPL_SIZE);
3714 sglq_entry->state = SGL_FREED;
3715 list_add_tail(&sglq_entry->list, &els_sgl_list);
3716 }
James Smart38c20672013-03-01 16:37:44 -05003717 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -08003718 spin_lock(&phba->sli4_hba.sgl_list_lock);
3719 list_splice_init(&els_sgl_list,
3720 &phba->sli4_hba.lpfc_els_sgl_list);
3721 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smart38c20672013-03-01 16:37:44 -05003722 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04003723 } else if (els_xri_cnt < phba->sli4_hba.els_xri_cnt) {
3724 /* els xri-sgl shrinked */
3725 xri_cnt = phba->sli4_hba.els_xri_cnt - els_xri_cnt;
3726 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3727 "3158 ELS xri-sgl count decreased from "
3728 "%d to %d\n", phba->sli4_hba.els_xri_cnt,
3729 els_xri_cnt);
3730 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -08003731 spin_lock(&phba->sli4_hba.sgl_list_lock);
3732 list_splice_init(&phba->sli4_hba.lpfc_els_sgl_list,
3733 &els_sgl_list);
James Smart8a9d2e82012-05-09 21:16:12 -04003734 /* release extra els sgls from list */
3735 for (i = 0; i < xri_cnt; i++) {
3736 list_remove_head(&els_sgl_list,
3737 sglq_entry, struct lpfc_sglq, list);
3738 if (sglq_entry) {
James Smart895427b2017-02-12 13:52:30 -08003739 __lpfc_mbuf_free(phba, sglq_entry->virt,
3740 sglq_entry->phys);
James Smart8a9d2e82012-05-09 21:16:12 -04003741 kfree(sglq_entry);
3742 }
3743 }
James Smart895427b2017-02-12 13:52:30 -08003744 list_splice_init(&els_sgl_list,
3745 &phba->sli4_hba.lpfc_els_sgl_list);
3746 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04003747 spin_unlock_irq(&phba->hbalock);
3748 } else
3749 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3750 "3163 ELS xri-sgl count unchanged: %d\n",
3751 els_xri_cnt);
3752 phba->sli4_hba.els_xri_cnt = els_xri_cnt;
3753
3754 /* update xris to els sgls on the list */
3755 sglq_entry = NULL;
3756 sglq_entry_next = NULL;
3757 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
James Smart895427b2017-02-12 13:52:30 -08003758 &phba->sli4_hba.lpfc_els_sgl_list, list) {
James Smart8a9d2e82012-05-09 21:16:12 -04003759 lxri = lpfc_sli4_next_xritag(phba);
3760 if (lxri == NO_XRI) {
3761 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3762 "2400 Failed to allocate xri for "
3763 "ELS sgl\n");
3764 rc = -ENOMEM;
3765 goto out_free_mem;
3766 }
3767 sglq_entry->sli4_lxritag = lxri;
3768 sglq_entry->sli4_xritag = phba->sli4_hba.xri_ids[lxri];
3769 }
James Smart895427b2017-02-12 13:52:30 -08003770 return 0;
3771
3772out_free_mem:
3773 lpfc_free_els_sgl_list(phba);
3774 return rc;
3775}
3776
3777/**
James Smartf358dd02017-02-12 13:52:34 -08003778 * lpfc_sli4_nvmet_sgl_update - update xri-sgl sizing and mapping
3779 * @phba: pointer to lpfc hba data structure.
3780 *
3781 * This routine first calculates the sizes of the current els and allocated
3782 * scsi sgl lists, and then goes through all sgls to updates the physical
3783 * XRIs assigned due to port function reset. During port initialization, the
3784 * current els and allocated scsi sgl lists are 0s.
3785 *
3786 * Return codes
3787 * 0 - successful (for now, it always returns 0)
3788 **/
3789int
3790lpfc_sli4_nvmet_sgl_update(struct lpfc_hba *phba)
3791{
3792 struct lpfc_sglq *sglq_entry = NULL, *sglq_entry_next = NULL;
3793 uint16_t i, lxri, xri_cnt, els_xri_cnt;
James Smart6c621a22017-05-15 15:20:45 -07003794 uint16_t nvmet_xri_cnt;
James Smartf358dd02017-02-12 13:52:34 -08003795 LIST_HEAD(nvmet_sgl_list);
3796 int rc;
3797
3798 /*
3799 * update on pci function's nvmet xri-sgl list
3800 */
3801 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
James Smart61f3d4b2017-05-15 15:20:41 -07003802
James Smart6c621a22017-05-15 15:20:45 -07003803 /* For NVMET, ALL remaining XRIs are dedicated for IO processing */
3804 nvmet_xri_cnt = phba->sli4_hba.max_cfg_param.max_xri - els_xri_cnt;
James Smartf358dd02017-02-12 13:52:34 -08003805 if (nvmet_xri_cnt > phba->sli4_hba.nvmet_xri_cnt) {
3806 /* els xri-sgl expanded */
3807 xri_cnt = nvmet_xri_cnt - phba->sli4_hba.nvmet_xri_cnt;
3808 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3809 "6302 NVMET xri-sgl cnt grew from %d to %d\n",
3810 phba->sli4_hba.nvmet_xri_cnt, nvmet_xri_cnt);
3811 /* allocate the additional nvmet sgls */
3812 for (i = 0; i < xri_cnt; i++) {
3813 sglq_entry = kzalloc(sizeof(struct lpfc_sglq),
3814 GFP_KERNEL);
3815 if (sglq_entry == NULL) {
3816 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3817 "6303 Failure to allocate an "
3818 "NVMET sgl entry:%d\n", i);
3819 rc = -ENOMEM;
3820 goto out_free_mem;
3821 }
3822 sglq_entry->buff_type = NVMET_BUFF_TYPE;
3823 sglq_entry->virt = lpfc_nvmet_buf_alloc(phba, 0,
3824 &sglq_entry->phys);
3825 if (sglq_entry->virt == NULL) {
3826 kfree(sglq_entry);
3827 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3828 "6304 Failure to allocate an "
3829 "NVMET buf:%d\n", i);
3830 rc = -ENOMEM;
3831 goto out_free_mem;
3832 }
3833 sglq_entry->sgl = sglq_entry->virt;
3834 memset(sglq_entry->sgl, 0,
3835 phba->cfg_sg_dma_buf_size);
3836 sglq_entry->state = SGL_FREED;
3837 list_add_tail(&sglq_entry->list, &nvmet_sgl_list);
3838 }
3839 spin_lock_irq(&phba->hbalock);
3840 spin_lock(&phba->sli4_hba.sgl_list_lock);
3841 list_splice_init(&nvmet_sgl_list,
3842 &phba->sli4_hba.lpfc_nvmet_sgl_list);
3843 spin_unlock(&phba->sli4_hba.sgl_list_lock);
3844 spin_unlock_irq(&phba->hbalock);
3845 } else if (nvmet_xri_cnt < phba->sli4_hba.nvmet_xri_cnt) {
3846 /* nvmet xri-sgl shrunk */
3847 xri_cnt = phba->sli4_hba.nvmet_xri_cnt - nvmet_xri_cnt;
3848 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3849 "6305 NVMET xri-sgl count decreased from "
3850 "%d to %d\n", phba->sli4_hba.nvmet_xri_cnt,
3851 nvmet_xri_cnt);
3852 spin_lock_irq(&phba->hbalock);
3853 spin_lock(&phba->sli4_hba.sgl_list_lock);
3854 list_splice_init(&phba->sli4_hba.lpfc_nvmet_sgl_list,
3855 &nvmet_sgl_list);
3856 /* release extra nvmet sgls from list */
3857 for (i = 0; i < xri_cnt; i++) {
3858 list_remove_head(&nvmet_sgl_list,
3859 sglq_entry, struct lpfc_sglq, list);
3860 if (sglq_entry) {
3861 lpfc_nvmet_buf_free(phba, sglq_entry->virt,
3862 sglq_entry->phys);
3863 kfree(sglq_entry);
3864 }
3865 }
3866 list_splice_init(&nvmet_sgl_list,
3867 &phba->sli4_hba.lpfc_nvmet_sgl_list);
3868 spin_unlock(&phba->sli4_hba.sgl_list_lock);
3869 spin_unlock_irq(&phba->hbalock);
3870 } else
3871 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3872 "6306 NVMET xri-sgl count unchanged: %d\n",
3873 nvmet_xri_cnt);
3874 phba->sli4_hba.nvmet_xri_cnt = nvmet_xri_cnt;
3875
3876 /* update xris to nvmet sgls on the list */
3877 sglq_entry = NULL;
3878 sglq_entry_next = NULL;
3879 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
3880 &phba->sli4_hba.lpfc_nvmet_sgl_list, list) {
3881 lxri = lpfc_sli4_next_xritag(phba);
3882 if (lxri == NO_XRI) {
3883 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3884 "6307 Failed to allocate xri for "
3885 "NVMET sgl\n");
3886 rc = -ENOMEM;
3887 goto out_free_mem;
3888 }
3889 sglq_entry->sli4_lxritag = lxri;
3890 sglq_entry->sli4_xritag = phba->sli4_hba.xri_ids[lxri];
3891 }
3892 return 0;
3893
3894out_free_mem:
3895 lpfc_free_nvmet_sgl_list(phba);
3896 return rc;
3897}
3898
James Smart5e5b5112019-01-28 11:14:22 -08003899int
3900lpfc_io_buf_flush(struct lpfc_hba *phba, struct list_head *cbuf)
3901{
3902 LIST_HEAD(blist);
3903 struct lpfc_sli4_hdw_queue *qp;
James Smartc4908502019-01-28 11:14:28 -08003904 struct lpfc_io_buf *lpfc_cmd;
3905 struct lpfc_io_buf *iobufp, *prev_iobufp;
James Smart5e5b5112019-01-28 11:14:22 -08003906 int idx, cnt, xri, inserted;
3907
3908 cnt = 0;
3909 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
3910 qp = &phba->sli4_hba.hdwq[idx];
3911 spin_lock_irq(&qp->io_buf_list_get_lock);
3912 spin_lock(&qp->io_buf_list_put_lock);
3913
3914 /* Take everything off the get and put lists */
3915 list_splice_init(&qp->lpfc_io_buf_list_get, &blist);
3916 list_splice(&qp->lpfc_io_buf_list_put, &blist);
3917 INIT_LIST_HEAD(&qp->lpfc_io_buf_list_get);
3918 INIT_LIST_HEAD(&qp->lpfc_io_buf_list_put);
3919 cnt += qp->get_io_bufs + qp->put_io_bufs;
3920 qp->get_io_bufs = 0;
3921 qp->put_io_bufs = 0;
3922 qp->total_io_bufs = 0;
3923 spin_unlock(&qp->io_buf_list_put_lock);
3924 spin_unlock_irq(&qp->io_buf_list_get_lock);
3925 }
3926
3927 /*
3928 * Take IO buffers off blist and put on cbuf sorted by XRI.
3929 * This is because POST_SGL takes a sequential range of XRIs
3930 * to post to the firmware.
3931 */
3932 for (idx = 0; idx < cnt; idx++) {
James Smartc4908502019-01-28 11:14:28 -08003933 list_remove_head(&blist, lpfc_cmd, struct lpfc_io_buf, list);
James Smart5e5b5112019-01-28 11:14:22 -08003934 if (!lpfc_cmd)
3935 return cnt;
3936 if (idx == 0) {
3937 list_add_tail(&lpfc_cmd->list, cbuf);
3938 continue;
3939 }
3940 xri = lpfc_cmd->cur_iocbq.sli4_xritag;
3941 inserted = 0;
3942 prev_iobufp = NULL;
3943 list_for_each_entry(iobufp, cbuf, list) {
3944 if (xri < iobufp->cur_iocbq.sli4_xritag) {
3945 if (prev_iobufp)
3946 list_add(&lpfc_cmd->list,
3947 &prev_iobufp->list);
3948 else
3949 list_add(&lpfc_cmd->list, cbuf);
3950 inserted = 1;
3951 break;
3952 }
3953 prev_iobufp = iobufp;
3954 }
3955 if (!inserted)
3956 list_add_tail(&lpfc_cmd->list, cbuf);
3957 }
3958 return cnt;
3959}
3960
3961int
3962lpfc_io_buf_replenish(struct lpfc_hba *phba, struct list_head *cbuf)
3963{
3964 struct lpfc_sli4_hdw_queue *qp;
James Smartc4908502019-01-28 11:14:28 -08003965 struct lpfc_io_buf *lpfc_cmd;
James Smart5e5b5112019-01-28 11:14:22 -08003966 int idx, cnt;
3967
3968 qp = phba->sli4_hba.hdwq;
3969 cnt = 0;
3970 while (!list_empty(cbuf)) {
3971 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
3972 list_remove_head(cbuf, lpfc_cmd,
James Smartc4908502019-01-28 11:14:28 -08003973 struct lpfc_io_buf, list);
James Smart5e5b5112019-01-28 11:14:22 -08003974 if (!lpfc_cmd)
3975 return cnt;
3976 cnt++;
3977 qp = &phba->sli4_hba.hdwq[idx];
James Smart1fbf9742019-01-28 11:14:26 -08003978 lpfc_cmd->hdwq_no = idx;
3979 lpfc_cmd->hdwq = qp;
James Smart5e5b5112019-01-28 11:14:22 -08003980 lpfc_cmd->cur_iocbq.wqe_cmpl = NULL;
3981 lpfc_cmd->cur_iocbq.iocb_cmpl = NULL;
3982 spin_lock(&qp->io_buf_list_put_lock);
3983 list_add_tail(&lpfc_cmd->list,
3984 &qp->lpfc_io_buf_list_put);
3985 qp->put_io_bufs++;
3986 qp->total_io_bufs++;
3987 spin_unlock(&qp->io_buf_list_put_lock);
3988 }
3989 }
3990 return cnt;
3991}
3992
James Smartf358dd02017-02-12 13:52:34 -08003993/**
James Smart5e5b5112019-01-28 11:14:22 -08003994 * lpfc_sli4_io_sgl_update - update xri-sgl sizing and mapping
James Smart895427b2017-02-12 13:52:30 -08003995 * @phba: pointer to lpfc hba data structure.
3996 *
3997 * This routine first calculates the sizes of the current els and allocated
3998 * scsi sgl lists, and then goes through all sgls to updates the physical
3999 * XRIs assigned due to port function reset. During port initialization, the
4000 * current els and allocated scsi sgl lists are 0s.
4001 *
4002 * Return codes
4003 * 0 - successful (for now, it always returns 0)
4004 **/
4005int
James Smart5e5b5112019-01-28 11:14:22 -08004006lpfc_sli4_io_sgl_update(struct lpfc_hba *phba)
James Smart895427b2017-02-12 13:52:30 -08004007{
James Smartc4908502019-01-28 11:14:28 -08004008 struct lpfc_io_buf *lpfc_ncmd = NULL, *lpfc_ncmd_next = NULL;
James Smart0794d602019-01-28 11:14:19 -08004009 uint16_t i, lxri, els_xri_cnt;
James Smart5e5b5112019-01-28 11:14:22 -08004010 uint16_t io_xri_cnt, io_xri_max;
4011 LIST_HEAD(io_sgl_list);
James Smart0794d602019-01-28 11:14:19 -08004012 int rc, cnt;
James Smart895427b2017-02-12 13:52:30 -08004013
4014 /*
James Smart0794d602019-01-28 11:14:19 -08004015 * update on pci function's allocated nvme xri-sgl list
James Smart895427b2017-02-12 13:52:30 -08004016 */
James Smart0794d602019-01-28 11:14:19 -08004017
4018 /* maximum number of xris available for nvme buffers */
James Smart895427b2017-02-12 13:52:30 -08004019 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
James Smart5e5b5112019-01-28 11:14:22 -08004020 io_xri_max = phba->sli4_hba.max_cfg_param.max_xri - els_xri_cnt;
4021 phba->sli4_hba.io_xri_max = io_xri_max;
James Smart8a9d2e82012-05-09 21:16:12 -04004022
James Smarte8c0a772017-04-21 16:04:49 -07004023 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart0794d602019-01-28 11:14:19 -08004024 "6074 Current allocated XRI sgl count:%d, "
4025 "maximum XRI count:%d\n",
James Smart5e5b5112019-01-28 11:14:22 -08004026 phba->sli4_hba.io_xri_cnt,
4027 phba->sli4_hba.io_xri_max);
James Smarte8c0a772017-04-21 16:04:49 -07004028
James Smart5e5b5112019-01-28 11:14:22 -08004029 cnt = lpfc_io_buf_flush(phba, &io_sgl_list);
James Smart0794d602019-01-28 11:14:19 -08004030
James Smart5e5b5112019-01-28 11:14:22 -08004031 if (phba->sli4_hba.io_xri_cnt > phba->sli4_hba.io_xri_max) {
James Smart0794d602019-01-28 11:14:19 -08004032 /* max nvme xri shrunk below the allocated nvme buffers */
James Smart5e5b5112019-01-28 11:14:22 -08004033 io_xri_cnt = phba->sli4_hba.io_xri_cnt -
4034 phba->sli4_hba.io_xri_max;
James Smart0794d602019-01-28 11:14:19 -08004035 /* release the extra allocated nvme buffers */
James Smart5e5b5112019-01-28 11:14:22 -08004036 for (i = 0; i < io_xri_cnt; i++) {
4037 list_remove_head(&io_sgl_list, lpfc_ncmd,
James Smartc4908502019-01-28 11:14:28 -08004038 struct lpfc_io_buf, list);
James Smart0794d602019-01-28 11:14:19 -08004039 if (lpfc_ncmd) {
Romain Perier771db5c2017-07-06 10:13:05 +02004040 dma_pool_free(phba->lpfc_sg_dma_buf_pool,
James Smart0794d602019-01-28 11:14:19 -08004041 lpfc_ncmd->data,
4042 lpfc_ncmd->dma_handle);
4043 kfree(lpfc_ncmd);
James Smarta2fc4aef2014-09-03 12:57:55 -04004044 }
James Smart8a9d2e82012-05-09 21:16:12 -04004045 }
James Smart5e5b5112019-01-28 11:14:22 -08004046 phba->sli4_hba.io_xri_cnt -= io_xri_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04004047 }
4048
James Smart0794d602019-01-28 11:14:19 -08004049 /* update xris associated to remaining allocated nvme buffers */
4050 lpfc_ncmd = NULL;
4051 lpfc_ncmd_next = NULL;
James Smart5e5b5112019-01-28 11:14:22 -08004052 phba->sli4_hba.io_xri_cnt = cnt;
James Smart0794d602019-01-28 11:14:19 -08004053 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
James Smart5e5b5112019-01-28 11:14:22 -08004054 &io_sgl_list, list) {
James Smart8a9d2e82012-05-09 21:16:12 -04004055 lxri = lpfc_sli4_next_xritag(phba);
4056 if (lxri == NO_XRI) {
4057 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart0794d602019-01-28 11:14:19 -08004058 "6075 Failed to allocate xri for "
4059 "nvme buffer\n");
James Smart8a9d2e82012-05-09 21:16:12 -04004060 rc = -ENOMEM;
4061 goto out_free_mem;
4062 }
James Smart0794d602019-01-28 11:14:19 -08004063 lpfc_ncmd->cur_iocbq.sli4_lxritag = lxri;
4064 lpfc_ncmd->cur_iocbq.sli4_xritag = phba->sli4_hba.xri_ids[lxri];
James Smart8a9d2e82012-05-09 21:16:12 -04004065 }
James Smart5e5b5112019-01-28 11:14:22 -08004066 cnt = lpfc_io_buf_replenish(phba, &io_sgl_list);
dea31012005-04-17 16:05:31 -05004067 return 0;
James Smart8a9d2e82012-05-09 21:16:12 -04004068
4069out_free_mem:
James Smart5e5b5112019-01-28 11:14:22 -08004070 lpfc_io_free(phba);
James Smart8a9d2e82012-05-09 21:16:12 -04004071 return rc;
dea31012005-04-17 16:05:31 -05004072}
4073
James Smart0794d602019-01-28 11:14:19 -08004074/**
James Smart5e5b5112019-01-28 11:14:22 -08004075 * lpfc_new_io_buf - IO buffer allocator for HBA with SLI4 IF spec
James Smart0794d602019-01-28 11:14:19 -08004076 * @vport: The virtual port for which this call being executed.
4077 * @num_to_allocate: The requested number of buffers to allocate.
4078 *
4079 * This routine allocates nvme buffers for device with SLI-4 interface spec,
4080 * the nvme buffer contains all the necessary information needed to initiate
4081 * an I/O. After allocating up to @num_to_allocate IO buffers and put
4082 * them on a list, it post them to the port by using SGL block post.
4083 *
4084 * Return codes:
James Smart5e5b5112019-01-28 11:14:22 -08004085 * int - number of IO buffers that were allocated and posted.
James Smart0794d602019-01-28 11:14:19 -08004086 * 0 = failure, less than num_to_alloc is a partial failure.
4087 **/
4088int
James Smart5e5b5112019-01-28 11:14:22 -08004089lpfc_new_io_buf(struct lpfc_hba *phba, int num_to_alloc)
James Smart0794d602019-01-28 11:14:19 -08004090{
James Smartc4908502019-01-28 11:14:28 -08004091 struct lpfc_io_buf *lpfc_ncmd;
James Smart0794d602019-01-28 11:14:19 -08004092 struct lpfc_iocbq *pwqeq;
4093 uint16_t iotag, lxri = 0;
4094 int bcnt, num_posted;
4095 LIST_HEAD(prep_nblist);
4096 LIST_HEAD(post_nblist);
4097 LIST_HEAD(nvme_nblist);
4098
4099 /* Sanity check to ensure our sizing is right for both SCSI and NVME */
James Smartc4908502019-01-28 11:14:28 -08004100 if (sizeof(struct lpfc_io_buf) > LPFC_COMMON_IO_BUF_SZ) {
James Smart0794d602019-01-28 11:14:19 -08004101 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
Arnd Bergmannf9968612019-03-04 20:39:09 +01004102 "6426 Common buffer size %zd exceeds %d\n",
James Smartc4908502019-01-28 11:14:28 -08004103 sizeof(struct lpfc_io_buf),
4104 LPFC_COMMON_IO_BUF_SZ);
James Smart0794d602019-01-28 11:14:19 -08004105 return 0;
4106 }
4107
James Smart5e5b5112019-01-28 11:14:22 -08004108 phba->sli4_hba.io_xri_cnt = 0;
James Smart0794d602019-01-28 11:14:19 -08004109 for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
4110 lpfc_ncmd = kzalloc(LPFC_COMMON_IO_BUF_SZ, GFP_KERNEL);
4111 if (!lpfc_ncmd)
4112 break;
4113 /*
4114 * Get memory from the pci pool to map the virt space to
4115 * pci bus space for an I/O. The DMA buffer includes the
4116 * number of SGE's necessary to support the sg_tablesize.
4117 */
4118 lpfc_ncmd->data = dma_pool_alloc(phba->lpfc_sg_dma_buf_pool,
4119 GFP_KERNEL,
4120 &lpfc_ncmd->dma_handle);
4121 if (!lpfc_ncmd->data) {
4122 kfree(lpfc_ncmd);
4123 break;
4124 }
4125 memset(lpfc_ncmd->data, 0, phba->cfg_sg_dma_buf_size);
4126
4127 /*
4128 * 4K Page alignment is CRITICAL to BlockGuard, double check
4129 * to be sure.
4130 */
4131 if ((phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
4132 (((unsigned long)(lpfc_ncmd->data) &
4133 (unsigned long)(SLI4_PAGE_SIZE - 1)) != 0)) {
4134 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
4135 "3369 Memory alignment err: addr=%lx\n",
4136 (unsigned long)lpfc_ncmd->data);
4137 dma_pool_free(phba->lpfc_sg_dma_buf_pool,
4138 lpfc_ncmd->data, lpfc_ncmd->dma_handle);
4139 kfree(lpfc_ncmd);
4140 break;
4141 }
4142
4143 lxri = lpfc_sli4_next_xritag(phba);
4144 if (lxri == NO_XRI) {
4145 dma_pool_free(phba->lpfc_sg_dma_buf_pool,
4146 lpfc_ncmd->data, lpfc_ncmd->dma_handle);
4147 kfree(lpfc_ncmd);
4148 break;
4149 }
4150 pwqeq = &lpfc_ncmd->cur_iocbq;
4151
4152 /* Allocate iotag for lpfc_ncmd->cur_iocbq. */
4153 iotag = lpfc_sli_next_iotag(phba, pwqeq);
4154 if (iotag == 0) {
4155 dma_pool_free(phba->lpfc_sg_dma_buf_pool,
4156 lpfc_ncmd->data, lpfc_ncmd->dma_handle);
4157 kfree(lpfc_ncmd);
4158 lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
4159 "6121 Failed to allocate IOTAG for"
4160 " XRI:0x%x\n", lxri);
4161 lpfc_sli4_free_xri(phba, lxri);
4162 break;
4163 }
4164 pwqeq->sli4_lxritag = lxri;
4165 pwqeq->sli4_xritag = phba->sli4_hba.xri_ids[lxri];
4166 pwqeq->context1 = lpfc_ncmd;
4167
4168 /* Initialize local short-hand pointers. */
4169 lpfc_ncmd->dma_sgl = lpfc_ncmd->data;
4170 lpfc_ncmd->dma_phys_sgl = lpfc_ncmd->dma_handle;
4171 lpfc_ncmd->cur_iocbq.context1 = lpfc_ncmd;
James Smartc2017262019-01-28 11:14:37 -08004172 spin_lock_init(&lpfc_ncmd->buf_lock);
James Smart0794d602019-01-28 11:14:19 -08004173
4174 /* add the nvme buffer to a post list */
4175 list_add_tail(&lpfc_ncmd->list, &post_nblist);
James Smart5e5b5112019-01-28 11:14:22 -08004176 phba->sli4_hba.io_xri_cnt++;
James Smart0794d602019-01-28 11:14:19 -08004177 }
4178 lpfc_printf_log(phba, KERN_INFO, LOG_NVME,
4179 "6114 Allocate %d out of %d requested new NVME "
4180 "buffers\n", bcnt, num_to_alloc);
4181
4182 /* post the list of nvme buffer sgls to port if available */
4183 if (!list_empty(&post_nblist))
James Smart5e5b5112019-01-28 11:14:22 -08004184 num_posted = lpfc_sli4_post_io_sgl_list(
James Smart0794d602019-01-28 11:14:19 -08004185 phba, &post_nblist, bcnt);
4186 else
4187 num_posted = 0;
4188
4189 return num_posted;
4190}
4191
James Smart96418b52017-03-04 09:30:31 -08004192static uint64_t
4193lpfc_get_wwpn(struct lpfc_hba *phba)
4194{
4195 uint64_t wwn;
4196 int rc;
4197 LPFC_MBOXQ_t *mboxq;
4198 MAILBOX_t *mb;
4199
James Smart96418b52017-03-04 09:30:31 -08004200 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
4201 GFP_KERNEL);
4202 if (!mboxq)
4203 return (uint64_t)-1;
4204
4205 /* First get WWN of HBA instance */
4206 lpfc_read_nv(phba, mboxq);
4207 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4208 if (rc != MBX_SUCCESS) {
4209 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4210 "6019 Mailbox failed , mbxCmd x%x "
4211 "READ_NV, mbxStatus x%x\n",
4212 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
4213 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
4214 mempool_free(mboxq, phba->mbox_mem_pool);
4215 return (uint64_t) -1;
4216 }
4217 mb = &mboxq->u.mb;
4218 memcpy(&wwn, (char *)mb->un.varRDnvp.portname, sizeof(uint64_t));
4219 /* wwn is WWPN of HBA instance */
4220 mempool_free(mboxq, phba->mbox_mem_pool);
4221 if (phba->sli_rev == LPFC_SLI_REV4)
4222 return be64_to_cpu(wwn);
4223 else
Maurizio Lombardi286871a2017-08-23 16:55:48 -07004224 return rol64(wwn, 32);
James Smart96418b52017-03-04 09:30:31 -08004225}
4226
James Smarte59058c2008-08-24 21:49:00 -04004227/**
James Smart3621a712009-04-06 18:47:14 -04004228 * lpfc_create_port - Create an FC port
James Smarte59058c2008-08-24 21:49:00 -04004229 * @phba: pointer to lpfc hba data structure.
4230 * @instance: a unique integer ID to this FC port.
4231 * @dev: pointer to the device data structure.
4232 *
4233 * This routine creates a FC port for the upper layer protocol. The FC port
4234 * can be created on top of either a physical port or a virtual port provided
4235 * by the HBA. This routine also allocates a SCSI host data structure (shost)
4236 * and associates the FC port created before adding the shost into the SCSI
4237 * layer.
4238 *
4239 * Return codes
4240 * @vport - pointer to the virtual N_Port data structure.
4241 * NULL - port create failed.
4242 **/
James Smart2e0fef82007-06-17 19:56:36 -05004243struct lpfc_vport *
James Smart3de2a652007-08-02 11:09:59 -04004244lpfc_create_port(struct lpfc_hba *phba, int instance, struct device *dev)
James Smart47a86172007-04-25 09:53:22 -04004245{
James Smart2e0fef82007-06-17 19:56:36 -05004246 struct lpfc_vport *vport;
James Smart895427b2017-02-12 13:52:30 -08004247 struct Scsi_Host *shost = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05004248 int error = 0;
James Smart96418b52017-03-04 09:30:31 -08004249 int i;
4250 uint64_t wwn;
4251 bool use_no_reset_hba = false;
James Smart56bc8022017-06-15 22:56:43 -07004252 int rc;
James Smart96418b52017-03-04 09:30:31 -08004253
James Smart56bc8022017-06-15 22:56:43 -07004254 if (lpfc_no_hba_reset_cnt) {
4255 if (phba->sli_rev < LPFC_SLI_REV4 &&
4256 dev == &phba->pcidev->dev) {
4257 /* Reset the port first */
4258 lpfc_sli_brdrestart(phba);
4259 rc = lpfc_sli_chipset_init(phba);
4260 if (rc)
4261 return NULL;
4262 }
4263 wwn = lpfc_get_wwpn(phba);
4264 }
James Smart96418b52017-03-04 09:30:31 -08004265
4266 for (i = 0; i < lpfc_no_hba_reset_cnt; i++) {
4267 if (wwn == lpfc_no_hba_reset[i]) {
4268 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4269 "6020 Setting use_no_reset port=%llx\n",
4270 wwn);
4271 use_no_reset_hba = true;
4272 break;
4273 }
4274 }
James Smart47a86172007-04-25 09:53:22 -04004275
James Smart895427b2017-02-12 13:52:30 -08004276 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) {
4277 if (dev != &phba->pcidev->dev) {
4278 shost = scsi_host_alloc(&lpfc_vport_template,
4279 sizeof(struct lpfc_vport));
4280 } else {
James Smart96418b52017-03-04 09:30:31 -08004281 if (!use_no_reset_hba)
James Smart895427b2017-02-12 13:52:30 -08004282 shost = scsi_host_alloc(&lpfc_template,
4283 sizeof(struct lpfc_vport));
4284 else
James Smart96418b52017-03-04 09:30:31 -08004285 shost = scsi_host_alloc(&lpfc_template_no_hr,
James Smart895427b2017-02-12 13:52:30 -08004286 sizeof(struct lpfc_vport));
4287 }
4288 } else if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
4289 shost = scsi_host_alloc(&lpfc_template_nvme,
James Smartea4142f2015-04-07 15:07:13 -04004290 sizeof(struct lpfc_vport));
4291 }
James Smart2e0fef82007-06-17 19:56:36 -05004292 if (!shost)
4293 goto out;
James Smart47a86172007-04-25 09:53:22 -04004294
James Smart2e0fef82007-06-17 19:56:36 -05004295 vport = (struct lpfc_vport *) shost->hostdata;
4296 vport->phba = phba;
James Smart2e0fef82007-06-17 19:56:36 -05004297 vport->load_flag |= FC_LOADING;
James Smart92d7f7b2007-06-17 19:56:38 -05004298 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart7f5f3d02008-02-08 18:50:14 -05004299 vport->fc_rscn_flush = 0;
James Smart3de2a652007-08-02 11:09:59 -04004300 lpfc_get_vport_cfgparam(vport);
James Smart895427b2017-02-12 13:52:30 -08004301
James Smartf6e84792019-01-28 11:14:38 -08004302 /* Adjust value in vport */
4303 vport->cfg_enable_fc4_type = phba->cfg_enable_fc4_type;
4304
James Smart2e0fef82007-06-17 19:56:36 -05004305 shost->unique_id = instance;
4306 shost->max_id = LPFC_MAX_TARGET;
James Smart3de2a652007-08-02 11:09:59 -04004307 shost->max_lun = vport->cfg_max_luns;
James Smart2e0fef82007-06-17 19:56:36 -05004308 shost->this_id = -1;
4309 shost->max_cmd_len = 16;
James Smart6a828b02019-01-28 11:14:31 -08004310
James Smartda0436e2009-05-22 14:51:39 -04004311 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart6a828b02019-01-28 11:14:31 -08004312 if (phba->cfg_fcp_io_sched == LPFC_FCP_SCHED_BY_HDWQ)
4313 shost->nr_hw_queues = phba->cfg_hdw_queue;
4314 else
4315 shost->nr_hw_queues = phba->sli4_hba.num_present_cpu;
4316
James Smart28baac72010-02-12 14:42:03 -05004317 shost->dma_boundary =
James Smartcb5172e2010-03-15 11:25:07 -04004318 phba->sli4_hba.pc_sli4_params.sge_supp_len-1;
James Smart5b9e70b2018-09-10 10:30:42 -07004319 shost->sg_tablesize = phba->cfg_scsi_seg_cnt;
James Smartace44e42019-01-28 11:14:27 -08004320 } else
4321 /* SLI-3 has a limited number of hardware queues (3),
4322 * thus there is only one for FCP processing.
4323 */
4324 shost->nr_hw_queues = 1;
James Smart81301a92008-12-04 22:39:46 -05004325
James Smart47a86172007-04-25 09:53:22 -04004326 /*
James Smart2e0fef82007-06-17 19:56:36 -05004327 * Set initial can_queue value since 0 is no longer supported and
4328 * scsi_add_host will fail. This will be adjusted later based on the
4329 * max xri value determined in hba setup.
James Smart47a86172007-04-25 09:53:22 -04004330 */
James Smart2e0fef82007-06-17 19:56:36 -05004331 shost->can_queue = phba->cfg_hba_queue_depth - 10;
James Smart3de2a652007-08-02 11:09:59 -04004332 if (dev != &phba->pcidev->dev) {
James Smart92d7f7b2007-06-17 19:56:38 -05004333 shost->transportt = lpfc_vport_transport_template;
4334 vport->port_type = LPFC_NPIV_PORT;
4335 } else {
4336 shost->transportt = lpfc_transport_template;
4337 vport->port_type = LPFC_PHYSICAL_PORT;
4338 }
James Smart47a86172007-04-25 09:53:22 -04004339
James Smart2e0fef82007-06-17 19:56:36 -05004340 /* Initialize all internally managed lists. */
4341 INIT_LIST_HEAD(&vport->fc_nodes);
James Smartda0436e2009-05-22 14:51:39 -04004342 INIT_LIST_HEAD(&vport->rcv_buffer_list);
James Smart2e0fef82007-06-17 19:56:36 -05004343 spin_lock_init(&vport->work_port_lock);
James Smart47a86172007-04-25 09:53:22 -04004344
Kees Cookf22eb4d2017-09-06 20:24:26 -07004345 timer_setup(&vport->fc_disctmo, lpfc_disc_timeout, 0);
James Smart47a86172007-04-25 09:53:22 -04004346
Kees Cookf22eb4d2017-09-06 20:24:26 -07004347 timer_setup(&vport->els_tmofunc, lpfc_els_timeout, 0);
James Smart92494142011-02-16 12:39:44 -05004348
Kees Cookf22eb4d2017-09-06 20:24:26 -07004349 timer_setup(&vport->delayed_disc_tmo, lpfc_delayed_disc_tmo, 0);
James Smart92494142011-02-16 12:39:44 -05004350
James Bottomleyd139b9b2009-11-05 13:33:12 -06004351 error = scsi_add_host_with_dma(shost, dev, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05004352 if (error)
4353 goto out_put_shost;
James Smart47a86172007-04-25 09:53:22 -04004354
James Smart523128e2018-09-10 10:30:46 -07004355 spin_lock_irq(&phba->port_list_lock);
James Smart2e0fef82007-06-17 19:56:36 -05004356 list_add_tail(&vport->listentry, &phba->port_list);
James Smart523128e2018-09-10 10:30:46 -07004357 spin_unlock_irq(&phba->port_list_lock);
James Smart2e0fef82007-06-17 19:56:36 -05004358 return vport;
James Smart47a86172007-04-25 09:53:22 -04004359
James Smart2e0fef82007-06-17 19:56:36 -05004360out_put_shost:
4361 scsi_host_put(shost);
4362out:
4363 return NULL;
James Smart47a86172007-04-25 09:53:22 -04004364}
4365
James Smarte59058c2008-08-24 21:49:00 -04004366/**
James Smart3621a712009-04-06 18:47:14 -04004367 * destroy_port - destroy an FC port
James Smarte59058c2008-08-24 21:49:00 -04004368 * @vport: pointer to an lpfc virtual N_Port data structure.
4369 *
4370 * This routine destroys a FC port from the upper layer protocol. All the
4371 * resources associated with the port are released.
4372 **/
James Smart2e0fef82007-06-17 19:56:36 -05004373void
4374destroy_port(struct lpfc_vport *vport)
James Smart47a86172007-04-25 09:53:22 -04004375{
James Smart92d7f7b2007-06-17 19:56:38 -05004376 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4377 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04004378
James Smart858c9f62007-06-17 19:56:39 -05004379 lpfc_debugfs_terminate(vport);
James Smart92d7f7b2007-06-17 19:56:38 -05004380 fc_remove_host(shost);
4381 scsi_remove_host(shost);
James Smart47a86172007-04-25 09:53:22 -04004382
James Smart523128e2018-09-10 10:30:46 -07004383 spin_lock_irq(&phba->port_list_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05004384 list_del_init(&vport->listentry);
James Smart523128e2018-09-10 10:30:46 -07004385 spin_unlock_irq(&phba->port_list_lock);
James Smart47a86172007-04-25 09:53:22 -04004386
James Smart92d7f7b2007-06-17 19:56:38 -05004387 lpfc_cleanup(vport);
James Smart47a86172007-04-25 09:53:22 -04004388 return;
James Smart47a86172007-04-25 09:53:22 -04004389}
4390
James Smarte59058c2008-08-24 21:49:00 -04004391/**
James Smart3621a712009-04-06 18:47:14 -04004392 * lpfc_get_instance - Get a unique integer ID
James Smarte59058c2008-08-24 21:49:00 -04004393 *
4394 * This routine allocates a unique integer ID from lpfc_hba_index pool. It
4395 * uses the kernel idr facility to perform the task.
4396 *
4397 * Return codes:
4398 * instance - a unique integer ID allocated as the new instance.
4399 * -1 - lpfc get instance failed.
4400 **/
James Smart92d7f7b2007-06-17 19:56:38 -05004401int
4402lpfc_get_instance(void)
4403{
Tejun Heoab516032013-02-27 17:04:44 -08004404 int ret;
James Smart92d7f7b2007-06-17 19:56:38 -05004405
Tejun Heoab516032013-02-27 17:04:44 -08004406 ret = idr_alloc(&lpfc_hba_index, NULL, 0, 0, GFP_KERNEL);
4407 return ret < 0 ? -1 : ret;
James Smart92d7f7b2007-06-17 19:56:38 -05004408}
4409
James Smarte59058c2008-08-24 21:49:00 -04004410/**
James Smart3621a712009-04-06 18:47:14 -04004411 * lpfc_scan_finished - method for SCSI layer to detect whether scan is done
James Smarte59058c2008-08-24 21:49:00 -04004412 * @shost: pointer to SCSI host data structure.
4413 * @time: elapsed time of the scan in jiffies.
4414 *
4415 * This routine is called by the SCSI layer with a SCSI host to determine
4416 * whether the scan host is finished.
4417 *
4418 * Note: there is no scan_start function as adapter initialization will have
4419 * asynchronously kicked off the link initialization.
4420 *
4421 * Return codes
4422 * 0 - SCSI host scan is not over yet.
4423 * 1 - SCSI host scan is over.
4424 **/
James Smart47a86172007-04-25 09:53:22 -04004425int lpfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
4426{
James Smart2e0fef82007-06-17 19:56:36 -05004427 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4428 struct lpfc_hba *phba = vport->phba;
James Smart858c9f62007-06-17 19:56:39 -05004429 int stat = 0;
James Smart47a86172007-04-25 09:53:22 -04004430
James Smart858c9f62007-06-17 19:56:39 -05004431 spin_lock_irq(shost->host_lock);
4432
James Smart51ef4c22007-08-02 11:10:31 -04004433 if (vport->load_flag & FC_UNLOADING) {
James Smart858c9f62007-06-17 19:56:39 -05004434 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04004435 goto finished;
James Smart858c9f62007-06-17 19:56:39 -05004436 }
James Smart256ec0d2013-04-17 20:14:58 -04004437 if (time >= msecs_to_jiffies(30 * 1000)) {
James Smart2e0fef82007-06-17 19:56:36 -05004438 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004439 "0461 Scanning longer than 30 "
4440 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05004441 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04004442 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05004443 }
James Smart256ec0d2013-04-17 20:14:58 -04004444 if (time >= msecs_to_jiffies(15 * 1000) &&
4445 phba->link_state <= LPFC_LINK_DOWN) {
James Smart2e0fef82007-06-17 19:56:36 -05004446 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004447 "0465 Link down longer than 15 "
4448 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05004449 stat = 1;
James Smart2e0fef82007-06-17 19:56:36 -05004450 goto finished;
James Smart47a86172007-04-25 09:53:22 -04004451 }
4452
James Smart2e0fef82007-06-17 19:56:36 -05004453 if (vport->port_state != LPFC_VPORT_READY)
James Smart858c9f62007-06-17 19:56:39 -05004454 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05004455 if (vport->num_disc_nodes || vport->fc_prli_sent)
James Smart858c9f62007-06-17 19:56:39 -05004456 goto finished;
James Smart256ec0d2013-04-17 20:14:58 -04004457 if (vport->fc_map_cnt == 0 && time < msecs_to_jiffies(2 * 1000))
James Smart858c9f62007-06-17 19:56:39 -05004458 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05004459 if ((phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) != 0)
James Smart858c9f62007-06-17 19:56:39 -05004460 goto finished;
4461
4462 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04004463
4464finished:
James Smart858c9f62007-06-17 19:56:39 -05004465 spin_unlock_irq(shost->host_lock);
4466 return stat;
James Smart92d7f7b2007-06-17 19:56:38 -05004467}
4468
Bart Van Assche3999df72019-03-28 11:06:16 -07004469static void lpfc_host_supported_speeds_set(struct Scsi_Host *shost)
James Smartcd713482018-10-23 13:41:01 -07004470{
4471 struct lpfc_vport *vport = (struct lpfc_vport *)shost->hostdata;
4472 struct lpfc_hba *phba = vport->phba;
4473
4474 fc_host_supported_speeds(shost) = 0;
James Smart1dc5ec22018-10-23 13:41:11 -07004475 if (phba->lmt & LMT_128Gb)
4476 fc_host_supported_speeds(shost) |= FC_PORTSPEED_128GBIT;
James Smartcd713482018-10-23 13:41:01 -07004477 if (phba->lmt & LMT_64Gb)
4478 fc_host_supported_speeds(shost) |= FC_PORTSPEED_64GBIT;
4479 if (phba->lmt & LMT_32Gb)
4480 fc_host_supported_speeds(shost) |= FC_PORTSPEED_32GBIT;
4481 if (phba->lmt & LMT_16Gb)
4482 fc_host_supported_speeds(shost) |= FC_PORTSPEED_16GBIT;
4483 if (phba->lmt & LMT_10Gb)
4484 fc_host_supported_speeds(shost) |= FC_PORTSPEED_10GBIT;
4485 if (phba->lmt & LMT_8Gb)
4486 fc_host_supported_speeds(shost) |= FC_PORTSPEED_8GBIT;
4487 if (phba->lmt & LMT_4Gb)
4488 fc_host_supported_speeds(shost) |= FC_PORTSPEED_4GBIT;
4489 if (phba->lmt & LMT_2Gb)
4490 fc_host_supported_speeds(shost) |= FC_PORTSPEED_2GBIT;
4491 if (phba->lmt & LMT_1Gb)
4492 fc_host_supported_speeds(shost) |= FC_PORTSPEED_1GBIT;
4493}
4494
James Smarte59058c2008-08-24 21:49:00 -04004495/**
James Smart3621a712009-04-06 18:47:14 -04004496 * lpfc_host_attrib_init - Initialize SCSI host attributes on a FC port
James Smarte59058c2008-08-24 21:49:00 -04004497 * @shost: pointer to SCSI host data structure.
4498 *
4499 * This routine initializes a given SCSI host attributes on a FC port. The
4500 * SCSI host can be either on top of a physical port or a virtual port.
4501 **/
James Smart92d7f7b2007-06-17 19:56:38 -05004502void lpfc_host_attrib_init(struct Scsi_Host *shost)
4503{
4504 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4505 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04004506 /*
James Smart2e0fef82007-06-17 19:56:36 -05004507 * Set fixed host attributes. Must done after lpfc_sli_hba_setup().
James Smart47a86172007-04-25 09:53:22 -04004508 */
4509
James Smart2e0fef82007-06-17 19:56:36 -05004510 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
4511 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
James Smart47a86172007-04-25 09:53:22 -04004512 fc_host_supported_classes(shost) = FC_COS_CLASS3;
4513
4514 memset(fc_host_supported_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05004515 sizeof(fc_host_supported_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04004516 fc_host_supported_fc4s(shost)[2] = 1;
4517 fc_host_supported_fc4s(shost)[7] = 1;
4518
James Smart92d7f7b2007-06-17 19:56:38 -05004519 lpfc_vport_symbolic_node_name(vport, fc_host_symbolic_name(shost),
4520 sizeof fc_host_symbolic_name(shost));
James Smart47a86172007-04-25 09:53:22 -04004521
James Smartcd713482018-10-23 13:41:01 -07004522 lpfc_host_supported_speeds_set(shost);
James Smart47a86172007-04-25 09:53:22 -04004523
4524 fc_host_maxframe_size(shost) =
James Smart2e0fef82007-06-17 19:56:36 -05004525 (((uint32_t) vport->fc_sparam.cmn.bbRcvSizeMsb & 0x0F) << 8) |
4526 (uint32_t) vport->fc_sparam.cmn.bbRcvSizeLsb;
James Smart47a86172007-04-25 09:53:22 -04004527
Mike Christie0af5d702010-09-15 16:52:31 -05004528 fc_host_dev_loss_tmo(shost) = vport->cfg_devloss_tmo;
4529
James Smart47a86172007-04-25 09:53:22 -04004530 /* This value is also unchanging */
4531 memset(fc_host_active_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05004532 sizeof(fc_host_active_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04004533 fc_host_active_fc4s(shost)[2] = 1;
4534 fc_host_active_fc4s(shost)[7] = 1;
4535
James Smart92d7f7b2007-06-17 19:56:38 -05004536 fc_host_max_npiv_vports(shost) = phba->max_vpi;
James Smart47a86172007-04-25 09:53:22 -04004537 spin_lock_irq(shost->host_lock);
James Smart51ef4c22007-08-02 11:10:31 -04004538 vport->load_flag &= ~FC_LOADING;
James Smart47a86172007-04-25 09:53:22 -04004539 spin_unlock_irq(shost->host_lock);
James Smart47a86172007-04-25 09:53:22 -04004540}
dea31012005-04-17 16:05:31 -05004541
James Smarte59058c2008-08-24 21:49:00 -04004542/**
James Smartda0436e2009-05-22 14:51:39 -04004543 * lpfc_stop_port_s3 - Stop SLI3 device port
James Smarte59058c2008-08-24 21:49:00 -04004544 * @phba: pointer to lpfc hba data structure.
4545 *
James Smartda0436e2009-05-22 14:51:39 -04004546 * This routine is invoked to stop an SLI3 device port, it stops the device
4547 * from generating interrupts and stops the device driver's timers for the
4548 * device.
James Smarte59058c2008-08-24 21:49:00 -04004549 **/
James Smartdb2378e2008-02-08 18:49:51 -05004550static void
James Smartda0436e2009-05-22 14:51:39 -04004551lpfc_stop_port_s3(struct lpfc_hba *phba)
James Smartdb2378e2008-02-08 18:49:51 -05004552{
James Smartda0436e2009-05-22 14:51:39 -04004553 /* Clear all interrupt enable conditions */
4554 writel(0, phba->HCregaddr);
4555 readl(phba->HCregaddr); /* flush */
4556 /* Clear all pending interrupts */
4557 writel(0xffffffff, phba->HAregaddr);
4558 readl(phba->HAregaddr); /* flush */
James Smart93996272008-08-24 21:50:30 -04004559
James Smartda0436e2009-05-22 14:51:39 -04004560 /* Reset some HBA SLI setup states */
4561 lpfc_stop_hba_timers(phba);
4562 phba->pport->work_port_events = 0;
James Smartdb2378e2008-02-08 18:49:51 -05004563}
4564
James Smarte59058c2008-08-24 21:49:00 -04004565/**
James Smartda0436e2009-05-22 14:51:39 -04004566 * lpfc_stop_port_s4 - Stop SLI4 device port
James Smart5b75da22008-12-04 22:39:35 -05004567 * @phba: pointer to lpfc hba data structure.
4568 *
James Smartda0436e2009-05-22 14:51:39 -04004569 * This routine is invoked to stop an SLI4 device port, it stops the device
4570 * from generating interrupts and stops the device driver's timers for the
4571 * device.
4572 **/
4573static void
4574lpfc_stop_port_s4(struct lpfc_hba *phba)
4575{
4576 /* Reset some HBA SLI4 setup states */
4577 lpfc_stop_hba_timers(phba);
James Smartcdb42be2019-01-28 11:14:21 -08004578 if (phba->pport)
4579 phba->pport->work_port_events = 0;
James Smartda0436e2009-05-22 14:51:39 -04004580 phba->sli4_hba.intr_enable = 0;
James Smartda0436e2009-05-22 14:51:39 -04004581}
4582
4583/**
4584 * lpfc_stop_port - Wrapper function for stopping hba port
4585 * @phba: Pointer to HBA context object.
James Smart5b75da22008-12-04 22:39:35 -05004586 *
James Smartda0436e2009-05-22 14:51:39 -04004587 * This routine wraps the actual SLI3 or SLI4 hba stop port routine from
4588 * the API jump table function pointer from the lpfc_hba struct.
4589 **/
4590void
4591lpfc_stop_port(struct lpfc_hba *phba)
4592{
4593 phba->lpfc_stop_port(phba);
Dick Kennedyf485c182017-09-29 17:34:34 -07004594
4595 if (phba->wq)
4596 flush_workqueue(phba->wq);
James Smartda0436e2009-05-22 14:51:39 -04004597}
4598
4599/**
James Smartecfd03c2010-02-12 14:41:27 -05004600 * lpfc_fcf_redisc_wait_start_timer - Start fcf rediscover wait timer
4601 * @phba: Pointer to hba for which this call is being executed.
4602 *
4603 * This routine starts the timer waiting for the FCF rediscovery to complete.
4604 **/
4605void
4606lpfc_fcf_redisc_wait_start_timer(struct lpfc_hba *phba)
4607{
4608 unsigned long fcf_redisc_wait_tmo =
4609 (jiffies + msecs_to_jiffies(LPFC_FCF_REDISCOVER_WAIT_TMO));
4610 /* Start fcf rediscovery wait period timer */
4611 mod_timer(&phba->fcf.redisc_wait, fcf_redisc_wait_tmo);
4612 spin_lock_irq(&phba->hbalock);
4613 /* Allow action to new fcf asynchronous event */
4614 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
4615 /* Mark the FCF rediscovery pending state */
4616 phba->fcf.fcf_flag |= FCF_REDISC_PEND;
4617 spin_unlock_irq(&phba->hbalock);
4618}
4619
4620/**
4621 * lpfc_sli4_fcf_redisc_wait_tmo - FCF table rediscover wait timeout
4622 * @ptr: Map to lpfc_hba data structure pointer.
4623 *
4624 * This routine is invoked when waiting for FCF table rediscover has been
4625 * timed out. If new FCF record(s) has (have) been discovered during the
4626 * wait period, a new FCF event shall be added to the FCOE async event
4627 * list, and then worker thread shall be waked up for processing from the
4628 * worker thread context.
4629 **/
Rashika Kheriae399b222014-09-03 12:55:28 -04004630static void
Kees Cookf22eb4d2017-09-06 20:24:26 -07004631lpfc_sli4_fcf_redisc_wait_tmo(struct timer_list *t)
James Smartecfd03c2010-02-12 14:41:27 -05004632{
Kees Cookf22eb4d2017-09-06 20:24:26 -07004633 struct lpfc_hba *phba = from_timer(phba, t, fcf.redisc_wait);
James Smartecfd03c2010-02-12 14:41:27 -05004634
4635 /* Don't send FCF rediscovery event if timer cancelled */
4636 spin_lock_irq(&phba->hbalock);
4637 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
4638 spin_unlock_irq(&phba->hbalock);
4639 return;
4640 }
4641 /* Clear FCF rediscovery timer pending flag */
4642 phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
4643 /* FCF rediscovery event to worker thread */
4644 phba->fcf.fcf_flag |= FCF_REDISC_EVT;
4645 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004646 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04004647 "2776 FCF rediscover quiescent timer expired\n");
James Smartecfd03c2010-02-12 14:41:27 -05004648 /* wake up worker thread */
4649 lpfc_worker_wake_up(phba);
4650}
4651
4652/**
James Smartda0436e2009-05-22 14:51:39 -04004653 * lpfc_sli4_parse_latt_fault - Parse sli4 link-attention link fault code
4654 * @phba: pointer to lpfc hba data structure.
4655 * @acqe_link: pointer to the async link completion queue entry.
4656 *
James Smart23288b72018-05-04 20:37:53 -07004657 * This routine is to parse the SLI4 link-attention link fault code.
James Smartda0436e2009-05-22 14:51:39 -04004658 **/
James Smart23288b72018-05-04 20:37:53 -07004659static void
James Smartda0436e2009-05-22 14:51:39 -04004660lpfc_sli4_parse_latt_fault(struct lpfc_hba *phba,
4661 struct lpfc_acqe_link *acqe_link)
4662{
James Smartda0436e2009-05-22 14:51:39 -04004663 switch (bf_get(lpfc_acqe_link_fault, acqe_link)) {
4664 case LPFC_ASYNC_LINK_FAULT_NONE:
4665 case LPFC_ASYNC_LINK_FAULT_LOCAL:
4666 case LPFC_ASYNC_LINK_FAULT_REMOTE:
James Smart23288b72018-05-04 20:37:53 -07004667 case LPFC_ASYNC_LINK_FAULT_LR_LRR:
James Smartda0436e2009-05-22 14:51:39 -04004668 break;
4669 default:
4670 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart23288b72018-05-04 20:37:53 -07004671 "0398 Unknown link fault code: x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04004672 bf_get(lpfc_acqe_link_fault, acqe_link));
James Smartda0436e2009-05-22 14:51:39 -04004673 break;
4674 }
James Smartda0436e2009-05-22 14:51:39 -04004675}
4676
4677/**
4678 * lpfc_sli4_parse_latt_type - Parse sli4 link attention type
4679 * @phba: pointer to lpfc hba data structure.
4680 * @acqe_link: pointer to the async link completion queue entry.
4681 *
4682 * This routine is to parse the SLI4 link attention type and translate it
4683 * into the base driver's link attention type coding.
4684 *
4685 * Return: Link attention type in terms of base driver's coding.
4686 **/
4687static uint8_t
4688lpfc_sli4_parse_latt_type(struct lpfc_hba *phba,
4689 struct lpfc_acqe_link *acqe_link)
4690{
4691 uint8_t att_type;
4692
4693 switch (bf_get(lpfc_acqe_link_status, acqe_link)) {
4694 case LPFC_ASYNC_LINK_STATUS_DOWN:
4695 case LPFC_ASYNC_LINK_STATUS_LOGICAL_DOWN:
James Smart76a95d72010-11-20 23:11:48 -05004696 att_type = LPFC_ATT_LINK_DOWN;
James Smartda0436e2009-05-22 14:51:39 -04004697 break;
4698 case LPFC_ASYNC_LINK_STATUS_UP:
4699 /* Ignore physical link up events - wait for logical link up */
James Smart76a95d72010-11-20 23:11:48 -05004700 att_type = LPFC_ATT_RESERVED;
James Smartda0436e2009-05-22 14:51:39 -04004701 break;
4702 case LPFC_ASYNC_LINK_STATUS_LOGICAL_UP:
James Smart76a95d72010-11-20 23:11:48 -05004703 att_type = LPFC_ATT_LINK_UP;
James Smartda0436e2009-05-22 14:51:39 -04004704 break;
4705 default:
4706 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4707 "0399 Invalid link attention type: x%x\n",
4708 bf_get(lpfc_acqe_link_status, acqe_link));
James Smart76a95d72010-11-20 23:11:48 -05004709 att_type = LPFC_ATT_RESERVED;
James Smartda0436e2009-05-22 14:51:39 -04004710 break;
4711 }
4712 return att_type;
4713}
4714
4715/**
James Smart8b68cd52012-09-29 11:32:37 -04004716 * lpfc_sli_port_speed_get - Get sli3 link speed code to link speed
4717 * @phba: pointer to lpfc hba data structure.
4718 *
4719 * This routine is to get an SLI3 FC port's link speed in Mbps.
4720 *
4721 * Return: link speed in terms of Mbps.
4722 **/
4723uint32_t
4724lpfc_sli_port_speed_get(struct lpfc_hba *phba)
4725{
4726 uint32_t link_speed;
4727
4728 if (!lpfc_is_link_up(phba))
4729 return 0;
4730
James Smarta085e872015-12-16 18:12:02 -05004731 if (phba->sli_rev <= LPFC_SLI_REV3) {
4732 switch (phba->fc_linkspeed) {
4733 case LPFC_LINK_SPEED_1GHZ:
4734 link_speed = 1000;
4735 break;
4736 case LPFC_LINK_SPEED_2GHZ:
4737 link_speed = 2000;
4738 break;
4739 case LPFC_LINK_SPEED_4GHZ:
4740 link_speed = 4000;
4741 break;
4742 case LPFC_LINK_SPEED_8GHZ:
4743 link_speed = 8000;
4744 break;
4745 case LPFC_LINK_SPEED_10GHZ:
4746 link_speed = 10000;
4747 break;
4748 case LPFC_LINK_SPEED_16GHZ:
4749 link_speed = 16000;
4750 break;
4751 default:
4752 link_speed = 0;
4753 }
4754 } else {
4755 if (phba->sli4_hba.link_state.logical_speed)
4756 link_speed =
4757 phba->sli4_hba.link_state.logical_speed;
4758 else
4759 link_speed = phba->sli4_hba.link_state.speed;
James Smart8b68cd52012-09-29 11:32:37 -04004760 }
4761 return link_speed;
4762}
4763
4764/**
4765 * lpfc_sli4_port_speed_parse - Parse async evt link speed code to link speed
4766 * @phba: pointer to lpfc hba data structure.
4767 * @evt_code: asynchronous event code.
4768 * @speed_code: asynchronous event link speed code.
4769 *
4770 * This routine is to parse the giving SLI4 async event link speed code into
4771 * value of Mbps for the link speed.
4772 *
4773 * Return: link speed in terms of Mbps.
4774 **/
4775static uint32_t
4776lpfc_sli4_port_speed_parse(struct lpfc_hba *phba, uint32_t evt_code,
4777 uint8_t speed_code)
4778{
4779 uint32_t port_speed;
4780
4781 switch (evt_code) {
4782 case LPFC_TRAILER_CODE_LINK:
4783 switch (speed_code) {
James Smart26d830e2015-04-07 15:07:17 -04004784 case LPFC_ASYNC_LINK_SPEED_ZERO:
James Smart8b68cd52012-09-29 11:32:37 -04004785 port_speed = 0;
4786 break;
James Smart26d830e2015-04-07 15:07:17 -04004787 case LPFC_ASYNC_LINK_SPEED_10MBPS:
James Smart8b68cd52012-09-29 11:32:37 -04004788 port_speed = 10;
4789 break;
James Smart26d830e2015-04-07 15:07:17 -04004790 case LPFC_ASYNC_LINK_SPEED_100MBPS:
James Smart8b68cd52012-09-29 11:32:37 -04004791 port_speed = 100;
4792 break;
James Smart26d830e2015-04-07 15:07:17 -04004793 case LPFC_ASYNC_LINK_SPEED_1GBPS:
James Smart8b68cd52012-09-29 11:32:37 -04004794 port_speed = 1000;
4795 break;
James Smart26d830e2015-04-07 15:07:17 -04004796 case LPFC_ASYNC_LINK_SPEED_10GBPS:
James Smart8b68cd52012-09-29 11:32:37 -04004797 port_speed = 10000;
4798 break;
James Smart26d830e2015-04-07 15:07:17 -04004799 case LPFC_ASYNC_LINK_SPEED_20GBPS:
4800 port_speed = 20000;
4801 break;
4802 case LPFC_ASYNC_LINK_SPEED_25GBPS:
4803 port_speed = 25000;
4804 break;
4805 case LPFC_ASYNC_LINK_SPEED_40GBPS:
4806 port_speed = 40000;
4807 break;
James Smart8b68cd52012-09-29 11:32:37 -04004808 default:
4809 port_speed = 0;
4810 }
4811 break;
4812 case LPFC_TRAILER_CODE_FC:
4813 switch (speed_code) {
James Smart26d830e2015-04-07 15:07:17 -04004814 case LPFC_FC_LA_SPEED_UNKNOWN:
James Smart8b68cd52012-09-29 11:32:37 -04004815 port_speed = 0;
4816 break;
James Smart26d830e2015-04-07 15:07:17 -04004817 case LPFC_FC_LA_SPEED_1G:
James Smart8b68cd52012-09-29 11:32:37 -04004818 port_speed = 1000;
4819 break;
James Smart26d830e2015-04-07 15:07:17 -04004820 case LPFC_FC_LA_SPEED_2G:
James Smart8b68cd52012-09-29 11:32:37 -04004821 port_speed = 2000;
4822 break;
James Smart26d830e2015-04-07 15:07:17 -04004823 case LPFC_FC_LA_SPEED_4G:
James Smart8b68cd52012-09-29 11:32:37 -04004824 port_speed = 4000;
4825 break;
James Smart26d830e2015-04-07 15:07:17 -04004826 case LPFC_FC_LA_SPEED_8G:
James Smart8b68cd52012-09-29 11:32:37 -04004827 port_speed = 8000;
4828 break;
James Smart26d830e2015-04-07 15:07:17 -04004829 case LPFC_FC_LA_SPEED_10G:
James Smart8b68cd52012-09-29 11:32:37 -04004830 port_speed = 10000;
4831 break;
James Smart26d830e2015-04-07 15:07:17 -04004832 case LPFC_FC_LA_SPEED_16G:
James Smart8b68cd52012-09-29 11:32:37 -04004833 port_speed = 16000;
4834 break;
James Smartd38dd522015-08-31 16:48:17 -04004835 case LPFC_FC_LA_SPEED_32G:
4836 port_speed = 32000;
4837 break;
James Smartfbd8a6b2018-02-22 08:18:45 -08004838 case LPFC_FC_LA_SPEED_64G:
4839 port_speed = 64000;
4840 break;
James Smart1dc5ec22018-10-23 13:41:11 -07004841 case LPFC_FC_LA_SPEED_128G:
4842 port_speed = 128000;
4843 break;
James Smart8b68cd52012-09-29 11:32:37 -04004844 default:
4845 port_speed = 0;
4846 }
4847 break;
4848 default:
4849 port_speed = 0;
4850 }
4851 return port_speed;
4852}
4853
4854/**
James Smart70f3c072010-12-15 17:57:33 -05004855 * lpfc_sli4_async_link_evt - Process the asynchronous FCoE link event
James Smartda0436e2009-05-22 14:51:39 -04004856 * @phba: pointer to lpfc hba data structure.
4857 * @acqe_link: pointer to the async link completion queue entry.
4858 *
James Smart70f3c072010-12-15 17:57:33 -05004859 * This routine is to handle the SLI4 asynchronous FCoE link event.
James Smartda0436e2009-05-22 14:51:39 -04004860 **/
4861static void
4862lpfc_sli4_async_link_evt(struct lpfc_hba *phba,
4863 struct lpfc_acqe_link *acqe_link)
4864{
4865 struct lpfc_dmabuf *mp;
4866 LPFC_MBOXQ_t *pmb;
4867 MAILBOX_t *mb;
James Smart76a95d72010-11-20 23:11:48 -05004868 struct lpfc_mbx_read_top *la;
James Smartda0436e2009-05-22 14:51:39 -04004869 uint8_t att_type;
James Smart76a95d72010-11-20 23:11:48 -05004870 int rc;
James Smartda0436e2009-05-22 14:51:39 -04004871
4872 att_type = lpfc_sli4_parse_latt_type(phba, acqe_link);
James Smart76a95d72010-11-20 23:11:48 -05004873 if (att_type != LPFC_ATT_LINK_DOWN && att_type != LPFC_ATT_LINK_UP)
James Smartda0436e2009-05-22 14:51:39 -04004874 return;
James Smart32b97932009-07-19 10:01:21 -04004875 phba->fcoe_eventtag = acqe_link->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04004876 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4877 if (!pmb) {
4878 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4879 "0395 The mboxq allocation failed\n");
4880 return;
4881 }
4882 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4883 if (!mp) {
4884 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4885 "0396 The lpfc_dmabuf allocation failed\n");
4886 goto out_free_pmb;
4887 }
4888 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
4889 if (!mp->virt) {
4890 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4891 "0397 The mbuf allocation failed\n");
4892 goto out_free_dmabuf;
4893 }
4894
4895 /* Cleanup any outstanding ELS commands */
4896 lpfc_els_flush_all_cmd(phba);
4897
4898 /* Block ELS IOCBs until we have done process link event */
James Smart895427b2017-02-12 13:52:30 -08004899 phba->sli4_hba.els_wq->pring->flag |= LPFC_STOP_IOCB_EVENT;
James Smartda0436e2009-05-22 14:51:39 -04004900
4901 /* Update link event statistics */
4902 phba->sli.slistat.link_event++;
4903
James Smart76a95d72010-11-20 23:11:48 -05004904 /* Create lpfc_handle_latt mailbox command from link ACQE */
4905 lpfc_read_topology(phba, pmb, mp);
4906 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
James Smartda0436e2009-05-22 14:51:39 -04004907 pmb->vport = phba->pport;
4908
James Smartda0436e2009-05-22 14:51:39 -04004909 /* Keep the link status for extra SLI4 state machine reference */
4910 phba->sli4_hba.link_state.speed =
James Smart8b68cd52012-09-29 11:32:37 -04004911 lpfc_sli4_port_speed_parse(phba, LPFC_TRAILER_CODE_LINK,
4912 bf_get(lpfc_acqe_link_speed, acqe_link));
James Smartda0436e2009-05-22 14:51:39 -04004913 phba->sli4_hba.link_state.duplex =
4914 bf_get(lpfc_acqe_link_duplex, acqe_link);
4915 phba->sli4_hba.link_state.status =
4916 bf_get(lpfc_acqe_link_status, acqe_link);
James Smart70f3c072010-12-15 17:57:33 -05004917 phba->sli4_hba.link_state.type =
4918 bf_get(lpfc_acqe_link_type, acqe_link);
4919 phba->sli4_hba.link_state.number =
4920 bf_get(lpfc_acqe_link_number, acqe_link);
James Smartda0436e2009-05-22 14:51:39 -04004921 phba->sli4_hba.link_state.fault =
4922 bf_get(lpfc_acqe_link_fault, acqe_link);
James Smart65467b62010-01-26 23:08:29 -05004923 phba->sli4_hba.link_state.logical_speed =
James Smart8b68cd52012-09-29 11:32:37 -04004924 bf_get(lpfc_acqe_logical_link_speed, acqe_link) * 10;
4925
James Smart70f3c072010-12-15 17:57:33 -05004926 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartc31098c2011-04-16 11:03:33 -04004927 "2900 Async FC/FCoE Link event - Speed:%dGBit "
4928 "duplex:x%x LA Type:x%x Port Type:%d Port Number:%d "
4929 "Logical speed:%dMbps Fault:%d\n",
James Smart70f3c072010-12-15 17:57:33 -05004930 phba->sli4_hba.link_state.speed,
4931 phba->sli4_hba.link_state.topology,
4932 phba->sli4_hba.link_state.status,
4933 phba->sli4_hba.link_state.type,
4934 phba->sli4_hba.link_state.number,
James Smart8b68cd52012-09-29 11:32:37 -04004935 phba->sli4_hba.link_state.logical_speed,
James Smart70f3c072010-12-15 17:57:33 -05004936 phba->sli4_hba.link_state.fault);
James Smart76a95d72010-11-20 23:11:48 -05004937 /*
4938 * For FC Mode: issue the READ_TOPOLOGY mailbox command to fetch
4939 * topology info. Note: Optional for non FC-AL ports.
4940 */
4941 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
4942 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
4943 if (rc == MBX_NOT_FINISHED)
4944 goto out_free_dmabuf;
4945 return;
4946 }
4947 /*
4948 * For FCoE Mode: fill in all the topology information we need and call
4949 * the READ_TOPOLOGY completion routine to continue without actually
4950 * sending the READ_TOPOLOGY mailbox command to the port.
4951 */
James Smart23288b72018-05-04 20:37:53 -07004952 /* Initialize completion status */
James Smart76a95d72010-11-20 23:11:48 -05004953 mb = &pmb->u.mb;
James Smart23288b72018-05-04 20:37:53 -07004954 mb->mbxStatus = MBX_SUCCESS;
4955
4956 /* Parse port fault information field */
4957 lpfc_sli4_parse_latt_fault(phba, acqe_link);
James Smart76a95d72010-11-20 23:11:48 -05004958
4959 /* Parse and translate link attention fields */
4960 la = (struct lpfc_mbx_read_top *) &pmb->u.mb.un.varReadTop;
4961 la->eventTag = acqe_link->event_tag;
4962 bf_set(lpfc_mbx_read_top_att_type, la, att_type);
4963 bf_set(lpfc_mbx_read_top_link_spd, la,
James Smarta085e872015-12-16 18:12:02 -05004964 (bf_get(lpfc_acqe_link_speed, acqe_link)));
James Smart76a95d72010-11-20 23:11:48 -05004965
4966 /* Fake the the following irrelvant fields */
4967 bf_set(lpfc_mbx_read_top_topology, la, LPFC_TOPOLOGY_PT_PT);
4968 bf_set(lpfc_mbx_read_top_alpa_granted, la, 0);
4969 bf_set(lpfc_mbx_read_top_il, la, 0);
4970 bf_set(lpfc_mbx_read_top_pb, la, 0);
4971 bf_set(lpfc_mbx_read_top_fa, la, 0);
4972 bf_set(lpfc_mbx_read_top_mm, la, 0);
James Smartda0436e2009-05-22 14:51:39 -04004973
4974 /* Invoke the lpfc_handle_latt mailbox command callback function */
James Smart76a95d72010-11-20 23:11:48 -05004975 lpfc_mbx_cmpl_read_topology(phba, pmb);
James Smartda0436e2009-05-22 14:51:39 -04004976
4977 return;
4978
4979out_free_dmabuf:
4980 kfree(mp);
4981out_free_pmb:
4982 mempool_free(pmb, phba->mbox_mem_pool);
4983}
4984
4985/**
James Smart1dc5ec22018-10-23 13:41:11 -07004986 * lpfc_async_link_speed_to_read_top - Parse async evt link speed code to read
4987 * topology.
4988 * @phba: pointer to lpfc hba data structure.
4989 * @evt_code: asynchronous event code.
4990 * @speed_code: asynchronous event link speed code.
4991 *
4992 * This routine is to parse the giving SLI4 async event link speed code into
4993 * value of Read topology link speed.
4994 *
4995 * Return: link speed in terms of Read topology.
4996 **/
4997static uint8_t
4998lpfc_async_link_speed_to_read_top(struct lpfc_hba *phba, uint8_t speed_code)
4999{
5000 uint8_t port_speed;
5001
5002 switch (speed_code) {
5003 case LPFC_FC_LA_SPEED_1G:
5004 port_speed = LPFC_LINK_SPEED_1GHZ;
5005 break;
5006 case LPFC_FC_LA_SPEED_2G:
5007 port_speed = LPFC_LINK_SPEED_2GHZ;
5008 break;
5009 case LPFC_FC_LA_SPEED_4G:
5010 port_speed = LPFC_LINK_SPEED_4GHZ;
5011 break;
5012 case LPFC_FC_LA_SPEED_8G:
5013 port_speed = LPFC_LINK_SPEED_8GHZ;
5014 break;
5015 case LPFC_FC_LA_SPEED_16G:
5016 port_speed = LPFC_LINK_SPEED_16GHZ;
5017 break;
5018 case LPFC_FC_LA_SPEED_32G:
5019 port_speed = LPFC_LINK_SPEED_32GHZ;
5020 break;
5021 case LPFC_FC_LA_SPEED_64G:
5022 port_speed = LPFC_LINK_SPEED_64GHZ;
5023 break;
5024 case LPFC_FC_LA_SPEED_128G:
5025 port_speed = LPFC_LINK_SPEED_128GHZ;
5026 break;
5027 case LPFC_FC_LA_SPEED_256G:
5028 port_speed = LPFC_LINK_SPEED_256GHZ;
5029 break;
5030 default:
5031 port_speed = 0;
5032 break;
5033 }
5034
5035 return port_speed;
5036}
5037
5038#define trunk_link_status(__idx)\
5039 bf_get(lpfc_acqe_fc_la_trunk_config_port##__idx, acqe_fc) ?\
5040 ((phba->trunk_link.link##__idx.state == LPFC_LINK_UP) ?\
5041 "Link up" : "Link down") : "NA"
5042/* Did port __idx reported an error */
5043#define trunk_port_fault(__idx)\
5044 bf_get(lpfc_acqe_fc_la_trunk_config_port##__idx, acqe_fc) ?\
5045 (port_fault & (1 << __idx) ? "YES" : "NO") : "NA"
5046
5047static void
5048lpfc_update_trunk_link_status(struct lpfc_hba *phba,
5049 struct lpfc_acqe_fc_la *acqe_fc)
5050{
5051 uint8_t port_fault = bf_get(lpfc_acqe_fc_la_trunk_linkmask, acqe_fc);
5052 uint8_t err = bf_get(lpfc_acqe_fc_la_trunk_fault, acqe_fc);
5053
5054 phba->sli4_hba.link_state.speed =
5055 lpfc_sli4_port_speed_parse(phba, LPFC_TRAILER_CODE_FC,
5056 bf_get(lpfc_acqe_fc_la_speed, acqe_fc));
5057
5058 phba->sli4_hba.link_state.logical_speed =
James Smartb8e6f132019-05-21 17:49:04 -07005059 bf_get(lpfc_acqe_fc_la_llink_spd, acqe_fc) * 10;
James Smart1dc5ec22018-10-23 13:41:11 -07005060 /* We got FC link speed, convert to fc_linkspeed (READ_TOPOLOGY) */
5061 phba->fc_linkspeed =
5062 lpfc_async_link_speed_to_read_top(
5063 phba,
5064 bf_get(lpfc_acqe_fc_la_speed, acqe_fc));
5065
5066 if (bf_get(lpfc_acqe_fc_la_trunk_config_port0, acqe_fc)) {
5067 phba->trunk_link.link0.state =
5068 bf_get(lpfc_acqe_fc_la_trunk_link_status_port0, acqe_fc)
5069 ? LPFC_LINK_UP : LPFC_LINK_DOWN;
James Smart529b3dd2018-12-13 15:17:54 -08005070 phba->trunk_link.link0.fault = port_fault & 0x1 ? err : 0;
James Smart1dc5ec22018-10-23 13:41:11 -07005071 }
5072 if (bf_get(lpfc_acqe_fc_la_trunk_config_port1, acqe_fc)) {
5073 phba->trunk_link.link1.state =
5074 bf_get(lpfc_acqe_fc_la_trunk_link_status_port1, acqe_fc)
5075 ? LPFC_LINK_UP : LPFC_LINK_DOWN;
James Smart529b3dd2018-12-13 15:17:54 -08005076 phba->trunk_link.link1.fault = port_fault & 0x2 ? err : 0;
James Smart1dc5ec22018-10-23 13:41:11 -07005077 }
5078 if (bf_get(lpfc_acqe_fc_la_trunk_config_port2, acqe_fc)) {
5079 phba->trunk_link.link2.state =
5080 bf_get(lpfc_acqe_fc_la_trunk_link_status_port2, acqe_fc)
5081 ? LPFC_LINK_UP : LPFC_LINK_DOWN;
James Smart529b3dd2018-12-13 15:17:54 -08005082 phba->trunk_link.link2.fault = port_fault & 0x4 ? err : 0;
James Smart1dc5ec22018-10-23 13:41:11 -07005083 }
5084 if (bf_get(lpfc_acqe_fc_la_trunk_config_port3, acqe_fc)) {
5085 phba->trunk_link.link3.state =
5086 bf_get(lpfc_acqe_fc_la_trunk_link_status_port3, acqe_fc)
5087 ? LPFC_LINK_UP : LPFC_LINK_DOWN;
James Smart529b3dd2018-12-13 15:17:54 -08005088 phba->trunk_link.link3.fault = port_fault & 0x8 ? err : 0;
James Smart1dc5ec22018-10-23 13:41:11 -07005089 }
5090
5091 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5092 "2910 Async FC Trunking Event - Speed:%d\n"
5093 "\tLogical speed:%d "
5094 "port0: %s port1: %s port2: %s port3: %s\n",
5095 phba->sli4_hba.link_state.speed,
5096 phba->sli4_hba.link_state.logical_speed,
5097 trunk_link_status(0), trunk_link_status(1),
5098 trunk_link_status(2), trunk_link_status(3));
5099
5100 if (port_fault)
5101 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5102 "3202 trunk error:0x%x (%s) seen on port0:%s "
5103 /*
5104 * SLI-4: We have only 0xA error codes
5105 * defined as of now. print an appropriate
5106 * message in case driver needs to be updated.
5107 */
5108 "port1:%s port2:%s port3:%s\n", err, err > 0xA ?
5109 "UNDEFINED. update driver." : trunk_errmsg[err],
5110 trunk_port_fault(0), trunk_port_fault(1),
5111 trunk_port_fault(2), trunk_port_fault(3));
5112}
5113
5114
5115/**
James Smart70f3c072010-12-15 17:57:33 -05005116 * lpfc_sli4_async_fc_evt - Process the asynchronous FC link event
5117 * @phba: pointer to lpfc hba data structure.
5118 * @acqe_fc: pointer to the async fc completion queue entry.
5119 *
5120 * This routine is to handle the SLI4 asynchronous FC event. It will simply log
5121 * that the event was received and then issue a read_topology mailbox command so
5122 * that the rest of the driver will treat it the same as SLI3.
5123 **/
5124static void
5125lpfc_sli4_async_fc_evt(struct lpfc_hba *phba, struct lpfc_acqe_fc_la *acqe_fc)
5126{
5127 struct lpfc_dmabuf *mp;
5128 LPFC_MBOXQ_t *pmb;
James Smart7bdedb32016-07-06 12:36:00 -07005129 MAILBOX_t *mb;
5130 struct lpfc_mbx_read_top *la;
James Smart70f3c072010-12-15 17:57:33 -05005131 int rc;
5132
5133 if (bf_get(lpfc_trailer_type, acqe_fc) !=
5134 LPFC_FC_LA_EVENT_TYPE_FC_LINK) {
5135 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5136 "2895 Non FC link Event detected.(%d)\n",
5137 bf_get(lpfc_trailer_type, acqe_fc));
5138 return;
5139 }
James Smart1dc5ec22018-10-23 13:41:11 -07005140
5141 if (bf_get(lpfc_acqe_fc_la_att_type, acqe_fc) ==
5142 LPFC_FC_LA_TYPE_TRUNKING_EVENT) {
5143 lpfc_update_trunk_link_status(phba, acqe_fc);
5144 return;
5145 }
5146
James Smart70f3c072010-12-15 17:57:33 -05005147 /* Keep the link status for extra SLI4 state machine reference */
5148 phba->sli4_hba.link_state.speed =
James Smart8b68cd52012-09-29 11:32:37 -04005149 lpfc_sli4_port_speed_parse(phba, LPFC_TRAILER_CODE_FC,
5150 bf_get(lpfc_acqe_fc_la_speed, acqe_fc));
James Smart70f3c072010-12-15 17:57:33 -05005151 phba->sli4_hba.link_state.duplex = LPFC_ASYNC_LINK_DUPLEX_FULL;
5152 phba->sli4_hba.link_state.topology =
5153 bf_get(lpfc_acqe_fc_la_topology, acqe_fc);
5154 phba->sli4_hba.link_state.status =
5155 bf_get(lpfc_acqe_fc_la_att_type, acqe_fc);
5156 phba->sli4_hba.link_state.type =
5157 bf_get(lpfc_acqe_fc_la_port_type, acqe_fc);
5158 phba->sli4_hba.link_state.number =
5159 bf_get(lpfc_acqe_fc_la_port_number, acqe_fc);
5160 phba->sli4_hba.link_state.fault =
5161 bf_get(lpfc_acqe_link_fault, acqe_fc);
James Smartb8e6f132019-05-21 17:49:04 -07005162
5163 if (bf_get(lpfc_acqe_fc_la_att_type, acqe_fc) ==
5164 LPFC_FC_LA_TYPE_LINK_DOWN)
5165 phba->sli4_hba.link_state.logical_speed = 0;
5166 else if (!phba->sli4_hba.conf_trunk)
5167 phba->sli4_hba.link_state.logical_speed =
James Smart8b68cd52012-09-29 11:32:37 -04005168 bf_get(lpfc_acqe_fc_la_llink_spd, acqe_fc) * 10;
James Smartb8e6f132019-05-21 17:49:04 -07005169
James Smart70f3c072010-12-15 17:57:33 -05005170 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5171 "2896 Async FC event - Speed:%dGBaud Topology:x%x "
5172 "LA Type:x%x Port Type:%d Port Number:%d Logical speed:"
5173 "%dMbps Fault:%d\n",
5174 phba->sli4_hba.link_state.speed,
5175 phba->sli4_hba.link_state.topology,
5176 phba->sli4_hba.link_state.status,
5177 phba->sli4_hba.link_state.type,
5178 phba->sli4_hba.link_state.number,
James Smart8b68cd52012-09-29 11:32:37 -04005179 phba->sli4_hba.link_state.logical_speed,
James Smart70f3c072010-12-15 17:57:33 -05005180 phba->sli4_hba.link_state.fault);
5181 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5182 if (!pmb) {
5183 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5184 "2897 The mboxq allocation failed\n");
5185 return;
5186 }
5187 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5188 if (!mp) {
5189 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5190 "2898 The lpfc_dmabuf allocation failed\n");
5191 goto out_free_pmb;
5192 }
5193 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
5194 if (!mp->virt) {
5195 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5196 "2899 The mbuf allocation failed\n");
5197 goto out_free_dmabuf;
5198 }
5199
5200 /* Cleanup any outstanding ELS commands */
5201 lpfc_els_flush_all_cmd(phba);
5202
5203 /* Block ELS IOCBs until we have done process link event */
James Smart895427b2017-02-12 13:52:30 -08005204 phba->sli4_hba.els_wq->pring->flag |= LPFC_STOP_IOCB_EVENT;
James Smart70f3c072010-12-15 17:57:33 -05005205
5206 /* Update link event statistics */
5207 phba->sli.slistat.link_event++;
5208
5209 /* Create lpfc_handle_latt mailbox command from link ACQE */
5210 lpfc_read_topology(phba, pmb, mp);
5211 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
5212 pmb->vport = phba->pport;
5213
James Smart7bdedb32016-07-06 12:36:00 -07005214 if (phba->sli4_hba.link_state.status != LPFC_FC_LA_TYPE_LINK_UP) {
James Smartae9e28f2017-05-15 15:20:51 -07005215 phba->link_flag &= ~(LS_MDS_LINK_DOWN | LS_MDS_LOOPBACK);
5216
5217 switch (phba->sli4_hba.link_state.status) {
5218 case LPFC_FC_LA_TYPE_MDS_LINK_DOWN:
5219 phba->link_flag |= LS_MDS_LINK_DOWN;
5220 break;
5221 case LPFC_FC_LA_TYPE_MDS_LOOPBACK:
5222 phba->link_flag |= LS_MDS_LOOPBACK;
5223 break;
5224 default:
5225 break;
5226 }
5227
James Smart23288b72018-05-04 20:37:53 -07005228 /* Initialize completion status */
James Smart7bdedb32016-07-06 12:36:00 -07005229 mb = &pmb->u.mb;
James Smart23288b72018-05-04 20:37:53 -07005230 mb->mbxStatus = MBX_SUCCESS;
5231
5232 /* Parse port fault information field */
5233 lpfc_sli4_parse_latt_fault(phba, (void *)acqe_fc);
James Smart7bdedb32016-07-06 12:36:00 -07005234
5235 /* Parse and translate link attention fields */
5236 la = (struct lpfc_mbx_read_top *)&pmb->u.mb.un.varReadTop;
5237 la->eventTag = acqe_fc->event_tag;
James Smart7bdedb32016-07-06 12:36:00 -07005238
James Smartaeb3c812017-04-21 16:05:02 -07005239 if (phba->sli4_hba.link_state.status ==
5240 LPFC_FC_LA_TYPE_UNEXP_WWPN) {
5241 bf_set(lpfc_mbx_read_top_att_type, la,
5242 LPFC_FC_LA_TYPE_UNEXP_WWPN);
5243 } else {
5244 bf_set(lpfc_mbx_read_top_att_type, la,
5245 LPFC_FC_LA_TYPE_LINK_DOWN);
5246 }
James Smart7bdedb32016-07-06 12:36:00 -07005247 /* Invoke the mailbox command callback function */
5248 lpfc_mbx_cmpl_read_topology(phba, pmb);
5249
5250 return;
5251 }
5252
James Smart70f3c072010-12-15 17:57:33 -05005253 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
5254 if (rc == MBX_NOT_FINISHED)
5255 goto out_free_dmabuf;
5256 return;
5257
5258out_free_dmabuf:
5259 kfree(mp);
5260out_free_pmb:
5261 mempool_free(pmb, phba->mbox_mem_pool);
5262}
5263
5264/**
5265 * lpfc_sli4_async_sli_evt - Process the asynchronous SLI link event
5266 * @phba: pointer to lpfc hba data structure.
5267 * @acqe_fc: pointer to the async SLI completion queue entry.
5268 *
5269 * This routine is to handle the SLI4 asynchronous SLI events.
5270 **/
5271static void
5272lpfc_sli4_async_sli_evt(struct lpfc_hba *phba, struct lpfc_acqe_sli *acqe_sli)
5273{
James Smart4b8bae02012-06-12 13:55:07 -04005274 char port_name;
James Smart8c1312e2012-10-31 14:45:09 -04005275 char message[128];
James Smart4b8bae02012-06-12 13:55:07 -04005276 uint8_t status;
James Smart946727d2015-04-07 15:07:09 -04005277 uint8_t evt_type;
James Smart448193b2015-12-16 18:12:05 -05005278 uint8_t operational = 0;
James Smart946727d2015-04-07 15:07:09 -04005279 struct temp_event temp_event_data;
James Smart4b8bae02012-06-12 13:55:07 -04005280 struct lpfc_acqe_misconfigured_event *misconfigured;
James Smart946727d2015-04-07 15:07:09 -04005281 struct Scsi_Host *shost;
James Smartcd713482018-10-23 13:41:01 -07005282 struct lpfc_vport **vports;
5283 int rc, i;
James Smart4b8bae02012-06-12 13:55:07 -04005284
James Smart946727d2015-04-07 15:07:09 -04005285 evt_type = bf_get(lpfc_trailer_type, acqe_sli);
5286
James Smart448193b2015-12-16 18:12:05 -05005287 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5288 "2901 Async SLI event - Event Data1:x%08x Event Data2:"
5289 "x%08x SLI Event Type:%d\n",
5290 acqe_sli->event_data1, acqe_sli->event_data2,
5291 evt_type);
James Smart4b8bae02012-06-12 13:55:07 -04005292
5293 port_name = phba->Port[0];
5294 if (port_name == 0x00)
5295 port_name = '?'; /* get port name is empty */
5296
James Smart946727d2015-04-07 15:07:09 -04005297 switch (evt_type) {
5298 case LPFC_SLI_EVENT_TYPE_OVER_TEMP:
5299 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
5300 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
5301 temp_event_data.data = (uint32_t)acqe_sli->event_data1;
5302
5303 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5304 "3190 Over Temperature:%d Celsius- Port Name %c\n",
5305 acqe_sli->event_data1, port_name);
5306
James Smart310429e2016-07-06 12:35:54 -07005307 phba->sfp_warning |= LPFC_TRANSGRESSION_HIGH_TEMPERATURE;
James Smart946727d2015-04-07 15:07:09 -04005308 shost = lpfc_shost_from_vport(phba->pport);
5309 fc_host_post_vendor_event(shost, fc_get_event_number(),
5310 sizeof(temp_event_data),
5311 (char *)&temp_event_data,
5312 SCSI_NL_VID_TYPE_PCI
5313 | PCI_VENDOR_ID_EMULEX);
5314 break;
5315 case LPFC_SLI_EVENT_TYPE_NORM_TEMP:
5316 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
5317 temp_event_data.event_code = LPFC_NORMAL_TEMP;
5318 temp_event_data.data = (uint32_t)acqe_sli->event_data1;
5319
5320 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5321 "3191 Normal Temperature:%d Celsius - Port Name %c\n",
5322 acqe_sli->event_data1, port_name);
5323
5324 shost = lpfc_shost_from_vport(phba->pport);
5325 fc_host_post_vendor_event(shost, fc_get_event_number(),
5326 sizeof(temp_event_data),
5327 (char *)&temp_event_data,
5328 SCSI_NL_VID_TYPE_PCI
5329 | PCI_VENDOR_ID_EMULEX);
5330 break;
5331 case LPFC_SLI_EVENT_TYPE_MISCONFIGURED:
5332 misconfigured = (struct lpfc_acqe_misconfigured_event *)
James Smart4b8bae02012-06-12 13:55:07 -04005333 &acqe_sli->event_data1;
5334
James Smart946727d2015-04-07 15:07:09 -04005335 /* fetch the status for this port */
5336 switch (phba->sli4_hba.lnk_info.lnk_no) {
5337 case LPFC_LINK_NUMBER_0:
James Smart448193b2015-12-16 18:12:05 -05005338 status = bf_get(lpfc_sli_misconfigured_port0_state,
5339 &misconfigured->theEvent);
5340 operational = bf_get(lpfc_sli_misconfigured_port0_op,
James Smart4b8bae02012-06-12 13:55:07 -04005341 &misconfigured->theEvent);
James Smart946727d2015-04-07 15:07:09 -04005342 break;
5343 case LPFC_LINK_NUMBER_1:
James Smart448193b2015-12-16 18:12:05 -05005344 status = bf_get(lpfc_sli_misconfigured_port1_state,
5345 &misconfigured->theEvent);
5346 operational = bf_get(lpfc_sli_misconfigured_port1_op,
James Smart4b8bae02012-06-12 13:55:07 -04005347 &misconfigured->theEvent);
James Smart946727d2015-04-07 15:07:09 -04005348 break;
5349 case LPFC_LINK_NUMBER_2:
James Smart448193b2015-12-16 18:12:05 -05005350 status = bf_get(lpfc_sli_misconfigured_port2_state,
5351 &misconfigured->theEvent);
5352 operational = bf_get(lpfc_sli_misconfigured_port2_op,
James Smart4b8bae02012-06-12 13:55:07 -04005353 &misconfigured->theEvent);
James Smart946727d2015-04-07 15:07:09 -04005354 break;
5355 case LPFC_LINK_NUMBER_3:
James Smart448193b2015-12-16 18:12:05 -05005356 status = bf_get(lpfc_sli_misconfigured_port3_state,
5357 &misconfigured->theEvent);
5358 operational = bf_get(lpfc_sli_misconfigured_port3_op,
James Smart4b8bae02012-06-12 13:55:07 -04005359 &misconfigured->theEvent);
James Smart946727d2015-04-07 15:07:09 -04005360 break;
5361 default:
James Smart448193b2015-12-16 18:12:05 -05005362 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5363 "3296 "
5364 "LPFC_SLI_EVENT_TYPE_MISCONFIGURED "
5365 "event: Invalid link %d",
5366 phba->sli4_hba.lnk_info.lnk_no);
5367 return;
James Smart946727d2015-04-07 15:07:09 -04005368 }
James Smart4b8bae02012-06-12 13:55:07 -04005369
James Smart448193b2015-12-16 18:12:05 -05005370 /* Skip if optic state unchanged */
5371 if (phba->sli4_hba.lnk_info.optic_state == status)
5372 return;
5373
James Smart946727d2015-04-07 15:07:09 -04005374 switch (status) {
5375 case LPFC_SLI_EVENT_STATUS_VALID:
James Smart448193b2015-12-16 18:12:05 -05005376 sprintf(message, "Physical Link is functional");
5377 break;
James Smart946727d2015-04-07 15:07:09 -04005378 case LPFC_SLI_EVENT_STATUS_NOT_PRESENT:
5379 sprintf(message, "Optics faulted/incorrectly "
5380 "installed/not installed - Reseat optics, "
5381 "if issue not resolved, replace.");
5382 break;
5383 case LPFC_SLI_EVENT_STATUS_WRONG_TYPE:
5384 sprintf(message,
5385 "Optics of two types installed - Remove one "
5386 "optic or install matching pair of optics.");
5387 break;
5388 case LPFC_SLI_EVENT_STATUS_UNSUPPORTED:
5389 sprintf(message, "Incompatible optics - Replace with "
James Smart292098b2012-09-29 11:31:41 -04005390 "compatible optics for card to function.");
James Smart946727d2015-04-07 15:07:09 -04005391 break;
James Smart448193b2015-12-16 18:12:05 -05005392 case LPFC_SLI_EVENT_STATUS_UNQUALIFIED:
5393 sprintf(message, "Unqualified optics - Replace with "
5394 "Avago optics for Warranty and Technical "
5395 "Support - Link is%s operational",
James Smart2ea259e2017-02-12 13:52:27 -08005396 (operational) ? " not" : "");
James Smart448193b2015-12-16 18:12:05 -05005397 break;
5398 case LPFC_SLI_EVENT_STATUS_UNCERTIFIED:
5399 sprintf(message, "Uncertified optics - Replace with "
5400 "Avago-certified optics to enable link "
5401 "operation - Link is%s operational",
James Smart2ea259e2017-02-12 13:52:27 -08005402 (operational) ? " not" : "");
James Smart448193b2015-12-16 18:12:05 -05005403 break;
James Smart946727d2015-04-07 15:07:09 -04005404 default:
5405 /* firmware is reporting a status we don't know about */
5406 sprintf(message, "Unknown event status x%02x", status);
5407 break;
5408 }
James Smartcd713482018-10-23 13:41:01 -07005409
5410 /* Issue READ_CONFIG mbox command to refresh supported speeds */
5411 rc = lpfc_sli4_read_config(phba);
James Smart3952e912018-10-23 13:41:02 -07005412 if (rc) {
James Smartcd713482018-10-23 13:41:01 -07005413 phba->lmt = 0;
5414 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5415 "3194 Unable to retrieve supported "
James Smart3952e912018-10-23 13:41:02 -07005416 "speeds, rc = 0x%x\n", rc);
James Smartcd713482018-10-23 13:41:01 -07005417 }
5418 vports = lpfc_create_vport_work_array(phba);
5419 if (vports != NULL) {
5420 for (i = 0; i <= phba->max_vports && vports[i] != NULL;
5421 i++) {
5422 shost = lpfc_shost_from_vport(vports[i]);
5423 lpfc_host_supported_speeds_set(shost);
5424 }
5425 }
5426 lpfc_destroy_vport_work_array(phba, vports);
5427
James Smart448193b2015-12-16 18:12:05 -05005428 phba->sli4_hba.lnk_info.optic_state = status;
James Smart946727d2015-04-07 15:07:09 -04005429 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart448193b2015-12-16 18:12:05 -05005430 "3176 Port Name %c %s\n", port_name, message);
James Smart946727d2015-04-07 15:07:09 -04005431 break;
5432 case LPFC_SLI_EVENT_TYPE_REMOTE_DPORT:
5433 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5434 "3192 Remote DPort Test Initiated - "
5435 "Event Data1:x%08x Event Data2: x%08x\n",
5436 acqe_sli->event_data1, acqe_sli->event_data2);
James Smart4b8bae02012-06-12 13:55:07 -04005437 break;
5438 default:
James Smart946727d2015-04-07 15:07:09 -04005439 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5440 "3193 Async SLI event - Event Data1:x%08x Event Data2:"
5441 "x%08x SLI Event Type:%d\n",
5442 acqe_sli->event_data1, acqe_sli->event_data2,
5443 evt_type);
James Smart4b8bae02012-06-12 13:55:07 -04005444 break;
5445 }
James Smart70f3c072010-12-15 17:57:33 -05005446}
5447
5448/**
James Smartfc2b9892010-02-26 14:15:29 -05005449 * lpfc_sli4_perform_vport_cvl - Perform clear virtual link on a vport
5450 * @vport: pointer to vport data structure.
5451 *
5452 * This routine is to perform Clear Virtual Link (CVL) on a vport in
5453 * response to a CVL event.
5454 *
5455 * Return the pointer to the ndlp with the vport if successful, otherwise
5456 * return NULL.
5457 **/
5458static struct lpfc_nodelist *
5459lpfc_sli4_perform_vport_cvl(struct lpfc_vport *vport)
5460{
5461 struct lpfc_nodelist *ndlp;
5462 struct Scsi_Host *shost;
5463 struct lpfc_hba *phba;
5464
5465 if (!vport)
5466 return NULL;
James Smartfc2b9892010-02-26 14:15:29 -05005467 phba = vport->phba;
5468 if (!phba)
5469 return NULL;
James Smart78730cf2010-04-06 15:06:30 -04005470 ndlp = lpfc_findnode_did(vport, Fabric_DID);
5471 if (!ndlp) {
5472 /* Cannot find existing Fabric ndlp, so allocate a new one */
James Smart9d3d3402017-04-21 16:05:00 -07005473 ndlp = lpfc_nlp_init(vport, Fabric_DID);
James Smart78730cf2010-04-06 15:06:30 -04005474 if (!ndlp)
5475 return 0;
James Smart78730cf2010-04-06 15:06:30 -04005476 /* Set the node type */
5477 ndlp->nlp_type |= NLP_FABRIC;
5478 /* Put ndlp onto node list */
5479 lpfc_enqueue_node(vport, ndlp);
5480 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
5481 /* re-setup ndlp without removing from node list */
5482 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
5483 if (!ndlp)
5484 return 0;
5485 }
James Smart63e801c2010-11-20 23:14:19 -05005486 if ((phba->pport->port_state < LPFC_FLOGI) &&
5487 (phba->pport->port_state != LPFC_VPORT_FAILED))
James Smartfc2b9892010-02-26 14:15:29 -05005488 return NULL;
5489 /* If virtual link is not yet instantiated ignore CVL */
James Smart63e801c2010-11-20 23:14:19 -05005490 if ((vport != phba->pport) && (vport->port_state < LPFC_FDISC)
5491 && (vport->port_state != LPFC_VPORT_FAILED))
James Smartfc2b9892010-02-26 14:15:29 -05005492 return NULL;
5493 shost = lpfc_shost_from_vport(vport);
5494 if (!shost)
5495 return NULL;
5496 lpfc_linkdown_port(vport);
5497 lpfc_cleanup_pending_mbox(vport);
5498 spin_lock_irq(shost->host_lock);
5499 vport->fc_flag |= FC_VPORT_CVL_RCVD;
5500 spin_unlock_irq(shost->host_lock);
5501
5502 return ndlp;
5503}
5504
5505/**
5506 * lpfc_sli4_perform_all_vport_cvl - Perform clear virtual link on all vports
5507 * @vport: pointer to lpfc hba data structure.
5508 *
5509 * This routine is to perform Clear Virtual Link (CVL) on all vports in
5510 * response to a FCF dead event.
5511 **/
5512static void
5513lpfc_sli4_perform_all_vport_cvl(struct lpfc_hba *phba)
5514{
5515 struct lpfc_vport **vports;
5516 int i;
5517
5518 vports = lpfc_create_vport_work_array(phba);
5519 if (vports)
5520 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
5521 lpfc_sli4_perform_vport_cvl(vports[i]);
5522 lpfc_destroy_vport_work_array(phba, vports);
5523}
5524
5525/**
James Smart76a95d72010-11-20 23:11:48 -05005526 * lpfc_sli4_async_fip_evt - Process the asynchronous FCoE FIP event
James Smartda0436e2009-05-22 14:51:39 -04005527 * @phba: pointer to lpfc hba data structure.
5528 * @acqe_link: pointer to the async fcoe completion queue entry.
5529 *
5530 * This routine is to handle the SLI4 asynchronous fcoe event.
5531 **/
5532static void
James Smart76a95d72010-11-20 23:11:48 -05005533lpfc_sli4_async_fip_evt(struct lpfc_hba *phba,
James Smart70f3c072010-12-15 17:57:33 -05005534 struct lpfc_acqe_fip *acqe_fip)
James Smartda0436e2009-05-22 14:51:39 -04005535{
James Smart70f3c072010-12-15 17:57:33 -05005536 uint8_t event_type = bf_get(lpfc_trailer_type, acqe_fip);
James Smartda0436e2009-05-22 14:51:39 -04005537 int rc;
James Smart6669f9b2009-10-02 15:16:45 -04005538 struct lpfc_vport *vport;
5539 struct lpfc_nodelist *ndlp;
5540 struct Scsi_Host *shost;
James Smart695a8142010-01-26 23:08:03 -05005541 int active_vlink_present;
5542 struct lpfc_vport **vports;
5543 int i;
James Smartda0436e2009-05-22 14:51:39 -04005544
James Smart70f3c072010-12-15 17:57:33 -05005545 phba->fc_eventTag = acqe_fip->event_tag;
5546 phba->fcoe_eventtag = acqe_fip->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04005547 switch (event_type) {
James Smart70f3c072010-12-15 17:57:33 -05005548 case LPFC_FIP_EVENT_TYPE_NEW_FCF:
5549 case LPFC_FIP_EVENT_TYPE_FCF_PARAM_MOD:
5550 if (event_type == LPFC_FIP_EVENT_TYPE_NEW_FCF)
James Smart999d8132010-03-15 11:24:56 -04005551 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
5552 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04005553 "2546 New FCF event, evt_tag:x%x, "
5554 "index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05005555 acqe_fip->event_tag,
5556 acqe_fip->index);
James Smart999d8132010-03-15 11:24:56 -04005557 else
5558 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP |
5559 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04005560 "2788 FCF param modified event, "
5561 "evt_tag:x%x, index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05005562 acqe_fip->event_tag,
5563 acqe_fip->index);
James Smart38b92ef2010-08-04 16:11:39 -04005564 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smart0c9ab6f2010-02-26 14:15:57 -05005565 /*
5566 * During period of FCF discovery, read the FCF
5567 * table record indexed by the event to update
James Smarta93ff372010-10-22 11:06:08 -04005568 * FCF roundrobin failover eligible FCF bmask.
James Smart0c9ab6f2010-02-26 14:15:57 -05005569 */
5570 lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
5571 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04005572 "2779 Read FCF (x%x) for updating "
5573 "roundrobin FCF failover bmask\n",
James Smart70f3c072010-12-15 17:57:33 -05005574 acqe_fip->index);
5575 rc = lpfc_sli4_read_fcf_rec(phba, acqe_fip->index);
James Smart0c9ab6f2010-02-26 14:15:57 -05005576 }
James Smart38b92ef2010-08-04 16:11:39 -04005577
5578 /* If the FCF discovery is in progress, do nothing. */
James Smart3804dc82010-07-14 15:31:37 -04005579 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04005580 if (phba->hba_flag & FCF_TS_INPROG) {
James Smart38b92ef2010-08-04 16:11:39 -04005581 spin_unlock_irq(&phba->hbalock);
5582 break;
5583 }
5584 /* If fast FCF failover rescan event is pending, do nothing */
James Smart036cad12018-10-23 13:41:06 -07005585 if (phba->fcf.fcf_flag & (FCF_REDISC_EVT | FCF_REDISC_PEND)) {
James Smart38b92ef2010-08-04 16:11:39 -04005586 spin_unlock_irq(&phba->hbalock);
5587 break;
5588 }
5589
James Smartc2b97122013-05-31 17:05:36 -04005590 /* If the FCF has been in discovered state, do nothing. */
5591 if (phba->fcf.fcf_flag & FCF_SCAN_DONE) {
James Smart3804dc82010-07-14 15:31:37 -04005592 spin_unlock_irq(&phba->hbalock);
5593 break;
5594 }
5595 spin_unlock_irq(&phba->hbalock);
James Smart38b92ef2010-08-04 16:11:39 -04005596
James Smart0c9ab6f2010-02-26 14:15:57 -05005597 /* Otherwise, scan the entire FCF table and re-discover SAN */
5598 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04005599 "2770 Start FCF table scan per async FCF "
5600 "event, evt_tag:x%x, index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05005601 acqe_fip->event_tag, acqe_fip->index);
James Smart0c9ab6f2010-02-26 14:15:57 -05005602 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba,
5603 LPFC_FCOE_FCF_GET_FIRST);
James Smartda0436e2009-05-22 14:51:39 -04005604 if (rc)
James Smart0c9ab6f2010-02-26 14:15:57 -05005605 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
5606 "2547 Issue FCF scan read FCF mailbox "
James Smarta93ff372010-10-22 11:06:08 -04005607 "command failed (x%x)\n", rc);
James Smartda0436e2009-05-22 14:51:39 -04005608 break;
5609
James Smart70f3c072010-12-15 17:57:33 -05005610 case LPFC_FIP_EVENT_TYPE_FCF_TABLE_FULL:
James Smartda0436e2009-05-22 14:51:39 -04005611 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte4e74272009-07-19 10:01:38 -04005612 "2548 FCF Table full count 0x%x tag 0x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05005613 bf_get(lpfc_acqe_fip_fcf_count, acqe_fip),
5614 acqe_fip->event_tag);
James Smartda0436e2009-05-22 14:51:39 -04005615 break;
5616
James Smart70f3c072010-12-15 17:57:33 -05005617 case LPFC_FIP_EVENT_TYPE_FCF_DEAD:
James Smart80c17842012-03-01 22:35:45 -05005618 phba->fcoe_cvl_eventtag = acqe_fip->event_tag;
James Smart0c9ab6f2010-02-26 14:15:57 -05005619 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04005620 "2549 FCF (x%x) disconnected from network, "
James Smart70f3c072010-12-15 17:57:33 -05005621 "tag:x%x\n", acqe_fip->index, acqe_fip->event_tag);
James Smart38b92ef2010-08-04 16:11:39 -04005622 /*
5623 * If we are in the middle of FCF failover process, clear
5624 * the corresponding FCF bit in the roundrobin bitmap.
James Smartda0436e2009-05-22 14:51:39 -04005625 */
James Smartfc2b9892010-02-26 14:15:29 -05005626 spin_lock_irq(&phba->hbalock);
James Smarta1cadfe2016-07-06 12:36:02 -07005627 if ((phba->fcf.fcf_flag & FCF_DISCOVERY) &&
5628 (phba->fcf.current_rec.fcf_indx != acqe_fip->index)) {
James Smartfc2b9892010-02-26 14:15:29 -05005629 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05005630 /* Update FLOGI FCF failover eligible FCF bmask */
James Smart70f3c072010-12-15 17:57:33 -05005631 lpfc_sli4_fcf_rr_index_clear(phba, acqe_fip->index);
James Smartfc2b9892010-02-26 14:15:29 -05005632 break;
5633 }
James Smart38b92ef2010-08-04 16:11:39 -04005634 spin_unlock_irq(&phba->hbalock);
5635
5636 /* If the event is not for currently used fcf do nothing */
James Smart70f3c072010-12-15 17:57:33 -05005637 if (phba->fcf.current_rec.fcf_indx != acqe_fip->index)
James Smart38b92ef2010-08-04 16:11:39 -04005638 break;
5639
5640 /*
5641 * Otherwise, request the port to rediscover the entire FCF
5642 * table for a fast recovery from case that the current FCF
5643 * is no longer valid as we are not in the middle of FCF
5644 * failover process already.
5645 */
James Smartc2b97122013-05-31 17:05:36 -04005646 spin_lock_irq(&phba->hbalock);
5647 /* Mark the fast failover process in progress */
5648 phba->fcf.fcf_flag |= FCF_DEAD_DISC;
5649 spin_unlock_irq(&phba->hbalock);
5650
5651 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
5652 "2771 Start FCF fast failover process due to "
5653 "FCF DEAD event: evt_tag:x%x, fcf_index:x%x "
5654 "\n", acqe_fip->event_tag, acqe_fip->index);
5655 rc = lpfc_sli4_redisc_fcf_table(phba);
5656 if (rc) {
5657 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
5658 LOG_DISCOVERY,
Colin Ian King7afc0ce2018-05-03 10:26:12 +01005659 "2772 Issue FCF rediscover mailbox "
James Smartc2b97122013-05-31 17:05:36 -04005660 "command failed, fail through to FCF "
5661 "dead event\n");
5662 spin_lock_irq(&phba->hbalock);
5663 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
5664 spin_unlock_irq(&phba->hbalock);
5665 /*
5666 * Last resort will fail over by treating this
5667 * as a link down to FCF registration.
5668 */
5669 lpfc_sli4_fcf_dead_failthrough(phba);
5670 } else {
5671 /* Reset FCF roundrobin bmask for new discovery */
5672 lpfc_sli4_clear_fcf_rr_bmask(phba);
5673 /*
5674 * Handling fast FCF failover to a DEAD FCF event is
5675 * considered equalivant to receiving CVL to all vports.
5676 */
5677 lpfc_sli4_perform_all_vport_cvl(phba);
5678 }
James Smartda0436e2009-05-22 14:51:39 -04005679 break;
James Smart70f3c072010-12-15 17:57:33 -05005680 case LPFC_FIP_EVENT_TYPE_CVL:
James Smart80c17842012-03-01 22:35:45 -05005681 phba->fcoe_cvl_eventtag = acqe_fip->event_tag;
James Smart0c9ab6f2010-02-26 14:15:57 -05005682 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
James Smart6669f9b2009-10-02 15:16:45 -04005683 "2718 Clear Virtual Link Received for VPI 0x%x"
James Smart70f3c072010-12-15 17:57:33 -05005684 " tag 0x%x\n", acqe_fip->index, acqe_fip->event_tag);
James Smart6d368e52011-05-24 11:44:12 -04005685
James Smart6669f9b2009-10-02 15:16:45 -04005686 vport = lpfc_find_vport_by_vpid(phba,
James Smart5248a742011-07-22 18:37:06 -04005687 acqe_fip->index);
James Smartfc2b9892010-02-26 14:15:29 -05005688 ndlp = lpfc_sli4_perform_vport_cvl(vport);
James Smart6669f9b2009-10-02 15:16:45 -04005689 if (!ndlp)
5690 break;
James Smart695a8142010-01-26 23:08:03 -05005691 active_vlink_present = 0;
5692
5693 vports = lpfc_create_vport_work_array(phba);
5694 if (vports) {
5695 for (i = 0; i <= phba->max_vports && vports[i] != NULL;
5696 i++) {
5697 if ((!(vports[i]->fc_flag &
5698 FC_VPORT_CVL_RCVD)) &&
5699 (vports[i]->port_state > LPFC_FDISC)) {
5700 active_vlink_present = 1;
5701 break;
5702 }
5703 }
5704 lpfc_destroy_vport_work_array(phba, vports);
5705 }
5706
James Smartcc823552015-05-21 13:55:26 -04005707 /*
5708 * Don't re-instantiate if vport is marked for deletion.
5709 * If we are here first then vport_delete is going to wait
5710 * for discovery to complete.
5711 */
5712 if (!(vport->load_flag & FC_UNLOADING) &&
5713 active_vlink_present) {
James Smart695a8142010-01-26 23:08:03 -05005714 /*
5715 * If there are other active VLinks present,
5716 * re-instantiate the Vlink using FDISC.
5717 */
James Smart256ec0d2013-04-17 20:14:58 -04005718 mod_timer(&ndlp->nlp_delayfunc,
5719 jiffies + msecs_to_jiffies(1000));
James Smartfc2b9892010-02-26 14:15:29 -05005720 shost = lpfc_shost_from_vport(vport);
James Smart6669f9b2009-10-02 15:16:45 -04005721 spin_lock_irq(shost->host_lock);
5722 ndlp->nlp_flag |= NLP_DELAY_TMO;
5723 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05005724 ndlp->nlp_last_elscmd = ELS_CMD_FDISC;
5725 vport->port_state = LPFC_FDISC;
5726 } else {
James Smartecfd03c2010-02-12 14:41:27 -05005727 /*
5728 * Otherwise, we request port to rediscover
5729 * the entire FCF table for a fast recovery
5730 * from possible case that the current FCF
James Smart0c9ab6f2010-02-26 14:15:57 -05005731 * is no longer valid if we are not already
5732 * in the FCF failover process.
James Smartecfd03c2010-02-12 14:41:27 -05005733 */
James Smartfc2b9892010-02-26 14:15:29 -05005734 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05005735 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smartfc2b9892010-02-26 14:15:29 -05005736 spin_unlock_irq(&phba->hbalock);
5737 break;
5738 }
5739 /* Mark the fast failover process in progress */
James Smart0c9ab6f2010-02-26 14:15:57 -05005740 phba->fcf.fcf_flag |= FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05005741 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05005742 lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
5743 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04005744 "2773 Start FCF failover per CVL, "
James Smart70f3c072010-12-15 17:57:33 -05005745 "evt_tag:x%x\n", acqe_fip->event_tag);
James Smartecfd03c2010-02-12 14:41:27 -05005746 rc = lpfc_sli4_redisc_fcf_table(phba);
James Smartfc2b9892010-02-26 14:15:29 -05005747 if (rc) {
James Smart0c9ab6f2010-02-26 14:15:57 -05005748 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
5749 LOG_DISCOVERY,
5750 "2774 Issue FCF rediscover "
Colin Ian King7afc0ce2018-05-03 10:26:12 +01005751 "mailbox command failed, "
James Smart0c9ab6f2010-02-26 14:15:57 -05005752 "through to CVL event\n");
James Smartfc2b9892010-02-26 14:15:29 -05005753 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05005754 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05005755 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05005756 /*
5757 * Last resort will be re-try on the
5758 * the current registered FCF entry.
5759 */
5760 lpfc_retry_pport_discovery(phba);
James Smart38b92ef2010-08-04 16:11:39 -04005761 } else
5762 /*
5763 * Reset FCF roundrobin bmask for new
5764 * discovery.
5765 */
James Smart7d791df2011-07-22 18:37:52 -04005766 lpfc_sli4_clear_fcf_rr_bmask(phba);
James Smart6669f9b2009-10-02 15:16:45 -04005767 }
5768 break;
James Smartda0436e2009-05-22 14:51:39 -04005769 default:
5770 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5771 "0288 Unknown FCoE event type 0x%x event tag "
James Smart70f3c072010-12-15 17:57:33 -05005772 "0x%x\n", event_type, acqe_fip->event_tag);
James Smartda0436e2009-05-22 14:51:39 -04005773 break;
5774 }
5775}
5776
5777/**
5778 * lpfc_sli4_async_dcbx_evt - Process the asynchronous dcbx event
5779 * @phba: pointer to lpfc hba data structure.
5780 * @acqe_link: pointer to the async dcbx completion queue entry.
5781 *
5782 * This routine is to handle the SLI4 asynchronous dcbx event.
5783 **/
5784static void
5785lpfc_sli4_async_dcbx_evt(struct lpfc_hba *phba,
5786 struct lpfc_acqe_dcbx *acqe_dcbx)
5787{
James Smart4d9ab992009-10-02 15:16:39 -04005788 phba->fc_eventTag = acqe_dcbx->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04005789 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5790 "0290 The SLI4 DCBX asynchronous event is not "
5791 "handled yet\n");
5792}
5793
5794/**
James Smartb19a0612010-04-06 14:48:51 -04005795 * lpfc_sli4_async_grp5_evt - Process the asynchronous group5 event
5796 * @phba: pointer to lpfc hba data structure.
5797 * @acqe_link: pointer to the async grp5 completion queue entry.
5798 *
5799 * This routine is to handle the SLI4 asynchronous grp5 event. A grp5 event
5800 * is an asynchronous notified of a logical link speed change. The Port
5801 * reports the logical link speed in units of 10Mbps.
5802 **/
5803static void
5804lpfc_sli4_async_grp5_evt(struct lpfc_hba *phba,
5805 struct lpfc_acqe_grp5 *acqe_grp5)
5806{
5807 uint16_t prev_ll_spd;
5808
5809 phba->fc_eventTag = acqe_grp5->event_tag;
5810 phba->fcoe_eventtag = acqe_grp5->event_tag;
5811 prev_ll_spd = phba->sli4_hba.link_state.logical_speed;
5812 phba->sli4_hba.link_state.logical_speed =
James Smart8b68cd52012-09-29 11:32:37 -04005813 (bf_get(lpfc_acqe_grp5_llink_spd, acqe_grp5)) * 10;
James Smartb19a0612010-04-06 14:48:51 -04005814 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5815 "2789 GRP5 Async Event: Updating logical link speed "
James Smart8b68cd52012-09-29 11:32:37 -04005816 "from %dMbps to %dMbps\n", prev_ll_spd,
5817 phba->sli4_hba.link_state.logical_speed);
James Smartb19a0612010-04-06 14:48:51 -04005818}
5819
5820/**
James Smartda0436e2009-05-22 14:51:39 -04005821 * lpfc_sli4_async_event_proc - Process all the pending asynchronous event
5822 * @phba: pointer to lpfc hba data structure.
5823 *
5824 * This routine is invoked by the worker thread to process all the pending
5825 * SLI4 asynchronous events.
5826 **/
5827void lpfc_sli4_async_event_proc(struct lpfc_hba *phba)
5828{
5829 struct lpfc_cq_event *cq_event;
5830
5831 /* First, declare the async event has been handled */
5832 spin_lock_irq(&phba->hbalock);
5833 phba->hba_flag &= ~ASYNC_EVENT;
5834 spin_unlock_irq(&phba->hbalock);
5835 /* Now, handle all the async events */
5836 while (!list_empty(&phba->sli4_hba.sp_asynce_work_queue)) {
5837 /* Get the first event from the head of the event queue */
5838 spin_lock_irq(&phba->hbalock);
5839 list_remove_head(&phba->sli4_hba.sp_asynce_work_queue,
5840 cq_event, struct lpfc_cq_event, list);
5841 spin_unlock_irq(&phba->hbalock);
5842 /* Process the asynchronous event */
5843 switch (bf_get(lpfc_trailer_code, &cq_event->cqe.mcqe_cmpl)) {
5844 case LPFC_TRAILER_CODE_LINK:
5845 lpfc_sli4_async_link_evt(phba,
5846 &cq_event->cqe.acqe_link);
5847 break;
5848 case LPFC_TRAILER_CODE_FCOE:
James Smart70f3c072010-12-15 17:57:33 -05005849 lpfc_sli4_async_fip_evt(phba, &cq_event->cqe.acqe_fip);
James Smartda0436e2009-05-22 14:51:39 -04005850 break;
5851 case LPFC_TRAILER_CODE_DCBX:
5852 lpfc_sli4_async_dcbx_evt(phba,
5853 &cq_event->cqe.acqe_dcbx);
5854 break;
James Smartb19a0612010-04-06 14:48:51 -04005855 case LPFC_TRAILER_CODE_GRP5:
5856 lpfc_sli4_async_grp5_evt(phba,
5857 &cq_event->cqe.acqe_grp5);
5858 break;
James Smart70f3c072010-12-15 17:57:33 -05005859 case LPFC_TRAILER_CODE_FC:
5860 lpfc_sli4_async_fc_evt(phba, &cq_event->cqe.acqe_fc);
5861 break;
5862 case LPFC_TRAILER_CODE_SLI:
5863 lpfc_sli4_async_sli_evt(phba, &cq_event->cqe.acqe_sli);
5864 break;
James Smartda0436e2009-05-22 14:51:39 -04005865 default:
5866 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5867 "1804 Invalid asynchrous event code: "
5868 "x%x\n", bf_get(lpfc_trailer_code,
5869 &cq_event->cqe.mcqe_cmpl));
5870 break;
5871 }
5872 /* Free the completion event processed to the free pool */
5873 lpfc_sli4_cq_event_release(phba, cq_event);
5874 }
5875}
5876
5877/**
James Smartecfd03c2010-02-12 14:41:27 -05005878 * lpfc_sli4_fcf_redisc_event_proc - Process fcf table rediscovery event
5879 * @phba: pointer to lpfc hba data structure.
5880 *
5881 * This routine is invoked by the worker thread to process FCF table
5882 * rediscovery pending completion event.
5883 **/
5884void lpfc_sli4_fcf_redisc_event_proc(struct lpfc_hba *phba)
5885{
5886 int rc;
5887
5888 spin_lock_irq(&phba->hbalock);
5889 /* Clear FCF rediscovery timeout event */
5890 phba->fcf.fcf_flag &= ~FCF_REDISC_EVT;
5891 /* Clear driver fast failover FCF record flag */
5892 phba->fcf.failover_rec.flag = 0;
5893 /* Set state for FCF fast failover */
5894 phba->fcf.fcf_flag |= FCF_REDISC_FOV;
5895 spin_unlock_irq(&phba->hbalock);
5896
5897 /* Scan FCF table from the first entry to re-discover SAN */
James Smart0c9ab6f2010-02-26 14:15:57 -05005898 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04005899 "2777 Start post-quiescent FCF table scan\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05005900 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
James Smartecfd03c2010-02-12 14:41:27 -05005901 if (rc)
James Smart0c9ab6f2010-02-26 14:15:57 -05005902 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
5903 "2747 Issue FCF scan read FCF mailbox "
5904 "command failed 0x%x\n", rc);
James Smartecfd03c2010-02-12 14:41:27 -05005905}
5906
5907/**
James Smartda0436e2009-05-22 14:51:39 -04005908 * lpfc_api_table_setup - Set up per hba pci-device group func api jump table
5909 * @phba: pointer to lpfc hba data structure.
5910 * @dev_grp: The HBA PCI-Device group number.
5911 *
5912 * This routine is invoked to set up the per HBA PCI-Device group function
5913 * API jump table entries.
5914 *
5915 * Return: 0 if success, otherwise -ENODEV
5916 **/
5917int
5918lpfc_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
James Smart5b75da22008-12-04 22:39:35 -05005919{
5920 int rc;
5921
James Smartda0436e2009-05-22 14:51:39 -04005922 /* Set up lpfc PCI-device group */
5923 phba->pci_dev_grp = dev_grp;
James Smart5b75da22008-12-04 22:39:35 -05005924
James Smartda0436e2009-05-22 14:51:39 -04005925 /* The LPFC_PCI_DEV_OC uses SLI4 */
5926 if (dev_grp == LPFC_PCI_DEV_OC)
5927 phba->sli_rev = LPFC_SLI_REV4;
James Smart5b75da22008-12-04 22:39:35 -05005928
James Smartda0436e2009-05-22 14:51:39 -04005929 /* Set up device INIT API function jump table */
5930 rc = lpfc_init_api_table_setup(phba, dev_grp);
5931 if (rc)
5932 return -ENODEV;
5933 /* Set up SCSI API function jump table */
5934 rc = lpfc_scsi_api_table_setup(phba, dev_grp);
5935 if (rc)
5936 return -ENODEV;
5937 /* Set up SLI API function jump table */
5938 rc = lpfc_sli_api_table_setup(phba, dev_grp);
5939 if (rc)
5940 return -ENODEV;
5941 /* Set up MBOX API function jump table */
5942 rc = lpfc_mbox_api_table_setup(phba, dev_grp);
5943 if (rc)
5944 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05005945
James Smartda0436e2009-05-22 14:51:39 -04005946 return 0;
James Smart5b75da22008-12-04 22:39:35 -05005947}
5948
5949/**
James Smart3621a712009-04-06 18:47:14 -04005950 * lpfc_log_intr_mode - Log the active interrupt mode
James Smart5b75da22008-12-04 22:39:35 -05005951 * @phba: pointer to lpfc hba data structure.
5952 * @intr_mode: active interrupt mode adopted.
5953 *
5954 * This routine it invoked to log the currently used active interrupt mode
5955 * to the device.
James Smart3772a992009-05-22 14:50:54 -04005956 **/
5957static void lpfc_log_intr_mode(struct lpfc_hba *phba, uint32_t intr_mode)
James Smart5b75da22008-12-04 22:39:35 -05005958{
5959 switch (intr_mode) {
5960 case 0:
5961 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5962 "0470 Enable INTx interrupt mode.\n");
5963 break;
5964 case 1:
5965 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5966 "0481 Enabled MSI interrupt mode.\n");
5967 break;
5968 case 2:
5969 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5970 "0480 Enabled MSI-X interrupt mode.\n");
5971 break;
5972 default:
5973 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5974 "0482 Illegal interrupt mode.\n");
5975 break;
5976 }
5977 return;
5978}
5979
James Smart5b75da22008-12-04 22:39:35 -05005980/**
James Smart3772a992009-05-22 14:50:54 -04005981 * lpfc_enable_pci_dev - Enable a generic PCI device.
James Smart5b75da22008-12-04 22:39:35 -05005982 * @phba: pointer to lpfc hba data structure.
5983 *
James Smart3772a992009-05-22 14:50:54 -04005984 * This routine is invoked to enable the PCI device that is common to all
5985 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05005986 *
5987 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005988 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04005989 * other values - error
James Smart5b75da22008-12-04 22:39:35 -05005990 **/
James Smart3772a992009-05-22 14:50:54 -04005991static int
5992lpfc_enable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05005993{
James Smart3772a992009-05-22 14:50:54 -04005994 struct pci_dev *pdev;
James Smart5b75da22008-12-04 22:39:35 -05005995
James Smart3772a992009-05-22 14:50:54 -04005996 /* Obtain PCI device reference */
5997 if (!phba->pcidev)
5998 goto out_error;
5999 else
6000 pdev = phba->pcidev;
James Smart3772a992009-05-22 14:50:54 -04006001 /* Enable PCI device */
6002 if (pci_enable_device_mem(pdev))
6003 goto out_error;
6004 /* Request PCI resource for the device */
Johannes Thumshirne0c04832016-06-07 09:44:03 +02006005 if (pci_request_mem_regions(pdev, LPFC_DRIVER_NAME))
James Smart3772a992009-05-22 14:50:54 -04006006 goto out_disable_device;
6007 /* Set up device as PCI master and save state for EEH */
6008 pci_set_master(pdev);
6009 pci_try_set_mwi(pdev);
6010 pci_save_state(pdev);
James Smart5b75da22008-12-04 22:39:35 -05006011
James Smart05580562011-05-24 11:40:48 -04006012 /* PCIe EEH recovery on powerpc platforms needs fundamental reset */
Jon Mason453193e2013-09-11 15:38:13 -07006013 if (pci_is_pcie(pdev))
James Smart05580562011-05-24 11:40:48 -04006014 pdev->needs_freset = 1;
6015
James Smart3772a992009-05-22 14:50:54 -04006016 return 0;
James Smart5b75da22008-12-04 22:39:35 -05006017
James Smart3772a992009-05-22 14:50:54 -04006018out_disable_device:
6019 pci_disable_device(pdev);
6020out_error:
James Smart079b5c92011-08-21 21:48:49 -04006021 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
Johannes Thumshirne0c04832016-06-07 09:44:03 +02006022 "1401 Failed to enable pci device\n");
James Smart3772a992009-05-22 14:50:54 -04006023 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05006024}
6025
6026/**
James Smart3772a992009-05-22 14:50:54 -04006027 * lpfc_disable_pci_dev - Disable a generic PCI device.
James Smart5b75da22008-12-04 22:39:35 -05006028 * @phba: pointer to lpfc hba data structure.
6029 *
James Smart3772a992009-05-22 14:50:54 -04006030 * This routine is invoked to disable the PCI device that is common to all
6031 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05006032 **/
6033static void
James Smart3772a992009-05-22 14:50:54 -04006034lpfc_disable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05006035{
James Smart3772a992009-05-22 14:50:54 -04006036 struct pci_dev *pdev;
James Smart5b75da22008-12-04 22:39:35 -05006037
James Smart3772a992009-05-22 14:50:54 -04006038 /* Obtain PCI device reference */
6039 if (!phba->pcidev)
6040 return;
6041 else
6042 pdev = phba->pcidev;
James Smart3772a992009-05-22 14:50:54 -04006043 /* Release PCI resource and disable PCI device */
Johannes Thumshirne0c04832016-06-07 09:44:03 +02006044 pci_release_mem_regions(pdev);
James Smart3772a992009-05-22 14:50:54 -04006045 pci_disable_device(pdev);
James Smart5b75da22008-12-04 22:39:35 -05006046
6047 return;
6048}
6049
6050/**
James Smart3772a992009-05-22 14:50:54 -04006051 * lpfc_reset_hba - Reset a hba
6052 * @phba: pointer to lpfc hba data structure.
James Smarte59058c2008-08-24 21:49:00 -04006053 *
James Smart3772a992009-05-22 14:50:54 -04006054 * This routine is invoked to reset a hba device. It brings the HBA
6055 * offline, performs a board restart, and then brings the board back
6056 * online. The lpfc_offline calls lpfc_sli_hba_down which will clean up
6057 * on outstanding mailbox commands.
James Smarte59058c2008-08-24 21:49:00 -04006058 **/
James Smart3772a992009-05-22 14:50:54 -04006059void
6060lpfc_reset_hba(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05006061{
James Smart3772a992009-05-22 14:50:54 -04006062 /* If resets are disabled then set error state and return. */
6063 if (!phba->cfg_enable_hba_reset) {
6064 phba->link_state = LPFC_HBA_ERROR;
6065 return;
6066 }
James Smartee620212014-04-04 13:51:53 -04006067 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
6068 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
6069 else
6070 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
James Smart3772a992009-05-22 14:50:54 -04006071 lpfc_offline(phba);
6072 lpfc_sli_brdrestart(phba);
6073 lpfc_online(phba);
6074 lpfc_unblock_mgmt_io(phba);
6075}
dea31012005-04-17 16:05:31 -05006076
James Smart3772a992009-05-22 14:50:54 -04006077/**
James Smart0a96e972011-07-22 18:37:28 -04006078 * lpfc_sli_sriov_nr_virtfn_get - Get the number of sr-iov virtual functions
6079 * @phba: pointer to lpfc hba data structure.
6080 *
6081 * This function enables the PCI SR-IOV virtual functions to a physical
6082 * function. It invokes the PCI SR-IOV api with the @nr_vfn provided to
6083 * enable the number of virtual functions to the physical function. As
6084 * not all devices support SR-IOV, the return code from the pci_enable_sriov()
6085 * API call does not considered as an error condition for most of the device.
6086 **/
6087uint16_t
6088lpfc_sli_sriov_nr_virtfn_get(struct lpfc_hba *phba)
6089{
6090 struct pci_dev *pdev = phba->pcidev;
6091 uint16_t nr_virtfn;
6092 int pos;
6093
James Smart0a96e972011-07-22 18:37:28 -04006094 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
6095 if (pos == 0)
6096 return 0;
6097
6098 pci_read_config_word(pdev, pos + PCI_SRIOV_TOTAL_VF, &nr_virtfn);
6099 return nr_virtfn;
6100}
6101
6102/**
James Smart912e3ac2011-05-24 11:42:11 -04006103 * lpfc_sli_probe_sriov_nr_virtfn - Enable a number of sr-iov virtual functions
6104 * @phba: pointer to lpfc hba data structure.
6105 * @nr_vfn: number of virtual functions to be enabled.
6106 *
6107 * This function enables the PCI SR-IOV virtual functions to a physical
6108 * function. It invokes the PCI SR-IOV api with the @nr_vfn provided to
6109 * enable the number of virtual functions to the physical function. As
6110 * not all devices support SR-IOV, the return code from the pci_enable_sriov()
6111 * API call does not considered as an error condition for most of the device.
6112 **/
6113int
6114lpfc_sli_probe_sriov_nr_virtfn(struct lpfc_hba *phba, int nr_vfn)
6115{
6116 struct pci_dev *pdev = phba->pcidev;
James Smart0a96e972011-07-22 18:37:28 -04006117 uint16_t max_nr_vfn;
James Smart912e3ac2011-05-24 11:42:11 -04006118 int rc;
6119
James Smart0a96e972011-07-22 18:37:28 -04006120 max_nr_vfn = lpfc_sli_sriov_nr_virtfn_get(phba);
6121 if (nr_vfn > max_nr_vfn) {
6122 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6123 "3057 Requested vfs (%d) greater than "
6124 "supported vfs (%d)", nr_vfn, max_nr_vfn);
6125 return -EINVAL;
6126 }
6127
James Smart912e3ac2011-05-24 11:42:11 -04006128 rc = pci_enable_sriov(pdev, nr_vfn);
6129 if (rc) {
6130 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6131 "2806 Failed to enable sriov on this device "
6132 "with vfn number nr_vf:%d, rc:%d\n",
6133 nr_vfn, rc);
6134 } else
6135 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6136 "2807 Successful enable sriov on this device "
6137 "with vfn number nr_vf:%d\n", nr_vfn);
6138 return rc;
6139}
6140
6141/**
James Smart895427b2017-02-12 13:52:30 -08006142 * lpfc_setup_driver_resource_phase1 - Phase1 etup driver internal resources.
James Smart3772a992009-05-22 14:50:54 -04006143 * @phba: pointer to lpfc hba data structure.
6144 *
James Smart895427b2017-02-12 13:52:30 -08006145 * This routine is invoked to set up the driver internal resources before the
6146 * device specific resource setup to support the HBA device it attached to.
James Smart3772a992009-05-22 14:50:54 -04006147 *
6148 * Return codes
James Smart895427b2017-02-12 13:52:30 -08006149 * 0 - successful
6150 * other values - error
James Smart3772a992009-05-22 14:50:54 -04006151 **/
6152static int
James Smart895427b2017-02-12 13:52:30 -08006153lpfc_setup_driver_resource_phase1(struct lpfc_hba *phba)
James Smart3772a992009-05-22 14:50:54 -04006154{
James Smart895427b2017-02-12 13:52:30 -08006155 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05006156
James Smart3772a992009-05-22 14:50:54 -04006157 /*
James Smart895427b2017-02-12 13:52:30 -08006158 * Driver resources common to all SLI revisions
James Smart3772a992009-05-22 14:50:54 -04006159 */
James Smart895427b2017-02-12 13:52:30 -08006160 atomic_set(&phba->fast_event_count, 0);
6161 spin_lock_init(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006162
James Smart895427b2017-02-12 13:52:30 -08006163 /* Initialize ndlp management spinlock */
6164 spin_lock_init(&phba->ndlp_lock);
James Smart3772a992009-05-22 14:50:54 -04006165
James Smart523128e2018-09-10 10:30:46 -07006166 /* Initialize port_list spinlock */
6167 spin_lock_init(&phba->port_list_lock);
James Smart895427b2017-02-12 13:52:30 -08006168 INIT_LIST_HEAD(&phba->port_list);
James Smart523128e2018-09-10 10:30:46 -07006169
James Smart895427b2017-02-12 13:52:30 -08006170 INIT_LIST_HEAD(&phba->work_list);
6171 init_waitqueue_head(&phba->wait_4_mlo_m_q);
6172
6173 /* Initialize the wait queue head for the kernel thread */
6174 init_waitqueue_head(&phba->work_waitq);
6175
6176 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smartf358dd02017-02-12 13:52:34 -08006177 "1403 Protocols supported %s %s %s\n",
James Smart895427b2017-02-12 13:52:30 -08006178 ((phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) ?
6179 "SCSI" : " "),
6180 ((phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) ?
James Smartf358dd02017-02-12 13:52:34 -08006181 "NVME" : " "),
6182 (phba->nvmet_support ? "NVMET" : " "));
James Smart895427b2017-02-12 13:52:30 -08006183
James Smart0794d602019-01-28 11:14:19 -08006184 /* Initialize the IO buffer list used by driver for SLI3 SCSI */
6185 spin_lock_init(&phba->scsi_buf_list_get_lock);
6186 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_get);
6187 spin_lock_init(&phba->scsi_buf_list_put_lock);
6188 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
James Smart895427b2017-02-12 13:52:30 -08006189
6190 /* Initialize the fabric iocb list */
6191 INIT_LIST_HEAD(&phba->fabric_iocb_list);
6192
6193 /* Initialize list to save ELS buffers */
6194 INIT_LIST_HEAD(&phba->elsbuf);
6195
6196 /* Initialize FCF connection rec list */
6197 INIT_LIST_HEAD(&phba->fcf_conn_rec_list);
6198
6199 /* Initialize OAS configuration list */
6200 spin_lock_init(&phba->devicelock);
6201 INIT_LIST_HEAD(&phba->luns);
6202
James Smart3772a992009-05-22 14:50:54 -04006203 /* MBOX heartbeat timer */
Kees Cookf22eb4d2017-09-06 20:24:26 -07006204 timer_setup(&psli->mbox_tmo, lpfc_mbox_timeout, 0);
James Smart3772a992009-05-22 14:50:54 -04006205 /* Fabric block timer */
Kees Cookf22eb4d2017-09-06 20:24:26 -07006206 timer_setup(&phba->fabric_block_timer, lpfc_fabric_block_timeout, 0);
James Smart3772a992009-05-22 14:50:54 -04006207 /* EA polling mode timer */
Kees Cookf22eb4d2017-09-06 20:24:26 -07006208 timer_setup(&phba->eratt_poll, lpfc_poll_eratt, 0);
James Smart895427b2017-02-12 13:52:30 -08006209 /* Heartbeat timer */
Kees Cookf22eb4d2017-09-06 20:24:26 -07006210 timer_setup(&phba->hb_tmofunc, lpfc_hb_timeout, 0);
James Smart895427b2017-02-12 13:52:30 -08006211
James Smart32517fc2019-01-28 11:14:33 -08006212 INIT_DELAYED_WORK(&phba->eq_delay_work, lpfc_hb_eq_delay_work);
6213
James Smart895427b2017-02-12 13:52:30 -08006214 return 0;
6215}
6216
6217/**
6218 * lpfc_sli_driver_resource_setup - Setup driver internal resources for SLI3 dev
6219 * @phba: pointer to lpfc hba data structure.
6220 *
6221 * This routine is invoked to set up the driver internal resources specific to
6222 * support the SLI-3 HBA device it attached to.
6223 *
6224 * Return codes
6225 * 0 - successful
6226 * other values - error
6227 **/
6228static int
6229lpfc_sli_driver_resource_setup(struct lpfc_hba *phba)
6230{
James Smart0794d602019-01-28 11:14:19 -08006231 int rc, entry_sz;
James Smart895427b2017-02-12 13:52:30 -08006232
6233 /*
6234 * Initialize timers used by driver
6235 */
6236
6237 /* FCP polling mode timer */
Kees Cookf22eb4d2017-09-06 20:24:26 -07006238 timer_setup(&phba->fcp_poll_timer, lpfc_poll_timeout, 0);
James Smart3772a992009-05-22 14:50:54 -04006239
6240 /* Host attention work mask setup */
6241 phba->work_ha_mask = (HA_ERATT | HA_MBATT | HA_LATT);
6242 phba->work_ha_mask |= (HA_RXMASK << (LPFC_ELS_RING * 4));
6243
6244 /* Get all the module params for configuring this host */
6245 lpfc_get_cfgparam(phba);
James Smart895427b2017-02-12 13:52:30 -08006246 /* Set up phase-1 common device driver resources */
6247
6248 rc = lpfc_setup_driver_resource_phase1(phba);
6249 if (rc)
6250 return -ENODEV;
6251
James Smart49198b32010-04-06 15:04:33 -04006252 if (phba->pcidev->device == PCI_DEVICE_ID_HORNET) {
6253 phba->menlo_flag |= HBA_MENLO_SUPPORT;
6254 /* check for menlo minimum sg count */
6255 if (phba->cfg_sg_seg_cnt < LPFC_DEFAULT_MENLO_SG_SEG_CNT)
6256 phba->cfg_sg_seg_cnt = LPFC_DEFAULT_MENLO_SG_SEG_CNT;
6257 }
6258
James Smart895427b2017-02-12 13:52:30 -08006259 if (!phba->sli.sli3_ring)
Kees Cook6396bb22018-06-12 14:03:40 -07006260 phba->sli.sli3_ring = kcalloc(LPFC_SLI3_MAX_RING,
6261 sizeof(struct lpfc_sli_ring),
6262 GFP_KERNEL);
James Smart895427b2017-02-12 13:52:30 -08006263 if (!phba->sli.sli3_ring)
James Smart2a76a282012-08-03 12:35:54 -04006264 return -ENOMEM;
6265
James Smart3772a992009-05-22 14:50:54 -04006266 /*
James Smart96f70772013-04-17 20:16:15 -04006267 * Since lpfc_sg_seg_cnt is module parameter, the sg_dma_buf_size
James Smart3772a992009-05-22 14:50:54 -04006268 * used to create the sg_dma_buf_pool must be dynamically calculated.
James Smart3772a992009-05-22 14:50:54 -04006269 */
James Smart3772a992009-05-22 14:50:54 -04006270
James Smart96f70772013-04-17 20:16:15 -04006271 /* Initialize the host templates the configured values. */
James Smart3772a992009-05-22 14:50:54 -04006272 lpfc_vport_template.sg_tablesize = phba->cfg_sg_seg_cnt;
James Smart96418b52017-03-04 09:30:31 -08006273 lpfc_template_no_hr.sg_tablesize = phba->cfg_sg_seg_cnt;
6274 lpfc_template.sg_tablesize = phba->cfg_sg_seg_cnt;
James Smart3772a992009-05-22 14:50:54 -04006275
James Smart0794d602019-01-28 11:14:19 -08006276 if (phba->sli_rev == LPFC_SLI_REV4)
6277 entry_sz = sizeof(struct sli4_sge);
6278 else
6279 entry_sz = sizeof(struct ulp_bde64);
6280
James Smart96f70772013-04-17 20:16:15 -04006281 /* There are going to be 2 reserved BDEs: 1 FCP cmnd + 1 FCP rsp */
6282 if (phba->cfg_enable_bg) {
6283 /*
6284 * The scsi_buf for a T10-DIF I/O will hold the FCP cmnd,
6285 * the FCP rsp, and a BDE for each. Sice we have no control
6286 * over how many protection data segments the SCSI Layer
6287 * will hand us (ie: there could be one for every block
6288 * in the IO), we just allocate enough BDEs to accomidate
6289 * our max amount and we need to limit lpfc_sg_seg_cnt to
6290 * minimize the risk of running out.
6291 */
6292 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
6293 sizeof(struct fcp_rsp) +
James Smart0794d602019-01-28 11:14:19 -08006294 (LPFC_MAX_SG_SEG_CNT * entry_sz);
James Smart96f70772013-04-17 20:16:15 -04006295
6296 if (phba->cfg_sg_seg_cnt > LPFC_MAX_SG_SEG_CNT_DIF)
6297 phba->cfg_sg_seg_cnt = LPFC_MAX_SG_SEG_CNT_DIF;
6298
6299 /* Total BDEs in BPL for scsi_sg_list and scsi_sg_prot_list */
6300 phba->cfg_total_seg_cnt = LPFC_MAX_SG_SEG_CNT;
6301 } else {
6302 /*
6303 * The scsi_buf for a regular I/O will hold the FCP cmnd,
6304 * the FCP rsp, a BDE for each, and a BDE for up to
6305 * cfg_sg_seg_cnt data segments.
6306 */
6307 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
6308 sizeof(struct fcp_rsp) +
James Smart0794d602019-01-28 11:14:19 -08006309 ((phba->cfg_sg_seg_cnt + 2) * entry_sz);
James Smart96f70772013-04-17 20:16:15 -04006310
6311 /* Total BDEs in BPL for scsi_sg_list */
6312 phba->cfg_total_seg_cnt = phba->cfg_sg_seg_cnt + 2;
6313 }
6314
6315 lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_FCP,
6316 "9088 sg_tablesize:%d dmabuf_size:%d total_bde:%d\n",
6317 phba->cfg_sg_seg_cnt, phba->cfg_sg_dma_buf_size,
6318 phba->cfg_total_seg_cnt);
6319
James Smart3772a992009-05-22 14:50:54 -04006320 phba->max_vpi = LPFC_MAX_VPI;
6321 /* This will be set to correct value after config_port mbox */
6322 phba->max_vports = 0;
6323
6324 /*
6325 * Initialize the SLI Layer to run with lpfc HBAs.
6326 */
6327 lpfc_sli_setup(phba);
James Smart895427b2017-02-12 13:52:30 -08006328 lpfc_sli_queue_init(phba);
James Smart3772a992009-05-22 14:50:54 -04006329
6330 /* Allocate device driver memory */
6331 if (lpfc_mem_alloc(phba, BPL_ALIGN_SZ))
6332 return -ENOMEM;
6333
James Smart912e3ac2011-05-24 11:42:11 -04006334 /*
6335 * Enable sr-iov virtual functions if supported and configured
6336 * through the module parameter.
6337 */
6338 if (phba->cfg_sriov_nr_virtfn > 0) {
6339 rc = lpfc_sli_probe_sriov_nr_virtfn(phba,
6340 phba->cfg_sriov_nr_virtfn);
6341 if (rc) {
6342 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6343 "2808 Requested number of SR-IOV "
6344 "virtual functions (%d) is not "
6345 "supported\n",
6346 phba->cfg_sriov_nr_virtfn);
6347 phba->cfg_sriov_nr_virtfn = 0;
6348 }
6349 }
6350
James Smart3772a992009-05-22 14:50:54 -04006351 return 0;
6352}
6353
6354/**
6355 * lpfc_sli_driver_resource_unset - Unset drvr internal resources for SLI3 dev
6356 * @phba: pointer to lpfc hba data structure.
6357 *
6358 * This routine is invoked to unset the driver internal resources set up
6359 * specific for supporting the SLI-3 HBA device it attached to.
6360 **/
6361static void
6362lpfc_sli_driver_resource_unset(struct lpfc_hba *phba)
6363{
6364 /* Free device driver memory allocated */
6365 lpfc_mem_free_all(phba);
6366
6367 return;
6368}
6369
6370/**
James Smartda0436e2009-05-22 14:51:39 -04006371 * lpfc_sli4_driver_resource_setup - Setup drvr internal resources for SLI4 dev
6372 * @phba: pointer to lpfc hba data structure.
6373 *
6374 * This routine is invoked to set up the driver internal resources specific to
6375 * support the SLI-4 HBA device it attached to.
6376 *
6377 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006378 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04006379 * other values - error
6380 **/
6381static int
6382lpfc_sli4_driver_resource_setup(struct lpfc_hba *phba)
6383{
James Smart28baac72010-02-12 14:42:03 -05006384 LPFC_MBOXQ_t *mboxq;
James Smartf358dd02017-02-12 13:52:34 -08006385 MAILBOX_t *mb;
James Smart895427b2017-02-12 13:52:30 -08006386 int rc, i, max_buf_size;
James Smart28baac72010-02-12 14:42:03 -05006387 uint8_t pn_page[LPFC_MAX_SUPPORTED_PAGES] = {0};
6388 struct lpfc_mqe *mqe;
James Smart09294d42013-04-17 20:16:05 -04006389 int longs;
James Smart81e6a632017-11-20 16:00:43 -08006390 int extra;
James Smartf358dd02017-02-12 13:52:34 -08006391 uint64_t wwn;
James Smartb92dc722018-05-24 21:09:01 -07006392 u32 if_type;
6393 u32 if_fam;
James Smartda0436e2009-05-22 14:51:39 -04006394
James Smart895427b2017-02-12 13:52:30 -08006395 phba->sli4_hba.num_present_cpu = lpfc_present_cpu;
James Smart222e9232019-01-28 11:14:35 -08006396 phba->sli4_hba.num_possible_cpu = num_possible_cpus();
James Smart895427b2017-02-12 13:52:30 -08006397 phba->sli4_hba.curr_disp_cpu = 0;
6398
James Smart716d3bc2013-09-06 12:18:28 -04006399 /* Get all the module params for configuring this host */
6400 lpfc_get_cfgparam(phba);
6401
James Smart895427b2017-02-12 13:52:30 -08006402 /* Set up phase-1 common device driver resources */
6403 rc = lpfc_setup_driver_resource_phase1(phba);
6404 if (rc)
6405 return -ENODEV;
6406
James Smartda0436e2009-05-22 14:51:39 -04006407 /* Before proceed, wait for POST done and device ready */
6408 rc = lpfc_sli4_post_status_check(phba);
6409 if (rc)
6410 return -ENODEV;
6411
6412 /*
6413 * Initialize timers used by driver
6414 */
6415
Kees Cookf22eb4d2017-09-06 20:24:26 -07006416 timer_setup(&phba->rrq_tmr, lpfc_rrq_timeout, 0);
James Smartda0436e2009-05-22 14:51:39 -04006417
James Smartecfd03c2010-02-12 14:41:27 -05006418 /* FCF rediscover timer */
Kees Cookf22eb4d2017-09-06 20:24:26 -07006419 timer_setup(&phba->fcf.redisc_wait, lpfc_sli4_fcf_redisc_wait_tmo, 0);
James Smartecfd03c2010-02-12 14:41:27 -05006420
James Smartda0436e2009-05-22 14:51:39 -04006421 /*
James Smart7ad20aa2011-05-24 11:44:28 -04006422 * Control structure for handling external multi-buffer mailbox
6423 * command pass-through.
6424 */
6425 memset((uint8_t *)&phba->mbox_ext_buf_ctx, 0,
6426 sizeof(struct lpfc_mbox_ext_buf_ctx));
6427 INIT_LIST_HEAD(&phba->mbox_ext_buf_ctx.ext_dmabuf_list);
6428
James Smartda0436e2009-05-22 14:51:39 -04006429 phba->max_vpi = LPFC_MAX_VPI;
James Smart67d12732012-08-03 12:36:13 -04006430
James Smartda0436e2009-05-22 14:51:39 -04006431 /* This will be set to correct value after the read_config mbox */
6432 phba->max_vports = 0;
6433
6434 /* Program the default value of vlan_id and fc_map */
6435 phba->valid_vlan = 0;
6436 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
6437 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
6438 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
6439
6440 /*
James Smart2a76a282012-08-03 12:35:54 -04006441 * For SLI4, instead of using ring 0 (LPFC_FCP_RING) for FCP commands
James Smart895427b2017-02-12 13:52:30 -08006442 * we will associate a new ring, for each EQ/CQ/WQ tuple.
6443 * The WQ create will allocate the ring.
James Smart2a76a282012-08-03 12:35:54 -04006444 */
James Smart085c6472010-11-20 23:11:37 -05006445
James Smart09294d42013-04-17 20:16:05 -04006446 /*
James Smart81e6a632017-11-20 16:00:43 -08006447 * 1 for cmd, 1 for rsp, NVME adds an extra one
6448 * for boundary conditions in its max_sgl_segment template.
6449 */
6450 extra = 2;
6451 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
6452 extra++;
6453
6454 /*
James Smart09294d42013-04-17 20:16:05 -04006455 * It doesn't matter what family our adapter is in, we are
6456 * limited to 2 Pages, 512 SGEs, for our SGL.
6457 * There are going to be 2 reserved SGEs: 1 FCP cmnd + 1 FCP rsp
6458 */
6459 max_buf_size = (2 * SLI4_PAGE_SIZE);
James Smart09294d42013-04-17 20:16:05 -04006460
James Smartda0436e2009-05-22 14:51:39 -04006461 /*
James Smart895427b2017-02-12 13:52:30 -08006462 * Since lpfc_sg_seg_cnt is module param, the sg_dma_buf_size
6463 * used to create the sg_dma_buf_pool must be calculated.
James Smart28baac72010-02-12 14:42:03 -05006464 */
James Smartf44ac122018-03-05 12:04:08 -08006465 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED) {
James Smart96f70772013-04-17 20:16:15 -04006466 /*
James Smart895427b2017-02-12 13:52:30 -08006467 * The scsi_buf for a T10-DIF I/O holds the FCP cmnd,
6468 * the FCP rsp, and a SGE. Sice we have no control
6469 * over how many protection segments the SCSI Layer
James Smart96f70772013-04-17 20:16:15 -04006470 * will hand us (ie: there could be one for every block
James Smart895427b2017-02-12 13:52:30 -08006471 * in the IO), just allocate enough SGEs to accomidate
6472 * our max amount and we need to limit lpfc_sg_seg_cnt
6473 * to minimize the risk of running out.
James Smart96f70772013-04-17 20:16:15 -04006474 */
6475 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
James Smart895427b2017-02-12 13:52:30 -08006476 sizeof(struct fcp_rsp) + max_buf_size;
James Smart96f70772013-04-17 20:16:15 -04006477
6478 /* Total SGEs for scsi_sg_list and scsi_sg_prot_list */
6479 phba->cfg_total_seg_cnt = LPFC_MAX_SGL_SEG_CNT;
6480
James Smart5b9e70b2018-09-10 10:30:42 -07006481 /*
6482 * If supporting DIF, reduce the seg count for scsi to
6483 * allow room for the DIF sges.
6484 */
6485 if (phba->cfg_enable_bg &&
6486 phba->cfg_sg_seg_cnt > LPFC_MAX_BG_SLI4_SEG_CNT_DIF)
6487 phba->cfg_scsi_seg_cnt = LPFC_MAX_BG_SLI4_SEG_CNT_DIF;
6488 else
6489 phba->cfg_scsi_seg_cnt = phba->cfg_sg_seg_cnt;
6490
James Smart96f70772013-04-17 20:16:15 -04006491 } else {
6492 /*
James Smart895427b2017-02-12 13:52:30 -08006493 * The scsi_buf for a regular I/O holds the FCP cmnd,
James Smart96f70772013-04-17 20:16:15 -04006494 * the FCP rsp, a SGE for each, and a SGE for up to
6495 * cfg_sg_seg_cnt data segments.
6496 */
6497 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
James Smart895427b2017-02-12 13:52:30 -08006498 sizeof(struct fcp_rsp) +
James Smart81e6a632017-11-20 16:00:43 -08006499 ((phba->cfg_sg_seg_cnt + extra) *
James Smart895427b2017-02-12 13:52:30 -08006500 sizeof(struct sli4_sge));
James Smart96f70772013-04-17 20:16:15 -04006501
6502 /* Total SGEs for scsi_sg_list */
James Smart81e6a632017-11-20 16:00:43 -08006503 phba->cfg_total_seg_cnt = phba->cfg_sg_seg_cnt + extra;
James Smart5b9e70b2018-09-10 10:30:42 -07006504 phba->cfg_scsi_seg_cnt = phba->cfg_sg_seg_cnt;
James Smart895427b2017-02-12 13:52:30 -08006505
James Smart96f70772013-04-17 20:16:15 -04006506 /*
James Smart81e6a632017-11-20 16:00:43 -08006507 * NOTE: if (phba->cfg_sg_seg_cnt + extra) <= 256 we only
James Smart895427b2017-02-12 13:52:30 -08006508 * need to post 1 page for the SGL.
James Smart96f70772013-04-17 20:16:15 -04006509 */
James Smart085c6472010-11-20 23:11:37 -05006510 }
James Smartacd68592012-01-18 16:25:09 -05006511
James Smart5b9e70b2018-09-10 10:30:42 -07006512 /* Limit to LPFC_MAX_NVME_SEG_CNT for NVME. */
6513 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
6514 if (phba->cfg_sg_seg_cnt > LPFC_MAX_NVME_SEG_CNT) {
6515 lpfc_printf_log(phba, KERN_INFO, LOG_NVME | LOG_INIT,
6516 "6300 Reducing NVME sg segment "
6517 "cnt to %d\n",
6518 LPFC_MAX_NVME_SEG_CNT);
6519 phba->cfg_nvme_seg_cnt = LPFC_MAX_NVME_SEG_CNT;
6520 } else
6521 phba->cfg_nvme_seg_cnt = phba->cfg_sg_seg_cnt;
6522 }
6523
James Smart96f70772013-04-17 20:16:15 -04006524 /* Initialize the host templates with the updated values. */
James Smart5b9e70b2018-09-10 10:30:42 -07006525 lpfc_vport_template.sg_tablesize = phba->cfg_scsi_seg_cnt;
6526 lpfc_template.sg_tablesize = phba->cfg_scsi_seg_cnt;
6527 lpfc_template_no_hr.sg_tablesize = phba->cfg_scsi_seg_cnt;
James Smart96f70772013-04-17 20:16:15 -04006528
6529 if (phba->cfg_sg_dma_buf_size <= LPFC_MIN_SG_SLI4_BUF_SZ)
6530 phba->cfg_sg_dma_buf_size = LPFC_MIN_SG_SLI4_BUF_SZ;
6531 else
6532 phba->cfg_sg_dma_buf_size =
6533 SLI4_PAGE_ALIGN(phba->cfg_sg_dma_buf_size);
6534
6535 lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_FCP,
James Smart5b9e70b2018-09-10 10:30:42 -07006536 "9087 sg_seg_cnt:%d dmabuf_size:%d "
6537 "total:%d scsi:%d nvme:%d\n",
James Smart96f70772013-04-17 20:16:15 -04006538 phba->cfg_sg_seg_cnt, phba->cfg_sg_dma_buf_size,
James Smart5b9e70b2018-09-10 10:30:42 -07006539 phba->cfg_total_seg_cnt, phba->cfg_scsi_seg_cnt,
6540 phba->cfg_nvme_seg_cnt);
James Smartda0436e2009-05-22 14:51:39 -04006541
6542 /* Initialize buffer queue management fields */
James Smart895427b2017-02-12 13:52:30 -08006543 INIT_LIST_HEAD(&phba->hbqs[LPFC_ELS_HBQ].hbq_buffer_list);
James Smartda0436e2009-05-22 14:51:39 -04006544 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_sli4_rb_alloc;
6545 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_sli4_rb_free;
6546
6547 /*
6548 * Initialize the SLI Layer to run with lpfc SLI4 HBAs.
6549 */
James Smart895427b2017-02-12 13:52:30 -08006550 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) {
6551 /* Initialize the Abort scsi buffer list used by driver */
6552 spin_lock_init(&phba->sli4_hba.abts_scsi_buf_list_lock);
6553 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
6554 }
6555
6556 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
6557 /* Initialize the Abort nvme buffer list used by driver */
James Smart5e5b5112019-01-28 11:14:22 -08006558 spin_lock_init(&phba->sli4_hba.abts_nvmet_buf_list_lock);
James Smart86c67372017-04-21 16:05:04 -07006559 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_nvmet_ctx_list);
James Smarta8cf5df2017-05-15 15:20:46 -07006560 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_nvmet_io_wait_list);
James Smart79d8c4c2019-05-21 17:48:56 -07006561 spin_lock_init(&phba->sli4_hba.t_active_list_lock);
6562 INIT_LIST_HEAD(&phba->sli4_hba.t_active_ctx_list);
James Smart895427b2017-02-12 13:52:30 -08006563 }
6564
James Smartda0436e2009-05-22 14:51:39 -04006565 /* This abort list used by worker thread */
James Smart895427b2017-02-12 13:52:30 -08006566 spin_lock_init(&phba->sli4_hba.sgl_list_lock);
James Smarta8cf5df2017-05-15 15:20:46 -07006567 spin_lock_init(&phba->sli4_hba.nvmet_io_wait_lock);
James Smartda0436e2009-05-22 14:51:39 -04006568
6569 /*
James Smart6d368e52011-05-24 11:44:12 -04006570 * Initialize driver internal slow-path work queues
James Smartda0436e2009-05-22 14:51:39 -04006571 */
6572
6573 /* Driver internel slow-path CQ Event pool */
6574 INIT_LIST_HEAD(&phba->sli4_hba.sp_cqe_event_pool);
6575 /* Response IOCB work queue list */
James Smart45ed1192009-10-02 15:17:02 -04006576 INIT_LIST_HEAD(&phba->sli4_hba.sp_queue_event);
James Smartda0436e2009-05-22 14:51:39 -04006577 /* Asynchronous event CQ Event work queue list */
6578 INIT_LIST_HEAD(&phba->sli4_hba.sp_asynce_work_queue);
6579 /* Fast-path XRI aborted CQ Event work queue list */
6580 INIT_LIST_HEAD(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
6581 /* Slow-path XRI aborted CQ Event work queue list */
6582 INIT_LIST_HEAD(&phba->sli4_hba.sp_els_xri_aborted_work_queue);
6583 /* Receive queue CQ Event work queue list */
6584 INIT_LIST_HEAD(&phba->sli4_hba.sp_unsol_work_queue);
6585
James Smart6d368e52011-05-24 11:44:12 -04006586 /* Initialize extent block lists. */
6587 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_blk_list);
6588 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_xri_blk_list);
6589 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_vfi_blk_list);
6590 INIT_LIST_HEAD(&phba->lpfc_vpi_blk_list);
6591
James Smartd1f525a2017-04-21 16:04:55 -07006592 /* Initialize mboxq lists. If the early init routines fail
6593 * these lists need to be correctly initialized.
6594 */
6595 INIT_LIST_HEAD(&phba->sli.mboxq);
6596 INIT_LIST_HEAD(&phba->sli.mboxq_cmpl);
6597
James Smart448193b2015-12-16 18:12:05 -05006598 /* initialize optic_state to 0xFF */
6599 phba->sli4_hba.lnk_info.optic_state = 0xff;
6600
James Smartda0436e2009-05-22 14:51:39 -04006601 /* Allocate device driver memory */
6602 rc = lpfc_mem_alloc(phba, SGL_ALIGN_SZ);
6603 if (rc)
6604 return -ENOMEM;
6605
James Smart2fcee4b2010-12-15 17:57:46 -05006606 /* IF Type 2 ports get initialized now. */
James Smart27d6ac02018-02-22 08:18:42 -08006607 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) >=
James Smart2fcee4b2010-12-15 17:57:46 -05006608 LPFC_SLI_INTF_IF_TYPE_2) {
6609 rc = lpfc_pci_function_reset(phba);
James Smart895427b2017-02-12 13:52:30 -08006610 if (unlikely(rc)) {
6611 rc = -ENODEV;
6612 goto out_free_mem;
6613 }
James Smart946727d2015-04-07 15:07:09 -04006614 phba->temp_sensor_support = 1;
James Smart2fcee4b2010-12-15 17:57:46 -05006615 }
6616
James Smartda0436e2009-05-22 14:51:39 -04006617 /* Create the bootstrap mailbox command */
6618 rc = lpfc_create_bootstrap_mbox(phba);
6619 if (unlikely(rc))
6620 goto out_free_mem;
6621
6622 /* Set up the host's endian order with the device. */
6623 rc = lpfc_setup_endian_order(phba);
6624 if (unlikely(rc))
6625 goto out_free_bsmbx;
6626
6627 /* Set up the hba's configuration parameters. */
6628 rc = lpfc_sli4_read_config(phba);
6629 if (unlikely(rc))
6630 goto out_free_bsmbx;
James Smartcff261f2013-12-17 20:29:47 -05006631 rc = lpfc_mem_alloc_active_rrq_pool_s4(phba);
6632 if (unlikely(rc))
6633 goto out_free_bsmbx;
James Smartda0436e2009-05-22 14:51:39 -04006634
James Smart2fcee4b2010-12-15 17:57:46 -05006635 /* IF Type 0 ports get initialized now. */
6636 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
6637 LPFC_SLI_INTF_IF_TYPE_0) {
6638 rc = lpfc_pci_function_reset(phba);
6639 if (unlikely(rc))
6640 goto out_free_bsmbx;
6641 }
James Smartda0436e2009-05-22 14:51:39 -04006642
James Smartcb5172e2010-03-15 11:25:07 -04006643 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
6644 GFP_KERNEL);
6645 if (!mboxq) {
6646 rc = -ENOMEM;
6647 goto out_free_bsmbx;
6648 }
6649
James Smartf358dd02017-02-12 13:52:34 -08006650 /* Check for NVMET being configured */
James Smart895427b2017-02-12 13:52:30 -08006651 phba->nvmet_support = 0;
James Smartf358dd02017-02-12 13:52:34 -08006652 if (lpfc_enable_nvmet_cnt) {
6653
6654 /* First get WWN of HBA instance */
6655 lpfc_read_nv(phba, mboxq);
6656 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6657 if (rc != MBX_SUCCESS) {
6658 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6659 "6016 Mailbox failed , mbxCmd x%x "
6660 "READ_NV, mbxStatus x%x\n",
6661 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
6662 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
James Smartd1f525a2017-04-21 16:04:55 -07006663 mempool_free(mboxq, phba->mbox_mem_pool);
James Smartf358dd02017-02-12 13:52:34 -08006664 rc = -EIO;
6665 goto out_free_bsmbx;
6666 }
6667 mb = &mboxq->u.mb;
6668 memcpy(&wwn, (char *)mb->un.varRDnvp.nodename,
6669 sizeof(uint64_t));
6670 wwn = cpu_to_be64(wwn);
6671 phba->sli4_hba.wwnn.u.name = wwn;
6672 memcpy(&wwn, (char *)mb->un.varRDnvp.portname,
6673 sizeof(uint64_t));
6674 /* wwn is WWPN of HBA instance */
6675 wwn = cpu_to_be64(wwn);
6676 phba->sli4_hba.wwpn.u.name = wwn;
6677
6678 /* Check to see if it matches any module parameter */
6679 for (i = 0; i < lpfc_enable_nvmet_cnt; i++) {
6680 if (wwn == lpfc_enable_nvmet[i]) {
James Smart7d708032017-03-08 14:36:01 -08006681#if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
James Smart3c603be2017-05-15 15:20:44 -07006682 if (lpfc_nvmet_mem_alloc(phba))
6683 break;
6684
6685 phba->nvmet_support = 1; /* a match */
6686
James Smartf358dd02017-02-12 13:52:34 -08006687 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6688 "6017 NVME Target %016llx\n",
6689 wwn);
James Smart7d708032017-03-08 14:36:01 -08006690#else
6691 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6692 "6021 Can't enable NVME Target."
6693 " NVME_TARGET_FC infrastructure"
6694 " is not in kernel\n");
6695#endif
James Smartc4908502019-01-28 11:14:28 -08006696 /* Not supported for NVMET */
6697 phba->cfg_xri_rebalancing = 0;
James Smart3c603be2017-05-15 15:20:44 -07006698 break;
James Smartf358dd02017-02-12 13:52:34 -08006699 }
6700 }
6701 }
James Smart895427b2017-02-12 13:52:30 -08006702
6703 lpfc_nvme_mod_param_dep(phba);
6704
James Smartfedd3b72011-02-16 12:39:24 -05006705 /* Get the Supported Pages if PORT_CAPABILITIES is supported by port. */
James Smartcb5172e2010-03-15 11:25:07 -04006706 lpfc_supported_pages(mboxq);
6707 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smartfedd3b72011-02-16 12:39:24 -05006708 if (!rc) {
6709 mqe = &mboxq->u.mqe;
6710 memcpy(&pn_page[0], ((uint8_t *)&mqe->un.supp_pages.word3),
6711 LPFC_MAX_SUPPORTED_PAGES);
6712 for (i = 0; i < LPFC_MAX_SUPPORTED_PAGES; i++) {
6713 switch (pn_page[i]) {
6714 case LPFC_SLI4_PARAMETERS:
6715 phba->sli4_hba.pc_sli4_params.supported = 1;
6716 break;
6717 default:
6718 break;
6719 }
6720 }
6721 /* Read the port's SLI4 Parameters capabilities if supported. */
6722 if (phba->sli4_hba.pc_sli4_params.supported)
6723 rc = lpfc_pc_sli4_params_get(phba, mboxq);
6724 if (rc) {
6725 mempool_free(mboxq, phba->mbox_mem_pool);
6726 rc = -EIO;
6727 goto out_free_bsmbx;
James Smartcb5172e2010-03-15 11:25:07 -04006728 }
6729 }
James Smart65791f12016-07-06 12:35:56 -07006730
James Smartfedd3b72011-02-16 12:39:24 -05006731 /*
6732 * Get sli4 parameters that override parameters from Port capabilities.
James Smart6d368e52011-05-24 11:44:12 -04006733 * If this call fails, it isn't critical unless the SLI4 parameters come
6734 * back in conflict.
James Smartfedd3b72011-02-16 12:39:24 -05006735 */
James Smart6d368e52011-05-24 11:44:12 -04006736 rc = lpfc_get_sli4_parameters(phba, mboxq);
6737 if (rc) {
James Smartb92dc722018-05-24 21:09:01 -07006738 if_type = bf_get(lpfc_sli_intf_if_type,
6739 &phba->sli4_hba.sli_intf);
6740 if_fam = bf_get(lpfc_sli_intf_sli_family,
6741 &phba->sli4_hba.sli_intf);
James Smart6d368e52011-05-24 11:44:12 -04006742 if (phba->sli4_hba.extents_in_use &&
6743 phba->sli4_hba.rpi_hdrs_in_use) {
6744 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6745 "2999 Unsupported SLI4 Parameters "
6746 "Extents and RPI headers enabled.\n");
James Smartb92dc722018-05-24 21:09:01 -07006747 if (if_type == LPFC_SLI_INTF_IF_TYPE_0 &&
6748 if_fam == LPFC_SLI_INTF_FAMILY_BE2) {
6749 mempool_free(mboxq, phba->mbox_mem_pool);
6750 rc = -EIO;
6751 goto out_free_bsmbx;
6752 }
James Smart6d368e52011-05-24 11:44:12 -04006753 }
James Smartb92dc722018-05-24 21:09:01 -07006754 if (!(if_type == LPFC_SLI_INTF_IF_TYPE_0 &&
6755 if_fam == LPFC_SLI_INTF_FAMILY_BE2)) {
6756 mempool_free(mboxq, phba->mbox_mem_pool);
6757 rc = -EIO;
6758 goto out_free_bsmbx;
6759 }
James Smart6d368e52011-05-24 11:44:12 -04006760 }
James Smart895427b2017-02-12 13:52:30 -08006761
James Smartcb5172e2010-03-15 11:25:07 -04006762 mempool_free(mboxq, phba->mbox_mem_pool);
James Smart1ba981f2014-02-20 09:56:45 -05006763
6764 /* Verify OAS is supported */
6765 lpfc_sli4_oas_verify(phba);
James Smart1ba981f2014-02-20 09:56:45 -05006766
James Smartd2cc9bc2018-09-10 10:30:50 -07006767 /* Verify RAS support on adapter */
6768 lpfc_sli4_ras_init(phba);
6769
James Smart5350d872011-10-10 21:33:49 -04006770 /* Verify all the SLI4 queues */
6771 rc = lpfc_sli4_queue_verify(phba);
James Smartda0436e2009-05-22 14:51:39 -04006772 if (rc)
6773 goto out_free_bsmbx;
6774
6775 /* Create driver internal CQE event pool */
6776 rc = lpfc_sli4_cq_event_pool_create(phba);
6777 if (rc)
James Smart5350d872011-10-10 21:33:49 -04006778 goto out_free_bsmbx;
James Smartda0436e2009-05-22 14:51:39 -04006779
James Smart8a9d2e82012-05-09 21:16:12 -04006780 /* Initialize sgl lists per host */
6781 lpfc_init_sgl_list(phba);
6782
6783 /* Allocate and initialize active sgl array */
James Smartda0436e2009-05-22 14:51:39 -04006784 rc = lpfc_init_active_sgl_array(phba);
6785 if (rc) {
6786 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6787 "1430 Failed to initialize sgl list.\n");
James Smart8a9d2e82012-05-09 21:16:12 -04006788 goto out_destroy_cq_event_pool;
James Smartda0436e2009-05-22 14:51:39 -04006789 }
James Smartda0436e2009-05-22 14:51:39 -04006790 rc = lpfc_sli4_init_rpi_hdrs(phba);
6791 if (rc) {
6792 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6793 "1432 Failed to initialize rpi headers.\n");
6794 goto out_free_active_sgl;
6795 }
6796
James Smarta93ff372010-10-22 11:06:08 -04006797 /* Allocate eligible FCF bmask memory for FCF roundrobin failover */
James Smart0c9ab6f2010-02-26 14:15:57 -05006798 longs = (LPFC_SLI4_FCF_TBL_INDX_MAX + BITS_PER_LONG - 1)/BITS_PER_LONG;
Kees Cook6396bb22018-06-12 14:03:40 -07006799 phba->fcf.fcf_rr_bmask = kcalloc(longs, sizeof(unsigned long),
James Smart0c9ab6f2010-02-26 14:15:57 -05006800 GFP_KERNEL);
6801 if (!phba->fcf.fcf_rr_bmask) {
6802 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6803 "2759 Failed allocate memory for FCF round "
6804 "robin failover bmask\n");
James Smart05580562011-05-24 11:40:48 -04006805 rc = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -05006806 goto out_remove_rpi_hdrs;
6807 }
6808
James Smart6a828b02019-01-28 11:14:31 -08006809 phba->sli4_hba.hba_eq_hdl = kcalloc(phba->cfg_irq_chann,
James Smartcdb42be2019-01-28 11:14:21 -08006810 sizeof(struct lpfc_hba_eq_hdl),
6811 GFP_KERNEL);
James Smart895427b2017-02-12 13:52:30 -08006812 if (!phba->sli4_hba.hba_eq_hdl) {
James Smart67d12732012-08-03 12:36:13 -04006813 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6814 "2572 Failed allocate memory for "
6815 "fast-path per-EQ handle array\n");
6816 rc = -ENOMEM;
6817 goto out_free_fcf_rr_bmask;
James Smartda0436e2009-05-22 14:51:39 -04006818 }
6819
James Smart222e9232019-01-28 11:14:35 -08006820 phba->sli4_hba.cpu_map = kcalloc(phba->sli4_hba.num_possible_cpu,
James Smart895427b2017-02-12 13:52:30 -08006821 sizeof(struct lpfc_vector_map_info),
6822 GFP_KERNEL);
James Smart7bb03bb2013-04-17 20:19:16 -04006823 if (!phba->sli4_hba.cpu_map) {
6824 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6825 "3327 Failed allocate memory for msi-x "
6826 "interrupt vector mapping\n");
6827 rc = -ENOMEM;
James Smart895427b2017-02-12 13:52:30 -08006828 goto out_free_hba_eq_hdl;
James Smart7bb03bb2013-04-17 20:19:16 -04006829 }
James Smartb246de12013-05-31 17:03:07 -04006830
James Smart32517fc2019-01-28 11:14:33 -08006831 phba->sli4_hba.eq_info = alloc_percpu(struct lpfc_eq_intr_info);
6832 if (!phba->sli4_hba.eq_info) {
6833 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6834 "3321 Failed allocation for per_cpu stats\n");
6835 rc = -ENOMEM;
6836 goto out_free_hba_cpu_map;
6837 }
James Smart912e3ac2011-05-24 11:42:11 -04006838 /*
6839 * Enable sr-iov virtual functions if supported and configured
6840 * through the module parameter.
6841 */
6842 if (phba->cfg_sriov_nr_virtfn > 0) {
6843 rc = lpfc_sli_probe_sriov_nr_virtfn(phba,
6844 phba->cfg_sriov_nr_virtfn);
6845 if (rc) {
6846 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6847 "3020 Requested number of SR-IOV "
6848 "virtual functions (%d) is not "
6849 "supported\n",
6850 phba->cfg_sriov_nr_virtfn);
6851 phba->cfg_sriov_nr_virtfn = 0;
6852 }
6853 }
6854
James Smart5248a742011-07-22 18:37:06 -04006855 return 0;
James Smartda0436e2009-05-22 14:51:39 -04006856
James Smart32517fc2019-01-28 11:14:33 -08006857out_free_hba_cpu_map:
6858 kfree(phba->sli4_hba.cpu_map);
James Smart895427b2017-02-12 13:52:30 -08006859out_free_hba_eq_hdl:
6860 kfree(phba->sli4_hba.hba_eq_hdl);
James Smart0c9ab6f2010-02-26 14:15:57 -05006861out_free_fcf_rr_bmask:
6862 kfree(phba->fcf.fcf_rr_bmask);
James Smartda0436e2009-05-22 14:51:39 -04006863out_remove_rpi_hdrs:
6864 lpfc_sli4_remove_rpi_hdrs(phba);
6865out_free_active_sgl:
6866 lpfc_free_active_sgl(phba);
James Smartda0436e2009-05-22 14:51:39 -04006867out_destroy_cq_event_pool:
6868 lpfc_sli4_cq_event_pool_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04006869out_free_bsmbx:
6870 lpfc_destroy_bootstrap_mbox(phba);
6871out_free_mem:
6872 lpfc_mem_free(phba);
6873 return rc;
6874}
6875
6876/**
6877 * lpfc_sli4_driver_resource_unset - Unset drvr internal resources for SLI4 dev
6878 * @phba: pointer to lpfc hba data structure.
6879 *
6880 * This routine is invoked to unset the driver internal resources set up
6881 * specific for supporting the SLI-4 HBA device it attached to.
6882 **/
6883static void
6884lpfc_sli4_driver_resource_unset(struct lpfc_hba *phba)
6885{
6886 struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
6887
James Smart32517fc2019-01-28 11:14:33 -08006888 free_percpu(phba->sli4_hba.eq_info);
6889
James Smart7bb03bb2013-04-17 20:19:16 -04006890 /* Free memory allocated for msi-x interrupt vector to CPU mapping */
6891 kfree(phba->sli4_hba.cpu_map);
James Smart222e9232019-01-28 11:14:35 -08006892 phba->sli4_hba.num_possible_cpu = 0;
James Smart7bb03bb2013-04-17 20:19:16 -04006893 phba->sli4_hba.num_present_cpu = 0;
James Smart76fd07a2014-02-20 09:57:18 -05006894 phba->sli4_hba.curr_disp_cpu = 0;
James Smart7bb03bb2013-04-17 20:19:16 -04006895
James Smartda0436e2009-05-22 14:51:39 -04006896 /* Free memory allocated for fast-path work queue handles */
James Smart895427b2017-02-12 13:52:30 -08006897 kfree(phba->sli4_hba.hba_eq_hdl);
James Smartda0436e2009-05-22 14:51:39 -04006898
6899 /* Free the allocated rpi headers. */
6900 lpfc_sli4_remove_rpi_hdrs(phba);
James Smartd11e31d2009-06-10 17:23:06 -04006901 lpfc_sli4_remove_rpis(phba);
James Smartda0436e2009-05-22 14:51:39 -04006902
James Smart0c9ab6f2010-02-26 14:15:57 -05006903 /* Free eligible FCF index bmask */
6904 kfree(phba->fcf.fcf_rr_bmask);
6905
James Smartda0436e2009-05-22 14:51:39 -04006906 /* Free the ELS sgl list */
6907 lpfc_free_active_sgl(phba);
James Smart8a9d2e82012-05-09 21:16:12 -04006908 lpfc_free_els_sgl_list(phba);
James Smartf358dd02017-02-12 13:52:34 -08006909 lpfc_free_nvmet_sgl_list(phba);
James Smartda0436e2009-05-22 14:51:39 -04006910
James Smartda0436e2009-05-22 14:51:39 -04006911 /* Free the completion queue EQ event pool */
6912 lpfc_sli4_cq_event_release_all(phba);
6913 lpfc_sli4_cq_event_pool_destroy(phba);
6914
James Smart6d368e52011-05-24 11:44:12 -04006915 /* Release resource identifiers. */
6916 lpfc_sli4_dealloc_resource_identifiers(phba);
6917
James Smartda0436e2009-05-22 14:51:39 -04006918 /* Free the bsmbx region. */
6919 lpfc_destroy_bootstrap_mbox(phba);
6920
6921 /* Free the SLI Layer memory with SLI4 HBAs */
6922 lpfc_mem_free_all(phba);
6923
6924 /* Free the current connect table */
6925 list_for_each_entry_safe(conn_entry, next_conn_entry,
James Smart4d9ab992009-10-02 15:16:39 -04006926 &phba->fcf_conn_rec_list, list) {
6927 list_del_init(&conn_entry->list);
James Smartda0436e2009-05-22 14:51:39 -04006928 kfree(conn_entry);
James Smart4d9ab992009-10-02 15:16:39 -04006929 }
James Smartda0436e2009-05-22 14:51:39 -04006930
6931 return;
6932}
6933
6934/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006935 * lpfc_init_api_table_setup - Set up init api function jump table
James Smart3772a992009-05-22 14:50:54 -04006936 * @phba: The hba struct for which this call is being executed.
6937 * @dev_grp: The HBA PCI-Device group number.
6938 *
6939 * This routine sets up the device INIT interface API function jump table
6940 * in @phba struct.
6941 *
6942 * Returns: 0 - success, -ENODEV - failure.
6943 **/
6944int
6945lpfc_init_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
6946{
James Smart84d1b002010-02-12 14:42:33 -05006947 phba->lpfc_hba_init_link = lpfc_hba_init_link;
6948 phba->lpfc_hba_down_link = lpfc_hba_down_link;
James Smart7f860592011-03-11 16:05:52 -05006949 phba->lpfc_selective_reset = lpfc_selective_reset;
James Smart3772a992009-05-22 14:50:54 -04006950 switch (dev_grp) {
6951 case LPFC_PCI_DEV_LP:
6952 phba->lpfc_hba_down_post = lpfc_hba_down_post_s3;
6953 phba->lpfc_handle_eratt = lpfc_handle_eratt_s3;
6954 phba->lpfc_stop_port = lpfc_stop_port_s3;
6955 break;
James Smartda0436e2009-05-22 14:51:39 -04006956 case LPFC_PCI_DEV_OC:
6957 phba->lpfc_hba_down_post = lpfc_hba_down_post_s4;
6958 phba->lpfc_handle_eratt = lpfc_handle_eratt_s4;
6959 phba->lpfc_stop_port = lpfc_stop_port_s4;
6960 break;
James Smart3772a992009-05-22 14:50:54 -04006961 default:
6962 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6963 "1431 Invalid HBA PCI-device group: 0x%x\n",
6964 dev_grp);
6965 return -ENODEV;
6966 break;
6967 }
6968 return 0;
6969}
6970
6971/**
James Smart3772a992009-05-22 14:50:54 -04006972 * lpfc_setup_driver_resource_phase2 - Phase2 setup driver internal resources.
6973 * @phba: pointer to lpfc hba data structure.
6974 *
6975 * This routine is invoked to set up the driver internal resources after the
6976 * device specific resource setup to support the HBA device it attached to.
6977 *
6978 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006979 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04006980 * other values - error
6981 **/
6982static int
6983lpfc_setup_driver_resource_phase2(struct lpfc_hba *phba)
6984{
6985 int error;
6986
6987 /* Startup the kernel thread for this host adapter. */
6988 phba->worker_thread = kthread_run(lpfc_do_work, phba,
6989 "lpfc_worker_%d", phba->brd_no);
6990 if (IS_ERR(phba->worker_thread)) {
6991 error = PTR_ERR(phba->worker_thread);
6992 return error;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05006993 }
6994
James Smart0cdb84e2018-04-09 14:24:26 -07006995 /* The lpfc_wq workqueue for deferred irq use, is only used for SLI4 */
6996 if (phba->sli_rev == LPFC_SLI_REV4)
6997 phba->wq = alloc_workqueue("lpfc_wq", WQ_MEM_RECLAIM, 0);
6998 else
6999 phba->wq = NULL;
Dick Kennedyf485c182017-09-29 17:34:34 -07007000
James Smart3772a992009-05-22 14:50:54 -04007001 return 0;
7002}
7003
7004/**
7005 * lpfc_unset_driver_resource_phase2 - Phase2 unset driver internal resources.
7006 * @phba: pointer to lpfc hba data structure.
7007 *
7008 * This routine is invoked to unset the driver internal resources set up after
7009 * the device specific resource setup for supporting the HBA device it
7010 * attached to.
7011 **/
7012static void
7013lpfc_unset_driver_resource_phase2(struct lpfc_hba *phba)
7014{
Dick Kennedyf485c182017-09-29 17:34:34 -07007015 if (phba->wq) {
7016 flush_workqueue(phba->wq);
7017 destroy_workqueue(phba->wq);
7018 phba->wq = NULL;
7019 }
7020
James Smart3772a992009-05-22 14:50:54 -04007021 /* Stop kernel worker thread */
James Smart0cdb84e2018-04-09 14:24:26 -07007022 if (phba->worker_thread)
7023 kthread_stop(phba->worker_thread);
James Smart3772a992009-05-22 14:50:54 -04007024}
7025
7026/**
7027 * lpfc_free_iocb_list - Free iocb list.
7028 * @phba: pointer to lpfc hba data structure.
7029 *
7030 * This routine is invoked to free the driver's IOCB list and memory.
7031 **/
James Smart6c621a22017-05-15 15:20:45 -07007032void
James Smart3772a992009-05-22 14:50:54 -04007033lpfc_free_iocb_list(struct lpfc_hba *phba)
7034{
7035 struct lpfc_iocbq *iocbq_entry = NULL, *iocbq_next = NULL;
7036
7037 spin_lock_irq(&phba->hbalock);
7038 list_for_each_entry_safe(iocbq_entry, iocbq_next,
7039 &phba->lpfc_iocb_list, list) {
7040 list_del(&iocbq_entry->list);
7041 kfree(iocbq_entry);
7042 phba->total_iocbq_bufs--;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05007043 }
James Smart3772a992009-05-22 14:50:54 -04007044 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007045
James Smart3772a992009-05-22 14:50:54 -04007046 return;
7047}
dea31012005-04-17 16:05:31 -05007048
James Smart3772a992009-05-22 14:50:54 -04007049/**
7050 * lpfc_init_iocb_list - Allocate and initialize iocb list.
7051 * @phba: pointer to lpfc hba data structure.
7052 *
7053 * This routine is invoked to allocate and initizlize the driver's IOCB
7054 * list and set up the IOCB tag array accordingly.
7055 *
7056 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007057 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04007058 * other values - error
7059 **/
James Smart6c621a22017-05-15 15:20:45 -07007060int
James Smart3772a992009-05-22 14:50:54 -04007061lpfc_init_iocb_list(struct lpfc_hba *phba, int iocb_count)
7062{
7063 struct lpfc_iocbq *iocbq_entry = NULL;
7064 uint16_t iotag;
7065 int i;
dea31012005-04-17 16:05:31 -05007066
7067 /* Initialize and populate the iocb list per host. */
7068 INIT_LIST_HEAD(&phba->lpfc_iocb_list);
James Smart3772a992009-05-22 14:50:54 -04007069 for (i = 0; i < iocb_count; i++) {
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07007070 iocbq_entry = kzalloc(sizeof(struct lpfc_iocbq), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05007071 if (iocbq_entry == NULL) {
7072 printk(KERN_ERR "%s: only allocated %d iocbs of "
7073 "expected %d count. Unloading driver.\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07007074 __func__, i, LPFC_IOCB_LIST_CNT);
dea31012005-04-17 16:05:31 -05007075 goto out_free_iocbq;
7076 }
7077
James Bottomley604a3e32005-10-29 10:28:33 -05007078 iotag = lpfc_sli_next_iotag(phba, iocbq_entry);
7079 if (iotag == 0) {
James Smart3772a992009-05-22 14:50:54 -04007080 kfree(iocbq_entry);
James Bottomley604a3e32005-10-29 10:28:33 -05007081 printk(KERN_ERR "%s: failed to allocate IOTAG. "
James Smart3772a992009-05-22 14:50:54 -04007082 "Unloading driver.\n", __func__);
James Bottomley604a3e32005-10-29 10:28:33 -05007083 goto out_free_iocbq;
7084 }
James Smart6d368e52011-05-24 11:44:12 -04007085 iocbq_entry->sli4_lxritag = NO_XRI;
James Smart3772a992009-05-22 14:50:54 -04007086 iocbq_entry->sli4_xritag = NO_XRI;
James Smart2e0fef82007-06-17 19:56:36 -05007087
7088 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007089 list_add(&iocbq_entry->list, &phba->lpfc_iocb_list);
7090 phba->total_iocbq_bufs++;
James Smart2e0fef82007-06-17 19:56:36 -05007091 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007092 }
7093
James Smart3772a992009-05-22 14:50:54 -04007094 return 0;
7095
7096out_free_iocbq:
7097 lpfc_free_iocb_list(phba);
7098
7099 return -ENOMEM;
7100}
7101
7102/**
James Smart8a9d2e82012-05-09 21:16:12 -04007103 * lpfc_free_sgl_list - Free a given sgl list.
James Smartda0436e2009-05-22 14:51:39 -04007104 * @phba: pointer to lpfc hba data structure.
James Smart8a9d2e82012-05-09 21:16:12 -04007105 * @sglq_list: pointer to the head of sgl list.
James Smartda0436e2009-05-22 14:51:39 -04007106 *
James Smart8a9d2e82012-05-09 21:16:12 -04007107 * This routine is invoked to free a give sgl list and memory.
James Smartda0436e2009-05-22 14:51:39 -04007108 **/
James Smart8a9d2e82012-05-09 21:16:12 -04007109void
7110lpfc_free_sgl_list(struct lpfc_hba *phba, struct list_head *sglq_list)
James Smartda0436e2009-05-22 14:51:39 -04007111{
7112 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
James Smart8a9d2e82012-05-09 21:16:12 -04007113
7114 list_for_each_entry_safe(sglq_entry, sglq_next, sglq_list, list) {
7115 list_del(&sglq_entry->list);
7116 lpfc_mbuf_free(phba, sglq_entry->virt, sglq_entry->phys);
7117 kfree(sglq_entry);
7118 }
7119}
7120
7121/**
7122 * lpfc_free_els_sgl_list - Free els sgl list.
7123 * @phba: pointer to lpfc hba data structure.
7124 *
7125 * This routine is invoked to free the driver's els sgl list and memory.
7126 **/
7127static void
7128lpfc_free_els_sgl_list(struct lpfc_hba *phba)
7129{
James Smartda0436e2009-05-22 14:51:39 -04007130 LIST_HEAD(sglq_list);
James Smartda0436e2009-05-22 14:51:39 -04007131
James Smart8a9d2e82012-05-09 21:16:12 -04007132 /* Retrieve all els sgls from driver list */
James Smartda0436e2009-05-22 14:51:39 -04007133 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -08007134 spin_lock(&phba->sli4_hba.sgl_list_lock);
7135 list_splice_init(&phba->sli4_hba.lpfc_els_sgl_list, &sglq_list);
7136 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smartda0436e2009-05-22 14:51:39 -04007137 spin_unlock_irq(&phba->hbalock);
7138
James Smart8a9d2e82012-05-09 21:16:12 -04007139 /* Now free the sgl list */
7140 lpfc_free_sgl_list(phba, &sglq_list);
James Smartda0436e2009-05-22 14:51:39 -04007141}
7142
7143/**
James Smartf358dd02017-02-12 13:52:34 -08007144 * lpfc_free_nvmet_sgl_list - Free nvmet sgl list.
7145 * @phba: pointer to lpfc hba data structure.
7146 *
7147 * This routine is invoked to free the driver's nvmet sgl list and memory.
7148 **/
7149static void
7150lpfc_free_nvmet_sgl_list(struct lpfc_hba *phba)
7151{
7152 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
7153 LIST_HEAD(sglq_list);
7154
7155 /* Retrieve all nvmet sgls from driver list */
7156 spin_lock_irq(&phba->hbalock);
7157 spin_lock(&phba->sli4_hba.sgl_list_lock);
7158 list_splice_init(&phba->sli4_hba.lpfc_nvmet_sgl_list, &sglq_list);
7159 spin_unlock(&phba->sli4_hba.sgl_list_lock);
7160 spin_unlock_irq(&phba->hbalock);
7161
7162 /* Now free the sgl list */
7163 list_for_each_entry_safe(sglq_entry, sglq_next, &sglq_list, list) {
7164 list_del(&sglq_entry->list);
7165 lpfc_nvmet_buf_free(phba, sglq_entry->virt, sglq_entry->phys);
7166 kfree(sglq_entry);
7167 }
Dick Kennedy4b40d022017-08-23 16:55:38 -07007168
7169 /* Update the nvmet_xri_cnt to reflect no current sgls.
7170 * The next initialization cycle sets the count and allocates
7171 * the sgls over again.
7172 */
7173 phba->sli4_hba.nvmet_xri_cnt = 0;
James Smartf358dd02017-02-12 13:52:34 -08007174}
7175
7176/**
James Smartda0436e2009-05-22 14:51:39 -04007177 * lpfc_init_active_sgl_array - Allocate the buf to track active ELS XRIs.
7178 * @phba: pointer to lpfc hba data structure.
7179 *
7180 * This routine is invoked to allocate the driver's active sgl memory.
7181 * This array will hold the sglq_entry's for active IOs.
7182 **/
7183static int
7184lpfc_init_active_sgl_array(struct lpfc_hba *phba)
7185{
7186 int size;
7187 size = sizeof(struct lpfc_sglq *);
7188 size *= phba->sli4_hba.max_cfg_param.max_xri;
7189
7190 phba->sli4_hba.lpfc_sglq_active_list =
7191 kzalloc(size, GFP_KERNEL);
7192 if (!phba->sli4_hba.lpfc_sglq_active_list)
7193 return -ENOMEM;
7194 return 0;
7195}
7196
7197/**
7198 * lpfc_free_active_sgl - Free the buf that tracks active ELS XRIs.
7199 * @phba: pointer to lpfc hba data structure.
7200 *
7201 * This routine is invoked to walk through the array of active sglq entries
7202 * and free all of the resources.
7203 * This is just a place holder for now.
7204 **/
7205static void
7206lpfc_free_active_sgl(struct lpfc_hba *phba)
7207{
7208 kfree(phba->sli4_hba.lpfc_sglq_active_list);
7209}
7210
7211/**
7212 * lpfc_init_sgl_list - Allocate and initialize sgl list.
7213 * @phba: pointer to lpfc hba data structure.
7214 *
7215 * This routine is invoked to allocate and initizlize the driver's sgl
7216 * list and set up the sgl xritag tag array accordingly.
7217 *
James Smartda0436e2009-05-22 14:51:39 -04007218 **/
James Smart8a9d2e82012-05-09 21:16:12 -04007219static void
James Smartda0436e2009-05-22 14:51:39 -04007220lpfc_init_sgl_list(struct lpfc_hba *phba)
7221{
James Smartda0436e2009-05-22 14:51:39 -04007222 /* Initialize and populate the sglq list per host/VF. */
James Smart895427b2017-02-12 13:52:30 -08007223 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_els_sgl_list);
James Smartda0436e2009-05-22 14:51:39 -04007224 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_els_sgl_list);
James Smartf358dd02017-02-12 13:52:34 -08007225 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_nvmet_sgl_list);
James Smart86c67372017-04-21 16:05:04 -07007226 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_nvmet_ctx_list);
James Smartda0436e2009-05-22 14:51:39 -04007227
James Smart8a9d2e82012-05-09 21:16:12 -04007228 /* els xri-sgl book keeping */
7229 phba->sli4_hba.els_xri_cnt = 0;
James Smartda0436e2009-05-22 14:51:39 -04007230
James Smart895427b2017-02-12 13:52:30 -08007231 /* nvme xri-buffer book keeping */
James Smart5e5b5112019-01-28 11:14:22 -08007232 phba->sli4_hba.io_xri_cnt = 0;
James Smartda0436e2009-05-22 14:51:39 -04007233}
7234
7235/**
7236 * lpfc_sli4_init_rpi_hdrs - Post the rpi header memory region to the port
7237 * @phba: pointer to lpfc hba data structure.
7238 *
7239 * This routine is invoked to post rpi header templates to the
James Smart88a2cfb2011-07-22 18:36:33 -04007240 * port for those SLI4 ports that do not support extents. This routine
James Smartda0436e2009-05-22 14:51:39 -04007241 * posts a PAGE_SIZE memory region to the port to hold up to
James Smart88a2cfb2011-07-22 18:36:33 -04007242 * PAGE_SIZE modulo 64 rpi context headers. This is an initialization routine
7243 * and should be called only when interrupts are disabled.
James Smartda0436e2009-05-22 14:51:39 -04007244 *
7245 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007246 * 0 - successful
James Smart88a2cfb2011-07-22 18:36:33 -04007247 * -ERROR - otherwise.
James Smartda0436e2009-05-22 14:51:39 -04007248 **/
7249int
7250lpfc_sli4_init_rpi_hdrs(struct lpfc_hba *phba)
7251{
7252 int rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04007253 struct lpfc_rpi_hdr *rpi_hdr;
7254
7255 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_hdr_list);
James Smartff78d8f2011-12-13 13:21:35 -05007256 if (!phba->sli4_hba.rpi_hdrs_in_use)
James Smart6d368e52011-05-24 11:44:12 -04007257 return rc;
James Smart6d368e52011-05-24 11:44:12 -04007258 if (phba->sli4_hba.extents_in_use)
7259 return -EIO;
James Smartda0436e2009-05-22 14:51:39 -04007260
7261 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
7262 if (!rpi_hdr) {
7263 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7264 "0391 Error during rpi post operation\n");
7265 lpfc_sli4_remove_rpis(phba);
7266 rc = -ENODEV;
7267 }
7268
7269 return rc;
7270}
7271
7272/**
7273 * lpfc_sli4_create_rpi_hdr - Allocate an rpi header memory region
7274 * @phba: pointer to lpfc hba data structure.
7275 *
7276 * This routine is invoked to allocate a single 4KB memory region to
7277 * support rpis and stores them in the phba. This single region
7278 * provides support for up to 64 rpis. The region is used globally
7279 * by the device.
7280 *
7281 * Returns:
7282 * A valid rpi hdr on success.
7283 * A NULL pointer on any failure.
7284 **/
7285struct lpfc_rpi_hdr *
7286lpfc_sli4_create_rpi_hdr(struct lpfc_hba *phba)
7287{
7288 uint16_t rpi_limit, curr_rpi_range;
7289 struct lpfc_dmabuf *dmabuf;
7290 struct lpfc_rpi_hdr *rpi_hdr;
7291
James Smart6d368e52011-05-24 11:44:12 -04007292 /*
7293 * If the SLI4 port supports extents, posting the rpi header isn't
7294 * required. Set the expected maximum count and let the actual value
7295 * get set when extents are fully allocated.
7296 */
7297 if (!phba->sli4_hba.rpi_hdrs_in_use)
7298 return NULL;
7299 if (phba->sli4_hba.extents_in_use)
7300 return NULL;
7301
7302 /* The limit on the logical index is just the max_rpi count. */
James Smart845d9e82017-05-15 15:20:38 -07007303 rpi_limit = phba->sli4_hba.max_cfg_param.max_rpi;
James Smartda0436e2009-05-22 14:51:39 -04007304
7305 spin_lock_irq(&phba->hbalock);
James Smart6d368e52011-05-24 11:44:12 -04007306 /*
7307 * Establish the starting RPI in this header block. The starting
7308 * rpi is normalized to a zero base because the physical rpi is
7309 * port based.
7310 */
James Smart97f2ecf2012-03-01 22:35:23 -05007311 curr_rpi_range = phba->sli4_hba.next_rpi;
James Smartda0436e2009-05-22 14:51:39 -04007312 spin_unlock_irq(&phba->hbalock);
7313
James Smart845d9e82017-05-15 15:20:38 -07007314 /* Reached full RPI range */
7315 if (curr_rpi_range == rpi_limit)
James Smart6d368e52011-05-24 11:44:12 -04007316 return NULL;
James Smart845d9e82017-05-15 15:20:38 -07007317
James Smartda0436e2009-05-22 14:51:39 -04007318 /*
7319 * First allocate the protocol header region for the port. The
7320 * port expects a 4KB DMA-mapped memory region that is 4K aligned.
7321 */
7322 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
7323 if (!dmabuf)
7324 return NULL;
7325
Luis Chamberlain750afb02019-01-04 09:23:09 +01007326 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
7327 LPFC_HDR_TEMPLATE_SIZE,
7328 &dmabuf->phys, GFP_KERNEL);
James Smartda0436e2009-05-22 14:51:39 -04007329 if (!dmabuf->virt) {
7330 rpi_hdr = NULL;
7331 goto err_free_dmabuf;
7332 }
7333
James Smartda0436e2009-05-22 14:51:39 -04007334 if (!IS_ALIGNED(dmabuf->phys, LPFC_HDR_TEMPLATE_SIZE)) {
7335 rpi_hdr = NULL;
7336 goto err_free_coherent;
7337 }
7338
7339 /* Save the rpi header data for cleanup later. */
7340 rpi_hdr = kzalloc(sizeof(struct lpfc_rpi_hdr), GFP_KERNEL);
7341 if (!rpi_hdr)
7342 goto err_free_coherent;
7343
7344 rpi_hdr->dmabuf = dmabuf;
7345 rpi_hdr->len = LPFC_HDR_TEMPLATE_SIZE;
7346 rpi_hdr->page_count = 1;
7347 spin_lock_irq(&phba->hbalock);
James Smart6d368e52011-05-24 11:44:12 -04007348
7349 /* The rpi_hdr stores the logical index only. */
7350 rpi_hdr->start_rpi = curr_rpi_range;
James Smart845d9e82017-05-15 15:20:38 -07007351 rpi_hdr->next_rpi = phba->sli4_hba.next_rpi + LPFC_RPI_HDR_COUNT;
James Smartda0436e2009-05-22 14:51:39 -04007352 list_add_tail(&rpi_hdr->list, &phba->sli4_hba.lpfc_rpi_hdr_list);
7353
James Smartda0436e2009-05-22 14:51:39 -04007354 spin_unlock_irq(&phba->hbalock);
7355 return rpi_hdr;
7356
7357 err_free_coherent:
7358 dma_free_coherent(&phba->pcidev->dev, LPFC_HDR_TEMPLATE_SIZE,
7359 dmabuf->virt, dmabuf->phys);
7360 err_free_dmabuf:
7361 kfree(dmabuf);
7362 return NULL;
7363}
7364
7365/**
7366 * lpfc_sli4_remove_rpi_hdrs - Remove all rpi header memory regions
7367 * @phba: pointer to lpfc hba data structure.
7368 *
7369 * This routine is invoked to remove all memory resources allocated
James Smart6d368e52011-05-24 11:44:12 -04007370 * to support rpis for SLI4 ports not supporting extents. This routine
7371 * presumes the caller has released all rpis consumed by fabric or port
7372 * logins and is prepared to have the header pages removed.
James Smartda0436e2009-05-22 14:51:39 -04007373 **/
7374void
7375lpfc_sli4_remove_rpi_hdrs(struct lpfc_hba *phba)
7376{
7377 struct lpfc_rpi_hdr *rpi_hdr, *next_rpi_hdr;
7378
James Smart6d368e52011-05-24 11:44:12 -04007379 if (!phba->sli4_hba.rpi_hdrs_in_use)
7380 goto exit;
7381
James Smartda0436e2009-05-22 14:51:39 -04007382 list_for_each_entry_safe(rpi_hdr, next_rpi_hdr,
7383 &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
7384 list_del(&rpi_hdr->list);
7385 dma_free_coherent(&phba->pcidev->dev, rpi_hdr->len,
7386 rpi_hdr->dmabuf->virt, rpi_hdr->dmabuf->phys);
7387 kfree(rpi_hdr->dmabuf);
7388 kfree(rpi_hdr);
7389 }
James Smart6d368e52011-05-24 11:44:12 -04007390 exit:
7391 /* There are no rpis available to the port now. */
7392 phba->sli4_hba.next_rpi = 0;
James Smartda0436e2009-05-22 14:51:39 -04007393}
7394
7395/**
James Smart3772a992009-05-22 14:50:54 -04007396 * lpfc_hba_alloc - Allocate driver hba data structure for a device.
7397 * @pdev: pointer to pci device data structure.
7398 *
7399 * This routine is invoked to allocate the driver hba data structure for an
7400 * HBA device. If the allocation is successful, the phba reference to the
7401 * PCI device data structure is set.
7402 *
7403 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007404 * pointer to @phba - successful
James Smart3772a992009-05-22 14:50:54 -04007405 * NULL - error
7406 **/
7407static struct lpfc_hba *
7408lpfc_hba_alloc(struct pci_dev *pdev)
7409{
7410 struct lpfc_hba *phba;
7411
7412 /* Allocate memory for HBA structure */
7413 phba = kzalloc(sizeof(struct lpfc_hba), GFP_KERNEL);
7414 if (!phba) {
Jiri Slabye34ccdf2009-07-13 23:25:54 +02007415 dev_err(&pdev->dev, "failed to allocate hba struct\n");
James Smart3772a992009-05-22 14:50:54 -04007416 return NULL;
7417 }
7418
7419 /* Set reference to PCI device in HBA structure */
7420 phba->pcidev = pdev;
7421
7422 /* Assign an unused board number */
7423 phba->brd_no = lpfc_get_instance();
7424 if (phba->brd_no < 0) {
7425 kfree(phba);
7426 return NULL;
7427 }
James Smart65791f12016-07-06 12:35:56 -07007428 phba->eratt_poll_interval = LPFC_ERATT_POLL_INTERVAL;
James Smart3772a992009-05-22 14:50:54 -04007429
James Smart4fede782010-01-26 23:08:55 -05007430 spin_lock_init(&phba->ct_ev_lock);
James Smartf1c3b0f2009-07-19 10:01:32 -04007431 INIT_LIST_HEAD(&phba->ct_ev_waiters);
7432
James Smart3772a992009-05-22 14:50:54 -04007433 return phba;
7434}
7435
7436/**
7437 * lpfc_hba_free - Free driver hba data structure with a device.
7438 * @phba: pointer to lpfc hba data structure.
7439 *
7440 * This routine is invoked to free the driver hba data structure with an
7441 * HBA device.
7442 **/
7443static void
7444lpfc_hba_free(struct lpfc_hba *phba)
7445{
James Smart5e5b5112019-01-28 11:14:22 -08007446 if (phba->sli_rev == LPFC_SLI_REV4)
7447 kfree(phba->sli4_hba.hdwq);
7448
James Smart3772a992009-05-22 14:50:54 -04007449 /* Release the driver assigned board number */
7450 idr_remove(&lpfc_hba_index, phba->brd_no);
7451
James Smart895427b2017-02-12 13:52:30 -08007452 /* Free memory allocated with sli3 rings */
7453 kfree(phba->sli.sli3_ring);
7454 phba->sli.sli3_ring = NULL;
James Smart2a76a282012-08-03 12:35:54 -04007455
James Smart3772a992009-05-22 14:50:54 -04007456 kfree(phba);
7457 return;
7458}
7459
7460/**
7461 * lpfc_create_shost - Create hba physical port with associated scsi host.
7462 * @phba: pointer to lpfc hba data structure.
7463 *
7464 * This routine is invoked to create HBA physical port and associate a SCSI
7465 * host with it.
7466 *
7467 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007468 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04007469 * other values - error
7470 **/
7471static int
7472lpfc_create_shost(struct lpfc_hba *phba)
7473{
7474 struct lpfc_vport *vport;
7475 struct Scsi_Host *shost;
7476
7477 /* Initialize HBA FC structure */
dea31012005-04-17 16:05:31 -05007478 phba->fc_edtov = FF_DEF_EDTOV;
7479 phba->fc_ratov = FF_DEF_RATOV;
7480 phba->fc_altov = FF_DEF_ALTOV;
7481 phba->fc_arbtov = FF_DEF_ARBTOV;
7482
James Smartd7c47992010-06-08 18:31:54 -04007483 atomic_set(&phba->sdev_cnt, 0);
James Smart3de2a652007-08-02 11:09:59 -04007484 vport = lpfc_create_port(phba, phba->brd_no, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05007485 if (!vport)
James Smart3772a992009-05-22 14:50:54 -04007486 return -ENODEV;
James Smart2e0fef82007-06-17 19:56:36 -05007487
7488 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05007489 phba->pport = vport;
James Smart2ea259e2017-02-12 13:52:27 -08007490
James Smartf358dd02017-02-12 13:52:34 -08007491 if (phba->nvmet_support) {
7492 /* Only 1 vport (pport) will support NVME target */
7493 if (phba->txrdy_payload_pool == NULL) {
Romain Perier771db5c2017-07-06 10:13:05 +02007494 phba->txrdy_payload_pool = dma_pool_create(
7495 "txrdy_pool", &phba->pcidev->dev,
James Smartf358dd02017-02-12 13:52:34 -08007496 TXRDY_PAYLOAD_LEN, 16, 0);
7497 if (phba->txrdy_payload_pool) {
7498 phba->targetport = NULL;
7499 phba->cfg_enable_fc4_type = LPFC_ENABLE_NVME;
7500 lpfc_printf_log(phba, KERN_INFO,
7501 LOG_INIT | LOG_NVME_DISC,
7502 "6076 NVME Target Found\n");
7503 }
7504 }
7505 }
7506
James Smart858c9f62007-06-17 19:56:39 -05007507 lpfc_debugfs_initialize(vport);
James Smart3772a992009-05-22 14:50:54 -04007508 /* Put reference to SCSI host to driver's device private data */
7509 pci_set_drvdata(phba->pcidev, shost);
James Smart2e0fef82007-06-17 19:56:36 -05007510
James Smart4258e982015-12-16 18:11:58 -05007511 /*
7512 * At this point we are fully registered with PSA. In addition,
7513 * any initial discovery should be completed.
7514 */
7515 vport->load_flag |= FC_ALLOW_FDMI;
James Smart8663cbb2016-03-31 14:12:33 -07007516 if (phba->cfg_enable_SmartSAN ||
7517 (phba->cfg_fdmi_on == LPFC_FDMI_SUPPORT)) {
James Smart4258e982015-12-16 18:11:58 -05007518
7519 /* Setup appropriate attribute masks */
7520 vport->fdmi_hba_mask = LPFC_FDMI2_HBA_ATTR;
James Smart8663cbb2016-03-31 14:12:33 -07007521 if (phba->cfg_enable_SmartSAN)
James Smart4258e982015-12-16 18:11:58 -05007522 vport->fdmi_port_mask = LPFC_FDMI2_SMART_ATTR;
7523 else
7524 vport->fdmi_port_mask = LPFC_FDMI2_PORT_ATTR;
7525 }
James Smart3772a992009-05-22 14:50:54 -04007526 return 0;
7527}
dea31012005-04-17 16:05:31 -05007528
James Smart3772a992009-05-22 14:50:54 -04007529/**
7530 * lpfc_destroy_shost - Destroy hba physical port with associated scsi host.
7531 * @phba: pointer to lpfc hba data structure.
7532 *
7533 * This routine is invoked to destroy HBA physical port and the associated
7534 * SCSI host.
7535 **/
7536static void
7537lpfc_destroy_shost(struct lpfc_hba *phba)
7538{
7539 struct lpfc_vport *vport = phba->pport;
James Smart93996272008-08-24 21:50:30 -04007540
James Smart3772a992009-05-22 14:50:54 -04007541 /* Destroy physical port that associated with the SCSI host */
7542 destroy_port(vport);
7543
7544 return;
7545}
7546
7547/**
7548 * lpfc_setup_bg - Setup Block guard structures and debug areas.
7549 * @phba: pointer to lpfc hba data structure.
7550 * @shost: the shost to be used to detect Block guard settings.
7551 *
7552 * This routine sets up the local Block guard protocol settings for @shost.
7553 * This routine also allocates memory for debugging bg buffers.
7554 **/
7555static void
7556lpfc_setup_bg(struct lpfc_hba *phba, struct Scsi_Host *shost)
7557{
James Smartbbeb79b2012-06-12 13:54:27 -04007558 uint32_t old_mask;
7559 uint32_t old_guard;
7560
James Smart3772a992009-05-22 14:50:54 -04007561 int pagecnt = 10;
James Smartb3b98b72016-10-13 15:06:06 -07007562 if (phba->cfg_prot_mask && phba->cfg_prot_guard) {
James Smart3772a992009-05-22 14:50:54 -04007563 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7564 "1478 Registering BlockGuard with the "
7565 "SCSI layer\n");
James Smartbbeb79b2012-06-12 13:54:27 -04007566
James Smartb3b98b72016-10-13 15:06:06 -07007567 old_mask = phba->cfg_prot_mask;
7568 old_guard = phba->cfg_prot_guard;
James Smartbbeb79b2012-06-12 13:54:27 -04007569
7570 /* Only allow supported values */
James Smartb3b98b72016-10-13 15:06:06 -07007571 phba->cfg_prot_mask &= (SHOST_DIF_TYPE1_PROTECTION |
James Smartbbeb79b2012-06-12 13:54:27 -04007572 SHOST_DIX_TYPE0_PROTECTION |
7573 SHOST_DIX_TYPE1_PROTECTION);
James Smartb3b98b72016-10-13 15:06:06 -07007574 phba->cfg_prot_guard &= (SHOST_DIX_GUARD_IP |
7575 SHOST_DIX_GUARD_CRC);
James Smartbbeb79b2012-06-12 13:54:27 -04007576
7577 /* DIF Type 1 protection for profiles AST1/C1 is end to end */
James Smartb3b98b72016-10-13 15:06:06 -07007578 if (phba->cfg_prot_mask == SHOST_DIX_TYPE1_PROTECTION)
7579 phba->cfg_prot_mask |= SHOST_DIF_TYPE1_PROTECTION;
James Smartbbeb79b2012-06-12 13:54:27 -04007580
James Smartb3b98b72016-10-13 15:06:06 -07007581 if (phba->cfg_prot_mask && phba->cfg_prot_guard) {
7582 if ((old_mask != phba->cfg_prot_mask) ||
7583 (old_guard != phba->cfg_prot_guard))
James Smartbbeb79b2012-06-12 13:54:27 -04007584 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7585 "1475 Registering BlockGuard with the "
7586 "SCSI layer: mask %d guard %d\n",
James Smartb3b98b72016-10-13 15:06:06 -07007587 phba->cfg_prot_mask,
7588 phba->cfg_prot_guard);
James Smartbbeb79b2012-06-12 13:54:27 -04007589
James Smartb3b98b72016-10-13 15:06:06 -07007590 scsi_host_set_prot(shost, phba->cfg_prot_mask);
7591 scsi_host_set_guard(shost, phba->cfg_prot_guard);
James Smartbbeb79b2012-06-12 13:54:27 -04007592 } else
7593 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7594 "1479 Not Registering BlockGuard with the SCSI "
7595 "layer, Bad protection parameters: %d %d\n",
7596 old_mask, old_guard);
James Smart98c9ea52007-10-27 13:37:33 -04007597 }
James Smartbbeb79b2012-06-12 13:54:27 -04007598
James Smart81301a92008-12-04 22:39:46 -05007599 if (!_dump_buf_data) {
James Smart81301a92008-12-04 22:39:46 -05007600 while (pagecnt) {
7601 spin_lock_init(&_dump_buf_lock);
7602 _dump_buf_data =
7603 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
7604 if (_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -04007605 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
7606 "9043 BLKGRD: allocated %d pages for "
James Smart3772a992009-05-22 14:50:54 -04007607 "_dump_buf_data at 0x%p\n",
7608 (1 << pagecnt), _dump_buf_data);
James Smart81301a92008-12-04 22:39:46 -05007609 _dump_buf_data_order = pagecnt;
James Smart3772a992009-05-22 14:50:54 -04007610 memset(_dump_buf_data, 0,
7611 ((1 << PAGE_SHIFT) << pagecnt));
James Smart81301a92008-12-04 22:39:46 -05007612 break;
James Smart3772a992009-05-22 14:50:54 -04007613 } else
James Smart81301a92008-12-04 22:39:46 -05007614 --pagecnt;
James Smart81301a92008-12-04 22:39:46 -05007615 }
James Smart81301a92008-12-04 22:39:46 -05007616 if (!_dump_buf_data_order)
James Smart6a9c52c2009-10-02 15:16:51 -04007617 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
7618 "9044 BLKGRD: ERROR unable to allocate "
James Smart3772a992009-05-22 14:50:54 -04007619 "memory for hexdump\n");
7620 } else
James Smart6a9c52c2009-10-02 15:16:51 -04007621 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
7622 "9045 BLKGRD: already allocated _dump_buf_data=0x%p"
James Smart81301a92008-12-04 22:39:46 -05007623 "\n", _dump_buf_data);
James Smart81301a92008-12-04 22:39:46 -05007624 if (!_dump_buf_dif) {
James Smart81301a92008-12-04 22:39:46 -05007625 while (pagecnt) {
7626 _dump_buf_dif =
7627 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
7628 if (_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -04007629 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
7630 "9046 BLKGRD: allocated %d pages for "
James Smart3772a992009-05-22 14:50:54 -04007631 "_dump_buf_dif at 0x%p\n",
7632 (1 << pagecnt), _dump_buf_dif);
James Smart81301a92008-12-04 22:39:46 -05007633 _dump_buf_dif_order = pagecnt;
James Smart3772a992009-05-22 14:50:54 -04007634 memset(_dump_buf_dif, 0,
7635 ((1 << PAGE_SHIFT) << pagecnt));
James Smart81301a92008-12-04 22:39:46 -05007636 break;
James Smart3772a992009-05-22 14:50:54 -04007637 } else
James Smart81301a92008-12-04 22:39:46 -05007638 --pagecnt;
James Smart81301a92008-12-04 22:39:46 -05007639 }
James Smart81301a92008-12-04 22:39:46 -05007640 if (!_dump_buf_dif_order)
James Smart6a9c52c2009-10-02 15:16:51 -04007641 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
7642 "9047 BLKGRD: ERROR unable to allocate "
James Smart3772a992009-05-22 14:50:54 -04007643 "memory for hexdump\n");
7644 } else
James Smart6a9c52c2009-10-02 15:16:51 -04007645 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
7646 "9048 BLKGRD: already allocated _dump_buf_dif=0x%p\n",
James Smart3772a992009-05-22 14:50:54 -04007647 _dump_buf_dif);
7648}
7649
7650/**
7651 * lpfc_post_init_setup - Perform necessary device post initialization setup.
7652 * @phba: pointer to lpfc hba data structure.
7653 *
7654 * This routine is invoked to perform all the necessary post initialization
7655 * setup for the device.
7656 **/
7657static void
7658lpfc_post_init_setup(struct lpfc_hba *phba)
7659{
7660 struct Scsi_Host *shost;
7661 struct lpfc_adapter_event_header adapter_event;
7662
7663 /* Get the default values for Model Name and Description */
7664 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
7665
7666 /*
7667 * hba setup may have changed the hba_queue_depth so we need to
7668 * adjust the value of can_queue.
7669 */
7670 shost = pci_get_drvdata(phba->pcidev);
7671 shost->can_queue = phba->cfg_hba_queue_depth - 10;
7672 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED)
7673 lpfc_setup_bg(phba, shost);
James Smart858c9f62007-06-17 19:56:39 -05007674
7675 lpfc_host_attrib_init(shost);
7676
James Smart2e0fef82007-06-17 19:56:36 -05007677 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
7678 spin_lock_irq(shost->host_lock);
7679 lpfc_poll_start_timer(phba);
7680 spin_unlock_irq(shost->host_lock);
7681 }
James Smart8f6d98d2006-08-01 07:34:00 -04007682
James Smart93996272008-08-24 21:50:30 -04007683 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7684 "0428 Perform SCSI scan\n");
James Smartea2151b2008-09-07 11:52:10 -04007685 /* Send board arrival event to upper layer */
7686 adapter_event.event_type = FC_REG_ADAPTER_EVENT;
7687 adapter_event.subcategory = LPFC_EVENT_ARRIVAL;
7688 fc_host_post_vendor_event(shost, fc_get_event_number(),
James Smart3772a992009-05-22 14:50:54 -04007689 sizeof(adapter_event),
7690 (char *) &adapter_event,
7691 LPFC_NL_VENDOR_ID);
7692 return;
7693}
7694
7695/**
7696 * lpfc_sli_pci_mem_setup - Setup SLI3 HBA PCI memory space.
7697 * @phba: pointer to lpfc hba data structure.
7698 *
7699 * This routine is invoked to set up the PCI device memory space for device
7700 * with SLI-3 interface spec.
7701 *
7702 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007703 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04007704 * other values - error
7705 **/
7706static int
7707lpfc_sli_pci_mem_setup(struct lpfc_hba *phba)
7708{
Christoph Hellwigf30e1bf2018-10-18 15:10:21 +02007709 struct pci_dev *pdev = phba->pcidev;
James Smart3772a992009-05-22 14:50:54 -04007710 unsigned long bar0map_len, bar2map_len;
7711 int i, hbq_count;
7712 void *ptr;
Hannes Reinecke56de8352019-02-18 08:34:19 +01007713 int error;
James Smart3772a992009-05-22 14:50:54 -04007714
Christoph Hellwigf30e1bf2018-10-18 15:10:21 +02007715 if (!pdev)
Hannes Reinecke56de8352019-02-18 08:34:19 +01007716 return -ENODEV;
James Smart3772a992009-05-22 14:50:54 -04007717
7718 /* Set the device DMA mask size */
Hannes Reinecke56de8352019-02-18 08:34:19 +01007719 error = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
7720 if (error)
7721 error = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
7722 if (error)
Christoph Hellwigf30e1bf2018-10-18 15:10:21 +02007723 return error;
Hannes Reinecke56de8352019-02-18 08:34:19 +01007724 error = -ENODEV;
James Smart3772a992009-05-22 14:50:54 -04007725
7726 /* Get the bus address of Bar0 and Bar2 and the number of bytes
7727 * required by each mapping.
7728 */
7729 phba->pci_bar0_map = pci_resource_start(pdev, 0);
7730 bar0map_len = pci_resource_len(pdev, 0);
7731
7732 phba->pci_bar2_map = pci_resource_start(pdev, 2);
7733 bar2map_len = pci_resource_len(pdev, 2);
7734
7735 /* Map HBA SLIM to a kernel virtual address. */
7736 phba->slim_memmap_p = ioremap(phba->pci_bar0_map, bar0map_len);
7737 if (!phba->slim_memmap_p) {
7738 dev_printk(KERN_ERR, &pdev->dev,
7739 "ioremap failed for SLIM memory.\n");
7740 goto out;
7741 }
7742
7743 /* Map HBA Control Registers to a kernel virtual address. */
7744 phba->ctrl_regs_memmap_p = ioremap(phba->pci_bar2_map, bar2map_len);
7745 if (!phba->ctrl_regs_memmap_p) {
7746 dev_printk(KERN_ERR, &pdev->dev,
7747 "ioremap failed for HBA control registers.\n");
7748 goto out_iounmap_slim;
7749 }
7750
7751 /* Allocate memory for SLI-2 structures */
Luis Chamberlain750afb02019-01-04 09:23:09 +01007752 phba->slim2p.virt = dma_alloc_coherent(&pdev->dev, SLI2_SLIM_SIZE,
7753 &phba->slim2p.phys, GFP_KERNEL);
James Smart3772a992009-05-22 14:50:54 -04007754 if (!phba->slim2p.virt)
7755 goto out_iounmap;
7756
James Smart3772a992009-05-22 14:50:54 -04007757 phba->mbox = phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, mbx);
James Smart7a470272010-03-15 11:25:20 -04007758 phba->mbox_ext = (phba->slim2p.virt +
7759 offsetof(struct lpfc_sli2_slim, mbx_ext_words));
James Smart3772a992009-05-22 14:50:54 -04007760 phba->pcb = (phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, pcb));
7761 phba->IOCBs = (phba->slim2p.virt +
7762 offsetof(struct lpfc_sli2_slim, IOCBs));
7763
7764 phba->hbqslimp.virt = dma_alloc_coherent(&pdev->dev,
7765 lpfc_sli_hbq_size(),
7766 &phba->hbqslimp.phys,
7767 GFP_KERNEL);
7768 if (!phba->hbqslimp.virt)
7769 goto out_free_slim;
7770
7771 hbq_count = lpfc_sli_hbq_count();
7772 ptr = phba->hbqslimp.virt;
7773 for (i = 0; i < hbq_count; ++i) {
7774 phba->hbqs[i].hbq_virt = ptr;
7775 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
7776 ptr += (lpfc_hbq_defs[i]->entry_count *
7777 sizeof(struct lpfc_hbq_entry));
7778 }
7779 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_els_hbq_alloc;
7780 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_els_hbq_free;
7781
7782 memset(phba->hbqslimp.virt, 0, lpfc_sli_hbq_size());
7783
James Smart3772a992009-05-22 14:50:54 -04007784 phba->MBslimaddr = phba->slim_memmap_p;
7785 phba->HAregaddr = phba->ctrl_regs_memmap_p + HA_REG_OFFSET;
7786 phba->CAregaddr = phba->ctrl_regs_memmap_p + CA_REG_OFFSET;
7787 phba->HSregaddr = phba->ctrl_regs_memmap_p + HS_REG_OFFSET;
7788 phba->HCregaddr = phba->ctrl_regs_memmap_p + HC_REG_OFFSET;
James Smartea2151b2008-09-07 11:52:10 -04007789
dea31012005-04-17 16:05:31 -05007790 return 0;
7791
dea31012005-04-17 16:05:31 -05007792out_free_slim:
James Smart34b02dc2008-08-24 21:49:55 -04007793 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
7794 phba->slim2p.virt, phba->slim2p.phys);
dea31012005-04-17 16:05:31 -05007795out_iounmap:
7796 iounmap(phba->ctrl_regs_memmap_p);
Jamie Wellnitz901a9202006-02-28 19:25:19 -05007797out_iounmap_slim:
dea31012005-04-17 16:05:31 -05007798 iounmap(phba->slim_memmap_p);
dea31012005-04-17 16:05:31 -05007799out:
7800 return error;
7801}
7802
James Smarte59058c2008-08-24 21:49:00 -04007803/**
James Smart3772a992009-05-22 14:50:54 -04007804 * lpfc_sli_pci_mem_unset - Unset SLI3 HBA PCI memory space.
7805 * @phba: pointer to lpfc hba data structure.
7806 *
7807 * This routine is invoked to unset the PCI device memory space for device
7808 * with SLI-3 interface spec.
7809 **/
7810static void
7811lpfc_sli_pci_mem_unset(struct lpfc_hba *phba)
7812{
7813 struct pci_dev *pdev;
7814
7815 /* Obtain PCI device reference */
7816 if (!phba->pcidev)
7817 return;
7818 else
7819 pdev = phba->pcidev;
7820
7821 /* Free coherent DMA memory allocated */
7822 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
7823 phba->hbqslimp.virt, phba->hbqslimp.phys);
7824 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
7825 phba->slim2p.virt, phba->slim2p.phys);
7826
7827 /* I/O memory unmap */
7828 iounmap(phba->ctrl_regs_memmap_p);
7829 iounmap(phba->slim_memmap_p);
7830
7831 return;
7832}
7833
7834/**
James Smartda0436e2009-05-22 14:51:39 -04007835 * lpfc_sli4_post_status_check - Wait for SLI4 POST done and check status
7836 * @phba: pointer to lpfc hba data structure.
7837 *
7838 * This routine is invoked to wait for SLI4 device Power On Self Test (POST)
7839 * done and check status.
7840 *
7841 * Return 0 if successful, otherwise -ENODEV.
7842 **/
7843int
7844lpfc_sli4_post_status_check(struct lpfc_hba *phba)
7845{
James Smart2fcee4b2010-12-15 17:57:46 -05007846 struct lpfc_register portsmphr_reg, uerrlo_reg, uerrhi_reg;
7847 struct lpfc_register reg_data;
7848 int i, port_error = 0;
7849 uint32_t if_type;
James Smartda0436e2009-05-22 14:51:39 -04007850
James Smart9940b972011-03-11 16:06:12 -05007851 memset(&portsmphr_reg, 0, sizeof(portsmphr_reg));
7852 memset(&reg_data, 0, sizeof(reg_data));
James Smart2fcee4b2010-12-15 17:57:46 -05007853 if (!phba->sli4_hba.PSMPHRregaddr)
James Smartda0436e2009-05-22 14:51:39 -04007854 return -ENODEV;
7855
James Smartda0436e2009-05-22 14:51:39 -04007856 /* Wait up to 30 seconds for the SLI Port POST done and ready */
7857 for (i = 0; i < 3000; i++) {
James Smart9940b972011-03-11 16:06:12 -05007858 if (lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
7859 &portsmphr_reg.word0) ||
7860 (bf_get(lpfc_port_smphr_perr, &portsmphr_reg))) {
James Smart2fcee4b2010-12-15 17:57:46 -05007861 /* Port has a fatal POST error, break out */
James Smartda0436e2009-05-22 14:51:39 -04007862 port_error = -ENODEV;
7863 break;
7864 }
James Smart2fcee4b2010-12-15 17:57:46 -05007865 if (LPFC_POST_STAGE_PORT_READY ==
7866 bf_get(lpfc_port_smphr_port_status, &portsmphr_reg))
James Smartda0436e2009-05-22 14:51:39 -04007867 break;
James Smartda0436e2009-05-22 14:51:39 -04007868 msleep(10);
7869 }
7870
James Smart2fcee4b2010-12-15 17:57:46 -05007871 /*
7872 * If there was a port error during POST, then don't proceed with
7873 * other register reads as the data may not be valid. Just exit.
7874 */
7875 if (port_error) {
James Smartda0436e2009-05-22 14:51:39 -04007876 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -05007877 "1408 Port Failed POST - portsmphr=0x%x, "
7878 "perr=x%x, sfi=x%x, nip=x%x, ipc=x%x, scr1=x%x, "
7879 "scr2=x%x, hscratch=x%x, pstatus=x%x\n",
7880 portsmphr_reg.word0,
7881 bf_get(lpfc_port_smphr_perr, &portsmphr_reg),
7882 bf_get(lpfc_port_smphr_sfi, &portsmphr_reg),
7883 bf_get(lpfc_port_smphr_nip, &portsmphr_reg),
7884 bf_get(lpfc_port_smphr_ipc, &portsmphr_reg),
7885 bf_get(lpfc_port_smphr_scr1, &portsmphr_reg),
7886 bf_get(lpfc_port_smphr_scr2, &portsmphr_reg),
7887 bf_get(lpfc_port_smphr_host_scratch, &portsmphr_reg),
7888 bf_get(lpfc_port_smphr_port_status, &portsmphr_reg));
7889 } else {
James Smart28baac72010-02-12 14:42:03 -05007890 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -05007891 "2534 Device Info: SLIFamily=0x%x, "
7892 "SLIRev=0x%x, IFType=0x%x, SLIHint_1=0x%x, "
7893 "SLIHint_2=0x%x, FT=0x%x\n",
James Smart28baac72010-02-12 14:42:03 -05007894 bf_get(lpfc_sli_intf_sli_family,
7895 &phba->sli4_hba.sli_intf),
7896 bf_get(lpfc_sli_intf_slirev,
7897 &phba->sli4_hba.sli_intf),
James Smart085c6472010-11-20 23:11:37 -05007898 bf_get(lpfc_sli_intf_if_type,
James Smart28baac72010-02-12 14:42:03 -05007899 &phba->sli4_hba.sli_intf),
James Smart085c6472010-11-20 23:11:37 -05007900 bf_get(lpfc_sli_intf_sli_hint1,
7901 &phba->sli4_hba.sli_intf),
7902 bf_get(lpfc_sli_intf_sli_hint2,
7903 &phba->sli4_hba.sli_intf),
7904 bf_get(lpfc_sli_intf_func_type,
James Smart28baac72010-02-12 14:42:03 -05007905 &phba->sli4_hba.sli_intf));
James Smart2fcee4b2010-12-15 17:57:46 -05007906 /*
7907 * Check for other Port errors during the initialization
7908 * process. Fail the load if the port did not come up
7909 * correctly.
7910 */
7911 if_type = bf_get(lpfc_sli_intf_if_type,
7912 &phba->sli4_hba.sli_intf);
7913 switch (if_type) {
7914 case LPFC_SLI_INTF_IF_TYPE_0:
7915 phba->sli4_hba.ue_mask_lo =
7916 readl(phba->sli4_hba.u.if_type0.UEMASKLOregaddr);
7917 phba->sli4_hba.ue_mask_hi =
7918 readl(phba->sli4_hba.u.if_type0.UEMASKHIregaddr);
7919 uerrlo_reg.word0 =
7920 readl(phba->sli4_hba.u.if_type0.UERRLOregaddr);
7921 uerrhi_reg.word0 =
7922 readl(phba->sli4_hba.u.if_type0.UERRHIregaddr);
7923 if ((~phba->sli4_hba.ue_mask_lo & uerrlo_reg.word0) ||
7924 (~phba->sli4_hba.ue_mask_hi & uerrhi_reg.word0)) {
7925 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7926 "1422 Unrecoverable Error "
7927 "Detected during POST "
7928 "uerr_lo_reg=0x%x, "
7929 "uerr_hi_reg=0x%x, "
7930 "ue_mask_lo_reg=0x%x, "
7931 "ue_mask_hi_reg=0x%x\n",
7932 uerrlo_reg.word0,
7933 uerrhi_reg.word0,
7934 phba->sli4_hba.ue_mask_lo,
7935 phba->sli4_hba.ue_mask_hi);
7936 port_error = -ENODEV;
7937 }
7938 break;
7939 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart27d6ac02018-02-22 08:18:42 -08007940 case LPFC_SLI_INTF_IF_TYPE_6:
James Smart2fcee4b2010-12-15 17:57:46 -05007941 /* Final checks. The port status should be clean. */
James Smart9940b972011-03-11 16:06:12 -05007942 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
7943 &reg_data.word0) ||
James Smart05580562011-05-24 11:40:48 -04007944 (bf_get(lpfc_sliport_status_err, &reg_data) &&
7945 !bf_get(lpfc_sliport_status_rn, &reg_data))) {
James Smart2fcee4b2010-12-15 17:57:46 -05007946 phba->work_status[0] =
7947 readl(phba->sli4_hba.u.if_type2.
7948 ERR1regaddr);
7949 phba->work_status[1] =
7950 readl(phba->sli4_hba.u.if_type2.
7951 ERR2regaddr);
7952 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart8fcb8ac2012-03-01 22:35:58 -05007953 "2888 Unrecoverable port error "
7954 "following POST: port status reg "
7955 "0x%x, port_smphr reg 0x%x, "
James Smart2fcee4b2010-12-15 17:57:46 -05007956 "error 1=0x%x, error 2=0x%x\n",
7957 reg_data.word0,
7958 portsmphr_reg.word0,
7959 phba->work_status[0],
7960 phba->work_status[1]);
7961 port_error = -ENODEV;
7962 }
7963 break;
7964 case LPFC_SLI_INTF_IF_TYPE_1:
7965 default:
7966 break;
7967 }
James Smart28baac72010-02-12 14:42:03 -05007968 }
James Smartda0436e2009-05-22 14:51:39 -04007969 return port_error;
7970}
7971
7972/**
7973 * lpfc_sli4_bar0_register_memmap - Set up SLI4 BAR0 register memory map.
7974 * @phba: pointer to lpfc hba data structure.
James Smart2fcee4b2010-12-15 17:57:46 -05007975 * @if_type: The SLI4 interface type getting configured.
James Smartda0436e2009-05-22 14:51:39 -04007976 *
7977 * This routine is invoked to set up SLI4 BAR0 PCI config space register
7978 * memory map.
7979 **/
7980static void
James Smart2fcee4b2010-12-15 17:57:46 -05007981lpfc_sli4_bar0_register_memmap(struct lpfc_hba *phba, uint32_t if_type)
James Smartda0436e2009-05-22 14:51:39 -04007982{
James Smart2fcee4b2010-12-15 17:57:46 -05007983 switch (if_type) {
7984 case LPFC_SLI_INTF_IF_TYPE_0:
7985 phba->sli4_hba.u.if_type0.UERRLOregaddr =
7986 phba->sli4_hba.conf_regs_memmap_p + LPFC_UERR_STATUS_LO;
7987 phba->sli4_hba.u.if_type0.UERRHIregaddr =
7988 phba->sli4_hba.conf_regs_memmap_p + LPFC_UERR_STATUS_HI;
7989 phba->sli4_hba.u.if_type0.UEMASKLOregaddr =
7990 phba->sli4_hba.conf_regs_memmap_p + LPFC_UE_MASK_LO;
7991 phba->sli4_hba.u.if_type0.UEMASKHIregaddr =
7992 phba->sli4_hba.conf_regs_memmap_p + LPFC_UE_MASK_HI;
7993 phba->sli4_hba.SLIINTFregaddr =
7994 phba->sli4_hba.conf_regs_memmap_p + LPFC_SLI_INTF;
7995 break;
7996 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart0cf07f842017-06-01 21:07:10 -07007997 phba->sli4_hba.u.if_type2.EQDregaddr =
7998 phba->sli4_hba.conf_regs_memmap_p +
7999 LPFC_CTL_PORT_EQ_DELAY_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05008000 phba->sli4_hba.u.if_type2.ERR1regaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04008001 phba->sli4_hba.conf_regs_memmap_p +
8002 LPFC_CTL_PORT_ER1_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05008003 phba->sli4_hba.u.if_type2.ERR2regaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04008004 phba->sli4_hba.conf_regs_memmap_p +
8005 LPFC_CTL_PORT_ER2_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05008006 phba->sli4_hba.u.if_type2.CTRLregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04008007 phba->sli4_hba.conf_regs_memmap_p +
8008 LPFC_CTL_PORT_CTL_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05008009 phba->sli4_hba.u.if_type2.STATUSregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04008010 phba->sli4_hba.conf_regs_memmap_p +
8011 LPFC_CTL_PORT_STA_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05008012 phba->sli4_hba.SLIINTFregaddr =
8013 phba->sli4_hba.conf_regs_memmap_p + LPFC_SLI_INTF;
8014 phba->sli4_hba.PSMPHRregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04008015 phba->sli4_hba.conf_regs_memmap_p +
8016 LPFC_CTL_PORT_SEM_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05008017 phba->sli4_hba.RQDBregaddr =
James Smart962bc512013-01-03 15:44:00 -05008018 phba->sli4_hba.conf_regs_memmap_p +
8019 LPFC_ULP0_RQ_DOORBELL;
James Smart2fcee4b2010-12-15 17:57:46 -05008020 phba->sli4_hba.WQDBregaddr =
James Smart962bc512013-01-03 15:44:00 -05008021 phba->sli4_hba.conf_regs_memmap_p +
8022 LPFC_ULP0_WQ_DOORBELL;
James Smart9dd35422018-02-22 08:18:41 -08008023 phba->sli4_hba.CQDBregaddr =
James Smart2fcee4b2010-12-15 17:57:46 -05008024 phba->sli4_hba.conf_regs_memmap_p + LPFC_EQCQ_DOORBELL;
James Smart9dd35422018-02-22 08:18:41 -08008025 phba->sli4_hba.EQDBregaddr = phba->sli4_hba.CQDBregaddr;
James Smart2fcee4b2010-12-15 17:57:46 -05008026 phba->sli4_hba.MQDBregaddr =
8027 phba->sli4_hba.conf_regs_memmap_p + LPFC_MQ_DOORBELL;
8028 phba->sli4_hba.BMBXregaddr =
8029 phba->sli4_hba.conf_regs_memmap_p + LPFC_BMBX;
8030 break;
James Smart27d6ac02018-02-22 08:18:42 -08008031 case LPFC_SLI_INTF_IF_TYPE_6:
8032 phba->sli4_hba.u.if_type2.EQDregaddr =
8033 phba->sli4_hba.conf_regs_memmap_p +
8034 LPFC_CTL_PORT_EQ_DELAY_OFFSET;
8035 phba->sli4_hba.u.if_type2.ERR1regaddr =
8036 phba->sli4_hba.conf_regs_memmap_p +
8037 LPFC_CTL_PORT_ER1_OFFSET;
8038 phba->sli4_hba.u.if_type2.ERR2regaddr =
8039 phba->sli4_hba.conf_regs_memmap_p +
8040 LPFC_CTL_PORT_ER2_OFFSET;
8041 phba->sli4_hba.u.if_type2.CTRLregaddr =
8042 phba->sli4_hba.conf_regs_memmap_p +
8043 LPFC_CTL_PORT_CTL_OFFSET;
8044 phba->sli4_hba.u.if_type2.STATUSregaddr =
8045 phba->sli4_hba.conf_regs_memmap_p +
8046 LPFC_CTL_PORT_STA_OFFSET;
8047 phba->sli4_hba.PSMPHRregaddr =
8048 phba->sli4_hba.conf_regs_memmap_p +
8049 LPFC_CTL_PORT_SEM_OFFSET;
8050 phba->sli4_hba.BMBXregaddr =
8051 phba->sli4_hba.conf_regs_memmap_p + LPFC_BMBX;
8052 break;
James Smart2fcee4b2010-12-15 17:57:46 -05008053 case LPFC_SLI_INTF_IF_TYPE_1:
8054 default:
8055 dev_printk(KERN_ERR, &phba->pcidev->dev,
8056 "FATAL - unsupported SLI4 interface type - %d\n",
8057 if_type);
8058 break;
8059 }
James Smartda0436e2009-05-22 14:51:39 -04008060}
8061
8062/**
8063 * lpfc_sli4_bar1_register_memmap - Set up SLI4 BAR1 register memory map.
8064 * @phba: pointer to lpfc hba data structure.
8065 *
James Smart27d6ac02018-02-22 08:18:42 -08008066 * This routine is invoked to set up SLI4 BAR1 register memory map.
James Smartda0436e2009-05-22 14:51:39 -04008067 **/
8068static void
James Smart27d6ac02018-02-22 08:18:42 -08008069lpfc_sli4_bar1_register_memmap(struct lpfc_hba *phba, uint32_t if_type)
James Smartda0436e2009-05-22 14:51:39 -04008070{
James Smart27d6ac02018-02-22 08:18:42 -08008071 switch (if_type) {
8072 case LPFC_SLI_INTF_IF_TYPE_0:
8073 phba->sli4_hba.PSMPHRregaddr =
8074 phba->sli4_hba.ctrl_regs_memmap_p +
8075 LPFC_SLIPORT_IF0_SMPHR;
8076 phba->sli4_hba.ISRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
8077 LPFC_HST_ISR0;
8078 phba->sli4_hba.IMRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
8079 LPFC_HST_IMR0;
8080 phba->sli4_hba.ISCRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
8081 LPFC_HST_ISCR0;
8082 break;
8083 case LPFC_SLI_INTF_IF_TYPE_6:
8084 phba->sli4_hba.RQDBregaddr = phba->sli4_hba.drbl_regs_memmap_p +
8085 LPFC_IF6_RQ_DOORBELL;
8086 phba->sli4_hba.WQDBregaddr = phba->sli4_hba.drbl_regs_memmap_p +
8087 LPFC_IF6_WQ_DOORBELL;
8088 phba->sli4_hba.CQDBregaddr = phba->sli4_hba.drbl_regs_memmap_p +
8089 LPFC_IF6_CQ_DOORBELL;
8090 phba->sli4_hba.EQDBregaddr = phba->sli4_hba.drbl_regs_memmap_p +
8091 LPFC_IF6_EQ_DOORBELL;
8092 phba->sli4_hba.MQDBregaddr = phba->sli4_hba.drbl_regs_memmap_p +
8093 LPFC_IF6_MQ_DOORBELL;
8094 break;
8095 case LPFC_SLI_INTF_IF_TYPE_2:
8096 case LPFC_SLI_INTF_IF_TYPE_1:
8097 default:
8098 dev_err(&phba->pcidev->dev,
8099 "FATAL - unsupported SLI4 interface type - %d\n",
8100 if_type);
8101 break;
8102 }
James Smartda0436e2009-05-22 14:51:39 -04008103}
8104
8105/**
8106 * lpfc_sli4_bar2_register_memmap - Set up SLI4 BAR2 register memory map.
8107 * @phba: pointer to lpfc hba data structure.
8108 * @vf: virtual function number
8109 *
8110 * This routine is invoked to set up SLI4 BAR2 doorbell register memory map
8111 * based on the given viftual function number, @vf.
8112 *
8113 * Return 0 if successful, otherwise -ENODEV.
8114 **/
8115static int
8116lpfc_sli4_bar2_register_memmap(struct lpfc_hba *phba, uint32_t vf)
8117{
8118 if (vf > LPFC_VIR_FUNC_MAX)
8119 return -ENODEV;
8120
8121 phba->sli4_hba.RQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
James Smart962bc512013-01-03 15:44:00 -05008122 vf * LPFC_VFR_PAGE_SIZE +
8123 LPFC_ULP0_RQ_DOORBELL);
James Smartda0436e2009-05-22 14:51:39 -04008124 phba->sli4_hba.WQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
James Smart962bc512013-01-03 15:44:00 -05008125 vf * LPFC_VFR_PAGE_SIZE +
8126 LPFC_ULP0_WQ_DOORBELL);
James Smart9dd35422018-02-22 08:18:41 -08008127 phba->sli4_hba.CQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
8128 vf * LPFC_VFR_PAGE_SIZE +
8129 LPFC_EQCQ_DOORBELL);
8130 phba->sli4_hba.EQDBregaddr = phba->sli4_hba.CQDBregaddr;
James Smartda0436e2009-05-22 14:51:39 -04008131 phba->sli4_hba.MQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
8132 vf * LPFC_VFR_PAGE_SIZE + LPFC_MQ_DOORBELL);
8133 phba->sli4_hba.BMBXregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
8134 vf * LPFC_VFR_PAGE_SIZE + LPFC_BMBX);
8135 return 0;
8136}
8137
8138/**
8139 * lpfc_create_bootstrap_mbox - Create the bootstrap mailbox
8140 * @phba: pointer to lpfc hba data structure.
8141 *
8142 * This routine is invoked to create the bootstrap mailbox
8143 * region consistent with the SLI-4 interface spec. This
8144 * routine allocates all memory necessary to communicate
8145 * mailbox commands to the port and sets up all alignment
8146 * needs. No locks are expected to be held when calling
8147 * this routine.
8148 *
8149 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008150 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04008151 * -ENOMEM - could not allocated memory.
James Smartda0436e2009-05-22 14:51:39 -04008152 **/
8153static int
8154lpfc_create_bootstrap_mbox(struct lpfc_hba *phba)
8155{
8156 uint32_t bmbx_size;
8157 struct lpfc_dmabuf *dmabuf;
8158 struct dma_address *dma_address;
8159 uint32_t pa_addr;
8160 uint64_t phys_addr;
8161
8162 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
8163 if (!dmabuf)
8164 return -ENOMEM;
8165
8166 /*
8167 * The bootstrap mailbox region is comprised of 2 parts
8168 * plus an alignment restriction of 16 bytes.
8169 */
8170 bmbx_size = sizeof(struct lpfc_bmbx_create) + (LPFC_ALIGN_16_BYTE - 1);
Luis Chamberlain750afb02019-01-04 09:23:09 +01008171 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev, bmbx_size,
8172 &dmabuf->phys, GFP_KERNEL);
James Smartda0436e2009-05-22 14:51:39 -04008173 if (!dmabuf->virt) {
8174 kfree(dmabuf);
8175 return -ENOMEM;
8176 }
James Smartda0436e2009-05-22 14:51:39 -04008177
8178 /*
8179 * Initialize the bootstrap mailbox pointers now so that the register
8180 * operations are simple later. The mailbox dma address is required
8181 * to be 16-byte aligned. Also align the virtual memory as each
8182 * maibox is copied into the bmbx mailbox region before issuing the
8183 * command to the port.
8184 */
8185 phba->sli4_hba.bmbx.dmabuf = dmabuf;
8186 phba->sli4_hba.bmbx.bmbx_size = bmbx_size;
8187
8188 phba->sli4_hba.bmbx.avirt = PTR_ALIGN(dmabuf->virt,
8189 LPFC_ALIGN_16_BYTE);
8190 phba->sli4_hba.bmbx.aphys = ALIGN(dmabuf->phys,
8191 LPFC_ALIGN_16_BYTE);
8192
8193 /*
8194 * Set the high and low physical addresses now. The SLI4 alignment
8195 * requirement is 16 bytes and the mailbox is posted to the port
8196 * as two 30-bit addresses. The other data is a bit marking whether
8197 * the 30-bit address is the high or low address.
8198 * Upcast bmbx aphys to 64bits so shift instruction compiles
8199 * clean on 32 bit machines.
8200 */
8201 dma_address = &phba->sli4_hba.bmbx.dma_address;
8202 phys_addr = (uint64_t)phba->sli4_hba.bmbx.aphys;
8203 pa_addr = (uint32_t) ((phys_addr >> 34) & 0x3fffffff);
8204 dma_address->addr_hi = (uint32_t) ((pa_addr << 2) |
8205 LPFC_BMBX_BIT1_ADDR_HI);
8206
8207 pa_addr = (uint32_t) ((phba->sli4_hba.bmbx.aphys >> 4) & 0x3fffffff);
8208 dma_address->addr_lo = (uint32_t) ((pa_addr << 2) |
8209 LPFC_BMBX_BIT1_ADDR_LO);
8210 return 0;
8211}
8212
8213/**
8214 * lpfc_destroy_bootstrap_mbox - Destroy all bootstrap mailbox resources
8215 * @phba: pointer to lpfc hba data structure.
8216 *
8217 * This routine is invoked to teardown the bootstrap mailbox
8218 * region and release all host resources. This routine requires
8219 * the caller to ensure all mailbox commands recovered, no
8220 * additional mailbox comands are sent, and interrupts are disabled
8221 * before calling this routine.
8222 *
8223 **/
8224static void
8225lpfc_destroy_bootstrap_mbox(struct lpfc_hba *phba)
8226{
8227 dma_free_coherent(&phba->pcidev->dev,
8228 phba->sli4_hba.bmbx.bmbx_size,
8229 phba->sli4_hba.bmbx.dmabuf->virt,
8230 phba->sli4_hba.bmbx.dmabuf->phys);
8231
8232 kfree(phba->sli4_hba.bmbx.dmabuf);
8233 memset(&phba->sli4_hba.bmbx, 0, sizeof(struct lpfc_bmbx));
8234}
8235
8236/**
8237 * lpfc_sli4_read_config - Get the config parameters.
8238 * @phba: pointer to lpfc hba data structure.
8239 *
8240 * This routine is invoked to read the configuration parameters from the HBA.
8241 * The configuration parameters are used to set the base and maximum values
8242 * for RPI's XRI's VPI's VFI's and FCFIs. These values also affect the resource
8243 * allocation for the port.
8244 *
8245 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008246 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008247 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04008248 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04008249 **/
James Smartff78d8f2011-12-13 13:21:35 -05008250int
James Smartda0436e2009-05-22 14:51:39 -04008251lpfc_sli4_read_config(struct lpfc_hba *phba)
8252{
8253 LPFC_MBOXQ_t *pmb;
8254 struct lpfc_mbx_read_config *rd_config;
James Smart912e3ac2011-05-24 11:42:11 -04008255 union lpfc_sli4_cfg_shdr *shdr;
8256 uint32_t shdr_status, shdr_add_status;
8257 struct lpfc_mbx_get_func_cfg *get_func_cfg;
8258 struct lpfc_rsrc_desc_fcfcoe *desc;
James Smart8aa134a2012-08-14 14:25:29 -04008259 char *pdesc_0;
James Smartc6918162016-10-13 15:06:16 -07008260 uint16_t forced_link_speed;
James Smart6a828b02019-01-28 11:14:31 -08008261 uint32_t if_type, qmin;
James Smart8aa134a2012-08-14 14:25:29 -04008262 int length, i, rc = 0, rc2;
James Smartda0436e2009-05-22 14:51:39 -04008263
8264 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
8265 if (!pmb) {
8266 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8267 "2011 Unable to allocate memory for issuing "
8268 "SLI_CONFIG_SPECIAL mailbox command\n");
8269 return -ENOMEM;
8270 }
8271
8272 lpfc_read_config(phba, pmb);
8273
8274 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
8275 if (rc != MBX_SUCCESS) {
8276 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8277 "2012 Mailbox failed , mbxCmd x%x "
8278 "READ_CONFIG, mbxStatus x%x\n",
8279 bf_get(lpfc_mqe_command, &pmb->u.mqe),
8280 bf_get(lpfc_mqe_status, &pmb->u.mqe));
8281 rc = -EIO;
8282 } else {
8283 rd_config = &pmb->u.mqe.un.rd_config;
James Smartff78d8f2011-12-13 13:21:35 -05008284 if (bf_get(lpfc_mbx_rd_conf_lnk_ldv, rd_config)) {
8285 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
8286 phba->sli4_hba.lnk_info.lnk_tp =
8287 bf_get(lpfc_mbx_rd_conf_lnk_type, rd_config);
8288 phba->sli4_hba.lnk_info.lnk_no =
8289 bf_get(lpfc_mbx_rd_conf_lnk_numb, rd_config);
8290 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8291 "3081 lnk_type:%d, lnk_numb:%d\n",
8292 phba->sli4_hba.lnk_info.lnk_tp,
8293 phba->sli4_hba.lnk_info.lnk_no);
8294 } else
8295 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8296 "3082 Mailbox (x%x) returned ldv:x0\n",
8297 bf_get(lpfc_mqe_command, &pmb->u.mqe));
James Smart44fd7fe2017-08-23 16:55:47 -07008298 if (bf_get(lpfc_mbx_rd_conf_bbscn_def, rd_config)) {
8299 phba->bbcredit_support = 1;
8300 phba->sli4_hba.bbscn_params.word0 = rd_config->word8;
8301 }
8302
James Smart1dc5ec22018-10-23 13:41:11 -07008303 phba->sli4_hba.conf_trunk =
8304 bf_get(lpfc_mbx_rd_conf_trunk, rd_config);
James Smart6d368e52011-05-24 11:44:12 -04008305 phba->sli4_hba.extents_in_use =
8306 bf_get(lpfc_mbx_rd_conf_extnts_inuse, rd_config);
James Smartda0436e2009-05-22 14:51:39 -04008307 phba->sli4_hba.max_cfg_param.max_xri =
8308 bf_get(lpfc_mbx_rd_conf_xri_count, rd_config);
8309 phba->sli4_hba.max_cfg_param.xri_base =
8310 bf_get(lpfc_mbx_rd_conf_xri_base, rd_config);
8311 phba->sli4_hba.max_cfg_param.max_vpi =
8312 bf_get(lpfc_mbx_rd_conf_vpi_count, rd_config);
James Smart8b47ae62018-11-29 16:09:33 -08008313 /* Limit the max we support */
8314 if (phba->sli4_hba.max_cfg_param.max_vpi > LPFC_MAX_VPORTS)
8315 phba->sli4_hba.max_cfg_param.max_vpi = LPFC_MAX_VPORTS;
James Smartda0436e2009-05-22 14:51:39 -04008316 phba->sli4_hba.max_cfg_param.vpi_base =
8317 bf_get(lpfc_mbx_rd_conf_vpi_base, rd_config);
8318 phba->sli4_hba.max_cfg_param.max_rpi =
8319 bf_get(lpfc_mbx_rd_conf_rpi_count, rd_config);
8320 phba->sli4_hba.max_cfg_param.rpi_base =
8321 bf_get(lpfc_mbx_rd_conf_rpi_base, rd_config);
8322 phba->sli4_hba.max_cfg_param.max_vfi =
8323 bf_get(lpfc_mbx_rd_conf_vfi_count, rd_config);
8324 phba->sli4_hba.max_cfg_param.vfi_base =
8325 bf_get(lpfc_mbx_rd_conf_vfi_base, rd_config);
8326 phba->sli4_hba.max_cfg_param.max_fcfi =
8327 bf_get(lpfc_mbx_rd_conf_fcfi_count, rd_config);
James Smartda0436e2009-05-22 14:51:39 -04008328 phba->sli4_hba.max_cfg_param.max_eq =
8329 bf_get(lpfc_mbx_rd_conf_eq_count, rd_config);
8330 phba->sli4_hba.max_cfg_param.max_rq =
8331 bf_get(lpfc_mbx_rd_conf_rq_count, rd_config);
8332 phba->sli4_hba.max_cfg_param.max_wq =
8333 bf_get(lpfc_mbx_rd_conf_wq_count, rd_config);
8334 phba->sli4_hba.max_cfg_param.max_cq =
8335 bf_get(lpfc_mbx_rd_conf_cq_count, rd_config);
8336 phba->lmt = bf_get(lpfc_mbx_rd_conf_lmt, rd_config);
8337 phba->sli4_hba.next_xri = phba->sli4_hba.max_cfg_param.xri_base;
8338 phba->vpi_base = phba->sli4_hba.max_cfg_param.vpi_base;
8339 phba->vfi_base = phba->sli4_hba.max_cfg_param.vfi_base;
James Smart5ffc2662009-11-18 15:39:44 -05008340 phba->max_vpi = (phba->sli4_hba.max_cfg_param.max_vpi > 0) ?
8341 (phba->sli4_hba.max_cfg_param.max_vpi - 1) : 0;
James Smartda0436e2009-05-22 14:51:39 -04008342 phba->max_vports = phba->max_vpi;
8343 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart6d368e52011-05-24 11:44:12 -04008344 "2003 cfg params Extents? %d "
8345 "XRI(B:%d M:%d), "
James Smartda0436e2009-05-22 14:51:39 -04008346 "VPI(B:%d M:%d) "
8347 "VFI(B:%d M:%d) "
8348 "RPI(B:%d M:%d) "
James Smart2ea259e2017-02-12 13:52:27 -08008349 "FCFI:%d EQ:%d CQ:%d WQ:%d RQ:%d\n",
James Smart6d368e52011-05-24 11:44:12 -04008350 phba->sli4_hba.extents_in_use,
James Smartda0436e2009-05-22 14:51:39 -04008351 phba->sli4_hba.max_cfg_param.xri_base,
8352 phba->sli4_hba.max_cfg_param.max_xri,
8353 phba->sli4_hba.max_cfg_param.vpi_base,
8354 phba->sli4_hba.max_cfg_param.max_vpi,
8355 phba->sli4_hba.max_cfg_param.vfi_base,
8356 phba->sli4_hba.max_cfg_param.max_vfi,
8357 phba->sli4_hba.max_cfg_param.rpi_base,
8358 phba->sli4_hba.max_cfg_param.max_rpi,
James Smart2ea259e2017-02-12 13:52:27 -08008359 phba->sli4_hba.max_cfg_param.max_fcfi,
8360 phba->sli4_hba.max_cfg_param.max_eq,
8361 phba->sli4_hba.max_cfg_param.max_cq,
8362 phba->sli4_hba.max_cfg_param.max_wq,
8363 phba->sli4_hba.max_cfg_param.max_rq);
8364
James Smartd38f33b2018-05-04 20:37:54 -07008365 /*
James Smart6a828b02019-01-28 11:14:31 -08008366 * Calculate queue resources based on how
8367 * many WQ/CQ/EQs are available.
James Smartd38f33b2018-05-04 20:37:54 -07008368 */
James Smart6a828b02019-01-28 11:14:31 -08008369 qmin = phba->sli4_hba.max_cfg_param.max_wq;
8370 if (phba->sli4_hba.max_cfg_param.max_cq < qmin)
8371 qmin = phba->sli4_hba.max_cfg_param.max_cq;
8372 if (phba->sli4_hba.max_cfg_param.max_eq < qmin)
8373 qmin = phba->sli4_hba.max_cfg_param.max_eq;
8374 /*
8375 * Whats left after this can go toward NVME / FCP.
8376 * The minus 4 accounts for ELS, NVME LS, MBOX
8377 * plus one extra. When configured for
8378 * NVMET, FCP io channel WQs are not created.
8379 */
8380 qmin -= 4;
James Smartd38f33b2018-05-04 20:37:54 -07008381
James Smart6a828b02019-01-28 11:14:31 -08008382 /* If NVME is configured, double the number of CQ/WQs needed */
8383 if ((phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) &&
8384 !phba->nvmet_support)
8385 qmin /= 2;
James Smartd38f33b2018-05-04 20:37:54 -07008386
James Smart6a828b02019-01-28 11:14:31 -08008387 /* Check to see if there is enough for NVME */
8388 if ((phba->cfg_irq_chann > qmin) ||
8389 (phba->cfg_hdw_queue > qmin)) {
8390 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8391 "2005 Reducing Queues: "
8392 "WQ %d CQ %d EQ %d: min %d: "
8393 "IRQ %d HDWQ %d\n",
James Smartd38f33b2018-05-04 20:37:54 -07008394 phba->sli4_hba.max_cfg_param.max_wq,
8395 phba->sli4_hba.max_cfg_param.max_cq,
James Smart6a828b02019-01-28 11:14:31 -08008396 phba->sli4_hba.max_cfg_param.max_eq,
8397 qmin, phba->cfg_irq_chann,
James Smartcdb42be2019-01-28 11:14:21 -08008398 phba->cfg_hdw_queue);
James Smartd38f33b2018-05-04 20:37:54 -07008399
James Smart6a828b02019-01-28 11:14:31 -08008400 if (phba->cfg_irq_chann > qmin)
8401 phba->cfg_irq_chann = qmin;
8402 if (phba->cfg_hdw_queue > qmin)
8403 phba->cfg_hdw_queue = qmin;
James Smartd38f33b2018-05-04 20:37:54 -07008404 }
James Smartda0436e2009-05-22 14:51:39 -04008405 }
James Smart912e3ac2011-05-24 11:42:11 -04008406
8407 if (rc)
8408 goto read_cfg_out;
James Smartda0436e2009-05-22 14:51:39 -04008409
James Smartc6918162016-10-13 15:06:16 -07008410 /* Update link speed if forced link speed is supported */
8411 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
James Smart27d6ac02018-02-22 08:18:42 -08008412 if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
James Smartc6918162016-10-13 15:06:16 -07008413 forced_link_speed =
8414 bf_get(lpfc_mbx_rd_conf_link_speed, rd_config);
8415 if (forced_link_speed) {
8416 phba->hba_flag |= HBA_FORCED_LINK_SPEED;
8417
8418 switch (forced_link_speed) {
8419 case LINK_SPEED_1G:
8420 phba->cfg_link_speed =
8421 LPFC_USER_LINK_SPEED_1G;
8422 break;
8423 case LINK_SPEED_2G:
8424 phba->cfg_link_speed =
8425 LPFC_USER_LINK_SPEED_2G;
8426 break;
8427 case LINK_SPEED_4G:
8428 phba->cfg_link_speed =
8429 LPFC_USER_LINK_SPEED_4G;
8430 break;
8431 case LINK_SPEED_8G:
8432 phba->cfg_link_speed =
8433 LPFC_USER_LINK_SPEED_8G;
8434 break;
8435 case LINK_SPEED_10G:
8436 phba->cfg_link_speed =
8437 LPFC_USER_LINK_SPEED_10G;
8438 break;
8439 case LINK_SPEED_16G:
8440 phba->cfg_link_speed =
8441 LPFC_USER_LINK_SPEED_16G;
8442 break;
8443 case LINK_SPEED_32G:
8444 phba->cfg_link_speed =
8445 LPFC_USER_LINK_SPEED_32G;
8446 break;
James Smartfbd8a6b2018-02-22 08:18:45 -08008447 case LINK_SPEED_64G:
8448 phba->cfg_link_speed =
8449 LPFC_USER_LINK_SPEED_64G;
8450 break;
James Smartc6918162016-10-13 15:06:16 -07008451 case 0xffff:
8452 phba->cfg_link_speed =
8453 LPFC_USER_LINK_SPEED_AUTO;
8454 break;
8455 default:
8456 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8457 "0047 Unrecognized link "
8458 "speed : %d\n",
8459 forced_link_speed);
8460 phba->cfg_link_speed =
8461 LPFC_USER_LINK_SPEED_AUTO;
8462 }
8463 }
8464 }
8465
James Smartda0436e2009-05-22 14:51:39 -04008466 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
James Smart572709e2013-07-15 18:32:43 -04008467 length = phba->sli4_hba.max_cfg_param.max_xri -
8468 lpfc_sli4_get_els_iocb_cnt(phba);
8469 if (phba->cfg_hba_queue_depth > length) {
8470 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
8471 "3361 HBA queue depth changed from %d to %d\n",
8472 phba->cfg_hba_queue_depth, length);
8473 phba->cfg_hba_queue_depth = length;
8474 }
James Smart912e3ac2011-05-24 11:42:11 -04008475
James Smart27d6ac02018-02-22 08:18:42 -08008476 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) <
James Smart912e3ac2011-05-24 11:42:11 -04008477 LPFC_SLI_INTF_IF_TYPE_2)
8478 goto read_cfg_out;
8479
8480 /* get the pf# and vf# for SLI4 if_type 2 port */
8481 length = (sizeof(struct lpfc_mbx_get_func_cfg) -
8482 sizeof(struct lpfc_sli4_cfg_mhdr));
8483 lpfc_sli4_config(phba, pmb, LPFC_MBOX_SUBSYSTEM_COMMON,
8484 LPFC_MBOX_OPCODE_GET_FUNCTION_CONFIG,
8485 length, LPFC_SLI4_MBX_EMBED);
8486
James Smart8aa134a2012-08-14 14:25:29 -04008487 rc2 = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart912e3ac2011-05-24 11:42:11 -04008488 shdr = (union lpfc_sli4_cfg_shdr *)
8489 &pmb->u.mqe.un.sli4_config.header.cfg_shdr;
8490 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
8491 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
James Smart8aa134a2012-08-14 14:25:29 -04008492 if (rc2 || shdr_status || shdr_add_status) {
James Smart912e3ac2011-05-24 11:42:11 -04008493 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8494 "3026 Mailbox failed , mbxCmd x%x "
8495 "GET_FUNCTION_CONFIG, mbxStatus x%x\n",
8496 bf_get(lpfc_mqe_command, &pmb->u.mqe),
8497 bf_get(lpfc_mqe_status, &pmb->u.mqe));
James Smart912e3ac2011-05-24 11:42:11 -04008498 goto read_cfg_out;
8499 }
8500
8501 /* search for fc_fcoe resrouce descriptor */
8502 get_func_cfg = &pmb->u.mqe.un.get_func_cfg;
James Smart912e3ac2011-05-24 11:42:11 -04008503
James Smart8aa134a2012-08-14 14:25:29 -04008504 pdesc_0 = (char *)&get_func_cfg->func_cfg.desc[0];
8505 desc = (struct lpfc_rsrc_desc_fcfcoe *)pdesc_0;
8506 length = bf_get(lpfc_rsrc_desc_fcfcoe_length, desc);
8507 if (length == LPFC_RSRC_DESC_TYPE_FCFCOE_V0_RSVD)
8508 length = LPFC_RSRC_DESC_TYPE_FCFCOE_V0_LENGTH;
8509 else if (length != LPFC_RSRC_DESC_TYPE_FCFCOE_V1_LENGTH)
8510 goto read_cfg_out;
8511
James Smart912e3ac2011-05-24 11:42:11 -04008512 for (i = 0; i < LPFC_RSRC_DESC_MAX_NUM; i++) {
James Smart8aa134a2012-08-14 14:25:29 -04008513 desc = (struct lpfc_rsrc_desc_fcfcoe *)(pdesc_0 + length * i);
James Smart912e3ac2011-05-24 11:42:11 -04008514 if (LPFC_RSRC_DESC_TYPE_FCFCOE ==
James Smart8aa134a2012-08-14 14:25:29 -04008515 bf_get(lpfc_rsrc_desc_fcfcoe_type, desc)) {
James Smart912e3ac2011-05-24 11:42:11 -04008516 phba->sli4_hba.iov.pf_number =
8517 bf_get(lpfc_rsrc_desc_fcfcoe_pfnum, desc);
8518 phba->sli4_hba.iov.vf_number =
8519 bf_get(lpfc_rsrc_desc_fcfcoe_vfnum, desc);
8520 break;
8521 }
8522 }
8523
8524 if (i < LPFC_RSRC_DESC_MAX_NUM)
8525 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8526 "3027 GET_FUNCTION_CONFIG: pf_number:%d, "
8527 "vf_number:%d\n", phba->sli4_hba.iov.pf_number,
8528 phba->sli4_hba.iov.vf_number);
James Smart8aa134a2012-08-14 14:25:29 -04008529 else
James Smart912e3ac2011-05-24 11:42:11 -04008530 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8531 "3028 GET_FUNCTION_CONFIG: failed to find "
Colin Ian Kingc4dba182018-10-16 18:28:53 +01008532 "Resource Descriptor:x%x\n",
James Smart912e3ac2011-05-24 11:42:11 -04008533 LPFC_RSRC_DESC_TYPE_FCFCOE);
James Smart912e3ac2011-05-24 11:42:11 -04008534
8535read_cfg_out:
8536 mempool_free(pmb, phba->mbox_mem_pool);
James Smartda0436e2009-05-22 14:51:39 -04008537 return rc;
8538}
8539
8540/**
James Smart2fcee4b2010-12-15 17:57:46 -05008541 * lpfc_setup_endian_order - Write endian order to an SLI4 if_type 0 port.
James Smartda0436e2009-05-22 14:51:39 -04008542 * @phba: pointer to lpfc hba data structure.
8543 *
James Smart2fcee4b2010-12-15 17:57:46 -05008544 * This routine is invoked to setup the port-side endian order when
8545 * the port if_type is 0. This routine has no function for other
8546 * if_types.
James Smartda0436e2009-05-22 14:51:39 -04008547 *
8548 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008549 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008550 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04008551 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04008552 **/
8553static int
8554lpfc_setup_endian_order(struct lpfc_hba *phba)
8555{
8556 LPFC_MBOXQ_t *mboxq;
James Smart2fcee4b2010-12-15 17:57:46 -05008557 uint32_t if_type, rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04008558 uint32_t endian_mb_data[2] = {HOST_ENDIAN_LOW_WORD0,
8559 HOST_ENDIAN_HIGH_WORD1};
8560
James Smart2fcee4b2010-12-15 17:57:46 -05008561 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
8562 switch (if_type) {
8563 case LPFC_SLI_INTF_IF_TYPE_0:
8564 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
8565 GFP_KERNEL);
8566 if (!mboxq) {
8567 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8568 "0492 Unable to allocate memory for "
8569 "issuing SLI_CONFIG_SPECIAL mailbox "
8570 "command\n");
8571 return -ENOMEM;
8572 }
James Smartda0436e2009-05-22 14:51:39 -04008573
James Smart2fcee4b2010-12-15 17:57:46 -05008574 /*
8575 * The SLI4_CONFIG_SPECIAL mailbox command requires the first
8576 * two words to contain special data values and no other data.
8577 */
8578 memset(mboxq, 0, sizeof(LPFC_MBOXQ_t));
8579 memcpy(&mboxq->u.mqe, &endian_mb_data, sizeof(endian_mb_data));
8580 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8581 if (rc != MBX_SUCCESS) {
8582 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8583 "0493 SLI_CONFIG_SPECIAL mailbox "
8584 "failed with status x%x\n",
8585 rc);
8586 rc = -EIO;
8587 }
8588 mempool_free(mboxq, phba->mbox_mem_pool);
8589 break;
James Smart27d6ac02018-02-22 08:18:42 -08008590 case LPFC_SLI_INTF_IF_TYPE_6:
James Smart2fcee4b2010-12-15 17:57:46 -05008591 case LPFC_SLI_INTF_IF_TYPE_2:
8592 case LPFC_SLI_INTF_IF_TYPE_1:
8593 default:
8594 break;
James Smartda0436e2009-05-22 14:51:39 -04008595 }
James Smartda0436e2009-05-22 14:51:39 -04008596 return rc;
8597}
8598
8599/**
James Smart895427b2017-02-12 13:52:30 -08008600 * lpfc_sli4_queue_verify - Verify and update EQ counts
James Smartda0436e2009-05-22 14:51:39 -04008601 * @phba: pointer to lpfc hba data structure.
8602 *
James Smart895427b2017-02-12 13:52:30 -08008603 * This routine is invoked to check the user settable queue counts for EQs.
8604 * After this routine is called the counts will be set to valid values that
James Smart5350d872011-10-10 21:33:49 -04008605 * adhere to the constraints of the system's interrupt vectors and the port's
8606 * queue resources.
James Smartda0436e2009-05-22 14:51:39 -04008607 *
8608 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008609 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008610 * -ENOMEM - No available memory
James Smartda0436e2009-05-22 14:51:39 -04008611 **/
8612static int
James Smart5350d872011-10-10 21:33:49 -04008613lpfc_sli4_queue_verify(struct lpfc_hba *phba)
James Smartda0436e2009-05-22 14:51:39 -04008614{
James Smartda0436e2009-05-22 14:51:39 -04008615 /*
James Smart67d12732012-08-03 12:36:13 -04008616 * Sanity check for configured queue parameters against the run-time
James Smartda0436e2009-05-22 14:51:39 -04008617 * device parameters
8618 */
8619
James Smartbcb24f62017-11-20 16:00:36 -08008620 if (phba->nvmet_support) {
James Smart6a828b02019-01-28 11:14:31 -08008621 if (phba->cfg_irq_chann < phba->cfg_nvmet_mrq)
8622 phba->cfg_nvmet_mrq = phba->cfg_irq_chann;
James Smart982ab122019-03-12 16:30:10 -07008623 if (phba->cfg_nvmet_mrq > LPFC_NVMET_MRQ_MAX)
8624 phba->cfg_nvmet_mrq = LPFC_NVMET_MRQ_MAX;
James Smartbcb24f62017-11-20 16:00:36 -08008625 }
James Smart895427b2017-02-12 13:52:30 -08008626
8627 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart6a828b02019-01-28 11:14:31 -08008628 "2574 IO channels: hdwQ %d IRQ %d MRQ: %d\n",
8629 phba->cfg_hdw_queue, phba->cfg_irq_chann,
8630 phba->cfg_nvmet_mrq);
James Smartda0436e2009-05-22 14:51:39 -04008631
James Smartda0436e2009-05-22 14:51:39 -04008632 /* Get EQ depth from module parameter, fake the default for now */
8633 phba->sli4_hba.eq_esize = LPFC_EQE_SIZE_4B;
8634 phba->sli4_hba.eq_ecount = LPFC_EQE_DEF_COUNT;
8635
James Smart5350d872011-10-10 21:33:49 -04008636 /* Get CQ depth from module parameter, fake the default for now */
8637 phba->sli4_hba.cq_esize = LPFC_CQE_SIZE;
8638 phba->sli4_hba.cq_ecount = LPFC_CQE_DEF_COUNT;
James Smart5350d872011-10-10 21:33:49 -04008639 return 0;
James Smart895427b2017-02-12 13:52:30 -08008640}
8641
8642static int
8643lpfc_alloc_nvme_wq_cq(struct lpfc_hba *phba, int wqidx)
8644{
8645 struct lpfc_queue *qdesc;
James Smartc1a21eb2019-03-12 16:30:29 -07008646 int cpu;
James Smart895427b2017-02-12 13:52:30 -08008647
James Smartc1a21eb2019-03-12 16:30:29 -07008648 cpu = lpfc_find_cpu_handle(phba, wqidx, LPFC_FIND_BY_HDWQ);
James Smarta51e41b2017-12-08 17:18:06 -08008649 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_EXPANDED_PAGE_SIZE,
James Smart81b96ed2017-11-20 16:00:29 -08008650 phba->sli4_hba.cq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -07008651 LPFC_CQE_EXP_COUNT, cpu);
James Smart895427b2017-02-12 13:52:30 -08008652 if (!qdesc) {
8653 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8654 "0508 Failed allocate fast-path NVME CQ (%d)\n",
8655 wqidx);
8656 return 1;
8657 }
James Smart7365f6f2018-02-22 08:18:46 -08008658 qdesc->qe_valid = 1;
James Smart5e5b5112019-01-28 11:14:22 -08008659 qdesc->hdwq = wqidx;
James Smartc1a21eb2019-03-12 16:30:29 -07008660 qdesc->chann = cpu;
James Smartcdb42be2019-01-28 11:14:21 -08008661 phba->sli4_hba.hdwq[wqidx].nvme_cq = qdesc;
James Smart895427b2017-02-12 13:52:30 -08008662
James Smarta51e41b2017-12-08 17:18:06 -08008663 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_EXPANDED_PAGE_SIZE,
James Smartc1a21eb2019-03-12 16:30:29 -07008664 LPFC_WQE128_SIZE, LPFC_WQE_EXP_COUNT,
8665 cpu);
James Smart895427b2017-02-12 13:52:30 -08008666 if (!qdesc) {
8667 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8668 "0509 Failed allocate fast-path NVME WQ (%d)\n",
8669 wqidx);
8670 return 1;
8671 }
James Smart5e5b5112019-01-28 11:14:22 -08008672 qdesc->hdwq = wqidx;
James Smart6a828b02019-01-28 11:14:31 -08008673 qdesc->chann = wqidx;
James Smartcdb42be2019-01-28 11:14:21 -08008674 phba->sli4_hba.hdwq[wqidx].nvme_wq = qdesc;
James Smart895427b2017-02-12 13:52:30 -08008675 list_add_tail(&qdesc->wq_list, &phba->sli4_hba.lpfc_wq_list);
8676 return 0;
8677}
8678
8679static int
8680lpfc_alloc_fcp_wq_cq(struct lpfc_hba *phba, int wqidx)
8681{
8682 struct lpfc_queue *qdesc;
James Smartc176ffa2018-01-30 15:58:46 -08008683 uint32_t wqesize;
James Smartc1a21eb2019-03-12 16:30:29 -07008684 int cpu;
James Smart895427b2017-02-12 13:52:30 -08008685
James Smartc1a21eb2019-03-12 16:30:29 -07008686 cpu = lpfc_find_cpu_handle(phba, wqidx, LPFC_FIND_BY_HDWQ);
James Smart895427b2017-02-12 13:52:30 -08008687 /* Create Fast Path FCP CQs */
James Smartc176ffa2018-01-30 15:58:46 -08008688 if (phba->enab_exp_wqcq_pages)
James Smarta51e41b2017-12-08 17:18:06 -08008689 /* Increase the CQ size when WQEs contain an embedded cdb */
8690 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_EXPANDED_PAGE_SIZE,
8691 phba->sli4_hba.cq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -07008692 LPFC_CQE_EXP_COUNT, cpu);
James Smarta51e41b2017-12-08 17:18:06 -08008693
8694 else
8695 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
8696 phba->sli4_hba.cq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -07008697 phba->sli4_hba.cq_ecount, cpu);
James Smart895427b2017-02-12 13:52:30 -08008698 if (!qdesc) {
8699 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8700 "0499 Failed allocate fast-path FCP CQ (%d)\n", wqidx);
8701 return 1;
8702 }
James Smart7365f6f2018-02-22 08:18:46 -08008703 qdesc->qe_valid = 1;
James Smart5e5b5112019-01-28 11:14:22 -08008704 qdesc->hdwq = wqidx;
James Smartc1a21eb2019-03-12 16:30:29 -07008705 qdesc->chann = cpu;
James Smartcdb42be2019-01-28 11:14:21 -08008706 phba->sli4_hba.hdwq[wqidx].fcp_cq = qdesc;
James Smart895427b2017-02-12 13:52:30 -08008707
8708 /* Create Fast Path FCP WQs */
James Smartc176ffa2018-01-30 15:58:46 -08008709 if (phba->enab_exp_wqcq_pages) {
James Smarta51e41b2017-12-08 17:18:06 -08008710 /* Increase the WQ size when WQEs contain an embedded cdb */
James Smartc176ffa2018-01-30 15:58:46 -08008711 wqesize = (phba->fcp_embed_io) ?
8712 LPFC_WQE128_SIZE : phba->sli4_hba.wq_esize;
James Smarta51e41b2017-12-08 17:18:06 -08008713 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_EXPANDED_PAGE_SIZE,
James Smartc176ffa2018-01-30 15:58:46 -08008714 wqesize,
James Smartc1a21eb2019-03-12 16:30:29 -07008715 LPFC_WQE_EXP_COUNT, cpu);
James Smartc176ffa2018-01-30 15:58:46 -08008716 } else
James Smarta51e41b2017-12-08 17:18:06 -08008717 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
8718 phba->sli4_hba.wq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -07008719 phba->sli4_hba.wq_ecount, cpu);
James Smartc176ffa2018-01-30 15:58:46 -08008720
James Smart895427b2017-02-12 13:52:30 -08008721 if (!qdesc) {
8722 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8723 "0503 Failed allocate fast-path FCP WQ (%d)\n",
8724 wqidx);
8725 return 1;
8726 }
James Smart5e5b5112019-01-28 11:14:22 -08008727 qdesc->hdwq = wqidx;
James Smart6a828b02019-01-28 11:14:31 -08008728 qdesc->chann = wqidx;
James Smartcdb42be2019-01-28 11:14:21 -08008729 phba->sli4_hba.hdwq[wqidx].fcp_wq = qdesc;
James Smart895427b2017-02-12 13:52:30 -08008730 list_add_tail(&qdesc->wq_list, &phba->sli4_hba.lpfc_wq_list);
8731 return 0;
James Smart5350d872011-10-10 21:33:49 -04008732}
8733
8734/**
8735 * lpfc_sli4_queue_create - Create all the SLI4 queues
8736 * @phba: pointer to lpfc hba data structure.
8737 *
8738 * This routine is invoked to allocate all the SLI4 queues for the FCoE HBA
8739 * operation. For each SLI4 queue type, the parameters such as queue entry
8740 * count (queue depth) shall be taken from the module parameter. For now,
8741 * we just use some constant number as place holder.
8742 *
8743 * Return codes
Anatol Pomozov4907cb72012-09-01 10:31:09 -07008744 * 0 - successful
James Smart5350d872011-10-10 21:33:49 -04008745 * -ENOMEM - No availble memory
8746 * -EIO - The mailbox failed to complete successfully.
8747 **/
8748int
8749lpfc_sli4_queue_create(struct lpfc_hba *phba)
8750{
8751 struct lpfc_queue *qdesc;
James Smart657add42019-05-21 17:49:06 -07008752 int idx, cpu, eqcpu;
James Smart5e5b5112019-01-28 11:14:22 -08008753 struct lpfc_sli4_hdw_queue *qp;
James Smart657add42019-05-21 17:49:06 -07008754 struct lpfc_vector_map_info *cpup;
8755 struct lpfc_vector_map_info *eqcpup;
James Smart32517fc2019-01-28 11:14:33 -08008756 struct lpfc_eq_intr_info *eqi;
James Smart5350d872011-10-10 21:33:49 -04008757
8758 /*
James Smart67d12732012-08-03 12:36:13 -04008759 * Create HBA Record arrays.
James Smart895427b2017-02-12 13:52:30 -08008760 * Both NVME and FCP will share that same vectors / EQs
James Smart5350d872011-10-10 21:33:49 -04008761 */
James Smart67d12732012-08-03 12:36:13 -04008762 phba->sli4_hba.mq_esize = LPFC_MQE_SIZE;
8763 phba->sli4_hba.mq_ecount = LPFC_MQE_DEF_COUNT;
8764 phba->sli4_hba.wq_esize = LPFC_WQE_SIZE;
8765 phba->sli4_hba.wq_ecount = LPFC_WQE_DEF_COUNT;
8766 phba->sli4_hba.rq_esize = LPFC_RQE_SIZE;
8767 phba->sli4_hba.rq_ecount = LPFC_RQE_DEF_COUNT;
James Smart895427b2017-02-12 13:52:30 -08008768 phba->sli4_hba.eq_esize = LPFC_EQE_SIZE_4B;
8769 phba->sli4_hba.eq_ecount = LPFC_EQE_DEF_COUNT;
8770 phba->sli4_hba.cq_esize = LPFC_CQE_SIZE;
8771 phba->sli4_hba.cq_ecount = LPFC_CQE_DEF_COUNT;
James Smart67d12732012-08-03 12:36:13 -04008772
James Smartcdb42be2019-01-28 11:14:21 -08008773 if (!phba->sli4_hba.hdwq) {
James Smart5e5b5112019-01-28 11:14:22 -08008774 phba->sli4_hba.hdwq = kcalloc(
8775 phba->cfg_hdw_queue, sizeof(struct lpfc_sli4_hdw_queue),
8776 GFP_KERNEL);
8777 if (!phba->sli4_hba.hdwq) {
James Smart895427b2017-02-12 13:52:30 -08008778 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart5e5b5112019-01-28 11:14:22 -08008779 "6427 Failed allocate memory for "
8780 "fast-path Hardware Queue array\n");
James Smart895427b2017-02-12 13:52:30 -08008781 goto out_error;
8782 }
James Smart5e5b5112019-01-28 11:14:22 -08008783 /* Prepare hardware queues to take IO buffers */
8784 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
8785 qp = &phba->sli4_hba.hdwq[idx];
8786 spin_lock_init(&qp->io_buf_list_get_lock);
8787 spin_lock_init(&qp->io_buf_list_put_lock);
8788 INIT_LIST_HEAD(&qp->lpfc_io_buf_list_get);
8789 INIT_LIST_HEAD(&qp->lpfc_io_buf_list_put);
8790 qp->get_io_bufs = 0;
8791 qp->put_io_bufs = 0;
8792 qp->total_io_bufs = 0;
8793 spin_lock_init(&qp->abts_scsi_buf_list_lock);
8794 INIT_LIST_HEAD(&qp->lpfc_abts_scsi_buf_list);
8795 qp->abts_scsi_io_bufs = 0;
8796 spin_lock_init(&qp->abts_nvme_buf_list_lock);
8797 INIT_LIST_HEAD(&qp->lpfc_abts_nvme_buf_list);
8798 qp->abts_nvme_io_bufs = 0;
James Smart895427b2017-02-12 13:52:30 -08008799 }
James Smart67d12732012-08-03 12:36:13 -04008800 }
8801
James Smartcdb42be2019-01-28 11:14:21 -08008802 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
James Smart2d7dbc42017-02-12 13:52:35 -08008803 if (phba->nvmet_support) {
8804 phba->sli4_hba.nvmet_cqset = kcalloc(
8805 phba->cfg_nvmet_mrq,
8806 sizeof(struct lpfc_queue *),
8807 GFP_KERNEL);
8808 if (!phba->sli4_hba.nvmet_cqset) {
8809 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8810 "3121 Fail allocate memory for "
8811 "fast-path CQ set array\n");
8812 goto out_error;
8813 }
8814 phba->sli4_hba.nvmet_mrq_hdr = kcalloc(
8815 phba->cfg_nvmet_mrq,
8816 sizeof(struct lpfc_queue *),
8817 GFP_KERNEL);
8818 if (!phba->sli4_hba.nvmet_mrq_hdr) {
8819 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8820 "3122 Fail allocate memory for "
8821 "fast-path RQ set hdr array\n");
8822 goto out_error;
8823 }
8824 phba->sli4_hba.nvmet_mrq_data = kcalloc(
8825 phba->cfg_nvmet_mrq,
8826 sizeof(struct lpfc_queue *),
8827 GFP_KERNEL);
8828 if (!phba->sli4_hba.nvmet_mrq_data) {
8829 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8830 "3124 Fail allocate memory for "
8831 "fast-path RQ set data array\n");
8832 goto out_error;
8833 }
8834 }
James Smart67d12732012-08-03 12:36:13 -04008835 }
James Smartda0436e2009-05-22 14:51:39 -04008836
James Smart895427b2017-02-12 13:52:30 -08008837 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_wq_list);
James Smart67d12732012-08-03 12:36:13 -04008838
James Smart895427b2017-02-12 13:52:30 -08008839 /* Create HBA Event Queues (EQs) */
James Smart657add42019-05-21 17:49:06 -07008840 for_each_present_cpu(cpu) {
8841 /* We only want to create 1 EQ per vector, even though
8842 * multiple CPUs might be using that vector. so only
8843 * selects the CPUs that are LPFC_CPU_FIRST_IRQ.
James Smart6a828b02019-01-28 11:14:31 -08008844 */
James Smart657add42019-05-21 17:49:06 -07008845 cpup = &phba->sli4_hba.cpu_map[cpu];
8846 if (!(cpup->flag & LPFC_CPU_FIRST_IRQ))
James Smart6a828b02019-01-28 11:14:31 -08008847 continue;
James Smart657add42019-05-21 17:49:06 -07008848
8849 /* Get a ptr to the Hardware Queue associated with this CPU */
8850 qp = &phba->sli4_hba.hdwq[cpup->hdwq];
8851
8852 /* Allocate an EQ */
James Smart81b96ed2017-11-20 16:00:29 -08008853 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
8854 phba->sli4_hba.eq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -07008855 phba->sli4_hba.eq_ecount, cpu);
James Smartda0436e2009-05-22 14:51:39 -04008856 if (!qdesc) {
8857 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart657add42019-05-21 17:49:06 -07008858 "0497 Failed allocate EQ (%d)\n",
8859 cpup->hdwq);
James Smart67d12732012-08-03 12:36:13 -04008860 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04008861 }
James Smart7365f6f2018-02-22 08:18:46 -08008862 qdesc->qe_valid = 1;
James Smart657add42019-05-21 17:49:06 -07008863 qdesc->hdwq = cpup->hdwq;
8864 qdesc->chann = cpu; /* First CPU this EQ is affinitised to */
James Smart32517fc2019-01-28 11:14:33 -08008865 qdesc->last_cpu = qdesc->chann;
James Smart657add42019-05-21 17:49:06 -07008866
8867 /* Save the allocated EQ in the Hardware Queue */
8868 qp->hba_eq = qdesc;
8869
James Smart32517fc2019-01-28 11:14:33 -08008870 eqi = per_cpu_ptr(phba->sli4_hba.eq_info, qdesc->last_cpu);
8871 list_add(&qdesc->cpu_list, &eqi->list);
James Smartda0436e2009-05-22 14:51:39 -04008872 }
8873
James Smart657add42019-05-21 17:49:06 -07008874 /* Now we need to populate the other Hardware Queues, that share
8875 * an IRQ vector, with the associated EQ ptr.
8876 */
8877 for_each_present_cpu(cpu) {
8878 cpup = &phba->sli4_hba.cpu_map[cpu];
8879
8880 /* Check for EQ already allocated in previous loop */
8881 if (cpup->flag & LPFC_CPU_FIRST_IRQ)
8882 continue;
8883
8884 /* Check for multiple CPUs per hdwq */
8885 qp = &phba->sli4_hba.hdwq[cpup->hdwq];
8886 if (qp->hba_eq)
8887 continue;
8888
8889 /* We need to share an EQ for this hdwq */
8890 eqcpu = lpfc_find_cpu_handle(phba, cpup->eq, LPFC_FIND_BY_EQ);
8891 eqcpup = &phba->sli4_hba.cpu_map[eqcpu];
8892 qp->hba_eq = phba->sli4_hba.hdwq[eqcpup->hdwq].hba_eq;
8893 }
James Smart895427b2017-02-12 13:52:30 -08008894
James Smartcdb42be2019-01-28 11:14:21 -08008895 /* Allocate SCSI SLI4 CQ/WQs */
James Smart6a828b02019-01-28 11:14:31 -08008896 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
James Smart895427b2017-02-12 13:52:30 -08008897 if (lpfc_alloc_fcp_wq_cq(phba, idx))
8898 goto out_error;
James Smart6a828b02019-01-28 11:14:31 -08008899 }
James Smart895427b2017-02-12 13:52:30 -08008900
James Smartcdb42be2019-01-28 11:14:21 -08008901 /* Allocate NVME SLI4 CQ/WQs */
8902 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
James Smart6a828b02019-01-28 11:14:31 -08008903 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
James Smartcdb42be2019-01-28 11:14:21 -08008904 if (lpfc_alloc_nvme_wq_cq(phba, idx))
8905 goto out_error;
James Smart6a828b02019-01-28 11:14:31 -08008906 }
James Smart67d12732012-08-03 12:36:13 -04008907
James Smartcdb42be2019-01-28 11:14:21 -08008908 if (phba->nvmet_support) {
8909 for (idx = 0; idx < phba->cfg_nvmet_mrq; idx++) {
James Smartc1a21eb2019-03-12 16:30:29 -07008910 cpu = lpfc_find_cpu_handle(phba, idx,
8911 LPFC_FIND_BY_HDWQ);
James Smartcdb42be2019-01-28 11:14:21 -08008912 qdesc = lpfc_sli4_queue_alloc(
8913 phba,
James Smart81b96ed2017-11-20 16:00:29 -08008914 LPFC_DEFAULT_PAGE_SIZE,
8915 phba->sli4_hba.cq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -07008916 phba->sli4_hba.cq_ecount,
8917 cpu);
James Smartcdb42be2019-01-28 11:14:21 -08008918 if (!qdesc) {
8919 lpfc_printf_log(
8920 phba, KERN_ERR, LOG_INIT,
8921 "3142 Failed allocate NVME "
8922 "CQ Set (%d)\n", idx);
8923 goto out_error;
8924 }
8925 qdesc->qe_valid = 1;
James Smart5e5b5112019-01-28 11:14:22 -08008926 qdesc->hdwq = idx;
James Smartc1a21eb2019-03-12 16:30:29 -07008927 qdesc->chann = cpu;
James Smartcdb42be2019-01-28 11:14:21 -08008928 phba->sli4_hba.nvmet_cqset[idx] = qdesc;
James Smart2d7dbc42017-02-12 13:52:35 -08008929 }
James Smart2d7dbc42017-02-12 13:52:35 -08008930 }
8931 }
8932
James Smartda0436e2009-05-22 14:51:39 -04008933 /*
James Smart67d12732012-08-03 12:36:13 -04008934 * Create Slow Path Completion Queues (CQs)
James Smartda0436e2009-05-22 14:51:39 -04008935 */
8936
James Smartc1a21eb2019-03-12 16:30:29 -07008937 cpu = lpfc_find_cpu_handle(phba, 0, LPFC_FIND_BY_EQ);
James Smartda0436e2009-05-22 14:51:39 -04008938 /* Create slow-path Mailbox Command Complete Queue */
James Smart81b96ed2017-11-20 16:00:29 -08008939 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
8940 phba->sli4_hba.cq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -07008941 phba->sli4_hba.cq_ecount, cpu);
James Smartda0436e2009-05-22 14:51:39 -04008942 if (!qdesc) {
8943 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8944 "0500 Failed allocate slow-path mailbox CQ\n");
James Smart67d12732012-08-03 12:36:13 -04008945 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04008946 }
James Smart7365f6f2018-02-22 08:18:46 -08008947 qdesc->qe_valid = 1;
James Smartda0436e2009-05-22 14:51:39 -04008948 phba->sli4_hba.mbx_cq = qdesc;
8949
8950 /* Create slow-path ELS Complete Queue */
James Smart81b96ed2017-11-20 16:00:29 -08008951 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
8952 phba->sli4_hba.cq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -07008953 phba->sli4_hba.cq_ecount, cpu);
James Smartda0436e2009-05-22 14:51:39 -04008954 if (!qdesc) {
8955 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8956 "0501 Failed allocate slow-path ELS CQ\n");
James Smart67d12732012-08-03 12:36:13 -04008957 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04008958 }
James Smart7365f6f2018-02-22 08:18:46 -08008959 qdesc->qe_valid = 1;
James Smart6a828b02019-01-28 11:14:31 -08008960 qdesc->chann = 0;
James Smartda0436e2009-05-22 14:51:39 -04008961 phba->sli4_hba.els_cq = qdesc;
8962
James Smartda0436e2009-05-22 14:51:39 -04008963
James Smart5350d872011-10-10 21:33:49 -04008964 /*
James Smart67d12732012-08-03 12:36:13 -04008965 * Create Slow Path Work Queues (WQs)
James Smart5350d872011-10-10 21:33:49 -04008966 */
James Smartda0436e2009-05-22 14:51:39 -04008967
8968 /* Create Mailbox Command Queue */
James Smartda0436e2009-05-22 14:51:39 -04008969
James Smart81b96ed2017-11-20 16:00:29 -08008970 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
8971 phba->sli4_hba.mq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -07008972 phba->sli4_hba.mq_ecount, cpu);
James Smartda0436e2009-05-22 14:51:39 -04008973 if (!qdesc) {
8974 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8975 "0505 Failed allocate slow-path MQ\n");
James Smart67d12732012-08-03 12:36:13 -04008976 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04008977 }
James Smart6a828b02019-01-28 11:14:31 -08008978 qdesc->chann = 0;
James Smartda0436e2009-05-22 14:51:39 -04008979 phba->sli4_hba.mbx_wq = qdesc;
8980
8981 /*
James Smart67d12732012-08-03 12:36:13 -04008982 * Create ELS Work Queues
James Smartda0436e2009-05-22 14:51:39 -04008983 */
James Smartda0436e2009-05-22 14:51:39 -04008984
8985 /* Create slow-path ELS Work Queue */
James Smart81b96ed2017-11-20 16:00:29 -08008986 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
8987 phba->sli4_hba.wq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -07008988 phba->sli4_hba.wq_ecount, cpu);
James Smartda0436e2009-05-22 14:51:39 -04008989 if (!qdesc) {
8990 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8991 "0504 Failed allocate slow-path ELS WQ\n");
James Smart67d12732012-08-03 12:36:13 -04008992 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04008993 }
James Smart6a828b02019-01-28 11:14:31 -08008994 qdesc->chann = 0;
James Smartda0436e2009-05-22 14:51:39 -04008995 phba->sli4_hba.els_wq = qdesc;
James Smart895427b2017-02-12 13:52:30 -08008996 list_add_tail(&qdesc->wq_list, &phba->sli4_hba.lpfc_wq_list);
8997
8998 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
8999 /* Create NVME LS Complete Queue */
James Smart81b96ed2017-11-20 16:00:29 -08009000 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
9001 phba->sli4_hba.cq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -07009002 phba->sli4_hba.cq_ecount, cpu);
James Smart895427b2017-02-12 13:52:30 -08009003 if (!qdesc) {
9004 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9005 "6079 Failed allocate NVME LS CQ\n");
9006 goto out_error;
9007 }
James Smart6a828b02019-01-28 11:14:31 -08009008 qdesc->chann = 0;
James Smart7365f6f2018-02-22 08:18:46 -08009009 qdesc->qe_valid = 1;
James Smart895427b2017-02-12 13:52:30 -08009010 phba->sli4_hba.nvmels_cq = qdesc;
9011
9012 /* Create NVME LS Work Queue */
James Smart81b96ed2017-11-20 16:00:29 -08009013 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
9014 phba->sli4_hba.wq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -07009015 phba->sli4_hba.wq_ecount, cpu);
James Smart895427b2017-02-12 13:52:30 -08009016 if (!qdesc) {
9017 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9018 "6080 Failed allocate NVME LS WQ\n");
9019 goto out_error;
9020 }
James Smart6a828b02019-01-28 11:14:31 -08009021 qdesc->chann = 0;
James Smart895427b2017-02-12 13:52:30 -08009022 phba->sli4_hba.nvmels_wq = qdesc;
9023 list_add_tail(&qdesc->wq_list, &phba->sli4_hba.lpfc_wq_list);
9024 }
James Smartda0436e2009-05-22 14:51:39 -04009025
James Smartda0436e2009-05-22 14:51:39 -04009026 /*
9027 * Create Receive Queue (RQ)
9028 */
James Smartda0436e2009-05-22 14:51:39 -04009029
9030 /* Create Receive Queue for header */
James Smart81b96ed2017-11-20 16:00:29 -08009031 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
9032 phba->sli4_hba.rq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -07009033 phba->sli4_hba.rq_ecount, cpu);
James Smartda0436e2009-05-22 14:51:39 -04009034 if (!qdesc) {
9035 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9036 "0506 Failed allocate receive HRQ\n");
James Smart67d12732012-08-03 12:36:13 -04009037 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04009038 }
9039 phba->sli4_hba.hdr_rq = qdesc;
9040
9041 /* Create Receive Queue for data */
James Smart81b96ed2017-11-20 16:00:29 -08009042 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
9043 phba->sli4_hba.rq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -07009044 phba->sli4_hba.rq_ecount, cpu);
James Smartda0436e2009-05-22 14:51:39 -04009045 if (!qdesc) {
9046 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9047 "0507 Failed allocate receive DRQ\n");
James Smart67d12732012-08-03 12:36:13 -04009048 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04009049 }
9050 phba->sli4_hba.dat_rq = qdesc;
9051
James Smartcdb42be2019-01-28 11:14:21 -08009052 if ((phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) &&
9053 phba->nvmet_support) {
James Smart2d7dbc42017-02-12 13:52:35 -08009054 for (idx = 0; idx < phba->cfg_nvmet_mrq; idx++) {
James Smartc1a21eb2019-03-12 16:30:29 -07009055 cpu = lpfc_find_cpu_handle(phba, idx,
9056 LPFC_FIND_BY_HDWQ);
James Smart2d7dbc42017-02-12 13:52:35 -08009057 /* Create NVMET Receive Queue for header */
9058 qdesc = lpfc_sli4_queue_alloc(phba,
James Smart81b96ed2017-11-20 16:00:29 -08009059 LPFC_DEFAULT_PAGE_SIZE,
James Smart2d7dbc42017-02-12 13:52:35 -08009060 phba->sli4_hba.rq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -07009061 LPFC_NVMET_RQE_DEF_COUNT,
9062 cpu);
James Smart2d7dbc42017-02-12 13:52:35 -08009063 if (!qdesc) {
9064 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9065 "3146 Failed allocate "
9066 "receive HRQ\n");
9067 goto out_error;
9068 }
James Smart5e5b5112019-01-28 11:14:22 -08009069 qdesc->hdwq = idx;
James Smart2d7dbc42017-02-12 13:52:35 -08009070 phba->sli4_hba.nvmet_mrq_hdr[idx] = qdesc;
9071
9072 /* Only needed for header of RQ pair */
James Smartc1a21eb2019-03-12 16:30:29 -07009073 qdesc->rqbp = kzalloc_node(sizeof(*qdesc->rqbp),
9074 GFP_KERNEL,
9075 cpu_to_node(cpu));
James Smart2d7dbc42017-02-12 13:52:35 -08009076 if (qdesc->rqbp == NULL) {
9077 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9078 "6131 Failed allocate "
9079 "Header RQBP\n");
9080 goto out_error;
9081 }
9082
Dick Kennedy4b40d022017-08-23 16:55:38 -07009083 /* Put list in known state in case driver load fails. */
9084 INIT_LIST_HEAD(&qdesc->rqbp->rqb_buffer_list);
9085
James Smart2d7dbc42017-02-12 13:52:35 -08009086 /* Create NVMET Receive Queue for data */
9087 qdesc = lpfc_sli4_queue_alloc(phba,
James Smart81b96ed2017-11-20 16:00:29 -08009088 LPFC_DEFAULT_PAGE_SIZE,
James Smart2d7dbc42017-02-12 13:52:35 -08009089 phba->sli4_hba.rq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -07009090 LPFC_NVMET_RQE_DEF_COUNT,
9091 cpu);
James Smart2d7dbc42017-02-12 13:52:35 -08009092 if (!qdesc) {
9093 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9094 "3156 Failed allocate "
9095 "receive DRQ\n");
9096 goto out_error;
9097 }
James Smart5e5b5112019-01-28 11:14:22 -08009098 qdesc->hdwq = idx;
James Smart2d7dbc42017-02-12 13:52:35 -08009099 phba->sli4_hba.nvmet_mrq_data[idx] = qdesc;
9100 }
9101 }
9102
James Smart4c47efc2019-01-28 11:14:25 -08009103#if defined(BUILD_NVME)
9104 /* Clear NVME stats */
9105 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
9106 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
9107 memset(&phba->sli4_hba.hdwq[idx].nvme_cstat, 0,
9108 sizeof(phba->sli4_hba.hdwq[idx].nvme_cstat));
9109 }
9110 }
9111#endif
9112
9113 /* Clear SCSI stats */
9114 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) {
9115 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
9116 memset(&phba->sli4_hba.hdwq[idx].scsi_cstat, 0,
9117 sizeof(phba->sli4_hba.hdwq[idx].scsi_cstat));
9118 }
9119 }
9120
James Smartda0436e2009-05-22 14:51:39 -04009121 return 0;
9122
James Smartda0436e2009-05-22 14:51:39 -04009123out_error:
James Smart67d12732012-08-03 12:36:13 -04009124 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04009125 return -ENOMEM;
9126}
9127
James Smart895427b2017-02-12 13:52:30 -08009128static inline void
9129__lpfc_sli4_release_queue(struct lpfc_queue **qp)
9130{
9131 if (*qp != NULL) {
9132 lpfc_sli4_queue_free(*qp);
9133 *qp = NULL;
9134 }
9135}
9136
9137static inline void
9138lpfc_sli4_release_queues(struct lpfc_queue ***qs, int max)
9139{
9140 int idx;
9141
9142 if (*qs == NULL)
9143 return;
9144
9145 for (idx = 0; idx < max; idx++)
9146 __lpfc_sli4_release_queue(&(*qs)[idx]);
9147
9148 kfree(*qs);
9149 *qs = NULL;
9150}
9151
9152static inline void
James Smart6a828b02019-01-28 11:14:31 -08009153lpfc_sli4_release_hdwq(struct lpfc_hba *phba)
James Smart895427b2017-02-12 13:52:30 -08009154{
James Smart6a828b02019-01-28 11:14:31 -08009155 struct lpfc_sli4_hdw_queue *hdwq;
James Smart657add42019-05-21 17:49:06 -07009156 struct lpfc_queue *eq;
James Smartcdb42be2019-01-28 11:14:21 -08009157 uint32_t idx;
9158
James Smart6a828b02019-01-28 11:14:31 -08009159 hdwq = phba->sli4_hba.hdwq;
James Smart6a828b02019-01-28 11:14:31 -08009160
James Smart657add42019-05-21 17:49:06 -07009161 /* Loop thru all Hardware Queues */
9162 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
9163 /* Free the CQ/WQ corresponding to the Hardware Queue */
James Smartcdb42be2019-01-28 11:14:21 -08009164 lpfc_sli4_queue_free(hdwq[idx].fcp_cq);
9165 lpfc_sli4_queue_free(hdwq[idx].nvme_cq);
9166 lpfc_sli4_queue_free(hdwq[idx].fcp_wq);
9167 lpfc_sli4_queue_free(hdwq[idx].nvme_wq);
James Smart657add42019-05-21 17:49:06 -07009168 hdwq[idx].hba_eq = NULL;
James Smartcdb42be2019-01-28 11:14:21 -08009169 hdwq[idx].fcp_cq = NULL;
9170 hdwq[idx].nvme_cq = NULL;
9171 hdwq[idx].fcp_wq = NULL;
9172 hdwq[idx].nvme_wq = NULL;
James Smart895427b2017-02-12 13:52:30 -08009173 }
James Smart657add42019-05-21 17:49:06 -07009174 /* Loop thru all IRQ vectors */
9175 for (idx = 0; idx < phba->cfg_irq_chann; idx++) {
9176 /* Free the EQ corresponding to the IRQ vector */
9177 eq = phba->sli4_hba.hba_eq_hdl[idx].eq;
9178 lpfc_sli4_queue_free(eq);
9179 phba->sli4_hba.hba_eq_hdl[idx].eq = NULL;
9180 }
James Smart895427b2017-02-12 13:52:30 -08009181}
9182
James Smartda0436e2009-05-22 14:51:39 -04009183/**
9184 * lpfc_sli4_queue_destroy - Destroy all the SLI4 queues
9185 * @phba: pointer to lpfc hba data structure.
9186 *
9187 * This routine is invoked to release all the SLI4 queues with the FCoE HBA
9188 * operation.
9189 *
9190 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02009191 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009192 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04009193 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04009194 **/
James Smart5350d872011-10-10 21:33:49 -04009195void
James Smartda0436e2009-05-22 14:51:39 -04009196lpfc_sli4_queue_destroy(struct lpfc_hba *phba)
9197{
James Smart4645f7b2019-03-12 16:30:14 -07009198 /*
9199 * Set FREE_INIT before beginning to free the queues.
9200 * Wait until the users of queues to acknowledge to
9201 * release queues by clearing FREE_WAIT.
9202 */
9203 spin_lock_irq(&phba->hbalock);
9204 phba->sli.sli_flag |= LPFC_QUEUE_FREE_INIT;
9205 while (phba->sli.sli_flag & LPFC_QUEUE_FREE_WAIT) {
9206 spin_unlock_irq(&phba->hbalock);
9207 msleep(20);
9208 spin_lock_irq(&phba->hbalock);
9209 }
9210 spin_unlock_irq(&phba->hbalock);
9211
James Smart895427b2017-02-12 13:52:30 -08009212 /* Release HBA eqs */
James Smartcdb42be2019-01-28 11:14:21 -08009213 if (phba->sli4_hba.hdwq)
James Smart6a828b02019-01-28 11:14:31 -08009214 lpfc_sli4_release_hdwq(phba);
James Smartda0436e2009-05-22 14:51:39 -04009215
James Smartbcb24f62017-11-20 16:00:36 -08009216 if (phba->nvmet_support) {
9217 lpfc_sli4_release_queues(&phba->sli4_hba.nvmet_cqset,
9218 phba->cfg_nvmet_mrq);
James Smart2d7dbc42017-02-12 13:52:35 -08009219
James Smartbcb24f62017-11-20 16:00:36 -08009220 lpfc_sli4_release_queues(&phba->sli4_hba.nvmet_mrq_hdr,
9221 phba->cfg_nvmet_mrq);
9222 lpfc_sli4_release_queues(&phba->sli4_hba.nvmet_mrq_data,
9223 phba->cfg_nvmet_mrq);
9224 }
James Smart2d7dbc42017-02-12 13:52:35 -08009225
James Smartda0436e2009-05-22 14:51:39 -04009226 /* Release mailbox command work queue */
James Smart895427b2017-02-12 13:52:30 -08009227 __lpfc_sli4_release_queue(&phba->sli4_hba.mbx_wq);
James Smartda0436e2009-05-22 14:51:39 -04009228
9229 /* Release ELS work queue */
James Smart895427b2017-02-12 13:52:30 -08009230 __lpfc_sli4_release_queue(&phba->sli4_hba.els_wq);
9231
9232 /* Release ELS work queue */
9233 __lpfc_sli4_release_queue(&phba->sli4_hba.nvmels_wq);
James Smartda0436e2009-05-22 14:51:39 -04009234
9235 /* Release unsolicited receive queue */
James Smart895427b2017-02-12 13:52:30 -08009236 __lpfc_sli4_release_queue(&phba->sli4_hba.hdr_rq);
9237 __lpfc_sli4_release_queue(&phba->sli4_hba.dat_rq);
James Smartda0436e2009-05-22 14:51:39 -04009238
James Smartda0436e2009-05-22 14:51:39 -04009239 /* Release ELS complete queue */
James Smart895427b2017-02-12 13:52:30 -08009240 __lpfc_sli4_release_queue(&phba->sli4_hba.els_cq);
9241
9242 /* Release NVME LS complete queue */
9243 __lpfc_sli4_release_queue(&phba->sli4_hba.nvmels_cq);
James Smartda0436e2009-05-22 14:51:39 -04009244
9245 /* Release mailbox command complete queue */
James Smart895427b2017-02-12 13:52:30 -08009246 __lpfc_sli4_release_queue(&phba->sli4_hba.mbx_cq);
9247
9248 /* Everything on this list has been freed */
9249 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_wq_list);
James Smart4645f7b2019-03-12 16:30:14 -07009250
9251 /* Done with freeing the queues */
9252 spin_lock_irq(&phba->hbalock);
9253 phba->sli.sli_flag &= ~LPFC_QUEUE_FREE_INIT;
9254 spin_unlock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -08009255}
9256
9257int
James Smart895427b2017-02-12 13:52:30 -08009258lpfc_free_rq_buffer(struct lpfc_hba *phba, struct lpfc_queue *rq)
9259{
9260 struct lpfc_rqb *rqbp;
9261 struct lpfc_dmabuf *h_buf;
9262 struct rqb_dmabuf *rqb_buffer;
9263
9264 rqbp = rq->rqbp;
9265 while (!list_empty(&rqbp->rqb_buffer_list)) {
9266 list_remove_head(&rqbp->rqb_buffer_list, h_buf,
9267 struct lpfc_dmabuf, list);
9268
9269 rqb_buffer = container_of(h_buf, struct rqb_dmabuf, hbuf);
9270 (rqbp->rqb_free_buffer)(phba, rqb_buffer);
9271 rqbp->buffer_count--;
9272 }
9273 return 1;
9274}
9275
9276static int
9277lpfc_create_wq_cq(struct lpfc_hba *phba, struct lpfc_queue *eq,
9278 struct lpfc_queue *cq, struct lpfc_queue *wq, uint16_t *cq_map,
9279 int qidx, uint32_t qtype)
9280{
9281 struct lpfc_sli_ring *pring;
9282 int rc;
9283
9284 if (!eq || !cq || !wq) {
9285 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9286 "6085 Fast-path %s (%d) not allocated\n",
9287 ((eq) ? ((cq) ? "WQ" : "CQ") : "EQ"), qidx);
9288 return -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04009289 }
James Smartda0436e2009-05-22 14:51:39 -04009290
James Smart895427b2017-02-12 13:52:30 -08009291 /* create the Cq first */
9292 rc = lpfc_cq_create(phba, cq, eq,
9293 (qtype == LPFC_MBOX) ? LPFC_MCQ : LPFC_WCQ, qtype);
9294 if (rc) {
9295 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9296 "6086 Failed setup of CQ (%d), rc = 0x%x\n",
9297 qidx, (uint32_t)rc);
9298 return rc;
9299 }
9300
9301 if (qtype != LPFC_MBOX) {
James Smartcdb42be2019-01-28 11:14:21 -08009302 /* Setup cq_map for fast lookup */
James Smart895427b2017-02-12 13:52:30 -08009303 if (cq_map)
9304 *cq_map = cq->queue_id;
9305
9306 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9307 "6087 CQ setup: cq[%d]-id=%d, parent eq[%d]-id=%d\n",
9308 qidx, cq->queue_id, qidx, eq->queue_id);
9309
9310 /* create the wq */
9311 rc = lpfc_wq_create(phba, wq, cq, qtype);
9312 if (rc) {
9313 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartc835c082019-03-12 16:30:30 -07009314 "4618 Fail setup fastpath WQ (%d), rc = 0x%x\n",
James Smart895427b2017-02-12 13:52:30 -08009315 qidx, (uint32_t)rc);
9316 /* no need to tear down cq - caller will do so */
9317 return rc;
9318 }
9319
9320 /* Bind this CQ/WQ to the NVME ring */
9321 pring = wq->pring;
9322 pring->sli.sli4.wqp = (void *)wq;
9323 cq->pring = pring;
9324
9325 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9326 "2593 WQ setup: wq[%d]-id=%d assoc=%d, cq[%d]-id=%d\n",
9327 qidx, wq->queue_id, wq->assoc_qid, qidx, cq->queue_id);
9328 } else {
9329 rc = lpfc_mq_create(phba, wq, cq, LPFC_MBOX);
9330 if (rc) {
9331 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9332 "0539 Failed setup of slow-path MQ: "
9333 "rc = 0x%x\n", rc);
9334 /* no need to tear down cq - caller will do so */
9335 return rc;
9336 }
9337
9338 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9339 "2589 MBX MQ setup: wq-id=%d, parent cq-id=%d\n",
9340 phba->sli4_hba.mbx_wq->queue_id,
9341 phba->sli4_hba.mbx_cq->queue_id);
9342 }
9343
9344 return 0;
James Smartda0436e2009-05-22 14:51:39 -04009345}
9346
9347/**
James Smart6a828b02019-01-28 11:14:31 -08009348 * lpfc_setup_cq_lookup - Setup the CQ lookup table
9349 * @phba: pointer to lpfc hba data structure.
9350 *
9351 * This routine will populate the cq_lookup table by all
9352 * available CQ queue_id's.
9353 **/
Bart Van Assche3999df72019-03-28 11:06:16 -07009354static void
James Smart6a828b02019-01-28 11:14:31 -08009355lpfc_setup_cq_lookup(struct lpfc_hba *phba)
9356{
9357 struct lpfc_queue *eq, *childq;
9358 struct lpfc_sli4_hdw_queue *qp;
9359 int qidx;
9360
9361 qp = phba->sli4_hba.hdwq;
9362 memset(phba->sli4_hba.cq_lookup, 0,
9363 (sizeof(struct lpfc_queue *) * (phba->sli4_hba.cq_max + 1)));
James Smart657add42019-05-21 17:49:06 -07009364 /* Loop thru all IRQ vectors */
James Smart6a828b02019-01-28 11:14:31 -08009365 for (qidx = 0; qidx < phba->cfg_irq_chann; qidx++) {
James Smart657add42019-05-21 17:49:06 -07009366 /* Get the EQ corresponding to the IRQ vector */
9367 eq = phba->sli4_hba.hba_eq_hdl[qidx].eq;
James Smart6a828b02019-01-28 11:14:31 -08009368 if (!eq)
9369 continue;
James Smart657add42019-05-21 17:49:06 -07009370 /* Loop through all CQs associated with that EQ */
James Smart6a828b02019-01-28 11:14:31 -08009371 list_for_each_entry(childq, &eq->child_list, list) {
9372 if (childq->queue_id > phba->sli4_hba.cq_max)
9373 continue;
9374 if ((childq->subtype == LPFC_FCP) ||
9375 (childq->subtype == LPFC_NVME))
9376 phba->sli4_hba.cq_lookup[childq->queue_id] =
9377 childq;
9378 }
9379 }
9380}
9381
9382/**
James Smartda0436e2009-05-22 14:51:39 -04009383 * lpfc_sli4_queue_setup - Set up all the SLI4 queues
9384 * @phba: pointer to lpfc hba data structure.
9385 *
9386 * This routine is invoked to set up all the SLI4 queues for the FCoE HBA
9387 * operation.
9388 *
9389 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02009390 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009391 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04009392 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04009393 **/
9394int
9395lpfc_sli4_queue_setup(struct lpfc_hba *phba)
9396{
James Smart962bc512013-01-03 15:44:00 -05009397 uint32_t shdr_status, shdr_add_status;
9398 union lpfc_sli4_cfg_shdr *shdr;
James Smart657add42019-05-21 17:49:06 -07009399 struct lpfc_vector_map_info *cpup;
James Smartcdb42be2019-01-28 11:14:21 -08009400 struct lpfc_sli4_hdw_queue *qp;
James Smart962bc512013-01-03 15:44:00 -05009401 LPFC_MBOXQ_t *mboxq;
James Smart657add42019-05-21 17:49:06 -07009402 int qidx, cpu;
James Smartcb733e32019-01-28 11:14:32 -08009403 uint32_t length, usdelay;
James Smart895427b2017-02-12 13:52:30 -08009404 int rc = -ENOMEM;
James Smart962bc512013-01-03 15:44:00 -05009405
9406 /* Check for dual-ULP support */
9407 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9408 if (!mboxq) {
9409 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9410 "3249 Unable to allocate memory for "
9411 "QUERY_FW_CFG mailbox command\n");
9412 return -ENOMEM;
9413 }
9414 length = (sizeof(struct lpfc_mbx_query_fw_config) -
9415 sizeof(struct lpfc_sli4_cfg_mhdr));
9416 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
9417 LPFC_MBOX_OPCODE_QUERY_FW_CFG,
9418 length, LPFC_SLI4_MBX_EMBED);
9419
9420 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
9421
9422 shdr = (union lpfc_sli4_cfg_shdr *)
9423 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
9424 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9425 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9426 if (shdr_status || shdr_add_status || rc) {
9427 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9428 "3250 QUERY_FW_CFG mailbox failed with status "
9429 "x%x add_status x%x, mbx status x%x\n",
9430 shdr_status, shdr_add_status, rc);
9431 if (rc != MBX_TIMEOUT)
9432 mempool_free(mboxq, phba->mbox_mem_pool);
9433 rc = -ENXIO;
9434 goto out_error;
9435 }
9436
9437 phba->sli4_hba.fw_func_mode =
9438 mboxq->u.mqe.un.query_fw_cfg.rsp.function_mode;
9439 phba->sli4_hba.ulp0_mode = mboxq->u.mqe.un.query_fw_cfg.rsp.ulp0_mode;
9440 phba->sli4_hba.ulp1_mode = mboxq->u.mqe.un.query_fw_cfg.rsp.ulp1_mode;
James Smart8b017a32015-05-21 13:55:18 -04009441 phba->sli4_hba.physical_port =
9442 mboxq->u.mqe.un.query_fw_cfg.rsp.physical_port;
James Smart962bc512013-01-03 15:44:00 -05009443 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9444 "3251 QUERY_FW_CFG: func_mode:x%x, ulp0_mode:x%x, "
9445 "ulp1_mode:x%x\n", phba->sli4_hba.fw_func_mode,
9446 phba->sli4_hba.ulp0_mode, phba->sli4_hba.ulp1_mode);
9447
9448 if (rc != MBX_TIMEOUT)
9449 mempool_free(mboxq, phba->mbox_mem_pool);
James Smartda0436e2009-05-22 14:51:39 -04009450
9451 /*
James Smart67d12732012-08-03 12:36:13 -04009452 * Set up HBA Event Queues (EQs)
James Smartda0436e2009-05-22 14:51:39 -04009453 */
James Smartcdb42be2019-01-28 11:14:21 -08009454 qp = phba->sli4_hba.hdwq;
James Smartda0436e2009-05-22 14:51:39 -04009455
James Smart67d12732012-08-03 12:36:13 -04009456 /* Set up HBA event queue */
James Smartcdb42be2019-01-28 11:14:21 -08009457 if (!qp) {
James Smart2e90f4b2011-12-13 13:22:37 -05009458 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9459 "3147 Fast-path EQs not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05009460 rc = -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04009461 goto out_error;
James Smart2e90f4b2011-12-13 13:22:37 -05009462 }
James Smart657add42019-05-21 17:49:06 -07009463
9464 /* Loop thru all IRQ vectors */
James Smart6a828b02019-01-28 11:14:31 -08009465 for (qidx = 0; qidx < phba->cfg_irq_chann; qidx++) {
James Smart657add42019-05-21 17:49:06 -07009466 /* Create HBA Event Queues (EQs) in order */
9467 for_each_present_cpu(cpu) {
9468 cpup = &phba->sli4_hba.cpu_map[cpu];
9469
9470 /* Look for the CPU thats using that vector with
9471 * LPFC_CPU_FIRST_IRQ set.
9472 */
9473 if (!(cpup->flag & LPFC_CPU_FIRST_IRQ))
9474 continue;
9475 if (qidx != cpup->eq)
9476 continue;
9477
9478 /* Create an EQ for that vector */
9479 rc = lpfc_eq_create(phba, qp[cpup->hdwq].hba_eq,
9480 phba->cfg_fcp_imax);
9481 if (rc) {
9482 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9483 "0523 Failed setup of fast-path"
9484 " EQ (%d), rc = 0x%x\n",
9485 cpup->eq, (uint32_t)rc);
9486 goto out_destroy;
9487 }
9488
9489 /* Save the EQ for that vector in the hba_eq_hdl */
9490 phba->sli4_hba.hba_eq_hdl[cpup->eq].eq =
9491 qp[cpup->hdwq].hba_eq;
9492
9493 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9494 "2584 HBA EQ setup: queue[%d]-id=%d\n",
9495 cpup->eq,
9496 qp[cpup->hdwq].hba_eq->queue_id);
James Smartda0436e2009-05-22 14:51:39 -04009497 }
James Smartda0436e2009-05-22 14:51:39 -04009498 }
9499
James Smart657add42019-05-21 17:49:06 -07009500 /* Loop thru all Hardware Queues */
James Smartcdb42be2019-01-28 11:14:21 -08009501 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
9502 for (qidx = 0; qidx < phba->cfg_hdw_queue; qidx++) {
James Smart657add42019-05-21 17:49:06 -07009503 cpu = lpfc_find_cpu_handle(phba, qidx,
9504 LPFC_FIND_BY_HDWQ);
9505 cpup = &phba->sli4_hba.cpu_map[cpu];
9506
9507 /* Create the CQ/WQ corresponding to the
9508 * Hardware Queue
9509 */
James Smart895427b2017-02-12 13:52:30 -08009510 rc = lpfc_create_wq_cq(phba,
James Smart657add42019-05-21 17:49:06 -07009511 phba->sli4_hba.hdwq[cpup->hdwq].hba_eq,
James Smartcdb42be2019-01-28 11:14:21 -08009512 qp[qidx].nvme_cq,
9513 qp[qidx].nvme_wq,
9514 &phba->sli4_hba.hdwq[qidx].nvme_cq_map,
James Smart895427b2017-02-12 13:52:30 -08009515 qidx, LPFC_NVME);
9516 if (rc) {
9517 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9518 "6123 Failed to setup fastpath "
9519 "NVME WQ/CQ (%d), rc = 0x%x\n",
9520 qidx, (uint32_t)rc);
9521 goto out_destroy;
9522 }
9523 }
James Smart67d12732012-08-03 12:36:13 -04009524 }
9525
James Smartcdb42be2019-01-28 11:14:21 -08009526 for (qidx = 0; qidx < phba->cfg_hdw_queue; qidx++) {
James Smart657add42019-05-21 17:49:06 -07009527 cpu = lpfc_find_cpu_handle(phba, qidx, LPFC_FIND_BY_HDWQ);
9528 cpup = &phba->sli4_hba.cpu_map[cpu];
9529
9530 /* Create the CQ/WQ corresponding to the Hardware Queue */
James Smartcdb42be2019-01-28 11:14:21 -08009531 rc = lpfc_create_wq_cq(phba,
James Smart657add42019-05-21 17:49:06 -07009532 phba->sli4_hba.hdwq[cpup->hdwq].hba_eq,
James Smartcdb42be2019-01-28 11:14:21 -08009533 qp[qidx].fcp_cq,
9534 qp[qidx].fcp_wq,
9535 &phba->sli4_hba.hdwq[qidx].fcp_cq_map,
9536 qidx, LPFC_FCP);
9537 if (rc) {
James Smart67d12732012-08-03 12:36:13 -04009538 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart895427b2017-02-12 13:52:30 -08009539 "0535 Failed to setup fastpath "
9540 "FCP WQ/CQ (%d), rc = 0x%x\n",
9541 qidx, (uint32_t)rc);
James Smartcdb42be2019-01-28 11:14:21 -08009542 goto out_destroy;
James Smart895427b2017-02-12 13:52:30 -08009543 }
James Smart67d12732012-08-03 12:36:13 -04009544 }
James Smartda0436e2009-05-22 14:51:39 -04009545
9546 /*
James Smart895427b2017-02-12 13:52:30 -08009547 * Set up Slow Path Complete Queues (CQs)
James Smartda0436e2009-05-22 14:51:39 -04009548 */
9549
James Smart895427b2017-02-12 13:52:30 -08009550 /* Set up slow-path MBOX CQ/MQ */
9551
9552 if (!phba->sli4_hba.mbx_cq || !phba->sli4_hba.mbx_wq) {
James Smartda0436e2009-05-22 14:51:39 -04009553 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart895427b2017-02-12 13:52:30 -08009554 "0528 %s not allocated\n",
9555 phba->sli4_hba.mbx_cq ?
James Smartd1f525a2017-04-21 16:04:55 -07009556 "Mailbox WQ" : "Mailbox CQ");
James Smart1b511972011-12-13 13:23:09 -05009557 rc = -ENOMEM;
James Smart895427b2017-02-12 13:52:30 -08009558 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -04009559 }
James Smart895427b2017-02-12 13:52:30 -08009560
James Smartcdb42be2019-01-28 11:14:21 -08009561 rc = lpfc_create_wq_cq(phba, qp[0].hba_eq,
James Smartd1f525a2017-04-21 16:04:55 -07009562 phba->sli4_hba.mbx_cq,
9563 phba->sli4_hba.mbx_wq,
9564 NULL, 0, LPFC_MBOX);
James Smartda0436e2009-05-22 14:51:39 -04009565 if (rc) {
9566 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart895427b2017-02-12 13:52:30 -08009567 "0529 Failed setup of mailbox WQ/CQ: rc = 0x%x\n",
9568 (uint32_t)rc);
9569 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -04009570 }
James Smart2d7dbc42017-02-12 13:52:35 -08009571 if (phba->nvmet_support) {
9572 if (!phba->sli4_hba.nvmet_cqset) {
9573 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9574 "3165 Fast-path NVME CQ Set "
9575 "array not allocated\n");
9576 rc = -ENOMEM;
9577 goto out_destroy;
9578 }
9579 if (phba->cfg_nvmet_mrq > 1) {
9580 rc = lpfc_cq_create_set(phba,
9581 phba->sli4_hba.nvmet_cqset,
James Smartcdb42be2019-01-28 11:14:21 -08009582 qp,
James Smart2d7dbc42017-02-12 13:52:35 -08009583 LPFC_WCQ, LPFC_NVMET);
9584 if (rc) {
9585 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9586 "3164 Failed setup of NVME CQ "
9587 "Set, rc = 0x%x\n",
9588 (uint32_t)rc);
9589 goto out_destroy;
9590 }
9591 } else {
9592 /* Set up NVMET Receive Complete Queue */
9593 rc = lpfc_cq_create(phba, phba->sli4_hba.nvmet_cqset[0],
James Smartcdb42be2019-01-28 11:14:21 -08009594 qp[0].hba_eq,
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 "6089 Failed setup NVMET CQ: "
9599 "rc = 0x%x\n", (uint32_t)rc);
9600 goto out_destroy;
9601 }
James Smart81b96ed2017-11-20 16:00:29 -08009602 phba->sli4_hba.nvmet_cqset[0]->chann = 0;
9603
James Smart2d7dbc42017-02-12 13:52:35 -08009604 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9605 "6090 NVMET CQ setup: cq-id=%d, "
9606 "parent eq-id=%d\n",
9607 phba->sli4_hba.nvmet_cqset[0]->queue_id,
James Smartcdb42be2019-01-28 11:14:21 -08009608 qp[0].hba_eq->queue_id);
James Smart2d7dbc42017-02-12 13:52:35 -08009609 }
9610 }
James Smartda0436e2009-05-22 14:51:39 -04009611
James Smart895427b2017-02-12 13:52:30 -08009612 /* Set up slow-path ELS WQ/CQ */
9613 if (!phba->sli4_hba.els_cq || !phba->sli4_hba.els_wq) {
James Smartda0436e2009-05-22 14:51:39 -04009614 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart895427b2017-02-12 13:52:30 -08009615 "0530 ELS %s not allocated\n",
9616 phba->sli4_hba.els_cq ? "WQ" : "CQ");
James Smart1b511972011-12-13 13:23:09 -05009617 rc = -ENOMEM;
James Smart895427b2017-02-12 13:52:30 -08009618 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -04009619 }
James Smartcdb42be2019-01-28 11:14:21 -08009620 rc = lpfc_create_wq_cq(phba, qp[0].hba_eq,
9621 phba->sli4_hba.els_cq,
9622 phba->sli4_hba.els_wq,
9623 NULL, 0, LPFC_ELS);
James Smartda0436e2009-05-22 14:51:39 -04009624 if (rc) {
9625 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartcdb42be2019-01-28 11:14:21 -08009626 "0525 Failed setup of ELS WQ/CQ: rc = 0x%x\n",
9627 (uint32_t)rc);
James Smart895427b2017-02-12 13:52:30 -08009628 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -04009629 }
9630 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9631 "2590 ELS WQ setup: wq-id=%d, parent cq-id=%d\n",
9632 phba->sli4_hba.els_wq->queue_id,
9633 phba->sli4_hba.els_cq->queue_id);
9634
James Smartcdb42be2019-01-28 11:14:21 -08009635 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
James Smart895427b2017-02-12 13:52:30 -08009636 /* Set up NVME LS Complete Queue */
9637 if (!phba->sli4_hba.nvmels_cq || !phba->sli4_hba.nvmels_wq) {
9638 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9639 "6091 LS %s not allocated\n",
9640 phba->sli4_hba.nvmels_cq ? "WQ" : "CQ");
9641 rc = -ENOMEM;
9642 goto out_destroy;
9643 }
James Smartcdb42be2019-01-28 11:14:21 -08009644 rc = lpfc_create_wq_cq(phba, qp[0].hba_eq,
9645 phba->sli4_hba.nvmels_cq,
9646 phba->sli4_hba.nvmels_wq,
9647 NULL, 0, LPFC_NVME_LS);
James Smart895427b2017-02-12 13:52:30 -08009648 if (rc) {
9649 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartcdb42be2019-01-28 11:14:21 -08009650 "0526 Failed setup of NVVME LS WQ/CQ: "
9651 "rc = 0x%x\n", (uint32_t)rc);
James Smart895427b2017-02-12 13:52:30 -08009652 goto out_destroy;
9653 }
9654
9655 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9656 "6096 ELS WQ setup: wq-id=%d, "
9657 "parent cq-id=%d\n",
9658 phba->sli4_hba.nvmels_wq->queue_id,
9659 phba->sli4_hba.nvmels_cq->queue_id);
9660 }
9661
James Smart2d7dbc42017-02-12 13:52:35 -08009662 /*
9663 * Create NVMET Receive Queue (RQ)
9664 */
9665 if (phba->nvmet_support) {
9666 if ((!phba->sli4_hba.nvmet_cqset) ||
9667 (!phba->sli4_hba.nvmet_mrq_hdr) ||
9668 (!phba->sli4_hba.nvmet_mrq_data)) {
9669 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9670 "6130 MRQ CQ Queues not "
9671 "allocated\n");
9672 rc = -ENOMEM;
9673 goto out_destroy;
9674 }
9675 if (phba->cfg_nvmet_mrq > 1) {
9676 rc = lpfc_mrq_create(phba,
9677 phba->sli4_hba.nvmet_mrq_hdr,
9678 phba->sli4_hba.nvmet_mrq_data,
9679 phba->sli4_hba.nvmet_cqset,
9680 LPFC_NVMET);
9681 if (rc) {
9682 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9683 "6098 Failed setup of NVMET "
9684 "MRQ: rc = 0x%x\n",
9685 (uint32_t)rc);
9686 goto out_destroy;
9687 }
9688
9689 } else {
9690 rc = lpfc_rq_create(phba,
9691 phba->sli4_hba.nvmet_mrq_hdr[0],
9692 phba->sli4_hba.nvmet_mrq_data[0],
9693 phba->sli4_hba.nvmet_cqset[0],
9694 LPFC_NVMET);
9695 if (rc) {
9696 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9697 "6057 Failed setup of NVMET "
9698 "Receive Queue: rc = 0x%x\n",
9699 (uint32_t)rc);
9700 goto out_destroy;
9701 }
9702
9703 lpfc_printf_log(
9704 phba, KERN_INFO, LOG_INIT,
9705 "6099 NVMET RQ setup: hdr-rq-id=%d, "
9706 "dat-rq-id=%d parent cq-id=%d\n",
9707 phba->sli4_hba.nvmet_mrq_hdr[0]->queue_id,
9708 phba->sli4_hba.nvmet_mrq_data[0]->queue_id,
9709 phba->sli4_hba.nvmet_cqset[0]->queue_id);
9710
9711 }
9712 }
9713
James Smartda0436e2009-05-22 14:51:39 -04009714 if (!phba->sli4_hba.hdr_rq || !phba->sli4_hba.dat_rq) {
9715 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9716 "0540 Receive Queue not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05009717 rc = -ENOMEM;
James Smart895427b2017-02-12 13:52:30 -08009718 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -04009719 }
James Smart73d91e52011-10-10 21:32:10 -04009720
James Smartda0436e2009-05-22 14:51:39 -04009721 rc = lpfc_rq_create(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
James Smart4d9ab992009-10-02 15:16:39 -04009722 phba->sli4_hba.els_cq, LPFC_USOL);
James Smartda0436e2009-05-22 14:51:39 -04009723 if (rc) {
9724 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9725 "0541 Failed setup of Receive Queue: "
James Smarta2fc4aef2014-09-03 12:57:55 -04009726 "rc = 0x%x\n", (uint32_t)rc);
James Smart895427b2017-02-12 13:52:30 -08009727 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -04009728 }
James Smart73d91e52011-10-10 21:32:10 -04009729
James Smartda0436e2009-05-22 14:51:39 -04009730 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9731 "2592 USL RQ setup: hdr-rq-id=%d, dat-rq-id=%d "
9732 "parent cq-id=%d\n",
9733 phba->sli4_hba.hdr_rq->queue_id,
9734 phba->sli4_hba.dat_rq->queue_id,
James Smart4d9ab992009-10-02 15:16:39 -04009735 phba->sli4_hba.els_cq->queue_id);
James Smart1ba981f2014-02-20 09:56:45 -05009736
James Smartcb733e32019-01-28 11:14:32 -08009737 if (phba->cfg_fcp_imax)
9738 usdelay = LPFC_SEC_TO_USEC / phba->cfg_fcp_imax;
9739 else
9740 usdelay = 0;
9741
James Smart6a828b02019-01-28 11:14:31 -08009742 for (qidx = 0; qidx < phba->cfg_irq_chann;
James Smartcdb42be2019-01-28 11:14:21 -08009743 qidx += LPFC_MAX_EQ_DELAY_EQID_CNT)
James Smart0cf07f842017-06-01 21:07:10 -07009744 lpfc_modify_hba_eq_delay(phba, qidx, LPFC_MAX_EQ_DELAY_EQID_CNT,
James Smartcb733e32019-01-28 11:14:32 -08009745 usdelay);
James Smart43140ca2017-03-04 09:30:34 -08009746
James Smart6a828b02019-01-28 11:14:31 -08009747 if (phba->sli4_hba.cq_max) {
9748 kfree(phba->sli4_hba.cq_lookup);
9749 phba->sli4_hba.cq_lookup = kcalloc((phba->sli4_hba.cq_max + 1),
9750 sizeof(struct lpfc_queue *), GFP_KERNEL);
9751 if (!phba->sli4_hba.cq_lookup) {
James Smart1ba981f2014-02-20 09:56:45 -05009752 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart6a828b02019-01-28 11:14:31 -08009753 "0549 Failed setup of CQ Lookup table: "
9754 "size 0x%x\n", phba->sli4_hba.cq_max);
Dan Carpenterfad28e32019-02-11 21:43:00 +03009755 rc = -ENOMEM;
James Smart1ba981f2014-02-20 09:56:45 -05009756 goto out_destroy;
James Smart895427b2017-02-12 13:52:30 -08009757 }
James Smart6a828b02019-01-28 11:14:31 -08009758 lpfc_setup_cq_lookup(phba);
James Smart1ba981f2014-02-20 09:56:45 -05009759 }
James Smartda0436e2009-05-22 14:51:39 -04009760 return 0;
9761
James Smart895427b2017-02-12 13:52:30 -08009762out_destroy:
9763 lpfc_sli4_queue_unset(phba);
James Smartda0436e2009-05-22 14:51:39 -04009764out_error:
9765 return rc;
9766}
9767
9768/**
9769 * lpfc_sli4_queue_unset - Unset all the SLI4 queues
9770 * @phba: pointer to lpfc hba data structure.
9771 *
9772 * This routine is invoked to unset all the SLI4 queues with the FCoE HBA
9773 * operation.
9774 *
9775 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02009776 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009777 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04009778 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04009779 **/
9780void
9781lpfc_sli4_queue_unset(struct lpfc_hba *phba)
9782{
James Smartcdb42be2019-01-28 11:14:21 -08009783 struct lpfc_sli4_hdw_queue *qp;
James Smart657add42019-05-21 17:49:06 -07009784 struct lpfc_queue *eq;
James Smart895427b2017-02-12 13:52:30 -08009785 int qidx;
James Smartda0436e2009-05-22 14:51:39 -04009786
9787 /* Unset mailbox command work queue */
James Smart895427b2017-02-12 13:52:30 -08009788 if (phba->sli4_hba.mbx_wq)
9789 lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
9790
9791 /* Unset NVME LS work queue */
9792 if (phba->sli4_hba.nvmels_wq)
9793 lpfc_wq_destroy(phba, phba->sli4_hba.nvmels_wq);
9794
James Smartda0436e2009-05-22 14:51:39 -04009795 /* Unset ELS work queue */
Colin Ian King019c0d62017-05-06 23:13:55 +01009796 if (phba->sli4_hba.els_wq)
James Smart895427b2017-02-12 13:52:30 -08009797 lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
9798
James Smartda0436e2009-05-22 14:51:39 -04009799 /* Unset unsolicited receive queue */
James Smart895427b2017-02-12 13:52:30 -08009800 if (phba->sli4_hba.hdr_rq)
9801 lpfc_rq_destroy(phba, phba->sli4_hba.hdr_rq,
9802 phba->sli4_hba.dat_rq);
9803
James Smartda0436e2009-05-22 14:51:39 -04009804 /* Unset mailbox command complete queue */
James Smart895427b2017-02-12 13:52:30 -08009805 if (phba->sli4_hba.mbx_cq)
9806 lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
9807
James Smartda0436e2009-05-22 14:51:39 -04009808 /* Unset ELS complete queue */
James Smart895427b2017-02-12 13:52:30 -08009809 if (phba->sli4_hba.els_cq)
9810 lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
9811
9812 /* Unset NVME LS complete queue */
9813 if (phba->sli4_hba.nvmels_cq)
9814 lpfc_cq_destroy(phba, phba->sli4_hba.nvmels_cq);
9815
James Smartbcb24f62017-11-20 16:00:36 -08009816 if (phba->nvmet_support) {
9817 /* Unset NVMET MRQ queue */
9818 if (phba->sli4_hba.nvmet_mrq_hdr) {
9819 for (qidx = 0; qidx < phba->cfg_nvmet_mrq; qidx++)
9820 lpfc_rq_destroy(
9821 phba,
James Smart2d7dbc42017-02-12 13:52:35 -08009822 phba->sli4_hba.nvmet_mrq_hdr[qidx],
9823 phba->sli4_hba.nvmet_mrq_data[qidx]);
James Smartbcb24f62017-11-20 16:00:36 -08009824 }
James Smart2d7dbc42017-02-12 13:52:35 -08009825
James Smartbcb24f62017-11-20 16:00:36 -08009826 /* Unset NVMET CQ Set complete queue */
9827 if (phba->sli4_hba.nvmet_cqset) {
9828 for (qidx = 0; qidx < phba->cfg_nvmet_mrq; qidx++)
9829 lpfc_cq_destroy(
9830 phba, phba->sli4_hba.nvmet_cqset[qidx]);
9831 }
James Smart2d7dbc42017-02-12 13:52:35 -08009832 }
9833
James Smartcdb42be2019-01-28 11:14:21 -08009834 /* Unset fast-path SLI4 queues */
9835 if (phba->sli4_hba.hdwq) {
James Smart657add42019-05-21 17:49:06 -07009836 /* Loop thru all Hardware Queues */
James Smartcdb42be2019-01-28 11:14:21 -08009837 for (qidx = 0; qidx < phba->cfg_hdw_queue; qidx++) {
James Smart657add42019-05-21 17:49:06 -07009838 /* Destroy the CQ/WQ corresponding to Hardware Queue */
James Smartcdb42be2019-01-28 11:14:21 -08009839 qp = &phba->sli4_hba.hdwq[qidx];
9840 lpfc_wq_destroy(phba, qp->fcp_wq);
9841 lpfc_wq_destroy(phba, qp->nvme_wq);
9842 lpfc_cq_destroy(phba, qp->fcp_cq);
9843 lpfc_cq_destroy(phba, qp->nvme_cq);
James Smart657add42019-05-21 17:49:06 -07009844 }
9845 /* Loop thru all IRQ vectors */
9846 for (qidx = 0; qidx < phba->cfg_irq_chann; qidx++) {
9847 /* Destroy the EQ corresponding to the IRQ vector */
9848 eq = phba->sli4_hba.hba_eq_hdl[qidx].eq;
9849 lpfc_eq_destroy(phba, eq);
James Smartcdb42be2019-01-28 11:14:21 -08009850 }
9851 }
James Smart895427b2017-02-12 13:52:30 -08009852
James Smart6a828b02019-01-28 11:14:31 -08009853 kfree(phba->sli4_hba.cq_lookup);
9854 phba->sli4_hba.cq_lookup = NULL;
9855 phba->sli4_hba.cq_max = 0;
James Smartda0436e2009-05-22 14:51:39 -04009856}
9857
9858/**
9859 * lpfc_sli4_cq_event_pool_create - Create completion-queue event free pool
9860 * @phba: pointer to lpfc hba data structure.
9861 *
9862 * This routine is invoked to allocate and set up a pool of completion queue
9863 * events. The body of the completion queue event is a completion queue entry
9864 * CQE. For now, this pool is used for the interrupt service routine to queue
9865 * the following HBA completion queue events for the worker thread to process:
9866 * - Mailbox asynchronous events
9867 * - Receive queue completion unsolicited events
9868 * Later, this can be used for all the slow-path events.
9869 *
9870 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02009871 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009872 * -ENOMEM - No available memory
James Smartda0436e2009-05-22 14:51:39 -04009873 **/
9874static int
9875lpfc_sli4_cq_event_pool_create(struct lpfc_hba *phba)
9876{
9877 struct lpfc_cq_event *cq_event;
9878 int i;
9879
9880 for (i = 0; i < (4 * phba->sli4_hba.cq_ecount); i++) {
9881 cq_event = kmalloc(sizeof(struct lpfc_cq_event), GFP_KERNEL);
9882 if (!cq_event)
9883 goto out_pool_create_fail;
9884 list_add_tail(&cq_event->list,
9885 &phba->sli4_hba.sp_cqe_event_pool);
9886 }
9887 return 0;
9888
9889out_pool_create_fail:
9890 lpfc_sli4_cq_event_pool_destroy(phba);
9891 return -ENOMEM;
9892}
9893
9894/**
9895 * lpfc_sli4_cq_event_pool_destroy - Free completion-queue event free pool
9896 * @phba: pointer to lpfc hba data structure.
9897 *
9898 * This routine is invoked to free the pool of completion queue events at
9899 * driver unload time. Note that, it is the responsibility of the driver
9900 * cleanup routine to free all the outstanding completion-queue events
9901 * allocated from this pool back into the pool before invoking this routine
9902 * to destroy the pool.
9903 **/
9904static void
9905lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *phba)
9906{
9907 struct lpfc_cq_event *cq_event, *next_cq_event;
9908
9909 list_for_each_entry_safe(cq_event, next_cq_event,
9910 &phba->sli4_hba.sp_cqe_event_pool, list) {
9911 list_del(&cq_event->list);
9912 kfree(cq_event);
9913 }
9914}
9915
9916/**
9917 * __lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
9918 * @phba: pointer to lpfc hba data structure.
9919 *
9920 * This routine is the lock free version of the API invoked to allocate a
9921 * completion-queue event from the free pool.
9922 *
9923 * Return: Pointer to the newly allocated completion-queue event if successful
9924 * NULL otherwise.
9925 **/
9926struct lpfc_cq_event *
9927__lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
9928{
9929 struct lpfc_cq_event *cq_event = NULL;
9930
9931 list_remove_head(&phba->sli4_hba.sp_cqe_event_pool, cq_event,
9932 struct lpfc_cq_event, list);
9933 return cq_event;
9934}
9935
9936/**
9937 * lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
9938 * @phba: pointer to lpfc hba data structure.
9939 *
9940 * This routine is the lock version of the API invoked to allocate a
9941 * completion-queue event from the free pool.
9942 *
9943 * Return: Pointer to the newly allocated completion-queue event if successful
9944 * NULL otherwise.
9945 **/
9946struct lpfc_cq_event *
9947lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
9948{
9949 struct lpfc_cq_event *cq_event;
9950 unsigned long iflags;
9951
9952 spin_lock_irqsave(&phba->hbalock, iflags);
9953 cq_event = __lpfc_sli4_cq_event_alloc(phba);
9954 spin_unlock_irqrestore(&phba->hbalock, iflags);
9955 return cq_event;
9956}
9957
9958/**
9959 * __lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
9960 * @phba: pointer to lpfc hba data structure.
9961 * @cq_event: pointer to the completion queue event to be freed.
9962 *
9963 * This routine is the lock free version of the API invoked to release a
9964 * completion-queue event back into the free pool.
9965 **/
9966void
9967__lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
9968 struct lpfc_cq_event *cq_event)
9969{
9970 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_cqe_event_pool);
9971}
9972
9973/**
9974 * lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
9975 * @phba: pointer to lpfc hba data structure.
9976 * @cq_event: pointer to the completion queue event to be freed.
9977 *
9978 * This routine is the lock version of the API invoked to release a
9979 * completion-queue event back into the free pool.
9980 **/
9981void
9982lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
9983 struct lpfc_cq_event *cq_event)
9984{
9985 unsigned long iflags;
9986 spin_lock_irqsave(&phba->hbalock, iflags);
9987 __lpfc_sli4_cq_event_release(phba, cq_event);
9988 spin_unlock_irqrestore(&phba->hbalock, iflags);
9989}
9990
9991/**
9992 * lpfc_sli4_cq_event_release_all - Release all cq events to the free pool
9993 * @phba: pointer to lpfc hba data structure.
9994 *
9995 * This routine is to free all the pending completion-queue events to the
9996 * back into the free pool for device reset.
9997 **/
9998static void
9999lpfc_sli4_cq_event_release_all(struct lpfc_hba *phba)
10000{
10001 LIST_HEAD(cqelist);
10002 struct lpfc_cq_event *cqe;
10003 unsigned long iflags;
10004
10005 /* Retrieve all the pending WCQEs from pending WCQE lists */
10006 spin_lock_irqsave(&phba->hbalock, iflags);
10007 /* Pending FCP XRI abort events */
10008 list_splice_init(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
10009 &cqelist);
10010 /* Pending ELS XRI abort events */
10011 list_splice_init(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
10012 &cqelist);
10013 /* Pending asynnc events */
10014 list_splice_init(&phba->sli4_hba.sp_asynce_work_queue,
10015 &cqelist);
10016 spin_unlock_irqrestore(&phba->hbalock, iflags);
10017
10018 while (!list_empty(&cqelist)) {
10019 list_remove_head(&cqelist, cqe, struct lpfc_cq_event, list);
10020 lpfc_sli4_cq_event_release(phba, cqe);
10021 }
10022}
10023
10024/**
10025 * lpfc_pci_function_reset - Reset pci function.
10026 * @phba: pointer to lpfc hba data structure.
10027 *
10028 * This routine is invoked to request a PCI function reset. It will destroys
10029 * all resources assigned to the PCI function which originates this request.
10030 *
10031 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020010032 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010033 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -040010034 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -040010035 **/
10036int
10037lpfc_pci_function_reset(struct lpfc_hba *phba)
10038{
10039 LPFC_MBOXQ_t *mboxq;
James Smart2fcee4b2010-12-15 17:57:46 -050010040 uint32_t rc = 0, if_type;
James Smartda0436e2009-05-22 14:51:39 -040010041 uint32_t shdr_status, shdr_add_status;
James Smart2f6fa2c2014-09-03 12:57:08 -040010042 uint32_t rdy_chk;
10043 uint32_t port_reset = 0;
James Smartda0436e2009-05-22 14:51:39 -040010044 union lpfc_sli4_cfg_shdr *shdr;
James Smart2fcee4b2010-12-15 17:57:46 -050010045 struct lpfc_register reg_data;
James Smart2b81f942012-03-01 22:37:18 -050010046 uint16_t devid;
James Smartda0436e2009-05-22 14:51:39 -040010047
James Smart2fcee4b2010-12-15 17:57:46 -050010048 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
10049 switch (if_type) {
10050 case LPFC_SLI_INTF_IF_TYPE_0:
10051 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
10052 GFP_KERNEL);
10053 if (!mboxq) {
10054 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10055 "0494 Unable to allocate memory for "
10056 "issuing SLI_FUNCTION_RESET mailbox "
10057 "command\n");
10058 return -ENOMEM;
10059 }
10060
10061 /* Setup PCI function reset mailbox-ioctl command */
10062 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
10063 LPFC_MBOX_OPCODE_FUNCTION_RESET, 0,
10064 LPFC_SLI4_MBX_EMBED);
10065 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
10066 shdr = (union lpfc_sli4_cfg_shdr *)
10067 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
10068 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10069 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
10070 &shdr->response);
10071 if (rc != MBX_TIMEOUT)
10072 mempool_free(mboxq, phba->mbox_mem_pool);
10073 if (shdr_status || shdr_add_status || rc) {
10074 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10075 "0495 SLI_FUNCTION_RESET mailbox "
10076 "failed with status x%x add_status x%x,"
10077 " mbx status x%x\n",
10078 shdr_status, shdr_add_status, rc);
10079 rc = -ENXIO;
10080 }
10081 break;
10082 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart27d6ac02018-02-22 08:18:42 -080010083 case LPFC_SLI_INTF_IF_TYPE_6:
James Smart2f6fa2c2014-09-03 12:57:08 -040010084wait:
10085 /*
10086 * Poll the Port Status Register and wait for RDY for
10087 * up to 30 seconds. If the port doesn't respond, treat
10088 * it as an error.
10089 */
James Smart77d093f2015-04-07 15:07:08 -040010090 for (rdy_chk = 0; rdy_chk < 1500; rdy_chk++) {
James Smart2f6fa2c2014-09-03 12:57:08 -040010091 if (lpfc_readl(phba->sli4_hba.u.if_type2.
10092 STATUSregaddr, &reg_data.word0)) {
10093 rc = -ENODEV;
10094 goto out;
10095 }
10096 if (bf_get(lpfc_sliport_status_rdy, &reg_data))
10097 break;
10098 msleep(20);
10099 }
10100
10101 if (!bf_get(lpfc_sliport_status_rdy, &reg_data)) {
10102 phba->work_status[0] = readl(
10103 phba->sli4_hba.u.if_type2.ERR1regaddr);
10104 phba->work_status[1] = readl(
10105 phba->sli4_hba.u.if_type2.ERR2regaddr);
10106 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10107 "2890 Port not ready, port status reg "
10108 "0x%x error 1=0x%x, error 2=0x%x\n",
10109 reg_data.word0,
10110 phba->work_status[0],
10111 phba->work_status[1]);
10112 rc = -ENODEV;
10113 goto out;
10114 }
10115
10116 if (!port_reset) {
10117 /*
10118 * Reset the port now
10119 */
James Smart2fcee4b2010-12-15 17:57:46 -050010120 reg_data.word0 = 0;
10121 bf_set(lpfc_sliport_ctrl_end, &reg_data,
10122 LPFC_SLIPORT_LITTLE_ENDIAN);
10123 bf_set(lpfc_sliport_ctrl_ip, &reg_data,
10124 LPFC_SLIPORT_INIT_PORT);
10125 writel(reg_data.word0, phba->sli4_hba.u.if_type2.
10126 CTRLregaddr);
James Smart8fcb8ac2012-03-01 22:35:58 -050010127 /* flush */
James Smart2b81f942012-03-01 22:37:18 -050010128 pci_read_config_word(phba->pcidev,
10129 PCI_DEVICE_ID, &devid);
James Smart2fcee4b2010-12-15 17:57:46 -050010130
James Smart2f6fa2c2014-09-03 12:57:08 -040010131 port_reset = 1;
10132 msleep(20);
10133 goto wait;
10134 } else if (bf_get(lpfc_sliport_status_rn, &reg_data)) {
10135 rc = -ENODEV;
10136 goto out;
James Smart2fcee4b2010-12-15 17:57:46 -050010137 }
10138 break;
James Smart2f6fa2c2014-09-03 12:57:08 -040010139
James Smart2fcee4b2010-12-15 17:57:46 -050010140 case LPFC_SLI_INTF_IF_TYPE_1:
10141 default:
10142 break;
James Smartda0436e2009-05-22 14:51:39 -040010143 }
10144
James Smart73d91e52011-10-10 21:32:10 -040010145out:
James Smart2fcee4b2010-12-15 17:57:46 -050010146 /* Catch the not-ready port failure after a port reset. */
James Smart2f6fa2c2014-09-03 12:57:08 -040010147 if (rc) {
James Smart229adb02013-04-17 20:16:51 -040010148 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10149 "3317 HBA not functional: IP Reset Failed "
James Smart2f6fa2c2014-09-03 12:57:08 -040010150 "try: echo fw_reset > board_mode\n");
James Smart2fcee4b2010-12-15 17:57:46 -050010151 rc = -ENODEV;
James Smart229adb02013-04-17 20:16:51 -040010152 }
James Smart2fcee4b2010-12-15 17:57:46 -050010153
James Smartda0436e2009-05-22 14:51:39 -040010154 return rc;
10155}
10156
10157/**
James Smartda0436e2009-05-22 14:51:39 -040010158 * lpfc_sli4_pci_mem_setup - Setup SLI4 HBA PCI memory space.
10159 * @phba: pointer to lpfc hba data structure.
10160 *
10161 * This routine is invoked to set up the PCI device memory space for device
10162 * with SLI-4 interface spec.
10163 *
10164 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020010165 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -040010166 * other values - error
10167 **/
10168static int
10169lpfc_sli4_pci_mem_setup(struct lpfc_hba *phba)
10170{
Christoph Hellwigf30e1bf2018-10-18 15:10:21 +020010171 struct pci_dev *pdev = phba->pcidev;
James Smartda0436e2009-05-22 14:51:39 -040010172 unsigned long bar0map_len, bar1map_len, bar2map_len;
Dan Carpenter3a487ff2019-03-07 08:33:44 +030010173 int error;
James Smart2fcee4b2010-12-15 17:57:46 -050010174 uint32_t if_type;
James Smartda0436e2009-05-22 14:51:39 -040010175
Christoph Hellwigf30e1bf2018-10-18 15:10:21 +020010176 if (!pdev)
Hannes Reinecke56de8352019-02-18 08:34:19 +010010177 return -ENODEV;
James Smartda0436e2009-05-22 14:51:39 -040010178
10179 /* Set the device DMA mask size */
Hannes Reinecke56de8352019-02-18 08:34:19 +010010180 error = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
10181 if (error)
10182 error = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
10183 if (error)
Christoph Hellwigf30e1bf2018-10-18 15:10:21 +020010184 return error;
James Smartda0436e2009-05-22 14:51:39 -040010185
James Smart2fcee4b2010-12-15 17:57:46 -050010186 /*
10187 * The BARs and register set definitions and offset locations are
10188 * dependent on the if_type.
10189 */
10190 if (pci_read_config_dword(pdev, LPFC_SLI_INTF,
10191 &phba->sli4_hba.sli_intf.word0)) {
Dan Carpenter3a487ff2019-03-07 08:33:44 +030010192 return -ENODEV;
James Smart2fcee4b2010-12-15 17:57:46 -050010193 }
10194
10195 /* There is no SLI3 failback for SLI4 devices. */
10196 if (bf_get(lpfc_sli_intf_valid, &phba->sli4_hba.sli_intf) !=
10197 LPFC_SLI_INTF_VALID) {
10198 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10199 "2894 SLI_INTF reg contents invalid "
10200 "sli_intf reg 0x%x\n",
10201 phba->sli4_hba.sli_intf.word0);
Dan Carpenter3a487ff2019-03-07 08:33:44 +030010202 return -ENODEV;
James Smart2fcee4b2010-12-15 17:57:46 -050010203 }
10204
10205 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
10206 /*
10207 * Get the bus address of SLI4 device Bar regions and the
10208 * number of bytes required by each mapping. The mapping of the
10209 * particular PCI BARs regions is dependent on the type of
10210 * SLI4 device.
James Smartda0436e2009-05-22 14:51:39 -040010211 */
James Smartf5ca6f22013-09-06 12:21:09 -040010212 if (pci_resource_start(pdev, PCI_64BIT_BAR0)) {
10213 phba->pci_bar0_map = pci_resource_start(pdev, PCI_64BIT_BAR0);
10214 bar0map_len = pci_resource_len(pdev, PCI_64BIT_BAR0);
James Smart2fcee4b2010-12-15 17:57:46 -050010215
10216 /*
10217 * Map SLI4 PCI Config Space Register base to a kernel virtual
10218 * addr
10219 */
10220 phba->sli4_hba.conf_regs_memmap_p =
10221 ioremap(phba->pci_bar0_map, bar0map_len);
10222 if (!phba->sli4_hba.conf_regs_memmap_p) {
10223 dev_printk(KERN_ERR, &pdev->dev,
10224 "ioremap failed for SLI4 PCI config "
10225 "registers.\n");
Dan Carpenter3a487ff2019-03-07 08:33:44 +030010226 return -ENODEV;
James Smart2fcee4b2010-12-15 17:57:46 -050010227 }
James Smartf5ca6f22013-09-06 12:21:09 -040010228 phba->pci_bar0_memmap_p = phba->sli4_hba.conf_regs_memmap_p;
James Smart2fcee4b2010-12-15 17:57:46 -050010229 /* Set up BAR0 PCI config space register memory map */
10230 lpfc_sli4_bar0_register_memmap(phba, if_type);
James Smart1dfb5a42010-02-12 14:40:50 -050010231 } else {
10232 phba->pci_bar0_map = pci_resource_start(pdev, 1);
10233 bar0map_len = pci_resource_len(pdev, 1);
James Smart27d6ac02018-02-22 08:18:42 -080010234 if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
James Smart2fcee4b2010-12-15 17:57:46 -050010235 dev_printk(KERN_ERR, &pdev->dev,
10236 "FATAL - No BAR0 mapping for SLI4, if_type 2\n");
Dan Carpenter3a487ff2019-03-07 08:33:44 +030010237 return -ENODEV;
James Smart2fcee4b2010-12-15 17:57:46 -050010238 }
10239 phba->sli4_hba.conf_regs_memmap_p =
James Smartda0436e2009-05-22 14:51:39 -040010240 ioremap(phba->pci_bar0_map, bar0map_len);
James Smart2fcee4b2010-12-15 17:57:46 -050010241 if (!phba->sli4_hba.conf_regs_memmap_p) {
10242 dev_printk(KERN_ERR, &pdev->dev,
10243 "ioremap failed for SLI4 PCI config "
10244 "registers.\n");
Dan Carpenter3a487ff2019-03-07 08:33:44 +030010245 return -ENODEV;
James Smart2fcee4b2010-12-15 17:57:46 -050010246 }
10247 lpfc_sli4_bar0_register_memmap(phba, if_type);
James Smartda0436e2009-05-22 14:51:39 -040010248 }
10249
James Smarte4b97942017-11-20 16:00:31 -080010250 if (if_type == LPFC_SLI_INTF_IF_TYPE_0) {
10251 if (pci_resource_start(pdev, PCI_64BIT_BAR2)) {
10252 /*
10253 * Map SLI4 if type 0 HBA Control Register base to a
10254 * kernel virtual address and setup the registers.
10255 */
10256 phba->pci_bar1_map = pci_resource_start(pdev,
10257 PCI_64BIT_BAR2);
10258 bar1map_len = pci_resource_len(pdev, PCI_64BIT_BAR2);
10259 phba->sli4_hba.ctrl_regs_memmap_p =
10260 ioremap(phba->pci_bar1_map,
10261 bar1map_len);
10262 if (!phba->sli4_hba.ctrl_regs_memmap_p) {
10263 dev_err(&pdev->dev,
10264 "ioremap failed for SLI4 HBA "
10265 "control registers.\n");
10266 error = -ENOMEM;
10267 goto out_iounmap_conf;
10268 }
10269 phba->pci_bar2_memmap_p =
10270 phba->sli4_hba.ctrl_regs_memmap_p;
James Smart27d6ac02018-02-22 08:18:42 -080010271 lpfc_sli4_bar1_register_memmap(phba, if_type);
James Smarte4b97942017-11-20 16:00:31 -080010272 } else {
10273 error = -ENOMEM;
James Smart2fcee4b2010-12-15 17:57:46 -050010274 goto out_iounmap_conf;
10275 }
James Smartda0436e2009-05-22 14:51:39 -040010276 }
10277
James Smart27d6ac02018-02-22 08:18:42 -080010278 if ((if_type == LPFC_SLI_INTF_IF_TYPE_6) &&
10279 (pci_resource_start(pdev, PCI_64BIT_BAR2))) {
10280 /*
10281 * Map SLI4 if type 6 HBA Doorbell Register base to a kernel
10282 * virtual address and setup the registers.
10283 */
10284 phba->pci_bar1_map = pci_resource_start(pdev, PCI_64BIT_BAR2);
10285 bar1map_len = pci_resource_len(pdev, PCI_64BIT_BAR2);
10286 phba->sli4_hba.drbl_regs_memmap_p =
10287 ioremap(phba->pci_bar1_map, bar1map_len);
10288 if (!phba->sli4_hba.drbl_regs_memmap_p) {
10289 dev_err(&pdev->dev,
10290 "ioremap failed for SLI4 HBA doorbell registers.\n");
Dan Carpenter3a487ff2019-03-07 08:33:44 +030010291 error = -ENOMEM;
James Smart27d6ac02018-02-22 08:18:42 -080010292 goto out_iounmap_conf;
10293 }
10294 phba->pci_bar2_memmap_p = phba->sli4_hba.drbl_regs_memmap_p;
10295 lpfc_sli4_bar1_register_memmap(phba, if_type);
10296 }
10297
James Smarte4b97942017-11-20 16:00:31 -080010298 if (if_type == LPFC_SLI_INTF_IF_TYPE_0) {
10299 if (pci_resource_start(pdev, PCI_64BIT_BAR4)) {
10300 /*
10301 * Map SLI4 if type 0 HBA Doorbell Register base to
10302 * a kernel virtual address and setup the registers.
10303 */
10304 phba->pci_bar2_map = pci_resource_start(pdev,
10305 PCI_64BIT_BAR4);
10306 bar2map_len = pci_resource_len(pdev, PCI_64BIT_BAR4);
10307 phba->sli4_hba.drbl_regs_memmap_p =
10308 ioremap(phba->pci_bar2_map,
10309 bar2map_len);
10310 if (!phba->sli4_hba.drbl_regs_memmap_p) {
10311 dev_err(&pdev->dev,
10312 "ioremap failed for SLI4 HBA"
10313 " doorbell registers.\n");
10314 error = -ENOMEM;
10315 goto out_iounmap_ctrl;
10316 }
10317 phba->pci_bar4_memmap_p =
10318 phba->sli4_hba.drbl_regs_memmap_p;
10319 error = lpfc_sli4_bar2_register_memmap(phba, LPFC_VF0);
10320 if (error)
10321 goto out_iounmap_all;
10322 } else {
10323 error = -ENOMEM;
James Smart2fcee4b2010-12-15 17:57:46 -050010324 goto out_iounmap_all;
James Smarte4b97942017-11-20 16:00:31 -080010325 }
James Smartda0436e2009-05-22 14:51:39 -040010326 }
10327
James Smart1351e692018-02-22 08:18:43 -080010328 if (if_type == LPFC_SLI_INTF_IF_TYPE_6 &&
10329 pci_resource_start(pdev, PCI_64BIT_BAR4)) {
10330 /*
10331 * Map SLI4 if type 6 HBA DPP Register base to a kernel
10332 * virtual address and setup the registers.
10333 */
10334 phba->pci_bar2_map = pci_resource_start(pdev, PCI_64BIT_BAR4);
10335 bar2map_len = pci_resource_len(pdev, PCI_64BIT_BAR4);
10336 phba->sli4_hba.dpp_regs_memmap_p =
10337 ioremap(phba->pci_bar2_map, bar2map_len);
10338 if (!phba->sli4_hba.dpp_regs_memmap_p) {
10339 dev_err(&pdev->dev,
10340 "ioremap failed for SLI4 HBA dpp registers.\n");
Dan Carpenter3a487ff2019-03-07 08:33:44 +030010341 error = -ENOMEM;
James Smart1351e692018-02-22 08:18:43 -080010342 goto out_iounmap_ctrl;
10343 }
10344 phba->pci_bar4_memmap_p = phba->sli4_hba.dpp_regs_memmap_p;
10345 }
10346
James Smartb71413d2018-02-22 08:18:40 -080010347 /* Set up the EQ/CQ register handeling functions now */
James Smart27d6ac02018-02-22 08:18:42 -080010348 switch (if_type) {
10349 case LPFC_SLI_INTF_IF_TYPE_0:
10350 case LPFC_SLI_INTF_IF_TYPE_2:
James Smartb71413d2018-02-22 08:18:40 -080010351 phba->sli4_hba.sli4_eq_clr_intr = lpfc_sli4_eq_clr_intr;
James Smart32517fc2019-01-28 11:14:33 -080010352 phba->sli4_hba.sli4_write_eq_db = lpfc_sli4_write_eq_db;
10353 phba->sli4_hba.sli4_write_cq_db = lpfc_sli4_write_cq_db;
James Smart27d6ac02018-02-22 08:18:42 -080010354 break;
10355 case LPFC_SLI_INTF_IF_TYPE_6:
10356 phba->sli4_hba.sli4_eq_clr_intr = lpfc_sli4_if6_eq_clr_intr;
James Smart32517fc2019-01-28 11:14:33 -080010357 phba->sli4_hba.sli4_write_eq_db = lpfc_sli4_if6_write_eq_db;
10358 phba->sli4_hba.sli4_write_cq_db = lpfc_sli4_if6_write_cq_db;
James Smart27d6ac02018-02-22 08:18:42 -080010359 break;
10360 default:
10361 break;
James Smartb71413d2018-02-22 08:18:40 -080010362 }
10363
James Smartda0436e2009-05-22 14:51:39 -040010364 return 0;
10365
10366out_iounmap_all:
10367 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
10368out_iounmap_ctrl:
10369 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
10370out_iounmap_conf:
10371 iounmap(phba->sli4_hba.conf_regs_memmap_p);
Dan Carpenter3a487ff2019-03-07 08:33:44 +030010372
James Smartda0436e2009-05-22 14:51:39 -040010373 return error;
10374}
10375
10376/**
10377 * lpfc_sli4_pci_mem_unset - Unset SLI4 HBA PCI memory space.
10378 * @phba: pointer to lpfc hba data structure.
10379 *
10380 * This routine is invoked to unset the PCI device memory space for device
10381 * with SLI-4 interface spec.
10382 **/
10383static void
10384lpfc_sli4_pci_mem_unset(struct lpfc_hba *phba)
10385{
James Smart2e90f4b2011-12-13 13:22:37 -050010386 uint32_t if_type;
10387 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
James Smartda0436e2009-05-22 14:51:39 -040010388
James Smart2e90f4b2011-12-13 13:22:37 -050010389 switch (if_type) {
10390 case LPFC_SLI_INTF_IF_TYPE_0:
10391 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
10392 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
10393 iounmap(phba->sli4_hba.conf_regs_memmap_p);
10394 break;
10395 case LPFC_SLI_INTF_IF_TYPE_2:
10396 iounmap(phba->sli4_hba.conf_regs_memmap_p);
10397 break;
James Smart27d6ac02018-02-22 08:18:42 -080010398 case LPFC_SLI_INTF_IF_TYPE_6:
10399 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
10400 iounmap(phba->sli4_hba.conf_regs_memmap_p);
10401 break;
James Smart2e90f4b2011-12-13 13:22:37 -050010402 case LPFC_SLI_INTF_IF_TYPE_1:
10403 default:
10404 dev_printk(KERN_ERR, &phba->pcidev->dev,
10405 "FATAL - unsupported SLI4 interface type - %d\n",
10406 if_type);
10407 break;
10408 }
James Smartda0436e2009-05-22 14:51:39 -040010409}
10410
10411/**
James Smart3772a992009-05-22 14:50:54 -040010412 * lpfc_sli_enable_msix - Enable MSI-X interrupt mode on SLI-3 device
10413 * @phba: pointer to lpfc hba data structure.
10414 *
10415 * This routine is invoked to enable the MSI-X interrupt vectors to device
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010416 * with SLI-3 interface specs.
James Smart3772a992009-05-22 14:50:54 -040010417 *
10418 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020010419 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -040010420 * other values - error
10421 **/
10422static int
10423lpfc_sli_enable_msix(struct lpfc_hba *phba)
10424{
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010425 int rc;
James Smart3772a992009-05-22 14:50:54 -040010426 LPFC_MBOXQ_t *pmb;
10427
10428 /* Set up MSI-X multi-message vectors */
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010429 rc = pci_alloc_irq_vectors(phba->pcidev,
10430 LPFC_MSIX_VECTORS, LPFC_MSIX_VECTORS, PCI_IRQ_MSIX);
10431 if (rc < 0) {
James Smart3772a992009-05-22 14:50:54 -040010432 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10433 "0420 PCI enable MSI-X failed (%d)\n", rc);
Alexander Gordeev029165a2014-07-16 20:05:15 +020010434 goto vec_fail_out;
James Smart3772a992009-05-22 14:50:54 -040010435 }
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010436
James Smart3772a992009-05-22 14:50:54 -040010437 /*
10438 * Assign MSI-X vectors to interrupt handlers
10439 */
10440
10441 /* vector-0 is associated to slow-path handler */
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010442 rc = request_irq(pci_irq_vector(phba->pcidev, 0),
James Smarted243d32015-05-21 13:55:25 -040010443 &lpfc_sli_sp_intr_handler, 0,
James Smart3772a992009-05-22 14:50:54 -040010444 LPFC_SP_DRIVER_HANDLER_NAME, phba);
10445 if (rc) {
10446 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
10447 "0421 MSI-X slow-path request_irq failed "
10448 "(%d)\n", rc);
10449 goto msi_fail_out;
10450 }
10451
10452 /* vector-1 is associated to fast-path handler */
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010453 rc = request_irq(pci_irq_vector(phba->pcidev, 1),
James Smarted243d32015-05-21 13:55:25 -040010454 &lpfc_sli_fp_intr_handler, 0,
James Smart3772a992009-05-22 14:50:54 -040010455 LPFC_FP_DRIVER_HANDLER_NAME, phba);
10456
10457 if (rc) {
10458 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
10459 "0429 MSI-X fast-path request_irq failed "
10460 "(%d)\n", rc);
10461 goto irq_fail_out;
10462 }
10463
10464 /*
10465 * Configure HBA MSI-X attention conditions to messages
10466 */
10467 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10468
10469 if (!pmb) {
10470 rc = -ENOMEM;
10471 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10472 "0474 Unable to allocate memory for issuing "
10473 "MBOX_CONFIG_MSI command\n");
10474 goto mem_fail_out;
10475 }
10476 rc = lpfc_config_msi(phba, pmb);
10477 if (rc)
10478 goto mbx_fail_out;
10479 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
10480 if (rc != MBX_SUCCESS) {
10481 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
10482 "0351 Config MSI mailbox command failed, "
10483 "mbxCmd x%x, mbxStatus x%x\n",
10484 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus);
10485 goto mbx_fail_out;
10486 }
10487
10488 /* Free memory allocated for mailbox command */
10489 mempool_free(pmb, phba->mbox_mem_pool);
10490 return rc;
10491
10492mbx_fail_out:
10493 /* Free memory allocated for mailbox command */
10494 mempool_free(pmb, phba->mbox_mem_pool);
10495
10496mem_fail_out:
10497 /* free the irq already requested */
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010498 free_irq(pci_irq_vector(phba->pcidev, 1), phba);
James Smart3772a992009-05-22 14:50:54 -040010499
10500irq_fail_out:
10501 /* free the irq already requested */
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010502 free_irq(pci_irq_vector(phba->pcidev, 0), phba);
James Smart3772a992009-05-22 14:50:54 -040010503
10504msi_fail_out:
10505 /* Unconfigure MSI-X capability structure */
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010506 pci_free_irq_vectors(phba->pcidev);
Alexander Gordeev029165a2014-07-16 20:05:15 +020010507
10508vec_fail_out:
James Smart3772a992009-05-22 14:50:54 -040010509 return rc;
10510}
10511
10512/**
James Smart3772a992009-05-22 14:50:54 -040010513 * lpfc_sli_enable_msi - Enable MSI interrupt mode on SLI-3 device.
10514 * @phba: pointer to lpfc hba data structure.
10515 *
10516 * This routine is invoked to enable the MSI interrupt mode to device with
10517 * SLI-3 interface spec. The kernel function pci_enable_msi() is called to
10518 * enable the MSI vector. The device driver is responsible for calling the
10519 * request_irq() to register MSI vector with a interrupt the handler, which
10520 * is done in this function.
10521 *
10522 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020010523 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -040010524 * other values - error
10525 */
10526static int
10527lpfc_sli_enable_msi(struct lpfc_hba *phba)
10528{
10529 int rc;
10530
10531 rc = pci_enable_msi(phba->pcidev);
10532 if (!rc)
10533 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10534 "0462 PCI enable MSI mode success.\n");
10535 else {
10536 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10537 "0471 PCI enable MSI mode failed (%d)\n", rc);
10538 return rc;
10539 }
10540
10541 rc = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
James Smarted243d32015-05-21 13:55:25 -040010542 0, LPFC_DRIVER_NAME, phba);
James Smart3772a992009-05-22 14:50:54 -040010543 if (rc) {
10544 pci_disable_msi(phba->pcidev);
10545 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
10546 "0478 MSI request_irq failed (%d)\n", rc);
10547 }
10548 return rc;
10549}
10550
10551/**
James Smart3772a992009-05-22 14:50:54 -040010552 * lpfc_sli_enable_intr - Enable device interrupt to SLI-3 device.
10553 * @phba: pointer to lpfc hba data structure.
10554 *
10555 * This routine is invoked to enable device interrupt and associate driver's
10556 * interrupt handler(s) to interrupt vector(s) to device with SLI-3 interface
10557 * spec. Depends on the interrupt mode configured to the driver, the driver
10558 * will try to fallback from the configured interrupt mode to an interrupt
10559 * mode which is supported by the platform, kernel, and device in the order
10560 * of:
10561 * MSI-X -> MSI -> IRQ.
10562 *
10563 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020010564 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -040010565 * other values - error
10566 **/
10567static uint32_t
10568lpfc_sli_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
10569{
10570 uint32_t intr_mode = LPFC_INTR_ERROR;
10571 int retval;
10572
10573 if (cfg_mode == 2) {
10574 /* Need to issue conf_port mbox cmd before conf_msi mbox cmd */
10575 retval = lpfc_sli_config_port(phba, LPFC_SLI_REV3);
10576 if (!retval) {
10577 /* Now, try to enable MSI-X interrupt mode */
10578 retval = lpfc_sli_enable_msix(phba);
10579 if (!retval) {
10580 /* Indicate initialization to MSI-X mode */
10581 phba->intr_type = MSIX;
10582 intr_mode = 2;
10583 }
10584 }
10585 }
10586
10587 /* Fallback to MSI if MSI-X initialization failed */
10588 if (cfg_mode >= 1 && phba->intr_type == NONE) {
10589 retval = lpfc_sli_enable_msi(phba);
10590 if (!retval) {
10591 /* Indicate initialization to MSI mode */
10592 phba->intr_type = MSI;
10593 intr_mode = 1;
10594 }
10595 }
10596
10597 /* Fallback to INTx if both MSI-X/MSI initalization failed */
10598 if (phba->intr_type == NONE) {
10599 retval = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
10600 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
10601 if (!retval) {
10602 /* Indicate initialization to INTx mode */
10603 phba->intr_type = INTx;
10604 intr_mode = 0;
10605 }
10606 }
10607 return intr_mode;
10608}
10609
10610/**
10611 * lpfc_sli_disable_intr - Disable device interrupt to SLI-3 device.
10612 * @phba: pointer to lpfc hba data structure.
10613 *
10614 * This routine is invoked to disable device interrupt and disassociate the
10615 * driver's interrupt handler(s) from interrupt vector(s) to device with
10616 * SLI-3 interface spec. Depending on the interrupt mode, the driver will
10617 * release the interrupt vector(s) for the message signaled interrupt.
10618 **/
10619static void
10620lpfc_sli_disable_intr(struct lpfc_hba *phba)
10621{
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010622 int nr_irqs, i;
10623
James Smart3772a992009-05-22 14:50:54 -040010624 if (phba->intr_type == MSIX)
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010625 nr_irqs = LPFC_MSIX_VECTORS;
10626 else
10627 nr_irqs = 1;
10628
10629 for (i = 0; i < nr_irqs; i++)
10630 free_irq(pci_irq_vector(phba->pcidev, i), phba);
10631 pci_free_irq_vectors(phba->pcidev);
James Smart3772a992009-05-22 14:50:54 -040010632
10633 /* Reset interrupt management states */
10634 phba->intr_type = NONE;
10635 phba->sli.slistat.sli_intr = 0;
James Smart3772a992009-05-22 14:50:54 -040010636}
10637
10638/**
James Smart657add42019-05-21 17:49:06 -070010639 * lpfc_find_cpu_handle - Find the CPU that corresponds to the specified Queue
James Smart6a828b02019-01-28 11:14:31 -080010640 * @phba: pointer to lpfc hba data structure.
10641 * @id: EQ vector index or Hardware Queue index
10642 * @match: LPFC_FIND_BY_EQ = match by EQ
10643 * LPFC_FIND_BY_HDWQ = match by Hardware Queue
James Smart657add42019-05-21 17:49:06 -070010644 * Return the CPU that matches the selection criteria
James Smart6a828b02019-01-28 11:14:31 -080010645 */
10646static uint16_t
10647lpfc_find_cpu_handle(struct lpfc_hba *phba, uint16_t id, int match)
10648{
10649 struct lpfc_vector_map_info *cpup;
10650 int cpu;
10651
James Smart657add42019-05-21 17:49:06 -070010652 /* Loop through all CPUs */
James Smart222e9232019-01-28 11:14:35 -080010653 for_each_present_cpu(cpu) {
10654 cpup = &phba->sli4_hba.cpu_map[cpu];
James Smart657add42019-05-21 17:49:06 -070010655
10656 /* If we are matching by EQ, there may be multiple CPUs using
10657 * using the same vector, so select the one with
10658 * LPFC_CPU_FIRST_IRQ set.
10659 */
James Smart6a828b02019-01-28 11:14:31 -080010660 if ((match == LPFC_FIND_BY_EQ) &&
James Smart657add42019-05-21 17:49:06 -070010661 (cpup->flag & LPFC_CPU_FIRST_IRQ) &&
James Smart6a828b02019-01-28 11:14:31 -080010662 (cpup->irq != LPFC_VECTOR_MAP_EMPTY) &&
10663 (cpup->eq == id))
10664 return cpu;
James Smart657add42019-05-21 17:49:06 -070010665
10666 /* If matching by HDWQ, select the first CPU that matches */
James Smart6a828b02019-01-28 11:14:31 -080010667 if ((match == LPFC_FIND_BY_HDWQ) && (cpup->hdwq == id))
10668 return cpu;
James Smart6a828b02019-01-28 11:14:31 -080010669 }
10670 return 0;
10671}
10672
James Smart6a828b02019-01-28 11:14:31 -080010673#ifdef CONFIG_X86
10674/**
10675 * lpfc_find_hyper - Determine if the CPU map entry is hyper-threaded
10676 * @phba: pointer to lpfc hba data structure.
10677 * @cpu: CPU map index
10678 * @phys_id: CPU package physical id
10679 * @core_id: CPU core id
10680 */
10681static int
10682lpfc_find_hyper(struct lpfc_hba *phba, int cpu,
10683 uint16_t phys_id, uint16_t core_id)
10684{
10685 struct lpfc_vector_map_info *cpup;
10686 int idx;
10687
James Smart222e9232019-01-28 11:14:35 -080010688 for_each_present_cpu(idx) {
10689 cpup = &phba->sli4_hba.cpu_map[idx];
James Smart6a828b02019-01-28 11:14:31 -080010690 /* Does the cpup match the one we are looking for */
10691 if ((cpup->phys_id == phys_id) &&
10692 (cpup->core_id == core_id) &&
James Smart222e9232019-01-28 11:14:35 -080010693 (cpu != idx))
James Smart6a828b02019-01-28 11:14:31 -080010694 return 1;
James Smart6a828b02019-01-28 11:14:31 -080010695 }
10696 return 0;
10697}
10698#endif
10699
10700/**
James Smart895427b2017-02-12 13:52:30 -080010701 * lpfc_cpu_affinity_check - Check vector CPU affinity mappings
James Smart7bb03bb2013-04-17 20:19:16 -040010702 * @phba: pointer to lpfc hba data structure.
James Smart895427b2017-02-12 13:52:30 -080010703 * @vectors: number of msix vectors allocated.
James Smart7bb03bb2013-04-17 20:19:16 -040010704 *
James Smart895427b2017-02-12 13:52:30 -080010705 * The routine will figure out the CPU affinity assignment for every
James Smart6a828b02019-01-28 11:14:31 -080010706 * MSI-X vector allocated for the HBA.
James Smart895427b2017-02-12 13:52:30 -080010707 * In addition, the CPU to IO channel mapping will be calculated
10708 * and the phba->sli4_hba.cpu_map array will reflect this.
James Smart7bb03bb2013-04-17 20:19:16 -040010709 */
James Smart895427b2017-02-12 13:52:30 -080010710static void
10711lpfc_cpu_affinity_check(struct lpfc_hba *phba, int vectors)
James Smart7bb03bb2013-04-17 20:19:16 -040010712{
James Smartd9954a22019-05-21 17:49:05 -070010713 int i, cpu, idx, new_cpu, start_cpu, first_cpu;
James Smart6a828b02019-01-28 11:14:31 -080010714 int max_phys_id, min_phys_id;
10715 int max_core_id, min_core_id;
James Smart7bb03bb2013-04-17 20:19:16 -040010716 struct lpfc_vector_map_info *cpup;
James Smartd9954a22019-05-21 17:49:05 -070010717 struct lpfc_vector_map_info *new_cpup;
James Smart75508a82019-01-28 11:14:34 -080010718 const struct cpumask *maskp;
James Smart7bb03bb2013-04-17 20:19:16 -040010719#ifdef CONFIG_X86
10720 struct cpuinfo_x86 *cpuinfo;
10721#endif
James Smart7bb03bb2013-04-17 20:19:16 -040010722
10723 /* Init cpu_map array */
James Smartd9954a22019-05-21 17:49:05 -070010724 for_each_possible_cpu(cpu) {
10725 cpup = &phba->sli4_hba.cpu_map[cpu];
10726 cpup->phys_id = LPFC_VECTOR_MAP_EMPTY;
10727 cpup->core_id = LPFC_VECTOR_MAP_EMPTY;
10728 cpup->hdwq = LPFC_VECTOR_MAP_EMPTY;
10729 cpup->eq = LPFC_VECTOR_MAP_EMPTY;
10730 cpup->irq = LPFC_VECTOR_MAP_EMPTY;
10731 cpup->flag = 0;
10732 }
James Smart7bb03bb2013-04-17 20:19:16 -040010733
James Smart6a828b02019-01-28 11:14:31 -080010734 max_phys_id = 0;
James Smartd9954a22019-05-21 17:49:05 -070010735 min_phys_id = LPFC_VECTOR_MAP_EMPTY;
James Smart6a828b02019-01-28 11:14:31 -080010736 max_core_id = 0;
James Smartd9954a22019-05-21 17:49:05 -070010737 min_core_id = LPFC_VECTOR_MAP_EMPTY;
James Smart7bb03bb2013-04-17 20:19:16 -040010738
10739 /* Update CPU map with physical id and core id of each CPU */
James Smart222e9232019-01-28 11:14:35 -080010740 for_each_present_cpu(cpu) {
10741 cpup = &phba->sli4_hba.cpu_map[cpu];
James Smart7bb03bb2013-04-17 20:19:16 -040010742#ifdef CONFIG_X86
10743 cpuinfo = &cpu_data(cpu);
10744 cpup->phys_id = cpuinfo->phys_proc_id;
10745 cpup->core_id = cpuinfo->cpu_core_id;
James Smartd9954a22019-05-21 17:49:05 -070010746 if (lpfc_find_hyper(phba, cpu, cpup->phys_id, cpup->core_id))
10747 cpup->flag |= LPFC_CPU_MAP_HYPER;
James Smart7bb03bb2013-04-17 20:19:16 -040010748#else
10749 /* No distinction between CPUs for other platforms */
10750 cpup->phys_id = 0;
James Smart6a828b02019-01-28 11:14:31 -080010751 cpup->core_id = cpu;
James Smart7bb03bb2013-04-17 20:19:16 -040010752#endif
James Smart7bb03bb2013-04-17 20:19:16 -040010753
James Smartb3295c22019-01-28 11:14:30 -080010754 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10755 "3328 CPU physid %d coreid %d\n",
10756 cpup->phys_id, cpup->core_id);
James Smart6a828b02019-01-28 11:14:31 -080010757
10758 if (cpup->phys_id > max_phys_id)
10759 max_phys_id = cpup->phys_id;
10760 if (cpup->phys_id < min_phys_id)
10761 min_phys_id = cpup->phys_id;
10762
10763 if (cpup->core_id > max_core_id)
10764 max_core_id = cpup->core_id;
10765 if (cpup->core_id < min_core_id)
10766 min_core_id = cpup->core_id;
James Smart7bb03bb2013-04-17 20:19:16 -040010767 }
James Smartb3295c22019-01-28 11:14:30 -080010768
James Smart32517fc2019-01-28 11:14:33 -080010769 for_each_possible_cpu(i) {
10770 struct lpfc_eq_intr_info *eqi =
10771 per_cpu_ptr(phba->sli4_hba.eq_info, i);
10772
10773 INIT_LIST_HEAD(&eqi->list);
10774 eqi->icnt = 0;
10775 }
10776
James Smartd9954a22019-05-21 17:49:05 -070010777 /* This loop sets up all CPUs that are affinitized with a
10778 * irq vector assigned to the driver. All affinitized CPUs
James Smart657add42019-05-21 17:49:06 -070010779 * will get a link to that vectors IRQ and EQ.
James Smartd9954a22019-05-21 17:49:05 -070010780 */
James Smart75508a82019-01-28 11:14:34 -080010781 for (idx = 0; idx < phba->cfg_irq_chann; idx++) {
James Smart657add42019-05-21 17:49:06 -070010782 /* Get a CPU mask for all CPUs affinitized to this vector */
James Smart75508a82019-01-28 11:14:34 -080010783 maskp = pci_irq_get_affinity(phba->pcidev, idx);
10784 if (!maskp)
10785 continue;
10786
James Smart657add42019-05-21 17:49:06 -070010787 i = 0;
10788 /* Loop through all CPUs associated with vector idx */
James Smart75508a82019-01-28 11:14:34 -080010789 for_each_cpu_and(cpu, maskp, cpu_present_mask) {
James Smart657add42019-05-21 17:49:06 -070010790 /* Set the EQ index and IRQ for that vector */
James Smart75508a82019-01-28 11:14:34 -080010791 cpup = &phba->sli4_hba.cpu_map[cpu];
James Smart6a828b02019-01-28 11:14:31 -080010792 cpup->eq = idx;
James Smart6a828b02019-01-28 11:14:31 -080010793 cpup->irq = pci_irq_vector(phba->pcidev, idx);
James Smartb3295c22019-01-28 11:14:30 -080010794
James Smart657add42019-05-21 17:49:06 -070010795 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart6a828b02019-01-28 11:14:31 -080010796 "3336 Set Affinity: CPU %d "
James Smart657add42019-05-21 17:49:06 -070010797 "irq %d eq %d\n",
10798 cpu, cpup->irq, cpup->eq);
10799
10800 /* If this is the first CPU thats assigned to this
10801 * vector, set LPFC_CPU_FIRST_IRQ.
10802 */
10803 if (!i)
10804 cpup->flag |= LPFC_CPU_FIRST_IRQ;
10805 i++;
James Smart6a828b02019-01-28 11:14:31 -080010806 }
James Smartb3295c22019-01-28 11:14:30 -080010807 }
James Smartd9954a22019-05-21 17:49:05 -070010808
10809 /* After looking at each irq vector assigned to this pcidev, its
10810 * possible to see that not ALL CPUs have been accounted for.
James Smart657add42019-05-21 17:49:06 -070010811 * Next we will set any unassigned (unaffinitized) cpu map
10812 * entries to a IRQ on the same phys_id.
James Smartd9954a22019-05-21 17:49:05 -070010813 */
10814 first_cpu = cpumask_first(cpu_present_mask);
10815 start_cpu = first_cpu;
10816
10817 for_each_present_cpu(cpu) {
10818 cpup = &phba->sli4_hba.cpu_map[cpu];
10819
10820 /* Is this CPU entry unassigned */
10821 if (cpup->eq == LPFC_VECTOR_MAP_EMPTY) {
10822 /* Mark CPU as IRQ not assigned by the kernel */
10823 cpup->flag |= LPFC_CPU_MAP_UNASSIGN;
10824
James Smart657add42019-05-21 17:49:06 -070010825 /* If so, find a new_cpup thats on the the SAME
James Smartd9954a22019-05-21 17:49:05 -070010826 * phys_id as cpup. start_cpu will start where we
10827 * left off so all unassigned entries don't get assgined
10828 * the IRQ of the first entry.
10829 */
10830 new_cpu = start_cpu;
10831 for (i = 0; i < phba->sli4_hba.num_present_cpu; i++) {
10832 new_cpup = &phba->sli4_hba.cpu_map[new_cpu];
10833 if (!(new_cpup->flag & LPFC_CPU_MAP_UNASSIGN) &&
10834 (new_cpup->irq != LPFC_VECTOR_MAP_EMPTY) &&
10835 (new_cpup->phys_id == cpup->phys_id))
10836 goto found_same;
10837 new_cpu = cpumask_next(
10838 new_cpu, cpu_present_mask);
10839 if (new_cpu == nr_cpumask_bits)
10840 new_cpu = first_cpu;
10841 }
10842 /* At this point, we leave the CPU as unassigned */
10843 continue;
10844found_same:
10845 /* We found a matching phys_id, so copy the IRQ info */
10846 cpup->eq = new_cpup->eq;
James Smartd9954a22019-05-21 17:49:05 -070010847 cpup->irq = new_cpup->irq;
10848
10849 /* Bump start_cpu to the next slot to minmize the
10850 * chance of having multiple unassigned CPU entries
10851 * selecting the same IRQ.
10852 */
10853 start_cpu = cpumask_next(new_cpu, cpu_present_mask);
10854 if (start_cpu == nr_cpumask_bits)
10855 start_cpu = first_cpu;
10856
James Smart657add42019-05-21 17:49:06 -070010857 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smartd9954a22019-05-21 17:49:05 -070010858 "3337 Set Affinity: CPU %d "
James Smart657add42019-05-21 17:49:06 -070010859 "irq %d from id %d same "
James Smartd9954a22019-05-21 17:49:05 -070010860 "phys_id (%d)\n",
James Smart657add42019-05-21 17:49:06 -070010861 cpu, cpup->irq, new_cpu, cpup->phys_id);
James Smartd9954a22019-05-21 17:49:05 -070010862 }
10863 }
10864
10865 /* Set any unassigned cpu map entries to a IRQ on any phys_id */
10866 start_cpu = first_cpu;
10867
10868 for_each_present_cpu(cpu) {
10869 cpup = &phba->sli4_hba.cpu_map[cpu];
10870
10871 /* Is this entry unassigned */
10872 if (cpup->eq == LPFC_VECTOR_MAP_EMPTY) {
10873 /* Mark it as IRQ not assigned by the kernel */
10874 cpup->flag |= LPFC_CPU_MAP_UNASSIGN;
10875
James Smart657add42019-05-21 17:49:06 -070010876 /* If so, find a new_cpup thats on ANY phys_id
James Smartd9954a22019-05-21 17:49:05 -070010877 * as the cpup. start_cpu will start where we
10878 * left off so all unassigned entries don't get
10879 * assigned the IRQ of the first entry.
10880 */
10881 new_cpu = start_cpu;
10882 for (i = 0; i < phba->sli4_hba.num_present_cpu; i++) {
10883 new_cpup = &phba->sli4_hba.cpu_map[new_cpu];
10884 if (!(new_cpup->flag & LPFC_CPU_MAP_UNASSIGN) &&
10885 (new_cpup->irq != LPFC_VECTOR_MAP_EMPTY))
10886 goto found_any;
10887 new_cpu = cpumask_next(
10888 new_cpu, cpu_present_mask);
10889 if (new_cpu == nr_cpumask_bits)
10890 new_cpu = first_cpu;
10891 }
10892 /* We should never leave an entry unassigned */
10893 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10894 "3339 Set Affinity: CPU %d "
James Smart657add42019-05-21 17:49:06 -070010895 "irq %d UNASSIGNED\n",
10896 cpup->hdwq, cpup->irq);
James Smartd9954a22019-05-21 17:49:05 -070010897 continue;
10898found_any:
10899 /* We found an available entry, copy the IRQ info */
10900 cpup->eq = new_cpup->eq;
James Smartd9954a22019-05-21 17:49:05 -070010901 cpup->irq = new_cpup->irq;
10902
10903 /* Bump start_cpu to the next slot to minmize the
10904 * chance of having multiple unassigned CPU entries
10905 * selecting the same IRQ.
10906 */
10907 start_cpu = cpumask_next(new_cpu, cpu_present_mask);
10908 if (start_cpu == nr_cpumask_bits)
10909 start_cpu = first_cpu;
10910
James Smart657add42019-05-21 17:49:06 -070010911 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smartd9954a22019-05-21 17:49:05 -070010912 "3338 Set Affinity: CPU %d "
James Smart657add42019-05-21 17:49:06 -070010913 "irq %d from id %d (%d/%d)\n",
10914 cpu, cpup->irq, new_cpu,
James Smartd9954a22019-05-21 17:49:05 -070010915 new_cpup->phys_id, new_cpup->core_id);
10916 }
10917 }
James Smart657add42019-05-21 17:49:06 -070010918
10919 /* Finally we need to associate a hdwq with each cpu_map entry
10920 * This will be 1 to 1 - hdwq to cpu, unless there are less
10921 * hardware queues then CPUs. For that case we will just round-robin
10922 * the available hardware queues as they get assigned to CPUs.
10923 */
10924 idx = 0;
10925 start_cpu = 0;
10926 for_each_present_cpu(cpu) {
10927 cpup = &phba->sli4_hba.cpu_map[cpu];
10928 if (idx >= phba->cfg_hdw_queue) {
10929 /* We need to reuse a Hardware Queue for another CPU,
10930 * so be smart about it and pick one that has its
10931 * IRQ/EQ mapped to the same phys_id (CPU package).
10932 * and core_id.
10933 */
10934 new_cpu = start_cpu;
10935 for (i = 0; i < phba->sli4_hba.num_present_cpu; i++) {
10936 new_cpup = &phba->sli4_hba.cpu_map[new_cpu];
10937 if ((new_cpup->hdwq != LPFC_VECTOR_MAP_EMPTY) &&
10938 (new_cpup->phys_id == cpup->phys_id) &&
10939 (new_cpup->core_id == cpup->core_id))
10940 goto found_hdwq;
10941 new_cpu = cpumask_next(
10942 new_cpu, cpu_present_mask);
10943 if (new_cpu == nr_cpumask_bits)
10944 new_cpu = first_cpu;
10945 }
10946
10947 /* If we can't match both phys_id and core_id,
10948 * settle for just a phys_id match.
10949 */
10950 new_cpu = start_cpu;
10951 for (i = 0; i < phba->sli4_hba.num_present_cpu; i++) {
10952 new_cpup = &phba->sli4_hba.cpu_map[new_cpu];
10953 if ((new_cpup->hdwq != LPFC_VECTOR_MAP_EMPTY) &&
10954 (new_cpup->phys_id == cpup->phys_id))
10955 goto found_hdwq;
10956 new_cpu = cpumask_next(
10957 new_cpu, cpu_present_mask);
10958 if (new_cpu == nr_cpumask_bits)
10959 new_cpu = first_cpu;
10960 }
10961
10962 /* Otherwise just round robin on cfg_hdw_queue */
10963 cpup->hdwq = idx % phba->cfg_hdw_queue;
10964 goto logit;
10965found_hdwq:
10966 /* We found an available entry, copy the IRQ info */
10967 start_cpu = cpumask_next(new_cpu, cpu_present_mask);
10968 if (start_cpu == nr_cpumask_bits)
10969 start_cpu = first_cpu;
10970 cpup->hdwq = new_cpup->hdwq;
10971 } else {
10972 /* 1 to 1, CPU to hdwq */
10973 cpup->hdwq = idx;
10974 }
10975logit:
10976 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10977 "3335 Set Affinity: CPU %d (phys %d core %d): "
10978 "hdwq %d eq %d irq %d flg x%x\n",
10979 cpu, cpup->phys_id, cpup->core_id,
10980 cpup->hdwq, cpup->eq, cpup->irq, cpup->flag);
10981 idx++;
10982 }
10983
10984 /* The cpu_map array will be used later during initialization
10985 * when EQ / CQ / WQs are allocated and configured.
10986 */
James Smartb3295c22019-01-28 11:14:30 -080010987 return;
James Smart7bb03bb2013-04-17 20:19:16 -040010988}
James Smart7bb03bb2013-04-17 20:19:16 -040010989
10990/**
James Smartda0436e2009-05-22 14:51:39 -040010991 * lpfc_sli4_enable_msix - Enable MSI-X interrupt mode to SLI-4 device
10992 * @phba: pointer to lpfc hba data structure.
10993 *
10994 * This routine is invoked to enable the MSI-X interrupt vectors to device
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010995 * with SLI-4 interface spec.
James Smartda0436e2009-05-22 14:51:39 -040010996 *
10997 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020010998 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -040010999 * other values - error
11000 **/
11001static int
11002lpfc_sli4_enable_msix(struct lpfc_hba *phba)
11003{
James Smart75baf692010-06-08 18:31:21 -040011004 int vectors, rc, index;
James Smartb83d0052017-06-01 21:07:05 -070011005 char *name;
James Smartda0436e2009-05-22 14:51:39 -040011006
11007 /* Set up MSI-X multi-message vectors */
James Smart6a828b02019-01-28 11:14:31 -080011008 vectors = phba->cfg_irq_chann;
Christoph Hellwig45ffac12017-02-12 13:52:26 -080011009
James Smartf358dd02017-02-12 13:52:34 -080011010 rc = pci_alloc_irq_vectors(phba->pcidev,
James Smart75508a82019-01-28 11:14:34 -080011011 1,
James Smartf358dd02017-02-12 13:52:34 -080011012 vectors, PCI_IRQ_MSIX | PCI_IRQ_AFFINITY);
Alexander Gordeev4f871e12014-09-03 12:56:29 -040011013 if (rc < 0) {
James Smartda0436e2009-05-22 14:51:39 -040011014 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
11015 "0484 PCI enable MSI-X failed (%d)\n", rc);
Alexander Gordeev029165a2014-07-16 20:05:15 +020011016 goto vec_fail_out;
James Smartda0436e2009-05-22 14:51:39 -040011017 }
Alexander Gordeev4f871e12014-09-03 12:56:29 -040011018 vectors = rc;
James Smart75baf692010-06-08 18:31:21 -040011019
James Smart7bb03bb2013-04-17 20:19:16 -040011020 /* Assign MSI-X vectors to interrupt handlers */
James Smart67d12732012-08-03 12:36:13 -040011021 for (index = 0; index < vectors; index++) {
James Smartb83d0052017-06-01 21:07:05 -070011022 name = phba->sli4_hba.hba_eq_hdl[index].handler_name;
11023 memset(name, 0, LPFC_SLI4_HANDLER_NAME_SZ);
11024 snprintf(name, LPFC_SLI4_HANDLER_NAME_SZ,
James Smart4305f182012-08-03 12:36:33 -040011025 LPFC_DRIVER_HANDLER_NAME"%d", index);
James Smartda0436e2009-05-22 14:51:39 -040011026
James Smart895427b2017-02-12 13:52:30 -080011027 phba->sli4_hba.hba_eq_hdl[index].idx = index;
11028 phba->sli4_hba.hba_eq_hdl[index].phba = phba;
James Smart7370d102019-01-28 11:14:20 -080011029 rc = request_irq(pci_irq_vector(phba->pcidev, index),
11030 &lpfc_sli4_hba_intr_handler, 0,
11031 name,
11032 &phba->sli4_hba.hba_eq_hdl[index]);
James Smartda0436e2009-05-22 14:51:39 -040011033 if (rc) {
11034 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
11035 "0486 MSI-X fast-path (%d) "
11036 "request_irq failed (%d)\n", index, rc);
11037 goto cfg_fail_out;
11038 }
11039 }
11040
James Smart6a828b02019-01-28 11:14:31 -080011041 if (vectors != phba->cfg_irq_chann) {
James Smart82c3e9b2012-09-29 11:29:50 -040011042 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11043 "3238 Reducing IO channels to match number of "
11044 "MSI-X vectors, requested %d got %d\n",
James Smart6a828b02019-01-28 11:14:31 -080011045 phba->cfg_irq_chann, vectors);
11046 if (phba->cfg_irq_chann > vectors)
11047 phba->cfg_irq_chann = vectors;
James Smart982ab122019-03-12 16:30:10 -070011048 if (phba->nvmet_support && (phba->cfg_nvmet_mrq > vectors))
James Smartcdb42be2019-01-28 11:14:21 -080011049 phba->cfg_nvmet_mrq = vectors;
James Smart82c3e9b2012-09-29 11:29:50 -040011050 }
James Smart7bb03bb2013-04-17 20:19:16 -040011051
James Smartda0436e2009-05-22 14:51:39 -040011052 return rc;
11053
11054cfg_fail_out:
11055 /* free the irq already requested */
James Smart895427b2017-02-12 13:52:30 -080011056 for (--index; index >= 0; index--)
11057 free_irq(pci_irq_vector(phba->pcidev, index),
11058 &phba->sli4_hba.hba_eq_hdl[index]);
James Smartda0436e2009-05-22 14:51:39 -040011059
James Smartda0436e2009-05-22 14:51:39 -040011060 /* Unconfigure MSI-X capability structure */
Christoph Hellwig45ffac12017-02-12 13:52:26 -080011061 pci_free_irq_vectors(phba->pcidev);
Alexander Gordeev029165a2014-07-16 20:05:15 +020011062
11063vec_fail_out:
James Smartda0436e2009-05-22 14:51:39 -040011064 return rc;
11065}
11066
11067/**
James Smartda0436e2009-05-22 14:51:39 -040011068 * lpfc_sli4_enable_msi - Enable MSI interrupt mode to SLI-4 device
11069 * @phba: pointer to lpfc hba data structure.
11070 *
11071 * This routine is invoked to enable the MSI interrupt mode to device with
11072 * SLI-4 interface spec. The kernel function pci_enable_msi() is called
11073 * to enable the MSI vector. The device driver is responsible for calling
11074 * the request_irq() to register MSI vector with a interrupt the handler,
11075 * which is done in this function.
11076 *
11077 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020011078 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -040011079 * other values - error
11080 **/
11081static int
11082lpfc_sli4_enable_msi(struct lpfc_hba *phba)
11083{
11084 int rc, index;
11085
11086 rc = pci_enable_msi(phba->pcidev);
11087 if (!rc)
11088 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
11089 "0487 PCI enable MSI mode success.\n");
11090 else {
11091 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
11092 "0488 PCI enable MSI mode failed (%d)\n", rc);
11093 return rc;
11094 }
11095
11096 rc = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
James Smarted243d32015-05-21 13:55:25 -040011097 0, LPFC_DRIVER_NAME, phba);
James Smartda0436e2009-05-22 14:51:39 -040011098 if (rc) {
11099 pci_disable_msi(phba->pcidev);
11100 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
11101 "0490 MSI request_irq failed (%d)\n", rc);
James Smart75baf692010-06-08 18:31:21 -040011102 return rc;
James Smartda0436e2009-05-22 14:51:39 -040011103 }
11104
James Smart6a828b02019-01-28 11:14:31 -080011105 for (index = 0; index < phba->cfg_irq_chann; index++) {
James Smart895427b2017-02-12 13:52:30 -080011106 phba->sli4_hba.hba_eq_hdl[index].idx = index;
11107 phba->sli4_hba.hba_eq_hdl[index].phba = phba;
James Smartda0436e2009-05-22 14:51:39 -040011108 }
11109
James Smart75baf692010-06-08 18:31:21 -040011110 return 0;
James Smartda0436e2009-05-22 14:51:39 -040011111}
11112
11113/**
James Smartda0436e2009-05-22 14:51:39 -040011114 * lpfc_sli4_enable_intr - Enable device interrupt to SLI-4 device
11115 * @phba: pointer to lpfc hba data structure.
11116 *
11117 * This routine is invoked to enable device interrupt and associate driver's
11118 * interrupt handler(s) to interrupt vector(s) to device with SLI-4
11119 * interface spec. Depends on the interrupt mode configured to the driver,
11120 * the driver will try to fallback from the configured interrupt mode to an
11121 * interrupt mode which is supported by the platform, kernel, and device in
11122 * the order of:
11123 * MSI-X -> MSI -> IRQ.
11124 *
11125 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020011126 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -040011127 * other values - error
11128 **/
11129static uint32_t
11130lpfc_sli4_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
11131{
11132 uint32_t intr_mode = LPFC_INTR_ERROR;
James Smart895427b2017-02-12 13:52:30 -080011133 int retval, idx;
James Smartda0436e2009-05-22 14:51:39 -040011134
11135 if (cfg_mode == 2) {
11136 /* Preparation before conf_msi mbox cmd */
11137 retval = 0;
11138 if (!retval) {
11139 /* Now, try to enable MSI-X interrupt mode */
11140 retval = lpfc_sli4_enable_msix(phba);
11141 if (!retval) {
11142 /* Indicate initialization to MSI-X mode */
11143 phba->intr_type = MSIX;
11144 intr_mode = 2;
11145 }
11146 }
11147 }
11148
11149 /* Fallback to MSI if MSI-X initialization failed */
11150 if (cfg_mode >= 1 && phba->intr_type == NONE) {
11151 retval = lpfc_sli4_enable_msi(phba);
11152 if (!retval) {
11153 /* Indicate initialization to MSI mode */
11154 phba->intr_type = MSI;
11155 intr_mode = 1;
11156 }
11157 }
11158
11159 /* Fallback to INTx if both MSI-X/MSI initalization failed */
11160 if (phba->intr_type == NONE) {
11161 retval = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
11162 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
11163 if (!retval) {
James Smart895427b2017-02-12 13:52:30 -080011164 struct lpfc_hba_eq_hdl *eqhdl;
11165
James Smartda0436e2009-05-22 14:51:39 -040011166 /* Indicate initialization to INTx mode */
11167 phba->intr_type = INTx;
11168 intr_mode = 0;
James Smart895427b2017-02-12 13:52:30 -080011169
James Smart6a828b02019-01-28 11:14:31 -080011170 for (idx = 0; idx < phba->cfg_irq_chann; idx++) {
James Smart895427b2017-02-12 13:52:30 -080011171 eqhdl = &phba->sli4_hba.hba_eq_hdl[idx];
11172 eqhdl->idx = idx;
11173 eqhdl->phba = phba;
James Smart1ba981f2014-02-20 09:56:45 -050011174 }
James Smartda0436e2009-05-22 14:51:39 -040011175 }
11176 }
11177 return intr_mode;
11178}
11179
11180/**
11181 * lpfc_sli4_disable_intr - Disable device interrupt to SLI-4 device
11182 * @phba: pointer to lpfc hba data structure.
11183 *
11184 * This routine is invoked to disable device interrupt and disassociate
11185 * the driver's interrupt handler(s) from interrupt vector(s) to device
11186 * with SLI-4 interface spec. Depending on the interrupt mode, the driver
11187 * will release the interrupt vector(s) for the message signaled interrupt.
11188 **/
11189static void
11190lpfc_sli4_disable_intr(struct lpfc_hba *phba)
11191{
11192 /* Disable the currently initialized interrupt mode */
Christoph Hellwig45ffac12017-02-12 13:52:26 -080011193 if (phba->intr_type == MSIX) {
11194 int index;
11195
11196 /* Free up MSI-X multi-message vectors */
James Smart6a828b02019-01-28 11:14:31 -080011197 for (index = 0; index < phba->cfg_irq_chann; index++) {
James Smartb3295c22019-01-28 11:14:30 -080011198 irq_set_affinity_hint(
11199 pci_irq_vector(phba->pcidev, index),
11200 NULL);
James Smart895427b2017-02-12 13:52:30 -080011201 free_irq(pci_irq_vector(phba->pcidev, index),
11202 &phba->sli4_hba.hba_eq_hdl[index]);
James Smartb3295c22019-01-28 11:14:30 -080011203 }
Christoph Hellwig45ffac12017-02-12 13:52:26 -080011204 } else {
James Smartda0436e2009-05-22 14:51:39 -040011205 free_irq(phba->pcidev->irq, phba);
Christoph Hellwig45ffac12017-02-12 13:52:26 -080011206 }
11207
11208 pci_free_irq_vectors(phba->pcidev);
James Smartda0436e2009-05-22 14:51:39 -040011209
11210 /* Reset interrupt management states */
11211 phba->intr_type = NONE;
11212 phba->sli.slistat.sli_intr = 0;
James Smartda0436e2009-05-22 14:51:39 -040011213}
11214
11215/**
James Smart3772a992009-05-22 14:50:54 -040011216 * lpfc_unset_hba - Unset SLI3 hba device initialization
11217 * @phba: pointer to lpfc hba data structure.
11218 *
11219 * This routine is invoked to unset the HBA device initialization steps to
11220 * a device with SLI-3 interface spec.
11221 **/
11222static void
11223lpfc_unset_hba(struct lpfc_hba *phba)
11224{
11225 struct lpfc_vport *vport = phba->pport;
11226 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
11227
11228 spin_lock_irq(shost->host_lock);
11229 vport->load_flag |= FC_UNLOADING;
11230 spin_unlock_irq(shost->host_lock);
11231
James Smart72859902012-01-18 16:25:38 -050011232 kfree(phba->vpi_bmask);
11233 kfree(phba->vpi_ids);
11234
James Smart3772a992009-05-22 14:50:54 -040011235 lpfc_stop_hba_timers(phba);
11236
11237 phba->pport->work_port_events = 0;
11238
11239 lpfc_sli_hba_down(phba);
11240
11241 lpfc_sli_brdrestart(phba);
11242
11243 lpfc_sli_disable_intr(phba);
11244
11245 return;
11246}
11247
11248/**
James Smart5af5eee2010-10-22 11:06:38 -040011249 * lpfc_sli4_xri_exchange_busy_wait - Wait for device XRI exchange busy
11250 * @phba: Pointer to HBA context object.
11251 *
11252 * This function is called in the SLI4 code path to wait for completion
11253 * of device's XRIs exchange busy. It will check the XRI exchange busy
11254 * on outstanding FCP and ELS I/Os every 10ms for up to 10 seconds; after
11255 * that, it will check the XRI exchange busy on outstanding FCP and ELS
11256 * I/Os every 30 seconds, log error message, and wait forever. Only when
11257 * all XRI exchange busy complete, the driver unload shall proceed with
11258 * invoking the function reset ioctl mailbox command to the CNA and the
11259 * the rest of the driver unload resource release.
11260 **/
11261static void
11262lpfc_sli4_xri_exchange_busy_wait(struct lpfc_hba *phba)
11263{
James Smart5e5b5112019-01-28 11:14:22 -080011264 struct lpfc_sli4_hdw_queue *qp;
11265 int idx, ccnt, fcnt;
James Smart5af5eee2010-10-22 11:06:38 -040011266 int wait_time = 0;
James Smart5e5b5112019-01-28 11:14:22 -080011267 int io_xri_cmpl = 1;
James Smart86c67372017-04-21 16:05:04 -070011268 int nvmet_xri_cmpl = 1;
James Smart895427b2017-02-12 13:52:30 -080011269 int fcp_xri_cmpl = 1;
James Smart5af5eee2010-10-22 11:06:38 -040011270 int els_xri_cmpl = list_empty(&phba->sli4_hba.lpfc_abts_els_sgl_list);
11271
James Smartc3725bd2017-11-20 16:00:42 -080011272 /* Driver just aborted IOs during the hba_unset process. Pause
11273 * here to give the HBA time to complete the IO and get entries
11274 * into the abts lists.
11275 */
11276 msleep(LPFC_XRI_EXCH_BUSY_WAIT_T1 * 5);
11277
11278 /* Wait for NVME pending IO to flush back to transport. */
11279 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
11280 lpfc_nvme_wait_for_io_drain(phba);
11281
James Smart5e5b5112019-01-28 11:14:22 -080011282 ccnt = 0;
11283 fcnt = 0;
11284 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
11285 qp = &phba->sli4_hba.hdwq[idx];
11286 fcp_xri_cmpl = list_empty(
11287 &qp->lpfc_abts_scsi_buf_list);
11288 if (!fcp_xri_cmpl) /* if list is NOT empty */
11289 fcnt++;
11290 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
11291 io_xri_cmpl = list_empty(
11292 &qp->lpfc_abts_nvme_buf_list);
11293 if (!io_xri_cmpl) /* if list is NOT empty */
11294 ccnt++;
11295 }
11296 }
11297 if (ccnt)
11298 io_xri_cmpl = 0;
11299 if (fcnt)
11300 fcp_xri_cmpl = 0;
11301
James Smart86c67372017-04-21 16:05:04 -070011302 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
James Smart86c67372017-04-21 16:05:04 -070011303 nvmet_xri_cmpl =
11304 list_empty(&phba->sli4_hba.lpfc_abts_nvmet_ctx_list);
11305 }
James Smart895427b2017-02-12 13:52:30 -080011306
James Smart5e5b5112019-01-28 11:14:22 -080011307 while (!fcp_xri_cmpl || !els_xri_cmpl || !io_xri_cmpl ||
James Smartf358dd02017-02-12 13:52:34 -080011308 !nvmet_xri_cmpl) {
James Smart5af5eee2010-10-22 11:06:38 -040011309 if (wait_time > LPFC_XRI_EXCH_BUSY_WAIT_TMO) {
James Smart68c9b552018-06-26 08:24:25 -070011310 if (!nvmet_xri_cmpl)
11311 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11312 "6424 NVMET XRI exchange busy "
11313 "wait time: %d seconds.\n",
11314 wait_time/1000);
James Smart5e5b5112019-01-28 11:14:22 -080011315 if (!io_xri_cmpl)
James Smart895427b2017-02-12 13:52:30 -080011316 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11317 "6100 NVME XRI exchange busy "
11318 "wait time: %d seconds.\n",
11319 wait_time/1000);
James Smart5af5eee2010-10-22 11:06:38 -040011320 if (!fcp_xri_cmpl)
11321 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11322 "2877 FCP XRI exchange busy "
11323 "wait time: %d seconds.\n",
11324 wait_time/1000);
11325 if (!els_xri_cmpl)
11326 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11327 "2878 ELS XRI exchange busy "
11328 "wait time: %d seconds.\n",
11329 wait_time/1000);
11330 msleep(LPFC_XRI_EXCH_BUSY_WAIT_T2);
11331 wait_time += LPFC_XRI_EXCH_BUSY_WAIT_T2;
11332 } else {
11333 msleep(LPFC_XRI_EXCH_BUSY_WAIT_T1);
11334 wait_time += LPFC_XRI_EXCH_BUSY_WAIT_T1;
11335 }
James Smart5e5b5112019-01-28 11:14:22 -080011336
11337 ccnt = 0;
11338 fcnt = 0;
11339 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
11340 qp = &phba->sli4_hba.hdwq[idx];
11341 fcp_xri_cmpl = list_empty(
11342 &qp->lpfc_abts_scsi_buf_list);
11343 if (!fcp_xri_cmpl) /* if list is NOT empty */
11344 fcnt++;
11345 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
11346 io_xri_cmpl = list_empty(
11347 &qp->lpfc_abts_nvme_buf_list);
11348 if (!io_xri_cmpl) /* if list is NOT empty */
11349 ccnt++;
11350 }
11351 }
11352 if (ccnt)
11353 io_xri_cmpl = 0;
11354 if (fcnt)
11355 fcp_xri_cmpl = 0;
11356
James Smart86c67372017-04-21 16:05:04 -070011357 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
James Smart86c67372017-04-21 16:05:04 -070011358 nvmet_xri_cmpl = list_empty(
11359 &phba->sli4_hba.lpfc_abts_nvmet_ctx_list);
11360 }
James Smart5af5eee2010-10-22 11:06:38 -040011361 els_xri_cmpl =
11362 list_empty(&phba->sli4_hba.lpfc_abts_els_sgl_list);
James Smartf358dd02017-02-12 13:52:34 -080011363
James Smart5af5eee2010-10-22 11:06:38 -040011364 }
11365}
11366
11367/**
James Smartda0436e2009-05-22 14:51:39 -040011368 * lpfc_sli4_hba_unset - Unset the fcoe hba
11369 * @phba: Pointer to HBA context object.
11370 *
11371 * This function is called in the SLI4 code path to reset the HBA's FCoE
11372 * function. The caller is not required to hold any lock. This routine
11373 * issues PCI function reset mailbox command to reset the FCoE function.
11374 * At the end of the function, it calls lpfc_hba_down_post function to
11375 * free any pending commands.
11376 **/
11377static void
11378lpfc_sli4_hba_unset(struct lpfc_hba *phba)
11379{
11380 int wait_cnt = 0;
11381 LPFC_MBOXQ_t *mboxq;
James Smart912e3ac2011-05-24 11:42:11 -040011382 struct pci_dev *pdev = phba->pcidev;
James Smartda0436e2009-05-22 14:51:39 -040011383
11384 lpfc_stop_hba_timers(phba);
James Smartcdb42be2019-01-28 11:14:21 -080011385 if (phba->pport)
11386 phba->sli4_hba.intr_enable = 0;
James Smartda0436e2009-05-22 14:51:39 -040011387
11388 /*
11389 * Gracefully wait out the potential current outstanding asynchronous
11390 * mailbox command.
11391 */
11392
11393 /* First, block any pending async mailbox command from posted */
11394 spin_lock_irq(&phba->hbalock);
11395 phba->sli.sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
11396 spin_unlock_irq(&phba->hbalock);
11397 /* Now, trying to wait it out if we can */
11398 while (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
11399 msleep(10);
11400 if (++wait_cnt > LPFC_ACTIVE_MBOX_WAIT_CNT)
11401 break;
11402 }
11403 /* Forcefully release the outstanding mailbox command if timed out */
11404 if (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
11405 spin_lock_irq(&phba->hbalock);
11406 mboxq = phba->sli.mbox_active;
11407 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
11408 __lpfc_mbox_cmpl_put(phba, mboxq);
11409 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
11410 phba->sli.mbox_active = NULL;
11411 spin_unlock_irq(&phba->hbalock);
11412 }
11413
James Smart5af5eee2010-10-22 11:06:38 -040011414 /* Abort all iocbs associated with the hba */
11415 lpfc_sli_hba_iocb_abort(phba);
11416
11417 /* Wait for completion of device XRI exchange busy */
11418 lpfc_sli4_xri_exchange_busy_wait(phba);
11419
James Smartda0436e2009-05-22 14:51:39 -040011420 /* Disable PCI subsystem interrupt */
11421 lpfc_sli4_disable_intr(phba);
11422
James Smart912e3ac2011-05-24 11:42:11 -040011423 /* Disable SR-IOV if enabled */
11424 if (phba->cfg_sriov_nr_virtfn)
11425 pci_disable_sriov(pdev);
11426
James Smartda0436e2009-05-22 14:51:39 -040011427 /* Stop kthread signal shall trigger work_done one more time */
11428 kthread_stop(phba->worker_thread);
11429
James Smartd2cc9bc2018-09-10 10:30:50 -070011430 /* Disable FW logging to host memory */
James Smart1165a5c2018-11-29 16:09:39 -080011431 lpfc_ras_stop_fwlog(phba);
James Smartd2cc9bc2018-09-10 10:30:50 -070011432
James Smartd1f525a2017-04-21 16:04:55 -070011433 /* Unset the queues shared with the hardware then release all
11434 * allocated resources.
11435 */
11436 lpfc_sli4_queue_unset(phba);
11437 lpfc_sli4_queue_destroy(phba);
11438
James Smart3677a3a2010-09-29 11:19:14 -040011439 /* Reset SLI4 HBA FCoE function */
11440 lpfc_pci_function_reset(phba);
11441
James Smart1165a5c2018-11-29 16:09:39 -080011442 /* Free RAS DMA memory */
11443 if (phba->ras_fwlog.ras_enabled)
11444 lpfc_sli4_ras_dma_free(phba);
11445
James Smartda0436e2009-05-22 14:51:39 -040011446 /* Stop the SLI4 device port */
James Smart1ffdd2c2019-03-04 15:27:51 -080011447 if (phba->pport)
11448 phba->pport->work_port_events = 0;
James Smartda0436e2009-05-22 14:51:39 -040011449}
11450
James Smart28baac72010-02-12 14:42:03 -050011451 /**
11452 * lpfc_pc_sli4_params_get - Get the SLI4_PARAMS port capabilities.
11453 * @phba: Pointer to HBA context object.
11454 * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
11455 *
11456 * This function is called in the SLI4 code path to read the port's
11457 * sli4 capabilities.
11458 *
11459 * This function may be be called from any context that can block-wait
11460 * for the completion. The expectation is that this routine is called
11461 * typically from probe_one or from the online routine.
11462 **/
11463int
11464lpfc_pc_sli4_params_get(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
11465{
11466 int rc;
11467 struct lpfc_mqe *mqe;
11468 struct lpfc_pc_sli4_params *sli4_params;
11469 uint32_t mbox_tmo;
11470
11471 rc = 0;
11472 mqe = &mboxq->u.mqe;
11473
11474 /* Read the port's SLI4 Parameters port capabilities */
James Smartfedd3b72011-02-16 12:39:24 -050011475 lpfc_pc_sli4_params(mboxq);
James Smart28baac72010-02-12 14:42:03 -050011476 if (!phba->sli4_hba.intr_enable)
11477 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
11478 else {
James Smarta183a152011-10-10 21:32:43 -040011479 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
James Smart28baac72010-02-12 14:42:03 -050011480 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
11481 }
11482
11483 if (unlikely(rc))
11484 return 1;
11485
11486 sli4_params = &phba->sli4_hba.pc_sli4_params;
11487 sli4_params->if_type = bf_get(if_type, &mqe->un.sli4_params);
11488 sli4_params->sli_rev = bf_get(sli_rev, &mqe->un.sli4_params);
11489 sli4_params->sli_family = bf_get(sli_family, &mqe->un.sli4_params);
11490 sli4_params->featurelevel_1 = bf_get(featurelevel_1,
11491 &mqe->un.sli4_params);
11492 sli4_params->featurelevel_2 = bf_get(featurelevel_2,
11493 &mqe->un.sli4_params);
11494 sli4_params->proto_types = mqe->un.sli4_params.word3;
11495 sli4_params->sge_supp_len = mqe->un.sli4_params.sge_supp_len;
11496 sli4_params->if_page_sz = bf_get(if_page_sz, &mqe->un.sli4_params);
11497 sli4_params->rq_db_window = bf_get(rq_db_window, &mqe->un.sli4_params);
11498 sli4_params->loopbk_scope = bf_get(loopbk_scope, &mqe->un.sli4_params);
11499 sli4_params->eq_pages_max = bf_get(eq_pages, &mqe->un.sli4_params);
11500 sli4_params->eqe_size = bf_get(eqe_size, &mqe->un.sli4_params);
11501 sli4_params->cq_pages_max = bf_get(cq_pages, &mqe->un.sli4_params);
11502 sli4_params->cqe_size = bf_get(cqe_size, &mqe->un.sli4_params);
11503 sli4_params->mq_pages_max = bf_get(mq_pages, &mqe->un.sli4_params);
11504 sli4_params->mqe_size = bf_get(mqe_size, &mqe->un.sli4_params);
11505 sli4_params->mq_elem_cnt = bf_get(mq_elem_cnt, &mqe->un.sli4_params);
11506 sli4_params->wq_pages_max = bf_get(wq_pages, &mqe->un.sli4_params);
11507 sli4_params->wqe_size = bf_get(wqe_size, &mqe->un.sli4_params);
11508 sli4_params->rq_pages_max = bf_get(rq_pages, &mqe->un.sli4_params);
11509 sli4_params->rqe_size = bf_get(rqe_size, &mqe->un.sli4_params);
11510 sli4_params->hdr_pages_max = bf_get(hdr_pages, &mqe->un.sli4_params);
11511 sli4_params->hdr_size = bf_get(hdr_size, &mqe->un.sli4_params);
11512 sli4_params->hdr_pp_align = bf_get(hdr_pp_align, &mqe->un.sli4_params);
11513 sli4_params->sgl_pages_max = bf_get(sgl_pages, &mqe->un.sli4_params);
11514 sli4_params->sgl_pp_align = bf_get(sgl_pp_align, &mqe->un.sli4_params);
James Smart05580562011-05-24 11:40:48 -040011515
11516 /* Make sure that sge_supp_len can be handled by the driver */
11517 if (sli4_params->sge_supp_len > LPFC_MAX_SGE_SIZE)
11518 sli4_params->sge_supp_len = LPFC_MAX_SGE_SIZE;
11519
James Smart28baac72010-02-12 14:42:03 -050011520 return rc;
11521}
11522
James Smartda0436e2009-05-22 14:51:39 -040011523/**
James Smartfedd3b72011-02-16 12:39:24 -050011524 * lpfc_get_sli4_parameters - Get the SLI4 Config PARAMETERS.
11525 * @phba: Pointer to HBA context object.
11526 * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
11527 *
11528 * This function is called in the SLI4 code path to read the port's
11529 * sli4 capabilities.
11530 *
11531 * This function may be be called from any context that can block-wait
11532 * for the completion. The expectation is that this routine is called
11533 * typically from probe_one or from the online routine.
11534 **/
11535int
11536lpfc_get_sli4_parameters(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
11537{
11538 int rc;
11539 struct lpfc_mqe *mqe = &mboxq->u.mqe;
11540 struct lpfc_pc_sli4_params *sli4_params;
James Smarta183a152011-10-10 21:32:43 -040011541 uint32_t mbox_tmo;
James Smartfedd3b72011-02-16 12:39:24 -050011542 int length;
James Smartbf316c72018-04-09 14:24:28 -070011543 bool exp_wqcq_pages = true;
James Smartfedd3b72011-02-16 12:39:24 -050011544 struct lpfc_sli4_parameters *mbx_sli4_parameters;
11545
James Smart6d368e52011-05-24 11:44:12 -040011546 /*
11547 * By default, the driver assumes the SLI4 port requires RPI
11548 * header postings. The SLI4_PARAM response will correct this
11549 * assumption.
11550 */
11551 phba->sli4_hba.rpi_hdrs_in_use = 1;
11552
James Smartfedd3b72011-02-16 12:39:24 -050011553 /* Read the port's SLI4 Config Parameters */
11554 length = (sizeof(struct lpfc_mbx_get_sli4_parameters) -
11555 sizeof(struct lpfc_sli4_cfg_mhdr));
11556 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
11557 LPFC_MBOX_OPCODE_GET_SLI4_PARAMETERS,
11558 length, LPFC_SLI4_MBX_EMBED);
11559 if (!phba->sli4_hba.intr_enable)
11560 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smarta183a152011-10-10 21:32:43 -040011561 else {
11562 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
11563 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
11564 }
James Smartfedd3b72011-02-16 12:39:24 -050011565 if (unlikely(rc))
11566 return rc;
11567 sli4_params = &phba->sli4_hba.pc_sli4_params;
11568 mbx_sli4_parameters = &mqe->un.get_sli4_parameters.sli4_parameters;
11569 sli4_params->if_type = bf_get(cfg_if_type, mbx_sli4_parameters);
11570 sli4_params->sli_rev = bf_get(cfg_sli_rev, mbx_sli4_parameters);
11571 sli4_params->sli_family = bf_get(cfg_sli_family, mbx_sli4_parameters);
11572 sli4_params->featurelevel_1 = bf_get(cfg_sli_hint_1,
11573 mbx_sli4_parameters);
11574 sli4_params->featurelevel_2 = bf_get(cfg_sli_hint_2,
11575 mbx_sli4_parameters);
11576 if (bf_get(cfg_phwq, mbx_sli4_parameters))
11577 phba->sli3_options |= LPFC_SLI4_PHWQ_ENABLED;
11578 else
11579 phba->sli3_options &= ~LPFC_SLI4_PHWQ_ENABLED;
11580 sli4_params->sge_supp_len = mbx_sli4_parameters->sge_supp_len;
11581 sli4_params->loopbk_scope = bf_get(loopbk_scope, mbx_sli4_parameters);
James Smart1ba981f2014-02-20 09:56:45 -050011582 sli4_params->oas_supported = bf_get(cfg_oas, mbx_sli4_parameters);
James Smartfedd3b72011-02-16 12:39:24 -050011583 sli4_params->cqv = bf_get(cfg_cqv, mbx_sli4_parameters);
11584 sli4_params->mqv = bf_get(cfg_mqv, mbx_sli4_parameters);
11585 sli4_params->wqv = bf_get(cfg_wqv, mbx_sli4_parameters);
11586 sli4_params->rqv = bf_get(cfg_rqv, mbx_sli4_parameters);
James Smart7365f6f2018-02-22 08:18:46 -080011587 sli4_params->eqav = bf_get(cfg_eqav, mbx_sli4_parameters);
11588 sli4_params->cqav = bf_get(cfg_cqav, mbx_sli4_parameters);
James Smart0c651872013-07-15 18:33:23 -040011589 sli4_params->wqsize = bf_get(cfg_wqsize, mbx_sli4_parameters);
James Smart66e9e6b2018-06-26 08:24:27 -070011590 sli4_params->bv1s = bf_get(cfg_bv1s, mbx_sli4_parameters);
James Smartfedd3b72011-02-16 12:39:24 -050011591 sli4_params->sgl_pages_max = bf_get(cfg_sgl_page_cnt,
11592 mbx_sli4_parameters);
James Smart895427b2017-02-12 13:52:30 -080011593 sli4_params->wqpcnt = bf_get(cfg_wqpcnt, mbx_sli4_parameters);
James Smartfedd3b72011-02-16 12:39:24 -050011594 sli4_params->sgl_pp_align = bf_get(cfg_sgl_pp_align,
11595 mbx_sli4_parameters);
James Smart6d368e52011-05-24 11:44:12 -040011596 phba->sli4_hba.extents_in_use = bf_get(cfg_ext, mbx_sli4_parameters);
11597 phba->sli4_hba.rpi_hdrs_in_use = bf_get(cfg_hdrr, mbx_sli4_parameters);
James Smart895427b2017-02-12 13:52:30 -080011598
James Smartc15e0702019-05-21 17:49:02 -070011599 /* Check for firmware nvme support */
11600 rc = (bf_get(cfg_nvme, mbx_sli4_parameters) &&
11601 bf_get(cfg_xib, mbx_sli4_parameters));
James Smart895427b2017-02-12 13:52:30 -080011602
James Smartc15e0702019-05-21 17:49:02 -070011603 if (rc) {
11604 /* Save this to indicate the Firmware supports NVME */
11605 sli4_params->nvme = 1;
11606
11607 /* Firmware NVME support, check driver FC4 NVME support */
11608 if (phba->cfg_enable_fc4_type == LPFC_ENABLE_FCP) {
11609 lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_NVME,
11610 "6133 Disabling NVME support: "
11611 "FC4 type not supported: x%x\n",
11612 phba->cfg_enable_fc4_type);
11613 goto fcponly;
11614 }
11615 } else {
11616 /* No firmware NVME support, check driver FC4 NVME support */
11617 sli4_params->nvme = 0;
11618 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
11619 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_NVME,
11620 "6101 Disabling NVME support: Not "
11621 "supported by firmware (%d %d) x%x\n",
11622 bf_get(cfg_nvme, mbx_sli4_parameters),
11623 bf_get(cfg_xib, mbx_sli4_parameters),
11624 phba->cfg_enable_fc4_type);
11625fcponly:
11626 phba->nvme_support = 0;
11627 phba->nvmet_support = 0;
11628 phba->cfg_nvmet_mrq = 0;
11629
11630 /* If no FC4 type support, move to just SCSI support */
11631 if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP))
11632 return -ENODEV;
11633 phba->cfg_enable_fc4_type = LPFC_ENABLE_FCP;
11634 }
James Smart895427b2017-02-12 13:52:30 -080011635 }
James Smart05580562011-05-24 11:40:48 -040011636
James Smart414abe02018-06-26 08:24:26 -070011637 /* Only embed PBDE for if_type 6, PBDE support requires xib be set */
11638 if ((bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
11639 LPFC_SLI_INTF_IF_TYPE_6) || (!bf_get(cfg_xib, mbx_sli4_parameters)))
11640 phba->cfg_enable_pbde = 0;
James Smart0bc2b7c2018-02-22 08:18:48 -080011641
James Smart20aefac2018-01-30 15:58:58 -080011642 /*
11643 * To support Suppress Response feature we must satisfy 3 conditions.
11644 * lpfc_suppress_rsp module parameter must be set (default).
11645 * In SLI4-Parameters Descriptor:
11646 * Extended Inline Buffers (XIB) must be supported.
11647 * Suppress Response IU Not Supported (SRIUNS) must NOT be supported
11648 * (double negative).
11649 */
11650 if (phba->cfg_suppress_rsp && bf_get(cfg_xib, mbx_sli4_parameters) &&
11651 !(bf_get(cfg_nosr, mbx_sli4_parameters)))
James Smartf358dd02017-02-12 13:52:34 -080011652 phba->sli.sli_flag |= LPFC_SLI_SUPPRESS_RSP;
James Smart20aefac2018-01-30 15:58:58 -080011653 else
11654 phba->cfg_suppress_rsp = 0;
James Smartf358dd02017-02-12 13:52:34 -080011655
James Smart0cf07f842017-06-01 21:07:10 -070011656 if (bf_get(cfg_eqdr, mbx_sli4_parameters))
11657 phba->sli.sli_flag |= LPFC_SLI_USE_EQDR;
11658
James Smart05580562011-05-24 11:40:48 -040011659 /* Make sure that sge_supp_len can be handled by the driver */
11660 if (sli4_params->sge_supp_len > LPFC_MAX_SGE_SIZE)
11661 sli4_params->sge_supp_len = LPFC_MAX_SGE_SIZE;
11662
James Smartb5c53952016-03-31 14:12:30 -070011663 /*
James Smartc176ffa2018-01-30 15:58:46 -080011664 * Check whether the adapter supports an embedded copy of the
11665 * FCP CMD IU within the WQE for FCP_Ixxx commands. In order
11666 * to use this option, 128-byte WQEs must be used.
James Smartb5c53952016-03-31 14:12:30 -070011667 */
11668 if (bf_get(cfg_ext_embed_cb, mbx_sli4_parameters))
11669 phba->fcp_embed_io = 1;
11670 else
11671 phba->fcp_embed_io = 0;
James Smart7bdedb32016-07-06 12:36:00 -070011672
James Smart0bc2b7c2018-02-22 08:18:48 -080011673 lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_NVME,
James Smart414abe02018-06-26 08:24:26 -070011674 "6422 XIB %d PBDE %d: FCP %d NVME %d %d %d\n",
James Smart0bc2b7c2018-02-22 08:18:48 -080011675 bf_get(cfg_xib, mbx_sli4_parameters),
James Smart414abe02018-06-26 08:24:26 -070011676 phba->cfg_enable_pbde,
11677 phba->fcp_embed_io, phba->nvme_support,
James Smart4e565cf2018-02-22 08:18:50 -080011678 phba->cfg_nvme_embed_cmd, phba->cfg_suppress_rsp);
James Smart0bc2b7c2018-02-22 08:18:48 -080011679
James Smartbf316c72018-04-09 14:24:28 -070011680 if ((bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
11681 LPFC_SLI_INTF_IF_TYPE_2) &&
11682 (bf_get(lpfc_sli_intf_sli_family, &phba->sli4_hba.sli_intf) ==
James Smartc2217682018-05-24 21:09:00 -070011683 LPFC_SLI_INTF_FAMILY_LNCR_A0))
James Smartbf316c72018-04-09 14:24:28 -070011684 exp_wqcq_pages = false;
11685
James Smartc176ffa2018-01-30 15:58:46 -080011686 if ((bf_get(cfg_cqpsize, mbx_sli4_parameters) & LPFC_CQ_16K_PAGE_SZ) &&
11687 (bf_get(cfg_wqpsize, mbx_sli4_parameters) & LPFC_WQ_16K_PAGE_SZ) &&
James Smartbf316c72018-04-09 14:24:28 -070011688 exp_wqcq_pages &&
James Smartc176ffa2018-01-30 15:58:46 -080011689 (sli4_params->wqsize & LPFC_WQ_SZ128_SUPPORT))
11690 phba->enab_exp_wqcq_pages = 1;
11691 else
11692 phba->enab_exp_wqcq_pages = 0;
James Smart7bdedb32016-07-06 12:36:00 -070011693 /*
11694 * Check if the SLI port supports MDS Diagnostics
11695 */
11696 if (bf_get(cfg_mds_diags, mbx_sli4_parameters))
11697 phba->mds_diags_support = 1;
11698 else
11699 phba->mds_diags_support = 0;
James Smartd2cc9bc2018-09-10 10:30:50 -070011700
James Smartfedd3b72011-02-16 12:39:24 -050011701 return 0;
11702}
11703
11704/**
James Smart3772a992009-05-22 14:50:54 -040011705 * lpfc_pci_probe_one_s3 - PCI probe func to reg SLI-3 device to PCI subsystem.
11706 * @pdev: pointer to PCI device
11707 * @pid: pointer to PCI device identifier
11708 *
11709 * This routine is to be called to attach a device with SLI-3 interface spec
11710 * to the PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
11711 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
11712 * information of the device and driver to see if the driver state that it can
11713 * support this kind of device. If the match is successful, the driver core
11714 * invokes this routine. If this routine determines it can claim the HBA, it
11715 * does all the initialization that it needs to do to handle the HBA properly.
11716 *
11717 * Return code
11718 * 0 - driver can claim the device
11719 * negative value - driver can not claim the device
11720 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080011721static int
James Smart3772a992009-05-22 14:50:54 -040011722lpfc_pci_probe_one_s3(struct pci_dev *pdev, const struct pci_device_id *pid)
11723{
11724 struct lpfc_hba *phba;
11725 struct lpfc_vport *vport = NULL;
James Smart6669f9b2009-10-02 15:16:45 -040011726 struct Scsi_Host *shost = NULL;
James Smart3772a992009-05-22 14:50:54 -040011727 int error;
11728 uint32_t cfg_mode, intr_mode;
11729
11730 /* Allocate memory for HBA structure */
11731 phba = lpfc_hba_alloc(pdev);
11732 if (!phba)
11733 return -ENOMEM;
11734
11735 /* Perform generic PCI device enabling operation */
11736 error = lpfc_enable_pci_dev(phba);
James Smart079b5c92011-08-21 21:48:49 -040011737 if (error)
James Smart3772a992009-05-22 14:50:54 -040011738 goto out_free_phba;
James Smart3772a992009-05-22 14:50:54 -040011739
11740 /* Set up SLI API function jump table for PCI-device group-0 HBAs */
11741 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_LP);
11742 if (error)
11743 goto out_disable_pci_dev;
11744
11745 /* Set up SLI-3 specific device PCI memory space */
11746 error = lpfc_sli_pci_mem_setup(phba);
11747 if (error) {
11748 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11749 "1402 Failed to set up pci memory space.\n");
11750 goto out_disable_pci_dev;
11751 }
11752
James Smart3772a992009-05-22 14:50:54 -040011753 /* Set up SLI-3 specific device driver resources */
11754 error = lpfc_sli_driver_resource_setup(phba);
11755 if (error) {
11756 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11757 "1404 Failed to set up driver resource.\n");
11758 goto out_unset_pci_mem_s3;
11759 }
11760
11761 /* Initialize and populate the iocb list per host */
James Smartd1f525a2017-04-21 16:04:55 -070011762
James Smart3772a992009-05-22 14:50:54 -040011763 error = lpfc_init_iocb_list(phba, LPFC_IOCB_LIST_CNT);
11764 if (error) {
11765 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11766 "1405 Failed to initialize iocb list.\n");
11767 goto out_unset_driver_resource_s3;
11768 }
11769
11770 /* Set up common device driver resources */
11771 error = lpfc_setup_driver_resource_phase2(phba);
11772 if (error) {
11773 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11774 "1406 Failed to set up driver resource.\n");
11775 goto out_free_iocb_list;
11776 }
11777
James Smart079b5c92011-08-21 21:48:49 -040011778 /* Get the default values for Model Name and Description */
11779 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
11780
James Smart3772a992009-05-22 14:50:54 -040011781 /* Create SCSI host to the physical port */
11782 error = lpfc_create_shost(phba);
11783 if (error) {
11784 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11785 "1407 Failed to create scsi host.\n");
11786 goto out_unset_driver_resource;
11787 }
11788
11789 /* Configure sysfs attributes */
11790 vport = phba->pport;
11791 error = lpfc_alloc_sysfs_attr(vport);
11792 if (error) {
11793 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11794 "1476 Failed to allocate sysfs attr\n");
11795 goto out_destroy_shost;
11796 }
11797
James Smart6669f9b2009-10-02 15:16:45 -040011798 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
James Smart3772a992009-05-22 14:50:54 -040011799 /* Now, trying to enable interrupt and bring up the device */
11800 cfg_mode = phba->cfg_use_msi;
11801 while (true) {
11802 /* Put device to a known state before enabling interrupt */
11803 lpfc_stop_port(phba);
11804 /* Configure and enable interrupt */
11805 intr_mode = lpfc_sli_enable_intr(phba, cfg_mode);
11806 if (intr_mode == LPFC_INTR_ERROR) {
11807 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11808 "0431 Failed to enable interrupt.\n");
11809 error = -ENODEV;
11810 goto out_free_sysfs_attr;
11811 }
11812 /* SLI-3 HBA setup */
11813 if (lpfc_sli_hba_setup(phba)) {
11814 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11815 "1477 Failed to set up hba\n");
11816 error = -ENODEV;
11817 goto out_remove_device;
11818 }
11819
11820 /* Wait 50ms for the interrupts of previous mailbox commands */
11821 msleep(50);
11822 /* Check active interrupts on message signaled interrupts */
11823 if (intr_mode == 0 ||
11824 phba->sli.slistat.sli_intr > LPFC_MSIX_VECTORS) {
11825 /* Log the current active interrupt mode */
11826 phba->intr_mode = intr_mode;
11827 lpfc_log_intr_mode(phba, intr_mode);
11828 break;
11829 } else {
11830 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
11831 "0447 Configure interrupt mode (%d) "
11832 "failed active interrupt test.\n",
11833 intr_mode);
11834 /* Disable the current interrupt mode */
11835 lpfc_sli_disable_intr(phba);
11836 /* Try next level of interrupt mode */
11837 cfg_mode = --intr_mode;
11838 }
11839 }
11840
11841 /* Perform post initialization setup */
11842 lpfc_post_init_setup(phba);
11843
11844 /* Check if there are static vports to be created. */
11845 lpfc_create_static_vport(phba);
11846
11847 return 0;
11848
11849out_remove_device:
11850 lpfc_unset_hba(phba);
11851out_free_sysfs_attr:
11852 lpfc_free_sysfs_attr(vport);
11853out_destroy_shost:
11854 lpfc_destroy_shost(phba);
11855out_unset_driver_resource:
11856 lpfc_unset_driver_resource_phase2(phba);
11857out_free_iocb_list:
11858 lpfc_free_iocb_list(phba);
11859out_unset_driver_resource_s3:
11860 lpfc_sli_driver_resource_unset(phba);
11861out_unset_pci_mem_s3:
11862 lpfc_sli_pci_mem_unset(phba);
11863out_disable_pci_dev:
11864 lpfc_disable_pci_dev(phba);
James Smart6669f9b2009-10-02 15:16:45 -040011865 if (shost)
11866 scsi_host_put(shost);
James Smart3772a992009-05-22 14:50:54 -040011867out_free_phba:
11868 lpfc_hba_free(phba);
11869 return error;
11870}
11871
11872/**
11873 * lpfc_pci_remove_one_s3 - PCI func to unreg SLI-3 device from PCI subsystem.
James Smarte59058c2008-08-24 21:49:00 -040011874 * @pdev: pointer to PCI device
11875 *
James Smart3772a992009-05-22 14:50:54 -040011876 * This routine is to be called to disattach a device with SLI-3 interface
11877 * spec from PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
11878 * removed from PCI bus, it performs all the necessary cleanup for the HBA
11879 * device to be removed from the PCI subsystem properly.
James Smarte59058c2008-08-24 21:49:00 -040011880 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080011881static void
James Smart3772a992009-05-22 14:50:54 -040011882lpfc_pci_remove_one_s3(struct pci_dev *pdev)
dea31012005-04-17 16:05:31 -050011883{
James Smart2e0fef82007-06-17 19:56:36 -050011884 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11885 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
James Smarteada2722008-12-04 22:39:13 -050011886 struct lpfc_vport **vports;
James Smart2e0fef82007-06-17 19:56:36 -050011887 struct lpfc_hba *phba = vport->phba;
James Smarteada2722008-12-04 22:39:13 -050011888 int i;
Tomohiro Kusumi8a4df1202008-01-11 01:53:00 -050011889
James Smart549e55c2007-08-02 11:09:51 -040011890 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -040011891 vport->load_flag |= FC_UNLOADING;
James Smart549e55c2007-08-02 11:09:51 -040011892 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050011893
James Smart858c9f62007-06-17 19:56:39 -050011894 lpfc_free_sysfs_attr(vport);
11895
James Smarteada2722008-12-04 22:39:13 -050011896 /* Release all the vports against this physical port */
11897 vports = lpfc_create_vport_work_array(phba);
11898 if (vports != NULL)
James Smart587a37f2012-05-09 21:16:03 -040011899 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
11900 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
11901 continue;
James Smarteada2722008-12-04 22:39:13 -050011902 fc_vport_terminate(vports[i]->fc_vport);
James Smart587a37f2012-05-09 21:16:03 -040011903 }
James Smarteada2722008-12-04 22:39:13 -050011904 lpfc_destroy_vport_work_array(phba, vports);
11905
11906 /* Remove FC host and then SCSI host with the physical port */
James Smart858c9f62007-06-17 19:56:39 -050011907 fc_remove_host(shost);
11908 scsi_remove_host(shost);
James Smartd613b6a2017-02-12 13:52:37 -080011909
James Smart87af33f2007-10-27 13:37:43 -040011910 lpfc_cleanup(vport);
11911
James Smart2e0fef82007-06-17 19:56:36 -050011912 /*
11913 * Bring down the SLI Layer. This step disable all interrupts,
11914 * clears the rings, discards all mailbox commands, and resets
11915 * the HBA.
11916 */
James Smarta257bf92009-04-06 18:48:10 -040011917
Justin P. Mattock48e34d02010-12-30 15:07:58 -080011918 /* HBA interrupt will be disabled after this call */
James Smart2e0fef82007-06-17 19:56:36 -050011919 lpfc_sli_hba_down(phba);
James Smarta257bf92009-04-06 18:48:10 -040011920 /* Stop kthread signal shall trigger work_done one more time */
11921 kthread_stop(phba->worker_thread);
11922 /* Final cleanup of txcmplq and reset the HBA */
James Smart2e0fef82007-06-17 19:56:36 -050011923 lpfc_sli_brdrestart(phba);
11924
James Smart72859902012-01-18 16:25:38 -050011925 kfree(phba->vpi_bmask);
11926 kfree(phba->vpi_ids);
11927
James Smart3772a992009-05-22 14:50:54 -040011928 lpfc_stop_hba_timers(phba);
James Smart523128e2018-09-10 10:30:46 -070011929 spin_lock_irq(&phba->port_list_lock);
James Smart858c9f62007-06-17 19:56:39 -050011930 list_del_init(&vport->listentry);
James Smart523128e2018-09-10 10:30:46 -070011931 spin_unlock_irq(&phba->port_list_lock);
James Smart858c9f62007-06-17 19:56:39 -050011932
James Smart858c9f62007-06-17 19:56:39 -050011933 lpfc_debugfs_terminate(vport);
James Smart2e0fef82007-06-17 19:56:36 -050011934
James Smart912e3ac2011-05-24 11:42:11 -040011935 /* Disable SR-IOV if enabled */
11936 if (phba->cfg_sriov_nr_virtfn)
11937 pci_disable_sriov(pdev);
11938
James Smart5b75da22008-12-04 22:39:35 -050011939 /* Disable interrupt */
James Smart3772a992009-05-22 14:50:54 -040011940 lpfc_sli_disable_intr(phba);
dea31012005-04-17 16:05:31 -050011941
James Smart858c9f62007-06-17 19:56:39 -050011942 scsi_host_put(shost);
James Smart2e0fef82007-06-17 19:56:36 -050011943
11944 /*
11945 * Call scsi_free before mem_free since scsi bufs are released to their
11946 * corresponding pools here.
11947 */
11948 lpfc_scsi_free(phba);
James Smart0794d602019-01-28 11:14:19 -080011949 lpfc_free_iocb_list(phba);
11950
James Smart3772a992009-05-22 14:50:54 -040011951 lpfc_mem_free_all(phba);
James Smart2e0fef82007-06-17 19:56:36 -050011952
James Smart34b02dc2008-08-24 21:49:55 -040011953 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
11954 phba->hbqslimp.virt, phba->hbqslimp.phys);
James Smarted957682007-06-17 19:56:37 -050011955
James Smart2e0fef82007-06-17 19:56:36 -050011956 /* Free resources associated with SLI2 interface */
11957 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
James Smart34b02dc2008-08-24 21:49:55 -040011958 phba->slim2p.virt, phba->slim2p.phys);
James Smart2e0fef82007-06-17 19:56:36 -050011959
11960 /* unmap adapter SLIM and Control Registers */
11961 iounmap(phba->ctrl_regs_memmap_p);
11962 iounmap(phba->slim_memmap_p);
11963
James Smart3772a992009-05-22 14:50:54 -040011964 lpfc_hba_free(phba);
James Smart2e0fef82007-06-17 19:56:36 -050011965
Johannes Thumshirne0c04832016-06-07 09:44:03 +020011966 pci_release_mem_regions(pdev);
James Smart2e0fef82007-06-17 19:56:36 -050011967 pci_disable_device(pdev);
dea31012005-04-17 16:05:31 -050011968}
11969
Linas Vepstas8d63f372007-02-14 14:28:36 -060011970/**
James Smart3772a992009-05-22 14:50:54 -040011971 * lpfc_pci_suspend_one_s3 - PCI func to suspend SLI-3 device for power mgmnt
James Smart3a55b532008-12-04 22:38:54 -050011972 * @pdev: pointer to PCI device
11973 * @msg: power management message
11974 *
James Smart3772a992009-05-22 14:50:54 -040011975 * This routine is to be called from the kernel's PCI subsystem to support
11976 * system Power Management (PM) to device with SLI-3 interface spec. When
11977 * PM invokes this method, it quiesces the device by stopping the driver's
11978 * worker thread for the device, turning off device's interrupt and DMA,
11979 * and bring the device offline. Note that as the driver implements the
11980 * minimum PM requirements to a power-aware driver's PM support for the
11981 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
11982 * to the suspend() method call will be treated as SUSPEND and the driver will
11983 * fully reinitialize its device during resume() method call, the driver will
11984 * set device to PCI_D3hot state in PCI config space instead of setting it
11985 * according to the @msg provided by the PM.
James Smart3a55b532008-12-04 22:38:54 -050011986 *
11987 * Return code
James Smart3772a992009-05-22 14:50:54 -040011988 * 0 - driver suspended the device
11989 * Error otherwise
James Smart3a55b532008-12-04 22:38:54 -050011990 **/
11991static int
James Smart3772a992009-05-22 14:50:54 -040011992lpfc_pci_suspend_one_s3(struct pci_dev *pdev, pm_message_t msg)
James Smart3a55b532008-12-04 22:38:54 -050011993{
11994 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11995 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11996
11997 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
11998 "0473 PCI device Power Management suspend.\n");
11999
12000 /* Bring down the device */
James Smart618a5232012-06-12 13:54:36 -040012001 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart3a55b532008-12-04 22:38:54 -050012002 lpfc_offline(phba);
12003 kthread_stop(phba->worker_thread);
12004
12005 /* Disable interrupt from device */
James Smart3772a992009-05-22 14:50:54 -040012006 lpfc_sli_disable_intr(phba);
James Smart3a55b532008-12-04 22:38:54 -050012007
12008 /* Save device state to PCI config space */
12009 pci_save_state(pdev);
12010 pci_set_power_state(pdev, PCI_D3hot);
12011
12012 return 0;
12013}
12014
12015/**
James Smart3772a992009-05-22 14:50:54 -040012016 * lpfc_pci_resume_one_s3 - PCI func to resume SLI-3 device for power mgmnt
James Smart3a55b532008-12-04 22:38:54 -050012017 * @pdev: pointer to PCI device
12018 *
James Smart3772a992009-05-22 14:50:54 -040012019 * This routine is to be called from the kernel's PCI subsystem to support
12020 * system Power Management (PM) to device with SLI-3 interface spec. When PM
12021 * invokes this method, it restores the device's PCI config space state and
12022 * fully reinitializes the device and brings it online. Note that as the
12023 * driver implements the minimum PM requirements to a power-aware driver's
12024 * PM for suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE,
12025 * FREEZE) to the suspend() method call will be treated as SUSPEND and the
12026 * driver will fully reinitialize its device during resume() method call,
12027 * the device will be set to PCI_D0 directly in PCI config space before
12028 * restoring the state.
James Smart3a55b532008-12-04 22:38:54 -050012029 *
12030 * Return code
James Smart3772a992009-05-22 14:50:54 -040012031 * 0 - driver suspended the device
12032 * Error otherwise
James Smart3a55b532008-12-04 22:38:54 -050012033 **/
12034static int
James Smart3772a992009-05-22 14:50:54 -040012035lpfc_pci_resume_one_s3(struct pci_dev *pdev)
James Smart3a55b532008-12-04 22:38:54 -050012036{
12037 struct Scsi_Host *shost = pci_get_drvdata(pdev);
12038 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
James Smart5b75da22008-12-04 22:39:35 -050012039 uint32_t intr_mode;
James Smart3a55b532008-12-04 22:38:54 -050012040 int error;
12041
12042 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
12043 "0452 PCI device Power Management resume.\n");
12044
12045 /* Restore device state from PCI config space */
12046 pci_set_power_state(pdev, PCI_D0);
12047 pci_restore_state(pdev);
James Smart0d878412009-10-02 15:16:56 -040012048
James Smart1dfb5a42010-02-12 14:40:50 -050012049 /*
12050 * As the new kernel behavior of pci_restore_state() API call clears
12051 * device saved_state flag, need to save the restored state again.
12052 */
12053 pci_save_state(pdev);
12054
James Smart3a55b532008-12-04 22:38:54 -050012055 if (pdev->is_busmaster)
12056 pci_set_master(pdev);
12057
12058 /* Startup the kernel thread for this host adapter. */
12059 phba->worker_thread = kthread_run(lpfc_do_work, phba,
12060 "lpfc_worker_%d", phba->brd_no);
12061 if (IS_ERR(phba->worker_thread)) {
12062 error = PTR_ERR(phba->worker_thread);
12063 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12064 "0434 PM resume failed to start worker "
12065 "thread: error=x%x.\n", error);
12066 return error;
12067 }
12068
James Smart5b75da22008-12-04 22:39:35 -050012069 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -040012070 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -050012071 if (intr_mode == LPFC_INTR_ERROR) {
James Smart3a55b532008-12-04 22:38:54 -050012072 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart5b75da22008-12-04 22:39:35 -050012073 "0430 PM resume Failed to enable interrupt\n");
12074 return -EIO;
12075 } else
12076 phba->intr_mode = intr_mode;
James Smart3a55b532008-12-04 22:38:54 -050012077
12078 /* Restart HBA and bring it online */
12079 lpfc_sli_brdrestart(phba);
12080 lpfc_online(phba);
12081
James Smart5b75da22008-12-04 22:39:35 -050012082 /* Log the current active interrupt mode */
12083 lpfc_log_intr_mode(phba, phba->intr_mode);
12084
James Smart3a55b532008-12-04 22:38:54 -050012085 return 0;
12086}
12087
12088/**
James Smart891478a2009-11-18 15:40:23 -050012089 * lpfc_sli_prep_dev_for_recover - Prepare SLI3 device for pci slot recover
12090 * @phba: pointer to lpfc hba data structure.
12091 *
12092 * This routine is called to prepare the SLI3 device for PCI slot recover. It
James Smarte2af0d22010-03-15 11:25:32 -040012093 * aborts all the outstanding SCSI I/Os to the pci device.
James Smart891478a2009-11-18 15:40:23 -050012094 **/
12095static void
12096lpfc_sli_prep_dev_for_recover(struct lpfc_hba *phba)
12097{
12098 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12099 "2723 PCI channel I/O abort preparing for recovery\n");
James Smarte2af0d22010-03-15 11:25:32 -040012100
12101 /*
12102 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
12103 * and let the SCSI mid-layer to retry them to recover.
12104 */
James Smartdb55fba2014-04-04 13:52:02 -040012105 lpfc_sli_abort_fcp_rings(phba);
James Smart891478a2009-11-18 15:40:23 -050012106}
12107
12108/**
James Smart0d878412009-10-02 15:16:56 -040012109 * lpfc_sli_prep_dev_for_reset - Prepare SLI3 device for pci slot reset
12110 * @phba: pointer to lpfc hba data structure.
12111 *
12112 * This routine is called to prepare the SLI3 device for PCI slot reset. It
12113 * disables the device interrupt and pci device, and aborts the internal FCP
12114 * pending I/Os.
12115 **/
12116static void
12117lpfc_sli_prep_dev_for_reset(struct lpfc_hba *phba)
12118{
James Smart0d878412009-10-02 15:16:56 -040012119 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart891478a2009-11-18 15:40:23 -050012120 "2710 PCI channel disable preparing for reset\n");
James Smarte2af0d22010-03-15 11:25:32 -040012121
James Smart75baf692010-06-08 18:31:21 -040012122 /* Block any management I/Os to the device */
James Smart618a5232012-06-12 13:54:36 -040012123 lpfc_block_mgmt_io(phba, LPFC_MBX_WAIT);
James Smart75baf692010-06-08 18:31:21 -040012124
James Smarte2af0d22010-03-15 11:25:32 -040012125 /* Block all SCSI devices' I/Os on the host */
12126 lpfc_scsi_dev_block(phba);
12127
James Smartea714f32013-04-17 20:18:47 -040012128 /* Flush all driver's outstanding SCSI I/Os as we are to reset */
12129 lpfc_sli_flush_fcp_rings(phba);
12130
James Smarte2af0d22010-03-15 11:25:32 -040012131 /* stop all timers */
12132 lpfc_stop_hba_timers(phba);
12133
James Smart0d878412009-10-02 15:16:56 -040012134 /* Disable interrupt and pci device */
12135 lpfc_sli_disable_intr(phba);
12136 pci_disable_device(phba->pcidev);
James Smart0d878412009-10-02 15:16:56 -040012137}
12138
12139/**
12140 * lpfc_sli_prep_dev_for_perm_failure - Prepare SLI3 dev for pci slot disable
12141 * @phba: pointer to lpfc hba data structure.
12142 *
12143 * This routine is called to prepare the SLI3 device for PCI slot permanently
12144 * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
12145 * pending I/Os.
12146 **/
12147static void
James Smart75baf692010-06-08 18:31:21 -040012148lpfc_sli_prep_dev_for_perm_failure(struct lpfc_hba *phba)
James Smart0d878412009-10-02 15:16:56 -040012149{
12150 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart891478a2009-11-18 15:40:23 -050012151 "2711 PCI channel permanent disable for failure\n");
James Smarte2af0d22010-03-15 11:25:32 -040012152 /* Block all SCSI devices' I/Os on the host */
12153 lpfc_scsi_dev_block(phba);
12154
12155 /* stop all timers */
12156 lpfc_stop_hba_timers(phba);
12157
James Smart0d878412009-10-02 15:16:56 -040012158 /* Clean up all driver's outstanding SCSI I/Os */
12159 lpfc_sli_flush_fcp_rings(phba);
12160}
12161
12162/**
James Smart3772a992009-05-22 14:50:54 -040012163 * lpfc_io_error_detected_s3 - Method for handling SLI-3 device PCI I/O error
James Smarte59058c2008-08-24 21:49:00 -040012164 * @pdev: pointer to PCI device.
12165 * @state: the current PCI connection state.
Linas Vepstas8d63f372007-02-14 14:28:36 -060012166 *
James Smart3772a992009-05-22 14:50:54 -040012167 * This routine is called from the PCI subsystem for I/O error handling to
12168 * device with SLI-3 interface spec. This function is called by the PCI
12169 * subsystem after a PCI bus error affecting this device has been detected.
12170 * When this function is invoked, it will need to stop all the I/Os and
12171 * interrupt(s) to the device. Once that is done, it will return
12172 * PCI_ERS_RESULT_NEED_RESET for the PCI subsystem to perform proper recovery
12173 * as desired.
James Smarte59058c2008-08-24 21:49:00 -040012174 *
12175 * Return codes
James Smart0d878412009-10-02 15:16:56 -040012176 * PCI_ERS_RESULT_CAN_RECOVER - can be recovered with reset_link
James Smart3772a992009-05-22 14:50:54 -040012177 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
12178 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
James Smarte59058c2008-08-24 21:49:00 -040012179 **/
James Smart3772a992009-05-22 14:50:54 -040012180static pci_ers_result_t
12181lpfc_io_error_detected_s3(struct pci_dev *pdev, pci_channel_state_t state)
Linas Vepstas8d63f372007-02-14 14:28:36 -060012182{
James Smart51ef4c22007-08-02 11:10:31 -040012183 struct Scsi_Host *shost = pci_get_drvdata(pdev);
12184 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -060012185
James Smart0d878412009-10-02 15:16:56 -040012186 switch (state) {
12187 case pci_channel_io_normal:
James Smart891478a2009-11-18 15:40:23 -050012188 /* Non-fatal error, prepare for recovery */
12189 lpfc_sli_prep_dev_for_recover(phba);
James Smart0d878412009-10-02 15:16:56 -040012190 return PCI_ERS_RESULT_CAN_RECOVER;
12191 case pci_channel_io_frozen:
12192 /* Fatal error, prepare for slot reset */
12193 lpfc_sli_prep_dev_for_reset(phba);
12194 return PCI_ERS_RESULT_NEED_RESET;
12195 case pci_channel_io_perm_failure:
12196 /* Permanent failure, prepare for device down */
James Smart75baf692010-06-08 18:31:21 -040012197 lpfc_sli_prep_dev_for_perm_failure(phba);
Linas Vepstas8d63f372007-02-14 14:28:36 -060012198 return PCI_ERS_RESULT_DISCONNECT;
James Smart0d878412009-10-02 15:16:56 -040012199 default:
12200 /* Unknown state, prepare and request slot reset */
12201 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12202 "0472 Unknown PCI error state: x%x\n", state);
12203 lpfc_sli_prep_dev_for_reset(phba);
12204 return PCI_ERS_RESULT_NEED_RESET;
James Smarta8e497d2008-08-24 21:50:11 -040012205 }
Linas Vepstas8d63f372007-02-14 14:28:36 -060012206}
12207
12208/**
James Smart3772a992009-05-22 14:50:54 -040012209 * lpfc_io_slot_reset_s3 - Method for restarting PCI SLI-3 device from scratch.
James Smarte59058c2008-08-24 21:49:00 -040012210 * @pdev: pointer to PCI device.
Linas Vepstas8d63f372007-02-14 14:28:36 -060012211 *
James Smart3772a992009-05-22 14:50:54 -040012212 * This routine is called from the PCI subsystem for error handling to
12213 * device with SLI-3 interface spec. This is called after PCI bus has been
12214 * reset to restart the PCI card from scratch, as if from a cold-boot.
12215 * During the PCI subsystem error recovery, after driver returns
12216 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
12217 * recovery and then call this routine before calling the .resume method
12218 * to recover the device. This function will initialize the HBA device,
12219 * enable the interrupt, but it will just put the HBA to offline state
12220 * without passing any I/O traffic.
James Smarte59058c2008-08-24 21:49:00 -040012221 *
12222 * Return codes
James Smart3772a992009-05-22 14:50:54 -040012223 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
12224 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
Linas Vepstas8d63f372007-02-14 14:28:36 -060012225 */
James Smart3772a992009-05-22 14:50:54 -040012226static pci_ers_result_t
12227lpfc_io_slot_reset_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -060012228{
James Smart51ef4c22007-08-02 11:10:31 -040012229 struct Scsi_Host *shost = pci_get_drvdata(pdev);
12230 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -060012231 struct lpfc_sli *psli = &phba->sli;
James Smart5b75da22008-12-04 22:39:35 -050012232 uint32_t intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -060012233
12234 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
Benjamin Herrenschmidt09483912007-12-20 15:28:09 +110012235 if (pci_enable_device_mem(pdev)) {
Linas Vepstas8d63f372007-02-14 14:28:36 -060012236 printk(KERN_ERR "lpfc: Cannot re-enable "
12237 "PCI device after reset.\n");
12238 return PCI_ERS_RESULT_DISCONNECT;
12239 }
12240
James Smart97207482008-12-04 22:39:19 -050012241 pci_restore_state(pdev);
James Smart1dfb5a42010-02-12 14:40:50 -050012242
12243 /*
12244 * As the new kernel behavior of pci_restore_state() API call clears
12245 * device saved_state flag, need to save the restored state again.
12246 */
12247 pci_save_state(pdev);
12248
James Smart97207482008-12-04 22:39:19 -050012249 if (pdev->is_busmaster)
12250 pci_set_master(pdev);
Linas Vepstas8d63f372007-02-14 14:28:36 -060012251
James Smart92d7f7b2007-06-17 19:56:38 -050012252 spin_lock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040012253 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -050012254 spin_unlock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -060012255
James Smart5b75da22008-12-04 22:39:35 -050012256 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -040012257 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -050012258 if (intr_mode == LPFC_INTR_ERROR) {
12259 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12260 "0427 Cannot re-enable interrupt after "
12261 "slot reset.\n");
12262 return PCI_ERS_RESULT_DISCONNECT;
12263 } else
12264 phba->intr_mode = intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -060012265
James Smart75baf692010-06-08 18:31:21 -040012266 /* Take device offline, it will perform cleanup */
James Smart618a5232012-06-12 13:54:36 -040012267 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
Linas Vepstas8d63f372007-02-14 14:28:36 -060012268 lpfc_offline(phba);
12269 lpfc_sli_brdrestart(phba);
12270
James Smart5b75da22008-12-04 22:39:35 -050012271 /* Log the current active interrupt mode */
12272 lpfc_log_intr_mode(phba, phba->intr_mode);
12273
Linas Vepstas8d63f372007-02-14 14:28:36 -060012274 return PCI_ERS_RESULT_RECOVERED;
12275}
12276
12277/**
James Smart3772a992009-05-22 14:50:54 -040012278 * lpfc_io_resume_s3 - Method for resuming PCI I/O operation on SLI-3 device.
James Smarte59058c2008-08-24 21:49:00 -040012279 * @pdev: pointer to PCI device
Linas Vepstas8d63f372007-02-14 14:28:36 -060012280 *
James Smart3772a992009-05-22 14:50:54 -040012281 * This routine is called from the PCI subsystem for error handling to device
12282 * with SLI-3 interface spec. It is called when kernel error recovery tells
12283 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
12284 * error recovery. After this call, traffic can start to flow from this device
12285 * again.
Linas Vepstas8d63f372007-02-14 14:28:36 -060012286 */
James Smart3772a992009-05-22 14:50:54 -040012287static void
12288lpfc_io_resume_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -060012289{
James Smart51ef4c22007-08-02 11:10:31 -040012290 struct Scsi_Host *shost = pci_get_drvdata(pdev);
12291 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -060012292
James Smarte2af0d22010-03-15 11:25:32 -040012293 /* Bring device online, it will be no-op for non-fatal error resume */
James Smart58da1ff2008-04-07 10:15:56 -040012294 lpfc_online(phba);
Linas Vepstas8d63f372007-02-14 14:28:36 -060012295}
12296
James Smart3772a992009-05-22 14:50:54 -040012297/**
James Smartda0436e2009-05-22 14:51:39 -040012298 * lpfc_sli4_get_els_iocb_cnt - Calculate the # of ELS IOCBs to reserve
12299 * @phba: pointer to lpfc hba data structure.
12300 *
12301 * returns the number of ELS/CT IOCBs to reserve
12302 **/
12303int
12304lpfc_sli4_get_els_iocb_cnt(struct lpfc_hba *phba)
12305{
12306 int max_xri = phba->sli4_hba.max_cfg_param.max_xri;
12307
James Smartf1126682009-06-10 17:22:44 -040012308 if (phba->sli_rev == LPFC_SLI_REV4) {
12309 if (max_xri <= 100)
James Smart6a9c52c2009-10-02 15:16:51 -040012310 return 10;
James Smartf1126682009-06-10 17:22:44 -040012311 else if (max_xri <= 256)
James Smart6a9c52c2009-10-02 15:16:51 -040012312 return 25;
James Smartf1126682009-06-10 17:22:44 -040012313 else if (max_xri <= 512)
James Smart6a9c52c2009-10-02 15:16:51 -040012314 return 50;
James Smartf1126682009-06-10 17:22:44 -040012315 else if (max_xri <= 1024)
James Smart6a9c52c2009-10-02 15:16:51 -040012316 return 100;
James Smart8a9d2e82012-05-09 21:16:12 -040012317 else if (max_xri <= 1536)
James Smart6a9c52c2009-10-02 15:16:51 -040012318 return 150;
James Smart8a9d2e82012-05-09 21:16:12 -040012319 else if (max_xri <= 2048)
12320 return 200;
12321 else
12322 return 250;
James Smartf1126682009-06-10 17:22:44 -040012323 } else
12324 return 0;
James Smartda0436e2009-05-22 14:51:39 -040012325}
12326
12327/**
James Smart895427b2017-02-12 13:52:30 -080012328 * lpfc_sli4_get_iocb_cnt - Calculate the # of total IOCBs to reserve
12329 * @phba: pointer to lpfc hba data structure.
12330 *
James Smartf358dd02017-02-12 13:52:34 -080012331 * returns the number of ELS/CT + NVMET IOCBs to reserve
James Smart895427b2017-02-12 13:52:30 -080012332 **/
12333int
12334lpfc_sli4_get_iocb_cnt(struct lpfc_hba *phba)
12335{
12336 int max_xri = lpfc_sli4_get_els_iocb_cnt(phba);
12337
James Smartf358dd02017-02-12 13:52:34 -080012338 if (phba->nvmet_support)
12339 max_xri += LPFC_NVMET_BUF_POST;
James Smart895427b2017-02-12 13:52:30 -080012340 return max_xri;
12341}
12342
12343
James Smart1feb8202018-02-22 08:18:47 -080012344static void
12345lpfc_log_write_firmware_error(struct lpfc_hba *phba, uint32_t offset,
12346 uint32_t magic_number, uint32_t ftype, uint32_t fid, uint32_t fsize,
12347 const struct firmware *fw)
12348{
James Smarta72d56b2018-05-04 20:37:52 -070012349 if ((offset == ADD_STATUS_FW_NOT_SUPPORTED) ||
12350 (phba->pcidev->device == PCI_DEVICE_ID_LANCER_G6_FC &&
12351 magic_number != MAGIC_NUMER_G6) ||
12352 (phba->pcidev->device == PCI_DEVICE_ID_LANCER_G7_FC &&
12353 magic_number != MAGIC_NUMER_G7))
James Smart1feb8202018-02-22 08:18:47 -080012354 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12355 "3030 This firmware version is not supported on "
12356 "this HBA model. Device:%x Magic:%x Type:%x "
12357 "ID:%x Size %d %zd\n",
12358 phba->pcidev->device, magic_number, ftype, fid,
12359 fsize, fw->size);
12360 else
12361 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12362 "3022 FW Download failed. Device:%x Magic:%x Type:%x "
12363 "ID:%x Size %d %zd\n",
12364 phba->pcidev->device, magic_number, ftype, fid,
12365 fsize, fw->size);
12366}
12367
12368
James Smart895427b2017-02-12 13:52:30 -080012369/**
James Smart52d52442011-05-24 11:42:45 -040012370 * lpfc_write_firmware - attempt to write a firmware image to the port
James Smart52d52442011-05-24 11:42:45 -040012371 * @fw: pointer to firmware image returned from request_firmware.
James Smartce396282012-09-29 11:30:56 -040012372 * @phba: pointer to lpfc hba data structure.
James Smart52d52442011-05-24 11:42:45 -040012373 *
James Smart52d52442011-05-24 11:42:45 -040012374 **/
James Smartce396282012-09-29 11:30:56 -040012375static void
12376lpfc_write_firmware(const struct firmware *fw, void *context)
James Smart52d52442011-05-24 11:42:45 -040012377{
James Smartce396282012-09-29 11:30:56 -040012378 struct lpfc_hba *phba = (struct lpfc_hba *)context;
James Smart6b5151f2012-01-18 16:24:06 -050012379 char fwrev[FW_REV_STR_SIZE];
James Smartce396282012-09-29 11:30:56 -040012380 struct lpfc_grp_hdr *image;
James Smart52d52442011-05-24 11:42:45 -040012381 struct list_head dma_buffer_list;
12382 int i, rc = 0;
12383 struct lpfc_dmabuf *dmabuf, *next;
12384 uint32_t offset = 0, temp_offset = 0;
James Smart6b6ef5d2016-10-13 15:06:17 -070012385 uint32_t magic_number, ftype, fid, fsize;
James Smart52d52442011-05-24 11:42:45 -040012386
James Smartc71ab862012-10-31 14:44:33 -040012387 /* It can be null in no-wait mode, sanity check */
James Smartce396282012-09-29 11:30:56 -040012388 if (!fw) {
12389 rc = -ENXIO;
12390 goto out;
12391 }
12392 image = (struct lpfc_grp_hdr *)fw->data;
12393
James Smart6b6ef5d2016-10-13 15:06:17 -070012394 magic_number = be32_to_cpu(image->magic_number);
12395 ftype = bf_get_be32(lpfc_grp_hdr_file_type, image);
James Smart1feb8202018-02-22 08:18:47 -080012396 fid = bf_get_be32(lpfc_grp_hdr_id, image);
James Smart6b6ef5d2016-10-13 15:06:17 -070012397 fsize = be32_to_cpu(image->size);
12398
James Smart52d52442011-05-24 11:42:45 -040012399 INIT_LIST_HEAD(&dma_buffer_list);
James Smart52d52442011-05-24 11:42:45 -040012400 lpfc_decode_firmware_rev(phba, fwrev, 1);
James Smart88a2cfb2011-07-22 18:36:33 -040012401 if (strncmp(fwrev, image->revision, strnlen(image->revision, 16))) {
James Smart52d52442011-05-24 11:42:45 -040012402 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartce396282012-09-29 11:30:56 -040012403 "3023 Updating Firmware, Current Version:%s "
James Smart52d52442011-05-24 11:42:45 -040012404 "New Version:%s\n",
James Smart88a2cfb2011-07-22 18:36:33 -040012405 fwrev, image->revision);
James Smart52d52442011-05-24 11:42:45 -040012406 for (i = 0; i < LPFC_MBX_WR_CONFIG_MAX_BDE; i++) {
12407 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf),
12408 GFP_KERNEL);
12409 if (!dmabuf) {
12410 rc = -ENOMEM;
James Smartce396282012-09-29 11:30:56 -040012411 goto release_out;
James Smart52d52442011-05-24 11:42:45 -040012412 }
12413 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
12414 SLI4_PAGE_SIZE,
12415 &dmabuf->phys,
12416 GFP_KERNEL);
12417 if (!dmabuf->virt) {
12418 kfree(dmabuf);
12419 rc = -ENOMEM;
James Smartce396282012-09-29 11:30:56 -040012420 goto release_out;
James Smart52d52442011-05-24 11:42:45 -040012421 }
12422 list_add_tail(&dmabuf->list, &dma_buffer_list);
12423 }
12424 while (offset < fw->size) {
12425 temp_offset = offset;
12426 list_for_each_entry(dmabuf, &dma_buffer_list, list) {
James Smart079b5c92011-08-21 21:48:49 -040012427 if (temp_offset + SLI4_PAGE_SIZE > fw->size) {
James Smart52d52442011-05-24 11:42:45 -040012428 memcpy(dmabuf->virt,
12429 fw->data + temp_offset,
James Smart079b5c92011-08-21 21:48:49 -040012430 fw->size - temp_offset);
12431 temp_offset = fw->size;
James Smart52d52442011-05-24 11:42:45 -040012432 break;
12433 }
James Smart52d52442011-05-24 11:42:45 -040012434 memcpy(dmabuf->virt, fw->data + temp_offset,
12435 SLI4_PAGE_SIZE);
James Smart88a2cfb2011-07-22 18:36:33 -040012436 temp_offset += SLI4_PAGE_SIZE;
James Smart52d52442011-05-24 11:42:45 -040012437 }
12438 rc = lpfc_wr_object(phba, &dma_buffer_list,
12439 (fw->size - offset), &offset);
James Smart1feb8202018-02-22 08:18:47 -080012440 if (rc) {
12441 lpfc_log_write_firmware_error(phba, offset,
12442 magic_number, ftype, fid, fsize, fw);
James Smartce396282012-09-29 11:30:56 -040012443 goto release_out;
James Smart1feb8202018-02-22 08:18:47 -080012444 }
James Smart52d52442011-05-24 11:42:45 -040012445 }
12446 rc = offset;
James Smart1feb8202018-02-22 08:18:47 -080012447 } else
12448 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12449 "3029 Skipped Firmware update, Current "
12450 "Version:%s New Version:%s\n",
12451 fwrev, image->revision);
James Smartce396282012-09-29 11:30:56 -040012452
12453release_out:
James Smart52d52442011-05-24 11:42:45 -040012454 list_for_each_entry_safe(dmabuf, next, &dma_buffer_list, list) {
12455 list_del(&dmabuf->list);
12456 dma_free_coherent(&phba->pcidev->dev, SLI4_PAGE_SIZE,
12457 dmabuf->virt, dmabuf->phys);
12458 kfree(dmabuf);
12459 }
James Smartce396282012-09-29 11:30:56 -040012460 release_firmware(fw);
12461out:
12462 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartc71ab862012-10-31 14:44:33 -040012463 "3024 Firmware update done: %d.\n", rc);
James Smartce396282012-09-29 11:30:56 -040012464 return;
James Smart52d52442011-05-24 11:42:45 -040012465}
12466
12467/**
James Smartc71ab862012-10-31 14:44:33 -040012468 * lpfc_sli4_request_firmware_update - Request linux generic firmware upgrade
12469 * @phba: pointer to lpfc hba data structure.
12470 *
12471 * This routine is called to perform Linux generic firmware upgrade on device
12472 * that supports such feature.
12473 **/
12474int
12475lpfc_sli4_request_firmware_update(struct lpfc_hba *phba, uint8_t fw_upgrade)
12476{
12477 uint8_t file_name[ELX_MODEL_NAME_SIZE];
12478 int ret;
12479 const struct firmware *fw;
12480
12481 /* Only supported on SLI4 interface type 2 for now */
James Smart27d6ac02018-02-22 08:18:42 -080012482 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) <
James Smartc71ab862012-10-31 14:44:33 -040012483 LPFC_SLI_INTF_IF_TYPE_2)
12484 return -EPERM;
12485
12486 snprintf(file_name, ELX_MODEL_NAME_SIZE, "%s.grp", phba->ModelName);
12487
12488 if (fw_upgrade == INT_FW_UPGRADE) {
12489 ret = request_firmware_nowait(THIS_MODULE, FW_ACTION_HOTPLUG,
12490 file_name, &phba->pcidev->dev,
12491 GFP_KERNEL, (void *)phba,
12492 lpfc_write_firmware);
12493 } else if (fw_upgrade == RUN_FW_UPGRADE) {
12494 ret = request_firmware(&fw, file_name, &phba->pcidev->dev);
12495 if (!ret)
12496 lpfc_write_firmware(fw, (void *)phba);
12497 } else {
12498 ret = -EINVAL;
12499 }
12500
12501 return ret;
12502}
12503
12504/**
James Smartda0436e2009-05-22 14:51:39 -040012505 * lpfc_pci_probe_one_s4 - PCI probe func to reg SLI-4 device to PCI subsys
12506 * @pdev: pointer to PCI device
12507 * @pid: pointer to PCI device identifier
12508 *
12509 * This routine is called from the kernel's PCI subsystem to device with
12510 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
12511 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
12512 * information of the device and driver to see if the driver state that it
12513 * can support this kind of device. If the match is successful, the driver
12514 * core invokes this routine. If this routine determines it can claim the HBA,
12515 * it does all the initialization that it needs to do to handle the HBA
12516 * properly.
12517 *
12518 * Return code
12519 * 0 - driver can claim the device
12520 * negative value - driver can not claim the device
12521 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080012522static int
James Smartda0436e2009-05-22 14:51:39 -040012523lpfc_pci_probe_one_s4(struct pci_dev *pdev, const struct pci_device_id *pid)
12524{
12525 struct lpfc_hba *phba;
12526 struct lpfc_vport *vport = NULL;
James Smart6669f9b2009-10-02 15:16:45 -040012527 struct Scsi_Host *shost = NULL;
James Smart6c621a22017-05-15 15:20:45 -070012528 int error;
James Smartda0436e2009-05-22 14:51:39 -040012529 uint32_t cfg_mode, intr_mode;
James Smartda0436e2009-05-22 14:51:39 -040012530
12531 /* Allocate memory for HBA structure */
12532 phba = lpfc_hba_alloc(pdev);
12533 if (!phba)
12534 return -ENOMEM;
12535
12536 /* Perform generic PCI device enabling operation */
12537 error = lpfc_enable_pci_dev(phba);
James Smart079b5c92011-08-21 21:48:49 -040012538 if (error)
James Smartda0436e2009-05-22 14:51:39 -040012539 goto out_free_phba;
James Smartda0436e2009-05-22 14:51:39 -040012540
12541 /* Set up SLI API function jump table for PCI-device group-1 HBAs */
12542 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_OC);
12543 if (error)
12544 goto out_disable_pci_dev;
12545
12546 /* Set up SLI-4 specific device PCI memory space */
12547 error = lpfc_sli4_pci_mem_setup(phba);
12548 if (error) {
12549 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12550 "1410 Failed to set up pci memory space.\n");
12551 goto out_disable_pci_dev;
12552 }
12553
James Smartda0436e2009-05-22 14:51:39 -040012554 /* Set up SLI-4 Specific device driver resources */
12555 error = lpfc_sli4_driver_resource_setup(phba);
12556 if (error) {
12557 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12558 "1412 Failed to set up driver resource.\n");
12559 goto out_unset_pci_mem_s4;
12560 }
12561
James Smart19ca7602010-11-20 23:11:55 -050012562 INIT_LIST_HEAD(&phba->active_rrq_list);
James Smart7d791df2011-07-22 18:37:52 -040012563 INIT_LIST_HEAD(&phba->fcf.fcf_pri_list);
James Smart19ca7602010-11-20 23:11:55 -050012564
James Smartda0436e2009-05-22 14:51:39 -040012565 /* Set up common device driver resources */
12566 error = lpfc_setup_driver_resource_phase2(phba);
12567 if (error) {
12568 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12569 "1414 Failed to set up driver resource.\n");
James Smart6c621a22017-05-15 15:20:45 -070012570 goto out_unset_driver_resource_s4;
James Smartda0436e2009-05-22 14:51:39 -040012571 }
12572
James Smart079b5c92011-08-21 21:48:49 -040012573 /* Get the default values for Model Name and Description */
12574 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
12575
James Smartda0436e2009-05-22 14:51:39 -040012576 /* Now, trying to enable interrupt and bring up the device */
12577 cfg_mode = phba->cfg_use_msi;
James Smartda0436e2009-05-22 14:51:39 -040012578
James Smart7b15db32013-01-03 15:43:29 -050012579 /* Put device to a known state before enabling interrupt */
James Smartcdb42be2019-01-28 11:14:21 -080012580 phba->pport = NULL;
James Smart7b15db32013-01-03 15:43:29 -050012581 lpfc_stop_port(phba);
James Smart895427b2017-02-12 13:52:30 -080012582
James Smart7b15db32013-01-03 15:43:29 -050012583 /* Configure and enable interrupt */
12584 intr_mode = lpfc_sli4_enable_intr(phba, cfg_mode);
12585 if (intr_mode == LPFC_INTR_ERROR) {
12586 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12587 "0426 Failed to enable interrupt.\n");
12588 error = -ENODEV;
James Smartcdb42be2019-01-28 11:14:21 -080012589 goto out_unset_driver_resource;
James Smartda0436e2009-05-22 14:51:39 -040012590 }
James Smart7b15db32013-01-03 15:43:29 -050012591 /* Default to single EQ for non-MSI-X */
James Smart895427b2017-02-12 13:52:30 -080012592 if (phba->intr_type != MSIX) {
James Smart6a828b02019-01-28 11:14:31 -080012593 phba->cfg_irq_chann = 1;
James Smart2d7dbc42017-02-12 13:52:35 -080012594 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
James Smart2d7dbc42017-02-12 13:52:35 -080012595 if (phba->nvmet_support)
12596 phba->cfg_nvmet_mrq = 1;
12597 }
James Smartcdb42be2019-01-28 11:14:21 -080012598 }
James Smart6a828b02019-01-28 11:14:31 -080012599 lpfc_cpu_affinity_check(phba, phba->cfg_irq_chann);
James Smartcdb42be2019-01-28 11:14:21 -080012600
12601 /* Create SCSI host to the physical port */
12602 error = lpfc_create_shost(phba);
12603 if (error) {
12604 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12605 "1415 Failed to create scsi host.\n");
12606 goto out_disable_intr;
12607 }
12608 vport = phba->pport;
12609 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
12610
12611 /* Configure sysfs attributes */
12612 error = lpfc_alloc_sysfs_attr(vport);
12613 if (error) {
12614 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12615 "1416 Failed to allocate sysfs attr\n");
12616 goto out_destroy_shost;
James Smart895427b2017-02-12 13:52:30 -080012617 }
12618
James Smart7b15db32013-01-03 15:43:29 -050012619 /* Set up SLI-4 HBA */
12620 if (lpfc_sli4_hba_setup(phba)) {
12621 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12622 "1421 Failed to set up hba\n");
12623 error = -ENODEV;
James Smartcdb42be2019-01-28 11:14:21 -080012624 goto out_free_sysfs_attr;
James Smart7b15db32013-01-03 15:43:29 -050012625 }
12626
12627 /* Log the current active interrupt mode */
12628 phba->intr_mode = intr_mode;
12629 lpfc_log_intr_mode(phba, intr_mode);
James Smartda0436e2009-05-22 14:51:39 -040012630
12631 /* Perform post initialization setup */
12632 lpfc_post_init_setup(phba);
12633
James Smart01649562017-02-12 13:52:32 -080012634 /* NVME support in FW earlier in the driver load corrects the
12635 * FC4 type making a check for nvme_support unnecessary.
12636 */
James Smart0794d602019-01-28 11:14:19 -080012637 if (phba->nvmet_support == 0) {
12638 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
12639 /* Create NVME binding with nvme_fc_transport. This
12640 * ensures the vport is initialized. If the localport
12641 * create fails, it should not unload the driver to
12642 * support field issues.
12643 */
12644 error = lpfc_nvme_create_localport(vport);
12645 if (error) {
12646 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12647 "6004 NVME registration "
12648 "failed, error x%x\n",
12649 error);
12650 }
James Smart01649562017-02-12 13:52:32 -080012651 }
12652 }
James Smart895427b2017-02-12 13:52:30 -080012653
James Smartc71ab862012-10-31 14:44:33 -040012654 /* check for firmware upgrade or downgrade */
12655 if (phba->cfg_request_firmware_upgrade)
Sebastian Herbsztdb6f1c22015-08-31 16:48:14 -040012656 lpfc_sli4_request_firmware_update(phba, INT_FW_UPGRADE);
James Smart52d52442011-05-24 11:42:45 -040012657
James Smart1c6834a2009-07-19 10:01:26 -040012658 /* Check if there are static vports to be created. */
12659 lpfc_create_static_vport(phba);
James Smartd2cc9bc2018-09-10 10:30:50 -070012660
12661 /* Enable RAS FW log support */
12662 lpfc_sli4_ras_setup(phba);
12663
James Smartda0436e2009-05-22 14:51:39 -040012664 return 0;
12665
James Smartda0436e2009-05-22 14:51:39 -040012666out_free_sysfs_attr:
12667 lpfc_free_sysfs_attr(vport);
12668out_destroy_shost:
12669 lpfc_destroy_shost(phba);
James Smartcdb42be2019-01-28 11:14:21 -080012670out_disable_intr:
12671 lpfc_sli4_disable_intr(phba);
James Smartda0436e2009-05-22 14:51:39 -040012672out_unset_driver_resource:
12673 lpfc_unset_driver_resource_phase2(phba);
James Smartda0436e2009-05-22 14:51:39 -040012674out_unset_driver_resource_s4:
12675 lpfc_sli4_driver_resource_unset(phba);
12676out_unset_pci_mem_s4:
12677 lpfc_sli4_pci_mem_unset(phba);
12678out_disable_pci_dev:
12679 lpfc_disable_pci_dev(phba);
James Smart6669f9b2009-10-02 15:16:45 -040012680 if (shost)
12681 scsi_host_put(shost);
James Smartda0436e2009-05-22 14:51:39 -040012682out_free_phba:
12683 lpfc_hba_free(phba);
12684 return error;
12685}
12686
12687/**
12688 * lpfc_pci_remove_one_s4 - PCI func to unreg SLI-4 device from PCI subsystem
12689 * @pdev: pointer to PCI device
12690 *
12691 * This routine is called from the kernel's PCI subsystem to device with
12692 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
12693 * removed from PCI bus, it performs all the necessary cleanup for the HBA
12694 * device to be removed from the PCI subsystem properly.
12695 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080012696static void
James Smartda0436e2009-05-22 14:51:39 -040012697lpfc_pci_remove_one_s4(struct pci_dev *pdev)
12698{
12699 struct Scsi_Host *shost = pci_get_drvdata(pdev);
12700 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
12701 struct lpfc_vport **vports;
12702 struct lpfc_hba *phba = vport->phba;
12703 int i;
12704
12705 /* Mark the device unloading flag */
12706 spin_lock_irq(&phba->hbalock);
12707 vport->load_flag |= FC_UNLOADING;
12708 spin_unlock_irq(&phba->hbalock);
12709
12710 /* Free the HBA sysfs attributes */
12711 lpfc_free_sysfs_attr(vport);
12712
12713 /* Release all the vports against this physical port */
12714 vports = lpfc_create_vport_work_array(phba);
12715 if (vports != NULL)
James Smart587a37f2012-05-09 21:16:03 -040012716 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
12717 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
12718 continue;
James Smartda0436e2009-05-22 14:51:39 -040012719 fc_vport_terminate(vports[i]->fc_vport);
James Smart587a37f2012-05-09 21:16:03 -040012720 }
James Smartda0436e2009-05-22 14:51:39 -040012721 lpfc_destroy_vport_work_array(phba, vports);
12722
12723 /* Remove FC host and then SCSI host with the physical port */
12724 fc_remove_host(shost);
12725 scsi_remove_host(shost);
James Smartda0436e2009-05-22 14:51:39 -040012726
James Smartda0436e2009-05-22 14:51:39 -040012727 /* Perform ndlp cleanup on the physical port. The nvme and nvmet
12728 * localports are destroyed after to cleanup all transport memory.
12729 */
12730 lpfc_cleanup(vport);
12731 lpfc_nvmet_destroy_targetport(phba);
12732 lpfc_nvme_destroy_localport(vport);
12733
James Smartc4908502019-01-28 11:14:28 -080012734 /* De-allocate multi-XRI pools */
12735 if (phba->cfg_xri_rebalancing)
12736 lpfc_destroy_multixri_pools(phba);
12737
James Smart281d6192018-01-30 15:58:47 -080012738 /*
12739 * Bring down the SLI Layer. This step disables all interrupts,
12740 * clears the rings, discards all mailbox commands, and resets
12741 * the HBA FCoE function.
12742 */
12743 lpfc_debugfs_terminate(vport);
James Smartda0436e2009-05-22 14:51:39 -040012744
Dick Kennedy19017622017-09-29 17:34:27 -070012745 lpfc_stop_hba_timers(phba);
James Smart523128e2018-09-10 10:30:46 -070012746 spin_lock_irq(&phba->port_list_lock);
James Smartda0436e2009-05-22 14:51:39 -040012747 list_del_init(&vport->listentry);
James Smart523128e2018-09-10 10:30:46 -070012748 spin_unlock_irq(&phba->port_list_lock);
James Smartda0436e2009-05-22 14:51:39 -040012749
James Smart3677a3a2010-09-29 11:19:14 -040012750 /* Perform scsi free before driver resource_unset since scsi
James Smartda0436e2009-05-22 14:51:39 -040012751 * buffers are released to their corresponding pools here.
12752 */
James Smart5e5b5112019-01-28 11:14:22 -080012753 lpfc_io_free(phba);
James Smart01649562017-02-12 13:52:32 -080012754 lpfc_free_iocb_list(phba);
James Smart5e5b5112019-01-28 11:14:22 -080012755 lpfc_sli4_hba_unset(phba);
James Smart67d12732012-08-03 12:36:13 -040012756
James Smart0cdb84e2018-04-09 14:24:26 -070012757 lpfc_unset_driver_resource_phase2(phba);
James Smartda0436e2009-05-22 14:51:39 -040012758 lpfc_sli4_driver_resource_unset(phba);
12759
12760 /* Unmap adapter Control and Doorbell registers */
12761 lpfc_sli4_pci_mem_unset(phba);
12762
12763 /* Release PCI resources and disable device's PCI function */
12764 scsi_host_put(shost);
12765 lpfc_disable_pci_dev(phba);
12766
12767 /* Finally, free the driver's device data structure */
12768 lpfc_hba_free(phba);
12769
12770 return;
12771}
12772
12773/**
12774 * lpfc_pci_suspend_one_s4 - PCI func to suspend SLI-4 device for power mgmnt
12775 * @pdev: pointer to PCI device
12776 * @msg: power management message
12777 *
12778 * This routine is called from the kernel's PCI subsystem to support system
12779 * Power Management (PM) to device with SLI-4 interface spec. When PM invokes
12780 * this method, it quiesces the device by stopping the driver's worker
12781 * thread for the device, turning off device's interrupt and DMA, and bring
12782 * the device offline. Note that as the driver implements the minimum PM
12783 * requirements to a power-aware driver's PM support for suspend/resume -- all
12784 * the possible PM messages (SUSPEND, HIBERNATE, FREEZE) to the suspend()
12785 * method call will be treated as SUSPEND and the driver will fully
12786 * reinitialize its device during resume() method call, the driver will set
12787 * device to PCI_D3hot state in PCI config space instead of setting it
12788 * according to the @msg provided by the PM.
12789 *
12790 * Return code
12791 * 0 - driver suspended the device
12792 * Error otherwise
12793 **/
12794static int
12795lpfc_pci_suspend_one_s4(struct pci_dev *pdev, pm_message_t msg)
12796{
12797 struct Scsi_Host *shost = pci_get_drvdata(pdev);
12798 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
12799
12800 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart75baf692010-06-08 18:31:21 -040012801 "2843 PCI device Power Management suspend.\n");
James Smartda0436e2009-05-22 14:51:39 -040012802
12803 /* Bring down the device */
James Smart618a5232012-06-12 13:54:36 -040012804 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smartda0436e2009-05-22 14:51:39 -040012805 lpfc_offline(phba);
12806 kthread_stop(phba->worker_thread);
12807
12808 /* Disable interrupt from device */
12809 lpfc_sli4_disable_intr(phba);
James Smart5350d872011-10-10 21:33:49 -040012810 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -040012811
12812 /* Save device state to PCI config space */
12813 pci_save_state(pdev);
12814 pci_set_power_state(pdev, PCI_D3hot);
12815
12816 return 0;
12817}
12818
12819/**
12820 * lpfc_pci_resume_one_s4 - PCI func to resume SLI-4 device for power mgmnt
12821 * @pdev: pointer to PCI device
12822 *
12823 * This routine is called from the kernel's PCI subsystem to support system
12824 * Power Management (PM) to device with SLI-4 interface spac. When PM invokes
12825 * this method, it restores the device's PCI config space state and fully
12826 * reinitializes the device and brings it online. Note that as the driver
12827 * implements the minimum PM requirements to a power-aware driver's PM for
12828 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
12829 * to the suspend() method call will be treated as SUSPEND and the driver
12830 * will fully reinitialize its device during resume() method call, the device
12831 * will be set to PCI_D0 directly in PCI config space before restoring the
12832 * state.
12833 *
12834 * Return code
12835 * 0 - driver suspended the device
12836 * Error otherwise
12837 **/
12838static int
12839lpfc_pci_resume_one_s4(struct pci_dev *pdev)
12840{
12841 struct Scsi_Host *shost = pci_get_drvdata(pdev);
12842 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
12843 uint32_t intr_mode;
12844 int error;
12845
12846 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
12847 "0292 PCI device Power Management resume.\n");
12848
12849 /* Restore device state from PCI config space */
12850 pci_set_power_state(pdev, PCI_D0);
12851 pci_restore_state(pdev);
James Smart1dfb5a42010-02-12 14:40:50 -050012852
12853 /*
12854 * As the new kernel behavior of pci_restore_state() API call clears
12855 * device saved_state flag, need to save the restored state again.
12856 */
12857 pci_save_state(pdev);
12858
James Smartda0436e2009-05-22 14:51:39 -040012859 if (pdev->is_busmaster)
12860 pci_set_master(pdev);
12861
12862 /* Startup the kernel thread for this host adapter. */
12863 phba->worker_thread = kthread_run(lpfc_do_work, phba,
12864 "lpfc_worker_%d", phba->brd_no);
12865 if (IS_ERR(phba->worker_thread)) {
12866 error = PTR_ERR(phba->worker_thread);
12867 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12868 "0293 PM resume failed to start worker "
12869 "thread: error=x%x.\n", error);
12870 return error;
12871 }
12872
12873 /* Configure and enable interrupt */
12874 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
12875 if (intr_mode == LPFC_INTR_ERROR) {
12876 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12877 "0294 PM resume Failed to enable interrupt\n");
12878 return -EIO;
12879 } else
12880 phba->intr_mode = intr_mode;
12881
12882 /* Restart HBA and bring it online */
12883 lpfc_sli_brdrestart(phba);
12884 lpfc_online(phba);
12885
12886 /* Log the current active interrupt mode */
12887 lpfc_log_intr_mode(phba, phba->intr_mode);
12888
12889 return 0;
12890}
12891
12892/**
James Smart75baf692010-06-08 18:31:21 -040012893 * lpfc_sli4_prep_dev_for_recover - Prepare SLI4 device for pci slot recover
12894 * @phba: pointer to lpfc hba data structure.
12895 *
12896 * This routine is called to prepare the SLI4 device for PCI slot recover. It
12897 * aborts all the outstanding SCSI I/Os to the pci device.
12898 **/
12899static void
12900lpfc_sli4_prep_dev_for_recover(struct lpfc_hba *phba)
12901{
James Smart75baf692010-06-08 18:31:21 -040012902 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12903 "2828 PCI channel I/O abort preparing for recovery\n");
12904 /*
12905 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
12906 * and let the SCSI mid-layer to retry them to recover.
12907 */
James Smartdb55fba2014-04-04 13:52:02 -040012908 lpfc_sli_abort_fcp_rings(phba);
James Smart75baf692010-06-08 18:31:21 -040012909}
12910
12911/**
12912 * lpfc_sli4_prep_dev_for_reset - Prepare SLI4 device for pci slot reset
12913 * @phba: pointer to lpfc hba data structure.
12914 *
12915 * This routine is called to prepare the SLI4 device for PCI slot reset. It
12916 * disables the device interrupt and pci device, and aborts the internal FCP
12917 * pending I/Os.
12918 **/
12919static void
12920lpfc_sli4_prep_dev_for_reset(struct lpfc_hba *phba)
12921{
12922 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12923 "2826 PCI channel disable preparing for reset\n");
12924
12925 /* Block any management I/Os to the device */
James Smart618a5232012-06-12 13:54:36 -040012926 lpfc_block_mgmt_io(phba, LPFC_MBX_NO_WAIT);
James Smart75baf692010-06-08 18:31:21 -040012927
12928 /* Block all SCSI devices' I/Os on the host */
12929 lpfc_scsi_dev_block(phba);
12930
James Smartea714f32013-04-17 20:18:47 -040012931 /* Flush all driver's outstanding SCSI I/Os as we are to reset */
12932 lpfc_sli_flush_fcp_rings(phba);
12933
James Smartc3725bd2017-11-20 16:00:42 -080012934 /* Flush the outstanding NVME IOs if fc4 type enabled. */
12935 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
12936 lpfc_sli_flush_nvme_rings(phba);
12937
James Smart75baf692010-06-08 18:31:21 -040012938 /* stop all timers */
12939 lpfc_stop_hba_timers(phba);
12940
12941 /* Disable interrupt and pci device */
12942 lpfc_sli4_disable_intr(phba);
James Smart5350d872011-10-10 21:33:49 -040012943 lpfc_sli4_queue_destroy(phba);
James Smart75baf692010-06-08 18:31:21 -040012944 pci_disable_device(phba->pcidev);
James Smart75baf692010-06-08 18:31:21 -040012945}
12946
12947/**
12948 * lpfc_sli4_prep_dev_for_perm_failure - Prepare SLI4 dev for pci slot disable
12949 * @phba: pointer to lpfc hba data structure.
12950 *
12951 * This routine is called to prepare the SLI4 device for PCI slot permanently
12952 * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
12953 * pending I/Os.
12954 **/
12955static void
12956lpfc_sli4_prep_dev_for_perm_failure(struct lpfc_hba *phba)
12957{
12958 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12959 "2827 PCI channel permanent disable for failure\n");
12960
12961 /* Block all SCSI devices' I/Os on the host */
12962 lpfc_scsi_dev_block(phba);
12963
12964 /* stop all timers */
12965 lpfc_stop_hba_timers(phba);
12966
12967 /* Clean up all driver's outstanding SCSI I/Os */
12968 lpfc_sli_flush_fcp_rings(phba);
James Smartc3725bd2017-11-20 16:00:42 -080012969
12970 /* Flush the outstanding NVME IOs if fc4 type enabled. */
12971 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
12972 lpfc_sli_flush_nvme_rings(phba);
James Smart75baf692010-06-08 18:31:21 -040012973}
12974
12975/**
James Smartda0436e2009-05-22 14:51:39 -040012976 * lpfc_io_error_detected_s4 - Method for handling PCI I/O error to SLI-4 device
12977 * @pdev: pointer to PCI device.
12978 * @state: the current PCI connection state.
12979 *
12980 * This routine is called from the PCI subsystem for error handling to device
12981 * with SLI-4 interface spec. This function is called by the PCI subsystem
12982 * after a PCI bus error affecting this device has been detected. When this
12983 * function is invoked, it will need to stop all the I/Os and interrupt(s)
12984 * to the device. Once that is done, it will return PCI_ERS_RESULT_NEED_RESET
12985 * for the PCI subsystem to perform proper recovery as desired.
12986 *
12987 * Return codes
12988 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
12989 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
12990 **/
12991static pci_ers_result_t
12992lpfc_io_error_detected_s4(struct pci_dev *pdev, pci_channel_state_t state)
12993{
James Smart75baf692010-06-08 18:31:21 -040012994 struct Scsi_Host *shost = pci_get_drvdata(pdev);
12995 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
12996
12997 switch (state) {
12998 case pci_channel_io_normal:
12999 /* Non-fatal error, prepare for recovery */
13000 lpfc_sli4_prep_dev_for_recover(phba);
13001 return PCI_ERS_RESULT_CAN_RECOVER;
13002 case pci_channel_io_frozen:
13003 /* Fatal error, prepare for slot reset */
13004 lpfc_sli4_prep_dev_for_reset(phba);
13005 return PCI_ERS_RESULT_NEED_RESET;
13006 case pci_channel_io_perm_failure:
13007 /* Permanent failure, prepare for device down */
13008 lpfc_sli4_prep_dev_for_perm_failure(phba);
13009 return PCI_ERS_RESULT_DISCONNECT;
13010 default:
13011 /* Unknown state, prepare and request slot reset */
13012 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13013 "2825 Unknown PCI error state: x%x\n", state);
13014 lpfc_sli4_prep_dev_for_reset(phba);
13015 return PCI_ERS_RESULT_NEED_RESET;
13016 }
James Smartda0436e2009-05-22 14:51:39 -040013017}
13018
13019/**
13020 * lpfc_io_slot_reset_s4 - Method for restart PCI SLI-4 device from scratch
13021 * @pdev: pointer to PCI device.
13022 *
13023 * This routine is called from the PCI subsystem for error handling to device
13024 * with SLI-4 interface spec. It is called after PCI bus has been reset to
13025 * restart the PCI card from scratch, as if from a cold-boot. During the
13026 * PCI subsystem error recovery, after the driver returns
13027 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
13028 * recovery and then call this routine before calling the .resume method to
13029 * recover the device. This function will initialize the HBA device, enable
13030 * the interrupt, but it will just put the HBA to offline state without
13031 * passing any I/O traffic.
13032 *
13033 * Return codes
13034 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
13035 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
13036 */
13037static pci_ers_result_t
13038lpfc_io_slot_reset_s4(struct pci_dev *pdev)
13039{
James Smart75baf692010-06-08 18:31:21 -040013040 struct Scsi_Host *shost = pci_get_drvdata(pdev);
13041 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
13042 struct lpfc_sli *psli = &phba->sli;
13043 uint32_t intr_mode;
13044
13045 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
13046 if (pci_enable_device_mem(pdev)) {
13047 printk(KERN_ERR "lpfc: Cannot re-enable "
13048 "PCI device after reset.\n");
13049 return PCI_ERS_RESULT_DISCONNECT;
13050 }
13051
13052 pci_restore_state(pdev);
James Smart0a96e972011-07-22 18:37:28 -040013053
13054 /*
13055 * As the new kernel behavior of pci_restore_state() API call clears
13056 * device saved_state flag, need to save the restored state again.
13057 */
13058 pci_save_state(pdev);
13059
James Smart75baf692010-06-08 18:31:21 -040013060 if (pdev->is_busmaster)
13061 pci_set_master(pdev);
13062
13063 spin_lock_irq(&phba->hbalock);
13064 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
13065 spin_unlock_irq(&phba->hbalock);
13066
13067 /* Configure and enable interrupt */
13068 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
13069 if (intr_mode == LPFC_INTR_ERROR) {
13070 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13071 "2824 Cannot re-enable interrupt after "
13072 "slot reset.\n");
13073 return PCI_ERS_RESULT_DISCONNECT;
13074 } else
13075 phba->intr_mode = intr_mode;
13076
13077 /* Log the current active interrupt mode */
13078 lpfc_log_intr_mode(phba, phba->intr_mode);
13079
James Smartda0436e2009-05-22 14:51:39 -040013080 return PCI_ERS_RESULT_RECOVERED;
13081}
13082
13083/**
13084 * lpfc_io_resume_s4 - Method for resuming PCI I/O operation to SLI-4 device
13085 * @pdev: pointer to PCI device
13086 *
13087 * This routine is called from the PCI subsystem for error handling to device
13088 * with SLI-4 interface spec. It is called when kernel error recovery tells
13089 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
13090 * error recovery. After this call, traffic can start to flow from this device
13091 * again.
13092 **/
13093static void
13094lpfc_io_resume_s4(struct pci_dev *pdev)
13095{
James Smart75baf692010-06-08 18:31:21 -040013096 struct Scsi_Host *shost = pci_get_drvdata(pdev);
13097 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
13098
13099 /*
13100 * In case of slot reset, as function reset is performed through
13101 * mailbox command which needs DMA to be enabled, this operation
13102 * has to be moved to the io resume phase. Taking device offline
13103 * will perform the necessary cleanup.
13104 */
13105 if (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE)) {
13106 /* Perform device reset */
James Smart618a5232012-06-12 13:54:36 -040013107 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart75baf692010-06-08 18:31:21 -040013108 lpfc_offline(phba);
13109 lpfc_sli_brdrestart(phba);
13110 /* Bring the device back online */
13111 lpfc_online(phba);
13112 }
James Smartda0436e2009-05-22 14:51:39 -040013113}
13114
13115/**
James Smart3772a992009-05-22 14:50:54 -040013116 * lpfc_pci_probe_one - lpfc PCI probe func to reg dev to PCI subsystem
13117 * @pdev: pointer to PCI device
13118 * @pid: pointer to PCI device identifier
13119 *
13120 * This routine is to be registered to the kernel's PCI subsystem. When an
13121 * Emulex HBA device is presented on PCI bus, the kernel PCI subsystem looks
13122 * at PCI device-specific information of the device and driver to see if the
13123 * driver state that it can support this kind of device. If the match is
13124 * successful, the driver core invokes this routine. This routine dispatches
13125 * the action to the proper SLI-3 or SLI-4 device probing routine, which will
13126 * do all the initialization that it needs to do to handle the HBA device
13127 * properly.
13128 *
13129 * Return code
13130 * 0 - driver can claim the device
13131 * negative value - driver can not claim the device
13132 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080013133static int
James Smart3772a992009-05-22 14:50:54 -040013134lpfc_pci_probe_one(struct pci_dev *pdev, const struct pci_device_id *pid)
13135{
13136 int rc;
James Smart8fa38512009-07-19 10:01:03 -040013137 struct lpfc_sli_intf intf;
James Smart3772a992009-05-22 14:50:54 -040013138
James Smart28baac72010-02-12 14:42:03 -050013139 if (pci_read_config_dword(pdev, LPFC_SLI_INTF, &intf.word0))
James Smart3772a992009-05-22 14:50:54 -040013140 return -ENODEV;
13141
James Smart8fa38512009-07-19 10:01:03 -040013142 if ((bf_get(lpfc_sli_intf_valid, &intf) == LPFC_SLI_INTF_VALID) &&
James Smart28baac72010-02-12 14:42:03 -050013143 (bf_get(lpfc_sli_intf_slirev, &intf) == LPFC_SLI_INTF_REV_SLI4))
James Smartda0436e2009-05-22 14:51:39 -040013144 rc = lpfc_pci_probe_one_s4(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -040013145 else
James Smart3772a992009-05-22 14:50:54 -040013146 rc = lpfc_pci_probe_one_s3(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -040013147
James Smart3772a992009-05-22 14:50:54 -040013148 return rc;
13149}
13150
13151/**
13152 * lpfc_pci_remove_one - lpfc PCI func to unreg dev from PCI subsystem
13153 * @pdev: pointer to PCI device
13154 *
13155 * This routine is to be registered to the kernel's PCI subsystem. When an
13156 * Emulex HBA is removed from PCI bus, the driver core invokes this routine.
13157 * This routine dispatches the action to the proper SLI-3 or SLI-4 device
13158 * remove routine, which will perform all the necessary cleanup for the
13159 * device to be removed from the PCI subsystem properly.
13160 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080013161static void
James Smart3772a992009-05-22 14:50:54 -040013162lpfc_pci_remove_one(struct pci_dev *pdev)
13163{
13164 struct Scsi_Host *shost = pci_get_drvdata(pdev);
13165 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
13166
13167 switch (phba->pci_dev_grp) {
13168 case LPFC_PCI_DEV_LP:
13169 lpfc_pci_remove_one_s3(pdev);
13170 break;
James Smartda0436e2009-05-22 14:51:39 -040013171 case LPFC_PCI_DEV_OC:
13172 lpfc_pci_remove_one_s4(pdev);
13173 break;
James Smart3772a992009-05-22 14:50:54 -040013174 default:
13175 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13176 "1424 Invalid PCI device group: 0x%x\n",
13177 phba->pci_dev_grp);
13178 break;
13179 }
13180 return;
13181}
13182
13183/**
13184 * lpfc_pci_suspend_one - lpfc PCI func to suspend dev for power management
13185 * @pdev: pointer to PCI device
13186 * @msg: power management message
13187 *
13188 * This routine is to be registered to the kernel's PCI subsystem to support
13189 * system Power Management (PM). When PM invokes this method, it dispatches
13190 * the action to the proper SLI-3 or SLI-4 device suspend routine, which will
13191 * suspend the device.
13192 *
13193 * Return code
13194 * 0 - driver suspended the device
13195 * Error otherwise
13196 **/
13197static int
13198lpfc_pci_suspend_one(struct pci_dev *pdev, pm_message_t msg)
13199{
13200 struct Scsi_Host *shost = pci_get_drvdata(pdev);
13201 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
13202 int rc = -ENODEV;
13203
13204 switch (phba->pci_dev_grp) {
13205 case LPFC_PCI_DEV_LP:
13206 rc = lpfc_pci_suspend_one_s3(pdev, msg);
13207 break;
James Smartda0436e2009-05-22 14:51:39 -040013208 case LPFC_PCI_DEV_OC:
13209 rc = lpfc_pci_suspend_one_s4(pdev, msg);
13210 break;
James Smart3772a992009-05-22 14:50:54 -040013211 default:
13212 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13213 "1425 Invalid PCI device group: 0x%x\n",
13214 phba->pci_dev_grp);
13215 break;
13216 }
13217 return rc;
13218}
13219
13220/**
13221 * lpfc_pci_resume_one - lpfc PCI func to resume dev for power management
13222 * @pdev: pointer to PCI device
13223 *
13224 * This routine is to be registered to the kernel's PCI subsystem to support
13225 * system Power Management (PM). When PM invokes this method, it dispatches
13226 * the action to the proper SLI-3 or SLI-4 device resume routine, which will
13227 * resume the device.
13228 *
13229 * Return code
13230 * 0 - driver suspended the device
13231 * Error otherwise
13232 **/
13233static int
13234lpfc_pci_resume_one(struct pci_dev *pdev)
13235{
13236 struct Scsi_Host *shost = pci_get_drvdata(pdev);
13237 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
13238 int rc = -ENODEV;
13239
13240 switch (phba->pci_dev_grp) {
13241 case LPFC_PCI_DEV_LP:
13242 rc = lpfc_pci_resume_one_s3(pdev);
13243 break;
James Smartda0436e2009-05-22 14:51:39 -040013244 case LPFC_PCI_DEV_OC:
13245 rc = lpfc_pci_resume_one_s4(pdev);
13246 break;
James Smart3772a992009-05-22 14:50:54 -040013247 default:
13248 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13249 "1426 Invalid PCI device group: 0x%x\n",
13250 phba->pci_dev_grp);
13251 break;
13252 }
13253 return rc;
13254}
13255
13256/**
13257 * lpfc_io_error_detected - lpfc method for handling PCI I/O error
13258 * @pdev: pointer to PCI device.
13259 * @state: the current PCI connection state.
13260 *
13261 * This routine is registered to the PCI subsystem for error handling. This
13262 * function is called by the PCI subsystem after a PCI bus error affecting
13263 * this device has been detected. When this routine is invoked, it dispatches
13264 * the action to the proper SLI-3 or SLI-4 device error detected handling
13265 * routine, which will perform the proper error detected operation.
13266 *
13267 * Return codes
13268 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
13269 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
13270 **/
13271static pci_ers_result_t
13272lpfc_io_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
13273{
13274 struct Scsi_Host *shost = pci_get_drvdata(pdev);
13275 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
13276 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
13277
13278 switch (phba->pci_dev_grp) {
13279 case LPFC_PCI_DEV_LP:
13280 rc = lpfc_io_error_detected_s3(pdev, state);
13281 break;
James Smartda0436e2009-05-22 14:51:39 -040013282 case LPFC_PCI_DEV_OC:
13283 rc = lpfc_io_error_detected_s4(pdev, state);
13284 break;
James Smart3772a992009-05-22 14:50:54 -040013285 default:
13286 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13287 "1427 Invalid PCI device group: 0x%x\n",
13288 phba->pci_dev_grp);
13289 break;
13290 }
13291 return rc;
13292}
13293
13294/**
13295 * lpfc_io_slot_reset - lpfc method for restart PCI dev from scratch
13296 * @pdev: pointer to PCI device.
13297 *
13298 * This routine is registered to the PCI subsystem for error handling. This
13299 * function is called after PCI bus has been reset to restart the PCI card
13300 * from scratch, as if from a cold-boot. When this routine is invoked, it
13301 * dispatches the action to the proper SLI-3 or SLI-4 device reset handling
13302 * routine, which will perform the proper device reset.
13303 *
13304 * Return codes
13305 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
13306 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
13307 **/
13308static pci_ers_result_t
13309lpfc_io_slot_reset(struct pci_dev *pdev)
13310{
13311 struct Scsi_Host *shost = pci_get_drvdata(pdev);
13312 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
13313 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
13314
13315 switch (phba->pci_dev_grp) {
13316 case LPFC_PCI_DEV_LP:
13317 rc = lpfc_io_slot_reset_s3(pdev);
13318 break;
James Smartda0436e2009-05-22 14:51:39 -040013319 case LPFC_PCI_DEV_OC:
13320 rc = lpfc_io_slot_reset_s4(pdev);
13321 break;
James Smart3772a992009-05-22 14:50:54 -040013322 default:
13323 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13324 "1428 Invalid PCI device group: 0x%x\n",
13325 phba->pci_dev_grp);
13326 break;
13327 }
13328 return rc;
13329}
13330
13331/**
13332 * lpfc_io_resume - lpfc method for resuming PCI I/O operation
13333 * @pdev: pointer to PCI device
13334 *
13335 * This routine is registered to the PCI subsystem for error handling. It
13336 * is called when kernel error recovery tells the lpfc driver that it is
13337 * OK to resume normal PCI operation after PCI bus error recovery. When
13338 * this routine is invoked, it dispatches the action to the proper SLI-3
13339 * or SLI-4 device io_resume routine, which will resume the device operation.
13340 **/
13341static void
13342lpfc_io_resume(struct pci_dev *pdev)
13343{
13344 struct Scsi_Host *shost = pci_get_drvdata(pdev);
13345 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
13346
13347 switch (phba->pci_dev_grp) {
13348 case LPFC_PCI_DEV_LP:
13349 lpfc_io_resume_s3(pdev);
13350 break;
James Smartda0436e2009-05-22 14:51:39 -040013351 case LPFC_PCI_DEV_OC:
13352 lpfc_io_resume_s4(pdev);
13353 break;
James Smart3772a992009-05-22 14:50:54 -040013354 default:
13355 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13356 "1429 Invalid PCI device group: 0x%x\n",
13357 phba->pci_dev_grp);
13358 break;
13359 }
13360 return;
13361}
13362
James Smart1ba981f2014-02-20 09:56:45 -050013363/**
13364 * lpfc_sli4_oas_verify - Verify OAS is supported by this adapter
13365 * @phba: pointer to lpfc hba data structure.
13366 *
13367 * This routine checks to see if OAS is supported for this adapter. If
13368 * supported, the configure Flash Optimized Fabric flag is set. Otherwise,
13369 * the enable oas flag is cleared and the pool created for OAS device data
13370 * is destroyed.
13371 *
13372 **/
YueHaibingc7092972019-04-17 23:05:54 +080013373static void
James Smart1ba981f2014-02-20 09:56:45 -050013374lpfc_sli4_oas_verify(struct lpfc_hba *phba)
13375{
13376
13377 if (!phba->cfg_EnableXLane)
13378 return;
13379
13380 if (phba->sli4_hba.pc_sli4_params.oas_supported) {
13381 phba->cfg_fof = 1;
13382 } else {
James Smartf38fa0b2014-04-04 13:52:21 -040013383 phba->cfg_fof = 0;
James Smart1ba981f2014-02-20 09:56:45 -050013384 if (phba->device_data_mem_pool)
13385 mempool_destroy(phba->device_data_mem_pool);
13386 phba->device_data_mem_pool = NULL;
13387 }
13388
13389 return;
13390}
13391
13392/**
James Smartd2cc9bc2018-09-10 10:30:50 -070013393 * lpfc_sli4_ras_init - Verify RAS-FW log is supported by this adapter
13394 * @phba: pointer to lpfc hba data structure.
13395 *
13396 * This routine checks to see if RAS is supported by the adapter. Check the
13397 * function through which RAS support enablement is to be done.
13398 **/
13399void
13400lpfc_sli4_ras_init(struct lpfc_hba *phba)
13401{
13402 switch (phba->pcidev->device) {
13403 case PCI_DEVICE_ID_LANCER_G6_FC:
13404 case PCI_DEVICE_ID_LANCER_G7_FC:
13405 phba->ras_fwlog.ras_hwsupport = true;
James Smartcb349902018-11-29 16:09:27 -080013406 if (phba->cfg_ras_fwlog_func == PCI_FUNC(phba->pcidev->devfn) &&
13407 phba->cfg_ras_fwlog_buffsize)
James Smartd2cc9bc2018-09-10 10:30:50 -070013408 phba->ras_fwlog.ras_enabled = true;
13409 else
13410 phba->ras_fwlog.ras_enabled = false;
13411 break;
13412 default:
13413 phba->ras_fwlog.ras_hwsupport = false;
13414 }
13415}
13416
James Smart1ba981f2014-02-20 09:56:45 -050013417
dea31012005-04-17 16:05:31 -050013418MODULE_DEVICE_TABLE(pci, lpfc_id_table);
13419
Stephen Hemmingera55b2d22012-09-07 09:33:16 -070013420static const struct pci_error_handlers lpfc_err_handler = {
Linas Vepstas8d63f372007-02-14 14:28:36 -060013421 .error_detected = lpfc_io_error_detected,
13422 .slot_reset = lpfc_io_slot_reset,
13423 .resume = lpfc_io_resume,
13424};
13425
dea31012005-04-17 16:05:31 -050013426static struct pci_driver lpfc_driver = {
13427 .name = LPFC_DRIVER_NAME,
13428 .id_table = lpfc_id_table,
13429 .probe = lpfc_pci_probe_one,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080013430 .remove = lpfc_pci_remove_one,
Anton Blanchard85e8a232017-02-13 08:49:20 +110013431 .shutdown = lpfc_pci_remove_one,
James Smart3a55b532008-12-04 22:38:54 -050013432 .suspend = lpfc_pci_suspend_one,
James Smart3772a992009-05-22 14:50:54 -040013433 .resume = lpfc_pci_resume_one,
James Smart2e0fef82007-06-17 19:56:36 -050013434 .err_handler = &lpfc_err_handler,
dea31012005-04-17 16:05:31 -050013435};
13436
James Smart3ef6d242012-01-18 16:23:48 -050013437static const struct file_operations lpfc_mgmt_fop = {
Al Viro858feac2013-04-14 22:39:37 -040013438 .owner = THIS_MODULE,
James Smart3ef6d242012-01-18 16:23:48 -050013439};
13440
13441static struct miscdevice lpfc_mgmt_dev = {
13442 .minor = MISC_DYNAMIC_MINOR,
13443 .name = "lpfcmgmt",
13444 .fops = &lpfc_mgmt_fop,
13445};
13446
James Smarte59058c2008-08-24 21:49:00 -040013447/**
James Smart3621a712009-04-06 18:47:14 -040013448 * lpfc_init - lpfc module initialization routine
James Smarte59058c2008-08-24 21:49:00 -040013449 *
13450 * This routine is to be invoked when the lpfc module is loaded into the
13451 * kernel. The special kernel macro module_init() is used to indicate the
13452 * role of this routine to the kernel as lpfc module entry point.
13453 *
13454 * Return codes
13455 * 0 - successful
13456 * -ENOMEM - FC attach transport failed
13457 * all others - failed
13458 */
dea31012005-04-17 16:05:31 -050013459static int __init
13460lpfc_init(void)
13461{
13462 int error = 0;
13463
13464 printk(LPFC_MODULE_DESC "\n");
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040013465 printk(LPFC_COPYRIGHT "\n");
dea31012005-04-17 16:05:31 -050013466
James Smart3ef6d242012-01-18 16:23:48 -050013467 error = misc_register(&lpfc_mgmt_dev);
13468 if (error)
13469 printk(KERN_ERR "Could not register lpfcmgmt device, "
13470 "misc_register returned with status %d", error);
13471
James Smart458c0832016-07-06 12:36:07 -070013472 lpfc_transport_functions.vport_create = lpfc_vport_create;
13473 lpfc_transport_functions.vport_delete = lpfc_vport_delete;
dea31012005-04-17 16:05:31 -050013474 lpfc_transport_template =
13475 fc_attach_transport(&lpfc_transport_functions);
James Smart7ee5d432007-10-27 13:37:17 -040013476 if (lpfc_transport_template == NULL)
dea31012005-04-17 16:05:31 -050013477 return -ENOMEM;
James Smart458c0832016-07-06 12:36:07 -070013478 lpfc_vport_transport_template =
13479 fc_attach_transport(&lpfc_vport_transport_functions);
13480 if (lpfc_vport_transport_template == NULL) {
13481 fc_release_transport(lpfc_transport_template);
13482 return -ENOMEM;
James Smart7ee5d432007-10-27 13:37:17 -040013483 }
James Smart5fd11082018-03-05 12:04:04 -080013484 lpfc_nvme_cmd_template();
James Smartbd3061b2018-03-05 12:04:05 -080013485 lpfc_nvmet_cmd_template();
James Smart7bb03bb2013-04-17 20:19:16 -040013486
13487 /* Initialize in case vector mapping is needed */
James Smart2ea259e2017-02-12 13:52:27 -080013488 lpfc_present_cpu = num_present_cpus();
James Smart7bb03bb2013-04-17 20:19:16 -040013489
dea31012005-04-17 16:05:31 -050013490 error = pci_register_driver(&lpfc_driver);
James Smart92d7f7b2007-06-17 19:56:38 -050013491 if (error) {
dea31012005-04-17 16:05:31 -050013492 fc_release_transport(lpfc_transport_template);
James Smart458c0832016-07-06 12:36:07 -070013493 fc_release_transport(lpfc_vport_transport_template);
James Smart92d7f7b2007-06-17 19:56:38 -050013494 }
dea31012005-04-17 16:05:31 -050013495
13496 return error;
13497}
13498
James Smarte59058c2008-08-24 21:49:00 -040013499/**
James Smart3621a712009-04-06 18:47:14 -040013500 * lpfc_exit - lpfc module removal routine
James Smarte59058c2008-08-24 21:49:00 -040013501 *
13502 * This routine is invoked when the lpfc module is removed from the kernel.
13503 * The special kernel macro module_exit() is used to indicate the role of
13504 * this routine to the kernel as lpfc module exit point.
13505 */
dea31012005-04-17 16:05:31 -050013506static void __exit
13507lpfc_exit(void)
13508{
James Smart3ef6d242012-01-18 16:23:48 -050013509 misc_deregister(&lpfc_mgmt_dev);
dea31012005-04-17 16:05:31 -050013510 pci_unregister_driver(&lpfc_driver);
13511 fc_release_transport(lpfc_transport_template);
James Smart458c0832016-07-06 12:36:07 -070013512 fc_release_transport(lpfc_vport_transport_template);
James Smart81301a92008-12-04 22:39:46 -050013513 if (_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -040013514 printk(KERN_ERR "9062 BLKGRD: freeing %lu pages for "
13515 "_dump_buf_data at 0x%p\n",
James Smart81301a92008-12-04 22:39:46 -050013516 (1L << _dump_buf_data_order), _dump_buf_data);
13517 free_pages((unsigned long)_dump_buf_data, _dump_buf_data_order);
13518 }
13519
13520 if (_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -040013521 printk(KERN_ERR "9049 BLKGRD: freeing %lu pages for "
13522 "_dump_buf_dif at 0x%p\n",
James Smart81301a92008-12-04 22:39:46 -050013523 (1L << _dump_buf_dif_order), _dump_buf_dif);
13524 free_pages((unsigned long)_dump_buf_dif, _dump_buf_dif_order);
13525 }
Johannes Thumshirn79739672015-08-31 16:48:11 -040013526 idr_destroy(&lpfc_hba_index);
dea31012005-04-17 16:05:31 -050013527}
13528
13529module_init(lpfc_init);
13530module_exit(lpfc_exit);
13531MODULE_LICENSE("GPL");
13532MODULE_DESCRIPTION(LPFC_MODULE_DESC);
James Smartd080abe2017-02-12 13:52:39 -080013533MODULE_AUTHOR("Broadcom");
dea31012005-04-17 16:05:31 -050013534MODULE_VERSION("0:" LPFC_DRIVER_VERSION);