virtio_ring.h revision 228301
1228301Sgrehan/*-
2228301Sgrehan * Copyright Rusty Russell IBM Corporation 2007.
3227652Sgrehan *
4228301Sgrehan * This header is BSD licensed so anyone can use the definitions to implement
5228301Sgrehan * compatible drivers/servers.
6228301Sgrehan *
7228301Sgrehan * Redistribution and use in source and binary forms, with or without
8228301Sgrehan * modification, are permitted provided that the following conditions
9228301Sgrehan * are met:
10228301Sgrehan * 1. Redistributions of source code must retain the above copyright
11228301Sgrehan *    notice, this list of conditions and the following disclaimer.
12228301Sgrehan * 2. Redistributions in binary form must reproduce the above copyright
13228301Sgrehan *    notice, this list of conditions and the following disclaimer in the
14228301Sgrehan *    documentation and/or other materials provided with the distribution.
15228301Sgrehan * 3. Neither the name of IBM nor the names of its contributors
16228301Sgrehan *    may be used to endorse or promote products derived from this software
17228301Sgrehan *    without specific prior written permission.
18228301Sgrehan * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19228301Sgrehan * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20228301Sgrehan * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21228301Sgrehan * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL IBM OR CONTRIBUTORS BE LIABLE
22228301Sgrehan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23228301Sgrehan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24228301Sgrehan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25228301Sgrehan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26228301Sgrehan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27228301Sgrehan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28228301Sgrehan * SUCH DAMAGE.
29228301Sgrehan *
30228301Sgrehan * $FreeBSD: head/sys/dev/virtio/virtio_ring.h 228301 2011-12-06 06:28:32Z grehan $
31227652Sgrehan */
32227652Sgrehan
33227652Sgrehan#ifndef VIRTIO_RING_H
34227652Sgrehan#define	VIRTIO_RING_H
35227652Sgrehan
36227652Sgrehan#include <sys/types.h>
37227652Sgrehan
38227652Sgrehan/* This marks a buffer as continuing via the next field. */
39227652Sgrehan#define VRING_DESC_F_NEXT       1
40227652Sgrehan/* This marks a buffer as write-only (otherwise read-only). */
41227652Sgrehan#define VRING_DESC_F_WRITE      2
42227652Sgrehan/* This means the buffer contains a list of buffer descriptors. */
43227652Sgrehan#define VRING_DESC_F_INDIRECT	4
44227652Sgrehan
45227652Sgrehan/* The Host uses this in used->flags to advise the Guest: don't kick me
46227652Sgrehan * when you add a buffer.  It's unreliable, so it's simply an
47227652Sgrehan * optimization.  Guest will still kick if it's out of buffers. */
48227652Sgrehan#define VRING_USED_F_NO_NOTIFY  1
49227652Sgrehan/* The Guest uses this in avail->flags to advise the Host: don't
50227652Sgrehan * interrupt me when you consume a buffer.  It's unreliable, so it's
51227652Sgrehan * simply an optimization.  */
52227652Sgrehan#define VRING_AVAIL_F_NO_INTERRUPT      1
53227652Sgrehan
54227652Sgrehan/* VirtIO ring descriptors: 16 bytes.
55227652Sgrehan * These can chain together via "next". */
56227652Sgrehanstruct vring_desc {
57227652Sgrehan        /* Address (guest-physical). */
58227652Sgrehan        uint64_t addr;
59227652Sgrehan        /* Length. */
60227652Sgrehan        uint32_t len;
61227652Sgrehan        /* The flags as indicated above. */
62227652Sgrehan        uint16_t flags;
63227652Sgrehan        /* We chain unused descriptors via this, too. */
64227652Sgrehan        uint16_t next;
65227652Sgrehan};
66227652Sgrehan
67227652Sgrehanstruct vring_avail {
68227652Sgrehan        uint16_t flags;
69227652Sgrehan        uint16_t idx;
70227652Sgrehan        uint16_t ring[0];
71227652Sgrehan};
72227652Sgrehan
73227652Sgrehan/* uint32_t is used here for ids for padding reasons. */
74227652Sgrehanstruct vring_used_elem {
75227652Sgrehan        /* Index of start of used descriptor chain. */
76227652Sgrehan        uint32_t id;
77227652Sgrehan        /* Total length of the descriptor chain which was written to. */
78227652Sgrehan        uint32_t len;
79227652Sgrehan};
80227652Sgrehan
81227652Sgrehanstruct vring_used {
82227652Sgrehan        uint16_t flags;
83227652Sgrehan        uint16_t idx;
84227652Sgrehan        struct vring_used_elem ring[0];
85227652Sgrehan};
86227652Sgrehan
87227652Sgrehanstruct vring {
88227652Sgrehan	unsigned int num;
89227652Sgrehan
90227652Sgrehan	struct vring_desc *desc;
91227652Sgrehan	struct vring_avail *avail;
92227652Sgrehan	struct vring_used *used;
93227652Sgrehan};
94227652Sgrehan
95227652Sgrehan/* The standard layout for the ring is a continuous chunk of memory which
96227652Sgrehan * looks like this.  We assume num is a power of 2.
97227652Sgrehan *
98227652Sgrehan * struct vring {
99227652Sgrehan *      // The actual descriptors (16 bytes each)
100227652Sgrehan *      struct vring_desc desc[num];
101227652Sgrehan *
102227652Sgrehan *      // A ring of available descriptor heads with free-running index.
103227652Sgrehan *      __u16 avail_flags;
104227652Sgrehan *      __u16 avail_idx;
105227652Sgrehan *      __u16 available[num];
106227652Sgrehan *
107227652Sgrehan *      // Padding to the next align boundary.
108227652Sgrehan *      char pad[];
109227652Sgrehan *
110227652Sgrehan *      // A ring of used descriptor heads with free-running index.
111227652Sgrehan *      __u16 used_flags;
112227652Sgrehan *      __u16 used_idx;
113227652Sgrehan *      struct vring_used_elem used[num];
114227652Sgrehan * };
115227652Sgrehan *
116227652Sgrehan * NOTE: for VirtIO PCI, align is 4096.
117227652Sgrehan */
118227652Sgrehan
119227652Sgrehanstatic inline int
120227652Sgrehanvring_size(unsigned int num, unsigned long align)
121227652Sgrehan{
122227652Sgrehan	int size;
123227652Sgrehan
124227652Sgrehan	size = num * sizeof(struct vring_desc);
125227652Sgrehan	size += sizeof(struct vring_avail) + (num * sizeof(uint16_t));
126227652Sgrehan	size = (size + align - 1) & ~(align - 1);
127227652Sgrehan	size += sizeof(struct vring_used) +
128227652Sgrehan	    (num * sizeof(struct vring_used_elem));
129227652Sgrehan	return (size);
130227652Sgrehan}
131227652Sgrehan
132227652Sgrehanstatic inline void
133227652Sgrehanvring_init(struct vring *vr, unsigned int num, uint8_t *p,
134227652Sgrehan    unsigned long align)
135227652Sgrehan{
136227652Sgrehan        vr->num = num;
137227652Sgrehan        vr->desc = (struct vring_desc *) p;
138227652Sgrehan        vr->avail = (struct vring_avail *) (p +
139227652Sgrehan	    num * sizeof(struct vring_desc));
140227652Sgrehan        vr->used = (void *)
141227652Sgrehan	    (((unsigned long) &vr->avail->ring[num] + align-1) & ~(align-1));
142227652Sgrehan}
143227652Sgrehan#endif /* VIRTIO_RING_H */
144