1 #ifndef _VIRTIO_RING_H_
2 # define _VIRTIO_RING_H_
4 /* Status byte for guest to report progress, and synchronize features. */
5 /* We have seen device and processed generic fields (VIRTIO_CONFIG_F_VIRTIO) */
6 #define VIRTIO_CONFIG_S_ACKNOWLEDGE 1
7 /* We have found a driver for the device. */
8 #define VIRTIO_CONFIG_S_DRIVER 2
9 /* Driver has used its parts of the config, and is happy */
10 #define VIRTIO_CONFIG_S_DRIVER_OK 4
11 /* We've given up on this device. */
12 #define VIRTIO_CONFIG_S_FAILED 0x80
14 #define MAX_QUEUE_NUM (256)
16 #define VRING_DESC_F_NEXT 1
17 #define VRING_DESC_F_WRITE 2
19 #define VRING_AVAIL_F_NO_INTERRUPT 1
21 #define VRING_USED_F_NO_NOTIFY 1
38 struct vring_used_elem
48 struct vring_used_elem ring[];
53 struct vring_desc *desc;
54 struct vring_avail *avail;
55 struct vring_used *used;
58 #define vring_size(num) \
59 (((((sizeof(struct vring_desc) * num) + \
60 (sizeof(struct vring_avail) + sizeof(u16) * num)) \
61 + PAGE_MASK) & ~PAGE_MASK) + \
62 (sizeof(struct vring_used) + sizeof(struct vring_used_elem) * num))
64 typedef unsigned char virtio_queue_t[PAGE_MASK + vring_size(MAX_QUEUE_NUM)];
66 struct vring_virtqueue {
71 void *vdata[MAX_QUEUE_NUM];
81 static inline void vring_init(struct vring *vr,
82 unsigned int num, unsigned char *queue)
89 /* physical address of desc must be page aligned */
91 pa = virt_to_phys(queue);
92 pa = (pa + PAGE_MASK) & ~PAGE_MASK;
93 vr->desc = phys_to_virt(pa);
95 vr->avail = (struct vring_avail *)&vr->desc[num];
97 /* physical address of used must be page aligned */
99 pa = virt_to_phys(&vr->avail->ring[num]);
100 pa = (pa + PAGE_MASK) & ~PAGE_MASK;
101 vr->used = phys_to_virt(pa);
103 for (i = 0; i < num - 1; i++)
104 vr->desc[i].next = i + 1;
105 vr->desc[i].next = 0;
108 static inline void vring_enable_cb(struct vring_virtqueue *vq)
110 vq->vring.avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT;
113 static inline void vring_disable_cb(struct vring_virtqueue *vq)
115 vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
122 * is there some used buffers ?
126 static inline int vring_more_used(struct vring_virtqueue *vq)
129 return vq->last_used_idx != vq->vring.used->idx;
132 void vring_detach(struct vring_virtqueue *vq, unsigned int head);
133 void *vring_get_buf(struct vring_virtqueue *vq, unsigned int *len);
134 void vring_add_buf(struct vring_virtqueue *vq, struct vring_list list[],
135 unsigned int out, unsigned int in,
136 void *index, int num_added);
137 void vring_kick(unsigned int ioaddr, struct vring_virtqueue *vq, int num_added);
139 #endif /* _VIRTIO_RING_H_ */