4 #include "types.h" // u64
5 #include "memmap.h" // PAGE_SIZE
7 /* Status byte for guest to report progress, and synchronize features. */
8 /* We have seen device and processed generic fields (VIRTIO_CONFIG_F_VIRTIO) */
9 #define VIRTIO_CONFIG_S_ACKNOWLEDGE 1
10 /* We have found a driver for the device. */
11 #define VIRTIO_CONFIG_S_DRIVER 2
12 /* Driver has used its parts of the config, and is happy */
13 #define VIRTIO_CONFIG_S_DRIVER_OK 4
14 /* Driver has finished configuring features */
15 #define VIRTIO_CONFIG_S_FEATURES_OK 8
16 /* We've given up on this device. */
17 #define VIRTIO_CONFIG_S_FAILED 0x80
20 #define VIRTIO_F_VERSION_1 32
22 #define MAX_QUEUE_NUM (128)
24 #define VRING_DESC_F_NEXT 1
25 #define VRING_DESC_F_WRITE 2
27 #define VRING_AVAIL_F_NO_INTERRUPT 1
29 #define VRING_USED_F_NO_NOTIFY 1
46 struct vring_used_elem
56 struct vring_used_elem ring[];
61 struct vring_desc *desc;
62 struct vring_avail *avail;
63 struct vring_used *used;
66 #define vring_size(num) \
67 (ALIGN(sizeof(struct vring_desc) * num + sizeof(struct vring_avail) \
68 + sizeof(u16) * num, PAGE_SIZE) \
69 + sizeof(struct vring_used) + sizeof(struct vring_used_elem) * num)
71 typedef unsigned char virtio_queue_t[vring_size(MAX_QUEUE_NUM)];
73 struct vring_virtqueue {
78 u16 vdata[MAX_QUEUE_NUM];
90 vring_init(struct vring *vr, unsigned int num, unsigned char *queue)
95 /* physical address of desc must be page aligned */
96 vr->desc = (void*)ALIGN((u32)queue, PAGE_SIZE);
98 vr->avail = (struct vring_avail *)&vr->desc[num];
99 /* disable interrupts */
100 vr->avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
102 /* physical address of used must be page aligned */
103 vr->used = (void*)ALIGN((u32)&vr->avail->ring[num], PAGE_SIZE);
106 for (i = 0; i < num - 1; i++)
107 vr->desc[i].next = i + 1;
108 vr->desc[i].next = 0;
112 int vring_more_used(struct vring_virtqueue *vq);
113 void vring_detach(struct vring_virtqueue *vq, unsigned int head);
114 int vring_get_buf(struct vring_virtqueue *vq, unsigned int *len);
115 void vring_add_buf(struct vring_virtqueue *vq, struct vring_list list[],
116 unsigned int out, unsigned int in,
117 int index, int num_added);
118 void vring_kick(struct vp_device *vp, struct vring_virtqueue *vq, int num_added);
120 #endif /* _VIRTIO_RING_H_ */