xref: /illumos-gate/usr/src/compat/bhyve/dev/virtio/virtio.h (revision 32640292339b07090f10ce34d455f98711077343)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
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 
29 #ifndef	_BHYVE_COMPAT_VIRTIO_H_
30 #define	_BHYVE_COMPAT_VIRTIO_H_
31 
32 #define VRING_DESC_F_NEXT	(1 << 0)
33 #define VRING_DESC_F_WRITE	(1 << 1)
34 #define VRING_DESC_F_INDIRECT	(1 << 2)
35 
36 struct vring_desc {
37 	uint64_t	addr;		/* guest physical address */
38 	uint32_t	len;		/* length of scatter/gather seg */
39 	uint16_t	flags;		/* VRING_F_DESC_* */
40 	uint16_t	next;		/* next desc if F_NEXT */
41 } __packed;
42 
43 struct vring_used_elem {
44 	uint32_t	id;		/* head of used descriptor chain */
45 	uint32_t	len;		/* length written-to */
46 } __packed;
47 
48 #define VRING_AVAIL_F_NO_INTERRUPT   1
49 
50 struct vring_avail {
51 	uint16_t	flags;		/* VRING_AVAIL_F_* */
52 	uint16_t	idx;		/* counts to 65535, then cycles */
53 	uint16_t	ring[];		/* size N, reported in QNUM value */
54 /*	uint16_t	used_event;	-- after N ring entries */
55 } __packed;
56 
57 #define	VRING_USED_F_NO_NOTIFY		1
58 struct vring_used {
59 	uint16_t	flags;		/* VRING_USED_F_* */
60 	uint16_t	idx;		/* counts to 65535, then cycles */
61 	struct vring_used_elem ring[];	/* size N */
62 /*	uint16_t	avail_event;	-- after N ring entries */
63 } __packed;
64 
65 /*
66  * Virtio device types
67  */
68 #define	VIRTIO_ID_NETWORK	1
69 #define	VIRTIO_ID_BLOCK		2
70 #define	VIRTIO_ID_CONSOLE	3
71 #define	VIRTIO_ID_ENTROPY	4
72 #define	VIRTIO_ID_BALLOON	5
73 #define	VIRTIO_ID_IOMEMORY	6
74 #define	VIRTIO_ID_RPMSG		7
75 #define	VIRTIO_ID_SCSI		8
76 #define	VIRTIO_ID_9P		9
77 
78 /* experimental IDs start at 65535 and work down */
79 
80 /*
81  * PCI config space constants.
82  *
83  * If MSI-X is enabled, the ISR register is generally not used,
84  * and the configuration vector and queue vector appear at offsets
85  * 20 and 22 with the remaining configuration registers at 24.
86  * If MSI-X is not enabled, those two registers disappear and
87  * the remaining configuration registers start at offset 20.
88  */
89 #define	VIRTIO_PCI_HOST_FEATURES		0
90 #define	VIRTIO_PCI_GUEST_FEATURES		4
91 #define	VIRTIO_PCI_QUEUE_PFN			8
92 #define	VIRTIO_PCI_QUEUE_NUM			12
93 #define	VIRTIO_PCI_QUEUE_SEL			14
94 #define	VIRTIO_PCI_QUEUE_NOTIFY			16
95 #define	VIRTIO_PCI_STATUS			18
96 #define	VIRTIO_PCI_ISR				19
97 #define	VIRTIO_MSI_CONFIG_VECTOR		20
98 #define	VIRTIO_MSI_QUEUE_VECTOR			22
99 #define	VIRTIO_PCI_CONFIG_OFF(msix_enabled)	((msix_enabled) ? 24 : 20)
100 
101 /*
102  * Bits in VTCFG_R_STATUS.  Guests need not actually set any of these,
103  * but a guest writing 0 to this register means "please reset".
104  */
105 #define	VTCFG_STATUS_ACK	0x01	/* guest OS has acknowledged dev */
106 #define	VTCFG_STATUS_DRIVER	0x02	/* guest OS driver is loaded */
107 #define	VTCFG_STATUS_DRIVER_OK	0x04	/* guest OS driver ready */
108 #define	VTCFG_STATUS_FAILED	0x80	/* guest has given up on this dev */
109 
110 /*
111  * Bits in VTCFG_R_ISR.  These apply only if not using MSI-X.
112  */
113 /* The bit of the ISR which indicates a device has an interrupt. */
114 #define VIRTIO_PCI_ISR_INTR     0x01
115 /* The bit of the ISR which indicates a device configuration change. */
116 #define VIRTIO_PCI_ISR_CONFIG   0x02
117 
118 #define	VIRTIO_MSI_NO_VECTOR	0xFFFF
119 
120 /*
121  * Feature flags.
122  * Note: bits 0 through 23 are reserved to each device type.
123  */
124 #define	VIRTIO_F_NOTIFY_ON_EMPTY	(1 << 24)
125 #define	VIRTIO_RING_F_INDIRECT_DESC	(1 << 28)
126 #define	VIRTIO_RING_F_EVENT_IDX		(1 << 29)
127 
128 static inline int
vring_size(unsigned int num,unsigned long align)129 vring_size(unsigned int num, unsigned long align)
130 {
131         int size;
132 
133         size = num * sizeof(struct vring_desc);
134         size += sizeof(struct vring_avail) + (num * sizeof(uint16_t)) +
135             sizeof(uint16_t);
136         size = (size + align - 1) & ~(align - 1);
137         size += sizeof(struct vring_used) +
138             (num * sizeof(struct vring_used_elem)) + sizeof(uint16_t);
139         return (size);
140 }
141 
142 #endif	/* _BHYVE_COMPAT_VIRTIO_H_ */
143