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