1239472Semaste/*-
2239472Semaste * Copyright Rusty Russell IBM Corporation 2007.
3227652Sgrehan *
4239472Semaste * This header is BSD licensed so anyone can use the definitions to implement
5239472Semaste * compatible drivers/servers.
6239472Semaste *
7239472Semaste * Redistribution and use in source and binary forms, with or without
8239472Semaste * modification, are permitted provided that the following conditions
9239472Semaste * are met:
10239472Semaste * 1. Redistributions of source code must retain the above copyright
11239472Semaste *    notice, this list of conditions and the following disclaimer.
12239472Semaste * 2. Redistributions in binary form must reproduce the above copyright
13239472Semaste *    notice, this list of conditions and the following disclaimer in the
14239472Semaste *    documentation and/or other materials provided with the distribution.
15239472Semaste * 3. Neither the name of IBM nor the names of its contributors
16239472Semaste *    may be used to endorse or promote products derived from this software
17239472Semaste *    without specific prior written permission.
18239472Semaste * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19239472Semaste * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20239472Semaste * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21239472Semaste * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL IBM OR CONTRIBUTORS BE LIABLE
22239472Semaste * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23239472Semaste * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24239472Semaste * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25239472Semaste * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26239472Semaste * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27239472Semaste * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28239472Semaste * SUCH DAMAGE.
29239472Semaste *
30239472Semaste * $FreeBSD$
31227652Sgrehan */
32227652Sgrehan
33227652Sgrehan#ifndef VIRTIO_RING_H
34227652Sgrehan#define	VIRTIO_RING_H
35227652Sgrehan
36227652Sgrehan/* This marks a buffer as continuing via the next field. */
37227652Sgrehan#define VRING_DESC_F_NEXT       1
38227652Sgrehan/* This marks a buffer as write-only (otherwise read-only). */
39227652Sgrehan#define VRING_DESC_F_WRITE      2
40227652Sgrehan/* This means the buffer contains a list of buffer descriptors. */
41227652Sgrehan#define VRING_DESC_F_INDIRECT	4
42227652Sgrehan
43227652Sgrehan/* The Host uses this in used->flags to advise the Guest: don't kick me
44227652Sgrehan * when you add a buffer.  It's unreliable, so it's simply an
45227652Sgrehan * optimization.  Guest will still kick if it's out of buffers. */
46227652Sgrehan#define VRING_USED_F_NO_NOTIFY  1
47227652Sgrehan/* The Guest uses this in avail->flags to advise the Host: don't
48227652Sgrehan * interrupt me when you consume a buffer.  It's unreliable, so it's
49227652Sgrehan * simply an optimization.  */
50227652Sgrehan#define VRING_AVAIL_F_NO_INTERRUPT      1
51227652Sgrehan
52227652Sgrehan/* VirtIO ring descriptors: 16 bytes.
53227652Sgrehan * These can chain together via "next". */
54227652Sgrehanstruct vring_desc {
55227652Sgrehan        /* Address (guest-physical). */
56227652Sgrehan        uint64_t addr;
57227652Sgrehan        /* Length. */
58227652Sgrehan        uint32_t len;
59227652Sgrehan        /* The flags as indicated above. */
60227652Sgrehan        uint16_t flags;
61227652Sgrehan        /* We chain unused descriptors via this, too. */
62227652Sgrehan        uint16_t next;
63227652Sgrehan};
64227652Sgrehan
65227652Sgrehanstruct vring_avail {
66227652Sgrehan        uint16_t flags;
67227652Sgrehan        uint16_t idx;
68227652Sgrehan        uint16_t ring[0];
69227652Sgrehan};
70227652Sgrehan
71227652Sgrehan/* uint32_t is used here for ids for padding reasons. */
72227652Sgrehanstruct vring_used_elem {
73227652Sgrehan        /* Index of start of used descriptor chain. */
74227652Sgrehan        uint32_t id;
75227652Sgrehan        /* Total length of the descriptor chain which was written to. */
76227652Sgrehan        uint32_t len;
77227652Sgrehan};
78227652Sgrehan
79227652Sgrehanstruct vring_used {
80227652Sgrehan        uint16_t flags;
81227652Sgrehan        uint16_t idx;
82227652Sgrehan        struct vring_used_elem ring[0];
83227652Sgrehan};
84227652Sgrehan
85227652Sgrehanstruct vring {
86227652Sgrehan	unsigned int num;
87227652Sgrehan
88227652Sgrehan	struct vring_desc *desc;
89227652Sgrehan	struct vring_avail *avail;
90227652Sgrehan	struct vring_used *used;
91227652Sgrehan};
92227652Sgrehan
93227652Sgrehan/* The standard layout for the ring is a continuous chunk of memory which
94227652Sgrehan * looks like this.  We assume num is a power of 2.
95227652Sgrehan *
96227652Sgrehan * struct vring {
97227652Sgrehan *      // The actual descriptors (16 bytes each)
98227652Sgrehan *      struct vring_desc desc[num];
99227652Sgrehan *
100227652Sgrehan *      // A ring of available descriptor heads with free-running index.
101227652Sgrehan *      __u16 avail_flags;
102227652Sgrehan *      __u16 avail_idx;
103227652Sgrehan *      __u16 available[num];
104239472Semaste *      __u16 used_event_idx;
105227652Sgrehan *
106227652Sgrehan *      // Padding to the next align boundary.
107227652Sgrehan *      char pad[];
108227652Sgrehan *
109227652Sgrehan *      // A ring of used descriptor heads with free-running index.
110227652Sgrehan *      __u16 used_flags;
111227652Sgrehan *      __u16 used_idx;
112227652Sgrehan *      struct vring_used_elem used[num];
113239472Semaste *      __u16 avail_event_idx;
114227652Sgrehan * };
115227652Sgrehan *
116227652Sgrehan * NOTE: for VirtIO PCI, align is 4096.
117227652Sgrehan */
118227652Sgrehan
119239472Semaste/*
120239472Semaste * We publish the used event index at the end of the available ring, and vice
121239472Semaste * versa. They are at the end for backwards compatibility.
122239472Semaste */
123246582Sbryanv#define vring_used_event(vr)	((vr)->avail->ring[(vr)->num])
124246582Sbryanv#define vring_avail_event(vr)	(*(uint16_t *)&(vr)->used->ring[(vr)->num])
125239472Semaste
126227652Sgrehanstatic inline int
127227652Sgrehanvring_size(unsigned int num, unsigned long align)
128227652Sgrehan{
129227652Sgrehan	int size;
130227652Sgrehan
131227652Sgrehan	size = num * sizeof(struct vring_desc);
132246582Sbryanv	size += sizeof(struct vring_avail) + (num * sizeof(uint16_t)) +
133246582Sbryanv	    sizeof(uint16_t);
134227652Sgrehan	size = (size + align - 1) & ~(align - 1);
135227652Sgrehan	size += sizeof(struct vring_used) +
136246582Sbryanv	    (num * sizeof(struct vring_used_elem)) + sizeof(uint16_t);
137227652Sgrehan	return (size);
138227652Sgrehan}
139227652Sgrehan
140227652Sgrehanstatic inline void
141227652Sgrehanvring_init(struct vring *vr, unsigned int num, uint8_t *p,
142227652Sgrehan    unsigned long align)
143227652Sgrehan{
144227652Sgrehan        vr->num = num;
145227652Sgrehan        vr->desc = (struct vring_desc *) p;
146227652Sgrehan        vr->avail = (struct vring_avail *) (p +
147227652Sgrehan	    num * sizeof(struct vring_desc));
148227652Sgrehan        vr->used = (void *)
149227652Sgrehan	    (((unsigned long) &vr->avail->ring[num] + align-1) & ~(align-1));
150227652Sgrehan}
151239472Semaste
152239472Semaste/*
153239472Semaste * The following is used with VIRTIO_RING_F_EVENT_IDX.
154239472Semaste *
155239472Semaste * Assuming a given event_idx value from the other size, if we have
156239472Semaste * just incremented index from old to new_idx, should we trigger an
157239472Semaste * event?
158239472Semaste */
159239472Semastestatic inline int
160239472Semastevring_need_event(uint16_t event_idx, uint16_t new_idx, uint16_t old)
161239472Semaste{
162239472Semaste
163239472Semaste	return (uint16_t)(new_idx - event_idx - 1) < (uint16_t)(new_idx - old);
164239472Semaste}
165227652Sgrehan#endif /* VIRTIO_RING_H */
166