virtio_ring.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #ifndef _LINUX_VIRTIO_RING_H
  2. #define _LINUX_VIRTIO_RING_H
  3. /* An interface for efficient virtio implementation, currently for use by KVM
  4. * and lguest, but hopefully others soon. Do NOT change this since it will
  5. * break existing servers and clients.
  6. *
  7. * This header is BSD licensed so anyone can use the definitions to implement
  8. * compatible drivers/servers.
  9. *
  10. * Copyright Rusty Russell IBM Corporation 2007. */
  11. #include <linux/types.h>
  12. /* This marks a buffer as continuing via the next field. */
  13. #define VRING_DESC_F_NEXT 1
  14. /* This marks a buffer as write-only (otherwise read-only). */
  15. #define VRING_DESC_F_WRITE 2
  16. /* This means the buffer contains a list of buffer descriptors. */
  17. #define VRING_DESC_F_INDIRECT 4
  18. /* The Host uses this in used->flags to advise the Guest: don't kick me when
  19. * you add a buffer. It's unreliable, so it's simply an optimization. Guest
  20. * will still kick if it's out of buffers. */
  21. #define VRING_USED_F_NO_NOTIFY 1
  22. /* The Guest uses this in avail->flags to advise the Host: don't interrupt me
  23. * when you consume a buffer. It's unreliable, so it's simply an
  24. * optimization. */
  25. #define VRING_AVAIL_F_NO_INTERRUPT 1
  26. /* We support indirect buffer descriptors */
  27. #define VIRTIO_RING_F_INDIRECT_DESC 28
  28. /* Virtio ring descriptors: 16 bytes. These can chain together via "next". */
  29. struct vring_desc
  30. {
  31. /* Address (guest-physical). */
  32. __u64 addr;
  33. /* Length. */
  34. __u32 len;
  35. /* The flags as indicated above. */
  36. __u16 flags;
  37. /* We chain unused descriptors via this, too */
  38. __u16 next;
  39. };
  40. struct vring_avail
  41. {
  42. __u16 flags;
  43. __u16 idx;
  44. __u16 ring[];
  45. };
  46. /* u32 is used here for ids for padding reasons. */
  47. struct vring_used_elem
  48. {
  49. /* Index of start of used descriptor chain. */
  50. __u32 id;
  51. /* Total length of the descriptor chain which was used (written to) */
  52. __u32 len;
  53. };
  54. struct vring_used
  55. {
  56. __u16 flags;
  57. __u16 idx;
  58. struct vring_used_elem ring[];
  59. };
  60. struct vring {
  61. unsigned int num;
  62. struct vring_desc *desc;
  63. struct vring_avail *avail;
  64. struct vring_used *used;
  65. };
  66. /* The standard layout for the ring is a continuous chunk of memory which looks
  67. * like this. We assume num is a power of 2.
  68. *
  69. * struct vring
  70. * {
  71. * // The actual descriptors (16 bytes each)
  72. * struct vring_desc desc[num];
  73. *
  74. * // A ring of available descriptor heads with free-running index.
  75. * __u16 avail_flags;
  76. * __u16 avail_idx;
  77. * __u16 available[num];
  78. *
  79. * // Padding to the next align boundary.
  80. * char pad[];
  81. *
  82. * // A ring of used descriptor heads with free-running index.
  83. * __u16 used_flags;
  84. * __u16 used_idx;
  85. * struct vring_used_elem used[num];
  86. * };
  87. */
  88. static inline void vring_init(struct vring *vr, unsigned int num, void *p,
  89. unsigned long align)
  90. {
  91. vr->num = num;
  92. vr->desc = p;
  93. vr->avail = p + num*sizeof(struct vring_desc);
  94. vr->used = (void *)(((unsigned long)&vr->avail->ring[num] + align-1)
  95. & ~(align - 1));
  96. }
  97. static inline unsigned vring_size(unsigned int num, unsigned long align)
  98. {
  99. return ((sizeof(struct vring_desc) * num + sizeof(__u16) * (2 + num)
  100. + align - 1) & ~(align - 1))
  101. + sizeof(__u16) * 2 + sizeof(struct vring_used_elem) * num;
  102. }
  103. #ifdef __KERNEL__
  104. #include <linux/irqreturn.h>
  105. struct virtio_device;
  106. struct virtqueue;
  107. struct virtqueue *vring_new_virtqueue(unsigned int num,
  108. unsigned int vring_align,
  109. struct virtio_device *vdev,
  110. void *pages,
  111. void (*notify)(struct virtqueue *vq),
  112. void (*callback)(struct virtqueue *vq),
  113. const char *name);
  114. void vring_del_virtqueue(struct virtqueue *vq);
  115. /* Filter out transport-specific feature bits. */
  116. void vring_transport_features(struct virtio_device *vdev);
  117. irqreturn_t vring_interrupt(int irq, void *_vq);
  118. #endif /* __KERNEL__ */
  119. #endif /* _LINUX_VIRTIO_RING_H */