xref: /illumos-gate/usr/src/cmd/bhyve/virtio.c (revision 32640292339b07090f10ce34d455f98711077343)
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.
64c87aefeSPatrick Mooney  * Copyright (c) 2019 Joyent, Inc.
7bf21cd93STycho Nightingale  *
8bf21cd93STycho Nightingale  * Redistribution and use in source and binary forms, with or without
9bf21cd93STycho Nightingale  * modification, are permitted provided that the following conditions
10bf21cd93STycho Nightingale  * are met:
11bf21cd93STycho Nightingale  * 1. Redistributions of source code must retain the above copyright
12bf21cd93STycho Nightingale  *    notice, this list of conditions and the following disclaimer.
13bf21cd93STycho Nightingale  * 2. Redistributions in binary form must reproduce the above copyright
14bf21cd93STycho Nightingale  *    notice, this list of conditions and the following disclaimer in the
15bf21cd93STycho Nightingale  *    documentation and/or other materials provided with the distribution.
16bf21cd93STycho Nightingale  *
17bf21cd93STycho Nightingale  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18bf21cd93STycho Nightingale  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19bf21cd93STycho Nightingale  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20bf21cd93STycho Nightingale  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21bf21cd93STycho Nightingale  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22bf21cd93STycho Nightingale  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23bf21cd93STycho Nightingale  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24bf21cd93STycho Nightingale  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25bf21cd93STycho Nightingale  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26bf21cd93STycho Nightingale  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27bf21cd93STycho Nightingale  * SUCH DAMAGE.
28bf21cd93STycho Nightingale  */
29bf21cd93STycho Nightingale 
30bf21cd93STycho Nightingale #include <sys/cdefs.h>
31bf21cd93STycho Nightingale 
32bf21cd93STycho Nightingale #include <sys/param.h>
33bf21cd93STycho Nightingale #include <sys/uio.h>
34bf21cd93STycho Nightingale 
354c87aefeSPatrick Mooney #include <machine/atomic.h>
364c87aefeSPatrick Mooney 
372b948146SAndy Fiddaman #ifdef __FreeBSD__
382b948146SAndy Fiddaman #include <dev/virtio/pci/virtio_pci_legacy_var.h>
392b948146SAndy Fiddaman #endif
402b948146SAndy Fiddaman 
41bf21cd93STycho Nightingale #include <stdio.h>
42bf21cd93STycho Nightingale #include <stdint.h>
43b0de25cbSAndy Fiddaman #include <string.h>
44bf21cd93STycho Nightingale #include <pthread.h>
45bf21cd93STycho Nightingale #include <pthread_np.h>
46bf21cd93STycho Nightingale 
47bf21cd93STycho Nightingale #include "bhyverun.h"
48154972afSPatrick Mooney #include "debug.h"
49bf21cd93STycho Nightingale #include "pci_emul.h"
50bf21cd93STycho Nightingale #include "virtio.h"
51bf21cd93STycho Nightingale 
52bf21cd93STycho Nightingale /*
53bf21cd93STycho Nightingale  * Functions for dealing with generalized "virtual devices" as
54bf21cd93STycho Nightingale  * defined by <https://www.google.com/#output=search&q=virtio+spec>
55bf21cd93STycho Nightingale  */
56bf21cd93STycho Nightingale 
57bf21cd93STycho Nightingale /*
58bf21cd93STycho Nightingale  * In case we decide to relax the "virtio softc comes at the
59bf21cd93STycho Nightingale  * front of virtio-based device softc" constraint, let's use
60bf21cd93STycho Nightingale  * this to convert.
61bf21cd93STycho Nightingale  */
62bf21cd93STycho Nightingale #define	DEV_SOFTC(vs) ((void *)(vs))
63bf21cd93STycho Nightingale 
64bf21cd93STycho Nightingale /*
65bf21cd93STycho Nightingale  * Link a virtio_softc to its constants, the device softc, and
66bf21cd93STycho Nightingale  * the PCI emulation.
67bf21cd93STycho Nightingale  */
68bf21cd93STycho Nightingale void
vi_softc_linkup(struct virtio_softc * vs,struct virtio_consts * vc,void * dev_softc,struct pci_devinst * pi,struct vqueue_info * queues)69bf21cd93STycho Nightingale vi_softc_linkup(struct virtio_softc *vs, struct virtio_consts *vc,
70bf21cd93STycho Nightingale 		void *dev_softc, struct pci_devinst *pi,
71bf21cd93STycho Nightingale 		struct vqueue_info *queues)
72bf21cd93STycho Nightingale {
73bf21cd93STycho Nightingale 	int i;
74bf21cd93STycho Nightingale 
75bf21cd93STycho Nightingale 	/* vs and dev_softc addresses must match */
76bf21cd93STycho Nightingale 	assert((void *)vs == dev_softc);
77bf21cd93STycho Nightingale 	vs->vs_vc = vc;
78bf21cd93STycho Nightingale 	vs->vs_pi = pi;
79bf21cd93STycho Nightingale 	pi->pi_arg = vs;
80bf21cd93STycho Nightingale 
81bf21cd93STycho Nightingale 	vs->vs_queues = queues;
82bf21cd93STycho Nightingale 	for (i = 0; i < vc->vc_nvq; i++) {
83bf21cd93STycho Nightingale 		queues[i].vq_vs = vs;
84bf21cd93STycho Nightingale 		queues[i].vq_num = i;
85bf21cd93STycho Nightingale 	}
86bf21cd93STycho Nightingale }
87bf21cd93STycho Nightingale 
88bf21cd93STycho Nightingale /*
89bf21cd93STycho Nightingale  * Reset device (device-wide).  This erases all queues, i.e.,
90bf21cd93STycho Nightingale  * all the queues become invalid (though we don't wipe out the
91bf21cd93STycho Nightingale  * internal pointers, we just clear the VQ_ALLOC flag).
92bf21cd93STycho Nightingale  *
93bf21cd93STycho Nightingale  * It resets negotiated features to "none".
94bf21cd93STycho Nightingale  *
95bf21cd93STycho Nightingale  * If MSI-X is enabled, this also resets all the vectors to NO_VECTOR.
96bf21cd93STycho Nightingale  */
97bf21cd93STycho Nightingale void
vi_reset_dev(struct virtio_softc * vs)98bf21cd93STycho Nightingale vi_reset_dev(struct virtio_softc *vs)
99bf21cd93STycho Nightingale {
100bf21cd93STycho Nightingale 	struct vqueue_info *vq;
101bf21cd93STycho Nightingale 	int i, nvq;
102bf21cd93STycho Nightingale 
103bf21cd93STycho Nightingale 	if (vs->vs_mtx)
104bf21cd93STycho Nightingale 		assert(pthread_mutex_isowned_np(vs->vs_mtx));
105bf21cd93STycho Nightingale 
106bf21cd93STycho Nightingale 	nvq = vs->vs_vc->vc_nvq;
107bf21cd93STycho Nightingale 	for (vq = vs->vs_queues, i = 0; i < nvq; vq++, i++) {
108bf21cd93STycho Nightingale 		vq->vq_flags = 0;
109bf21cd93STycho Nightingale 		vq->vq_last_avail = 0;
110154972afSPatrick Mooney 		vq->vq_next_used = 0;
1114c87aefeSPatrick Mooney 		vq->vq_save_used = 0;
112bf21cd93STycho Nightingale 		vq->vq_pfn = 0;
113bf21cd93STycho Nightingale 		vq->vq_msix_idx = VIRTIO_MSI_NO_VECTOR;
114bf21cd93STycho Nightingale 	}
115bf21cd93STycho Nightingale 	vs->vs_negotiated_caps = 0;
116bf21cd93STycho Nightingale 	vs->vs_curq = 0;
117bf21cd93STycho Nightingale 	/* vs->vs_status = 0; -- redundant */
118bf21cd93STycho Nightingale 	if (vs->vs_isr)
119bf21cd93STycho Nightingale 		pci_lintr_deassert(vs->vs_pi);
120bf21cd93STycho Nightingale 	vs->vs_isr = 0;
121bf21cd93STycho Nightingale 	vs->vs_msix_cfg_idx = VIRTIO_MSI_NO_VECTOR;
122bf21cd93STycho Nightingale }
123bf21cd93STycho Nightingale 
124bf21cd93STycho Nightingale /*
125bf21cd93STycho Nightingale  * Set I/O BAR (usually 0) to map PCI config registers.
126bf21cd93STycho Nightingale  */
127bf21cd93STycho Nightingale void
vi_set_io_bar(struct virtio_softc * vs,int barnum)128bf21cd93STycho Nightingale vi_set_io_bar(struct virtio_softc *vs, int barnum)
129bf21cd93STycho Nightingale {
130bf21cd93STycho Nightingale 	size_t size;
131bf21cd93STycho Nightingale 
132bf21cd93STycho Nightingale 	/*
1332b948146SAndy Fiddaman 	 * ??? should we use VIRTIO_PCI_CONFIG_OFF(0) if MSI-X is disabled?
134bf21cd93STycho Nightingale 	 * Existing code did not...
135bf21cd93STycho Nightingale 	 */
1362b948146SAndy Fiddaman 	size = VIRTIO_PCI_CONFIG_OFF(1) + vs->vs_vc->vc_cfgsize;
137bf21cd93STycho Nightingale 	pci_emul_alloc_bar(vs->vs_pi, barnum, PCIBAR_IO, size);
138bf21cd93STycho Nightingale }
139bf21cd93STycho Nightingale 
140bf21cd93STycho Nightingale /*
141bf21cd93STycho Nightingale  * Initialize MSI-X vector capabilities if we're to use MSI-X,
142bf21cd93STycho Nightingale  * or MSI capabilities if not.
143bf21cd93STycho Nightingale  *
144bf21cd93STycho Nightingale  * We assume we want one MSI-X vector per queue, here, plus one
145bf21cd93STycho Nightingale  * for the config vec.
146bf21cd93STycho Nightingale  */
147bf21cd93STycho Nightingale int
vi_intr_init(struct virtio_softc * vs,int barnum,int use_msix)148bf21cd93STycho Nightingale vi_intr_init(struct virtio_softc *vs, int barnum, int use_msix)
149bf21cd93STycho Nightingale {
150bf21cd93STycho Nightingale 	int nvec;
151bf21cd93STycho Nightingale 
152bf21cd93STycho Nightingale 	if (use_msix) {
153bf21cd93STycho Nightingale 		vs->vs_flags |= VIRTIO_USE_MSIX;
154bf21cd93STycho Nightingale 		VS_LOCK(vs);
155bf21cd93STycho Nightingale 		vi_reset_dev(vs); /* set all vectors to NO_VECTOR */
156bf21cd93STycho Nightingale 		VS_UNLOCK(vs);
157bf21cd93STycho Nightingale 		nvec = vs->vs_vc->vc_nvq + 1;
158bf21cd93STycho Nightingale 		if (pci_emul_add_msixcap(vs->vs_pi, nvec, barnum))
159bf21cd93STycho Nightingale 			return (1);
160bf21cd93STycho Nightingale 	} else
161bf21cd93STycho Nightingale 		vs->vs_flags &= ~VIRTIO_USE_MSIX;
1624c87aefeSPatrick Mooney 
163bf21cd93STycho Nightingale 	/* Only 1 MSI vector for bhyve */
164bf21cd93STycho Nightingale 	pci_emul_add_msicap(vs->vs_pi, 1);
1654c87aefeSPatrick Mooney 
1664c87aefeSPatrick Mooney 	/* Legacy interrupts are mandatory for virtio devices */
1674c87aefeSPatrick Mooney 	pci_lintr_request(vs->vs_pi);
1684c87aefeSPatrick Mooney 
169bf21cd93STycho Nightingale 	return (0);
170bf21cd93STycho Nightingale }
171bf21cd93STycho Nightingale 
172bf21cd93STycho Nightingale /*
173bf21cd93STycho Nightingale  * Initialize the currently-selected virtio queue (vs->vs_curq).
174bf21cd93STycho Nightingale  * The guest just gave us a page frame number, from which we can
175bf21cd93STycho Nightingale  * calculate the addresses of the queue.
176bf21cd93STycho Nightingale  */
177bf21cd93STycho Nightingale void
vi_vq_init(struct virtio_softc * vs,uint32_t pfn)178bf21cd93STycho Nightingale vi_vq_init(struct virtio_softc *vs, uint32_t pfn)
179bf21cd93STycho Nightingale {
180bf21cd93STycho Nightingale 	struct vqueue_info *vq;
181bf21cd93STycho Nightingale 	uint64_t phys;
182bf21cd93STycho Nightingale 	size_t size;
183bf21cd93STycho Nightingale 	char *base;
184bf21cd93STycho Nightingale 
185bf21cd93STycho Nightingale 	vq = &vs->vs_queues[vs->vs_curq];
186bf21cd93STycho Nightingale 	vq->vq_pfn = pfn;
187bf21cd93STycho Nightingale 	phys = (uint64_t)pfn << VRING_PFN;
1882b948146SAndy Fiddaman 	size = vring_size_aligned(vq->vq_qsize);
189bf21cd93STycho Nightingale 	base = paddr_guest2host(vs->vs_pi->pi_vmctx, phys, size);
190bf21cd93STycho Nightingale 
191bf21cd93STycho Nightingale 	/* First page(s) are descriptors... */
1922b948146SAndy Fiddaman 	vq->vq_desc = (struct vring_desc *)base;
1932b948146SAndy Fiddaman 	base += vq->vq_qsize * sizeof(struct vring_desc);
194bf21cd93STycho Nightingale 
195bf21cd93STycho Nightingale 	/* ... immediately followed by "avail" ring (entirely uint16_t's) */
196bf21cd93STycho Nightingale 	vq->vq_avail = (struct vring_avail *)base;
197bf21cd93STycho Nightingale 	base += (2 + vq->vq_qsize + 1) * sizeof(uint16_t);
198bf21cd93STycho Nightingale 
199bf21cd93STycho Nightingale 	/* Then it's rounded up to the next page... */
200bf21cd93STycho Nightingale 	base = (char *)roundup2((uintptr_t)base, VRING_ALIGN);
201bf21cd93STycho Nightingale 
202bf21cd93STycho Nightingale 	/* ... and the last page(s) are the used ring. */
203bf21cd93STycho Nightingale 	vq->vq_used = (struct vring_used *)base;
204bf21cd93STycho Nightingale 
205bf21cd93STycho Nightingale 	/* Mark queue as allocated, and start at 0 when we use it. */
206bf21cd93STycho Nightingale 	vq->vq_flags = VQ_ALLOC;
207bf21cd93STycho Nightingale 	vq->vq_last_avail = 0;
208154972afSPatrick Mooney 	vq->vq_next_used = 0;
2094c87aefeSPatrick Mooney 	vq->vq_save_used = 0;
210bf21cd93STycho Nightingale }
211bf21cd93STycho Nightingale 
212bf21cd93STycho Nightingale /*
213bf21cd93STycho Nightingale  * Helper inline for vq_getchain(): record the i'th "real"
214bf21cd93STycho Nightingale  * descriptor.
215bf21cd93STycho Nightingale  */
216bf21cd93STycho Nightingale static inline void
_vq_record(int i,struct vring_desc * vd,struct vmctx * ctx,struct iovec * iov,int n_iov,struct vi_req * reqp)21759d65d31SAndy Fiddaman _vq_record(int i, struct vring_desc *vd, struct vmctx *ctx, struct iovec *iov,
21859d65d31SAndy Fiddaman     int n_iov, struct vi_req *reqp)
21959d65d31SAndy Fiddaman {
220bf21cd93STycho Nightingale 	if (i >= n_iov)
221bf21cd93STycho Nightingale 		return;
2222b948146SAndy Fiddaman 	iov[i].iov_base = paddr_guest2host(ctx, vd->addr, vd->len);
2232b948146SAndy Fiddaman 	iov[i].iov_len = vd->len;
224b0de25cbSAndy Fiddaman 	if ((vd->flags & VRING_DESC_F_WRITE) == 0)
225b0de25cbSAndy Fiddaman 		reqp->readable++;
226b0de25cbSAndy Fiddaman 	else
227b0de25cbSAndy Fiddaman 		reqp->writable++;
228bf21cd93STycho Nightingale }
229bf21cd93STycho Nightingale #define	VQ_MAX_DESCRIPTORS	512	/* see below */
230bf21cd93STycho Nightingale 
231bf21cd93STycho Nightingale /*
232bf21cd93STycho Nightingale  * Examine the chain of descriptors starting at the "next one" to
233bf21cd93STycho Nightingale  * make sure that they describe a sensible request.  If so, return
234bf21cd93STycho Nightingale  * the number of "real" descriptors that would be needed/used in
235bf21cd93STycho Nightingale  * acting on this request.  This may be smaller than the number of
236bf21cd93STycho Nightingale  * available descriptors, e.g., if there are two available but
237bf21cd93STycho Nightingale  * they are two separate requests, this just returns 1.  Or, it
238bf21cd93STycho Nightingale  * may be larger: if there are indirect descriptors involved,
239bf21cd93STycho Nightingale  * there may only be one descriptor available but it may be an
240bf21cd93STycho Nightingale  * indirect pointing to eight more.  We return 8 in this case,
241bf21cd93STycho Nightingale  * i.e., we do not count the indirect descriptors, only the "real"
242bf21cd93STycho Nightingale  * ones.
243bf21cd93STycho Nightingale  *
2442b948146SAndy Fiddaman  * Basically, this vets the "flags" and "next" field of each
245bf21cd93STycho Nightingale  * descriptor and tells you how many are involved.  Since some may
246bf21cd93STycho Nightingale  * be indirect, this also needs the vmctx (in the pci_devinst
247bf21cd93STycho Nightingale  * at vs->vs_pi) so that it can find indirect descriptors.
248bf21cd93STycho Nightingale  *
249bf21cd93STycho Nightingale  * As we process each descriptor, we copy and adjust it (guest to
250bf21cd93STycho Nightingale  * host address wise, also using the vmtctx) into the given iov[]
251bf21cd93STycho Nightingale  * array (of the given size).  If the array overflows, we stop
252bf21cd93STycho Nightingale  * placing values into the array but keep processing descriptors,
253bf21cd93STycho Nightingale  * up to VQ_MAX_DESCRIPTORS, before giving up and returning -1.
254bf21cd93STycho Nightingale  * So you, the caller, must not assume that iov[] is as big as the
255bf21cd93STycho Nightingale  * return value (you can process the same thing twice to allocate
256bf21cd93STycho Nightingale  * a larger iov array if needed, or supply a zero length to find
257bf21cd93STycho Nightingale  * out how much space is needed).
258bf21cd93STycho Nightingale  *
259bf21cd93STycho Nightingale  * If some descriptor(s) are invalid, this prints a diagnostic message
260bf21cd93STycho Nightingale  * and returns -1.  If no descriptors are ready now it simply returns 0.
261bf21cd93STycho Nightingale  *
262bf21cd93STycho Nightingale  * You are assumed to have done a vq_ring_ready() if needed (note
263bf21cd93STycho Nightingale  * that vq_has_descs() does one).
264bf21cd93STycho Nightingale  */
265bf21cd93STycho Nightingale int
vq_getchain(struct vqueue_info * vq,struct iovec * iov,int niov,struct vi_req * reqp)266b0de25cbSAndy Fiddaman vq_getchain(struct vqueue_info *vq, struct iovec *iov, int niov,
267b0de25cbSAndy Fiddaman 	    struct vi_req *reqp)
268bf21cd93STycho Nightingale {
269bf21cd93STycho Nightingale 	int i;
270bf21cd93STycho Nightingale 	u_int ndesc, n_indir;
2714c87aefeSPatrick Mooney 	u_int idx, next;
272b0de25cbSAndy Fiddaman 	struct vi_req req;
27359d65d31SAndy Fiddaman 	struct vring_desc *vdir, *vindir, *vp;
274bf21cd93STycho Nightingale 	struct vmctx *ctx;
275bf21cd93STycho Nightingale 	struct virtio_softc *vs;
276bf21cd93STycho Nightingale 	const char *name;
277bf21cd93STycho Nightingale 
278bf21cd93STycho Nightingale 	vs = vq->vq_vs;
279bf21cd93STycho Nightingale 	name = vs->vs_vc->vc_name;
280b0de25cbSAndy Fiddaman 	memset(&req, 0, sizeof(req));
281bf21cd93STycho Nightingale 
282bf21cd93STycho Nightingale 	/*
283bf21cd93STycho Nightingale 	 * Note: it's the responsibility of the guest not to
2842b948146SAndy Fiddaman 	 * update vq->vq_avail->idx until all of the descriptors
285bf21cd93STycho Nightingale          * the guest has written are valid (including all their
2862b948146SAndy Fiddaman          * "next" fields and "flags").
287bf21cd93STycho Nightingale 	 *
2882b948146SAndy Fiddaman 	 * Compute (vq_avail->idx - last_avail) in integers mod 2**16.  This is
289bf21cd93STycho Nightingale 	 * the number of descriptors the device has made available
290bf21cd93STycho Nightingale 	 * since the last time we updated vq->vq_last_avail.
291bf21cd93STycho Nightingale 	 *
292bf21cd93STycho Nightingale 	 * We just need to do the subtraction as an unsigned int,
293bf21cd93STycho Nightingale 	 * then trim off excess bits.
294bf21cd93STycho Nightingale 	 */
295bf21cd93STycho Nightingale 	idx = vq->vq_last_avail;
2962b948146SAndy Fiddaman 	ndesc = (uint16_t)((u_int)vq->vq_avail->idx - idx);
297bf21cd93STycho Nightingale 	if (ndesc == 0)
298bf21cd93STycho Nightingale 		return (0);
299bf21cd93STycho Nightingale 	if (ndesc > vq->vq_qsize) {
300bf21cd93STycho Nightingale 		/* XXX need better way to diagnose issues */
301154972afSPatrick Mooney 		EPRINTLN(
302154972afSPatrick Mooney 		    "%s: ndesc (%u) out of range, driver confused?",
303bf21cd93STycho Nightingale 		    name, (u_int)ndesc);
304bf21cd93STycho Nightingale 		return (-1);
305bf21cd93STycho Nightingale 	}
306bf21cd93STycho Nightingale 
307bf21cd93STycho Nightingale 	/*
308bf21cd93STycho Nightingale 	 * Now count/parse "involved" descriptors starting from
309bf21cd93STycho Nightingale 	 * the head of the chain.
310bf21cd93STycho Nightingale 	 *
311bf21cd93STycho Nightingale 	 * To prevent loops, we could be more complicated and
312bf21cd93STycho Nightingale 	 * check whether we're re-visiting a previously visited
313bf21cd93STycho Nightingale 	 * index, but we just abort if the count gets excessive.
314bf21cd93STycho Nightingale 	 */
315bf21cd93STycho Nightingale 	ctx = vs->vs_pi->pi_vmctx;
316b0de25cbSAndy Fiddaman 	req.idx = next = vq->vq_avail->ring[idx & (vq->vq_qsize - 1)];
3174c87aefeSPatrick Mooney 	vq->vq_last_avail++;
3182b948146SAndy Fiddaman 	for (i = 0; i < VQ_MAX_DESCRIPTORS; next = vdir->next) {
319bf21cd93STycho Nightingale 		if (next >= vq->vq_qsize) {
320154972afSPatrick Mooney 			EPRINTLN(
321bf21cd93STycho Nightingale 			    "%s: descriptor index %u out of range, "
322154972afSPatrick Mooney 			    "driver confused?",
323bf21cd93STycho Nightingale 			    name, next);
324bf21cd93STycho Nightingale 			return (-1);
325bf21cd93STycho Nightingale 		}
326bf21cd93STycho Nightingale 		vdir = &vq->vq_desc[next];
3272b948146SAndy Fiddaman 		if ((vdir->flags & VRING_DESC_F_INDIRECT) == 0) {
328b0de25cbSAndy Fiddaman 			_vq_record(i, vdir, ctx, iov, niov, &req);
329bf21cd93STycho Nightingale 			i++;
3304c87aefeSPatrick Mooney 		} else if ((vs->vs_vc->vc_hv_caps &
331bf21cd93STycho Nightingale 		    VIRTIO_RING_F_INDIRECT_DESC) == 0) {
332154972afSPatrick Mooney 			EPRINTLN(
333bf21cd93STycho Nightingale 			    "%s: descriptor has forbidden INDIRECT flag, "
334154972afSPatrick Mooney 			    "driver confused?",
335bf21cd93STycho Nightingale 			    name);
336bf21cd93STycho Nightingale 			return (-1);
337bf21cd93STycho Nightingale 		} else {
3382b948146SAndy Fiddaman 			n_indir = vdir->len / 16;
3392b948146SAndy Fiddaman 			if ((vdir->len & 0xf) || n_indir == 0) {
340154972afSPatrick Mooney 				EPRINTLN(
341bf21cd93STycho Nightingale 				    "%s: invalid indir len 0x%x, "
342154972afSPatrick Mooney 				    "driver confused?",
3432b948146SAndy Fiddaman 				    name, (u_int)vdir->len);
344bf21cd93STycho Nightingale 				return (-1);
345bf21cd93STycho Nightingale 			}
346bf21cd93STycho Nightingale 			vindir = paddr_guest2host(ctx,
3472b948146SAndy Fiddaman 			    vdir->addr, vdir->len);
348bf21cd93STycho Nightingale 			/*
349bf21cd93STycho Nightingale 			 * Indirects start at the 0th, then follow
350bf21cd93STycho Nightingale 			 * their own embedded "next"s until those run
351bf21cd93STycho Nightingale 			 * out.  Each one's indirect flag must be off
352bf21cd93STycho Nightingale 			 * (we don't really have to check, could just
353bf21cd93STycho Nightingale 			 * ignore errors...).
354bf21cd93STycho Nightingale 			 */
355bf21cd93STycho Nightingale 			next = 0;
356bf21cd93STycho Nightingale 			for (;;) {
357bf21cd93STycho Nightingale 				vp = &vindir[next];
3582b948146SAndy Fiddaman 				if (vp->flags & VRING_DESC_F_INDIRECT) {
359154972afSPatrick Mooney 					EPRINTLN(
360bf21cd93STycho Nightingale 					    "%s: indirect desc has INDIR flag,"
361154972afSPatrick Mooney 					    " driver confused?",
362bf21cd93STycho Nightingale 					    name);
363bf21cd93STycho Nightingale 					return (-1);
364bf21cd93STycho Nightingale 				}
365b0de25cbSAndy Fiddaman 				_vq_record(i, vp, ctx, iov, niov, &req);
366bf21cd93STycho Nightingale 				if (++i > VQ_MAX_DESCRIPTORS)
367bf21cd93STycho Nightingale 					goto loopy;
3682b948146SAndy Fiddaman 				if ((vp->flags & VRING_DESC_F_NEXT) == 0)
369bf21cd93STycho Nightingale 					break;
3702b948146SAndy Fiddaman 				next = vp->next;
371bf21cd93STycho Nightingale 				if (next >= n_indir) {
372154972afSPatrick Mooney 					EPRINTLN(
373bf21cd93STycho Nightingale 					    "%s: invalid next %u > %u, "
374154972afSPatrick Mooney 					    "driver confused?",
375bf21cd93STycho Nightingale 					    name, (u_int)next, n_indir);
376bf21cd93STycho Nightingale 					return (-1);
377bf21cd93STycho Nightingale 				}
378bf21cd93STycho Nightingale 			}
379bf21cd93STycho Nightingale 		}
3802b948146SAndy Fiddaman 		if ((vdir->flags & VRING_DESC_F_NEXT) == 0)
381b0de25cbSAndy Fiddaman 			goto done;
382bf21cd93STycho Nightingale 	}
383b0de25cbSAndy Fiddaman 
384bf21cd93STycho Nightingale loopy:
385154972afSPatrick Mooney 	EPRINTLN(
386154972afSPatrick Mooney 	    "%s: descriptor loop? count > %d - driver confused?",
387bf21cd93STycho Nightingale 	    name, i);
388bf21cd93STycho Nightingale 	return (-1);
389b0de25cbSAndy Fiddaman 
390b0de25cbSAndy Fiddaman done:
391b0de25cbSAndy Fiddaman 	*reqp = req;
392b0de25cbSAndy Fiddaman 	return (i);
393bf21cd93STycho Nightingale }
394bf21cd93STycho Nightingale 
395bf21cd93STycho Nightingale /*
396154972afSPatrick Mooney  * Return the first n_chain request chains back to the available queue.
397bf21cd93STycho Nightingale  *
398154972afSPatrick Mooney  * (These chains are the ones you handled when you called vq_getchain()
399bf21cd93STycho Nightingale  * and used its positive return value.)
400bf21cd93STycho Nightingale  */
401bf21cd93STycho Nightingale void
vq_retchains(struct vqueue_info * vq,uint16_t n_chains)402154972afSPatrick Mooney vq_retchains(struct vqueue_info *vq, uint16_t n_chains)
403bf21cd93STycho Nightingale {
4044c87aefeSPatrick Mooney 
405154972afSPatrick Mooney 	vq->vq_last_avail -= n_chains;
406154972afSPatrick Mooney }
407154972afSPatrick Mooney 
408154972afSPatrick Mooney void
vq_relchain_prepare(struct vqueue_info * vq,uint16_t idx,uint32_t iolen)409154972afSPatrick Mooney vq_relchain_prepare(struct vqueue_info *vq, uint16_t idx, uint32_t iolen)
410154972afSPatrick Mooney {
41159d65d31SAndy Fiddaman 	struct vring_used *vuh;
41259d65d31SAndy Fiddaman 	struct vring_used_elem *vue;
413154972afSPatrick Mooney 	uint16_t mask;
414154972afSPatrick Mooney 
415154972afSPatrick Mooney 	/*
416154972afSPatrick Mooney 	 * Notes:
417154972afSPatrick Mooney 	 *  - mask is N-1 where N is a power of 2 so computes x % N
418154972afSPatrick Mooney 	 *  - vuh points to the "used" data shared with guest
419154972afSPatrick Mooney 	 *  - vue points to the "used" ring entry we want to update
420154972afSPatrick Mooney 	 */
421154972afSPatrick Mooney 	mask = vq->vq_qsize - 1;
422154972afSPatrick Mooney 	vuh = vq->vq_used;
423154972afSPatrick Mooney 
4242b948146SAndy Fiddaman 	vue = &vuh->ring[vq->vq_next_used++ & mask];
4252b948146SAndy Fiddaman 	vue->id = idx;
4262b948146SAndy Fiddaman 	vue->len = iolen;
427154972afSPatrick Mooney }
428154972afSPatrick Mooney 
429154972afSPatrick Mooney void
vq_relchain_publish(struct vqueue_info * vq)430154972afSPatrick Mooney vq_relchain_publish(struct vqueue_info *vq)
431154972afSPatrick Mooney {
432154972afSPatrick Mooney 	/*
433154972afSPatrick Mooney 	 * Ensure the used descriptor is visible before updating the index.
434154972afSPatrick Mooney 	 * This is necessary on ISAs with memory ordering less strict than x86
435154972afSPatrick Mooney 	 * (and even on x86 to act as a compiler barrier).
436154972afSPatrick Mooney 	 */
437154972afSPatrick Mooney 	atomic_thread_fence_rel();
4382b948146SAndy Fiddaman 	vq->vq_used->idx = vq->vq_next_used;
4394c87aefeSPatrick Mooney }
4404c87aefeSPatrick Mooney 
4414c87aefeSPatrick Mooney /*
4424c87aefeSPatrick Mooney  * Return specified request chain to the guest, setting its I/O length
4434c87aefeSPatrick Mooney  * to the provided value.
4444c87aefeSPatrick Mooney  *
4454c87aefeSPatrick Mooney  * (This chain is the one you handled when you called vq_getchain()
4464c87aefeSPatrick Mooney  * and used its positive return value.)
4474c87aefeSPatrick Mooney  */
4484c87aefeSPatrick Mooney void
vq_relchain(struct vqueue_info * vq,uint16_t idx,uint32_t iolen)4494c87aefeSPatrick Mooney vq_relchain(struct vqueue_info *vq, uint16_t idx, uint32_t iolen)
4504c87aefeSPatrick Mooney {
451154972afSPatrick Mooney 	vq_relchain_prepare(vq, idx, iolen);
452154972afSPatrick Mooney 	vq_relchain_publish(vq);
453bf21cd93STycho Nightingale }
454bf21cd93STycho Nightingale 
455bf21cd93STycho Nightingale /*
456bf21cd93STycho Nightingale  * Driver has finished processing "available" chains and calling
457bf21cd93STycho Nightingale  * vq_relchain on each one.  If driver used all the available
458bf21cd93STycho Nightingale  * chains, used_all should be set.
459bf21cd93STycho Nightingale  *
460bf21cd93STycho Nightingale  * If the "used" index moved we may need to inform the guest, i.e.,
461bf21cd93STycho Nightingale  * deliver an interrupt.  Even if the used index did NOT move we
462bf21cd93STycho Nightingale  * may need to deliver an interrupt, if the avail ring is empty and
463bf21cd93STycho Nightingale  * we are supposed to interrupt on empty.
464bf21cd93STycho Nightingale  *
465bf21cd93STycho Nightingale  * Note that used_all_avail is provided by the caller because it's
466bf21cd93STycho Nightingale  * a snapshot of the ring state when he decided to finish interrupt
467bf21cd93STycho Nightingale  * processing -- it's possible that descriptors became available after
468bf21cd93STycho Nightingale  * that point.  (It's also typically a constant 1/True as well.)
469bf21cd93STycho Nightingale  */
470bf21cd93STycho Nightingale void
vq_endchains(struct vqueue_info * vq,int used_all_avail)471bf21cd93STycho Nightingale vq_endchains(struct vqueue_info *vq, int used_all_avail)
472bf21cd93STycho Nightingale {
473bf21cd93STycho Nightingale 	struct virtio_softc *vs;
474bf21cd93STycho Nightingale 	uint16_t event_idx, new_idx, old_idx;
475bf21cd93STycho Nightingale 	int intr;
476bf21cd93STycho Nightingale 
477bf21cd93STycho Nightingale 	/*
478bf21cd93STycho Nightingale 	 * Interrupt generation: if we're using EVENT_IDX,
479bf21cd93STycho Nightingale 	 * interrupt if we've crossed the event threshold.
480bf21cd93STycho Nightingale 	 * Otherwise interrupt is generated if we added "used" entries,
481bf21cd93STycho Nightingale 	 * but suppressed by VRING_AVAIL_F_NO_INTERRUPT.
482bf21cd93STycho Nightingale 	 *
483bf21cd93STycho Nightingale 	 * In any case, though, if NOTIFY_ON_EMPTY is set and the
484bf21cd93STycho Nightingale 	 * entire avail was processed, we need to interrupt always.
485bf21cd93STycho Nightingale 	 */
486bf21cd93STycho Nightingale 	vs = vq->vq_vs;
487bf21cd93STycho Nightingale 	old_idx = vq->vq_save_used;
4882b948146SAndy Fiddaman 	vq->vq_save_used = new_idx = vq->vq_used->idx;
4894c87aefeSPatrick Mooney 
4904c87aefeSPatrick Mooney 	/*
4912b948146SAndy Fiddaman 	 * Use full memory barrier between "idx" store from preceding
4924c87aefeSPatrick Mooney 	 * vq_relchain() call and the loads from VQ_USED_EVENT_IDX() or
4932b948146SAndy Fiddaman 	 * "flags" field below.
4944c87aefeSPatrick Mooney 	 */
4954c87aefeSPatrick Mooney 	atomic_thread_fence_seq_cst();
496bf21cd93STycho Nightingale 	if (used_all_avail &&
497bf21cd93STycho Nightingale 	    (vs->vs_negotiated_caps & VIRTIO_F_NOTIFY_ON_EMPTY))
498bf21cd93STycho Nightingale 		intr = 1;
499bf21cd93STycho Nightingale 	else if (vs->vs_negotiated_caps & VIRTIO_RING_F_EVENT_IDX) {
500bf21cd93STycho Nightingale 		event_idx = VQ_USED_EVENT_IDX(vq);
501bf21cd93STycho Nightingale 		/*
502bf21cd93STycho Nightingale 		 * This calculation is per docs and the kernel
503bf21cd93STycho Nightingale 		 * (see src/sys/dev/virtio/virtio_ring.h).
504bf21cd93STycho Nightingale 		 */
505bf21cd93STycho Nightingale 		intr = (uint16_t)(new_idx - event_idx - 1) <
506bf21cd93STycho Nightingale 			(uint16_t)(new_idx - old_idx);
507bf21cd93STycho Nightingale 	} else {
508bf21cd93STycho Nightingale 		intr = new_idx != old_idx &&
5092b948146SAndy Fiddaman 		    !(vq->vq_avail->flags & VRING_AVAIL_F_NO_INTERRUPT);
510bf21cd93STycho Nightingale 	}
511bf21cd93STycho Nightingale 	if (intr)
512bf21cd93STycho Nightingale 		vq_interrupt(vs, vq);
513bf21cd93STycho Nightingale }
514bf21cd93STycho Nightingale 
515bf21cd93STycho Nightingale /* Note: these are in sorted order to make for a fast search */
516bf21cd93STycho Nightingale static struct config_reg {
517bf21cd93STycho Nightingale 	uint16_t	cr_offset;	/* register offset */
518bf21cd93STycho Nightingale 	uint8_t		cr_size;	/* size (bytes) */
519bf21cd93STycho Nightingale 	uint8_t		cr_ro;		/* true => reg is read only */
520bf21cd93STycho Nightingale 	const char	*cr_name;	/* name of reg */
521bf21cd93STycho Nightingale } config_regs[] = {
5222b948146SAndy Fiddaman 	{ VIRTIO_PCI_HOST_FEATURES,	4, 1, "HOST_FEATURES" },
5232b948146SAndy Fiddaman 	{ VIRTIO_PCI_GUEST_FEATURES,	4, 0, "GUEST_FEATURES" },
5242b948146SAndy Fiddaman 	{ VIRTIO_PCI_QUEUE_PFN,		4, 0, "QUEUE_PFN" },
5252b948146SAndy Fiddaman 	{ VIRTIO_PCI_QUEUE_NUM,		2, 1, "QUEUE_NUM" },
5262b948146SAndy Fiddaman 	{ VIRTIO_PCI_QUEUE_SEL,		2, 0, "QUEUE_SEL" },
5272b948146SAndy Fiddaman 	{ VIRTIO_PCI_QUEUE_NOTIFY,	2, 0, "QUEUE_NOTIFY" },
5282b948146SAndy Fiddaman 	{ VIRTIO_PCI_STATUS,		1, 0, "STATUS" },
5292b948146SAndy Fiddaman 	{ VIRTIO_PCI_ISR,		1, 0, "ISR" },
5302b948146SAndy Fiddaman 	{ VIRTIO_MSI_CONFIG_VECTOR,	2, 0, "CONFIG_VECTOR" },
5312b948146SAndy Fiddaman 	{ VIRTIO_MSI_QUEUE_VECTOR,	2, 0, "QUEUE_VECTOR" },
532bf21cd93STycho Nightingale };
533bf21cd93STycho Nightingale 
534bf21cd93STycho Nightingale static inline struct config_reg *
vi_find_cr(int offset)535bf21cd93STycho Nightingale vi_find_cr(int offset) {
536bf21cd93STycho Nightingale 	u_int hi, lo, mid;
537bf21cd93STycho Nightingale 	struct config_reg *cr;
538bf21cd93STycho Nightingale 
539bf21cd93STycho Nightingale 	lo = 0;
540bf21cd93STycho Nightingale 	hi = sizeof(config_regs) / sizeof(*config_regs) - 1;
541bf21cd93STycho Nightingale 	while (hi >= lo) {
542bf21cd93STycho Nightingale 		mid = (hi + lo) >> 1;
543bf21cd93STycho Nightingale 		cr = &config_regs[mid];
544bf21cd93STycho Nightingale 		if (cr->cr_offset == offset)
545bf21cd93STycho Nightingale 			return (cr);
546bf21cd93STycho Nightingale 		if (cr->cr_offset < offset)
547bf21cd93STycho Nightingale 			lo = mid + 1;
548bf21cd93STycho Nightingale 		else
549bf21cd93STycho Nightingale 			hi = mid - 1;
550bf21cd93STycho Nightingale 	}
551bf21cd93STycho Nightingale 	return (NULL);
552bf21cd93STycho Nightingale }
553bf21cd93STycho Nightingale 
554bf21cd93STycho Nightingale /*
555bf21cd93STycho Nightingale  * Handle pci config space reads.
556bf21cd93STycho Nightingale  * If it's to the MSI-X info, do that.
557bf21cd93STycho Nightingale  * If it's part of the virtio standard stuff, do that.
558bf21cd93STycho Nightingale  * Otherwise dispatch to the actual driver.
559bf21cd93STycho Nightingale  */
560bf21cd93STycho Nightingale uint64_t
vi_pci_read(struct pci_devinst * pi,int baridx,uint64_t offset,int size)561*32640292SAndy Fiddaman vi_pci_read(struct pci_devinst *pi, int baridx, uint64_t offset, int size)
562bf21cd93STycho Nightingale {
563bf21cd93STycho Nightingale 	struct virtio_softc *vs = pi->pi_arg;
564bf21cd93STycho Nightingale 	struct virtio_consts *vc;
565bf21cd93STycho Nightingale 	struct config_reg *cr;
566bf21cd93STycho Nightingale 	uint64_t virtio_config_size, max;
567bf21cd93STycho Nightingale 	const char *name;
568bf21cd93STycho Nightingale 	uint32_t newoff;
569bf21cd93STycho Nightingale 	uint32_t value;
570bf21cd93STycho Nightingale 	int error;
571bf21cd93STycho Nightingale 
572bf21cd93STycho Nightingale 	if (vs->vs_flags & VIRTIO_USE_MSIX) {
573bf21cd93STycho Nightingale 		if (baridx == pci_msix_table_bar(pi) ||
574bf21cd93STycho Nightingale 		    baridx == pci_msix_pba_bar(pi)) {
575bf21cd93STycho Nightingale 			return (pci_emul_msix_tread(pi, offset, size));
576bf21cd93STycho Nightingale 		}
577bf21cd93STycho Nightingale 	}
578bf21cd93STycho Nightingale 
579bf21cd93STycho Nightingale 	/* XXX probably should do something better than just assert() */
580bf21cd93STycho Nightingale 	assert(baridx == 0);
581bf21cd93STycho Nightingale 
582bf21cd93STycho Nightingale 	if (vs->vs_mtx)
583bf21cd93STycho Nightingale 		pthread_mutex_lock(vs->vs_mtx);
584bf21cd93STycho Nightingale 
585bf21cd93STycho Nightingale 	vc = vs->vs_vc;
586bf21cd93STycho Nightingale 	name = vc->vc_name;
587bf21cd93STycho Nightingale 	value = size == 1 ? 0xff : size == 2 ? 0xffff : 0xffffffff;
588bf21cd93STycho Nightingale 
589bf21cd93STycho Nightingale 	if (size != 1 && size != 2 && size != 4)
590bf21cd93STycho Nightingale 		goto bad;
591bf21cd93STycho Nightingale 
5922b948146SAndy Fiddaman 	virtio_config_size = VIRTIO_PCI_CONFIG_OFF(pci_msix_enabled(pi));
593bf21cd93STycho Nightingale 
594bf21cd93STycho Nightingale 	if (offset >= virtio_config_size) {
595bf21cd93STycho Nightingale 		/*
596bf21cd93STycho Nightingale 		 * Subtract off the standard size (including MSI-X
597bf21cd93STycho Nightingale 		 * registers if enabled) and dispatch to underlying driver.
598bf21cd93STycho Nightingale 		 * If that fails, fall into general code.
599bf21cd93STycho Nightingale 		 */
600bf21cd93STycho Nightingale 		newoff = offset - virtio_config_size;
601bf21cd93STycho Nightingale 		max = vc->vc_cfgsize ? vc->vc_cfgsize : 0x100000000;
602bf21cd93STycho Nightingale 		if (newoff + size > max)
603bf21cd93STycho Nightingale 			goto bad;
604b0de25cbSAndy Fiddaman 		if (vc->vc_cfgread != NULL)
605bf21cd93STycho Nightingale 			error = (*vc->vc_cfgread)(DEV_SOFTC(vs), newoff, size, &value);
606b0de25cbSAndy Fiddaman 		else
607b0de25cbSAndy Fiddaman 			error = 0;
608bf21cd93STycho Nightingale 		if (!error)
609bf21cd93STycho Nightingale 			goto done;
610bf21cd93STycho Nightingale 	}
611bf21cd93STycho Nightingale 
612bf21cd93STycho Nightingale bad:
613bf21cd93STycho Nightingale 	cr = vi_find_cr(offset);
614bf21cd93STycho Nightingale 	if (cr == NULL || cr->cr_size != size) {
615bf21cd93STycho Nightingale 		if (cr != NULL) {
616bf21cd93STycho Nightingale 			/* offset must be OK, so size must be bad */
617154972afSPatrick Mooney 			EPRINTLN(
618154972afSPatrick Mooney 			    "%s: read from %s: bad size %d",
619bf21cd93STycho Nightingale 			    name, cr->cr_name, size);
620bf21cd93STycho Nightingale 		} else {
621154972afSPatrick Mooney 			EPRINTLN(
622154972afSPatrick Mooney 			    "%s: read from bad offset/size %jd/%d",
623bf21cd93STycho Nightingale 			    name, (uintmax_t)offset, size);
624bf21cd93STycho Nightingale 		}
625bf21cd93STycho Nightingale 		goto done;
626bf21cd93STycho Nightingale 	}
627bf21cd93STycho Nightingale 
628bf21cd93STycho Nightingale 	switch (offset) {
6292b948146SAndy Fiddaman 	case VIRTIO_PCI_HOST_FEATURES:
630bf21cd93STycho Nightingale 		value = vc->vc_hv_caps;
631bf21cd93STycho Nightingale 		break;
6322b948146SAndy Fiddaman 	case VIRTIO_PCI_GUEST_FEATURES:
633bf21cd93STycho Nightingale 		value = vs->vs_negotiated_caps;
634bf21cd93STycho Nightingale 		break;
6352b948146SAndy Fiddaman 	case VIRTIO_PCI_QUEUE_PFN:
636bf21cd93STycho Nightingale 		if (vs->vs_curq < vc->vc_nvq)
637bf21cd93STycho Nightingale 			value = vs->vs_queues[vs->vs_curq].vq_pfn;
638bf21cd93STycho Nightingale 		break;
6392b948146SAndy Fiddaman 	case VIRTIO_PCI_QUEUE_NUM:
640bf21cd93STycho Nightingale 		value = vs->vs_curq < vc->vc_nvq ?
641bf21cd93STycho Nightingale 		    vs->vs_queues[vs->vs_curq].vq_qsize : 0;
642bf21cd93STycho Nightingale 		break;
6432b948146SAndy Fiddaman 	case VIRTIO_PCI_QUEUE_SEL:
644bf21cd93STycho Nightingale 		value = vs->vs_curq;
645bf21cd93STycho Nightingale 		break;
6462b948146SAndy Fiddaman 	case VIRTIO_PCI_QUEUE_NOTIFY:
647bf21cd93STycho Nightingale 		value = 0;	/* XXX */
648bf21cd93STycho Nightingale 		break;
6492b948146SAndy Fiddaman 	case VIRTIO_PCI_STATUS:
650bf21cd93STycho Nightingale 		value = vs->vs_status;
651bf21cd93STycho Nightingale 		break;
6522b948146SAndy Fiddaman 	case VIRTIO_PCI_ISR:
653bf21cd93STycho Nightingale 		value = vs->vs_isr;
654bf21cd93STycho Nightingale 		vs->vs_isr = 0;		/* a read clears this flag */
655bf21cd93STycho Nightingale 		if (value)
656bf21cd93STycho Nightingale 			pci_lintr_deassert(pi);
657bf21cd93STycho Nightingale 		break;
6582b948146SAndy Fiddaman 	case VIRTIO_MSI_CONFIG_VECTOR:
659bf21cd93STycho Nightingale 		value = vs->vs_msix_cfg_idx;
660bf21cd93STycho Nightingale 		break;
6612b948146SAndy Fiddaman 	case VIRTIO_MSI_QUEUE_VECTOR:
662bf21cd93STycho Nightingale 		value = vs->vs_curq < vc->vc_nvq ?
663bf21cd93STycho Nightingale 		    vs->vs_queues[vs->vs_curq].vq_msix_idx :
664bf21cd93STycho Nightingale 		    VIRTIO_MSI_NO_VECTOR;
665bf21cd93STycho Nightingale 		break;
666bf21cd93STycho Nightingale 	}
667bf21cd93STycho Nightingale done:
668bf21cd93STycho Nightingale 	if (vs->vs_mtx)
669bf21cd93STycho Nightingale 		pthread_mutex_unlock(vs->vs_mtx);
670bf21cd93STycho Nightingale 	return (value);
671bf21cd93STycho Nightingale }
672bf21cd93STycho Nightingale 
673bf21cd93STycho Nightingale /*
674bf21cd93STycho Nightingale  * Handle pci config space writes.
675bf21cd93STycho Nightingale  * If it's to the MSI-X info, do that.
676bf21cd93STycho Nightingale  * If it's part of the virtio standard stuff, do that.
677bf21cd93STycho Nightingale  * Otherwise dispatch to the actual driver.
678bf21cd93STycho Nightingale  */
679bf21cd93STycho Nightingale void
vi_pci_write(struct pci_devinst * pi,int baridx,uint64_t offset,int size,uint64_t value)680*32640292SAndy Fiddaman vi_pci_write(struct pci_devinst *pi, int baridx, uint64_t offset, int size,
68159d65d31SAndy Fiddaman     uint64_t value)
682bf21cd93STycho Nightingale {
683bf21cd93STycho Nightingale 	struct virtio_softc *vs = pi->pi_arg;
684bf21cd93STycho Nightingale 	struct vqueue_info *vq;
685bf21cd93STycho Nightingale 	struct virtio_consts *vc;
686bf21cd93STycho Nightingale 	struct config_reg *cr;
687bf21cd93STycho Nightingale 	uint64_t virtio_config_size, max;
688bf21cd93STycho Nightingale 	const char *name;
689bf21cd93STycho Nightingale 	uint32_t newoff;
690bf21cd93STycho Nightingale 	int error;
691bf21cd93STycho Nightingale 
692bf21cd93STycho Nightingale 	if (vs->vs_flags & VIRTIO_USE_MSIX) {
693bf21cd93STycho Nightingale 		if (baridx == pci_msix_table_bar(pi) ||
694bf21cd93STycho Nightingale 		    baridx == pci_msix_pba_bar(pi)) {
695bf21cd93STycho Nightingale 			pci_emul_msix_twrite(pi, offset, size, value);
696bf21cd93STycho Nightingale 			return;
697bf21cd93STycho Nightingale 		}
698bf21cd93STycho Nightingale 	}
699bf21cd93STycho Nightingale 
700bf21cd93STycho Nightingale 	/* XXX probably should do something better than just assert() */
701bf21cd93STycho Nightingale 	assert(baridx == 0);
702bf21cd93STycho Nightingale 
703bf21cd93STycho Nightingale 	if (vs->vs_mtx)
704bf21cd93STycho Nightingale 		pthread_mutex_lock(vs->vs_mtx);
705bf21cd93STycho Nightingale 
706bf21cd93STycho Nightingale 	vc = vs->vs_vc;
707bf21cd93STycho Nightingale 	name = vc->vc_name;
708bf21cd93STycho Nightingale 
709bf21cd93STycho Nightingale 	if (size != 1 && size != 2 && size != 4)
710bf21cd93STycho Nightingale 		goto bad;
711bf21cd93STycho Nightingale 
7122b948146SAndy Fiddaman 	virtio_config_size = VIRTIO_PCI_CONFIG_OFF(pci_msix_enabled(pi));
713bf21cd93STycho Nightingale 
714bf21cd93STycho Nightingale 	if (offset >= virtio_config_size) {
715bf21cd93STycho Nightingale 		/*
716bf21cd93STycho Nightingale 		 * Subtract off the standard size (including MSI-X
717bf21cd93STycho Nightingale 		 * registers if enabled) and dispatch to underlying driver.
718bf21cd93STycho Nightingale 		 */
719bf21cd93STycho Nightingale 		newoff = offset - virtio_config_size;
720bf21cd93STycho Nightingale 		max = vc->vc_cfgsize ? vc->vc_cfgsize : 0x100000000;
721bf21cd93STycho Nightingale 		if (newoff + size > max)
722bf21cd93STycho Nightingale 			goto bad;
723b0de25cbSAndy Fiddaman 		if (vc->vc_cfgwrite != NULL)
724bf21cd93STycho Nightingale 			error = (*vc->vc_cfgwrite)(DEV_SOFTC(vs), newoff, size, value);
725b0de25cbSAndy Fiddaman 		else
726b0de25cbSAndy Fiddaman 			error = 0;
727bf21cd93STycho Nightingale 		if (!error)
728bf21cd93STycho Nightingale 			goto done;
729bf21cd93STycho Nightingale 	}
730bf21cd93STycho Nightingale 
731bf21cd93STycho Nightingale bad:
732bf21cd93STycho Nightingale 	cr = vi_find_cr(offset);
733bf21cd93STycho Nightingale 	if (cr == NULL || cr->cr_size != size || cr->cr_ro) {
734bf21cd93STycho Nightingale 		if (cr != NULL) {
735bf21cd93STycho Nightingale 			/* offset must be OK, wrong size and/or reg is R/O */
736bf21cd93STycho Nightingale 			if (cr->cr_size != size)
737154972afSPatrick Mooney 				EPRINTLN(
738154972afSPatrick Mooney 				    "%s: write to %s: bad size %d",
739bf21cd93STycho Nightingale 				    name, cr->cr_name, size);
740bf21cd93STycho Nightingale 			if (cr->cr_ro)
741154972afSPatrick Mooney 				EPRINTLN(
742154972afSPatrick Mooney 				    "%s: write to read-only reg %s",
743bf21cd93STycho Nightingale 				    name, cr->cr_name);
744bf21cd93STycho Nightingale 		} else {
745154972afSPatrick Mooney 			EPRINTLN(
746154972afSPatrick Mooney 			    "%s: write to bad offset/size %jd/%d",
747bf21cd93STycho Nightingale 			    name, (uintmax_t)offset, size);
748bf21cd93STycho Nightingale 		}
749bf21cd93STycho Nightingale 		goto done;
750bf21cd93STycho Nightingale 	}
751bf21cd93STycho Nightingale 
752bf21cd93STycho Nightingale 	switch (offset) {
7532b948146SAndy Fiddaman 	case VIRTIO_PCI_GUEST_FEATURES:
754bf21cd93STycho Nightingale 		vs->vs_negotiated_caps = value & vc->vc_hv_caps;
7554c87aefeSPatrick Mooney 		if (vc->vc_apply_features)
7564c87aefeSPatrick Mooney 			(*vc->vc_apply_features)(DEV_SOFTC(vs),
7574c87aefeSPatrick Mooney 			    vs->vs_negotiated_caps);
758bf21cd93STycho Nightingale 		break;
7592b948146SAndy Fiddaman 	case VIRTIO_PCI_QUEUE_PFN:
760bf21cd93STycho Nightingale 		if (vs->vs_curq >= vc->vc_nvq)
761bf21cd93STycho Nightingale 			goto bad_qindex;
762bf21cd93STycho Nightingale 		vi_vq_init(vs, value);
763bf21cd93STycho Nightingale 		break;
7642b948146SAndy Fiddaman 	case VIRTIO_PCI_QUEUE_SEL:
765bf21cd93STycho Nightingale 		/*
766bf21cd93STycho Nightingale 		 * Note that the guest is allowed to select an
767bf21cd93STycho Nightingale 		 * invalid queue; we just need to return a QNUM
768bf21cd93STycho Nightingale 		 * of 0 while the bad queue is selected.
769bf21cd93STycho Nightingale 		 */
770bf21cd93STycho Nightingale 		vs->vs_curq = value;
771bf21cd93STycho Nightingale 		break;
7722b948146SAndy Fiddaman 	case VIRTIO_PCI_QUEUE_NOTIFY:
77359d65d31SAndy Fiddaman 		if (value >= (unsigned int)vc->vc_nvq) {
774154972afSPatrick Mooney 			EPRINTLN("%s: queue %d notify out of range",
775bf21cd93STycho Nightingale 				name, (int)value);
776bf21cd93STycho Nightingale 			goto done;
777bf21cd93STycho Nightingale 		}
778bf21cd93STycho Nightingale 		vq = &vs->vs_queues[value];
779bf21cd93STycho Nightingale 		if (vq->vq_notify)
780bf21cd93STycho Nightingale 			(*vq->vq_notify)(DEV_SOFTC(vs), vq);
781bf21cd93STycho Nightingale 		else if (vc->vc_qnotify)
782bf21cd93STycho Nightingale 			(*vc->vc_qnotify)(DEV_SOFTC(vs), vq);
783bf21cd93STycho Nightingale 		else
784154972afSPatrick Mooney 			EPRINTLN(
785154972afSPatrick Mooney 			    "%s: qnotify queue %d: missing vq/vc notify",
786bf21cd93STycho Nightingale 				name, (int)value);
787bf21cd93STycho Nightingale 		break;
7882b948146SAndy Fiddaman 	case VIRTIO_PCI_STATUS:
789bf21cd93STycho Nightingale 		vs->vs_status = value;
790bf21cd93STycho Nightingale 		if (value == 0)
791bf21cd93STycho Nightingale 			(*vc->vc_reset)(DEV_SOFTC(vs));
792bf21cd93STycho Nightingale 		break;
7932b948146SAndy Fiddaman 	case VIRTIO_MSI_CONFIG_VECTOR:
794bf21cd93STycho Nightingale 		vs->vs_msix_cfg_idx = value;
795bf21cd93STycho Nightingale 		break;
7962b948146SAndy Fiddaman 	case VIRTIO_MSI_QUEUE_VECTOR:
797bf21cd93STycho Nightingale 		if (vs->vs_curq >= vc->vc_nvq)
798bf21cd93STycho Nightingale 			goto bad_qindex;
799bf21cd93STycho Nightingale 		vq = &vs->vs_queues[vs->vs_curq];
800bf21cd93STycho Nightingale 		vq->vq_msix_idx = value;
801bf21cd93STycho Nightingale 		break;
802bf21cd93STycho Nightingale 	}
803bf21cd93STycho Nightingale 	goto done;
804bf21cd93STycho Nightingale 
805bf21cd93STycho Nightingale bad_qindex:
806154972afSPatrick Mooney 	EPRINTLN(
807154972afSPatrick Mooney 	    "%s: write config reg %s: curq %d >= max %d",
808bf21cd93STycho Nightingale 	    name, cr->cr_name, vs->vs_curq, vc->vc_nvq);
809bf21cd93STycho Nightingale done:
810bf21cd93STycho Nightingale 	if (vs->vs_mtx)
811bf21cd93STycho Nightingale 		pthread_mutex_unlock(vs->vs_mtx);
812bf21cd93STycho Nightingale }
813