blob: 73b77aaf7135fa432f6bbd680063bb75ce494e39 [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_eq_handle(struct lpfc_hba *, uint16_t);
97static uint16_t lpfc_find_cpu_handle(struct lpfc_hba *, uint16_t, int);
dea31012005-04-17 16:05:31 -050098
99static struct scsi_transport_template *lpfc_transport_template = NULL;
James Smart92d7f7b2007-06-17 19:56:38 -0500100static struct scsi_transport_template *lpfc_vport_transport_template = NULL;
dea31012005-04-17 16:05:31 -0500101static DEFINE_IDR(lpfc_hba_index);
James Smartf358dd02017-02-12 13:52:34 -0800102#define LPFC_NVMET_BUF_POST 254
dea31012005-04-17 16:05:31 -0500103
James Smarte59058c2008-08-24 21:49:00 -0400104/**
James Smart3621a712009-04-06 18:47:14 -0400105 * lpfc_config_port_prep - Perform lpfc initialization prior to config port
James Smarte59058c2008-08-24 21:49:00 -0400106 * @phba: pointer to lpfc hba data structure.
107 *
108 * This routine will do LPFC initialization prior to issuing the CONFIG_PORT
109 * mailbox command. It retrieves the revision information from the HBA and
110 * collects the Vital Product Data (VPD) about the HBA for preparing the
111 * configuration of the HBA.
112 *
113 * Return codes:
114 * 0 - success.
115 * -ERESTART - requests the SLI layer to reset the HBA and try again.
116 * Any other value - indicates an error.
117 **/
dea31012005-04-17 16:05:31 -0500118int
James Smart2e0fef82007-06-17 19:56:36 -0500119lpfc_config_port_prep(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500120{
121 lpfc_vpd_t *vp = &phba->vpd;
122 int i = 0, rc;
123 LPFC_MBOXQ_t *pmb;
124 MAILBOX_t *mb;
125 char *lpfc_vpd_data = NULL;
126 uint16_t offset = 0;
127 static char licensed[56] =
128 "key unlock for use with gnu public licensed code only\0";
James Smart65a29c12006-07-06 15:50:50 -0400129 static int init_key = 1;
dea31012005-04-17 16:05:31 -0500130
131 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
132 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -0500133 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500134 return -ENOMEM;
135 }
136
James Smart04c68492009-05-22 14:52:52 -0400137 mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -0500138 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -0500139
140 if (lpfc_is_LC_HBA(phba->pcidev->device)) {
James Smart65a29c12006-07-06 15:50:50 -0400141 if (init_key) {
142 uint32_t *ptext = (uint32_t *) licensed;
dea31012005-04-17 16:05:31 -0500143
James Smart65a29c12006-07-06 15:50:50 -0400144 for (i = 0; i < 56; i += sizeof (uint32_t), ptext++)
145 *ptext = cpu_to_be32(*ptext);
146 init_key = 0;
147 }
dea31012005-04-17 16:05:31 -0500148
149 lpfc_read_nv(phba, pmb);
150 memset((char*)mb->un.varRDnvp.rsvd3, 0,
151 sizeof (mb->un.varRDnvp.rsvd3));
152 memcpy((char*)mb->un.varRDnvp.rsvd3, licensed,
153 sizeof (licensed));
154
155 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
156
157 if (rc != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500158 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
James Smarte8b62012007-08-02 11:10:09 -0400159 "0324 Config Port initialization "
dea31012005-04-17 16:05:31 -0500160 "error, mbxCmd x%x READ_NVPARM, "
161 "mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500162 mb->mbxCommand, mb->mbxStatus);
163 mempool_free(pmb, phba->mbox_mem_pool);
164 return -ERESTART;
165 }
166 memcpy(phba->wwnn, (char *)mb->un.varRDnvp.nodename,
James Smart2e0fef82007-06-17 19:56:36 -0500167 sizeof(phba->wwnn));
168 memcpy(phba->wwpn, (char *)mb->un.varRDnvp.portname,
169 sizeof(phba->wwpn));
dea31012005-04-17 16:05:31 -0500170 }
171
Martin Wilckdfb75132018-11-12 09:58:37 +0100172 /*
173 * Clear all option bits except LPFC_SLI3_BG_ENABLED,
174 * which was already set in lpfc_get_cfgparam()
175 */
176 phba->sli3_options &= (uint32_t)LPFC_SLI3_BG_ENABLED;
James Smart92d7f7b2007-06-17 19:56:38 -0500177
dea31012005-04-17 16:05:31 -0500178 /* Setup and issue mailbox READ REV command */
179 lpfc_read_rev(phba, pmb);
180 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
181 if (rc != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500182 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400183 "0439 Adapter failed to init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500184 "READ_REV, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500185 mb->mbxCommand, mb->mbxStatus);
186 mempool_free( pmb, phba->mbox_mem_pool);
187 return -ERESTART;
188 }
189
James Smart92d7f7b2007-06-17 19:56:38 -0500190
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500191 /*
192 * The value of rr must be 1 since the driver set the cv field to 1.
193 * This setting requires the FW to set all revision fields.
dea31012005-04-17 16:05:31 -0500194 */
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500195 if (mb->un.varRdRev.rr == 0) {
dea31012005-04-17 16:05:31 -0500196 vp->rev.rBit = 0;
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500197 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400198 "0440 Adapter failed to init, READ_REV has "
199 "missing revision information.\n");
dea31012005-04-17 16:05:31 -0500200 mempool_free(pmb, phba->mbox_mem_pool);
201 return -ERESTART;
dea31012005-04-17 16:05:31 -0500202 }
203
James Smart495a7142008-06-14 22:52:59 -0400204 if (phba->sli_rev == 3 && !mb->un.varRdRev.v3rsp) {
205 mempool_free(pmb, phba->mbox_mem_pool);
James Smarted957682007-06-17 19:56:37 -0500206 return -EINVAL;
James Smart495a7142008-06-14 22:52:59 -0400207 }
James Smarted957682007-06-17 19:56:37 -0500208
dea31012005-04-17 16:05:31 -0500209 /* Save information as VPD data */
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500210 vp->rev.rBit = 1;
James Smart92d7f7b2007-06-17 19:56:38 -0500211 memcpy(&vp->sli3Feat, &mb->un.varRdRev.sli3Feat, sizeof(uint32_t));
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500212 vp->rev.sli1FwRev = mb->un.varRdRev.sli1FwRev;
213 memcpy(vp->rev.sli1FwName, (char*) mb->un.varRdRev.sli1FwName, 16);
214 vp->rev.sli2FwRev = mb->un.varRdRev.sli2FwRev;
215 memcpy(vp->rev.sli2FwName, (char *) mb->un.varRdRev.sli2FwName, 16);
dea31012005-04-17 16:05:31 -0500216 vp->rev.biuRev = mb->un.varRdRev.biuRev;
217 vp->rev.smRev = mb->un.varRdRev.smRev;
218 vp->rev.smFwRev = mb->un.varRdRev.un.smFwRev;
219 vp->rev.endecRev = mb->un.varRdRev.endecRev;
220 vp->rev.fcphHigh = mb->un.varRdRev.fcphHigh;
221 vp->rev.fcphLow = mb->un.varRdRev.fcphLow;
222 vp->rev.feaLevelHigh = mb->un.varRdRev.feaLevelHigh;
223 vp->rev.feaLevelLow = mb->un.varRdRev.feaLevelLow;
224 vp->rev.postKernRev = mb->un.varRdRev.postKernRev;
225 vp->rev.opFwRev = mb->un.varRdRev.opFwRev;
226
James Smart92d7f7b2007-06-17 19:56:38 -0500227 /* If the sli feature level is less then 9, we must
228 * tear down all RPIs and VPIs on link down if NPIV
229 * is enabled.
230 */
231 if (vp->rev.feaLevelHigh < 9)
232 phba->sli3_options |= LPFC_SLI3_VPORT_TEARDOWN;
233
dea31012005-04-17 16:05:31 -0500234 if (lpfc_is_LC_HBA(phba->pcidev->device))
235 memcpy(phba->RandomData, (char *)&mb->un.varWords[24],
236 sizeof (phba->RandomData));
237
dea31012005-04-17 16:05:31 -0500238 /* Get adapter VPD information */
dea31012005-04-17 16:05:31 -0500239 lpfc_vpd_data = kmalloc(DMP_VPD_SIZE, GFP_KERNEL);
240 if (!lpfc_vpd_data)
James Smartd7c255b2008-08-24 21:50:00 -0400241 goto out_free_mbox;
dea31012005-04-17 16:05:31 -0500242 do {
James Smarta0c87cb2009-07-19 10:01:10 -0400243 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_VPD);
dea31012005-04-17 16:05:31 -0500244 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
245
246 if (rc != MBX_SUCCESS) {
247 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400248 "0441 VPD not present on adapter, "
dea31012005-04-17 16:05:31 -0500249 "mbxCmd x%x DUMP VPD, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500250 mb->mbxCommand, mb->mbxStatus);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500251 mb->un.varDmp.word_cnt = 0;
dea31012005-04-17 16:05:31 -0500252 }
James Smart04c68492009-05-22 14:52:52 -0400253 /* dump mem may return a zero when finished or we got a
254 * mailbox error, either way we are done.
255 */
256 if (mb->un.varDmp.word_cnt == 0)
257 break;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500258 if (mb->un.varDmp.word_cnt > DMP_VPD_SIZE - offset)
259 mb->un.varDmp.word_cnt = DMP_VPD_SIZE - offset;
James Smartd7c255b2008-08-24 21:50:00 -0400260 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
261 lpfc_vpd_data + offset,
James Smart92d7f7b2007-06-17 19:56:38 -0500262 mb->un.varDmp.word_cnt);
dea31012005-04-17 16:05:31 -0500263 offset += mb->un.varDmp.word_cnt;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500264 } while (mb->un.varDmp.word_cnt && offset < DMP_VPD_SIZE);
265 lpfc_parse_vpd(phba, lpfc_vpd_data, offset);
dea31012005-04-17 16:05:31 -0500266
267 kfree(lpfc_vpd_data);
dea31012005-04-17 16:05:31 -0500268out_free_mbox:
269 mempool_free(pmb, phba->mbox_mem_pool);
270 return 0;
271}
272
James Smarte59058c2008-08-24 21:49:00 -0400273/**
James Smart3621a712009-04-06 18:47:14 -0400274 * lpfc_config_async_cmpl - Completion handler for config async event mbox cmd
James Smarte59058c2008-08-24 21:49:00 -0400275 * @phba: pointer to lpfc hba data structure.
276 * @pmboxq: pointer to the driver internal queue element for mailbox command.
277 *
278 * This is the completion handler for driver's configuring asynchronous event
279 * mailbox command to the device. If the mailbox command returns successfully,
280 * it will set internal async event support flag to 1; otherwise, it will
281 * set internal async event support flag to 0.
282 **/
James Smart57127f12007-10-27 13:37:05 -0400283static void
284lpfc_config_async_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
285{
James Smart04c68492009-05-22 14:52:52 -0400286 if (pmboxq->u.mb.mbxStatus == MBX_SUCCESS)
James Smart57127f12007-10-27 13:37:05 -0400287 phba->temp_sensor_support = 1;
288 else
289 phba->temp_sensor_support = 0;
290 mempool_free(pmboxq, phba->mbox_mem_pool);
291 return;
292}
293
James Smarte59058c2008-08-24 21:49:00 -0400294/**
James Smart3621a712009-04-06 18:47:14 -0400295 * lpfc_dump_wakeup_param_cmpl - dump memory mailbox command completion handler
James Smart97207482008-12-04 22:39:19 -0500296 * @phba: pointer to lpfc hba data structure.
297 * @pmboxq: pointer to the driver internal queue element for mailbox command.
298 *
299 * This is the completion handler for dump mailbox command for getting
300 * wake up parameters. When this command complete, the response contain
301 * Option rom version of the HBA. This function translate the version number
302 * into a human readable string and store it in OptionROMVersion.
303 **/
304static void
305lpfc_dump_wakeup_param_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
306{
307 struct prog_id *prg;
308 uint32_t prog_id_word;
309 char dist = ' ';
310 /* character array used for decoding dist type. */
311 char dist_char[] = "nabx";
312
James Smart04c68492009-05-22 14:52:52 -0400313 if (pmboxq->u.mb.mbxStatus != MBX_SUCCESS) {
James Smart9f1e1b52008-12-04 22:39:40 -0500314 mempool_free(pmboxq, phba->mbox_mem_pool);
James Smart97207482008-12-04 22:39:19 -0500315 return;
James Smart9f1e1b52008-12-04 22:39:40 -0500316 }
James Smart97207482008-12-04 22:39:19 -0500317
318 prg = (struct prog_id *) &prog_id_word;
319
320 /* word 7 contain option rom version */
James Smart04c68492009-05-22 14:52:52 -0400321 prog_id_word = pmboxq->u.mb.un.varWords[7];
James Smart97207482008-12-04 22:39:19 -0500322
323 /* Decode the Option rom version word to a readable string */
324 if (prg->dist < 4)
325 dist = dist_char[prg->dist];
326
327 if ((prg->dist == 3) && (prg->num == 0))
James Smarta2fc4aef2014-09-03 12:57:55 -0400328 snprintf(phba->OptionROMVersion, 32, "%d.%d%d",
James Smart97207482008-12-04 22:39:19 -0500329 prg->ver, prg->rev, prg->lev);
330 else
James Smarta2fc4aef2014-09-03 12:57:55 -0400331 snprintf(phba->OptionROMVersion, 32, "%d.%d%d%c%d",
James Smart97207482008-12-04 22:39:19 -0500332 prg->ver, prg->rev, prg->lev,
333 dist, prg->num);
James Smart9f1e1b52008-12-04 22:39:40 -0500334 mempool_free(pmboxq, phba->mbox_mem_pool);
James Smart97207482008-12-04 22:39:19 -0500335 return;
336}
337
338/**
James Smart05580562011-05-24 11:40:48 -0400339 * lpfc_update_vport_wwn - Updates the fc_nodename, fc_portname,
340 * cfg_soft_wwnn, cfg_soft_wwpn
341 * @vport: pointer to lpfc vport data structure.
342 *
343 *
344 * Return codes
345 * None.
346 **/
347void
348lpfc_update_vport_wwn(struct lpfc_vport *vport)
349{
James Smartaeb3c812017-04-21 16:05:02 -0700350 uint8_t vvvl = vport->fc_sparam.cmn.valid_vendor_ver_level;
351 u32 *fawwpn_key = (u32 *)&vport->fc_sparam.un.vendorVersion[0];
352
James Smart05580562011-05-24 11:40:48 -0400353 /* If the soft name exists then update it using the service params */
354 if (vport->phba->cfg_soft_wwnn)
355 u64_to_wwn(vport->phba->cfg_soft_wwnn,
356 vport->fc_sparam.nodeName.u.wwn);
357 if (vport->phba->cfg_soft_wwpn)
358 u64_to_wwn(vport->phba->cfg_soft_wwpn,
359 vport->fc_sparam.portName.u.wwn);
360
361 /*
362 * If the name is empty or there exists a soft name
363 * then copy the service params name, otherwise use the fc name
364 */
365 if (vport->fc_nodename.u.wwn[0] == 0 || vport->phba->cfg_soft_wwnn)
366 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
367 sizeof(struct lpfc_name));
368 else
369 memcpy(&vport->fc_sparam.nodeName, &vport->fc_nodename,
370 sizeof(struct lpfc_name));
371
James Smartaeb3c812017-04-21 16:05:02 -0700372 /*
373 * If the port name has changed, then set the Param changes flag
374 * to unreg the login
375 */
376 if (vport->fc_portname.u.wwn[0] != 0 &&
377 memcmp(&vport->fc_portname, &vport->fc_sparam.portName,
378 sizeof(struct lpfc_name)))
379 vport->vport_flag |= FAWWPN_PARAM_CHG;
380
381 if (vport->fc_portname.u.wwn[0] == 0 ||
382 vport->phba->cfg_soft_wwpn ||
383 (vvvl == 1 && cpu_to_be32(*fawwpn_key) == FAPWWN_KEY_VENDOR) ||
384 vport->vport_flag & FAWWPN_SET) {
James Smart05580562011-05-24 11:40:48 -0400385 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
386 sizeof(struct lpfc_name));
James Smartaeb3c812017-04-21 16:05:02 -0700387 vport->vport_flag &= ~FAWWPN_SET;
388 if (vvvl == 1 && cpu_to_be32(*fawwpn_key) == FAPWWN_KEY_VENDOR)
389 vport->vport_flag |= FAWWPN_SET;
390 }
James Smart05580562011-05-24 11:40:48 -0400391 else
392 memcpy(&vport->fc_sparam.portName, &vport->fc_portname,
393 sizeof(struct lpfc_name));
394}
395
396/**
James Smart3621a712009-04-06 18:47:14 -0400397 * lpfc_config_port_post - Perform lpfc initialization after config port
James Smarte59058c2008-08-24 21:49:00 -0400398 * @phba: pointer to lpfc hba data structure.
399 *
400 * This routine will do LPFC initialization after the CONFIG_PORT mailbox
401 * command call. It performs all internal resource and state setups on the
402 * port: post IOCB buffers, enable appropriate host interrupt attentions,
403 * ELS ring timers, etc.
404 *
405 * Return codes
406 * 0 - success.
407 * Any other value - error.
408 **/
dea31012005-04-17 16:05:31 -0500409int
James Smart2e0fef82007-06-17 19:56:36 -0500410lpfc_config_port_post(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500411{
James Smart2e0fef82007-06-17 19:56:36 -0500412 struct lpfc_vport *vport = phba->pport;
James Smarta257bf92009-04-06 18:48:10 -0400413 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -0500414 LPFC_MBOXQ_t *pmb;
415 MAILBOX_t *mb;
416 struct lpfc_dmabuf *mp;
417 struct lpfc_sli *psli = &phba->sli;
418 uint32_t status, timeout;
James Smart2e0fef82007-06-17 19:56:36 -0500419 int i, j;
420 int rc;
dea31012005-04-17 16:05:31 -0500421
James Smart7af67052007-10-27 13:38:11 -0400422 spin_lock_irq(&phba->hbalock);
423 /*
424 * If the Config port completed correctly the HBA is not
425 * over heated any more.
426 */
427 if (phba->over_temp_state == HBA_OVER_TEMP)
428 phba->over_temp_state = HBA_NORMAL_TEMP;
429 spin_unlock_irq(&phba->hbalock);
430
dea31012005-04-17 16:05:31 -0500431 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
432 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -0500433 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500434 return -ENOMEM;
435 }
James Smart04c68492009-05-22 14:52:52 -0400436 mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -0500437
dea31012005-04-17 16:05:31 -0500438 /* Get login parameters for NID. */
James Smart9f1177a2010-02-26 14:12:57 -0500439 rc = lpfc_read_sparam(phba, pmb, 0);
440 if (rc) {
441 mempool_free(pmb, phba->mbox_mem_pool);
442 return -ENOMEM;
443 }
444
James Smarted957682007-06-17 19:56:37 -0500445 pmb->vport = vport;
dea31012005-04-17 16:05:31 -0500446 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500447 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400448 "0448 Adapter failed init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500449 "READ_SPARM mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500450 mb->mbxCommand, mb->mbxStatus);
James Smart2e0fef82007-06-17 19:56:36 -0500451 phba->link_state = LPFC_HBA_ERROR;
James Smart3e1f0712018-11-29 16:09:29 -0800452 mp = (struct lpfc_dmabuf *)pmb->ctx_buf;
James Smart9f1177a2010-02-26 14:12:57 -0500453 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500454 lpfc_mbuf_free(phba, mp->virt, mp->phys);
455 kfree(mp);
456 return -EIO;
457 }
458
James Smart3e1f0712018-11-29 16:09:29 -0800459 mp = (struct lpfc_dmabuf *)pmb->ctx_buf;
dea31012005-04-17 16:05:31 -0500460
James Smart2e0fef82007-06-17 19:56:36 -0500461 memcpy(&vport->fc_sparam, mp->virt, sizeof (struct serv_parm));
dea31012005-04-17 16:05:31 -0500462 lpfc_mbuf_free(phba, mp->virt, mp->phys);
463 kfree(mp);
James Smart3e1f0712018-11-29 16:09:29 -0800464 pmb->ctx_buf = NULL;
James Smart05580562011-05-24 11:40:48 -0400465 lpfc_update_vport_wwn(vport);
James Smarta257bf92009-04-06 18:48:10 -0400466
467 /* Update the fc_host data structures with new wwn. */
468 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
469 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
James Smart21e9a0a2009-05-22 14:53:21 -0400470 fc_host_max_npiv_vports(shost) = phba->max_vpi;
James Smarta257bf92009-04-06 18:48:10 -0400471
dea31012005-04-17 16:05:31 -0500472 /* If no serial number in VPD data, use low 6 bytes of WWNN */
473 /* This should be consolidated into parse_vpd ? - mr */
474 if (phba->SerialNumber[0] == 0) {
475 uint8_t *outptr;
476
James Smart2e0fef82007-06-17 19:56:36 -0500477 outptr = &vport->fc_nodename.u.s.IEEE[0];
dea31012005-04-17 16:05:31 -0500478 for (i = 0; i < 12; i++) {
479 status = *outptr++;
480 j = ((status & 0xf0) >> 4);
481 if (j <= 9)
482 phba->SerialNumber[i] =
483 (char)((uint8_t) 0x30 + (uint8_t) j);
484 else
485 phba->SerialNumber[i] =
486 (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
487 i++;
488 j = (status & 0xf);
489 if (j <= 9)
490 phba->SerialNumber[i] =
491 (char)((uint8_t) 0x30 + (uint8_t) j);
492 else
493 phba->SerialNumber[i] =
494 (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
495 }
496 }
497
dea31012005-04-17 16:05:31 -0500498 lpfc_read_config(phba, pmb);
James Smarted957682007-06-17 19:56:37 -0500499 pmb->vport = vport;
dea31012005-04-17 16:05:31 -0500500 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500501 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400502 "0453 Adapter failed to init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500503 "READ_CONFIG, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500504 mb->mbxCommand, mb->mbxStatus);
James Smart2e0fef82007-06-17 19:56:36 -0500505 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500506 mempool_free( pmb, phba->mbox_mem_pool);
507 return -EIO;
508 }
509
James Smarta0c87cb2009-07-19 10:01:10 -0400510 /* Check if the port is disabled */
511 lpfc_sli_read_link_ste(phba);
512
dea31012005-04-17 16:05:31 -0500513 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
James Smart572709e2013-07-15 18:32:43 -0400514 i = (mb->un.varRdConfig.max_xri + 1);
515 if (phba->cfg_hba_queue_depth > i) {
516 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
517 "3359 HBA queue depth changed from %d to %d\n",
518 phba->cfg_hba_queue_depth, i);
519 phba->cfg_hba_queue_depth = i;
520 }
521
522 /* Reset the DFT_LUN_Q_DEPTH to (max xri >> 3) */
523 i = (mb->un.varRdConfig.max_xri >> 3);
524 if (phba->pport->cfg_lun_queue_depth > i) {
525 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
526 "3360 LUN queue depth changed from %d to %d\n",
527 phba->pport->cfg_lun_queue_depth, i);
528 phba->pport->cfg_lun_queue_depth = i;
529 }
dea31012005-04-17 16:05:31 -0500530
531 phba->lmt = mb->un.varRdConfig.lmt;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500532
533 /* Get the default values for Model Name and Description */
534 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
535
James Smart2e0fef82007-06-17 19:56:36 -0500536 phba->link_state = LPFC_LINK_DOWN;
dea31012005-04-17 16:05:31 -0500537
James Smart0b727fe2007-10-27 13:37:25 -0400538 /* Only process IOCBs on ELS ring till hba_state is READY */
James Smart895427b2017-02-12 13:52:30 -0800539 if (psli->sli3_ring[LPFC_EXTRA_RING].sli.sli3.cmdringaddr)
540 psli->sli3_ring[LPFC_EXTRA_RING].flag |= LPFC_STOP_IOCB_EVENT;
541 if (psli->sli3_ring[LPFC_FCP_RING].sli.sli3.cmdringaddr)
542 psli->sli3_ring[LPFC_FCP_RING].flag |= LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -0500543
544 /* Post receive buffers for desired rings */
James Smarted957682007-06-17 19:56:37 -0500545 if (phba->sli_rev != 3)
546 lpfc_post_rcv_buf(phba);
dea31012005-04-17 16:05:31 -0500547
James Smart93996272008-08-24 21:50:30 -0400548 /*
549 * Configure HBA MSI-X attention conditions to messages if MSI-X mode
550 */
551 if (phba->intr_type == MSIX) {
552 rc = lpfc_config_msi(phba, pmb);
553 if (rc) {
554 mempool_free(pmb, phba->mbox_mem_pool);
555 return -EIO;
556 }
557 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
558 if (rc != MBX_SUCCESS) {
559 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
560 "0352 Config MSI mailbox command "
561 "failed, mbxCmd x%x, mbxStatus x%x\n",
James Smart04c68492009-05-22 14:52:52 -0400562 pmb->u.mb.mbxCommand,
563 pmb->u.mb.mbxStatus);
James Smart93996272008-08-24 21:50:30 -0400564 mempool_free(pmb, phba->mbox_mem_pool);
565 return -EIO;
566 }
567 }
568
James Smart04c68492009-05-22 14:52:52 -0400569 spin_lock_irq(&phba->hbalock);
James Smart93996272008-08-24 21:50:30 -0400570 /* Initialize ERATT handling flag */
571 phba->hba_flag &= ~HBA_ERATT_HANDLED;
572
dea31012005-04-17 16:05:31 -0500573 /* Enable appropriate host interrupts */
James Smart9940b972011-03-11 16:06:12 -0500574 if (lpfc_readl(phba->HCregaddr, &status)) {
575 spin_unlock_irq(&phba->hbalock);
576 return -EIO;
577 }
dea31012005-04-17 16:05:31 -0500578 status |= HC_MBINT_ENA | HC_ERINT_ENA | HC_LAINT_ENA;
579 if (psli->num_rings > 0)
580 status |= HC_R0INT_ENA;
581 if (psli->num_rings > 1)
582 status |= HC_R1INT_ENA;
583 if (psli->num_rings > 2)
584 status |= HC_R2INT_ENA;
585 if (psli->num_rings > 3)
586 status |= HC_R3INT_ENA;
587
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500588 if ((phba->cfg_poll & ENABLE_FCP_RING_POLLING) &&
589 (phba->cfg_poll & DISABLE_FCP_RING_INT))
James Smart93996272008-08-24 21:50:30 -0400590 status &= ~(HC_R0INT_ENA);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500591
dea31012005-04-17 16:05:31 -0500592 writel(status, phba->HCregaddr);
593 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -0500594 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500595
James Smart93996272008-08-24 21:50:30 -0400596 /* Set up ring-0 (ELS) timer */
597 timeout = phba->fc_ratov * 2;
James Smart256ec0d2013-04-17 20:14:58 -0400598 mod_timer(&vport->els_tmofunc,
599 jiffies + msecs_to_jiffies(1000 * timeout));
James Smart93996272008-08-24 21:50:30 -0400600 /* Set up heart beat (HB) timer */
James Smart256ec0d2013-04-17 20:14:58 -0400601 mod_timer(&phba->hb_tmofunc,
602 jiffies + msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smart858c9f62007-06-17 19:56:39 -0500603 phba->hb_outstanding = 0;
604 phba->last_completion_time = jiffies;
James Smart93996272008-08-24 21:50:30 -0400605 /* Set up error attention (ERATT) polling timer */
James Smart256ec0d2013-04-17 20:14:58 -0400606 mod_timer(&phba->eratt_poll,
James Smart65791f12016-07-06 12:35:56 -0700607 jiffies + msecs_to_jiffies(1000 * phba->eratt_poll_interval));
dea31012005-04-17 16:05:31 -0500608
James Smarta0c87cb2009-07-19 10:01:10 -0400609 if (phba->hba_flag & LINK_DISABLED) {
610 lpfc_printf_log(phba,
611 KERN_ERR, LOG_INIT,
612 "2598 Adapter Link is disabled.\n");
613 lpfc_down_link(phba, pmb);
614 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
615 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
616 if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
617 lpfc_printf_log(phba,
618 KERN_ERR, LOG_INIT,
619 "2599 Adapter failed to issue DOWN_LINK"
620 " mbox command rc 0x%x\n", rc);
621
622 mempool_free(pmb, phba->mbox_mem_pool);
623 return -EIO;
624 }
James Smarte40a02c2010-02-26 14:13:54 -0500625 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
James Smart026abb82011-12-13 13:20:45 -0500626 mempool_free(pmb, phba->mbox_mem_pool);
627 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
628 if (rc)
629 return rc;
dea31012005-04-17 16:05:31 -0500630 }
631 /* MBOX buffer will be freed in mbox compl */
James Smart57127f12007-10-27 13:37:05 -0400632 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart9f1177a2010-02-26 14:12:57 -0500633 if (!pmb) {
634 phba->link_state = LPFC_HBA_ERROR;
635 return -ENOMEM;
636 }
637
James Smart57127f12007-10-27 13:37:05 -0400638 lpfc_config_async(phba, pmb, LPFC_ELS_RING);
639 pmb->mbox_cmpl = lpfc_config_async_cmpl;
640 pmb->vport = phba->pport;
641 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -0500642
James Smart57127f12007-10-27 13:37:05 -0400643 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
644 lpfc_printf_log(phba,
645 KERN_ERR,
646 LOG_INIT,
647 "0456 Adapter failed to issue "
James Smarte4e74272009-07-19 10:01:38 -0400648 "ASYNCEVT_ENABLE mbox status x%x\n",
James Smart57127f12007-10-27 13:37:05 -0400649 rc);
650 mempool_free(pmb, phba->mbox_mem_pool);
651 }
James Smart97207482008-12-04 22:39:19 -0500652
653 /* Get Option rom version */
654 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart9f1177a2010-02-26 14:12:57 -0500655 if (!pmb) {
656 phba->link_state = LPFC_HBA_ERROR;
657 return -ENOMEM;
658 }
659
James Smart97207482008-12-04 22:39:19 -0500660 lpfc_dump_wakeup_param(phba, pmb);
661 pmb->mbox_cmpl = lpfc_dump_wakeup_param_cmpl;
662 pmb->vport = phba->pport;
663 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
664
665 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
666 lpfc_printf_log(phba, KERN_ERR, LOG_INIT, "0435 Adapter failed "
James Smarte4e74272009-07-19 10:01:38 -0400667 "to get Option ROM version status x%x\n", rc);
James Smart97207482008-12-04 22:39:19 -0500668 mempool_free(pmb, phba->mbox_mem_pool);
669 }
670
James Smartd7c255b2008-08-24 21:50:00 -0400671 return 0;
James Smartce8b3ce2006-07-06 15:50:36 -0400672}
673
James Smarte59058c2008-08-24 21:49:00 -0400674/**
James Smart84d1b002010-02-12 14:42:33 -0500675 * lpfc_hba_init_link - Initialize the FC link
676 * @phba: pointer to lpfc hba data structure.
James Smart6e7288d2010-06-07 15:23:35 -0400677 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
James Smart84d1b002010-02-12 14:42:33 -0500678 *
679 * This routine will issue the INIT_LINK mailbox command call.
680 * It is available to other drivers through the lpfc_hba data
681 * structure for use as a delayed link up mechanism with the
682 * module parameter lpfc_suppress_link_up.
683 *
684 * Return code
685 * 0 - success
686 * Any other value - error
687 **/
Rashika Kheriae399b222014-09-03 12:55:28 -0400688static int
James Smart6e7288d2010-06-07 15:23:35 -0400689lpfc_hba_init_link(struct lpfc_hba *phba, uint32_t flag)
James Smart84d1b002010-02-12 14:42:33 -0500690{
James Smart1b511972011-12-13 13:23:09 -0500691 return lpfc_hba_init_link_fc_topology(phba, phba->cfg_topology, flag);
692}
693
694/**
695 * lpfc_hba_init_link_fc_topology - Initialize FC link with desired topology
696 * @phba: pointer to lpfc hba data structure.
697 * @fc_topology: desired fc topology.
698 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
699 *
700 * This routine will issue the INIT_LINK mailbox command call.
701 * It is available to other drivers through the lpfc_hba data
702 * structure for use as a delayed link up mechanism with the
703 * module parameter lpfc_suppress_link_up.
704 *
705 * Return code
706 * 0 - success
707 * Any other value - error
708 **/
709int
710lpfc_hba_init_link_fc_topology(struct lpfc_hba *phba, uint32_t fc_topology,
711 uint32_t flag)
712{
James Smart84d1b002010-02-12 14:42:33 -0500713 struct lpfc_vport *vport = phba->pport;
714 LPFC_MBOXQ_t *pmb;
715 MAILBOX_t *mb;
716 int rc;
717
718 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
719 if (!pmb) {
720 phba->link_state = LPFC_HBA_ERROR;
721 return -ENOMEM;
722 }
723 mb = &pmb->u.mb;
724 pmb->vport = vport;
725
James Smart026abb82011-12-13 13:20:45 -0500726 if ((phba->cfg_link_speed > LPFC_USER_LINK_SPEED_MAX) ||
727 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_1G) &&
728 !(phba->lmt & LMT_1Gb)) ||
729 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_2G) &&
730 !(phba->lmt & LMT_2Gb)) ||
731 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_4G) &&
732 !(phba->lmt & LMT_4Gb)) ||
733 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_8G) &&
734 !(phba->lmt & LMT_8Gb)) ||
735 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_10G) &&
736 !(phba->lmt & LMT_10Gb)) ||
737 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_16G) &&
James Smartd38dd522015-08-31 16:48:17 -0400738 !(phba->lmt & LMT_16Gb)) ||
739 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_32G) &&
James Smartfbd8a6b2018-02-22 08:18:45 -0800740 !(phba->lmt & LMT_32Gb)) ||
741 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_64G) &&
742 !(phba->lmt & LMT_64Gb))) {
James Smart026abb82011-12-13 13:20:45 -0500743 /* Reset link speed to auto */
744 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
745 "1302 Invalid speed for this board:%d "
746 "Reset link speed to auto.\n",
747 phba->cfg_link_speed);
748 phba->cfg_link_speed = LPFC_USER_LINK_SPEED_AUTO;
749 }
James Smart1b511972011-12-13 13:23:09 -0500750 lpfc_init_link(phba, pmb, fc_topology, phba->cfg_link_speed);
James Smart84d1b002010-02-12 14:42:33 -0500751 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart1b511972011-12-13 13:23:09 -0500752 if (phba->sli_rev < LPFC_SLI_REV4)
753 lpfc_set_loopback_flag(phba);
James Smart6e7288d2010-06-07 15:23:35 -0400754 rc = lpfc_sli_issue_mbox(phba, pmb, flag);
James Smart76a95d72010-11-20 23:11:48 -0500755 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
James Smart84d1b002010-02-12 14:42:33 -0500756 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
757 "0498 Adapter failed to init, mbxCmd x%x "
758 "INIT_LINK, mbxStatus x%x\n",
759 mb->mbxCommand, mb->mbxStatus);
James Smart76a95d72010-11-20 23:11:48 -0500760 if (phba->sli_rev <= LPFC_SLI_REV3) {
761 /* Clear all interrupt enable conditions */
762 writel(0, phba->HCregaddr);
763 readl(phba->HCregaddr); /* flush */
764 /* Clear all pending interrupts */
765 writel(0xffffffff, phba->HAregaddr);
766 readl(phba->HAregaddr); /* flush */
767 }
James Smart84d1b002010-02-12 14:42:33 -0500768 phba->link_state = LPFC_HBA_ERROR;
James Smart6e7288d2010-06-07 15:23:35 -0400769 if (rc != MBX_BUSY || flag == MBX_POLL)
James Smart84d1b002010-02-12 14:42:33 -0500770 mempool_free(pmb, phba->mbox_mem_pool);
771 return -EIO;
772 }
James Smarte40a02c2010-02-26 14:13:54 -0500773 phba->cfg_suppress_link_up = LPFC_INITIALIZE_LINK;
James Smart6e7288d2010-06-07 15:23:35 -0400774 if (flag == MBX_POLL)
775 mempool_free(pmb, phba->mbox_mem_pool);
James Smart84d1b002010-02-12 14:42:33 -0500776
777 return 0;
778}
779
780/**
781 * lpfc_hba_down_link - this routine downs the FC link
James Smart6e7288d2010-06-07 15:23:35 -0400782 * @phba: pointer to lpfc hba data structure.
783 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
James Smart84d1b002010-02-12 14:42:33 -0500784 *
785 * This routine will issue the DOWN_LINK mailbox command call.
786 * It is available to other drivers through the lpfc_hba data
787 * structure for use to stop the link.
788 *
789 * Return code
790 * 0 - success
791 * Any other value - error
792 **/
Rashika Kheriae399b222014-09-03 12:55:28 -0400793static int
James Smart6e7288d2010-06-07 15:23:35 -0400794lpfc_hba_down_link(struct lpfc_hba *phba, uint32_t flag)
James Smart84d1b002010-02-12 14:42:33 -0500795{
796 LPFC_MBOXQ_t *pmb;
797 int rc;
798
799 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
800 if (!pmb) {
801 phba->link_state = LPFC_HBA_ERROR;
802 return -ENOMEM;
803 }
804
805 lpfc_printf_log(phba,
806 KERN_ERR, LOG_INIT,
807 "0491 Adapter Link is disabled.\n");
808 lpfc_down_link(phba, pmb);
809 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart6e7288d2010-06-07 15:23:35 -0400810 rc = lpfc_sli_issue_mbox(phba, pmb, flag);
James Smart84d1b002010-02-12 14:42:33 -0500811 if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
812 lpfc_printf_log(phba,
813 KERN_ERR, LOG_INIT,
814 "2522 Adapter failed to issue DOWN_LINK"
815 " mbox command rc 0x%x\n", rc);
816
817 mempool_free(pmb, phba->mbox_mem_pool);
818 return -EIO;
819 }
James Smart6e7288d2010-06-07 15:23:35 -0400820 if (flag == MBX_POLL)
821 mempool_free(pmb, phba->mbox_mem_pool);
822
James Smart84d1b002010-02-12 14:42:33 -0500823 return 0;
824}
825
826/**
James Smart3621a712009-04-06 18:47:14 -0400827 * lpfc_hba_down_prep - Perform lpfc uninitialization prior to HBA reset
James Smarte59058c2008-08-24 21:49:00 -0400828 * @phba: pointer to lpfc HBA data structure.
829 *
830 * This routine will do LPFC uninitialization before the HBA is reset when
831 * bringing down the SLI Layer.
832 *
833 * Return codes
834 * 0 - success.
835 * Any other value - error.
836 **/
dea31012005-04-17 16:05:31 -0500837int
James Smart2e0fef82007-06-17 19:56:36 -0500838lpfc_hba_down_prep(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500839{
James Smart1b32f6a2008-02-08 18:49:39 -0500840 struct lpfc_vport **vports;
841 int i;
James Smart3772a992009-05-22 14:50:54 -0400842
843 if (phba->sli_rev <= LPFC_SLI_REV3) {
844 /* Disable interrupts */
845 writel(0, phba->HCregaddr);
846 readl(phba->HCregaddr); /* flush */
847 }
dea31012005-04-17 16:05:31 -0500848
James Smart1b32f6a2008-02-08 18:49:39 -0500849 if (phba->pport->load_flag & FC_UNLOADING)
850 lpfc_cleanup_discovery_resources(phba->pport);
851 else {
852 vports = lpfc_create_vport_work_array(phba);
853 if (vports != NULL)
James Smart3772a992009-05-22 14:50:54 -0400854 for (i = 0; i <= phba->max_vports &&
855 vports[i] != NULL; i++)
James Smart1b32f6a2008-02-08 18:49:39 -0500856 lpfc_cleanup_discovery_resources(vports[i]);
857 lpfc_destroy_vport_work_array(phba, vports);
James Smart7f5f3d02008-02-08 18:50:14 -0500858 }
859 return 0;
dea31012005-04-17 16:05:31 -0500860}
861
James Smarte59058c2008-08-24 21:49:00 -0400862/**
James Smart68e814f2014-05-21 08:04:59 -0400863 * lpfc_sli4_free_sp_events - Cleanup sp_queue_events to free
864 * rspiocb which got deferred
865 *
866 * @phba: pointer to lpfc HBA data structure.
867 *
868 * This routine will cleanup completed slow path events after HBA is reset
869 * when bringing down the SLI Layer.
870 *
871 *
872 * Return codes
873 * void.
874 **/
875static void
876lpfc_sli4_free_sp_events(struct lpfc_hba *phba)
877{
878 struct lpfc_iocbq *rspiocbq;
879 struct hbq_dmabuf *dmabuf;
880 struct lpfc_cq_event *cq_event;
881
882 spin_lock_irq(&phba->hbalock);
883 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
884 spin_unlock_irq(&phba->hbalock);
885
886 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
887 /* Get the response iocb from the head of work queue */
888 spin_lock_irq(&phba->hbalock);
889 list_remove_head(&phba->sli4_hba.sp_queue_event,
890 cq_event, struct lpfc_cq_event, list);
891 spin_unlock_irq(&phba->hbalock);
892
893 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
894 case CQE_CODE_COMPL_WQE:
895 rspiocbq = container_of(cq_event, struct lpfc_iocbq,
896 cq_event);
897 lpfc_sli_release_iocbq(phba, rspiocbq);
898 break;
899 case CQE_CODE_RECEIVE:
900 case CQE_CODE_RECEIVE_V1:
901 dmabuf = container_of(cq_event, struct hbq_dmabuf,
902 cq_event);
903 lpfc_in_buf_free(phba, &dmabuf->dbuf);
904 }
905 }
906}
907
908/**
James Smartbcece5f2014-04-04 13:51:34 -0400909 * lpfc_hba_free_post_buf - Perform lpfc uninitialization after HBA reset
910 * @phba: pointer to lpfc HBA data structure.
911 *
912 * This routine will cleanup posted ELS buffers after the HBA is reset
913 * when bringing down the SLI Layer.
914 *
915 *
916 * Return codes
917 * void.
918 **/
919static void
920lpfc_hba_free_post_buf(struct lpfc_hba *phba)
921{
922 struct lpfc_sli *psli = &phba->sli;
923 struct lpfc_sli_ring *pring;
924 struct lpfc_dmabuf *mp, *next_mp;
James Smart07eab622014-04-04 13:51:44 -0400925 LIST_HEAD(buflist);
926 int count;
James Smartbcece5f2014-04-04 13:51:34 -0400927
928 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
929 lpfc_sli_hbqbuf_free_all(phba);
930 else {
931 /* Cleanup preposted buffers on the ELS ring */
James Smart895427b2017-02-12 13:52:30 -0800932 pring = &psli->sli3_ring[LPFC_ELS_RING];
James Smart07eab622014-04-04 13:51:44 -0400933 spin_lock_irq(&phba->hbalock);
934 list_splice_init(&pring->postbufq, &buflist);
935 spin_unlock_irq(&phba->hbalock);
936
937 count = 0;
938 list_for_each_entry_safe(mp, next_mp, &buflist, list) {
James Smartbcece5f2014-04-04 13:51:34 -0400939 list_del(&mp->list);
James Smart07eab622014-04-04 13:51:44 -0400940 count++;
James Smartbcece5f2014-04-04 13:51:34 -0400941 lpfc_mbuf_free(phba, mp->virt, mp->phys);
942 kfree(mp);
943 }
James Smart07eab622014-04-04 13:51:44 -0400944
945 spin_lock_irq(&phba->hbalock);
946 pring->postbufq_cnt -= count;
James Smartbcece5f2014-04-04 13:51:34 -0400947 spin_unlock_irq(&phba->hbalock);
948 }
949}
950
951/**
952 * lpfc_hba_clean_txcmplq - Perform lpfc uninitialization after HBA reset
953 * @phba: pointer to lpfc HBA data structure.
954 *
955 * This routine will cleanup the txcmplq after the HBA is reset when bringing
956 * down the SLI Layer.
957 *
958 * Return codes
959 * void
960 **/
961static void
962lpfc_hba_clean_txcmplq(struct lpfc_hba *phba)
963{
964 struct lpfc_sli *psli = &phba->sli;
James Smart895427b2017-02-12 13:52:30 -0800965 struct lpfc_queue *qp = NULL;
James Smartbcece5f2014-04-04 13:51:34 -0400966 struct lpfc_sli_ring *pring;
967 LIST_HEAD(completions);
968 int i;
James Smartc1dd9112018-01-30 15:58:57 -0800969 struct lpfc_iocbq *piocb, *next_iocb;
James Smartbcece5f2014-04-04 13:51:34 -0400970
James Smart895427b2017-02-12 13:52:30 -0800971 if (phba->sli_rev != LPFC_SLI_REV4) {
972 for (i = 0; i < psli->num_rings; i++) {
973 pring = &psli->sli3_ring[i];
James Smartbcece5f2014-04-04 13:51:34 -0400974 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -0800975 /* At this point in time the HBA is either reset or DOA
976 * Nothing should be on txcmplq as it will
977 * NEVER complete.
978 */
979 list_splice_init(&pring->txcmplq, &completions);
980 pring->txcmplq_cnt = 0;
James Smartbcece5f2014-04-04 13:51:34 -0400981 spin_unlock_irq(&phba->hbalock);
982
James Smart895427b2017-02-12 13:52:30 -0800983 lpfc_sli_abort_iocb_ring(phba, pring);
984 }
James Smartbcece5f2014-04-04 13:51:34 -0400985 /* Cancel all the IOCBs from the completions list */
James Smart895427b2017-02-12 13:52:30 -0800986 lpfc_sli_cancel_iocbs(phba, &completions,
987 IOSTAT_LOCAL_REJECT, IOERR_SLI_ABORTED);
988 return;
989 }
990 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
991 pring = qp->pring;
992 if (!pring)
993 continue;
994 spin_lock_irq(&pring->ring_lock);
James Smartc1dd9112018-01-30 15:58:57 -0800995 list_for_each_entry_safe(piocb, next_iocb,
996 &pring->txcmplq, list)
997 piocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smart895427b2017-02-12 13:52:30 -0800998 list_splice_init(&pring->txcmplq, &completions);
999 pring->txcmplq_cnt = 0;
1000 spin_unlock_irq(&pring->ring_lock);
James Smartbcece5f2014-04-04 13:51:34 -04001001 lpfc_sli_abort_iocb_ring(phba, pring);
1002 }
James Smart895427b2017-02-12 13:52:30 -08001003 /* Cancel all the IOCBs from the completions list */
1004 lpfc_sli_cancel_iocbs(phba, &completions,
1005 IOSTAT_LOCAL_REJECT, IOERR_SLI_ABORTED);
James Smartbcece5f2014-04-04 13:51:34 -04001006}
1007
1008/**
James Smart3772a992009-05-22 14:50:54 -04001009 * lpfc_hba_down_post_s3 - Perform lpfc uninitialization after HBA reset
James Smartbcece5f2014-04-04 13:51:34 -04001010 int i;
James Smarte59058c2008-08-24 21:49:00 -04001011 * @phba: pointer to lpfc HBA data structure.
1012 *
1013 * This routine will do uninitialization after the HBA is reset when bring
1014 * down the SLI Layer.
1015 *
1016 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001017 * 0 - success.
James Smarte59058c2008-08-24 21:49:00 -04001018 * Any other value - error.
1019 **/
James Smart3772a992009-05-22 14:50:54 -04001020static int
1021lpfc_hba_down_post_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05001022{
James Smartbcece5f2014-04-04 13:51:34 -04001023 lpfc_hba_free_post_buf(phba);
1024 lpfc_hba_clean_txcmplq(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -05001025 return 0;
1026}
James Smart5af5eee2010-10-22 11:06:38 -04001027
James Smartda0436e2009-05-22 14:51:39 -04001028/**
1029 * lpfc_hba_down_post_s4 - Perform lpfc uninitialization after HBA reset
1030 * @phba: pointer to lpfc HBA data structure.
1031 *
1032 * This routine will do uninitialization after the HBA is reset when bring
1033 * down the SLI Layer.
1034 *
1035 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001036 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -04001037 * Any other value - error.
1038 **/
1039static int
1040lpfc_hba_down_post_s4(struct lpfc_hba *phba)
1041{
James Smartc4908502019-01-28 11:14:28 -08001042 struct lpfc_io_buf *psb, *psb_next;
James Smart86c67372017-04-21 16:05:04 -07001043 struct lpfc_nvmet_rcv_ctx *ctxp, *ctxp_next;
James Smart5e5b5112019-01-28 11:14:22 -08001044 struct lpfc_sli4_hdw_queue *qp;
James Smartda0436e2009-05-22 14:51:39 -04001045 LIST_HEAD(aborts);
James Smart895427b2017-02-12 13:52:30 -08001046 LIST_HEAD(nvme_aborts);
James Smart86c67372017-04-21 16:05:04 -07001047 LIST_HEAD(nvmet_aborts);
James Smart0f65ff62010-02-26 14:14:23 -05001048 struct lpfc_sglq *sglq_entry = NULL;
James Smart5e5b5112019-01-28 11:14:22 -08001049 int cnt, idx;
James Smart0f65ff62010-02-26 14:14:23 -05001050
James Smart895427b2017-02-12 13:52:30 -08001051
1052 lpfc_sli_hbqbuf_free_all(phba);
James Smartbcece5f2014-04-04 13:51:34 -04001053 lpfc_hba_clean_txcmplq(phba);
1054
James Smartda0436e2009-05-22 14:51:39 -04001055 /* At this point in time the HBA is either reset or DOA. Either
1056 * way, nothing should be on lpfc_abts_els_sgl_list, it needs to be
James Smart895427b2017-02-12 13:52:30 -08001057 * on the lpfc_els_sgl_list so that it can either be freed if the
James Smartda0436e2009-05-22 14:51:39 -04001058 * driver is unloading or reposted if the driver is restarting
1059 * the port.
1060 */
James Smart895427b2017-02-12 13:52:30 -08001061 spin_lock_irq(&phba->hbalock); /* required for lpfc_els_sgl_list and */
James Smartda0436e2009-05-22 14:51:39 -04001062 /* scsl_buf_list */
James Smart895427b2017-02-12 13:52:30 -08001063 /* sgl_list_lock required because worker thread uses this
James Smartda0436e2009-05-22 14:51:39 -04001064 * list.
1065 */
James Smart895427b2017-02-12 13:52:30 -08001066 spin_lock(&phba->sli4_hba.sgl_list_lock);
James Smart0f65ff62010-02-26 14:14:23 -05001067 list_for_each_entry(sglq_entry,
1068 &phba->sli4_hba.lpfc_abts_els_sgl_list, list)
1069 sglq_entry->state = SGL_FREED;
1070
James Smartda0436e2009-05-22 14:51:39 -04001071 list_splice_init(&phba->sli4_hba.lpfc_abts_els_sgl_list,
James Smart895427b2017-02-12 13:52:30 -08001072 &phba->sli4_hba.lpfc_els_sgl_list);
1073
James Smartf358dd02017-02-12 13:52:34 -08001074
James Smart895427b2017-02-12 13:52:30 -08001075 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smart5e5b5112019-01-28 11:14:22 -08001076
1077 /* abts_xxxx_buf_list_lock required because worker thread uses this
James Smartda0436e2009-05-22 14:51:39 -04001078 * list.
1079 */
James Smart5e5b5112019-01-28 11:14:22 -08001080 cnt = 0;
1081 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
1082 qp = &phba->sli4_hba.hdwq[idx];
1083
1084 spin_lock(&qp->abts_scsi_buf_list_lock);
1085 list_splice_init(&qp->lpfc_abts_scsi_buf_list,
James Smart895427b2017-02-12 13:52:30 -08001086 &aborts);
James Smart895427b2017-02-12 13:52:30 -08001087
James Smart0794d602019-01-28 11:14:19 -08001088 list_for_each_entry_safe(psb, psb_next, &aborts, list) {
James Smart86c67372017-04-21 16:05:04 -07001089 psb->pCmd = NULL;
1090 psb->status = IOSTAT_SUCCESS;
James Smartcf1a1d32017-12-08 17:18:03 -08001091 cnt++;
James Smart86c67372017-04-21 16:05:04 -07001092 }
James Smart5e5b5112019-01-28 11:14:22 -08001093 spin_lock(&qp->io_buf_list_put_lock);
1094 list_splice_init(&aborts, &qp->lpfc_io_buf_list_put);
1095 qp->put_io_bufs += qp->abts_scsi_io_bufs;
1096 qp->abts_scsi_io_bufs = 0;
1097 spin_unlock(&qp->io_buf_list_put_lock);
1098 spin_unlock(&qp->abts_scsi_buf_list_lock);
James Smart86c67372017-04-21 16:05:04 -07001099
James Smart5e5b5112019-01-28 11:14:22 -08001100 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
1101 spin_lock(&qp->abts_nvme_buf_list_lock);
1102 list_splice_init(&qp->lpfc_abts_nvme_buf_list,
1103 &nvme_aborts);
1104 list_for_each_entry_safe(psb, psb_next, &nvme_aborts,
1105 list) {
1106 psb->pCmd = NULL;
1107 psb->status = IOSTAT_SUCCESS;
1108 cnt++;
1109 }
1110 spin_lock(&qp->io_buf_list_put_lock);
1111 qp->put_io_bufs += qp->abts_nvme_io_bufs;
1112 qp->abts_nvme_io_bufs = 0;
1113 list_splice_init(&nvme_aborts,
1114 &qp->lpfc_io_buf_list_put);
1115 spin_unlock(&qp->io_buf_list_put_lock);
1116 spin_unlock(&qp->abts_nvme_buf_list_lock);
1117
1118 }
1119 }
James Smart731eedc2019-03-12 16:30:12 -07001120 spin_unlock_irq(&phba->hbalock);
James Smart5e5b5112019-01-28 11:14:22 -08001121
1122 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
James Smart731eedc2019-03-12 16:30:12 -07001123 spin_lock_irq(&phba->sli4_hba.abts_nvmet_buf_list_lock);
James Smart5e5b5112019-01-28 11:14:22 -08001124 list_splice_init(&phba->sli4_hba.lpfc_abts_nvmet_ctx_list,
1125 &nvmet_aborts);
James Smart731eedc2019-03-12 16:30:12 -07001126 spin_unlock_irq(&phba->sli4_hba.abts_nvmet_buf_list_lock);
James Smart86c67372017-04-21 16:05:04 -07001127 list_for_each_entry_safe(ctxp, ctxp_next, &nvmet_aborts, list) {
1128 ctxp->flag &= ~(LPFC_NVMET_XBUSY | LPFC_NVMET_ABORT_OP);
James Smart6c621a22017-05-15 15:20:45 -07001129 lpfc_nvmet_ctxbuf_post(phba, ctxp->ctxbuf);
James Smart86c67372017-04-21 16:05:04 -07001130 }
James Smart895427b2017-02-12 13:52:30 -08001131 }
James Smart895427b2017-02-12 13:52:30 -08001132
James Smart68e814f2014-05-21 08:04:59 -04001133 lpfc_sli4_free_sp_events(phba);
James Smart5e5b5112019-01-28 11:14:22 -08001134 return cnt;
James Smartda0436e2009-05-22 14:51:39 -04001135}
1136
1137/**
1138 * lpfc_hba_down_post - Wrapper func for hba down post routine
1139 * @phba: pointer to lpfc HBA data structure.
1140 *
1141 * This routine wraps the actual SLI3 or SLI4 routine for performing
1142 * uninitialization after the HBA is reset when bring down the SLI Layer.
1143 *
1144 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001145 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -04001146 * Any other value - error.
1147 **/
1148int
1149lpfc_hba_down_post(struct lpfc_hba *phba)
1150{
1151 return (*phba->lpfc_hba_down_post)(phba);
1152}
Jamie Wellnitz41415862006-02-28 19:25:27 -05001153
James Smarte59058c2008-08-24 21:49:00 -04001154/**
James Smart3621a712009-04-06 18:47:14 -04001155 * lpfc_hb_timeout - The HBA-timer timeout handler
James Smarte59058c2008-08-24 21:49:00 -04001156 * @ptr: unsigned long holds the pointer to lpfc hba data structure.
1157 *
1158 * This is the HBA-timer timeout handler registered to the lpfc driver. When
1159 * this timer fires, a HBA timeout event shall be posted to the lpfc driver
1160 * work-port-events bitmap and the worker thread is notified. This timeout
1161 * event will be used by the worker thread to invoke the actual timeout
1162 * handler routine, lpfc_hb_timeout_handler. Any periodical operations will
1163 * be performed in the timeout handler and the HBA timeout event bit shall
1164 * be cleared by the worker thread after it has taken the event bitmap out.
1165 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001166static void
Kees Cookf22eb4d2017-09-06 20:24:26 -07001167lpfc_hb_timeout(struct timer_list *t)
James Smart858c9f62007-06-17 19:56:39 -05001168{
1169 struct lpfc_hba *phba;
James Smart5e9d9b82008-06-14 22:52:53 -04001170 uint32_t tmo_posted;
James Smart858c9f62007-06-17 19:56:39 -05001171 unsigned long iflag;
1172
Kees Cookf22eb4d2017-09-06 20:24:26 -07001173 phba = from_timer(phba, t, hb_tmofunc);
James Smart93996272008-08-24 21:50:30 -04001174
1175 /* Check for heart beat timeout conditions */
James Smart858c9f62007-06-17 19:56:39 -05001176 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -04001177 tmo_posted = phba->pport->work_port_events & WORKER_HB_TMO;
1178 if (!tmo_posted)
James Smart858c9f62007-06-17 19:56:39 -05001179 phba->pport->work_port_events |= WORKER_HB_TMO;
1180 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
1181
James Smart93996272008-08-24 21:50:30 -04001182 /* Tell the worker thread there is work to do */
James Smart5e9d9b82008-06-14 22:52:53 -04001183 if (!tmo_posted)
1184 lpfc_worker_wake_up(phba);
James Smart858c9f62007-06-17 19:56:39 -05001185 return;
1186}
1187
James Smarte59058c2008-08-24 21:49:00 -04001188/**
James Smart19ca7602010-11-20 23:11:55 -05001189 * lpfc_rrq_timeout - The RRQ-timer timeout handler
1190 * @ptr: unsigned long holds the pointer to lpfc hba data structure.
1191 *
1192 * This is the RRQ-timer timeout handler registered to the lpfc driver. When
1193 * this timer fires, a RRQ timeout event shall be posted to the lpfc driver
1194 * work-port-events bitmap and the worker thread is notified. This timeout
1195 * event will be used by the worker thread to invoke the actual timeout
1196 * handler routine, lpfc_rrq_handler. Any periodical operations will
1197 * be performed in the timeout handler and the RRQ timeout event bit shall
1198 * be cleared by the worker thread after it has taken the event bitmap out.
1199 **/
1200static void
Kees Cookf22eb4d2017-09-06 20:24:26 -07001201lpfc_rrq_timeout(struct timer_list *t)
James Smart19ca7602010-11-20 23:11:55 -05001202{
1203 struct lpfc_hba *phba;
James Smart19ca7602010-11-20 23:11:55 -05001204 unsigned long iflag;
1205
Kees Cookf22eb4d2017-09-06 20:24:26 -07001206 phba = from_timer(phba, t, rrq_tmr);
James Smart19ca7602010-11-20 23:11:55 -05001207 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart06918ac2014-02-20 09:57:57 -05001208 if (!(phba->pport->load_flag & FC_UNLOADING))
1209 phba->hba_flag |= HBA_RRQ_ACTIVE;
1210 else
1211 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
James Smart19ca7602010-11-20 23:11:55 -05001212 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
James Smart06918ac2014-02-20 09:57:57 -05001213
1214 if (!(phba->pport->load_flag & FC_UNLOADING))
1215 lpfc_worker_wake_up(phba);
James Smart19ca7602010-11-20 23:11:55 -05001216}
1217
1218/**
James Smart3621a712009-04-06 18:47:14 -04001219 * lpfc_hb_mbox_cmpl - The lpfc heart-beat mailbox command callback function
James Smarte59058c2008-08-24 21:49:00 -04001220 * @phba: pointer to lpfc hba data structure.
1221 * @pmboxq: pointer to the driver internal queue element for mailbox command.
1222 *
1223 * This is the callback function to the lpfc heart-beat mailbox command.
1224 * If configured, the lpfc driver issues the heart-beat mailbox command to
1225 * the HBA every LPFC_HB_MBOX_INTERVAL (current 5) seconds. At the time the
1226 * heart-beat mailbox command is issued, the driver shall set up heart-beat
1227 * timeout timer to LPFC_HB_MBOX_TIMEOUT (current 30) seconds and marks
1228 * heart-beat outstanding state. Once the mailbox command comes back and
1229 * no error conditions detected, the heart-beat mailbox command timer is
1230 * reset to LPFC_HB_MBOX_INTERVAL seconds and the heart-beat outstanding
1231 * state is cleared for the next heart-beat. If the timer expired with the
1232 * heart-beat outstanding state set, the driver will put the HBA offline.
1233 **/
James Smart858c9f62007-06-17 19:56:39 -05001234static void
1235lpfc_hb_mbox_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
1236{
1237 unsigned long drvr_flag;
1238
1239 spin_lock_irqsave(&phba->hbalock, drvr_flag);
1240 phba->hb_outstanding = 0;
1241 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
1242
James Smart93996272008-08-24 21:50:30 -04001243 /* Check and reset heart-beat timer is necessary */
James Smart858c9f62007-06-17 19:56:39 -05001244 mempool_free(pmboxq, phba->mbox_mem_pool);
1245 if (!(phba->pport->fc_flag & FC_OFFLINE_MODE) &&
1246 !(phba->link_state == LPFC_HBA_ERROR) &&
James Smart51ef4c22007-08-02 11:10:31 -04001247 !(phba->pport->load_flag & FC_UNLOADING))
James Smart858c9f62007-06-17 19:56:39 -05001248 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001249 jiffies +
1250 msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smart858c9f62007-06-17 19:56:39 -05001251 return;
1252}
1253
James Smart32517fc2019-01-28 11:14:33 -08001254static void
1255lpfc_hb_eq_delay_work(struct work_struct *work)
1256{
1257 struct lpfc_hba *phba = container_of(to_delayed_work(work),
1258 struct lpfc_hba, eq_delay_work);
1259 struct lpfc_eq_intr_info *eqi, *eqi_new;
1260 struct lpfc_queue *eq, *eq_next;
1261 unsigned char *eqcnt = NULL;
1262 uint32_t usdelay;
1263 int i;
1264
1265 if (!phba->cfg_auto_imax || phba->pport->load_flag & FC_UNLOADING)
1266 return;
1267
1268 if (phba->link_state == LPFC_HBA_ERROR ||
1269 phba->pport->fc_flag & FC_OFFLINE_MODE)
1270 goto requeue;
1271
1272 eqcnt = kcalloc(num_possible_cpus(), sizeof(unsigned char),
1273 GFP_KERNEL);
1274 if (!eqcnt)
1275 goto requeue;
1276
1277 for (i = 0; i < phba->cfg_irq_chann; i++) {
1278 eq = phba->sli4_hba.hdwq[i].hba_eq;
1279 if (eq && eqcnt[eq->last_cpu] < 2)
1280 eqcnt[eq->last_cpu]++;
1281 continue;
1282 }
1283
1284 for_each_present_cpu(i) {
1285 if (phba->cfg_irq_chann > 1 && eqcnt[i] < 2)
1286 continue;
1287
1288 eqi = per_cpu_ptr(phba->sli4_hba.eq_info, i);
1289
1290 usdelay = (eqi->icnt / LPFC_IMAX_THRESHOLD) *
1291 LPFC_EQ_DELAY_STEP;
1292 if (usdelay > LPFC_MAX_AUTO_EQ_DELAY)
1293 usdelay = LPFC_MAX_AUTO_EQ_DELAY;
1294
1295 eqi->icnt = 0;
1296
1297 list_for_each_entry_safe(eq, eq_next, &eqi->list, cpu_list) {
1298 if (eq->last_cpu != i) {
1299 eqi_new = per_cpu_ptr(phba->sli4_hba.eq_info,
1300 eq->last_cpu);
1301 list_move_tail(&eq->cpu_list, &eqi_new->list);
1302 continue;
1303 }
1304 if (usdelay != eq->q_mode)
1305 lpfc_modify_hba_eq_delay(phba, eq->hdwq, 1,
1306 usdelay);
1307 }
1308 }
1309
1310 kfree(eqcnt);
1311
1312requeue:
1313 queue_delayed_work(phba->wq, &phba->eq_delay_work,
1314 msecs_to_jiffies(LPFC_EQ_DELAY_MSECS));
1315}
1316
James Smarte59058c2008-08-24 21:49:00 -04001317/**
James Smartc4908502019-01-28 11:14:28 -08001318 * lpfc_hb_mxp_handler - Multi-XRI pools handler to adjust XRI distribution
1319 * @phba: pointer to lpfc hba data structure.
1320 *
1321 * For each heartbeat, this routine does some heuristic methods to adjust
1322 * XRI distribution. The goal is to fully utilize free XRIs.
1323 **/
1324static void lpfc_hb_mxp_handler(struct lpfc_hba *phba)
1325{
1326 u32 i;
1327 u32 hwq_count;
1328
1329 hwq_count = phba->cfg_hdw_queue;
1330 for (i = 0; i < hwq_count; i++) {
1331 /* Adjust XRIs in private pool */
1332 lpfc_adjust_pvt_pool_count(phba, i);
1333
1334 /* Adjust high watermark */
1335 lpfc_adjust_high_watermark(phba, i);
1336
1337#ifdef LPFC_MXP_STAT
1338 /* Snapshot pbl, pvt and busy count */
1339 lpfc_snapshot_mxp(phba, i);
1340#endif
1341 }
1342}
1343
James Smarte59058c2008-08-24 21:49:00 -04001344/**
James Smart3621a712009-04-06 18:47:14 -04001345 * lpfc_hb_timeout_handler - The HBA-timer timeout handler
James Smarte59058c2008-08-24 21:49:00 -04001346 * @phba: pointer to lpfc hba data structure.
1347 *
1348 * This is the actual HBA-timer timeout handler to be invoked by the worker
1349 * thread whenever the HBA timer fired and HBA-timeout event posted. This
1350 * handler performs any periodic operations needed for the device. If such
1351 * periodic event has already been attended to either in the interrupt handler
1352 * or by processing slow-ring or fast-ring events within the HBA-timer
1353 * timeout window (LPFC_HB_MBOX_INTERVAL), this handler just simply resets
1354 * the timer for the next timeout period. If lpfc heart-beat mailbox command
1355 * is configured and there is no heart-beat mailbox command outstanding, a
1356 * heart-beat mailbox is issued and timer set properly. Otherwise, if there
1357 * has been a heart-beat mailbox command outstanding, the HBA shall be put
1358 * to offline.
1359 **/
James Smart858c9f62007-06-17 19:56:39 -05001360void
1361lpfc_hb_timeout_handler(struct lpfc_hba *phba)
1362{
James Smart45ed1192009-10-02 15:17:02 -04001363 struct lpfc_vport **vports;
James Smart858c9f62007-06-17 19:56:39 -05001364 LPFC_MBOXQ_t *pmboxq;
James Smart0ff10d42008-01-11 01:52:36 -05001365 struct lpfc_dmabuf *buf_ptr;
James Smart45ed1192009-10-02 15:17:02 -04001366 int retval, i;
James Smart858c9f62007-06-17 19:56:39 -05001367 struct lpfc_sli *psli = &phba->sli;
James Smart0ff10d42008-01-11 01:52:36 -05001368 LIST_HEAD(completions);
James Smart858c9f62007-06-17 19:56:39 -05001369
James Smartc4908502019-01-28 11:14:28 -08001370 if (phba->cfg_xri_rebalancing) {
1371 /* Multi-XRI pools handler */
1372 lpfc_hb_mxp_handler(phba);
1373 }
James Smart858c9f62007-06-17 19:56:39 -05001374
James Smart45ed1192009-10-02 15:17:02 -04001375 vports = lpfc_create_vport_work_array(phba);
1376 if (vports != NULL)
James Smart4258e982015-12-16 18:11:58 -05001377 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart45ed1192009-10-02 15:17:02 -04001378 lpfc_rcv_seq_check_edtov(vports[i]);
James Smart4258e982015-12-16 18:11:58 -05001379 lpfc_fdmi_num_disc_check(vports[i]);
1380 }
James Smart45ed1192009-10-02 15:17:02 -04001381 lpfc_destroy_vport_work_array(phba, vports);
1382
James Smart858c9f62007-06-17 19:56:39 -05001383 if ((phba->link_state == LPFC_HBA_ERROR) ||
James Smart51ef4c22007-08-02 11:10:31 -04001384 (phba->pport->load_flag & FC_UNLOADING) ||
James Smart858c9f62007-06-17 19:56:39 -05001385 (phba->pport->fc_flag & FC_OFFLINE_MODE))
1386 return;
1387
1388 spin_lock_irq(&phba->pport->work_port_lock);
James Smart858c9f62007-06-17 19:56:39 -05001389
James Smart256ec0d2013-04-17 20:14:58 -04001390 if (time_after(phba->last_completion_time +
1391 msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL),
1392 jiffies)) {
James Smart858c9f62007-06-17 19:56:39 -05001393 spin_unlock_irq(&phba->pport->work_port_lock);
1394 if (!phba->hb_outstanding)
1395 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001396 jiffies +
1397 msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smart858c9f62007-06-17 19:56:39 -05001398 else
1399 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001400 jiffies +
1401 msecs_to_jiffies(1000 * LPFC_HB_MBOX_TIMEOUT));
James Smart858c9f62007-06-17 19:56:39 -05001402 return;
1403 }
1404 spin_unlock_irq(&phba->pport->work_port_lock);
1405
James Smart0ff10d42008-01-11 01:52:36 -05001406 if (phba->elsbuf_cnt &&
1407 (phba->elsbuf_cnt == phba->elsbuf_prev_cnt)) {
1408 spin_lock_irq(&phba->hbalock);
1409 list_splice_init(&phba->elsbuf, &completions);
1410 phba->elsbuf_cnt = 0;
1411 phba->elsbuf_prev_cnt = 0;
1412 spin_unlock_irq(&phba->hbalock);
1413
1414 while (!list_empty(&completions)) {
1415 list_remove_head(&completions, buf_ptr,
1416 struct lpfc_dmabuf, list);
1417 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
1418 kfree(buf_ptr);
1419 }
1420 }
1421 phba->elsbuf_prev_cnt = phba->elsbuf_cnt;
1422
James Smart858c9f62007-06-17 19:56:39 -05001423 /* If there is no heart beat outstanding, issue a heartbeat command */
James Smart13815c82008-01-11 01:52:48 -05001424 if (phba->cfg_enable_hba_heartbeat) {
1425 if (!phba->hb_outstanding) {
James Smartbc739052010-08-04 16:11:18 -04001426 if ((!(psli->sli_flag & LPFC_SLI_MBOX_ACTIVE)) &&
1427 (list_empty(&psli->mboxq))) {
1428 pmboxq = mempool_alloc(phba->mbox_mem_pool,
1429 GFP_KERNEL);
1430 if (!pmboxq) {
1431 mod_timer(&phba->hb_tmofunc,
1432 jiffies +
James Smart256ec0d2013-04-17 20:14:58 -04001433 msecs_to_jiffies(1000 *
1434 LPFC_HB_MBOX_INTERVAL));
James Smartbc739052010-08-04 16:11:18 -04001435 return;
1436 }
James Smart13815c82008-01-11 01:52:48 -05001437
James Smartbc739052010-08-04 16:11:18 -04001438 lpfc_heart_beat(phba, pmboxq);
1439 pmboxq->mbox_cmpl = lpfc_hb_mbox_cmpl;
1440 pmboxq->vport = phba->pport;
1441 retval = lpfc_sli_issue_mbox(phba, pmboxq,
1442 MBX_NOWAIT);
James Smart13815c82008-01-11 01:52:48 -05001443
James Smartbc739052010-08-04 16:11:18 -04001444 if (retval != MBX_BUSY &&
1445 retval != MBX_SUCCESS) {
1446 mempool_free(pmboxq,
1447 phba->mbox_mem_pool);
1448 mod_timer(&phba->hb_tmofunc,
1449 jiffies +
James Smart256ec0d2013-04-17 20:14:58 -04001450 msecs_to_jiffies(1000 *
1451 LPFC_HB_MBOX_INTERVAL));
James Smartbc739052010-08-04 16:11:18 -04001452 return;
1453 }
1454 phba->skipped_hb = 0;
1455 phba->hb_outstanding = 1;
1456 } else if (time_before_eq(phba->last_completion_time,
1457 phba->skipped_hb)) {
1458 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
1459 "2857 Last completion time not "
1460 " updated in %d ms\n",
1461 jiffies_to_msecs(jiffies
1462 - phba->last_completion_time));
1463 } else
1464 phba->skipped_hb = jiffies;
1465
James Smart858c9f62007-06-17 19:56:39 -05001466 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001467 jiffies +
1468 msecs_to_jiffies(1000 * LPFC_HB_MBOX_TIMEOUT));
James Smart858c9f62007-06-17 19:56:39 -05001469 return;
James Smart13815c82008-01-11 01:52:48 -05001470 } else {
1471 /*
1472 * If heart beat timeout called with hb_outstanding set
James Smartdcf2a4e2010-09-29 11:18:53 -04001473 * we need to give the hb mailbox cmd a chance to
1474 * complete or TMO.
James Smart13815c82008-01-11 01:52:48 -05001475 */
James Smartdcf2a4e2010-09-29 11:18:53 -04001476 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
1477 "0459 Adapter heartbeat still out"
1478 "standing:last compl time was %d ms.\n",
1479 jiffies_to_msecs(jiffies
1480 - phba->last_completion_time));
1481 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001482 jiffies +
1483 msecs_to_jiffies(1000 * LPFC_HB_MBOX_TIMEOUT));
James Smart858c9f62007-06-17 19:56:39 -05001484 }
James Smart4258e982015-12-16 18:11:58 -05001485 } else {
1486 mod_timer(&phba->hb_tmofunc,
1487 jiffies +
1488 msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smart858c9f62007-06-17 19:56:39 -05001489 }
1490}
1491
James Smarte59058c2008-08-24 21:49:00 -04001492/**
James Smart3621a712009-04-06 18:47:14 -04001493 * lpfc_offline_eratt - Bring lpfc offline on hardware error attention
James Smarte59058c2008-08-24 21:49:00 -04001494 * @phba: pointer to lpfc hba data structure.
1495 *
1496 * This routine is called to bring the HBA offline when HBA hardware error
1497 * other than Port Error 6 has been detected.
1498 **/
James Smart09372822008-01-11 01:52:54 -05001499static void
1500lpfc_offline_eratt(struct lpfc_hba *phba)
1501{
1502 struct lpfc_sli *psli = &phba->sli;
1503
1504 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001505 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart09372822008-01-11 01:52:54 -05001506 spin_unlock_irq(&phba->hbalock);
James Smart618a5232012-06-12 13:54:36 -04001507 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
James Smart09372822008-01-11 01:52:54 -05001508
1509 lpfc_offline(phba);
1510 lpfc_reset_barrier(phba);
James Smartf4b4c682009-05-22 14:53:12 -04001511 spin_lock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001512 lpfc_sli_brdreset(phba);
James Smartf4b4c682009-05-22 14:53:12 -04001513 spin_unlock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001514 lpfc_hba_down_post(phba);
1515 lpfc_sli_brdready(phba, HS_MBRDY);
1516 lpfc_unblock_mgmt_io(phba);
1517 phba->link_state = LPFC_HBA_ERROR;
1518 return;
1519}
1520
James Smarte59058c2008-08-24 21:49:00 -04001521/**
James Smartda0436e2009-05-22 14:51:39 -04001522 * lpfc_sli4_offline_eratt - Bring lpfc offline on SLI4 hardware error attention
1523 * @phba: pointer to lpfc hba data structure.
1524 *
1525 * This routine is called to bring a SLI4 HBA offline when HBA hardware error
1526 * other than Port Error 6 has been detected.
1527 **/
James Smarta88dbb62013-04-17 20:18:39 -04001528void
James Smartda0436e2009-05-22 14:51:39 -04001529lpfc_sli4_offline_eratt(struct lpfc_hba *phba)
1530{
James Smart946727d2015-04-07 15:07:09 -04001531 spin_lock_irq(&phba->hbalock);
1532 phba->link_state = LPFC_HBA_ERROR;
1533 spin_unlock_irq(&phba->hbalock);
1534
James Smart618a5232012-06-12 13:54:36 -04001535 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
James Smartda0436e2009-05-22 14:51:39 -04001536 lpfc_offline(phba);
James Smartda0436e2009-05-22 14:51:39 -04001537 lpfc_hba_down_post(phba);
James Smartda0436e2009-05-22 14:51:39 -04001538 lpfc_unblock_mgmt_io(phba);
James Smartda0436e2009-05-22 14:51:39 -04001539}
1540
1541/**
James Smarta257bf92009-04-06 18:48:10 -04001542 * lpfc_handle_deferred_eratt - The HBA hardware deferred error handler
1543 * @phba: pointer to lpfc hba data structure.
1544 *
1545 * This routine is invoked to handle the deferred HBA hardware error
1546 * conditions. This type of error is indicated by HBA by setting ER1
1547 * and another ER bit in the host status register. The driver will
1548 * wait until the ER1 bit clears before handling the error condition.
1549 **/
1550static void
1551lpfc_handle_deferred_eratt(struct lpfc_hba *phba)
1552{
1553 uint32_t old_host_status = phba->work_hs;
James Smarta257bf92009-04-06 18:48:10 -04001554 struct lpfc_sli *psli = &phba->sli;
1555
James Smartf4b4c682009-05-22 14:53:12 -04001556 /* If the pci channel is offline, ignore possible errors,
1557 * since we cannot communicate with the pci card anyway.
1558 */
1559 if (pci_channel_offline(phba->pcidev)) {
1560 spin_lock_irq(&phba->hbalock);
1561 phba->hba_flag &= ~DEFER_ERATT;
1562 spin_unlock_irq(&phba->hbalock);
1563 return;
1564 }
1565
James Smarta257bf92009-04-06 18:48:10 -04001566 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1567 "0479 Deferred Adapter Hardware Error "
1568 "Data: x%x x%x x%x\n",
1569 phba->work_hs,
1570 phba->work_status[0], phba->work_status[1]);
1571
1572 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001573 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smarta257bf92009-04-06 18:48:10 -04001574 spin_unlock_irq(&phba->hbalock);
1575
1576
1577 /*
1578 * Firmware stops when it triggred erratt. That could cause the I/Os
1579 * dropped by the firmware. Error iocb (I/O) on txcmplq and let the
1580 * SCSI layer retry it after re-establishing link.
1581 */
James Smartdb55fba2014-04-04 13:52:02 -04001582 lpfc_sli_abort_fcp_rings(phba);
James Smarta257bf92009-04-06 18:48:10 -04001583
1584 /*
1585 * There was a firmware error. Take the hba offline and then
1586 * attempt to restart it.
1587 */
James Smart618a5232012-06-12 13:54:36 -04001588 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smarta257bf92009-04-06 18:48:10 -04001589 lpfc_offline(phba);
1590
1591 /* Wait for the ER1 bit to clear.*/
1592 while (phba->work_hs & HS_FFER1) {
1593 msleep(100);
James Smart9940b972011-03-11 16:06:12 -05001594 if (lpfc_readl(phba->HSregaddr, &phba->work_hs)) {
1595 phba->work_hs = UNPLUG_ERR ;
1596 break;
1597 }
James Smarta257bf92009-04-06 18:48:10 -04001598 /* If driver is unloading let the worker thread continue */
1599 if (phba->pport->load_flag & FC_UNLOADING) {
1600 phba->work_hs = 0;
1601 break;
1602 }
1603 }
1604
1605 /*
1606 * This is to ptrotect against a race condition in which
1607 * first write to the host attention register clear the
1608 * host status register.
1609 */
1610 if ((!phba->work_hs) && (!(phba->pport->load_flag & FC_UNLOADING)))
1611 phba->work_hs = old_host_status & ~HS_FFER1;
1612
James Smart3772a992009-05-22 14:50:54 -04001613 spin_lock_irq(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -04001614 phba->hba_flag &= ~DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -04001615 spin_unlock_irq(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -04001616 phba->work_status[0] = readl(phba->MBslimaddr + 0xa8);
1617 phba->work_status[1] = readl(phba->MBslimaddr + 0xac);
1618}
1619
James Smart3772a992009-05-22 14:50:54 -04001620static void
1621lpfc_board_errevt_to_mgmt(struct lpfc_hba *phba)
1622{
1623 struct lpfc_board_event_header board_event;
1624 struct Scsi_Host *shost;
1625
1626 board_event.event_type = FC_REG_BOARD_EVENT;
1627 board_event.subcategory = LPFC_EVENT_PORTINTERR;
1628 shost = lpfc_shost_from_vport(phba->pport);
1629 fc_host_post_vendor_event(shost, fc_get_event_number(),
1630 sizeof(board_event),
1631 (char *) &board_event,
1632 LPFC_NL_VENDOR_ID);
1633}
1634
James Smarta257bf92009-04-06 18:48:10 -04001635/**
James Smart3772a992009-05-22 14:50:54 -04001636 * lpfc_handle_eratt_s3 - The SLI3 HBA hardware error handler
James Smarte59058c2008-08-24 21:49:00 -04001637 * @phba: pointer to lpfc hba data structure.
1638 *
1639 * This routine is invoked to handle the following HBA hardware error
1640 * conditions:
1641 * 1 - HBA error attention interrupt
1642 * 2 - DMA ring index out of range
1643 * 3 - Mailbox command came back as unknown
1644 **/
James Smart3772a992009-05-22 14:50:54 -04001645static void
1646lpfc_handle_eratt_s3(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001647{
James Smart2e0fef82007-06-17 19:56:36 -05001648 struct lpfc_vport *vport = phba->pport;
James Smart2e0fef82007-06-17 19:56:36 -05001649 struct lpfc_sli *psli = &phba->sli;
James Smartd2873e42006-08-18 17:46:43 -04001650 uint32_t event_data;
James Smart57127f12007-10-27 13:37:05 -04001651 unsigned long temperature;
1652 struct temp_event temp_event_data;
James Smart92d7f7b2007-06-17 19:56:38 -05001653 struct Scsi_Host *shost;
James Smart2e0fef82007-06-17 19:56:36 -05001654
Linas Vepstas8d63f372007-02-14 14:28:36 -06001655 /* If the pci channel is offline, ignore possible errors,
James Smart3772a992009-05-22 14:50:54 -04001656 * since we cannot communicate with the pci card anyway.
1657 */
1658 if (pci_channel_offline(phba->pcidev)) {
1659 spin_lock_irq(&phba->hbalock);
1660 phba->hba_flag &= ~DEFER_ERATT;
1661 spin_unlock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -06001662 return;
James Smart3772a992009-05-22 14:50:54 -04001663 }
1664
James Smart13815c82008-01-11 01:52:48 -05001665 /* If resets are disabled then leave the HBA alone and return */
1666 if (!phba->cfg_enable_hba_reset)
1667 return;
dea31012005-04-17 16:05:31 -05001668
James Smartea2151b2008-09-07 11:52:10 -04001669 /* Send an internal error event to mgmt application */
James Smart3772a992009-05-22 14:50:54 -04001670 lpfc_board_errevt_to_mgmt(phba);
James Smartea2151b2008-09-07 11:52:10 -04001671
James Smarta257bf92009-04-06 18:48:10 -04001672 if (phba->hba_flag & DEFER_ERATT)
1673 lpfc_handle_deferred_eratt(phba);
1674
James Smartdcf2a4e2010-09-29 11:18:53 -04001675 if ((phba->work_hs & HS_FFER6) || (phba->work_hs & HS_FFER8)) {
1676 if (phba->work_hs & HS_FFER6)
1677 /* Re-establishing Link */
1678 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1679 "1301 Re-establishing Link "
1680 "Data: x%x x%x x%x\n",
1681 phba->work_hs, phba->work_status[0],
1682 phba->work_status[1]);
1683 if (phba->work_hs & HS_FFER8)
1684 /* Device Zeroization */
1685 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1686 "2861 Host Authentication device "
1687 "zeroization Data:x%x x%x x%x\n",
1688 phba->work_hs, phba->work_status[0],
1689 phba->work_status[1]);
James Smart58da1ff2008-04-07 10:15:56 -04001690
James Smart92d7f7b2007-06-17 19:56:38 -05001691 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001692 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -05001693 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001694
1695 /*
1696 * Firmware stops when it triggled erratt with HS_FFER6.
1697 * That could cause the I/Os dropped by the firmware.
1698 * Error iocb (I/O) on txcmplq and let the SCSI layer
1699 * retry it after re-establishing link.
1700 */
James Smartdb55fba2014-04-04 13:52:02 -04001701 lpfc_sli_abort_fcp_rings(phba);
dea31012005-04-17 16:05:31 -05001702
dea31012005-04-17 16:05:31 -05001703 /*
1704 * There was a firmware error. Take the hba offline and then
1705 * attempt to restart it.
1706 */
James Smart618a5232012-06-12 13:54:36 -04001707 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
dea31012005-04-17 16:05:31 -05001708 lpfc_offline(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -05001709 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05001710 if (lpfc_online(phba) == 0) { /* Initialize the HBA */
James Smart46fa3112007-04-25 09:51:45 -04001711 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05001712 return;
1713 }
James Smart46fa3112007-04-25 09:51:45 -04001714 lpfc_unblock_mgmt_io(phba);
James Smart57127f12007-10-27 13:37:05 -04001715 } else if (phba->work_hs & HS_CRIT_TEMP) {
1716 temperature = readl(phba->MBslimaddr + TEMPERATURE_OFFSET);
1717 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
1718 temp_event_data.event_code = LPFC_CRIT_TEMP;
1719 temp_event_data.data = (uint32_t)temperature;
1720
1721 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -04001722 "0406 Adapter maximum temperature exceeded "
James Smart57127f12007-10-27 13:37:05 -04001723 "(%ld), taking this port offline "
1724 "Data: x%x x%x x%x\n",
1725 temperature, phba->work_hs,
1726 phba->work_status[0], phba->work_status[1]);
1727
1728 shost = lpfc_shost_from_vport(phba->pport);
1729 fc_host_post_vendor_event(shost, fc_get_event_number(),
1730 sizeof(temp_event_data),
1731 (char *) &temp_event_data,
1732 SCSI_NL_VID_TYPE_PCI
1733 | PCI_VENDOR_ID_EMULEX);
1734
James Smart7af67052007-10-27 13:38:11 -04001735 spin_lock_irq(&phba->hbalock);
James Smart7af67052007-10-27 13:38:11 -04001736 phba->over_temp_state = HBA_OVER_TEMP;
1737 spin_unlock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001738 lpfc_offline_eratt(phba);
James Smart57127f12007-10-27 13:37:05 -04001739
dea31012005-04-17 16:05:31 -05001740 } else {
1741 /* The if clause above forces this code path when the status
James Smart93996272008-08-24 21:50:30 -04001742 * failure is a value other than FFER6. Do not call the offline
1743 * twice. This is the adapter hardware error path.
dea31012005-04-17 16:05:31 -05001744 */
1745 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001746 "0457 Adapter Hardware Error "
dea31012005-04-17 16:05:31 -05001747 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001748 phba->work_hs,
dea31012005-04-17 16:05:31 -05001749 phba->work_status[0], phba->work_status[1]);
1750
James Smartd2873e42006-08-18 17:46:43 -04001751 event_data = FC_REG_DUMP_EVENT;
James Smart92d7f7b2007-06-17 19:56:38 -05001752 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05001753 fc_host_post_vendor_event(shost, fc_get_event_number(),
James Smartd2873e42006-08-18 17:46:43 -04001754 sizeof(event_data), (char *) &event_data,
1755 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
1756
James Smart09372822008-01-11 01:52:54 -05001757 lpfc_offline_eratt(phba);
dea31012005-04-17 16:05:31 -05001758 }
James Smart93996272008-08-24 21:50:30 -04001759 return;
dea31012005-04-17 16:05:31 -05001760}
1761
James Smarte59058c2008-08-24 21:49:00 -04001762/**
James Smart618a5232012-06-12 13:54:36 -04001763 * lpfc_sli4_port_sta_fn_reset - The SLI4 function reset due to port status reg
1764 * @phba: pointer to lpfc hba data structure.
1765 * @mbx_action: flag for mailbox shutdown action.
1766 *
1767 * This routine is invoked to perform an SLI4 port PCI function reset in
1768 * response to port status register polling attention. It waits for port
1769 * status register (ERR, RDY, RN) bits before proceeding with function reset.
1770 * During this process, interrupt vectors are freed and later requested
1771 * for handling possible port resource change.
1772 **/
1773static int
James Smarte10b2022014-02-20 09:57:43 -05001774lpfc_sli4_port_sta_fn_reset(struct lpfc_hba *phba, int mbx_action,
1775 bool en_rn_msg)
James Smart618a5232012-06-12 13:54:36 -04001776{
1777 int rc;
1778 uint32_t intr_mode;
1779
James Smart27d6ac02018-02-22 08:18:42 -08001780 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) >=
James Smart65791f12016-07-06 12:35:56 -07001781 LPFC_SLI_INTF_IF_TYPE_2) {
1782 /*
1783 * On error status condition, driver need to wait for port
1784 * ready before performing reset.
1785 */
1786 rc = lpfc_sli4_pdev_status_reg_wait(phba);
James Smart0e916ee2016-07-06 12:36:06 -07001787 if (rc)
James Smart65791f12016-07-06 12:35:56 -07001788 return rc;
James Smart618a5232012-06-12 13:54:36 -04001789 }
James Smart0e916ee2016-07-06 12:36:06 -07001790
James Smart65791f12016-07-06 12:35:56 -07001791 /* need reset: attempt for port recovery */
1792 if (en_rn_msg)
1793 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1794 "2887 Reset Needed: Attempting Port "
1795 "Recovery...\n");
1796 lpfc_offline_prep(phba, mbx_action);
1797 lpfc_offline(phba);
1798 /* release interrupt for possible resource change */
1799 lpfc_sli4_disable_intr(phba);
James Smart5a9eeff2018-11-29 16:09:32 -08001800 rc = lpfc_sli_brdrestart(phba);
1801 if (rc) {
1802 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1803 "6309 Failed to restart board\n");
1804 return rc;
1805 }
James Smart65791f12016-07-06 12:35:56 -07001806 /* request and enable interrupt */
1807 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
1808 if (intr_mode == LPFC_INTR_ERROR) {
1809 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1810 "3175 Failed to enable interrupt\n");
1811 return -EIO;
1812 }
1813 phba->intr_mode = intr_mode;
1814 rc = lpfc_online(phba);
1815 if (rc == 0)
1816 lpfc_unblock_mgmt_io(phba);
1817
James Smart618a5232012-06-12 13:54:36 -04001818 return rc;
1819}
1820
1821/**
James Smartda0436e2009-05-22 14:51:39 -04001822 * lpfc_handle_eratt_s4 - The SLI4 HBA hardware error handler
1823 * @phba: pointer to lpfc hba data structure.
1824 *
1825 * This routine is invoked to handle the SLI4 HBA hardware error attention
1826 * conditions.
1827 **/
1828static void
1829lpfc_handle_eratt_s4(struct lpfc_hba *phba)
1830{
1831 struct lpfc_vport *vport = phba->pport;
1832 uint32_t event_data;
1833 struct Scsi_Host *shost;
James Smart2fcee4b2010-12-15 17:57:46 -05001834 uint32_t if_type;
James Smart2e90f4b2011-12-13 13:22:37 -05001835 struct lpfc_register portstat_reg = {0};
1836 uint32_t reg_err1, reg_err2;
1837 uint32_t uerrlo_reg, uemasklo_reg;
James Smart65791f12016-07-06 12:35:56 -07001838 uint32_t smphr_port_status = 0, pci_rd_rc1, pci_rd_rc2;
James Smarte10b2022014-02-20 09:57:43 -05001839 bool en_rn_msg = true;
James Smart946727d2015-04-07 15:07:09 -04001840 struct temp_event temp_event_data;
James Smart65791f12016-07-06 12:35:56 -07001841 struct lpfc_register portsmphr_reg;
1842 int rc, i;
James Smartda0436e2009-05-22 14:51:39 -04001843
1844 /* If the pci channel is offline, ignore possible errors, since
1845 * we cannot communicate with the pci card anyway.
1846 */
James Smart32a93102019-03-12 16:30:13 -07001847 if (pci_channel_offline(phba->pcidev)) {
1848 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1849 "3166 pci channel is offline\n");
1850 lpfc_sli4_offline_eratt(phba);
James Smartda0436e2009-05-22 14:51:39 -04001851 return;
James Smart32a93102019-03-12 16:30:13 -07001852 }
James Smartda0436e2009-05-22 14:51:39 -04001853
James Smart65791f12016-07-06 12:35:56 -07001854 memset(&portsmphr_reg, 0, sizeof(portsmphr_reg));
James Smart2fcee4b2010-12-15 17:57:46 -05001855 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
1856 switch (if_type) {
1857 case LPFC_SLI_INTF_IF_TYPE_0:
James Smart2e90f4b2011-12-13 13:22:37 -05001858 pci_rd_rc1 = lpfc_readl(
1859 phba->sli4_hba.u.if_type0.UERRLOregaddr,
1860 &uerrlo_reg);
1861 pci_rd_rc2 = lpfc_readl(
1862 phba->sli4_hba.u.if_type0.UEMASKLOregaddr,
1863 &uemasklo_reg);
1864 /* consider PCI bus read error as pci_channel_offline */
1865 if (pci_rd_rc1 == -EIO && pci_rd_rc2 == -EIO)
1866 return;
James Smart65791f12016-07-06 12:35:56 -07001867 if (!(phba->hba_flag & HBA_RECOVERABLE_UE)) {
1868 lpfc_sli4_offline_eratt(phba);
1869 return;
1870 }
1871 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1872 "7623 Checking UE recoverable");
1873
1874 for (i = 0; i < phba->sli4_hba.ue_to_sr / 1000; i++) {
1875 if (lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
1876 &portsmphr_reg.word0))
1877 continue;
1878
1879 smphr_port_status = bf_get(lpfc_port_smphr_port_status,
1880 &portsmphr_reg);
1881 if ((smphr_port_status & LPFC_PORT_SEM_MASK) ==
1882 LPFC_PORT_SEM_UE_RECOVERABLE)
1883 break;
1884 /*Sleep for 1Sec, before checking SEMAPHORE */
1885 msleep(1000);
1886 }
1887
1888 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1889 "4827 smphr_port_status x%x : Waited %dSec",
1890 smphr_port_status, i);
1891
1892 /* Recoverable UE, reset the HBA device */
1893 if ((smphr_port_status & LPFC_PORT_SEM_MASK) ==
1894 LPFC_PORT_SEM_UE_RECOVERABLE) {
1895 for (i = 0; i < 20; i++) {
1896 msleep(1000);
1897 if (!lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
1898 &portsmphr_reg.word0) &&
1899 (LPFC_POST_STAGE_PORT_READY ==
1900 bf_get(lpfc_port_smphr_port_status,
1901 &portsmphr_reg))) {
1902 rc = lpfc_sli4_port_sta_fn_reset(phba,
1903 LPFC_MBX_NO_WAIT, en_rn_msg);
1904 if (rc == 0)
1905 return;
1906 lpfc_printf_log(phba,
1907 KERN_ERR, LOG_INIT,
1908 "4215 Failed to recover UE");
1909 break;
1910 }
1911 }
1912 }
1913 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1914 "7624 Firmware not ready: Failing UE recovery,"
1915 " waited %dSec", i);
James Smart2fcee4b2010-12-15 17:57:46 -05001916 lpfc_sli4_offline_eratt(phba);
1917 break;
James Smart946727d2015-04-07 15:07:09 -04001918
James Smart2fcee4b2010-12-15 17:57:46 -05001919 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart27d6ac02018-02-22 08:18:42 -08001920 case LPFC_SLI_INTF_IF_TYPE_6:
James Smart2e90f4b2011-12-13 13:22:37 -05001921 pci_rd_rc1 = lpfc_readl(
1922 phba->sli4_hba.u.if_type2.STATUSregaddr,
1923 &portstat_reg.word0);
1924 /* consider PCI bus read error as pci_channel_offline */
James Smart6b5151f2012-01-18 16:24:06 -05001925 if (pci_rd_rc1 == -EIO) {
1926 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1927 "3151 PCI bus read access failure: x%x\n",
1928 readl(phba->sli4_hba.u.if_type2.STATUSregaddr));
James Smart32a93102019-03-12 16:30:13 -07001929 lpfc_sli4_offline_eratt(phba);
James Smart2e90f4b2011-12-13 13:22:37 -05001930 return;
James Smart6b5151f2012-01-18 16:24:06 -05001931 }
James Smart2e90f4b2011-12-13 13:22:37 -05001932 reg_err1 = readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
1933 reg_err2 = readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
James Smart2fcee4b2010-12-15 17:57:46 -05001934 if (bf_get(lpfc_sliport_status_oti, &portstat_reg)) {
James Smart2fcee4b2010-12-15 17:57:46 -05001935 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1936 "2889 Port Overtemperature event, "
James Smart946727d2015-04-07 15:07:09 -04001937 "taking port offline Data: x%x x%x\n",
1938 reg_err1, reg_err2);
1939
James Smart310429e2016-07-06 12:35:54 -07001940 phba->sfp_alarm |= LPFC_TRANSGRESSION_HIGH_TEMPERATURE;
James Smart946727d2015-04-07 15:07:09 -04001941 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
1942 temp_event_data.event_code = LPFC_CRIT_TEMP;
1943 temp_event_data.data = 0xFFFFFFFF;
1944
1945 shost = lpfc_shost_from_vport(phba->pport);
1946 fc_host_post_vendor_event(shost, fc_get_event_number(),
1947 sizeof(temp_event_data),
1948 (char *)&temp_event_data,
1949 SCSI_NL_VID_TYPE_PCI
1950 | PCI_VENDOR_ID_EMULEX);
1951
James Smart2fcee4b2010-12-15 17:57:46 -05001952 spin_lock_irq(&phba->hbalock);
1953 phba->over_temp_state = HBA_OVER_TEMP;
1954 spin_unlock_irq(&phba->hbalock);
1955 lpfc_sli4_offline_eratt(phba);
James Smart946727d2015-04-07 15:07:09 -04001956 return;
James Smart2fcee4b2010-12-15 17:57:46 -05001957 }
James Smart2e90f4b2011-12-13 13:22:37 -05001958 if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
James Smarte10b2022014-02-20 09:57:43 -05001959 reg_err2 == SLIPORT_ERR2_REG_FW_RESTART) {
James Smart2e90f4b2011-12-13 13:22:37 -05001960 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte10b2022014-02-20 09:57:43 -05001961 "3143 Port Down: Firmware Update "
1962 "Detected\n");
1963 en_rn_msg = false;
1964 } else if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
James Smart2e90f4b2011-12-13 13:22:37 -05001965 reg_err2 == SLIPORT_ERR2_REG_FORCED_DUMP)
1966 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1967 "3144 Port Down: Debug Dump\n");
1968 else if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1969 reg_err2 == SLIPORT_ERR2_REG_FUNC_PROVISON)
1970 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1971 "3145 Port Down: Provisioning\n");
James Smart618a5232012-06-12 13:54:36 -04001972
James Smart946727d2015-04-07 15:07:09 -04001973 /* If resets are disabled then leave the HBA alone and return */
1974 if (!phba->cfg_enable_hba_reset)
1975 return;
1976
James Smart618a5232012-06-12 13:54:36 -04001977 /* Check port status register for function reset */
James Smarte10b2022014-02-20 09:57:43 -05001978 rc = lpfc_sli4_port_sta_fn_reset(phba, LPFC_MBX_NO_WAIT,
1979 en_rn_msg);
James Smart618a5232012-06-12 13:54:36 -04001980 if (rc == 0) {
1981 /* don't report event on forced debug dump */
1982 if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1983 reg_err2 == SLIPORT_ERR2_REG_FORCED_DUMP)
1984 return;
1985 else
1986 break;
James Smart2fcee4b2010-12-15 17:57:46 -05001987 }
James Smart618a5232012-06-12 13:54:36 -04001988 /* fall through for not able to recover */
James Smart6b5151f2012-01-18 16:24:06 -05001989 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1990 "3152 Unrecoverable error, bring the port "
1991 "offline\n");
James Smart2fcee4b2010-12-15 17:57:46 -05001992 lpfc_sli4_offline_eratt(phba);
1993 break;
1994 case LPFC_SLI_INTF_IF_TYPE_1:
1995 default:
1996 break;
1997 }
James Smart2e90f4b2011-12-13 13:22:37 -05001998 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
1999 "3123 Report dump event to upper layer\n");
2000 /* Send an internal error event to mgmt application */
2001 lpfc_board_errevt_to_mgmt(phba);
2002
2003 event_data = FC_REG_DUMP_EVENT;
2004 shost = lpfc_shost_from_vport(vport);
2005 fc_host_post_vendor_event(shost, fc_get_event_number(),
2006 sizeof(event_data), (char *) &event_data,
2007 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
James Smartda0436e2009-05-22 14:51:39 -04002008}
2009
2010/**
2011 * lpfc_handle_eratt - Wrapper func for handling hba error attention
2012 * @phba: pointer to lpfc HBA data structure.
2013 *
2014 * This routine wraps the actual SLI3 or SLI4 hba error attention handling
2015 * routine from the API jump table function pointer from the lpfc_hba struct.
2016 *
2017 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002018 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -04002019 * Any other value - error.
2020 **/
2021void
2022lpfc_handle_eratt(struct lpfc_hba *phba)
2023{
2024 (*phba->lpfc_handle_eratt)(phba);
2025}
2026
2027/**
James Smart3621a712009-04-06 18:47:14 -04002028 * lpfc_handle_latt - The HBA link event handler
James Smarte59058c2008-08-24 21:49:00 -04002029 * @phba: pointer to lpfc hba data structure.
2030 *
2031 * This routine is invoked from the worker thread to handle a HBA host
James Smart895427b2017-02-12 13:52:30 -08002032 * attention link event. SLI3 only.
James Smarte59058c2008-08-24 21:49:00 -04002033 **/
dea31012005-04-17 16:05:31 -05002034void
James Smart2e0fef82007-06-17 19:56:36 -05002035lpfc_handle_latt(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002036{
James Smart2e0fef82007-06-17 19:56:36 -05002037 struct lpfc_vport *vport = phba->pport;
2038 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05002039 LPFC_MBOXQ_t *pmb;
2040 volatile uint32_t control;
2041 struct lpfc_dmabuf *mp;
James Smart09372822008-01-11 01:52:54 -05002042 int rc = 0;
dea31012005-04-17 16:05:31 -05002043
2044 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart09372822008-01-11 01:52:54 -05002045 if (!pmb) {
2046 rc = 1;
dea31012005-04-17 16:05:31 -05002047 goto lpfc_handle_latt_err_exit;
James Smart09372822008-01-11 01:52:54 -05002048 }
dea31012005-04-17 16:05:31 -05002049
2050 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
James Smart09372822008-01-11 01:52:54 -05002051 if (!mp) {
2052 rc = 2;
dea31012005-04-17 16:05:31 -05002053 goto lpfc_handle_latt_free_pmb;
James Smart09372822008-01-11 01:52:54 -05002054 }
dea31012005-04-17 16:05:31 -05002055
2056 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
James Smart09372822008-01-11 01:52:54 -05002057 if (!mp->virt) {
2058 rc = 3;
dea31012005-04-17 16:05:31 -05002059 goto lpfc_handle_latt_free_mp;
James Smart09372822008-01-11 01:52:54 -05002060 }
dea31012005-04-17 16:05:31 -05002061
James.Smart@Emulex.Com6281bfe2005-11-28 11:41:33 -05002062 /* Cleanup any outstanding ELS commands */
James Smart549e55c2007-08-02 11:09:51 -04002063 lpfc_els_flush_all_cmd(phba);
dea31012005-04-17 16:05:31 -05002064
2065 psli->slistat.link_event++;
James Smart76a95d72010-11-20 23:11:48 -05002066 lpfc_read_topology(phba, pmb, mp);
2067 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
James Smart2e0fef82007-06-17 19:56:36 -05002068 pmb->vport = vport;
James Smart0d2b6b82008-06-14 22:52:47 -04002069 /* Block ELS IOCBs until we have processed this mbox command */
James Smart895427b2017-02-12 13:52:30 -08002070 phba->sli.sli3_ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
James Smart0b727fe2007-10-27 13:37:25 -04002071 rc = lpfc_sli_issue_mbox (phba, pmb, MBX_NOWAIT);
James Smart09372822008-01-11 01:52:54 -05002072 if (rc == MBX_NOT_FINISHED) {
2073 rc = 4;
James Smart14691152006-12-02 13:34:28 -05002074 goto lpfc_handle_latt_free_mbuf;
James Smart09372822008-01-11 01:52:54 -05002075 }
dea31012005-04-17 16:05:31 -05002076
2077 /* Clear Link Attention in HA REG */
James Smart2e0fef82007-06-17 19:56:36 -05002078 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002079 writel(HA_LATT, phba->HAregaddr);
2080 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05002081 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002082
2083 return;
2084
James Smart14691152006-12-02 13:34:28 -05002085lpfc_handle_latt_free_mbuf:
James Smart895427b2017-02-12 13:52:30 -08002086 phba->sli.sli3_ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart14691152006-12-02 13:34:28 -05002087 lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea31012005-04-17 16:05:31 -05002088lpfc_handle_latt_free_mp:
2089 kfree(mp);
2090lpfc_handle_latt_free_pmb:
James Smart1dcb58e2007-04-25 09:51:30 -04002091 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002092lpfc_handle_latt_err_exit:
2093 /* Enable Link attention interrupts */
James Smart2e0fef82007-06-17 19:56:36 -05002094 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002095 psli->sli_flag |= LPFC_PROCESS_LA;
2096 control = readl(phba->HCregaddr);
2097 control |= HC_LAINT_ENA;
2098 writel(control, phba->HCregaddr);
2099 readl(phba->HCregaddr); /* flush */
2100
2101 /* Clear Link Attention in HA REG */
2102 writel(HA_LATT, phba->HAregaddr);
2103 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05002104 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002105 lpfc_linkdown(phba);
James Smart2e0fef82007-06-17 19:56:36 -05002106 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002107
James Smart09372822008-01-11 01:52:54 -05002108 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
2109 "0300 LATT: Cannot issue READ_LA: Data:%d\n", rc);
dea31012005-04-17 16:05:31 -05002110
2111 return;
2112}
2113
James Smarte59058c2008-08-24 21:49:00 -04002114/**
James Smart3621a712009-04-06 18:47:14 -04002115 * lpfc_parse_vpd - Parse VPD (Vital Product Data)
James Smarte59058c2008-08-24 21:49:00 -04002116 * @phba: pointer to lpfc hba data structure.
2117 * @vpd: pointer to the vital product data.
2118 * @len: length of the vital product data in bytes.
2119 *
2120 * This routine parses the Vital Product Data (VPD). The VPD is treated as
2121 * an array of characters. In this routine, the ModelName, ProgramType, and
2122 * ModelDesc, etc. fields of the phba data structure will be populated.
2123 *
2124 * Return codes
2125 * 0 - pointer to the VPD passed in is NULL
2126 * 1 - success
2127 **/
James Smart3772a992009-05-22 14:50:54 -04002128int
James Smart2e0fef82007-06-17 19:56:36 -05002129lpfc_parse_vpd(struct lpfc_hba *phba, uint8_t *vpd, int len)
dea31012005-04-17 16:05:31 -05002130{
2131 uint8_t lenlo, lenhi;
Anton Blanchard07da60c2007-03-21 08:41:47 -05002132 int Length;
dea31012005-04-17 16:05:31 -05002133 int i, j;
2134 int finished = 0;
2135 int index = 0;
2136
2137 if (!vpd)
2138 return 0;
2139
2140 /* Vital Product */
James Smarted957682007-06-17 19:56:37 -05002141 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002142 "0455 Vital Product Data: x%x x%x x%x x%x\n",
dea31012005-04-17 16:05:31 -05002143 (uint32_t) vpd[0], (uint32_t) vpd[1], (uint32_t) vpd[2],
2144 (uint32_t) vpd[3]);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002145 while (!finished && (index < (len - 4))) {
dea31012005-04-17 16:05:31 -05002146 switch (vpd[index]) {
2147 case 0x82:
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002148 case 0x91:
dea31012005-04-17 16:05:31 -05002149 index += 1;
2150 lenlo = vpd[index];
2151 index += 1;
2152 lenhi = vpd[index];
2153 index += 1;
2154 i = ((((unsigned short)lenhi) << 8) + lenlo);
2155 index += i;
2156 break;
2157 case 0x90:
2158 index += 1;
2159 lenlo = vpd[index];
2160 index += 1;
2161 lenhi = vpd[index];
2162 index += 1;
2163 Length = ((((unsigned short)lenhi) << 8) + lenlo);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002164 if (Length > len - index)
2165 Length = len - index;
dea31012005-04-17 16:05:31 -05002166 while (Length > 0) {
2167 /* Look for Serial Number */
2168 if ((vpd[index] == 'S') && (vpd[index+1] == 'N')) {
2169 index += 2;
2170 i = vpd[index];
2171 index += 1;
2172 j = 0;
2173 Length -= (3+i);
2174 while(i--) {
2175 phba->SerialNumber[j++] = vpd[index++];
2176 if (j == 31)
2177 break;
2178 }
2179 phba->SerialNumber[j] = 0;
2180 continue;
2181 }
2182 else if ((vpd[index] == 'V') && (vpd[index+1] == '1')) {
2183 phba->vpd_flag |= VPD_MODEL_DESC;
2184 index += 2;
2185 i = vpd[index];
2186 index += 1;
2187 j = 0;
2188 Length -= (3+i);
2189 while(i--) {
2190 phba->ModelDesc[j++] = vpd[index++];
2191 if (j == 255)
2192 break;
2193 }
2194 phba->ModelDesc[j] = 0;
2195 continue;
2196 }
2197 else if ((vpd[index] == 'V') && (vpd[index+1] == '2')) {
2198 phba->vpd_flag |= VPD_MODEL_NAME;
2199 index += 2;
2200 i = vpd[index];
2201 index += 1;
2202 j = 0;
2203 Length -= (3+i);
2204 while(i--) {
2205 phba->ModelName[j++] = vpd[index++];
2206 if (j == 79)
2207 break;
2208 }
2209 phba->ModelName[j] = 0;
2210 continue;
2211 }
2212 else if ((vpd[index] == 'V') && (vpd[index+1] == '3')) {
2213 phba->vpd_flag |= VPD_PROGRAM_TYPE;
2214 index += 2;
2215 i = vpd[index];
2216 index += 1;
2217 j = 0;
2218 Length -= (3+i);
2219 while(i--) {
2220 phba->ProgramType[j++] = vpd[index++];
2221 if (j == 255)
2222 break;
2223 }
2224 phba->ProgramType[j] = 0;
2225 continue;
2226 }
2227 else if ((vpd[index] == 'V') && (vpd[index+1] == '4')) {
2228 phba->vpd_flag |= VPD_PORT;
2229 index += 2;
2230 i = vpd[index];
2231 index += 1;
2232 j = 0;
2233 Length -= (3+i);
2234 while(i--) {
James Smartcd1c8302011-10-10 21:33:25 -04002235 if ((phba->sli_rev == LPFC_SLI_REV4) &&
2236 (phba->sli4_hba.pport_name_sta ==
2237 LPFC_SLI4_PPNAME_GET)) {
2238 j++;
2239 index++;
2240 } else
2241 phba->Port[j++] = vpd[index++];
2242 if (j == 19)
2243 break;
dea31012005-04-17 16:05:31 -05002244 }
James Smartcd1c8302011-10-10 21:33:25 -04002245 if ((phba->sli_rev != LPFC_SLI_REV4) ||
2246 (phba->sli4_hba.pport_name_sta ==
2247 LPFC_SLI4_PPNAME_NON))
2248 phba->Port[j] = 0;
dea31012005-04-17 16:05:31 -05002249 continue;
2250 }
2251 else {
2252 index += 2;
2253 i = vpd[index];
2254 index += 1;
2255 index += i;
2256 Length -= (3 + i);
2257 }
2258 }
2259 finished = 0;
2260 break;
2261 case 0x78:
2262 finished = 1;
2263 break;
2264 default:
2265 index ++;
2266 break;
2267 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002268 }
dea31012005-04-17 16:05:31 -05002269
2270 return(1);
2271}
2272
James Smarte59058c2008-08-24 21:49:00 -04002273/**
James Smart3621a712009-04-06 18:47:14 -04002274 * lpfc_get_hba_model_desc - Retrieve HBA device model name and description
James Smarte59058c2008-08-24 21:49:00 -04002275 * @phba: pointer to lpfc hba data structure.
2276 * @mdp: pointer to the data structure to hold the derived model name.
2277 * @descp: pointer to the data structure to hold the derived description.
2278 *
2279 * This routine retrieves HBA's description based on its registered PCI device
2280 * ID. The @descp passed into this function points to an array of 256 chars. It
2281 * shall be returned with the model name, maximum speed, and the host bus type.
2282 * The @mdp passed into this function points to an array of 80 chars. When the
2283 * function returns, the @mdp will be filled with the model name.
2284 **/
dea31012005-04-17 16:05:31 -05002285static void
James Smart2e0fef82007-06-17 19:56:36 -05002286lpfc_get_hba_model_desc(struct lpfc_hba *phba, uint8_t *mdp, uint8_t *descp)
dea31012005-04-17 16:05:31 -05002287{
2288 lpfc_vpd_t *vp;
James.Smart@Emulex.Comfefcb2b2005-11-28 15:08:56 -05002289 uint16_t dev_id = phba->pcidev->device;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002290 int max_speed;
James Smart84774a42008-08-24 21:50:06 -04002291 int GE = 0;
James Smartda0436e2009-05-22 14:51:39 -04002292 int oneConnect = 0; /* default is not a oneConnect */
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002293 struct {
James Smarta747c9c2009-11-18 15:41:10 -05002294 char *name;
2295 char *bus;
2296 char *function;
2297 } m = {"<Unknown>", "", ""};
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002298
2299 if (mdp && mdp[0] != '\0'
2300 && descp && descp[0] != '\0')
2301 return;
2302
James Smartfbd8a6b2018-02-22 08:18:45 -08002303 if (phba->lmt & LMT_64Gb)
2304 max_speed = 64;
2305 else if (phba->lmt & LMT_32Gb)
James Smartd38dd522015-08-31 16:48:17 -04002306 max_speed = 32;
2307 else if (phba->lmt & LMT_16Gb)
James Smartc0c11512011-05-24 11:41:34 -04002308 max_speed = 16;
2309 else if (phba->lmt & LMT_10Gb)
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002310 max_speed = 10;
2311 else if (phba->lmt & LMT_8Gb)
2312 max_speed = 8;
2313 else if (phba->lmt & LMT_4Gb)
2314 max_speed = 4;
2315 else if (phba->lmt & LMT_2Gb)
2316 max_speed = 2;
James Smart4169d862012-09-29 11:32:09 -04002317 else if (phba->lmt & LMT_1Gb)
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002318 max_speed = 1;
James Smart4169d862012-09-29 11:32:09 -04002319 else
2320 max_speed = 0;
dea31012005-04-17 16:05:31 -05002321
2322 vp = &phba->vpd;
dea31012005-04-17 16:05:31 -05002323
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002324 switch (dev_id) {
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04002325 case PCI_DEVICE_ID_FIREFLY:
James Smart12222f42014-05-21 08:05:19 -04002326 m = (typeof(m)){"LP6000", "PCI",
2327 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04002328 break;
dea31012005-04-17 16:05:31 -05002329 case PCI_DEVICE_ID_SUPERFLY:
2330 if (vp->rev.biuRev >= 1 && vp->rev.biuRev <= 3)
James Smart12222f42014-05-21 08:05:19 -04002331 m = (typeof(m)){"LP7000", "PCI", ""};
dea31012005-04-17 16:05:31 -05002332 else
James Smart12222f42014-05-21 08:05:19 -04002333 m = (typeof(m)){"LP7000E", "PCI", ""};
2334 m.function = "Obsolete, Unsupported Fibre Channel Adapter";
dea31012005-04-17 16:05:31 -05002335 break;
2336 case PCI_DEVICE_ID_DRAGONFLY:
James Smarta747c9c2009-11-18 15:41:10 -05002337 m = (typeof(m)){"LP8000", "PCI",
James Smart12222f42014-05-21 08:05:19 -04002338 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002339 break;
2340 case PCI_DEVICE_ID_CENTAUR:
2341 if (FC_JEDEC_ID(vp->rev.biuRev) == CENTAUR_2G_JEDEC_ID)
James Smart12222f42014-05-21 08:05:19 -04002342 m = (typeof(m)){"LP9002", "PCI", ""};
dea31012005-04-17 16:05:31 -05002343 else
James Smart12222f42014-05-21 08:05:19 -04002344 m = (typeof(m)){"LP9000", "PCI", ""};
2345 m.function = "Obsolete, Unsupported Fibre Channel Adapter";
dea31012005-04-17 16:05:31 -05002346 break;
2347 case PCI_DEVICE_ID_RFLY:
James Smarta747c9c2009-11-18 15:41:10 -05002348 m = (typeof(m)){"LP952", "PCI",
James Smart12222f42014-05-21 08:05:19 -04002349 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002350 break;
2351 case PCI_DEVICE_ID_PEGASUS:
James Smarta747c9c2009-11-18 15:41:10 -05002352 m = (typeof(m)){"LP9802", "PCI-X",
James Smart12222f42014-05-21 08:05:19 -04002353 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002354 break;
2355 case PCI_DEVICE_ID_THOR:
James Smarta747c9c2009-11-18 15:41:10 -05002356 m = (typeof(m)){"LP10000", "PCI-X",
James Smart12222f42014-05-21 08:05:19 -04002357 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002358 break;
2359 case PCI_DEVICE_ID_VIPER:
James Smarta747c9c2009-11-18 15:41:10 -05002360 m = (typeof(m)){"LPX1000", "PCI-X",
James Smart12222f42014-05-21 08:05:19 -04002361 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002362 break;
2363 case PCI_DEVICE_ID_PFLY:
James Smarta747c9c2009-11-18 15:41:10 -05002364 m = (typeof(m)){"LP982", "PCI-X",
James Smart12222f42014-05-21 08:05:19 -04002365 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002366 break;
2367 case PCI_DEVICE_ID_TFLY:
James Smarta747c9c2009-11-18 15:41:10 -05002368 m = (typeof(m)){"LP1050", "PCI-X",
James Smart12222f42014-05-21 08:05:19 -04002369 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002370 break;
2371 case PCI_DEVICE_ID_HELIOS:
James Smarta747c9c2009-11-18 15:41:10 -05002372 m = (typeof(m)){"LP11000", "PCI-X2",
James Smart12222f42014-05-21 08:05:19 -04002373 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002374 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002375 case PCI_DEVICE_ID_HELIOS_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002376 m = (typeof(m)){"LP11000-SP", "PCI-X2",
James Smart12222f42014-05-21 08:05:19 -04002377 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002378 break;
2379 case PCI_DEVICE_ID_HELIOS_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002380 m = (typeof(m)){"LP11002-SP", "PCI-X2",
James Smart12222f42014-05-21 08:05:19 -04002381 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002382 break;
2383 case PCI_DEVICE_ID_NEPTUNE:
James Smart12222f42014-05-21 08:05:19 -04002384 m = (typeof(m)){"LPe1000", "PCIe",
2385 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002386 break;
2387 case PCI_DEVICE_ID_NEPTUNE_SCSP:
James Smart12222f42014-05-21 08:05:19 -04002388 m = (typeof(m)){"LPe1000-SP", "PCIe",
2389 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002390 break;
2391 case PCI_DEVICE_ID_NEPTUNE_DCSP:
James Smart12222f42014-05-21 08:05:19 -04002392 m = (typeof(m)){"LPe1002-SP", "PCIe",
2393 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002394 break;
dea31012005-04-17 16:05:31 -05002395 case PCI_DEVICE_ID_BMID:
James Smarta747c9c2009-11-18 15:41:10 -05002396 m = (typeof(m)){"LP1150", "PCI-X2", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002397 break;
2398 case PCI_DEVICE_ID_BSMB:
James Smart12222f42014-05-21 08:05:19 -04002399 m = (typeof(m)){"LP111", "PCI-X2",
2400 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002401 break;
2402 case PCI_DEVICE_ID_ZEPHYR:
James Smarta747c9c2009-11-18 15:41:10 -05002403 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002404 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002405 case PCI_DEVICE_ID_ZEPHYR_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002406 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002407 break;
2408 case PCI_DEVICE_ID_ZEPHYR_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002409 m = (typeof(m)){"LP2105", "PCIe", "FCoE Adapter"};
James Smarta257bf92009-04-06 18:48:10 -04002410 GE = 1;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002411 break;
dea31012005-04-17 16:05:31 -05002412 case PCI_DEVICE_ID_ZMID:
James Smarta747c9c2009-11-18 15:41:10 -05002413 m = (typeof(m)){"LPe1150", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002414 break;
2415 case PCI_DEVICE_ID_ZSMB:
James Smarta747c9c2009-11-18 15:41:10 -05002416 m = (typeof(m)){"LPe111", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002417 break;
2418 case PCI_DEVICE_ID_LP101:
James Smart12222f42014-05-21 08:05:19 -04002419 m = (typeof(m)){"LP101", "PCI-X",
2420 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002421 break;
2422 case PCI_DEVICE_ID_LP10000S:
James Smart12222f42014-05-21 08:05:19 -04002423 m = (typeof(m)){"LP10000-S", "PCI",
2424 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04002425 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002426 case PCI_DEVICE_ID_LP11000S:
James Smart12222f42014-05-21 08:05:19 -04002427 m = (typeof(m)){"LP11000-S", "PCI-X2",
2428 "Obsolete, Unsupported Fibre Channel Adapter"};
James Smart18a3b592006-12-02 13:35:08 -05002429 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002430 case PCI_DEVICE_ID_LPE11000S:
James Smart12222f42014-05-21 08:05:19 -04002431 m = (typeof(m)){"LPe11000-S", "PCIe",
2432 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Com5cc36b32005-11-28 11:42:19 -05002433 break;
James Smartb87eab32007-04-25 09:53:28 -04002434 case PCI_DEVICE_ID_SAT:
James Smarta747c9c2009-11-18 15:41:10 -05002435 m = (typeof(m)){"LPe12000", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002436 break;
2437 case PCI_DEVICE_ID_SAT_MID:
James Smarta747c9c2009-11-18 15:41:10 -05002438 m = (typeof(m)){"LPe1250", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002439 break;
2440 case PCI_DEVICE_ID_SAT_SMB:
James Smarta747c9c2009-11-18 15:41:10 -05002441 m = (typeof(m)){"LPe121", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002442 break;
2443 case PCI_DEVICE_ID_SAT_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002444 m = (typeof(m)){"LPe12002-SP", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002445 break;
2446 case PCI_DEVICE_ID_SAT_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002447 m = (typeof(m)){"LPe12000-SP", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002448 break;
2449 case PCI_DEVICE_ID_SAT_S:
James Smarta747c9c2009-11-18 15:41:10 -05002450 m = (typeof(m)){"LPe12000-S", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002451 break;
James Smart84774a42008-08-24 21:50:06 -04002452 case PCI_DEVICE_ID_HORNET:
James Smart12222f42014-05-21 08:05:19 -04002453 m = (typeof(m)){"LP21000", "PCIe",
2454 "Obsolete, Unsupported FCoE Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002455 GE = 1;
2456 break;
2457 case PCI_DEVICE_ID_PROTEUS_VF:
James Smarta747c9c2009-11-18 15:41:10 -05002458 m = (typeof(m)){"LPev12000", "PCIe IOV",
James Smart12222f42014-05-21 08:05:19 -04002459 "Obsolete, Unsupported Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002460 break;
2461 case PCI_DEVICE_ID_PROTEUS_PF:
James Smarta747c9c2009-11-18 15:41:10 -05002462 m = (typeof(m)){"LPev12000", "PCIe IOV",
James Smart12222f42014-05-21 08:05:19 -04002463 "Obsolete, Unsupported Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002464 break;
2465 case PCI_DEVICE_ID_PROTEUS_S:
James Smarta747c9c2009-11-18 15:41:10 -05002466 m = (typeof(m)){"LPemv12002-S", "PCIe IOV",
James Smart12222f42014-05-21 08:05:19 -04002467 "Obsolete, Unsupported Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002468 break;
James Smartda0436e2009-05-22 14:51:39 -04002469 case PCI_DEVICE_ID_TIGERSHARK:
2470 oneConnect = 1;
James Smarta747c9c2009-11-18 15:41:10 -05002471 m = (typeof(m)){"OCe10100", "PCIe", "FCoE"};
James Smartda0436e2009-05-22 14:51:39 -04002472 break;
James Smarta747c9c2009-11-18 15:41:10 -05002473 case PCI_DEVICE_ID_TOMCAT:
James Smart6669f9b2009-10-02 15:16:45 -04002474 oneConnect = 1;
James Smarta747c9c2009-11-18 15:41:10 -05002475 m = (typeof(m)){"OCe11100", "PCIe", "FCoE"};
2476 break;
2477 case PCI_DEVICE_ID_FALCON:
2478 m = (typeof(m)){"LPSe12002-ML1-E", "PCIe",
2479 "EmulexSecure Fibre"};
James Smart6669f9b2009-10-02 15:16:45 -04002480 break;
James Smart98fc5dd2010-06-07 15:24:29 -04002481 case PCI_DEVICE_ID_BALIUS:
2482 m = (typeof(m)){"LPVe12002", "PCIe Shared I/O",
James Smart12222f42014-05-21 08:05:19 -04002483 "Obsolete, Unsupported Fibre Channel Adapter"};
James Smart98fc5dd2010-06-07 15:24:29 -04002484 break;
James Smart085c6472010-11-20 23:11:37 -05002485 case PCI_DEVICE_ID_LANCER_FC:
James Smartc0c11512011-05-24 11:41:34 -04002486 m = (typeof(m)){"LPe16000", "PCIe", "Fibre Channel Adapter"};
James Smart085c6472010-11-20 23:11:37 -05002487 break;
James Smart12222f42014-05-21 08:05:19 -04002488 case PCI_DEVICE_ID_LANCER_FC_VF:
2489 m = (typeof(m)){"LPe16000", "PCIe",
2490 "Obsolete, Unsupported Fibre Channel Adapter"};
2491 break;
James Smart085c6472010-11-20 23:11:37 -05002492 case PCI_DEVICE_ID_LANCER_FCOE:
2493 oneConnect = 1;
James Smart079b5c92011-08-21 21:48:49 -04002494 m = (typeof(m)){"OCe15100", "PCIe", "FCoE"};
James Smart085c6472010-11-20 23:11:37 -05002495 break;
James Smart12222f42014-05-21 08:05:19 -04002496 case PCI_DEVICE_ID_LANCER_FCOE_VF:
2497 oneConnect = 1;
2498 m = (typeof(m)){"OCe15100", "PCIe",
2499 "Obsolete, Unsupported FCoE"};
2500 break;
James Smartd38dd522015-08-31 16:48:17 -04002501 case PCI_DEVICE_ID_LANCER_G6_FC:
2502 m = (typeof(m)){"LPe32000", "PCIe", "Fibre Channel Adapter"};
2503 break;
James Smartc238b9b2018-02-22 08:18:44 -08002504 case PCI_DEVICE_ID_LANCER_G7_FC:
2505 m = (typeof(m)){"LPe36000", "PCIe", "Fibre Channel Adapter"};
2506 break;
James Smartf8cafd32012-08-03 12:42:51 -04002507 case PCI_DEVICE_ID_SKYHAWK:
2508 case PCI_DEVICE_ID_SKYHAWK_VF:
2509 oneConnect = 1;
2510 m = (typeof(m)){"OCe14000", "PCIe", "FCoE"};
2511 break;
James.Smart@Emulex.Com5cc36b32005-11-28 11:42:19 -05002512 default:
James Smarta747c9c2009-11-18 15:41:10 -05002513 m = (typeof(m)){"Unknown", "", ""};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002514 break;
dea31012005-04-17 16:05:31 -05002515 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002516
2517 if (mdp && mdp[0] == '\0')
2518 snprintf(mdp, 79,"%s", m.name);
James Smartc0c11512011-05-24 11:41:34 -04002519 /*
2520 * oneConnect hba requires special processing, they are all initiators
James Smartda0436e2009-05-22 14:51:39 -04002521 * and we put the port number on the end
2522 */
2523 if (descp && descp[0] == '\0') {
2524 if (oneConnect)
2525 snprintf(descp, 255,
James Smart4169d862012-09-29 11:32:09 -04002526 "Emulex OneConnect %s, %s Initiator %s",
James Smarta747c9c2009-11-18 15:41:10 -05002527 m.name, m.function,
James Smartda0436e2009-05-22 14:51:39 -04002528 phba->Port);
James Smart4169d862012-09-29 11:32:09 -04002529 else if (max_speed == 0)
2530 snprintf(descp, 255,
Sebastian Herbszt290237d2015-08-31 16:48:08 -04002531 "Emulex %s %s %s",
James Smart4169d862012-09-29 11:32:09 -04002532 m.name, m.bus, m.function);
James Smartda0436e2009-05-22 14:51:39 -04002533 else
2534 snprintf(descp, 255,
2535 "Emulex %s %d%s %s %s",
James Smarta747c9c2009-11-18 15:41:10 -05002536 m.name, max_speed, (GE) ? "GE" : "Gb",
2537 m.bus, m.function);
James Smartda0436e2009-05-22 14:51:39 -04002538 }
dea31012005-04-17 16:05:31 -05002539}
2540
James Smarte59058c2008-08-24 21:49:00 -04002541/**
James Smart3621a712009-04-06 18:47:14 -04002542 * lpfc_post_buffer - Post IOCB(s) with DMA buffer descriptor(s) to a IOCB ring
James Smarte59058c2008-08-24 21:49:00 -04002543 * @phba: pointer to lpfc hba data structure.
2544 * @pring: pointer to a IOCB ring.
2545 * @cnt: the number of IOCBs to be posted to the IOCB ring.
2546 *
2547 * This routine posts a given number of IOCBs with the associated DMA buffer
2548 * descriptors specified by the cnt argument to the given IOCB ring.
2549 *
2550 * Return codes
2551 * The number of IOCBs NOT able to be posted to the IOCB ring.
2552 **/
dea31012005-04-17 16:05:31 -05002553int
James Smart495a7142008-06-14 22:52:59 -04002554lpfc_post_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring, int cnt)
dea31012005-04-17 16:05:31 -05002555{
2556 IOCB_t *icmd;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002557 struct lpfc_iocbq *iocb;
dea31012005-04-17 16:05:31 -05002558 struct lpfc_dmabuf *mp1, *mp2;
2559
2560 cnt += pring->missbufcnt;
2561
2562 /* While there are buffers to post */
2563 while (cnt > 0) {
2564 /* Allocate buffer for command iocb */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002565 iocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05002566 if (iocb == NULL) {
2567 pring->missbufcnt = cnt;
2568 return cnt;
2569 }
dea31012005-04-17 16:05:31 -05002570 icmd = &iocb->iocb;
2571
2572 /* 2 buffers can be posted per command */
2573 /* Allocate buffer to post */
2574 mp1 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
2575 if (mp1)
James Smart98c9ea52007-10-27 13:37:33 -04002576 mp1->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &mp1->phys);
2577 if (!mp1 || !mp1->virt) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002578 kfree(mp1);
James Bottomley604a3e32005-10-29 10:28:33 -05002579 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002580 pring->missbufcnt = cnt;
2581 return cnt;
2582 }
2583
2584 INIT_LIST_HEAD(&mp1->list);
2585 /* Allocate buffer to post */
2586 if (cnt > 1) {
2587 mp2 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
2588 if (mp2)
2589 mp2->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
2590 &mp2->phys);
James Smart98c9ea52007-10-27 13:37:33 -04002591 if (!mp2 || !mp2->virt) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002592 kfree(mp2);
dea31012005-04-17 16:05:31 -05002593 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
2594 kfree(mp1);
James Bottomley604a3e32005-10-29 10:28:33 -05002595 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002596 pring->missbufcnt = cnt;
2597 return cnt;
2598 }
2599
2600 INIT_LIST_HEAD(&mp2->list);
2601 } else {
2602 mp2 = NULL;
2603 }
2604
2605 icmd->un.cont64[0].addrHigh = putPaddrHigh(mp1->phys);
2606 icmd->un.cont64[0].addrLow = putPaddrLow(mp1->phys);
2607 icmd->un.cont64[0].tus.f.bdeSize = FCELSSIZE;
2608 icmd->ulpBdeCount = 1;
2609 cnt--;
2610 if (mp2) {
2611 icmd->un.cont64[1].addrHigh = putPaddrHigh(mp2->phys);
2612 icmd->un.cont64[1].addrLow = putPaddrLow(mp2->phys);
2613 icmd->un.cont64[1].tus.f.bdeSize = FCELSSIZE;
2614 cnt--;
2615 icmd->ulpBdeCount = 2;
2616 }
2617
2618 icmd->ulpCommand = CMD_QUE_RING_BUF64_CN;
2619 icmd->ulpLe = 1;
2620
James Smart3772a992009-05-22 14:50:54 -04002621 if (lpfc_sli_issue_iocb(phba, pring->ringno, iocb, 0) ==
2622 IOCB_ERROR) {
dea31012005-04-17 16:05:31 -05002623 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
2624 kfree(mp1);
2625 cnt++;
2626 if (mp2) {
2627 lpfc_mbuf_free(phba, mp2->virt, mp2->phys);
2628 kfree(mp2);
2629 cnt++;
2630 }
James Bottomley604a3e32005-10-29 10:28:33 -05002631 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002632 pring->missbufcnt = cnt;
dea31012005-04-17 16:05:31 -05002633 return cnt;
2634 }
dea31012005-04-17 16:05:31 -05002635 lpfc_sli_ringpostbuf_put(phba, pring, mp1);
James Smart92d7f7b2007-06-17 19:56:38 -05002636 if (mp2)
dea31012005-04-17 16:05:31 -05002637 lpfc_sli_ringpostbuf_put(phba, pring, mp2);
dea31012005-04-17 16:05:31 -05002638 }
2639 pring->missbufcnt = 0;
2640 return 0;
2641}
2642
James Smarte59058c2008-08-24 21:49:00 -04002643/**
James Smart3621a712009-04-06 18:47:14 -04002644 * lpfc_post_rcv_buf - Post the initial receive IOCB buffers to ELS ring
James Smarte59058c2008-08-24 21:49:00 -04002645 * @phba: pointer to lpfc hba data structure.
2646 *
2647 * This routine posts initial receive IOCB buffers to the ELS ring. The
2648 * current number of initial IOCB buffers specified by LPFC_BUF_RING0 is
James Smart895427b2017-02-12 13:52:30 -08002649 * set to 64 IOCBs. SLI3 only.
James Smarte59058c2008-08-24 21:49:00 -04002650 *
2651 * Return codes
2652 * 0 - success (currently always success)
2653 **/
dea31012005-04-17 16:05:31 -05002654static int
James Smart2e0fef82007-06-17 19:56:36 -05002655lpfc_post_rcv_buf(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002656{
2657 struct lpfc_sli *psli = &phba->sli;
2658
2659 /* Ring 0, ELS / CT buffers */
James Smart895427b2017-02-12 13:52:30 -08002660 lpfc_post_buffer(phba, &psli->sli3_ring[LPFC_ELS_RING], LPFC_BUF_RING0);
dea31012005-04-17 16:05:31 -05002661 /* Ring 2 - FCP no buffers needed */
2662
2663 return 0;
2664}
2665
2666#define S(N,V) (((V)<<(N))|((V)>>(32-(N))))
2667
James Smarte59058c2008-08-24 21:49:00 -04002668/**
James Smart3621a712009-04-06 18:47:14 -04002669 * lpfc_sha_init - Set up initial array of hash table entries
James Smarte59058c2008-08-24 21:49:00 -04002670 * @HashResultPointer: pointer to an array as hash table.
2671 *
2672 * This routine sets up the initial values to the array of hash table entries
2673 * for the LC HBAs.
2674 **/
dea31012005-04-17 16:05:31 -05002675static void
2676lpfc_sha_init(uint32_t * HashResultPointer)
2677{
2678 HashResultPointer[0] = 0x67452301;
2679 HashResultPointer[1] = 0xEFCDAB89;
2680 HashResultPointer[2] = 0x98BADCFE;
2681 HashResultPointer[3] = 0x10325476;
2682 HashResultPointer[4] = 0xC3D2E1F0;
2683}
2684
James Smarte59058c2008-08-24 21:49:00 -04002685/**
James Smart3621a712009-04-06 18:47:14 -04002686 * lpfc_sha_iterate - Iterate initial hash table with the working hash table
James Smarte59058c2008-08-24 21:49:00 -04002687 * @HashResultPointer: pointer to an initial/result hash table.
2688 * @HashWorkingPointer: pointer to an working hash table.
2689 *
2690 * This routine iterates an initial hash table pointed by @HashResultPointer
2691 * with the values from the working hash table pointeed by @HashWorkingPointer.
2692 * The results are putting back to the initial hash table, returned through
2693 * the @HashResultPointer as the result hash table.
2694 **/
dea31012005-04-17 16:05:31 -05002695static void
2696lpfc_sha_iterate(uint32_t * HashResultPointer, uint32_t * HashWorkingPointer)
2697{
2698 int t;
2699 uint32_t TEMP;
2700 uint32_t A, B, C, D, E;
2701 t = 16;
2702 do {
2703 HashWorkingPointer[t] =
2704 S(1,
2705 HashWorkingPointer[t - 3] ^ HashWorkingPointer[t -
2706 8] ^
2707 HashWorkingPointer[t - 14] ^ HashWorkingPointer[t - 16]);
2708 } while (++t <= 79);
2709 t = 0;
2710 A = HashResultPointer[0];
2711 B = HashResultPointer[1];
2712 C = HashResultPointer[2];
2713 D = HashResultPointer[3];
2714 E = HashResultPointer[4];
2715
2716 do {
2717 if (t < 20) {
2718 TEMP = ((B & C) | ((~B) & D)) + 0x5A827999;
2719 } else if (t < 40) {
2720 TEMP = (B ^ C ^ D) + 0x6ED9EBA1;
2721 } else if (t < 60) {
2722 TEMP = ((B & C) | (B & D) | (C & D)) + 0x8F1BBCDC;
2723 } else {
2724 TEMP = (B ^ C ^ D) + 0xCA62C1D6;
2725 }
2726 TEMP += S(5, A) + E + HashWorkingPointer[t];
2727 E = D;
2728 D = C;
2729 C = S(30, B);
2730 B = A;
2731 A = TEMP;
2732 } while (++t <= 79);
2733
2734 HashResultPointer[0] += A;
2735 HashResultPointer[1] += B;
2736 HashResultPointer[2] += C;
2737 HashResultPointer[3] += D;
2738 HashResultPointer[4] += E;
2739
2740}
2741
James Smarte59058c2008-08-24 21:49:00 -04002742/**
James Smart3621a712009-04-06 18:47:14 -04002743 * lpfc_challenge_key - Create challenge key based on WWPN of the HBA
James Smarte59058c2008-08-24 21:49:00 -04002744 * @RandomChallenge: pointer to the entry of host challenge random number array.
2745 * @HashWorking: pointer to the entry of the working hash array.
2746 *
2747 * This routine calculates the working hash array referred by @HashWorking
2748 * from the challenge random numbers associated with the host, referred by
2749 * @RandomChallenge. The result is put into the entry of the working hash
2750 * array and returned by reference through @HashWorking.
2751 **/
dea31012005-04-17 16:05:31 -05002752static void
2753lpfc_challenge_key(uint32_t * RandomChallenge, uint32_t * HashWorking)
2754{
2755 *HashWorking = (*RandomChallenge ^ *HashWorking);
2756}
2757
James Smarte59058c2008-08-24 21:49:00 -04002758/**
James Smart3621a712009-04-06 18:47:14 -04002759 * lpfc_hba_init - Perform special handling for LC HBA initialization
James Smarte59058c2008-08-24 21:49:00 -04002760 * @phba: pointer to lpfc hba data structure.
2761 * @hbainit: pointer to an array of unsigned 32-bit integers.
2762 *
2763 * This routine performs the special handling for LC HBA initialization.
2764 **/
dea31012005-04-17 16:05:31 -05002765void
2766lpfc_hba_init(struct lpfc_hba *phba, uint32_t *hbainit)
2767{
2768 int t;
2769 uint32_t *HashWorking;
James Smart2e0fef82007-06-17 19:56:36 -05002770 uint32_t *pwwnn = (uint32_t *) phba->wwnn;
dea31012005-04-17 16:05:31 -05002771
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +02002772 HashWorking = kcalloc(80, sizeof(uint32_t), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05002773 if (!HashWorking)
2774 return;
2775
dea31012005-04-17 16:05:31 -05002776 HashWorking[0] = HashWorking[78] = *pwwnn++;
2777 HashWorking[1] = HashWorking[79] = *pwwnn;
2778
2779 for (t = 0; t < 7; t++)
2780 lpfc_challenge_key(phba->RandomData + t, HashWorking + t);
2781
2782 lpfc_sha_init(hbainit);
2783 lpfc_sha_iterate(hbainit, HashWorking);
2784 kfree(HashWorking);
2785}
2786
James Smarte59058c2008-08-24 21:49:00 -04002787/**
James Smart3621a712009-04-06 18:47:14 -04002788 * lpfc_cleanup - Performs vport cleanups before deleting a vport
James Smarte59058c2008-08-24 21:49:00 -04002789 * @vport: pointer to a virtual N_Port data structure.
2790 *
2791 * This routine performs the necessary cleanups before deleting the @vport.
2792 * It invokes the discovery state machine to perform necessary state
2793 * transitions and to release the ndlps associated with the @vport. Note,
2794 * the physical port is treated as @vport 0.
2795 **/
James Smart87af33f2007-10-27 13:37:43 -04002796void
James Smart2e0fef82007-06-17 19:56:36 -05002797lpfc_cleanup(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002798{
James Smart87af33f2007-10-27 13:37:43 -04002799 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002800 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smarta8adb832007-10-27 13:37:53 -04002801 int i = 0;
dea31012005-04-17 16:05:31 -05002802
James Smart87af33f2007-10-27 13:37:43 -04002803 if (phba->link_state > LPFC_LINK_DOWN)
2804 lpfc_port_link_failure(vport);
2805
2806 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05002807 if (!NLP_CHK_NODE_ACT(ndlp)) {
2808 ndlp = lpfc_enable_node(vport, ndlp,
2809 NLP_STE_UNUSED_NODE);
2810 if (!ndlp)
2811 continue;
2812 spin_lock_irq(&phba->ndlp_lock);
2813 NLP_SET_FREE_REQ(ndlp);
2814 spin_unlock_irq(&phba->ndlp_lock);
2815 /* Trigger the release of the ndlp memory */
2816 lpfc_nlp_put(ndlp);
2817 continue;
2818 }
2819 spin_lock_irq(&phba->ndlp_lock);
2820 if (NLP_CHK_FREE_REQ(ndlp)) {
2821 /* The ndlp should not be in memory free mode already */
2822 spin_unlock_irq(&phba->ndlp_lock);
2823 continue;
2824 } else
2825 /* Indicate request for freeing ndlp memory */
2826 NLP_SET_FREE_REQ(ndlp);
2827 spin_unlock_irq(&phba->ndlp_lock);
2828
James Smart58da1ff2008-04-07 10:15:56 -04002829 if (vport->port_type != LPFC_PHYSICAL_PORT &&
2830 ndlp->nlp_DID == Fabric_DID) {
2831 /* Just free up ndlp with Fabric_DID for vports */
2832 lpfc_nlp_put(ndlp);
2833 continue;
2834 }
2835
James Smarteff4a012012-01-18 16:25:25 -05002836 /* take care of nodes in unused state before the state
2837 * machine taking action.
2838 */
2839 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE) {
2840 lpfc_nlp_put(ndlp);
2841 continue;
2842 }
2843
James Smart87af33f2007-10-27 13:37:43 -04002844 if (ndlp->nlp_type & NLP_FABRIC)
2845 lpfc_disc_state_machine(vport, ndlp, NULL,
2846 NLP_EVT_DEVICE_RECOVERY);
James Smarte47c9092008-02-08 18:49:26 -05002847
James Smart87af33f2007-10-27 13:37:43 -04002848 lpfc_disc_state_machine(vport, ndlp, NULL,
2849 NLP_EVT_DEVICE_RM);
2850 }
2851
James Smarta8adb832007-10-27 13:37:53 -04002852 /* At this point, ALL ndlp's should be gone
2853 * because of the previous NLP_EVT_DEVICE_RM.
2854 * Lets wait for this to happen, if needed.
2855 */
James Smart87af33f2007-10-27 13:37:43 -04002856 while (!list_empty(&vport->fc_nodes)) {
James Smarta8adb832007-10-27 13:37:53 -04002857 if (i++ > 3000) {
James Smart87af33f2007-10-27 13:37:43 -04002858 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
James Smarta8adb832007-10-27 13:37:53 -04002859 "0233 Nodelist not empty\n");
James Smarte47c9092008-02-08 18:49:26 -05002860 list_for_each_entry_safe(ndlp, next_ndlp,
2861 &vport->fc_nodes, nlp_listp) {
2862 lpfc_printf_vlog(ndlp->vport, KERN_ERR,
2863 LOG_NODE,
James Smartd7c255b2008-08-24 21:50:00 -04002864 "0282 did:x%x ndlp:x%p "
James Smarte47c9092008-02-08 18:49:26 -05002865 "usgmap:x%x refcnt:%d\n",
2866 ndlp->nlp_DID, (void *)ndlp,
2867 ndlp->nlp_usg_map,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01002868 kref_read(&ndlp->kref));
James Smarte47c9092008-02-08 18:49:26 -05002869 }
James Smarta8adb832007-10-27 13:37:53 -04002870 break;
James Smart87af33f2007-10-27 13:37:43 -04002871 }
James Smarta8adb832007-10-27 13:37:53 -04002872
2873 /* Wait for any activity on ndlps to settle */
2874 msleep(10);
James Smart87af33f2007-10-27 13:37:43 -04002875 }
James Smart1151e3e2011-02-16 12:39:35 -05002876 lpfc_cleanup_vports_rrqs(vport, NULL);
dea31012005-04-17 16:05:31 -05002877}
2878
James Smarte59058c2008-08-24 21:49:00 -04002879/**
James Smart3621a712009-04-06 18:47:14 -04002880 * lpfc_stop_vport_timers - Stop all the timers associated with a vport
James Smarte59058c2008-08-24 21:49:00 -04002881 * @vport: pointer to a virtual N_Port data structure.
2882 *
2883 * This routine stops all the timers associated with a @vport. This function
2884 * is invoked before disabling or deleting a @vport. Note that the physical
2885 * port is treated as @vport 0.
2886 **/
James Smart92d7f7b2007-06-17 19:56:38 -05002887void
2888lpfc_stop_vport_timers(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002889{
James Smart92d7f7b2007-06-17 19:56:38 -05002890 del_timer_sync(&vport->els_tmofunc);
James Smart92494142011-02-16 12:39:44 -05002891 del_timer_sync(&vport->delayed_disc_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05002892 lpfc_can_disctmo(vport);
2893 return;
dea31012005-04-17 16:05:31 -05002894}
2895
James Smarte59058c2008-08-24 21:49:00 -04002896/**
James Smartecfd03c2010-02-12 14:41:27 -05002897 * __lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2898 * @phba: pointer to lpfc hba data structure.
2899 *
2900 * This routine stops the SLI4 FCF rediscover wait timer if it's on. The
2901 * caller of this routine should already hold the host lock.
2902 **/
2903void
2904__lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2905{
James Smart5ac6b302010-10-22 11:05:36 -04002906 /* Clear pending FCF rediscovery wait flag */
2907 phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
2908
James Smartecfd03c2010-02-12 14:41:27 -05002909 /* Now, try to stop the timer */
2910 del_timer(&phba->fcf.redisc_wait);
2911}
2912
2913/**
2914 * lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2915 * @phba: pointer to lpfc hba data structure.
2916 *
2917 * This routine stops the SLI4 FCF rediscover wait timer if it's on. It
2918 * checks whether the FCF rediscovery wait timer is pending with the host
2919 * lock held before proceeding with disabling the timer and clearing the
2920 * wait timer pendig flag.
2921 **/
2922void
2923lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2924{
2925 spin_lock_irq(&phba->hbalock);
2926 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
2927 /* FCF rediscovery timer already fired or stopped */
2928 spin_unlock_irq(&phba->hbalock);
2929 return;
2930 }
2931 __lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
James Smart5ac6b302010-10-22 11:05:36 -04002932 /* Clear failover in progress flags */
2933 phba->fcf.fcf_flag &= ~(FCF_DEAD_DISC | FCF_ACVL_DISC);
James Smartecfd03c2010-02-12 14:41:27 -05002934 spin_unlock_irq(&phba->hbalock);
2935}
2936
2937/**
James Smart3772a992009-05-22 14:50:54 -04002938 * lpfc_stop_hba_timers - Stop all the timers associated with an HBA
James Smarte59058c2008-08-24 21:49:00 -04002939 * @phba: pointer to lpfc hba data structure.
2940 *
2941 * This routine stops all the timers associated with a HBA. This function is
2942 * invoked before either putting a HBA offline or unloading the driver.
2943 **/
James Smart3772a992009-05-22 14:50:54 -04002944void
2945lpfc_stop_hba_timers(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002946{
James Smartcdb42be2019-01-28 11:14:21 -08002947 if (phba->pport)
2948 lpfc_stop_vport_timers(phba->pport);
James Smart32517fc2019-01-28 11:14:33 -08002949 cancel_delayed_work_sync(&phba->eq_delay_work);
James Smart2e0fef82007-06-17 19:56:36 -05002950 del_timer_sync(&phba->sli.mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05002951 del_timer_sync(&phba->fabric_block_timer);
James Smart93996272008-08-24 21:50:30 -04002952 del_timer_sync(&phba->eratt_poll);
James Smart3772a992009-05-22 14:50:54 -04002953 del_timer_sync(&phba->hb_tmofunc);
James Smart1151e3e2011-02-16 12:39:35 -05002954 if (phba->sli_rev == LPFC_SLI_REV4) {
2955 del_timer_sync(&phba->rrq_tmr);
2956 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
2957 }
James Smart3772a992009-05-22 14:50:54 -04002958 phba->hb_outstanding = 0;
2959
2960 switch (phba->pci_dev_grp) {
2961 case LPFC_PCI_DEV_LP:
2962 /* Stop any LightPulse device specific driver timers */
2963 del_timer_sync(&phba->fcp_poll_timer);
2964 break;
2965 case LPFC_PCI_DEV_OC:
2966 /* Stop any OneConnect device sepcific driver timers */
James Smartecfd03c2010-02-12 14:41:27 -05002967 lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
James Smart3772a992009-05-22 14:50:54 -04002968 break;
2969 default:
2970 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2971 "0297 Invalid device group (x%x)\n",
2972 phba->pci_dev_grp);
2973 break;
2974 }
James Smart2e0fef82007-06-17 19:56:36 -05002975 return;
dea31012005-04-17 16:05:31 -05002976}
2977
James Smarte59058c2008-08-24 21:49:00 -04002978/**
James Smart3621a712009-04-06 18:47:14 -04002979 * lpfc_block_mgmt_io - Mark a HBA's management interface as blocked
James Smarte59058c2008-08-24 21:49:00 -04002980 * @phba: pointer to lpfc hba data structure.
2981 *
2982 * This routine marks a HBA's management interface as blocked. Once the HBA's
2983 * management interface is marked as blocked, all the user space access to
2984 * the HBA, whether they are from sysfs interface or libdfc interface will
2985 * all be blocked. The HBA is set to block the management interface when the
2986 * driver prepares the HBA interface for online or offline.
2987 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01002988static void
James Smart618a5232012-06-12 13:54:36 -04002989lpfc_block_mgmt_io(struct lpfc_hba *phba, int mbx_action)
Adrian Bunka6ababd2007-11-05 18:07:33 +01002990{
2991 unsigned long iflag;
James Smart6e7288d2010-06-07 15:23:35 -04002992 uint8_t actcmd = MBX_HEARTBEAT;
2993 unsigned long timeout;
2994
Adrian Bunka6ababd2007-11-05 18:07:33 +01002995 spin_lock_irqsave(&phba->hbalock, iflag);
2996 phba->sli.sli_flag |= LPFC_BLOCK_MGMT_IO;
James Smart618a5232012-06-12 13:54:36 -04002997 spin_unlock_irqrestore(&phba->hbalock, iflag);
2998 if (mbx_action == LPFC_MBX_NO_WAIT)
2999 return;
3000 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
3001 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta183a152011-10-10 21:32:43 -04003002 if (phba->sli.mbox_active) {
James Smart6e7288d2010-06-07 15:23:35 -04003003 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
James Smarta183a152011-10-10 21:32:43 -04003004 /* Determine how long we might wait for the active mailbox
3005 * command to be gracefully completed by firmware.
3006 */
3007 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
3008 phba->sli.mbox_active) * 1000) + jiffies;
3009 }
Adrian Bunka6ababd2007-11-05 18:07:33 +01003010 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta183a152011-10-10 21:32:43 -04003011
James Smart6e7288d2010-06-07 15:23:35 -04003012 /* Wait for the outstnading mailbox command to complete */
3013 while (phba->sli.mbox_active) {
3014 /* Check active mailbox complete status every 2ms */
3015 msleep(2);
3016 if (time_after(jiffies, timeout)) {
3017 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3018 "2813 Mgmt IO is Blocked %x "
3019 "- mbox cmd %x still active\n",
3020 phba->sli.sli_flag, actcmd);
3021 break;
3022 }
3023 }
Adrian Bunka6ababd2007-11-05 18:07:33 +01003024}
3025
James Smarte59058c2008-08-24 21:49:00 -04003026/**
James Smart6b5151f2012-01-18 16:24:06 -05003027 * lpfc_sli4_node_prep - Assign RPIs for active nodes.
3028 * @phba: pointer to lpfc hba data structure.
3029 *
3030 * Allocate RPIs for all active remote nodes. This is needed whenever
3031 * an SLI4 adapter is reset and the driver is not unloading. Its purpose
3032 * is to fixup the temporary rpi assignments.
3033 **/
3034void
3035lpfc_sli4_node_prep(struct lpfc_hba *phba)
3036{
3037 struct lpfc_nodelist *ndlp, *next_ndlp;
3038 struct lpfc_vport **vports;
James Smart9d3d3402017-04-21 16:05:00 -07003039 int i, rpi;
3040 unsigned long flags;
James Smart6b5151f2012-01-18 16:24:06 -05003041
3042 if (phba->sli_rev != LPFC_SLI_REV4)
3043 return;
3044
3045 vports = lpfc_create_vport_work_array(phba);
James Smart9d3d3402017-04-21 16:05:00 -07003046 if (vports == NULL)
3047 return;
James Smart6b5151f2012-01-18 16:24:06 -05003048
James Smart9d3d3402017-04-21 16:05:00 -07003049 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
3050 if (vports[i]->load_flag & FC_UNLOADING)
3051 continue;
3052
3053 list_for_each_entry_safe(ndlp, next_ndlp,
3054 &vports[i]->fc_nodes,
3055 nlp_listp) {
3056 if (!NLP_CHK_NODE_ACT(ndlp))
3057 continue;
3058 rpi = lpfc_sli4_alloc_rpi(phba);
3059 if (rpi == LPFC_RPI_ALLOC_ERROR) {
3060 spin_lock_irqsave(&phba->ndlp_lock, flags);
3061 NLP_CLR_NODE_ACT(ndlp);
3062 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
3063 continue;
James Smart6b5151f2012-01-18 16:24:06 -05003064 }
James Smart9d3d3402017-04-21 16:05:00 -07003065 ndlp->nlp_rpi = rpi;
3066 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
3067 "0009 rpi:%x DID:%x "
3068 "flg:%x map:%x %p\n", ndlp->nlp_rpi,
3069 ndlp->nlp_DID, ndlp->nlp_flag,
3070 ndlp->nlp_usg_map, ndlp);
James Smart6b5151f2012-01-18 16:24:06 -05003071 }
3072 }
3073 lpfc_destroy_vport_work_array(phba, vports);
3074}
3075
3076/**
James Smartc4908502019-01-28 11:14:28 -08003077 * lpfc_create_expedite_pool - create expedite pool
3078 * @phba: pointer to lpfc hba data structure.
3079 *
3080 * This routine moves a batch of XRIs from lpfc_io_buf_list_put of HWQ 0
3081 * to expedite pool. Mark them as expedite.
3082 **/
Bart Van Assche3999df72019-03-28 11:06:16 -07003083static void lpfc_create_expedite_pool(struct lpfc_hba *phba)
James Smartc4908502019-01-28 11:14:28 -08003084{
3085 struct lpfc_sli4_hdw_queue *qp;
3086 struct lpfc_io_buf *lpfc_ncmd;
3087 struct lpfc_io_buf *lpfc_ncmd_next;
3088 struct lpfc_epd_pool *epd_pool;
3089 unsigned long iflag;
3090
3091 epd_pool = &phba->epd_pool;
3092 qp = &phba->sli4_hba.hdwq[0];
3093
3094 spin_lock_init(&epd_pool->lock);
3095 spin_lock_irqsave(&qp->io_buf_list_put_lock, iflag);
3096 spin_lock(&epd_pool->lock);
3097 INIT_LIST_HEAD(&epd_pool->list);
3098 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
3099 &qp->lpfc_io_buf_list_put, list) {
3100 list_move_tail(&lpfc_ncmd->list, &epd_pool->list);
3101 lpfc_ncmd->expedite = true;
3102 qp->put_io_bufs--;
3103 epd_pool->count++;
3104 if (epd_pool->count >= XRI_BATCH)
3105 break;
3106 }
3107 spin_unlock(&epd_pool->lock);
3108 spin_unlock_irqrestore(&qp->io_buf_list_put_lock, iflag);
3109}
3110
3111/**
3112 * lpfc_destroy_expedite_pool - destroy expedite pool
3113 * @phba: pointer to lpfc hba data structure.
3114 *
3115 * This routine returns XRIs from expedite pool to lpfc_io_buf_list_put
3116 * of HWQ 0. Clear the mark.
3117 **/
Bart Van Assche3999df72019-03-28 11:06:16 -07003118static void lpfc_destroy_expedite_pool(struct lpfc_hba *phba)
James Smartc4908502019-01-28 11:14:28 -08003119{
3120 struct lpfc_sli4_hdw_queue *qp;
3121 struct lpfc_io_buf *lpfc_ncmd;
3122 struct lpfc_io_buf *lpfc_ncmd_next;
3123 struct lpfc_epd_pool *epd_pool;
3124 unsigned long iflag;
3125
3126 epd_pool = &phba->epd_pool;
3127 qp = &phba->sli4_hba.hdwq[0];
3128
3129 spin_lock_irqsave(&qp->io_buf_list_put_lock, iflag);
3130 spin_lock(&epd_pool->lock);
3131 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
3132 &epd_pool->list, list) {
3133 list_move_tail(&lpfc_ncmd->list,
3134 &qp->lpfc_io_buf_list_put);
3135 lpfc_ncmd->flags = false;
3136 qp->put_io_bufs++;
3137 epd_pool->count--;
3138 }
3139 spin_unlock(&epd_pool->lock);
3140 spin_unlock_irqrestore(&qp->io_buf_list_put_lock, iflag);
3141}
3142
3143/**
3144 * lpfc_create_multixri_pools - create multi-XRI pools
3145 * @phba: pointer to lpfc hba data structure.
3146 *
3147 * This routine initialize public, private per HWQ. Then, move XRIs from
3148 * lpfc_io_buf_list_put to public pool. High and low watermark are also
3149 * Initialized.
3150 **/
3151void lpfc_create_multixri_pools(struct lpfc_hba *phba)
3152{
3153 u32 i, j;
3154 u32 hwq_count;
3155 u32 count_per_hwq;
3156 struct lpfc_io_buf *lpfc_ncmd;
3157 struct lpfc_io_buf *lpfc_ncmd_next;
3158 unsigned long iflag;
3159 struct lpfc_sli4_hdw_queue *qp;
3160 struct lpfc_multixri_pool *multixri_pool;
3161 struct lpfc_pbl_pool *pbl_pool;
3162 struct lpfc_pvt_pool *pvt_pool;
3163
3164 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3165 "1234 num_hdw_queue=%d num_present_cpu=%d common_xri_cnt=%d\n",
3166 phba->cfg_hdw_queue, phba->sli4_hba.num_present_cpu,
3167 phba->sli4_hba.io_xri_cnt);
3168
3169 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
3170 lpfc_create_expedite_pool(phba);
3171
3172 hwq_count = phba->cfg_hdw_queue;
3173 count_per_hwq = phba->sli4_hba.io_xri_cnt / hwq_count;
3174
3175 for (i = 0; i < hwq_count; i++) {
3176 multixri_pool = kzalloc(sizeof(*multixri_pool), GFP_KERNEL);
3177
3178 if (!multixri_pool) {
3179 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3180 "1238 Failed to allocate memory for "
3181 "multixri_pool\n");
3182
3183 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
3184 lpfc_destroy_expedite_pool(phba);
3185
3186 j = 0;
3187 while (j < i) {
3188 qp = &phba->sli4_hba.hdwq[j];
3189 kfree(qp->p_multixri_pool);
3190 j++;
3191 }
3192 phba->cfg_xri_rebalancing = 0;
3193 return;
3194 }
3195
3196 qp = &phba->sli4_hba.hdwq[i];
3197 qp->p_multixri_pool = multixri_pool;
3198
3199 multixri_pool->xri_limit = count_per_hwq;
3200 multixri_pool->rrb_next_hwqid = i;
3201
3202 /* Deal with public free xri pool */
3203 pbl_pool = &multixri_pool->pbl_pool;
3204 spin_lock_init(&pbl_pool->lock);
3205 spin_lock_irqsave(&qp->io_buf_list_put_lock, iflag);
3206 spin_lock(&pbl_pool->lock);
3207 INIT_LIST_HEAD(&pbl_pool->list);
3208 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
3209 &qp->lpfc_io_buf_list_put, list) {
3210 list_move_tail(&lpfc_ncmd->list, &pbl_pool->list);
3211 qp->put_io_bufs--;
3212 pbl_pool->count++;
3213 }
3214 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3215 "1235 Moved %d buffers from PUT list over to pbl_pool[%d]\n",
3216 pbl_pool->count, i);
3217 spin_unlock(&pbl_pool->lock);
3218 spin_unlock_irqrestore(&qp->io_buf_list_put_lock, iflag);
3219
3220 /* Deal with private free xri pool */
3221 pvt_pool = &multixri_pool->pvt_pool;
3222 pvt_pool->high_watermark = multixri_pool->xri_limit / 2;
3223 pvt_pool->low_watermark = XRI_BATCH;
3224 spin_lock_init(&pvt_pool->lock);
3225 spin_lock_irqsave(&pvt_pool->lock, iflag);
3226 INIT_LIST_HEAD(&pvt_pool->list);
3227 pvt_pool->count = 0;
3228 spin_unlock_irqrestore(&pvt_pool->lock, iflag);
3229 }
3230}
3231
3232/**
3233 * lpfc_destroy_multixri_pools - destroy multi-XRI pools
3234 * @phba: pointer to lpfc hba data structure.
3235 *
3236 * This routine returns XRIs from public/private to lpfc_io_buf_list_put.
3237 **/
Bart Van Assche3999df72019-03-28 11:06:16 -07003238static void lpfc_destroy_multixri_pools(struct lpfc_hba *phba)
James Smartc4908502019-01-28 11:14:28 -08003239{
3240 u32 i;
3241 u32 hwq_count;
3242 struct lpfc_io_buf *lpfc_ncmd;
3243 struct lpfc_io_buf *lpfc_ncmd_next;
3244 unsigned long iflag;
3245 struct lpfc_sli4_hdw_queue *qp;
3246 struct lpfc_multixri_pool *multixri_pool;
3247 struct lpfc_pbl_pool *pbl_pool;
3248 struct lpfc_pvt_pool *pvt_pool;
3249
3250 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
3251 lpfc_destroy_expedite_pool(phba);
3252
James Smartc66a9192019-03-12 16:30:19 -07003253 if (!(phba->pport->load_flag & FC_UNLOADING)) {
3254 lpfc_sli_flush_fcp_rings(phba);
3255
3256 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
3257 lpfc_sli_flush_nvme_rings(phba);
3258 }
3259
James Smartc4908502019-01-28 11:14:28 -08003260 hwq_count = phba->cfg_hdw_queue;
3261
3262 for (i = 0; i < hwq_count; i++) {
3263 qp = &phba->sli4_hba.hdwq[i];
3264 multixri_pool = qp->p_multixri_pool;
3265 if (!multixri_pool)
3266 continue;
3267
3268 qp->p_multixri_pool = NULL;
3269
3270 spin_lock_irqsave(&qp->io_buf_list_put_lock, iflag);
3271
3272 /* Deal with public free xri pool */
3273 pbl_pool = &multixri_pool->pbl_pool;
3274 spin_lock(&pbl_pool->lock);
3275
3276 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3277 "1236 Moving %d buffers from pbl_pool[%d] TO PUT list\n",
3278 pbl_pool->count, i);
3279
3280 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
3281 &pbl_pool->list, list) {
3282 list_move_tail(&lpfc_ncmd->list,
3283 &qp->lpfc_io_buf_list_put);
3284 qp->put_io_bufs++;
3285 pbl_pool->count--;
3286 }
3287
3288 INIT_LIST_HEAD(&pbl_pool->list);
3289 pbl_pool->count = 0;
3290
3291 spin_unlock(&pbl_pool->lock);
3292
3293 /* Deal with private free xri pool */
3294 pvt_pool = &multixri_pool->pvt_pool;
3295 spin_lock(&pvt_pool->lock);
3296
3297 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3298 "1237 Moving %d buffers from pvt_pool[%d] TO PUT list\n",
3299 pvt_pool->count, i);
3300
3301 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
3302 &pvt_pool->list, list) {
3303 list_move_tail(&lpfc_ncmd->list,
3304 &qp->lpfc_io_buf_list_put);
3305 qp->put_io_bufs++;
3306 pvt_pool->count--;
3307 }
3308
3309 INIT_LIST_HEAD(&pvt_pool->list);
3310 pvt_pool->count = 0;
3311
3312 spin_unlock(&pvt_pool->lock);
3313 spin_unlock_irqrestore(&qp->io_buf_list_put_lock, iflag);
3314
3315 kfree(multixri_pool);
3316 }
3317}
3318
3319/**
James Smart3621a712009-04-06 18:47:14 -04003320 * lpfc_online - Initialize and bring a HBA online
James Smarte59058c2008-08-24 21:49:00 -04003321 * @phba: pointer to lpfc hba data structure.
3322 *
3323 * This routine initializes the HBA and brings a HBA online. During this
3324 * process, the management interface is blocked to prevent user space access
3325 * to the HBA interfering with the driver initialization.
3326 *
3327 * Return codes
3328 * 0 - successful
3329 * 1 - failed
3330 **/
dea31012005-04-17 16:05:31 -05003331int
James Smart2e0fef82007-06-17 19:56:36 -05003332lpfc_online(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05003333{
Julia Lawall372bd282008-12-16 16:15:08 +01003334 struct lpfc_vport *vport;
James Smart549e55c2007-08-02 11:09:51 -04003335 struct lpfc_vport **vports;
Dick Kennedya145fda2017-08-23 16:55:44 -07003336 int i, error = 0;
James Smart16a3a202013-04-17 20:14:38 -04003337 bool vpis_cleared = false;
James Smart2e0fef82007-06-17 19:56:36 -05003338
dea31012005-04-17 16:05:31 -05003339 if (!phba)
3340 return 0;
Julia Lawall372bd282008-12-16 16:15:08 +01003341 vport = phba->pport;
dea31012005-04-17 16:05:31 -05003342
James Smart2e0fef82007-06-17 19:56:36 -05003343 if (!(vport->fc_flag & FC_OFFLINE_MODE))
dea31012005-04-17 16:05:31 -05003344 return 0;
3345
James Smarted957682007-06-17 19:56:37 -05003346 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003347 "0458 Bring Adapter online\n");
dea31012005-04-17 16:05:31 -05003348
James Smart618a5232012-06-12 13:54:36 -04003349 lpfc_block_mgmt_io(phba, LPFC_MBX_WAIT);
dea31012005-04-17 16:05:31 -05003350
James Smartda0436e2009-05-22 14:51:39 -04003351 if (phba->sli_rev == LPFC_SLI_REV4) {
3352 if (lpfc_sli4_hba_setup(phba)) { /* Initialize SLI4 HBA */
3353 lpfc_unblock_mgmt_io(phba);
3354 return 1;
3355 }
James Smart16a3a202013-04-17 20:14:38 -04003356 spin_lock_irq(&phba->hbalock);
3357 if (!phba->sli4_hba.max_cfg_param.vpi_used)
3358 vpis_cleared = true;
3359 spin_unlock_irq(&phba->hbalock);
Dick Kennedya145fda2017-08-23 16:55:44 -07003360
3361 /* Reestablish the local initiator port.
3362 * The offline process destroyed the previous lport.
3363 */
3364 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME &&
3365 !phba->nvmet_support) {
3366 error = lpfc_nvme_create_localport(phba->pport);
3367 if (error)
3368 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3369 "6132 NVME restore reg failed "
3370 "on nvmei error x%x\n", error);
3371 }
James Smartda0436e2009-05-22 14:51:39 -04003372 } else {
James Smart895427b2017-02-12 13:52:30 -08003373 lpfc_sli_queue_init(phba);
James Smartda0436e2009-05-22 14:51:39 -04003374 if (lpfc_sli_hba_setup(phba)) { /* Initialize SLI2/SLI3 HBA */
3375 lpfc_unblock_mgmt_io(phba);
3376 return 1;
3377 }
James Smart46fa3112007-04-25 09:51:45 -04003378 }
dea31012005-04-17 16:05:31 -05003379
James Smart549e55c2007-08-02 11:09:51 -04003380 vports = lpfc_create_vport_work_array(phba);
Arnd Bergmannaeb66412016-03-14 15:29:44 +01003381 if (vports != NULL) {
James Smartda0436e2009-05-22 14:51:39 -04003382 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -04003383 struct Scsi_Host *shost;
3384 shost = lpfc_shost_from_vport(vports[i]);
3385 spin_lock_irq(shost->host_lock);
3386 vports[i]->fc_flag &= ~FC_OFFLINE_MODE;
3387 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
3388 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart16a3a202013-04-17 20:14:38 -04003389 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart1c6834a2009-07-19 10:01:26 -04003390 vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
James Smart16a3a202013-04-17 20:14:38 -04003391 if ((vpis_cleared) &&
3392 (vports[i]->port_type !=
3393 LPFC_PHYSICAL_PORT))
3394 vports[i]->vpi = 0;
3395 }
James Smart549e55c2007-08-02 11:09:51 -04003396 spin_unlock_irq(shost->host_lock);
3397 }
Arnd Bergmannaeb66412016-03-14 15:29:44 +01003398 }
3399 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05003400
James Smartc4908502019-01-28 11:14:28 -08003401 if (phba->cfg_xri_rebalancing)
3402 lpfc_create_multixri_pools(phba);
3403
James Smart46fa3112007-04-25 09:51:45 -04003404 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05003405 return 0;
3406}
3407
James Smarte59058c2008-08-24 21:49:00 -04003408/**
James Smart3621a712009-04-06 18:47:14 -04003409 * lpfc_unblock_mgmt_io - Mark a HBA's management interface to be not blocked
James Smarte59058c2008-08-24 21:49:00 -04003410 * @phba: pointer to lpfc hba data structure.
3411 *
3412 * This routine marks a HBA's management interface as not blocked. Once the
3413 * HBA's management interface is marked as not blocked, all the user space
3414 * access to the HBA, whether they are from sysfs interface or libdfc
3415 * interface will be allowed. The HBA is set to block the management interface
3416 * when the driver prepares the HBA interface for online or offline and then
3417 * set to unblock the management interface afterwards.
3418 **/
James Smart46fa3112007-04-25 09:51:45 -04003419void
James Smart46fa3112007-04-25 09:51:45 -04003420lpfc_unblock_mgmt_io(struct lpfc_hba * phba)
3421{
3422 unsigned long iflag;
3423
James Smart2e0fef82007-06-17 19:56:36 -05003424 spin_lock_irqsave(&phba->hbalock, iflag);
3425 phba->sli.sli_flag &= ~LPFC_BLOCK_MGMT_IO;
3426 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart46fa3112007-04-25 09:51:45 -04003427}
3428
James Smarte59058c2008-08-24 21:49:00 -04003429/**
James Smart3621a712009-04-06 18:47:14 -04003430 * lpfc_offline_prep - Prepare a HBA to be brought offline
James Smarte59058c2008-08-24 21:49:00 -04003431 * @phba: pointer to lpfc hba data structure.
3432 *
3433 * This routine is invoked to prepare a HBA to be brought offline. It performs
3434 * unregistration login to all the nodes on all vports and flushes the mailbox
3435 * queue to make it ready to be brought offline.
3436 **/
James Smart46fa3112007-04-25 09:51:45 -04003437void
James Smart618a5232012-06-12 13:54:36 -04003438lpfc_offline_prep(struct lpfc_hba *phba, int mbx_action)
James Smart46fa3112007-04-25 09:51:45 -04003439{
James Smart2e0fef82007-06-17 19:56:36 -05003440 struct lpfc_vport *vport = phba->pport;
James Smart46fa3112007-04-25 09:51:45 -04003441 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart87af33f2007-10-27 13:37:43 -04003442 struct lpfc_vport **vports;
James Smart72100cc2010-02-12 14:43:01 -05003443 struct Scsi_Host *shost;
James Smart87af33f2007-10-27 13:37:43 -04003444 int i;
dea31012005-04-17 16:05:31 -05003445
James Smart2e0fef82007-06-17 19:56:36 -05003446 if (vport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04003447 return;
dea31012005-04-17 16:05:31 -05003448
James Smart618a5232012-06-12 13:54:36 -04003449 lpfc_block_mgmt_io(phba, mbx_action);
dea31012005-04-17 16:05:31 -05003450
3451 lpfc_linkdown(phba);
3452
James Smart87af33f2007-10-27 13:37:43 -04003453 /* Issue an unreg_login to all nodes on all vports */
3454 vports = lpfc_create_vport_work_array(phba);
3455 if (vports != NULL) {
James Smartda0436e2009-05-22 14:51:39 -04003456 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smarta8adb832007-10-27 13:37:53 -04003457 if (vports[i]->load_flag & FC_UNLOADING)
3458 continue;
James Smart72100cc2010-02-12 14:43:01 -05003459 shost = lpfc_shost_from_vport(vports[i]);
3460 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05003461 vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05003462 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
3463 vports[i]->fc_flag &= ~FC_VFI_REGISTERED;
James Smart72100cc2010-02-12 14:43:01 -05003464 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05003465
James Smart87af33f2007-10-27 13:37:43 -04003466 shost = lpfc_shost_from_vport(vports[i]);
3467 list_for_each_entry_safe(ndlp, next_ndlp,
3468 &vports[i]->fc_nodes,
3469 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05003470 if (!NLP_CHK_NODE_ACT(ndlp))
3471 continue;
James Smart87af33f2007-10-27 13:37:43 -04003472 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
3473 continue;
3474 if (ndlp->nlp_type & NLP_FABRIC) {
3475 lpfc_disc_state_machine(vports[i], ndlp,
3476 NULL, NLP_EVT_DEVICE_RECOVERY);
3477 lpfc_disc_state_machine(vports[i], ndlp,
3478 NULL, NLP_EVT_DEVICE_RM);
3479 }
3480 spin_lock_irq(shost->host_lock);
3481 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
James Smart401ee0c2012-03-01 22:35:34 -05003482 spin_unlock_irq(shost->host_lock);
James Smart6b5151f2012-01-18 16:24:06 -05003483 /*
3484 * Whenever an SLI4 port goes offline, free the
James Smart401ee0c2012-03-01 22:35:34 -05003485 * RPI. Get a new RPI when the adapter port
3486 * comes back online.
James Smart6b5151f2012-01-18 16:24:06 -05003487 */
James Smartbe6bb942015-04-07 15:07:22 -04003488 if (phba->sli_rev == LPFC_SLI_REV4) {
3489 lpfc_printf_vlog(ndlp->vport,
3490 KERN_INFO, LOG_NODE,
3491 "0011 lpfc_offline: "
3492 "ndlp:x%p did %x "
3493 "usgmap:x%x rpi:%x\n",
3494 ndlp, ndlp->nlp_DID,
3495 ndlp->nlp_usg_map,
3496 ndlp->nlp_rpi);
3497
James Smart6b5151f2012-01-18 16:24:06 -05003498 lpfc_sli4_free_rpi(phba, ndlp->nlp_rpi);
James Smartbe6bb942015-04-07 15:07:22 -04003499 }
James Smart87af33f2007-10-27 13:37:43 -04003500 lpfc_unreg_rpi(vports[i], ndlp);
3501 }
3502 }
3503 }
James Smart09372822008-01-11 01:52:54 -05003504 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05003505
James Smart618a5232012-06-12 13:54:36 -04003506 lpfc_sli_mbox_sys_shutdown(phba, mbx_action);
Dick Kennedyf485c182017-09-29 17:34:34 -07003507
3508 if (phba->wq)
3509 flush_workqueue(phba->wq);
James Smart46fa3112007-04-25 09:51:45 -04003510}
3511
James Smarte59058c2008-08-24 21:49:00 -04003512/**
James Smart3621a712009-04-06 18:47:14 -04003513 * lpfc_offline - Bring a HBA offline
James Smarte59058c2008-08-24 21:49:00 -04003514 * @phba: pointer to lpfc hba data structure.
3515 *
3516 * This routine actually brings a HBA offline. It stops all the timers
3517 * associated with the HBA, brings down the SLI layer, and eventually
3518 * marks the HBA as in offline state for the upper layer protocol.
3519 **/
James Smart46fa3112007-04-25 09:51:45 -04003520void
James Smart2e0fef82007-06-17 19:56:36 -05003521lpfc_offline(struct lpfc_hba *phba)
James Smart46fa3112007-04-25 09:51:45 -04003522{
James Smart549e55c2007-08-02 11:09:51 -04003523 struct Scsi_Host *shost;
3524 struct lpfc_vport **vports;
3525 int i;
James Smart46fa3112007-04-25 09:51:45 -04003526
James Smart549e55c2007-08-02 11:09:51 -04003527 if (phba->pport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04003528 return;
James Smart688a8862006-07-06 15:49:56 -04003529
James Smartda0436e2009-05-22 14:51:39 -04003530 /* stop port and all timers associated with this hba */
3531 lpfc_stop_port(phba);
Dick Kennedy4b40d022017-08-23 16:55:38 -07003532
3533 /* Tear down the local and target port registrations. The
3534 * nvme transports need to cleanup.
3535 */
3536 lpfc_nvmet_destroy_targetport(phba);
3537 lpfc_nvme_destroy_localport(phba->pport);
3538
James Smart51ef4c22007-08-02 11:10:31 -04003539 vports = lpfc_create_vport_work_array(phba);
3540 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04003541 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
James Smart51ef4c22007-08-02 11:10:31 -04003542 lpfc_stop_vport_timers(vports[i]);
James Smart09372822008-01-11 01:52:54 -05003543 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -05003544 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003545 "0460 Bring Adapter offline\n");
dea31012005-04-17 16:05:31 -05003546 /* Bring down the SLI Layer and cleanup. The HBA is offline
3547 now. */
3548 lpfc_sli_hba_down(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05003549 spin_lock_irq(&phba->hbalock);
James Smart7054a602007-04-25 09:52:34 -04003550 phba->work_ha = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05003551 spin_unlock_irq(&phba->hbalock);
James Smart549e55c2007-08-02 11:09:51 -04003552 vports = lpfc_create_vport_work_array(phba);
3553 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04003554 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -04003555 shost = lpfc_shost_from_vport(vports[i]);
James Smart549e55c2007-08-02 11:09:51 -04003556 spin_lock_irq(shost->host_lock);
3557 vports[i]->work_port_events = 0;
3558 vports[i]->fc_flag |= FC_OFFLINE_MODE;
3559 spin_unlock_irq(shost->host_lock);
3560 }
James Smart09372822008-01-11 01:52:54 -05003561 lpfc_destroy_vport_work_array(phba, vports);
James Smartc4908502019-01-28 11:14:28 -08003562
3563 if (phba->cfg_xri_rebalancing)
3564 lpfc_destroy_multixri_pools(phba);
dea31012005-04-17 16:05:31 -05003565}
3566
James Smarte59058c2008-08-24 21:49:00 -04003567/**
James Smart3621a712009-04-06 18:47:14 -04003568 * lpfc_scsi_free - Free all the SCSI buffers and IOCBs from driver lists
James Smarte59058c2008-08-24 21:49:00 -04003569 * @phba: pointer to lpfc hba data structure.
3570 *
3571 * This routine is to free all the SCSI buffers and IOCBs from the driver
3572 * list back to kernel. It is called from lpfc_pci_remove_one to free
3573 * the internal resources before the device is removed from the system.
James Smarte59058c2008-08-24 21:49:00 -04003574 **/
James Smart8a9d2e82012-05-09 21:16:12 -04003575static void
James Smart2e0fef82007-06-17 19:56:36 -05003576lpfc_scsi_free(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05003577{
James Smartc4908502019-01-28 11:14:28 -08003578 struct lpfc_io_buf *sb, *sb_next;
dea31012005-04-17 16:05:31 -05003579
James Smart895427b2017-02-12 13:52:30 -08003580 if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP))
3581 return;
3582
James Smart2e0fef82007-06-17 19:56:36 -05003583 spin_lock_irq(&phba->hbalock);
James Smarta40fc5f2013-04-17 20:17:40 -04003584
dea31012005-04-17 16:05:31 -05003585 /* Release all the lpfc_scsi_bufs maintained by this host. */
James Smarta40fc5f2013-04-17 20:17:40 -04003586
3587 spin_lock(&phba->scsi_buf_list_put_lock);
3588 list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list_put,
3589 list) {
dea31012005-04-17 16:05:31 -05003590 list_del(&sb->list);
Romain Perier771db5c2017-07-06 10:13:05 +02003591 dma_pool_free(phba->lpfc_sg_dma_buf_pool, sb->data,
James Smart92d7f7b2007-06-17 19:56:38 -05003592 sb->dma_handle);
dea31012005-04-17 16:05:31 -05003593 kfree(sb);
3594 phba->total_scsi_bufs--;
3595 }
James Smarta40fc5f2013-04-17 20:17:40 -04003596 spin_unlock(&phba->scsi_buf_list_put_lock);
3597
3598 spin_lock(&phba->scsi_buf_list_get_lock);
3599 list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list_get,
3600 list) {
3601 list_del(&sb->list);
Romain Perier771db5c2017-07-06 10:13:05 +02003602 dma_pool_free(phba->lpfc_sg_dma_buf_pool, sb->data,
James Smarta40fc5f2013-04-17 20:17:40 -04003603 sb->dma_handle);
3604 kfree(sb);
3605 phba->total_scsi_bufs--;
3606 }
3607 spin_unlock(&phba->scsi_buf_list_get_lock);
James Smart2e0fef82007-06-17 19:56:36 -05003608 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04003609}
James Smart0794d602019-01-28 11:14:19 -08003610
James Smart8a9d2e82012-05-09 21:16:12 -04003611/**
James Smart5e5b5112019-01-28 11:14:22 -08003612 * lpfc_io_free - Free all the IO buffers and IOCBs from driver lists
James Smart895427b2017-02-12 13:52:30 -08003613 * @phba: pointer to lpfc hba data structure.
3614 *
James Smart0794d602019-01-28 11:14:19 -08003615 * This routine is to free all the IO buffers and IOCBs from the driver
James Smart895427b2017-02-12 13:52:30 -08003616 * list back to kernel. It is called from lpfc_pci_remove_one to free
3617 * the internal resources before the device is removed from the system.
3618 **/
James Smartc4908502019-01-28 11:14:28 -08003619void
James Smart5e5b5112019-01-28 11:14:22 -08003620lpfc_io_free(struct lpfc_hba *phba)
James Smart895427b2017-02-12 13:52:30 -08003621{
James Smartc4908502019-01-28 11:14:28 -08003622 struct lpfc_io_buf *lpfc_ncmd, *lpfc_ncmd_next;
James Smart5e5b5112019-01-28 11:14:22 -08003623 struct lpfc_sli4_hdw_queue *qp;
3624 int idx;
James Smart895427b2017-02-12 13:52:30 -08003625
James Smart5e5b5112019-01-28 11:14:22 -08003626 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
3627 qp = &phba->sli4_hba.hdwq[idx];
3628 /* Release all the lpfc_nvme_bufs maintained by this host. */
3629 spin_lock(&qp->io_buf_list_put_lock);
3630 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
3631 &qp->lpfc_io_buf_list_put,
3632 list) {
3633 list_del(&lpfc_ncmd->list);
3634 qp->put_io_bufs--;
3635 dma_pool_free(phba->lpfc_sg_dma_buf_pool,
3636 lpfc_ncmd->data, lpfc_ncmd->dma_handle);
3637 kfree(lpfc_ncmd);
3638 qp->total_io_bufs--;
3639 }
3640 spin_unlock(&qp->io_buf_list_put_lock);
James Smart895427b2017-02-12 13:52:30 -08003641
James Smart5e5b5112019-01-28 11:14:22 -08003642 spin_lock(&qp->io_buf_list_get_lock);
3643 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
3644 &qp->lpfc_io_buf_list_get,
3645 list) {
3646 list_del(&lpfc_ncmd->list);
3647 qp->get_io_bufs--;
3648 dma_pool_free(phba->lpfc_sg_dma_buf_pool,
3649 lpfc_ncmd->data, lpfc_ncmd->dma_handle);
3650 kfree(lpfc_ncmd);
3651 qp->total_io_bufs--;
3652 }
3653 spin_unlock(&qp->io_buf_list_get_lock);
James Smart895427b2017-02-12 13:52:30 -08003654 }
James Smart895427b2017-02-12 13:52:30 -08003655}
James Smart0794d602019-01-28 11:14:19 -08003656
James Smart895427b2017-02-12 13:52:30 -08003657/**
3658 * lpfc_sli4_els_sgl_update - update ELS xri-sgl sizing and mapping
James Smart8a9d2e82012-05-09 21:16:12 -04003659 * @phba: pointer to lpfc hba data structure.
3660 *
3661 * This routine first calculates the sizes of the current els and allocated
3662 * scsi sgl lists, and then goes through all sgls to updates the physical
3663 * XRIs assigned due to port function reset. During port initialization, the
3664 * current els and allocated scsi sgl lists are 0s.
3665 *
3666 * Return codes
3667 * 0 - successful (for now, it always returns 0)
3668 **/
3669int
James Smart895427b2017-02-12 13:52:30 -08003670lpfc_sli4_els_sgl_update(struct lpfc_hba *phba)
James Smart8a9d2e82012-05-09 21:16:12 -04003671{
3672 struct lpfc_sglq *sglq_entry = NULL, *sglq_entry_next = NULL;
James Smart895427b2017-02-12 13:52:30 -08003673 uint16_t i, lxri, xri_cnt, els_xri_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04003674 LIST_HEAD(els_sgl_list);
James Smart8a9d2e82012-05-09 21:16:12 -04003675 int rc;
3676
3677 /*
3678 * update on pci function's els xri-sgl list
3679 */
3680 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
James Smart895427b2017-02-12 13:52:30 -08003681
James Smart8a9d2e82012-05-09 21:16:12 -04003682 if (els_xri_cnt > phba->sli4_hba.els_xri_cnt) {
3683 /* els xri-sgl expanded */
3684 xri_cnt = els_xri_cnt - phba->sli4_hba.els_xri_cnt;
3685 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3686 "3157 ELS xri-sgl count increased from "
3687 "%d to %d\n", phba->sli4_hba.els_xri_cnt,
3688 els_xri_cnt);
3689 /* allocate the additional els sgls */
3690 for (i = 0; i < xri_cnt; i++) {
3691 sglq_entry = kzalloc(sizeof(struct lpfc_sglq),
3692 GFP_KERNEL);
3693 if (sglq_entry == NULL) {
3694 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3695 "2562 Failure to allocate an "
3696 "ELS sgl entry:%d\n", i);
3697 rc = -ENOMEM;
3698 goto out_free_mem;
3699 }
3700 sglq_entry->buff_type = GEN_BUFF_TYPE;
3701 sglq_entry->virt = lpfc_mbuf_alloc(phba, 0,
3702 &sglq_entry->phys);
3703 if (sglq_entry->virt == NULL) {
3704 kfree(sglq_entry);
3705 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3706 "2563 Failure to allocate an "
3707 "ELS mbuf:%d\n", i);
3708 rc = -ENOMEM;
3709 goto out_free_mem;
3710 }
3711 sglq_entry->sgl = sglq_entry->virt;
3712 memset(sglq_entry->sgl, 0, LPFC_BPL_SIZE);
3713 sglq_entry->state = SGL_FREED;
3714 list_add_tail(&sglq_entry->list, &els_sgl_list);
3715 }
James Smart38c20672013-03-01 16:37:44 -05003716 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -08003717 spin_lock(&phba->sli4_hba.sgl_list_lock);
3718 list_splice_init(&els_sgl_list,
3719 &phba->sli4_hba.lpfc_els_sgl_list);
3720 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smart38c20672013-03-01 16:37:44 -05003721 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04003722 } else if (els_xri_cnt < phba->sli4_hba.els_xri_cnt) {
3723 /* els xri-sgl shrinked */
3724 xri_cnt = phba->sli4_hba.els_xri_cnt - els_xri_cnt;
3725 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3726 "3158 ELS xri-sgl count decreased from "
3727 "%d to %d\n", phba->sli4_hba.els_xri_cnt,
3728 els_xri_cnt);
3729 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -08003730 spin_lock(&phba->sli4_hba.sgl_list_lock);
3731 list_splice_init(&phba->sli4_hba.lpfc_els_sgl_list,
3732 &els_sgl_list);
James Smart8a9d2e82012-05-09 21:16:12 -04003733 /* release extra els sgls from list */
3734 for (i = 0; i < xri_cnt; i++) {
3735 list_remove_head(&els_sgl_list,
3736 sglq_entry, struct lpfc_sglq, list);
3737 if (sglq_entry) {
James Smart895427b2017-02-12 13:52:30 -08003738 __lpfc_mbuf_free(phba, sglq_entry->virt,
3739 sglq_entry->phys);
James Smart8a9d2e82012-05-09 21:16:12 -04003740 kfree(sglq_entry);
3741 }
3742 }
James Smart895427b2017-02-12 13:52:30 -08003743 list_splice_init(&els_sgl_list,
3744 &phba->sli4_hba.lpfc_els_sgl_list);
3745 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04003746 spin_unlock_irq(&phba->hbalock);
3747 } else
3748 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3749 "3163 ELS xri-sgl count unchanged: %d\n",
3750 els_xri_cnt);
3751 phba->sli4_hba.els_xri_cnt = els_xri_cnt;
3752
3753 /* update xris to els sgls on the list */
3754 sglq_entry = NULL;
3755 sglq_entry_next = NULL;
3756 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
James Smart895427b2017-02-12 13:52:30 -08003757 &phba->sli4_hba.lpfc_els_sgl_list, list) {
James Smart8a9d2e82012-05-09 21:16:12 -04003758 lxri = lpfc_sli4_next_xritag(phba);
3759 if (lxri == NO_XRI) {
3760 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3761 "2400 Failed to allocate xri for "
3762 "ELS sgl\n");
3763 rc = -ENOMEM;
3764 goto out_free_mem;
3765 }
3766 sglq_entry->sli4_lxritag = lxri;
3767 sglq_entry->sli4_xritag = phba->sli4_hba.xri_ids[lxri];
3768 }
James Smart895427b2017-02-12 13:52:30 -08003769 return 0;
3770
3771out_free_mem:
3772 lpfc_free_els_sgl_list(phba);
3773 return rc;
3774}
3775
3776/**
James Smartf358dd02017-02-12 13:52:34 -08003777 * lpfc_sli4_nvmet_sgl_update - update xri-sgl sizing and mapping
3778 * @phba: pointer to lpfc hba data structure.
3779 *
3780 * This routine first calculates the sizes of the current els and allocated
3781 * scsi sgl lists, and then goes through all sgls to updates the physical
3782 * XRIs assigned due to port function reset. During port initialization, the
3783 * current els and allocated scsi sgl lists are 0s.
3784 *
3785 * Return codes
3786 * 0 - successful (for now, it always returns 0)
3787 **/
3788int
3789lpfc_sli4_nvmet_sgl_update(struct lpfc_hba *phba)
3790{
3791 struct lpfc_sglq *sglq_entry = NULL, *sglq_entry_next = NULL;
3792 uint16_t i, lxri, xri_cnt, els_xri_cnt;
James Smart6c621a22017-05-15 15:20:45 -07003793 uint16_t nvmet_xri_cnt;
James Smartf358dd02017-02-12 13:52:34 -08003794 LIST_HEAD(nvmet_sgl_list);
3795 int rc;
3796
3797 /*
3798 * update on pci function's nvmet xri-sgl list
3799 */
3800 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
James Smart61f3d4b2017-05-15 15:20:41 -07003801
James Smart6c621a22017-05-15 15:20:45 -07003802 /* For NVMET, ALL remaining XRIs are dedicated for IO processing */
3803 nvmet_xri_cnt = phba->sli4_hba.max_cfg_param.max_xri - els_xri_cnt;
James Smartf358dd02017-02-12 13:52:34 -08003804 if (nvmet_xri_cnt > phba->sli4_hba.nvmet_xri_cnt) {
3805 /* els xri-sgl expanded */
3806 xri_cnt = nvmet_xri_cnt - phba->sli4_hba.nvmet_xri_cnt;
3807 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3808 "6302 NVMET xri-sgl cnt grew from %d to %d\n",
3809 phba->sli4_hba.nvmet_xri_cnt, nvmet_xri_cnt);
3810 /* allocate the additional nvmet sgls */
3811 for (i = 0; i < xri_cnt; i++) {
3812 sglq_entry = kzalloc(sizeof(struct lpfc_sglq),
3813 GFP_KERNEL);
3814 if (sglq_entry == NULL) {
3815 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3816 "6303 Failure to allocate an "
3817 "NVMET sgl entry:%d\n", i);
3818 rc = -ENOMEM;
3819 goto out_free_mem;
3820 }
3821 sglq_entry->buff_type = NVMET_BUFF_TYPE;
3822 sglq_entry->virt = lpfc_nvmet_buf_alloc(phba, 0,
3823 &sglq_entry->phys);
3824 if (sglq_entry->virt == NULL) {
3825 kfree(sglq_entry);
3826 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3827 "6304 Failure to allocate an "
3828 "NVMET buf:%d\n", i);
3829 rc = -ENOMEM;
3830 goto out_free_mem;
3831 }
3832 sglq_entry->sgl = sglq_entry->virt;
3833 memset(sglq_entry->sgl, 0,
3834 phba->cfg_sg_dma_buf_size);
3835 sglq_entry->state = SGL_FREED;
3836 list_add_tail(&sglq_entry->list, &nvmet_sgl_list);
3837 }
3838 spin_lock_irq(&phba->hbalock);
3839 spin_lock(&phba->sli4_hba.sgl_list_lock);
3840 list_splice_init(&nvmet_sgl_list,
3841 &phba->sli4_hba.lpfc_nvmet_sgl_list);
3842 spin_unlock(&phba->sli4_hba.sgl_list_lock);
3843 spin_unlock_irq(&phba->hbalock);
3844 } else if (nvmet_xri_cnt < phba->sli4_hba.nvmet_xri_cnt) {
3845 /* nvmet xri-sgl shrunk */
3846 xri_cnt = phba->sli4_hba.nvmet_xri_cnt - nvmet_xri_cnt;
3847 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3848 "6305 NVMET xri-sgl count decreased from "
3849 "%d to %d\n", phba->sli4_hba.nvmet_xri_cnt,
3850 nvmet_xri_cnt);
3851 spin_lock_irq(&phba->hbalock);
3852 spin_lock(&phba->sli4_hba.sgl_list_lock);
3853 list_splice_init(&phba->sli4_hba.lpfc_nvmet_sgl_list,
3854 &nvmet_sgl_list);
3855 /* release extra nvmet sgls from list */
3856 for (i = 0; i < xri_cnt; i++) {
3857 list_remove_head(&nvmet_sgl_list,
3858 sglq_entry, struct lpfc_sglq, list);
3859 if (sglq_entry) {
3860 lpfc_nvmet_buf_free(phba, sglq_entry->virt,
3861 sglq_entry->phys);
3862 kfree(sglq_entry);
3863 }
3864 }
3865 list_splice_init(&nvmet_sgl_list,
3866 &phba->sli4_hba.lpfc_nvmet_sgl_list);
3867 spin_unlock(&phba->sli4_hba.sgl_list_lock);
3868 spin_unlock_irq(&phba->hbalock);
3869 } else
3870 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3871 "6306 NVMET xri-sgl count unchanged: %d\n",
3872 nvmet_xri_cnt);
3873 phba->sli4_hba.nvmet_xri_cnt = nvmet_xri_cnt;
3874
3875 /* update xris to nvmet sgls on the list */
3876 sglq_entry = NULL;
3877 sglq_entry_next = NULL;
3878 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
3879 &phba->sli4_hba.lpfc_nvmet_sgl_list, list) {
3880 lxri = lpfc_sli4_next_xritag(phba);
3881 if (lxri == NO_XRI) {
3882 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3883 "6307 Failed to allocate xri for "
3884 "NVMET sgl\n");
3885 rc = -ENOMEM;
3886 goto out_free_mem;
3887 }
3888 sglq_entry->sli4_lxritag = lxri;
3889 sglq_entry->sli4_xritag = phba->sli4_hba.xri_ids[lxri];
3890 }
3891 return 0;
3892
3893out_free_mem:
3894 lpfc_free_nvmet_sgl_list(phba);
3895 return rc;
3896}
3897
James Smart5e5b5112019-01-28 11:14:22 -08003898int
3899lpfc_io_buf_flush(struct lpfc_hba *phba, struct list_head *cbuf)
3900{
3901 LIST_HEAD(blist);
3902 struct lpfc_sli4_hdw_queue *qp;
James Smartc4908502019-01-28 11:14:28 -08003903 struct lpfc_io_buf *lpfc_cmd;
3904 struct lpfc_io_buf *iobufp, *prev_iobufp;
James Smart5e5b5112019-01-28 11:14:22 -08003905 int idx, cnt, xri, inserted;
3906
3907 cnt = 0;
3908 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
3909 qp = &phba->sli4_hba.hdwq[idx];
3910 spin_lock_irq(&qp->io_buf_list_get_lock);
3911 spin_lock(&qp->io_buf_list_put_lock);
3912
3913 /* Take everything off the get and put lists */
3914 list_splice_init(&qp->lpfc_io_buf_list_get, &blist);
3915 list_splice(&qp->lpfc_io_buf_list_put, &blist);
3916 INIT_LIST_HEAD(&qp->lpfc_io_buf_list_get);
3917 INIT_LIST_HEAD(&qp->lpfc_io_buf_list_put);
3918 cnt += qp->get_io_bufs + qp->put_io_bufs;
3919 qp->get_io_bufs = 0;
3920 qp->put_io_bufs = 0;
3921 qp->total_io_bufs = 0;
3922 spin_unlock(&qp->io_buf_list_put_lock);
3923 spin_unlock_irq(&qp->io_buf_list_get_lock);
3924 }
3925
3926 /*
3927 * Take IO buffers off blist and put on cbuf sorted by XRI.
3928 * This is because POST_SGL takes a sequential range of XRIs
3929 * to post to the firmware.
3930 */
3931 for (idx = 0; idx < cnt; idx++) {
James Smartc4908502019-01-28 11:14:28 -08003932 list_remove_head(&blist, lpfc_cmd, struct lpfc_io_buf, list);
James Smart5e5b5112019-01-28 11:14:22 -08003933 if (!lpfc_cmd)
3934 return cnt;
3935 if (idx == 0) {
3936 list_add_tail(&lpfc_cmd->list, cbuf);
3937 continue;
3938 }
3939 xri = lpfc_cmd->cur_iocbq.sli4_xritag;
3940 inserted = 0;
3941 prev_iobufp = NULL;
3942 list_for_each_entry(iobufp, cbuf, list) {
3943 if (xri < iobufp->cur_iocbq.sli4_xritag) {
3944 if (prev_iobufp)
3945 list_add(&lpfc_cmd->list,
3946 &prev_iobufp->list);
3947 else
3948 list_add(&lpfc_cmd->list, cbuf);
3949 inserted = 1;
3950 break;
3951 }
3952 prev_iobufp = iobufp;
3953 }
3954 if (!inserted)
3955 list_add_tail(&lpfc_cmd->list, cbuf);
3956 }
3957 return cnt;
3958}
3959
3960int
3961lpfc_io_buf_replenish(struct lpfc_hba *phba, struct list_head *cbuf)
3962{
3963 struct lpfc_sli4_hdw_queue *qp;
James Smartc4908502019-01-28 11:14:28 -08003964 struct lpfc_io_buf *lpfc_cmd;
James Smart5e5b5112019-01-28 11:14:22 -08003965 int idx, cnt;
3966
3967 qp = phba->sli4_hba.hdwq;
3968 cnt = 0;
3969 while (!list_empty(cbuf)) {
3970 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
3971 list_remove_head(cbuf, lpfc_cmd,
James Smartc4908502019-01-28 11:14:28 -08003972 struct lpfc_io_buf, list);
James Smart5e5b5112019-01-28 11:14:22 -08003973 if (!lpfc_cmd)
3974 return cnt;
3975 cnt++;
3976 qp = &phba->sli4_hba.hdwq[idx];
James Smart1fbf9742019-01-28 11:14:26 -08003977 lpfc_cmd->hdwq_no = idx;
3978 lpfc_cmd->hdwq = qp;
James Smart5e5b5112019-01-28 11:14:22 -08003979 lpfc_cmd->cur_iocbq.wqe_cmpl = NULL;
3980 lpfc_cmd->cur_iocbq.iocb_cmpl = NULL;
3981 spin_lock(&qp->io_buf_list_put_lock);
3982 list_add_tail(&lpfc_cmd->list,
3983 &qp->lpfc_io_buf_list_put);
3984 qp->put_io_bufs++;
3985 qp->total_io_bufs++;
3986 spin_unlock(&qp->io_buf_list_put_lock);
3987 }
3988 }
3989 return cnt;
3990}
3991
James Smartf358dd02017-02-12 13:52:34 -08003992/**
James Smart5e5b5112019-01-28 11:14:22 -08003993 * lpfc_sli4_io_sgl_update - update xri-sgl sizing and mapping
James Smart895427b2017-02-12 13:52:30 -08003994 * @phba: pointer to lpfc hba data structure.
3995 *
3996 * This routine first calculates the sizes of the current els and allocated
3997 * scsi sgl lists, and then goes through all sgls to updates the physical
3998 * XRIs assigned due to port function reset. During port initialization, the
3999 * current els and allocated scsi sgl lists are 0s.
4000 *
4001 * Return codes
4002 * 0 - successful (for now, it always returns 0)
4003 **/
4004int
James Smart5e5b5112019-01-28 11:14:22 -08004005lpfc_sli4_io_sgl_update(struct lpfc_hba *phba)
James Smart895427b2017-02-12 13:52:30 -08004006{
James Smartc4908502019-01-28 11:14:28 -08004007 struct lpfc_io_buf *lpfc_ncmd = NULL, *lpfc_ncmd_next = NULL;
James Smart0794d602019-01-28 11:14:19 -08004008 uint16_t i, lxri, els_xri_cnt;
James Smart5e5b5112019-01-28 11:14:22 -08004009 uint16_t io_xri_cnt, io_xri_max;
4010 LIST_HEAD(io_sgl_list);
James Smart0794d602019-01-28 11:14:19 -08004011 int rc, cnt;
James Smart895427b2017-02-12 13:52:30 -08004012
4013 /*
James Smart0794d602019-01-28 11:14:19 -08004014 * update on pci function's allocated nvme xri-sgl list
James Smart895427b2017-02-12 13:52:30 -08004015 */
James Smart0794d602019-01-28 11:14:19 -08004016
4017 /* maximum number of xris available for nvme buffers */
James Smart895427b2017-02-12 13:52:30 -08004018 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
James Smart5e5b5112019-01-28 11:14:22 -08004019 io_xri_max = phba->sli4_hba.max_cfg_param.max_xri - els_xri_cnt;
4020 phba->sli4_hba.io_xri_max = io_xri_max;
James Smart8a9d2e82012-05-09 21:16:12 -04004021
James Smarte8c0a772017-04-21 16:04:49 -07004022 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart0794d602019-01-28 11:14:19 -08004023 "6074 Current allocated XRI sgl count:%d, "
4024 "maximum XRI count:%d\n",
James Smart5e5b5112019-01-28 11:14:22 -08004025 phba->sli4_hba.io_xri_cnt,
4026 phba->sli4_hba.io_xri_max);
James Smarte8c0a772017-04-21 16:04:49 -07004027
James Smart5e5b5112019-01-28 11:14:22 -08004028 cnt = lpfc_io_buf_flush(phba, &io_sgl_list);
James Smart0794d602019-01-28 11:14:19 -08004029
James Smart5e5b5112019-01-28 11:14:22 -08004030 if (phba->sli4_hba.io_xri_cnt > phba->sli4_hba.io_xri_max) {
James Smart0794d602019-01-28 11:14:19 -08004031 /* max nvme xri shrunk below the allocated nvme buffers */
James Smart5e5b5112019-01-28 11:14:22 -08004032 io_xri_cnt = phba->sli4_hba.io_xri_cnt -
4033 phba->sli4_hba.io_xri_max;
James Smart0794d602019-01-28 11:14:19 -08004034 /* release the extra allocated nvme buffers */
James Smart5e5b5112019-01-28 11:14:22 -08004035 for (i = 0; i < io_xri_cnt; i++) {
4036 list_remove_head(&io_sgl_list, lpfc_ncmd,
James Smartc4908502019-01-28 11:14:28 -08004037 struct lpfc_io_buf, list);
James Smart0794d602019-01-28 11:14:19 -08004038 if (lpfc_ncmd) {
Romain Perier771db5c2017-07-06 10:13:05 +02004039 dma_pool_free(phba->lpfc_sg_dma_buf_pool,
James Smart0794d602019-01-28 11:14:19 -08004040 lpfc_ncmd->data,
4041 lpfc_ncmd->dma_handle);
4042 kfree(lpfc_ncmd);
James Smarta2fc4aef2014-09-03 12:57:55 -04004043 }
James Smart8a9d2e82012-05-09 21:16:12 -04004044 }
James Smart5e5b5112019-01-28 11:14:22 -08004045 phba->sli4_hba.io_xri_cnt -= io_xri_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04004046 }
4047
James Smart0794d602019-01-28 11:14:19 -08004048 /* update xris associated to remaining allocated nvme buffers */
4049 lpfc_ncmd = NULL;
4050 lpfc_ncmd_next = NULL;
James Smart5e5b5112019-01-28 11:14:22 -08004051 phba->sli4_hba.io_xri_cnt = cnt;
James Smart0794d602019-01-28 11:14:19 -08004052 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
James Smart5e5b5112019-01-28 11:14:22 -08004053 &io_sgl_list, list) {
James Smart8a9d2e82012-05-09 21:16:12 -04004054 lxri = lpfc_sli4_next_xritag(phba);
4055 if (lxri == NO_XRI) {
4056 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart0794d602019-01-28 11:14:19 -08004057 "6075 Failed to allocate xri for "
4058 "nvme buffer\n");
James Smart8a9d2e82012-05-09 21:16:12 -04004059 rc = -ENOMEM;
4060 goto out_free_mem;
4061 }
James Smart0794d602019-01-28 11:14:19 -08004062 lpfc_ncmd->cur_iocbq.sli4_lxritag = lxri;
4063 lpfc_ncmd->cur_iocbq.sli4_xritag = phba->sli4_hba.xri_ids[lxri];
James Smart8a9d2e82012-05-09 21:16:12 -04004064 }
James Smart5e5b5112019-01-28 11:14:22 -08004065 cnt = lpfc_io_buf_replenish(phba, &io_sgl_list);
dea31012005-04-17 16:05:31 -05004066 return 0;
James Smart8a9d2e82012-05-09 21:16:12 -04004067
4068out_free_mem:
James Smart5e5b5112019-01-28 11:14:22 -08004069 lpfc_io_free(phba);
James Smart8a9d2e82012-05-09 21:16:12 -04004070 return rc;
dea31012005-04-17 16:05:31 -05004071}
4072
James Smart0794d602019-01-28 11:14:19 -08004073/**
James Smart5e5b5112019-01-28 11:14:22 -08004074 * lpfc_new_io_buf - IO buffer allocator for HBA with SLI4 IF spec
James Smart0794d602019-01-28 11:14:19 -08004075 * @vport: The virtual port for which this call being executed.
4076 * @num_to_allocate: The requested number of buffers to allocate.
4077 *
4078 * This routine allocates nvme buffers for device with SLI-4 interface spec,
4079 * the nvme buffer contains all the necessary information needed to initiate
4080 * an I/O. After allocating up to @num_to_allocate IO buffers and put
4081 * them on a list, it post them to the port by using SGL block post.
4082 *
4083 * Return codes:
James Smart5e5b5112019-01-28 11:14:22 -08004084 * int - number of IO buffers that were allocated and posted.
James Smart0794d602019-01-28 11:14:19 -08004085 * 0 = failure, less than num_to_alloc is a partial failure.
4086 **/
4087int
James Smart5e5b5112019-01-28 11:14:22 -08004088lpfc_new_io_buf(struct lpfc_hba *phba, int num_to_alloc)
James Smart0794d602019-01-28 11:14:19 -08004089{
James Smartc4908502019-01-28 11:14:28 -08004090 struct lpfc_io_buf *lpfc_ncmd;
James Smart0794d602019-01-28 11:14:19 -08004091 struct lpfc_iocbq *pwqeq;
4092 uint16_t iotag, lxri = 0;
4093 int bcnt, num_posted;
4094 LIST_HEAD(prep_nblist);
4095 LIST_HEAD(post_nblist);
4096 LIST_HEAD(nvme_nblist);
4097
4098 /* Sanity check to ensure our sizing is right for both SCSI and NVME */
James Smartc4908502019-01-28 11:14:28 -08004099 if (sizeof(struct lpfc_io_buf) > LPFC_COMMON_IO_BUF_SZ) {
James Smart0794d602019-01-28 11:14:19 -08004100 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
Arnd Bergmannf9968612019-03-04 20:39:09 +01004101 "6426 Common buffer size %zd exceeds %d\n",
James Smartc4908502019-01-28 11:14:28 -08004102 sizeof(struct lpfc_io_buf),
4103 LPFC_COMMON_IO_BUF_SZ);
James Smart0794d602019-01-28 11:14:19 -08004104 return 0;
4105 }
4106
James Smart5e5b5112019-01-28 11:14:22 -08004107 phba->sli4_hba.io_xri_cnt = 0;
James Smart0794d602019-01-28 11:14:19 -08004108 for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
4109 lpfc_ncmd = kzalloc(LPFC_COMMON_IO_BUF_SZ, GFP_KERNEL);
4110 if (!lpfc_ncmd)
4111 break;
4112 /*
4113 * Get memory from the pci pool to map the virt space to
4114 * pci bus space for an I/O. The DMA buffer includes the
4115 * number of SGE's necessary to support the sg_tablesize.
4116 */
4117 lpfc_ncmd->data = dma_pool_alloc(phba->lpfc_sg_dma_buf_pool,
4118 GFP_KERNEL,
4119 &lpfc_ncmd->dma_handle);
4120 if (!lpfc_ncmd->data) {
4121 kfree(lpfc_ncmd);
4122 break;
4123 }
4124 memset(lpfc_ncmd->data, 0, phba->cfg_sg_dma_buf_size);
4125
4126 /*
4127 * 4K Page alignment is CRITICAL to BlockGuard, double check
4128 * to be sure.
4129 */
4130 if ((phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
4131 (((unsigned long)(lpfc_ncmd->data) &
4132 (unsigned long)(SLI4_PAGE_SIZE - 1)) != 0)) {
4133 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
4134 "3369 Memory alignment err: addr=%lx\n",
4135 (unsigned long)lpfc_ncmd->data);
4136 dma_pool_free(phba->lpfc_sg_dma_buf_pool,
4137 lpfc_ncmd->data, lpfc_ncmd->dma_handle);
4138 kfree(lpfc_ncmd);
4139 break;
4140 }
4141
4142 lxri = lpfc_sli4_next_xritag(phba);
4143 if (lxri == NO_XRI) {
4144 dma_pool_free(phba->lpfc_sg_dma_buf_pool,
4145 lpfc_ncmd->data, lpfc_ncmd->dma_handle);
4146 kfree(lpfc_ncmd);
4147 break;
4148 }
4149 pwqeq = &lpfc_ncmd->cur_iocbq;
4150
4151 /* Allocate iotag for lpfc_ncmd->cur_iocbq. */
4152 iotag = lpfc_sli_next_iotag(phba, pwqeq);
4153 if (iotag == 0) {
4154 dma_pool_free(phba->lpfc_sg_dma_buf_pool,
4155 lpfc_ncmd->data, lpfc_ncmd->dma_handle);
4156 kfree(lpfc_ncmd);
4157 lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
4158 "6121 Failed to allocate IOTAG for"
4159 " XRI:0x%x\n", lxri);
4160 lpfc_sli4_free_xri(phba, lxri);
4161 break;
4162 }
4163 pwqeq->sli4_lxritag = lxri;
4164 pwqeq->sli4_xritag = phba->sli4_hba.xri_ids[lxri];
4165 pwqeq->context1 = lpfc_ncmd;
4166
4167 /* Initialize local short-hand pointers. */
4168 lpfc_ncmd->dma_sgl = lpfc_ncmd->data;
4169 lpfc_ncmd->dma_phys_sgl = lpfc_ncmd->dma_handle;
4170 lpfc_ncmd->cur_iocbq.context1 = lpfc_ncmd;
James Smartc2017262019-01-28 11:14:37 -08004171 spin_lock_init(&lpfc_ncmd->buf_lock);
James Smart0794d602019-01-28 11:14:19 -08004172
4173 /* add the nvme buffer to a post list */
4174 list_add_tail(&lpfc_ncmd->list, &post_nblist);
James Smart5e5b5112019-01-28 11:14:22 -08004175 phba->sli4_hba.io_xri_cnt++;
James Smart0794d602019-01-28 11:14:19 -08004176 }
4177 lpfc_printf_log(phba, KERN_INFO, LOG_NVME,
4178 "6114 Allocate %d out of %d requested new NVME "
4179 "buffers\n", bcnt, num_to_alloc);
4180
4181 /* post the list of nvme buffer sgls to port if available */
4182 if (!list_empty(&post_nblist))
James Smart5e5b5112019-01-28 11:14:22 -08004183 num_posted = lpfc_sli4_post_io_sgl_list(
James Smart0794d602019-01-28 11:14:19 -08004184 phba, &post_nblist, bcnt);
4185 else
4186 num_posted = 0;
4187
4188 return num_posted;
4189}
4190
James Smart96418b52017-03-04 09:30:31 -08004191static uint64_t
4192lpfc_get_wwpn(struct lpfc_hba *phba)
4193{
4194 uint64_t wwn;
4195 int rc;
4196 LPFC_MBOXQ_t *mboxq;
4197 MAILBOX_t *mb;
4198
James Smart96418b52017-03-04 09:30:31 -08004199 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
4200 GFP_KERNEL);
4201 if (!mboxq)
4202 return (uint64_t)-1;
4203
4204 /* First get WWN of HBA instance */
4205 lpfc_read_nv(phba, mboxq);
4206 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4207 if (rc != MBX_SUCCESS) {
4208 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4209 "6019 Mailbox failed , mbxCmd x%x "
4210 "READ_NV, mbxStatus x%x\n",
4211 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
4212 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
4213 mempool_free(mboxq, phba->mbox_mem_pool);
4214 return (uint64_t) -1;
4215 }
4216 mb = &mboxq->u.mb;
4217 memcpy(&wwn, (char *)mb->un.varRDnvp.portname, sizeof(uint64_t));
4218 /* wwn is WWPN of HBA instance */
4219 mempool_free(mboxq, phba->mbox_mem_pool);
4220 if (phba->sli_rev == LPFC_SLI_REV4)
4221 return be64_to_cpu(wwn);
4222 else
Maurizio Lombardi286871a2017-08-23 16:55:48 -07004223 return rol64(wwn, 32);
James Smart96418b52017-03-04 09:30:31 -08004224}
4225
James Smarte59058c2008-08-24 21:49:00 -04004226/**
James Smart3621a712009-04-06 18:47:14 -04004227 * lpfc_create_port - Create an FC port
James Smarte59058c2008-08-24 21:49:00 -04004228 * @phba: pointer to lpfc hba data structure.
4229 * @instance: a unique integer ID to this FC port.
4230 * @dev: pointer to the device data structure.
4231 *
4232 * This routine creates a FC port for the upper layer protocol. The FC port
4233 * can be created on top of either a physical port or a virtual port provided
4234 * by the HBA. This routine also allocates a SCSI host data structure (shost)
4235 * and associates the FC port created before adding the shost into the SCSI
4236 * layer.
4237 *
4238 * Return codes
4239 * @vport - pointer to the virtual N_Port data structure.
4240 * NULL - port create failed.
4241 **/
James Smart2e0fef82007-06-17 19:56:36 -05004242struct lpfc_vport *
James Smart3de2a652007-08-02 11:09:59 -04004243lpfc_create_port(struct lpfc_hba *phba, int instance, struct device *dev)
James Smart47a86172007-04-25 09:53:22 -04004244{
James Smart2e0fef82007-06-17 19:56:36 -05004245 struct lpfc_vport *vport;
James Smart895427b2017-02-12 13:52:30 -08004246 struct Scsi_Host *shost = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05004247 int error = 0;
James Smart96418b52017-03-04 09:30:31 -08004248 int i;
4249 uint64_t wwn;
4250 bool use_no_reset_hba = false;
James Smart56bc8022017-06-15 22:56:43 -07004251 int rc;
James Smart96418b52017-03-04 09:30:31 -08004252
James Smart56bc8022017-06-15 22:56:43 -07004253 if (lpfc_no_hba_reset_cnt) {
4254 if (phba->sli_rev < LPFC_SLI_REV4 &&
4255 dev == &phba->pcidev->dev) {
4256 /* Reset the port first */
4257 lpfc_sli_brdrestart(phba);
4258 rc = lpfc_sli_chipset_init(phba);
4259 if (rc)
4260 return NULL;
4261 }
4262 wwn = lpfc_get_wwpn(phba);
4263 }
James Smart96418b52017-03-04 09:30:31 -08004264
4265 for (i = 0; i < lpfc_no_hba_reset_cnt; i++) {
4266 if (wwn == lpfc_no_hba_reset[i]) {
4267 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4268 "6020 Setting use_no_reset port=%llx\n",
4269 wwn);
4270 use_no_reset_hba = true;
4271 break;
4272 }
4273 }
James Smart47a86172007-04-25 09:53:22 -04004274
James Smart895427b2017-02-12 13:52:30 -08004275 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) {
4276 if (dev != &phba->pcidev->dev) {
4277 shost = scsi_host_alloc(&lpfc_vport_template,
4278 sizeof(struct lpfc_vport));
4279 } else {
James Smart96418b52017-03-04 09:30:31 -08004280 if (!use_no_reset_hba)
James Smart895427b2017-02-12 13:52:30 -08004281 shost = scsi_host_alloc(&lpfc_template,
4282 sizeof(struct lpfc_vport));
4283 else
James Smart96418b52017-03-04 09:30:31 -08004284 shost = scsi_host_alloc(&lpfc_template_no_hr,
James Smart895427b2017-02-12 13:52:30 -08004285 sizeof(struct lpfc_vport));
4286 }
4287 } else if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
4288 shost = scsi_host_alloc(&lpfc_template_nvme,
James Smartea4142f2015-04-07 15:07:13 -04004289 sizeof(struct lpfc_vport));
4290 }
James Smart2e0fef82007-06-17 19:56:36 -05004291 if (!shost)
4292 goto out;
James Smart47a86172007-04-25 09:53:22 -04004293
James Smart2e0fef82007-06-17 19:56:36 -05004294 vport = (struct lpfc_vport *) shost->hostdata;
4295 vport->phba = phba;
James Smart2e0fef82007-06-17 19:56:36 -05004296 vport->load_flag |= FC_LOADING;
James Smart92d7f7b2007-06-17 19:56:38 -05004297 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart7f5f3d02008-02-08 18:50:14 -05004298 vport->fc_rscn_flush = 0;
James Smart3de2a652007-08-02 11:09:59 -04004299 lpfc_get_vport_cfgparam(vport);
James Smart895427b2017-02-12 13:52:30 -08004300
James Smartf6e84792019-01-28 11:14:38 -08004301 /* Adjust value in vport */
4302 vport->cfg_enable_fc4_type = phba->cfg_enable_fc4_type;
4303
James Smart2e0fef82007-06-17 19:56:36 -05004304 shost->unique_id = instance;
4305 shost->max_id = LPFC_MAX_TARGET;
James Smart3de2a652007-08-02 11:09:59 -04004306 shost->max_lun = vport->cfg_max_luns;
James Smart2e0fef82007-06-17 19:56:36 -05004307 shost->this_id = -1;
4308 shost->max_cmd_len = 16;
James Smart6a828b02019-01-28 11:14:31 -08004309
James Smartda0436e2009-05-22 14:51:39 -04004310 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart6a828b02019-01-28 11:14:31 -08004311 if (phba->cfg_fcp_io_sched == LPFC_FCP_SCHED_BY_HDWQ)
4312 shost->nr_hw_queues = phba->cfg_hdw_queue;
4313 else
4314 shost->nr_hw_queues = phba->sli4_hba.num_present_cpu;
4315
James Smart28baac72010-02-12 14:42:03 -05004316 shost->dma_boundary =
James Smartcb5172e2010-03-15 11:25:07 -04004317 phba->sli4_hba.pc_sli4_params.sge_supp_len-1;
James Smart5b9e70b2018-09-10 10:30:42 -07004318 shost->sg_tablesize = phba->cfg_scsi_seg_cnt;
James Smartace44e42019-01-28 11:14:27 -08004319 } else
4320 /* SLI-3 has a limited number of hardware queues (3),
4321 * thus there is only one for FCP processing.
4322 */
4323 shost->nr_hw_queues = 1;
James Smart81301a92008-12-04 22:39:46 -05004324
James Smart47a86172007-04-25 09:53:22 -04004325 /*
James Smart2e0fef82007-06-17 19:56:36 -05004326 * Set initial can_queue value since 0 is no longer supported and
4327 * scsi_add_host will fail. This will be adjusted later based on the
4328 * max xri value determined in hba setup.
James Smart47a86172007-04-25 09:53:22 -04004329 */
James Smart2e0fef82007-06-17 19:56:36 -05004330 shost->can_queue = phba->cfg_hba_queue_depth - 10;
James Smart3de2a652007-08-02 11:09:59 -04004331 if (dev != &phba->pcidev->dev) {
James Smart92d7f7b2007-06-17 19:56:38 -05004332 shost->transportt = lpfc_vport_transport_template;
4333 vport->port_type = LPFC_NPIV_PORT;
4334 } else {
4335 shost->transportt = lpfc_transport_template;
4336 vport->port_type = LPFC_PHYSICAL_PORT;
4337 }
James Smart47a86172007-04-25 09:53:22 -04004338
James Smart2e0fef82007-06-17 19:56:36 -05004339 /* Initialize all internally managed lists. */
4340 INIT_LIST_HEAD(&vport->fc_nodes);
James Smartda0436e2009-05-22 14:51:39 -04004341 INIT_LIST_HEAD(&vport->rcv_buffer_list);
James Smart2e0fef82007-06-17 19:56:36 -05004342 spin_lock_init(&vport->work_port_lock);
James Smart47a86172007-04-25 09:53:22 -04004343
Kees Cookf22eb4d2017-09-06 20:24:26 -07004344 timer_setup(&vport->fc_disctmo, lpfc_disc_timeout, 0);
James Smart47a86172007-04-25 09:53:22 -04004345
Kees Cookf22eb4d2017-09-06 20:24:26 -07004346 timer_setup(&vport->els_tmofunc, lpfc_els_timeout, 0);
James Smart92494142011-02-16 12:39:44 -05004347
Kees Cookf22eb4d2017-09-06 20:24:26 -07004348 timer_setup(&vport->delayed_disc_tmo, lpfc_delayed_disc_tmo, 0);
James Smart92494142011-02-16 12:39:44 -05004349
James Bottomleyd139b9b2009-11-05 13:33:12 -06004350 error = scsi_add_host_with_dma(shost, dev, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05004351 if (error)
4352 goto out_put_shost;
James Smart47a86172007-04-25 09:53:22 -04004353
James Smart523128e2018-09-10 10:30:46 -07004354 spin_lock_irq(&phba->port_list_lock);
James Smart2e0fef82007-06-17 19:56:36 -05004355 list_add_tail(&vport->listentry, &phba->port_list);
James Smart523128e2018-09-10 10:30:46 -07004356 spin_unlock_irq(&phba->port_list_lock);
James Smart2e0fef82007-06-17 19:56:36 -05004357 return vport;
James Smart47a86172007-04-25 09:53:22 -04004358
James Smart2e0fef82007-06-17 19:56:36 -05004359out_put_shost:
4360 scsi_host_put(shost);
4361out:
4362 return NULL;
James Smart47a86172007-04-25 09:53:22 -04004363}
4364
James Smarte59058c2008-08-24 21:49:00 -04004365/**
James Smart3621a712009-04-06 18:47:14 -04004366 * destroy_port - destroy an FC port
James Smarte59058c2008-08-24 21:49:00 -04004367 * @vport: pointer to an lpfc virtual N_Port data structure.
4368 *
4369 * This routine destroys a FC port from the upper layer protocol. All the
4370 * resources associated with the port are released.
4371 **/
James Smart2e0fef82007-06-17 19:56:36 -05004372void
4373destroy_port(struct lpfc_vport *vport)
James Smart47a86172007-04-25 09:53:22 -04004374{
James Smart92d7f7b2007-06-17 19:56:38 -05004375 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4376 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04004377
James Smart858c9f62007-06-17 19:56:39 -05004378 lpfc_debugfs_terminate(vport);
James Smart92d7f7b2007-06-17 19:56:38 -05004379 fc_remove_host(shost);
4380 scsi_remove_host(shost);
James Smart47a86172007-04-25 09:53:22 -04004381
James Smart523128e2018-09-10 10:30:46 -07004382 spin_lock_irq(&phba->port_list_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05004383 list_del_init(&vport->listentry);
James Smart523128e2018-09-10 10:30:46 -07004384 spin_unlock_irq(&phba->port_list_lock);
James Smart47a86172007-04-25 09:53:22 -04004385
James Smart92d7f7b2007-06-17 19:56:38 -05004386 lpfc_cleanup(vport);
James Smart47a86172007-04-25 09:53:22 -04004387 return;
James Smart47a86172007-04-25 09:53:22 -04004388}
4389
James Smarte59058c2008-08-24 21:49:00 -04004390/**
James Smart3621a712009-04-06 18:47:14 -04004391 * lpfc_get_instance - Get a unique integer ID
James Smarte59058c2008-08-24 21:49:00 -04004392 *
4393 * This routine allocates a unique integer ID from lpfc_hba_index pool. It
4394 * uses the kernel idr facility to perform the task.
4395 *
4396 * Return codes:
4397 * instance - a unique integer ID allocated as the new instance.
4398 * -1 - lpfc get instance failed.
4399 **/
James Smart92d7f7b2007-06-17 19:56:38 -05004400int
4401lpfc_get_instance(void)
4402{
Tejun Heoab516032013-02-27 17:04:44 -08004403 int ret;
James Smart92d7f7b2007-06-17 19:56:38 -05004404
Tejun Heoab516032013-02-27 17:04:44 -08004405 ret = idr_alloc(&lpfc_hba_index, NULL, 0, 0, GFP_KERNEL);
4406 return ret < 0 ? -1 : ret;
James Smart92d7f7b2007-06-17 19:56:38 -05004407}
4408
James Smarte59058c2008-08-24 21:49:00 -04004409/**
James Smart3621a712009-04-06 18:47:14 -04004410 * lpfc_scan_finished - method for SCSI layer to detect whether scan is done
James Smarte59058c2008-08-24 21:49:00 -04004411 * @shost: pointer to SCSI host data structure.
4412 * @time: elapsed time of the scan in jiffies.
4413 *
4414 * This routine is called by the SCSI layer with a SCSI host to determine
4415 * whether the scan host is finished.
4416 *
4417 * Note: there is no scan_start function as adapter initialization will have
4418 * asynchronously kicked off the link initialization.
4419 *
4420 * Return codes
4421 * 0 - SCSI host scan is not over yet.
4422 * 1 - SCSI host scan is over.
4423 **/
James Smart47a86172007-04-25 09:53:22 -04004424int lpfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
4425{
James Smart2e0fef82007-06-17 19:56:36 -05004426 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4427 struct lpfc_hba *phba = vport->phba;
James Smart858c9f62007-06-17 19:56:39 -05004428 int stat = 0;
James Smart47a86172007-04-25 09:53:22 -04004429
James Smart858c9f62007-06-17 19:56:39 -05004430 spin_lock_irq(shost->host_lock);
4431
James Smart51ef4c22007-08-02 11:10:31 -04004432 if (vport->load_flag & FC_UNLOADING) {
James Smart858c9f62007-06-17 19:56:39 -05004433 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04004434 goto finished;
James Smart858c9f62007-06-17 19:56:39 -05004435 }
James Smart256ec0d2013-04-17 20:14:58 -04004436 if (time >= msecs_to_jiffies(30 * 1000)) {
James Smart2e0fef82007-06-17 19:56:36 -05004437 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004438 "0461 Scanning longer than 30 "
4439 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05004440 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04004441 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05004442 }
James Smart256ec0d2013-04-17 20:14:58 -04004443 if (time >= msecs_to_jiffies(15 * 1000) &&
4444 phba->link_state <= LPFC_LINK_DOWN) {
James Smart2e0fef82007-06-17 19:56:36 -05004445 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004446 "0465 Link down longer than 15 "
4447 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05004448 stat = 1;
James Smart2e0fef82007-06-17 19:56:36 -05004449 goto finished;
James Smart47a86172007-04-25 09:53:22 -04004450 }
4451
James Smart2e0fef82007-06-17 19:56:36 -05004452 if (vport->port_state != LPFC_VPORT_READY)
James Smart858c9f62007-06-17 19:56:39 -05004453 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05004454 if (vport->num_disc_nodes || vport->fc_prli_sent)
James Smart858c9f62007-06-17 19:56:39 -05004455 goto finished;
James Smart256ec0d2013-04-17 20:14:58 -04004456 if (vport->fc_map_cnt == 0 && time < msecs_to_jiffies(2 * 1000))
James Smart858c9f62007-06-17 19:56:39 -05004457 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05004458 if ((phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) != 0)
James Smart858c9f62007-06-17 19:56:39 -05004459 goto finished;
4460
4461 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04004462
4463finished:
James Smart858c9f62007-06-17 19:56:39 -05004464 spin_unlock_irq(shost->host_lock);
4465 return stat;
James Smart92d7f7b2007-06-17 19:56:38 -05004466}
4467
Bart Van Assche3999df72019-03-28 11:06:16 -07004468static void lpfc_host_supported_speeds_set(struct Scsi_Host *shost)
James Smartcd713482018-10-23 13:41:01 -07004469{
4470 struct lpfc_vport *vport = (struct lpfc_vport *)shost->hostdata;
4471 struct lpfc_hba *phba = vport->phba;
4472
4473 fc_host_supported_speeds(shost) = 0;
James Smart1dc5ec22018-10-23 13:41:11 -07004474 if (phba->lmt & LMT_128Gb)
4475 fc_host_supported_speeds(shost) |= FC_PORTSPEED_128GBIT;
James Smartcd713482018-10-23 13:41:01 -07004476 if (phba->lmt & LMT_64Gb)
4477 fc_host_supported_speeds(shost) |= FC_PORTSPEED_64GBIT;
4478 if (phba->lmt & LMT_32Gb)
4479 fc_host_supported_speeds(shost) |= FC_PORTSPEED_32GBIT;
4480 if (phba->lmt & LMT_16Gb)
4481 fc_host_supported_speeds(shost) |= FC_PORTSPEED_16GBIT;
4482 if (phba->lmt & LMT_10Gb)
4483 fc_host_supported_speeds(shost) |= FC_PORTSPEED_10GBIT;
4484 if (phba->lmt & LMT_8Gb)
4485 fc_host_supported_speeds(shost) |= FC_PORTSPEED_8GBIT;
4486 if (phba->lmt & LMT_4Gb)
4487 fc_host_supported_speeds(shost) |= FC_PORTSPEED_4GBIT;
4488 if (phba->lmt & LMT_2Gb)
4489 fc_host_supported_speeds(shost) |= FC_PORTSPEED_2GBIT;
4490 if (phba->lmt & LMT_1Gb)
4491 fc_host_supported_speeds(shost) |= FC_PORTSPEED_1GBIT;
4492}
4493
James Smarte59058c2008-08-24 21:49:00 -04004494/**
James Smart3621a712009-04-06 18:47:14 -04004495 * lpfc_host_attrib_init - Initialize SCSI host attributes on a FC port
James Smarte59058c2008-08-24 21:49:00 -04004496 * @shost: pointer to SCSI host data structure.
4497 *
4498 * This routine initializes a given SCSI host attributes on a FC port. The
4499 * SCSI host can be either on top of a physical port or a virtual port.
4500 **/
James Smart92d7f7b2007-06-17 19:56:38 -05004501void lpfc_host_attrib_init(struct Scsi_Host *shost)
4502{
4503 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4504 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04004505 /*
James Smart2e0fef82007-06-17 19:56:36 -05004506 * Set fixed host attributes. Must done after lpfc_sli_hba_setup().
James Smart47a86172007-04-25 09:53:22 -04004507 */
4508
James Smart2e0fef82007-06-17 19:56:36 -05004509 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
4510 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
James Smart47a86172007-04-25 09:53:22 -04004511 fc_host_supported_classes(shost) = FC_COS_CLASS3;
4512
4513 memset(fc_host_supported_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05004514 sizeof(fc_host_supported_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04004515 fc_host_supported_fc4s(shost)[2] = 1;
4516 fc_host_supported_fc4s(shost)[7] = 1;
4517
James Smart92d7f7b2007-06-17 19:56:38 -05004518 lpfc_vport_symbolic_node_name(vport, fc_host_symbolic_name(shost),
4519 sizeof fc_host_symbolic_name(shost));
James Smart47a86172007-04-25 09:53:22 -04004520
James Smartcd713482018-10-23 13:41:01 -07004521 lpfc_host_supported_speeds_set(shost);
James Smart47a86172007-04-25 09:53:22 -04004522
4523 fc_host_maxframe_size(shost) =
James Smart2e0fef82007-06-17 19:56:36 -05004524 (((uint32_t) vport->fc_sparam.cmn.bbRcvSizeMsb & 0x0F) << 8) |
4525 (uint32_t) vport->fc_sparam.cmn.bbRcvSizeLsb;
James Smart47a86172007-04-25 09:53:22 -04004526
Mike Christie0af5d702010-09-15 16:52:31 -05004527 fc_host_dev_loss_tmo(shost) = vport->cfg_devloss_tmo;
4528
James Smart47a86172007-04-25 09:53:22 -04004529 /* This value is also unchanging */
4530 memset(fc_host_active_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05004531 sizeof(fc_host_active_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04004532 fc_host_active_fc4s(shost)[2] = 1;
4533 fc_host_active_fc4s(shost)[7] = 1;
4534
James Smart92d7f7b2007-06-17 19:56:38 -05004535 fc_host_max_npiv_vports(shost) = phba->max_vpi;
James Smart47a86172007-04-25 09:53:22 -04004536 spin_lock_irq(shost->host_lock);
James Smart51ef4c22007-08-02 11:10:31 -04004537 vport->load_flag &= ~FC_LOADING;
James Smart47a86172007-04-25 09:53:22 -04004538 spin_unlock_irq(shost->host_lock);
James Smart47a86172007-04-25 09:53:22 -04004539}
dea31012005-04-17 16:05:31 -05004540
James Smarte59058c2008-08-24 21:49:00 -04004541/**
James Smartda0436e2009-05-22 14:51:39 -04004542 * lpfc_stop_port_s3 - Stop SLI3 device port
James Smarte59058c2008-08-24 21:49:00 -04004543 * @phba: pointer to lpfc hba data structure.
4544 *
James Smartda0436e2009-05-22 14:51:39 -04004545 * This routine is invoked to stop an SLI3 device port, it stops the device
4546 * from generating interrupts and stops the device driver's timers for the
4547 * device.
James Smarte59058c2008-08-24 21:49:00 -04004548 **/
James Smartdb2378e2008-02-08 18:49:51 -05004549static void
James Smartda0436e2009-05-22 14:51:39 -04004550lpfc_stop_port_s3(struct lpfc_hba *phba)
James Smartdb2378e2008-02-08 18:49:51 -05004551{
James Smartda0436e2009-05-22 14:51:39 -04004552 /* Clear all interrupt enable conditions */
4553 writel(0, phba->HCregaddr);
4554 readl(phba->HCregaddr); /* flush */
4555 /* Clear all pending interrupts */
4556 writel(0xffffffff, phba->HAregaddr);
4557 readl(phba->HAregaddr); /* flush */
James Smart93996272008-08-24 21:50:30 -04004558
James Smartda0436e2009-05-22 14:51:39 -04004559 /* Reset some HBA SLI setup states */
4560 lpfc_stop_hba_timers(phba);
4561 phba->pport->work_port_events = 0;
James Smartdb2378e2008-02-08 18:49:51 -05004562}
4563
James Smarte59058c2008-08-24 21:49:00 -04004564/**
James Smartda0436e2009-05-22 14:51:39 -04004565 * lpfc_stop_port_s4 - Stop SLI4 device port
James Smart5b75da22008-12-04 22:39:35 -05004566 * @phba: pointer to lpfc hba data structure.
4567 *
James Smartda0436e2009-05-22 14:51:39 -04004568 * This routine is invoked to stop an SLI4 device port, it stops the device
4569 * from generating interrupts and stops the device driver's timers for the
4570 * device.
4571 **/
4572static void
4573lpfc_stop_port_s4(struct lpfc_hba *phba)
4574{
4575 /* Reset some HBA SLI4 setup states */
4576 lpfc_stop_hba_timers(phba);
James Smartcdb42be2019-01-28 11:14:21 -08004577 if (phba->pport)
4578 phba->pport->work_port_events = 0;
James Smartda0436e2009-05-22 14:51:39 -04004579 phba->sli4_hba.intr_enable = 0;
James Smartda0436e2009-05-22 14:51:39 -04004580}
4581
4582/**
4583 * lpfc_stop_port - Wrapper function for stopping hba port
4584 * @phba: Pointer to HBA context object.
James Smart5b75da22008-12-04 22:39:35 -05004585 *
James Smartda0436e2009-05-22 14:51:39 -04004586 * This routine wraps the actual SLI3 or SLI4 hba stop port routine from
4587 * the API jump table function pointer from the lpfc_hba struct.
4588 **/
4589void
4590lpfc_stop_port(struct lpfc_hba *phba)
4591{
4592 phba->lpfc_stop_port(phba);
Dick Kennedyf485c182017-09-29 17:34:34 -07004593
4594 if (phba->wq)
4595 flush_workqueue(phba->wq);
James Smartda0436e2009-05-22 14:51:39 -04004596}
4597
4598/**
James Smartecfd03c2010-02-12 14:41:27 -05004599 * lpfc_fcf_redisc_wait_start_timer - Start fcf rediscover wait timer
4600 * @phba: Pointer to hba for which this call is being executed.
4601 *
4602 * This routine starts the timer waiting for the FCF rediscovery to complete.
4603 **/
4604void
4605lpfc_fcf_redisc_wait_start_timer(struct lpfc_hba *phba)
4606{
4607 unsigned long fcf_redisc_wait_tmo =
4608 (jiffies + msecs_to_jiffies(LPFC_FCF_REDISCOVER_WAIT_TMO));
4609 /* Start fcf rediscovery wait period timer */
4610 mod_timer(&phba->fcf.redisc_wait, fcf_redisc_wait_tmo);
4611 spin_lock_irq(&phba->hbalock);
4612 /* Allow action to new fcf asynchronous event */
4613 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
4614 /* Mark the FCF rediscovery pending state */
4615 phba->fcf.fcf_flag |= FCF_REDISC_PEND;
4616 spin_unlock_irq(&phba->hbalock);
4617}
4618
4619/**
4620 * lpfc_sli4_fcf_redisc_wait_tmo - FCF table rediscover wait timeout
4621 * @ptr: Map to lpfc_hba data structure pointer.
4622 *
4623 * This routine is invoked when waiting for FCF table rediscover has been
4624 * timed out. If new FCF record(s) has (have) been discovered during the
4625 * wait period, a new FCF event shall be added to the FCOE async event
4626 * list, and then worker thread shall be waked up for processing from the
4627 * worker thread context.
4628 **/
Rashika Kheriae399b222014-09-03 12:55:28 -04004629static void
Kees Cookf22eb4d2017-09-06 20:24:26 -07004630lpfc_sli4_fcf_redisc_wait_tmo(struct timer_list *t)
James Smartecfd03c2010-02-12 14:41:27 -05004631{
Kees Cookf22eb4d2017-09-06 20:24:26 -07004632 struct lpfc_hba *phba = from_timer(phba, t, fcf.redisc_wait);
James Smartecfd03c2010-02-12 14:41:27 -05004633
4634 /* Don't send FCF rediscovery event if timer cancelled */
4635 spin_lock_irq(&phba->hbalock);
4636 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
4637 spin_unlock_irq(&phba->hbalock);
4638 return;
4639 }
4640 /* Clear FCF rediscovery timer pending flag */
4641 phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
4642 /* FCF rediscovery event to worker thread */
4643 phba->fcf.fcf_flag |= FCF_REDISC_EVT;
4644 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004645 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04004646 "2776 FCF rediscover quiescent timer expired\n");
James Smartecfd03c2010-02-12 14:41:27 -05004647 /* wake up worker thread */
4648 lpfc_worker_wake_up(phba);
4649}
4650
4651/**
James Smartda0436e2009-05-22 14:51:39 -04004652 * lpfc_sli4_parse_latt_fault - Parse sli4 link-attention link fault code
4653 * @phba: pointer to lpfc hba data structure.
4654 * @acqe_link: pointer to the async link completion queue entry.
4655 *
James Smart23288b72018-05-04 20:37:53 -07004656 * This routine is to parse the SLI4 link-attention link fault code.
James Smartda0436e2009-05-22 14:51:39 -04004657 **/
James Smart23288b72018-05-04 20:37:53 -07004658static void
James Smartda0436e2009-05-22 14:51:39 -04004659lpfc_sli4_parse_latt_fault(struct lpfc_hba *phba,
4660 struct lpfc_acqe_link *acqe_link)
4661{
James Smartda0436e2009-05-22 14:51:39 -04004662 switch (bf_get(lpfc_acqe_link_fault, acqe_link)) {
4663 case LPFC_ASYNC_LINK_FAULT_NONE:
4664 case LPFC_ASYNC_LINK_FAULT_LOCAL:
4665 case LPFC_ASYNC_LINK_FAULT_REMOTE:
James Smart23288b72018-05-04 20:37:53 -07004666 case LPFC_ASYNC_LINK_FAULT_LR_LRR:
James Smartda0436e2009-05-22 14:51:39 -04004667 break;
4668 default:
4669 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart23288b72018-05-04 20:37:53 -07004670 "0398 Unknown link fault code: x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04004671 bf_get(lpfc_acqe_link_fault, acqe_link));
James Smartda0436e2009-05-22 14:51:39 -04004672 break;
4673 }
James Smartda0436e2009-05-22 14:51:39 -04004674}
4675
4676/**
4677 * lpfc_sli4_parse_latt_type - Parse sli4 link attention type
4678 * @phba: pointer to lpfc hba data structure.
4679 * @acqe_link: pointer to the async link completion queue entry.
4680 *
4681 * This routine is to parse the SLI4 link attention type and translate it
4682 * into the base driver's link attention type coding.
4683 *
4684 * Return: Link attention type in terms of base driver's coding.
4685 **/
4686static uint8_t
4687lpfc_sli4_parse_latt_type(struct lpfc_hba *phba,
4688 struct lpfc_acqe_link *acqe_link)
4689{
4690 uint8_t att_type;
4691
4692 switch (bf_get(lpfc_acqe_link_status, acqe_link)) {
4693 case LPFC_ASYNC_LINK_STATUS_DOWN:
4694 case LPFC_ASYNC_LINK_STATUS_LOGICAL_DOWN:
James Smart76a95d72010-11-20 23:11:48 -05004695 att_type = LPFC_ATT_LINK_DOWN;
James Smartda0436e2009-05-22 14:51:39 -04004696 break;
4697 case LPFC_ASYNC_LINK_STATUS_UP:
4698 /* Ignore physical link up events - wait for logical link up */
James Smart76a95d72010-11-20 23:11:48 -05004699 att_type = LPFC_ATT_RESERVED;
James Smartda0436e2009-05-22 14:51:39 -04004700 break;
4701 case LPFC_ASYNC_LINK_STATUS_LOGICAL_UP:
James Smart76a95d72010-11-20 23:11:48 -05004702 att_type = LPFC_ATT_LINK_UP;
James Smartda0436e2009-05-22 14:51:39 -04004703 break;
4704 default:
4705 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4706 "0399 Invalid link attention type: x%x\n",
4707 bf_get(lpfc_acqe_link_status, acqe_link));
James Smart76a95d72010-11-20 23:11:48 -05004708 att_type = LPFC_ATT_RESERVED;
James Smartda0436e2009-05-22 14:51:39 -04004709 break;
4710 }
4711 return att_type;
4712}
4713
4714/**
James Smart8b68cd52012-09-29 11:32:37 -04004715 * lpfc_sli_port_speed_get - Get sli3 link speed code to link speed
4716 * @phba: pointer to lpfc hba data structure.
4717 *
4718 * This routine is to get an SLI3 FC port's link speed in Mbps.
4719 *
4720 * Return: link speed in terms of Mbps.
4721 **/
4722uint32_t
4723lpfc_sli_port_speed_get(struct lpfc_hba *phba)
4724{
4725 uint32_t link_speed;
4726
4727 if (!lpfc_is_link_up(phba))
4728 return 0;
4729
James Smarta085e872015-12-16 18:12:02 -05004730 if (phba->sli_rev <= LPFC_SLI_REV3) {
4731 switch (phba->fc_linkspeed) {
4732 case LPFC_LINK_SPEED_1GHZ:
4733 link_speed = 1000;
4734 break;
4735 case LPFC_LINK_SPEED_2GHZ:
4736 link_speed = 2000;
4737 break;
4738 case LPFC_LINK_SPEED_4GHZ:
4739 link_speed = 4000;
4740 break;
4741 case LPFC_LINK_SPEED_8GHZ:
4742 link_speed = 8000;
4743 break;
4744 case LPFC_LINK_SPEED_10GHZ:
4745 link_speed = 10000;
4746 break;
4747 case LPFC_LINK_SPEED_16GHZ:
4748 link_speed = 16000;
4749 break;
4750 default:
4751 link_speed = 0;
4752 }
4753 } else {
4754 if (phba->sli4_hba.link_state.logical_speed)
4755 link_speed =
4756 phba->sli4_hba.link_state.logical_speed;
4757 else
4758 link_speed = phba->sli4_hba.link_state.speed;
James Smart8b68cd52012-09-29 11:32:37 -04004759 }
4760 return link_speed;
4761}
4762
4763/**
4764 * lpfc_sli4_port_speed_parse - Parse async evt link speed code to link speed
4765 * @phba: pointer to lpfc hba data structure.
4766 * @evt_code: asynchronous event code.
4767 * @speed_code: asynchronous event link speed code.
4768 *
4769 * This routine is to parse the giving SLI4 async event link speed code into
4770 * value of Mbps for the link speed.
4771 *
4772 * Return: link speed in terms of Mbps.
4773 **/
4774static uint32_t
4775lpfc_sli4_port_speed_parse(struct lpfc_hba *phba, uint32_t evt_code,
4776 uint8_t speed_code)
4777{
4778 uint32_t port_speed;
4779
4780 switch (evt_code) {
4781 case LPFC_TRAILER_CODE_LINK:
4782 switch (speed_code) {
James Smart26d830e2015-04-07 15:07:17 -04004783 case LPFC_ASYNC_LINK_SPEED_ZERO:
James Smart8b68cd52012-09-29 11:32:37 -04004784 port_speed = 0;
4785 break;
James Smart26d830e2015-04-07 15:07:17 -04004786 case LPFC_ASYNC_LINK_SPEED_10MBPS:
James Smart8b68cd52012-09-29 11:32:37 -04004787 port_speed = 10;
4788 break;
James Smart26d830e2015-04-07 15:07:17 -04004789 case LPFC_ASYNC_LINK_SPEED_100MBPS:
James Smart8b68cd52012-09-29 11:32:37 -04004790 port_speed = 100;
4791 break;
James Smart26d830e2015-04-07 15:07:17 -04004792 case LPFC_ASYNC_LINK_SPEED_1GBPS:
James Smart8b68cd52012-09-29 11:32:37 -04004793 port_speed = 1000;
4794 break;
James Smart26d830e2015-04-07 15:07:17 -04004795 case LPFC_ASYNC_LINK_SPEED_10GBPS:
James Smart8b68cd52012-09-29 11:32:37 -04004796 port_speed = 10000;
4797 break;
James Smart26d830e2015-04-07 15:07:17 -04004798 case LPFC_ASYNC_LINK_SPEED_20GBPS:
4799 port_speed = 20000;
4800 break;
4801 case LPFC_ASYNC_LINK_SPEED_25GBPS:
4802 port_speed = 25000;
4803 break;
4804 case LPFC_ASYNC_LINK_SPEED_40GBPS:
4805 port_speed = 40000;
4806 break;
James Smart8b68cd52012-09-29 11:32:37 -04004807 default:
4808 port_speed = 0;
4809 }
4810 break;
4811 case LPFC_TRAILER_CODE_FC:
4812 switch (speed_code) {
James Smart26d830e2015-04-07 15:07:17 -04004813 case LPFC_FC_LA_SPEED_UNKNOWN:
James Smart8b68cd52012-09-29 11:32:37 -04004814 port_speed = 0;
4815 break;
James Smart26d830e2015-04-07 15:07:17 -04004816 case LPFC_FC_LA_SPEED_1G:
James Smart8b68cd52012-09-29 11:32:37 -04004817 port_speed = 1000;
4818 break;
James Smart26d830e2015-04-07 15:07:17 -04004819 case LPFC_FC_LA_SPEED_2G:
James Smart8b68cd52012-09-29 11:32:37 -04004820 port_speed = 2000;
4821 break;
James Smart26d830e2015-04-07 15:07:17 -04004822 case LPFC_FC_LA_SPEED_4G:
James Smart8b68cd52012-09-29 11:32:37 -04004823 port_speed = 4000;
4824 break;
James Smart26d830e2015-04-07 15:07:17 -04004825 case LPFC_FC_LA_SPEED_8G:
James Smart8b68cd52012-09-29 11:32:37 -04004826 port_speed = 8000;
4827 break;
James Smart26d830e2015-04-07 15:07:17 -04004828 case LPFC_FC_LA_SPEED_10G:
James Smart8b68cd52012-09-29 11:32:37 -04004829 port_speed = 10000;
4830 break;
James Smart26d830e2015-04-07 15:07:17 -04004831 case LPFC_FC_LA_SPEED_16G:
James Smart8b68cd52012-09-29 11:32:37 -04004832 port_speed = 16000;
4833 break;
James Smartd38dd522015-08-31 16:48:17 -04004834 case LPFC_FC_LA_SPEED_32G:
4835 port_speed = 32000;
4836 break;
James Smartfbd8a6b2018-02-22 08:18:45 -08004837 case LPFC_FC_LA_SPEED_64G:
4838 port_speed = 64000;
4839 break;
James Smart1dc5ec22018-10-23 13:41:11 -07004840 case LPFC_FC_LA_SPEED_128G:
4841 port_speed = 128000;
4842 break;
James Smart8b68cd52012-09-29 11:32:37 -04004843 default:
4844 port_speed = 0;
4845 }
4846 break;
4847 default:
4848 port_speed = 0;
4849 }
4850 return port_speed;
4851}
4852
4853/**
James Smart70f3c072010-12-15 17:57:33 -05004854 * lpfc_sli4_async_link_evt - Process the asynchronous FCoE link event
James Smartda0436e2009-05-22 14:51:39 -04004855 * @phba: pointer to lpfc hba data structure.
4856 * @acqe_link: pointer to the async link completion queue entry.
4857 *
James Smart70f3c072010-12-15 17:57:33 -05004858 * This routine is to handle the SLI4 asynchronous FCoE link event.
James Smartda0436e2009-05-22 14:51:39 -04004859 **/
4860static void
4861lpfc_sli4_async_link_evt(struct lpfc_hba *phba,
4862 struct lpfc_acqe_link *acqe_link)
4863{
4864 struct lpfc_dmabuf *mp;
4865 LPFC_MBOXQ_t *pmb;
4866 MAILBOX_t *mb;
James Smart76a95d72010-11-20 23:11:48 -05004867 struct lpfc_mbx_read_top *la;
James Smartda0436e2009-05-22 14:51:39 -04004868 uint8_t att_type;
James Smart76a95d72010-11-20 23:11:48 -05004869 int rc;
James Smartda0436e2009-05-22 14:51:39 -04004870
4871 att_type = lpfc_sli4_parse_latt_type(phba, acqe_link);
James Smart76a95d72010-11-20 23:11:48 -05004872 if (att_type != LPFC_ATT_LINK_DOWN && att_type != LPFC_ATT_LINK_UP)
James Smartda0436e2009-05-22 14:51:39 -04004873 return;
James Smart32b97932009-07-19 10:01:21 -04004874 phba->fcoe_eventtag = acqe_link->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04004875 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4876 if (!pmb) {
4877 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4878 "0395 The mboxq allocation failed\n");
4879 return;
4880 }
4881 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4882 if (!mp) {
4883 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4884 "0396 The lpfc_dmabuf allocation failed\n");
4885 goto out_free_pmb;
4886 }
4887 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
4888 if (!mp->virt) {
4889 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4890 "0397 The mbuf allocation failed\n");
4891 goto out_free_dmabuf;
4892 }
4893
4894 /* Cleanup any outstanding ELS commands */
4895 lpfc_els_flush_all_cmd(phba);
4896
4897 /* Block ELS IOCBs until we have done process link event */
James Smart895427b2017-02-12 13:52:30 -08004898 phba->sli4_hba.els_wq->pring->flag |= LPFC_STOP_IOCB_EVENT;
James Smartda0436e2009-05-22 14:51:39 -04004899
4900 /* Update link event statistics */
4901 phba->sli.slistat.link_event++;
4902
James Smart76a95d72010-11-20 23:11:48 -05004903 /* Create lpfc_handle_latt mailbox command from link ACQE */
4904 lpfc_read_topology(phba, pmb, mp);
4905 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
James Smartda0436e2009-05-22 14:51:39 -04004906 pmb->vport = phba->pport;
4907
James Smartda0436e2009-05-22 14:51:39 -04004908 /* Keep the link status for extra SLI4 state machine reference */
4909 phba->sli4_hba.link_state.speed =
James Smart8b68cd52012-09-29 11:32:37 -04004910 lpfc_sli4_port_speed_parse(phba, LPFC_TRAILER_CODE_LINK,
4911 bf_get(lpfc_acqe_link_speed, acqe_link));
James Smartda0436e2009-05-22 14:51:39 -04004912 phba->sli4_hba.link_state.duplex =
4913 bf_get(lpfc_acqe_link_duplex, acqe_link);
4914 phba->sli4_hba.link_state.status =
4915 bf_get(lpfc_acqe_link_status, acqe_link);
James Smart70f3c072010-12-15 17:57:33 -05004916 phba->sli4_hba.link_state.type =
4917 bf_get(lpfc_acqe_link_type, acqe_link);
4918 phba->sli4_hba.link_state.number =
4919 bf_get(lpfc_acqe_link_number, acqe_link);
James Smartda0436e2009-05-22 14:51:39 -04004920 phba->sli4_hba.link_state.fault =
4921 bf_get(lpfc_acqe_link_fault, acqe_link);
James Smart65467b62010-01-26 23:08:29 -05004922 phba->sli4_hba.link_state.logical_speed =
James Smart8b68cd52012-09-29 11:32:37 -04004923 bf_get(lpfc_acqe_logical_link_speed, acqe_link) * 10;
4924
James Smart70f3c072010-12-15 17:57:33 -05004925 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartc31098c2011-04-16 11:03:33 -04004926 "2900 Async FC/FCoE Link event - Speed:%dGBit "
4927 "duplex:x%x LA Type:x%x Port Type:%d Port Number:%d "
4928 "Logical speed:%dMbps Fault:%d\n",
James Smart70f3c072010-12-15 17:57:33 -05004929 phba->sli4_hba.link_state.speed,
4930 phba->sli4_hba.link_state.topology,
4931 phba->sli4_hba.link_state.status,
4932 phba->sli4_hba.link_state.type,
4933 phba->sli4_hba.link_state.number,
James Smart8b68cd52012-09-29 11:32:37 -04004934 phba->sli4_hba.link_state.logical_speed,
James Smart70f3c072010-12-15 17:57:33 -05004935 phba->sli4_hba.link_state.fault);
James Smart76a95d72010-11-20 23:11:48 -05004936 /*
4937 * For FC Mode: issue the READ_TOPOLOGY mailbox command to fetch
4938 * topology info. Note: Optional for non FC-AL ports.
4939 */
4940 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
4941 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
4942 if (rc == MBX_NOT_FINISHED)
4943 goto out_free_dmabuf;
4944 return;
4945 }
4946 /*
4947 * For FCoE Mode: fill in all the topology information we need and call
4948 * the READ_TOPOLOGY completion routine to continue without actually
4949 * sending the READ_TOPOLOGY mailbox command to the port.
4950 */
James Smart23288b72018-05-04 20:37:53 -07004951 /* Initialize completion status */
James Smart76a95d72010-11-20 23:11:48 -05004952 mb = &pmb->u.mb;
James Smart23288b72018-05-04 20:37:53 -07004953 mb->mbxStatus = MBX_SUCCESS;
4954
4955 /* Parse port fault information field */
4956 lpfc_sli4_parse_latt_fault(phba, acqe_link);
James Smart76a95d72010-11-20 23:11:48 -05004957
4958 /* Parse and translate link attention fields */
4959 la = (struct lpfc_mbx_read_top *) &pmb->u.mb.un.varReadTop;
4960 la->eventTag = acqe_link->event_tag;
4961 bf_set(lpfc_mbx_read_top_att_type, la, att_type);
4962 bf_set(lpfc_mbx_read_top_link_spd, la,
James Smarta085e872015-12-16 18:12:02 -05004963 (bf_get(lpfc_acqe_link_speed, acqe_link)));
James Smart76a95d72010-11-20 23:11:48 -05004964
4965 /* Fake the the following irrelvant fields */
4966 bf_set(lpfc_mbx_read_top_topology, la, LPFC_TOPOLOGY_PT_PT);
4967 bf_set(lpfc_mbx_read_top_alpa_granted, la, 0);
4968 bf_set(lpfc_mbx_read_top_il, la, 0);
4969 bf_set(lpfc_mbx_read_top_pb, la, 0);
4970 bf_set(lpfc_mbx_read_top_fa, la, 0);
4971 bf_set(lpfc_mbx_read_top_mm, la, 0);
James Smartda0436e2009-05-22 14:51:39 -04004972
4973 /* Invoke the lpfc_handle_latt mailbox command callback function */
James Smart76a95d72010-11-20 23:11:48 -05004974 lpfc_mbx_cmpl_read_topology(phba, pmb);
James Smartda0436e2009-05-22 14:51:39 -04004975
4976 return;
4977
4978out_free_dmabuf:
4979 kfree(mp);
4980out_free_pmb:
4981 mempool_free(pmb, phba->mbox_mem_pool);
4982}
4983
4984/**
James Smart1dc5ec22018-10-23 13:41:11 -07004985 * lpfc_async_link_speed_to_read_top - Parse async evt link speed code to read
4986 * topology.
4987 * @phba: pointer to lpfc hba data structure.
4988 * @evt_code: asynchronous event code.
4989 * @speed_code: asynchronous event link speed code.
4990 *
4991 * This routine is to parse the giving SLI4 async event link speed code into
4992 * value of Read topology link speed.
4993 *
4994 * Return: link speed in terms of Read topology.
4995 **/
4996static uint8_t
4997lpfc_async_link_speed_to_read_top(struct lpfc_hba *phba, uint8_t speed_code)
4998{
4999 uint8_t port_speed;
5000
5001 switch (speed_code) {
5002 case LPFC_FC_LA_SPEED_1G:
5003 port_speed = LPFC_LINK_SPEED_1GHZ;
5004 break;
5005 case LPFC_FC_LA_SPEED_2G:
5006 port_speed = LPFC_LINK_SPEED_2GHZ;
5007 break;
5008 case LPFC_FC_LA_SPEED_4G:
5009 port_speed = LPFC_LINK_SPEED_4GHZ;
5010 break;
5011 case LPFC_FC_LA_SPEED_8G:
5012 port_speed = LPFC_LINK_SPEED_8GHZ;
5013 break;
5014 case LPFC_FC_LA_SPEED_16G:
5015 port_speed = LPFC_LINK_SPEED_16GHZ;
5016 break;
5017 case LPFC_FC_LA_SPEED_32G:
5018 port_speed = LPFC_LINK_SPEED_32GHZ;
5019 break;
5020 case LPFC_FC_LA_SPEED_64G:
5021 port_speed = LPFC_LINK_SPEED_64GHZ;
5022 break;
5023 case LPFC_FC_LA_SPEED_128G:
5024 port_speed = LPFC_LINK_SPEED_128GHZ;
5025 break;
5026 case LPFC_FC_LA_SPEED_256G:
5027 port_speed = LPFC_LINK_SPEED_256GHZ;
5028 break;
5029 default:
5030 port_speed = 0;
5031 break;
5032 }
5033
5034 return port_speed;
5035}
5036
5037#define trunk_link_status(__idx)\
5038 bf_get(lpfc_acqe_fc_la_trunk_config_port##__idx, acqe_fc) ?\
5039 ((phba->trunk_link.link##__idx.state == LPFC_LINK_UP) ?\
5040 "Link up" : "Link down") : "NA"
5041/* Did port __idx reported an error */
5042#define trunk_port_fault(__idx)\
5043 bf_get(lpfc_acqe_fc_la_trunk_config_port##__idx, acqe_fc) ?\
5044 (port_fault & (1 << __idx) ? "YES" : "NO") : "NA"
5045
5046static void
5047lpfc_update_trunk_link_status(struct lpfc_hba *phba,
5048 struct lpfc_acqe_fc_la *acqe_fc)
5049{
5050 uint8_t port_fault = bf_get(lpfc_acqe_fc_la_trunk_linkmask, acqe_fc);
5051 uint8_t err = bf_get(lpfc_acqe_fc_la_trunk_fault, acqe_fc);
5052
5053 phba->sli4_hba.link_state.speed =
5054 lpfc_sli4_port_speed_parse(phba, LPFC_TRAILER_CODE_FC,
5055 bf_get(lpfc_acqe_fc_la_speed, acqe_fc));
5056
5057 phba->sli4_hba.link_state.logical_speed =
James Smartb8e6f132019-05-21 17:49:04 -07005058 bf_get(lpfc_acqe_fc_la_llink_spd, acqe_fc) * 10;
James Smart1dc5ec22018-10-23 13:41:11 -07005059 /* We got FC link speed, convert to fc_linkspeed (READ_TOPOLOGY) */
5060 phba->fc_linkspeed =
5061 lpfc_async_link_speed_to_read_top(
5062 phba,
5063 bf_get(lpfc_acqe_fc_la_speed, acqe_fc));
5064
5065 if (bf_get(lpfc_acqe_fc_la_trunk_config_port0, acqe_fc)) {
5066 phba->trunk_link.link0.state =
5067 bf_get(lpfc_acqe_fc_la_trunk_link_status_port0, acqe_fc)
5068 ? LPFC_LINK_UP : LPFC_LINK_DOWN;
James Smart529b3dd2018-12-13 15:17:54 -08005069 phba->trunk_link.link0.fault = port_fault & 0x1 ? err : 0;
James Smart1dc5ec22018-10-23 13:41:11 -07005070 }
5071 if (bf_get(lpfc_acqe_fc_la_trunk_config_port1, acqe_fc)) {
5072 phba->trunk_link.link1.state =
5073 bf_get(lpfc_acqe_fc_la_trunk_link_status_port1, acqe_fc)
5074 ? LPFC_LINK_UP : LPFC_LINK_DOWN;
James Smart529b3dd2018-12-13 15:17:54 -08005075 phba->trunk_link.link1.fault = port_fault & 0x2 ? err : 0;
James Smart1dc5ec22018-10-23 13:41:11 -07005076 }
5077 if (bf_get(lpfc_acqe_fc_la_trunk_config_port2, acqe_fc)) {
5078 phba->trunk_link.link2.state =
5079 bf_get(lpfc_acqe_fc_la_trunk_link_status_port2, acqe_fc)
5080 ? LPFC_LINK_UP : LPFC_LINK_DOWN;
James Smart529b3dd2018-12-13 15:17:54 -08005081 phba->trunk_link.link2.fault = port_fault & 0x4 ? err : 0;
James Smart1dc5ec22018-10-23 13:41:11 -07005082 }
5083 if (bf_get(lpfc_acqe_fc_la_trunk_config_port3, acqe_fc)) {
5084 phba->trunk_link.link3.state =
5085 bf_get(lpfc_acqe_fc_la_trunk_link_status_port3, acqe_fc)
5086 ? LPFC_LINK_UP : LPFC_LINK_DOWN;
James Smart529b3dd2018-12-13 15:17:54 -08005087 phba->trunk_link.link3.fault = port_fault & 0x8 ? err : 0;
James Smart1dc5ec22018-10-23 13:41:11 -07005088 }
5089
5090 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5091 "2910 Async FC Trunking Event - Speed:%d\n"
5092 "\tLogical speed:%d "
5093 "port0: %s port1: %s port2: %s port3: %s\n",
5094 phba->sli4_hba.link_state.speed,
5095 phba->sli4_hba.link_state.logical_speed,
5096 trunk_link_status(0), trunk_link_status(1),
5097 trunk_link_status(2), trunk_link_status(3));
5098
5099 if (port_fault)
5100 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5101 "3202 trunk error:0x%x (%s) seen on port0:%s "
5102 /*
5103 * SLI-4: We have only 0xA error codes
5104 * defined as of now. print an appropriate
5105 * message in case driver needs to be updated.
5106 */
5107 "port1:%s port2:%s port3:%s\n", err, err > 0xA ?
5108 "UNDEFINED. update driver." : trunk_errmsg[err],
5109 trunk_port_fault(0), trunk_port_fault(1),
5110 trunk_port_fault(2), trunk_port_fault(3));
5111}
5112
5113
5114/**
James Smart70f3c072010-12-15 17:57:33 -05005115 * lpfc_sli4_async_fc_evt - Process the asynchronous FC link event
5116 * @phba: pointer to lpfc hba data structure.
5117 * @acqe_fc: pointer to the async fc completion queue entry.
5118 *
5119 * This routine is to handle the SLI4 asynchronous FC event. It will simply log
5120 * that the event was received and then issue a read_topology mailbox command so
5121 * that the rest of the driver will treat it the same as SLI3.
5122 **/
5123static void
5124lpfc_sli4_async_fc_evt(struct lpfc_hba *phba, struct lpfc_acqe_fc_la *acqe_fc)
5125{
5126 struct lpfc_dmabuf *mp;
5127 LPFC_MBOXQ_t *pmb;
James Smart7bdedb32016-07-06 12:36:00 -07005128 MAILBOX_t *mb;
5129 struct lpfc_mbx_read_top *la;
James Smart70f3c072010-12-15 17:57:33 -05005130 int rc;
5131
5132 if (bf_get(lpfc_trailer_type, acqe_fc) !=
5133 LPFC_FC_LA_EVENT_TYPE_FC_LINK) {
5134 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5135 "2895 Non FC link Event detected.(%d)\n",
5136 bf_get(lpfc_trailer_type, acqe_fc));
5137 return;
5138 }
James Smart1dc5ec22018-10-23 13:41:11 -07005139
5140 if (bf_get(lpfc_acqe_fc_la_att_type, acqe_fc) ==
5141 LPFC_FC_LA_TYPE_TRUNKING_EVENT) {
5142 lpfc_update_trunk_link_status(phba, acqe_fc);
5143 return;
5144 }
5145
James Smart70f3c072010-12-15 17:57:33 -05005146 /* Keep the link status for extra SLI4 state machine reference */
5147 phba->sli4_hba.link_state.speed =
James Smart8b68cd52012-09-29 11:32:37 -04005148 lpfc_sli4_port_speed_parse(phba, LPFC_TRAILER_CODE_FC,
5149 bf_get(lpfc_acqe_fc_la_speed, acqe_fc));
James Smart70f3c072010-12-15 17:57:33 -05005150 phba->sli4_hba.link_state.duplex = LPFC_ASYNC_LINK_DUPLEX_FULL;
5151 phba->sli4_hba.link_state.topology =
5152 bf_get(lpfc_acqe_fc_la_topology, acqe_fc);
5153 phba->sli4_hba.link_state.status =
5154 bf_get(lpfc_acqe_fc_la_att_type, acqe_fc);
5155 phba->sli4_hba.link_state.type =
5156 bf_get(lpfc_acqe_fc_la_port_type, acqe_fc);
5157 phba->sli4_hba.link_state.number =
5158 bf_get(lpfc_acqe_fc_la_port_number, acqe_fc);
5159 phba->sli4_hba.link_state.fault =
5160 bf_get(lpfc_acqe_link_fault, acqe_fc);
James Smartb8e6f132019-05-21 17:49:04 -07005161
5162 if (bf_get(lpfc_acqe_fc_la_att_type, acqe_fc) ==
5163 LPFC_FC_LA_TYPE_LINK_DOWN)
5164 phba->sli4_hba.link_state.logical_speed = 0;
5165 else if (!phba->sli4_hba.conf_trunk)
5166 phba->sli4_hba.link_state.logical_speed =
James Smart8b68cd52012-09-29 11:32:37 -04005167 bf_get(lpfc_acqe_fc_la_llink_spd, acqe_fc) * 10;
James Smartb8e6f132019-05-21 17:49:04 -07005168
James Smart70f3c072010-12-15 17:57:33 -05005169 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5170 "2896 Async FC event - Speed:%dGBaud Topology:x%x "
5171 "LA Type:x%x Port Type:%d Port Number:%d Logical speed:"
5172 "%dMbps Fault:%d\n",
5173 phba->sli4_hba.link_state.speed,
5174 phba->sli4_hba.link_state.topology,
5175 phba->sli4_hba.link_state.status,
5176 phba->sli4_hba.link_state.type,
5177 phba->sli4_hba.link_state.number,
James Smart8b68cd52012-09-29 11:32:37 -04005178 phba->sli4_hba.link_state.logical_speed,
James Smart70f3c072010-12-15 17:57:33 -05005179 phba->sli4_hba.link_state.fault);
5180 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5181 if (!pmb) {
5182 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5183 "2897 The mboxq allocation failed\n");
5184 return;
5185 }
5186 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5187 if (!mp) {
5188 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5189 "2898 The lpfc_dmabuf allocation failed\n");
5190 goto out_free_pmb;
5191 }
5192 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
5193 if (!mp->virt) {
5194 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5195 "2899 The mbuf allocation failed\n");
5196 goto out_free_dmabuf;
5197 }
5198
5199 /* Cleanup any outstanding ELS commands */
5200 lpfc_els_flush_all_cmd(phba);
5201
5202 /* Block ELS IOCBs until we have done process link event */
James Smart895427b2017-02-12 13:52:30 -08005203 phba->sli4_hba.els_wq->pring->flag |= LPFC_STOP_IOCB_EVENT;
James Smart70f3c072010-12-15 17:57:33 -05005204
5205 /* Update link event statistics */
5206 phba->sli.slistat.link_event++;
5207
5208 /* Create lpfc_handle_latt mailbox command from link ACQE */
5209 lpfc_read_topology(phba, pmb, mp);
5210 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
5211 pmb->vport = phba->pport;
5212
James Smart7bdedb32016-07-06 12:36:00 -07005213 if (phba->sli4_hba.link_state.status != LPFC_FC_LA_TYPE_LINK_UP) {
James Smartae9e28f2017-05-15 15:20:51 -07005214 phba->link_flag &= ~(LS_MDS_LINK_DOWN | LS_MDS_LOOPBACK);
5215
5216 switch (phba->sli4_hba.link_state.status) {
5217 case LPFC_FC_LA_TYPE_MDS_LINK_DOWN:
5218 phba->link_flag |= LS_MDS_LINK_DOWN;
5219 break;
5220 case LPFC_FC_LA_TYPE_MDS_LOOPBACK:
5221 phba->link_flag |= LS_MDS_LOOPBACK;
5222 break;
5223 default:
5224 break;
5225 }
5226
James Smart23288b72018-05-04 20:37:53 -07005227 /* Initialize completion status */
James Smart7bdedb32016-07-06 12:36:00 -07005228 mb = &pmb->u.mb;
James Smart23288b72018-05-04 20:37:53 -07005229 mb->mbxStatus = MBX_SUCCESS;
5230
5231 /* Parse port fault information field */
5232 lpfc_sli4_parse_latt_fault(phba, (void *)acqe_fc);
James Smart7bdedb32016-07-06 12:36:00 -07005233
5234 /* Parse and translate link attention fields */
5235 la = (struct lpfc_mbx_read_top *)&pmb->u.mb.un.varReadTop;
5236 la->eventTag = acqe_fc->event_tag;
James Smart7bdedb32016-07-06 12:36:00 -07005237
James Smartaeb3c812017-04-21 16:05:02 -07005238 if (phba->sli4_hba.link_state.status ==
5239 LPFC_FC_LA_TYPE_UNEXP_WWPN) {
5240 bf_set(lpfc_mbx_read_top_att_type, la,
5241 LPFC_FC_LA_TYPE_UNEXP_WWPN);
5242 } else {
5243 bf_set(lpfc_mbx_read_top_att_type, la,
5244 LPFC_FC_LA_TYPE_LINK_DOWN);
5245 }
James Smart7bdedb32016-07-06 12:36:00 -07005246 /* Invoke the mailbox command callback function */
5247 lpfc_mbx_cmpl_read_topology(phba, pmb);
5248
5249 return;
5250 }
5251
James Smart70f3c072010-12-15 17:57:33 -05005252 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
5253 if (rc == MBX_NOT_FINISHED)
5254 goto out_free_dmabuf;
5255 return;
5256
5257out_free_dmabuf:
5258 kfree(mp);
5259out_free_pmb:
5260 mempool_free(pmb, phba->mbox_mem_pool);
5261}
5262
5263/**
5264 * lpfc_sli4_async_sli_evt - Process the asynchronous SLI link event
5265 * @phba: pointer to lpfc hba data structure.
5266 * @acqe_fc: pointer to the async SLI completion queue entry.
5267 *
5268 * This routine is to handle the SLI4 asynchronous SLI events.
5269 **/
5270static void
5271lpfc_sli4_async_sli_evt(struct lpfc_hba *phba, struct lpfc_acqe_sli *acqe_sli)
5272{
James Smart4b8bae02012-06-12 13:55:07 -04005273 char port_name;
James Smart8c1312e2012-10-31 14:45:09 -04005274 char message[128];
James Smart4b8bae02012-06-12 13:55:07 -04005275 uint8_t status;
James Smart946727d2015-04-07 15:07:09 -04005276 uint8_t evt_type;
James Smart448193b2015-12-16 18:12:05 -05005277 uint8_t operational = 0;
James Smart946727d2015-04-07 15:07:09 -04005278 struct temp_event temp_event_data;
James Smart4b8bae02012-06-12 13:55:07 -04005279 struct lpfc_acqe_misconfigured_event *misconfigured;
James Smart946727d2015-04-07 15:07:09 -04005280 struct Scsi_Host *shost;
James Smartcd713482018-10-23 13:41:01 -07005281 struct lpfc_vport **vports;
5282 int rc, i;
James Smart4b8bae02012-06-12 13:55:07 -04005283
James Smart946727d2015-04-07 15:07:09 -04005284 evt_type = bf_get(lpfc_trailer_type, acqe_sli);
5285
James Smart448193b2015-12-16 18:12:05 -05005286 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5287 "2901 Async SLI event - Event Data1:x%08x Event Data2:"
5288 "x%08x SLI Event Type:%d\n",
5289 acqe_sli->event_data1, acqe_sli->event_data2,
5290 evt_type);
James Smart4b8bae02012-06-12 13:55:07 -04005291
5292 port_name = phba->Port[0];
5293 if (port_name == 0x00)
5294 port_name = '?'; /* get port name is empty */
5295
James Smart946727d2015-04-07 15:07:09 -04005296 switch (evt_type) {
5297 case LPFC_SLI_EVENT_TYPE_OVER_TEMP:
5298 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
5299 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
5300 temp_event_data.data = (uint32_t)acqe_sli->event_data1;
5301
5302 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5303 "3190 Over Temperature:%d Celsius- Port Name %c\n",
5304 acqe_sli->event_data1, port_name);
5305
James Smart310429e2016-07-06 12:35:54 -07005306 phba->sfp_warning |= LPFC_TRANSGRESSION_HIGH_TEMPERATURE;
James Smart946727d2015-04-07 15:07:09 -04005307 shost = lpfc_shost_from_vport(phba->pport);
5308 fc_host_post_vendor_event(shost, fc_get_event_number(),
5309 sizeof(temp_event_data),
5310 (char *)&temp_event_data,
5311 SCSI_NL_VID_TYPE_PCI
5312 | PCI_VENDOR_ID_EMULEX);
5313 break;
5314 case LPFC_SLI_EVENT_TYPE_NORM_TEMP:
5315 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
5316 temp_event_data.event_code = LPFC_NORMAL_TEMP;
5317 temp_event_data.data = (uint32_t)acqe_sli->event_data1;
5318
5319 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5320 "3191 Normal Temperature:%d Celsius - Port Name %c\n",
5321 acqe_sli->event_data1, port_name);
5322
5323 shost = lpfc_shost_from_vport(phba->pport);
5324 fc_host_post_vendor_event(shost, fc_get_event_number(),
5325 sizeof(temp_event_data),
5326 (char *)&temp_event_data,
5327 SCSI_NL_VID_TYPE_PCI
5328 | PCI_VENDOR_ID_EMULEX);
5329 break;
5330 case LPFC_SLI_EVENT_TYPE_MISCONFIGURED:
5331 misconfigured = (struct lpfc_acqe_misconfigured_event *)
James Smart4b8bae02012-06-12 13:55:07 -04005332 &acqe_sli->event_data1;
5333
James Smart946727d2015-04-07 15:07:09 -04005334 /* fetch the status for this port */
5335 switch (phba->sli4_hba.lnk_info.lnk_no) {
5336 case LPFC_LINK_NUMBER_0:
James Smart448193b2015-12-16 18:12:05 -05005337 status = bf_get(lpfc_sli_misconfigured_port0_state,
5338 &misconfigured->theEvent);
5339 operational = bf_get(lpfc_sli_misconfigured_port0_op,
James Smart4b8bae02012-06-12 13:55:07 -04005340 &misconfigured->theEvent);
James Smart946727d2015-04-07 15:07:09 -04005341 break;
5342 case LPFC_LINK_NUMBER_1:
James Smart448193b2015-12-16 18:12:05 -05005343 status = bf_get(lpfc_sli_misconfigured_port1_state,
5344 &misconfigured->theEvent);
5345 operational = bf_get(lpfc_sli_misconfigured_port1_op,
James Smart4b8bae02012-06-12 13:55:07 -04005346 &misconfigured->theEvent);
James Smart946727d2015-04-07 15:07:09 -04005347 break;
5348 case LPFC_LINK_NUMBER_2:
James Smart448193b2015-12-16 18:12:05 -05005349 status = bf_get(lpfc_sli_misconfigured_port2_state,
5350 &misconfigured->theEvent);
5351 operational = bf_get(lpfc_sli_misconfigured_port2_op,
James Smart4b8bae02012-06-12 13:55:07 -04005352 &misconfigured->theEvent);
James Smart946727d2015-04-07 15:07:09 -04005353 break;
5354 case LPFC_LINK_NUMBER_3:
James Smart448193b2015-12-16 18:12:05 -05005355 status = bf_get(lpfc_sli_misconfigured_port3_state,
5356 &misconfigured->theEvent);
5357 operational = bf_get(lpfc_sli_misconfigured_port3_op,
James Smart4b8bae02012-06-12 13:55:07 -04005358 &misconfigured->theEvent);
James Smart946727d2015-04-07 15:07:09 -04005359 break;
5360 default:
James Smart448193b2015-12-16 18:12:05 -05005361 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5362 "3296 "
5363 "LPFC_SLI_EVENT_TYPE_MISCONFIGURED "
5364 "event: Invalid link %d",
5365 phba->sli4_hba.lnk_info.lnk_no);
5366 return;
James Smart946727d2015-04-07 15:07:09 -04005367 }
James Smart4b8bae02012-06-12 13:55:07 -04005368
James Smart448193b2015-12-16 18:12:05 -05005369 /* Skip if optic state unchanged */
5370 if (phba->sli4_hba.lnk_info.optic_state == status)
5371 return;
5372
James Smart946727d2015-04-07 15:07:09 -04005373 switch (status) {
5374 case LPFC_SLI_EVENT_STATUS_VALID:
James Smart448193b2015-12-16 18:12:05 -05005375 sprintf(message, "Physical Link is functional");
5376 break;
James Smart946727d2015-04-07 15:07:09 -04005377 case LPFC_SLI_EVENT_STATUS_NOT_PRESENT:
5378 sprintf(message, "Optics faulted/incorrectly "
5379 "installed/not installed - Reseat optics, "
5380 "if issue not resolved, replace.");
5381 break;
5382 case LPFC_SLI_EVENT_STATUS_WRONG_TYPE:
5383 sprintf(message,
5384 "Optics of two types installed - Remove one "
5385 "optic or install matching pair of optics.");
5386 break;
5387 case LPFC_SLI_EVENT_STATUS_UNSUPPORTED:
5388 sprintf(message, "Incompatible optics - Replace with "
James Smart292098b2012-09-29 11:31:41 -04005389 "compatible optics for card to function.");
James Smart946727d2015-04-07 15:07:09 -04005390 break;
James Smart448193b2015-12-16 18:12:05 -05005391 case LPFC_SLI_EVENT_STATUS_UNQUALIFIED:
5392 sprintf(message, "Unqualified optics - Replace with "
5393 "Avago optics for Warranty and Technical "
5394 "Support - Link is%s operational",
James Smart2ea259e2017-02-12 13:52:27 -08005395 (operational) ? " not" : "");
James Smart448193b2015-12-16 18:12:05 -05005396 break;
5397 case LPFC_SLI_EVENT_STATUS_UNCERTIFIED:
5398 sprintf(message, "Uncertified optics - Replace with "
5399 "Avago-certified optics to enable link "
5400 "operation - Link is%s operational",
James Smart2ea259e2017-02-12 13:52:27 -08005401 (operational) ? " not" : "");
James Smart448193b2015-12-16 18:12:05 -05005402 break;
James Smart946727d2015-04-07 15:07:09 -04005403 default:
5404 /* firmware is reporting a status we don't know about */
5405 sprintf(message, "Unknown event status x%02x", status);
5406 break;
5407 }
James Smartcd713482018-10-23 13:41:01 -07005408
5409 /* Issue READ_CONFIG mbox command to refresh supported speeds */
5410 rc = lpfc_sli4_read_config(phba);
James Smart3952e912018-10-23 13:41:02 -07005411 if (rc) {
James Smartcd713482018-10-23 13:41:01 -07005412 phba->lmt = 0;
5413 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5414 "3194 Unable to retrieve supported "
James Smart3952e912018-10-23 13:41:02 -07005415 "speeds, rc = 0x%x\n", rc);
James Smartcd713482018-10-23 13:41:01 -07005416 }
5417 vports = lpfc_create_vport_work_array(phba);
5418 if (vports != NULL) {
5419 for (i = 0; i <= phba->max_vports && vports[i] != NULL;
5420 i++) {
5421 shost = lpfc_shost_from_vport(vports[i]);
5422 lpfc_host_supported_speeds_set(shost);
5423 }
5424 }
5425 lpfc_destroy_vport_work_array(phba, vports);
5426
James Smart448193b2015-12-16 18:12:05 -05005427 phba->sli4_hba.lnk_info.optic_state = status;
James Smart946727d2015-04-07 15:07:09 -04005428 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart448193b2015-12-16 18:12:05 -05005429 "3176 Port Name %c %s\n", port_name, message);
James Smart946727d2015-04-07 15:07:09 -04005430 break;
5431 case LPFC_SLI_EVENT_TYPE_REMOTE_DPORT:
5432 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5433 "3192 Remote DPort Test Initiated - "
5434 "Event Data1:x%08x Event Data2: x%08x\n",
5435 acqe_sli->event_data1, acqe_sli->event_data2);
James Smart4b8bae02012-06-12 13:55:07 -04005436 break;
5437 default:
James Smart946727d2015-04-07 15:07:09 -04005438 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5439 "3193 Async SLI event - Event Data1:x%08x Event Data2:"
5440 "x%08x SLI Event Type:%d\n",
5441 acqe_sli->event_data1, acqe_sli->event_data2,
5442 evt_type);
James Smart4b8bae02012-06-12 13:55:07 -04005443 break;
5444 }
James Smart70f3c072010-12-15 17:57:33 -05005445}
5446
5447/**
James Smartfc2b9892010-02-26 14:15:29 -05005448 * lpfc_sli4_perform_vport_cvl - Perform clear virtual link on a vport
5449 * @vport: pointer to vport data structure.
5450 *
5451 * This routine is to perform Clear Virtual Link (CVL) on a vport in
5452 * response to a CVL event.
5453 *
5454 * Return the pointer to the ndlp with the vport if successful, otherwise
5455 * return NULL.
5456 **/
5457static struct lpfc_nodelist *
5458lpfc_sli4_perform_vport_cvl(struct lpfc_vport *vport)
5459{
5460 struct lpfc_nodelist *ndlp;
5461 struct Scsi_Host *shost;
5462 struct lpfc_hba *phba;
5463
5464 if (!vport)
5465 return NULL;
James Smartfc2b9892010-02-26 14:15:29 -05005466 phba = vport->phba;
5467 if (!phba)
5468 return NULL;
James Smart78730cf2010-04-06 15:06:30 -04005469 ndlp = lpfc_findnode_did(vport, Fabric_DID);
5470 if (!ndlp) {
5471 /* Cannot find existing Fabric ndlp, so allocate a new one */
James Smart9d3d3402017-04-21 16:05:00 -07005472 ndlp = lpfc_nlp_init(vport, Fabric_DID);
James Smart78730cf2010-04-06 15:06:30 -04005473 if (!ndlp)
5474 return 0;
James Smart78730cf2010-04-06 15:06:30 -04005475 /* Set the node type */
5476 ndlp->nlp_type |= NLP_FABRIC;
5477 /* Put ndlp onto node list */
5478 lpfc_enqueue_node(vport, ndlp);
5479 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
5480 /* re-setup ndlp without removing from node list */
5481 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
5482 if (!ndlp)
5483 return 0;
5484 }
James Smart63e801c2010-11-20 23:14:19 -05005485 if ((phba->pport->port_state < LPFC_FLOGI) &&
5486 (phba->pport->port_state != LPFC_VPORT_FAILED))
James Smartfc2b9892010-02-26 14:15:29 -05005487 return NULL;
5488 /* If virtual link is not yet instantiated ignore CVL */
James Smart63e801c2010-11-20 23:14:19 -05005489 if ((vport != phba->pport) && (vport->port_state < LPFC_FDISC)
5490 && (vport->port_state != LPFC_VPORT_FAILED))
James Smartfc2b9892010-02-26 14:15:29 -05005491 return NULL;
5492 shost = lpfc_shost_from_vport(vport);
5493 if (!shost)
5494 return NULL;
5495 lpfc_linkdown_port(vport);
5496 lpfc_cleanup_pending_mbox(vport);
5497 spin_lock_irq(shost->host_lock);
5498 vport->fc_flag |= FC_VPORT_CVL_RCVD;
5499 spin_unlock_irq(shost->host_lock);
5500
5501 return ndlp;
5502}
5503
5504/**
5505 * lpfc_sli4_perform_all_vport_cvl - Perform clear virtual link on all vports
5506 * @vport: pointer to lpfc hba data structure.
5507 *
5508 * This routine is to perform Clear Virtual Link (CVL) on all vports in
5509 * response to a FCF dead event.
5510 **/
5511static void
5512lpfc_sli4_perform_all_vport_cvl(struct lpfc_hba *phba)
5513{
5514 struct lpfc_vport **vports;
5515 int i;
5516
5517 vports = lpfc_create_vport_work_array(phba);
5518 if (vports)
5519 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
5520 lpfc_sli4_perform_vport_cvl(vports[i]);
5521 lpfc_destroy_vport_work_array(phba, vports);
5522}
5523
5524/**
James Smart76a95d72010-11-20 23:11:48 -05005525 * lpfc_sli4_async_fip_evt - Process the asynchronous FCoE FIP event
James Smartda0436e2009-05-22 14:51:39 -04005526 * @phba: pointer to lpfc hba data structure.
5527 * @acqe_link: pointer to the async fcoe completion queue entry.
5528 *
5529 * This routine is to handle the SLI4 asynchronous fcoe event.
5530 **/
5531static void
James Smart76a95d72010-11-20 23:11:48 -05005532lpfc_sli4_async_fip_evt(struct lpfc_hba *phba,
James Smart70f3c072010-12-15 17:57:33 -05005533 struct lpfc_acqe_fip *acqe_fip)
James Smartda0436e2009-05-22 14:51:39 -04005534{
James Smart70f3c072010-12-15 17:57:33 -05005535 uint8_t event_type = bf_get(lpfc_trailer_type, acqe_fip);
James Smartda0436e2009-05-22 14:51:39 -04005536 int rc;
James Smart6669f9b2009-10-02 15:16:45 -04005537 struct lpfc_vport *vport;
5538 struct lpfc_nodelist *ndlp;
5539 struct Scsi_Host *shost;
James Smart695a8142010-01-26 23:08:03 -05005540 int active_vlink_present;
5541 struct lpfc_vport **vports;
5542 int i;
James Smartda0436e2009-05-22 14:51:39 -04005543
James Smart70f3c072010-12-15 17:57:33 -05005544 phba->fc_eventTag = acqe_fip->event_tag;
5545 phba->fcoe_eventtag = acqe_fip->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04005546 switch (event_type) {
James Smart70f3c072010-12-15 17:57:33 -05005547 case LPFC_FIP_EVENT_TYPE_NEW_FCF:
5548 case LPFC_FIP_EVENT_TYPE_FCF_PARAM_MOD:
5549 if (event_type == LPFC_FIP_EVENT_TYPE_NEW_FCF)
James Smart999d8132010-03-15 11:24:56 -04005550 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
5551 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04005552 "2546 New FCF event, evt_tag:x%x, "
5553 "index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05005554 acqe_fip->event_tag,
5555 acqe_fip->index);
James Smart999d8132010-03-15 11:24:56 -04005556 else
5557 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP |
5558 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04005559 "2788 FCF param modified event, "
5560 "evt_tag:x%x, index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05005561 acqe_fip->event_tag,
5562 acqe_fip->index);
James Smart38b92ef2010-08-04 16:11:39 -04005563 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smart0c9ab6f2010-02-26 14:15:57 -05005564 /*
5565 * During period of FCF discovery, read the FCF
5566 * table record indexed by the event to update
James Smarta93ff372010-10-22 11:06:08 -04005567 * FCF roundrobin failover eligible FCF bmask.
James Smart0c9ab6f2010-02-26 14:15:57 -05005568 */
5569 lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
5570 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04005571 "2779 Read FCF (x%x) for updating "
5572 "roundrobin FCF failover bmask\n",
James Smart70f3c072010-12-15 17:57:33 -05005573 acqe_fip->index);
5574 rc = lpfc_sli4_read_fcf_rec(phba, acqe_fip->index);
James Smart0c9ab6f2010-02-26 14:15:57 -05005575 }
James Smart38b92ef2010-08-04 16:11:39 -04005576
5577 /* If the FCF discovery is in progress, do nothing. */
James Smart3804dc82010-07-14 15:31:37 -04005578 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04005579 if (phba->hba_flag & FCF_TS_INPROG) {
James Smart38b92ef2010-08-04 16:11:39 -04005580 spin_unlock_irq(&phba->hbalock);
5581 break;
5582 }
5583 /* If fast FCF failover rescan event is pending, do nothing */
James Smart036cad12018-10-23 13:41:06 -07005584 if (phba->fcf.fcf_flag & (FCF_REDISC_EVT | FCF_REDISC_PEND)) {
James Smart38b92ef2010-08-04 16:11:39 -04005585 spin_unlock_irq(&phba->hbalock);
5586 break;
5587 }
5588
James Smartc2b97122013-05-31 17:05:36 -04005589 /* If the FCF has been in discovered state, do nothing. */
5590 if (phba->fcf.fcf_flag & FCF_SCAN_DONE) {
James Smart3804dc82010-07-14 15:31:37 -04005591 spin_unlock_irq(&phba->hbalock);
5592 break;
5593 }
5594 spin_unlock_irq(&phba->hbalock);
James Smart38b92ef2010-08-04 16:11:39 -04005595
James Smart0c9ab6f2010-02-26 14:15:57 -05005596 /* Otherwise, scan the entire FCF table and re-discover SAN */
5597 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04005598 "2770 Start FCF table scan per async FCF "
5599 "event, evt_tag:x%x, index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05005600 acqe_fip->event_tag, acqe_fip->index);
James Smart0c9ab6f2010-02-26 14:15:57 -05005601 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba,
5602 LPFC_FCOE_FCF_GET_FIRST);
James Smartda0436e2009-05-22 14:51:39 -04005603 if (rc)
James Smart0c9ab6f2010-02-26 14:15:57 -05005604 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
5605 "2547 Issue FCF scan read FCF mailbox "
James Smarta93ff372010-10-22 11:06:08 -04005606 "command failed (x%x)\n", rc);
James Smartda0436e2009-05-22 14:51:39 -04005607 break;
5608
James Smart70f3c072010-12-15 17:57:33 -05005609 case LPFC_FIP_EVENT_TYPE_FCF_TABLE_FULL:
James Smartda0436e2009-05-22 14:51:39 -04005610 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte4e74272009-07-19 10:01:38 -04005611 "2548 FCF Table full count 0x%x tag 0x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05005612 bf_get(lpfc_acqe_fip_fcf_count, acqe_fip),
5613 acqe_fip->event_tag);
James Smartda0436e2009-05-22 14:51:39 -04005614 break;
5615
James Smart70f3c072010-12-15 17:57:33 -05005616 case LPFC_FIP_EVENT_TYPE_FCF_DEAD:
James Smart80c17842012-03-01 22:35:45 -05005617 phba->fcoe_cvl_eventtag = acqe_fip->event_tag;
James Smart0c9ab6f2010-02-26 14:15:57 -05005618 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04005619 "2549 FCF (x%x) disconnected from network, "
James Smart70f3c072010-12-15 17:57:33 -05005620 "tag:x%x\n", acqe_fip->index, acqe_fip->event_tag);
James Smart38b92ef2010-08-04 16:11:39 -04005621 /*
5622 * If we are in the middle of FCF failover process, clear
5623 * the corresponding FCF bit in the roundrobin bitmap.
James Smartda0436e2009-05-22 14:51:39 -04005624 */
James Smartfc2b9892010-02-26 14:15:29 -05005625 spin_lock_irq(&phba->hbalock);
James Smarta1cadfe2016-07-06 12:36:02 -07005626 if ((phba->fcf.fcf_flag & FCF_DISCOVERY) &&
5627 (phba->fcf.current_rec.fcf_indx != acqe_fip->index)) {
James Smartfc2b9892010-02-26 14:15:29 -05005628 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05005629 /* Update FLOGI FCF failover eligible FCF bmask */
James Smart70f3c072010-12-15 17:57:33 -05005630 lpfc_sli4_fcf_rr_index_clear(phba, acqe_fip->index);
James Smartfc2b9892010-02-26 14:15:29 -05005631 break;
5632 }
James Smart38b92ef2010-08-04 16:11:39 -04005633 spin_unlock_irq(&phba->hbalock);
5634
5635 /* If the event is not for currently used fcf do nothing */
James Smart70f3c072010-12-15 17:57:33 -05005636 if (phba->fcf.current_rec.fcf_indx != acqe_fip->index)
James Smart38b92ef2010-08-04 16:11:39 -04005637 break;
5638
5639 /*
5640 * Otherwise, request the port to rediscover the entire FCF
5641 * table for a fast recovery from case that the current FCF
5642 * is no longer valid as we are not in the middle of FCF
5643 * failover process already.
5644 */
James Smartc2b97122013-05-31 17:05:36 -04005645 spin_lock_irq(&phba->hbalock);
5646 /* Mark the fast failover process in progress */
5647 phba->fcf.fcf_flag |= FCF_DEAD_DISC;
5648 spin_unlock_irq(&phba->hbalock);
5649
5650 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
5651 "2771 Start FCF fast failover process due to "
5652 "FCF DEAD event: evt_tag:x%x, fcf_index:x%x "
5653 "\n", acqe_fip->event_tag, acqe_fip->index);
5654 rc = lpfc_sli4_redisc_fcf_table(phba);
5655 if (rc) {
5656 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
5657 LOG_DISCOVERY,
Colin Ian King7afc0ce2018-05-03 10:26:12 +01005658 "2772 Issue FCF rediscover mailbox "
James Smartc2b97122013-05-31 17:05:36 -04005659 "command failed, fail through to FCF "
5660 "dead event\n");
5661 spin_lock_irq(&phba->hbalock);
5662 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
5663 spin_unlock_irq(&phba->hbalock);
5664 /*
5665 * Last resort will fail over by treating this
5666 * as a link down to FCF registration.
5667 */
5668 lpfc_sli4_fcf_dead_failthrough(phba);
5669 } else {
5670 /* Reset FCF roundrobin bmask for new discovery */
5671 lpfc_sli4_clear_fcf_rr_bmask(phba);
5672 /*
5673 * Handling fast FCF failover to a DEAD FCF event is
5674 * considered equalivant to receiving CVL to all vports.
5675 */
5676 lpfc_sli4_perform_all_vport_cvl(phba);
5677 }
James Smartda0436e2009-05-22 14:51:39 -04005678 break;
James Smart70f3c072010-12-15 17:57:33 -05005679 case LPFC_FIP_EVENT_TYPE_CVL:
James Smart80c17842012-03-01 22:35:45 -05005680 phba->fcoe_cvl_eventtag = acqe_fip->event_tag;
James Smart0c9ab6f2010-02-26 14:15:57 -05005681 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
James Smart6669f9b2009-10-02 15:16:45 -04005682 "2718 Clear Virtual Link Received for VPI 0x%x"
James Smart70f3c072010-12-15 17:57:33 -05005683 " tag 0x%x\n", acqe_fip->index, acqe_fip->event_tag);
James Smart6d368e52011-05-24 11:44:12 -04005684
James Smart6669f9b2009-10-02 15:16:45 -04005685 vport = lpfc_find_vport_by_vpid(phba,
James Smart5248a742011-07-22 18:37:06 -04005686 acqe_fip->index);
James Smartfc2b9892010-02-26 14:15:29 -05005687 ndlp = lpfc_sli4_perform_vport_cvl(vport);
James Smart6669f9b2009-10-02 15:16:45 -04005688 if (!ndlp)
5689 break;
James Smart695a8142010-01-26 23:08:03 -05005690 active_vlink_present = 0;
5691
5692 vports = lpfc_create_vport_work_array(phba);
5693 if (vports) {
5694 for (i = 0; i <= phba->max_vports && vports[i] != NULL;
5695 i++) {
5696 if ((!(vports[i]->fc_flag &
5697 FC_VPORT_CVL_RCVD)) &&
5698 (vports[i]->port_state > LPFC_FDISC)) {
5699 active_vlink_present = 1;
5700 break;
5701 }
5702 }
5703 lpfc_destroy_vport_work_array(phba, vports);
5704 }
5705
James Smartcc823552015-05-21 13:55:26 -04005706 /*
5707 * Don't re-instantiate if vport is marked for deletion.
5708 * If we are here first then vport_delete is going to wait
5709 * for discovery to complete.
5710 */
5711 if (!(vport->load_flag & FC_UNLOADING) &&
5712 active_vlink_present) {
James Smart695a8142010-01-26 23:08:03 -05005713 /*
5714 * If there are other active VLinks present,
5715 * re-instantiate the Vlink using FDISC.
5716 */
James Smart256ec0d2013-04-17 20:14:58 -04005717 mod_timer(&ndlp->nlp_delayfunc,
5718 jiffies + msecs_to_jiffies(1000));
James Smartfc2b9892010-02-26 14:15:29 -05005719 shost = lpfc_shost_from_vport(vport);
James Smart6669f9b2009-10-02 15:16:45 -04005720 spin_lock_irq(shost->host_lock);
5721 ndlp->nlp_flag |= NLP_DELAY_TMO;
5722 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05005723 ndlp->nlp_last_elscmd = ELS_CMD_FDISC;
5724 vport->port_state = LPFC_FDISC;
5725 } else {
James Smartecfd03c2010-02-12 14:41:27 -05005726 /*
5727 * Otherwise, we request port to rediscover
5728 * the entire FCF table for a fast recovery
5729 * from possible case that the current FCF
James Smart0c9ab6f2010-02-26 14:15:57 -05005730 * is no longer valid if we are not already
5731 * in the FCF failover process.
James Smartecfd03c2010-02-12 14:41:27 -05005732 */
James Smartfc2b9892010-02-26 14:15:29 -05005733 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05005734 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smartfc2b9892010-02-26 14:15:29 -05005735 spin_unlock_irq(&phba->hbalock);
5736 break;
5737 }
5738 /* Mark the fast failover process in progress */
James Smart0c9ab6f2010-02-26 14:15:57 -05005739 phba->fcf.fcf_flag |= FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05005740 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05005741 lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
5742 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04005743 "2773 Start FCF failover per CVL, "
James Smart70f3c072010-12-15 17:57:33 -05005744 "evt_tag:x%x\n", acqe_fip->event_tag);
James Smartecfd03c2010-02-12 14:41:27 -05005745 rc = lpfc_sli4_redisc_fcf_table(phba);
James Smartfc2b9892010-02-26 14:15:29 -05005746 if (rc) {
James Smart0c9ab6f2010-02-26 14:15:57 -05005747 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
5748 LOG_DISCOVERY,
5749 "2774 Issue FCF rediscover "
Colin Ian King7afc0ce2018-05-03 10:26:12 +01005750 "mailbox command failed, "
James Smart0c9ab6f2010-02-26 14:15:57 -05005751 "through to CVL event\n");
James Smartfc2b9892010-02-26 14:15:29 -05005752 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05005753 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05005754 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05005755 /*
5756 * Last resort will be re-try on the
5757 * the current registered FCF entry.
5758 */
5759 lpfc_retry_pport_discovery(phba);
James Smart38b92ef2010-08-04 16:11:39 -04005760 } else
5761 /*
5762 * Reset FCF roundrobin bmask for new
5763 * discovery.
5764 */
James Smart7d791df2011-07-22 18:37:52 -04005765 lpfc_sli4_clear_fcf_rr_bmask(phba);
James Smart6669f9b2009-10-02 15:16:45 -04005766 }
5767 break;
James Smartda0436e2009-05-22 14:51:39 -04005768 default:
5769 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5770 "0288 Unknown FCoE event type 0x%x event tag "
James Smart70f3c072010-12-15 17:57:33 -05005771 "0x%x\n", event_type, acqe_fip->event_tag);
James Smartda0436e2009-05-22 14:51:39 -04005772 break;
5773 }
5774}
5775
5776/**
5777 * lpfc_sli4_async_dcbx_evt - Process the asynchronous dcbx event
5778 * @phba: pointer to lpfc hba data structure.
5779 * @acqe_link: pointer to the async dcbx completion queue entry.
5780 *
5781 * This routine is to handle the SLI4 asynchronous dcbx event.
5782 **/
5783static void
5784lpfc_sli4_async_dcbx_evt(struct lpfc_hba *phba,
5785 struct lpfc_acqe_dcbx *acqe_dcbx)
5786{
James Smart4d9ab992009-10-02 15:16:39 -04005787 phba->fc_eventTag = acqe_dcbx->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04005788 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5789 "0290 The SLI4 DCBX asynchronous event is not "
5790 "handled yet\n");
5791}
5792
5793/**
James Smartb19a0612010-04-06 14:48:51 -04005794 * lpfc_sli4_async_grp5_evt - Process the asynchronous group5 event
5795 * @phba: pointer to lpfc hba data structure.
5796 * @acqe_link: pointer to the async grp5 completion queue entry.
5797 *
5798 * This routine is to handle the SLI4 asynchronous grp5 event. A grp5 event
5799 * is an asynchronous notified of a logical link speed change. The Port
5800 * reports the logical link speed in units of 10Mbps.
5801 **/
5802static void
5803lpfc_sli4_async_grp5_evt(struct lpfc_hba *phba,
5804 struct lpfc_acqe_grp5 *acqe_grp5)
5805{
5806 uint16_t prev_ll_spd;
5807
5808 phba->fc_eventTag = acqe_grp5->event_tag;
5809 phba->fcoe_eventtag = acqe_grp5->event_tag;
5810 prev_ll_spd = phba->sli4_hba.link_state.logical_speed;
5811 phba->sli4_hba.link_state.logical_speed =
James Smart8b68cd52012-09-29 11:32:37 -04005812 (bf_get(lpfc_acqe_grp5_llink_spd, acqe_grp5)) * 10;
James Smartb19a0612010-04-06 14:48:51 -04005813 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5814 "2789 GRP5 Async Event: Updating logical link speed "
James Smart8b68cd52012-09-29 11:32:37 -04005815 "from %dMbps to %dMbps\n", prev_ll_spd,
5816 phba->sli4_hba.link_state.logical_speed);
James Smartb19a0612010-04-06 14:48:51 -04005817}
5818
5819/**
James Smartda0436e2009-05-22 14:51:39 -04005820 * lpfc_sli4_async_event_proc - Process all the pending asynchronous event
5821 * @phba: pointer to lpfc hba data structure.
5822 *
5823 * This routine is invoked by the worker thread to process all the pending
5824 * SLI4 asynchronous events.
5825 **/
5826void lpfc_sli4_async_event_proc(struct lpfc_hba *phba)
5827{
5828 struct lpfc_cq_event *cq_event;
5829
5830 /* First, declare the async event has been handled */
5831 spin_lock_irq(&phba->hbalock);
5832 phba->hba_flag &= ~ASYNC_EVENT;
5833 spin_unlock_irq(&phba->hbalock);
5834 /* Now, handle all the async events */
5835 while (!list_empty(&phba->sli4_hba.sp_asynce_work_queue)) {
5836 /* Get the first event from the head of the event queue */
5837 spin_lock_irq(&phba->hbalock);
5838 list_remove_head(&phba->sli4_hba.sp_asynce_work_queue,
5839 cq_event, struct lpfc_cq_event, list);
5840 spin_unlock_irq(&phba->hbalock);
5841 /* Process the asynchronous event */
5842 switch (bf_get(lpfc_trailer_code, &cq_event->cqe.mcqe_cmpl)) {
5843 case LPFC_TRAILER_CODE_LINK:
5844 lpfc_sli4_async_link_evt(phba,
5845 &cq_event->cqe.acqe_link);
5846 break;
5847 case LPFC_TRAILER_CODE_FCOE:
James Smart70f3c072010-12-15 17:57:33 -05005848 lpfc_sli4_async_fip_evt(phba, &cq_event->cqe.acqe_fip);
James Smartda0436e2009-05-22 14:51:39 -04005849 break;
5850 case LPFC_TRAILER_CODE_DCBX:
5851 lpfc_sli4_async_dcbx_evt(phba,
5852 &cq_event->cqe.acqe_dcbx);
5853 break;
James Smartb19a0612010-04-06 14:48:51 -04005854 case LPFC_TRAILER_CODE_GRP5:
5855 lpfc_sli4_async_grp5_evt(phba,
5856 &cq_event->cqe.acqe_grp5);
5857 break;
James Smart70f3c072010-12-15 17:57:33 -05005858 case LPFC_TRAILER_CODE_FC:
5859 lpfc_sli4_async_fc_evt(phba, &cq_event->cqe.acqe_fc);
5860 break;
5861 case LPFC_TRAILER_CODE_SLI:
5862 lpfc_sli4_async_sli_evt(phba, &cq_event->cqe.acqe_sli);
5863 break;
James Smartda0436e2009-05-22 14:51:39 -04005864 default:
5865 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5866 "1804 Invalid asynchrous event code: "
5867 "x%x\n", bf_get(lpfc_trailer_code,
5868 &cq_event->cqe.mcqe_cmpl));
5869 break;
5870 }
5871 /* Free the completion event processed to the free pool */
5872 lpfc_sli4_cq_event_release(phba, cq_event);
5873 }
5874}
5875
5876/**
James Smartecfd03c2010-02-12 14:41:27 -05005877 * lpfc_sli4_fcf_redisc_event_proc - Process fcf table rediscovery event
5878 * @phba: pointer to lpfc hba data structure.
5879 *
5880 * This routine is invoked by the worker thread to process FCF table
5881 * rediscovery pending completion event.
5882 **/
5883void lpfc_sli4_fcf_redisc_event_proc(struct lpfc_hba *phba)
5884{
5885 int rc;
5886
5887 spin_lock_irq(&phba->hbalock);
5888 /* Clear FCF rediscovery timeout event */
5889 phba->fcf.fcf_flag &= ~FCF_REDISC_EVT;
5890 /* Clear driver fast failover FCF record flag */
5891 phba->fcf.failover_rec.flag = 0;
5892 /* Set state for FCF fast failover */
5893 phba->fcf.fcf_flag |= FCF_REDISC_FOV;
5894 spin_unlock_irq(&phba->hbalock);
5895
5896 /* Scan FCF table from the first entry to re-discover SAN */
James Smart0c9ab6f2010-02-26 14:15:57 -05005897 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04005898 "2777 Start post-quiescent FCF table scan\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05005899 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
James Smartecfd03c2010-02-12 14:41:27 -05005900 if (rc)
James Smart0c9ab6f2010-02-26 14:15:57 -05005901 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
5902 "2747 Issue FCF scan read FCF mailbox "
5903 "command failed 0x%x\n", rc);
James Smartecfd03c2010-02-12 14:41:27 -05005904}
5905
5906/**
James Smartda0436e2009-05-22 14:51:39 -04005907 * lpfc_api_table_setup - Set up per hba pci-device group func api jump table
5908 * @phba: pointer to lpfc hba data structure.
5909 * @dev_grp: The HBA PCI-Device group number.
5910 *
5911 * This routine is invoked to set up the per HBA PCI-Device group function
5912 * API jump table entries.
5913 *
5914 * Return: 0 if success, otherwise -ENODEV
5915 **/
5916int
5917lpfc_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
James Smart5b75da22008-12-04 22:39:35 -05005918{
5919 int rc;
5920
James Smartda0436e2009-05-22 14:51:39 -04005921 /* Set up lpfc PCI-device group */
5922 phba->pci_dev_grp = dev_grp;
James Smart5b75da22008-12-04 22:39:35 -05005923
James Smartda0436e2009-05-22 14:51:39 -04005924 /* The LPFC_PCI_DEV_OC uses SLI4 */
5925 if (dev_grp == LPFC_PCI_DEV_OC)
5926 phba->sli_rev = LPFC_SLI_REV4;
James Smart5b75da22008-12-04 22:39:35 -05005927
James Smartda0436e2009-05-22 14:51:39 -04005928 /* Set up device INIT API function jump table */
5929 rc = lpfc_init_api_table_setup(phba, dev_grp);
5930 if (rc)
5931 return -ENODEV;
5932 /* Set up SCSI API function jump table */
5933 rc = lpfc_scsi_api_table_setup(phba, dev_grp);
5934 if (rc)
5935 return -ENODEV;
5936 /* Set up SLI API function jump table */
5937 rc = lpfc_sli_api_table_setup(phba, dev_grp);
5938 if (rc)
5939 return -ENODEV;
5940 /* Set up MBOX API function jump table */
5941 rc = lpfc_mbox_api_table_setup(phba, dev_grp);
5942 if (rc)
5943 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05005944
James Smartda0436e2009-05-22 14:51:39 -04005945 return 0;
James Smart5b75da22008-12-04 22:39:35 -05005946}
5947
5948/**
James Smart3621a712009-04-06 18:47:14 -04005949 * lpfc_log_intr_mode - Log the active interrupt mode
James Smart5b75da22008-12-04 22:39:35 -05005950 * @phba: pointer to lpfc hba data structure.
5951 * @intr_mode: active interrupt mode adopted.
5952 *
5953 * This routine it invoked to log the currently used active interrupt mode
5954 * to the device.
James Smart3772a992009-05-22 14:50:54 -04005955 **/
5956static void lpfc_log_intr_mode(struct lpfc_hba *phba, uint32_t intr_mode)
James Smart5b75da22008-12-04 22:39:35 -05005957{
5958 switch (intr_mode) {
5959 case 0:
5960 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5961 "0470 Enable INTx interrupt mode.\n");
5962 break;
5963 case 1:
5964 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5965 "0481 Enabled MSI interrupt mode.\n");
5966 break;
5967 case 2:
5968 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5969 "0480 Enabled MSI-X interrupt mode.\n");
5970 break;
5971 default:
5972 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5973 "0482 Illegal interrupt mode.\n");
5974 break;
5975 }
5976 return;
5977}
5978
James Smart5b75da22008-12-04 22:39:35 -05005979/**
James Smart3772a992009-05-22 14:50:54 -04005980 * lpfc_enable_pci_dev - Enable a generic PCI device.
James Smart5b75da22008-12-04 22:39:35 -05005981 * @phba: pointer to lpfc hba data structure.
5982 *
James Smart3772a992009-05-22 14:50:54 -04005983 * This routine is invoked to enable the PCI device that is common to all
5984 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05005985 *
5986 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005987 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04005988 * other values - error
James Smart5b75da22008-12-04 22:39:35 -05005989 **/
James Smart3772a992009-05-22 14:50:54 -04005990static int
5991lpfc_enable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05005992{
James Smart3772a992009-05-22 14:50:54 -04005993 struct pci_dev *pdev;
James Smart5b75da22008-12-04 22:39:35 -05005994
James Smart3772a992009-05-22 14:50:54 -04005995 /* Obtain PCI device reference */
5996 if (!phba->pcidev)
5997 goto out_error;
5998 else
5999 pdev = phba->pcidev;
James Smart3772a992009-05-22 14:50:54 -04006000 /* Enable PCI device */
6001 if (pci_enable_device_mem(pdev))
6002 goto out_error;
6003 /* Request PCI resource for the device */
Johannes Thumshirne0c04832016-06-07 09:44:03 +02006004 if (pci_request_mem_regions(pdev, LPFC_DRIVER_NAME))
James Smart3772a992009-05-22 14:50:54 -04006005 goto out_disable_device;
6006 /* Set up device as PCI master and save state for EEH */
6007 pci_set_master(pdev);
6008 pci_try_set_mwi(pdev);
6009 pci_save_state(pdev);
James Smart5b75da22008-12-04 22:39:35 -05006010
James Smart05580562011-05-24 11:40:48 -04006011 /* PCIe EEH recovery on powerpc platforms needs fundamental reset */
Jon Mason453193e2013-09-11 15:38:13 -07006012 if (pci_is_pcie(pdev))
James Smart05580562011-05-24 11:40:48 -04006013 pdev->needs_freset = 1;
6014
James Smart3772a992009-05-22 14:50:54 -04006015 return 0;
James Smart5b75da22008-12-04 22:39:35 -05006016
James Smart3772a992009-05-22 14:50:54 -04006017out_disable_device:
6018 pci_disable_device(pdev);
6019out_error:
James Smart079b5c92011-08-21 21:48:49 -04006020 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
Johannes Thumshirne0c04832016-06-07 09:44:03 +02006021 "1401 Failed to enable pci device\n");
James Smart3772a992009-05-22 14:50:54 -04006022 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05006023}
6024
6025/**
James Smart3772a992009-05-22 14:50:54 -04006026 * lpfc_disable_pci_dev - Disable a generic PCI device.
James Smart5b75da22008-12-04 22:39:35 -05006027 * @phba: pointer to lpfc hba data structure.
6028 *
James Smart3772a992009-05-22 14:50:54 -04006029 * This routine is invoked to disable the PCI device that is common to all
6030 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05006031 **/
6032static void
James Smart3772a992009-05-22 14:50:54 -04006033lpfc_disable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05006034{
James Smart3772a992009-05-22 14:50:54 -04006035 struct pci_dev *pdev;
James Smart5b75da22008-12-04 22:39:35 -05006036
James Smart3772a992009-05-22 14:50:54 -04006037 /* Obtain PCI device reference */
6038 if (!phba->pcidev)
6039 return;
6040 else
6041 pdev = phba->pcidev;
James Smart3772a992009-05-22 14:50:54 -04006042 /* Release PCI resource and disable PCI device */
Johannes Thumshirne0c04832016-06-07 09:44:03 +02006043 pci_release_mem_regions(pdev);
James Smart3772a992009-05-22 14:50:54 -04006044 pci_disable_device(pdev);
James Smart5b75da22008-12-04 22:39:35 -05006045
6046 return;
6047}
6048
6049/**
James Smart3772a992009-05-22 14:50:54 -04006050 * lpfc_reset_hba - Reset a hba
6051 * @phba: pointer to lpfc hba data structure.
James Smarte59058c2008-08-24 21:49:00 -04006052 *
James Smart3772a992009-05-22 14:50:54 -04006053 * This routine is invoked to reset a hba device. It brings the HBA
6054 * offline, performs a board restart, and then brings the board back
6055 * online. The lpfc_offline calls lpfc_sli_hba_down which will clean up
6056 * on outstanding mailbox commands.
James Smarte59058c2008-08-24 21:49:00 -04006057 **/
James Smart3772a992009-05-22 14:50:54 -04006058void
6059lpfc_reset_hba(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05006060{
James Smart3772a992009-05-22 14:50:54 -04006061 /* If resets are disabled then set error state and return. */
6062 if (!phba->cfg_enable_hba_reset) {
6063 phba->link_state = LPFC_HBA_ERROR;
6064 return;
6065 }
James Smartee620212014-04-04 13:51:53 -04006066 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
6067 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
6068 else
6069 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
James Smart3772a992009-05-22 14:50:54 -04006070 lpfc_offline(phba);
6071 lpfc_sli_brdrestart(phba);
6072 lpfc_online(phba);
6073 lpfc_unblock_mgmt_io(phba);
6074}
dea31012005-04-17 16:05:31 -05006075
James Smart3772a992009-05-22 14:50:54 -04006076/**
James Smart0a96e972011-07-22 18:37:28 -04006077 * lpfc_sli_sriov_nr_virtfn_get - Get the number of sr-iov virtual functions
6078 * @phba: pointer to lpfc hba data structure.
6079 *
6080 * This function enables the PCI SR-IOV virtual functions to a physical
6081 * function. It invokes the PCI SR-IOV api with the @nr_vfn provided to
6082 * enable the number of virtual functions to the physical function. As
6083 * not all devices support SR-IOV, the return code from the pci_enable_sriov()
6084 * API call does not considered as an error condition for most of the device.
6085 **/
6086uint16_t
6087lpfc_sli_sriov_nr_virtfn_get(struct lpfc_hba *phba)
6088{
6089 struct pci_dev *pdev = phba->pcidev;
6090 uint16_t nr_virtfn;
6091 int pos;
6092
James Smart0a96e972011-07-22 18:37:28 -04006093 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
6094 if (pos == 0)
6095 return 0;
6096
6097 pci_read_config_word(pdev, pos + PCI_SRIOV_TOTAL_VF, &nr_virtfn);
6098 return nr_virtfn;
6099}
6100
6101/**
James Smart912e3ac2011-05-24 11:42:11 -04006102 * lpfc_sli_probe_sriov_nr_virtfn - Enable a number of sr-iov virtual functions
6103 * @phba: pointer to lpfc hba data structure.
6104 * @nr_vfn: number of virtual functions to be enabled.
6105 *
6106 * This function enables the PCI SR-IOV virtual functions to a physical
6107 * function. It invokes the PCI SR-IOV api with the @nr_vfn provided to
6108 * enable the number of virtual functions to the physical function. As
6109 * not all devices support SR-IOV, the return code from the pci_enable_sriov()
6110 * API call does not considered as an error condition for most of the device.
6111 **/
6112int
6113lpfc_sli_probe_sriov_nr_virtfn(struct lpfc_hba *phba, int nr_vfn)
6114{
6115 struct pci_dev *pdev = phba->pcidev;
James Smart0a96e972011-07-22 18:37:28 -04006116 uint16_t max_nr_vfn;
James Smart912e3ac2011-05-24 11:42:11 -04006117 int rc;
6118
James Smart0a96e972011-07-22 18:37:28 -04006119 max_nr_vfn = lpfc_sli_sriov_nr_virtfn_get(phba);
6120 if (nr_vfn > max_nr_vfn) {
6121 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6122 "3057 Requested vfs (%d) greater than "
6123 "supported vfs (%d)", nr_vfn, max_nr_vfn);
6124 return -EINVAL;
6125 }
6126
James Smart912e3ac2011-05-24 11:42:11 -04006127 rc = pci_enable_sriov(pdev, nr_vfn);
6128 if (rc) {
6129 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6130 "2806 Failed to enable sriov on this device "
6131 "with vfn number nr_vf:%d, rc:%d\n",
6132 nr_vfn, rc);
6133 } else
6134 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6135 "2807 Successful enable sriov on this device "
6136 "with vfn number nr_vf:%d\n", nr_vfn);
6137 return rc;
6138}
6139
6140/**
James Smart895427b2017-02-12 13:52:30 -08006141 * lpfc_setup_driver_resource_phase1 - Phase1 etup driver internal resources.
James Smart3772a992009-05-22 14:50:54 -04006142 * @phba: pointer to lpfc hba data structure.
6143 *
James Smart895427b2017-02-12 13:52:30 -08006144 * This routine is invoked to set up the driver internal resources before the
6145 * device specific resource setup to support the HBA device it attached to.
James Smart3772a992009-05-22 14:50:54 -04006146 *
6147 * Return codes
James Smart895427b2017-02-12 13:52:30 -08006148 * 0 - successful
6149 * other values - error
James Smart3772a992009-05-22 14:50:54 -04006150 **/
6151static int
James Smart895427b2017-02-12 13:52:30 -08006152lpfc_setup_driver_resource_phase1(struct lpfc_hba *phba)
James Smart3772a992009-05-22 14:50:54 -04006153{
James Smart895427b2017-02-12 13:52:30 -08006154 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05006155
James Smart3772a992009-05-22 14:50:54 -04006156 /*
James Smart895427b2017-02-12 13:52:30 -08006157 * Driver resources common to all SLI revisions
James Smart3772a992009-05-22 14:50:54 -04006158 */
James Smart895427b2017-02-12 13:52:30 -08006159 atomic_set(&phba->fast_event_count, 0);
6160 spin_lock_init(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006161
James Smart895427b2017-02-12 13:52:30 -08006162 /* Initialize ndlp management spinlock */
6163 spin_lock_init(&phba->ndlp_lock);
James Smart3772a992009-05-22 14:50:54 -04006164
James Smart523128e2018-09-10 10:30:46 -07006165 /* Initialize port_list spinlock */
6166 spin_lock_init(&phba->port_list_lock);
James Smart895427b2017-02-12 13:52:30 -08006167 INIT_LIST_HEAD(&phba->port_list);
James Smart523128e2018-09-10 10:30:46 -07006168
James Smart895427b2017-02-12 13:52:30 -08006169 INIT_LIST_HEAD(&phba->work_list);
6170 init_waitqueue_head(&phba->wait_4_mlo_m_q);
6171
6172 /* Initialize the wait queue head for the kernel thread */
6173 init_waitqueue_head(&phba->work_waitq);
6174
6175 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smartf358dd02017-02-12 13:52:34 -08006176 "1403 Protocols supported %s %s %s\n",
James Smart895427b2017-02-12 13:52:30 -08006177 ((phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) ?
6178 "SCSI" : " "),
6179 ((phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) ?
James Smartf358dd02017-02-12 13:52:34 -08006180 "NVME" : " "),
6181 (phba->nvmet_support ? "NVMET" : " "));
James Smart895427b2017-02-12 13:52:30 -08006182
James Smart0794d602019-01-28 11:14:19 -08006183 /* Initialize the IO buffer list used by driver for SLI3 SCSI */
6184 spin_lock_init(&phba->scsi_buf_list_get_lock);
6185 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_get);
6186 spin_lock_init(&phba->scsi_buf_list_put_lock);
6187 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
James Smart895427b2017-02-12 13:52:30 -08006188
6189 /* Initialize the fabric iocb list */
6190 INIT_LIST_HEAD(&phba->fabric_iocb_list);
6191
6192 /* Initialize list to save ELS buffers */
6193 INIT_LIST_HEAD(&phba->elsbuf);
6194
6195 /* Initialize FCF connection rec list */
6196 INIT_LIST_HEAD(&phba->fcf_conn_rec_list);
6197
6198 /* Initialize OAS configuration list */
6199 spin_lock_init(&phba->devicelock);
6200 INIT_LIST_HEAD(&phba->luns);
6201
James Smart3772a992009-05-22 14:50:54 -04006202 /* MBOX heartbeat timer */
Kees Cookf22eb4d2017-09-06 20:24:26 -07006203 timer_setup(&psli->mbox_tmo, lpfc_mbox_timeout, 0);
James Smart3772a992009-05-22 14:50:54 -04006204 /* Fabric block timer */
Kees Cookf22eb4d2017-09-06 20:24:26 -07006205 timer_setup(&phba->fabric_block_timer, lpfc_fabric_block_timeout, 0);
James Smart3772a992009-05-22 14:50:54 -04006206 /* EA polling mode timer */
Kees Cookf22eb4d2017-09-06 20:24:26 -07006207 timer_setup(&phba->eratt_poll, lpfc_poll_eratt, 0);
James Smart895427b2017-02-12 13:52:30 -08006208 /* Heartbeat timer */
Kees Cookf22eb4d2017-09-06 20:24:26 -07006209 timer_setup(&phba->hb_tmofunc, lpfc_hb_timeout, 0);
James Smart895427b2017-02-12 13:52:30 -08006210
James Smart32517fc2019-01-28 11:14:33 -08006211 INIT_DELAYED_WORK(&phba->eq_delay_work, lpfc_hb_eq_delay_work);
6212
James Smart895427b2017-02-12 13:52:30 -08006213 return 0;
6214}
6215
6216/**
6217 * lpfc_sli_driver_resource_setup - Setup driver internal resources for SLI3 dev
6218 * @phba: pointer to lpfc hba data structure.
6219 *
6220 * This routine is invoked to set up the driver internal resources specific to
6221 * support the SLI-3 HBA device it attached to.
6222 *
6223 * Return codes
6224 * 0 - successful
6225 * other values - error
6226 **/
6227static int
6228lpfc_sli_driver_resource_setup(struct lpfc_hba *phba)
6229{
James Smart0794d602019-01-28 11:14:19 -08006230 int rc, entry_sz;
James Smart895427b2017-02-12 13:52:30 -08006231
6232 /*
6233 * Initialize timers used by driver
6234 */
6235
6236 /* FCP polling mode timer */
Kees Cookf22eb4d2017-09-06 20:24:26 -07006237 timer_setup(&phba->fcp_poll_timer, lpfc_poll_timeout, 0);
James Smart3772a992009-05-22 14:50:54 -04006238
6239 /* Host attention work mask setup */
6240 phba->work_ha_mask = (HA_ERATT | HA_MBATT | HA_LATT);
6241 phba->work_ha_mask |= (HA_RXMASK << (LPFC_ELS_RING * 4));
6242
6243 /* Get all the module params for configuring this host */
6244 lpfc_get_cfgparam(phba);
James Smart895427b2017-02-12 13:52:30 -08006245 /* Set up phase-1 common device driver resources */
6246
6247 rc = lpfc_setup_driver_resource_phase1(phba);
6248 if (rc)
6249 return -ENODEV;
6250
James Smart49198b32010-04-06 15:04:33 -04006251 if (phba->pcidev->device == PCI_DEVICE_ID_HORNET) {
6252 phba->menlo_flag |= HBA_MENLO_SUPPORT;
6253 /* check for menlo minimum sg count */
6254 if (phba->cfg_sg_seg_cnt < LPFC_DEFAULT_MENLO_SG_SEG_CNT)
6255 phba->cfg_sg_seg_cnt = LPFC_DEFAULT_MENLO_SG_SEG_CNT;
6256 }
6257
James Smart895427b2017-02-12 13:52:30 -08006258 if (!phba->sli.sli3_ring)
Kees Cook6396bb22018-06-12 14:03:40 -07006259 phba->sli.sli3_ring = kcalloc(LPFC_SLI3_MAX_RING,
6260 sizeof(struct lpfc_sli_ring),
6261 GFP_KERNEL);
James Smart895427b2017-02-12 13:52:30 -08006262 if (!phba->sli.sli3_ring)
James Smart2a76a282012-08-03 12:35:54 -04006263 return -ENOMEM;
6264
James Smart3772a992009-05-22 14:50:54 -04006265 /*
James Smart96f70772013-04-17 20:16:15 -04006266 * Since lpfc_sg_seg_cnt is module parameter, the sg_dma_buf_size
James Smart3772a992009-05-22 14:50:54 -04006267 * used to create the sg_dma_buf_pool must be dynamically calculated.
James Smart3772a992009-05-22 14:50:54 -04006268 */
James Smart3772a992009-05-22 14:50:54 -04006269
James Smart96f70772013-04-17 20:16:15 -04006270 /* Initialize the host templates the configured values. */
James Smart3772a992009-05-22 14:50:54 -04006271 lpfc_vport_template.sg_tablesize = phba->cfg_sg_seg_cnt;
James Smart96418b52017-03-04 09:30:31 -08006272 lpfc_template_no_hr.sg_tablesize = phba->cfg_sg_seg_cnt;
6273 lpfc_template.sg_tablesize = phba->cfg_sg_seg_cnt;
James Smart3772a992009-05-22 14:50:54 -04006274
James Smart0794d602019-01-28 11:14:19 -08006275 if (phba->sli_rev == LPFC_SLI_REV4)
6276 entry_sz = sizeof(struct sli4_sge);
6277 else
6278 entry_sz = sizeof(struct ulp_bde64);
6279
James Smart96f70772013-04-17 20:16:15 -04006280 /* There are going to be 2 reserved BDEs: 1 FCP cmnd + 1 FCP rsp */
6281 if (phba->cfg_enable_bg) {
6282 /*
6283 * The scsi_buf for a T10-DIF I/O will hold the FCP cmnd,
6284 * the FCP rsp, and a BDE for each. Sice we have no control
6285 * over how many protection data segments the SCSI Layer
6286 * will hand us (ie: there could be one for every block
6287 * in the IO), we just allocate enough BDEs to accomidate
6288 * our max amount and we need to limit lpfc_sg_seg_cnt to
6289 * minimize the risk of running out.
6290 */
6291 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
6292 sizeof(struct fcp_rsp) +
James Smart0794d602019-01-28 11:14:19 -08006293 (LPFC_MAX_SG_SEG_CNT * entry_sz);
James Smart96f70772013-04-17 20:16:15 -04006294
6295 if (phba->cfg_sg_seg_cnt > LPFC_MAX_SG_SEG_CNT_DIF)
6296 phba->cfg_sg_seg_cnt = LPFC_MAX_SG_SEG_CNT_DIF;
6297
6298 /* Total BDEs in BPL for scsi_sg_list and scsi_sg_prot_list */
6299 phba->cfg_total_seg_cnt = LPFC_MAX_SG_SEG_CNT;
6300 } else {
6301 /*
6302 * The scsi_buf for a regular I/O will hold the FCP cmnd,
6303 * the FCP rsp, a BDE for each, and a BDE for up to
6304 * cfg_sg_seg_cnt data segments.
6305 */
6306 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
6307 sizeof(struct fcp_rsp) +
James Smart0794d602019-01-28 11:14:19 -08006308 ((phba->cfg_sg_seg_cnt + 2) * entry_sz);
James Smart96f70772013-04-17 20:16:15 -04006309
6310 /* Total BDEs in BPL for scsi_sg_list */
6311 phba->cfg_total_seg_cnt = phba->cfg_sg_seg_cnt + 2;
6312 }
6313
6314 lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_FCP,
6315 "9088 sg_tablesize:%d dmabuf_size:%d total_bde:%d\n",
6316 phba->cfg_sg_seg_cnt, phba->cfg_sg_dma_buf_size,
6317 phba->cfg_total_seg_cnt);
6318
James Smart3772a992009-05-22 14:50:54 -04006319 phba->max_vpi = LPFC_MAX_VPI;
6320 /* This will be set to correct value after config_port mbox */
6321 phba->max_vports = 0;
6322
6323 /*
6324 * Initialize the SLI Layer to run with lpfc HBAs.
6325 */
6326 lpfc_sli_setup(phba);
James Smart895427b2017-02-12 13:52:30 -08006327 lpfc_sli_queue_init(phba);
James Smart3772a992009-05-22 14:50:54 -04006328
6329 /* Allocate device driver memory */
6330 if (lpfc_mem_alloc(phba, BPL_ALIGN_SZ))
6331 return -ENOMEM;
6332
James Smart912e3ac2011-05-24 11:42:11 -04006333 /*
6334 * Enable sr-iov virtual functions if supported and configured
6335 * through the module parameter.
6336 */
6337 if (phba->cfg_sriov_nr_virtfn > 0) {
6338 rc = lpfc_sli_probe_sriov_nr_virtfn(phba,
6339 phba->cfg_sriov_nr_virtfn);
6340 if (rc) {
6341 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6342 "2808 Requested number of SR-IOV "
6343 "virtual functions (%d) is not "
6344 "supported\n",
6345 phba->cfg_sriov_nr_virtfn);
6346 phba->cfg_sriov_nr_virtfn = 0;
6347 }
6348 }
6349
James Smart3772a992009-05-22 14:50:54 -04006350 return 0;
6351}
6352
6353/**
6354 * lpfc_sli_driver_resource_unset - Unset drvr internal resources for SLI3 dev
6355 * @phba: pointer to lpfc hba data structure.
6356 *
6357 * This routine is invoked to unset the driver internal resources set up
6358 * specific for supporting the SLI-3 HBA device it attached to.
6359 **/
6360static void
6361lpfc_sli_driver_resource_unset(struct lpfc_hba *phba)
6362{
6363 /* Free device driver memory allocated */
6364 lpfc_mem_free_all(phba);
6365
6366 return;
6367}
6368
6369/**
James Smartda0436e2009-05-22 14:51:39 -04006370 * lpfc_sli4_driver_resource_setup - Setup drvr internal resources for SLI4 dev
6371 * @phba: pointer to lpfc hba data structure.
6372 *
6373 * This routine is invoked to set up the driver internal resources specific to
6374 * support the SLI-4 HBA device it attached to.
6375 *
6376 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006377 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04006378 * other values - error
6379 **/
6380static int
6381lpfc_sli4_driver_resource_setup(struct lpfc_hba *phba)
6382{
James Smart28baac72010-02-12 14:42:03 -05006383 LPFC_MBOXQ_t *mboxq;
James Smartf358dd02017-02-12 13:52:34 -08006384 MAILBOX_t *mb;
James Smart895427b2017-02-12 13:52:30 -08006385 int rc, i, max_buf_size;
James Smart28baac72010-02-12 14:42:03 -05006386 uint8_t pn_page[LPFC_MAX_SUPPORTED_PAGES] = {0};
6387 struct lpfc_mqe *mqe;
James Smart09294d42013-04-17 20:16:05 -04006388 int longs;
James Smart81e6a632017-11-20 16:00:43 -08006389 int extra;
James Smartf358dd02017-02-12 13:52:34 -08006390 uint64_t wwn;
James Smartb92dc722018-05-24 21:09:01 -07006391 u32 if_type;
6392 u32 if_fam;
James Smartda0436e2009-05-22 14:51:39 -04006393
James Smart895427b2017-02-12 13:52:30 -08006394 phba->sli4_hba.num_present_cpu = lpfc_present_cpu;
James Smart222e9232019-01-28 11:14:35 -08006395 phba->sli4_hba.num_possible_cpu = num_possible_cpus();
James Smart895427b2017-02-12 13:52:30 -08006396 phba->sli4_hba.curr_disp_cpu = 0;
6397
James Smart716d3bc2013-09-06 12:18:28 -04006398 /* Get all the module params for configuring this host */
6399 lpfc_get_cfgparam(phba);
6400
James Smart895427b2017-02-12 13:52:30 -08006401 /* Set up phase-1 common device driver resources */
6402 rc = lpfc_setup_driver_resource_phase1(phba);
6403 if (rc)
6404 return -ENODEV;
6405
James Smartda0436e2009-05-22 14:51:39 -04006406 /* Before proceed, wait for POST done and device ready */
6407 rc = lpfc_sli4_post_status_check(phba);
6408 if (rc)
6409 return -ENODEV;
6410
6411 /*
6412 * Initialize timers used by driver
6413 */
6414
Kees Cookf22eb4d2017-09-06 20:24:26 -07006415 timer_setup(&phba->rrq_tmr, lpfc_rrq_timeout, 0);
James Smartda0436e2009-05-22 14:51:39 -04006416
James Smartecfd03c2010-02-12 14:41:27 -05006417 /* FCF rediscover timer */
Kees Cookf22eb4d2017-09-06 20:24:26 -07006418 timer_setup(&phba->fcf.redisc_wait, lpfc_sli4_fcf_redisc_wait_tmo, 0);
James Smartecfd03c2010-02-12 14:41:27 -05006419
James Smartda0436e2009-05-22 14:51:39 -04006420 /*
James Smart7ad20aa2011-05-24 11:44:28 -04006421 * Control structure for handling external multi-buffer mailbox
6422 * command pass-through.
6423 */
6424 memset((uint8_t *)&phba->mbox_ext_buf_ctx, 0,
6425 sizeof(struct lpfc_mbox_ext_buf_ctx));
6426 INIT_LIST_HEAD(&phba->mbox_ext_buf_ctx.ext_dmabuf_list);
6427
James Smartda0436e2009-05-22 14:51:39 -04006428 phba->max_vpi = LPFC_MAX_VPI;
James Smart67d12732012-08-03 12:36:13 -04006429
James Smartda0436e2009-05-22 14:51:39 -04006430 /* This will be set to correct value after the read_config mbox */
6431 phba->max_vports = 0;
6432
6433 /* Program the default value of vlan_id and fc_map */
6434 phba->valid_vlan = 0;
6435 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
6436 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
6437 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
6438
6439 /*
James Smart2a76a282012-08-03 12:35:54 -04006440 * For SLI4, instead of using ring 0 (LPFC_FCP_RING) for FCP commands
James Smart895427b2017-02-12 13:52:30 -08006441 * we will associate a new ring, for each EQ/CQ/WQ tuple.
6442 * The WQ create will allocate the ring.
James Smart2a76a282012-08-03 12:35:54 -04006443 */
James Smart085c6472010-11-20 23:11:37 -05006444
James Smart09294d42013-04-17 20:16:05 -04006445 /*
James Smart81e6a632017-11-20 16:00:43 -08006446 * 1 for cmd, 1 for rsp, NVME adds an extra one
6447 * for boundary conditions in its max_sgl_segment template.
6448 */
6449 extra = 2;
6450 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
6451 extra++;
6452
6453 /*
James Smart09294d42013-04-17 20:16:05 -04006454 * It doesn't matter what family our adapter is in, we are
6455 * limited to 2 Pages, 512 SGEs, for our SGL.
6456 * There are going to be 2 reserved SGEs: 1 FCP cmnd + 1 FCP rsp
6457 */
6458 max_buf_size = (2 * SLI4_PAGE_SIZE);
James Smart09294d42013-04-17 20:16:05 -04006459
James Smartda0436e2009-05-22 14:51:39 -04006460 /*
James Smart895427b2017-02-12 13:52:30 -08006461 * Since lpfc_sg_seg_cnt is module param, the sg_dma_buf_size
6462 * used to create the sg_dma_buf_pool must be calculated.
James Smart28baac72010-02-12 14:42:03 -05006463 */
James Smartf44ac122018-03-05 12:04:08 -08006464 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED) {
James Smart96f70772013-04-17 20:16:15 -04006465 /*
James Smart895427b2017-02-12 13:52:30 -08006466 * The scsi_buf for a T10-DIF I/O holds the FCP cmnd,
6467 * the FCP rsp, and a SGE. Sice we have no control
6468 * over how many protection segments the SCSI Layer
James Smart96f70772013-04-17 20:16:15 -04006469 * will hand us (ie: there could be one for every block
James Smart895427b2017-02-12 13:52:30 -08006470 * in the IO), just allocate enough SGEs to accomidate
6471 * our max amount and we need to limit lpfc_sg_seg_cnt
6472 * to minimize the risk of running out.
James Smart96f70772013-04-17 20:16:15 -04006473 */
6474 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
James Smart895427b2017-02-12 13:52:30 -08006475 sizeof(struct fcp_rsp) + max_buf_size;
James Smart96f70772013-04-17 20:16:15 -04006476
6477 /* Total SGEs for scsi_sg_list and scsi_sg_prot_list */
6478 phba->cfg_total_seg_cnt = LPFC_MAX_SGL_SEG_CNT;
6479
James Smart5b9e70b2018-09-10 10:30:42 -07006480 /*
6481 * If supporting DIF, reduce the seg count for scsi to
6482 * allow room for the DIF sges.
6483 */
6484 if (phba->cfg_enable_bg &&
6485 phba->cfg_sg_seg_cnt > LPFC_MAX_BG_SLI4_SEG_CNT_DIF)
6486 phba->cfg_scsi_seg_cnt = LPFC_MAX_BG_SLI4_SEG_CNT_DIF;
6487 else
6488 phba->cfg_scsi_seg_cnt = phba->cfg_sg_seg_cnt;
6489
James Smart96f70772013-04-17 20:16:15 -04006490 } else {
6491 /*
James Smart895427b2017-02-12 13:52:30 -08006492 * The scsi_buf for a regular I/O holds the FCP cmnd,
James Smart96f70772013-04-17 20:16:15 -04006493 * the FCP rsp, a SGE for each, and a SGE for up to
6494 * cfg_sg_seg_cnt data segments.
6495 */
6496 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
James Smart895427b2017-02-12 13:52:30 -08006497 sizeof(struct fcp_rsp) +
James Smart81e6a632017-11-20 16:00:43 -08006498 ((phba->cfg_sg_seg_cnt + extra) *
James Smart895427b2017-02-12 13:52:30 -08006499 sizeof(struct sli4_sge));
James Smart96f70772013-04-17 20:16:15 -04006500
6501 /* Total SGEs for scsi_sg_list */
James Smart81e6a632017-11-20 16:00:43 -08006502 phba->cfg_total_seg_cnt = phba->cfg_sg_seg_cnt + extra;
James Smart5b9e70b2018-09-10 10:30:42 -07006503 phba->cfg_scsi_seg_cnt = phba->cfg_sg_seg_cnt;
James Smart895427b2017-02-12 13:52:30 -08006504
James Smart96f70772013-04-17 20:16:15 -04006505 /*
James Smart81e6a632017-11-20 16:00:43 -08006506 * NOTE: if (phba->cfg_sg_seg_cnt + extra) <= 256 we only
James Smart895427b2017-02-12 13:52:30 -08006507 * need to post 1 page for the SGL.
James Smart96f70772013-04-17 20:16:15 -04006508 */
James Smart085c6472010-11-20 23:11:37 -05006509 }
James Smartacd68592012-01-18 16:25:09 -05006510
James Smart5b9e70b2018-09-10 10:30:42 -07006511 /* Limit to LPFC_MAX_NVME_SEG_CNT for NVME. */
6512 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
6513 if (phba->cfg_sg_seg_cnt > LPFC_MAX_NVME_SEG_CNT) {
6514 lpfc_printf_log(phba, KERN_INFO, LOG_NVME | LOG_INIT,
6515 "6300 Reducing NVME sg segment "
6516 "cnt to %d\n",
6517 LPFC_MAX_NVME_SEG_CNT);
6518 phba->cfg_nvme_seg_cnt = LPFC_MAX_NVME_SEG_CNT;
6519 } else
6520 phba->cfg_nvme_seg_cnt = phba->cfg_sg_seg_cnt;
6521 }
6522
James Smart96f70772013-04-17 20:16:15 -04006523 /* Initialize the host templates with the updated values. */
James Smart5b9e70b2018-09-10 10:30:42 -07006524 lpfc_vport_template.sg_tablesize = phba->cfg_scsi_seg_cnt;
6525 lpfc_template.sg_tablesize = phba->cfg_scsi_seg_cnt;
6526 lpfc_template_no_hr.sg_tablesize = phba->cfg_scsi_seg_cnt;
James Smart96f70772013-04-17 20:16:15 -04006527
6528 if (phba->cfg_sg_dma_buf_size <= LPFC_MIN_SG_SLI4_BUF_SZ)
6529 phba->cfg_sg_dma_buf_size = LPFC_MIN_SG_SLI4_BUF_SZ;
6530 else
6531 phba->cfg_sg_dma_buf_size =
6532 SLI4_PAGE_ALIGN(phba->cfg_sg_dma_buf_size);
6533
6534 lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_FCP,
James Smart5b9e70b2018-09-10 10:30:42 -07006535 "9087 sg_seg_cnt:%d dmabuf_size:%d "
6536 "total:%d scsi:%d nvme:%d\n",
James Smart96f70772013-04-17 20:16:15 -04006537 phba->cfg_sg_seg_cnt, phba->cfg_sg_dma_buf_size,
James Smart5b9e70b2018-09-10 10:30:42 -07006538 phba->cfg_total_seg_cnt, phba->cfg_scsi_seg_cnt,
6539 phba->cfg_nvme_seg_cnt);
James Smartda0436e2009-05-22 14:51:39 -04006540
6541 /* Initialize buffer queue management fields */
James Smart895427b2017-02-12 13:52:30 -08006542 INIT_LIST_HEAD(&phba->hbqs[LPFC_ELS_HBQ].hbq_buffer_list);
James Smartda0436e2009-05-22 14:51:39 -04006543 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_sli4_rb_alloc;
6544 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_sli4_rb_free;
6545
6546 /*
6547 * Initialize the SLI Layer to run with lpfc SLI4 HBAs.
6548 */
James Smart895427b2017-02-12 13:52:30 -08006549 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) {
6550 /* Initialize the Abort scsi buffer list used by driver */
6551 spin_lock_init(&phba->sli4_hba.abts_scsi_buf_list_lock);
6552 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
6553 }
6554
6555 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
6556 /* Initialize the Abort nvme buffer list used by driver */
James Smart5e5b5112019-01-28 11:14:22 -08006557 spin_lock_init(&phba->sli4_hba.abts_nvmet_buf_list_lock);
James Smart86c67372017-04-21 16:05:04 -07006558 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_nvmet_ctx_list);
James Smarta8cf5df2017-05-15 15:20:46 -07006559 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_nvmet_io_wait_list);
James Smart79d8c4c2019-05-21 17:48:56 -07006560 spin_lock_init(&phba->sli4_hba.t_active_list_lock);
6561 INIT_LIST_HEAD(&phba->sli4_hba.t_active_ctx_list);
James Smart895427b2017-02-12 13:52:30 -08006562 }
6563
James Smartda0436e2009-05-22 14:51:39 -04006564 /* This abort list used by worker thread */
James Smart895427b2017-02-12 13:52:30 -08006565 spin_lock_init(&phba->sli4_hba.sgl_list_lock);
James Smarta8cf5df2017-05-15 15:20:46 -07006566 spin_lock_init(&phba->sli4_hba.nvmet_io_wait_lock);
James Smartda0436e2009-05-22 14:51:39 -04006567
6568 /*
James Smart6d368e52011-05-24 11:44:12 -04006569 * Initialize driver internal slow-path work queues
James Smartda0436e2009-05-22 14:51:39 -04006570 */
6571
6572 /* Driver internel slow-path CQ Event pool */
6573 INIT_LIST_HEAD(&phba->sli4_hba.sp_cqe_event_pool);
6574 /* Response IOCB work queue list */
James Smart45ed1192009-10-02 15:17:02 -04006575 INIT_LIST_HEAD(&phba->sli4_hba.sp_queue_event);
James Smartda0436e2009-05-22 14:51:39 -04006576 /* Asynchronous event CQ Event work queue list */
6577 INIT_LIST_HEAD(&phba->sli4_hba.sp_asynce_work_queue);
6578 /* Fast-path XRI aborted CQ Event work queue list */
6579 INIT_LIST_HEAD(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
6580 /* Slow-path XRI aborted CQ Event work queue list */
6581 INIT_LIST_HEAD(&phba->sli4_hba.sp_els_xri_aborted_work_queue);
6582 /* Receive queue CQ Event work queue list */
6583 INIT_LIST_HEAD(&phba->sli4_hba.sp_unsol_work_queue);
6584
James Smart6d368e52011-05-24 11:44:12 -04006585 /* Initialize extent block lists. */
6586 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_blk_list);
6587 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_xri_blk_list);
6588 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_vfi_blk_list);
6589 INIT_LIST_HEAD(&phba->lpfc_vpi_blk_list);
6590
James Smartd1f525a2017-04-21 16:04:55 -07006591 /* Initialize mboxq lists. If the early init routines fail
6592 * these lists need to be correctly initialized.
6593 */
6594 INIT_LIST_HEAD(&phba->sli.mboxq);
6595 INIT_LIST_HEAD(&phba->sli.mboxq_cmpl);
6596
James Smart448193b2015-12-16 18:12:05 -05006597 /* initialize optic_state to 0xFF */
6598 phba->sli4_hba.lnk_info.optic_state = 0xff;
6599
James Smartda0436e2009-05-22 14:51:39 -04006600 /* Allocate device driver memory */
6601 rc = lpfc_mem_alloc(phba, SGL_ALIGN_SZ);
6602 if (rc)
6603 return -ENOMEM;
6604
James Smart2fcee4b2010-12-15 17:57:46 -05006605 /* IF Type 2 ports get initialized now. */
James Smart27d6ac02018-02-22 08:18:42 -08006606 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) >=
James Smart2fcee4b2010-12-15 17:57:46 -05006607 LPFC_SLI_INTF_IF_TYPE_2) {
6608 rc = lpfc_pci_function_reset(phba);
James Smart895427b2017-02-12 13:52:30 -08006609 if (unlikely(rc)) {
6610 rc = -ENODEV;
6611 goto out_free_mem;
6612 }
James Smart946727d2015-04-07 15:07:09 -04006613 phba->temp_sensor_support = 1;
James Smart2fcee4b2010-12-15 17:57:46 -05006614 }
6615
James Smartda0436e2009-05-22 14:51:39 -04006616 /* Create the bootstrap mailbox command */
6617 rc = lpfc_create_bootstrap_mbox(phba);
6618 if (unlikely(rc))
6619 goto out_free_mem;
6620
6621 /* Set up the host's endian order with the device. */
6622 rc = lpfc_setup_endian_order(phba);
6623 if (unlikely(rc))
6624 goto out_free_bsmbx;
6625
6626 /* Set up the hba's configuration parameters. */
6627 rc = lpfc_sli4_read_config(phba);
6628 if (unlikely(rc))
6629 goto out_free_bsmbx;
James Smartcff261f2013-12-17 20:29:47 -05006630 rc = lpfc_mem_alloc_active_rrq_pool_s4(phba);
6631 if (unlikely(rc))
6632 goto out_free_bsmbx;
James Smartda0436e2009-05-22 14:51:39 -04006633
James Smart2fcee4b2010-12-15 17:57:46 -05006634 /* IF Type 0 ports get initialized now. */
6635 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
6636 LPFC_SLI_INTF_IF_TYPE_0) {
6637 rc = lpfc_pci_function_reset(phba);
6638 if (unlikely(rc))
6639 goto out_free_bsmbx;
6640 }
James Smartda0436e2009-05-22 14:51:39 -04006641
James Smartcb5172e2010-03-15 11:25:07 -04006642 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
6643 GFP_KERNEL);
6644 if (!mboxq) {
6645 rc = -ENOMEM;
6646 goto out_free_bsmbx;
6647 }
6648
James Smartf358dd02017-02-12 13:52:34 -08006649 /* Check for NVMET being configured */
James Smart895427b2017-02-12 13:52:30 -08006650 phba->nvmet_support = 0;
James Smartf358dd02017-02-12 13:52:34 -08006651 if (lpfc_enable_nvmet_cnt) {
6652
6653 /* First get WWN of HBA instance */
6654 lpfc_read_nv(phba, mboxq);
6655 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6656 if (rc != MBX_SUCCESS) {
6657 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6658 "6016 Mailbox failed , mbxCmd x%x "
6659 "READ_NV, mbxStatus x%x\n",
6660 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
6661 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
James Smartd1f525a2017-04-21 16:04:55 -07006662 mempool_free(mboxq, phba->mbox_mem_pool);
James Smartf358dd02017-02-12 13:52:34 -08006663 rc = -EIO;
6664 goto out_free_bsmbx;
6665 }
6666 mb = &mboxq->u.mb;
6667 memcpy(&wwn, (char *)mb->un.varRDnvp.nodename,
6668 sizeof(uint64_t));
6669 wwn = cpu_to_be64(wwn);
6670 phba->sli4_hba.wwnn.u.name = wwn;
6671 memcpy(&wwn, (char *)mb->un.varRDnvp.portname,
6672 sizeof(uint64_t));
6673 /* wwn is WWPN of HBA instance */
6674 wwn = cpu_to_be64(wwn);
6675 phba->sli4_hba.wwpn.u.name = wwn;
6676
6677 /* Check to see if it matches any module parameter */
6678 for (i = 0; i < lpfc_enable_nvmet_cnt; i++) {
6679 if (wwn == lpfc_enable_nvmet[i]) {
James Smart7d708032017-03-08 14:36:01 -08006680#if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
James Smart3c603be2017-05-15 15:20:44 -07006681 if (lpfc_nvmet_mem_alloc(phba))
6682 break;
6683
6684 phba->nvmet_support = 1; /* a match */
6685
James Smartf358dd02017-02-12 13:52:34 -08006686 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6687 "6017 NVME Target %016llx\n",
6688 wwn);
James Smart7d708032017-03-08 14:36:01 -08006689#else
6690 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6691 "6021 Can't enable NVME Target."
6692 " NVME_TARGET_FC infrastructure"
6693 " is not in kernel\n");
6694#endif
James Smartc4908502019-01-28 11:14:28 -08006695 /* Not supported for NVMET */
6696 phba->cfg_xri_rebalancing = 0;
James Smart3c603be2017-05-15 15:20:44 -07006697 break;
James Smartf358dd02017-02-12 13:52:34 -08006698 }
6699 }
6700 }
James Smart895427b2017-02-12 13:52:30 -08006701
6702 lpfc_nvme_mod_param_dep(phba);
6703
James Smartfedd3b72011-02-16 12:39:24 -05006704 /* Get the Supported Pages if PORT_CAPABILITIES is supported by port. */
James Smartcb5172e2010-03-15 11:25:07 -04006705 lpfc_supported_pages(mboxq);
6706 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smartfedd3b72011-02-16 12:39:24 -05006707 if (!rc) {
6708 mqe = &mboxq->u.mqe;
6709 memcpy(&pn_page[0], ((uint8_t *)&mqe->un.supp_pages.word3),
6710 LPFC_MAX_SUPPORTED_PAGES);
6711 for (i = 0; i < LPFC_MAX_SUPPORTED_PAGES; i++) {
6712 switch (pn_page[i]) {
6713 case LPFC_SLI4_PARAMETERS:
6714 phba->sli4_hba.pc_sli4_params.supported = 1;
6715 break;
6716 default:
6717 break;
6718 }
6719 }
6720 /* Read the port's SLI4 Parameters capabilities if supported. */
6721 if (phba->sli4_hba.pc_sli4_params.supported)
6722 rc = lpfc_pc_sli4_params_get(phba, mboxq);
6723 if (rc) {
6724 mempool_free(mboxq, phba->mbox_mem_pool);
6725 rc = -EIO;
6726 goto out_free_bsmbx;
James Smartcb5172e2010-03-15 11:25:07 -04006727 }
6728 }
James Smart65791f12016-07-06 12:35:56 -07006729
James Smartfedd3b72011-02-16 12:39:24 -05006730 /*
6731 * Get sli4 parameters that override parameters from Port capabilities.
James Smart6d368e52011-05-24 11:44:12 -04006732 * If this call fails, it isn't critical unless the SLI4 parameters come
6733 * back in conflict.
James Smartfedd3b72011-02-16 12:39:24 -05006734 */
James Smart6d368e52011-05-24 11:44:12 -04006735 rc = lpfc_get_sli4_parameters(phba, mboxq);
6736 if (rc) {
James Smartb92dc722018-05-24 21:09:01 -07006737 if_type = bf_get(lpfc_sli_intf_if_type,
6738 &phba->sli4_hba.sli_intf);
6739 if_fam = bf_get(lpfc_sli_intf_sli_family,
6740 &phba->sli4_hba.sli_intf);
James Smart6d368e52011-05-24 11:44:12 -04006741 if (phba->sli4_hba.extents_in_use &&
6742 phba->sli4_hba.rpi_hdrs_in_use) {
6743 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6744 "2999 Unsupported SLI4 Parameters "
6745 "Extents and RPI headers enabled.\n");
James Smartb92dc722018-05-24 21:09:01 -07006746 if (if_type == LPFC_SLI_INTF_IF_TYPE_0 &&
6747 if_fam == LPFC_SLI_INTF_FAMILY_BE2) {
6748 mempool_free(mboxq, phba->mbox_mem_pool);
6749 rc = -EIO;
6750 goto out_free_bsmbx;
6751 }
James Smart6d368e52011-05-24 11:44:12 -04006752 }
James Smartb92dc722018-05-24 21:09:01 -07006753 if (!(if_type == LPFC_SLI_INTF_IF_TYPE_0 &&
6754 if_fam == LPFC_SLI_INTF_FAMILY_BE2)) {
6755 mempool_free(mboxq, phba->mbox_mem_pool);
6756 rc = -EIO;
6757 goto out_free_bsmbx;
6758 }
James Smart6d368e52011-05-24 11:44:12 -04006759 }
James Smart895427b2017-02-12 13:52:30 -08006760
James Smartcb5172e2010-03-15 11:25:07 -04006761 mempool_free(mboxq, phba->mbox_mem_pool);
James Smart1ba981f2014-02-20 09:56:45 -05006762
6763 /* Verify OAS is supported */
6764 lpfc_sli4_oas_verify(phba);
James Smart1ba981f2014-02-20 09:56:45 -05006765
James Smartd2cc9bc2018-09-10 10:30:50 -07006766 /* Verify RAS support on adapter */
6767 lpfc_sli4_ras_init(phba);
6768
James Smart5350d872011-10-10 21:33:49 -04006769 /* Verify all the SLI4 queues */
6770 rc = lpfc_sli4_queue_verify(phba);
James Smartda0436e2009-05-22 14:51:39 -04006771 if (rc)
6772 goto out_free_bsmbx;
6773
6774 /* Create driver internal CQE event pool */
6775 rc = lpfc_sli4_cq_event_pool_create(phba);
6776 if (rc)
James Smart5350d872011-10-10 21:33:49 -04006777 goto out_free_bsmbx;
James Smartda0436e2009-05-22 14:51:39 -04006778
James Smart8a9d2e82012-05-09 21:16:12 -04006779 /* Initialize sgl lists per host */
6780 lpfc_init_sgl_list(phba);
6781
6782 /* Allocate and initialize active sgl array */
James Smartda0436e2009-05-22 14:51:39 -04006783 rc = lpfc_init_active_sgl_array(phba);
6784 if (rc) {
6785 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6786 "1430 Failed to initialize sgl list.\n");
James Smart8a9d2e82012-05-09 21:16:12 -04006787 goto out_destroy_cq_event_pool;
James Smartda0436e2009-05-22 14:51:39 -04006788 }
James Smartda0436e2009-05-22 14:51:39 -04006789 rc = lpfc_sli4_init_rpi_hdrs(phba);
6790 if (rc) {
6791 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6792 "1432 Failed to initialize rpi headers.\n");
6793 goto out_free_active_sgl;
6794 }
6795
James Smarta93ff372010-10-22 11:06:08 -04006796 /* Allocate eligible FCF bmask memory for FCF roundrobin failover */
James Smart0c9ab6f2010-02-26 14:15:57 -05006797 longs = (LPFC_SLI4_FCF_TBL_INDX_MAX + BITS_PER_LONG - 1)/BITS_PER_LONG;
Kees Cook6396bb22018-06-12 14:03:40 -07006798 phba->fcf.fcf_rr_bmask = kcalloc(longs, sizeof(unsigned long),
James Smart0c9ab6f2010-02-26 14:15:57 -05006799 GFP_KERNEL);
6800 if (!phba->fcf.fcf_rr_bmask) {
6801 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6802 "2759 Failed allocate memory for FCF round "
6803 "robin failover bmask\n");
James Smart05580562011-05-24 11:40:48 -04006804 rc = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -05006805 goto out_remove_rpi_hdrs;
6806 }
6807
James Smart6a828b02019-01-28 11:14:31 -08006808 phba->sli4_hba.hba_eq_hdl = kcalloc(phba->cfg_irq_chann,
James Smartcdb42be2019-01-28 11:14:21 -08006809 sizeof(struct lpfc_hba_eq_hdl),
6810 GFP_KERNEL);
James Smart895427b2017-02-12 13:52:30 -08006811 if (!phba->sli4_hba.hba_eq_hdl) {
James Smart67d12732012-08-03 12:36:13 -04006812 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6813 "2572 Failed allocate memory for "
6814 "fast-path per-EQ handle array\n");
6815 rc = -ENOMEM;
6816 goto out_free_fcf_rr_bmask;
James Smartda0436e2009-05-22 14:51:39 -04006817 }
6818
James Smart222e9232019-01-28 11:14:35 -08006819 phba->sli4_hba.cpu_map = kcalloc(phba->sli4_hba.num_possible_cpu,
James Smart895427b2017-02-12 13:52:30 -08006820 sizeof(struct lpfc_vector_map_info),
6821 GFP_KERNEL);
James Smart7bb03bb2013-04-17 20:19:16 -04006822 if (!phba->sli4_hba.cpu_map) {
6823 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6824 "3327 Failed allocate memory for msi-x "
6825 "interrupt vector mapping\n");
6826 rc = -ENOMEM;
James Smart895427b2017-02-12 13:52:30 -08006827 goto out_free_hba_eq_hdl;
James Smart7bb03bb2013-04-17 20:19:16 -04006828 }
James Smartb246de12013-05-31 17:03:07 -04006829
James Smart32517fc2019-01-28 11:14:33 -08006830 phba->sli4_hba.eq_info = alloc_percpu(struct lpfc_eq_intr_info);
6831 if (!phba->sli4_hba.eq_info) {
6832 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6833 "3321 Failed allocation for per_cpu stats\n");
6834 rc = -ENOMEM;
6835 goto out_free_hba_cpu_map;
6836 }
James Smart912e3ac2011-05-24 11:42:11 -04006837 /*
6838 * Enable sr-iov virtual functions if supported and configured
6839 * through the module parameter.
6840 */
6841 if (phba->cfg_sriov_nr_virtfn > 0) {
6842 rc = lpfc_sli_probe_sriov_nr_virtfn(phba,
6843 phba->cfg_sriov_nr_virtfn);
6844 if (rc) {
6845 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6846 "3020 Requested number of SR-IOV "
6847 "virtual functions (%d) is not "
6848 "supported\n",
6849 phba->cfg_sriov_nr_virtfn);
6850 phba->cfg_sriov_nr_virtfn = 0;
6851 }
6852 }
6853
James Smart5248a742011-07-22 18:37:06 -04006854 return 0;
James Smartda0436e2009-05-22 14:51:39 -04006855
James Smart32517fc2019-01-28 11:14:33 -08006856out_free_hba_cpu_map:
6857 kfree(phba->sli4_hba.cpu_map);
James Smart895427b2017-02-12 13:52:30 -08006858out_free_hba_eq_hdl:
6859 kfree(phba->sli4_hba.hba_eq_hdl);
James Smart0c9ab6f2010-02-26 14:15:57 -05006860out_free_fcf_rr_bmask:
6861 kfree(phba->fcf.fcf_rr_bmask);
James Smartda0436e2009-05-22 14:51:39 -04006862out_remove_rpi_hdrs:
6863 lpfc_sli4_remove_rpi_hdrs(phba);
6864out_free_active_sgl:
6865 lpfc_free_active_sgl(phba);
James Smartda0436e2009-05-22 14:51:39 -04006866out_destroy_cq_event_pool:
6867 lpfc_sli4_cq_event_pool_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04006868out_free_bsmbx:
6869 lpfc_destroy_bootstrap_mbox(phba);
6870out_free_mem:
6871 lpfc_mem_free(phba);
6872 return rc;
6873}
6874
6875/**
6876 * lpfc_sli4_driver_resource_unset - Unset drvr internal resources for SLI4 dev
6877 * @phba: pointer to lpfc hba data structure.
6878 *
6879 * This routine is invoked to unset the driver internal resources set up
6880 * specific for supporting the SLI-4 HBA device it attached to.
6881 **/
6882static void
6883lpfc_sli4_driver_resource_unset(struct lpfc_hba *phba)
6884{
6885 struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
6886
James Smart32517fc2019-01-28 11:14:33 -08006887 free_percpu(phba->sli4_hba.eq_info);
6888
James Smart7bb03bb2013-04-17 20:19:16 -04006889 /* Free memory allocated for msi-x interrupt vector to CPU mapping */
6890 kfree(phba->sli4_hba.cpu_map);
James Smart222e9232019-01-28 11:14:35 -08006891 phba->sli4_hba.num_possible_cpu = 0;
James Smart7bb03bb2013-04-17 20:19:16 -04006892 phba->sli4_hba.num_present_cpu = 0;
James Smart76fd07a2014-02-20 09:57:18 -05006893 phba->sli4_hba.curr_disp_cpu = 0;
James Smart7bb03bb2013-04-17 20:19:16 -04006894
James Smartda0436e2009-05-22 14:51:39 -04006895 /* Free memory allocated for fast-path work queue handles */
James Smart895427b2017-02-12 13:52:30 -08006896 kfree(phba->sli4_hba.hba_eq_hdl);
James Smartda0436e2009-05-22 14:51:39 -04006897
6898 /* Free the allocated rpi headers. */
6899 lpfc_sli4_remove_rpi_hdrs(phba);
James Smartd11e31d2009-06-10 17:23:06 -04006900 lpfc_sli4_remove_rpis(phba);
James Smartda0436e2009-05-22 14:51:39 -04006901
James Smart0c9ab6f2010-02-26 14:15:57 -05006902 /* Free eligible FCF index bmask */
6903 kfree(phba->fcf.fcf_rr_bmask);
6904
James Smartda0436e2009-05-22 14:51:39 -04006905 /* Free the ELS sgl list */
6906 lpfc_free_active_sgl(phba);
James Smart8a9d2e82012-05-09 21:16:12 -04006907 lpfc_free_els_sgl_list(phba);
James Smartf358dd02017-02-12 13:52:34 -08006908 lpfc_free_nvmet_sgl_list(phba);
James Smartda0436e2009-05-22 14:51:39 -04006909
James Smartda0436e2009-05-22 14:51:39 -04006910 /* Free the completion queue EQ event pool */
6911 lpfc_sli4_cq_event_release_all(phba);
6912 lpfc_sli4_cq_event_pool_destroy(phba);
6913
James Smart6d368e52011-05-24 11:44:12 -04006914 /* Release resource identifiers. */
6915 lpfc_sli4_dealloc_resource_identifiers(phba);
6916
James Smartda0436e2009-05-22 14:51:39 -04006917 /* Free the bsmbx region. */
6918 lpfc_destroy_bootstrap_mbox(phba);
6919
6920 /* Free the SLI Layer memory with SLI4 HBAs */
6921 lpfc_mem_free_all(phba);
6922
6923 /* Free the current connect table */
6924 list_for_each_entry_safe(conn_entry, next_conn_entry,
James Smart4d9ab992009-10-02 15:16:39 -04006925 &phba->fcf_conn_rec_list, list) {
6926 list_del_init(&conn_entry->list);
James Smartda0436e2009-05-22 14:51:39 -04006927 kfree(conn_entry);
James Smart4d9ab992009-10-02 15:16:39 -04006928 }
James Smartda0436e2009-05-22 14:51:39 -04006929
6930 return;
6931}
6932
6933/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006934 * lpfc_init_api_table_setup - Set up init api function jump table
James Smart3772a992009-05-22 14:50:54 -04006935 * @phba: The hba struct for which this call is being executed.
6936 * @dev_grp: The HBA PCI-Device group number.
6937 *
6938 * This routine sets up the device INIT interface API function jump table
6939 * in @phba struct.
6940 *
6941 * Returns: 0 - success, -ENODEV - failure.
6942 **/
6943int
6944lpfc_init_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
6945{
James Smart84d1b002010-02-12 14:42:33 -05006946 phba->lpfc_hba_init_link = lpfc_hba_init_link;
6947 phba->lpfc_hba_down_link = lpfc_hba_down_link;
James Smart7f860592011-03-11 16:05:52 -05006948 phba->lpfc_selective_reset = lpfc_selective_reset;
James Smart3772a992009-05-22 14:50:54 -04006949 switch (dev_grp) {
6950 case LPFC_PCI_DEV_LP:
6951 phba->lpfc_hba_down_post = lpfc_hba_down_post_s3;
6952 phba->lpfc_handle_eratt = lpfc_handle_eratt_s3;
6953 phba->lpfc_stop_port = lpfc_stop_port_s3;
6954 break;
James Smartda0436e2009-05-22 14:51:39 -04006955 case LPFC_PCI_DEV_OC:
6956 phba->lpfc_hba_down_post = lpfc_hba_down_post_s4;
6957 phba->lpfc_handle_eratt = lpfc_handle_eratt_s4;
6958 phba->lpfc_stop_port = lpfc_stop_port_s4;
6959 break;
James Smart3772a992009-05-22 14:50:54 -04006960 default:
6961 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6962 "1431 Invalid HBA PCI-device group: 0x%x\n",
6963 dev_grp);
6964 return -ENODEV;
6965 break;
6966 }
6967 return 0;
6968}
6969
6970/**
James Smart3772a992009-05-22 14:50:54 -04006971 * lpfc_setup_driver_resource_phase2 - Phase2 setup driver internal resources.
6972 * @phba: pointer to lpfc hba data structure.
6973 *
6974 * This routine is invoked to set up the driver internal resources after the
6975 * device specific resource setup to support the HBA device it attached to.
6976 *
6977 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006978 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04006979 * other values - error
6980 **/
6981static int
6982lpfc_setup_driver_resource_phase2(struct lpfc_hba *phba)
6983{
6984 int error;
6985
6986 /* Startup the kernel thread for this host adapter. */
6987 phba->worker_thread = kthread_run(lpfc_do_work, phba,
6988 "lpfc_worker_%d", phba->brd_no);
6989 if (IS_ERR(phba->worker_thread)) {
6990 error = PTR_ERR(phba->worker_thread);
6991 return error;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05006992 }
6993
James Smart0cdb84e2018-04-09 14:24:26 -07006994 /* The lpfc_wq workqueue for deferred irq use, is only used for SLI4 */
6995 if (phba->sli_rev == LPFC_SLI_REV4)
6996 phba->wq = alloc_workqueue("lpfc_wq", WQ_MEM_RECLAIM, 0);
6997 else
6998 phba->wq = NULL;
Dick Kennedyf485c182017-09-29 17:34:34 -07006999
James Smart3772a992009-05-22 14:50:54 -04007000 return 0;
7001}
7002
7003/**
7004 * lpfc_unset_driver_resource_phase2 - Phase2 unset driver internal resources.
7005 * @phba: pointer to lpfc hba data structure.
7006 *
7007 * This routine is invoked to unset the driver internal resources set up after
7008 * the device specific resource setup for supporting the HBA device it
7009 * attached to.
7010 **/
7011static void
7012lpfc_unset_driver_resource_phase2(struct lpfc_hba *phba)
7013{
Dick Kennedyf485c182017-09-29 17:34:34 -07007014 if (phba->wq) {
7015 flush_workqueue(phba->wq);
7016 destroy_workqueue(phba->wq);
7017 phba->wq = NULL;
7018 }
7019
James Smart3772a992009-05-22 14:50:54 -04007020 /* Stop kernel worker thread */
James Smart0cdb84e2018-04-09 14:24:26 -07007021 if (phba->worker_thread)
7022 kthread_stop(phba->worker_thread);
James Smart3772a992009-05-22 14:50:54 -04007023}
7024
7025/**
7026 * lpfc_free_iocb_list - Free iocb list.
7027 * @phba: pointer to lpfc hba data structure.
7028 *
7029 * This routine is invoked to free the driver's IOCB list and memory.
7030 **/
James Smart6c621a22017-05-15 15:20:45 -07007031void
James Smart3772a992009-05-22 14:50:54 -04007032lpfc_free_iocb_list(struct lpfc_hba *phba)
7033{
7034 struct lpfc_iocbq *iocbq_entry = NULL, *iocbq_next = NULL;
7035
7036 spin_lock_irq(&phba->hbalock);
7037 list_for_each_entry_safe(iocbq_entry, iocbq_next,
7038 &phba->lpfc_iocb_list, list) {
7039 list_del(&iocbq_entry->list);
7040 kfree(iocbq_entry);
7041 phba->total_iocbq_bufs--;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05007042 }
James Smart3772a992009-05-22 14:50:54 -04007043 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007044
James Smart3772a992009-05-22 14:50:54 -04007045 return;
7046}
dea31012005-04-17 16:05:31 -05007047
James Smart3772a992009-05-22 14:50:54 -04007048/**
7049 * lpfc_init_iocb_list - Allocate and initialize iocb list.
7050 * @phba: pointer to lpfc hba data structure.
7051 *
7052 * This routine is invoked to allocate and initizlize the driver's IOCB
7053 * list and set up the IOCB tag array accordingly.
7054 *
7055 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007056 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04007057 * other values - error
7058 **/
James Smart6c621a22017-05-15 15:20:45 -07007059int
James Smart3772a992009-05-22 14:50:54 -04007060lpfc_init_iocb_list(struct lpfc_hba *phba, int iocb_count)
7061{
7062 struct lpfc_iocbq *iocbq_entry = NULL;
7063 uint16_t iotag;
7064 int i;
dea31012005-04-17 16:05:31 -05007065
7066 /* Initialize and populate the iocb list per host. */
7067 INIT_LIST_HEAD(&phba->lpfc_iocb_list);
James Smart3772a992009-05-22 14:50:54 -04007068 for (i = 0; i < iocb_count; i++) {
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07007069 iocbq_entry = kzalloc(sizeof(struct lpfc_iocbq), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05007070 if (iocbq_entry == NULL) {
7071 printk(KERN_ERR "%s: only allocated %d iocbs of "
7072 "expected %d count. Unloading driver.\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07007073 __func__, i, LPFC_IOCB_LIST_CNT);
dea31012005-04-17 16:05:31 -05007074 goto out_free_iocbq;
7075 }
7076
James Bottomley604a3e32005-10-29 10:28:33 -05007077 iotag = lpfc_sli_next_iotag(phba, iocbq_entry);
7078 if (iotag == 0) {
James Smart3772a992009-05-22 14:50:54 -04007079 kfree(iocbq_entry);
James Bottomley604a3e32005-10-29 10:28:33 -05007080 printk(KERN_ERR "%s: failed to allocate IOTAG. "
James Smart3772a992009-05-22 14:50:54 -04007081 "Unloading driver.\n", __func__);
James Bottomley604a3e32005-10-29 10:28:33 -05007082 goto out_free_iocbq;
7083 }
James Smart6d368e52011-05-24 11:44:12 -04007084 iocbq_entry->sli4_lxritag = NO_XRI;
James Smart3772a992009-05-22 14:50:54 -04007085 iocbq_entry->sli4_xritag = NO_XRI;
James Smart2e0fef82007-06-17 19:56:36 -05007086
7087 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007088 list_add(&iocbq_entry->list, &phba->lpfc_iocb_list);
7089 phba->total_iocbq_bufs++;
James Smart2e0fef82007-06-17 19:56:36 -05007090 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007091 }
7092
James Smart3772a992009-05-22 14:50:54 -04007093 return 0;
7094
7095out_free_iocbq:
7096 lpfc_free_iocb_list(phba);
7097
7098 return -ENOMEM;
7099}
7100
7101/**
James Smart8a9d2e82012-05-09 21:16:12 -04007102 * lpfc_free_sgl_list - Free a given sgl list.
James Smartda0436e2009-05-22 14:51:39 -04007103 * @phba: pointer to lpfc hba data structure.
James Smart8a9d2e82012-05-09 21:16:12 -04007104 * @sglq_list: pointer to the head of sgl list.
James Smartda0436e2009-05-22 14:51:39 -04007105 *
James Smart8a9d2e82012-05-09 21:16:12 -04007106 * This routine is invoked to free a give sgl list and memory.
James Smartda0436e2009-05-22 14:51:39 -04007107 **/
James Smart8a9d2e82012-05-09 21:16:12 -04007108void
7109lpfc_free_sgl_list(struct lpfc_hba *phba, struct list_head *sglq_list)
James Smartda0436e2009-05-22 14:51:39 -04007110{
7111 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
James Smart8a9d2e82012-05-09 21:16:12 -04007112
7113 list_for_each_entry_safe(sglq_entry, sglq_next, sglq_list, list) {
7114 list_del(&sglq_entry->list);
7115 lpfc_mbuf_free(phba, sglq_entry->virt, sglq_entry->phys);
7116 kfree(sglq_entry);
7117 }
7118}
7119
7120/**
7121 * lpfc_free_els_sgl_list - Free els sgl list.
7122 * @phba: pointer to lpfc hba data structure.
7123 *
7124 * This routine is invoked to free the driver's els sgl list and memory.
7125 **/
7126static void
7127lpfc_free_els_sgl_list(struct lpfc_hba *phba)
7128{
James Smartda0436e2009-05-22 14:51:39 -04007129 LIST_HEAD(sglq_list);
James Smartda0436e2009-05-22 14:51:39 -04007130
James Smart8a9d2e82012-05-09 21:16:12 -04007131 /* Retrieve all els sgls from driver list */
James Smartda0436e2009-05-22 14:51:39 -04007132 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -08007133 spin_lock(&phba->sli4_hba.sgl_list_lock);
7134 list_splice_init(&phba->sli4_hba.lpfc_els_sgl_list, &sglq_list);
7135 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smartda0436e2009-05-22 14:51:39 -04007136 spin_unlock_irq(&phba->hbalock);
7137
James Smart8a9d2e82012-05-09 21:16:12 -04007138 /* Now free the sgl list */
7139 lpfc_free_sgl_list(phba, &sglq_list);
James Smartda0436e2009-05-22 14:51:39 -04007140}
7141
7142/**
James Smartf358dd02017-02-12 13:52:34 -08007143 * lpfc_free_nvmet_sgl_list - Free nvmet sgl list.
7144 * @phba: pointer to lpfc hba data structure.
7145 *
7146 * This routine is invoked to free the driver's nvmet sgl list and memory.
7147 **/
7148static void
7149lpfc_free_nvmet_sgl_list(struct lpfc_hba *phba)
7150{
7151 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
7152 LIST_HEAD(sglq_list);
7153
7154 /* Retrieve all nvmet sgls from driver list */
7155 spin_lock_irq(&phba->hbalock);
7156 spin_lock(&phba->sli4_hba.sgl_list_lock);
7157 list_splice_init(&phba->sli4_hba.lpfc_nvmet_sgl_list, &sglq_list);
7158 spin_unlock(&phba->sli4_hba.sgl_list_lock);
7159 spin_unlock_irq(&phba->hbalock);
7160
7161 /* Now free the sgl list */
7162 list_for_each_entry_safe(sglq_entry, sglq_next, &sglq_list, list) {
7163 list_del(&sglq_entry->list);
7164 lpfc_nvmet_buf_free(phba, sglq_entry->virt, sglq_entry->phys);
7165 kfree(sglq_entry);
7166 }
Dick Kennedy4b40d022017-08-23 16:55:38 -07007167
7168 /* Update the nvmet_xri_cnt to reflect no current sgls.
7169 * The next initialization cycle sets the count and allocates
7170 * the sgls over again.
7171 */
7172 phba->sli4_hba.nvmet_xri_cnt = 0;
James Smartf358dd02017-02-12 13:52:34 -08007173}
7174
7175/**
James Smartda0436e2009-05-22 14:51:39 -04007176 * lpfc_init_active_sgl_array - Allocate the buf to track active ELS XRIs.
7177 * @phba: pointer to lpfc hba data structure.
7178 *
7179 * This routine is invoked to allocate the driver's active sgl memory.
7180 * This array will hold the sglq_entry's for active IOs.
7181 **/
7182static int
7183lpfc_init_active_sgl_array(struct lpfc_hba *phba)
7184{
7185 int size;
7186 size = sizeof(struct lpfc_sglq *);
7187 size *= phba->sli4_hba.max_cfg_param.max_xri;
7188
7189 phba->sli4_hba.lpfc_sglq_active_list =
7190 kzalloc(size, GFP_KERNEL);
7191 if (!phba->sli4_hba.lpfc_sglq_active_list)
7192 return -ENOMEM;
7193 return 0;
7194}
7195
7196/**
7197 * lpfc_free_active_sgl - Free the buf that tracks active ELS XRIs.
7198 * @phba: pointer to lpfc hba data structure.
7199 *
7200 * This routine is invoked to walk through the array of active sglq entries
7201 * and free all of the resources.
7202 * This is just a place holder for now.
7203 **/
7204static void
7205lpfc_free_active_sgl(struct lpfc_hba *phba)
7206{
7207 kfree(phba->sli4_hba.lpfc_sglq_active_list);
7208}
7209
7210/**
7211 * lpfc_init_sgl_list - Allocate and initialize sgl list.
7212 * @phba: pointer to lpfc hba data structure.
7213 *
7214 * This routine is invoked to allocate and initizlize the driver's sgl
7215 * list and set up the sgl xritag tag array accordingly.
7216 *
James Smartda0436e2009-05-22 14:51:39 -04007217 **/
James Smart8a9d2e82012-05-09 21:16:12 -04007218static void
James Smartda0436e2009-05-22 14:51:39 -04007219lpfc_init_sgl_list(struct lpfc_hba *phba)
7220{
James Smartda0436e2009-05-22 14:51:39 -04007221 /* Initialize and populate the sglq list per host/VF. */
James Smart895427b2017-02-12 13:52:30 -08007222 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_els_sgl_list);
James Smartda0436e2009-05-22 14:51:39 -04007223 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_els_sgl_list);
James Smartf358dd02017-02-12 13:52:34 -08007224 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_nvmet_sgl_list);
James Smart86c67372017-04-21 16:05:04 -07007225 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_nvmet_ctx_list);
James Smartda0436e2009-05-22 14:51:39 -04007226
James Smart8a9d2e82012-05-09 21:16:12 -04007227 /* els xri-sgl book keeping */
7228 phba->sli4_hba.els_xri_cnt = 0;
James Smartda0436e2009-05-22 14:51:39 -04007229
James Smart895427b2017-02-12 13:52:30 -08007230 /* nvme xri-buffer book keeping */
James Smart5e5b5112019-01-28 11:14:22 -08007231 phba->sli4_hba.io_xri_cnt = 0;
James Smartda0436e2009-05-22 14:51:39 -04007232}
7233
7234/**
7235 * lpfc_sli4_init_rpi_hdrs - Post the rpi header memory region to the port
7236 * @phba: pointer to lpfc hba data structure.
7237 *
7238 * This routine is invoked to post rpi header templates to the
James Smart88a2cfb2011-07-22 18:36:33 -04007239 * port for those SLI4 ports that do not support extents. This routine
James Smartda0436e2009-05-22 14:51:39 -04007240 * posts a PAGE_SIZE memory region to the port to hold up to
James Smart88a2cfb2011-07-22 18:36:33 -04007241 * PAGE_SIZE modulo 64 rpi context headers. This is an initialization routine
7242 * and should be called only when interrupts are disabled.
James Smartda0436e2009-05-22 14:51:39 -04007243 *
7244 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007245 * 0 - successful
James Smart88a2cfb2011-07-22 18:36:33 -04007246 * -ERROR - otherwise.
James Smartda0436e2009-05-22 14:51:39 -04007247 **/
7248int
7249lpfc_sli4_init_rpi_hdrs(struct lpfc_hba *phba)
7250{
7251 int rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04007252 struct lpfc_rpi_hdr *rpi_hdr;
7253
7254 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_hdr_list);
James Smartff78d8f2011-12-13 13:21:35 -05007255 if (!phba->sli4_hba.rpi_hdrs_in_use)
James Smart6d368e52011-05-24 11:44:12 -04007256 return rc;
James Smart6d368e52011-05-24 11:44:12 -04007257 if (phba->sli4_hba.extents_in_use)
7258 return -EIO;
James Smartda0436e2009-05-22 14:51:39 -04007259
7260 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
7261 if (!rpi_hdr) {
7262 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7263 "0391 Error during rpi post operation\n");
7264 lpfc_sli4_remove_rpis(phba);
7265 rc = -ENODEV;
7266 }
7267
7268 return rc;
7269}
7270
7271/**
7272 * lpfc_sli4_create_rpi_hdr - Allocate an rpi header memory region
7273 * @phba: pointer to lpfc hba data structure.
7274 *
7275 * This routine is invoked to allocate a single 4KB memory region to
7276 * support rpis and stores them in the phba. This single region
7277 * provides support for up to 64 rpis. The region is used globally
7278 * by the device.
7279 *
7280 * Returns:
7281 * A valid rpi hdr on success.
7282 * A NULL pointer on any failure.
7283 **/
7284struct lpfc_rpi_hdr *
7285lpfc_sli4_create_rpi_hdr(struct lpfc_hba *phba)
7286{
7287 uint16_t rpi_limit, curr_rpi_range;
7288 struct lpfc_dmabuf *dmabuf;
7289 struct lpfc_rpi_hdr *rpi_hdr;
7290
James Smart6d368e52011-05-24 11:44:12 -04007291 /*
7292 * If the SLI4 port supports extents, posting the rpi header isn't
7293 * required. Set the expected maximum count and let the actual value
7294 * get set when extents are fully allocated.
7295 */
7296 if (!phba->sli4_hba.rpi_hdrs_in_use)
7297 return NULL;
7298 if (phba->sli4_hba.extents_in_use)
7299 return NULL;
7300
7301 /* The limit on the logical index is just the max_rpi count. */
James Smart845d9e82017-05-15 15:20:38 -07007302 rpi_limit = phba->sli4_hba.max_cfg_param.max_rpi;
James Smartda0436e2009-05-22 14:51:39 -04007303
7304 spin_lock_irq(&phba->hbalock);
James Smart6d368e52011-05-24 11:44:12 -04007305 /*
7306 * Establish the starting RPI in this header block. The starting
7307 * rpi is normalized to a zero base because the physical rpi is
7308 * port based.
7309 */
James Smart97f2ecf2012-03-01 22:35:23 -05007310 curr_rpi_range = phba->sli4_hba.next_rpi;
James Smartda0436e2009-05-22 14:51:39 -04007311 spin_unlock_irq(&phba->hbalock);
7312
James Smart845d9e82017-05-15 15:20:38 -07007313 /* Reached full RPI range */
7314 if (curr_rpi_range == rpi_limit)
James Smart6d368e52011-05-24 11:44:12 -04007315 return NULL;
James Smart845d9e82017-05-15 15:20:38 -07007316
James Smartda0436e2009-05-22 14:51:39 -04007317 /*
7318 * First allocate the protocol header region for the port. The
7319 * port expects a 4KB DMA-mapped memory region that is 4K aligned.
7320 */
7321 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
7322 if (!dmabuf)
7323 return NULL;
7324
Luis Chamberlain750afb02019-01-04 09:23:09 +01007325 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
7326 LPFC_HDR_TEMPLATE_SIZE,
7327 &dmabuf->phys, GFP_KERNEL);
James Smartda0436e2009-05-22 14:51:39 -04007328 if (!dmabuf->virt) {
7329 rpi_hdr = NULL;
7330 goto err_free_dmabuf;
7331 }
7332
James Smartda0436e2009-05-22 14:51:39 -04007333 if (!IS_ALIGNED(dmabuf->phys, LPFC_HDR_TEMPLATE_SIZE)) {
7334 rpi_hdr = NULL;
7335 goto err_free_coherent;
7336 }
7337
7338 /* Save the rpi header data for cleanup later. */
7339 rpi_hdr = kzalloc(sizeof(struct lpfc_rpi_hdr), GFP_KERNEL);
7340 if (!rpi_hdr)
7341 goto err_free_coherent;
7342
7343 rpi_hdr->dmabuf = dmabuf;
7344 rpi_hdr->len = LPFC_HDR_TEMPLATE_SIZE;
7345 rpi_hdr->page_count = 1;
7346 spin_lock_irq(&phba->hbalock);
James Smart6d368e52011-05-24 11:44:12 -04007347
7348 /* The rpi_hdr stores the logical index only. */
7349 rpi_hdr->start_rpi = curr_rpi_range;
James Smart845d9e82017-05-15 15:20:38 -07007350 rpi_hdr->next_rpi = phba->sli4_hba.next_rpi + LPFC_RPI_HDR_COUNT;
James Smartda0436e2009-05-22 14:51:39 -04007351 list_add_tail(&rpi_hdr->list, &phba->sli4_hba.lpfc_rpi_hdr_list);
7352
James Smartda0436e2009-05-22 14:51:39 -04007353 spin_unlock_irq(&phba->hbalock);
7354 return rpi_hdr;
7355
7356 err_free_coherent:
7357 dma_free_coherent(&phba->pcidev->dev, LPFC_HDR_TEMPLATE_SIZE,
7358 dmabuf->virt, dmabuf->phys);
7359 err_free_dmabuf:
7360 kfree(dmabuf);
7361 return NULL;
7362}
7363
7364/**
7365 * lpfc_sli4_remove_rpi_hdrs - Remove all rpi header memory regions
7366 * @phba: pointer to lpfc hba data structure.
7367 *
7368 * This routine is invoked to remove all memory resources allocated
James Smart6d368e52011-05-24 11:44:12 -04007369 * to support rpis for SLI4 ports not supporting extents. This routine
7370 * presumes the caller has released all rpis consumed by fabric or port
7371 * logins and is prepared to have the header pages removed.
James Smartda0436e2009-05-22 14:51:39 -04007372 **/
7373void
7374lpfc_sli4_remove_rpi_hdrs(struct lpfc_hba *phba)
7375{
7376 struct lpfc_rpi_hdr *rpi_hdr, *next_rpi_hdr;
7377
James Smart6d368e52011-05-24 11:44:12 -04007378 if (!phba->sli4_hba.rpi_hdrs_in_use)
7379 goto exit;
7380
James Smartda0436e2009-05-22 14:51:39 -04007381 list_for_each_entry_safe(rpi_hdr, next_rpi_hdr,
7382 &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
7383 list_del(&rpi_hdr->list);
7384 dma_free_coherent(&phba->pcidev->dev, rpi_hdr->len,
7385 rpi_hdr->dmabuf->virt, rpi_hdr->dmabuf->phys);
7386 kfree(rpi_hdr->dmabuf);
7387 kfree(rpi_hdr);
7388 }
James Smart6d368e52011-05-24 11:44:12 -04007389 exit:
7390 /* There are no rpis available to the port now. */
7391 phba->sli4_hba.next_rpi = 0;
James Smartda0436e2009-05-22 14:51:39 -04007392}
7393
7394/**
James Smart3772a992009-05-22 14:50:54 -04007395 * lpfc_hba_alloc - Allocate driver hba data structure for a device.
7396 * @pdev: pointer to pci device data structure.
7397 *
7398 * This routine is invoked to allocate the driver hba data structure for an
7399 * HBA device. If the allocation is successful, the phba reference to the
7400 * PCI device data structure is set.
7401 *
7402 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007403 * pointer to @phba - successful
James Smart3772a992009-05-22 14:50:54 -04007404 * NULL - error
7405 **/
7406static struct lpfc_hba *
7407lpfc_hba_alloc(struct pci_dev *pdev)
7408{
7409 struct lpfc_hba *phba;
7410
7411 /* Allocate memory for HBA structure */
7412 phba = kzalloc(sizeof(struct lpfc_hba), GFP_KERNEL);
7413 if (!phba) {
Jiri Slabye34ccdf2009-07-13 23:25:54 +02007414 dev_err(&pdev->dev, "failed to allocate hba struct\n");
James Smart3772a992009-05-22 14:50:54 -04007415 return NULL;
7416 }
7417
7418 /* Set reference to PCI device in HBA structure */
7419 phba->pcidev = pdev;
7420
7421 /* Assign an unused board number */
7422 phba->brd_no = lpfc_get_instance();
7423 if (phba->brd_no < 0) {
7424 kfree(phba);
7425 return NULL;
7426 }
James Smart65791f12016-07-06 12:35:56 -07007427 phba->eratt_poll_interval = LPFC_ERATT_POLL_INTERVAL;
James Smart3772a992009-05-22 14:50:54 -04007428
James Smart4fede782010-01-26 23:08:55 -05007429 spin_lock_init(&phba->ct_ev_lock);
James Smartf1c3b0f2009-07-19 10:01:32 -04007430 INIT_LIST_HEAD(&phba->ct_ev_waiters);
7431
James Smart3772a992009-05-22 14:50:54 -04007432 return phba;
7433}
7434
7435/**
7436 * lpfc_hba_free - Free driver hba data structure with a device.
7437 * @phba: pointer to lpfc hba data structure.
7438 *
7439 * This routine is invoked to free the driver hba data structure with an
7440 * HBA device.
7441 **/
7442static void
7443lpfc_hba_free(struct lpfc_hba *phba)
7444{
James Smart5e5b5112019-01-28 11:14:22 -08007445 if (phba->sli_rev == LPFC_SLI_REV4)
7446 kfree(phba->sli4_hba.hdwq);
7447
James Smart3772a992009-05-22 14:50:54 -04007448 /* Release the driver assigned board number */
7449 idr_remove(&lpfc_hba_index, phba->brd_no);
7450
James Smart895427b2017-02-12 13:52:30 -08007451 /* Free memory allocated with sli3 rings */
7452 kfree(phba->sli.sli3_ring);
7453 phba->sli.sli3_ring = NULL;
James Smart2a76a282012-08-03 12:35:54 -04007454
James Smart3772a992009-05-22 14:50:54 -04007455 kfree(phba);
7456 return;
7457}
7458
7459/**
7460 * lpfc_create_shost - Create hba physical port with associated scsi host.
7461 * @phba: pointer to lpfc hba data structure.
7462 *
7463 * This routine is invoked to create HBA physical port and associate a SCSI
7464 * host with it.
7465 *
7466 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007467 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04007468 * other values - error
7469 **/
7470static int
7471lpfc_create_shost(struct lpfc_hba *phba)
7472{
7473 struct lpfc_vport *vport;
7474 struct Scsi_Host *shost;
7475
7476 /* Initialize HBA FC structure */
dea31012005-04-17 16:05:31 -05007477 phba->fc_edtov = FF_DEF_EDTOV;
7478 phba->fc_ratov = FF_DEF_RATOV;
7479 phba->fc_altov = FF_DEF_ALTOV;
7480 phba->fc_arbtov = FF_DEF_ARBTOV;
7481
James Smartd7c47992010-06-08 18:31:54 -04007482 atomic_set(&phba->sdev_cnt, 0);
James Smart3de2a652007-08-02 11:09:59 -04007483 vport = lpfc_create_port(phba, phba->brd_no, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05007484 if (!vport)
James Smart3772a992009-05-22 14:50:54 -04007485 return -ENODEV;
James Smart2e0fef82007-06-17 19:56:36 -05007486
7487 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05007488 phba->pport = vport;
James Smart2ea259e2017-02-12 13:52:27 -08007489
James Smartf358dd02017-02-12 13:52:34 -08007490 if (phba->nvmet_support) {
7491 /* Only 1 vport (pport) will support NVME target */
7492 if (phba->txrdy_payload_pool == NULL) {
Romain Perier771db5c2017-07-06 10:13:05 +02007493 phba->txrdy_payload_pool = dma_pool_create(
7494 "txrdy_pool", &phba->pcidev->dev,
James Smartf358dd02017-02-12 13:52:34 -08007495 TXRDY_PAYLOAD_LEN, 16, 0);
7496 if (phba->txrdy_payload_pool) {
7497 phba->targetport = NULL;
7498 phba->cfg_enable_fc4_type = LPFC_ENABLE_NVME;
7499 lpfc_printf_log(phba, KERN_INFO,
7500 LOG_INIT | LOG_NVME_DISC,
7501 "6076 NVME Target Found\n");
7502 }
7503 }
7504 }
7505
James Smart858c9f62007-06-17 19:56:39 -05007506 lpfc_debugfs_initialize(vport);
James Smart3772a992009-05-22 14:50:54 -04007507 /* Put reference to SCSI host to driver's device private data */
7508 pci_set_drvdata(phba->pcidev, shost);
James Smart2e0fef82007-06-17 19:56:36 -05007509
James Smart4258e982015-12-16 18:11:58 -05007510 /*
7511 * At this point we are fully registered with PSA. In addition,
7512 * any initial discovery should be completed.
7513 */
7514 vport->load_flag |= FC_ALLOW_FDMI;
James Smart8663cbb2016-03-31 14:12:33 -07007515 if (phba->cfg_enable_SmartSAN ||
7516 (phba->cfg_fdmi_on == LPFC_FDMI_SUPPORT)) {
James Smart4258e982015-12-16 18:11:58 -05007517
7518 /* Setup appropriate attribute masks */
7519 vport->fdmi_hba_mask = LPFC_FDMI2_HBA_ATTR;
James Smart8663cbb2016-03-31 14:12:33 -07007520 if (phba->cfg_enable_SmartSAN)
James Smart4258e982015-12-16 18:11:58 -05007521 vport->fdmi_port_mask = LPFC_FDMI2_SMART_ATTR;
7522 else
7523 vport->fdmi_port_mask = LPFC_FDMI2_PORT_ATTR;
7524 }
James Smart3772a992009-05-22 14:50:54 -04007525 return 0;
7526}
dea31012005-04-17 16:05:31 -05007527
James Smart3772a992009-05-22 14:50:54 -04007528/**
7529 * lpfc_destroy_shost - Destroy hba physical port with associated scsi host.
7530 * @phba: pointer to lpfc hba data structure.
7531 *
7532 * This routine is invoked to destroy HBA physical port and the associated
7533 * SCSI host.
7534 **/
7535static void
7536lpfc_destroy_shost(struct lpfc_hba *phba)
7537{
7538 struct lpfc_vport *vport = phba->pport;
James Smart93996272008-08-24 21:50:30 -04007539
James Smart3772a992009-05-22 14:50:54 -04007540 /* Destroy physical port that associated with the SCSI host */
7541 destroy_port(vport);
7542
7543 return;
7544}
7545
7546/**
7547 * lpfc_setup_bg - Setup Block guard structures and debug areas.
7548 * @phba: pointer to lpfc hba data structure.
7549 * @shost: the shost to be used to detect Block guard settings.
7550 *
7551 * This routine sets up the local Block guard protocol settings for @shost.
7552 * This routine also allocates memory for debugging bg buffers.
7553 **/
7554static void
7555lpfc_setup_bg(struct lpfc_hba *phba, struct Scsi_Host *shost)
7556{
James Smartbbeb79b2012-06-12 13:54:27 -04007557 uint32_t old_mask;
7558 uint32_t old_guard;
7559
James Smart3772a992009-05-22 14:50:54 -04007560 int pagecnt = 10;
James Smartb3b98b72016-10-13 15:06:06 -07007561 if (phba->cfg_prot_mask && phba->cfg_prot_guard) {
James Smart3772a992009-05-22 14:50:54 -04007562 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7563 "1478 Registering BlockGuard with the "
7564 "SCSI layer\n");
James Smartbbeb79b2012-06-12 13:54:27 -04007565
James Smartb3b98b72016-10-13 15:06:06 -07007566 old_mask = phba->cfg_prot_mask;
7567 old_guard = phba->cfg_prot_guard;
James Smartbbeb79b2012-06-12 13:54:27 -04007568
7569 /* Only allow supported values */
James Smartb3b98b72016-10-13 15:06:06 -07007570 phba->cfg_prot_mask &= (SHOST_DIF_TYPE1_PROTECTION |
James Smartbbeb79b2012-06-12 13:54:27 -04007571 SHOST_DIX_TYPE0_PROTECTION |
7572 SHOST_DIX_TYPE1_PROTECTION);
James Smartb3b98b72016-10-13 15:06:06 -07007573 phba->cfg_prot_guard &= (SHOST_DIX_GUARD_IP |
7574 SHOST_DIX_GUARD_CRC);
James Smartbbeb79b2012-06-12 13:54:27 -04007575
7576 /* DIF Type 1 protection for profiles AST1/C1 is end to end */
James Smartb3b98b72016-10-13 15:06:06 -07007577 if (phba->cfg_prot_mask == SHOST_DIX_TYPE1_PROTECTION)
7578 phba->cfg_prot_mask |= SHOST_DIF_TYPE1_PROTECTION;
James Smartbbeb79b2012-06-12 13:54:27 -04007579
James Smartb3b98b72016-10-13 15:06:06 -07007580 if (phba->cfg_prot_mask && phba->cfg_prot_guard) {
7581 if ((old_mask != phba->cfg_prot_mask) ||
7582 (old_guard != phba->cfg_prot_guard))
James Smartbbeb79b2012-06-12 13:54:27 -04007583 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7584 "1475 Registering BlockGuard with the "
7585 "SCSI layer: mask %d guard %d\n",
James Smartb3b98b72016-10-13 15:06:06 -07007586 phba->cfg_prot_mask,
7587 phba->cfg_prot_guard);
James Smartbbeb79b2012-06-12 13:54:27 -04007588
James Smartb3b98b72016-10-13 15:06:06 -07007589 scsi_host_set_prot(shost, phba->cfg_prot_mask);
7590 scsi_host_set_guard(shost, phba->cfg_prot_guard);
James Smartbbeb79b2012-06-12 13:54:27 -04007591 } else
7592 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7593 "1479 Not Registering BlockGuard with the SCSI "
7594 "layer, Bad protection parameters: %d %d\n",
7595 old_mask, old_guard);
James Smart98c9ea52007-10-27 13:37:33 -04007596 }
James Smartbbeb79b2012-06-12 13:54:27 -04007597
James Smart81301a92008-12-04 22:39:46 -05007598 if (!_dump_buf_data) {
James Smart81301a92008-12-04 22:39:46 -05007599 while (pagecnt) {
7600 spin_lock_init(&_dump_buf_lock);
7601 _dump_buf_data =
7602 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
7603 if (_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -04007604 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
7605 "9043 BLKGRD: allocated %d pages for "
James Smart3772a992009-05-22 14:50:54 -04007606 "_dump_buf_data at 0x%p\n",
7607 (1 << pagecnt), _dump_buf_data);
James Smart81301a92008-12-04 22:39:46 -05007608 _dump_buf_data_order = pagecnt;
James Smart3772a992009-05-22 14:50:54 -04007609 memset(_dump_buf_data, 0,
7610 ((1 << PAGE_SHIFT) << pagecnt));
James Smart81301a92008-12-04 22:39:46 -05007611 break;
James Smart3772a992009-05-22 14:50:54 -04007612 } else
James Smart81301a92008-12-04 22:39:46 -05007613 --pagecnt;
James Smart81301a92008-12-04 22:39:46 -05007614 }
James Smart81301a92008-12-04 22:39:46 -05007615 if (!_dump_buf_data_order)
James Smart6a9c52c2009-10-02 15:16:51 -04007616 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
7617 "9044 BLKGRD: ERROR unable to allocate "
James Smart3772a992009-05-22 14:50:54 -04007618 "memory for hexdump\n");
7619 } else
James Smart6a9c52c2009-10-02 15:16:51 -04007620 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
7621 "9045 BLKGRD: already allocated _dump_buf_data=0x%p"
James Smart81301a92008-12-04 22:39:46 -05007622 "\n", _dump_buf_data);
James Smart81301a92008-12-04 22:39:46 -05007623 if (!_dump_buf_dif) {
James Smart81301a92008-12-04 22:39:46 -05007624 while (pagecnt) {
7625 _dump_buf_dif =
7626 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
7627 if (_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -04007628 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
7629 "9046 BLKGRD: allocated %d pages for "
James Smart3772a992009-05-22 14:50:54 -04007630 "_dump_buf_dif at 0x%p\n",
7631 (1 << pagecnt), _dump_buf_dif);
James Smart81301a92008-12-04 22:39:46 -05007632 _dump_buf_dif_order = pagecnt;
James Smart3772a992009-05-22 14:50:54 -04007633 memset(_dump_buf_dif, 0,
7634 ((1 << PAGE_SHIFT) << pagecnt));
James Smart81301a92008-12-04 22:39:46 -05007635 break;
James Smart3772a992009-05-22 14:50:54 -04007636 } else
James Smart81301a92008-12-04 22:39:46 -05007637 --pagecnt;
James Smart81301a92008-12-04 22:39:46 -05007638 }
James Smart81301a92008-12-04 22:39:46 -05007639 if (!_dump_buf_dif_order)
James Smart6a9c52c2009-10-02 15:16:51 -04007640 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
7641 "9047 BLKGRD: ERROR unable to allocate "
James Smart3772a992009-05-22 14:50:54 -04007642 "memory for hexdump\n");
7643 } else
James Smart6a9c52c2009-10-02 15:16:51 -04007644 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
7645 "9048 BLKGRD: already allocated _dump_buf_dif=0x%p\n",
James Smart3772a992009-05-22 14:50:54 -04007646 _dump_buf_dif);
7647}
7648
7649/**
7650 * lpfc_post_init_setup - Perform necessary device post initialization setup.
7651 * @phba: pointer to lpfc hba data structure.
7652 *
7653 * This routine is invoked to perform all the necessary post initialization
7654 * setup for the device.
7655 **/
7656static void
7657lpfc_post_init_setup(struct lpfc_hba *phba)
7658{
7659 struct Scsi_Host *shost;
7660 struct lpfc_adapter_event_header adapter_event;
7661
7662 /* Get the default values for Model Name and Description */
7663 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
7664
7665 /*
7666 * hba setup may have changed the hba_queue_depth so we need to
7667 * adjust the value of can_queue.
7668 */
7669 shost = pci_get_drvdata(phba->pcidev);
7670 shost->can_queue = phba->cfg_hba_queue_depth - 10;
7671 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED)
7672 lpfc_setup_bg(phba, shost);
James Smart858c9f62007-06-17 19:56:39 -05007673
7674 lpfc_host_attrib_init(shost);
7675
James Smart2e0fef82007-06-17 19:56:36 -05007676 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
7677 spin_lock_irq(shost->host_lock);
7678 lpfc_poll_start_timer(phba);
7679 spin_unlock_irq(shost->host_lock);
7680 }
James Smart8f6d98d2006-08-01 07:34:00 -04007681
James Smart93996272008-08-24 21:50:30 -04007682 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7683 "0428 Perform SCSI scan\n");
James Smartea2151b2008-09-07 11:52:10 -04007684 /* Send board arrival event to upper layer */
7685 adapter_event.event_type = FC_REG_ADAPTER_EVENT;
7686 adapter_event.subcategory = LPFC_EVENT_ARRIVAL;
7687 fc_host_post_vendor_event(shost, fc_get_event_number(),
James Smart3772a992009-05-22 14:50:54 -04007688 sizeof(adapter_event),
7689 (char *) &adapter_event,
7690 LPFC_NL_VENDOR_ID);
7691 return;
7692}
7693
7694/**
7695 * lpfc_sli_pci_mem_setup - Setup SLI3 HBA PCI memory space.
7696 * @phba: pointer to lpfc hba data structure.
7697 *
7698 * This routine is invoked to set up the PCI device memory space for device
7699 * with SLI-3 interface spec.
7700 *
7701 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007702 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04007703 * other values - error
7704 **/
7705static int
7706lpfc_sli_pci_mem_setup(struct lpfc_hba *phba)
7707{
Christoph Hellwigf30e1bf2018-10-18 15:10:21 +02007708 struct pci_dev *pdev = phba->pcidev;
James Smart3772a992009-05-22 14:50:54 -04007709 unsigned long bar0map_len, bar2map_len;
7710 int i, hbq_count;
7711 void *ptr;
Hannes Reinecke56de8352019-02-18 08:34:19 +01007712 int error;
James Smart3772a992009-05-22 14:50:54 -04007713
Christoph Hellwigf30e1bf2018-10-18 15:10:21 +02007714 if (!pdev)
Hannes Reinecke56de8352019-02-18 08:34:19 +01007715 return -ENODEV;
James Smart3772a992009-05-22 14:50:54 -04007716
7717 /* Set the device DMA mask size */
Hannes Reinecke56de8352019-02-18 08:34:19 +01007718 error = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
7719 if (error)
7720 error = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
7721 if (error)
Christoph Hellwigf30e1bf2018-10-18 15:10:21 +02007722 return error;
Hannes Reinecke56de8352019-02-18 08:34:19 +01007723 error = -ENODEV;
James Smart3772a992009-05-22 14:50:54 -04007724
7725 /* Get the bus address of Bar0 and Bar2 and the number of bytes
7726 * required by each mapping.
7727 */
7728 phba->pci_bar0_map = pci_resource_start(pdev, 0);
7729 bar0map_len = pci_resource_len(pdev, 0);
7730
7731 phba->pci_bar2_map = pci_resource_start(pdev, 2);
7732 bar2map_len = pci_resource_len(pdev, 2);
7733
7734 /* Map HBA SLIM to a kernel virtual address. */
7735 phba->slim_memmap_p = ioremap(phba->pci_bar0_map, bar0map_len);
7736 if (!phba->slim_memmap_p) {
7737 dev_printk(KERN_ERR, &pdev->dev,
7738 "ioremap failed for SLIM memory.\n");
7739 goto out;
7740 }
7741
7742 /* Map HBA Control Registers to a kernel virtual address. */
7743 phba->ctrl_regs_memmap_p = ioremap(phba->pci_bar2_map, bar2map_len);
7744 if (!phba->ctrl_regs_memmap_p) {
7745 dev_printk(KERN_ERR, &pdev->dev,
7746 "ioremap failed for HBA control registers.\n");
7747 goto out_iounmap_slim;
7748 }
7749
7750 /* Allocate memory for SLI-2 structures */
Luis Chamberlain750afb02019-01-04 09:23:09 +01007751 phba->slim2p.virt = dma_alloc_coherent(&pdev->dev, SLI2_SLIM_SIZE,
7752 &phba->slim2p.phys, GFP_KERNEL);
James Smart3772a992009-05-22 14:50:54 -04007753 if (!phba->slim2p.virt)
7754 goto out_iounmap;
7755
James Smart3772a992009-05-22 14:50:54 -04007756 phba->mbox = phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, mbx);
James Smart7a470272010-03-15 11:25:20 -04007757 phba->mbox_ext = (phba->slim2p.virt +
7758 offsetof(struct lpfc_sli2_slim, mbx_ext_words));
James Smart3772a992009-05-22 14:50:54 -04007759 phba->pcb = (phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, pcb));
7760 phba->IOCBs = (phba->slim2p.virt +
7761 offsetof(struct lpfc_sli2_slim, IOCBs));
7762
7763 phba->hbqslimp.virt = dma_alloc_coherent(&pdev->dev,
7764 lpfc_sli_hbq_size(),
7765 &phba->hbqslimp.phys,
7766 GFP_KERNEL);
7767 if (!phba->hbqslimp.virt)
7768 goto out_free_slim;
7769
7770 hbq_count = lpfc_sli_hbq_count();
7771 ptr = phba->hbqslimp.virt;
7772 for (i = 0; i < hbq_count; ++i) {
7773 phba->hbqs[i].hbq_virt = ptr;
7774 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
7775 ptr += (lpfc_hbq_defs[i]->entry_count *
7776 sizeof(struct lpfc_hbq_entry));
7777 }
7778 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_els_hbq_alloc;
7779 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_els_hbq_free;
7780
7781 memset(phba->hbqslimp.virt, 0, lpfc_sli_hbq_size());
7782
James Smart3772a992009-05-22 14:50:54 -04007783 phba->MBslimaddr = phba->slim_memmap_p;
7784 phba->HAregaddr = phba->ctrl_regs_memmap_p + HA_REG_OFFSET;
7785 phba->CAregaddr = phba->ctrl_regs_memmap_p + CA_REG_OFFSET;
7786 phba->HSregaddr = phba->ctrl_regs_memmap_p + HS_REG_OFFSET;
7787 phba->HCregaddr = phba->ctrl_regs_memmap_p + HC_REG_OFFSET;
James Smartea2151b2008-09-07 11:52:10 -04007788
dea31012005-04-17 16:05:31 -05007789 return 0;
7790
dea31012005-04-17 16:05:31 -05007791out_free_slim:
James Smart34b02dc2008-08-24 21:49:55 -04007792 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
7793 phba->slim2p.virt, phba->slim2p.phys);
dea31012005-04-17 16:05:31 -05007794out_iounmap:
7795 iounmap(phba->ctrl_regs_memmap_p);
Jamie Wellnitz901a9202006-02-28 19:25:19 -05007796out_iounmap_slim:
dea31012005-04-17 16:05:31 -05007797 iounmap(phba->slim_memmap_p);
dea31012005-04-17 16:05:31 -05007798out:
7799 return error;
7800}
7801
James Smarte59058c2008-08-24 21:49:00 -04007802/**
James Smart3772a992009-05-22 14:50:54 -04007803 * lpfc_sli_pci_mem_unset - Unset SLI3 HBA PCI memory space.
7804 * @phba: pointer to lpfc hba data structure.
7805 *
7806 * This routine is invoked to unset the PCI device memory space for device
7807 * with SLI-3 interface spec.
7808 **/
7809static void
7810lpfc_sli_pci_mem_unset(struct lpfc_hba *phba)
7811{
7812 struct pci_dev *pdev;
7813
7814 /* Obtain PCI device reference */
7815 if (!phba->pcidev)
7816 return;
7817 else
7818 pdev = phba->pcidev;
7819
7820 /* Free coherent DMA memory allocated */
7821 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
7822 phba->hbqslimp.virt, phba->hbqslimp.phys);
7823 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
7824 phba->slim2p.virt, phba->slim2p.phys);
7825
7826 /* I/O memory unmap */
7827 iounmap(phba->ctrl_regs_memmap_p);
7828 iounmap(phba->slim_memmap_p);
7829
7830 return;
7831}
7832
7833/**
James Smartda0436e2009-05-22 14:51:39 -04007834 * lpfc_sli4_post_status_check - Wait for SLI4 POST done and check status
7835 * @phba: pointer to lpfc hba data structure.
7836 *
7837 * This routine is invoked to wait for SLI4 device Power On Self Test (POST)
7838 * done and check status.
7839 *
7840 * Return 0 if successful, otherwise -ENODEV.
7841 **/
7842int
7843lpfc_sli4_post_status_check(struct lpfc_hba *phba)
7844{
James Smart2fcee4b2010-12-15 17:57:46 -05007845 struct lpfc_register portsmphr_reg, uerrlo_reg, uerrhi_reg;
7846 struct lpfc_register reg_data;
7847 int i, port_error = 0;
7848 uint32_t if_type;
James Smartda0436e2009-05-22 14:51:39 -04007849
James Smart9940b972011-03-11 16:06:12 -05007850 memset(&portsmphr_reg, 0, sizeof(portsmphr_reg));
7851 memset(&reg_data, 0, sizeof(reg_data));
James Smart2fcee4b2010-12-15 17:57:46 -05007852 if (!phba->sli4_hba.PSMPHRregaddr)
James Smartda0436e2009-05-22 14:51:39 -04007853 return -ENODEV;
7854
James Smartda0436e2009-05-22 14:51:39 -04007855 /* Wait up to 30 seconds for the SLI Port POST done and ready */
7856 for (i = 0; i < 3000; i++) {
James Smart9940b972011-03-11 16:06:12 -05007857 if (lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
7858 &portsmphr_reg.word0) ||
7859 (bf_get(lpfc_port_smphr_perr, &portsmphr_reg))) {
James Smart2fcee4b2010-12-15 17:57:46 -05007860 /* Port has a fatal POST error, break out */
James Smartda0436e2009-05-22 14:51:39 -04007861 port_error = -ENODEV;
7862 break;
7863 }
James Smart2fcee4b2010-12-15 17:57:46 -05007864 if (LPFC_POST_STAGE_PORT_READY ==
7865 bf_get(lpfc_port_smphr_port_status, &portsmphr_reg))
James Smartda0436e2009-05-22 14:51:39 -04007866 break;
James Smartda0436e2009-05-22 14:51:39 -04007867 msleep(10);
7868 }
7869
James Smart2fcee4b2010-12-15 17:57:46 -05007870 /*
7871 * If there was a port error during POST, then don't proceed with
7872 * other register reads as the data may not be valid. Just exit.
7873 */
7874 if (port_error) {
James Smartda0436e2009-05-22 14:51:39 -04007875 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -05007876 "1408 Port Failed POST - portsmphr=0x%x, "
7877 "perr=x%x, sfi=x%x, nip=x%x, ipc=x%x, scr1=x%x, "
7878 "scr2=x%x, hscratch=x%x, pstatus=x%x\n",
7879 portsmphr_reg.word0,
7880 bf_get(lpfc_port_smphr_perr, &portsmphr_reg),
7881 bf_get(lpfc_port_smphr_sfi, &portsmphr_reg),
7882 bf_get(lpfc_port_smphr_nip, &portsmphr_reg),
7883 bf_get(lpfc_port_smphr_ipc, &portsmphr_reg),
7884 bf_get(lpfc_port_smphr_scr1, &portsmphr_reg),
7885 bf_get(lpfc_port_smphr_scr2, &portsmphr_reg),
7886 bf_get(lpfc_port_smphr_host_scratch, &portsmphr_reg),
7887 bf_get(lpfc_port_smphr_port_status, &portsmphr_reg));
7888 } else {
James Smart28baac72010-02-12 14:42:03 -05007889 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -05007890 "2534 Device Info: SLIFamily=0x%x, "
7891 "SLIRev=0x%x, IFType=0x%x, SLIHint_1=0x%x, "
7892 "SLIHint_2=0x%x, FT=0x%x\n",
James Smart28baac72010-02-12 14:42:03 -05007893 bf_get(lpfc_sli_intf_sli_family,
7894 &phba->sli4_hba.sli_intf),
7895 bf_get(lpfc_sli_intf_slirev,
7896 &phba->sli4_hba.sli_intf),
James Smart085c6472010-11-20 23:11:37 -05007897 bf_get(lpfc_sli_intf_if_type,
James Smart28baac72010-02-12 14:42:03 -05007898 &phba->sli4_hba.sli_intf),
James Smart085c6472010-11-20 23:11:37 -05007899 bf_get(lpfc_sli_intf_sli_hint1,
7900 &phba->sli4_hba.sli_intf),
7901 bf_get(lpfc_sli_intf_sli_hint2,
7902 &phba->sli4_hba.sli_intf),
7903 bf_get(lpfc_sli_intf_func_type,
James Smart28baac72010-02-12 14:42:03 -05007904 &phba->sli4_hba.sli_intf));
James Smart2fcee4b2010-12-15 17:57:46 -05007905 /*
7906 * Check for other Port errors during the initialization
7907 * process. Fail the load if the port did not come up
7908 * correctly.
7909 */
7910 if_type = bf_get(lpfc_sli_intf_if_type,
7911 &phba->sli4_hba.sli_intf);
7912 switch (if_type) {
7913 case LPFC_SLI_INTF_IF_TYPE_0:
7914 phba->sli4_hba.ue_mask_lo =
7915 readl(phba->sli4_hba.u.if_type0.UEMASKLOregaddr);
7916 phba->sli4_hba.ue_mask_hi =
7917 readl(phba->sli4_hba.u.if_type0.UEMASKHIregaddr);
7918 uerrlo_reg.word0 =
7919 readl(phba->sli4_hba.u.if_type0.UERRLOregaddr);
7920 uerrhi_reg.word0 =
7921 readl(phba->sli4_hba.u.if_type0.UERRHIregaddr);
7922 if ((~phba->sli4_hba.ue_mask_lo & uerrlo_reg.word0) ||
7923 (~phba->sli4_hba.ue_mask_hi & uerrhi_reg.word0)) {
7924 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7925 "1422 Unrecoverable Error "
7926 "Detected during POST "
7927 "uerr_lo_reg=0x%x, "
7928 "uerr_hi_reg=0x%x, "
7929 "ue_mask_lo_reg=0x%x, "
7930 "ue_mask_hi_reg=0x%x\n",
7931 uerrlo_reg.word0,
7932 uerrhi_reg.word0,
7933 phba->sli4_hba.ue_mask_lo,
7934 phba->sli4_hba.ue_mask_hi);
7935 port_error = -ENODEV;
7936 }
7937 break;
7938 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart27d6ac02018-02-22 08:18:42 -08007939 case LPFC_SLI_INTF_IF_TYPE_6:
James Smart2fcee4b2010-12-15 17:57:46 -05007940 /* Final checks. The port status should be clean. */
James Smart9940b972011-03-11 16:06:12 -05007941 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
7942 &reg_data.word0) ||
James Smart05580562011-05-24 11:40:48 -04007943 (bf_get(lpfc_sliport_status_err, &reg_data) &&
7944 !bf_get(lpfc_sliport_status_rn, &reg_data))) {
James Smart2fcee4b2010-12-15 17:57:46 -05007945 phba->work_status[0] =
7946 readl(phba->sli4_hba.u.if_type2.
7947 ERR1regaddr);
7948 phba->work_status[1] =
7949 readl(phba->sli4_hba.u.if_type2.
7950 ERR2regaddr);
7951 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart8fcb8ac2012-03-01 22:35:58 -05007952 "2888 Unrecoverable port error "
7953 "following POST: port status reg "
7954 "0x%x, port_smphr reg 0x%x, "
James Smart2fcee4b2010-12-15 17:57:46 -05007955 "error 1=0x%x, error 2=0x%x\n",
7956 reg_data.word0,
7957 portsmphr_reg.word0,
7958 phba->work_status[0],
7959 phba->work_status[1]);
7960 port_error = -ENODEV;
7961 }
7962 break;
7963 case LPFC_SLI_INTF_IF_TYPE_1:
7964 default:
7965 break;
7966 }
James Smart28baac72010-02-12 14:42:03 -05007967 }
James Smartda0436e2009-05-22 14:51:39 -04007968 return port_error;
7969}
7970
7971/**
7972 * lpfc_sli4_bar0_register_memmap - Set up SLI4 BAR0 register memory map.
7973 * @phba: pointer to lpfc hba data structure.
James Smart2fcee4b2010-12-15 17:57:46 -05007974 * @if_type: The SLI4 interface type getting configured.
James Smartda0436e2009-05-22 14:51:39 -04007975 *
7976 * This routine is invoked to set up SLI4 BAR0 PCI config space register
7977 * memory map.
7978 **/
7979static void
James Smart2fcee4b2010-12-15 17:57:46 -05007980lpfc_sli4_bar0_register_memmap(struct lpfc_hba *phba, uint32_t if_type)
James Smartda0436e2009-05-22 14:51:39 -04007981{
James Smart2fcee4b2010-12-15 17:57:46 -05007982 switch (if_type) {
7983 case LPFC_SLI_INTF_IF_TYPE_0:
7984 phba->sli4_hba.u.if_type0.UERRLOregaddr =
7985 phba->sli4_hba.conf_regs_memmap_p + LPFC_UERR_STATUS_LO;
7986 phba->sli4_hba.u.if_type0.UERRHIregaddr =
7987 phba->sli4_hba.conf_regs_memmap_p + LPFC_UERR_STATUS_HI;
7988 phba->sli4_hba.u.if_type0.UEMASKLOregaddr =
7989 phba->sli4_hba.conf_regs_memmap_p + LPFC_UE_MASK_LO;
7990 phba->sli4_hba.u.if_type0.UEMASKHIregaddr =
7991 phba->sli4_hba.conf_regs_memmap_p + LPFC_UE_MASK_HI;
7992 phba->sli4_hba.SLIINTFregaddr =
7993 phba->sli4_hba.conf_regs_memmap_p + LPFC_SLI_INTF;
7994 break;
7995 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart0cf07f842017-06-01 21:07:10 -07007996 phba->sli4_hba.u.if_type2.EQDregaddr =
7997 phba->sli4_hba.conf_regs_memmap_p +
7998 LPFC_CTL_PORT_EQ_DELAY_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05007999 phba->sli4_hba.u.if_type2.ERR1regaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04008000 phba->sli4_hba.conf_regs_memmap_p +
8001 LPFC_CTL_PORT_ER1_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05008002 phba->sli4_hba.u.if_type2.ERR2regaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04008003 phba->sli4_hba.conf_regs_memmap_p +
8004 LPFC_CTL_PORT_ER2_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05008005 phba->sli4_hba.u.if_type2.CTRLregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04008006 phba->sli4_hba.conf_regs_memmap_p +
8007 LPFC_CTL_PORT_CTL_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05008008 phba->sli4_hba.u.if_type2.STATUSregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04008009 phba->sli4_hba.conf_regs_memmap_p +
8010 LPFC_CTL_PORT_STA_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05008011 phba->sli4_hba.SLIINTFregaddr =
8012 phba->sli4_hba.conf_regs_memmap_p + LPFC_SLI_INTF;
8013 phba->sli4_hba.PSMPHRregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04008014 phba->sli4_hba.conf_regs_memmap_p +
8015 LPFC_CTL_PORT_SEM_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05008016 phba->sli4_hba.RQDBregaddr =
James Smart962bc512013-01-03 15:44:00 -05008017 phba->sli4_hba.conf_regs_memmap_p +
8018 LPFC_ULP0_RQ_DOORBELL;
James Smart2fcee4b2010-12-15 17:57:46 -05008019 phba->sli4_hba.WQDBregaddr =
James Smart962bc512013-01-03 15:44:00 -05008020 phba->sli4_hba.conf_regs_memmap_p +
8021 LPFC_ULP0_WQ_DOORBELL;
James Smart9dd35422018-02-22 08:18:41 -08008022 phba->sli4_hba.CQDBregaddr =
James Smart2fcee4b2010-12-15 17:57:46 -05008023 phba->sli4_hba.conf_regs_memmap_p + LPFC_EQCQ_DOORBELL;
James Smart9dd35422018-02-22 08:18:41 -08008024 phba->sli4_hba.EQDBregaddr = phba->sli4_hba.CQDBregaddr;
James Smart2fcee4b2010-12-15 17:57:46 -05008025 phba->sli4_hba.MQDBregaddr =
8026 phba->sli4_hba.conf_regs_memmap_p + LPFC_MQ_DOORBELL;
8027 phba->sli4_hba.BMBXregaddr =
8028 phba->sli4_hba.conf_regs_memmap_p + LPFC_BMBX;
8029 break;
James Smart27d6ac02018-02-22 08:18:42 -08008030 case LPFC_SLI_INTF_IF_TYPE_6:
8031 phba->sli4_hba.u.if_type2.EQDregaddr =
8032 phba->sli4_hba.conf_regs_memmap_p +
8033 LPFC_CTL_PORT_EQ_DELAY_OFFSET;
8034 phba->sli4_hba.u.if_type2.ERR1regaddr =
8035 phba->sli4_hba.conf_regs_memmap_p +
8036 LPFC_CTL_PORT_ER1_OFFSET;
8037 phba->sli4_hba.u.if_type2.ERR2regaddr =
8038 phba->sli4_hba.conf_regs_memmap_p +
8039 LPFC_CTL_PORT_ER2_OFFSET;
8040 phba->sli4_hba.u.if_type2.CTRLregaddr =
8041 phba->sli4_hba.conf_regs_memmap_p +
8042 LPFC_CTL_PORT_CTL_OFFSET;
8043 phba->sli4_hba.u.if_type2.STATUSregaddr =
8044 phba->sli4_hba.conf_regs_memmap_p +
8045 LPFC_CTL_PORT_STA_OFFSET;
8046 phba->sli4_hba.PSMPHRregaddr =
8047 phba->sli4_hba.conf_regs_memmap_p +
8048 LPFC_CTL_PORT_SEM_OFFSET;
8049 phba->sli4_hba.BMBXregaddr =
8050 phba->sli4_hba.conf_regs_memmap_p + LPFC_BMBX;
8051 break;
James Smart2fcee4b2010-12-15 17:57:46 -05008052 case LPFC_SLI_INTF_IF_TYPE_1:
8053 default:
8054 dev_printk(KERN_ERR, &phba->pcidev->dev,
8055 "FATAL - unsupported SLI4 interface type - %d\n",
8056 if_type);
8057 break;
8058 }
James Smartda0436e2009-05-22 14:51:39 -04008059}
8060
8061/**
8062 * lpfc_sli4_bar1_register_memmap - Set up SLI4 BAR1 register memory map.
8063 * @phba: pointer to lpfc hba data structure.
8064 *
James Smart27d6ac02018-02-22 08:18:42 -08008065 * This routine is invoked to set up SLI4 BAR1 register memory map.
James Smartda0436e2009-05-22 14:51:39 -04008066 **/
8067static void
James Smart27d6ac02018-02-22 08:18:42 -08008068lpfc_sli4_bar1_register_memmap(struct lpfc_hba *phba, uint32_t if_type)
James Smartda0436e2009-05-22 14:51:39 -04008069{
James Smart27d6ac02018-02-22 08:18:42 -08008070 switch (if_type) {
8071 case LPFC_SLI_INTF_IF_TYPE_0:
8072 phba->sli4_hba.PSMPHRregaddr =
8073 phba->sli4_hba.ctrl_regs_memmap_p +
8074 LPFC_SLIPORT_IF0_SMPHR;
8075 phba->sli4_hba.ISRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
8076 LPFC_HST_ISR0;
8077 phba->sli4_hba.IMRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
8078 LPFC_HST_IMR0;
8079 phba->sli4_hba.ISCRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
8080 LPFC_HST_ISCR0;
8081 break;
8082 case LPFC_SLI_INTF_IF_TYPE_6:
8083 phba->sli4_hba.RQDBregaddr = phba->sli4_hba.drbl_regs_memmap_p +
8084 LPFC_IF6_RQ_DOORBELL;
8085 phba->sli4_hba.WQDBregaddr = phba->sli4_hba.drbl_regs_memmap_p +
8086 LPFC_IF6_WQ_DOORBELL;
8087 phba->sli4_hba.CQDBregaddr = phba->sli4_hba.drbl_regs_memmap_p +
8088 LPFC_IF6_CQ_DOORBELL;
8089 phba->sli4_hba.EQDBregaddr = phba->sli4_hba.drbl_regs_memmap_p +
8090 LPFC_IF6_EQ_DOORBELL;
8091 phba->sli4_hba.MQDBregaddr = phba->sli4_hba.drbl_regs_memmap_p +
8092 LPFC_IF6_MQ_DOORBELL;
8093 break;
8094 case LPFC_SLI_INTF_IF_TYPE_2:
8095 case LPFC_SLI_INTF_IF_TYPE_1:
8096 default:
8097 dev_err(&phba->pcidev->dev,
8098 "FATAL - unsupported SLI4 interface type - %d\n",
8099 if_type);
8100 break;
8101 }
James Smartda0436e2009-05-22 14:51:39 -04008102}
8103
8104/**
8105 * lpfc_sli4_bar2_register_memmap - Set up SLI4 BAR2 register memory map.
8106 * @phba: pointer to lpfc hba data structure.
8107 * @vf: virtual function number
8108 *
8109 * This routine is invoked to set up SLI4 BAR2 doorbell register memory map
8110 * based on the given viftual function number, @vf.
8111 *
8112 * Return 0 if successful, otherwise -ENODEV.
8113 **/
8114static int
8115lpfc_sli4_bar2_register_memmap(struct lpfc_hba *phba, uint32_t vf)
8116{
8117 if (vf > LPFC_VIR_FUNC_MAX)
8118 return -ENODEV;
8119
8120 phba->sli4_hba.RQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
James Smart962bc512013-01-03 15:44:00 -05008121 vf * LPFC_VFR_PAGE_SIZE +
8122 LPFC_ULP0_RQ_DOORBELL);
James Smartda0436e2009-05-22 14:51:39 -04008123 phba->sli4_hba.WQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
James Smart962bc512013-01-03 15:44:00 -05008124 vf * LPFC_VFR_PAGE_SIZE +
8125 LPFC_ULP0_WQ_DOORBELL);
James Smart9dd35422018-02-22 08:18:41 -08008126 phba->sli4_hba.CQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
8127 vf * LPFC_VFR_PAGE_SIZE +
8128 LPFC_EQCQ_DOORBELL);
8129 phba->sli4_hba.EQDBregaddr = phba->sli4_hba.CQDBregaddr;
James Smartda0436e2009-05-22 14:51:39 -04008130 phba->sli4_hba.MQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
8131 vf * LPFC_VFR_PAGE_SIZE + LPFC_MQ_DOORBELL);
8132 phba->sli4_hba.BMBXregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
8133 vf * LPFC_VFR_PAGE_SIZE + LPFC_BMBX);
8134 return 0;
8135}
8136
8137/**
8138 * lpfc_create_bootstrap_mbox - Create the bootstrap mailbox
8139 * @phba: pointer to lpfc hba data structure.
8140 *
8141 * This routine is invoked to create the bootstrap mailbox
8142 * region consistent with the SLI-4 interface spec. This
8143 * routine allocates all memory necessary to communicate
8144 * mailbox commands to the port and sets up all alignment
8145 * needs. No locks are expected to be held when calling
8146 * this routine.
8147 *
8148 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008149 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04008150 * -ENOMEM - could not allocated memory.
James Smartda0436e2009-05-22 14:51:39 -04008151 **/
8152static int
8153lpfc_create_bootstrap_mbox(struct lpfc_hba *phba)
8154{
8155 uint32_t bmbx_size;
8156 struct lpfc_dmabuf *dmabuf;
8157 struct dma_address *dma_address;
8158 uint32_t pa_addr;
8159 uint64_t phys_addr;
8160
8161 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
8162 if (!dmabuf)
8163 return -ENOMEM;
8164
8165 /*
8166 * The bootstrap mailbox region is comprised of 2 parts
8167 * plus an alignment restriction of 16 bytes.
8168 */
8169 bmbx_size = sizeof(struct lpfc_bmbx_create) + (LPFC_ALIGN_16_BYTE - 1);
Luis Chamberlain750afb02019-01-04 09:23:09 +01008170 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev, bmbx_size,
8171 &dmabuf->phys, GFP_KERNEL);
James Smartda0436e2009-05-22 14:51:39 -04008172 if (!dmabuf->virt) {
8173 kfree(dmabuf);
8174 return -ENOMEM;
8175 }
James Smartda0436e2009-05-22 14:51:39 -04008176
8177 /*
8178 * Initialize the bootstrap mailbox pointers now so that the register
8179 * operations are simple later. The mailbox dma address is required
8180 * to be 16-byte aligned. Also align the virtual memory as each
8181 * maibox is copied into the bmbx mailbox region before issuing the
8182 * command to the port.
8183 */
8184 phba->sli4_hba.bmbx.dmabuf = dmabuf;
8185 phba->sli4_hba.bmbx.bmbx_size = bmbx_size;
8186
8187 phba->sli4_hba.bmbx.avirt = PTR_ALIGN(dmabuf->virt,
8188 LPFC_ALIGN_16_BYTE);
8189 phba->sli4_hba.bmbx.aphys = ALIGN(dmabuf->phys,
8190 LPFC_ALIGN_16_BYTE);
8191
8192 /*
8193 * Set the high and low physical addresses now. The SLI4 alignment
8194 * requirement is 16 bytes and the mailbox is posted to the port
8195 * as two 30-bit addresses. The other data is a bit marking whether
8196 * the 30-bit address is the high or low address.
8197 * Upcast bmbx aphys to 64bits so shift instruction compiles
8198 * clean on 32 bit machines.
8199 */
8200 dma_address = &phba->sli4_hba.bmbx.dma_address;
8201 phys_addr = (uint64_t)phba->sli4_hba.bmbx.aphys;
8202 pa_addr = (uint32_t) ((phys_addr >> 34) & 0x3fffffff);
8203 dma_address->addr_hi = (uint32_t) ((pa_addr << 2) |
8204 LPFC_BMBX_BIT1_ADDR_HI);
8205
8206 pa_addr = (uint32_t) ((phba->sli4_hba.bmbx.aphys >> 4) & 0x3fffffff);
8207 dma_address->addr_lo = (uint32_t) ((pa_addr << 2) |
8208 LPFC_BMBX_BIT1_ADDR_LO);
8209 return 0;
8210}
8211
8212/**
8213 * lpfc_destroy_bootstrap_mbox - Destroy all bootstrap mailbox resources
8214 * @phba: pointer to lpfc hba data structure.
8215 *
8216 * This routine is invoked to teardown the bootstrap mailbox
8217 * region and release all host resources. This routine requires
8218 * the caller to ensure all mailbox commands recovered, no
8219 * additional mailbox comands are sent, and interrupts are disabled
8220 * before calling this routine.
8221 *
8222 **/
8223static void
8224lpfc_destroy_bootstrap_mbox(struct lpfc_hba *phba)
8225{
8226 dma_free_coherent(&phba->pcidev->dev,
8227 phba->sli4_hba.bmbx.bmbx_size,
8228 phba->sli4_hba.bmbx.dmabuf->virt,
8229 phba->sli4_hba.bmbx.dmabuf->phys);
8230
8231 kfree(phba->sli4_hba.bmbx.dmabuf);
8232 memset(&phba->sli4_hba.bmbx, 0, sizeof(struct lpfc_bmbx));
8233}
8234
8235/**
8236 * lpfc_sli4_read_config - Get the config parameters.
8237 * @phba: pointer to lpfc hba data structure.
8238 *
8239 * This routine is invoked to read the configuration parameters from the HBA.
8240 * The configuration parameters are used to set the base and maximum values
8241 * for RPI's XRI's VPI's VFI's and FCFIs. These values also affect the resource
8242 * allocation for the port.
8243 *
8244 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008245 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008246 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04008247 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04008248 **/
James Smartff78d8f2011-12-13 13:21:35 -05008249int
James Smartda0436e2009-05-22 14:51:39 -04008250lpfc_sli4_read_config(struct lpfc_hba *phba)
8251{
8252 LPFC_MBOXQ_t *pmb;
8253 struct lpfc_mbx_read_config *rd_config;
James Smart912e3ac2011-05-24 11:42:11 -04008254 union lpfc_sli4_cfg_shdr *shdr;
8255 uint32_t shdr_status, shdr_add_status;
8256 struct lpfc_mbx_get_func_cfg *get_func_cfg;
8257 struct lpfc_rsrc_desc_fcfcoe *desc;
James Smart8aa134a2012-08-14 14:25:29 -04008258 char *pdesc_0;
James Smartc6918162016-10-13 15:06:16 -07008259 uint16_t forced_link_speed;
James Smart6a828b02019-01-28 11:14:31 -08008260 uint32_t if_type, qmin;
James Smart8aa134a2012-08-14 14:25:29 -04008261 int length, i, rc = 0, rc2;
James Smartda0436e2009-05-22 14:51:39 -04008262
8263 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
8264 if (!pmb) {
8265 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8266 "2011 Unable to allocate memory for issuing "
8267 "SLI_CONFIG_SPECIAL mailbox command\n");
8268 return -ENOMEM;
8269 }
8270
8271 lpfc_read_config(phba, pmb);
8272
8273 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
8274 if (rc != MBX_SUCCESS) {
8275 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8276 "2012 Mailbox failed , mbxCmd x%x "
8277 "READ_CONFIG, mbxStatus x%x\n",
8278 bf_get(lpfc_mqe_command, &pmb->u.mqe),
8279 bf_get(lpfc_mqe_status, &pmb->u.mqe));
8280 rc = -EIO;
8281 } else {
8282 rd_config = &pmb->u.mqe.un.rd_config;
James Smartff78d8f2011-12-13 13:21:35 -05008283 if (bf_get(lpfc_mbx_rd_conf_lnk_ldv, rd_config)) {
8284 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
8285 phba->sli4_hba.lnk_info.lnk_tp =
8286 bf_get(lpfc_mbx_rd_conf_lnk_type, rd_config);
8287 phba->sli4_hba.lnk_info.lnk_no =
8288 bf_get(lpfc_mbx_rd_conf_lnk_numb, rd_config);
8289 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8290 "3081 lnk_type:%d, lnk_numb:%d\n",
8291 phba->sli4_hba.lnk_info.lnk_tp,
8292 phba->sli4_hba.lnk_info.lnk_no);
8293 } else
8294 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8295 "3082 Mailbox (x%x) returned ldv:x0\n",
8296 bf_get(lpfc_mqe_command, &pmb->u.mqe));
James Smart44fd7fe2017-08-23 16:55:47 -07008297 if (bf_get(lpfc_mbx_rd_conf_bbscn_def, rd_config)) {
8298 phba->bbcredit_support = 1;
8299 phba->sli4_hba.bbscn_params.word0 = rd_config->word8;
8300 }
8301
James Smart1dc5ec22018-10-23 13:41:11 -07008302 phba->sli4_hba.conf_trunk =
8303 bf_get(lpfc_mbx_rd_conf_trunk, rd_config);
James Smart6d368e52011-05-24 11:44:12 -04008304 phba->sli4_hba.extents_in_use =
8305 bf_get(lpfc_mbx_rd_conf_extnts_inuse, rd_config);
James Smartda0436e2009-05-22 14:51:39 -04008306 phba->sli4_hba.max_cfg_param.max_xri =
8307 bf_get(lpfc_mbx_rd_conf_xri_count, rd_config);
8308 phba->sli4_hba.max_cfg_param.xri_base =
8309 bf_get(lpfc_mbx_rd_conf_xri_base, rd_config);
8310 phba->sli4_hba.max_cfg_param.max_vpi =
8311 bf_get(lpfc_mbx_rd_conf_vpi_count, rd_config);
James Smart8b47ae62018-11-29 16:09:33 -08008312 /* Limit the max we support */
8313 if (phba->sli4_hba.max_cfg_param.max_vpi > LPFC_MAX_VPORTS)
8314 phba->sli4_hba.max_cfg_param.max_vpi = LPFC_MAX_VPORTS;
James Smartda0436e2009-05-22 14:51:39 -04008315 phba->sli4_hba.max_cfg_param.vpi_base =
8316 bf_get(lpfc_mbx_rd_conf_vpi_base, rd_config);
8317 phba->sli4_hba.max_cfg_param.max_rpi =
8318 bf_get(lpfc_mbx_rd_conf_rpi_count, rd_config);
8319 phba->sli4_hba.max_cfg_param.rpi_base =
8320 bf_get(lpfc_mbx_rd_conf_rpi_base, rd_config);
8321 phba->sli4_hba.max_cfg_param.max_vfi =
8322 bf_get(lpfc_mbx_rd_conf_vfi_count, rd_config);
8323 phba->sli4_hba.max_cfg_param.vfi_base =
8324 bf_get(lpfc_mbx_rd_conf_vfi_base, rd_config);
8325 phba->sli4_hba.max_cfg_param.max_fcfi =
8326 bf_get(lpfc_mbx_rd_conf_fcfi_count, rd_config);
James Smartda0436e2009-05-22 14:51:39 -04008327 phba->sli4_hba.max_cfg_param.max_eq =
8328 bf_get(lpfc_mbx_rd_conf_eq_count, rd_config);
8329 phba->sli4_hba.max_cfg_param.max_rq =
8330 bf_get(lpfc_mbx_rd_conf_rq_count, rd_config);
8331 phba->sli4_hba.max_cfg_param.max_wq =
8332 bf_get(lpfc_mbx_rd_conf_wq_count, rd_config);
8333 phba->sli4_hba.max_cfg_param.max_cq =
8334 bf_get(lpfc_mbx_rd_conf_cq_count, rd_config);
8335 phba->lmt = bf_get(lpfc_mbx_rd_conf_lmt, rd_config);
8336 phba->sli4_hba.next_xri = phba->sli4_hba.max_cfg_param.xri_base;
8337 phba->vpi_base = phba->sli4_hba.max_cfg_param.vpi_base;
8338 phba->vfi_base = phba->sli4_hba.max_cfg_param.vfi_base;
James Smart5ffc2662009-11-18 15:39:44 -05008339 phba->max_vpi = (phba->sli4_hba.max_cfg_param.max_vpi > 0) ?
8340 (phba->sli4_hba.max_cfg_param.max_vpi - 1) : 0;
James Smartda0436e2009-05-22 14:51:39 -04008341 phba->max_vports = phba->max_vpi;
8342 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart6d368e52011-05-24 11:44:12 -04008343 "2003 cfg params Extents? %d "
8344 "XRI(B:%d M:%d), "
James Smartda0436e2009-05-22 14:51:39 -04008345 "VPI(B:%d M:%d) "
8346 "VFI(B:%d M:%d) "
8347 "RPI(B:%d M:%d) "
James Smart2ea259e2017-02-12 13:52:27 -08008348 "FCFI:%d EQ:%d CQ:%d WQ:%d RQ:%d\n",
James Smart6d368e52011-05-24 11:44:12 -04008349 phba->sli4_hba.extents_in_use,
James Smartda0436e2009-05-22 14:51:39 -04008350 phba->sli4_hba.max_cfg_param.xri_base,
8351 phba->sli4_hba.max_cfg_param.max_xri,
8352 phba->sli4_hba.max_cfg_param.vpi_base,
8353 phba->sli4_hba.max_cfg_param.max_vpi,
8354 phba->sli4_hba.max_cfg_param.vfi_base,
8355 phba->sli4_hba.max_cfg_param.max_vfi,
8356 phba->sli4_hba.max_cfg_param.rpi_base,
8357 phba->sli4_hba.max_cfg_param.max_rpi,
James Smart2ea259e2017-02-12 13:52:27 -08008358 phba->sli4_hba.max_cfg_param.max_fcfi,
8359 phba->sli4_hba.max_cfg_param.max_eq,
8360 phba->sli4_hba.max_cfg_param.max_cq,
8361 phba->sli4_hba.max_cfg_param.max_wq,
8362 phba->sli4_hba.max_cfg_param.max_rq);
8363
James Smartd38f33b2018-05-04 20:37:54 -07008364 /*
James Smart6a828b02019-01-28 11:14:31 -08008365 * Calculate queue resources based on how
8366 * many WQ/CQ/EQs are available.
James Smartd38f33b2018-05-04 20:37:54 -07008367 */
James Smart6a828b02019-01-28 11:14:31 -08008368 qmin = phba->sli4_hba.max_cfg_param.max_wq;
8369 if (phba->sli4_hba.max_cfg_param.max_cq < qmin)
8370 qmin = phba->sli4_hba.max_cfg_param.max_cq;
8371 if (phba->sli4_hba.max_cfg_param.max_eq < qmin)
8372 qmin = phba->sli4_hba.max_cfg_param.max_eq;
8373 /*
8374 * Whats left after this can go toward NVME / FCP.
8375 * The minus 4 accounts for ELS, NVME LS, MBOX
8376 * plus one extra. When configured for
8377 * NVMET, FCP io channel WQs are not created.
8378 */
8379 qmin -= 4;
James Smartd38f33b2018-05-04 20:37:54 -07008380
James Smart6a828b02019-01-28 11:14:31 -08008381 /* If NVME is configured, double the number of CQ/WQs needed */
8382 if ((phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) &&
8383 !phba->nvmet_support)
8384 qmin /= 2;
James Smartd38f33b2018-05-04 20:37:54 -07008385
James Smart6a828b02019-01-28 11:14:31 -08008386 /* Check to see if there is enough for NVME */
8387 if ((phba->cfg_irq_chann > qmin) ||
8388 (phba->cfg_hdw_queue > qmin)) {
8389 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8390 "2005 Reducing Queues: "
8391 "WQ %d CQ %d EQ %d: min %d: "
8392 "IRQ %d HDWQ %d\n",
James Smartd38f33b2018-05-04 20:37:54 -07008393 phba->sli4_hba.max_cfg_param.max_wq,
8394 phba->sli4_hba.max_cfg_param.max_cq,
James Smart6a828b02019-01-28 11:14:31 -08008395 phba->sli4_hba.max_cfg_param.max_eq,
8396 qmin, phba->cfg_irq_chann,
James Smartcdb42be2019-01-28 11:14:21 -08008397 phba->cfg_hdw_queue);
James Smartd38f33b2018-05-04 20:37:54 -07008398
James Smart6a828b02019-01-28 11:14:31 -08008399 if (phba->cfg_irq_chann > qmin)
8400 phba->cfg_irq_chann = qmin;
8401 if (phba->cfg_hdw_queue > qmin)
8402 phba->cfg_hdw_queue = qmin;
James Smartd38f33b2018-05-04 20:37:54 -07008403 }
James Smartda0436e2009-05-22 14:51:39 -04008404 }
James Smart912e3ac2011-05-24 11:42:11 -04008405
8406 if (rc)
8407 goto read_cfg_out;
James Smartda0436e2009-05-22 14:51:39 -04008408
James Smartc6918162016-10-13 15:06:16 -07008409 /* Update link speed if forced link speed is supported */
8410 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
James Smart27d6ac02018-02-22 08:18:42 -08008411 if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
James Smartc6918162016-10-13 15:06:16 -07008412 forced_link_speed =
8413 bf_get(lpfc_mbx_rd_conf_link_speed, rd_config);
8414 if (forced_link_speed) {
8415 phba->hba_flag |= HBA_FORCED_LINK_SPEED;
8416
8417 switch (forced_link_speed) {
8418 case LINK_SPEED_1G:
8419 phba->cfg_link_speed =
8420 LPFC_USER_LINK_SPEED_1G;
8421 break;
8422 case LINK_SPEED_2G:
8423 phba->cfg_link_speed =
8424 LPFC_USER_LINK_SPEED_2G;
8425 break;
8426 case LINK_SPEED_4G:
8427 phba->cfg_link_speed =
8428 LPFC_USER_LINK_SPEED_4G;
8429 break;
8430 case LINK_SPEED_8G:
8431 phba->cfg_link_speed =
8432 LPFC_USER_LINK_SPEED_8G;
8433 break;
8434 case LINK_SPEED_10G:
8435 phba->cfg_link_speed =
8436 LPFC_USER_LINK_SPEED_10G;
8437 break;
8438 case LINK_SPEED_16G:
8439 phba->cfg_link_speed =
8440 LPFC_USER_LINK_SPEED_16G;
8441 break;
8442 case LINK_SPEED_32G:
8443 phba->cfg_link_speed =
8444 LPFC_USER_LINK_SPEED_32G;
8445 break;
James Smartfbd8a6b2018-02-22 08:18:45 -08008446 case LINK_SPEED_64G:
8447 phba->cfg_link_speed =
8448 LPFC_USER_LINK_SPEED_64G;
8449 break;
James Smartc6918162016-10-13 15:06:16 -07008450 case 0xffff:
8451 phba->cfg_link_speed =
8452 LPFC_USER_LINK_SPEED_AUTO;
8453 break;
8454 default:
8455 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8456 "0047 Unrecognized link "
8457 "speed : %d\n",
8458 forced_link_speed);
8459 phba->cfg_link_speed =
8460 LPFC_USER_LINK_SPEED_AUTO;
8461 }
8462 }
8463 }
8464
James Smartda0436e2009-05-22 14:51:39 -04008465 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
James Smart572709e2013-07-15 18:32:43 -04008466 length = phba->sli4_hba.max_cfg_param.max_xri -
8467 lpfc_sli4_get_els_iocb_cnt(phba);
8468 if (phba->cfg_hba_queue_depth > length) {
8469 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
8470 "3361 HBA queue depth changed from %d to %d\n",
8471 phba->cfg_hba_queue_depth, length);
8472 phba->cfg_hba_queue_depth = length;
8473 }
James Smart912e3ac2011-05-24 11:42:11 -04008474
James Smart27d6ac02018-02-22 08:18:42 -08008475 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) <
James Smart912e3ac2011-05-24 11:42:11 -04008476 LPFC_SLI_INTF_IF_TYPE_2)
8477 goto read_cfg_out;
8478
8479 /* get the pf# and vf# for SLI4 if_type 2 port */
8480 length = (sizeof(struct lpfc_mbx_get_func_cfg) -
8481 sizeof(struct lpfc_sli4_cfg_mhdr));
8482 lpfc_sli4_config(phba, pmb, LPFC_MBOX_SUBSYSTEM_COMMON,
8483 LPFC_MBOX_OPCODE_GET_FUNCTION_CONFIG,
8484 length, LPFC_SLI4_MBX_EMBED);
8485
James Smart8aa134a2012-08-14 14:25:29 -04008486 rc2 = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart912e3ac2011-05-24 11:42:11 -04008487 shdr = (union lpfc_sli4_cfg_shdr *)
8488 &pmb->u.mqe.un.sli4_config.header.cfg_shdr;
8489 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
8490 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
James Smart8aa134a2012-08-14 14:25:29 -04008491 if (rc2 || shdr_status || shdr_add_status) {
James Smart912e3ac2011-05-24 11:42:11 -04008492 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8493 "3026 Mailbox failed , mbxCmd x%x "
8494 "GET_FUNCTION_CONFIG, mbxStatus x%x\n",
8495 bf_get(lpfc_mqe_command, &pmb->u.mqe),
8496 bf_get(lpfc_mqe_status, &pmb->u.mqe));
James Smart912e3ac2011-05-24 11:42:11 -04008497 goto read_cfg_out;
8498 }
8499
8500 /* search for fc_fcoe resrouce descriptor */
8501 get_func_cfg = &pmb->u.mqe.un.get_func_cfg;
James Smart912e3ac2011-05-24 11:42:11 -04008502
James Smart8aa134a2012-08-14 14:25:29 -04008503 pdesc_0 = (char *)&get_func_cfg->func_cfg.desc[0];
8504 desc = (struct lpfc_rsrc_desc_fcfcoe *)pdesc_0;
8505 length = bf_get(lpfc_rsrc_desc_fcfcoe_length, desc);
8506 if (length == LPFC_RSRC_DESC_TYPE_FCFCOE_V0_RSVD)
8507 length = LPFC_RSRC_DESC_TYPE_FCFCOE_V0_LENGTH;
8508 else if (length != LPFC_RSRC_DESC_TYPE_FCFCOE_V1_LENGTH)
8509 goto read_cfg_out;
8510
James Smart912e3ac2011-05-24 11:42:11 -04008511 for (i = 0; i < LPFC_RSRC_DESC_MAX_NUM; i++) {
James Smart8aa134a2012-08-14 14:25:29 -04008512 desc = (struct lpfc_rsrc_desc_fcfcoe *)(pdesc_0 + length * i);
James Smart912e3ac2011-05-24 11:42:11 -04008513 if (LPFC_RSRC_DESC_TYPE_FCFCOE ==
James Smart8aa134a2012-08-14 14:25:29 -04008514 bf_get(lpfc_rsrc_desc_fcfcoe_type, desc)) {
James Smart912e3ac2011-05-24 11:42:11 -04008515 phba->sli4_hba.iov.pf_number =
8516 bf_get(lpfc_rsrc_desc_fcfcoe_pfnum, desc);
8517 phba->sli4_hba.iov.vf_number =
8518 bf_get(lpfc_rsrc_desc_fcfcoe_vfnum, desc);
8519 break;
8520 }
8521 }
8522
8523 if (i < LPFC_RSRC_DESC_MAX_NUM)
8524 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8525 "3027 GET_FUNCTION_CONFIG: pf_number:%d, "
8526 "vf_number:%d\n", phba->sli4_hba.iov.pf_number,
8527 phba->sli4_hba.iov.vf_number);
James Smart8aa134a2012-08-14 14:25:29 -04008528 else
James Smart912e3ac2011-05-24 11:42:11 -04008529 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8530 "3028 GET_FUNCTION_CONFIG: failed to find "
Colin Ian Kingc4dba182018-10-16 18:28:53 +01008531 "Resource Descriptor:x%x\n",
James Smart912e3ac2011-05-24 11:42:11 -04008532 LPFC_RSRC_DESC_TYPE_FCFCOE);
James Smart912e3ac2011-05-24 11:42:11 -04008533
8534read_cfg_out:
8535 mempool_free(pmb, phba->mbox_mem_pool);
James Smartda0436e2009-05-22 14:51:39 -04008536 return rc;
8537}
8538
8539/**
James Smart2fcee4b2010-12-15 17:57:46 -05008540 * lpfc_setup_endian_order - Write endian order to an SLI4 if_type 0 port.
James Smartda0436e2009-05-22 14:51:39 -04008541 * @phba: pointer to lpfc hba data structure.
8542 *
James Smart2fcee4b2010-12-15 17:57:46 -05008543 * This routine is invoked to setup the port-side endian order when
8544 * the port if_type is 0. This routine has no function for other
8545 * if_types.
James Smartda0436e2009-05-22 14:51:39 -04008546 *
8547 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008548 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008549 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04008550 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04008551 **/
8552static int
8553lpfc_setup_endian_order(struct lpfc_hba *phba)
8554{
8555 LPFC_MBOXQ_t *mboxq;
James Smart2fcee4b2010-12-15 17:57:46 -05008556 uint32_t if_type, rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04008557 uint32_t endian_mb_data[2] = {HOST_ENDIAN_LOW_WORD0,
8558 HOST_ENDIAN_HIGH_WORD1};
8559
James Smart2fcee4b2010-12-15 17:57:46 -05008560 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
8561 switch (if_type) {
8562 case LPFC_SLI_INTF_IF_TYPE_0:
8563 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
8564 GFP_KERNEL);
8565 if (!mboxq) {
8566 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8567 "0492 Unable to allocate memory for "
8568 "issuing SLI_CONFIG_SPECIAL mailbox "
8569 "command\n");
8570 return -ENOMEM;
8571 }
James Smartda0436e2009-05-22 14:51:39 -04008572
James Smart2fcee4b2010-12-15 17:57:46 -05008573 /*
8574 * The SLI4_CONFIG_SPECIAL mailbox command requires the first
8575 * two words to contain special data values and no other data.
8576 */
8577 memset(mboxq, 0, sizeof(LPFC_MBOXQ_t));
8578 memcpy(&mboxq->u.mqe, &endian_mb_data, sizeof(endian_mb_data));
8579 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8580 if (rc != MBX_SUCCESS) {
8581 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8582 "0493 SLI_CONFIG_SPECIAL mailbox "
8583 "failed with status x%x\n",
8584 rc);
8585 rc = -EIO;
8586 }
8587 mempool_free(mboxq, phba->mbox_mem_pool);
8588 break;
James Smart27d6ac02018-02-22 08:18:42 -08008589 case LPFC_SLI_INTF_IF_TYPE_6:
James Smart2fcee4b2010-12-15 17:57:46 -05008590 case LPFC_SLI_INTF_IF_TYPE_2:
8591 case LPFC_SLI_INTF_IF_TYPE_1:
8592 default:
8593 break;
James Smartda0436e2009-05-22 14:51:39 -04008594 }
James Smartda0436e2009-05-22 14:51:39 -04008595 return rc;
8596}
8597
8598/**
James Smart895427b2017-02-12 13:52:30 -08008599 * lpfc_sli4_queue_verify - Verify and update EQ counts
James Smartda0436e2009-05-22 14:51:39 -04008600 * @phba: pointer to lpfc hba data structure.
8601 *
James Smart895427b2017-02-12 13:52:30 -08008602 * This routine is invoked to check the user settable queue counts for EQs.
8603 * After this routine is called the counts will be set to valid values that
James Smart5350d872011-10-10 21:33:49 -04008604 * adhere to the constraints of the system's interrupt vectors and the port's
8605 * queue resources.
James Smartda0436e2009-05-22 14:51:39 -04008606 *
8607 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008608 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008609 * -ENOMEM - No available memory
James Smartda0436e2009-05-22 14:51:39 -04008610 **/
8611static int
James Smart5350d872011-10-10 21:33:49 -04008612lpfc_sli4_queue_verify(struct lpfc_hba *phba)
James Smartda0436e2009-05-22 14:51:39 -04008613{
James Smartda0436e2009-05-22 14:51:39 -04008614 /*
James Smart67d12732012-08-03 12:36:13 -04008615 * Sanity check for configured queue parameters against the run-time
James Smartda0436e2009-05-22 14:51:39 -04008616 * device parameters
8617 */
8618
James Smartbcb24f62017-11-20 16:00:36 -08008619 if (phba->nvmet_support) {
James Smart6a828b02019-01-28 11:14:31 -08008620 if (phba->cfg_irq_chann < phba->cfg_nvmet_mrq)
8621 phba->cfg_nvmet_mrq = phba->cfg_irq_chann;
James Smart982ab122019-03-12 16:30:10 -07008622 if (phba->cfg_nvmet_mrq > LPFC_NVMET_MRQ_MAX)
8623 phba->cfg_nvmet_mrq = LPFC_NVMET_MRQ_MAX;
James Smartbcb24f62017-11-20 16:00:36 -08008624 }
James Smart895427b2017-02-12 13:52:30 -08008625
8626 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart6a828b02019-01-28 11:14:31 -08008627 "2574 IO channels: hdwQ %d IRQ %d MRQ: %d\n",
8628 phba->cfg_hdw_queue, phba->cfg_irq_chann,
8629 phba->cfg_nvmet_mrq);
James Smartda0436e2009-05-22 14:51:39 -04008630
James Smartda0436e2009-05-22 14:51:39 -04008631 /* Get EQ depth from module parameter, fake the default for now */
8632 phba->sli4_hba.eq_esize = LPFC_EQE_SIZE_4B;
8633 phba->sli4_hba.eq_ecount = LPFC_EQE_DEF_COUNT;
8634
James Smart5350d872011-10-10 21:33:49 -04008635 /* Get CQ depth from module parameter, fake the default for now */
8636 phba->sli4_hba.cq_esize = LPFC_CQE_SIZE;
8637 phba->sli4_hba.cq_ecount = LPFC_CQE_DEF_COUNT;
James Smart5350d872011-10-10 21:33:49 -04008638 return 0;
James Smart895427b2017-02-12 13:52:30 -08008639}
8640
8641static int
8642lpfc_alloc_nvme_wq_cq(struct lpfc_hba *phba, int wqidx)
8643{
8644 struct lpfc_queue *qdesc;
James Smartc1a21eb2019-03-12 16:30:29 -07008645 int cpu;
James Smart895427b2017-02-12 13:52:30 -08008646
James Smartc1a21eb2019-03-12 16:30:29 -07008647 cpu = lpfc_find_cpu_handle(phba, wqidx, LPFC_FIND_BY_HDWQ);
James Smarta51e41b2017-12-08 17:18:06 -08008648 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_EXPANDED_PAGE_SIZE,
James Smart81b96ed2017-11-20 16:00:29 -08008649 phba->sli4_hba.cq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -07008650 LPFC_CQE_EXP_COUNT, cpu);
James Smart895427b2017-02-12 13:52:30 -08008651 if (!qdesc) {
8652 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8653 "0508 Failed allocate fast-path NVME CQ (%d)\n",
8654 wqidx);
8655 return 1;
8656 }
James Smart7365f6f2018-02-22 08:18:46 -08008657 qdesc->qe_valid = 1;
James Smart5e5b5112019-01-28 11:14:22 -08008658 qdesc->hdwq = wqidx;
James Smartc1a21eb2019-03-12 16:30:29 -07008659 qdesc->chann = cpu;
James Smartcdb42be2019-01-28 11:14:21 -08008660 phba->sli4_hba.hdwq[wqidx].nvme_cq = qdesc;
James Smart895427b2017-02-12 13:52:30 -08008661
James Smarta51e41b2017-12-08 17:18:06 -08008662 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_EXPANDED_PAGE_SIZE,
James Smartc1a21eb2019-03-12 16:30:29 -07008663 LPFC_WQE128_SIZE, LPFC_WQE_EXP_COUNT,
8664 cpu);
James Smart895427b2017-02-12 13:52:30 -08008665 if (!qdesc) {
8666 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8667 "0509 Failed allocate fast-path NVME WQ (%d)\n",
8668 wqidx);
8669 return 1;
8670 }
James Smart5e5b5112019-01-28 11:14:22 -08008671 qdesc->hdwq = wqidx;
James Smart6a828b02019-01-28 11:14:31 -08008672 qdesc->chann = wqidx;
James Smartcdb42be2019-01-28 11:14:21 -08008673 phba->sli4_hba.hdwq[wqidx].nvme_wq = qdesc;
James Smart895427b2017-02-12 13:52:30 -08008674 list_add_tail(&qdesc->wq_list, &phba->sli4_hba.lpfc_wq_list);
8675 return 0;
8676}
8677
8678static int
8679lpfc_alloc_fcp_wq_cq(struct lpfc_hba *phba, int wqidx)
8680{
8681 struct lpfc_queue *qdesc;
James Smartc176ffa2018-01-30 15:58:46 -08008682 uint32_t wqesize;
James Smartc1a21eb2019-03-12 16:30:29 -07008683 int cpu;
James Smart895427b2017-02-12 13:52:30 -08008684
James Smartc1a21eb2019-03-12 16:30:29 -07008685 cpu = lpfc_find_cpu_handle(phba, wqidx, LPFC_FIND_BY_HDWQ);
James Smart895427b2017-02-12 13:52:30 -08008686 /* Create Fast Path FCP CQs */
James Smartc176ffa2018-01-30 15:58:46 -08008687 if (phba->enab_exp_wqcq_pages)
James Smarta51e41b2017-12-08 17:18:06 -08008688 /* Increase the CQ size when WQEs contain an embedded cdb */
8689 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_EXPANDED_PAGE_SIZE,
8690 phba->sli4_hba.cq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -07008691 LPFC_CQE_EXP_COUNT, cpu);
James Smarta51e41b2017-12-08 17:18:06 -08008692
8693 else
8694 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
8695 phba->sli4_hba.cq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -07008696 phba->sli4_hba.cq_ecount, cpu);
James Smart895427b2017-02-12 13:52:30 -08008697 if (!qdesc) {
8698 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8699 "0499 Failed allocate fast-path FCP CQ (%d)\n", wqidx);
8700 return 1;
8701 }
James Smart7365f6f2018-02-22 08:18:46 -08008702 qdesc->qe_valid = 1;
James Smart5e5b5112019-01-28 11:14:22 -08008703 qdesc->hdwq = wqidx;
James Smartc1a21eb2019-03-12 16:30:29 -07008704 qdesc->chann = cpu;
James Smartcdb42be2019-01-28 11:14:21 -08008705 phba->sli4_hba.hdwq[wqidx].fcp_cq = qdesc;
James Smart895427b2017-02-12 13:52:30 -08008706
8707 /* Create Fast Path FCP WQs */
James Smartc176ffa2018-01-30 15:58:46 -08008708 if (phba->enab_exp_wqcq_pages) {
James Smarta51e41b2017-12-08 17:18:06 -08008709 /* Increase the WQ size when WQEs contain an embedded cdb */
James Smartc176ffa2018-01-30 15:58:46 -08008710 wqesize = (phba->fcp_embed_io) ?
8711 LPFC_WQE128_SIZE : phba->sli4_hba.wq_esize;
James Smarta51e41b2017-12-08 17:18:06 -08008712 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_EXPANDED_PAGE_SIZE,
James Smartc176ffa2018-01-30 15:58:46 -08008713 wqesize,
James Smartc1a21eb2019-03-12 16:30:29 -07008714 LPFC_WQE_EXP_COUNT, cpu);
James Smartc176ffa2018-01-30 15:58:46 -08008715 } else
James Smarta51e41b2017-12-08 17:18:06 -08008716 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
8717 phba->sli4_hba.wq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -07008718 phba->sli4_hba.wq_ecount, cpu);
James Smartc176ffa2018-01-30 15:58:46 -08008719
James Smart895427b2017-02-12 13:52:30 -08008720 if (!qdesc) {
8721 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8722 "0503 Failed allocate fast-path FCP WQ (%d)\n",
8723 wqidx);
8724 return 1;
8725 }
James Smart5e5b5112019-01-28 11:14:22 -08008726 qdesc->hdwq = wqidx;
James Smart6a828b02019-01-28 11:14:31 -08008727 qdesc->chann = wqidx;
James Smartcdb42be2019-01-28 11:14:21 -08008728 phba->sli4_hba.hdwq[wqidx].fcp_wq = qdesc;
James Smart895427b2017-02-12 13:52:30 -08008729 list_add_tail(&qdesc->wq_list, &phba->sli4_hba.lpfc_wq_list);
8730 return 0;
James Smart5350d872011-10-10 21:33:49 -04008731}
8732
8733/**
8734 * lpfc_sli4_queue_create - Create all the SLI4 queues
8735 * @phba: pointer to lpfc hba data structure.
8736 *
8737 * This routine is invoked to allocate all the SLI4 queues for the FCoE HBA
8738 * operation. For each SLI4 queue type, the parameters such as queue entry
8739 * count (queue depth) shall be taken from the module parameter. For now,
8740 * we just use some constant number as place holder.
8741 *
8742 * Return codes
Anatol Pomozov4907cb72012-09-01 10:31:09 -07008743 * 0 - successful
James Smart5350d872011-10-10 21:33:49 -04008744 * -ENOMEM - No availble memory
8745 * -EIO - The mailbox failed to complete successfully.
8746 **/
8747int
8748lpfc_sli4_queue_create(struct lpfc_hba *phba)
8749{
8750 struct lpfc_queue *qdesc;
James Smartc1a21eb2019-03-12 16:30:29 -07008751 int idx, eqidx, cpu;
James Smart5e5b5112019-01-28 11:14:22 -08008752 struct lpfc_sli4_hdw_queue *qp;
James Smart32517fc2019-01-28 11:14:33 -08008753 struct lpfc_eq_intr_info *eqi;
James Smart5350d872011-10-10 21:33:49 -04008754
8755 /*
James Smart67d12732012-08-03 12:36:13 -04008756 * Create HBA Record arrays.
James Smart895427b2017-02-12 13:52:30 -08008757 * Both NVME and FCP will share that same vectors / EQs
James Smart5350d872011-10-10 21:33:49 -04008758 */
James Smart67d12732012-08-03 12:36:13 -04008759 phba->sli4_hba.mq_esize = LPFC_MQE_SIZE;
8760 phba->sli4_hba.mq_ecount = LPFC_MQE_DEF_COUNT;
8761 phba->sli4_hba.wq_esize = LPFC_WQE_SIZE;
8762 phba->sli4_hba.wq_ecount = LPFC_WQE_DEF_COUNT;
8763 phba->sli4_hba.rq_esize = LPFC_RQE_SIZE;
8764 phba->sli4_hba.rq_ecount = LPFC_RQE_DEF_COUNT;
James Smart895427b2017-02-12 13:52:30 -08008765 phba->sli4_hba.eq_esize = LPFC_EQE_SIZE_4B;
8766 phba->sli4_hba.eq_ecount = LPFC_EQE_DEF_COUNT;
8767 phba->sli4_hba.cq_esize = LPFC_CQE_SIZE;
8768 phba->sli4_hba.cq_ecount = LPFC_CQE_DEF_COUNT;
James Smart67d12732012-08-03 12:36:13 -04008769
James Smartcdb42be2019-01-28 11:14:21 -08008770 if (!phba->sli4_hba.hdwq) {
James Smart5e5b5112019-01-28 11:14:22 -08008771 phba->sli4_hba.hdwq = kcalloc(
8772 phba->cfg_hdw_queue, sizeof(struct lpfc_sli4_hdw_queue),
8773 GFP_KERNEL);
8774 if (!phba->sli4_hba.hdwq) {
James Smart895427b2017-02-12 13:52:30 -08008775 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart5e5b5112019-01-28 11:14:22 -08008776 "6427 Failed allocate memory for "
8777 "fast-path Hardware Queue array\n");
James Smart895427b2017-02-12 13:52:30 -08008778 goto out_error;
8779 }
James Smart5e5b5112019-01-28 11:14:22 -08008780 /* Prepare hardware queues to take IO buffers */
8781 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
8782 qp = &phba->sli4_hba.hdwq[idx];
8783 spin_lock_init(&qp->io_buf_list_get_lock);
8784 spin_lock_init(&qp->io_buf_list_put_lock);
8785 INIT_LIST_HEAD(&qp->lpfc_io_buf_list_get);
8786 INIT_LIST_HEAD(&qp->lpfc_io_buf_list_put);
8787 qp->get_io_bufs = 0;
8788 qp->put_io_bufs = 0;
8789 qp->total_io_bufs = 0;
8790 spin_lock_init(&qp->abts_scsi_buf_list_lock);
8791 INIT_LIST_HEAD(&qp->lpfc_abts_scsi_buf_list);
8792 qp->abts_scsi_io_bufs = 0;
8793 spin_lock_init(&qp->abts_nvme_buf_list_lock);
8794 INIT_LIST_HEAD(&qp->lpfc_abts_nvme_buf_list);
8795 qp->abts_nvme_io_bufs = 0;
James Smart895427b2017-02-12 13:52:30 -08008796 }
James Smart67d12732012-08-03 12:36:13 -04008797 }
8798
James Smartcdb42be2019-01-28 11:14:21 -08008799 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
James Smart2d7dbc42017-02-12 13:52:35 -08008800 if (phba->nvmet_support) {
8801 phba->sli4_hba.nvmet_cqset = kcalloc(
8802 phba->cfg_nvmet_mrq,
8803 sizeof(struct lpfc_queue *),
8804 GFP_KERNEL);
8805 if (!phba->sli4_hba.nvmet_cqset) {
8806 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8807 "3121 Fail allocate memory for "
8808 "fast-path CQ set array\n");
8809 goto out_error;
8810 }
8811 phba->sli4_hba.nvmet_mrq_hdr = kcalloc(
8812 phba->cfg_nvmet_mrq,
8813 sizeof(struct lpfc_queue *),
8814 GFP_KERNEL);
8815 if (!phba->sli4_hba.nvmet_mrq_hdr) {
8816 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8817 "3122 Fail allocate memory for "
8818 "fast-path RQ set hdr array\n");
8819 goto out_error;
8820 }
8821 phba->sli4_hba.nvmet_mrq_data = kcalloc(
8822 phba->cfg_nvmet_mrq,
8823 sizeof(struct lpfc_queue *),
8824 GFP_KERNEL);
8825 if (!phba->sli4_hba.nvmet_mrq_data) {
8826 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8827 "3124 Fail allocate memory for "
8828 "fast-path RQ set data array\n");
8829 goto out_error;
8830 }
8831 }
James Smart67d12732012-08-03 12:36:13 -04008832 }
James Smartda0436e2009-05-22 14:51:39 -04008833
James Smart895427b2017-02-12 13:52:30 -08008834 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_wq_list);
James Smart67d12732012-08-03 12:36:13 -04008835
James Smart895427b2017-02-12 13:52:30 -08008836 /* Create HBA Event Queues (EQs) */
James Smartcdb42be2019-01-28 11:14:21 -08008837 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
James Smartc1a21eb2019-03-12 16:30:29 -07008838 /* determine EQ affinity */
8839 eqidx = lpfc_find_eq_handle(phba, idx);
8840 cpu = lpfc_find_cpu_handle(phba, eqidx, LPFC_FIND_BY_EQ);
James Smart6a828b02019-01-28 11:14:31 -08008841 /*
8842 * If there are more Hardware Queues than available
James Smartc1a21eb2019-03-12 16:30:29 -07008843 * EQs, multiple Hardware Queues may share a common EQ.
James Smart6a828b02019-01-28 11:14:31 -08008844 */
8845 if (idx >= phba->cfg_irq_chann) {
8846 /* Share an existing EQ */
James Smart6a828b02019-01-28 11:14:31 -08008847 phba->sli4_hba.hdwq[idx].hba_eq =
8848 phba->sli4_hba.hdwq[eqidx].hba_eq;
8849 continue;
8850 }
8851 /* Create an EQ */
James Smart81b96ed2017-11-20 16:00:29 -08008852 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
8853 phba->sli4_hba.eq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -07008854 phba->sli4_hba.eq_ecount, cpu);
James Smartda0436e2009-05-22 14:51:39 -04008855 if (!qdesc) {
8856 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart67d12732012-08-03 12:36:13 -04008857 "0497 Failed allocate EQ (%d)\n", idx);
8858 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04008859 }
James Smart7365f6f2018-02-22 08:18:46 -08008860 qdesc->qe_valid = 1;
James Smart5e5b5112019-01-28 11:14:22 -08008861 qdesc->hdwq = idx;
James Smart6a828b02019-01-28 11:14:31 -08008862
8863 /* Save the CPU this EQ is affinitised to */
James Smartc1a21eb2019-03-12 16:30:29 -07008864 qdesc->chann = cpu;
James Smartcdb42be2019-01-28 11:14:21 -08008865 phba->sli4_hba.hdwq[idx].hba_eq = qdesc;
James Smart32517fc2019-01-28 11:14:33 -08008866 qdesc->last_cpu = qdesc->chann;
8867 eqi = per_cpu_ptr(phba->sli4_hba.eq_info, qdesc->last_cpu);
8868 list_add(&qdesc->cpu_list, &eqi->list);
James Smartda0436e2009-05-22 14:51:39 -04008869 }
8870
James Smart895427b2017-02-12 13:52:30 -08008871
James Smartcdb42be2019-01-28 11:14:21 -08008872 /* Allocate SCSI SLI4 CQ/WQs */
James Smart6a828b02019-01-28 11:14:31 -08008873 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
James Smart895427b2017-02-12 13:52:30 -08008874 if (lpfc_alloc_fcp_wq_cq(phba, idx))
8875 goto out_error;
James Smart6a828b02019-01-28 11:14:31 -08008876 }
James Smart895427b2017-02-12 13:52:30 -08008877
James Smartcdb42be2019-01-28 11:14:21 -08008878 /* Allocate NVME SLI4 CQ/WQs */
8879 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
James Smart6a828b02019-01-28 11:14:31 -08008880 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
James Smartcdb42be2019-01-28 11:14:21 -08008881 if (lpfc_alloc_nvme_wq_cq(phba, idx))
8882 goto out_error;
James Smart6a828b02019-01-28 11:14:31 -08008883 }
James Smart67d12732012-08-03 12:36:13 -04008884
James Smartcdb42be2019-01-28 11:14:21 -08008885 if (phba->nvmet_support) {
8886 for (idx = 0; idx < phba->cfg_nvmet_mrq; idx++) {
James Smartc1a21eb2019-03-12 16:30:29 -07008887 cpu = lpfc_find_cpu_handle(phba, idx,
8888 LPFC_FIND_BY_HDWQ);
James Smartcdb42be2019-01-28 11:14:21 -08008889 qdesc = lpfc_sli4_queue_alloc(
8890 phba,
James Smart81b96ed2017-11-20 16:00:29 -08008891 LPFC_DEFAULT_PAGE_SIZE,
8892 phba->sli4_hba.cq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -07008893 phba->sli4_hba.cq_ecount,
8894 cpu);
James Smartcdb42be2019-01-28 11:14:21 -08008895 if (!qdesc) {
8896 lpfc_printf_log(
8897 phba, KERN_ERR, LOG_INIT,
8898 "3142 Failed allocate NVME "
8899 "CQ Set (%d)\n", idx);
8900 goto out_error;
8901 }
8902 qdesc->qe_valid = 1;
James Smart5e5b5112019-01-28 11:14:22 -08008903 qdesc->hdwq = idx;
James Smartc1a21eb2019-03-12 16:30:29 -07008904 qdesc->chann = cpu;
James Smartcdb42be2019-01-28 11:14:21 -08008905 phba->sli4_hba.nvmet_cqset[idx] = qdesc;
James Smart2d7dbc42017-02-12 13:52:35 -08008906 }
James Smart2d7dbc42017-02-12 13:52:35 -08008907 }
8908 }
8909
James Smartda0436e2009-05-22 14:51:39 -04008910 /*
James Smart67d12732012-08-03 12:36:13 -04008911 * Create Slow Path Completion Queues (CQs)
James Smartda0436e2009-05-22 14:51:39 -04008912 */
8913
James Smartc1a21eb2019-03-12 16:30:29 -07008914 cpu = lpfc_find_cpu_handle(phba, 0, LPFC_FIND_BY_EQ);
James Smartda0436e2009-05-22 14:51:39 -04008915 /* Create slow-path Mailbox Command Complete Queue */
James Smart81b96ed2017-11-20 16:00:29 -08008916 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
8917 phba->sli4_hba.cq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -07008918 phba->sli4_hba.cq_ecount, cpu);
James Smartda0436e2009-05-22 14:51:39 -04008919 if (!qdesc) {
8920 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8921 "0500 Failed allocate slow-path mailbox CQ\n");
James Smart67d12732012-08-03 12:36:13 -04008922 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04008923 }
James Smart7365f6f2018-02-22 08:18:46 -08008924 qdesc->qe_valid = 1;
James Smartda0436e2009-05-22 14:51:39 -04008925 phba->sli4_hba.mbx_cq = qdesc;
8926
8927 /* Create slow-path ELS Complete Queue */
James Smart81b96ed2017-11-20 16:00:29 -08008928 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
8929 phba->sli4_hba.cq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -07008930 phba->sli4_hba.cq_ecount, cpu);
James Smartda0436e2009-05-22 14:51:39 -04008931 if (!qdesc) {
8932 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8933 "0501 Failed allocate slow-path ELS CQ\n");
James Smart67d12732012-08-03 12:36:13 -04008934 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04008935 }
James Smart7365f6f2018-02-22 08:18:46 -08008936 qdesc->qe_valid = 1;
James Smart6a828b02019-01-28 11:14:31 -08008937 qdesc->chann = 0;
James Smartda0436e2009-05-22 14:51:39 -04008938 phba->sli4_hba.els_cq = qdesc;
8939
James Smartda0436e2009-05-22 14:51:39 -04008940
James Smart5350d872011-10-10 21:33:49 -04008941 /*
James Smart67d12732012-08-03 12:36:13 -04008942 * Create Slow Path Work Queues (WQs)
James Smart5350d872011-10-10 21:33:49 -04008943 */
James Smartda0436e2009-05-22 14:51:39 -04008944
8945 /* Create Mailbox Command Queue */
James Smartda0436e2009-05-22 14:51:39 -04008946
James Smart81b96ed2017-11-20 16:00:29 -08008947 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
8948 phba->sli4_hba.mq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -07008949 phba->sli4_hba.mq_ecount, cpu);
James Smartda0436e2009-05-22 14:51:39 -04008950 if (!qdesc) {
8951 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8952 "0505 Failed allocate slow-path MQ\n");
James Smart67d12732012-08-03 12:36:13 -04008953 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04008954 }
James Smart6a828b02019-01-28 11:14:31 -08008955 qdesc->chann = 0;
James Smartda0436e2009-05-22 14:51:39 -04008956 phba->sli4_hba.mbx_wq = qdesc;
8957
8958 /*
James Smart67d12732012-08-03 12:36:13 -04008959 * Create ELS Work Queues
James Smartda0436e2009-05-22 14:51:39 -04008960 */
James Smartda0436e2009-05-22 14:51:39 -04008961
8962 /* Create slow-path ELS Work Queue */
James Smart81b96ed2017-11-20 16:00:29 -08008963 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
8964 phba->sli4_hba.wq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -07008965 phba->sli4_hba.wq_ecount, cpu);
James Smartda0436e2009-05-22 14:51:39 -04008966 if (!qdesc) {
8967 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8968 "0504 Failed allocate slow-path ELS WQ\n");
James Smart67d12732012-08-03 12:36:13 -04008969 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04008970 }
James Smart6a828b02019-01-28 11:14:31 -08008971 qdesc->chann = 0;
James Smartda0436e2009-05-22 14:51:39 -04008972 phba->sli4_hba.els_wq = qdesc;
James Smart895427b2017-02-12 13:52:30 -08008973 list_add_tail(&qdesc->wq_list, &phba->sli4_hba.lpfc_wq_list);
8974
8975 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
8976 /* Create NVME LS Complete Queue */
James Smart81b96ed2017-11-20 16:00:29 -08008977 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
8978 phba->sli4_hba.cq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -07008979 phba->sli4_hba.cq_ecount, cpu);
James Smart895427b2017-02-12 13:52:30 -08008980 if (!qdesc) {
8981 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8982 "6079 Failed allocate NVME LS CQ\n");
8983 goto out_error;
8984 }
James Smart6a828b02019-01-28 11:14:31 -08008985 qdesc->chann = 0;
James Smart7365f6f2018-02-22 08:18:46 -08008986 qdesc->qe_valid = 1;
James Smart895427b2017-02-12 13:52:30 -08008987 phba->sli4_hba.nvmels_cq = qdesc;
8988
8989 /* Create NVME LS Work Queue */
James Smart81b96ed2017-11-20 16:00:29 -08008990 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
8991 phba->sli4_hba.wq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -07008992 phba->sli4_hba.wq_ecount, cpu);
James Smart895427b2017-02-12 13:52:30 -08008993 if (!qdesc) {
8994 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8995 "6080 Failed allocate NVME LS WQ\n");
8996 goto out_error;
8997 }
James Smart6a828b02019-01-28 11:14:31 -08008998 qdesc->chann = 0;
James Smart895427b2017-02-12 13:52:30 -08008999 phba->sli4_hba.nvmels_wq = qdesc;
9000 list_add_tail(&qdesc->wq_list, &phba->sli4_hba.lpfc_wq_list);
9001 }
James Smartda0436e2009-05-22 14:51:39 -04009002
James Smartda0436e2009-05-22 14:51:39 -04009003 /*
9004 * Create Receive Queue (RQ)
9005 */
James Smartda0436e2009-05-22 14:51:39 -04009006
9007 /* Create Receive Queue for header */
James Smart81b96ed2017-11-20 16:00:29 -08009008 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
9009 phba->sli4_hba.rq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -07009010 phba->sli4_hba.rq_ecount, cpu);
James Smartda0436e2009-05-22 14:51:39 -04009011 if (!qdesc) {
9012 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9013 "0506 Failed allocate receive HRQ\n");
James Smart67d12732012-08-03 12:36:13 -04009014 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04009015 }
9016 phba->sli4_hba.hdr_rq = qdesc;
9017
9018 /* Create Receive Queue for data */
James Smart81b96ed2017-11-20 16:00:29 -08009019 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
9020 phba->sli4_hba.rq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -07009021 phba->sli4_hba.rq_ecount, cpu);
James Smartda0436e2009-05-22 14:51:39 -04009022 if (!qdesc) {
9023 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9024 "0507 Failed allocate receive DRQ\n");
James Smart67d12732012-08-03 12:36:13 -04009025 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04009026 }
9027 phba->sli4_hba.dat_rq = qdesc;
9028
James Smartcdb42be2019-01-28 11:14:21 -08009029 if ((phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) &&
9030 phba->nvmet_support) {
James Smart2d7dbc42017-02-12 13:52:35 -08009031 for (idx = 0; idx < phba->cfg_nvmet_mrq; idx++) {
James Smartc1a21eb2019-03-12 16:30:29 -07009032 cpu = lpfc_find_cpu_handle(phba, idx,
9033 LPFC_FIND_BY_HDWQ);
James Smart2d7dbc42017-02-12 13:52:35 -08009034 /* Create NVMET Receive Queue for header */
9035 qdesc = lpfc_sli4_queue_alloc(phba,
James Smart81b96ed2017-11-20 16:00:29 -08009036 LPFC_DEFAULT_PAGE_SIZE,
James Smart2d7dbc42017-02-12 13:52:35 -08009037 phba->sli4_hba.rq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -07009038 LPFC_NVMET_RQE_DEF_COUNT,
9039 cpu);
James Smart2d7dbc42017-02-12 13:52:35 -08009040 if (!qdesc) {
9041 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9042 "3146 Failed allocate "
9043 "receive HRQ\n");
9044 goto out_error;
9045 }
James Smart5e5b5112019-01-28 11:14:22 -08009046 qdesc->hdwq = idx;
James Smart2d7dbc42017-02-12 13:52:35 -08009047 phba->sli4_hba.nvmet_mrq_hdr[idx] = qdesc;
9048
9049 /* Only needed for header of RQ pair */
James Smartc1a21eb2019-03-12 16:30:29 -07009050 qdesc->rqbp = kzalloc_node(sizeof(*qdesc->rqbp),
9051 GFP_KERNEL,
9052 cpu_to_node(cpu));
James Smart2d7dbc42017-02-12 13:52:35 -08009053 if (qdesc->rqbp == NULL) {
9054 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9055 "6131 Failed allocate "
9056 "Header RQBP\n");
9057 goto out_error;
9058 }
9059
Dick Kennedy4b40d022017-08-23 16:55:38 -07009060 /* Put list in known state in case driver load fails. */
9061 INIT_LIST_HEAD(&qdesc->rqbp->rqb_buffer_list);
9062
James Smart2d7dbc42017-02-12 13:52:35 -08009063 /* Create NVMET Receive Queue for data */
9064 qdesc = lpfc_sli4_queue_alloc(phba,
James Smart81b96ed2017-11-20 16:00:29 -08009065 LPFC_DEFAULT_PAGE_SIZE,
James Smart2d7dbc42017-02-12 13:52:35 -08009066 phba->sli4_hba.rq_esize,
James Smartc1a21eb2019-03-12 16:30:29 -07009067 LPFC_NVMET_RQE_DEF_COUNT,
9068 cpu);
James Smart2d7dbc42017-02-12 13:52:35 -08009069 if (!qdesc) {
9070 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9071 "3156 Failed allocate "
9072 "receive DRQ\n");
9073 goto out_error;
9074 }
James Smart5e5b5112019-01-28 11:14:22 -08009075 qdesc->hdwq = idx;
James Smart2d7dbc42017-02-12 13:52:35 -08009076 phba->sli4_hba.nvmet_mrq_data[idx] = qdesc;
9077 }
9078 }
9079
James Smart4c47efc2019-01-28 11:14:25 -08009080#if defined(BUILD_NVME)
9081 /* Clear NVME stats */
9082 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
9083 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
9084 memset(&phba->sli4_hba.hdwq[idx].nvme_cstat, 0,
9085 sizeof(phba->sli4_hba.hdwq[idx].nvme_cstat));
9086 }
9087 }
9088#endif
9089
9090 /* Clear SCSI stats */
9091 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) {
9092 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
9093 memset(&phba->sli4_hba.hdwq[idx].scsi_cstat, 0,
9094 sizeof(phba->sli4_hba.hdwq[idx].scsi_cstat));
9095 }
9096 }
9097
James Smartda0436e2009-05-22 14:51:39 -04009098 return 0;
9099
James Smartda0436e2009-05-22 14:51:39 -04009100out_error:
James Smart67d12732012-08-03 12:36:13 -04009101 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04009102 return -ENOMEM;
9103}
9104
James Smart895427b2017-02-12 13:52:30 -08009105static inline void
9106__lpfc_sli4_release_queue(struct lpfc_queue **qp)
9107{
9108 if (*qp != NULL) {
9109 lpfc_sli4_queue_free(*qp);
9110 *qp = NULL;
9111 }
9112}
9113
9114static inline void
9115lpfc_sli4_release_queues(struct lpfc_queue ***qs, int max)
9116{
9117 int idx;
9118
9119 if (*qs == NULL)
9120 return;
9121
9122 for (idx = 0; idx < max; idx++)
9123 __lpfc_sli4_release_queue(&(*qs)[idx]);
9124
9125 kfree(*qs);
9126 *qs = NULL;
9127}
9128
9129static inline void
James Smart6a828b02019-01-28 11:14:31 -08009130lpfc_sli4_release_hdwq(struct lpfc_hba *phba)
James Smart895427b2017-02-12 13:52:30 -08009131{
James Smart6a828b02019-01-28 11:14:31 -08009132 struct lpfc_sli4_hdw_queue *hdwq;
James Smartcdb42be2019-01-28 11:14:21 -08009133 uint32_t idx;
9134
James Smart6a828b02019-01-28 11:14:31 -08009135 hdwq = phba->sli4_hba.hdwq;
9136 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
9137 if (idx < phba->cfg_irq_chann)
9138 lpfc_sli4_queue_free(hdwq[idx].hba_eq);
9139 hdwq[idx].hba_eq = NULL;
9140
James Smartcdb42be2019-01-28 11:14:21 -08009141 lpfc_sli4_queue_free(hdwq[idx].fcp_cq);
9142 lpfc_sli4_queue_free(hdwq[idx].nvme_cq);
9143 lpfc_sli4_queue_free(hdwq[idx].fcp_wq);
9144 lpfc_sli4_queue_free(hdwq[idx].nvme_wq);
James Smartcdb42be2019-01-28 11:14:21 -08009145 hdwq[idx].fcp_cq = NULL;
9146 hdwq[idx].nvme_cq = NULL;
9147 hdwq[idx].fcp_wq = NULL;
9148 hdwq[idx].nvme_wq = NULL;
James Smart895427b2017-02-12 13:52:30 -08009149 }
9150}
9151
James Smartda0436e2009-05-22 14:51:39 -04009152/**
9153 * lpfc_sli4_queue_destroy - Destroy all the SLI4 queues
9154 * @phba: pointer to lpfc hba data structure.
9155 *
9156 * This routine is invoked to release all the SLI4 queues with the FCoE HBA
9157 * operation.
9158 *
9159 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02009160 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009161 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04009162 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04009163 **/
James Smart5350d872011-10-10 21:33:49 -04009164void
James Smartda0436e2009-05-22 14:51:39 -04009165lpfc_sli4_queue_destroy(struct lpfc_hba *phba)
9166{
James Smart4645f7b2019-03-12 16:30:14 -07009167 /*
9168 * Set FREE_INIT before beginning to free the queues.
9169 * Wait until the users of queues to acknowledge to
9170 * release queues by clearing FREE_WAIT.
9171 */
9172 spin_lock_irq(&phba->hbalock);
9173 phba->sli.sli_flag |= LPFC_QUEUE_FREE_INIT;
9174 while (phba->sli.sli_flag & LPFC_QUEUE_FREE_WAIT) {
9175 spin_unlock_irq(&phba->hbalock);
9176 msleep(20);
9177 spin_lock_irq(&phba->hbalock);
9178 }
9179 spin_unlock_irq(&phba->hbalock);
9180
James Smart895427b2017-02-12 13:52:30 -08009181 /* Release HBA eqs */
James Smartcdb42be2019-01-28 11:14:21 -08009182 if (phba->sli4_hba.hdwq)
James Smart6a828b02019-01-28 11:14:31 -08009183 lpfc_sli4_release_hdwq(phba);
James Smartda0436e2009-05-22 14:51:39 -04009184
James Smartbcb24f62017-11-20 16:00:36 -08009185 if (phba->nvmet_support) {
9186 lpfc_sli4_release_queues(&phba->sli4_hba.nvmet_cqset,
9187 phba->cfg_nvmet_mrq);
James Smart2d7dbc42017-02-12 13:52:35 -08009188
James Smartbcb24f62017-11-20 16:00:36 -08009189 lpfc_sli4_release_queues(&phba->sli4_hba.nvmet_mrq_hdr,
9190 phba->cfg_nvmet_mrq);
9191 lpfc_sli4_release_queues(&phba->sli4_hba.nvmet_mrq_data,
9192 phba->cfg_nvmet_mrq);
9193 }
James Smart2d7dbc42017-02-12 13:52:35 -08009194
James Smartda0436e2009-05-22 14:51:39 -04009195 /* Release mailbox command work queue */
James Smart895427b2017-02-12 13:52:30 -08009196 __lpfc_sli4_release_queue(&phba->sli4_hba.mbx_wq);
James Smartda0436e2009-05-22 14:51:39 -04009197
9198 /* Release ELS work queue */
James Smart895427b2017-02-12 13:52:30 -08009199 __lpfc_sli4_release_queue(&phba->sli4_hba.els_wq);
9200
9201 /* Release ELS work queue */
9202 __lpfc_sli4_release_queue(&phba->sli4_hba.nvmels_wq);
James Smartda0436e2009-05-22 14:51:39 -04009203
9204 /* Release unsolicited receive queue */
James Smart895427b2017-02-12 13:52:30 -08009205 __lpfc_sli4_release_queue(&phba->sli4_hba.hdr_rq);
9206 __lpfc_sli4_release_queue(&phba->sli4_hba.dat_rq);
James Smartda0436e2009-05-22 14:51:39 -04009207
James Smartda0436e2009-05-22 14:51:39 -04009208 /* Release ELS complete queue */
James Smart895427b2017-02-12 13:52:30 -08009209 __lpfc_sli4_release_queue(&phba->sli4_hba.els_cq);
9210
9211 /* Release NVME LS complete queue */
9212 __lpfc_sli4_release_queue(&phba->sli4_hba.nvmels_cq);
James Smartda0436e2009-05-22 14:51:39 -04009213
9214 /* Release mailbox command complete queue */
James Smart895427b2017-02-12 13:52:30 -08009215 __lpfc_sli4_release_queue(&phba->sli4_hba.mbx_cq);
9216
9217 /* Everything on this list has been freed */
9218 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_wq_list);
James Smart4645f7b2019-03-12 16:30:14 -07009219
9220 /* Done with freeing the queues */
9221 spin_lock_irq(&phba->hbalock);
9222 phba->sli.sli_flag &= ~LPFC_QUEUE_FREE_INIT;
9223 spin_unlock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -08009224}
9225
9226int
James Smart895427b2017-02-12 13:52:30 -08009227lpfc_free_rq_buffer(struct lpfc_hba *phba, struct lpfc_queue *rq)
9228{
9229 struct lpfc_rqb *rqbp;
9230 struct lpfc_dmabuf *h_buf;
9231 struct rqb_dmabuf *rqb_buffer;
9232
9233 rqbp = rq->rqbp;
9234 while (!list_empty(&rqbp->rqb_buffer_list)) {
9235 list_remove_head(&rqbp->rqb_buffer_list, h_buf,
9236 struct lpfc_dmabuf, list);
9237
9238 rqb_buffer = container_of(h_buf, struct rqb_dmabuf, hbuf);
9239 (rqbp->rqb_free_buffer)(phba, rqb_buffer);
9240 rqbp->buffer_count--;
9241 }
9242 return 1;
9243}
9244
9245static int
9246lpfc_create_wq_cq(struct lpfc_hba *phba, struct lpfc_queue *eq,
9247 struct lpfc_queue *cq, struct lpfc_queue *wq, uint16_t *cq_map,
9248 int qidx, uint32_t qtype)
9249{
9250 struct lpfc_sli_ring *pring;
9251 int rc;
9252
9253 if (!eq || !cq || !wq) {
9254 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9255 "6085 Fast-path %s (%d) not allocated\n",
9256 ((eq) ? ((cq) ? "WQ" : "CQ") : "EQ"), qidx);
9257 return -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04009258 }
James Smartda0436e2009-05-22 14:51:39 -04009259
James Smart895427b2017-02-12 13:52:30 -08009260 /* create the Cq first */
9261 rc = lpfc_cq_create(phba, cq, eq,
9262 (qtype == LPFC_MBOX) ? LPFC_MCQ : LPFC_WCQ, qtype);
9263 if (rc) {
9264 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9265 "6086 Failed setup of CQ (%d), rc = 0x%x\n",
9266 qidx, (uint32_t)rc);
9267 return rc;
9268 }
9269
9270 if (qtype != LPFC_MBOX) {
James Smartcdb42be2019-01-28 11:14:21 -08009271 /* Setup cq_map for fast lookup */
James Smart895427b2017-02-12 13:52:30 -08009272 if (cq_map)
9273 *cq_map = cq->queue_id;
9274
9275 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9276 "6087 CQ setup: cq[%d]-id=%d, parent eq[%d]-id=%d\n",
9277 qidx, cq->queue_id, qidx, eq->queue_id);
9278
9279 /* create the wq */
9280 rc = lpfc_wq_create(phba, wq, cq, qtype);
9281 if (rc) {
9282 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartc835c082019-03-12 16:30:30 -07009283 "4618 Fail setup fastpath WQ (%d), rc = 0x%x\n",
James Smart895427b2017-02-12 13:52:30 -08009284 qidx, (uint32_t)rc);
9285 /* no need to tear down cq - caller will do so */
9286 return rc;
9287 }
9288
9289 /* Bind this CQ/WQ to the NVME ring */
9290 pring = wq->pring;
9291 pring->sli.sli4.wqp = (void *)wq;
9292 cq->pring = pring;
9293
9294 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9295 "2593 WQ setup: wq[%d]-id=%d assoc=%d, cq[%d]-id=%d\n",
9296 qidx, wq->queue_id, wq->assoc_qid, qidx, cq->queue_id);
9297 } else {
9298 rc = lpfc_mq_create(phba, wq, cq, LPFC_MBOX);
9299 if (rc) {
9300 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9301 "0539 Failed setup of slow-path MQ: "
9302 "rc = 0x%x\n", rc);
9303 /* no need to tear down cq - caller will do so */
9304 return rc;
9305 }
9306
9307 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9308 "2589 MBX MQ setup: wq-id=%d, parent cq-id=%d\n",
9309 phba->sli4_hba.mbx_wq->queue_id,
9310 phba->sli4_hba.mbx_cq->queue_id);
9311 }
9312
9313 return 0;
James Smartda0436e2009-05-22 14:51:39 -04009314}
9315
9316/**
James Smart6a828b02019-01-28 11:14:31 -08009317 * lpfc_setup_cq_lookup - Setup the CQ lookup table
9318 * @phba: pointer to lpfc hba data structure.
9319 *
9320 * This routine will populate the cq_lookup table by all
9321 * available CQ queue_id's.
9322 **/
Bart Van Assche3999df72019-03-28 11:06:16 -07009323static void
James Smart6a828b02019-01-28 11:14:31 -08009324lpfc_setup_cq_lookup(struct lpfc_hba *phba)
9325{
9326 struct lpfc_queue *eq, *childq;
9327 struct lpfc_sli4_hdw_queue *qp;
9328 int qidx;
9329
9330 qp = phba->sli4_hba.hdwq;
9331 memset(phba->sli4_hba.cq_lookup, 0,
9332 (sizeof(struct lpfc_queue *) * (phba->sli4_hba.cq_max + 1)));
9333 for (qidx = 0; qidx < phba->cfg_irq_chann; qidx++) {
9334 eq = qp[qidx].hba_eq;
9335 if (!eq)
9336 continue;
9337 list_for_each_entry(childq, &eq->child_list, list) {
9338 if (childq->queue_id > phba->sli4_hba.cq_max)
9339 continue;
9340 if ((childq->subtype == LPFC_FCP) ||
9341 (childq->subtype == LPFC_NVME))
9342 phba->sli4_hba.cq_lookup[childq->queue_id] =
9343 childq;
9344 }
9345 }
9346}
9347
9348/**
James Smartda0436e2009-05-22 14:51:39 -04009349 * lpfc_sli4_queue_setup - Set up all the SLI4 queues
9350 * @phba: pointer to lpfc hba data structure.
9351 *
9352 * This routine is invoked to set up all the SLI4 queues for the FCoE HBA
9353 * operation.
9354 *
9355 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02009356 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009357 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04009358 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04009359 **/
9360int
9361lpfc_sli4_queue_setup(struct lpfc_hba *phba)
9362{
James Smart962bc512013-01-03 15:44:00 -05009363 uint32_t shdr_status, shdr_add_status;
9364 union lpfc_sli4_cfg_shdr *shdr;
James Smartcdb42be2019-01-28 11:14:21 -08009365 struct lpfc_sli4_hdw_queue *qp;
James Smart962bc512013-01-03 15:44:00 -05009366 LPFC_MBOXQ_t *mboxq;
James Smart895427b2017-02-12 13:52:30 -08009367 int qidx;
James Smartcb733e32019-01-28 11:14:32 -08009368 uint32_t length, usdelay;
James Smart895427b2017-02-12 13:52:30 -08009369 int rc = -ENOMEM;
James Smart962bc512013-01-03 15:44:00 -05009370
9371 /* Check for dual-ULP support */
9372 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9373 if (!mboxq) {
9374 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9375 "3249 Unable to allocate memory for "
9376 "QUERY_FW_CFG mailbox command\n");
9377 return -ENOMEM;
9378 }
9379 length = (sizeof(struct lpfc_mbx_query_fw_config) -
9380 sizeof(struct lpfc_sli4_cfg_mhdr));
9381 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
9382 LPFC_MBOX_OPCODE_QUERY_FW_CFG,
9383 length, LPFC_SLI4_MBX_EMBED);
9384
9385 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
9386
9387 shdr = (union lpfc_sli4_cfg_shdr *)
9388 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
9389 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9390 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9391 if (shdr_status || shdr_add_status || rc) {
9392 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9393 "3250 QUERY_FW_CFG mailbox failed with status "
9394 "x%x add_status x%x, mbx status x%x\n",
9395 shdr_status, shdr_add_status, rc);
9396 if (rc != MBX_TIMEOUT)
9397 mempool_free(mboxq, phba->mbox_mem_pool);
9398 rc = -ENXIO;
9399 goto out_error;
9400 }
9401
9402 phba->sli4_hba.fw_func_mode =
9403 mboxq->u.mqe.un.query_fw_cfg.rsp.function_mode;
9404 phba->sli4_hba.ulp0_mode = mboxq->u.mqe.un.query_fw_cfg.rsp.ulp0_mode;
9405 phba->sli4_hba.ulp1_mode = mboxq->u.mqe.un.query_fw_cfg.rsp.ulp1_mode;
James Smart8b017a32015-05-21 13:55:18 -04009406 phba->sli4_hba.physical_port =
9407 mboxq->u.mqe.un.query_fw_cfg.rsp.physical_port;
James Smart962bc512013-01-03 15:44:00 -05009408 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9409 "3251 QUERY_FW_CFG: func_mode:x%x, ulp0_mode:x%x, "
9410 "ulp1_mode:x%x\n", phba->sli4_hba.fw_func_mode,
9411 phba->sli4_hba.ulp0_mode, phba->sli4_hba.ulp1_mode);
9412
9413 if (rc != MBX_TIMEOUT)
9414 mempool_free(mboxq, phba->mbox_mem_pool);
James Smartda0436e2009-05-22 14:51:39 -04009415
9416 /*
James Smart67d12732012-08-03 12:36:13 -04009417 * Set up HBA Event Queues (EQs)
James Smartda0436e2009-05-22 14:51:39 -04009418 */
James Smartcdb42be2019-01-28 11:14:21 -08009419 qp = phba->sli4_hba.hdwq;
James Smartda0436e2009-05-22 14:51:39 -04009420
James Smart67d12732012-08-03 12:36:13 -04009421 /* Set up HBA event queue */
James Smartcdb42be2019-01-28 11:14:21 -08009422 if (!qp) {
James Smart2e90f4b2011-12-13 13:22:37 -05009423 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9424 "3147 Fast-path EQs not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05009425 rc = -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04009426 goto out_error;
James Smart2e90f4b2011-12-13 13:22:37 -05009427 }
James Smart6a828b02019-01-28 11:14:31 -08009428 for (qidx = 0; qidx < phba->cfg_irq_chann; qidx++) {
James Smartcdb42be2019-01-28 11:14:21 -08009429 if (!qp[qidx].hba_eq) {
James Smartda0436e2009-05-22 14:51:39 -04009430 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9431 "0522 Fast-path EQ (%d) not "
James Smart895427b2017-02-12 13:52:30 -08009432 "allocated\n", qidx);
James Smart1b511972011-12-13 13:23:09 -05009433 rc = -ENOMEM;
James Smart895427b2017-02-12 13:52:30 -08009434 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -04009435 }
James Smartcdb42be2019-01-28 11:14:21 -08009436 rc = lpfc_eq_create(phba, qp[qidx].hba_eq,
9437 phba->cfg_fcp_imax);
James Smartda0436e2009-05-22 14:51:39 -04009438 if (rc) {
9439 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9440 "0523 Failed setup of fast-path EQ "
James Smart895427b2017-02-12 13:52:30 -08009441 "(%d), rc = 0x%x\n", qidx,
James Smarta2fc4aef2014-09-03 12:57:55 -04009442 (uint32_t)rc);
James Smart895427b2017-02-12 13:52:30 -08009443 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -04009444 }
9445 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smartcdb42be2019-01-28 11:14:21 -08009446 "2584 HBA EQ setup: queue[%d]-id=%d\n", qidx,
9447 qp[qidx].hba_eq->queue_id);
James Smartda0436e2009-05-22 14:51:39 -04009448 }
9449
James Smartcdb42be2019-01-28 11:14:21 -08009450 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
9451 for (qidx = 0; qidx < phba->cfg_hdw_queue; qidx++) {
James Smart895427b2017-02-12 13:52:30 -08009452 rc = lpfc_create_wq_cq(phba,
James Smartcdb42be2019-01-28 11:14:21 -08009453 qp[qidx].hba_eq,
9454 qp[qidx].nvme_cq,
9455 qp[qidx].nvme_wq,
9456 &phba->sli4_hba.hdwq[qidx].nvme_cq_map,
James Smart895427b2017-02-12 13:52:30 -08009457 qidx, LPFC_NVME);
9458 if (rc) {
9459 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9460 "6123 Failed to setup fastpath "
9461 "NVME WQ/CQ (%d), rc = 0x%x\n",
9462 qidx, (uint32_t)rc);
9463 goto out_destroy;
9464 }
9465 }
James Smart67d12732012-08-03 12:36:13 -04009466 }
9467
James Smartcdb42be2019-01-28 11:14:21 -08009468 for (qidx = 0; qidx < phba->cfg_hdw_queue; qidx++) {
9469 rc = lpfc_create_wq_cq(phba,
9470 qp[qidx].hba_eq,
9471 qp[qidx].fcp_cq,
9472 qp[qidx].fcp_wq,
9473 &phba->sli4_hba.hdwq[qidx].fcp_cq_map,
9474 qidx, LPFC_FCP);
9475 if (rc) {
James Smart67d12732012-08-03 12:36:13 -04009476 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart895427b2017-02-12 13:52:30 -08009477 "0535 Failed to setup fastpath "
9478 "FCP WQ/CQ (%d), rc = 0x%x\n",
9479 qidx, (uint32_t)rc);
James Smartcdb42be2019-01-28 11:14:21 -08009480 goto out_destroy;
James Smart895427b2017-02-12 13:52:30 -08009481 }
James Smart67d12732012-08-03 12:36:13 -04009482 }
James Smartda0436e2009-05-22 14:51:39 -04009483
9484 /*
James Smart895427b2017-02-12 13:52:30 -08009485 * Set up Slow Path Complete Queues (CQs)
James Smartda0436e2009-05-22 14:51:39 -04009486 */
9487
James Smart895427b2017-02-12 13:52:30 -08009488 /* Set up slow-path MBOX CQ/MQ */
9489
9490 if (!phba->sli4_hba.mbx_cq || !phba->sli4_hba.mbx_wq) {
James Smartda0436e2009-05-22 14:51:39 -04009491 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart895427b2017-02-12 13:52:30 -08009492 "0528 %s not allocated\n",
9493 phba->sli4_hba.mbx_cq ?
James Smartd1f525a2017-04-21 16:04:55 -07009494 "Mailbox WQ" : "Mailbox CQ");
James Smart1b511972011-12-13 13:23:09 -05009495 rc = -ENOMEM;
James Smart895427b2017-02-12 13:52:30 -08009496 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -04009497 }
James Smart895427b2017-02-12 13:52:30 -08009498
James Smartcdb42be2019-01-28 11:14:21 -08009499 rc = lpfc_create_wq_cq(phba, qp[0].hba_eq,
James Smartd1f525a2017-04-21 16:04:55 -07009500 phba->sli4_hba.mbx_cq,
9501 phba->sli4_hba.mbx_wq,
9502 NULL, 0, LPFC_MBOX);
James Smartda0436e2009-05-22 14:51:39 -04009503 if (rc) {
9504 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart895427b2017-02-12 13:52:30 -08009505 "0529 Failed setup of mailbox WQ/CQ: rc = 0x%x\n",
9506 (uint32_t)rc);
9507 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -04009508 }
James Smart2d7dbc42017-02-12 13:52:35 -08009509 if (phba->nvmet_support) {
9510 if (!phba->sli4_hba.nvmet_cqset) {
9511 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9512 "3165 Fast-path NVME CQ Set "
9513 "array not allocated\n");
9514 rc = -ENOMEM;
9515 goto out_destroy;
9516 }
9517 if (phba->cfg_nvmet_mrq > 1) {
9518 rc = lpfc_cq_create_set(phba,
9519 phba->sli4_hba.nvmet_cqset,
James Smartcdb42be2019-01-28 11:14:21 -08009520 qp,
James Smart2d7dbc42017-02-12 13:52:35 -08009521 LPFC_WCQ, LPFC_NVMET);
9522 if (rc) {
9523 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9524 "3164 Failed setup of NVME CQ "
9525 "Set, rc = 0x%x\n",
9526 (uint32_t)rc);
9527 goto out_destroy;
9528 }
9529 } else {
9530 /* Set up NVMET Receive Complete Queue */
9531 rc = lpfc_cq_create(phba, phba->sli4_hba.nvmet_cqset[0],
James Smartcdb42be2019-01-28 11:14:21 -08009532 qp[0].hba_eq,
James Smart2d7dbc42017-02-12 13:52:35 -08009533 LPFC_WCQ, LPFC_NVMET);
9534 if (rc) {
9535 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9536 "6089 Failed setup NVMET CQ: "
9537 "rc = 0x%x\n", (uint32_t)rc);
9538 goto out_destroy;
9539 }
James Smart81b96ed2017-11-20 16:00:29 -08009540 phba->sli4_hba.nvmet_cqset[0]->chann = 0;
9541
James Smart2d7dbc42017-02-12 13:52:35 -08009542 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9543 "6090 NVMET CQ setup: cq-id=%d, "
9544 "parent eq-id=%d\n",
9545 phba->sli4_hba.nvmet_cqset[0]->queue_id,
James Smartcdb42be2019-01-28 11:14:21 -08009546 qp[0].hba_eq->queue_id);
James Smart2d7dbc42017-02-12 13:52:35 -08009547 }
9548 }
James Smartda0436e2009-05-22 14:51:39 -04009549
James Smart895427b2017-02-12 13:52:30 -08009550 /* Set up slow-path ELS WQ/CQ */
9551 if (!phba->sli4_hba.els_cq || !phba->sli4_hba.els_wq) {
James Smartda0436e2009-05-22 14:51:39 -04009552 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart895427b2017-02-12 13:52:30 -08009553 "0530 ELS %s not allocated\n",
9554 phba->sli4_hba.els_cq ? "WQ" : "CQ");
James Smart1b511972011-12-13 13:23:09 -05009555 rc = -ENOMEM;
James Smart895427b2017-02-12 13:52:30 -08009556 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -04009557 }
James Smartcdb42be2019-01-28 11:14:21 -08009558 rc = lpfc_create_wq_cq(phba, qp[0].hba_eq,
9559 phba->sli4_hba.els_cq,
9560 phba->sli4_hba.els_wq,
9561 NULL, 0, LPFC_ELS);
James Smartda0436e2009-05-22 14:51:39 -04009562 if (rc) {
9563 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartcdb42be2019-01-28 11:14:21 -08009564 "0525 Failed setup of ELS WQ/CQ: rc = 0x%x\n",
9565 (uint32_t)rc);
James Smart895427b2017-02-12 13:52:30 -08009566 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -04009567 }
9568 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9569 "2590 ELS WQ setup: wq-id=%d, parent cq-id=%d\n",
9570 phba->sli4_hba.els_wq->queue_id,
9571 phba->sli4_hba.els_cq->queue_id);
9572
James Smartcdb42be2019-01-28 11:14:21 -08009573 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
James Smart895427b2017-02-12 13:52:30 -08009574 /* Set up NVME LS Complete Queue */
9575 if (!phba->sli4_hba.nvmels_cq || !phba->sli4_hba.nvmels_wq) {
9576 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9577 "6091 LS %s not allocated\n",
9578 phba->sli4_hba.nvmels_cq ? "WQ" : "CQ");
9579 rc = -ENOMEM;
9580 goto out_destroy;
9581 }
James Smartcdb42be2019-01-28 11:14:21 -08009582 rc = lpfc_create_wq_cq(phba, qp[0].hba_eq,
9583 phba->sli4_hba.nvmels_cq,
9584 phba->sli4_hba.nvmels_wq,
9585 NULL, 0, LPFC_NVME_LS);
James Smart895427b2017-02-12 13:52:30 -08009586 if (rc) {
9587 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartcdb42be2019-01-28 11:14:21 -08009588 "0526 Failed setup of NVVME LS WQ/CQ: "
9589 "rc = 0x%x\n", (uint32_t)rc);
James Smart895427b2017-02-12 13:52:30 -08009590 goto out_destroy;
9591 }
9592
9593 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9594 "6096 ELS WQ setup: wq-id=%d, "
9595 "parent cq-id=%d\n",
9596 phba->sli4_hba.nvmels_wq->queue_id,
9597 phba->sli4_hba.nvmels_cq->queue_id);
9598 }
9599
James Smart2d7dbc42017-02-12 13:52:35 -08009600 /*
9601 * Create NVMET Receive Queue (RQ)
9602 */
9603 if (phba->nvmet_support) {
9604 if ((!phba->sli4_hba.nvmet_cqset) ||
9605 (!phba->sli4_hba.nvmet_mrq_hdr) ||
9606 (!phba->sli4_hba.nvmet_mrq_data)) {
9607 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9608 "6130 MRQ CQ Queues not "
9609 "allocated\n");
9610 rc = -ENOMEM;
9611 goto out_destroy;
9612 }
9613 if (phba->cfg_nvmet_mrq > 1) {
9614 rc = lpfc_mrq_create(phba,
9615 phba->sli4_hba.nvmet_mrq_hdr,
9616 phba->sli4_hba.nvmet_mrq_data,
9617 phba->sli4_hba.nvmet_cqset,
9618 LPFC_NVMET);
9619 if (rc) {
9620 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9621 "6098 Failed setup of NVMET "
9622 "MRQ: rc = 0x%x\n",
9623 (uint32_t)rc);
9624 goto out_destroy;
9625 }
9626
9627 } else {
9628 rc = lpfc_rq_create(phba,
9629 phba->sli4_hba.nvmet_mrq_hdr[0],
9630 phba->sli4_hba.nvmet_mrq_data[0],
9631 phba->sli4_hba.nvmet_cqset[0],
9632 LPFC_NVMET);
9633 if (rc) {
9634 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9635 "6057 Failed setup of NVMET "
9636 "Receive Queue: rc = 0x%x\n",
9637 (uint32_t)rc);
9638 goto out_destroy;
9639 }
9640
9641 lpfc_printf_log(
9642 phba, KERN_INFO, LOG_INIT,
9643 "6099 NVMET RQ setup: hdr-rq-id=%d, "
9644 "dat-rq-id=%d parent cq-id=%d\n",
9645 phba->sli4_hba.nvmet_mrq_hdr[0]->queue_id,
9646 phba->sli4_hba.nvmet_mrq_data[0]->queue_id,
9647 phba->sli4_hba.nvmet_cqset[0]->queue_id);
9648
9649 }
9650 }
9651
James Smartda0436e2009-05-22 14:51:39 -04009652 if (!phba->sli4_hba.hdr_rq || !phba->sli4_hba.dat_rq) {
9653 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9654 "0540 Receive Queue not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05009655 rc = -ENOMEM;
James Smart895427b2017-02-12 13:52:30 -08009656 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -04009657 }
James Smart73d91e52011-10-10 21:32:10 -04009658
James Smartda0436e2009-05-22 14:51:39 -04009659 rc = lpfc_rq_create(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
James Smart4d9ab992009-10-02 15:16:39 -04009660 phba->sli4_hba.els_cq, LPFC_USOL);
James Smartda0436e2009-05-22 14:51:39 -04009661 if (rc) {
9662 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9663 "0541 Failed setup of Receive Queue: "
James Smarta2fc4aef2014-09-03 12:57:55 -04009664 "rc = 0x%x\n", (uint32_t)rc);
James Smart895427b2017-02-12 13:52:30 -08009665 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -04009666 }
James Smart73d91e52011-10-10 21:32:10 -04009667
James Smartda0436e2009-05-22 14:51:39 -04009668 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9669 "2592 USL RQ setup: hdr-rq-id=%d, dat-rq-id=%d "
9670 "parent cq-id=%d\n",
9671 phba->sli4_hba.hdr_rq->queue_id,
9672 phba->sli4_hba.dat_rq->queue_id,
James Smart4d9ab992009-10-02 15:16:39 -04009673 phba->sli4_hba.els_cq->queue_id);
James Smart1ba981f2014-02-20 09:56:45 -05009674
James Smartcb733e32019-01-28 11:14:32 -08009675 if (phba->cfg_fcp_imax)
9676 usdelay = LPFC_SEC_TO_USEC / phba->cfg_fcp_imax;
9677 else
9678 usdelay = 0;
9679
James Smart6a828b02019-01-28 11:14:31 -08009680 for (qidx = 0; qidx < phba->cfg_irq_chann;
James Smartcdb42be2019-01-28 11:14:21 -08009681 qidx += LPFC_MAX_EQ_DELAY_EQID_CNT)
James Smart0cf07f842017-06-01 21:07:10 -07009682 lpfc_modify_hba_eq_delay(phba, qidx, LPFC_MAX_EQ_DELAY_EQID_CNT,
James Smartcb733e32019-01-28 11:14:32 -08009683 usdelay);
James Smart43140ca2017-03-04 09:30:34 -08009684
James Smart6a828b02019-01-28 11:14:31 -08009685 if (phba->sli4_hba.cq_max) {
9686 kfree(phba->sli4_hba.cq_lookup);
9687 phba->sli4_hba.cq_lookup = kcalloc((phba->sli4_hba.cq_max + 1),
9688 sizeof(struct lpfc_queue *), GFP_KERNEL);
9689 if (!phba->sli4_hba.cq_lookup) {
James Smart1ba981f2014-02-20 09:56:45 -05009690 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart6a828b02019-01-28 11:14:31 -08009691 "0549 Failed setup of CQ Lookup table: "
9692 "size 0x%x\n", phba->sli4_hba.cq_max);
Dan Carpenterfad28e32019-02-11 21:43:00 +03009693 rc = -ENOMEM;
James Smart1ba981f2014-02-20 09:56:45 -05009694 goto out_destroy;
James Smart895427b2017-02-12 13:52:30 -08009695 }
James Smart6a828b02019-01-28 11:14:31 -08009696 lpfc_setup_cq_lookup(phba);
James Smart1ba981f2014-02-20 09:56:45 -05009697 }
James Smartda0436e2009-05-22 14:51:39 -04009698 return 0;
9699
James Smart895427b2017-02-12 13:52:30 -08009700out_destroy:
9701 lpfc_sli4_queue_unset(phba);
James Smartda0436e2009-05-22 14:51:39 -04009702out_error:
9703 return rc;
9704}
9705
9706/**
9707 * lpfc_sli4_queue_unset - Unset all the SLI4 queues
9708 * @phba: pointer to lpfc hba data structure.
9709 *
9710 * This routine is invoked to unset all the SLI4 queues with the FCoE HBA
9711 * operation.
9712 *
9713 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02009714 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009715 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04009716 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04009717 **/
9718void
9719lpfc_sli4_queue_unset(struct lpfc_hba *phba)
9720{
James Smartcdb42be2019-01-28 11:14:21 -08009721 struct lpfc_sli4_hdw_queue *qp;
James Smart895427b2017-02-12 13:52:30 -08009722 int qidx;
James Smartda0436e2009-05-22 14:51:39 -04009723
9724 /* Unset mailbox command work queue */
James Smart895427b2017-02-12 13:52:30 -08009725 if (phba->sli4_hba.mbx_wq)
9726 lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
9727
9728 /* Unset NVME LS work queue */
9729 if (phba->sli4_hba.nvmels_wq)
9730 lpfc_wq_destroy(phba, phba->sli4_hba.nvmels_wq);
9731
James Smartda0436e2009-05-22 14:51:39 -04009732 /* Unset ELS work queue */
Colin Ian King019c0d62017-05-06 23:13:55 +01009733 if (phba->sli4_hba.els_wq)
James Smart895427b2017-02-12 13:52:30 -08009734 lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
9735
James Smartda0436e2009-05-22 14:51:39 -04009736 /* Unset unsolicited receive queue */
James Smart895427b2017-02-12 13:52:30 -08009737 if (phba->sli4_hba.hdr_rq)
9738 lpfc_rq_destroy(phba, phba->sli4_hba.hdr_rq,
9739 phba->sli4_hba.dat_rq);
9740
James Smartda0436e2009-05-22 14:51:39 -04009741 /* Unset mailbox command complete queue */
James Smart895427b2017-02-12 13:52:30 -08009742 if (phba->sli4_hba.mbx_cq)
9743 lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
9744
James Smartda0436e2009-05-22 14:51:39 -04009745 /* Unset ELS complete queue */
James Smart895427b2017-02-12 13:52:30 -08009746 if (phba->sli4_hba.els_cq)
9747 lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
9748
9749 /* Unset NVME LS complete queue */
9750 if (phba->sli4_hba.nvmels_cq)
9751 lpfc_cq_destroy(phba, phba->sli4_hba.nvmels_cq);
9752
James Smartbcb24f62017-11-20 16:00:36 -08009753 if (phba->nvmet_support) {
9754 /* Unset NVMET MRQ queue */
9755 if (phba->sli4_hba.nvmet_mrq_hdr) {
9756 for (qidx = 0; qidx < phba->cfg_nvmet_mrq; qidx++)
9757 lpfc_rq_destroy(
9758 phba,
James Smart2d7dbc42017-02-12 13:52:35 -08009759 phba->sli4_hba.nvmet_mrq_hdr[qidx],
9760 phba->sli4_hba.nvmet_mrq_data[qidx]);
James Smartbcb24f62017-11-20 16:00:36 -08009761 }
James Smart2d7dbc42017-02-12 13:52:35 -08009762
James Smartbcb24f62017-11-20 16:00:36 -08009763 /* Unset NVMET CQ Set complete queue */
9764 if (phba->sli4_hba.nvmet_cqset) {
9765 for (qidx = 0; qidx < phba->cfg_nvmet_mrq; qidx++)
9766 lpfc_cq_destroy(
9767 phba, phba->sli4_hba.nvmet_cqset[qidx]);
9768 }
James Smart2d7dbc42017-02-12 13:52:35 -08009769 }
9770
James Smartcdb42be2019-01-28 11:14:21 -08009771 /* Unset fast-path SLI4 queues */
9772 if (phba->sli4_hba.hdwq) {
9773 for (qidx = 0; qidx < phba->cfg_hdw_queue; qidx++) {
9774 qp = &phba->sli4_hba.hdwq[qidx];
9775 lpfc_wq_destroy(phba, qp->fcp_wq);
9776 lpfc_wq_destroy(phba, qp->nvme_wq);
9777 lpfc_cq_destroy(phba, qp->fcp_cq);
9778 lpfc_cq_destroy(phba, qp->nvme_cq);
James Smart6a828b02019-01-28 11:14:31 -08009779 if (qidx < phba->cfg_irq_chann)
9780 lpfc_eq_destroy(phba, qp->hba_eq);
James Smartcdb42be2019-01-28 11:14:21 -08009781 }
9782 }
James Smart895427b2017-02-12 13:52:30 -08009783
James Smart6a828b02019-01-28 11:14:31 -08009784 kfree(phba->sli4_hba.cq_lookup);
9785 phba->sli4_hba.cq_lookup = NULL;
9786 phba->sli4_hba.cq_max = 0;
James Smartda0436e2009-05-22 14:51:39 -04009787}
9788
9789/**
9790 * lpfc_sli4_cq_event_pool_create - Create completion-queue event free pool
9791 * @phba: pointer to lpfc hba data structure.
9792 *
9793 * This routine is invoked to allocate and set up a pool of completion queue
9794 * events. The body of the completion queue event is a completion queue entry
9795 * CQE. For now, this pool is used for the interrupt service routine to queue
9796 * the following HBA completion queue events for the worker thread to process:
9797 * - Mailbox asynchronous events
9798 * - Receive queue completion unsolicited events
9799 * Later, this can be used for all the slow-path events.
9800 *
9801 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02009802 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009803 * -ENOMEM - No available memory
James Smartda0436e2009-05-22 14:51:39 -04009804 **/
9805static int
9806lpfc_sli4_cq_event_pool_create(struct lpfc_hba *phba)
9807{
9808 struct lpfc_cq_event *cq_event;
9809 int i;
9810
9811 for (i = 0; i < (4 * phba->sli4_hba.cq_ecount); i++) {
9812 cq_event = kmalloc(sizeof(struct lpfc_cq_event), GFP_KERNEL);
9813 if (!cq_event)
9814 goto out_pool_create_fail;
9815 list_add_tail(&cq_event->list,
9816 &phba->sli4_hba.sp_cqe_event_pool);
9817 }
9818 return 0;
9819
9820out_pool_create_fail:
9821 lpfc_sli4_cq_event_pool_destroy(phba);
9822 return -ENOMEM;
9823}
9824
9825/**
9826 * lpfc_sli4_cq_event_pool_destroy - Free completion-queue event free pool
9827 * @phba: pointer to lpfc hba data structure.
9828 *
9829 * This routine is invoked to free the pool of completion queue events at
9830 * driver unload time. Note that, it is the responsibility of the driver
9831 * cleanup routine to free all the outstanding completion-queue events
9832 * allocated from this pool back into the pool before invoking this routine
9833 * to destroy the pool.
9834 **/
9835static void
9836lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *phba)
9837{
9838 struct lpfc_cq_event *cq_event, *next_cq_event;
9839
9840 list_for_each_entry_safe(cq_event, next_cq_event,
9841 &phba->sli4_hba.sp_cqe_event_pool, list) {
9842 list_del(&cq_event->list);
9843 kfree(cq_event);
9844 }
9845}
9846
9847/**
9848 * __lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
9849 * @phba: pointer to lpfc hba data structure.
9850 *
9851 * This routine is the lock free version of the API invoked to allocate a
9852 * completion-queue event from the free pool.
9853 *
9854 * Return: Pointer to the newly allocated completion-queue event if successful
9855 * NULL otherwise.
9856 **/
9857struct lpfc_cq_event *
9858__lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
9859{
9860 struct lpfc_cq_event *cq_event = NULL;
9861
9862 list_remove_head(&phba->sli4_hba.sp_cqe_event_pool, cq_event,
9863 struct lpfc_cq_event, list);
9864 return cq_event;
9865}
9866
9867/**
9868 * lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
9869 * @phba: pointer to lpfc hba data structure.
9870 *
9871 * This routine is the lock version of the API invoked to allocate a
9872 * completion-queue event from the free pool.
9873 *
9874 * Return: Pointer to the newly allocated completion-queue event if successful
9875 * NULL otherwise.
9876 **/
9877struct lpfc_cq_event *
9878lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
9879{
9880 struct lpfc_cq_event *cq_event;
9881 unsigned long iflags;
9882
9883 spin_lock_irqsave(&phba->hbalock, iflags);
9884 cq_event = __lpfc_sli4_cq_event_alloc(phba);
9885 spin_unlock_irqrestore(&phba->hbalock, iflags);
9886 return cq_event;
9887}
9888
9889/**
9890 * __lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
9891 * @phba: pointer to lpfc hba data structure.
9892 * @cq_event: pointer to the completion queue event to be freed.
9893 *
9894 * This routine is the lock free version of the API invoked to release a
9895 * completion-queue event back into the free pool.
9896 **/
9897void
9898__lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
9899 struct lpfc_cq_event *cq_event)
9900{
9901 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_cqe_event_pool);
9902}
9903
9904/**
9905 * lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
9906 * @phba: pointer to lpfc hba data structure.
9907 * @cq_event: pointer to the completion queue event to be freed.
9908 *
9909 * This routine is the lock version of the API invoked to release a
9910 * completion-queue event back into the free pool.
9911 **/
9912void
9913lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
9914 struct lpfc_cq_event *cq_event)
9915{
9916 unsigned long iflags;
9917 spin_lock_irqsave(&phba->hbalock, iflags);
9918 __lpfc_sli4_cq_event_release(phba, cq_event);
9919 spin_unlock_irqrestore(&phba->hbalock, iflags);
9920}
9921
9922/**
9923 * lpfc_sli4_cq_event_release_all - Release all cq events to the free pool
9924 * @phba: pointer to lpfc hba data structure.
9925 *
9926 * This routine is to free all the pending completion-queue events to the
9927 * back into the free pool for device reset.
9928 **/
9929static void
9930lpfc_sli4_cq_event_release_all(struct lpfc_hba *phba)
9931{
9932 LIST_HEAD(cqelist);
9933 struct lpfc_cq_event *cqe;
9934 unsigned long iflags;
9935
9936 /* Retrieve all the pending WCQEs from pending WCQE lists */
9937 spin_lock_irqsave(&phba->hbalock, iflags);
9938 /* Pending FCP XRI abort events */
9939 list_splice_init(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
9940 &cqelist);
9941 /* Pending ELS XRI abort events */
9942 list_splice_init(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
9943 &cqelist);
9944 /* Pending asynnc events */
9945 list_splice_init(&phba->sli4_hba.sp_asynce_work_queue,
9946 &cqelist);
9947 spin_unlock_irqrestore(&phba->hbalock, iflags);
9948
9949 while (!list_empty(&cqelist)) {
9950 list_remove_head(&cqelist, cqe, struct lpfc_cq_event, list);
9951 lpfc_sli4_cq_event_release(phba, cqe);
9952 }
9953}
9954
9955/**
9956 * lpfc_pci_function_reset - Reset pci function.
9957 * @phba: pointer to lpfc hba data structure.
9958 *
9959 * This routine is invoked to request a PCI function reset. It will destroys
9960 * all resources assigned to the PCI function which originates this request.
9961 *
9962 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02009963 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009964 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04009965 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04009966 **/
9967int
9968lpfc_pci_function_reset(struct lpfc_hba *phba)
9969{
9970 LPFC_MBOXQ_t *mboxq;
James Smart2fcee4b2010-12-15 17:57:46 -05009971 uint32_t rc = 0, if_type;
James Smartda0436e2009-05-22 14:51:39 -04009972 uint32_t shdr_status, shdr_add_status;
James Smart2f6fa2c2014-09-03 12:57:08 -04009973 uint32_t rdy_chk;
9974 uint32_t port_reset = 0;
James Smartda0436e2009-05-22 14:51:39 -04009975 union lpfc_sli4_cfg_shdr *shdr;
James Smart2fcee4b2010-12-15 17:57:46 -05009976 struct lpfc_register reg_data;
James Smart2b81f942012-03-01 22:37:18 -05009977 uint16_t devid;
James Smartda0436e2009-05-22 14:51:39 -04009978
James Smart2fcee4b2010-12-15 17:57:46 -05009979 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
9980 switch (if_type) {
9981 case LPFC_SLI_INTF_IF_TYPE_0:
9982 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
9983 GFP_KERNEL);
9984 if (!mboxq) {
9985 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9986 "0494 Unable to allocate memory for "
9987 "issuing SLI_FUNCTION_RESET mailbox "
9988 "command\n");
9989 return -ENOMEM;
9990 }
9991
9992 /* Setup PCI function reset mailbox-ioctl command */
9993 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
9994 LPFC_MBOX_OPCODE_FUNCTION_RESET, 0,
9995 LPFC_SLI4_MBX_EMBED);
9996 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
9997 shdr = (union lpfc_sli4_cfg_shdr *)
9998 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
9999 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10000 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
10001 &shdr->response);
10002 if (rc != MBX_TIMEOUT)
10003 mempool_free(mboxq, phba->mbox_mem_pool);
10004 if (shdr_status || shdr_add_status || rc) {
10005 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10006 "0495 SLI_FUNCTION_RESET mailbox "
10007 "failed with status x%x add_status x%x,"
10008 " mbx status x%x\n",
10009 shdr_status, shdr_add_status, rc);
10010 rc = -ENXIO;
10011 }
10012 break;
10013 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart27d6ac02018-02-22 08:18:42 -080010014 case LPFC_SLI_INTF_IF_TYPE_6:
James Smart2f6fa2c2014-09-03 12:57:08 -040010015wait:
10016 /*
10017 * Poll the Port Status Register and wait for RDY for
10018 * up to 30 seconds. If the port doesn't respond, treat
10019 * it as an error.
10020 */
James Smart77d093f2015-04-07 15:07:08 -040010021 for (rdy_chk = 0; rdy_chk < 1500; rdy_chk++) {
James Smart2f6fa2c2014-09-03 12:57:08 -040010022 if (lpfc_readl(phba->sli4_hba.u.if_type2.
10023 STATUSregaddr, &reg_data.word0)) {
10024 rc = -ENODEV;
10025 goto out;
10026 }
10027 if (bf_get(lpfc_sliport_status_rdy, &reg_data))
10028 break;
10029 msleep(20);
10030 }
10031
10032 if (!bf_get(lpfc_sliport_status_rdy, &reg_data)) {
10033 phba->work_status[0] = readl(
10034 phba->sli4_hba.u.if_type2.ERR1regaddr);
10035 phba->work_status[1] = readl(
10036 phba->sli4_hba.u.if_type2.ERR2regaddr);
10037 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10038 "2890 Port not ready, port status reg "
10039 "0x%x error 1=0x%x, error 2=0x%x\n",
10040 reg_data.word0,
10041 phba->work_status[0],
10042 phba->work_status[1]);
10043 rc = -ENODEV;
10044 goto out;
10045 }
10046
10047 if (!port_reset) {
10048 /*
10049 * Reset the port now
10050 */
James Smart2fcee4b2010-12-15 17:57:46 -050010051 reg_data.word0 = 0;
10052 bf_set(lpfc_sliport_ctrl_end, &reg_data,
10053 LPFC_SLIPORT_LITTLE_ENDIAN);
10054 bf_set(lpfc_sliport_ctrl_ip, &reg_data,
10055 LPFC_SLIPORT_INIT_PORT);
10056 writel(reg_data.word0, phba->sli4_hba.u.if_type2.
10057 CTRLregaddr);
James Smart8fcb8ac2012-03-01 22:35:58 -050010058 /* flush */
James Smart2b81f942012-03-01 22:37:18 -050010059 pci_read_config_word(phba->pcidev,
10060 PCI_DEVICE_ID, &devid);
James Smart2fcee4b2010-12-15 17:57:46 -050010061
James Smart2f6fa2c2014-09-03 12:57:08 -040010062 port_reset = 1;
10063 msleep(20);
10064 goto wait;
10065 } else if (bf_get(lpfc_sliport_status_rn, &reg_data)) {
10066 rc = -ENODEV;
10067 goto out;
James Smart2fcee4b2010-12-15 17:57:46 -050010068 }
10069 break;
James Smart2f6fa2c2014-09-03 12:57:08 -040010070
James Smart2fcee4b2010-12-15 17:57:46 -050010071 case LPFC_SLI_INTF_IF_TYPE_1:
10072 default:
10073 break;
James Smartda0436e2009-05-22 14:51:39 -040010074 }
10075
James Smart73d91e52011-10-10 21:32:10 -040010076out:
James Smart2fcee4b2010-12-15 17:57:46 -050010077 /* Catch the not-ready port failure after a port reset. */
James Smart2f6fa2c2014-09-03 12:57:08 -040010078 if (rc) {
James Smart229adb02013-04-17 20:16:51 -040010079 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10080 "3317 HBA not functional: IP Reset Failed "
James Smart2f6fa2c2014-09-03 12:57:08 -040010081 "try: echo fw_reset > board_mode\n");
James Smart2fcee4b2010-12-15 17:57:46 -050010082 rc = -ENODEV;
James Smart229adb02013-04-17 20:16:51 -040010083 }
James Smart2fcee4b2010-12-15 17:57:46 -050010084
James Smartda0436e2009-05-22 14:51:39 -040010085 return rc;
10086}
10087
10088/**
James Smartda0436e2009-05-22 14:51:39 -040010089 * lpfc_sli4_pci_mem_setup - Setup SLI4 HBA PCI memory space.
10090 * @phba: pointer to lpfc hba data structure.
10091 *
10092 * This routine is invoked to set up the PCI device memory space for device
10093 * with SLI-4 interface spec.
10094 *
10095 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020010096 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -040010097 * other values - error
10098 **/
10099static int
10100lpfc_sli4_pci_mem_setup(struct lpfc_hba *phba)
10101{
Christoph Hellwigf30e1bf2018-10-18 15:10:21 +020010102 struct pci_dev *pdev = phba->pcidev;
James Smartda0436e2009-05-22 14:51:39 -040010103 unsigned long bar0map_len, bar1map_len, bar2map_len;
Dan Carpenter3a487ff2019-03-07 08:33:44 +030010104 int error;
James Smart2fcee4b2010-12-15 17:57:46 -050010105 uint32_t if_type;
James Smartda0436e2009-05-22 14:51:39 -040010106
Christoph Hellwigf30e1bf2018-10-18 15:10:21 +020010107 if (!pdev)
Hannes Reinecke56de8352019-02-18 08:34:19 +010010108 return -ENODEV;
James Smartda0436e2009-05-22 14:51:39 -040010109
10110 /* Set the device DMA mask size */
Hannes Reinecke56de8352019-02-18 08:34:19 +010010111 error = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
10112 if (error)
10113 error = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
10114 if (error)
Christoph Hellwigf30e1bf2018-10-18 15:10:21 +020010115 return error;
James Smartda0436e2009-05-22 14:51:39 -040010116
James Smart2fcee4b2010-12-15 17:57:46 -050010117 /*
10118 * The BARs and register set definitions and offset locations are
10119 * dependent on the if_type.
10120 */
10121 if (pci_read_config_dword(pdev, LPFC_SLI_INTF,
10122 &phba->sli4_hba.sli_intf.word0)) {
Dan Carpenter3a487ff2019-03-07 08:33:44 +030010123 return -ENODEV;
James Smart2fcee4b2010-12-15 17:57:46 -050010124 }
10125
10126 /* There is no SLI3 failback for SLI4 devices. */
10127 if (bf_get(lpfc_sli_intf_valid, &phba->sli4_hba.sli_intf) !=
10128 LPFC_SLI_INTF_VALID) {
10129 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10130 "2894 SLI_INTF reg contents invalid "
10131 "sli_intf reg 0x%x\n",
10132 phba->sli4_hba.sli_intf.word0);
Dan Carpenter3a487ff2019-03-07 08:33:44 +030010133 return -ENODEV;
James Smart2fcee4b2010-12-15 17:57:46 -050010134 }
10135
10136 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
10137 /*
10138 * Get the bus address of SLI4 device Bar regions and the
10139 * number of bytes required by each mapping. The mapping of the
10140 * particular PCI BARs regions is dependent on the type of
10141 * SLI4 device.
James Smartda0436e2009-05-22 14:51:39 -040010142 */
James Smartf5ca6f22013-09-06 12:21:09 -040010143 if (pci_resource_start(pdev, PCI_64BIT_BAR0)) {
10144 phba->pci_bar0_map = pci_resource_start(pdev, PCI_64BIT_BAR0);
10145 bar0map_len = pci_resource_len(pdev, PCI_64BIT_BAR0);
James Smart2fcee4b2010-12-15 17:57:46 -050010146
10147 /*
10148 * Map SLI4 PCI Config Space Register base to a kernel virtual
10149 * addr
10150 */
10151 phba->sli4_hba.conf_regs_memmap_p =
10152 ioremap(phba->pci_bar0_map, bar0map_len);
10153 if (!phba->sli4_hba.conf_regs_memmap_p) {
10154 dev_printk(KERN_ERR, &pdev->dev,
10155 "ioremap failed for SLI4 PCI config "
10156 "registers.\n");
Dan Carpenter3a487ff2019-03-07 08:33:44 +030010157 return -ENODEV;
James Smart2fcee4b2010-12-15 17:57:46 -050010158 }
James Smartf5ca6f22013-09-06 12:21:09 -040010159 phba->pci_bar0_memmap_p = phba->sli4_hba.conf_regs_memmap_p;
James Smart2fcee4b2010-12-15 17:57:46 -050010160 /* Set up BAR0 PCI config space register memory map */
10161 lpfc_sli4_bar0_register_memmap(phba, if_type);
James Smart1dfb5a42010-02-12 14:40:50 -050010162 } else {
10163 phba->pci_bar0_map = pci_resource_start(pdev, 1);
10164 bar0map_len = pci_resource_len(pdev, 1);
James Smart27d6ac02018-02-22 08:18:42 -080010165 if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
James Smart2fcee4b2010-12-15 17:57:46 -050010166 dev_printk(KERN_ERR, &pdev->dev,
10167 "FATAL - No BAR0 mapping for SLI4, if_type 2\n");
Dan Carpenter3a487ff2019-03-07 08:33:44 +030010168 return -ENODEV;
James Smart2fcee4b2010-12-15 17:57:46 -050010169 }
10170 phba->sli4_hba.conf_regs_memmap_p =
James Smartda0436e2009-05-22 14:51:39 -040010171 ioremap(phba->pci_bar0_map, bar0map_len);
James Smart2fcee4b2010-12-15 17:57:46 -050010172 if (!phba->sli4_hba.conf_regs_memmap_p) {
10173 dev_printk(KERN_ERR, &pdev->dev,
10174 "ioremap failed for SLI4 PCI config "
10175 "registers.\n");
Dan Carpenter3a487ff2019-03-07 08:33:44 +030010176 return -ENODEV;
James Smart2fcee4b2010-12-15 17:57:46 -050010177 }
10178 lpfc_sli4_bar0_register_memmap(phba, if_type);
James Smartda0436e2009-05-22 14:51:39 -040010179 }
10180
James Smarte4b97942017-11-20 16:00:31 -080010181 if (if_type == LPFC_SLI_INTF_IF_TYPE_0) {
10182 if (pci_resource_start(pdev, PCI_64BIT_BAR2)) {
10183 /*
10184 * Map SLI4 if type 0 HBA Control Register base to a
10185 * kernel virtual address and setup the registers.
10186 */
10187 phba->pci_bar1_map = pci_resource_start(pdev,
10188 PCI_64BIT_BAR2);
10189 bar1map_len = pci_resource_len(pdev, PCI_64BIT_BAR2);
10190 phba->sli4_hba.ctrl_regs_memmap_p =
10191 ioremap(phba->pci_bar1_map,
10192 bar1map_len);
10193 if (!phba->sli4_hba.ctrl_regs_memmap_p) {
10194 dev_err(&pdev->dev,
10195 "ioremap failed for SLI4 HBA "
10196 "control registers.\n");
10197 error = -ENOMEM;
10198 goto out_iounmap_conf;
10199 }
10200 phba->pci_bar2_memmap_p =
10201 phba->sli4_hba.ctrl_regs_memmap_p;
James Smart27d6ac02018-02-22 08:18:42 -080010202 lpfc_sli4_bar1_register_memmap(phba, if_type);
James Smarte4b97942017-11-20 16:00:31 -080010203 } else {
10204 error = -ENOMEM;
James Smart2fcee4b2010-12-15 17:57:46 -050010205 goto out_iounmap_conf;
10206 }
James Smartda0436e2009-05-22 14:51:39 -040010207 }
10208
James Smart27d6ac02018-02-22 08:18:42 -080010209 if ((if_type == LPFC_SLI_INTF_IF_TYPE_6) &&
10210 (pci_resource_start(pdev, PCI_64BIT_BAR2))) {
10211 /*
10212 * Map SLI4 if type 6 HBA Doorbell Register base to a kernel
10213 * virtual address and setup the registers.
10214 */
10215 phba->pci_bar1_map = pci_resource_start(pdev, PCI_64BIT_BAR2);
10216 bar1map_len = pci_resource_len(pdev, PCI_64BIT_BAR2);
10217 phba->sli4_hba.drbl_regs_memmap_p =
10218 ioremap(phba->pci_bar1_map, bar1map_len);
10219 if (!phba->sli4_hba.drbl_regs_memmap_p) {
10220 dev_err(&pdev->dev,
10221 "ioremap failed for SLI4 HBA doorbell registers.\n");
Dan Carpenter3a487ff2019-03-07 08:33:44 +030010222 error = -ENOMEM;
James Smart27d6ac02018-02-22 08:18:42 -080010223 goto out_iounmap_conf;
10224 }
10225 phba->pci_bar2_memmap_p = phba->sli4_hba.drbl_regs_memmap_p;
10226 lpfc_sli4_bar1_register_memmap(phba, if_type);
10227 }
10228
James Smarte4b97942017-11-20 16:00:31 -080010229 if (if_type == LPFC_SLI_INTF_IF_TYPE_0) {
10230 if (pci_resource_start(pdev, PCI_64BIT_BAR4)) {
10231 /*
10232 * Map SLI4 if type 0 HBA Doorbell Register base to
10233 * a kernel virtual address and setup the registers.
10234 */
10235 phba->pci_bar2_map = pci_resource_start(pdev,
10236 PCI_64BIT_BAR4);
10237 bar2map_len = pci_resource_len(pdev, PCI_64BIT_BAR4);
10238 phba->sli4_hba.drbl_regs_memmap_p =
10239 ioremap(phba->pci_bar2_map,
10240 bar2map_len);
10241 if (!phba->sli4_hba.drbl_regs_memmap_p) {
10242 dev_err(&pdev->dev,
10243 "ioremap failed for SLI4 HBA"
10244 " doorbell registers.\n");
10245 error = -ENOMEM;
10246 goto out_iounmap_ctrl;
10247 }
10248 phba->pci_bar4_memmap_p =
10249 phba->sli4_hba.drbl_regs_memmap_p;
10250 error = lpfc_sli4_bar2_register_memmap(phba, LPFC_VF0);
10251 if (error)
10252 goto out_iounmap_all;
10253 } else {
10254 error = -ENOMEM;
James Smart2fcee4b2010-12-15 17:57:46 -050010255 goto out_iounmap_all;
James Smarte4b97942017-11-20 16:00:31 -080010256 }
James Smartda0436e2009-05-22 14:51:39 -040010257 }
10258
James Smart1351e692018-02-22 08:18:43 -080010259 if (if_type == LPFC_SLI_INTF_IF_TYPE_6 &&
10260 pci_resource_start(pdev, PCI_64BIT_BAR4)) {
10261 /*
10262 * Map SLI4 if type 6 HBA DPP Register base to a kernel
10263 * virtual address and setup the registers.
10264 */
10265 phba->pci_bar2_map = pci_resource_start(pdev, PCI_64BIT_BAR4);
10266 bar2map_len = pci_resource_len(pdev, PCI_64BIT_BAR4);
10267 phba->sli4_hba.dpp_regs_memmap_p =
10268 ioremap(phba->pci_bar2_map, bar2map_len);
10269 if (!phba->sli4_hba.dpp_regs_memmap_p) {
10270 dev_err(&pdev->dev,
10271 "ioremap failed for SLI4 HBA dpp registers.\n");
Dan Carpenter3a487ff2019-03-07 08:33:44 +030010272 error = -ENOMEM;
James Smart1351e692018-02-22 08:18:43 -080010273 goto out_iounmap_ctrl;
10274 }
10275 phba->pci_bar4_memmap_p = phba->sli4_hba.dpp_regs_memmap_p;
10276 }
10277
James Smartb71413d2018-02-22 08:18:40 -080010278 /* Set up the EQ/CQ register handeling functions now */
James Smart27d6ac02018-02-22 08:18:42 -080010279 switch (if_type) {
10280 case LPFC_SLI_INTF_IF_TYPE_0:
10281 case LPFC_SLI_INTF_IF_TYPE_2:
James Smartb71413d2018-02-22 08:18:40 -080010282 phba->sli4_hba.sli4_eq_clr_intr = lpfc_sli4_eq_clr_intr;
James Smart32517fc2019-01-28 11:14:33 -080010283 phba->sli4_hba.sli4_write_eq_db = lpfc_sli4_write_eq_db;
10284 phba->sli4_hba.sli4_write_cq_db = lpfc_sli4_write_cq_db;
James Smart27d6ac02018-02-22 08:18:42 -080010285 break;
10286 case LPFC_SLI_INTF_IF_TYPE_6:
10287 phba->sli4_hba.sli4_eq_clr_intr = lpfc_sli4_if6_eq_clr_intr;
James Smart32517fc2019-01-28 11:14:33 -080010288 phba->sli4_hba.sli4_write_eq_db = lpfc_sli4_if6_write_eq_db;
10289 phba->sli4_hba.sli4_write_cq_db = lpfc_sli4_if6_write_cq_db;
James Smart27d6ac02018-02-22 08:18:42 -080010290 break;
10291 default:
10292 break;
James Smartb71413d2018-02-22 08:18:40 -080010293 }
10294
James Smartda0436e2009-05-22 14:51:39 -040010295 return 0;
10296
10297out_iounmap_all:
10298 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
10299out_iounmap_ctrl:
10300 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
10301out_iounmap_conf:
10302 iounmap(phba->sli4_hba.conf_regs_memmap_p);
Dan Carpenter3a487ff2019-03-07 08:33:44 +030010303
James Smartda0436e2009-05-22 14:51:39 -040010304 return error;
10305}
10306
10307/**
10308 * lpfc_sli4_pci_mem_unset - Unset SLI4 HBA PCI memory space.
10309 * @phba: pointer to lpfc hba data structure.
10310 *
10311 * This routine is invoked to unset the PCI device memory space for device
10312 * with SLI-4 interface spec.
10313 **/
10314static void
10315lpfc_sli4_pci_mem_unset(struct lpfc_hba *phba)
10316{
James Smart2e90f4b2011-12-13 13:22:37 -050010317 uint32_t if_type;
10318 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
James Smartda0436e2009-05-22 14:51:39 -040010319
James Smart2e90f4b2011-12-13 13:22:37 -050010320 switch (if_type) {
10321 case LPFC_SLI_INTF_IF_TYPE_0:
10322 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
10323 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
10324 iounmap(phba->sli4_hba.conf_regs_memmap_p);
10325 break;
10326 case LPFC_SLI_INTF_IF_TYPE_2:
10327 iounmap(phba->sli4_hba.conf_regs_memmap_p);
10328 break;
James Smart27d6ac02018-02-22 08:18:42 -080010329 case LPFC_SLI_INTF_IF_TYPE_6:
10330 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
10331 iounmap(phba->sli4_hba.conf_regs_memmap_p);
10332 break;
James Smart2e90f4b2011-12-13 13:22:37 -050010333 case LPFC_SLI_INTF_IF_TYPE_1:
10334 default:
10335 dev_printk(KERN_ERR, &phba->pcidev->dev,
10336 "FATAL - unsupported SLI4 interface type - %d\n",
10337 if_type);
10338 break;
10339 }
James Smartda0436e2009-05-22 14:51:39 -040010340}
10341
10342/**
James Smart3772a992009-05-22 14:50:54 -040010343 * lpfc_sli_enable_msix - Enable MSI-X interrupt mode on SLI-3 device
10344 * @phba: pointer to lpfc hba data structure.
10345 *
10346 * This routine is invoked to enable the MSI-X interrupt vectors to device
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010347 * with SLI-3 interface specs.
James Smart3772a992009-05-22 14:50:54 -040010348 *
10349 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020010350 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -040010351 * other values - error
10352 **/
10353static int
10354lpfc_sli_enable_msix(struct lpfc_hba *phba)
10355{
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010356 int rc;
James Smart3772a992009-05-22 14:50:54 -040010357 LPFC_MBOXQ_t *pmb;
10358
10359 /* Set up MSI-X multi-message vectors */
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010360 rc = pci_alloc_irq_vectors(phba->pcidev,
10361 LPFC_MSIX_VECTORS, LPFC_MSIX_VECTORS, PCI_IRQ_MSIX);
10362 if (rc < 0) {
James Smart3772a992009-05-22 14:50:54 -040010363 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10364 "0420 PCI enable MSI-X failed (%d)\n", rc);
Alexander Gordeev029165a2014-07-16 20:05:15 +020010365 goto vec_fail_out;
James Smart3772a992009-05-22 14:50:54 -040010366 }
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010367
James Smart3772a992009-05-22 14:50:54 -040010368 /*
10369 * Assign MSI-X vectors to interrupt handlers
10370 */
10371
10372 /* vector-0 is associated to slow-path handler */
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010373 rc = request_irq(pci_irq_vector(phba->pcidev, 0),
James Smarted243d32015-05-21 13:55:25 -040010374 &lpfc_sli_sp_intr_handler, 0,
James Smart3772a992009-05-22 14:50:54 -040010375 LPFC_SP_DRIVER_HANDLER_NAME, phba);
10376 if (rc) {
10377 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
10378 "0421 MSI-X slow-path request_irq failed "
10379 "(%d)\n", rc);
10380 goto msi_fail_out;
10381 }
10382
10383 /* vector-1 is associated to fast-path handler */
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010384 rc = request_irq(pci_irq_vector(phba->pcidev, 1),
James Smarted243d32015-05-21 13:55:25 -040010385 &lpfc_sli_fp_intr_handler, 0,
James Smart3772a992009-05-22 14:50:54 -040010386 LPFC_FP_DRIVER_HANDLER_NAME, phba);
10387
10388 if (rc) {
10389 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
10390 "0429 MSI-X fast-path request_irq failed "
10391 "(%d)\n", rc);
10392 goto irq_fail_out;
10393 }
10394
10395 /*
10396 * Configure HBA MSI-X attention conditions to messages
10397 */
10398 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10399
10400 if (!pmb) {
10401 rc = -ENOMEM;
10402 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10403 "0474 Unable to allocate memory for issuing "
10404 "MBOX_CONFIG_MSI command\n");
10405 goto mem_fail_out;
10406 }
10407 rc = lpfc_config_msi(phba, pmb);
10408 if (rc)
10409 goto mbx_fail_out;
10410 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
10411 if (rc != MBX_SUCCESS) {
10412 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
10413 "0351 Config MSI mailbox command failed, "
10414 "mbxCmd x%x, mbxStatus x%x\n",
10415 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus);
10416 goto mbx_fail_out;
10417 }
10418
10419 /* Free memory allocated for mailbox command */
10420 mempool_free(pmb, phba->mbox_mem_pool);
10421 return rc;
10422
10423mbx_fail_out:
10424 /* Free memory allocated for mailbox command */
10425 mempool_free(pmb, phba->mbox_mem_pool);
10426
10427mem_fail_out:
10428 /* free the irq already requested */
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010429 free_irq(pci_irq_vector(phba->pcidev, 1), phba);
James Smart3772a992009-05-22 14:50:54 -040010430
10431irq_fail_out:
10432 /* free the irq already requested */
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010433 free_irq(pci_irq_vector(phba->pcidev, 0), phba);
James Smart3772a992009-05-22 14:50:54 -040010434
10435msi_fail_out:
10436 /* Unconfigure MSI-X capability structure */
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010437 pci_free_irq_vectors(phba->pcidev);
Alexander Gordeev029165a2014-07-16 20:05:15 +020010438
10439vec_fail_out:
James Smart3772a992009-05-22 14:50:54 -040010440 return rc;
10441}
10442
10443/**
James Smart3772a992009-05-22 14:50:54 -040010444 * lpfc_sli_enable_msi - Enable MSI interrupt mode on SLI-3 device.
10445 * @phba: pointer to lpfc hba data structure.
10446 *
10447 * This routine is invoked to enable the MSI interrupt mode to device with
10448 * SLI-3 interface spec. The kernel function pci_enable_msi() is called to
10449 * enable the MSI vector. The device driver is responsible for calling the
10450 * request_irq() to register MSI vector with a interrupt the handler, which
10451 * is done in this function.
10452 *
10453 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020010454 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -040010455 * other values - error
10456 */
10457static int
10458lpfc_sli_enable_msi(struct lpfc_hba *phba)
10459{
10460 int rc;
10461
10462 rc = pci_enable_msi(phba->pcidev);
10463 if (!rc)
10464 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10465 "0462 PCI enable MSI mode success.\n");
10466 else {
10467 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10468 "0471 PCI enable MSI mode failed (%d)\n", rc);
10469 return rc;
10470 }
10471
10472 rc = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
James Smarted243d32015-05-21 13:55:25 -040010473 0, LPFC_DRIVER_NAME, phba);
James Smart3772a992009-05-22 14:50:54 -040010474 if (rc) {
10475 pci_disable_msi(phba->pcidev);
10476 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
10477 "0478 MSI request_irq failed (%d)\n", rc);
10478 }
10479 return rc;
10480}
10481
10482/**
James Smart3772a992009-05-22 14:50:54 -040010483 * lpfc_sli_enable_intr - Enable device interrupt to SLI-3 device.
10484 * @phba: pointer to lpfc hba data structure.
10485 *
10486 * This routine is invoked to enable device interrupt and associate driver's
10487 * interrupt handler(s) to interrupt vector(s) to device with SLI-3 interface
10488 * spec. Depends on the interrupt mode configured to the driver, the driver
10489 * will try to fallback from the configured interrupt mode to an interrupt
10490 * mode which is supported by the platform, kernel, and device in the order
10491 * of:
10492 * MSI-X -> MSI -> IRQ.
10493 *
10494 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020010495 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -040010496 * other values - error
10497 **/
10498static uint32_t
10499lpfc_sli_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
10500{
10501 uint32_t intr_mode = LPFC_INTR_ERROR;
10502 int retval;
10503
10504 if (cfg_mode == 2) {
10505 /* Need to issue conf_port mbox cmd before conf_msi mbox cmd */
10506 retval = lpfc_sli_config_port(phba, LPFC_SLI_REV3);
10507 if (!retval) {
10508 /* Now, try to enable MSI-X interrupt mode */
10509 retval = lpfc_sli_enable_msix(phba);
10510 if (!retval) {
10511 /* Indicate initialization to MSI-X mode */
10512 phba->intr_type = MSIX;
10513 intr_mode = 2;
10514 }
10515 }
10516 }
10517
10518 /* Fallback to MSI if MSI-X initialization failed */
10519 if (cfg_mode >= 1 && phba->intr_type == NONE) {
10520 retval = lpfc_sli_enable_msi(phba);
10521 if (!retval) {
10522 /* Indicate initialization to MSI mode */
10523 phba->intr_type = MSI;
10524 intr_mode = 1;
10525 }
10526 }
10527
10528 /* Fallback to INTx if both MSI-X/MSI initalization failed */
10529 if (phba->intr_type == NONE) {
10530 retval = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
10531 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
10532 if (!retval) {
10533 /* Indicate initialization to INTx mode */
10534 phba->intr_type = INTx;
10535 intr_mode = 0;
10536 }
10537 }
10538 return intr_mode;
10539}
10540
10541/**
10542 * lpfc_sli_disable_intr - Disable device interrupt to SLI-3 device.
10543 * @phba: pointer to lpfc hba data structure.
10544 *
10545 * This routine is invoked to disable device interrupt and disassociate the
10546 * driver's interrupt handler(s) from interrupt vector(s) to device with
10547 * SLI-3 interface spec. Depending on the interrupt mode, the driver will
10548 * release the interrupt vector(s) for the message signaled interrupt.
10549 **/
10550static void
10551lpfc_sli_disable_intr(struct lpfc_hba *phba)
10552{
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010553 int nr_irqs, i;
10554
James Smart3772a992009-05-22 14:50:54 -040010555 if (phba->intr_type == MSIX)
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010556 nr_irqs = LPFC_MSIX_VECTORS;
10557 else
10558 nr_irqs = 1;
10559
10560 for (i = 0; i < nr_irqs; i++)
10561 free_irq(pci_irq_vector(phba->pcidev, i), phba);
10562 pci_free_irq_vectors(phba->pcidev);
James Smart3772a992009-05-22 14:50:54 -040010563
10564 /* Reset interrupt management states */
10565 phba->intr_type = NONE;
10566 phba->sli.slistat.sli_intr = 0;
James Smart3772a992009-05-22 14:50:54 -040010567}
10568
10569/**
James Smart6a828b02019-01-28 11:14:31 -080010570 * lpfc_find_cpu_handle - Find the CPU that corresponds to the specified EQ
10571 * @phba: pointer to lpfc hba data structure.
10572 * @id: EQ vector index or Hardware Queue index
10573 * @match: LPFC_FIND_BY_EQ = match by EQ
10574 * LPFC_FIND_BY_HDWQ = match by Hardware Queue
10575 */
10576static uint16_t
10577lpfc_find_cpu_handle(struct lpfc_hba *phba, uint16_t id, int match)
10578{
10579 struct lpfc_vector_map_info *cpup;
10580 int cpu;
10581
10582 /* Find the desired phys_id for the specified EQ */
James Smart222e9232019-01-28 11:14:35 -080010583 for_each_present_cpu(cpu) {
10584 cpup = &phba->sli4_hba.cpu_map[cpu];
James Smart6a828b02019-01-28 11:14:31 -080010585 if ((match == LPFC_FIND_BY_EQ) &&
10586 (cpup->irq != LPFC_VECTOR_MAP_EMPTY) &&
10587 (cpup->eq == id))
10588 return cpu;
10589 if ((match == LPFC_FIND_BY_HDWQ) && (cpup->hdwq == id))
10590 return cpu;
James Smart6a828b02019-01-28 11:14:31 -080010591 }
10592 return 0;
10593}
10594
10595/**
10596 * lpfc_find_eq_handle - Find the EQ that corresponds to the specified
10597 * Hardware Queue
10598 * @phba: pointer to lpfc hba data structure.
10599 * @hdwq: Hardware Queue index
10600 */
10601static uint16_t
10602lpfc_find_eq_handle(struct lpfc_hba *phba, uint16_t hdwq)
10603{
10604 struct lpfc_vector_map_info *cpup;
10605 int cpu;
10606
10607 /* Find the desired phys_id for the specified EQ */
James Smart222e9232019-01-28 11:14:35 -080010608 for_each_present_cpu(cpu) {
10609 cpup = &phba->sli4_hba.cpu_map[cpu];
James Smart6a828b02019-01-28 11:14:31 -080010610 if (cpup->hdwq == hdwq)
10611 return cpup->eq;
James Smart6a828b02019-01-28 11:14:31 -080010612 }
10613 return 0;
10614}
10615
James Smart6a828b02019-01-28 11:14:31 -080010616#ifdef CONFIG_X86
10617/**
10618 * lpfc_find_hyper - Determine if the CPU map entry is hyper-threaded
10619 * @phba: pointer to lpfc hba data structure.
10620 * @cpu: CPU map index
10621 * @phys_id: CPU package physical id
10622 * @core_id: CPU core id
10623 */
10624static int
10625lpfc_find_hyper(struct lpfc_hba *phba, int cpu,
10626 uint16_t phys_id, uint16_t core_id)
10627{
10628 struct lpfc_vector_map_info *cpup;
10629 int idx;
10630
James Smart222e9232019-01-28 11:14:35 -080010631 for_each_present_cpu(idx) {
10632 cpup = &phba->sli4_hba.cpu_map[idx];
James Smart6a828b02019-01-28 11:14:31 -080010633 /* Does the cpup match the one we are looking for */
10634 if ((cpup->phys_id == phys_id) &&
10635 (cpup->core_id == core_id) &&
James Smart222e9232019-01-28 11:14:35 -080010636 (cpu != idx))
James Smart6a828b02019-01-28 11:14:31 -080010637 return 1;
James Smart6a828b02019-01-28 11:14:31 -080010638 }
10639 return 0;
10640}
10641#endif
10642
10643/**
James Smart895427b2017-02-12 13:52:30 -080010644 * lpfc_cpu_affinity_check - Check vector CPU affinity mappings
James Smart7bb03bb2013-04-17 20:19:16 -040010645 * @phba: pointer to lpfc hba data structure.
James Smart895427b2017-02-12 13:52:30 -080010646 * @vectors: number of msix vectors allocated.
James Smart7bb03bb2013-04-17 20:19:16 -040010647 *
James Smart895427b2017-02-12 13:52:30 -080010648 * The routine will figure out the CPU affinity assignment for every
James Smart6a828b02019-01-28 11:14:31 -080010649 * MSI-X vector allocated for the HBA.
James Smart895427b2017-02-12 13:52:30 -080010650 * In addition, the CPU to IO channel mapping will be calculated
10651 * and the phba->sli4_hba.cpu_map array will reflect this.
James Smart7bb03bb2013-04-17 20:19:16 -040010652 */
James Smart895427b2017-02-12 13:52:30 -080010653static void
10654lpfc_cpu_affinity_check(struct lpfc_hba *phba, int vectors)
James Smart7bb03bb2013-04-17 20:19:16 -040010655{
YueHaibing59e54d92019-02-14 01:51:52 +000010656 int i, cpu, idx;
James Smart6a828b02019-01-28 11:14:31 -080010657 int max_phys_id, min_phys_id;
10658 int max_core_id, min_core_id;
James Smart7bb03bb2013-04-17 20:19:16 -040010659 struct lpfc_vector_map_info *cpup;
James Smart75508a82019-01-28 11:14:34 -080010660 const struct cpumask *maskp;
James Smart7bb03bb2013-04-17 20:19:16 -040010661#ifdef CONFIG_X86
10662 struct cpuinfo_x86 *cpuinfo;
10663#endif
James Smart7bb03bb2013-04-17 20:19:16 -040010664
10665 /* Init cpu_map array */
10666 memset(phba->sli4_hba.cpu_map, 0xff,
10667 (sizeof(struct lpfc_vector_map_info) *
James Smart222e9232019-01-28 11:14:35 -080010668 phba->sli4_hba.num_possible_cpu));
James Smart7bb03bb2013-04-17 20:19:16 -040010669
James Smart6a828b02019-01-28 11:14:31 -080010670 max_phys_id = 0;
10671 min_phys_id = 0xffff;
10672 max_core_id = 0;
10673 min_core_id = 0xffff;
James Smart7bb03bb2013-04-17 20:19:16 -040010674
10675 /* Update CPU map with physical id and core id of each CPU */
James Smart222e9232019-01-28 11:14:35 -080010676 for_each_present_cpu(cpu) {
10677 cpup = &phba->sli4_hba.cpu_map[cpu];
James Smart7bb03bb2013-04-17 20:19:16 -040010678#ifdef CONFIG_X86
10679 cpuinfo = &cpu_data(cpu);
10680 cpup->phys_id = cpuinfo->phys_proc_id;
10681 cpup->core_id = cpuinfo->cpu_core_id;
James Smart6a828b02019-01-28 11:14:31 -080010682 cpup->hyper = lpfc_find_hyper(phba, cpu,
10683 cpup->phys_id, cpup->core_id);
James Smart7bb03bb2013-04-17 20:19:16 -040010684#else
10685 /* No distinction between CPUs for other platforms */
10686 cpup->phys_id = 0;
James Smart6a828b02019-01-28 11:14:31 -080010687 cpup->core_id = cpu;
10688 cpup->hyper = 0;
James Smart7bb03bb2013-04-17 20:19:16 -040010689#endif
James Smart7bb03bb2013-04-17 20:19:16 -040010690
James Smartb3295c22019-01-28 11:14:30 -080010691 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10692 "3328 CPU physid %d coreid %d\n",
10693 cpup->phys_id, cpup->core_id);
James Smart6a828b02019-01-28 11:14:31 -080010694
10695 if (cpup->phys_id > max_phys_id)
10696 max_phys_id = cpup->phys_id;
10697 if (cpup->phys_id < min_phys_id)
10698 min_phys_id = cpup->phys_id;
10699
10700 if (cpup->core_id > max_core_id)
10701 max_core_id = cpup->core_id;
10702 if (cpup->core_id < min_core_id)
10703 min_core_id = cpup->core_id;
James Smart7bb03bb2013-04-17 20:19:16 -040010704 }
James Smartb3295c22019-01-28 11:14:30 -080010705
James Smart32517fc2019-01-28 11:14:33 -080010706 for_each_possible_cpu(i) {
10707 struct lpfc_eq_intr_info *eqi =
10708 per_cpu_ptr(phba->sli4_hba.eq_info, i);
10709
10710 INIT_LIST_HEAD(&eqi->list);
10711 eqi->icnt = 0;
10712 }
10713
James Smart75508a82019-01-28 11:14:34 -080010714 for (idx = 0; idx < phba->cfg_irq_chann; idx++) {
10715 maskp = pci_irq_get_affinity(phba->pcidev, idx);
10716 if (!maskp)
10717 continue;
10718
10719 for_each_cpu_and(cpu, maskp, cpu_present_mask) {
10720 cpup = &phba->sli4_hba.cpu_map[cpu];
James Smart6a828b02019-01-28 11:14:31 -080010721 cpup->eq = idx;
10722 cpup->hdwq = idx;
10723 cpup->irq = pci_irq_vector(phba->pcidev, idx);
James Smartb3295c22019-01-28 11:14:30 -080010724
James Smart75508a82019-01-28 11:14:34 -080010725 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart6a828b02019-01-28 11:14:31 -080010726 "3336 Set Affinity: CPU %d "
James Smart75508a82019-01-28 11:14:34 -080010727 "hdwq %d irq %d\n",
10728 cpu, cpup->hdwq, cpup->irq);
James Smart6a828b02019-01-28 11:14:31 -080010729 }
James Smartb3295c22019-01-28 11:14:30 -080010730 }
10731 return;
James Smart7bb03bb2013-04-17 20:19:16 -040010732}
James Smart7bb03bb2013-04-17 20:19:16 -040010733
10734/**
James Smartda0436e2009-05-22 14:51:39 -040010735 * lpfc_sli4_enable_msix - Enable MSI-X interrupt mode to SLI-4 device
10736 * @phba: pointer to lpfc hba data structure.
10737 *
10738 * This routine is invoked to enable the MSI-X interrupt vectors to device
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010739 * with SLI-4 interface spec.
James Smartda0436e2009-05-22 14:51:39 -040010740 *
10741 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020010742 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -040010743 * other values - error
10744 **/
10745static int
10746lpfc_sli4_enable_msix(struct lpfc_hba *phba)
10747{
James Smart75baf692010-06-08 18:31:21 -040010748 int vectors, rc, index;
James Smartb83d0052017-06-01 21:07:05 -070010749 char *name;
James Smartda0436e2009-05-22 14:51:39 -040010750
10751 /* Set up MSI-X multi-message vectors */
James Smart6a828b02019-01-28 11:14:31 -080010752 vectors = phba->cfg_irq_chann;
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010753
James Smartf358dd02017-02-12 13:52:34 -080010754 rc = pci_alloc_irq_vectors(phba->pcidev,
James Smart75508a82019-01-28 11:14:34 -080010755 1,
James Smartf358dd02017-02-12 13:52:34 -080010756 vectors, PCI_IRQ_MSIX | PCI_IRQ_AFFINITY);
Alexander Gordeev4f871e12014-09-03 12:56:29 -040010757 if (rc < 0) {
James Smartda0436e2009-05-22 14:51:39 -040010758 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10759 "0484 PCI enable MSI-X failed (%d)\n", rc);
Alexander Gordeev029165a2014-07-16 20:05:15 +020010760 goto vec_fail_out;
James Smartda0436e2009-05-22 14:51:39 -040010761 }
Alexander Gordeev4f871e12014-09-03 12:56:29 -040010762 vectors = rc;
James Smart75baf692010-06-08 18:31:21 -040010763
James Smart7bb03bb2013-04-17 20:19:16 -040010764 /* Assign MSI-X vectors to interrupt handlers */
James Smart67d12732012-08-03 12:36:13 -040010765 for (index = 0; index < vectors; index++) {
James Smartb83d0052017-06-01 21:07:05 -070010766 name = phba->sli4_hba.hba_eq_hdl[index].handler_name;
10767 memset(name, 0, LPFC_SLI4_HANDLER_NAME_SZ);
10768 snprintf(name, LPFC_SLI4_HANDLER_NAME_SZ,
James Smart4305f182012-08-03 12:36:33 -040010769 LPFC_DRIVER_HANDLER_NAME"%d", index);
James Smartda0436e2009-05-22 14:51:39 -040010770
James Smart895427b2017-02-12 13:52:30 -080010771 phba->sli4_hba.hba_eq_hdl[index].idx = index;
10772 phba->sli4_hba.hba_eq_hdl[index].phba = phba;
James Smart7370d102019-01-28 11:14:20 -080010773 rc = request_irq(pci_irq_vector(phba->pcidev, index),
10774 &lpfc_sli4_hba_intr_handler, 0,
10775 name,
10776 &phba->sli4_hba.hba_eq_hdl[index]);
James Smartda0436e2009-05-22 14:51:39 -040010777 if (rc) {
10778 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
10779 "0486 MSI-X fast-path (%d) "
10780 "request_irq failed (%d)\n", index, rc);
10781 goto cfg_fail_out;
10782 }
10783 }
10784
James Smart6a828b02019-01-28 11:14:31 -080010785 if (vectors != phba->cfg_irq_chann) {
James Smart82c3e9b2012-09-29 11:29:50 -040010786 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10787 "3238 Reducing IO channels to match number of "
10788 "MSI-X vectors, requested %d got %d\n",
James Smart6a828b02019-01-28 11:14:31 -080010789 phba->cfg_irq_chann, vectors);
10790 if (phba->cfg_irq_chann > vectors)
10791 phba->cfg_irq_chann = vectors;
James Smart982ab122019-03-12 16:30:10 -070010792 if (phba->nvmet_support && (phba->cfg_nvmet_mrq > vectors))
James Smartcdb42be2019-01-28 11:14:21 -080010793 phba->cfg_nvmet_mrq = vectors;
James Smart82c3e9b2012-09-29 11:29:50 -040010794 }
James Smart7bb03bb2013-04-17 20:19:16 -040010795
James Smartda0436e2009-05-22 14:51:39 -040010796 return rc;
10797
10798cfg_fail_out:
10799 /* free the irq already requested */
James Smart895427b2017-02-12 13:52:30 -080010800 for (--index; index >= 0; index--)
10801 free_irq(pci_irq_vector(phba->pcidev, index),
10802 &phba->sli4_hba.hba_eq_hdl[index]);
James Smartda0436e2009-05-22 14:51:39 -040010803
James Smartda0436e2009-05-22 14:51:39 -040010804 /* Unconfigure MSI-X capability structure */
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010805 pci_free_irq_vectors(phba->pcidev);
Alexander Gordeev029165a2014-07-16 20:05:15 +020010806
10807vec_fail_out:
James Smartda0436e2009-05-22 14:51:39 -040010808 return rc;
10809}
10810
10811/**
James Smartda0436e2009-05-22 14:51:39 -040010812 * lpfc_sli4_enable_msi - Enable MSI interrupt mode to SLI-4 device
10813 * @phba: pointer to lpfc hba data structure.
10814 *
10815 * This routine is invoked to enable the MSI interrupt mode to device with
10816 * SLI-4 interface spec. The kernel function pci_enable_msi() is called
10817 * to enable the MSI vector. The device driver is responsible for calling
10818 * the request_irq() to register MSI vector with a interrupt the handler,
10819 * which is done in this function.
10820 *
10821 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020010822 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -040010823 * other values - error
10824 **/
10825static int
10826lpfc_sli4_enable_msi(struct lpfc_hba *phba)
10827{
10828 int rc, index;
10829
10830 rc = pci_enable_msi(phba->pcidev);
10831 if (!rc)
10832 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10833 "0487 PCI enable MSI mode success.\n");
10834 else {
10835 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10836 "0488 PCI enable MSI mode failed (%d)\n", rc);
10837 return rc;
10838 }
10839
10840 rc = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
James Smarted243d32015-05-21 13:55:25 -040010841 0, LPFC_DRIVER_NAME, phba);
James Smartda0436e2009-05-22 14:51:39 -040010842 if (rc) {
10843 pci_disable_msi(phba->pcidev);
10844 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
10845 "0490 MSI request_irq failed (%d)\n", rc);
James Smart75baf692010-06-08 18:31:21 -040010846 return rc;
James Smartda0436e2009-05-22 14:51:39 -040010847 }
10848
James Smart6a828b02019-01-28 11:14:31 -080010849 for (index = 0; index < phba->cfg_irq_chann; index++) {
James Smart895427b2017-02-12 13:52:30 -080010850 phba->sli4_hba.hba_eq_hdl[index].idx = index;
10851 phba->sli4_hba.hba_eq_hdl[index].phba = phba;
James Smartda0436e2009-05-22 14:51:39 -040010852 }
10853
James Smart75baf692010-06-08 18:31:21 -040010854 return 0;
James Smartda0436e2009-05-22 14:51:39 -040010855}
10856
10857/**
James Smartda0436e2009-05-22 14:51:39 -040010858 * lpfc_sli4_enable_intr - Enable device interrupt to SLI-4 device
10859 * @phba: pointer to lpfc hba data structure.
10860 *
10861 * This routine is invoked to enable device interrupt and associate driver's
10862 * interrupt handler(s) to interrupt vector(s) to device with SLI-4
10863 * interface spec. Depends on the interrupt mode configured to the driver,
10864 * the driver will try to fallback from the configured interrupt mode to an
10865 * interrupt mode which is supported by the platform, kernel, and device in
10866 * the order of:
10867 * MSI-X -> MSI -> IRQ.
10868 *
10869 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020010870 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -040010871 * other values - error
10872 **/
10873static uint32_t
10874lpfc_sli4_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
10875{
10876 uint32_t intr_mode = LPFC_INTR_ERROR;
James Smart895427b2017-02-12 13:52:30 -080010877 int retval, idx;
James Smartda0436e2009-05-22 14:51:39 -040010878
10879 if (cfg_mode == 2) {
10880 /* Preparation before conf_msi mbox cmd */
10881 retval = 0;
10882 if (!retval) {
10883 /* Now, try to enable MSI-X interrupt mode */
10884 retval = lpfc_sli4_enable_msix(phba);
10885 if (!retval) {
10886 /* Indicate initialization to MSI-X mode */
10887 phba->intr_type = MSIX;
10888 intr_mode = 2;
10889 }
10890 }
10891 }
10892
10893 /* Fallback to MSI if MSI-X initialization failed */
10894 if (cfg_mode >= 1 && phba->intr_type == NONE) {
10895 retval = lpfc_sli4_enable_msi(phba);
10896 if (!retval) {
10897 /* Indicate initialization to MSI mode */
10898 phba->intr_type = MSI;
10899 intr_mode = 1;
10900 }
10901 }
10902
10903 /* Fallback to INTx if both MSI-X/MSI initalization failed */
10904 if (phba->intr_type == NONE) {
10905 retval = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
10906 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
10907 if (!retval) {
James Smart895427b2017-02-12 13:52:30 -080010908 struct lpfc_hba_eq_hdl *eqhdl;
10909
James Smartda0436e2009-05-22 14:51:39 -040010910 /* Indicate initialization to INTx mode */
10911 phba->intr_type = INTx;
10912 intr_mode = 0;
James Smart895427b2017-02-12 13:52:30 -080010913
James Smart6a828b02019-01-28 11:14:31 -080010914 for (idx = 0; idx < phba->cfg_irq_chann; idx++) {
James Smart895427b2017-02-12 13:52:30 -080010915 eqhdl = &phba->sli4_hba.hba_eq_hdl[idx];
10916 eqhdl->idx = idx;
10917 eqhdl->phba = phba;
James Smart1ba981f2014-02-20 09:56:45 -050010918 }
James Smartda0436e2009-05-22 14:51:39 -040010919 }
10920 }
10921 return intr_mode;
10922}
10923
10924/**
10925 * lpfc_sli4_disable_intr - Disable device interrupt to SLI-4 device
10926 * @phba: pointer to lpfc hba data structure.
10927 *
10928 * This routine is invoked to disable device interrupt and disassociate
10929 * the driver's interrupt handler(s) from interrupt vector(s) to device
10930 * with SLI-4 interface spec. Depending on the interrupt mode, the driver
10931 * will release the interrupt vector(s) for the message signaled interrupt.
10932 **/
10933static void
10934lpfc_sli4_disable_intr(struct lpfc_hba *phba)
10935{
10936 /* Disable the currently initialized interrupt mode */
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010937 if (phba->intr_type == MSIX) {
10938 int index;
10939
10940 /* Free up MSI-X multi-message vectors */
James Smart6a828b02019-01-28 11:14:31 -080010941 for (index = 0; index < phba->cfg_irq_chann; index++) {
James Smartb3295c22019-01-28 11:14:30 -080010942 irq_set_affinity_hint(
10943 pci_irq_vector(phba->pcidev, index),
10944 NULL);
James Smart895427b2017-02-12 13:52:30 -080010945 free_irq(pci_irq_vector(phba->pcidev, index),
10946 &phba->sli4_hba.hba_eq_hdl[index]);
James Smartb3295c22019-01-28 11:14:30 -080010947 }
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010948 } else {
James Smartda0436e2009-05-22 14:51:39 -040010949 free_irq(phba->pcidev->irq, phba);
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010950 }
10951
10952 pci_free_irq_vectors(phba->pcidev);
James Smartda0436e2009-05-22 14:51:39 -040010953
10954 /* Reset interrupt management states */
10955 phba->intr_type = NONE;
10956 phba->sli.slistat.sli_intr = 0;
James Smartda0436e2009-05-22 14:51:39 -040010957}
10958
10959/**
James Smart3772a992009-05-22 14:50:54 -040010960 * lpfc_unset_hba - Unset SLI3 hba device initialization
10961 * @phba: pointer to lpfc hba data structure.
10962 *
10963 * This routine is invoked to unset the HBA device initialization steps to
10964 * a device with SLI-3 interface spec.
10965 **/
10966static void
10967lpfc_unset_hba(struct lpfc_hba *phba)
10968{
10969 struct lpfc_vport *vport = phba->pport;
10970 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
10971
10972 spin_lock_irq(shost->host_lock);
10973 vport->load_flag |= FC_UNLOADING;
10974 spin_unlock_irq(shost->host_lock);
10975
James Smart72859902012-01-18 16:25:38 -050010976 kfree(phba->vpi_bmask);
10977 kfree(phba->vpi_ids);
10978
James Smart3772a992009-05-22 14:50:54 -040010979 lpfc_stop_hba_timers(phba);
10980
10981 phba->pport->work_port_events = 0;
10982
10983 lpfc_sli_hba_down(phba);
10984
10985 lpfc_sli_brdrestart(phba);
10986
10987 lpfc_sli_disable_intr(phba);
10988
10989 return;
10990}
10991
10992/**
James Smart5af5eee2010-10-22 11:06:38 -040010993 * lpfc_sli4_xri_exchange_busy_wait - Wait for device XRI exchange busy
10994 * @phba: Pointer to HBA context object.
10995 *
10996 * This function is called in the SLI4 code path to wait for completion
10997 * of device's XRIs exchange busy. It will check the XRI exchange busy
10998 * on outstanding FCP and ELS I/Os every 10ms for up to 10 seconds; after
10999 * that, it will check the XRI exchange busy on outstanding FCP and ELS
11000 * I/Os every 30 seconds, log error message, and wait forever. Only when
11001 * all XRI exchange busy complete, the driver unload shall proceed with
11002 * invoking the function reset ioctl mailbox command to the CNA and the
11003 * the rest of the driver unload resource release.
11004 **/
11005static void
11006lpfc_sli4_xri_exchange_busy_wait(struct lpfc_hba *phba)
11007{
James Smart5e5b5112019-01-28 11:14:22 -080011008 struct lpfc_sli4_hdw_queue *qp;
11009 int idx, ccnt, fcnt;
James Smart5af5eee2010-10-22 11:06:38 -040011010 int wait_time = 0;
James Smart5e5b5112019-01-28 11:14:22 -080011011 int io_xri_cmpl = 1;
James Smart86c67372017-04-21 16:05:04 -070011012 int nvmet_xri_cmpl = 1;
James Smart895427b2017-02-12 13:52:30 -080011013 int fcp_xri_cmpl = 1;
James Smart5af5eee2010-10-22 11:06:38 -040011014 int els_xri_cmpl = list_empty(&phba->sli4_hba.lpfc_abts_els_sgl_list);
11015
James Smartc3725bd2017-11-20 16:00:42 -080011016 /* Driver just aborted IOs during the hba_unset process. Pause
11017 * here to give the HBA time to complete the IO and get entries
11018 * into the abts lists.
11019 */
11020 msleep(LPFC_XRI_EXCH_BUSY_WAIT_T1 * 5);
11021
11022 /* Wait for NVME pending IO to flush back to transport. */
11023 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
11024 lpfc_nvme_wait_for_io_drain(phba);
11025
James Smart5e5b5112019-01-28 11:14:22 -080011026 ccnt = 0;
11027 fcnt = 0;
11028 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
11029 qp = &phba->sli4_hba.hdwq[idx];
11030 fcp_xri_cmpl = list_empty(
11031 &qp->lpfc_abts_scsi_buf_list);
11032 if (!fcp_xri_cmpl) /* if list is NOT empty */
11033 fcnt++;
11034 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
11035 io_xri_cmpl = list_empty(
11036 &qp->lpfc_abts_nvme_buf_list);
11037 if (!io_xri_cmpl) /* if list is NOT empty */
11038 ccnt++;
11039 }
11040 }
11041 if (ccnt)
11042 io_xri_cmpl = 0;
11043 if (fcnt)
11044 fcp_xri_cmpl = 0;
11045
James Smart86c67372017-04-21 16:05:04 -070011046 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
James Smart86c67372017-04-21 16:05:04 -070011047 nvmet_xri_cmpl =
11048 list_empty(&phba->sli4_hba.lpfc_abts_nvmet_ctx_list);
11049 }
James Smart895427b2017-02-12 13:52:30 -080011050
James Smart5e5b5112019-01-28 11:14:22 -080011051 while (!fcp_xri_cmpl || !els_xri_cmpl || !io_xri_cmpl ||
James Smartf358dd02017-02-12 13:52:34 -080011052 !nvmet_xri_cmpl) {
James Smart5af5eee2010-10-22 11:06:38 -040011053 if (wait_time > LPFC_XRI_EXCH_BUSY_WAIT_TMO) {
James Smart68c9b552018-06-26 08:24:25 -070011054 if (!nvmet_xri_cmpl)
11055 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11056 "6424 NVMET XRI exchange busy "
11057 "wait time: %d seconds.\n",
11058 wait_time/1000);
James Smart5e5b5112019-01-28 11:14:22 -080011059 if (!io_xri_cmpl)
James Smart895427b2017-02-12 13:52:30 -080011060 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11061 "6100 NVME XRI exchange busy "
11062 "wait time: %d seconds.\n",
11063 wait_time/1000);
James Smart5af5eee2010-10-22 11:06:38 -040011064 if (!fcp_xri_cmpl)
11065 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11066 "2877 FCP XRI exchange busy "
11067 "wait time: %d seconds.\n",
11068 wait_time/1000);
11069 if (!els_xri_cmpl)
11070 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11071 "2878 ELS XRI exchange busy "
11072 "wait time: %d seconds.\n",
11073 wait_time/1000);
11074 msleep(LPFC_XRI_EXCH_BUSY_WAIT_T2);
11075 wait_time += LPFC_XRI_EXCH_BUSY_WAIT_T2;
11076 } else {
11077 msleep(LPFC_XRI_EXCH_BUSY_WAIT_T1);
11078 wait_time += LPFC_XRI_EXCH_BUSY_WAIT_T1;
11079 }
James Smart5e5b5112019-01-28 11:14:22 -080011080
11081 ccnt = 0;
11082 fcnt = 0;
11083 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
11084 qp = &phba->sli4_hba.hdwq[idx];
11085 fcp_xri_cmpl = list_empty(
11086 &qp->lpfc_abts_scsi_buf_list);
11087 if (!fcp_xri_cmpl) /* if list is NOT empty */
11088 fcnt++;
11089 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
11090 io_xri_cmpl = list_empty(
11091 &qp->lpfc_abts_nvme_buf_list);
11092 if (!io_xri_cmpl) /* if list is NOT empty */
11093 ccnt++;
11094 }
11095 }
11096 if (ccnt)
11097 io_xri_cmpl = 0;
11098 if (fcnt)
11099 fcp_xri_cmpl = 0;
11100
James Smart86c67372017-04-21 16:05:04 -070011101 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
James Smart86c67372017-04-21 16:05:04 -070011102 nvmet_xri_cmpl = list_empty(
11103 &phba->sli4_hba.lpfc_abts_nvmet_ctx_list);
11104 }
James Smart5af5eee2010-10-22 11:06:38 -040011105 els_xri_cmpl =
11106 list_empty(&phba->sli4_hba.lpfc_abts_els_sgl_list);
James Smartf358dd02017-02-12 13:52:34 -080011107
James Smart5af5eee2010-10-22 11:06:38 -040011108 }
11109}
11110
11111/**
James Smartda0436e2009-05-22 14:51:39 -040011112 * lpfc_sli4_hba_unset - Unset the fcoe hba
11113 * @phba: Pointer to HBA context object.
11114 *
11115 * This function is called in the SLI4 code path to reset the HBA's FCoE
11116 * function. The caller is not required to hold any lock. This routine
11117 * issues PCI function reset mailbox command to reset the FCoE function.
11118 * At the end of the function, it calls lpfc_hba_down_post function to
11119 * free any pending commands.
11120 **/
11121static void
11122lpfc_sli4_hba_unset(struct lpfc_hba *phba)
11123{
11124 int wait_cnt = 0;
11125 LPFC_MBOXQ_t *mboxq;
James Smart912e3ac2011-05-24 11:42:11 -040011126 struct pci_dev *pdev = phba->pcidev;
James Smartda0436e2009-05-22 14:51:39 -040011127
11128 lpfc_stop_hba_timers(phba);
James Smartcdb42be2019-01-28 11:14:21 -080011129 if (phba->pport)
11130 phba->sli4_hba.intr_enable = 0;
James Smartda0436e2009-05-22 14:51:39 -040011131
11132 /*
11133 * Gracefully wait out the potential current outstanding asynchronous
11134 * mailbox command.
11135 */
11136
11137 /* First, block any pending async mailbox command from posted */
11138 spin_lock_irq(&phba->hbalock);
11139 phba->sli.sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
11140 spin_unlock_irq(&phba->hbalock);
11141 /* Now, trying to wait it out if we can */
11142 while (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
11143 msleep(10);
11144 if (++wait_cnt > LPFC_ACTIVE_MBOX_WAIT_CNT)
11145 break;
11146 }
11147 /* Forcefully release the outstanding mailbox command if timed out */
11148 if (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
11149 spin_lock_irq(&phba->hbalock);
11150 mboxq = phba->sli.mbox_active;
11151 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
11152 __lpfc_mbox_cmpl_put(phba, mboxq);
11153 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
11154 phba->sli.mbox_active = NULL;
11155 spin_unlock_irq(&phba->hbalock);
11156 }
11157
James Smart5af5eee2010-10-22 11:06:38 -040011158 /* Abort all iocbs associated with the hba */
11159 lpfc_sli_hba_iocb_abort(phba);
11160
11161 /* Wait for completion of device XRI exchange busy */
11162 lpfc_sli4_xri_exchange_busy_wait(phba);
11163
James Smartda0436e2009-05-22 14:51:39 -040011164 /* Disable PCI subsystem interrupt */
11165 lpfc_sli4_disable_intr(phba);
11166
James Smart912e3ac2011-05-24 11:42:11 -040011167 /* Disable SR-IOV if enabled */
11168 if (phba->cfg_sriov_nr_virtfn)
11169 pci_disable_sriov(pdev);
11170
James Smartda0436e2009-05-22 14:51:39 -040011171 /* Stop kthread signal shall trigger work_done one more time */
11172 kthread_stop(phba->worker_thread);
11173
James Smartd2cc9bc2018-09-10 10:30:50 -070011174 /* Disable FW logging to host memory */
James Smart1165a5c2018-11-29 16:09:39 -080011175 lpfc_ras_stop_fwlog(phba);
James Smartd2cc9bc2018-09-10 10:30:50 -070011176
James Smartd1f525a2017-04-21 16:04:55 -070011177 /* Unset the queues shared with the hardware then release all
11178 * allocated resources.
11179 */
11180 lpfc_sli4_queue_unset(phba);
11181 lpfc_sli4_queue_destroy(phba);
11182
James Smart3677a3a2010-09-29 11:19:14 -040011183 /* Reset SLI4 HBA FCoE function */
11184 lpfc_pci_function_reset(phba);
11185
James Smart1165a5c2018-11-29 16:09:39 -080011186 /* Free RAS DMA memory */
11187 if (phba->ras_fwlog.ras_enabled)
11188 lpfc_sli4_ras_dma_free(phba);
11189
James Smartda0436e2009-05-22 14:51:39 -040011190 /* Stop the SLI4 device port */
James Smart1ffdd2c2019-03-04 15:27:51 -080011191 if (phba->pport)
11192 phba->pport->work_port_events = 0;
James Smartda0436e2009-05-22 14:51:39 -040011193}
11194
James Smart28baac72010-02-12 14:42:03 -050011195 /**
11196 * lpfc_pc_sli4_params_get - Get the SLI4_PARAMS port capabilities.
11197 * @phba: Pointer to HBA context object.
11198 * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
11199 *
11200 * This function is called in the SLI4 code path to read the port's
11201 * sli4 capabilities.
11202 *
11203 * This function may be be called from any context that can block-wait
11204 * for the completion. The expectation is that this routine is called
11205 * typically from probe_one or from the online routine.
11206 **/
11207int
11208lpfc_pc_sli4_params_get(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
11209{
11210 int rc;
11211 struct lpfc_mqe *mqe;
11212 struct lpfc_pc_sli4_params *sli4_params;
11213 uint32_t mbox_tmo;
11214
11215 rc = 0;
11216 mqe = &mboxq->u.mqe;
11217
11218 /* Read the port's SLI4 Parameters port capabilities */
James Smartfedd3b72011-02-16 12:39:24 -050011219 lpfc_pc_sli4_params(mboxq);
James Smart28baac72010-02-12 14:42:03 -050011220 if (!phba->sli4_hba.intr_enable)
11221 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
11222 else {
James Smarta183a152011-10-10 21:32:43 -040011223 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
James Smart28baac72010-02-12 14:42:03 -050011224 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
11225 }
11226
11227 if (unlikely(rc))
11228 return 1;
11229
11230 sli4_params = &phba->sli4_hba.pc_sli4_params;
11231 sli4_params->if_type = bf_get(if_type, &mqe->un.sli4_params);
11232 sli4_params->sli_rev = bf_get(sli_rev, &mqe->un.sli4_params);
11233 sli4_params->sli_family = bf_get(sli_family, &mqe->un.sli4_params);
11234 sli4_params->featurelevel_1 = bf_get(featurelevel_1,
11235 &mqe->un.sli4_params);
11236 sli4_params->featurelevel_2 = bf_get(featurelevel_2,
11237 &mqe->un.sli4_params);
11238 sli4_params->proto_types = mqe->un.sli4_params.word3;
11239 sli4_params->sge_supp_len = mqe->un.sli4_params.sge_supp_len;
11240 sli4_params->if_page_sz = bf_get(if_page_sz, &mqe->un.sli4_params);
11241 sli4_params->rq_db_window = bf_get(rq_db_window, &mqe->un.sli4_params);
11242 sli4_params->loopbk_scope = bf_get(loopbk_scope, &mqe->un.sli4_params);
11243 sli4_params->eq_pages_max = bf_get(eq_pages, &mqe->un.sli4_params);
11244 sli4_params->eqe_size = bf_get(eqe_size, &mqe->un.sli4_params);
11245 sli4_params->cq_pages_max = bf_get(cq_pages, &mqe->un.sli4_params);
11246 sli4_params->cqe_size = bf_get(cqe_size, &mqe->un.sli4_params);
11247 sli4_params->mq_pages_max = bf_get(mq_pages, &mqe->un.sli4_params);
11248 sli4_params->mqe_size = bf_get(mqe_size, &mqe->un.sli4_params);
11249 sli4_params->mq_elem_cnt = bf_get(mq_elem_cnt, &mqe->un.sli4_params);
11250 sli4_params->wq_pages_max = bf_get(wq_pages, &mqe->un.sli4_params);
11251 sli4_params->wqe_size = bf_get(wqe_size, &mqe->un.sli4_params);
11252 sli4_params->rq_pages_max = bf_get(rq_pages, &mqe->un.sli4_params);
11253 sli4_params->rqe_size = bf_get(rqe_size, &mqe->un.sli4_params);
11254 sli4_params->hdr_pages_max = bf_get(hdr_pages, &mqe->un.sli4_params);
11255 sli4_params->hdr_size = bf_get(hdr_size, &mqe->un.sli4_params);
11256 sli4_params->hdr_pp_align = bf_get(hdr_pp_align, &mqe->un.sli4_params);
11257 sli4_params->sgl_pages_max = bf_get(sgl_pages, &mqe->un.sli4_params);
11258 sli4_params->sgl_pp_align = bf_get(sgl_pp_align, &mqe->un.sli4_params);
James Smart05580562011-05-24 11:40:48 -040011259
11260 /* Make sure that sge_supp_len can be handled by the driver */
11261 if (sli4_params->sge_supp_len > LPFC_MAX_SGE_SIZE)
11262 sli4_params->sge_supp_len = LPFC_MAX_SGE_SIZE;
11263
James Smart28baac72010-02-12 14:42:03 -050011264 return rc;
11265}
11266
James Smartda0436e2009-05-22 14:51:39 -040011267/**
James Smartfedd3b72011-02-16 12:39:24 -050011268 * lpfc_get_sli4_parameters - Get the SLI4 Config PARAMETERS.
11269 * @phba: Pointer to HBA context object.
11270 * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
11271 *
11272 * This function is called in the SLI4 code path to read the port's
11273 * sli4 capabilities.
11274 *
11275 * This function may be be called from any context that can block-wait
11276 * for the completion. The expectation is that this routine is called
11277 * typically from probe_one or from the online routine.
11278 **/
11279int
11280lpfc_get_sli4_parameters(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
11281{
11282 int rc;
11283 struct lpfc_mqe *mqe = &mboxq->u.mqe;
11284 struct lpfc_pc_sli4_params *sli4_params;
James Smarta183a152011-10-10 21:32:43 -040011285 uint32_t mbox_tmo;
James Smartfedd3b72011-02-16 12:39:24 -050011286 int length;
James Smartbf316c72018-04-09 14:24:28 -070011287 bool exp_wqcq_pages = true;
James Smartfedd3b72011-02-16 12:39:24 -050011288 struct lpfc_sli4_parameters *mbx_sli4_parameters;
11289
James Smart6d368e52011-05-24 11:44:12 -040011290 /*
11291 * By default, the driver assumes the SLI4 port requires RPI
11292 * header postings. The SLI4_PARAM response will correct this
11293 * assumption.
11294 */
11295 phba->sli4_hba.rpi_hdrs_in_use = 1;
11296
James Smartfedd3b72011-02-16 12:39:24 -050011297 /* Read the port's SLI4 Config Parameters */
11298 length = (sizeof(struct lpfc_mbx_get_sli4_parameters) -
11299 sizeof(struct lpfc_sli4_cfg_mhdr));
11300 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
11301 LPFC_MBOX_OPCODE_GET_SLI4_PARAMETERS,
11302 length, LPFC_SLI4_MBX_EMBED);
11303 if (!phba->sli4_hba.intr_enable)
11304 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smarta183a152011-10-10 21:32:43 -040011305 else {
11306 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
11307 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
11308 }
James Smartfedd3b72011-02-16 12:39:24 -050011309 if (unlikely(rc))
11310 return rc;
11311 sli4_params = &phba->sli4_hba.pc_sli4_params;
11312 mbx_sli4_parameters = &mqe->un.get_sli4_parameters.sli4_parameters;
11313 sli4_params->if_type = bf_get(cfg_if_type, mbx_sli4_parameters);
11314 sli4_params->sli_rev = bf_get(cfg_sli_rev, mbx_sli4_parameters);
11315 sli4_params->sli_family = bf_get(cfg_sli_family, mbx_sli4_parameters);
11316 sli4_params->featurelevel_1 = bf_get(cfg_sli_hint_1,
11317 mbx_sli4_parameters);
11318 sli4_params->featurelevel_2 = bf_get(cfg_sli_hint_2,
11319 mbx_sli4_parameters);
11320 if (bf_get(cfg_phwq, mbx_sli4_parameters))
11321 phba->sli3_options |= LPFC_SLI4_PHWQ_ENABLED;
11322 else
11323 phba->sli3_options &= ~LPFC_SLI4_PHWQ_ENABLED;
11324 sli4_params->sge_supp_len = mbx_sli4_parameters->sge_supp_len;
11325 sli4_params->loopbk_scope = bf_get(loopbk_scope, mbx_sli4_parameters);
James Smart1ba981f2014-02-20 09:56:45 -050011326 sli4_params->oas_supported = bf_get(cfg_oas, mbx_sli4_parameters);
James Smartfedd3b72011-02-16 12:39:24 -050011327 sli4_params->cqv = bf_get(cfg_cqv, mbx_sli4_parameters);
11328 sli4_params->mqv = bf_get(cfg_mqv, mbx_sli4_parameters);
11329 sli4_params->wqv = bf_get(cfg_wqv, mbx_sli4_parameters);
11330 sli4_params->rqv = bf_get(cfg_rqv, mbx_sli4_parameters);
James Smart7365f6f2018-02-22 08:18:46 -080011331 sli4_params->eqav = bf_get(cfg_eqav, mbx_sli4_parameters);
11332 sli4_params->cqav = bf_get(cfg_cqav, mbx_sli4_parameters);
James Smart0c651872013-07-15 18:33:23 -040011333 sli4_params->wqsize = bf_get(cfg_wqsize, mbx_sli4_parameters);
James Smart66e9e6b2018-06-26 08:24:27 -070011334 sli4_params->bv1s = bf_get(cfg_bv1s, mbx_sli4_parameters);
James Smartfedd3b72011-02-16 12:39:24 -050011335 sli4_params->sgl_pages_max = bf_get(cfg_sgl_page_cnt,
11336 mbx_sli4_parameters);
James Smart895427b2017-02-12 13:52:30 -080011337 sli4_params->wqpcnt = bf_get(cfg_wqpcnt, mbx_sli4_parameters);
James Smartfedd3b72011-02-16 12:39:24 -050011338 sli4_params->sgl_pp_align = bf_get(cfg_sgl_pp_align,
11339 mbx_sli4_parameters);
James Smart6d368e52011-05-24 11:44:12 -040011340 phba->sli4_hba.extents_in_use = bf_get(cfg_ext, mbx_sli4_parameters);
11341 phba->sli4_hba.rpi_hdrs_in_use = bf_get(cfg_hdrr, mbx_sli4_parameters);
James Smart895427b2017-02-12 13:52:30 -080011342
James Smartc15e0702019-05-21 17:49:02 -070011343 /* Check for firmware nvme support */
11344 rc = (bf_get(cfg_nvme, mbx_sli4_parameters) &&
11345 bf_get(cfg_xib, mbx_sli4_parameters));
James Smart895427b2017-02-12 13:52:30 -080011346
James Smartc15e0702019-05-21 17:49:02 -070011347 if (rc) {
11348 /* Save this to indicate the Firmware supports NVME */
11349 sli4_params->nvme = 1;
11350
11351 /* Firmware NVME support, check driver FC4 NVME support */
11352 if (phba->cfg_enable_fc4_type == LPFC_ENABLE_FCP) {
11353 lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_NVME,
11354 "6133 Disabling NVME support: "
11355 "FC4 type not supported: x%x\n",
11356 phba->cfg_enable_fc4_type);
11357 goto fcponly;
11358 }
11359 } else {
11360 /* No firmware NVME support, check driver FC4 NVME support */
11361 sli4_params->nvme = 0;
11362 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
11363 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_NVME,
11364 "6101 Disabling NVME support: Not "
11365 "supported by firmware (%d %d) x%x\n",
11366 bf_get(cfg_nvme, mbx_sli4_parameters),
11367 bf_get(cfg_xib, mbx_sli4_parameters),
11368 phba->cfg_enable_fc4_type);
11369fcponly:
11370 phba->nvme_support = 0;
11371 phba->nvmet_support = 0;
11372 phba->cfg_nvmet_mrq = 0;
11373
11374 /* If no FC4 type support, move to just SCSI support */
11375 if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP))
11376 return -ENODEV;
11377 phba->cfg_enable_fc4_type = LPFC_ENABLE_FCP;
11378 }
James Smart895427b2017-02-12 13:52:30 -080011379 }
James Smart05580562011-05-24 11:40:48 -040011380
James Smart414abe02018-06-26 08:24:26 -070011381 /* Only embed PBDE for if_type 6, PBDE support requires xib be set */
11382 if ((bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
11383 LPFC_SLI_INTF_IF_TYPE_6) || (!bf_get(cfg_xib, mbx_sli4_parameters)))
11384 phba->cfg_enable_pbde = 0;
James Smart0bc2b7c2018-02-22 08:18:48 -080011385
James Smart20aefac2018-01-30 15:58:58 -080011386 /*
11387 * To support Suppress Response feature we must satisfy 3 conditions.
11388 * lpfc_suppress_rsp module parameter must be set (default).
11389 * In SLI4-Parameters Descriptor:
11390 * Extended Inline Buffers (XIB) must be supported.
11391 * Suppress Response IU Not Supported (SRIUNS) must NOT be supported
11392 * (double negative).
11393 */
11394 if (phba->cfg_suppress_rsp && bf_get(cfg_xib, mbx_sli4_parameters) &&
11395 !(bf_get(cfg_nosr, mbx_sli4_parameters)))
James Smartf358dd02017-02-12 13:52:34 -080011396 phba->sli.sli_flag |= LPFC_SLI_SUPPRESS_RSP;
James Smart20aefac2018-01-30 15:58:58 -080011397 else
11398 phba->cfg_suppress_rsp = 0;
James Smartf358dd02017-02-12 13:52:34 -080011399
James Smart0cf07f842017-06-01 21:07:10 -070011400 if (bf_get(cfg_eqdr, mbx_sli4_parameters))
11401 phba->sli.sli_flag |= LPFC_SLI_USE_EQDR;
11402
James Smart05580562011-05-24 11:40:48 -040011403 /* Make sure that sge_supp_len can be handled by the driver */
11404 if (sli4_params->sge_supp_len > LPFC_MAX_SGE_SIZE)
11405 sli4_params->sge_supp_len = LPFC_MAX_SGE_SIZE;
11406
James Smartb5c53952016-03-31 14:12:30 -070011407 /*
James Smartc176ffa2018-01-30 15:58:46 -080011408 * Check whether the adapter supports an embedded copy of the
11409 * FCP CMD IU within the WQE for FCP_Ixxx commands. In order
11410 * to use this option, 128-byte WQEs must be used.
James Smartb5c53952016-03-31 14:12:30 -070011411 */
11412 if (bf_get(cfg_ext_embed_cb, mbx_sli4_parameters))
11413 phba->fcp_embed_io = 1;
11414 else
11415 phba->fcp_embed_io = 0;
James Smart7bdedb32016-07-06 12:36:00 -070011416
James Smart0bc2b7c2018-02-22 08:18:48 -080011417 lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_NVME,
James Smart414abe02018-06-26 08:24:26 -070011418 "6422 XIB %d PBDE %d: FCP %d NVME %d %d %d\n",
James Smart0bc2b7c2018-02-22 08:18:48 -080011419 bf_get(cfg_xib, mbx_sli4_parameters),
James Smart414abe02018-06-26 08:24:26 -070011420 phba->cfg_enable_pbde,
11421 phba->fcp_embed_io, phba->nvme_support,
James Smart4e565cf2018-02-22 08:18:50 -080011422 phba->cfg_nvme_embed_cmd, phba->cfg_suppress_rsp);
James Smart0bc2b7c2018-02-22 08:18:48 -080011423
James Smartbf316c72018-04-09 14:24:28 -070011424 if ((bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
11425 LPFC_SLI_INTF_IF_TYPE_2) &&
11426 (bf_get(lpfc_sli_intf_sli_family, &phba->sli4_hba.sli_intf) ==
James Smartc2217682018-05-24 21:09:00 -070011427 LPFC_SLI_INTF_FAMILY_LNCR_A0))
James Smartbf316c72018-04-09 14:24:28 -070011428 exp_wqcq_pages = false;
11429
James Smartc176ffa2018-01-30 15:58:46 -080011430 if ((bf_get(cfg_cqpsize, mbx_sli4_parameters) & LPFC_CQ_16K_PAGE_SZ) &&
11431 (bf_get(cfg_wqpsize, mbx_sli4_parameters) & LPFC_WQ_16K_PAGE_SZ) &&
James Smartbf316c72018-04-09 14:24:28 -070011432 exp_wqcq_pages &&
James Smartc176ffa2018-01-30 15:58:46 -080011433 (sli4_params->wqsize & LPFC_WQ_SZ128_SUPPORT))
11434 phba->enab_exp_wqcq_pages = 1;
11435 else
11436 phba->enab_exp_wqcq_pages = 0;
James Smart7bdedb32016-07-06 12:36:00 -070011437 /*
11438 * Check if the SLI port supports MDS Diagnostics
11439 */
11440 if (bf_get(cfg_mds_diags, mbx_sli4_parameters))
11441 phba->mds_diags_support = 1;
11442 else
11443 phba->mds_diags_support = 0;
James Smartd2cc9bc2018-09-10 10:30:50 -070011444
James Smartfedd3b72011-02-16 12:39:24 -050011445 return 0;
11446}
11447
11448/**
James Smart3772a992009-05-22 14:50:54 -040011449 * lpfc_pci_probe_one_s3 - PCI probe func to reg SLI-3 device to PCI subsystem.
11450 * @pdev: pointer to PCI device
11451 * @pid: pointer to PCI device identifier
11452 *
11453 * This routine is to be called to attach a device with SLI-3 interface spec
11454 * to the PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
11455 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
11456 * information of the device and driver to see if the driver state that it can
11457 * support this kind of device. If the match is successful, the driver core
11458 * invokes this routine. If this routine determines it can claim the HBA, it
11459 * does all the initialization that it needs to do to handle the HBA properly.
11460 *
11461 * Return code
11462 * 0 - driver can claim the device
11463 * negative value - driver can not claim the device
11464 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080011465static int
James Smart3772a992009-05-22 14:50:54 -040011466lpfc_pci_probe_one_s3(struct pci_dev *pdev, const struct pci_device_id *pid)
11467{
11468 struct lpfc_hba *phba;
11469 struct lpfc_vport *vport = NULL;
James Smart6669f9b2009-10-02 15:16:45 -040011470 struct Scsi_Host *shost = NULL;
James Smart3772a992009-05-22 14:50:54 -040011471 int error;
11472 uint32_t cfg_mode, intr_mode;
11473
11474 /* Allocate memory for HBA structure */
11475 phba = lpfc_hba_alloc(pdev);
11476 if (!phba)
11477 return -ENOMEM;
11478
11479 /* Perform generic PCI device enabling operation */
11480 error = lpfc_enable_pci_dev(phba);
James Smart079b5c92011-08-21 21:48:49 -040011481 if (error)
James Smart3772a992009-05-22 14:50:54 -040011482 goto out_free_phba;
James Smart3772a992009-05-22 14:50:54 -040011483
11484 /* Set up SLI API function jump table for PCI-device group-0 HBAs */
11485 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_LP);
11486 if (error)
11487 goto out_disable_pci_dev;
11488
11489 /* Set up SLI-3 specific device PCI memory space */
11490 error = lpfc_sli_pci_mem_setup(phba);
11491 if (error) {
11492 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11493 "1402 Failed to set up pci memory space.\n");
11494 goto out_disable_pci_dev;
11495 }
11496
James Smart3772a992009-05-22 14:50:54 -040011497 /* Set up SLI-3 specific device driver resources */
11498 error = lpfc_sli_driver_resource_setup(phba);
11499 if (error) {
11500 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11501 "1404 Failed to set up driver resource.\n");
11502 goto out_unset_pci_mem_s3;
11503 }
11504
11505 /* Initialize and populate the iocb list per host */
James Smartd1f525a2017-04-21 16:04:55 -070011506
James Smart3772a992009-05-22 14:50:54 -040011507 error = lpfc_init_iocb_list(phba, LPFC_IOCB_LIST_CNT);
11508 if (error) {
11509 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11510 "1405 Failed to initialize iocb list.\n");
11511 goto out_unset_driver_resource_s3;
11512 }
11513
11514 /* Set up common device driver resources */
11515 error = lpfc_setup_driver_resource_phase2(phba);
11516 if (error) {
11517 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11518 "1406 Failed to set up driver resource.\n");
11519 goto out_free_iocb_list;
11520 }
11521
James Smart079b5c92011-08-21 21:48:49 -040011522 /* Get the default values for Model Name and Description */
11523 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
11524
James Smart3772a992009-05-22 14:50:54 -040011525 /* Create SCSI host to the physical port */
11526 error = lpfc_create_shost(phba);
11527 if (error) {
11528 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11529 "1407 Failed to create scsi host.\n");
11530 goto out_unset_driver_resource;
11531 }
11532
11533 /* Configure sysfs attributes */
11534 vport = phba->pport;
11535 error = lpfc_alloc_sysfs_attr(vport);
11536 if (error) {
11537 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11538 "1476 Failed to allocate sysfs attr\n");
11539 goto out_destroy_shost;
11540 }
11541
James Smart6669f9b2009-10-02 15:16:45 -040011542 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
James Smart3772a992009-05-22 14:50:54 -040011543 /* Now, trying to enable interrupt and bring up the device */
11544 cfg_mode = phba->cfg_use_msi;
11545 while (true) {
11546 /* Put device to a known state before enabling interrupt */
11547 lpfc_stop_port(phba);
11548 /* Configure and enable interrupt */
11549 intr_mode = lpfc_sli_enable_intr(phba, cfg_mode);
11550 if (intr_mode == LPFC_INTR_ERROR) {
11551 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11552 "0431 Failed to enable interrupt.\n");
11553 error = -ENODEV;
11554 goto out_free_sysfs_attr;
11555 }
11556 /* SLI-3 HBA setup */
11557 if (lpfc_sli_hba_setup(phba)) {
11558 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11559 "1477 Failed to set up hba\n");
11560 error = -ENODEV;
11561 goto out_remove_device;
11562 }
11563
11564 /* Wait 50ms for the interrupts of previous mailbox commands */
11565 msleep(50);
11566 /* Check active interrupts on message signaled interrupts */
11567 if (intr_mode == 0 ||
11568 phba->sli.slistat.sli_intr > LPFC_MSIX_VECTORS) {
11569 /* Log the current active interrupt mode */
11570 phba->intr_mode = intr_mode;
11571 lpfc_log_intr_mode(phba, intr_mode);
11572 break;
11573 } else {
11574 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
11575 "0447 Configure interrupt mode (%d) "
11576 "failed active interrupt test.\n",
11577 intr_mode);
11578 /* Disable the current interrupt mode */
11579 lpfc_sli_disable_intr(phba);
11580 /* Try next level of interrupt mode */
11581 cfg_mode = --intr_mode;
11582 }
11583 }
11584
11585 /* Perform post initialization setup */
11586 lpfc_post_init_setup(phba);
11587
11588 /* Check if there are static vports to be created. */
11589 lpfc_create_static_vport(phba);
11590
11591 return 0;
11592
11593out_remove_device:
11594 lpfc_unset_hba(phba);
11595out_free_sysfs_attr:
11596 lpfc_free_sysfs_attr(vport);
11597out_destroy_shost:
11598 lpfc_destroy_shost(phba);
11599out_unset_driver_resource:
11600 lpfc_unset_driver_resource_phase2(phba);
11601out_free_iocb_list:
11602 lpfc_free_iocb_list(phba);
11603out_unset_driver_resource_s3:
11604 lpfc_sli_driver_resource_unset(phba);
11605out_unset_pci_mem_s3:
11606 lpfc_sli_pci_mem_unset(phba);
11607out_disable_pci_dev:
11608 lpfc_disable_pci_dev(phba);
James Smart6669f9b2009-10-02 15:16:45 -040011609 if (shost)
11610 scsi_host_put(shost);
James Smart3772a992009-05-22 14:50:54 -040011611out_free_phba:
11612 lpfc_hba_free(phba);
11613 return error;
11614}
11615
11616/**
11617 * lpfc_pci_remove_one_s3 - PCI func to unreg SLI-3 device from PCI subsystem.
James Smarte59058c2008-08-24 21:49:00 -040011618 * @pdev: pointer to PCI device
11619 *
James Smart3772a992009-05-22 14:50:54 -040011620 * This routine is to be called to disattach a device with SLI-3 interface
11621 * spec from PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
11622 * removed from PCI bus, it performs all the necessary cleanup for the HBA
11623 * device to be removed from the PCI subsystem properly.
James Smarte59058c2008-08-24 21:49:00 -040011624 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080011625static void
James Smart3772a992009-05-22 14:50:54 -040011626lpfc_pci_remove_one_s3(struct pci_dev *pdev)
dea31012005-04-17 16:05:31 -050011627{
James Smart2e0fef82007-06-17 19:56:36 -050011628 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11629 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
James Smarteada2722008-12-04 22:39:13 -050011630 struct lpfc_vport **vports;
James Smart2e0fef82007-06-17 19:56:36 -050011631 struct lpfc_hba *phba = vport->phba;
James Smarteada2722008-12-04 22:39:13 -050011632 int i;
Tomohiro Kusumi8a4df1202008-01-11 01:53:00 -050011633
James Smart549e55c2007-08-02 11:09:51 -040011634 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -040011635 vport->load_flag |= FC_UNLOADING;
James Smart549e55c2007-08-02 11:09:51 -040011636 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050011637
James Smart858c9f62007-06-17 19:56:39 -050011638 lpfc_free_sysfs_attr(vport);
11639
James Smarteada2722008-12-04 22:39:13 -050011640 /* Release all the vports against this physical port */
11641 vports = lpfc_create_vport_work_array(phba);
11642 if (vports != NULL)
James Smart587a37f2012-05-09 21:16:03 -040011643 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
11644 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
11645 continue;
James Smarteada2722008-12-04 22:39:13 -050011646 fc_vport_terminate(vports[i]->fc_vport);
James Smart587a37f2012-05-09 21:16:03 -040011647 }
James Smarteada2722008-12-04 22:39:13 -050011648 lpfc_destroy_vport_work_array(phba, vports);
11649
11650 /* Remove FC host and then SCSI host with the physical port */
James Smart858c9f62007-06-17 19:56:39 -050011651 fc_remove_host(shost);
11652 scsi_remove_host(shost);
James Smartd613b6a2017-02-12 13:52:37 -080011653
James Smart87af33f2007-10-27 13:37:43 -040011654 lpfc_cleanup(vport);
11655
James Smart2e0fef82007-06-17 19:56:36 -050011656 /*
11657 * Bring down the SLI Layer. This step disable all interrupts,
11658 * clears the rings, discards all mailbox commands, and resets
11659 * the HBA.
11660 */
James Smarta257bf92009-04-06 18:48:10 -040011661
Justin P. Mattock48e34d02010-12-30 15:07:58 -080011662 /* HBA interrupt will be disabled after this call */
James Smart2e0fef82007-06-17 19:56:36 -050011663 lpfc_sli_hba_down(phba);
James Smarta257bf92009-04-06 18:48:10 -040011664 /* Stop kthread signal shall trigger work_done one more time */
11665 kthread_stop(phba->worker_thread);
11666 /* Final cleanup of txcmplq and reset the HBA */
James Smart2e0fef82007-06-17 19:56:36 -050011667 lpfc_sli_brdrestart(phba);
11668
James Smart72859902012-01-18 16:25:38 -050011669 kfree(phba->vpi_bmask);
11670 kfree(phba->vpi_ids);
11671
James Smart3772a992009-05-22 14:50:54 -040011672 lpfc_stop_hba_timers(phba);
James Smart523128e2018-09-10 10:30:46 -070011673 spin_lock_irq(&phba->port_list_lock);
James Smart858c9f62007-06-17 19:56:39 -050011674 list_del_init(&vport->listentry);
James Smart523128e2018-09-10 10:30:46 -070011675 spin_unlock_irq(&phba->port_list_lock);
James Smart858c9f62007-06-17 19:56:39 -050011676
James Smart858c9f62007-06-17 19:56:39 -050011677 lpfc_debugfs_terminate(vport);
James Smart2e0fef82007-06-17 19:56:36 -050011678
James Smart912e3ac2011-05-24 11:42:11 -040011679 /* Disable SR-IOV if enabled */
11680 if (phba->cfg_sriov_nr_virtfn)
11681 pci_disable_sriov(pdev);
11682
James Smart5b75da22008-12-04 22:39:35 -050011683 /* Disable interrupt */
James Smart3772a992009-05-22 14:50:54 -040011684 lpfc_sli_disable_intr(phba);
dea31012005-04-17 16:05:31 -050011685
James Smart858c9f62007-06-17 19:56:39 -050011686 scsi_host_put(shost);
James Smart2e0fef82007-06-17 19:56:36 -050011687
11688 /*
11689 * Call scsi_free before mem_free since scsi bufs are released to their
11690 * corresponding pools here.
11691 */
11692 lpfc_scsi_free(phba);
James Smart0794d602019-01-28 11:14:19 -080011693 lpfc_free_iocb_list(phba);
11694
James Smart3772a992009-05-22 14:50:54 -040011695 lpfc_mem_free_all(phba);
James Smart2e0fef82007-06-17 19:56:36 -050011696
James Smart34b02dc2008-08-24 21:49:55 -040011697 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
11698 phba->hbqslimp.virt, phba->hbqslimp.phys);
James Smarted957682007-06-17 19:56:37 -050011699
James Smart2e0fef82007-06-17 19:56:36 -050011700 /* Free resources associated with SLI2 interface */
11701 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
James Smart34b02dc2008-08-24 21:49:55 -040011702 phba->slim2p.virt, phba->slim2p.phys);
James Smart2e0fef82007-06-17 19:56:36 -050011703
11704 /* unmap adapter SLIM and Control Registers */
11705 iounmap(phba->ctrl_regs_memmap_p);
11706 iounmap(phba->slim_memmap_p);
11707
James Smart3772a992009-05-22 14:50:54 -040011708 lpfc_hba_free(phba);
James Smart2e0fef82007-06-17 19:56:36 -050011709
Johannes Thumshirne0c04832016-06-07 09:44:03 +020011710 pci_release_mem_regions(pdev);
James Smart2e0fef82007-06-17 19:56:36 -050011711 pci_disable_device(pdev);
dea31012005-04-17 16:05:31 -050011712}
11713
Linas Vepstas8d63f372007-02-14 14:28:36 -060011714/**
James Smart3772a992009-05-22 14:50:54 -040011715 * lpfc_pci_suspend_one_s3 - PCI func to suspend SLI-3 device for power mgmnt
James Smart3a55b532008-12-04 22:38:54 -050011716 * @pdev: pointer to PCI device
11717 * @msg: power management message
11718 *
James Smart3772a992009-05-22 14:50:54 -040011719 * This routine is to be called from the kernel's PCI subsystem to support
11720 * system Power Management (PM) to device with SLI-3 interface spec. When
11721 * PM invokes this method, it quiesces the device by stopping the driver's
11722 * worker thread for the device, turning off device's interrupt and DMA,
11723 * and bring the device offline. Note that as the driver implements the
11724 * minimum PM requirements to a power-aware driver's PM support for the
11725 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
11726 * to the suspend() method call will be treated as SUSPEND and the driver will
11727 * fully reinitialize its device during resume() method call, the driver will
11728 * set device to PCI_D3hot state in PCI config space instead of setting it
11729 * according to the @msg provided by the PM.
James Smart3a55b532008-12-04 22:38:54 -050011730 *
11731 * Return code
James Smart3772a992009-05-22 14:50:54 -040011732 * 0 - driver suspended the device
11733 * Error otherwise
James Smart3a55b532008-12-04 22:38:54 -050011734 **/
11735static int
James Smart3772a992009-05-22 14:50:54 -040011736lpfc_pci_suspend_one_s3(struct pci_dev *pdev, pm_message_t msg)
James Smart3a55b532008-12-04 22:38:54 -050011737{
11738 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11739 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11740
11741 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
11742 "0473 PCI device Power Management suspend.\n");
11743
11744 /* Bring down the device */
James Smart618a5232012-06-12 13:54:36 -040011745 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart3a55b532008-12-04 22:38:54 -050011746 lpfc_offline(phba);
11747 kthread_stop(phba->worker_thread);
11748
11749 /* Disable interrupt from device */
James Smart3772a992009-05-22 14:50:54 -040011750 lpfc_sli_disable_intr(phba);
James Smart3a55b532008-12-04 22:38:54 -050011751
11752 /* Save device state to PCI config space */
11753 pci_save_state(pdev);
11754 pci_set_power_state(pdev, PCI_D3hot);
11755
11756 return 0;
11757}
11758
11759/**
James Smart3772a992009-05-22 14:50:54 -040011760 * lpfc_pci_resume_one_s3 - PCI func to resume SLI-3 device for power mgmnt
James Smart3a55b532008-12-04 22:38:54 -050011761 * @pdev: pointer to PCI device
11762 *
James Smart3772a992009-05-22 14:50:54 -040011763 * This routine is to be called from the kernel's PCI subsystem to support
11764 * system Power Management (PM) to device with SLI-3 interface spec. When PM
11765 * invokes this method, it restores the device's PCI config space state and
11766 * fully reinitializes the device and brings it online. Note that as the
11767 * driver implements the minimum PM requirements to a power-aware driver's
11768 * PM for suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE,
11769 * FREEZE) to the suspend() method call will be treated as SUSPEND and the
11770 * driver will fully reinitialize its device during resume() method call,
11771 * the device will be set to PCI_D0 directly in PCI config space before
11772 * restoring the state.
James Smart3a55b532008-12-04 22:38:54 -050011773 *
11774 * Return code
James Smart3772a992009-05-22 14:50:54 -040011775 * 0 - driver suspended the device
11776 * Error otherwise
James Smart3a55b532008-12-04 22:38:54 -050011777 **/
11778static int
James Smart3772a992009-05-22 14:50:54 -040011779lpfc_pci_resume_one_s3(struct pci_dev *pdev)
James Smart3a55b532008-12-04 22:38:54 -050011780{
11781 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11782 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
James Smart5b75da22008-12-04 22:39:35 -050011783 uint32_t intr_mode;
James Smart3a55b532008-12-04 22:38:54 -050011784 int error;
11785
11786 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
11787 "0452 PCI device Power Management resume.\n");
11788
11789 /* Restore device state from PCI config space */
11790 pci_set_power_state(pdev, PCI_D0);
11791 pci_restore_state(pdev);
James Smart0d878412009-10-02 15:16:56 -040011792
James Smart1dfb5a42010-02-12 14:40:50 -050011793 /*
11794 * As the new kernel behavior of pci_restore_state() API call clears
11795 * device saved_state flag, need to save the restored state again.
11796 */
11797 pci_save_state(pdev);
11798
James Smart3a55b532008-12-04 22:38:54 -050011799 if (pdev->is_busmaster)
11800 pci_set_master(pdev);
11801
11802 /* Startup the kernel thread for this host adapter. */
11803 phba->worker_thread = kthread_run(lpfc_do_work, phba,
11804 "lpfc_worker_%d", phba->brd_no);
11805 if (IS_ERR(phba->worker_thread)) {
11806 error = PTR_ERR(phba->worker_thread);
11807 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11808 "0434 PM resume failed to start worker "
11809 "thread: error=x%x.\n", error);
11810 return error;
11811 }
11812
James Smart5b75da22008-12-04 22:39:35 -050011813 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -040011814 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -050011815 if (intr_mode == LPFC_INTR_ERROR) {
James Smart3a55b532008-12-04 22:38:54 -050011816 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart5b75da22008-12-04 22:39:35 -050011817 "0430 PM resume Failed to enable interrupt\n");
11818 return -EIO;
11819 } else
11820 phba->intr_mode = intr_mode;
James Smart3a55b532008-12-04 22:38:54 -050011821
11822 /* Restart HBA and bring it online */
11823 lpfc_sli_brdrestart(phba);
11824 lpfc_online(phba);
11825
James Smart5b75da22008-12-04 22:39:35 -050011826 /* Log the current active interrupt mode */
11827 lpfc_log_intr_mode(phba, phba->intr_mode);
11828
James Smart3a55b532008-12-04 22:38:54 -050011829 return 0;
11830}
11831
11832/**
James Smart891478a2009-11-18 15:40:23 -050011833 * lpfc_sli_prep_dev_for_recover - Prepare SLI3 device for pci slot recover
11834 * @phba: pointer to lpfc hba data structure.
11835 *
11836 * This routine is called to prepare the SLI3 device for PCI slot recover. It
James Smarte2af0d22010-03-15 11:25:32 -040011837 * aborts all the outstanding SCSI I/Os to the pci device.
James Smart891478a2009-11-18 15:40:23 -050011838 **/
11839static void
11840lpfc_sli_prep_dev_for_recover(struct lpfc_hba *phba)
11841{
11842 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11843 "2723 PCI channel I/O abort preparing for recovery\n");
James Smarte2af0d22010-03-15 11:25:32 -040011844
11845 /*
11846 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
11847 * and let the SCSI mid-layer to retry them to recover.
11848 */
James Smartdb55fba2014-04-04 13:52:02 -040011849 lpfc_sli_abort_fcp_rings(phba);
James Smart891478a2009-11-18 15:40:23 -050011850}
11851
11852/**
James Smart0d878412009-10-02 15:16:56 -040011853 * lpfc_sli_prep_dev_for_reset - Prepare SLI3 device for pci slot reset
11854 * @phba: pointer to lpfc hba data structure.
11855 *
11856 * This routine is called to prepare the SLI3 device for PCI slot reset. It
11857 * disables the device interrupt and pci device, and aborts the internal FCP
11858 * pending I/Os.
11859 **/
11860static void
11861lpfc_sli_prep_dev_for_reset(struct lpfc_hba *phba)
11862{
James Smart0d878412009-10-02 15:16:56 -040011863 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart891478a2009-11-18 15:40:23 -050011864 "2710 PCI channel disable preparing for reset\n");
James Smarte2af0d22010-03-15 11:25:32 -040011865
James Smart75baf692010-06-08 18:31:21 -040011866 /* Block any management I/Os to the device */
James Smart618a5232012-06-12 13:54:36 -040011867 lpfc_block_mgmt_io(phba, LPFC_MBX_WAIT);
James Smart75baf692010-06-08 18:31:21 -040011868
James Smarte2af0d22010-03-15 11:25:32 -040011869 /* Block all SCSI devices' I/Os on the host */
11870 lpfc_scsi_dev_block(phba);
11871
James Smartea714f32013-04-17 20:18:47 -040011872 /* Flush all driver's outstanding SCSI I/Os as we are to reset */
11873 lpfc_sli_flush_fcp_rings(phba);
11874
James Smarte2af0d22010-03-15 11:25:32 -040011875 /* stop all timers */
11876 lpfc_stop_hba_timers(phba);
11877
James Smart0d878412009-10-02 15:16:56 -040011878 /* Disable interrupt and pci device */
11879 lpfc_sli_disable_intr(phba);
11880 pci_disable_device(phba->pcidev);
James Smart0d878412009-10-02 15:16:56 -040011881}
11882
11883/**
11884 * lpfc_sli_prep_dev_for_perm_failure - Prepare SLI3 dev for pci slot disable
11885 * @phba: pointer to lpfc hba data structure.
11886 *
11887 * This routine is called to prepare the SLI3 device for PCI slot permanently
11888 * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
11889 * pending I/Os.
11890 **/
11891static void
James Smart75baf692010-06-08 18:31:21 -040011892lpfc_sli_prep_dev_for_perm_failure(struct lpfc_hba *phba)
James Smart0d878412009-10-02 15:16:56 -040011893{
11894 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart891478a2009-11-18 15:40:23 -050011895 "2711 PCI channel permanent disable for failure\n");
James Smarte2af0d22010-03-15 11:25:32 -040011896 /* Block all SCSI devices' I/Os on the host */
11897 lpfc_scsi_dev_block(phba);
11898
11899 /* stop all timers */
11900 lpfc_stop_hba_timers(phba);
11901
James Smart0d878412009-10-02 15:16:56 -040011902 /* Clean up all driver's outstanding SCSI I/Os */
11903 lpfc_sli_flush_fcp_rings(phba);
11904}
11905
11906/**
James Smart3772a992009-05-22 14:50:54 -040011907 * lpfc_io_error_detected_s3 - Method for handling SLI-3 device PCI I/O error
James Smarte59058c2008-08-24 21:49:00 -040011908 * @pdev: pointer to PCI device.
11909 * @state: the current PCI connection state.
Linas Vepstas8d63f372007-02-14 14:28:36 -060011910 *
James Smart3772a992009-05-22 14:50:54 -040011911 * This routine is called from the PCI subsystem for I/O error handling to
11912 * device with SLI-3 interface spec. This function is called by the PCI
11913 * subsystem after a PCI bus error affecting this device has been detected.
11914 * When this function is invoked, it will need to stop all the I/Os and
11915 * interrupt(s) to the device. Once that is done, it will return
11916 * PCI_ERS_RESULT_NEED_RESET for the PCI subsystem to perform proper recovery
11917 * as desired.
James Smarte59058c2008-08-24 21:49:00 -040011918 *
11919 * Return codes
James Smart0d878412009-10-02 15:16:56 -040011920 * PCI_ERS_RESULT_CAN_RECOVER - can be recovered with reset_link
James Smart3772a992009-05-22 14:50:54 -040011921 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
11922 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
James Smarte59058c2008-08-24 21:49:00 -040011923 **/
James Smart3772a992009-05-22 14:50:54 -040011924static pci_ers_result_t
11925lpfc_io_error_detected_s3(struct pci_dev *pdev, pci_channel_state_t state)
Linas Vepstas8d63f372007-02-14 14:28:36 -060011926{
James Smart51ef4c22007-08-02 11:10:31 -040011927 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11928 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -060011929
James Smart0d878412009-10-02 15:16:56 -040011930 switch (state) {
11931 case pci_channel_io_normal:
James Smart891478a2009-11-18 15:40:23 -050011932 /* Non-fatal error, prepare for recovery */
11933 lpfc_sli_prep_dev_for_recover(phba);
James Smart0d878412009-10-02 15:16:56 -040011934 return PCI_ERS_RESULT_CAN_RECOVER;
11935 case pci_channel_io_frozen:
11936 /* Fatal error, prepare for slot reset */
11937 lpfc_sli_prep_dev_for_reset(phba);
11938 return PCI_ERS_RESULT_NEED_RESET;
11939 case pci_channel_io_perm_failure:
11940 /* Permanent failure, prepare for device down */
James Smart75baf692010-06-08 18:31:21 -040011941 lpfc_sli_prep_dev_for_perm_failure(phba);
Linas Vepstas8d63f372007-02-14 14:28:36 -060011942 return PCI_ERS_RESULT_DISCONNECT;
James Smart0d878412009-10-02 15:16:56 -040011943 default:
11944 /* Unknown state, prepare and request slot reset */
11945 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11946 "0472 Unknown PCI error state: x%x\n", state);
11947 lpfc_sli_prep_dev_for_reset(phba);
11948 return PCI_ERS_RESULT_NEED_RESET;
James Smarta8e497d2008-08-24 21:50:11 -040011949 }
Linas Vepstas8d63f372007-02-14 14:28:36 -060011950}
11951
11952/**
James Smart3772a992009-05-22 14:50:54 -040011953 * lpfc_io_slot_reset_s3 - Method for restarting PCI SLI-3 device from scratch.
James Smarte59058c2008-08-24 21:49:00 -040011954 * @pdev: pointer to PCI device.
Linas Vepstas8d63f372007-02-14 14:28:36 -060011955 *
James Smart3772a992009-05-22 14:50:54 -040011956 * This routine is called from the PCI subsystem for error handling to
11957 * device with SLI-3 interface spec. This is called after PCI bus has been
11958 * reset to restart the PCI card from scratch, as if from a cold-boot.
11959 * During the PCI subsystem error recovery, after driver returns
11960 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
11961 * recovery and then call this routine before calling the .resume method
11962 * to recover the device. This function will initialize the HBA device,
11963 * enable the interrupt, but it will just put the HBA to offline state
11964 * without passing any I/O traffic.
James Smarte59058c2008-08-24 21:49:00 -040011965 *
11966 * Return codes
James Smart3772a992009-05-22 14:50:54 -040011967 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
11968 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
Linas Vepstas8d63f372007-02-14 14:28:36 -060011969 */
James Smart3772a992009-05-22 14:50:54 -040011970static pci_ers_result_t
11971lpfc_io_slot_reset_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -060011972{
James Smart51ef4c22007-08-02 11:10:31 -040011973 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11974 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -060011975 struct lpfc_sli *psli = &phba->sli;
James Smart5b75da22008-12-04 22:39:35 -050011976 uint32_t intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -060011977
11978 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
Benjamin Herrenschmidt09483912007-12-20 15:28:09 +110011979 if (pci_enable_device_mem(pdev)) {
Linas Vepstas8d63f372007-02-14 14:28:36 -060011980 printk(KERN_ERR "lpfc: Cannot re-enable "
11981 "PCI device after reset.\n");
11982 return PCI_ERS_RESULT_DISCONNECT;
11983 }
11984
James Smart97207482008-12-04 22:39:19 -050011985 pci_restore_state(pdev);
James Smart1dfb5a42010-02-12 14:40:50 -050011986
11987 /*
11988 * As the new kernel behavior of pci_restore_state() API call clears
11989 * device saved_state flag, need to save the restored state again.
11990 */
11991 pci_save_state(pdev);
11992
James Smart97207482008-12-04 22:39:19 -050011993 if (pdev->is_busmaster)
11994 pci_set_master(pdev);
Linas Vepstas8d63f372007-02-14 14:28:36 -060011995
James Smart92d7f7b2007-06-17 19:56:38 -050011996 spin_lock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040011997 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -050011998 spin_unlock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -060011999
James Smart5b75da22008-12-04 22:39:35 -050012000 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -040012001 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -050012002 if (intr_mode == LPFC_INTR_ERROR) {
12003 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12004 "0427 Cannot re-enable interrupt after "
12005 "slot reset.\n");
12006 return PCI_ERS_RESULT_DISCONNECT;
12007 } else
12008 phba->intr_mode = intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -060012009
James Smart75baf692010-06-08 18:31:21 -040012010 /* Take device offline, it will perform cleanup */
James Smart618a5232012-06-12 13:54:36 -040012011 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
Linas Vepstas8d63f372007-02-14 14:28:36 -060012012 lpfc_offline(phba);
12013 lpfc_sli_brdrestart(phba);
12014
James Smart5b75da22008-12-04 22:39:35 -050012015 /* Log the current active interrupt mode */
12016 lpfc_log_intr_mode(phba, phba->intr_mode);
12017
Linas Vepstas8d63f372007-02-14 14:28:36 -060012018 return PCI_ERS_RESULT_RECOVERED;
12019}
12020
12021/**
James Smart3772a992009-05-22 14:50:54 -040012022 * lpfc_io_resume_s3 - Method for resuming PCI I/O operation on SLI-3 device.
James Smarte59058c2008-08-24 21:49:00 -040012023 * @pdev: pointer to PCI device
Linas Vepstas8d63f372007-02-14 14:28:36 -060012024 *
James Smart3772a992009-05-22 14:50:54 -040012025 * This routine is called from the PCI subsystem for error handling to device
12026 * with SLI-3 interface spec. It is called when kernel error recovery tells
12027 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
12028 * error recovery. After this call, traffic can start to flow from this device
12029 * again.
Linas Vepstas8d63f372007-02-14 14:28:36 -060012030 */
James Smart3772a992009-05-22 14:50:54 -040012031static void
12032lpfc_io_resume_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -060012033{
James Smart51ef4c22007-08-02 11:10:31 -040012034 struct Scsi_Host *shost = pci_get_drvdata(pdev);
12035 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -060012036
James Smarte2af0d22010-03-15 11:25:32 -040012037 /* Bring device online, it will be no-op for non-fatal error resume */
James Smart58da1ff2008-04-07 10:15:56 -040012038 lpfc_online(phba);
Linas Vepstas8d63f372007-02-14 14:28:36 -060012039}
12040
James Smart3772a992009-05-22 14:50:54 -040012041/**
James Smartda0436e2009-05-22 14:51:39 -040012042 * lpfc_sli4_get_els_iocb_cnt - Calculate the # of ELS IOCBs to reserve
12043 * @phba: pointer to lpfc hba data structure.
12044 *
12045 * returns the number of ELS/CT IOCBs to reserve
12046 **/
12047int
12048lpfc_sli4_get_els_iocb_cnt(struct lpfc_hba *phba)
12049{
12050 int max_xri = phba->sli4_hba.max_cfg_param.max_xri;
12051
James Smartf1126682009-06-10 17:22:44 -040012052 if (phba->sli_rev == LPFC_SLI_REV4) {
12053 if (max_xri <= 100)
James Smart6a9c52c2009-10-02 15:16:51 -040012054 return 10;
James Smartf1126682009-06-10 17:22:44 -040012055 else if (max_xri <= 256)
James Smart6a9c52c2009-10-02 15:16:51 -040012056 return 25;
James Smartf1126682009-06-10 17:22:44 -040012057 else if (max_xri <= 512)
James Smart6a9c52c2009-10-02 15:16:51 -040012058 return 50;
James Smartf1126682009-06-10 17:22:44 -040012059 else if (max_xri <= 1024)
James Smart6a9c52c2009-10-02 15:16:51 -040012060 return 100;
James Smart8a9d2e82012-05-09 21:16:12 -040012061 else if (max_xri <= 1536)
James Smart6a9c52c2009-10-02 15:16:51 -040012062 return 150;
James Smart8a9d2e82012-05-09 21:16:12 -040012063 else if (max_xri <= 2048)
12064 return 200;
12065 else
12066 return 250;
James Smartf1126682009-06-10 17:22:44 -040012067 } else
12068 return 0;
James Smartda0436e2009-05-22 14:51:39 -040012069}
12070
12071/**
James Smart895427b2017-02-12 13:52:30 -080012072 * lpfc_sli4_get_iocb_cnt - Calculate the # of total IOCBs to reserve
12073 * @phba: pointer to lpfc hba data structure.
12074 *
James Smartf358dd02017-02-12 13:52:34 -080012075 * returns the number of ELS/CT + NVMET IOCBs to reserve
James Smart895427b2017-02-12 13:52:30 -080012076 **/
12077int
12078lpfc_sli4_get_iocb_cnt(struct lpfc_hba *phba)
12079{
12080 int max_xri = lpfc_sli4_get_els_iocb_cnt(phba);
12081
James Smartf358dd02017-02-12 13:52:34 -080012082 if (phba->nvmet_support)
12083 max_xri += LPFC_NVMET_BUF_POST;
James Smart895427b2017-02-12 13:52:30 -080012084 return max_xri;
12085}
12086
12087
James Smart1feb8202018-02-22 08:18:47 -080012088static void
12089lpfc_log_write_firmware_error(struct lpfc_hba *phba, uint32_t offset,
12090 uint32_t magic_number, uint32_t ftype, uint32_t fid, uint32_t fsize,
12091 const struct firmware *fw)
12092{
James Smarta72d56b2018-05-04 20:37:52 -070012093 if ((offset == ADD_STATUS_FW_NOT_SUPPORTED) ||
12094 (phba->pcidev->device == PCI_DEVICE_ID_LANCER_G6_FC &&
12095 magic_number != MAGIC_NUMER_G6) ||
12096 (phba->pcidev->device == PCI_DEVICE_ID_LANCER_G7_FC &&
12097 magic_number != MAGIC_NUMER_G7))
James Smart1feb8202018-02-22 08:18:47 -080012098 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12099 "3030 This firmware version is not supported on "
12100 "this HBA model. Device:%x Magic:%x Type:%x "
12101 "ID:%x Size %d %zd\n",
12102 phba->pcidev->device, magic_number, ftype, fid,
12103 fsize, fw->size);
12104 else
12105 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12106 "3022 FW Download failed. Device:%x Magic:%x Type:%x "
12107 "ID:%x Size %d %zd\n",
12108 phba->pcidev->device, magic_number, ftype, fid,
12109 fsize, fw->size);
12110}
12111
12112
James Smart895427b2017-02-12 13:52:30 -080012113/**
James Smart52d52442011-05-24 11:42:45 -040012114 * lpfc_write_firmware - attempt to write a firmware image to the port
James Smart52d52442011-05-24 11:42:45 -040012115 * @fw: pointer to firmware image returned from request_firmware.
James Smartce396282012-09-29 11:30:56 -040012116 * @phba: pointer to lpfc hba data structure.
James Smart52d52442011-05-24 11:42:45 -040012117 *
James Smart52d52442011-05-24 11:42:45 -040012118 **/
James Smartce396282012-09-29 11:30:56 -040012119static void
12120lpfc_write_firmware(const struct firmware *fw, void *context)
James Smart52d52442011-05-24 11:42:45 -040012121{
James Smartce396282012-09-29 11:30:56 -040012122 struct lpfc_hba *phba = (struct lpfc_hba *)context;
James Smart6b5151f2012-01-18 16:24:06 -050012123 char fwrev[FW_REV_STR_SIZE];
James Smartce396282012-09-29 11:30:56 -040012124 struct lpfc_grp_hdr *image;
James Smart52d52442011-05-24 11:42:45 -040012125 struct list_head dma_buffer_list;
12126 int i, rc = 0;
12127 struct lpfc_dmabuf *dmabuf, *next;
12128 uint32_t offset = 0, temp_offset = 0;
James Smart6b6ef5d2016-10-13 15:06:17 -070012129 uint32_t magic_number, ftype, fid, fsize;
James Smart52d52442011-05-24 11:42:45 -040012130
James Smartc71ab862012-10-31 14:44:33 -040012131 /* It can be null in no-wait mode, sanity check */
James Smartce396282012-09-29 11:30:56 -040012132 if (!fw) {
12133 rc = -ENXIO;
12134 goto out;
12135 }
12136 image = (struct lpfc_grp_hdr *)fw->data;
12137
James Smart6b6ef5d2016-10-13 15:06:17 -070012138 magic_number = be32_to_cpu(image->magic_number);
12139 ftype = bf_get_be32(lpfc_grp_hdr_file_type, image);
James Smart1feb8202018-02-22 08:18:47 -080012140 fid = bf_get_be32(lpfc_grp_hdr_id, image);
James Smart6b6ef5d2016-10-13 15:06:17 -070012141 fsize = be32_to_cpu(image->size);
12142
James Smart52d52442011-05-24 11:42:45 -040012143 INIT_LIST_HEAD(&dma_buffer_list);
James Smart52d52442011-05-24 11:42:45 -040012144 lpfc_decode_firmware_rev(phba, fwrev, 1);
James Smart88a2cfb2011-07-22 18:36:33 -040012145 if (strncmp(fwrev, image->revision, strnlen(image->revision, 16))) {
James Smart52d52442011-05-24 11:42:45 -040012146 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartce396282012-09-29 11:30:56 -040012147 "3023 Updating Firmware, Current Version:%s "
James Smart52d52442011-05-24 11:42:45 -040012148 "New Version:%s\n",
James Smart88a2cfb2011-07-22 18:36:33 -040012149 fwrev, image->revision);
James Smart52d52442011-05-24 11:42:45 -040012150 for (i = 0; i < LPFC_MBX_WR_CONFIG_MAX_BDE; i++) {
12151 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf),
12152 GFP_KERNEL);
12153 if (!dmabuf) {
12154 rc = -ENOMEM;
James Smartce396282012-09-29 11:30:56 -040012155 goto release_out;
James Smart52d52442011-05-24 11:42:45 -040012156 }
12157 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
12158 SLI4_PAGE_SIZE,
12159 &dmabuf->phys,
12160 GFP_KERNEL);
12161 if (!dmabuf->virt) {
12162 kfree(dmabuf);
12163 rc = -ENOMEM;
James Smartce396282012-09-29 11:30:56 -040012164 goto release_out;
James Smart52d52442011-05-24 11:42:45 -040012165 }
12166 list_add_tail(&dmabuf->list, &dma_buffer_list);
12167 }
12168 while (offset < fw->size) {
12169 temp_offset = offset;
12170 list_for_each_entry(dmabuf, &dma_buffer_list, list) {
James Smart079b5c92011-08-21 21:48:49 -040012171 if (temp_offset + SLI4_PAGE_SIZE > fw->size) {
James Smart52d52442011-05-24 11:42:45 -040012172 memcpy(dmabuf->virt,
12173 fw->data + temp_offset,
James Smart079b5c92011-08-21 21:48:49 -040012174 fw->size - temp_offset);
12175 temp_offset = fw->size;
James Smart52d52442011-05-24 11:42:45 -040012176 break;
12177 }
James Smart52d52442011-05-24 11:42:45 -040012178 memcpy(dmabuf->virt, fw->data + temp_offset,
12179 SLI4_PAGE_SIZE);
James Smart88a2cfb2011-07-22 18:36:33 -040012180 temp_offset += SLI4_PAGE_SIZE;
James Smart52d52442011-05-24 11:42:45 -040012181 }
12182 rc = lpfc_wr_object(phba, &dma_buffer_list,
12183 (fw->size - offset), &offset);
James Smart1feb8202018-02-22 08:18:47 -080012184 if (rc) {
12185 lpfc_log_write_firmware_error(phba, offset,
12186 magic_number, ftype, fid, fsize, fw);
James Smartce396282012-09-29 11:30:56 -040012187 goto release_out;
James Smart1feb8202018-02-22 08:18:47 -080012188 }
James Smart52d52442011-05-24 11:42:45 -040012189 }
12190 rc = offset;
James Smart1feb8202018-02-22 08:18:47 -080012191 } else
12192 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12193 "3029 Skipped Firmware update, Current "
12194 "Version:%s New Version:%s\n",
12195 fwrev, image->revision);
James Smartce396282012-09-29 11:30:56 -040012196
12197release_out:
James Smart52d52442011-05-24 11:42:45 -040012198 list_for_each_entry_safe(dmabuf, next, &dma_buffer_list, list) {
12199 list_del(&dmabuf->list);
12200 dma_free_coherent(&phba->pcidev->dev, SLI4_PAGE_SIZE,
12201 dmabuf->virt, dmabuf->phys);
12202 kfree(dmabuf);
12203 }
James Smartce396282012-09-29 11:30:56 -040012204 release_firmware(fw);
12205out:
12206 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartc71ab862012-10-31 14:44:33 -040012207 "3024 Firmware update done: %d.\n", rc);
James Smartce396282012-09-29 11:30:56 -040012208 return;
James Smart52d52442011-05-24 11:42:45 -040012209}
12210
12211/**
James Smartc71ab862012-10-31 14:44:33 -040012212 * lpfc_sli4_request_firmware_update - Request linux generic firmware upgrade
12213 * @phba: pointer to lpfc hba data structure.
12214 *
12215 * This routine is called to perform Linux generic firmware upgrade on device
12216 * that supports such feature.
12217 **/
12218int
12219lpfc_sli4_request_firmware_update(struct lpfc_hba *phba, uint8_t fw_upgrade)
12220{
12221 uint8_t file_name[ELX_MODEL_NAME_SIZE];
12222 int ret;
12223 const struct firmware *fw;
12224
12225 /* Only supported on SLI4 interface type 2 for now */
James Smart27d6ac02018-02-22 08:18:42 -080012226 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) <
James Smartc71ab862012-10-31 14:44:33 -040012227 LPFC_SLI_INTF_IF_TYPE_2)
12228 return -EPERM;
12229
12230 snprintf(file_name, ELX_MODEL_NAME_SIZE, "%s.grp", phba->ModelName);
12231
12232 if (fw_upgrade == INT_FW_UPGRADE) {
12233 ret = request_firmware_nowait(THIS_MODULE, FW_ACTION_HOTPLUG,
12234 file_name, &phba->pcidev->dev,
12235 GFP_KERNEL, (void *)phba,
12236 lpfc_write_firmware);
12237 } else if (fw_upgrade == RUN_FW_UPGRADE) {
12238 ret = request_firmware(&fw, file_name, &phba->pcidev->dev);
12239 if (!ret)
12240 lpfc_write_firmware(fw, (void *)phba);
12241 } else {
12242 ret = -EINVAL;
12243 }
12244
12245 return ret;
12246}
12247
12248/**
James Smartda0436e2009-05-22 14:51:39 -040012249 * lpfc_pci_probe_one_s4 - PCI probe func to reg SLI-4 device to PCI subsys
12250 * @pdev: pointer to PCI device
12251 * @pid: pointer to PCI device identifier
12252 *
12253 * This routine is called from the kernel's PCI subsystem to device with
12254 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
12255 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
12256 * information of the device and driver to see if the driver state that it
12257 * can support this kind of device. If the match is successful, the driver
12258 * core invokes this routine. If this routine determines it can claim the HBA,
12259 * it does all the initialization that it needs to do to handle the HBA
12260 * properly.
12261 *
12262 * Return code
12263 * 0 - driver can claim the device
12264 * negative value - driver can not claim the device
12265 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080012266static int
James Smartda0436e2009-05-22 14:51:39 -040012267lpfc_pci_probe_one_s4(struct pci_dev *pdev, const struct pci_device_id *pid)
12268{
12269 struct lpfc_hba *phba;
12270 struct lpfc_vport *vport = NULL;
James Smart6669f9b2009-10-02 15:16:45 -040012271 struct Scsi_Host *shost = NULL;
James Smart6c621a22017-05-15 15:20:45 -070012272 int error;
James Smartda0436e2009-05-22 14:51:39 -040012273 uint32_t cfg_mode, intr_mode;
James Smartda0436e2009-05-22 14:51:39 -040012274
12275 /* Allocate memory for HBA structure */
12276 phba = lpfc_hba_alloc(pdev);
12277 if (!phba)
12278 return -ENOMEM;
12279
12280 /* Perform generic PCI device enabling operation */
12281 error = lpfc_enable_pci_dev(phba);
James Smart079b5c92011-08-21 21:48:49 -040012282 if (error)
James Smartda0436e2009-05-22 14:51:39 -040012283 goto out_free_phba;
James Smartda0436e2009-05-22 14:51:39 -040012284
12285 /* Set up SLI API function jump table for PCI-device group-1 HBAs */
12286 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_OC);
12287 if (error)
12288 goto out_disable_pci_dev;
12289
12290 /* Set up SLI-4 specific device PCI memory space */
12291 error = lpfc_sli4_pci_mem_setup(phba);
12292 if (error) {
12293 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12294 "1410 Failed to set up pci memory space.\n");
12295 goto out_disable_pci_dev;
12296 }
12297
James Smartda0436e2009-05-22 14:51:39 -040012298 /* Set up SLI-4 Specific device driver resources */
12299 error = lpfc_sli4_driver_resource_setup(phba);
12300 if (error) {
12301 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12302 "1412 Failed to set up driver resource.\n");
12303 goto out_unset_pci_mem_s4;
12304 }
12305
James Smart19ca7602010-11-20 23:11:55 -050012306 INIT_LIST_HEAD(&phba->active_rrq_list);
James Smart7d791df2011-07-22 18:37:52 -040012307 INIT_LIST_HEAD(&phba->fcf.fcf_pri_list);
James Smart19ca7602010-11-20 23:11:55 -050012308
James Smartda0436e2009-05-22 14:51:39 -040012309 /* Set up common device driver resources */
12310 error = lpfc_setup_driver_resource_phase2(phba);
12311 if (error) {
12312 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12313 "1414 Failed to set up driver resource.\n");
James Smart6c621a22017-05-15 15:20:45 -070012314 goto out_unset_driver_resource_s4;
James Smartda0436e2009-05-22 14:51:39 -040012315 }
12316
James Smart079b5c92011-08-21 21:48:49 -040012317 /* Get the default values for Model Name and Description */
12318 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
12319
James Smartda0436e2009-05-22 14:51:39 -040012320 /* Now, trying to enable interrupt and bring up the device */
12321 cfg_mode = phba->cfg_use_msi;
James Smartda0436e2009-05-22 14:51:39 -040012322
James Smart7b15db32013-01-03 15:43:29 -050012323 /* Put device to a known state before enabling interrupt */
James Smartcdb42be2019-01-28 11:14:21 -080012324 phba->pport = NULL;
James Smart7b15db32013-01-03 15:43:29 -050012325 lpfc_stop_port(phba);
James Smart895427b2017-02-12 13:52:30 -080012326
James Smart7b15db32013-01-03 15:43:29 -050012327 /* Configure and enable interrupt */
12328 intr_mode = lpfc_sli4_enable_intr(phba, cfg_mode);
12329 if (intr_mode == LPFC_INTR_ERROR) {
12330 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12331 "0426 Failed to enable interrupt.\n");
12332 error = -ENODEV;
James Smartcdb42be2019-01-28 11:14:21 -080012333 goto out_unset_driver_resource;
James Smartda0436e2009-05-22 14:51:39 -040012334 }
James Smart7b15db32013-01-03 15:43:29 -050012335 /* Default to single EQ for non-MSI-X */
James Smart895427b2017-02-12 13:52:30 -080012336 if (phba->intr_type != MSIX) {
James Smart6a828b02019-01-28 11:14:31 -080012337 phba->cfg_irq_chann = 1;
James Smart2d7dbc42017-02-12 13:52:35 -080012338 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
James Smart2d7dbc42017-02-12 13:52:35 -080012339 if (phba->nvmet_support)
12340 phba->cfg_nvmet_mrq = 1;
12341 }
James Smartcdb42be2019-01-28 11:14:21 -080012342 }
James Smart6a828b02019-01-28 11:14:31 -080012343 lpfc_cpu_affinity_check(phba, phba->cfg_irq_chann);
James Smartcdb42be2019-01-28 11:14:21 -080012344
12345 /* Create SCSI host to the physical port */
12346 error = lpfc_create_shost(phba);
12347 if (error) {
12348 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12349 "1415 Failed to create scsi host.\n");
12350 goto out_disable_intr;
12351 }
12352 vport = phba->pport;
12353 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
12354
12355 /* Configure sysfs attributes */
12356 error = lpfc_alloc_sysfs_attr(vport);
12357 if (error) {
12358 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12359 "1416 Failed to allocate sysfs attr\n");
12360 goto out_destroy_shost;
James Smart895427b2017-02-12 13:52:30 -080012361 }
12362
James Smart7b15db32013-01-03 15:43:29 -050012363 /* Set up SLI-4 HBA */
12364 if (lpfc_sli4_hba_setup(phba)) {
12365 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12366 "1421 Failed to set up hba\n");
12367 error = -ENODEV;
James Smartcdb42be2019-01-28 11:14:21 -080012368 goto out_free_sysfs_attr;
James Smart7b15db32013-01-03 15:43:29 -050012369 }
12370
12371 /* Log the current active interrupt mode */
12372 phba->intr_mode = intr_mode;
12373 lpfc_log_intr_mode(phba, intr_mode);
James Smartda0436e2009-05-22 14:51:39 -040012374
12375 /* Perform post initialization setup */
12376 lpfc_post_init_setup(phba);
12377
James Smart01649562017-02-12 13:52:32 -080012378 /* NVME support in FW earlier in the driver load corrects the
12379 * FC4 type making a check for nvme_support unnecessary.
12380 */
James Smart0794d602019-01-28 11:14:19 -080012381 if (phba->nvmet_support == 0) {
12382 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
12383 /* Create NVME binding with nvme_fc_transport. This
12384 * ensures the vport is initialized. If the localport
12385 * create fails, it should not unload the driver to
12386 * support field issues.
12387 */
12388 error = lpfc_nvme_create_localport(vport);
12389 if (error) {
12390 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12391 "6004 NVME registration "
12392 "failed, error x%x\n",
12393 error);
12394 }
James Smart01649562017-02-12 13:52:32 -080012395 }
12396 }
James Smart895427b2017-02-12 13:52:30 -080012397
James Smartc71ab862012-10-31 14:44:33 -040012398 /* check for firmware upgrade or downgrade */
12399 if (phba->cfg_request_firmware_upgrade)
Sebastian Herbsztdb6f1c22015-08-31 16:48:14 -040012400 lpfc_sli4_request_firmware_update(phba, INT_FW_UPGRADE);
James Smart52d52442011-05-24 11:42:45 -040012401
James Smart1c6834a2009-07-19 10:01:26 -040012402 /* Check if there are static vports to be created. */
12403 lpfc_create_static_vport(phba);
James Smartd2cc9bc2018-09-10 10:30:50 -070012404
12405 /* Enable RAS FW log support */
12406 lpfc_sli4_ras_setup(phba);
12407
James Smartda0436e2009-05-22 14:51:39 -040012408 return 0;
12409
James Smartda0436e2009-05-22 14:51:39 -040012410out_free_sysfs_attr:
12411 lpfc_free_sysfs_attr(vport);
12412out_destroy_shost:
12413 lpfc_destroy_shost(phba);
James Smartcdb42be2019-01-28 11:14:21 -080012414out_disable_intr:
12415 lpfc_sli4_disable_intr(phba);
James Smartda0436e2009-05-22 14:51:39 -040012416out_unset_driver_resource:
12417 lpfc_unset_driver_resource_phase2(phba);
James Smartda0436e2009-05-22 14:51:39 -040012418out_unset_driver_resource_s4:
12419 lpfc_sli4_driver_resource_unset(phba);
12420out_unset_pci_mem_s4:
12421 lpfc_sli4_pci_mem_unset(phba);
12422out_disable_pci_dev:
12423 lpfc_disable_pci_dev(phba);
James Smart6669f9b2009-10-02 15:16:45 -040012424 if (shost)
12425 scsi_host_put(shost);
James Smartda0436e2009-05-22 14:51:39 -040012426out_free_phba:
12427 lpfc_hba_free(phba);
12428 return error;
12429}
12430
12431/**
12432 * lpfc_pci_remove_one_s4 - PCI func to unreg SLI-4 device from PCI subsystem
12433 * @pdev: pointer to PCI device
12434 *
12435 * This routine is called from the kernel's PCI subsystem to device with
12436 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
12437 * removed from PCI bus, it performs all the necessary cleanup for the HBA
12438 * device to be removed from the PCI subsystem properly.
12439 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080012440static void
James Smartda0436e2009-05-22 14:51:39 -040012441lpfc_pci_remove_one_s4(struct pci_dev *pdev)
12442{
12443 struct Scsi_Host *shost = pci_get_drvdata(pdev);
12444 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
12445 struct lpfc_vport **vports;
12446 struct lpfc_hba *phba = vport->phba;
12447 int i;
12448
12449 /* Mark the device unloading flag */
12450 spin_lock_irq(&phba->hbalock);
12451 vport->load_flag |= FC_UNLOADING;
12452 spin_unlock_irq(&phba->hbalock);
12453
12454 /* Free the HBA sysfs attributes */
12455 lpfc_free_sysfs_attr(vport);
12456
12457 /* Release all the vports against this physical port */
12458 vports = lpfc_create_vport_work_array(phba);
12459 if (vports != NULL)
James Smart587a37f2012-05-09 21:16:03 -040012460 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
12461 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
12462 continue;
James Smartda0436e2009-05-22 14:51:39 -040012463 fc_vport_terminate(vports[i]->fc_vport);
James Smart587a37f2012-05-09 21:16:03 -040012464 }
James Smartda0436e2009-05-22 14:51:39 -040012465 lpfc_destroy_vport_work_array(phba, vports);
12466
12467 /* Remove FC host and then SCSI host with the physical port */
12468 fc_remove_host(shost);
12469 scsi_remove_host(shost);
James Smartda0436e2009-05-22 14:51:39 -040012470
James Smartda0436e2009-05-22 14:51:39 -040012471 /* Perform ndlp cleanup on the physical port. The nvme and nvmet
12472 * localports are destroyed after to cleanup all transport memory.
12473 */
12474 lpfc_cleanup(vport);
12475 lpfc_nvmet_destroy_targetport(phba);
12476 lpfc_nvme_destroy_localport(vport);
12477
James Smartc4908502019-01-28 11:14:28 -080012478 /* De-allocate multi-XRI pools */
12479 if (phba->cfg_xri_rebalancing)
12480 lpfc_destroy_multixri_pools(phba);
12481
James Smart281d6192018-01-30 15:58:47 -080012482 /*
12483 * Bring down the SLI Layer. This step disables all interrupts,
12484 * clears the rings, discards all mailbox commands, and resets
12485 * the HBA FCoE function.
12486 */
12487 lpfc_debugfs_terminate(vport);
James Smartda0436e2009-05-22 14:51:39 -040012488
Dick Kennedy19017622017-09-29 17:34:27 -070012489 lpfc_stop_hba_timers(phba);
James Smart523128e2018-09-10 10:30:46 -070012490 spin_lock_irq(&phba->port_list_lock);
James Smartda0436e2009-05-22 14:51:39 -040012491 list_del_init(&vport->listentry);
James Smart523128e2018-09-10 10:30:46 -070012492 spin_unlock_irq(&phba->port_list_lock);
James Smartda0436e2009-05-22 14:51:39 -040012493
James Smart3677a3a2010-09-29 11:19:14 -040012494 /* Perform scsi free before driver resource_unset since scsi
James Smartda0436e2009-05-22 14:51:39 -040012495 * buffers are released to their corresponding pools here.
12496 */
James Smart5e5b5112019-01-28 11:14:22 -080012497 lpfc_io_free(phba);
James Smart01649562017-02-12 13:52:32 -080012498 lpfc_free_iocb_list(phba);
James Smart5e5b5112019-01-28 11:14:22 -080012499 lpfc_sli4_hba_unset(phba);
James Smart67d12732012-08-03 12:36:13 -040012500
James Smart0cdb84e2018-04-09 14:24:26 -070012501 lpfc_unset_driver_resource_phase2(phba);
James Smartda0436e2009-05-22 14:51:39 -040012502 lpfc_sli4_driver_resource_unset(phba);
12503
12504 /* Unmap adapter Control and Doorbell registers */
12505 lpfc_sli4_pci_mem_unset(phba);
12506
12507 /* Release PCI resources and disable device's PCI function */
12508 scsi_host_put(shost);
12509 lpfc_disable_pci_dev(phba);
12510
12511 /* Finally, free the driver's device data structure */
12512 lpfc_hba_free(phba);
12513
12514 return;
12515}
12516
12517/**
12518 * lpfc_pci_suspend_one_s4 - PCI func to suspend SLI-4 device for power mgmnt
12519 * @pdev: pointer to PCI device
12520 * @msg: power management message
12521 *
12522 * This routine is called from the kernel's PCI subsystem to support system
12523 * Power Management (PM) to device with SLI-4 interface spec. When PM invokes
12524 * this method, it quiesces the device by stopping the driver's worker
12525 * thread for the device, turning off device's interrupt and DMA, and bring
12526 * the device offline. Note that as the driver implements the minimum PM
12527 * requirements to a power-aware driver's PM support for suspend/resume -- all
12528 * the possible PM messages (SUSPEND, HIBERNATE, FREEZE) to the suspend()
12529 * method call will be treated as SUSPEND and the driver will fully
12530 * reinitialize its device during resume() method call, the driver will set
12531 * device to PCI_D3hot state in PCI config space instead of setting it
12532 * according to the @msg provided by the PM.
12533 *
12534 * Return code
12535 * 0 - driver suspended the device
12536 * Error otherwise
12537 **/
12538static int
12539lpfc_pci_suspend_one_s4(struct pci_dev *pdev, pm_message_t msg)
12540{
12541 struct Scsi_Host *shost = pci_get_drvdata(pdev);
12542 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
12543
12544 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart75baf692010-06-08 18:31:21 -040012545 "2843 PCI device Power Management suspend.\n");
James Smartda0436e2009-05-22 14:51:39 -040012546
12547 /* Bring down the device */
James Smart618a5232012-06-12 13:54:36 -040012548 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smartda0436e2009-05-22 14:51:39 -040012549 lpfc_offline(phba);
12550 kthread_stop(phba->worker_thread);
12551
12552 /* Disable interrupt from device */
12553 lpfc_sli4_disable_intr(phba);
James Smart5350d872011-10-10 21:33:49 -040012554 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -040012555
12556 /* Save device state to PCI config space */
12557 pci_save_state(pdev);
12558 pci_set_power_state(pdev, PCI_D3hot);
12559
12560 return 0;
12561}
12562
12563/**
12564 * lpfc_pci_resume_one_s4 - PCI func to resume SLI-4 device for power mgmnt
12565 * @pdev: pointer to PCI device
12566 *
12567 * This routine is called from the kernel's PCI subsystem to support system
12568 * Power Management (PM) to device with SLI-4 interface spac. When PM invokes
12569 * this method, it restores the device's PCI config space state and fully
12570 * reinitializes the device and brings it online. Note that as the driver
12571 * implements the minimum PM requirements to a power-aware driver's PM for
12572 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
12573 * to the suspend() method call will be treated as SUSPEND and the driver
12574 * will fully reinitialize its device during resume() method call, the device
12575 * will be set to PCI_D0 directly in PCI config space before restoring the
12576 * state.
12577 *
12578 * Return code
12579 * 0 - driver suspended the device
12580 * Error otherwise
12581 **/
12582static int
12583lpfc_pci_resume_one_s4(struct pci_dev *pdev)
12584{
12585 struct Scsi_Host *shost = pci_get_drvdata(pdev);
12586 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
12587 uint32_t intr_mode;
12588 int error;
12589
12590 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
12591 "0292 PCI device Power Management resume.\n");
12592
12593 /* Restore device state from PCI config space */
12594 pci_set_power_state(pdev, PCI_D0);
12595 pci_restore_state(pdev);
James Smart1dfb5a42010-02-12 14:40:50 -050012596
12597 /*
12598 * As the new kernel behavior of pci_restore_state() API call clears
12599 * device saved_state flag, need to save the restored state again.
12600 */
12601 pci_save_state(pdev);
12602
James Smartda0436e2009-05-22 14:51:39 -040012603 if (pdev->is_busmaster)
12604 pci_set_master(pdev);
12605
12606 /* Startup the kernel thread for this host adapter. */
12607 phba->worker_thread = kthread_run(lpfc_do_work, phba,
12608 "lpfc_worker_%d", phba->brd_no);
12609 if (IS_ERR(phba->worker_thread)) {
12610 error = PTR_ERR(phba->worker_thread);
12611 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12612 "0293 PM resume failed to start worker "
12613 "thread: error=x%x.\n", error);
12614 return error;
12615 }
12616
12617 /* Configure and enable interrupt */
12618 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
12619 if (intr_mode == LPFC_INTR_ERROR) {
12620 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12621 "0294 PM resume Failed to enable interrupt\n");
12622 return -EIO;
12623 } else
12624 phba->intr_mode = intr_mode;
12625
12626 /* Restart HBA and bring it online */
12627 lpfc_sli_brdrestart(phba);
12628 lpfc_online(phba);
12629
12630 /* Log the current active interrupt mode */
12631 lpfc_log_intr_mode(phba, phba->intr_mode);
12632
12633 return 0;
12634}
12635
12636/**
James Smart75baf692010-06-08 18:31:21 -040012637 * lpfc_sli4_prep_dev_for_recover - Prepare SLI4 device for pci slot recover
12638 * @phba: pointer to lpfc hba data structure.
12639 *
12640 * This routine is called to prepare the SLI4 device for PCI slot recover. It
12641 * aborts all the outstanding SCSI I/Os to the pci device.
12642 **/
12643static void
12644lpfc_sli4_prep_dev_for_recover(struct lpfc_hba *phba)
12645{
James Smart75baf692010-06-08 18:31:21 -040012646 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12647 "2828 PCI channel I/O abort preparing for recovery\n");
12648 /*
12649 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
12650 * and let the SCSI mid-layer to retry them to recover.
12651 */
James Smartdb55fba2014-04-04 13:52:02 -040012652 lpfc_sli_abort_fcp_rings(phba);
James Smart75baf692010-06-08 18:31:21 -040012653}
12654
12655/**
12656 * lpfc_sli4_prep_dev_for_reset - Prepare SLI4 device for pci slot reset
12657 * @phba: pointer to lpfc hba data structure.
12658 *
12659 * This routine is called to prepare the SLI4 device for PCI slot reset. It
12660 * disables the device interrupt and pci device, and aborts the internal FCP
12661 * pending I/Os.
12662 **/
12663static void
12664lpfc_sli4_prep_dev_for_reset(struct lpfc_hba *phba)
12665{
12666 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12667 "2826 PCI channel disable preparing for reset\n");
12668
12669 /* Block any management I/Os to the device */
James Smart618a5232012-06-12 13:54:36 -040012670 lpfc_block_mgmt_io(phba, LPFC_MBX_NO_WAIT);
James Smart75baf692010-06-08 18:31:21 -040012671
12672 /* Block all SCSI devices' I/Os on the host */
12673 lpfc_scsi_dev_block(phba);
12674
James Smartea714f32013-04-17 20:18:47 -040012675 /* Flush all driver's outstanding SCSI I/Os as we are to reset */
12676 lpfc_sli_flush_fcp_rings(phba);
12677
James Smartc3725bd2017-11-20 16:00:42 -080012678 /* Flush the outstanding NVME IOs if fc4 type enabled. */
12679 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
12680 lpfc_sli_flush_nvme_rings(phba);
12681
James Smart75baf692010-06-08 18:31:21 -040012682 /* stop all timers */
12683 lpfc_stop_hba_timers(phba);
12684
12685 /* Disable interrupt and pci device */
12686 lpfc_sli4_disable_intr(phba);
James Smart5350d872011-10-10 21:33:49 -040012687 lpfc_sli4_queue_destroy(phba);
James Smart75baf692010-06-08 18:31:21 -040012688 pci_disable_device(phba->pcidev);
James Smart75baf692010-06-08 18:31:21 -040012689}
12690
12691/**
12692 * lpfc_sli4_prep_dev_for_perm_failure - Prepare SLI4 dev for pci slot disable
12693 * @phba: pointer to lpfc hba data structure.
12694 *
12695 * This routine is called to prepare the SLI4 device for PCI slot permanently
12696 * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
12697 * pending I/Os.
12698 **/
12699static void
12700lpfc_sli4_prep_dev_for_perm_failure(struct lpfc_hba *phba)
12701{
12702 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12703 "2827 PCI channel permanent disable for failure\n");
12704
12705 /* Block all SCSI devices' I/Os on the host */
12706 lpfc_scsi_dev_block(phba);
12707
12708 /* stop all timers */
12709 lpfc_stop_hba_timers(phba);
12710
12711 /* Clean up all driver's outstanding SCSI I/Os */
12712 lpfc_sli_flush_fcp_rings(phba);
James Smartc3725bd2017-11-20 16:00:42 -080012713
12714 /* Flush the outstanding NVME IOs if fc4 type enabled. */
12715 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
12716 lpfc_sli_flush_nvme_rings(phba);
James Smart75baf692010-06-08 18:31:21 -040012717}
12718
12719/**
James Smartda0436e2009-05-22 14:51:39 -040012720 * lpfc_io_error_detected_s4 - Method for handling PCI I/O error to SLI-4 device
12721 * @pdev: pointer to PCI device.
12722 * @state: the current PCI connection state.
12723 *
12724 * This routine is called from the PCI subsystem for error handling to device
12725 * with SLI-4 interface spec. This function is called by the PCI subsystem
12726 * after a PCI bus error affecting this device has been detected. When this
12727 * function is invoked, it will need to stop all the I/Os and interrupt(s)
12728 * to the device. Once that is done, it will return PCI_ERS_RESULT_NEED_RESET
12729 * for the PCI subsystem to perform proper recovery as desired.
12730 *
12731 * Return codes
12732 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
12733 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
12734 **/
12735static pci_ers_result_t
12736lpfc_io_error_detected_s4(struct pci_dev *pdev, pci_channel_state_t state)
12737{
James Smart75baf692010-06-08 18:31:21 -040012738 struct Scsi_Host *shost = pci_get_drvdata(pdev);
12739 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
12740
12741 switch (state) {
12742 case pci_channel_io_normal:
12743 /* Non-fatal error, prepare for recovery */
12744 lpfc_sli4_prep_dev_for_recover(phba);
12745 return PCI_ERS_RESULT_CAN_RECOVER;
12746 case pci_channel_io_frozen:
12747 /* Fatal error, prepare for slot reset */
12748 lpfc_sli4_prep_dev_for_reset(phba);
12749 return PCI_ERS_RESULT_NEED_RESET;
12750 case pci_channel_io_perm_failure:
12751 /* Permanent failure, prepare for device down */
12752 lpfc_sli4_prep_dev_for_perm_failure(phba);
12753 return PCI_ERS_RESULT_DISCONNECT;
12754 default:
12755 /* Unknown state, prepare and request slot reset */
12756 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12757 "2825 Unknown PCI error state: x%x\n", state);
12758 lpfc_sli4_prep_dev_for_reset(phba);
12759 return PCI_ERS_RESULT_NEED_RESET;
12760 }
James Smartda0436e2009-05-22 14:51:39 -040012761}
12762
12763/**
12764 * lpfc_io_slot_reset_s4 - Method for restart PCI SLI-4 device from scratch
12765 * @pdev: pointer to PCI device.
12766 *
12767 * This routine is called from the PCI subsystem for error handling to device
12768 * with SLI-4 interface spec. It is called after PCI bus has been reset to
12769 * restart the PCI card from scratch, as if from a cold-boot. During the
12770 * PCI subsystem error recovery, after the driver returns
12771 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
12772 * recovery and then call this routine before calling the .resume method to
12773 * recover the device. This function will initialize the HBA device, enable
12774 * the interrupt, but it will just put the HBA to offline state without
12775 * passing any I/O traffic.
12776 *
12777 * Return codes
12778 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
12779 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
12780 */
12781static pci_ers_result_t
12782lpfc_io_slot_reset_s4(struct pci_dev *pdev)
12783{
James Smart75baf692010-06-08 18:31:21 -040012784 struct Scsi_Host *shost = pci_get_drvdata(pdev);
12785 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
12786 struct lpfc_sli *psli = &phba->sli;
12787 uint32_t intr_mode;
12788
12789 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
12790 if (pci_enable_device_mem(pdev)) {
12791 printk(KERN_ERR "lpfc: Cannot re-enable "
12792 "PCI device after reset.\n");
12793 return PCI_ERS_RESULT_DISCONNECT;
12794 }
12795
12796 pci_restore_state(pdev);
James Smart0a96e972011-07-22 18:37:28 -040012797
12798 /*
12799 * As the new kernel behavior of pci_restore_state() API call clears
12800 * device saved_state flag, need to save the restored state again.
12801 */
12802 pci_save_state(pdev);
12803
James Smart75baf692010-06-08 18:31:21 -040012804 if (pdev->is_busmaster)
12805 pci_set_master(pdev);
12806
12807 spin_lock_irq(&phba->hbalock);
12808 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
12809 spin_unlock_irq(&phba->hbalock);
12810
12811 /* Configure and enable interrupt */
12812 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
12813 if (intr_mode == LPFC_INTR_ERROR) {
12814 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12815 "2824 Cannot re-enable interrupt after "
12816 "slot reset.\n");
12817 return PCI_ERS_RESULT_DISCONNECT;
12818 } else
12819 phba->intr_mode = intr_mode;
12820
12821 /* Log the current active interrupt mode */
12822 lpfc_log_intr_mode(phba, phba->intr_mode);
12823
James Smartda0436e2009-05-22 14:51:39 -040012824 return PCI_ERS_RESULT_RECOVERED;
12825}
12826
12827/**
12828 * lpfc_io_resume_s4 - Method for resuming PCI I/O operation to SLI-4 device
12829 * @pdev: pointer to PCI device
12830 *
12831 * This routine is called from the PCI subsystem for error handling to device
12832 * with SLI-4 interface spec. It is called when kernel error recovery tells
12833 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
12834 * error recovery. After this call, traffic can start to flow from this device
12835 * again.
12836 **/
12837static void
12838lpfc_io_resume_s4(struct pci_dev *pdev)
12839{
James Smart75baf692010-06-08 18:31:21 -040012840 struct Scsi_Host *shost = pci_get_drvdata(pdev);
12841 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
12842
12843 /*
12844 * In case of slot reset, as function reset is performed through
12845 * mailbox command which needs DMA to be enabled, this operation
12846 * has to be moved to the io resume phase. Taking device offline
12847 * will perform the necessary cleanup.
12848 */
12849 if (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE)) {
12850 /* Perform device reset */
James Smart618a5232012-06-12 13:54:36 -040012851 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart75baf692010-06-08 18:31:21 -040012852 lpfc_offline(phba);
12853 lpfc_sli_brdrestart(phba);
12854 /* Bring the device back online */
12855 lpfc_online(phba);
12856 }
James Smartda0436e2009-05-22 14:51:39 -040012857}
12858
12859/**
James Smart3772a992009-05-22 14:50:54 -040012860 * lpfc_pci_probe_one - lpfc PCI probe func to reg dev to PCI subsystem
12861 * @pdev: pointer to PCI device
12862 * @pid: pointer to PCI device identifier
12863 *
12864 * This routine is to be registered to the kernel's PCI subsystem. When an
12865 * Emulex HBA device is presented on PCI bus, the kernel PCI subsystem looks
12866 * at PCI device-specific information of the device and driver to see if the
12867 * driver state that it can support this kind of device. If the match is
12868 * successful, the driver core invokes this routine. This routine dispatches
12869 * the action to the proper SLI-3 or SLI-4 device probing routine, which will
12870 * do all the initialization that it needs to do to handle the HBA device
12871 * properly.
12872 *
12873 * Return code
12874 * 0 - driver can claim the device
12875 * negative value - driver can not claim the device
12876 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080012877static int
James Smart3772a992009-05-22 14:50:54 -040012878lpfc_pci_probe_one(struct pci_dev *pdev, const struct pci_device_id *pid)
12879{
12880 int rc;
James Smart8fa38512009-07-19 10:01:03 -040012881 struct lpfc_sli_intf intf;
James Smart3772a992009-05-22 14:50:54 -040012882
James Smart28baac72010-02-12 14:42:03 -050012883 if (pci_read_config_dword(pdev, LPFC_SLI_INTF, &intf.word0))
James Smart3772a992009-05-22 14:50:54 -040012884 return -ENODEV;
12885
James Smart8fa38512009-07-19 10:01:03 -040012886 if ((bf_get(lpfc_sli_intf_valid, &intf) == LPFC_SLI_INTF_VALID) &&
James Smart28baac72010-02-12 14:42:03 -050012887 (bf_get(lpfc_sli_intf_slirev, &intf) == LPFC_SLI_INTF_REV_SLI4))
James Smartda0436e2009-05-22 14:51:39 -040012888 rc = lpfc_pci_probe_one_s4(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -040012889 else
James Smart3772a992009-05-22 14:50:54 -040012890 rc = lpfc_pci_probe_one_s3(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -040012891
James Smart3772a992009-05-22 14:50:54 -040012892 return rc;
12893}
12894
12895/**
12896 * lpfc_pci_remove_one - lpfc PCI func to unreg dev from PCI subsystem
12897 * @pdev: pointer to PCI device
12898 *
12899 * This routine is to be registered to the kernel's PCI subsystem. When an
12900 * Emulex HBA is removed from PCI bus, the driver core invokes this routine.
12901 * This routine dispatches the action to the proper SLI-3 or SLI-4 device
12902 * remove routine, which will perform all the necessary cleanup for the
12903 * device to be removed from the PCI subsystem properly.
12904 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080012905static void
James Smart3772a992009-05-22 14:50:54 -040012906lpfc_pci_remove_one(struct pci_dev *pdev)
12907{
12908 struct Scsi_Host *shost = pci_get_drvdata(pdev);
12909 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
12910
12911 switch (phba->pci_dev_grp) {
12912 case LPFC_PCI_DEV_LP:
12913 lpfc_pci_remove_one_s3(pdev);
12914 break;
James Smartda0436e2009-05-22 14:51:39 -040012915 case LPFC_PCI_DEV_OC:
12916 lpfc_pci_remove_one_s4(pdev);
12917 break;
James Smart3772a992009-05-22 14:50:54 -040012918 default:
12919 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12920 "1424 Invalid PCI device group: 0x%x\n",
12921 phba->pci_dev_grp);
12922 break;
12923 }
12924 return;
12925}
12926
12927/**
12928 * lpfc_pci_suspend_one - lpfc PCI func to suspend dev for power management
12929 * @pdev: pointer to PCI device
12930 * @msg: power management message
12931 *
12932 * This routine is to be registered to the kernel's PCI subsystem to support
12933 * system Power Management (PM). When PM invokes this method, it dispatches
12934 * the action to the proper SLI-3 or SLI-4 device suspend routine, which will
12935 * suspend the device.
12936 *
12937 * Return code
12938 * 0 - driver suspended the device
12939 * Error otherwise
12940 **/
12941static int
12942lpfc_pci_suspend_one(struct pci_dev *pdev, pm_message_t msg)
12943{
12944 struct Scsi_Host *shost = pci_get_drvdata(pdev);
12945 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
12946 int rc = -ENODEV;
12947
12948 switch (phba->pci_dev_grp) {
12949 case LPFC_PCI_DEV_LP:
12950 rc = lpfc_pci_suspend_one_s3(pdev, msg);
12951 break;
James Smartda0436e2009-05-22 14:51:39 -040012952 case LPFC_PCI_DEV_OC:
12953 rc = lpfc_pci_suspend_one_s4(pdev, msg);
12954 break;
James Smart3772a992009-05-22 14:50:54 -040012955 default:
12956 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12957 "1425 Invalid PCI device group: 0x%x\n",
12958 phba->pci_dev_grp);
12959 break;
12960 }
12961 return rc;
12962}
12963
12964/**
12965 * lpfc_pci_resume_one - lpfc PCI func to resume dev for power management
12966 * @pdev: pointer to PCI device
12967 *
12968 * This routine is to be registered to the kernel's PCI subsystem to support
12969 * system Power Management (PM). When PM invokes this method, it dispatches
12970 * the action to the proper SLI-3 or SLI-4 device resume routine, which will
12971 * resume the device.
12972 *
12973 * Return code
12974 * 0 - driver suspended the device
12975 * Error otherwise
12976 **/
12977static int
12978lpfc_pci_resume_one(struct pci_dev *pdev)
12979{
12980 struct Scsi_Host *shost = pci_get_drvdata(pdev);
12981 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
12982 int rc = -ENODEV;
12983
12984 switch (phba->pci_dev_grp) {
12985 case LPFC_PCI_DEV_LP:
12986 rc = lpfc_pci_resume_one_s3(pdev);
12987 break;
James Smartda0436e2009-05-22 14:51:39 -040012988 case LPFC_PCI_DEV_OC:
12989 rc = lpfc_pci_resume_one_s4(pdev);
12990 break;
James Smart3772a992009-05-22 14:50:54 -040012991 default:
12992 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12993 "1426 Invalid PCI device group: 0x%x\n",
12994 phba->pci_dev_grp);
12995 break;
12996 }
12997 return rc;
12998}
12999
13000/**
13001 * lpfc_io_error_detected - lpfc method for handling PCI I/O error
13002 * @pdev: pointer to PCI device.
13003 * @state: the current PCI connection state.
13004 *
13005 * This routine is registered to the PCI subsystem for error handling. This
13006 * function is called by the PCI subsystem after a PCI bus error affecting
13007 * this device has been detected. When this routine is invoked, it dispatches
13008 * the action to the proper SLI-3 or SLI-4 device error detected handling
13009 * routine, which will perform the proper error detected operation.
13010 *
13011 * Return codes
13012 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
13013 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
13014 **/
13015static pci_ers_result_t
13016lpfc_io_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
13017{
13018 struct Scsi_Host *shost = pci_get_drvdata(pdev);
13019 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
13020 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
13021
13022 switch (phba->pci_dev_grp) {
13023 case LPFC_PCI_DEV_LP:
13024 rc = lpfc_io_error_detected_s3(pdev, state);
13025 break;
James Smartda0436e2009-05-22 14:51:39 -040013026 case LPFC_PCI_DEV_OC:
13027 rc = lpfc_io_error_detected_s4(pdev, state);
13028 break;
James Smart3772a992009-05-22 14:50:54 -040013029 default:
13030 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13031 "1427 Invalid PCI device group: 0x%x\n",
13032 phba->pci_dev_grp);
13033 break;
13034 }
13035 return rc;
13036}
13037
13038/**
13039 * lpfc_io_slot_reset - lpfc method for restart PCI dev from scratch
13040 * @pdev: pointer to PCI device.
13041 *
13042 * This routine is registered to the PCI subsystem for error handling. This
13043 * function is called after PCI bus has been reset to restart the PCI card
13044 * from scratch, as if from a cold-boot. When this routine is invoked, it
13045 * dispatches the action to the proper SLI-3 or SLI-4 device reset handling
13046 * routine, which will perform the proper device reset.
13047 *
13048 * Return codes
13049 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
13050 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
13051 **/
13052static pci_ers_result_t
13053lpfc_io_slot_reset(struct pci_dev *pdev)
13054{
13055 struct Scsi_Host *shost = pci_get_drvdata(pdev);
13056 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
13057 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
13058
13059 switch (phba->pci_dev_grp) {
13060 case LPFC_PCI_DEV_LP:
13061 rc = lpfc_io_slot_reset_s3(pdev);
13062 break;
James Smartda0436e2009-05-22 14:51:39 -040013063 case LPFC_PCI_DEV_OC:
13064 rc = lpfc_io_slot_reset_s4(pdev);
13065 break;
James Smart3772a992009-05-22 14:50:54 -040013066 default:
13067 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13068 "1428 Invalid PCI device group: 0x%x\n",
13069 phba->pci_dev_grp);
13070 break;
13071 }
13072 return rc;
13073}
13074
13075/**
13076 * lpfc_io_resume - lpfc method for resuming PCI I/O operation
13077 * @pdev: pointer to PCI device
13078 *
13079 * This routine is registered to the PCI subsystem for error handling. It
13080 * is called when kernel error recovery tells the lpfc driver that it is
13081 * OK to resume normal PCI operation after PCI bus error recovery. When
13082 * this routine is invoked, it dispatches the action to the proper SLI-3
13083 * or SLI-4 device io_resume routine, which will resume the device operation.
13084 **/
13085static void
13086lpfc_io_resume(struct pci_dev *pdev)
13087{
13088 struct Scsi_Host *shost = pci_get_drvdata(pdev);
13089 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
13090
13091 switch (phba->pci_dev_grp) {
13092 case LPFC_PCI_DEV_LP:
13093 lpfc_io_resume_s3(pdev);
13094 break;
James Smartda0436e2009-05-22 14:51:39 -040013095 case LPFC_PCI_DEV_OC:
13096 lpfc_io_resume_s4(pdev);
13097 break;
James Smart3772a992009-05-22 14:50:54 -040013098 default:
13099 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13100 "1429 Invalid PCI device group: 0x%x\n",
13101 phba->pci_dev_grp);
13102 break;
13103 }
13104 return;
13105}
13106
James Smart1ba981f2014-02-20 09:56:45 -050013107/**
13108 * lpfc_sli4_oas_verify - Verify OAS is supported by this adapter
13109 * @phba: pointer to lpfc hba data structure.
13110 *
13111 * This routine checks to see if OAS is supported for this adapter. If
13112 * supported, the configure Flash Optimized Fabric flag is set. Otherwise,
13113 * the enable oas flag is cleared and the pool created for OAS device data
13114 * is destroyed.
13115 *
13116 **/
YueHaibingc7092972019-04-17 23:05:54 +080013117static void
James Smart1ba981f2014-02-20 09:56:45 -050013118lpfc_sli4_oas_verify(struct lpfc_hba *phba)
13119{
13120
13121 if (!phba->cfg_EnableXLane)
13122 return;
13123
13124 if (phba->sli4_hba.pc_sli4_params.oas_supported) {
13125 phba->cfg_fof = 1;
13126 } else {
James Smartf38fa0b2014-04-04 13:52:21 -040013127 phba->cfg_fof = 0;
James Smart1ba981f2014-02-20 09:56:45 -050013128 if (phba->device_data_mem_pool)
13129 mempool_destroy(phba->device_data_mem_pool);
13130 phba->device_data_mem_pool = NULL;
13131 }
13132
13133 return;
13134}
13135
13136/**
James Smartd2cc9bc2018-09-10 10:30:50 -070013137 * lpfc_sli4_ras_init - Verify RAS-FW log is supported by this adapter
13138 * @phba: pointer to lpfc hba data structure.
13139 *
13140 * This routine checks to see if RAS is supported by the adapter. Check the
13141 * function through which RAS support enablement is to be done.
13142 **/
13143void
13144lpfc_sli4_ras_init(struct lpfc_hba *phba)
13145{
13146 switch (phba->pcidev->device) {
13147 case PCI_DEVICE_ID_LANCER_G6_FC:
13148 case PCI_DEVICE_ID_LANCER_G7_FC:
13149 phba->ras_fwlog.ras_hwsupport = true;
James Smartcb349902018-11-29 16:09:27 -080013150 if (phba->cfg_ras_fwlog_func == PCI_FUNC(phba->pcidev->devfn) &&
13151 phba->cfg_ras_fwlog_buffsize)
James Smartd2cc9bc2018-09-10 10:30:50 -070013152 phba->ras_fwlog.ras_enabled = true;
13153 else
13154 phba->ras_fwlog.ras_enabled = false;
13155 break;
13156 default:
13157 phba->ras_fwlog.ras_hwsupport = false;
13158 }
13159}
13160
James Smart1ba981f2014-02-20 09:56:45 -050013161
dea31012005-04-17 16:05:31 -050013162MODULE_DEVICE_TABLE(pci, lpfc_id_table);
13163
Stephen Hemmingera55b2d22012-09-07 09:33:16 -070013164static const struct pci_error_handlers lpfc_err_handler = {
Linas Vepstas8d63f372007-02-14 14:28:36 -060013165 .error_detected = lpfc_io_error_detected,
13166 .slot_reset = lpfc_io_slot_reset,
13167 .resume = lpfc_io_resume,
13168};
13169
dea31012005-04-17 16:05:31 -050013170static struct pci_driver lpfc_driver = {
13171 .name = LPFC_DRIVER_NAME,
13172 .id_table = lpfc_id_table,
13173 .probe = lpfc_pci_probe_one,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080013174 .remove = lpfc_pci_remove_one,
Anton Blanchard85e8a232017-02-13 08:49:20 +110013175 .shutdown = lpfc_pci_remove_one,
James Smart3a55b532008-12-04 22:38:54 -050013176 .suspend = lpfc_pci_suspend_one,
James Smart3772a992009-05-22 14:50:54 -040013177 .resume = lpfc_pci_resume_one,
James Smart2e0fef82007-06-17 19:56:36 -050013178 .err_handler = &lpfc_err_handler,
dea31012005-04-17 16:05:31 -050013179};
13180
James Smart3ef6d242012-01-18 16:23:48 -050013181static const struct file_operations lpfc_mgmt_fop = {
Al Viro858feac2013-04-14 22:39:37 -040013182 .owner = THIS_MODULE,
James Smart3ef6d242012-01-18 16:23:48 -050013183};
13184
13185static struct miscdevice lpfc_mgmt_dev = {
13186 .minor = MISC_DYNAMIC_MINOR,
13187 .name = "lpfcmgmt",
13188 .fops = &lpfc_mgmt_fop,
13189};
13190
James Smarte59058c2008-08-24 21:49:00 -040013191/**
James Smart3621a712009-04-06 18:47:14 -040013192 * lpfc_init - lpfc module initialization routine
James Smarte59058c2008-08-24 21:49:00 -040013193 *
13194 * This routine is to be invoked when the lpfc module is loaded into the
13195 * kernel. The special kernel macro module_init() is used to indicate the
13196 * role of this routine to the kernel as lpfc module entry point.
13197 *
13198 * Return codes
13199 * 0 - successful
13200 * -ENOMEM - FC attach transport failed
13201 * all others - failed
13202 */
dea31012005-04-17 16:05:31 -050013203static int __init
13204lpfc_init(void)
13205{
13206 int error = 0;
13207
13208 printk(LPFC_MODULE_DESC "\n");
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040013209 printk(LPFC_COPYRIGHT "\n");
dea31012005-04-17 16:05:31 -050013210
James Smart3ef6d242012-01-18 16:23:48 -050013211 error = misc_register(&lpfc_mgmt_dev);
13212 if (error)
13213 printk(KERN_ERR "Could not register lpfcmgmt device, "
13214 "misc_register returned with status %d", error);
13215
James Smart458c0832016-07-06 12:36:07 -070013216 lpfc_transport_functions.vport_create = lpfc_vport_create;
13217 lpfc_transport_functions.vport_delete = lpfc_vport_delete;
dea31012005-04-17 16:05:31 -050013218 lpfc_transport_template =
13219 fc_attach_transport(&lpfc_transport_functions);
James Smart7ee5d432007-10-27 13:37:17 -040013220 if (lpfc_transport_template == NULL)
dea31012005-04-17 16:05:31 -050013221 return -ENOMEM;
James Smart458c0832016-07-06 12:36:07 -070013222 lpfc_vport_transport_template =
13223 fc_attach_transport(&lpfc_vport_transport_functions);
13224 if (lpfc_vport_transport_template == NULL) {
13225 fc_release_transport(lpfc_transport_template);
13226 return -ENOMEM;
James Smart7ee5d432007-10-27 13:37:17 -040013227 }
James Smart5fd11082018-03-05 12:04:04 -080013228 lpfc_nvme_cmd_template();
James Smartbd3061b2018-03-05 12:04:05 -080013229 lpfc_nvmet_cmd_template();
James Smart7bb03bb2013-04-17 20:19:16 -040013230
13231 /* Initialize in case vector mapping is needed */
James Smart2ea259e2017-02-12 13:52:27 -080013232 lpfc_present_cpu = num_present_cpus();
James Smart7bb03bb2013-04-17 20:19:16 -040013233
dea31012005-04-17 16:05:31 -050013234 error = pci_register_driver(&lpfc_driver);
James Smart92d7f7b2007-06-17 19:56:38 -050013235 if (error) {
dea31012005-04-17 16:05:31 -050013236 fc_release_transport(lpfc_transport_template);
James Smart458c0832016-07-06 12:36:07 -070013237 fc_release_transport(lpfc_vport_transport_template);
James Smart92d7f7b2007-06-17 19:56:38 -050013238 }
dea31012005-04-17 16:05:31 -050013239
13240 return error;
13241}
13242
James Smarte59058c2008-08-24 21:49:00 -040013243/**
James Smart3621a712009-04-06 18:47:14 -040013244 * lpfc_exit - lpfc module removal routine
James Smarte59058c2008-08-24 21:49:00 -040013245 *
13246 * This routine is invoked when the lpfc module is removed from the kernel.
13247 * The special kernel macro module_exit() is used to indicate the role of
13248 * this routine to the kernel as lpfc module exit point.
13249 */
dea31012005-04-17 16:05:31 -050013250static void __exit
13251lpfc_exit(void)
13252{
James Smart3ef6d242012-01-18 16:23:48 -050013253 misc_deregister(&lpfc_mgmt_dev);
dea31012005-04-17 16:05:31 -050013254 pci_unregister_driver(&lpfc_driver);
13255 fc_release_transport(lpfc_transport_template);
James Smart458c0832016-07-06 12:36:07 -070013256 fc_release_transport(lpfc_vport_transport_template);
James Smart81301a92008-12-04 22:39:46 -050013257 if (_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -040013258 printk(KERN_ERR "9062 BLKGRD: freeing %lu pages for "
13259 "_dump_buf_data at 0x%p\n",
James Smart81301a92008-12-04 22:39:46 -050013260 (1L << _dump_buf_data_order), _dump_buf_data);
13261 free_pages((unsigned long)_dump_buf_data, _dump_buf_data_order);
13262 }
13263
13264 if (_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -040013265 printk(KERN_ERR "9049 BLKGRD: freeing %lu pages for "
13266 "_dump_buf_dif at 0x%p\n",
James Smart81301a92008-12-04 22:39:46 -050013267 (1L << _dump_buf_dif_order), _dump_buf_dif);
13268 free_pages((unsigned long)_dump_buf_dif, _dump_buf_dif_order);
13269 }
Johannes Thumshirn79739672015-08-31 16:48:11 -040013270 idr_destroy(&lpfc_hba_index);
dea31012005-04-17 16:05:31 -050013271}
13272
13273module_init(lpfc_init);
13274module_exit(lpfc_exit);
13275MODULE_LICENSE("GPL");
13276MODULE_DESCRIPTION(LPFC_MODULE_DESC);
James Smartd080abe2017-02-12 13:52:39 -080013277MODULE_AUTHOR("Broadcom");
dea31012005-04-17 16:05:31 -050013278MODULE_VERSION("0:" LPFC_DRIVER_VERSION);