blob: 76c0b0ca9388c05840a6c6644b70163d64eb378b [file] [log] [blame]
Ian Munsief204e0b2014-10-08 19:55:02 +11001/*
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 Molnar174cd4b2017-02-02 19:15:33 +010015#include <linux/sched/signal.h>
Ian Munsief204e0b2014-10-08 19:55:02 +110016#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 Lombard6dd2d232017-04-07 16:11:55 +020021#include <linux/sched/mm.h>
Frederic Barrat03b8abe2017-09-03 20:15:13 +020022#include <linux/mmu_context.h>
Ian Munsief204e0b2014-10-08 19:55:02 +110023#include <asm/cputable.h>
24#include <asm/current.h>
25#include <asm/copro.h>
26
27#include "cxl.h"
Ian Munsie9bcf28c2015-01-09 20:34:36 +110028#include "trace.h"
Ian Munsief204e0b2014-10-08 19:55:02 +110029
30#define CXL_NUM_MINORS 256 /* Total to reserve */
Ian Munsief204e0b2014-10-08 19:55:02 +110031
Ian Munsief204e0b2014-10-08 19:55:02 +110032#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 Munsief204e0b2014-10-08 19:55:02 +110039#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
43static dev_t cxl_dev;
44
45static struct class *cxl_class;
46
47static 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 Jain1b5df592015-11-16 09:33:45 +053069
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 Munsief204e0b2014-10-08 19:55:02 +110076 spin_unlock(&adapter->afu_list_lock);
77
78 if (!afu->current_mode)
79 goto err_put_afu;
80
Christophe Lombard0d400f72016-03-04 12:26:41 +010081 if (!cxl_ops->link_ok(adapter, afu)) {
Daniel Axtens0b3f9c72015-08-14 17:41:18 +100082 rc = -EIO;
83 goto err_put_afu;
84 }
85
Ian Munsief204e0b2014-10-08 19:55:02 +110086 if (!(ctx = cxl_context_alloc())) {
87 rc = -ENOMEM;
88 goto err_put_afu;
89 }
90
Frederic Barratbdecf762016-11-18 23:00:31 +110091 rc = cxl_context_init(ctx, afu, master);
92 if (rc)
Ian Munsief204e0b2014-10-08 19:55:02 +110093 goto err_put_afu;
94
Frederic Barratbdecf762016-11-18 23:00:31 +110095 cxl_context_set_mapping(ctx, inode->i_mapping);
96
Ian Munsief204e0b2014-10-08 19:55:02 +110097 pr_devel("afu_open pe: %i\n", ctx->pe);
98 file->private_data = ctx;
Ian Munsief204e0b2014-10-08 19:55:02 +110099
Vaibhav Jain1b5df592015-11-16 09:33:45 +0530100 /* indicate success */
101 rc = 0;
Ian Munsief204e0b2014-10-08 19:55:02 +1100102
103err_put_afu:
Vaibhav Jain1b5df592015-11-16 09:33:45 +0530104 /* release the ref taken earlier */
105 cxl_afu_put(afu);
Ian Munsief204e0b2014-10-08 19:55:02 +1100106err_put_adapter:
107 put_device(&adapter->dev);
108 return rc;
109}
Michael Neuling05203362015-05-27 16:07:17 +1000110
111int afu_open(struct inode *inode, struct file *file)
Ian Munsief204e0b2014-10-08 19:55:02 +1100112{
113 return __afu_open(inode, file, false);
114}
115
116static int afu_master_open(struct inode *inode, struct file *file)
117{
118 return __afu_open(inode, file, true);
119}
120
Michael Neuling05203362015-05-27 16:07:17 +1000121int afu_release(struct inode *inode, struct file *file)
Ian Munsief204e0b2014-10-08 19:55:02 +1100122{
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 Donnellan5f81b952015-09-30 11:58:07 +1000129
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 Munsieb1234292014-12-08 19:18:01 +1100139
Ian Munsief204e0b2014-10-08 19:55:02 +1100140 /*
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 Munsief204e0b2014-10-08 19:55:02 +1100148 return 0;
149}
150
151static 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 Munsie0712dc72015-01-07 16:33:04 +1100160 /* Do this outside the status_mutex to avoid a circular dependency with
161 * the locking in cxl_mmap_fault() */
Frederic Barratcec422c2017-06-06 11:43:41 +0200162 if (copy_from_user(&work, uwork, sizeof(work)))
163 return -EFAULT;
Ian Munsief204e0b2014-10-08 19:55:02 +1100164
Ian Munsie0712dc72015-01-07 16:33:04 +1100165 mutex_lock(&ctx->status_mutex);
166 if (ctx->status != OPENED) {
167 rc = -EIO;
168 goto out;
169 }
170
Ian Munsief204e0b2014-10-08 19:55:02 +1100171 /*
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 Munsied9232a32015-07-23 16:43:56 +1000195 ctx->mmio_err_ff = !!(work.flags & CXL_START_WORK_ERR_FF);
196
Ian Munsief204e0b2014-10-08 19:55:02 +1100197 /*
Vaibhav Jaina05b82d2016-10-21 14:53:53 +0530198 * 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 Munsief204e0b2014-10-08 19:55:02 +1100208 * 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 Jain7b8ad492015-11-24 16:26:18 +0530212 * 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 Munsief204e0b2014-10-08 19:55:02 +1100215 */
Vaibhav Jain7b8ad492015-11-24 16:26:18 +0530216 ctx->pid = get_task_pid(current, PIDTYPE_PID);
Ian Munsief204e0b2014-10-08 19:55:02 +1100217
Christophe Lombard6dd2d232017-04-07 16:11:55 +0200218 /* 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 Barrat03b8abe2017-09-03 20:15:13 +0200224 if (ctx->mm) {
225 /* decrement the use count from above */
Christophe Lombard6dd2d232017-04-07 16:11:55 +0200226 mmput(ctx->mm);
Frederic Barrat03b8abe2017-09-03 20:15:13 +0200227 /* make TLBIs for this context global */
228 mm_context_add_copro(ctx->mm);
229 }
Vaibhav Jain70b565b2016-10-14 15:08:36 +0530230
Frederic Barrat197267d2017-08-30 12:15:49 +0200231 /*
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 Barrat03b8abe2017-09-03 20:15:13 +0200237 /*
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 Munsie9bcf28c2015-01-09 20:34:36 +1100251 trace_cxl_attach(ctx, work.work_element_descriptor, work.num_interrupts, amr);
252
Frederic Barrat5be587b2016-03-04 12:26:28 +0100253 if ((rc = cxl_ops->attach_process(ctx, false, work.work_element_descriptor,
254 amr))) {
Michael Neuling64288322015-05-27 16:07:07 +1000255 afu_release_irqs(ctx, ctx);
Vaibhav Jain70b565b2016-10-14 15:08:36 +0530256 cxl_adapter_context_put(ctx->afu->adapter);
Vaibhav Jaina05b82d2016-10-21 14:53:53 +0530257 put_pid(ctx->pid);
Christophe Lombard6dd2d232017-04-07 16:11:55 +0200258 ctx->pid = NULL;
Frederic Barrat197267d2017-08-30 12:15:49 +0200259 cxl_ctx_put();
Christophe Lombard6dd2d232017-04-07 16:11:55 +0200260 cxl_context_mm_count_put(ctx);
Frederic Barrat03b8abe2017-09-03 20:15:13 +0200261 if (ctx->mm)
262 mm_context_remove_copro(ctx->mm);
Ian Munsief204e0b2014-10-08 19:55:02 +1100263 goto out;
Ian Munsie456295e2014-12-08 19:17:57 +1100264 }
Ian Munsief204e0b2014-10-08 19:55:02 +1100265
266 ctx->status = STARTED;
267 rc = 0;
268out:
269 mutex_unlock(&ctx->status_mutex);
270 return rc;
271}
Frederic Barrat5be587b2016-03-04 12:26:28 +0100272
Ian Munsief204e0b2014-10-08 19:55:02 +1100273static 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 Lombard14baf4d2016-03-04 12:26:36 +0100278 if (copy_to_user(upe, &ctx->external_pe, sizeof(__u32)))
Ian Munsief204e0b2014-10-08 19:55:02 +1100279 return -EFAULT;
280
281 return 0;
282}
283
Vaibhav Jain27d4dc72015-04-29 15:47:23 +0530284static 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 Neuling05203362015-05-27 16:07:17 +1000303long afu_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Ian Munsief204e0b2014-10-08 19:55:02 +1100304{
305 struct cxl_context *ctx = file->private_data;
306
307 if (ctx->status == CLOSED)
308 return -EIO;
309
Christophe Lombard0d400f72016-03-04 12:26:41 +0100310 if (!cxl_ops->link_ok(ctx->afu->adapter, ctx->afu))
Daniel Axtens0b3f9c72015-08-14 17:41:18 +1000311 return -EIO;
312
Ian Munsief204e0b2014-10-08 19:55:02 +1100313 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 Jain27d4dc72015-04-29 15:47:23 +0530319 case CXL_IOCTL_GET_AFU_ID:
320 return afu_ioctl_get_afu_id(ctx, (struct cxl_afu_id __user *)
321 arg);
Ian Munsief204e0b2014-10-08 19:55:02 +1100322 }
323 return -EINVAL;
324}
325
Daniel Axtens3d6b0402015-08-07 13:18:18 +1000326static long afu_compat_ioctl(struct file *file, unsigned int cmd,
Ian Munsief204e0b2014-10-08 19:55:02 +1100327 unsigned long arg)
328{
329 return afu_ioctl(file, cmd, arg);
330}
331
Michael Neuling05203362015-05-27 16:07:17 +1000332int afu_mmap(struct file *file, struct vm_area_struct *vm)
Ian Munsief204e0b2014-10-08 19:55:02 +1100333{
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 Lombard0d400f72016-03-04 12:26:41 +0100340 if (!cxl_ops->link_ok(ctx->afu->adapter, ctx->afu))
Daniel Axtens0b3f9c72015-08-14 17:41:18 +1000341 return -EIO;
342
Ian Munsief204e0b2014-10-08 19:55:02 +1100343 return cxl_context_iomap(ctx, vm);
344}
345
Philippe Bergheaudb8102532016-06-23 15:03:53 +0200346static 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 Neuling05203362015-05-27 16:07:17 +1000357unsigned int afu_poll(struct file *file, struct poll_table_struct *poll)
Ian Munsief204e0b2014-10-08 19:55:02 +1100358{
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 Bergheaudb8102532016-06-23 15:03:53 +0200369 if (ctx_event_pending(ctx))
Ian Munsief204e0b2014-10-08 19:55:02 +1100370 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 Bergheaudb8102532016-06-23 15:03:53 +0200382static 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 Munsief204e0b2014-10-08 19:55:02 +1100386{
Philippe Bergheaudb8102532016-06-23 15:03:53 +0200387 /* 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 Munsief204e0b2014-10-08 19:55:02 +1100415}
416
Michael Neuling05203362015-05-27 16:07:17 +1000417ssize_t afu_read(struct file *file, char __user *buf, size_t count,
Ian Munsief204e0b2014-10-08 19:55:02 +1100418 loff_t *off)
419{
420 struct cxl_context *ctx = file->private_data;
Philippe Bergheaudb8102532016-06-23 15:03:53 +0200421 struct cxl_event_afu_driver_reserved *pl = NULL;
Ian Munsief204e0b2014-10-08 19:55:02 +1100422 struct cxl_event event;
423 unsigned long flags;
Ian Munsied53ba6b2014-10-09 11:17:46 +1100424 int rc;
Ian Munsief204e0b2014-10-08 19:55:02 +1100425 DEFINE_WAIT(wait);
426
Christophe Lombard0d400f72016-03-04 12:26:41 +0100427 if (!cxl_ops->link_ok(ctx->afu->adapter, ctx->afu))
Daniel Axtens0b3f9c72015-08-14 17:41:18 +1000428 return -EIO;
429
Ian Munsief204e0b2014-10-08 19:55:02 +1100430 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 Bergheaudb8102532016-06-23 15:03:53 +0200437 if (ctx_event_pending(ctx) || (ctx->status == CLOSED))
Ian Munsief204e0b2014-10-08 19:55:02 +1100438 break;
439
Christophe Lombard0d400f72016-03-04 12:26:41 +0100440 if (!cxl_ops->link_ok(ctx->afu->adapter, ctx->afu)) {
Daniel Axtens0b3f9c72015-08-14 17:41:18 +1000441 rc = -EIO;
442 goto out;
443 }
444
Ian Munsied53ba6b2014-10-09 11:17:46 +1100445 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 Munsief204e0b2014-10-08 19:55:02 +1100455 spin_unlock_irqrestore(&ctx->lock, flags);
Ian Munsief204e0b2014-10-08 19:55:02 +1100456 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 Bergheaudb8102532016-06-23 15:03:53 +0200467 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 Munsief204e0b2014-10-08 19:55:02 +1100473 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 Bergheaudb8102532016-06-23 15:03:53 +0200502 if (event.header.type == CXL_EVENT_AFU_DRIVER)
503 return afu_driver_event_copy(ctx, buf, &event, pl);
504
Ian Munsief204e0b2014-10-08 19:55:02 +1100505 if (copy_to_user(buf, &event, event.header.size))
506 return -EFAULT;
507 return event.header.size;
Ian Munsied53ba6b2014-10-09 11:17:46 +1100508
509out:
510 finish_wait(&ctx->wq, &wait);
511 spin_unlock_irqrestore(&ctx->lock, flags);
512 return rc;
Ian Munsief204e0b2014-10-08 19:55:02 +1100513}
514
Michael Neuling05203362015-05-27 16:07:17 +1000515/*
516 * Note: if this is updated, we need to update api.c to patch the new ones in
517 * too
518 */
519const struct file_operations afu_fops = {
Ian Munsief204e0b2014-10-08 19:55:02 +1100520 .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 Axtens3d6b0402015-08-07 13:18:18 +1000530static const struct file_operations afu_master_fops = {
Ian Munsief204e0b2014-10-08 19:55:02 +1100531 .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
542static char *cxl_devnode(struct device *dev, umode_t *mode)
543{
Christophe Lombard594ff7d2016-03-04 12:26:38 +0100544 if (cpu_has_feature(CPU_FTR_HVMODE) &&
545 CXL_DEVT_IS_CARD(dev->devt)) {
Ian Munsief204e0b2014-10-08 19:55:02 +1100546 /*
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
555extern struct class *cxl_class;
556
557static 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;
581err:
582 cdev_del(cdev);
583 return rc;
584}
585
586int 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
593int 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
600int 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
607void 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
626int cxl_register_afu(struct cxl_afu *afu)
627{
628 afu->dev.class = cxl_class;
629
630 return device_register(&afu->dev);
631}
632
633int 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 Lombard594ff7d2016-03-04 12:26:38 +0100646dev_t cxl_get_dev(void)
647{
648 return cxl_dev;
649}
650
Ian Munsief204e0b2014-10-08 19:55:02 +1100651int __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 Bergheaudb8102532016-06-23 15:03:53 +0200659 BUILD_BUG_ON(CXL_API_VERSION != 3);
Ian Munsief204e0b2014-10-08 19:55:02 +1100660 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
683err:
684 unregister_chrdev_region(cxl_dev, CXL_NUM_MINORS);
685 return rc;
686}
687
688void cxl_file_exit(void)
689{
690 unregister_chrdev_region(cxl_dev, CXL_NUM_MINORS);
691 class_destroy(cxl_class);
692}