vduse.h 9.6 KB

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