18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * CXL Flash Device Driver
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Written by: Matthew R. Ochs <mrochs@linux.vnet.ibm.com>, IBM Corporation
68c2ecf20Sopenharmony_ci *             Uma Krishnan <ukrishn@linux.vnet.ibm.com>, IBM Corporation
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * Copyright (C) 2018 IBM Corporation
98c2ecf20Sopenharmony_ci */
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#include <linux/file.h>
128c2ecf20Sopenharmony_ci#include <linux/idr.h>
138c2ecf20Sopenharmony_ci#include <linux/module.h>
148c2ecf20Sopenharmony_ci#include <linux/mount.h>
158c2ecf20Sopenharmony_ci#include <linux/pseudo_fs.h>
168c2ecf20Sopenharmony_ci#include <linux/poll.h>
178c2ecf20Sopenharmony_ci#include <linux/sched/signal.h>
188c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
198c2ecf20Sopenharmony_ci#include <asm/xive.h>
208c2ecf20Sopenharmony_ci#include <misc/ocxl.h>
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci#include <uapi/misc/cxl.h>
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci#include "backend.h"
258c2ecf20Sopenharmony_ci#include "ocxl_hw.h"
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci/*
288c2ecf20Sopenharmony_ci * Pseudo-filesystem to allocate inodes.
298c2ecf20Sopenharmony_ci */
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci#define OCXLFLASH_FS_MAGIC      0x1697698f
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_cistatic int ocxlflash_fs_cnt;
348c2ecf20Sopenharmony_cistatic struct vfsmount *ocxlflash_vfs_mount;
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_cistatic int ocxlflash_fs_init_fs_context(struct fs_context *fc)
378c2ecf20Sopenharmony_ci{
388c2ecf20Sopenharmony_ci	return init_pseudo(fc, OCXLFLASH_FS_MAGIC) ? 0 : -ENOMEM;
398c2ecf20Sopenharmony_ci}
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_cistatic struct file_system_type ocxlflash_fs_type = {
428c2ecf20Sopenharmony_ci	.name		= "ocxlflash",
438c2ecf20Sopenharmony_ci	.owner		= THIS_MODULE,
448c2ecf20Sopenharmony_ci	.init_fs_context = ocxlflash_fs_init_fs_context,
458c2ecf20Sopenharmony_ci	.kill_sb	= kill_anon_super,
468c2ecf20Sopenharmony_ci};
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci/*
498c2ecf20Sopenharmony_ci * ocxlflash_release_mapping() - release the memory mapping
508c2ecf20Sopenharmony_ci * @ctx:	Context whose mapping is to be released.
518c2ecf20Sopenharmony_ci */
528c2ecf20Sopenharmony_cistatic void ocxlflash_release_mapping(struct ocxlflash_context *ctx)
538c2ecf20Sopenharmony_ci{
548c2ecf20Sopenharmony_ci	if (ctx->mapping)
558c2ecf20Sopenharmony_ci		simple_release_fs(&ocxlflash_vfs_mount, &ocxlflash_fs_cnt);
568c2ecf20Sopenharmony_ci	ctx->mapping = NULL;
578c2ecf20Sopenharmony_ci}
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci/*
608c2ecf20Sopenharmony_ci * ocxlflash_getfile() - allocate pseudo filesystem, inode, and the file
618c2ecf20Sopenharmony_ci * @dev:	Generic device of the host.
628c2ecf20Sopenharmony_ci * @name:	Name of the pseudo filesystem.
638c2ecf20Sopenharmony_ci * @fops:	File operations.
648c2ecf20Sopenharmony_ci * @priv:	Private data.
658c2ecf20Sopenharmony_ci * @flags:	Flags for the file.
668c2ecf20Sopenharmony_ci *
678c2ecf20Sopenharmony_ci * Return: pointer to the file on success, ERR_PTR on failure
688c2ecf20Sopenharmony_ci */
698c2ecf20Sopenharmony_cistatic struct file *ocxlflash_getfile(struct device *dev, const char *name,
708c2ecf20Sopenharmony_ci				      const struct file_operations *fops,
718c2ecf20Sopenharmony_ci				      void *priv, int flags)
728c2ecf20Sopenharmony_ci{
738c2ecf20Sopenharmony_ci	struct file *file;
748c2ecf20Sopenharmony_ci	struct inode *inode;
758c2ecf20Sopenharmony_ci	int rc;
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci	if (fops->owner && !try_module_get(fops->owner)) {
788c2ecf20Sopenharmony_ci		dev_err(dev, "%s: Owner does not exist\n", __func__);
798c2ecf20Sopenharmony_ci		rc = -ENOENT;
808c2ecf20Sopenharmony_ci		goto err1;
818c2ecf20Sopenharmony_ci	}
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci	rc = simple_pin_fs(&ocxlflash_fs_type, &ocxlflash_vfs_mount,
848c2ecf20Sopenharmony_ci			   &ocxlflash_fs_cnt);
858c2ecf20Sopenharmony_ci	if (unlikely(rc < 0)) {
868c2ecf20Sopenharmony_ci		dev_err(dev, "%s: Cannot mount ocxlflash pseudofs rc=%d\n",
878c2ecf20Sopenharmony_ci			__func__, rc);
888c2ecf20Sopenharmony_ci		goto err2;
898c2ecf20Sopenharmony_ci	}
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci	inode = alloc_anon_inode(ocxlflash_vfs_mount->mnt_sb);
928c2ecf20Sopenharmony_ci	if (IS_ERR(inode)) {
938c2ecf20Sopenharmony_ci		rc = PTR_ERR(inode);
948c2ecf20Sopenharmony_ci		dev_err(dev, "%s: alloc_anon_inode failed rc=%d\n",
958c2ecf20Sopenharmony_ci			__func__, rc);
968c2ecf20Sopenharmony_ci		goto err3;
978c2ecf20Sopenharmony_ci	}
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci	file = alloc_file_pseudo(inode, ocxlflash_vfs_mount, name,
1008c2ecf20Sopenharmony_ci				 flags & (O_ACCMODE | O_NONBLOCK), fops);
1018c2ecf20Sopenharmony_ci	if (IS_ERR(file)) {
1028c2ecf20Sopenharmony_ci		rc = PTR_ERR(file);
1038c2ecf20Sopenharmony_ci		dev_err(dev, "%s: alloc_file failed rc=%d\n",
1048c2ecf20Sopenharmony_ci			__func__, rc);
1058c2ecf20Sopenharmony_ci		goto err4;
1068c2ecf20Sopenharmony_ci	}
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci	file->private_data = priv;
1098c2ecf20Sopenharmony_ciout:
1108c2ecf20Sopenharmony_ci	return file;
1118c2ecf20Sopenharmony_cierr4:
1128c2ecf20Sopenharmony_ci	iput(inode);
1138c2ecf20Sopenharmony_cierr3:
1148c2ecf20Sopenharmony_ci	simple_release_fs(&ocxlflash_vfs_mount, &ocxlflash_fs_cnt);
1158c2ecf20Sopenharmony_cierr2:
1168c2ecf20Sopenharmony_ci	module_put(fops->owner);
1178c2ecf20Sopenharmony_cierr1:
1188c2ecf20Sopenharmony_ci	file = ERR_PTR(rc);
1198c2ecf20Sopenharmony_ci	goto out;
1208c2ecf20Sopenharmony_ci}
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci/**
1238c2ecf20Sopenharmony_ci * ocxlflash_psa_map() - map the process specific MMIO space
1248c2ecf20Sopenharmony_ci * @ctx_cookie:	Adapter context for which the mapping needs to be done.
1258c2ecf20Sopenharmony_ci *
1268c2ecf20Sopenharmony_ci * Return: MMIO pointer of the mapped region
1278c2ecf20Sopenharmony_ci */
1288c2ecf20Sopenharmony_cistatic void __iomem *ocxlflash_psa_map(void *ctx_cookie)
1298c2ecf20Sopenharmony_ci{
1308c2ecf20Sopenharmony_ci	struct ocxlflash_context *ctx = ctx_cookie;
1318c2ecf20Sopenharmony_ci	struct device *dev = ctx->hw_afu->dev;
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci	mutex_lock(&ctx->state_mutex);
1348c2ecf20Sopenharmony_ci	if (ctx->state != STARTED) {
1358c2ecf20Sopenharmony_ci		dev_err(dev, "%s: Context not started, state=%d\n", __func__,
1368c2ecf20Sopenharmony_ci			ctx->state);
1378c2ecf20Sopenharmony_ci		mutex_unlock(&ctx->state_mutex);
1388c2ecf20Sopenharmony_ci		return NULL;
1398c2ecf20Sopenharmony_ci	}
1408c2ecf20Sopenharmony_ci	mutex_unlock(&ctx->state_mutex);
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci	return ioremap(ctx->psn_phys, ctx->psn_size);
1438c2ecf20Sopenharmony_ci}
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci/**
1468c2ecf20Sopenharmony_ci * ocxlflash_psa_unmap() - unmap the process specific MMIO space
1478c2ecf20Sopenharmony_ci * @addr:	MMIO pointer to unmap.
1488c2ecf20Sopenharmony_ci */
1498c2ecf20Sopenharmony_cistatic void ocxlflash_psa_unmap(void __iomem *addr)
1508c2ecf20Sopenharmony_ci{
1518c2ecf20Sopenharmony_ci	iounmap(addr);
1528c2ecf20Sopenharmony_ci}
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci/**
1558c2ecf20Sopenharmony_ci * ocxlflash_process_element() - get process element of the adapter context
1568c2ecf20Sopenharmony_ci * @ctx_cookie:	Adapter context associated with the process element.
1578c2ecf20Sopenharmony_ci *
1588c2ecf20Sopenharmony_ci * Return: process element of the adapter context
1598c2ecf20Sopenharmony_ci */
1608c2ecf20Sopenharmony_cistatic int ocxlflash_process_element(void *ctx_cookie)
1618c2ecf20Sopenharmony_ci{
1628c2ecf20Sopenharmony_ci	struct ocxlflash_context *ctx = ctx_cookie;
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci	return ctx->pe;
1658c2ecf20Sopenharmony_ci}
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ci/**
1688c2ecf20Sopenharmony_ci * afu_map_irq() - map the interrupt of the adapter context
1698c2ecf20Sopenharmony_ci * @flags:	Flags.
1708c2ecf20Sopenharmony_ci * @ctx:	Adapter context.
1718c2ecf20Sopenharmony_ci * @num:	Per-context AFU interrupt number.
1728c2ecf20Sopenharmony_ci * @handler:	Interrupt handler to register.
1738c2ecf20Sopenharmony_ci * @cookie:	Interrupt handler private data.
1748c2ecf20Sopenharmony_ci * @name:	Name of the interrupt.
1758c2ecf20Sopenharmony_ci *
1768c2ecf20Sopenharmony_ci * Return: 0 on success, -errno on failure
1778c2ecf20Sopenharmony_ci */
1788c2ecf20Sopenharmony_cistatic int afu_map_irq(u64 flags, struct ocxlflash_context *ctx, int num,
1798c2ecf20Sopenharmony_ci		       irq_handler_t handler, void *cookie, char *name)
1808c2ecf20Sopenharmony_ci{
1818c2ecf20Sopenharmony_ci	struct ocxl_hw_afu *afu = ctx->hw_afu;
1828c2ecf20Sopenharmony_ci	struct device *dev = afu->dev;
1838c2ecf20Sopenharmony_ci	struct ocxlflash_irqs *irq;
1848c2ecf20Sopenharmony_ci	struct xive_irq_data *xd;
1858c2ecf20Sopenharmony_ci	u32 virq;
1868c2ecf20Sopenharmony_ci	int rc = 0;
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci	if (num < 0 || num >= ctx->num_irqs) {
1898c2ecf20Sopenharmony_ci		dev_err(dev, "%s: Interrupt %d not allocated\n", __func__, num);
1908c2ecf20Sopenharmony_ci		rc = -ENOENT;
1918c2ecf20Sopenharmony_ci		goto out;
1928c2ecf20Sopenharmony_ci	}
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci	irq = &ctx->irqs[num];
1958c2ecf20Sopenharmony_ci	virq = irq_create_mapping(NULL, irq->hwirq);
1968c2ecf20Sopenharmony_ci	if (unlikely(!virq)) {
1978c2ecf20Sopenharmony_ci		dev_err(dev, "%s: irq_create_mapping failed\n", __func__);
1988c2ecf20Sopenharmony_ci		rc = -ENOMEM;
1998c2ecf20Sopenharmony_ci		goto out;
2008c2ecf20Sopenharmony_ci	}
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_ci	rc = request_irq(virq, handler, 0, name, cookie);
2038c2ecf20Sopenharmony_ci	if (unlikely(rc)) {
2048c2ecf20Sopenharmony_ci		dev_err(dev, "%s: request_irq failed rc=%d\n", __func__, rc);
2058c2ecf20Sopenharmony_ci		goto err1;
2068c2ecf20Sopenharmony_ci	}
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_ci	xd = irq_get_handler_data(virq);
2098c2ecf20Sopenharmony_ci	if (unlikely(!xd)) {
2108c2ecf20Sopenharmony_ci		dev_err(dev, "%s: Can't get interrupt data\n", __func__);
2118c2ecf20Sopenharmony_ci		rc = -ENXIO;
2128c2ecf20Sopenharmony_ci		goto err2;
2138c2ecf20Sopenharmony_ci	}
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_ci	irq->virq = virq;
2168c2ecf20Sopenharmony_ci	irq->vtrig = xd->trig_mmio;
2178c2ecf20Sopenharmony_ciout:
2188c2ecf20Sopenharmony_ci	return rc;
2198c2ecf20Sopenharmony_cierr2:
2208c2ecf20Sopenharmony_ci	free_irq(virq, cookie);
2218c2ecf20Sopenharmony_cierr1:
2228c2ecf20Sopenharmony_ci	irq_dispose_mapping(virq);
2238c2ecf20Sopenharmony_ci	goto out;
2248c2ecf20Sopenharmony_ci}
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ci/**
2278c2ecf20Sopenharmony_ci * ocxlflash_map_afu_irq() - map the interrupt of the adapter context
2288c2ecf20Sopenharmony_ci * @ctx_cookie:	Adapter context.
2298c2ecf20Sopenharmony_ci * @num:	Per-context AFU interrupt number.
2308c2ecf20Sopenharmony_ci * @handler:	Interrupt handler to register.
2318c2ecf20Sopenharmony_ci * @cookie:	Interrupt handler private data.
2328c2ecf20Sopenharmony_ci * @name:	Name of the interrupt.
2338c2ecf20Sopenharmony_ci *
2348c2ecf20Sopenharmony_ci * Return: 0 on success, -errno on failure
2358c2ecf20Sopenharmony_ci */
2368c2ecf20Sopenharmony_cistatic int ocxlflash_map_afu_irq(void *ctx_cookie, int num,
2378c2ecf20Sopenharmony_ci				 irq_handler_t handler, void *cookie,
2388c2ecf20Sopenharmony_ci				 char *name)
2398c2ecf20Sopenharmony_ci{
2408c2ecf20Sopenharmony_ci	return afu_map_irq(0, ctx_cookie, num, handler, cookie, name);
2418c2ecf20Sopenharmony_ci}
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_ci/**
2448c2ecf20Sopenharmony_ci * afu_unmap_irq() - unmap the interrupt
2458c2ecf20Sopenharmony_ci * @flags:	Flags.
2468c2ecf20Sopenharmony_ci * @ctx:	Adapter context.
2478c2ecf20Sopenharmony_ci * @num:	Per-context AFU interrupt number.
2488c2ecf20Sopenharmony_ci * @cookie:	Interrupt handler private data.
2498c2ecf20Sopenharmony_ci */
2508c2ecf20Sopenharmony_cistatic void afu_unmap_irq(u64 flags, struct ocxlflash_context *ctx, int num,
2518c2ecf20Sopenharmony_ci			  void *cookie)
2528c2ecf20Sopenharmony_ci{
2538c2ecf20Sopenharmony_ci	struct ocxl_hw_afu *afu = ctx->hw_afu;
2548c2ecf20Sopenharmony_ci	struct device *dev = afu->dev;
2558c2ecf20Sopenharmony_ci	struct ocxlflash_irqs *irq;
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_ci	if (num < 0 || num >= ctx->num_irqs) {
2588c2ecf20Sopenharmony_ci		dev_err(dev, "%s: Interrupt %d not allocated\n", __func__, num);
2598c2ecf20Sopenharmony_ci		return;
2608c2ecf20Sopenharmony_ci	}
2618c2ecf20Sopenharmony_ci
2628c2ecf20Sopenharmony_ci	irq = &ctx->irqs[num];
2638c2ecf20Sopenharmony_ci
2648c2ecf20Sopenharmony_ci	if (irq_find_mapping(NULL, irq->hwirq)) {
2658c2ecf20Sopenharmony_ci		free_irq(irq->virq, cookie);
2668c2ecf20Sopenharmony_ci		irq_dispose_mapping(irq->virq);
2678c2ecf20Sopenharmony_ci	}
2688c2ecf20Sopenharmony_ci
2698c2ecf20Sopenharmony_ci	memset(irq, 0, sizeof(*irq));
2708c2ecf20Sopenharmony_ci}
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_ci/**
2738c2ecf20Sopenharmony_ci * ocxlflash_unmap_afu_irq() - unmap the interrupt
2748c2ecf20Sopenharmony_ci * @ctx_cookie:	Adapter context.
2758c2ecf20Sopenharmony_ci * @num:	Per-context AFU interrupt number.
2768c2ecf20Sopenharmony_ci * @cookie:	Interrupt handler private data.
2778c2ecf20Sopenharmony_ci */
2788c2ecf20Sopenharmony_cistatic void ocxlflash_unmap_afu_irq(void *ctx_cookie, int num, void *cookie)
2798c2ecf20Sopenharmony_ci{
2808c2ecf20Sopenharmony_ci	return afu_unmap_irq(0, ctx_cookie, num, cookie);
2818c2ecf20Sopenharmony_ci}
2828c2ecf20Sopenharmony_ci
2838c2ecf20Sopenharmony_ci/**
2848c2ecf20Sopenharmony_ci * ocxlflash_get_irq_objhndl() - get the object handle for an interrupt
2858c2ecf20Sopenharmony_ci * @ctx_cookie:	Context associated with the interrupt.
2868c2ecf20Sopenharmony_ci * @irq:	Interrupt number.
2878c2ecf20Sopenharmony_ci *
2888c2ecf20Sopenharmony_ci * Return: effective address of the mapped region
2898c2ecf20Sopenharmony_ci */
2908c2ecf20Sopenharmony_cistatic u64 ocxlflash_get_irq_objhndl(void *ctx_cookie, int irq)
2918c2ecf20Sopenharmony_ci{
2928c2ecf20Sopenharmony_ci	struct ocxlflash_context *ctx = ctx_cookie;
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_ci	if (irq < 0 || irq >= ctx->num_irqs)
2958c2ecf20Sopenharmony_ci		return 0;
2968c2ecf20Sopenharmony_ci
2978c2ecf20Sopenharmony_ci	return (__force u64)ctx->irqs[irq].vtrig;
2988c2ecf20Sopenharmony_ci}
2998c2ecf20Sopenharmony_ci
3008c2ecf20Sopenharmony_ci/**
3018c2ecf20Sopenharmony_ci * ocxlflash_xsl_fault() - callback when translation error is triggered
3028c2ecf20Sopenharmony_ci * @data:	Private data provided at callback registration, the context.
3038c2ecf20Sopenharmony_ci * @addr:	Address that triggered the error.
3048c2ecf20Sopenharmony_ci * @dsisr:	Value of dsisr register.
3058c2ecf20Sopenharmony_ci */
3068c2ecf20Sopenharmony_cistatic void ocxlflash_xsl_fault(void *data, u64 addr, u64 dsisr)
3078c2ecf20Sopenharmony_ci{
3088c2ecf20Sopenharmony_ci	struct ocxlflash_context *ctx = data;
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_ci	spin_lock(&ctx->slock);
3118c2ecf20Sopenharmony_ci	ctx->fault_addr = addr;
3128c2ecf20Sopenharmony_ci	ctx->fault_dsisr = dsisr;
3138c2ecf20Sopenharmony_ci	ctx->pending_fault = true;
3148c2ecf20Sopenharmony_ci	spin_unlock(&ctx->slock);
3158c2ecf20Sopenharmony_ci
3168c2ecf20Sopenharmony_ci	wake_up_all(&ctx->wq);
3178c2ecf20Sopenharmony_ci}
3188c2ecf20Sopenharmony_ci
3198c2ecf20Sopenharmony_ci/**
3208c2ecf20Sopenharmony_ci * start_context() - local routine to start a context
3218c2ecf20Sopenharmony_ci * @ctx:	Adapter context to be started.
3228c2ecf20Sopenharmony_ci *
3238c2ecf20Sopenharmony_ci * Assign the context specific MMIO space, add and enable the PE.
3248c2ecf20Sopenharmony_ci *
3258c2ecf20Sopenharmony_ci * Return: 0 on success, -errno on failure
3268c2ecf20Sopenharmony_ci */
3278c2ecf20Sopenharmony_cistatic int start_context(struct ocxlflash_context *ctx)
3288c2ecf20Sopenharmony_ci{
3298c2ecf20Sopenharmony_ci	struct ocxl_hw_afu *afu = ctx->hw_afu;
3308c2ecf20Sopenharmony_ci	struct ocxl_afu_config *acfg = &afu->acfg;
3318c2ecf20Sopenharmony_ci	void *link_token = afu->link_token;
3328c2ecf20Sopenharmony_ci	struct device *dev = afu->dev;
3338c2ecf20Sopenharmony_ci	bool master = ctx->master;
3348c2ecf20Sopenharmony_ci	struct mm_struct *mm;
3358c2ecf20Sopenharmony_ci	int rc = 0;
3368c2ecf20Sopenharmony_ci	u32 pid;
3378c2ecf20Sopenharmony_ci
3388c2ecf20Sopenharmony_ci	mutex_lock(&ctx->state_mutex);
3398c2ecf20Sopenharmony_ci	if (ctx->state != OPENED) {
3408c2ecf20Sopenharmony_ci		dev_err(dev, "%s: Context state invalid, state=%d\n",
3418c2ecf20Sopenharmony_ci			__func__, ctx->state);
3428c2ecf20Sopenharmony_ci		rc = -EINVAL;
3438c2ecf20Sopenharmony_ci		goto out;
3448c2ecf20Sopenharmony_ci	}
3458c2ecf20Sopenharmony_ci
3468c2ecf20Sopenharmony_ci	if (master) {
3478c2ecf20Sopenharmony_ci		ctx->psn_size = acfg->global_mmio_size;
3488c2ecf20Sopenharmony_ci		ctx->psn_phys = afu->gmmio_phys;
3498c2ecf20Sopenharmony_ci	} else {
3508c2ecf20Sopenharmony_ci		ctx->psn_size = acfg->pp_mmio_stride;
3518c2ecf20Sopenharmony_ci		ctx->psn_phys = afu->ppmmio_phys + (ctx->pe * ctx->psn_size);
3528c2ecf20Sopenharmony_ci	}
3538c2ecf20Sopenharmony_ci
3548c2ecf20Sopenharmony_ci	/* pid and mm not set for master contexts */
3558c2ecf20Sopenharmony_ci	if (master) {
3568c2ecf20Sopenharmony_ci		pid = 0;
3578c2ecf20Sopenharmony_ci		mm = NULL;
3588c2ecf20Sopenharmony_ci	} else {
3598c2ecf20Sopenharmony_ci		pid = current->mm->context.id;
3608c2ecf20Sopenharmony_ci		mm = current->mm;
3618c2ecf20Sopenharmony_ci	}
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_ci	rc = ocxl_link_add_pe(link_token, ctx->pe, pid, 0, 0, mm,
3648c2ecf20Sopenharmony_ci			      ocxlflash_xsl_fault, ctx);
3658c2ecf20Sopenharmony_ci	if (unlikely(rc)) {
3668c2ecf20Sopenharmony_ci		dev_err(dev, "%s: ocxl_link_add_pe failed rc=%d\n",
3678c2ecf20Sopenharmony_ci			__func__, rc);
3688c2ecf20Sopenharmony_ci		goto out;
3698c2ecf20Sopenharmony_ci	}
3708c2ecf20Sopenharmony_ci
3718c2ecf20Sopenharmony_ci	ctx->state = STARTED;
3728c2ecf20Sopenharmony_ciout:
3738c2ecf20Sopenharmony_ci	mutex_unlock(&ctx->state_mutex);
3748c2ecf20Sopenharmony_ci	return rc;
3758c2ecf20Sopenharmony_ci}
3768c2ecf20Sopenharmony_ci
3778c2ecf20Sopenharmony_ci/**
3788c2ecf20Sopenharmony_ci * ocxlflash_start_context() - start a kernel context
3798c2ecf20Sopenharmony_ci * @ctx_cookie:	Adapter context to be started.
3808c2ecf20Sopenharmony_ci *
3818c2ecf20Sopenharmony_ci * Return: 0 on success, -errno on failure
3828c2ecf20Sopenharmony_ci */
3838c2ecf20Sopenharmony_cistatic int ocxlflash_start_context(void *ctx_cookie)
3848c2ecf20Sopenharmony_ci{
3858c2ecf20Sopenharmony_ci	struct ocxlflash_context *ctx = ctx_cookie;
3868c2ecf20Sopenharmony_ci
3878c2ecf20Sopenharmony_ci	return start_context(ctx);
3888c2ecf20Sopenharmony_ci}
3898c2ecf20Sopenharmony_ci
3908c2ecf20Sopenharmony_ci/**
3918c2ecf20Sopenharmony_ci * ocxlflash_stop_context() - stop a context
3928c2ecf20Sopenharmony_ci * @ctx_cookie:	Adapter context to be stopped.
3938c2ecf20Sopenharmony_ci *
3948c2ecf20Sopenharmony_ci * Return: 0 on success, -errno on failure
3958c2ecf20Sopenharmony_ci */
3968c2ecf20Sopenharmony_cistatic int ocxlflash_stop_context(void *ctx_cookie)
3978c2ecf20Sopenharmony_ci{
3988c2ecf20Sopenharmony_ci	struct ocxlflash_context *ctx = ctx_cookie;
3998c2ecf20Sopenharmony_ci	struct ocxl_hw_afu *afu = ctx->hw_afu;
4008c2ecf20Sopenharmony_ci	struct ocxl_afu_config *acfg = &afu->acfg;
4018c2ecf20Sopenharmony_ci	struct pci_dev *pdev = afu->pdev;
4028c2ecf20Sopenharmony_ci	struct device *dev = afu->dev;
4038c2ecf20Sopenharmony_ci	enum ocxlflash_ctx_state state;
4048c2ecf20Sopenharmony_ci	int rc = 0;
4058c2ecf20Sopenharmony_ci
4068c2ecf20Sopenharmony_ci	mutex_lock(&ctx->state_mutex);
4078c2ecf20Sopenharmony_ci	state = ctx->state;
4088c2ecf20Sopenharmony_ci	ctx->state = CLOSED;
4098c2ecf20Sopenharmony_ci	mutex_unlock(&ctx->state_mutex);
4108c2ecf20Sopenharmony_ci	if (state != STARTED)
4118c2ecf20Sopenharmony_ci		goto out;
4128c2ecf20Sopenharmony_ci
4138c2ecf20Sopenharmony_ci	rc = ocxl_config_terminate_pasid(pdev, acfg->dvsec_afu_control_pos,
4148c2ecf20Sopenharmony_ci					 ctx->pe);
4158c2ecf20Sopenharmony_ci	if (unlikely(rc)) {
4168c2ecf20Sopenharmony_ci		dev_err(dev, "%s: ocxl_config_terminate_pasid failed rc=%d\n",
4178c2ecf20Sopenharmony_ci			__func__, rc);
4188c2ecf20Sopenharmony_ci		/* If EBUSY, PE could be referenced in future by the AFU */
4198c2ecf20Sopenharmony_ci		if (rc == -EBUSY)
4208c2ecf20Sopenharmony_ci			goto out;
4218c2ecf20Sopenharmony_ci	}
4228c2ecf20Sopenharmony_ci
4238c2ecf20Sopenharmony_ci	rc = ocxl_link_remove_pe(afu->link_token, ctx->pe);
4248c2ecf20Sopenharmony_ci	if (unlikely(rc)) {
4258c2ecf20Sopenharmony_ci		dev_err(dev, "%s: ocxl_link_remove_pe failed rc=%d\n",
4268c2ecf20Sopenharmony_ci			__func__, rc);
4278c2ecf20Sopenharmony_ci		goto out;
4288c2ecf20Sopenharmony_ci	}
4298c2ecf20Sopenharmony_ciout:
4308c2ecf20Sopenharmony_ci	return rc;
4318c2ecf20Sopenharmony_ci}
4328c2ecf20Sopenharmony_ci
4338c2ecf20Sopenharmony_ci/**
4348c2ecf20Sopenharmony_ci * ocxlflash_afu_reset() - reset the AFU
4358c2ecf20Sopenharmony_ci * @ctx_cookie:	Adapter context.
4368c2ecf20Sopenharmony_ci */
4378c2ecf20Sopenharmony_cistatic int ocxlflash_afu_reset(void *ctx_cookie)
4388c2ecf20Sopenharmony_ci{
4398c2ecf20Sopenharmony_ci	struct ocxlflash_context *ctx = ctx_cookie;
4408c2ecf20Sopenharmony_ci	struct device *dev = ctx->hw_afu->dev;
4418c2ecf20Sopenharmony_ci
4428c2ecf20Sopenharmony_ci	/* Pending implementation from OCXL transport services */
4438c2ecf20Sopenharmony_ci	dev_err_once(dev, "%s: afu_reset() fop not supported\n", __func__);
4448c2ecf20Sopenharmony_ci
4458c2ecf20Sopenharmony_ci	/* Silently return success until it is implemented */
4468c2ecf20Sopenharmony_ci	return 0;
4478c2ecf20Sopenharmony_ci}
4488c2ecf20Sopenharmony_ci
4498c2ecf20Sopenharmony_ci/**
4508c2ecf20Sopenharmony_ci * ocxlflash_set_master() - sets the context as master
4518c2ecf20Sopenharmony_ci * @ctx_cookie:	Adapter context to set as master.
4528c2ecf20Sopenharmony_ci */
4538c2ecf20Sopenharmony_cistatic void ocxlflash_set_master(void *ctx_cookie)
4548c2ecf20Sopenharmony_ci{
4558c2ecf20Sopenharmony_ci	struct ocxlflash_context *ctx = ctx_cookie;
4568c2ecf20Sopenharmony_ci
4578c2ecf20Sopenharmony_ci	ctx->master = true;
4588c2ecf20Sopenharmony_ci}
4598c2ecf20Sopenharmony_ci
4608c2ecf20Sopenharmony_ci/**
4618c2ecf20Sopenharmony_ci * ocxlflash_get_context() - obtains the context associated with the host
4628c2ecf20Sopenharmony_ci * @pdev:	PCI device associated with the host.
4638c2ecf20Sopenharmony_ci * @afu_cookie:	Hardware AFU associated with the host.
4648c2ecf20Sopenharmony_ci *
4658c2ecf20Sopenharmony_ci * Return: returns the pointer to host adapter context
4668c2ecf20Sopenharmony_ci */
4678c2ecf20Sopenharmony_cistatic void *ocxlflash_get_context(struct pci_dev *pdev, void *afu_cookie)
4688c2ecf20Sopenharmony_ci{
4698c2ecf20Sopenharmony_ci	struct ocxl_hw_afu *afu = afu_cookie;
4708c2ecf20Sopenharmony_ci
4718c2ecf20Sopenharmony_ci	return afu->ocxl_ctx;
4728c2ecf20Sopenharmony_ci}
4738c2ecf20Sopenharmony_ci
4748c2ecf20Sopenharmony_ci/**
4758c2ecf20Sopenharmony_ci * ocxlflash_dev_context_init() - allocate and initialize an adapter context
4768c2ecf20Sopenharmony_ci * @pdev:	PCI device associated with the host.
4778c2ecf20Sopenharmony_ci * @afu_cookie:	Hardware AFU associated with the host.
4788c2ecf20Sopenharmony_ci *
4798c2ecf20Sopenharmony_ci * Return: returns the adapter context on success, ERR_PTR on failure
4808c2ecf20Sopenharmony_ci */
4818c2ecf20Sopenharmony_cistatic void *ocxlflash_dev_context_init(struct pci_dev *pdev, void *afu_cookie)
4828c2ecf20Sopenharmony_ci{
4838c2ecf20Sopenharmony_ci	struct ocxl_hw_afu *afu = afu_cookie;
4848c2ecf20Sopenharmony_ci	struct device *dev = afu->dev;
4858c2ecf20Sopenharmony_ci	struct ocxlflash_context *ctx;
4868c2ecf20Sopenharmony_ci	int rc;
4878c2ecf20Sopenharmony_ci
4888c2ecf20Sopenharmony_ci	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
4898c2ecf20Sopenharmony_ci	if (unlikely(!ctx)) {
4908c2ecf20Sopenharmony_ci		dev_err(dev, "%s: Context allocation failed\n", __func__);
4918c2ecf20Sopenharmony_ci		rc = -ENOMEM;
4928c2ecf20Sopenharmony_ci		goto err1;
4938c2ecf20Sopenharmony_ci	}
4948c2ecf20Sopenharmony_ci
4958c2ecf20Sopenharmony_ci	idr_preload(GFP_KERNEL);
4968c2ecf20Sopenharmony_ci	rc = idr_alloc(&afu->idr, ctx, 0, afu->max_pasid, GFP_NOWAIT);
4978c2ecf20Sopenharmony_ci	idr_preload_end();
4988c2ecf20Sopenharmony_ci	if (unlikely(rc < 0)) {
4998c2ecf20Sopenharmony_ci		dev_err(dev, "%s: idr_alloc failed rc=%d\n", __func__, rc);
5008c2ecf20Sopenharmony_ci		goto err2;
5018c2ecf20Sopenharmony_ci	}
5028c2ecf20Sopenharmony_ci
5038c2ecf20Sopenharmony_ci	spin_lock_init(&ctx->slock);
5048c2ecf20Sopenharmony_ci	init_waitqueue_head(&ctx->wq);
5058c2ecf20Sopenharmony_ci	mutex_init(&ctx->state_mutex);
5068c2ecf20Sopenharmony_ci
5078c2ecf20Sopenharmony_ci	ctx->state = OPENED;
5088c2ecf20Sopenharmony_ci	ctx->pe = rc;
5098c2ecf20Sopenharmony_ci	ctx->master = false;
5108c2ecf20Sopenharmony_ci	ctx->mapping = NULL;
5118c2ecf20Sopenharmony_ci	ctx->hw_afu = afu;
5128c2ecf20Sopenharmony_ci	ctx->irq_bitmap = 0;
5138c2ecf20Sopenharmony_ci	ctx->pending_irq = false;
5148c2ecf20Sopenharmony_ci	ctx->pending_fault = false;
5158c2ecf20Sopenharmony_ciout:
5168c2ecf20Sopenharmony_ci	return ctx;
5178c2ecf20Sopenharmony_cierr2:
5188c2ecf20Sopenharmony_ci	kfree(ctx);
5198c2ecf20Sopenharmony_cierr1:
5208c2ecf20Sopenharmony_ci	ctx = ERR_PTR(rc);
5218c2ecf20Sopenharmony_ci	goto out;
5228c2ecf20Sopenharmony_ci}
5238c2ecf20Sopenharmony_ci
5248c2ecf20Sopenharmony_ci/**
5258c2ecf20Sopenharmony_ci * ocxlflash_release_context() - releases an adapter context
5268c2ecf20Sopenharmony_ci * @ctx_cookie:	Adapter context to be released.
5278c2ecf20Sopenharmony_ci *
5288c2ecf20Sopenharmony_ci * Return: 0 on success, -errno on failure
5298c2ecf20Sopenharmony_ci */
5308c2ecf20Sopenharmony_cistatic int ocxlflash_release_context(void *ctx_cookie)
5318c2ecf20Sopenharmony_ci{
5328c2ecf20Sopenharmony_ci	struct ocxlflash_context *ctx = ctx_cookie;
5338c2ecf20Sopenharmony_ci	struct device *dev;
5348c2ecf20Sopenharmony_ci	int rc = 0;
5358c2ecf20Sopenharmony_ci
5368c2ecf20Sopenharmony_ci	if (!ctx)
5378c2ecf20Sopenharmony_ci		goto out;
5388c2ecf20Sopenharmony_ci
5398c2ecf20Sopenharmony_ci	dev = ctx->hw_afu->dev;
5408c2ecf20Sopenharmony_ci	mutex_lock(&ctx->state_mutex);
5418c2ecf20Sopenharmony_ci	if (ctx->state >= STARTED) {
5428c2ecf20Sopenharmony_ci		dev_err(dev, "%s: Context in use, state=%d\n", __func__,
5438c2ecf20Sopenharmony_ci			ctx->state);
5448c2ecf20Sopenharmony_ci		mutex_unlock(&ctx->state_mutex);
5458c2ecf20Sopenharmony_ci		rc = -EBUSY;
5468c2ecf20Sopenharmony_ci		goto out;
5478c2ecf20Sopenharmony_ci	}
5488c2ecf20Sopenharmony_ci	mutex_unlock(&ctx->state_mutex);
5498c2ecf20Sopenharmony_ci
5508c2ecf20Sopenharmony_ci	idr_remove(&ctx->hw_afu->idr, ctx->pe);
5518c2ecf20Sopenharmony_ci	ocxlflash_release_mapping(ctx);
5528c2ecf20Sopenharmony_ci	kfree(ctx);
5538c2ecf20Sopenharmony_ciout:
5548c2ecf20Sopenharmony_ci	return rc;
5558c2ecf20Sopenharmony_ci}
5568c2ecf20Sopenharmony_ci
5578c2ecf20Sopenharmony_ci/**
5588c2ecf20Sopenharmony_ci * ocxlflash_perst_reloads_same_image() - sets the image reload policy
5598c2ecf20Sopenharmony_ci * @afu_cookie:	Hardware AFU associated with the host.
5608c2ecf20Sopenharmony_ci * @image:	Whether to load the same image on PERST.
5618c2ecf20Sopenharmony_ci */
5628c2ecf20Sopenharmony_cistatic void ocxlflash_perst_reloads_same_image(void *afu_cookie, bool image)
5638c2ecf20Sopenharmony_ci{
5648c2ecf20Sopenharmony_ci	struct ocxl_hw_afu *afu = afu_cookie;
5658c2ecf20Sopenharmony_ci
5668c2ecf20Sopenharmony_ci	afu->perst_same_image = image;
5678c2ecf20Sopenharmony_ci}
5688c2ecf20Sopenharmony_ci
5698c2ecf20Sopenharmony_ci/**
5708c2ecf20Sopenharmony_ci * ocxlflash_read_adapter_vpd() - reads the adapter VPD
5718c2ecf20Sopenharmony_ci * @pdev:	PCI device associated with the host.
5728c2ecf20Sopenharmony_ci * @buf:	Buffer to get the VPD data.
5738c2ecf20Sopenharmony_ci * @count:	Size of buffer (maximum bytes that can be read).
5748c2ecf20Sopenharmony_ci *
5758c2ecf20Sopenharmony_ci * Return: size of VPD on success, -errno on failure
5768c2ecf20Sopenharmony_ci */
5778c2ecf20Sopenharmony_cistatic ssize_t ocxlflash_read_adapter_vpd(struct pci_dev *pdev, void *buf,
5788c2ecf20Sopenharmony_ci					  size_t count)
5798c2ecf20Sopenharmony_ci{
5808c2ecf20Sopenharmony_ci	return pci_read_vpd(pdev, 0, count, buf);
5818c2ecf20Sopenharmony_ci}
5828c2ecf20Sopenharmony_ci
5838c2ecf20Sopenharmony_ci/**
5848c2ecf20Sopenharmony_ci * free_afu_irqs() - internal service to free interrupts
5858c2ecf20Sopenharmony_ci * @ctx:	Adapter context.
5868c2ecf20Sopenharmony_ci */
5878c2ecf20Sopenharmony_cistatic void free_afu_irqs(struct ocxlflash_context *ctx)
5888c2ecf20Sopenharmony_ci{
5898c2ecf20Sopenharmony_ci	struct ocxl_hw_afu *afu = ctx->hw_afu;
5908c2ecf20Sopenharmony_ci	struct device *dev = afu->dev;
5918c2ecf20Sopenharmony_ci	int i;
5928c2ecf20Sopenharmony_ci
5938c2ecf20Sopenharmony_ci	if (!ctx->irqs) {
5948c2ecf20Sopenharmony_ci		dev_err(dev, "%s: Interrupts not allocated\n", __func__);
5958c2ecf20Sopenharmony_ci		return;
5968c2ecf20Sopenharmony_ci	}
5978c2ecf20Sopenharmony_ci
5988c2ecf20Sopenharmony_ci	for (i = ctx->num_irqs; i >= 0; i--)
5998c2ecf20Sopenharmony_ci		ocxl_link_free_irq(afu->link_token, ctx->irqs[i].hwirq);
6008c2ecf20Sopenharmony_ci
6018c2ecf20Sopenharmony_ci	kfree(ctx->irqs);
6028c2ecf20Sopenharmony_ci	ctx->irqs = NULL;
6038c2ecf20Sopenharmony_ci}
6048c2ecf20Sopenharmony_ci
6058c2ecf20Sopenharmony_ci/**
6068c2ecf20Sopenharmony_ci * alloc_afu_irqs() - internal service to allocate interrupts
6078c2ecf20Sopenharmony_ci * @ctx:	Context associated with the request.
6088c2ecf20Sopenharmony_ci * @num:	Number of interrupts requested.
6098c2ecf20Sopenharmony_ci *
6108c2ecf20Sopenharmony_ci * Return: 0 on success, -errno on failure
6118c2ecf20Sopenharmony_ci */
6128c2ecf20Sopenharmony_cistatic int alloc_afu_irqs(struct ocxlflash_context *ctx, int num)
6138c2ecf20Sopenharmony_ci{
6148c2ecf20Sopenharmony_ci	struct ocxl_hw_afu *afu = ctx->hw_afu;
6158c2ecf20Sopenharmony_ci	struct device *dev = afu->dev;
6168c2ecf20Sopenharmony_ci	struct ocxlflash_irqs *irqs;
6178c2ecf20Sopenharmony_ci	int rc = 0;
6188c2ecf20Sopenharmony_ci	int hwirq;
6198c2ecf20Sopenharmony_ci	int i;
6208c2ecf20Sopenharmony_ci
6218c2ecf20Sopenharmony_ci	if (ctx->irqs) {
6228c2ecf20Sopenharmony_ci		dev_err(dev, "%s: Interrupts already allocated\n", __func__);
6238c2ecf20Sopenharmony_ci		rc = -EEXIST;
6248c2ecf20Sopenharmony_ci		goto out;
6258c2ecf20Sopenharmony_ci	}
6268c2ecf20Sopenharmony_ci
6278c2ecf20Sopenharmony_ci	if (num > OCXL_MAX_IRQS) {
6288c2ecf20Sopenharmony_ci		dev_err(dev, "%s: Too many interrupts num=%d\n", __func__, num);
6298c2ecf20Sopenharmony_ci		rc = -EINVAL;
6308c2ecf20Sopenharmony_ci		goto out;
6318c2ecf20Sopenharmony_ci	}
6328c2ecf20Sopenharmony_ci
6338c2ecf20Sopenharmony_ci	irqs = kcalloc(num, sizeof(*irqs), GFP_KERNEL);
6348c2ecf20Sopenharmony_ci	if (unlikely(!irqs)) {
6358c2ecf20Sopenharmony_ci		dev_err(dev, "%s: Context irqs allocation failed\n", __func__);
6368c2ecf20Sopenharmony_ci		rc = -ENOMEM;
6378c2ecf20Sopenharmony_ci		goto out;
6388c2ecf20Sopenharmony_ci	}
6398c2ecf20Sopenharmony_ci
6408c2ecf20Sopenharmony_ci	for (i = 0; i < num; i++) {
6418c2ecf20Sopenharmony_ci		rc = ocxl_link_irq_alloc(afu->link_token, &hwirq);
6428c2ecf20Sopenharmony_ci		if (unlikely(rc)) {
6438c2ecf20Sopenharmony_ci			dev_err(dev, "%s: ocxl_link_irq_alloc failed rc=%d\n",
6448c2ecf20Sopenharmony_ci				__func__, rc);
6458c2ecf20Sopenharmony_ci			goto err;
6468c2ecf20Sopenharmony_ci		}
6478c2ecf20Sopenharmony_ci
6488c2ecf20Sopenharmony_ci		irqs[i].hwirq = hwirq;
6498c2ecf20Sopenharmony_ci	}
6508c2ecf20Sopenharmony_ci
6518c2ecf20Sopenharmony_ci	ctx->irqs = irqs;
6528c2ecf20Sopenharmony_ci	ctx->num_irqs = num;
6538c2ecf20Sopenharmony_ciout:
6548c2ecf20Sopenharmony_ci	return rc;
6558c2ecf20Sopenharmony_cierr:
6568c2ecf20Sopenharmony_ci	for (i = i-1; i >= 0; i--)
6578c2ecf20Sopenharmony_ci		ocxl_link_free_irq(afu->link_token, irqs[i].hwirq);
6588c2ecf20Sopenharmony_ci	kfree(irqs);
6598c2ecf20Sopenharmony_ci	goto out;
6608c2ecf20Sopenharmony_ci}
6618c2ecf20Sopenharmony_ci
6628c2ecf20Sopenharmony_ci/**
6638c2ecf20Sopenharmony_ci * ocxlflash_allocate_afu_irqs() - allocates the requested number of interrupts
6648c2ecf20Sopenharmony_ci * @ctx_cookie:	Context associated with the request.
6658c2ecf20Sopenharmony_ci * @num:	Number of interrupts requested.
6668c2ecf20Sopenharmony_ci *
6678c2ecf20Sopenharmony_ci * Return: 0 on success, -errno on failure
6688c2ecf20Sopenharmony_ci */
6698c2ecf20Sopenharmony_cistatic int ocxlflash_allocate_afu_irqs(void *ctx_cookie, int num)
6708c2ecf20Sopenharmony_ci{
6718c2ecf20Sopenharmony_ci	return alloc_afu_irqs(ctx_cookie, num);
6728c2ecf20Sopenharmony_ci}
6738c2ecf20Sopenharmony_ci
6748c2ecf20Sopenharmony_ci/**
6758c2ecf20Sopenharmony_ci * ocxlflash_free_afu_irqs() - frees the interrupts of an adapter context
6768c2ecf20Sopenharmony_ci * @ctx_cookie:	Adapter context.
6778c2ecf20Sopenharmony_ci */
6788c2ecf20Sopenharmony_cistatic void ocxlflash_free_afu_irqs(void *ctx_cookie)
6798c2ecf20Sopenharmony_ci{
6808c2ecf20Sopenharmony_ci	free_afu_irqs(ctx_cookie);
6818c2ecf20Sopenharmony_ci}
6828c2ecf20Sopenharmony_ci
6838c2ecf20Sopenharmony_ci/**
6848c2ecf20Sopenharmony_ci * ocxlflash_unconfig_afu() - unconfigure the AFU
6858c2ecf20Sopenharmony_ci * @afu: AFU associated with the host.
6868c2ecf20Sopenharmony_ci */
6878c2ecf20Sopenharmony_cistatic void ocxlflash_unconfig_afu(struct ocxl_hw_afu *afu)
6888c2ecf20Sopenharmony_ci{
6898c2ecf20Sopenharmony_ci	if (afu->gmmio_virt) {
6908c2ecf20Sopenharmony_ci		iounmap(afu->gmmio_virt);
6918c2ecf20Sopenharmony_ci		afu->gmmio_virt = NULL;
6928c2ecf20Sopenharmony_ci	}
6938c2ecf20Sopenharmony_ci}
6948c2ecf20Sopenharmony_ci
6958c2ecf20Sopenharmony_ci/**
6968c2ecf20Sopenharmony_ci * ocxlflash_destroy_afu() - destroy the AFU structure
6978c2ecf20Sopenharmony_ci * @afu_cookie:	AFU to be freed.
6988c2ecf20Sopenharmony_ci */
6998c2ecf20Sopenharmony_cistatic void ocxlflash_destroy_afu(void *afu_cookie)
7008c2ecf20Sopenharmony_ci{
7018c2ecf20Sopenharmony_ci	struct ocxl_hw_afu *afu = afu_cookie;
7028c2ecf20Sopenharmony_ci	int pos;
7038c2ecf20Sopenharmony_ci
7048c2ecf20Sopenharmony_ci	if (!afu)
7058c2ecf20Sopenharmony_ci		return;
7068c2ecf20Sopenharmony_ci
7078c2ecf20Sopenharmony_ci	ocxlflash_release_context(afu->ocxl_ctx);
7088c2ecf20Sopenharmony_ci	idr_destroy(&afu->idr);
7098c2ecf20Sopenharmony_ci
7108c2ecf20Sopenharmony_ci	/* Disable the AFU */
7118c2ecf20Sopenharmony_ci	pos = afu->acfg.dvsec_afu_control_pos;
7128c2ecf20Sopenharmony_ci	ocxl_config_set_afu_state(afu->pdev, pos, 0);
7138c2ecf20Sopenharmony_ci
7148c2ecf20Sopenharmony_ci	ocxlflash_unconfig_afu(afu);
7158c2ecf20Sopenharmony_ci	kfree(afu);
7168c2ecf20Sopenharmony_ci}
7178c2ecf20Sopenharmony_ci
7188c2ecf20Sopenharmony_ci/**
7198c2ecf20Sopenharmony_ci * ocxlflash_config_fn() - configure the host function
7208c2ecf20Sopenharmony_ci * @pdev:	PCI device associated with the host.
7218c2ecf20Sopenharmony_ci * @afu:	AFU associated with the host.
7228c2ecf20Sopenharmony_ci *
7238c2ecf20Sopenharmony_ci * Return: 0 on success, -errno on failure
7248c2ecf20Sopenharmony_ci */
7258c2ecf20Sopenharmony_cistatic int ocxlflash_config_fn(struct pci_dev *pdev, struct ocxl_hw_afu *afu)
7268c2ecf20Sopenharmony_ci{
7278c2ecf20Sopenharmony_ci	struct ocxl_fn_config *fcfg = &afu->fcfg;
7288c2ecf20Sopenharmony_ci	struct device *dev = &pdev->dev;
7298c2ecf20Sopenharmony_ci	u16 base, enabled, supported;
7308c2ecf20Sopenharmony_ci	int rc = 0;
7318c2ecf20Sopenharmony_ci
7328c2ecf20Sopenharmony_ci	/* Read DVSEC config of the function */
7338c2ecf20Sopenharmony_ci	rc = ocxl_config_read_function(pdev, fcfg);
7348c2ecf20Sopenharmony_ci	if (unlikely(rc)) {
7358c2ecf20Sopenharmony_ci		dev_err(dev, "%s: ocxl_config_read_function failed rc=%d\n",
7368c2ecf20Sopenharmony_ci			__func__, rc);
7378c2ecf20Sopenharmony_ci		goto out;
7388c2ecf20Sopenharmony_ci	}
7398c2ecf20Sopenharmony_ci
7408c2ecf20Sopenharmony_ci	/* Check if function has AFUs defined, only 1 per function supported */
7418c2ecf20Sopenharmony_ci	if (fcfg->max_afu_index >= 0) {
7428c2ecf20Sopenharmony_ci		afu->is_present = true;
7438c2ecf20Sopenharmony_ci		if (fcfg->max_afu_index != 0)
7448c2ecf20Sopenharmony_ci			dev_warn(dev, "%s: Unexpected AFU index value %d\n",
7458c2ecf20Sopenharmony_ci				 __func__, fcfg->max_afu_index);
7468c2ecf20Sopenharmony_ci	}
7478c2ecf20Sopenharmony_ci
7488c2ecf20Sopenharmony_ci	rc = ocxl_config_get_actag_info(pdev, &base, &enabled, &supported);
7498c2ecf20Sopenharmony_ci	if (unlikely(rc)) {
7508c2ecf20Sopenharmony_ci		dev_err(dev, "%s: ocxl_config_get_actag_info failed rc=%d\n",
7518c2ecf20Sopenharmony_ci			__func__, rc);
7528c2ecf20Sopenharmony_ci		goto out;
7538c2ecf20Sopenharmony_ci	}
7548c2ecf20Sopenharmony_ci
7558c2ecf20Sopenharmony_ci	afu->fn_actag_base = base;
7568c2ecf20Sopenharmony_ci	afu->fn_actag_enabled = enabled;
7578c2ecf20Sopenharmony_ci
7588c2ecf20Sopenharmony_ci	ocxl_config_set_actag(pdev, fcfg->dvsec_function_pos, base, enabled);
7598c2ecf20Sopenharmony_ci	dev_dbg(dev, "%s: Function acTag range base=%u enabled=%u\n",
7608c2ecf20Sopenharmony_ci		__func__, base, enabled);
7618c2ecf20Sopenharmony_ci
7628c2ecf20Sopenharmony_ci	rc = ocxl_link_setup(pdev, 0, &afu->link_token);
7638c2ecf20Sopenharmony_ci	if (unlikely(rc)) {
7648c2ecf20Sopenharmony_ci		dev_err(dev, "%s: ocxl_link_setup failed rc=%d\n",
7658c2ecf20Sopenharmony_ci			__func__, rc);
7668c2ecf20Sopenharmony_ci		goto out;
7678c2ecf20Sopenharmony_ci	}
7688c2ecf20Sopenharmony_ci
7698c2ecf20Sopenharmony_ci	rc = ocxl_config_set_TL(pdev, fcfg->dvsec_tl_pos);
7708c2ecf20Sopenharmony_ci	if (unlikely(rc)) {
7718c2ecf20Sopenharmony_ci		dev_err(dev, "%s: ocxl_config_set_TL failed rc=%d\n",
7728c2ecf20Sopenharmony_ci			__func__, rc);
7738c2ecf20Sopenharmony_ci		goto err;
7748c2ecf20Sopenharmony_ci	}
7758c2ecf20Sopenharmony_ciout:
7768c2ecf20Sopenharmony_ci	return rc;
7778c2ecf20Sopenharmony_cierr:
7788c2ecf20Sopenharmony_ci	ocxl_link_release(pdev, afu->link_token);
7798c2ecf20Sopenharmony_ci	goto out;
7808c2ecf20Sopenharmony_ci}
7818c2ecf20Sopenharmony_ci
7828c2ecf20Sopenharmony_ci/**
7838c2ecf20Sopenharmony_ci * ocxlflash_unconfig_fn() - unconfigure the host function
7848c2ecf20Sopenharmony_ci * @pdev:	PCI device associated with the host.
7858c2ecf20Sopenharmony_ci * @afu:	AFU associated with the host.
7868c2ecf20Sopenharmony_ci */
7878c2ecf20Sopenharmony_cistatic void ocxlflash_unconfig_fn(struct pci_dev *pdev, struct ocxl_hw_afu *afu)
7888c2ecf20Sopenharmony_ci{
7898c2ecf20Sopenharmony_ci	ocxl_link_release(pdev, afu->link_token);
7908c2ecf20Sopenharmony_ci}
7918c2ecf20Sopenharmony_ci
7928c2ecf20Sopenharmony_ci/**
7938c2ecf20Sopenharmony_ci * ocxlflash_map_mmio() - map the AFU MMIO space
7948c2ecf20Sopenharmony_ci * @afu: AFU associated with the host.
7958c2ecf20Sopenharmony_ci *
7968c2ecf20Sopenharmony_ci * Return: 0 on success, -errno on failure
7978c2ecf20Sopenharmony_ci */
7988c2ecf20Sopenharmony_cistatic int ocxlflash_map_mmio(struct ocxl_hw_afu *afu)
7998c2ecf20Sopenharmony_ci{
8008c2ecf20Sopenharmony_ci	struct ocxl_afu_config *acfg = &afu->acfg;
8018c2ecf20Sopenharmony_ci	struct pci_dev *pdev = afu->pdev;
8028c2ecf20Sopenharmony_ci	struct device *dev = afu->dev;
8038c2ecf20Sopenharmony_ci	phys_addr_t gmmio, ppmmio;
8048c2ecf20Sopenharmony_ci	int rc = 0;
8058c2ecf20Sopenharmony_ci
8068c2ecf20Sopenharmony_ci	rc = pci_request_region(pdev, acfg->global_mmio_bar, "ocxlflash");
8078c2ecf20Sopenharmony_ci	if (unlikely(rc)) {
8088c2ecf20Sopenharmony_ci		dev_err(dev, "%s: pci_request_region for global failed rc=%d\n",
8098c2ecf20Sopenharmony_ci			__func__, rc);
8108c2ecf20Sopenharmony_ci		goto out;
8118c2ecf20Sopenharmony_ci	}
8128c2ecf20Sopenharmony_ci	gmmio = pci_resource_start(pdev, acfg->global_mmio_bar);
8138c2ecf20Sopenharmony_ci	gmmio += acfg->global_mmio_offset;
8148c2ecf20Sopenharmony_ci
8158c2ecf20Sopenharmony_ci	rc = pci_request_region(pdev, acfg->pp_mmio_bar, "ocxlflash");
8168c2ecf20Sopenharmony_ci	if (unlikely(rc)) {
8178c2ecf20Sopenharmony_ci		dev_err(dev, "%s: pci_request_region for pp bar failed rc=%d\n",
8188c2ecf20Sopenharmony_ci			__func__, rc);
8198c2ecf20Sopenharmony_ci		goto err1;
8208c2ecf20Sopenharmony_ci	}
8218c2ecf20Sopenharmony_ci	ppmmio = pci_resource_start(pdev, acfg->pp_mmio_bar);
8228c2ecf20Sopenharmony_ci	ppmmio += acfg->pp_mmio_offset;
8238c2ecf20Sopenharmony_ci
8248c2ecf20Sopenharmony_ci	afu->gmmio_virt = ioremap(gmmio, acfg->global_mmio_size);
8258c2ecf20Sopenharmony_ci	if (unlikely(!afu->gmmio_virt)) {
8268c2ecf20Sopenharmony_ci		dev_err(dev, "%s: MMIO mapping failed\n", __func__);
8278c2ecf20Sopenharmony_ci		rc = -ENOMEM;
8288c2ecf20Sopenharmony_ci		goto err2;
8298c2ecf20Sopenharmony_ci	}
8308c2ecf20Sopenharmony_ci
8318c2ecf20Sopenharmony_ci	afu->gmmio_phys = gmmio;
8328c2ecf20Sopenharmony_ci	afu->ppmmio_phys = ppmmio;
8338c2ecf20Sopenharmony_ciout:
8348c2ecf20Sopenharmony_ci	return rc;
8358c2ecf20Sopenharmony_cierr2:
8368c2ecf20Sopenharmony_ci	pci_release_region(pdev, acfg->pp_mmio_bar);
8378c2ecf20Sopenharmony_cierr1:
8388c2ecf20Sopenharmony_ci	pci_release_region(pdev, acfg->global_mmio_bar);
8398c2ecf20Sopenharmony_ci	goto out;
8408c2ecf20Sopenharmony_ci}
8418c2ecf20Sopenharmony_ci
8428c2ecf20Sopenharmony_ci/**
8438c2ecf20Sopenharmony_ci * ocxlflash_config_afu() - configure the host AFU
8448c2ecf20Sopenharmony_ci * @pdev:	PCI device associated with the host.
8458c2ecf20Sopenharmony_ci * @afu:	AFU associated with the host.
8468c2ecf20Sopenharmony_ci *
8478c2ecf20Sopenharmony_ci * Must be called _after_ host function configuration.
8488c2ecf20Sopenharmony_ci *
8498c2ecf20Sopenharmony_ci * Return: 0 on success, -errno on failure
8508c2ecf20Sopenharmony_ci */
8518c2ecf20Sopenharmony_cistatic int ocxlflash_config_afu(struct pci_dev *pdev, struct ocxl_hw_afu *afu)
8528c2ecf20Sopenharmony_ci{
8538c2ecf20Sopenharmony_ci	struct ocxl_afu_config *acfg = &afu->acfg;
8548c2ecf20Sopenharmony_ci	struct ocxl_fn_config *fcfg = &afu->fcfg;
8558c2ecf20Sopenharmony_ci	struct device *dev = &pdev->dev;
8568c2ecf20Sopenharmony_ci	int count;
8578c2ecf20Sopenharmony_ci	int base;
8588c2ecf20Sopenharmony_ci	int pos;
8598c2ecf20Sopenharmony_ci	int rc = 0;
8608c2ecf20Sopenharmony_ci
8618c2ecf20Sopenharmony_ci	/* This HW AFU function does not have any AFUs defined */
8628c2ecf20Sopenharmony_ci	if (!afu->is_present)
8638c2ecf20Sopenharmony_ci		goto out;
8648c2ecf20Sopenharmony_ci
8658c2ecf20Sopenharmony_ci	/* Read AFU config at index 0 */
8668c2ecf20Sopenharmony_ci	rc = ocxl_config_read_afu(pdev, fcfg, acfg, 0);
8678c2ecf20Sopenharmony_ci	if (unlikely(rc)) {
8688c2ecf20Sopenharmony_ci		dev_err(dev, "%s: ocxl_config_read_afu failed rc=%d\n",
8698c2ecf20Sopenharmony_ci			__func__, rc);
8708c2ecf20Sopenharmony_ci		goto out;
8718c2ecf20Sopenharmony_ci	}
8728c2ecf20Sopenharmony_ci
8738c2ecf20Sopenharmony_ci	/* Only one AFU per function is supported, so actag_base is same */
8748c2ecf20Sopenharmony_ci	base = afu->fn_actag_base;
8758c2ecf20Sopenharmony_ci	count = min_t(int, acfg->actag_supported, afu->fn_actag_enabled);
8768c2ecf20Sopenharmony_ci	pos = acfg->dvsec_afu_control_pos;
8778c2ecf20Sopenharmony_ci
8788c2ecf20Sopenharmony_ci	ocxl_config_set_afu_actag(pdev, pos, base, count);
8798c2ecf20Sopenharmony_ci	dev_dbg(dev, "%s: acTag base=%d enabled=%d\n", __func__, base, count);
8808c2ecf20Sopenharmony_ci	afu->afu_actag_base = base;
8818c2ecf20Sopenharmony_ci	afu->afu_actag_enabled = count;
8828c2ecf20Sopenharmony_ci	afu->max_pasid = 1 << acfg->pasid_supported_log;
8838c2ecf20Sopenharmony_ci
8848c2ecf20Sopenharmony_ci	ocxl_config_set_afu_pasid(pdev, pos, 0, acfg->pasid_supported_log);
8858c2ecf20Sopenharmony_ci
8868c2ecf20Sopenharmony_ci	rc = ocxlflash_map_mmio(afu);
8878c2ecf20Sopenharmony_ci	if (unlikely(rc)) {
8888c2ecf20Sopenharmony_ci		dev_err(dev, "%s: ocxlflash_map_mmio failed rc=%d\n",
8898c2ecf20Sopenharmony_ci			__func__, rc);
8908c2ecf20Sopenharmony_ci		goto out;
8918c2ecf20Sopenharmony_ci	}
8928c2ecf20Sopenharmony_ci
8938c2ecf20Sopenharmony_ci	/* Enable the AFU */
8948c2ecf20Sopenharmony_ci	ocxl_config_set_afu_state(pdev, acfg->dvsec_afu_control_pos, 1);
8958c2ecf20Sopenharmony_ciout:
8968c2ecf20Sopenharmony_ci	return rc;
8978c2ecf20Sopenharmony_ci}
8988c2ecf20Sopenharmony_ci
8998c2ecf20Sopenharmony_ci/**
9008c2ecf20Sopenharmony_ci * ocxlflash_create_afu() - create the AFU for OCXL
9018c2ecf20Sopenharmony_ci * @pdev:	PCI device associated with the host.
9028c2ecf20Sopenharmony_ci *
9038c2ecf20Sopenharmony_ci * Return: AFU on success, NULL on failure
9048c2ecf20Sopenharmony_ci */
9058c2ecf20Sopenharmony_cistatic void *ocxlflash_create_afu(struct pci_dev *pdev)
9068c2ecf20Sopenharmony_ci{
9078c2ecf20Sopenharmony_ci	struct device *dev = &pdev->dev;
9088c2ecf20Sopenharmony_ci	struct ocxlflash_context *ctx;
9098c2ecf20Sopenharmony_ci	struct ocxl_hw_afu *afu;
9108c2ecf20Sopenharmony_ci	int rc;
9118c2ecf20Sopenharmony_ci
9128c2ecf20Sopenharmony_ci	afu = kzalloc(sizeof(*afu), GFP_KERNEL);
9138c2ecf20Sopenharmony_ci	if (unlikely(!afu)) {
9148c2ecf20Sopenharmony_ci		dev_err(dev, "%s: HW AFU allocation failed\n", __func__);
9158c2ecf20Sopenharmony_ci		goto out;
9168c2ecf20Sopenharmony_ci	}
9178c2ecf20Sopenharmony_ci
9188c2ecf20Sopenharmony_ci	afu->pdev = pdev;
9198c2ecf20Sopenharmony_ci	afu->dev = dev;
9208c2ecf20Sopenharmony_ci	idr_init(&afu->idr);
9218c2ecf20Sopenharmony_ci
9228c2ecf20Sopenharmony_ci	rc = ocxlflash_config_fn(pdev, afu);
9238c2ecf20Sopenharmony_ci	if (unlikely(rc)) {
9248c2ecf20Sopenharmony_ci		dev_err(dev, "%s: Function configuration failed rc=%d\n",
9258c2ecf20Sopenharmony_ci			__func__, rc);
9268c2ecf20Sopenharmony_ci		goto err1;
9278c2ecf20Sopenharmony_ci	}
9288c2ecf20Sopenharmony_ci
9298c2ecf20Sopenharmony_ci	rc = ocxlflash_config_afu(pdev, afu);
9308c2ecf20Sopenharmony_ci	if (unlikely(rc)) {
9318c2ecf20Sopenharmony_ci		dev_err(dev, "%s: AFU configuration failed rc=%d\n",
9328c2ecf20Sopenharmony_ci			__func__, rc);
9338c2ecf20Sopenharmony_ci		goto err2;
9348c2ecf20Sopenharmony_ci	}
9358c2ecf20Sopenharmony_ci
9368c2ecf20Sopenharmony_ci	ctx = ocxlflash_dev_context_init(pdev, afu);
9378c2ecf20Sopenharmony_ci	if (IS_ERR(ctx)) {
9388c2ecf20Sopenharmony_ci		rc = PTR_ERR(ctx);
9398c2ecf20Sopenharmony_ci		dev_err(dev, "%s: ocxlflash_dev_context_init failed rc=%d\n",
9408c2ecf20Sopenharmony_ci			__func__, rc);
9418c2ecf20Sopenharmony_ci		goto err3;
9428c2ecf20Sopenharmony_ci	}
9438c2ecf20Sopenharmony_ci
9448c2ecf20Sopenharmony_ci	afu->ocxl_ctx = ctx;
9458c2ecf20Sopenharmony_ciout:
9468c2ecf20Sopenharmony_ci	return afu;
9478c2ecf20Sopenharmony_cierr3:
9488c2ecf20Sopenharmony_ci	ocxlflash_unconfig_afu(afu);
9498c2ecf20Sopenharmony_cierr2:
9508c2ecf20Sopenharmony_ci	ocxlflash_unconfig_fn(pdev, afu);
9518c2ecf20Sopenharmony_cierr1:
9528c2ecf20Sopenharmony_ci	idr_destroy(&afu->idr);
9538c2ecf20Sopenharmony_ci	kfree(afu);
9548c2ecf20Sopenharmony_ci	afu = NULL;
9558c2ecf20Sopenharmony_ci	goto out;
9568c2ecf20Sopenharmony_ci}
9578c2ecf20Sopenharmony_ci
9588c2ecf20Sopenharmony_ci/**
9598c2ecf20Sopenharmony_ci * ctx_event_pending() - check for any event pending on the context
9608c2ecf20Sopenharmony_ci * @ctx:	Context to be checked.
9618c2ecf20Sopenharmony_ci *
9628c2ecf20Sopenharmony_ci * Return: true if there is an event pending, false if none pending
9638c2ecf20Sopenharmony_ci */
9648c2ecf20Sopenharmony_cistatic inline bool ctx_event_pending(struct ocxlflash_context *ctx)
9658c2ecf20Sopenharmony_ci{
9668c2ecf20Sopenharmony_ci	if (ctx->pending_irq || ctx->pending_fault)
9678c2ecf20Sopenharmony_ci		return true;
9688c2ecf20Sopenharmony_ci
9698c2ecf20Sopenharmony_ci	return false;
9708c2ecf20Sopenharmony_ci}
9718c2ecf20Sopenharmony_ci
9728c2ecf20Sopenharmony_ci/**
9738c2ecf20Sopenharmony_ci * afu_poll() - poll the AFU for events on the context
9748c2ecf20Sopenharmony_ci * @file:	File associated with the adapter context.
9758c2ecf20Sopenharmony_ci * @poll:	Poll structure from the user.
9768c2ecf20Sopenharmony_ci *
9778c2ecf20Sopenharmony_ci * Return: poll mask
9788c2ecf20Sopenharmony_ci */
9798c2ecf20Sopenharmony_cistatic unsigned int afu_poll(struct file *file, struct poll_table_struct *poll)
9808c2ecf20Sopenharmony_ci{
9818c2ecf20Sopenharmony_ci	struct ocxlflash_context *ctx = file->private_data;
9828c2ecf20Sopenharmony_ci	struct device *dev = ctx->hw_afu->dev;
9838c2ecf20Sopenharmony_ci	ulong lock_flags;
9848c2ecf20Sopenharmony_ci	int mask = 0;
9858c2ecf20Sopenharmony_ci
9868c2ecf20Sopenharmony_ci	poll_wait(file, &ctx->wq, poll);
9878c2ecf20Sopenharmony_ci
9888c2ecf20Sopenharmony_ci	spin_lock_irqsave(&ctx->slock, lock_flags);
9898c2ecf20Sopenharmony_ci	if (ctx_event_pending(ctx))
9908c2ecf20Sopenharmony_ci		mask |= POLLIN | POLLRDNORM;
9918c2ecf20Sopenharmony_ci	else if (ctx->state == CLOSED)
9928c2ecf20Sopenharmony_ci		mask |= POLLERR;
9938c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&ctx->slock, lock_flags);
9948c2ecf20Sopenharmony_ci
9958c2ecf20Sopenharmony_ci	dev_dbg(dev, "%s: Poll wait completed for pe %i mask %i\n",
9968c2ecf20Sopenharmony_ci		__func__, ctx->pe, mask);
9978c2ecf20Sopenharmony_ci
9988c2ecf20Sopenharmony_ci	return mask;
9998c2ecf20Sopenharmony_ci}
10008c2ecf20Sopenharmony_ci
10018c2ecf20Sopenharmony_ci/**
10028c2ecf20Sopenharmony_ci * afu_read() - perform a read on the context for any event
10038c2ecf20Sopenharmony_ci * @file:	File associated with the adapter context.
10048c2ecf20Sopenharmony_ci * @buf:	Buffer to receive the data.
10058c2ecf20Sopenharmony_ci * @count:	Size of buffer (maximum bytes that can be read).
10068c2ecf20Sopenharmony_ci * @off:	Offset.
10078c2ecf20Sopenharmony_ci *
10088c2ecf20Sopenharmony_ci * Return: size of the data read on success, -errno on failure
10098c2ecf20Sopenharmony_ci */
10108c2ecf20Sopenharmony_cistatic ssize_t afu_read(struct file *file, char __user *buf, size_t count,
10118c2ecf20Sopenharmony_ci			loff_t *off)
10128c2ecf20Sopenharmony_ci{
10138c2ecf20Sopenharmony_ci	struct ocxlflash_context *ctx = file->private_data;
10148c2ecf20Sopenharmony_ci	struct device *dev = ctx->hw_afu->dev;
10158c2ecf20Sopenharmony_ci	struct cxl_event event;
10168c2ecf20Sopenharmony_ci	ulong lock_flags;
10178c2ecf20Sopenharmony_ci	ssize_t esize;
10188c2ecf20Sopenharmony_ci	ssize_t rc;
10198c2ecf20Sopenharmony_ci	int bit;
10208c2ecf20Sopenharmony_ci	DEFINE_WAIT(event_wait);
10218c2ecf20Sopenharmony_ci
10228c2ecf20Sopenharmony_ci	if (*off != 0) {
10238c2ecf20Sopenharmony_ci		dev_err(dev, "%s: Non-zero offset not supported, off=%lld\n",
10248c2ecf20Sopenharmony_ci			__func__, *off);
10258c2ecf20Sopenharmony_ci		rc = -EINVAL;
10268c2ecf20Sopenharmony_ci		goto out;
10278c2ecf20Sopenharmony_ci	}
10288c2ecf20Sopenharmony_ci
10298c2ecf20Sopenharmony_ci	spin_lock_irqsave(&ctx->slock, lock_flags);
10308c2ecf20Sopenharmony_ci
10318c2ecf20Sopenharmony_ci	for (;;) {
10328c2ecf20Sopenharmony_ci		prepare_to_wait(&ctx->wq, &event_wait, TASK_INTERRUPTIBLE);
10338c2ecf20Sopenharmony_ci
10348c2ecf20Sopenharmony_ci		if (ctx_event_pending(ctx) || (ctx->state == CLOSED))
10358c2ecf20Sopenharmony_ci			break;
10368c2ecf20Sopenharmony_ci
10378c2ecf20Sopenharmony_ci		if (file->f_flags & O_NONBLOCK) {
10388c2ecf20Sopenharmony_ci			dev_err(dev, "%s: File cannot be blocked on I/O\n",
10398c2ecf20Sopenharmony_ci				__func__);
10408c2ecf20Sopenharmony_ci			rc = -EAGAIN;
10418c2ecf20Sopenharmony_ci			goto err;
10428c2ecf20Sopenharmony_ci		}
10438c2ecf20Sopenharmony_ci
10448c2ecf20Sopenharmony_ci		if (signal_pending(current)) {
10458c2ecf20Sopenharmony_ci			dev_err(dev, "%s: Signal pending on the process\n",
10468c2ecf20Sopenharmony_ci				__func__);
10478c2ecf20Sopenharmony_ci			rc = -ERESTARTSYS;
10488c2ecf20Sopenharmony_ci			goto err;
10498c2ecf20Sopenharmony_ci		}
10508c2ecf20Sopenharmony_ci
10518c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&ctx->slock, lock_flags);
10528c2ecf20Sopenharmony_ci		schedule();
10538c2ecf20Sopenharmony_ci		spin_lock_irqsave(&ctx->slock, lock_flags);
10548c2ecf20Sopenharmony_ci	}
10558c2ecf20Sopenharmony_ci
10568c2ecf20Sopenharmony_ci	finish_wait(&ctx->wq, &event_wait);
10578c2ecf20Sopenharmony_ci
10588c2ecf20Sopenharmony_ci	memset(&event, 0, sizeof(event));
10598c2ecf20Sopenharmony_ci	event.header.process_element = ctx->pe;
10608c2ecf20Sopenharmony_ci	event.header.size = sizeof(struct cxl_event_header);
10618c2ecf20Sopenharmony_ci	if (ctx->pending_irq) {
10628c2ecf20Sopenharmony_ci		esize = sizeof(struct cxl_event_afu_interrupt);
10638c2ecf20Sopenharmony_ci		event.header.size += esize;
10648c2ecf20Sopenharmony_ci		event.header.type = CXL_EVENT_AFU_INTERRUPT;
10658c2ecf20Sopenharmony_ci
10668c2ecf20Sopenharmony_ci		bit = find_first_bit(&ctx->irq_bitmap, ctx->num_irqs);
10678c2ecf20Sopenharmony_ci		clear_bit(bit, &ctx->irq_bitmap);
10688c2ecf20Sopenharmony_ci		event.irq.irq = bit + 1;
10698c2ecf20Sopenharmony_ci		if (bitmap_empty(&ctx->irq_bitmap, ctx->num_irqs))
10708c2ecf20Sopenharmony_ci			ctx->pending_irq = false;
10718c2ecf20Sopenharmony_ci	} else if (ctx->pending_fault) {
10728c2ecf20Sopenharmony_ci		event.header.size += sizeof(struct cxl_event_data_storage);
10738c2ecf20Sopenharmony_ci		event.header.type = CXL_EVENT_DATA_STORAGE;
10748c2ecf20Sopenharmony_ci		event.fault.addr = ctx->fault_addr;
10758c2ecf20Sopenharmony_ci		event.fault.dsisr = ctx->fault_dsisr;
10768c2ecf20Sopenharmony_ci		ctx->pending_fault = false;
10778c2ecf20Sopenharmony_ci	}
10788c2ecf20Sopenharmony_ci
10798c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&ctx->slock, lock_flags);
10808c2ecf20Sopenharmony_ci
10818c2ecf20Sopenharmony_ci	if (copy_to_user(buf, &event, event.header.size)) {
10828c2ecf20Sopenharmony_ci		dev_err(dev, "%s: copy_to_user failed\n", __func__);
10838c2ecf20Sopenharmony_ci		rc = -EFAULT;
10848c2ecf20Sopenharmony_ci		goto out;
10858c2ecf20Sopenharmony_ci	}
10868c2ecf20Sopenharmony_ci
10878c2ecf20Sopenharmony_ci	rc = event.header.size;
10888c2ecf20Sopenharmony_ciout:
10898c2ecf20Sopenharmony_ci	return rc;
10908c2ecf20Sopenharmony_cierr:
10918c2ecf20Sopenharmony_ci	finish_wait(&ctx->wq, &event_wait);
10928c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&ctx->slock, lock_flags);
10938c2ecf20Sopenharmony_ci	goto out;
10948c2ecf20Sopenharmony_ci}
10958c2ecf20Sopenharmony_ci
10968c2ecf20Sopenharmony_ci/**
10978c2ecf20Sopenharmony_ci * afu_release() - release and free the context
10988c2ecf20Sopenharmony_ci * @inode:	File inode pointer.
10998c2ecf20Sopenharmony_ci * @file:	File associated with the context.
11008c2ecf20Sopenharmony_ci *
11018c2ecf20Sopenharmony_ci * Return: 0 on success, -errno on failure
11028c2ecf20Sopenharmony_ci */
11038c2ecf20Sopenharmony_cistatic int afu_release(struct inode *inode, struct file *file)
11048c2ecf20Sopenharmony_ci{
11058c2ecf20Sopenharmony_ci	struct ocxlflash_context *ctx = file->private_data;
11068c2ecf20Sopenharmony_ci	int i;
11078c2ecf20Sopenharmony_ci
11088c2ecf20Sopenharmony_ci	/* Unmap and free the interrupts associated with the context */
11098c2ecf20Sopenharmony_ci	for (i = ctx->num_irqs; i >= 0; i--)
11108c2ecf20Sopenharmony_ci		afu_unmap_irq(0, ctx, i, ctx);
11118c2ecf20Sopenharmony_ci	free_afu_irqs(ctx);
11128c2ecf20Sopenharmony_ci
11138c2ecf20Sopenharmony_ci	return ocxlflash_release_context(ctx);
11148c2ecf20Sopenharmony_ci}
11158c2ecf20Sopenharmony_ci
11168c2ecf20Sopenharmony_ci/**
11178c2ecf20Sopenharmony_ci * ocxlflash_mmap_fault() - mmap fault handler
11188c2ecf20Sopenharmony_ci * @vmf:	VM fault associated with current fault.
11198c2ecf20Sopenharmony_ci *
11208c2ecf20Sopenharmony_ci * Return: 0 on success, -errno on failure
11218c2ecf20Sopenharmony_ci */
11228c2ecf20Sopenharmony_cistatic vm_fault_t ocxlflash_mmap_fault(struct vm_fault *vmf)
11238c2ecf20Sopenharmony_ci{
11248c2ecf20Sopenharmony_ci	struct vm_area_struct *vma = vmf->vma;
11258c2ecf20Sopenharmony_ci	struct ocxlflash_context *ctx = vma->vm_file->private_data;
11268c2ecf20Sopenharmony_ci	struct device *dev = ctx->hw_afu->dev;
11278c2ecf20Sopenharmony_ci	u64 mmio_area, offset;
11288c2ecf20Sopenharmony_ci
11298c2ecf20Sopenharmony_ci	offset = vmf->pgoff << PAGE_SHIFT;
11308c2ecf20Sopenharmony_ci	if (offset >= ctx->psn_size)
11318c2ecf20Sopenharmony_ci		return VM_FAULT_SIGBUS;
11328c2ecf20Sopenharmony_ci
11338c2ecf20Sopenharmony_ci	mutex_lock(&ctx->state_mutex);
11348c2ecf20Sopenharmony_ci	if (ctx->state != STARTED) {
11358c2ecf20Sopenharmony_ci		dev_err(dev, "%s: Context not started, state=%d\n",
11368c2ecf20Sopenharmony_ci			__func__, ctx->state);
11378c2ecf20Sopenharmony_ci		mutex_unlock(&ctx->state_mutex);
11388c2ecf20Sopenharmony_ci		return VM_FAULT_SIGBUS;
11398c2ecf20Sopenharmony_ci	}
11408c2ecf20Sopenharmony_ci	mutex_unlock(&ctx->state_mutex);
11418c2ecf20Sopenharmony_ci
11428c2ecf20Sopenharmony_ci	mmio_area = ctx->psn_phys;
11438c2ecf20Sopenharmony_ci	mmio_area += offset;
11448c2ecf20Sopenharmony_ci
11458c2ecf20Sopenharmony_ci	return vmf_insert_pfn(vma, vmf->address, mmio_area >> PAGE_SHIFT);
11468c2ecf20Sopenharmony_ci}
11478c2ecf20Sopenharmony_ci
11488c2ecf20Sopenharmony_cistatic const struct vm_operations_struct ocxlflash_vmops = {
11498c2ecf20Sopenharmony_ci	.fault = ocxlflash_mmap_fault,
11508c2ecf20Sopenharmony_ci};
11518c2ecf20Sopenharmony_ci
11528c2ecf20Sopenharmony_ci/**
11538c2ecf20Sopenharmony_ci * afu_mmap() - map the fault handler operations
11548c2ecf20Sopenharmony_ci * @file:	File associated with the context.
11558c2ecf20Sopenharmony_ci * @vma:	VM area associated with mapping.
11568c2ecf20Sopenharmony_ci *
11578c2ecf20Sopenharmony_ci * Return: 0 on success, -errno on failure
11588c2ecf20Sopenharmony_ci */
11598c2ecf20Sopenharmony_cistatic int afu_mmap(struct file *file, struct vm_area_struct *vma)
11608c2ecf20Sopenharmony_ci{
11618c2ecf20Sopenharmony_ci	struct ocxlflash_context *ctx = file->private_data;
11628c2ecf20Sopenharmony_ci
11638c2ecf20Sopenharmony_ci	if ((vma_pages(vma) + vma->vm_pgoff) >
11648c2ecf20Sopenharmony_ci	    (ctx->psn_size >> PAGE_SHIFT))
11658c2ecf20Sopenharmony_ci		return -EINVAL;
11668c2ecf20Sopenharmony_ci
11678c2ecf20Sopenharmony_ci	vma->vm_flags |= VM_IO | VM_PFNMAP;
11688c2ecf20Sopenharmony_ci	vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
11698c2ecf20Sopenharmony_ci	vma->vm_ops = &ocxlflash_vmops;
11708c2ecf20Sopenharmony_ci	return 0;
11718c2ecf20Sopenharmony_ci}
11728c2ecf20Sopenharmony_ci
11738c2ecf20Sopenharmony_cistatic const struct file_operations ocxl_afu_fops = {
11748c2ecf20Sopenharmony_ci	.owner		= THIS_MODULE,
11758c2ecf20Sopenharmony_ci	.poll		= afu_poll,
11768c2ecf20Sopenharmony_ci	.read		= afu_read,
11778c2ecf20Sopenharmony_ci	.release	= afu_release,
11788c2ecf20Sopenharmony_ci	.mmap		= afu_mmap,
11798c2ecf20Sopenharmony_ci};
11808c2ecf20Sopenharmony_ci
11818c2ecf20Sopenharmony_ci#define PATCH_FOPS(NAME)						\
11828c2ecf20Sopenharmony_ci	do { if (!fops->NAME) fops->NAME = ocxl_afu_fops.NAME; } while (0)
11838c2ecf20Sopenharmony_ci
11848c2ecf20Sopenharmony_ci/**
11858c2ecf20Sopenharmony_ci * ocxlflash_get_fd() - get file descriptor for an adapter context
11868c2ecf20Sopenharmony_ci * @ctx_cookie:	Adapter context.
11878c2ecf20Sopenharmony_ci * @fops:	File operations to be associated.
11888c2ecf20Sopenharmony_ci * @fd:		File descriptor to be returned back.
11898c2ecf20Sopenharmony_ci *
11908c2ecf20Sopenharmony_ci * Return: pointer to the file on success, ERR_PTR on failure
11918c2ecf20Sopenharmony_ci */
11928c2ecf20Sopenharmony_cistatic struct file *ocxlflash_get_fd(void *ctx_cookie,
11938c2ecf20Sopenharmony_ci				     struct file_operations *fops, int *fd)
11948c2ecf20Sopenharmony_ci{
11958c2ecf20Sopenharmony_ci	struct ocxlflash_context *ctx = ctx_cookie;
11968c2ecf20Sopenharmony_ci	struct device *dev = ctx->hw_afu->dev;
11978c2ecf20Sopenharmony_ci	struct file *file;
11988c2ecf20Sopenharmony_ci	int flags, fdtmp;
11998c2ecf20Sopenharmony_ci	int rc = 0;
12008c2ecf20Sopenharmony_ci	char *name = NULL;
12018c2ecf20Sopenharmony_ci
12028c2ecf20Sopenharmony_ci	/* Only allow one fd per context */
12038c2ecf20Sopenharmony_ci	if (ctx->mapping) {
12048c2ecf20Sopenharmony_ci		dev_err(dev, "%s: Context is already mapped to an fd\n",
12058c2ecf20Sopenharmony_ci			__func__);
12068c2ecf20Sopenharmony_ci		rc = -EEXIST;
12078c2ecf20Sopenharmony_ci		goto err1;
12088c2ecf20Sopenharmony_ci	}
12098c2ecf20Sopenharmony_ci
12108c2ecf20Sopenharmony_ci	flags = O_RDWR | O_CLOEXEC;
12118c2ecf20Sopenharmony_ci
12128c2ecf20Sopenharmony_ci	/* This code is similar to anon_inode_getfd() */
12138c2ecf20Sopenharmony_ci	rc = get_unused_fd_flags(flags);
12148c2ecf20Sopenharmony_ci	if (unlikely(rc < 0)) {
12158c2ecf20Sopenharmony_ci		dev_err(dev, "%s: get_unused_fd_flags failed rc=%d\n",
12168c2ecf20Sopenharmony_ci			__func__, rc);
12178c2ecf20Sopenharmony_ci		goto err1;
12188c2ecf20Sopenharmony_ci	}
12198c2ecf20Sopenharmony_ci	fdtmp = rc;
12208c2ecf20Sopenharmony_ci
12218c2ecf20Sopenharmony_ci	/* Patch the file ops that are not defined */
12228c2ecf20Sopenharmony_ci	if (fops) {
12238c2ecf20Sopenharmony_ci		PATCH_FOPS(poll);
12248c2ecf20Sopenharmony_ci		PATCH_FOPS(read);
12258c2ecf20Sopenharmony_ci		PATCH_FOPS(release);
12268c2ecf20Sopenharmony_ci		PATCH_FOPS(mmap);
12278c2ecf20Sopenharmony_ci	} else /* Use default ops */
12288c2ecf20Sopenharmony_ci		fops = (struct file_operations *)&ocxl_afu_fops;
12298c2ecf20Sopenharmony_ci
12308c2ecf20Sopenharmony_ci	name = kasprintf(GFP_KERNEL, "ocxlflash:%d", ctx->pe);
12318c2ecf20Sopenharmony_ci	file = ocxlflash_getfile(dev, name, fops, ctx, flags);
12328c2ecf20Sopenharmony_ci	kfree(name);
12338c2ecf20Sopenharmony_ci	if (IS_ERR(file)) {
12348c2ecf20Sopenharmony_ci		rc = PTR_ERR(file);
12358c2ecf20Sopenharmony_ci		dev_err(dev, "%s: ocxlflash_getfile failed rc=%d\n",
12368c2ecf20Sopenharmony_ci			__func__, rc);
12378c2ecf20Sopenharmony_ci		goto err2;
12388c2ecf20Sopenharmony_ci	}
12398c2ecf20Sopenharmony_ci
12408c2ecf20Sopenharmony_ci	ctx->mapping = file->f_mapping;
12418c2ecf20Sopenharmony_ci	*fd = fdtmp;
12428c2ecf20Sopenharmony_ciout:
12438c2ecf20Sopenharmony_ci	return file;
12448c2ecf20Sopenharmony_cierr2:
12458c2ecf20Sopenharmony_ci	put_unused_fd(fdtmp);
12468c2ecf20Sopenharmony_cierr1:
12478c2ecf20Sopenharmony_ci	file = ERR_PTR(rc);
12488c2ecf20Sopenharmony_ci	goto out;
12498c2ecf20Sopenharmony_ci}
12508c2ecf20Sopenharmony_ci
12518c2ecf20Sopenharmony_ci/**
12528c2ecf20Sopenharmony_ci * ocxlflash_fops_get_context() - get the context associated with the file
12538c2ecf20Sopenharmony_ci * @file:	File associated with the adapter context.
12548c2ecf20Sopenharmony_ci *
12558c2ecf20Sopenharmony_ci * Return: pointer to the context
12568c2ecf20Sopenharmony_ci */
12578c2ecf20Sopenharmony_cistatic void *ocxlflash_fops_get_context(struct file *file)
12588c2ecf20Sopenharmony_ci{
12598c2ecf20Sopenharmony_ci	return file->private_data;
12608c2ecf20Sopenharmony_ci}
12618c2ecf20Sopenharmony_ci
12628c2ecf20Sopenharmony_ci/**
12638c2ecf20Sopenharmony_ci * ocxlflash_afu_irq() - interrupt handler for user contexts
12648c2ecf20Sopenharmony_ci * @irq:	Interrupt number.
12658c2ecf20Sopenharmony_ci * @data:	Private data provided at interrupt registration, the context.
12668c2ecf20Sopenharmony_ci *
12678c2ecf20Sopenharmony_ci * Return: Always return IRQ_HANDLED.
12688c2ecf20Sopenharmony_ci */
12698c2ecf20Sopenharmony_cistatic irqreturn_t ocxlflash_afu_irq(int irq, void *data)
12708c2ecf20Sopenharmony_ci{
12718c2ecf20Sopenharmony_ci	struct ocxlflash_context *ctx = data;
12728c2ecf20Sopenharmony_ci	struct device *dev = ctx->hw_afu->dev;
12738c2ecf20Sopenharmony_ci	int i;
12748c2ecf20Sopenharmony_ci
12758c2ecf20Sopenharmony_ci	dev_dbg(dev, "%s: Interrupt raised for pe %i virq %i\n",
12768c2ecf20Sopenharmony_ci		__func__, ctx->pe, irq);
12778c2ecf20Sopenharmony_ci
12788c2ecf20Sopenharmony_ci	for (i = 0; i < ctx->num_irqs; i++) {
12798c2ecf20Sopenharmony_ci		if (ctx->irqs[i].virq == irq)
12808c2ecf20Sopenharmony_ci			break;
12818c2ecf20Sopenharmony_ci	}
12828c2ecf20Sopenharmony_ci	if (unlikely(i >= ctx->num_irqs)) {
12838c2ecf20Sopenharmony_ci		dev_err(dev, "%s: Received AFU IRQ out of range\n", __func__);
12848c2ecf20Sopenharmony_ci		goto out;
12858c2ecf20Sopenharmony_ci	}
12868c2ecf20Sopenharmony_ci
12878c2ecf20Sopenharmony_ci	spin_lock(&ctx->slock);
12888c2ecf20Sopenharmony_ci	set_bit(i - 1, &ctx->irq_bitmap);
12898c2ecf20Sopenharmony_ci	ctx->pending_irq = true;
12908c2ecf20Sopenharmony_ci	spin_unlock(&ctx->slock);
12918c2ecf20Sopenharmony_ci
12928c2ecf20Sopenharmony_ci	wake_up_all(&ctx->wq);
12938c2ecf20Sopenharmony_ciout:
12948c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
12958c2ecf20Sopenharmony_ci}
12968c2ecf20Sopenharmony_ci
12978c2ecf20Sopenharmony_ci/**
12988c2ecf20Sopenharmony_ci * ocxlflash_start_work() - start a user context
12998c2ecf20Sopenharmony_ci * @ctx_cookie:	Context to be started.
13008c2ecf20Sopenharmony_ci * @num_irqs:	Number of interrupts requested.
13018c2ecf20Sopenharmony_ci *
13028c2ecf20Sopenharmony_ci * Return: 0 on success, -errno on failure
13038c2ecf20Sopenharmony_ci */
13048c2ecf20Sopenharmony_cistatic int ocxlflash_start_work(void *ctx_cookie, u64 num_irqs)
13058c2ecf20Sopenharmony_ci{
13068c2ecf20Sopenharmony_ci	struct ocxlflash_context *ctx = ctx_cookie;
13078c2ecf20Sopenharmony_ci	struct ocxl_hw_afu *afu = ctx->hw_afu;
13088c2ecf20Sopenharmony_ci	struct device *dev = afu->dev;
13098c2ecf20Sopenharmony_ci	char *name;
13108c2ecf20Sopenharmony_ci	int rc = 0;
13118c2ecf20Sopenharmony_ci	int i;
13128c2ecf20Sopenharmony_ci
13138c2ecf20Sopenharmony_ci	rc = alloc_afu_irqs(ctx, num_irqs);
13148c2ecf20Sopenharmony_ci	if (unlikely(rc < 0)) {
13158c2ecf20Sopenharmony_ci		dev_err(dev, "%s: alloc_afu_irqs failed rc=%d\n", __func__, rc);
13168c2ecf20Sopenharmony_ci		goto out;
13178c2ecf20Sopenharmony_ci	}
13188c2ecf20Sopenharmony_ci
13198c2ecf20Sopenharmony_ci	for (i = 0; i < num_irqs; i++) {
13208c2ecf20Sopenharmony_ci		name = kasprintf(GFP_KERNEL, "ocxlflash-%s-pe%i-%i",
13218c2ecf20Sopenharmony_ci				 dev_name(dev), ctx->pe, i);
13228c2ecf20Sopenharmony_ci		rc = afu_map_irq(0, ctx, i, ocxlflash_afu_irq, ctx, name);
13238c2ecf20Sopenharmony_ci		kfree(name);
13248c2ecf20Sopenharmony_ci		if (unlikely(rc < 0)) {
13258c2ecf20Sopenharmony_ci			dev_err(dev, "%s: afu_map_irq failed rc=%d\n",
13268c2ecf20Sopenharmony_ci				__func__, rc);
13278c2ecf20Sopenharmony_ci			goto err;
13288c2ecf20Sopenharmony_ci		}
13298c2ecf20Sopenharmony_ci	}
13308c2ecf20Sopenharmony_ci
13318c2ecf20Sopenharmony_ci	rc = start_context(ctx);
13328c2ecf20Sopenharmony_ci	if (unlikely(rc)) {
13338c2ecf20Sopenharmony_ci		dev_err(dev, "%s: start_context failed rc=%d\n", __func__, rc);
13348c2ecf20Sopenharmony_ci		goto err;
13358c2ecf20Sopenharmony_ci	}
13368c2ecf20Sopenharmony_ciout:
13378c2ecf20Sopenharmony_ci	return rc;
13388c2ecf20Sopenharmony_cierr:
13398c2ecf20Sopenharmony_ci	for (i = i-1; i >= 0; i--)
13408c2ecf20Sopenharmony_ci		afu_unmap_irq(0, ctx, i, ctx);
13418c2ecf20Sopenharmony_ci	free_afu_irqs(ctx);
13428c2ecf20Sopenharmony_ci	goto out;
13438c2ecf20Sopenharmony_ci};
13448c2ecf20Sopenharmony_ci
13458c2ecf20Sopenharmony_ci/**
13468c2ecf20Sopenharmony_ci * ocxlflash_fd_mmap() - mmap handler for adapter file descriptor
13478c2ecf20Sopenharmony_ci * @file:	File installed with adapter file descriptor.
13488c2ecf20Sopenharmony_ci * @vma:	VM area associated with mapping.
13498c2ecf20Sopenharmony_ci *
13508c2ecf20Sopenharmony_ci * Return: 0 on success, -errno on failure
13518c2ecf20Sopenharmony_ci */
13528c2ecf20Sopenharmony_cistatic int ocxlflash_fd_mmap(struct file *file, struct vm_area_struct *vma)
13538c2ecf20Sopenharmony_ci{
13548c2ecf20Sopenharmony_ci	return afu_mmap(file, vma);
13558c2ecf20Sopenharmony_ci}
13568c2ecf20Sopenharmony_ci
13578c2ecf20Sopenharmony_ci/**
13588c2ecf20Sopenharmony_ci * ocxlflash_fd_release() - release the context associated with the file
13598c2ecf20Sopenharmony_ci * @inode:	File inode pointer.
13608c2ecf20Sopenharmony_ci * @file:	File associated with the adapter context.
13618c2ecf20Sopenharmony_ci *
13628c2ecf20Sopenharmony_ci * Return: 0 on success, -errno on failure
13638c2ecf20Sopenharmony_ci */
13648c2ecf20Sopenharmony_cistatic int ocxlflash_fd_release(struct inode *inode, struct file *file)
13658c2ecf20Sopenharmony_ci{
13668c2ecf20Sopenharmony_ci	return afu_release(inode, file);
13678c2ecf20Sopenharmony_ci}
13688c2ecf20Sopenharmony_ci
13698c2ecf20Sopenharmony_ci/* Backend ops to ocxlflash services */
13708c2ecf20Sopenharmony_ciconst struct cxlflash_backend_ops cxlflash_ocxl_ops = {
13718c2ecf20Sopenharmony_ci	.module			= THIS_MODULE,
13728c2ecf20Sopenharmony_ci	.psa_map		= ocxlflash_psa_map,
13738c2ecf20Sopenharmony_ci	.psa_unmap		= ocxlflash_psa_unmap,
13748c2ecf20Sopenharmony_ci	.process_element	= ocxlflash_process_element,
13758c2ecf20Sopenharmony_ci	.map_afu_irq		= ocxlflash_map_afu_irq,
13768c2ecf20Sopenharmony_ci	.unmap_afu_irq		= ocxlflash_unmap_afu_irq,
13778c2ecf20Sopenharmony_ci	.get_irq_objhndl	= ocxlflash_get_irq_objhndl,
13788c2ecf20Sopenharmony_ci	.start_context		= ocxlflash_start_context,
13798c2ecf20Sopenharmony_ci	.stop_context		= ocxlflash_stop_context,
13808c2ecf20Sopenharmony_ci	.afu_reset		= ocxlflash_afu_reset,
13818c2ecf20Sopenharmony_ci	.set_master		= ocxlflash_set_master,
13828c2ecf20Sopenharmony_ci	.get_context		= ocxlflash_get_context,
13838c2ecf20Sopenharmony_ci	.dev_context_init	= ocxlflash_dev_context_init,
13848c2ecf20Sopenharmony_ci	.release_context	= ocxlflash_release_context,
13858c2ecf20Sopenharmony_ci	.perst_reloads_same_image = ocxlflash_perst_reloads_same_image,
13868c2ecf20Sopenharmony_ci	.read_adapter_vpd	= ocxlflash_read_adapter_vpd,
13878c2ecf20Sopenharmony_ci	.allocate_afu_irqs	= ocxlflash_allocate_afu_irqs,
13888c2ecf20Sopenharmony_ci	.free_afu_irqs		= ocxlflash_free_afu_irqs,
13898c2ecf20Sopenharmony_ci	.create_afu		= ocxlflash_create_afu,
13908c2ecf20Sopenharmony_ci	.destroy_afu		= ocxlflash_destroy_afu,
13918c2ecf20Sopenharmony_ci	.get_fd			= ocxlflash_get_fd,
13928c2ecf20Sopenharmony_ci	.fops_get_context	= ocxlflash_fops_get_context,
13938c2ecf20Sopenharmony_ci	.start_work		= ocxlflash_start_work,
13948c2ecf20Sopenharmony_ci	.fd_mmap		= ocxlflash_fd_mmap,
13958c2ecf20Sopenharmony_ci	.fd_release		= ocxlflash_fd_release,
13968c2ecf20Sopenharmony_ci};
1397