virtio_ring.h 3.1 KB

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