virtio_config.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #ifndef _LINUX_VIRTIO_CONFIG_H
  2. #define _LINUX_VIRTIO_CONFIG_H
  3. /* Virtio devices use a standardized configuration space to define their
  4. * features and pass configuration information, but each implementation can
  5. * store and access that space differently. */
  6. #include <linux/types.h>
  7. /* Status byte for guest to report progress, and synchronize features. */
  8. /* We have seen device and processed generic fields (VIRTIO_CONFIG_F_VIRTIO) */
  9. #define VIRTIO_CONFIG_S_ACKNOWLEDGE 1
  10. /* We have found a driver for the device. */
  11. #define VIRTIO_CONFIG_S_DRIVER 2
  12. /* Driver has used its parts of the config, and is happy */
  13. #define VIRTIO_CONFIG_S_DRIVER_OK 4
  14. /* We've given up on this device. */
  15. #define VIRTIO_CONFIG_S_FAILED 0x80
  16. #ifdef __KERNEL__
  17. struct virtio_device;
  18. /**
  19. * virtio_config_ops - operations for configuring a virtio device
  20. * @feature: search for a feature in this config
  21. * vdev: the virtio_device
  22. * bit: the feature bit
  23. * Returns true if the feature is supported. Acknowledges the feature
  24. * so the host can see it.
  25. * @get: read the value of a configuration field
  26. * vdev: the virtio_device
  27. * offset: the offset of the configuration field
  28. * buf: the buffer to write the field value into.
  29. * len: the length of the buffer
  30. * Note that contents are conventionally little-endian.
  31. * @set: write the value of a configuration field
  32. * vdev: the virtio_device
  33. * offset: the offset of the configuration field
  34. * buf: the buffer to read the field value from.
  35. * len: the length of the buffer
  36. * Note that contents are conventionally little-endian.
  37. * @get_status: read the status byte
  38. * vdev: the virtio_device
  39. * Returns the status byte
  40. * @set_status: write the status byte
  41. * vdev: the virtio_device
  42. * status: the new status byte
  43. * @find_vq: find a virtqueue and instantiate it.
  44. * vdev: the virtio_device
  45. * index: the 0-based virtqueue number in case there's more than one.
  46. * callback: the virqtueue callback
  47. * Returns the new virtqueue or ERR_PTR() (eg. -ENOENT).
  48. * @del_vq: free a virtqueue found by find_vq().
  49. */
  50. struct virtio_config_ops
  51. {
  52. bool (*feature)(struct virtio_device *vdev, unsigned bit);
  53. void (*get)(struct virtio_device *vdev, unsigned offset,
  54. void *buf, unsigned len);
  55. void (*set)(struct virtio_device *vdev, unsigned offset,
  56. const void *buf, unsigned len);
  57. u8 (*get_status)(struct virtio_device *vdev);
  58. void (*set_status)(struct virtio_device *vdev, u8 status);
  59. struct virtqueue *(*find_vq)(struct virtio_device *vdev,
  60. unsigned index,
  61. bool (*callback)(struct virtqueue *));
  62. void (*del_vq)(struct virtqueue *vq);
  63. };
  64. /**
  65. * virtio_config_val - look for a feature and get a single virtio config.
  66. * @vdev: the virtio device
  67. * @fbit: the feature bit
  68. * @offset: the type to search for.
  69. * @val: a pointer to the value to fill in.
  70. *
  71. * The return value is -ENOENT if the feature doesn't exist. Otherwise
  72. * the value is endian-corrected and returned in v. */
  73. #define virtio_config_val(vdev, fbit, offset, v) ({ \
  74. int _err; \
  75. if ((vdev)->config->feature((vdev), (fbit))) { \
  76. __virtio_config_val((vdev), (offset), (v)); \
  77. _err = 0; \
  78. } else \
  79. _err = -ENOENT; \
  80. _err; \
  81. })
  82. /**
  83. * __virtio_config_val - get a single virtio config without feature check.
  84. * @vdev: the virtio device
  85. * @offset: the type to search for.
  86. * @val: a pointer to the value to fill in.
  87. *
  88. * The value is endian-corrected and returned in v. */
  89. #define __virtio_config_val(vdev, offset, v) do { \
  90. BUILD_BUG_ON(sizeof(*(v)) != 1 && sizeof(*(v)) != 2 \
  91. && sizeof(*(v)) != 4 && sizeof(*(v)) != 8); \
  92. (vdev)->config->get((vdev), (offset), (v), sizeof(*(v))); \
  93. switch (sizeof(*(v))) { \
  94. case 2: le16_to_cpus((__u16 *) v); break; \
  95. case 4: le32_to_cpus((__u32 *) v); break; \
  96. case 8: le64_to_cpus((__u64 *) v); break; \
  97. } \
  98. } while(0)
  99. #endif /* __KERNEL__ */
  100. #endif /* _LINUX_VIRTIO_CONFIG_H */