blob: 1a4ed49f64780cc553a1ac3f6dcca0f2065972ff [file] [log] [blame]
Rusty Russell0a8a69d2007-10-22 11:03:40 +10001#ifndef _LINUX_VIRTIO_RING_H
2#define _LINUX_VIRTIO_RING_H
3/* An interface for efficient virtio implementation, currently for use by KVM
4 * and lguest, but hopefully others soon. Do NOT change this since it will
5 * break existing servers and clients.
6 *
7 * This header is BSD licensed so anyone can use the definitions to implement
8 * compatible drivers/servers.
9 *
10 * Copyright Rusty Russell IBM Corporation 2007. */
11#include <linux/types.h>
12
13/* This marks a buffer as continuing via the next field. */
14#define VRING_DESC_F_NEXT 1
15/* This marks a buffer as write-only (otherwise read-only). */
16#define VRING_DESC_F_WRITE 2
17
18/* This means don't notify other side when buffer added. */
19#define VRING_USED_F_NO_NOTIFY 1
20/* This means don't interrupt guest when buffer consumed. */
21#define VRING_AVAIL_F_NO_INTERRUPT 1
22
23/* Virtio ring descriptors: 16 bytes. These can chain together via "next". */
24struct vring_desc
25{
26 /* Address (guest-physical). */
27 __u64 addr;
28 /* Length. */
29 __u32 len;
30 /* The flags as indicated above. */
31 __u16 flags;
32 /* We chain unused descriptors via this, too */
33 __u16 next;
34};
35
36struct vring_avail
37{
38 __u16 flags;
39 __u16 idx;
40 __u16 ring[];
41};
42
43/* u32 is used here for ids for padding reasons. */
44struct vring_used_elem
45{
46 /* Index of start of used descriptor chain. */
47 __u32 id;
48 /* Total length of the descriptor chain which was used (written to) */
49 __u32 len;
50};
51
52struct vring_used
53{
54 __u16 flags;
55 __u16 idx;
56 struct vring_used_elem ring[];
57};
58
59struct vring {
60 unsigned int num;
61
62 struct vring_desc *desc;
63
64 struct vring_avail *avail;
65
66 struct vring_used *used;
67};
68
69/* The standard layout for the ring is a continuous chunk of memory which looks
Rusty Russell42b36cc2007-11-12 13:39:18 +110070 * like this. We assume num is a power of 2.
Rusty Russell0a8a69d2007-10-22 11:03:40 +100071 *
72 * struct vring
73 * {
74 * // The actual descriptors (16 bytes each)
75 * struct vring_desc desc[num];
76 *
77 * // A ring of available descriptor heads with free-running index.
78 * __u16 avail_flags;
79 * __u16 avail_idx;
80 * __u16 available[num];
81 *
Rusty Russell42b36cc2007-11-12 13:39:18 +110082 * // Padding to the next page boundary.
83 * char pad[];
Rusty Russell0a8a69d2007-10-22 11:03:40 +100084 *
85 * // A ring of used descriptor heads with free-running index.
86 * __u16 used_flags;
87 * __u16 used_idx;
88 * struct vring_used_elem used[num];
89 * };
90 */
Rusty Russell42b36cc2007-11-12 13:39:18 +110091static inline void vring_init(struct vring *vr, unsigned int num, void *p,
92 unsigned int pagesize)
Rusty Russell0a8a69d2007-10-22 11:03:40 +100093{
94 vr->num = num;
95 vr->desc = p;
Anthony Liguori44332f72007-11-07 16:31:52 +110096 vr->avail = p + num*sizeof(struct vring_desc);
Rusty Russell42b36cc2007-11-12 13:39:18 +110097 vr->used = (void *)(((unsigned long)&vr->avail->ring[num] + pagesize-1)
98 & ~(pagesize - 1));
Rusty Russell0a8a69d2007-10-22 11:03:40 +100099}
100
Rusty Russell42b36cc2007-11-12 13:39:18 +1100101static inline unsigned vring_size(unsigned int num, unsigned int pagesize)
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000102{
Rusty Russell42b36cc2007-11-12 13:39:18 +1100103 return ((sizeof(struct vring_desc) * num + sizeof(__u16) * (2 + num)
104 + pagesize - 1) & ~(pagesize - 1))
105 + sizeof(__u16) * 2 + sizeof(struct vring_used_elem) * num;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000106}
107
108#ifdef __KERNEL__
109#include <linux/irqreturn.h>
110struct virtio_device;
111struct virtqueue;
112
113struct virtqueue *vring_new_virtqueue(unsigned int num,
114 struct virtio_device *vdev,
115 void *pages,
116 void (*notify)(struct virtqueue *vq),
117 bool (*callback)(struct virtqueue *vq));
118void vring_del_virtqueue(struct virtqueue *vq);
119
120irqreturn_t vring_interrupt(int irq, void *_vq);
121#endif /* __KERNEL__ */
122#endif /* _LINUX_VIRTIO_RING_H */