virtio_config.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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/err.h>
  28. #include <linux/virtio.h>
  29. /**
  30. * virtio_config_ops - operations for configuring a virtio device
  31. * @get: read the value of a configuration field
  32. * vdev: the virtio_device
  33. * offset: the offset of the configuration field
  34. * buf: the buffer to write the field value into.
  35. * len: the length of the buffer
  36. * @set: write the value of a configuration field
  37. * vdev: the virtio_device
  38. * offset: the offset of the configuration field
  39. * buf: the buffer to read the field value from.
  40. * len: the length of the buffer
  41. * @get_status: read the status byte
  42. * vdev: the virtio_device
  43. * Returns the status byte
  44. * @set_status: write the status byte
  45. * vdev: the virtio_device
  46. * status: the new status byte
  47. * @request_vqs: request the specified number of virtqueues
  48. * vdev: the virtio_device
  49. * max_vqs: the max number of virtqueues we want
  50. * If supplied, must call before any virtqueues are instantiated.
  51. * To modify the max number of virtqueues after request_vqs has been
  52. * called, call free_vqs and then request_vqs with a new value.
  53. * @free_vqs: cleanup resources allocated by request_vqs
  54. * vdev: the virtio_device
  55. * If supplied, must call after all virtqueues have been deleted.
  56. * @reset: reset the device
  57. * vdev: the virtio device
  58. * After this, status and feature negotiation must be done again
  59. * @find_vqs: find virtqueues and instantiate them.
  60. * vdev: the virtio_device
  61. * nvqs: the number of virtqueues to find
  62. * vqs: on success, includes new virtqueues
  63. * callbacks: array of callbacks, for each virtqueue
  64. * names: array of virtqueue names (mainly for debugging)
  65. * Returns 0 on success or error status
  66. * @del_vqs: free virtqueues found by find_vqs().
  67. * @get_features: get the array of feature bits for this device.
  68. * vdev: the virtio_device
  69. * Returns the first 32 feature bits (all we currently need).
  70. * @finalize_features: confirm what device features we'll be using.
  71. * vdev: the virtio_device
  72. * This gives the final feature bits for the device: it can change
  73. * the dev->feature bits if it wants.
  74. */
  75. typedef void vq_callback_t(struct virtqueue *);
  76. struct virtio_config_ops {
  77. void (*get)(struct virtio_device *vdev, unsigned offset,
  78. void *buf, unsigned len);
  79. void (*set)(struct virtio_device *vdev, unsigned offset,
  80. const void *buf, unsigned len);
  81. u8 (*get_status)(struct virtio_device *vdev);
  82. void (*set_status)(struct virtio_device *vdev, u8 status);
  83. void (*reset)(struct virtio_device *vdev);
  84. int (*find_vqs)(struct virtio_device *, unsigned nvqs,
  85. struct virtqueue *vqs[],
  86. vq_callback_t *callbacks[],
  87. const char *names[]);
  88. void (*del_vqs)(struct virtio_device *);
  89. u32 (*get_features)(struct virtio_device *vdev);
  90. void (*finalize_features)(struct virtio_device *vdev);
  91. };
  92. /* If driver didn't advertise the feature, it will never appear. */
  93. void virtio_check_driver_offered_feature(const struct virtio_device *vdev,
  94. unsigned int fbit);
  95. /**
  96. * virtio_has_feature - helper to determine if this device has this feature.
  97. * @vdev: the device
  98. * @fbit: the feature bit
  99. */
  100. static inline bool virtio_has_feature(const struct virtio_device *vdev,
  101. unsigned int fbit)
  102. {
  103. /* Did you forget to fix assumptions on max features? */
  104. if (__builtin_constant_p(fbit))
  105. BUILD_BUG_ON(fbit >= 32);
  106. if (fbit < VIRTIO_TRANSPORT_F_START)
  107. virtio_check_driver_offered_feature(vdev, fbit);
  108. return test_bit(fbit, vdev->features);
  109. }
  110. /**
  111. * virtio_config_val - look for a feature and get a virtio config entry.
  112. * @vdev: the virtio device
  113. * @fbit: the feature bit
  114. * @offset: the type to search for.
  115. * @val: a pointer to the value to fill in.
  116. *
  117. * The return value is -ENOENT if the feature doesn't exist. Otherwise
  118. * the config value is copied into whatever is pointed to by v. */
  119. #define virtio_config_val(vdev, fbit, offset, v) \
  120. virtio_config_buf((vdev), (fbit), (offset), (v), sizeof(*v))
  121. static inline int virtio_config_buf(struct virtio_device *vdev,
  122. unsigned int fbit,
  123. unsigned int offset,
  124. void *buf, unsigned len)
  125. {
  126. if (!virtio_has_feature(vdev, fbit))
  127. return -ENOENT;
  128. vdev->config->get(vdev, offset, buf, len);
  129. return 0;
  130. }
  131. static inline
  132. struct virtqueue *virtio_find_single_vq(struct virtio_device *vdev,
  133. vq_callback_t *c, const char *n)
  134. {
  135. vq_callback_t *callbacks[] = { c };
  136. const char *names[] = { n };
  137. struct virtqueue *vq;
  138. int err = vdev->config->find_vqs(vdev, 1, &vq, callbacks, names);
  139. if (err < 0)
  140. return ERR_PTR(err);
  141. return vq;
  142. }
  143. #endif /* __KERNEL__ */
  144. #endif /* _LINUX_VIRTIO_CONFIG_H */