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