xref: /openbmc/qemu/linux-headers/linux/vhost.h (revision b89485a5)
151b24e34SJan Kiszka #ifndef _LINUX_VHOST_H
251b24e34SJan Kiszka #define _LINUX_VHOST_H
351b24e34SJan Kiszka /* Userspace interface for in-kernel virtio accelerators. */
451b24e34SJan Kiszka 
551b24e34SJan Kiszka /* vhost is used to reduce the number of system calls involved in virtio.
651b24e34SJan Kiszka  *
751b24e34SJan Kiszka  * Existing virtio net code is used in the guest without modification.
851b24e34SJan Kiszka  *
951b24e34SJan Kiszka  * This header includes interface used by userspace hypervisor for
1051b24e34SJan Kiszka  * device configuration.
1151b24e34SJan Kiszka  */
1251b24e34SJan Kiszka 
1351b24e34SJan Kiszka #include <linux/types.h>
1451b24e34SJan Kiszka 
1551b24e34SJan Kiszka #include <linux/ioctl.h>
1651b24e34SJan Kiszka #include <linux/virtio_config.h>
17a9fd1654SJens Freimann #include <linux/virtio_ring.h>
1851b24e34SJan Kiszka 
1951b24e34SJan Kiszka struct vhost_vring_state {
2051b24e34SJan Kiszka 	unsigned int index;
2151b24e34SJan Kiszka 	unsigned int num;
2251b24e34SJan Kiszka };
2351b24e34SJan Kiszka 
2451b24e34SJan Kiszka struct vhost_vring_file {
2551b24e34SJan Kiszka 	unsigned int index;
2651b24e34SJan Kiszka 	int fd; /* Pass -1 to unbind from file. */
2751b24e34SJan Kiszka 
2851b24e34SJan Kiszka };
2951b24e34SJan Kiszka 
3051b24e34SJan Kiszka struct vhost_vring_addr {
3151b24e34SJan Kiszka 	unsigned int index;
3251b24e34SJan Kiszka 	/* Option flags. */
3351b24e34SJan Kiszka 	unsigned int flags;
3451b24e34SJan Kiszka 	/* Flag values: */
3551b24e34SJan Kiszka 	/* Whether log address is valid. If set enables logging. */
3651b24e34SJan Kiszka #define VHOST_VRING_F_LOG 0
3751b24e34SJan Kiszka 
3851b24e34SJan Kiszka 	/* Start of array of descriptors (virtually contiguous) */
3951b24e34SJan Kiszka 	__u64 desc_user_addr;
4051b24e34SJan Kiszka 	/* Used structure address. Must be 32 bit aligned */
4151b24e34SJan Kiszka 	__u64 used_user_addr;
4251b24e34SJan Kiszka 	/* Available structure address. Must be 16 bit aligned */
4351b24e34SJan Kiszka 	__u64 avail_user_addr;
4451b24e34SJan Kiszka 	/* Logging support. */
4551b24e34SJan Kiszka 	/* Log writes to used structure, at offset calculated from specified
4651b24e34SJan Kiszka 	 * address. Address must be 32 bit aligned. */
4751b24e34SJan Kiszka 	__u64 log_guest_addr;
4851b24e34SJan Kiszka };
4951b24e34SJan Kiszka 
5051b24e34SJan Kiszka struct vhost_memory_region {
5151b24e34SJan Kiszka 	__u64 guest_phys_addr;
5251b24e34SJan Kiszka 	__u64 memory_size; /* bytes */
5351b24e34SJan Kiszka 	__u64 userspace_addr;
5451b24e34SJan Kiszka 	__u64 flags_padding; /* No flags are currently specified. */
5551b24e34SJan Kiszka };
5651b24e34SJan Kiszka 
5751b24e34SJan Kiszka /* All region addresses and sizes must be 4K aligned. */
5851b24e34SJan Kiszka #define VHOST_PAGE_SIZE 0x1000
5951b24e34SJan Kiszka 
6051b24e34SJan Kiszka struct vhost_memory {
6151b24e34SJan Kiszka 	__u32 nregions;
6251b24e34SJan Kiszka 	__u32 padding;
6351b24e34SJan Kiszka 	struct vhost_memory_region regions[0];
6451b24e34SJan Kiszka };
6551b24e34SJan Kiszka 
6651b24e34SJan Kiszka /* ioctls */
6751b24e34SJan Kiszka 
6851b24e34SJan Kiszka #define VHOST_VIRTIO 0xAF
6951b24e34SJan Kiszka 
7051b24e34SJan Kiszka /* Features bitmask for forward compatibility.  Transport bits are used for
7151b24e34SJan Kiszka  * vhost specific features. */
7251b24e34SJan Kiszka #define VHOST_GET_FEATURES	_IOR(VHOST_VIRTIO, 0x00, __u64)
7351b24e34SJan Kiszka #define VHOST_SET_FEATURES	_IOW(VHOST_VIRTIO, 0x00, __u64)
7451b24e34SJan Kiszka 
7551b24e34SJan Kiszka /* Set current process as the (exclusive) owner of this file descriptor.  This
7651b24e34SJan Kiszka  * must be called before any other vhost command.  Further calls to
7751b24e34SJan Kiszka  * VHOST_OWNER_SET fail until VHOST_OWNER_RESET is called. */
7851b24e34SJan Kiszka #define VHOST_SET_OWNER _IO(VHOST_VIRTIO, 0x01)
7951b24e34SJan Kiszka /* Give up ownership, and reset the device to default values.
8051b24e34SJan Kiszka  * Allows subsequent call to VHOST_OWNER_SET to succeed. */
8160915dc4SYuanhan Liu #define VHOST_RESET_OWNER _IO(VHOST_VIRTIO, 0x02)
8251b24e34SJan Kiszka 
8351b24e34SJan Kiszka /* Set up/modify memory layout */
8451b24e34SJan Kiszka #define VHOST_SET_MEM_TABLE	_IOW(VHOST_VIRTIO, 0x03, struct vhost_memory)
8551b24e34SJan Kiszka 
8651b24e34SJan Kiszka /* Write logging setup. */
8751b24e34SJan Kiszka /* Memory writes can optionally be logged by setting bit at an offset
8851b24e34SJan Kiszka  * (calculated from the physical address) from specified log base.
8951b24e34SJan Kiszka  * The bit is set using an atomic 32 bit operation. */
9051b24e34SJan Kiszka /* Set base address for logging. */
9151b24e34SJan Kiszka #define VHOST_SET_LOG_BASE _IOW(VHOST_VIRTIO, 0x04, __u64)
9251b24e34SJan Kiszka /* Specify an eventfd file descriptor to signal on log write. */
9351b24e34SJan Kiszka #define VHOST_SET_LOG_FD _IOW(VHOST_VIRTIO, 0x07, int)
9451b24e34SJan Kiszka 
9551b24e34SJan Kiszka /* Ring setup. */
9651b24e34SJan Kiszka /* Set number of descriptors in ring. This parameter can not
9751b24e34SJan Kiszka  * be modified while ring is running (bound to a device). */
9851b24e34SJan Kiszka #define VHOST_SET_VRING_NUM _IOW(VHOST_VIRTIO, 0x10, struct vhost_vring_state)
9951b24e34SJan Kiszka /* Set addresses for the ring. */
10051b24e34SJan Kiszka #define VHOST_SET_VRING_ADDR _IOW(VHOST_VIRTIO, 0x11, struct vhost_vring_addr)
10151b24e34SJan Kiszka /* Base value where queue looks for available descriptors */
10251b24e34SJan Kiszka #define VHOST_SET_VRING_BASE _IOW(VHOST_VIRTIO, 0x12, struct vhost_vring_state)
10351b24e34SJan Kiszka /* Get accessor: reads index, writes value in num */
10451b24e34SJan Kiszka #define VHOST_GET_VRING_BASE _IOWR(VHOST_VIRTIO, 0x12, struct vhost_vring_state)
10551b24e34SJan Kiszka 
106332f6407SGreg Kurz /* Set the vring byte order in num. Valid values are VHOST_VRING_LITTLE_ENDIAN
107332f6407SGreg Kurz  * or VHOST_VRING_BIG_ENDIAN (other values return -EINVAL).
108332f6407SGreg Kurz  * The byte order cannot be changed while the device is active: trying to do so
109332f6407SGreg Kurz  * returns -EBUSY.
110332f6407SGreg Kurz  * This is a legacy only API that is simply ignored when VIRTIO_F_VERSION_1 is
111332f6407SGreg Kurz  * set.
112332f6407SGreg Kurz  * Not all kernel configurations support this ioctl, but all configurations that
113332f6407SGreg Kurz  * support SET also support GET.
114332f6407SGreg Kurz  */
115332f6407SGreg Kurz #define VHOST_VRING_LITTLE_ENDIAN 0
116332f6407SGreg Kurz #define VHOST_VRING_BIG_ENDIAN 1
117332f6407SGreg Kurz #define VHOST_SET_VRING_ENDIAN _IOW(VHOST_VIRTIO, 0x13, struct vhost_vring_state)
118332f6407SGreg Kurz #define VHOST_GET_VRING_ENDIAN _IOW(VHOST_VIRTIO, 0x14, struct vhost_vring_state)
119332f6407SGreg Kurz 
12051b24e34SJan Kiszka /* The following ioctls use eventfd file descriptors to signal and poll
12151b24e34SJan Kiszka  * for events. */
12251b24e34SJan Kiszka 
12351b24e34SJan Kiszka /* Set eventfd to poll for added buffers */
12451b24e34SJan Kiszka #define VHOST_SET_VRING_KICK _IOW(VHOST_VIRTIO, 0x20, struct vhost_vring_file)
12551b24e34SJan Kiszka /* Set eventfd to signal when buffers have beed used */
12651b24e34SJan Kiszka #define VHOST_SET_VRING_CALL _IOW(VHOST_VIRTIO, 0x21, struct vhost_vring_file)
12751b24e34SJan Kiszka /* Set eventfd to signal an error */
12851b24e34SJan Kiszka #define VHOST_SET_VRING_ERR _IOW(VHOST_VIRTIO, 0x22, struct vhost_vring_file)
129*b89485a5SPaolo Bonzini /* Set busy loop timeout (in us) */
130*b89485a5SPaolo Bonzini #define VHOST_SET_VRING_BUSYLOOP_TIMEOUT _IOW(VHOST_VIRTIO, 0x23,	\
131*b89485a5SPaolo Bonzini 					 struct vhost_vring_state)
132*b89485a5SPaolo Bonzini /* Get busy loop timeout (in us) */
133*b89485a5SPaolo Bonzini #define VHOST_GET_VRING_BUSYLOOP_TIMEOUT _IOW(VHOST_VIRTIO, 0x24,	\
134*b89485a5SPaolo Bonzini 					 struct vhost_vring_state)
13551b24e34SJan Kiszka 
13651b24e34SJan Kiszka /* VHOST_NET specific defines */
13751b24e34SJan Kiszka 
13851b24e34SJan Kiszka /* Attach virtio net ring to a raw socket, or tap device.
13951b24e34SJan Kiszka  * The socket must be already bound to an ethernet device, this device will be
14051b24e34SJan Kiszka  * used for transmit.  Pass fd -1 to unbind from the socket and the transmit
14151b24e34SJan Kiszka  * device.  This can be used to stop the ring (e.g. for migration). */
14251b24e34SJan Kiszka #define VHOST_NET_SET_BACKEND _IOW(VHOST_VIRTIO, 0x30, struct vhost_vring_file)
14351b24e34SJan Kiszka 
14451b24e34SJan Kiszka /* Feature bits */
14551b24e34SJan Kiszka /* Log all write descriptors. Can be changed while device is active. */
14651b24e34SJan Kiszka #define VHOST_F_LOG_ALL 26
14751b24e34SJan Kiszka /* vhost-net should add virtio_net_hdr for RX, and strip for TX packets. */
14851b24e34SJan Kiszka #define VHOST_NET_F_VIRTIO_NET_HDR 27
14951b24e34SJan Kiszka 
150e098b453SAlexey Kardashevskiy /* VHOST_SCSI specific definitions */
151e098b453SAlexey Kardashevskiy 
152e098b453SAlexey Kardashevskiy /*
153e098b453SAlexey Kardashevskiy  * Used by QEMU userspace to ensure a consistent vhost-scsi ABI.
154e098b453SAlexey Kardashevskiy  *
155e098b453SAlexey Kardashevskiy  * ABI Rev 0: July 2012 version starting point for v3.6-rc merge candidate +
156e098b453SAlexey Kardashevskiy  *            RFC-v2 vhost-scsi userspace.  Add GET_ABI_VERSION ioctl usage
157e098b453SAlexey Kardashevskiy  * ABI Rev 1: January 2013. Ignore vhost_tpgt filed in struct vhost_scsi_target.
158e098b453SAlexey Kardashevskiy  *            All the targets under vhost_wwpn can be seen and used by guset.
159e098b453SAlexey Kardashevskiy  */
160e098b453SAlexey Kardashevskiy 
161e098b453SAlexey Kardashevskiy #define VHOST_SCSI_ABI_VERSION	1
162e098b453SAlexey Kardashevskiy 
163e098b453SAlexey Kardashevskiy struct vhost_scsi_target {
164e098b453SAlexey Kardashevskiy 	int abi_version;
165e098b453SAlexey Kardashevskiy 	char vhost_wwpn[224]; /* TRANSPORT_IQN_LEN */
166e098b453SAlexey Kardashevskiy 	unsigned short vhost_tpgt;
167e098b453SAlexey Kardashevskiy 	unsigned short reserved;
168e098b453SAlexey Kardashevskiy };
169e098b453SAlexey Kardashevskiy 
170e098b453SAlexey Kardashevskiy #define VHOST_SCSI_SET_ENDPOINT _IOW(VHOST_VIRTIO, 0x40, struct vhost_scsi_target)
171e098b453SAlexey Kardashevskiy #define VHOST_SCSI_CLEAR_ENDPOINT _IOW(VHOST_VIRTIO, 0x41, struct vhost_scsi_target)
172e098b453SAlexey Kardashevskiy /* Changing this breaks userspace. */
173e098b453SAlexey Kardashevskiy #define VHOST_SCSI_GET_ABI_VERSION _IOW(VHOST_VIRTIO, 0x42, int)
174e098b453SAlexey Kardashevskiy /* Set and get the events missed flag */
175e098b453SAlexey Kardashevskiy #define VHOST_SCSI_SET_EVENTS_MISSED _IOW(VHOST_VIRTIO, 0x43, __u32)
176e098b453SAlexey Kardashevskiy #define VHOST_SCSI_GET_EVENTS_MISSED _IOW(VHOST_VIRTIO, 0x44, __u32)
177e098b453SAlexey Kardashevskiy 
17851b24e34SJan Kiszka #endif
179