blob: d881f5e40ad9edb5f07ebdca9dccf00c9df263c7 [file] [log] [blame]
Frederic Barrat5ef31662018-01-23 12:31:41 +01001// SPDX-License-Identifier: GPL-2.0+
2// Copyright 2017 IBM Corp.
3#include <linux/fs.h>
4#include <linux/poll.h>
5#include <linux/sched/signal.h>
Alastair D'Silva06014662019-03-27 16:31:35 +11006#include <linux/eventfd.h>
Frederic Barrat5ef31662018-01-23 12:31:41 +01007#include <linux/uaccess.h>
8#include <uapi/misc/ocxl.h>
Alastair D'Silvae948e062018-05-11 16:13:01 +10009#include <asm/reg.h>
10#include <asm/switch_to.h>
Frederic Barrat5ef31662018-01-23 12:31:41 +010011#include "ocxl_internal.h"
12
13
14#define OCXL_NUM_MINORS 256 /* Total to reserve */
15
16static dev_t ocxl_dev;
17static struct class *ocxl_class;
Zheng Yongjun52f6b0a2020-12-24 21:24:46 +080018static DEFINE_MUTEX(minors_idr_lock);
Frederic Barrat5ef31662018-01-23 12:31:41 +010019static struct idr minors_idr;
20
Frederic Barrata58d37b2019-06-24 16:41:48 +020021static struct ocxl_file_info *find_and_get_file_info(dev_t devno)
Frederic Barrat5ef31662018-01-23 12:31:41 +010022{
Alastair D'Silva75ca7582019-03-27 16:31:32 +110023 struct ocxl_file_info *info;
Frederic Barrat5ef31662018-01-23 12:31:41 +010024
Frederic Barrata58d37b2019-06-24 16:41:48 +020025 mutex_lock(&minors_idr_lock);
Alastair D'Silva75ca7582019-03-27 16:31:32 +110026 info = idr_find(&minors_idr, MINOR(devno));
Frederic Barrata58d37b2019-06-24 16:41:48 +020027 if (info)
28 get_device(&info->dev);
29 mutex_unlock(&minors_idr_lock);
Alastair D'Silva75ca7582019-03-27 16:31:32 +110030 return info;
Frederic Barrat5ef31662018-01-23 12:31:41 +010031}
32
Alastair D'Silva75ca7582019-03-27 16:31:32 +110033static int allocate_minor(struct ocxl_file_info *info)
Frederic Barrat5ef31662018-01-23 12:31:41 +010034{
35 int minor;
36
37 mutex_lock(&minors_idr_lock);
Alastair D'Silva75ca7582019-03-27 16:31:32 +110038 minor = idr_alloc(&minors_idr, info, 0, OCXL_NUM_MINORS, GFP_KERNEL);
Frederic Barrat5ef31662018-01-23 12:31:41 +010039 mutex_unlock(&minors_idr_lock);
40 return minor;
41}
42
Alastair D'Silva75ca7582019-03-27 16:31:32 +110043static void free_minor(struct ocxl_file_info *info)
Frederic Barrat5ef31662018-01-23 12:31:41 +010044{
45 mutex_lock(&minors_idr_lock);
Alastair D'Silva75ca7582019-03-27 16:31:32 +110046 idr_remove(&minors_idr, MINOR(info->dev.devt));
Frederic Barrat5ef31662018-01-23 12:31:41 +010047 mutex_unlock(&minors_idr_lock);
48}
49
50static int afu_open(struct inode *inode, struct file *file)
51{
Alastair D'Silva75ca7582019-03-27 16:31:32 +110052 struct ocxl_file_info *info;
Frederic Barrat5ef31662018-01-23 12:31:41 +010053 struct ocxl_context *ctx;
54 int rc;
55
56 pr_debug("%s for device %x\n", __func__, inode->i_rdev);
57
Frederic Barrata58d37b2019-06-24 16:41:48 +020058 info = find_and_get_file_info(inode->i_rdev);
Alastair D'Silva75ca7582019-03-27 16:31:32 +110059 if (!info)
Frederic Barrat5ef31662018-01-23 12:31:41 +010060 return -ENODEV;
61
Alastair D'Silvab9721d22019-03-27 16:31:33 +110062 rc = ocxl_context_alloc(&ctx, info->afu, inode->i_mapping);
Frederic Barrata58d37b2019-06-24 16:41:48 +020063 if (rc) {
64 put_device(&info->dev);
Alastair D'Silva75ca7582019-03-27 16:31:32 +110065 return rc;
Frederic Barrata58d37b2019-06-24 16:41:48 +020066 }
67 put_device(&info->dev);
Alastair D'Silva75ca7582019-03-27 16:31:32 +110068 file->private_data = ctx;
69 return 0;
Frederic Barrat5ef31662018-01-23 12:31:41 +010070}
71
72static long afu_ioctl_attach(struct ocxl_context *ctx,
73 struct ocxl_ioctl_attach __user *uarg)
74{
75 struct ocxl_ioctl_attach arg;
76 u64 amr = 0;
Frederic Barrat5ef31662018-01-23 12:31:41 +010077
78 pr_debug("%s for context %d\n", __func__, ctx->pasid);
79
80 if (copy_from_user(&arg, uarg, sizeof(arg)))
81 return -EFAULT;
82
83 /* Make sure reserved fields are not set for forward compatibility */
84 if (arg.reserved1 || arg.reserved2 || arg.reserved3)
85 return -EINVAL;
86
87 amr = arg.amr & mfspr(SPRN_UAMOR);
Minghao Chi30e120e2021-12-15 06:04:38 +000088 return ocxl_context_attach(ctx, amr, current->mm);
Frederic Barrat5ef31662018-01-23 12:31:41 +010089}
90
Alastair D'Silva07c5ccd2018-02-22 15:17:38 +110091static long afu_ioctl_get_metadata(struct ocxl_context *ctx,
92 struct ocxl_ioctl_metadata __user *uarg)
93{
94 struct ocxl_ioctl_metadata arg;
95
96 memset(&arg, 0, sizeof(arg));
97
98 arg.version = 0;
99
100 arg.afu_version_major = ctx->afu->config.version_major;
101 arg.afu_version_minor = ctx->afu->config.version_minor;
102 arg.pasid = ctx->pasid;
103 arg.pp_mmio_size = ctx->afu->config.pp_mmio_stride;
104 arg.global_mmio_size = ctx->afu->config.global_mmio_size;
105
106 if (copy_to_user(uarg, &arg, sizeof(arg)))
107 return -EFAULT;
108
109 return 0;
110}
111
Alastair D'Silvae948e062018-05-11 16:13:01 +1000112#ifdef CONFIG_PPC64
113static long afu_ioctl_enable_p9_wait(struct ocxl_context *ctx,
114 struct ocxl_ioctl_p9_wait __user *uarg)
115{
116 struct ocxl_ioctl_p9_wait arg;
117
118 memset(&arg, 0, sizeof(arg));
119
120 if (cpu_has_feature(CPU_FTR_P9_TIDR)) {
121 enum ocxl_context_status status;
122
123 // Locks both status & tidr
124 mutex_lock(&ctx->status_mutex);
125 if (!ctx->tidr) {
Wei Yongjun2e5c93d2018-06-05 09:16:21 +0000126 if (set_thread_tidr(current)) {
127 mutex_unlock(&ctx->status_mutex);
Alastair D'Silvae948e062018-05-11 16:13:01 +1000128 return -ENOENT;
Wei Yongjun2e5c93d2018-06-05 09:16:21 +0000129 }
Alastair D'Silvae948e062018-05-11 16:13:01 +1000130
131 ctx->tidr = current->thread.tidr;
132 }
133
134 status = ctx->status;
135 mutex_unlock(&ctx->status_mutex);
136
137 if (status == ATTACHED) {
Alastair D'Silva9c4ae062019-03-25 16:34:52 +1100138 int rc = ocxl_link_update_pe(ctx->afu->fn->link,
139 ctx->pasid, ctx->tidr);
Alastair D'Silvae948e062018-05-11 16:13:01 +1000140
Alastair D'Silvae948e062018-05-11 16:13:01 +1000141 if (rc)
142 return rc;
143 }
144
145 arg.thread_id = ctx->tidr;
146 } else
147 return -ENOENT;
148
149 if (copy_to_user(uarg, &arg, sizeof(arg)))
150 return -EFAULT;
151
152 return 0;
153}
154#endif
155
Alastair D'Silva02a8e5b2018-05-11 16:13:02 +1000156
157static long afu_ioctl_get_features(struct ocxl_context *ctx,
158 struct ocxl_ioctl_features __user *uarg)
159{
160 struct ocxl_ioctl_features arg;
161
162 memset(&arg, 0, sizeof(arg));
163
164#ifdef CONFIG_PPC64
165 if (cpu_has_feature(CPU_FTR_P9_TIDR))
166 arg.flags[0] |= OCXL_IOCTL_FEATURES_FLAGS0_P9_WAIT;
167#endif
168
169 if (copy_to_user(uarg, &arg, sizeof(arg)))
170 return -EFAULT;
171
172 return 0;
173}
174
Frederic Barrat5ef31662018-01-23 12:31:41 +0100175#define CMD_STR(x) (x == OCXL_IOCTL_ATTACH ? "ATTACH" : \
Frederic Barrataeddad12018-01-23 12:31:42 +0100176 x == OCXL_IOCTL_IRQ_ALLOC ? "IRQ_ALLOC" : \
177 x == OCXL_IOCTL_IRQ_FREE ? "IRQ_FREE" : \
178 x == OCXL_IOCTL_IRQ_SET_FD ? "IRQ_SET_FD" : \
Alastair D'Silva07c5ccd2018-02-22 15:17:38 +1100179 x == OCXL_IOCTL_GET_METADATA ? "GET_METADATA" : \
Alastair D'Silvae948e062018-05-11 16:13:01 +1000180 x == OCXL_IOCTL_ENABLE_P9_WAIT ? "ENABLE_P9_WAIT" : \
Alastair D'Silva02a8e5b2018-05-11 16:13:02 +1000181 x == OCXL_IOCTL_GET_FEATURES ? "GET_FEATURES" : \
Frederic Barrat5ef31662018-01-23 12:31:41 +0100182 "UNKNOWN")
183
Alastair D'Silva06014662019-03-27 16:31:35 +1100184static irqreturn_t irq_handler(void *private)
185{
186 struct eventfd_ctx *ev_ctx = private;
187
188 eventfd_signal(ev_ctx, 1);
189 return IRQ_HANDLED;
190}
191
192static void irq_free(void *private)
193{
194 struct eventfd_ctx *ev_ctx = private;
195
196 eventfd_ctx_put(ev_ctx);
197}
198
Frederic Barrat5ef31662018-01-23 12:31:41 +0100199static long afu_ioctl(struct file *file, unsigned int cmd,
200 unsigned long args)
201{
202 struct ocxl_context *ctx = file->private_data;
Frederic Barrataeddad12018-01-23 12:31:42 +0100203 struct ocxl_ioctl_irq_fd irq_fd;
Alastair D'Silva06014662019-03-27 16:31:35 +1100204 struct eventfd_ctx *ev_ctx;
Alastair D'Silva2ec3b7e2019-03-27 16:31:34 +1100205 int irq_id;
Frederic Barrataeddad12018-01-23 12:31:42 +0100206 u64 irq_offset;
Frederic Barrat5ef31662018-01-23 12:31:41 +0100207 long rc;
Alastair D'Silva75ca7582019-03-27 16:31:32 +1100208 bool closed;
Frederic Barrat5ef31662018-01-23 12:31:41 +0100209
210 pr_debug("%s for context %d, command %s\n", __func__, ctx->pasid,
211 CMD_STR(cmd));
212
Alastair D'Silva75ca7582019-03-27 16:31:32 +1100213 mutex_lock(&ctx->status_mutex);
214 closed = (ctx->status == CLOSED);
215 mutex_unlock(&ctx->status_mutex);
216
217 if (closed)
Frederic Barrat5ef31662018-01-23 12:31:41 +0100218 return -EIO;
219
220 switch (cmd) {
221 case OCXL_IOCTL_ATTACH:
222 rc = afu_ioctl_attach(ctx,
223 (struct ocxl_ioctl_attach __user *) args);
224 break;
225
Frederic Barrataeddad12018-01-23 12:31:42 +0100226 case OCXL_IOCTL_IRQ_ALLOC:
Alastair D'Silva2ec3b7e2019-03-27 16:31:34 +1100227 rc = ocxl_afu_irq_alloc(ctx, &irq_id);
Frederic Barrataeddad12018-01-23 12:31:42 +0100228 if (!rc) {
Alastair D'Silva2ec3b7e2019-03-27 16:31:34 +1100229 irq_offset = ocxl_irq_id_to_offset(ctx, irq_id);
Frederic Barrataeddad12018-01-23 12:31:42 +0100230 rc = copy_to_user((u64 __user *) args, &irq_offset,
231 sizeof(irq_offset));
Frederic Barrat423688a2018-02-16 14:01:18 +0100232 if (rc) {
Alastair D'Silva2ec3b7e2019-03-27 16:31:34 +1100233 ocxl_afu_irq_free(ctx, irq_id);
Frederic Barrat423688a2018-02-16 14:01:18 +0100234 return -EFAULT;
235 }
Frederic Barrataeddad12018-01-23 12:31:42 +0100236 }
237 break;
238
239 case OCXL_IOCTL_IRQ_FREE:
240 rc = copy_from_user(&irq_offset, (u64 __user *) args,
241 sizeof(irq_offset));
242 if (rc)
243 return -EFAULT;
Alastair D'Silva2ec3b7e2019-03-27 16:31:34 +1100244 irq_id = ocxl_irq_offset_to_id(ctx, irq_offset);
245 rc = ocxl_afu_irq_free(ctx, irq_id);
Frederic Barrataeddad12018-01-23 12:31:42 +0100246 break;
247
248 case OCXL_IOCTL_IRQ_SET_FD:
249 rc = copy_from_user(&irq_fd, (u64 __user *) args,
250 sizeof(irq_fd));
251 if (rc)
252 return -EFAULT;
253 if (irq_fd.reserved)
254 return -EINVAL;
Alastair D'Silva2ec3b7e2019-03-27 16:31:34 +1100255 irq_id = ocxl_irq_offset_to_id(ctx, irq_fd.irq_offset);
Alastair D'Silva06014662019-03-27 16:31:35 +1100256 ev_ctx = eventfd_ctx_fdget(irq_fd.eventfd);
Wei Yongjun6be6a8d2019-05-04 07:04:30 +0000257 if (IS_ERR(ev_ctx))
258 return PTR_ERR(ev_ctx);
Alastair D'Silva06014662019-03-27 16:31:35 +1100259 rc = ocxl_irq_set_handler(ctx, irq_id, irq_handler, irq_free, ev_ctx);
Frederic Barrataeddad12018-01-23 12:31:42 +0100260 break;
261
Alastair D'Silva07c5ccd2018-02-22 15:17:38 +1100262 case OCXL_IOCTL_GET_METADATA:
263 rc = afu_ioctl_get_metadata(ctx,
264 (struct ocxl_ioctl_metadata __user *) args);
265 break;
266
Alastair D'Silvae948e062018-05-11 16:13:01 +1000267#ifdef CONFIG_PPC64
268 case OCXL_IOCTL_ENABLE_P9_WAIT:
269 rc = afu_ioctl_enable_p9_wait(ctx,
270 (struct ocxl_ioctl_p9_wait __user *) args);
271 break;
272#endif
273
Alastair D'Silva02a8e5b2018-05-11 16:13:02 +1000274 case OCXL_IOCTL_GET_FEATURES:
275 rc = afu_ioctl_get_features(ctx,
276 (struct ocxl_ioctl_features __user *) args);
277 break;
278
Frederic Barrat5ef31662018-01-23 12:31:41 +0100279 default:
280 rc = -EINVAL;
281 }
282 return rc;
283}
284
285static long afu_compat_ioctl(struct file *file, unsigned int cmd,
286 unsigned long args)
287{
288 return afu_ioctl(file, cmd, args);
289}
290
291static int afu_mmap(struct file *file, struct vm_area_struct *vma)
292{
293 struct ocxl_context *ctx = file->private_data;
294
295 pr_debug("%s for context %d\n", __func__, ctx->pasid);
296 return ocxl_context_mmap(ctx, vma);
297}
298
299static bool has_xsl_error(struct ocxl_context *ctx)
300{
301 bool ret;
302
303 mutex_lock(&ctx->xsl_error_lock);
304 ret = !!ctx->xsl_error.addr;
305 mutex_unlock(&ctx->xsl_error_lock);
306
307 return ret;
308}
309
310/*
311 * Are there any events pending on the AFU
312 * ctx: The AFU context
313 * Returns: true if there are events pending
314 */
315static bool afu_events_pending(struct ocxl_context *ctx)
316{
317 if (has_xsl_error(ctx))
318 return true;
319 return false;
320}
321
322static unsigned int afu_poll(struct file *file, struct poll_table_struct *wait)
323{
324 struct ocxl_context *ctx = file->private_data;
325 unsigned int mask = 0;
326 bool closed;
327
328 pr_debug("%s for context %d\n", __func__, ctx->pasid);
329
330 poll_wait(file, &ctx->events_wq, wait);
331
332 mutex_lock(&ctx->status_mutex);
333 closed = (ctx->status == CLOSED);
334 mutex_unlock(&ctx->status_mutex);
335
336 if (afu_events_pending(ctx))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800337 mask = EPOLLIN | EPOLLRDNORM;
Frederic Barrat5ef31662018-01-23 12:31:41 +0100338 else if (closed)
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800339 mask = EPOLLERR;
Frederic Barrat5ef31662018-01-23 12:31:41 +0100340
341 return mask;
342}
343
344/*
345 * Populate the supplied buffer with a single XSL error
346 * ctx: The AFU context to report the error from
347 * header: the event header to populate
348 * buf: The buffer to write the body into (should be at least
349 * AFU_EVENT_BODY_XSL_ERROR_SIZE)
350 * Return: the amount of buffer that was populated
351 */
352static ssize_t append_xsl_error(struct ocxl_context *ctx,
353 struct ocxl_kernel_event_header *header,
354 char __user *buf)
355{
356 struct ocxl_kernel_event_xsl_fault_error body;
357
358 memset(&body, 0, sizeof(body));
359
360 mutex_lock(&ctx->xsl_error_lock);
361 if (!ctx->xsl_error.addr) {
362 mutex_unlock(&ctx->xsl_error_lock);
363 return 0;
364 }
365
366 body.addr = ctx->xsl_error.addr;
367 body.dsisr = ctx->xsl_error.dsisr;
368 body.count = ctx->xsl_error.count;
369
370 ctx->xsl_error.addr = 0;
371 ctx->xsl_error.dsisr = 0;
372 ctx->xsl_error.count = 0;
373
374 mutex_unlock(&ctx->xsl_error_lock);
375
376 header->type = OCXL_AFU_EVENT_XSL_FAULT_ERROR;
377
378 if (copy_to_user(buf, &body, sizeof(body)))
379 return -EFAULT;
380
381 return sizeof(body);
382}
383
384#define AFU_EVENT_BODY_MAX_SIZE sizeof(struct ocxl_kernel_event_xsl_fault_error)
385
386/*
387 * Reports events on the AFU
388 * Format:
389 * Header (struct ocxl_kernel_event_header)
390 * Body (struct ocxl_kernel_event_*)
391 * Header...
392 */
393static ssize_t afu_read(struct file *file, char __user *buf, size_t count,
394 loff_t *off)
395{
396 struct ocxl_context *ctx = file->private_data;
397 struct ocxl_kernel_event_header header;
398 ssize_t rc;
Colin Ian Kingdedab7f2018-01-30 15:11:44 +0000399 ssize_t used = 0;
Frederic Barrat5ef31662018-01-23 12:31:41 +0100400 DEFINE_WAIT(event_wait);
401
402 memset(&header, 0, sizeof(header));
403
404 /* Require offset to be 0 */
405 if (*off != 0)
406 return -EINVAL;
407
408 if (count < (sizeof(struct ocxl_kernel_event_header) +
409 AFU_EVENT_BODY_MAX_SIZE))
410 return -EINVAL;
411
412 for (;;) {
413 prepare_to_wait(&ctx->events_wq, &event_wait,
414 TASK_INTERRUPTIBLE);
415
416 if (afu_events_pending(ctx))
417 break;
418
419 if (ctx->status == CLOSED)
420 break;
421
422 if (file->f_flags & O_NONBLOCK) {
423 finish_wait(&ctx->events_wq, &event_wait);
424 return -EAGAIN;
425 }
426
427 if (signal_pending(current)) {
428 finish_wait(&ctx->events_wq, &event_wait);
429 return -ERESTARTSYS;
430 }
431
432 schedule();
433 }
434
435 finish_wait(&ctx->events_wq, &event_wait);
436
437 if (has_xsl_error(ctx)) {
438 used = append_xsl_error(ctx, &header, buf + sizeof(header));
439 if (used < 0)
440 return used;
441 }
442
443 if (!afu_events_pending(ctx))
444 header.flags |= OCXL_KERNEL_EVENT_FLAG_LAST;
445
446 if (copy_to_user(buf, &header, sizeof(header)))
447 return -EFAULT;
448
449 used += sizeof(header);
450
Frederic Barrat423688a2018-02-16 14:01:18 +0100451 rc = used;
Frederic Barrat5ef31662018-01-23 12:31:41 +0100452 return rc;
453}
454
455static int afu_release(struct inode *inode, struct file *file)
456{
457 struct ocxl_context *ctx = file->private_data;
458 int rc;
459
460 pr_debug("%s for device %x\n", __func__, inode->i_rdev);
461 rc = ocxl_context_detach(ctx);
462 mutex_lock(&ctx->mapping_lock);
463 ctx->mapping = NULL;
464 mutex_unlock(&ctx->mapping_lock);
465 wake_up_all(&ctx->events_wq);
466 if (rc != -EBUSY)
467 ocxl_context_free(ctx);
468 return 0;
469}
470
471static const struct file_operations ocxl_afu_fops = {
472 .owner = THIS_MODULE,
473 .open = afu_open,
474 .unlocked_ioctl = afu_ioctl,
475 .compat_ioctl = afu_compat_ioctl,
476 .mmap = afu_mmap,
477 .poll = afu_poll,
478 .read = afu_read,
479 .release = afu_release,
480};
481
Alastair D'Silva75ca7582019-03-27 16:31:32 +1100482// Free the info struct
483static void info_release(struct device *dev)
484{
485 struct ocxl_file_info *info = container_of(dev, struct ocxl_file_info, dev);
486
Alastair D'Silva75ca7582019-03-27 16:31:32 +1100487 ocxl_afu_put(info->afu);
488 kfree(info);
489}
490
491static int ocxl_file_make_visible(struct ocxl_file_info *info)
Frederic Barrat5ef31662018-01-23 12:31:41 +0100492{
493 int rc;
494
Alastair D'Silva75ca7582019-03-27 16:31:32 +1100495 cdev_init(&info->cdev, &ocxl_afu_fops);
496 rc = cdev_add(&info->cdev, info->dev.devt, 1);
Frederic Barrat5ef31662018-01-23 12:31:41 +0100497 if (rc) {
Alastair D'Silva75ca7582019-03-27 16:31:32 +1100498 dev_err(&info->dev, "Unable to add afu char device: %d\n", rc);
Frederic Barrat5ef31662018-01-23 12:31:41 +0100499 return rc;
500 }
Alastair D'Silva75ca7582019-03-27 16:31:32 +1100501
Frederic Barrat5ef31662018-01-23 12:31:41 +0100502 return 0;
503}
504
Alastair D'Silva75ca7582019-03-27 16:31:32 +1100505static void ocxl_file_make_invisible(struct ocxl_file_info *info)
Frederic Barrat5ef31662018-01-23 12:31:41 +0100506{
Alastair D'Silva75ca7582019-03-27 16:31:32 +1100507 cdev_del(&info->cdev);
Frederic Barrat5ef31662018-01-23 12:31:41 +0100508}
509
Alastair D'Silva75ca7582019-03-27 16:31:32 +1100510int ocxl_file_register_afu(struct ocxl_afu *afu)
Frederic Barrat5ef31662018-01-23 12:31:41 +0100511{
512 int minor;
Alastair D'Silva75ca7582019-03-27 16:31:32 +1100513 int rc;
514 struct ocxl_file_info *info;
515 struct ocxl_fn *fn = afu->fn;
516 struct pci_dev *pci_dev = to_pci_dev(fn->dev.parent);
Frederic Barrat5ef31662018-01-23 12:31:41 +0100517
Alastair D'Silva75ca7582019-03-27 16:31:32 +1100518 info = kzalloc(sizeof(*info), GFP_KERNEL);
519 if (info == NULL)
520 return -ENOMEM;
521
522 minor = allocate_minor(info);
523 if (minor < 0) {
524 kfree(info);
Frederic Barrat5ef31662018-01-23 12:31:41 +0100525 return minor;
Alastair D'Silva75ca7582019-03-27 16:31:32 +1100526 }
527
528 info->dev.parent = &fn->dev;
529 info->dev.devt = MKDEV(MAJOR(ocxl_dev), minor);
530 info->dev.class = ocxl_class;
531 info->dev.release = info_release;
532
533 info->afu = afu;
534 ocxl_afu_get(afu);
535
536 rc = dev_set_name(&info->dev, "%s.%s.%hhu",
537 afu->config.name, dev_name(&pci_dev->dev), afu->config.idx);
538 if (rc)
539 goto err_put;
540
541 rc = device_register(&info->dev);
542 if (rc)
543 goto err_put;
544
545 rc = ocxl_sysfs_register_afu(info);
546 if (rc)
547 goto err_unregister;
548
549 rc = ocxl_file_make_visible(info);
550 if (rc)
551 goto err_unregister;
552
553 ocxl_afu_set_private(afu, info);
554
555 return 0;
556
557err_unregister:
558 ocxl_sysfs_unregister_afu(info); // safe to call even if register failed
559 device_unregister(&info->dev);
560err_put:
561 ocxl_afu_put(afu);
562 free_minor(info);
563 kfree(info);
564 return rc;
Frederic Barrat5ef31662018-01-23 12:31:41 +0100565}
566
Alastair D'Silva75ca7582019-03-27 16:31:32 +1100567void ocxl_file_unregister_afu(struct ocxl_afu *afu)
Frederic Barrat5ef31662018-01-23 12:31:41 +0100568{
Alastair D'Silva75ca7582019-03-27 16:31:32 +1100569 struct ocxl_file_info *info = ocxl_afu_get_private(afu);
570
571 if (!info)
572 return;
573
574 ocxl_file_make_invisible(info);
575 ocxl_sysfs_unregister_afu(info);
Frederic Barrata58d37b2019-06-24 16:41:48 +0200576 free_minor(info);
Alastair D'Silva75ca7582019-03-27 16:31:32 +1100577 device_unregister(&info->dev);
Frederic Barrat5ef31662018-01-23 12:31:41 +0100578}
579
580static char *ocxl_devnode(struct device *dev, umode_t *mode)
581{
582 return kasprintf(GFP_KERNEL, "ocxl/%s", dev_name(dev));
583}
584
585int ocxl_file_init(void)
586{
587 int rc;
588
Frederic Barrat5ef31662018-01-23 12:31:41 +0100589 idr_init(&minors_idr);
590
591 rc = alloc_chrdev_region(&ocxl_dev, 0, OCXL_NUM_MINORS, "ocxl");
592 if (rc) {
593 pr_err("Unable to allocate ocxl major number: %d\n", rc);
594 return rc;
595 }
596
597 ocxl_class = class_create(THIS_MODULE, "ocxl");
598 if (IS_ERR(ocxl_class)) {
599 pr_err("Unable to create ocxl class\n");
600 unregister_chrdev_region(ocxl_dev, OCXL_NUM_MINORS);
601 return PTR_ERR(ocxl_class);
602 }
603
604 ocxl_class->devnode = ocxl_devnode;
605 return 0;
606}
607
608void ocxl_file_exit(void)
609{
610 class_destroy(ocxl_class);
611 unregister_chrdev_region(ocxl_dev, OCXL_NUM_MINORS);
612 idr_destroy(&minors_idr);
613}