blob: 563b6009ffced629f051f717d39ad7aadffd54de [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 Smart128bdda2018-01-30 15:59:03 -08004 * Copyright (C) 2017-2018 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) {
1089 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 }
1120
1121 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
1122 spin_lock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
1123 list_splice_init(&phba->sli4_hba.lpfc_abts_nvmet_ctx_list,
1124 &nvmet_aborts);
1125 spin_unlock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
James Smart86c67372017-04-21 16:05:04 -07001126 list_for_each_entry_safe(ctxp, ctxp_next, &nvmet_aborts, list) {
1127 ctxp->flag &= ~(LPFC_NVMET_XBUSY | LPFC_NVMET_ABORT_OP);
James Smart6c621a22017-05-15 15:20:45 -07001128 lpfc_nvmet_ctxbuf_post(phba, ctxp->ctxbuf);
James Smart86c67372017-04-21 16:05:04 -07001129 }
James Smart895427b2017-02-12 13:52:30 -08001130 }
James Smart895427b2017-02-12 13:52:30 -08001131
James Smart5e5b5112019-01-28 11:14:22 -08001132 spin_unlock_irq(&phba->hbalock);
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
1344/**
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 }
1374
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 */
1847 if (pci_channel_offline(phba->pcidev))
1848 return;
James Smartda0436e2009-05-22 14:51:39 -04001849
James Smart65791f12016-07-06 12:35:56 -07001850 memset(&portsmphr_reg, 0, sizeof(portsmphr_reg));
James Smart2fcee4b2010-12-15 17:57:46 -05001851 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
1852 switch (if_type) {
1853 case LPFC_SLI_INTF_IF_TYPE_0:
James Smart2e90f4b2011-12-13 13:22:37 -05001854 pci_rd_rc1 = lpfc_readl(
1855 phba->sli4_hba.u.if_type0.UERRLOregaddr,
1856 &uerrlo_reg);
1857 pci_rd_rc2 = lpfc_readl(
1858 phba->sli4_hba.u.if_type0.UEMASKLOregaddr,
1859 &uemasklo_reg);
1860 /* consider PCI bus read error as pci_channel_offline */
1861 if (pci_rd_rc1 == -EIO && pci_rd_rc2 == -EIO)
1862 return;
James Smart65791f12016-07-06 12:35:56 -07001863 if (!(phba->hba_flag & HBA_RECOVERABLE_UE)) {
1864 lpfc_sli4_offline_eratt(phba);
1865 return;
1866 }
1867 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1868 "7623 Checking UE recoverable");
1869
1870 for (i = 0; i < phba->sli4_hba.ue_to_sr / 1000; i++) {
1871 if (lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
1872 &portsmphr_reg.word0))
1873 continue;
1874
1875 smphr_port_status = bf_get(lpfc_port_smphr_port_status,
1876 &portsmphr_reg);
1877 if ((smphr_port_status & LPFC_PORT_SEM_MASK) ==
1878 LPFC_PORT_SEM_UE_RECOVERABLE)
1879 break;
1880 /*Sleep for 1Sec, before checking SEMAPHORE */
1881 msleep(1000);
1882 }
1883
1884 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1885 "4827 smphr_port_status x%x : Waited %dSec",
1886 smphr_port_status, i);
1887
1888 /* Recoverable UE, reset the HBA device */
1889 if ((smphr_port_status & LPFC_PORT_SEM_MASK) ==
1890 LPFC_PORT_SEM_UE_RECOVERABLE) {
1891 for (i = 0; i < 20; i++) {
1892 msleep(1000);
1893 if (!lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
1894 &portsmphr_reg.word0) &&
1895 (LPFC_POST_STAGE_PORT_READY ==
1896 bf_get(lpfc_port_smphr_port_status,
1897 &portsmphr_reg))) {
1898 rc = lpfc_sli4_port_sta_fn_reset(phba,
1899 LPFC_MBX_NO_WAIT, en_rn_msg);
1900 if (rc == 0)
1901 return;
1902 lpfc_printf_log(phba,
1903 KERN_ERR, LOG_INIT,
1904 "4215 Failed to recover UE");
1905 break;
1906 }
1907 }
1908 }
1909 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1910 "7624 Firmware not ready: Failing UE recovery,"
1911 " waited %dSec", i);
James Smart2fcee4b2010-12-15 17:57:46 -05001912 lpfc_sli4_offline_eratt(phba);
1913 break;
James Smart946727d2015-04-07 15:07:09 -04001914
James Smart2fcee4b2010-12-15 17:57:46 -05001915 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart27d6ac02018-02-22 08:18:42 -08001916 case LPFC_SLI_INTF_IF_TYPE_6:
James Smart2e90f4b2011-12-13 13:22:37 -05001917 pci_rd_rc1 = lpfc_readl(
1918 phba->sli4_hba.u.if_type2.STATUSregaddr,
1919 &portstat_reg.word0);
1920 /* consider PCI bus read error as pci_channel_offline */
James Smart6b5151f2012-01-18 16:24:06 -05001921 if (pci_rd_rc1 == -EIO) {
1922 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1923 "3151 PCI bus read access failure: x%x\n",
1924 readl(phba->sli4_hba.u.if_type2.STATUSregaddr));
James Smart2e90f4b2011-12-13 13:22:37 -05001925 return;
James Smart6b5151f2012-01-18 16:24:06 -05001926 }
James Smart2e90f4b2011-12-13 13:22:37 -05001927 reg_err1 = readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
1928 reg_err2 = readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
James Smart2fcee4b2010-12-15 17:57:46 -05001929 if (bf_get(lpfc_sliport_status_oti, &portstat_reg)) {
James Smart2fcee4b2010-12-15 17:57:46 -05001930 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1931 "2889 Port Overtemperature event, "
James Smart946727d2015-04-07 15:07:09 -04001932 "taking port offline Data: x%x x%x\n",
1933 reg_err1, reg_err2);
1934
James Smart310429e2016-07-06 12:35:54 -07001935 phba->sfp_alarm |= LPFC_TRANSGRESSION_HIGH_TEMPERATURE;
James Smart946727d2015-04-07 15:07:09 -04001936 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
1937 temp_event_data.event_code = LPFC_CRIT_TEMP;
1938 temp_event_data.data = 0xFFFFFFFF;
1939
1940 shost = lpfc_shost_from_vport(phba->pport);
1941 fc_host_post_vendor_event(shost, fc_get_event_number(),
1942 sizeof(temp_event_data),
1943 (char *)&temp_event_data,
1944 SCSI_NL_VID_TYPE_PCI
1945 | PCI_VENDOR_ID_EMULEX);
1946
James Smart2fcee4b2010-12-15 17:57:46 -05001947 spin_lock_irq(&phba->hbalock);
1948 phba->over_temp_state = HBA_OVER_TEMP;
1949 spin_unlock_irq(&phba->hbalock);
1950 lpfc_sli4_offline_eratt(phba);
James Smart946727d2015-04-07 15:07:09 -04001951 return;
James Smart2fcee4b2010-12-15 17:57:46 -05001952 }
James Smart2e90f4b2011-12-13 13:22:37 -05001953 if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
James Smarte10b2022014-02-20 09:57:43 -05001954 reg_err2 == SLIPORT_ERR2_REG_FW_RESTART) {
James Smart2e90f4b2011-12-13 13:22:37 -05001955 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte10b2022014-02-20 09:57:43 -05001956 "3143 Port Down: Firmware Update "
1957 "Detected\n");
1958 en_rn_msg = false;
1959 } else if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
James Smart2e90f4b2011-12-13 13:22:37 -05001960 reg_err2 == SLIPORT_ERR2_REG_FORCED_DUMP)
1961 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1962 "3144 Port Down: Debug Dump\n");
1963 else if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1964 reg_err2 == SLIPORT_ERR2_REG_FUNC_PROVISON)
1965 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1966 "3145 Port Down: Provisioning\n");
James Smart618a5232012-06-12 13:54:36 -04001967
James Smart946727d2015-04-07 15:07:09 -04001968 /* If resets are disabled then leave the HBA alone and return */
1969 if (!phba->cfg_enable_hba_reset)
1970 return;
1971
James Smart618a5232012-06-12 13:54:36 -04001972 /* Check port status register for function reset */
James Smarte10b2022014-02-20 09:57:43 -05001973 rc = lpfc_sli4_port_sta_fn_reset(phba, LPFC_MBX_NO_WAIT,
1974 en_rn_msg);
James Smart618a5232012-06-12 13:54:36 -04001975 if (rc == 0) {
1976 /* don't report event on forced debug dump */
1977 if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1978 reg_err2 == SLIPORT_ERR2_REG_FORCED_DUMP)
1979 return;
1980 else
1981 break;
James Smart2fcee4b2010-12-15 17:57:46 -05001982 }
James Smart618a5232012-06-12 13:54:36 -04001983 /* fall through for not able to recover */
James Smart6b5151f2012-01-18 16:24:06 -05001984 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1985 "3152 Unrecoverable error, bring the port "
1986 "offline\n");
James Smart2fcee4b2010-12-15 17:57:46 -05001987 lpfc_sli4_offline_eratt(phba);
1988 break;
1989 case LPFC_SLI_INTF_IF_TYPE_1:
1990 default:
1991 break;
1992 }
James Smart2e90f4b2011-12-13 13:22:37 -05001993 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
1994 "3123 Report dump event to upper layer\n");
1995 /* Send an internal error event to mgmt application */
1996 lpfc_board_errevt_to_mgmt(phba);
1997
1998 event_data = FC_REG_DUMP_EVENT;
1999 shost = lpfc_shost_from_vport(vport);
2000 fc_host_post_vendor_event(shost, fc_get_event_number(),
2001 sizeof(event_data), (char *) &event_data,
2002 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
James Smartda0436e2009-05-22 14:51:39 -04002003}
2004
2005/**
2006 * lpfc_handle_eratt - Wrapper func for handling hba error attention
2007 * @phba: pointer to lpfc HBA data structure.
2008 *
2009 * This routine wraps the actual SLI3 or SLI4 hba error attention handling
2010 * routine from the API jump table function pointer from the lpfc_hba struct.
2011 *
2012 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002013 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -04002014 * Any other value - error.
2015 **/
2016void
2017lpfc_handle_eratt(struct lpfc_hba *phba)
2018{
2019 (*phba->lpfc_handle_eratt)(phba);
2020}
2021
2022/**
James Smart3621a712009-04-06 18:47:14 -04002023 * lpfc_handle_latt - The HBA link event handler
James Smarte59058c2008-08-24 21:49:00 -04002024 * @phba: pointer to lpfc hba data structure.
2025 *
2026 * This routine is invoked from the worker thread to handle a HBA host
James Smart895427b2017-02-12 13:52:30 -08002027 * attention link event. SLI3 only.
James Smarte59058c2008-08-24 21:49:00 -04002028 **/
dea31012005-04-17 16:05:31 -05002029void
James Smart2e0fef82007-06-17 19:56:36 -05002030lpfc_handle_latt(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002031{
James Smart2e0fef82007-06-17 19:56:36 -05002032 struct lpfc_vport *vport = phba->pport;
2033 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05002034 LPFC_MBOXQ_t *pmb;
2035 volatile uint32_t control;
2036 struct lpfc_dmabuf *mp;
James Smart09372822008-01-11 01:52:54 -05002037 int rc = 0;
dea31012005-04-17 16:05:31 -05002038
2039 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart09372822008-01-11 01:52:54 -05002040 if (!pmb) {
2041 rc = 1;
dea31012005-04-17 16:05:31 -05002042 goto lpfc_handle_latt_err_exit;
James Smart09372822008-01-11 01:52:54 -05002043 }
dea31012005-04-17 16:05:31 -05002044
2045 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
James Smart09372822008-01-11 01:52:54 -05002046 if (!mp) {
2047 rc = 2;
dea31012005-04-17 16:05:31 -05002048 goto lpfc_handle_latt_free_pmb;
James Smart09372822008-01-11 01:52:54 -05002049 }
dea31012005-04-17 16:05:31 -05002050
2051 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
James Smart09372822008-01-11 01:52:54 -05002052 if (!mp->virt) {
2053 rc = 3;
dea31012005-04-17 16:05:31 -05002054 goto lpfc_handle_latt_free_mp;
James Smart09372822008-01-11 01:52:54 -05002055 }
dea31012005-04-17 16:05:31 -05002056
James.Smart@Emulex.Com6281bfe2005-11-28 11:41:33 -05002057 /* Cleanup any outstanding ELS commands */
James Smart549e55c2007-08-02 11:09:51 -04002058 lpfc_els_flush_all_cmd(phba);
dea31012005-04-17 16:05:31 -05002059
2060 psli->slistat.link_event++;
James Smart76a95d72010-11-20 23:11:48 -05002061 lpfc_read_topology(phba, pmb, mp);
2062 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
James Smart2e0fef82007-06-17 19:56:36 -05002063 pmb->vport = vport;
James Smart0d2b6b82008-06-14 22:52:47 -04002064 /* Block ELS IOCBs until we have processed this mbox command */
James Smart895427b2017-02-12 13:52:30 -08002065 phba->sli.sli3_ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
James Smart0b727fe2007-10-27 13:37:25 -04002066 rc = lpfc_sli_issue_mbox (phba, pmb, MBX_NOWAIT);
James Smart09372822008-01-11 01:52:54 -05002067 if (rc == MBX_NOT_FINISHED) {
2068 rc = 4;
James Smart14691152006-12-02 13:34:28 -05002069 goto lpfc_handle_latt_free_mbuf;
James Smart09372822008-01-11 01:52:54 -05002070 }
dea31012005-04-17 16:05:31 -05002071
2072 /* Clear Link Attention in HA REG */
James Smart2e0fef82007-06-17 19:56:36 -05002073 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002074 writel(HA_LATT, phba->HAregaddr);
2075 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05002076 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002077
2078 return;
2079
James Smart14691152006-12-02 13:34:28 -05002080lpfc_handle_latt_free_mbuf:
James Smart895427b2017-02-12 13:52:30 -08002081 phba->sli.sli3_ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart14691152006-12-02 13:34:28 -05002082 lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea31012005-04-17 16:05:31 -05002083lpfc_handle_latt_free_mp:
2084 kfree(mp);
2085lpfc_handle_latt_free_pmb:
James Smart1dcb58e2007-04-25 09:51:30 -04002086 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002087lpfc_handle_latt_err_exit:
2088 /* Enable Link attention interrupts */
James Smart2e0fef82007-06-17 19:56:36 -05002089 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002090 psli->sli_flag |= LPFC_PROCESS_LA;
2091 control = readl(phba->HCregaddr);
2092 control |= HC_LAINT_ENA;
2093 writel(control, phba->HCregaddr);
2094 readl(phba->HCregaddr); /* flush */
2095
2096 /* Clear Link Attention in HA REG */
2097 writel(HA_LATT, phba->HAregaddr);
2098 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05002099 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002100 lpfc_linkdown(phba);
James Smart2e0fef82007-06-17 19:56:36 -05002101 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002102
James Smart09372822008-01-11 01:52:54 -05002103 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
2104 "0300 LATT: Cannot issue READ_LA: Data:%d\n", rc);
dea31012005-04-17 16:05:31 -05002105
2106 return;
2107}
2108
James Smarte59058c2008-08-24 21:49:00 -04002109/**
James Smart3621a712009-04-06 18:47:14 -04002110 * lpfc_parse_vpd - Parse VPD (Vital Product Data)
James Smarte59058c2008-08-24 21:49:00 -04002111 * @phba: pointer to lpfc hba data structure.
2112 * @vpd: pointer to the vital product data.
2113 * @len: length of the vital product data in bytes.
2114 *
2115 * This routine parses the Vital Product Data (VPD). The VPD is treated as
2116 * an array of characters. In this routine, the ModelName, ProgramType, and
2117 * ModelDesc, etc. fields of the phba data structure will be populated.
2118 *
2119 * Return codes
2120 * 0 - pointer to the VPD passed in is NULL
2121 * 1 - success
2122 **/
James Smart3772a992009-05-22 14:50:54 -04002123int
James Smart2e0fef82007-06-17 19:56:36 -05002124lpfc_parse_vpd(struct lpfc_hba *phba, uint8_t *vpd, int len)
dea31012005-04-17 16:05:31 -05002125{
2126 uint8_t lenlo, lenhi;
Anton Blanchard07da60c2007-03-21 08:41:47 -05002127 int Length;
dea31012005-04-17 16:05:31 -05002128 int i, j;
2129 int finished = 0;
2130 int index = 0;
2131
2132 if (!vpd)
2133 return 0;
2134
2135 /* Vital Product */
James Smarted957682007-06-17 19:56:37 -05002136 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002137 "0455 Vital Product Data: x%x x%x x%x x%x\n",
dea31012005-04-17 16:05:31 -05002138 (uint32_t) vpd[0], (uint32_t) vpd[1], (uint32_t) vpd[2],
2139 (uint32_t) vpd[3]);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002140 while (!finished && (index < (len - 4))) {
dea31012005-04-17 16:05:31 -05002141 switch (vpd[index]) {
2142 case 0x82:
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002143 case 0x91:
dea31012005-04-17 16:05:31 -05002144 index += 1;
2145 lenlo = vpd[index];
2146 index += 1;
2147 lenhi = vpd[index];
2148 index += 1;
2149 i = ((((unsigned short)lenhi) << 8) + lenlo);
2150 index += i;
2151 break;
2152 case 0x90:
2153 index += 1;
2154 lenlo = vpd[index];
2155 index += 1;
2156 lenhi = vpd[index];
2157 index += 1;
2158 Length = ((((unsigned short)lenhi) << 8) + lenlo);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002159 if (Length > len - index)
2160 Length = len - index;
dea31012005-04-17 16:05:31 -05002161 while (Length > 0) {
2162 /* Look for Serial Number */
2163 if ((vpd[index] == 'S') && (vpd[index+1] == 'N')) {
2164 index += 2;
2165 i = vpd[index];
2166 index += 1;
2167 j = 0;
2168 Length -= (3+i);
2169 while(i--) {
2170 phba->SerialNumber[j++] = vpd[index++];
2171 if (j == 31)
2172 break;
2173 }
2174 phba->SerialNumber[j] = 0;
2175 continue;
2176 }
2177 else if ((vpd[index] == 'V') && (vpd[index+1] == '1')) {
2178 phba->vpd_flag |= VPD_MODEL_DESC;
2179 index += 2;
2180 i = vpd[index];
2181 index += 1;
2182 j = 0;
2183 Length -= (3+i);
2184 while(i--) {
2185 phba->ModelDesc[j++] = vpd[index++];
2186 if (j == 255)
2187 break;
2188 }
2189 phba->ModelDesc[j] = 0;
2190 continue;
2191 }
2192 else if ((vpd[index] == 'V') && (vpd[index+1] == '2')) {
2193 phba->vpd_flag |= VPD_MODEL_NAME;
2194 index += 2;
2195 i = vpd[index];
2196 index += 1;
2197 j = 0;
2198 Length -= (3+i);
2199 while(i--) {
2200 phba->ModelName[j++] = vpd[index++];
2201 if (j == 79)
2202 break;
2203 }
2204 phba->ModelName[j] = 0;
2205 continue;
2206 }
2207 else if ((vpd[index] == 'V') && (vpd[index+1] == '3')) {
2208 phba->vpd_flag |= VPD_PROGRAM_TYPE;
2209 index += 2;
2210 i = vpd[index];
2211 index += 1;
2212 j = 0;
2213 Length -= (3+i);
2214 while(i--) {
2215 phba->ProgramType[j++] = vpd[index++];
2216 if (j == 255)
2217 break;
2218 }
2219 phba->ProgramType[j] = 0;
2220 continue;
2221 }
2222 else if ((vpd[index] == 'V') && (vpd[index+1] == '4')) {
2223 phba->vpd_flag |= VPD_PORT;
2224 index += 2;
2225 i = vpd[index];
2226 index += 1;
2227 j = 0;
2228 Length -= (3+i);
2229 while(i--) {
James Smartcd1c8302011-10-10 21:33:25 -04002230 if ((phba->sli_rev == LPFC_SLI_REV4) &&
2231 (phba->sli4_hba.pport_name_sta ==
2232 LPFC_SLI4_PPNAME_GET)) {
2233 j++;
2234 index++;
2235 } else
2236 phba->Port[j++] = vpd[index++];
2237 if (j == 19)
2238 break;
dea31012005-04-17 16:05:31 -05002239 }
James Smartcd1c8302011-10-10 21:33:25 -04002240 if ((phba->sli_rev != LPFC_SLI_REV4) ||
2241 (phba->sli4_hba.pport_name_sta ==
2242 LPFC_SLI4_PPNAME_NON))
2243 phba->Port[j] = 0;
dea31012005-04-17 16:05:31 -05002244 continue;
2245 }
2246 else {
2247 index += 2;
2248 i = vpd[index];
2249 index += 1;
2250 index += i;
2251 Length -= (3 + i);
2252 }
2253 }
2254 finished = 0;
2255 break;
2256 case 0x78:
2257 finished = 1;
2258 break;
2259 default:
2260 index ++;
2261 break;
2262 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002263 }
dea31012005-04-17 16:05:31 -05002264
2265 return(1);
2266}
2267
James Smarte59058c2008-08-24 21:49:00 -04002268/**
James Smart3621a712009-04-06 18:47:14 -04002269 * lpfc_get_hba_model_desc - Retrieve HBA device model name and description
James Smarte59058c2008-08-24 21:49:00 -04002270 * @phba: pointer to lpfc hba data structure.
2271 * @mdp: pointer to the data structure to hold the derived model name.
2272 * @descp: pointer to the data structure to hold the derived description.
2273 *
2274 * This routine retrieves HBA's description based on its registered PCI device
2275 * ID. The @descp passed into this function points to an array of 256 chars. It
2276 * shall be returned with the model name, maximum speed, and the host bus type.
2277 * The @mdp passed into this function points to an array of 80 chars. When the
2278 * function returns, the @mdp will be filled with the model name.
2279 **/
dea31012005-04-17 16:05:31 -05002280static void
James Smart2e0fef82007-06-17 19:56:36 -05002281lpfc_get_hba_model_desc(struct lpfc_hba *phba, uint8_t *mdp, uint8_t *descp)
dea31012005-04-17 16:05:31 -05002282{
2283 lpfc_vpd_t *vp;
James.Smart@Emulex.Comfefcb2b2005-11-28 15:08:56 -05002284 uint16_t dev_id = phba->pcidev->device;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002285 int max_speed;
James Smart84774a42008-08-24 21:50:06 -04002286 int GE = 0;
James Smartda0436e2009-05-22 14:51:39 -04002287 int oneConnect = 0; /* default is not a oneConnect */
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002288 struct {
James Smarta747c9c2009-11-18 15:41:10 -05002289 char *name;
2290 char *bus;
2291 char *function;
2292 } m = {"<Unknown>", "", ""};
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002293
2294 if (mdp && mdp[0] != '\0'
2295 && descp && descp[0] != '\0')
2296 return;
2297
James Smartfbd8a6b2018-02-22 08:18:45 -08002298 if (phba->lmt & LMT_64Gb)
2299 max_speed = 64;
2300 else if (phba->lmt & LMT_32Gb)
James Smartd38dd522015-08-31 16:48:17 -04002301 max_speed = 32;
2302 else if (phba->lmt & LMT_16Gb)
James Smartc0c11512011-05-24 11:41:34 -04002303 max_speed = 16;
2304 else if (phba->lmt & LMT_10Gb)
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002305 max_speed = 10;
2306 else if (phba->lmt & LMT_8Gb)
2307 max_speed = 8;
2308 else if (phba->lmt & LMT_4Gb)
2309 max_speed = 4;
2310 else if (phba->lmt & LMT_2Gb)
2311 max_speed = 2;
James Smart4169d862012-09-29 11:32:09 -04002312 else if (phba->lmt & LMT_1Gb)
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002313 max_speed = 1;
James Smart4169d862012-09-29 11:32:09 -04002314 else
2315 max_speed = 0;
dea31012005-04-17 16:05:31 -05002316
2317 vp = &phba->vpd;
dea31012005-04-17 16:05:31 -05002318
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002319 switch (dev_id) {
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04002320 case PCI_DEVICE_ID_FIREFLY:
James Smart12222f42014-05-21 08:05:19 -04002321 m = (typeof(m)){"LP6000", "PCI",
2322 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04002323 break;
dea31012005-04-17 16:05:31 -05002324 case PCI_DEVICE_ID_SUPERFLY:
2325 if (vp->rev.biuRev >= 1 && vp->rev.biuRev <= 3)
James Smart12222f42014-05-21 08:05:19 -04002326 m = (typeof(m)){"LP7000", "PCI", ""};
dea31012005-04-17 16:05:31 -05002327 else
James Smart12222f42014-05-21 08:05:19 -04002328 m = (typeof(m)){"LP7000E", "PCI", ""};
2329 m.function = "Obsolete, Unsupported Fibre Channel Adapter";
dea31012005-04-17 16:05:31 -05002330 break;
2331 case PCI_DEVICE_ID_DRAGONFLY:
James Smarta747c9c2009-11-18 15:41:10 -05002332 m = (typeof(m)){"LP8000", "PCI",
James Smart12222f42014-05-21 08:05:19 -04002333 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002334 break;
2335 case PCI_DEVICE_ID_CENTAUR:
2336 if (FC_JEDEC_ID(vp->rev.biuRev) == CENTAUR_2G_JEDEC_ID)
James Smart12222f42014-05-21 08:05:19 -04002337 m = (typeof(m)){"LP9002", "PCI", ""};
dea31012005-04-17 16:05:31 -05002338 else
James Smart12222f42014-05-21 08:05:19 -04002339 m = (typeof(m)){"LP9000", "PCI", ""};
2340 m.function = "Obsolete, Unsupported Fibre Channel Adapter";
dea31012005-04-17 16:05:31 -05002341 break;
2342 case PCI_DEVICE_ID_RFLY:
James Smarta747c9c2009-11-18 15:41:10 -05002343 m = (typeof(m)){"LP952", "PCI",
James Smart12222f42014-05-21 08:05:19 -04002344 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002345 break;
2346 case PCI_DEVICE_ID_PEGASUS:
James Smarta747c9c2009-11-18 15:41:10 -05002347 m = (typeof(m)){"LP9802", "PCI-X",
James Smart12222f42014-05-21 08:05:19 -04002348 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002349 break;
2350 case PCI_DEVICE_ID_THOR:
James Smarta747c9c2009-11-18 15:41:10 -05002351 m = (typeof(m)){"LP10000", "PCI-X",
James Smart12222f42014-05-21 08:05:19 -04002352 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002353 break;
2354 case PCI_DEVICE_ID_VIPER:
James Smarta747c9c2009-11-18 15:41:10 -05002355 m = (typeof(m)){"LPX1000", "PCI-X",
James Smart12222f42014-05-21 08:05:19 -04002356 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002357 break;
2358 case PCI_DEVICE_ID_PFLY:
James Smarta747c9c2009-11-18 15:41:10 -05002359 m = (typeof(m)){"LP982", "PCI-X",
James Smart12222f42014-05-21 08:05:19 -04002360 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002361 break;
2362 case PCI_DEVICE_ID_TFLY:
James Smarta747c9c2009-11-18 15:41:10 -05002363 m = (typeof(m)){"LP1050", "PCI-X",
James Smart12222f42014-05-21 08:05:19 -04002364 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002365 break;
2366 case PCI_DEVICE_ID_HELIOS:
James Smarta747c9c2009-11-18 15:41:10 -05002367 m = (typeof(m)){"LP11000", "PCI-X2",
James Smart12222f42014-05-21 08:05:19 -04002368 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002369 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002370 case PCI_DEVICE_ID_HELIOS_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002371 m = (typeof(m)){"LP11000-SP", "PCI-X2",
James Smart12222f42014-05-21 08:05:19 -04002372 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002373 break;
2374 case PCI_DEVICE_ID_HELIOS_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002375 m = (typeof(m)){"LP11002-SP", "PCI-X2",
James Smart12222f42014-05-21 08:05:19 -04002376 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002377 break;
2378 case PCI_DEVICE_ID_NEPTUNE:
James Smart12222f42014-05-21 08:05:19 -04002379 m = (typeof(m)){"LPe1000", "PCIe",
2380 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002381 break;
2382 case PCI_DEVICE_ID_NEPTUNE_SCSP:
James Smart12222f42014-05-21 08:05:19 -04002383 m = (typeof(m)){"LPe1000-SP", "PCIe",
2384 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002385 break;
2386 case PCI_DEVICE_ID_NEPTUNE_DCSP:
James Smart12222f42014-05-21 08:05:19 -04002387 m = (typeof(m)){"LPe1002-SP", "PCIe",
2388 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002389 break;
dea31012005-04-17 16:05:31 -05002390 case PCI_DEVICE_ID_BMID:
James Smarta747c9c2009-11-18 15:41:10 -05002391 m = (typeof(m)){"LP1150", "PCI-X2", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002392 break;
2393 case PCI_DEVICE_ID_BSMB:
James Smart12222f42014-05-21 08:05:19 -04002394 m = (typeof(m)){"LP111", "PCI-X2",
2395 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002396 break;
2397 case PCI_DEVICE_ID_ZEPHYR:
James Smarta747c9c2009-11-18 15:41:10 -05002398 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002399 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002400 case PCI_DEVICE_ID_ZEPHYR_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002401 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002402 break;
2403 case PCI_DEVICE_ID_ZEPHYR_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002404 m = (typeof(m)){"LP2105", "PCIe", "FCoE Adapter"};
James Smarta257bf92009-04-06 18:48:10 -04002405 GE = 1;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002406 break;
dea31012005-04-17 16:05:31 -05002407 case PCI_DEVICE_ID_ZMID:
James Smarta747c9c2009-11-18 15:41:10 -05002408 m = (typeof(m)){"LPe1150", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002409 break;
2410 case PCI_DEVICE_ID_ZSMB:
James Smarta747c9c2009-11-18 15:41:10 -05002411 m = (typeof(m)){"LPe111", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002412 break;
2413 case PCI_DEVICE_ID_LP101:
James Smart12222f42014-05-21 08:05:19 -04002414 m = (typeof(m)){"LP101", "PCI-X",
2415 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002416 break;
2417 case PCI_DEVICE_ID_LP10000S:
James Smart12222f42014-05-21 08:05:19 -04002418 m = (typeof(m)){"LP10000-S", "PCI",
2419 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04002420 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002421 case PCI_DEVICE_ID_LP11000S:
James Smart12222f42014-05-21 08:05:19 -04002422 m = (typeof(m)){"LP11000-S", "PCI-X2",
2423 "Obsolete, Unsupported Fibre Channel Adapter"};
James Smart18a3b592006-12-02 13:35:08 -05002424 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002425 case PCI_DEVICE_ID_LPE11000S:
James Smart12222f42014-05-21 08:05:19 -04002426 m = (typeof(m)){"LPe11000-S", "PCIe",
2427 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Com5cc36b32005-11-28 11:42:19 -05002428 break;
James Smartb87eab32007-04-25 09:53:28 -04002429 case PCI_DEVICE_ID_SAT:
James Smarta747c9c2009-11-18 15:41:10 -05002430 m = (typeof(m)){"LPe12000", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002431 break;
2432 case PCI_DEVICE_ID_SAT_MID:
James Smarta747c9c2009-11-18 15:41:10 -05002433 m = (typeof(m)){"LPe1250", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002434 break;
2435 case PCI_DEVICE_ID_SAT_SMB:
James Smarta747c9c2009-11-18 15:41:10 -05002436 m = (typeof(m)){"LPe121", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002437 break;
2438 case PCI_DEVICE_ID_SAT_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002439 m = (typeof(m)){"LPe12002-SP", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002440 break;
2441 case PCI_DEVICE_ID_SAT_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002442 m = (typeof(m)){"LPe12000-SP", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002443 break;
2444 case PCI_DEVICE_ID_SAT_S:
James Smarta747c9c2009-11-18 15:41:10 -05002445 m = (typeof(m)){"LPe12000-S", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002446 break;
James Smart84774a42008-08-24 21:50:06 -04002447 case PCI_DEVICE_ID_HORNET:
James Smart12222f42014-05-21 08:05:19 -04002448 m = (typeof(m)){"LP21000", "PCIe",
2449 "Obsolete, Unsupported FCoE Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002450 GE = 1;
2451 break;
2452 case PCI_DEVICE_ID_PROTEUS_VF:
James Smarta747c9c2009-11-18 15:41:10 -05002453 m = (typeof(m)){"LPev12000", "PCIe IOV",
James Smart12222f42014-05-21 08:05:19 -04002454 "Obsolete, Unsupported Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002455 break;
2456 case PCI_DEVICE_ID_PROTEUS_PF:
James Smarta747c9c2009-11-18 15:41:10 -05002457 m = (typeof(m)){"LPev12000", "PCIe IOV",
James Smart12222f42014-05-21 08:05:19 -04002458 "Obsolete, Unsupported Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002459 break;
2460 case PCI_DEVICE_ID_PROTEUS_S:
James Smarta747c9c2009-11-18 15:41:10 -05002461 m = (typeof(m)){"LPemv12002-S", "PCIe IOV",
James Smart12222f42014-05-21 08:05:19 -04002462 "Obsolete, Unsupported Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002463 break;
James Smartda0436e2009-05-22 14:51:39 -04002464 case PCI_DEVICE_ID_TIGERSHARK:
2465 oneConnect = 1;
James Smarta747c9c2009-11-18 15:41:10 -05002466 m = (typeof(m)){"OCe10100", "PCIe", "FCoE"};
James Smartda0436e2009-05-22 14:51:39 -04002467 break;
James Smarta747c9c2009-11-18 15:41:10 -05002468 case PCI_DEVICE_ID_TOMCAT:
James Smart6669f9b2009-10-02 15:16:45 -04002469 oneConnect = 1;
James Smarta747c9c2009-11-18 15:41:10 -05002470 m = (typeof(m)){"OCe11100", "PCIe", "FCoE"};
2471 break;
2472 case PCI_DEVICE_ID_FALCON:
2473 m = (typeof(m)){"LPSe12002-ML1-E", "PCIe",
2474 "EmulexSecure Fibre"};
James Smart6669f9b2009-10-02 15:16:45 -04002475 break;
James Smart98fc5dd2010-06-07 15:24:29 -04002476 case PCI_DEVICE_ID_BALIUS:
2477 m = (typeof(m)){"LPVe12002", "PCIe Shared I/O",
James Smart12222f42014-05-21 08:05:19 -04002478 "Obsolete, Unsupported Fibre Channel Adapter"};
James Smart98fc5dd2010-06-07 15:24:29 -04002479 break;
James Smart085c6472010-11-20 23:11:37 -05002480 case PCI_DEVICE_ID_LANCER_FC:
James Smartc0c11512011-05-24 11:41:34 -04002481 m = (typeof(m)){"LPe16000", "PCIe", "Fibre Channel Adapter"};
James Smart085c6472010-11-20 23:11:37 -05002482 break;
James Smart12222f42014-05-21 08:05:19 -04002483 case PCI_DEVICE_ID_LANCER_FC_VF:
2484 m = (typeof(m)){"LPe16000", "PCIe",
2485 "Obsolete, Unsupported Fibre Channel Adapter"};
2486 break;
James Smart085c6472010-11-20 23:11:37 -05002487 case PCI_DEVICE_ID_LANCER_FCOE:
2488 oneConnect = 1;
James Smart079b5c92011-08-21 21:48:49 -04002489 m = (typeof(m)){"OCe15100", "PCIe", "FCoE"};
James Smart085c6472010-11-20 23:11:37 -05002490 break;
James Smart12222f42014-05-21 08:05:19 -04002491 case PCI_DEVICE_ID_LANCER_FCOE_VF:
2492 oneConnect = 1;
2493 m = (typeof(m)){"OCe15100", "PCIe",
2494 "Obsolete, Unsupported FCoE"};
2495 break;
James Smartd38dd522015-08-31 16:48:17 -04002496 case PCI_DEVICE_ID_LANCER_G6_FC:
2497 m = (typeof(m)){"LPe32000", "PCIe", "Fibre Channel Adapter"};
2498 break;
James Smartc238b9b2018-02-22 08:18:44 -08002499 case PCI_DEVICE_ID_LANCER_G7_FC:
2500 m = (typeof(m)){"LPe36000", "PCIe", "Fibre Channel Adapter"};
2501 break;
James Smartf8cafd32012-08-03 12:42:51 -04002502 case PCI_DEVICE_ID_SKYHAWK:
2503 case PCI_DEVICE_ID_SKYHAWK_VF:
2504 oneConnect = 1;
2505 m = (typeof(m)){"OCe14000", "PCIe", "FCoE"};
2506 break;
James.Smart@Emulex.Com5cc36b32005-11-28 11:42:19 -05002507 default:
James Smarta747c9c2009-11-18 15:41:10 -05002508 m = (typeof(m)){"Unknown", "", ""};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002509 break;
dea31012005-04-17 16:05:31 -05002510 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002511
2512 if (mdp && mdp[0] == '\0')
2513 snprintf(mdp, 79,"%s", m.name);
James Smartc0c11512011-05-24 11:41:34 -04002514 /*
2515 * oneConnect hba requires special processing, they are all initiators
James Smartda0436e2009-05-22 14:51:39 -04002516 * and we put the port number on the end
2517 */
2518 if (descp && descp[0] == '\0') {
2519 if (oneConnect)
2520 snprintf(descp, 255,
James Smart4169d862012-09-29 11:32:09 -04002521 "Emulex OneConnect %s, %s Initiator %s",
James Smarta747c9c2009-11-18 15:41:10 -05002522 m.name, m.function,
James Smartda0436e2009-05-22 14:51:39 -04002523 phba->Port);
James Smart4169d862012-09-29 11:32:09 -04002524 else if (max_speed == 0)
2525 snprintf(descp, 255,
Sebastian Herbszt290237d2015-08-31 16:48:08 -04002526 "Emulex %s %s %s",
James Smart4169d862012-09-29 11:32:09 -04002527 m.name, m.bus, m.function);
James Smartda0436e2009-05-22 14:51:39 -04002528 else
2529 snprintf(descp, 255,
2530 "Emulex %s %d%s %s %s",
James Smarta747c9c2009-11-18 15:41:10 -05002531 m.name, max_speed, (GE) ? "GE" : "Gb",
2532 m.bus, m.function);
James Smartda0436e2009-05-22 14:51:39 -04002533 }
dea31012005-04-17 16:05:31 -05002534}
2535
James Smarte59058c2008-08-24 21:49:00 -04002536/**
James Smart3621a712009-04-06 18:47:14 -04002537 * lpfc_post_buffer - Post IOCB(s) with DMA buffer descriptor(s) to a IOCB ring
James Smarte59058c2008-08-24 21:49:00 -04002538 * @phba: pointer to lpfc hba data structure.
2539 * @pring: pointer to a IOCB ring.
2540 * @cnt: the number of IOCBs to be posted to the IOCB ring.
2541 *
2542 * This routine posts a given number of IOCBs with the associated DMA buffer
2543 * descriptors specified by the cnt argument to the given IOCB ring.
2544 *
2545 * Return codes
2546 * The number of IOCBs NOT able to be posted to the IOCB ring.
2547 **/
dea31012005-04-17 16:05:31 -05002548int
James Smart495a7142008-06-14 22:52:59 -04002549lpfc_post_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring, int cnt)
dea31012005-04-17 16:05:31 -05002550{
2551 IOCB_t *icmd;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002552 struct lpfc_iocbq *iocb;
dea31012005-04-17 16:05:31 -05002553 struct lpfc_dmabuf *mp1, *mp2;
2554
2555 cnt += pring->missbufcnt;
2556
2557 /* While there are buffers to post */
2558 while (cnt > 0) {
2559 /* Allocate buffer for command iocb */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002560 iocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05002561 if (iocb == NULL) {
2562 pring->missbufcnt = cnt;
2563 return cnt;
2564 }
dea31012005-04-17 16:05:31 -05002565 icmd = &iocb->iocb;
2566
2567 /* 2 buffers can be posted per command */
2568 /* Allocate buffer to post */
2569 mp1 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
2570 if (mp1)
James Smart98c9ea52007-10-27 13:37:33 -04002571 mp1->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &mp1->phys);
2572 if (!mp1 || !mp1->virt) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002573 kfree(mp1);
James Bottomley604a3e32005-10-29 10:28:33 -05002574 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002575 pring->missbufcnt = cnt;
2576 return cnt;
2577 }
2578
2579 INIT_LIST_HEAD(&mp1->list);
2580 /* Allocate buffer to post */
2581 if (cnt > 1) {
2582 mp2 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
2583 if (mp2)
2584 mp2->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
2585 &mp2->phys);
James Smart98c9ea52007-10-27 13:37:33 -04002586 if (!mp2 || !mp2->virt) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002587 kfree(mp2);
dea31012005-04-17 16:05:31 -05002588 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
2589 kfree(mp1);
James Bottomley604a3e32005-10-29 10:28:33 -05002590 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002591 pring->missbufcnt = cnt;
2592 return cnt;
2593 }
2594
2595 INIT_LIST_HEAD(&mp2->list);
2596 } else {
2597 mp2 = NULL;
2598 }
2599
2600 icmd->un.cont64[0].addrHigh = putPaddrHigh(mp1->phys);
2601 icmd->un.cont64[0].addrLow = putPaddrLow(mp1->phys);
2602 icmd->un.cont64[0].tus.f.bdeSize = FCELSSIZE;
2603 icmd->ulpBdeCount = 1;
2604 cnt--;
2605 if (mp2) {
2606 icmd->un.cont64[1].addrHigh = putPaddrHigh(mp2->phys);
2607 icmd->un.cont64[1].addrLow = putPaddrLow(mp2->phys);
2608 icmd->un.cont64[1].tus.f.bdeSize = FCELSSIZE;
2609 cnt--;
2610 icmd->ulpBdeCount = 2;
2611 }
2612
2613 icmd->ulpCommand = CMD_QUE_RING_BUF64_CN;
2614 icmd->ulpLe = 1;
2615
James Smart3772a992009-05-22 14:50:54 -04002616 if (lpfc_sli_issue_iocb(phba, pring->ringno, iocb, 0) ==
2617 IOCB_ERROR) {
dea31012005-04-17 16:05:31 -05002618 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
2619 kfree(mp1);
2620 cnt++;
2621 if (mp2) {
2622 lpfc_mbuf_free(phba, mp2->virt, mp2->phys);
2623 kfree(mp2);
2624 cnt++;
2625 }
James Bottomley604a3e32005-10-29 10:28:33 -05002626 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002627 pring->missbufcnt = cnt;
dea31012005-04-17 16:05:31 -05002628 return cnt;
2629 }
dea31012005-04-17 16:05:31 -05002630 lpfc_sli_ringpostbuf_put(phba, pring, mp1);
James Smart92d7f7b2007-06-17 19:56:38 -05002631 if (mp2)
dea31012005-04-17 16:05:31 -05002632 lpfc_sli_ringpostbuf_put(phba, pring, mp2);
dea31012005-04-17 16:05:31 -05002633 }
2634 pring->missbufcnt = 0;
2635 return 0;
2636}
2637
James Smarte59058c2008-08-24 21:49:00 -04002638/**
James Smart3621a712009-04-06 18:47:14 -04002639 * lpfc_post_rcv_buf - Post the initial receive IOCB buffers to ELS ring
James Smarte59058c2008-08-24 21:49:00 -04002640 * @phba: pointer to lpfc hba data structure.
2641 *
2642 * This routine posts initial receive IOCB buffers to the ELS ring. The
2643 * current number of initial IOCB buffers specified by LPFC_BUF_RING0 is
James Smart895427b2017-02-12 13:52:30 -08002644 * set to 64 IOCBs. SLI3 only.
James Smarte59058c2008-08-24 21:49:00 -04002645 *
2646 * Return codes
2647 * 0 - success (currently always success)
2648 **/
dea31012005-04-17 16:05:31 -05002649static int
James Smart2e0fef82007-06-17 19:56:36 -05002650lpfc_post_rcv_buf(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002651{
2652 struct lpfc_sli *psli = &phba->sli;
2653
2654 /* Ring 0, ELS / CT buffers */
James Smart895427b2017-02-12 13:52:30 -08002655 lpfc_post_buffer(phba, &psli->sli3_ring[LPFC_ELS_RING], LPFC_BUF_RING0);
dea31012005-04-17 16:05:31 -05002656 /* Ring 2 - FCP no buffers needed */
2657
2658 return 0;
2659}
2660
2661#define S(N,V) (((V)<<(N))|((V)>>(32-(N))))
2662
James Smarte59058c2008-08-24 21:49:00 -04002663/**
James Smart3621a712009-04-06 18:47:14 -04002664 * lpfc_sha_init - Set up initial array of hash table entries
James Smarte59058c2008-08-24 21:49:00 -04002665 * @HashResultPointer: pointer to an array as hash table.
2666 *
2667 * This routine sets up the initial values to the array of hash table entries
2668 * for the LC HBAs.
2669 **/
dea31012005-04-17 16:05:31 -05002670static void
2671lpfc_sha_init(uint32_t * HashResultPointer)
2672{
2673 HashResultPointer[0] = 0x67452301;
2674 HashResultPointer[1] = 0xEFCDAB89;
2675 HashResultPointer[2] = 0x98BADCFE;
2676 HashResultPointer[3] = 0x10325476;
2677 HashResultPointer[4] = 0xC3D2E1F0;
2678}
2679
James Smarte59058c2008-08-24 21:49:00 -04002680/**
James Smart3621a712009-04-06 18:47:14 -04002681 * lpfc_sha_iterate - Iterate initial hash table with the working hash table
James Smarte59058c2008-08-24 21:49:00 -04002682 * @HashResultPointer: pointer to an initial/result hash table.
2683 * @HashWorkingPointer: pointer to an working hash table.
2684 *
2685 * This routine iterates an initial hash table pointed by @HashResultPointer
2686 * with the values from the working hash table pointeed by @HashWorkingPointer.
2687 * The results are putting back to the initial hash table, returned through
2688 * the @HashResultPointer as the result hash table.
2689 **/
dea31012005-04-17 16:05:31 -05002690static void
2691lpfc_sha_iterate(uint32_t * HashResultPointer, uint32_t * HashWorkingPointer)
2692{
2693 int t;
2694 uint32_t TEMP;
2695 uint32_t A, B, C, D, E;
2696 t = 16;
2697 do {
2698 HashWorkingPointer[t] =
2699 S(1,
2700 HashWorkingPointer[t - 3] ^ HashWorkingPointer[t -
2701 8] ^
2702 HashWorkingPointer[t - 14] ^ HashWorkingPointer[t - 16]);
2703 } while (++t <= 79);
2704 t = 0;
2705 A = HashResultPointer[0];
2706 B = HashResultPointer[1];
2707 C = HashResultPointer[2];
2708 D = HashResultPointer[3];
2709 E = HashResultPointer[4];
2710
2711 do {
2712 if (t < 20) {
2713 TEMP = ((B & C) | ((~B) & D)) + 0x5A827999;
2714 } else if (t < 40) {
2715 TEMP = (B ^ C ^ D) + 0x6ED9EBA1;
2716 } else if (t < 60) {
2717 TEMP = ((B & C) | (B & D) | (C & D)) + 0x8F1BBCDC;
2718 } else {
2719 TEMP = (B ^ C ^ D) + 0xCA62C1D6;
2720 }
2721 TEMP += S(5, A) + E + HashWorkingPointer[t];
2722 E = D;
2723 D = C;
2724 C = S(30, B);
2725 B = A;
2726 A = TEMP;
2727 } while (++t <= 79);
2728
2729 HashResultPointer[0] += A;
2730 HashResultPointer[1] += B;
2731 HashResultPointer[2] += C;
2732 HashResultPointer[3] += D;
2733 HashResultPointer[4] += E;
2734
2735}
2736
James Smarte59058c2008-08-24 21:49:00 -04002737/**
James Smart3621a712009-04-06 18:47:14 -04002738 * lpfc_challenge_key - Create challenge key based on WWPN of the HBA
James Smarte59058c2008-08-24 21:49:00 -04002739 * @RandomChallenge: pointer to the entry of host challenge random number array.
2740 * @HashWorking: pointer to the entry of the working hash array.
2741 *
2742 * This routine calculates the working hash array referred by @HashWorking
2743 * from the challenge random numbers associated with the host, referred by
2744 * @RandomChallenge. The result is put into the entry of the working hash
2745 * array and returned by reference through @HashWorking.
2746 **/
dea31012005-04-17 16:05:31 -05002747static void
2748lpfc_challenge_key(uint32_t * RandomChallenge, uint32_t * HashWorking)
2749{
2750 *HashWorking = (*RandomChallenge ^ *HashWorking);
2751}
2752
James Smarte59058c2008-08-24 21:49:00 -04002753/**
James Smart3621a712009-04-06 18:47:14 -04002754 * lpfc_hba_init - Perform special handling for LC HBA initialization
James Smarte59058c2008-08-24 21:49:00 -04002755 * @phba: pointer to lpfc hba data structure.
2756 * @hbainit: pointer to an array of unsigned 32-bit integers.
2757 *
2758 * This routine performs the special handling for LC HBA initialization.
2759 **/
dea31012005-04-17 16:05:31 -05002760void
2761lpfc_hba_init(struct lpfc_hba *phba, uint32_t *hbainit)
2762{
2763 int t;
2764 uint32_t *HashWorking;
James Smart2e0fef82007-06-17 19:56:36 -05002765 uint32_t *pwwnn = (uint32_t *) phba->wwnn;
dea31012005-04-17 16:05:31 -05002766
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +02002767 HashWorking = kcalloc(80, sizeof(uint32_t), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05002768 if (!HashWorking)
2769 return;
2770
dea31012005-04-17 16:05:31 -05002771 HashWorking[0] = HashWorking[78] = *pwwnn++;
2772 HashWorking[1] = HashWorking[79] = *pwwnn;
2773
2774 for (t = 0; t < 7; t++)
2775 lpfc_challenge_key(phba->RandomData + t, HashWorking + t);
2776
2777 lpfc_sha_init(hbainit);
2778 lpfc_sha_iterate(hbainit, HashWorking);
2779 kfree(HashWorking);
2780}
2781
James Smarte59058c2008-08-24 21:49:00 -04002782/**
James Smart3621a712009-04-06 18:47:14 -04002783 * lpfc_cleanup - Performs vport cleanups before deleting a vport
James Smarte59058c2008-08-24 21:49:00 -04002784 * @vport: pointer to a virtual N_Port data structure.
2785 *
2786 * This routine performs the necessary cleanups before deleting the @vport.
2787 * It invokes the discovery state machine to perform necessary state
2788 * transitions and to release the ndlps associated with the @vport. Note,
2789 * the physical port is treated as @vport 0.
2790 **/
James Smart87af33f2007-10-27 13:37:43 -04002791void
James Smart2e0fef82007-06-17 19:56:36 -05002792lpfc_cleanup(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002793{
James Smart87af33f2007-10-27 13:37:43 -04002794 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002795 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smarta8adb832007-10-27 13:37:53 -04002796 int i = 0;
dea31012005-04-17 16:05:31 -05002797
James Smart87af33f2007-10-27 13:37:43 -04002798 if (phba->link_state > LPFC_LINK_DOWN)
2799 lpfc_port_link_failure(vport);
2800
2801 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05002802 if (!NLP_CHK_NODE_ACT(ndlp)) {
2803 ndlp = lpfc_enable_node(vport, ndlp,
2804 NLP_STE_UNUSED_NODE);
2805 if (!ndlp)
2806 continue;
2807 spin_lock_irq(&phba->ndlp_lock);
2808 NLP_SET_FREE_REQ(ndlp);
2809 spin_unlock_irq(&phba->ndlp_lock);
2810 /* Trigger the release of the ndlp memory */
2811 lpfc_nlp_put(ndlp);
2812 continue;
2813 }
2814 spin_lock_irq(&phba->ndlp_lock);
2815 if (NLP_CHK_FREE_REQ(ndlp)) {
2816 /* The ndlp should not be in memory free mode already */
2817 spin_unlock_irq(&phba->ndlp_lock);
2818 continue;
2819 } else
2820 /* Indicate request for freeing ndlp memory */
2821 NLP_SET_FREE_REQ(ndlp);
2822 spin_unlock_irq(&phba->ndlp_lock);
2823
James Smart58da1ff2008-04-07 10:15:56 -04002824 if (vport->port_type != LPFC_PHYSICAL_PORT &&
2825 ndlp->nlp_DID == Fabric_DID) {
2826 /* Just free up ndlp with Fabric_DID for vports */
2827 lpfc_nlp_put(ndlp);
2828 continue;
2829 }
2830
James Smarteff4a012012-01-18 16:25:25 -05002831 /* take care of nodes in unused state before the state
2832 * machine taking action.
2833 */
2834 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE) {
2835 lpfc_nlp_put(ndlp);
2836 continue;
2837 }
2838
James Smart87af33f2007-10-27 13:37:43 -04002839 if (ndlp->nlp_type & NLP_FABRIC)
2840 lpfc_disc_state_machine(vport, ndlp, NULL,
2841 NLP_EVT_DEVICE_RECOVERY);
James Smarte47c9092008-02-08 18:49:26 -05002842
James Smart87af33f2007-10-27 13:37:43 -04002843 lpfc_disc_state_machine(vport, ndlp, NULL,
2844 NLP_EVT_DEVICE_RM);
2845 }
2846
James Smarta8adb832007-10-27 13:37:53 -04002847 /* At this point, ALL ndlp's should be gone
2848 * because of the previous NLP_EVT_DEVICE_RM.
2849 * Lets wait for this to happen, if needed.
2850 */
James Smart87af33f2007-10-27 13:37:43 -04002851 while (!list_empty(&vport->fc_nodes)) {
James Smarta8adb832007-10-27 13:37:53 -04002852 if (i++ > 3000) {
James Smart87af33f2007-10-27 13:37:43 -04002853 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
James Smarta8adb832007-10-27 13:37:53 -04002854 "0233 Nodelist not empty\n");
James Smarte47c9092008-02-08 18:49:26 -05002855 list_for_each_entry_safe(ndlp, next_ndlp,
2856 &vport->fc_nodes, nlp_listp) {
2857 lpfc_printf_vlog(ndlp->vport, KERN_ERR,
2858 LOG_NODE,
James Smartd7c255b2008-08-24 21:50:00 -04002859 "0282 did:x%x ndlp:x%p "
James Smarte47c9092008-02-08 18:49:26 -05002860 "usgmap:x%x refcnt:%d\n",
2861 ndlp->nlp_DID, (void *)ndlp,
2862 ndlp->nlp_usg_map,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01002863 kref_read(&ndlp->kref));
James Smarte47c9092008-02-08 18:49:26 -05002864 }
James Smarta8adb832007-10-27 13:37:53 -04002865 break;
James Smart87af33f2007-10-27 13:37:43 -04002866 }
James Smarta8adb832007-10-27 13:37:53 -04002867
2868 /* Wait for any activity on ndlps to settle */
2869 msleep(10);
James Smart87af33f2007-10-27 13:37:43 -04002870 }
James Smart1151e3e2011-02-16 12:39:35 -05002871 lpfc_cleanup_vports_rrqs(vport, NULL);
dea31012005-04-17 16:05:31 -05002872}
2873
James Smarte59058c2008-08-24 21:49:00 -04002874/**
James Smart3621a712009-04-06 18:47:14 -04002875 * lpfc_stop_vport_timers - Stop all the timers associated with a vport
James Smarte59058c2008-08-24 21:49:00 -04002876 * @vport: pointer to a virtual N_Port data structure.
2877 *
2878 * This routine stops all the timers associated with a @vport. This function
2879 * is invoked before disabling or deleting a @vport. Note that the physical
2880 * port is treated as @vport 0.
2881 **/
James Smart92d7f7b2007-06-17 19:56:38 -05002882void
2883lpfc_stop_vport_timers(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002884{
James Smart92d7f7b2007-06-17 19:56:38 -05002885 del_timer_sync(&vport->els_tmofunc);
James Smart92494142011-02-16 12:39:44 -05002886 del_timer_sync(&vport->delayed_disc_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05002887 lpfc_can_disctmo(vport);
2888 return;
dea31012005-04-17 16:05:31 -05002889}
2890
James Smarte59058c2008-08-24 21:49:00 -04002891/**
James Smartecfd03c2010-02-12 14:41:27 -05002892 * __lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2893 * @phba: pointer to lpfc hba data structure.
2894 *
2895 * This routine stops the SLI4 FCF rediscover wait timer if it's on. The
2896 * caller of this routine should already hold the host lock.
2897 **/
2898void
2899__lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2900{
James Smart5ac6b302010-10-22 11:05:36 -04002901 /* Clear pending FCF rediscovery wait flag */
2902 phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
2903
James Smartecfd03c2010-02-12 14:41:27 -05002904 /* Now, try to stop the timer */
2905 del_timer(&phba->fcf.redisc_wait);
2906}
2907
2908/**
2909 * lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2910 * @phba: pointer to lpfc hba data structure.
2911 *
2912 * This routine stops the SLI4 FCF rediscover wait timer if it's on. It
2913 * checks whether the FCF rediscovery wait timer is pending with the host
2914 * lock held before proceeding with disabling the timer and clearing the
2915 * wait timer pendig flag.
2916 **/
2917void
2918lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2919{
2920 spin_lock_irq(&phba->hbalock);
2921 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
2922 /* FCF rediscovery timer already fired or stopped */
2923 spin_unlock_irq(&phba->hbalock);
2924 return;
2925 }
2926 __lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
James Smart5ac6b302010-10-22 11:05:36 -04002927 /* Clear failover in progress flags */
2928 phba->fcf.fcf_flag &= ~(FCF_DEAD_DISC | FCF_ACVL_DISC);
James Smartecfd03c2010-02-12 14:41:27 -05002929 spin_unlock_irq(&phba->hbalock);
2930}
2931
2932/**
James Smart3772a992009-05-22 14:50:54 -04002933 * lpfc_stop_hba_timers - Stop all the timers associated with an HBA
James Smarte59058c2008-08-24 21:49:00 -04002934 * @phba: pointer to lpfc hba data structure.
2935 *
2936 * This routine stops all the timers associated with a HBA. This function is
2937 * invoked before either putting a HBA offline or unloading the driver.
2938 **/
James Smart3772a992009-05-22 14:50:54 -04002939void
2940lpfc_stop_hba_timers(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002941{
James Smartcdb42be2019-01-28 11:14:21 -08002942 if (phba->pport)
2943 lpfc_stop_vport_timers(phba->pport);
James Smart32517fc2019-01-28 11:14:33 -08002944 cancel_delayed_work_sync(&phba->eq_delay_work);
James Smart2e0fef82007-06-17 19:56:36 -05002945 del_timer_sync(&phba->sli.mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05002946 del_timer_sync(&phba->fabric_block_timer);
James Smart93996272008-08-24 21:50:30 -04002947 del_timer_sync(&phba->eratt_poll);
James Smart3772a992009-05-22 14:50:54 -04002948 del_timer_sync(&phba->hb_tmofunc);
James Smart1151e3e2011-02-16 12:39:35 -05002949 if (phba->sli_rev == LPFC_SLI_REV4) {
2950 del_timer_sync(&phba->rrq_tmr);
2951 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
2952 }
James Smart3772a992009-05-22 14:50:54 -04002953 phba->hb_outstanding = 0;
2954
2955 switch (phba->pci_dev_grp) {
2956 case LPFC_PCI_DEV_LP:
2957 /* Stop any LightPulse device specific driver timers */
2958 del_timer_sync(&phba->fcp_poll_timer);
2959 break;
2960 case LPFC_PCI_DEV_OC:
2961 /* Stop any OneConnect device sepcific driver timers */
James Smartecfd03c2010-02-12 14:41:27 -05002962 lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
James Smart3772a992009-05-22 14:50:54 -04002963 break;
2964 default:
2965 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2966 "0297 Invalid device group (x%x)\n",
2967 phba->pci_dev_grp);
2968 break;
2969 }
James Smart2e0fef82007-06-17 19:56:36 -05002970 return;
dea31012005-04-17 16:05:31 -05002971}
2972
James Smarte59058c2008-08-24 21:49:00 -04002973/**
James Smart3621a712009-04-06 18:47:14 -04002974 * lpfc_block_mgmt_io - Mark a HBA's management interface as blocked
James Smarte59058c2008-08-24 21:49:00 -04002975 * @phba: pointer to lpfc hba data structure.
2976 *
2977 * This routine marks a HBA's management interface as blocked. Once the HBA's
2978 * management interface is marked as blocked, all the user space access to
2979 * the HBA, whether they are from sysfs interface or libdfc interface will
2980 * all be blocked. The HBA is set to block the management interface when the
2981 * driver prepares the HBA interface for online or offline.
2982 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01002983static void
James Smart618a5232012-06-12 13:54:36 -04002984lpfc_block_mgmt_io(struct lpfc_hba *phba, int mbx_action)
Adrian Bunka6ababd2007-11-05 18:07:33 +01002985{
2986 unsigned long iflag;
James Smart6e7288d2010-06-07 15:23:35 -04002987 uint8_t actcmd = MBX_HEARTBEAT;
2988 unsigned long timeout;
2989
Adrian Bunka6ababd2007-11-05 18:07:33 +01002990 spin_lock_irqsave(&phba->hbalock, iflag);
2991 phba->sli.sli_flag |= LPFC_BLOCK_MGMT_IO;
James Smart618a5232012-06-12 13:54:36 -04002992 spin_unlock_irqrestore(&phba->hbalock, iflag);
2993 if (mbx_action == LPFC_MBX_NO_WAIT)
2994 return;
2995 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
2996 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta183a152011-10-10 21:32:43 -04002997 if (phba->sli.mbox_active) {
James Smart6e7288d2010-06-07 15:23:35 -04002998 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
James Smarta183a152011-10-10 21:32:43 -04002999 /* Determine how long we might wait for the active mailbox
3000 * command to be gracefully completed by firmware.
3001 */
3002 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
3003 phba->sli.mbox_active) * 1000) + jiffies;
3004 }
Adrian Bunka6ababd2007-11-05 18:07:33 +01003005 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta183a152011-10-10 21:32:43 -04003006
James Smart6e7288d2010-06-07 15:23:35 -04003007 /* Wait for the outstnading mailbox command to complete */
3008 while (phba->sli.mbox_active) {
3009 /* Check active mailbox complete status every 2ms */
3010 msleep(2);
3011 if (time_after(jiffies, timeout)) {
3012 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3013 "2813 Mgmt IO is Blocked %x "
3014 "- mbox cmd %x still active\n",
3015 phba->sli.sli_flag, actcmd);
3016 break;
3017 }
3018 }
Adrian Bunka6ababd2007-11-05 18:07:33 +01003019}
3020
James Smarte59058c2008-08-24 21:49:00 -04003021/**
James Smart6b5151f2012-01-18 16:24:06 -05003022 * lpfc_sli4_node_prep - Assign RPIs for active nodes.
3023 * @phba: pointer to lpfc hba data structure.
3024 *
3025 * Allocate RPIs for all active remote nodes. This is needed whenever
3026 * an SLI4 adapter is reset and the driver is not unloading. Its purpose
3027 * is to fixup the temporary rpi assignments.
3028 **/
3029void
3030lpfc_sli4_node_prep(struct lpfc_hba *phba)
3031{
3032 struct lpfc_nodelist *ndlp, *next_ndlp;
3033 struct lpfc_vport **vports;
James Smart9d3d3402017-04-21 16:05:00 -07003034 int i, rpi;
3035 unsigned long flags;
James Smart6b5151f2012-01-18 16:24:06 -05003036
3037 if (phba->sli_rev != LPFC_SLI_REV4)
3038 return;
3039
3040 vports = lpfc_create_vport_work_array(phba);
James Smart9d3d3402017-04-21 16:05:00 -07003041 if (vports == NULL)
3042 return;
James Smart6b5151f2012-01-18 16:24:06 -05003043
James Smart9d3d3402017-04-21 16:05:00 -07003044 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
3045 if (vports[i]->load_flag & FC_UNLOADING)
3046 continue;
3047
3048 list_for_each_entry_safe(ndlp, next_ndlp,
3049 &vports[i]->fc_nodes,
3050 nlp_listp) {
3051 if (!NLP_CHK_NODE_ACT(ndlp))
3052 continue;
3053 rpi = lpfc_sli4_alloc_rpi(phba);
3054 if (rpi == LPFC_RPI_ALLOC_ERROR) {
3055 spin_lock_irqsave(&phba->ndlp_lock, flags);
3056 NLP_CLR_NODE_ACT(ndlp);
3057 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
3058 continue;
James Smart6b5151f2012-01-18 16:24:06 -05003059 }
James Smart9d3d3402017-04-21 16:05:00 -07003060 ndlp->nlp_rpi = rpi;
3061 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
3062 "0009 rpi:%x DID:%x "
3063 "flg:%x map:%x %p\n", ndlp->nlp_rpi,
3064 ndlp->nlp_DID, ndlp->nlp_flag,
3065 ndlp->nlp_usg_map, ndlp);
James Smart6b5151f2012-01-18 16:24:06 -05003066 }
3067 }
3068 lpfc_destroy_vport_work_array(phba, vports);
3069}
3070
3071/**
James Smartc4908502019-01-28 11:14:28 -08003072 * lpfc_create_expedite_pool - create expedite pool
3073 * @phba: pointer to lpfc hba data structure.
3074 *
3075 * This routine moves a batch of XRIs from lpfc_io_buf_list_put of HWQ 0
3076 * to expedite pool. Mark them as expedite.
3077 **/
3078void lpfc_create_expedite_pool(struct lpfc_hba *phba)
3079{
3080 struct lpfc_sli4_hdw_queue *qp;
3081 struct lpfc_io_buf *lpfc_ncmd;
3082 struct lpfc_io_buf *lpfc_ncmd_next;
3083 struct lpfc_epd_pool *epd_pool;
3084 unsigned long iflag;
3085
3086 epd_pool = &phba->epd_pool;
3087 qp = &phba->sli4_hba.hdwq[0];
3088
3089 spin_lock_init(&epd_pool->lock);
3090 spin_lock_irqsave(&qp->io_buf_list_put_lock, iflag);
3091 spin_lock(&epd_pool->lock);
3092 INIT_LIST_HEAD(&epd_pool->list);
3093 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
3094 &qp->lpfc_io_buf_list_put, list) {
3095 list_move_tail(&lpfc_ncmd->list, &epd_pool->list);
3096 lpfc_ncmd->expedite = true;
3097 qp->put_io_bufs--;
3098 epd_pool->count++;
3099 if (epd_pool->count >= XRI_BATCH)
3100 break;
3101 }
3102 spin_unlock(&epd_pool->lock);
3103 spin_unlock_irqrestore(&qp->io_buf_list_put_lock, iflag);
3104}
3105
3106/**
3107 * lpfc_destroy_expedite_pool - destroy expedite pool
3108 * @phba: pointer to lpfc hba data structure.
3109 *
3110 * This routine returns XRIs from expedite pool to lpfc_io_buf_list_put
3111 * of HWQ 0. Clear the mark.
3112 **/
3113void lpfc_destroy_expedite_pool(struct lpfc_hba *phba)
3114{
3115 struct lpfc_sli4_hdw_queue *qp;
3116 struct lpfc_io_buf *lpfc_ncmd;
3117 struct lpfc_io_buf *lpfc_ncmd_next;
3118 struct lpfc_epd_pool *epd_pool;
3119 unsigned long iflag;
3120
3121 epd_pool = &phba->epd_pool;
3122 qp = &phba->sli4_hba.hdwq[0];
3123
3124 spin_lock_irqsave(&qp->io_buf_list_put_lock, iflag);
3125 spin_lock(&epd_pool->lock);
3126 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
3127 &epd_pool->list, list) {
3128 list_move_tail(&lpfc_ncmd->list,
3129 &qp->lpfc_io_buf_list_put);
3130 lpfc_ncmd->flags = false;
3131 qp->put_io_bufs++;
3132 epd_pool->count--;
3133 }
3134 spin_unlock(&epd_pool->lock);
3135 spin_unlock_irqrestore(&qp->io_buf_list_put_lock, iflag);
3136}
3137
3138/**
3139 * lpfc_create_multixri_pools - create multi-XRI pools
3140 * @phba: pointer to lpfc hba data structure.
3141 *
3142 * This routine initialize public, private per HWQ. Then, move XRIs from
3143 * lpfc_io_buf_list_put to public pool. High and low watermark are also
3144 * Initialized.
3145 **/
3146void lpfc_create_multixri_pools(struct lpfc_hba *phba)
3147{
3148 u32 i, j;
3149 u32 hwq_count;
3150 u32 count_per_hwq;
3151 struct lpfc_io_buf *lpfc_ncmd;
3152 struct lpfc_io_buf *lpfc_ncmd_next;
3153 unsigned long iflag;
3154 struct lpfc_sli4_hdw_queue *qp;
3155 struct lpfc_multixri_pool *multixri_pool;
3156 struct lpfc_pbl_pool *pbl_pool;
3157 struct lpfc_pvt_pool *pvt_pool;
3158
3159 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3160 "1234 num_hdw_queue=%d num_present_cpu=%d common_xri_cnt=%d\n",
3161 phba->cfg_hdw_queue, phba->sli4_hba.num_present_cpu,
3162 phba->sli4_hba.io_xri_cnt);
3163
3164 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
3165 lpfc_create_expedite_pool(phba);
3166
3167 hwq_count = phba->cfg_hdw_queue;
3168 count_per_hwq = phba->sli4_hba.io_xri_cnt / hwq_count;
3169
3170 for (i = 0; i < hwq_count; i++) {
3171 multixri_pool = kzalloc(sizeof(*multixri_pool), GFP_KERNEL);
3172
3173 if (!multixri_pool) {
3174 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3175 "1238 Failed to allocate memory for "
3176 "multixri_pool\n");
3177
3178 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
3179 lpfc_destroy_expedite_pool(phba);
3180
3181 j = 0;
3182 while (j < i) {
3183 qp = &phba->sli4_hba.hdwq[j];
3184 kfree(qp->p_multixri_pool);
3185 j++;
3186 }
3187 phba->cfg_xri_rebalancing = 0;
3188 return;
3189 }
3190
3191 qp = &phba->sli4_hba.hdwq[i];
3192 qp->p_multixri_pool = multixri_pool;
3193
3194 multixri_pool->xri_limit = count_per_hwq;
3195 multixri_pool->rrb_next_hwqid = i;
3196
3197 /* Deal with public free xri pool */
3198 pbl_pool = &multixri_pool->pbl_pool;
3199 spin_lock_init(&pbl_pool->lock);
3200 spin_lock_irqsave(&qp->io_buf_list_put_lock, iflag);
3201 spin_lock(&pbl_pool->lock);
3202 INIT_LIST_HEAD(&pbl_pool->list);
3203 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
3204 &qp->lpfc_io_buf_list_put, list) {
3205 list_move_tail(&lpfc_ncmd->list, &pbl_pool->list);
3206 qp->put_io_bufs--;
3207 pbl_pool->count++;
3208 }
3209 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3210 "1235 Moved %d buffers from PUT list over to pbl_pool[%d]\n",
3211 pbl_pool->count, i);
3212 spin_unlock(&pbl_pool->lock);
3213 spin_unlock_irqrestore(&qp->io_buf_list_put_lock, iflag);
3214
3215 /* Deal with private free xri pool */
3216 pvt_pool = &multixri_pool->pvt_pool;
3217 pvt_pool->high_watermark = multixri_pool->xri_limit / 2;
3218 pvt_pool->low_watermark = XRI_BATCH;
3219 spin_lock_init(&pvt_pool->lock);
3220 spin_lock_irqsave(&pvt_pool->lock, iflag);
3221 INIT_LIST_HEAD(&pvt_pool->list);
3222 pvt_pool->count = 0;
3223 spin_unlock_irqrestore(&pvt_pool->lock, iflag);
3224 }
3225}
3226
3227/**
3228 * lpfc_destroy_multixri_pools - destroy multi-XRI pools
3229 * @phba: pointer to lpfc hba data structure.
3230 *
3231 * This routine returns XRIs from public/private to lpfc_io_buf_list_put.
3232 **/
3233void lpfc_destroy_multixri_pools(struct lpfc_hba *phba)
3234{
3235 u32 i;
3236 u32 hwq_count;
3237 struct lpfc_io_buf *lpfc_ncmd;
3238 struct lpfc_io_buf *lpfc_ncmd_next;
3239 unsigned long iflag;
3240 struct lpfc_sli4_hdw_queue *qp;
3241 struct lpfc_multixri_pool *multixri_pool;
3242 struct lpfc_pbl_pool *pbl_pool;
3243 struct lpfc_pvt_pool *pvt_pool;
3244
3245 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
3246 lpfc_destroy_expedite_pool(phba);
3247
3248 hwq_count = phba->cfg_hdw_queue;
3249
3250 for (i = 0; i < hwq_count; i++) {
3251 qp = &phba->sli4_hba.hdwq[i];
3252 multixri_pool = qp->p_multixri_pool;
3253 if (!multixri_pool)
3254 continue;
3255
3256 qp->p_multixri_pool = NULL;
3257
3258 spin_lock_irqsave(&qp->io_buf_list_put_lock, iflag);
3259
3260 /* Deal with public free xri pool */
3261 pbl_pool = &multixri_pool->pbl_pool;
3262 spin_lock(&pbl_pool->lock);
3263
3264 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3265 "1236 Moving %d buffers from pbl_pool[%d] TO PUT list\n",
3266 pbl_pool->count, i);
3267
3268 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
3269 &pbl_pool->list, list) {
3270 list_move_tail(&lpfc_ncmd->list,
3271 &qp->lpfc_io_buf_list_put);
3272 qp->put_io_bufs++;
3273 pbl_pool->count--;
3274 }
3275
3276 INIT_LIST_HEAD(&pbl_pool->list);
3277 pbl_pool->count = 0;
3278
3279 spin_unlock(&pbl_pool->lock);
3280
3281 /* Deal with private free xri pool */
3282 pvt_pool = &multixri_pool->pvt_pool;
3283 spin_lock(&pvt_pool->lock);
3284
3285 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3286 "1237 Moving %d buffers from pvt_pool[%d] TO PUT list\n",
3287 pvt_pool->count, i);
3288
3289 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
3290 &pvt_pool->list, list) {
3291 list_move_tail(&lpfc_ncmd->list,
3292 &qp->lpfc_io_buf_list_put);
3293 qp->put_io_bufs++;
3294 pvt_pool->count--;
3295 }
3296
3297 INIT_LIST_HEAD(&pvt_pool->list);
3298 pvt_pool->count = 0;
3299
3300 spin_unlock(&pvt_pool->lock);
3301 spin_unlock_irqrestore(&qp->io_buf_list_put_lock, iflag);
3302
3303 kfree(multixri_pool);
3304 }
3305}
3306
3307/**
James Smart3621a712009-04-06 18:47:14 -04003308 * lpfc_online - Initialize and bring a HBA online
James Smarte59058c2008-08-24 21:49:00 -04003309 * @phba: pointer to lpfc hba data structure.
3310 *
3311 * This routine initializes the HBA and brings a HBA online. During this
3312 * process, the management interface is blocked to prevent user space access
3313 * to the HBA interfering with the driver initialization.
3314 *
3315 * Return codes
3316 * 0 - successful
3317 * 1 - failed
3318 **/
dea31012005-04-17 16:05:31 -05003319int
James Smart2e0fef82007-06-17 19:56:36 -05003320lpfc_online(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05003321{
Julia Lawall372bd282008-12-16 16:15:08 +01003322 struct lpfc_vport *vport;
James Smart549e55c2007-08-02 11:09:51 -04003323 struct lpfc_vport **vports;
Dick Kennedya145fda2017-08-23 16:55:44 -07003324 int i, error = 0;
James Smart16a3a202013-04-17 20:14:38 -04003325 bool vpis_cleared = false;
James Smart2e0fef82007-06-17 19:56:36 -05003326
dea31012005-04-17 16:05:31 -05003327 if (!phba)
3328 return 0;
Julia Lawall372bd282008-12-16 16:15:08 +01003329 vport = phba->pport;
dea31012005-04-17 16:05:31 -05003330
James Smart2e0fef82007-06-17 19:56:36 -05003331 if (!(vport->fc_flag & FC_OFFLINE_MODE))
dea31012005-04-17 16:05:31 -05003332 return 0;
3333
James Smarted957682007-06-17 19:56:37 -05003334 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003335 "0458 Bring Adapter online\n");
dea31012005-04-17 16:05:31 -05003336
James Smart618a5232012-06-12 13:54:36 -04003337 lpfc_block_mgmt_io(phba, LPFC_MBX_WAIT);
dea31012005-04-17 16:05:31 -05003338
James Smartda0436e2009-05-22 14:51:39 -04003339 if (phba->sli_rev == LPFC_SLI_REV4) {
3340 if (lpfc_sli4_hba_setup(phba)) { /* Initialize SLI4 HBA */
3341 lpfc_unblock_mgmt_io(phba);
3342 return 1;
3343 }
James Smart16a3a202013-04-17 20:14:38 -04003344 spin_lock_irq(&phba->hbalock);
3345 if (!phba->sli4_hba.max_cfg_param.vpi_used)
3346 vpis_cleared = true;
3347 spin_unlock_irq(&phba->hbalock);
Dick Kennedya145fda2017-08-23 16:55:44 -07003348
3349 /* Reestablish the local initiator port.
3350 * The offline process destroyed the previous lport.
3351 */
3352 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME &&
3353 !phba->nvmet_support) {
3354 error = lpfc_nvme_create_localport(phba->pport);
3355 if (error)
3356 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3357 "6132 NVME restore reg failed "
3358 "on nvmei error x%x\n", error);
3359 }
James Smartda0436e2009-05-22 14:51:39 -04003360 } else {
James Smart895427b2017-02-12 13:52:30 -08003361 lpfc_sli_queue_init(phba);
James Smartda0436e2009-05-22 14:51:39 -04003362 if (lpfc_sli_hba_setup(phba)) { /* Initialize SLI2/SLI3 HBA */
3363 lpfc_unblock_mgmt_io(phba);
3364 return 1;
3365 }
James Smart46fa3112007-04-25 09:51:45 -04003366 }
dea31012005-04-17 16:05:31 -05003367
James Smart549e55c2007-08-02 11:09:51 -04003368 vports = lpfc_create_vport_work_array(phba);
Arnd Bergmannaeb66412016-03-14 15:29:44 +01003369 if (vports != NULL) {
James Smartda0436e2009-05-22 14:51:39 -04003370 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -04003371 struct Scsi_Host *shost;
3372 shost = lpfc_shost_from_vport(vports[i]);
3373 spin_lock_irq(shost->host_lock);
3374 vports[i]->fc_flag &= ~FC_OFFLINE_MODE;
3375 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
3376 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart16a3a202013-04-17 20:14:38 -04003377 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart1c6834a2009-07-19 10:01:26 -04003378 vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
James Smart16a3a202013-04-17 20:14:38 -04003379 if ((vpis_cleared) &&
3380 (vports[i]->port_type !=
3381 LPFC_PHYSICAL_PORT))
3382 vports[i]->vpi = 0;
3383 }
James Smart549e55c2007-08-02 11:09:51 -04003384 spin_unlock_irq(shost->host_lock);
3385 }
Arnd Bergmannaeb66412016-03-14 15:29:44 +01003386 }
3387 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05003388
James Smartc4908502019-01-28 11:14:28 -08003389 if (phba->cfg_xri_rebalancing)
3390 lpfc_create_multixri_pools(phba);
3391
James Smart46fa3112007-04-25 09:51:45 -04003392 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05003393 return 0;
3394}
3395
James Smarte59058c2008-08-24 21:49:00 -04003396/**
James Smart3621a712009-04-06 18:47:14 -04003397 * lpfc_unblock_mgmt_io - Mark a HBA's management interface to be not blocked
James Smarte59058c2008-08-24 21:49:00 -04003398 * @phba: pointer to lpfc hba data structure.
3399 *
3400 * This routine marks a HBA's management interface as not blocked. Once the
3401 * HBA's management interface is marked as not blocked, all the user space
3402 * access to the HBA, whether they are from sysfs interface or libdfc
3403 * interface will be allowed. The HBA is set to block the management interface
3404 * when the driver prepares the HBA interface for online or offline and then
3405 * set to unblock the management interface afterwards.
3406 **/
James Smart46fa3112007-04-25 09:51:45 -04003407void
James Smart46fa3112007-04-25 09:51:45 -04003408lpfc_unblock_mgmt_io(struct lpfc_hba * phba)
3409{
3410 unsigned long iflag;
3411
James Smart2e0fef82007-06-17 19:56:36 -05003412 spin_lock_irqsave(&phba->hbalock, iflag);
3413 phba->sli.sli_flag &= ~LPFC_BLOCK_MGMT_IO;
3414 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart46fa3112007-04-25 09:51:45 -04003415}
3416
James Smarte59058c2008-08-24 21:49:00 -04003417/**
James Smart3621a712009-04-06 18:47:14 -04003418 * lpfc_offline_prep - Prepare a HBA to be brought offline
James Smarte59058c2008-08-24 21:49:00 -04003419 * @phba: pointer to lpfc hba data structure.
3420 *
3421 * This routine is invoked to prepare a HBA to be brought offline. It performs
3422 * unregistration login to all the nodes on all vports and flushes the mailbox
3423 * queue to make it ready to be brought offline.
3424 **/
James Smart46fa3112007-04-25 09:51:45 -04003425void
James Smart618a5232012-06-12 13:54:36 -04003426lpfc_offline_prep(struct lpfc_hba *phba, int mbx_action)
James Smart46fa3112007-04-25 09:51:45 -04003427{
James Smart2e0fef82007-06-17 19:56:36 -05003428 struct lpfc_vport *vport = phba->pport;
James Smart46fa3112007-04-25 09:51:45 -04003429 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart87af33f2007-10-27 13:37:43 -04003430 struct lpfc_vport **vports;
James Smart72100cc2010-02-12 14:43:01 -05003431 struct Scsi_Host *shost;
James Smart87af33f2007-10-27 13:37:43 -04003432 int i;
dea31012005-04-17 16:05:31 -05003433
James Smart2e0fef82007-06-17 19:56:36 -05003434 if (vport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04003435 return;
dea31012005-04-17 16:05:31 -05003436
James Smart618a5232012-06-12 13:54:36 -04003437 lpfc_block_mgmt_io(phba, mbx_action);
dea31012005-04-17 16:05:31 -05003438
3439 lpfc_linkdown(phba);
3440
James Smart87af33f2007-10-27 13:37:43 -04003441 /* Issue an unreg_login to all nodes on all vports */
3442 vports = lpfc_create_vport_work_array(phba);
3443 if (vports != NULL) {
James Smartda0436e2009-05-22 14:51:39 -04003444 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smarta8adb832007-10-27 13:37:53 -04003445 if (vports[i]->load_flag & FC_UNLOADING)
3446 continue;
James Smart72100cc2010-02-12 14:43:01 -05003447 shost = lpfc_shost_from_vport(vports[i]);
3448 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05003449 vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05003450 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
3451 vports[i]->fc_flag &= ~FC_VFI_REGISTERED;
James Smart72100cc2010-02-12 14:43:01 -05003452 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05003453
James Smart87af33f2007-10-27 13:37:43 -04003454 shost = lpfc_shost_from_vport(vports[i]);
3455 list_for_each_entry_safe(ndlp, next_ndlp,
3456 &vports[i]->fc_nodes,
3457 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05003458 if (!NLP_CHK_NODE_ACT(ndlp))
3459 continue;
James Smart87af33f2007-10-27 13:37:43 -04003460 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
3461 continue;
3462 if (ndlp->nlp_type & NLP_FABRIC) {
3463 lpfc_disc_state_machine(vports[i], ndlp,
3464 NULL, NLP_EVT_DEVICE_RECOVERY);
3465 lpfc_disc_state_machine(vports[i], ndlp,
3466 NULL, NLP_EVT_DEVICE_RM);
3467 }
3468 spin_lock_irq(shost->host_lock);
3469 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
James Smart401ee0c2012-03-01 22:35:34 -05003470 spin_unlock_irq(shost->host_lock);
James Smart6b5151f2012-01-18 16:24:06 -05003471 /*
3472 * Whenever an SLI4 port goes offline, free the
James Smart401ee0c2012-03-01 22:35:34 -05003473 * RPI. Get a new RPI when the adapter port
3474 * comes back online.
James Smart6b5151f2012-01-18 16:24:06 -05003475 */
James Smartbe6bb942015-04-07 15:07:22 -04003476 if (phba->sli_rev == LPFC_SLI_REV4) {
3477 lpfc_printf_vlog(ndlp->vport,
3478 KERN_INFO, LOG_NODE,
3479 "0011 lpfc_offline: "
3480 "ndlp:x%p did %x "
3481 "usgmap:x%x rpi:%x\n",
3482 ndlp, ndlp->nlp_DID,
3483 ndlp->nlp_usg_map,
3484 ndlp->nlp_rpi);
3485
James Smart6b5151f2012-01-18 16:24:06 -05003486 lpfc_sli4_free_rpi(phba, ndlp->nlp_rpi);
James Smartbe6bb942015-04-07 15:07:22 -04003487 }
James Smart87af33f2007-10-27 13:37:43 -04003488 lpfc_unreg_rpi(vports[i], ndlp);
3489 }
3490 }
3491 }
James Smart09372822008-01-11 01:52:54 -05003492 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05003493
James Smart618a5232012-06-12 13:54:36 -04003494 lpfc_sli_mbox_sys_shutdown(phba, mbx_action);
Dick Kennedyf485c182017-09-29 17:34:34 -07003495
3496 if (phba->wq)
3497 flush_workqueue(phba->wq);
James Smart46fa3112007-04-25 09:51:45 -04003498}
3499
James Smarte59058c2008-08-24 21:49:00 -04003500/**
James Smart3621a712009-04-06 18:47:14 -04003501 * lpfc_offline - Bring a HBA offline
James Smarte59058c2008-08-24 21:49:00 -04003502 * @phba: pointer to lpfc hba data structure.
3503 *
3504 * This routine actually brings a HBA offline. It stops all the timers
3505 * associated with the HBA, brings down the SLI layer, and eventually
3506 * marks the HBA as in offline state for the upper layer protocol.
3507 **/
James Smart46fa3112007-04-25 09:51:45 -04003508void
James Smart2e0fef82007-06-17 19:56:36 -05003509lpfc_offline(struct lpfc_hba *phba)
James Smart46fa3112007-04-25 09:51:45 -04003510{
James Smart549e55c2007-08-02 11:09:51 -04003511 struct Scsi_Host *shost;
3512 struct lpfc_vport **vports;
3513 int i;
James Smart46fa3112007-04-25 09:51:45 -04003514
James Smart549e55c2007-08-02 11:09:51 -04003515 if (phba->pport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04003516 return;
James Smart688a8862006-07-06 15:49:56 -04003517
James Smartda0436e2009-05-22 14:51:39 -04003518 /* stop port and all timers associated with this hba */
3519 lpfc_stop_port(phba);
Dick Kennedy4b40d022017-08-23 16:55:38 -07003520
3521 /* Tear down the local and target port registrations. The
3522 * nvme transports need to cleanup.
3523 */
3524 lpfc_nvmet_destroy_targetport(phba);
3525 lpfc_nvme_destroy_localport(phba->pport);
3526
James Smart51ef4c22007-08-02 11:10:31 -04003527 vports = lpfc_create_vport_work_array(phba);
3528 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04003529 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
James Smart51ef4c22007-08-02 11:10:31 -04003530 lpfc_stop_vport_timers(vports[i]);
James Smart09372822008-01-11 01:52:54 -05003531 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -05003532 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003533 "0460 Bring Adapter offline\n");
dea31012005-04-17 16:05:31 -05003534 /* Bring down the SLI Layer and cleanup. The HBA is offline
3535 now. */
3536 lpfc_sli_hba_down(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05003537 spin_lock_irq(&phba->hbalock);
James Smart7054a602007-04-25 09:52:34 -04003538 phba->work_ha = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05003539 spin_unlock_irq(&phba->hbalock);
James Smart549e55c2007-08-02 11:09:51 -04003540 vports = lpfc_create_vport_work_array(phba);
3541 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04003542 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -04003543 shost = lpfc_shost_from_vport(vports[i]);
James Smart549e55c2007-08-02 11:09:51 -04003544 spin_lock_irq(shost->host_lock);
3545 vports[i]->work_port_events = 0;
3546 vports[i]->fc_flag |= FC_OFFLINE_MODE;
3547 spin_unlock_irq(shost->host_lock);
3548 }
James Smart09372822008-01-11 01:52:54 -05003549 lpfc_destroy_vport_work_array(phba, vports);
James Smartc4908502019-01-28 11:14:28 -08003550
3551 if (phba->cfg_xri_rebalancing)
3552 lpfc_destroy_multixri_pools(phba);
dea31012005-04-17 16:05:31 -05003553}
3554
James Smarte59058c2008-08-24 21:49:00 -04003555/**
James Smart3621a712009-04-06 18:47:14 -04003556 * lpfc_scsi_free - Free all the SCSI buffers and IOCBs from driver lists
James Smarte59058c2008-08-24 21:49:00 -04003557 * @phba: pointer to lpfc hba data structure.
3558 *
3559 * This routine is to free all the SCSI buffers and IOCBs from the driver
3560 * list back to kernel. It is called from lpfc_pci_remove_one to free
3561 * the internal resources before the device is removed from the system.
James Smarte59058c2008-08-24 21:49:00 -04003562 **/
James Smart8a9d2e82012-05-09 21:16:12 -04003563static void
James Smart2e0fef82007-06-17 19:56:36 -05003564lpfc_scsi_free(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05003565{
James Smartc4908502019-01-28 11:14:28 -08003566 struct lpfc_io_buf *sb, *sb_next;
dea31012005-04-17 16:05:31 -05003567
James Smart895427b2017-02-12 13:52:30 -08003568 if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP))
3569 return;
3570
James Smart2e0fef82007-06-17 19:56:36 -05003571 spin_lock_irq(&phba->hbalock);
James Smarta40fc5f2013-04-17 20:17:40 -04003572
dea31012005-04-17 16:05:31 -05003573 /* Release all the lpfc_scsi_bufs maintained by this host. */
James Smarta40fc5f2013-04-17 20:17:40 -04003574
3575 spin_lock(&phba->scsi_buf_list_put_lock);
3576 list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list_put,
3577 list) {
dea31012005-04-17 16:05:31 -05003578 list_del(&sb->list);
Romain Perier771db5c2017-07-06 10:13:05 +02003579 dma_pool_free(phba->lpfc_sg_dma_buf_pool, sb->data,
James Smart92d7f7b2007-06-17 19:56:38 -05003580 sb->dma_handle);
dea31012005-04-17 16:05:31 -05003581 kfree(sb);
3582 phba->total_scsi_bufs--;
3583 }
James Smarta40fc5f2013-04-17 20:17:40 -04003584 spin_unlock(&phba->scsi_buf_list_put_lock);
3585
3586 spin_lock(&phba->scsi_buf_list_get_lock);
3587 list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list_get,
3588 list) {
3589 list_del(&sb->list);
Romain Perier771db5c2017-07-06 10:13:05 +02003590 dma_pool_free(phba->lpfc_sg_dma_buf_pool, sb->data,
James Smarta40fc5f2013-04-17 20:17:40 -04003591 sb->dma_handle);
3592 kfree(sb);
3593 phba->total_scsi_bufs--;
3594 }
3595 spin_unlock(&phba->scsi_buf_list_get_lock);
James Smart2e0fef82007-06-17 19:56:36 -05003596 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04003597}
James Smart0794d602019-01-28 11:14:19 -08003598
James Smart8a9d2e82012-05-09 21:16:12 -04003599/**
James Smart5e5b5112019-01-28 11:14:22 -08003600 * lpfc_io_free - Free all the IO buffers and IOCBs from driver lists
James Smart895427b2017-02-12 13:52:30 -08003601 * @phba: pointer to lpfc hba data structure.
3602 *
James Smart0794d602019-01-28 11:14:19 -08003603 * This routine is to free all the IO buffers and IOCBs from the driver
James Smart895427b2017-02-12 13:52:30 -08003604 * list back to kernel. It is called from lpfc_pci_remove_one to free
3605 * the internal resources before the device is removed from the system.
3606 **/
James Smartc4908502019-01-28 11:14:28 -08003607void
James Smart5e5b5112019-01-28 11:14:22 -08003608lpfc_io_free(struct lpfc_hba *phba)
James Smart895427b2017-02-12 13:52:30 -08003609{
James Smartc4908502019-01-28 11:14:28 -08003610 struct lpfc_io_buf *lpfc_ncmd, *lpfc_ncmd_next;
James Smart5e5b5112019-01-28 11:14:22 -08003611 struct lpfc_sli4_hdw_queue *qp;
3612 int idx;
James Smart895427b2017-02-12 13:52:30 -08003613
James Smart895427b2017-02-12 13:52:30 -08003614 spin_lock_irq(&phba->hbalock);
3615
James Smart5e5b5112019-01-28 11:14:22 -08003616 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
3617 qp = &phba->sli4_hba.hdwq[idx];
3618 /* Release all the lpfc_nvme_bufs maintained by this host. */
3619 spin_lock(&qp->io_buf_list_put_lock);
3620 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
3621 &qp->lpfc_io_buf_list_put,
3622 list) {
3623 list_del(&lpfc_ncmd->list);
3624 qp->put_io_bufs--;
3625 dma_pool_free(phba->lpfc_sg_dma_buf_pool,
3626 lpfc_ncmd->data, lpfc_ncmd->dma_handle);
3627 kfree(lpfc_ncmd);
3628 qp->total_io_bufs--;
3629 }
3630 spin_unlock(&qp->io_buf_list_put_lock);
James Smart895427b2017-02-12 13:52:30 -08003631
James Smart5e5b5112019-01-28 11:14:22 -08003632 spin_lock(&qp->io_buf_list_get_lock);
3633 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
3634 &qp->lpfc_io_buf_list_get,
3635 list) {
3636 list_del(&lpfc_ncmd->list);
3637 qp->get_io_bufs--;
3638 dma_pool_free(phba->lpfc_sg_dma_buf_pool,
3639 lpfc_ncmd->data, lpfc_ncmd->dma_handle);
3640 kfree(lpfc_ncmd);
3641 qp->total_io_bufs--;
3642 }
3643 spin_unlock(&qp->io_buf_list_get_lock);
James Smart895427b2017-02-12 13:52:30 -08003644 }
James Smart5e5b5112019-01-28 11:14:22 -08003645
James Smart895427b2017-02-12 13:52:30 -08003646 spin_unlock_irq(&phba->hbalock);
3647}
James Smart0794d602019-01-28 11:14:19 -08003648
James Smart895427b2017-02-12 13:52:30 -08003649/**
3650 * lpfc_sli4_els_sgl_update - update ELS xri-sgl sizing and mapping
James Smart8a9d2e82012-05-09 21:16:12 -04003651 * @phba: pointer to lpfc hba data structure.
3652 *
3653 * This routine first calculates the sizes of the current els and allocated
3654 * scsi sgl lists, and then goes through all sgls to updates the physical
3655 * XRIs assigned due to port function reset. During port initialization, the
3656 * current els and allocated scsi sgl lists are 0s.
3657 *
3658 * Return codes
3659 * 0 - successful (for now, it always returns 0)
3660 **/
3661int
James Smart895427b2017-02-12 13:52:30 -08003662lpfc_sli4_els_sgl_update(struct lpfc_hba *phba)
James Smart8a9d2e82012-05-09 21:16:12 -04003663{
3664 struct lpfc_sglq *sglq_entry = NULL, *sglq_entry_next = NULL;
James Smart895427b2017-02-12 13:52:30 -08003665 uint16_t i, lxri, xri_cnt, els_xri_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04003666 LIST_HEAD(els_sgl_list);
James Smart8a9d2e82012-05-09 21:16:12 -04003667 int rc;
3668
3669 /*
3670 * update on pci function's els xri-sgl list
3671 */
3672 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
James Smart895427b2017-02-12 13:52:30 -08003673
James Smart8a9d2e82012-05-09 21:16:12 -04003674 if (els_xri_cnt > phba->sli4_hba.els_xri_cnt) {
3675 /* els xri-sgl expanded */
3676 xri_cnt = els_xri_cnt - phba->sli4_hba.els_xri_cnt;
3677 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3678 "3157 ELS xri-sgl count increased from "
3679 "%d to %d\n", phba->sli4_hba.els_xri_cnt,
3680 els_xri_cnt);
3681 /* allocate the additional els sgls */
3682 for (i = 0; i < xri_cnt; i++) {
3683 sglq_entry = kzalloc(sizeof(struct lpfc_sglq),
3684 GFP_KERNEL);
3685 if (sglq_entry == NULL) {
3686 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3687 "2562 Failure to allocate an "
3688 "ELS sgl entry:%d\n", i);
3689 rc = -ENOMEM;
3690 goto out_free_mem;
3691 }
3692 sglq_entry->buff_type = GEN_BUFF_TYPE;
3693 sglq_entry->virt = lpfc_mbuf_alloc(phba, 0,
3694 &sglq_entry->phys);
3695 if (sglq_entry->virt == NULL) {
3696 kfree(sglq_entry);
3697 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3698 "2563 Failure to allocate an "
3699 "ELS mbuf:%d\n", i);
3700 rc = -ENOMEM;
3701 goto out_free_mem;
3702 }
3703 sglq_entry->sgl = sglq_entry->virt;
3704 memset(sglq_entry->sgl, 0, LPFC_BPL_SIZE);
3705 sglq_entry->state = SGL_FREED;
3706 list_add_tail(&sglq_entry->list, &els_sgl_list);
3707 }
James Smart38c20672013-03-01 16:37:44 -05003708 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -08003709 spin_lock(&phba->sli4_hba.sgl_list_lock);
3710 list_splice_init(&els_sgl_list,
3711 &phba->sli4_hba.lpfc_els_sgl_list);
3712 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smart38c20672013-03-01 16:37:44 -05003713 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04003714 } else if (els_xri_cnt < phba->sli4_hba.els_xri_cnt) {
3715 /* els xri-sgl shrinked */
3716 xri_cnt = phba->sli4_hba.els_xri_cnt - els_xri_cnt;
3717 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3718 "3158 ELS xri-sgl count decreased from "
3719 "%d to %d\n", phba->sli4_hba.els_xri_cnt,
3720 els_xri_cnt);
3721 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -08003722 spin_lock(&phba->sli4_hba.sgl_list_lock);
3723 list_splice_init(&phba->sli4_hba.lpfc_els_sgl_list,
3724 &els_sgl_list);
James Smart8a9d2e82012-05-09 21:16:12 -04003725 /* release extra els sgls from list */
3726 for (i = 0; i < xri_cnt; i++) {
3727 list_remove_head(&els_sgl_list,
3728 sglq_entry, struct lpfc_sglq, list);
3729 if (sglq_entry) {
James Smart895427b2017-02-12 13:52:30 -08003730 __lpfc_mbuf_free(phba, sglq_entry->virt,
3731 sglq_entry->phys);
James Smart8a9d2e82012-05-09 21:16:12 -04003732 kfree(sglq_entry);
3733 }
3734 }
James Smart895427b2017-02-12 13:52:30 -08003735 list_splice_init(&els_sgl_list,
3736 &phba->sli4_hba.lpfc_els_sgl_list);
3737 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04003738 spin_unlock_irq(&phba->hbalock);
3739 } else
3740 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3741 "3163 ELS xri-sgl count unchanged: %d\n",
3742 els_xri_cnt);
3743 phba->sli4_hba.els_xri_cnt = els_xri_cnt;
3744
3745 /* update xris to els sgls on the list */
3746 sglq_entry = NULL;
3747 sglq_entry_next = NULL;
3748 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
James Smart895427b2017-02-12 13:52:30 -08003749 &phba->sli4_hba.lpfc_els_sgl_list, list) {
James Smart8a9d2e82012-05-09 21:16:12 -04003750 lxri = lpfc_sli4_next_xritag(phba);
3751 if (lxri == NO_XRI) {
3752 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3753 "2400 Failed to allocate xri for "
3754 "ELS sgl\n");
3755 rc = -ENOMEM;
3756 goto out_free_mem;
3757 }
3758 sglq_entry->sli4_lxritag = lxri;
3759 sglq_entry->sli4_xritag = phba->sli4_hba.xri_ids[lxri];
3760 }
James Smart895427b2017-02-12 13:52:30 -08003761 return 0;
3762
3763out_free_mem:
3764 lpfc_free_els_sgl_list(phba);
3765 return rc;
3766}
3767
3768/**
James Smartf358dd02017-02-12 13:52:34 -08003769 * lpfc_sli4_nvmet_sgl_update - update xri-sgl sizing and mapping
3770 * @phba: pointer to lpfc hba data structure.
3771 *
3772 * This routine first calculates the sizes of the current els and allocated
3773 * scsi sgl lists, and then goes through all sgls to updates the physical
3774 * XRIs assigned due to port function reset. During port initialization, the
3775 * current els and allocated scsi sgl lists are 0s.
3776 *
3777 * Return codes
3778 * 0 - successful (for now, it always returns 0)
3779 **/
3780int
3781lpfc_sli4_nvmet_sgl_update(struct lpfc_hba *phba)
3782{
3783 struct lpfc_sglq *sglq_entry = NULL, *sglq_entry_next = NULL;
3784 uint16_t i, lxri, xri_cnt, els_xri_cnt;
James Smart6c621a22017-05-15 15:20:45 -07003785 uint16_t nvmet_xri_cnt;
James Smartf358dd02017-02-12 13:52:34 -08003786 LIST_HEAD(nvmet_sgl_list);
3787 int rc;
3788
3789 /*
3790 * update on pci function's nvmet xri-sgl list
3791 */
3792 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
James Smart61f3d4b2017-05-15 15:20:41 -07003793
James Smart6c621a22017-05-15 15:20:45 -07003794 /* For NVMET, ALL remaining XRIs are dedicated for IO processing */
3795 nvmet_xri_cnt = phba->sli4_hba.max_cfg_param.max_xri - els_xri_cnt;
James Smartf358dd02017-02-12 13:52:34 -08003796 if (nvmet_xri_cnt > phba->sli4_hba.nvmet_xri_cnt) {
3797 /* els xri-sgl expanded */
3798 xri_cnt = nvmet_xri_cnt - phba->sli4_hba.nvmet_xri_cnt;
3799 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3800 "6302 NVMET xri-sgl cnt grew from %d to %d\n",
3801 phba->sli4_hba.nvmet_xri_cnt, nvmet_xri_cnt);
3802 /* allocate the additional nvmet sgls */
3803 for (i = 0; i < xri_cnt; i++) {
3804 sglq_entry = kzalloc(sizeof(struct lpfc_sglq),
3805 GFP_KERNEL);
3806 if (sglq_entry == NULL) {
3807 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3808 "6303 Failure to allocate an "
3809 "NVMET sgl entry:%d\n", i);
3810 rc = -ENOMEM;
3811 goto out_free_mem;
3812 }
3813 sglq_entry->buff_type = NVMET_BUFF_TYPE;
3814 sglq_entry->virt = lpfc_nvmet_buf_alloc(phba, 0,
3815 &sglq_entry->phys);
3816 if (sglq_entry->virt == NULL) {
3817 kfree(sglq_entry);
3818 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3819 "6304 Failure to allocate an "
3820 "NVMET buf:%d\n", i);
3821 rc = -ENOMEM;
3822 goto out_free_mem;
3823 }
3824 sglq_entry->sgl = sglq_entry->virt;
3825 memset(sglq_entry->sgl, 0,
3826 phba->cfg_sg_dma_buf_size);
3827 sglq_entry->state = SGL_FREED;
3828 list_add_tail(&sglq_entry->list, &nvmet_sgl_list);
3829 }
3830 spin_lock_irq(&phba->hbalock);
3831 spin_lock(&phba->sli4_hba.sgl_list_lock);
3832 list_splice_init(&nvmet_sgl_list,
3833 &phba->sli4_hba.lpfc_nvmet_sgl_list);
3834 spin_unlock(&phba->sli4_hba.sgl_list_lock);
3835 spin_unlock_irq(&phba->hbalock);
3836 } else if (nvmet_xri_cnt < phba->sli4_hba.nvmet_xri_cnt) {
3837 /* nvmet xri-sgl shrunk */
3838 xri_cnt = phba->sli4_hba.nvmet_xri_cnt - nvmet_xri_cnt;
3839 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3840 "6305 NVMET xri-sgl count decreased from "
3841 "%d to %d\n", phba->sli4_hba.nvmet_xri_cnt,
3842 nvmet_xri_cnt);
3843 spin_lock_irq(&phba->hbalock);
3844 spin_lock(&phba->sli4_hba.sgl_list_lock);
3845 list_splice_init(&phba->sli4_hba.lpfc_nvmet_sgl_list,
3846 &nvmet_sgl_list);
3847 /* release extra nvmet sgls from list */
3848 for (i = 0; i < xri_cnt; i++) {
3849 list_remove_head(&nvmet_sgl_list,
3850 sglq_entry, struct lpfc_sglq, list);
3851 if (sglq_entry) {
3852 lpfc_nvmet_buf_free(phba, sglq_entry->virt,
3853 sglq_entry->phys);
3854 kfree(sglq_entry);
3855 }
3856 }
3857 list_splice_init(&nvmet_sgl_list,
3858 &phba->sli4_hba.lpfc_nvmet_sgl_list);
3859 spin_unlock(&phba->sli4_hba.sgl_list_lock);
3860 spin_unlock_irq(&phba->hbalock);
3861 } else
3862 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3863 "6306 NVMET xri-sgl count unchanged: %d\n",
3864 nvmet_xri_cnt);
3865 phba->sli4_hba.nvmet_xri_cnt = nvmet_xri_cnt;
3866
3867 /* update xris to nvmet sgls on the list */
3868 sglq_entry = NULL;
3869 sglq_entry_next = NULL;
3870 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
3871 &phba->sli4_hba.lpfc_nvmet_sgl_list, list) {
3872 lxri = lpfc_sli4_next_xritag(phba);
3873 if (lxri == NO_XRI) {
3874 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3875 "6307 Failed to allocate xri for "
3876 "NVMET sgl\n");
3877 rc = -ENOMEM;
3878 goto out_free_mem;
3879 }
3880 sglq_entry->sli4_lxritag = lxri;
3881 sglq_entry->sli4_xritag = phba->sli4_hba.xri_ids[lxri];
3882 }
3883 return 0;
3884
3885out_free_mem:
3886 lpfc_free_nvmet_sgl_list(phba);
3887 return rc;
3888}
3889
James Smart5e5b5112019-01-28 11:14:22 -08003890int
3891lpfc_io_buf_flush(struct lpfc_hba *phba, struct list_head *cbuf)
3892{
3893 LIST_HEAD(blist);
3894 struct lpfc_sli4_hdw_queue *qp;
James Smartc4908502019-01-28 11:14:28 -08003895 struct lpfc_io_buf *lpfc_cmd;
3896 struct lpfc_io_buf *iobufp, *prev_iobufp;
James Smart5e5b5112019-01-28 11:14:22 -08003897 int idx, cnt, xri, inserted;
3898
3899 cnt = 0;
3900 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
3901 qp = &phba->sli4_hba.hdwq[idx];
3902 spin_lock_irq(&qp->io_buf_list_get_lock);
3903 spin_lock(&qp->io_buf_list_put_lock);
3904
3905 /* Take everything off the get and put lists */
3906 list_splice_init(&qp->lpfc_io_buf_list_get, &blist);
3907 list_splice(&qp->lpfc_io_buf_list_put, &blist);
3908 INIT_LIST_HEAD(&qp->lpfc_io_buf_list_get);
3909 INIT_LIST_HEAD(&qp->lpfc_io_buf_list_put);
3910 cnt += qp->get_io_bufs + qp->put_io_bufs;
3911 qp->get_io_bufs = 0;
3912 qp->put_io_bufs = 0;
3913 qp->total_io_bufs = 0;
3914 spin_unlock(&qp->io_buf_list_put_lock);
3915 spin_unlock_irq(&qp->io_buf_list_get_lock);
3916 }
3917
3918 /*
3919 * Take IO buffers off blist and put on cbuf sorted by XRI.
3920 * This is because POST_SGL takes a sequential range of XRIs
3921 * to post to the firmware.
3922 */
3923 for (idx = 0; idx < cnt; idx++) {
James Smartc4908502019-01-28 11:14:28 -08003924 list_remove_head(&blist, lpfc_cmd, struct lpfc_io_buf, list);
James Smart5e5b5112019-01-28 11:14:22 -08003925 if (!lpfc_cmd)
3926 return cnt;
3927 if (idx == 0) {
3928 list_add_tail(&lpfc_cmd->list, cbuf);
3929 continue;
3930 }
3931 xri = lpfc_cmd->cur_iocbq.sli4_xritag;
3932 inserted = 0;
3933 prev_iobufp = NULL;
3934 list_for_each_entry(iobufp, cbuf, list) {
3935 if (xri < iobufp->cur_iocbq.sli4_xritag) {
3936 if (prev_iobufp)
3937 list_add(&lpfc_cmd->list,
3938 &prev_iobufp->list);
3939 else
3940 list_add(&lpfc_cmd->list, cbuf);
3941 inserted = 1;
3942 break;
3943 }
3944 prev_iobufp = iobufp;
3945 }
3946 if (!inserted)
3947 list_add_tail(&lpfc_cmd->list, cbuf);
3948 }
3949 return cnt;
3950}
3951
3952int
3953lpfc_io_buf_replenish(struct lpfc_hba *phba, struct list_head *cbuf)
3954{
3955 struct lpfc_sli4_hdw_queue *qp;
James Smartc4908502019-01-28 11:14:28 -08003956 struct lpfc_io_buf *lpfc_cmd;
James Smart5e5b5112019-01-28 11:14:22 -08003957 int idx, cnt;
3958
3959 qp = phba->sli4_hba.hdwq;
3960 cnt = 0;
3961 while (!list_empty(cbuf)) {
3962 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
3963 list_remove_head(cbuf, lpfc_cmd,
James Smartc4908502019-01-28 11:14:28 -08003964 struct lpfc_io_buf, list);
James Smart5e5b5112019-01-28 11:14:22 -08003965 if (!lpfc_cmd)
3966 return cnt;
3967 cnt++;
3968 qp = &phba->sli4_hba.hdwq[idx];
James Smart1fbf9742019-01-28 11:14:26 -08003969 lpfc_cmd->hdwq_no = idx;
3970 lpfc_cmd->hdwq = qp;
James Smart5e5b5112019-01-28 11:14:22 -08003971 lpfc_cmd->cur_iocbq.wqe_cmpl = NULL;
3972 lpfc_cmd->cur_iocbq.iocb_cmpl = NULL;
3973 spin_lock(&qp->io_buf_list_put_lock);
3974 list_add_tail(&lpfc_cmd->list,
3975 &qp->lpfc_io_buf_list_put);
3976 qp->put_io_bufs++;
3977 qp->total_io_bufs++;
3978 spin_unlock(&qp->io_buf_list_put_lock);
3979 }
3980 }
3981 return cnt;
3982}
3983
James Smartf358dd02017-02-12 13:52:34 -08003984/**
James Smart5e5b5112019-01-28 11:14:22 -08003985 * lpfc_sli4_io_sgl_update - update xri-sgl sizing and mapping
James Smart895427b2017-02-12 13:52:30 -08003986 * @phba: pointer to lpfc hba data structure.
3987 *
3988 * This routine first calculates the sizes of the current els and allocated
3989 * scsi sgl lists, and then goes through all sgls to updates the physical
3990 * XRIs assigned due to port function reset. During port initialization, the
3991 * current els and allocated scsi sgl lists are 0s.
3992 *
3993 * Return codes
3994 * 0 - successful (for now, it always returns 0)
3995 **/
3996int
James Smart5e5b5112019-01-28 11:14:22 -08003997lpfc_sli4_io_sgl_update(struct lpfc_hba *phba)
James Smart895427b2017-02-12 13:52:30 -08003998{
James Smartc4908502019-01-28 11:14:28 -08003999 struct lpfc_io_buf *lpfc_ncmd = NULL, *lpfc_ncmd_next = NULL;
James Smart0794d602019-01-28 11:14:19 -08004000 uint16_t i, lxri, els_xri_cnt;
James Smart5e5b5112019-01-28 11:14:22 -08004001 uint16_t io_xri_cnt, io_xri_max;
4002 LIST_HEAD(io_sgl_list);
James Smart0794d602019-01-28 11:14:19 -08004003 int rc, cnt;
4004
James Smart895427b2017-02-12 13:52:30 -08004005 /*
James Smart0794d602019-01-28 11:14:19 -08004006 * update on pci function's allocated nvme xri-sgl list
James Smart895427b2017-02-12 13:52:30 -08004007 */
James Smart0794d602019-01-28 11:14:19 -08004008
4009 /* maximum number of xris available for nvme buffers */
James Smart895427b2017-02-12 13:52:30 -08004010 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
James Smart5e5b5112019-01-28 11:14:22 -08004011 io_xri_max = phba->sli4_hba.max_cfg_param.max_xri - els_xri_cnt;
4012 phba->sli4_hba.io_xri_max = io_xri_max;
James Smart8a9d2e82012-05-09 21:16:12 -04004013
James Smarte8c0a772017-04-21 16:04:49 -07004014 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart0794d602019-01-28 11:14:19 -08004015 "6074 Current allocated XRI sgl count:%d, "
4016 "maximum XRI count:%d\n",
James Smart5e5b5112019-01-28 11:14:22 -08004017 phba->sli4_hba.io_xri_cnt,
4018 phba->sli4_hba.io_xri_max);
James Smarte8c0a772017-04-21 16:04:49 -07004019
James Smart5e5b5112019-01-28 11:14:22 -08004020 cnt = lpfc_io_buf_flush(phba, &io_sgl_list);
James Smart0794d602019-01-28 11:14:19 -08004021
James Smart5e5b5112019-01-28 11:14:22 -08004022 if (phba->sli4_hba.io_xri_cnt > phba->sli4_hba.io_xri_max) {
James Smart0794d602019-01-28 11:14:19 -08004023 /* max nvme xri shrunk below the allocated nvme buffers */
James Smart5e5b5112019-01-28 11:14:22 -08004024 io_xri_cnt = phba->sli4_hba.io_xri_cnt -
4025 phba->sli4_hba.io_xri_max;
James Smart0794d602019-01-28 11:14:19 -08004026 /* release the extra allocated nvme buffers */
James Smart5e5b5112019-01-28 11:14:22 -08004027 for (i = 0; i < io_xri_cnt; i++) {
4028 list_remove_head(&io_sgl_list, lpfc_ncmd,
James Smartc4908502019-01-28 11:14:28 -08004029 struct lpfc_io_buf, list);
James Smart0794d602019-01-28 11:14:19 -08004030 if (lpfc_ncmd) {
Romain Perier771db5c2017-07-06 10:13:05 +02004031 dma_pool_free(phba->lpfc_sg_dma_buf_pool,
James Smart0794d602019-01-28 11:14:19 -08004032 lpfc_ncmd->data,
4033 lpfc_ncmd->dma_handle);
4034 kfree(lpfc_ncmd);
James Smarta2fc4aef2014-09-03 12:57:55 -04004035 }
James Smart8a9d2e82012-05-09 21:16:12 -04004036 }
James Smart5e5b5112019-01-28 11:14:22 -08004037 phba->sli4_hba.io_xri_cnt -= io_xri_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04004038 }
4039
James Smart0794d602019-01-28 11:14:19 -08004040 /* update xris associated to remaining allocated nvme buffers */
4041 lpfc_ncmd = NULL;
4042 lpfc_ncmd_next = NULL;
James Smart5e5b5112019-01-28 11:14:22 -08004043 phba->sli4_hba.io_xri_cnt = cnt;
James Smart0794d602019-01-28 11:14:19 -08004044 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
James Smart5e5b5112019-01-28 11:14:22 -08004045 &io_sgl_list, list) {
James Smart8a9d2e82012-05-09 21:16:12 -04004046 lxri = lpfc_sli4_next_xritag(phba);
4047 if (lxri == NO_XRI) {
4048 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart0794d602019-01-28 11:14:19 -08004049 "6075 Failed to allocate xri for "
4050 "nvme buffer\n");
James Smart8a9d2e82012-05-09 21:16:12 -04004051 rc = -ENOMEM;
4052 goto out_free_mem;
4053 }
James Smart0794d602019-01-28 11:14:19 -08004054 lpfc_ncmd->cur_iocbq.sli4_lxritag = lxri;
4055 lpfc_ncmd->cur_iocbq.sli4_xritag = phba->sli4_hba.xri_ids[lxri];
James Smart8a9d2e82012-05-09 21:16:12 -04004056 }
James Smart5e5b5112019-01-28 11:14:22 -08004057 cnt = lpfc_io_buf_replenish(phba, &io_sgl_list);
dea31012005-04-17 16:05:31 -05004058 return 0;
James Smart8a9d2e82012-05-09 21:16:12 -04004059
4060out_free_mem:
James Smart5e5b5112019-01-28 11:14:22 -08004061 lpfc_io_free(phba);
James Smart8a9d2e82012-05-09 21:16:12 -04004062 return rc;
dea31012005-04-17 16:05:31 -05004063}
4064
James Smart0794d602019-01-28 11:14:19 -08004065/**
James Smart5e5b5112019-01-28 11:14:22 -08004066 * lpfc_new_io_buf - IO buffer allocator for HBA with SLI4 IF spec
James Smart0794d602019-01-28 11:14:19 -08004067 * @vport: The virtual port for which this call being executed.
4068 * @num_to_allocate: The requested number of buffers to allocate.
4069 *
4070 * This routine allocates nvme buffers for device with SLI-4 interface spec,
4071 * the nvme buffer contains all the necessary information needed to initiate
4072 * an I/O. After allocating up to @num_to_allocate IO buffers and put
4073 * them on a list, it post them to the port by using SGL block post.
4074 *
4075 * Return codes:
James Smart5e5b5112019-01-28 11:14:22 -08004076 * int - number of IO buffers that were allocated and posted.
James Smart0794d602019-01-28 11:14:19 -08004077 * 0 = failure, less than num_to_alloc is a partial failure.
4078 **/
4079int
James Smart5e5b5112019-01-28 11:14:22 -08004080lpfc_new_io_buf(struct lpfc_hba *phba, int num_to_alloc)
James Smart0794d602019-01-28 11:14:19 -08004081{
James Smartc4908502019-01-28 11:14:28 -08004082 struct lpfc_io_buf *lpfc_ncmd;
James Smart0794d602019-01-28 11:14:19 -08004083 struct lpfc_iocbq *pwqeq;
4084 uint16_t iotag, lxri = 0;
4085 int bcnt, num_posted;
4086 LIST_HEAD(prep_nblist);
4087 LIST_HEAD(post_nblist);
4088 LIST_HEAD(nvme_nblist);
4089
4090 /* Sanity check to ensure our sizing is right for both SCSI and NVME */
James Smartc4908502019-01-28 11:14:28 -08004091 if (sizeof(struct lpfc_io_buf) > LPFC_COMMON_IO_BUF_SZ) {
James Smart0794d602019-01-28 11:14:19 -08004092 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
James Smartc4908502019-01-28 11:14:28 -08004093 "6426 Common buffer size %ld exceeds %d\n",
4094 sizeof(struct lpfc_io_buf),
4095 LPFC_COMMON_IO_BUF_SZ);
James Smart0794d602019-01-28 11:14:19 -08004096 return 0;
4097 }
4098
James Smart5e5b5112019-01-28 11:14:22 -08004099 phba->sli4_hba.io_xri_cnt = 0;
James Smart0794d602019-01-28 11:14:19 -08004100 for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
4101 lpfc_ncmd = kzalloc(LPFC_COMMON_IO_BUF_SZ, GFP_KERNEL);
4102 if (!lpfc_ncmd)
4103 break;
4104 /*
4105 * Get memory from the pci pool to map the virt space to
4106 * pci bus space for an I/O. The DMA buffer includes the
4107 * number of SGE's necessary to support the sg_tablesize.
4108 */
4109 lpfc_ncmd->data = dma_pool_alloc(phba->lpfc_sg_dma_buf_pool,
4110 GFP_KERNEL,
4111 &lpfc_ncmd->dma_handle);
4112 if (!lpfc_ncmd->data) {
4113 kfree(lpfc_ncmd);
4114 break;
4115 }
4116 memset(lpfc_ncmd->data, 0, phba->cfg_sg_dma_buf_size);
4117
4118 /*
4119 * 4K Page alignment is CRITICAL to BlockGuard, double check
4120 * to be sure.
4121 */
4122 if ((phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
4123 (((unsigned long)(lpfc_ncmd->data) &
4124 (unsigned long)(SLI4_PAGE_SIZE - 1)) != 0)) {
4125 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
4126 "3369 Memory alignment err: addr=%lx\n",
4127 (unsigned long)lpfc_ncmd->data);
4128 dma_pool_free(phba->lpfc_sg_dma_buf_pool,
4129 lpfc_ncmd->data, lpfc_ncmd->dma_handle);
4130 kfree(lpfc_ncmd);
4131 break;
4132 }
4133
4134 lxri = lpfc_sli4_next_xritag(phba);
4135 if (lxri == NO_XRI) {
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 pwqeq = &lpfc_ncmd->cur_iocbq;
4142
4143 /* Allocate iotag for lpfc_ncmd->cur_iocbq. */
4144 iotag = lpfc_sli_next_iotag(phba, pwqeq);
4145 if (iotag == 0) {
4146 dma_pool_free(phba->lpfc_sg_dma_buf_pool,
4147 lpfc_ncmd->data, lpfc_ncmd->dma_handle);
4148 kfree(lpfc_ncmd);
4149 lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
4150 "6121 Failed to allocate IOTAG for"
4151 " XRI:0x%x\n", lxri);
4152 lpfc_sli4_free_xri(phba, lxri);
4153 break;
4154 }
4155 pwqeq->sli4_lxritag = lxri;
4156 pwqeq->sli4_xritag = phba->sli4_hba.xri_ids[lxri];
4157 pwqeq->context1 = lpfc_ncmd;
4158
4159 /* Initialize local short-hand pointers. */
4160 lpfc_ncmd->dma_sgl = lpfc_ncmd->data;
4161 lpfc_ncmd->dma_phys_sgl = lpfc_ncmd->dma_handle;
4162 lpfc_ncmd->cur_iocbq.context1 = lpfc_ncmd;
James Smartc2017262019-01-28 11:14:37 -08004163 spin_lock_init(&lpfc_ncmd->buf_lock);
James Smart0794d602019-01-28 11:14:19 -08004164
4165 /* add the nvme buffer to a post list */
4166 list_add_tail(&lpfc_ncmd->list, &post_nblist);
James Smart5e5b5112019-01-28 11:14:22 -08004167 phba->sli4_hba.io_xri_cnt++;
James Smart0794d602019-01-28 11:14:19 -08004168 }
4169 lpfc_printf_log(phba, KERN_INFO, LOG_NVME,
4170 "6114 Allocate %d out of %d requested new NVME "
4171 "buffers\n", bcnt, num_to_alloc);
4172
4173 /* post the list of nvme buffer sgls to port if available */
4174 if (!list_empty(&post_nblist))
James Smart5e5b5112019-01-28 11:14:22 -08004175 num_posted = lpfc_sli4_post_io_sgl_list(
James Smart0794d602019-01-28 11:14:19 -08004176 phba, &post_nblist, bcnt);
4177 else
4178 num_posted = 0;
4179
4180 return num_posted;
4181}
4182
James Smart96418b52017-03-04 09:30:31 -08004183static uint64_t
4184lpfc_get_wwpn(struct lpfc_hba *phba)
4185{
4186 uint64_t wwn;
4187 int rc;
4188 LPFC_MBOXQ_t *mboxq;
4189 MAILBOX_t *mb;
4190
James Smart96418b52017-03-04 09:30:31 -08004191 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
4192 GFP_KERNEL);
4193 if (!mboxq)
4194 return (uint64_t)-1;
4195
4196 /* First get WWN of HBA instance */
4197 lpfc_read_nv(phba, mboxq);
4198 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4199 if (rc != MBX_SUCCESS) {
4200 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4201 "6019 Mailbox failed , mbxCmd x%x "
4202 "READ_NV, mbxStatus x%x\n",
4203 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
4204 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
4205 mempool_free(mboxq, phba->mbox_mem_pool);
4206 return (uint64_t) -1;
4207 }
4208 mb = &mboxq->u.mb;
4209 memcpy(&wwn, (char *)mb->un.varRDnvp.portname, sizeof(uint64_t));
4210 /* wwn is WWPN of HBA instance */
4211 mempool_free(mboxq, phba->mbox_mem_pool);
4212 if (phba->sli_rev == LPFC_SLI_REV4)
4213 return be64_to_cpu(wwn);
4214 else
Maurizio Lombardi286871a2017-08-23 16:55:48 -07004215 return rol64(wwn, 32);
James Smart96418b52017-03-04 09:30:31 -08004216}
4217
James Smarte59058c2008-08-24 21:49:00 -04004218/**
James Smart3621a712009-04-06 18:47:14 -04004219 * lpfc_create_port - Create an FC port
James Smarte59058c2008-08-24 21:49:00 -04004220 * @phba: pointer to lpfc hba data structure.
4221 * @instance: a unique integer ID to this FC port.
4222 * @dev: pointer to the device data structure.
4223 *
4224 * This routine creates a FC port for the upper layer protocol. The FC port
4225 * can be created on top of either a physical port or a virtual port provided
4226 * by the HBA. This routine also allocates a SCSI host data structure (shost)
4227 * and associates the FC port created before adding the shost into the SCSI
4228 * layer.
4229 *
4230 * Return codes
4231 * @vport - pointer to the virtual N_Port data structure.
4232 * NULL - port create failed.
4233 **/
James Smart2e0fef82007-06-17 19:56:36 -05004234struct lpfc_vport *
James Smart3de2a652007-08-02 11:09:59 -04004235lpfc_create_port(struct lpfc_hba *phba, int instance, struct device *dev)
James Smart47a86172007-04-25 09:53:22 -04004236{
James Smart2e0fef82007-06-17 19:56:36 -05004237 struct lpfc_vport *vport;
James Smart895427b2017-02-12 13:52:30 -08004238 struct Scsi_Host *shost = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05004239 int error = 0;
James Smart96418b52017-03-04 09:30:31 -08004240 int i;
4241 uint64_t wwn;
4242 bool use_no_reset_hba = false;
James Smart56bc8022017-06-15 22:56:43 -07004243 int rc;
James Smart96418b52017-03-04 09:30:31 -08004244
James Smart56bc8022017-06-15 22:56:43 -07004245 if (lpfc_no_hba_reset_cnt) {
4246 if (phba->sli_rev < LPFC_SLI_REV4 &&
4247 dev == &phba->pcidev->dev) {
4248 /* Reset the port first */
4249 lpfc_sli_brdrestart(phba);
4250 rc = lpfc_sli_chipset_init(phba);
4251 if (rc)
4252 return NULL;
4253 }
4254 wwn = lpfc_get_wwpn(phba);
4255 }
James Smart96418b52017-03-04 09:30:31 -08004256
4257 for (i = 0; i < lpfc_no_hba_reset_cnt; i++) {
4258 if (wwn == lpfc_no_hba_reset[i]) {
4259 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4260 "6020 Setting use_no_reset port=%llx\n",
4261 wwn);
4262 use_no_reset_hba = true;
4263 break;
4264 }
4265 }
James Smart47a86172007-04-25 09:53:22 -04004266
James Smart895427b2017-02-12 13:52:30 -08004267 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) {
4268 if (dev != &phba->pcidev->dev) {
4269 shost = scsi_host_alloc(&lpfc_vport_template,
4270 sizeof(struct lpfc_vport));
4271 } else {
James Smart96418b52017-03-04 09:30:31 -08004272 if (!use_no_reset_hba)
James Smart895427b2017-02-12 13:52:30 -08004273 shost = scsi_host_alloc(&lpfc_template,
4274 sizeof(struct lpfc_vport));
4275 else
James Smart96418b52017-03-04 09:30:31 -08004276 shost = scsi_host_alloc(&lpfc_template_no_hr,
James Smart895427b2017-02-12 13:52:30 -08004277 sizeof(struct lpfc_vport));
4278 }
4279 } else if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
4280 shost = scsi_host_alloc(&lpfc_template_nvme,
James Smartea4142f2015-04-07 15:07:13 -04004281 sizeof(struct lpfc_vport));
4282 }
James Smart2e0fef82007-06-17 19:56:36 -05004283 if (!shost)
4284 goto out;
James Smart47a86172007-04-25 09:53:22 -04004285
James Smart2e0fef82007-06-17 19:56:36 -05004286 vport = (struct lpfc_vport *) shost->hostdata;
4287 vport->phba = phba;
James Smart2e0fef82007-06-17 19:56:36 -05004288 vport->load_flag |= FC_LOADING;
James Smart92d7f7b2007-06-17 19:56:38 -05004289 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart7f5f3d02008-02-08 18:50:14 -05004290 vport->fc_rscn_flush = 0;
James Smart3de2a652007-08-02 11:09:59 -04004291 lpfc_get_vport_cfgparam(vport);
James Smart895427b2017-02-12 13:52:30 -08004292
James Smartf6e84792019-01-28 11:14:38 -08004293 /* Adjust value in vport */
4294 vport->cfg_enable_fc4_type = phba->cfg_enable_fc4_type;
4295
James Smart2e0fef82007-06-17 19:56:36 -05004296 shost->unique_id = instance;
4297 shost->max_id = LPFC_MAX_TARGET;
James Smart3de2a652007-08-02 11:09:59 -04004298 shost->max_lun = vport->cfg_max_luns;
James Smart2e0fef82007-06-17 19:56:36 -05004299 shost->this_id = -1;
4300 shost->max_cmd_len = 16;
James Smart6a828b02019-01-28 11:14:31 -08004301
James Smartda0436e2009-05-22 14:51:39 -04004302 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart6a828b02019-01-28 11:14:31 -08004303 if (phba->cfg_fcp_io_sched == LPFC_FCP_SCHED_BY_HDWQ)
4304 shost->nr_hw_queues = phba->cfg_hdw_queue;
4305 else
4306 shost->nr_hw_queues = phba->sli4_hba.num_present_cpu;
4307
James Smart28baac72010-02-12 14:42:03 -05004308 shost->dma_boundary =
James Smartcb5172e2010-03-15 11:25:07 -04004309 phba->sli4_hba.pc_sli4_params.sge_supp_len-1;
James Smart5b9e70b2018-09-10 10:30:42 -07004310 shost->sg_tablesize = phba->cfg_scsi_seg_cnt;
James Smartace44e42019-01-28 11:14:27 -08004311 } else
4312 /* SLI-3 has a limited number of hardware queues (3),
4313 * thus there is only one for FCP processing.
4314 */
4315 shost->nr_hw_queues = 1;
James Smart81301a92008-12-04 22:39:46 -05004316
James Smart47a86172007-04-25 09:53:22 -04004317 /*
James Smart2e0fef82007-06-17 19:56:36 -05004318 * Set initial can_queue value since 0 is no longer supported and
4319 * scsi_add_host will fail. This will be adjusted later based on the
4320 * max xri value determined in hba setup.
James Smart47a86172007-04-25 09:53:22 -04004321 */
James Smart2e0fef82007-06-17 19:56:36 -05004322 shost->can_queue = phba->cfg_hba_queue_depth - 10;
James Smart3de2a652007-08-02 11:09:59 -04004323 if (dev != &phba->pcidev->dev) {
James Smart92d7f7b2007-06-17 19:56:38 -05004324 shost->transportt = lpfc_vport_transport_template;
4325 vport->port_type = LPFC_NPIV_PORT;
4326 } else {
4327 shost->transportt = lpfc_transport_template;
4328 vport->port_type = LPFC_PHYSICAL_PORT;
4329 }
James Smart47a86172007-04-25 09:53:22 -04004330
James Smart2e0fef82007-06-17 19:56:36 -05004331 /* Initialize all internally managed lists. */
4332 INIT_LIST_HEAD(&vport->fc_nodes);
James Smartda0436e2009-05-22 14:51:39 -04004333 INIT_LIST_HEAD(&vport->rcv_buffer_list);
James Smart2e0fef82007-06-17 19:56:36 -05004334 spin_lock_init(&vport->work_port_lock);
James Smart47a86172007-04-25 09:53:22 -04004335
Kees Cookf22eb4d2017-09-06 20:24:26 -07004336 timer_setup(&vport->fc_disctmo, lpfc_disc_timeout, 0);
James Smart47a86172007-04-25 09:53:22 -04004337
Kees Cookf22eb4d2017-09-06 20:24:26 -07004338 timer_setup(&vport->els_tmofunc, lpfc_els_timeout, 0);
James Smart92494142011-02-16 12:39:44 -05004339
Kees Cookf22eb4d2017-09-06 20:24:26 -07004340 timer_setup(&vport->delayed_disc_tmo, lpfc_delayed_disc_tmo, 0);
James Smart92494142011-02-16 12:39:44 -05004341
James Bottomleyd139b9b2009-11-05 13:33:12 -06004342 error = scsi_add_host_with_dma(shost, dev, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05004343 if (error)
4344 goto out_put_shost;
James Smart47a86172007-04-25 09:53:22 -04004345
James Smart523128e2018-09-10 10:30:46 -07004346 spin_lock_irq(&phba->port_list_lock);
James Smart2e0fef82007-06-17 19:56:36 -05004347 list_add_tail(&vport->listentry, &phba->port_list);
James Smart523128e2018-09-10 10:30:46 -07004348 spin_unlock_irq(&phba->port_list_lock);
James Smart2e0fef82007-06-17 19:56:36 -05004349 return vport;
James Smart47a86172007-04-25 09:53:22 -04004350
James Smart2e0fef82007-06-17 19:56:36 -05004351out_put_shost:
4352 scsi_host_put(shost);
4353out:
4354 return NULL;
James Smart47a86172007-04-25 09:53:22 -04004355}
4356
James Smarte59058c2008-08-24 21:49:00 -04004357/**
James Smart3621a712009-04-06 18:47:14 -04004358 * destroy_port - destroy an FC port
James Smarte59058c2008-08-24 21:49:00 -04004359 * @vport: pointer to an lpfc virtual N_Port data structure.
4360 *
4361 * This routine destroys a FC port from the upper layer protocol. All the
4362 * resources associated with the port are released.
4363 **/
James Smart2e0fef82007-06-17 19:56:36 -05004364void
4365destroy_port(struct lpfc_vport *vport)
James Smart47a86172007-04-25 09:53:22 -04004366{
James Smart92d7f7b2007-06-17 19:56:38 -05004367 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4368 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04004369
James Smart858c9f62007-06-17 19:56:39 -05004370 lpfc_debugfs_terminate(vport);
James Smart92d7f7b2007-06-17 19:56:38 -05004371 fc_remove_host(shost);
4372 scsi_remove_host(shost);
James Smart47a86172007-04-25 09:53:22 -04004373
James Smart523128e2018-09-10 10:30:46 -07004374 spin_lock_irq(&phba->port_list_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05004375 list_del_init(&vport->listentry);
James Smart523128e2018-09-10 10:30:46 -07004376 spin_unlock_irq(&phba->port_list_lock);
James Smart47a86172007-04-25 09:53:22 -04004377
James Smart92d7f7b2007-06-17 19:56:38 -05004378 lpfc_cleanup(vport);
James Smart47a86172007-04-25 09:53:22 -04004379 return;
James Smart47a86172007-04-25 09:53:22 -04004380}
4381
James Smarte59058c2008-08-24 21:49:00 -04004382/**
James Smart3621a712009-04-06 18:47:14 -04004383 * lpfc_get_instance - Get a unique integer ID
James Smarte59058c2008-08-24 21:49:00 -04004384 *
4385 * This routine allocates a unique integer ID from lpfc_hba_index pool. It
4386 * uses the kernel idr facility to perform the task.
4387 *
4388 * Return codes:
4389 * instance - a unique integer ID allocated as the new instance.
4390 * -1 - lpfc get instance failed.
4391 **/
James Smart92d7f7b2007-06-17 19:56:38 -05004392int
4393lpfc_get_instance(void)
4394{
Tejun Heoab516032013-02-27 17:04:44 -08004395 int ret;
James Smart92d7f7b2007-06-17 19:56:38 -05004396
Tejun Heoab516032013-02-27 17:04:44 -08004397 ret = idr_alloc(&lpfc_hba_index, NULL, 0, 0, GFP_KERNEL);
4398 return ret < 0 ? -1 : ret;
James Smart92d7f7b2007-06-17 19:56:38 -05004399}
4400
James Smarte59058c2008-08-24 21:49:00 -04004401/**
James Smart3621a712009-04-06 18:47:14 -04004402 * lpfc_scan_finished - method for SCSI layer to detect whether scan is done
James Smarte59058c2008-08-24 21:49:00 -04004403 * @shost: pointer to SCSI host data structure.
4404 * @time: elapsed time of the scan in jiffies.
4405 *
4406 * This routine is called by the SCSI layer with a SCSI host to determine
4407 * whether the scan host is finished.
4408 *
4409 * Note: there is no scan_start function as adapter initialization will have
4410 * asynchronously kicked off the link initialization.
4411 *
4412 * Return codes
4413 * 0 - SCSI host scan is not over yet.
4414 * 1 - SCSI host scan is over.
4415 **/
James Smart47a86172007-04-25 09:53:22 -04004416int lpfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
4417{
James Smart2e0fef82007-06-17 19:56:36 -05004418 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4419 struct lpfc_hba *phba = vport->phba;
James Smart858c9f62007-06-17 19:56:39 -05004420 int stat = 0;
James Smart47a86172007-04-25 09:53:22 -04004421
James Smart858c9f62007-06-17 19:56:39 -05004422 spin_lock_irq(shost->host_lock);
4423
James Smart51ef4c22007-08-02 11:10:31 -04004424 if (vport->load_flag & FC_UNLOADING) {
James Smart858c9f62007-06-17 19:56:39 -05004425 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04004426 goto finished;
James Smart858c9f62007-06-17 19:56:39 -05004427 }
James Smart256ec0d2013-04-17 20:14:58 -04004428 if (time >= msecs_to_jiffies(30 * 1000)) {
James Smart2e0fef82007-06-17 19:56:36 -05004429 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004430 "0461 Scanning longer than 30 "
4431 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05004432 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04004433 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05004434 }
James Smart256ec0d2013-04-17 20:14:58 -04004435 if (time >= msecs_to_jiffies(15 * 1000) &&
4436 phba->link_state <= LPFC_LINK_DOWN) {
James Smart2e0fef82007-06-17 19:56:36 -05004437 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004438 "0465 Link down longer than 15 "
4439 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05004440 stat = 1;
James Smart2e0fef82007-06-17 19:56:36 -05004441 goto finished;
James Smart47a86172007-04-25 09:53:22 -04004442 }
4443
James Smart2e0fef82007-06-17 19:56:36 -05004444 if (vport->port_state != LPFC_VPORT_READY)
James Smart858c9f62007-06-17 19:56:39 -05004445 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05004446 if (vport->num_disc_nodes || vport->fc_prli_sent)
James Smart858c9f62007-06-17 19:56:39 -05004447 goto finished;
James Smart256ec0d2013-04-17 20:14:58 -04004448 if (vport->fc_map_cnt == 0 && time < msecs_to_jiffies(2 * 1000))
James Smart858c9f62007-06-17 19:56:39 -05004449 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05004450 if ((phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) != 0)
James Smart858c9f62007-06-17 19:56:39 -05004451 goto finished;
4452
4453 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04004454
4455finished:
James Smart858c9f62007-06-17 19:56:39 -05004456 spin_unlock_irq(shost->host_lock);
4457 return stat;
James Smart92d7f7b2007-06-17 19:56:38 -05004458}
4459
James Smartcd713482018-10-23 13:41:01 -07004460void lpfc_host_supported_speeds_set(struct Scsi_Host *shost)
4461{
4462 struct lpfc_vport *vport = (struct lpfc_vport *)shost->hostdata;
4463 struct lpfc_hba *phba = vport->phba;
4464
4465 fc_host_supported_speeds(shost) = 0;
James Smart1dc5ec22018-10-23 13:41:11 -07004466 if (phba->lmt & LMT_128Gb)
4467 fc_host_supported_speeds(shost) |= FC_PORTSPEED_128GBIT;
James Smartcd713482018-10-23 13:41:01 -07004468 if (phba->lmt & LMT_64Gb)
4469 fc_host_supported_speeds(shost) |= FC_PORTSPEED_64GBIT;
4470 if (phba->lmt & LMT_32Gb)
4471 fc_host_supported_speeds(shost) |= FC_PORTSPEED_32GBIT;
4472 if (phba->lmt & LMT_16Gb)
4473 fc_host_supported_speeds(shost) |= FC_PORTSPEED_16GBIT;
4474 if (phba->lmt & LMT_10Gb)
4475 fc_host_supported_speeds(shost) |= FC_PORTSPEED_10GBIT;
4476 if (phba->lmt & LMT_8Gb)
4477 fc_host_supported_speeds(shost) |= FC_PORTSPEED_8GBIT;
4478 if (phba->lmt & LMT_4Gb)
4479 fc_host_supported_speeds(shost) |= FC_PORTSPEED_4GBIT;
4480 if (phba->lmt & LMT_2Gb)
4481 fc_host_supported_speeds(shost) |= FC_PORTSPEED_2GBIT;
4482 if (phba->lmt & LMT_1Gb)
4483 fc_host_supported_speeds(shost) |= FC_PORTSPEED_1GBIT;
4484}
4485
James Smarte59058c2008-08-24 21:49:00 -04004486/**
James Smart3621a712009-04-06 18:47:14 -04004487 * lpfc_host_attrib_init - Initialize SCSI host attributes on a FC port
James Smarte59058c2008-08-24 21:49:00 -04004488 * @shost: pointer to SCSI host data structure.
4489 *
4490 * This routine initializes a given SCSI host attributes on a FC port. The
4491 * SCSI host can be either on top of a physical port or a virtual port.
4492 **/
James Smart92d7f7b2007-06-17 19:56:38 -05004493void lpfc_host_attrib_init(struct Scsi_Host *shost)
4494{
4495 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4496 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04004497 /*
James Smart2e0fef82007-06-17 19:56:36 -05004498 * Set fixed host attributes. Must done after lpfc_sli_hba_setup().
James Smart47a86172007-04-25 09:53:22 -04004499 */
4500
James Smart2e0fef82007-06-17 19:56:36 -05004501 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
4502 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
James Smart47a86172007-04-25 09:53:22 -04004503 fc_host_supported_classes(shost) = FC_COS_CLASS3;
4504
4505 memset(fc_host_supported_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05004506 sizeof(fc_host_supported_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04004507 fc_host_supported_fc4s(shost)[2] = 1;
4508 fc_host_supported_fc4s(shost)[7] = 1;
4509
James Smart92d7f7b2007-06-17 19:56:38 -05004510 lpfc_vport_symbolic_node_name(vport, fc_host_symbolic_name(shost),
4511 sizeof fc_host_symbolic_name(shost));
James Smart47a86172007-04-25 09:53:22 -04004512
James Smartcd713482018-10-23 13:41:01 -07004513 lpfc_host_supported_speeds_set(shost);
James Smart47a86172007-04-25 09:53:22 -04004514
4515 fc_host_maxframe_size(shost) =
James Smart2e0fef82007-06-17 19:56:36 -05004516 (((uint32_t) vport->fc_sparam.cmn.bbRcvSizeMsb & 0x0F) << 8) |
4517 (uint32_t) vport->fc_sparam.cmn.bbRcvSizeLsb;
James Smart47a86172007-04-25 09:53:22 -04004518
Mike Christie0af5d702010-09-15 16:52:31 -05004519 fc_host_dev_loss_tmo(shost) = vport->cfg_devloss_tmo;
4520
James Smart47a86172007-04-25 09:53:22 -04004521 /* This value is also unchanging */
4522 memset(fc_host_active_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05004523 sizeof(fc_host_active_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04004524 fc_host_active_fc4s(shost)[2] = 1;
4525 fc_host_active_fc4s(shost)[7] = 1;
4526
James Smart92d7f7b2007-06-17 19:56:38 -05004527 fc_host_max_npiv_vports(shost) = phba->max_vpi;
James Smart47a86172007-04-25 09:53:22 -04004528 spin_lock_irq(shost->host_lock);
James Smart51ef4c22007-08-02 11:10:31 -04004529 vport->load_flag &= ~FC_LOADING;
James Smart47a86172007-04-25 09:53:22 -04004530 spin_unlock_irq(shost->host_lock);
James Smart47a86172007-04-25 09:53:22 -04004531}
dea31012005-04-17 16:05:31 -05004532
James Smarte59058c2008-08-24 21:49:00 -04004533/**
James Smartda0436e2009-05-22 14:51:39 -04004534 * lpfc_stop_port_s3 - Stop SLI3 device port
James Smarte59058c2008-08-24 21:49:00 -04004535 * @phba: pointer to lpfc hba data structure.
4536 *
James Smartda0436e2009-05-22 14:51:39 -04004537 * This routine is invoked to stop an SLI3 device port, it stops the device
4538 * from generating interrupts and stops the device driver's timers for the
4539 * device.
James Smarte59058c2008-08-24 21:49:00 -04004540 **/
James Smartdb2378e2008-02-08 18:49:51 -05004541static void
James Smartda0436e2009-05-22 14:51:39 -04004542lpfc_stop_port_s3(struct lpfc_hba *phba)
James Smartdb2378e2008-02-08 18:49:51 -05004543{
James Smartda0436e2009-05-22 14:51:39 -04004544 /* Clear all interrupt enable conditions */
4545 writel(0, phba->HCregaddr);
4546 readl(phba->HCregaddr); /* flush */
4547 /* Clear all pending interrupts */
4548 writel(0xffffffff, phba->HAregaddr);
4549 readl(phba->HAregaddr); /* flush */
James Smart93996272008-08-24 21:50:30 -04004550
James Smartda0436e2009-05-22 14:51:39 -04004551 /* Reset some HBA SLI setup states */
4552 lpfc_stop_hba_timers(phba);
4553 phba->pport->work_port_events = 0;
James Smartdb2378e2008-02-08 18:49:51 -05004554}
4555
James Smarte59058c2008-08-24 21:49:00 -04004556/**
James Smartda0436e2009-05-22 14:51:39 -04004557 * lpfc_stop_port_s4 - Stop SLI4 device port
James Smart5b75da22008-12-04 22:39:35 -05004558 * @phba: pointer to lpfc hba data structure.
4559 *
James Smartda0436e2009-05-22 14:51:39 -04004560 * This routine is invoked to stop an SLI4 device port, it stops the device
4561 * from generating interrupts and stops the device driver's timers for the
4562 * device.
4563 **/
4564static void
4565lpfc_stop_port_s4(struct lpfc_hba *phba)
4566{
4567 /* Reset some HBA SLI4 setup states */
4568 lpfc_stop_hba_timers(phba);
James Smartcdb42be2019-01-28 11:14:21 -08004569 if (phba->pport)
4570 phba->pport->work_port_events = 0;
James Smartda0436e2009-05-22 14:51:39 -04004571 phba->sli4_hba.intr_enable = 0;
James Smartda0436e2009-05-22 14:51:39 -04004572}
4573
4574/**
4575 * lpfc_stop_port - Wrapper function for stopping hba port
4576 * @phba: Pointer to HBA context object.
James Smart5b75da22008-12-04 22:39:35 -05004577 *
James Smartda0436e2009-05-22 14:51:39 -04004578 * This routine wraps the actual SLI3 or SLI4 hba stop port routine from
4579 * the API jump table function pointer from the lpfc_hba struct.
4580 **/
4581void
4582lpfc_stop_port(struct lpfc_hba *phba)
4583{
4584 phba->lpfc_stop_port(phba);
Dick Kennedyf485c182017-09-29 17:34:34 -07004585
4586 if (phba->wq)
4587 flush_workqueue(phba->wq);
James Smartda0436e2009-05-22 14:51:39 -04004588}
4589
4590/**
James Smartecfd03c2010-02-12 14:41:27 -05004591 * lpfc_fcf_redisc_wait_start_timer - Start fcf rediscover wait timer
4592 * @phba: Pointer to hba for which this call is being executed.
4593 *
4594 * This routine starts the timer waiting for the FCF rediscovery to complete.
4595 **/
4596void
4597lpfc_fcf_redisc_wait_start_timer(struct lpfc_hba *phba)
4598{
4599 unsigned long fcf_redisc_wait_tmo =
4600 (jiffies + msecs_to_jiffies(LPFC_FCF_REDISCOVER_WAIT_TMO));
4601 /* Start fcf rediscovery wait period timer */
4602 mod_timer(&phba->fcf.redisc_wait, fcf_redisc_wait_tmo);
4603 spin_lock_irq(&phba->hbalock);
4604 /* Allow action to new fcf asynchronous event */
4605 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
4606 /* Mark the FCF rediscovery pending state */
4607 phba->fcf.fcf_flag |= FCF_REDISC_PEND;
4608 spin_unlock_irq(&phba->hbalock);
4609}
4610
4611/**
4612 * lpfc_sli4_fcf_redisc_wait_tmo - FCF table rediscover wait timeout
4613 * @ptr: Map to lpfc_hba data structure pointer.
4614 *
4615 * This routine is invoked when waiting for FCF table rediscover has been
4616 * timed out. If new FCF record(s) has (have) been discovered during the
4617 * wait period, a new FCF event shall be added to the FCOE async event
4618 * list, and then worker thread shall be waked up for processing from the
4619 * worker thread context.
4620 **/
Rashika Kheriae399b222014-09-03 12:55:28 -04004621static void
Kees Cookf22eb4d2017-09-06 20:24:26 -07004622lpfc_sli4_fcf_redisc_wait_tmo(struct timer_list *t)
James Smartecfd03c2010-02-12 14:41:27 -05004623{
Kees Cookf22eb4d2017-09-06 20:24:26 -07004624 struct lpfc_hba *phba = from_timer(phba, t, fcf.redisc_wait);
James Smartecfd03c2010-02-12 14:41:27 -05004625
4626 /* Don't send FCF rediscovery event if timer cancelled */
4627 spin_lock_irq(&phba->hbalock);
4628 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
4629 spin_unlock_irq(&phba->hbalock);
4630 return;
4631 }
4632 /* Clear FCF rediscovery timer pending flag */
4633 phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
4634 /* FCF rediscovery event to worker thread */
4635 phba->fcf.fcf_flag |= FCF_REDISC_EVT;
4636 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004637 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04004638 "2776 FCF rediscover quiescent timer expired\n");
James Smartecfd03c2010-02-12 14:41:27 -05004639 /* wake up worker thread */
4640 lpfc_worker_wake_up(phba);
4641}
4642
4643/**
James Smartda0436e2009-05-22 14:51:39 -04004644 * lpfc_sli4_parse_latt_fault - Parse sli4 link-attention link fault code
4645 * @phba: pointer to lpfc hba data structure.
4646 * @acqe_link: pointer to the async link completion queue entry.
4647 *
James Smart23288b72018-05-04 20:37:53 -07004648 * This routine is to parse the SLI4 link-attention link fault code.
James Smartda0436e2009-05-22 14:51:39 -04004649 **/
James Smart23288b72018-05-04 20:37:53 -07004650static void
James Smartda0436e2009-05-22 14:51:39 -04004651lpfc_sli4_parse_latt_fault(struct lpfc_hba *phba,
4652 struct lpfc_acqe_link *acqe_link)
4653{
James Smartda0436e2009-05-22 14:51:39 -04004654 switch (bf_get(lpfc_acqe_link_fault, acqe_link)) {
4655 case LPFC_ASYNC_LINK_FAULT_NONE:
4656 case LPFC_ASYNC_LINK_FAULT_LOCAL:
4657 case LPFC_ASYNC_LINK_FAULT_REMOTE:
James Smart23288b72018-05-04 20:37:53 -07004658 case LPFC_ASYNC_LINK_FAULT_LR_LRR:
James Smartda0436e2009-05-22 14:51:39 -04004659 break;
4660 default:
4661 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart23288b72018-05-04 20:37:53 -07004662 "0398 Unknown link fault code: x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04004663 bf_get(lpfc_acqe_link_fault, acqe_link));
James Smartda0436e2009-05-22 14:51:39 -04004664 break;
4665 }
James Smartda0436e2009-05-22 14:51:39 -04004666}
4667
4668/**
4669 * lpfc_sli4_parse_latt_type - Parse sli4 link attention type
4670 * @phba: pointer to lpfc hba data structure.
4671 * @acqe_link: pointer to the async link completion queue entry.
4672 *
4673 * This routine is to parse the SLI4 link attention type and translate it
4674 * into the base driver's link attention type coding.
4675 *
4676 * Return: Link attention type in terms of base driver's coding.
4677 **/
4678static uint8_t
4679lpfc_sli4_parse_latt_type(struct lpfc_hba *phba,
4680 struct lpfc_acqe_link *acqe_link)
4681{
4682 uint8_t att_type;
4683
4684 switch (bf_get(lpfc_acqe_link_status, acqe_link)) {
4685 case LPFC_ASYNC_LINK_STATUS_DOWN:
4686 case LPFC_ASYNC_LINK_STATUS_LOGICAL_DOWN:
James Smart76a95d72010-11-20 23:11:48 -05004687 att_type = LPFC_ATT_LINK_DOWN;
James Smartda0436e2009-05-22 14:51:39 -04004688 break;
4689 case LPFC_ASYNC_LINK_STATUS_UP:
4690 /* Ignore physical link up events - wait for logical link up */
James Smart76a95d72010-11-20 23:11:48 -05004691 att_type = LPFC_ATT_RESERVED;
James Smartda0436e2009-05-22 14:51:39 -04004692 break;
4693 case LPFC_ASYNC_LINK_STATUS_LOGICAL_UP:
James Smart76a95d72010-11-20 23:11:48 -05004694 att_type = LPFC_ATT_LINK_UP;
James Smartda0436e2009-05-22 14:51:39 -04004695 break;
4696 default:
4697 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4698 "0399 Invalid link attention type: x%x\n",
4699 bf_get(lpfc_acqe_link_status, acqe_link));
James Smart76a95d72010-11-20 23:11:48 -05004700 att_type = LPFC_ATT_RESERVED;
James Smartda0436e2009-05-22 14:51:39 -04004701 break;
4702 }
4703 return att_type;
4704}
4705
4706/**
James Smart8b68cd52012-09-29 11:32:37 -04004707 * lpfc_sli_port_speed_get - Get sli3 link speed code to link speed
4708 * @phba: pointer to lpfc hba data structure.
4709 *
4710 * This routine is to get an SLI3 FC port's link speed in Mbps.
4711 *
4712 * Return: link speed in terms of Mbps.
4713 **/
4714uint32_t
4715lpfc_sli_port_speed_get(struct lpfc_hba *phba)
4716{
4717 uint32_t link_speed;
4718
4719 if (!lpfc_is_link_up(phba))
4720 return 0;
4721
James Smarta085e872015-12-16 18:12:02 -05004722 if (phba->sli_rev <= LPFC_SLI_REV3) {
4723 switch (phba->fc_linkspeed) {
4724 case LPFC_LINK_SPEED_1GHZ:
4725 link_speed = 1000;
4726 break;
4727 case LPFC_LINK_SPEED_2GHZ:
4728 link_speed = 2000;
4729 break;
4730 case LPFC_LINK_SPEED_4GHZ:
4731 link_speed = 4000;
4732 break;
4733 case LPFC_LINK_SPEED_8GHZ:
4734 link_speed = 8000;
4735 break;
4736 case LPFC_LINK_SPEED_10GHZ:
4737 link_speed = 10000;
4738 break;
4739 case LPFC_LINK_SPEED_16GHZ:
4740 link_speed = 16000;
4741 break;
4742 default:
4743 link_speed = 0;
4744 }
4745 } else {
4746 if (phba->sli4_hba.link_state.logical_speed)
4747 link_speed =
4748 phba->sli4_hba.link_state.logical_speed;
4749 else
4750 link_speed = phba->sli4_hba.link_state.speed;
James Smart8b68cd52012-09-29 11:32:37 -04004751 }
4752 return link_speed;
4753}
4754
4755/**
4756 * lpfc_sli4_port_speed_parse - Parse async evt link speed code to link speed
4757 * @phba: pointer to lpfc hba data structure.
4758 * @evt_code: asynchronous event code.
4759 * @speed_code: asynchronous event link speed code.
4760 *
4761 * This routine is to parse the giving SLI4 async event link speed code into
4762 * value of Mbps for the link speed.
4763 *
4764 * Return: link speed in terms of Mbps.
4765 **/
4766static uint32_t
4767lpfc_sli4_port_speed_parse(struct lpfc_hba *phba, uint32_t evt_code,
4768 uint8_t speed_code)
4769{
4770 uint32_t port_speed;
4771
4772 switch (evt_code) {
4773 case LPFC_TRAILER_CODE_LINK:
4774 switch (speed_code) {
James Smart26d830e2015-04-07 15:07:17 -04004775 case LPFC_ASYNC_LINK_SPEED_ZERO:
James Smart8b68cd52012-09-29 11:32:37 -04004776 port_speed = 0;
4777 break;
James Smart26d830e2015-04-07 15:07:17 -04004778 case LPFC_ASYNC_LINK_SPEED_10MBPS:
James Smart8b68cd52012-09-29 11:32:37 -04004779 port_speed = 10;
4780 break;
James Smart26d830e2015-04-07 15:07:17 -04004781 case LPFC_ASYNC_LINK_SPEED_100MBPS:
James Smart8b68cd52012-09-29 11:32:37 -04004782 port_speed = 100;
4783 break;
James Smart26d830e2015-04-07 15:07:17 -04004784 case LPFC_ASYNC_LINK_SPEED_1GBPS:
James Smart8b68cd52012-09-29 11:32:37 -04004785 port_speed = 1000;
4786 break;
James Smart26d830e2015-04-07 15:07:17 -04004787 case LPFC_ASYNC_LINK_SPEED_10GBPS:
James Smart8b68cd52012-09-29 11:32:37 -04004788 port_speed = 10000;
4789 break;
James Smart26d830e2015-04-07 15:07:17 -04004790 case LPFC_ASYNC_LINK_SPEED_20GBPS:
4791 port_speed = 20000;
4792 break;
4793 case LPFC_ASYNC_LINK_SPEED_25GBPS:
4794 port_speed = 25000;
4795 break;
4796 case LPFC_ASYNC_LINK_SPEED_40GBPS:
4797 port_speed = 40000;
4798 break;
James Smart8b68cd52012-09-29 11:32:37 -04004799 default:
4800 port_speed = 0;
4801 }
4802 break;
4803 case LPFC_TRAILER_CODE_FC:
4804 switch (speed_code) {
James Smart26d830e2015-04-07 15:07:17 -04004805 case LPFC_FC_LA_SPEED_UNKNOWN:
James Smart8b68cd52012-09-29 11:32:37 -04004806 port_speed = 0;
4807 break;
James Smart26d830e2015-04-07 15:07:17 -04004808 case LPFC_FC_LA_SPEED_1G:
James Smart8b68cd52012-09-29 11:32:37 -04004809 port_speed = 1000;
4810 break;
James Smart26d830e2015-04-07 15:07:17 -04004811 case LPFC_FC_LA_SPEED_2G:
James Smart8b68cd52012-09-29 11:32:37 -04004812 port_speed = 2000;
4813 break;
James Smart26d830e2015-04-07 15:07:17 -04004814 case LPFC_FC_LA_SPEED_4G:
James Smart8b68cd52012-09-29 11:32:37 -04004815 port_speed = 4000;
4816 break;
James Smart26d830e2015-04-07 15:07:17 -04004817 case LPFC_FC_LA_SPEED_8G:
James Smart8b68cd52012-09-29 11:32:37 -04004818 port_speed = 8000;
4819 break;
James Smart26d830e2015-04-07 15:07:17 -04004820 case LPFC_FC_LA_SPEED_10G:
James Smart8b68cd52012-09-29 11:32:37 -04004821 port_speed = 10000;
4822 break;
James Smart26d830e2015-04-07 15:07:17 -04004823 case LPFC_FC_LA_SPEED_16G:
James Smart8b68cd52012-09-29 11:32:37 -04004824 port_speed = 16000;
4825 break;
James Smartd38dd522015-08-31 16:48:17 -04004826 case LPFC_FC_LA_SPEED_32G:
4827 port_speed = 32000;
4828 break;
James Smartfbd8a6b2018-02-22 08:18:45 -08004829 case LPFC_FC_LA_SPEED_64G:
4830 port_speed = 64000;
4831 break;
James Smart1dc5ec22018-10-23 13:41:11 -07004832 case LPFC_FC_LA_SPEED_128G:
4833 port_speed = 128000;
4834 break;
James Smart8b68cd52012-09-29 11:32:37 -04004835 default:
4836 port_speed = 0;
4837 }
4838 break;
4839 default:
4840 port_speed = 0;
4841 }
4842 return port_speed;
4843}
4844
4845/**
James Smart70f3c072010-12-15 17:57:33 -05004846 * lpfc_sli4_async_link_evt - Process the asynchronous FCoE link event
James Smartda0436e2009-05-22 14:51:39 -04004847 * @phba: pointer to lpfc hba data structure.
4848 * @acqe_link: pointer to the async link completion queue entry.
4849 *
James Smart70f3c072010-12-15 17:57:33 -05004850 * This routine is to handle the SLI4 asynchronous FCoE link event.
James Smartda0436e2009-05-22 14:51:39 -04004851 **/
4852static void
4853lpfc_sli4_async_link_evt(struct lpfc_hba *phba,
4854 struct lpfc_acqe_link *acqe_link)
4855{
4856 struct lpfc_dmabuf *mp;
4857 LPFC_MBOXQ_t *pmb;
4858 MAILBOX_t *mb;
James Smart76a95d72010-11-20 23:11:48 -05004859 struct lpfc_mbx_read_top *la;
James Smartda0436e2009-05-22 14:51:39 -04004860 uint8_t att_type;
James Smart76a95d72010-11-20 23:11:48 -05004861 int rc;
James Smartda0436e2009-05-22 14:51:39 -04004862
4863 att_type = lpfc_sli4_parse_latt_type(phba, acqe_link);
James Smart76a95d72010-11-20 23:11:48 -05004864 if (att_type != LPFC_ATT_LINK_DOWN && att_type != LPFC_ATT_LINK_UP)
James Smartda0436e2009-05-22 14:51:39 -04004865 return;
James Smart32b97932009-07-19 10:01:21 -04004866 phba->fcoe_eventtag = acqe_link->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04004867 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4868 if (!pmb) {
4869 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4870 "0395 The mboxq allocation failed\n");
4871 return;
4872 }
4873 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4874 if (!mp) {
4875 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4876 "0396 The lpfc_dmabuf allocation failed\n");
4877 goto out_free_pmb;
4878 }
4879 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
4880 if (!mp->virt) {
4881 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4882 "0397 The mbuf allocation failed\n");
4883 goto out_free_dmabuf;
4884 }
4885
4886 /* Cleanup any outstanding ELS commands */
4887 lpfc_els_flush_all_cmd(phba);
4888
4889 /* Block ELS IOCBs until we have done process link event */
James Smart895427b2017-02-12 13:52:30 -08004890 phba->sli4_hba.els_wq->pring->flag |= LPFC_STOP_IOCB_EVENT;
James Smartda0436e2009-05-22 14:51:39 -04004891
4892 /* Update link event statistics */
4893 phba->sli.slistat.link_event++;
4894
James Smart76a95d72010-11-20 23:11:48 -05004895 /* Create lpfc_handle_latt mailbox command from link ACQE */
4896 lpfc_read_topology(phba, pmb, mp);
4897 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
James Smartda0436e2009-05-22 14:51:39 -04004898 pmb->vport = phba->pport;
4899
James Smartda0436e2009-05-22 14:51:39 -04004900 /* Keep the link status for extra SLI4 state machine reference */
4901 phba->sli4_hba.link_state.speed =
James Smart8b68cd52012-09-29 11:32:37 -04004902 lpfc_sli4_port_speed_parse(phba, LPFC_TRAILER_CODE_LINK,
4903 bf_get(lpfc_acqe_link_speed, acqe_link));
James Smartda0436e2009-05-22 14:51:39 -04004904 phba->sli4_hba.link_state.duplex =
4905 bf_get(lpfc_acqe_link_duplex, acqe_link);
4906 phba->sli4_hba.link_state.status =
4907 bf_get(lpfc_acqe_link_status, acqe_link);
James Smart70f3c072010-12-15 17:57:33 -05004908 phba->sli4_hba.link_state.type =
4909 bf_get(lpfc_acqe_link_type, acqe_link);
4910 phba->sli4_hba.link_state.number =
4911 bf_get(lpfc_acqe_link_number, acqe_link);
James Smartda0436e2009-05-22 14:51:39 -04004912 phba->sli4_hba.link_state.fault =
4913 bf_get(lpfc_acqe_link_fault, acqe_link);
James Smart65467b62010-01-26 23:08:29 -05004914 phba->sli4_hba.link_state.logical_speed =
James Smart8b68cd52012-09-29 11:32:37 -04004915 bf_get(lpfc_acqe_logical_link_speed, acqe_link) * 10;
4916
James Smart70f3c072010-12-15 17:57:33 -05004917 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartc31098c2011-04-16 11:03:33 -04004918 "2900 Async FC/FCoE Link event - Speed:%dGBit "
4919 "duplex:x%x LA Type:x%x Port Type:%d Port Number:%d "
4920 "Logical speed:%dMbps Fault:%d\n",
James Smart70f3c072010-12-15 17:57:33 -05004921 phba->sli4_hba.link_state.speed,
4922 phba->sli4_hba.link_state.topology,
4923 phba->sli4_hba.link_state.status,
4924 phba->sli4_hba.link_state.type,
4925 phba->sli4_hba.link_state.number,
James Smart8b68cd52012-09-29 11:32:37 -04004926 phba->sli4_hba.link_state.logical_speed,
James Smart70f3c072010-12-15 17:57:33 -05004927 phba->sli4_hba.link_state.fault);
James Smart76a95d72010-11-20 23:11:48 -05004928 /*
4929 * For FC Mode: issue the READ_TOPOLOGY mailbox command to fetch
4930 * topology info. Note: Optional for non FC-AL ports.
4931 */
4932 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
4933 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
4934 if (rc == MBX_NOT_FINISHED)
4935 goto out_free_dmabuf;
4936 return;
4937 }
4938 /*
4939 * For FCoE Mode: fill in all the topology information we need and call
4940 * the READ_TOPOLOGY completion routine to continue without actually
4941 * sending the READ_TOPOLOGY mailbox command to the port.
4942 */
James Smart23288b72018-05-04 20:37:53 -07004943 /* Initialize completion status */
James Smart76a95d72010-11-20 23:11:48 -05004944 mb = &pmb->u.mb;
James Smart23288b72018-05-04 20:37:53 -07004945 mb->mbxStatus = MBX_SUCCESS;
4946
4947 /* Parse port fault information field */
4948 lpfc_sli4_parse_latt_fault(phba, acqe_link);
James Smart76a95d72010-11-20 23:11:48 -05004949
4950 /* Parse and translate link attention fields */
4951 la = (struct lpfc_mbx_read_top *) &pmb->u.mb.un.varReadTop;
4952 la->eventTag = acqe_link->event_tag;
4953 bf_set(lpfc_mbx_read_top_att_type, la, att_type);
4954 bf_set(lpfc_mbx_read_top_link_spd, la,
James Smarta085e872015-12-16 18:12:02 -05004955 (bf_get(lpfc_acqe_link_speed, acqe_link)));
James Smart76a95d72010-11-20 23:11:48 -05004956
4957 /* Fake the the following irrelvant fields */
4958 bf_set(lpfc_mbx_read_top_topology, la, LPFC_TOPOLOGY_PT_PT);
4959 bf_set(lpfc_mbx_read_top_alpa_granted, la, 0);
4960 bf_set(lpfc_mbx_read_top_il, la, 0);
4961 bf_set(lpfc_mbx_read_top_pb, la, 0);
4962 bf_set(lpfc_mbx_read_top_fa, la, 0);
4963 bf_set(lpfc_mbx_read_top_mm, la, 0);
James Smartda0436e2009-05-22 14:51:39 -04004964
4965 /* Invoke the lpfc_handle_latt mailbox command callback function */
James Smart76a95d72010-11-20 23:11:48 -05004966 lpfc_mbx_cmpl_read_topology(phba, pmb);
James Smartda0436e2009-05-22 14:51:39 -04004967
4968 return;
4969
4970out_free_dmabuf:
4971 kfree(mp);
4972out_free_pmb:
4973 mempool_free(pmb, phba->mbox_mem_pool);
4974}
4975
4976/**
James Smart1dc5ec22018-10-23 13:41:11 -07004977 * lpfc_async_link_speed_to_read_top - Parse async evt link speed code to read
4978 * topology.
4979 * @phba: pointer to lpfc hba data structure.
4980 * @evt_code: asynchronous event code.
4981 * @speed_code: asynchronous event link speed code.
4982 *
4983 * This routine is to parse the giving SLI4 async event link speed code into
4984 * value of Read topology link speed.
4985 *
4986 * Return: link speed in terms of Read topology.
4987 **/
4988static uint8_t
4989lpfc_async_link_speed_to_read_top(struct lpfc_hba *phba, uint8_t speed_code)
4990{
4991 uint8_t port_speed;
4992
4993 switch (speed_code) {
4994 case LPFC_FC_LA_SPEED_1G:
4995 port_speed = LPFC_LINK_SPEED_1GHZ;
4996 break;
4997 case LPFC_FC_LA_SPEED_2G:
4998 port_speed = LPFC_LINK_SPEED_2GHZ;
4999 break;
5000 case LPFC_FC_LA_SPEED_4G:
5001 port_speed = LPFC_LINK_SPEED_4GHZ;
5002 break;
5003 case LPFC_FC_LA_SPEED_8G:
5004 port_speed = LPFC_LINK_SPEED_8GHZ;
5005 break;
5006 case LPFC_FC_LA_SPEED_16G:
5007 port_speed = LPFC_LINK_SPEED_16GHZ;
5008 break;
5009 case LPFC_FC_LA_SPEED_32G:
5010 port_speed = LPFC_LINK_SPEED_32GHZ;
5011 break;
5012 case LPFC_FC_LA_SPEED_64G:
5013 port_speed = LPFC_LINK_SPEED_64GHZ;
5014 break;
5015 case LPFC_FC_LA_SPEED_128G:
5016 port_speed = LPFC_LINK_SPEED_128GHZ;
5017 break;
5018 case LPFC_FC_LA_SPEED_256G:
5019 port_speed = LPFC_LINK_SPEED_256GHZ;
5020 break;
5021 default:
5022 port_speed = 0;
5023 break;
5024 }
5025
5026 return port_speed;
5027}
5028
5029#define trunk_link_status(__idx)\
5030 bf_get(lpfc_acqe_fc_la_trunk_config_port##__idx, acqe_fc) ?\
5031 ((phba->trunk_link.link##__idx.state == LPFC_LINK_UP) ?\
5032 "Link up" : "Link down") : "NA"
5033/* Did port __idx reported an error */
5034#define trunk_port_fault(__idx)\
5035 bf_get(lpfc_acqe_fc_la_trunk_config_port##__idx, acqe_fc) ?\
5036 (port_fault & (1 << __idx) ? "YES" : "NO") : "NA"
5037
5038static void
5039lpfc_update_trunk_link_status(struct lpfc_hba *phba,
5040 struct lpfc_acqe_fc_la *acqe_fc)
5041{
5042 uint8_t port_fault = bf_get(lpfc_acqe_fc_la_trunk_linkmask, acqe_fc);
5043 uint8_t err = bf_get(lpfc_acqe_fc_la_trunk_fault, acqe_fc);
5044
5045 phba->sli4_hba.link_state.speed =
5046 lpfc_sli4_port_speed_parse(phba, LPFC_TRAILER_CODE_FC,
5047 bf_get(lpfc_acqe_fc_la_speed, acqe_fc));
5048
5049 phba->sli4_hba.link_state.logical_speed =
5050 bf_get(lpfc_acqe_fc_la_llink_spd, acqe_fc);
5051 /* We got FC link speed, convert to fc_linkspeed (READ_TOPOLOGY) */
5052 phba->fc_linkspeed =
5053 lpfc_async_link_speed_to_read_top(
5054 phba,
5055 bf_get(lpfc_acqe_fc_la_speed, acqe_fc));
5056
5057 if (bf_get(lpfc_acqe_fc_la_trunk_config_port0, acqe_fc)) {
5058 phba->trunk_link.link0.state =
5059 bf_get(lpfc_acqe_fc_la_trunk_link_status_port0, acqe_fc)
5060 ? LPFC_LINK_UP : LPFC_LINK_DOWN;
James Smart529b3dd2018-12-13 15:17:54 -08005061 phba->trunk_link.link0.fault = port_fault & 0x1 ? err : 0;
James Smart1dc5ec22018-10-23 13:41:11 -07005062 }
5063 if (bf_get(lpfc_acqe_fc_la_trunk_config_port1, acqe_fc)) {
5064 phba->trunk_link.link1.state =
5065 bf_get(lpfc_acqe_fc_la_trunk_link_status_port1, acqe_fc)
5066 ? LPFC_LINK_UP : LPFC_LINK_DOWN;
James Smart529b3dd2018-12-13 15:17:54 -08005067 phba->trunk_link.link1.fault = port_fault & 0x2 ? err : 0;
James Smart1dc5ec22018-10-23 13:41:11 -07005068 }
5069 if (bf_get(lpfc_acqe_fc_la_trunk_config_port2, acqe_fc)) {
5070 phba->trunk_link.link2.state =
5071 bf_get(lpfc_acqe_fc_la_trunk_link_status_port2, acqe_fc)
5072 ? LPFC_LINK_UP : LPFC_LINK_DOWN;
James Smart529b3dd2018-12-13 15:17:54 -08005073 phba->trunk_link.link2.fault = port_fault & 0x4 ? err : 0;
James Smart1dc5ec22018-10-23 13:41:11 -07005074 }
5075 if (bf_get(lpfc_acqe_fc_la_trunk_config_port3, acqe_fc)) {
5076 phba->trunk_link.link3.state =
5077 bf_get(lpfc_acqe_fc_la_trunk_link_status_port3, acqe_fc)
5078 ? LPFC_LINK_UP : LPFC_LINK_DOWN;
James Smart529b3dd2018-12-13 15:17:54 -08005079 phba->trunk_link.link3.fault = port_fault & 0x8 ? err : 0;
James Smart1dc5ec22018-10-23 13:41:11 -07005080 }
5081
5082 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5083 "2910 Async FC Trunking Event - Speed:%d\n"
5084 "\tLogical speed:%d "
5085 "port0: %s port1: %s port2: %s port3: %s\n",
5086 phba->sli4_hba.link_state.speed,
5087 phba->sli4_hba.link_state.logical_speed,
5088 trunk_link_status(0), trunk_link_status(1),
5089 trunk_link_status(2), trunk_link_status(3));
5090
5091 if (port_fault)
5092 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5093 "3202 trunk error:0x%x (%s) seen on port0:%s "
5094 /*
5095 * SLI-4: We have only 0xA error codes
5096 * defined as of now. print an appropriate
5097 * message in case driver needs to be updated.
5098 */
5099 "port1:%s port2:%s port3:%s\n", err, err > 0xA ?
5100 "UNDEFINED. update driver." : trunk_errmsg[err],
5101 trunk_port_fault(0), trunk_port_fault(1),
5102 trunk_port_fault(2), trunk_port_fault(3));
5103}
5104
5105
5106/**
James Smart70f3c072010-12-15 17:57:33 -05005107 * lpfc_sli4_async_fc_evt - Process the asynchronous FC link event
5108 * @phba: pointer to lpfc hba data structure.
5109 * @acqe_fc: pointer to the async fc completion queue entry.
5110 *
5111 * This routine is to handle the SLI4 asynchronous FC event. It will simply log
5112 * that the event was received and then issue a read_topology mailbox command so
5113 * that the rest of the driver will treat it the same as SLI3.
5114 **/
5115static void
5116lpfc_sli4_async_fc_evt(struct lpfc_hba *phba, struct lpfc_acqe_fc_la *acqe_fc)
5117{
5118 struct lpfc_dmabuf *mp;
5119 LPFC_MBOXQ_t *pmb;
James Smart7bdedb32016-07-06 12:36:00 -07005120 MAILBOX_t *mb;
5121 struct lpfc_mbx_read_top *la;
James Smart70f3c072010-12-15 17:57:33 -05005122 int rc;
5123
5124 if (bf_get(lpfc_trailer_type, acqe_fc) !=
5125 LPFC_FC_LA_EVENT_TYPE_FC_LINK) {
5126 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5127 "2895 Non FC link Event detected.(%d)\n",
5128 bf_get(lpfc_trailer_type, acqe_fc));
5129 return;
5130 }
James Smart1dc5ec22018-10-23 13:41:11 -07005131
5132 if (bf_get(lpfc_acqe_fc_la_att_type, acqe_fc) ==
5133 LPFC_FC_LA_TYPE_TRUNKING_EVENT) {
5134 lpfc_update_trunk_link_status(phba, acqe_fc);
5135 return;
5136 }
5137
James Smart70f3c072010-12-15 17:57:33 -05005138 /* Keep the link status for extra SLI4 state machine reference */
5139 phba->sli4_hba.link_state.speed =
James Smart8b68cd52012-09-29 11:32:37 -04005140 lpfc_sli4_port_speed_parse(phba, LPFC_TRAILER_CODE_FC,
5141 bf_get(lpfc_acqe_fc_la_speed, acqe_fc));
James Smart70f3c072010-12-15 17:57:33 -05005142 phba->sli4_hba.link_state.duplex = LPFC_ASYNC_LINK_DUPLEX_FULL;
5143 phba->sli4_hba.link_state.topology =
5144 bf_get(lpfc_acqe_fc_la_topology, acqe_fc);
5145 phba->sli4_hba.link_state.status =
5146 bf_get(lpfc_acqe_fc_la_att_type, acqe_fc);
5147 phba->sli4_hba.link_state.type =
5148 bf_get(lpfc_acqe_fc_la_port_type, acqe_fc);
5149 phba->sli4_hba.link_state.number =
5150 bf_get(lpfc_acqe_fc_la_port_number, acqe_fc);
5151 phba->sli4_hba.link_state.fault =
5152 bf_get(lpfc_acqe_link_fault, acqe_fc);
5153 phba->sli4_hba.link_state.logical_speed =
James Smart8b68cd52012-09-29 11:32:37 -04005154 bf_get(lpfc_acqe_fc_la_llink_spd, acqe_fc) * 10;
James Smart70f3c072010-12-15 17:57:33 -05005155 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5156 "2896 Async FC event - Speed:%dGBaud Topology:x%x "
5157 "LA Type:x%x Port Type:%d Port Number:%d Logical speed:"
5158 "%dMbps Fault:%d\n",
5159 phba->sli4_hba.link_state.speed,
5160 phba->sli4_hba.link_state.topology,
5161 phba->sli4_hba.link_state.status,
5162 phba->sli4_hba.link_state.type,
5163 phba->sli4_hba.link_state.number,
James Smart8b68cd52012-09-29 11:32:37 -04005164 phba->sli4_hba.link_state.logical_speed,
James Smart70f3c072010-12-15 17:57:33 -05005165 phba->sli4_hba.link_state.fault);
5166 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5167 if (!pmb) {
5168 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5169 "2897 The mboxq allocation failed\n");
5170 return;
5171 }
5172 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5173 if (!mp) {
5174 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5175 "2898 The lpfc_dmabuf allocation failed\n");
5176 goto out_free_pmb;
5177 }
5178 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
5179 if (!mp->virt) {
5180 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5181 "2899 The mbuf allocation failed\n");
5182 goto out_free_dmabuf;
5183 }
5184
5185 /* Cleanup any outstanding ELS commands */
5186 lpfc_els_flush_all_cmd(phba);
5187
5188 /* Block ELS IOCBs until we have done process link event */
James Smart895427b2017-02-12 13:52:30 -08005189 phba->sli4_hba.els_wq->pring->flag |= LPFC_STOP_IOCB_EVENT;
James Smart70f3c072010-12-15 17:57:33 -05005190
5191 /* Update link event statistics */
5192 phba->sli.slistat.link_event++;
5193
5194 /* Create lpfc_handle_latt mailbox command from link ACQE */
5195 lpfc_read_topology(phba, pmb, mp);
5196 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
5197 pmb->vport = phba->pport;
5198
James Smart7bdedb32016-07-06 12:36:00 -07005199 if (phba->sli4_hba.link_state.status != LPFC_FC_LA_TYPE_LINK_UP) {
James Smartae9e28f2017-05-15 15:20:51 -07005200 phba->link_flag &= ~(LS_MDS_LINK_DOWN | LS_MDS_LOOPBACK);
5201
5202 switch (phba->sli4_hba.link_state.status) {
5203 case LPFC_FC_LA_TYPE_MDS_LINK_DOWN:
5204 phba->link_flag |= LS_MDS_LINK_DOWN;
5205 break;
5206 case LPFC_FC_LA_TYPE_MDS_LOOPBACK:
5207 phba->link_flag |= LS_MDS_LOOPBACK;
5208 break;
5209 default:
5210 break;
5211 }
5212
James Smart23288b72018-05-04 20:37:53 -07005213 /* Initialize completion status */
James Smart7bdedb32016-07-06 12:36:00 -07005214 mb = &pmb->u.mb;
James Smart23288b72018-05-04 20:37:53 -07005215 mb->mbxStatus = MBX_SUCCESS;
5216
5217 /* Parse port fault information field */
5218 lpfc_sli4_parse_latt_fault(phba, (void *)acqe_fc);
James Smart7bdedb32016-07-06 12:36:00 -07005219
5220 /* Parse and translate link attention fields */
5221 la = (struct lpfc_mbx_read_top *)&pmb->u.mb.un.varReadTop;
5222 la->eventTag = acqe_fc->event_tag;
James Smart7bdedb32016-07-06 12:36:00 -07005223
James Smartaeb3c812017-04-21 16:05:02 -07005224 if (phba->sli4_hba.link_state.status ==
5225 LPFC_FC_LA_TYPE_UNEXP_WWPN) {
5226 bf_set(lpfc_mbx_read_top_att_type, la,
5227 LPFC_FC_LA_TYPE_UNEXP_WWPN);
5228 } else {
5229 bf_set(lpfc_mbx_read_top_att_type, la,
5230 LPFC_FC_LA_TYPE_LINK_DOWN);
5231 }
James Smart7bdedb32016-07-06 12:36:00 -07005232 /* Invoke the mailbox command callback function */
5233 lpfc_mbx_cmpl_read_topology(phba, pmb);
5234
5235 return;
5236 }
5237
James Smart70f3c072010-12-15 17:57:33 -05005238 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
5239 if (rc == MBX_NOT_FINISHED)
5240 goto out_free_dmabuf;
5241 return;
5242
5243out_free_dmabuf:
5244 kfree(mp);
5245out_free_pmb:
5246 mempool_free(pmb, phba->mbox_mem_pool);
5247}
5248
5249/**
5250 * lpfc_sli4_async_sli_evt - Process the asynchronous SLI link event
5251 * @phba: pointer to lpfc hba data structure.
5252 * @acqe_fc: pointer to the async SLI completion queue entry.
5253 *
5254 * This routine is to handle the SLI4 asynchronous SLI events.
5255 **/
5256static void
5257lpfc_sli4_async_sli_evt(struct lpfc_hba *phba, struct lpfc_acqe_sli *acqe_sli)
5258{
James Smart4b8bae02012-06-12 13:55:07 -04005259 char port_name;
James Smart8c1312e2012-10-31 14:45:09 -04005260 char message[128];
James Smart4b8bae02012-06-12 13:55:07 -04005261 uint8_t status;
James Smart946727d2015-04-07 15:07:09 -04005262 uint8_t evt_type;
James Smart448193b2015-12-16 18:12:05 -05005263 uint8_t operational = 0;
James Smart946727d2015-04-07 15:07:09 -04005264 struct temp_event temp_event_data;
James Smart4b8bae02012-06-12 13:55:07 -04005265 struct lpfc_acqe_misconfigured_event *misconfigured;
James Smart946727d2015-04-07 15:07:09 -04005266 struct Scsi_Host *shost;
James Smartcd713482018-10-23 13:41:01 -07005267 struct lpfc_vport **vports;
5268 int rc, i;
James Smart4b8bae02012-06-12 13:55:07 -04005269
James Smart946727d2015-04-07 15:07:09 -04005270 evt_type = bf_get(lpfc_trailer_type, acqe_sli);
5271
James Smart448193b2015-12-16 18:12:05 -05005272 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5273 "2901 Async SLI event - Event Data1:x%08x Event Data2:"
5274 "x%08x SLI Event Type:%d\n",
5275 acqe_sli->event_data1, acqe_sli->event_data2,
5276 evt_type);
James Smart4b8bae02012-06-12 13:55:07 -04005277
5278 port_name = phba->Port[0];
5279 if (port_name == 0x00)
5280 port_name = '?'; /* get port name is empty */
5281
James Smart946727d2015-04-07 15:07:09 -04005282 switch (evt_type) {
5283 case LPFC_SLI_EVENT_TYPE_OVER_TEMP:
5284 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
5285 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
5286 temp_event_data.data = (uint32_t)acqe_sli->event_data1;
5287
5288 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5289 "3190 Over Temperature:%d Celsius- Port Name %c\n",
5290 acqe_sli->event_data1, port_name);
5291
James Smart310429e2016-07-06 12:35:54 -07005292 phba->sfp_warning |= LPFC_TRANSGRESSION_HIGH_TEMPERATURE;
James Smart946727d2015-04-07 15:07:09 -04005293 shost = lpfc_shost_from_vport(phba->pport);
5294 fc_host_post_vendor_event(shost, fc_get_event_number(),
5295 sizeof(temp_event_data),
5296 (char *)&temp_event_data,
5297 SCSI_NL_VID_TYPE_PCI
5298 | PCI_VENDOR_ID_EMULEX);
5299 break;
5300 case LPFC_SLI_EVENT_TYPE_NORM_TEMP:
5301 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
5302 temp_event_data.event_code = LPFC_NORMAL_TEMP;
5303 temp_event_data.data = (uint32_t)acqe_sli->event_data1;
5304
5305 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5306 "3191 Normal Temperature:%d Celsius - Port Name %c\n",
5307 acqe_sli->event_data1, port_name);
5308
5309 shost = lpfc_shost_from_vport(phba->pport);
5310 fc_host_post_vendor_event(shost, fc_get_event_number(),
5311 sizeof(temp_event_data),
5312 (char *)&temp_event_data,
5313 SCSI_NL_VID_TYPE_PCI
5314 | PCI_VENDOR_ID_EMULEX);
5315 break;
5316 case LPFC_SLI_EVENT_TYPE_MISCONFIGURED:
5317 misconfigured = (struct lpfc_acqe_misconfigured_event *)
James Smart4b8bae02012-06-12 13:55:07 -04005318 &acqe_sli->event_data1;
5319
James Smart946727d2015-04-07 15:07:09 -04005320 /* fetch the status for this port */
5321 switch (phba->sli4_hba.lnk_info.lnk_no) {
5322 case LPFC_LINK_NUMBER_0:
James Smart448193b2015-12-16 18:12:05 -05005323 status = bf_get(lpfc_sli_misconfigured_port0_state,
5324 &misconfigured->theEvent);
5325 operational = bf_get(lpfc_sli_misconfigured_port0_op,
James Smart4b8bae02012-06-12 13:55:07 -04005326 &misconfigured->theEvent);
James Smart946727d2015-04-07 15:07:09 -04005327 break;
5328 case LPFC_LINK_NUMBER_1:
James Smart448193b2015-12-16 18:12:05 -05005329 status = bf_get(lpfc_sli_misconfigured_port1_state,
5330 &misconfigured->theEvent);
5331 operational = bf_get(lpfc_sli_misconfigured_port1_op,
James Smart4b8bae02012-06-12 13:55:07 -04005332 &misconfigured->theEvent);
James Smart946727d2015-04-07 15:07:09 -04005333 break;
5334 case LPFC_LINK_NUMBER_2:
James Smart448193b2015-12-16 18:12:05 -05005335 status = bf_get(lpfc_sli_misconfigured_port2_state,
5336 &misconfigured->theEvent);
5337 operational = bf_get(lpfc_sli_misconfigured_port2_op,
James Smart4b8bae02012-06-12 13:55:07 -04005338 &misconfigured->theEvent);
James Smart946727d2015-04-07 15:07:09 -04005339 break;
5340 case LPFC_LINK_NUMBER_3:
James Smart448193b2015-12-16 18:12:05 -05005341 status = bf_get(lpfc_sli_misconfigured_port3_state,
5342 &misconfigured->theEvent);
5343 operational = bf_get(lpfc_sli_misconfigured_port3_op,
James Smart4b8bae02012-06-12 13:55:07 -04005344 &misconfigured->theEvent);
James Smart946727d2015-04-07 15:07:09 -04005345 break;
5346 default:
James Smart448193b2015-12-16 18:12:05 -05005347 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5348 "3296 "
5349 "LPFC_SLI_EVENT_TYPE_MISCONFIGURED "
5350 "event: Invalid link %d",
5351 phba->sli4_hba.lnk_info.lnk_no);
5352 return;
James Smart946727d2015-04-07 15:07:09 -04005353 }
James Smart4b8bae02012-06-12 13:55:07 -04005354
James Smart448193b2015-12-16 18:12:05 -05005355 /* Skip if optic state unchanged */
5356 if (phba->sli4_hba.lnk_info.optic_state == status)
5357 return;
5358
James Smart946727d2015-04-07 15:07:09 -04005359 switch (status) {
5360 case LPFC_SLI_EVENT_STATUS_VALID:
James Smart448193b2015-12-16 18:12:05 -05005361 sprintf(message, "Physical Link is functional");
5362 break;
James Smart946727d2015-04-07 15:07:09 -04005363 case LPFC_SLI_EVENT_STATUS_NOT_PRESENT:
5364 sprintf(message, "Optics faulted/incorrectly "
5365 "installed/not installed - Reseat optics, "
5366 "if issue not resolved, replace.");
5367 break;
5368 case LPFC_SLI_EVENT_STATUS_WRONG_TYPE:
5369 sprintf(message,
5370 "Optics of two types installed - Remove one "
5371 "optic or install matching pair of optics.");
5372 break;
5373 case LPFC_SLI_EVENT_STATUS_UNSUPPORTED:
5374 sprintf(message, "Incompatible optics - Replace with "
James Smart292098b2012-09-29 11:31:41 -04005375 "compatible optics for card to function.");
James Smart946727d2015-04-07 15:07:09 -04005376 break;
James Smart448193b2015-12-16 18:12:05 -05005377 case LPFC_SLI_EVENT_STATUS_UNQUALIFIED:
5378 sprintf(message, "Unqualified optics - Replace with "
5379 "Avago optics for Warranty and Technical "
5380 "Support - Link is%s operational",
James Smart2ea259e2017-02-12 13:52:27 -08005381 (operational) ? " not" : "");
James Smart448193b2015-12-16 18:12:05 -05005382 break;
5383 case LPFC_SLI_EVENT_STATUS_UNCERTIFIED:
5384 sprintf(message, "Uncertified optics - Replace with "
5385 "Avago-certified optics to enable link "
5386 "operation - Link is%s operational",
James Smart2ea259e2017-02-12 13:52:27 -08005387 (operational) ? " not" : "");
James Smart448193b2015-12-16 18:12:05 -05005388 break;
James Smart946727d2015-04-07 15:07:09 -04005389 default:
5390 /* firmware is reporting a status we don't know about */
5391 sprintf(message, "Unknown event status x%02x", status);
5392 break;
5393 }
James Smartcd713482018-10-23 13:41:01 -07005394
5395 /* Issue READ_CONFIG mbox command to refresh supported speeds */
5396 rc = lpfc_sli4_read_config(phba);
James Smart3952e912018-10-23 13:41:02 -07005397 if (rc) {
James Smartcd713482018-10-23 13:41:01 -07005398 phba->lmt = 0;
5399 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5400 "3194 Unable to retrieve supported "
James Smart3952e912018-10-23 13:41:02 -07005401 "speeds, rc = 0x%x\n", rc);
James Smartcd713482018-10-23 13:41:01 -07005402 }
5403 vports = lpfc_create_vport_work_array(phba);
5404 if (vports != NULL) {
5405 for (i = 0; i <= phba->max_vports && vports[i] != NULL;
5406 i++) {
5407 shost = lpfc_shost_from_vport(vports[i]);
5408 lpfc_host_supported_speeds_set(shost);
5409 }
5410 }
5411 lpfc_destroy_vport_work_array(phba, vports);
5412
James Smart448193b2015-12-16 18:12:05 -05005413 phba->sli4_hba.lnk_info.optic_state = status;
James Smart946727d2015-04-07 15:07:09 -04005414 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart448193b2015-12-16 18:12:05 -05005415 "3176 Port Name %c %s\n", port_name, message);
James Smart946727d2015-04-07 15:07:09 -04005416 break;
5417 case LPFC_SLI_EVENT_TYPE_REMOTE_DPORT:
5418 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5419 "3192 Remote DPort Test Initiated - "
5420 "Event Data1:x%08x Event Data2: x%08x\n",
5421 acqe_sli->event_data1, acqe_sli->event_data2);
James Smart4b8bae02012-06-12 13:55:07 -04005422 break;
5423 default:
James Smart946727d2015-04-07 15:07:09 -04005424 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5425 "3193 Async SLI event - Event Data1:x%08x Event Data2:"
5426 "x%08x SLI Event Type:%d\n",
5427 acqe_sli->event_data1, acqe_sli->event_data2,
5428 evt_type);
James Smart4b8bae02012-06-12 13:55:07 -04005429 break;
5430 }
James Smart70f3c072010-12-15 17:57:33 -05005431}
5432
5433/**
James Smartfc2b9892010-02-26 14:15:29 -05005434 * lpfc_sli4_perform_vport_cvl - Perform clear virtual link on a vport
5435 * @vport: pointer to vport data structure.
5436 *
5437 * This routine is to perform Clear Virtual Link (CVL) on a vport in
5438 * response to a CVL event.
5439 *
5440 * Return the pointer to the ndlp with the vport if successful, otherwise
5441 * return NULL.
5442 **/
5443static struct lpfc_nodelist *
5444lpfc_sli4_perform_vport_cvl(struct lpfc_vport *vport)
5445{
5446 struct lpfc_nodelist *ndlp;
5447 struct Scsi_Host *shost;
5448 struct lpfc_hba *phba;
5449
5450 if (!vport)
5451 return NULL;
James Smartfc2b9892010-02-26 14:15:29 -05005452 phba = vport->phba;
5453 if (!phba)
5454 return NULL;
James Smart78730cf2010-04-06 15:06:30 -04005455 ndlp = lpfc_findnode_did(vport, Fabric_DID);
5456 if (!ndlp) {
5457 /* Cannot find existing Fabric ndlp, so allocate a new one */
James Smart9d3d3402017-04-21 16:05:00 -07005458 ndlp = lpfc_nlp_init(vport, Fabric_DID);
James Smart78730cf2010-04-06 15:06:30 -04005459 if (!ndlp)
5460 return 0;
James Smart78730cf2010-04-06 15:06:30 -04005461 /* Set the node type */
5462 ndlp->nlp_type |= NLP_FABRIC;
5463 /* Put ndlp onto node list */
5464 lpfc_enqueue_node(vport, ndlp);
5465 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
5466 /* re-setup ndlp without removing from node list */
5467 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
5468 if (!ndlp)
5469 return 0;
5470 }
James Smart63e801c2010-11-20 23:14:19 -05005471 if ((phba->pport->port_state < LPFC_FLOGI) &&
5472 (phba->pport->port_state != LPFC_VPORT_FAILED))
James Smartfc2b9892010-02-26 14:15:29 -05005473 return NULL;
5474 /* If virtual link is not yet instantiated ignore CVL */
James Smart63e801c2010-11-20 23:14:19 -05005475 if ((vport != phba->pport) && (vport->port_state < LPFC_FDISC)
5476 && (vport->port_state != LPFC_VPORT_FAILED))
James Smartfc2b9892010-02-26 14:15:29 -05005477 return NULL;
5478 shost = lpfc_shost_from_vport(vport);
5479 if (!shost)
5480 return NULL;
5481 lpfc_linkdown_port(vport);
5482 lpfc_cleanup_pending_mbox(vport);
5483 spin_lock_irq(shost->host_lock);
5484 vport->fc_flag |= FC_VPORT_CVL_RCVD;
5485 spin_unlock_irq(shost->host_lock);
5486
5487 return ndlp;
5488}
5489
5490/**
5491 * lpfc_sli4_perform_all_vport_cvl - Perform clear virtual link on all vports
5492 * @vport: pointer to lpfc hba data structure.
5493 *
5494 * This routine is to perform Clear Virtual Link (CVL) on all vports in
5495 * response to a FCF dead event.
5496 **/
5497static void
5498lpfc_sli4_perform_all_vport_cvl(struct lpfc_hba *phba)
5499{
5500 struct lpfc_vport **vports;
5501 int i;
5502
5503 vports = lpfc_create_vport_work_array(phba);
5504 if (vports)
5505 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
5506 lpfc_sli4_perform_vport_cvl(vports[i]);
5507 lpfc_destroy_vport_work_array(phba, vports);
5508}
5509
5510/**
James Smart76a95d72010-11-20 23:11:48 -05005511 * lpfc_sli4_async_fip_evt - Process the asynchronous FCoE FIP event
James Smartda0436e2009-05-22 14:51:39 -04005512 * @phba: pointer to lpfc hba data structure.
5513 * @acqe_link: pointer to the async fcoe completion queue entry.
5514 *
5515 * This routine is to handle the SLI4 asynchronous fcoe event.
5516 **/
5517static void
James Smart76a95d72010-11-20 23:11:48 -05005518lpfc_sli4_async_fip_evt(struct lpfc_hba *phba,
James Smart70f3c072010-12-15 17:57:33 -05005519 struct lpfc_acqe_fip *acqe_fip)
James Smartda0436e2009-05-22 14:51:39 -04005520{
James Smart70f3c072010-12-15 17:57:33 -05005521 uint8_t event_type = bf_get(lpfc_trailer_type, acqe_fip);
James Smartda0436e2009-05-22 14:51:39 -04005522 int rc;
James Smart6669f9b2009-10-02 15:16:45 -04005523 struct lpfc_vport *vport;
5524 struct lpfc_nodelist *ndlp;
5525 struct Scsi_Host *shost;
James Smart695a8142010-01-26 23:08:03 -05005526 int active_vlink_present;
5527 struct lpfc_vport **vports;
5528 int i;
James Smartda0436e2009-05-22 14:51:39 -04005529
James Smart70f3c072010-12-15 17:57:33 -05005530 phba->fc_eventTag = acqe_fip->event_tag;
5531 phba->fcoe_eventtag = acqe_fip->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04005532 switch (event_type) {
James Smart70f3c072010-12-15 17:57:33 -05005533 case LPFC_FIP_EVENT_TYPE_NEW_FCF:
5534 case LPFC_FIP_EVENT_TYPE_FCF_PARAM_MOD:
5535 if (event_type == LPFC_FIP_EVENT_TYPE_NEW_FCF)
James Smart999d8132010-03-15 11:24:56 -04005536 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
5537 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04005538 "2546 New FCF event, evt_tag:x%x, "
5539 "index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05005540 acqe_fip->event_tag,
5541 acqe_fip->index);
James Smart999d8132010-03-15 11:24:56 -04005542 else
5543 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP |
5544 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04005545 "2788 FCF param modified event, "
5546 "evt_tag:x%x, index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05005547 acqe_fip->event_tag,
5548 acqe_fip->index);
James Smart38b92ef2010-08-04 16:11:39 -04005549 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smart0c9ab6f2010-02-26 14:15:57 -05005550 /*
5551 * During period of FCF discovery, read the FCF
5552 * table record indexed by the event to update
James Smarta93ff372010-10-22 11:06:08 -04005553 * FCF roundrobin failover eligible FCF bmask.
James Smart0c9ab6f2010-02-26 14:15:57 -05005554 */
5555 lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
5556 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04005557 "2779 Read FCF (x%x) for updating "
5558 "roundrobin FCF failover bmask\n",
James Smart70f3c072010-12-15 17:57:33 -05005559 acqe_fip->index);
5560 rc = lpfc_sli4_read_fcf_rec(phba, acqe_fip->index);
James Smart0c9ab6f2010-02-26 14:15:57 -05005561 }
James Smart38b92ef2010-08-04 16:11:39 -04005562
5563 /* If the FCF discovery is in progress, do nothing. */
James Smart3804dc82010-07-14 15:31:37 -04005564 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04005565 if (phba->hba_flag & FCF_TS_INPROG) {
James Smart38b92ef2010-08-04 16:11:39 -04005566 spin_unlock_irq(&phba->hbalock);
5567 break;
5568 }
5569 /* If fast FCF failover rescan event is pending, do nothing */
James Smart036cad12018-10-23 13:41:06 -07005570 if (phba->fcf.fcf_flag & (FCF_REDISC_EVT | FCF_REDISC_PEND)) {
James Smart38b92ef2010-08-04 16:11:39 -04005571 spin_unlock_irq(&phba->hbalock);
5572 break;
5573 }
5574
James Smartc2b97122013-05-31 17:05:36 -04005575 /* If the FCF has been in discovered state, do nothing. */
5576 if (phba->fcf.fcf_flag & FCF_SCAN_DONE) {
James Smart3804dc82010-07-14 15:31:37 -04005577 spin_unlock_irq(&phba->hbalock);
5578 break;
5579 }
5580 spin_unlock_irq(&phba->hbalock);
James Smart38b92ef2010-08-04 16:11:39 -04005581
James Smart0c9ab6f2010-02-26 14:15:57 -05005582 /* Otherwise, scan the entire FCF table and re-discover SAN */
5583 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04005584 "2770 Start FCF table scan per async FCF "
5585 "event, evt_tag:x%x, index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05005586 acqe_fip->event_tag, acqe_fip->index);
James Smart0c9ab6f2010-02-26 14:15:57 -05005587 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba,
5588 LPFC_FCOE_FCF_GET_FIRST);
James Smartda0436e2009-05-22 14:51:39 -04005589 if (rc)
James Smart0c9ab6f2010-02-26 14:15:57 -05005590 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
5591 "2547 Issue FCF scan read FCF mailbox "
James Smarta93ff372010-10-22 11:06:08 -04005592 "command failed (x%x)\n", rc);
James Smartda0436e2009-05-22 14:51:39 -04005593 break;
5594
James Smart70f3c072010-12-15 17:57:33 -05005595 case LPFC_FIP_EVENT_TYPE_FCF_TABLE_FULL:
James Smartda0436e2009-05-22 14:51:39 -04005596 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte4e74272009-07-19 10:01:38 -04005597 "2548 FCF Table full count 0x%x tag 0x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05005598 bf_get(lpfc_acqe_fip_fcf_count, acqe_fip),
5599 acqe_fip->event_tag);
James Smartda0436e2009-05-22 14:51:39 -04005600 break;
5601
James Smart70f3c072010-12-15 17:57:33 -05005602 case LPFC_FIP_EVENT_TYPE_FCF_DEAD:
James Smart80c17842012-03-01 22:35:45 -05005603 phba->fcoe_cvl_eventtag = acqe_fip->event_tag;
James Smart0c9ab6f2010-02-26 14:15:57 -05005604 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04005605 "2549 FCF (x%x) disconnected from network, "
James Smart70f3c072010-12-15 17:57:33 -05005606 "tag:x%x\n", acqe_fip->index, acqe_fip->event_tag);
James Smart38b92ef2010-08-04 16:11:39 -04005607 /*
5608 * If we are in the middle of FCF failover process, clear
5609 * the corresponding FCF bit in the roundrobin bitmap.
James Smartda0436e2009-05-22 14:51:39 -04005610 */
James Smartfc2b9892010-02-26 14:15:29 -05005611 spin_lock_irq(&phba->hbalock);
James Smarta1cadfe2016-07-06 12:36:02 -07005612 if ((phba->fcf.fcf_flag & FCF_DISCOVERY) &&
5613 (phba->fcf.current_rec.fcf_indx != acqe_fip->index)) {
James Smartfc2b9892010-02-26 14:15:29 -05005614 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05005615 /* Update FLOGI FCF failover eligible FCF bmask */
James Smart70f3c072010-12-15 17:57:33 -05005616 lpfc_sli4_fcf_rr_index_clear(phba, acqe_fip->index);
James Smartfc2b9892010-02-26 14:15:29 -05005617 break;
5618 }
James Smart38b92ef2010-08-04 16:11:39 -04005619 spin_unlock_irq(&phba->hbalock);
5620
5621 /* If the event is not for currently used fcf do nothing */
James Smart70f3c072010-12-15 17:57:33 -05005622 if (phba->fcf.current_rec.fcf_indx != acqe_fip->index)
James Smart38b92ef2010-08-04 16:11:39 -04005623 break;
5624
5625 /*
5626 * Otherwise, request the port to rediscover the entire FCF
5627 * table for a fast recovery from case that the current FCF
5628 * is no longer valid as we are not in the middle of FCF
5629 * failover process already.
5630 */
James Smartc2b97122013-05-31 17:05:36 -04005631 spin_lock_irq(&phba->hbalock);
5632 /* Mark the fast failover process in progress */
5633 phba->fcf.fcf_flag |= FCF_DEAD_DISC;
5634 spin_unlock_irq(&phba->hbalock);
5635
5636 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
5637 "2771 Start FCF fast failover process due to "
5638 "FCF DEAD event: evt_tag:x%x, fcf_index:x%x "
5639 "\n", acqe_fip->event_tag, acqe_fip->index);
5640 rc = lpfc_sli4_redisc_fcf_table(phba);
5641 if (rc) {
5642 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
5643 LOG_DISCOVERY,
Colin Ian King7afc0ce2018-05-03 10:26:12 +01005644 "2772 Issue FCF rediscover mailbox "
James Smartc2b97122013-05-31 17:05:36 -04005645 "command failed, fail through to FCF "
5646 "dead event\n");
5647 spin_lock_irq(&phba->hbalock);
5648 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
5649 spin_unlock_irq(&phba->hbalock);
5650 /*
5651 * Last resort will fail over by treating this
5652 * as a link down to FCF registration.
5653 */
5654 lpfc_sli4_fcf_dead_failthrough(phba);
5655 } else {
5656 /* Reset FCF roundrobin bmask for new discovery */
5657 lpfc_sli4_clear_fcf_rr_bmask(phba);
5658 /*
5659 * Handling fast FCF failover to a DEAD FCF event is
5660 * considered equalivant to receiving CVL to all vports.
5661 */
5662 lpfc_sli4_perform_all_vport_cvl(phba);
5663 }
James Smartda0436e2009-05-22 14:51:39 -04005664 break;
James Smart70f3c072010-12-15 17:57:33 -05005665 case LPFC_FIP_EVENT_TYPE_CVL:
James Smart80c17842012-03-01 22:35:45 -05005666 phba->fcoe_cvl_eventtag = acqe_fip->event_tag;
James Smart0c9ab6f2010-02-26 14:15:57 -05005667 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
James Smart6669f9b2009-10-02 15:16:45 -04005668 "2718 Clear Virtual Link Received for VPI 0x%x"
James Smart70f3c072010-12-15 17:57:33 -05005669 " tag 0x%x\n", acqe_fip->index, acqe_fip->event_tag);
James Smart6d368e52011-05-24 11:44:12 -04005670
James Smart6669f9b2009-10-02 15:16:45 -04005671 vport = lpfc_find_vport_by_vpid(phba,
James Smart5248a742011-07-22 18:37:06 -04005672 acqe_fip->index);
James Smartfc2b9892010-02-26 14:15:29 -05005673 ndlp = lpfc_sli4_perform_vport_cvl(vport);
James Smart6669f9b2009-10-02 15:16:45 -04005674 if (!ndlp)
5675 break;
James Smart695a8142010-01-26 23:08:03 -05005676 active_vlink_present = 0;
5677
5678 vports = lpfc_create_vport_work_array(phba);
5679 if (vports) {
5680 for (i = 0; i <= phba->max_vports && vports[i] != NULL;
5681 i++) {
5682 if ((!(vports[i]->fc_flag &
5683 FC_VPORT_CVL_RCVD)) &&
5684 (vports[i]->port_state > LPFC_FDISC)) {
5685 active_vlink_present = 1;
5686 break;
5687 }
5688 }
5689 lpfc_destroy_vport_work_array(phba, vports);
5690 }
5691
James Smartcc823552015-05-21 13:55:26 -04005692 /*
5693 * Don't re-instantiate if vport is marked for deletion.
5694 * If we are here first then vport_delete is going to wait
5695 * for discovery to complete.
5696 */
5697 if (!(vport->load_flag & FC_UNLOADING) &&
5698 active_vlink_present) {
James Smart695a8142010-01-26 23:08:03 -05005699 /*
5700 * If there are other active VLinks present,
5701 * re-instantiate the Vlink using FDISC.
5702 */
James Smart256ec0d2013-04-17 20:14:58 -04005703 mod_timer(&ndlp->nlp_delayfunc,
5704 jiffies + msecs_to_jiffies(1000));
James Smartfc2b9892010-02-26 14:15:29 -05005705 shost = lpfc_shost_from_vport(vport);
James Smart6669f9b2009-10-02 15:16:45 -04005706 spin_lock_irq(shost->host_lock);
5707 ndlp->nlp_flag |= NLP_DELAY_TMO;
5708 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05005709 ndlp->nlp_last_elscmd = ELS_CMD_FDISC;
5710 vport->port_state = LPFC_FDISC;
5711 } else {
James Smartecfd03c2010-02-12 14:41:27 -05005712 /*
5713 * Otherwise, we request port to rediscover
5714 * the entire FCF table for a fast recovery
5715 * from possible case that the current FCF
James Smart0c9ab6f2010-02-26 14:15:57 -05005716 * is no longer valid if we are not already
5717 * in the FCF failover process.
James Smartecfd03c2010-02-12 14:41:27 -05005718 */
James Smartfc2b9892010-02-26 14:15:29 -05005719 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05005720 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smartfc2b9892010-02-26 14:15:29 -05005721 spin_unlock_irq(&phba->hbalock);
5722 break;
5723 }
5724 /* Mark the fast failover process in progress */
James Smart0c9ab6f2010-02-26 14:15:57 -05005725 phba->fcf.fcf_flag |= FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05005726 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05005727 lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
5728 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04005729 "2773 Start FCF failover per CVL, "
James Smart70f3c072010-12-15 17:57:33 -05005730 "evt_tag:x%x\n", acqe_fip->event_tag);
James Smartecfd03c2010-02-12 14:41:27 -05005731 rc = lpfc_sli4_redisc_fcf_table(phba);
James Smartfc2b9892010-02-26 14:15:29 -05005732 if (rc) {
James Smart0c9ab6f2010-02-26 14:15:57 -05005733 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
5734 LOG_DISCOVERY,
5735 "2774 Issue FCF rediscover "
Colin Ian King7afc0ce2018-05-03 10:26:12 +01005736 "mailbox command failed, "
James Smart0c9ab6f2010-02-26 14:15:57 -05005737 "through to CVL event\n");
James Smartfc2b9892010-02-26 14:15:29 -05005738 spin_lock_irq(&phba->hbalock);
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 Smartecfd03c2010-02-12 14:41:27 -05005741 /*
5742 * Last resort will be re-try on the
5743 * the current registered FCF entry.
5744 */
5745 lpfc_retry_pport_discovery(phba);
James Smart38b92ef2010-08-04 16:11:39 -04005746 } else
5747 /*
5748 * Reset FCF roundrobin bmask for new
5749 * discovery.
5750 */
James Smart7d791df2011-07-22 18:37:52 -04005751 lpfc_sli4_clear_fcf_rr_bmask(phba);
James Smart6669f9b2009-10-02 15:16:45 -04005752 }
5753 break;
James Smartda0436e2009-05-22 14:51:39 -04005754 default:
5755 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5756 "0288 Unknown FCoE event type 0x%x event tag "
James Smart70f3c072010-12-15 17:57:33 -05005757 "0x%x\n", event_type, acqe_fip->event_tag);
James Smartda0436e2009-05-22 14:51:39 -04005758 break;
5759 }
5760}
5761
5762/**
5763 * lpfc_sli4_async_dcbx_evt - Process the asynchronous dcbx event
5764 * @phba: pointer to lpfc hba data structure.
5765 * @acqe_link: pointer to the async dcbx completion queue entry.
5766 *
5767 * This routine is to handle the SLI4 asynchronous dcbx event.
5768 **/
5769static void
5770lpfc_sli4_async_dcbx_evt(struct lpfc_hba *phba,
5771 struct lpfc_acqe_dcbx *acqe_dcbx)
5772{
James Smart4d9ab992009-10-02 15:16:39 -04005773 phba->fc_eventTag = acqe_dcbx->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04005774 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5775 "0290 The SLI4 DCBX asynchronous event is not "
5776 "handled yet\n");
5777}
5778
5779/**
James Smartb19a0612010-04-06 14:48:51 -04005780 * lpfc_sli4_async_grp5_evt - Process the asynchronous group5 event
5781 * @phba: pointer to lpfc hba data structure.
5782 * @acqe_link: pointer to the async grp5 completion queue entry.
5783 *
5784 * This routine is to handle the SLI4 asynchronous grp5 event. A grp5 event
5785 * is an asynchronous notified of a logical link speed change. The Port
5786 * reports the logical link speed in units of 10Mbps.
5787 **/
5788static void
5789lpfc_sli4_async_grp5_evt(struct lpfc_hba *phba,
5790 struct lpfc_acqe_grp5 *acqe_grp5)
5791{
5792 uint16_t prev_ll_spd;
5793
5794 phba->fc_eventTag = acqe_grp5->event_tag;
5795 phba->fcoe_eventtag = acqe_grp5->event_tag;
5796 prev_ll_spd = phba->sli4_hba.link_state.logical_speed;
5797 phba->sli4_hba.link_state.logical_speed =
James Smart8b68cd52012-09-29 11:32:37 -04005798 (bf_get(lpfc_acqe_grp5_llink_spd, acqe_grp5)) * 10;
James Smartb19a0612010-04-06 14:48:51 -04005799 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5800 "2789 GRP5 Async Event: Updating logical link speed "
James Smart8b68cd52012-09-29 11:32:37 -04005801 "from %dMbps to %dMbps\n", prev_ll_spd,
5802 phba->sli4_hba.link_state.logical_speed);
James Smartb19a0612010-04-06 14:48:51 -04005803}
5804
5805/**
James Smartda0436e2009-05-22 14:51:39 -04005806 * lpfc_sli4_async_event_proc - Process all the pending asynchronous event
5807 * @phba: pointer to lpfc hba data structure.
5808 *
5809 * This routine is invoked by the worker thread to process all the pending
5810 * SLI4 asynchronous events.
5811 **/
5812void lpfc_sli4_async_event_proc(struct lpfc_hba *phba)
5813{
5814 struct lpfc_cq_event *cq_event;
5815
5816 /* First, declare the async event has been handled */
5817 spin_lock_irq(&phba->hbalock);
5818 phba->hba_flag &= ~ASYNC_EVENT;
5819 spin_unlock_irq(&phba->hbalock);
5820 /* Now, handle all the async events */
5821 while (!list_empty(&phba->sli4_hba.sp_asynce_work_queue)) {
5822 /* Get the first event from the head of the event queue */
5823 spin_lock_irq(&phba->hbalock);
5824 list_remove_head(&phba->sli4_hba.sp_asynce_work_queue,
5825 cq_event, struct lpfc_cq_event, list);
5826 spin_unlock_irq(&phba->hbalock);
5827 /* Process the asynchronous event */
5828 switch (bf_get(lpfc_trailer_code, &cq_event->cqe.mcqe_cmpl)) {
5829 case LPFC_TRAILER_CODE_LINK:
5830 lpfc_sli4_async_link_evt(phba,
5831 &cq_event->cqe.acqe_link);
5832 break;
5833 case LPFC_TRAILER_CODE_FCOE:
James Smart70f3c072010-12-15 17:57:33 -05005834 lpfc_sli4_async_fip_evt(phba, &cq_event->cqe.acqe_fip);
James Smartda0436e2009-05-22 14:51:39 -04005835 break;
5836 case LPFC_TRAILER_CODE_DCBX:
5837 lpfc_sli4_async_dcbx_evt(phba,
5838 &cq_event->cqe.acqe_dcbx);
5839 break;
James Smartb19a0612010-04-06 14:48:51 -04005840 case LPFC_TRAILER_CODE_GRP5:
5841 lpfc_sli4_async_grp5_evt(phba,
5842 &cq_event->cqe.acqe_grp5);
5843 break;
James Smart70f3c072010-12-15 17:57:33 -05005844 case LPFC_TRAILER_CODE_FC:
5845 lpfc_sli4_async_fc_evt(phba, &cq_event->cqe.acqe_fc);
5846 break;
5847 case LPFC_TRAILER_CODE_SLI:
5848 lpfc_sli4_async_sli_evt(phba, &cq_event->cqe.acqe_sli);
5849 break;
James Smartda0436e2009-05-22 14:51:39 -04005850 default:
5851 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5852 "1804 Invalid asynchrous event code: "
5853 "x%x\n", bf_get(lpfc_trailer_code,
5854 &cq_event->cqe.mcqe_cmpl));
5855 break;
5856 }
5857 /* Free the completion event processed to the free pool */
5858 lpfc_sli4_cq_event_release(phba, cq_event);
5859 }
5860}
5861
5862/**
James Smartecfd03c2010-02-12 14:41:27 -05005863 * lpfc_sli4_fcf_redisc_event_proc - Process fcf table rediscovery event
5864 * @phba: pointer to lpfc hba data structure.
5865 *
5866 * This routine is invoked by the worker thread to process FCF table
5867 * rediscovery pending completion event.
5868 **/
5869void lpfc_sli4_fcf_redisc_event_proc(struct lpfc_hba *phba)
5870{
5871 int rc;
5872
5873 spin_lock_irq(&phba->hbalock);
5874 /* Clear FCF rediscovery timeout event */
5875 phba->fcf.fcf_flag &= ~FCF_REDISC_EVT;
5876 /* Clear driver fast failover FCF record flag */
5877 phba->fcf.failover_rec.flag = 0;
5878 /* Set state for FCF fast failover */
5879 phba->fcf.fcf_flag |= FCF_REDISC_FOV;
5880 spin_unlock_irq(&phba->hbalock);
5881
5882 /* Scan FCF table from the first entry to re-discover SAN */
James Smart0c9ab6f2010-02-26 14:15:57 -05005883 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04005884 "2777 Start post-quiescent FCF table scan\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05005885 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
James Smartecfd03c2010-02-12 14:41:27 -05005886 if (rc)
James Smart0c9ab6f2010-02-26 14:15:57 -05005887 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
5888 "2747 Issue FCF scan read FCF mailbox "
5889 "command failed 0x%x\n", rc);
James Smartecfd03c2010-02-12 14:41:27 -05005890}
5891
5892/**
James Smartda0436e2009-05-22 14:51:39 -04005893 * lpfc_api_table_setup - Set up per hba pci-device group func api jump table
5894 * @phba: pointer to lpfc hba data structure.
5895 * @dev_grp: The HBA PCI-Device group number.
5896 *
5897 * This routine is invoked to set up the per HBA PCI-Device group function
5898 * API jump table entries.
5899 *
5900 * Return: 0 if success, otherwise -ENODEV
5901 **/
5902int
5903lpfc_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
James Smart5b75da22008-12-04 22:39:35 -05005904{
5905 int rc;
5906
James Smartda0436e2009-05-22 14:51:39 -04005907 /* Set up lpfc PCI-device group */
5908 phba->pci_dev_grp = dev_grp;
James Smart5b75da22008-12-04 22:39:35 -05005909
James Smartda0436e2009-05-22 14:51:39 -04005910 /* The LPFC_PCI_DEV_OC uses SLI4 */
5911 if (dev_grp == LPFC_PCI_DEV_OC)
5912 phba->sli_rev = LPFC_SLI_REV4;
James Smart5b75da22008-12-04 22:39:35 -05005913
James Smartda0436e2009-05-22 14:51:39 -04005914 /* Set up device INIT API function jump table */
5915 rc = lpfc_init_api_table_setup(phba, dev_grp);
5916 if (rc)
5917 return -ENODEV;
5918 /* Set up SCSI API function jump table */
5919 rc = lpfc_scsi_api_table_setup(phba, dev_grp);
5920 if (rc)
5921 return -ENODEV;
5922 /* Set up SLI API function jump table */
5923 rc = lpfc_sli_api_table_setup(phba, dev_grp);
5924 if (rc)
5925 return -ENODEV;
5926 /* Set up MBOX API function jump table */
5927 rc = lpfc_mbox_api_table_setup(phba, dev_grp);
5928 if (rc)
5929 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05005930
James Smartda0436e2009-05-22 14:51:39 -04005931 return 0;
James Smart5b75da22008-12-04 22:39:35 -05005932}
5933
5934/**
James Smart3621a712009-04-06 18:47:14 -04005935 * lpfc_log_intr_mode - Log the active interrupt mode
James Smart5b75da22008-12-04 22:39:35 -05005936 * @phba: pointer to lpfc hba data structure.
5937 * @intr_mode: active interrupt mode adopted.
5938 *
5939 * This routine it invoked to log the currently used active interrupt mode
5940 * to the device.
James Smart3772a992009-05-22 14:50:54 -04005941 **/
5942static void lpfc_log_intr_mode(struct lpfc_hba *phba, uint32_t intr_mode)
James Smart5b75da22008-12-04 22:39:35 -05005943{
5944 switch (intr_mode) {
5945 case 0:
5946 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5947 "0470 Enable INTx interrupt mode.\n");
5948 break;
5949 case 1:
5950 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5951 "0481 Enabled MSI interrupt mode.\n");
5952 break;
5953 case 2:
5954 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5955 "0480 Enabled MSI-X interrupt mode.\n");
5956 break;
5957 default:
5958 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5959 "0482 Illegal interrupt mode.\n");
5960 break;
5961 }
5962 return;
5963}
5964
James Smart5b75da22008-12-04 22:39:35 -05005965/**
James Smart3772a992009-05-22 14:50:54 -04005966 * lpfc_enable_pci_dev - Enable a generic PCI device.
James Smart5b75da22008-12-04 22:39:35 -05005967 * @phba: pointer to lpfc hba data structure.
5968 *
James Smart3772a992009-05-22 14:50:54 -04005969 * This routine is invoked to enable the PCI device that is common to all
5970 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05005971 *
5972 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005973 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04005974 * other values - error
James Smart5b75da22008-12-04 22:39:35 -05005975 **/
James Smart3772a992009-05-22 14:50:54 -04005976static int
5977lpfc_enable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05005978{
James Smart3772a992009-05-22 14:50:54 -04005979 struct pci_dev *pdev;
James Smart5b75da22008-12-04 22:39:35 -05005980
James Smart3772a992009-05-22 14:50:54 -04005981 /* Obtain PCI device reference */
5982 if (!phba->pcidev)
5983 goto out_error;
5984 else
5985 pdev = phba->pcidev;
James Smart3772a992009-05-22 14:50:54 -04005986 /* Enable PCI device */
5987 if (pci_enable_device_mem(pdev))
5988 goto out_error;
5989 /* Request PCI resource for the device */
Johannes Thumshirne0c04832016-06-07 09:44:03 +02005990 if (pci_request_mem_regions(pdev, LPFC_DRIVER_NAME))
James Smart3772a992009-05-22 14:50:54 -04005991 goto out_disable_device;
5992 /* Set up device as PCI master and save state for EEH */
5993 pci_set_master(pdev);
5994 pci_try_set_mwi(pdev);
5995 pci_save_state(pdev);
James Smart5b75da22008-12-04 22:39:35 -05005996
James Smart05580562011-05-24 11:40:48 -04005997 /* PCIe EEH recovery on powerpc platforms needs fundamental reset */
Jon Mason453193e2013-09-11 15:38:13 -07005998 if (pci_is_pcie(pdev))
James Smart05580562011-05-24 11:40:48 -04005999 pdev->needs_freset = 1;
6000
James Smart3772a992009-05-22 14:50:54 -04006001 return 0;
James Smart5b75da22008-12-04 22:39:35 -05006002
James Smart3772a992009-05-22 14:50:54 -04006003out_disable_device:
6004 pci_disable_device(pdev);
6005out_error:
James Smart079b5c92011-08-21 21:48:49 -04006006 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
Johannes Thumshirne0c04832016-06-07 09:44:03 +02006007 "1401 Failed to enable pci device\n");
James Smart3772a992009-05-22 14:50:54 -04006008 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05006009}
6010
6011/**
James Smart3772a992009-05-22 14:50:54 -04006012 * lpfc_disable_pci_dev - Disable a generic PCI device.
James Smart5b75da22008-12-04 22:39:35 -05006013 * @phba: pointer to lpfc hba data structure.
6014 *
James Smart3772a992009-05-22 14:50:54 -04006015 * This routine is invoked to disable the PCI device that is common to all
6016 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05006017 **/
6018static void
James Smart3772a992009-05-22 14:50:54 -04006019lpfc_disable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05006020{
James Smart3772a992009-05-22 14:50:54 -04006021 struct pci_dev *pdev;
James Smart5b75da22008-12-04 22:39:35 -05006022
James Smart3772a992009-05-22 14:50:54 -04006023 /* Obtain PCI device reference */
6024 if (!phba->pcidev)
6025 return;
6026 else
6027 pdev = phba->pcidev;
James Smart3772a992009-05-22 14:50:54 -04006028 /* Release PCI resource and disable PCI device */
Johannes Thumshirne0c04832016-06-07 09:44:03 +02006029 pci_release_mem_regions(pdev);
James Smart3772a992009-05-22 14:50:54 -04006030 pci_disable_device(pdev);
James Smart5b75da22008-12-04 22:39:35 -05006031
6032 return;
6033}
6034
6035/**
James Smart3772a992009-05-22 14:50:54 -04006036 * lpfc_reset_hba - Reset a hba
6037 * @phba: pointer to lpfc hba data structure.
James Smarte59058c2008-08-24 21:49:00 -04006038 *
James Smart3772a992009-05-22 14:50:54 -04006039 * This routine is invoked to reset a hba device. It brings the HBA
6040 * offline, performs a board restart, and then brings the board back
6041 * online. The lpfc_offline calls lpfc_sli_hba_down which will clean up
6042 * on outstanding mailbox commands.
James Smarte59058c2008-08-24 21:49:00 -04006043 **/
James Smart3772a992009-05-22 14:50:54 -04006044void
6045lpfc_reset_hba(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05006046{
James Smart3772a992009-05-22 14:50:54 -04006047 /* If resets are disabled then set error state and return. */
6048 if (!phba->cfg_enable_hba_reset) {
6049 phba->link_state = LPFC_HBA_ERROR;
6050 return;
6051 }
James Smartee620212014-04-04 13:51:53 -04006052 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
6053 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
6054 else
6055 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
James Smart3772a992009-05-22 14:50:54 -04006056 lpfc_offline(phba);
6057 lpfc_sli_brdrestart(phba);
6058 lpfc_online(phba);
6059 lpfc_unblock_mgmt_io(phba);
6060}
dea31012005-04-17 16:05:31 -05006061
James Smart3772a992009-05-22 14:50:54 -04006062/**
James Smart0a96e972011-07-22 18:37:28 -04006063 * lpfc_sli_sriov_nr_virtfn_get - Get the number of sr-iov virtual functions
6064 * @phba: pointer to lpfc hba data structure.
6065 *
6066 * This function enables the PCI SR-IOV virtual functions to a physical
6067 * function. It invokes the PCI SR-IOV api with the @nr_vfn provided to
6068 * enable the number of virtual functions to the physical function. As
6069 * not all devices support SR-IOV, the return code from the pci_enable_sriov()
6070 * API call does not considered as an error condition for most of the device.
6071 **/
6072uint16_t
6073lpfc_sli_sriov_nr_virtfn_get(struct lpfc_hba *phba)
6074{
6075 struct pci_dev *pdev = phba->pcidev;
6076 uint16_t nr_virtfn;
6077 int pos;
6078
James Smart0a96e972011-07-22 18:37:28 -04006079 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
6080 if (pos == 0)
6081 return 0;
6082
6083 pci_read_config_word(pdev, pos + PCI_SRIOV_TOTAL_VF, &nr_virtfn);
6084 return nr_virtfn;
6085}
6086
6087/**
James Smart912e3ac2011-05-24 11:42:11 -04006088 * lpfc_sli_probe_sriov_nr_virtfn - Enable a number of sr-iov virtual functions
6089 * @phba: pointer to lpfc hba data structure.
6090 * @nr_vfn: number of virtual functions to be enabled.
6091 *
6092 * This function enables the PCI SR-IOV virtual functions to a physical
6093 * function. It invokes the PCI SR-IOV api with the @nr_vfn provided to
6094 * enable the number of virtual functions to the physical function. As
6095 * not all devices support SR-IOV, the return code from the pci_enable_sriov()
6096 * API call does not considered as an error condition for most of the device.
6097 **/
6098int
6099lpfc_sli_probe_sriov_nr_virtfn(struct lpfc_hba *phba, int nr_vfn)
6100{
6101 struct pci_dev *pdev = phba->pcidev;
James Smart0a96e972011-07-22 18:37:28 -04006102 uint16_t max_nr_vfn;
James Smart912e3ac2011-05-24 11:42:11 -04006103 int rc;
6104
James Smart0a96e972011-07-22 18:37:28 -04006105 max_nr_vfn = lpfc_sli_sriov_nr_virtfn_get(phba);
6106 if (nr_vfn > max_nr_vfn) {
6107 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6108 "3057 Requested vfs (%d) greater than "
6109 "supported vfs (%d)", nr_vfn, max_nr_vfn);
6110 return -EINVAL;
6111 }
6112
James Smart912e3ac2011-05-24 11:42:11 -04006113 rc = pci_enable_sriov(pdev, nr_vfn);
6114 if (rc) {
6115 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6116 "2806 Failed to enable sriov on this device "
6117 "with vfn number nr_vf:%d, rc:%d\n",
6118 nr_vfn, rc);
6119 } else
6120 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6121 "2807 Successful enable sriov on this device "
6122 "with vfn number nr_vf:%d\n", nr_vfn);
6123 return rc;
6124}
6125
6126/**
James Smart895427b2017-02-12 13:52:30 -08006127 * lpfc_setup_driver_resource_phase1 - Phase1 etup driver internal resources.
James Smart3772a992009-05-22 14:50:54 -04006128 * @phba: pointer to lpfc hba data structure.
6129 *
James Smart895427b2017-02-12 13:52:30 -08006130 * This routine is invoked to set up the driver internal resources before the
6131 * device specific resource setup to support the HBA device it attached to.
James Smart3772a992009-05-22 14:50:54 -04006132 *
6133 * Return codes
James Smart895427b2017-02-12 13:52:30 -08006134 * 0 - successful
6135 * other values - error
James Smart3772a992009-05-22 14:50:54 -04006136 **/
6137static int
James Smart895427b2017-02-12 13:52:30 -08006138lpfc_setup_driver_resource_phase1(struct lpfc_hba *phba)
James Smart3772a992009-05-22 14:50:54 -04006139{
James Smart895427b2017-02-12 13:52:30 -08006140 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05006141
James Smart3772a992009-05-22 14:50:54 -04006142 /*
James Smart895427b2017-02-12 13:52:30 -08006143 * Driver resources common to all SLI revisions
James Smart3772a992009-05-22 14:50:54 -04006144 */
James Smart895427b2017-02-12 13:52:30 -08006145 atomic_set(&phba->fast_event_count, 0);
6146 spin_lock_init(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006147
James Smart895427b2017-02-12 13:52:30 -08006148 /* Initialize ndlp management spinlock */
6149 spin_lock_init(&phba->ndlp_lock);
James Smart3772a992009-05-22 14:50:54 -04006150
James Smart523128e2018-09-10 10:30:46 -07006151 /* Initialize port_list spinlock */
6152 spin_lock_init(&phba->port_list_lock);
James Smart895427b2017-02-12 13:52:30 -08006153 INIT_LIST_HEAD(&phba->port_list);
James Smart523128e2018-09-10 10:30:46 -07006154
James Smart895427b2017-02-12 13:52:30 -08006155 INIT_LIST_HEAD(&phba->work_list);
6156 init_waitqueue_head(&phba->wait_4_mlo_m_q);
6157
6158 /* Initialize the wait queue head for the kernel thread */
6159 init_waitqueue_head(&phba->work_waitq);
6160
6161 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smartf358dd02017-02-12 13:52:34 -08006162 "1403 Protocols supported %s %s %s\n",
James Smart895427b2017-02-12 13:52:30 -08006163 ((phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) ?
6164 "SCSI" : " "),
6165 ((phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) ?
James Smartf358dd02017-02-12 13:52:34 -08006166 "NVME" : " "),
6167 (phba->nvmet_support ? "NVMET" : " "));
James Smart895427b2017-02-12 13:52:30 -08006168
James Smart0794d602019-01-28 11:14:19 -08006169 /* Initialize the IO buffer list used by driver for SLI3 SCSI */
6170 spin_lock_init(&phba->scsi_buf_list_get_lock);
6171 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_get);
6172 spin_lock_init(&phba->scsi_buf_list_put_lock);
6173 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
James Smart895427b2017-02-12 13:52:30 -08006174
James Smart895427b2017-02-12 13:52:30 -08006175 /* Initialize the fabric iocb list */
6176 INIT_LIST_HEAD(&phba->fabric_iocb_list);
6177
6178 /* Initialize list to save ELS buffers */
6179 INIT_LIST_HEAD(&phba->elsbuf);
6180
6181 /* Initialize FCF connection rec list */
6182 INIT_LIST_HEAD(&phba->fcf_conn_rec_list);
6183
6184 /* Initialize OAS configuration list */
6185 spin_lock_init(&phba->devicelock);
6186 INIT_LIST_HEAD(&phba->luns);
6187
James Smart3772a992009-05-22 14:50:54 -04006188 /* MBOX heartbeat timer */
Kees Cookf22eb4d2017-09-06 20:24:26 -07006189 timer_setup(&psli->mbox_tmo, lpfc_mbox_timeout, 0);
James Smart3772a992009-05-22 14:50:54 -04006190 /* Fabric block timer */
Kees Cookf22eb4d2017-09-06 20:24:26 -07006191 timer_setup(&phba->fabric_block_timer, lpfc_fabric_block_timeout, 0);
James Smart3772a992009-05-22 14:50:54 -04006192 /* EA polling mode timer */
Kees Cookf22eb4d2017-09-06 20:24:26 -07006193 timer_setup(&phba->eratt_poll, lpfc_poll_eratt, 0);
James Smart895427b2017-02-12 13:52:30 -08006194 /* Heartbeat timer */
Kees Cookf22eb4d2017-09-06 20:24:26 -07006195 timer_setup(&phba->hb_tmofunc, lpfc_hb_timeout, 0);
James Smart895427b2017-02-12 13:52:30 -08006196
James Smart32517fc2019-01-28 11:14:33 -08006197 INIT_DELAYED_WORK(&phba->eq_delay_work, lpfc_hb_eq_delay_work);
6198
James Smart895427b2017-02-12 13:52:30 -08006199 return 0;
6200}
6201
6202/**
6203 * lpfc_sli_driver_resource_setup - Setup driver internal resources for SLI3 dev
6204 * @phba: pointer to lpfc hba data structure.
6205 *
6206 * This routine is invoked to set up the driver internal resources specific to
6207 * support the SLI-3 HBA device it attached to.
6208 *
6209 * Return codes
6210 * 0 - successful
6211 * other values - error
6212 **/
6213static int
6214lpfc_sli_driver_resource_setup(struct lpfc_hba *phba)
6215{
James Smart0794d602019-01-28 11:14:19 -08006216 int rc, entry_sz;
James Smart895427b2017-02-12 13:52:30 -08006217
6218 /*
6219 * Initialize timers used by driver
6220 */
6221
6222 /* FCP polling mode timer */
Kees Cookf22eb4d2017-09-06 20:24:26 -07006223 timer_setup(&phba->fcp_poll_timer, lpfc_poll_timeout, 0);
James Smart3772a992009-05-22 14:50:54 -04006224
6225 /* Host attention work mask setup */
6226 phba->work_ha_mask = (HA_ERATT | HA_MBATT | HA_LATT);
6227 phba->work_ha_mask |= (HA_RXMASK << (LPFC_ELS_RING * 4));
6228
6229 /* Get all the module params for configuring this host */
6230 lpfc_get_cfgparam(phba);
James Smart895427b2017-02-12 13:52:30 -08006231 /* Set up phase-1 common device driver resources */
6232
6233 rc = lpfc_setup_driver_resource_phase1(phba);
6234 if (rc)
6235 return -ENODEV;
6236
James Smart49198b32010-04-06 15:04:33 -04006237 if (phba->pcidev->device == PCI_DEVICE_ID_HORNET) {
6238 phba->menlo_flag |= HBA_MENLO_SUPPORT;
6239 /* check for menlo minimum sg count */
6240 if (phba->cfg_sg_seg_cnt < LPFC_DEFAULT_MENLO_SG_SEG_CNT)
6241 phba->cfg_sg_seg_cnt = LPFC_DEFAULT_MENLO_SG_SEG_CNT;
6242 }
6243
James Smart895427b2017-02-12 13:52:30 -08006244 if (!phba->sli.sli3_ring)
Kees Cook6396bb22018-06-12 14:03:40 -07006245 phba->sli.sli3_ring = kcalloc(LPFC_SLI3_MAX_RING,
6246 sizeof(struct lpfc_sli_ring),
6247 GFP_KERNEL);
James Smart895427b2017-02-12 13:52:30 -08006248 if (!phba->sli.sli3_ring)
James Smart2a76a282012-08-03 12:35:54 -04006249 return -ENOMEM;
6250
James Smart3772a992009-05-22 14:50:54 -04006251 /*
James Smart96f70772013-04-17 20:16:15 -04006252 * Since lpfc_sg_seg_cnt is module parameter, the sg_dma_buf_size
James Smart3772a992009-05-22 14:50:54 -04006253 * used to create the sg_dma_buf_pool must be dynamically calculated.
James Smart3772a992009-05-22 14:50:54 -04006254 */
James Smart3772a992009-05-22 14:50:54 -04006255
James Smart96f70772013-04-17 20:16:15 -04006256 /* Initialize the host templates the configured values. */
James Smart3772a992009-05-22 14:50:54 -04006257 lpfc_vport_template.sg_tablesize = phba->cfg_sg_seg_cnt;
James Smart96418b52017-03-04 09:30:31 -08006258 lpfc_template_no_hr.sg_tablesize = phba->cfg_sg_seg_cnt;
6259 lpfc_template.sg_tablesize = phba->cfg_sg_seg_cnt;
James Smart3772a992009-05-22 14:50:54 -04006260
James Smart0794d602019-01-28 11:14:19 -08006261 if (phba->sli_rev == LPFC_SLI_REV4)
6262 entry_sz = sizeof(struct sli4_sge);
6263 else
6264 entry_sz = sizeof(struct ulp_bde64);
6265
James Smart96f70772013-04-17 20:16:15 -04006266 /* There are going to be 2 reserved BDEs: 1 FCP cmnd + 1 FCP rsp */
6267 if (phba->cfg_enable_bg) {
6268 /*
6269 * The scsi_buf for a T10-DIF I/O will hold the FCP cmnd,
6270 * the FCP rsp, and a BDE for each. Sice we have no control
6271 * over how many protection data segments the SCSI Layer
6272 * will hand us (ie: there could be one for every block
6273 * in the IO), we just allocate enough BDEs to accomidate
6274 * our max amount and we need to limit lpfc_sg_seg_cnt to
6275 * minimize the risk of running out.
6276 */
6277 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
6278 sizeof(struct fcp_rsp) +
James Smart0794d602019-01-28 11:14:19 -08006279 (LPFC_MAX_SG_SEG_CNT * entry_sz);
James Smart96f70772013-04-17 20:16:15 -04006280
6281 if (phba->cfg_sg_seg_cnt > LPFC_MAX_SG_SEG_CNT_DIF)
6282 phba->cfg_sg_seg_cnt = LPFC_MAX_SG_SEG_CNT_DIF;
6283
6284 /* Total BDEs in BPL for scsi_sg_list and scsi_sg_prot_list */
6285 phba->cfg_total_seg_cnt = LPFC_MAX_SG_SEG_CNT;
6286 } else {
6287 /*
6288 * The scsi_buf for a regular I/O will hold the FCP cmnd,
6289 * the FCP rsp, a BDE for each, and a BDE for up to
6290 * cfg_sg_seg_cnt data segments.
6291 */
6292 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
6293 sizeof(struct fcp_rsp) +
James Smart0794d602019-01-28 11:14:19 -08006294 ((phba->cfg_sg_seg_cnt + 2) * entry_sz);
James Smart96f70772013-04-17 20:16:15 -04006295
6296 /* Total BDEs in BPL for scsi_sg_list */
6297 phba->cfg_total_seg_cnt = phba->cfg_sg_seg_cnt + 2;
6298 }
6299
6300 lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_FCP,
6301 "9088 sg_tablesize:%d dmabuf_size:%d total_bde:%d\n",
6302 phba->cfg_sg_seg_cnt, phba->cfg_sg_dma_buf_size,
6303 phba->cfg_total_seg_cnt);
6304
James Smart3772a992009-05-22 14:50:54 -04006305 phba->max_vpi = LPFC_MAX_VPI;
6306 /* This will be set to correct value after config_port mbox */
6307 phba->max_vports = 0;
6308
6309 /*
6310 * Initialize the SLI Layer to run with lpfc HBAs.
6311 */
6312 lpfc_sli_setup(phba);
James Smart895427b2017-02-12 13:52:30 -08006313 lpfc_sli_queue_init(phba);
James Smart3772a992009-05-22 14:50:54 -04006314
6315 /* Allocate device driver memory */
6316 if (lpfc_mem_alloc(phba, BPL_ALIGN_SZ))
6317 return -ENOMEM;
6318
James Smart912e3ac2011-05-24 11:42:11 -04006319 /*
6320 * Enable sr-iov virtual functions if supported and configured
6321 * through the module parameter.
6322 */
6323 if (phba->cfg_sriov_nr_virtfn > 0) {
6324 rc = lpfc_sli_probe_sriov_nr_virtfn(phba,
6325 phba->cfg_sriov_nr_virtfn);
6326 if (rc) {
6327 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6328 "2808 Requested number of SR-IOV "
6329 "virtual functions (%d) is not "
6330 "supported\n",
6331 phba->cfg_sriov_nr_virtfn);
6332 phba->cfg_sriov_nr_virtfn = 0;
6333 }
6334 }
6335
James Smart3772a992009-05-22 14:50:54 -04006336 return 0;
6337}
6338
6339/**
6340 * lpfc_sli_driver_resource_unset - Unset drvr internal resources for SLI3 dev
6341 * @phba: pointer to lpfc hba data structure.
6342 *
6343 * This routine is invoked to unset the driver internal resources set up
6344 * specific for supporting the SLI-3 HBA device it attached to.
6345 **/
6346static void
6347lpfc_sli_driver_resource_unset(struct lpfc_hba *phba)
6348{
6349 /* Free device driver memory allocated */
6350 lpfc_mem_free_all(phba);
6351
6352 return;
6353}
6354
6355/**
James Smartda0436e2009-05-22 14:51:39 -04006356 * lpfc_sli4_driver_resource_setup - Setup drvr internal resources for SLI4 dev
6357 * @phba: pointer to lpfc hba data structure.
6358 *
6359 * This routine is invoked to set up the driver internal resources specific to
6360 * support the SLI-4 HBA device it attached to.
6361 *
6362 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006363 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04006364 * other values - error
6365 **/
6366static int
6367lpfc_sli4_driver_resource_setup(struct lpfc_hba *phba)
6368{
James Smart28baac72010-02-12 14:42:03 -05006369 LPFC_MBOXQ_t *mboxq;
James Smartf358dd02017-02-12 13:52:34 -08006370 MAILBOX_t *mb;
James Smart895427b2017-02-12 13:52:30 -08006371 int rc, i, max_buf_size;
James Smart28baac72010-02-12 14:42:03 -05006372 uint8_t pn_page[LPFC_MAX_SUPPORTED_PAGES] = {0};
6373 struct lpfc_mqe *mqe;
James Smart09294d42013-04-17 20:16:05 -04006374 int longs;
James Smart81e6a632017-11-20 16:00:43 -08006375 int extra;
James Smartf358dd02017-02-12 13:52:34 -08006376 uint64_t wwn;
James Smartb92dc722018-05-24 21:09:01 -07006377 u32 if_type;
6378 u32 if_fam;
James Smartda0436e2009-05-22 14:51:39 -04006379
James Smart895427b2017-02-12 13:52:30 -08006380 phba->sli4_hba.num_present_cpu = lpfc_present_cpu;
James Smart222e9232019-01-28 11:14:35 -08006381 phba->sli4_hba.num_possible_cpu = num_possible_cpus();
James Smart895427b2017-02-12 13:52:30 -08006382 phba->sli4_hba.curr_disp_cpu = 0;
6383
James Smart716d3bc2013-09-06 12:18:28 -04006384 /* Get all the module params for configuring this host */
6385 lpfc_get_cfgparam(phba);
6386
James Smart895427b2017-02-12 13:52:30 -08006387 /* Set up phase-1 common device driver resources */
6388 rc = lpfc_setup_driver_resource_phase1(phba);
6389 if (rc)
6390 return -ENODEV;
6391
James Smartda0436e2009-05-22 14:51:39 -04006392 /* Before proceed, wait for POST done and device ready */
6393 rc = lpfc_sli4_post_status_check(phba);
6394 if (rc)
6395 return -ENODEV;
6396
6397 /*
6398 * Initialize timers used by driver
6399 */
6400
Kees Cookf22eb4d2017-09-06 20:24:26 -07006401 timer_setup(&phba->rrq_tmr, lpfc_rrq_timeout, 0);
James Smartda0436e2009-05-22 14:51:39 -04006402
James Smartecfd03c2010-02-12 14:41:27 -05006403 /* FCF rediscover timer */
Kees Cookf22eb4d2017-09-06 20:24:26 -07006404 timer_setup(&phba->fcf.redisc_wait, lpfc_sli4_fcf_redisc_wait_tmo, 0);
James Smartecfd03c2010-02-12 14:41:27 -05006405
James Smartda0436e2009-05-22 14:51:39 -04006406 /*
James Smart7ad20aa2011-05-24 11:44:28 -04006407 * Control structure for handling external multi-buffer mailbox
6408 * command pass-through.
6409 */
6410 memset((uint8_t *)&phba->mbox_ext_buf_ctx, 0,
6411 sizeof(struct lpfc_mbox_ext_buf_ctx));
6412 INIT_LIST_HEAD(&phba->mbox_ext_buf_ctx.ext_dmabuf_list);
6413
James Smartda0436e2009-05-22 14:51:39 -04006414 phba->max_vpi = LPFC_MAX_VPI;
James Smart67d12732012-08-03 12:36:13 -04006415
James Smartda0436e2009-05-22 14:51:39 -04006416 /* This will be set to correct value after the read_config mbox */
6417 phba->max_vports = 0;
6418
6419 /* Program the default value of vlan_id and fc_map */
6420 phba->valid_vlan = 0;
6421 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
6422 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
6423 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
6424
6425 /*
James Smart2a76a282012-08-03 12:35:54 -04006426 * For SLI4, instead of using ring 0 (LPFC_FCP_RING) for FCP commands
James Smart895427b2017-02-12 13:52:30 -08006427 * we will associate a new ring, for each EQ/CQ/WQ tuple.
6428 * The WQ create will allocate the ring.
James Smart2a76a282012-08-03 12:35:54 -04006429 */
James Smart085c6472010-11-20 23:11:37 -05006430
James Smart09294d42013-04-17 20:16:05 -04006431 /*
James Smart81e6a632017-11-20 16:00:43 -08006432 * 1 for cmd, 1 for rsp, NVME adds an extra one
6433 * for boundary conditions in its max_sgl_segment template.
6434 */
6435 extra = 2;
6436 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
6437 extra++;
6438
6439 /*
James Smart09294d42013-04-17 20:16:05 -04006440 * It doesn't matter what family our adapter is in, we are
6441 * limited to 2 Pages, 512 SGEs, for our SGL.
6442 * There are going to be 2 reserved SGEs: 1 FCP cmnd + 1 FCP rsp
6443 */
6444 max_buf_size = (2 * SLI4_PAGE_SIZE);
James Smart09294d42013-04-17 20:16:05 -04006445
James Smartda0436e2009-05-22 14:51:39 -04006446 /*
James Smart895427b2017-02-12 13:52:30 -08006447 * Since lpfc_sg_seg_cnt is module param, the sg_dma_buf_size
6448 * used to create the sg_dma_buf_pool must be calculated.
James Smart28baac72010-02-12 14:42:03 -05006449 */
James Smartf44ac122018-03-05 12:04:08 -08006450 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED) {
James Smart96f70772013-04-17 20:16:15 -04006451 /*
James Smart895427b2017-02-12 13:52:30 -08006452 * The scsi_buf for a T10-DIF I/O holds the FCP cmnd,
6453 * the FCP rsp, and a SGE. Sice we have no control
6454 * over how many protection segments the SCSI Layer
James Smart96f70772013-04-17 20:16:15 -04006455 * will hand us (ie: there could be one for every block
James Smart895427b2017-02-12 13:52:30 -08006456 * in the IO), just allocate enough SGEs to accomidate
6457 * our max amount and we need to limit lpfc_sg_seg_cnt
6458 * to minimize the risk of running out.
James Smart96f70772013-04-17 20:16:15 -04006459 */
6460 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
James Smart895427b2017-02-12 13:52:30 -08006461 sizeof(struct fcp_rsp) + max_buf_size;
James Smart96f70772013-04-17 20:16:15 -04006462
6463 /* Total SGEs for scsi_sg_list and scsi_sg_prot_list */
6464 phba->cfg_total_seg_cnt = LPFC_MAX_SGL_SEG_CNT;
6465
James Smart5b9e70b2018-09-10 10:30:42 -07006466 /*
6467 * If supporting DIF, reduce the seg count for scsi to
6468 * allow room for the DIF sges.
6469 */
6470 if (phba->cfg_enable_bg &&
6471 phba->cfg_sg_seg_cnt > LPFC_MAX_BG_SLI4_SEG_CNT_DIF)
6472 phba->cfg_scsi_seg_cnt = LPFC_MAX_BG_SLI4_SEG_CNT_DIF;
6473 else
6474 phba->cfg_scsi_seg_cnt = phba->cfg_sg_seg_cnt;
6475
James Smart96f70772013-04-17 20:16:15 -04006476 } else {
6477 /*
James Smart895427b2017-02-12 13:52:30 -08006478 * The scsi_buf for a regular I/O holds the FCP cmnd,
James Smart96f70772013-04-17 20:16:15 -04006479 * the FCP rsp, a SGE for each, and a SGE for up to
6480 * cfg_sg_seg_cnt data segments.
6481 */
6482 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
James Smart895427b2017-02-12 13:52:30 -08006483 sizeof(struct fcp_rsp) +
James Smart81e6a632017-11-20 16:00:43 -08006484 ((phba->cfg_sg_seg_cnt + extra) *
James Smart895427b2017-02-12 13:52:30 -08006485 sizeof(struct sli4_sge));
James Smart96f70772013-04-17 20:16:15 -04006486
6487 /* Total SGEs for scsi_sg_list */
James Smart81e6a632017-11-20 16:00:43 -08006488 phba->cfg_total_seg_cnt = phba->cfg_sg_seg_cnt + extra;
James Smart5b9e70b2018-09-10 10:30:42 -07006489 phba->cfg_scsi_seg_cnt = phba->cfg_sg_seg_cnt;
James Smart895427b2017-02-12 13:52:30 -08006490
James Smart96f70772013-04-17 20:16:15 -04006491 /*
James Smart81e6a632017-11-20 16:00:43 -08006492 * NOTE: if (phba->cfg_sg_seg_cnt + extra) <= 256 we only
James Smart895427b2017-02-12 13:52:30 -08006493 * need to post 1 page for the SGL.
James Smart96f70772013-04-17 20:16:15 -04006494 */
James Smart085c6472010-11-20 23:11:37 -05006495 }
James Smartacd68592012-01-18 16:25:09 -05006496
James Smart5b9e70b2018-09-10 10:30:42 -07006497 /* Limit to LPFC_MAX_NVME_SEG_CNT for NVME. */
6498 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
6499 if (phba->cfg_sg_seg_cnt > LPFC_MAX_NVME_SEG_CNT) {
6500 lpfc_printf_log(phba, KERN_INFO, LOG_NVME | LOG_INIT,
6501 "6300 Reducing NVME sg segment "
6502 "cnt to %d\n",
6503 LPFC_MAX_NVME_SEG_CNT);
6504 phba->cfg_nvme_seg_cnt = LPFC_MAX_NVME_SEG_CNT;
6505 } else
6506 phba->cfg_nvme_seg_cnt = phba->cfg_sg_seg_cnt;
6507 }
6508
James Smart96f70772013-04-17 20:16:15 -04006509 /* Initialize the host templates with the updated values. */
James Smart5b9e70b2018-09-10 10:30:42 -07006510 lpfc_vport_template.sg_tablesize = phba->cfg_scsi_seg_cnt;
6511 lpfc_template.sg_tablesize = phba->cfg_scsi_seg_cnt;
6512 lpfc_template_no_hr.sg_tablesize = phba->cfg_scsi_seg_cnt;
James Smart96f70772013-04-17 20:16:15 -04006513
6514 if (phba->cfg_sg_dma_buf_size <= LPFC_MIN_SG_SLI4_BUF_SZ)
6515 phba->cfg_sg_dma_buf_size = LPFC_MIN_SG_SLI4_BUF_SZ;
6516 else
6517 phba->cfg_sg_dma_buf_size =
6518 SLI4_PAGE_ALIGN(phba->cfg_sg_dma_buf_size);
6519
6520 lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_FCP,
James Smart5b9e70b2018-09-10 10:30:42 -07006521 "9087 sg_seg_cnt:%d dmabuf_size:%d "
6522 "total:%d scsi:%d nvme:%d\n",
James Smart96f70772013-04-17 20:16:15 -04006523 phba->cfg_sg_seg_cnt, phba->cfg_sg_dma_buf_size,
James Smart5b9e70b2018-09-10 10:30:42 -07006524 phba->cfg_total_seg_cnt, phba->cfg_scsi_seg_cnt,
6525 phba->cfg_nvme_seg_cnt);
James Smartda0436e2009-05-22 14:51:39 -04006526
6527 /* Initialize buffer queue management fields */
James Smart895427b2017-02-12 13:52:30 -08006528 INIT_LIST_HEAD(&phba->hbqs[LPFC_ELS_HBQ].hbq_buffer_list);
James Smartda0436e2009-05-22 14:51:39 -04006529 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_sli4_rb_alloc;
6530 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_sli4_rb_free;
6531
6532 /*
6533 * Initialize the SLI Layer to run with lpfc SLI4 HBAs.
6534 */
James Smart895427b2017-02-12 13:52:30 -08006535 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) {
6536 /* Initialize the Abort scsi buffer list used by driver */
6537 spin_lock_init(&phba->sli4_hba.abts_scsi_buf_list_lock);
6538 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
6539 }
6540
6541 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
6542 /* Initialize the Abort nvme buffer list used by driver */
James Smart5e5b5112019-01-28 11:14:22 -08006543 spin_lock_init(&phba->sli4_hba.abts_nvmet_buf_list_lock);
James Smart86c67372017-04-21 16:05:04 -07006544 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_nvmet_ctx_list);
James Smarta8cf5df2017-05-15 15:20:46 -07006545 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_nvmet_io_wait_list);
James Smart895427b2017-02-12 13:52:30 -08006546 }
6547
James Smartda0436e2009-05-22 14:51:39 -04006548 /* This abort list used by worker thread */
James Smart895427b2017-02-12 13:52:30 -08006549 spin_lock_init(&phba->sli4_hba.sgl_list_lock);
James Smarta8cf5df2017-05-15 15:20:46 -07006550 spin_lock_init(&phba->sli4_hba.nvmet_io_wait_lock);
James Smartda0436e2009-05-22 14:51:39 -04006551
6552 /*
James Smart6d368e52011-05-24 11:44:12 -04006553 * Initialize driver internal slow-path work queues
James Smartda0436e2009-05-22 14:51:39 -04006554 */
6555
6556 /* Driver internel slow-path CQ Event pool */
6557 INIT_LIST_HEAD(&phba->sli4_hba.sp_cqe_event_pool);
6558 /* Response IOCB work queue list */
James Smart45ed1192009-10-02 15:17:02 -04006559 INIT_LIST_HEAD(&phba->sli4_hba.sp_queue_event);
James Smartda0436e2009-05-22 14:51:39 -04006560 /* Asynchronous event CQ Event work queue list */
6561 INIT_LIST_HEAD(&phba->sli4_hba.sp_asynce_work_queue);
6562 /* Fast-path XRI aborted CQ Event work queue list */
6563 INIT_LIST_HEAD(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
6564 /* Slow-path XRI aborted CQ Event work queue list */
6565 INIT_LIST_HEAD(&phba->sli4_hba.sp_els_xri_aborted_work_queue);
6566 /* Receive queue CQ Event work queue list */
6567 INIT_LIST_HEAD(&phba->sli4_hba.sp_unsol_work_queue);
6568
James Smart6d368e52011-05-24 11:44:12 -04006569 /* Initialize extent block lists. */
6570 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_blk_list);
6571 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_xri_blk_list);
6572 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_vfi_blk_list);
6573 INIT_LIST_HEAD(&phba->lpfc_vpi_blk_list);
6574
James Smartd1f525a2017-04-21 16:04:55 -07006575 /* Initialize mboxq lists. If the early init routines fail
6576 * these lists need to be correctly initialized.
6577 */
6578 INIT_LIST_HEAD(&phba->sli.mboxq);
6579 INIT_LIST_HEAD(&phba->sli.mboxq_cmpl);
6580
James Smart448193b2015-12-16 18:12:05 -05006581 /* initialize optic_state to 0xFF */
6582 phba->sli4_hba.lnk_info.optic_state = 0xff;
6583
James Smartda0436e2009-05-22 14:51:39 -04006584 /* Allocate device driver memory */
6585 rc = lpfc_mem_alloc(phba, SGL_ALIGN_SZ);
6586 if (rc)
6587 return -ENOMEM;
6588
James Smart2fcee4b2010-12-15 17:57:46 -05006589 /* IF Type 2 ports get initialized now. */
James Smart27d6ac02018-02-22 08:18:42 -08006590 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) >=
James Smart2fcee4b2010-12-15 17:57:46 -05006591 LPFC_SLI_INTF_IF_TYPE_2) {
6592 rc = lpfc_pci_function_reset(phba);
James Smart895427b2017-02-12 13:52:30 -08006593 if (unlikely(rc)) {
6594 rc = -ENODEV;
6595 goto out_free_mem;
6596 }
James Smart946727d2015-04-07 15:07:09 -04006597 phba->temp_sensor_support = 1;
James Smart2fcee4b2010-12-15 17:57:46 -05006598 }
6599
James Smartda0436e2009-05-22 14:51:39 -04006600 /* Create the bootstrap mailbox command */
6601 rc = lpfc_create_bootstrap_mbox(phba);
6602 if (unlikely(rc))
6603 goto out_free_mem;
6604
6605 /* Set up the host's endian order with the device. */
6606 rc = lpfc_setup_endian_order(phba);
6607 if (unlikely(rc))
6608 goto out_free_bsmbx;
6609
6610 /* Set up the hba's configuration parameters. */
6611 rc = lpfc_sli4_read_config(phba);
6612 if (unlikely(rc))
6613 goto out_free_bsmbx;
James Smartcff261f2013-12-17 20:29:47 -05006614 rc = lpfc_mem_alloc_active_rrq_pool_s4(phba);
6615 if (unlikely(rc))
6616 goto out_free_bsmbx;
James Smartda0436e2009-05-22 14:51:39 -04006617
James Smart2fcee4b2010-12-15 17:57:46 -05006618 /* IF Type 0 ports get initialized now. */
6619 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
6620 LPFC_SLI_INTF_IF_TYPE_0) {
6621 rc = lpfc_pci_function_reset(phba);
6622 if (unlikely(rc))
6623 goto out_free_bsmbx;
6624 }
James Smartda0436e2009-05-22 14:51:39 -04006625
James Smartcb5172e2010-03-15 11:25:07 -04006626 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
6627 GFP_KERNEL);
6628 if (!mboxq) {
6629 rc = -ENOMEM;
6630 goto out_free_bsmbx;
6631 }
6632
James Smartf358dd02017-02-12 13:52:34 -08006633 /* Check for NVMET being configured */
James Smart895427b2017-02-12 13:52:30 -08006634 phba->nvmet_support = 0;
James Smartf358dd02017-02-12 13:52:34 -08006635 if (lpfc_enable_nvmet_cnt) {
6636
6637 /* First get WWN of HBA instance */
6638 lpfc_read_nv(phba, mboxq);
6639 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6640 if (rc != MBX_SUCCESS) {
6641 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6642 "6016 Mailbox failed , mbxCmd x%x "
6643 "READ_NV, mbxStatus x%x\n",
6644 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
6645 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
James Smartd1f525a2017-04-21 16:04:55 -07006646 mempool_free(mboxq, phba->mbox_mem_pool);
James Smartf358dd02017-02-12 13:52:34 -08006647 rc = -EIO;
6648 goto out_free_bsmbx;
6649 }
6650 mb = &mboxq->u.mb;
6651 memcpy(&wwn, (char *)mb->un.varRDnvp.nodename,
6652 sizeof(uint64_t));
6653 wwn = cpu_to_be64(wwn);
6654 phba->sli4_hba.wwnn.u.name = wwn;
6655 memcpy(&wwn, (char *)mb->un.varRDnvp.portname,
6656 sizeof(uint64_t));
6657 /* wwn is WWPN of HBA instance */
6658 wwn = cpu_to_be64(wwn);
6659 phba->sli4_hba.wwpn.u.name = wwn;
6660
6661 /* Check to see if it matches any module parameter */
6662 for (i = 0; i < lpfc_enable_nvmet_cnt; i++) {
6663 if (wwn == lpfc_enable_nvmet[i]) {
James Smart7d708032017-03-08 14:36:01 -08006664#if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
James Smart3c603be2017-05-15 15:20:44 -07006665 if (lpfc_nvmet_mem_alloc(phba))
6666 break;
6667
6668 phba->nvmet_support = 1; /* a match */
6669
James Smartf358dd02017-02-12 13:52:34 -08006670 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6671 "6017 NVME Target %016llx\n",
6672 wwn);
James Smart7d708032017-03-08 14:36:01 -08006673#else
6674 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6675 "6021 Can't enable NVME Target."
6676 " NVME_TARGET_FC infrastructure"
6677 " is not in kernel\n");
6678#endif
James Smartc4908502019-01-28 11:14:28 -08006679 /* Not supported for NVMET */
6680 phba->cfg_xri_rebalancing = 0;
James Smart3c603be2017-05-15 15:20:44 -07006681 break;
James Smartf358dd02017-02-12 13:52:34 -08006682 }
6683 }
6684 }
James Smart895427b2017-02-12 13:52:30 -08006685
6686 lpfc_nvme_mod_param_dep(phba);
6687
James Smartfedd3b72011-02-16 12:39:24 -05006688 /* Get the Supported Pages if PORT_CAPABILITIES is supported by port. */
James Smartcb5172e2010-03-15 11:25:07 -04006689 lpfc_supported_pages(mboxq);
6690 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smartfedd3b72011-02-16 12:39:24 -05006691 if (!rc) {
6692 mqe = &mboxq->u.mqe;
6693 memcpy(&pn_page[0], ((uint8_t *)&mqe->un.supp_pages.word3),
6694 LPFC_MAX_SUPPORTED_PAGES);
6695 for (i = 0; i < LPFC_MAX_SUPPORTED_PAGES; i++) {
6696 switch (pn_page[i]) {
6697 case LPFC_SLI4_PARAMETERS:
6698 phba->sli4_hba.pc_sli4_params.supported = 1;
6699 break;
6700 default:
6701 break;
6702 }
6703 }
6704 /* Read the port's SLI4 Parameters capabilities if supported. */
6705 if (phba->sli4_hba.pc_sli4_params.supported)
6706 rc = lpfc_pc_sli4_params_get(phba, mboxq);
6707 if (rc) {
6708 mempool_free(mboxq, phba->mbox_mem_pool);
6709 rc = -EIO;
6710 goto out_free_bsmbx;
James Smartcb5172e2010-03-15 11:25:07 -04006711 }
6712 }
James Smart65791f12016-07-06 12:35:56 -07006713
James Smartfedd3b72011-02-16 12:39:24 -05006714 /*
6715 * Get sli4 parameters that override parameters from Port capabilities.
James Smart6d368e52011-05-24 11:44:12 -04006716 * If this call fails, it isn't critical unless the SLI4 parameters come
6717 * back in conflict.
James Smartfedd3b72011-02-16 12:39:24 -05006718 */
James Smart6d368e52011-05-24 11:44:12 -04006719 rc = lpfc_get_sli4_parameters(phba, mboxq);
6720 if (rc) {
James Smartb92dc722018-05-24 21:09:01 -07006721 if_type = bf_get(lpfc_sli_intf_if_type,
6722 &phba->sli4_hba.sli_intf);
6723 if_fam = bf_get(lpfc_sli_intf_sli_family,
6724 &phba->sli4_hba.sli_intf);
James Smart6d368e52011-05-24 11:44:12 -04006725 if (phba->sli4_hba.extents_in_use &&
6726 phba->sli4_hba.rpi_hdrs_in_use) {
6727 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6728 "2999 Unsupported SLI4 Parameters "
6729 "Extents and RPI headers enabled.\n");
James Smartb92dc722018-05-24 21:09:01 -07006730 if (if_type == LPFC_SLI_INTF_IF_TYPE_0 &&
6731 if_fam == LPFC_SLI_INTF_FAMILY_BE2) {
6732 mempool_free(mboxq, phba->mbox_mem_pool);
6733 rc = -EIO;
6734 goto out_free_bsmbx;
6735 }
James Smart6d368e52011-05-24 11:44:12 -04006736 }
James Smartb92dc722018-05-24 21:09:01 -07006737 if (!(if_type == LPFC_SLI_INTF_IF_TYPE_0 &&
6738 if_fam == LPFC_SLI_INTF_FAMILY_BE2)) {
6739 mempool_free(mboxq, phba->mbox_mem_pool);
6740 rc = -EIO;
6741 goto out_free_bsmbx;
6742 }
James Smart6d368e52011-05-24 11:44:12 -04006743 }
James Smart895427b2017-02-12 13:52:30 -08006744
James Smartcb5172e2010-03-15 11:25:07 -04006745 mempool_free(mboxq, phba->mbox_mem_pool);
James Smart1ba981f2014-02-20 09:56:45 -05006746
6747 /* Verify OAS is supported */
6748 lpfc_sli4_oas_verify(phba);
James Smart1ba981f2014-02-20 09:56:45 -05006749
James Smartd2cc9bc2018-09-10 10:30:50 -07006750 /* Verify RAS support on adapter */
6751 lpfc_sli4_ras_init(phba);
6752
James Smart5350d872011-10-10 21:33:49 -04006753 /* Verify all the SLI4 queues */
6754 rc = lpfc_sli4_queue_verify(phba);
James Smartda0436e2009-05-22 14:51:39 -04006755 if (rc)
6756 goto out_free_bsmbx;
6757
6758 /* Create driver internal CQE event pool */
6759 rc = lpfc_sli4_cq_event_pool_create(phba);
6760 if (rc)
James Smart5350d872011-10-10 21:33:49 -04006761 goto out_free_bsmbx;
James Smartda0436e2009-05-22 14:51:39 -04006762
James Smart8a9d2e82012-05-09 21:16:12 -04006763 /* Initialize sgl lists per host */
6764 lpfc_init_sgl_list(phba);
6765
6766 /* Allocate and initialize active sgl array */
James Smartda0436e2009-05-22 14:51:39 -04006767 rc = lpfc_init_active_sgl_array(phba);
6768 if (rc) {
6769 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6770 "1430 Failed to initialize sgl list.\n");
James Smart8a9d2e82012-05-09 21:16:12 -04006771 goto out_destroy_cq_event_pool;
James Smartda0436e2009-05-22 14:51:39 -04006772 }
James Smartda0436e2009-05-22 14:51:39 -04006773 rc = lpfc_sli4_init_rpi_hdrs(phba);
6774 if (rc) {
6775 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6776 "1432 Failed to initialize rpi headers.\n");
6777 goto out_free_active_sgl;
6778 }
6779
James Smarta93ff372010-10-22 11:06:08 -04006780 /* Allocate eligible FCF bmask memory for FCF roundrobin failover */
James Smart0c9ab6f2010-02-26 14:15:57 -05006781 longs = (LPFC_SLI4_FCF_TBL_INDX_MAX + BITS_PER_LONG - 1)/BITS_PER_LONG;
Kees Cook6396bb22018-06-12 14:03:40 -07006782 phba->fcf.fcf_rr_bmask = kcalloc(longs, sizeof(unsigned long),
James Smart0c9ab6f2010-02-26 14:15:57 -05006783 GFP_KERNEL);
6784 if (!phba->fcf.fcf_rr_bmask) {
6785 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6786 "2759 Failed allocate memory for FCF round "
6787 "robin failover bmask\n");
James Smart05580562011-05-24 11:40:48 -04006788 rc = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -05006789 goto out_remove_rpi_hdrs;
6790 }
6791
James Smart6a828b02019-01-28 11:14:31 -08006792 phba->sli4_hba.hba_eq_hdl = kcalloc(phba->cfg_irq_chann,
James Smartcdb42be2019-01-28 11:14:21 -08006793 sizeof(struct lpfc_hba_eq_hdl),
6794 GFP_KERNEL);
James Smart895427b2017-02-12 13:52:30 -08006795 if (!phba->sli4_hba.hba_eq_hdl) {
James Smart67d12732012-08-03 12:36:13 -04006796 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6797 "2572 Failed allocate memory for "
6798 "fast-path per-EQ handle array\n");
6799 rc = -ENOMEM;
6800 goto out_free_fcf_rr_bmask;
James Smartda0436e2009-05-22 14:51:39 -04006801 }
6802
James Smart222e9232019-01-28 11:14:35 -08006803 phba->sli4_hba.cpu_map = kcalloc(phba->sli4_hba.num_possible_cpu,
James Smart895427b2017-02-12 13:52:30 -08006804 sizeof(struct lpfc_vector_map_info),
6805 GFP_KERNEL);
James Smart7bb03bb2013-04-17 20:19:16 -04006806 if (!phba->sli4_hba.cpu_map) {
6807 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6808 "3327 Failed allocate memory for msi-x "
6809 "interrupt vector mapping\n");
6810 rc = -ENOMEM;
James Smart895427b2017-02-12 13:52:30 -08006811 goto out_free_hba_eq_hdl;
James Smart7bb03bb2013-04-17 20:19:16 -04006812 }
James Smartb246de12013-05-31 17:03:07 -04006813
James Smart32517fc2019-01-28 11:14:33 -08006814 phba->sli4_hba.eq_info = alloc_percpu(struct lpfc_eq_intr_info);
6815 if (!phba->sli4_hba.eq_info) {
6816 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6817 "3321 Failed allocation for per_cpu stats\n");
6818 rc = -ENOMEM;
6819 goto out_free_hba_cpu_map;
6820 }
James Smart912e3ac2011-05-24 11:42:11 -04006821 /*
6822 * Enable sr-iov virtual functions if supported and configured
6823 * through the module parameter.
6824 */
6825 if (phba->cfg_sriov_nr_virtfn > 0) {
6826 rc = lpfc_sli_probe_sriov_nr_virtfn(phba,
6827 phba->cfg_sriov_nr_virtfn);
6828 if (rc) {
6829 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6830 "3020 Requested number of SR-IOV "
6831 "virtual functions (%d) is not "
6832 "supported\n",
6833 phba->cfg_sriov_nr_virtfn);
6834 phba->cfg_sriov_nr_virtfn = 0;
6835 }
6836 }
6837
James Smart5248a742011-07-22 18:37:06 -04006838 return 0;
James Smartda0436e2009-05-22 14:51:39 -04006839
James Smart32517fc2019-01-28 11:14:33 -08006840out_free_hba_cpu_map:
6841 kfree(phba->sli4_hba.cpu_map);
James Smart895427b2017-02-12 13:52:30 -08006842out_free_hba_eq_hdl:
6843 kfree(phba->sli4_hba.hba_eq_hdl);
James Smart0c9ab6f2010-02-26 14:15:57 -05006844out_free_fcf_rr_bmask:
6845 kfree(phba->fcf.fcf_rr_bmask);
James Smartda0436e2009-05-22 14:51:39 -04006846out_remove_rpi_hdrs:
6847 lpfc_sli4_remove_rpi_hdrs(phba);
6848out_free_active_sgl:
6849 lpfc_free_active_sgl(phba);
James Smartda0436e2009-05-22 14:51:39 -04006850out_destroy_cq_event_pool:
6851 lpfc_sli4_cq_event_pool_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04006852out_free_bsmbx:
6853 lpfc_destroy_bootstrap_mbox(phba);
6854out_free_mem:
6855 lpfc_mem_free(phba);
6856 return rc;
6857}
6858
6859/**
6860 * lpfc_sli4_driver_resource_unset - Unset drvr internal resources for SLI4 dev
6861 * @phba: pointer to lpfc hba data structure.
6862 *
6863 * This routine is invoked to unset the driver internal resources set up
6864 * specific for supporting the SLI-4 HBA device it attached to.
6865 **/
6866static void
6867lpfc_sli4_driver_resource_unset(struct lpfc_hba *phba)
6868{
6869 struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
6870
James Smart32517fc2019-01-28 11:14:33 -08006871 free_percpu(phba->sli4_hba.eq_info);
6872
James Smart7bb03bb2013-04-17 20:19:16 -04006873 /* Free memory allocated for msi-x interrupt vector to CPU mapping */
6874 kfree(phba->sli4_hba.cpu_map);
James Smart222e9232019-01-28 11:14:35 -08006875 phba->sli4_hba.num_possible_cpu = 0;
James Smart7bb03bb2013-04-17 20:19:16 -04006876 phba->sli4_hba.num_present_cpu = 0;
James Smart76fd07a2014-02-20 09:57:18 -05006877 phba->sli4_hba.curr_disp_cpu = 0;
James Smart7bb03bb2013-04-17 20:19:16 -04006878
James Smartda0436e2009-05-22 14:51:39 -04006879 /* Free memory allocated for fast-path work queue handles */
James Smart895427b2017-02-12 13:52:30 -08006880 kfree(phba->sli4_hba.hba_eq_hdl);
James Smartda0436e2009-05-22 14:51:39 -04006881
6882 /* Free the allocated rpi headers. */
6883 lpfc_sli4_remove_rpi_hdrs(phba);
James Smartd11e31d2009-06-10 17:23:06 -04006884 lpfc_sli4_remove_rpis(phba);
James Smartda0436e2009-05-22 14:51:39 -04006885
James Smart0c9ab6f2010-02-26 14:15:57 -05006886 /* Free eligible FCF index bmask */
6887 kfree(phba->fcf.fcf_rr_bmask);
6888
James Smartda0436e2009-05-22 14:51:39 -04006889 /* Free the ELS sgl list */
6890 lpfc_free_active_sgl(phba);
James Smart8a9d2e82012-05-09 21:16:12 -04006891 lpfc_free_els_sgl_list(phba);
James Smartf358dd02017-02-12 13:52:34 -08006892 lpfc_free_nvmet_sgl_list(phba);
James Smartda0436e2009-05-22 14:51:39 -04006893
James Smartda0436e2009-05-22 14:51:39 -04006894 /* Free the completion queue EQ event pool */
6895 lpfc_sli4_cq_event_release_all(phba);
6896 lpfc_sli4_cq_event_pool_destroy(phba);
6897
James Smart6d368e52011-05-24 11:44:12 -04006898 /* Release resource identifiers. */
6899 lpfc_sli4_dealloc_resource_identifiers(phba);
6900
James Smartda0436e2009-05-22 14:51:39 -04006901 /* Free the bsmbx region. */
6902 lpfc_destroy_bootstrap_mbox(phba);
6903
6904 /* Free the SLI Layer memory with SLI4 HBAs */
6905 lpfc_mem_free_all(phba);
6906
6907 /* Free the current connect table */
6908 list_for_each_entry_safe(conn_entry, next_conn_entry,
James Smart4d9ab992009-10-02 15:16:39 -04006909 &phba->fcf_conn_rec_list, list) {
6910 list_del_init(&conn_entry->list);
James Smartda0436e2009-05-22 14:51:39 -04006911 kfree(conn_entry);
James Smart4d9ab992009-10-02 15:16:39 -04006912 }
James Smartda0436e2009-05-22 14:51:39 -04006913
6914 return;
6915}
6916
6917/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006918 * lpfc_init_api_table_setup - Set up init api function jump table
James Smart3772a992009-05-22 14:50:54 -04006919 * @phba: The hba struct for which this call is being executed.
6920 * @dev_grp: The HBA PCI-Device group number.
6921 *
6922 * This routine sets up the device INIT interface API function jump table
6923 * in @phba struct.
6924 *
6925 * Returns: 0 - success, -ENODEV - failure.
6926 **/
6927int
6928lpfc_init_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
6929{
James Smart84d1b002010-02-12 14:42:33 -05006930 phba->lpfc_hba_init_link = lpfc_hba_init_link;
6931 phba->lpfc_hba_down_link = lpfc_hba_down_link;
James Smart7f860592011-03-11 16:05:52 -05006932 phba->lpfc_selective_reset = lpfc_selective_reset;
James Smart3772a992009-05-22 14:50:54 -04006933 switch (dev_grp) {
6934 case LPFC_PCI_DEV_LP:
6935 phba->lpfc_hba_down_post = lpfc_hba_down_post_s3;
6936 phba->lpfc_handle_eratt = lpfc_handle_eratt_s3;
6937 phba->lpfc_stop_port = lpfc_stop_port_s3;
6938 break;
James Smartda0436e2009-05-22 14:51:39 -04006939 case LPFC_PCI_DEV_OC:
6940 phba->lpfc_hba_down_post = lpfc_hba_down_post_s4;
6941 phba->lpfc_handle_eratt = lpfc_handle_eratt_s4;
6942 phba->lpfc_stop_port = lpfc_stop_port_s4;
6943 break;
James Smart3772a992009-05-22 14:50:54 -04006944 default:
6945 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6946 "1431 Invalid HBA PCI-device group: 0x%x\n",
6947 dev_grp);
6948 return -ENODEV;
6949 break;
6950 }
6951 return 0;
6952}
6953
6954/**
James Smart3772a992009-05-22 14:50:54 -04006955 * lpfc_setup_driver_resource_phase2 - Phase2 setup driver internal resources.
6956 * @phba: pointer to lpfc hba data structure.
6957 *
6958 * This routine is invoked to set up the driver internal resources after the
6959 * device specific resource setup to support the HBA device it attached to.
6960 *
6961 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006962 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04006963 * other values - error
6964 **/
6965static int
6966lpfc_setup_driver_resource_phase2(struct lpfc_hba *phba)
6967{
6968 int error;
6969
6970 /* Startup the kernel thread for this host adapter. */
6971 phba->worker_thread = kthread_run(lpfc_do_work, phba,
6972 "lpfc_worker_%d", phba->brd_no);
6973 if (IS_ERR(phba->worker_thread)) {
6974 error = PTR_ERR(phba->worker_thread);
6975 return error;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05006976 }
6977
James Smart0cdb84e2018-04-09 14:24:26 -07006978 /* The lpfc_wq workqueue for deferred irq use, is only used for SLI4 */
6979 if (phba->sli_rev == LPFC_SLI_REV4)
6980 phba->wq = alloc_workqueue("lpfc_wq", WQ_MEM_RECLAIM, 0);
6981 else
6982 phba->wq = NULL;
Dick Kennedyf485c182017-09-29 17:34:34 -07006983
James Smart3772a992009-05-22 14:50:54 -04006984 return 0;
6985}
6986
6987/**
6988 * lpfc_unset_driver_resource_phase2 - Phase2 unset driver internal resources.
6989 * @phba: pointer to lpfc hba data structure.
6990 *
6991 * This routine is invoked to unset the driver internal resources set up after
6992 * the device specific resource setup for supporting the HBA device it
6993 * attached to.
6994 **/
6995static void
6996lpfc_unset_driver_resource_phase2(struct lpfc_hba *phba)
6997{
Dick Kennedyf485c182017-09-29 17:34:34 -07006998 if (phba->wq) {
6999 flush_workqueue(phba->wq);
7000 destroy_workqueue(phba->wq);
7001 phba->wq = NULL;
7002 }
7003
James Smart3772a992009-05-22 14:50:54 -04007004 /* Stop kernel worker thread */
James Smart0cdb84e2018-04-09 14:24:26 -07007005 if (phba->worker_thread)
7006 kthread_stop(phba->worker_thread);
James Smart3772a992009-05-22 14:50:54 -04007007}
7008
7009/**
7010 * lpfc_free_iocb_list - Free iocb list.
7011 * @phba: pointer to lpfc hba data structure.
7012 *
7013 * This routine is invoked to free the driver's IOCB list and memory.
7014 **/
James Smart6c621a22017-05-15 15:20:45 -07007015void
James Smart3772a992009-05-22 14:50:54 -04007016lpfc_free_iocb_list(struct lpfc_hba *phba)
7017{
7018 struct lpfc_iocbq *iocbq_entry = NULL, *iocbq_next = NULL;
7019
7020 spin_lock_irq(&phba->hbalock);
7021 list_for_each_entry_safe(iocbq_entry, iocbq_next,
7022 &phba->lpfc_iocb_list, list) {
7023 list_del(&iocbq_entry->list);
7024 kfree(iocbq_entry);
7025 phba->total_iocbq_bufs--;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05007026 }
James Smart3772a992009-05-22 14:50:54 -04007027 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007028
James Smart3772a992009-05-22 14:50:54 -04007029 return;
7030}
dea31012005-04-17 16:05:31 -05007031
James Smart3772a992009-05-22 14:50:54 -04007032/**
7033 * lpfc_init_iocb_list - Allocate and initialize iocb list.
7034 * @phba: pointer to lpfc hba data structure.
7035 *
7036 * This routine is invoked to allocate and initizlize the driver's IOCB
7037 * list and set up the IOCB tag array accordingly.
7038 *
7039 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007040 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04007041 * other values - error
7042 **/
James Smart6c621a22017-05-15 15:20:45 -07007043int
James Smart3772a992009-05-22 14:50:54 -04007044lpfc_init_iocb_list(struct lpfc_hba *phba, int iocb_count)
7045{
7046 struct lpfc_iocbq *iocbq_entry = NULL;
7047 uint16_t iotag;
7048 int i;
dea31012005-04-17 16:05:31 -05007049
7050 /* Initialize and populate the iocb list per host. */
7051 INIT_LIST_HEAD(&phba->lpfc_iocb_list);
James Smart3772a992009-05-22 14:50:54 -04007052 for (i = 0; i < iocb_count; i++) {
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07007053 iocbq_entry = kzalloc(sizeof(struct lpfc_iocbq), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05007054 if (iocbq_entry == NULL) {
7055 printk(KERN_ERR "%s: only allocated %d iocbs of "
7056 "expected %d count. Unloading driver.\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07007057 __func__, i, LPFC_IOCB_LIST_CNT);
dea31012005-04-17 16:05:31 -05007058 goto out_free_iocbq;
7059 }
7060
James Bottomley604a3e32005-10-29 10:28:33 -05007061 iotag = lpfc_sli_next_iotag(phba, iocbq_entry);
7062 if (iotag == 0) {
James Smart3772a992009-05-22 14:50:54 -04007063 kfree(iocbq_entry);
James Bottomley604a3e32005-10-29 10:28:33 -05007064 printk(KERN_ERR "%s: failed to allocate IOTAG. "
James Smart3772a992009-05-22 14:50:54 -04007065 "Unloading driver.\n", __func__);
James Bottomley604a3e32005-10-29 10:28:33 -05007066 goto out_free_iocbq;
7067 }
James Smart6d368e52011-05-24 11:44:12 -04007068 iocbq_entry->sli4_lxritag = NO_XRI;
James Smart3772a992009-05-22 14:50:54 -04007069 iocbq_entry->sli4_xritag = NO_XRI;
James Smart2e0fef82007-06-17 19:56:36 -05007070
7071 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007072 list_add(&iocbq_entry->list, &phba->lpfc_iocb_list);
7073 phba->total_iocbq_bufs++;
James Smart2e0fef82007-06-17 19:56:36 -05007074 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007075 }
7076
James Smart3772a992009-05-22 14:50:54 -04007077 return 0;
7078
7079out_free_iocbq:
7080 lpfc_free_iocb_list(phba);
7081
7082 return -ENOMEM;
7083}
7084
7085/**
James Smart8a9d2e82012-05-09 21:16:12 -04007086 * lpfc_free_sgl_list - Free a given sgl list.
James Smartda0436e2009-05-22 14:51:39 -04007087 * @phba: pointer to lpfc hba data structure.
James Smart8a9d2e82012-05-09 21:16:12 -04007088 * @sglq_list: pointer to the head of sgl list.
James Smartda0436e2009-05-22 14:51:39 -04007089 *
James Smart8a9d2e82012-05-09 21:16:12 -04007090 * This routine is invoked to free a give sgl list and memory.
James Smartda0436e2009-05-22 14:51:39 -04007091 **/
James Smart8a9d2e82012-05-09 21:16:12 -04007092void
7093lpfc_free_sgl_list(struct lpfc_hba *phba, struct list_head *sglq_list)
James Smartda0436e2009-05-22 14:51:39 -04007094{
7095 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
James Smart8a9d2e82012-05-09 21:16:12 -04007096
7097 list_for_each_entry_safe(sglq_entry, sglq_next, sglq_list, list) {
7098 list_del(&sglq_entry->list);
7099 lpfc_mbuf_free(phba, sglq_entry->virt, sglq_entry->phys);
7100 kfree(sglq_entry);
7101 }
7102}
7103
7104/**
7105 * lpfc_free_els_sgl_list - Free els sgl list.
7106 * @phba: pointer to lpfc hba data structure.
7107 *
7108 * This routine is invoked to free the driver's els sgl list and memory.
7109 **/
7110static void
7111lpfc_free_els_sgl_list(struct lpfc_hba *phba)
7112{
James Smartda0436e2009-05-22 14:51:39 -04007113 LIST_HEAD(sglq_list);
James Smartda0436e2009-05-22 14:51:39 -04007114
James Smart8a9d2e82012-05-09 21:16:12 -04007115 /* Retrieve all els sgls from driver list */
James Smartda0436e2009-05-22 14:51:39 -04007116 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -08007117 spin_lock(&phba->sli4_hba.sgl_list_lock);
7118 list_splice_init(&phba->sli4_hba.lpfc_els_sgl_list, &sglq_list);
7119 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smartda0436e2009-05-22 14:51:39 -04007120 spin_unlock_irq(&phba->hbalock);
7121
James Smart8a9d2e82012-05-09 21:16:12 -04007122 /* Now free the sgl list */
7123 lpfc_free_sgl_list(phba, &sglq_list);
James Smartda0436e2009-05-22 14:51:39 -04007124}
7125
7126/**
James Smartf358dd02017-02-12 13:52:34 -08007127 * lpfc_free_nvmet_sgl_list - Free nvmet sgl list.
7128 * @phba: pointer to lpfc hba data structure.
7129 *
7130 * This routine is invoked to free the driver's nvmet sgl list and memory.
7131 **/
7132static void
7133lpfc_free_nvmet_sgl_list(struct lpfc_hba *phba)
7134{
7135 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
7136 LIST_HEAD(sglq_list);
7137
7138 /* Retrieve all nvmet sgls from driver list */
7139 spin_lock_irq(&phba->hbalock);
7140 spin_lock(&phba->sli4_hba.sgl_list_lock);
7141 list_splice_init(&phba->sli4_hba.lpfc_nvmet_sgl_list, &sglq_list);
7142 spin_unlock(&phba->sli4_hba.sgl_list_lock);
7143 spin_unlock_irq(&phba->hbalock);
7144
7145 /* Now free the sgl list */
7146 list_for_each_entry_safe(sglq_entry, sglq_next, &sglq_list, list) {
7147 list_del(&sglq_entry->list);
7148 lpfc_nvmet_buf_free(phba, sglq_entry->virt, sglq_entry->phys);
7149 kfree(sglq_entry);
7150 }
Dick Kennedy4b40d022017-08-23 16:55:38 -07007151
7152 /* Update the nvmet_xri_cnt to reflect no current sgls.
7153 * The next initialization cycle sets the count and allocates
7154 * the sgls over again.
7155 */
7156 phba->sli4_hba.nvmet_xri_cnt = 0;
James Smartf358dd02017-02-12 13:52:34 -08007157}
7158
7159/**
James Smartda0436e2009-05-22 14:51:39 -04007160 * lpfc_init_active_sgl_array - Allocate the buf to track active ELS XRIs.
7161 * @phba: pointer to lpfc hba data structure.
7162 *
7163 * This routine is invoked to allocate the driver's active sgl memory.
7164 * This array will hold the sglq_entry's for active IOs.
7165 **/
7166static int
7167lpfc_init_active_sgl_array(struct lpfc_hba *phba)
7168{
7169 int size;
7170 size = sizeof(struct lpfc_sglq *);
7171 size *= phba->sli4_hba.max_cfg_param.max_xri;
7172
7173 phba->sli4_hba.lpfc_sglq_active_list =
7174 kzalloc(size, GFP_KERNEL);
7175 if (!phba->sli4_hba.lpfc_sglq_active_list)
7176 return -ENOMEM;
7177 return 0;
7178}
7179
7180/**
7181 * lpfc_free_active_sgl - Free the buf that tracks active ELS XRIs.
7182 * @phba: pointer to lpfc hba data structure.
7183 *
7184 * This routine is invoked to walk through the array of active sglq entries
7185 * and free all of the resources.
7186 * This is just a place holder for now.
7187 **/
7188static void
7189lpfc_free_active_sgl(struct lpfc_hba *phba)
7190{
7191 kfree(phba->sli4_hba.lpfc_sglq_active_list);
7192}
7193
7194/**
7195 * lpfc_init_sgl_list - Allocate and initialize sgl list.
7196 * @phba: pointer to lpfc hba data structure.
7197 *
7198 * This routine is invoked to allocate and initizlize the driver's sgl
7199 * list and set up the sgl xritag tag array accordingly.
7200 *
James Smartda0436e2009-05-22 14:51:39 -04007201 **/
James Smart8a9d2e82012-05-09 21:16:12 -04007202static void
James Smartda0436e2009-05-22 14:51:39 -04007203lpfc_init_sgl_list(struct lpfc_hba *phba)
7204{
James Smartda0436e2009-05-22 14:51:39 -04007205 /* Initialize and populate the sglq list per host/VF. */
James Smart895427b2017-02-12 13:52:30 -08007206 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_els_sgl_list);
James Smartda0436e2009-05-22 14:51:39 -04007207 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_els_sgl_list);
James Smartf358dd02017-02-12 13:52:34 -08007208 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_nvmet_sgl_list);
James Smart86c67372017-04-21 16:05:04 -07007209 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_nvmet_ctx_list);
James Smartda0436e2009-05-22 14:51:39 -04007210
James Smart8a9d2e82012-05-09 21:16:12 -04007211 /* els xri-sgl book keeping */
7212 phba->sli4_hba.els_xri_cnt = 0;
James Smartda0436e2009-05-22 14:51:39 -04007213
James Smart895427b2017-02-12 13:52:30 -08007214 /* nvme xri-buffer book keeping */
James Smart5e5b5112019-01-28 11:14:22 -08007215 phba->sli4_hba.io_xri_cnt = 0;
James Smartda0436e2009-05-22 14:51:39 -04007216}
7217
7218/**
7219 * lpfc_sli4_init_rpi_hdrs - Post the rpi header memory region to the port
7220 * @phba: pointer to lpfc hba data structure.
7221 *
7222 * This routine is invoked to post rpi header templates to the
James Smart88a2cfb2011-07-22 18:36:33 -04007223 * port for those SLI4 ports that do not support extents. This routine
James Smartda0436e2009-05-22 14:51:39 -04007224 * posts a PAGE_SIZE memory region to the port to hold up to
James Smart88a2cfb2011-07-22 18:36:33 -04007225 * PAGE_SIZE modulo 64 rpi context headers. This is an initialization routine
7226 * and should be called only when interrupts are disabled.
James Smartda0436e2009-05-22 14:51:39 -04007227 *
7228 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007229 * 0 - successful
James Smart88a2cfb2011-07-22 18:36:33 -04007230 * -ERROR - otherwise.
James Smartda0436e2009-05-22 14:51:39 -04007231 **/
7232int
7233lpfc_sli4_init_rpi_hdrs(struct lpfc_hba *phba)
7234{
7235 int rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04007236 struct lpfc_rpi_hdr *rpi_hdr;
7237
7238 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_hdr_list);
James Smartff78d8f2011-12-13 13:21:35 -05007239 if (!phba->sli4_hba.rpi_hdrs_in_use)
James Smart6d368e52011-05-24 11:44:12 -04007240 return rc;
James Smart6d368e52011-05-24 11:44:12 -04007241 if (phba->sli4_hba.extents_in_use)
7242 return -EIO;
James Smartda0436e2009-05-22 14:51:39 -04007243
7244 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
7245 if (!rpi_hdr) {
7246 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7247 "0391 Error during rpi post operation\n");
7248 lpfc_sli4_remove_rpis(phba);
7249 rc = -ENODEV;
7250 }
7251
7252 return rc;
7253}
7254
7255/**
7256 * lpfc_sli4_create_rpi_hdr - Allocate an rpi header memory region
7257 * @phba: pointer to lpfc hba data structure.
7258 *
7259 * This routine is invoked to allocate a single 4KB memory region to
7260 * support rpis and stores them in the phba. This single region
7261 * provides support for up to 64 rpis. The region is used globally
7262 * by the device.
7263 *
7264 * Returns:
7265 * A valid rpi hdr on success.
7266 * A NULL pointer on any failure.
7267 **/
7268struct lpfc_rpi_hdr *
7269lpfc_sli4_create_rpi_hdr(struct lpfc_hba *phba)
7270{
7271 uint16_t rpi_limit, curr_rpi_range;
7272 struct lpfc_dmabuf *dmabuf;
7273 struct lpfc_rpi_hdr *rpi_hdr;
7274
James Smart6d368e52011-05-24 11:44:12 -04007275 /*
7276 * If the SLI4 port supports extents, posting the rpi header isn't
7277 * required. Set the expected maximum count and let the actual value
7278 * get set when extents are fully allocated.
7279 */
7280 if (!phba->sli4_hba.rpi_hdrs_in_use)
7281 return NULL;
7282 if (phba->sli4_hba.extents_in_use)
7283 return NULL;
7284
7285 /* The limit on the logical index is just the max_rpi count. */
James Smart845d9e82017-05-15 15:20:38 -07007286 rpi_limit = phba->sli4_hba.max_cfg_param.max_rpi;
James Smartda0436e2009-05-22 14:51:39 -04007287
7288 spin_lock_irq(&phba->hbalock);
James Smart6d368e52011-05-24 11:44:12 -04007289 /*
7290 * Establish the starting RPI in this header block. The starting
7291 * rpi is normalized to a zero base because the physical rpi is
7292 * port based.
7293 */
James Smart97f2ecf2012-03-01 22:35:23 -05007294 curr_rpi_range = phba->sli4_hba.next_rpi;
James Smartda0436e2009-05-22 14:51:39 -04007295 spin_unlock_irq(&phba->hbalock);
7296
James Smart845d9e82017-05-15 15:20:38 -07007297 /* Reached full RPI range */
7298 if (curr_rpi_range == rpi_limit)
James Smart6d368e52011-05-24 11:44:12 -04007299 return NULL;
James Smart845d9e82017-05-15 15:20:38 -07007300
James Smartda0436e2009-05-22 14:51:39 -04007301 /*
7302 * First allocate the protocol header region for the port. The
7303 * port expects a 4KB DMA-mapped memory region that is 4K aligned.
7304 */
7305 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
7306 if (!dmabuf)
7307 return NULL;
7308
Joe Perches1aee3832014-09-03 12:56:12 -04007309 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev,
7310 LPFC_HDR_TEMPLATE_SIZE,
7311 &dmabuf->phys, GFP_KERNEL);
James Smartda0436e2009-05-22 14:51:39 -04007312 if (!dmabuf->virt) {
7313 rpi_hdr = NULL;
7314 goto err_free_dmabuf;
7315 }
7316
James Smartda0436e2009-05-22 14:51:39 -04007317 if (!IS_ALIGNED(dmabuf->phys, LPFC_HDR_TEMPLATE_SIZE)) {
7318 rpi_hdr = NULL;
7319 goto err_free_coherent;
7320 }
7321
7322 /* Save the rpi header data for cleanup later. */
7323 rpi_hdr = kzalloc(sizeof(struct lpfc_rpi_hdr), GFP_KERNEL);
7324 if (!rpi_hdr)
7325 goto err_free_coherent;
7326
7327 rpi_hdr->dmabuf = dmabuf;
7328 rpi_hdr->len = LPFC_HDR_TEMPLATE_SIZE;
7329 rpi_hdr->page_count = 1;
7330 spin_lock_irq(&phba->hbalock);
James Smart6d368e52011-05-24 11:44:12 -04007331
7332 /* The rpi_hdr stores the logical index only. */
7333 rpi_hdr->start_rpi = curr_rpi_range;
James Smart845d9e82017-05-15 15:20:38 -07007334 rpi_hdr->next_rpi = phba->sli4_hba.next_rpi + LPFC_RPI_HDR_COUNT;
James Smartda0436e2009-05-22 14:51:39 -04007335 list_add_tail(&rpi_hdr->list, &phba->sli4_hba.lpfc_rpi_hdr_list);
7336
James Smartda0436e2009-05-22 14:51:39 -04007337 spin_unlock_irq(&phba->hbalock);
7338 return rpi_hdr;
7339
7340 err_free_coherent:
7341 dma_free_coherent(&phba->pcidev->dev, LPFC_HDR_TEMPLATE_SIZE,
7342 dmabuf->virt, dmabuf->phys);
7343 err_free_dmabuf:
7344 kfree(dmabuf);
7345 return NULL;
7346}
7347
7348/**
7349 * lpfc_sli4_remove_rpi_hdrs - Remove all rpi header memory regions
7350 * @phba: pointer to lpfc hba data structure.
7351 *
7352 * This routine is invoked to remove all memory resources allocated
James Smart6d368e52011-05-24 11:44:12 -04007353 * to support rpis for SLI4 ports not supporting extents. This routine
7354 * presumes the caller has released all rpis consumed by fabric or port
7355 * logins and is prepared to have the header pages removed.
James Smartda0436e2009-05-22 14:51:39 -04007356 **/
7357void
7358lpfc_sli4_remove_rpi_hdrs(struct lpfc_hba *phba)
7359{
7360 struct lpfc_rpi_hdr *rpi_hdr, *next_rpi_hdr;
7361
James Smart6d368e52011-05-24 11:44:12 -04007362 if (!phba->sli4_hba.rpi_hdrs_in_use)
7363 goto exit;
7364
James Smartda0436e2009-05-22 14:51:39 -04007365 list_for_each_entry_safe(rpi_hdr, next_rpi_hdr,
7366 &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
7367 list_del(&rpi_hdr->list);
7368 dma_free_coherent(&phba->pcidev->dev, rpi_hdr->len,
7369 rpi_hdr->dmabuf->virt, rpi_hdr->dmabuf->phys);
7370 kfree(rpi_hdr->dmabuf);
7371 kfree(rpi_hdr);
7372 }
James Smart6d368e52011-05-24 11:44:12 -04007373 exit:
7374 /* There are no rpis available to the port now. */
7375 phba->sli4_hba.next_rpi = 0;
James Smartda0436e2009-05-22 14:51:39 -04007376}
7377
7378/**
James Smart3772a992009-05-22 14:50:54 -04007379 * lpfc_hba_alloc - Allocate driver hba data structure for a device.
7380 * @pdev: pointer to pci device data structure.
7381 *
7382 * This routine is invoked to allocate the driver hba data structure for an
7383 * HBA device. If the allocation is successful, the phba reference to the
7384 * PCI device data structure is set.
7385 *
7386 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007387 * pointer to @phba - successful
James Smart3772a992009-05-22 14:50:54 -04007388 * NULL - error
7389 **/
7390static struct lpfc_hba *
7391lpfc_hba_alloc(struct pci_dev *pdev)
7392{
7393 struct lpfc_hba *phba;
7394
7395 /* Allocate memory for HBA structure */
7396 phba = kzalloc(sizeof(struct lpfc_hba), GFP_KERNEL);
7397 if (!phba) {
Jiri Slabye34ccdf2009-07-13 23:25:54 +02007398 dev_err(&pdev->dev, "failed to allocate hba struct\n");
James Smart3772a992009-05-22 14:50:54 -04007399 return NULL;
7400 }
7401
7402 /* Set reference to PCI device in HBA structure */
7403 phba->pcidev = pdev;
7404
7405 /* Assign an unused board number */
7406 phba->brd_no = lpfc_get_instance();
7407 if (phba->brd_no < 0) {
7408 kfree(phba);
7409 return NULL;
7410 }
James Smart65791f12016-07-06 12:35:56 -07007411 phba->eratt_poll_interval = LPFC_ERATT_POLL_INTERVAL;
James Smart3772a992009-05-22 14:50:54 -04007412
James Smart4fede782010-01-26 23:08:55 -05007413 spin_lock_init(&phba->ct_ev_lock);
James Smartf1c3b0f2009-07-19 10:01:32 -04007414 INIT_LIST_HEAD(&phba->ct_ev_waiters);
7415
James Smart3772a992009-05-22 14:50:54 -04007416 return phba;
7417}
7418
7419/**
7420 * lpfc_hba_free - Free driver hba data structure with a device.
7421 * @phba: pointer to lpfc hba data structure.
7422 *
7423 * This routine is invoked to free the driver hba data structure with an
7424 * HBA device.
7425 **/
7426static void
7427lpfc_hba_free(struct lpfc_hba *phba)
7428{
James Smart5e5b5112019-01-28 11:14:22 -08007429 if (phba->sli_rev == LPFC_SLI_REV4)
7430 kfree(phba->sli4_hba.hdwq);
7431
James Smart3772a992009-05-22 14:50:54 -04007432 /* Release the driver assigned board number */
7433 idr_remove(&lpfc_hba_index, phba->brd_no);
7434
James Smart895427b2017-02-12 13:52:30 -08007435 /* Free memory allocated with sli3 rings */
7436 kfree(phba->sli.sli3_ring);
7437 phba->sli.sli3_ring = NULL;
James Smart2a76a282012-08-03 12:35:54 -04007438
James Smart3772a992009-05-22 14:50:54 -04007439 kfree(phba);
7440 return;
7441}
7442
7443/**
7444 * lpfc_create_shost - Create hba physical port with associated scsi host.
7445 * @phba: pointer to lpfc hba data structure.
7446 *
7447 * This routine is invoked to create HBA physical port and associate a SCSI
7448 * host with it.
7449 *
7450 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007451 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04007452 * other values - error
7453 **/
7454static int
7455lpfc_create_shost(struct lpfc_hba *phba)
7456{
7457 struct lpfc_vport *vport;
7458 struct Scsi_Host *shost;
7459
7460 /* Initialize HBA FC structure */
dea31012005-04-17 16:05:31 -05007461 phba->fc_edtov = FF_DEF_EDTOV;
7462 phba->fc_ratov = FF_DEF_RATOV;
7463 phba->fc_altov = FF_DEF_ALTOV;
7464 phba->fc_arbtov = FF_DEF_ARBTOV;
7465
James Smartd7c47992010-06-08 18:31:54 -04007466 atomic_set(&phba->sdev_cnt, 0);
James Smart3de2a652007-08-02 11:09:59 -04007467 vport = lpfc_create_port(phba, phba->brd_no, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05007468 if (!vport)
James Smart3772a992009-05-22 14:50:54 -04007469 return -ENODEV;
James Smart2e0fef82007-06-17 19:56:36 -05007470
7471 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05007472 phba->pport = vport;
James Smart2ea259e2017-02-12 13:52:27 -08007473
James Smartf358dd02017-02-12 13:52:34 -08007474 if (phba->nvmet_support) {
7475 /* Only 1 vport (pport) will support NVME target */
7476 if (phba->txrdy_payload_pool == NULL) {
Romain Perier771db5c2017-07-06 10:13:05 +02007477 phba->txrdy_payload_pool = dma_pool_create(
7478 "txrdy_pool", &phba->pcidev->dev,
James Smartf358dd02017-02-12 13:52:34 -08007479 TXRDY_PAYLOAD_LEN, 16, 0);
7480 if (phba->txrdy_payload_pool) {
7481 phba->targetport = NULL;
7482 phba->cfg_enable_fc4_type = LPFC_ENABLE_NVME;
7483 lpfc_printf_log(phba, KERN_INFO,
7484 LOG_INIT | LOG_NVME_DISC,
7485 "6076 NVME Target Found\n");
7486 }
7487 }
7488 }
7489
James Smart858c9f62007-06-17 19:56:39 -05007490 lpfc_debugfs_initialize(vport);
James Smart3772a992009-05-22 14:50:54 -04007491 /* Put reference to SCSI host to driver's device private data */
7492 pci_set_drvdata(phba->pcidev, shost);
James Smart2e0fef82007-06-17 19:56:36 -05007493
James Smart4258e982015-12-16 18:11:58 -05007494 /*
7495 * At this point we are fully registered with PSA. In addition,
7496 * any initial discovery should be completed.
7497 */
7498 vport->load_flag |= FC_ALLOW_FDMI;
James Smart8663cbb2016-03-31 14:12:33 -07007499 if (phba->cfg_enable_SmartSAN ||
7500 (phba->cfg_fdmi_on == LPFC_FDMI_SUPPORT)) {
James Smart4258e982015-12-16 18:11:58 -05007501
7502 /* Setup appropriate attribute masks */
7503 vport->fdmi_hba_mask = LPFC_FDMI2_HBA_ATTR;
James Smart8663cbb2016-03-31 14:12:33 -07007504 if (phba->cfg_enable_SmartSAN)
James Smart4258e982015-12-16 18:11:58 -05007505 vport->fdmi_port_mask = LPFC_FDMI2_SMART_ATTR;
7506 else
7507 vport->fdmi_port_mask = LPFC_FDMI2_PORT_ATTR;
7508 }
James Smart3772a992009-05-22 14:50:54 -04007509 return 0;
7510}
dea31012005-04-17 16:05:31 -05007511
James Smart3772a992009-05-22 14:50:54 -04007512/**
7513 * lpfc_destroy_shost - Destroy hba physical port with associated scsi host.
7514 * @phba: pointer to lpfc hba data structure.
7515 *
7516 * This routine is invoked to destroy HBA physical port and the associated
7517 * SCSI host.
7518 **/
7519static void
7520lpfc_destroy_shost(struct lpfc_hba *phba)
7521{
7522 struct lpfc_vport *vport = phba->pport;
James Smart93996272008-08-24 21:50:30 -04007523
James Smart3772a992009-05-22 14:50:54 -04007524 /* Destroy physical port that associated with the SCSI host */
7525 destroy_port(vport);
7526
7527 return;
7528}
7529
7530/**
7531 * lpfc_setup_bg - Setup Block guard structures and debug areas.
7532 * @phba: pointer to lpfc hba data structure.
7533 * @shost: the shost to be used to detect Block guard settings.
7534 *
7535 * This routine sets up the local Block guard protocol settings for @shost.
7536 * This routine also allocates memory for debugging bg buffers.
7537 **/
7538static void
7539lpfc_setup_bg(struct lpfc_hba *phba, struct Scsi_Host *shost)
7540{
James Smartbbeb79b2012-06-12 13:54:27 -04007541 uint32_t old_mask;
7542 uint32_t old_guard;
7543
James Smart3772a992009-05-22 14:50:54 -04007544 int pagecnt = 10;
James Smartb3b98b72016-10-13 15:06:06 -07007545 if (phba->cfg_prot_mask && phba->cfg_prot_guard) {
James Smart3772a992009-05-22 14:50:54 -04007546 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7547 "1478 Registering BlockGuard with the "
7548 "SCSI layer\n");
James Smartbbeb79b2012-06-12 13:54:27 -04007549
James Smartb3b98b72016-10-13 15:06:06 -07007550 old_mask = phba->cfg_prot_mask;
7551 old_guard = phba->cfg_prot_guard;
James Smartbbeb79b2012-06-12 13:54:27 -04007552
7553 /* Only allow supported values */
James Smartb3b98b72016-10-13 15:06:06 -07007554 phba->cfg_prot_mask &= (SHOST_DIF_TYPE1_PROTECTION |
James Smartbbeb79b2012-06-12 13:54:27 -04007555 SHOST_DIX_TYPE0_PROTECTION |
7556 SHOST_DIX_TYPE1_PROTECTION);
James Smartb3b98b72016-10-13 15:06:06 -07007557 phba->cfg_prot_guard &= (SHOST_DIX_GUARD_IP |
7558 SHOST_DIX_GUARD_CRC);
James Smartbbeb79b2012-06-12 13:54:27 -04007559
7560 /* DIF Type 1 protection for profiles AST1/C1 is end to end */
James Smartb3b98b72016-10-13 15:06:06 -07007561 if (phba->cfg_prot_mask == SHOST_DIX_TYPE1_PROTECTION)
7562 phba->cfg_prot_mask |= SHOST_DIF_TYPE1_PROTECTION;
James Smartbbeb79b2012-06-12 13:54:27 -04007563
James Smartb3b98b72016-10-13 15:06:06 -07007564 if (phba->cfg_prot_mask && phba->cfg_prot_guard) {
7565 if ((old_mask != phba->cfg_prot_mask) ||
7566 (old_guard != phba->cfg_prot_guard))
James Smartbbeb79b2012-06-12 13:54:27 -04007567 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7568 "1475 Registering BlockGuard with the "
7569 "SCSI layer: mask %d guard %d\n",
James Smartb3b98b72016-10-13 15:06:06 -07007570 phba->cfg_prot_mask,
7571 phba->cfg_prot_guard);
James Smartbbeb79b2012-06-12 13:54:27 -04007572
James Smartb3b98b72016-10-13 15:06:06 -07007573 scsi_host_set_prot(shost, phba->cfg_prot_mask);
7574 scsi_host_set_guard(shost, phba->cfg_prot_guard);
James Smartbbeb79b2012-06-12 13:54:27 -04007575 } else
7576 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7577 "1479 Not Registering BlockGuard with the SCSI "
7578 "layer, Bad protection parameters: %d %d\n",
7579 old_mask, old_guard);
James Smart98c9ea52007-10-27 13:37:33 -04007580 }
James Smartbbeb79b2012-06-12 13:54:27 -04007581
James Smart81301a92008-12-04 22:39:46 -05007582 if (!_dump_buf_data) {
James Smart81301a92008-12-04 22:39:46 -05007583 while (pagecnt) {
7584 spin_lock_init(&_dump_buf_lock);
7585 _dump_buf_data =
7586 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
7587 if (_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -04007588 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
7589 "9043 BLKGRD: allocated %d pages for "
James Smart3772a992009-05-22 14:50:54 -04007590 "_dump_buf_data at 0x%p\n",
7591 (1 << pagecnt), _dump_buf_data);
James Smart81301a92008-12-04 22:39:46 -05007592 _dump_buf_data_order = pagecnt;
James Smart3772a992009-05-22 14:50:54 -04007593 memset(_dump_buf_data, 0,
7594 ((1 << PAGE_SHIFT) << pagecnt));
James Smart81301a92008-12-04 22:39:46 -05007595 break;
James Smart3772a992009-05-22 14:50:54 -04007596 } else
James Smart81301a92008-12-04 22:39:46 -05007597 --pagecnt;
James Smart81301a92008-12-04 22:39:46 -05007598 }
James Smart81301a92008-12-04 22:39:46 -05007599 if (!_dump_buf_data_order)
James Smart6a9c52c2009-10-02 15:16:51 -04007600 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
7601 "9044 BLKGRD: ERROR unable to allocate "
James Smart3772a992009-05-22 14:50:54 -04007602 "memory for hexdump\n");
7603 } else
James Smart6a9c52c2009-10-02 15:16:51 -04007604 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
7605 "9045 BLKGRD: already allocated _dump_buf_data=0x%p"
James Smart81301a92008-12-04 22:39:46 -05007606 "\n", _dump_buf_data);
James Smart81301a92008-12-04 22:39:46 -05007607 if (!_dump_buf_dif) {
James Smart81301a92008-12-04 22:39:46 -05007608 while (pagecnt) {
7609 _dump_buf_dif =
7610 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
7611 if (_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -04007612 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
7613 "9046 BLKGRD: allocated %d pages for "
James Smart3772a992009-05-22 14:50:54 -04007614 "_dump_buf_dif at 0x%p\n",
7615 (1 << pagecnt), _dump_buf_dif);
James Smart81301a92008-12-04 22:39:46 -05007616 _dump_buf_dif_order = pagecnt;
James Smart3772a992009-05-22 14:50:54 -04007617 memset(_dump_buf_dif, 0,
7618 ((1 << PAGE_SHIFT) << pagecnt));
James Smart81301a92008-12-04 22:39:46 -05007619 break;
James Smart3772a992009-05-22 14:50:54 -04007620 } else
James Smart81301a92008-12-04 22:39:46 -05007621 --pagecnt;
James Smart81301a92008-12-04 22:39:46 -05007622 }
James Smart81301a92008-12-04 22:39:46 -05007623 if (!_dump_buf_dif_order)
James Smart6a9c52c2009-10-02 15:16:51 -04007624 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
7625 "9047 BLKGRD: ERROR unable to allocate "
James Smart3772a992009-05-22 14:50:54 -04007626 "memory for hexdump\n");
7627 } else
James Smart6a9c52c2009-10-02 15:16:51 -04007628 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
7629 "9048 BLKGRD: already allocated _dump_buf_dif=0x%p\n",
James Smart3772a992009-05-22 14:50:54 -04007630 _dump_buf_dif);
7631}
7632
7633/**
7634 * lpfc_post_init_setup - Perform necessary device post initialization setup.
7635 * @phba: pointer to lpfc hba data structure.
7636 *
7637 * This routine is invoked to perform all the necessary post initialization
7638 * setup for the device.
7639 **/
7640static void
7641lpfc_post_init_setup(struct lpfc_hba *phba)
7642{
7643 struct Scsi_Host *shost;
7644 struct lpfc_adapter_event_header adapter_event;
7645
7646 /* Get the default values for Model Name and Description */
7647 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
7648
7649 /*
7650 * hba setup may have changed the hba_queue_depth so we need to
7651 * adjust the value of can_queue.
7652 */
7653 shost = pci_get_drvdata(phba->pcidev);
7654 shost->can_queue = phba->cfg_hba_queue_depth - 10;
7655 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED)
7656 lpfc_setup_bg(phba, shost);
James Smart858c9f62007-06-17 19:56:39 -05007657
7658 lpfc_host_attrib_init(shost);
7659
James Smart2e0fef82007-06-17 19:56:36 -05007660 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
7661 spin_lock_irq(shost->host_lock);
7662 lpfc_poll_start_timer(phba);
7663 spin_unlock_irq(shost->host_lock);
7664 }
James Smart8f6d98d2006-08-01 07:34:00 -04007665
James Smart93996272008-08-24 21:50:30 -04007666 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7667 "0428 Perform SCSI scan\n");
James Smartea2151b2008-09-07 11:52:10 -04007668 /* Send board arrival event to upper layer */
7669 adapter_event.event_type = FC_REG_ADAPTER_EVENT;
7670 adapter_event.subcategory = LPFC_EVENT_ARRIVAL;
7671 fc_host_post_vendor_event(shost, fc_get_event_number(),
James Smart3772a992009-05-22 14:50:54 -04007672 sizeof(adapter_event),
7673 (char *) &adapter_event,
7674 LPFC_NL_VENDOR_ID);
7675 return;
7676}
7677
7678/**
7679 * lpfc_sli_pci_mem_setup - Setup SLI3 HBA PCI memory space.
7680 * @phba: pointer to lpfc hba data structure.
7681 *
7682 * This routine is invoked to set up the PCI device memory space for device
7683 * with SLI-3 interface spec.
7684 *
7685 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007686 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04007687 * other values - error
7688 **/
7689static int
7690lpfc_sli_pci_mem_setup(struct lpfc_hba *phba)
7691{
Christoph Hellwigf30e1bf2018-10-18 15:10:21 +02007692 struct pci_dev *pdev = phba->pcidev;
James Smart3772a992009-05-22 14:50:54 -04007693 unsigned long bar0map_len, bar2map_len;
7694 int i, hbq_count;
7695 void *ptr;
7696 int error = -ENODEV;
7697
Christoph Hellwigf30e1bf2018-10-18 15:10:21 +02007698 if (!pdev)
James Smart3772a992009-05-22 14:50:54 -04007699 return error;
James Smart3772a992009-05-22 14:50:54 -04007700
7701 /* Set the device DMA mask size */
Christoph Hellwigf30e1bf2018-10-18 15:10:21 +02007702 if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) ||
7703 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)))
7704 return error;
James Smart3772a992009-05-22 14:50:54 -04007705
7706 /* Get the bus address of Bar0 and Bar2 and the number of bytes
7707 * required by each mapping.
7708 */
7709 phba->pci_bar0_map = pci_resource_start(pdev, 0);
7710 bar0map_len = pci_resource_len(pdev, 0);
7711
7712 phba->pci_bar2_map = pci_resource_start(pdev, 2);
7713 bar2map_len = pci_resource_len(pdev, 2);
7714
7715 /* Map HBA SLIM to a kernel virtual address. */
7716 phba->slim_memmap_p = ioremap(phba->pci_bar0_map, bar0map_len);
7717 if (!phba->slim_memmap_p) {
7718 dev_printk(KERN_ERR, &pdev->dev,
7719 "ioremap failed for SLIM memory.\n");
7720 goto out;
7721 }
7722
7723 /* Map HBA Control Registers to a kernel virtual address. */
7724 phba->ctrl_regs_memmap_p = ioremap(phba->pci_bar2_map, bar2map_len);
7725 if (!phba->ctrl_regs_memmap_p) {
7726 dev_printk(KERN_ERR, &pdev->dev,
7727 "ioremap failed for HBA control registers.\n");
7728 goto out_iounmap_slim;
7729 }
7730
7731 /* Allocate memory for SLI-2 structures */
Joe Perches1aee3832014-09-03 12:56:12 -04007732 phba->slim2p.virt = dma_zalloc_coherent(&pdev->dev, SLI2_SLIM_SIZE,
7733 &phba->slim2p.phys, GFP_KERNEL);
James Smart3772a992009-05-22 14:50:54 -04007734 if (!phba->slim2p.virt)
7735 goto out_iounmap;
7736
James Smart3772a992009-05-22 14:50:54 -04007737 phba->mbox = phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, mbx);
James Smart7a470272010-03-15 11:25:20 -04007738 phba->mbox_ext = (phba->slim2p.virt +
7739 offsetof(struct lpfc_sli2_slim, mbx_ext_words));
James Smart3772a992009-05-22 14:50:54 -04007740 phba->pcb = (phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, pcb));
7741 phba->IOCBs = (phba->slim2p.virt +
7742 offsetof(struct lpfc_sli2_slim, IOCBs));
7743
7744 phba->hbqslimp.virt = dma_alloc_coherent(&pdev->dev,
7745 lpfc_sli_hbq_size(),
7746 &phba->hbqslimp.phys,
7747 GFP_KERNEL);
7748 if (!phba->hbqslimp.virt)
7749 goto out_free_slim;
7750
7751 hbq_count = lpfc_sli_hbq_count();
7752 ptr = phba->hbqslimp.virt;
7753 for (i = 0; i < hbq_count; ++i) {
7754 phba->hbqs[i].hbq_virt = ptr;
7755 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
7756 ptr += (lpfc_hbq_defs[i]->entry_count *
7757 sizeof(struct lpfc_hbq_entry));
7758 }
7759 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_els_hbq_alloc;
7760 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_els_hbq_free;
7761
7762 memset(phba->hbqslimp.virt, 0, lpfc_sli_hbq_size());
7763
James Smart3772a992009-05-22 14:50:54 -04007764 phba->MBslimaddr = phba->slim_memmap_p;
7765 phba->HAregaddr = phba->ctrl_regs_memmap_p + HA_REG_OFFSET;
7766 phba->CAregaddr = phba->ctrl_regs_memmap_p + CA_REG_OFFSET;
7767 phba->HSregaddr = phba->ctrl_regs_memmap_p + HS_REG_OFFSET;
7768 phba->HCregaddr = phba->ctrl_regs_memmap_p + HC_REG_OFFSET;
James Smartea2151b2008-09-07 11:52:10 -04007769
dea31012005-04-17 16:05:31 -05007770 return 0;
7771
dea31012005-04-17 16:05:31 -05007772out_free_slim:
James Smart34b02dc2008-08-24 21:49:55 -04007773 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
7774 phba->slim2p.virt, phba->slim2p.phys);
dea31012005-04-17 16:05:31 -05007775out_iounmap:
7776 iounmap(phba->ctrl_regs_memmap_p);
Jamie Wellnitz901a9202006-02-28 19:25:19 -05007777out_iounmap_slim:
dea31012005-04-17 16:05:31 -05007778 iounmap(phba->slim_memmap_p);
dea31012005-04-17 16:05:31 -05007779out:
7780 return error;
7781}
7782
James Smarte59058c2008-08-24 21:49:00 -04007783/**
James Smart3772a992009-05-22 14:50:54 -04007784 * lpfc_sli_pci_mem_unset - Unset SLI3 HBA PCI memory space.
7785 * @phba: pointer to lpfc hba data structure.
7786 *
7787 * This routine is invoked to unset the PCI device memory space for device
7788 * with SLI-3 interface spec.
7789 **/
7790static void
7791lpfc_sli_pci_mem_unset(struct lpfc_hba *phba)
7792{
7793 struct pci_dev *pdev;
7794
7795 /* Obtain PCI device reference */
7796 if (!phba->pcidev)
7797 return;
7798 else
7799 pdev = phba->pcidev;
7800
7801 /* Free coherent DMA memory allocated */
7802 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
7803 phba->hbqslimp.virt, phba->hbqslimp.phys);
7804 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
7805 phba->slim2p.virt, phba->slim2p.phys);
7806
7807 /* I/O memory unmap */
7808 iounmap(phba->ctrl_regs_memmap_p);
7809 iounmap(phba->slim_memmap_p);
7810
7811 return;
7812}
7813
7814/**
James Smartda0436e2009-05-22 14:51:39 -04007815 * lpfc_sli4_post_status_check - Wait for SLI4 POST done and check status
7816 * @phba: pointer to lpfc hba data structure.
7817 *
7818 * This routine is invoked to wait for SLI4 device Power On Self Test (POST)
7819 * done and check status.
7820 *
7821 * Return 0 if successful, otherwise -ENODEV.
7822 **/
7823int
7824lpfc_sli4_post_status_check(struct lpfc_hba *phba)
7825{
James Smart2fcee4b2010-12-15 17:57:46 -05007826 struct lpfc_register portsmphr_reg, uerrlo_reg, uerrhi_reg;
7827 struct lpfc_register reg_data;
7828 int i, port_error = 0;
7829 uint32_t if_type;
James Smartda0436e2009-05-22 14:51:39 -04007830
James Smart9940b972011-03-11 16:06:12 -05007831 memset(&portsmphr_reg, 0, sizeof(portsmphr_reg));
7832 memset(&reg_data, 0, sizeof(reg_data));
James Smart2fcee4b2010-12-15 17:57:46 -05007833 if (!phba->sli4_hba.PSMPHRregaddr)
James Smartda0436e2009-05-22 14:51:39 -04007834 return -ENODEV;
7835
James Smartda0436e2009-05-22 14:51:39 -04007836 /* Wait up to 30 seconds for the SLI Port POST done and ready */
7837 for (i = 0; i < 3000; i++) {
James Smart9940b972011-03-11 16:06:12 -05007838 if (lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
7839 &portsmphr_reg.word0) ||
7840 (bf_get(lpfc_port_smphr_perr, &portsmphr_reg))) {
James Smart2fcee4b2010-12-15 17:57:46 -05007841 /* Port has a fatal POST error, break out */
James Smartda0436e2009-05-22 14:51:39 -04007842 port_error = -ENODEV;
7843 break;
7844 }
James Smart2fcee4b2010-12-15 17:57:46 -05007845 if (LPFC_POST_STAGE_PORT_READY ==
7846 bf_get(lpfc_port_smphr_port_status, &portsmphr_reg))
James Smartda0436e2009-05-22 14:51:39 -04007847 break;
James Smartda0436e2009-05-22 14:51:39 -04007848 msleep(10);
7849 }
7850
James Smart2fcee4b2010-12-15 17:57:46 -05007851 /*
7852 * If there was a port error during POST, then don't proceed with
7853 * other register reads as the data may not be valid. Just exit.
7854 */
7855 if (port_error) {
James Smartda0436e2009-05-22 14:51:39 -04007856 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -05007857 "1408 Port Failed POST - portsmphr=0x%x, "
7858 "perr=x%x, sfi=x%x, nip=x%x, ipc=x%x, scr1=x%x, "
7859 "scr2=x%x, hscratch=x%x, pstatus=x%x\n",
7860 portsmphr_reg.word0,
7861 bf_get(lpfc_port_smphr_perr, &portsmphr_reg),
7862 bf_get(lpfc_port_smphr_sfi, &portsmphr_reg),
7863 bf_get(lpfc_port_smphr_nip, &portsmphr_reg),
7864 bf_get(lpfc_port_smphr_ipc, &portsmphr_reg),
7865 bf_get(lpfc_port_smphr_scr1, &portsmphr_reg),
7866 bf_get(lpfc_port_smphr_scr2, &portsmphr_reg),
7867 bf_get(lpfc_port_smphr_host_scratch, &portsmphr_reg),
7868 bf_get(lpfc_port_smphr_port_status, &portsmphr_reg));
7869 } else {
James Smart28baac72010-02-12 14:42:03 -05007870 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -05007871 "2534 Device Info: SLIFamily=0x%x, "
7872 "SLIRev=0x%x, IFType=0x%x, SLIHint_1=0x%x, "
7873 "SLIHint_2=0x%x, FT=0x%x\n",
James Smart28baac72010-02-12 14:42:03 -05007874 bf_get(lpfc_sli_intf_sli_family,
7875 &phba->sli4_hba.sli_intf),
7876 bf_get(lpfc_sli_intf_slirev,
7877 &phba->sli4_hba.sli_intf),
James Smart085c6472010-11-20 23:11:37 -05007878 bf_get(lpfc_sli_intf_if_type,
James Smart28baac72010-02-12 14:42:03 -05007879 &phba->sli4_hba.sli_intf),
James Smart085c6472010-11-20 23:11:37 -05007880 bf_get(lpfc_sli_intf_sli_hint1,
7881 &phba->sli4_hba.sli_intf),
7882 bf_get(lpfc_sli_intf_sli_hint2,
7883 &phba->sli4_hba.sli_intf),
7884 bf_get(lpfc_sli_intf_func_type,
James Smart28baac72010-02-12 14:42:03 -05007885 &phba->sli4_hba.sli_intf));
James Smart2fcee4b2010-12-15 17:57:46 -05007886 /*
7887 * Check for other Port errors during the initialization
7888 * process. Fail the load if the port did not come up
7889 * correctly.
7890 */
7891 if_type = bf_get(lpfc_sli_intf_if_type,
7892 &phba->sli4_hba.sli_intf);
7893 switch (if_type) {
7894 case LPFC_SLI_INTF_IF_TYPE_0:
7895 phba->sli4_hba.ue_mask_lo =
7896 readl(phba->sli4_hba.u.if_type0.UEMASKLOregaddr);
7897 phba->sli4_hba.ue_mask_hi =
7898 readl(phba->sli4_hba.u.if_type0.UEMASKHIregaddr);
7899 uerrlo_reg.word0 =
7900 readl(phba->sli4_hba.u.if_type0.UERRLOregaddr);
7901 uerrhi_reg.word0 =
7902 readl(phba->sli4_hba.u.if_type0.UERRHIregaddr);
7903 if ((~phba->sli4_hba.ue_mask_lo & uerrlo_reg.word0) ||
7904 (~phba->sli4_hba.ue_mask_hi & uerrhi_reg.word0)) {
7905 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7906 "1422 Unrecoverable Error "
7907 "Detected during POST "
7908 "uerr_lo_reg=0x%x, "
7909 "uerr_hi_reg=0x%x, "
7910 "ue_mask_lo_reg=0x%x, "
7911 "ue_mask_hi_reg=0x%x\n",
7912 uerrlo_reg.word0,
7913 uerrhi_reg.word0,
7914 phba->sli4_hba.ue_mask_lo,
7915 phba->sli4_hba.ue_mask_hi);
7916 port_error = -ENODEV;
7917 }
7918 break;
7919 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart27d6ac02018-02-22 08:18:42 -08007920 case LPFC_SLI_INTF_IF_TYPE_6:
James Smart2fcee4b2010-12-15 17:57:46 -05007921 /* Final checks. The port status should be clean. */
James Smart9940b972011-03-11 16:06:12 -05007922 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
7923 &reg_data.word0) ||
James Smart05580562011-05-24 11:40:48 -04007924 (bf_get(lpfc_sliport_status_err, &reg_data) &&
7925 !bf_get(lpfc_sliport_status_rn, &reg_data))) {
James Smart2fcee4b2010-12-15 17:57:46 -05007926 phba->work_status[0] =
7927 readl(phba->sli4_hba.u.if_type2.
7928 ERR1regaddr);
7929 phba->work_status[1] =
7930 readl(phba->sli4_hba.u.if_type2.
7931 ERR2regaddr);
7932 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart8fcb8ac2012-03-01 22:35:58 -05007933 "2888 Unrecoverable port error "
7934 "following POST: port status reg "
7935 "0x%x, port_smphr reg 0x%x, "
James Smart2fcee4b2010-12-15 17:57:46 -05007936 "error 1=0x%x, error 2=0x%x\n",
7937 reg_data.word0,
7938 portsmphr_reg.word0,
7939 phba->work_status[0],
7940 phba->work_status[1]);
7941 port_error = -ENODEV;
7942 }
7943 break;
7944 case LPFC_SLI_INTF_IF_TYPE_1:
7945 default:
7946 break;
7947 }
James Smart28baac72010-02-12 14:42:03 -05007948 }
James Smartda0436e2009-05-22 14:51:39 -04007949 return port_error;
7950}
7951
7952/**
7953 * lpfc_sli4_bar0_register_memmap - Set up SLI4 BAR0 register memory map.
7954 * @phba: pointer to lpfc hba data structure.
James Smart2fcee4b2010-12-15 17:57:46 -05007955 * @if_type: The SLI4 interface type getting configured.
James Smartda0436e2009-05-22 14:51:39 -04007956 *
7957 * This routine is invoked to set up SLI4 BAR0 PCI config space register
7958 * memory map.
7959 **/
7960static void
James Smart2fcee4b2010-12-15 17:57:46 -05007961lpfc_sli4_bar0_register_memmap(struct lpfc_hba *phba, uint32_t if_type)
James Smartda0436e2009-05-22 14:51:39 -04007962{
James Smart2fcee4b2010-12-15 17:57:46 -05007963 switch (if_type) {
7964 case LPFC_SLI_INTF_IF_TYPE_0:
7965 phba->sli4_hba.u.if_type0.UERRLOregaddr =
7966 phba->sli4_hba.conf_regs_memmap_p + LPFC_UERR_STATUS_LO;
7967 phba->sli4_hba.u.if_type0.UERRHIregaddr =
7968 phba->sli4_hba.conf_regs_memmap_p + LPFC_UERR_STATUS_HI;
7969 phba->sli4_hba.u.if_type0.UEMASKLOregaddr =
7970 phba->sli4_hba.conf_regs_memmap_p + LPFC_UE_MASK_LO;
7971 phba->sli4_hba.u.if_type0.UEMASKHIregaddr =
7972 phba->sli4_hba.conf_regs_memmap_p + LPFC_UE_MASK_HI;
7973 phba->sli4_hba.SLIINTFregaddr =
7974 phba->sli4_hba.conf_regs_memmap_p + LPFC_SLI_INTF;
7975 break;
7976 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart0cf07f842017-06-01 21:07:10 -07007977 phba->sli4_hba.u.if_type2.EQDregaddr =
7978 phba->sli4_hba.conf_regs_memmap_p +
7979 LPFC_CTL_PORT_EQ_DELAY_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05007980 phba->sli4_hba.u.if_type2.ERR1regaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04007981 phba->sli4_hba.conf_regs_memmap_p +
7982 LPFC_CTL_PORT_ER1_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05007983 phba->sli4_hba.u.if_type2.ERR2regaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04007984 phba->sli4_hba.conf_regs_memmap_p +
7985 LPFC_CTL_PORT_ER2_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05007986 phba->sli4_hba.u.if_type2.CTRLregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04007987 phba->sli4_hba.conf_regs_memmap_p +
7988 LPFC_CTL_PORT_CTL_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05007989 phba->sli4_hba.u.if_type2.STATUSregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04007990 phba->sli4_hba.conf_regs_memmap_p +
7991 LPFC_CTL_PORT_STA_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05007992 phba->sli4_hba.SLIINTFregaddr =
7993 phba->sli4_hba.conf_regs_memmap_p + LPFC_SLI_INTF;
7994 phba->sli4_hba.PSMPHRregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04007995 phba->sli4_hba.conf_regs_memmap_p +
7996 LPFC_CTL_PORT_SEM_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05007997 phba->sli4_hba.RQDBregaddr =
James Smart962bc512013-01-03 15:44:00 -05007998 phba->sli4_hba.conf_regs_memmap_p +
7999 LPFC_ULP0_RQ_DOORBELL;
James Smart2fcee4b2010-12-15 17:57:46 -05008000 phba->sli4_hba.WQDBregaddr =
James Smart962bc512013-01-03 15:44:00 -05008001 phba->sli4_hba.conf_regs_memmap_p +
8002 LPFC_ULP0_WQ_DOORBELL;
James Smart9dd35422018-02-22 08:18:41 -08008003 phba->sli4_hba.CQDBregaddr =
James Smart2fcee4b2010-12-15 17:57:46 -05008004 phba->sli4_hba.conf_regs_memmap_p + LPFC_EQCQ_DOORBELL;
James Smart9dd35422018-02-22 08:18:41 -08008005 phba->sli4_hba.EQDBregaddr = phba->sli4_hba.CQDBregaddr;
James Smart2fcee4b2010-12-15 17:57:46 -05008006 phba->sli4_hba.MQDBregaddr =
8007 phba->sli4_hba.conf_regs_memmap_p + LPFC_MQ_DOORBELL;
8008 phba->sli4_hba.BMBXregaddr =
8009 phba->sli4_hba.conf_regs_memmap_p + LPFC_BMBX;
8010 break;
James Smart27d6ac02018-02-22 08:18:42 -08008011 case LPFC_SLI_INTF_IF_TYPE_6:
8012 phba->sli4_hba.u.if_type2.EQDregaddr =
8013 phba->sli4_hba.conf_regs_memmap_p +
8014 LPFC_CTL_PORT_EQ_DELAY_OFFSET;
8015 phba->sli4_hba.u.if_type2.ERR1regaddr =
8016 phba->sli4_hba.conf_regs_memmap_p +
8017 LPFC_CTL_PORT_ER1_OFFSET;
8018 phba->sli4_hba.u.if_type2.ERR2regaddr =
8019 phba->sli4_hba.conf_regs_memmap_p +
8020 LPFC_CTL_PORT_ER2_OFFSET;
8021 phba->sli4_hba.u.if_type2.CTRLregaddr =
8022 phba->sli4_hba.conf_regs_memmap_p +
8023 LPFC_CTL_PORT_CTL_OFFSET;
8024 phba->sli4_hba.u.if_type2.STATUSregaddr =
8025 phba->sli4_hba.conf_regs_memmap_p +
8026 LPFC_CTL_PORT_STA_OFFSET;
8027 phba->sli4_hba.PSMPHRregaddr =
8028 phba->sli4_hba.conf_regs_memmap_p +
8029 LPFC_CTL_PORT_SEM_OFFSET;
8030 phba->sli4_hba.BMBXregaddr =
8031 phba->sli4_hba.conf_regs_memmap_p + LPFC_BMBX;
8032 break;
James Smart2fcee4b2010-12-15 17:57:46 -05008033 case LPFC_SLI_INTF_IF_TYPE_1:
8034 default:
8035 dev_printk(KERN_ERR, &phba->pcidev->dev,
8036 "FATAL - unsupported SLI4 interface type - %d\n",
8037 if_type);
8038 break;
8039 }
James Smartda0436e2009-05-22 14:51:39 -04008040}
8041
8042/**
8043 * lpfc_sli4_bar1_register_memmap - Set up SLI4 BAR1 register memory map.
8044 * @phba: pointer to lpfc hba data structure.
8045 *
James Smart27d6ac02018-02-22 08:18:42 -08008046 * This routine is invoked to set up SLI4 BAR1 register memory map.
James Smartda0436e2009-05-22 14:51:39 -04008047 **/
8048static void
James Smart27d6ac02018-02-22 08:18:42 -08008049lpfc_sli4_bar1_register_memmap(struct lpfc_hba *phba, uint32_t if_type)
James Smartda0436e2009-05-22 14:51:39 -04008050{
James Smart27d6ac02018-02-22 08:18:42 -08008051 switch (if_type) {
8052 case LPFC_SLI_INTF_IF_TYPE_0:
8053 phba->sli4_hba.PSMPHRregaddr =
8054 phba->sli4_hba.ctrl_regs_memmap_p +
8055 LPFC_SLIPORT_IF0_SMPHR;
8056 phba->sli4_hba.ISRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
8057 LPFC_HST_ISR0;
8058 phba->sli4_hba.IMRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
8059 LPFC_HST_IMR0;
8060 phba->sli4_hba.ISCRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
8061 LPFC_HST_ISCR0;
8062 break;
8063 case LPFC_SLI_INTF_IF_TYPE_6:
8064 phba->sli4_hba.RQDBregaddr = phba->sli4_hba.drbl_regs_memmap_p +
8065 LPFC_IF6_RQ_DOORBELL;
8066 phba->sli4_hba.WQDBregaddr = phba->sli4_hba.drbl_regs_memmap_p +
8067 LPFC_IF6_WQ_DOORBELL;
8068 phba->sli4_hba.CQDBregaddr = phba->sli4_hba.drbl_regs_memmap_p +
8069 LPFC_IF6_CQ_DOORBELL;
8070 phba->sli4_hba.EQDBregaddr = phba->sli4_hba.drbl_regs_memmap_p +
8071 LPFC_IF6_EQ_DOORBELL;
8072 phba->sli4_hba.MQDBregaddr = phba->sli4_hba.drbl_regs_memmap_p +
8073 LPFC_IF6_MQ_DOORBELL;
8074 break;
8075 case LPFC_SLI_INTF_IF_TYPE_2:
8076 case LPFC_SLI_INTF_IF_TYPE_1:
8077 default:
8078 dev_err(&phba->pcidev->dev,
8079 "FATAL - unsupported SLI4 interface type - %d\n",
8080 if_type);
8081 break;
8082 }
James Smartda0436e2009-05-22 14:51:39 -04008083}
8084
8085/**
8086 * lpfc_sli4_bar2_register_memmap - Set up SLI4 BAR2 register memory map.
8087 * @phba: pointer to lpfc hba data structure.
8088 * @vf: virtual function number
8089 *
8090 * This routine is invoked to set up SLI4 BAR2 doorbell register memory map
8091 * based on the given viftual function number, @vf.
8092 *
8093 * Return 0 if successful, otherwise -ENODEV.
8094 **/
8095static int
8096lpfc_sli4_bar2_register_memmap(struct lpfc_hba *phba, uint32_t vf)
8097{
8098 if (vf > LPFC_VIR_FUNC_MAX)
8099 return -ENODEV;
8100
8101 phba->sli4_hba.RQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
James Smart962bc512013-01-03 15:44:00 -05008102 vf * LPFC_VFR_PAGE_SIZE +
8103 LPFC_ULP0_RQ_DOORBELL);
James Smartda0436e2009-05-22 14:51:39 -04008104 phba->sli4_hba.WQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
James Smart962bc512013-01-03 15:44:00 -05008105 vf * LPFC_VFR_PAGE_SIZE +
8106 LPFC_ULP0_WQ_DOORBELL);
James Smart9dd35422018-02-22 08:18:41 -08008107 phba->sli4_hba.CQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
8108 vf * LPFC_VFR_PAGE_SIZE +
8109 LPFC_EQCQ_DOORBELL);
8110 phba->sli4_hba.EQDBregaddr = phba->sli4_hba.CQDBregaddr;
James Smartda0436e2009-05-22 14:51:39 -04008111 phba->sli4_hba.MQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
8112 vf * LPFC_VFR_PAGE_SIZE + LPFC_MQ_DOORBELL);
8113 phba->sli4_hba.BMBXregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
8114 vf * LPFC_VFR_PAGE_SIZE + LPFC_BMBX);
8115 return 0;
8116}
8117
8118/**
8119 * lpfc_create_bootstrap_mbox - Create the bootstrap mailbox
8120 * @phba: pointer to lpfc hba data structure.
8121 *
8122 * This routine is invoked to create the bootstrap mailbox
8123 * region consistent with the SLI-4 interface spec. This
8124 * routine allocates all memory necessary to communicate
8125 * mailbox commands to the port and sets up all alignment
8126 * needs. No locks are expected to be held when calling
8127 * this routine.
8128 *
8129 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008130 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04008131 * -ENOMEM - could not allocated memory.
James Smartda0436e2009-05-22 14:51:39 -04008132 **/
8133static int
8134lpfc_create_bootstrap_mbox(struct lpfc_hba *phba)
8135{
8136 uint32_t bmbx_size;
8137 struct lpfc_dmabuf *dmabuf;
8138 struct dma_address *dma_address;
8139 uint32_t pa_addr;
8140 uint64_t phys_addr;
8141
8142 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
8143 if (!dmabuf)
8144 return -ENOMEM;
8145
8146 /*
8147 * The bootstrap mailbox region is comprised of 2 parts
8148 * plus an alignment restriction of 16 bytes.
8149 */
8150 bmbx_size = sizeof(struct lpfc_bmbx_create) + (LPFC_ALIGN_16_BYTE - 1);
Joe Perches1aee3832014-09-03 12:56:12 -04008151 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev, bmbx_size,
8152 &dmabuf->phys, GFP_KERNEL);
James Smartda0436e2009-05-22 14:51:39 -04008153 if (!dmabuf->virt) {
8154 kfree(dmabuf);
8155 return -ENOMEM;
8156 }
James Smartda0436e2009-05-22 14:51:39 -04008157
8158 /*
8159 * Initialize the bootstrap mailbox pointers now so that the register
8160 * operations are simple later. The mailbox dma address is required
8161 * to be 16-byte aligned. Also align the virtual memory as each
8162 * maibox is copied into the bmbx mailbox region before issuing the
8163 * command to the port.
8164 */
8165 phba->sli4_hba.bmbx.dmabuf = dmabuf;
8166 phba->sli4_hba.bmbx.bmbx_size = bmbx_size;
8167
8168 phba->sli4_hba.bmbx.avirt = PTR_ALIGN(dmabuf->virt,
8169 LPFC_ALIGN_16_BYTE);
8170 phba->sli4_hba.bmbx.aphys = ALIGN(dmabuf->phys,
8171 LPFC_ALIGN_16_BYTE);
8172
8173 /*
8174 * Set the high and low physical addresses now. The SLI4 alignment
8175 * requirement is 16 bytes and the mailbox is posted to the port
8176 * as two 30-bit addresses. The other data is a bit marking whether
8177 * the 30-bit address is the high or low address.
8178 * Upcast bmbx aphys to 64bits so shift instruction compiles
8179 * clean on 32 bit machines.
8180 */
8181 dma_address = &phba->sli4_hba.bmbx.dma_address;
8182 phys_addr = (uint64_t)phba->sli4_hba.bmbx.aphys;
8183 pa_addr = (uint32_t) ((phys_addr >> 34) & 0x3fffffff);
8184 dma_address->addr_hi = (uint32_t) ((pa_addr << 2) |
8185 LPFC_BMBX_BIT1_ADDR_HI);
8186
8187 pa_addr = (uint32_t) ((phba->sli4_hba.bmbx.aphys >> 4) & 0x3fffffff);
8188 dma_address->addr_lo = (uint32_t) ((pa_addr << 2) |
8189 LPFC_BMBX_BIT1_ADDR_LO);
8190 return 0;
8191}
8192
8193/**
8194 * lpfc_destroy_bootstrap_mbox - Destroy all bootstrap mailbox resources
8195 * @phba: pointer to lpfc hba data structure.
8196 *
8197 * This routine is invoked to teardown the bootstrap mailbox
8198 * region and release all host resources. This routine requires
8199 * the caller to ensure all mailbox commands recovered, no
8200 * additional mailbox comands are sent, and interrupts are disabled
8201 * before calling this routine.
8202 *
8203 **/
8204static void
8205lpfc_destroy_bootstrap_mbox(struct lpfc_hba *phba)
8206{
8207 dma_free_coherent(&phba->pcidev->dev,
8208 phba->sli4_hba.bmbx.bmbx_size,
8209 phba->sli4_hba.bmbx.dmabuf->virt,
8210 phba->sli4_hba.bmbx.dmabuf->phys);
8211
8212 kfree(phba->sli4_hba.bmbx.dmabuf);
8213 memset(&phba->sli4_hba.bmbx, 0, sizeof(struct lpfc_bmbx));
8214}
8215
8216/**
8217 * lpfc_sli4_read_config - Get the config parameters.
8218 * @phba: pointer to lpfc hba data structure.
8219 *
8220 * This routine is invoked to read the configuration parameters from the HBA.
8221 * The configuration parameters are used to set the base and maximum values
8222 * for RPI's XRI's VPI's VFI's and FCFIs. These values also affect the resource
8223 * allocation for the port.
8224 *
8225 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008226 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008227 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04008228 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04008229 **/
James Smartff78d8f2011-12-13 13:21:35 -05008230int
James Smartda0436e2009-05-22 14:51:39 -04008231lpfc_sli4_read_config(struct lpfc_hba *phba)
8232{
8233 LPFC_MBOXQ_t *pmb;
8234 struct lpfc_mbx_read_config *rd_config;
James Smart912e3ac2011-05-24 11:42:11 -04008235 union lpfc_sli4_cfg_shdr *shdr;
8236 uint32_t shdr_status, shdr_add_status;
8237 struct lpfc_mbx_get_func_cfg *get_func_cfg;
8238 struct lpfc_rsrc_desc_fcfcoe *desc;
James Smart8aa134a2012-08-14 14:25:29 -04008239 char *pdesc_0;
James Smartc6918162016-10-13 15:06:16 -07008240 uint16_t forced_link_speed;
James Smart6a828b02019-01-28 11:14:31 -08008241 uint32_t if_type, qmin;
James Smart8aa134a2012-08-14 14:25:29 -04008242 int length, i, rc = 0, rc2;
James Smartda0436e2009-05-22 14:51:39 -04008243
8244 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
8245 if (!pmb) {
8246 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8247 "2011 Unable to allocate memory for issuing "
8248 "SLI_CONFIG_SPECIAL mailbox command\n");
8249 return -ENOMEM;
8250 }
8251
8252 lpfc_read_config(phba, pmb);
8253
8254 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
8255 if (rc != MBX_SUCCESS) {
8256 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8257 "2012 Mailbox failed , mbxCmd x%x "
8258 "READ_CONFIG, mbxStatus x%x\n",
8259 bf_get(lpfc_mqe_command, &pmb->u.mqe),
8260 bf_get(lpfc_mqe_status, &pmb->u.mqe));
8261 rc = -EIO;
8262 } else {
8263 rd_config = &pmb->u.mqe.un.rd_config;
James Smartff78d8f2011-12-13 13:21:35 -05008264 if (bf_get(lpfc_mbx_rd_conf_lnk_ldv, rd_config)) {
8265 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
8266 phba->sli4_hba.lnk_info.lnk_tp =
8267 bf_get(lpfc_mbx_rd_conf_lnk_type, rd_config);
8268 phba->sli4_hba.lnk_info.lnk_no =
8269 bf_get(lpfc_mbx_rd_conf_lnk_numb, rd_config);
8270 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8271 "3081 lnk_type:%d, lnk_numb:%d\n",
8272 phba->sli4_hba.lnk_info.lnk_tp,
8273 phba->sli4_hba.lnk_info.lnk_no);
8274 } else
8275 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8276 "3082 Mailbox (x%x) returned ldv:x0\n",
8277 bf_get(lpfc_mqe_command, &pmb->u.mqe));
James Smart44fd7fe2017-08-23 16:55:47 -07008278 if (bf_get(lpfc_mbx_rd_conf_bbscn_def, rd_config)) {
8279 phba->bbcredit_support = 1;
8280 phba->sli4_hba.bbscn_params.word0 = rd_config->word8;
8281 }
8282
James Smart1dc5ec22018-10-23 13:41:11 -07008283 phba->sli4_hba.conf_trunk =
8284 bf_get(lpfc_mbx_rd_conf_trunk, rd_config);
James Smart6d368e52011-05-24 11:44:12 -04008285 phba->sli4_hba.extents_in_use =
8286 bf_get(lpfc_mbx_rd_conf_extnts_inuse, rd_config);
James Smartda0436e2009-05-22 14:51:39 -04008287 phba->sli4_hba.max_cfg_param.max_xri =
8288 bf_get(lpfc_mbx_rd_conf_xri_count, rd_config);
8289 phba->sli4_hba.max_cfg_param.xri_base =
8290 bf_get(lpfc_mbx_rd_conf_xri_base, rd_config);
8291 phba->sli4_hba.max_cfg_param.max_vpi =
8292 bf_get(lpfc_mbx_rd_conf_vpi_count, rd_config);
James Smart8b47ae62018-11-29 16:09:33 -08008293 /* Limit the max we support */
8294 if (phba->sli4_hba.max_cfg_param.max_vpi > LPFC_MAX_VPORTS)
8295 phba->sli4_hba.max_cfg_param.max_vpi = LPFC_MAX_VPORTS;
James Smartda0436e2009-05-22 14:51:39 -04008296 phba->sli4_hba.max_cfg_param.vpi_base =
8297 bf_get(lpfc_mbx_rd_conf_vpi_base, rd_config);
8298 phba->sli4_hba.max_cfg_param.max_rpi =
8299 bf_get(lpfc_mbx_rd_conf_rpi_count, rd_config);
8300 phba->sli4_hba.max_cfg_param.rpi_base =
8301 bf_get(lpfc_mbx_rd_conf_rpi_base, rd_config);
8302 phba->sli4_hba.max_cfg_param.max_vfi =
8303 bf_get(lpfc_mbx_rd_conf_vfi_count, rd_config);
8304 phba->sli4_hba.max_cfg_param.vfi_base =
8305 bf_get(lpfc_mbx_rd_conf_vfi_base, rd_config);
8306 phba->sli4_hba.max_cfg_param.max_fcfi =
8307 bf_get(lpfc_mbx_rd_conf_fcfi_count, rd_config);
James Smartda0436e2009-05-22 14:51:39 -04008308 phba->sli4_hba.max_cfg_param.max_eq =
8309 bf_get(lpfc_mbx_rd_conf_eq_count, rd_config);
8310 phba->sli4_hba.max_cfg_param.max_rq =
8311 bf_get(lpfc_mbx_rd_conf_rq_count, rd_config);
8312 phba->sli4_hba.max_cfg_param.max_wq =
8313 bf_get(lpfc_mbx_rd_conf_wq_count, rd_config);
8314 phba->sli4_hba.max_cfg_param.max_cq =
8315 bf_get(lpfc_mbx_rd_conf_cq_count, rd_config);
8316 phba->lmt = bf_get(lpfc_mbx_rd_conf_lmt, rd_config);
8317 phba->sli4_hba.next_xri = phba->sli4_hba.max_cfg_param.xri_base;
8318 phba->vpi_base = phba->sli4_hba.max_cfg_param.vpi_base;
8319 phba->vfi_base = phba->sli4_hba.max_cfg_param.vfi_base;
James Smart5ffc2662009-11-18 15:39:44 -05008320 phba->max_vpi = (phba->sli4_hba.max_cfg_param.max_vpi > 0) ?
8321 (phba->sli4_hba.max_cfg_param.max_vpi - 1) : 0;
James Smartda0436e2009-05-22 14:51:39 -04008322 phba->max_vports = phba->max_vpi;
8323 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart6d368e52011-05-24 11:44:12 -04008324 "2003 cfg params Extents? %d "
8325 "XRI(B:%d M:%d), "
James Smartda0436e2009-05-22 14:51:39 -04008326 "VPI(B:%d M:%d) "
8327 "VFI(B:%d M:%d) "
8328 "RPI(B:%d M:%d) "
James Smart2ea259e2017-02-12 13:52:27 -08008329 "FCFI:%d EQ:%d CQ:%d WQ:%d RQ:%d\n",
James Smart6d368e52011-05-24 11:44:12 -04008330 phba->sli4_hba.extents_in_use,
James Smartda0436e2009-05-22 14:51:39 -04008331 phba->sli4_hba.max_cfg_param.xri_base,
8332 phba->sli4_hba.max_cfg_param.max_xri,
8333 phba->sli4_hba.max_cfg_param.vpi_base,
8334 phba->sli4_hba.max_cfg_param.max_vpi,
8335 phba->sli4_hba.max_cfg_param.vfi_base,
8336 phba->sli4_hba.max_cfg_param.max_vfi,
8337 phba->sli4_hba.max_cfg_param.rpi_base,
8338 phba->sli4_hba.max_cfg_param.max_rpi,
James Smart2ea259e2017-02-12 13:52:27 -08008339 phba->sli4_hba.max_cfg_param.max_fcfi,
8340 phba->sli4_hba.max_cfg_param.max_eq,
8341 phba->sli4_hba.max_cfg_param.max_cq,
8342 phba->sli4_hba.max_cfg_param.max_wq,
8343 phba->sli4_hba.max_cfg_param.max_rq);
8344
James Smartd38f33b2018-05-04 20:37:54 -07008345 /*
James Smart6a828b02019-01-28 11:14:31 -08008346 * Calculate queue resources based on how
8347 * many WQ/CQ/EQs are available.
James Smartd38f33b2018-05-04 20:37:54 -07008348 */
James Smart6a828b02019-01-28 11:14:31 -08008349 qmin = phba->sli4_hba.max_cfg_param.max_wq;
8350 if (phba->sli4_hba.max_cfg_param.max_cq < qmin)
8351 qmin = phba->sli4_hba.max_cfg_param.max_cq;
8352 if (phba->sli4_hba.max_cfg_param.max_eq < qmin)
8353 qmin = phba->sli4_hba.max_cfg_param.max_eq;
8354 /*
8355 * Whats left after this can go toward NVME / FCP.
8356 * The minus 4 accounts for ELS, NVME LS, MBOX
8357 * plus one extra. When configured for
8358 * NVMET, FCP io channel WQs are not created.
8359 */
8360 qmin -= 4;
James Smartd38f33b2018-05-04 20:37:54 -07008361
James Smart6a828b02019-01-28 11:14:31 -08008362 /* If NVME is configured, double the number of CQ/WQs needed */
8363 if ((phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) &&
8364 !phba->nvmet_support)
8365 qmin /= 2;
James Smartd38f33b2018-05-04 20:37:54 -07008366
James Smart6a828b02019-01-28 11:14:31 -08008367 /* Check to see if there is enough for NVME */
8368 if ((phba->cfg_irq_chann > qmin) ||
8369 (phba->cfg_hdw_queue > qmin)) {
8370 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8371 "2005 Reducing Queues: "
8372 "WQ %d CQ %d EQ %d: min %d: "
8373 "IRQ %d HDWQ %d\n",
James Smartd38f33b2018-05-04 20:37:54 -07008374 phba->sli4_hba.max_cfg_param.max_wq,
8375 phba->sli4_hba.max_cfg_param.max_cq,
James Smart6a828b02019-01-28 11:14:31 -08008376 phba->sli4_hba.max_cfg_param.max_eq,
8377 qmin, phba->cfg_irq_chann,
James Smartcdb42be2019-01-28 11:14:21 -08008378 phba->cfg_hdw_queue);
James Smartd38f33b2018-05-04 20:37:54 -07008379
James Smart6a828b02019-01-28 11:14:31 -08008380 if (phba->cfg_irq_chann > qmin)
8381 phba->cfg_irq_chann = qmin;
8382 if (phba->cfg_hdw_queue > qmin)
8383 phba->cfg_hdw_queue = qmin;
James Smartd38f33b2018-05-04 20:37:54 -07008384 }
James Smartda0436e2009-05-22 14:51:39 -04008385 }
James Smart912e3ac2011-05-24 11:42:11 -04008386
8387 if (rc)
8388 goto read_cfg_out;
James Smartda0436e2009-05-22 14:51:39 -04008389
James Smartc6918162016-10-13 15:06:16 -07008390 /* Update link speed if forced link speed is supported */
8391 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
James Smart27d6ac02018-02-22 08:18:42 -08008392 if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
James Smartc6918162016-10-13 15:06:16 -07008393 forced_link_speed =
8394 bf_get(lpfc_mbx_rd_conf_link_speed, rd_config);
8395 if (forced_link_speed) {
8396 phba->hba_flag |= HBA_FORCED_LINK_SPEED;
8397
8398 switch (forced_link_speed) {
8399 case LINK_SPEED_1G:
8400 phba->cfg_link_speed =
8401 LPFC_USER_LINK_SPEED_1G;
8402 break;
8403 case LINK_SPEED_2G:
8404 phba->cfg_link_speed =
8405 LPFC_USER_LINK_SPEED_2G;
8406 break;
8407 case LINK_SPEED_4G:
8408 phba->cfg_link_speed =
8409 LPFC_USER_LINK_SPEED_4G;
8410 break;
8411 case LINK_SPEED_8G:
8412 phba->cfg_link_speed =
8413 LPFC_USER_LINK_SPEED_8G;
8414 break;
8415 case LINK_SPEED_10G:
8416 phba->cfg_link_speed =
8417 LPFC_USER_LINK_SPEED_10G;
8418 break;
8419 case LINK_SPEED_16G:
8420 phba->cfg_link_speed =
8421 LPFC_USER_LINK_SPEED_16G;
8422 break;
8423 case LINK_SPEED_32G:
8424 phba->cfg_link_speed =
8425 LPFC_USER_LINK_SPEED_32G;
8426 break;
James Smartfbd8a6b2018-02-22 08:18:45 -08008427 case LINK_SPEED_64G:
8428 phba->cfg_link_speed =
8429 LPFC_USER_LINK_SPEED_64G;
8430 break;
James Smartc6918162016-10-13 15:06:16 -07008431 case 0xffff:
8432 phba->cfg_link_speed =
8433 LPFC_USER_LINK_SPEED_AUTO;
8434 break;
8435 default:
8436 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8437 "0047 Unrecognized link "
8438 "speed : %d\n",
8439 forced_link_speed);
8440 phba->cfg_link_speed =
8441 LPFC_USER_LINK_SPEED_AUTO;
8442 }
8443 }
8444 }
8445
James Smartda0436e2009-05-22 14:51:39 -04008446 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
James Smart572709e2013-07-15 18:32:43 -04008447 length = phba->sli4_hba.max_cfg_param.max_xri -
8448 lpfc_sli4_get_els_iocb_cnt(phba);
8449 if (phba->cfg_hba_queue_depth > length) {
8450 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
8451 "3361 HBA queue depth changed from %d to %d\n",
8452 phba->cfg_hba_queue_depth, length);
8453 phba->cfg_hba_queue_depth = length;
8454 }
James Smart912e3ac2011-05-24 11:42:11 -04008455
James Smart27d6ac02018-02-22 08:18:42 -08008456 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) <
James Smart912e3ac2011-05-24 11:42:11 -04008457 LPFC_SLI_INTF_IF_TYPE_2)
8458 goto read_cfg_out;
8459
8460 /* get the pf# and vf# for SLI4 if_type 2 port */
8461 length = (sizeof(struct lpfc_mbx_get_func_cfg) -
8462 sizeof(struct lpfc_sli4_cfg_mhdr));
8463 lpfc_sli4_config(phba, pmb, LPFC_MBOX_SUBSYSTEM_COMMON,
8464 LPFC_MBOX_OPCODE_GET_FUNCTION_CONFIG,
8465 length, LPFC_SLI4_MBX_EMBED);
8466
James Smart8aa134a2012-08-14 14:25:29 -04008467 rc2 = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart912e3ac2011-05-24 11:42:11 -04008468 shdr = (union lpfc_sli4_cfg_shdr *)
8469 &pmb->u.mqe.un.sli4_config.header.cfg_shdr;
8470 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
8471 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
James Smart8aa134a2012-08-14 14:25:29 -04008472 if (rc2 || shdr_status || shdr_add_status) {
James Smart912e3ac2011-05-24 11:42:11 -04008473 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8474 "3026 Mailbox failed , mbxCmd x%x "
8475 "GET_FUNCTION_CONFIG, mbxStatus x%x\n",
8476 bf_get(lpfc_mqe_command, &pmb->u.mqe),
8477 bf_get(lpfc_mqe_status, &pmb->u.mqe));
James Smart912e3ac2011-05-24 11:42:11 -04008478 goto read_cfg_out;
8479 }
8480
8481 /* search for fc_fcoe resrouce descriptor */
8482 get_func_cfg = &pmb->u.mqe.un.get_func_cfg;
James Smart912e3ac2011-05-24 11:42:11 -04008483
James Smart8aa134a2012-08-14 14:25:29 -04008484 pdesc_0 = (char *)&get_func_cfg->func_cfg.desc[0];
8485 desc = (struct lpfc_rsrc_desc_fcfcoe *)pdesc_0;
8486 length = bf_get(lpfc_rsrc_desc_fcfcoe_length, desc);
8487 if (length == LPFC_RSRC_DESC_TYPE_FCFCOE_V0_RSVD)
8488 length = LPFC_RSRC_DESC_TYPE_FCFCOE_V0_LENGTH;
8489 else if (length != LPFC_RSRC_DESC_TYPE_FCFCOE_V1_LENGTH)
8490 goto read_cfg_out;
8491
James Smart912e3ac2011-05-24 11:42:11 -04008492 for (i = 0; i < LPFC_RSRC_DESC_MAX_NUM; i++) {
James Smart8aa134a2012-08-14 14:25:29 -04008493 desc = (struct lpfc_rsrc_desc_fcfcoe *)(pdesc_0 + length * i);
James Smart912e3ac2011-05-24 11:42:11 -04008494 if (LPFC_RSRC_DESC_TYPE_FCFCOE ==
James Smart8aa134a2012-08-14 14:25:29 -04008495 bf_get(lpfc_rsrc_desc_fcfcoe_type, desc)) {
James Smart912e3ac2011-05-24 11:42:11 -04008496 phba->sli4_hba.iov.pf_number =
8497 bf_get(lpfc_rsrc_desc_fcfcoe_pfnum, desc);
8498 phba->sli4_hba.iov.vf_number =
8499 bf_get(lpfc_rsrc_desc_fcfcoe_vfnum, desc);
8500 break;
8501 }
8502 }
8503
8504 if (i < LPFC_RSRC_DESC_MAX_NUM)
8505 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8506 "3027 GET_FUNCTION_CONFIG: pf_number:%d, "
8507 "vf_number:%d\n", phba->sli4_hba.iov.pf_number,
8508 phba->sli4_hba.iov.vf_number);
James Smart8aa134a2012-08-14 14:25:29 -04008509 else
James Smart912e3ac2011-05-24 11:42:11 -04008510 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8511 "3028 GET_FUNCTION_CONFIG: failed to find "
Colin Ian Kingc4dba182018-10-16 18:28:53 +01008512 "Resource Descriptor:x%x\n",
James Smart912e3ac2011-05-24 11:42:11 -04008513 LPFC_RSRC_DESC_TYPE_FCFCOE);
James Smart912e3ac2011-05-24 11:42:11 -04008514
8515read_cfg_out:
8516 mempool_free(pmb, phba->mbox_mem_pool);
James Smartda0436e2009-05-22 14:51:39 -04008517 return rc;
8518}
8519
8520/**
James Smart2fcee4b2010-12-15 17:57:46 -05008521 * lpfc_setup_endian_order - Write endian order to an SLI4 if_type 0 port.
James Smartda0436e2009-05-22 14:51:39 -04008522 * @phba: pointer to lpfc hba data structure.
8523 *
James Smart2fcee4b2010-12-15 17:57:46 -05008524 * This routine is invoked to setup the port-side endian order when
8525 * the port if_type is 0. This routine has no function for other
8526 * if_types.
James Smartda0436e2009-05-22 14:51:39 -04008527 *
8528 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008529 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008530 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04008531 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04008532 **/
8533static int
8534lpfc_setup_endian_order(struct lpfc_hba *phba)
8535{
8536 LPFC_MBOXQ_t *mboxq;
James Smart2fcee4b2010-12-15 17:57:46 -05008537 uint32_t if_type, rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04008538 uint32_t endian_mb_data[2] = {HOST_ENDIAN_LOW_WORD0,
8539 HOST_ENDIAN_HIGH_WORD1};
8540
James Smart2fcee4b2010-12-15 17:57:46 -05008541 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
8542 switch (if_type) {
8543 case LPFC_SLI_INTF_IF_TYPE_0:
8544 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
8545 GFP_KERNEL);
8546 if (!mboxq) {
8547 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8548 "0492 Unable to allocate memory for "
8549 "issuing SLI_CONFIG_SPECIAL mailbox "
8550 "command\n");
8551 return -ENOMEM;
8552 }
James Smartda0436e2009-05-22 14:51:39 -04008553
James Smart2fcee4b2010-12-15 17:57:46 -05008554 /*
8555 * The SLI4_CONFIG_SPECIAL mailbox command requires the first
8556 * two words to contain special data values and no other data.
8557 */
8558 memset(mboxq, 0, sizeof(LPFC_MBOXQ_t));
8559 memcpy(&mboxq->u.mqe, &endian_mb_data, sizeof(endian_mb_data));
8560 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8561 if (rc != MBX_SUCCESS) {
8562 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8563 "0493 SLI_CONFIG_SPECIAL mailbox "
8564 "failed with status x%x\n",
8565 rc);
8566 rc = -EIO;
8567 }
8568 mempool_free(mboxq, phba->mbox_mem_pool);
8569 break;
James Smart27d6ac02018-02-22 08:18:42 -08008570 case LPFC_SLI_INTF_IF_TYPE_6:
James Smart2fcee4b2010-12-15 17:57:46 -05008571 case LPFC_SLI_INTF_IF_TYPE_2:
8572 case LPFC_SLI_INTF_IF_TYPE_1:
8573 default:
8574 break;
James Smartda0436e2009-05-22 14:51:39 -04008575 }
James Smartda0436e2009-05-22 14:51:39 -04008576 return rc;
8577}
8578
8579/**
James Smart895427b2017-02-12 13:52:30 -08008580 * lpfc_sli4_queue_verify - Verify and update EQ counts
James Smartda0436e2009-05-22 14:51:39 -04008581 * @phba: pointer to lpfc hba data structure.
8582 *
James Smart895427b2017-02-12 13:52:30 -08008583 * This routine is invoked to check the user settable queue counts for EQs.
8584 * After this routine is called the counts will be set to valid values that
James Smart5350d872011-10-10 21:33:49 -04008585 * adhere to the constraints of the system's interrupt vectors and the port's
8586 * queue resources.
James Smartda0436e2009-05-22 14:51:39 -04008587 *
8588 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008589 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008590 * -ENOMEM - No available memory
James Smartda0436e2009-05-22 14:51:39 -04008591 **/
8592static int
James Smart5350d872011-10-10 21:33:49 -04008593lpfc_sli4_queue_verify(struct lpfc_hba *phba)
James Smartda0436e2009-05-22 14:51:39 -04008594{
James Smartda0436e2009-05-22 14:51:39 -04008595 /*
James Smart67d12732012-08-03 12:36:13 -04008596 * Sanity check for configured queue parameters against the run-time
James Smartda0436e2009-05-22 14:51:39 -04008597 * device parameters
8598 */
8599
James Smartbcb24f62017-11-20 16:00:36 -08008600 if (phba->nvmet_support) {
James Smart6a828b02019-01-28 11:14:31 -08008601 if (phba->cfg_irq_chann < phba->cfg_nvmet_mrq)
8602 phba->cfg_nvmet_mrq = phba->cfg_irq_chann;
James Smartbcb24f62017-11-20 16:00:36 -08008603 }
8604 if (phba->cfg_nvmet_mrq > LPFC_NVMET_MRQ_MAX)
8605 phba->cfg_nvmet_mrq = LPFC_NVMET_MRQ_MAX;
James Smart895427b2017-02-12 13:52:30 -08008606
8607 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart6a828b02019-01-28 11:14:31 -08008608 "2574 IO channels: hdwQ %d IRQ %d MRQ: %d\n",
8609 phba->cfg_hdw_queue, phba->cfg_irq_chann,
8610 phba->cfg_nvmet_mrq);
James Smartda0436e2009-05-22 14:51:39 -04008611
James Smartda0436e2009-05-22 14:51:39 -04008612 /* Get EQ depth from module parameter, fake the default for now */
8613 phba->sli4_hba.eq_esize = LPFC_EQE_SIZE_4B;
8614 phba->sli4_hba.eq_ecount = LPFC_EQE_DEF_COUNT;
8615
James Smart5350d872011-10-10 21:33:49 -04008616 /* Get CQ depth from module parameter, fake the default for now */
8617 phba->sli4_hba.cq_esize = LPFC_CQE_SIZE;
8618 phba->sli4_hba.cq_ecount = LPFC_CQE_DEF_COUNT;
James Smart5350d872011-10-10 21:33:49 -04008619 return 0;
James Smart895427b2017-02-12 13:52:30 -08008620}
8621
8622static int
8623lpfc_alloc_nvme_wq_cq(struct lpfc_hba *phba, int wqidx)
8624{
8625 struct lpfc_queue *qdesc;
James Smart895427b2017-02-12 13:52:30 -08008626
James Smarta51e41b2017-12-08 17:18:06 -08008627 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_EXPANDED_PAGE_SIZE,
James Smart81b96ed2017-11-20 16:00:29 -08008628 phba->sli4_hba.cq_esize,
James Smarta51e41b2017-12-08 17:18:06 -08008629 LPFC_CQE_EXP_COUNT);
James Smart895427b2017-02-12 13:52:30 -08008630 if (!qdesc) {
8631 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8632 "0508 Failed allocate fast-path NVME CQ (%d)\n",
8633 wqidx);
8634 return 1;
8635 }
James Smart7365f6f2018-02-22 08:18:46 -08008636 qdesc->qe_valid = 1;
James Smart5e5b5112019-01-28 11:14:22 -08008637 qdesc->hdwq = wqidx;
James Smart6a828b02019-01-28 11:14:31 -08008638 qdesc->chann = lpfc_find_cpu_handle(phba, wqidx, LPFC_FIND_BY_HDWQ);
James Smartcdb42be2019-01-28 11:14:21 -08008639 phba->sli4_hba.hdwq[wqidx].nvme_cq = qdesc;
James Smart895427b2017-02-12 13:52:30 -08008640
James Smarta51e41b2017-12-08 17:18:06 -08008641 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_EXPANDED_PAGE_SIZE,
8642 LPFC_WQE128_SIZE, LPFC_WQE_EXP_COUNT);
James Smart895427b2017-02-12 13:52:30 -08008643 if (!qdesc) {
8644 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8645 "0509 Failed allocate fast-path NVME WQ (%d)\n",
8646 wqidx);
8647 return 1;
8648 }
James Smart5e5b5112019-01-28 11:14:22 -08008649 qdesc->hdwq = wqidx;
James Smart6a828b02019-01-28 11:14:31 -08008650 qdesc->chann = wqidx;
James Smartcdb42be2019-01-28 11:14:21 -08008651 phba->sli4_hba.hdwq[wqidx].nvme_wq = qdesc;
James Smart895427b2017-02-12 13:52:30 -08008652 list_add_tail(&qdesc->wq_list, &phba->sli4_hba.lpfc_wq_list);
8653 return 0;
8654}
8655
8656static int
8657lpfc_alloc_fcp_wq_cq(struct lpfc_hba *phba, int wqidx)
8658{
8659 struct lpfc_queue *qdesc;
James Smartc176ffa2018-01-30 15:58:46 -08008660 uint32_t wqesize;
James Smart895427b2017-02-12 13:52:30 -08008661
8662 /* Create Fast Path FCP CQs */
James Smartc176ffa2018-01-30 15:58:46 -08008663 if (phba->enab_exp_wqcq_pages)
James Smarta51e41b2017-12-08 17:18:06 -08008664 /* Increase the CQ size when WQEs contain an embedded cdb */
8665 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_EXPANDED_PAGE_SIZE,
8666 phba->sli4_hba.cq_esize,
8667 LPFC_CQE_EXP_COUNT);
8668
8669 else
8670 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
8671 phba->sli4_hba.cq_esize,
8672 phba->sli4_hba.cq_ecount);
James Smart895427b2017-02-12 13:52:30 -08008673 if (!qdesc) {
8674 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8675 "0499 Failed allocate fast-path FCP CQ (%d)\n", wqidx);
8676 return 1;
8677 }
James Smart7365f6f2018-02-22 08:18:46 -08008678 qdesc->qe_valid = 1;
James Smart5e5b5112019-01-28 11:14:22 -08008679 qdesc->hdwq = wqidx;
James Smart6a828b02019-01-28 11:14:31 -08008680 qdesc->chann = lpfc_find_cpu_handle(phba, wqidx, LPFC_FIND_BY_HDWQ);
James Smartcdb42be2019-01-28 11:14:21 -08008681 phba->sli4_hba.hdwq[wqidx].fcp_cq = qdesc;
James Smart895427b2017-02-12 13:52:30 -08008682
8683 /* Create Fast Path FCP WQs */
James Smartc176ffa2018-01-30 15:58:46 -08008684 if (phba->enab_exp_wqcq_pages) {
James Smarta51e41b2017-12-08 17:18:06 -08008685 /* Increase the WQ size when WQEs contain an embedded cdb */
James Smartc176ffa2018-01-30 15:58:46 -08008686 wqesize = (phba->fcp_embed_io) ?
8687 LPFC_WQE128_SIZE : phba->sli4_hba.wq_esize;
James Smarta51e41b2017-12-08 17:18:06 -08008688 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_EXPANDED_PAGE_SIZE,
James Smartc176ffa2018-01-30 15:58:46 -08008689 wqesize,
James Smarta51e41b2017-12-08 17:18:06 -08008690 LPFC_WQE_EXP_COUNT);
James Smartc176ffa2018-01-30 15:58:46 -08008691 } else
James Smarta51e41b2017-12-08 17:18:06 -08008692 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
8693 phba->sli4_hba.wq_esize,
8694 phba->sli4_hba.wq_ecount);
James Smartc176ffa2018-01-30 15:58:46 -08008695
James Smart895427b2017-02-12 13:52:30 -08008696 if (!qdesc) {
8697 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8698 "0503 Failed allocate fast-path FCP WQ (%d)\n",
8699 wqidx);
8700 return 1;
8701 }
James Smart5e5b5112019-01-28 11:14:22 -08008702 qdesc->hdwq = wqidx;
James Smart6a828b02019-01-28 11:14:31 -08008703 qdesc->chann = wqidx;
James Smartcdb42be2019-01-28 11:14:21 -08008704 phba->sli4_hba.hdwq[wqidx].fcp_wq = qdesc;
James Smart895427b2017-02-12 13:52:30 -08008705 list_add_tail(&qdesc->wq_list, &phba->sli4_hba.lpfc_wq_list);
8706 return 0;
James Smart5350d872011-10-10 21:33:49 -04008707}
8708
8709/**
8710 * lpfc_sli4_queue_create - Create all the SLI4 queues
8711 * @phba: pointer to lpfc hba data structure.
8712 *
8713 * This routine is invoked to allocate all the SLI4 queues for the FCoE HBA
8714 * operation. For each SLI4 queue type, the parameters such as queue entry
8715 * count (queue depth) shall be taken from the module parameter. For now,
8716 * we just use some constant number as place holder.
8717 *
8718 * Return codes
Anatol Pomozov4907cb72012-09-01 10:31:09 -07008719 * 0 - successful
James Smart5350d872011-10-10 21:33:49 -04008720 * -ENOMEM - No availble memory
8721 * -EIO - The mailbox failed to complete successfully.
8722 **/
8723int
8724lpfc_sli4_queue_create(struct lpfc_hba *phba)
8725{
8726 struct lpfc_queue *qdesc;
James Smart6a828b02019-01-28 11:14:31 -08008727 int idx, eqidx;
James Smart5e5b5112019-01-28 11:14:22 -08008728 struct lpfc_sli4_hdw_queue *qp;
James Smart32517fc2019-01-28 11:14:33 -08008729 struct lpfc_eq_intr_info *eqi;
James Smart5350d872011-10-10 21:33:49 -04008730
8731 /*
James Smart67d12732012-08-03 12:36:13 -04008732 * Create HBA Record arrays.
James Smart895427b2017-02-12 13:52:30 -08008733 * Both NVME and FCP will share that same vectors / EQs
James Smart5350d872011-10-10 21:33:49 -04008734 */
James Smart67d12732012-08-03 12:36:13 -04008735 phba->sli4_hba.mq_esize = LPFC_MQE_SIZE;
8736 phba->sli4_hba.mq_ecount = LPFC_MQE_DEF_COUNT;
8737 phba->sli4_hba.wq_esize = LPFC_WQE_SIZE;
8738 phba->sli4_hba.wq_ecount = LPFC_WQE_DEF_COUNT;
8739 phba->sli4_hba.rq_esize = LPFC_RQE_SIZE;
8740 phba->sli4_hba.rq_ecount = LPFC_RQE_DEF_COUNT;
James Smart895427b2017-02-12 13:52:30 -08008741 phba->sli4_hba.eq_esize = LPFC_EQE_SIZE_4B;
8742 phba->sli4_hba.eq_ecount = LPFC_EQE_DEF_COUNT;
8743 phba->sli4_hba.cq_esize = LPFC_CQE_SIZE;
8744 phba->sli4_hba.cq_ecount = LPFC_CQE_DEF_COUNT;
James Smart67d12732012-08-03 12:36:13 -04008745
James Smartcdb42be2019-01-28 11:14:21 -08008746 if (!phba->sli4_hba.hdwq) {
James Smart5e5b5112019-01-28 11:14:22 -08008747 phba->sli4_hba.hdwq = kcalloc(
8748 phba->cfg_hdw_queue, sizeof(struct lpfc_sli4_hdw_queue),
8749 GFP_KERNEL);
8750 if (!phba->sli4_hba.hdwq) {
8751 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8752 "6427 Failed allocate memory for "
8753 "fast-path Hardware Queue array\n");
8754 goto out_error;
8755 }
8756 /* Prepare hardware queues to take IO buffers */
8757 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
8758 qp = &phba->sli4_hba.hdwq[idx];
8759 spin_lock_init(&qp->io_buf_list_get_lock);
8760 spin_lock_init(&qp->io_buf_list_put_lock);
8761 INIT_LIST_HEAD(&qp->lpfc_io_buf_list_get);
8762 INIT_LIST_HEAD(&qp->lpfc_io_buf_list_put);
8763 qp->get_io_bufs = 0;
8764 qp->put_io_bufs = 0;
8765 qp->total_io_bufs = 0;
8766 spin_lock_init(&qp->abts_scsi_buf_list_lock);
8767 INIT_LIST_HEAD(&qp->lpfc_abts_scsi_buf_list);
8768 qp->abts_scsi_io_bufs = 0;
8769 spin_lock_init(&qp->abts_nvme_buf_list_lock);
8770 INIT_LIST_HEAD(&qp->lpfc_abts_nvme_buf_list);
8771 qp->abts_nvme_io_bufs = 0;
8772 }
James Smartda0436e2009-05-22 14:51:39 -04008773 }
James Smart67d12732012-08-03 12:36:13 -04008774
James Smartcdb42be2019-01-28 11:14:21 -08008775 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
James Smart2d7dbc42017-02-12 13:52:35 -08008776 if (phba->nvmet_support) {
8777 phba->sli4_hba.nvmet_cqset = kcalloc(
8778 phba->cfg_nvmet_mrq,
8779 sizeof(struct lpfc_queue *),
8780 GFP_KERNEL);
8781 if (!phba->sli4_hba.nvmet_cqset) {
8782 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8783 "3121 Fail allocate memory for "
8784 "fast-path CQ set array\n");
8785 goto out_error;
8786 }
8787 phba->sli4_hba.nvmet_mrq_hdr = kcalloc(
8788 phba->cfg_nvmet_mrq,
8789 sizeof(struct lpfc_queue *),
8790 GFP_KERNEL);
8791 if (!phba->sli4_hba.nvmet_mrq_hdr) {
8792 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8793 "3122 Fail allocate memory for "
8794 "fast-path RQ set hdr array\n");
8795 goto out_error;
8796 }
8797 phba->sli4_hba.nvmet_mrq_data = kcalloc(
8798 phba->cfg_nvmet_mrq,
8799 sizeof(struct lpfc_queue *),
8800 GFP_KERNEL);
8801 if (!phba->sli4_hba.nvmet_mrq_data) {
8802 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8803 "3124 Fail allocate memory for "
8804 "fast-path RQ set data array\n");
8805 goto out_error;
8806 }
8807 }
James Smart67d12732012-08-03 12:36:13 -04008808 }
James Smartda0436e2009-05-22 14:51:39 -04008809
James Smart895427b2017-02-12 13:52:30 -08008810 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_wq_list);
James Smart67d12732012-08-03 12:36:13 -04008811
James Smart895427b2017-02-12 13:52:30 -08008812 /* Create HBA Event Queues (EQs) */
James Smartcdb42be2019-01-28 11:14:21 -08008813 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
James Smart6a828b02019-01-28 11:14:31 -08008814 /*
8815 * If there are more Hardware Queues than available
8816 * CQs, multiple Hardware Queues may share a common EQ.
8817 */
8818 if (idx >= phba->cfg_irq_chann) {
8819 /* Share an existing EQ */
8820 eqidx = lpfc_find_eq_handle(phba, idx);
8821 phba->sli4_hba.hdwq[idx].hba_eq =
8822 phba->sli4_hba.hdwq[eqidx].hba_eq;
8823 continue;
8824 }
8825 /* Create an EQ */
James Smart81b96ed2017-11-20 16:00:29 -08008826 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
8827 phba->sli4_hba.eq_esize,
James Smartda0436e2009-05-22 14:51:39 -04008828 phba->sli4_hba.eq_ecount);
8829 if (!qdesc) {
8830 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart67d12732012-08-03 12:36:13 -04008831 "0497 Failed allocate EQ (%d)\n", idx);
8832 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04008833 }
James Smart7365f6f2018-02-22 08:18:46 -08008834 qdesc->qe_valid = 1;
James Smart5e5b5112019-01-28 11:14:22 -08008835 qdesc->hdwq = idx;
James Smart6a828b02019-01-28 11:14:31 -08008836
8837 /* Save the CPU this EQ is affinitised to */
8838 eqidx = lpfc_find_eq_handle(phba, idx);
8839 qdesc->chann = lpfc_find_cpu_handle(phba, eqidx,
8840 LPFC_FIND_BY_EQ);
James Smartcdb42be2019-01-28 11:14:21 -08008841 phba->sli4_hba.hdwq[idx].hba_eq = qdesc;
James Smart32517fc2019-01-28 11:14:33 -08008842 qdesc->last_cpu = qdesc->chann;
8843 eqi = per_cpu_ptr(phba->sli4_hba.eq_info, qdesc->last_cpu);
8844 list_add(&qdesc->cpu_list, &eqi->list);
James Smartda0436e2009-05-22 14:51:39 -04008845 }
8846
James Smart895427b2017-02-12 13:52:30 -08008847
James Smartcdb42be2019-01-28 11:14:21 -08008848 /* Allocate SCSI SLI4 CQ/WQs */
James Smart6a828b02019-01-28 11:14:31 -08008849 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
James Smart895427b2017-02-12 13:52:30 -08008850 if (lpfc_alloc_fcp_wq_cq(phba, idx))
8851 goto out_error;
James Smart6a828b02019-01-28 11:14:31 -08008852 }
James Smart895427b2017-02-12 13:52:30 -08008853
James Smartcdb42be2019-01-28 11:14:21 -08008854 /* Allocate NVME SLI4 CQ/WQs */
8855 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
James Smart6a828b02019-01-28 11:14:31 -08008856 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
James Smartcdb42be2019-01-28 11:14:21 -08008857 if (lpfc_alloc_nvme_wq_cq(phba, idx))
8858 goto out_error;
James Smart6a828b02019-01-28 11:14:31 -08008859 }
James Smart67d12732012-08-03 12:36:13 -04008860
James Smartcdb42be2019-01-28 11:14:21 -08008861 if (phba->nvmet_support) {
8862 for (idx = 0; idx < phba->cfg_nvmet_mrq; idx++) {
8863 qdesc = lpfc_sli4_queue_alloc(
8864 phba,
James Smart81b96ed2017-11-20 16:00:29 -08008865 LPFC_DEFAULT_PAGE_SIZE,
8866 phba->sli4_hba.cq_esize,
8867 phba->sli4_hba.cq_ecount);
James Smartcdb42be2019-01-28 11:14:21 -08008868 if (!qdesc) {
8869 lpfc_printf_log(
8870 phba, KERN_ERR, LOG_INIT,
8871 "3142 Failed allocate NVME "
8872 "CQ Set (%d)\n", idx);
8873 goto out_error;
8874 }
8875 qdesc->qe_valid = 1;
James Smart5e5b5112019-01-28 11:14:22 -08008876 qdesc->hdwq = idx;
James Smart6a828b02019-01-28 11:14:31 -08008877 qdesc->chann = idx;
James Smartcdb42be2019-01-28 11:14:21 -08008878 phba->sli4_hba.nvmet_cqset[idx] = qdesc;
James Smart2d7dbc42017-02-12 13:52:35 -08008879 }
James Smart2d7dbc42017-02-12 13:52:35 -08008880 }
8881 }
8882
James Smartda0436e2009-05-22 14:51:39 -04008883 /*
James Smart67d12732012-08-03 12:36:13 -04008884 * Create Slow Path Completion Queues (CQs)
James Smartda0436e2009-05-22 14:51:39 -04008885 */
8886
James Smartda0436e2009-05-22 14:51:39 -04008887 /* Create slow-path Mailbox Command Complete Queue */
James Smart81b96ed2017-11-20 16:00:29 -08008888 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
8889 phba->sli4_hba.cq_esize,
James Smartda0436e2009-05-22 14:51:39 -04008890 phba->sli4_hba.cq_ecount);
8891 if (!qdesc) {
8892 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8893 "0500 Failed allocate slow-path mailbox CQ\n");
James Smart67d12732012-08-03 12:36:13 -04008894 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04008895 }
James Smart7365f6f2018-02-22 08:18:46 -08008896 qdesc->qe_valid = 1;
James Smartda0436e2009-05-22 14:51:39 -04008897 phba->sli4_hba.mbx_cq = qdesc;
8898
8899 /* Create slow-path ELS Complete Queue */
James Smart81b96ed2017-11-20 16:00:29 -08008900 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
8901 phba->sli4_hba.cq_esize,
James Smartda0436e2009-05-22 14:51:39 -04008902 phba->sli4_hba.cq_ecount);
8903 if (!qdesc) {
8904 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8905 "0501 Failed allocate slow-path ELS CQ\n");
James Smart67d12732012-08-03 12:36:13 -04008906 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04008907 }
James Smart7365f6f2018-02-22 08:18:46 -08008908 qdesc->qe_valid = 1;
James Smart6a828b02019-01-28 11:14:31 -08008909 qdesc->chann = 0;
James Smartda0436e2009-05-22 14:51:39 -04008910 phba->sli4_hba.els_cq = qdesc;
8911
James Smartda0436e2009-05-22 14:51:39 -04008912
James Smart5350d872011-10-10 21:33:49 -04008913 /*
James Smart67d12732012-08-03 12:36:13 -04008914 * Create Slow Path Work Queues (WQs)
James Smart5350d872011-10-10 21:33:49 -04008915 */
James Smartda0436e2009-05-22 14:51:39 -04008916
8917 /* Create Mailbox Command Queue */
James Smartda0436e2009-05-22 14:51:39 -04008918
James Smart81b96ed2017-11-20 16:00:29 -08008919 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
8920 phba->sli4_hba.mq_esize,
James Smartda0436e2009-05-22 14:51:39 -04008921 phba->sli4_hba.mq_ecount);
8922 if (!qdesc) {
8923 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8924 "0505 Failed allocate slow-path MQ\n");
James Smart67d12732012-08-03 12:36:13 -04008925 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04008926 }
James Smart6a828b02019-01-28 11:14:31 -08008927 qdesc->chann = 0;
James Smartda0436e2009-05-22 14:51:39 -04008928 phba->sli4_hba.mbx_wq = qdesc;
8929
8930 /*
James Smart67d12732012-08-03 12:36:13 -04008931 * Create ELS Work Queues
James Smartda0436e2009-05-22 14:51:39 -04008932 */
James Smartda0436e2009-05-22 14:51:39 -04008933
8934 /* Create slow-path ELS Work Queue */
James Smart81b96ed2017-11-20 16:00:29 -08008935 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
8936 phba->sli4_hba.wq_esize,
James Smartda0436e2009-05-22 14:51:39 -04008937 phba->sli4_hba.wq_ecount);
8938 if (!qdesc) {
8939 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8940 "0504 Failed allocate slow-path ELS WQ\n");
James Smart67d12732012-08-03 12:36:13 -04008941 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04008942 }
James Smart6a828b02019-01-28 11:14:31 -08008943 qdesc->chann = 0;
James Smartda0436e2009-05-22 14:51:39 -04008944 phba->sli4_hba.els_wq = qdesc;
James Smart895427b2017-02-12 13:52:30 -08008945 list_add_tail(&qdesc->wq_list, &phba->sli4_hba.lpfc_wq_list);
8946
8947 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
8948 /* Create NVME LS Complete Queue */
James Smart81b96ed2017-11-20 16:00:29 -08008949 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
8950 phba->sli4_hba.cq_esize,
James Smart895427b2017-02-12 13:52:30 -08008951 phba->sli4_hba.cq_ecount);
8952 if (!qdesc) {
8953 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8954 "6079 Failed allocate NVME LS CQ\n");
8955 goto out_error;
8956 }
James Smart6a828b02019-01-28 11:14:31 -08008957 qdesc->chann = 0;
James Smart7365f6f2018-02-22 08:18:46 -08008958 qdesc->qe_valid = 1;
James Smart895427b2017-02-12 13:52:30 -08008959 phba->sli4_hba.nvmels_cq = qdesc;
8960
8961 /* Create NVME LS Work Queue */
James Smart81b96ed2017-11-20 16:00:29 -08008962 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
8963 phba->sli4_hba.wq_esize,
James Smart895427b2017-02-12 13:52:30 -08008964 phba->sli4_hba.wq_ecount);
8965 if (!qdesc) {
8966 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8967 "6080 Failed allocate NVME LS WQ\n");
8968 goto out_error;
8969 }
James Smart6a828b02019-01-28 11:14:31 -08008970 qdesc->chann = 0;
James Smart895427b2017-02-12 13:52:30 -08008971 phba->sli4_hba.nvmels_wq = qdesc;
8972 list_add_tail(&qdesc->wq_list, &phba->sli4_hba.lpfc_wq_list);
8973 }
James Smartda0436e2009-05-22 14:51:39 -04008974
James Smartda0436e2009-05-22 14:51:39 -04008975 /*
8976 * Create Receive Queue (RQ)
8977 */
James Smartda0436e2009-05-22 14:51:39 -04008978
8979 /* Create Receive Queue for header */
James Smart81b96ed2017-11-20 16:00:29 -08008980 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
8981 phba->sli4_hba.rq_esize,
James Smartda0436e2009-05-22 14:51:39 -04008982 phba->sli4_hba.rq_ecount);
8983 if (!qdesc) {
8984 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8985 "0506 Failed allocate receive HRQ\n");
James Smart67d12732012-08-03 12:36:13 -04008986 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04008987 }
8988 phba->sli4_hba.hdr_rq = qdesc;
8989
8990 /* Create Receive Queue for data */
James Smart81b96ed2017-11-20 16:00:29 -08008991 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
8992 phba->sli4_hba.rq_esize,
James Smartda0436e2009-05-22 14:51:39 -04008993 phba->sli4_hba.rq_ecount);
8994 if (!qdesc) {
8995 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8996 "0507 Failed allocate receive DRQ\n");
James Smart67d12732012-08-03 12:36:13 -04008997 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04008998 }
8999 phba->sli4_hba.dat_rq = qdesc;
9000
James Smartcdb42be2019-01-28 11:14:21 -08009001 if ((phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) &&
9002 phba->nvmet_support) {
James Smart2d7dbc42017-02-12 13:52:35 -08009003 for (idx = 0; idx < phba->cfg_nvmet_mrq; idx++) {
9004 /* Create NVMET Receive Queue for header */
9005 qdesc = lpfc_sli4_queue_alloc(phba,
James Smart81b96ed2017-11-20 16:00:29 -08009006 LPFC_DEFAULT_PAGE_SIZE,
James Smart2d7dbc42017-02-12 13:52:35 -08009007 phba->sli4_hba.rq_esize,
James Smart61f3d4b2017-05-15 15:20:41 -07009008 LPFC_NVMET_RQE_DEF_COUNT);
James Smart2d7dbc42017-02-12 13:52:35 -08009009 if (!qdesc) {
9010 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9011 "3146 Failed allocate "
9012 "receive HRQ\n");
9013 goto out_error;
9014 }
James Smart5e5b5112019-01-28 11:14:22 -08009015 qdesc->hdwq = idx;
James Smart2d7dbc42017-02-12 13:52:35 -08009016 phba->sli4_hba.nvmet_mrq_hdr[idx] = qdesc;
9017
9018 /* Only needed for header of RQ pair */
9019 qdesc->rqbp = kzalloc(sizeof(struct lpfc_rqb),
9020 GFP_KERNEL);
9021 if (qdesc->rqbp == NULL) {
9022 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9023 "6131 Failed allocate "
9024 "Header RQBP\n");
9025 goto out_error;
9026 }
9027
Dick Kennedy4b40d022017-08-23 16:55:38 -07009028 /* Put list in known state in case driver load fails. */
9029 INIT_LIST_HEAD(&qdesc->rqbp->rqb_buffer_list);
9030
James Smart2d7dbc42017-02-12 13:52:35 -08009031 /* Create NVMET Receive Queue for data */
9032 qdesc = lpfc_sli4_queue_alloc(phba,
James Smart81b96ed2017-11-20 16:00:29 -08009033 LPFC_DEFAULT_PAGE_SIZE,
James Smart2d7dbc42017-02-12 13:52:35 -08009034 phba->sli4_hba.rq_esize,
James Smart61f3d4b2017-05-15 15:20:41 -07009035 LPFC_NVMET_RQE_DEF_COUNT);
James Smart2d7dbc42017-02-12 13:52:35 -08009036 if (!qdesc) {
9037 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9038 "3156 Failed allocate "
9039 "receive DRQ\n");
9040 goto out_error;
9041 }
James Smart5e5b5112019-01-28 11:14:22 -08009042 qdesc->hdwq = idx;
James Smart2d7dbc42017-02-12 13:52:35 -08009043 phba->sli4_hba.nvmet_mrq_data[idx] = qdesc;
9044 }
9045 }
James Smart4c47efc2019-01-28 11:14:25 -08009046
9047#if defined(BUILD_NVME)
9048 /* Clear NVME stats */
9049 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
9050 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
9051 memset(&phba->sli4_hba.hdwq[idx].nvme_cstat, 0,
9052 sizeof(phba->sli4_hba.hdwq[idx].nvme_cstat));
9053 }
9054 }
9055#endif
9056
9057 /* Clear SCSI stats */
9058 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) {
9059 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
9060 memset(&phba->sli4_hba.hdwq[idx].scsi_cstat, 0,
9061 sizeof(phba->sli4_hba.hdwq[idx].scsi_cstat));
9062 }
9063 }
9064
James Smartda0436e2009-05-22 14:51:39 -04009065 return 0;
9066
James Smartda0436e2009-05-22 14:51:39 -04009067out_error:
James Smart67d12732012-08-03 12:36:13 -04009068 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04009069 return -ENOMEM;
9070}
9071
James Smart895427b2017-02-12 13:52:30 -08009072static inline void
9073__lpfc_sli4_release_queue(struct lpfc_queue **qp)
9074{
9075 if (*qp != NULL) {
9076 lpfc_sli4_queue_free(*qp);
9077 *qp = NULL;
9078 }
9079}
9080
9081static inline void
9082lpfc_sli4_release_queues(struct lpfc_queue ***qs, int max)
9083{
9084 int idx;
9085
9086 if (*qs == NULL)
9087 return;
9088
9089 for (idx = 0; idx < max; idx++)
9090 __lpfc_sli4_release_queue(&(*qs)[idx]);
9091
9092 kfree(*qs);
9093 *qs = NULL;
9094}
9095
9096static inline void
James Smart6a828b02019-01-28 11:14:31 -08009097lpfc_sli4_release_hdwq(struct lpfc_hba *phba)
James Smart895427b2017-02-12 13:52:30 -08009098{
James Smart6a828b02019-01-28 11:14:31 -08009099 struct lpfc_sli4_hdw_queue *hdwq;
James Smartcdb42be2019-01-28 11:14:21 -08009100 uint32_t idx;
9101
James Smart6a828b02019-01-28 11:14:31 -08009102 hdwq = phba->sli4_hba.hdwq;
9103 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
9104 if (idx < phba->cfg_irq_chann)
9105 lpfc_sli4_queue_free(hdwq[idx].hba_eq);
9106 hdwq[idx].hba_eq = NULL;
9107
James Smartcdb42be2019-01-28 11:14:21 -08009108 lpfc_sli4_queue_free(hdwq[idx].fcp_cq);
9109 lpfc_sli4_queue_free(hdwq[idx].nvme_cq);
9110 lpfc_sli4_queue_free(hdwq[idx].fcp_wq);
9111 lpfc_sli4_queue_free(hdwq[idx].nvme_wq);
James Smartcdb42be2019-01-28 11:14:21 -08009112 hdwq[idx].fcp_cq = NULL;
9113 hdwq[idx].nvme_cq = NULL;
9114 hdwq[idx].fcp_wq = NULL;
9115 hdwq[idx].nvme_wq = NULL;
James Smart895427b2017-02-12 13:52:30 -08009116 }
9117}
9118
James Smartda0436e2009-05-22 14:51:39 -04009119/**
9120 * lpfc_sli4_queue_destroy - Destroy all the SLI4 queues
9121 * @phba: pointer to lpfc hba data structure.
9122 *
9123 * This routine is invoked to release all the SLI4 queues with the FCoE HBA
9124 * operation.
9125 *
9126 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02009127 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009128 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04009129 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04009130 **/
James Smart5350d872011-10-10 21:33:49 -04009131void
James Smartda0436e2009-05-22 14:51:39 -04009132lpfc_sli4_queue_destroy(struct lpfc_hba *phba)
9133{
James Smart895427b2017-02-12 13:52:30 -08009134 /* Release HBA eqs */
James Smartcdb42be2019-01-28 11:14:21 -08009135 if (phba->sli4_hba.hdwq)
James Smart6a828b02019-01-28 11:14:31 -08009136 lpfc_sli4_release_hdwq(phba);
James Smartda0436e2009-05-22 14:51:39 -04009137
James Smartbcb24f62017-11-20 16:00:36 -08009138 if (phba->nvmet_support) {
9139 lpfc_sli4_release_queues(&phba->sli4_hba.nvmet_cqset,
9140 phba->cfg_nvmet_mrq);
James Smart2d7dbc42017-02-12 13:52:35 -08009141
James Smartbcb24f62017-11-20 16:00:36 -08009142 lpfc_sli4_release_queues(&phba->sli4_hba.nvmet_mrq_hdr,
9143 phba->cfg_nvmet_mrq);
9144 lpfc_sli4_release_queues(&phba->sli4_hba.nvmet_mrq_data,
9145 phba->cfg_nvmet_mrq);
9146 }
James Smart2d7dbc42017-02-12 13:52:35 -08009147
James Smartda0436e2009-05-22 14:51:39 -04009148 /* Release mailbox command work queue */
James Smart895427b2017-02-12 13:52:30 -08009149 __lpfc_sli4_release_queue(&phba->sli4_hba.mbx_wq);
James Smartda0436e2009-05-22 14:51:39 -04009150
9151 /* Release ELS work queue */
James Smart895427b2017-02-12 13:52:30 -08009152 __lpfc_sli4_release_queue(&phba->sli4_hba.els_wq);
9153
9154 /* Release ELS work queue */
9155 __lpfc_sli4_release_queue(&phba->sli4_hba.nvmels_wq);
James Smartda0436e2009-05-22 14:51:39 -04009156
9157 /* Release unsolicited receive queue */
James Smart895427b2017-02-12 13:52:30 -08009158 __lpfc_sli4_release_queue(&phba->sli4_hba.hdr_rq);
9159 __lpfc_sli4_release_queue(&phba->sli4_hba.dat_rq);
James Smartda0436e2009-05-22 14:51:39 -04009160
James Smartda0436e2009-05-22 14:51:39 -04009161 /* Release ELS complete queue */
James Smart895427b2017-02-12 13:52:30 -08009162 __lpfc_sli4_release_queue(&phba->sli4_hba.els_cq);
9163
9164 /* Release NVME LS complete queue */
9165 __lpfc_sli4_release_queue(&phba->sli4_hba.nvmels_cq);
James Smartda0436e2009-05-22 14:51:39 -04009166
9167 /* Release mailbox command complete queue */
James Smart895427b2017-02-12 13:52:30 -08009168 __lpfc_sli4_release_queue(&phba->sli4_hba.mbx_cq);
9169
9170 /* Everything on this list has been freed */
9171 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_wq_list);
9172}
9173
9174int
James Smart895427b2017-02-12 13:52:30 -08009175lpfc_free_rq_buffer(struct lpfc_hba *phba, struct lpfc_queue *rq)
9176{
9177 struct lpfc_rqb *rqbp;
9178 struct lpfc_dmabuf *h_buf;
9179 struct rqb_dmabuf *rqb_buffer;
9180
9181 rqbp = rq->rqbp;
9182 while (!list_empty(&rqbp->rqb_buffer_list)) {
9183 list_remove_head(&rqbp->rqb_buffer_list, h_buf,
9184 struct lpfc_dmabuf, list);
9185
9186 rqb_buffer = container_of(h_buf, struct rqb_dmabuf, hbuf);
9187 (rqbp->rqb_free_buffer)(phba, rqb_buffer);
9188 rqbp->buffer_count--;
9189 }
9190 return 1;
9191}
9192
9193static int
9194lpfc_create_wq_cq(struct lpfc_hba *phba, struct lpfc_queue *eq,
9195 struct lpfc_queue *cq, struct lpfc_queue *wq, uint16_t *cq_map,
9196 int qidx, uint32_t qtype)
9197{
9198 struct lpfc_sli_ring *pring;
9199 int rc;
9200
9201 if (!eq || !cq || !wq) {
9202 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9203 "6085 Fast-path %s (%d) not allocated\n",
9204 ((eq) ? ((cq) ? "WQ" : "CQ") : "EQ"), qidx);
9205 return -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04009206 }
James Smartda0436e2009-05-22 14:51:39 -04009207
James Smart895427b2017-02-12 13:52:30 -08009208 /* create the Cq first */
9209 rc = lpfc_cq_create(phba, cq, eq,
9210 (qtype == LPFC_MBOX) ? LPFC_MCQ : LPFC_WCQ, qtype);
9211 if (rc) {
9212 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9213 "6086 Failed setup of CQ (%d), rc = 0x%x\n",
9214 qidx, (uint32_t)rc);
9215 return rc;
9216 }
9217
9218 if (qtype != LPFC_MBOX) {
James Smartcdb42be2019-01-28 11:14:21 -08009219 /* Setup cq_map for fast lookup */
James Smart895427b2017-02-12 13:52:30 -08009220 if (cq_map)
9221 *cq_map = cq->queue_id;
9222
9223 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9224 "6087 CQ setup: cq[%d]-id=%d, parent eq[%d]-id=%d\n",
9225 qidx, cq->queue_id, qidx, eq->queue_id);
9226
9227 /* create the wq */
9228 rc = lpfc_wq_create(phba, wq, cq, qtype);
9229 if (rc) {
9230 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9231 "6123 Fail setup fastpath WQ (%d), rc = 0x%x\n",
9232 qidx, (uint32_t)rc);
9233 /* no need to tear down cq - caller will do so */
9234 return rc;
9235 }
9236
9237 /* Bind this CQ/WQ to the NVME ring */
9238 pring = wq->pring;
9239 pring->sli.sli4.wqp = (void *)wq;
9240 cq->pring = pring;
9241
9242 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9243 "2593 WQ setup: wq[%d]-id=%d assoc=%d, cq[%d]-id=%d\n",
9244 qidx, wq->queue_id, wq->assoc_qid, qidx, cq->queue_id);
9245 } else {
9246 rc = lpfc_mq_create(phba, wq, cq, LPFC_MBOX);
9247 if (rc) {
9248 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9249 "0539 Failed setup of slow-path MQ: "
9250 "rc = 0x%x\n", rc);
9251 /* no need to tear down cq - caller will do so */
9252 return rc;
9253 }
9254
9255 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9256 "2589 MBX MQ setup: wq-id=%d, parent cq-id=%d\n",
9257 phba->sli4_hba.mbx_wq->queue_id,
9258 phba->sli4_hba.mbx_cq->queue_id);
9259 }
9260
9261 return 0;
James Smartda0436e2009-05-22 14:51:39 -04009262}
9263
9264/**
James Smart6a828b02019-01-28 11:14:31 -08009265 * lpfc_setup_cq_lookup - Setup the CQ lookup table
9266 * @phba: pointer to lpfc hba data structure.
9267 *
9268 * This routine will populate the cq_lookup table by all
9269 * available CQ queue_id's.
9270 **/
9271void
9272lpfc_setup_cq_lookup(struct lpfc_hba *phba)
9273{
9274 struct lpfc_queue *eq, *childq;
9275 struct lpfc_sli4_hdw_queue *qp;
9276 int qidx;
9277
9278 qp = phba->sli4_hba.hdwq;
9279 memset(phba->sli4_hba.cq_lookup, 0,
9280 (sizeof(struct lpfc_queue *) * (phba->sli4_hba.cq_max + 1)));
9281 for (qidx = 0; qidx < phba->cfg_irq_chann; qidx++) {
9282 eq = qp[qidx].hba_eq;
9283 if (!eq)
9284 continue;
9285 list_for_each_entry(childq, &eq->child_list, list) {
9286 if (childq->queue_id > phba->sli4_hba.cq_max)
9287 continue;
9288 if ((childq->subtype == LPFC_FCP) ||
9289 (childq->subtype == LPFC_NVME))
9290 phba->sli4_hba.cq_lookup[childq->queue_id] =
9291 childq;
9292 }
9293 }
9294}
9295
9296/**
James Smartda0436e2009-05-22 14:51:39 -04009297 * lpfc_sli4_queue_setup - Set up all the SLI4 queues
9298 * @phba: pointer to lpfc hba data structure.
9299 *
9300 * This routine is invoked to set up all the SLI4 queues for the FCoE HBA
9301 * operation.
9302 *
9303 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02009304 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009305 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04009306 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04009307 **/
9308int
9309lpfc_sli4_queue_setup(struct lpfc_hba *phba)
9310{
James Smart962bc512013-01-03 15:44:00 -05009311 uint32_t shdr_status, shdr_add_status;
9312 union lpfc_sli4_cfg_shdr *shdr;
James Smartcdb42be2019-01-28 11:14:21 -08009313 struct lpfc_sli4_hdw_queue *qp;
James Smart962bc512013-01-03 15:44:00 -05009314 LPFC_MBOXQ_t *mboxq;
James Smart895427b2017-02-12 13:52:30 -08009315 int qidx;
James Smartcb733e32019-01-28 11:14:32 -08009316 uint32_t length, usdelay;
James Smart895427b2017-02-12 13:52:30 -08009317 int rc = -ENOMEM;
James Smart962bc512013-01-03 15:44:00 -05009318
9319 /* Check for dual-ULP support */
9320 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9321 if (!mboxq) {
9322 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9323 "3249 Unable to allocate memory for "
9324 "QUERY_FW_CFG mailbox command\n");
9325 return -ENOMEM;
9326 }
9327 length = (sizeof(struct lpfc_mbx_query_fw_config) -
9328 sizeof(struct lpfc_sli4_cfg_mhdr));
9329 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
9330 LPFC_MBOX_OPCODE_QUERY_FW_CFG,
9331 length, LPFC_SLI4_MBX_EMBED);
9332
9333 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
9334
9335 shdr = (union lpfc_sli4_cfg_shdr *)
9336 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
9337 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9338 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9339 if (shdr_status || shdr_add_status || rc) {
9340 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9341 "3250 QUERY_FW_CFG mailbox failed with status "
9342 "x%x add_status x%x, mbx status x%x\n",
9343 shdr_status, shdr_add_status, rc);
9344 if (rc != MBX_TIMEOUT)
9345 mempool_free(mboxq, phba->mbox_mem_pool);
9346 rc = -ENXIO;
9347 goto out_error;
9348 }
9349
9350 phba->sli4_hba.fw_func_mode =
9351 mboxq->u.mqe.un.query_fw_cfg.rsp.function_mode;
9352 phba->sli4_hba.ulp0_mode = mboxq->u.mqe.un.query_fw_cfg.rsp.ulp0_mode;
9353 phba->sli4_hba.ulp1_mode = mboxq->u.mqe.un.query_fw_cfg.rsp.ulp1_mode;
James Smart8b017a32015-05-21 13:55:18 -04009354 phba->sli4_hba.physical_port =
9355 mboxq->u.mqe.un.query_fw_cfg.rsp.physical_port;
James Smart962bc512013-01-03 15:44:00 -05009356 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9357 "3251 QUERY_FW_CFG: func_mode:x%x, ulp0_mode:x%x, "
9358 "ulp1_mode:x%x\n", phba->sli4_hba.fw_func_mode,
9359 phba->sli4_hba.ulp0_mode, phba->sli4_hba.ulp1_mode);
9360
9361 if (rc != MBX_TIMEOUT)
9362 mempool_free(mboxq, phba->mbox_mem_pool);
James Smartda0436e2009-05-22 14:51:39 -04009363
9364 /*
James Smart67d12732012-08-03 12:36:13 -04009365 * Set up HBA Event Queues (EQs)
James Smartda0436e2009-05-22 14:51:39 -04009366 */
James Smartcdb42be2019-01-28 11:14:21 -08009367 qp = phba->sli4_hba.hdwq;
James Smartda0436e2009-05-22 14:51:39 -04009368
James Smart67d12732012-08-03 12:36:13 -04009369 /* Set up HBA event queue */
James Smartcdb42be2019-01-28 11:14:21 -08009370 if (!qp) {
James Smart2e90f4b2011-12-13 13:22:37 -05009371 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9372 "3147 Fast-path EQs not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05009373 rc = -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04009374 goto out_error;
James Smart2e90f4b2011-12-13 13:22:37 -05009375 }
James Smart6a828b02019-01-28 11:14:31 -08009376 for (qidx = 0; qidx < phba->cfg_irq_chann; qidx++) {
James Smartcdb42be2019-01-28 11:14:21 -08009377 if (!qp[qidx].hba_eq) {
James Smartda0436e2009-05-22 14:51:39 -04009378 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9379 "0522 Fast-path EQ (%d) not "
James Smart895427b2017-02-12 13:52:30 -08009380 "allocated\n", qidx);
James Smart1b511972011-12-13 13:23:09 -05009381 rc = -ENOMEM;
James Smart895427b2017-02-12 13:52:30 -08009382 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -04009383 }
James Smartcdb42be2019-01-28 11:14:21 -08009384 rc = lpfc_eq_create(phba, qp[qidx].hba_eq,
9385 phba->cfg_fcp_imax);
James Smartda0436e2009-05-22 14:51:39 -04009386 if (rc) {
9387 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9388 "0523 Failed setup of fast-path EQ "
James Smart895427b2017-02-12 13:52:30 -08009389 "(%d), rc = 0x%x\n", qidx,
James Smarta2fc4aef2014-09-03 12:57:55 -04009390 (uint32_t)rc);
James Smart895427b2017-02-12 13:52:30 -08009391 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -04009392 }
9393 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smartcdb42be2019-01-28 11:14:21 -08009394 "2584 HBA EQ setup: queue[%d]-id=%d\n", qidx,
9395 qp[qidx].hba_eq->queue_id);
James Smartda0436e2009-05-22 14:51:39 -04009396 }
9397
James Smartcdb42be2019-01-28 11:14:21 -08009398 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
9399 for (qidx = 0; qidx < phba->cfg_hdw_queue; qidx++) {
James Smart895427b2017-02-12 13:52:30 -08009400 rc = lpfc_create_wq_cq(phba,
James Smartcdb42be2019-01-28 11:14:21 -08009401 qp[qidx].hba_eq,
9402 qp[qidx].nvme_cq,
9403 qp[qidx].nvme_wq,
9404 &phba->sli4_hba.hdwq[qidx].nvme_cq_map,
James Smart895427b2017-02-12 13:52:30 -08009405 qidx, LPFC_NVME);
9406 if (rc) {
9407 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9408 "6123 Failed to setup fastpath "
9409 "NVME WQ/CQ (%d), rc = 0x%x\n",
9410 qidx, (uint32_t)rc);
9411 goto out_destroy;
9412 }
9413 }
James Smart67d12732012-08-03 12:36:13 -04009414 }
9415
James Smartcdb42be2019-01-28 11:14:21 -08009416 for (qidx = 0; qidx < phba->cfg_hdw_queue; qidx++) {
9417 rc = lpfc_create_wq_cq(phba,
9418 qp[qidx].hba_eq,
9419 qp[qidx].fcp_cq,
9420 qp[qidx].fcp_wq,
9421 &phba->sli4_hba.hdwq[qidx].fcp_cq_map,
9422 qidx, LPFC_FCP);
9423 if (rc) {
James Smart67d12732012-08-03 12:36:13 -04009424 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart895427b2017-02-12 13:52:30 -08009425 "0535 Failed to setup fastpath "
9426 "FCP WQ/CQ (%d), rc = 0x%x\n",
9427 qidx, (uint32_t)rc);
James Smartcdb42be2019-01-28 11:14:21 -08009428 goto out_destroy;
James Smart895427b2017-02-12 13:52:30 -08009429 }
James Smart67d12732012-08-03 12:36:13 -04009430 }
James Smartda0436e2009-05-22 14:51:39 -04009431
9432 /*
James Smart895427b2017-02-12 13:52:30 -08009433 * Set up Slow Path Complete Queues (CQs)
James Smartda0436e2009-05-22 14:51:39 -04009434 */
9435
James Smart895427b2017-02-12 13:52:30 -08009436 /* Set up slow-path MBOX CQ/MQ */
9437
9438 if (!phba->sli4_hba.mbx_cq || !phba->sli4_hba.mbx_wq) {
James Smartda0436e2009-05-22 14:51:39 -04009439 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart895427b2017-02-12 13:52:30 -08009440 "0528 %s not allocated\n",
9441 phba->sli4_hba.mbx_cq ?
James Smartd1f525a2017-04-21 16:04:55 -07009442 "Mailbox WQ" : "Mailbox CQ");
James Smart1b511972011-12-13 13:23:09 -05009443 rc = -ENOMEM;
James Smart895427b2017-02-12 13:52:30 -08009444 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -04009445 }
James Smart895427b2017-02-12 13:52:30 -08009446
James Smartcdb42be2019-01-28 11:14:21 -08009447 rc = lpfc_create_wq_cq(phba, qp[0].hba_eq,
James Smartd1f525a2017-04-21 16:04:55 -07009448 phba->sli4_hba.mbx_cq,
9449 phba->sli4_hba.mbx_wq,
9450 NULL, 0, LPFC_MBOX);
James Smartda0436e2009-05-22 14:51:39 -04009451 if (rc) {
9452 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart895427b2017-02-12 13:52:30 -08009453 "0529 Failed setup of mailbox WQ/CQ: rc = 0x%x\n",
9454 (uint32_t)rc);
9455 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -04009456 }
James Smart2d7dbc42017-02-12 13:52:35 -08009457 if (phba->nvmet_support) {
9458 if (!phba->sli4_hba.nvmet_cqset) {
9459 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9460 "3165 Fast-path NVME CQ Set "
9461 "array not allocated\n");
9462 rc = -ENOMEM;
9463 goto out_destroy;
9464 }
9465 if (phba->cfg_nvmet_mrq > 1) {
9466 rc = lpfc_cq_create_set(phba,
9467 phba->sli4_hba.nvmet_cqset,
James Smartcdb42be2019-01-28 11:14:21 -08009468 qp,
James Smart2d7dbc42017-02-12 13:52:35 -08009469 LPFC_WCQ, LPFC_NVMET);
9470 if (rc) {
9471 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9472 "3164 Failed setup of NVME CQ "
9473 "Set, rc = 0x%x\n",
9474 (uint32_t)rc);
9475 goto out_destroy;
9476 }
9477 } else {
9478 /* Set up NVMET Receive Complete Queue */
9479 rc = lpfc_cq_create(phba, phba->sli4_hba.nvmet_cqset[0],
James Smartcdb42be2019-01-28 11:14:21 -08009480 qp[0].hba_eq,
James Smart2d7dbc42017-02-12 13:52:35 -08009481 LPFC_WCQ, LPFC_NVMET);
9482 if (rc) {
9483 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9484 "6089 Failed setup NVMET CQ: "
9485 "rc = 0x%x\n", (uint32_t)rc);
9486 goto out_destroy;
9487 }
James Smart81b96ed2017-11-20 16:00:29 -08009488 phba->sli4_hba.nvmet_cqset[0]->chann = 0;
9489
James Smart2d7dbc42017-02-12 13:52:35 -08009490 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9491 "6090 NVMET CQ setup: cq-id=%d, "
9492 "parent eq-id=%d\n",
9493 phba->sli4_hba.nvmet_cqset[0]->queue_id,
James Smartcdb42be2019-01-28 11:14:21 -08009494 qp[0].hba_eq->queue_id);
James Smart2d7dbc42017-02-12 13:52:35 -08009495 }
9496 }
James Smartda0436e2009-05-22 14:51:39 -04009497
James Smart895427b2017-02-12 13:52:30 -08009498 /* Set up slow-path ELS WQ/CQ */
9499 if (!phba->sli4_hba.els_cq || !phba->sli4_hba.els_wq) {
James Smartda0436e2009-05-22 14:51:39 -04009500 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart895427b2017-02-12 13:52:30 -08009501 "0530 ELS %s not allocated\n",
9502 phba->sli4_hba.els_cq ? "WQ" : "CQ");
James Smart1b511972011-12-13 13:23:09 -05009503 rc = -ENOMEM;
James Smart895427b2017-02-12 13:52:30 -08009504 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -04009505 }
James Smartcdb42be2019-01-28 11:14:21 -08009506 rc = lpfc_create_wq_cq(phba, qp[0].hba_eq,
9507 phba->sli4_hba.els_cq,
9508 phba->sli4_hba.els_wq,
9509 NULL, 0, LPFC_ELS);
James Smartda0436e2009-05-22 14:51:39 -04009510 if (rc) {
9511 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartcdb42be2019-01-28 11:14:21 -08009512 "0525 Failed setup of ELS WQ/CQ: rc = 0x%x\n",
9513 (uint32_t)rc);
James Smart895427b2017-02-12 13:52:30 -08009514 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -04009515 }
9516 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9517 "2590 ELS WQ setup: wq-id=%d, parent cq-id=%d\n",
9518 phba->sli4_hba.els_wq->queue_id,
9519 phba->sli4_hba.els_cq->queue_id);
9520
James Smartcdb42be2019-01-28 11:14:21 -08009521 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
James Smart895427b2017-02-12 13:52:30 -08009522 /* Set up NVME LS Complete Queue */
9523 if (!phba->sli4_hba.nvmels_cq || !phba->sli4_hba.nvmels_wq) {
9524 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9525 "6091 LS %s not allocated\n",
9526 phba->sli4_hba.nvmels_cq ? "WQ" : "CQ");
9527 rc = -ENOMEM;
9528 goto out_destroy;
9529 }
James Smartcdb42be2019-01-28 11:14:21 -08009530 rc = lpfc_create_wq_cq(phba, qp[0].hba_eq,
9531 phba->sli4_hba.nvmels_cq,
9532 phba->sli4_hba.nvmels_wq,
9533 NULL, 0, LPFC_NVME_LS);
James Smart895427b2017-02-12 13:52:30 -08009534 if (rc) {
9535 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartcdb42be2019-01-28 11:14:21 -08009536 "0526 Failed setup of NVVME LS WQ/CQ: "
9537 "rc = 0x%x\n", (uint32_t)rc);
James Smart895427b2017-02-12 13:52:30 -08009538 goto out_destroy;
9539 }
9540
9541 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9542 "6096 ELS WQ setup: wq-id=%d, "
9543 "parent cq-id=%d\n",
9544 phba->sli4_hba.nvmels_wq->queue_id,
9545 phba->sli4_hba.nvmels_cq->queue_id);
9546 }
9547
James Smart2d7dbc42017-02-12 13:52:35 -08009548 /*
9549 * Create NVMET Receive Queue (RQ)
9550 */
9551 if (phba->nvmet_support) {
9552 if ((!phba->sli4_hba.nvmet_cqset) ||
9553 (!phba->sli4_hba.nvmet_mrq_hdr) ||
9554 (!phba->sli4_hba.nvmet_mrq_data)) {
9555 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9556 "6130 MRQ CQ Queues not "
9557 "allocated\n");
9558 rc = -ENOMEM;
9559 goto out_destroy;
9560 }
9561 if (phba->cfg_nvmet_mrq > 1) {
9562 rc = lpfc_mrq_create(phba,
9563 phba->sli4_hba.nvmet_mrq_hdr,
9564 phba->sli4_hba.nvmet_mrq_data,
9565 phba->sli4_hba.nvmet_cqset,
9566 LPFC_NVMET);
9567 if (rc) {
9568 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9569 "6098 Failed setup of NVMET "
9570 "MRQ: rc = 0x%x\n",
9571 (uint32_t)rc);
9572 goto out_destroy;
9573 }
9574
9575 } else {
9576 rc = lpfc_rq_create(phba,
9577 phba->sli4_hba.nvmet_mrq_hdr[0],
9578 phba->sli4_hba.nvmet_mrq_data[0],
9579 phba->sli4_hba.nvmet_cqset[0],
9580 LPFC_NVMET);
9581 if (rc) {
9582 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9583 "6057 Failed setup of NVMET "
9584 "Receive Queue: rc = 0x%x\n",
9585 (uint32_t)rc);
9586 goto out_destroy;
9587 }
9588
9589 lpfc_printf_log(
9590 phba, KERN_INFO, LOG_INIT,
9591 "6099 NVMET RQ setup: hdr-rq-id=%d, "
9592 "dat-rq-id=%d parent cq-id=%d\n",
9593 phba->sli4_hba.nvmet_mrq_hdr[0]->queue_id,
9594 phba->sli4_hba.nvmet_mrq_data[0]->queue_id,
9595 phba->sli4_hba.nvmet_cqset[0]->queue_id);
9596
9597 }
9598 }
9599
James Smartda0436e2009-05-22 14:51:39 -04009600 if (!phba->sli4_hba.hdr_rq || !phba->sli4_hba.dat_rq) {
9601 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9602 "0540 Receive Queue not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05009603 rc = -ENOMEM;
James Smart895427b2017-02-12 13:52:30 -08009604 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -04009605 }
James Smart73d91e52011-10-10 21:32:10 -04009606
James Smartda0436e2009-05-22 14:51:39 -04009607 rc = lpfc_rq_create(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
James Smart4d9ab992009-10-02 15:16:39 -04009608 phba->sli4_hba.els_cq, LPFC_USOL);
James Smartda0436e2009-05-22 14:51:39 -04009609 if (rc) {
9610 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9611 "0541 Failed setup of Receive Queue: "
James Smarta2fc4aef2014-09-03 12:57:55 -04009612 "rc = 0x%x\n", (uint32_t)rc);
James Smart895427b2017-02-12 13:52:30 -08009613 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -04009614 }
James Smart73d91e52011-10-10 21:32:10 -04009615
James Smartda0436e2009-05-22 14:51:39 -04009616 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9617 "2592 USL RQ setup: hdr-rq-id=%d, dat-rq-id=%d "
9618 "parent cq-id=%d\n",
9619 phba->sli4_hba.hdr_rq->queue_id,
9620 phba->sli4_hba.dat_rq->queue_id,
James Smart4d9ab992009-10-02 15:16:39 -04009621 phba->sli4_hba.els_cq->queue_id);
James Smart1ba981f2014-02-20 09:56:45 -05009622
James Smartcb733e32019-01-28 11:14:32 -08009623 if (phba->cfg_fcp_imax)
9624 usdelay = LPFC_SEC_TO_USEC / phba->cfg_fcp_imax;
9625 else
9626 usdelay = 0;
9627
James Smart6a828b02019-01-28 11:14:31 -08009628 for (qidx = 0; qidx < phba->cfg_irq_chann;
James Smartcdb42be2019-01-28 11:14:21 -08009629 qidx += LPFC_MAX_EQ_DELAY_EQID_CNT)
James Smart0cf07f842017-06-01 21:07:10 -07009630 lpfc_modify_hba_eq_delay(phba, qidx, LPFC_MAX_EQ_DELAY_EQID_CNT,
James Smartcb733e32019-01-28 11:14:32 -08009631 usdelay);
James Smart43140ca2017-03-04 09:30:34 -08009632
James Smart6a828b02019-01-28 11:14:31 -08009633 if (phba->sli4_hba.cq_max) {
9634 kfree(phba->sli4_hba.cq_lookup);
9635 phba->sli4_hba.cq_lookup = kcalloc((phba->sli4_hba.cq_max + 1),
9636 sizeof(struct lpfc_queue *), GFP_KERNEL);
9637 if (!phba->sli4_hba.cq_lookup) {
9638 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9639 "0549 Failed setup of CQ Lookup table: "
9640 "size 0x%x\n", phba->sli4_hba.cq_max);
9641 goto out_destroy;
9642 }
9643 lpfc_setup_cq_lookup(phba);
9644 }
James Smartda0436e2009-05-22 14:51:39 -04009645 return 0;
9646
James Smart895427b2017-02-12 13:52:30 -08009647out_destroy:
9648 lpfc_sli4_queue_unset(phba);
James Smartda0436e2009-05-22 14:51:39 -04009649out_error:
9650 return rc;
9651}
9652
9653/**
9654 * lpfc_sli4_queue_unset - Unset all the SLI4 queues
9655 * @phba: pointer to lpfc hba data structure.
9656 *
9657 * This routine is invoked to unset all the SLI4 queues with the FCoE HBA
9658 * operation.
9659 *
9660 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02009661 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009662 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04009663 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04009664 **/
9665void
9666lpfc_sli4_queue_unset(struct lpfc_hba *phba)
9667{
James Smartcdb42be2019-01-28 11:14:21 -08009668 struct lpfc_sli4_hdw_queue *qp;
James Smart895427b2017-02-12 13:52:30 -08009669 int qidx;
James Smartda0436e2009-05-22 14:51:39 -04009670
9671 /* Unset mailbox command work queue */
James Smart895427b2017-02-12 13:52:30 -08009672 if (phba->sli4_hba.mbx_wq)
9673 lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
9674
9675 /* Unset NVME LS work queue */
9676 if (phba->sli4_hba.nvmels_wq)
9677 lpfc_wq_destroy(phba, phba->sli4_hba.nvmels_wq);
9678
James Smartda0436e2009-05-22 14:51:39 -04009679 /* Unset ELS work queue */
Colin Ian King019c0d62017-05-06 23:13:55 +01009680 if (phba->sli4_hba.els_wq)
James Smart895427b2017-02-12 13:52:30 -08009681 lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
9682
James Smartda0436e2009-05-22 14:51:39 -04009683 /* Unset unsolicited receive queue */
James Smart895427b2017-02-12 13:52:30 -08009684 if (phba->sli4_hba.hdr_rq)
9685 lpfc_rq_destroy(phba, phba->sli4_hba.hdr_rq,
9686 phba->sli4_hba.dat_rq);
9687
James Smartda0436e2009-05-22 14:51:39 -04009688 /* Unset mailbox command complete queue */
James Smart895427b2017-02-12 13:52:30 -08009689 if (phba->sli4_hba.mbx_cq)
9690 lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
9691
James Smartda0436e2009-05-22 14:51:39 -04009692 /* Unset ELS complete queue */
James Smart895427b2017-02-12 13:52:30 -08009693 if (phba->sli4_hba.els_cq)
9694 lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
9695
9696 /* Unset NVME LS complete queue */
9697 if (phba->sli4_hba.nvmels_cq)
9698 lpfc_cq_destroy(phba, phba->sli4_hba.nvmels_cq);
9699
James Smartbcb24f62017-11-20 16:00:36 -08009700 if (phba->nvmet_support) {
9701 /* Unset NVMET MRQ queue */
9702 if (phba->sli4_hba.nvmet_mrq_hdr) {
9703 for (qidx = 0; qidx < phba->cfg_nvmet_mrq; qidx++)
9704 lpfc_rq_destroy(
9705 phba,
James Smart2d7dbc42017-02-12 13:52:35 -08009706 phba->sli4_hba.nvmet_mrq_hdr[qidx],
9707 phba->sli4_hba.nvmet_mrq_data[qidx]);
James Smartbcb24f62017-11-20 16:00:36 -08009708 }
James Smart2d7dbc42017-02-12 13:52:35 -08009709
James Smartbcb24f62017-11-20 16:00:36 -08009710 /* Unset NVMET CQ Set complete queue */
9711 if (phba->sli4_hba.nvmet_cqset) {
9712 for (qidx = 0; qidx < phba->cfg_nvmet_mrq; qidx++)
9713 lpfc_cq_destroy(
9714 phba, phba->sli4_hba.nvmet_cqset[qidx]);
9715 }
James Smart2d7dbc42017-02-12 13:52:35 -08009716 }
9717
James Smartcdb42be2019-01-28 11:14:21 -08009718 /* Unset fast-path SLI4 queues */
9719 if (phba->sli4_hba.hdwq) {
9720 for (qidx = 0; qidx < phba->cfg_hdw_queue; qidx++) {
9721 qp = &phba->sli4_hba.hdwq[qidx];
9722 lpfc_wq_destroy(phba, qp->fcp_wq);
9723 lpfc_wq_destroy(phba, qp->nvme_wq);
9724 lpfc_cq_destroy(phba, qp->fcp_cq);
9725 lpfc_cq_destroy(phba, qp->nvme_cq);
James Smart6a828b02019-01-28 11:14:31 -08009726 if (qidx < phba->cfg_irq_chann)
9727 lpfc_eq_destroy(phba, qp->hba_eq);
James Smartcdb42be2019-01-28 11:14:21 -08009728 }
9729 }
James Smart6a828b02019-01-28 11:14:31 -08009730
9731 kfree(phba->sli4_hba.cq_lookup);
9732 phba->sli4_hba.cq_lookup = NULL;
9733 phba->sli4_hba.cq_max = 0;
James Smartda0436e2009-05-22 14:51:39 -04009734}
9735
9736/**
9737 * lpfc_sli4_cq_event_pool_create - Create completion-queue event free pool
9738 * @phba: pointer to lpfc hba data structure.
9739 *
9740 * This routine is invoked to allocate and set up a pool of completion queue
9741 * events. The body of the completion queue event is a completion queue entry
9742 * CQE. For now, this pool is used for the interrupt service routine to queue
9743 * the following HBA completion queue events for the worker thread to process:
9744 * - Mailbox asynchronous events
9745 * - Receive queue completion unsolicited events
9746 * Later, this can be used for all the slow-path events.
9747 *
9748 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02009749 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009750 * -ENOMEM - No available memory
James Smartda0436e2009-05-22 14:51:39 -04009751 **/
9752static int
9753lpfc_sli4_cq_event_pool_create(struct lpfc_hba *phba)
9754{
9755 struct lpfc_cq_event *cq_event;
9756 int i;
9757
9758 for (i = 0; i < (4 * phba->sli4_hba.cq_ecount); i++) {
9759 cq_event = kmalloc(sizeof(struct lpfc_cq_event), GFP_KERNEL);
9760 if (!cq_event)
9761 goto out_pool_create_fail;
9762 list_add_tail(&cq_event->list,
9763 &phba->sli4_hba.sp_cqe_event_pool);
9764 }
9765 return 0;
9766
9767out_pool_create_fail:
9768 lpfc_sli4_cq_event_pool_destroy(phba);
9769 return -ENOMEM;
9770}
9771
9772/**
9773 * lpfc_sli4_cq_event_pool_destroy - Free completion-queue event free pool
9774 * @phba: pointer to lpfc hba data structure.
9775 *
9776 * This routine is invoked to free the pool of completion queue events at
9777 * driver unload time. Note that, it is the responsibility of the driver
9778 * cleanup routine to free all the outstanding completion-queue events
9779 * allocated from this pool back into the pool before invoking this routine
9780 * to destroy the pool.
9781 **/
9782static void
9783lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *phba)
9784{
9785 struct lpfc_cq_event *cq_event, *next_cq_event;
9786
9787 list_for_each_entry_safe(cq_event, next_cq_event,
9788 &phba->sli4_hba.sp_cqe_event_pool, list) {
9789 list_del(&cq_event->list);
9790 kfree(cq_event);
9791 }
9792}
9793
9794/**
9795 * __lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
9796 * @phba: pointer to lpfc hba data structure.
9797 *
9798 * This routine is the lock free version of the API invoked to allocate a
9799 * completion-queue event from the free pool.
9800 *
9801 * Return: Pointer to the newly allocated completion-queue event if successful
9802 * NULL otherwise.
9803 **/
9804struct lpfc_cq_event *
9805__lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
9806{
9807 struct lpfc_cq_event *cq_event = NULL;
9808
9809 list_remove_head(&phba->sli4_hba.sp_cqe_event_pool, cq_event,
9810 struct lpfc_cq_event, list);
9811 return cq_event;
9812}
9813
9814/**
9815 * lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
9816 * @phba: pointer to lpfc hba data structure.
9817 *
9818 * This routine is the lock version of the API invoked to allocate a
9819 * completion-queue event from the free pool.
9820 *
9821 * Return: Pointer to the newly allocated completion-queue event if successful
9822 * NULL otherwise.
9823 **/
9824struct lpfc_cq_event *
9825lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
9826{
9827 struct lpfc_cq_event *cq_event;
9828 unsigned long iflags;
9829
9830 spin_lock_irqsave(&phba->hbalock, iflags);
9831 cq_event = __lpfc_sli4_cq_event_alloc(phba);
9832 spin_unlock_irqrestore(&phba->hbalock, iflags);
9833 return cq_event;
9834}
9835
9836/**
9837 * __lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
9838 * @phba: pointer to lpfc hba data structure.
9839 * @cq_event: pointer to the completion queue event to be freed.
9840 *
9841 * This routine is the lock free version of the API invoked to release a
9842 * completion-queue event back into the free pool.
9843 **/
9844void
9845__lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
9846 struct lpfc_cq_event *cq_event)
9847{
9848 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_cqe_event_pool);
9849}
9850
9851/**
9852 * lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
9853 * @phba: pointer to lpfc hba data structure.
9854 * @cq_event: pointer to the completion queue event to be freed.
9855 *
9856 * This routine is the lock version of the API invoked to release a
9857 * completion-queue event back into the free pool.
9858 **/
9859void
9860lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
9861 struct lpfc_cq_event *cq_event)
9862{
9863 unsigned long iflags;
9864 spin_lock_irqsave(&phba->hbalock, iflags);
9865 __lpfc_sli4_cq_event_release(phba, cq_event);
9866 spin_unlock_irqrestore(&phba->hbalock, iflags);
9867}
9868
9869/**
9870 * lpfc_sli4_cq_event_release_all - Release all cq events to the free pool
9871 * @phba: pointer to lpfc hba data structure.
9872 *
9873 * This routine is to free all the pending completion-queue events to the
9874 * back into the free pool for device reset.
9875 **/
9876static void
9877lpfc_sli4_cq_event_release_all(struct lpfc_hba *phba)
9878{
9879 LIST_HEAD(cqelist);
9880 struct lpfc_cq_event *cqe;
9881 unsigned long iflags;
9882
9883 /* Retrieve all the pending WCQEs from pending WCQE lists */
9884 spin_lock_irqsave(&phba->hbalock, iflags);
9885 /* Pending FCP XRI abort events */
9886 list_splice_init(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
9887 &cqelist);
9888 /* Pending ELS XRI abort events */
9889 list_splice_init(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
9890 &cqelist);
9891 /* Pending asynnc events */
9892 list_splice_init(&phba->sli4_hba.sp_asynce_work_queue,
9893 &cqelist);
9894 spin_unlock_irqrestore(&phba->hbalock, iflags);
9895
9896 while (!list_empty(&cqelist)) {
9897 list_remove_head(&cqelist, cqe, struct lpfc_cq_event, list);
9898 lpfc_sli4_cq_event_release(phba, cqe);
9899 }
9900}
9901
9902/**
9903 * lpfc_pci_function_reset - Reset pci function.
9904 * @phba: pointer to lpfc hba data structure.
9905 *
9906 * This routine is invoked to request a PCI function reset. It will destroys
9907 * all resources assigned to the PCI function which originates this request.
9908 *
9909 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02009910 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009911 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04009912 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04009913 **/
9914int
9915lpfc_pci_function_reset(struct lpfc_hba *phba)
9916{
9917 LPFC_MBOXQ_t *mboxq;
James Smart2fcee4b2010-12-15 17:57:46 -05009918 uint32_t rc = 0, if_type;
James Smartda0436e2009-05-22 14:51:39 -04009919 uint32_t shdr_status, shdr_add_status;
James Smart2f6fa2c2014-09-03 12:57:08 -04009920 uint32_t rdy_chk;
9921 uint32_t port_reset = 0;
James Smartda0436e2009-05-22 14:51:39 -04009922 union lpfc_sli4_cfg_shdr *shdr;
James Smart2fcee4b2010-12-15 17:57:46 -05009923 struct lpfc_register reg_data;
James Smart2b81f942012-03-01 22:37:18 -05009924 uint16_t devid;
James Smartda0436e2009-05-22 14:51:39 -04009925
James Smart2fcee4b2010-12-15 17:57:46 -05009926 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
9927 switch (if_type) {
9928 case LPFC_SLI_INTF_IF_TYPE_0:
9929 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
9930 GFP_KERNEL);
9931 if (!mboxq) {
9932 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9933 "0494 Unable to allocate memory for "
9934 "issuing SLI_FUNCTION_RESET mailbox "
9935 "command\n");
9936 return -ENOMEM;
9937 }
9938
9939 /* Setup PCI function reset mailbox-ioctl command */
9940 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
9941 LPFC_MBOX_OPCODE_FUNCTION_RESET, 0,
9942 LPFC_SLI4_MBX_EMBED);
9943 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
9944 shdr = (union lpfc_sli4_cfg_shdr *)
9945 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
9946 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9947 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
9948 &shdr->response);
9949 if (rc != MBX_TIMEOUT)
9950 mempool_free(mboxq, phba->mbox_mem_pool);
9951 if (shdr_status || shdr_add_status || rc) {
9952 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9953 "0495 SLI_FUNCTION_RESET mailbox "
9954 "failed with status x%x add_status x%x,"
9955 " mbx status x%x\n",
9956 shdr_status, shdr_add_status, rc);
9957 rc = -ENXIO;
9958 }
9959 break;
9960 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart27d6ac02018-02-22 08:18:42 -08009961 case LPFC_SLI_INTF_IF_TYPE_6:
James Smart2f6fa2c2014-09-03 12:57:08 -04009962wait:
9963 /*
9964 * Poll the Port Status Register and wait for RDY for
9965 * up to 30 seconds. If the port doesn't respond, treat
9966 * it as an error.
9967 */
James Smart77d093f2015-04-07 15:07:08 -04009968 for (rdy_chk = 0; rdy_chk < 1500; rdy_chk++) {
James Smart2f6fa2c2014-09-03 12:57:08 -04009969 if (lpfc_readl(phba->sli4_hba.u.if_type2.
9970 STATUSregaddr, &reg_data.word0)) {
9971 rc = -ENODEV;
9972 goto out;
9973 }
9974 if (bf_get(lpfc_sliport_status_rdy, &reg_data))
9975 break;
9976 msleep(20);
9977 }
9978
9979 if (!bf_get(lpfc_sliport_status_rdy, &reg_data)) {
9980 phba->work_status[0] = readl(
9981 phba->sli4_hba.u.if_type2.ERR1regaddr);
9982 phba->work_status[1] = readl(
9983 phba->sli4_hba.u.if_type2.ERR2regaddr);
9984 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9985 "2890 Port not ready, port status reg "
9986 "0x%x error 1=0x%x, error 2=0x%x\n",
9987 reg_data.word0,
9988 phba->work_status[0],
9989 phba->work_status[1]);
9990 rc = -ENODEV;
9991 goto out;
9992 }
9993
9994 if (!port_reset) {
9995 /*
9996 * Reset the port now
9997 */
James Smart2fcee4b2010-12-15 17:57:46 -05009998 reg_data.word0 = 0;
9999 bf_set(lpfc_sliport_ctrl_end, &reg_data,
10000 LPFC_SLIPORT_LITTLE_ENDIAN);
10001 bf_set(lpfc_sliport_ctrl_ip, &reg_data,
10002 LPFC_SLIPORT_INIT_PORT);
10003 writel(reg_data.word0, phba->sli4_hba.u.if_type2.
10004 CTRLregaddr);
James Smart8fcb8ac2012-03-01 22:35:58 -050010005 /* flush */
James Smart2b81f942012-03-01 22:37:18 -050010006 pci_read_config_word(phba->pcidev,
10007 PCI_DEVICE_ID, &devid);
James Smart2fcee4b2010-12-15 17:57:46 -050010008
James Smart2f6fa2c2014-09-03 12:57:08 -040010009 port_reset = 1;
10010 msleep(20);
10011 goto wait;
10012 } else if (bf_get(lpfc_sliport_status_rn, &reg_data)) {
10013 rc = -ENODEV;
10014 goto out;
James Smart2fcee4b2010-12-15 17:57:46 -050010015 }
10016 break;
James Smart2f6fa2c2014-09-03 12:57:08 -040010017
James Smart2fcee4b2010-12-15 17:57:46 -050010018 case LPFC_SLI_INTF_IF_TYPE_1:
10019 default:
10020 break;
James Smartda0436e2009-05-22 14:51:39 -040010021 }
10022
James Smart73d91e52011-10-10 21:32:10 -040010023out:
James Smart2fcee4b2010-12-15 17:57:46 -050010024 /* Catch the not-ready port failure after a port reset. */
James Smart2f6fa2c2014-09-03 12:57:08 -040010025 if (rc) {
James Smart229adb02013-04-17 20:16:51 -040010026 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10027 "3317 HBA not functional: IP Reset Failed "
James Smart2f6fa2c2014-09-03 12:57:08 -040010028 "try: echo fw_reset > board_mode\n");
James Smart2fcee4b2010-12-15 17:57:46 -050010029 rc = -ENODEV;
James Smart229adb02013-04-17 20:16:51 -040010030 }
James Smart2fcee4b2010-12-15 17:57:46 -050010031
James Smartda0436e2009-05-22 14:51:39 -040010032 return rc;
10033}
10034
10035/**
James Smartda0436e2009-05-22 14:51:39 -040010036 * lpfc_sli4_pci_mem_setup - Setup SLI4 HBA PCI memory space.
10037 * @phba: pointer to lpfc hba data structure.
10038 *
10039 * This routine is invoked to set up the PCI device memory space for device
10040 * with SLI-4 interface spec.
10041 *
10042 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020010043 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -040010044 * other values - error
10045 **/
10046static int
10047lpfc_sli4_pci_mem_setup(struct lpfc_hba *phba)
10048{
Christoph Hellwigf30e1bf2018-10-18 15:10:21 +020010049 struct pci_dev *pdev = phba->pcidev;
James Smartda0436e2009-05-22 14:51:39 -040010050 unsigned long bar0map_len, bar1map_len, bar2map_len;
10051 int error = -ENODEV;
James Smart2fcee4b2010-12-15 17:57:46 -050010052 uint32_t if_type;
James Smartda0436e2009-05-22 14:51:39 -040010053
Christoph Hellwigf30e1bf2018-10-18 15:10:21 +020010054 if (!pdev)
James Smartda0436e2009-05-22 14:51:39 -040010055 return error;
James Smartda0436e2009-05-22 14:51:39 -040010056
10057 /* Set the device DMA mask size */
Christoph Hellwigf30e1bf2018-10-18 15:10:21 +020010058 if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) ||
10059 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)))
10060 return error;
James Smartda0436e2009-05-22 14:51:39 -040010061
James Smart2fcee4b2010-12-15 17:57:46 -050010062 /*
10063 * The BARs and register set definitions and offset locations are
10064 * dependent on the if_type.
10065 */
10066 if (pci_read_config_dword(pdev, LPFC_SLI_INTF,
10067 &phba->sli4_hba.sli_intf.word0)) {
10068 return error;
10069 }
10070
10071 /* There is no SLI3 failback for SLI4 devices. */
10072 if (bf_get(lpfc_sli_intf_valid, &phba->sli4_hba.sli_intf) !=
10073 LPFC_SLI_INTF_VALID) {
10074 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10075 "2894 SLI_INTF reg contents invalid "
10076 "sli_intf reg 0x%x\n",
10077 phba->sli4_hba.sli_intf.word0);
10078 return error;
10079 }
10080
10081 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
10082 /*
10083 * Get the bus address of SLI4 device Bar regions and the
10084 * number of bytes required by each mapping. The mapping of the
10085 * particular PCI BARs regions is dependent on the type of
10086 * SLI4 device.
James Smartda0436e2009-05-22 14:51:39 -040010087 */
James Smartf5ca6f22013-09-06 12:21:09 -040010088 if (pci_resource_start(pdev, PCI_64BIT_BAR0)) {
10089 phba->pci_bar0_map = pci_resource_start(pdev, PCI_64BIT_BAR0);
10090 bar0map_len = pci_resource_len(pdev, PCI_64BIT_BAR0);
James Smart2fcee4b2010-12-15 17:57:46 -050010091
10092 /*
10093 * Map SLI4 PCI Config Space Register base to a kernel virtual
10094 * addr
10095 */
10096 phba->sli4_hba.conf_regs_memmap_p =
10097 ioremap(phba->pci_bar0_map, bar0map_len);
10098 if (!phba->sli4_hba.conf_regs_memmap_p) {
10099 dev_printk(KERN_ERR, &pdev->dev,
10100 "ioremap failed for SLI4 PCI config "
10101 "registers.\n");
10102 goto out;
10103 }
James Smartf5ca6f22013-09-06 12:21:09 -040010104 phba->pci_bar0_memmap_p = phba->sli4_hba.conf_regs_memmap_p;
James Smart2fcee4b2010-12-15 17:57:46 -050010105 /* Set up BAR0 PCI config space register memory map */
10106 lpfc_sli4_bar0_register_memmap(phba, if_type);
James Smart1dfb5a42010-02-12 14:40:50 -050010107 } else {
10108 phba->pci_bar0_map = pci_resource_start(pdev, 1);
10109 bar0map_len = pci_resource_len(pdev, 1);
James Smart27d6ac02018-02-22 08:18:42 -080010110 if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
James Smart2fcee4b2010-12-15 17:57:46 -050010111 dev_printk(KERN_ERR, &pdev->dev,
10112 "FATAL - No BAR0 mapping for SLI4, if_type 2\n");
10113 goto out;
10114 }
10115 phba->sli4_hba.conf_regs_memmap_p =
James Smartda0436e2009-05-22 14:51:39 -040010116 ioremap(phba->pci_bar0_map, bar0map_len);
James Smart2fcee4b2010-12-15 17:57:46 -050010117 if (!phba->sli4_hba.conf_regs_memmap_p) {
10118 dev_printk(KERN_ERR, &pdev->dev,
10119 "ioremap failed for SLI4 PCI config "
10120 "registers.\n");
10121 goto out;
10122 }
10123 lpfc_sli4_bar0_register_memmap(phba, if_type);
James Smartda0436e2009-05-22 14:51:39 -040010124 }
10125
James Smarte4b97942017-11-20 16:00:31 -080010126 if (if_type == LPFC_SLI_INTF_IF_TYPE_0) {
10127 if (pci_resource_start(pdev, PCI_64BIT_BAR2)) {
10128 /*
10129 * Map SLI4 if type 0 HBA Control Register base to a
10130 * kernel virtual address and setup the registers.
10131 */
10132 phba->pci_bar1_map = pci_resource_start(pdev,
10133 PCI_64BIT_BAR2);
10134 bar1map_len = pci_resource_len(pdev, PCI_64BIT_BAR2);
10135 phba->sli4_hba.ctrl_regs_memmap_p =
10136 ioremap(phba->pci_bar1_map,
10137 bar1map_len);
10138 if (!phba->sli4_hba.ctrl_regs_memmap_p) {
10139 dev_err(&pdev->dev,
10140 "ioremap failed for SLI4 HBA "
10141 "control registers.\n");
10142 error = -ENOMEM;
10143 goto out_iounmap_conf;
10144 }
10145 phba->pci_bar2_memmap_p =
10146 phba->sli4_hba.ctrl_regs_memmap_p;
James Smart27d6ac02018-02-22 08:18:42 -080010147 lpfc_sli4_bar1_register_memmap(phba, if_type);
James Smarte4b97942017-11-20 16:00:31 -080010148 } else {
10149 error = -ENOMEM;
James Smart2fcee4b2010-12-15 17:57:46 -050010150 goto out_iounmap_conf;
10151 }
James Smartda0436e2009-05-22 14:51:39 -040010152 }
10153
James Smart27d6ac02018-02-22 08:18:42 -080010154 if ((if_type == LPFC_SLI_INTF_IF_TYPE_6) &&
10155 (pci_resource_start(pdev, PCI_64BIT_BAR2))) {
10156 /*
10157 * Map SLI4 if type 6 HBA Doorbell Register base to a kernel
10158 * virtual address and setup the registers.
10159 */
10160 phba->pci_bar1_map = pci_resource_start(pdev, PCI_64BIT_BAR2);
10161 bar1map_len = pci_resource_len(pdev, PCI_64BIT_BAR2);
10162 phba->sli4_hba.drbl_regs_memmap_p =
10163 ioremap(phba->pci_bar1_map, bar1map_len);
10164 if (!phba->sli4_hba.drbl_regs_memmap_p) {
10165 dev_err(&pdev->dev,
10166 "ioremap failed for SLI4 HBA doorbell registers.\n");
10167 goto out_iounmap_conf;
10168 }
10169 phba->pci_bar2_memmap_p = phba->sli4_hba.drbl_regs_memmap_p;
10170 lpfc_sli4_bar1_register_memmap(phba, if_type);
10171 }
10172
James Smarte4b97942017-11-20 16:00:31 -080010173 if (if_type == LPFC_SLI_INTF_IF_TYPE_0) {
10174 if (pci_resource_start(pdev, PCI_64BIT_BAR4)) {
10175 /*
10176 * Map SLI4 if type 0 HBA Doorbell Register base to
10177 * a kernel virtual address and setup the registers.
10178 */
10179 phba->pci_bar2_map = pci_resource_start(pdev,
10180 PCI_64BIT_BAR4);
10181 bar2map_len = pci_resource_len(pdev, PCI_64BIT_BAR4);
10182 phba->sli4_hba.drbl_regs_memmap_p =
10183 ioremap(phba->pci_bar2_map,
10184 bar2map_len);
10185 if (!phba->sli4_hba.drbl_regs_memmap_p) {
10186 dev_err(&pdev->dev,
10187 "ioremap failed for SLI4 HBA"
10188 " doorbell registers.\n");
10189 error = -ENOMEM;
10190 goto out_iounmap_ctrl;
10191 }
10192 phba->pci_bar4_memmap_p =
10193 phba->sli4_hba.drbl_regs_memmap_p;
10194 error = lpfc_sli4_bar2_register_memmap(phba, LPFC_VF0);
10195 if (error)
10196 goto out_iounmap_all;
10197 } else {
10198 error = -ENOMEM;
James Smart2fcee4b2010-12-15 17:57:46 -050010199 goto out_iounmap_all;
James Smarte4b97942017-11-20 16:00:31 -080010200 }
James Smartda0436e2009-05-22 14:51:39 -040010201 }
10202
James Smart1351e692018-02-22 08:18:43 -080010203 if (if_type == LPFC_SLI_INTF_IF_TYPE_6 &&
10204 pci_resource_start(pdev, PCI_64BIT_BAR4)) {
10205 /*
10206 * Map SLI4 if type 6 HBA DPP Register base to a kernel
10207 * virtual address and setup the registers.
10208 */
10209 phba->pci_bar2_map = pci_resource_start(pdev, PCI_64BIT_BAR4);
10210 bar2map_len = pci_resource_len(pdev, PCI_64BIT_BAR4);
10211 phba->sli4_hba.dpp_regs_memmap_p =
10212 ioremap(phba->pci_bar2_map, bar2map_len);
10213 if (!phba->sli4_hba.dpp_regs_memmap_p) {
10214 dev_err(&pdev->dev,
10215 "ioremap failed for SLI4 HBA dpp registers.\n");
10216 goto out_iounmap_ctrl;
10217 }
10218 phba->pci_bar4_memmap_p = phba->sli4_hba.dpp_regs_memmap_p;
10219 }
10220
James Smartb71413d2018-02-22 08:18:40 -080010221 /* Set up the EQ/CQ register handeling functions now */
James Smart27d6ac02018-02-22 08:18:42 -080010222 switch (if_type) {
10223 case LPFC_SLI_INTF_IF_TYPE_0:
10224 case LPFC_SLI_INTF_IF_TYPE_2:
James Smartb71413d2018-02-22 08:18:40 -080010225 phba->sli4_hba.sli4_eq_clr_intr = lpfc_sli4_eq_clr_intr;
James Smart32517fc2019-01-28 11:14:33 -080010226 phba->sli4_hba.sli4_write_eq_db = lpfc_sli4_write_eq_db;
10227 phba->sli4_hba.sli4_write_cq_db = lpfc_sli4_write_cq_db;
James Smart27d6ac02018-02-22 08:18:42 -080010228 break;
10229 case LPFC_SLI_INTF_IF_TYPE_6:
10230 phba->sli4_hba.sli4_eq_clr_intr = lpfc_sli4_if6_eq_clr_intr;
James Smart32517fc2019-01-28 11:14:33 -080010231 phba->sli4_hba.sli4_write_eq_db = lpfc_sli4_if6_write_eq_db;
10232 phba->sli4_hba.sli4_write_cq_db = lpfc_sli4_if6_write_cq_db;
James Smart27d6ac02018-02-22 08:18:42 -080010233 break;
10234 default:
10235 break;
James Smartb71413d2018-02-22 08:18:40 -080010236 }
10237
James Smartda0436e2009-05-22 14:51:39 -040010238 return 0;
10239
10240out_iounmap_all:
10241 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
10242out_iounmap_ctrl:
10243 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
10244out_iounmap_conf:
10245 iounmap(phba->sli4_hba.conf_regs_memmap_p);
10246out:
10247 return error;
10248}
10249
10250/**
10251 * lpfc_sli4_pci_mem_unset - Unset SLI4 HBA PCI memory space.
10252 * @phba: pointer to lpfc hba data structure.
10253 *
10254 * This routine is invoked to unset the PCI device memory space for device
10255 * with SLI-4 interface spec.
10256 **/
10257static void
10258lpfc_sli4_pci_mem_unset(struct lpfc_hba *phba)
10259{
James Smart2e90f4b2011-12-13 13:22:37 -050010260 uint32_t if_type;
10261 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
James Smartda0436e2009-05-22 14:51:39 -040010262
James Smart2e90f4b2011-12-13 13:22:37 -050010263 switch (if_type) {
10264 case LPFC_SLI_INTF_IF_TYPE_0:
10265 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
10266 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
10267 iounmap(phba->sli4_hba.conf_regs_memmap_p);
10268 break;
10269 case LPFC_SLI_INTF_IF_TYPE_2:
10270 iounmap(phba->sli4_hba.conf_regs_memmap_p);
10271 break;
James Smart27d6ac02018-02-22 08:18:42 -080010272 case LPFC_SLI_INTF_IF_TYPE_6:
10273 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
10274 iounmap(phba->sli4_hba.conf_regs_memmap_p);
10275 break;
James Smart2e90f4b2011-12-13 13:22:37 -050010276 case LPFC_SLI_INTF_IF_TYPE_1:
10277 default:
10278 dev_printk(KERN_ERR, &phba->pcidev->dev,
10279 "FATAL - unsupported SLI4 interface type - %d\n",
10280 if_type);
10281 break;
10282 }
James Smartda0436e2009-05-22 14:51:39 -040010283}
10284
10285/**
James Smart3772a992009-05-22 14:50:54 -040010286 * lpfc_sli_enable_msix - Enable MSI-X interrupt mode on SLI-3 device
10287 * @phba: pointer to lpfc hba data structure.
10288 *
10289 * This routine is invoked to enable the MSI-X interrupt vectors to device
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010290 * with SLI-3 interface specs.
James Smart3772a992009-05-22 14:50:54 -040010291 *
10292 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020010293 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -040010294 * other values - error
10295 **/
10296static int
10297lpfc_sli_enable_msix(struct lpfc_hba *phba)
10298{
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010299 int rc;
James Smart3772a992009-05-22 14:50:54 -040010300 LPFC_MBOXQ_t *pmb;
10301
10302 /* Set up MSI-X multi-message vectors */
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010303 rc = pci_alloc_irq_vectors(phba->pcidev,
10304 LPFC_MSIX_VECTORS, LPFC_MSIX_VECTORS, PCI_IRQ_MSIX);
10305 if (rc < 0) {
James Smart3772a992009-05-22 14:50:54 -040010306 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10307 "0420 PCI enable MSI-X failed (%d)\n", rc);
Alexander Gordeev029165a2014-07-16 20:05:15 +020010308 goto vec_fail_out;
James Smart3772a992009-05-22 14:50:54 -040010309 }
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010310
James Smart3772a992009-05-22 14:50:54 -040010311 /*
10312 * Assign MSI-X vectors to interrupt handlers
10313 */
10314
10315 /* vector-0 is associated to slow-path handler */
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010316 rc = request_irq(pci_irq_vector(phba->pcidev, 0),
James Smarted243d32015-05-21 13:55:25 -040010317 &lpfc_sli_sp_intr_handler, 0,
James Smart3772a992009-05-22 14:50:54 -040010318 LPFC_SP_DRIVER_HANDLER_NAME, phba);
10319 if (rc) {
10320 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
10321 "0421 MSI-X slow-path request_irq failed "
10322 "(%d)\n", rc);
10323 goto msi_fail_out;
10324 }
10325
10326 /* vector-1 is associated to fast-path handler */
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010327 rc = request_irq(pci_irq_vector(phba->pcidev, 1),
James Smarted243d32015-05-21 13:55:25 -040010328 &lpfc_sli_fp_intr_handler, 0,
James Smart3772a992009-05-22 14:50:54 -040010329 LPFC_FP_DRIVER_HANDLER_NAME, phba);
10330
10331 if (rc) {
10332 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
10333 "0429 MSI-X fast-path request_irq failed "
10334 "(%d)\n", rc);
10335 goto irq_fail_out;
10336 }
10337
10338 /*
10339 * Configure HBA MSI-X attention conditions to messages
10340 */
10341 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10342
10343 if (!pmb) {
10344 rc = -ENOMEM;
10345 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10346 "0474 Unable to allocate memory for issuing "
10347 "MBOX_CONFIG_MSI command\n");
10348 goto mem_fail_out;
10349 }
10350 rc = lpfc_config_msi(phba, pmb);
10351 if (rc)
10352 goto mbx_fail_out;
10353 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
10354 if (rc != MBX_SUCCESS) {
10355 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
10356 "0351 Config MSI mailbox command failed, "
10357 "mbxCmd x%x, mbxStatus x%x\n",
10358 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus);
10359 goto mbx_fail_out;
10360 }
10361
10362 /* Free memory allocated for mailbox command */
10363 mempool_free(pmb, phba->mbox_mem_pool);
10364 return rc;
10365
10366mbx_fail_out:
10367 /* Free memory allocated for mailbox command */
10368 mempool_free(pmb, phba->mbox_mem_pool);
10369
10370mem_fail_out:
10371 /* free the irq already requested */
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010372 free_irq(pci_irq_vector(phba->pcidev, 1), phba);
James Smart3772a992009-05-22 14:50:54 -040010373
10374irq_fail_out:
10375 /* free the irq already requested */
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010376 free_irq(pci_irq_vector(phba->pcidev, 0), phba);
James Smart3772a992009-05-22 14:50:54 -040010377
10378msi_fail_out:
10379 /* Unconfigure MSI-X capability structure */
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010380 pci_free_irq_vectors(phba->pcidev);
Alexander Gordeev029165a2014-07-16 20:05:15 +020010381
10382vec_fail_out:
James Smart3772a992009-05-22 14:50:54 -040010383 return rc;
10384}
10385
10386/**
James Smart3772a992009-05-22 14:50:54 -040010387 * lpfc_sli_enable_msi - Enable MSI interrupt mode on SLI-3 device.
10388 * @phba: pointer to lpfc hba data structure.
10389 *
10390 * This routine is invoked to enable the MSI interrupt mode to device with
10391 * SLI-3 interface spec. The kernel function pci_enable_msi() is called to
10392 * enable the MSI vector. The device driver is responsible for calling the
10393 * request_irq() to register MSI vector with a interrupt the handler, which
10394 * is done in this function.
10395 *
10396 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020010397 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -040010398 * other values - error
10399 */
10400static int
10401lpfc_sli_enable_msi(struct lpfc_hba *phba)
10402{
10403 int rc;
10404
10405 rc = pci_enable_msi(phba->pcidev);
10406 if (!rc)
10407 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10408 "0462 PCI enable MSI mode success.\n");
10409 else {
10410 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10411 "0471 PCI enable MSI mode failed (%d)\n", rc);
10412 return rc;
10413 }
10414
10415 rc = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
James Smarted243d32015-05-21 13:55:25 -040010416 0, LPFC_DRIVER_NAME, phba);
James Smart3772a992009-05-22 14:50:54 -040010417 if (rc) {
10418 pci_disable_msi(phba->pcidev);
10419 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
10420 "0478 MSI request_irq failed (%d)\n", rc);
10421 }
10422 return rc;
10423}
10424
10425/**
James Smart3772a992009-05-22 14:50:54 -040010426 * lpfc_sli_enable_intr - Enable device interrupt to SLI-3 device.
10427 * @phba: pointer to lpfc hba data structure.
10428 *
10429 * This routine is invoked to enable device interrupt and associate driver's
10430 * interrupt handler(s) to interrupt vector(s) to device with SLI-3 interface
10431 * spec. Depends on the interrupt mode configured to the driver, the driver
10432 * will try to fallback from the configured interrupt mode to an interrupt
10433 * mode which is supported by the platform, kernel, and device in the order
10434 * of:
10435 * MSI-X -> MSI -> IRQ.
10436 *
10437 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020010438 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -040010439 * other values - error
10440 **/
10441static uint32_t
10442lpfc_sli_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
10443{
10444 uint32_t intr_mode = LPFC_INTR_ERROR;
10445 int retval;
10446
10447 if (cfg_mode == 2) {
10448 /* Need to issue conf_port mbox cmd before conf_msi mbox cmd */
10449 retval = lpfc_sli_config_port(phba, LPFC_SLI_REV3);
10450 if (!retval) {
10451 /* Now, try to enable MSI-X interrupt mode */
10452 retval = lpfc_sli_enable_msix(phba);
10453 if (!retval) {
10454 /* Indicate initialization to MSI-X mode */
10455 phba->intr_type = MSIX;
10456 intr_mode = 2;
10457 }
10458 }
10459 }
10460
10461 /* Fallback to MSI if MSI-X initialization failed */
10462 if (cfg_mode >= 1 && phba->intr_type == NONE) {
10463 retval = lpfc_sli_enable_msi(phba);
10464 if (!retval) {
10465 /* Indicate initialization to MSI mode */
10466 phba->intr_type = MSI;
10467 intr_mode = 1;
10468 }
10469 }
10470
10471 /* Fallback to INTx if both MSI-X/MSI initalization failed */
10472 if (phba->intr_type == NONE) {
10473 retval = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
10474 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
10475 if (!retval) {
10476 /* Indicate initialization to INTx mode */
10477 phba->intr_type = INTx;
10478 intr_mode = 0;
10479 }
10480 }
10481 return intr_mode;
10482}
10483
10484/**
10485 * lpfc_sli_disable_intr - Disable device interrupt to SLI-3 device.
10486 * @phba: pointer to lpfc hba data structure.
10487 *
10488 * This routine is invoked to disable device interrupt and disassociate the
10489 * driver's interrupt handler(s) from interrupt vector(s) to device with
10490 * SLI-3 interface spec. Depending on the interrupt mode, the driver will
10491 * release the interrupt vector(s) for the message signaled interrupt.
10492 **/
10493static void
10494lpfc_sli_disable_intr(struct lpfc_hba *phba)
10495{
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010496 int nr_irqs, i;
10497
James Smart3772a992009-05-22 14:50:54 -040010498 if (phba->intr_type == MSIX)
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010499 nr_irqs = LPFC_MSIX_VECTORS;
10500 else
10501 nr_irqs = 1;
10502
10503 for (i = 0; i < nr_irqs; i++)
10504 free_irq(pci_irq_vector(phba->pcidev, i), phba);
10505 pci_free_irq_vectors(phba->pcidev);
James Smart3772a992009-05-22 14:50:54 -040010506
10507 /* Reset interrupt management states */
10508 phba->intr_type = NONE;
10509 phba->sli.slistat.sli_intr = 0;
James Smart3772a992009-05-22 14:50:54 -040010510}
10511
10512/**
James Smart6a828b02019-01-28 11:14:31 -080010513 * lpfc_find_cpu_handle - Find the CPU that corresponds to the specified EQ
10514 * @phba: pointer to lpfc hba data structure.
10515 * @id: EQ vector index or Hardware Queue index
10516 * @match: LPFC_FIND_BY_EQ = match by EQ
10517 * LPFC_FIND_BY_HDWQ = match by Hardware Queue
10518 */
10519static uint16_t
10520lpfc_find_cpu_handle(struct lpfc_hba *phba, uint16_t id, int match)
10521{
10522 struct lpfc_vector_map_info *cpup;
10523 int cpu;
10524
10525 /* Find the desired phys_id for the specified EQ */
James Smart222e9232019-01-28 11:14:35 -080010526 for_each_present_cpu(cpu) {
10527 cpup = &phba->sli4_hba.cpu_map[cpu];
James Smart6a828b02019-01-28 11:14:31 -080010528 if ((match == LPFC_FIND_BY_EQ) &&
10529 (cpup->irq != LPFC_VECTOR_MAP_EMPTY) &&
10530 (cpup->eq == id))
10531 return cpu;
10532 if ((match == LPFC_FIND_BY_HDWQ) && (cpup->hdwq == id))
10533 return cpu;
James Smart6a828b02019-01-28 11:14:31 -080010534 }
10535 return 0;
10536}
10537
10538/**
10539 * lpfc_find_eq_handle - Find the EQ that corresponds to the specified
10540 * Hardware Queue
10541 * @phba: pointer to lpfc hba data structure.
10542 * @hdwq: Hardware Queue index
10543 */
10544static uint16_t
10545lpfc_find_eq_handle(struct lpfc_hba *phba, uint16_t hdwq)
10546{
10547 struct lpfc_vector_map_info *cpup;
10548 int cpu;
10549
10550 /* Find the desired phys_id for the specified EQ */
James Smart222e9232019-01-28 11:14:35 -080010551 for_each_present_cpu(cpu) {
10552 cpup = &phba->sli4_hba.cpu_map[cpu];
James Smart6a828b02019-01-28 11:14:31 -080010553 if (cpup->hdwq == hdwq)
10554 return cpup->eq;
James Smart6a828b02019-01-28 11:14:31 -080010555 }
10556 return 0;
10557}
10558
James Smart6a828b02019-01-28 11:14:31 -080010559#ifdef CONFIG_X86
10560/**
10561 * lpfc_find_hyper - Determine if the CPU map entry is hyper-threaded
10562 * @phba: pointer to lpfc hba data structure.
10563 * @cpu: CPU map index
10564 * @phys_id: CPU package physical id
10565 * @core_id: CPU core id
10566 */
10567static int
10568lpfc_find_hyper(struct lpfc_hba *phba, int cpu,
10569 uint16_t phys_id, uint16_t core_id)
10570{
10571 struct lpfc_vector_map_info *cpup;
10572 int idx;
10573
James Smart222e9232019-01-28 11:14:35 -080010574 for_each_present_cpu(idx) {
10575 cpup = &phba->sli4_hba.cpu_map[idx];
James Smart6a828b02019-01-28 11:14:31 -080010576 /* Does the cpup match the one we are looking for */
10577 if ((cpup->phys_id == phys_id) &&
10578 (cpup->core_id == core_id) &&
James Smart222e9232019-01-28 11:14:35 -080010579 (cpu != idx))
James Smart6a828b02019-01-28 11:14:31 -080010580 return 1;
James Smart6a828b02019-01-28 11:14:31 -080010581 }
10582 return 0;
10583}
10584#endif
10585
10586/**
James Smart895427b2017-02-12 13:52:30 -080010587 * lpfc_cpu_affinity_check - Check vector CPU affinity mappings
James Smart7bb03bb2013-04-17 20:19:16 -040010588 * @phba: pointer to lpfc hba data structure.
James Smart6a828b02019-01-28 11:14:31 -080010589 * @vectors: number of msix vectors allocated.
James Smart7bb03bb2013-04-17 20:19:16 -040010590 *
James Smart895427b2017-02-12 13:52:30 -080010591 * The routine will figure out the CPU affinity assignment for every
James Smart6a828b02019-01-28 11:14:31 -080010592 * MSI-X vector allocated for the HBA.
James Smart895427b2017-02-12 13:52:30 -080010593 * In addition, the CPU to IO channel mapping will be calculated
10594 * and the phba->sli4_hba.cpu_map array will reflect this.
James Smart7bb03bb2013-04-17 20:19:16 -040010595 */
James Smart895427b2017-02-12 13:52:30 -080010596static void
James Smart6a828b02019-01-28 11:14:31 -080010597lpfc_cpu_affinity_check(struct lpfc_hba *phba, int vectors)
James Smart7bb03bb2013-04-17 20:19:16 -040010598{
James Smart75508a82019-01-28 11:14:34 -080010599 int i, cpu, idx, phys_id;
James Smart6a828b02019-01-28 11:14:31 -080010600 int max_phys_id, min_phys_id;
10601 int max_core_id, min_core_id;
James Smart7bb03bb2013-04-17 20:19:16 -040010602 struct lpfc_vector_map_info *cpup;
James Smart75508a82019-01-28 11:14:34 -080010603 const struct cpumask *maskp;
James Smart7bb03bb2013-04-17 20:19:16 -040010604#ifdef CONFIG_X86
10605 struct cpuinfo_x86 *cpuinfo;
10606#endif
James Smart7bb03bb2013-04-17 20:19:16 -040010607
10608 /* Init cpu_map array */
10609 memset(phba->sli4_hba.cpu_map, 0xff,
10610 (sizeof(struct lpfc_vector_map_info) *
James Smart222e9232019-01-28 11:14:35 -080010611 phba->sli4_hba.num_possible_cpu));
James Smart7bb03bb2013-04-17 20:19:16 -040010612
James Smart6a828b02019-01-28 11:14:31 -080010613 max_phys_id = 0;
10614 min_phys_id = 0xffff;
10615 max_core_id = 0;
10616 min_core_id = 0xffff;
10617 phys_id = 0;
10618
James Smart7bb03bb2013-04-17 20:19:16 -040010619 /* Update CPU map with physical id and core id of each CPU */
James Smart222e9232019-01-28 11:14:35 -080010620 for_each_present_cpu(cpu) {
10621 cpup = &phba->sli4_hba.cpu_map[cpu];
James Smart7bb03bb2013-04-17 20:19:16 -040010622#ifdef CONFIG_X86
10623 cpuinfo = &cpu_data(cpu);
10624 cpup->phys_id = cpuinfo->phys_proc_id;
10625 cpup->core_id = cpuinfo->cpu_core_id;
James Smart6a828b02019-01-28 11:14:31 -080010626 cpup->hyper = lpfc_find_hyper(phba, cpu,
10627 cpup->phys_id, cpup->core_id);
James Smart7bb03bb2013-04-17 20:19:16 -040010628#else
10629 /* No distinction between CPUs for other platforms */
10630 cpup->phys_id = 0;
James Smart6a828b02019-01-28 11:14:31 -080010631 cpup->core_id = cpu;
10632 cpup->hyper = 0;
James Smart7bb03bb2013-04-17 20:19:16 -040010633#endif
James Smart6a828b02019-01-28 11:14:31 -080010634
James Smartb3295c22019-01-28 11:14:30 -080010635 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10636 "3328 CPU physid %d coreid %d\n",
10637 cpup->phys_id, cpup->core_id);
James Smart6a828b02019-01-28 11:14:31 -080010638
10639 if (cpup->phys_id > max_phys_id)
10640 max_phys_id = cpup->phys_id;
10641 if (cpup->phys_id < min_phys_id)
10642 min_phys_id = cpup->phys_id;
10643
10644 if (cpup->core_id > max_core_id)
10645 max_core_id = cpup->core_id;
10646 if (cpup->core_id < min_core_id)
10647 min_core_id = cpup->core_id;
James Smart7bb03bb2013-04-17 20:19:16 -040010648 }
James Smartb3295c22019-01-28 11:14:30 -080010649
James Smart32517fc2019-01-28 11:14:33 -080010650 for_each_possible_cpu(i) {
10651 struct lpfc_eq_intr_info *eqi =
10652 per_cpu_ptr(phba->sli4_hba.eq_info, i);
10653
10654 INIT_LIST_HEAD(&eqi->list);
10655 eqi->icnt = 0;
10656 }
10657
James Smart75508a82019-01-28 11:14:34 -080010658 for (idx = 0; idx < phba->cfg_irq_chann; idx++) {
10659 maskp = pci_irq_get_affinity(phba->pcidev, idx);
10660 if (!maskp)
10661 continue;
10662
10663 for_each_cpu_and(cpu, maskp, cpu_present_mask) {
10664 cpup = &phba->sli4_hba.cpu_map[cpu];
James Smart6a828b02019-01-28 11:14:31 -080010665 cpup->eq = idx;
10666 cpup->hdwq = idx;
10667 cpup->irq = pci_irq_vector(phba->pcidev, idx);
James Smartb3295c22019-01-28 11:14:30 -080010668
James Smart75508a82019-01-28 11:14:34 -080010669 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart6a828b02019-01-28 11:14:31 -080010670 "3336 Set Affinity: CPU %d "
James Smart75508a82019-01-28 11:14:34 -080010671 "hdwq %d irq %d\n",
10672 cpu, cpup->hdwq, cpup->irq);
James Smart6a828b02019-01-28 11:14:31 -080010673 }
James Smartb3295c22019-01-28 11:14:30 -080010674 }
10675 return;
James Smart7bb03bb2013-04-17 20:19:16 -040010676}
10677
James Smart7bb03bb2013-04-17 20:19:16 -040010678/**
James Smartda0436e2009-05-22 14:51:39 -040010679 * lpfc_sli4_enable_msix - Enable MSI-X interrupt mode to SLI-4 device
10680 * @phba: pointer to lpfc hba data structure.
10681 *
10682 * This routine is invoked to enable the MSI-X interrupt vectors to device
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010683 * with SLI-4 interface spec.
James Smartda0436e2009-05-22 14:51:39 -040010684 *
10685 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020010686 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -040010687 * other values - error
10688 **/
10689static int
10690lpfc_sli4_enable_msix(struct lpfc_hba *phba)
10691{
James Smart75baf692010-06-08 18:31:21 -040010692 int vectors, rc, index;
James Smartb83d0052017-06-01 21:07:05 -070010693 char *name;
James Smartda0436e2009-05-22 14:51:39 -040010694
10695 /* Set up MSI-X multi-message vectors */
James Smart6a828b02019-01-28 11:14:31 -080010696 vectors = phba->cfg_irq_chann;
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010697
James Smartf358dd02017-02-12 13:52:34 -080010698 rc = pci_alloc_irq_vectors(phba->pcidev,
James Smart75508a82019-01-28 11:14:34 -080010699 1,
James Smartf358dd02017-02-12 13:52:34 -080010700 vectors, PCI_IRQ_MSIX | PCI_IRQ_AFFINITY);
Alexander Gordeev4f871e12014-09-03 12:56:29 -040010701 if (rc < 0) {
James Smartda0436e2009-05-22 14:51:39 -040010702 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10703 "0484 PCI enable MSI-X failed (%d)\n", rc);
Alexander Gordeev029165a2014-07-16 20:05:15 +020010704 goto vec_fail_out;
James Smartda0436e2009-05-22 14:51:39 -040010705 }
Alexander Gordeev4f871e12014-09-03 12:56:29 -040010706 vectors = rc;
James Smart75baf692010-06-08 18:31:21 -040010707
James Smart7bb03bb2013-04-17 20:19:16 -040010708 /* Assign MSI-X vectors to interrupt handlers */
James Smart67d12732012-08-03 12:36:13 -040010709 for (index = 0; index < vectors; index++) {
James Smartb83d0052017-06-01 21:07:05 -070010710 name = phba->sli4_hba.hba_eq_hdl[index].handler_name;
10711 memset(name, 0, LPFC_SLI4_HANDLER_NAME_SZ);
10712 snprintf(name, LPFC_SLI4_HANDLER_NAME_SZ,
James Smart4305f182012-08-03 12:36:33 -040010713 LPFC_DRIVER_HANDLER_NAME"%d", index);
James Smartda0436e2009-05-22 14:51:39 -040010714
James Smart895427b2017-02-12 13:52:30 -080010715 phba->sli4_hba.hba_eq_hdl[index].idx = index;
10716 phba->sli4_hba.hba_eq_hdl[index].phba = phba;
James Smart7370d102019-01-28 11:14:20 -080010717 rc = request_irq(pci_irq_vector(phba->pcidev, index),
10718 &lpfc_sli4_hba_intr_handler, 0,
10719 name,
10720 &phba->sli4_hba.hba_eq_hdl[index]);
James Smartda0436e2009-05-22 14:51:39 -040010721 if (rc) {
10722 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
10723 "0486 MSI-X fast-path (%d) "
10724 "request_irq failed (%d)\n", index, rc);
10725 goto cfg_fail_out;
10726 }
10727 }
10728
James Smart6a828b02019-01-28 11:14:31 -080010729 if (vectors != phba->cfg_irq_chann) {
James Smart82c3e9b2012-09-29 11:29:50 -040010730 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10731 "3238 Reducing IO channels to match number of "
10732 "MSI-X vectors, requested %d got %d\n",
James Smart6a828b02019-01-28 11:14:31 -080010733 phba->cfg_irq_chann, vectors);
10734 if (phba->cfg_irq_chann > vectors)
10735 phba->cfg_irq_chann = vectors;
James Smartcdb42be2019-01-28 11:14:21 -080010736 if (phba->cfg_nvmet_mrq > vectors)
10737 phba->cfg_nvmet_mrq = vectors;
James Smart82c3e9b2012-09-29 11:29:50 -040010738 }
James Smart7bb03bb2013-04-17 20:19:16 -040010739
James Smartda0436e2009-05-22 14:51:39 -040010740 return rc;
10741
10742cfg_fail_out:
10743 /* free the irq already requested */
James Smart895427b2017-02-12 13:52:30 -080010744 for (--index; index >= 0; index--)
10745 free_irq(pci_irq_vector(phba->pcidev, index),
10746 &phba->sli4_hba.hba_eq_hdl[index]);
James Smartda0436e2009-05-22 14:51:39 -040010747
James Smartda0436e2009-05-22 14:51:39 -040010748 /* Unconfigure MSI-X capability structure */
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010749 pci_free_irq_vectors(phba->pcidev);
Alexander Gordeev029165a2014-07-16 20:05:15 +020010750
10751vec_fail_out:
James Smartda0436e2009-05-22 14:51:39 -040010752 return rc;
10753}
10754
10755/**
James Smartda0436e2009-05-22 14:51:39 -040010756 * lpfc_sli4_enable_msi - Enable MSI interrupt mode to SLI-4 device
10757 * @phba: pointer to lpfc hba data structure.
10758 *
10759 * This routine is invoked to enable the MSI interrupt mode to device with
10760 * SLI-4 interface spec. The kernel function pci_enable_msi() is called
10761 * to enable the MSI vector. The device driver is responsible for calling
10762 * the request_irq() to register MSI vector with a interrupt the handler,
10763 * which is done in this function.
10764 *
10765 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020010766 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -040010767 * other values - error
10768 **/
10769static int
10770lpfc_sli4_enable_msi(struct lpfc_hba *phba)
10771{
10772 int rc, index;
10773
10774 rc = pci_enable_msi(phba->pcidev);
10775 if (!rc)
10776 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10777 "0487 PCI enable MSI mode success.\n");
10778 else {
10779 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10780 "0488 PCI enable MSI mode failed (%d)\n", rc);
10781 return rc;
10782 }
10783
10784 rc = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
James Smarted243d32015-05-21 13:55:25 -040010785 0, LPFC_DRIVER_NAME, phba);
James Smartda0436e2009-05-22 14:51:39 -040010786 if (rc) {
10787 pci_disable_msi(phba->pcidev);
10788 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
10789 "0490 MSI request_irq failed (%d)\n", rc);
James Smart75baf692010-06-08 18:31:21 -040010790 return rc;
James Smartda0436e2009-05-22 14:51:39 -040010791 }
10792
James Smart6a828b02019-01-28 11:14:31 -080010793 for (index = 0; index < phba->cfg_irq_chann; index++) {
James Smart895427b2017-02-12 13:52:30 -080010794 phba->sli4_hba.hba_eq_hdl[index].idx = index;
10795 phba->sli4_hba.hba_eq_hdl[index].phba = phba;
James Smartda0436e2009-05-22 14:51:39 -040010796 }
10797
James Smart75baf692010-06-08 18:31:21 -040010798 return 0;
James Smartda0436e2009-05-22 14:51:39 -040010799}
10800
10801/**
James Smartda0436e2009-05-22 14:51:39 -040010802 * lpfc_sli4_enable_intr - Enable device interrupt to SLI-4 device
10803 * @phba: pointer to lpfc hba data structure.
10804 *
10805 * This routine is invoked to enable device interrupt and associate driver's
10806 * interrupt handler(s) to interrupt vector(s) to device with SLI-4
10807 * interface spec. Depends on the interrupt mode configured to the driver,
10808 * the driver will try to fallback from the configured interrupt mode to an
10809 * interrupt mode which is supported by the platform, kernel, and device in
10810 * the order of:
10811 * MSI-X -> MSI -> IRQ.
10812 *
10813 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020010814 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -040010815 * other values - error
10816 **/
10817static uint32_t
10818lpfc_sli4_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
10819{
10820 uint32_t intr_mode = LPFC_INTR_ERROR;
James Smart895427b2017-02-12 13:52:30 -080010821 int retval, idx;
James Smartda0436e2009-05-22 14:51:39 -040010822
10823 if (cfg_mode == 2) {
10824 /* Preparation before conf_msi mbox cmd */
10825 retval = 0;
10826 if (!retval) {
10827 /* Now, try to enable MSI-X interrupt mode */
10828 retval = lpfc_sli4_enable_msix(phba);
10829 if (!retval) {
10830 /* Indicate initialization to MSI-X mode */
10831 phba->intr_type = MSIX;
10832 intr_mode = 2;
10833 }
10834 }
10835 }
10836
10837 /* Fallback to MSI if MSI-X initialization failed */
10838 if (cfg_mode >= 1 && phba->intr_type == NONE) {
10839 retval = lpfc_sli4_enable_msi(phba);
10840 if (!retval) {
10841 /* Indicate initialization to MSI mode */
10842 phba->intr_type = MSI;
10843 intr_mode = 1;
10844 }
10845 }
10846
10847 /* Fallback to INTx if both MSI-X/MSI initalization failed */
10848 if (phba->intr_type == NONE) {
10849 retval = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
10850 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
10851 if (!retval) {
James Smart895427b2017-02-12 13:52:30 -080010852 struct lpfc_hba_eq_hdl *eqhdl;
10853
James Smartda0436e2009-05-22 14:51:39 -040010854 /* Indicate initialization to INTx mode */
10855 phba->intr_type = INTx;
10856 intr_mode = 0;
James Smart895427b2017-02-12 13:52:30 -080010857
James Smart6a828b02019-01-28 11:14:31 -080010858 for (idx = 0; idx < phba->cfg_irq_chann; idx++) {
James Smart895427b2017-02-12 13:52:30 -080010859 eqhdl = &phba->sli4_hba.hba_eq_hdl[idx];
10860 eqhdl->idx = idx;
10861 eqhdl->phba = phba;
James Smartda0436e2009-05-22 14:51:39 -040010862 }
10863 }
10864 }
10865 return intr_mode;
10866}
10867
10868/**
10869 * lpfc_sli4_disable_intr - Disable device interrupt to SLI-4 device
10870 * @phba: pointer to lpfc hba data structure.
10871 *
10872 * This routine is invoked to disable device interrupt and disassociate
10873 * the driver's interrupt handler(s) from interrupt vector(s) to device
10874 * with SLI-4 interface spec. Depending on the interrupt mode, the driver
10875 * will release the interrupt vector(s) for the message signaled interrupt.
10876 **/
10877static void
10878lpfc_sli4_disable_intr(struct lpfc_hba *phba)
10879{
10880 /* Disable the currently initialized interrupt mode */
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010881 if (phba->intr_type == MSIX) {
10882 int index;
10883
10884 /* Free up MSI-X multi-message vectors */
James Smart6a828b02019-01-28 11:14:31 -080010885 for (index = 0; index < phba->cfg_irq_chann; index++) {
James Smartb3295c22019-01-28 11:14:30 -080010886 irq_set_affinity_hint(
10887 pci_irq_vector(phba->pcidev, index),
10888 NULL);
James Smart895427b2017-02-12 13:52:30 -080010889 free_irq(pci_irq_vector(phba->pcidev, index),
10890 &phba->sli4_hba.hba_eq_hdl[index]);
James Smartb3295c22019-01-28 11:14:30 -080010891 }
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010892 } else {
James Smartda0436e2009-05-22 14:51:39 -040010893 free_irq(phba->pcidev->irq, phba);
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010894 }
10895
10896 pci_free_irq_vectors(phba->pcidev);
James Smartda0436e2009-05-22 14:51:39 -040010897
10898 /* Reset interrupt management states */
10899 phba->intr_type = NONE;
10900 phba->sli.slistat.sli_intr = 0;
James Smartda0436e2009-05-22 14:51:39 -040010901}
10902
10903/**
James Smart3772a992009-05-22 14:50:54 -040010904 * lpfc_unset_hba - Unset SLI3 hba device initialization
10905 * @phba: pointer to lpfc hba data structure.
10906 *
10907 * This routine is invoked to unset the HBA device initialization steps to
10908 * a device with SLI-3 interface spec.
10909 **/
10910static void
10911lpfc_unset_hba(struct lpfc_hba *phba)
10912{
10913 struct lpfc_vport *vport = phba->pport;
10914 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
10915
10916 spin_lock_irq(shost->host_lock);
10917 vport->load_flag |= FC_UNLOADING;
10918 spin_unlock_irq(shost->host_lock);
10919
James Smart72859902012-01-18 16:25:38 -050010920 kfree(phba->vpi_bmask);
10921 kfree(phba->vpi_ids);
10922
James Smart3772a992009-05-22 14:50:54 -040010923 lpfc_stop_hba_timers(phba);
10924
10925 phba->pport->work_port_events = 0;
10926
10927 lpfc_sli_hba_down(phba);
10928
10929 lpfc_sli_brdrestart(phba);
10930
10931 lpfc_sli_disable_intr(phba);
10932
10933 return;
10934}
10935
10936/**
James Smart5af5eee2010-10-22 11:06:38 -040010937 * lpfc_sli4_xri_exchange_busy_wait - Wait for device XRI exchange busy
10938 * @phba: Pointer to HBA context object.
10939 *
10940 * This function is called in the SLI4 code path to wait for completion
10941 * of device's XRIs exchange busy. It will check the XRI exchange busy
10942 * on outstanding FCP and ELS I/Os every 10ms for up to 10 seconds; after
10943 * that, it will check the XRI exchange busy on outstanding FCP and ELS
10944 * I/Os every 30 seconds, log error message, and wait forever. Only when
10945 * all XRI exchange busy complete, the driver unload shall proceed with
10946 * invoking the function reset ioctl mailbox command to the CNA and the
10947 * the rest of the driver unload resource release.
10948 **/
10949static void
10950lpfc_sli4_xri_exchange_busy_wait(struct lpfc_hba *phba)
10951{
James Smart5e5b5112019-01-28 11:14:22 -080010952 struct lpfc_sli4_hdw_queue *qp;
10953 int idx, ccnt, fcnt;
James Smart5af5eee2010-10-22 11:06:38 -040010954 int wait_time = 0;
James Smart5e5b5112019-01-28 11:14:22 -080010955 int io_xri_cmpl = 1;
James Smart86c67372017-04-21 16:05:04 -070010956 int nvmet_xri_cmpl = 1;
James Smart895427b2017-02-12 13:52:30 -080010957 int fcp_xri_cmpl = 1;
James Smart5af5eee2010-10-22 11:06:38 -040010958 int els_xri_cmpl = list_empty(&phba->sli4_hba.lpfc_abts_els_sgl_list);
10959
James Smartc3725bd2017-11-20 16:00:42 -080010960 /* Driver just aborted IOs during the hba_unset process. Pause
10961 * here to give the HBA time to complete the IO and get entries
10962 * into the abts lists.
10963 */
10964 msleep(LPFC_XRI_EXCH_BUSY_WAIT_T1 * 5);
10965
10966 /* Wait for NVME pending IO to flush back to transport. */
10967 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
10968 lpfc_nvme_wait_for_io_drain(phba);
10969
James Smart5e5b5112019-01-28 11:14:22 -080010970 ccnt = 0;
10971 fcnt = 0;
10972 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
10973 qp = &phba->sli4_hba.hdwq[idx];
10974 fcp_xri_cmpl = list_empty(
10975 &qp->lpfc_abts_scsi_buf_list);
10976 if (!fcp_xri_cmpl) /* if list is NOT empty */
10977 fcnt++;
10978 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
10979 io_xri_cmpl = list_empty(
10980 &qp->lpfc_abts_nvme_buf_list);
10981 if (!io_xri_cmpl) /* if list is NOT empty */
10982 ccnt++;
10983 }
10984 }
10985 if (ccnt)
10986 io_xri_cmpl = 0;
10987 if (fcnt)
10988 fcp_xri_cmpl = 0;
10989
James Smart86c67372017-04-21 16:05:04 -070010990 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
James Smart86c67372017-04-21 16:05:04 -070010991 nvmet_xri_cmpl =
10992 list_empty(&phba->sli4_hba.lpfc_abts_nvmet_ctx_list);
10993 }
James Smart895427b2017-02-12 13:52:30 -080010994
James Smart5e5b5112019-01-28 11:14:22 -080010995 while (!fcp_xri_cmpl || !els_xri_cmpl || !io_xri_cmpl ||
James Smartf358dd02017-02-12 13:52:34 -080010996 !nvmet_xri_cmpl) {
James Smart5af5eee2010-10-22 11:06:38 -040010997 if (wait_time > LPFC_XRI_EXCH_BUSY_WAIT_TMO) {
James Smart68c9b552018-06-26 08:24:25 -070010998 if (!nvmet_xri_cmpl)
10999 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11000 "6424 NVMET XRI exchange busy "
11001 "wait time: %d seconds.\n",
11002 wait_time/1000);
James Smart5e5b5112019-01-28 11:14:22 -080011003 if (!io_xri_cmpl)
James Smart895427b2017-02-12 13:52:30 -080011004 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11005 "6100 NVME XRI exchange busy "
11006 "wait time: %d seconds.\n",
11007 wait_time/1000);
James Smart5af5eee2010-10-22 11:06:38 -040011008 if (!fcp_xri_cmpl)
11009 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11010 "2877 FCP XRI exchange busy "
11011 "wait time: %d seconds.\n",
11012 wait_time/1000);
11013 if (!els_xri_cmpl)
11014 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11015 "2878 ELS XRI exchange busy "
11016 "wait time: %d seconds.\n",
11017 wait_time/1000);
11018 msleep(LPFC_XRI_EXCH_BUSY_WAIT_T2);
11019 wait_time += LPFC_XRI_EXCH_BUSY_WAIT_T2;
11020 } else {
11021 msleep(LPFC_XRI_EXCH_BUSY_WAIT_T1);
11022 wait_time += LPFC_XRI_EXCH_BUSY_WAIT_T1;
11023 }
James Smart5e5b5112019-01-28 11:14:22 -080011024
11025 ccnt = 0;
11026 fcnt = 0;
11027 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
11028 qp = &phba->sli4_hba.hdwq[idx];
11029 fcp_xri_cmpl = list_empty(
11030 &qp->lpfc_abts_scsi_buf_list);
11031 if (!fcp_xri_cmpl) /* if list is NOT empty */
11032 fcnt++;
11033 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
11034 io_xri_cmpl = list_empty(
11035 &qp->lpfc_abts_nvme_buf_list);
11036 if (!io_xri_cmpl) /* if list is NOT empty */
11037 ccnt++;
11038 }
11039 }
11040 if (ccnt)
11041 io_xri_cmpl = 0;
11042 if (fcnt)
11043 fcp_xri_cmpl = 0;
11044
James Smart86c67372017-04-21 16:05:04 -070011045 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
James Smart86c67372017-04-21 16:05:04 -070011046 nvmet_xri_cmpl = list_empty(
11047 &phba->sli4_hba.lpfc_abts_nvmet_ctx_list);
11048 }
James Smart5af5eee2010-10-22 11:06:38 -040011049 els_xri_cmpl =
11050 list_empty(&phba->sli4_hba.lpfc_abts_els_sgl_list);
James Smartf358dd02017-02-12 13:52:34 -080011051
James Smart5af5eee2010-10-22 11:06:38 -040011052 }
11053}
11054
11055/**
James Smartda0436e2009-05-22 14:51:39 -040011056 * lpfc_sli4_hba_unset - Unset the fcoe hba
11057 * @phba: Pointer to HBA context object.
11058 *
11059 * This function is called in the SLI4 code path to reset the HBA's FCoE
11060 * function. The caller is not required to hold any lock. This routine
11061 * issues PCI function reset mailbox command to reset the FCoE function.
11062 * At the end of the function, it calls lpfc_hba_down_post function to
11063 * free any pending commands.
11064 **/
11065static void
11066lpfc_sli4_hba_unset(struct lpfc_hba *phba)
11067{
11068 int wait_cnt = 0;
11069 LPFC_MBOXQ_t *mboxq;
James Smart912e3ac2011-05-24 11:42:11 -040011070 struct pci_dev *pdev = phba->pcidev;
James Smartda0436e2009-05-22 14:51:39 -040011071
11072 lpfc_stop_hba_timers(phba);
James Smartcdb42be2019-01-28 11:14:21 -080011073 if (phba->pport)
11074 phba->sli4_hba.intr_enable = 0;
James Smartda0436e2009-05-22 14:51:39 -040011075
11076 /*
11077 * Gracefully wait out the potential current outstanding asynchronous
11078 * mailbox command.
11079 */
11080
11081 /* First, block any pending async mailbox command from posted */
11082 spin_lock_irq(&phba->hbalock);
11083 phba->sli.sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
11084 spin_unlock_irq(&phba->hbalock);
11085 /* Now, trying to wait it out if we can */
11086 while (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
11087 msleep(10);
11088 if (++wait_cnt > LPFC_ACTIVE_MBOX_WAIT_CNT)
11089 break;
11090 }
11091 /* Forcefully release the outstanding mailbox command if timed out */
11092 if (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
11093 spin_lock_irq(&phba->hbalock);
11094 mboxq = phba->sli.mbox_active;
11095 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
11096 __lpfc_mbox_cmpl_put(phba, mboxq);
11097 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
11098 phba->sli.mbox_active = NULL;
11099 spin_unlock_irq(&phba->hbalock);
11100 }
11101
James Smart5af5eee2010-10-22 11:06:38 -040011102 /* Abort all iocbs associated with the hba */
11103 lpfc_sli_hba_iocb_abort(phba);
11104
11105 /* Wait for completion of device XRI exchange busy */
11106 lpfc_sli4_xri_exchange_busy_wait(phba);
11107
James Smartda0436e2009-05-22 14:51:39 -040011108 /* Disable PCI subsystem interrupt */
11109 lpfc_sli4_disable_intr(phba);
11110
James Smart912e3ac2011-05-24 11:42:11 -040011111 /* Disable SR-IOV if enabled */
11112 if (phba->cfg_sriov_nr_virtfn)
11113 pci_disable_sriov(pdev);
11114
James Smartda0436e2009-05-22 14:51:39 -040011115 /* Stop kthread signal shall trigger work_done one more time */
11116 kthread_stop(phba->worker_thread);
11117
James Smartd2cc9bc2018-09-10 10:30:50 -070011118 /* Disable FW logging to host memory */
James Smart1165a5c2018-11-29 16:09:39 -080011119 lpfc_ras_stop_fwlog(phba);
James Smartd2cc9bc2018-09-10 10:30:50 -070011120
James Smartd1f525a2017-04-21 16:04:55 -070011121 /* Unset the queues shared with the hardware then release all
11122 * allocated resources.
11123 */
11124 lpfc_sli4_queue_unset(phba);
11125 lpfc_sli4_queue_destroy(phba);
11126
James Smart3677a3a2010-09-29 11:19:14 -040011127 /* Reset SLI4 HBA FCoE function */
11128 lpfc_pci_function_reset(phba);
11129
James Smart1165a5c2018-11-29 16:09:39 -080011130 /* Free RAS DMA memory */
11131 if (phba->ras_fwlog.ras_enabled)
11132 lpfc_sli4_ras_dma_free(phba);
11133
James Smartda0436e2009-05-22 14:51:39 -040011134 /* Stop the SLI4 device port */
11135 phba->pport->work_port_events = 0;
11136}
11137
James Smart28baac72010-02-12 14:42:03 -050011138 /**
11139 * lpfc_pc_sli4_params_get - Get the SLI4_PARAMS port capabilities.
11140 * @phba: Pointer to HBA context object.
11141 * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
11142 *
11143 * This function is called in the SLI4 code path to read the port's
11144 * sli4 capabilities.
11145 *
11146 * This function may be be called from any context that can block-wait
11147 * for the completion. The expectation is that this routine is called
11148 * typically from probe_one or from the online routine.
11149 **/
11150int
11151lpfc_pc_sli4_params_get(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
11152{
11153 int rc;
11154 struct lpfc_mqe *mqe;
11155 struct lpfc_pc_sli4_params *sli4_params;
11156 uint32_t mbox_tmo;
11157
11158 rc = 0;
11159 mqe = &mboxq->u.mqe;
11160
11161 /* Read the port's SLI4 Parameters port capabilities */
James Smartfedd3b72011-02-16 12:39:24 -050011162 lpfc_pc_sli4_params(mboxq);
James Smart28baac72010-02-12 14:42:03 -050011163 if (!phba->sli4_hba.intr_enable)
11164 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
11165 else {
James Smarta183a152011-10-10 21:32:43 -040011166 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
James Smart28baac72010-02-12 14:42:03 -050011167 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
11168 }
11169
11170 if (unlikely(rc))
11171 return 1;
11172
11173 sli4_params = &phba->sli4_hba.pc_sli4_params;
11174 sli4_params->if_type = bf_get(if_type, &mqe->un.sli4_params);
11175 sli4_params->sli_rev = bf_get(sli_rev, &mqe->un.sli4_params);
11176 sli4_params->sli_family = bf_get(sli_family, &mqe->un.sli4_params);
11177 sli4_params->featurelevel_1 = bf_get(featurelevel_1,
11178 &mqe->un.sli4_params);
11179 sli4_params->featurelevel_2 = bf_get(featurelevel_2,
11180 &mqe->un.sli4_params);
11181 sli4_params->proto_types = mqe->un.sli4_params.word3;
11182 sli4_params->sge_supp_len = mqe->un.sli4_params.sge_supp_len;
11183 sli4_params->if_page_sz = bf_get(if_page_sz, &mqe->un.sli4_params);
11184 sli4_params->rq_db_window = bf_get(rq_db_window, &mqe->un.sli4_params);
11185 sli4_params->loopbk_scope = bf_get(loopbk_scope, &mqe->un.sli4_params);
11186 sli4_params->eq_pages_max = bf_get(eq_pages, &mqe->un.sli4_params);
11187 sli4_params->eqe_size = bf_get(eqe_size, &mqe->un.sli4_params);
11188 sli4_params->cq_pages_max = bf_get(cq_pages, &mqe->un.sli4_params);
11189 sli4_params->cqe_size = bf_get(cqe_size, &mqe->un.sli4_params);
11190 sli4_params->mq_pages_max = bf_get(mq_pages, &mqe->un.sli4_params);
11191 sli4_params->mqe_size = bf_get(mqe_size, &mqe->un.sli4_params);
11192 sli4_params->mq_elem_cnt = bf_get(mq_elem_cnt, &mqe->un.sli4_params);
11193 sli4_params->wq_pages_max = bf_get(wq_pages, &mqe->un.sli4_params);
11194 sli4_params->wqe_size = bf_get(wqe_size, &mqe->un.sli4_params);
11195 sli4_params->rq_pages_max = bf_get(rq_pages, &mqe->un.sli4_params);
11196 sli4_params->rqe_size = bf_get(rqe_size, &mqe->un.sli4_params);
11197 sli4_params->hdr_pages_max = bf_get(hdr_pages, &mqe->un.sli4_params);
11198 sli4_params->hdr_size = bf_get(hdr_size, &mqe->un.sli4_params);
11199 sli4_params->hdr_pp_align = bf_get(hdr_pp_align, &mqe->un.sli4_params);
11200 sli4_params->sgl_pages_max = bf_get(sgl_pages, &mqe->un.sli4_params);
11201 sli4_params->sgl_pp_align = bf_get(sgl_pp_align, &mqe->un.sli4_params);
James Smart05580562011-05-24 11:40:48 -040011202
11203 /* Make sure that sge_supp_len can be handled by the driver */
11204 if (sli4_params->sge_supp_len > LPFC_MAX_SGE_SIZE)
11205 sli4_params->sge_supp_len = LPFC_MAX_SGE_SIZE;
11206
James Smart28baac72010-02-12 14:42:03 -050011207 return rc;
11208}
11209
James Smartda0436e2009-05-22 14:51:39 -040011210/**
James Smartfedd3b72011-02-16 12:39:24 -050011211 * lpfc_get_sli4_parameters - Get the SLI4 Config PARAMETERS.
11212 * @phba: Pointer to HBA context object.
11213 * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
11214 *
11215 * This function is called in the SLI4 code path to read the port's
11216 * sli4 capabilities.
11217 *
11218 * This function may be be called from any context that can block-wait
11219 * for the completion. The expectation is that this routine is called
11220 * typically from probe_one or from the online routine.
11221 **/
11222int
11223lpfc_get_sli4_parameters(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
11224{
11225 int rc;
11226 struct lpfc_mqe *mqe = &mboxq->u.mqe;
11227 struct lpfc_pc_sli4_params *sli4_params;
James Smarta183a152011-10-10 21:32:43 -040011228 uint32_t mbox_tmo;
James Smartfedd3b72011-02-16 12:39:24 -050011229 int length;
James Smartbf316c72018-04-09 14:24:28 -070011230 bool exp_wqcq_pages = true;
James Smartfedd3b72011-02-16 12:39:24 -050011231 struct lpfc_sli4_parameters *mbx_sli4_parameters;
11232
James Smart6d368e52011-05-24 11:44:12 -040011233 /*
11234 * By default, the driver assumes the SLI4 port requires RPI
11235 * header postings. The SLI4_PARAM response will correct this
11236 * assumption.
11237 */
11238 phba->sli4_hba.rpi_hdrs_in_use = 1;
11239
James Smartfedd3b72011-02-16 12:39:24 -050011240 /* Read the port's SLI4 Config Parameters */
11241 length = (sizeof(struct lpfc_mbx_get_sli4_parameters) -
11242 sizeof(struct lpfc_sli4_cfg_mhdr));
11243 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
11244 LPFC_MBOX_OPCODE_GET_SLI4_PARAMETERS,
11245 length, LPFC_SLI4_MBX_EMBED);
11246 if (!phba->sli4_hba.intr_enable)
11247 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smarta183a152011-10-10 21:32:43 -040011248 else {
11249 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
11250 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
11251 }
James Smartfedd3b72011-02-16 12:39:24 -050011252 if (unlikely(rc))
11253 return rc;
11254 sli4_params = &phba->sli4_hba.pc_sli4_params;
11255 mbx_sli4_parameters = &mqe->un.get_sli4_parameters.sli4_parameters;
11256 sli4_params->if_type = bf_get(cfg_if_type, mbx_sli4_parameters);
11257 sli4_params->sli_rev = bf_get(cfg_sli_rev, mbx_sli4_parameters);
11258 sli4_params->sli_family = bf_get(cfg_sli_family, mbx_sli4_parameters);
11259 sli4_params->featurelevel_1 = bf_get(cfg_sli_hint_1,
11260 mbx_sli4_parameters);
11261 sli4_params->featurelevel_2 = bf_get(cfg_sli_hint_2,
11262 mbx_sli4_parameters);
11263 if (bf_get(cfg_phwq, mbx_sli4_parameters))
11264 phba->sli3_options |= LPFC_SLI4_PHWQ_ENABLED;
11265 else
11266 phba->sli3_options &= ~LPFC_SLI4_PHWQ_ENABLED;
11267 sli4_params->sge_supp_len = mbx_sli4_parameters->sge_supp_len;
11268 sli4_params->loopbk_scope = bf_get(loopbk_scope, mbx_sli4_parameters);
James Smart1ba981f2014-02-20 09:56:45 -050011269 sli4_params->oas_supported = bf_get(cfg_oas, mbx_sli4_parameters);
James Smartfedd3b72011-02-16 12:39:24 -050011270 sli4_params->cqv = bf_get(cfg_cqv, mbx_sli4_parameters);
11271 sli4_params->mqv = bf_get(cfg_mqv, mbx_sli4_parameters);
11272 sli4_params->wqv = bf_get(cfg_wqv, mbx_sli4_parameters);
11273 sli4_params->rqv = bf_get(cfg_rqv, mbx_sli4_parameters);
James Smart7365f6f2018-02-22 08:18:46 -080011274 sli4_params->eqav = bf_get(cfg_eqav, mbx_sli4_parameters);
11275 sli4_params->cqav = bf_get(cfg_cqav, mbx_sli4_parameters);
James Smart0c651872013-07-15 18:33:23 -040011276 sli4_params->wqsize = bf_get(cfg_wqsize, mbx_sli4_parameters);
James Smart66e9e6b2018-06-26 08:24:27 -070011277 sli4_params->bv1s = bf_get(cfg_bv1s, mbx_sli4_parameters);
James Smartfedd3b72011-02-16 12:39:24 -050011278 sli4_params->sgl_pages_max = bf_get(cfg_sgl_page_cnt,
11279 mbx_sli4_parameters);
James Smart895427b2017-02-12 13:52:30 -080011280 sli4_params->wqpcnt = bf_get(cfg_wqpcnt, mbx_sli4_parameters);
James Smartfedd3b72011-02-16 12:39:24 -050011281 sli4_params->sgl_pp_align = bf_get(cfg_sgl_pp_align,
11282 mbx_sli4_parameters);
James Smart6d368e52011-05-24 11:44:12 -040011283 phba->sli4_hba.extents_in_use = bf_get(cfg_ext, mbx_sli4_parameters);
11284 phba->sli4_hba.rpi_hdrs_in_use = bf_get(cfg_hdrr, mbx_sli4_parameters);
James Smart895427b2017-02-12 13:52:30 -080011285 phba->nvme_support = (bf_get(cfg_nvme, mbx_sli4_parameters) &&
11286 bf_get(cfg_xib, mbx_sli4_parameters));
11287
11288 if ((phba->cfg_enable_fc4_type == LPFC_ENABLE_FCP) ||
11289 !phba->nvme_support) {
11290 phba->nvme_support = 0;
11291 phba->nvmet_support = 0;
James Smartbcb24f62017-11-20 16:00:36 -080011292 phba->cfg_nvmet_mrq = LPFC_NVMET_MRQ_OFF;
James Smart895427b2017-02-12 13:52:30 -080011293 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_NVME,
11294 "6101 Disabling NVME support: "
11295 "Not supported by firmware: %d %d\n",
11296 bf_get(cfg_nvme, mbx_sli4_parameters),
11297 bf_get(cfg_xib, mbx_sli4_parameters));
11298
11299 /* If firmware doesn't support NVME, just use SCSI support */
11300 if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP))
11301 return -ENODEV;
11302 phba->cfg_enable_fc4_type = LPFC_ENABLE_FCP;
11303 }
James Smart05580562011-05-24 11:40:48 -040011304
James Smart414abe02018-06-26 08:24:26 -070011305 /* Only embed PBDE for if_type 6, PBDE support requires xib be set */
11306 if ((bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
11307 LPFC_SLI_INTF_IF_TYPE_6) || (!bf_get(cfg_xib, mbx_sli4_parameters)))
11308 phba->cfg_enable_pbde = 0;
James Smart0bc2b7c2018-02-22 08:18:48 -080011309
James Smart20aefac2018-01-30 15:58:58 -080011310 /*
11311 * To support Suppress Response feature we must satisfy 3 conditions.
11312 * lpfc_suppress_rsp module parameter must be set (default).
11313 * In SLI4-Parameters Descriptor:
11314 * Extended Inline Buffers (XIB) must be supported.
11315 * Suppress Response IU Not Supported (SRIUNS) must NOT be supported
11316 * (double negative).
11317 */
11318 if (phba->cfg_suppress_rsp && bf_get(cfg_xib, mbx_sli4_parameters) &&
11319 !(bf_get(cfg_nosr, mbx_sli4_parameters)))
James Smartf358dd02017-02-12 13:52:34 -080011320 phba->sli.sli_flag |= LPFC_SLI_SUPPRESS_RSP;
James Smart20aefac2018-01-30 15:58:58 -080011321 else
11322 phba->cfg_suppress_rsp = 0;
James Smartf358dd02017-02-12 13:52:34 -080011323
James Smart0cf07f842017-06-01 21:07:10 -070011324 if (bf_get(cfg_eqdr, mbx_sli4_parameters))
11325 phba->sli.sli_flag |= LPFC_SLI_USE_EQDR;
11326
James Smart05580562011-05-24 11:40:48 -040011327 /* Make sure that sge_supp_len can be handled by the driver */
11328 if (sli4_params->sge_supp_len > LPFC_MAX_SGE_SIZE)
11329 sli4_params->sge_supp_len = LPFC_MAX_SGE_SIZE;
11330
James Smartb5c53952016-03-31 14:12:30 -070011331 /*
James Smartc176ffa2018-01-30 15:58:46 -080011332 * Check whether the adapter supports an embedded copy of the
11333 * FCP CMD IU within the WQE for FCP_Ixxx commands. In order
11334 * to use this option, 128-byte WQEs must be used.
James Smartb5c53952016-03-31 14:12:30 -070011335 */
11336 if (bf_get(cfg_ext_embed_cb, mbx_sli4_parameters))
11337 phba->fcp_embed_io = 1;
11338 else
11339 phba->fcp_embed_io = 0;
James Smart7bdedb32016-07-06 12:36:00 -070011340
James Smart0bc2b7c2018-02-22 08:18:48 -080011341 lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_NVME,
James Smart414abe02018-06-26 08:24:26 -070011342 "6422 XIB %d PBDE %d: FCP %d NVME %d %d %d\n",
James Smart0bc2b7c2018-02-22 08:18:48 -080011343 bf_get(cfg_xib, mbx_sli4_parameters),
James Smart414abe02018-06-26 08:24:26 -070011344 phba->cfg_enable_pbde,
11345 phba->fcp_embed_io, phba->nvme_support,
James Smart4e565cf2018-02-22 08:18:50 -080011346 phba->cfg_nvme_embed_cmd, phba->cfg_suppress_rsp);
James Smart0bc2b7c2018-02-22 08:18:48 -080011347
James Smartbf316c72018-04-09 14:24:28 -070011348 if ((bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
11349 LPFC_SLI_INTF_IF_TYPE_2) &&
11350 (bf_get(lpfc_sli_intf_sli_family, &phba->sli4_hba.sli_intf) ==
James Smartc2217682018-05-24 21:09:00 -070011351 LPFC_SLI_INTF_FAMILY_LNCR_A0))
James Smartbf316c72018-04-09 14:24:28 -070011352 exp_wqcq_pages = false;
11353
James Smartc176ffa2018-01-30 15:58:46 -080011354 if ((bf_get(cfg_cqpsize, mbx_sli4_parameters) & LPFC_CQ_16K_PAGE_SZ) &&
11355 (bf_get(cfg_wqpsize, mbx_sli4_parameters) & LPFC_WQ_16K_PAGE_SZ) &&
James Smartbf316c72018-04-09 14:24:28 -070011356 exp_wqcq_pages &&
James Smartc176ffa2018-01-30 15:58:46 -080011357 (sli4_params->wqsize & LPFC_WQ_SZ128_SUPPORT))
11358 phba->enab_exp_wqcq_pages = 1;
11359 else
11360 phba->enab_exp_wqcq_pages = 0;
James Smart7bdedb32016-07-06 12:36:00 -070011361 /*
11362 * Check if the SLI port supports MDS Diagnostics
11363 */
11364 if (bf_get(cfg_mds_diags, mbx_sli4_parameters))
11365 phba->mds_diags_support = 1;
11366 else
11367 phba->mds_diags_support = 0;
James Smartd2cc9bc2018-09-10 10:30:50 -070011368
James Smartfedd3b72011-02-16 12:39:24 -050011369 return 0;
11370}
11371
11372/**
James Smart3772a992009-05-22 14:50:54 -040011373 * lpfc_pci_probe_one_s3 - PCI probe func to reg SLI-3 device to PCI subsystem.
11374 * @pdev: pointer to PCI device
11375 * @pid: pointer to PCI device identifier
11376 *
11377 * This routine is to be called to attach a device with SLI-3 interface spec
11378 * to the PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
11379 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
11380 * information of the device and driver to see if the driver state that it can
11381 * support this kind of device. If the match is successful, the driver core
11382 * invokes this routine. If this routine determines it can claim the HBA, it
11383 * does all the initialization that it needs to do to handle the HBA properly.
11384 *
11385 * Return code
11386 * 0 - driver can claim the device
11387 * negative value - driver can not claim the device
11388 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080011389static int
James Smart3772a992009-05-22 14:50:54 -040011390lpfc_pci_probe_one_s3(struct pci_dev *pdev, const struct pci_device_id *pid)
11391{
11392 struct lpfc_hba *phba;
11393 struct lpfc_vport *vport = NULL;
James Smart6669f9b2009-10-02 15:16:45 -040011394 struct Scsi_Host *shost = NULL;
James Smart3772a992009-05-22 14:50:54 -040011395 int error;
11396 uint32_t cfg_mode, intr_mode;
11397
11398 /* Allocate memory for HBA structure */
11399 phba = lpfc_hba_alloc(pdev);
11400 if (!phba)
11401 return -ENOMEM;
11402
11403 /* Perform generic PCI device enabling operation */
11404 error = lpfc_enable_pci_dev(phba);
James Smart079b5c92011-08-21 21:48:49 -040011405 if (error)
James Smart3772a992009-05-22 14:50:54 -040011406 goto out_free_phba;
James Smart3772a992009-05-22 14:50:54 -040011407
11408 /* Set up SLI API function jump table for PCI-device group-0 HBAs */
11409 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_LP);
11410 if (error)
11411 goto out_disable_pci_dev;
11412
11413 /* Set up SLI-3 specific device PCI memory space */
11414 error = lpfc_sli_pci_mem_setup(phba);
11415 if (error) {
11416 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11417 "1402 Failed to set up pci memory space.\n");
11418 goto out_disable_pci_dev;
11419 }
11420
James Smart3772a992009-05-22 14:50:54 -040011421 /* Set up SLI-3 specific device driver resources */
11422 error = lpfc_sli_driver_resource_setup(phba);
11423 if (error) {
11424 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11425 "1404 Failed to set up driver resource.\n");
11426 goto out_unset_pci_mem_s3;
11427 }
11428
11429 /* Initialize and populate the iocb list per host */
James Smartd1f525a2017-04-21 16:04:55 -070011430
James Smart3772a992009-05-22 14:50:54 -040011431 error = lpfc_init_iocb_list(phba, LPFC_IOCB_LIST_CNT);
11432 if (error) {
11433 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11434 "1405 Failed to initialize iocb list.\n");
11435 goto out_unset_driver_resource_s3;
11436 }
11437
11438 /* Set up common device driver resources */
11439 error = lpfc_setup_driver_resource_phase2(phba);
11440 if (error) {
11441 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11442 "1406 Failed to set up driver resource.\n");
11443 goto out_free_iocb_list;
11444 }
11445
James Smart079b5c92011-08-21 21:48:49 -040011446 /* Get the default values for Model Name and Description */
11447 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
11448
James Smart3772a992009-05-22 14:50:54 -040011449 /* Create SCSI host to the physical port */
11450 error = lpfc_create_shost(phba);
11451 if (error) {
11452 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11453 "1407 Failed to create scsi host.\n");
11454 goto out_unset_driver_resource;
11455 }
11456
11457 /* Configure sysfs attributes */
11458 vport = phba->pport;
11459 error = lpfc_alloc_sysfs_attr(vport);
11460 if (error) {
11461 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11462 "1476 Failed to allocate sysfs attr\n");
11463 goto out_destroy_shost;
11464 }
11465
James Smart6669f9b2009-10-02 15:16:45 -040011466 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
James Smart3772a992009-05-22 14:50:54 -040011467 /* Now, trying to enable interrupt and bring up the device */
11468 cfg_mode = phba->cfg_use_msi;
11469 while (true) {
11470 /* Put device to a known state before enabling interrupt */
11471 lpfc_stop_port(phba);
11472 /* Configure and enable interrupt */
11473 intr_mode = lpfc_sli_enable_intr(phba, cfg_mode);
11474 if (intr_mode == LPFC_INTR_ERROR) {
11475 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11476 "0431 Failed to enable interrupt.\n");
11477 error = -ENODEV;
11478 goto out_free_sysfs_attr;
11479 }
11480 /* SLI-3 HBA setup */
11481 if (lpfc_sli_hba_setup(phba)) {
11482 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11483 "1477 Failed to set up hba\n");
11484 error = -ENODEV;
11485 goto out_remove_device;
11486 }
11487
11488 /* Wait 50ms for the interrupts of previous mailbox commands */
11489 msleep(50);
11490 /* Check active interrupts on message signaled interrupts */
11491 if (intr_mode == 0 ||
11492 phba->sli.slistat.sli_intr > LPFC_MSIX_VECTORS) {
11493 /* Log the current active interrupt mode */
11494 phba->intr_mode = intr_mode;
11495 lpfc_log_intr_mode(phba, intr_mode);
11496 break;
11497 } else {
11498 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
11499 "0447 Configure interrupt mode (%d) "
11500 "failed active interrupt test.\n",
11501 intr_mode);
11502 /* Disable the current interrupt mode */
11503 lpfc_sli_disable_intr(phba);
11504 /* Try next level of interrupt mode */
11505 cfg_mode = --intr_mode;
11506 }
11507 }
11508
11509 /* Perform post initialization setup */
11510 lpfc_post_init_setup(phba);
11511
11512 /* Check if there are static vports to be created. */
11513 lpfc_create_static_vport(phba);
11514
11515 return 0;
11516
11517out_remove_device:
11518 lpfc_unset_hba(phba);
11519out_free_sysfs_attr:
11520 lpfc_free_sysfs_attr(vport);
11521out_destroy_shost:
11522 lpfc_destroy_shost(phba);
11523out_unset_driver_resource:
11524 lpfc_unset_driver_resource_phase2(phba);
11525out_free_iocb_list:
11526 lpfc_free_iocb_list(phba);
11527out_unset_driver_resource_s3:
11528 lpfc_sli_driver_resource_unset(phba);
11529out_unset_pci_mem_s3:
11530 lpfc_sli_pci_mem_unset(phba);
11531out_disable_pci_dev:
11532 lpfc_disable_pci_dev(phba);
James Smart6669f9b2009-10-02 15:16:45 -040011533 if (shost)
11534 scsi_host_put(shost);
James Smart3772a992009-05-22 14:50:54 -040011535out_free_phba:
11536 lpfc_hba_free(phba);
11537 return error;
11538}
11539
11540/**
11541 * lpfc_pci_remove_one_s3 - PCI func to unreg SLI-3 device from PCI subsystem.
James Smarte59058c2008-08-24 21:49:00 -040011542 * @pdev: pointer to PCI device
11543 *
James Smart3772a992009-05-22 14:50:54 -040011544 * This routine is to be called to disattach a device with SLI-3 interface
11545 * spec from PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
11546 * removed from PCI bus, it performs all the necessary cleanup for the HBA
11547 * device to be removed from the PCI subsystem properly.
James Smarte59058c2008-08-24 21:49:00 -040011548 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080011549static void
James Smart3772a992009-05-22 14:50:54 -040011550lpfc_pci_remove_one_s3(struct pci_dev *pdev)
dea31012005-04-17 16:05:31 -050011551{
James Smart2e0fef82007-06-17 19:56:36 -050011552 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11553 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
James Smarteada2722008-12-04 22:39:13 -050011554 struct lpfc_vport **vports;
James Smart2e0fef82007-06-17 19:56:36 -050011555 struct lpfc_hba *phba = vport->phba;
James Smarteada2722008-12-04 22:39:13 -050011556 int i;
Tomohiro Kusumi8a4df1202008-01-11 01:53:00 -050011557
James Smart549e55c2007-08-02 11:09:51 -040011558 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -040011559 vport->load_flag |= FC_UNLOADING;
James Smart549e55c2007-08-02 11:09:51 -040011560 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050011561
James Smart858c9f62007-06-17 19:56:39 -050011562 lpfc_free_sysfs_attr(vport);
11563
James Smarteada2722008-12-04 22:39:13 -050011564 /* Release all the vports against this physical port */
11565 vports = lpfc_create_vport_work_array(phba);
11566 if (vports != NULL)
James Smart587a37f2012-05-09 21:16:03 -040011567 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
11568 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
11569 continue;
James Smarteada2722008-12-04 22:39:13 -050011570 fc_vport_terminate(vports[i]->fc_vport);
James Smart587a37f2012-05-09 21:16:03 -040011571 }
James Smarteada2722008-12-04 22:39:13 -050011572 lpfc_destroy_vport_work_array(phba, vports);
11573
11574 /* Remove FC host and then SCSI host with the physical port */
James Smart858c9f62007-06-17 19:56:39 -050011575 fc_remove_host(shost);
11576 scsi_remove_host(shost);
James Smartd613b6a2017-02-12 13:52:37 -080011577
James Smart87af33f2007-10-27 13:37:43 -040011578 lpfc_cleanup(vport);
11579
James Smart2e0fef82007-06-17 19:56:36 -050011580 /*
11581 * Bring down the SLI Layer. This step disable all interrupts,
11582 * clears the rings, discards all mailbox commands, and resets
11583 * the HBA.
11584 */
James Smarta257bf92009-04-06 18:48:10 -040011585
Justin P. Mattock48e34d02010-12-30 15:07:58 -080011586 /* HBA interrupt will be disabled after this call */
James Smart2e0fef82007-06-17 19:56:36 -050011587 lpfc_sli_hba_down(phba);
James Smarta257bf92009-04-06 18:48:10 -040011588 /* Stop kthread signal shall trigger work_done one more time */
11589 kthread_stop(phba->worker_thread);
11590 /* Final cleanup of txcmplq and reset the HBA */
James Smart2e0fef82007-06-17 19:56:36 -050011591 lpfc_sli_brdrestart(phba);
11592
James Smart72859902012-01-18 16:25:38 -050011593 kfree(phba->vpi_bmask);
11594 kfree(phba->vpi_ids);
11595
James Smart3772a992009-05-22 14:50:54 -040011596 lpfc_stop_hba_timers(phba);
James Smart523128e2018-09-10 10:30:46 -070011597 spin_lock_irq(&phba->port_list_lock);
James Smart858c9f62007-06-17 19:56:39 -050011598 list_del_init(&vport->listentry);
James Smart523128e2018-09-10 10:30:46 -070011599 spin_unlock_irq(&phba->port_list_lock);
James Smart858c9f62007-06-17 19:56:39 -050011600
James Smart858c9f62007-06-17 19:56:39 -050011601 lpfc_debugfs_terminate(vport);
James Smart2e0fef82007-06-17 19:56:36 -050011602
James Smart912e3ac2011-05-24 11:42:11 -040011603 /* Disable SR-IOV if enabled */
11604 if (phba->cfg_sriov_nr_virtfn)
11605 pci_disable_sriov(pdev);
11606
James Smart5b75da22008-12-04 22:39:35 -050011607 /* Disable interrupt */
James Smart3772a992009-05-22 14:50:54 -040011608 lpfc_sli_disable_intr(phba);
dea31012005-04-17 16:05:31 -050011609
James Smart858c9f62007-06-17 19:56:39 -050011610 scsi_host_put(shost);
James Smart2e0fef82007-06-17 19:56:36 -050011611
11612 /*
11613 * Call scsi_free before mem_free since scsi bufs are released to their
11614 * corresponding pools here.
11615 */
11616 lpfc_scsi_free(phba);
James Smart0794d602019-01-28 11:14:19 -080011617 lpfc_free_iocb_list(phba);
11618
James Smart3772a992009-05-22 14:50:54 -040011619 lpfc_mem_free_all(phba);
James Smart2e0fef82007-06-17 19:56:36 -050011620
James Smart34b02dc2008-08-24 21:49:55 -040011621 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
11622 phba->hbqslimp.virt, phba->hbqslimp.phys);
James Smarted957682007-06-17 19:56:37 -050011623
James Smart2e0fef82007-06-17 19:56:36 -050011624 /* Free resources associated with SLI2 interface */
11625 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
James Smart34b02dc2008-08-24 21:49:55 -040011626 phba->slim2p.virt, phba->slim2p.phys);
James Smart2e0fef82007-06-17 19:56:36 -050011627
11628 /* unmap adapter SLIM and Control Registers */
11629 iounmap(phba->ctrl_regs_memmap_p);
11630 iounmap(phba->slim_memmap_p);
11631
James Smart3772a992009-05-22 14:50:54 -040011632 lpfc_hba_free(phba);
James Smart2e0fef82007-06-17 19:56:36 -050011633
Johannes Thumshirne0c04832016-06-07 09:44:03 +020011634 pci_release_mem_regions(pdev);
James Smart2e0fef82007-06-17 19:56:36 -050011635 pci_disable_device(pdev);
dea31012005-04-17 16:05:31 -050011636}
11637
Linas Vepstas8d63f372007-02-14 14:28:36 -060011638/**
James Smart3772a992009-05-22 14:50:54 -040011639 * lpfc_pci_suspend_one_s3 - PCI func to suspend SLI-3 device for power mgmnt
James Smart3a55b532008-12-04 22:38:54 -050011640 * @pdev: pointer to PCI device
11641 * @msg: power management message
11642 *
James Smart3772a992009-05-22 14:50:54 -040011643 * This routine is to be called from the kernel's PCI subsystem to support
11644 * system Power Management (PM) to device with SLI-3 interface spec. When
11645 * PM invokes this method, it quiesces the device by stopping the driver's
11646 * worker thread for the device, turning off device's interrupt and DMA,
11647 * and bring the device offline. Note that as the driver implements the
11648 * minimum PM requirements to a power-aware driver's PM support for the
11649 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
11650 * to the suspend() method call will be treated as SUSPEND and the driver will
11651 * fully reinitialize its device during resume() method call, the driver will
11652 * set device to PCI_D3hot state in PCI config space instead of setting it
11653 * according to the @msg provided by the PM.
James Smart3a55b532008-12-04 22:38:54 -050011654 *
11655 * Return code
James Smart3772a992009-05-22 14:50:54 -040011656 * 0 - driver suspended the device
11657 * Error otherwise
James Smart3a55b532008-12-04 22:38:54 -050011658 **/
11659static int
James Smart3772a992009-05-22 14:50:54 -040011660lpfc_pci_suspend_one_s3(struct pci_dev *pdev, pm_message_t msg)
James Smart3a55b532008-12-04 22:38:54 -050011661{
11662 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11663 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11664
11665 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
11666 "0473 PCI device Power Management suspend.\n");
11667
11668 /* Bring down the device */
James Smart618a5232012-06-12 13:54:36 -040011669 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart3a55b532008-12-04 22:38:54 -050011670 lpfc_offline(phba);
11671 kthread_stop(phba->worker_thread);
11672
11673 /* Disable interrupt from device */
James Smart3772a992009-05-22 14:50:54 -040011674 lpfc_sli_disable_intr(phba);
James Smart3a55b532008-12-04 22:38:54 -050011675
11676 /* Save device state to PCI config space */
11677 pci_save_state(pdev);
11678 pci_set_power_state(pdev, PCI_D3hot);
11679
11680 return 0;
11681}
11682
11683/**
James Smart3772a992009-05-22 14:50:54 -040011684 * lpfc_pci_resume_one_s3 - PCI func to resume SLI-3 device for power mgmnt
James Smart3a55b532008-12-04 22:38:54 -050011685 * @pdev: pointer to PCI device
11686 *
James Smart3772a992009-05-22 14:50:54 -040011687 * This routine is to be called from the kernel's PCI subsystem to support
11688 * system Power Management (PM) to device with SLI-3 interface spec. When PM
11689 * invokes this method, it restores the device's PCI config space state and
11690 * fully reinitializes the device and brings it online. Note that as the
11691 * driver implements the minimum PM requirements to a power-aware driver's
11692 * PM for suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE,
11693 * FREEZE) to the suspend() method call will be treated as SUSPEND and the
11694 * driver will fully reinitialize its device during resume() method call,
11695 * the device will be set to PCI_D0 directly in PCI config space before
11696 * restoring the state.
James Smart3a55b532008-12-04 22:38:54 -050011697 *
11698 * Return code
James Smart3772a992009-05-22 14:50:54 -040011699 * 0 - driver suspended the device
11700 * Error otherwise
James Smart3a55b532008-12-04 22:38:54 -050011701 **/
11702static int
James Smart3772a992009-05-22 14:50:54 -040011703lpfc_pci_resume_one_s3(struct pci_dev *pdev)
James Smart3a55b532008-12-04 22:38:54 -050011704{
11705 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11706 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
James Smart5b75da22008-12-04 22:39:35 -050011707 uint32_t intr_mode;
James Smart3a55b532008-12-04 22:38:54 -050011708 int error;
11709
11710 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
11711 "0452 PCI device Power Management resume.\n");
11712
11713 /* Restore device state from PCI config space */
11714 pci_set_power_state(pdev, PCI_D0);
11715 pci_restore_state(pdev);
James Smart0d878412009-10-02 15:16:56 -040011716
James Smart1dfb5a42010-02-12 14:40:50 -050011717 /*
11718 * As the new kernel behavior of pci_restore_state() API call clears
11719 * device saved_state flag, need to save the restored state again.
11720 */
11721 pci_save_state(pdev);
11722
James Smart3a55b532008-12-04 22:38:54 -050011723 if (pdev->is_busmaster)
11724 pci_set_master(pdev);
11725
11726 /* Startup the kernel thread for this host adapter. */
11727 phba->worker_thread = kthread_run(lpfc_do_work, phba,
11728 "lpfc_worker_%d", phba->brd_no);
11729 if (IS_ERR(phba->worker_thread)) {
11730 error = PTR_ERR(phba->worker_thread);
11731 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11732 "0434 PM resume failed to start worker "
11733 "thread: error=x%x.\n", error);
11734 return error;
11735 }
11736
James Smart5b75da22008-12-04 22:39:35 -050011737 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -040011738 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -050011739 if (intr_mode == LPFC_INTR_ERROR) {
James Smart3a55b532008-12-04 22:38:54 -050011740 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart5b75da22008-12-04 22:39:35 -050011741 "0430 PM resume Failed to enable interrupt\n");
11742 return -EIO;
11743 } else
11744 phba->intr_mode = intr_mode;
James Smart3a55b532008-12-04 22:38:54 -050011745
11746 /* Restart HBA and bring it online */
11747 lpfc_sli_brdrestart(phba);
11748 lpfc_online(phba);
11749
James Smart5b75da22008-12-04 22:39:35 -050011750 /* Log the current active interrupt mode */
11751 lpfc_log_intr_mode(phba, phba->intr_mode);
11752
James Smart3a55b532008-12-04 22:38:54 -050011753 return 0;
11754}
11755
11756/**
James Smart891478a2009-11-18 15:40:23 -050011757 * lpfc_sli_prep_dev_for_recover - Prepare SLI3 device for pci slot recover
11758 * @phba: pointer to lpfc hba data structure.
11759 *
11760 * This routine is called to prepare the SLI3 device for PCI slot recover. It
James Smarte2af0d22010-03-15 11:25:32 -040011761 * aborts all the outstanding SCSI I/Os to the pci device.
James Smart891478a2009-11-18 15:40:23 -050011762 **/
11763static void
11764lpfc_sli_prep_dev_for_recover(struct lpfc_hba *phba)
11765{
11766 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11767 "2723 PCI channel I/O abort preparing for recovery\n");
James Smarte2af0d22010-03-15 11:25:32 -040011768
11769 /*
11770 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
11771 * and let the SCSI mid-layer to retry them to recover.
11772 */
James Smartdb55fba2014-04-04 13:52:02 -040011773 lpfc_sli_abort_fcp_rings(phba);
James Smart891478a2009-11-18 15:40:23 -050011774}
11775
11776/**
James Smart0d878412009-10-02 15:16:56 -040011777 * lpfc_sli_prep_dev_for_reset - Prepare SLI3 device for pci slot reset
11778 * @phba: pointer to lpfc hba data structure.
11779 *
11780 * This routine is called to prepare the SLI3 device for PCI slot reset. It
11781 * disables the device interrupt and pci device, and aborts the internal FCP
11782 * pending I/Os.
11783 **/
11784static void
11785lpfc_sli_prep_dev_for_reset(struct lpfc_hba *phba)
11786{
James Smart0d878412009-10-02 15:16:56 -040011787 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart891478a2009-11-18 15:40:23 -050011788 "2710 PCI channel disable preparing for reset\n");
James Smarte2af0d22010-03-15 11:25:32 -040011789
James Smart75baf692010-06-08 18:31:21 -040011790 /* Block any management I/Os to the device */
James Smart618a5232012-06-12 13:54:36 -040011791 lpfc_block_mgmt_io(phba, LPFC_MBX_WAIT);
James Smart75baf692010-06-08 18:31:21 -040011792
James Smarte2af0d22010-03-15 11:25:32 -040011793 /* Block all SCSI devices' I/Os on the host */
11794 lpfc_scsi_dev_block(phba);
11795
James Smartea714f32013-04-17 20:18:47 -040011796 /* Flush all driver's outstanding SCSI I/Os as we are to reset */
11797 lpfc_sli_flush_fcp_rings(phba);
11798
James Smarte2af0d22010-03-15 11:25:32 -040011799 /* stop all timers */
11800 lpfc_stop_hba_timers(phba);
11801
James Smart0d878412009-10-02 15:16:56 -040011802 /* Disable interrupt and pci device */
11803 lpfc_sli_disable_intr(phba);
11804 pci_disable_device(phba->pcidev);
James Smart0d878412009-10-02 15:16:56 -040011805}
11806
11807/**
11808 * lpfc_sli_prep_dev_for_perm_failure - Prepare SLI3 dev for pci slot disable
11809 * @phba: pointer to lpfc hba data structure.
11810 *
11811 * This routine is called to prepare the SLI3 device for PCI slot permanently
11812 * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
11813 * pending I/Os.
11814 **/
11815static void
James Smart75baf692010-06-08 18:31:21 -040011816lpfc_sli_prep_dev_for_perm_failure(struct lpfc_hba *phba)
James Smart0d878412009-10-02 15:16:56 -040011817{
11818 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart891478a2009-11-18 15:40:23 -050011819 "2711 PCI channel permanent disable for failure\n");
James Smarte2af0d22010-03-15 11:25:32 -040011820 /* Block all SCSI devices' I/Os on the host */
11821 lpfc_scsi_dev_block(phba);
11822
11823 /* stop all timers */
11824 lpfc_stop_hba_timers(phba);
11825
James Smart0d878412009-10-02 15:16:56 -040011826 /* Clean up all driver's outstanding SCSI I/Os */
11827 lpfc_sli_flush_fcp_rings(phba);
11828}
11829
11830/**
James Smart3772a992009-05-22 14:50:54 -040011831 * lpfc_io_error_detected_s3 - Method for handling SLI-3 device PCI I/O error
James Smarte59058c2008-08-24 21:49:00 -040011832 * @pdev: pointer to PCI device.
11833 * @state: the current PCI connection state.
Linas Vepstas8d63f372007-02-14 14:28:36 -060011834 *
James Smart3772a992009-05-22 14:50:54 -040011835 * This routine is called from the PCI subsystem for I/O error handling to
11836 * device with SLI-3 interface spec. This function is called by the PCI
11837 * subsystem after a PCI bus error affecting this device has been detected.
11838 * When this function is invoked, it will need to stop all the I/Os and
11839 * interrupt(s) to the device. Once that is done, it will return
11840 * PCI_ERS_RESULT_NEED_RESET for the PCI subsystem to perform proper recovery
11841 * as desired.
James Smarte59058c2008-08-24 21:49:00 -040011842 *
11843 * Return codes
James Smart0d878412009-10-02 15:16:56 -040011844 * PCI_ERS_RESULT_CAN_RECOVER - can be recovered with reset_link
James Smart3772a992009-05-22 14:50:54 -040011845 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
11846 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
James Smarte59058c2008-08-24 21:49:00 -040011847 **/
James Smart3772a992009-05-22 14:50:54 -040011848static pci_ers_result_t
11849lpfc_io_error_detected_s3(struct pci_dev *pdev, pci_channel_state_t state)
Linas Vepstas8d63f372007-02-14 14:28:36 -060011850{
James Smart51ef4c22007-08-02 11:10:31 -040011851 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11852 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -060011853
James Smart0d878412009-10-02 15:16:56 -040011854 switch (state) {
11855 case pci_channel_io_normal:
James Smart891478a2009-11-18 15:40:23 -050011856 /* Non-fatal error, prepare for recovery */
11857 lpfc_sli_prep_dev_for_recover(phba);
James Smart0d878412009-10-02 15:16:56 -040011858 return PCI_ERS_RESULT_CAN_RECOVER;
11859 case pci_channel_io_frozen:
11860 /* Fatal error, prepare for slot reset */
11861 lpfc_sli_prep_dev_for_reset(phba);
11862 return PCI_ERS_RESULT_NEED_RESET;
11863 case pci_channel_io_perm_failure:
11864 /* Permanent failure, prepare for device down */
James Smart75baf692010-06-08 18:31:21 -040011865 lpfc_sli_prep_dev_for_perm_failure(phba);
Linas Vepstas8d63f372007-02-14 14:28:36 -060011866 return PCI_ERS_RESULT_DISCONNECT;
James Smart0d878412009-10-02 15:16:56 -040011867 default:
11868 /* Unknown state, prepare and request slot reset */
11869 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11870 "0472 Unknown PCI error state: x%x\n", state);
11871 lpfc_sli_prep_dev_for_reset(phba);
11872 return PCI_ERS_RESULT_NEED_RESET;
James Smarta8e497d2008-08-24 21:50:11 -040011873 }
Linas Vepstas8d63f372007-02-14 14:28:36 -060011874}
11875
11876/**
James Smart3772a992009-05-22 14:50:54 -040011877 * lpfc_io_slot_reset_s3 - Method for restarting PCI SLI-3 device from scratch.
James Smarte59058c2008-08-24 21:49:00 -040011878 * @pdev: pointer to PCI device.
Linas Vepstas8d63f372007-02-14 14:28:36 -060011879 *
James Smart3772a992009-05-22 14:50:54 -040011880 * This routine is called from the PCI subsystem for error handling to
11881 * device with SLI-3 interface spec. This is called after PCI bus has been
11882 * reset to restart the PCI card from scratch, as if from a cold-boot.
11883 * During the PCI subsystem error recovery, after driver returns
11884 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
11885 * recovery and then call this routine before calling the .resume method
11886 * to recover the device. This function will initialize the HBA device,
11887 * enable the interrupt, but it will just put the HBA to offline state
11888 * without passing any I/O traffic.
James Smarte59058c2008-08-24 21:49:00 -040011889 *
11890 * Return codes
James Smart3772a992009-05-22 14:50:54 -040011891 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
11892 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
Linas Vepstas8d63f372007-02-14 14:28:36 -060011893 */
James Smart3772a992009-05-22 14:50:54 -040011894static pci_ers_result_t
11895lpfc_io_slot_reset_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -060011896{
James Smart51ef4c22007-08-02 11:10:31 -040011897 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11898 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -060011899 struct lpfc_sli *psli = &phba->sli;
James Smart5b75da22008-12-04 22:39:35 -050011900 uint32_t intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -060011901
11902 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
Benjamin Herrenschmidt09483912007-12-20 15:28:09 +110011903 if (pci_enable_device_mem(pdev)) {
Linas Vepstas8d63f372007-02-14 14:28:36 -060011904 printk(KERN_ERR "lpfc: Cannot re-enable "
11905 "PCI device after reset.\n");
11906 return PCI_ERS_RESULT_DISCONNECT;
11907 }
11908
James Smart97207482008-12-04 22:39:19 -050011909 pci_restore_state(pdev);
James Smart1dfb5a42010-02-12 14:40:50 -050011910
11911 /*
11912 * As the new kernel behavior of pci_restore_state() API call clears
11913 * device saved_state flag, need to save the restored state again.
11914 */
11915 pci_save_state(pdev);
11916
James Smart97207482008-12-04 22:39:19 -050011917 if (pdev->is_busmaster)
11918 pci_set_master(pdev);
Linas Vepstas8d63f372007-02-14 14:28:36 -060011919
James Smart92d7f7b2007-06-17 19:56:38 -050011920 spin_lock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040011921 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -050011922 spin_unlock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -060011923
James Smart5b75da22008-12-04 22:39:35 -050011924 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -040011925 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -050011926 if (intr_mode == LPFC_INTR_ERROR) {
11927 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11928 "0427 Cannot re-enable interrupt after "
11929 "slot reset.\n");
11930 return PCI_ERS_RESULT_DISCONNECT;
11931 } else
11932 phba->intr_mode = intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -060011933
James Smart75baf692010-06-08 18:31:21 -040011934 /* Take device offline, it will perform cleanup */
James Smart618a5232012-06-12 13:54:36 -040011935 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
Linas Vepstas8d63f372007-02-14 14:28:36 -060011936 lpfc_offline(phba);
11937 lpfc_sli_brdrestart(phba);
11938
James Smart5b75da22008-12-04 22:39:35 -050011939 /* Log the current active interrupt mode */
11940 lpfc_log_intr_mode(phba, phba->intr_mode);
11941
Linas Vepstas8d63f372007-02-14 14:28:36 -060011942 return PCI_ERS_RESULT_RECOVERED;
11943}
11944
11945/**
James Smart3772a992009-05-22 14:50:54 -040011946 * lpfc_io_resume_s3 - Method for resuming PCI I/O operation on SLI-3 device.
James Smarte59058c2008-08-24 21:49:00 -040011947 * @pdev: pointer to PCI device
Linas Vepstas8d63f372007-02-14 14:28:36 -060011948 *
James Smart3772a992009-05-22 14:50:54 -040011949 * This routine is called from the PCI subsystem for error handling to device
11950 * with SLI-3 interface spec. It is called when kernel error recovery tells
11951 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
11952 * error recovery. After this call, traffic can start to flow from this device
11953 * again.
Linas Vepstas8d63f372007-02-14 14:28:36 -060011954 */
James Smart3772a992009-05-22 14:50:54 -040011955static void
11956lpfc_io_resume_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -060011957{
James Smart51ef4c22007-08-02 11:10:31 -040011958 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11959 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -060011960
James Smarte2af0d22010-03-15 11:25:32 -040011961 /* Bring device online, it will be no-op for non-fatal error resume */
James Smart58da1ff2008-04-07 10:15:56 -040011962 lpfc_online(phba);
Linas Vepstas8d63f372007-02-14 14:28:36 -060011963}
11964
James Smart3772a992009-05-22 14:50:54 -040011965/**
James Smartda0436e2009-05-22 14:51:39 -040011966 * lpfc_sli4_get_els_iocb_cnt - Calculate the # of ELS IOCBs to reserve
11967 * @phba: pointer to lpfc hba data structure.
11968 *
11969 * returns the number of ELS/CT IOCBs to reserve
11970 **/
11971int
11972lpfc_sli4_get_els_iocb_cnt(struct lpfc_hba *phba)
11973{
11974 int max_xri = phba->sli4_hba.max_cfg_param.max_xri;
11975
James Smartf1126682009-06-10 17:22:44 -040011976 if (phba->sli_rev == LPFC_SLI_REV4) {
11977 if (max_xri <= 100)
James Smart6a9c52c2009-10-02 15:16:51 -040011978 return 10;
James Smartf1126682009-06-10 17:22:44 -040011979 else if (max_xri <= 256)
James Smart6a9c52c2009-10-02 15:16:51 -040011980 return 25;
James Smartf1126682009-06-10 17:22:44 -040011981 else if (max_xri <= 512)
James Smart6a9c52c2009-10-02 15:16:51 -040011982 return 50;
James Smartf1126682009-06-10 17:22:44 -040011983 else if (max_xri <= 1024)
James Smart6a9c52c2009-10-02 15:16:51 -040011984 return 100;
James Smart8a9d2e82012-05-09 21:16:12 -040011985 else if (max_xri <= 1536)
James Smart6a9c52c2009-10-02 15:16:51 -040011986 return 150;
James Smart8a9d2e82012-05-09 21:16:12 -040011987 else if (max_xri <= 2048)
11988 return 200;
11989 else
11990 return 250;
James Smartf1126682009-06-10 17:22:44 -040011991 } else
11992 return 0;
James Smartda0436e2009-05-22 14:51:39 -040011993}
11994
11995/**
James Smart895427b2017-02-12 13:52:30 -080011996 * lpfc_sli4_get_iocb_cnt - Calculate the # of total IOCBs to reserve
11997 * @phba: pointer to lpfc hba data structure.
11998 *
James Smartf358dd02017-02-12 13:52:34 -080011999 * returns the number of ELS/CT + NVMET IOCBs to reserve
James Smart895427b2017-02-12 13:52:30 -080012000 **/
12001int
12002lpfc_sli4_get_iocb_cnt(struct lpfc_hba *phba)
12003{
12004 int max_xri = lpfc_sli4_get_els_iocb_cnt(phba);
12005
James Smartf358dd02017-02-12 13:52:34 -080012006 if (phba->nvmet_support)
12007 max_xri += LPFC_NVMET_BUF_POST;
James Smart895427b2017-02-12 13:52:30 -080012008 return max_xri;
12009}
12010
12011
James Smart1feb8202018-02-22 08:18:47 -080012012static void
12013lpfc_log_write_firmware_error(struct lpfc_hba *phba, uint32_t offset,
12014 uint32_t magic_number, uint32_t ftype, uint32_t fid, uint32_t fsize,
12015 const struct firmware *fw)
12016{
James Smarta72d56b2018-05-04 20:37:52 -070012017 if ((offset == ADD_STATUS_FW_NOT_SUPPORTED) ||
12018 (phba->pcidev->device == PCI_DEVICE_ID_LANCER_G6_FC &&
12019 magic_number != MAGIC_NUMER_G6) ||
12020 (phba->pcidev->device == PCI_DEVICE_ID_LANCER_G7_FC &&
12021 magic_number != MAGIC_NUMER_G7))
James Smart1feb8202018-02-22 08:18:47 -080012022 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12023 "3030 This firmware version is not supported on "
12024 "this HBA model. Device:%x Magic:%x Type:%x "
12025 "ID:%x Size %d %zd\n",
12026 phba->pcidev->device, magic_number, ftype, fid,
12027 fsize, fw->size);
12028 else
12029 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12030 "3022 FW Download failed. Device:%x Magic:%x Type:%x "
12031 "ID:%x Size %d %zd\n",
12032 phba->pcidev->device, magic_number, ftype, fid,
12033 fsize, fw->size);
12034}
12035
12036
James Smart895427b2017-02-12 13:52:30 -080012037/**
James Smart52d52442011-05-24 11:42:45 -040012038 * lpfc_write_firmware - attempt to write a firmware image to the port
James Smart52d52442011-05-24 11:42:45 -040012039 * @fw: pointer to firmware image returned from request_firmware.
James Smartce396282012-09-29 11:30:56 -040012040 * @phba: pointer to lpfc hba data structure.
James Smart52d52442011-05-24 11:42:45 -040012041 *
James Smart52d52442011-05-24 11:42:45 -040012042 **/
James Smartce396282012-09-29 11:30:56 -040012043static void
12044lpfc_write_firmware(const struct firmware *fw, void *context)
James Smart52d52442011-05-24 11:42:45 -040012045{
James Smartce396282012-09-29 11:30:56 -040012046 struct lpfc_hba *phba = (struct lpfc_hba *)context;
James Smart6b5151f2012-01-18 16:24:06 -050012047 char fwrev[FW_REV_STR_SIZE];
James Smartce396282012-09-29 11:30:56 -040012048 struct lpfc_grp_hdr *image;
James Smart52d52442011-05-24 11:42:45 -040012049 struct list_head dma_buffer_list;
12050 int i, rc = 0;
12051 struct lpfc_dmabuf *dmabuf, *next;
12052 uint32_t offset = 0, temp_offset = 0;
James Smart6b6ef5d2016-10-13 15:06:17 -070012053 uint32_t magic_number, ftype, fid, fsize;
James Smart52d52442011-05-24 11:42:45 -040012054
James Smartc71ab862012-10-31 14:44:33 -040012055 /* It can be null in no-wait mode, sanity check */
James Smartce396282012-09-29 11:30:56 -040012056 if (!fw) {
12057 rc = -ENXIO;
12058 goto out;
12059 }
12060 image = (struct lpfc_grp_hdr *)fw->data;
12061
James Smart6b6ef5d2016-10-13 15:06:17 -070012062 magic_number = be32_to_cpu(image->magic_number);
12063 ftype = bf_get_be32(lpfc_grp_hdr_file_type, image);
James Smart1feb8202018-02-22 08:18:47 -080012064 fid = bf_get_be32(lpfc_grp_hdr_id, image);
James Smart6b6ef5d2016-10-13 15:06:17 -070012065 fsize = be32_to_cpu(image->size);
12066
James Smart52d52442011-05-24 11:42:45 -040012067 INIT_LIST_HEAD(&dma_buffer_list);
James Smart52d52442011-05-24 11:42:45 -040012068 lpfc_decode_firmware_rev(phba, fwrev, 1);
James Smart88a2cfb2011-07-22 18:36:33 -040012069 if (strncmp(fwrev, image->revision, strnlen(image->revision, 16))) {
James Smart52d52442011-05-24 11:42:45 -040012070 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartce396282012-09-29 11:30:56 -040012071 "3023 Updating Firmware, Current Version:%s "
James Smart52d52442011-05-24 11:42:45 -040012072 "New Version:%s\n",
James Smart88a2cfb2011-07-22 18:36:33 -040012073 fwrev, image->revision);
James Smart52d52442011-05-24 11:42:45 -040012074 for (i = 0; i < LPFC_MBX_WR_CONFIG_MAX_BDE; i++) {
12075 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf),
12076 GFP_KERNEL);
12077 if (!dmabuf) {
12078 rc = -ENOMEM;
James Smartce396282012-09-29 11:30:56 -040012079 goto release_out;
James Smart52d52442011-05-24 11:42:45 -040012080 }
12081 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
12082 SLI4_PAGE_SIZE,
12083 &dmabuf->phys,
12084 GFP_KERNEL);
12085 if (!dmabuf->virt) {
12086 kfree(dmabuf);
12087 rc = -ENOMEM;
James Smartce396282012-09-29 11:30:56 -040012088 goto release_out;
James Smart52d52442011-05-24 11:42:45 -040012089 }
12090 list_add_tail(&dmabuf->list, &dma_buffer_list);
12091 }
12092 while (offset < fw->size) {
12093 temp_offset = offset;
12094 list_for_each_entry(dmabuf, &dma_buffer_list, list) {
James Smart079b5c92011-08-21 21:48:49 -040012095 if (temp_offset + SLI4_PAGE_SIZE > fw->size) {
James Smart52d52442011-05-24 11:42:45 -040012096 memcpy(dmabuf->virt,
12097 fw->data + temp_offset,
James Smart079b5c92011-08-21 21:48:49 -040012098 fw->size - temp_offset);
12099 temp_offset = fw->size;
James Smart52d52442011-05-24 11:42:45 -040012100 break;
12101 }
James Smart52d52442011-05-24 11:42:45 -040012102 memcpy(dmabuf->virt, fw->data + temp_offset,
12103 SLI4_PAGE_SIZE);
James Smart88a2cfb2011-07-22 18:36:33 -040012104 temp_offset += SLI4_PAGE_SIZE;
James Smart52d52442011-05-24 11:42:45 -040012105 }
12106 rc = lpfc_wr_object(phba, &dma_buffer_list,
12107 (fw->size - offset), &offset);
James Smart1feb8202018-02-22 08:18:47 -080012108 if (rc) {
12109 lpfc_log_write_firmware_error(phba, offset,
12110 magic_number, ftype, fid, fsize, fw);
James Smartce396282012-09-29 11:30:56 -040012111 goto release_out;
James Smart1feb8202018-02-22 08:18:47 -080012112 }
James Smart52d52442011-05-24 11:42:45 -040012113 }
12114 rc = offset;
James Smart1feb8202018-02-22 08:18:47 -080012115 } else
12116 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12117 "3029 Skipped Firmware update, Current "
12118 "Version:%s New Version:%s\n",
12119 fwrev, image->revision);
James Smartce396282012-09-29 11:30:56 -040012120
12121release_out:
James Smart52d52442011-05-24 11:42:45 -040012122 list_for_each_entry_safe(dmabuf, next, &dma_buffer_list, list) {
12123 list_del(&dmabuf->list);
12124 dma_free_coherent(&phba->pcidev->dev, SLI4_PAGE_SIZE,
12125 dmabuf->virt, dmabuf->phys);
12126 kfree(dmabuf);
12127 }
James Smartce396282012-09-29 11:30:56 -040012128 release_firmware(fw);
12129out:
12130 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartc71ab862012-10-31 14:44:33 -040012131 "3024 Firmware update done: %d.\n", rc);
James Smartce396282012-09-29 11:30:56 -040012132 return;
James Smart52d52442011-05-24 11:42:45 -040012133}
12134
12135/**
James Smartc71ab862012-10-31 14:44:33 -040012136 * lpfc_sli4_request_firmware_update - Request linux generic firmware upgrade
12137 * @phba: pointer to lpfc hba data structure.
12138 *
12139 * This routine is called to perform Linux generic firmware upgrade on device
12140 * that supports such feature.
12141 **/
12142int
12143lpfc_sli4_request_firmware_update(struct lpfc_hba *phba, uint8_t fw_upgrade)
12144{
12145 uint8_t file_name[ELX_MODEL_NAME_SIZE];
12146 int ret;
12147 const struct firmware *fw;
12148
12149 /* Only supported on SLI4 interface type 2 for now */
James Smart27d6ac02018-02-22 08:18:42 -080012150 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) <
James Smartc71ab862012-10-31 14:44:33 -040012151 LPFC_SLI_INTF_IF_TYPE_2)
12152 return -EPERM;
12153
12154 snprintf(file_name, ELX_MODEL_NAME_SIZE, "%s.grp", phba->ModelName);
12155
12156 if (fw_upgrade == INT_FW_UPGRADE) {
12157 ret = request_firmware_nowait(THIS_MODULE, FW_ACTION_HOTPLUG,
12158 file_name, &phba->pcidev->dev,
12159 GFP_KERNEL, (void *)phba,
12160 lpfc_write_firmware);
12161 } else if (fw_upgrade == RUN_FW_UPGRADE) {
12162 ret = request_firmware(&fw, file_name, &phba->pcidev->dev);
12163 if (!ret)
12164 lpfc_write_firmware(fw, (void *)phba);
12165 } else {
12166 ret = -EINVAL;
12167 }
12168
12169 return ret;
12170}
12171
12172/**
James Smartda0436e2009-05-22 14:51:39 -040012173 * lpfc_pci_probe_one_s4 - PCI probe func to reg SLI-4 device to PCI subsys
12174 * @pdev: pointer to PCI device
12175 * @pid: pointer to PCI device identifier
12176 *
12177 * This routine is called from the kernel's PCI subsystem to device with
12178 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
12179 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
12180 * information of the device and driver to see if the driver state that it
12181 * can support this kind of device. If the match is successful, the driver
12182 * core invokes this routine. If this routine determines it can claim the HBA,
12183 * it does all the initialization that it needs to do to handle the HBA
12184 * properly.
12185 *
12186 * Return code
12187 * 0 - driver can claim the device
12188 * negative value - driver can not claim the device
12189 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080012190static int
James Smartda0436e2009-05-22 14:51:39 -040012191lpfc_pci_probe_one_s4(struct pci_dev *pdev, const struct pci_device_id *pid)
12192{
12193 struct lpfc_hba *phba;
12194 struct lpfc_vport *vport = NULL;
James Smart6669f9b2009-10-02 15:16:45 -040012195 struct Scsi_Host *shost = NULL;
James Smartc4908502019-01-28 11:14:28 -080012196 int error;
James Smartda0436e2009-05-22 14:51:39 -040012197 uint32_t cfg_mode, intr_mode;
James Smartda0436e2009-05-22 14:51:39 -040012198
12199 /* Allocate memory for HBA structure */
12200 phba = lpfc_hba_alloc(pdev);
12201 if (!phba)
12202 return -ENOMEM;
12203
12204 /* Perform generic PCI device enabling operation */
12205 error = lpfc_enable_pci_dev(phba);
James Smart079b5c92011-08-21 21:48:49 -040012206 if (error)
James Smartda0436e2009-05-22 14:51:39 -040012207 goto out_free_phba;
James Smartda0436e2009-05-22 14:51:39 -040012208
12209 /* Set up SLI API function jump table for PCI-device group-1 HBAs */
12210 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_OC);
12211 if (error)
12212 goto out_disable_pci_dev;
12213
12214 /* Set up SLI-4 specific device PCI memory space */
12215 error = lpfc_sli4_pci_mem_setup(phba);
12216 if (error) {
12217 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12218 "1410 Failed to set up pci memory space.\n");
12219 goto out_disable_pci_dev;
12220 }
12221
James Smartda0436e2009-05-22 14:51:39 -040012222 /* Set up SLI-4 Specific device driver resources */
12223 error = lpfc_sli4_driver_resource_setup(phba);
12224 if (error) {
12225 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12226 "1412 Failed to set up driver resource.\n");
12227 goto out_unset_pci_mem_s4;
12228 }
12229
James Smart19ca7602010-11-20 23:11:55 -050012230 INIT_LIST_HEAD(&phba->active_rrq_list);
James Smart7d791df2011-07-22 18:37:52 -040012231 INIT_LIST_HEAD(&phba->fcf.fcf_pri_list);
James Smart19ca7602010-11-20 23:11:55 -050012232
James Smartda0436e2009-05-22 14:51:39 -040012233 /* Set up common device driver resources */
12234 error = lpfc_setup_driver_resource_phase2(phba);
12235 if (error) {
12236 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12237 "1414 Failed to set up driver resource.\n");
James Smart6c621a22017-05-15 15:20:45 -070012238 goto out_unset_driver_resource_s4;
James Smartda0436e2009-05-22 14:51:39 -040012239 }
12240
James Smart079b5c92011-08-21 21:48:49 -040012241 /* Get the default values for Model Name and Description */
12242 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
12243
James Smartda0436e2009-05-22 14:51:39 -040012244 /* Now, trying to enable interrupt and bring up the device */
12245 cfg_mode = phba->cfg_use_msi;
James Smartda0436e2009-05-22 14:51:39 -040012246
James Smart7b15db32013-01-03 15:43:29 -050012247 /* Put device to a known state before enabling interrupt */
James Smartcdb42be2019-01-28 11:14:21 -080012248 phba->pport = NULL;
James Smart7b15db32013-01-03 15:43:29 -050012249 lpfc_stop_port(phba);
James Smart895427b2017-02-12 13:52:30 -080012250
James Smart7b15db32013-01-03 15:43:29 -050012251 /* Configure and enable interrupt */
12252 intr_mode = lpfc_sli4_enable_intr(phba, cfg_mode);
12253 if (intr_mode == LPFC_INTR_ERROR) {
12254 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12255 "0426 Failed to enable interrupt.\n");
12256 error = -ENODEV;
James Smartcdb42be2019-01-28 11:14:21 -080012257 goto out_unset_driver_resource;
James Smartda0436e2009-05-22 14:51:39 -040012258 }
James Smart7b15db32013-01-03 15:43:29 -050012259 /* Default to single EQ for non-MSI-X */
James Smart895427b2017-02-12 13:52:30 -080012260 if (phba->intr_type != MSIX) {
James Smart6a828b02019-01-28 11:14:31 -080012261 phba->cfg_irq_chann = 1;
James Smart2d7dbc42017-02-12 13:52:35 -080012262 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
James Smart2d7dbc42017-02-12 13:52:35 -080012263 if (phba->nvmet_support)
12264 phba->cfg_nvmet_mrq = 1;
12265 }
James Smartcdb42be2019-01-28 11:14:21 -080012266 }
James Smart6a828b02019-01-28 11:14:31 -080012267 lpfc_cpu_affinity_check(phba, phba->cfg_irq_chann);
James Smartcdb42be2019-01-28 11:14:21 -080012268
12269 /* Create SCSI host to the physical port */
12270 error = lpfc_create_shost(phba);
12271 if (error) {
12272 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12273 "1415 Failed to create scsi host.\n");
12274 goto out_disable_intr;
12275 }
12276 vport = phba->pport;
12277 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
12278
12279 /* Configure sysfs attributes */
12280 error = lpfc_alloc_sysfs_attr(vport);
12281 if (error) {
12282 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12283 "1416 Failed to allocate sysfs attr\n");
12284 goto out_destroy_shost;
James Smart895427b2017-02-12 13:52:30 -080012285 }
12286
James Smart7b15db32013-01-03 15:43:29 -050012287 /* Set up SLI-4 HBA */
12288 if (lpfc_sli4_hba_setup(phba)) {
12289 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12290 "1421 Failed to set up hba\n");
12291 error = -ENODEV;
James Smartcdb42be2019-01-28 11:14:21 -080012292 goto out_free_sysfs_attr;
James Smart7b15db32013-01-03 15:43:29 -050012293 }
12294
12295 /* Log the current active interrupt mode */
12296 phba->intr_mode = intr_mode;
12297 lpfc_log_intr_mode(phba, intr_mode);
James Smartda0436e2009-05-22 14:51:39 -040012298
12299 /* Perform post initialization setup */
12300 lpfc_post_init_setup(phba);
12301
James Smart01649562017-02-12 13:52:32 -080012302 /* NVME support in FW earlier in the driver load corrects the
12303 * FC4 type making a check for nvme_support unnecessary.
12304 */
James Smart0794d602019-01-28 11:14:19 -080012305 if (phba->nvmet_support == 0) {
12306 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
12307 /* Create NVME binding with nvme_fc_transport. This
12308 * ensures the vport is initialized. If the localport
12309 * create fails, it should not unload the driver to
12310 * support field issues.
12311 */
12312 error = lpfc_nvme_create_localport(vport);
12313 if (error) {
12314 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12315 "6004 NVME registration "
12316 "failed, error x%x\n",
12317 error);
12318 }
12319 }
James Smart01649562017-02-12 13:52:32 -080012320 }
James Smart895427b2017-02-12 13:52:30 -080012321
James Smartc71ab862012-10-31 14:44:33 -040012322 /* check for firmware upgrade or downgrade */
12323 if (phba->cfg_request_firmware_upgrade)
Sebastian Herbsztdb6f1c22015-08-31 16:48:14 -040012324 lpfc_sli4_request_firmware_update(phba, INT_FW_UPGRADE);
James Smart52d52442011-05-24 11:42:45 -040012325
James Smart1c6834a2009-07-19 10:01:26 -040012326 /* Check if there are static vports to be created. */
12327 lpfc_create_static_vport(phba);
James Smartd2cc9bc2018-09-10 10:30:50 -070012328
12329 /* Enable RAS FW log support */
12330 lpfc_sli4_ras_setup(phba);
12331
James Smartda0436e2009-05-22 14:51:39 -040012332 return 0;
12333
James Smartda0436e2009-05-22 14:51:39 -040012334out_free_sysfs_attr:
12335 lpfc_free_sysfs_attr(vport);
12336out_destroy_shost:
12337 lpfc_destroy_shost(phba);
James Smartcdb42be2019-01-28 11:14:21 -080012338out_disable_intr:
12339 lpfc_sli4_disable_intr(phba);
James Smartda0436e2009-05-22 14:51:39 -040012340out_unset_driver_resource:
12341 lpfc_unset_driver_resource_phase2(phba);
James Smartda0436e2009-05-22 14:51:39 -040012342out_unset_driver_resource_s4:
12343 lpfc_sli4_driver_resource_unset(phba);
12344out_unset_pci_mem_s4:
12345 lpfc_sli4_pci_mem_unset(phba);
12346out_disable_pci_dev:
12347 lpfc_disable_pci_dev(phba);
James Smart6669f9b2009-10-02 15:16:45 -040012348 if (shost)
12349 scsi_host_put(shost);
James Smartda0436e2009-05-22 14:51:39 -040012350out_free_phba:
12351 lpfc_hba_free(phba);
12352 return error;
12353}
12354
12355/**
12356 * lpfc_pci_remove_one_s4 - PCI func to unreg SLI-4 device from PCI subsystem
12357 * @pdev: pointer to PCI device
12358 *
12359 * This routine is called from the kernel's PCI subsystem to device with
12360 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
12361 * removed from PCI bus, it performs all the necessary cleanup for the HBA
12362 * device to be removed from the PCI subsystem properly.
12363 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080012364static void
James Smartda0436e2009-05-22 14:51:39 -040012365lpfc_pci_remove_one_s4(struct pci_dev *pdev)
12366{
12367 struct Scsi_Host *shost = pci_get_drvdata(pdev);
12368 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
12369 struct lpfc_vport **vports;
12370 struct lpfc_hba *phba = vport->phba;
12371 int i;
12372
12373 /* Mark the device unloading flag */
12374 spin_lock_irq(&phba->hbalock);
12375 vport->load_flag |= FC_UNLOADING;
12376 spin_unlock_irq(&phba->hbalock);
12377
12378 /* Free the HBA sysfs attributes */
12379 lpfc_free_sysfs_attr(vport);
12380
12381 /* Release all the vports against this physical port */
12382 vports = lpfc_create_vport_work_array(phba);
12383 if (vports != NULL)
James Smart587a37f2012-05-09 21:16:03 -040012384 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
12385 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
12386 continue;
James Smartda0436e2009-05-22 14:51:39 -040012387 fc_vport_terminate(vports[i]->fc_vport);
James Smart587a37f2012-05-09 21:16:03 -040012388 }
James Smartda0436e2009-05-22 14:51:39 -040012389 lpfc_destroy_vport_work_array(phba, vports);
12390
12391 /* Remove FC host and then SCSI host with the physical port */
12392 fc_remove_host(shost);
12393 scsi_remove_host(shost);
James Smartda0436e2009-05-22 14:51:39 -040012394
James Smartda0436e2009-05-22 14:51:39 -040012395 /* Perform ndlp cleanup on the physical port. The nvme and nvmet
12396 * localports are destroyed after to cleanup all transport memory.
12397 */
12398 lpfc_cleanup(vport);
12399 lpfc_nvmet_destroy_targetport(phba);
12400 lpfc_nvme_destroy_localport(vport);
12401
James Smartc4908502019-01-28 11:14:28 -080012402 /* De-allocate multi-XRI pools */
12403 if (phba->cfg_xri_rebalancing)
12404 lpfc_destroy_multixri_pools(phba);
12405
James Smart281d6192018-01-30 15:58:47 -080012406 /*
12407 * Bring down the SLI Layer. This step disables all interrupts,
12408 * clears the rings, discards all mailbox commands, and resets
12409 * the HBA FCoE function.
12410 */
12411 lpfc_debugfs_terminate(vport);
James Smartda0436e2009-05-22 14:51:39 -040012412
Dick Kennedy19017622017-09-29 17:34:27 -070012413 lpfc_stop_hba_timers(phba);
James Smart523128e2018-09-10 10:30:46 -070012414 spin_lock_irq(&phba->port_list_lock);
James Smartda0436e2009-05-22 14:51:39 -040012415 list_del_init(&vport->listentry);
James Smart523128e2018-09-10 10:30:46 -070012416 spin_unlock_irq(&phba->port_list_lock);
James Smartda0436e2009-05-22 14:51:39 -040012417
James Smart3677a3a2010-09-29 11:19:14 -040012418 /* Perform scsi free before driver resource_unset since scsi
James Smartda0436e2009-05-22 14:51:39 -040012419 * buffers are released to their corresponding pools here.
12420 */
James Smart5e5b5112019-01-28 11:14:22 -080012421 lpfc_io_free(phba);
James Smart01649562017-02-12 13:52:32 -080012422 lpfc_free_iocb_list(phba);
James Smart5e5b5112019-01-28 11:14:22 -080012423 lpfc_sli4_hba_unset(phba);
James Smart67d12732012-08-03 12:36:13 -040012424
James Smart0cdb84e2018-04-09 14:24:26 -070012425 lpfc_unset_driver_resource_phase2(phba);
James Smartda0436e2009-05-22 14:51:39 -040012426 lpfc_sli4_driver_resource_unset(phba);
12427
12428 /* Unmap adapter Control and Doorbell registers */
12429 lpfc_sli4_pci_mem_unset(phba);
12430
12431 /* Release PCI resources and disable device's PCI function */
12432 scsi_host_put(shost);
12433 lpfc_disable_pci_dev(phba);
12434
12435 /* Finally, free the driver's device data structure */
12436 lpfc_hba_free(phba);
12437
12438 return;
12439}
12440
12441/**
12442 * lpfc_pci_suspend_one_s4 - PCI func to suspend SLI-4 device for power mgmnt
12443 * @pdev: pointer to PCI device
12444 * @msg: power management message
12445 *
12446 * This routine is called from the kernel's PCI subsystem to support system
12447 * Power Management (PM) to device with SLI-4 interface spec. When PM invokes
12448 * this method, it quiesces the device by stopping the driver's worker
12449 * thread for the device, turning off device's interrupt and DMA, and bring
12450 * the device offline. Note that as the driver implements the minimum PM
12451 * requirements to a power-aware driver's PM support for suspend/resume -- all
12452 * the possible PM messages (SUSPEND, HIBERNATE, FREEZE) to the suspend()
12453 * method call will be treated as SUSPEND and the driver will fully
12454 * reinitialize its device during resume() method call, the driver will set
12455 * device to PCI_D3hot state in PCI config space instead of setting it
12456 * according to the @msg provided by the PM.
12457 *
12458 * Return code
12459 * 0 - driver suspended the device
12460 * Error otherwise
12461 **/
12462static int
12463lpfc_pci_suspend_one_s4(struct pci_dev *pdev, pm_message_t msg)
12464{
12465 struct Scsi_Host *shost = pci_get_drvdata(pdev);
12466 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
12467
12468 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart75baf692010-06-08 18:31:21 -040012469 "2843 PCI device Power Management suspend.\n");
James Smartda0436e2009-05-22 14:51:39 -040012470
12471 /* Bring down the device */
James Smart618a5232012-06-12 13:54:36 -040012472 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smartda0436e2009-05-22 14:51:39 -040012473 lpfc_offline(phba);
12474 kthread_stop(phba->worker_thread);
12475
12476 /* Disable interrupt from device */
12477 lpfc_sli4_disable_intr(phba);
James Smart5350d872011-10-10 21:33:49 -040012478 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -040012479
12480 /* Save device state to PCI config space */
12481 pci_save_state(pdev);
12482 pci_set_power_state(pdev, PCI_D3hot);
12483
12484 return 0;
12485}
12486
12487/**
12488 * lpfc_pci_resume_one_s4 - PCI func to resume SLI-4 device for power mgmnt
12489 * @pdev: pointer to PCI device
12490 *
12491 * This routine is called from the kernel's PCI subsystem to support system
12492 * Power Management (PM) to device with SLI-4 interface spac. When PM invokes
12493 * this method, it restores the device's PCI config space state and fully
12494 * reinitializes the device and brings it online. Note that as the driver
12495 * implements the minimum PM requirements to a power-aware driver's PM for
12496 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
12497 * to the suspend() method call will be treated as SUSPEND and the driver
12498 * will fully reinitialize its device during resume() method call, the device
12499 * will be set to PCI_D0 directly in PCI config space before restoring the
12500 * state.
12501 *
12502 * Return code
12503 * 0 - driver suspended the device
12504 * Error otherwise
12505 **/
12506static int
12507lpfc_pci_resume_one_s4(struct pci_dev *pdev)
12508{
12509 struct Scsi_Host *shost = pci_get_drvdata(pdev);
12510 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
12511 uint32_t intr_mode;
12512 int error;
12513
12514 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
12515 "0292 PCI device Power Management resume.\n");
12516
12517 /* Restore device state from PCI config space */
12518 pci_set_power_state(pdev, PCI_D0);
12519 pci_restore_state(pdev);
James Smart1dfb5a42010-02-12 14:40:50 -050012520
12521 /*
12522 * As the new kernel behavior of pci_restore_state() API call clears
12523 * device saved_state flag, need to save the restored state again.
12524 */
12525 pci_save_state(pdev);
12526
James Smartda0436e2009-05-22 14:51:39 -040012527 if (pdev->is_busmaster)
12528 pci_set_master(pdev);
12529
12530 /* Startup the kernel thread for this host adapter. */
12531 phba->worker_thread = kthread_run(lpfc_do_work, phba,
12532 "lpfc_worker_%d", phba->brd_no);
12533 if (IS_ERR(phba->worker_thread)) {
12534 error = PTR_ERR(phba->worker_thread);
12535 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12536 "0293 PM resume failed to start worker "
12537 "thread: error=x%x.\n", error);
12538 return error;
12539 }
12540
12541 /* Configure and enable interrupt */
12542 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
12543 if (intr_mode == LPFC_INTR_ERROR) {
12544 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12545 "0294 PM resume Failed to enable interrupt\n");
12546 return -EIO;
12547 } else
12548 phba->intr_mode = intr_mode;
12549
12550 /* Restart HBA and bring it online */
12551 lpfc_sli_brdrestart(phba);
12552 lpfc_online(phba);
12553
12554 /* Log the current active interrupt mode */
12555 lpfc_log_intr_mode(phba, phba->intr_mode);
12556
12557 return 0;
12558}
12559
12560/**
James Smart75baf692010-06-08 18:31:21 -040012561 * lpfc_sli4_prep_dev_for_recover - Prepare SLI4 device for pci slot recover
12562 * @phba: pointer to lpfc hba data structure.
12563 *
12564 * This routine is called to prepare the SLI4 device for PCI slot recover. It
12565 * aborts all the outstanding SCSI I/Os to the pci device.
12566 **/
12567static void
12568lpfc_sli4_prep_dev_for_recover(struct lpfc_hba *phba)
12569{
James Smart75baf692010-06-08 18:31:21 -040012570 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12571 "2828 PCI channel I/O abort preparing for recovery\n");
12572 /*
12573 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
12574 * and let the SCSI mid-layer to retry them to recover.
12575 */
James Smartdb55fba2014-04-04 13:52:02 -040012576 lpfc_sli_abort_fcp_rings(phba);
James Smart75baf692010-06-08 18:31:21 -040012577}
12578
12579/**
12580 * lpfc_sli4_prep_dev_for_reset - Prepare SLI4 device for pci slot reset
12581 * @phba: pointer to lpfc hba data structure.
12582 *
12583 * This routine is called to prepare the SLI4 device for PCI slot reset. It
12584 * disables the device interrupt and pci device, and aborts the internal FCP
12585 * pending I/Os.
12586 **/
12587static void
12588lpfc_sli4_prep_dev_for_reset(struct lpfc_hba *phba)
12589{
12590 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12591 "2826 PCI channel disable preparing for reset\n");
12592
12593 /* Block any management I/Os to the device */
James Smart618a5232012-06-12 13:54:36 -040012594 lpfc_block_mgmt_io(phba, LPFC_MBX_NO_WAIT);
James Smart75baf692010-06-08 18:31:21 -040012595
12596 /* Block all SCSI devices' I/Os on the host */
12597 lpfc_scsi_dev_block(phba);
12598
James Smartea714f32013-04-17 20:18:47 -040012599 /* Flush all driver's outstanding SCSI I/Os as we are to reset */
12600 lpfc_sli_flush_fcp_rings(phba);
12601
James Smartc3725bd2017-11-20 16:00:42 -080012602 /* Flush the outstanding NVME IOs if fc4 type enabled. */
12603 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
12604 lpfc_sli_flush_nvme_rings(phba);
12605
James Smart75baf692010-06-08 18:31:21 -040012606 /* stop all timers */
12607 lpfc_stop_hba_timers(phba);
12608
12609 /* Disable interrupt and pci device */
12610 lpfc_sli4_disable_intr(phba);
James Smart5350d872011-10-10 21:33:49 -040012611 lpfc_sli4_queue_destroy(phba);
James Smart75baf692010-06-08 18:31:21 -040012612 pci_disable_device(phba->pcidev);
James Smart75baf692010-06-08 18:31:21 -040012613}
12614
12615/**
12616 * lpfc_sli4_prep_dev_for_perm_failure - Prepare SLI4 dev for pci slot disable
12617 * @phba: pointer to lpfc hba data structure.
12618 *
12619 * This routine is called to prepare the SLI4 device for PCI slot permanently
12620 * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
12621 * pending I/Os.
12622 **/
12623static void
12624lpfc_sli4_prep_dev_for_perm_failure(struct lpfc_hba *phba)
12625{
12626 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12627 "2827 PCI channel permanent disable for failure\n");
12628
12629 /* Block all SCSI devices' I/Os on the host */
12630 lpfc_scsi_dev_block(phba);
12631
12632 /* stop all timers */
12633 lpfc_stop_hba_timers(phba);
12634
12635 /* Clean up all driver's outstanding SCSI I/Os */
12636 lpfc_sli_flush_fcp_rings(phba);
James Smartc3725bd2017-11-20 16:00:42 -080012637
12638 /* Flush the outstanding NVME IOs if fc4 type enabled. */
12639 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
12640 lpfc_sli_flush_nvme_rings(phba);
James Smart75baf692010-06-08 18:31:21 -040012641}
12642
12643/**
James Smartda0436e2009-05-22 14:51:39 -040012644 * lpfc_io_error_detected_s4 - Method for handling PCI I/O error to SLI-4 device
12645 * @pdev: pointer to PCI device.
12646 * @state: the current PCI connection state.
12647 *
12648 * This routine is called from the PCI subsystem for error handling to device
12649 * with SLI-4 interface spec. This function is called by the PCI subsystem
12650 * after a PCI bus error affecting this device has been detected. When this
12651 * function is invoked, it will need to stop all the I/Os and interrupt(s)
12652 * to the device. Once that is done, it will return PCI_ERS_RESULT_NEED_RESET
12653 * for the PCI subsystem to perform proper recovery as desired.
12654 *
12655 * Return codes
12656 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
12657 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
12658 **/
12659static pci_ers_result_t
12660lpfc_io_error_detected_s4(struct pci_dev *pdev, pci_channel_state_t state)
12661{
James Smart75baf692010-06-08 18:31:21 -040012662 struct Scsi_Host *shost = pci_get_drvdata(pdev);
12663 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
12664
12665 switch (state) {
12666 case pci_channel_io_normal:
12667 /* Non-fatal error, prepare for recovery */
12668 lpfc_sli4_prep_dev_for_recover(phba);
12669 return PCI_ERS_RESULT_CAN_RECOVER;
12670 case pci_channel_io_frozen:
12671 /* Fatal error, prepare for slot reset */
12672 lpfc_sli4_prep_dev_for_reset(phba);
12673 return PCI_ERS_RESULT_NEED_RESET;
12674 case pci_channel_io_perm_failure:
12675 /* Permanent failure, prepare for device down */
12676 lpfc_sli4_prep_dev_for_perm_failure(phba);
12677 return PCI_ERS_RESULT_DISCONNECT;
12678 default:
12679 /* Unknown state, prepare and request slot reset */
12680 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12681 "2825 Unknown PCI error state: x%x\n", state);
12682 lpfc_sli4_prep_dev_for_reset(phba);
12683 return PCI_ERS_RESULT_NEED_RESET;
12684 }
James Smartda0436e2009-05-22 14:51:39 -040012685}
12686
12687/**
12688 * lpfc_io_slot_reset_s4 - Method for restart PCI SLI-4 device from scratch
12689 * @pdev: pointer to PCI device.
12690 *
12691 * This routine is called from the PCI subsystem for error handling to device
12692 * with SLI-4 interface spec. It is called after PCI bus has been reset to
12693 * restart the PCI card from scratch, as if from a cold-boot. During the
12694 * PCI subsystem error recovery, after the driver returns
12695 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
12696 * recovery and then call this routine before calling the .resume method to
12697 * recover the device. This function will initialize the HBA device, enable
12698 * the interrupt, but it will just put the HBA to offline state without
12699 * passing any I/O traffic.
12700 *
12701 * Return codes
12702 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
12703 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
12704 */
12705static pci_ers_result_t
12706lpfc_io_slot_reset_s4(struct pci_dev *pdev)
12707{
James Smart75baf692010-06-08 18:31:21 -040012708 struct Scsi_Host *shost = pci_get_drvdata(pdev);
12709 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
12710 struct lpfc_sli *psli = &phba->sli;
12711 uint32_t intr_mode;
12712
12713 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
12714 if (pci_enable_device_mem(pdev)) {
12715 printk(KERN_ERR "lpfc: Cannot re-enable "
12716 "PCI device after reset.\n");
12717 return PCI_ERS_RESULT_DISCONNECT;
12718 }
12719
12720 pci_restore_state(pdev);
James Smart0a96e972011-07-22 18:37:28 -040012721
12722 /*
12723 * As the new kernel behavior of pci_restore_state() API call clears
12724 * device saved_state flag, need to save the restored state again.
12725 */
12726 pci_save_state(pdev);
12727
James Smart75baf692010-06-08 18:31:21 -040012728 if (pdev->is_busmaster)
12729 pci_set_master(pdev);
12730
12731 spin_lock_irq(&phba->hbalock);
12732 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
12733 spin_unlock_irq(&phba->hbalock);
12734
12735 /* Configure and enable interrupt */
12736 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
12737 if (intr_mode == LPFC_INTR_ERROR) {
12738 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12739 "2824 Cannot re-enable interrupt after "
12740 "slot reset.\n");
12741 return PCI_ERS_RESULT_DISCONNECT;
12742 } else
12743 phba->intr_mode = intr_mode;
12744
12745 /* Log the current active interrupt mode */
12746 lpfc_log_intr_mode(phba, phba->intr_mode);
12747
James Smartda0436e2009-05-22 14:51:39 -040012748 return PCI_ERS_RESULT_RECOVERED;
12749}
12750
12751/**
12752 * lpfc_io_resume_s4 - Method for resuming PCI I/O operation to SLI-4 device
12753 * @pdev: pointer to PCI device
12754 *
12755 * This routine is called from the PCI subsystem for error handling to device
12756 * with SLI-4 interface spec. It is called when kernel error recovery tells
12757 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
12758 * error recovery. After this call, traffic can start to flow from this device
12759 * again.
12760 **/
12761static void
12762lpfc_io_resume_s4(struct pci_dev *pdev)
12763{
James Smart75baf692010-06-08 18:31:21 -040012764 struct Scsi_Host *shost = pci_get_drvdata(pdev);
12765 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
12766
12767 /*
12768 * In case of slot reset, as function reset is performed through
12769 * mailbox command which needs DMA to be enabled, this operation
12770 * has to be moved to the io resume phase. Taking device offline
12771 * will perform the necessary cleanup.
12772 */
12773 if (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE)) {
12774 /* Perform device reset */
James Smart618a5232012-06-12 13:54:36 -040012775 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart75baf692010-06-08 18:31:21 -040012776 lpfc_offline(phba);
12777 lpfc_sli_brdrestart(phba);
12778 /* Bring the device back online */
12779 lpfc_online(phba);
12780 }
James Smartda0436e2009-05-22 14:51:39 -040012781}
12782
12783/**
James Smart3772a992009-05-22 14:50:54 -040012784 * lpfc_pci_probe_one - lpfc PCI probe func to reg dev to PCI subsystem
12785 * @pdev: pointer to PCI device
12786 * @pid: pointer to PCI device identifier
12787 *
12788 * This routine is to be registered to the kernel's PCI subsystem. When an
12789 * Emulex HBA device is presented on PCI bus, the kernel PCI subsystem looks
12790 * at PCI device-specific information of the device and driver to see if the
12791 * driver state that it can support this kind of device. If the match is
12792 * successful, the driver core invokes this routine. This routine dispatches
12793 * the action to the proper SLI-3 or SLI-4 device probing routine, which will
12794 * do all the initialization that it needs to do to handle the HBA device
12795 * properly.
12796 *
12797 * Return code
12798 * 0 - driver can claim the device
12799 * negative value - driver can not claim the device
12800 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080012801static int
James Smart3772a992009-05-22 14:50:54 -040012802lpfc_pci_probe_one(struct pci_dev *pdev, const struct pci_device_id *pid)
12803{
12804 int rc;
James Smart8fa38512009-07-19 10:01:03 -040012805 struct lpfc_sli_intf intf;
James Smart3772a992009-05-22 14:50:54 -040012806
James Smart28baac72010-02-12 14:42:03 -050012807 if (pci_read_config_dword(pdev, LPFC_SLI_INTF, &intf.word0))
James Smart3772a992009-05-22 14:50:54 -040012808 return -ENODEV;
12809
James Smart8fa38512009-07-19 10:01:03 -040012810 if ((bf_get(lpfc_sli_intf_valid, &intf) == LPFC_SLI_INTF_VALID) &&
James Smart28baac72010-02-12 14:42:03 -050012811 (bf_get(lpfc_sli_intf_slirev, &intf) == LPFC_SLI_INTF_REV_SLI4))
James Smartda0436e2009-05-22 14:51:39 -040012812 rc = lpfc_pci_probe_one_s4(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -040012813 else
James Smart3772a992009-05-22 14:50:54 -040012814 rc = lpfc_pci_probe_one_s3(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -040012815
James Smart3772a992009-05-22 14:50:54 -040012816 return rc;
12817}
12818
12819/**
12820 * lpfc_pci_remove_one - lpfc PCI func to unreg dev from PCI subsystem
12821 * @pdev: pointer to PCI device
12822 *
12823 * This routine is to be registered to the kernel's PCI subsystem. When an
12824 * Emulex HBA is removed from PCI bus, the driver core invokes this routine.
12825 * This routine dispatches the action to the proper SLI-3 or SLI-4 device
12826 * remove routine, which will perform all the necessary cleanup for the
12827 * device to be removed from the PCI subsystem properly.
12828 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080012829static void
James Smart3772a992009-05-22 14:50:54 -040012830lpfc_pci_remove_one(struct pci_dev *pdev)
12831{
12832 struct Scsi_Host *shost = pci_get_drvdata(pdev);
12833 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
12834
12835 switch (phba->pci_dev_grp) {
12836 case LPFC_PCI_DEV_LP:
12837 lpfc_pci_remove_one_s3(pdev);
12838 break;
James Smartda0436e2009-05-22 14:51:39 -040012839 case LPFC_PCI_DEV_OC:
12840 lpfc_pci_remove_one_s4(pdev);
12841 break;
James Smart3772a992009-05-22 14:50:54 -040012842 default:
12843 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12844 "1424 Invalid PCI device group: 0x%x\n",
12845 phba->pci_dev_grp);
12846 break;
12847 }
12848 return;
12849}
12850
12851/**
12852 * lpfc_pci_suspend_one - lpfc PCI func to suspend dev for power management
12853 * @pdev: pointer to PCI device
12854 * @msg: power management message
12855 *
12856 * This routine is to be registered to the kernel's PCI subsystem to support
12857 * system Power Management (PM). When PM invokes this method, it dispatches
12858 * the action to the proper SLI-3 or SLI-4 device suspend routine, which will
12859 * suspend the device.
12860 *
12861 * Return code
12862 * 0 - driver suspended the device
12863 * Error otherwise
12864 **/
12865static int
12866lpfc_pci_suspend_one(struct pci_dev *pdev, pm_message_t msg)
12867{
12868 struct Scsi_Host *shost = pci_get_drvdata(pdev);
12869 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
12870 int rc = -ENODEV;
12871
12872 switch (phba->pci_dev_grp) {
12873 case LPFC_PCI_DEV_LP:
12874 rc = lpfc_pci_suspend_one_s3(pdev, msg);
12875 break;
James Smartda0436e2009-05-22 14:51:39 -040012876 case LPFC_PCI_DEV_OC:
12877 rc = lpfc_pci_suspend_one_s4(pdev, msg);
12878 break;
James Smart3772a992009-05-22 14:50:54 -040012879 default:
12880 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12881 "1425 Invalid PCI device group: 0x%x\n",
12882 phba->pci_dev_grp);
12883 break;
12884 }
12885 return rc;
12886}
12887
12888/**
12889 * lpfc_pci_resume_one - lpfc PCI func to resume dev for power management
12890 * @pdev: pointer to PCI device
12891 *
12892 * This routine is to be registered to the kernel's PCI subsystem to support
12893 * system Power Management (PM). When PM invokes this method, it dispatches
12894 * the action to the proper SLI-3 or SLI-4 device resume routine, which will
12895 * resume the device.
12896 *
12897 * Return code
12898 * 0 - driver suspended the device
12899 * Error otherwise
12900 **/
12901static int
12902lpfc_pci_resume_one(struct pci_dev *pdev)
12903{
12904 struct Scsi_Host *shost = pci_get_drvdata(pdev);
12905 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
12906 int rc = -ENODEV;
12907
12908 switch (phba->pci_dev_grp) {
12909 case LPFC_PCI_DEV_LP:
12910 rc = lpfc_pci_resume_one_s3(pdev);
12911 break;
James Smartda0436e2009-05-22 14:51:39 -040012912 case LPFC_PCI_DEV_OC:
12913 rc = lpfc_pci_resume_one_s4(pdev);
12914 break;
James Smart3772a992009-05-22 14:50:54 -040012915 default:
12916 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12917 "1426 Invalid PCI device group: 0x%x\n",
12918 phba->pci_dev_grp);
12919 break;
12920 }
12921 return rc;
12922}
12923
12924/**
12925 * lpfc_io_error_detected - lpfc method for handling PCI I/O error
12926 * @pdev: pointer to PCI device.
12927 * @state: the current PCI connection state.
12928 *
12929 * This routine is registered to the PCI subsystem for error handling. This
12930 * function is called by the PCI subsystem after a PCI bus error affecting
12931 * this device has been detected. When this routine is invoked, it dispatches
12932 * the action to the proper SLI-3 or SLI-4 device error detected handling
12933 * routine, which will perform the proper error detected operation.
12934 *
12935 * Return codes
12936 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
12937 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
12938 **/
12939static pci_ers_result_t
12940lpfc_io_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
12941{
12942 struct Scsi_Host *shost = pci_get_drvdata(pdev);
12943 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
12944 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
12945
12946 switch (phba->pci_dev_grp) {
12947 case LPFC_PCI_DEV_LP:
12948 rc = lpfc_io_error_detected_s3(pdev, state);
12949 break;
James Smartda0436e2009-05-22 14:51:39 -040012950 case LPFC_PCI_DEV_OC:
12951 rc = lpfc_io_error_detected_s4(pdev, state);
12952 break;
James Smart3772a992009-05-22 14:50:54 -040012953 default:
12954 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12955 "1427 Invalid PCI device group: 0x%x\n",
12956 phba->pci_dev_grp);
12957 break;
12958 }
12959 return rc;
12960}
12961
12962/**
12963 * lpfc_io_slot_reset - lpfc method for restart PCI dev from scratch
12964 * @pdev: pointer to PCI device.
12965 *
12966 * This routine is registered to the PCI subsystem for error handling. This
12967 * function is called after PCI bus has been reset to restart the PCI card
12968 * from scratch, as if from a cold-boot. When this routine is invoked, it
12969 * dispatches the action to the proper SLI-3 or SLI-4 device reset handling
12970 * routine, which will perform the proper device reset.
12971 *
12972 * Return codes
12973 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
12974 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
12975 **/
12976static pci_ers_result_t
12977lpfc_io_slot_reset(struct pci_dev *pdev)
12978{
12979 struct Scsi_Host *shost = pci_get_drvdata(pdev);
12980 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
12981 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
12982
12983 switch (phba->pci_dev_grp) {
12984 case LPFC_PCI_DEV_LP:
12985 rc = lpfc_io_slot_reset_s3(pdev);
12986 break;
James Smartda0436e2009-05-22 14:51:39 -040012987 case LPFC_PCI_DEV_OC:
12988 rc = lpfc_io_slot_reset_s4(pdev);
12989 break;
James Smart3772a992009-05-22 14:50:54 -040012990 default:
12991 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12992 "1428 Invalid PCI device group: 0x%x\n",
12993 phba->pci_dev_grp);
12994 break;
12995 }
12996 return rc;
12997}
12998
12999/**
13000 * lpfc_io_resume - lpfc method for resuming PCI I/O operation
13001 * @pdev: pointer to PCI device
13002 *
13003 * This routine is registered to the PCI subsystem for error handling. It
13004 * is called when kernel error recovery tells the lpfc driver that it is
13005 * OK to resume normal PCI operation after PCI bus error recovery. When
13006 * this routine is invoked, it dispatches the action to the proper SLI-3
13007 * or SLI-4 device io_resume routine, which will resume the device operation.
13008 **/
13009static void
13010lpfc_io_resume(struct pci_dev *pdev)
13011{
13012 struct Scsi_Host *shost = pci_get_drvdata(pdev);
13013 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
13014
13015 switch (phba->pci_dev_grp) {
13016 case LPFC_PCI_DEV_LP:
13017 lpfc_io_resume_s3(pdev);
13018 break;
James Smartda0436e2009-05-22 14:51:39 -040013019 case LPFC_PCI_DEV_OC:
13020 lpfc_io_resume_s4(pdev);
13021 break;
James Smart3772a992009-05-22 14:50:54 -040013022 default:
13023 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13024 "1429 Invalid PCI device group: 0x%x\n",
13025 phba->pci_dev_grp);
13026 break;
13027 }
13028 return;
13029}
13030
James Smart1ba981f2014-02-20 09:56:45 -050013031/**
13032 * lpfc_sli4_oas_verify - Verify OAS is supported by this adapter
13033 * @phba: pointer to lpfc hba data structure.
13034 *
13035 * This routine checks to see if OAS is supported for this adapter. If
13036 * supported, the configure Flash Optimized Fabric flag is set. Otherwise,
13037 * the enable oas flag is cleared and the pool created for OAS device data
13038 * is destroyed.
13039 *
13040 **/
13041void
13042lpfc_sli4_oas_verify(struct lpfc_hba *phba)
13043{
13044
13045 if (!phba->cfg_EnableXLane)
13046 return;
13047
13048 if (phba->sli4_hba.pc_sli4_params.oas_supported) {
13049 phba->cfg_fof = 1;
13050 } else {
James Smartf38fa0b2014-04-04 13:52:21 -040013051 phba->cfg_fof = 0;
James Smart1ba981f2014-02-20 09:56:45 -050013052 if (phba->device_data_mem_pool)
13053 mempool_destroy(phba->device_data_mem_pool);
13054 phba->device_data_mem_pool = NULL;
13055 }
13056
13057 return;
13058}
13059
13060/**
James Smartd2cc9bc2018-09-10 10:30:50 -070013061 * lpfc_sli4_ras_init - Verify RAS-FW log is supported by this adapter
13062 * @phba: pointer to lpfc hba data structure.
13063 *
13064 * This routine checks to see if RAS is supported by the adapter. Check the
13065 * function through which RAS support enablement is to be done.
13066 **/
13067void
13068lpfc_sli4_ras_init(struct lpfc_hba *phba)
13069{
13070 switch (phba->pcidev->device) {
13071 case PCI_DEVICE_ID_LANCER_G6_FC:
13072 case PCI_DEVICE_ID_LANCER_G7_FC:
13073 phba->ras_fwlog.ras_hwsupport = true;
James Smartcb349902018-11-29 16:09:27 -080013074 if (phba->cfg_ras_fwlog_func == PCI_FUNC(phba->pcidev->devfn) &&
13075 phba->cfg_ras_fwlog_buffsize)
James Smartd2cc9bc2018-09-10 10:30:50 -070013076 phba->ras_fwlog.ras_enabled = true;
13077 else
13078 phba->ras_fwlog.ras_enabled = false;
13079 break;
13080 default:
13081 phba->ras_fwlog.ras_hwsupport = false;
13082 }
13083}
13084
James Smart1ba981f2014-02-20 09:56:45 -050013085
dea31012005-04-17 16:05:31 -050013086MODULE_DEVICE_TABLE(pci, lpfc_id_table);
13087
Stephen Hemmingera55b2d22012-09-07 09:33:16 -070013088static const struct pci_error_handlers lpfc_err_handler = {
Linas Vepstas8d63f372007-02-14 14:28:36 -060013089 .error_detected = lpfc_io_error_detected,
13090 .slot_reset = lpfc_io_slot_reset,
13091 .resume = lpfc_io_resume,
13092};
13093
dea31012005-04-17 16:05:31 -050013094static struct pci_driver lpfc_driver = {
13095 .name = LPFC_DRIVER_NAME,
13096 .id_table = lpfc_id_table,
13097 .probe = lpfc_pci_probe_one,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080013098 .remove = lpfc_pci_remove_one,
Anton Blanchard85e8a232017-02-13 08:49:20 +110013099 .shutdown = lpfc_pci_remove_one,
James Smart3a55b532008-12-04 22:38:54 -050013100 .suspend = lpfc_pci_suspend_one,
James Smart3772a992009-05-22 14:50:54 -040013101 .resume = lpfc_pci_resume_one,
James Smart2e0fef82007-06-17 19:56:36 -050013102 .err_handler = &lpfc_err_handler,
dea31012005-04-17 16:05:31 -050013103};
13104
James Smart3ef6d242012-01-18 16:23:48 -050013105static const struct file_operations lpfc_mgmt_fop = {
Al Viro858feac2013-04-14 22:39:37 -040013106 .owner = THIS_MODULE,
James Smart3ef6d242012-01-18 16:23:48 -050013107};
13108
13109static struct miscdevice lpfc_mgmt_dev = {
13110 .minor = MISC_DYNAMIC_MINOR,
13111 .name = "lpfcmgmt",
13112 .fops = &lpfc_mgmt_fop,
13113};
13114
James Smarte59058c2008-08-24 21:49:00 -040013115/**
James Smart3621a712009-04-06 18:47:14 -040013116 * lpfc_init - lpfc module initialization routine
James Smarte59058c2008-08-24 21:49:00 -040013117 *
13118 * This routine is to be invoked when the lpfc module is loaded into the
13119 * kernel. The special kernel macro module_init() is used to indicate the
13120 * role of this routine to the kernel as lpfc module entry point.
13121 *
13122 * Return codes
13123 * 0 - successful
13124 * -ENOMEM - FC attach transport failed
13125 * all others - failed
13126 */
dea31012005-04-17 16:05:31 -050013127static int __init
13128lpfc_init(void)
13129{
13130 int error = 0;
13131
13132 printk(LPFC_MODULE_DESC "\n");
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040013133 printk(LPFC_COPYRIGHT "\n");
dea31012005-04-17 16:05:31 -050013134
James Smart3ef6d242012-01-18 16:23:48 -050013135 error = misc_register(&lpfc_mgmt_dev);
13136 if (error)
13137 printk(KERN_ERR "Could not register lpfcmgmt device, "
13138 "misc_register returned with status %d", error);
13139
James Smart458c0832016-07-06 12:36:07 -070013140 lpfc_transport_functions.vport_create = lpfc_vport_create;
13141 lpfc_transport_functions.vport_delete = lpfc_vport_delete;
dea31012005-04-17 16:05:31 -050013142 lpfc_transport_template =
13143 fc_attach_transport(&lpfc_transport_functions);
James Smart7ee5d432007-10-27 13:37:17 -040013144 if (lpfc_transport_template == NULL)
dea31012005-04-17 16:05:31 -050013145 return -ENOMEM;
James Smart458c0832016-07-06 12:36:07 -070013146 lpfc_vport_transport_template =
13147 fc_attach_transport(&lpfc_vport_transport_functions);
13148 if (lpfc_vport_transport_template == NULL) {
13149 fc_release_transport(lpfc_transport_template);
13150 return -ENOMEM;
James Smart7ee5d432007-10-27 13:37:17 -040013151 }
James Smart5fd11082018-03-05 12:04:04 -080013152 lpfc_nvme_cmd_template();
James Smartbd3061b2018-03-05 12:04:05 -080013153 lpfc_nvmet_cmd_template();
James Smart7bb03bb2013-04-17 20:19:16 -040013154
13155 /* Initialize in case vector mapping is needed */
James Smart2ea259e2017-02-12 13:52:27 -080013156 lpfc_present_cpu = num_present_cpus();
James Smart7bb03bb2013-04-17 20:19:16 -040013157
dea31012005-04-17 16:05:31 -050013158 error = pci_register_driver(&lpfc_driver);
James Smart92d7f7b2007-06-17 19:56:38 -050013159 if (error) {
dea31012005-04-17 16:05:31 -050013160 fc_release_transport(lpfc_transport_template);
James Smart458c0832016-07-06 12:36:07 -070013161 fc_release_transport(lpfc_vport_transport_template);
James Smart92d7f7b2007-06-17 19:56:38 -050013162 }
dea31012005-04-17 16:05:31 -050013163
13164 return error;
13165}
13166
James Smarte59058c2008-08-24 21:49:00 -040013167/**
James Smart3621a712009-04-06 18:47:14 -040013168 * lpfc_exit - lpfc module removal routine
James Smarte59058c2008-08-24 21:49:00 -040013169 *
13170 * This routine is invoked when the lpfc module is removed from the kernel.
13171 * The special kernel macro module_exit() is used to indicate the role of
13172 * this routine to the kernel as lpfc module exit point.
13173 */
dea31012005-04-17 16:05:31 -050013174static void __exit
13175lpfc_exit(void)
13176{
James Smart3ef6d242012-01-18 16:23:48 -050013177 misc_deregister(&lpfc_mgmt_dev);
dea31012005-04-17 16:05:31 -050013178 pci_unregister_driver(&lpfc_driver);
13179 fc_release_transport(lpfc_transport_template);
James Smart458c0832016-07-06 12:36:07 -070013180 fc_release_transport(lpfc_vport_transport_template);
James Smart81301a92008-12-04 22:39:46 -050013181 if (_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -040013182 printk(KERN_ERR "9062 BLKGRD: freeing %lu pages for "
13183 "_dump_buf_data at 0x%p\n",
James Smart81301a92008-12-04 22:39:46 -050013184 (1L << _dump_buf_data_order), _dump_buf_data);
13185 free_pages((unsigned long)_dump_buf_data, _dump_buf_data_order);
13186 }
13187
13188 if (_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -040013189 printk(KERN_ERR "9049 BLKGRD: freeing %lu pages for "
13190 "_dump_buf_dif at 0x%p\n",
James Smart81301a92008-12-04 22:39:46 -050013191 (1L << _dump_buf_dif_order), _dump_buf_dif);
13192 free_pages((unsigned long)_dump_buf_dif, _dump_buf_dif_order);
13193 }
Johannes Thumshirn79739672015-08-31 16:48:11 -040013194 idr_destroy(&lpfc_hba_index);
dea31012005-04-17 16:05:31 -050013195}
13196
13197module_init(lpfc_init);
13198module_exit(lpfc_exit);
13199MODULE_LICENSE("GPL");
13200MODULE_DESCRIPTION(LPFC_MODULE_DESC);
James Smartd080abe2017-02-12 13:52:39 -080013201MODULE_AUTHOR("Broadcom");
dea31012005-04-17 16:05:31 -050013202MODULE_VERSION("0:" LPFC_DRIVER_VERSION);