1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Private stuff for vfio_ccw driver
5 * Copyright IBM Corp. 2017
6 * Copyright Red Hat, Inc. 2019
8 * Author(s): Dong Jia Shi <bjsdjshi@linux.vnet.ibm.com>
9 * Xiao Feng Ren <renxiaof@linux.vnet.ibm.com>
10 * Cornelia Huck <cohuck@redhat.com>
13 #ifndef _VFIO_CCW_PRIVATE_H_
14 #define _VFIO_CCW_PRIVATE_H_
16 #include <linux/completion.h>
17 #include <linux/eventfd.h>
18 #include <linux/workqueue.h>
19 #include <linux/vfio_ccw.h>
20 #include <asm/debug.h>
23 #include "vfio_ccw_cp.h"
25 #define VFIO_CCW_OFFSET_SHIFT 10
26 #define VFIO_CCW_OFFSET_TO_INDEX(off) (off >> VFIO_CCW_OFFSET_SHIFT)
27 #define VFIO_CCW_INDEX_TO_OFFSET(index) ((u64)(index) << VFIO_CCW_OFFSET_SHIFT)
28 #define VFIO_CCW_OFFSET_MASK (((u64)(1) << VFIO_CCW_OFFSET_SHIFT) - 1)
30 /* capability chain handling similar to vfio-pci */
31 struct vfio_ccw_private;
32 struct vfio_ccw_region;
34 struct vfio_ccw_regops {
35 ssize_t (*read)(struct vfio_ccw_private *private, char __user *buf,
36 size_t count, loff_t *ppos);
37 ssize_t (*write)(struct vfio_ccw_private *private,
38 const char __user *buf, size_t count, loff_t *ppos);
39 void (*release)(struct vfio_ccw_private *private,
40 struct vfio_ccw_region *region);
43 struct vfio_ccw_region {
46 const struct vfio_ccw_regops *ops;
52 int vfio_ccw_register_dev_region(struct vfio_ccw_private *private,
54 const struct vfio_ccw_regops *ops,
55 size_t size, u32 flags, void *data);
57 int vfio_ccw_register_async_dev_regions(struct vfio_ccw_private *private);
60 * struct vfio_ccw_private
61 * @sch: pointer to the subchannel
62 * @state: internal state of the device
63 * @completion: synchronization helper of the I/O completion
64 * @avail: available for creating a mediated device
65 * @mdev: pointer to the mediated device
66 * @nb: notifier for vfio events
67 * @io_region: MMIO region to input/output I/O arguments/results
68 * @io_mutex: protect against concurrent update of I/O regions
69 * @region: additional regions for other subchannel operations
70 * @cmd_region: MMIO region for asynchronous I/O commands other than START
71 * @num_regions: number of additional regions
72 * @cp: channel program for the current I/O operation
73 * @irb: irb info received from interrupt
75 * @io_trigger: eventfd ctx for signaling userspace I/O results
76 * @io_work: work for deferral process of I/O handling
78 struct vfio_ccw_private {
79 struct subchannel *sch;
81 struct completion *completion;
83 struct mdev_device *mdev;
84 struct notifier_block nb;
85 struct ccw_io_region *io_region;
86 struct mutex io_mutex;
87 struct vfio_ccw_region *region;
88 struct ccw_cmd_region *cmd_region;
91 struct channel_program cp;
95 struct eventfd_ctx *io_trigger;
96 struct work_struct io_work;
99 extern int vfio_ccw_mdev_reg(struct subchannel *sch);
100 extern void vfio_ccw_mdev_unreg(struct subchannel *sch);
102 extern int vfio_ccw_sch_quiesce(struct subchannel *sch);
105 * States of the device statemachine.
107 enum vfio_ccw_state {
108 VFIO_CCW_STATE_NOT_OPER,
109 VFIO_CCW_STATE_STANDBY,
111 VFIO_CCW_STATE_CP_PROCESSING,
112 VFIO_CCW_STATE_CP_PENDING,
118 * Asynchronous events of the device statemachine.
120 enum vfio_ccw_event {
121 VFIO_CCW_EVENT_NOT_OPER,
122 VFIO_CCW_EVENT_IO_REQ,
123 VFIO_CCW_EVENT_INTERRUPT,
124 VFIO_CCW_EVENT_ASYNC_REQ,
130 * Action called through jumptable.
132 typedef void (fsm_func_t)(struct vfio_ccw_private *, enum vfio_ccw_event);
133 extern fsm_func_t *vfio_ccw_jumptable[NR_VFIO_CCW_STATES][NR_VFIO_CCW_EVENTS];
135 static inline void vfio_ccw_fsm_event(struct vfio_ccw_private *private,
138 trace_vfio_ccw_fsm_event(private->sch->schid, private->state, event);
139 vfio_ccw_jumptable[private->state][event](private, event);
142 extern struct workqueue_struct *vfio_ccw_work_q;
145 /* s390 debug feature, similar to base cio */
146 extern debug_info_t *vfio_ccw_debug_msg_id;
147 extern debug_info_t *vfio_ccw_debug_trace_id;
149 #define VFIO_CCW_TRACE_EVENT(imp, txt) \
150 debug_text_event(vfio_ccw_debug_trace_id, imp, txt)
152 #define VFIO_CCW_MSG_EVENT(imp, args...) \
153 debug_sprintf_event(vfio_ccw_debug_msg_id, imp, ##args)
155 static inline void VFIO_CCW_HEX_EVENT(int level, void *data, int length)
157 debug_event(vfio_ccw_debug_trace_id, level, data, length);