blob: 71e03722fb5946fa60ab67a88a7250bee3c77b7c [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
Rusty Russell426e3e02008-02-04 23:49:59 -050018/* The Host uses this in used->flags to advise the Guest: don't kick me when
19 * you add a buffer. It's unreliable, so it's simply an optimization. Guest
20 * will still kick if it's out of buffers. */
Rusty Russell0a8a69d2007-10-22 11:03:40 +100021#define VRING_USED_F_NO_NOTIFY 1
Rusty Russell426e3e02008-02-04 23:49:59 -050022/* The Guest uses this in avail->flags to advise the Host: don't interrupt me
23 * when you consume a buffer. It's unreliable, so it's simply an
24 * optimization. */
Rusty Russell0a8a69d2007-10-22 11:03:40 +100025#define VRING_AVAIL_F_NO_INTERRUPT 1
26
27/* Virtio ring descriptors: 16 bytes. These can chain together via "next". */
28struct vring_desc
29{
30 /* Address (guest-physical). */
31 __u64 addr;
32 /* Length. */
33 __u32 len;
34 /* The flags as indicated above. */
35 __u16 flags;
36 /* We chain unused descriptors via this, too */
37 __u16 next;
38};
39
40struct vring_avail
41{
42 __u16 flags;
43 __u16 idx;
44 __u16 ring[];
45};
46
47/* u32 is used here for ids for padding reasons. */
48struct vring_used_elem
49{
50 /* Index of start of used descriptor chain. */
51 __u32 id;
52 /* Total length of the descriptor chain which was used (written to) */
53 __u32 len;
54};
55
56struct vring_used
57{
58 __u16 flags;
59 __u16 idx;
60 struct vring_used_elem ring[];
61};
62
63struct vring {
64 unsigned int num;
65
66 struct vring_desc *desc;
67
68 struct vring_avail *avail;
69
70 struct vring_used *used;
71};
72
73/* The standard layout for the ring is a continuous chunk of memory which looks
Rusty Russell42b36cc2007-11-12 13:39:18 +110074 * like this. We assume num is a power of 2.
Rusty Russell0a8a69d2007-10-22 11:03:40 +100075 *
76 * struct vring
77 * {
78 * // The actual descriptors (16 bytes each)
79 * struct vring_desc desc[num];
80 *
81 * // A ring of available descriptor heads with free-running index.
82 * __u16 avail_flags;
83 * __u16 avail_idx;
84 * __u16 available[num];
85 *
Rusty Russell5f0d1d72008-12-30 09:25:57 -060086 * // Padding to the next align boundary.
Rusty Russell42b36cc2007-11-12 13:39:18 +110087 * char pad[];
Rusty Russell0a8a69d2007-10-22 11:03:40 +100088 *
89 * // A ring of used descriptor heads with free-running index.
90 * __u16 used_flags;
91 * __u16 used_idx;
92 * struct vring_used_elem used[num];
93 * };
94 */
Rusty Russell42b36cc2007-11-12 13:39:18 +110095static inline void vring_init(struct vring *vr, unsigned int num, void *p,
Rusty Russell5f0d1d72008-12-30 09:25:57 -060096 unsigned long align)
Rusty Russell0a8a69d2007-10-22 11:03:40 +100097{
98 vr->num = num;
99 vr->desc = p;
Anthony Liguori44332f72007-11-07 16:31:52 +1100100 vr->avail = p + num*sizeof(struct vring_desc);
Rusty Russell5f0d1d72008-12-30 09:25:57 -0600101 vr->used = (void *)(((unsigned long)&vr->avail->ring[num] + align-1)
102 & ~(align - 1));
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000103}
104
Rusty Russell5f0d1d72008-12-30 09:25:57 -0600105static inline unsigned vring_size(unsigned int num, unsigned long align)
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000106{
Rusty Russell42b36cc2007-11-12 13:39:18 +1100107 return ((sizeof(struct vring_desc) * num + sizeof(__u16) * (2 + num)
Rusty Russell5f0d1d72008-12-30 09:25:57 -0600108 + align - 1) & ~(align - 1))
Rusty Russell42b36cc2007-11-12 13:39:18 +1100109 + sizeof(__u16) * 2 + sizeof(struct vring_used_elem) * num;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000110}
111
112#ifdef __KERNEL__
113#include <linux/irqreturn.h>
114struct virtio_device;
115struct virtqueue;
116
117struct virtqueue *vring_new_virtqueue(unsigned int num,
Rusty Russell87c7d572008-12-30 09:26:03 -0600118 unsigned int vring_align,
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000119 struct virtio_device *vdev,
120 void *pages,
121 void (*notify)(struct virtqueue *vq),
Rusty Russell18445c42008-02-04 23:49:57 -0500122 void (*callback)(struct virtqueue *vq));
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000123void vring_del_virtqueue(struct virtqueue *vq);
Rusty Russelle34f8722008-07-25 12:06:13 -0500124/* Filter out transport-specific feature bits. */
125void vring_transport_features(struct virtio_device *vdev);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000126
127irqreturn_t vring_interrupt(int irq, void *_vq);
128#endif /* __KERNEL__ */
129#endif /* _LINUX_VIRTIO_RING_H */