1dd873966SEric Auger /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
251b24e34SJan Kiszka #ifndef _LINUX_VHOST_H
351b24e34SJan Kiszka #define _LINUX_VHOST_H
451b24e34SJan Kiszka /* Userspace interface for in-kernel virtio accelerators. */
551b24e34SJan Kiszka 
651b24e34SJan Kiszka /* vhost is used to reduce the number of system calls involved in virtio.
751b24e34SJan Kiszka  *
851b24e34SJan Kiszka  * Existing virtio net code is used in the guest without modification.
951b24e34SJan Kiszka  *
1051b24e34SJan Kiszka  * This header includes interface used by userspace hypervisor for
1151b24e34SJan Kiszka  * device configuration.
1251b24e34SJan Kiszka  */
1351b24e34SJan Kiszka 
14da054c64SPaolo Bonzini #include <linux/vhost_types.h>
1551b24e34SJan Kiszka #include <linux/types.h>
1651b24e34SJan Kiszka #include <linux/ioctl.h>
1751b24e34SJan Kiszka 
18f76b348eSCornelia Huck #define VHOST_FILE_UNBIND -1
19f76b348eSCornelia Huck 
2051b24e34SJan Kiszka /* ioctls */
2151b24e34SJan Kiszka 
2251b24e34SJan Kiszka #define VHOST_VIRTIO 0xAF
2351b24e34SJan Kiszka 
2451b24e34SJan Kiszka /* Features bitmask for forward compatibility.  Transport bits are used for
2551b24e34SJan Kiszka  * vhost specific features. */
2651b24e34SJan Kiszka #define VHOST_GET_FEATURES	_IOR(VHOST_VIRTIO, 0x00, __u64)
2751b24e34SJan Kiszka #define VHOST_SET_FEATURES	_IOW(VHOST_VIRTIO, 0x00, __u64)
2851b24e34SJan Kiszka 
2951b24e34SJan Kiszka /* Set current process as the (exclusive) owner of this file descriptor.  This
3051b24e34SJan Kiszka  * must be called before any other vhost command.  Further calls to
3151b24e34SJan Kiszka  * VHOST_OWNER_SET fail until VHOST_OWNER_RESET is called. */
3251b24e34SJan Kiszka #define VHOST_SET_OWNER _IO(VHOST_VIRTIO, 0x01)
3351b24e34SJan Kiszka /* Give up ownership, and reset the device to default values.
3451b24e34SJan Kiszka  * Allows subsequent call to VHOST_OWNER_SET to succeed. */
3560915dc4SYuanhan Liu #define VHOST_RESET_OWNER _IO(VHOST_VIRTIO, 0x02)
3651b24e34SJan Kiszka 
3751b24e34SJan Kiszka /* Set up/modify memory layout */
3851b24e34SJan Kiszka #define VHOST_SET_MEM_TABLE	_IOW(VHOST_VIRTIO, 0x03, struct vhost_memory)
3951b24e34SJan Kiszka 
4051b24e34SJan Kiszka /* Write logging setup. */
4151b24e34SJan Kiszka /* Memory writes can optionally be logged by setting bit at an offset
4251b24e34SJan Kiszka  * (calculated from the physical address) from specified log base.
4351b24e34SJan Kiszka  * The bit is set using an atomic 32 bit operation. */
4451b24e34SJan Kiszka /* Set base address for logging. */
4551b24e34SJan Kiszka #define VHOST_SET_LOG_BASE _IOW(VHOST_VIRTIO, 0x04, __u64)
4651b24e34SJan Kiszka /* Specify an eventfd file descriptor to signal on log write. */
4751b24e34SJan Kiszka #define VHOST_SET_LOG_FD _IOW(VHOST_VIRTIO, 0x07, int)
48d0bf492fSCédric Le Goater /* By default, a device gets one vhost_worker that its virtqueues share. This
49d0bf492fSCédric Le Goater  * command allows the owner of the device to create an additional vhost_worker
50d0bf492fSCédric Le Goater  * for the device. It can later be bound to 1 or more of its virtqueues using
51d0bf492fSCédric Le Goater  * the VHOST_ATTACH_VRING_WORKER command.
52d0bf492fSCédric Le Goater  *
53d0bf492fSCédric Le Goater  * This must be called after VHOST_SET_OWNER and the caller must be the owner
54d0bf492fSCédric Le Goater  * of the device. The new thread will inherit caller's cgroups and namespaces,
55d0bf492fSCédric Le Goater  * and will share the caller's memory space. The new thread will also be
56d0bf492fSCédric Le Goater  * counted against the caller's RLIMIT_NPROC value.
57d0bf492fSCédric Le Goater  *
58d0bf492fSCédric Le Goater  * The worker's ID used in other commands will be returned in
59d0bf492fSCédric Le Goater  * vhost_worker_state.
60d0bf492fSCédric Le Goater  */
61d0bf492fSCédric Le Goater #define VHOST_NEW_WORKER _IOR(VHOST_VIRTIO, 0x8, struct vhost_worker_state)
62d0bf492fSCédric Le Goater /* Free a worker created with VHOST_NEW_WORKER if it's not attached to any
63d0bf492fSCédric Le Goater  * virtqueue. If userspace is not able to call this for workers its created,
64d0bf492fSCédric Le Goater  * the kernel will free all the device's workers when the device is closed.
65d0bf492fSCédric Le Goater  */
66d0bf492fSCédric Le Goater #define VHOST_FREE_WORKER _IOW(VHOST_VIRTIO, 0x9, struct vhost_worker_state)
6751b24e34SJan Kiszka 
6851b24e34SJan Kiszka /* Ring setup. */
6951b24e34SJan Kiszka /* Set number of descriptors in ring. This parameter can not
7051b24e34SJan Kiszka  * be modified while ring is running (bound to a device). */
7151b24e34SJan Kiszka #define VHOST_SET_VRING_NUM _IOW(VHOST_VIRTIO, 0x10, struct vhost_vring_state)
7251b24e34SJan Kiszka /* Set addresses for the ring. */
7351b24e34SJan Kiszka #define VHOST_SET_VRING_ADDR _IOW(VHOST_VIRTIO, 0x11, struct vhost_vring_addr)
7451b24e34SJan Kiszka /* Base value where queue looks for available descriptors */
7551b24e34SJan Kiszka #define VHOST_SET_VRING_BASE _IOW(VHOST_VIRTIO, 0x12, struct vhost_vring_state)
7651b24e34SJan Kiszka /* Get accessor: reads index, writes value in num */
7751b24e34SJan Kiszka #define VHOST_GET_VRING_BASE _IOWR(VHOST_VIRTIO, 0x12, struct vhost_vring_state)
7851b24e34SJan Kiszka 
79332f6407SGreg Kurz /* Set the vring byte order in num. Valid values are VHOST_VRING_LITTLE_ENDIAN
80332f6407SGreg Kurz  * or VHOST_VRING_BIG_ENDIAN (other values return -EINVAL).
81332f6407SGreg Kurz  * The byte order cannot be changed while the device is active: trying to do so
82332f6407SGreg Kurz  * returns -EBUSY.
83332f6407SGreg Kurz  * This is a legacy only API that is simply ignored when VIRTIO_F_VERSION_1 is
84332f6407SGreg Kurz  * set.
85332f6407SGreg Kurz  * Not all kernel configurations support this ioctl, but all configurations that
86332f6407SGreg Kurz  * support SET also support GET.
87332f6407SGreg Kurz  */
88332f6407SGreg Kurz #define VHOST_VRING_LITTLE_ENDIAN 0
89332f6407SGreg Kurz #define VHOST_VRING_BIG_ENDIAN 1
90332f6407SGreg Kurz #define VHOST_SET_VRING_ENDIAN _IOW(VHOST_VIRTIO, 0x13, struct vhost_vring_state)
91332f6407SGreg Kurz #define VHOST_GET_VRING_ENDIAN _IOW(VHOST_VIRTIO, 0x14, struct vhost_vring_state)
92d0bf492fSCédric Le Goater /* Attach a vhost_worker created with VHOST_NEW_WORKER to one of the device's
93d0bf492fSCédric Le Goater  * virtqueues.
94d0bf492fSCédric Le Goater  *
95d0bf492fSCédric Le Goater  * This will replace the virtqueue's existing worker. If the replaced worker
96d0bf492fSCédric Le Goater  * is no longer attached to any virtqueues, it can be freed with
97d0bf492fSCédric Le Goater  * VHOST_FREE_WORKER.
98d0bf492fSCédric Le Goater  */
99d0bf492fSCédric Le Goater #define VHOST_ATTACH_VRING_WORKER _IOW(VHOST_VIRTIO, 0x15,		\
100d0bf492fSCédric Le Goater 				       struct vhost_vring_worker)
101d0bf492fSCédric Le Goater /* Return the vring worker's ID */
102d0bf492fSCédric Le Goater #define VHOST_GET_VRING_WORKER _IOWR(VHOST_VIRTIO, 0x16,		\
103d0bf492fSCédric Le Goater 				     struct vhost_vring_worker)
104332f6407SGreg Kurz 
10551b24e34SJan Kiszka /* The following ioctls use eventfd file descriptors to signal and poll
10651b24e34SJan Kiszka  * for events. */
10751b24e34SJan Kiszka 
10851b24e34SJan Kiszka /* Set eventfd to poll for added buffers */
10951b24e34SJan Kiszka #define VHOST_SET_VRING_KICK _IOW(VHOST_VIRTIO, 0x20, struct vhost_vring_file)
11051b24e34SJan Kiszka /* Set eventfd to signal when buffers have beed used */
11151b24e34SJan Kiszka #define VHOST_SET_VRING_CALL _IOW(VHOST_VIRTIO, 0x21, struct vhost_vring_file)
11251b24e34SJan Kiszka /* Set eventfd to signal an error */
11351b24e34SJan Kiszka #define VHOST_SET_VRING_ERR _IOW(VHOST_VIRTIO, 0x22, struct vhost_vring_file)
114b89485a5SPaolo Bonzini /* Set busy loop timeout (in us) */
115b89485a5SPaolo Bonzini #define VHOST_SET_VRING_BUSYLOOP_TIMEOUT _IOW(VHOST_VIRTIO, 0x23,	\
116b89485a5SPaolo Bonzini 					 struct vhost_vring_state)
117b89485a5SPaolo Bonzini /* Get busy loop timeout (in us) */
118b89485a5SPaolo Bonzini #define VHOST_GET_VRING_BUSYLOOP_TIMEOUT _IOW(VHOST_VIRTIO, 0x24,	\
119b89485a5SPaolo Bonzini 					 struct vhost_vring_state)
12051b24e34SJan Kiszka 
121d36f7de8SCornelia Huck /* Set or get vhost backend capability */
122d36f7de8SCornelia Huck 
123d36f7de8SCornelia Huck #define VHOST_SET_BACKEND_FEATURES _IOW(VHOST_VIRTIO, 0x25, __u64)
1248f3cd250SCornelia Huck #define VHOST_GET_BACKEND_FEATURES _IOR(VHOST_VIRTIO, 0x26, __u64)
125d36f7de8SCornelia Huck 
12651b24e34SJan Kiszka /* VHOST_NET specific defines */
12751b24e34SJan Kiszka 
12851b24e34SJan Kiszka /* Attach virtio net ring to a raw socket, or tap device.
12951b24e34SJan Kiszka  * The socket must be already bound to an ethernet device, this device will be
13051b24e34SJan Kiszka  * used for transmit.  Pass fd -1 to unbind from the socket and the transmit
13151b24e34SJan Kiszka  * device.  This can be used to stop the ring (e.g. for migration). */
13251b24e34SJan Kiszka #define VHOST_NET_SET_BACKEND _IOW(VHOST_VIRTIO, 0x30, struct vhost_vring_file)
13351b24e34SJan Kiszka 
134da054c64SPaolo Bonzini /* VHOST_SCSI specific defines */
135e098b453SAlexey Kardashevskiy 
136e098b453SAlexey Kardashevskiy #define VHOST_SCSI_SET_ENDPOINT _IOW(VHOST_VIRTIO, 0x40, struct vhost_scsi_target)
137e098b453SAlexey Kardashevskiy #define VHOST_SCSI_CLEAR_ENDPOINT _IOW(VHOST_VIRTIO, 0x41, struct vhost_scsi_target)
138e098b453SAlexey Kardashevskiy /* Changing this breaks userspace. */
139e098b453SAlexey Kardashevskiy #define VHOST_SCSI_GET_ABI_VERSION _IOW(VHOST_VIRTIO, 0x42, int)
140e098b453SAlexey Kardashevskiy /* Set and get the events missed flag */
141e098b453SAlexey Kardashevskiy #define VHOST_SCSI_SET_EVENTS_MISSED _IOW(VHOST_VIRTIO, 0x43, __u32)
142e098b453SAlexey Kardashevskiy #define VHOST_SCSI_GET_EVENTS_MISSED _IOW(VHOST_VIRTIO, 0x44, __u32)
143e098b453SAlexey Kardashevskiy 
144dbdfea92SCornelia Huck /* VHOST_VSOCK specific defines */
145dbdfea92SCornelia Huck 
146dbdfea92SCornelia Huck #define VHOST_VSOCK_SET_GUEST_CID	_IOW(VHOST_VIRTIO, 0x60, __u64)
147dbdfea92SCornelia Huck #define VHOST_VSOCK_SET_RUNNING		_IOW(VHOST_VIRTIO, 0x61, int)
148dbdfea92SCornelia Huck 
149dc6f8d45SCornelia Huck /* VHOST_VDPA specific defines */
150dc6f8d45SCornelia Huck 
151dc6f8d45SCornelia Huck /* Get the device id. The device ids follow the same definition of
152dc6f8d45SCornelia Huck  * the device id defined in virtio-spec.
153dc6f8d45SCornelia Huck  */
154dc6f8d45SCornelia Huck #define VHOST_VDPA_GET_DEVICE_ID	_IOR(VHOST_VIRTIO, 0x70, __u32)
155dc6f8d45SCornelia Huck /* Get and set the status. The status bits follow the same definition
156dc6f8d45SCornelia Huck  * of the device status defined in virtio-spec.
157dc6f8d45SCornelia Huck  */
158dc6f8d45SCornelia Huck #define VHOST_VDPA_GET_STATUS		_IOR(VHOST_VIRTIO, 0x71, __u8)
159dc6f8d45SCornelia Huck #define VHOST_VDPA_SET_STATUS		_IOW(VHOST_VIRTIO, 0x72, __u8)
160dc6f8d45SCornelia Huck /* Get and set the device config. The device config follows the same
161dc6f8d45SCornelia Huck  * definition of the device config defined in virtio-spec.
162dc6f8d45SCornelia Huck  */
163dc6f8d45SCornelia Huck #define VHOST_VDPA_GET_CONFIG		_IOR(VHOST_VIRTIO, 0x73, \
164dc6f8d45SCornelia Huck 					     struct vhost_vdpa_config)
165dc6f8d45SCornelia Huck #define VHOST_VDPA_SET_CONFIG		_IOW(VHOST_VIRTIO, 0x74, \
166dc6f8d45SCornelia Huck 					     struct vhost_vdpa_config)
167dc6f8d45SCornelia Huck /* Enable/disable the ring. */
168dc6f8d45SCornelia Huck #define VHOST_VDPA_SET_VRING_ENABLE	_IOW(VHOST_VIRTIO, 0x75, \
169dc6f8d45SCornelia Huck 					     struct vhost_vring_state)
170dc6f8d45SCornelia Huck /* Get the max ring size. */
171dc6f8d45SCornelia Huck #define VHOST_VDPA_GET_VRING_NUM	_IOR(VHOST_VIRTIO, 0x76, __u16)
172dc6f8d45SCornelia Huck 
173f76b348eSCornelia Huck /* Set event fd for config interrupt*/
174f76b348eSCornelia Huck #define VHOST_VDPA_SET_CONFIG_CALL	_IOW(VHOST_VIRTIO, 0x77, int)
175b3c818a4SEric Farman 
176b3c818a4SEric Farman /* Get the valid iova range */
177b3c818a4SEric Farman #define VHOST_VDPA_GET_IOVA_RANGE	_IOR(VHOST_VIRTIO, 0x78, \
178b3c818a4SEric Farman 					     struct vhost_vdpa_iova_range)
179e4082063SAlex Williamson /* Get the config size */
180e4082063SAlex Williamson #define VHOST_VDPA_GET_CONFIG_SIZE	_IOR(VHOST_VIRTIO, 0x79, __u32)
181e4082063SAlex Williamson 
182e4082063SAlex Williamson /* Get the count of all virtqueues */
183e4082063SAlex Williamson #define VHOST_VDPA_GET_VQS_COUNT	_IOR(VHOST_VIRTIO, 0x80, __u32)
184e4082063SAlex Williamson 
185d525f73fSChenyi Qiang /* Get the number of virtqueue groups. */
186d525f73fSChenyi Qiang #define VHOST_VDPA_GET_GROUP_NUM	_IOR(VHOST_VIRTIO, 0x81, __u32)
187d525f73fSChenyi Qiang 
188d525f73fSChenyi Qiang /* Get the number of address spaces. */
189d525f73fSChenyi Qiang #define VHOST_VDPA_GET_AS_NUM		_IOR(VHOST_VIRTIO, 0x7A, unsigned int)
190d525f73fSChenyi Qiang 
191d525f73fSChenyi Qiang /* Get the group for a virtqueue: read index, write group in num,
192d525f73fSChenyi Qiang  * The virtqueue index is stored in the index field of
193d525f73fSChenyi Qiang  * vhost_vring_state. The group for this specific virtqueue is
194d525f73fSChenyi Qiang  * returned via num field of vhost_vring_state.
195d525f73fSChenyi Qiang  */
196d525f73fSChenyi Qiang #define VHOST_VDPA_GET_VRING_GROUP	_IOWR(VHOST_VIRTIO, 0x7B,	\
197d525f73fSChenyi Qiang 					      struct vhost_vring_state)
198d525f73fSChenyi Qiang /* Set the ASID for a virtqueue group. The group index is stored in
199d525f73fSChenyi Qiang  * the index field of vhost_vring_state, the ASID associated with this
200d525f73fSChenyi Qiang  * group is stored at num field of vhost_vring_state.
201d525f73fSChenyi Qiang  */
202d525f73fSChenyi Qiang #define VHOST_VDPA_SET_GROUP_ASID	_IOW(VHOST_VIRTIO, 0x7C, \
203d525f73fSChenyi Qiang 					     struct vhost_vring_state)
204d525f73fSChenyi Qiang 
205d525f73fSChenyi Qiang /* Suspend a device so it does not process virtqueue requests anymore
206d525f73fSChenyi Qiang  *
207d525f73fSChenyi Qiang  * After the return of ioctl the device must preserve all the necessary state
208d525f73fSChenyi Qiang  * (the virtqueue vring base plus the possible device specific states) that is
209d525f73fSChenyi Qiang  * required for restoring in the future. The device must not change its
210d525f73fSChenyi Qiang  * configuration after that point.
211d525f73fSChenyi Qiang  */
212d525f73fSChenyi Qiang #define VHOST_VDPA_SUSPEND		_IO(VHOST_VIRTIO, 0x7D)
213d525f73fSChenyi Qiang 
214c5c0fdbeSDavid 'Digit' Turner /* Resume a device so it can resume processing virtqueue requests
215c5c0fdbeSDavid 'Digit' Turner  *
216c5c0fdbeSDavid 'Digit' Turner  * After the return of this ioctl the device will have restored all the
217c5c0fdbeSDavid 'Digit' Turner  * necessary states and it is fully operational to continue processing the
218c5c0fdbeSDavid 'Digit' Turner  * virtqueue descriptors.
219c5c0fdbeSDavid 'Digit' Turner  */
220c5c0fdbeSDavid 'Digit' Turner #define VHOST_VDPA_RESUME		_IO(VHOST_VIRTIO, 0x7E)
221c5c0fdbeSDavid 'Digit' Turner 
222efb91426SDaniel Henrique Barboza /* Get the group for the descriptor table including driver & device areas
223efb91426SDaniel Henrique Barboza  * of a virtqueue: read index, write group in num.
224efb91426SDaniel Henrique Barboza  * The virtqueue index is stored in the index field of vhost_vring_state.
225efb91426SDaniel Henrique Barboza  * The group ID of the descriptor table for this specific virtqueue
226efb91426SDaniel Henrique Barboza  * is returned via num field of vhost_vring_state.
227efb91426SDaniel Henrique Barboza  */
228efb91426SDaniel Henrique Barboza #define VHOST_VDPA_GET_VRING_DESC_GROUP	_IOWR(VHOST_VIRTIO, 0x7F,	\
229efb91426SDaniel Henrique Barboza 					      struct vhost_vring_state)
230*ab0c7fb2SPaolo Bonzini 
231*ab0c7fb2SPaolo Bonzini /* Get the queue size of a specific virtqueue.
232*ab0c7fb2SPaolo Bonzini  * userspace set the vring index in vhost_vring_state.index
233*ab0c7fb2SPaolo Bonzini  * kernel set the queue size in vhost_vring_state.num
234*ab0c7fb2SPaolo Bonzini  */
235*ab0c7fb2SPaolo Bonzini #define VHOST_VDPA_GET_VRING_SIZE	_IOWR(VHOST_VIRTIO, 0x80,	\
236*ab0c7fb2SPaolo Bonzini 					      struct vhost_vring_state)
23751b24e34SJan Kiszka #endif
238