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 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #ifndef _VIRTIO_RING_H_
  2. # define _VIRTIO_RING_H_
  3. #define PAGE_SHIFT (12)
  4. #define PAGE_SIZE (1<<PAGE_SHIFT)
  5. #define PAGE_MASK (PAGE_SIZE-1)
  6. /* Status byte for guest to report progress, and synchronize features. */
  7. /* We have seen device and processed generic fields (VIRTIO_CONFIG_F_VIRTIO) */
  8. #define VIRTIO_CONFIG_S_ACKNOWLEDGE 1
  9. /* We have found a driver for the device. */
  10. #define VIRTIO_CONFIG_S_DRIVER 2
  11. /* Driver has used its parts of the config, and is happy */
  12. #define VIRTIO_CONFIG_S_DRIVER_OK 4
  13. /* We've given up on this device. */
  14. #define VIRTIO_CONFIG_S_FAILED 0x80
  15. #define MAX_QUEUE_NUM (512)
  16. #define VRING_DESC_F_NEXT 1
  17. #define VRING_DESC_F_WRITE 2
  18. #define VRING_AVAIL_F_NO_INTERRUPT 1
  19. #define VRING_USED_F_NO_NOTIFY 1
  20. struct vring_desc
  21. {
  22. u64 addr;
  23. u32 len;
  24. u16 flags;
  25. u16 next;
  26. };
  27. struct vring_avail
  28. {
  29. u16 flags;
  30. u16 idx;
  31. u16 ring[0];
  32. };
  33. struct vring_used_elem
  34. {
  35. u32 id;
  36. u32 len;
  37. };
  38. struct vring_used
  39. {
  40. u16 flags;
  41. u16 idx;
  42. struct vring_used_elem ring[];
  43. };
  44. struct vring {
  45. unsigned int num;
  46. struct vring_desc *desc;
  47. struct vring_avail *avail;
  48. struct vring_used *used;
  49. };
  50. static inline void vring_init(struct vring *vr,
  51. unsigned int num, unsigned char *queue)
  52. {
  53. unsigned int i;
  54. unsigned long pa;
  55. vr->num = num;
  56. /* physical address of desc must be page aligned */
  57. pa = virt_to_phys(queue);
  58. pa = (pa + PAGE_MASK) & ~PAGE_MASK;
  59. vr->desc = phys_to_virt(pa);
  60. vr->avail = (struct vring_avail *)&vr->desc[num];
  61. /* physical address of used must be page aligned */
  62. pa = virt_to_phys(&vr->avail->ring[num]);
  63. pa = (pa + PAGE_MASK) & ~PAGE_MASK;
  64. vr->used = phys_to_virt(pa);
  65. for (i = 0; i < num - 1; i++)
  66. vr->desc[i].next = i + 1;
  67. vr->desc[i].next = 0;
  68. }
  69. #define vring_size(num) \
  70. (((((sizeof(struct vring_desc) * num) + \
  71. (sizeof(struct vring_avail) + sizeof(u16) * num)) \
  72. + PAGE_MASK) & ~PAGE_MASK) + \
  73. (sizeof(struct vring_used) + sizeof(struct vring_used_elem) * num))
  74. #endif /* _VIRTIO_RING_H_ */