2
0

virtio_pci.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. /*
  2. * Virtio PCI driver
  3. *
  4. * This module allows virtio devices to be used over a virtual PCI device.
  5. * This can be used with QEMU based VMMs like KVM or Xen.
  6. *
  7. * Copyright IBM Corp. 2007
  8. *
  9. * Authors:
  10. * Anthony Liguori <aliguori@us.ibm.com>
  11. *
  12. * This header is BSD licensed so anyone can use the definitions to implement
  13. * compatible drivers/servers.
  14. *
  15. * Redistribution and use in source and binary forms, with or without
  16. * modification, are permitted provided that the following conditions
  17. * are met:
  18. * 1. Redistributions of source code must retain the above copyright
  19. * notice, this list of conditions and the following disclaimer.
  20. * 2. Redistributions in binary form must reproduce the above copyright
  21. * notice, this list of conditions and the following disclaimer in the
  22. * documentation and/or other materials provided with the distribution.
  23. * 3. Neither the name of IBM nor the names of its contributors
  24. * may be used to endorse or promote products derived from this software
  25. * without specific prior written permission.
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND
  27. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  29. * ARE DISCLAIMED. IN NO EVENT SHALL IBM OR CONTRIBUTORS BE LIABLE
  30. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  31. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  32. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  33. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  34. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  35. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  36. * SUCH DAMAGE.
  37. */
  38. #ifndef _LINUX_VIRTIO_PCI_H
  39. #define _LINUX_VIRTIO_PCI_H
  40. #include "standard-headers/linux/types.h"
  41. #include "standard-headers/linux/kernel.h"
  42. #ifndef VIRTIO_PCI_NO_LEGACY
  43. /* A 32-bit r/o bitmask of the features supported by the host */
  44. #define VIRTIO_PCI_HOST_FEATURES 0
  45. /* A 32-bit r/w bitmask of features activated by the guest */
  46. #define VIRTIO_PCI_GUEST_FEATURES 4
  47. /* A 32-bit r/w PFN for the currently selected queue */
  48. #define VIRTIO_PCI_QUEUE_PFN 8
  49. /* A 16-bit r/o queue size for the currently selected queue */
  50. #define VIRTIO_PCI_QUEUE_NUM 12
  51. /* A 16-bit r/w queue selector */
  52. #define VIRTIO_PCI_QUEUE_SEL 14
  53. /* A 16-bit r/w queue notifier */
  54. #define VIRTIO_PCI_QUEUE_NOTIFY 16
  55. /* An 8-bit device status register. */
  56. #define VIRTIO_PCI_STATUS 18
  57. /* An 8-bit r/o interrupt status register. Reading the value will return the
  58. * current contents of the ISR and will also clear it. This is effectively
  59. * a read-and-acknowledge. */
  60. #define VIRTIO_PCI_ISR 19
  61. /* MSI-X registers: only enabled if MSI-X is enabled. */
  62. /* A 16-bit vector for configuration changes. */
  63. #define VIRTIO_MSI_CONFIG_VECTOR 20
  64. /* A 16-bit vector for selected queue notifications. */
  65. #define VIRTIO_MSI_QUEUE_VECTOR 22
  66. /* The remaining space is defined by each driver as the per-driver
  67. * configuration space */
  68. #define VIRTIO_PCI_CONFIG_OFF(msix_enabled) ((msix_enabled) ? 24 : 20)
  69. /* Deprecated: please use VIRTIO_PCI_CONFIG_OFF instead */
  70. #define VIRTIO_PCI_CONFIG(dev) VIRTIO_PCI_CONFIG_OFF((dev)->msix_enabled)
  71. /* Virtio ABI version, this must match exactly */
  72. #define VIRTIO_PCI_ABI_VERSION 0
  73. /* How many bits to shift physical queue address written to QUEUE_PFN.
  74. * 12 is historical, and due to x86 page size. */
  75. #define VIRTIO_PCI_QUEUE_ADDR_SHIFT 12
  76. /* The alignment to use between consumer and producer parts of vring.
  77. * x86 pagesize again. */
  78. #define VIRTIO_PCI_VRING_ALIGN 4096
  79. #endif /* VIRTIO_PCI_NO_LEGACY */
  80. /* The bit of the ISR which indicates a device configuration change. */
  81. #define VIRTIO_PCI_ISR_CONFIG 0x2
  82. /* Vector value used to disable MSI for queue */
  83. #define VIRTIO_MSI_NO_VECTOR 0xffff
  84. #ifndef VIRTIO_PCI_NO_MODERN
  85. /* IDs for different capabilities. Must all exist. */
  86. /* Common configuration */
  87. #define VIRTIO_PCI_CAP_COMMON_CFG 1
  88. /* Notifications */
  89. #define VIRTIO_PCI_CAP_NOTIFY_CFG 2
  90. /* ISR access */
  91. #define VIRTIO_PCI_CAP_ISR_CFG 3
  92. /* Device specific configuration */
  93. #define VIRTIO_PCI_CAP_DEVICE_CFG 4
  94. /* PCI configuration access */
  95. #define VIRTIO_PCI_CAP_PCI_CFG 5
  96. /* Additional shared memory capability */
  97. #define VIRTIO_PCI_CAP_SHARED_MEMORY_CFG 8
  98. /* PCI vendor data configuration */
  99. #define VIRTIO_PCI_CAP_VENDOR_CFG 9
  100. /* This is the PCI capability header: */
  101. struct virtio_pci_cap {
  102. uint8_t cap_vndr; /* Generic PCI field: PCI_CAP_ID_VNDR */
  103. uint8_t cap_next; /* Generic PCI field: next ptr. */
  104. uint8_t cap_len; /* Generic PCI field: capability length */
  105. uint8_t cfg_type; /* Identifies the structure. */
  106. uint8_t bar; /* Where to find it. */
  107. uint8_t id; /* Multiple capabilities of the same type */
  108. uint8_t padding[2]; /* Pad to full dword. */
  109. uint32_t offset; /* Offset within bar. */
  110. uint32_t length; /* Length of the structure, in bytes. */
  111. };
  112. /* This is the PCI vendor data capability header: */
  113. struct virtio_pci_vndr_data {
  114. uint8_t cap_vndr; /* Generic PCI field: PCI_CAP_ID_VNDR */
  115. uint8_t cap_next; /* Generic PCI field: next ptr. */
  116. uint8_t cap_len; /* Generic PCI field: capability length */
  117. uint8_t cfg_type; /* Identifies the structure. */
  118. uint16_t vendor_id; /* Identifies the vendor-specific format. */
  119. /* For Vendor Definition */
  120. /* Pads structure to a multiple of 4 bytes */
  121. /* Reads must not have side effects */
  122. };
  123. struct virtio_pci_cap64 {
  124. struct virtio_pci_cap cap;
  125. uint32_t offset_hi; /* Most sig 32 bits of offset */
  126. uint32_t length_hi; /* Most sig 32 bits of length */
  127. };
  128. struct virtio_pci_notify_cap {
  129. struct virtio_pci_cap cap;
  130. uint32_t notify_off_multiplier; /* Multiplier for queue_notify_off. */
  131. };
  132. /* Fields in VIRTIO_PCI_CAP_COMMON_CFG: */
  133. struct virtio_pci_common_cfg {
  134. /* About the whole device. */
  135. uint32_t device_feature_select; /* read-write */
  136. uint32_t device_feature; /* read-only */
  137. uint32_t guest_feature_select; /* read-write */
  138. uint32_t guest_feature; /* read-write */
  139. uint16_t msix_config; /* read-write */
  140. uint16_t num_queues; /* read-only */
  141. uint8_t device_status; /* read-write */
  142. uint8_t config_generation; /* read-only */
  143. /* About a specific virtqueue. */
  144. uint16_t queue_select; /* read-write */
  145. uint16_t queue_size; /* read-write, power of 2. */
  146. uint16_t queue_msix_vector; /* read-write */
  147. uint16_t queue_enable; /* read-write */
  148. uint16_t queue_notify_off; /* read-only */
  149. uint32_t queue_desc_lo; /* read-write */
  150. uint32_t queue_desc_hi; /* read-write */
  151. uint32_t queue_avail_lo; /* read-write */
  152. uint32_t queue_avail_hi; /* read-write */
  153. uint32_t queue_used_lo; /* read-write */
  154. uint32_t queue_used_hi; /* read-write */
  155. };
  156. /*
  157. * Warning: do not use sizeof on this: use offsetofend for
  158. * specific fields you need.
  159. */
  160. struct virtio_pci_modern_common_cfg {
  161. struct virtio_pci_common_cfg cfg;
  162. uint16_t queue_notify_data; /* read-write */
  163. uint16_t queue_reset; /* read-write */
  164. uint16_t admin_queue_index; /* read-only */
  165. uint16_t admin_queue_num; /* read-only */
  166. };
  167. /* Fields in VIRTIO_PCI_CAP_PCI_CFG: */
  168. struct virtio_pci_cfg_cap {
  169. struct virtio_pci_cap cap;
  170. uint8_t pci_cfg_data[4]; /* Data for BAR access. */
  171. };
  172. /* Macro versions of offsets for the Old Timers! */
  173. #define VIRTIO_PCI_CAP_VNDR 0
  174. #define VIRTIO_PCI_CAP_NEXT 1
  175. #define VIRTIO_PCI_CAP_LEN 2
  176. #define VIRTIO_PCI_CAP_CFG_TYPE 3
  177. #define VIRTIO_PCI_CAP_BAR 4
  178. #define VIRTIO_PCI_CAP_OFFSET 8
  179. #define VIRTIO_PCI_CAP_LENGTH 12
  180. #define VIRTIO_PCI_NOTIFY_CAP_MULT 16
  181. #define VIRTIO_PCI_COMMON_DFSELECT 0
  182. #define VIRTIO_PCI_COMMON_DF 4
  183. #define VIRTIO_PCI_COMMON_GFSELECT 8
  184. #define VIRTIO_PCI_COMMON_GF 12
  185. #define VIRTIO_PCI_COMMON_MSIX 16
  186. #define VIRTIO_PCI_COMMON_NUMQ 18
  187. #define VIRTIO_PCI_COMMON_STATUS 20
  188. #define VIRTIO_PCI_COMMON_CFGGENERATION 21
  189. #define VIRTIO_PCI_COMMON_Q_SELECT 22
  190. #define VIRTIO_PCI_COMMON_Q_SIZE 24
  191. #define VIRTIO_PCI_COMMON_Q_MSIX 26
  192. #define VIRTIO_PCI_COMMON_Q_ENABLE 28
  193. #define VIRTIO_PCI_COMMON_Q_NOFF 30
  194. #define VIRTIO_PCI_COMMON_Q_DESCLO 32
  195. #define VIRTIO_PCI_COMMON_Q_DESCHI 36
  196. #define VIRTIO_PCI_COMMON_Q_AVAILLO 40
  197. #define VIRTIO_PCI_COMMON_Q_AVAILHI 44
  198. #define VIRTIO_PCI_COMMON_Q_USEDLO 48
  199. #define VIRTIO_PCI_COMMON_Q_USEDHI 52
  200. #define VIRTIO_PCI_COMMON_Q_NDATA 56
  201. #define VIRTIO_PCI_COMMON_Q_RESET 58
  202. #define VIRTIO_PCI_COMMON_ADM_Q_IDX 60
  203. #define VIRTIO_PCI_COMMON_ADM_Q_NUM 62
  204. #endif /* VIRTIO_PCI_NO_MODERN */
  205. /* Admin command status. */
  206. #define VIRTIO_ADMIN_STATUS_OK 0
  207. /* Admin command opcode. */
  208. #define VIRTIO_ADMIN_CMD_LIST_QUERY 0x0
  209. #define VIRTIO_ADMIN_CMD_LIST_USE 0x1
  210. /* Admin command group type. */
  211. #define VIRTIO_ADMIN_GROUP_TYPE_SRIOV 0x1
  212. /* Transitional device admin command. */
  213. #define VIRTIO_ADMIN_CMD_LEGACY_COMMON_CFG_WRITE 0x2
  214. #define VIRTIO_ADMIN_CMD_LEGACY_COMMON_CFG_READ 0x3
  215. #define VIRTIO_ADMIN_CMD_LEGACY_DEV_CFG_WRITE 0x4
  216. #define VIRTIO_ADMIN_CMD_LEGACY_DEV_CFG_READ 0x5
  217. #define VIRTIO_ADMIN_CMD_LEGACY_NOTIFY_INFO 0x6
  218. /* Device parts access commands. */
  219. #define VIRTIO_ADMIN_CMD_CAP_ID_LIST_QUERY 0x7
  220. #define VIRTIO_ADMIN_CMD_DEVICE_CAP_GET 0x8
  221. #define VIRTIO_ADMIN_CMD_DRIVER_CAP_SET 0x9
  222. #define VIRTIO_ADMIN_CMD_RESOURCE_OBJ_CREATE 0xa
  223. #define VIRTIO_ADMIN_CMD_RESOURCE_OBJ_DESTROY 0xd
  224. #define VIRTIO_ADMIN_CMD_DEV_PARTS_METADATA_GET 0xe
  225. #define VIRTIO_ADMIN_CMD_DEV_PARTS_GET 0xf
  226. #define VIRTIO_ADMIN_CMD_DEV_PARTS_SET 0x10
  227. #define VIRTIO_ADMIN_CMD_DEV_MODE_SET 0x11
  228. struct virtio_admin_cmd_hdr {
  229. uint16_t opcode;
  230. /*
  231. * 1 - SR-IOV
  232. * 2-65535 - reserved
  233. */
  234. uint16_t group_type;
  235. /* Unused, reserved for future extensions. */
  236. uint8_t reserved1[12];
  237. uint64_t group_member_id;
  238. };
  239. struct virtio_admin_cmd_status {
  240. uint16_t status;
  241. uint16_t status_qualifier;
  242. /* Unused, reserved for future extensions. */
  243. uint8_t reserved2[4];
  244. };
  245. struct virtio_admin_cmd_legacy_wr_data {
  246. uint8_t offset; /* Starting offset of the register(s) to write. */
  247. uint8_t reserved[7];
  248. uint8_t registers[];
  249. };
  250. struct virtio_admin_cmd_legacy_rd_data {
  251. uint8_t offset; /* Starting offset of the register(s) to read. */
  252. };
  253. #define VIRTIO_ADMIN_CMD_NOTIFY_INFO_FLAGS_END 0
  254. #define VIRTIO_ADMIN_CMD_NOTIFY_INFO_FLAGS_OWNER_DEV 0x1
  255. #define VIRTIO_ADMIN_CMD_NOTIFY_INFO_FLAGS_OWNER_MEM 0x2
  256. #define VIRTIO_ADMIN_CMD_MAX_NOTIFY_INFO 4
  257. struct virtio_admin_cmd_notify_info_data {
  258. uint8_t flags; /* 0 = end of list, 1 = owner device, 2 = member device */
  259. uint8_t bar; /* BAR of the member or the owner device */
  260. uint8_t padding[6];
  261. uint64_t offset; /* Offset within bar. */
  262. };
  263. struct virtio_admin_cmd_notify_info_result {
  264. struct virtio_admin_cmd_notify_info_data entries[VIRTIO_ADMIN_CMD_MAX_NOTIFY_INFO];
  265. };
  266. #define VIRTIO_DEV_PARTS_CAP 0x0000
  267. struct virtio_dev_parts_cap {
  268. uint8_t get_parts_resource_objects_limit;
  269. uint8_t set_parts_resource_objects_limit;
  270. };
  271. #define MAX_CAP_ID __KERNEL_DIV_ROUND_UP(VIRTIO_DEV_PARTS_CAP + 1, 64)
  272. struct virtio_admin_cmd_query_cap_id_result {
  273. uint64_t supported_caps[MAX_CAP_ID];
  274. };
  275. struct virtio_admin_cmd_cap_get_data {
  276. uint16_t id;
  277. uint8_t reserved[6];
  278. };
  279. struct virtio_admin_cmd_cap_set_data {
  280. uint16_t id;
  281. uint8_t reserved[6];
  282. uint8_t cap_specific_data[];
  283. };
  284. struct virtio_admin_cmd_resource_obj_cmd_hdr {
  285. uint16_t type;
  286. uint8_t reserved[2];
  287. uint32_t id; /* Indicates unique resource object id per resource object type */
  288. };
  289. struct virtio_admin_cmd_resource_obj_create_data {
  290. struct virtio_admin_cmd_resource_obj_cmd_hdr hdr;
  291. uint64_t flags;
  292. uint8_t resource_obj_specific_data[];
  293. };
  294. #define VIRTIO_RESOURCE_OBJ_DEV_PARTS 0
  295. #define VIRTIO_RESOURCE_OBJ_DEV_PARTS_TYPE_GET 0
  296. #define VIRTIO_RESOURCE_OBJ_DEV_PARTS_TYPE_SET 1
  297. struct virtio_resource_obj_dev_parts {
  298. uint8_t type;
  299. uint8_t reserved[7];
  300. };
  301. #define VIRTIO_ADMIN_CMD_DEV_PARTS_METADATA_TYPE_SIZE 0
  302. #define VIRTIO_ADMIN_CMD_DEV_PARTS_METADATA_TYPE_COUNT 1
  303. #define VIRTIO_ADMIN_CMD_DEV_PARTS_METADATA_TYPE_LIST 2
  304. struct virtio_admin_cmd_dev_parts_metadata_data {
  305. struct virtio_admin_cmd_resource_obj_cmd_hdr hdr;
  306. uint8_t type;
  307. uint8_t reserved[7];
  308. };
  309. #define VIRTIO_DEV_PART_F_OPTIONAL 0
  310. struct virtio_dev_part_hdr {
  311. uint16_t part_type;
  312. uint8_t flags;
  313. uint8_t reserved;
  314. union {
  315. struct {
  316. uint32_t offset;
  317. uint32_t reserved;
  318. } pci_common_cfg;
  319. struct {
  320. uint16_t index;
  321. uint8_t reserved[6];
  322. } vq_index;
  323. } selector;
  324. uint32_t length;
  325. };
  326. struct virtio_dev_part {
  327. struct virtio_dev_part_hdr hdr;
  328. uint8_t value[];
  329. };
  330. struct virtio_admin_cmd_dev_parts_metadata_result {
  331. union {
  332. struct {
  333. uint32_t size;
  334. uint32_t reserved;
  335. } parts_size;
  336. struct {
  337. uint32_t count;
  338. uint32_t reserved;
  339. } hdr_list_count;
  340. struct {
  341. uint32_t count;
  342. uint32_t reserved;
  343. struct virtio_dev_part_hdr hdrs[];
  344. } hdr_list;
  345. };
  346. };
  347. #define VIRTIO_ADMIN_CMD_DEV_PARTS_GET_TYPE_SELECTED 0
  348. #define VIRTIO_ADMIN_CMD_DEV_PARTS_GET_TYPE_ALL 1
  349. struct virtio_admin_cmd_dev_parts_get_data {
  350. struct virtio_admin_cmd_resource_obj_cmd_hdr hdr;
  351. uint8_t type;
  352. uint8_t reserved[7];
  353. struct virtio_dev_part_hdr hdr_list[];
  354. };
  355. struct virtio_admin_cmd_dev_parts_set_data {
  356. struct virtio_admin_cmd_resource_obj_cmd_hdr hdr;
  357. struct virtio_dev_part parts[];
  358. };
  359. #define VIRTIO_ADMIN_CMD_DEV_MODE_F_STOPPED 0
  360. struct virtio_admin_cmd_dev_mode_set_data {
  361. uint8_t flags;
  362. };
  363. #endif