18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * CXL Flash Device Driver
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Written by: Manoj N. Kumar <manoj@linux.vnet.ibm.com>, IBM Corporation
68c2ecf20Sopenharmony_ci *             Matthew R. Ochs <mrochs@linux.vnet.ibm.com>, IBM Corporation
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * Copyright (C) 2015 IBM Corporation
98c2ecf20Sopenharmony_ci */
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#include <linux/delay.h>
128c2ecf20Sopenharmony_ci#include <linux/list.h>
138c2ecf20Sopenharmony_ci#include <linux/module.h>
148c2ecf20Sopenharmony_ci#include <linux/pci.h>
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci#include <asm/unaligned.h>
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci#include <scsi/scsi_cmnd.h>
198c2ecf20Sopenharmony_ci#include <scsi/scsi_host.h>
208c2ecf20Sopenharmony_ci#include <uapi/scsi/cxlflash_ioctl.h>
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci#include "main.h"
238c2ecf20Sopenharmony_ci#include "sislite.h"
248c2ecf20Sopenharmony_ci#include "common.h"
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ciMODULE_DESCRIPTION(CXLFLASH_ADAPTER_NAME);
278c2ecf20Sopenharmony_ciMODULE_AUTHOR("Manoj N. Kumar <manoj@linux.vnet.ibm.com>");
288c2ecf20Sopenharmony_ciMODULE_AUTHOR("Matthew R. Ochs <mrochs@linux.vnet.ibm.com>");
298c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_cistatic struct class *cxlflash_class;
328c2ecf20Sopenharmony_cistatic u32 cxlflash_major;
338c2ecf20Sopenharmony_cistatic DECLARE_BITMAP(cxlflash_minor, CXLFLASH_MAX_ADAPTERS);
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci/**
368c2ecf20Sopenharmony_ci * process_cmd_err() - command error handler
378c2ecf20Sopenharmony_ci * @cmd:	AFU command that experienced the error.
388c2ecf20Sopenharmony_ci * @scp:	SCSI command associated with the AFU command in error.
398c2ecf20Sopenharmony_ci *
408c2ecf20Sopenharmony_ci * Translates error bits from AFU command to SCSI command results.
418c2ecf20Sopenharmony_ci */
428c2ecf20Sopenharmony_cistatic void process_cmd_err(struct afu_cmd *cmd, struct scsi_cmnd *scp)
438c2ecf20Sopenharmony_ci{
448c2ecf20Sopenharmony_ci	struct afu *afu = cmd->parent;
458c2ecf20Sopenharmony_ci	struct cxlflash_cfg *cfg = afu->parent;
468c2ecf20Sopenharmony_ci	struct device *dev = &cfg->dev->dev;
478c2ecf20Sopenharmony_ci	struct sisl_ioasa *ioasa;
488c2ecf20Sopenharmony_ci	u32 resid;
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci	ioasa = &(cmd->sa);
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci	if (ioasa->rc.flags & SISL_RC_FLAGS_UNDERRUN) {
538c2ecf20Sopenharmony_ci		resid = ioasa->resid;
548c2ecf20Sopenharmony_ci		scsi_set_resid(scp, resid);
558c2ecf20Sopenharmony_ci		dev_dbg(dev, "%s: cmd underrun cmd = %p scp = %p, resid = %d\n",
568c2ecf20Sopenharmony_ci			__func__, cmd, scp, resid);
578c2ecf20Sopenharmony_ci	}
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci	if (ioasa->rc.flags & SISL_RC_FLAGS_OVERRUN) {
608c2ecf20Sopenharmony_ci		dev_dbg(dev, "%s: cmd underrun cmd = %p scp = %p\n",
618c2ecf20Sopenharmony_ci			__func__, cmd, scp);
628c2ecf20Sopenharmony_ci		scp->result = (DID_ERROR << 16);
638c2ecf20Sopenharmony_ci	}
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci	dev_dbg(dev, "%s: cmd failed afu_rc=%02x scsi_rc=%02x fc_rc=%02x "
668c2ecf20Sopenharmony_ci		"afu_extra=%02x scsi_extra=%02x fc_extra=%02x\n", __func__,
678c2ecf20Sopenharmony_ci		ioasa->rc.afu_rc, ioasa->rc.scsi_rc, ioasa->rc.fc_rc,
688c2ecf20Sopenharmony_ci		ioasa->afu_extra, ioasa->scsi_extra, ioasa->fc_extra);
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci	if (ioasa->rc.scsi_rc) {
718c2ecf20Sopenharmony_ci		/* We have a SCSI status */
728c2ecf20Sopenharmony_ci		if (ioasa->rc.flags & SISL_RC_FLAGS_SENSE_VALID) {
738c2ecf20Sopenharmony_ci			memcpy(scp->sense_buffer, ioasa->sense_data,
748c2ecf20Sopenharmony_ci			       SISL_SENSE_DATA_LEN);
758c2ecf20Sopenharmony_ci			scp->result = ioasa->rc.scsi_rc;
768c2ecf20Sopenharmony_ci		} else
778c2ecf20Sopenharmony_ci			scp->result = ioasa->rc.scsi_rc | (DID_ERROR << 16);
788c2ecf20Sopenharmony_ci	}
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci	/*
818c2ecf20Sopenharmony_ci	 * We encountered an error. Set scp->result based on nature
828c2ecf20Sopenharmony_ci	 * of error.
838c2ecf20Sopenharmony_ci	 */
848c2ecf20Sopenharmony_ci	if (ioasa->rc.fc_rc) {
858c2ecf20Sopenharmony_ci		/* We have an FC status */
868c2ecf20Sopenharmony_ci		switch (ioasa->rc.fc_rc) {
878c2ecf20Sopenharmony_ci		case SISL_FC_RC_LINKDOWN:
888c2ecf20Sopenharmony_ci			scp->result = (DID_REQUEUE << 16);
898c2ecf20Sopenharmony_ci			break;
908c2ecf20Sopenharmony_ci		case SISL_FC_RC_RESID:
918c2ecf20Sopenharmony_ci			/* This indicates an FCP resid underrun */
928c2ecf20Sopenharmony_ci			if (!(ioasa->rc.flags & SISL_RC_FLAGS_OVERRUN)) {
938c2ecf20Sopenharmony_ci				/* If the SISL_RC_FLAGS_OVERRUN flag was set,
948c2ecf20Sopenharmony_ci				 * then we will handle this error else where.
958c2ecf20Sopenharmony_ci				 * If not then we must handle it here.
968c2ecf20Sopenharmony_ci				 * This is probably an AFU bug.
978c2ecf20Sopenharmony_ci				 */
988c2ecf20Sopenharmony_ci				scp->result = (DID_ERROR << 16);
998c2ecf20Sopenharmony_ci			}
1008c2ecf20Sopenharmony_ci			break;
1018c2ecf20Sopenharmony_ci		case SISL_FC_RC_RESIDERR:
1028c2ecf20Sopenharmony_ci			/* Resid mismatch between adapter and device */
1038c2ecf20Sopenharmony_ci		case SISL_FC_RC_TGTABORT:
1048c2ecf20Sopenharmony_ci		case SISL_FC_RC_ABORTOK:
1058c2ecf20Sopenharmony_ci		case SISL_FC_RC_ABORTFAIL:
1068c2ecf20Sopenharmony_ci		case SISL_FC_RC_NOLOGI:
1078c2ecf20Sopenharmony_ci		case SISL_FC_RC_ABORTPEND:
1088c2ecf20Sopenharmony_ci		case SISL_FC_RC_WRABORTPEND:
1098c2ecf20Sopenharmony_ci		case SISL_FC_RC_NOEXP:
1108c2ecf20Sopenharmony_ci		case SISL_FC_RC_INUSE:
1118c2ecf20Sopenharmony_ci			scp->result = (DID_ERROR << 16);
1128c2ecf20Sopenharmony_ci			break;
1138c2ecf20Sopenharmony_ci		}
1148c2ecf20Sopenharmony_ci	}
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci	if (ioasa->rc.afu_rc) {
1178c2ecf20Sopenharmony_ci		/* We have an AFU error */
1188c2ecf20Sopenharmony_ci		switch (ioasa->rc.afu_rc) {
1198c2ecf20Sopenharmony_ci		case SISL_AFU_RC_NO_CHANNELS:
1208c2ecf20Sopenharmony_ci			scp->result = (DID_NO_CONNECT << 16);
1218c2ecf20Sopenharmony_ci			break;
1228c2ecf20Sopenharmony_ci		case SISL_AFU_RC_DATA_DMA_ERR:
1238c2ecf20Sopenharmony_ci			switch (ioasa->afu_extra) {
1248c2ecf20Sopenharmony_ci			case SISL_AFU_DMA_ERR_PAGE_IN:
1258c2ecf20Sopenharmony_ci				/* Retry */
1268c2ecf20Sopenharmony_ci				scp->result = (DID_IMM_RETRY << 16);
1278c2ecf20Sopenharmony_ci				break;
1288c2ecf20Sopenharmony_ci			case SISL_AFU_DMA_ERR_INVALID_EA:
1298c2ecf20Sopenharmony_ci			default:
1308c2ecf20Sopenharmony_ci				scp->result = (DID_ERROR << 16);
1318c2ecf20Sopenharmony_ci			}
1328c2ecf20Sopenharmony_ci			break;
1338c2ecf20Sopenharmony_ci		case SISL_AFU_RC_OUT_OF_DATA_BUFS:
1348c2ecf20Sopenharmony_ci			/* Retry */
1358c2ecf20Sopenharmony_ci			scp->result = (DID_ALLOC_FAILURE << 16);
1368c2ecf20Sopenharmony_ci			break;
1378c2ecf20Sopenharmony_ci		default:
1388c2ecf20Sopenharmony_ci			scp->result = (DID_ERROR << 16);
1398c2ecf20Sopenharmony_ci		}
1408c2ecf20Sopenharmony_ci	}
1418c2ecf20Sopenharmony_ci}
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci/**
1448c2ecf20Sopenharmony_ci * cmd_complete() - command completion handler
1458c2ecf20Sopenharmony_ci * @cmd:	AFU command that has completed.
1468c2ecf20Sopenharmony_ci *
1478c2ecf20Sopenharmony_ci * For SCSI commands this routine prepares and submits commands that have
1488c2ecf20Sopenharmony_ci * either completed or timed out to the SCSI stack. For internal commands
1498c2ecf20Sopenharmony_ci * (TMF or AFU), this routine simply notifies the originator that the
1508c2ecf20Sopenharmony_ci * command has completed.
1518c2ecf20Sopenharmony_ci */
1528c2ecf20Sopenharmony_cistatic void cmd_complete(struct afu_cmd *cmd)
1538c2ecf20Sopenharmony_ci{
1548c2ecf20Sopenharmony_ci	struct scsi_cmnd *scp;
1558c2ecf20Sopenharmony_ci	ulong lock_flags;
1568c2ecf20Sopenharmony_ci	struct afu *afu = cmd->parent;
1578c2ecf20Sopenharmony_ci	struct cxlflash_cfg *cfg = afu->parent;
1588c2ecf20Sopenharmony_ci	struct device *dev = &cfg->dev->dev;
1598c2ecf20Sopenharmony_ci	struct hwq *hwq = get_hwq(afu, cmd->hwq_index);
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci	spin_lock_irqsave(&hwq->hsq_slock, lock_flags);
1628c2ecf20Sopenharmony_ci	list_del(&cmd->list);
1638c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&hwq->hsq_slock, lock_flags);
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci	if (cmd->scp) {
1668c2ecf20Sopenharmony_ci		scp = cmd->scp;
1678c2ecf20Sopenharmony_ci		if (unlikely(cmd->sa.ioasc))
1688c2ecf20Sopenharmony_ci			process_cmd_err(cmd, scp);
1698c2ecf20Sopenharmony_ci		else
1708c2ecf20Sopenharmony_ci			scp->result = (DID_OK << 16);
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ci		dev_dbg_ratelimited(dev, "%s:scp=%p result=%08x ioasc=%08x\n",
1738c2ecf20Sopenharmony_ci				    __func__, scp, scp->result, cmd->sa.ioasc);
1748c2ecf20Sopenharmony_ci		scp->scsi_done(scp);
1758c2ecf20Sopenharmony_ci	} else if (cmd->cmd_tmf) {
1768c2ecf20Sopenharmony_ci		spin_lock_irqsave(&cfg->tmf_slock, lock_flags);
1778c2ecf20Sopenharmony_ci		cfg->tmf_active = false;
1788c2ecf20Sopenharmony_ci		wake_up_all_locked(&cfg->tmf_waitq);
1798c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
1808c2ecf20Sopenharmony_ci	} else
1818c2ecf20Sopenharmony_ci		complete(&cmd->cevent);
1828c2ecf20Sopenharmony_ci}
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci/**
1858c2ecf20Sopenharmony_ci * flush_pending_cmds() - flush all pending commands on this hardware queue
1868c2ecf20Sopenharmony_ci * @hwq:	Hardware queue to flush.
1878c2ecf20Sopenharmony_ci *
1888c2ecf20Sopenharmony_ci * The hardware send queue lock associated with this hardware queue must be
1898c2ecf20Sopenharmony_ci * held when calling this routine.
1908c2ecf20Sopenharmony_ci */
1918c2ecf20Sopenharmony_cistatic void flush_pending_cmds(struct hwq *hwq)
1928c2ecf20Sopenharmony_ci{
1938c2ecf20Sopenharmony_ci	struct cxlflash_cfg *cfg = hwq->afu->parent;
1948c2ecf20Sopenharmony_ci	struct afu_cmd *cmd, *tmp;
1958c2ecf20Sopenharmony_ci	struct scsi_cmnd *scp;
1968c2ecf20Sopenharmony_ci	ulong lock_flags;
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ci	list_for_each_entry_safe(cmd, tmp, &hwq->pending_cmds, list) {
1998c2ecf20Sopenharmony_ci		/* Bypass command when on a doneq, cmd_complete() will handle */
2008c2ecf20Sopenharmony_ci		if (!list_empty(&cmd->queue))
2018c2ecf20Sopenharmony_ci			continue;
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_ci		list_del(&cmd->list);
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_ci		if (cmd->scp) {
2068c2ecf20Sopenharmony_ci			scp = cmd->scp;
2078c2ecf20Sopenharmony_ci			scp->result = (DID_IMM_RETRY << 16);
2088c2ecf20Sopenharmony_ci			scp->scsi_done(scp);
2098c2ecf20Sopenharmony_ci		} else {
2108c2ecf20Sopenharmony_ci			cmd->cmd_aborted = true;
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_ci			if (cmd->cmd_tmf) {
2138c2ecf20Sopenharmony_ci				spin_lock_irqsave(&cfg->tmf_slock, lock_flags);
2148c2ecf20Sopenharmony_ci				cfg->tmf_active = false;
2158c2ecf20Sopenharmony_ci				wake_up_all_locked(&cfg->tmf_waitq);
2168c2ecf20Sopenharmony_ci				spin_unlock_irqrestore(&cfg->tmf_slock,
2178c2ecf20Sopenharmony_ci						       lock_flags);
2188c2ecf20Sopenharmony_ci			} else
2198c2ecf20Sopenharmony_ci				complete(&cmd->cevent);
2208c2ecf20Sopenharmony_ci		}
2218c2ecf20Sopenharmony_ci	}
2228c2ecf20Sopenharmony_ci}
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_ci/**
2258c2ecf20Sopenharmony_ci * context_reset() - reset context via specified register
2268c2ecf20Sopenharmony_ci * @hwq:	Hardware queue owning the context to be reset.
2278c2ecf20Sopenharmony_ci * @reset_reg:	MMIO register to perform reset.
2288c2ecf20Sopenharmony_ci *
2298c2ecf20Sopenharmony_ci * When the reset is successful, the SISLite specification guarantees that
2308c2ecf20Sopenharmony_ci * the AFU has aborted all currently pending I/O. Accordingly, these commands
2318c2ecf20Sopenharmony_ci * must be flushed.
2328c2ecf20Sopenharmony_ci *
2338c2ecf20Sopenharmony_ci * Return: 0 on success, -errno on failure
2348c2ecf20Sopenharmony_ci */
2358c2ecf20Sopenharmony_cistatic int context_reset(struct hwq *hwq, __be64 __iomem *reset_reg)
2368c2ecf20Sopenharmony_ci{
2378c2ecf20Sopenharmony_ci	struct cxlflash_cfg *cfg = hwq->afu->parent;
2388c2ecf20Sopenharmony_ci	struct device *dev = &cfg->dev->dev;
2398c2ecf20Sopenharmony_ci	int rc = -ETIMEDOUT;
2408c2ecf20Sopenharmony_ci	int nretry = 0;
2418c2ecf20Sopenharmony_ci	u64 val = 0x1;
2428c2ecf20Sopenharmony_ci	ulong lock_flags;
2438c2ecf20Sopenharmony_ci
2448c2ecf20Sopenharmony_ci	dev_dbg(dev, "%s: hwq=%p\n", __func__, hwq);
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_ci	spin_lock_irqsave(&hwq->hsq_slock, lock_flags);
2478c2ecf20Sopenharmony_ci
2488c2ecf20Sopenharmony_ci	writeq_be(val, reset_reg);
2498c2ecf20Sopenharmony_ci	do {
2508c2ecf20Sopenharmony_ci		val = readq_be(reset_reg);
2518c2ecf20Sopenharmony_ci		if ((val & 0x1) == 0x0) {
2528c2ecf20Sopenharmony_ci			rc = 0;
2538c2ecf20Sopenharmony_ci			break;
2548c2ecf20Sopenharmony_ci		}
2558c2ecf20Sopenharmony_ci
2568c2ecf20Sopenharmony_ci		/* Double delay each time */
2578c2ecf20Sopenharmony_ci		udelay(1 << nretry);
2588c2ecf20Sopenharmony_ci	} while (nretry++ < MC_ROOM_RETRY_CNT);
2598c2ecf20Sopenharmony_ci
2608c2ecf20Sopenharmony_ci	if (!rc)
2618c2ecf20Sopenharmony_ci		flush_pending_cmds(hwq);
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&hwq->hsq_slock, lock_flags);
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_ci	dev_dbg(dev, "%s: returning rc=%d, val=%016llx nretry=%d\n",
2668c2ecf20Sopenharmony_ci		__func__, rc, val, nretry);
2678c2ecf20Sopenharmony_ci	return rc;
2688c2ecf20Sopenharmony_ci}
2698c2ecf20Sopenharmony_ci
2708c2ecf20Sopenharmony_ci/**
2718c2ecf20Sopenharmony_ci * context_reset_ioarrin() - reset context via IOARRIN register
2728c2ecf20Sopenharmony_ci * @hwq:	Hardware queue owning the context to be reset.
2738c2ecf20Sopenharmony_ci *
2748c2ecf20Sopenharmony_ci * Return: 0 on success, -errno on failure
2758c2ecf20Sopenharmony_ci */
2768c2ecf20Sopenharmony_cistatic int context_reset_ioarrin(struct hwq *hwq)
2778c2ecf20Sopenharmony_ci{
2788c2ecf20Sopenharmony_ci	return context_reset(hwq, &hwq->host_map->ioarrin);
2798c2ecf20Sopenharmony_ci}
2808c2ecf20Sopenharmony_ci
2818c2ecf20Sopenharmony_ci/**
2828c2ecf20Sopenharmony_ci * context_reset_sq() - reset context via SQ_CONTEXT_RESET register
2838c2ecf20Sopenharmony_ci * @hwq:	Hardware queue owning the context to be reset.
2848c2ecf20Sopenharmony_ci *
2858c2ecf20Sopenharmony_ci * Return: 0 on success, -errno on failure
2868c2ecf20Sopenharmony_ci */
2878c2ecf20Sopenharmony_cistatic int context_reset_sq(struct hwq *hwq)
2888c2ecf20Sopenharmony_ci{
2898c2ecf20Sopenharmony_ci	return context_reset(hwq, &hwq->host_map->sq_ctx_reset);
2908c2ecf20Sopenharmony_ci}
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_ci/**
2938c2ecf20Sopenharmony_ci * send_cmd_ioarrin() - sends an AFU command via IOARRIN register
2948c2ecf20Sopenharmony_ci * @afu:	AFU associated with the host.
2958c2ecf20Sopenharmony_ci * @cmd:	AFU command to send.
2968c2ecf20Sopenharmony_ci *
2978c2ecf20Sopenharmony_ci * Return:
2988c2ecf20Sopenharmony_ci *	0 on success, SCSI_MLQUEUE_HOST_BUSY on failure
2998c2ecf20Sopenharmony_ci */
3008c2ecf20Sopenharmony_cistatic int send_cmd_ioarrin(struct afu *afu, struct afu_cmd *cmd)
3018c2ecf20Sopenharmony_ci{
3028c2ecf20Sopenharmony_ci	struct cxlflash_cfg *cfg = afu->parent;
3038c2ecf20Sopenharmony_ci	struct device *dev = &cfg->dev->dev;
3048c2ecf20Sopenharmony_ci	struct hwq *hwq = get_hwq(afu, cmd->hwq_index);
3058c2ecf20Sopenharmony_ci	int rc = 0;
3068c2ecf20Sopenharmony_ci	s64 room;
3078c2ecf20Sopenharmony_ci	ulong lock_flags;
3088c2ecf20Sopenharmony_ci
3098c2ecf20Sopenharmony_ci	/*
3108c2ecf20Sopenharmony_ci	 * To avoid the performance penalty of MMIO, spread the update of
3118c2ecf20Sopenharmony_ci	 * 'room' over multiple commands.
3128c2ecf20Sopenharmony_ci	 */
3138c2ecf20Sopenharmony_ci	spin_lock_irqsave(&hwq->hsq_slock, lock_flags);
3148c2ecf20Sopenharmony_ci	if (--hwq->room < 0) {
3158c2ecf20Sopenharmony_ci		room = readq_be(&hwq->host_map->cmd_room);
3168c2ecf20Sopenharmony_ci		if (room <= 0) {
3178c2ecf20Sopenharmony_ci			dev_dbg_ratelimited(dev, "%s: no cmd_room to send "
3188c2ecf20Sopenharmony_ci					    "0x%02X, room=0x%016llX\n",
3198c2ecf20Sopenharmony_ci					    __func__, cmd->rcb.cdb[0], room);
3208c2ecf20Sopenharmony_ci			hwq->room = 0;
3218c2ecf20Sopenharmony_ci			rc = SCSI_MLQUEUE_HOST_BUSY;
3228c2ecf20Sopenharmony_ci			goto out;
3238c2ecf20Sopenharmony_ci		}
3248c2ecf20Sopenharmony_ci		hwq->room = room - 1;
3258c2ecf20Sopenharmony_ci	}
3268c2ecf20Sopenharmony_ci
3278c2ecf20Sopenharmony_ci	list_add(&cmd->list, &hwq->pending_cmds);
3288c2ecf20Sopenharmony_ci	writeq_be((u64)&cmd->rcb, &hwq->host_map->ioarrin);
3298c2ecf20Sopenharmony_ciout:
3308c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&hwq->hsq_slock, lock_flags);
3318c2ecf20Sopenharmony_ci	dev_dbg_ratelimited(dev, "%s: cmd=%p len=%u ea=%016llx rc=%d\n",
3328c2ecf20Sopenharmony_ci		__func__, cmd, cmd->rcb.data_len, cmd->rcb.data_ea, rc);
3338c2ecf20Sopenharmony_ci	return rc;
3348c2ecf20Sopenharmony_ci}
3358c2ecf20Sopenharmony_ci
3368c2ecf20Sopenharmony_ci/**
3378c2ecf20Sopenharmony_ci * send_cmd_sq() - sends an AFU command via SQ ring
3388c2ecf20Sopenharmony_ci * @afu:	AFU associated with the host.
3398c2ecf20Sopenharmony_ci * @cmd:	AFU command to send.
3408c2ecf20Sopenharmony_ci *
3418c2ecf20Sopenharmony_ci * Return:
3428c2ecf20Sopenharmony_ci *	0 on success, SCSI_MLQUEUE_HOST_BUSY on failure
3438c2ecf20Sopenharmony_ci */
3448c2ecf20Sopenharmony_cistatic int send_cmd_sq(struct afu *afu, struct afu_cmd *cmd)
3458c2ecf20Sopenharmony_ci{
3468c2ecf20Sopenharmony_ci	struct cxlflash_cfg *cfg = afu->parent;
3478c2ecf20Sopenharmony_ci	struct device *dev = &cfg->dev->dev;
3488c2ecf20Sopenharmony_ci	struct hwq *hwq = get_hwq(afu, cmd->hwq_index);
3498c2ecf20Sopenharmony_ci	int rc = 0;
3508c2ecf20Sopenharmony_ci	int newval;
3518c2ecf20Sopenharmony_ci	ulong lock_flags;
3528c2ecf20Sopenharmony_ci
3538c2ecf20Sopenharmony_ci	newval = atomic_dec_if_positive(&hwq->hsq_credits);
3548c2ecf20Sopenharmony_ci	if (newval <= 0) {
3558c2ecf20Sopenharmony_ci		rc = SCSI_MLQUEUE_HOST_BUSY;
3568c2ecf20Sopenharmony_ci		goto out;
3578c2ecf20Sopenharmony_ci	}
3588c2ecf20Sopenharmony_ci
3598c2ecf20Sopenharmony_ci	cmd->rcb.ioasa = &cmd->sa;
3608c2ecf20Sopenharmony_ci
3618c2ecf20Sopenharmony_ci	spin_lock_irqsave(&hwq->hsq_slock, lock_flags);
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_ci	*hwq->hsq_curr = cmd->rcb;
3648c2ecf20Sopenharmony_ci	if (hwq->hsq_curr < hwq->hsq_end)
3658c2ecf20Sopenharmony_ci		hwq->hsq_curr++;
3668c2ecf20Sopenharmony_ci	else
3678c2ecf20Sopenharmony_ci		hwq->hsq_curr = hwq->hsq_start;
3688c2ecf20Sopenharmony_ci
3698c2ecf20Sopenharmony_ci	list_add(&cmd->list, &hwq->pending_cmds);
3708c2ecf20Sopenharmony_ci	writeq_be((u64)hwq->hsq_curr, &hwq->host_map->sq_tail);
3718c2ecf20Sopenharmony_ci
3728c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&hwq->hsq_slock, lock_flags);
3738c2ecf20Sopenharmony_ciout:
3748c2ecf20Sopenharmony_ci	dev_dbg(dev, "%s: cmd=%p len=%u ea=%016llx ioasa=%p rc=%d curr=%p "
3758c2ecf20Sopenharmony_ci	       "head=%016llx tail=%016llx\n", __func__, cmd, cmd->rcb.data_len,
3768c2ecf20Sopenharmony_ci	       cmd->rcb.data_ea, cmd->rcb.ioasa, rc, hwq->hsq_curr,
3778c2ecf20Sopenharmony_ci	       readq_be(&hwq->host_map->sq_head),
3788c2ecf20Sopenharmony_ci	       readq_be(&hwq->host_map->sq_tail));
3798c2ecf20Sopenharmony_ci	return rc;
3808c2ecf20Sopenharmony_ci}
3818c2ecf20Sopenharmony_ci
3828c2ecf20Sopenharmony_ci/**
3838c2ecf20Sopenharmony_ci * wait_resp() - polls for a response or timeout to a sent AFU command
3848c2ecf20Sopenharmony_ci * @afu:	AFU associated with the host.
3858c2ecf20Sopenharmony_ci * @cmd:	AFU command that was sent.
3868c2ecf20Sopenharmony_ci *
3878c2ecf20Sopenharmony_ci * Return: 0 on success, -errno on failure
3888c2ecf20Sopenharmony_ci */
3898c2ecf20Sopenharmony_cistatic int wait_resp(struct afu *afu, struct afu_cmd *cmd)
3908c2ecf20Sopenharmony_ci{
3918c2ecf20Sopenharmony_ci	struct cxlflash_cfg *cfg = afu->parent;
3928c2ecf20Sopenharmony_ci	struct device *dev = &cfg->dev->dev;
3938c2ecf20Sopenharmony_ci	int rc = 0;
3948c2ecf20Sopenharmony_ci	ulong timeout = msecs_to_jiffies(cmd->rcb.timeout * 2 * 1000);
3958c2ecf20Sopenharmony_ci
3968c2ecf20Sopenharmony_ci	timeout = wait_for_completion_timeout(&cmd->cevent, timeout);
3978c2ecf20Sopenharmony_ci	if (!timeout)
3988c2ecf20Sopenharmony_ci		rc = -ETIMEDOUT;
3998c2ecf20Sopenharmony_ci
4008c2ecf20Sopenharmony_ci	if (cmd->cmd_aborted)
4018c2ecf20Sopenharmony_ci		rc = -EAGAIN;
4028c2ecf20Sopenharmony_ci
4038c2ecf20Sopenharmony_ci	if (unlikely(cmd->sa.ioasc != 0)) {
4048c2ecf20Sopenharmony_ci		dev_err(dev, "%s: cmd %02x failed, ioasc=%08x\n",
4058c2ecf20Sopenharmony_ci			__func__, cmd->rcb.cdb[0], cmd->sa.ioasc);
4068c2ecf20Sopenharmony_ci		rc = -EIO;
4078c2ecf20Sopenharmony_ci	}
4088c2ecf20Sopenharmony_ci
4098c2ecf20Sopenharmony_ci	return rc;
4108c2ecf20Sopenharmony_ci}
4118c2ecf20Sopenharmony_ci
4128c2ecf20Sopenharmony_ci/**
4138c2ecf20Sopenharmony_ci * cmd_to_target_hwq() - selects a target hardware queue for a SCSI command
4148c2ecf20Sopenharmony_ci * @host:	SCSI host associated with device.
4158c2ecf20Sopenharmony_ci * @scp:	SCSI command to send.
4168c2ecf20Sopenharmony_ci * @afu:	SCSI command to send.
4178c2ecf20Sopenharmony_ci *
4188c2ecf20Sopenharmony_ci * Hashes a command based upon the hardware queue mode.
4198c2ecf20Sopenharmony_ci *
4208c2ecf20Sopenharmony_ci * Return: Trusted index of target hardware queue
4218c2ecf20Sopenharmony_ci */
4228c2ecf20Sopenharmony_cistatic u32 cmd_to_target_hwq(struct Scsi_Host *host, struct scsi_cmnd *scp,
4238c2ecf20Sopenharmony_ci			     struct afu *afu)
4248c2ecf20Sopenharmony_ci{
4258c2ecf20Sopenharmony_ci	u32 tag;
4268c2ecf20Sopenharmony_ci	u32 hwq = 0;
4278c2ecf20Sopenharmony_ci
4288c2ecf20Sopenharmony_ci	if (afu->num_hwqs == 1)
4298c2ecf20Sopenharmony_ci		return 0;
4308c2ecf20Sopenharmony_ci
4318c2ecf20Sopenharmony_ci	switch (afu->hwq_mode) {
4328c2ecf20Sopenharmony_ci	case HWQ_MODE_RR:
4338c2ecf20Sopenharmony_ci		hwq = afu->hwq_rr_count++ % afu->num_hwqs;
4348c2ecf20Sopenharmony_ci		break;
4358c2ecf20Sopenharmony_ci	case HWQ_MODE_TAG:
4368c2ecf20Sopenharmony_ci		tag = blk_mq_unique_tag(scp->request);
4378c2ecf20Sopenharmony_ci		hwq = blk_mq_unique_tag_to_hwq(tag);
4388c2ecf20Sopenharmony_ci		break;
4398c2ecf20Sopenharmony_ci	case HWQ_MODE_CPU:
4408c2ecf20Sopenharmony_ci		hwq = smp_processor_id() % afu->num_hwqs;
4418c2ecf20Sopenharmony_ci		break;
4428c2ecf20Sopenharmony_ci	default:
4438c2ecf20Sopenharmony_ci		WARN_ON_ONCE(1);
4448c2ecf20Sopenharmony_ci	}
4458c2ecf20Sopenharmony_ci
4468c2ecf20Sopenharmony_ci	return hwq;
4478c2ecf20Sopenharmony_ci}
4488c2ecf20Sopenharmony_ci
4498c2ecf20Sopenharmony_ci/**
4508c2ecf20Sopenharmony_ci * send_tmf() - sends a Task Management Function (TMF)
4518c2ecf20Sopenharmony_ci * @cfg:	Internal structure associated with the host.
4528c2ecf20Sopenharmony_ci * @sdev:	SCSI device destined for TMF.
4538c2ecf20Sopenharmony_ci * @tmfcmd:	TMF command to send.
4548c2ecf20Sopenharmony_ci *
4558c2ecf20Sopenharmony_ci * Return:
4568c2ecf20Sopenharmony_ci *	0 on success, SCSI_MLQUEUE_HOST_BUSY or -errno on failure
4578c2ecf20Sopenharmony_ci */
4588c2ecf20Sopenharmony_cistatic int send_tmf(struct cxlflash_cfg *cfg, struct scsi_device *sdev,
4598c2ecf20Sopenharmony_ci		    u64 tmfcmd)
4608c2ecf20Sopenharmony_ci{
4618c2ecf20Sopenharmony_ci	struct afu *afu = cfg->afu;
4628c2ecf20Sopenharmony_ci	struct afu_cmd *cmd = NULL;
4638c2ecf20Sopenharmony_ci	struct device *dev = &cfg->dev->dev;
4648c2ecf20Sopenharmony_ci	struct hwq *hwq = get_hwq(afu, PRIMARY_HWQ);
4658c2ecf20Sopenharmony_ci	bool needs_deletion = false;
4668c2ecf20Sopenharmony_ci	char *buf = NULL;
4678c2ecf20Sopenharmony_ci	ulong lock_flags;
4688c2ecf20Sopenharmony_ci	int rc = 0;
4698c2ecf20Sopenharmony_ci	ulong to;
4708c2ecf20Sopenharmony_ci
4718c2ecf20Sopenharmony_ci	buf = kzalloc(sizeof(*cmd) + __alignof__(*cmd) - 1, GFP_KERNEL);
4728c2ecf20Sopenharmony_ci	if (unlikely(!buf)) {
4738c2ecf20Sopenharmony_ci		dev_err(dev, "%s: no memory for command\n", __func__);
4748c2ecf20Sopenharmony_ci		rc = -ENOMEM;
4758c2ecf20Sopenharmony_ci		goto out;
4768c2ecf20Sopenharmony_ci	}
4778c2ecf20Sopenharmony_ci
4788c2ecf20Sopenharmony_ci	cmd = (struct afu_cmd *)PTR_ALIGN(buf, __alignof__(*cmd));
4798c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&cmd->queue);
4808c2ecf20Sopenharmony_ci
4818c2ecf20Sopenharmony_ci	/* When Task Management Function is active do not send another */
4828c2ecf20Sopenharmony_ci	spin_lock_irqsave(&cfg->tmf_slock, lock_flags);
4838c2ecf20Sopenharmony_ci	if (cfg->tmf_active)
4848c2ecf20Sopenharmony_ci		wait_event_interruptible_lock_irq(cfg->tmf_waitq,
4858c2ecf20Sopenharmony_ci						  !cfg->tmf_active,
4868c2ecf20Sopenharmony_ci						  cfg->tmf_slock);
4878c2ecf20Sopenharmony_ci	cfg->tmf_active = true;
4888c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
4898c2ecf20Sopenharmony_ci
4908c2ecf20Sopenharmony_ci	cmd->parent = afu;
4918c2ecf20Sopenharmony_ci	cmd->cmd_tmf = true;
4928c2ecf20Sopenharmony_ci	cmd->hwq_index = hwq->index;
4938c2ecf20Sopenharmony_ci
4948c2ecf20Sopenharmony_ci	cmd->rcb.ctx_id = hwq->ctx_hndl;
4958c2ecf20Sopenharmony_ci	cmd->rcb.msi = SISL_MSI_RRQ_UPDATED;
4968c2ecf20Sopenharmony_ci	cmd->rcb.port_sel = CHAN2PORTMASK(sdev->channel);
4978c2ecf20Sopenharmony_ci	cmd->rcb.lun_id = lun_to_lunid(sdev->lun);
4988c2ecf20Sopenharmony_ci	cmd->rcb.req_flags = (SISL_REQ_FLAGS_PORT_LUN_ID |
4998c2ecf20Sopenharmony_ci			      SISL_REQ_FLAGS_SUP_UNDERRUN |
5008c2ecf20Sopenharmony_ci			      SISL_REQ_FLAGS_TMF_CMD);
5018c2ecf20Sopenharmony_ci	memcpy(cmd->rcb.cdb, &tmfcmd, sizeof(tmfcmd));
5028c2ecf20Sopenharmony_ci
5038c2ecf20Sopenharmony_ci	rc = afu->send_cmd(afu, cmd);
5048c2ecf20Sopenharmony_ci	if (unlikely(rc)) {
5058c2ecf20Sopenharmony_ci		spin_lock_irqsave(&cfg->tmf_slock, lock_flags);
5068c2ecf20Sopenharmony_ci		cfg->tmf_active = false;
5078c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
5088c2ecf20Sopenharmony_ci		goto out;
5098c2ecf20Sopenharmony_ci	}
5108c2ecf20Sopenharmony_ci
5118c2ecf20Sopenharmony_ci	spin_lock_irqsave(&cfg->tmf_slock, lock_flags);
5128c2ecf20Sopenharmony_ci	to = msecs_to_jiffies(5000);
5138c2ecf20Sopenharmony_ci	to = wait_event_interruptible_lock_irq_timeout(cfg->tmf_waitq,
5148c2ecf20Sopenharmony_ci						       !cfg->tmf_active,
5158c2ecf20Sopenharmony_ci						       cfg->tmf_slock,
5168c2ecf20Sopenharmony_ci						       to);
5178c2ecf20Sopenharmony_ci	if (!to) {
5188c2ecf20Sopenharmony_ci		dev_err(dev, "%s: TMF timed out\n", __func__);
5198c2ecf20Sopenharmony_ci		rc = -ETIMEDOUT;
5208c2ecf20Sopenharmony_ci		needs_deletion = true;
5218c2ecf20Sopenharmony_ci	} else if (cmd->cmd_aborted) {
5228c2ecf20Sopenharmony_ci		dev_err(dev, "%s: TMF aborted\n", __func__);
5238c2ecf20Sopenharmony_ci		rc = -EAGAIN;
5248c2ecf20Sopenharmony_ci	} else if (cmd->sa.ioasc) {
5258c2ecf20Sopenharmony_ci		dev_err(dev, "%s: TMF failed ioasc=%08x\n",
5268c2ecf20Sopenharmony_ci			__func__, cmd->sa.ioasc);
5278c2ecf20Sopenharmony_ci		rc = -EIO;
5288c2ecf20Sopenharmony_ci	}
5298c2ecf20Sopenharmony_ci	cfg->tmf_active = false;
5308c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
5318c2ecf20Sopenharmony_ci
5328c2ecf20Sopenharmony_ci	if (needs_deletion) {
5338c2ecf20Sopenharmony_ci		spin_lock_irqsave(&hwq->hsq_slock, lock_flags);
5348c2ecf20Sopenharmony_ci		list_del(&cmd->list);
5358c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&hwq->hsq_slock, lock_flags);
5368c2ecf20Sopenharmony_ci	}
5378c2ecf20Sopenharmony_ciout:
5388c2ecf20Sopenharmony_ci	kfree(buf);
5398c2ecf20Sopenharmony_ci	return rc;
5408c2ecf20Sopenharmony_ci}
5418c2ecf20Sopenharmony_ci
5428c2ecf20Sopenharmony_ci/**
5438c2ecf20Sopenharmony_ci * cxlflash_driver_info() - information handler for this host driver
5448c2ecf20Sopenharmony_ci * @host:	SCSI host associated with device.
5458c2ecf20Sopenharmony_ci *
5468c2ecf20Sopenharmony_ci * Return: A string describing the device.
5478c2ecf20Sopenharmony_ci */
5488c2ecf20Sopenharmony_cistatic const char *cxlflash_driver_info(struct Scsi_Host *host)
5498c2ecf20Sopenharmony_ci{
5508c2ecf20Sopenharmony_ci	return CXLFLASH_ADAPTER_NAME;
5518c2ecf20Sopenharmony_ci}
5528c2ecf20Sopenharmony_ci
5538c2ecf20Sopenharmony_ci/**
5548c2ecf20Sopenharmony_ci * cxlflash_queuecommand() - sends a mid-layer request
5558c2ecf20Sopenharmony_ci * @host:	SCSI host associated with device.
5568c2ecf20Sopenharmony_ci * @scp:	SCSI command to send.
5578c2ecf20Sopenharmony_ci *
5588c2ecf20Sopenharmony_ci * Return: 0 on success, SCSI_MLQUEUE_HOST_BUSY on failure
5598c2ecf20Sopenharmony_ci */
5608c2ecf20Sopenharmony_cistatic int cxlflash_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *scp)
5618c2ecf20Sopenharmony_ci{
5628c2ecf20Sopenharmony_ci	struct cxlflash_cfg *cfg = shost_priv(host);
5638c2ecf20Sopenharmony_ci	struct afu *afu = cfg->afu;
5648c2ecf20Sopenharmony_ci	struct device *dev = &cfg->dev->dev;
5658c2ecf20Sopenharmony_ci	struct afu_cmd *cmd = sc_to_afuci(scp);
5668c2ecf20Sopenharmony_ci	struct scatterlist *sg = scsi_sglist(scp);
5678c2ecf20Sopenharmony_ci	int hwq_index = cmd_to_target_hwq(host, scp, afu);
5688c2ecf20Sopenharmony_ci	struct hwq *hwq = get_hwq(afu, hwq_index);
5698c2ecf20Sopenharmony_ci	u16 req_flags = SISL_REQ_FLAGS_SUP_UNDERRUN;
5708c2ecf20Sopenharmony_ci	ulong lock_flags;
5718c2ecf20Sopenharmony_ci	int rc = 0;
5728c2ecf20Sopenharmony_ci
5738c2ecf20Sopenharmony_ci	dev_dbg_ratelimited(dev, "%s: (scp=%p) %d/%d/%d/%llu "
5748c2ecf20Sopenharmony_ci			    "cdb=(%08x-%08x-%08x-%08x)\n",
5758c2ecf20Sopenharmony_ci			    __func__, scp, host->host_no, scp->device->channel,
5768c2ecf20Sopenharmony_ci			    scp->device->id, scp->device->lun,
5778c2ecf20Sopenharmony_ci			    get_unaligned_be32(&((u32 *)scp->cmnd)[0]),
5788c2ecf20Sopenharmony_ci			    get_unaligned_be32(&((u32 *)scp->cmnd)[1]),
5798c2ecf20Sopenharmony_ci			    get_unaligned_be32(&((u32 *)scp->cmnd)[2]),
5808c2ecf20Sopenharmony_ci			    get_unaligned_be32(&((u32 *)scp->cmnd)[3]));
5818c2ecf20Sopenharmony_ci
5828c2ecf20Sopenharmony_ci	/*
5838c2ecf20Sopenharmony_ci	 * If a Task Management Function is active, wait for it to complete
5848c2ecf20Sopenharmony_ci	 * before continuing with regular commands.
5858c2ecf20Sopenharmony_ci	 */
5868c2ecf20Sopenharmony_ci	spin_lock_irqsave(&cfg->tmf_slock, lock_flags);
5878c2ecf20Sopenharmony_ci	if (cfg->tmf_active) {
5888c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
5898c2ecf20Sopenharmony_ci		rc = SCSI_MLQUEUE_HOST_BUSY;
5908c2ecf20Sopenharmony_ci		goto out;
5918c2ecf20Sopenharmony_ci	}
5928c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
5938c2ecf20Sopenharmony_ci
5948c2ecf20Sopenharmony_ci	switch (cfg->state) {
5958c2ecf20Sopenharmony_ci	case STATE_PROBING:
5968c2ecf20Sopenharmony_ci	case STATE_PROBED:
5978c2ecf20Sopenharmony_ci	case STATE_RESET:
5988c2ecf20Sopenharmony_ci		dev_dbg_ratelimited(dev, "%s: device is in reset\n", __func__);
5998c2ecf20Sopenharmony_ci		rc = SCSI_MLQUEUE_HOST_BUSY;
6008c2ecf20Sopenharmony_ci		goto out;
6018c2ecf20Sopenharmony_ci	case STATE_FAILTERM:
6028c2ecf20Sopenharmony_ci		dev_dbg_ratelimited(dev, "%s: device has failed\n", __func__);
6038c2ecf20Sopenharmony_ci		scp->result = (DID_NO_CONNECT << 16);
6048c2ecf20Sopenharmony_ci		scp->scsi_done(scp);
6058c2ecf20Sopenharmony_ci		rc = 0;
6068c2ecf20Sopenharmony_ci		goto out;
6078c2ecf20Sopenharmony_ci	default:
6088c2ecf20Sopenharmony_ci		atomic_inc(&afu->cmds_active);
6098c2ecf20Sopenharmony_ci		break;
6108c2ecf20Sopenharmony_ci	}
6118c2ecf20Sopenharmony_ci
6128c2ecf20Sopenharmony_ci	if (likely(sg)) {
6138c2ecf20Sopenharmony_ci		cmd->rcb.data_len = sg->length;
6148c2ecf20Sopenharmony_ci		cmd->rcb.data_ea = (uintptr_t)sg_virt(sg);
6158c2ecf20Sopenharmony_ci	}
6168c2ecf20Sopenharmony_ci
6178c2ecf20Sopenharmony_ci	cmd->scp = scp;
6188c2ecf20Sopenharmony_ci	cmd->parent = afu;
6198c2ecf20Sopenharmony_ci	cmd->hwq_index = hwq_index;
6208c2ecf20Sopenharmony_ci
6218c2ecf20Sopenharmony_ci	cmd->sa.ioasc = 0;
6228c2ecf20Sopenharmony_ci	cmd->rcb.ctx_id = hwq->ctx_hndl;
6238c2ecf20Sopenharmony_ci	cmd->rcb.msi = SISL_MSI_RRQ_UPDATED;
6248c2ecf20Sopenharmony_ci	cmd->rcb.port_sel = CHAN2PORTMASK(scp->device->channel);
6258c2ecf20Sopenharmony_ci	cmd->rcb.lun_id = lun_to_lunid(scp->device->lun);
6268c2ecf20Sopenharmony_ci
6278c2ecf20Sopenharmony_ci	if (scp->sc_data_direction == DMA_TO_DEVICE)
6288c2ecf20Sopenharmony_ci		req_flags |= SISL_REQ_FLAGS_HOST_WRITE;
6298c2ecf20Sopenharmony_ci
6308c2ecf20Sopenharmony_ci	cmd->rcb.req_flags = req_flags;
6318c2ecf20Sopenharmony_ci	memcpy(cmd->rcb.cdb, scp->cmnd, sizeof(cmd->rcb.cdb));
6328c2ecf20Sopenharmony_ci
6338c2ecf20Sopenharmony_ci	rc = afu->send_cmd(afu, cmd);
6348c2ecf20Sopenharmony_ci	atomic_dec(&afu->cmds_active);
6358c2ecf20Sopenharmony_ciout:
6368c2ecf20Sopenharmony_ci	return rc;
6378c2ecf20Sopenharmony_ci}
6388c2ecf20Sopenharmony_ci
6398c2ecf20Sopenharmony_ci/**
6408c2ecf20Sopenharmony_ci * cxlflash_wait_for_pci_err_recovery() - wait for error recovery during probe
6418c2ecf20Sopenharmony_ci * @cfg:	Internal structure associated with the host.
6428c2ecf20Sopenharmony_ci */
6438c2ecf20Sopenharmony_cistatic void cxlflash_wait_for_pci_err_recovery(struct cxlflash_cfg *cfg)
6448c2ecf20Sopenharmony_ci{
6458c2ecf20Sopenharmony_ci	struct pci_dev *pdev = cfg->dev;
6468c2ecf20Sopenharmony_ci
6478c2ecf20Sopenharmony_ci	if (pci_channel_offline(pdev))
6488c2ecf20Sopenharmony_ci		wait_event_timeout(cfg->reset_waitq,
6498c2ecf20Sopenharmony_ci				   !pci_channel_offline(pdev),
6508c2ecf20Sopenharmony_ci				   CXLFLASH_PCI_ERROR_RECOVERY_TIMEOUT);
6518c2ecf20Sopenharmony_ci}
6528c2ecf20Sopenharmony_ci
6538c2ecf20Sopenharmony_ci/**
6548c2ecf20Sopenharmony_ci * free_mem() - free memory associated with the AFU
6558c2ecf20Sopenharmony_ci * @cfg:	Internal structure associated with the host.
6568c2ecf20Sopenharmony_ci */
6578c2ecf20Sopenharmony_cistatic void free_mem(struct cxlflash_cfg *cfg)
6588c2ecf20Sopenharmony_ci{
6598c2ecf20Sopenharmony_ci	struct afu *afu = cfg->afu;
6608c2ecf20Sopenharmony_ci
6618c2ecf20Sopenharmony_ci	if (cfg->afu) {
6628c2ecf20Sopenharmony_ci		free_pages((ulong)afu, get_order(sizeof(struct afu)));
6638c2ecf20Sopenharmony_ci		cfg->afu = NULL;
6648c2ecf20Sopenharmony_ci	}
6658c2ecf20Sopenharmony_ci}
6668c2ecf20Sopenharmony_ci
6678c2ecf20Sopenharmony_ci/**
6688c2ecf20Sopenharmony_ci * cxlflash_reset_sync() - synchronizing point for asynchronous resets
6698c2ecf20Sopenharmony_ci * @cfg:	Internal structure associated with the host.
6708c2ecf20Sopenharmony_ci */
6718c2ecf20Sopenharmony_cistatic void cxlflash_reset_sync(struct cxlflash_cfg *cfg)
6728c2ecf20Sopenharmony_ci{
6738c2ecf20Sopenharmony_ci	if (cfg->async_reset_cookie == 0)
6748c2ecf20Sopenharmony_ci		return;
6758c2ecf20Sopenharmony_ci
6768c2ecf20Sopenharmony_ci	/* Wait until all async calls prior to this cookie have completed */
6778c2ecf20Sopenharmony_ci	async_synchronize_cookie(cfg->async_reset_cookie + 1);
6788c2ecf20Sopenharmony_ci	cfg->async_reset_cookie = 0;
6798c2ecf20Sopenharmony_ci}
6808c2ecf20Sopenharmony_ci
6818c2ecf20Sopenharmony_ci/**
6828c2ecf20Sopenharmony_ci * stop_afu() - stops the AFU command timers and unmaps the MMIO space
6838c2ecf20Sopenharmony_ci * @cfg:	Internal structure associated with the host.
6848c2ecf20Sopenharmony_ci *
6858c2ecf20Sopenharmony_ci * Safe to call with AFU in a partially allocated/initialized state.
6868c2ecf20Sopenharmony_ci *
6878c2ecf20Sopenharmony_ci * Cancels scheduled worker threads, waits for any active internal AFU
6888c2ecf20Sopenharmony_ci * commands to timeout, disables IRQ polling and then unmaps the MMIO space.
6898c2ecf20Sopenharmony_ci */
6908c2ecf20Sopenharmony_cistatic void stop_afu(struct cxlflash_cfg *cfg)
6918c2ecf20Sopenharmony_ci{
6928c2ecf20Sopenharmony_ci	struct afu *afu = cfg->afu;
6938c2ecf20Sopenharmony_ci	struct hwq *hwq;
6948c2ecf20Sopenharmony_ci	int i;
6958c2ecf20Sopenharmony_ci
6968c2ecf20Sopenharmony_ci	cancel_work_sync(&cfg->work_q);
6978c2ecf20Sopenharmony_ci	if (!current_is_async())
6988c2ecf20Sopenharmony_ci		cxlflash_reset_sync(cfg);
6998c2ecf20Sopenharmony_ci
7008c2ecf20Sopenharmony_ci	if (likely(afu)) {
7018c2ecf20Sopenharmony_ci		while (atomic_read(&afu->cmds_active))
7028c2ecf20Sopenharmony_ci			ssleep(1);
7038c2ecf20Sopenharmony_ci
7048c2ecf20Sopenharmony_ci		if (afu_is_irqpoll_enabled(afu)) {
7058c2ecf20Sopenharmony_ci			for (i = 0; i < afu->num_hwqs; i++) {
7068c2ecf20Sopenharmony_ci				hwq = get_hwq(afu, i);
7078c2ecf20Sopenharmony_ci
7088c2ecf20Sopenharmony_ci				irq_poll_disable(&hwq->irqpoll);
7098c2ecf20Sopenharmony_ci			}
7108c2ecf20Sopenharmony_ci		}
7118c2ecf20Sopenharmony_ci
7128c2ecf20Sopenharmony_ci		if (likely(afu->afu_map)) {
7138c2ecf20Sopenharmony_ci			cfg->ops->psa_unmap(afu->afu_map);
7148c2ecf20Sopenharmony_ci			afu->afu_map = NULL;
7158c2ecf20Sopenharmony_ci		}
7168c2ecf20Sopenharmony_ci	}
7178c2ecf20Sopenharmony_ci}
7188c2ecf20Sopenharmony_ci
7198c2ecf20Sopenharmony_ci/**
7208c2ecf20Sopenharmony_ci * term_intr() - disables all AFU interrupts
7218c2ecf20Sopenharmony_ci * @cfg:	Internal structure associated with the host.
7228c2ecf20Sopenharmony_ci * @level:	Depth of allocation, where to begin waterfall tear down.
7238c2ecf20Sopenharmony_ci * @index:	Index of the hardware queue.
7248c2ecf20Sopenharmony_ci *
7258c2ecf20Sopenharmony_ci * Safe to call with AFU/MC in partially allocated/initialized state.
7268c2ecf20Sopenharmony_ci */
7278c2ecf20Sopenharmony_cistatic void term_intr(struct cxlflash_cfg *cfg, enum undo_level level,
7288c2ecf20Sopenharmony_ci		      u32 index)
7298c2ecf20Sopenharmony_ci{
7308c2ecf20Sopenharmony_ci	struct afu *afu = cfg->afu;
7318c2ecf20Sopenharmony_ci	struct device *dev = &cfg->dev->dev;
7328c2ecf20Sopenharmony_ci	struct hwq *hwq;
7338c2ecf20Sopenharmony_ci
7348c2ecf20Sopenharmony_ci	if (!afu) {
7358c2ecf20Sopenharmony_ci		dev_err(dev, "%s: returning with NULL afu\n", __func__);
7368c2ecf20Sopenharmony_ci		return;
7378c2ecf20Sopenharmony_ci	}
7388c2ecf20Sopenharmony_ci
7398c2ecf20Sopenharmony_ci	hwq = get_hwq(afu, index);
7408c2ecf20Sopenharmony_ci
7418c2ecf20Sopenharmony_ci	if (!hwq->ctx_cookie) {
7428c2ecf20Sopenharmony_ci		dev_err(dev, "%s: returning with NULL MC\n", __func__);
7438c2ecf20Sopenharmony_ci		return;
7448c2ecf20Sopenharmony_ci	}
7458c2ecf20Sopenharmony_ci
7468c2ecf20Sopenharmony_ci	switch (level) {
7478c2ecf20Sopenharmony_ci	case UNMAP_THREE:
7488c2ecf20Sopenharmony_ci		/* SISL_MSI_ASYNC_ERROR is setup only for the primary HWQ */
7498c2ecf20Sopenharmony_ci		if (index == PRIMARY_HWQ)
7508c2ecf20Sopenharmony_ci			cfg->ops->unmap_afu_irq(hwq->ctx_cookie, 3, hwq);
7518c2ecf20Sopenharmony_ci		fallthrough;
7528c2ecf20Sopenharmony_ci	case UNMAP_TWO:
7538c2ecf20Sopenharmony_ci		cfg->ops->unmap_afu_irq(hwq->ctx_cookie, 2, hwq);
7548c2ecf20Sopenharmony_ci		fallthrough;
7558c2ecf20Sopenharmony_ci	case UNMAP_ONE:
7568c2ecf20Sopenharmony_ci		cfg->ops->unmap_afu_irq(hwq->ctx_cookie, 1, hwq);
7578c2ecf20Sopenharmony_ci		fallthrough;
7588c2ecf20Sopenharmony_ci	case FREE_IRQ:
7598c2ecf20Sopenharmony_ci		cfg->ops->free_afu_irqs(hwq->ctx_cookie);
7608c2ecf20Sopenharmony_ci		fallthrough;
7618c2ecf20Sopenharmony_ci	case UNDO_NOOP:
7628c2ecf20Sopenharmony_ci		/* No action required */
7638c2ecf20Sopenharmony_ci		break;
7648c2ecf20Sopenharmony_ci	}
7658c2ecf20Sopenharmony_ci}
7668c2ecf20Sopenharmony_ci
7678c2ecf20Sopenharmony_ci/**
7688c2ecf20Sopenharmony_ci * term_mc() - terminates the master context
7698c2ecf20Sopenharmony_ci * @cfg:	Internal structure associated with the host.
7708c2ecf20Sopenharmony_ci * @index:	Index of the hardware queue.
7718c2ecf20Sopenharmony_ci *
7728c2ecf20Sopenharmony_ci * Safe to call with AFU/MC in partially allocated/initialized state.
7738c2ecf20Sopenharmony_ci */
7748c2ecf20Sopenharmony_cistatic void term_mc(struct cxlflash_cfg *cfg, u32 index)
7758c2ecf20Sopenharmony_ci{
7768c2ecf20Sopenharmony_ci	struct afu *afu = cfg->afu;
7778c2ecf20Sopenharmony_ci	struct device *dev = &cfg->dev->dev;
7788c2ecf20Sopenharmony_ci	struct hwq *hwq;
7798c2ecf20Sopenharmony_ci	ulong lock_flags;
7808c2ecf20Sopenharmony_ci
7818c2ecf20Sopenharmony_ci	if (!afu) {
7828c2ecf20Sopenharmony_ci		dev_err(dev, "%s: returning with NULL afu\n", __func__);
7838c2ecf20Sopenharmony_ci		return;
7848c2ecf20Sopenharmony_ci	}
7858c2ecf20Sopenharmony_ci
7868c2ecf20Sopenharmony_ci	hwq = get_hwq(afu, index);
7878c2ecf20Sopenharmony_ci
7888c2ecf20Sopenharmony_ci	if (!hwq->ctx_cookie) {
7898c2ecf20Sopenharmony_ci		dev_err(dev, "%s: returning with NULL MC\n", __func__);
7908c2ecf20Sopenharmony_ci		return;
7918c2ecf20Sopenharmony_ci	}
7928c2ecf20Sopenharmony_ci
7938c2ecf20Sopenharmony_ci	WARN_ON(cfg->ops->stop_context(hwq->ctx_cookie));
7948c2ecf20Sopenharmony_ci	if (index != PRIMARY_HWQ)
7958c2ecf20Sopenharmony_ci		WARN_ON(cfg->ops->release_context(hwq->ctx_cookie));
7968c2ecf20Sopenharmony_ci	hwq->ctx_cookie = NULL;
7978c2ecf20Sopenharmony_ci
7988c2ecf20Sopenharmony_ci	spin_lock_irqsave(&hwq->hrrq_slock, lock_flags);
7998c2ecf20Sopenharmony_ci	hwq->hrrq_online = false;
8008c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&hwq->hrrq_slock, lock_flags);
8018c2ecf20Sopenharmony_ci
8028c2ecf20Sopenharmony_ci	spin_lock_irqsave(&hwq->hsq_slock, lock_flags);
8038c2ecf20Sopenharmony_ci	flush_pending_cmds(hwq);
8048c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&hwq->hsq_slock, lock_flags);
8058c2ecf20Sopenharmony_ci}
8068c2ecf20Sopenharmony_ci
8078c2ecf20Sopenharmony_ci/**
8088c2ecf20Sopenharmony_ci * term_afu() - terminates the AFU
8098c2ecf20Sopenharmony_ci * @cfg:	Internal structure associated with the host.
8108c2ecf20Sopenharmony_ci *
8118c2ecf20Sopenharmony_ci * Safe to call with AFU/MC in partially allocated/initialized state.
8128c2ecf20Sopenharmony_ci */
8138c2ecf20Sopenharmony_cistatic void term_afu(struct cxlflash_cfg *cfg)
8148c2ecf20Sopenharmony_ci{
8158c2ecf20Sopenharmony_ci	struct device *dev = &cfg->dev->dev;
8168c2ecf20Sopenharmony_ci	int k;
8178c2ecf20Sopenharmony_ci
8188c2ecf20Sopenharmony_ci	/*
8198c2ecf20Sopenharmony_ci	 * Tear down is carefully orchestrated to ensure
8208c2ecf20Sopenharmony_ci	 * no interrupts can come in when the problem state
8218c2ecf20Sopenharmony_ci	 * area is unmapped.
8228c2ecf20Sopenharmony_ci	 *
8238c2ecf20Sopenharmony_ci	 * 1) Disable all AFU interrupts for each master
8248c2ecf20Sopenharmony_ci	 * 2) Unmap the problem state area
8258c2ecf20Sopenharmony_ci	 * 3) Stop each master context
8268c2ecf20Sopenharmony_ci	 */
8278c2ecf20Sopenharmony_ci	for (k = cfg->afu->num_hwqs - 1; k >= 0; k--)
8288c2ecf20Sopenharmony_ci		term_intr(cfg, UNMAP_THREE, k);
8298c2ecf20Sopenharmony_ci
8308c2ecf20Sopenharmony_ci	stop_afu(cfg);
8318c2ecf20Sopenharmony_ci
8328c2ecf20Sopenharmony_ci	for (k = cfg->afu->num_hwqs - 1; k >= 0; k--)
8338c2ecf20Sopenharmony_ci		term_mc(cfg, k);
8348c2ecf20Sopenharmony_ci
8358c2ecf20Sopenharmony_ci	dev_dbg(dev, "%s: returning\n", __func__);
8368c2ecf20Sopenharmony_ci}
8378c2ecf20Sopenharmony_ci
8388c2ecf20Sopenharmony_ci/**
8398c2ecf20Sopenharmony_ci * notify_shutdown() - notifies device of pending shutdown
8408c2ecf20Sopenharmony_ci * @cfg:	Internal structure associated with the host.
8418c2ecf20Sopenharmony_ci * @wait:	Whether to wait for shutdown processing to complete.
8428c2ecf20Sopenharmony_ci *
8438c2ecf20Sopenharmony_ci * This function will notify the AFU that the adapter is being shutdown
8448c2ecf20Sopenharmony_ci * and will wait for shutdown processing to complete if wait is true.
8458c2ecf20Sopenharmony_ci * This notification should flush pending I/Os to the device and halt
8468c2ecf20Sopenharmony_ci * further I/Os until the next AFU reset is issued and device restarted.
8478c2ecf20Sopenharmony_ci */
8488c2ecf20Sopenharmony_cistatic void notify_shutdown(struct cxlflash_cfg *cfg, bool wait)
8498c2ecf20Sopenharmony_ci{
8508c2ecf20Sopenharmony_ci	struct afu *afu = cfg->afu;
8518c2ecf20Sopenharmony_ci	struct device *dev = &cfg->dev->dev;
8528c2ecf20Sopenharmony_ci	struct dev_dependent_vals *ddv;
8538c2ecf20Sopenharmony_ci	__be64 __iomem *fc_port_regs;
8548c2ecf20Sopenharmony_ci	u64 reg, status;
8558c2ecf20Sopenharmony_ci	int i, retry_cnt = 0;
8568c2ecf20Sopenharmony_ci
8578c2ecf20Sopenharmony_ci	ddv = (struct dev_dependent_vals *)cfg->dev_id->driver_data;
8588c2ecf20Sopenharmony_ci	if (!(ddv->flags & CXLFLASH_NOTIFY_SHUTDOWN))
8598c2ecf20Sopenharmony_ci		return;
8608c2ecf20Sopenharmony_ci
8618c2ecf20Sopenharmony_ci	if (!afu || !afu->afu_map) {
8628c2ecf20Sopenharmony_ci		dev_dbg(dev, "%s: Problem state area not mapped\n", __func__);
8638c2ecf20Sopenharmony_ci		return;
8648c2ecf20Sopenharmony_ci	}
8658c2ecf20Sopenharmony_ci
8668c2ecf20Sopenharmony_ci	/* Notify AFU */
8678c2ecf20Sopenharmony_ci	for (i = 0; i < cfg->num_fc_ports; i++) {
8688c2ecf20Sopenharmony_ci		fc_port_regs = get_fc_port_regs(cfg, i);
8698c2ecf20Sopenharmony_ci
8708c2ecf20Sopenharmony_ci		reg = readq_be(&fc_port_regs[FC_CONFIG2 / 8]);
8718c2ecf20Sopenharmony_ci		reg |= SISL_FC_SHUTDOWN_NORMAL;
8728c2ecf20Sopenharmony_ci		writeq_be(reg, &fc_port_regs[FC_CONFIG2 / 8]);
8738c2ecf20Sopenharmony_ci	}
8748c2ecf20Sopenharmony_ci
8758c2ecf20Sopenharmony_ci	if (!wait)
8768c2ecf20Sopenharmony_ci		return;
8778c2ecf20Sopenharmony_ci
8788c2ecf20Sopenharmony_ci	/* Wait up to 1.5 seconds for shutdown processing to complete */
8798c2ecf20Sopenharmony_ci	for (i = 0; i < cfg->num_fc_ports; i++) {
8808c2ecf20Sopenharmony_ci		fc_port_regs = get_fc_port_regs(cfg, i);
8818c2ecf20Sopenharmony_ci		retry_cnt = 0;
8828c2ecf20Sopenharmony_ci
8838c2ecf20Sopenharmony_ci		while (true) {
8848c2ecf20Sopenharmony_ci			status = readq_be(&fc_port_regs[FC_STATUS / 8]);
8858c2ecf20Sopenharmony_ci			if (status & SISL_STATUS_SHUTDOWN_COMPLETE)
8868c2ecf20Sopenharmony_ci				break;
8878c2ecf20Sopenharmony_ci			if (++retry_cnt >= MC_RETRY_CNT) {
8888c2ecf20Sopenharmony_ci				dev_dbg(dev, "%s: port %d shutdown processing "
8898c2ecf20Sopenharmony_ci					"not yet completed\n", __func__, i);
8908c2ecf20Sopenharmony_ci				break;
8918c2ecf20Sopenharmony_ci			}
8928c2ecf20Sopenharmony_ci			msleep(100 * retry_cnt);
8938c2ecf20Sopenharmony_ci		}
8948c2ecf20Sopenharmony_ci	}
8958c2ecf20Sopenharmony_ci}
8968c2ecf20Sopenharmony_ci
8978c2ecf20Sopenharmony_ci/**
8988c2ecf20Sopenharmony_ci * cxlflash_get_minor() - gets the first available minor number
8998c2ecf20Sopenharmony_ci *
9008c2ecf20Sopenharmony_ci * Return: Unique minor number that can be used to create the character device.
9018c2ecf20Sopenharmony_ci */
9028c2ecf20Sopenharmony_cistatic int cxlflash_get_minor(void)
9038c2ecf20Sopenharmony_ci{
9048c2ecf20Sopenharmony_ci	int minor;
9058c2ecf20Sopenharmony_ci	long bit;
9068c2ecf20Sopenharmony_ci
9078c2ecf20Sopenharmony_ci	bit = find_first_zero_bit(cxlflash_minor, CXLFLASH_MAX_ADAPTERS);
9088c2ecf20Sopenharmony_ci	if (bit >= CXLFLASH_MAX_ADAPTERS)
9098c2ecf20Sopenharmony_ci		return -1;
9108c2ecf20Sopenharmony_ci
9118c2ecf20Sopenharmony_ci	minor = bit & MINORMASK;
9128c2ecf20Sopenharmony_ci	set_bit(minor, cxlflash_minor);
9138c2ecf20Sopenharmony_ci	return minor;
9148c2ecf20Sopenharmony_ci}
9158c2ecf20Sopenharmony_ci
9168c2ecf20Sopenharmony_ci/**
9178c2ecf20Sopenharmony_ci * cxlflash_put_minor() - releases the minor number
9188c2ecf20Sopenharmony_ci * @minor:	Minor number that is no longer needed.
9198c2ecf20Sopenharmony_ci */
9208c2ecf20Sopenharmony_cistatic void cxlflash_put_minor(int minor)
9218c2ecf20Sopenharmony_ci{
9228c2ecf20Sopenharmony_ci	clear_bit(minor, cxlflash_minor);
9238c2ecf20Sopenharmony_ci}
9248c2ecf20Sopenharmony_ci
9258c2ecf20Sopenharmony_ci/**
9268c2ecf20Sopenharmony_ci * cxlflash_release_chrdev() - release the character device for the host
9278c2ecf20Sopenharmony_ci * @cfg:	Internal structure associated with the host.
9288c2ecf20Sopenharmony_ci */
9298c2ecf20Sopenharmony_cistatic void cxlflash_release_chrdev(struct cxlflash_cfg *cfg)
9308c2ecf20Sopenharmony_ci{
9318c2ecf20Sopenharmony_ci	device_unregister(cfg->chardev);
9328c2ecf20Sopenharmony_ci	cfg->chardev = NULL;
9338c2ecf20Sopenharmony_ci	cdev_del(&cfg->cdev);
9348c2ecf20Sopenharmony_ci	cxlflash_put_minor(MINOR(cfg->cdev.dev));
9358c2ecf20Sopenharmony_ci}
9368c2ecf20Sopenharmony_ci
9378c2ecf20Sopenharmony_ci/**
9388c2ecf20Sopenharmony_ci * cxlflash_remove() - PCI entry point to tear down host
9398c2ecf20Sopenharmony_ci * @pdev:	PCI device associated with the host.
9408c2ecf20Sopenharmony_ci *
9418c2ecf20Sopenharmony_ci * Safe to use as a cleanup in partially allocated/initialized state. Note that
9428c2ecf20Sopenharmony_ci * the reset_waitq is flushed as part of the stop/termination of user contexts.
9438c2ecf20Sopenharmony_ci */
9448c2ecf20Sopenharmony_cistatic void cxlflash_remove(struct pci_dev *pdev)
9458c2ecf20Sopenharmony_ci{
9468c2ecf20Sopenharmony_ci	struct cxlflash_cfg *cfg = pci_get_drvdata(pdev);
9478c2ecf20Sopenharmony_ci	struct device *dev = &pdev->dev;
9488c2ecf20Sopenharmony_ci	ulong lock_flags;
9498c2ecf20Sopenharmony_ci
9508c2ecf20Sopenharmony_ci	if (!pci_is_enabled(pdev)) {
9518c2ecf20Sopenharmony_ci		dev_dbg(dev, "%s: Device is disabled\n", __func__);
9528c2ecf20Sopenharmony_ci		return;
9538c2ecf20Sopenharmony_ci	}
9548c2ecf20Sopenharmony_ci
9558c2ecf20Sopenharmony_ci	/* Yield to running recovery threads before continuing with remove */
9568c2ecf20Sopenharmony_ci	wait_event(cfg->reset_waitq, cfg->state != STATE_RESET &&
9578c2ecf20Sopenharmony_ci				     cfg->state != STATE_PROBING);
9588c2ecf20Sopenharmony_ci	spin_lock_irqsave(&cfg->tmf_slock, lock_flags);
9598c2ecf20Sopenharmony_ci	if (cfg->tmf_active)
9608c2ecf20Sopenharmony_ci		wait_event_interruptible_lock_irq(cfg->tmf_waitq,
9618c2ecf20Sopenharmony_ci						  !cfg->tmf_active,
9628c2ecf20Sopenharmony_ci						  cfg->tmf_slock);
9638c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
9648c2ecf20Sopenharmony_ci
9658c2ecf20Sopenharmony_ci	/* Notify AFU and wait for shutdown processing to complete */
9668c2ecf20Sopenharmony_ci	notify_shutdown(cfg, true);
9678c2ecf20Sopenharmony_ci
9688c2ecf20Sopenharmony_ci	cfg->state = STATE_FAILTERM;
9698c2ecf20Sopenharmony_ci	cxlflash_stop_term_user_contexts(cfg);
9708c2ecf20Sopenharmony_ci
9718c2ecf20Sopenharmony_ci	switch (cfg->init_state) {
9728c2ecf20Sopenharmony_ci	case INIT_STATE_CDEV:
9738c2ecf20Sopenharmony_ci		cxlflash_release_chrdev(cfg);
9748c2ecf20Sopenharmony_ci		fallthrough;
9758c2ecf20Sopenharmony_ci	case INIT_STATE_SCSI:
9768c2ecf20Sopenharmony_ci		cxlflash_term_local_luns(cfg);
9778c2ecf20Sopenharmony_ci		scsi_remove_host(cfg->host);
9788c2ecf20Sopenharmony_ci		fallthrough;
9798c2ecf20Sopenharmony_ci	case INIT_STATE_AFU:
9808c2ecf20Sopenharmony_ci		term_afu(cfg);
9818c2ecf20Sopenharmony_ci		fallthrough;
9828c2ecf20Sopenharmony_ci	case INIT_STATE_PCI:
9838c2ecf20Sopenharmony_ci		cfg->ops->destroy_afu(cfg->afu_cookie);
9848c2ecf20Sopenharmony_ci		pci_disable_device(pdev);
9858c2ecf20Sopenharmony_ci		fallthrough;
9868c2ecf20Sopenharmony_ci	case INIT_STATE_NONE:
9878c2ecf20Sopenharmony_ci		free_mem(cfg);
9888c2ecf20Sopenharmony_ci		scsi_host_put(cfg->host);
9898c2ecf20Sopenharmony_ci		break;
9908c2ecf20Sopenharmony_ci	}
9918c2ecf20Sopenharmony_ci
9928c2ecf20Sopenharmony_ci	dev_dbg(dev, "%s: returning\n", __func__);
9938c2ecf20Sopenharmony_ci}
9948c2ecf20Sopenharmony_ci
9958c2ecf20Sopenharmony_ci/**
9968c2ecf20Sopenharmony_ci * alloc_mem() - allocates the AFU and its command pool
9978c2ecf20Sopenharmony_ci * @cfg:	Internal structure associated with the host.
9988c2ecf20Sopenharmony_ci *
9998c2ecf20Sopenharmony_ci * A partially allocated state remains on failure.
10008c2ecf20Sopenharmony_ci *
10018c2ecf20Sopenharmony_ci * Return:
10028c2ecf20Sopenharmony_ci *	0 on success
10038c2ecf20Sopenharmony_ci *	-ENOMEM on failure to allocate memory
10048c2ecf20Sopenharmony_ci */
10058c2ecf20Sopenharmony_cistatic int alloc_mem(struct cxlflash_cfg *cfg)
10068c2ecf20Sopenharmony_ci{
10078c2ecf20Sopenharmony_ci	int rc = 0;
10088c2ecf20Sopenharmony_ci	struct device *dev = &cfg->dev->dev;
10098c2ecf20Sopenharmony_ci
10108c2ecf20Sopenharmony_ci	/* AFU is ~28k, i.e. only one 64k page or up to seven 4k pages */
10118c2ecf20Sopenharmony_ci	cfg->afu = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
10128c2ecf20Sopenharmony_ci					    get_order(sizeof(struct afu)));
10138c2ecf20Sopenharmony_ci	if (unlikely(!cfg->afu)) {
10148c2ecf20Sopenharmony_ci		dev_err(dev, "%s: cannot get %d free pages\n",
10158c2ecf20Sopenharmony_ci			__func__, get_order(sizeof(struct afu)));
10168c2ecf20Sopenharmony_ci		rc = -ENOMEM;
10178c2ecf20Sopenharmony_ci		goto out;
10188c2ecf20Sopenharmony_ci	}
10198c2ecf20Sopenharmony_ci	cfg->afu->parent = cfg;
10208c2ecf20Sopenharmony_ci	cfg->afu->desired_hwqs = CXLFLASH_DEF_HWQS;
10218c2ecf20Sopenharmony_ci	cfg->afu->afu_map = NULL;
10228c2ecf20Sopenharmony_ciout:
10238c2ecf20Sopenharmony_ci	return rc;
10248c2ecf20Sopenharmony_ci}
10258c2ecf20Sopenharmony_ci
10268c2ecf20Sopenharmony_ci/**
10278c2ecf20Sopenharmony_ci * init_pci() - initializes the host as a PCI device
10288c2ecf20Sopenharmony_ci * @cfg:	Internal structure associated with the host.
10298c2ecf20Sopenharmony_ci *
10308c2ecf20Sopenharmony_ci * Return: 0 on success, -errno on failure
10318c2ecf20Sopenharmony_ci */
10328c2ecf20Sopenharmony_cistatic int init_pci(struct cxlflash_cfg *cfg)
10338c2ecf20Sopenharmony_ci{
10348c2ecf20Sopenharmony_ci	struct pci_dev *pdev = cfg->dev;
10358c2ecf20Sopenharmony_ci	struct device *dev = &cfg->dev->dev;
10368c2ecf20Sopenharmony_ci	int rc = 0;
10378c2ecf20Sopenharmony_ci
10388c2ecf20Sopenharmony_ci	rc = pci_enable_device(pdev);
10398c2ecf20Sopenharmony_ci	if (rc || pci_channel_offline(pdev)) {
10408c2ecf20Sopenharmony_ci		if (pci_channel_offline(pdev)) {
10418c2ecf20Sopenharmony_ci			cxlflash_wait_for_pci_err_recovery(cfg);
10428c2ecf20Sopenharmony_ci			rc = pci_enable_device(pdev);
10438c2ecf20Sopenharmony_ci		}
10448c2ecf20Sopenharmony_ci
10458c2ecf20Sopenharmony_ci		if (rc) {
10468c2ecf20Sopenharmony_ci			dev_err(dev, "%s: Cannot enable adapter\n", __func__);
10478c2ecf20Sopenharmony_ci			cxlflash_wait_for_pci_err_recovery(cfg);
10488c2ecf20Sopenharmony_ci			goto out;
10498c2ecf20Sopenharmony_ci		}
10508c2ecf20Sopenharmony_ci	}
10518c2ecf20Sopenharmony_ci
10528c2ecf20Sopenharmony_ciout:
10538c2ecf20Sopenharmony_ci	dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
10548c2ecf20Sopenharmony_ci	return rc;
10558c2ecf20Sopenharmony_ci}
10568c2ecf20Sopenharmony_ci
10578c2ecf20Sopenharmony_ci/**
10588c2ecf20Sopenharmony_ci * init_scsi() - adds the host to the SCSI stack and kicks off host scan
10598c2ecf20Sopenharmony_ci * @cfg:	Internal structure associated with the host.
10608c2ecf20Sopenharmony_ci *
10618c2ecf20Sopenharmony_ci * Return: 0 on success, -errno on failure
10628c2ecf20Sopenharmony_ci */
10638c2ecf20Sopenharmony_cistatic int init_scsi(struct cxlflash_cfg *cfg)
10648c2ecf20Sopenharmony_ci{
10658c2ecf20Sopenharmony_ci	struct pci_dev *pdev = cfg->dev;
10668c2ecf20Sopenharmony_ci	struct device *dev = &cfg->dev->dev;
10678c2ecf20Sopenharmony_ci	int rc = 0;
10688c2ecf20Sopenharmony_ci
10698c2ecf20Sopenharmony_ci	rc = scsi_add_host(cfg->host, &pdev->dev);
10708c2ecf20Sopenharmony_ci	if (rc) {
10718c2ecf20Sopenharmony_ci		dev_err(dev, "%s: scsi_add_host failed rc=%d\n", __func__, rc);
10728c2ecf20Sopenharmony_ci		goto out;
10738c2ecf20Sopenharmony_ci	}
10748c2ecf20Sopenharmony_ci
10758c2ecf20Sopenharmony_ci	scsi_scan_host(cfg->host);
10768c2ecf20Sopenharmony_ci
10778c2ecf20Sopenharmony_ciout:
10788c2ecf20Sopenharmony_ci	dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
10798c2ecf20Sopenharmony_ci	return rc;
10808c2ecf20Sopenharmony_ci}
10818c2ecf20Sopenharmony_ci
10828c2ecf20Sopenharmony_ci/**
10838c2ecf20Sopenharmony_ci * set_port_online() - transitions the specified host FC port to online state
10848c2ecf20Sopenharmony_ci * @fc_regs:	Top of MMIO region defined for specified port.
10858c2ecf20Sopenharmony_ci *
10868c2ecf20Sopenharmony_ci * The provided MMIO region must be mapped prior to call. Online state means
10878c2ecf20Sopenharmony_ci * that the FC link layer has synced, completed the handshaking process, and
10888c2ecf20Sopenharmony_ci * is ready for login to start.
10898c2ecf20Sopenharmony_ci */
10908c2ecf20Sopenharmony_cistatic void set_port_online(__be64 __iomem *fc_regs)
10918c2ecf20Sopenharmony_ci{
10928c2ecf20Sopenharmony_ci	u64 cmdcfg;
10938c2ecf20Sopenharmony_ci
10948c2ecf20Sopenharmony_ci	cmdcfg = readq_be(&fc_regs[FC_MTIP_CMDCONFIG / 8]);
10958c2ecf20Sopenharmony_ci	cmdcfg &= (~FC_MTIP_CMDCONFIG_OFFLINE);	/* clear OFF_LINE */
10968c2ecf20Sopenharmony_ci	cmdcfg |= (FC_MTIP_CMDCONFIG_ONLINE);	/* set ON_LINE */
10978c2ecf20Sopenharmony_ci	writeq_be(cmdcfg, &fc_regs[FC_MTIP_CMDCONFIG / 8]);
10988c2ecf20Sopenharmony_ci}
10998c2ecf20Sopenharmony_ci
11008c2ecf20Sopenharmony_ci/**
11018c2ecf20Sopenharmony_ci * set_port_offline() - transitions the specified host FC port to offline state
11028c2ecf20Sopenharmony_ci * @fc_regs:	Top of MMIO region defined for specified port.
11038c2ecf20Sopenharmony_ci *
11048c2ecf20Sopenharmony_ci * The provided MMIO region must be mapped prior to call.
11058c2ecf20Sopenharmony_ci */
11068c2ecf20Sopenharmony_cistatic void set_port_offline(__be64 __iomem *fc_regs)
11078c2ecf20Sopenharmony_ci{
11088c2ecf20Sopenharmony_ci	u64 cmdcfg;
11098c2ecf20Sopenharmony_ci
11108c2ecf20Sopenharmony_ci	cmdcfg = readq_be(&fc_regs[FC_MTIP_CMDCONFIG / 8]);
11118c2ecf20Sopenharmony_ci	cmdcfg &= (~FC_MTIP_CMDCONFIG_ONLINE);	/* clear ON_LINE */
11128c2ecf20Sopenharmony_ci	cmdcfg |= (FC_MTIP_CMDCONFIG_OFFLINE);	/* set OFF_LINE */
11138c2ecf20Sopenharmony_ci	writeq_be(cmdcfg, &fc_regs[FC_MTIP_CMDCONFIG / 8]);
11148c2ecf20Sopenharmony_ci}
11158c2ecf20Sopenharmony_ci
11168c2ecf20Sopenharmony_ci/**
11178c2ecf20Sopenharmony_ci * wait_port_online() - waits for the specified host FC port come online
11188c2ecf20Sopenharmony_ci * @fc_regs:	Top of MMIO region defined for specified port.
11198c2ecf20Sopenharmony_ci * @delay_us:	Number of microseconds to delay between reading port status.
11208c2ecf20Sopenharmony_ci * @nretry:	Number of cycles to retry reading port status.
11218c2ecf20Sopenharmony_ci *
11228c2ecf20Sopenharmony_ci * The provided MMIO region must be mapped prior to call. This will timeout
11238c2ecf20Sopenharmony_ci * when the cable is not plugged in.
11248c2ecf20Sopenharmony_ci *
11258c2ecf20Sopenharmony_ci * Return:
11268c2ecf20Sopenharmony_ci *	TRUE (1) when the specified port is online
11278c2ecf20Sopenharmony_ci *	FALSE (0) when the specified port fails to come online after timeout
11288c2ecf20Sopenharmony_ci */
11298c2ecf20Sopenharmony_cistatic bool wait_port_online(__be64 __iomem *fc_regs, u32 delay_us, u32 nretry)
11308c2ecf20Sopenharmony_ci{
11318c2ecf20Sopenharmony_ci	u64 status;
11328c2ecf20Sopenharmony_ci
11338c2ecf20Sopenharmony_ci	WARN_ON(delay_us < 1000);
11348c2ecf20Sopenharmony_ci
11358c2ecf20Sopenharmony_ci	do {
11368c2ecf20Sopenharmony_ci		msleep(delay_us / 1000);
11378c2ecf20Sopenharmony_ci		status = readq_be(&fc_regs[FC_MTIP_STATUS / 8]);
11388c2ecf20Sopenharmony_ci		if (status == U64_MAX)
11398c2ecf20Sopenharmony_ci			nretry /= 2;
11408c2ecf20Sopenharmony_ci	} while ((status & FC_MTIP_STATUS_MASK) != FC_MTIP_STATUS_ONLINE &&
11418c2ecf20Sopenharmony_ci		 nretry--);
11428c2ecf20Sopenharmony_ci
11438c2ecf20Sopenharmony_ci	return ((status & FC_MTIP_STATUS_MASK) == FC_MTIP_STATUS_ONLINE);
11448c2ecf20Sopenharmony_ci}
11458c2ecf20Sopenharmony_ci
11468c2ecf20Sopenharmony_ci/**
11478c2ecf20Sopenharmony_ci * wait_port_offline() - waits for the specified host FC port go offline
11488c2ecf20Sopenharmony_ci * @fc_regs:	Top of MMIO region defined for specified port.
11498c2ecf20Sopenharmony_ci * @delay_us:	Number of microseconds to delay between reading port status.
11508c2ecf20Sopenharmony_ci * @nretry:	Number of cycles to retry reading port status.
11518c2ecf20Sopenharmony_ci *
11528c2ecf20Sopenharmony_ci * The provided MMIO region must be mapped prior to call.
11538c2ecf20Sopenharmony_ci *
11548c2ecf20Sopenharmony_ci * Return:
11558c2ecf20Sopenharmony_ci *	TRUE (1) when the specified port is offline
11568c2ecf20Sopenharmony_ci *	FALSE (0) when the specified port fails to go offline after timeout
11578c2ecf20Sopenharmony_ci */
11588c2ecf20Sopenharmony_cistatic bool wait_port_offline(__be64 __iomem *fc_regs, u32 delay_us, u32 nretry)
11598c2ecf20Sopenharmony_ci{
11608c2ecf20Sopenharmony_ci	u64 status;
11618c2ecf20Sopenharmony_ci
11628c2ecf20Sopenharmony_ci	WARN_ON(delay_us < 1000);
11638c2ecf20Sopenharmony_ci
11648c2ecf20Sopenharmony_ci	do {
11658c2ecf20Sopenharmony_ci		msleep(delay_us / 1000);
11668c2ecf20Sopenharmony_ci		status = readq_be(&fc_regs[FC_MTIP_STATUS / 8]);
11678c2ecf20Sopenharmony_ci		if (status == U64_MAX)
11688c2ecf20Sopenharmony_ci			nretry /= 2;
11698c2ecf20Sopenharmony_ci	} while ((status & FC_MTIP_STATUS_MASK) != FC_MTIP_STATUS_OFFLINE &&
11708c2ecf20Sopenharmony_ci		 nretry--);
11718c2ecf20Sopenharmony_ci
11728c2ecf20Sopenharmony_ci	return ((status & FC_MTIP_STATUS_MASK) == FC_MTIP_STATUS_OFFLINE);
11738c2ecf20Sopenharmony_ci}
11748c2ecf20Sopenharmony_ci
11758c2ecf20Sopenharmony_ci/**
11768c2ecf20Sopenharmony_ci * afu_set_wwpn() - configures the WWPN for the specified host FC port
11778c2ecf20Sopenharmony_ci * @afu:	AFU associated with the host that owns the specified FC port.
11788c2ecf20Sopenharmony_ci * @port:	Port number being configured.
11798c2ecf20Sopenharmony_ci * @fc_regs:	Top of MMIO region defined for specified port.
11808c2ecf20Sopenharmony_ci * @wwpn:	The world-wide-port-number previously discovered for port.
11818c2ecf20Sopenharmony_ci *
11828c2ecf20Sopenharmony_ci * The provided MMIO region must be mapped prior to call. As part of the
11838c2ecf20Sopenharmony_ci * sequence to configure the WWPN, the port is toggled offline and then back
11848c2ecf20Sopenharmony_ci * online. This toggling action can cause this routine to delay up to a few
11858c2ecf20Sopenharmony_ci * seconds. When configured to use the internal LUN feature of the AFU, a
11868c2ecf20Sopenharmony_ci * failure to come online is overridden.
11878c2ecf20Sopenharmony_ci */
11888c2ecf20Sopenharmony_cistatic void afu_set_wwpn(struct afu *afu, int port, __be64 __iomem *fc_regs,
11898c2ecf20Sopenharmony_ci			 u64 wwpn)
11908c2ecf20Sopenharmony_ci{
11918c2ecf20Sopenharmony_ci	struct cxlflash_cfg *cfg = afu->parent;
11928c2ecf20Sopenharmony_ci	struct device *dev = &cfg->dev->dev;
11938c2ecf20Sopenharmony_ci
11948c2ecf20Sopenharmony_ci	set_port_offline(fc_regs);
11958c2ecf20Sopenharmony_ci	if (!wait_port_offline(fc_regs, FC_PORT_STATUS_RETRY_INTERVAL_US,
11968c2ecf20Sopenharmony_ci			       FC_PORT_STATUS_RETRY_CNT)) {
11978c2ecf20Sopenharmony_ci		dev_dbg(dev, "%s: wait on port %d to go offline timed out\n",
11988c2ecf20Sopenharmony_ci			__func__, port);
11998c2ecf20Sopenharmony_ci	}
12008c2ecf20Sopenharmony_ci
12018c2ecf20Sopenharmony_ci	writeq_be(wwpn, &fc_regs[FC_PNAME / 8]);
12028c2ecf20Sopenharmony_ci
12038c2ecf20Sopenharmony_ci	set_port_online(fc_regs);
12048c2ecf20Sopenharmony_ci	if (!wait_port_online(fc_regs, FC_PORT_STATUS_RETRY_INTERVAL_US,
12058c2ecf20Sopenharmony_ci			      FC_PORT_STATUS_RETRY_CNT)) {
12068c2ecf20Sopenharmony_ci		dev_dbg(dev, "%s: wait on port %d to go online timed out\n",
12078c2ecf20Sopenharmony_ci			__func__, port);
12088c2ecf20Sopenharmony_ci	}
12098c2ecf20Sopenharmony_ci}
12108c2ecf20Sopenharmony_ci
12118c2ecf20Sopenharmony_ci/**
12128c2ecf20Sopenharmony_ci * afu_link_reset() - resets the specified host FC port
12138c2ecf20Sopenharmony_ci * @afu:	AFU associated with the host that owns the specified FC port.
12148c2ecf20Sopenharmony_ci * @port:	Port number being configured.
12158c2ecf20Sopenharmony_ci * @fc_regs:	Top of MMIO region defined for specified port.
12168c2ecf20Sopenharmony_ci *
12178c2ecf20Sopenharmony_ci * The provided MMIO region must be mapped prior to call. The sequence to
12188c2ecf20Sopenharmony_ci * reset the port involves toggling it offline and then back online. This
12198c2ecf20Sopenharmony_ci * action can cause this routine to delay up to a few seconds. An effort
12208c2ecf20Sopenharmony_ci * is made to maintain link with the device by switching to host to use
12218c2ecf20Sopenharmony_ci * the alternate port exclusively while the reset takes place.
12228c2ecf20Sopenharmony_ci * failure to come online is overridden.
12238c2ecf20Sopenharmony_ci */
12248c2ecf20Sopenharmony_cistatic void afu_link_reset(struct afu *afu, int port, __be64 __iomem *fc_regs)
12258c2ecf20Sopenharmony_ci{
12268c2ecf20Sopenharmony_ci	struct cxlflash_cfg *cfg = afu->parent;
12278c2ecf20Sopenharmony_ci	struct device *dev = &cfg->dev->dev;
12288c2ecf20Sopenharmony_ci	u64 port_sel;
12298c2ecf20Sopenharmony_ci
12308c2ecf20Sopenharmony_ci	/* first switch the AFU to the other links, if any */
12318c2ecf20Sopenharmony_ci	port_sel = readq_be(&afu->afu_map->global.regs.afu_port_sel);
12328c2ecf20Sopenharmony_ci	port_sel &= ~(1ULL << port);
12338c2ecf20Sopenharmony_ci	writeq_be(port_sel, &afu->afu_map->global.regs.afu_port_sel);
12348c2ecf20Sopenharmony_ci	cxlflash_afu_sync(afu, 0, 0, AFU_GSYNC);
12358c2ecf20Sopenharmony_ci
12368c2ecf20Sopenharmony_ci	set_port_offline(fc_regs);
12378c2ecf20Sopenharmony_ci	if (!wait_port_offline(fc_regs, FC_PORT_STATUS_RETRY_INTERVAL_US,
12388c2ecf20Sopenharmony_ci			       FC_PORT_STATUS_RETRY_CNT))
12398c2ecf20Sopenharmony_ci		dev_err(dev, "%s: wait on port %d to go offline timed out\n",
12408c2ecf20Sopenharmony_ci			__func__, port);
12418c2ecf20Sopenharmony_ci
12428c2ecf20Sopenharmony_ci	set_port_online(fc_regs);
12438c2ecf20Sopenharmony_ci	if (!wait_port_online(fc_regs, FC_PORT_STATUS_RETRY_INTERVAL_US,
12448c2ecf20Sopenharmony_ci			      FC_PORT_STATUS_RETRY_CNT))
12458c2ecf20Sopenharmony_ci		dev_err(dev, "%s: wait on port %d to go online timed out\n",
12468c2ecf20Sopenharmony_ci			__func__, port);
12478c2ecf20Sopenharmony_ci
12488c2ecf20Sopenharmony_ci	/* switch back to include this port */
12498c2ecf20Sopenharmony_ci	port_sel |= (1ULL << port);
12508c2ecf20Sopenharmony_ci	writeq_be(port_sel, &afu->afu_map->global.regs.afu_port_sel);
12518c2ecf20Sopenharmony_ci	cxlflash_afu_sync(afu, 0, 0, AFU_GSYNC);
12528c2ecf20Sopenharmony_ci
12538c2ecf20Sopenharmony_ci	dev_dbg(dev, "%s: returning port_sel=%016llx\n", __func__, port_sel);
12548c2ecf20Sopenharmony_ci}
12558c2ecf20Sopenharmony_ci
12568c2ecf20Sopenharmony_ci/**
12578c2ecf20Sopenharmony_ci * afu_err_intr_init() - clears and initializes the AFU for error interrupts
12588c2ecf20Sopenharmony_ci * @afu:	AFU associated with the host.
12598c2ecf20Sopenharmony_ci */
12608c2ecf20Sopenharmony_cistatic void afu_err_intr_init(struct afu *afu)
12618c2ecf20Sopenharmony_ci{
12628c2ecf20Sopenharmony_ci	struct cxlflash_cfg *cfg = afu->parent;
12638c2ecf20Sopenharmony_ci	__be64 __iomem *fc_port_regs;
12648c2ecf20Sopenharmony_ci	int i;
12658c2ecf20Sopenharmony_ci	struct hwq *hwq = get_hwq(afu, PRIMARY_HWQ);
12668c2ecf20Sopenharmony_ci	u64 reg;
12678c2ecf20Sopenharmony_ci
12688c2ecf20Sopenharmony_ci	/* global async interrupts: AFU clears afu_ctrl on context exit
12698c2ecf20Sopenharmony_ci	 * if async interrupts were sent to that context. This prevents
12708c2ecf20Sopenharmony_ci	 * the AFU form sending further async interrupts when
12718c2ecf20Sopenharmony_ci	 * there is
12728c2ecf20Sopenharmony_ci	 * nobody to receive them.
12738c2ecf20Sopenharmony_ci	 */
12748c2ecf20Sopenharmony_ci
12758c2ecf20Sopenharmony_ci	/* mask all */
12768c2ecf20Sopenharmony_ci	writeq_be(-1ULL, &afu->afu_map->global.regs.aintr_mask);
12778c2ecf20Sopenharmony_ci	/* set LISN# to send and point to primary master context */
12788c2ecf20Sopenharmony_ci	reg = ((u64) (((hwq->ctx_hndl << 8) | SISL_MSI_ASYNC_ERROR)) << 40);
12798c2ecf20Sopenharmony_ci
12808c2ecf20Sopenharmony_ci	if (afu->internal_lun)
12818c2ecf20Sopenharmony_ci		reg |= 1;	/* Bit 63 indicates local lun */
12828c2ecf20Sopenharmony_ci	writeq_be(reg, &afu->afu_map->global.regs.afu_ctrl);
12838c2ecf20Sopenharmony_ci	/* clear all */
12848c2ecf20Sopenharmony_ci	writeq_be(-1ULL, &afu->afu_map->global.regs.aintr_clear);
12858c2ecf20Sopenharmony_ci	/* unmask bits that are of interest */
12868c2ecf20Sopenharmony_ci	/* note: afu can send an interrupt after this step */
12878c2ecf20Sopenharmony_ci	writeq_be(SISL_ASTATUS_MASK, &afu->afu_map->global.regs.aintr_mask);
12888c2ecf20Sopenharmony_ci	/* clear again in case a bit came on after previous clear but before */
12898c2ecf20Sopenharmony_ci	/* unmask */
12908c2ecf20Sopenharmony_ci	writeq_be(-1ULL, &afu->afu_map->global.regs.aintr_clear);
12918c2ecf20Sopenharmony_ci
12928c2ecf20Sopenharmony_ci	/* Clear/Set internal lun bits */
12938c2ecf20Sopenharmony_ci	fc_port_regs = get_fc_port_regs(cfg, 0);
12948c2ecf20Sopenharmony_ci	reg = readq_be(&fc_port_regs[FC_CONFIG2 / 8]);
12958c2ecf20Sopenharmony_ci	reg &= SISL_FC_INTERNAL_MASK;
12968c2ecf20Sopenharmony_ci	if (afu->internal_lun)
12978c2ecf20Sopenharmony_ci		reg |= ((u64)(afu->internal_lun - 1) << SISL_FC_INTERNAL_SHIFT);
12988c2ecf20Sopenharmony_ci	writeq_be(reg, &fc_port_regs[FC_CONFIG2 / 8]);
12998c2ecf20Sopenharmony_ci
13008c2ecf20Sopenharmony_ci	/* now clear FC errors */
13018c2ecf20Sopenharmony_ci	for (i = 0; i < cfg->num_fc_ports; i++) {
13028c2ecf20Sopenharmony_ci		fc_port_regs = get_fc_port_regs(cfg, i);
13038c2ecf20Sopenharmony_ci
13048c2ecf20Sopenharmony_ci		writeq_be(0xFFFFFFFFU, &fc_port_regs[FC_ERROR / 8]);
13058c2ecf20Sopenharmony_ci		writeq_be(0, &fc_port_regs[FC_ERRCAP / 8]);
13068c2ecf20Sopenharmony_ci	}
13078c2ecf20Sopenharmony_ci
13088c2ecf20Sopenharmony_ci	/* sync interrupts for master's IOARRIN write */
13098c2ecf20Sopenharmony_ci	/* note that unlike asyncs, there can be no pending sync interrupts */
13108c2ecf20Sopenharmony_ci	/* at this time (this is a fresh context and master has not written */
13118c2ecf20Sopenharmony_ci	/* IOARRIN yet), so there is nothing to clear. */
13128c2ecf20Sopenharmony_ci
13138c2ecf20Sopenharmony_ci	/* set LISN#, it is always sent to the context that wrote IOARRIN */
13148c2ecf20Sopenharmony_ci	for (i = 0; i < afu->num_hwqs; i++) {
13158c2ecf20Sopenharmony_ci		hwq = get_hwq(afu, i);
13168c2ecf20Sopenharmony_ci
13178c2ecf20Sopenharmony_ci		reg = readq_be(&hwq->host_map->ctx_ctrl);
13188c2ecf20Sopenharmony_ci		WARN_ON((reg & SISL_CTX_CTRL_LISN_MASK) != 0);
13198c2ecf20Sopenharmony_ci		reg |= SISL_MSI_SYNC_ERROR;
13208c2ecf20Sopenharmony_ci		writeq_be(reg, &hwq->host_map->ctx_ctrl);
13218c2ecf20Sopenharmony_ci		writeq_be(SISL_ISTATUS_MASK, &hwq->host_map->intr_mask);
13228c2ecf20Sopenharmony_ci	}
13238c2ecf20Sopenharmony_ci}
13248c2ecf20Sopenharmony_ci
13258c2ecf20Sopenharmony_ci/**
13268c2ecf20Sopenharmony_ci * cxlflash_sync_err_irq() - interrupt handler for synchronous errors
13278c2ecf20Sopenharmony_ci * @irq:	Interrupt number.
13288c2ecf20Sopenharmony_ci * @data:	Private data provided at interrupt registration, the AFU.
13298c2ecf20Sopenharmony_ci *
13308c2ecf20Sopenharmony_ci * Return: Always return IRQ_HANDLED.
13318c2ecf20Sopenharmony_ci */
13328c2ecf20Sopenharmony_cistatic irqreturn_t cxlflash_sync_err_irq(int irq, void *data)
13338c2ecf20Sopenharmony_ci{
13348c2ecf20Sopenharmony_ci	struct hwq *hwq = (struct hwq *)data;
13358c2ecf20Sopenharmony_ci	struct cxlflash_cfg *cfg = hwq->afu->parent;
13368c2ecf20Sopenharmony_ci	struct device *dev = &cfg->dev->dev;
13378c2ecf20Sopenharmony_ci	u64 reg;
13388c2ecf20Sopenharmony_ci	u64 reg_unmasked;
13398c2ecf20Sopenharmony_ci
13408c2ecf20Sopenharmony_ci	reg = readq_be(&hwq->host_map->intr_status);
13418c2ecf20Sopenharmony_ci	reg_unmasked = (reg & SISL_ISTATUS_UNMASK);
13428c2ecf20Sopenharmony_ci
13438c2ecf20Sopenharmony_ci	if (reg_unmasked == 0UL) {
13448c2ecf20Sopenharmony_ci		dev_err(dev, "%s: spurious interrupt, intr_status=%016llx\n",
13458c2ecf20Sopenharmony_ci			__func__, reg);
13468c2ecf20Sopenharmony_ci		goto cxlflash_sync_err_irq_exit;
13478c2ecf20Sopenharmony_ci	}
13488c2ecf20Sopenharmony_ci
13498c2ecf20Sopenharmony_ci	dev_err(dev, "%s: unexpected interrupt, intr_status=%016llx\n",
13508c2ecf20Sopenharmony_ci		__func__, reg);
13518c2ecf20Sopenharmony_ci
13528c2ecf20Sopenharmony_ci	writeq_be(reg_unmasked, &hwq->host_map->intr_clear);
13538c2ecf20Sopenharmony_ci
13548c2ecf20Sopenharmony_cicxlflash_sync_err_irq_exit:
13558c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
13568c2ecf20Sopenharmony_ci}
13578c2ecf20Sopenharmony_ci
13588c2ecf20Sopenharmony_ci/**
13598c2ecf20Sopenharmony_ci * process_hrrq() - process the read-response queue
13608c2ecf20Sopenharmony_ci * @afu:	AFU associated with the host.
13618c2ecf20Sopenharmony_ci * @doneq:	Queue of commands harvested from the RRQ.
13628c2ecf20Sopenharmony_ci * @budget:	Threshold of RRQ entries to process.
13638c2ecf20Sopenharmony_ci *
13648c2ecf20Sopenharmony_ci * This routine must be called holding the disabled RRQ spin lock.
13658c2ecf20Sopenharmony_ci *
13668c2ecf20Sopenharmony_ci * Return: The number of entries processed.
13678c2ecf20Sopenharmony_ci */
13688c2ecf20Sopenharmony_cistatic int process_hrrq(struct hwq *hwq, struct list_head *doneq, int budget)
13698c2ecf20Sopenharmony_ci{
13708c2ecf20Sopenharmony_ci	struct afu *afu = hwq->afu;
13718c2ecf20Sopenharmony_ci	struct afu_cmd *cmd;
13728c2ecf20Sopenharmony_ci	struct sisl_ioasa *ioasa;
13738c2ecf20Sopenharmony_ci	struct sisl_ioarcb *ioarcb;
13748c2ecf20Sopenharmony_ci	bool toggle = hwq->toggle;
13758c2ecf20Sopenharmony_ci	int num_hrrq = 0;
13768c2ecf20Sopenharmony_ci	u64 entry,
13778c2ecf20Sopenharmony_ci	    *hrrq_start = hwq->hrrq_start,
13788c2ecf20Sopenharmony_ci	    *hrrq_end = hwq->hrrq_end,
13798c2ecf20Sopenharmony_ci	    *hrrq_curr = hwq->hrrq_curr;
13808c2ecf20Sopenharmony_ci
13818c2ecf20Sopenharmony_ci	/* Process ready RRQ entries up to the specified budget (if any) */
13828c2ecf20Sopenharmony_ci	while (true) {
13838c2ecf20Sopenharmony_ci		entry = *hrrq_curr;
13848c2ecf20Sopenharmony_ci
13858c2ecf20Sopenharmony_ci		if ((entry & SISL_RESP_HANDLE_T_BIT) != toggle)
13868c2ecf20Sopenharmony_ci			break;
13878c2ecf20Sopenharmony_ci
13888c2ecf20Sopenharmony_ci		entry &= ~SISL_RESP_HANDLE_T_BIT;
13898c2ecf20Sopenharmony_ci
13908c2ecf20Sopenharmony_ci		if (afu_is_sq_cmd_mode(afu)) {
13918c2ecf20Sopenharmony_ci			ioasa = (struct sisl_ioasa *)entry;
13928c2ecf20Sopenharmony_ci			cmd = container_of(ioasa, struct afu_cmd, sa);
13938c2ecf20Sopenharmony_ci		} else {
13948c2ecf20Sopenharmony_ci			ioarcb = (struct sisl_ioarcb *)entry;
13958c2ecf20Sopenharmony_ci			cmd = container_of(ioarcb, struct afu_cmd, rcb);
13968c2ecf20Sopenharmony_ci		}
13978c2ecf20Sopenharmony_ci
13988c2ecf20Sopenharmony_ci		list_add_tail(&cmd->queue, doneq);
13998c2ecf20Sopenharmony_ci
14008c2ecf20Sopenharmony_ci		/* Advance to next entry or wrap and flip the toggle bit */
14018c2ecf20Sopenharmony_ci		if (hrrq_curr < hrrq_end)
14028c2ecf20Sopenharmony_ci			hrrq_curr++;
14038c2ecf20Sopenharmony_ci		else {
14048c2ecf20Sopenharmony_ci			hrrq_curr = hrrq_start;
14058c2ecf20Sopenharmony_ci			toggle ^= SISL_RESP_HANDLE_T_BIT;
14068c2ecf20Sopenharmony_ci		}
14078c2ecf20Sopenharmony_ci
14088c2ecf20Sopenharmony_ci		atomic_inc(&hwq->hsq_credits);
14098c2ecf20Sopenharmony_ci		num_hrrq++;
14108c2ecf20Sopenharmony_ci
14118c2ecf20Sopenharmony_ci		if (budget > 0 && num_hrrq >= budget)
14128c2ecf20Sopenharmony_ci			break;
14138c2ecf20Sopenharmony_ci	}
14148c2ecf20Sopenharmony_ci
14158c2ecf20Sopenharmony_ci	hwq->hrrq_curr = hrrq_curr;
14168c2ecf20Sopenharmony_ci	hwq->toggle = toggle;
14178c2ecf20Sopenharmony_ci
14188c2ecf20Sopenharmony_ci	return num_hrrq;
14198c2ecf20Sopenharmony_ci}
14208c2ecf20Sopenharmony_ci
14218c2ecf20Sopenharmony_ci/**
14228c2ecf20Sopenharmony_ci * process_cmd_doneq() - process a queue of harvested RRQ commands
14238c2ecf20Sopenharmony_ci * @doneq:	Queue of completed commands.
14248c2ecf20Sopenharmony_ci *
14258c2ecf20Sopenharmony_ci * Note that upon return the queue can no longer be trusted.
14268c2ecf20Sopenharmony_ci */
14278c2ecf20Sopenharmony_cistatic void process_cmd_doneq(struct list_head *doneq)
14288c2ecf20Sopenharmony_ci{
14298c2ecf20Sopenharmony_ci	struct afu_cmd *cmd, *tmp;
14308c2ecf20Sopenharmony_ci
14318c2ecf20Sopenharmony_ci	WARN_ON(list_empty(doneq));
14328c2ecf20Sopenharmony_ci
14338c2ecf20Sopenharmony_ci	list_for_each_entry_safe(cmd, tmp, doneq, queue)
14348c2ecf20Sopenharmony_ci		cmd_complete(cmd);
14358c2ecf20Sopenharmony_ci}
14368c2ecf20Sopenharmony_ci
14378c2ecf20Sopenharmony_ci/**
14388c2ecf20Sopenharmony_ci * cxlflash_irqpoll() - process a queue of harvested RRQ commands
14398c2ecf20Sopenharmony_ci * @irqpoll:	IRQ poll structure associated with queue to poll.
14408c2ecf20Sopenharmony_ci * @budget:	Threshold of RRQ entries to process per poll.
14418c2ecf20Sopenharmony_ci *
14428c2ecf20Sopenharmony_ci * Return: The number of entries processed.
14438c2ecf20Sopenharmony_ci */
14448c2ecf20Sopenharmony_cistatic int cxlflash_irqpoll(struct irq_poll *irqpoll, int budget)
14458c2ecf20Sopenharmony_ci{
14468c2ecf20Sopenharmony_ci	struct hwq *hwq = container_of(irqpoll, struct hwq, irqpoll);
14478c2ecf20Sopenharmony_ci	unsigned long hrrq_flags;
14488c2ecf20Sopenharmony_ci	LIST_HEAD(doneq);
14498c2ecf20Sopenharmony_ci	int num_entries = 0;
14508c2ecf20Sopenharmony_ci
14518c2ecf20Sopenharmony_ci	spin_lock_irqsave(&hwq->hrrq_slock, hrrq_flags);
14528c2ecf20Sopenharmony_ci
14538c2ecf20Sopenharmony_ci	num_entries = process_hrrq(hwq, &doneq, budget);
14548c2ecf20Sopenharmony_ci	if (num_entries < budget)
14558c2ecf20Sopenharmony_ci		irq_poll_complete(irqpoll);
14568c2ecf20Sopenharmony_ci
14578c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&hwq->hrrq_slock, hrrq_flags);
14588c2ecf20Sopenharmony_ci
14598c2ecf20Sopenharmony_ci	process_cmd_doneq(&doneq);
14608c2ecf20Sopenharmony_ci	return num_entries;
14618c2ecf20Sopenharmony_ci}
14628c2ecf20Sopenharmony_ci
14638c2ecf20Sopenharmony_ci/**
14648c2ecf20Sopenharmony_ci * cxlflash_rrq_irq() - interrupt handler for read-response queue (normal path)
14658c2ecf20Sopenharmony_ci * @irq:	Interrupt number.
14668c2ecf20Sopenharmony_ci * @data:	Private data provided at interrupt registration, the AFU.
14678c2ecf20Sopenharmony_ci *
14688c2ecf20Sopenharmony_ci * Return: IRQ_HANDLED or IRQ_NONE when no ready entries found.
14698c2ecf20Sopenharmony_ci */
14708c2ecf20Sopenharmony_cistatic irqreturn_t cxlflash_rrq_irq(int irq, void *data)
14718c2ecf20Sopenharmony_ci{
14728c2ecf20Sopenharmony_ci	struct hwq *hwq = (struct hwq *)data;
14738c2ecf20Sopenharmony_ci	struct afu *afu = hwq->afu;
14748c2ecf20Sopenharmony_ci	unsigned long hrrq_flags;
14758c2ecf20Sopenharmony_ci	LIST_HEAD(doneq);
14768c2ecf20Sopenharmony_ci	int num_entries = 0;
14778c2ecf20Sopenharmony_ci
14788c2ecf20Sopenharmony_ci	spin_lock_irqsave(&hwq->hrrq_slock, hrrq_flags);
14798c2ecf20Sopenharmony_ci
14808c2ecf20Sopenharmony_ci	/* Silently drop spurious interrupts when queue is not online */
14818c2ecf20Sopenharmony_ci	if (!hwq->hrrq_online) {
14828c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&hwq->hrrq_slock, hrrq_flags);
14838c2ecf20Sopenharmony_ci		return IRQ_HANDLED;
14848c2ecf20Sopenharmony_ci	}
14858c2ecf20Sopenharmony_ci
14868c2ecf20Sopenharmony_ci	if (afu_is_irqpoll_enabled(afu)) {
14878c2ecf20Sopenharmony_ci		irq_poll_sched(&hwq->irqpoll);
14888c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&hwq->hrrq_slock, hrrq_flags);
14898c2ecf20Sopenharmony_ci		return IRQ_HANDLED;
14908c2ecf20Sopenharmony_ci	}
14918c2ecf20Sopenharmony_ci
14928c2ecf20Sopenharmony_ci	num_entries = process_hrrq(hwq, &doneq, -1);
14938c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&hwq->hrrq_slock, hrrq_flags);
14948c2ecf20Sopenharmony_ci
14958c2ecf20Sopenharmony_ci	if (num_entries == 0)
14968c2ecf20Sopenharmony_ci		return IRQ_NONE;
14978c2ecf20Sopenharmony_ci
14988c2ecf20Sopenharmony_ci	process_cmd_doneq(&doneq);
14998c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
15008c2ecf20Sopenharmony_ci}
15018c2ecf20Sopenharmony_ci
15028c2ecf20Sopenharmony_ci/*
15038c2ecf20Sopenharmony_ci * Asynchronous interrupt information table
15048c2ecf20Sopenharmony_ci *
15058c2ecf20Sopenharmony_ci * NOTE:
15068c2ecf20Sopenharmony_ci *	- Order matters here as this array is indexed by bit position.
15078c2ecf20Sopenharmony_ci *
15088c2ecf20Sopenharmony_ci *	- The checkpatch script considers the BUILD_SISL_ASTATUS_FC_PORT macro
15098c2ecf20Sopenharmony_ci *	  as complex and complains due to a lack of parentheses/braces.
15108c2ecf20Sopenharmony_ci */
15118c2ecf20Sopenharmony_ci#define ASTATUS_FC(_a, _b, _c, _d)					 \
15128c2ecf20Sopenharmony_ci	{ SISL_ASTATUS_FC##_a##_##_b, _c, _a, (_d) }
15138c2ecf20Sopenharmony_ci
15148c2ecf20Sopenharmony_ci#define BUILD_SISL_ASTATUS_FC_PORT(_a)					 \
15158c2ecf20Sopenharmony_ci	ASTATUS_FC(_a, LINK_UP, "link up", 0),				 \
15168c2ecf20Sopenharmony_ci	ASTATUS_FC(_a, LINK_DN, "link down", 0),			 \
15178c2ecf20Sopenharmony_ci	ASTATUS_FC(_a, LOGI_S, "login succeeded", SCAN_HOST),		 \
15188c2ecf20Sopenharmony_ci	ASTATUS_FC(_a, LOGI_F, "login failed", CLR_FC_ERROR),		 \
15198c2ecf20Sopenharmony_ci	ASTATUS_FC(_a, LOGI_R, "login timed out, retrying", LINK_RESET), \
15208c2ecf20Sopenharmony_ci	ASTATUS_FC(_a, CRC_T, "CRC threshold exceeded", LINK_RESET),	 \
15218c2ecf20Sopenharmony_ci	ASTATUS_FC(_a, LOGO, "target initiated LOGO", 0),		 \
15228c2ecf20Sopenharmony_ci	ASTATUS_FC(_a, OTHER, "other error", CLR_FC_ERROR | LINK_RESET)
15238c2ecf20Sopenharmony_ci
15248c2ecf20Sopenharmony_cistatic const struct asyc_intr_info ainfo[] = {
15258c2ecf20Sopenharmony_ci	BUILD_SISL_ASTATUS_FC_PORT(1),
15268c2ecf20Sopenharmony_ci	BUILD_SISL_ASTATUS_FC_PORT(0),
15278c2ecf20Sopenharmony_ci	BUILD_SISL_ASTATUS_FC_PORT(3),
15288c2ecf20Sopenharmony_ci	BUILD_SISL_ASTATUS_FC_PORT(2)
15298c2ecf20Sopenharmony_ci};
15308c2ecf20Sopenharmony_ci
15318c2ecf20Sopenharmony_ci/**
15328c2ecf20Sopenharmony_ci * cxlflash_async_err_irq() - interrupt handler for asynchronous errors
15338c2ecf20Sopenharmony_ci * @irq:	Interrupt number.
15348c2ecf20Sopenharmony_ci * @data:	Private data provided at interrupt registration, the AFU.
15358c2ecf20Sopenharmony_ci *
15368c2ecf20Sopenharmony_ci * Return: Always return IRQ_HANDLED.
15378c2ecf20Sopenharmony_ci */
15388c2ecf20Sopenharmony_cistatic irqreturn_t cxlflash_async_err_irq(int irq, void *data)
15398c2ecf20Sopenharmony_ci{
15408c2ecf20Sopenharmony_ci	struct hwq *hwq = (struct hwq *)data;
15418c2ecf20Sopenharmony_ci	struct afu *afu = hwq->afu;
15428c2ecf20Sopenharmony_ci	struct cxlflash_cfg *cfg = afu->parent;
15438c2ecf20Sopenharmony_ci	struct device *dev = &cfg->dev->dev;
15448c2ecf20Sopenharmony_ci	const struct asyc_intr_info *info;
15458c2ecf20Sopenharmony_ci	struct sisl_global_map __iomem *global = &afu->afu_map->global;
15468c2ecf20Sopenharmony_ci	__be64 __iomem *fc_port_regs;
15478c2ecf20Sopenharmony_ci	u64 reg_unmasked;
15488c2ecf20Sopenharmony_ci	u64 reg;
15498c2ecf20Sopenharmony_ci	u64 bit;
15508c2ecf20Sopenharmony_ci	u8 port;
15518c2ecf20Sopenharmony_ci
15528c2ecf20Sopenharmony_ci	reg = readq_be(&global->regs.aintr_status);
15538c2ecf20Sopenharmony_ci	reg_unmasked = (reg & SISL_ASTATUS_UNMASK);
15548c2ecf20Sopenharmony_ci
15558c2ecf20Sopenharmony_ci	if (unlikely(reg_unmasked == 0)) {
15568c2ecf20Sopenharmony_ci		dev_err(dev, "%s: spurious interrupt, aintr_status=%016llx\n",
15578c2ecf20Sopenharmony_ci			__func__, reg);
15588c2ecf20Sopenharmony_ci		goto out;
15598c2ecf20Sopenharmony_ci	}
15608c2ecf20Sopenharmony_ci
15618c2ecf20Sopenharmony_ci	/* FYI, it is 'okay' to clear AFU status before FC_ERROR */
15628c2ecf20Sopenharmony_ci	writeq_be(reg_unmasked, &global->regs.aintr_clear);
15638c2ecf20Sopenharmony_ci
15648c2ecf20Sopenharmony_ci	/* Check each bit that is on */
15658c2ecf20Sopenharmony_ci	for_each_set_bit(bit, (ulong *)&reg_unmasked, BITS_PER_LONG) {
15668c2ecf20Sopenharmony_ci		if (unlikely(bit >= ARRAY_SIZE(ainfo))) {
15678c2ecf20Sopenharmony_ci			WARN_ON_ONCE(1);
15688c2ecf20Sopenharmony_ci			continue;
15698c2ecf20Sopenharmony_ci		}
15708c2ecf20Sopenharmony_ci
15718c2ecf20Sopenharmony_ci		info = &ainfo[bit];
15728c2ecf20Sopenharmony_ci		if (unlikely(info->status != 1ULL << bit)) {
15738c2ecf20Sopenharmony_ci			WARN_ON_ONCE(1);
15748c2ecf20Sopenharmony_ci			continue;
15758c2ecf20Sopenharmony_ci		}
15768c2ecf20Sopenharmony_ci
15778c2ecf20Sopenharmony_ci		port = info->port;
15788c2ecf20Sopenharmony_ci		fc_port_regs = get_fc_port_regs(cfg, port);
15798c2ecf20Sopenharmony_ci
15808c2ecf20Sopenharmony_ci		dev_err(dev, "%s: FC Port %d -> %s, fc_status=%016llx\n",
15818c2ecf20Sopenharmony_ci			__func__, port, info->desc,
15828c2ecf20Sopenharmony_ci		       readq_be(&fc_port_regs[FC_STATUS / 8]));
15838c2ecf20Sopenharmony_ci
15848c2ecf20Sopenharmony_ci		/*
15858c2ecf20Sopenharmony_ci		 * Do link reset first, some OTHER errors will set FC_ERROR
15868c2ecf20Sopenharmony_ci		 * again if cleared before or w/o a reset
15878c2ecf20Sopenharmony_ci		 */
15888c2ecf20Sopenharmony_ci		if (info->action & LINK_RESET) {
15898c2ecf20Sopenharmony_ci			dev_err(dev, "%s: FC Port %d: resetting link\n",
15908c2ecf20Sopenharmony_ci				__func__, port);
15918c2ecf20Sopenharmony_ci			cfg->lr_state = LINK_RESET_REQUIRED;
15928c2ecf20Sopenharmony_ci			cfg->lr_port = port;
15938c2ecf20Sopenharmony_ci			schedule_work(&cfg->work_q);
15948c2ecf20Sopenharmony_ci		}
15958c2ecf20Sopenharmony_ci
15968c2ecf20Sopenharmony_ci		if (info->action & CLR_FC_ERROR) {
15978c2ecf20Sopenharmony_ci			reg = readq_be(&fc_port_regs[FC_ERROR / 8]);
15988c2ecf20Sopenharmony_ci
15998c2ecf20Sopenharmony_ci			/*
16008c2ecf20Sopenharmony_ci			 * Since all errors are unmasked, FC_ERROR and FC_ERRCAP
16018c2ecf20Sopenharmony_ci			 * should be the same and tracing one is sufficient.
16028c2ecf20Sopenharmony_ci			 */
16038c2ecf20Sopenharmony_ci
16048c2ecf20Sopenharmony_ci			dev_err(dev, "%s: fc %d: clearing fc_error=%016llx\n",
16058c2ecf20Sopenharmony_ci				__func__, port, reg);
16068c2ecf20Sopenharmony_ci
16078c2ecf20Sopenharmony_ci			writeq_be(reg, &fc_port_regs[FC_ERROR / 8]);
16088c2ecf20Sopenharmony_ci			writeq_be(0, &fc_port_regs[FC_ERRCAP / 8]);
16098c2ecf20Sopenharmony_ci		}
16108c2ecf20Sopenharmony_ci
16118c2ecf20Sopenharmony_ci		if (info->action & SCAN_HOST) {
16128c2ecf20Sopenharmony_ci			atomic_inc(&cfg->scan_host_needed);
16138c2ecf20Sopenharmony_ci			schedule_work(&cfg->work_q);
16148c2ecf20Sopenharmony_ci		}
16158c2ecf20Sopenharmony_ci	}
16168c2ecf20Sopenharmony_ci
16178c2ecf20Sopenharmony_ciout:
16188c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
16198c2ecf20Sopenharmony_ci}
16208c2ecf20Sopenharmony_ci
16218c2ecf20Sopenharmony_ci/**
16228c2ecf20Sopenharmony_ci * read_vpd() - obtains the WWPNs from VPD
16238c2ecf20Sopenharmony_ci * @cfg:	Internal structure associated with the host.
16248c2ecf20Sopenharmony_ci * @wwpn:	Array of size MAX_FC_PORTS to pass back WWPNs
16258c2ecf20Sopenharmony_ci *
16268c2ecf20Sopenharmony_ci * Return: 0 on success, -errno on failure
16278c2ecf20Sopenharmony_ci */
16288c2ecf20Sopenharmony_cistatic int read_vpd(struct cxlflash_cfg *cfg, u64 wwpn[])
16298c2ecf20Sopenharmony_ci{
16308c2ecf20Sopenharmony_ci	struct device *dev = &cfg->dev->dev;
16318c2ecf20Sopenharmony_ci	struct pci_dev *pdev = cfg->dev;
16328c2ecf20Sopenharmony_ci	int rc = 0;
16338c2ecf20Sopenharmony_ci	int ro_start, ro_size, i, j, k;
16348c2ecf20Sopenharmony_ci	ssize_t vpd_size;
16358c2ecf20Sopenharmony_ci	char vpd_data[CXLFLASH_VPD_LEN];
16368c2ecf20Sopenharmony_ci	char tmp_buf[WWPN_BUF_LEN] = { 0 };
16378c2ecf20Sopenharmony_ci	const struct dev_dependent_vals *ddv = (struct dev_dependent_vals *)
16388c2ecf20Sopenharmony_ci						cfg->dev_id->driver_data;
16398c2ecf20Sopenharmony_ci	const bool wwpn_vpd_required = ddv->flags & CXLFLASH_WWPN_VPD_REQUIRED;
16408c2ecf20Sopenharmony_ci	const char *wwpn_vpd_tags[MAX_FC_PORTS] = { "V5", "V6", "V7", "V8" };
16418c2ecf20Sopenharmony_ci
16428c2ecf20Sopenharmony_ci	/* Get the VPD data from the device */
16438c2ecf20Sopenharmony_ci	vpd_size = cfg->ops->read_adapter_vpd(pdev, vpd_data, sizeof(vpd_data));
16448c2ecf20Sopenharmony_ci	if (unlikely(vpd_size <= 0)) {
16458c2ecf20Sopenharmony_ci		dev_err(dev, "%s: Unable to read VPD (size = %ld)\n",
16468c2ecf20Sopenharmony_ci			__func__, vpd_size);
16478c2ecf20Sopenharmony_ci		rc = -ENODEV;
16488c2ecf20Sopenharmony_ci		goto out;
16498c2ecf20Sopenharmony_ci	}
16508c2ecf20Sopenharmony_ci
16518c2ecf20Sopenharmony_ci	/* Get the read only section offset */
16528c2ecf20Sopenharmony_ci	ro_start = pci_vpd_find_tag(vpd_data, 0, vpd_size,
16538c2ecf20Sopenharmony_ci				    PCI_VPD_LRDT_RO_DATA);
16548c2ecf20Sopenharmony_ci	if (unlikely(ro_start < 0)) {
16558c2ecf20Sopenharmony_ci		dev_err(dev, "%s: VPD Read-only data not found\n", __func__);
16568c2ecf20Sopenharmony_ci		rc = -ENODEV;
16578c2ecf20Sopenharmony_ci		goto out;
16588c2ecf20Sopenharmony_ci	}
16598c2ecf20Sopenharmony_ci
16608c2ecf20Sopenharmony_ci	/* Get the read only section size, cap when extends beyond read VPD */
16618c2ecf20Sopenharmony_ci	ro_size = pci_vpd_lrdt_size(&vpd_data[ro_start]);
16628c2ecf20Sopenharmony_ci	j = ro_size;
16638c2ecf20Sopenharmony_ci	i = ro_start + PCI_VPD_LRDT_TAG_SIZE;
16648c2ecf20Sopenharmony_ci	if (unlikely((i + j) > vpd_size)) {
16658c2ecf20Sopenharmony_ci		dev_dbg(dev, "%s: Might need to read more VPD (%d > %ld)\n",
16668c2ecf20Sopenharmony_ci			__func__, (i + j), vpd_size);
16678c2ecf20Sopenharmony_ci		ro_size = vpd_size - i;
16688c2ecf20Sopenharmony_ci	}
16698c2ecf20Sopenharmony_ci
16708c2ecf20Sopenharmony_ci	/*
16718c2ecf20Sopenharmony_ci	 * Find the offset of the WWPN tag within the read only
16728c2ecf20Sopenharmony_ci	 * VPD data and validate the found field (partials are
16738c2ecf20Sopenharmony_ci	 * no good to us). Convert the ASCII data to an integer
16748c2ecf20Sopenharmony_ci	 * value. Note that we must copy to a temporary buffer
16758c2ecf20Sopenharmony_ci	 * because the conversion service requires that the ASCII
16768c2ecf20Sopenharmony_ci	 * string be terminated.
16778c2ecf20Sopenharmony_ci	 *
16788c2ecf20Sopenharmony_ci	 * Allow for WWPN not being found for all devices, setting
16798c2ecf20Sopenharmony_ci	 * the returned WWPN to zero when not found. Notify with a
16808c2ecf20Sopenharmony_ci	 * log error for cards that should have had WWPN keywords
16818c2ecf20Sopenharmony_ci	 * in the VPD - cards requiring WWPN will not have their
16828c2ecf20Sopenharmony_ci	 * ports programmed and operate in an undefined state.
16838c2ecf20Sopenharmony_ci	 */
16848c2ecf20Sopenharmony_ci	for (k = 0; k < cfg->num_fc_ports; k++) {
16858c2ecf20Sopenharmony_ci		j = ro_size;
16868c2ecf20Sopenharmony_ci		i = ro_start + PCI_VPD_LRDT_TAG_SIZE;
16878c2ecf20Sopenharmony_ci
16888c2ecf20Sopenharmony_ci		i = pci_vpd_find_info_keyword(vpd_data, i, j, wwpn_vpd_tags[k]);
16898c2ecf20Sopenharmony_ci		if (i < 0) {
16908c2ecf20Sopenharmony_ci			if (wwpn_vpd_required)
16918c2ecf20Sopenharmony_ci				dev_err(dev, "%s: Port %d WWPN not found\n",
16928c2ecf20Sopenharmony_ci					__func__, k);
16938c2ecf20Sopenharmony_ci			wwpn[k] = 0ULL;
16948c2ecf20Sopenharmony_ci			continue;
16958c2ecf20Sopenharmony_ci		}
16968c2ecf20Sopenharmony_ci
16978c2ecf20Sopenharmony_ci		j = pci_vpd_info_field_size(&vpd_data[i]);
16988c2ecf20Sopenharmony_ci		i += PCI_VPD_INFO_FLD_HDR_SIZE;
16998c2ecf20Sopenharmony_ci		if (unlikely((i + j > vpd_size) || (j != WWPN_LEN))) {
17008c2ecf20Sopenharmony_ci			dev_err(dev, "%s: Port %d WWPN incomplete or bad VPD\n",
17018c2ecf20Sopenharmony_ci				__func__, k);
17028c2ecf20Sopenharmony_ci			rc = -ENODEV;
17038c2ecf20Sopenharmony_ci			goto out;
17048c2ecf20Sopenharmony_ci		}
17058c2ecf20Sopenharmony_ci
17068c2ecf20Sopenharmony_ci		memcpy(tmp_buf, &vpd_data[i], WWPN_LEN);
17078c2ecf20Sopenharmony_ci		rc = kstrtoul(tmp_buf, WWPN_LEN, (ulong *)&wwpn[k]);
17088c2ecf20Sopenharmony_ci		if (unlikely(rc)) {
17098c2ecf20Sopenharmony_ci			dev_err(dev, "%s: WWPN conversion failed for port %d\n",
17108c2ecf20Sopenharmony_ci				__func__, k);
17118c2ecf20Sopenharmony_ci			rc = -ENODEV;
17128c2ecf20Sopenharmony_ci			goto out;
17138c2ecf20Sopenharmony_ci		}
17148c2ecf20Sopenharmony_ci
17158c2ecf20Sopenharmony_ci		dev_dbg(dev, "%s: wwpn%d=%016llx\n", __func__, k, wwpn[k]);
17168c2ecf20Sopenharmony_ci	}
17178c2ecf20Sopenharmony_ci
17188c2ecf20Sopenharmony_ciout:
17198c2ecf20Sopenharmony_ci	dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
17208c2ecf20Sopenharmony_ci	return rc;
17218c2ecf20Sopenharmony_ci}
17228c2ecf20Sopenharmony_ci
17238c2ecf20Sopenharmony_ci/**
17248c2ecf20Sopenharmony_ci * init_pcr() - initialize the provisioning and control registers
17258c2ecf20Sopenharmony_ci * @cfg:	Internal structure associated with the host.
17268c2ecf20Sopenharmony_ci *
17278c2ecf20Sopenharmony_ci * Also sets up fast access to the mapped registers and initializes AFU
17288c2ecf20Sopenharmony_ci * command fields that never change.
17298c2ecf20Sopenharmony_ci */
17308c2ecf20Sopenharmony_cistatic void init_pcr(struct cxlflash_cfg *cfg)
17318c2ecf20Sopenharmony_ci{
17328c2ecf20Sopenharmony_ci	struct afu *afu = cfg->afu;
17338c2ecf20Sopenharmony_ci	struct sisl_ctrl_map __iomem *ctrl_map;
17348c2ecf20Sopenharmony_ci	struct hwq *hwq;
17358c2ecf20Sopenharmony_ci	void *cookie;
17368c2ecf20Sopenharmony_ci	int i;
17378c2ecf20Sopenharmony_ci
17388c2ecf20Sopenharmony_ci	for (i = 0; i < MAX_CONTEXT; i++) {
17398c2ecf20Sopenharmony_ci		ctrl_map = &afu->afu_map->ctrls[i].ctrl;
17408c2ecf20Sopenharmony_ci		/* Disrupt any clients that could be running */
17418c2ecf20Sopenharmony_ci		/* e.g. clients that survived a master restart */
17428c2ecf20Sopenharmony_ci		writeq_be(0, &ctrl_map->rht_start);
17438c2ecf20Sopenharmony_ci		writeq_be(0, &ctrl_map->rht_cnt_id);
17448c2ecf20Sopenharmony_ci		writeq_be(0, &ctrl_map->ctx_cap);
17458c2ecf20Sopenharmony_ci	}
17468c2ecf20Sopenharmony_ci
17478c2ecf20Sopenharmony_ci	/* Copy frequently used fields into hwq */
17488c2ecf20Sopenharmony_ci	for (i = 0; i < afu->num_hwqs; i++) {
17498c2ecf20Sopenharmony_ci		hwq = get_hwq(afu, i);
17508c2ecf20Sopenharmony_ci		cookie = hwq->ctx_cookie;
17518c2ecf20Sopenharmony_ci
17528c2ecf20Sopenharmony_ci		hwq->ctx_hndl = (u16) cfg->ops->process_element(cookie);
17538c2ecf20Sopenharmony_ci		hwq->host_map = &afu->afu_map->hosts[hwq->ctx_hndl].host;
17548c2ecf20Sopenharmony_ci		hwq->ctrl_map = &afu->afu_map->ctrls[hwq->ctx_hndl].ctrl;
17558c2ecf20Sopenharmony_ci
17568c2ecf20Sopenharmony_ci		/* Program the Endian Control for the master context */
17578c2ecf20Sopenharmony_ci		writeq_be(SISL_ENDIAN_CTRL, &hwq->host_map->endian_ctrl);
17588c2ecf20Sopenharmony_ci	}
17598c2ecf20Sopenharmony_ci}
17608c2ecf20Sopenharmony_ci
17618c2ecf20Sopenharmony_ci/**
17628c2ecf20Sopenharmony_ci * init_global() - initialize AFU global registers
17638c2ecf20Sopenharmony_ci * @cfg:	Internal structure associated with the host.
17648c2ecf20Sopenharmony_ci */
17658c2ecf20Sopenharmony_cistatic int init_global(struct cxlflash_cfg *cfg)
17668c2ecf20Sopenharmony_ci{
17678c2ecf20Sopenharmony_ci	struct afu *afu = cfg->afu;
17688c2ecf20Sopenharmony_ci	struct device *dev = &cfg->dev->dev;
17698c2ecf20Sopenharmony_ci	struct hwq *hwq;
17708c2ecf20Sopenharmony_ci	struct sisl_host_map __iomem *hmap;
17718c2ecf20Sopenharmony_ci	__be64 __iomem *fc_port_regs;
17728c2ecf20Sopenharmony_ci	u64 wwpn[MAX_FC_PORTS];	/* wwpn of AFU ports */
17738c2ecf20Sopenharmony_ci	int i = 0, num_ports = 0;
17748c2ecf20Sopenharmony_ci	int rc = 0;
17758c2ecf20Sopenharmony_ci	int j;
17768c2ecf20Sopenharmony_ci	void *ctx;
17778c2ecf20Sopenharmony_ci	u64 reg;
17788c2ecf20Sopenharmony_ci
17798c2ecf20Sopenharmony_ci	rc = read_vpd(cfg, &wwpn[0]);
17808c2ecf20Sopenharmony_ci	if (rc) {
17818c2ecf20Sopenharmony_ci		dev_err(dev, "%s: could not read vpd rc=%d\n", __func__, rc);
17828c2ecf20Sopenharmony_ci		goto out;
17838c2ecf20Sopenharmony_ci	}
17848c2ecf20Sopenharmony_ci
17858c2ecf20Sopenharmony_ci	/* Set up RRQ and SQ in HWQ for master issued cmds */
17868c2ecf20Sopenharmony_ci	for (i = 0; i < afu->num_hwqs; i++) {
17878c2ecf20Sopenharmony_ci		hwq = get_hwq(afu, i);
17888c2ecf20Sopenharmony_ci		hmap = hwq->host_map;
17898c2ecf20Sopenharmony_ci
17908c2ecf20Sopenharmony_ci		writeq_be((u64) hwq->hrrq_start, &hmap->rrq_start);
17918c2ecf20Sopenharmony_ci		writeq_be((u64) hwq->hrrq_end, &hmap->rrq_end);
17928c2ecf20Sopenharmony_ci		hwq->hrrq_online = true;
17938c2ecf20Sopenharmony_ci
17948c2ecf20Sopenharmony_ci		if (afu_is_sq_cmd_mode(afu)) {
17958c2ecf20Sopenharmony_ci			writeq_be((u64)hwq->hsq_start, &hmap->sq_start);
17968c2ecf20Sopenharmony_ci			writeq_be((u64)hwq->hsq_end, &hmap->sq_end);
17978c2ecf20Sopenharmony_ci		}
17988c2ecf20Sopenharmony_ci	}
17998c2ecf20Sopenharmony_ci
18008c2ecf20Sopenharmony_ci	/* AFU configuration */
18018c2ecf20Sopenharmony_ci	reg = readq_be(&afu->afu_map->global.regs.afu_config);
18028c2ecf20Sopenharmony_ci	reg |= SISL_AFUCONF_AR_ALL|SISL_AFUCONF_ENDIAN;
18038c2ecf20Sopenharmony_ci	/* enable all auto retry options and control endianness */
18048c2ecf20Sopenharmony_ci	/* leave others at default: */
18058c2ecf20Sopenharmony_ci	/* CTX_CAP write protected, mbox_r does not clear on read and */
18068c2ecf20Sopenharmony_ci	/* checker on if dual afu */
18078c2ecf20Sopenharmony_ci	writeq_be(reg, &afu->afu_map->global.regs.afu_config);
18088c2ecf20Sopenharmony_ci
18098c2ecf20Sopenharmony_ci	/* Global port select: select either port */
18108c2ecf20Sopenharmony_ci	if (afu->internal_lun) {
18118c2ecf20Sopenharmony_ci		/* Only use port 0 */
18128c2ecf20Sopenharmony_ci		writeq_be(PORT0, &afu->afu_map->global.regs.afu_port_sel);
18138c2ecf20Sopenharmony_ci		num_ports = 0;
18148c2ecf20Sopenharmony_ci	} else {
18158c2ecf20Sopenharmony_ci		writeq_be(PORT_MASK(cfg->num_fc_ports),
18168c2ecf20Sopenharmony_ci			  &afu->afu_map->global.regs.afu_port_sel);
18178c2ecf20Sopenharmony_ci		num_ports = cfg->num_fc_ports;
18188c2ecf20Sopenharmony_ci	}
18198c2ecf20Sopenharmony_ci
18208c2ecf20Sopenharmony_ci	for (i = 0; i < num_ports; i++) {
18218c2ecf20Sopenharmony_ci		fc_port_regs = get_fc_port_regs(cfg, i);
18228c2ecf20Sopenharmony_ci
18238c2ecf20Sopenharmony_ci		/* Unmask all errors (but they are still masked at AFU) */
18248c2ecf20Sopenharmony_ci		writeq_be(0, &fc_port_regs[FC_ERRMSK / 8]);
18258c2ecf20Sopenharmony_ci		/* Clear CRC error cnt & set a threshold */
18268c2ecf20Sopenharmony_ci		(void)readq_be(&fc_port_regs[FC_CNT_CRCERR / 8]);
18278c2ecf20Sopenharmony_ci		writeq_be(MC_CRC_THRESH, &fc_port_regs[FC_CRC_THRESH / 8]);
18288c2ecf20Sopenharmony_ci
18298c2ecf20Sopenharmony_ci		/* Set WWPNs. If already programmed, wwpn[i] is 0 */
18308c2ecf20Sopenharmony_ci		if (wwpn[i] != 0)
18318c2ecf20Sopenharmony_ci			afu_set_wwpn(afu, i, &fc_port_regs[0], wwpn[i]);
18328c2ecf20Sopenharmony_ci		/* Programming WWPN back to back causes additional
18338c2ecf20Sopenharmony_ci		 * offline/online transitions and a PLOGI
18348c2ecf20Sopenharmony_ci		 */
18358c2ecf20Sopenharmony_ci		msleep(100);
18368c2ecf20Sopenharmony_ci	}
18378c2ecf20Sopenharmony_ci
18388c2ecf20Sopenharmony_ci	if (afu_is_ocxl_lisn(afu)) {
18398c2ecf20Sopenharmony_ci		/* Set up the LISN effective address for each master */
18408c2ecf20Sopenharmony_ci		for (i = 0; i < afu->num_hwqs; i++) {
18418c2ecf20Sopenharmony_ci			hwq = get_hwq(afu, i);
18428c2ecf20Sopenharmony_ci			ctx = hwq->ctx_cookie;
18438c2ecf20Sopenharmony_ci
18448c2ecf20Sopenharmony_ci			for (j = 0; j < hwq->num_irqs; j++) {
18458c2ecf20Sopenharmony_ci				reg = cfg->ops->get_irq_objhndl(ctx, j);
18468c2ecf20Sopenharmony_ci				writeq_be(reg, &hwq->ctrl_map->lisn_ea[j]);
18478c2ecf20Sopenharmony_ci			}
18488c2ecf20Sopenharmony_ci
18498c2ecf20Sopenharmony_ci			reg = hwq->ctx_hndl;
18508c2ecf20Sopenharmony_ci			writeq_be(SISL_LISN_PASID(reg, reg),
18518c2ecf20Sopenharmony_ci				  &hwq->ctrl_map->lisn_pasid[0]);
18528c2ecf20Sopenharmony_ci			writeq_be(SISL_LISN_PASID(0UL, reg),
18538c2ecf20Sopenharmony_ci				  &hwq->ctrl_map->lisn_pasid[1]);
18548c2ecf20Sopenharmony_ci		}
18558c2ecf20Sopenharmony_ci	}
18568c2ecf20Sopenharmony_ci
18578c2ecf20Sopenharmony_ci	/* Set up master's own CTX_CAP to allow real mode, host translation */
18588c2ecf20Sopenharmony_ci	/* tables, afu cmds and read/write GSCSI cmds. */
18598c2ecf20Sopenharmony_ci	/* First, unlock ctx_cap write by reading mbox */
18608c2ecf20Sopenharmony_ci	for (i = 0; i < afu->num_hwqs; i++) {
18618c2ecf20Sopenharmony_ci		hwq = get_hwq(afu, i);
18628c2ecf20Sopenharmony_ci
18638c2ecf20Sopenharmony_ci		(void)readq_be(&hwq->ctrl_map->mbox_r);	/* unlock ctx_cap */
18648c2ecf20Sopenharmony_ci		writeq_be((SISL_CTX_CAP_REAL_MODE | SISL_CTX_CAP_HOST_XLATE |
18658c2ecf20Sopenharmony_ci			SISL_CTX_CAP_READ_CMD | SISL_CTX_CAP_WRITE_CMD |
18668c2ecf20Sopenharmony_ci			SISL_CTX_CAP_AFU_CMD | SISL_CTX_CAP_GSCSI_CMD),
18678c2ecf20Sopenharmony_ci			&hwq->ctrl_map->ctx_cap);
18688c2ecf20Sopenharmony_ci	}
18698c2ecf20Sopenharmony_ci
18708c2ecf20Sopenharmony_ci	/*
18718c2ecf20Sopenharmony_ci	 * Determine write-same unmap support for host by evaluating the unmap
18728c2ecf20Sopenharmony_ci	 * sector support bit of the context control register associated with
18738c2ecf20Sopenharmony_ci	 * the primary hardware queue. Note that while this status is reflected
18748c2ecf20Sopenharmony_ci	 * in a context register, the outcome can be assumed to be host-wide.
18758c2ecf20Sopenharmony_ci	 */
18768c2ecf20Sopenharmony_ci	hwq = get_hwq(afu, PRIMARY_HWQ);
18778c2ecf20Sopenharmony_ci	reg = readq_be(&hwq->host_map->ctx_ctrl);
18788c2ecf20Sopenharmony_ci	if (reg & SISL_CTX_CTRL_UNMAP_SECTOR)
18798c2ecf20Sopenharmony_ci		cfg->ws_unmap = true;
18808c2ecf20Sopenharmony_ci
18818c2ecf20Sopenharmony_ci	/* Initialize heartbeat */
18828c2ecf20Sopenharmony_ci	afu->hb = readq_be(&afu->afu_map->global.regs.afu_hb);
18838c2ecf20Sopenharmony_ciout:
18848c2ecf20Sopenharmony_ci	return rc;
18858c2ecf20Sopenharmony_ci}
18868c2ecf20Sopenharmony_ci
18878c2ecf20Sopenharmony_ci/**
18888c2ecf20Sopenharmony_ci * start_afu() - initializes and starts the AFU
18898c2ecf20Sopenharmony_ci * @cfg:	Internal structure associated with the host.
18908c2ecf20Sopenharmony_ci */
18918c2ecf20Sopenharmony_cistatic int start_afu(struct cxlflash_cfg *cfg)
18928c2ecf20Sopenharmony_ci{
18938c2ecf20Sopenharmony_ci	struct afu *afu = cfg->afu;
18948c2ecf20Sopenharmony_ci	struct device *dev = &cfg->dev->dev;
18958c2ecf20Sopenharmony_ci	struct hwq *hwq;
18968c2ecf20Sopenharmony_ci	int rc = 0;
18978c2ecf20Sopenharmony_ci	int i;
18988c2ecf20Sopenharmony_ci
18998c2ecf20Sopenharmony_ci	init_pcr(cfg);
19008c2ecf20Sopenharmony_ci
19018c2ecf20Sopenharmony_ci	/* Initialize each HWQ */
19028c2ecf20Sopenharmony_ci	for (i = 0; i < afu->num_hwqs; i++) {
19038c2ecf20Sopenharmony_ci		hwq = get_hwq(afu, i);
19048c2ecf20Sopenharmony_ci
19058c2ecf20Sopenharmony_ci		/* After an AFU reset, RRQ entries are stale, clear them */
19068c2ecf20Sopenharmony_ci		memset(&hwq->rrq_entry, 0, sizeof(hwq->rrq_entry));
19078c2ecf20Sopenharmony_ci
19088c2ecf20Sopenharmony_ci		/* Initialize RRQ pointers */
19098c2ecf20Sopenharmony_ci		hwq->hrrq_start = &hwq->rrq_entry[0];
19108c2ecf20Sopenharmony_ci		hwq->hrrq_end = &hwq->rrq_entry[NUM_RRQ_ENTRY - 1];
19118c2ecf20Sopenharmony_ci		hwq->hrrq_curr = hwq->hrrq_start;
19128c2ecf20Sopenharmony_ci		hwq->toggle = 1;
19138c2ecf20Sopenharmony_ci
19148c2ecf20Sopenharmony_ci		/* Initialize spin locks */
19158c2ecf20Sopenharmony_ci		spin_lock_init(&hwq->hrrq_slock);
19168c2ecf20Sopenharmony_ci		spin_lock_init(&hwq->hsq_slock);
19178c2ecf20Sopenharmony_ci
19188c2ecf20Sopenharmony_ci		/* Initialize SQ */
19198c2ecf20Sopenharmony_ci		if (afu_is_sq_cmd_mode(afu)) {
19208c2ecf20Sopenharmony_ci			memset(&hwq->sq, 0, sizeof(hwq->sq));
19218c2ecf20Sopenharmony_ci			hwq->hsq_start = &hwq->sq[0];
19228c2ecf20Sopenharmony_ci			hwq->hsq_end = &hwq->sq[NUM_SQ_ENTRY - 1];
19238c2ecf20Sopenharmony_ci			hwq->hsq_curr = hwq->hsq_start;
19248c2ecf20Sopenharmony_ci
19258c2ecf20Sopenharmony_ci			atomic_set(&hwq->hsq_credits, NUM_SQ_ENTRY - 1);
19268c2ecf20Sopenharmony_ci		}
19278c2ecf20Sopenharmony_ci
19288c2ecf20Sopenharmony_ci		/* Initialize IRQ poll */
19298c2ecf20Sopenharmony_ci		if (afu_is_irqpoll_enabled(afu))
19308c2ecf20Sopenharmony_ci			irq_poll_init(&hwq->irqpoll, afu->irqpoll_weight,
19318c2ecf20Sopenharmony_ci				      cxlflash_irqpoll);
19328c2ecf20Sopenharmony_ci
19338c2ecf20Sopenharmony_ci	}
19348c2ecf20Sopenharmony_ci
19358c2ecf20Sopenharmony_ci	rc = init_global(cfg);
19368c2ecf20Sopenharmony_ci
19378c2ecf20Sopenharmony_ci	dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
19388c2ecf20Sopenharmony_ci	return rc;
19398c2ecf20Sopenharmony_ci}
19408c2ecf20Sopenharmony_ci
19418c2ecf20Sopenharmony_ci/**
19428c2ecf20Sopenharmony_ci * init_intr() - setup interrupt handlers for the master context
19438c2ecf20Sopenharmony_ci * @cfg:	Internal structure associated with the host.
19448c2ecf20Sopenharmony_ci * @hwq:	Hardware queue to initialize.
19458c2ecf20Sopenharmony_ci *
19468c2ecf20Sopenharmony_ci * Return: 0 on success, -errno on failure
19478c2ecf20Sopenharmony_ci */
19488c2ecf20Sopenharmony_cistatic enum undo_level init_intr(struct cxlflash_cfg *cfg,
19498c2ecf20Sopenharmony_ci				 struct hwq *hwq)
19508c2ecf20Sopenharmony_ci{
19518c2ecf20Sopenharmony_ci	struct device *dev = &cfg->dev->dev;
19528c2ecf20Sopenharmony_ci	void *ctx = hwq->ctx_cookie;
19538c2ecf20Sopenharmony_ci	int rc = 0;
19548c2ecf20Sopenharmony_ci	enum undo_level level = UNDO_NOOP;
19558c2ecf20Sopenharmony_ci	bool is_primary_hwq = (hwq->index == PRIMARY_HWQ);
19568c2ecf20Sopenharmony_ci	int num_irqs = hwq->num_irqs;
19578c2ecf20Sopenharmony_ci
19588c2ecf20Sopenharmony_ci	rc = cfg->ops->allocate_afu_irqs(ctx, num_irqs);
19598c2ecf20Sopenharmony_ci	if (unlikely(rc)) {
19608c2ecf20Sopenharmony_ci		dev_err(dev, "%s: allocate_afu_irqs failed rc=%d\n",
19618c2ecf20Sopenharmony_ci			__func__, rc);
19628c2ecf20Sopenharmony_ci		level = UNDO_NOOP;
19638c2ecf20Sopenharmony_ci		goto out;
19648c2ecf20Sopenharmony_ci	}
19658c2ecf20Sopenharmony_ci
19668c2ecf20Sopenharmony_ci	rc = cfg->ops->map_afu_irq(ctx, 1, cxlflash_sync_err_irq, hwq,
19678c2ecf20Sopenharmony_ci				   "SISL_MSI_SYNC_ERROR");
19688c2ecf20Sopenharmony_ci	if (unlikely(rc <= 0)) {
19698c2ecf20Sopenharmony_ci		dev_err(dev, "%s: SISL_MSI_SYNC_ERROR map failed\n", __func__);
19708c2ecf20Sopenharmony_ci		level = FREE_IRQ;
19718c2ecf20Sopenharmony_ci		goto out;
19728c2ecf20Sopenharmony_ci	}
19738c2ecf20Sopenharmony_ci
19748c2ecf20Sopenharmony_ci	rc = cfg->ops->map_afu_irq(ctx, 2, cxlflash_rrq_irq, hwq,
19758c2ecf20Sopenharmony_ci				   "SISL_MSI_RRQ_UPDATED");
19768c2ecf20Sopenharmony_ci	if (unlikely(rc <= 0)) {
19778c2ecf20Sopenharmony_ci		dev_err(dev, "%s: SISL_MSI_RRQ_UPDATED map failed\n", __func__);
19788c2ecf20Sopenharmony_ci		level = UNMAP_ONE;
19798c2ecf20Sopenharmony_ci		goto out;
19808c2ecf20Sopenharmony_ci	}
19818c2ecf20Sopenharmony_ci
19828c2ecf20Sopenharmony_ci	/* SISL_MSI_ASYNC_ERROR is setup only for the primary HWQ */
19838c2ecf20Sopenharmony_ci	if (!is_primary_hwq)
19848c2ecf20Sopenharmony_ci		goto out;
19858c2ecf20Sopenharmony_ci
19868c2ecf20Sopenharmony_ci	rc = cfg->ops->map_afu_irq(ctx, 3, cxlflash_async_err_irq, hwq,
19878c2ecf20Sopenharmony_ci				   "SISL_MSI_ASYNC_ERROR");
19888c2ecf20Sopenharmony_ci	if (unlikely(rc <= 0)) {
19898c2ecf20Sopenharmony_ci		dev_err(dev, "%s: SISL_MSI_ASYNC_ERROR map failed\n", __func__);
19908c2ecf20Sopenharmony_ci		level = UNMAP_TWO;
19918c2ecf20Sopenharmony_ci		goto out;
19928c2ecf20Sopenharmony_ci	}
19938c2ecf20Sopenharmony_ciout:
19948c2ecf20Sopenharmony_ci	return level;
19958c2ecf20Sopenharmony_ci}
19968c2ecf20Sopenharmony_ci
19978c2ecf20Sopenharmony_ci/**
19988c2ecf20Sopenharmony_ci * init_mc() - create and register as the master context
19998c2ecf20Sopenharmony_ci * @cfg:	Internal structure associated with the host.
20008c2ecf20Sopenharmony_ci * index:	HWQ Index of the master context.
20018c2ecf20Sopenharmony_ci *
20028c2ecf20Sopenharmony_ci * Return: 0 on success, -errno on failure
20038c2ecf20Sopenharmony_ci */
20048c2ecf20Sopenharmony_cistatic int init_mc(struct cxlflash_cfg *cfg, u32 index)
20058c2ecf20Sopenharmony_ci{
20068c2ecf20Sopenharmony_ci	void *ctx;
20078c2ecf20Sopenharmony_ci	struct device *dev = &cfg->dev->dev;
20088c2ecf20Sopenharmony_ci	struct hwq *hwq = get_hwq(cfg->afu, index);
20098c2ecf20Sopenharmony_ci	int rc = 0;
20108c2ecf20Sopenharmony_ci	int num_irqs;
20118c2ecf20Sopenharmony_ci	enum undo_level level;
20128c2ecf20Sopenharmony_ci
20138c2ecf20Sopenharmony_ci	hwq->afu = cfg->afu;
20148c2ecf20Sopenharmony_ci	hwq->index = index;
20158c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&hwq->pending_cmds);
20168c2ecf20Sopenharmony_ci
20178c2ecf20Sopenharmony_ci	if (index == PRIMARY_HWQ) {
20188c2ecf20Sopenharmony_ci		ctx = cfg->ops->get_context(cfg->dev, cfg->afu_cookie);
20198c2ecf20Sopenharmony_ci		num_irqs = 3;
20208c2ecf20Sopenharmony_ci	} else {
20218c2ecf20Sopenharmony_ci		ctx = cfg->ops->dev_context_init(cfg->dev, cfg->afu_cookie);
20228c2ecf20Sopenharmony_ci		num_irqs = 2;
20238c2ecf20Sopenharmony_ci	}
20248c2ecf20Sopenharmony_ci	if (IS_ERR_OR_NULL(ctx)) {
20258c2ecf20Sopenharmony_ci		rc = -ENOMEM;
20268c2ecf20Sopenharmony_ci		goto err1;
20278c2ecf20Sopenharmony_ci	}
20288c2ecf20Sopenharmony_ci
20298c2ecf20Sopenharmony_ci	WARN_ON(hwq->ctx_cookie);
20308c2ecf20Sopenharmony_ci	hwq->ctx_cookie = ctx;
20318c2ecf20Sopenharmony_ci	hwq->num_irqs = num_irqs;
20328c2ecf20Sopenharmony_ci
20338c2ecf20Sopenharmony_ci	/* Set it up as a master with the CXL */
20348c2ecf20Sopenharmony_ci	cfg->ops->set_master(ctx);
20358c2ecf20Sopenharmony_ci
20368c2ecf20Sopenharmony_ci	/* Reset AFU when initializing primary context */
20378c2ecf20Sopenharmony_ci	if (index == PRIMARY_HWQ) {
20388c2ecf20Sopenharmony_ci		rc = cfg->ops->afu_reset(ctx);
20398c2ecf20Sopenharmony_ci		if (unlikely(rc)) {
20408c2ecf20Sopenharmony_ci			dev_err(dev, "%s: AFU reset failed rc=%d\n",
20418c2ecf20Sopenharmony_ci				      __func__, rc);
20428c2ecf20Sopenharmony_ci			goto err1;
20438c2ecf20Sopenharmony_ci		}
20448c2ecf20Sopenharmony_ci	}
20458c2ecf20Sopenharmony_ci
20468c2ecf20Sopenharmony_ci	level = init_intr(cfg, hwq);
20478c2ecf20Sopenharmony_ci	if (unlikely(level)) {
20488c2ecf20Sopenharmony_ci		dev_err(dev, "%s: interrupt init failed rc=%d\n", __func__, rc);
20498c2ecf20Sopenharmony_ci		goto err2;
20508c2ecf20Sopenharmony_ci	}
20518c2ecf20Sopenharmony_ci
20528c2ecf20Sopenharmony_ci	/* Finally, activate the context by starting it */
20538c2ecf20Sopenharmony_ci	rc = cfg->ops->start_context(hwq->ctx_cookie);
20548c2ecf20Sopenharmony_ci	if (unlikely(rc)) {
20558c2ecf20Sopenharmony_ci		dev_err(dev, "%s: start context failed rc=%d\n", __func__, rc);
20568c2ecf20Sopenharmony_ci		level = UNMAP_THREE;
20578c2ecf20Sopenharmony_ci		goto err2;
20588c2ecf20Sopenharmony_ci	}
20598c2ecf20Sopenharmony_ci
20608c2ecf20Sopenharmony_ciout:
20618c2ecf20Sopenharmony_ci	dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
20628c2ecf20Sopenharmony_ci	return rc;
20638c2ecf20Sopenharmony_cierr2:
20648c2ecf20Sopenharmony_ci	term_intr(cfg, level, index);
20658c2ecf20Sopenharmony_ci	if (index != PRIMARY_HWQ)
20668c2ecf20Sopenharmony_ci		cfg->ops->release_context(ctx);
20678c2ecf20Sopenharmony_cierr1:
20688c2ecf20Sopenharmony_ci	hwq->ctx_cookie = NULL;
20698c2ecf20Sopenharmony_ci	goto out;
20708c2ecf20Sopenharmony_ci}
20718c2ecf20Sopenharmony_ci
20728c2ecf20Sopenharmony_ci/**
20738c2ecf20Sopenharmony_ci * get_num_afu_ports() - determines and configures the number of AFU ports
20748c2ecf20Sopenharmony_ci * @cfg:	Internal structure associated with the host.
20758c2ecf20Sopenharmony_ci *
20768c2ecf20Sopenharmony_ci * This routine determines the number of AFU ports by converting the global
20778c2ecf20Sopenharmony_ci * port selection mask. The converted value is only valid following an AFU
20788c2ecf20Sopenharmony_ci * reset (explicit or power-on). This routine must be invoked shortly after
20798c2ecf20Sopenharmony_ci * mapping as other routines are dependent on the number of ports during the
20808c2ecf20Sopenharmony_ci * initialization sequence.
20818c2ecf20Sopenharmony_ci *
20828c2ecf20Sopenharmony_ci * To support legacy AFUs that might not have reflected an initial global
20838c2ecf20Sopenharmony_ci * port mask (value read is 0), default to the number of ports originally
20848c2ecf20Sopenharmony_ci * supported by the cxlflash driver (2) before hardware with other port
20858c2ecf20Sopenharmony_ci * offerings was introduced.
20868c2ecf20Sopenharmony_ci */
20878c2ecf20Sopenharmony_cistatic void get_num_afu_ports(struct cxlflash_cfg *cfg)
20888c2ecf20Sopenharmony_ci{
20898c2ecf20Sopenharmony_ci	struct afu *afu = cfg->afu;
20908c2ecf20Sopenharmony_ci	struct device *dev = &cfg->dev->dev;
20918c2ecf20Sopenharmony_ci	u64 port_mask;
20928c2ecf20Sopenharmony_ci	int num_fc_ports = LEGACY_FC_PORTS;
20938c2ecf20Sopenharmony_ci
20948c2ecf20Sopenharmony_ci	port_mask = readq_be(&afu->afu_map->global.regs.afu_port_sel);
20958c2ecf20Sopenharmony_ci	if (port_mask != 0ULL)
20968c2ecf20Sopenharmony_ci		num_fc_ports = min(ilog2(port_mask) + 1, MAX_FC_PORTS);
20978c2ecf20Sopenharmony_ci
20988c2ecf20Sopenharmony_ci	dev_dbg(dev, "%s: port_mask=%016llx num_fc_ports=%d\n",
20998c2ecf20Sopenharmony_ci		__func__, port_mask, num_fc_ports);
21008c2ecf20Sopenharmony_ci
21018c2ecf20Sopenharmony_ci	cfg->num_fc_ports = num_fc_ports;
21028c2ecf20Sopenharmony_ci	cfg->host->max_channel = PORTNUM2CHAN(num_fc_ports);
21038c2ecf20Sopenharmony_ci}
21048c2ecf20Sopenharmony_ci
21058c2ecf20Sopenharmony_ci/**
21068c2ecf20Sopenharmony_ci * init_afu() - setup as master context and start AFU
21078c2ecf20Sopenharmony_ci * @cfg:	Internal structure associated with the host.
21088c2ecf20Sopenharmony_ci *
21098c2ecf20Sopenharmony_ci * This routine is a higher level of control for configuring the
21108c2ecf20Sopenharmony_ci * AFU on probe and reset paths.
21118c2ecf20Sopenharmony_ci *
21128c2ecf20Sopenharmony_ci * Return: 0 on success, -errno on failure
21138c2ecf20Sopenharmony_ci */
21148c2ecf20Sopenharmony_cistatic int init_afu(struct cxlflash_cfg *cfg)
21158c2ecf20Sopenharmony_ci{
21168c2ecf20Sopenharmony_ci	u64 reg;
21178c2ecf20Sopenharmony_ci	int rc = 0;
21188c2ecf20Sopenharmony_ci	struct afu *afu = cfg->afu;
21198c2ecf20Sopenharmony_ci	struct device *dev = &cfg->dev->dev;
21208c2ecf20Sopenharmony_ci	struct hwq *hwq;
21218c2ecf20Sopenharmony_ci	int i;
21228c2ecf20Sopenharmony_ci
21238c2ecf20Sopenharmony_ci	cfg->ops->perst_reloads_same_image(cfg->afu_cookie, true);
21248c2ecf20Sopenharmony_ci
21258c2ecf20Sopenharmony_ci	mutex_init(&afu->sync_active);
21268c2ecf20Sopenharmony_ci	afu->num_hwqs = afu->desired_hwqs;
21278c2ecf20Sopenharmony_ci	for (i = 0; i < afu->num_hwqs; i++) {
21288c2ecf20Sopenharmony_ci		rc = init_mc(cfg, i);
21298c2ecf20Sopenharmony_ci		if (rc) {
21308c2ecf20Sopenharmony_ci			dev_err(dev, "%s: init_mc failed rc=%d index=%d\n",
21318c2ecf20Sopenharmony_ci				__func__, rc, i);
21328c2ecf20Sopenharmony_ci			goto err1;
21338c2ecf20Sopenharmony_ci		}
21348c2ecf20Sopenharmony_ci	}
21358c2ecf20Sopenharmony_ci
21368c2ecf20Sopenharmony_ci	/* Map the entire MMIO space of the AFU using the first context */
21378c2ecf20Sopenharmony_ci	hwq = get_hwq(afu, PRIMARY_HWQ);
21388c2ecf20Sopenharmony_ci	afu->afu_map = cfg->ops->psa_map(hwq->ctx_cookie);
21398c2ecf20Sopenharmony_ci	if (!afu->afu_map) {
21408c2ecf20Sopenharmony_ci		dev_err(dev, "%s: psa_map failed\n", __func__);
21418c2ecf20Sopenharmony_ci		rc = -ENOMEM;
21428c2ecf20Sopenharmony_ci		goto err1;
21438c2ecf20Sopenharmony_ci	}
21448c2ecf20Sopenharmony_ci
21458c2ecf20Sopenharmony_ci	/* No byte reverse on reading afu_version or string will be backwards */
21468c2ecf20Sopenharmony_ci	reg = readq(&afu->afu_map->global.regs.afu_version);
21478c2ecf20Sopenharmony_ci	memcpy(afu->version, &reg, sizeof(reg));
21488c2ecf20Sopenharmony_ci	afu->interface_version =
21498c2ecf20Sopenharmony_ci	    readq_be(&afu->afu_map->global.regs.interface_version);
21508c2ecf20Sopenharmony_ci	if ((afu->interface_version + 1) == 0) {
21518c2ecf20Sopenharmony_ci		dev_err(dev, "Back level AFU, please upgrade. AFU version %s "
21528c2ecf20Sopenharmony_ci			"interface version %016llx\n", afu->version,
21538c2ecf20Sopenharmony_ci		       afu->interface_version);
21548c2ecf20Sopenharmony_ci		rc = -EINVAL;
21558c2ecf20Sopenharmony_ci		goto err1;
21568c2ecf20Sopenharmony_ci	}
21578c2ecf20Sopenharmony_ci
21588c2ecf20Sopenharmony_ci	if (afu_is_sq_cmd_mode(afu)) {
21598c2ecf20Sopenharmony_ci		afu->send_cmd = send_cmd_sq;
21608c2ecf20Sopenharmony_ci		afu->context_reset = context_reset_sq;
21618c2ecf20Sopenharmony_ci	} else {
21628c2ecf20Sopenharmony_ci		afu->send_cmd = send_cmd_ioarrin;
21638c2ecf20Sopenharmony_ci		afu->context_reset = context_reset_ioarrin;
21648c2ecf20Sopenharmony_ci	}
21658c2ecf20Sopenharmony_ci
21668c2ecf20Sopenharmony_ci	dev_dbg(dev, "%s: afu_ver=%s interface_ver=%016llx\n", __func__,
21678c2ecf20Sopenharmony_ci		afu->version, afu->interface_version);
21688c2ecf20Sopenharmony_ci
21698c2ecf20Sopenharmony_ci	get_num_afu_ports(cfg);
21708c2ecf20Sopenharmony_ci
21718c2ecf20Sopenharmony_ci	rc = start_afu(cfg);
21728c2ecf20Sopenharmony_ci	if (rc) {
21738c2ecf20Sopenharmony_ci		dev_err(dev, "%s: start_afu failed, rc=%d\n", __func__, rc);
21748c2ecf20Sopenharmony_ci		goto err1;
21758c2ecf20Sopenharmony_ci	}
21768c2ecf20Sopenharmony_ci
21778c2ecf20Sopenharmony_ci	afu_err_intr_init(cfg->afu);
21788c2ecf20Sopenharmony_ci	for (i = 0; i < afu->num_hwqs; i++) {
21798c2ecf20Sopenharmony_ci		hwq = get_hwq(afu, i);
21808c2ecf20Sopenharmony_ci
21818c2ecf20Sopenharmony_ci		hwq->room = readq_be(&hwq->host_map->cmd_room);
21828c2ecf20Sopenharmony_ci	}
21838c2ecf20Sopenharmony_ci
21848c2ecf20Sopenharmony_ci	/* Restore the LUN mappings */
21858c2ecf20Sopenharmony_ci	cxlflash_restore_luntable(cfg);
21868c2ecf20Sopenharmony_ciout:
21878c2ecf20Sopenharmony_ci	dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
21888c2ecf20Sopenharmony_ci	return rc;
21898c2ecf20Sopenharmony_ci
21908c2ecf20Sopenharmony_cierr1:
21918c2ecf20Sopenharmony_ci	for (i = afu->num_hwqs - 1; i >= 0; i--) {
21928c2ecf20Sopenharmony_ci		term_intr(cfg, UNMAP_THREE, i);
21938c2ecf20Sopenharmony_ci		term_mc(cfg, i);
21948c2ecf20Sopenharmony_ci	}
21958c2ecf20Sopenharmony_ci	goto out;
21968c2ecf20Sopenharmony_ci}
21978c2ecf20Sopenharmony_ci
21988c2ecf20Sopenharmony_ci/**
21998c2ecf20Sopenharmony_ci * afu_reset() - resets the AFU
22008c2ecf20Sopenharmony_ci * @cfg:	Internal structure associated with the host.
22018c2ecf20Sopenharmony_ci *
22028c2ecf20Sopenharmony_ci * Return: 0 on success, -errno on failure
22038c2ecf20Sopenharmony_ci */
22048c2ecf20Sopenharmony_cistatic int afu_reset(struct cxlflash_cfg *cfg)
22058c2ecf20Sopenharmony_ci{
22068c2ecf20Sopenharmony_ci	struct device *dev = &cfg->dev->dev;
22078c2ecf20Sopenharmony_ci	int rc = 0;
22088c2ecf20Sopenharmony_ci
22098c2ecf20Sopenharmony_ci	/* Stop the context before the reset. Since the context is
22108c2ecf20Sopenharmony_ci	 * no longer available restart it after the reset is complete
22118c2ecf20Sopenharmony_ci	 */
22128c2ecf20Sopenharmony_ci	term_afu(cfg);
22138c2ecf20Sopenharmony_ci
22148c2ecf20Sopenharmony_ci	rc = init_afu(cfg);
22158c2ecf20Sopenharmony_ci
22168c2ecf20Sopenharmony_ci	dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
22178c2ecf20Sopenharmony_ci	return rc;
22188c2ecf20Sopenharmony_ci}
22198c2ecf20Sopenharmony_ci
22208c2ecf20Sopenharmony_ci/**
22218c2ecf20Sopenharmony_ci * drain_ioctls() - wait until all currently executing ioctls have completed
22228c2ecf20Sopenharmony_ci * @cfg:	Internal structure associated with the host.
22238c2ecf20Sopenharmony_ci *
22248c2ecf20Sopenharmony_ci * Obtain write access to read/write semaphore that wraps ioctl
22258c2ecf20Sopenharmony_ci * handling to 'drain' ioctls currently executing.
22268c2ecf20Sopenharmony_ci */
22278c2ecf20Sopenharmony_cistatic void drain_ioctls(struct cxlflash_cfg *cfg)
22288c2ecf20Sopenharmony_ci{
22298c2ecf20Sopenharmony_ci	down_write(&cfg->ioctl_rwsem);
22308c2ecf20Sopenharmony_ci	up_write(&cfg->ioctl_rwsem);
22318c2ecf20Sopenharmony_ci}
22328c2ecf20Sopenharmony_ci
22338c2ecf20Sopenharmony_ci/**
22348c2ecf20Sopenharmony_ci * cxlflash_async_reset_host() - asynchronous host reset handler
22358c2ecf20Sopenharmony_ci * @data:	Private data provided while scheduling reset.
22368c2ecf20Sopenharmony_ci * @cookie:	Cookie that can be used for checkpointing.
22378c2ecf20Sopenharmony_ci */
22388c2ecf20Sopenharmony_cistatic void cxlflash_async_reset_host(void *data, async_cookie_t cookie)
22398c2ecf20Sopenharmony_ci{
22408c2ecf20Sopenharmony_ci	struct cxlflash_cfg *cfg = data;
22418c2ecf20Sopenharmony_ci	struct device *dev = &cfg->dev->dev;
22428c2ecf20Sopenharmony_ci	int rc = 0;
22438c2ecf20Sopenharmony_ci
22448c2ecf20Sopenharmony_ci	if (cfg->state != STATE_RESET) {
22458c2ecf20Sopenharmony_ci		dev_dbg(dev, "%s: Not performing a reset, state=%d\n",
22468c2ecf20Sopenharmony_ci			__func__, cfg->state);
22478c2ecf20Sopenharmony_ci		goto out;
22488c2ecf20Sopenharmony_ci	}
22498c2ecf20Sopenharmony_ci
22508c2ecf20Sopenharmony_ci	drain_ioctls(cfg);
22518c2ecf20Sopenharmony_ci	cxlflash_mark_contexts_error(cfg);
22528c2ecf20Sopenharmony_ci	rc = afu_reset(cfg);
22538c2ecf20Sopenharmony_ci	if (rc)
22548c2ecf20Sopenharmony_ci		cfg->state = STATE_FAILTERM;
22558c2ecf20Sopenharmony_ci	else
22568c2ecf20Sopenharmony_ci		cfg->state = STATE_NORMAL;
22578c2ecf20Sopenharmony_ci	wake_up_all(&cfg->reset_waitq);
22588c2ecf20Sopenharmony_ci
22598c2ecf20Sopenharmony_ciout:
22608c2ecf20Sopenharmony_ci	scsi_unblock_requests(cfg->host);
22618c2ecf20Sopenharmony_ci}
22628c2ecf20Sopenharmony_ci
22638c2ecf20Sopenharmony_ci/**
22648c2ecf20Sopenharmony_ci * cxlflash_schedule_async_reset() - schedule an asynchronous host reset
22658c2ecf20Sopenharmony_ci * @cfg:	Internal structure associated with the host.
22668c2ecf20Sopenharmony_ci */
22678c2ecf20Sopenharmony_cistatic void cxlflash_schedule_async_reset(struct cxlflash_cfg *cfg)
22688c2ecf20Sopenharmony_ci{
22698c2ecf20Sopenharmony_ci	struct device *dev = &cfg->dev->dev;
22708c2ecf20Sopenharmony_ci
22718c2ecf20Sopenharmony_ci	if (cfg->state != STATE_NORMAL) {
22728c2ecf20Sopenharmony_ci		dev_dbg(dev, "%s: Not performing reset state=%d\n",
22738c2ecf20Sopenharmony_ci			__func__, cfg->state);
22748c2ecf20Sopenharmony_ci		return;
22758c2ecf20Sopenharmony_ci	}
22768c2ecf20Sopenharmony_ci
22778c2ecf20Sopenharmony_ci	cfg->state = STATE_RESET;
22788c2ecf20Sopenharmony_ci	scsi_block_requests(cfg->host);
22798c2ecf20Sopenharmony_ci	cfg->async_reset_cookie = async_schedule(cxlflash_async_reset_host,
22808c2ecf20Sopenharmony_ci						 cfg);
22818c2ecf20Sopenharmony_ci}
22828c2ecf20Sopenharmony_ci
22838c2ecf20Sopenharmony_ci/**
22848c2ecf20Sopenharmony_ci * send_afu_cmd() - builds and sends an internal AFU command
22858c2ecf20Sopenharmony_ci * @afu:	AFU associated with the host.
22868c2ecf20Sopenharmony_ci * @rcb:	Pre-populated IOARCB describing command to send.
22878c2ecf20Sopenharmony_ci *
22888c2ecf20Sopenharmony_ci * The AFU can only take one internal AFU command at a time. This limitation is
22898c2ecf20Sopenharmony_ci * enforced by using a mutex to provide exclusive access to the AFU during the
22908c2ecf20Sopenharmony_ci * operation. This design point requires calling threads to not be on interrupt
22918c2ecf20Sopenharmony_ci * context due to the possibility of sleeping during concurrent AFU operations.
22928c2ecf20Sopenharmony_ci *
22938c2ecf20Sopenharmony_ci * The command status is optionally passed back to the caller when the caller
22948c2ecf20Sopenharmony_ci * populates the IOASA field of the IOARCB with a pointer to an IOASA structure.
22958c2ecf20Sopenharmony_ci *
22968c2ecf20Sopenharmony_ci * Return:
22978c2ecf20Sopenharmony_ci *	0 on success, -errno on failure
22988c2ecf20Sopenharmony_ci */
22998c2ecf20Sopenharmony_cistatic int send_afu_cmd(struct afu *afu, struct sisl_ioarcb *rcb)
23008c2ecf20Sopenharmony_ci{
23018c2ecf20Sopenharmony_ci	struct cxlflash_cfg *cfg = afu->parent;
23028c2ecf20Sopenharmony_ci	struct device *dev = &cfg->dev->dev;
23038c2ecf20Sopenharmony_ci	struct afu_cmd *cmd = NULL;
23048c2ecf20Sopenharmony_ci	struct hwq *hwq = get_hwq(afu, PRIMARY_HWQ);
23058c2ecf20Sopenharmony_ci	ulong lock_flags;
23068c2ecf20Sopenharmony_ci	char *buf = NULL;
23078c2ecf20Sopenharmony_ci	int rc = 0;
23088c2ecf20Sopenharmony_ci	int nretry = 0;
23098c2ecf20Sopenharmony_ci
23108c2ecf20Sopenharmony_ci	if (cfg->state != STATE_NORMAL) {
23118c2ecf20Sopenharmony_ci		dev_dbg(dev, "%s: Sync not required state=%u\n",
23128c2ecf20Sopenharmony_ci			__func__, cfg->state);
23138c2ecf20Sopenharmony_ci		return 0;
23148c2ecf20Sopenharmony_ci	}
23158c2ecf20Sopenharmony_ci
23168c2ecf20Sopenharmony_ci	mutex_lock(&afu->sync_active);
23178c2ecf20Sopenharmony_ci	atomic_inc(&afu->cmds_active);
23188c2ecf20Sopenharmony_ci	buf = kmalloc(sizeof(*cmd) + __alignof__(*cmd) - 1, GFP_KERNEL);
23198c2ecf20Sopenharmony_ci	if (unlikely(!buf)) {
23208c2ecf20Sopenharmony_ci		dev_err(dev, "%s: no memory for command\n", __func__);
23218c2ecf20Sopenharmony_ci		rc = -ENOMEM;
23228c2ecf20Sopenharmony_ci		goto out;
23238c2ecf20Sopenharmony_ci	}
23248c2ecf20Sopenharmony_ci
23258c2ecf20Sopenharmony_ci	cmd = (struct afu_cmd *)PTR_ALIGN(buf, __alignof__(*cmd));
23268c2ecf20Sopenharmony_ci
23278c2ecf20Sopenharmony_ciretry:
23288c2ecf20Sopenharmony_ci	memset(cmd, 0, sizeof(*cmd));
23298c2ecf20Sopenharmony_ci	memcpy(&cmd->rcb, rcb, sizeof(*rcb));
23308c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&cmd->queue);
23318c2ecf20Sopenharmony_ci	init_completion(&cmd->cevent);
23328c2ecf20Sopenharmony_ci	cmd->parent = afu;
23338c2ecf20Sopenharmony_ci	cmd->hwq_index = hwq->index;
23348c2ecf20Sopenharmony_ci	cmd->rcb.ctx_id = hwq->ctx_hndl;
23358c2ecf20Sopenharmony_ci
23368c2ecf20Sopenharmony_ci	dev_dbg(dev, "%s: afu=%p cmd=%p type=%02x nretry=%d\n",
23378c2ecf20Sopenharmony_ci		__func__, afu, cmd, cmd->rcb.cdb[0], nretry);
23388c2ecf20Sopenharmony_ci
23398c2ecf20Sopenharmony_ci	rc = afu->send_cmd(afu, cmd);
23408c2ecf20Sopenharmony_ci	if (unlikely(rc)) {
23418c2ecf20Sopenharmony_ci		rc = -ENOBUFS;
23428c2ecf20Sopenharmony_ci		goto out;
23438c2ecf20Sopenharmony_ci	}
23448c2ecf20Sopenharmony_ci
23458c2ecf20Sopenharmony_ci	rc = wait_resp(afu, cmd);
23468c2ecf20Sopenharmony_ci	switch (rc) {
23478c2ecf20Sopenharmony_ci	case -ETIMEDOUT:
23488c2ecf20Sopenharmony_ci		rc = afu->context_reset(hwq);
23498c2ecf20Sopenharmony_ci		if (rc) {
23508c2ecf20Sopenharmony_ci			/* Delete the command from pending_cmds list */
23518c2ecf20Sopenharmony_ci			spin_lock_irqsave(&hwq->hsq_slock, lock_flags);
23528c2ecf20Sopenharmony_ci			list_del(&cmd->list);
23538c2ecf20Sopenharmony_ci			spin_unlock_irqrestore(&hwq->hsq_slock, lock_flags);
23548c2ecf20Sopenharmony_ci
23558c2ecf20Sopenharmony_ci			cxlflash_schedule_async_reset(cfg);
23568c2ecf20Sopenharmony_ci			break;
23578c2ecf20Sopenharmony_ci		}
23588c2ecf20Sopenharmony_ci		fallthrough;	/* to retry */
23598c2ecf20Sopenharmony_ci	case -EAGAIN:
23608c2ecf20Sopenharmony_ci		if (++nretry < 2)
23618c2ecf20Sopenharmony_ci			goto retry;
23628c2ecf20Sopenharmony_ci		fallthrough;	/* to exit */
23638c2ecf20Sopenharmony_ci	default:
23648c2ecf20Sopenharmony_ci		break;
23658c2ecf20Sopenharmony_ci	}
23668c2ecf20Sopenharmony_ci
23678c2ecf20Sopenharmony_ci	if (rcb->ioasa)
23688c2ecf20Sopenharmony_ci		*rcb->ioasa = cmd->sa;
23698c2ecf20Sopenharmony_ciout:
23708c2ecf20Sopenharmony_ci	atomic_dec(&afu->cmds_active);
23718c2ecf20Sopenharmony_ci	mutex_unlock(&afu->sync_active);
23728c2ecf20Sopenharmony_ci	kfree(buf);
23738c2ecf20Sopenharmony_ci	dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
23748c2ecf20Sopenharmony_ci	return rc;
23758c2ecf20Sopenharmony_ci}
23768c2ecf20Sopenharmony_ci
23778c2ecf20Sopenharmony_ci/**
23788c2ecf20Sopenharmony_ci * cxlflash_afu_sync() - builds and sends an AFU sync command
23798c2ecf20Sopenharmony_ci * @afu:	AFU associated with the host.
23808c2ecf20Sopenharmony_ci * @ctx:	Identifies context requesting sync.
23818c2ecf20Sopenharmony_ci * @res:	Identifies resource requesting sync.
23828c2ecf20Sopenharmony_ci * @mode:	Type of sync to issue (lightweight, heavyweight, global).
23838c2ecf20Sopenharmony_ci *
23848c2ecf20Sopenharmony_ci * AFU sync operations are only necessary and allowed when the device is
23858c2ecf20Sopenharmony_ci * operating normally. When not operating normally, sync requests can occur as
23868c2ecf20Sopenharmony_ci * part of cleaning up resources associated with an adapter prior to removal.
23878c2ecf20Sopenharmony_ci * In this scenario, these requests are simply ignored (safe due to the AFU
23888c2ecf20Sopenharmony_ci * going away).
23898c2ecf20Sopenharmony_ci *
23908c2ecf20Sopenharmony_ci * Return:
23918c2ecf20Sopenharmony_ci *	0 on success, -errno on failure
23928c2ecf20Sopenharmony_ci */
23938c2ecf20Sopenharmony_ciint cxlflash_afu_sync(struct afu *afu, ctx_hndl_t ctx, res_hndl_t res, u8 mode)
23948c2ecf20Sopenharmony_ci{
23958c2ecf20Sopenharmony_ci	struct cxlflash_cfg *cfg = afu->parent;
23968c2ecf20Sopenharmony_ci	struct device *dev = &cfg->dev->dev;
23978c2ecf20Sopenharmony_ci	struct sisl_ioarcb rcb = { 0 };
23988c2ecf20Sopenharmony_ci
23998c2ecf20Sopenharmony_ci	dev_dbg(dev, "%s: afu=%p ctx=%u res=%u mode=%u\n",
24008c2ecf20Sopenharmony_ci		__func__, afu, ctx, res, mode);
24018c2ecf20Sopenharmony_ci
24028c2ecf20Sopenharmony_ci	rcb.req_flags = SISL_REQ_FLAGS_AFU_CMD;
24038c2ecf20Sopenharmony_ci	rcb.msi = SISL_MSI_RRQ_UPDATED;
24048c2ecf20Sopenharmony_ci	rcb.timeout = MC_AFU_SYNC_TIMEOUT;
24058c2ecf20Sopenharmony_ci
24068c2ecf20Sopenharmony_ci	rcb.cdb[0] = SISL_AFU_CMD_SYNC;
24078c2ecf20Sopenharmony_ci	rcb.cdb[1] = mode;
24088c2ecf20Sopenharmony_ci	put_unaligned_be16(ctx, &rcb.cdb[2]);
24098c2ecf20Sopenharmony_ci	put_unaligned_be32(res, &rcb.cdb[4]);
24108c2ecf20Sopenharmony_ci
24118c2ecf20Sopenharmony_ci	return send_afu_cmd(afu, &rcb);
24128c2ecf20Sopenharmony_ci}
24138c2ecf20Sopenharmony_ci
24148c2ecf20Sopenharmony_ci/**
24158c2ecf20Sopenharmony_ci * cxlflash_eh_abort_handler() - abort a SCSI command
24168c2ecf20Sopenharmony_ci * @scp:	SCSI command to abort.
24178c2ecf20Sopenharmony_ci *
24188c2ecf20Sopenharmony_ci * CXL Flash devices do not support a single command abort. Reset the context
24198c2ecf20Sopenharmony_ci * as per SISLite specification. Flush any pending commands in the hardware
24208c2ecf20Sopenharmony_ci * queue before the reset.
24218c2ecf20Sopenharmony_ci *
24228c2ecf20Sopenharmony_ci * Return: SUCCESS/FAILED as defined in scsi/scsi.h
24238c2ecf20Sopenharmony_ci */
24248c2ecf20Sopenharmony_cistatic int cxlflash_eh_abort_handler(struct scsi_cmnd *scp)
24258c2ecf20Sopenharmony_ci{
24268c2ecf20Sopenharmony_ci	int rc = FAILED;
24278c2ecf20Sopenharmony_ci	struct Scsi_Host *host = scp->device->host;
24288c2ecf20Sopenharmony_ci	struct cxlflash_cfg *cfg = shost_priv(host);
24298c2ecf20Sopenharmony_ci	struct afu_cmd *cmd = sc_to_afuc(scp);
24308c2ecf20Sopenharmony_ci	struct device *dev = &cfg->dev->dev;
24318c2ecf20Sopenharmony_ci	struct afu *afu = cfg->afu;
24328c2ecf20Sopenharmony_ci	struct hwq *hwq = get_hwq(afu, cmd->hwq_index);
24338c2ecf20Sopenharmony_ci
24348c2ecf20Sopenharmony_ci	dev_dbg(dev, "%s: (scp=%p) %d/%d/%d/%llu "
24358c2ecf20Sopenharmony_ci		"cdb=(%08x-%08x-%08x-%08x)\n", __func__, scp, host->host_no,
24368c2ecf20Sopenharmony_ci		scp->device->channel, scp->device->id, scp->device->lun,
24378c2ecf20Sopenharmony_ci		get_unaligned_be32(&((u32 *)scp->cmnd)[0]),
24388c2ecf20Sopenharmony_ci		get_unaligned_be32(&((u32 *)scp->cmnd)[1]),
24398c2ecf20Sopenharmony_ci		get_unaligned_be32(&((u32 *)scp->cmnd)[2]),
24408c2ecf20Sopenharmony_ci		get_unaligned_be32(&((u32 *)scp->cmnd)[3]));
24418c2ecf20Sopenharmony_ci
24428c2ecf20Sopenharmony_ci	/* When the state is not normal, another reset/reload is in progress.
24438c2ecf20Sopenharmony_ci	 * Return failed and the mid-layer will invoke host reset handler.
24448c2ecf20Sopenharmony_ci	 */
24458c2ecf20Sopenharmony_ci	if (cfg->state != STATE_NORMAL) {
24468c2ecf20Sopenharmony_ci		dev_dbg(dev, "%s: Invalid state for abort, state=%d\n",
24478c2ecf20Sopenharmony_ci			__func__, cfg->state);
24488c2ecf20Sopenharmony_ci		goto out;
24498c2ecf20Sopenharmony_ci	}
24508c2ecf20Sopenharmony_ci
24518c2ecf20Sopenharmony_ci	rc = afu->context_reset(hwq);
24528c2ecf20Sopenharmony_ci	if (unlikely(rc))
24538c2ecf20Sopenharmony_ci		goto out;
24548c2ecf20Sopenharmony_ci
24558c2ecf20Sopenharmony_ci	rc = SUCCESS;
24568c2ecf20Sopenharmony_ci
24578c2ecf20Sopenharmony_ciout:
24588c2ecf20Sopenharmony_ci	dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
24598c2ecf20Sopenharmony_ci	return rc;
24608c2ecf20Sopenharmony_ci}
24618c2ecf20Sopenharmony_ci
24628c2ecf20Sopenharmony_ci/**
24638c2ecf20Sopenharmony_ci * cxlflash_eh_device_reset_handler() - reset a single LUN
24648c2ecf20Sopenharmony_ci * @scp:	SCSI command to send.
24658c2ecf20Sopenharmony_ci *
24668c2ecf20Sopenharmony_ci * Return:
24678c2ecf20Sopenharmony_ci *	SUCCESS as defined in scsi/scsi.h
24688c2ecf20Sopenharmony_ci *	FAILED as defined in scsi/scsi.h
24698c2ecf20Sopenharmony_ci */
24708c2ecf20Sopenharmony_cistatic int cxlflash_eh_device_reset_handler(struct scsi_cmnd *scp)
24718c2ecf20Sopenharmony_ci{
24728c2ecf20Sopenharmony_ci	int rc = SUCCESS;
24738c2ecf20Sopenharmony_ci	struct scsi_device *sdev = scp->device;
24748c2ecf20Sopenharmony_ci	struct Scsi_Host *host = sdev->host;
24758c2ecf20Sopenharmony_ci	struct cxlflash_cfg *cfg = shost_priv(host);
24768c2ecf20Sopenharmony_ci	struct device *dev = &cfg->dev->dev;
24778c2ecf20Sopenharmony_ci	int rcr = 0;
24788c2ecf20Sopenharmony_ci
24798c2ecf20Sopenharmony_ci	dev_dbg(dev, "%s: %d/%d/%d/%llu\n", __func__,
24808c2ecf20Sopenharmony_ci		host->host_no, sdev->channel, sdev->id, sdev->lun);
24818c2ecf20Sopenharmony_ciretry:
24828c2ecf20Sopenharmony_ci	switch (cfg->state) {
24838c2ecf20Sopenharmony_ci	case STATE_NORMAL:
24848c2ecf20Sopenharmony_ci		rcr = send_tmf(cfg, sdev, TMF_LUN_RESET);
24858c2ecf20Sopenharmony_ci		if (unlikely(rcr))
24868c2ecf20Sopenharmony_ci			rc = FAILED;
24878c2ecf20Sopenharmony_ci		break;
24888c2ecf20Sopenharmony_ci	case STATE_RESET:
24898c2ecf20Sopenharmony_ci		wait_event(cfg->reset_waitq, cfg->state != STATE_RESET);
24908c2ecf20Sopenharmony_ci		goto retry;
24918c2ecf20Sopenharmony_ci	default:
24928c2ecf20Sopenharmony_ci		rc = FAILED;
24938c2ecf20Sopenharmony_ci		break;
24948c2ecf20Sopenharmony_ci	}
24958c2ecf20Sopenharmony_ci
24968c2ecf20Sopenharmony_ci	dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
24978c2ecf20Sopenharmony_ci	return rc;
24988c2ecf20Sopenharmony_ci}
24998c2ecf20Sopenharmony_ci
25008c2ecf20Sopenharmony_ci/**
25018c2ecf20Sopenharmony_ci * cxlflash_eh_host_reset_handler() - reset the host adapter
25028c2ecf20Sopenharmony_ci * @scp:	SCSI command from stack identifying host.
25038c2ecf20Sopenharmony_ci *
25048c2ecf20Sopenharmony_ci * Following a reset, the state is evaluated again in case an EEH occurred
25058c2ecf20Sopenharmony_ci * during the reset. In such a scenario, the host reset will either yield
25068c2ecf20Sopenharmony_ci * until the EEH recovery is complete or return success or failure based
25078c2ecf20Sopenharmony_ci * upon the current device state.
25088c2ecf20Sopenharmony_ci *
25098c2ecf20Sopenharmony_ci * Return:
25108c2ecf20Sopenharmony_ci *	SUCCESS as defined in scsi/scsi.h
25118c2ecf20Sopenharmony_ci *	FAILED as defined in scsi/scsi.h
25128c2ecf20Sopenharmony_ci */
25138c2ecf20Sopenharmony_cistatic int cxlflash_eh_host_reset_handler(struct scsi_cmnd *scp)
25148c2ecf20Sopenharmony_ci{
25158c2ecf20Sopenharmony_ci	int rc = SUCCESS;
25168c2ecf20Sopenharmony_ci	int rcr = 0;
25178c2ecf20Sopenharmony_ci	struct Scsi_Host *host = scp->device->host;
25188c2ecf20Sopenharmony_ci	struct cxlflash_cfg *cfg = shost_priv(host);
25198c2ecf20Sopenharmony_ci	struct device *dev = &cfg->dev->dev;
25208c2ecf20Sopenharmony_ci
25218c2ecf20Sopenharmony_ci	dev_dbg(dev, "%s: %d\n", __func__, host->host_no);
25228c2ecf20Sopenharmony_ci
25238c2ecf20Sopenharmony_ci	switch (cfg->state) {
25248c2ecf20Sopenharmony_ci	case STATE_NORMAL:
25258c2ecf20Sopenharmony_ci		cfg->state = STATE_RESET;
25268c2ecf20Sopenharmony_ci		drain_ioctls(cfg);
25278c2ecf20Sopenharmony_ci		cxlflash_mark_contexts_error(cfg);
25288c2ecf20Sopenharmony_ci		rcr = afu_reset(cfg);
25298c2ecf20Sopenharmony_ci		if (rcr) {
25308c2ecf20Sopenharmony_ci			rc = FAILED;
25318c2ecf20Sopenharmony_ci			cfg->state = STATE_FAILTERM;
25328c2ecf20Sopenharmony_ci		} else
25338c2ecf20Sopenharmony_ci			cfg->state = STATE_NORMAL;
25348c2ecf20Sopenharmony_ci		wake_up_all(&cfg->reset_waitq);
25358c2ecf20Sopenharmony_ci		ssleep(1);
25368c2ecf20Sopenharmony_ci		fallthrough;
25378c2ecf20Sopenharmony_ci	case STATE_RESET:
25388c2ecf20Sopenharmony_ci		wait_event(cfg->reset_waitq, cfg->state != STATE_RESET);
25398c2ecf20Sopenharmony_ci		if (cfg->state == STATE_NORMAL)
25408c2ecf20Sopenharmony_ci			break;
25418c2ecf20Sopenharmony_ci		fallthrough;
25428c2ecf20Sopenharmony_ci	default:
25438c2ecf20Sopenharmony_ci		rc = FAILED;
25448c2ecf20Sopenharmony_ci		break;
25458c2ecf20Sopenharmony_ci	}
25468c2ecf20Sopenharmony_ci
25478c2ecf20Sopenharmony_ci	dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
25488c2ecf20Sopenharmony_ci	return rc;
25498c2ecf20Sopenharmony_ci}
25508c2ecf20Sopenharmony_ci
25518c2ecf20Sopenharmony_ci/**
25528c2ecf20Sopenharmony_ci * cxlflash_change_queue_depth() - change the queue depth for the device
25538c2ecf20Sopenharmony_ci * @sdev:	SCSI device destined for queue depth change.
25548c2ecf20Sopenharmony_ci * @qdepth:	Requested queue depth value to set.
25558c2ecf20Sopenharmony_ci *
25568c2ecf20Sopenharmony_ci * The requested queue depth is capped to the maximum supported value.
25578c2ecf20Sopenharmony_ci *
25588c2ecf20Sopenharmony_ci * Return: The actual queue depth set.
25598c2ecf20Sopenharmony_ci */
25608c2ecf20Sopenharmony_cistatic int cxlflash_change_queue_depth(struct scsi_device *sdev, int qdepth)
25618c2ecf20Sopenharmony_ci{
25628c2ecf20Sopenharmony_ci
25638c2ecf20Sopenharmony_ci	if (qdepth > CXLFLASH_MAX_CMDS_PER_LUN)
25648c2ecf20Sopenharmony_ci		qdepth = CXLFLASH_MAX_CMDS_PER_LUN;
25658c2ecf20Sopenharmony_ci
25668c2ecf20Sopenharmony_ci	scsi_change_queue_depth(sdev, qdepth);
25678c2ecf20Sopenharmony_ci	return sdev->queue_depth;
25688c2ecf20Sopenharmony_ci}
25698c2ecf20Sopenharmony_ci
25708c2ecf20Sopenharmony_ci/**
25718c2ecf20Sopenharmony_ci * cxlflash_show_port_status() - queries and presents the current port status
25728c2ecf20Sopenharmony_ci * @port:	Desired port for status reporting.
25738c2ecf20Sopenharmony_ci * @cfg:	Internal structure associated with the host.
25748c2ecf20Sopenharmony_ci * @buf:	Buffer of length PAGE_SIZE to report back port status in ASCII.
25758c2ecf20Sopenharmony_ci *
25768c2ecf20Sopenharmony_ci * Return: The size of the ASCII string returned in @buf or -EINVAL.
25778c2ecf20Sopenharmony_ci */
25788c2ecf20Sopenharmony_cistatic ssize_t cxlflash_show_port_status(u32 port,
25798c2ecf20Sopenharmony_ci					 struct cxlflash_cfg *cfg,
25808c2ecf20Sopenharmony_ci					 char *buf)
25818c2ecf20Sopenharmony_ci{
25828c2ecf20Sopenharmony_ci	struct device *dev = &cfg->dev->dev;
25838c2ecf20Sopenharmony_ci	char *disp_status;
25848c2ecf20Sopenharmony_ci	u64 status;
25858c2ecf20Sopenharmony_ci	__be64 __iomem *fc_port_regs;
25868c2ecf20Sopenharmony_ci
25878c2ecf20Sopenharmony_ci	WARN_ON(port >= MAX_FC_PORTS);
25888c2ecf20Sopenharmony_ci
25898c2ecf20Sopenharmony_ci	if (port >= cfg->num_fc_ports) {
25908c2ecf20Sopenharmony_ci		dev_info(dev, "%s: Port %d not supported on this card.\n",
25918c2ecf20Sopenharmony_ci			__func__, port);
25928c2ecf20Sopenharmony_ci		return -EINVAL;
25938c2ecf20Sopenharmony_ci	}
25948c2ecf20Sopenharmony_ci
25958c2ecf20Sopenharmony_ci	fc_port_regs = get_fc_port_regs(cfg, port);
25968c2ecf20Sopenharmony_ci	status = readq_be(&fc_port_regs[FC_MTIP_STATUS / 8]);
25978c2ecf20Sopenharmony_ci	status &= FC_MTIP_STATUS_MASK;
25988c2ecf20Sopenharmony_ci
25998c2ecf20Sopenharmony_ci	if (status == FC_MTIP_STATUS_ONLINE)
26008c2ecf20Sopenharmony_ci		disp_status = "online";
26018c2ecf20Sopenharmony_ci	else if (status == FC_MTIP_STATUS_OFFLINE)
26028c2ecf20Sopenharmony_ci		disp_status = "offline";
26038c2ecf20Sopenharmony_ci	else
26048c2ecf20Sopenharmony_ci		disp_status = "unknown";
26058c2ecf20Sopenharmony_ci
26068c2ecf20Sopenharmony_ci	return scnprintf(buf, PAGE_SIZE, "%s\n", disp_status);
26078c2ecf20Sopenharmony_ci}
26088c2ecf20Sopenharmony_ci
26098c2ecf20Sopenharmony_ci/**
26108c2ecf20Sopenharmony_ci * port0_show() - queries and presents the current status of port 0
26118c2ecf20Sopenharmony_ci * @dev:	Generic device associated with the host owning the port.
26128c2ecf20Sopenharmony_ci * @attr:	Device attribute representing the port.
26138c2ecf20Sopenharmony_ci * @buf:	Buffer of length PAGE_SIZE to report back port status in ASCII.
26148c2ecf20Sopenharmony_ci *
26158c2ecf20Sopenharmony_ci * Return: The size of the ASCII string returned in @buf.
26168c2ecf20Sopenharmony_ci */
26178c2ecf20Sopenharmony_cistatic ssize_t port0_show(struct device *dev,
26188c2ecf20Sopenharmony_ci			  struct device_attribute *attr,
26198c2ecf20Sopenharmony_ci			  char *buf)
26208c2ecf20Sopenharmony_ci{
26218c2ecf20Sopenharmony_ci	struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
26228c2ecf20Sopenharmony_ci
26238c2ecf20Sopenharmony_ci	return cxlflash_show_port_status(0, cfg, buf);
26248c2ecf20Sopenharmony_ci}
26258c2ecf20Sopenharmony_ci
26268c2ecf20Sopenharmony_ci/**
26278c2ecf20Sopenharmony_ci * port1_show() - queries and presents the current status of port 1
26288c2ecf20Sopenharmony_ci * @dev:	Generic device associated with the host owning the port.
26298c2ecf20Sopenharmony_ci * @attr:	Device attribute representing the port.
26308c2ecf20Sopenharmony_ci * @buf:	Buffer of length PAGE_SIZE to report back port status in ASCII.
26318c2ecf20Sopenharmony_ci *
26328c2ecf20Sopenharmony_ci * Return: The size of the ASCII string returned in @buf.
26338c2ecf20Sopenharmony_ci */
26348c2ecf20Sopenharmony_cistatic ssize_t port1_show(struct device *dev,
26358c2ecf20Sopenharmony_ci			  struct device_attribute *attr,
26368c2ecf20Sopenharmony_ci			  char *buf)
26378c2ecf20Sopenharmony_ci{
26388c2ecf20Sopenharmony_ci	struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
26398c2ecf20Sopenharmony_ci
26408c2ecf20Sopenharmony_ci	return cxlflash_show_port_status(1, cfg, buf);
26418c2ecf20Sopenharmony_ci}
26428c2ecf20Sopenharmony_ci
26438c2ecf20Sopenharmony_ci/**
26448c2ecf20Sopenharmony_ci * port2_show() - queries and presents the current status of port 2
26458c2ecf20Sopenharmony_ci * @dev:	Generic device associated with the host owning the port.
26468c2ecf20Sopenharmony_ci * @attr:	Device attribute representing the port.
26478c2ecf20Sopenharmony_ci * @buf:	Buffer of length PAGE_SIZE to report back port status in ASCII.
26488c2ecf20Sopenharmony_ci *
26498c2ecf20Sopenharmony_ci * Return: The size of the ASCII string returned in @buf.
26508c2ecf20Sopenharmony_ci */
26518c2ecf20Sopenharmony_cistatic ssize_t port2_show(struct device *dev,
26528c2ecf20Sopenharmony_ci			  struct device_attribute *attr,
26538c2ecf20Sopenharmony_ci			  char *buf)
26548c2ecf20Sopenharmony_ci{
26558c2ecf20Sopenharmony_ci	struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
26568c2ecf20Sopenharmony_ci
26578c2ecf20Sopenharmony_ci	return cxlflash_show_port_status(2, cfg, buf);
26588c2ecf20Sopenharmony_ci}
26598c2ecf20Sopenharmony_ci
26608c2ecf20Sopenharmony_ci/**
26618c2ecf20Sopenharmony_ci * port3_show() - queries and presents the current status of port 3
26628c2ecf20Sopenharmony_ci * @dev:	Generic device associated with the host owning the port.
26638c2ecf20Sopenharmony_ci * @attr:	Device attribute representing the port.
26648c2ecf20Sopenharmony_ci * @buf:	Buffer of length PAGE_SIZE to report back port status in ASCII.
26658c2ecf20Sopenharmony_ci *
26668c2ecf20Sopenharmony_ci * Return: The size of the ASCII string returned in @buf.
26678c2ecf20Sopenharmony_ci */
26688c2ecf20Sopenharmony_cistatic ssize_t port3_show(struct device *dev,
26698c2ecf20Sopenharmony_ci			  struct device_attribute *attr,
26708c2ecf20Sopenharmony_ci			  char *buf)
26718c2ecf20Sopenharmony_ci{
26728c2ecf20Sopenharmony_ci	struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
26738c2ecf20Sopenharmony_ci
26748c2ecf20Sopenharmony_ci	return cxlflash_show_port_status(3, cfg, buf);
26758c2ecf20Sopenharmony_ci}
26768c2ecf20Sopenharmony_ci
26778c2ecf20Sopenharmony_ci/**
26788c2ecf20Sopenharmony_ci * lun_mode_show() - presents the current LUN mode of the host
26798c2ecf20Sopenharmony_ci * @dev:	Generic device associated with the host.
26808c2ecf20Sopenharmony_ci * @attr:	Device attribute representing the LUN mode.
26818c2ecf20Sopenharmony_ci * @buf:	Buffer of length PAGE_SIZE to report back the LUN mode in ASCII.
26828c2ecf20Sopenharmony_ci *
26838c2ecf20Sopenharmony_ci * Return: The size of the ASCII string returned in @buf.
26848c2ecf20Sopenharmony_ci */
26858c2ecf20Sopenharmony_cistatic ssize_t lun_mode_show(struct device *dev,
26868c2ecf20Sopenharmony_ci			     struct device_attribute *attr, char *buf)
26878c2ecf20Sopenharmony_ci{
26888c2ecf20Sopenharmony_ci	struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
26898c2ecf20Sopenharmony_ci	struct afu *afu = cfg->afu;
26908c2ecf20Sopenharmony_ci
26918c2ecf20Sopenharmony_ci	return scnprintf(buf, PAGE_SIZE, "%u\n", afu->internal_lun);
26928c2ecf20Sopenharmony_ci}
26938c2ecf20Sopenharmony_ci
26948c2ecf20Sopenharmony_ci/**
26958c2ecf20Sopenharmony_ci * lun_mode_store() - sets the LUN mode of the host
26968c2ecf20Sopenharmony_ci * @dev:	Generic device associated with the host.
26978c2ecf20Sopenharmony_ci * @attr:	Device attribute representing the LUN mode.
26988c2ecf20Sopenharmony_ci * @buf:	Buffer of length PAGE_SIZE containing the LUN mode in ASCII.
26998c2ecf20Sopenharmony_ci * @count:	Length of data resizing in @buf.
27008c2ecf20Sopenharmony_ci *
27018c2ecf20Sopenharmony_ci * The CXL Flash AFU supports a dummy LUN mode where the external
27028c2ecf20Sopenharmony_ci * links and storage are not required. Space on the FPGA is used
27038c2ecf20Sopenharmony_ci * to create 1 or 2 small LUNs which are presented to the system
27048c2ecf20Sopenharmony_ci * as if they were a normal storage device. This feature is useful
27058c2ecf20Sopenharmony_ci * during development and also provides manufacturing with a way
27068c2ecf20Sopenharmony_ci * to test the AFU without an actual device.
27078c2ecf20Sopenharmony_ci *
27088c2ecf20Sopenharmony_ci * 0 = external LUN[s] (default)
27098c2ecf20Sopenharmony_ci * 1 = internal LUN (1 x 64K, 512B blocks, id 0)
27108c2ecf20Sopenharmony_ci * 2 = internal LUN (1 x 64K, 4K blocks, id 0)
27118c2ecf20Sopenharmony_ci * 3 = internal LUN (2 x 32K, 512B blocks, ids 0,1)
27128c2ecf20Sopenharmony_ci * 4 = internal LUN (2 x 32K, 4K blocks, ids 0,1)
27138c2ecf20Sopenharmony_ci *
27148c2ecf20Sopenharmony_ci * Return: The size of the ASCII string returned in @buf.
27158c2ecf20Sopenharmony_ci */
27168c2ecf20Sopenharmony_cistatic ssize_t lun_mode_store(struct device *dev,
27178c2ecf20Sopenharmony_ci			      struct device_attribute *attr,
27188c2ecf20Sopenharmony_ci			      const char *buf, size_t count)
27198c2ecf20Sopenharmony_ci{
27208c2ecf20Sopenharmony_ci	struct Scsi_Host *shost = class_to_shost(dev);
27218c2ecf20Sopenharmony_ci	struct cxlflash_cfg *cfg = shost_priv(shost);
27228c2ecf20Sopenharmony_ci	struct afu *afu = cfg->afu;
27238c2ecf20Sopenharmony_ci	int rc;
27248c2ecf20Sopenharmony_ci	u32 lun_mode;
27258c2ecf20Sopenharmony_ci
27268c2ecf20Sopenharmony_ci	rc = kstrtouint(buf, 10, &lun_mode);
27278c2ecf20Sopenharmony_ci	if (!rc && (lun_mode < 5) && (lun_mode != afu->internal_lun)) {
27288c2ecf20Sopenharmony_ci		afu->internal_lun = lun_mode;
27298c2ecf20Sopenharmony_ci
27308c2ecf20Sopenharmony_ci		/*
27318c2ecf20Sopenharmony_ci		 * When configured for internal LUN, there is only one channel,
27328c2ecf20Sopenharmony_ci		 * channel number 0, else there will be one less than the number
27338c2ecf20Sopenharmony_ci		 * of fc ports for this card.
27348c2ecf20Sopenharmony_ci		 */
27358c2ecf20Sopenharmony_ci		if (afu->internal_lun)
27368c2ecf20Sopenharmony_ci			shost->max_channel = 0;
27378c2ecf20Sopenharmony_ci		else
27388c2ecf20Sopenharmony_ci			shost->max_channel = PORTNUM2CHAN(cfg->num_fc_ports);
27398c2ecf20Sopenharmony_ci
27408c2ecf20Sopenharmony_ci		afu_reset(cfg);
27418c2ecf20Sopenharmony_ci		scsi_scan_host(cfg->host);
27428c2ecf20Sopenharmony_ci	}
27438c2ecf20Sopenharmony_ci
27448c2ecf20Sopenharmony_ci	return count;
27458c2ecf20Sopenharmony_ci}
27468c2ecf20Sopenharmony_ci
27478c2ecf20Sopenharmony_ci/**
27488c2ecf20Sopenharmony_ci * ioctl_version_show() - presents the current ioctl version of the host
27498c2ecf20Sopenharmony_ci * @dev:	Generic device associated with the host.
27508c2ecf20Sopenharmony_ci * @attr:	Device attribute representing the ioctl version.
27518c2ecf20Sopenharmony_ci * @buf:	Buffer of length PAGE_SIZE to report back the ioctl version.
27528c2ecf20Sopenharmony_ci *
27538c2ecf20Sopenharmony_ci * Return: The size of the ASCII string returned in @buf.
27548c2ecf20Sopenharmony_ci */
27558c2ecf20Sopenharmony_cistatic ssize_t ioctl_version_show(struct device *dev,
27568c2ecf20Sopenharmony_ci				  struct device_attribute *attr, char *buf)
27578c2ecf20Sopenharmony_ci{
27588c2ecf20Sopenharmony_ci	ssize_t bytes = 0;
27598c2ecf20Sopenharmony_ci
27608c2ecf20Sopenharmony_ci	bytes = scnprintf(buf, PAGE_SIZE,
27618c2ecf20Sopenharmony_ci			  "disk: %u\n", DK_CXLFLASH_VERSION_0);
27628c2ecf20Sopenharmony_ci	bytes += scnprintf(buf + bytes, PAGE_SIZE - bytes,
27638c2ecf20Sopenharmony_ci			   "host: %u\n", HT_CXLFLASH_VERSION_0);
27648c2ecf20Sopenharmony_ci
27658c2ecf20Sopenharmony_ci	return bytes;
27668c2ecf20Sopenharmony_ci}
27678c2ecf20Sopenharmony_ci
27688c2ecf20Sopenharmony_ci/**
27698c2ecf20Sopenharmony_ci * cxlflash_show_port_lun_table() - queries and presents the port LUN table
27708c2ecf20Sopenharmony_ci * @port:	Desired port for status reporting.
27718c2ecf20Sopenharmony_ci * @cfg:	Internal structure associated with the host.
27728c2ecf20Sopenharmony_ci * @buf:	Buffer of length PAGE_SIZE to report back port status in ASCII.
27738c2ecf20Sopenharmony_ci *
27748c2ecf20Sopenharmony_ci * Return: The size of the ASCII string returned in @buf or -EINVAL.
27758c2ecf20Sopenharmony_ci */
27768c2ecf20Sopenharmony_cistatic ssize_t cxlflash_show_port_lun_table(u32 port,
27778c2ecf20Sopenharmony_ci					    struct cxlflash_cfg *cfg,
27788c2ecf20Sopenharmony_ci					    char *buf)
27798c2ecf20Sopenharmony_ci{
27808c2ecf20Sopenharmony_ci	struct device *dev = &cfg->dev->dev;
27818c2ecf20Sopenharmony_ci	__be64 __iomem *fc_port_luns;
27828c2ecf20Sopenharmony_ci	int i;
27838c2ecf20Sopenharmony_ci	ssize_t bytes = 0;
27848c2ecf20Sopenharmony_ci
27858c2ecf20Sopenharmony_ci	WARN_ON(port >= MAX_FC_PORTS);
27868c2ecf20Sopenharmony_ci
27878c2ecf20Sopenharmony_ci	if (port >= cfg->num_fc_ports) {
27888c2ecf20Sopenharmony_ci		dev_info(dev, "%s: Port %d not supported on this card.\n",
27898c2ecf20Sopenharmony_ci			__func__, port);
27908c2ecf20Sopenharmony_ci		return -EINVAL;
27918c2ecf20Sopenharmony_ci	}
27928c2ecf20Sopenharmony_ci
27938c2ecf20Sopenharmony_ci	fc_port_luns = get_fc_port_luns(cfg, port);
27948c2ecf20Sopenharmony_ci
27958c2ecf20Sopenharmony_ci	for (i = 0; i < CXLFLASH_NUM_VLUNS; i++)
27968c2ecf20Sopenharmony_ci		bytes += scnprintf(buf + bytes, PAGE_SIZE - bytes,
27978c2ecf20Sopenharmony_ci				   "%03d: %016llx\n",
27988c2ecf20Sopenharmony_ci				   i, readq_be(&fc_port_luns[i]));
27998c2ecf20Sopenharmony_ci	return bytes;
28008c2ecf20Sopenharmony_ci}
28018c2ecf20Sopenharmony_ci
28028c2ecf20Sopenharmony_ci/**
28038c2ecf20Sopenharmony_ci * port0_lun_table_show() - presents the current LUN table of port 0
28048c2ecf20Sopenharmony_ci * @dev:	Generic device associated with the host owning the port.
28058c2ecf20Sopenharmony_ci * @attr:	Device attribute representing the port.
28068c2ecf20Sopenharmony_ci * @buf:	Buffer of length PAGE_SIZE to report back port status in ASCII.
28078c2ecf20Sopenharmony_ci *
28088c2ecf20Sopenharmony_ci * Return: The size of the ASCII string returned in @buf.
28098c2ecf20Sopenharmony_ci */
28108c2ecf20Sopenharmony_cistatic ssize_t port0_lun_table_show(struct device *dev,
28118c2ecf20Sopenharmony_ci				    struct device_attribute *attr,
28128c2ecf20Sopenharmony_ci				    char *buf)
28138c2ecf20Sopenharmony_ci{
28148c2ecf20Sopenharmony_ci	struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
28158c2ecf20Sopenharmony_ci
28168c2ecf20Sopenharmony_ci	return cxlflash_show_port_lun_table(0, cfg, buf);
28178c2ecf20Sopenharmony_ci}
28188c2ecf20Sopenharmony_ci
28198c2ecf20Sopenharmony_ci/**
28208c2ecf20Sopenharmony_ci * port1_lun_table_show() - presents the current LUN table of port 1
28218c2ecf20Sopenharmony_ci * @dev:	Generic device associated with the host owning the port.
28228c2ecf20Sopenharmony_ci * @attr:	Device attribute representing the port.
28238c2ecf20Sopenharmony_ci * @buf:	Buffer of length PAGE_SIZE to report back port status in ASCII.
28248c2ecf20Sopenharmony_ci *
28258c2ecf20Sopenharmony_ci * Return: The size of the ASCII string returned in @buf.
28268c2ecf20Sopenharmony_ci */
28278c2ecf20Sopenharmony_cistatic ssize_t port1_lun_table_show(struct device *dev,
28288c2ecf20Sopenharmony_ci				    struct device_attribute *attr,
28298c2ecf20Sopenharmony_ci				    char *buf)
28308c2ecf20Sopenharmony_ci{
28318c2ecf20Sopenharmony_ci	struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
28328c2ecf20Sopenharmony_ci
28338c2ecf20Sopenharmony_ci	return cxlflash_show_port_lun_table(1, cfg, buf);
28348c2ecf20Sopenharmony_ci}
28358c2ecf20Sopenharmony_ci
28368c2ecf20Sopenharmony_ci/**
28378c2ecf20Sopenharmony_ci * port2_lun_table_show() - presents the current LUN table of port 2
28388c2ecf20Sopenharmony_ci * @dev:	Generic device associated with the host owning the port.
28398c2ecf20Sopenharmony_ci * @attr:	Device attribute representing the port.
28408c2ecf20Sopenharmony_ci * @buf:	Buffer of length PAGE_SIZE to report back port status in ASCII.
28418c2ecf20Sopenharmony_ci *
28428c2ecf20Sopenharmony_ci * Return: The size of the ASCII string returned in @buf.
28438c2ecf20Sopenharmony_ci */
28448c2ecf20Sopenharmony_cistatic ssize_t port2_lun_table_show(struct device *dev,
28458c2ecf20Sopenharmony_ci				    struct device_attribute *attr,
28468c2ecf20Sopenharmony_ci				    char *buf)
28478c2ecf20Sopenharmony_ci{
28488c2ecf20Sopenharmony_ci	struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
28498c2ecf20Sopenharmony_ci
28508c2ecf20Sopenharmony_ci	return cxlflash_show_port_lun_table(2, cfg, buf);
28518c2ecf20Sopenharmony_ci}
28528c2ecf20Sopenharmony_ci
28538c2ecf20Sopenharmony_ci/**
28548c2ecf20Sopenharmony_ci * port3_lun_table_show() - presents the current LUN table of port 3
28558c2ecf20Sopenharmony_ci * @dev:	Generic device associated with the host owning the port.
28568c2ecf20Sopenharmony_ci * @attr:	Device attribute representing the port.
28578c2ecf20Sopenharmony_ci * @buf:	Buffer of length PAGE_SIZE to report back port status in ASCII.
28588c2ecf20Sopenharmony_ci *
28598c2ecf20Sopenharmony_ci * Return: The size of the ASCII string returned in @buf.
28608c2ecf20Sopenharmony_ci */
28618c2ecf20Sopenharmony_cistatic ssize_t port3_lun_table_show(struct device *dev,
28628c2ecf20Sopenharmony_ci				    struct device_attribute *attr,
28638c2ecf20Sopenharmony_ci				    char *buf)
28648c2ecf20Sopenharmony_ci{
28658c2ecf20Sopenharmony_ci	struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
28668c2ecf20Sopenharmony_ci
28678c2ecf20Sopenharmony_ci	return cxlflash_show_port_lun_table(3, cfg, buf);
28688c2ecf20Sopenharmony_ci}
28698c2ecf20Sopenharmony_ci
28708c2ecf20Sopenharmony_ci/**
28718c2ecf20Sopenharmony_ci * irqpoll_weight_show() - presents the current IRQ poll weight for the host
28728c2ecf20Sopenharmony_ci * @dev:	Generic device associated with the host.
28738c2ecf20Sopenharmony_ci * @attr:	Device attribute representing the IRQ poll weight.
28748c2ecf20Sopenharmony_ci * @buf:	Buffer of length PAGE_SIZE to report back the current IRQ poll
28758c2ecf20Sopenharmony_ci *		weight in ASCII.
28768c2ecf20Sopenharmony_ci *
28778c2ecf20Sopenharmony_ci * An IRQ poll weight of 0 indicates polling is disabled.
28788c2ecf20Sopenharmony_ci *
28798c2ecf20Sopenharmony_ci * Return: The size of the ASCII string returned in @buf.
28808c2ecf20Sopenharmony_ci */
28818c2ecf20Sopenharmony_cistatic ssize_t irqpoll_weight_show(struct device *dev,
28828c2ecf20Sopenharmony_ci				   struct device_attribute *attr, char *buf)
28838c2ecf20Sopenharmony_ci{
28848c2ecf20Sopenharmony_ci	struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
28858c2ecf20Sopenharmony_ci	struct afu *afu = cfg->afu;
28868c2ecf20Sopenharmony_ci
28878c2ecf20Sopenharmony_ci	return scnprintf(buf, PAGE_SIZE, "%u\n", afu->irqpoll_weight);
28888c2ecf20Sopenharmony_ci}
28898c2ecf20Sopenharmony_ci
28908c2ecf20Sopenharmony_ci/**
28918c2ecf20Sopenharmony_ci * irqpoll_weight_store() - sets the current IRQ poll weight for the host
28928c2ecf20Sopenharmony_ci * @dev:	Generic device associated with the host.
28938c2ecf20Sopenharmony_ci * @attr:	Device attribute representing the IRQ poll weight.
28948c2ecf20Sopenharmony_ci * @buf:	Buffer of length PAGE_SIZE containing the desired IRQ poll
28958c2ecf20Sopenharmony_ci *		weight in ASCII.
28968c2ecf20Sopenharmony_ci * @count:	Length of data resizing in @buf.
28978c2ecf20Sopenharmony_ci *
28988c2ecf20Sopenharmony_ci * An IRQ poll weight of 0 indicates polling is disabled.
28998c2ecf20Sopenharmony_ci *
29008c2ecf20Sopenharmony_ci * Return: The size of the ASCII string returned in @buf.
29018c2ecf20Sopenharmony_ci */
29028c2ecf20Sopenharmony_cistatic ssize_t irqpoll_weight_store(struct device *dev,
29038c2ecf20Sopenharmony_ci				    struct device_attribute *attr,
29048c2ecf20Sopenharmony_ci				    const char *buf, size_t count)
29058c2ecf20Sopenharmony_ci{
29068c2ecf20Sopenharmony_ci	struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
29078c2ecf20Sopenharmony_ci	struct device *cfgdev = &cfg->dev->dev;
29088c2ecf20Sopenharmony_ci	struct afu *afu = cfg->afu;
29098c2ecf20Sopenharmony_ci	struct hwq *hwq;
29108c2ecf20Sopenharmony_ci	u32 weight;
29118c2ecf20Sopenharmony_ci	int rc, i;
29128c2ecf20Sopenharmony_ci
29138c2ecf20Sopenharmony_ci	rc = kstrtouint(buf, 10, &weight);
29148c2ecf20Sopenharmony_ci	if (rc)
29158c2ecf20Sopenharmony_ci		return -EINVAL;
29168c2ecf20Sopenharmony_ci
29178c2ecf20Sopenharmony_ci	if (weight > 256) {
29188c2ecf20Sopenharmony_ci		dev_info(cfgdev,
29198c2ecf20Sopenharmony_ci			 "Invalid IRQ poll weight. It must be 256 or less.\n");
29208c2ecf20Sopenharmony_ci		return -EINVAL;
29218c2ecf20Sopenharmony_ci	}
29228c2ecf20Sopenharmony_ci
29238c2ecf20Sopenharmony_ci	if (weight == afu->irqpoll_weight) {
29248c2ecf20Sopenharmony_ci		dev_info(cfgdev,
29258c2ecf20Sopenharmony_ci			 "Current IRQ poll weight has the same weight.\n");
29268c2ecf20Sopenharmony_ci		return -EINVAL;
29278c2ecf20Sopenharmony_ci	}
29288c2ecf20Sopenharmony_ci
29298c2ecf20Sopenharmony_ci	if (afu_is_irqpoll_enabled(afu)) {
29308c2ecf20Sopenharmony_ci		for (i = 0; i < afu->num_hwqs; i++) {
29318c2ecf20Sopenharmony_ci			hwq = get_hwq(afu, i);
29328c2ecf20Sopenharmony_ci
29338c2ecf20Sopenharmony_ci			irq_poll_disable(&hwq->irqpoll);
29348c2ecf20Sopenharmony_ci		}
29358c2ecf20Sopenharmony_ci	}
29368c2ecf20Sopenharmony_ci
29378c2ecf20Sopenharmony_ci	afu->irqpoll_weight = weight;
29388c2ecf20Sopenharmony_ci
29398c2ecf20Sopenharmony_ci	if (weight > 0) {
29408c2ecf20Sopenharmony_ci		for (i = 0; i < afu->num_hwqs; i++) {
29418c2ecf20Sopenharmony_ci			hwq = get_hwq(afu, i);
29428c2ecf20Sopenharmony_ci
29438c2ecf20Sopenharmony_ci			irq_poll_init(&hwq->irqpoll, weight, cxlflash_irqpoll);
29448c2ecf20Sopenharmony_ci		}
29458c2ecf20Sopenharmony_ci	}
29468c2ecf20Sopenharmony_ci
29478c2ecf20Sopenharmony_ci	return count;
29488c2ecf20Sopenharmony_ci}
29498c2ecf20Sopenharmony_ci
29508c2ecf20Sopenharmony_ci/**
29518c2ecf20Sopenharmony_ci * num_hwqs_show() - presents the number of hardware queues for the host
29528c2ecf20Sopenharmony_ci * @dev:	Generic device associated with the host.
29538c2ecf20Sopenharmony_ci * @attr:	Device attribute representing the number of hardware queues.
29548c2ecf20Sopenharmony_ci * @buf:	Buffer of length PAGE_SIZE to report back the number of hardware
29558c2ecf20Sopenharmony_ci *		queues in ASCII.
29568c2ecf20Sopenharmony_ci *
29578c2ecf20Sopenharmony_ci * Return: The size of the ASCII string returned in @buf.
29588c2ecf20Sopenharmony_ci */
29598c2ecf20Sopenharmony_cistatic ssize_t num_hwqs_show(struct device *dev,
29608c2ecf20Sopenharmony_ci			     struct device_attribute *attr, char *buf)
29618c2ecf20Sopenharmony_ci{
29628c2ecf20Sopenharmony_ci	struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
29638c2ecf20Sopenharmony_ci	struct afu *afu = cfg->afu;
29648c2ecf20Sopenharmony_ci
29658c2ecf20Sopenharmony_ci	return scnprintf(buf, PAGE_SIZE, "%u\n", afu->num_hwqs);
29668c2ecf20Sopenharmony_ci}
29678c2ecf20Sopenharmony_ci
29688c2ecf20Sopenharmony_ci/**
29698c2ecf20Sopenharmony_ci * num_hwqs_store() - sets the number of hardware queues for the host
29708c2ecf20Sopenharmony_ci * @dev:	Generic device associated with the host.
29718c2ecf20Sopenharmony_ci * @attr:	Device attribute representing the number of hardware queues.
29728c2ecf20Sopenharmony_ci * @buf:	Buffer of length PAGE_SIZE containing the number of hardware
29738c2ecf20Sopenharmony_ci *		queues in ASCII.
29748c2ecf20Sopenharmony_ci * @count:	Length of data resizing in @buf.
29758c2ecf20Sopenharmony_ci *
29768c2ecf20Sopenharmony_ci * n > 0: num_hwqs = n
29778c2ecf20Sopenharmony_ci * n = 0: num_hwqs = num_online_cpus()
29788c2ecf20Sopenharmony_ci * n < 0: num_online_cpus() / abs(n)
29798c2ecf20Sopenharmony_ci *
29808c2ecf20Sopenharmony_ci * Return: The size of the ASCII string returned in @buf.
29818c2ecf20Sopenharmony_ci */
29828c2ecf20Sopenharmony_cistatic ssize_t num_hwqs_store(struct device *dev,
29838c2ecf20Sopenharmony_ci			      struct device_attribute *attr,
29848c2ecf20Sopenharmony_ci			      const char *buf, size_t count)
29858c2ecf20Sopenharmony_ci{
29868c2ecf20Sopenharmony_ci	struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
29878c2ecf20Sopenharmony_ci	struct afu *afu = cfg->afu;
29888c2ecf20Sopenharmony_ci	int rc;
29898c2ecf20Sopenharmony_ci	int nhwqs, num_hwqs;
29908c2ecf20Sopenharmony_ci
29918c2ecf20Sopenharmony_ci	rc = kstrtoint(buf, 10, &nhwqs);
29928c2ecf20Sopenharmony_ci	if (rc)
29938c2ecf20Sopenharmony_ci		return -EINVAL;
29948c2ecf20Sopenharmony_ci
29958c2ecf20Sopenharmony_ci	if (nhwqs >= 1)
29968c2ecf20Sopenharmony_ci		num_hwqs = nhwqs;
29978c2ecf20Sopenharmony_ci	else if (nhwqs == 0)
29988c2ecf20Sopenharmony_ci		num_hwqs = num_online_cpus();
29998c2ecf20Sopenharmony_ci	else
30008c2ecf20Sopenharmony_ci		num_hwqs = num_online_cpus() / abs(nhwqs);
30018c2ecf20Sopenharmony_ci
30028c2ecf20Sopenharmony_ci	afu->desired_hwqs = min(num_hwqs, CXLFLASH_MAX_HWQS);
30038c2ecf20Sopenharmony_ci	WARN_ON_ONCE(afu->desired_hwqs == 0);
30048c2ecf20Sopenharmony_ci
30058c2ecf20Sopenharmony_ciretry:
30068c2ecf20Sopenharmony_ci	switch (cfg->state) {
30078c2ecf20Sopenharmony_ci	case STATE_NORMAL:
30088c2ecf20Sopenharmony_ci		cfg->state = STATE_RESET;
30098c2ecf20Sopenharmony_ci		drain_ioctls(cfg);
30108c2ecf20Sopenharmony_ci		cxlflash_mark_contexts_error(cfg);
30118c2ecf20Sopenharmony_ci		rc = afu_reset(cfg);
30128c2ecf20Sopenharmony_ci		if (rc)
30138c2ecf20Sopenharmony_ci			cfg->state = STATE_FAILTERM;
30148c2ecf20Sopenharmony_ci		else
30158c2ecf20Sopenharmony_ci			cfg->state = STATE_NORMAL;
30168c2ecf20Sopenharmony_ci		wake_up_all(&cfg->reset_waitq);
30178c2ecf20Sopenharmony_ci		break;
30188c2ecf20Sopenharmony_ci	case STATE_RESET:
30198c2ecf20Sopenharmony_ci		wait_event(cfg->reset_waitq, cfg->state != STATE_RESET);
30208c2ecf20Sopenharmony_ci		if (cfg->state == STATE_NORMAL)
30218c2ecf20Sopenharmony_ci			goto retry;
30228c2ecf20Sopenharmony_ci		fallthrough;
30238c2ecf20Sopenharmony_ci	default:
30248c2ecf20Sopenharmony_ci		/* Ideally should not happen */
30258c2ecf20Sopenharmony_ci		dev_err(dev, "%s: Device is not ready, state=%d\n",
30268c2ecf20Sopenharmony_ci			__func__, cfg->state);
30278c2ecf20Sopenharmony_ci		break;
30288c2ecf20Sopenharmony_ci	}
30298c2ecf20Sopenharmony_ci
30308c2ecf20Sopenharmony_ci	return count;
30318c2ecf20Sopenharmony_ci}
30328c2ecf20Sopenharmony_ci
30338c2ecf20Sopenharmony_cistatic const char *hwq_mode_name[MAX_HWQ_MODE] = { "rr", "tag", "cpu" };
30348c2ecf20Sopenharmony_ci
30358c2ecf20Sopenharmony_ci/**
30368c2ecf20Sopenharmony_ci * hwq_mode_show() - presents the HWQ steering mode for the host
30378c2ecf20Sopenharmony_ci * @dev:	Generic device associated with the host.
30388c2ecf20Sopenharmony_ci * @attr:	Device attribute representing the HWQ steering mode.
30398c2ecf20Sopenharmony_ci * @buf:	Buffer of length PAGE_SIZE to report back the HWQ steering mode
30408c2ecf20Sopenharmony_ci *		as a character string.
30418c2ecf20Sopenharmony_ci *
30428c2ecf20Sopenharmony_ci * Return: The size of the ASCII string returned in @buf.
30438c2ecf20Sopenharmony_ci */
30448c2ecf20Sopenharmony_cistatic ssize_t hwq_mode_show(struct device *dev,
30458c2ecf20Sopenharmony_ci			     struct device_attribute *attr, char *buf)
30468c2ecf20Sopenharmony_ci{
30478c2ecf20Sopenharmony_ci	struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
30488c2ecf20Sopenharmony_ci	struct afu *afu = cfg->afu;
30498c2ecf20Sopenharmony_ci
30508c2ecf20Sopenharmony_ci	return scnprintf(buf, PAGE_SIZE, "%s\n", hwq_mode_name[afu->hwq_mode]);
30518c2ecf20Sopenharmony_ci}
30528c2ecf20Sopenharmony_ci
30538c2ecf20Sopenharmony_ci/**
30548c2ecf20Sopenharmony_ci * hwq_mode_store() - sets the HWQ steering mode for the host
30558c2ecf20Sopenharmony_ci * @dev:	Generic device associated with the host.
30568c2ecf20Sopenharmony_ci * @attr:	Device attribute representing the HWQ steering mode.
30578c2ecf20Sopenharmony_ci * @buf:	Buffer of length PAGE_SIZE containing the HWQ steering mode
30588c2ecf20Sopenharmony_ci *		as a character string.
30598c2ecf20Sopenharmony_ci * @count:	Length of data resizing in @buf.
30608c2ecf20Sopenharmony_ci *
30618c2ecf20Sopenharmony_ci * rr = Round-Robin
30628c2ecf20Sopenharmony_ci * tag = Block MQ Tagging
30638c2ecf20Sopenharmony_ci * cpu = CPU Affinity
30648c2ecf20Sopenharmony_ci *
30658c2ecf20Sopenharmony_ci * Return: The size of the ASCII string returned in @buf.
30668c2ecf20Sopenharmony_ci */
30678c2ecf20Sopenharmony_cistatic ssize_t hwq_mode_store(struct device *dev,
30688c2ecf20Sopenharmony_ci			      struct device_attribute *attr,
30698c2ecf20Sopenharmony_ci			      const char *buf, size_t count)
30708c2ecf20Sopenharmony_ci{
30718c2ecf20Sopenharmony_ci	struct Scsi_Host *shost = class_to_shost(dev);
30728c2ecf20Sopenharmony_ci	struct cxlflash_cfg *cfg = shost_priv(shost);
30738c2ecf20Sopenharmony_ci	struct device *cfgdev = &cfg->dev->dev;
30748c2ecf20Sopenharmony_ci	struct afu *afu = cfg->afu;
30758c2ecf20Sopenharmony_ci	int i;
30768c2ecf20Sopenharmony_ci	u32 mode = MAX_HWQ_MODE;
30778c2ecf20Sopenharmony_ci
30788c2ecf20Sopenharmony_ci	for (i = 0; i < MAX_HWQ_MODE; i++) {
30798c2ecf20Sopenharmony_ci		if (!strncmp(hwq_mode_name[i], buf, strlen(hwq_mode_name[i]))) {
30808c2ecf20Sopenharmony_ci			mode = i;
30818c2ecf20Sopenharmony_ci			break;
30828c2ecf20Sopenharmony_ci		}
30838c2ecf20Sopenharmony_ci	}
30848c2ecf20Sopenharmony_ci
30858c2ecf20Sopenharmony_ci	if (mode >= MAX_HWQ_MODE) {
30868c2ecf20Sopenharmony_ci		dev_info(cfgdev, "Invalid HWQ steering mode.\n");
30878c2ecf20Sopenharmony_ci		return -EINVAL;
30888c2ecf20Sopenharmony_ci	}
30898c2ecf20Sopenharmony_ci
30908c2ecf20Sopenharmony_ci	afu->hwq_mode = mode;
30918c2ecf20Sopenharmony_ci
30928c2ecf20Sopenharmony_ci	return count;
30938c2ecf20Sopenharmony_ci}
30948c2ecf20Sopenharmony_ci
30958c2ecf20Sopenharmony_ci/**
30968c2ecf20Sopenharmony_ci * mode_show() - presents the current mode of the device
30978c2ecf20Sopenharmony_ci * @dev:	Generic device associated with the device.
30988c2ecf20Sopenharmony_ci * @attr:	Device attribute representing the device mode.
30998c2ecf20Sopenharmony_ci * @buf:	Buffer of length PAGE_SIZE to report back the dev mode in ASCII.
31008c2ecf20Sopenharmony_ci *
31018c2ecf20Sopenharmony_ci * Return: The size of the ASCII string returned in @buf.
31028c2ecf20Sopenharmony_ci */
31038c2ecf20Sopenharmony_cistatic ssize_t mode_show(struct device *dev,
31048c2ecf20Sopenharmony_ci			 struct device_attribute *attr, char *buf)
31058c2ecf20Sopenharmony_ci{
31068c2ecf20Sopenharmony_ci	struct scsi_device *sdev = to_scsi_device(dev);
31078c2ecf20Sopenharmony_ci
31088c2ecf20Sopenharmony_ci	return scnprintf(buf, PAGE_SIZE, "%s\n",
31098c2ecf20Sopenharmony_ci			 sdev->hostdata ? "superpipe" : "legacy");
31108c2ecf20Sopenharmony_ci}
31118c2ecf20Sopenharmony_ci
31128c2ecf20Sopenharmony_ci/*
31138c2ecf20Sopenharmony_ci * Host attributes
31148c2ecf20Sopenharmony_ci */
31158c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RO(port0);
31168c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RO(port1);
31178c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RO(port2);
31188c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RO(port3);
31198c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RW(lun_mode);
31208c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RO(ioctl_version);
31218c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RO(port0_lun_table);
31228c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RO(port1_lun_table);
31238c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RO(port2_lun_table);
31248c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RO(port3_lun_table);
31258c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RW(irqpoll_weight);
31268c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RW(num_hwqs);
31278c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RW(hwq_mode);
31288c2ecf20Sopenharmony_ci
31298c2ecf20Sopenharmony_cistatic struct device_attribute *cxlflash_host_attrs[] = {
31308c2ecf20Sopenharmony_ci	&dev_attr_port0,
31318c2ecf20Sopenharmony_ci	&dev_attr_port1,
31328c2ecf20Sopenharmony_ci	&dev_attr_port2,
31338c2ecf20Sopenharmony_ci	&dev_attr_port3,
31348c2ecf20Sopenharmony_ci	&dev_attr_lun_mode,
31358c2ecf20Sopenharmony_ci	&dev_attr_ioctl_version,
31368c2ecf20Sopenharmony_ci	&dev_attr_port0_lun_table,
31378c2ecf20Sopenharmony_ci	&dev_attr_port1_lun_table,
31388c2ecf20Sopenharmony_ci	&dev_attr_port2_lun_table,
31398c2ecf20Sopenharmony_ci	&dev_attr_port3_lun_table,
31408c2ecf20Sopenharmony_ci	&dev_attr_irqpoll_weight,
31418c2ecf20Sopenharmony_ci	&dev_attr_num_hwqs,
31428c2ecf20Sopenharmony_ci	&dev_attr_hwq_mode,
31438c2ecf20Sopenharmony_ci	NULL
31448c2ecf20Sopenharmony_ci};
31458c2ecf20Sopenharmony_ci
31468c2ecf20Sopenharmony_ci/*
31478c2ecf20Sopenharmony_ci * Device attributes
31488c2ecf20Sopenharmony_ci */
31498c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RO(mode);
31508c2ecf20Sopenharmony_ci
31518c2ecf20Sopenharmony_cistatic struct device_attribute *cxlflash_dev_attrs[] = {
31528c2ecf20Sopenharmony_ci	&dev_attr_mode,
31538c2ecf20Sopenharmony_ci	NULL
31548c2ecf20Sopenharmony_ci};
31558c2ecf20Sopenharmony_ci
31568c2ecf20Sopenharmony_ci/*
31578c2ecf20Sopenharmony_ci * Host template
31588c2ecf20Sopenharmony_ci */
31598c2ecf20Sopenharmony_cistatic struct scsi_host_template driver_template = {
31608c2ecf20Sopenharmony_ci	.module = THIS_MODULE,
31618c2ecf20Sopenharmony_ci	.name = CXLFLASH_ADAPTER_NAME,
31628c2ecf20Sopenharmony_ci	.info = cxlflash_driver_info,
31638c2ecf20Sopenharmony_ci	.ioctl = cxlflash_ioctl,
31648c2ecf20Sopenharmony_ci	.proc_name = CXLFLASH_NAME,
31658c2ecf20Sopenharmony_ci	.queuecommand = cxlflash_queuecommand,
31668c2ecf20Sopenharmony_ci	.eh_abort_handler = cxlflash_eh_abort_handler,
31678c2ecf20Sopenharmony_ci	.eh_device_reset_handler = cxlflash_eh_device_reset_handler,
31688c2ecf20Sopenharmony_ci	.eh_host_reset_handler = cxlflash_eh_host_reset_handler,
31698c2ecf20Sopenharmony_ci	.change_queue_depth = cxlflash_change_queue_depth,
31708c2ecf20Sopenharmony_ci	.cmd_per_lun = CXLFLASH_MAX_CMDS_PER_LUN,
31718c2ecf20Sopenharmony_ci	.can_queue = CXLFLASH_MAX_CMDS,
31728c2ecf20Sopenharmony_ci	.cmd_size = sizeof(struct afu_cmd) + __alignof__(struct afu_cmd) - 1,
31738c2ecf20Sopenharmony_ci	.this_id = -1,
31748c2ecf20Sopenharmony_ci	.sg_tablesize = 1,	/* No scatter gather support */
31758c2ecf20Sopenharmony_ci	.max_sectors = CXLFLASH_MAX_SECTORS,
31768c2ecf20Sopenharmony_ci	.shost_attrs = cxlflash_host_attrs,
31778c2ecf20Sopenharmony_ci	.sdev_attrs = cxlflash_dev_attrs,
31788c2ecf20Sopenharmony_ci};
31798c2ecf20Sopenharmony_ci
31808c2ecf20Sopenharmony_ci/*
31818c2ecf20Sopenharmony_ci * Device dependent values
31828c2ecf20Sopenharmony_ci */
31838c2ecf20Sopenharmony_cistatic struct dev_dependent_vals dev_corsa_vals = { CXLFLASH_MAX_SECTORS,
31848c2ecf20Sopenharmony_ci					CXLFLASH_WWPN_VPD_REQUIRED };
31858c2ecf20Sopenharmony_cistatic struct dev_dependent_vals dev_flash_gt_vals = { CXLFLASH_MAX_SECTORS,
31868c2ecf20Sopenharmony_ci					CXLFLASH_NOTIFY_SHUTDOWN };
31878c2ecf20Sopenharmony_cistatic struct dev_dependent_vals dev_briard_vals = { CXLFLASH_MAX_SECTORS,
31888c2ecf20Sopenharmony_ci					(CXLFLASH_NOTIFY_SHUTDOWN |
31898c2ecf20Sopenharmony_ci					CXLFLASH_OCXL_DEV) };
31908c2ecf20Sopenharmony_ci
31918c2ecf20Sopenharmony_ci/*
31928c2ecf20Sopenharmony_ci * PCI device binding table
31938c2ecf20Sopenharmony_ci */
31948c2ecf20Sopenharmony_cistatic struct pci_device_id cxlflash_pci_table[] = {
31958c2ecf20Sopenharmony_ci	{PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CORSA,
31968c2ecf20Sopenharmony_ci	 PCI_ANY_ID, PCI_ANY_ID, 0, 0, (kernel_ulong_t)&dev_corsa_vals},
31978c2ecf20Sopenharmony_ci	{PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_FLASH_GT,
31988c2ecf20Sopenharmony_ci	 PCI_ANY_ID, PCI_ANY_ID, 0, 0, (kernel_ulong_t)&dev_flash_gt_vals},
31998c2ecf20Sopenharmony_ci	{PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_BRIARD,
32008c2ecf20Sopenharmony_ci	 PCI_ANY_ID, PCI_ANY_ID, 0, 0, (kernel_ulong_t)&dev_briard_vals},
32018c2ecf20Sopenharmony_ci	{}
32028c2ecf20Sopenharmony_ci};
32038c2ecf20Sopenharmony_ci
32048c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(pci, cxlflash_pci_table);
32058c2ecf20Sopenharmony_ci
32068c2ecf20Sopenharmony_ci/**
32078c2ecf20Sopenharmony_ci * cxlflash_worker_thread() - work thread handler for the AFU
32088c2ecf20Sopenharmony_ci * @work:	Work structure contained within cxlflash associated with host.
32098c2ecf20Sopenharmony_ci *
32108c2ecf20Sopenharmony_ci * Handles the following events:
32118c2ecf20Sopenharmony_ci * - Link reset which cannot be performed on interrupt context due to
32128c2ecf20Sopenharmony_ci * blocking up to a few seconds
32138c2ecf20Sopenharmony_ci * - Rescan the host
32148c2ecf20Sopenharmony_ci */
32158c2ecf20Sopenharmony_cistatic void cxlflash_worker_thread(struct work_struct *work)
32168c2ecf20Sopenharmony_ci{
32178c2ecf20Sopenharmony_ci	struct cxlflash_cfg *cfg = container_of(work, struct cxlflash_cfg,
32188c2ecf20Sopenharmony_ci						work_q);
32198c2ecf20Sopenharmony_ci	struct afu *afu = cfg->afu;
32208c2ecf20Sopenharmony_ci	struct device *dev = &cfg->dev->dev;
32218c2ecf20Sopenharmony_ci	__be64 __iomem *fc_port_regs;
32228c2ecf20Sopenharmony_ci	int port;
32238c2ecf20Sopenharmony_ci	ulong lock_flags;
32248c2ecf20Sopenharmony_ci
32258c2ecf20Sopenharmony_ci	/* Avoid MMIO if the device has failed */
32268c2ecf20Sopenharmony_ci
32278c2ecf20Sopenharmony_ci	if (cfg->state != STATE_NORMAL)
32288c2ecf20Sopenharmony_ci		return;
32298c2ecf20Sopenharmony_ci
32308c2ecf20Sopenharmony_ci	spin_lock_irqsave(cfg->host->host_lock, lock_flags);
32318c2ecf20Sopenharmony_ci
32328c2ecf20Sopenharmony_ci	if (cfg->lr_state == LINK_RESET_REQUIRED) {
32338c2ecf20Sopenharmony_ci		port = cfg->lr_port;
32348c2ecf20Sopenharmony_ci		if (port < 0)
32358c2ecf20Sopenharmony_ci			dev_err(dev, "%s: invalid port index %d\n",
32368c2ecf20Sopenharmony_ci				__func__, port);
32378c2ecf20Sopenharmony_ci		else {
32388c2ecf20Sopenharmony_ci			spin_unlock_irqrestore(cfg->host->host_lock,
32398c2ecf20Sopenharmony_ci					       lock_flags);
32408c2ecf20Sopenharmony_ci
32418c2ecf20Sopenharmony_ci			/* The reset can block... */
32428c2ecf20Sopenharmony_ci			fc_port_regs = get_fc_port_regs(cfg, port);
32438c2ecf20Sopenharmony_ci			afu_link_reset(afu, port, fc_port_regs);
32448c2ecf20Sopenharmony_ci			spin_lock_irqsave(cfg->host->host_lock, lock_flags);
32458c2ecf20Sopenharmony_ci		}
32468c2ecf20Sopenharmony_ci
32478c2ecf20Sopenharmony_ci		cfg->lr_state = LINK_RESET_COMPLETE;
32488c2ecf20Sopenharmony_ci	}
32498c2ecf20Sopenharmony_ci
32508c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(cfg->host->host_lock, lock_flags);
32518c2ecf20Sopenharmony_ci
32528c2ecf20Sopenharmony_ci	if (atomic_dec_if_positive(&cfg->scan_host_needed) >= 0)
32538c2ecf20Sopenharmony_ci		scsi_scan_host(cfg->host);
32548c2ecf20Sopenharmony_ci}
32558c2ecf20Sopenharmony_ci
32568c2ecf20Sopenharmony_ci/**
32578c2ecf20Sopenharmony_ci * cxlflash_chr_open() - character device open handler
32588c2ecf20Sopenharmony_ci * @inode:	Device inode associated with this character device.
32598c2ecf20Sopenharmony_ci * @file:	File pointer for this device.
32608c2ecf20Sopenharmony_ci *
32618c2ecf20Sopenharmony_ci * Only users with admin privileges are allowed to open the character device.
32628c2ecf20Sopenharmony_ci *
32638c2ecf20Sopenharmony_ci * Return: 0 on success, -errno on failure
32648c2ecf20Sopenharmony_ci */
32658c2ecf20Sopenharmony_cistatic int cxlflash_chr_open(struct inode *inode, struct file *file)
32668c2ecf20Sopenharmony_ci{
32678c2ecf20Sopenharmony_ci	struct cxlflash_cfg *cfg;
32688c2ecf20Sopenharmony_ci
32698c2ecf20Sopenharmony_ci	if (!capable(CAP_SYS_ADMIN))
32708c2ecf20Sopenharmony_ci		return -EACCES;
32718c2ecf20Sopenharmony_ci
32728c2ecf20Sopenharmony_ci	cfg = container_of(inode->i_cdev, struct cxlflash_cfg, cdev);
32738c2ecf20Sopenharmony_ci	file->private_data = cfg;
32748c2ecf20Sopenharmony_ci
32758c2ecf20Sopenharmony_ci	return 0;
32768c2ecf20Sopenharmony_ci}
32778c2ecf20Sopenharmony_ci
32788c2ecf20Sopenharmony_ci/**
32798c2ecf20Sopenharmony_ci * decode_hioctl() - translates encoded host ioctl to easily identifiable string
32808c2ecf20Sopenharmony_ci * @cmd:        The host ioctl command to decode.
32818c2ecf20Sopenharmony_ci *
32828c2ecf20Sopenharmony_ci * Return: A string identifying the decoded host ioctl.
32838c2ecf20Sopenharmony_ci */
32848c2ecf20Sopenharmony_cistatic char *decode_hioctl(unsigned int cmd)
32858c2ecf20Sopenharmony_ci{
32868c2ecf20Sopenharmony_ci	switch (cmd) {
32878c2ecf20Sopenharmony_ci	case HT_CXLFLASH_LUN_PROVISION:
32888c2ecf20Sopenharmony_ci		return __stringify_1(HT_CXLFLASH_LUN_PROVISION);
32898c2ecf20Sopenharmony_ci	}
32908c2ecf20Sopenharmony_ci
32918c2ecf20Sopenharmony_ci	return "UNKNOWN";
32928c2ecf20Sopenharmony_ci}
32938c2ecf20Sopenharmony_ci
32948c2ecf20Sopenharmony_ci/**
32958c2ecf20Sopenharmony_ci * cxlflash_lun_provision() - host LUN provisioning handler
32968c2ecf20Sopenharmony_ci * @cfg:	Internal structure associated with the host.
32978c2ecf20Sopenharmony_ci * @arg:	Kernel copy of userspace ioctl data structure.
32988c2ecf20Sopenharmony_ci *
32998c2ecf20Sopenharmony_ci * Return: 0 on success, -errno on failure
33008c2ecf20Sopenharmony_ci */
33018c2ecf20Sopenharmony_cistatic int cxlflash_lun_provision(struct cxlflash_cfg *cfg,
33028c2ecf20Sopenharmony_ci				  struct ht_cxlflash_lun_provision *lunprov)
33038c2ecf20Sopenharmony_ci{
33048c2ecf20Sopenharmony_ci	struct afu *afu = cfg->afu;
33058c2ecf20Sopenharmony_ci	struct device *dev = &cfg->dev->dev;
33068c2ecf20Sopenharmony_ci	struct sisl_ioarcb rcb;
33078c2ecf20Sopenharmony_ci	struct sisl_ioasa asa;
33088c2ecf20Sopenharmony_ci	__be64 __iomem *fc_port_regs;
33098c2ecf20Sopenharmony_ci	u16 port = lunprov->port;
33108c2ecf20Sopenharmony_ci	u16 scmd = lunprov->hdr.subcmd;
33118c2ecf20Sopenharmony_ci	u16 type;
33128c2ecf20Sopenharmony_ci	u64 reg;
33138c2ecf20Sopenharmony_ci	u64 size;
33148c2ecf20Sopenharmony_ci	u64 lun_id;
33158c2ecf20Sopenharmony_ci	int rc = 0;
33168c2ecf20Sopenharmony_ci
33178c2ecf20Sopenharmony_ci	if (!afu_is_lun_provision(afu)) {
33188c2ecf20Sopenharmony_ci		rc = -ENOTSUPP;
33198c2ecf20Sopenharmony_ci		goto out;
33208c2ecf20Sopenharmony_ci	}
33218c2ecf20Sopenharmony_ci
33228c2ecf20Sopenharmony_ci	if (port >= cfg->num_fc_ports) {
33238c2ecf20Sopenharmony_ci		rc = -EINVAL;
33248c2ecf20Sopenharmony_ci		goto out;
33258c2ecf20Sopenharmony_ci	}
33268c2ecf20Sopenharmony_ci
33278c2ecf20Sopenharmony_ci	switch (scmd) {
33288c2ecf20Sopenharmony_ci	case HT_CXLFLASH_LUN_PROVISION_SUBCMD_CREATE_LUN:
33298c2ecf20Sopenharmony_ci		type = SISL_AFU_LUN_PROVISION_CREATE;
33308c2ecf20Sopenharmony_ci		size = lunprov->size;
33318c2ecf20Sopenharmony_ci		lun_id = 0;
33328c2ecf20Sopenharmony_ci		break;
33338c2ecf20Sopenharmony_ci	case HT_CXLFLASH_LUN_PROVISION_SUBCMD_DELETE_LUN:
33348c2ecf20Sopenharmony_ci		type = SISL_AFU_LUN_PROVISION_DELETE;
33358c2ecf20Sopenharmony_ci		size = 0;
33368c2ecf20Sopenharmony_ci		lun_id = lunprov->lun_id;
33378c2ecf20Sopenharmony_ci		break;
33388c2ecf20Sopenharmony_ci	case HT_CXLFLASH_LUN_PROVISION_SUBCMD_QUERY_PORT:
33398c2ecf20Sopenharmony_ci		fc_port_regs = get_fc_port_regs(cfg, port);
33408c2ecf20Sopenharmony_ci
33418c2ecf20Sopenharmony_ci		reg = readq_be(&fc_port_regs[FC_MAX_NUM_LUNS / 8]);
33428c2ecf20Sopenharmony_ci		lunprov->max_num_luns = reg;
33438c2ecf20Sopenharmony_ci		reg = readq_be(&fc_port_regs[FC_CUR_NUM_LUNS / 8]);
33448c2ecf20Sopenharmony_ci		lunprov->cur_num_luns = reg;
33458c2ecf20Sopenharmony_ci		reg = readq_be(&fc_port_regs[FC_MAX_CAP_PORT / 8]);
33468c2ecf20Sopenharmony_ci		lunprov->max_cap_port = reg;
33478c2ecf20Sopenharmony_ci		reg = readq_be(&fc_port_regs[FC_CUR_CAP_PORT / 8]);
33488c2ecf20Sopenharmony_ci		lunprov->cur_cap_port = reg;
33498c2ecf20Sopenharmony_ci
33508c2ecf20Sopenharmony_ci		goto out;
33518c2ecf20Sopenharmony_ci	default:
33528c2ecf20Sopenharmony_ci		rc = -EINVAL;
33538c2ecf20Sopenharmony_ci		goto out;
33548c2ecf20Sopenharmony_ci	}
33558c2ecf20Sopenharmony_ci
33568c2ecf20Sopenharmony_ci	memset(&rcb, 0, sizeof(rcb));
33578c2ecf20Sopenharmony_ci	memset(&asa, 0, sizeof(asa));
33588c2ecf20Sopenharmony_ci	rcb.req_flags = SISL_REQ_FLAGS_AFU_CMD;
33598c2ecf20Sopenharmony_ci	rcb.lun_id = lun_id;
33608c2ecf20Sopenharmony_ci	rcb.msi = SISL_MSI_RRQ_UPDATED;
33618c2ecf20Sopenharmony_ci	rcb.timeout = MC_LUN_PROV_TIMEOUT;
33628c2ecf20Sopenharmony_ci	rcb.ioasa = &asa;
33638c2ecf20Sopenharmony_ci
33648c2ecf20Sopenharmony_ci	rcb.cdb[0] = SISL_AFU_CMD_LUN_PROVISION;
33658c2ecf20Sopenharmony_ci	rcb.cdb[1] = type;
33668c2ecf20Sopenharmony_ci	rcb.cdb[2] = port;
33678c2ecf20Sopenharmony_ci	put_unaligned_be64(size, &rcb.cdb[8]);
33688c2ecf20Sopenharmony_ci
33698c2ecf20Sopenharmony_ci	rc = send_afu_cmd(afu, &rcb);
33708c2ecf20Sopenharmony_ci	if (rc) {
33718c2ecf20Sopenharmony_ci		dev_err(dev, "%s: send_afu_cmd failed rc=%d asc=%08x afux=%x\n",
33728c2ecf20Sopenharmony_ci			__func__, rc, asa.ioasc, asa.afu_extra);
33738c2ecf20Sopenharmony_ci		goto out;
33748c2ecf20Sopenharmony_ci	}
33758c2ecf20Sopenharmony_ci
33768c2ecf20Sopenharmony_ci	if (scmd == HT_CXLFLASH_LUN_PROVISION_SUBCMD_CREATE_LUN) {
33778c2ecf20Sopenharmony_ci		lunprov->lun_id = (u64)asa.lunid_hi << 32 | asa.lunid_lo;
33788c2ecf20Sopenharmony_ci		memcpy(lunprov->wwid, asa.wwid, sizeof(lunprov->wwid));
33798c2ecf20Sopenharmony_ci	}
33808c2ecf20Sopenharmony_ciout:
33818c2ecf20Sopenharmony_ci	dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
33828c2ecf20Sopenharmony_ci	return rc;
33838c2ecf20Sopenharmony_ci}
33848c2ecf20Sopenharmony_ci
33858c2ecf20Sopenharmony_ci/**
33868c2ecf20Sopenharmony_ci * cxlflash_afu_debug() - host AFU debug handler
33878c2ecf20Sopenharmony_ci * @cfg:	Internal structure associated with the host.
33888c2ecf20Sopenharmony_ci * @arg:	Kernel copy of userspace ioctl data structure.
33898c2ecf20Sopenharmony_ci *
33908c2ecf20Sopenharmony_ci * For debug requests requiring a data buffer, always provide an aligned
33918c2ecf20Sopenharmony_ci * (cache line) buffer to the AFU to appease any alignment requirements.
33928c2ecf20Sopenharmony_ci *
33938c2ecf20Sopenharmony_ci * Return: 0 on success, -errno on failure
33948c2ecf20Sopenharmony_ci */
33958c2ecf20Sopenharmony_cistatic int cxlflash_afu_debug(struct cxlflash_cfg *cfg,
33968c2ecf20Sopenharmony_ci			      struct ht_cxlflash_afu_debug *afu_dbg)
33978c2ecf20Sopenharmony_ci{
33988c2ecf20Sopenharmony_ci	struct afu *afu = cfg->afu;
33998c2ecf20Sopenharmony_ci	struct device *dev = &cfg->dev->dev;
34008c2ecf20Sopenharmony_ci	struct sisl_ioarcb rcb;
34018c2ecf20Sopenharmony_ci	struct sisl_ioasa asa;
34028c2ecf20Sopenharmony_ci	char *buf = NULL;
34038c2ecf20Sopenharmony_ci	char *kbuf = NULL;
34048c2ecf20Sopenharmony_ci	void __user *ubuf = (__force void __user *)afu_dbg->data_ea;
34058c2ecf20Sopenharmony_ci	u16 req_flags = SISL_REQ_FLAGS_AFU_CMD;
34068c2ecf20Sopenharmony_ci	u32 ulen = afu_dbg->data_len;
34078c2ecf20Sopenharmony_ci	bool is_write = afu_dbg->hdr.flags & HT_CXLFLASH_HOST_WRITE;
34088c2ecf20Sopenharmony_ci	int rc = 0;
34098c2ecf20Sopenharmony_ci
34108c2ecf20Sopenharmony_ci	if (!afu_is_afu_debug(afu)) {
34118c2ecf20Sopenharmony_ci		rc = -ENOTSUPP;
34128c2ecf20Sopenharmony_ci		goto out;
34138c2ecf20Sopenharmony_ci	}
34148c2ecf20Sopenharmony_ci
34158c2ecf20Sopenharmony_ci	if (ulen) {
34168c2ecf20Sopenharmony_ci		req_flags |= SISL_REQ_FLAGS_SUP_UNDERRUN;
34178c2ecf20Sopenharmony_ci
34188c2ecf20Sopenharmony_ci		if (ulen > HT_CXLFLASH_AFU_DEBUG_MAX_DATA_LEN) {
34198c2ecf20Sopenharmony_ci			rc = -EINVAL;
34208c2ecf20Sopenharmony_ci			goto out;
34218c2ecf20Sopenharmony_ci		}
34228c2ecf20Sopenharmony_ci
34238c2ecf20Sopenharmony_ci		buf = kmalloc(ulen + cache_line_size() - 1, GFP_KERNEL);
34248c2ecf20Sopenharmony_ci		if (unlikely(!buf)) {
34258c2ecf20Sopenharmony_ci			rc = -ENOMEM;
34268c2ecf20Sopenharmony_ci			goto out;
34278c2ecf20Sopenharmony_ci		}
34288c2ecf20Sopenharmony_ci
34298c2ecf20Sopenharmony_ci		kbuf = PTR_ALIGN(buf, cache_line_size());
34308c2ecf20Sopenharmony_ci
34318c2ecf20Sopenharmony_ci		if (is_write) {
34328c2ecf20Sopenharmony_ci			req_flags |= SISL_REQ_FLAGS_HOST_WRITE;
34338c2ecf20Sopenharmony_ci
34348c2ecf20Sopenharmony_ci			if (copy_from_user(kbuf, ubuf, ulen)) {
34358c2ecf20Sopenharmony_ci				rc = -EFAULT;
34368c2ecf20Sopenharmony_ci				goto out;
34378c2ecf20Sopenharmony_ci			}
34388c2ecf20Sopenharmony_ci		}
34398c2ecf20Sopenharmony_ci	}
34408c2ecf20Sopenharmony_ci
34418c2ecf20Sopenharmony_ci	memset(&rcb, 0, sizeof(rcb));
34428c2ecf20Sopenharmony_ci	memset(&asa, 0, sizeof(asa));
34438c2ecf20Sopenharmony_ci
34448c2ecf20Sopenharmony_ci	rcb.req_flags = req_flags;
34458c2ecf20Sopenharmony_ci	rcb.msi = SISL_MSI_RRQ_UPDATED;
34468c2ecf20Sopenharmony_ci	rcb.timeout = MC_AFU_DEBUG_TIMEOUT;
34478c2ecf20Sopenharmony_ci	rcb.ioasa = &asa;
34488c2ecf20Sopenharmony_ci
34498c2ecf20Sopenharmony_ci	if (ulen) {
34508c2ecf20Sopenharmony_ci		rcb.data_len = ulen;
34518c2ecf20Sopenharmony_ci		rcb.data_ea = (uintptr_t)kbuf;
34528c2ecf20Sopenharmony_ci	}
34538c2ecf20Sopenharmony_ci
34548c2ecf20Sopenharmony_ci	rcb.cdb[0] = SISL_AFU_CMD_DEBUG;
34558c2ecf20Sopenharmony_ci	memcpy(&rcb.cdb[4], afu_dbg->afu_subcmd,
34568c2ecf20Sopenharmony_ci	       HT_CXLFLASH_AFU_DEBUG_SUBCMD_LEN);
34578c2ecf20Sopenharmony_ci
34588c2ecf20Sopenharmony_ci	rc = send_afu_cmd(afu, &rcb);
34598c2ecf20Sopenharmony_ci	if (rc) {
34608c2ecf20Sopenharmony_ci		dev_err(dev, "%s: send_afu_cmd failed rc=%d asc=%08x afux=%x\n",
34618c2ecf20Sopenharmony_ci			__func__, rc, asa.ioasc, asa.afu_extra);
34628c2ecf20Sopenharmony_ci		goto out;
34638c2ecf20Sopenharmony_ci	}
34648c2ecf20Sopenharmony_ci
34658c2ecf20Sopenharmony_ci	if (ulen && !is_write) {
34668c2ecf20Sopenharmony_ci		if (copy_to_user(ubuf, kbuf, ulen))
34678c2ecf20Sopenharmony_ci			rc = -EFAULT;
34688c2ecf20Sopenharmony_ci	}
34698c2ecf20Sopenharmony_ciout:
34708c2ecf20Sopenharmony_ci	kfree(buf);
34718c2ecf20Sopenharmony_ci	dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
34728c2ecf20Sopenharmony_ci	return rc;
34738c2ecf20Sopenharmony_ci}
34748c2ecf20Sopenharmony_ci
34758c2ecf20Sopenharmony_ci/**
34768c2ecf20Sopenharmony_ci * cxlflash_chr_ioctl() - character device IOCTL handler
34778c2ecf20Sopenharmony_ci * @file:	File pointer for this device.
34788c2ecf20Sopenharmony_ci * @cmd:	IOCTL command.
34798c2ecf20Sopenharmony_ci * @arg:	Userspace ioctl data structure.
34808c2ecf20Sopenharmony_ci *
34818c2ecf20Sopenharmony_ci * A read/write semaphore is used to implement a 'drain' of currently
34828c2ecf20Sopenharmony_ci * running ioctls. The read semaphore is taken at the beginning of each
34838c2ecf20Sopenharmony_ci * ioctl thread and released upon concluding execution. Additionally the
34848c2ecf20Sopenharmony_ci * semaphore should be released and then reacquired in any ioctl execution
34858c2ecf20Sopenharmony_ci * path which will wait for an event to occur that is outside the scope of
34868c2ecf20Sopenharmony_ci * the ioctl (i.e. an adapter reset). To drain the ioctls currently running,
34878c2ecf20Sopenharmony_ci * a thread simply needs to acquire the write semaphore.
34888c2ecf20Sopenharmony_ci *
34898c2ecf20Sopenharmony_ci * Return: 0 on success, -errno on failure
34908c2ecf20Sopenharmony_ci */
34918c2ecf20Sopenharmony_cistatic long cxlflash_chr_ioctl(struct file *file, unsigned int cmd,
34928c2ecf20Sopenharmony_ci			       unsigned long arg)
34938c2ecf20Sopenharmony_ci{
34948c2ecf20Sopenharmony_ci	typedef int (*hioctl) (struct cxlflash_cfg *, void *);
34958c2ecf20Sopenharmony_ci
34968c2ecf20Sopenharmony_ci	struct cxlflash_cfg *cfg = file->private_data;
34978c2ecf20Sopenharmony_ci	struct device *dev = &cfg->dev->dev;
34988c2ecf20Sopenharmony_ci	char buf[sizeof(union cxlflash_ht_ioctls)];
34998c2ecf20Sopenharmony_ci	void __user *uarg = (void __user *)arg;
35008c2ecf20Sopenharmony_ci	struct ht_cxlflash_hdr *hdr;
35018c2ecf20Sopenharmony_ci	size_t size = 0;
35028c2ecf20Sopenharmony_ci	bool known_ioctl = false;
35038c2ecf20Sopenharmony_ci	int idx = 0;
35048c2ecf20Sopenharmony_ci	int rc = 0;
35058c2ecf20Sopenharmony_ci	hioctl do_ioctl = NULL;
35068c2ecf20Sopenharmony_ci
35078c2ecf20Sopenharmony_ci	static const struct {
35088c2ecf20Sopenharmony_ci		size_t size;
35098c2ecf20Sopenharmony_ci		hioctl ioctl;
35108c2ecf20Sopenharmony_ci	} ioctl_tbl[] = {	/* NOTE: order matters here */
35118c2ecf20Sopenharmony_ci	{ sizeof(struct ht_cxlflash_lun_provision),
35128c2ecf20Sopenharmony_ci		(hioctl)cxlflash_lun_provision },
35138c2ecf20Sopenharmony_ci	{ sizeof(struct ht_cxlflash_afu_debug),
35148c2ecf20Sopenharmony_ci		(hioctl)cxlflash_afu_debug },
35158c2ecf20Sopenharmony_ci	};
35168c2ecf20Sopenharmony_ci
35178c2ecf20Sopenharmony_ci	/* Hold read semaphore so we can drain if needed */
35188c2ecf20Sopenharmony_ci	down_read(&cfg->ioctl_rwsem);
35198c2ecf20Sopenharmony_ci
35208c2ecf20Sopenharmony_ci	dev_dbg(dev, "%s: cmd=%u idx=%d tbl_size=%lu\n",
35218c2ecf20Sopenharmony_ci		__func__, cmd, idx, sizeof(ioctl_tbl));
35228c2ecf20Sopenharmony_ci
35238c2ecf20Sopenharmony_ci	switch (cmd) {
35248c2ecf20Sopenharmony_ci	case HT_CXLFLASH_LUN_PROVISION:
35258c2ecf20Sopenharmony_ci	case HT_CXLFLASH_AFU_DEBUG:
35268c2ecf20Sopenharmony_ci		known_ioctl = true;
35278c2ecf20Sopenharmony_ci		idx = _IOC_NR(HT_CXLFLASH_LUN_PROVISION) - _IOC_NR(cmd);
35288c2ecf20Sopenharmony_ci		size = ioctl_tbl[idx].size;
35298c2ecf20Sopenharmony_ci		do_ioctl = ioctl_tbl[idx].ioctl;
35308c2ecf20Sopenharmony_ci
35318c2ecf20Sopenharmony_ci		if (likely(do_ioctl))
35328c2ecf20Sopenharmony_ci			break;
35338c2ecf20Sopenharmony_ci
35348c2ecf20Sopenharmony_ci		fallthrough;
35358c2ecf20Sopenharmony_ci	default:
35368c2ecf20Sopenharmony_ci		rc = -EINVAL;
35378c2ecf20Sopenharmony_ci		goto out;
35388c2ecf20Sopenharmony_ci	}
35398c2ecf20Sopenharmony_ci
35408c2ecf20Sopenharmony_ci	if (unlikely(copy_from_user(&buf, uarg, size))) {
35418c2ecf20Sopenharmony_ci		dev_err(dev, "%s: copy_from_user() fail "
35428c2ecf20Sopenharmony_ci			"size=%lu cmd=%d (%s) uarg=%p\n",
35438c2ecf20Sopenharmony_ci			__func__, size, cmd, decode_hioctl(cmd), uarg);
35448c2ecf20Sopenharmony_ci		rc = -EFAULT;
35458c2ecf20Sopenharmony_ci		goto out;
35468c2ecf20Sopenharmony_ci	}
35478c2ecf20Sopenharmony_ci
35488c2ecf20Sopenharmony_ci	hdr = (struct ht_cxlflash_hdr *)&buf;
35498c2ecf20Sopenharmony_ci	if (hdr->version != HT_CXLFLASH_VERSION_0) {
35508c2ecf20Sopenharmony_ci		dev_dbg(dev, "%s: Version %u not supported for %s\n",
35518c2ecf20Sopenharmony_ci			__func__, hdr->version, decode_hioctl(cmd));
35528c2ecf20Sopenharmony_ci		rc = -EINVAL;
35538c2ecf20Sopenharmony_ci		goto out;
35548c2ecf20Sopenharmony_ci	}
35558c2ecf20Sopenharmony_ci
35568c2ecf20Sopenharmony_ci	if (hdr->rsvd[0] || hdr->rsvd[1] || hdr->return_flags) {
35578c2ecf20Sopenharmony_ci		dev_dbg(dev, "%s: Reserved/rflags populated\n", __func__);
35588c2ecf20Sopenharmony_ci		rc = -EINVAL;
35598c2ecf20Sopenharmony_ci		goto out;
35608c2ecf20Sopenharmony_ci	}
35618c2ecf20Sopenharmony_ci
35628c2ecf20Sopenharmony_ci	rc = do_ioctl(cfg, (void *)&buf);
35638c2ecf20Sopenharmony_ci	if (likely(!rc))
35648c2ecf20Sopenharmony_ci		if (unlikely(copy_to_user(uarg, &buf, size))) {
35658c2ecf20Sopenharmony_ci			dev_err(dev, "%s: copy_to_user() fail "
35668c2ecf20Sopenharmony_ci				"size=%lu cmd=%d (%s) uarg=%p\n",
35678c2ecf20Sopenharmony_ci				__func__, size, cmd, decode_hioctl(cmd), uarg);
35688c2ecf20Sopenharmony_ci			rc = -EFAULT;
35698c2ecf20Sopenharmony_ci		}
35708c2ecf20Sopenharmony_ci
35718c2ecf20Sopenharmony_ci	/* fall through to exit */
35728c2ecf20Sopenharmony_ci
35738c2ecf20Sopenharmony_ciout:
35748c2ecf20Sopenharmony_ci	up_read(&cfg->ioctl_rwsem);
35758c2ecf20Sopenharmony_ci	if (unlikely(rc && known_ioctl))
35768c2ecf20Sopenharmony_ci		dev_err(dev, "%s: ioctl %s (%08X) returned rc=%d\n",
35778c2ecf20Sopenharmony_ci			__func__, decode_hioctl(cmd), cmd, rc);
35788c2ecf20Sopenharmony_ci	else
35798c2ecf20Sopenharmony_ci		dev_dbg(dev, "%s: ioctl %s (%08X) returned rc=%d\n",
35808c2ecf20Sopenharmony_ci			__func__, decode_hioctl(cmd), cmd, rc);
35818c2ecf20Sopenharmony_ci	return rc;
35828c2ecf20Sopenharmony_ci}
35838c2ecf20Sopenharmony_ci
35848c2ecf20Sopenharmony_ci/*
35858c2ecf20Sopenharmony_ci * Character device file operations
35868c2ecf20Sopenharmony_ci */
35878c2ecf20Sopenharmony_cistatic const struct file_operations cxlflash_chr_fops = {
35888c2ecf20Sopenharmony_ci	.owner          = THIS_MODULE,
35898c2ecf20Sopenharmony_ci	.open           = cxlflash_chr_open,
35908c2ecf20Sopenharmony_ci	.unlocked_ioctl	= cxlflash_chr_ioctl,
35918c2ecf20Sopenharmony_ci	.compat_ioctl	= compat_ptr_ioctl,
35928c2ecf20Sopenharmony_ci};
35938c2ecf20Sopenharmony_ci
35948c2ecf20Sopenharmony_ci/**
35958c2ecf20Sopenharmony_ci * init_chrdev() - initialize the character device for the host
35968c2ecf20Sopenharmony_ci * @cfg:	Internal structure associated with the host.
35978c2ecf20Sopenharmony_ci *
35988c2ecf20Sopenharmony_ci * Return: 0 on success, -errno on failure
35998c2ecf20Sopenharmony_ci */
36008c2ecf20Sopenharmony_cistatic int init_chrdev(struct cxlflash_cfg *cfg)
36018c2ecf20Sopenharmony_ci{
36028c2ecf20Sopenharmony_ci	struct device *dev = &cfg->dev->dev;
36038c2ecf20Sopenharmony_ci	struct device *char_dev;
36048c2ecf20Sopenharmony_ci	dev_t devno;
36058c2ecf20Sopenharmony_ci	int minor;
36068c2ecf20Sopenharmony_ci	int rc = 0;
36078c2ecf20Sopenharmony_ci
36088c2ecf20Sopenharmony_ci	minor = cxlflash_get_minor();
36098c2ecf20Sopenharmony_ci	if (unlikely(minor < 0)) {
36108c2ecf20Sopenharmony_ci		dev_err(dev, "%s: Exhausted allowed adapters\n", __func__);
36118c2ecf20Sopenharmony_ci		rc = -ENOSPC;
36128c2ecf20Sopenharmony_ci		goto out;
36138c2ecf20Sopenharmony_ci	}
36148c2ecf20Sopenharmony_ci
36158c2ecf20Sopenharmony_ci	devno = MKDEV(cxlflash_major, minor);
36168c2ecf20Sopenharmony_ci	cdev_init(&cfg->cdev, &cxlflash_chr_fops);
36178c2ecf20Sopenharmony_ci
36188c2ecf20Sopenharmony_ci	rc = cdev_add(&cfg->cdev, devno, 1);
36198c2ecf20Sopenharmony_ci	if (rc) {
36208c2ecf20Sopenharmony_ci		dev_err(dev, "%s: cdev_add failed rc=%d\n", __func__, rc);
36218c2ecf20Sopenharmony_ci		goto err1;
36228c2ecf20Sopenharmony_ci	}
36238c2ecf20Sopenharmony_ci
36248c2ecf20Sopenharmony_ci	char_dev = device_create(cxlflash_class, NULL, devno,
36258c2ecf20Sopenharmony_ci				 NULL, "cxlflash%d", minor);
36268c2ecf20Sopenharmony_ci	if (IS_ERR(char_dev)) {
36278c2ecf20Sopenharmony_ci		rc = PTR_ERR(char_dev);
36288c2ecf20Sopenharmony_ci		dev_err(dev, "%s: device_create failed rc=%d\n",
36298c2ecf20Sopenharmony_ci			__func__, rc);
36308c2ecf20Sopenharmony_ci		goto err2;
36318c2ecf20Sopenharmony_ci	}
36328c2ecf20Sopenharmony_ci
36338c2ecf20Sopenharmony_ci	cfg->chardev = char_dev;
36348c2ecf20Sopenharmony_ciout:
36358c2ecf20Sopenharmony_ci	dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
36368c2ecf20Sopenharmony_ci	return rc;
36378c2ecf20Sopenharmony_cierr2:
36388c2ecf20Sopenharmony_ci	cdev_del(&cfg->cdev);
36398c2ecf20Sopenharmony_cierr1:
36408c2ecf20Sopenharmony_ci	cxlflash_put_minor(minor);
36418c2ecf20Sopenharmony_ci	goto out;
36428c2ecf20Sopenharmony_ci}
36438c2ecf20Sopenharmony_ci
36448c2ecf20Sopenharmony_ci/**
36458c2ecf20Sopenharmony_ci * cxlflash_probe() - PCI entry point to add host
36468c2ecf20Sopenharmony_ci * @pdev:	PCI device associated with the host.
36478c2ecf20Sopenharmony_ci * @dev_id:	PCI device id associated with device.
36488c2ecf20Sopenharmony_ci *
36498c2ecf20Sopenharmony_ci * The device will initially start out in a 'probing' state and
36508c2ecf20Sopenharmony_ci * transition to the 'normal' state at the end of a successful
36518c2ecf20Sopenharmony_ci * probe. Should an EEH event occur during probe, the notification
36528c2ecf20Sopenharmony_ci * thread (error_detected()) will wait until the probe handler
36538c2ecf20Sopenharmony_ci * is nearly complete. At that time, the device will be moved to
36548c2ecf20Sopenharmony_ci * a 'probed' state and the EEH thread woken up to drive the slot
36558c2ecf20Sopenharmony_ci * reset and recovery (device moves to 'normal' state). Meanwhile,
36568c2ecf20Sopenharmony_ci * the probe will be allowed to exit successfully.
36578c2ecf20Sopenharmony_ci *
36588c2ecf20Sopenharmony_ci * Return: 0 on success, -errno on failure
36598c2ecf20Sopenharmony_ci */
36608c2ecf20Sopenharmony_cistatic int cxlflash_probe(struct pci_dev *pdev,
36618c2ecf20Sopenharmony_ci			  const struct pci_device_id *dev_id)
36628c2ecf20Sopenharmony_ci{
36638c2ecf20Sopenharmony_ci	struct Scsi_Host *host;
36648c2ecf20Sopenharmony_ci	struct cxlflash_cfg *cfg = NULL;
36658c2ecf20Sopenharmony_ci	struct device *dev = &pdev->dev;
36668c2ecf20Sopenharmony_ci	struct dev_dependent_vals *ddv;
36678c2ecf20Sopenharmony_ci	int rc = 0;
36688c2ecf20Sopenharmony_ci	int k;
36698c2ecf20Sopenharmony_ci
36708c2ecf20Sopenharmony_ci	dev_dbg(&pdev->dev, "%s: Found CXLFLASH with IRQ: %d\n",
36718c2ecf20Sopenharmony_ci		__func__, pdev->irq);
36728c2ecf20Sopenharmony_ci
36738c2ecf20Sopenharmony_ci	ddv = (struct dev_dependent_vals *)dev_id->driver_data;
36748c2ecf20Sopenharmony_ci	driver_template.max_sectors = ddv->max_sectors;
36758c2ecf20Sopenharmony_ci
36768c2ecf20Sopenharmony_ci	host = scsi_host_alloc(&driver_template, sizeof(struct cxlflash_cfg));
36778c2ecf20Sopenharmony_ci	if (!host) {
36788c2ecf20Sopenharmony_ci		dev_err(dev, "%s: scsi_host_alloc failed\n", __func__);
36798c2ecf20Sopenharmony_ci		rc = -ENOMEM;
36808c2ecf20Sopenharmony_ci		goto out;
36818c2ecf20Sopenharmony_ci	}
36828c2ecf20Sopenharmony_ci
36838c2ecf20Sopenharmony_ci	host->max_id = CXLFLASH_MAX_NUM_TARGETS_PER_BUS;
36848c2ecf20Sopenharmony_ci	host->max_lun = CXLFLASH_MAX_NUM_LUNS_PER_TARGET;
36858c2ecf20Sopenharmony_ci	host->unique_id = host->host_no;
36868c2ecf20Sopenharmony_ci	host->max_cmd_len = CXLFLASH_MAX_CDB_LEN;
36878c2ecf20Sopenharmony_ci
36888c2ecf20Sopenharmony_ci	cfg = shost_priv(host);
36898c2ecf20Sopenharmony_ci	cfg->state = STATE_PROBING;
36908c2ecf20Sopenharmony_ci	cfg->host = host;
36918c2ecf20Sopenharmony_ci	rc = alloc_mem(cfg);
36928c2ecf20Sopenharmony_ci	if (rc) {
36938c2ecf20Sopenharmony_ci		dev_err(dev, "%s: alloc_mem failed\n", __func__);
36948c2ecf20Sopenharmony_ci		rc = -ENOMEM;
36958c2ecf20Sopenharmony_ci		scsi_host_put(cfg->host);
36968c2ecf20Sopenharmony_ci		goto out;
36978c2ecf20Sopenharmony_ci	}
36988c2ecf20Sopenharmony_ci
36998c2ecf20Sopenharmony_ci	cfg->init_state = INIT_STATE_NONE;
37008c2ecf20Sopenharmony_ci	cfg->dev = pdev;
37018c2ecf20Sopenharmony_ci	cfg->cxl_fops = cxlflash_cxl_fops;
37028c2ecf20Sopenharmony_ci	cfg->ops = cxlflash_assign_ops(ddv);
37038c2ecf20Sopenharmony_ci	WARN_ON_ONCE(!cfg->ops);
37048c2ecf20Sopenharmony_ci
37058c2ecf20Sopenharmony_ci	/*
37068c2ecf20Sopenharmony_ci	 * Promoted LUNs move to the top of the LUN table. The rest stay on
37078c2ecf20Sopenharmony_ci	 * the bottom half. The bottom half grows from the end (index = 255),
37088c2ecf20Sopenharmony_ci	 * whereas the top half grows from the beginning (index = 0).
37098c2ecf20Sopenharmony_ci	 *
37108c2ecf20Sopenharmony_ci	 * Initialize the last LUN index for all possible ports.
37118c2ecf20Sopenharmony_ci	 */
37128c2ecf20Sopenharmony_ci	cfg->promote_lun_index = 0;
37138c2ecf20Sopenharmony_ci
37148c2ecf20Sopenharmony_ci	for (k = 0; k < MAX_FC_PORTS; k++)
37158c2ecf20Sopenharmony_ci		cfg->last_lun_index[k] = CXLFLASH_NUM_VLUNS/2 - 1;
37168c2ecf20Sopenharmony_ci
37178c2ecf20Sopenharmony_ci	cfg->dev_id = (struct pci_device_id *)dev_id;
37188c2ecf20Sopenharmony_ci
37198c2ecf20Sopenharmony_ci	init_waitqueue_head(&cfg->tmf_waitq);
37208c2ecf20Sopenharmony_ci	init_waitqueue_head(&cfg->reset_waitq);
37218c2ecf20Sopenharmony_ci
37228c2ecf20Sopenharmony_ci	INIT_WORK(&cfg->work_q, cxlflash_worker_thread);
37238c2ecf20Sopenharmony_ci	cfg->lr_state = LINK_RESET_INVALID;
37248c2ecf20Sopenharmony_ci	cfg->lr_port = -1;
37258c2ecf20Sopenharmony_ci	spin_lock_init(&cfg->tmf_slock);
37268c2ecf20Sopenharmony_ci	mutex_init(&cfg->ctx_tbl_list_mutex);
37278c2ecf20Sopenharmony_ci	mutex_init(&cfg->ctx_recovery_mutex);
37288c2ecf20Sopenharmony_ci	init_rwsem(&cfg->ioctl_rwsem);
37298c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&cfg->ctx_err_recovery);
37308c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&cfg->lluns);
37318c2ecf20Sopenharmony_ci
37328c2ecf20Sopenharmony_ci	pci_set_drvdata(pdev, cfg);
37338c2ecf20Sopenharmony_ci
37348c2ecf20Sopenharmony_ci	rc = init_pci(cfg);
37358c2ecf20Sopenharmony_ci	if (rc) {
37368c2ecf20Sopenharmony_ci		dev_err(dev, "%s: init_pci failed rc=%d\n", __func__, rc);
37378c2ecf20Sopenharmony_ci		goto out_remove;
37388c2ecf20Sopenharmony_ci	}
37398c2ecf20Sopenharmony_ci	cfg->init_state = INIT_STATE_PCI;
37408c2ecf20Sopenharmony_ci
37418c2ecf20Sopenharmony_ci	cfg->afu_cookie = cfg->ops->create_afu(pdev);
37428c2ecf20Sopenharmony_ci	if (unlikely(!cfg->afu_cookie)) {
37438c2ecf20Sopenharmony_ci		dev_err(dev, "%s: create_afu failed\n", __func__);
37448c2ecf20Sopenharmony_ci		rc = -ENOMEM;
37458c2ecf20Sopenharmony_ci		goto out_remove;
37468c2ecf20Sopenharmony_ci	}
37478c2ecf20Sopenharmony_ci
37488c2ecf20Sopenharmony_ci	rc = init_afu(cfg);
37498c2ecf20Sopenharmony_ci	if (rc && !wq_has_sleeper(&cfg->reset_waitq)) {
37508c2ecf20Sopenharmony_ci		dev_err(dev, "%s: init_afu failed rc=%d\n", __func__, rc);
37518c2ecf20Sopenharmony_ci		goto out_remove;
37528c2ecf20Sopenharmony_ci	}
37538c2ecf20Sopenharmony_ci	cfg->init_state = INIT_STATE_AFU;
37548c2ecf20Sopenharmony_ci
37558c2ecf20Sopenharmony_ci	rc = init_scsi(cfg);
37568c2ecf20Sopenharmony_ci	if (rc) {
37578c2ecf20Sopenharmony_ci		dev_err(dev, "%s: init_scsi failed rc=%d\n", __func__, rc);
37588c2ecf20Sopenharmony_ci		goto out_remove;
37598c2ecf20Sopenharmony_ci	}
37608c2ecf20Sopenharmony_ci	cfg->init_state = INIT_STATE_SCSI;
37618c2ecf20Sopenharmony_ci
37628c2ecf20Sopenharmony_ci	rc = init_chrdev(cfg);
37638c2ecf20Sopenharmony_ci	if (rc) {
37648c2ecf20Sopenharmony_ci		dev_err(dev, "%s: init_chrdev failed rc=%d\n", __func__, rc);
37658c2ecf20Sopenharmony_ci		goto out_remove;
37668c2ecf20Sopenharmony_ci	}
37678c2ecf20Sopenharmony_ci	cfg->init_state = INIT_STATE_CDEV;
37688c2ecf20Sopenharmony_ci
37698c2ecf20Sopenharmony_ci	if (wq_has_sleeper(&cfg->reset_waitq)) {
37708c2ecf20Sopenharmony_ci		cfg->state = STATE_PROBED;
37718c2ecf20Sopenharmony_ci		wake_up_all(&cfg->reset_waitq);
37728c2ecf20Sopenharmony_ci	} else
37738c2ecf20Sopenharmony_ci		cfg->state = STATE_NORMAL;
37748c2ecf20Sopenharmony_ciout:
37758c2ecf20Sopenharmony_ci	dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
37768c2ecf20Sopenharmony_ci	return rc;
37778c2ecf20Sopenharmony_ci
37788c2ecf20Sopenharmony_ciout_remove:
37798c2ecf20Sopenharmony_ci	cfg->state = STATE_PROBED;
37808c2ecf20Sopenharmony_ci	cxlflash_remove(pdev);
37818c2ecf20Sopenharmony_ci	goto out;
37828c2ecf20Sopenharmony_ci}
37838c2ecf20Sopenharmony_ci
37848c2ecf20Sopenharmony_ci/**
37858c2ecf20Sopenharmony_ci * cxlflash_pci_error_detected() - called when a PCI error is detected
37868c2ecf20Sopenharmony_ci * @pdev:	PCI device struct.
37878c2ecf20Sopenharmony_ci * @state:	PCI channel state.
37888c2ecf20Sopenharmony_ci *
37898c2ecf20Sopenharmony_ci * When an EEH occurs during an active reset, wait until the reset is
37908c2ecf20Sopenharmony_ci * complete and then take action based upon the device state.
37918c2ecf20Sopenharmony_ci *
37928c2ecf20Sopenharmony_ci * Return: PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT
37938c2ecf20Sopenharmony_ci */
37948c2ecf20Sopenharmony_cistatic pci_ers_result_t cxlflash_pci_error_detected(struct pci_dev *pdev,
37958c2ecf20Sopenharmony_ci						    pci_channel_state_t state)
37968c2ecf20Sopenharmony_ci{
37978c2ecf20Sopenharmony_ci	int rc = 0;
37988c2ecf20Sopenharmony_ci	struct cxlflash_cfg *cfg = pci_get_drvdata(pdev);
37998c2ecf20Sopenharmony_ci	struct device *dev = &cfg->dev->dev;
38008c2ecf20Sopenharmony_ci
38018c2ecf20Sopenharmony_ci	dev_dbg(dev, "%s: pdev=%p state=%u\n", __func__, pdev, state);
38028c2ecf20Sopenharmony_ci
38038c2ecf20Sopenharmony_ci	switch (state) {
38048c2ecf20Sopenharmony_ci	case pci_channel_io_frozen:
38058c2ecf20Sopenharmony_ci		wait_event(cfg->reset_waitq, cfg->state != STATE_RESET &&
38068c2ecf20Sopenharmony_ci					     cfg->state != STATE_PROBING);
38078c2ecf20Sopenharmony_ci		if (cfg->state == STATE_FAILTERM)
38088c2ecf20Sopenharmony_ci			return PCI_ERS_RESULT_DISCONNECT;
38098c2ecf20Sopenharmony_ci
38108c2ecf20Sopenharmony_ci		cfg->state = STATE_RESET;
38118c2ecf20Sopenharmony_ci		scsi_block_requests(cfg->host);
38128c2ecf20Sopenharmony_ci		drain_ioctls(cfg);
38138c2ecf20Sopenharmony_ci		rc = cxlflash_mark_contexts_error(cfg);
38148c2ecf20Sopenharmony_ci		if (unlikely(rc))
38158c2ecf20Sopenharmony_ci			dev_err(dev, "%s: Failed to mark user contexts rc=%d\n",
38168c2ecf20Sopenharmony_ci				__func__, rc);
38178c2ecf20Sopenharmony_ci		term_afu(cfg);
38188c2ecf20Sopenharmony_ci		return PCI_ERS_RESULT_NEED_RESET;
38198c2ecf20Sopenharmony_ci	case pci_channel_io_perm_failure:
38208c2ecf20Sopenharmony_ci		cfg->state = STATE_FAILTERM;
38218c2ecf20Sopenharmony_ci		wake_up_all(&cfg->reset_waitq);
38228c2ecf20Sopenharmony_ci		scsi_unblock_requests(cfg->host);
38238c2ecf20Sopenharmony_ci		return PCI_ERS_RESULT_DISCONNECT;
38248c2ecf20Sopenharmony_ci	default:
38258c2ecf20Sopenharmony_ci		break;
38268c2ecf20Sopenharmony_ci	}
38278c2ecf20Sopenharmony_ci	return PCI_ERS_RESULT_NEED_RESET;
38288c2ecf20Sopenharmony_ci}
38298c2ecf20Sopenharmony_ci
38308c2ecf20Sopenharmony_ci/**
38318c2ecf20Sopenharmony_ci * cxlflash_pci_slot_reset() - called when PCI slot has been reset
38328c2ecf20Sopenharmony_ci * @pdev:	PCI device struct.
38338c2ecf20Sopenharmony_ci *
38348c2ecf20Sopenharmony_ci * This routine is called by the pci error recovery code after the PCI
38358c2ecf20Sopenharmony_ci * slot has been reset, just before we should resume normal operations.
38368c2ecf20Sopenharmony_ci *
38378c2ecf20Sopenharmony_ci * Return: PCI_ERS_RESULT_RECOVERED or PCI_ERS_RESULT_DISCONNECT
38388c2ecf20Sopenharmony_ci */
38398c2ecf20Sopenharmony_cistatic pci_ers_result_t cxlflash_pci_slot_reset(struct pci_dev *pdev)
38408c2ecf20Sopenharmony_ci{
38418c2ecf20Sopenharmony_ci	int rc = 0;
38428c2ecf20Sopenharmony_ci	struct cxlflash_cfg *cfg = pci_get_drvdata(pdev);
38438c2ecf20Sopenharmony_ci	struct device *dev = &cfg->dev->dev;
38448c2ecf20Sopenharmony_ci
38458c2ecf20Sopenharmony_ci	dev_dbg(dev, "%s: pdev=%p\n", __func__, pdev);
38468c2ecf20Sopenharmony_ci
38478c2ecf20Sopenharmony_ci	rc = init_afu(cfg);
38488c2ecf20Sopenharmony_ci	if (unlikely(rc)) {
38498c2ecf20Sopenharmony_ci		dev_err(dev, "%s: EEH recovery failed rc=%d\n", __func__, rc);
38508c2ecf20Sopenharmony_ci		return PCI_ERS_RESULT_DISCONNECT;
38518c2ecf20Sopenharmony_ci	}
38528c2ecf20Sopenharmony_ci
38538c2ecf20Sopenharmony_ci	return PCI_ERS_RESULT_RECOVERED;
38548c2ecf20Sopenharmony_ci}
38558c2ecf20Sopenharmony_ci
38568c2ecf20Sopenharmony_ci/**
38578c2ecf20Sopenharmony_ci * cxlflash_pci_resume() - called when normal operation can resume
38588c2ecf20Sopenharmony_ci * @pdev:	PCI device struct
38598c2ecf20Sopenharmony_ci */
38608c2ecf20Sopenharmony_cistatic void cxlflash_pci_resume(struct pci_dev *pdev)
38618c2ecf20Sopenharmony_ci{
38628c2ecf20Sopenharmony_ci	struct cxlflash_cfg *cfg = pci_get_drvdata(pdev);
38638c2ecf20Sopenharmony_ci	struct device *dev = &cfg->dev->dev;
38648c2ecf20Sopenharmony_ci
38658c2ecf20Sopenharmony_ci	dev_dbg(dev, "%s: pdev=%p\n", __func__, pdev);
38668c2ecf20Sopenharmony_ci
38678c2ecf20Sopenharmony_ci	cfg->state = STATE_NORMAL;
38688c2ecf20Sopenharmony_ci	wake_up_all(&cfg->reset_waitq);
38698c2ecf20Sopenharmony_ci	scsi_unblock_requests(cfg->host);
38708c2ecf20Sopenharmony_ci}
38718c2ecf20Sopenharmony_ci
38728c2ecf20Sopenharmony_ci/**
38738c2ecf20Sopenharmony_ci * cxlflash_devnode() - provides devtmpfs for devices in the cxlflash class
38748c2ecf20Sopenharmony_ci * @dev:	Character device.
38758c2ecf20Sopenharmony_ci * @mode:	Mode that can be used to verify access.
38768c2ecf20Sopenharmony_ci *
38778c2ecf20Sopenharmony_ci * Return: Allocated string describing the devtmpfs structure.
38788c2ecf20Sopenharmony_ci */
38798c2ecf20Sopenharmony_cistatic char *cxlflash_devnode(struct device *dev, umode_t *mode)
38808c2ecf20Sopenharmony_ci{
38818c2ecf20Sopenharmony_ci	return kasprintf(GFP_KERNEL, "cxlflash/%s", dev_name(dev));
38828c2ecf20Sopenharmony_ci}
38838c2ecf20Sopenharmony_ci
38848c2ecf20Sopenharmony_ci/**
38858c2ecf20Sopenharmony_ci * cxlflash_class_init() - create character device class
38868c2ecf20Sopenharmony_ci *
38878c2ecf20Sopenharmony_ci * Return: 0 on success, -errno on failure
38888c2ecf20Sopenharmony_ci */
38898c2ecf20Sopenharmony_cistatic int cxlflash_class_init(void)
38908c2ecf20Sopenharmony_ci{
38918c2ecf20Sopenharmony_ci	dev_t devno;
38928c2ecf20Sopenharmony_ci	int rc = 0;
38938c2ecf20Sopenharmony_ci
38948c2ecf20Sopenharmony_ci	rc = alloc_chrdev_region(&devno, 0, CXLFLASH_MAX_ADAPTERS, "cxlflash");
38958c2ecf20Sopenharmony_ci	if (unlikely(rc)) {
38968c2ecf20Sopenharmony_ci		pr_err("%s: alloc_chrdev_region failed rc=%d\n", __func__, rc);
38978c2ecf20Sopenharmony_ci		goto out;
38988c2ecf20Sopenharmony_ci	}
38998c2ecf20Sopenharmony_ci
39008c2ecf20Sopenharmony_ci	cxlflash_major = MAJOR(devno);
39018c2ecf20Sopenharmony_ci
39028c2ecf20Sopenharmony_ci	cxlflash_class = class_create(THIS_MODULE, "cxlflash");
39038c2ecf20Sopenharmony_ci	if (IS_ERR(cxlflash_class)) {
39048c2ecf20Sopenharmony_ci		rc = PTR_ERR(cxlflash_class);
39058c2ecf20Sopenharmony_ci		pr_err("%s: class_create failed rc=%d\n", __func__, rc);
39068c2ecf20Sopenharmony_ci		goto err;
39078c2ecf20Sopenharmony_ci	}
39088c2ecf20Sopenharmony_ci
39098c2ecf20Sopenharmony_ci	cxlflash_class->devnode = cxlflash_devnode;
39108c2ecf20Sopenharmony_ciout:
39118c2ecf20Sopenharmony_ci	pr_debug("%s: returning rc=%d\n", __func__, rc);
39128c2ecf20Sopenharmony_ci	return rc;
39138c2ecf20Sopenharmony_cierr:
39148c2ecf20Sopenharmony_ci	unregister_chrdev_region(devno, CXLFLASH_MAX_ADAPTERS);
39158c2ecf20Sopenharmony_ci	goto out;
39168c2ecf20Sopenharmony_ci}
39178c2ecf20Sopenharmony_ci
39188c2ecf20Sopenharmony_ci/**
39198c2ecf20Sopenharmony_ci * cxlflash_class_exit() - destroy character device class
39208c2ecf20Sopenharmony_ci */
39218c2ecf20Sopenharmony_cistatic void cxlflash_class_exit(void)
39228c2ecf20Sopenharmony_ci{
39238c2ecf20Sopenharmony_ci	dev_t devno = MKDEV(cxlflash_major, 0);
39248c2ecf20Sopenharmony_ci
39258c2ecf20Sopenharmony_ci	class_destroy(cxlflash_class);
39268c2ecf20Sopenharmony_ci	unregister_chrdev_region(devno, CXLFLASH_MAX_ADAPTERS);
39278c2ecf20Sopenharmony_ci}
39288c2ecf20Sopenharmony_ci
39298c2ecf20Sopenharmony_cistatic const struct pci_error_handlers cxlflash_err_handler = {
39308c2ecf20Sopenharmony_ci	.error_detected = cxlflash_pci_error_detected,
39318c2ecf20Sopenharmony_ci	.slot_reset = cxlflash_pci_slot_reset,
39328c2ecf20Sopenharmony_ci	.resume = cxlflash_pci_resume,
39338c2ecf20Sopenharmony_ci};
39348c2ecf20Sopenharmony_ci
39358c2ecf20Sopenharmony_ci/*
39368c2ecf20Sopenharmony_ci * PCI device structure
39378c2ecf20Sopenharmony_ci */
39388c2ecf20Sopenharmony_cistatic struct pci_driver cxlflash_driver = {
39398c2ecf20Sopenharmony_ci	.name = CXLFLASH_NAME,
39408c2ecf20Sopenharmony_ci	.id_table = cxlflash_pci_table,
39418c2ecf20Sopenharmony_ci	.probe = cxlflash_probe,
39428c2ecf20Sopenharmony_ci	.remove = cxlflash_remove,
39438c2ecf20Sopenharmony_ci	.shutdown = cxlflash_remove,
39448c2ecf20Sopenharmony_ci	.err_handler = &cxlflash_err_handler,
39458c2ecf20Sopenharmony_ci};
39468c2ecf20Sopenharmony_ci
39478c2ecf20Sopenharmony_ci/**
39488c2ecf20Sopenharmony_ci * init_cxlflash() - module entry point
39498c2ecf20Sopenharmony_ci *
39508c2ecf20Sopenharmony_ci * Return: 0 on success, -errno on failure
39518c2ecf20Sopenharmony_ci */
39528c2ecf20Sopenharmony_cistatic int __init init_cxlflash(void)
39538c2ecf20Sopenharmony_ci{
39548c2ecf20Sopenharmony_ci	int rc;
39558c2ecf20Sopenharmony_ci
39568c2ecf20Sopenharmony_ci	check_sizes();
39578c2ecf20Sopenharmony_ci	cxlflash_list_init();
39588c2ecf20Sopenharmony_ci	rc = cxlflash_class_init();
39598c2ecf20Sopenharmony_ci	if (unlikely(rc))
39608c2ecf20Sopenharmony_ci		goto out;
39618c2ecf20Sopenharmony_ci
39628c2ecf20Sopenharmony_ci	rc = pci_register_driver(&cxlflash_driver);
39638c2ecf20Sopenharmony_ci	if (unlikely(rc))
39648c2ecf20Sopenharmony_ci		goto err;
39658c2ecf20Sopenharmony_ciout:
39668c2ecf20Sopenharmony_ci	pr_debug("%s: returning rc=%d\n", __func__, rc);
39678c2ecf20Sopenharmony_ci	return rc;
39688c2ecf20Sopenharmony_cierr:
39698c2ecf20Sopenharmony_ci	cxlflash_class_exit();
39708c2ecf20Sopenharmony_ci	goto out;
39718c2ecf20Sopenharmony_ci}
39728c2ecf20Sopenharmony_ci
39738c2ecf20Sopenharmony_ci/**
39748c2ecf20Sopenharmony_ci * exit_cxlflash() - module exit point
39758c2ecf20Sopenharmony_ci */
39768c2ecf20Sopenharmony_cistatic void __exit exit_cxlflash(void)
39778c2ecf20Sopenharmony_ci{
39788c2ecf20Sopenharmony_ci	cxlflash_term_global_luns();
39798c2ecf20Sopenharmony_ci	cxlflash_free_errpage();
39808c2ecf20Sopenharmony_ci
39818c2ecf20Sopenharmony_ci	pci_unregister_driver(&cxlflash_driver);
39828c2ecf20Sopenharmony_ci	cxlflash_class_exit();
39838c2ecf20Sopenharmony_ci}
39848c2ecf20Sopenharmony_ci
39858c2ecf20Sopenharmony_cimodule_init(init_cxlflash);
39868c2ecf20Sopenharmony_cimodule_exit(exit_cxlflash);
3987