blob: e4d144b132b5e27d3c4713738acfc63ce6262781 [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
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +010017/* This means the buffer contains a list of buffer descriptors. */
18#define VRING_DESC_F_INDIRECT 4
Rusty Russell0a8a69d2007-10-22 11:03:40 +100019
Rusty Russell426e3e02008-02-04 23:49:59 -050020/* The Host uses this in used->flags to advise the Guest: don't kick me when
21 * you add a buffer. It's unreliable, so it's simply an optimization. Guest
22 * will still kick if it's out of buffers. */
Rusty Russell0a8a69d2007-10-22 11:03:40 +100023#define VRING_USED_F_NO_NOTIFY 1
Rusty Russell426e3e02008-02-04 23:49:59 -050024/* The Guest uses this in avail->flags to advise the Host: don't interrupt me
25 * when you consume a buffer. It's unreliable, so it's simply an
26 * optimization. */
Rusty Russell0a8a69d2007-10-22 11:03:40 +100027#define VRING_AVAIL_F_NO_INTERRUPT 1
28
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +010029/* We support indirect buffer descriptors */
30#define VIRTIO_RING_F_INDIRECT_DESC 28
31
Rusty Russell0a8a69d2007-10-22 11:03:40 +100032/* Virtio ring descriptors: 16 bytes. These can chain together via "next". */
Rusty Russell1842f232009-07-30 16:03:46 -060033struct vring_desc {
Rusty Russell0a8a69d2007-10-22 11:03:40 +100034 /* Address (guest-physical). */
35 __u64 addr;
36 /* Length. */
37 __u32 len;
38 /* The flags as indicated above. */
39 __u16 flags;
40 /* We chain unused descriptors via this, too */
41 __u16 next;
42};
43
Rusty Russell1842f232009-07-30 16:03:46 -060044struct vring_avail {
Rusty Russell0a8a69d2007-10-22 11:03:40 +100045 __u16 flags;
46 __u16 idx;
47 __u16 ring[];
48};
49
50/* u32 is used here for ids for padding reasons. */
Rusty Russell1842f232009-07-30 16:03:46 -060051struct vring_used_elem {
Rusty Russell0a8a69d2007-10-22 11:03:40 +100052 /* Index of start of used descriptor chain. */
53 __u32 id;
54 /* Total length of the descriptor chain which was used (written to) */
55 __u32 len;
56};
57
Rusty Russell1842f232009-07-30 16:03:46 -060058struct vring_used {
Rusty Russell0a8a69d2007-10-22 11:03:40 +100059 __u16 flags;
60 __u16 idx;
61 struct vring_used_elem ring[];
62};
63
64struct vring {
65 unsigned int num;
66
67 struct vring_desc *desc;
68
69 struct vring_avail *avail;
70
71 struct vring_used *used;
72};
73
74/* The standard layout for the ring is a continuous chunk of memory which looks
Rusty Russell42b36cc2007-11-12 13:39:18 +110075 * like this. We assume num is a power of 2.
Rusty Russell0a8a69d2007-10-22 11:03:40 +100076 *
77 * struct vring
78 * {
79 * // The actual descriptors (16 bytes each)
80 * struct vring_desc desc[num];
81 *
82 * // A ring of available descriptor heads with free-running index.
83 * __u16 avail_flags;
84 * __u16 avail_idx;
85 * __u16 available[num];
86 *
Rusty Russell5f0d1d72008-12-30 09:25:57 -060087 * // Padding to the next align boundary.
Rusty Russell42b36cc2007-11-12 13:39:18 +110088 * char pad[];
Rusty Russell0a8a69d2007-10-22 11:03:40 +100089 *
90 * // A ring of used descriptor heads with free-running index.
91 * __u16 used_flags;
92 * __u16 used_idx;
93 * struct vring_used_elem used[num];
94 * };
95 */
Rusty Russell42b36cc2007-11-12 13:39:18 +110096static inline void vring_init(struct vring *vr, unsigned int num, void *p,
Rusty Russell5f0d1d72008-12-30 09:25:57 -060097 unsigned long align)
Rusty Russell0a8a69d2007-10-22 11:03:40 +100098{
99 vr->num = num;
100 vr->desc = p;
Anthony Liguori44332f72007-11-07 16:31:52 +1100101 vr->avail = p + num*sizeof(struct vring_desc);
Rusty Russell5f0d1d72008-12-30 09:25:57 -0600102 vr->used = (void *)(((unsigned long)&vr->avail->ring[num] + align-1)
103 & ~(align - 1));
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000104}
105
Rusty Russell5f0d1d72008-12-30 09:25:57 -0600106static inline unsigned vring_size(unsigned int num, unsigned long align)
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000107{
Rusty Russell42b36cc2007-11-12 13:39:18 +1100108 return ((sizeof(struct vring_desc) * num + sizeof(__u16) * (2 + num)
Rusty Russell5f0d1d72008-12-30 09:25:57 -0600109 + align - 1) & ~(align - 1))
Rusty Russell42b36cc2007-11-12 13:39:18 +1100110 + sizeof(__u16) * 2 + sizeof(struct vring_used_elem) * num;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000111}
112
113#ifdef __KERNEL__
114#include <linux/irqreturn.h>
115struct virtio_device;
116struct virtqueue;
117
118struct virtqueue *vring_new_virtqueue(unsigned int num,
Rusty Russell87c7d572008-12-30 09:26:03 -0600119 unsigned int vring_align,
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000120 struct virtio_device *vdev,
121 void *pages,
122 void (*notify)(struct virtqueue *vq),
Rusty Russell9499f5e2009-06-12 22:16:35 -0600123 void (*callback)(struct virtqueue *vq),
124 const char *name);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000125void vring_del_virtqueue(struct virtqueue *vq);
Rusty Russelle34f8722008-07-25 12:06:13 -0500126/* Filter out transport-specific feature bits. */
127void vring_transport_features(struct virtio_device *vdev);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000128
129irqreturn_t vring_interrupt(int irq, void *_vq);
130#endif /* __KERNEL__ */
131#endif /* _LINUX_VIRTIO_RING_H */