1*92e87950SXie Yongji /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
2*92e87950SXie Yongji #ifndef _VDUSE_H_
3*92e87950SXie Yongji #define _VDUSE_H_
4*92e87950SXie Yongji 
5*92e87950SXie Yongji #include <linux/types.h>
6*92e87950SXie Yongji 
7*92e87950SXie Yongji #define VDUSE_BASE	0x81
8*92e87950SXie Yongji 
9*92e87950SXie Yongji /* The ioctls for control device (/dev/vduse/control) */
10*92e87950SXie Yongji 
11*92e87950SXie Yongji #define VDUSE_API_VERSION	0
12*92e87950SXie Yongji 
13*92e87950SXie Yongji /*
14*92e87950SXie Yongji  * Get the version of VDUSE API that kernel supported (VDUSE_API_VERSION).
15*92e87950SXie Yongji  * This is used for future extension.
16*92e87950SXie Yongji  */
17*92e87950SXie Yongji #define VDUSE_GET_API_VERSION	_IOR(VDUSE_BASE, 0x00, __u64)
18*92e87950SXie Yongji 
19*92e87950SXie Yongji /* Set the version of VDUSE API that userspace supported. */
20*92e87950SXie Yongji #define VDUSE_SET_API_VERSION	_IOW(VDUSE_BASE, 0x01, __u64)
21*92e87950SXie Yongji 
22*92e87950SXie Yongji /**
23*92e87950SXie Yongji  * struct vduse_dev_config - basic configuration of a VDUSE device
24*92e87950SXie Yongji  * @name: VDUSE device name, needs to be NUL terminated
25*92e87950SXie Yongji  * @vendor_id: virtio vendor id
26*92e87950SXie Yongji  * @device_id: virtio device id
27*92e87950SXie Yongji  * @features: virtio features
28*92e87950SXie Yongji  * @vq_num: the number of virtqueues
29*92e87950SXie Yongji  * @vq_align: the allocation alignment of virtqueue's metadata
30*92e87950SXie Yongji  * @reserved: for future use, needs to be initialized to zero
31*92e87950SXie Yongji  * @config_size: the size of the configuration space
32*92e87950SXie Yongji  * @config: the buffer of the configuration space
33*92e87950SXie Yongji  *
34*92e87950SXie Yongji  * Structure used by VDUSE_CREATE_DEV ioctl to create VDUSE device.
35*92e87950SXie Yongji  */
36*92e87950SXie Yongji struct vduse_dev_config {
37*92e87950SXie Yongji #define VDUSE_NAME_MAX	256
38*92e87950SXie Yongji 	char name[VDUSE_NAME_MAX];
39*92e87950SXie Yongji 	__u32 vendor_id;
40*92e87950SXie Yongji 	__u32 device_id;
41*92e87950SXie Yongji 	__u64 features;
42*92e87950SXie Yongji 	__u32 vq_num;
43*92e87950SXie Yongji 	__u32 vq_align;
44*92e87950SXie Yongji 	__u32 reserved[13];
45*92e87950SXie Yongji 	__u32 config_size;
46*92e87950SXie Yongji 	__u8 config[];
47*92e87950SXie Yongji };
48*92e87950SXie Yongji 
49*92e87950SXie Yongji /* Create a VDUSE device which is represented by a char device (/dev/vduse/$NAME) */
50*92e87950SXie Yongji #define VDUSE_CREATE_DEV	_IOW(VDUSE_BASE, 0x02, struct vduse_dev_config)
51*92e87950SXie Yongji 
52*92e87950SXie Yongji /*
53*92e87950SXie Yongji  * Destroy a VDUSE device. Make sure there are no more references
54*92e87950SXie Yongji  * to the char device (/dev/vduse/$NAME).
55*92e87950SXie Yongji  */
56*92e87950SXie Yongji #define VDUSE_DESTROY_DEV	_IOW(VDUSE_BASE, 0x03, char[VDUSE_NAME_MAX])
57*92e87950SXie Yongji 
58*92e87950SXie Yongji /* The ioctls for VDUSE device (/dev/vduse/$NAME) */
59*92e87950SXie Yongji 
60*92e87950SXie Yongji /**
61*92e87950SXie Yongji  * struct vduse_iotlb_entry - entry of IOTLB to describe one IOVA region [start, last]
62*92e87950SXie Yongji  * @offset: the mmap offset on returned file descriptor
63*92e87950SXie Yongji  * @start: start of the IOVA region
64*92e87950SXie Yongji  * @last: last of the IOVA region
65*92e87950SXie Yongji  * @perm: access permission of the IOVA region
66*92e87950SXie Yongji  *
67*92e87950SXie Yongji  * Structure used by VDUSE_IOTLB_GET_FD ioctl to find an overlapped IOVA region.
68*92e87950SXie Yongji  */
69*92e87950SXie Yongji struct vduse_iotlb_entry {
70*92e87950SXie Yongji 	__u64 offset;
71*92e87950SXie Yongji 	__u64 start;
72*92e87950SXie Yongji 	__u64 last;
73*92e87950SXie Yongji #define VDUSE_ACCESS_RO 0x1
74*92e87950SXie Yongji #define VDUSE_ACCESS_WO 0x2
75*92e87950SXie Yongji #define VDUSE_ACCESS_RW 0x3
76*92e87950SXie Yongji 	__u8 perm;
77*92e87950SXie Yongji };
78*92e87950SXie Yongji 
79*92e87950SXie Yongji /*
80*92e87950SXie Yongji  * Find the first IOVA region that overlaps with the range [start, last]
81*92e87950SXie Yongji  * and return the corresponding file descriptor. Return -EINVAL means the
82*92e87950SXie Yongji  * IOVA region doesn't exist. Caller should set start and last fields.
83*92e87950SXie Yongji  */
84*92e87950SXie Yongji #define VDUSE_IOTLB_GET_FD	_IOWR(VDUSE_BASE, 0x10, struct vduse_iotlb_entry)
85*92e87950SXie Yongji 
86*92e87950SXie Yongji /*
87*92e87950SXie Yongji  * Get the negotiated virtio features. It's a subset of the features in
88*92e87950SXie Yongji  * struct vduse_dev_config which can be accepted by virtio driver. It's
89*92e87950SXie Yongji  * only valid after FEATURES_OK status bit is set.
90*92e87950SXie Yongji  */
91*92e87950SXie Yongji #define VDUSE_DEV_GET_FEATURES	_IOR(VDUSE_BASE, 0x11, __u64)
92*92e87950SXie Yongji 
93*92e87950SXie Yongji /**
94*92e87950SXie Yongji  * struct vduse_config_data - data used to update configuration space
95*92e87950SXie Yongji  * @offset: the offset from the beginning of configuration space
96*92e87950SXie Yongji  * @length: the length to write to configuration space
97*92e87950SXie Yongji  * @buffer: the buffer used to write from
98*92e87950SXie Yongji  *
99*92e87950SXie Yongji  * Structure used by VDUSE_DEV_SET_CONFIG ioctl to update device
100*92e87950SXie Yongji  * configuration space.
101*92e87950SXie Yongji  */
102*92e87950SXie Yongji struct vduse_config_data {
103*92e87950SXie Yongji 	__u32 offset;
104*92e87950SXie Yongji 	__u32 length;
105*92e87950SXie Yongji 	__u8 buffer[];
106*92e87950SXie Yongji };
107*92e87950SXie Yongji 
108*92e87950SXie Yongji /* Set device configuration space */
109*92e87950SXie Yongji #define VDUSE_DEV_SET_CONFIG	_IOW(VDUSE_BASE, 0x12, struct vduse_config_data)
110*92e87950SXie Yongji 
111*92e87950SXie Yongji /*
112*92e87950SXie Yongji  * Inject a config interrupt. It's usually used to notify virtio driver
113*92e87950SXie Yongji  * that device configuration space has changed.
114*92e87950SXie Yongji  */
115*92e87950SXie Yongji #define VDUSE_DEV_INJECT_CONFIG_IRQ	_IO(VDUSE_BASE, 0x13)
116*92e87950SXie Yongji 
117*92e87950SXie Yongji /**
118*92e87950SXie Yongji  * struct vduse_vq_config - basic configuration of a virtqueue
119*92e87950SXie Yongji  * @index: virtqueue index
120*92e87950SXie Yongji  * @max_size: the max size of virtqueue
121*92e87950SXie Yongji  * @reserved: for future use, needs to be initialized to zero
122*92e87950SXie Yongji  *
123*92e87950SXie Yongji  * Structure used by VDUSE_VQ_SETUP ioctl to setup a virtqueue.
124*92e87950SXie Yongji  */
125*92e87950SXie Yongji struct vduse_vq_config {
126*92e87950SXie Yongji 	__u32 index;
127*92e87950SXie Yongji 	__u16 max_size;
128*92e87950SXie Yongji 	__u16 reserved[13];
129*92e87950SXie Yongji };
130*92e87950SXie Yongji 
131*92e87950SXie Yongji /*
132*92e87950SXie Yongji  * Setup the specified virtqueue. Make sure all virtqueues have been
133*92e87950SXie Yongji  * configured before the device is attached to vDPA bus.
134*92e87950SXie Yongji  */
135*92e87950SXie Yongji #define VDUSE_VQ_SETUP		_IOW(VDUSE_BASE, 0x14, struct vduse_vq_config)
136*92e87950SXie Yongji 
137*92e87950SXie Yongji /**
138*92e87950SXie Yongji  * struct vduse_vq_state_split - split virtqueue state
139*92e87950SXie Yongji  * @avail_index: available index
140*92e87950SXie Yongji  */
141*92e87950SXie Yongji struct vduse_vq_state_split {
142*92e87950SXie Yongji 	__u16 avail_index;
143*92e87950SXie Yongji };
144*92e87950SXie Yongji 
145*92e87950SXie Yongji /**
146*92e87950SXie Yongji  * struct vduse_vq_state_packed - packed virtqueue state
147*92e87950SXie Yongji  * @last_avail_counter: last driver ring wrap counter observed by device
148*92e87950SXie Yongji  * @last_avail_idx: device available index
149*92e87950SXie Yongji  * @last_used_counter: device ring wrap counter
150*92e87950SXie Yongji  * @last_used_idx: used index
151*92e87950SXie Yongji  */
152*92e87950SXie Yongji struct vduse_vq_state_packed {
153*92e87950SXie Yongji 	__u16 last_avail_counter;
154*92e87950SXie Yongji 	__u16 last_avail_idx;
155*92e87950SXie Yongji 	__u16 last_used_counter;
156*92e87950SXie Yongji 	__u16 last_used_idx;
157*92e87950SXie Yongji };
158*92e87950SXie Yongji 
159*92e87950SXie Yongji /**
160*92e87950SXie Yongji  * struct vduse_vq_info - information of a virtqueue
161*92e87950SXie Yongji  * @index: virtqueue index
162*92e87950SXie Yongji  * @num: the size of virtqueue
163*92e87950SXie Yongji  * @desc_addr: address of desc area
164*92e87950SXie Yongji  * @driver_addr: address of driver area
165*92e87950SXie Yongji  * @device_addr: address of device area
166*92e87950SXie Yongji  * @split: split virtqueue state
167*92e87950SXie Yongji  * @packed: packed virtqueue state
168*92e87950SXie Yongji  * @ready: ready status of virtqueue
169*92e87950SXie Yongji  *
170*92e87950SXie Yongji  * Structure used by VDUSE_VQ_GET_INFO ioctl to get virtqueue's information.
171*92e87950SXie Yongji  */
172*92e87950SXie Yongji struct vduse_vq_info {
173*92e87950SXie Yongji 	__u32 index;
174*92e87950SXie Yongji 	__u32 num;
175*92e87950SXie Yongji 	__u64 desc_addr;
176*92e87950SXie Yongji 	__u64 driver_addr;
177*92e87950SXie Yongji 	__u64 device_addr;
178*92e87950SXie Yongji 	union {
179*92e87950SXie Yongji 		struct vduse_vq_state_split split;
180*92e87950SXie Yongji 		struct vduse_vq_state_packed packed;
181*92e87950SXie Yongji 	};
182*92e87950SXie Yongji 	__u8 ready;
183*92e87950SXie Yongji };
184*92e87950SXie Yongji 
185*92e87950SXie Yongji /* Get the specified virtqueue's information. Caller should set index field. */
186*92e87950SXie Yongji #define VDUSE_VQ_GET_INFO	_IOWR(VDUSE_BASE, 0x15, struct vduse_vq_info)
187*92e87950SXie Yongji 
188*92e87950SXie Yongji /**
189*92e87950SXie Yongji  * struct vduse_vq_eventfd - eventfd configuration for a virtqueue
190*92e87950SXie Yongji  * @index: virtqueue index
191*92e87950SXie Yongji  * @fd: eventfd, -1 means de-assigning the eventfd
192*92e87950SXie Yongji  *
193*92e87950SXie Yongji  * Structure used by VDUSE_VQ_SETUP_KICKFD ioctl to setup kick eventfd.
194*92e87950SXie Yongji  */
195*92e87950SXie Yongji struct vduse_vq_eventfd {
196*92e87950SXie Yongji 	__u32 index;
197*92e87950SXie Yongji #define VDUSE_EVENTFD_DEASSIGN -1
198*92e87950SXie Yongji 	int fd;
199*92e87950SXie Yongji };
200*92e87950SXie Yongji 
201*92e87950SXie Yongji /*
202*92e87950SXie Yongji  * Setup kick eventfd for specified virtqueue. The kick eventfd is used
203*92e87950SXie Yongji  * by VDUSE kernel module to notify userspace to consume the avail vring.
204*92e87950SXie Yongji  */
205*92e87950SXie Yongji #define VDUSE_VQ_SETUP_KICKFD	_IOW(VDUSE_BASE, 0x16, struct vduse_vq_eventfd)
206*92e87950SXie Yongji 
207*92e87950SXie Yongji /*
208*92e87950SXie Yongji  * Inject an interrupt for specific virtqueue. It's used to notify virtio driver
209*92e87950SXie Yongji  * to consume the used vring.
210*92e87950SXie Yongji  */
211*92e87950SXie Yongji #define VDUSE_VQ_INJECT_IRQ	_IOW(VDUSE_BASE, 0x17, __u32)
212*92e87950SXie Yongji 
213*92e87950SXie Yongji /* The control messages definition for read(2)/write(2) on /dev/vduse/$NAME */
214*92e87950SXie Yongji 
215*92e87950SXie Yongji /**
216*92e87950SXie Yongji  * enum vduse_req_type - request type
217*92e87950SXie Yongji  * @VDUSE_GET_VQ_STATE: get the state for specified virtqueue from userspace
218*92e87950SXie Yongji  * @VDUSE_SET_STATUS: set the device status
219*92e87950SXie Yongji  * @VDUSE_UPDATE_IOTLB: Notify userspace to update the memory mapping for
220*92e87950SXie Yongji  *                      specified IOVA range via VDUSE_IOTLB_GET_FD ioctl
221*92e87950SXie Yongji  */
222*92e87950SXie Yongji enum vduse_req_type {
223*92e87950SXie Yongji 	VDUSE_GET_VQ_STATE,
224*92e87950SXie Yongji 	VDUSE_SET_STATUS,
225*92e87950SXie Yongji 	VDUSE_UPDATE_IOTLB,
226*92e87950SXie Yongji };
227*92e87950SXie Yongji 
228*92e87950SXie Yongji /**
229*92e87950SXie Yongji  * struct vduse_vq_state - virtqueue state
230*92e87950SXie Yongji  * @index: virtqueue index
231*92e87950SXie Yongji  * @split: split virtqueue state
232*92e87950SXie Yongji  * @packed: packed virtqueue state
233*92e87950SXie Yongji  */
234*92e87950SXie Yongji struct vduse_vq_state {
235*92e87950SXie Yongji 	__u32 index;
236*92e87950SXie Yongji 	union {
237*92e87950SXie Yongji 		struct vduse_vq_state_split split;
238*92e87950SXie Yongji 		struct vduse_vq_state_packed packed;
239*92e87950SXie Yongji 	};
240*92e87950SXie Yongji };
241*92e87950SXie Yongji 
242*92e87950SXie Yongji /**
243*92e87950SXie Yongji  * struct vduse_dev_status - device status
244*92e87950SXie Yongji  * @status: device status
245*92e87950SXie Yongji  */
246*92e87950SXie Yongji struct vduse_dev_status {
247*92e87950SXie Yongji 	__u8 status;
248*92e87950SXie Yongji };
249*92e87950SXie Yongji 
250*92e87950SXie Yongji /**
251*92e87950SXie Yongji  * struct vduse_iova_range - IOVA range [start, last]
252*92e87950SXie Yongji  * @start: start of the IOVA range
253*92e87950SXie Yongji  * @last: last of the IOVA range
254*92e87950SXie Yongji  */
255*92e87950SXie Yongji struct vduse_iova_range {
256*92e87950SXie Yongji 	__u64 start;
257*92e87950SXie Yongji 	__u64 last;
258*92e87950SXie Yongji };
259*92e87950SXie Yongji 
260*92e87950SXie Yongji /**
261*92e87950SXie Yongji  * struct vduse_dev_request - control request
262*92e87950SXie Yongji  * @type: request type
263*92e87950SXie Yongji  * @request_id: request id
264*92e87950SXie Yongji  * @reserved: for future use
265*92e87950SXie Yongji  * @vq_state: virtqueue state, only index field is available
266*92e87950SXie Yongji  * @s: device status
267*92e87950SXie Yongji  * @iova: IOVA range for updating
268*92e87950SXie Yongji  * @padding: padding
269*92e87950SXie Yongji  *
270*92e87950SXie Yongji  * Structure used by read(2) on /dev/vduse/$NAME.
271*92e87950SXie Yongji  */
272*92e87950SXie Yongji struct vduse_dev_request {
273*92e87950SXie Yongji 	__u32 type;
274*92e87950SXie Yongji 	__u32 request_id;
275*92e87950SXie Yongji 	__u32 reserved[4];
276*92e87950SXie Yongji 	union {
277*92e87950SXie Yongji 		struct vduse_vq_state vq_state;
278*92e87950SXie Yongji 		struct vduse_dev_status s;
279*92e87950SXie Yongji 		struct vduse_iova_range iova;
280*92e87950SXie Yongji 		__u32 padding[32];
281*92e87950SXie Yongji 	};
282*92e87950SXie Yongji };
283*92e87950SXie Yongji 
284*92e87950SXie Yongji /**
285*92e87950SXie Yongji  * struct vduse_dev_response - response to control request
286*92e87950SXie Yongji  * @request_id: corresponding request id
287*92e87950SXie Yongji  * @result: the result of request
288*92e87950SXie Yongji  * @reserved: for future use, needs to be initialized to zero
289*92e87950SXie Yongji  * @vq_state: virtqueue state
290*92e87950SXie Yongji  * @padding: padding
291*92e87950SXie Yongji  *
292*92e87950SXie Yongji  * Structure used by write(2) on /dev/vduse/$NAME.
293*92e87950SXie Yongji  */
294*92e87950SXie Yongji struct vduse_dev_response {
295*92e87950SXie Yongji 	__u32 request_id;
296*92e87950SXie Yongji #define VDUSE_REQ_RESULT_OK	0x00
297*92e87950SXie Yongji #define VDUSE_REQ_RESULT_FAILED	0x01
298*92e87950SXie Yongji 	__u32 result;
299*92e87950SXie Yongji 	__u32 reserved[4];
300*92e87950SXie Yongji 	union {
301*92e87950SXie Yongji 		struct vduse_vq_state vq_state;
302*92e87950SXie Yongji 		__u32 padding[32];
303*92e87950SXie Yongji 	};
304*92e87950SXie Yongji };
305*92e87950SXie Yongji 
306*92e87950SXie Yongji #endif /* _VDUSE_H_ */
307