virtio_ring.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. /* The Host uses this in used->flags to advise the Guest: don't kick me when
  17. * you add a buffer. It's unreliable, so it's simply an optimization. Guest
  18. * will still kick if it's out of buffers. */
  19. #define VRING_USED_F_NO_NOTIFY 1
  20. /* The Guest uses this in avail->flags to advise the Host: don't interrupt me
  21. * when you consume a buffer. It's unreliable, so it's simply an
  22. * optimization. */
  23. #define VRING_AVAIL_F_NO_INTERRUPT 1
  24. /* Virtio ring descriptors: 16 bytes. These can chain together via "next". */
  25. struct vring_desc
  26. {
  27. /* Address (guest-physical). */
  28. __u64 addr;
  29. /* Length. */
  30. __u32 len;
  31. /* The flags as indicated above. */
  32. __u16 flags;
  33. /* We chain unused descriptors via this, too */
  34. __u16 next;
  35. };
  36. struct vring_avail
  37. {
  38. __u16 flags;
  39. __u16 idx;
  40. __u16 ring[];
  41. };
  42. /* u32 is used here for ids for padding reasons. */
  43. struct vring_used_elem
  44. {
  45. /* Index of start of used descriptor chain. */
  46. __u32 id;
  47. /* Total length of the descriptor chain which was used (written to) */
  48. __u32 len;
  49. };
  50. struct vring_used
  51. {
  52. __u16 flags;
  53. __u16 idx;
  54. struct vring_used_elem ring[];
  55. };
  56. struct vring {
  57. unsigned int num;
  58. struct vring_desc *desc;
  59. struct vring_avail *avail;
  60. struct vring_used *used;
  61. };
  62. /* The standard layout for the ring is a continuous chunk of memory which looks
  63. * like this. We assume num is a power of 2.
  64. *
  65. * struct vring
  66. * {
  67. * // The actual descriptors (16 bytes each)
  68. * struct vring_desc desc[num];
  69. *
  70. * // A ring of available descriptor heads with free-running index.
  71. * __u16 avail_flags;
  72. * __u16 avail_idx;
  73. * __u16 available[num];
  74. *
  75. * // Padding to the next page boundary.
  76. * char pad[];
  77. *
  78. * // A ring of used descriptor heads with free-running index.
  79. * __u16 used_flags;
  80. * __u16 used_idx;
  81. * struct vring_used_elem used[num];
  82. * };
  83. */
  84. static inline void vring_init(struct vring *vr, unsigned int num, void *p,
  85. unsigned long pagesize)
  86. {
  87. vr->num = num;
  88. vr->desc = p;
  89. vr->avail = p + num*sizeof(struct vring_desc);
  90. vr->used = (void *)(((unsigned long)&vr->avail->ring[num] + pagesize-1)
  91. & ~(pagesize - 1));
  92. }
  93. static inline unsigned vring_size(unsigned int num, unsigned long pagesize)
  94. {
  95. return ((sizeof(struct vring_desc) * num + sizeof(__u16) * (2 + num)
  96. + pagesize - 1) & ~(pagesize - 1))
  97. + sizeof(__u16) * 2 + sizeof(struct vring_used_elem) * num;
  98. }
  99. #ifdef __KERNEL__
  100. #include <linux/irqreturn.h>
  101. struct virtio_device;
  102. struct virtqueue;
  103. struct virtqueue *vring_new_virtqueue(unsigned int num,
  104. struct virtio_device *vdev,
  105. void *pages,
  106. void (*notify)(struct virtqueue *vq),
  107. void (*callback)(struct virtqueue *vq));
  108. void vring_del_virtqueue(struct virtqueue *vq);
  109. irqreturn_t vring_interrupt(int irq, void *_vq);
  110. #endif /* __KERNEL__ */
  111. #endif /* _LINUX_VIRTIO_RING_H */