1 /*	$NetBSD: virtioreg.h,v 1.6 2015/10/29 01:56:12 christos Exp $	*/
2 
3 /*
4  * Copyright (c) 2010 Minoura Makoto.
5  * All rights reserved.
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  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 /*
29  * Part of the file derived from `Virtio PCI Card Specification v0.8.6 DRAFT'
30  * Appendix A.
31  */
32 /* An interface for efficient virtio implementation.
33  *
34  * This header is BSD licensed so anyone can use the definitions
35  * to implement compatible drivers/servers.
36  *
37  * Copyright 2007, 2009, IBM Corporation
38  * All rights reserved.
39  *
40  * Redistribution and use in source and binary forms, with or without
41  * modification, are permitted provided that the following conditions
42  * are met:
43  * 1. Redistributions of source code must retain the above copyright
44  *    notice, this list of conditions and the following disclaimer.
45  * 2. Redistributions in binary form must reproduce the above copyright
46  *    notice, this list of conditions and the following disclaimer in the
47  *    documentation and/or other materials provided with the distribution.
48  * 3. Neither the name of IBM nor the names of its contributors
49  *    may be used to endorse or promote products derived from this software
50  *    without specific prior written permission.
51  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND
52  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54  * ARE DISCLAIMED.  IN NO EVENT SHALL IBM OR CONTRIBUTORS BE LIABLE
55  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61  * SUCH DAMAGE.
62  */
63 
64 
65 #ifndef _DEV_PCI_VIRTIOREG_H_
66 #define	_DEV_PCI_VIRTIOREG_H_
67 
68 #include <sys/types.h>
69 
70 /* Virtio product id (subsystem) */
71 #define PCI_PRODUCT_VIRTIO_NETWORK	1
72 #define PCI_PRODUCT_VIRTIO_BLOCK	2
73 #define PCI_PRODUCT_VIRTIO_CONSOLE	3
74 #define PCI_PRODUCT_VIRTIO_ENTROPY	4
75 #define PCI_PRODUCT_VIRTIO_BALLOON	5
76 #define	PCI_PRODUCT_VIRTIO_IOMEM	6
77 #define PCI_PRODUCT_VIRTIO_RPMSG	7
78 #define PCI_PRODUCT_VIRTIO_SCSI		8
79 #define PCI_PRODUCT_VIRTIO_9P		9
80 #define PCI_PRODUCT_VIRTIO_MAC80211	10
81 
82 /* Virtio header */
83 #define VIRTIO_CONFIG_DEVICE_FEATURES	0 /* 32bit */
84 #define VIRTIO_CONFIG_GUEST_FEATURES	4 /* 32bit */
85 #define  VIRTIO_F_NOTIFY_ON_EMPTY		(1<<24)
86 #define  VIRTIO_F_RING_INDIRECT_DESC		(1<<28)
87 #define  VIRTIO_F_BAD_FEATURE			(1<<30)
88 #define VIRTIO_CONFIG_QUEUE_ADDRESS	8 /* 32bit */
89 #define VIRTIO_CONFIG_QUEUE_SIZE	12 /* 16bit */
90 #define VIRTIO_CONFIG_QUEUE_SELECT	14 /* 16bit */
91 #define VIRTIO_CONFIG_QUEUE_NOTIFY	16 /* 16bit */
92 #define VIRTIO_CONFIG_DEVICE_STATUS	18 /* 8bit */
93 #define  VIRTIO_CONFIG_DEVICE_STATUS_RESET	0
94 #define  VIRTIO_CONFIG_DEVICE_STATUS_ACK	1
95 #define  VIRTIO_CONFIG_DEVICE_STATUS_DRIVER	2
96 #define  VIRTIO_CONFIG_DEVICE_STATUS_DRIVER_OK	4
97 #define  VIRTIO_CONFIG_DEVICE_STATUS_FAILED	128
98 #define VIRTIO_CONFIG_ISR_STATUS	19 /* 8bit */
99 #define  VIRTIO_CONFIG_ISR_CONFIG_CHANGE	2
100 #define VIRTIO_CONFIG_CONFIG_VECTOR	20 /* 16bit, optional */
101 #define VIRTIO_CONFIG_DEVICE_CONFIG_NOMSI	20
102 #define VIRTIO_CONFIG_DEVICE_CONFIG_MSI		24
103 /* MSI/MSI-X */
104 #define VIRTIO_CONFIG_MSI_CONFIG_VECTOR		20
105 #define VIRTIO_CONFIG_MSI_QUEUE_VECTOR		22
106 
107 #define VIRTIO_COMMON_FLAG_BITS \
108         "\20" \
109 	"\x1f""BAD_FEATURE" \
110 	"\x1e""EVENT_IDX" \
111 	"\x1d""INDIRECT_DESC" \
112 	"\x19""NOTIFY_ON_EMPTY"
113 
114 /* Virtqueue */
115 /* This marks a buffer as continuing via the next field. */
116 #define VRING_DESC_F_NEXT       1
117 /* This marks a buffer as write-only (otherwise read-only). */
118 #define VRING_DESC_F_WRITE      2
119 /* This means the buffer contains a list of buffer descriptors. */
120 #define VRING_DESC_F_INDIRECT	4
121 
122 /* The Host uses this in used->flags to advise the Guest: don't kick me
123  * when you add a buffer.  It's unreliable, so it's simply an
124  * optimization.  Guest will still kick if it's out of buffers. */
125 #define VRING_USED_F_NO_NOTIFY  1
126 /* The Guest uses this in avail->flags to advise the Host: don't
127  * interrupt me when you consume a buffer.  It's unreliable, so it's
128  * simply an optimization.  */
129 #define VRING_AVAIL_F_NO_INTERRUPT      1
130 
131 /* Virtio ring descriptors: 16 bytes.
132  * These can chain together via "next". */
133 struct vring_desc {
134         /* Address (guest-physical). */
135         uint64_t addr;
136         /* Length. */
137         uint32_t len;
138         /* The flags as indicated above. */
139         uint16_t flags;
140         /* We chain unused descriptors via this, too */
141         uint16_t next;
142 } __packed;
143 
144 struct vring_avail {
145         uint16_t flags;
146         uint16_t idx;
147         uint16_t ring[0];
148 } __packed;
149 
150 /* u32 is used here for ids for padding reasons. */
151 struct vring_used_elem {
152         /* Index of start of used descriptor chain. */
153         uint32_t id;
154         /* Total length of the descriptor chain which was written to. */
155         uint32_t len;
156 } __packed;
157 
158 struct vring_used {
159         uint16_t flags;
160         uint16_t idx;
161         struct vring_used_elem ring[0];
162 } __packed;
163 
164 /* The standard layout for the ring is a continuous chunk of memory which
165  * looks like this.  We assume num is a power of 2.
166  *
167  * struct vring {
168  *      // The actual descriptors (16 bytes each)
169  *      struct vring_desc desc[num];
170  *
171  *      // A ring of available descriptor heads with free-running index.
172  *      __u16 avail_flags;
173  *      __u16 avail_idx;
174  *      __u16 available[num];
175  *
176  *      // Padding to the next align boundary.
177  *      char pad[];
178  *
179  *      // A ring of used descriptor heads with free-running index.
180  *      __u16 used_flags;
181  *      __u16 used_idx;
182  *      struct vring_used_elem used[num];
183  * };
184  * Note: for virtio PCI, align is 4096.
185  */
186 
187 #define VIRTIO_PAGE_SIZE	(4096)
188 
189 #endif /* _DEV_PCI_VIRTIOREG_H_ */
190