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 <linux/vfio.h>
21 #include <asm/crw.h>
22 #include <asm/debug.h>
23 
24 #include "css.h"
25 #include "vfio_ccw_cp.h"
26 
27 #define VFIO_CCW_OFFSET_SHIFT   10
28 #define VFIO_CCW_OFFSET_TO_INDEX(off)	(off >> VFIO_CCW_OFFSET_SHIFT)
29 #define VFIO_CCW_INDEX_TO_OFFSET(index)	((u64)(index) << VFIO_CCW_OFFSET_SHIFT)
30 #define VFIO_CCW_OFFSET_MASK	(((u64)(1) << VFIO_CCW_OFFSET_SHIFT) - 1)
31 
32 /* capability chain handling similar to vfio-pci */
33 struct vfio_ccw_private;
34 struct vfio_ccw_region;
35 
36 struct vfio_ccw_regops {
37 	ssize_t	(*read)(struct vfio_ccw_private *private, char __user *buf,
38 			size_t count, loff_t *ppos);
39 	ssize_t	(*write)(struct vfio_ccw_private *private,
40 			 const char __user *buf, size_t count, loff_t *ppos);
41 	void	(*release)(struct vfio_ccw_private *private,
42 			   struct vfio_ccw_region *region);
43 };
44 
45 struct vfio_ccw_region {
46 	u32				type;
47 	u32				subtype;
48 	const struct vfio_ccw_regops	*ops;
49 	void				*data;
50 	size_t				size;
51 	u32				flags;
52 };
53 
54 int vfio_ccw_register_dev_region(struct vfio_ccw_private *private,
55 				 unsigned int subtype,
56 				 const struct vfio_ccw_regops *ops,
57 				 size_t size, u32 flags, void *data);
58 void vfio_ccw_unregister_dev_regions(struct vfio_ccw_private *private);
59 
60 int vfio_ccw_register_async_dev_regions(struct vfio_ccw_private *private);
61 int vfio_ccw_register_schib_dev_regions(struct vfio_ccw_private *private);
62 int vfio_ccw_register_crw_dev_regions(struct vfio_ccw_private *private);
63 
64 struct vfio_ccw_crw {
65 	struct list_head	next;
66 	struct crw		crw;
67 };
68 
69 /**
70  * struct vfio_ccw_private
71  * @vdev: Embedded VFIO device
72  * @sch: pointer to the subchannel
73  * @state: internal state of the device
74  * @completion: synchronization helper of the I/O completion
75  * @avail: available for creating a mediated device
76  * @io_region: MMIO region to input/output I/O arguments/results
77  * @io_mutex: protect against concurrent update of I/O regions
78  * @region: additional regions for other subchannel operations
79  * @cmd_region: MMIO region for asynchronous I/O commands other than START
80  * @schib_region: MMIO region for SCHIB information
81  * @crw_region: MMIO region for getting channel report words
82  * @num_regions: number of additional regions
83  * @cp: channel program for the current I/O operation
84  * @irb: irb info received from interrupt
85  * @scsw: scsw info
86  * @io_trigger: eventfd ctx for signaling userspace I/O results
87  * @crw_trigger: eventfd ctx for signaling userspace CRW information
88  * @req_trigger: eventfd ctx for signaling userspace to return device
89  * @io_work: work for deferral process of I/O handling
90  * @crw_work: work for deferral process of CRW handling
91  */
92 struct vfio_ccw_private {
93 	struct vfio_device vdev;
94 	struct subchannel	*sch;
95 	int			state;
96 	struct completion	*completion;
97 	atomic_t		avail;
98 	struct ccw_io_region	*io_region;
99 	struct mutex		io_mutex;
100 	struct vfio_ccw_region *region;
101 	struct ccw_cmd_region	*cmd_region;
102 	struct ccw_schib_region *schib_region;
103 	struct ccw_crw_region	*crw_region;
104 	int num_regions;
105 
106 	struct channel_program	cp;
107 	struct irb		irb;
108 	union scsw		scsw;
109 	struct list_head	crw;
110 
111 	struct eventfd_ctx	*io_trigger;
112 	struct eventfd_ctx	*crw_trigger;
113 	struct eventfd_ctx	*req_trigger;
114 	struct work_struct	io_work;
115 	struct work_struct	crw_work;
116 } __aligned(8);
117 
118 int vfio_ccw_sch_quiesce(struct subchannel *sch);
119 
120 extern struct mdev_driver vfio_ccw_mdev_driver;
121 
122 /*
123  * States of the device statemachine.
124  */
125 enum vfio_ccw_state {
126 	VFIO_CCW_STATE_NOT_OPER,
127 	VFIO_CCW_STATE_STANDBY,
128 	VFIO_CCW_STATE_IDLE,
129 	VFIO_CCW_STATE_CP_PROCESSING,
130 	VFIO_CCW_STATE_CP_PENDING,
131 	/* last element! */
132 	NR_VFIO_CCW_STATES
133 };
134 
135 /*
136  * Asynchronous events of the device statemachine.
137  */
138 enum vfio_ccw_event {
139 	VFIO_CCW_EVENT_NOT_OPER,
140 	VFIO_CCW_EVENT_IO_REQ,
141 	VFIO_CCW_EVENT_INTERRUPT,
142 	VFIO_CCW_EVENT_ASYNC_REQ,
143 	VFIO_CCW_EVENT_OPEN,
144 	VFIO_CCW_EVENT_CLOSE,
145 	/* last element! */
146 	NR_VFIO_CCW_EVENTS
147 };
148 
149 /*
150  * Action called through jumptable.
151  */
152 typedef void (fsm_func_t)(struct vfio_ccw_private *, enum vfio_ccw_event);
153 extern fsm_func_t *vfio_ccw_jumptable[NR_VFIO_CCW_STATES][NR_VFIO_CCW_EVENTS];
154 
155 static inline void vfio_ccw_fsm_event(struct vfio_ccw_private *private,
156 				      enum vfio_ccw_event event)
157 {
158 	trace_vfio_ccw_fsm_event(private->sch->schid, private->state, event);
159 	vfio_ccw_jumptable[private->state][event](private, event);
160 }
161 
162 extern struct workqueue_struct *vfio_ccw_work_q;
163 
164 
165 /* s390 debug feature, similar to base cio */
166 extern debug_info_t *vfio_ccw_debug_msg_id;
167 extern debug_info_t *vfio_ccw_debug_trace_id;
168 
169 #define VFIO_CCW_TRACE_EVENT(imp, txt) \
170 		debug_text_event(vfio_ccw_debug_trace_id, imp, txt)
171 
172 #define VFIO_CCW_MSG_EVENT(imp, args...) \
173 		debug_sprintf_event(vfio_ccw_debug_msg_id, imp, ##args)
174 
175 static inline void VFIO_CCW_HEX_EVENT(int level, void *data, int length)
176 {
177 	debug_event(vfio_ccw_debug_trace_id, level, data, length);
178 }
179 
180 #endif
181