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>
1751b24e34SJan Kiszka #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. */
8151b24e34SJan Kiszka #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 
10651b24e34SJan Kiszka /* The following ioctls use eventfd file descriptors to signal and poll
10751b24e34SJan Kiszka  * for events. */
10851b24e34SJan Kiszka 
10951b24e34SJan Kiszka /* Set eventfd to poll for added buffers */
11051b24e34SJan Kiszka #define VHOST_SET_VRING_KICK _IOW(VHOST_VIRTIO, 0x20, struct vhost_vring_file)
11151b24e34SJan Kiszka /* Set eventfd to signal when buffers have beed used */
11251b24e34SJan Kiszka #define VHOST_SET_VRING_CALL _IOW(VHOST_VIRTIO, 0x21, struct vhost_vring_file)
11351b24e34SJan Kiszka /* Set eventfd to signal an error */
11451b24e34SJan Kiszka #define VHOST_SET_VRING_ERR _IOW(VHOST_VIRTIO, 0x22, struct vhost_vring_file)
11551b24e34SJan Kiszka 
11651b24e34SJan Kiszka /* VHOST_NET specific defines */
11751b24e34SJan Kiszka 
11851b24e34SJan Kiszka /* Attach virtio net ring to a raw socket, or tap device.
11951b24e34SJan Kiszka  * The socket must be already bound to an ethernet device, this device will be
12051b24e34SJan Kiszka  * used for transmit.  Pass fd -1 to unbind from the socket and the transmit
12151b24e34SJan Kiszka  * device.  This can be used to stop the ring (e.g. for migration). */
12251b24e34SJan Kiszka #define VHOST_NET_SET_BACKEND _IOW(VHOST_VIRTIO, 0x30, struct vhost_vring_file)
12351b24e34SJan Kiszka 
12451b24e34SJan Kiszka /* Feature bits */
12551b24e34SJan Kiszka /* Log all write descriptors. Can be changed while device is active. */
12651b24e34SJan Kiszka #define VHOST_F_LOG_ALL 26
12751b24e34SJan Kiszka /* vhost-net should add virtio_net_hdr for RX, and strip for TX packets. */
12851b24e34SJan Kiszka #define VHOST_NET_F_VIRTIO_NET_HDR 27
12951b24e34SJan Kiszka 
130*e098b453SAlexey Kardashevskiy /* VHOST_SCSI specific definitions */
131*e098b453SAlexey Kardashevskiy 
132*e098b453SAlexey Kardashevskiy /*
133*e098b453SAlexey Kardashevskiy  * Used by QEMU userspace to ensure a consistent vhost-scsi ABI.
134*e098b453SAlexey Kardashevskiy  *
135*e098b453SAlexey Kardashevskiy  * ABI Rev 0: July 2012 version starting point for v3.6-rc merge candidate +
136*e098b453SAlexey Kardashevskiy  *            RFC-v2 vhost-scsi userspace.  Add GET_ABI_VERSION ioctl usage
137*e098b453SAlexey Kardashevskiy  * ABI Rev 1: January 2013. Ignore vhost_tpgt filed in struct vhost_scsi_target.
138*e098b453SAlexey Kardashevskiy  *            All the targets under vhost_wwpn can be seen and used by guset.
139*e098b453SAlexey Kardashevskiy  */
140*e098b453SAlexey Kardashevskiy 
141*e098b453SAlexey Kardashevskiy #define VHOST_SCSI_ABI_VERSION	1
142*e098b453SAlexey Kardashevskiy 
143*e098b453SAlexey Kardashevskiy struct vhost_scsi_target {
144*e098b453SAlexey Kardashevskiy 	int abi_version;
145*e098b453SAlexey Kardashevskiy 	char vhost_wwpn[224]; /* TRANSPORT_IQN_LEN */
146*e098b453SAlexey Kardashevskiy 	unsigned short vhost_tpgt;
147*e098b453SAlexey Kardashevskiy 	unsigned short reserved;
148*e098b453SAlexey Kardashevskiy };
149*e098b453SAlexey Kardashevskiy 
150*e098b453SAlexey Kardashevskiy #define VHOST_SCSI_SET_ENDPOINT _IOW(VHOST_VIRTIO, 0x40, struct vhost_scsi_target)
151*e098b453SAlexey Kardashevskiy #define VHOST_SCSI_CLEAR_ENDPOINT _IOW(VHOST_VIRTIO, 0x41, struct vhost_scsi_target)
152*e098b453SAlexey Kardashevskiy /* Changing this breaks userspace. */
153*e098b453SAlexey Kardashevskiy #define VHOST_SCSI_GET_ABI_VERSION _IOW(VHOST_VIRTIO, 0x42, int)
154*e098b453SAlexey Kardashevskiy /* Set and get the events missed flag */
155*e098b453SAlexey Kardashevskiy #define VHOST_SCSI_SET_EVENTS_MISSED _IOW(VHOST_VIRTIO, 0x43, __u32)
156*e098b453SAlexey Kardashevskiy #define VHOST_SCSI_GET_EVENTS_MISSED _IOW(VHOST_VIRTIO, 0x44, __u32)
157*e098b453SAlexey Kardashevskiy 
15851b24e34SJan Kiszka #endif
159