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