Skip to content

Commit b810253

Browse files
Philippe Bergheaudmpe
authored andcommitted
cxl: Add mechanism for delivering AFU driver specific events
This adds an afu_driver_ops structure with fetch_event() and event_delivered() callbacks. An AFU driver such as cxlflash can fill this out and associate it with a context to enable passing custom AFU specific events to userspace. This also adds a new kernel API function cxl_context_pending_events(), that the AFU driver can use to notify the cxl driver that new specific events are ready to be delivered, and wake up anyone waiting on the context wait queue. The current count of AFU driver specific events is stored in the field afu_driver_events of the context structure. The cxl driver checks the afu_driver_events count during poll, select, read, etc. calls to check if an AFU driver specific event is pending, and calls fetch_event() to obtain and deliver that event. This way, the cxl driver takes care of all the usual locking semantics around these calls and handles all the generic cxl events, so that the AFU driver only needs to worry about it's own events. fetch_event() return a struct cxl_event_afu_driver_reserved, allocated by the AFU driver, and filled in with the specific event information and size. Total event size (header + data) should not be greater than CXL_READ_MIN_SIZE (4K). Th cxl driver prepends an appropriate cxl event header, copies the event to userspace, and finally calls event_delivered() to return the status of the operation to the AFU driver. The event is identified by the context and cxl_event_afu_driver_reserved pointers. Since AFU drivers provide their own means for userspace to obtain the AFU file descriptor (i.e. cxlflash uses an ioctl on their scsi file descriptor to obtain the AFU file descriptor) and the generic cxl driver will never use this event, the ABI of the event is up to each individual AFU driver. Signed-off-by: Philippe Bergheaud <felix@linux.vnet.ibm.com> Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
1 parent 6e8a927 commit b810253

6 files changed

Lines changed: 149 additions & 9 deletions

File tree

‎drivers/misc/cxl/Kconfig‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,17 @@ config CXL_EEH
1515
bool
1616
default n
1717

18+
config CXL_AFU_DRIVER_OPS
19+
bool
20+
default n
21+
1822
config CXL
1923
tristate "Support for IBM Coherent Accelerators (CXL)"
2024
depends on PPC_POWERNV && PCI_MSI && EEH
2125
select CXL_BASE
2226
select CXL_KERNEL_API
2327
select CXL_EEH
28+
select CXL_AFU_DRIVER_OPS
2429
default m
2530
help
2631
Select this option to enable driver support for IBM Coherent

‎drivers/misc/cxl/api.c‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,23 @@ struct cxl_context *cxl_fops_get_context(struct file *file)
333333
}
334334
EXPORT_SYMBOL_GPL(cxl_fops_get_context);
335335

336+
void cxl_set_driver_ops(struct cxl_context *ctx,
337+
struct cxl_afu_driver_ops *ops)
338+
{
339+
WARN_ON(!ops->fetch_event || !ops->event_delivered);
340+
atomic_set(&ctx->afu_driver_events, 0);
341+
ctx->afu_driver_ops = ops;
342+
}
343+
EXPORT_SYMBOL_GPL(cxl_set_driver_ops);
344+
345+
void cxl_context_events_pending(struct cxl_context *ctx,
346+
unsigned int new_events)
347+
{
348+
atomic_add(new_events, &ctx->afu_driver_events);
349+
wake_up_all(&ctx->wq);
350+
}
351+
EXPORT_SYMBOL_GPL(cxl_context_events_pending);
352+
336353
int cxl_start_work(struct cxl_context *ctx,
337354
struct cxl_ioctl_start_work *work)
338355
{

‎drivers/misc/cxl/cxl.h‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <asm/reg.h>
2525
#include <misc/cxl-base.h>
2626

27+
#include <misc/cxl.h>
2728
#include <uapi/misc/cxl.h>
2829

2930
extern uint cxl_verbose;
@@ -34,7 +35,7 @@ extern uint cxl_verbose;
3435
* Bump version each time a user API change is made, whether it is
3536
* backwards compatible ot not.
3637
*/
37-
#define CXL_API_VERSION 2
38+
#define CXL_API_VERSION 3
3839
#define CXL_API_VERSION_COMPATIBLE 1
3940

4041
/*
@@ -528,6 +529,10 @@ struct cxl_context {
528529
bool pending_fault;
529530
bool pending_afu_err;
530531

532+
/* Used by AFU drivers for driver specific event delivery */
533+
struct cxl_afu_driver_ops *afu_driver_ops;
534+
atomic_t afu_driver_events;
535+
531536
struct rcu_head rcu;
532537
};
533538

‎drivers/misc/cxl/file.c‎

Lines changed: 56 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,17 @@ int afu_mmap(struct file *file, struct vm_area_struct *vm)
293293
return cxl_context_iomap(ctx, vm);
294294
}
295295

296+
static inline bool ctx_event_pending(struct cxl_context *ctx)
297+
{
298+
if (ctx->pending_irq || ctx->pending_fault || ctx->pending_afu_err)
299+
return true;
300+
301+
if (ctx->afu_driver_ops && atomic_read(&ctx->afu_driver_events))
302+
return true;
303+
304+
return false;
305+
}
306+
296307
unsigned int afu_poll(struct file *file, struct poll_table_struct *poll)
297308
{
298309
struct cxl_context *ctx = file->private_data;
@@ -305,8 +316,7 @@ unsigned int afu_poll(struct file *file, struct poll_table_struct *poll)
305316
pr_devel("afu_poll wait done pe: %i\n", ctx->pe);
306317

307318
spin_lock_irqsave(&ctx->lock, flags);
308-
if (ctx->pending_irq || ctx->pending_fault ||
309-
ctx->pending_afu_err)
319+
if (ctx_event_pending(ctx))
310320
mask |= POLLIN | POLLRDNORM;
311321
else if (ctx->status == CLOSED)
312322
/* Only error on closed when there are no futher events pending
@@ -319,16 +329,46 @@ unsigned int afu_poll(struct file *file, struct poll_table_struct *poll)
319329
return mask;
320330
}
321331

322-
static inline int ctx_event_pending(struct cxl_context *ctx)
332+
static ssize_t afu_driver_event_copy(struct cxl_context *ctx,
333+
char __user *buf,
334+
struct cxl_event *event,
335+
struct cxl_event_afu_driver_reserved *pl)
323336
{
324-
return (ctx->pending_irq || ctx->pending_fault ||
325-
ctx->pending_afu_err || (ctx->status == CLOSED));
337+
/* Check event */
338+
if (!pl) {
339+
ctx->afu_driver_ops->event_delivered(ctx, pl, -EINVAL);
340+
return -EFAULT;
341+
}
342+
343+
/* Check event size */
344+
event->header.size += pl->data_size;
345+
if (event->header.size > CXL_READ_MIN_SIZE) {
346+
ctx->afu_driver_ops->event_delivered(ctx, pl, -EINVAL);
347+
return -EFAULT;
348+
}
349+
350+
/* Copy event header */
351+
if (copy_to_user(buf, event, sizeof(struct cxl_event_header))) {
352+
ctx->afu_driver_ops->event_delivered(ctx, pl, -EFAULT);
353+
return -EFAULT;
354+
}
355+
356+
/* Copy event data */
357+
buf += sizeof(struct cxl_event_header);
358+
if (copy_to_user(buf, &pl->data, pl->data_size)) {
359+
ctx->afu_driver_ops->event_delivered(ctx, pl, -EFAULT);
360+
return -EFAULT;
361+
}
362+
363+
ctx->afu_driver_ops->event_delivered(ctx, pl, 0); /* Success */
364+
return event->header.size;
326365
}
327366

328367
ssize_t afu_read(struct file *file, char __user *buf, size_t count,
329368
loff_t *off)
330369
{
331370
struct cxl_context *ctx = file->private_data;
371+
struct cxl_event_afu_driver_reserved *pl = NULL;
332372
struct cxl_event event;
333373
unsigned long flags;
334374
int rc;
@@ -344,7 +384,7 @@ ssize_t afu_read(struct file *file, char __user *buf, size_t count,
344384

345385
for (;;) {
346386
prepare_to_wait(&ctx->wq, &wait, TASK_INTERRUPTIBLE);
347-
if (ctx_event_pending(ctx))
387+
if (ctx_event_pending(ctx) || (ctx->status == CLOSED))
348388
break;
349389

350390
if (!cxl_ops->link_ok(ctx->afu->adapter, ctx->afu)) {
@@ -374,7 +414,12 @@ ssize_t afu_read(struct file *file, char __user *buf, size_t count,
374414
memset(&event, 0, sizeof(event));
375415
event.header.process_element = ctx->pe;
376416
event.header.size = sizeof(struct cxl_event_header);
377-
if (ctx->pending_irq) {
417+
if (ctx->afu_driver_ops && atomic_read(&ctx->afu_driver_events)) {
418+
pr_devel("afu_read delivering AFU driver specific event\n");
419+
pl = ctx->afu_driver_ops->fetch_event(ctx);
420+
atomic_dec(&ctx->afu_driver_events);
421+
event.header.type = CXL_EVENT_AFU_DRIVER;
422+
} else if (ctx->pending_irq) {
378423
pr_devel("afu_read delivering AFU interrupt\n");
379424
event.header.size += sizeof(struct cxl_event_afu_interrupt);
380425
event.header.type = CXL_EVENT_AFU_INTERRUPT;
@@ -404,6 +449,9 @@ ssize_t afu_read(struct file *file, char __user *buf, size_t count,
404449

405450
spin_unlock_irqrestore(&ctx->lock, flags);
406451

452+
if (event.header.type == CXL_EVENT_AFU_DRIVER)
453+
return afu_driver_event_copy(ctx, buf, &event, pl);
454+
407455
if (copy_to_user(buf, &event, event.header.size))
408456
return -EFAULT;
409457
return event.header.size;
@@ -558,7 +606,7 @@ int __init cxl_file_init(void)
558606
* If these change we really need to update API. Either change some
559607
* flags or update API version number CXL_API_VERSION.
560608
*/
561-
BUILD_BUG_ON(CXL_API_VERSION != 2);
609+
BUILD_BUG_ON(CXL_API_VERSION != 3);
562610
BUILD_BUG_ON(sizeof(struct cxl_ioctl_start_work) != 64);
563611
BUILD_BUG_ON(sizeof(struct cxl_event_header) != 8);
564612
BUILD_BUG_ON(sizeof(struct cxl_event_afu_interrupt) != 8);

‎include/misc/cxl.h‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,4 +220,52 @@ void cxl_perst_reloads_same_image(struct cxl_afu *afu,
220220
*/
221221
ssize_t cxl_read_adapter_vpd(struct pci_dev *dev, void *buf, size_t count);
222222

223+
/*
224+
* AFU driver ops allow an AFU driver to create their own events to pass to
225+
* userspace through the file descriptor as a simpler alternative to overriding
226+
* the read() and poll() calls that works with the generic cxl events. These
227+
* events are given priority over the generic cxl events, so they will be
228+
* delivered first if multiple types of events are pending.
229+
*
230+
* The AFU driver must call cxl_context_events_pending() to notify the cxl
231+
* driver that new events are ready to be delivered for a specific context.
232+
* cxl_context_events_pending() will adjust the current count of AFU driver
233+
* events for this context, and wake up anyone waiting on the context wait
234+
* queue.
235+
*
236+
* The cxl driver will then call fetch_event() to get a structure defining
237+
* the size and address of the driver specific event data. The cxl driver
238+
* will build a cxl header with type and process_element fields filled in,
239+
* and header.size set to sizeof(struct cxl_event_header) + data_size.
240+
* The total size of the event is limited to CXL_READ_MIN_SIZE (4K).
241+
*
242+
* fetch_event() is called with a spin lock held, so it must not sleep.
243+
*
244+
* The cxl driver will then deliver the event to userspace, and finally
245+
* call event_delivered() to return the status of the operation, identified
246+
* by cxl context and AFU driver event data pointers.
247+
* 0 Success
248+
* -EFAULT copy_to_user() has failed
249+
* -EINVAL Event data pointer is NULL, or event size is greater than
250+
* CXL_READ_MIN_SIZE.
251+
*/
252+
struct cxl_afu_driver_ops {
253+
struct cxl_event_afu_driver_reserved *(*fetch_event) (
254+
struct cxl_context *ctx);
255+
void (*event_delivered) (struct cxl_context *ctx,
256+
struct cxl_event_afu_driver_reserved *event,
257+
int rc);
258+
};
259+
260+
/*
261+
* Associate the above driver ops with a specific context.
262+
* Reset the current count of AFU driver events.
263+
*/
264+
void cxl_set_driver_ops(struct cxl_context *ctx,
265+
struct cxl_afu_driver_ops *ops);
266+
267+
/* Notify cxl driver that new events are ready to be delivered for context */
268+
void cxl_context_events_pending(struct cxl_context *ctx,
269+
unsigned int new_events);
270+
223271
#endif /* _MISC_CXL_H */

‎include/uapi/misc/cxl.h‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ enum cxl_event_type {
9393
CXL_EVENT_AFU_INTERRUPT = 1,
9494
CXL_EVENT_DATA_STORAGE = 2,
9595
CXL_EVENT_AFU_ERROR = 3,
96+
CXL_EVENT_AFU_DRIVER = 4,
9697
};
9798

9899
struct cxl_event_header {
@@ -124,12 +125,28 @@ struct cxl_event_afu_error {
124125
__u64 error;
125126
};
126127

128+
struct cxl_event_afu_driver_reserved {
129+
/*
130+
* Defines the buffer passed to the cxl driver by the AFU driver.
131+
*
132+
* This is not ABI since the event header.size passed to the user for
133+
* existing events is set in the read call to sizeof(cxl_event_header)
134+
* + sizeof(whatever event is being dispatched) and the user is already
135+
* required to use a 4K buffer on the read call.
136+
*
137+
* Of course the contents will be ABI, but that's up the AFU driver.
138+
*/
139+
size_t data_size;
140+
u8 data[];
141+
};
142+
127143
struct cxl_event {
128144
struct cxl_event_header header;
129145
union {
130146
struct cxl_event_afu_interrupt irq;
131147
struct cxl_event_data_storage fault;
132148
struct cxl_event_afu_error afu_error;
149+
struct cxl_event_afu_driver_reserved afu_driver_event;
133150
};
134151
};
135152

0 commit comments

Comments
 (0)