Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) // Copyright 2017 IBM Corp.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4) #include <linux/poll.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) #include <linux/sched/signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) #include <linux/eventfd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <uapi/misc/ocxl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <asm/reg.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <asm/switch_to.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include "ocxl_internal.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #define OCXL_NUM_MINORS 256 /* Total to reserve */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) static dev_t ocxl_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) static struct class *ocxl_class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) static struct mutex minors_idr_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) static struct idr minors_idr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) static struct ocxl_file_info *find_and_get_file_info(dev_t devno)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 	struct ocxl_file_info *info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 	mutex_lock(&minors_idr_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	info = idr_find(&minors_idr, MINOR(devno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	if (info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 		get_device(&info->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	mutex_unlock(&minors_idr_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	return info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) static int allocate_minor(struct ocxl_file_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	int minor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	mutex_lock(&minors_idr_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	minor = idr_alloc(&minors_idr, info, 0, OCXL_NUM_MINORS, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	mutex_unlock(&minors_idr_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	return minor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) static void free_minor(struct ocxl_file_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	mutex_lock(&minors_idr_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	idr_remove(&minors_idr, MINOR(info->dev.devt));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	mutex_unlock(&minors_idr_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) static int afu_open(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	struct ocxl_file_info *info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	struct ocxl_context *ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	pr_debug("%s for device %x\n", __func__, inode->i_rdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	info = find_and_get_file_info(inode->i_rdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	if (!info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	rc = ocxl_context_alloc(&ctx, info->afu, inode->i_mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 		put_device(&info->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	put_device(&info->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	file->private_data = ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) static long afu_ioctl_attach(struct ocxl_context *ctx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 			struct ocxl_ioctl_attach __user *uarg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	struct ocxl_ioctl_attach arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	u64 amr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	pr_debug("%s for context %d\n", __func__, ctx->pasid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	if (copy_from_user(&arg, uarg, sizeof(arg)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	/* Make sure reserved fields are not set for forward compatibility */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	if (arg.reserved1 || arg.reserved2 || arg.reserved3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	amr = arg.amr & mfspr(SPRN_UAMOR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	rc = ocxl_context_attach(ctx, amr, current->mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) static long afu_ioctl_get_metadata(struct ocxl_context *ctx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 		struct ocxl_ioctl_metadata __user *uarg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	struct ocxl_ioctl_metadata arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	memset(&arg, 0, sizeof(arg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	arg.version = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	arg.afu_version_major = ctx->afu->config.version_major;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	arg.afu_version_minor = ctx->afu->config.version_minor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	arg.pasid = ctx->pasid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	arg.pp_mmio_size = ctx->afu->config.pp_mmio_stride;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	arg.global_mmio_size = ctx->afu->config.global_mmio_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	if (copy_to_user(uarg, &arg, sizeof(arg)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) #ifdef CONFIG_PPC64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) static long afu_ioctl_enable_p9_wait(struct ocxl_context *ctx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 		struct ocxl_ioctl_p9_wait __user *uarg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	struct ocxl_ioctl_p9_wait arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	memset(&arg, 0, sizeof(arg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	if (cpu_has_feature(CPU_FTR_P9_TIDR)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 		enum ocxl_context_status status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 		// Locks both status & tidr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 		mutex_lock(&ctx->status_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		if (!ctx->tidr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 			if (set_thread_tidr(current)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 				mutex_unlock(&ctx->status_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 				return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 			ctx->tidr = current->thread.tidr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 		status = ctx->status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 		mutex_unlock(&ctx->status_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 		if (status == ATTACHED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 			int rc = ocxl_link_update_pe(ctx->afu->fn->link,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 				ctx->pasid, ctx->tidr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 			if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 				return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 		arg.thread_id = ctx->tidr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 		return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	if (copy_to_user(uarg, &arg, sizeof(arg)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) static long afu_ioctl_get_features(struct ocxl_context *ctx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 		struct ocxl_ioctl_features __user *uarg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	struct ocxl_ioctl_features arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	memset(&arg, 0, sizeof(arg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) #ifdef CONFIG_PPC64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	if (cpu_has_feature(CPU_FTR_P9_TIDR))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 		arg.flags[0] |= OCXL_IOCTL_FEATURES_FLAGS0_P9_WAIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	if (copy_to_user(uarg, &arg, sizeof(arg)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) #define CMD_STR(x) (x == OCXL_IOCTL_ATTACH ? "ATTACH" :			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 			x == OCXL_IOCTL_IRQ_ALLOC ? "IRQ_ALLOC" :	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 			x == OCXL_IOCTL_IRQ_FREE ? "IRQ_FREE" :		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 			x == OCXL_IOCTL_IRQ_SET_FD ? "IRQ_SET_FD" :	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 			x == OCXL_IOCTL_GET_METADATA ? "GET_METADATA" :	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 			x == OCXL_IOCTL_ENABLE_P9_WAIT ? "ENABLE_P9_WAIT" :	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 			x == OCXL_IOCTL_GET_FEATURES ? "GET_FEATURES" :	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 			"UNKNOWN")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) static irqreturn_t irq_handler(void *private)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	struct eventfd_ctx *ev_ctx = private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	eventfd_signal(ev_ctx, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) static void irq_free(void *private)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	struct eventfd_ctx *ev_ctx = private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	eventfd_ctx_put(ev_ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) static long afu_ioctl(struct file *file, unsigned int cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 		unsigned long args)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	struct ocxl_context *ctx = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	struct ocxl_ioctl_irq_fd irq_fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	struct eventfd_ctx *ev_ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	int irq_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	u64 irq_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	long rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	bool closed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	pr_debug("%s for context %d, command %s\n", __func__, ctx->pasid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 		CMD_STR(cmd));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	mutex_lock(&ctx->status_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	closed = (ctx->status == CLOSED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	mutex_unlock(&ctx->status_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	if (closed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	case OCXL_IOCTL_ATTACH:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 		rc = afu_ioctl_attach(ctx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 				(struct ocxl_ioctl_attach __user *) args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	case OCXL_IOCTL_IRQ_ALLOC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 		rc = ocxl_afu_irq_alloc(ctx, &irq_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 		if (!rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 			irq_offset = ocxl_irq_id_to_offset(ctx, irq_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 			rc = copy_to_user((u64 __user *) args, &irq_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 					sizeof(irq_offset));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 			if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 				ocxl_afu_irq_free(ctx, irq_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 				return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	case OCXL_IOCTL_IRQ_FREE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 		rc = copy_from_user(&irq_offset, (u64 __user *) args,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 				sizeof(irq_offset));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 			return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 		irq_id = ocxl_irq_offset_to_id(ctx, irq_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 		rc = ocxl_afu_irq_free(ctx, irq_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	case OCXL_IOCTL_IRQ_SET_FD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 		rc = copy_from_user(&irq_fd, (u64 __user *) args,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 				sizeof(irq_fd));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 			return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 		if (irq_fd.reserved)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 		irq_id = ocxl_irq_offset_to_id(ctx, irq_fd.irq_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 		ev_ctx = eventfd_ctx_fdget(irq_fd.eventfd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 		if (IS_ERR(ev_ctx))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 			return PTR_ERR(ev_ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 		rc = ocxl_irq_set_handler(ctx, irq_id, irq_handler, irq_free, ev_ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	case OCXL_IOCTL_GET_METADATA:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 		rc = afu_ioctl_get_metadata(ctx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 				(struct ocxl_ioctl_metadata __user *) args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) #ifdef CONFIG_PPC64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	case OCXL_IOCTL_ENABLE_P9_WAIT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 		rc = afu_ioctl_enable_p9_wait(ctx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 				(struct ocxl_ioctl_p9_wait __user *) args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	case OCXL_IOCTL_GET_FEATURES:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 		rc = afu_ioctl_get_features(ctx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 				(struct ocxl_ioctl_features __user *) args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 		rc = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) static long afu_compat_ioctl(struct file *file, unsigned int cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 			unsigned long args)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	return afu_ioctl(file, cmd, args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) static int afu_mmap(struct file *file, struct vm_area_struct *vma)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	struct ocxl_context *ctx = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	pr_debug("%s for context %d\n", __func__, ctx->pasid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	return ocxl_context_mmap(ctx, vma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) static bool has_xsl_error(struct ocxl_context *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	bool ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	mutex_lock(&ctx->xsl_error_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	ret = !!ctx->xsl_error.addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	mutex_unlock(&ctx->xsl_error_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313)  * Are there any events pending on the AFU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314)  * ctx: The AFU context
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315)  * Returns: true if there are events pending
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) static bool afu_events_pending(struct ocxl_context *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	if (has_xsl_error(ctx))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) static unsigned int afu_poll(struct file *file, struct poll_table_struct *wait)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	struct ocxl_context *ctx = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	unsigned int mask = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	bool closed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	pr_debug("%s for context %d\n", __func__, ctx->pasid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 	poll_wait(file, &ctx->events_wq, wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	mutex_lock(&ctx->status_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 	closed = (ctx->status == CLOSED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	mutex_unlock(&ctx->status_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	if (afu_events_pending(ctx))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 		mask = EPOLLIN | EPOLLRDNORM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	else if (closed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 		mask = EPOLLERR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	return mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347)  * Populate the supplied buffer with a single XSL error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348)  * ctx:	The AFU context to report the error from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349)  * header: the event header to populate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350)  * buf: The buffer to write the body into (should be at least
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351)  *      AFU_EVENT_BODY_XSL_ERROR_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352)  * Return: the amount of buffer that was populated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) static ssize_t append_xsl_error(struct ocxl_context *ctx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 				struct ocxl_kernel_event_header *header,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 				char __user *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	struct ocxl_kernel_event_xsl_fault_error body;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	memset(&body, 0, sizeof(body));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	mutex_lock(&ctx->xsl_error_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	if (!ctx->xsl_error.addr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 		mutex_unlock(&ctx->xsl_error_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	body.addr = ctx->xsl_error.addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	body.dsisr = ctx->xsl_error.dsisr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	body.count = ctx->xsl_error.count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	ctx->xsl_error.addr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 	ctx->xsl_error.dsisr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	ctx->xsl_error.count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	mutex_unlock(&ctx->xsl_error_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	header->type = OCXL_AFU_EVENT_XSL_FAULT_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 	if (copy_to_user(buf, &body, sizeof(body)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	return sizeof(body);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) #define AFU_EVENT_BODY_MAX_SIZE sizeof(struct ocxl_kernel_event_xsl_fault_error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389)  * Reports events on the AFU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390)  * Format:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391)  *	Header (struct ocxl_kernel_event_header)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392)  *	Body (struct ocxl_kernel_event_*)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393)  *	Header...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) static ssize_t afu_read(struct file *file, char __user *buf, size_t count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 			loff_t *off)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	struct ocxl_context *ctx = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 	struct ocxl_kernel_event_header header;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 	ssize_t rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 	ssize_t used = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 	DEFINE_WAIT(event_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	memset(&header, 0, sizeof(header));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 	/* Require offset to be 0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 	if (*off != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 	if (count < (sizeof(struct ocxl_kernel_event_header) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 			AFU_EVENT_BODY_MAX_SIZE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 	for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 		prepare_to_wait(&ctx->events_wq, &event_wait,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 				TASK_INTERRUPTIBLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 		if (afu_events_pending(ctx))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 		if (ctx->status == CLOSED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 		if (file->f_flags & O_NONBLOCK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 			finish_wait(&ctx->events_wq, &event_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 			return -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 		if (signal_pending(current)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 			finish_wait(&ctx->events_wq, &event_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 			return -ERESTARTSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 		schedule();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 	finish_wait(&ctx->events_wq, &event_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 	if (has_xsl_error(ctx)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 		used = append_xsl_error(ctx, &header, buf + sizeof(header));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 		if (used < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 			return used;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 	if (!afu_events_pending(ctx))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 		header.flags |= OCXL_KERNEL_EVENT_FLAG_LAST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 	if (copy_to_user(buf, &header, sizeof(header)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 	used += sizeof(header);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 	rc = used;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) static int afu_release(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 	struct ocxl_context *ctx = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 	pr_debug("%s for device %x\n", __func__, inode->i_rdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 	rc = ocxl_context_detach(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 	mutex_lock(&ctx->mapping_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 	ctx->mapping = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 	mutex_unlock(&ctx->mapping_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 	wake_up_all(&ctx->events_wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 	if (rc != -EBUSY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 		ocxl_context_free(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) static const struct file_operations ocxl_afu_fops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 	.owner		= THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 	.open           = afu_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 	.unlocked_ioctl = afu_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 	.compat_ioctl   = afu_compat_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 	.mmap           = afu_mmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 	.poll           = afu_poll,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 	.read           = afu_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 	.release        = afu_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) // Free the info struct
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) static void info_release(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 	struct ocxl_file_info *info = container_of(dev, struct ocxl_file_info, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 	ocxl_afu_put(info->afu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 	kfree(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) static int ocxl_file_make_visible(struct ocxl_file_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 	cdev_init(&info->cdev, &ocxl_afu_fops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 	rc = cdev_add(&info->cdev, info->dev.devt, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 	if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 		dev_err(&info->dev, "Unable to add afu char device: %d\n", rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) static void ocxl_file_make_invisible(struct ocxl_file_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 	cdev_del(&info->cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) int ocxl_file_register_afu(struct ocxl_afu *afu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 	int minor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 	struct ocxl_file_info *info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 	struct ocxl_fn *fn = afu->fn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 	struct pci_dev *pci_dev = to_pci_dev(fn->dev.parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 	info = kzalloc(sizeof(*info), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 	if (info == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 	minor = allocate_minor(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 	if (minor < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 		kfree(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 		return minor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 	info->dev.parent = &fn->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 	info->dev.devt = MKDEV(MAJOR(ocxl_dev), minor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 	info->dev.class = ocxl_class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 	info->dev.release = info_release;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 	info->afu = afu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 	ocxl_afu_get(afu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 	rc = dev_set_name(&info->dev, "%s.%s.%hhu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 		afu->config.name, dev_name(&pci_dev->dev), afu->config.idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 		goto err_put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 	rc = device_register(&info->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 		goto err_put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 	rc = ocxl_sysfs_register_afu(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 		goto err_unregister;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 	rc = ocxl_file_make_visible(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 		goto err_unregister;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 	ocxl_afu_set_private(afu, info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) err_unregister:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 	ocxl_sysfs_unregister_afu(info); // safe to call even if register failed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 	device_unregister(&info->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) err_put:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 	ocxl_afu_put(afu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 	free_minor(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 	kfree(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) void ocxl_file_unregister_afu(struct ocxl_afu *afu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) 	struct ocxl_file_info *info = ocxl_afu_get_private(afu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) 	if (!info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 	ocxl_file_make_invisible(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 	ocxl_sysfs_unregister_afu(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) 	free_minor(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) 	device_unregister(&info->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) static char *ocxl_devnode(struct device *dev, umode_t *mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) 	return kasprintf(GFP_KERNEL, "ocxl/%s", dev_name(dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) int ocxl_file_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) 	mutex_init(&minors_idr_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) 	idr_init(&minors_idr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) 	rc = alloc_chrdev_region(&ocxl_dev, 0, OCXL_NUM_MINORS, "ocxl");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) 	if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) 		pr_err("Unable to allocate ocxl major number: %d\n", rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) 	ocxl_class = class_create(THIS_MODULE, "ocxl");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) 	if (IS_ERR(ocxl_class)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) 		pr_err("Unable to create ocxl class\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) 		unregister_chrdev_region(ocxl_dev, OCXL_NUM_MINORS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) 		return PTR_ERR(ocxl_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) 	ocxl_class->devnode = ocxl_devnode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) void ocxl_file_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) 	class_destroy(ocxl_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) 	unregister_chrdev_region(ocxl_dev, OCXL_NUM_MINORS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) 	idr_destroy(&minors_idr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) }