1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Private stuff for vfio_ccw driver
4  *
5  * Copyright IBM Corp. 2017
6  * Copyright Red Hat, Inc. 2019
7  *
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>
11  */
12 
13 #ifndef _VFIO_CCW_PRIVATE_H_
14 #define _VFIO_CCW_PRIVATE_H_
15 
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>
21 
22 #include "css.h"
23 #include "vfio_ccw_cp.h"
24 
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)
29 
30 /* capability chain handling similar to vfio-pci */
31 struct vfio_ccw_private;
32 struct vfio_ccw_region;
33 
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);
41 };
42 
43 struct vfio_ccw_region {
44 	u32				type;
45 	u32				subtype;
46 	const struct vfio_ccw_regops	*ops;
47 	void				*data;
48 	size_t				size;
49 	u32				flags;
50 };
51 
52 int vfio_ccw_register_dev_region(struct vfio_ccw_private *private,
53 				 unsigned int subtype,
54 				 const struct vfio_ccw_regops *ops,
55 				 size_t size, u32 flags, void *data);
56 
57 int vfio_ccw_register_async_dev_regions(struct vfio_ccw_private *private);
58 
59 /**
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
74  * @scsw: scsw info
75  * @io_trigger: eventfd ctx for signaling userspace I/O results
76  * @io_work: work for deferral process of I/O handling
77  */
78 struct vfio_ccw_private {
79 	struct subchannel	*sch;
80 	int			state;
81 	struct completion	*completion;
82 	atomic_t		avail;
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;
89 	int num_regions;
90 
91 	struct channel_program	cp;
92 	struct irb		irb;
93 	union scsw		scsw;
94 
95 	struct eventfd_ctx	*io_trigger;
96 	struct work_struct	io_work;
97 } __aligned(8);
98 
99 extern int vfio_ccw_mdev_reg(struct subchannel *sch);
100 extern void vfio_ccw_mdev_unreg(struct subchannel *sch);
101 
102 extern int vfio_ccw_sch_quiesce(struct subchannel *sch);
103 
104 /*
105  * States of the device statemachine.
106  */
107 enum vfio_ccw_state {
108 	VFIO_CCW_STATE_NOT_OPER,
109 	VFIO_CCW_STATE_STANDBY,
110 	VFIO_CCW_STATE_IDLE,
111 	VFIO_CCW_STATE_CP_PROCESSING,
112 	VFIO_CCW_STATE_CP_PENDING,
113 	/* last element! */
114 	NR_VFIO_CCW_STATES
115 };
116 
117 /*
118  * Asynchronous events of the device statemachine.
119  */
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,
125 	/* last element! */
126 	NR_VFIO_CCW_EVENTS
127 };
128 
129 /*
130  * Action called through jumptable.
131  */
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];
134 
135 static inline void vfio_ccw_fsm_event(struct vfio_ccw_private *private,
136 				     int event)
137 {
138 	trace_vfio_ccw_fsm_event(private->sch->schid, private->state, event);
139 	vfio_ccw_jumptable[private->state][event](private, event);
140 }
141 
142 extern struct workqueue_struct *vfio_ccw_work_q;
143 
144 
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;
148 
149 #define VFIO_CCW_TRACE_EVENT(imp, txt) \
150 		debug_text_event(vfio_ccw_debug_trace_id, imp, txt)
151 
152 #define VFIO_CCW_MSG_EVENT(imp, args...) \
153 		debug_sprintf_event(vfio_ccw_debug_msg_id, imp, ##args)
154 
155 static inline void VFIO_CCW_HEX_EVENT(int level, void *data, int length)
156 {
157 	debug_event(vfio_ccw_debug_trace_id, level, data, length);
158 }
159 
160 #endif
161