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