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-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Copyright 2014 IBM Corp.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) #include <linux/spinlock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/bitmap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/sched/signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/poll.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/pid.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/sched/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/mmu_context.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <asm/cputable.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <asm/current.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <asm/copro.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include "cxl.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #include "trace.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #define CXL_NUM_MINORS 256 /* Total to reserve */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #define CXL_AFU_MINOR_D(afu) (CXL_CARD_MINOR(afu->adapter) + 1 + (3 * afu->slice))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) #define CXL_AFU_MINOR_M(afu) (CXL_AFU_MINOR_D(afu) + 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #define CXL_AFU_MINOR_S(afu) (CXL_AFU_MINOR_D(afu) + 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) #define CXL_AFU_MKDEV_D(afu) MKDEV(MAJOR(cxl_dev), CXL_AFU_MINOR_D(afu))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #define CXL_AFU_MKDEV_M(afu) MKDEV(MAJOR(cxl_dev), CXL_AFU_MINOR_M(afu))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) #define CXL_AFU_MKDEV_S(afu) MKDEV(MAJOR(cxl_dev), CXL_AFU_MINOR_S(afu))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) #define CXL_DEVT_AFU(dev) ((MINOR(dev) % CXL_DEV_MINORS - 1) / 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) #define CXL_DEVT_IS_CARD(dev) (MINOR(dev) % CXL_DEV_MINORS == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) static dev_t cxl_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) static struct class *cxl_class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) static int __afu_open(struct inode *inode, struct file *file, bool master)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	struct cxl *adapter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	struct cxl_afu *afu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	struct cxl_context *ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	int adapter_num = CXL_DEVT_ADAPTER(inode->i_rdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	int slice = CXL_DEVT_AFU(inode->i_rdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	int rc = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	pr_devel("afu_open afu%i.%i\n", slice, adapter_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	if (!(adapter = get_cxl_adapter(adapter_num)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	if (slice > adapter->slices)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 		goto err_put_adapter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	spin_lock(&adapter->afu_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	if (!(afu = adapter->afu[slice])) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 		spin_unlock(&adapter->afu_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 		goto err_put_adapter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	 * taking a ref to the afu so that it doesn't go away
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	 * for rest of the function. This ref is released before
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	 * we return.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	cxl_afu_get(afu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	spin_unlock(&adapter->afu_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	if (!afu->current_mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 		goto err_put_afu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	if (!cxl_ops->link_ok(adapter, afu)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 		rc = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 		goto err_put_afu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	if (!(ctx = cxl_context_alloc())) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 		rc = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 		goto err_put_afu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	rc = cxl_context_init(ctx, afu, master);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 		goto err_put_afu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	cxl_context_set_mapping(ctx, inode->i_mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	pr_devel("afu_open pe: %i\n", ctx->pe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	file->private_data = ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	/* indicate success */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) err_put_afu:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	/* release the ref taken earlier */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	cxl_afu_put(afu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) err_put_adapter:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	put_device(&adapter->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) int afu_open(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	return __afu_open(inode, file, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) static int afu_master_open(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	return __afu_open(inode, file, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) int afu_release(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	struct cxl_context *ctx = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	pr_devel("%s: closing cxl file descriptor. pe: %i\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 		 __func__, ctx->pe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	cxl_context_detach(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	 * Delete the context's mapping pointer, unless it's created by the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	 * kernel API, in which case leave it so it can be freed by reclaim_ctx()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	if (!ctx->kernelapi) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 		mutex_lock(&ctx->mapping_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 		ctx->mapping = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 		mutex_unlock(&ctx->mapping_lock);
^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) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	 * At this this point all bottom halfs have finished and we should be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	 * getting no more IRQs from the hardware for this context.  Once it's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	 * removed from the IDR (and RCU synchronised) it's safe to free the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	 * sstp and context.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	cxl_context_free(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	return 0;
^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) static long afu_ioctl_start_work(struct cxl_context *ctx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 				 struct cxl_ioctl_start_work __user *uwork)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	struct cxl_ioctl_start_work work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	u64 amr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	pr_devel("%s: pe: %i\n", __func__, ctx->pe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	/* Do this outside the status_mutex to avoid a circular dependency with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	 * the locking in cxl_mmap_fault() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	if (copy_from_user(&work, uwork, sizeof(work)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	mutex_lock(&ctx->status_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	if (ctx->status != OPENED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 		rc = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	 * if any of the reserved fields are set or any of the unused
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	 * flags are set it's invalid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	if (work.reserved1 || work.reserved2 || work.reserved3 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	    work.reserved4 || work.reserved5 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	    (work.flags & ~CXL_START_WORK_ALL)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 		rc = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	if (!(work.flags & CXL_START_WORK_NUM_IRQS))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 		work.num_interrupts = ctx->afu->pp_irqs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	else if ((work.num_interrupts < ctx->afu->pp_irqs) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 		 (work.num_interrupts > ctx->afu->irqs_max)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 		rc =  -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	if ((rc = afu_register_irqs(ctx, work.num_interrupts)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	if (work.flags & CXL_START_WORK_AMR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 		amr = work.amr & mfspr(SPRN_UAMOR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	if (work.flags & CXL_START_WORK_TID)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 		ctx->assign_tidr = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	ctx->mmio_err_ff = !!(work.flags & CXL_START_WORK_ERR_FF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	 * Increment the mapped context count for adapter. This also checks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	 * if adapter_context_lock is taken.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	rc = cxl_adapter_context_get(ctx->afu->adapter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 		afu_release_irqs(ctx, ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	 * We grab the PID here and not in the file open to allow for the case
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	 * where a process (master, some daemon, etc) has opened the chardev on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	 * behalf of another process, so the AFU's mm gets bound to the process
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	 * that performs this ioctl and not the process that opened the file.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	 * Also we grab the PID of the group leader so that if the task that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	 * has performed the attach operation exits the mm context of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	 * process is still accessible.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	ctx->pid = get_task_pid(current, PIDTYPE_PID);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	/* acquire a reference to the task's mm */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	ctx->mm = get_task_mm(current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	/* ensure this mm_struct can't be freed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	cxl_context_mm_count_get(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	if (ctx->mm) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 		/* decrement the use count from above */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 		mmput(ctx->mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 		/* make TLBIs for this context global */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 		mm_context_add_copro(ctx->mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	 * Increment driver use count. Enables global TLBIs for hash
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	 * and callbacks to handle the segment table
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	cxl_ctx_get();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	 * A barrier is needed to make sure all TLBIs are global
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	 * before we attach and the context starts being used by the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	 * adapter.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	 * Needed after mm_context_add_copro() for radix and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	 * cxl_ctx_get() for hash/p8.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	 * The barrier should really be mb(), since it involves a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	 * device. However, it's only useful when we have local
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	 * vs. global TLBIs, i.e SMP=y. So keep smp_mb().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	smp_mb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	trace_cxl_attach(ctx, work.work_element_descriptor, work.num_interrupts, amr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	if ((rc = cxl_ops->attach_process(ctx, false, work.work_element_descriptor,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 							amr))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 		afu_release_irqs(ctx, ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 		cxl_adapter_context_put(ctx->afu->adapter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 		put_pid(ctx->pid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 		ctx->pid = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 		cxl_ctx_put();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 		cxl_context_mm_count_put(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 		if (ctx->mm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 			mm_context_remove_copro(ctx->mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	if (work.flags & CXL_START_WORK_TID) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 		work.tid = ctx->tidr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 		if (copy_to_user(uwork, &work, sizeof(work)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 			rc = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	ctx->status = STARTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	mutex_unlock(&ctx->status_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) static long afu_ioctl_process_element(struct cxl_context *ctx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 				      int __user *upe)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	pr_devel("%s: pe: %i\n", __func__, ctx->pe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	if (copy_to_user(upe, &ctx->external_pe, sizeof(__u32)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) static long afu_ioctl_get_afu_id(struct cxl_context *ctx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 				 struct cxl_afu_id __user *upafuid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	struct cxl_afu_id afuid = { 0 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	afuid.card_id = ctx->afu->adapter->adapter_num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	afuid.afu_offset = ctx->afu->slice;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	afuid.afu_mode = ctx->afu->current_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	/* set the flag bit in case the afu is a slave */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	if (ctx->afu->current_mode == CXL_MODE_DIRECTED && !ctx->master)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 		afuid.flags |= CXL_AFUID_FLAG_SLAVE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	if (copy_to_user(upafuid, &afuid, sizeof(afuid)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) long afu_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	struct cxl_context *ctx = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	if (ctx->status == CLOSED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	if (!cxl_ops->link_ok(ctx->afu->adapter, ctx->afu))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	pr_devel("afu_ioctl\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	case CXL_IOCTL_START_WORK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 		return afu_ioctl_start_work(ctx, (struct cxl_ioctl_start_work __user *)arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 	case CXL_IOCTL_GET_PROCESS_ELEMENT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 		return afu_ioctl_process_element(ctx, (__u32 __user *)arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	case CXL_IOCTL_GET_AFU_ID:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 		return afu_ioctl_get_afu_id(ctx, (struct cxl_afu_id __user *)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 					    arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) static long afu_compat_ioctl(struct file *file, unsigned int cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 			     unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	return afu_ioctl(file, cmd, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) int afu_mmap(struct file *file, struct vm_area_struct *vm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	struct cxl_context *ctx = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	/* AFU must be started before we can MMIO */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	if (ctx->status != STARTED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 	if (!cxl_ops->link_ok(ctx->afu->adapter, ctx->afu))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 	return cxl_context_iomap(ctx, vm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) static inline bool ctx_event_pending(struct cxl_context *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	if (ctx->pending_irq || ctx->pending_fault || ctx->pending_afu_err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	if (ctx->afu_driver_ops && atomic_read(&ctx->afu_driver_events))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) __poll_t afu_poll(struct file *file, struct poll_table_struct *poll)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	struct cxl_context *ctx = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 	__poll_t mask = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 	poll_wait(file, &ctx->wq, poll);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 	pr_devel("afu_poll wait done pe: %i\n", ctx->pe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 	spin_lock_irqsave(&ctx->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	if (ctx_event_pending(ctx))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 		mask |= EPOLLIN | EPOLLRDNORM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	else if (ctx->status == CLOSED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 		/* Only error on closed when there are no futher events pending
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 		mask |= EPOLLERR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	spin_unlock_irqrestore(&ctx->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	pr_devel("afu_poll pe: %i returning %#x\n", ctx->pe, mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	return mask;
^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) static ssize_t afu_driver_event_copy(struct cxl_context *ctx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 				     char __user *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 				     struct cxl_event *event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 				     struct cxl_event_afu_driver_reserved *pl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 	/* Check event */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	if (!pl) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 		ctx->afu_driver_ops->event_delivered(ctx, pl, -EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 	/* Check event size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 	event->header.size += pl->data_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 	if (event->header.size > CXL_READ_MIN_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 		ctx->afu_driver_ops->event_delivered(ctx, pl, -EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 	/* Copy event header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 	if (copy_to_user(buf, event, sizeof(struct cxl_event_header))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 		ctx->afu_driver_ops->event_delivered(ctx, pl, -EFAULT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 	/* Copy event data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 	buf += sizeof(struct cxl_event_header);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 	if (copy_to_user(buf, &pl->data, pl->data_size)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 		ctx->afu_driver_ops->event_delivered(ctx, pl, -EFAULT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 	ctx->afu_driver_ops->event_delivered(ctx, pl, 0); /* Success */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 	return event->header.size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) ssize_t afu_read(struct file *file, char __user *buf, size_t count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 			loff_t *off)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 	struct cxl_context *ctx = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 	struct cxl_event_afu_driver_reserved *pl = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 	struct cxl_event event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 	DEFINE_WAIT(wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 	if (!cxl_ops->link_ok(ctx->afu->adapter, ctx->afu))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 	if (count < CXL_READ_MIN_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 	spin_lock_irqsave(&ctx->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 	for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 		prepare_to_wait(&ctx->wq, &wait, TASK_INTERRUPTIBLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 		if (ctx_event_pending(ctx) || (ctx->status == CLOSED))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 		if (!cxl_ops->link_ok(ctx->afu->adapter, ctx->afu)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 			rc = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 		if (file->f_flags & O_NONBLOCK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 			rc = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 			goto out;
^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) 		if (signal_pending(current)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 			rc = -ERESTARTSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 		spin_unlock_irqrestore(&ctx->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 		pr_devel("afu_read going to sleep...\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 		schedule();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 		pr_devel("afu_read woken up\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 		spin_lock_irqsave(&ctx->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 	finish_wait(&ctx->wq, &wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 	memset(&event, 0, sizeof(event));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 	event.header.process_element = ctx->pe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 	event.header.size = sizeof(struct cxl_event_header);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 	if (ctx->afu_driver_ops && atomic_read(&ctx->afu_driver_events)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 		pr_devel("afu_read delivering AFU driver specific event\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 		pl = ctx->afu_driver_ops->fetch_event(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 		atomic_dec(&ctx->afu_driver_events);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 		event.header.type = CXL_EVENT_AFU_DRIVER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 	} else if (ctx->pending_irq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 		pr_devel("afu_read delivering AFU interrupt\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 		event.header.size += sizeof(struct cxl_event_afu_interrupt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 		event.header.type = CXL_EVENT_AFU_INTERRUPT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 		event.irq.irq = find_first_bit(ctx->irq_bitmap, ctx->irq_count) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 		clear_bit(event.irq.irq - 1, ctx->irq_bitmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 		if (bitmap_empty(ctx->irq_bitmap, ctx->irq_count))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 			ctx->pending_irq = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 	} else if (ctx->pending_fault) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 		pr_devel("afu_read delivering data storage fault\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 		event.header.size += sizeof(struct cxl_event_data_storage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 		event.header.type = CXL_EVENT_DATA_STORAGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 		event.fault.addr = ctx->fault_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 		event.fault.dsisr = ctx->fault_dsisr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 		ctx->pending_fault = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 	} else if (ctx->pending_afu_err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 		pr_devel("afu_read delivering afu error\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 		event.header.size += sizeof(struct cxl_event_afu_error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 		event.header.type = CXL_EVENT_AFU_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 		event.afu_error.error = ctx->afu_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 		ctx->pending_afu_err = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 	} else if (ctx->status == CLOSED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 		pr_devel("afu_read fatal error\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 		spin_unlock_irqrestore(&ctx->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 		WARN(1, "afu_read must be buggy\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 	spin_unlock_irqrestore(&ctx->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 	if (event.header.type == CXL_EVENT_AFU_DRIVER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 		return afu_driver_event_copy(ctx, buf, &event, pl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 	if (copy_to_user(buf, &event, event.header.size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 	return event.header.size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 	finish_wait(&ctx->wq, &wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 	spin_unlock_irqrestore(&ctx->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) /* 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523)  * Note: if this is updated, we need to update api.c to patch the new ones in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524)  * too
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) const struct file_operations afu_fops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 	.owner		= THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 	.open           = afu_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 	.poll		= afu_poll,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 	.read		= afu_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 	.release        = afu_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 	.unlocked_ioctl = afu_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 	.compat_ioctl   = afu_compat_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 	.mmap           = afu_mmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) static const struct file_operations afu_master_fops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 	.owner		= THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 	.open           = afu_master_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 	.poll		= afu_poll,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 	.read		= afu_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 	.release        = afu_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 	.unlocked_ioctl = afu_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 	.compat_ioctl   = afu_compat_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 	.mmap           = afu_mmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) static char *cxl_devnode(struct device *dev, umode_t *mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 	if (cpu_has_feature(CPU_FTR_HVMODE) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 	    CXL_DEVT_IS_CARD(dev->devt)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 		 * These minor numbers will eventually be used to program the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 		 * PSL and AFUs once we have dynamic reprogramming support
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 	return kasprintf(GFP_KERNEL, "cxl/%s", dev_name(dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) extern struct class *cxl_class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) static int cxl_add_chardev(struct cxl_afu *afu, dev_t devt, struct cdev *cdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 			   struct device **chardev, char *postfix, char *desc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 			   const struct file_operations *fops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 	struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) 	cdev_init(cdev, fops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 	if ((rc = cdev_add(cdev, devt, 1))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) 		dev_err(&afu->dev, "Unable to add %s chardev: %i\n", desc, rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 	dev = device_create(cxl_class, &afu->dev, devt, afu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) 			"afu%i.%i%s", afu->adapter->adapter_num, afu->slice, postfix);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) 	if (IS_ERR(dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) 		dev_err(&afu->dev, "Unable to create %s chardev in sysfs: %i\n", desc, rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) 		rc = PTR_ERR(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) 	*chardev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) 	cdev_del(cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) int cxl_chardev_d_afu_add(struct cxl_afu *afu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) 	return cxl_add_chardev(afu, CXL_AFU_MKDEV_D(afu), &afu->afu_cdev_d,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) 			       &afu->chardev_d, "d", "dedicated",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) 			       &afu_master_fops); /* Uses master fops */
^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) int cxl_chardev_m_afu_add(struct cxl_afu *afu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) 	return cxl_add_chardev(afu, CXL_AFU_MKDEV_M(afu), &afu->afu_cdev_m,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) 			       &afu->chardev_m, "m", "master",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) 			       &afu_master_fops);
^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) int cxl_chardev_s_afu_add(struct cxl_afu *afu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) 	return cxl_add_chardev(afu, CXL_AFU_MKDEV_S(afu), &afu->afu_cdev_s,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) 			       &afu->chardev_s, "s", "shared",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) 			       &afu_fops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) void cxl_chardev_afu_remove(struct cxl_afu *afu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) 	if (afu->chardev_d) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) 		cdev_del(&afu->afu_cdev_d);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) 		device_unregister(afu->chardev_d);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) 		afu->chardev_d = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) 	if (afu->chardev_m) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) 		cdev_del(&afu->afu_cdev_m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) 		device_unregister(afu->chardev_m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) 		afu->chardev_m = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) 	if (afu->chardev_s) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) 		cdev_del(&afu->afu_cdev_s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) 		device_unregister(afu->chardev_s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) 		afu->chardev_s = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) int cxl_register_afu(struct cxl_afu *afu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) 	afu->dev.class = cxl_class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) 	return device_register(&afu->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) int cxl_register_adapter(struct cxl *adapter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) 	adapter->dev.class = cxl_class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) 	 * Future: When we support dynamically reprogramming the PSL & AFU we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) 	 * will expose the interface to do that via a chardev:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) 	 * adapter->dev.devt = CXL_CARD_MKDEV(adapter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) 	return device_register(&adapter->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) dev_t cxl_get_dev(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) 	return cxl_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) int __init cxl_file_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) 	 * If these change we really need to update API.  Either change some
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) 	 * flags or update API version number CXL_API_VERSION.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) 	BUILD_BUG_ON(CXL_API_VERSION != 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) 	BUILD_BUG_ON(sizeof(struct cxl_ioctl_start_work) != 64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) 	BUILD_BUG_ON(sizeof(struct cxl_event_header) != 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) 	BUILD_BUG_ON(sizeof(struct cxl_event_afu_interrupt) != 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) 	BUILD_BUG_ON(sizeof(struct cxl_event_data_storage) != 32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) 	BUILD_BUG_ON(sizeof(struct cxl_event_afu_error) != 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) 	if ((rc = alloc_chrdev_region(&cxl_dev, 0, CXL_NUM_MINORS, "cxl"))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) 		pr_err("Unable to allocate CXL major number: %i\n", rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) 	pr_devel("CXL device allocated, MAJOR %i\n", MAJOR(cxl_dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) 	cxl_class = class_create(THIS_MODULE, "cxl");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) 	if (IS_ERR(cxl_class)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) 		pr_err("Unable to create CXL class\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) 		rc = PTR_ERR(cxl_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) 	cxl_class->devnode = cxl_devnode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) 	unregister_chrdev_region(cxl_dev, CXL_NUM_MINORS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) void cxl_file_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) 	unregister_chrdev_region(cxl_dev, CXL_NUM_MINORS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) 	class_destroy(cxl_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) }