virtio_ring.h revision 228301
1/*-
2 * Copyright Rusty Russell IBM Corporation 2007.
3 *
4 * This header is BSD licensed so anyone can use the definitions to implement
5 * compatible drivers/servers.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of IBM nor the names of its contributors
16 *    may be used to endorse or promote products derived from this software
17 *    without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL IBM OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 *
30 * $FreeBSD: head/sys/dev/virtio/virtio_ring.h 228301 2011-12-06 06:28:32Z grehan $
31 */
32
33#ifndef VIRTIO_RING_H
34#define	VIRTIO_RING_H
35
36#include <sys/types.h>
37
38/* This marks a buffer as continuing via the next field. */
39#define VRING_DESC_F_NEXT       1
40/* This marks a buffer as write-only (otherwise read-only). */
41#define VRING_DESC_F_WRITE      2
42/* This means the buffer contains a list of buffer descriptors. */
43#define VRING_DESC_F_INDIRECT	4
44
45/* The Host uses this in used->flags to advise the Guest: don't kick me
46 * when you add a buffer.  It's unreliable, so it's simply an
47 * optimization.  Guest will still kick if it's out of buffers. */
48#define VRING_USED_F_NO_NOTIFY  1
49/* The Guest uses this in avail->flags to advise the Host: don't
50 * interrupt me when you consume a buffer.  It's unreliable, so it's
51 * simply an optimization.  */
52#define VRING_AVAIL_F_NO_INTERRUPT      1
53
54/* VirtIO ring descriptors: 16 bytes.
55 * These can chain together via "next". */
56struct vring_desc {
57        /* Address (guest-physical). */
58        uint64_t addr;
59        /* Length. */
60        uint32_t len;
61        /* The flags as indicated above. */
62        uint16_t flags;
63        /* We chain unused descriptors via this, too. */
64        uint16_t next;
65};
66
67struct vring_avail {
68        uint16_t flags;
69        uint16_t idx;
70        uint16_t ring[0];
71};
72
73/* uint32_t is used here for ids for padding reasons. */
74struct vring_used_elem {
75        /* Index of start of used descriptor chain. */
76        uint32_t id;
77        /* Total length of the descriptor chain which was written to. */
78        uint32_t len;
79};
80
81struct vring_used {
82        uint16_t flags;
83        uint16_t idx;
84        struct vring_used_elem ring[0];
85};
86
87struct vring {
88	unsigned int num;
89
90	struct vring_desc *desc;
91	struct vring_avail *avail;
92	struct vring_used *used;
93};
94
95/* The standard layout for the ring is a continuous chunk of memory which
96 * looks like this.  We assume num is a power of 2.
97 *
98 * struct vring {
99 *      // The actual descriptors (16 bytes each)
100 *      struct vring_desc desc[num];
101 *
102 *      // A ring of available descriptor heads with free-running index.
103 *      __u16 avail_flags;
104 *      __u16 avail_idx;
105 *      __u16 available[num];
106 *
107 *      // Padding to the next align boundary.
108 *      char pad[];
109 *
110 *      // A ring of used descriptor heads with free-running index.
111 *      __u16 used_flags;
112 *      __u16 used_idx;
113 *      struct vring_used_elem used[num];
114 * };
115 *
116 * NOTE: for VirtIO PCI, align is 4096.
117 */
118
119static inline int
120vring_size(unsigned int num, unsigned long align)
121{
122	int size;
123
124	size = num * sizeof(struct vring_desc);
125	size += sizeof(struct vring_avail) + (num * sizeof(uint16_t));
126	size = (size + align - 1) & ~(align - 1);
127	size += sizeof(struct vring_used) +
128	    (num * sizeof(struct vring_used_elem));
129	return (size);
130}
131
132static inline void
133vring_init(struct vring *vr, unsigned int num, uint8_t *p,
134    unsigned long align)
135{
136        vr->num = num;
137        vr->desc = (struct vring_desc *) p;
138        vr->avail = (struct vring_avail *) (p +
139	    num * sizeof(struct vring_desc));
140        vr->used = (void *)
141	    (((unsigned long) &vr->avail->ring[num] + align-1) & ~(align-1));
142}
143#endif /* VIRTIO_RING_H */
144