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/file.h>
138c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
148c2ecf20Sopenharmony_ci#include <linux/pci.h>
158c2ecf20Sopenharmony_ci#include <linux/syscalls.h>
168c2ecf20Sopenharmony_ci#include <asm/unaligned.h>
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci#include <scsi/scsi.h>
198c2ecf20Sopenharmony_ci#include <scsi/scsi_host.h>
208c2ecf20Sopenharmony_ci#include <scsi/scsi_cmnd.h>
218c2ecf20Sopenharmony_ci#include <scsi/scsi_eh.h>
228c2ecf20Sopenharmony_ci#include <uapi/scsi/cxlflash_ioctl.h>
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci#include "sislite.h"
258c2ecf20Sopenharmony_ci#include "common.h"
268c2ecf20Sopenharmony_ci#include "vlun.h"
278c2ecf20Sopenharmony_ci#include "superpipe.h"
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_cistruct cxlflash_global global;
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci/**
328c2ecf20Sopenharmony_ci * marshal_rele_to_resize() - translate release to resize structure
338c2ecf20Sopenharmony_ci * @rele:	Source structure from which to translate/copy.
348c2ecf20Sopenharmony_ci * @resize:	Destination structure for the translate/copy.
358c2ecf20Sopenharmony_ci */
368c2ecf20Sopenharmony_cistatic void marshal_rele_to_resize(struct dk_cxlflash_release *release,
378c2ecf20Sopenharmony_ci				   struct dk_cxlflash_resize *resize)
388c2ecf20Sopenharmony_ci{
398c2ecf20Sopenharmony_ci	resize->hdr = release->hdr;
408c2ecf20Sopenharmony_ci	resize->context_id = release->context_id;
418c2ecf20Sopenharmony_ci	resize->rsrc_handle = release->rsrc_handle;
428c2ecf20Sopenharmony_ci}
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci/**
458c2ecf20Sopenharmony_ci * marshal_det_to_rele() - translate detach to release structure
468c2ecf20Sopenharmony_ci * @detach:	Destination structure for the translate/copy.
478c2ecf20Sopenharmony_ci * @rele:	Source structure from which to translate/copy.
488c2ecf20Sopenharmony_ci */
498c2ecf20Sopenharmony_cistatic void marshal_det_to_rele(struct dk_cxlflash_detach *detach,
508c2ecf20Sopenharmony_ci				struct dk_cxlflash_release *release)
518c2ecf20Sopenharmony_ci{
528c2ecf20Sopenharmony_ci	release->hdr = detach->hdr;
538c2ecf20Sopenharmony_ci	release->context_id = detach->context_id;
548c2ecf20Sopenharmony_ci}
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci/**
578c2ecf20Sopenharmony_ci * marshal_udir_to_rele() - translate udirect to release structure
588c2ecf20Sopenharmony_ci * @udirect:	Source structure from which to translate/copy.
598c2ecf20Sopenharmony_ci * @release:	Destination structure for the translate/copy.
608c2ecf20Sopenharmony_ci */
618c2ecf20Sopenharmony_cistatic void marshal_udir_to_rele(struct dk_cxlflash_udirect *udirect,
628c2ecf20Sopenharmony_ci				 struct dk_cxlflash_release *release)
638c2ecf20Sopenharmony_ci{
648c2ecf20Sopenharmony_ci	release->hdr = udirect->hdr;
658c2ecf20Sopenharmony_ci	release->context_id = udirect->context_id;
668c2ecf20Sopenharmony_ci	release->rsrc_handle = udirect->rsrc_handle;
678c2ecf20Sopenharmony_ci}
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci/**
708c2ecf20Sopenharmony_ci * cxlflash_free_errpage() - frees resources associated with global error page
718c2ecf20Sopenharmony_ci */
728c2ecf20Sopenharmony_civoid cxlflash_free_errpage(void)
738c2ecf20Sopenharmony_ci{
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci	mutex_lock(&global.mutex);
768c2ecf20Sopenharmony_ci	if (global.err_page) {
778c2ecf20Sopenharmony_ci		__free_page(global.err_page);
788c2ecf20Sopenharmony_ci		global.err_page = NULL;
798c2ecf20Sopenharmony_ci	}
808c2ecf20Sopenharmony_ci	mutex_unlock(&global.mutex);
818c2ecf20Sopenharmony_ci}
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci/**
848c2ecf20Sopenharmony_ci * cxlflash_stop_term_user_contexts() - stops/terminates known user contexts
858c2ecf20Sopenharmony_ci * @cfg:	Internal structure associated with the host.
868c2ecf20Sopenharmony_ci *
878c2ecf20Sopenharmony_ci * When the host needs to go down, all users must be quiesced and their
888c2ecf20Sopenharmony_ci * memory freed. This is accomplished by putting the contexts in error
898c2ecf20Sopenharmony_ci * state which will notify the user and let them 'drive' the tear down.
908c2ecf20Sopenharmony_ci * Meanwhile, this routine camps until all user contexts have been removed.
918c2ecf20Sopenharmony_ci *
928c2ecf20Sopenharmony_ci * Note that the main loop in this routine will always execute at least once
938c2ecf20Sopenharmony_ci * to flush the reset_waitq.
948c2ecf20Sopenharmony_ci */
958c2ecf20Sopenharmony_civoid cxlflash_stop_term_user_contexts(struct cxlflash_cfg *cfg)
968c2ecf20Sopenharmony_ci{
978c2ecf20Sopenharmony_ci	struct device *dev = &cfg->dev->dev;
988c2ecf20Sopenharmony_ci	int i, found = true;
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci	cxlflash_mark_contexts_error(cfg);
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci	while (true) {
1038c2ecf20Sopenharmony_ci		for (i = 0; i < MAX_CONTEXT; i++)
1048c2ecf20Sopenharmony_ci			if (cfg->ctx_tbl[i]) {
1058c2ecf20Sopenharmony_ci				found = true;
1068c2ecf20Sopenharmony_ci				break;
1078c2ecf20Sopenharmony_ci			}
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci		if (!found && list_empty(&cfg->ctx_err_recovery))
1108c2ecf20Sopenharmony_ci			return;
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci		dev_dbg(dev, "%s: Wait for user contexts to quiesce...\n",
1138c2ecf20Sopenharmony_ci			__func__);
1148c2ecf20Sopenharmony_ci		wake_up_all(&cfg->reset_waitq);
1158c2ecf20Sopenharmony_ci		ssleep(1);
1168c2ecf20Sopenharmony_ci		found = false;
1178c2ecf20Sopenharmony_ci	}
1188c2ecf20Sopenharmony_ci}
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci/**
1218c2ecf20Sopenharmony_ci * find_error_context() - locates a context by cookie on the error recovery list
1228c2ecf20Sopenharmony_ci * @cfg:	Internal structure associated with the host.
1238c2ecf20Sopenharmony_ci * @rctxid:	Desired context by id.
1248c2ecf20Sopenharmony_ci * @file:	Desired context by file.
1258c2ecf20Sopenharmony_ci *
1268c2ecf20Sopenharmony_ci * Return: Found context on success, NULL on failure
1278c2ecf20Sopenharmony_ci */
1288c2ecf20Sopenharmony_cistatic struct ctx_info *find_error_context(struct cxlflash_cfg *cfg, u64 rctxid,
1298c2ecf20Sopenharmony_ci					   struct file *file)
1308c2ecf20Sopenharmony_ci{
1318c2ecf20Sopenharmony_ci	struct ctx_info *ctxi;
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci	list_for_each_entry(ctxi, &cfg->ctx_err_recovery, list)
1348c2ecf20Sopenharmony_ci		if ((ctxi->ctxid == rctxid) || (ctxi->file == file))
1358c2ecf20Sopenharmony_ci			return ctxi;
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ci	return NULL;
1388c2ecf20Sopenharmony_ci}
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci/**
1418c2ecf20Sopenharmony_ci * get_context() - obtains a validated and locked context reference
1428c2ecf20Sopenharmony_ci * @cfg:	Internal structure associated with the host.
1438c2ecf20Sopenharmony_ci * @rctxid:	Desired context (raw, un-decoded format).
1448c2ecf20Sopenharmony_ci * @arg:	LUN information or file associated with request.
1458c2ecf20Sopenharmony_ci * @ctx_ctrl:	Control information to 'steer' desired lookup.
1468c2ecf20Sopenharmony_ci *
1478c2ecf20Sopenharmony_ci * NOTE: despite the name pid, in linux, current->pid actually refers
1488c2ecf20Sopenharmony_ci * to the lightweight process id (tid) and can change if the process is
1498c2ecf20Sopenharmony_ci * multi threaded. The tgid remains constant for the process and only changes
1508c2ecf20Sopenharmony_ci * when the process of fork. For all intents and purposes, think of tgid
1518c2ecf20Sopenharmony_ci * as a pid in the traditional sense.
1528c2ecf20Sopenharmony_ci *
1538c2ecf20Sopenharmony_ci * Return: Validated context on success, NULL on failure
1548c2ecf20Sopenharmony_ci */
1558c2ecf20Sopenharmony_cistruct ctx_info *get_context(struct cxlflash_cfg *cfg, u64 rctxid,
1568c2ecf20Sopenharmony_ci			     void *arg, enum ctx_ctrl ctx_ctrl)
1578c2ecf20Sopenharmony_ci{
1588c2ecf20Sopenharmony_ci	struct device *dev = &cfg->dev->dev;
1598c2ecf20Sopenharmony_ci	struct ctx_info *ctxi = NULL;
1608c2ecf20Sopenharmony_ci	struct lun_access *lun_access = NULL;
1618c2ecf20Sopenharmony_ci	struct file *file = NULL;
1628c2ecf20Sopenharmony_ci	struct llun_info *lli = arg;
1638c2ecf20Sopenharmony_ci	u64 ctxid = DECODE_CTXID(rctxid);
1648c2ecf20Sopenharmony_ci	int rc;
1658c2ecf20Sopenharmony_ci	pid_t pid = task_tgid_nr(current), ctxpid = 0;
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ci	if (ctx_ctrl & CTX_CTRL_FILE) {
1688c2ecf20Sopenharmony_ci		lli = NULL;
1698c2ecf20Sopenharmony_ci		file = (struct file *)arg;
1708c2ecf20Sopenharmony_ci	}
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ci	if (ctx_ctrl & CTX_CTRL_CLONE)
1738c2ecf20Sopenharmony_ci		pid = task_ppid_nr(current);
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci	if (likely(ctxid < MAX_CONTEXT)) {
1768c2ecf20Sopenharmony_ci		while (true) {
1778c2ecf20Sopenharmony_ci			mutex_lock(&cfg->ctx_tbl_list_mutex);
1788c2ecf20Sopenharmony_ci			ctxi = cfg->ctx_tbl[ctxid];
1798c2ecf20Sopenharmony_ci			if (ctxi)
1808c2ecf20Sopenharmony_ci				if ((file && (ctxi->file != file)) ||
1818c2ecf20Sopenharmony_ci				    (!file && (ctxi->ctxid != rctxid)))
1828c2ecf20Sopenharmony_ci					ctxi = NULL;
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci			if ((ctx_ctrl & CTX_CTRL_ERR) ||
1858c2ecf20Sopenharmony_ci			    (!ctxi && (ctx_ctrl & CTX_CTRL_ERR_FALLBACK)))
1868c2ecf20Sopenharmony_ci				ctxi = find_error_context(cfg, rctxid, file);
1878c2ecf20Sopenharmony_ci			if (!ctxi) {
1888c2ecf20Sopenharmony_ci				mutex_unlock(&cfg->ctx_tbl_list_mutex);
1898c2ecf20Sopenharmony_ci				goto out;
1908c2ecf20Sopenharmony_ci			}
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci			/*
1938c2ecf20Sopenharmony_ci			 * Need to acquire ownership of the context while still
1948c2ecf20Sopenharmony_ci			 * under the table/list lock to serialize with a remove
1958c2ecf20Sopenharmony_ci			 * thread. Use the 'try' to avoid stalling the
1968c2ecf20Sopenharmony_ci			 * table/list lock for a single context.
1978c2ecf20Sopenharmony_ci			 *
1988c2ecf20Sopenharmony_ci			 * Note that the lock order is:
1998c2ecf20Sopenharmony_ci			 *
2008c2ecf20Sopenharmony_ci			 *	cfg->ctx_tbl_list_mutex -> ctxi->mutex
2018c2ecf20Sopenharmony_ci			 *
2028c2ecf20Sopenharmony_ci			 * Therefore release ctx_tbl_list_mutex before retrying.
2038c2ecf20Sopenharmony_ci			 */
2048c2ecf20Sopenharmony_ci			rc = mutex_trylock(&ctxi->mutex);
2058c2ecf20Sopenharmony_ci			mutex_unlock(&cfg->ctx_tbl_list_mutex);
2068c2ecf20Sopenharmony_ci			if (rc)
2078c2ecf20Sopenharmony_ci				break; /* got the context's lock! */
2088c2ecf20Sopenharmony_ci		}
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_ci		if (ctxi->unavail)
2118c2ecf20Sopenharmony_ci			goto denied;
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ci		ctxpid = ctxi->pid;
2148c2ecf20Sopenharmony_ci		if (likely(!(ctx_ctrl & CTX_CTRL_NOPID)))
2158c2ecf20Sopenharmony_ci			if (pid != ctxpid)
2168c2ecf20Sopenharmony_ci				goto denied;
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_ci		if (lli) {
2198c2ecf20Sopenharmony_ci			list_for_each_entry(lun_access, &ctxi->luns, list)
2208c2ecf20Sopenharmony_ci				if (lun_access->lli == lli)
2218c2ecf20Sopenharmony_ci					goto out;
2228c2ecf20Sopenharmony_ci			goto denied;
2238c2ecf20Sopenharmony_ci		}
2248c2ecf20Sopenharmony_ci	}
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ciout:
2278c2ecf20Sopenharmony_ci	dev_dbg(dev, "%s: rctxid=%016llx ctxinfo=%p ctxpid=%u pid=%u "
2288c2ecf20Sopenharmony_ci		"ctx_ctrl=%u\n", __func__, rctxid, ctxi, ctxpid, pid,
2298c2ecf20Sopenharmony_ci		ctx_ctrl);
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_ci	return ctxi;
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_cidenied:
2348c2ecf20Sopenharmony_ci	mutex_unlock(&ctxi->mutex);
2358c2ecf20Sopenharmony_ci	ctxi = NULL;
2368c2ecf20Sopenharmony_ci	goto out;
2378c2ecf20Sopenharmony_ci}
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ci/**
2408c2ecf20Sopenharmony_ci * put_context() - release a context that was retrieved from get_context()
2418c2ecf20Sopenharmony_ci * @ctxi:	Context to release.
2428c2ecf20Sopenharmony_ci *
2438c2ecf20Sopenharmony_ci * For now, releasing the context equates to unlocking it's mutex.
2448c2ecf20Sopenharmony_ci */
2458c2ecf20Sopenharmony_civoid put_context(struct ctx_info *ctxi)
2468c2ecf20Sopenharmony_ci{
2478c2ecf20Sopenharmony_ci	mutex_unlock(&ctxi->mutex);
2488c2ecf20Sopenharmony_ci}
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_ci/**
2518c2ecf20Sopenharmony_ci * afu_attach() - attach a context to the AFU
2528c2ecf20Sopenharmony_ci * @cfg:	Internal structure associated with the host.
2538c2ecf20Sopenharmony_ci * @ctxi:	Context to attach.
2548c2ecf20Sopenharmony_ci *
2558c2ecf20Sopenharmony_ci * Upon setting the context capabilities, they must be confirmed with
2568c2ecf20Sopenharmony_ci * a read back operation as the context might have been closed since
2578c2ecf20Sopenharmony_ci * the mailbox was unlocked. When this occurs, registration is failed.
2588c2ecf20Sopenharmony_ci *
2598c2ecf20Sopenharmony_ci * Return: 0 on success, -errno on failure
2608c2ecf20Sopenharmony_ci */
2618c2ecf20Sopenharmony_cistatic int afu_attach(struct cxlflash_cfg *cfg, struct ctx_info *ctxi)
2628c2ecf20Sopenharmony_ci{
2638c2ecf20Sopenharmony_ci	struct device *dev = &cfg->dev->dev;
2648c2ecf20Sopenharmony_ci	struct afu *afu = cfg->afu;
2658c2ecf20Sopenharmony_ci	struct sisl_ctrl_map __iomem *ctrl_map = ctxi->ctrl_map;
2668c2ecf20Sopenharmony_ci	int rc = 0;
2678c2ecf20Sopenharmony_ci	struct hwq *hwq = get_hwq(afu, PRIMARY_HWQ);
2688c2ecf20Sopenharmony_ci	u64 val;
2698c2ecf20Sopenharmony_ci	int i;
2708c2ecf20Sopenharmony_ci
2718c2ecf20Sopenharmony_ci	/* Unlock cap and restrict user to read/write cmds in translated mode */
2728c2ecf20Sopenharmony_ci	readq_be(&ctrl_map->mbox_r);
2738c2ecf20Sopenharmony_ci	val = (SISL_CTX_CAP_READ_CMD | SISL_CTX_CAP_WRITE_CMD);
2748c2ecf20Sopenharmony_ci	writeq_be(val, &ctrl_map->ctx_cap);
2758c2ecf20Sopenharmony_ci	val = readq_be(&ctrl_map->ctx_cap);
2768c2ecf20Sopenharmony_ci	if (val != (SISL_CTX_CAP_READ_CMD | SISL_CTX_CAP_WRITE_CMD)) {
2778c2ecf20Sopenharmony_ci		dev_err(dev, "%s: ctx may be closed val=%016llx\n",
2788c2ecf20Sopenharmony_ci			__func__, val);
2798c2ecf20Sopenharmony_ci		rc = -EAGAIN;
2808c2ecf20Sopenharmony_ci		goto out;
2818c2ecf20Sopenharmony_ci	}
2828c2ecf20Sopenharmony_ci
2838c2ecf20Sopenharmony_ci	if (afu_is_ocxl_lisn(afu)) {
2848c2ecf20Sopenharmony_ci		/* Set up the LISN effective address for each interrupt */
2858c2ecf20Sopenharmony_ci		for (i = 0; i < ctxi->irqs; i++) {
2868c2ecf20Sopenharmony_ci			val = cfg->ops->get_irq_objhndl(ctxi->ctx, i);
2878c2ecf20Sopenharmony_ci			writeq_be(val, &ctrl_map->lisn_ea[i]);
2888c2ecf20Sopenharmony_ci		}
2898c2ecf20Sopenharmony_ci
2908c2ecf20Sopenharmony_ci		/* Use primary HWQ PASID as identifier for all interrupts */
2918c2ecf20Sopenharmony_ci		val = hwq->ctx_hndl;
2928c2ecf20Sopenharmony_ci		writeq_be(SISL_LISN_PASID(val, val), &ctrl_map->lisn_pasid[0]);
2938c2ecf20Sopenharmony_ci		writeq_be(SISL_LISN_PASID(0UL, val), &ctrl_map->lisn_pasid[1]);
2948c2ecf20Sopenharmony_ci	}
2958c2ecf20Sopenharmony_ci
2968c2ecf20Sopenharmony_ci	/* Set up MMIO registers pointing to the RHT */
2978c2ecf20Sopenharmony_ci	writeq_be((u64)ctxi->rht_start, &ctrl_map->rht_start);
2988c2ecf20Sopenharmony_ci	val = SISL_RHT_CNT_ID((u64)MAX_RHT_PER_CONTEXT, (u64)(hwq->ctx_hndl));
2998c2ecf20Sopenharmony_ci	writeq_be(val, &ctrl_map->rht_cnt_id);
3008c2ecf20Sopenharmony_ciout:
3018c2ecf20Sopenharmony_ci	dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
3028c2ecf20Sopenharmony_ci	return rc;
3038c2ecf20Sopenharmony_ci}
3048c2ecf20Sopenharmony_ci
3058c2ecf20Sopenharmony_ci/**
3068c2ecf20Sopenharmony_ci * read_cap16() - issues a SCSI READ_CAP16 command
3078c2ecf20Sopenharmony_ci * @sdev:	SCSI device associated with LUN.
3088c2ecf20Sopenharmony_ci * @lli:	LUN destined for capacity request.
3098c2ecf20Sopenharmony_ci *
3108c2ecf20Sopenharmony_ci * The READ_CAP16 can take quite a while to complete. Should an EEH occur while
3118c2ecf20Sopenharmony_ci * in scsi_execute(), the EEH handler will attempt to recover. As part of the
3128c2ecf20Sopenharmony_ci * recovery, the handler drains all currently running ioctls, waiting until they
3138c2ecf20Sopenharmony_ci * have completed before proceeding with a reset. As this routine is used on the
3148c2ecf20Sopenharmony_ci * ioctl path, this can create a condition where the EEH handler becomes stuck,
3158c2ecf20Sopenharmony_ci * infinitely waiting for this ioctl thread. To avoid this behavior, temporarily
3168c2ecf20Sopenharmony_ci * unmark this thread as an ioctl thread by releasing the ioctl read semaphore.
3178c2ecf20Sopenharmony_ci * This will allow the EEH handler to proceed with a recovery while this thread
3188c2ecf20Sopenharmony_ci * is still running. Once the scsi_execute() returns, reacquire the ioctl read
3198c2ecf20Sopenharmony_ci * semaphore and check the adapter state in case it changed while inside of
3208c2ecf20Sopenharmony_ci * scsi_execute(). The state check will wait if the adapter is still being
3218c2ecf20Sopenharmony_ci * recovered or return a failure if the recovery failed. In the event that the
3228c2ecf20Sopenharmony_ci * adapter reset failed, simply return the failure as the ioctl would be unable
3238c2ecf20Sopenharmony_ci * to continue.
3248c2ecf20Sopenharmony_ci *
3258c2ecf20Sopenharmony_ci * Note that the above puts a requirement on this routine to only be called on
3268c2ecf20Sopenharmony_ci * an ioctl thread.
3278c2ecf20Sopenharmony_ci *
3288c2ecf20Sopenharmony_ci * Return: 0 on success, -errno on failure
3298c2ecf20Sopenharmony_ci */
3308c2ecf20Sopenharmony_cistatic int read_cap16(struct scsi_device *sdev, struct llun_info *lli)
3318c2ecf20Sopenharmony_ci{
3328c2ecf20Sopenharmony_ci	struct cxlflash_cfg *cfg = shost_priv(sdev->host);
3338c2ecf20Sopenharmony_ci	struct device *dev = &cfg->dev->dev;
3348c2ecf20Sopenharmony_ci	struct glun_info *gli = lli->parent;
3358c2ecf20Sopenharmony_ci	struct scsi_sense_hdr sshdr;
3368c2ecf20Sopenharmony_ci	u8 *cmd_buf = NULL;
3378c2ecf20Sopenharmony_ci	u8 *scsi_cmd = NULL;
3388c2ecf20Sopenharmony_ci	int rc = 0;
3398c2ecf20Sopenharmony_ci	int result = 0;
3408c2ecf20Sopenharmony_ci	int retry_cnt = 0;
3418c2ecf20Sopenharmony_ci	u32 to = CMD_TIMEOUT * HZ;
3428c2ecf20Sopenharmony_ci
3438c2ecf20Sopenharmony_ciretry:
3448c2ecf20Sopenharmony_ci	cmd_buf = kzalloc(CMD_BUFSIZE, GFP_KERNEL);
3458c2ecf20Sopenharmony_ci	scsi_cmd = kzalloc(MAX_COMMAND_SIZE, GFP_KERNEL);
3468c2ecf20Sopenharmony_ci	if (unlikely(!cmd_buf || !scsi_cmd)) {
3478c2ecf20Sopenharmony_ci		rc = -ENOMEM;
3488c2ecf20Sopenharmony_ci		goto out;
3498c2ecf20Sopenharmony_ci	}
3508c2ecf20Sopenharmony_ci
3518c2ecf20Sopenharmony_ci	scsi_cmd[0] = SERVICE_ACTION_IN_16;	/* read cap(16) */
3528c2ecf20Sopenharmony_ci	scsi_cmd[1] = SAI_READ_CAPACITY_16;	/* service action */
3538c2ecf20Sopenharmony_ci	put_unaligned_be32(CMD_BUFSIZE, &scsi_cmd[10]);
3548c2ecf20Sopenharmony_ci
3558c2ecf20Sopenharmony_ci	dev_dbg(dev, "%s: %ssending cmd(%02x)\n", __func__,
3568c2ecf20Sopenharmony_ci		retry_cnt ? "re" : "", scsi_cmd[0]);
3578c2ecf20Sopenharmony_ci
3588c2ecf20Sopenharmony_ci	/* Drop the ioctl read semahpore across lengthy call */
3598c2ecf20Sopenharmony_ci	up_read(&cfg->ioctl_rwsem);
3608c2ecf20Sopenharmony_ci	result = scsi_execute(sdev, scsi_cmd, DMA_FROM_DEVICE, cmd_buf,
3618c2ecf20Sopenharmony_ci			      CMD_BUFSIZE, NULL, &sshdr, to, CMD_RETRIES,
3628c2ecf20Sopenharmony_ci			      0, 0, NULL);
3638c2ecf20Sopenharmony_ci	down_read(&cfg->ioctl_rwsem);
3648c2ecf20Sopenharmony_ci	rc = check_state(cfg);
3658c2ecf20Sopenharmony_ci	if (rc) {
3668c2ecf20Sopenharmony_ci		dev_err(dev, "%s: Failed state result=%08x\n",
3678c2ecf20Sopenharmony_ci			__func__, result);
3688c2ecf20Sopenharmony_ci		rc = -ENODEV;
3698c2ecf20Sopenharmony_ci		goto out;
3708c2ecf20Sopenharmony_ci	}
3718c2ecf20Sopenharmony_ci
3728c2ecf20Sopenharmony_ci	if (driver_byte(result) == DRIVER_SENSE) {
3738c2ecf20Sopenharmony_ci		result &= ~(0xFF<<24); /* DRIVER_SENSE is not an error */
3748c2ecf20Sopenharmony_ci		if (result & SAM_STAT_CHECK_CONDITION) {
3758c2ecf20Sopenharmony_ci			switch (sshdr.sense_key) {
3768c2ecf20Sopenharmony_ci			case NO_SENSE:
3778c2ecf20Sopenharmony_ci			case RECOVERED_ERROR:
3788c2ecf20Sopenharmony_ci			case NOT_READY:
3798c2ecf20Sopenharmony_ci				result &= ~SAM_STAT_CHECK_CONDITION;
3808c2ecf20Sopenharmony_ci				break;
3818c2ecf20Sopenharmony_ci			case UNIT_ATTENTION:
3828c2ecf20Sopenharmony_ci				switch (sshdr.asc) {
3838c2ecf20Sopenharmony_ci				case 0x29: /* Power on Reset or Device Reset */
3848c2ecf20Sopenharmony_ci					fallthrough;
3858c2ecf20Sopenharmony_ci				case 0x2A: /* Device capacity changed */
3868c2ecf20Sopenharmony_ci				case 0x3F: /* Report LUNs changed */
3878c2ecf20Sopenharmony_ci					/* Retry the command once more */
3888c2ecf20Sopenharmony_ci					if (retry_cnt++ < 1) {
3898c2ecf20Sopenharmony_ci						kfree(cmd_buf);
3908c2ecf20Sopenharmony_ci						kfree(scsi_cmd);
3918c2ecf20Sopenharmony_ci						goto retry;
3928c2ecf20Sopenharmony_ci					}
3938c2ecf20Sopenharmony_ci				}
3948c2ecf20Sopenharmony_ci				break;
3958c2ecf20Sopenharmony_ci			default:
3968c2ecf20Sopenharmony_ci				break;
3978c2ecf20Sopenharmony_ci			}
3988c2ecf20Sopenharmony_ci		}
3998c2ecf20Sopenharmony_ci	}
4008c2ecf20Sopenharmony_ci
4018c2ecf20Sopenharmony_ci	if (result) {
4028c2ecf20Sopenharmony_ci		dev_err(dev, "%s: command failed, result=%08x\n",
4038c2ecf20Sopenharmony_ci			__func__, result);
4048c2ecf20Sopenharmony_ci		rc = -EIO;
4058c2ecf20Sopenharmony_ci		goto out;
4068c2ecf20Sopenharmony_ci	}
4078c2ecf20Sopenharmony_ci
4088c2ecf20Sopenharmony_ci	/*
4098c2ecf20Sopenharmony_ci	 * Read cap was successful, grab values from the buffer;
4108c2ecf20Sopenharmony_ci	 * note that we don't need to worry about unaligned access
4118c2ecf20Sopenharmony_ci	 * as the buffer is allocated on an aligned boundary.
4128c2ecf20Sopenharmony_ci	 */
4138c2ecf20Sopenharmony_ci	mutex_lock(&gli->mutex);
4148c2ecf20Sopenharmony_ci	gli->max_lba = be64_to_cpu(*((__be64 *)&cmd_buf[0]));
4158c2ecf20Sopenharmony_ci	gli->blk_len = be32_to_cpu(*((__be32 *)&cmd_buf[8]));
4168c2ecf20Sopenharmony_ci	mutex_unlock(&gli->mutex);
4178c2ecf20Sopenharmony_ci
4188c2ecf20Sopenharmony_ciout:
4198c2ecf20Sopenharmony_ci	kfree(cmd_buf);
4208c2ecf20Sopenharmony_ci	kfree(scsi_cmd);
4218c2ecf20Sopenharmony_ci
4228c2ecf20Sopenharmony_ci	dev_dbg(dev, "%s: maxlba=%lld blklen=%d rc=%d\n",
4238c2ecf20Sopenharmony_ci		__func__, gli->max_lba, gli->blk_len, rc);
4248c2ecf20Sopenharmony_ci	return rc;
4258c2ecf20Sopenharmony_ci}
4268c2ecf20Sopenharmony_ci
4278c2ecf20Sopenharmony_ci/**
4288c2ecf20Sopenharmony_ci * get_rhte() - obtains validated resource handle table entry reference
4298c2ecf20Sopenharmony_ci * @ctxi:	Context owning the resource handle.
4308c2ecf20Sopenharmony_ci * @rhndl:	Resource handle associated with entry.
4318c2ecf20Sopenharmony_ci * @lli:	LUN associated with request.
4328c2ecf20Sopenharmony_ci *
4338c2ecf20Sopenharmony_ci * Return: Validated RHTE on success, NULL on failure
4348c2ecf20Sopenharmony_ci */
4358c2ecf20Sopenharmony_cistruct sisl_rht_entry *get_rhte(struct ctx_info *ctxi, res_hndl_t rhndl,
4368c2ecf20Sopenharmony_ci				struct llun_info *lli)
4378c2ecf20Sopenharmony_ci{
4388c2ecf20Sopenharmony_ci	struct cxlflash_cfg *cfg = ctxi->cfg;
4398c2ecf20Sopenharmony_ci	struct device *dev = &cfg->dev->dev;
4408c2ecf20Sopenharmony_ci	struct sisl_rht_entry *rhte = NULL;
4418c2ecf20Sopenharmony_ci
4428c2ecf20Sopenharmony_ci	if (unlikely(!ctxi->rht_start)) {
4438c2ecf20Sopenharmony_ci		dev_dbg(dev, "%s: Context does not have allocated RHT\n",
4448c2ecf20Sopenharmony_ci			 __func__);
4458c2ecf20Sopenharmony_ci		goto out;
4468c2ecf20Sopenharmony_ci	}
4478c2ecf20Sopenharmony_ci
4488c2ecf20Sopenharmony_ci	if (unlikely(rhndl >= MAX_RHT_PER_CONTEXT)) {
4498c2ecf20Sopenharmony_ci		dev_dbg(dev, "%s: Bad resource handle rhndl=%d\n",
4508c2ecf20Sopenharmony_ci			__func__, rhndl);
4518c2ecf20Sopenharmony_ci		goto out;
4528c2ecf20Sopenharmony_ci	}
4538c2ecf20Sopenharmony_ci
4548c2ecf20Sopenharmony_ci	if (unlikely(ctxi->rht_lun[rhndl] != lli)) {
4558c2ecf20Sopenharmony_ci		dev_dbg(dev, "%s: Bad resource handle LUN rhndl=%d\n",
4568c2ecf20Sopenharmony_ci			__func__, rhndl);
4578c2ecf20Sopenharmony_ci		goto out;
4588c2ecf20Sopenharmony_ci	}
4598c2ecf20Sopenharmony_ci
4608c2ecf20Sopenharmony_ci	rhte = &ctxi->rht_start[rhndl];
4618c2ecf20Sopenharmony_ci	if (unlikely(rhte->nmask == 0)) {
4628c2ecf20Sopenharmony_ci		dev_dbg(dev, "%s: Unopened resource handle rhndl=%d\n",
4638c2ecf20Sopenharmony_ci			__func__, rhndl);
4648c2ecf20Sopenharmony_ci		rhte = NULL;
4658c2ecf20Sopenharmony_ci		goto out;
4668c2ecf20Sopenharmony_ci	}
4678c2ecf20Sopenharmony_ci
4688c2ecf20Sopenharmony_ciout:
4698c2ecf20Sopenharmony_ci	return rhte;
4708c2ecf20Sopenharmony_ci}
4718c2ecf20Sopenharmony_ci
4728c2ecf20Sopenharmony_ci/**
4738c2ecf20Sopenharmony_ci * rhte_checkout() - obtains free/empty resource handle table entry
4748c2ecf20Sopenharmony_ci * @ctxi:	Context owning the resource handle.
4758c2ecf20Sopenharmony_ci * @lli:	LUN associated with request.
4768c2ecf20Sopenharmony_ci *
4778c2ecf20Sopenharmony_ci * Return: Free RHTE on success, NULL on failure
4788c2ecf20Sopenharmony_ci */
4798c2ecf20Sopenharmony_cistruct sisl_rht_entry *rhte_checkout(struct ctx_info *ctxi,
4808c2ecf20Sopenharmony_ci				     struct llun_info *lli)
4818c2ecf20Sopenharmony_ci{
4828c2ecf20Sopenharmony_ci	struct cxlflash_cfg *cfg = ctxi->cfg;
4838c2ecf20Sopenharmony_ci	struct device *dev = &cfg->dev->dev;
4848c2ecf20Sopenharmony_ci	struct sisl_rht_entry *rhte = NULL;
4858c2ecf20Sopenharmony_ci	int i;
4868c2ecf20Sopenharmony_ci
4878c2ecf20Sopenharmony_ci	/* Find a free RHT entry */
4888c2ecf20Sopenharmony_ci	for (i = 0; i < MAX_RHT_PER_CONTEXT; i++)
4898c2ecf20Sopenharmony_ci		if (ctxi->rht_start[i].nmask == 0) {
4908c2ecf20Sopenharmony_ci			rhte = &ctxi->rht_start[i];
4918c2ecf20Sopenharmony_ci			ctxi->rht_out++;
4928c2ecf20Sopenharmony_ci			break;
4938c2ecf20Sopenharmony_ci		}
4948c2ecf20Sopenharmony_ci
4958c2ecf20Sopenharmony_ci	if (likely(rhte))
4968c2ecf20Sopenharmony_ci		ctxi->rht_lun[i] = lli;
4978c2ecf20Sopenharmony_ci
4988c2ecf20Sopenharmony_ci	dev_dbg(dev, "%s: returning rhte=%p index=%d\n", __func__, rhte, i);
4998c2ecf20Sopenharmony_ci	return rhte;
5008c2ecf20Sopenharmony_ci}
5018c2ecf20Sopenharmony_ci
5028c2ecf20Sopenharmony_ci/**
5038c2ecf20Sopenharmony_ci * rhte_checkin() - releases a resource handle table entry
5048c2ecf20Sopenharmony_ci * @ctxi:	Context owning the resource handle.
5058c2ecf20Sopenharmony_ci * @rhte:	RHTE to release.
5068c2ecf20Sopenharmony_ci */
5078c2ecf20Sopenharmony_civoid rhte_checkin(struct ctx_info *ctxi,
5088c2ecf20Sopenharmony_ci		  struct sisl_rht_entry *rhte)
5098c2ecf20Sopenharmony_ci{
5108c2ecf20Sopenharmony_ci	u32 rsrc_handle = rhte - ctxi->rht_start;
5118c2ecf20Sopenharmony_ci
5128c2ecf20Sopenharmony_ci	rhte->nmask = 0;
5138c2ecf20Sopenharmony_ci	rhte->fp = 0;
5148c2ecf20Sopenharmony_ci	ctxi->rht_out--;
5158c2ecf20Sopenharmony_ci	ctxi->rht_lun[rsrc_handle] = NULL;
5168c2ecf20Sopenharmony_ci	ctxi->rht_needs_ws[rsrc_handle] = false;
5178c2ecf20Sopenharmony_ci}
5188c2ecf20Sopenharmony_ci
5198c2ecf20Sopenharmony_ci/**
5208c2ecf20Sopenharmony_ci * rhte_format1() - populates a RHTE for format 1
5218c2ecf20Sopenharmony_ci * @rhte:	RHTE to populate.
5228c2ecf20Sopenharmony_ci * @lun_id:	LUN ID of LUN associated with RHTE.
5238c2ecf20Sopenharmony_ci * @perm:	Desired permissions for RHTE.
5248c2ecf20Sopenharmony_ci * @port_sel:	Port selection mask
5258c2ecf20Sopenharmony_ci */
5268c2ecf20Sopenharmony_cistatic void rht_format1(struct sisl_rht_entry *rhte, u64 lun_id, u32 perm,
5278c2ecf20Sopenharmony_ci			u32 port_sel)
5288c2ecf20Sopenharmony_ci{
5298c2ecf20Sopenharmony_ci	/*
5308c2ecf20Sopenharmony_ci	 * Populate the Format 1 RHT entry for direct access (physical
5318c2ecf20Sopenharmony_ci	 * LUN) using the synchronization sequence defined in the
5328c2ecf20Sopenharmony_ci	 * SISLite specification.
5338c2ecf20Sopenharmony_ci	 */
5348c2ecf20Sopenharmony_ci	struct sisl_rht_entry_f1 dummy = { 0 };
5358c2ecf20Sopenharmony_ci	struct sisl_rht_entry_f1 *rhte_f1 = (struct sisl_rht_entry_f1 *)rhte;
5368c2ecf20Sopenharmony_ci
5378c2ecf20Sopenharmony_ci	memset(rhte_f1, 0, sizeof(*rhte_f1));
5388c2ecf20Sopenharmony_ci	rhte_f1->fp = SISL_RHT_FP(1U, 0);
5398c2ecf20Sopenharmony_ci	dma_wmb(); /* Make setting of format bit visible */
5408c2ecf20Sopenharmony_ci
5418c2ecf20Sopenharmony_ci	rhte_f1->lun_id = lun_id;
5428c2ecf20Sopenharmony_ci	dma_wmb(); /* Make setting of LUN id visible */
5438c2ecf20Sopenharmony_ci
5448c2ecf20Sopenharmony_ci	/*
5458c2ecf20Sopenharmony_ci	 * Use a dummy RHT Format 1 entry to build the second dword
5468c2ecf20Sopenharmony_ci	 * of the entry that must be populated in a single write when
5478c2ecf20Sopenharmony_ci	 * enabled (valid bit set to TRUE).
5488c2ecf20Sopenharmony_ci	 */
5498c2ecf20Sopenharmony_ci	dummy.valid = 0x80;
5508c2ecf20Sopenharmony_ci	dummy.fp = SISL_RHT_FP(1U, perm);
5518c2ecf20Sopenharmony_ci	dummy.port_sel = port_sel;
5528c2ecf20Sopenharmony_ci	rhte_f1->dw = dummy.dw;
5538c2ecf20Sopenharmony_ci
5548c2ecf20Sopenharmony_ci	dma_wmb(); /* Make remaining RHT entry fields visible */
5558c2ecf20Sopenharmony_ci}
5568c2ecf20Sopenharmony_ci
5578c2ecf20Sopenharmony_ci/**
5588c2ecf20Sopenharmony_ci * cxlflash_lun_attach() - attaches a user to a LUN and manages the LUN's mode
5598c2ecf20Sopenharmony_ci * @gli:	LUN to attach.
5608c2ecf20Sopenharmony_ci * @mode:	Desired mode of the LUN.
5618c2ecf20Sopenharmony_ci * @locked:	Mutex status on current thread.
5628c2ecf20Sopenharmony_ci *
5638c2ecf20Sopenharmony_ci * Return: 0 on success, -errno on failure
5648c2ecf20Sopenharmony_ci */
5658c2ecf20Sopenharmony_ciint cxlflash_lun_attach(struct glun_info *gli, enum lun_mode mode, bool locked)
5668c2ecf20Sopenharmony_ci{
5678c2ecf20Sopenharmony_ci	int rc = 0;
5688c2ecf20Sopenharmony_ci
5698c2ecf20Sopenharmony_ci	if (!locked)
5708c2ecf20Sopenharmony_ci		mutex_lock(&gli->mutex);
5718c2ecf20Sopenharmony_ci
5728c2ecf20Sopenharmony_ci	if (gli->mode == MODE_NONE)
5738c2ecf20Sopenharmony_ci		gli->mode = mode;
5748c2ecf20Sopenharmony_ci	else if (gli->mode != mode) {
5758c2ecf20Sopenharmony_ci		pr_debug("%s: gli_mode=%d requested_mode=%d\n",
5768c2ecf20Sopenharmony_ci			 __func__, gli->mode, mode);
5778c2ecf20Sopenharmony_ci		rc = -EINVAL;
5788c2ecf20Sopenharmony_ci		goto out;
5798c2ecf20Sopenharmony_ci	}
5808c2ecf20Sopenharmony_ci
5818c2ecf20Sopenharmony_ci	gli->users++;
5828c2ecf20Sopenharmony_ci	WARN_ON(gli->users <= 0);
5838c2ecf20Sopenharmony_ciout:
5848c2ecf20Sopenharmony_ci	pr_debug("%s: Returning rc=%d gli->mode=%u gli->users=%u\n",
5858c2ecf20Sopenharmony_ci		 __func__, rc, gli->mode, gli->users);
5868c2ecf20Sopenharmony_ci	if (!locked)
5878c2ecf20Sopenharmony_ci		mutex_unlock(&gli->mutex);
5888c2ecf20Sopenharmony_ci	return rc;
5898c2ecf20Sopenharmony_ci}
5908c2ecf20Sopenharmony_ci
5918c2ecf20Sopenharmony_ci/**
5928c2ecf20Sopenharmony_ci * cxlflash_lun_detach() - detaches a user from a LUN and resets the LUN's mode
5938c2ecf20Sopenharmony_ci * @gli:	LUN to detach.
5948c2ecf20Sopenharmony_ci *
5958c2ecf20Sopenharmony_ci * When resetting the mode, terminate block allocation resources as they
5968c2ecf20Sopenharmony_ci * are no longer required (service is safe to call even when block allocation
5978c2ecf20Sopenharmony_ci * resources were not present - such as when transitioning from physical mode).
5988c2ecf20Sopenharmony_ci * These resources will be reallocated when needed (subsequent transition to
5998c2ecf20Sopenharmony_ci * virtual mode).
6008c2ecf20Sopenharmony_ci */
6018c2ecf20Sopenharmony_civoid cxlflash_lun_detach(struct glun_info *gli)
6028c2ecf20Sopenharmony_ci{
6038c2ecf20Sopenharmony_ci	mutex_lock(&gli->mutex);
6048c2ecf20Sopenharmony_ci	WARN_ON(gli->mode == MODE_NONE);
6058c2ecf20Sopenharmony_ci	if (--gli->users == 0) {
6068c2ecf20Sopenharmony_ci		gli->mode = MODE_NONE;
6078c2ecf20Sopenharmony_ci		cxlflash_ba_terminate(&gli->blka.ba_lun);
6088c2ecf20Sopenharmony_ci	}
6098c2ecf20Sopenharmony_ci	pr_debug("%s: gli->users=%u\n", __func__, gli->users);
6108c2ecf20Sopenharmony_ci	WARN_ON(gli->users < 0);
6118c2ecf20Sopenharmony_ci	mutex_unlock(&gli->mutex);
6128c2ecf20Sopenharmony_ci}
6138c2ecf20Sopenharmony_ci
6148c2ecf20Sopenharmony_ci/**
6158c2ecf20Sopenharmony_ci * _cxlflash_disk_release() - releases the specified resource entry
6168c2ecf20Sopenharmony_ci * @sdev:	SCSI device associated with LUN.
6178c2ecf20Sopenharmony_ci * @ctxi:	Context owning resources.
6188c2ecf20Sopenharmony_ci * @release:	Release ioctl data structure.
6198c2ecf20Sopenharmony_ci *
6208c2ecf20Sopenharmony_ci * For LUNs in virtual mode, the virtual LUN associated with the specified
6218c2ecf20Sopenharmony_ci * resource handle is resized to 0 prior to releasing the RHTE. Note that the
6228c2ecf20Sopenharmony_ci * AFU sync should _not_ be performed when the context is sitting on the error
6238c2ecf20Sopenharmony_ci * recovery list. A context on the error recovery list is not known to the AFU
6248c2ecf20Sopenharmony_ci * due to reset. When the context is recovered, it will be reattached and made
6258c2ecf20Sopenharmony_ci * known again to the AFU.
6268c2ecf20Sopenharmony_ci *
6278c2ecf20Sopenharmony_ci * Return: 0 on success, -errno on failure
6288c2ecf20Sopenharmony_ci */
6298c2ecf20Sopenharmony_ciint _cxlflash_disk_release(struct scsi_device *sdev,
6308c2ecf20Sopenharmony_ci			   struct ctx_info *ctxi,
6318c2ecf20Sopenharmony_ci			   struct dk_cxlflash_release *release)
6328c2ecf20Sopenharmony_ci{
6338c2ecf20Sopenharmony_ci	struct cxlflash_cfg *cfg = shost_priv(sdev->host);
6348c2ecf20Sopenharmony_ci	struct device *dev = &cfg->dev->dev;
6358c2ecf20Sopenharmony_ci	struct llun_info *lli = sdev->hostdata;
6368c2ecf20Sopenharmony_ci	struct glun_info *gli = lli->parent;
6378c2ecf20Sopenharmony_ci	struct afu *afu = cfg->afu;
6388c2ecf20Sopenharmony_ci	bool put_ctx = false;
6398c2ecf20Sopenharmony_ci
6408c2ecf20Sopenharmony_ci	struct dk_cxlflash_resize size;
6418c2ecf20Sopenharmony_ci	res_hndl_t rhndl = release->rsrc_handle;
6428c2ecf20Sopenharmony_ci
6438c2ecf20Sopenharmony_ci	int rc = 0;
6448c2ecf20Sopenharmony_ci	int rcr = 0;
6458c2ecf20Sopenharmony_ci	u64 ctxid = DECODE_CTXID(release->context_id),
6468c2ecf20Sopenharmony_ci	    rctxid = release->context_id;
6478c2ecf20Sopenharmony_ci
6488c2ecf20Sopenharmony_ci	struct sisl_rht_entry *rhte;
6498c2ecf20Sopenharmony_ci	struct sisl_rht_entry_f1 *rhte_f1;
6508c2ecf20Sopenharmony_ci
6518c2ecf20Sopenharmony_ci	dev_dbg(dev, "%s: ctxid=%llu rhndl=%llu gli->mode=%u gli->users=%u\n",
6528c2ecf20Sopenharmony_ci		__func__, ctxid, release->rsrc_handle, gli->mode, gli->users);
6538c2ecf20Sopenharmony_ci
6548c2ecf20Sopenharmony_ci	if (!ctxi) {
6558c2ecf20Sopenharmony_ci		ctxi = get_context(cfg, rctxid, lli, CTX_CTRL_ERR_FALLBACK);
6568c2ecf20Sopenharmony_ci		if (unlikely(!ctxi)) {
6578c2ecf20Sopenharmony_ci			dev_dbg(dev, "%s: Bad context ctxid=%llu\n",
6588c2ecf20Sopenharmony_ci				__func__, ctxid);
6598c2ecf20Sopenharmony_ci			rc = -EINVAL;
6608c2ecf20Sopenharmony_ci			goto out;
6618c2ecf20Sopenharmony_ci		}
6628c2ecf20Sopenharmony_ci
6638c2ecf20Sopenharmony_ci		put_ctx = true;
6648c2ecf20Sopenharmony_ci	}
6658c2ecf20Sopenharmony_ci
6668c2ecf20Sopenharmony_ci	rhte = get_rhte(ctxi, rhndl, lli);
6678c2ecf20Sopenharmony_ci	if (unlikely(!rhte)) {
6688c2ecf20Sopenharmony_ci		dev_dbg(dev, "%s: Bad resource handle rhndl=%d\n",
6698c2ecf20Sopenharmony_ci			__func__, rhndl);
6708c2ecf20Sopenharmony_ci		rc = -EINVAL;
6718c2ecf20Sopenharmony_ci		goto out;
6728c2ecf20Sopenharmony_ci	}
6738c2ecf20Sopenharmony_ci
6748c2ecf20Sopenharmony_ci	/*
6758c2ecf20Sopenharmony_ci	 * Resize to 0 for virtual LUNS by setting the size
6768c2ecf20Sopenharmony_ci	 * to 0. This will clear LXT_START and LXT_CNT fields
6778c2ecf20Sopenharmony_ci	 * in the RHT entry and properly sync with the AFU.
6788c2ecf20Sopenharmony_ci	 *
6798c2ecf20Sopenharmony_ci	 * Afterwards we clear the remaining fields.
6808c2ecf20Sopenharmony_ci	 */
6818c2ecf20Sopenharmony_ci	switch (gli->mode) {
6828c2ecf20Sopenharmony_ci	case MODE_VIRTUAL:
6838c2ecf20Sopenharmony_ci		marshal_rele_to_resize(release, &size);
6848c2ecf20Sopenharmony_ci		size.req_size = 0;
6858c2ecf20Sopenharmony_ci		rc = _cxlflash_vlun_resize(sdev, ctxi, &size);
6868c2ecf20Sopenharmony_ci		if (rc) {
6878c2ecf20Sopenharmony_ci			dev_dbg(dev, "%s: resize failed rc %d\n", __func__, rc);
6888c2ecf20Sopenharmony_ci			goto out;
6898c2ecf20Sopenharmony_ci		}
6908c2ecf20Sopenharmony_ci
6918c2ecf20Sopenharmony_ci		break;
6928c2ecf20Sopenharmony_ci	case MODE_PHYSICAL:
6938c2ecf20Sopenharmony_ci		/*
6948c2ecf20Sopenharmony_ci		 * Clear the Format 1 RHT entry for direct access
6958c2ecf20Sopenharmony_ci		 * (physical LUN) using the synchronization sequence
6968c2ecf20Sopenharmony_ci		 * defined in the SISLite specification.
6978c2ecf20Sopenharmony_ci		 */
6988c2ecf20Sopenharmony_ci		rhte_f1 = (struct sisl_rht_entry_f1 *)rhte;
6998c2ecf20Sopenharmony_ci
7008c2ecf20Sopenharmony_ci		rhte_f1->valid = 0;
7018c2ecf20Sopenharmony_ci		dma_wmb(); /* Make revocation of RHT entry visible */
7028c2ecf20Sopenharmony_ci
7038c2ecf20Sopenharmony_ci		rhte_f1->lun_id = 0;
7048c2ecf20Sopenharmony_ci		dma_wmb(); /* Make clearing of LUN id visible */
7058c2ecf20Sopenharmony_ci
7068c2ecf20Sopenharmony_ci		rhte_f1->dw = 0;
7078c2ecf20Sopenharmony_ci		dma_wmb(); /* Make RHT entry bottom-half clearing visible */
7088c2ecf20Sopenharmony_ci
7098c2ecf20Sopenharmony_ci		if (!ctxi->err_recovery_active) {
7108c2ecf20Sopenharmony_ci			rcr = cxlflash_afu_sync(afu, ctxid, rhndl, AFU_HW_SYNC);
7118c2ecf20Sopenharmony_ci			if (unlikely(rcr))
7128c2ecf20Sopenharmony_ci				dev_dbg(dev, "%s: AFU sync failed rc=%d\n",
7138c2ecf20Sopenharmony_ci					__func__, rcr);
7148c2ecf20Sopenharmony_ci		}
7158c2ecf20Sopenharmony_ci		break;
7168c2ecf20Sopenharmony_ci	default:
7178c2ecf20Sopenharmony_ci		WARN(1, "Unsupported LUN mode!");
7188c2ecf20Sopenharmony_ci		goto out;
7198c2ecf20Sopenharmony_ci	}
7208c2ecf20Sopenharmony_ci
7218c2ecf20Sopenharmony_ci	rhte_checkin(ctxi, rhte);
7228c2ecf20Sopenharmony_ci	cxlflash_lun_detach(gli);
7238c2ecf20Sopenharmony_ci
7248c2ecf20Sopenharmony_ciout:
7258c2ecf20Sopenharmony_ci	if (put_ctx)
7268c2ecf20Sopenharmony_ci		put_context(ctxi);
7278c2ecf20Sopenharmony_ci	dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
7288c2ecf20Sopenharmony_ci	return rc;
7298c2ecf20Sopenharmony_ci}
7308c2ecf20Sopenharmony_ci
7318c2ecf20Sopenharmony_ciint cxlflash_disk_release(struct scsi_device *sdev,
7328c2ecf20Sopenharmony_ci			  struct dk_cxlflash_release *release)
7338c2ecf20Sopenharmony_ci{
7348c2ecf20Sopenharmony_ci	return _cxlflash_disk_release(sdev, NULL, release);
7358c2ecf20Sopenharmony_ci}
7368c2ecf20Sopenharmony_ci
7378c2ecf20Sopenharmony_ci/**
7388c2ecf20Sopenharmony_ci * destroy_context() - releases a context
7398c2ecf20Sopenharmony_ci * @cfg:	Internal structure associated with the host.
7408c2ecf20Sopenharmony_ci * @ctxi:	Context to release.
7418c2ecf20Sopenharmony_ci *
7428c2ecf20Sopenharmony_ci * This routine is safe to be called with a a non-initialized context.
7438c2ecf20Sopenharmony_ci * Also note that the routine conditionally checks for the existence
7448c2ecf20Sopenharmony_ci * of the context control map before clearing the RHT registers and
7458c2ecf20Sopenharmony_ci * context capabilities because it is possible to destroy a context
7468c2ecf20Sopenharmony_ci * while the context is in the error state (previous mapping was
7478c2ecf20Sopenharmony_ci * removed [so there is no need to worry about clearing] and context
7488c2ecf20Sopenharmony_ci * is waiting for a new mapping).
7498c2ecf20Sopenharmony_ci */
7508c2ecf20Sopenharmony_cistatic void destroy_context(struct cxlflash_cfg *cfg,
7518c2ecf20Sopenharmony_ci			    struct ctx_info *ctxi)
7528c2ecf20Sopenharmony_ci{
7538c2ecf20Sopenharmony_ci	struct afu *afu = cfg->afu;
7548c2ecf20Sopenharmony_ci
7558c2ecf20Sopenharmony_ci	if (ctxi->initialized) {
7568c2ecf20Sopenharmony_ci		WARN_ON(!list_empty(&ctxi->luns));
7578c2ecf20Sopenharmony_ci
7588c2ecf20Sopenharmony_ci		/* Clear RHT registers and drop all capabilities for context */
7598c2ecf20Sopenharmony_ci		if (afu->afu_map && ctxi->ctrl_map) {
7608c2ecf20Sopenharmony_ci			writeq_be(0, &ctxi->ctrl_map->rht_start);
7618c2ecf20Sopenharmony_ci			writeq_be(0, &ctxi->ctrl_map->rht_cnt_id);
7628c2ecf20Sopenharmony_ci			writeq_be(0, &ctxi->ctrl_map->ctx_cap);
7638c2ecf20Sopenharmony_ci		}
7648c2ecf20Sopenharmony_ci	}
7658c2ecf20Sopenharmony_ci
7668c2ecf20Sopenharmony_ci	/* Free memory associated with context */
7678c2ecf20Sopenharmony_ci	free_page((ulong)ctxi->rht_start);
7688c2ecf20Sopenharmony_ci	kfree(ctxi->rht_needs_ws);
7698c2ecf20Sopenharmony_ci	kfree(ctxi->rht_lun);
7708c2ecf20Sopenharmony_ci	kfree(ctxi);
7718c2ecf20Sopenharmony_ci}
7728c2ecf20Sopenharmony_ci
7738c2ecf20Sopenharmony_ci/**
7748c2ecf20Sopenharmony_ci * create_context() - allocates and initializes a context
7758c2ecf20Sopenharmony_ci * @cfg:	Internal structure associated with the host.
7768c2ecf20Sopenharmony_ci *
7778c2ecf20Sopenharmony_ci * Return: Allocated context on success, NULL on failure
7788c2ecf20Sopenharmony_ci */
7798c2ecf20Sopenharmony_cistatic struct ctx_info *create_context(struct cxlflash_cfg *cfg)
7808c2ecf20Sopenharmony_ci{
7818c2ecf20Sopenharmony_ci	struct device *dev = &cfg->dev->dev;
7828c2ecf20Sopenharmony_ci	struct ctx_info *ctxi = NULL;
7838c2ecf20Sopenharmony_ci	struct llun_info **lli = NULL;
7848c2ecf20Sopenharmony_ci	u8 *ws = NULL;
7858c2ecf20Sopenharmony_ci	struct sisl_rht_entry *rhte;
7868c2ecf20Sopenharmony_ci
7878c2ecf20Sopenharmony_ci	ctxi = kzalloc(sizeof(*ctxi), GFP_KERNEL);
7888c2ecf20Sopenharmony_ci	lli = kzalloc((MAX_RHT_PER_CONTEXT * sizeof(*lli)), GFP_KERNEL);
7898c2ecf20Sopenharmony_ci	ws = kzalloc((MAX_RHT_PER_CONTEXT * sizeof(*ws)), GFP_KERNEL);
7908c2ecf20Sopenharmony_ci	if (unlikely(!ctxi || !lli || !ws)) {
7918c2ecf20Sopenharmony_ci		dev_err(dev, "%s: Unable to allocate context\n", __func__);
7928c2ecf20Sopenharmony_ci		goto err;
7938c2ecf20Sopenharmony_ci	}
7948c2ecf20Sopenharmony_ci
7958c2ecf20Sopenharmony_ci	rhte = (struct sisl_rht_entry *)get_zeroed_page(GFP_KERNEL);
7968c2ecf20Sopenharmony_ci	if (unlikely(!rhte)) {
7978c2ecf20Sopenharmony_ci		dev_err(dev, "%s: Unable to allocate RHT\n", __func__);
7988c2ecf20Sopenharmony_ci		goto err;
7998c2ecf20Sopenharmony_ci	}
8008c2ecf20Sopenharmony_ci
8018c2ecf20Sopenharmony_ci	ctxi->rht_lun = lli;
8028c2ecf20Sopenharmony_ci	ctxi->rht_needs_ws = ws;
8038c2ecf20Sopenharmony_ci	ctxi->rht_start = rhte;
8048c2ecf20Sopenharmony_ciout:
8058c2ecf20Sopenharmony_ci	return ctxi;
8068c2ecf20Sopenharmony_ci
8078c2ecf20Sopenharmony_cierr:
8088c2ecf20Sopenharmony_ci	kfree(ws);
8098c2ecf20Sopenharmony_ci	kfree(lli);
8108c2ecf20Sopenharmony_ci	kfree(ctxi);
8118c2ecf20Sopenharmony_ci	ctxi = NULL;
8128c2ecf20Sopenharmony_ci	goto out;
8138c2ecf20Sopenharmony_ci}
8148c2ecf20Sopenharmony_ci
8158c2ecf20Sopenharmony_ci/**
8168c2ecf20Sopenharmony_ci * init_context() - initializes a previously allocated context
8178c2ecf20Sopenharmony_ci * @ctxi:	Previously allocated context
8188c2ecf20Sopenharmony_ci * @cfg:	Internal structure associated with the host.
8198c2ecf20Sopenharmony_ci * @ctx:	Previously obtained context cookie.
8208c2ecf20Sopenharmony_ci * @ctxid:	Previously obtained process element associated with CXL context.
8218c2ecf20Sopenharmony_ci * @file:	Previously obtained file associated with CXL context.
8228c2ecf20Sopenharmony_ci * @perms:	User-specified permissions.
8238c2ecf20Sopenharmony_ci * @irqs:	User-specified number of interrupts.
8248c2ecf20Sopenharmony_ci */
8258c2ecf20Sopenharmony_cistatic void init_context(struct ctx_info *ctxi, struct cxlflash_cfg *cfg,
8268c2ecf20Sopenharmony_ci			 void *ctx, int ctxid, struct file *file, u32 perms,
8278c2ecf20Sopenharmony_ci			 u64 irqs)
8288c2ecf20Sopenharmony_ci{
8298c2ecf20Sopenharmony_ci	struct afu *afu = cfg->afu;
8308c2ecf20Sopenharmony_ci
8318c2ecf20Sopenharmony_ci	ctxi->rht_perms = perms;
8328c2ecf20Sopenharmony_ci	ctxi->ctrl_map = &afu->afu_map->ctrls[ctxid].ctrl;
8338c2ecf20Sopenharmony_ci	ctxi->ctxid = ENCODE_CTXID(ctxi, ctxid);
8348c2ecf20Sopenharmony_ci	ctxi->irqs = irqs;
8358c2ecf20Sopenharmony_ci	ctxi->pid = task_tgid_nr(current); /* tgid = pid */
8368c2ecf20Sopenharmony_ci	ctxi->ctx = ctx;
8378c2ecf20Sopenharmony_ci	ctxi->cfg = cfg;
8388c2ecf20Sopenharmony_ci	ctxi->file = file;
8398c2ecf20Sopenharmony_ci	ctxi->initialized = true;
8408c2ecf20Sopenharmony_ci	mutex_init(&ctxi->mutex);
8418c2ecf20Sopenharmony_ci	kref_init(&ctxi->kref);
8428c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&ctxi->luns);
8438c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&ctxi->list); /* initialize for list_empty() */
8448c2ecf20Sopenharmony_ci}
8458c2ecf20Sopenharmony_ci
8468c2ecf20Sopenharmony_ci/**
8478c2ecf20Sopenharmony_ci * remove_context() - context kref release handler
8488c2ecf20Sopenharmony_ci * @kref:	Kernel reference associated with context to be removed.
8498c2ecf20Sopenharmony_ci *
8508c2ecf20Sopenharmony_ci * When a context no longer has any references it can safely be removed
8518c2ecf20Sopenharmony_ci * from global access and destroyed. Note that it is assumed the thread
8528c2ecf20Sopenharmony_ci * relinquishing access to the context holds its mutex.
8538c2ecf20Sopenharmony_ci */
8548c2ecf20Sopenharmony_cistatic void remove_context(struct kref *kref)
8558c2ecf20Sopenharmony_ci{
8568c2ecf20Sopenharmony_ci	struct ctx_info *ctxi = container_of(kref, struct ctx_info, kref);
8578c2ecf20Sopenharmony_ci	struct cxlflash_cfg *cfg = ctxi->cfg;
8588c2ecf20Sopenharmony_ci	u64 ctxid = DECODE_CTXID(ctxi->ctxid);
8598c2ecf20Sopenharmony_ci
8608c2ecf20Sopenharmony_ci	/* Remove context from table/error list */
8618c2ecf20Sopenharmony_ci	WARN_ON(!mutex_is_locked(&ctxi->mutex));
8628c2ecf20Sopenharmony_ci	ctxi->unavail = true;
8638c2ecf20Sopenharmony_ci	mutex_unlock(&ctxi->mutex);
8648c2ecf20Sopenharmony_ci	mutex_lock(&cfg->ctx_tbl_list_mutex);
8658c2ecf20Sopenharmony_ci	mutex_lock(&ctxi->mutex);
8668c2ecf20Sopenharmony_ci
8678c2ecf20Sopenharmony_ci	if (!list_empty(&ctxi->list))
8688c2ecf20Sopenharmony_ci		list_del(&ctxi->list);
8698c2ecf20Sopenharmony_ci	cfg->ctx_tbl[ctxid] = NULL;
8708c2ecf20Sopenharmony_ci	mutex_unlock(&cfg->ctx_tbl_list_mutex);
8718c2ecf20Sopenharmony_ci	mutex_unlock(&ctxi->mutex);
8728c2ecf20Sopenharmony_ci
8738c2ecf20Sopenharmony_ci	/* Context now completely uncoupled/unreachable */
8748c2ecf20Sopenharmony_ci	destroy_context(cfg, ctxi);
8758c2ecf20Sopenharmony_ci}
8768c2ecf20Sopenharmony_ci
8778c2ecf20Sopenharmony_ci/**
8788c2ecf20Sopenharmony_ci * _cxlflash_disk_detach() - detaches a LUN from a context
8798c2ecf20Sopenharmony_ci * @sdev:	SCSI device associated with LUN.
8808c2ecf20Sopenharmony_ci * @ctxi:	Context owning resources.
8818c2ecf20Sopenharmony_ci * @detach:	Detach ioctl data structure.
8828c2ecf20Sopenharmony_ci *
8838c2ecf20Sopenharmony_ci * As part of the detach, all per-context resources associated with the LUN
8848c2ecf20Sopenharmony_ci * are cleaned up. When detaching the last LUN for a context, the context
8858c2ecf20Sopenharmony_ci * itself is cleaned up and released.
8868c2ecf20Sopenharmony_ci *
8878c2ecf20Sopenharmony_ci * Return: 0 on success, -errno on failure
8888c2ecf20Sopenharmony_ci */
8898c2ecf20Sopenharmony_cistatic int _cxlflash_disk_detach(struct scsi_device *sdev,
8908c2ecf20Sopenharmony_ci				 struct ctx_info *ctxi,
8918c2ecf20Sopenharmony_ci				 struct dk_cxlflash_detach *detach)
8928c2ecf20Sopenharmony_ci{
8938c2ecf20Sopenharmony_ci	struct cxlflash_cfg *cfg = shost_priv(sdev->host);
8948c2ecf20Sopenharmony_ci	struct device *dev = &cfg->dev->dev;
8958c2ecf20Sopenharmony_ci	struct llun_info *lli = sdev->hostdata;
8968c2ecf20Sopenharmony_ci	struct lun_access *lun_access, *t;
8978c2ecf20Sopenharmony_ci	struct dk_cxlflash_release rel;
8988c2ecf20Sopenharmony_ci	bool put_ctx = false;
8998c2ecf20Sopenharmony_ci
9008c2ecf20Sopenharmony_ci	int i;
9018c2ecf20Sopenharmony_ci	int rc = 0;
9028c2ecf20Sopenharmony_ci	u64 ctxid = DECODE_CTXID(detach->context_id),
9038c2ecf20Sopenharmony_ci	    rctxid = detach->context_id;
9048c2ecf20Sopenharmony_ci
9058c2ecf20Sopenharmony_ci	dev_dbg(dev, "%s: ctxid=%llu\n", __func__, ctxid);
9068c2ecf20Sopenharmony_ci
9078c2ecf20Sopenharmony_ci	if (!ctxi) {
9088c2ecf20Sopenharmony_ci		ctxi = get_context(cfg, rctxid, lli, CTX_CTRL_ERR_FALLBACK);
9098c2ecf20Sopenharmony_ci		if (unlikely(!ctxi)) {
9108c2ecf20Sopenharmony_ci			dev_dbg(dev, "%s: Bad context ctxid=%llu\n",
9118c2ecf20Sopenharmony_ci				__func__, ctxid);
9128c2ecf20Sopenharmony_ci			rc = -EINVAL;
9138c2ecf20Sopenharmony_ci			goto out;
9148c2ecf20Sopenharmony_ci		}
9158c2ecf20Sopenharmony_ci
9168c2ecf20Sopenharmony_ci		put_ctx = true;
9178c2ecf20Sopenharmony_ci	}
9188c2ecf20Sopenharmony_ci
9198c2ecf20Sopenharmony_ci	/* Cleanup outstanding resources tied to this LUN */
9208c2ecf20Sopenharmony_ci	if (ctxi->rht_out) {
9218c2ecf20Sopenharmony_ci		marshal_det_to_rele(detach, &rel);
9228c2ecf20Sopenharmony_ci		for (i = 0; i < MAX_RHT_PER_CONTEXT; i++) {
9238c2ecf20Sopenharmony_ci			if (ctxi->rht_lun[i] == lli) {
9248c2ecf20Sopenharmony_ci				rel.rsrc_handle = i;
9258c2ecf20Sopenharmony_ci				_cxlflash_disk_release(sdev, ctxi, &rel);
9268c2ecf20Sopenharmony_ci			}
9278c2ecf20Sopenharmony_ci
9288c2ecf20Sopenharmony_ci			/* No need to loop further if we're done */
9298c2ecf20Sopenharmony_ci			if (ctxi->rht_out == 0)
9308c2ecf20Sopenharmony_ci				break;
9318c2ecf20Sopenharmony_ci		}
9328c2ecf20Sopenharmony_ci	}
9338c2ecf20Sopenharmony_ci
9348c2ecf20Sopenharmony_ci	/* Take our LUN out of context, free the node */
9358c2ecf20Sopenharmony_ci	list_for_each_entry_safe(lun_access, t, &ctxi->luns, list)
9368c2ecf20Sopenharmony_ci		if (lun_access->lli == lli) {
9378c2ecf20Sopenharmony_ci			list_del(&lun_access->list);
9388c2ecf20Sopenharmony_ci			kfree(lun_access);
9398c2ecf20Sopenharmony_ci			lun_access = NULL;
9408c2ecf20Sopenharmony_ci			break;
9418c2ecf20Sopenharmony_ci		}
9428c2ecf20Sopenharmony_ci
9438c2ecf20Sopenharmony_ci	/*
9448c2ecf20Sopenharmony_ci	 * Release the context reference and the sdev reference that
9458c2ecf20Sopenharmony_ci	 * bound this LUN to the context.
9468c2ecf20Sopenharmony_ci	 */
9478c2ecf20Sopenharmony_ci	if (kref_put(&ctxi->kref, remove_context))
9488c2ecf20Sopenharmony_ci		put_ctx = false;
9498c2ecf20Sopenharmony_ci	scsi_device_put(sdev);
9508c2ecf20Sopenharmony_ciout:
9518c2ecf20Sopenharmony_ci	if (put_ctx)
9528c2ecf20Sopenharmony_ci		put_context(ctxi);
9538c2ecf20Sopenharmony_ci	dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
9548c2ecf20Sopenharmony_ci	return rc;
9558c2ecf20Sopenharmony_ci}
9568c2ecf20Sopenharmony_ci
9578c2ecf20Sopenharmony_cistatic int cxlflash_disk_detach(struct scsi_device *sdev,
9588c2ecf20Sopenharmony_ci				struct dk_cxlflash_detach *detach)
9598c2ecf20Sopenharmony_ci{
9608c2ecf20Sopenharmony_ci	return _cxlflash_disk_detach(sdev, NULL, detach);
9618c2ecf20Sopenharmony_ci}
9628c2ecf20Sopenharmony_ci
9638c2ecf20Sopenharmony_ci/**
9648c2ecf20Sopenharmony_ci * cxlflash_cxl_release() - release handler for adapter file descriptor
9658c2ecf20Sopenharmony_ci * @inode:	File-system inode associated with fd.
9668c2ecf20Sopenharmony_ci * @file:	File installed with adapter file descriptor.
9678c2ecf20Sopenharmony_ci *
9688c2ecf20Sopenharmony_ci * This routine is the release handler for the fops registered with
9698c2ecf20Sopenharmony_ci * the CXL services on an initial attach for a context. It is called
9708c2ecf20Sopenharmony_ci * when a close (explicity by the user or as part of a process tear
9718c2ecf20Sopenharmony_ci * down) is performed on the adapter file descriptor returned to the
9728c2ecf20Sopenharmony_ci * user. The user should be aware that explicitly performing a close
9738c2ecf20Sopenharmony_ci * considered catastrophic and subsequent usage of the superpipe API
9748c2ecf20Sopenharmony_ci * with previously saved off tokens will fail.
9758c2ecf20Sopenharmony_ci *
9768c2ecf20Sopenharmony_ci * This routine derives the context reference and calls detach for
9778c2ecf20Sopenharmony_ci * each LUN associated with the context.The final detach operation
9788c2ecf20Sopenharmony_ci * causes the context itself to be freed. With exception to when the
9798c2ecf20Sopenharmony_ci * CXL process element (context id) lookup fails (a case that should
9808c2ecf20Sopenharmony_ci * theoretically never occur), every call into this routine results
9818c2ecf20Sopenharmony_ci * in a complete freeing of a context.
9828c2ecf20Sopenharmony_ci *
9838c2ecf20Sopenharmony_ci * Detaching the LUN is typically an ioctl() operation and the underlying
9848c2ecf20Sopenharmony_ci * code assumes that ioctl_rwsem has been acquired as a reader. To support
9858c2ecf20Sopenharmony_ci * that design point, the semaphore is acquired and released around detach.
9868c2ecf20Sopenharmony_ci *
9878c2ecf20Sopenharmony_ci * Return: 0 on success
9888c2ecf20Sopenharmony_ci */
9898c2ecf20Sopenharmony_cistatic int cxlflash_cxl_release(struct inode *inode, struct file *file)
9908c2ecf20Sopenharmony_ci{
9918c2ecf20Sopenharmony_ci	struct cxlflash_cfg *cfg = container_of(file->f_op, struct cxlflash_cfg,
9928c2ecf20Sopenharmony_ci						cxl_fops);
9938c2ecf20Sopenharmony_ci	void *ctx = cfg->ops->fops_get_context(file);
9948c2ecf20Sopenharmony_ci	struct device *dev = &cfg->dev->dev;
9958c2ecf20Sopenharmony_ci	struct ctx_info *ctxi = NULL;
9968c2ecf20Sopenharmony_ci	struct dk_cxlflash_detach detach = { { 0 }, 0 };
9978c2ecf20Sopenharmony_ci	struct lun_access *lun_access, *t;
9988c2ecf20Sopenharmony_ci	enum ctx_ctrl ctrl = CTX_CTRL_ERR_FALLBACK | CTX_CTRL_FILE;
9998c2ecf20Sopenharmony_ci	int ctxid;
10008c2ecf20Sopenharmony_ci
10018c2ecf20Sopenharmony_ci	ctxid = cfg->ops->process_element(ctx);
10028c2ecf20Sopenharmony_ci	if (unlikely(ctxid < 0)) {
10038c2ecf20Sopenharmony_ci		dev_err(dev, "%s: Context %p was closed ctxid=%d\n",
10048c2ecf20Sopenharmony_ci			__func__, ctx, ctxid);
10058c2ecf20Sopenharmony_ci		goto out;
10068c2ecf20Sopenharmony_ci	}
10078c2ecf20Sopenharmony_ci
10088c2ecf20Sopenharmony_ci	ctxi = get_context(cfg, ctxid, file, ctrl);
10098c2ecf20Sopenharmony_ci	if (unlikely(!ctxi)) {
10108c2ecf20Sopenharmony_ci		ctxi = get_context(cfg, ctxid, file, ctrl | CTX_CTRL_CLONE);
10118c2ecf20Sopenharmony_ci		if (!ctxi) {
10128c2ecf20Sopenharmony_ci			dev_dbg(dev, "%s: ctxid=%d already free\n",
10138c2ecf20Sopenharmony_ci				__func__, ctxid);
10148c2ecf20Sopenharmony_ci			goto out_release;
10158c2ecf20Sopenharmony_ci		}
10168c2ecf20Sopenharmony_ci
10178c2ecf20Sopenharmony_ci		dev_dbg(dev, "%s: Another process owns ctxid=%d\n",
10188c2ecf20Sopenharmony_ci			__func__, ctxid);
10198c2ecf20Sopenharmony_ci		put_context(ctxi);
10208c2ecf20Sopenharmony_ci		goto out;
10218c2ecf20Sopenharmony_ci	}
10228c2ecf20Sopenharmony_ci
10238c2ecf20Sopenharmony_ci	dev_dbg(dev, "%s: close for ctxid=%d\n", __func__, ctxid);
10248c2ecf20Sopenharmony_ci
10258c2ecf20Sopenharmony_ci	down_read(&cfg->ioctl_rwsem);
10268c2ecf20Sopenharmony_ci	detach.context_id = ctxi->ctxid;
10278c2ecf20Sopenharmony_ci	list_for_each_entry_safe(lun_access, t, &ctxi->luns, list)
10288c2ecf20Sopenharmony_ci		_cxlflash_disk_detach(lun_access->sdev, ctxi, &detach);
10298c2ecf20Sopenharmony_ci	up_read(&cfg->ioctl_rwsem);
10308c2ecf20Sopenharmony_ciout_release:
10318c2ecf20Sopenharmony_ci	cfg->ops->fd_release(inode, file);
10328c2ecf20Sopenharmony_ciout:
10338c2ecf20Sopenharmony_ci	dev_dbg(dev, "%s: returning\n", __func__);
10348c2ecf20Sopenharmony_ci	return 0;
10358c2ecf20Sopenharmony_ci}
10368c2ecf20Sopenharmony_ci
10378c2ecf20Sopenharmony_ci/**
10388c2ecf20Sopenharmony_ci * unmap_context() - clears a previously established mapping
10398c2ecf20Sopenharmony_ci * @ctxi:	Context owning the mapping.
10408c2ecf20Sopenharmony_ci *
10418c2ecf20Sopenharmony_ci * This routine is used to switch between the error notification page
10428c2ecf20Sopenharmony_ci * (dummy page of all 1's) and the real mapping (established by the CXL
10438c2ecf20Sopenharmony_ci * fault handler).
10448c2ecf20Sopenharmony_ci */
10458c2ecf20Sopenharmony_cistatic void unmap_context(struct ctx_info *ctxi)
10468c2ecf20Sopenharmony_ci{
10478c2ecf20Sopenharmony_ci	unmap_mapping_range(ctxi->file->f_mapping, 0, 0, 1);
10488c2ecf20Sopenharmony_ci}
10498c2ecf20Sopenharmony_ci
10508c2ecf20Sopenharmony_ci/**
10518c2ecf20Sopenharmony_ci * get_err_page() - obtains and allocates the error notification page
10528c2ecf20Sopenharmony_ci * @cfg:	Internal structure associated with the host.
10538c2ecf20Sopenharmony_ci *
10548c2ecf20Sopenharmony_ci * Return: error notification page on success, NULL on failure
10558c2ecf20Sopenharmony_ci */
10568c2ecf20Sopenharmony_cistatic struct page *get_err_page(struct cxlflash_cfg *cfg)
10578c2ecf20Sopenharmony_ci{
10588c2ecf20Sopenharmony_ci	struct page *err_page = global.err_page;
10598c2ecf20Sopenharmony_ci	struct device *dev = &cfg->dev->dev;
10608c2ecf20Sopenharmony_ci
10618c2ecf20Sopenharmony_ci	if (unlikely(!err_page)) {
10628c2ecf20Sopenharmony_ci		err_page = alloc_page(GFP_KERNEL);
10638c2ecf20Sopenharmony_ci		if (unlikely(!err_page)) {
10648c2ecf20Sopenharmony_ci			dev_err(dev, "%s: Unable to allocate err_page\n",
10658c2ecf20Sopenharmony_ci				__func__);
10668c2ecf20Sopenharmony_ci			goto out;
10678c2ecf20Sopenharmony_ci		}
10688c2ecf20Sopenharmony_ci
10698c2ecf20Sopenharmony_ci		memset(page_address(err_page), -1, PAGE_SIZE);
10708c2ecf20Sopenharmony_ci
10718c2ecf20Sopenharmony_ci		/* Serialize update w/ other threads to avoid a leak */
10728c2ecf20Sopenharmony_ci		mutex_lock(&global.mutex);
10738c2ecf20Sopenharmony_ci		if (likely(!global.err_page))
10748c2ecf20Sopenharmony_ci			global.err_page = err_page;
10758c2ecf20Sopenharmony_ci		else {
10768c2ecf20Sopenharmony_ci			__free_page(err_page);
10778c2ecf20Sopenharmony_ci			err_page = global.err_page;
10788c2ecf20Sopenharmony_ci		}
10798c2ecf20Sopenharmony_ci		mutex_unlock(&global.mutex);
10808c2ecf20Sopenharmony_ci	}
10818c2ecf20Sopenharmony_ci
10828c2ecf20Sopenharmony_ciout:
10838c2ecf20Sopenharmony_ci	dev_dbg(dev, "%s: returning err_page=%p\n", __func__, err_page);
10848c2ecf20Sopenharmony_ci	return err_page;
10858c2ecf20Sopenharmony_ci}
10868c2ecf20Sopenharmony_ci
10878c2ecf20Sopenharmony_ci/**
10888c2ecf20Sopenharmony_ci * cxlflash_mmap_fault() - mmap fault handler for adapter file descriptor
10898c2ecf20Sopenharmony_ci * @vmf:	VM fault associated with current fault.
10908c2ecf20Sopenharmony_ci *
10918c2ecf20Sopenharmony_ci * To support error notification via MMIO, faults are 'caught' by this routine
10928c2ecf20Sopenharmony_ci * that was inserted before passing back the adapter file descriptor on attach.
10938c2ecf20Sopenharmony_ci * When a fault occurs, this routine evaluates if error recovery is active and
10948c2ecf20Sopenharmony_ci * if so, installs the error page to 'notify' the user about the error state.
10958c2ecf20Sopenharmony_ci * During normal operation, the fault is simply handled by the original fault
10968c2ecf20Sopenharmony_ci * handler that was installed by CXL services as part of initializing the
10978c2ecf20Sopenharmony_ci * adapter file descriptor. The VMA's page protection bits are toggled to
10988c2ecf20Sopenharmony_ci * indicate cached/not-cached depending on the memory backing the fault.
10998c2ecf20Sopenharmony_ci *
11008c2ecf20Sopenharmony_ci * Return: 0 on success, VM_FAULT_SIGBUS on failure
11018c2ecf20Sopenharmony_ci */
11028c2ecf20Sopenharmony_cistatic vm_fault_t cxlflash_mmap_fault(struct vm_fault *vmf)
11038c2ecf20Sopenharmony_ci{
11048c2ecf20Sopenharmony_ci	struct vm_area_struct *vma = vmf->vma;
11058c2ecf20Sopenharmony_ci	struct file *file = vma->vm_file;
11068c2ecf20Sopenharmony_ci	struct cxlflash_cfg *cfg = container_of(file->f_op, struct cxlflash_cfg,
11078c2ecf20Sopenharmony_ci						cxl_fops);
11088c2ecf20Sopenharmony_ci	void *ctx = cfg->ops->fops_get_context(file);
11098c2ecf20Sopenharmony_ci	struct device *dev = &cfg->dev->dev;
11108c2ecf20Sopenharmony_ci	struct ctx_info *ctxi = NULL;
11118c2ecf20Sopenharmony_ci	struct page *err_page = NULL;
11128c2ecf20Sopenharmony_ci	enum ctx_ctrl ctrl = CTX_CTRL_ERR_FALLBACK | CTX_CTRL_FILE;
11138c2ecf20Sopenharmony_ci	vm_fault_t rc = 0;
11148c2ecf20Sopenharmony_ci	int ctxid;
11158c2ecf20Sopenharmony_ci
11168c2ecf20Sopenharmony_ci	ctxid = cfg->ops->process_element(ctx);
11178c2ecf20Sopenharmony_ci	if (unlikely(ctxid < 0)) {
11188c2ecf20Sopenharmony_ci		dev_err(dev, "%s: Context %p was closed ctxid=%d\n",
11198c2ecf20Sopenharmony_ci			__func__, ctx, ctxid);
11208c2ecf20Sopenharmony_ci		goto err;
11218c2ecf20Sopenharmony_ci	}
11228c2ecf20Sopenharmony_ci
11238c2ecf20Sopenharmony_ci	ctxi = get_context(cfg, ctxid, file, ctrl);
11248c2ecf20Sopenharmony_ci	if (unlikely(!ctxi)) {
11258c2ecf20Sopenharmony_ci		dev_dbg(dev, "%s: Bad context ctxid=%d\n", __func__, ctxid);
11268c2ecf20Sopenharmony_ci		goto err;
11278c2ecf20Sopenharmony_ci	}
11288c2ecf20Sopenharmony_ci
11298c2ecf20Sopenharmony_ci	dev_dbg(dev, "%s: fault for context %d\n", __func__, ctxid);
11308c2ecf20Sopenharmony_ci
11318c2ecf20Sopenharmony_ci	if (likely(!ctxi->err_recovery_active)) {
11328c2ecf20Sopenharmony_ci		vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
11338c2ecf20Sopenharmony_ci		rc = ctxi->cxl_mmap_vmops->fault(vmf);
11348c2ecf20Sopenharmony_ci	} else {
11358c2ecf20Sopenharmony_ci		dev_dbg(dev, "%s: err recovery active, use err_page\n",
11368c2ecf20Sopenharmony_ci			__func__);
11378c2ecf20Sopenharmony_ci
11388c2ecf20Sopenharmony_ci		err_page = get_err_page(cfg);
11398c2ecf20Sopenharmony_ci		if (unlikely(!err_page)) {
11408c2ecf20Sopenharmony_ci			dev_err(dev, "%s: Could not get err_page\n", __func__);
11418c2ecf20Sopenharmony_ci			rc = VM_FAULT_RETRY;
11428c2ecf20Sopenharmony_ci			goto out;
11438c2ecf20Sopenharmony_ci		}
11448c2ecf20Sopenharmony_ci
11458c2ecf20Sopenharmony_ci		get_page(err_page);
11468c2ecf20Sopenharmony_ci		vmf->page = err_page;
11478c2ecf20Sopenharmony_ci		vma->vm_page_prot = pgprot_cached(vma->vm_page_prot);
11488c2ecf20Sopenharmony_ci	}
11498c2ecf20Sopenharmony_ci
11508c2ecf20Sopenharmony_ciout:
11518c2ecf20Sopenharmony_ci	if (likely(ctxi))
11528c2ecf20Sopenharmony_ci		put_context(ctxi);
11538c2ecf20Sopenharmony_ci	dev_dbg(dev, "%s: returning rc=%x\n", __func__, rc);
11548c2ecf20Sopenharmony_ci	return rc;
11558c2ecf20Sopenharmony_ci
11568c2ecf20Sopenharmony_cierr:
11578c2ecf20Sopenharmony_ci	rc = VM_FAULT_SIGBUS;
11588c2ecf20Sopenharmony_ci	goto out;
11598c2ecf20Sopenharmony_ci}
11608c2ecf20Sopenharmony_ci
11618c2ecf20Sopenharmony_ci/*
11628c2ecf20Sopenharmony_ci * Local MMAP vmops to 'catch' faults
11638c2ecf20Sopenharmony_ci */
11648c2ecf20Sopenharmony_cistatic const struct vm_operations_struct cxlflash_mmap_vmops = {
11658c2ecf20Sopenharmony_ci	.fault = cxlflash_mmap_fault,
11668c2ecf20Sopenharmony_ci};
11678c2ecf20Sopenharmony_ci
11688c2ecf20Sopenharmony_ci/**
11698c2ecf20Sopenharmony_ci * cxlflash_cxl_mmap() - mmap handler for adapter file descriptor
11708c2ecf20Sopenharmony_ci * @file:	File installed with adapter file descriptor.
11718c2ecf20Sopenharmony_ci * @vma:	VM area associated with mapping.
11728c2ecf20Sopenharmony_ci *
11738c2ecf20Sopenharmony_ci * Installs local mmap vmops to 'catch' faults for error notification support.
11748c2ecf20Sopenharmony_ci *
11758c2ecf20Sopenharmony_ci * Return: 0 on success, -errno on failure
11768c2ecf20Sopenharmony_ci */
11778c2ecf20Sopenharmony_cistatic int cxlflash_cxl_mmap(struct file *file, struct vm_area_struct *vma)
11788c2ecf20Sopenharmony_ci{
11798c2ecf20Sopenharmony_ci	struct cxlflash_cfg *cfg = container_of(file->f_op, struct cxlflash_cfg,
11808c2ecf20Sopenharmony_ci						cxl_fops);
11818c2ecf20Sopenharmony_ci	void *ctx = cfg->ops->fops_get_context(file);
11828c2ecf20Sopenharmony_ci	struct device *dev = &cfg->dev->dev;
11838c2ecf20Sopenharmony_ci	struct ctx_info *ctxi = NULL;
11848c2ecf20Sopenharmony_ci	enum ctx_ctrl ctrl = CTX_CTRL_ERR_FALLBACK | CTX_CTRL_FILE;
11858c2ecf20Sopenharmony_ci	int ctxid;
11868c2ecf20Sopenharmony_ci	int rc = 0;
11878c2ecf20Sopenharmony_ci
11888c2ecf20Sopenharmony_ci	ctxid = cfg->ops->process_element(ctx);
11898c2ecf20Sopenharmony_ci	if (unlikely(ctxid < 0)) {
11908c2ecf20Sopenharmony_ci		dev_err(dev, "%s: Context %p was closed ctxid=%d\n",
11918c2ecf20Sopenharmony_ci			__func__, ctx, ctxid);
11928c2ecf20Sopenharmony_ci		rc = -EIO;
11938c2ecf20Sopenharmony_ci		goto out;
11948c2ecf20Sopenharmony_ci	}
11958c2ecf20Sopenharmony_ci
11968c2ecf20Sopenharmony_ci	ctxi = get_context(cfg, ctxid, file, ctrl);
11978c2ecf20Sopenharmony_ci	if (unlikely(!ctxi)) {
11988c2ecf20Sopenharmony_ci		dev_dbg(dev, "%s: Bad context ctxid=%d\n", __func__, ctxid);
11998c2ecf20Sopenharmony_ci		rc = -EIO;
12008c2ecf20Sopenharmony_ci		goto out;
12018c2ecf20Sopenharmony_ci	}
12028c2ecf20Sopenharmony_ci
12038c2ecf20Sopenharmony_ci	dev_dbg(dev, "%s: mmap for context %d\n", __func__, ctxid);
12048c2ecf20Sopenharmony_ci
12058c2ecf20Sopenharmony_ci	rc = cfg->ops->fd_mmap(file, vma);
12068c2ecf20Sopenharmony_ci	if (likely(!rc)) {
12078c2ecf20Sopenharmony_ci		/* Insert ourself in the mmap fault handler path */
12088c2ecf20Sopenharmony_ci		ctxi->cxl_mmap_vmops = vma->vm_ops;
12098c2ecf20Sopenharmony_ci		vma->vm_ops = &cxlflash_mmap_vmops;
12108c2ecf20Sopenharmony_ci	}
12118c2ecf20Sopenharmony_ci
12128c2ecf20Sopenharmony_ciout:
12138c2ecf20Sopenharmony_ci	if (likely(ctxi))
12148c2ecf20Sopenharmony_ci		put_context(ctxi);
12158c2ecf20Sopenharmony_ci	return rc;
12168c2ecf20Sopenharmony_ci}
12178c2ecf20Sopenharmony_ci
12188c2ecf20Sopenharmony_ciconst struct file_operations cxlflash_cxl_fops = {
12198c2ecf20Sopenharmony_ci	.owner = THIS_MODULE,
12208c2ecf20Sopenharmony_ci	.mmap = cxlflash_cxl_mmap,
12218c2ecf20Sopenharmony_ci	.release = cxlflash_cxl_release,
12228c2ecf20Sopenharmony_ci};
12238c2ecf20Sopenharmony_ci
12248c2ecf20Sopenharmony_ci/**
12258c2ecf20Sopenharmony_ci * cxlflash_mark_contexts_error() - move contexts to error state and list
12268c2ecf20Sopenharmony_ci * @cfg:	Internal structure associated with the host.
12278c2ecf20Sopenharmony_ci *
12288c2ecf20Sopenharmony_ci * A context is only moved over to the error list when there are no outstanding
12298c2ecf20Sopenharmony_ci * references to it. This ensures that a running operation has completed.
12308c2ecf20Sopenharmony_ci *
12318c2ecf20Sopenharmony_ci * Return: 0 on success, -errno on failure
12328c2ecf20Sopenharmony_ci */
12338c2ecf20Sopenharmony_ciint cxlflash_mark_contexts_error(struct cxlflash_cfg *cfg)
12348c2ecf20Sopenharmony_ci{
12358c2ecf20Sopenharmony_ci	int i, rc = 0;
12368c2ecf20Sopenharmony_ci	struct ctx_info *ctxi = NULL;
12378c2ecf20Sopenharmony_ci
12388c2ecf20Sopenharmony_ci	mutex_lock(&cfg->ctx_tbl_list_mutex);
12398c2ecf20Sopenharmony_ci
12408c2ecf20Sopenharmony_ci	for (i = 0; i < MAX_CONTEXT; i++) {
12418c2ecf20Sopenharmony_ci		ctxi = cfg->ctx_tbl[i];
12428c2ecf20Sopenharmony_ci		if (ctxi) {
12438c2ecf20Sopenharmony_ci			mutex_lock(&ctxi->mutex);
12448c2ecf20Sopenharmony_ci			cfg->ctx_tbl[i] = NULL;
12458c2ecf20Sopenharmony_ci			list_add(&ctxi->list, &cfg->ctx_err_recovery);
12468c2ecf20Sopenharmony_ci			ctxi->err_recovery_active = true;
12478c2ecf20Sopenharmony_ci			ctxi->ctrl_map = NULL;
12488c2ecf20Sopenharmony_ci			unmap_context(ctxi);
12498c2ecf20Sopenharmony_ci			mutex_unlock(&ctxi->mutex);
12508c2ecf20Sopenharmony_ci		}
12518c2ecf20Sopenharmony_ci	}
12528c2ecf20Sopenharmony_ci
12538c2ecf20Sopenharmony_ci	mutex_unlock(&cfg->ctx_tbl_list_mutex);
12548c2ecf20Sopenharmony_ci	return rc;
12558c2ecf20Sopenharmony_ci}
12568c2ecf20Sopenharmony_ci
12578c2ecf20Sopenharmony_ci/*
12588c2ecf20Sopenharmony_ci * Dummy NULL fops
12598c2ecf20Sopenharmony_ci */
12608c2ecf20Sopenharmony_cistatic const struct file_operations null_fops = {
12618c2ecf20Sopenharmony_ci	.owner = THIS_MODULE,
12628c2ecf20Sopenharmony_ci};
12638c2ecf20Sopenharmony_ci
12648c2ecf20Sopenharmony_ci/**
12658c2ecf20Sopenharmony_ci * check_state() - checks and responds to the current adapter state
12668c2ecf20Sopenharmony_ci * @cfg:	Internal structure associated with the host.
12678c2ecf20Sopenharmony_ci *
12688c2ecf20Sopenharmony_ci * This routine can block and should only be used on process context.
12698c2ecf20Sopenharmony_ci * It assumes that the caller is an ioctl thread and holding the ioctl
12708c2ecf20Sopenharmony_ci * read semaphore. This is temporarily let up across the wait to allow
12718c2ecf20Sopenharmony_ci * for draining actively running ioctls. Also note that when waking up
12728c2ecf20Sopenharmony_ci * from waiting in reset, the state is unknown and must be checked again
12738c2ecf20Sopenharmony_ci * before proceeding.
12748c2ecf20Sopenharmony_ci *
12758c2ecf20Sopenharmony_ci * Return: 0 on success, -errno on failure
12768c2ecf20Sopenharmony_ci */
12778c2ecf20Sopenharmony_ciint check_state(struct cxlflash_cfg *cfg)
12788c2ecf20Sopenharmony_ci{
12798c2ecf20Sopenharmony_ci	struct device *dev = &cfg->dev->dev;
12808c2ecf20Sopenharmony_ci	int rc = 0;
12818c2ecf20Sopenharmony_ci
12828c2ecf20Sopenharmony_ciretry:
12838c2ecf20Sopenharmony_ci	switch (cfg->state) {
12848c2ecf20Sopenharmony_ci	case STATE_RESET:
12858c2ecf20Sopenharmony_ci		dev_dbg(dev, "%s: Reset state, going to wait...\n", __func__);
12868c2ecf20Sopenharmony_ci		up_read(&cfg->ioctl_rwsem);
12878c2ecf20Sopenharmony_ci		rc = wait_event_interruptible(cfg->reset_waitq,
12888c2ecf20Sopenharmony_ci					      cfg->state != STATE_RESET);
12898c2ecf20Sopenharmony_ci		down_read(&cfg->ioctl_rwsem);
12908c2ecf20Sopenharmony_ci		if (unlikely(rc))
12918c2ecf20Sopenharmony_ci			break;
12928c2ecf20Sopenharmony_ci		goto retry;
12938c2ecf20Sopenharmony_ci	case STATE_FAILTERM:
12948c2ecf20Sopenharmony_ci		dev_dbg(dev, "%s: Failed/Terminating\n", __func__);
12958c2ecf20Sopenharmony_ci		rc = -ENODEV;
12968c2ecf20Sopenharmony_ci		break;
12978c2ecf20Sopenharmony_ci	default:
12988c2ecf20Sopenharmony_ci		break;
12998c2ecf20Sopenharmony_ci	}
13008c2ecf20Sopenharmony_ci
13018c2ecf20Sopenharmony_ci	return rc;
13028c2ecf20Sopenharmony_ci}
13038c2ecf20Sopenharmony_ci
13048c2ecf20Sopenharmony_ci/**
13058c2ecf20Sopenharmony_ci * cxlflash_disk_attach() - attach a LUN to a context
13068c2ecf20Sopenharmony_ci * @sdev:	SCSI device associated with LUN.
13078c2ecf20Sopenharmony_ci * @attach:	Attach ioctl data structure.
13088c2ecf20Sopenharmony_ci *
13098c2ecf20Sopenharmony_ci * Creates a context and attaches LUN to it. A LUN can only be attached
13108c2ecf20Sopenharmony_ci * one time to a context (subsequent attaches for the same context/LUN pair
13118c2ecf20Sopenharmony_ci * are not supported). Additional LUNs can be attached to a context by
13128c2ecf20Sopenharmony_ci * specifying the 'reuse' flag defined in the cxlflash_ioctl.h header.
13138c2ecf20Sopenharmony_ci *
13148c2ecf20Sopenharmony_ci * Return: 0 on success, -errno on failure
13158c2ecf20Sopenharmony_ci */
13168c2ecf20Sopenharmony_cistatic int cxlflash_disk_attach(struct scsi_device *sdev,
13178c2ecf20Sopenharmony_ci				struct dk_cxlflash_attach *attach)
13188c2ecf20Sopenharmony_ci{
13198c2ecf20Sopenharmony_ci	struct cxlflash_cfg *cfg = shost_priv(sdev->host);
13208c2ecf20Sopenharmony_ci	struct device *dev = &cfg->dev->dev;
13218c2ecf20Sopenharmony_ci	struct afu *afu = cfg->afu;
13228c2ecf20Sopenharmony_ci	struct llun_info *lli = sdev->hostdata;
13238c2ecf20Sopenharmony_ci	struct glun_info *gli = lli->parent;
13248c2ecf20Sopenharmony_ci	struct ctx_info *ctxi = NULL;
13258c2ecf20Sopenharmony_ci	struct lun_access *lun_access = NULL;
13268c2ecf20Sopenharmony_ci	int rc = 0;
13278c2ecf20Sopenharmony_ci	u32 perms;
13288c2ecf20Sopenharmony_ci	int ctxid = -1;
13298c2ecf20Sopenharmony_ci	u64 irqs = attach->num_interrupts;
13308c2ecf20Sopenharmony_ci	u64 flags = 0UL;
13318c2ecf20Sopenharmony_ci	u64 rctxid = 0UL;
13328c2ecf20Sopenharmony_ci	struct file *file = NULL;
13338c2ecf20Sopenharmony_ci
13348c2ecf20Sopenharmony_ci	void *ctx = NULL;
13358c2ecf20Sopenharmony_ci
13368c2ecf20Sopenharmony_ci	int fd = -1;
13378c2ecf20Sopenharmony_ci
13388c2ecf20Sopenharmony_ci	if (irqs > 4) {
13398c2ecf20Sopenharmony_ci		dev_dbg(dev, "%s: Cannot support this many interrupts %llu\n",
13408c2ecf20Sopenharmony_ci			__func__, irqs);
13418c2ecf20Sopenharmony_ci		rc = -EINVAL;
13428c2ecf20Sopenharmony_ci		goto out;
13438c2ecf20Sopenharmony_ci	}
13448c2ecf20Sopenharmony_ci
13458c2ecf20Sopenharmony_ci	if (gli->max_lba == 0) {
13468c2ecf20Sopenharmony_ci		dev_dbg(dev, "%s: No capacity info for LUN=%016llx\n",
13478c2ecf20Sopenharmony_ci			__func__, lli->lun_id[sdev->channel]);
13488c2ecf20Sopenharmony_ci		rc = read_cap16(sdev, lli);
13498c2ecf20Sopenharmony_ci		if (rc) {
13508c2ecf20Sopenharmony_ci			dev_err(dev, "%s: Invalid device rc=%d\n",
13518c2ecf20Sopenharmony_ci				__func__, rc);
13528c2ecf20Sopenharmony_ci			rc = -ENODEV;
13538c2ecf20Sopenharmony_ci			goto out;
13548c2ecf20Sopenharmony_ci		}
13558c2ecf20Sopenharmony_ci		dev_dbg(dev, "%s: LBA = %016llx\n", __func__, gli->max_lba);
13568c2ecf20Sopenharmony_ci		dev_dbg(dev, "%s: BLK_LEN = %08x\n", __func__, gli->blk_len);
13578c2ecf20Sopenharmony_ci	}
13588c2ecf20Sopenharmony_ci
13598c2ecf20Sopenharmony_ci	if (attach->hdr.flags & DK_CXLFLASH_ATTACH_REUSE_CONTEXT) {
13608c2ecf20Sopenharmony_ci		rctxid = attach->context_id;
13618c2ecf20Sopenharmony_ci		ctxi = get_context(cfg, rctxid, NULL, 0);
13628c2ecf20Sopenharmony_ci		if (!ctxi) {
13638c2ecf20Sopenharmony_ci			dev_dbg(dev, "%s: Bad context rctxid=%016llx\n",
13648c2ecf20Sopenharmony_ci				__func__, rctxid);
13658c2ecf20Sopenharmony_ci			rc = -EINVAL;
13668c2ecf20Sopenharmony_ci			goto out;
13678c2ecf20Sopenharmony_ci		}
13688c2ecf20Sopenharmony_ci
13698c2ecf20Sopenharmony_ci		list_for_each_entry(lun_access, &ctxi->luns, list)
13708c2ecf20Sopenharmony_ci			if (lun_access->lli == lli) {
13718c2ecf20Sopenharmony_ci				dev_dbg(dev, "%s: Already attached\n",
13728c2ecf20Sopenharmony_ci					__func__);
13738c2ecf20Sopenharmony_ci				rc = -EINVAL;
13748c2ecf20Sopenharmony_ci				goto out;
13758c2ecf20Sopenharmony_ci			}
13768c2ecf20Sopenharmony_ci	}
13778c2ecf20Sopenharmony_ci
13788c2ecf20Sopenharmony_ci	rc = scsi_device_get(sdev);
13798c2ecf20Sopenharmony_ci	if (unlikely(rc)) {
13808c2ecf20Sopenharmony_ci		dev_err(dev, "%s: Unable to get sdev reference\n", __func__);
13818c2ecf20Sopenharmony_ci		goto out;
13828c2ecf20Sopenharmony_ci	}
13838c2ecf20Sopenharmony_ci
13848c2ecf20Sopenharmony_ci	lun_access = kzalloc(sizeof(*lun_access), GFP_KERNEL);
13858c2ecf20Sopenharmony_ci	if (unlikely(!lun_access)) {
13868c2ecf20Sopenharmony_ci		dev_err(dev, "%s: Unable to allocate lun_access\n", __func__);
13878c2ecf20Sopenharmony_ci		rc = -ENOMEM;
13888c2ecf20Sopenharmony_ci		goto err;
13898c2ecf20Sopenharmony_ci	}
13908c2ecf20Sopenharmony_ci
13918c2ecf20Sopenharmony_ci	lun_access->lli = lli;
13928c2ecf20Sopenharmony_ci	lun_access->sdev = sdev;
13938c2ecf20Sopenharmony_ci
13948c2ecf20Sopenharmony_ci	/* Non-NULL context indicates reuse (another context reference) */
13958c2ecf20Sopenharmony_ci	if (ctxi) {
13968c2ecf20Sopenharmony_ci		dev_dbg(dev, "%s: Reusing context for LUN rctxid=%016llx\n",
13978c2ecf20Sopenharmony_ci			__func__, rctxid);
13988c2ecf20Sopenharmony_ci		kref_get(&ctxi->kref);
13998c2ecf20Sopenharmony_ci		list_add(&lun_access->list, &ctxi->luns);
14008c2ecf20Sopenharmony_ci		goto out_attach;
14018c2ecf20Sopenharmony_ci	}
14028c2ecf20Sopenharmony_ci
14038c2ecf20Sopenharmony_ci	ctxi = create_context(cfg);
14048c2ecf20Sopenharmony_ci	if (unlikely(!ctxi)) {
14058c2ecf20Sopenharmony_ci		dev_err(dev, "%s: Failed to create context ctxid=%d\n",
14068c2ecf20Sopenharmony_ci			__func__, ctxid);
14078c2ecf20Sopenharmony_ci		rc = -ENOMEM;
14088c2ecf20Sopenharmony_ci		goto err;
14098c2ecf20Sopenharmony_ci	}
14108c2ecf20Sopenharmony_ci
14118c2ecf20Sopenharmony_ci	ctx = cfg->ops->dev_context_init(cfg->dev, cfg->afu_cookie);
14128c2ecf20Sopenharmony_ci	if (IS_ERR_OR_NULL(ctx)) {
14138c2ecf20Sopenharmony_ci		dev_err(dev, "%s: Could not initialize context %p\n",
14148c2ecf20Sopenharmony_ci			__func__, ctx);
14158c2ecf20Sopenharmony_ci		rc = -ENODEV;
14168c2ecf20Sopenharmony_ci		goto err;
14178c2ecf20Sopenharmony_ci	}
14188c2ecf20Sopenharmony_ci
14198c2ecf20Sopenharmony_ci	rc = cfg->ops->start_work(ctx, irqs);
14208c2ecf20Sopenharmony_ci	if (unlikely(rc)) {
14218c2ecf20Sopenharmony_ci		dev_dbg(dev, "%s: Could not start context rc=%d\n",
14228c2ecf20Sopenharmony_ci			__func__, rc);
14238c2ecf20Sopenharmony_ci		goto err;
14248c2ecf20Sopenharmony_ci	}
14258c2ecf20Sopenharmony_ci
14268c2ecf20Sopenharmony_ci	ctxid = cfg->ops->process_element(ctx);
14278c2ecf20Sopenharmony_ci	if (unlikely((ctxid >= MAX_CONTEXT) || (ctxid < 0))) {
14288c2ecf20Sopenharmony_ci		dev_err(dev, "%s: ctxid=%d invalid\n", __func__, ctxid);
14298c2ecf20Sopenharmony_ci		rc = -EPERM;
14308c2ecf20Sopenharmony_ci		goto err;
14318c2ecf20Sopenharmony_ci	}
14328c2ecf20Sopenharmony_ci
14338c2ecf20Sopenharmony_ci	file = cfg->ops->get_fd(ctx, &cfg->cxl_fops, &fd);
14348c2ecf20Sopenharmony_ci	if (unlikely(fd < 0)) {
14358c2ecf20Sopenharmony_ci		rc = -ENODEV;
14368c2ecf20Sopenharmony_ci		dev_err(dev, "%s: Could not get file descriptor\n", __func__);
14378c2ecf20Sopenharmony_ci		goto err;
14388c2ecf20Sopenharmony_ci	}
14398c2ecf20Sopenharmony_ci
14408c2ecf20Sopenharmony_ci	/* Translate read/write O_* flags from fcntl.h to AFU permission bits */
14418c2ecf20Sopenharmony_ci	perms = SISL_RHT_PERM(attach->hdr.flags + 1);
14428c2ecf20Sopenharmony_ci
14438c2ecf20Sopenharmony_ci	/* Context mutex is locked upon return */
14448c2ecf20Sopenharmony_ci	init_context(ctxi, cfg, ctx, ctxid, file, perms, irqs);
14458c2ecf20Sopenharmony_ci
14468c2ecf20Sopenharmony_ci	rc = afu_attach(cfg, ctxi);
14478c2ecf20Sopenharmony_ci	if (unlikely(rc)) {
14488c2ecf20Sopenharmony_ci		dev_err(dev, "%s: Could not attach AFU rc %d\n", __func__, rc);
14498c2ecf20Sopenharmony_ci		goto err;
14508c2ecf20Sopenharmony_ci	}
14518c2ecf20Sopenharmony_ci
14528c2ecf20Sopenharmony_ci	/*
14538c2ecf20Sopenharmony_ci	 * No error paths after this point. Once the fd is installed it's
14548c2ecf20Sopenharmony_ci	 * visible to user space and can't be undone safely on this thread.
14558c2ecf20Sopenharmony_ci	 * There is no need to worry about a deadlock here because no one
14568c2ecf20Sopenharmony_ci	 * knows about us yet; we can be the only one holding our mutex.
14578c2ecf20Sopenharmony_ci	 */
14588c2ecf20Sopenharmony_ci	list_add(&lun_access->list, &ctxi->luns);
14598c2ecf20Sopenharmony_ci	mutex_lock(&cfg->ctx_tbl_list_mutex);
14608c2ecf20Sopenharmony_ci	mutex_lock(&ctxi->mutex);
14618c2ecf20Sopenharmony_ci	cfg->ctx_tbl[ctxid] = ctxi;
14628c2ecf20Sopenharmony_ci	mutex_unlock(&cfg->ctx_tbl_list_mutex);
14638c2ecf20Sopenharmony_ci	fd_install(fd, file);
14648c2ecf20Sopenharmony_ci
14658c2ecf20Sopenharmony_ciout_attach:
14668c2ecf20Sopenharmony_ci	if (fd != -1)
14678c2ecf20Sopenharmony_ci		flags |= DK_CXLFLASH_APP_CLOSE_ADAP_FD;
14688c2ecf20Sopenharmony_ci	if (afu_is_sq_cmd_mode(afu))
14698c2ecf20Sopenharmony_ci		flags |= DK_CXLFLASH_CONTEXT_SQ_CMD_MODE;
14708c2ecf20Sopenharmony_ci
14718c2ecf20Sopenharmony_ci	attach->hdr.return_flags = flags;
14728c2ecf20Sopenharmony_ci	attach->context_id = ctxi->ctxid;
14738c2ecf20Sopenharmony_ci	attach->block_size = gli->blk_len;
14748c2ecf20Sopenharmony_ci	attach->mmio_size = sizeof(afu->afu_map->hosts[0].harea);
14758c2ecf20Sopenharmony_ci	attach->last_lba = gli->max_lba;
14768c2ecf20Sopenharmony_ci	attach->max_xfer = sdev->host->max_sectors * MAX_SECTOR_UNIT;
14778c2ecf20Sopenharmony_ci	attach->max_xfer /= gli->blk_len;
14788c2ecf20Sopenharmony_ci
14798c2ecf20Sopenharmony_ciout:
14808c2ecf20Sopenharmony_ci	attach->adap_fd = fd;
14818c2ecf20Sopenharmony_ci
14828c2ecf20Sopenharmony_ci	if (ctxi)
14838c2ecf20Sopenharmony_ci		put_context(ctxi);
14848c2ecf20Sopenharmony_ci
14858c2ecf20Sopenharmony_ci	dev_dbg(dev, "%s: returning ctxid=%d fd=%d bs=%lld rc=%d llba=%lld\n",
14868c2ecf20Sopenharmony_ci		__func__, ctxid, fd, attach->block_size, rc, attach->last_lba);
14878c2ecf20Sopenharmony_ci	return rc;
14888c2ecf20Sopenharmony_ci
14898c2ecf20Sopenharmony_cierr:
14908c2ecf20Sopenharmony_ci	/* Cleanup CXL context; okay to 'stop' even if it was not started */
14918c2ecf20Sopenharmony_ci	if (!IS_ERR_OR_NULL(ctx)) {
14928c2ecf20Sopenharmony_ci		cfg->ops->stop_context(ctx);
14938c2ecf20Sopenharmony_ci		cfg->ops->release_context(ctx);
14948c2ecf20Sopenharmony_ci		ctx = NULL;
14958c2ecf20Sopenharmony_ci	}
14968c2ecf20Sopenharmony_ci
14978c2ecf20Sopenharmony_ci	/*
14988c2ecf20Sopenharmony_ci	 * Here, we're overriding the fops with a dummy all-NULL fops because
14998c2ecf20Sopenharmony_ci	 * fput() calls the release fop, which will cause us to mistakenly
15008c2ecf20Sopenharmony_ci	 * call into the CXL code. Rather than try to add yet more complexity
15018c2ecf20Sopenharmony_ci	 * to that routine (cxlflash_cxl_release) we should try to fix the
15028c2ecf20Sopenharmony_ci	 * issue here.
15038c2ecf20Sopenharmony_ci	 */
15048c2ecf20Sopenharmony_ci	if (fd > 0) {
15058c2ecf20Sopenharmony_ci		file->f_op = &null_fops;
15068c2ecf20Sopenharmony_ci		fput(file);
15078c2ecf20Sopenharmony_ci		put_unused_fd(fd);
15088c2ecf20Sopenharmony_ci		fd = -1;
15098c2ecf20Sopenharmony_ci		file = NULL;
15108c2ecf20Sopenharmony_ci	}
15118c2ecf20Sopenharmony_ci
15128c2ecf20Sopenharmony_ci	/* Cleanup our context */
15138c2ecf20Sopenharmony_ci	if (ctxi) {
15148c2ecf20Sopenharmony_ci		destroy_context(cfg, ctxi);
15158c2ecf20Sopenharmony_ci		ctxi = NULL;
15168c2ecf20Sopenharmony_ci	}
15178c2ecf20Sopenharmony_ci
15188c2ecf20Sopenharmony_ci	kfree(lun_access);
15198c2ecf20Sopenharmony_ci	scsi_device_put(sdev);
15208c2ecf20Sopenharmony_ci	goto out;
15218c2ecf20Sopenharmony_ci}
15228c2ecf20Sopenharmony_ci
15238c2ecf20Sopenharmony_ci/**
15248c2ecf20Sopenharmony_ci * recover_context() - recovers a context in error
15258c2ecf20Sopenharmony_ci * @cfg:	Internal structure associated with the host.
15268c2ecf20Sopenharmony_ci * @ctxi:	Context to release.
15278c2ecf20Sopenharmony_ci * @adap_fd:	Adapter file descriptor associated with new/recovered context.
15288c2ecf20Sopenharmony_ci *
15298c2ecf20Sopenharmony_ci * Restablishes the state for a context-in-error.
15308c2ecf20Sopenharmony_ci *
15318c2ecf20Sopenharmony_ci * Return: 0 on success, -errno on failure
15328c2ecf20Sopenharmony_ci */
15338c2ecf20Sopenharmony_cistatic int recover_context(struct cxlflash_cfg *cfg,
15348c2ecf20Sopenharmony_ci			   struct ctx_info *ctxi,
15358c2ecf20Sopenharmony_ci			   int *adap_fd)
15368c2ecf20Sopenharmony_ci{
15378c2ecf20Sopenharmony_ci	struct device *dev = &cfg->dev->dev;
15388c2ecf20Sopenharmony_ci	int rc = 0;
15398c2ecf20Sopenharmony_ci	int fd = -1;
15408c2ecf20Sopenharmony_ci	int ctxid = -1;
15418c2ecf20Sopenharmony_ci	struct file *file;
15428c2ecf20Sopenharmony_ci	void *ctx;
15438c2ecf20Sopenharmony_ci	struct afu *afu = cfg->afu;
15448c2ecf20Sopenharmony_ci
15458c2ecf20Sopenharmony_ci	ctx = cfg->ops->dev_context_init(cfg->dev, cfg->afu_cookie);
15468c2ecf20Sopenharmony_ci	if (IS_ERR_OR_NULL(ctx)) {
15478c2ecf20Sopenharmony_ci		dev_err(dev, "%s: Could not initialize context %p\n",
15488c2ecf20Sopenharmony_ci			__func__, ctx);
15498c2ecf20Sopenharmony_ci		rc = -ENODEV;
15508c2ecf20Sopenharmony_ci		goto out;
15518c2ecf20Sopenharmony_ci	}
15528c2ecf20Sopenharmony_ci
15538c2ecf20Sopenharmony_ci	rc = cfg->ops->start_work(ctx, ctxi->irqs);
15548c2ecf20Sopenharmony_ci	if (unlikely(rc)) {
15558c2ecf20Sopenharmony_ci		dev_dbg(dev, "%s: Could not start context rc=%d\n",
15568c2ecf20Sopenharmony_ci			__func__, rc);
15578c2ecf20Sopenharmony_ci		goto err1;
15588c2ecf20Sopenharmony_ci	}
15598c2ecf20Sopenharmony_ci
15608c2ecf20Sopenharmony_ci	ctxid = cfg->ops->process_element(ctx);
15618c2ecf20Sopenharmony_ci	if (unlikely((ctxid >= MAX_CONTEXT) || (ctxid < 0))) {
15628c2ecf20Sopenharmony_ci		dev_err(dev, "%s: ctxid=%d invalid\n", __func__, ctxid);
15638c2ecf20Sopenharmony_ci		rc = -EPERM;
15648c2ecf20Sopenharmony_ci		goto err2;
15658c2ecf20Sopenharmony_ci	}
15668c2ecf20Sopenharmony_ci
15678c2ecf20Sopenharmony_ci	file = cfg->ops->get_fd(ctx, &cfg->cxl_fops, &fd);
15688c2ecf20Sopenharmony_ci	if (unlikely(fd < 0)) {
15698c2ecf20Sopenharmony_ci		rc = -ENODEV;
15708c2ecf20Sopenharmony_ci		dev_err(dev, "%s: Could not get file descriptor\n", __func__);
15718c2ecf20Sopenharmony_ci		goto err2;
15728c2ecf20Sopenharmony_ci	}
15738c2ecf20Sopenharmony_ci
15748c2ecf20Sopenharmony_ci	/* Update with new MMIO area based on updated context id */
15758c2ecf20Sopenharmony_ci	ctxi->ctrl_map = &afu->afu_map->ctrls[ctxid].ctrl;
15768c2ecf20Sopenharmony_ci
15778c2ecf20Sopenharmony_ci	rc = afu_attach(cfg, ctxi);
15788c2ecf20Sopenharmony_ci	if (rc) {
15798c2ecf20Sopenharmony_ci		dev_err(dev, "%s: Could not attach AFU rc %d\n", __func__, rc);
15808c2ecf20Sopenharmony_ci		goto err3;
15818c2ecf20Sopenharmony_ci	}
15828c2ecf20Sopenharmony_ci
15838c2ecf20Sopenharmony_ci	/*
15848c2ecf20Sopenharmony_ci	 * No error paths after this point. Once the fd is installed it's
15858c2ecf20Sopenharmony_ci	 * visible to user space and can't be undone safely on this thread.
15868c2ecf20Sopenharmony_ci	 */
15878c2ecf20Sopenharmony_ci	ctxi->ctxid = ENCODE_CTXID(ctxi, ctxid);
15888c2ecf20Sopenharmony_ci	ctxi->ctx = ctx;
15898c2ecf20Sopenharmony_ci	ctxi->file = file;
15908c2ecf20Sopenharmony_ci
15918c2ecf20Sopenharmony_ci	/*
15928c2ecf20Sopenharmony_ci	 * Put context back in table (note the reinit of the context list);
15938c2ecf20Sopenharmony_ci	 * we must first drop the context's mutex and then acquire it in
15948c2ecf20Sopenharmony_ci	 * order with the table/list mutex to avoid a deadlock - safe to do
15958c2ecf20Sopenharmony_ci	 * here because no one can find us at this moment in time.
15968c2ecf20Sopenharmony_ci	 */
15978c2ecf20Sopenharmony_ci	mutex_unlock(&ctxi->mutex);
15988c2ecf20Sopenharmony_ci	mutex_lock(&cfg->ctx_tbl_list_mutex);
15998c2ecf20Sopenharmony_ci	mutex_lock(&ctxi->mutex);
16008c2ecf20Sopenharmony_ci	list_del_init(&ctxi->list);
16018c2ecf20Sopenharmony_ci	cfg->ctx_tbl[ctxid] = ctxi;
16028c2ecf20Sopenharmony_ci	mutex_unlock(&cfg->ctx_tbl_list_mutex);
16038c2ecf20Sopenharmony_ci	fd_install(fd, file);
16048c2ecf20Sopenharmony_ci	*adap_fd = fd;
16058c2ecf20Sopenharmony_ciout:
16068c2ecf20Sopenharmony_ci	dev_dbg(dev, "%s: returning ctxid=%d fd=%d rc=%d\n",
16078c2ecf20Sopenharmony_ci		__func__, ctxid, fd, rc);
16088c2ecf20Sopenharmony_ci	return rc;
16098c2ecf20Sopenharmony_ci
16108c2ecf20Sopenharmony_cierr3:
16118c2ecf20Sopenharmony_ci	fput(file);
16128c2ecf20Sopenharmony_ci	put_unused_fd(fd);
16138c2ecf20Sopenharmony_cierr2:
16148c2ecf20Sopenharmony_ci	cfg->ops->stop_context(ctx);
16158c2ecf20Sopenharmony_cierr1:
16168c2ecf20Sopenharmony_ci	cfg->ops->release_context(ctx);
16178c2ecf20Sopenharmony_ci	goto out;
16188c2ecf20Sopenharmony_ci}
16198c2ecf20Sopenharmony_ci
16208c2ecf20Sopenharmony_ci/**
16218c2ecf20Sopenharmony_ci * cxlflash_afu_recover() - initiates AFU recovery
16228c2ecf20Sopenharmony_ci * @sdev:	SCSI device associated with LUN.
16238c2ecf20Sopenharmony_ci * @recover:	Recover ioctl data structure.
16248c2ecf20Sopenharmony_ci *
16258c2ecf20Sopenharmony_ci * Only a single recovery is allowed at a time to avoid exhausting CXL
16268c2ecf20Sopenharmony_ci * resources (leading to recovery failure) in the event that we're up
16278c2ecf20Sopenharmony_ci * against the maximum number of contexts limit. For similar reasons,
16288c2ecf20Sopenharmony_ci * a context recovery is retried if there are multiple recoveries taking
16298c2ecf20Sopenharmony_ci * place at the same time and the failure was due to CXL services being
16308c2ecf20Sopenharmony_ci * unable to keep up.
16318c2ecf20Sopenharmony_ci *
16328c2ecf20Sopenharmony_ci * As this routine is called on ioctl context, it holds the ioctl r/w
16338c2ecf20Sopenharmony_ci * semaphore that is used to drain ioctls in recovery scenarios. The
16348c2ecf20Sopenharmony_ci * implementation to achieve the pacing described above (a local mutex)
16358c2ecf20Sopenharmony_ci * requires that the ioctl r/w semaphore be dropped and reacquired to
16368c2ecf20Sopenharmony_ci * avoid a 3-way deadlock when multiple process recoveries operate in
16378c2ecf20Sopenharmony_ci * parallel.
16388c2ecf20Sopenharmony_ci *
16398c2ecf20Sopenharmony_ci * Because a user can detect an error condition before the kernel, it is
16408c2ecf20Sopenharmony_ci * quite possible for this routine to act as the kernel's EEH detection
16418c2ecf20Sopenharmony_ci * source (MMIO read of mbox_r). Because of this, there is a window of
16428c2ecf20Sopenharmony_ci * time where an EEH might have been detected but not yet 'serviced'
16438c2ecf20Sopenharmony_ci * (callback invoked, causing the device to enter reset state). To avoid
16448c2ecf20Sopenharmony_ci * looping in this routine during that window, a 1 second sleep is in place
16458c2ecf20Sopenharmony_ci * between the time the MMIO failure is detected and the time a wait on the
16468c2ecf20Sopenharmony_ci * reset wait queue is attempted via check_state().
16478c2ecf20Sopenharmony_ci *
16488c2ecf20Sopenharmony_ci * Return: 0 on success, -errno on failure
16498c2ecf20Sopenharmony_ci */
16508c2ecf20Sopenharmony_cistatic int cxlflash_afu_recover(struct scsi_device *sdev,
16518c2ecf20Sopenharmony_ci				struct dk_cxlflash_recover_afu *recover)
16528c2ecf20Sopenharmony_ci{
16538c2ecf20Sopenharmony_ci	struct cxlflash_cfg *cfg = shost_priv(sdev->host);
16548c2ecf20Sopenharmony_ci	struct device *dev = &cfg->dev->dev;
16558c2ecf20Sopenharmony_ci	struct llun_info *lli = sdev->hostdata;
16568c2ecf20Sopenharmony_ci	struct afu *afu = cfg->afu;
16578c2ecf20Sopenharmony_ci	struct ctx_info *ctxi = NULL;
16588c2ecf20Sopenharmony_ci	struct mutex *mutex = &cfg->ctx_recovery_mutex;
16598c2ecf20Sopenharmony_ci	struct hwq *hwq = get_hwq(afu, PRIMARY_HWQ);
16608c2ecf20Sopenharmony_ci	u64 flags;
16618c2ecf20Sopenharmony_ci	u64 ctxid = DECODE_CTXID(recover->context_id),
16628c2ecf20Sopenharmony_ci	    rctxid = recover->context_id;
16638c2ecf20Sopenharmony_ci	long reg;
16648c2ecf20Sopenharmony_ci	bool locked = true;
16658c2ecf20Sopenharmony_ci	int lretry = 20; /* up to 2 seconds */
16668c2ecf20Sopenharmony_ci	int new_adap_fd = -1;
16678c2ecf20Sopenharmony_ci	int rc = 0;
16688c2ecf20Sopenharmony_ci
16698c2ecf20Sopenharmony_ci	atomic_inc(&cfg->recovery_threads);
16708c2ecf20Sopenharmony_ci	up_read(&cfg->ioctl_rwsem);
16718c2ecf20Sopenharmony_ci	rc = mutex_lock_interruptible(mutex);
16728c2ecf20Sopenharmony_ci	down_read(&cfg->ioctl_rwsem);
16738c2ecf20Sopenharmony_ci	if (rc) {
16748c2ecf20Sopenharmony_ci		locked = false;
16758c2ecf20Sopenharmony_ci		goto out;
16768c2ecf20Sopenharmony_ci	}
16778c2ecf20Sopenharmony_ci
16788c2ecf20Sopenharmony_ci	rc = check_state(cfg);
16798c2ecf20Sopenharmony_ci	if (rc) {
16808c2ecf20Sopenharmony_ci		dev_err(dev, "%s: Failed state rc=%d\n", __func__, rc);
16818c2ecf20Sopenharmony_ci		rc = -ENODEV;
16828c2ecf20Sopenharmony_ci		goto out;
16838c2ecf20Sopenharmony_ci	}
16848c2ecf20Sopenharmony_ci
16858c2ecf20Sopenharmony_ci	dev_dbg(dev, "%s: reason=%016llx rctxid=%016llx\n",
16868c2ecf20Sopenharmony_ci		__func__, recover->reason, rctxid);
16878c2ecf20Sopenharmony_ci
16888c2ecf20Sopenharmony_ciretry:
16898c2ecf20Sopenharmony_ci	/* Ensure that this process is attached to the context */
16908c2ecf20Sopenharmony_ci	ctxi = get_context(cfg, rctxid, lli, CTX_CTRL_ERR_FALLBACK);
16918c2ecf20Sopenharmony_ci	if (unlikely(!ctxi)) {
16928c2ecf20Sopenharmony_ci		dev_dbg(dev, "%s: Bad context ctxid=%llu\n", __func__, ctxid);
16938c2ecf20Sopenharmony_ci		rc = -EINVAL;
16948c2ecf20Sopenharmony_ci		goto out;
16958c2ecf20Sopenharmony_ci	}
16968c2ecf20Sopenharmony_ci
16978c2ecf20Sopenharmony_ci	if (ctxi->err_recovery_active) {
16988c2ecf20Sopenharmony_ciretry_recover:
16998c2ecf20Sopenharmony_ci		rc = recover_context(cfg, ctxi, &new_adap_fd);
17008c2ecf20Sopenharmony_ci		if (unlikely(rc)) {
17018c2ecf20Sopenharmony_ci			dev_err(dev, "%s: Recovery failed ctxid=%llu rc=%d\n",
17028c2ecf20Sopenharmony_ci				__func__, ctxid, rc);
17038c2ecf20Sopenharmony_ci			if ((rc == -ENODEV) &&
17048c2ecf20Sopenharmony_ci			    ((atomic_read(&cfg->recovery_threads) > 1) ||
17058c2ecf20Sopenharmony_ci			     (lretry--))) {
17068c2ecf20Sopenharmony_ci				dev_dbg(dev, "%s: Going to try again\n",
17078c2ecf20Sopenharmony_ci					__func__);
17088c2ecf20Sopenharmony_ci				mutex_unlock(mutex);
17098c2ecf20Sopenharmony_ci				msleep(100);
17108c2ecf20Sopenharmony_ci				rc = mutex_lock_interruptible(mutex);
17118c2ecf20Sopenharmony_ci				if (rc) {
17128c2ecf20Sopenharmony_ci					locked = false;
17138c2ecf20Sopenharmony_ci					goto out;
17148c2ecf20Sopenharmony_ci				}
17158c2ecf20Sopenharmony_ci				goto retry_recover;
17168c2ecf20Sopenharmony_ci			}
17178c2ecf20Sopenharmony_ci
17188c2ecf20Sopenharmony_ci			goto out;
17198c2ecf20Sopenharmony_ci		}
17208c2ecf20Sopenharmony_ci
17218c2ecf20Sopenharmony_ci		ctxi->err_recovery_active = false;
17228c2ecf20Sopenharmony_ci
17238c2ecf20Sopenharmony_ci		flags = DK_CXLFLASH_APP_CLOSE_ADAP_FD |
17248c2ecf20Sopenharmony_ci			DK_CXLFLASH_RECOVER_AFU_CONTEXT_RESET;
17258c2ecf20Sopenharmony_ci		if (afu_is_sq_cmd_mode(afu))
17268c2ecf20Sopenharmony_ci			flags |= DK_CXLFLASH_CONTEXT_SQ_CMD_MODE;
17278c2ecf20Sopenharmony_ci
17288c2ecf20Sopenharmony_ci		recover->hdr.return_flags = flags;
17298c2ecf20Sopenharmony_ci		recover->context_id = ctxi->ctxid;
17308c2ecf20Sopenharmony_ci		recover->adap_fd = new_adap_fd;
17318c2ecf20Sopenharmony_ci		recover->mmio_size = sizeof(afu->afu_map->hosts[0].harea);
17328c2ecf20Sopenharmony_ci		goto out;
17338c2ecf20Sopenharmony_ci	}
17348c2ecf20Sopenharmony_ci
17358c2ecf20Sopenharmony_ci	/* Test if in error state */
17368c2ecf20Sopenharmony_ci	reg = readq_be(&hwq->ctrl_map->mbox_r);
17378c2ecf20Sopenharmony_ci	if (reg == -1) {
17388c2ecf20Sopenharmony_ci		dev_dbg(dev, "%s: MMIO fail, wait for recovery.\n", __func__);
17398c2ecf20Sopenharmony_ci
17408c2ecf20Sopenharmony_ci		/*
17418c2ecf20Sopenharmony_ci		 * Before checking the state, put back the context obtained with
17428c2ecf20Sopenharmony_ci		 * get_context() as it is no longer needed and sleep for a short
17438c2ecf20Sopenharmony_ci		 * period of time (see prolog notes).
17448c2ecf20Sopenharmony_ci		 */
17458c2ecf20Sopenharmony_ci		put_context(ctxi);
17468c2ecf20Sopenharmony_ci		ctxi = NULL;
17478c2ecf20Sopenharmony_ci		ssleep(1);
17488c2ecf20Sopenharmony_ci		rc = check_state(cfg);
17498c2ecf20Sopenharmony_ci		if (unlikely(rc))
17508c2ecf20Sopenharmony_ci			goto out;
17518c2ecf20Sopenharmony_ci		goto retry;
17528c2ecf20Sopenharmony_ci	}
17538c2ecf20Sopenharmony_ci
17548c2ecf20Sopenharmony_ci	dev_dbg(dev, "%s: MMIO working, no recovery required\n", __func__);
17558c2ecf20Sopenharmony_ciout:
17568c2ecf20Sopenharmony_ci	if (likely(ctxi))
17578c2ecf20Sopenharmony_ci		put_context(ctxi);
17588c2ecf20Sopenharmony_ci	if (locked)
17598c2ecf20Sopenharmony_ci		mutex_unlock(mutex);
17608c2ecf20Sopenharmony_ci	atomic_dec_if_positive(&cfg->recovery_threads);
17618c2ecf20Sopenharmony_ci	return rc;
17628c2ecf20Sopenharmony_ci}
17638c2ecf20Sopenharmony_ci
17648c2ecf20Sopenharmony_ci/**
17658c2ecf20Sopenharmony_ci * process_sense() - evaluates and processes sense data
17668c2ecf20Sopenharmony_ci * @sdev:	SCSI device associated with LUN.
17678c2ecf20Sopenharmony_ci * @verify:	Verify ioctl data structure.
17688c2ecf20Sopenharmony_ci *
17698c2ecf20Sopenharmony_ci * Return: 0 on success, -errno on failure
17708c2ecf20Sopenharmony_ci */
17718c2ecf20Sopenharmony_cistatic int process_sense(struct scsi_device *sdev,
17728c2ecf20Sopenharmony_ci			 struct dk_cxlflash_verify *verify)
17738c2ecf20Sopenharmony_ci{
17748c2ecf20Sopenharmony_ci	struct cxlflash_cfg *cfg = shost_priv(sdev->host);
17758c2ecf20Sopenharmony_ci	struct device *dev = &cfg->dev->dev;
17768c2ecf20Sopenharmony_ci	struct llun_info *lli = sdev->hostdata;
17778c2ecf20Sopenharmony_ci	struct glun_info *gli = lli->parent;
17788c2ecf20Sopenharmony_ci	u64 prev_lba = gli->max_lba;
17798c2ecf20Sopenharmony_ci	struct scsi_sense_hdr sshdr = { 0 };
17808c2ecf20Sopenharmony_ci	int rc = 0;
17818c2ecf20Sopenharmony_ci
17828c2ecf20Sopenharmony_ci	rc = scsi_normalize_sense((const u8 *)&verify->sense_data,
17838c2ecf20Sopenharmony_ci				  DK_CXLFLASH_VERIFY_SENSE_LEN, &sshdr);
17848c2ecf20Sopenharmony_ci	if (!rc) {
17858c2ecf20Sopenharmony_ci		dev_err(dev, "%s: Failed to normalize sense data\n", __func__);
17868c2ecf20Sopenharmony_ci		rc = -EINVAL;
17878c2ecf20Sopenharmony_ci		goto out;
17888c2ecf20Sopenharmony_ci	}
17898c2ecf20Sopenharmony_ci
17908c2ecf20Sopenharmony_ci	switch (sshdr.sense_key) {
17918c2ecf20Sopenharmony_ci	case NO_SENSE:
17928c2ecf20Sopenharmony_ci	case RECOVERED_ERROR:
17938c2ecf20Sopenharmony_ci	case NOT_READY:
17948c2ecf20Sopenharmony_ci		break;
17958c2ecf20Sopenharmony_ci	case UNIT_ATTENTION:
17968c2ecf20Sopenharmony_ci		switch (sshdr.asc) {
17978c2ecf20Sopenharmony_ci		case 0x29: /* Power on Reset or Device Reset */
17988c2ecf20Sopenharmony_ci			fallthrough;
17998c2ecf20Sopenharmony_ci		case 0x2A: /* Device settings/capacity changed */
18008c2ecf20Sopenharmony_ci			rc = read_cap16(sdev, lli);
18018c2ecf20Sopenharmony_ci			if (rc) {
18028c2ecf20Sopenharmony_ci				rc = -ENODEV;
18038c2ecf20Sopenharmony_ci				break;
18048c2ecf20Sopenharmony_ci			}
18058c2ecf20Sopenharmony_ci			if (prev_lba != gli->max_lba)
18068c2ecf20Sopenharmony_ci				dev_dbg(dev, "%s: Capacity changed old=%lld "
18078c2ecf20Sopenharmony_ci					"new=%lld\n", __func__, prev_lba,
18088c2ecf20Sopenharmony_ci					gli->max_lba);
18098c2ecf20Sopenharmony_ci			break;
18108c2ecf20Sopenharmony_ci		case 0x3F: /* Report LUNs changed, Rescan. */
18118c2ecf20Sopenharmony_ci			scsi_scan_host(cfg->host);
18128c2ecf20Sopenharmony_ci			break;
18138c2ecf20Sopenharmony_ci		default:
18148c2ecf20Sopenharmony_ci			rc = -EIO;
18158c2ecf20Sopenharmony_ci			break;
18168c2ecf20Sopenharmony_ci		}
18178c2ecf20Sopenharmony_ci		break;
18188c2ecf20Sopenharmony_ci	default:
18198c2ecf20Sopenharmony_ci		rc = -EIO;
18208c2ecf20Sopenharmony_ci		break;
18218c2ecf20Sopenharmony_ci	}
18228c2ecf20Sopenharmony_ciout:
18238c2ecf20Sopenharmony_ci	dev_dbg(dev, "%s: sense_key %x asc %x ascq %x rc %d\n", __func__,
18248c2ecf20Sopenharmony_ci		sshdr.sense_key, sshdr.asc, sshdr.ascq, rc);
18258c2ecf20Sopenharmony_ci	return rc;
18268c2ecf20Sopenharmony_ci}
18278c2ecf20Sopenharmony_ci
18288c2ecf20Sopenharmony_ci/**
18298c2ecf20Sopenharmony_ci * cxlflash_disk_verify() - verifies a LUN is the same and handle size changes
18308c2ecf20Sopenharmony_ci * @sdev:	SCSI device associated with LUN.
18318c2ecf20Sopenharmony_ci * @verify:	Verify ioctl data structure.
18328c2ecf20Sopenharmony_ci *
18338c2ecf20Sopenharmony_ci * Return: 0 on success, -errno on failure
18348c2ecf20Sopenharmony_ci */
18358c2ecf20Sopenharmony_cistatic int cxlflash_disk_verify(struct scsi_device *sdev,
18368c2ecf20Sopenharmony_ci				struct dk_cxlflash_verify *verify)
18378c2ecf20Sopenharmony_ci{
18388c2ecf20Sopenharmony_ci	int rc = 0;
18398c2ecf20Sopenharmony_ci	struct ctx_info *ctxi = NULL;
18408c2ecf20Sopenharmony_ci	struct cxlflash_cfg *cfg = shost_priv(sdev->host);
18418c2ecf20Sopenharmony_ci	struct device *dev = &cfg->dev->dev;
18428c2ecf20Sopenharmony_ci	struct llun_info *lli = sdev->hostdata;
18438c2ecf20Sopenharmony_ci	struct glun_info *gli = lli->parent;
18448c2ecf20Sopenharmony_ci	struct sisl_rht_entry *rhte = NULL;
18458c2ecf20Sopenharmony_ci	res_hndl_t rhndl = verify->rsrc_handle;
18468c2ecf20Sopenharmony_ci	u64 ctxid = DECODE_CTXID(verify->context_id),
18478c2ecf20Sopenharmony_ci	    rctxid = verify->context_id;
18488c2ecf20Sopenharmony_ci	u64 last_lba = 0;
18498c2ecf20Sopenharmony_ci
18508c2ecf20Sopenharmony_ci	dev_dbg(dev, "%s: ctxid=%llu rhndl=%016llx, hint=%016llx, "
18518c2ecf20Sopenharmony_ci		"flags=%016llx\n", __func__, ctxid, verify->rsrc_handle,
18528c2ecf20Sopenharmony_ci		verify->hint, verify->hdr.flags);
18538c2ecf20Sopenharmony_ci
18548c2ecf20Sopenharmony_ci	ctxi = get_context(cfg, rctxid, lli, 0);
18558c2ecf20Sopenharmony_ci	if (unlikely(!ctxi)) {
18568c2ecf20Sopenharmony_ci		dev_dbg(dev, "%s: Bad context ctxid=%llu\n", __func__, ctxid);
18578c2ecf20Sopenharmony_ci		rc = -EINVAL;
18588c2ecf20Sopenharmony_ci		goto out;
18598c2ecf20Sopenharmony_ci	}
18608c2ecf20Sopenharmony_ci
18618c2ecf20Sopenharmony_ci	rhte = get_rhte(ctxi, rhndl, lli);
18628c2ecf20Sopenharmony_ci	if (unlikely(!rhte)) {
18638c2ecf20Sopenharmony_ci		dev_dbg(dev, "%s: Bad resource handle rhndl=%d\n",
18648c2ecf20Sopenharmony_ci			__func__, rhndl);
18658c2ecf20Sopenharmony_ci		rc = -EINVAL;
18668c2ecf20Sopenharmony_ci		goto out;
18678c2ecf20Sopenharmony_ci	}
18688c2ecf20Sopenharmony_ci
18698c2ecf20Sopenharmony_ci	/*
18708c2ecf20Sopenharmony_ci	 * Look at the hint/sense to see if it requires us to redrive
18718c2ecf20Sopenharmony_ci	 * inquiry (i.e. the Unit attention is due to the WWN changing).
18728c2ecf20Sopenharmony_ci	 */
18738c2ecf20Sopenharmony_ci	if (verify->hint & DK_CXLFLASH_VERIFY_HINT_SENSE) {
18748c2ecf20Sopenharmony_ci		/* Can't hold mutex across process_sense/read_cap16,
18758c2ecf20Sopenharmony_ci		 * since we could have an intervening EEH event.
18768c2ecf20Sopenharmony_ci		 */
18778c2ecf20Sopenharmony_ci		ctxi->unavail = true;
18788c2ecf20Sopenharmony_ci		mutex_unlock(&ctxi->mutex);
18798c2ecf20Sopenharmony_ci		rc = process_sense(sdev, verify);
18808c2ecf20Sopenharmony_ci		if (unlikely(rc)) {
18818c2ecf20Sopenharmony_ci			dev_err(dev, "%s: Failed to validate sense data (%d)\n",
18828c2ecf20Sopenharmony_ci				__func__, rc);
18838c2ecf20Sopenharmony_ci			mutex_lock(&ctxi->mutex);
18848c2ecf20Sopenharmony_ci			ctxi->unavail = false;
18858c2ecf20Sopenharmony_ci			goto out;
18868c2ecf20Sopenharmony_ci		}
18878c2ecf20Sopenharmony_ci		mutex_lock(&ctxi->mutex);
18888c2ecf20Sopenharmony_ci		ctxi->unavail = false;
18898c2ecf20Sopenharmony_ci	}
18908c2ecf20Sopenharmony_ci
18918c2ecf20Sopenharmony_ci	switch (gli->mode) {
18928c2ecf20Sopenharmony_ci	case MODE_PHYSICAL:
18938c2ecf20Sopenharmony_ci		last_lba = gli->max_lba;
18948c2ecf20Sopenharmony_ci		break;
18958c2ecf20Sopenharmony_ci	case MODE_VIRTUAL:
18968c2ecf20Sopenharmony_ci		/* Cast lxt_cnt to u64 for multiply to be treated as 64bit op */
18978c2ecf20Sopenharmony_ci		last_lba = ((u64)rhte->lxt_cnt * MC_CHUNK_SIZE * gli->blk_len);
18988c2ecf20Sopenharmony_ci		last_lba /= CXLFLASH_BLOCK_SIZE;
18998c2ecf20Sopenharmony_ci		last_lba--;
19008c2ecf20Sopenharmony_ci		break;
19018c2ecf20Sopenharmony_ci	default:
19028c2ecf20Sopenharmony_ci		WARN(1, "Unsupported LUN mode!");
19038c2ecf20Sopenharmony_ci	}
19048c2ecf20Sopenharmony_ci
19058c2ecf20Sopenharmony_ci	verify->last_lba = last_lba;
19068c2ecf20Sopenharmony_ci
19078c2ecf20Sopenharmony_ciout:
19088c2ecf20Sopenharmony_ci	if (likely(ctxi))
19098c2ecf20Sopenharmony_ci		put_context(ctxi);
19108c2ecf20Sopenharmony_ci	dev_dbg(dev, "%s: returning rc=%d llba=%llx\n",
19118c2ecf20Sopenharmony_ci		__func__, rc, verify->last_lba);
19128c2ecf20Sopenharmony_ci	return rc;
19138c2ecf20Sopenharmony_ci}
19148c2ecf20Sopenharmony_ci
19158c2ecf20Sopenharmony_ci/**
19168c2ecf20Sopenharmony_ci * decode_ioctl() - translates an encoded ioctl to an easily identifiable string
19178c2ecf20Sopenharmony_ci * @cmd:	The ioctl command to decode.
19188c2ecf20Sopenharmony_ci *
19198c2ecf20Sopenharmony_ci * Return: A string identifying the decoded ioctl.
19208c2ecf20Sopenharmony_ci */
19218c2ecf20Sopenharmony_cistatic char *decode_ioctl(unsigned int cmd)
19228c2ecf20Sopenharmony_ci{
19238c2ecf20Sopenharmony_ci	switch (cmd) {
19248c2ecf20Sopenharmony_ci	case DK_CXLFLASH_ATTACH:
19258c2ecf20Sopenharmony_ci		return __stringify_1(DK_CXLFLASH_ATTACH);
19268c2ecf20Sopenharmony_ci	case DK_CXLFLASH_USER_DIRECT:
19278c2ecf20Sopenharmony_ci		return __stringify_1(DK_CXLFLASH_USER_DIRECT);
19288c2ecf20Sopenharmony_ci	case DK_CXLFLASH_USER_VIRTUAL:
19298c2ecf20Sopenharmony_ci		return __stringify_1(DK_CXLFLASH_USER_VIRTUAL);
19308c2ecf20Sopenharmony_ci	case DK_CXLFLASH_VLUN_RESIZE:
19318c2ecf20Sopenharmony_ci		return __stringify_1(DK_CXLFLASH_VLUN_RESIZE);
19328c2ecf20Sopenharmony_ci	case DK_CXLFLASH_RELEASE:
19338c2ecf20Sopenharmony_ci		return __stringify_1(DK_CXLFLASH_RELEASE);
19348c2ecf20Sopenharmony_ci	case DK_CXLFLASH_DETACH:
19358c2ecf20Sopenharmony_ci		return __stringify_1(DK_CXLFLASH_DETACH);
19368c2ecf20Sopenharmony_ci	case DK_CXLFLASH_VERIFY:
19378c2ecf20Sopenharmony_ci		return __stringify_1(DK_CXLFLASH_VERIFY);
19388c2ecf20Sopenharmony_ci	case DK_CXLFLASH_VLUN_CLONE:
19398c2ecf20Sopenharmony_ci		return __stringify_1(DK_CXLFLASH_VLUN_CLONE);
19408c2ecf20Sopenharmony_ci	case DK_CXLFLASH_RECOVER_AFU:
19418c2ecf20Sopenharmony_ci		return __stringify_1(DK_CXLFLASH_RECOVER_AFU);
19428c2ecf20Sopenharmony_ci	case DK_CXLFLASH_MANAGE_LUN:
19438c2ecf20Sopenharmony_ci		return __stringify_1(DK_CXLFLASH_MANAGE_LUN);
19448c2ecf20Sopenharmony_ci	}
19458c2ecf20Sopenharmony_ci
19468c2ecf20Sopenharmony_ci	return "UNKNOWN";
19478c2ecf20Sopenharmony_ci}
19488c2ecf20Sopenharmony_ci
19498c2ecf20Sopenharmony_ci/**
19508c2ecf20Sopenharmony_ci * cxlflash_disk_direct_open() - opens a direct (physical) disk
19518c2ecf20Sopenharmony_ci * @sdev:	SCSI device associated with LUN.
19528c2ecf20Sopenharmony_ci * @arg:	UDirect ioctl data structure.
19538c2ecf20Sopenharmony_ci *
19548c2ecf20Sopenharmony_ci * On successful return, the user is informed of the resource handle
19558c2ecf20Sopenharmony_ci * to be used to identify the direct lun and the size (in blocks) of
19568c2ecf20Sopenharmony_ci * the direct lun in last LBA format.
19578c2ecf20Sopenharmony_ci *
19588c2ecf20Sopenharmony_ci * Return: 0 on success, -errno on failure
19598c2ecf20Sopenharmony_ci */
19608c2ecf20Sopenharmony_cistatic int cxlflash_disk_direct_open(struct scsi_device *sdev, void *arg)
19618c2ecf20Sopenharmony_ci{
19628c2ecf20Sopenharmony_ci	struct cxlflash_cfg *cfg = shost_priv(sdev->host);
19638c2ecf20Sopenharmony_ci	struct device *dev = &cfg->dev->dev;
19648c2ecf20Sopenharmony_ci	struct afu *afu = cfg->afu;
19658c2ecf20Sopenharmony_ci	struct llun_info *lli = sdev->hostdata;
19668c2ecf20Sopenharmony_ci	struct glun_info *gli = lli->parent;
19678c2ecf20Sopenharmony_ci	struct dk_cxlflash_release rel = { { 0 }, 0 };
19688c2ecf20Sopenharmony_ci
19698c2ecf20Sopenharmony_ci	struct dk_cxlflash_udirect *pphys = (struct dk_cxlflash_udirect *)arg;
19708c2ecf20Sopenharmony_ci
19718c2ecf20Sopenharmony_ci	u64 ctxid = DECODE_CTXID(pphys->context_id),
19728c2ecf20Sopenharmony_ci	    rctxid = pphys->context_id;
19738c2ecf20Sopenharmony_ci	u64 lun_size = 0;
19748c2ecf20Sopenharmony_ci	u64 last_lba = 0;
19758c2ecf20Sopenharmony_ci	u64 rsrc_handle = -1;
19768c2ecf20Sopenharmony_ci	u32 port = CHAN2PORTMASK(sdev->channel);
19778c2ecf20Sopenharmony_ci
19788c2ecf20Sopenharmony_ci	int rc = 0;
19798c2ecf20Sopenharmony_ci
19808c2ecf20Sopenharmony_ci	struct ctx_info *ctxi = NULL;
19818c2ecf20Sopenharmony_ci	struct sisl_rht_entry *rhte = NULL;
19828c2ecf20Sopenharmony_ci
19838c2ecf20Sopenharmony_ci	dev_dbg(dev, "%s: ctxid=%llu ls=%llu\n", __func__, ctxid, lun_size);
19848c2ecf20Sopenharmony_ci
19858c2ecf20Sopenharmony_ci	rc = cxlflash_lun_attach(gli, MODE_PHYSICAL, false);
19868c2ecf20Sopenharmony_ci	if (unlikely(rc)) {
19878c2ecf20Sopenharmony_ci		dev_dbg(dev, "%s: Failed attach to LUN (PHYSICAL)\n", __func__);
19888c2ecf20Sopenharmony_ci		goto out;
19898c2ecf20Sopenharmony_ci	}
19908c2ecf20Sopenharmony_ci
19918c2ecf20Sopenharmony_ci	ctxi = get_context(cfg, rctxid, lli, 0);
19928c2ecf20Sopenharmony_ci	if (unlikely(!ctxi)) {
19938c2ecf20Sopenharmony_ci		dev_dbg(dev, "%s: Bad context ctxid=%llu\n", __func__, ctxid);
19948c2ecf20Sopenharmony_ci		rc = -EINVAL;
19958c2ecf20Sopenharmony_ci		goto err1;
19968c2ecf20Sopenharmony_ci	}
19978c2ecf20Sopenharmony_ci
19988c2ecf20Sopenharmony_ci	rhte = rhte_checkout(ctxi, lli);
19998c2ecf20Sopenharmony_ci	if (unlikely(!rhte)) {
20008c2ecf20Sopenharmony_ci		dev_dbg(dev, "%s: Too many opens ctxid=%lld\n",
20018c2ecf20Sopenharmony_ci			__func__, ctxid);
20028c2ecf20Sopenharmony_ci		rc = -EMFILE;	/* too many opens  */
20038c2ecf20Sopenharmony_ci		goto err1;
20048c2ecf20Sopenharmony_ci	}
20058c2ecf20Sopenharmony_ci
20068c2ecf20Sopenharmony_ci	rsrc_handle = (rhte - ctxi->rht_start);
20078c2ecf20Sopenharmony_ci
20088c2ecf20Sopenharmony_ci	rht_format1(rhte, lli->lun_id[sdev->channel], ctxi->rht_perms, port);
20098c2ecf20Sopenharmony_ci
20108c2ecf20Sopenharmony_ci	last_lba = gli->max_lba;
20118c2ecf20Sopenharmony_ci	pphys->hdr.return_flags = 0;
20128c2ecf20Sopenharmony_ci	pphys->last_lba = last_lba;
20138c2ecf20Sopenharmony_ci	pphys->rsrc_handle = rsrc_handle;
20148c2ecf20Sopenharmony_ci
20158c2ecf20Sopenharmony_ci	rc = cxlflash_afu_sync(afu, ctxid, rsrc_handle, AFU_LW_SYNC);
20168c2ecf20Sopenharmony_ci	if (unlikely(rc)) {
20178c2ecf20Sopenharmony_ci		dev_dbg(dev, "%s: AFU sync failed rc=%d\n", __func__, rc);
20188c2ecf20Sopenharmony_ci		goto err2;
20198c2ecf20Sopenharmony_ci	}
20208c2ecf20Sopenharmony_ci
20218c2ecf20Sopenharmony_ciout:
20228c2ecf20Sopenharmony_ci	if (likely(ctxi))
20238c2ecf20Sopenharmony_ci		put_context(ctxi);
20248c2ecf20Sopenharmony_ci	dev_dbg(dev, "%s: returning handle=%llu rc=%d llba=%llu\n",
20258c2ecf20Sopenharmony_ci		__func__, rsrc_handle, rc, last_lba);
20268c2ecf20Sopenharmony_ci	return rc;
20278c2ecf20Sopenharmony_ci
20288c2ecf20Sopenharmony_cierr2:
20298c2ecf20Sopenharmony_ci	marshal_udir_to_rele(pphys, &rel);
20308c2ecf20Sopenharmony_ci	_cxlflash_disk_release(sdev, ctxi, &rel);
20318c2ecf20Sopenharmony_ci	goto out;
20328c2ecf20Sopenharmony_cierr1:
20338c2ecf20Sopenharmony_ci	cxlflash_lun_detach(gli);
20348c2ecf20Sopenharmony_ci	goto out;
20358c2ecf20Sopenharmony_ci}
20368c2ecf20Sopenharmony_ci
20378c2ecf20Sopenharmony_ci/**
20388c2ecf20Sopenharmony_ci * ioctl_common() - common IOCTL handler for driver
20398c2ecf20Sopenharmony_ci * @sdev:	SCSI device associated with LUN.
20408c2ecf20Sopenharmony_ci * @cmd:	IOCTL command.
20418c2ecf20Sopenharmony_ci *
20428c2ecf20Sopenharmony_ci * Handles common fencing operations that are valid for multiple ioctls. Always
20438c2ecf20Sopenharmony_ci * allow through ioctls that are cleanup oriented in nature, even when operating
20448c2ecf20Sopenharmony_ci * in a failed/terminating state.
20458c2ecf20Sopenharmony_ci *
20468c2ecf20Sopenharmony_ci * Return: 0 on success, -errno on failure
20478c2ecf20Sopenharmony_ci */
20488c2ecf20Sopenharmony_cistatic int ioctl_common(struct scsi_device *sdev, unsigned int cmd)
20498c2ecf20Sopenharmony_ci{
20508c2ecf20Sopenharmony_ci	struct cxlflash_cfg *cfg = shost_priv(sdev->host);
20518c2ecf20Sopenharmony_ci	struct device *dev = &cfg->dev->dev;
20528c2ecf20Sopenharmony_ci	struct llun_info *lli = sdev->hostdata;
20538c2ecf20Sopenharmony_ci	int rc = 0;
20548c2ecf20Sopenharmony_ci
20558c2ecf20Sopenharmony_ci	if (unlikely(!lli)) {
20568c2ecf20Sopenharmony_ci		dev_dbg(dev, "%s: Unknown LUN\n", __func__);
20578c2ecf20Sopenharmony_ci		rc = -EINVAL;
20588c2ecf20Sopenharmony_ci		goto out;
20598c2ecf20Sopenharmony_ci	}
20608c2ecf20Sopenharmony_ci
20618c2ecf20Sopenharmony_ci	rc = check_state(cfg);
20628c2ecf20Sopenharmony_ci	if (unlikely(rc) && (cfg->state == STATE_FAILTERM)) {
20638c2ecf20Sopenharmony_ci		switch (cmd) {
20648c2ecf20Sopenharmony_ci		case DK_CXLFLASH_VLUN_RESIZE:
20658c2ecf20Sopenharmony_ci		case DK_CXLFLASH_RELEASE:
20668c2ecf20Sopenharmony_ci		case DK_CXLFLASH_DETACH:
20678c2ecf20Sopenharmony_ci			dev_dbg(dev, "%s: Command override rc=%d\n",
20688c2ecf20Sopenharmony_ci				__func__, rc);
20698c2ecf20Sopenharmony_ci			rc = 0;
20708c2ecf20Sopenharmony_ci			break;
20718c2ecf20Sopenharmony_ci		}
20728c2ecf20Sopenharmony_ci	}
20738c2ecf20Sopenharmony_ciout:
20748c2ecf20Sopenharmony_ci	return rc;
20758c2ecf20Sopenharmony_ci}
20768c2ecf20Sopenharmony_ci
20778c2ecf20Sopenharmony_ci/**
20788c2ecf20Sopenharmony_ci * cxlflash_ioctl() - IOCTL handler for driver
20798c2ecf20Sopenharmony_ci * @sdev:	SCSI device associated with LUN.
20808c2ecf20Sopenharmony_ci * @cmd:	IOCTL command.
20818c2ecf20Sopenharmony_ci * @arg:	Userspace ioctl data structure.
20828c2ecf20Sopenharmony_ci *
20838c2ecf20Sopenharmony_ci * A read/write semaphore is used to implement a 'drain' of currently
20848c2ecf20Sopenharmony_ci * running ioctls. The read semaphore is taken at the beginning of each
20858c2ecf20Sopenharmony_ci * ioctl thread and released upon concluding execution. Additionally the
20868c2ecf20Sopenharmony_ci * semaphore should be released and then reacquired in any ioctl execution
20878c2ecf20Sopenharmony_ci * path which will wait for an event to occur that is outside the scope of
20888c2ecf20Sopenharmony_ci * the ioctl (i.e. an adapter reset). To drain the ioctls currently running,
20898c2ecf20Sopenharmony_ci * a thread simply needs to acquire the write semaphore.
20908c2ecf20Sopenharmony_ci *
20918c2ecf20Sopenharmony_ci * Return: 0 on success, -errno on failure
20928c2ecf20Sopenharmony_ci */
20938c2ecf20Sopenharmony_ciint cxlflash_ioctl(struct scsi_device *sdev, unsigned int cmd, void __user *arg)
20948c2ecf20Sopenharmony_ci{
20958c2ecf20Sopenharmony_ci	typedef int (*sioctl) (struct scsi_device *, void *);
20968c2ecf20Sopenharmony_ci
20978c2ecf20Sopenharmony_ci	struct cxlflash_cfg *cfg = shost_priv(sdev->host);
20988c2ecf20Sopenharmony_ci	struct device *dev = &cfg->dev->dev;
20998c2ecf20Sopenharmony_ci	struct afu *afu = cfg->afu;
21008c2ecf20Sopenharmony_ci	struct dk_cxlflash_hdr *hdr;
21018c2ecf20Sopenharmony_ci	char buf[sizeof(union cxlflash_ioctls)];
21028c2ecf20Sopenharmony_ci	size_t size = 0;
21038c2ecf20Sopenharmony_ci	bool known_ioctl = false;
21048c2ecf20Sopenharmony_ci	int idx;
21058c2ecf20Sopenharmony_ci	int rc = 0;
21068c2ecf20Sopenharmony_ci	struct Scsi_Host *shost = sdev->host;
21078c2ecf20Sopenharmony_ci	sioctl do_ioctl = NULL;
21088c2ecf20Sopenharmony_ci
21098c2ecf20Sopenharmony_ci	static const struct {
21108c2ecf20Sopenharmony_ci		size_t size;
21118c2ecf20Sopenharmony_ci		sioctl ioctl;
21128c2ecf20Sopenharmony_ci	} ioctl_tbl[] = {	/* NOTE: order matters here */
21138c2ecf20Sopenharmony_ci	{sizeof(struct dk_cxlflash_attach), (sioctl)cxlflash_disk_attach},
21148c2ecf20Sopenharmony_ci	{sizeof(struct dk_cxlflash_udirect), cxlflash_disk_direct_open},
21158c2ecf20Sopenharmony_ci	{sizeof(struct dk_cxlflash_release), (sioctl)cxlflash_disk_release},
21168c2ecf20Sopenharmony_ci	{sizeof(struct dk_cxlflash_detach), (sioctl)cxlflash_disk_detach},
21178c2ecf20Sopenharmony_ci	{sizeof(struct dk_cxlflash_verify), (sioctl)cxlflash_disk_verify},
21188c2ecf20Sopenharmony_ci	{sizeof(struct dk_cxlflash_recover_afu), (sioctl)cxlflash_afu_recover},
21198c2ecf20Sopenharmony_ci	{sizeof(struct dk_cxlflash_manage_lun), (sioctl)cxlflash_manage_lun},
21208c2ecf20Sopenharmony_ci	{sizeof(struct dk_cxlflash_uvirtual), cxlflash_disk_virtual_open},
21218c2ecf20Sopenharmony_ci	{sizeof(struct dk_cxlflash_resize), (sioctl)cxlflash_vlun_resize},
21228c2ecf20Sopenharmony_ci	{sizeof(struct dk_cxlflash_clone), (sioctl)cxlflash_disk_clone},
21238c2ecf20Sopenharmony_ci	};
21248c2ecf20Sopenharmony_ci
21258c2ecf20Sopenharmony_ci	/* Hold read semaphore so we can drain if needed */
21268c2ecf20Sopenharmony_ci	down_read(&cfg->ioctl_rwsem);
21278c2ecf20Sopenharmony_ci
21288c2ecf20Sopenharmony_ci	/* Restrict command set to physical support only for internal LUN */
21298c2ecf20Sopenharmony_ci	if (afu->internal_lun)
21308c2ecf20Sopenharmony_ci		switch (cmd) {
21318c2ecf20Sopenharmony_ci		case DK_CXLFLASH_RELEASE:
21328c2ecf20Sopenharmony_ci		case DK_CXLFLASH_USER_VIRTUAL:
21338c2ecf20Sopenharmony_ci		case DK_CXLFLASH_VLUN_RESIZE:
21348c2ecf20Sopenharmony_ci		case DK_CXLFLASH_VLUN_CLONE:
21358c2ecf20Sopenharmony_ci			dev_dbg(dev, "%s: %s not supported for lun_mode=%d\n",
21368c2ecf20Sopenharmony_ci				__func__, decode_ioctl(cmd), afu->internal_lun);
21378c2ecf20Sopenharmony_ci			rc = -EINVAL;
21388c2ecf20Sopenharmony_ci			goto cxlflash_ioctl_exit;
21398c2ecf20Sopenharmony_ci		}
21408c2ecf20Sopenharmony_ci
21418c2ecf20Sopenharmony_ci	switch (cmd) {
21428c2ecf20Sopenharmony_ci	case DK_CXLFLASH_ATTACH:
21438c2ecf20Sopenharmony_ci	case DK_CXLFLASH_USER_DIRECT:
21448c2ecf20Sopenharmony_ci	case DK_CXLFLASH_RELEASE:
21458c2ecf20Sopenharmony_ci	case DK_CXLFLASH_DETACH:
21468c2ecf20Sopenharmony_ci	case DK_CXLFLASH_VERIFY:
21478c2ecf20Sopenharmony_ci	case DK_CXLFLASH_RECOVER_AFU:
21488c2ecf20Sopenharmony_ci	case DK_CXLFLASH_USER_VIRTUAL:
21498c2ecf20Sopenharmony_ci	case DK_CXLFLASH_VLUN_RESIZE:
21508c2ecf20Sopenharmony_ci	case DK_CXLFLASH_VLUN_CLONE:
21518c2ecf20Sopenharmony_ci		dev_dbg(dev, "%s: %s (%08X) on dev(%d/%d/%d/%llu)\n",
21528c2ecf20Sopenharmony_ci			__func__, decode_ioctl(cmd), cmd, shost->host_no,
21538c2ecf20Sopenharmony_ci			sdev->channel, sdev->id, sdev->lun);
21548c2ecf20Sopenharmony_ci		rc = ioctl_common(sdev, cmd);
21558c2ecf20Sopenharmony_ci		if (unlikely(rc))
21568c2ecf20Sopenharmony_ci			goto cxlflash_ioctl_exit;
21578c2ecf20Sopenharmony_ci
21588c2ecf20Sopenharmony_ci		fallthrough;
21598c2ecf20Sopenharmony_ci
21608c2ecf20Sopenharmony_ci	case DK_CXLFLASH_MANAGE_LUN:
21618c2ecf20Sopenharmony_ci		known_ioctl = true;
21628c2ecf20Sopenharmony_ci		idx = _IOC_NR(cmd) - _IOC_NR(DK_CXLFLASH_ATTACH);
21638c2ecf20Sopenharmony_ci		size = ioctl_tbl[idx].size;
21648c2ecf20Sopenharmony_ci		do_ioctl = ioctl_tbl[idx].ioctl;
21658c2ecf20Sopenharmony_ci
21668c2ecf20Sopenharmony_ci		if (likely(do_ioctl))
21678c2ecf20Sopenharmony_ci			break;
21688c2ecf20Sopenharmony_ci
21698c2ecf20Sopenharmony_ci		fallthrough;
21708c2ecf20Sopenharmony_ci	default:
21718c2ecf20Sopenharmony_ci		rc = -EINVAL;
21728c2ecf20Sopenharmony_ci		goto cxlflash_ioctl_exit;
21738c2ecf20Sopenharmony_ci	}
21748c2ecf20Sopenharmony_ci
21758c2ecf20Sopenharmony_ci	if (unlikely(copy_from_user(&buf, arg, size))) {
21768c2ecf20Sopenharmony_ci		dev_err(dev, "%s: copy_from_user() fail size=%lu cmd=%u (%s) arg=%p\n",
21778c2ecf20Sopenharmony_ci			__func__, size, cmd, decode_ioctl(cmd), arg);
21788c2ecf20Sopenharmony_ci		rc = -EFAULT;
21798c2ecf20Sopenharmony_ci		goto cxlflash_ioctl_exit;
21808c2ecf20Sopenharmony_ci	}
21818c2ecf20Sopenharmony_ci
21828c2ecf20Sopenharmony_ci	hdr = (struct dk_cxlflash_hdr *)&buf;
21838c2ecf20Sopenharmony_ci	if (hdr->version != DK_CXLFLASH_VERSION_0) {
21848c2ecf20Sopenharmony_ci		dev_dbg(dev, "%s: Version %u not supported for %s\n",
21858c2ecf20Sopenharmony_ci			__func__, hdr->version, decode_ioctl(cmd));
21868c2ecf20Sopenharmony_ci		rc = -EINVAL;
21878c2ecf20Sopenharmony_ci		goto cxlflash_ioctl_exit;
21888c2ecf20Sopenharmony_ci	}
21898c2ecf20Sopenharmony_ci
21908c2ecf20Sopenharmony_ci	if (hdr->rsvd[0] || hdr->rsvd[1] || hdr->rsvd[2] || hdr->return_flags) {
21918c2ecf20Sopenharmony_ci		dev_dbg(dev, "%s: Reserved/rflags populated\n", __func__);
21928c2ecf20Sopenharmony_ci		rc = -EINVAL;
21938c2ecf20Sopenharmony_ci		goto cxlflash_ioctl_exit;
21948c2ecf20Sopenharmony_ci	}
21958c2ecf20Sopenharmony_ci
21968c2ecf20Sopenharmony_ci	rc = do_ioctl(sdev, (void *)&buf);
21978c2ecf20Sopenharmony_ci	if (likely(!rc))
21988c2ecf20Sopenharmony_ci		if (unlikely(copy_to_user(arg, &buf, size))) {
21998c2ecf20Sopenharmony_ci			dev_err(dev, "%s: copy_to_user() fail size=%lu cmd=%u (%s) arg=%p\n",
22008c2ecf20Sopenharmony_ci				__func__, size, cmd, decode_ioctl(cmd), arg);
22018c2ecf20Sopenharmony_ci			rc = -EFAULT;
22028c2ecf20Sopenharmony_ci		}
22038c2ecf20Sopenharmony_ci
22048c2ecf20Sopenharmony_ci	/* fall through to exit */
22058c2ecf20Sopenharmony_ci
22068c2ecf20Sopenharmony_cicxlflash_ioctl_exit:
22078c2ecf20Sopenharmony_ci	up_read(&cfg->ioctl_rwsem);
22088c2ecf20Sopenharmony_ci	if (unlikely(rc && known_ioctl))
22098c2ecf20Sopenharmony_ci		dev_err(dev, "%s: ioctl %s (%08X) on dev(%d/%d/%d/%llu) "
22108c2ecf20Sopenharmony_ci			"returned rc %d\n", __func__,
22118c2ecf20Sopenharmony_ci			decode_ioctl(cmd), cmd, shost->host_no,
22128c2ecf20Sopenharmony_ci			sdev->channel, sdev->id, sdev->lun, rc);
22138c2ecf20Sopenharmony_ci	else
22148c2ecf20Sopenharmony_ci		dev_dbg(dev, "%s: ioctl %s (%08X) on dev(%d/%d/%d/%llu) "
22158c2ecf20Sopenharmony_ci			"returned rc %d\n", __func__, decode_ioctl(cmd),
22168c2ecf20Sopenharmony_ci			cmd, shost->host_no, sdev->channel, sdev->id,
22178c2ecf20Sopenharmony_ci			sdev->lun, rc);
22188c2ecf20Sopenharmony_ci	return rc;
22198c2ecf20Sopenharmony_ci}
2220