Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / include / ipxe / virtio-ring.h
1 #ifndef _VIRTIO_RING_H_
2 # define _VIRTIO_RING_H_
3
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
13
14 #define MAX_QUEUE_NUM      (256)
15
16 #define VRING_DESC_F_NEXT  1
17 #define VRING_DESC_F_WRITE 2
18
19 #define VRING_AVAIL_F_NO_INTERRUPT 1
20
21 #define VRING_USED_F_NO_NOTIFY     1
22
23 struct vring_desc
24 {
25    u64 addr;
26    u32 len;
27    u16 flags;
28    u16 next;
29 };
30
31 struct vring_avail
32 {
33    u16 flags;
34    u16 idx;
35    u16 ring[0];
36 };
37
38 struct vring_used_elem
39 {
40    u32 id;
41    u32 len;
42 };
43
44 struct vring_used
45 {
46    u16 flags;
47    u16 idx;
48    struct vring_used_elem ring[];
49 };
50
51 struct vring {
52    unsigned int num;
53    struct vring_desc *desc;
54    struct vring_avail *avail;
55    struct vring_used *used;
56 };
57
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))
63
64 typedef unsigned char virtio_queue_t[PAGE_MASK + vring_size(MAX_QUEUE_NUM)];
65
66 struct vring_virtqueue {
67    virtio_queue_t queue;
68    struct vring vring;
69    u16 free_head;
70    u16 last_used_idx;
71    void *vdata[MAX_QUEUE_NUM];
72    /* PCI */
73    int queue_index;
74 };
75
76 struct vring_list {
77   char *addr;
78   unsigned int length;
79 };
80
81 static inline void vring_init(struct vring *vr,
82                          unsigned int num, unsigned char *queue)
83 {
84    unsigned int i;
85    unsigned long pa;
86
87         vr->num = num;
88
89    /* physical address of desc must be page aligned */
90
91    pa = virt_to_phys(queue);
92    pa = (pa + PAGE_MASK) & ~PAGE_MASK;
93    vr->desc = phys_to_virt(pa);
94
95         vr->avail = (struct vring_avail *)&vr->desc[num];
96
97    /* physical address of used must be page aligned */
98
99    pa = virt_to_phys(&vr->avail->ring[num]);
100    pa = (pa + PAGE_MASK) & ~PAGE_MASK;
101         vr->used = phys_to_virt(pa);
102
103    for (i = 0; i < num - 1; i++)
104            vr->desc[i].next = i + 1;
105    vr->desc[i].next = 0;
106 }
107
108 static inline void vring_enable_cb(struct vring_virtqueue *vq)
109 {
110    vq->vring.avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT;
111 }
112
113 static inline void vring_disable_cb(struct vring_virtqueue *vq)
114 {
115    vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
116 }
117
118
119 /*
120  * vring_more_used
121  *
122  * is there some used buffers ?
123  *
124  */
125
126 static inline int vring_more_used(struct vring_virtqueue *vq)
127 {
128    wmb();
129    return vq->last_used_idx != vq->vring.used->idx;
130 }
131
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);
138
139 #endif /* _VIRTIO_RING_H_ */