xref: /illumos-gate/usr/src/compat/bhyve/dev/virtio/virtio.h (revision 8e458de0baeb1fee50643403223bc7e909a48464)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2013  Chris Torek <torek @ torek net>
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 AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * $FreeBSD$
29  */
30 
31 #ifndef	_BHYVE_COMPAT_VIRTIO_H_
32 #define	_BHYVE_COMPAT_VIRTIO_H_
33 
34 #define VRING_DESC_F_NEXT	(1 << 0)
35 #define VRING_DESC_F_WRITE	(1 << 1)
36 #define VRING_DESC_F_INDIRECT	(1 << 2)
37 
38 struct vring_desc {
39 	uint64_t	addr;		/* guest physical address */
40 	uint32_t	len;		/* length of scatter/gather seg */
41 	uint16_t	flags;		/* VRING_F_DESC_* */
42 	uint16_t	next;		/* next desc if F_NEXT */
43 } __packed;
44 
45 struct vring_used_elem {
46 	uint32_t	id;		/* head of used descriptor chain */
47 	uint32_t	len;		/* length written-to */
48 } __packed;
49 
50 #define VRING_AVAIL_F_NO_INTERRUPT   1
51 
52 struct vring_avail {
53 	uint16_t	flags;		/* VRING_AVAIL_F_* */
54 	uint16_t	idx;		/* counts to 65535, then cycles */
55 	uint16_t	ring[];		/* size N, reported in QNUM value */
56 /*	uint16_t	used_event;	-- after N ring entries */
57 } __packed;
58 
59 #define	VRING_USED_F_NO_NOTIFY		1
60 struct vring_used {
61 	uint16_t	flags;		/* VRING_USED_F_* */
62 	uint16_t	idx;		/* counts to 65535, then cycles */
63 	struct vring_used_elem ring[];	/* size N */
64 /*	uint16_t	avail_event;	-- after N ring entries */
65 } __packed;
66 
67 /*
68  * Virtio device types
69  */
70 #define	VIRTIO_ID_NETWORK	1
71 #define	VIRTIO_ID_BLOCK		2
72 #define	VIRTIO_ID_CONSOLE	3
73 #define	VIRTIO_ID_ENTROPY	4
74 #define	VIRTIO_ID_BALLOON	5
75 #define	VIRTIO_ID_IOMEMORY	6
76 #define	VIRTIO_ID_RPMSG		7
77 #define	VIRTIO_ID_SCSI		8
78 #define	VIRTIO_ID_9P		9
79 
80 /* experimental IDs start at 65535 and work down */
81 
82 /*
83  * PCI config space constants.
84  *
85  * If MSI-X is enabled, the ISR register is generally not used,
86  * and the configuration vector and queue vector appear at offsets
87  * 20 and 22 with the remaining configuration registers at 24.
88  * If MSI-X is not enabled, those two registers disappear and
89  * the remaining configuration registers start at offset 20.
90  */
91 #define	VIRTIO_PCI_HOST_FEATURES		0
92 #define	VIRTIO_PCI_GUEST_FEATURES		4
93 #define	VIRTIO_PCI_QUEUE_PFN			8
94 #define	VIRTIO_PCI_QUEUE_NUM			12
95 #define	VIRTIO_PCI_QUEUE_SEL			14
96 #define	VIRTIO_PCI_QUEUE_NOTIFY			16
97 #define	VIRTIO_PCI_STATUS			18
98 #define	VIRTIO_PCI_ISR				19
99 #define	VIRTIO_MSI_CONFIG_VECTOR		20
100 #define	VIRTIO_MSI_QUEUE_VECTOR			22
101 #define	VIRTIO_PCI_CONFIG_OFF(msix_enabled)	((msix_enabled) ? 24 : 20)
102 
103 /*
104  * Bits in VTCFG_R_STATUS.  Guests need not actually set any of these,
105  * but a guest writing 0 to this register means "please reset".
106  */
107 #define	VTCFG_STATUS_ACK	0x01	/* guest OS has acknowledged dev */
108 #define	VTCFG_STATUS_DRIVER	0x02	/* guest OS driver is loaded */
109 #define	VTCFG_STATUS_DRIVER_OK	0x04	/* guest OS driver ready */
110 #define	VTCFG_STATUS_FAILED	0x80	/* guest has given up on this dev */
111 
112 /*
113  * Bits in VTCFG_R_ISR.  These apply only if not using MSI-X.
114  *
115  * (We don't [yet?] ever use CONF_CHANGED.)
116  */
117 #define	VTCFG_ISR_QUEUES	0x01	/* re-scan queues */
118 #define	VTCFG_ISR_CONF_CHANGED	0x80	/* configuration changed */
119 
120 #define	VIRTIO_MSI_NO_VECTOR	0xFFFF
121 
122 /*
123  * Feature flags.
124  * Note: bits 0 through 23 are reserved to each device type.
125  */
126 #define	VIRTIO_F_NOTIFY_ON_EMPTY	(1 << 24)
127 #define	VIRTIO_RING_F_INDIRECT_DESC	(1 << 28)
128 #define	VIRTIO_RING_F_EVENT_IDX		(1 << 29)
129 
130 static inline int
131 vring_size(unsigned int num, unsigned long align)
132 {
133         int size;
134 
135         size = num * sizeof(struct vring_desc);
136         size += sizeof(struct vring_avail) + (num * sizeof(uint16_t)) +
137             sizeof(uint16_t);
138         size = (size + align - 1) & ~(align - 1);
139         size += sizeof(struct vring_used) +
140             (num * sizeof(struct vring_used_elem)) + sizeof(uint16_t);
141         return (size);
142 }
143 
144 #endif	/* _BHYVE_COMPAT_VIRTIO_H_ */
145