virtio_config.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. #include <linux/virtio.h>
  18. /**
  19. * virtio_config_ops - operations for configuring a virtio device
  20. * @get: read the value of a configuration field
  21. * vdev: the virtio_device
  22. * offset: the offset of the configuration field
  23. * buf: the buffer to write the field value into.
  24. * len: the length of the buffer
  25. * @set: write the value of a configuration field
  26. * vdev: the virtio_device
  27. * offset: the offset of the configuration field
  28. * buf: the buffer to read the field value from.
  29. * len: the length of the buffer
  30. * @get_status: read the status byte
  31. * vdev: the virtio_device
  32. * Returns the status byte
  33. * @set_status: write the status byte
  34. * vdev: the virtio_device
  35. * status: the new status byte
  36. * @reset: reset the device
  37. * vdev: the virtio device
  38. * After this, status and feature negotiation must be done again
  39. * @find_vq: find a virtqueue and instantiate it.
  40. * vdev: the virtio_device
  41. * index: the 0-based virtqueue number in case there's more than one.
  42. * callback: the virqtueue callback
  43. * Returns the new virtqueue or ERR_PTR() (eg. -ENOENT).
  44. * @del_vq: free a virtqueue found by find_vq().
  45. * @get_features: get the array of feature bits for this device.
  46. * vdev: the virtio_device
  47. * Returns the first 32 feature bits (all we currently need).
  48. * @set_features: confirm what device features we'll be using.
  49. * vdev: the virtio_device
  50. * feature: the first 32 feature bits
  51. */
  52. struct virtio_config_ops
  53. {
  54. void (*get)(struct virtio_device *vdev, unsigned offset,
  55. void *buf, unsigned len);
  56. void (*set)(struct virtio_device *vdev, unsigned offset,
  57. const void *buf, unsigned len);
  58. u8 (*get_status)(struct virtio_device *vdev);
  59. void (*set_status)(struct virtio_device *vdev, u8 status);
  60. void (*reset)(struct virtio_device *vdev);
  61. struct virtqueue *(*find_vq)(struct virtio_device *vdev,
  62. unsigned index,
  63. void (*callback)(struct virtqueue *));
  64. void (*del_vq)(struct virtqueue *vq);
  65. u32 (*get_features)(struct virtio_device *vdev);
  66. void (*set_features)(struct virtio_device *vdev, u32 features);
  67. };
  68. /* If driver didn't advertise the feature, it will never appear. */
  69. void virtio_check_driver_offered_feature(const struct virtio_device *vdev,
  70. unsigned int fbit);
  71. /**
  72. * virtio_has_feature - helper to determine if this device has this feature.
  73. * @vdev: the device
  74. * @fbit: the feature bit
  75. */
  76. static inline bool virtio_has_feature(const struct virtio_device *vdev,
  77. unsigned int fbit)
  78. {
  79. /* Did you forget to fix assumptions on max features? */
  80. if (__builtin_constant_p(fbit))
  81. BUILD_BUG_ON(fbit >= 32);
  82. virtio_check_driver_offered_feature(vdev, fbit);
  83. return test_bit(fbit, vdev->features);
  84. }
  85. /**
  86. * virtio_config_val - look for a feature and get a virtio config entry.
  87. * @vdev: the virtio device
  88. * @fbit: the feature bit
  89. * @offset: the type to search for.
  90. * @val: a pointer to the value to fill in.
  91. *
  92. * The return value is -ENOENT if the feature doesn't exist. Otherwise
  93. * the config value is copied into whatever is pointed to by v. */
  94. #define virtio_config_val(vdev, fbit, offset, v) \
  95. virtio_config_buf((vdev), (fbit), (offset), (v), sizeof(v))
  96. static inline int virtio_config_buf(struct virtio_device *vdev,
  97. unsigned int fbit,
  98. unsigned int offset,
  99. void *buf, unsigned len)
  100. {
  101. if (!virtio_has_feature(vdev, fbit))
  102. return -ENOENT;
  103. vdev->config->get(vdev, offset, buf, len);
  104. return 0;
  105. }
  106. #endif /* __KERNEL__ */
  107. #endif /* _LINUX_VIRTIO_CONFIG_H */