virtio_config.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #ifndef _LINUX_VIRTIO_CONFIG_H
  2. #define _LINUX_VIRTIO_CONFIG_H
  3. /* This header, excluding the #ifdef __KERNEL__ part, is BSD licensed so
  4. * anyone can use the definitions to implement compatible drivers/servers. */
  5. /* Virtio devices use a standardized configuration space to define their
  6. * features and pass configuration information, but each implementation can
  7. * store and access that space differently. */
  8. #include <linux/types.h>
  9. /* Status byte for guest to report progress, and synchronize features. */
  10. /* We have seen device and processed generic fields (VIRTIO_CONFIG_F_VIRTIO) */
  11. #define VIRTIO_CONFIG_S_ACKNOWLEDGE 1
  12. /* We have found a driver for the device. */
  13. #define VIRTIO_CONFIG_S_DRIVER 2
  14. /* Driver has used its parts of the config, and is happy */
  15. #define VIRTIO_CONFIG_S_DRIVER_OK 4
  16. /* We've given up on this device. */
  17. #define VIRTIO_CONFIG_S_FAILED 0x80
  18. /* Some virtio feature bits (currently bits 28 through 31) are reserved for the
  19. * transport being used (eg. virtio_ring), the rest are per-device feature
  20. * bits. */
  21. #define VIRTIO_TRANSPORT_F_START 28
  22. #define VIRTIO_TRANSPORT_F_END 32
  23. /* Do we get callbacks when the ring is completely used, even if we've
  24. * suppressed them? */
  25. #define VIRTIO_F_NOTIFY_ON_EMPTY 24
  26. #ifdef __KERNEL__
  27. #include <linux/virtio.h>
  28. /**
  29. * virtio_config_ops - operations for configuring a virtio device
  30. * @get: read the value of a configuration field
  31. * vdev: the virtio_device
  32. * offset: the offset of the configuration field
  33. * buf: the buffer to write the field value into.
  34. * len: the length of the buffer
  35. * @set: write the value of a configuration field
  36. * vdev: the virtio_device
  37. * offset: the offset of the configuration field
  38. * buf: the buffer to read the field value from.
  39. * len: the length of the buffer
  40. * @get_status: read the status byte
  41. * vdev: the virtio_device
  42. * Returns the status byte
  43. * @set_status: write the status byte
  44. * vdev: the virtio_device
  45. * status: the new status byte
  46. * @reset: reset the device
  47. * vdev: the virtio device
  48. * After this, status and feature negotiation must be done again
  49. * @find_vq: find a virtqueue and instantiate it.
  50. * vdev: the virtio_device
  51. * index: the 0-based virtqueue number in case there's more than one.
  52. * callback: the virqtueue callback
  53. * Returns the new virtqueue or ERR_PTR() (eg. -ENOENT).
  54. * @del_vq: free a virtqueue found by find_vq().
  55. * @get_features: get the array of feature bits for this device.
  56. * vdev: the virtio_device
  57. * Returns the first 32 feature bits (all we currently need).
  58. * @finalize_features: confirm what device features we'll be using.
  59. * vdev: the virtio_device
  60. * This gives the final feature bits for the device: it can change
  61. * the dev->feature bits if it wants.
  62. */
  63. struct virtio_config_ops
  64. {
  65. void (*get)(struct virtio_device *vdev, unsigned offset,
  66. void *buf, unsigned len);
  67. void (*set)(struct virtio_device *vdev, unsigned offset,
  68. const void *buf, unsigned len);
  69. u8 (*get_status)(struct virtio_device *vdev);
  70. void (*set_status)(struct virtio_device *vdev, u8 status);
  71. void (*reset)(struct virtio_device *vdev);
  72. struct virtqueue *(*find_vq)(struct virtio_device *vdev,
  73. unsigned index,
  74. void (*callback)(struct virtqueue *));
  75. void (*del_vq)(struct virtqueue *vq);
  76. u32 (*get_features)(struct virtio_device *vdev);
  77. void (*finalize_features)(struct virtio_device *vdev);
  78. };
  79. /* If driver didn't advertise the feature, it will never appear. */
  80. void virtio_check_driver_offered_feature(const struct virtio_device *vdev,
  81. unsigned int fbit);
  82. /**
  83. * virtio_has_feature - helper to determine if this device has this feature.
  84. * @vdev: the device
  85. * @fbit: the feature bit
  86. */
  87. static inline bool virtio_has_feature(const struct virtio_device *vdev,
  88. unsigned int fbit)
  89. {
  90. /* Did you forget to fix assumptions on max features? */
  91. if (__builtin_constant_p(fbit))
  92. BUILD_BUG_ON(fbit >= 32);
  93. virtio_check_driver_offered_feature(vdev, fbit);
  94. return test_bit(fbit, vdev->features);
  95. }
  96. /**
  97. * virtio_config_val - look for a feature and get a virtio config entry.
  98. * @vdev: the virtio device
  99. * @fbit: the feature bit
  100. * @offset: the type to search for.
  101. * @val: a pointer to the value to fill in.
  102. *
  103. * The return value is -ENOENT if the feature doesn't exist. Otherwise
  104. * the config value is copied into whatever is pointed to by v. */
  105. #define virtio_config_val(vdev, fbit, offset, v) \
  106. virtio_config_buf((vdev), (fbit), (offset), (v), sizeof(*v))
  107. static inline int virtio_config_buf(struct virtio_device *vdev,
  108. unsigned int fbit,
  109. unsigned int offset,
  110. void *buf, unsigned len)
  111. {
  112. if (!virtio_has_feature(vdev, fbit))
  113. return -ENOENT;
  114. vdev->config->get(vdev, offset, buf, len);
  115. return 0;
  116. }
  117. #endif /* __KERNEL__ */
  118. #endif /* _LINUX_VIRTIO_CONFIG_H */