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