You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

virtio-ring.h 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #ifndef _VIRTIO_RING_H_
  2. # define _VIRTIO_RING_H_
  3. /* Status byte for guest to report progress, and synchronize features. */
  4. /* We have seen device and processed generic fields (VIRTIO_CONFIG_F_VIRTIO) */
  5. #define VIRTIO_CONFIG_S_ACKNOWLEDGE 1
  6. /* We have found a driver for the device. */
  7. #define VIRTIO_CONFIG_S_DRIVER 2
  8. /* Driver has used its parts of the config, and is happy */
  9. #define VIRTIO_CONFIG_S_DRIVER_OK 4
  10. /* We've given up on this device. */
  11. #define VIRTIO_CONFIG_S_FAILED 0x80
  12. #define MAX_QUEUE_NUM (256)
  13. #define VRING_DESC_F_NEXT 1
  14. #define VRING_DESC_F_WRITE 2
  15. #define VRING_AVAIL_F_NO_INTERRUPT 1
  16. #define VRING_USED_F_NO_NOTIFY 1
  17. struct vring_desc
  18. {
  19. u64 addr;
  20. u32 len;
  21. u16 flags;
  22. u16 next;
  23. };
  24. struct vring_avail
  25. {
  26. u16 flags;
  27. u16 idx;
  28. u16 ring[0];
  29. };
  30. struct vring_used_elem
  31. {
  32. u32 id;
  33. u32 len;
  34. };
  35. struct vring_used
  36. {
  37. u16 flags;
  38. u16 idx;
  39. struct vring_used_elem ring[];
  40. };
  41. struct vring {
  42. unsigned int num;
  43. struct vring_desc *desc;
  44. struct vring_avail *avail;
  45. struct vring_used *used;
  46. };
  47. #define vring_size(num) \
  48. (((((sizeof(struct vring_desc) * num) + \
  49. (sizeof(struct vring_avail) + sizeof(u16) * num)) \
  50. + PAGE_MASK) & ~PAGE_MASK) + \
  51. (sizeof(struct vring_used) + sizeof(struct vring_used_elem) * num))
  52. typedef unsigned char virtio_queue_t[PAGE_MASK + vring_size(MAX_QUEUE_NUM)];
  53. struct vring_virtqueue {
  54. virtio_queue_t queue;
  55. struct vring vring;
  56. u16 free_head;
  57. u16 last_used_idx;
  58. void *vdata[MAX_QUEUE_NUM];
  59. /* PCI */
  60. int queue_index;
  61. };
  62. struct vring_list {
  63. char *addr;
  64. unsigned int length;
  65. };
  66. static inline void vring_init(struct vring *vr,
  67. unsigned int num, unsigned char *queue)
  68. {
  69. unsigned int i;
  70. unsigned long pa;
  71. vr->num = num;
  72. /* physical address of desc must be page aligned */
  73. pa = virt_to_phys(queue);
  74. pa = (pa + PAGE_MASK) & ~PAGE_MASK;
  75. vr->desc = phys_to_virt(pa);
  76. vr->avail = (struct vring_avail *)&vr->desc[num];
  77. /* physical address of used must be page aligned */
  78. pa = virt_to_phys(&vr->avail->ring[num]);
  79. pa = (pa + PAGE_MASK) & ~PAGE_MASK;
  80. vr->used = phys_to_virt(pa);
  81. for (i = 0; i < num - 1; i++)
  82. vr->desc[i].next = i + 1;
  83. vr->desc[i].next = 0;
  84. }
  85. static inline void vring_enable_cb(struct vring_virtqueue *vq)
  86. {
  87. vq->vring.avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT;
  88. }
  89. static inline void vring_disable_cb(struct vring_virtqueue *vq)
  90. {
  91. vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
  92. }
  93. /*
  94. * vring_more_used
  95. *
  96. * is there some used buffers ?
  97. *
  98. */
  99. static inline int vring_more_used(struct vring_virtqueue *vq)
  100. {
  101. wmb();
  102. return vq->last_used_idx != vq->vring.used->idx;
  103. }
  104. void vring_detach(struct vring_virtqueue *vq, unsigned int head);
  105. void *vring_get_buf(struct vring_virtqueue *vq, unsigned int *len);
  106. void vring_add_buf(struct vring_virtqueue *vq, struct vring_list list[],
  107. unsigned int out, unsigned int in,
  108. void *index, int num_added);
  109. void vring_kick(unsigned int ioaddr, struct vring_virtqueue *vq, int num_added);
  110. #endif /* _VIRTIO_RING_H_ */