xref: /illumos-gate/usr/src/cmd/bhyve/virtio.h (revision 32640292)
1bf21cd93STycho Nightingale /*-
2*32640292SAndy Fiddaman  * SPDX-License-Identifier: BSD-2-Clause
34c87aefeSPatrick Mooney  *
4bf21cd93STycho Nightingale  * Copyright (c) 2013  Chris Torek <torek @ torek net>
5bf21cd93STycho Nightingale  * All rights reserved.
6bf21cd93STycho Nightingale  *
7bf21cd93STycho Nightingale  * Redistribution and use in source and binary forms, with or without
8bf21cd93STycho Nightingale  * modification, are permitted provided that the following conditions
9bf21cd93STycho Nightingale  * are met:
10bf21cd93STycho Nightingale  * 1. Redistributions of source code must retain the above copyright
11bf21cd93STycho Nightingale  *    notice, this list of conditions and the following disclaimer.
12bf21cd93STycho Nightingale  * 2. Redistributions in binary form must reproduce the above copyright
13bf21cd93STycho Nightingale  *    notice, this list of conditions and the following disclaimer in the
14bf21cd93STycho Nightingale  *    documentation and/or other materials provided with the distribution.
15bf21cd93STycho Nightingale  *
16bf21cd93STycho Nightingale  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17bf21cd93STycho Nightingale  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18bf21cd93STycho Nightingale  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19bf21cd93STycho Nightingale  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20bf21cd93STycho Nightingale  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21bf21cd93STycho Nightingale  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22bf21cd93STycho Nightingale  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23bf21cd93STycho Nightingale  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24bf21cd93STycho Nightingale  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25bf21cd93STycho Nightingale  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26bf21cd93STycho Nightingale  * SUCH DAMAGE.
27bf21cd93STycho Nightingale  */
28bf21cd93STycho Nightingale 
292b948146SAndy Fiddaman #ifndef	_BHYVE_VIRTIO_H_
302b948146SAndy Fiddaman #define	_BHYVE_VIRTIO_H_
31bf21cd93STycho Nightingale 
324c87aefeSPatrick Mooney #include <pthread_np.h>
3384659b24SMichael Zeller #include <machine/atomic.h>
344c87aefeSPatrick Mooney 
352b948146SAndy Fiddaman #include <dev/virtio/virtio.h>
362b948146SAndy Fiddaman #ifdef __FreeBSD__
372b948146SAndy Fiddaman #include <dev/virtio/virtio_ring.h>
382b948146SAndy Fiddaman #include <dev/virtio/pci/virtio_pci_var.h>
392b948146SAndy Fiddaman #endif
402b948146SAndy Fiddaman 
41bf21cd93STycho Nightingale /*
42bf21cd93STycho Nightingale  * These are derived from several virtio specifications.
43bf21cd93STycho Nightingale  *
44bf21cd93STycho Nightingale  * Some useful links:
45bf21cd93STycho Nightingale  *    https://github.com/rustyrussell/virtio-spec
46bf21cd93STycho Nightingale  *    http://people.redhat.com/pbonzini/virtio-spec.pdf
47bf21cd93STycho Nightingale  */
48bf21cd93STycho Nightingale 
49bf21cd93STycho Nightingale /*
50bf21cd93STycho Nightingale  * A virtual device has zero or more "virtual queues" (virtqueue).
51bf21cd93STycho Nightingale  * Each virtqueue uses at least two 4096-byte pages, laid out thus:
52bf21cd93STycho Nightingale  *
53bf21cd93STycho Nightingale  *      +-----------------------------------------------+
54bf21cd93STycho Nightingale  *      |    "desc":  <N> descriptors, 16 bytes each    |
55bf21cd93STycho Nightingale  *      |   -----------------------------------------   |
56bf21cd93STycho Nightingale  *      |   "avail":   2 uint16; <N> uint16; 1 uint16   |
57bf21cd93STycho Nightingale  *      |   -----------------------------------------   |
58bf21cd93STycho Nightingale  *      |              pad to 4k boundary               |
59bf21cd93STycho Nightingale  *      +-----------------------------------------------+
60bf21cd93STycho Nightingale  *      |   "used": 2 x uint16; <N> elems; 1 uint16     |
61bf21cd93STycho Nightingale  *      |   -----------------------------------------   |
62bf21cd93STycho Nightingale  *      |              pad to 4k boundary               |
63bf21cd93STycho Nightingale  *      +-----------------------------------------------+
64bf21cd93STycho Nightingale  *
65bf21cd93STycho Nightingale  * The number <N> that appears here is always a power of two and is
66bf21cd93STycho Nightingale  * limited to no more than 32768 (as it must fit in a 16-bit field).
67bf21cd93STycho Nightingale  * If <N> is sufficiently large, the above will occupy more than
68bf21cd93STycho Nightingale  * two pages.  In any case, all pages must be physically contiguous
69bf21cd93STycho Nightingale  * within the guest's physical address space.
70bf21cd93STycho Nightingale  *
71bf21cd93STycho Nightingale  * The <N> 16-byte "desc" descriptors consist of a 64-bit guest
72bf21cd93STycho Nightingale  * physical address <addr>, a 32-bit length <len>, a 16-bit
73bf21cd93STycho Nightingale  * <flags>, and a 16-bit <next> field (all in guest byte order).
74bf21cd93STycho Nightingale  *
75bf21cd93STycho Nightingale  * There are three flags that may be set :
76bf21cd93STycho Nightingale  *	NEXT    descriptor is chained, so use its "next" field
77bf21cd93STycho Nightingale  *	WRITE   descriptor is for host to write into guest RAM
78bf21cd93STycho Nightingale  *		(else host is to read from guest RAM)
79bf21cd93STycho Nightingale  *	INDIRECT   descriptor address field is (guest physical)
80bf21cd93STycho Nightingale  *		address of a linear array of descriptors
81bf21cd93STycho Nightingale  *
82bf21cd93STycho Nightingale  * Unless INDIRECT is set, <len> is the number of bytes that may
83bf21cd93STycho Nightingale  * be read/written from guest physical address <addr>.  If
84bf21cd93STycho Nightingale  * INDIRECT is set, WRITE is ignored and <len> provides the length
85bf21cd93STycho Nightingale  * of the indirect descriptors (and <len> must be a multiple of
86bf21cd93STycho Nightingale  * 16).  Note that NEXT may still be set in the main descriptor
87bf21cd93STycho Nightingale  * pointing to the indirect, and should be set in each indirect
88bf21cd93STycho Nightingale  * descriptor that uses the next descriptor (these should generally
89bf21cd93STycho Nightingale  * be numbered sequentially).  However, INDIRECT must not be set
90bf21cd93STycho Nightingale  * in the indirect descriptors.  Upon reaching an indirect descriptor
91bf21cd93STycho Nightingale  * without a NEXT bit, control returns to the direct descriptors.
92bf21cd93STycho Nightingale  *
93bf21cd93STycho Nightingale  * Except inside an indirect, each <next> value must be in the
94bf21cd93STycho Nightingale  * range [0 .. N) (i.e., the half-open interval).  (Inside an
95bf21cd93STycho Nightingale  * indirect, each <next> must be in the range [0 .. <len>/16).)
96bf21cd93STycho Nightingale  *
97bf21cd93STycho Nightingale  * The "avail" data structures reside in the same pages as the
98bf21cd93STycho Nightingale  * "desc" structures since both together are used by the device to
99bf21cd93STycho Nightingale  * pass information to the hypervisor's virtual driver.  These
100bf21cd93STycho Nightingale  * begin with a 16-bit <flags> field and 16-bit index <idx>, then
101bf21cd93STycho Nightingale  * have <N> 16-bit <ring> values, followed by one final 16-bit
102bf21cd93STycho Nightingale  * field <used_event>.  The <N> <ring> entries are simply indices
103bf21cd93STycho Nightingale  * indices into the descriptor ring (and thus must meet the same
104bf21cd93STycho Nightingale  * constraints as each <next> value).  However, <idx> is counted
105bf21cd93STycho Nightingale  * up from 0 (initially) and simply wraps around after 65535; it
106bf21cd93STycho Nightingale  * is taken mod <N> to find the next available entry.
107bf21cd93STycho Nightingale  *
108bf21cd93STycho Nightingale  * The "used" ring occupies a separate page or pages, and contains
109bf21cd93STycho Nightingale  * values written from the virtual driver back to the guest OS.
110bf21cd93STycho Nightingale  * This begins with a 16-bit <flags> and 16-bit <idx>, then there
111bf21cd93STycho Nightingale  * are <N> "vring_used" elements, followed by a 16-bit <avail_event>.
112bf21cd93STycho Nightingale  * The <N> "vring_used" elements consist of a 32-bit <id> and a
113bf21cd93STycho Nightingale  * 32-bit <len> (vu_tlen below).  The <id> is simply the index of
114bf21cd93STycho Nightingale  * the head of a descriptor chain the guest made available
115bf21cd93STycho Nightingale  * earlier, and the <len> is the number of bytes actually written,
116bf21cd93STycho Nightingale  * e.g., in the case of a network driver that provided a large
117bf21cd93STycho Nightingale  * receive buffer but received only a small amount of data.
118bf21cd93STycho Nightingale  *
119bf21cd93STycho Nightingale  * The two event fields, <used_event> and <avail_event>, in the
120bf21cd93STycho Nightingale  * avail and used rings (respectively -- note the reversal!), are
121bf21cd93STycho Nightingale  * always provided, but are used only if the virtual device
122bf21cd93STycho Nightingale  * negotiates the VIRTIO_RING_F_EVENT_IDX feature during feature
123bf21cd93STycho Nightingale  * negotiation.  Similarly, both rings provide a flag --
124bf21cd93STycho Nightingale  * VRING_AVAIL_F_NO_INTERRUPT and VRING_USED_F_NO_NOTIFY -- in
125bf21cd93STycho Nightingale  * their <flags> field, indicating that the guest does not need an
126bf21cd93STycho Nightingale  * interrupt, or that the hypervisor driver does not need a
127bf21cd93STycho Nightingale  * notify, when descriptors are added to the corresponding ring.
128bf21cd93STycho Nightingale  * (These are provided only for interrupt optimization and need
129bf21cd93STycho Nightingale  * not be implemented.)
130bf21cd93STycho Nightingale  */
131bf21cd93STycho Nightingale #define VRING_ALIGN	4096
132bf21cd93STycho Nightingale 
133bf21cd93STycho Nightingale /*
134bf21cd93STycho Nightingale  * The address of any given virtual queue is determined by a single
135bf21cd93STycho Nightingale  * Page Frame Number register.  The guest writes the PFN into the
136bf21cd93STycho Nightingale  * PCI config space.  However, a device that has two or more
137bf21cd93STycho Nightingale  * virtqueues can have a different PFN, and size, for each queue.
138bf21cd93STycho Nightingale  * The number of queues is determinable via the PCI config space
139bf21cd93STycho Nightingale  * VTCFG_R_QSEL register.  Writes to QSEL select the queue: 0 means
140bf21cd93STycho Nightingale  * queue #0, 1 means queue#1, etc.  Once a queue is selected, the
141bf21cd93STycho Nightingale  * remaining PFN and QNUM registers refer to that queue.
142bf21cd93STycho Nightingale  *
143bf21cd93STycho Nightingale  * QNUM is a read-only register containing a nonzero power of two
144bf21cd93STycho Nightingale  * that indicates the (hypervisor's) queue size.  Or, if reading it
145bf21cd93STycho Nightingale  * produces zero, the hypervisor does not have a corresponding
146bf21cd93STycho Nightingale  * queue.  (The number of possible queues depends on the virtual
147bf21cd93STycho Nightingale  * device.  The block device has just one; the network device
148bf21cd93STycho Nightingale  * provides either two -- 0 = receive, 1 = transmit -- or three,
149bf21cd93STycho Nightingale  * with 2 = control.)
150bf21cd93STycho Nightingale  *
151bf21cd93STycho Nightingale  * PFN is a read/write register giving the physical page address of
152bf21cd93STycho Nightingale  * the virtqueue in guest memory (the guest must allocate enough space
153bf21cd93STycho Nightingale  * based on the hypervisor's provided QNUM).
154bf21cd93STycho Nightingale  *
155bf21cd93STycho Nightingale  * QNOTIFY is effectively write-only: when the guest writes a queue
156bf21cd93STycho Nightingale  * number to the register, the hypervisor should scan the specified
157bf21cd93STycho Nightingale  * virtqueue. (Reading QNOTIFY currently always gets 0).
158bf21cd93STycho Nightingale  */
159bf21cd93STycho Nightingale 
160bf21cd93STycho Nightingale /*
161bf21cd93STycho Nightingale  * PFN register shift amount
162bf21cd93STycho Nightingale  */
1634c87aefeSPatrick Mooney #define	VRING_PFN		12
164bf21cd93STycho Nightingale 
165bf21cd93STycho Nightingale /*
166bf21cd93STycho Nightingale  * PCI vendor/device IDs
167bf21cd93STycho Nightingale  */
168bf21cd93STycho Nightingale #define	VIRTIO_VENDOR		0x1AF4
169bf21cd93STycho Nightingale #define	VIRTIO_DEV_NET		0x1000
170bf21cd93STycho Nightingale #define	VIRTIO_DEV_BLOCK	0x1001
1714c87aefeSPatrick Mooney #define	VIRTIO_DEV_CONSOLE	0x1003
172d7b72f7bSAndy Fiddaman #define	VIRTIO_DEV_SCSI		0x1004
1734c87aefeSPatrick Mooney #define	VIRTIO_DEV_RANDOM	0x1005
1742b948146SAndy Fiddaman #define	VIRTIO_DEV_9P		0x1009
175b0de25cbSAndy Fiddaman #define VIRTIO_DEV_INPUT	0x1052
176b0de25cbSAndy Fiddaman 
177b0de25cbSAndy Fiddaman /*
178b0de25cbSAndy Fiddaman  * PCI revision IDs
179b0de25cbSAndy Fiddaman  */
180b0de25cbSAndy Fiddaman #define VIRTIO_REV_INPUT	1
181b0de25cbSAndy Fiddaman 
182b0de25cbSAndy Fiddaman /*
183b0de25cbSAndy Fiddaman  * PCI subvendor IDs
184b0de25cbSAndy Fiddaman  */
185b0de25cbSAndy Fiddaman #define VIRTIO_SUBVEN_INPUT	0x108E
186b0de25cbSAndy Fiddaman 
187b0de25cbSAndy Fiddaman /*
188b0de25cbSAndy Fiddaman  * PCI subdevice IDs
189b0de25cbSAndy Fiddaman  */
190b0de25cbSAndy Fiddaman #define VIRTIO_SUBDEV_INPUT	0x1100
191bf21cd93STycho Nightingale 
192bf21cd93STycho Nightingale /* From section 2.3, "Virtqueue Configuration", of the virtio specification */
1932b948146SAndy Fiddaman static inline int
vring_size_aligned(u_int qsz)1942b948146SAndy Fiddaman vring_size_aligned(u_int qsz)
195bf21cd93STycho Nightingale {
1962b948146SAndy Fiddaman 	return (roundup2(vring_size(qsz, VRING_ALIGN), VRING_ALIGN));
197bf21cd93STycho Nightingale }
198bf21cd93STycho Nightingale 
199bf21cd93STycho Nightingale struct pci_devinst;
200bf21cd93STycho Nightingale struct vqueue_info;
201bf21cd93STycho Nightingale 
202bf21cd93STycho Nightingale /*
203bf21cd93STycho Nightingale  * A virtual device, with some number (possibly 0) of virtual
204bf21cd93STycho Nightingale  * queues and some size (possibly 0) of configuration-space
205bf21cd93STycho Nightingale  * registers private to the device.  The virtio_softc should come
206bf21cd93STycho Nightingale  * at the front of each "derived class", so that a pointer to the
207bf21cd93STycho Nightingale  * virtio_softc is also a pointer to the more specific, derived-
208bf21cd93STycho Nightingale  * from-virtio driver's softc.
209bf21cd93STycho Nightingale  *
210bf21cd93STycho Nightingale  * Note: inside each hypervisor virtio driver, changes to these
211bf21cd93STycho Nightingale  * data structures must be locked against other threads, if any.
212bf21cd93STycho Nightingale  * Except for PCI config space register read/write, we assume each
213bf21cd93STycho Nightingale  * driver does the required locking, but we need a pointer to the
214bf21cd93STycho Nightingale  * lock (if there is one) for PCI config space read/write ops.
215bf21cd93STycho Nightingale  *
216bf21cd93STycho Nightingale  * When the guest reads or writes the device's config space, the
217bf21cd93STycho Nightingale  * generic layer checks for operations on the special registers
218bf21cd93STycho Nightingale  * described above.  If the offset of the register(s) being read
219bf21cd93STycho Nightingale  * or written is past the CFG area (CFG0 or CFG1), the request is
220bf21cd93STycho Nightingale  * passed on to the virtual device, after subtracting off the
221bf21cd93STycho Nightingale  * generic-layer size.  (So, drivers can just use the offset as
222bf21cd93STycho Nightingale  * an offset into "struct config", for instance.)
223bf21cd93STycho Nightingale  *
224bf21cd93STycho Nightingale  * (The virtio layer also makes sure that the read or write is to/
225bf21cd93STycho Nightingale  * from a "good" config offset, hence vc_cfgsize, and on BAR #0.
226bf21cd93STycho Nightingale  * However, the driver must verify the read or write size and offset
227bf21cd93STycho Nightingale  * and that no one is writing a readonly register.)
228bf21cd93STycho Nightingale  *
229bf21cd93STycho Nightingale  * The BROKED flag ("this thing done gone and broked") is for future
230bf21cd93STycho Nightingale  * use.
231bf21cd93STycho Nightingale  */
232bf21cd93STycho Nightingale #define	VIRTIO_USE_MSIX		0x01
233bf21cd93STycho Nightingale #define	VIRTIO_EVENT_IDX	0x02	/* use the event-index values */
234bf21cd93STycho Nightingale #define	VIRTIO_BROKED		0x08	/* ??? */
235bf21cd93STycho Nightingale 
236bf21cd93STycho Nightingale struct virtio_softc {
237bf21cd93STycho Nightingale 	struct virtio_consts *vs_vc;	/* constants (see below) */
238bf21cd93STycho Nightingale 	int	vs_flags;		/* VIRTIO_* flags from above */
239bf21cd93STycho Nightingale 	pthread_mutex_t *vs_mtx;	/* POSIX mutex, if any */
240bf21cd93STycho Nightingale 	struct pci_devinst *vs_pi;	/* PCI device instance */
241bf21cd93STycho Nightingale 	uint32_t vs_negotiated_caps;	/* negotiated capabilities */
242bf21cd93STycho Nightingale 	struct vqueue_info *vs_queues;	/* one per vc_nvq */
243bf21cd93STycho Nightingale 	int	vs_curq;		/* current queue */
244bf21cd93STycho Nightingale 	uint8_t	vs_status;		/* value from last status write */
245bf21cd93STycho Nightingale 	uint8_t	vs_isr;			/* ISR flags, if not MSI-X */
246bf21cd93STycho Nightingale 	uint16_t vs_msix_cfg_idx;	/* MSI-X vector for config event */
247bf21cd93STycho Nightingale };
248bf21cd93STycho Nightingale 
249bf21cd93STycho Nightingale #define	VS_LOCK(vs)							\
250bf21cd93STycho Nightingale do {									\
251bf21cd93STycho Nightingale 	if (vs->vs_mtx)							\
252bf21cd93STycho Nightingale 		pthread_mutex_lock(vs->vs_mtx);				\
253bf21cd93STycho Nightingale } while (0)
254bf21cd93STycho Nightingale 
255bf21cd93STycho Nightingale #define	VS_UNLOCK(vs)							\
256bf21cd93STycho Nightingale do {									\
257bf21cd93STycho Nightingale 	if (vs->vs_mtx)							\
258bf21cd93STycho Nightingale 		pthread_mutex_unlock(vs->vs_mtx);			\
259bf21cd93STycho Nightingale } while (0)
260bf21cd93STycho Nightingale 
261bf21cd93STycho Nightingale struct virtio_consts {
262bf21cd93STycho Nightingale 	const char *vc_name;		/* name of driver (for diagnostics) */
263bf21cd93STycho Nightingale 	int	vc_nvq;			/* number of virtual queues */
264bf21cd93STycho Nightingale 	size_t	vc_cfgsize;		/* size of dev-specific config regs */
265bf21cd93STycho Nightingale 	void	(*vc_reset)(void *);	/* called on virtual device reset */
266bf21cd93STycho Nightingale 	void	(*vc_qnotify)(void *, struct vqueue_info *);
267bf21cd93STycho Nightingale 					/* called on QNOTIFY if no VQ notify */
268bf21cd93STycho Nightingale 	int	(*vc_cfgread)(void *, int, int, uint32_t *);
269bf21cd93STycho Nightingale 					/* called to read config regs */
270bf21cd93STycho Nightingale 	int	(*vc_cfgwrite)(void *, int, int, uint32_t);
271bf21cd93STycho Nightingale 					/* called to write config regs */
2724c87aefeSPatrick Mooney 	void    (*vc_apply_features)(void *, uint64_t);
2734c87aefeSPatrick Mooney 				/* called to apply negotiated features */
274bf21cd93STycho Nightingale 	uint64_t vc_hv_caps;		/* hypervisor-provided capabilities */
275bf21cd93STycho Nightingale };
276bf21cd93STycho Nightingale 
277bf21cd93STycho Nightingale /*
278bf21cd93STycho Nightingale  * Data structure allocated (statically) per virtual queue.
279bf21cd93STycho Nightingale  *
280bf21cd93STycho Nightingale  * Drivers may change vq_qsize after a reset.  When the guest OS
281bf21cd93STycho Nightingale  * requests a device reset, the hypervisor first calls
282bf21cd93STycho Nightingale  * vs->vs_vc->vc_reset(); then the data structure below is
283bf21cd93STycho Nightingale  * reinitialized (for each virtqueue: vs->vs_vc->vc_nvq).
284bf21cd93STycho Nightingale  *
285bf21cd93STycho Nightingale  * The remaining fields should only be fussed-with by the generic
286bf21cd93STycho Nightingale  * code.
287bf21cd93STycho Nightingale  *
288bf21cd93STycho Nightingale  * Note: the addresses of vq_desc, vq_avail, and vq_used are all
289bf21cd93STycho Nightingale  * computable from each other, but it's a lot simpler if we just
290bf21cd93STycho Nightingale  * keep a pointer to each one.  The event indices are similarly
291bf21cd93STycho Nightingale  * (but more easily) computable, and this time we'll compute them:
292bf21cd93STycho Nightingale  * they're just XX_ring[N].
293bf21cd93STycho Nightingale  */
294bf21cd93STycho Nightingale #define	VQ_ALLOC	0x01	/* set once we have a pfn */
295bf21cd93STycho Nightingale #define	VQ_BROKED	0x02	/* ??? */
296bf21cd93STycho Nightingale struct vqueue_info {
297bf21cd93STycho Nightingale 	uint16_t vq_qsize;	/* size of this queue (a power of 2) */
298bf21cd93STycho Nightingale 	void	(*vq_notify)(void *, struct vqueue_info *);
299bf21cd93STycho Nightingale 				/* called instead of vc_notify, if not NULL */
300bf21cd93STycho Nightingale 
301bf21cd93STycho Nightingale 	struct virtio_softc *vq_vs;	/* backpointer to softc */
302bf21cd93STycho Nightingale 	uint16_t vq_num;	/* we're the num'th queue in the softc */
303bf21cd93STycho Nightingale 
304bf21cd93STycho Nightingale 	uint16_t vq_flags;	/* flags (see above) */
3052b948146SAndy Fiddaman 	uint16_t vq_last_avail;	/* a recent value of vq_avail->idx */
306154972afSPatrick Mooney 	uint16_t vq_next_used;	/* index of the next used slot to be filled */
3072b948146SAndy Fiddaman 	uint16_t vq_save_used;	/* saved vq_used->idx; see vq_endchains */
308bf21cd93STycho Nightingale 	uint16_t vq_msix_idx;	/* MSI-X index, or VIRTIO_MSI_NO_VECTOR */
309bf21cd93STycho Nightingale 
310bf21cd93STycho Nightingale 	uint32_t vq_pfn;	/* PFN of virt queue (not shifted!) */
311bf21cd93STycho Nightingale 
31259d65d31SAndy Fiddaman 	struct vring_desc *vq_desc;	/* descriptor array */
31359d65d31SAndy Fiddaman 	struct vring_avail *vq_avail;	/* the "avail" ring */
31459d65d31SAndy Fiddaman 	struct vring_used *vq_used;	/* the "used" ring */
315bf21cd93STycho Nightingale 
316bf21cd93STycho Nightingale };
317bf21cd93STycho Nightingale /* as noted above, these are sort of backwards, name-wise */
318bf21cd93STycho Nightingale #define VQ_AVAIL_EVENT_IDX(vq) \
31959d65d31SAndy Fiddaman 	(*(uint16_t *)&(vq)->vq_used->ring[(vq)->vq_qsize])
320bf21cd93STycho Nightingale #define VQ_USED_EVENT_IDX(vq) \
3212b948146SAndy Fiddaman 	((vq)->vq_avail->ring[(vq)->vq_qsize])
322bf21cd93STycho Nightingale 
323bf21cd93STycho Nightingale /*
324bf21cd93STycho Nightingale  * Is this ring ready for I/O?
325bf21cd93STycho Nightingale  */
326bf21cd93STycho Nightingale static inline int
vq_ring_ready(struct vqueue_info * vq)327bf21cd93STycho Nightingale vq_ring_ready(struct vqueue_info *vq)
328bf21cd93STycho Nightingale {
329bf21cd93STycho Nightingale 
330bf21cd93STycho Nightingale 	return (vq->vq_flags & VQ_ALLOC);
331bf21cd93STycho Nightingale }
332bf21cd93STycho Nightingale 
333bf21cd93STycho Nightingale /*
334bf21cd93STycho Nightingale  * Are there "available" descriptors?  (This does not count
335bf21cd93STycho Nightingale  * how many, just returns True if there are some.)
336bf21cd93STycho Nightingale  */
337bf21cd93STycho Nightingale static inline int
vq_has_descs(struct vqueue_info * vq)338bf21cd93STycho Nightingale vq_has_descs(struct vqueue_info *vq)
339bf21cd93STycho Nightingale {
340bf21cd93STycho Nightingale 
341bf21cd93STycho Nightingale 	return (vq_ring_ready(vq) && vq->vq_last_avail !=
3422b948146SAndy Fiddaman 	    vq->vq_avail->idx);
343bf21cd93STycho Nightingale }
344bf21cd93STycho Nightingale 
345bf21cd93STycho Nightingale /*
346b0de25cbSAndy Fiddaman  * Deliver an interrupt to the guest for a specific MSI-X queue or
347b0de25cbSAndy Fiddaman  * event.
348bf21cd93STycho Nightingale  */
349bf21cd93STycho Nightingale static inline void
vi_interrupt(struct virtio_softc * vs,uint8_t isr,uint16_t msix_idx)350b0de25cbSAndy Fiddaman vi_interrupt(struct virtio_softc *vs, uint8_t isr, uint16_t msix_idx)
351bf21cd93STycho Nightingale {
352bf21cd93STycho Nightingale 
353bf21cd93STycho Nightingale 	if (pci_msix_enabled(vs->vs_pi))
354b0de25cbSAndy Fiddaman 		pci_generate_msix(vs->vs_pi, msix_idx);
355bf21cd93STycho Nightingale 	else {
3564c87aefeSPatrick Mooney #ifndef __FreeBSD__
3574c87aefeSPatrick Mooney 		boolean_t unlock = B_FALSE;
3584c87aefeSPatrick Mooney 
3594c87aefeSPatrick Mooney 		if (vs->vs_mtx && !pthread_mutex_isowned_np(vs->vs_mtx)) {
3604c87aefeSPatrick Mooney 			unlock = B_TRUE;
3614c87aefeSPatrick Mooney 			pthread_mutex_lock(vs->vs_mtx);
3624c87aefeSPatrick Mooney 		}
3634c87aefeSPatrick Mooney #else
364bf21cd93STycho Nightingale 		VS_LOCK(vs);
3654c87aefeSPatrick Mooney #endif
366b0de25cbSAndy Fiddaman 		vs->vs_isr |= isr;
367bf21cd93STycho Nightingale 		pci_generate_msi(vs->vs_pi, 0);
368bf21cd93STycho Nightingale 		pci_lintr_assert(vs->vs_pi);
3694c87aefeSPatrick Mooney #ifndef __FreeBSD__
3704c87aefeSPatrick Mooney 		if (unlock)
3714c87aefeSPatrick Mooney 			pthread_mutex_unlock(vs->vs_mtx);
3724c87aefeSPatrick Mooney #else
373bf21cd93STycho Nightingale 		VS_UNLOCK(vs);
3744c87aefeSPatrick Mooney #endif
375bf21cd93STycho Nightingale 	}
376bf21cd93STycho Nightingale }
377bf21cd93STycho Nightingale 
378b0de25cbSAndy Fiddaman /*
379b0de25cbSAndy Fiddaman  * Deliver an interrupt to the guest on the given virtual queue (if
380b0de25cbSAndy Fiddaman  * possible, or a generic MSI interrupt if not using MSI-X).
381b0de25cbSAndy Fiddaman  */
382b0de25cbSAndy Fiddaman static inline void
vq_interrupt(struct virtio_softc * vs,struct vqueue_info * vq)383b0de25cbSAndy Fiddaman vq_interrupt(struct virtio_softc *vs, struct vqueue_info *vq)
384b0de25cbSAndy Fiddaman {
385b0de25cbSAndy Fiddaman 
386b0de25cbSAndy Fiddaman 	vi_interrupt(vs, VIRTIO_PCI_ISR_INTR, vq->vq_msix_idx);
387b0de25cbSAndy Fiddaman }
388b0de25cbSAndy Fiddaman 
38984659b24SMichael Zeller static inline void
vq_kick_enable(struct vqueue_info * vq)39084659b24SMichael Zeller vq_kick_enable(struct vqueue_info *vq)
39184659b24SMichael Zeller {
39284659b24SMichael Zeller 
3932b948146SAndy Fiddaman 	vq->vq_used->flags &= ~VRING_USED_F_NO_NOTIFY;
39484659b24SMichael Zeller 	/*
3952b948146SAndy Fiddaman 	 * Full memory barrier to make sure the store to vq_used->flags
3962b948146SAndy Fiddaman 	 * happens before the load from vq_avail->idx, which results from a
3972b948146SAndy Fiddaman 	 * subsequent call to vq_has_descs().
39884659b24SMichael Zeller 	 */
39984659b24SMichael Zeller 	atomic_thread_fence_seq_cst();
40084659b24SMichael Zeller }
40184659b24SMichael Zeller 
40284659b24SMichael Zeller static inline void
vq_kick_disable(struct vqueue_info * vq)40384659b24SMichael Zeller vq_kick_disable(struct vqueue_info *vq)
40484659b24SMichael Zeller {
40584659b24SMichael Zeller 
4062b948146SAndy Fiddaman 	vq->vq_used->flags |= VRING_USED_F_NO_NOTIFY;
40784659b24SMichael Zeller }
40884659b24SMichael Zeller 
409bf21cd93STycho Nightingale struct iovec;
410b0de25cbSAndy Fiddaman 
411b0de25cbSAndy Fiddaman /*
412b0de25cbSAndy Fiddaman  * Request description returned by vq_getchain.
413b0de25cbSAndy Fiddaman  *
414b0de25cbSAndy Fiddaman  * Writable iovecs start at iov[req.readable].
415b0de25cbSAndy Fiddaman  */
416b0de25cbSAndy Fiddaman struct vi_req {
417b0de25cbSAndy Fiddaman 	int readable;		/* num of readable iovecs */
418b0de25cbSAndy Fiddaman 	int writable;		/* num of writable iovecs */
419b0de25cbSAndy Fiddaman 	unsigned int idx;	/* ring index */
420b0de25cbSAndy Fiddaman };
421b0de25cbSAndy Fiddaman 
422bf21cd93STycho Nightingale void	vi_softc_linkup(struct virtio_softc *vs, struct virtio_consts *vc,
423bf21cd93STycho Nightingale 			void *dev_softc, struct pci_devinst *pi,
424bf21cd93STycho Nightingale 			struct vqueue_info *queues);
425bf21cd93STycho Nightingale int	vi_intr_init(struct virtio_softc *vs, int barnum, int use_msix);
426bf21cd93STycho Nightingale void	vi_reset_dev(struct virtio_softc *);
427bf21cd93STycho Nightingale void	vi_set_io_bar(struct virtio_softc *, int);
428bf21cd93STycho Nightingale 
429b0de25cbSAndy Fiddaman int	vq_getchain(struct vqueue_info *vq, struct iovec *iov, int niov,
430b0de25cbSAndy Fiddaman 	    struct vi_req *reqp);
431154972afSPatrick Mooney void	vq_retchains(struct vqueue_info *vq, uint16_t n_chains);
432154972afSPatrick Mooney void	vq_relchain_prepare(struct vqueue_info *vq, uint16_t idx,
433154972afSPatrick Mooney 			    uint32_t iolen);
434154972afSPatrick Mooney void	vq_relchain_publish(struct vqueue_info *vq);
4354c87aefeSPatrick Mooney void	vq_relchain(struct vqueue_info *vq, uint16_t idx, uint32_t iolen);
436bf21cd93STycho Nightingale void	vq_endchains(struct vqueue_info *vq, int used_all_avail);
437bf21cd93STycho Nightingale 
438*32640292SAndy Fiddaman uint64_t vi_pci_read(struct pci_devinst *pi, int baridx, uint64_t offset,
439*32640292SAndy Fiddaman 	    int size);
440*32640292SAndy Fiddaman void	vi_pci_write(struct pci_devinst *pi, int baridx, uint64_t offset,
441*32640292SAndy Fiddaman 	    int size, uint64_t value);
442d4221574SAndy Fiddaman 
443d4221574SAndy Fiddaman #ifndef __FreeBSD__
444d4221574SAndy Fiddaman void	vi_vq_init(struct virtio_softc *, uint32_t);
445d4221574SAndy Fiddaman #endif
446d4221574SAndy Fiddaman 
4472b948146SAndy Fiddaman #endif	/* _BHYVE_VIRTIO_H_ */
448