xref: /freebsd/sys/dev/virtio/pci/virtio_pci_legacy.c (revision 3332f1b444d4a73238e9f59cca27bfc95fe936bd)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2011, Bryan Venteicher <bryanv@FreeBSD.org>
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 unmodified, this list of conditions, and the following
12  *    disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 /* Driver for the legacy VirtIO PCI interface. */
30 
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/bus.h>
37 #include <sys/lock.h>
38 #include <sys/kernel.h>
39 #include <sys/module.h>
40 #include <sys/endian.h>
41 
42 #include <machine/bus.h>
43 #include <machine/resource.h>
44 #include <sys/bus.h>
45 #include <sys/rman.h>
46 
47 #include <dev/pci/pcivar.h>
48 #include <dev/pci/pcireg.h>
49 
50 #include <dev/virtio/virtio.h>
51 #include <dev/virtio/virtqueue.h>
52 #include <dev/virtio/pci/virtio_pci.h>
53 #include <dev/virtio/pci/virtio_pci_legacy_var.h>
54 
55 #include "virtio_bus_if.h"
56 #include "virtio_pci_if.h"
57 #include "virtio_if.h"
58 
59 struct vtpci_legacy_softc {
60 	device_t			 vtpci_dev;
61 	struct vtpci_common		 vtpci_common;
62 	int				 vtpci_res_type;
63 	struct resource			*vtpci_res;
64 	struct resource			*vtpci_msix_table_res;
65 	struct resource			*vtpci_msix_pba_res;
66 };
67 
68 static int	vtpci_legacy_probe(device_t);
69 static int	vtpci_legacy_attach(device_t);
70 static int	vtpci_legacy_detach(device_t);
71 static int	vtpci_legacy_suspend(device_t);
72 static int	vtpci_legacy_resume(device_t);
73 static int	vtpci_legacy_shutdown(device_t);
74 
75 static void	vtpci_legacy_driver_added(device_t, driver_t *);
76 static void	vtpci_legacy_child_detached(device_t, device_t);
77 static int	vtpci_legacy_read_ivar(device_t, device_t, int, uintptr_t *);
78 static int	vtpci_legacy_write_ivar(device_t, device_t, int, uintptr_t);
79 
80 static uint8_t	vtpci_legacy_read_isr(device_t);
81 static uint16_t	vtpci_legacy_get_vq_size(device_t, int);
82 static bus_size_t vtpci_legacy_get_vq_notify_off(device_t, int);
83 static void	vtpci_legacy_set_vq(device_t, struct virtqueue *);
84 static void	vtpci_legacy_disable_vq(device_t, int);
85 static int	vtpci_legacy_register_cfg_msix(device_t,
86 		    struct vtpci_interrupt *);
87 static int	vtpci_legacy_register_vq_msix(device_t, int idx,
88 		    struct vtpci_interrupt *);
89 
90 static uint64_t	vtpci_legacy_negotiate_features(device_t, uint64_t);
91 static int	vtpci_legacy_with_feature(device_t, uint64_t);
92 static int	vtpci_legacy_alloc_virtqueues(device_t, int, int,
93 		    struct vq_alloc_info *);
94 static int	vtpci_legacy_setup_interrupts(device_t, enum intr_type);
95 static void	vtpci_legacy_stop(device_t);
96 static int	vtpci_legacy_reinit(device_t, uint64_t);
97 static void	vtpci_legacy_reinit_complete(device_t);
98 static void	vtpci_legacy_notify_vq(device_t, uint16_t, bus_size_t);
99 static void	vtpci_legacy_read_dev_config(device_t, bus_size_t, void *, int);
100 static void	vtpci_legacy_write_dev_config(device_t, bus_size_t, const void *, int);
101 
102 static bool	vtpci_legacy_setup_msix(struct vtpci_legacy_softc *sc);
103 static void	vtpci_legacy_teardown_msix(struct vtpci_legacy_softc *sc);
104 static int	vtpci_legacy_alloc_resources(struct vtpci_legacy_softc *);
105 static void	vtpci_legacy_free_resources(struct vtpci_legacy_softc *);
106 
107 static void	vtpci_legacy_probe_and_attach_child(struct vtpci_legacy_softc *);
108 
109 static uint8_t	vtpci_legacy_get_status(struct vtpci_legacy_softc *);
110 static void	vtpci_legacy_set_status(struct vtpci_legacy_softc *, uint8_t);
111 static void	vtpci_legacy_select_virtqueue(struct vtpci_legacy_softc *, int);
112 static void	vtpci_legacy_reset(struct vtpci_legacy_softc *);
113 
114 #define VIRTIO_PCI_LEGACY_CONFIG(_sc) \
115     VIRTIO_PCI_CONFIG_OFF(vtpci_is_msix_enabled(&(_sc)->vtpci_common))
116 
117 #define vtpci_legacy_read_config_1(sc, o) \
118     bus_read_1((sc)->vtpci_res, (o))
119 #define vtpci_legacy_write_config_1(sc, o, v) \
120     bus_write_1((sc)->vtpci_res, (o), (v))
121 /*
122  * VirtIO specifies that PCI Configuration area is guest endian. However,
123  * since PCI devices are inherently little-endian, on big-endian systems
124  * the bus layer transparently converts it to BE. For virtio-legacy, this
125  * conversion is undesired, so an extra byte swap is required to fix it.
126  */
127 #define vtpci_legacy_read_config_2(sc, o) \
128     le16toh(bus_read_2((sc)->vtpci_res, (o)))
129 #define vtpci_legacy_read_config_4(sc, o) \
130     le32toh(bus_read_4((sc)->vtpci_res, (o)))
131 #define vtpci_legacy_write_config_2(sc, o, v) \
132     bus_write_2((sc)->vtpci_res, (o), (htole16(v)))
133 #define vtpci_legacy_write_config_4(sc, o, v) \
134     bus_write_4((sc)->vtpci_res, (o), (htole32(v)))
135 /* PCI Header LE. On BE systems the bus layer takes care of byte swapping. */
136 #define vtpci_legacy_read_header_2(sc, o) \
137     bus_read_2((sc)->vtpci_res, (o))
138 #define vtpci_legacy_read_header_4(sc, o) \
139     bus_read_4((sc)->vtpci_res, (o))
140 #define vtpci_legacy_write_header_2(sc, o, v) \
141     bus_write_2((sc)->vtpci_res, (o), (v))
142 #define vtpci_legacy_write_header_4(sc, o, v) \
143     bus_write_4((sc)->vtpci_res, (o), (v))
144 
145 static device_method_t vtpci_legacy_methods[] = {
146 	/* Device interface. */
147 	DEVMETHOD(device_probe,			  vtpci_legacy_probe),
148 	DEVMETHOD(device_attach,		  vtpci_legacy_attach),
149 	DEVMETHOD(device_detach,		  vtpci_legacy_detach),
150 	DEVMETHOD(device_suspend,		  vtpci_legacy_suspend),
151 	DEVMETHOD(device_resume,		  vtpci_legacy_resume),
152 	DEVMETHOD(device_shutdown,		  vtpci_legacy_shutdown),
153 
154 	/* Bus interface. */
155 	DEVMETHOD(bus_driver_added,		  vtpci_legacy_driver_added),
156 	DEVMETHOD(bus_child_detached,		  vtpci_legacy_child_detached),
157 	DEVMETHOD(bus_child_pnpinfo,		  virtio_child_pnpinfo),
158 	DEVMETHOD(bus_read_ivar,		  vtpci_legacy_read_ivar),
159 	DEVMETHOD(bus_write_ivar,		  vtpci_legacy_write_ivar),
160 
161 	/* VirtIO PCI interface. */
162 	DEVMETHOD(virtio_pci_read_isr,		 vtpci_legacy_read_isr),
163 	DEVMETHOD(virtio_pci_get_vq_size,	 vtpci_legacy_get_vq_size),
164 	DEVMETHOD(virtio_pci_get_vq_notify_off,	 vtpci_legacy_get_vq_notify_off),
165 	DEVMETHOD(virtio_pci_set_vq,		 vtpci_legacy_set_vq),
166 	DEVMETHOD(virtio_pci_disable_vq,	 vtpci_legacy_disable_vq),
167 	DEVMETHOD(virtio_pci_register_cfg_msix,  vtpci_legacy_register_cfg_msix),
168 	DEVMETHOD(virtio_pci_register_vq_msix,	 vtpci_legacy_register_vq_msix),
169 
170 	/* VirtIO bus interface. */
171 	DEVMETHOD(virtio_bus_negotiate_features,  vtpci_legacy_negotiate_features),
172 	DEVMETHOD(virtio_bus_with_feature,	  vtpci_legacy_with_feature),
173 	DEVMETHOD(virtio_bus_alloc_virtqueues,	  vtpci_legacy_alloc_virtqueues),
174 	DEVMETHOD(virtio_bus_setup_intr,	  vtpci_legacy_setup_interrupts),
175 	DEVMETHOD(virtio_bus_stop,		  vtpci_legacy_stop),
176 	DEVMETHOD(virtio_bus_reinit,		  vtpci_legacy_reinit),
177 	DEVMETHOD(virtio_bus_reinit_complete,	  vtpci_legacy_reinit_complete),
178 	DEVMETHOD(virtio_bus_notify_vq,		  vtpci_legacy_notify_vq),
179 	DEVMETHOD(virtio_bus_read_device_config,  vtpci_legacy_read_dev_config),
180 	DEVMETHOD(virtio_bus_write_device_config, vtpci_legacy_write_dev_config),
181 
182 	DEVMETHOD_END
183 };
184 
185 static driver_t vtpci_legacy_driver = {
186 	.name = "virtio_pci",
187 	.methods = vtpci_legacy_methods,
188 	.size = sizeof(struct vtpci_legacy_softc)
189 };
190 
191 devclass_t vtpci_legacy_devclass;
192 
193 DRIVER_MODULE(virtio_pci_legacy, pci, vtpci_legacy_driver,
194     vtpci_legacy_devclass, 0, 0);
195 
196 static int
197 vtpci_legacy_probe(device_t dev)
198 {
199 	char desc[64];
200 	const char *name;
201 
202 	if (pci_get_vendor(dev) != VIRTIO_PCI_VENDORID)
203 		return (ENXIO);
204 
205 	if (pci_get_device(dev) < VIRTIO_PCI_DEVICEID_MIN ||
206 	    pci_get_device(dev) > VIRTIO_PCI_DEVICEID_LEGACY_MAX)
207 		return (ENXIO);
208 
209 	if (pci_get_revid(dev) != VIRTIO_PCI_ABI_VERSION)
210 		return (ENXIO);
211 
212 	name = virtio_device_name(pci_get_subdevice(dev));
213 	if (name == NULL)
214 		name = "Unknown";
215 
216 	snprintf(desc, sizeof(desc), "VirtIO PCI (legacy) %s adapter", name);
217 	device_set_desc_copy(dev, desc);
218 
219 	/* Prefer transitional modern VirtIO PCI. */
220 	return (BUS_PROBE_LOW_PRIORITY);
221 }
222 
223 static int
224 vtpci_legacy_attach(device_t dev)
225 {
226 	struct vtpci_legacy_softc *sc;
227 	int error;
228 
229 	sc = device_get_softc(dev);
230 	sc->vtpci_dev = dev;
231 	vtpci_init(&sc->vtpci_common, dev, false);
232 
233 	error = vtpci_legacy_alloc_resources(sc);
234 	if (error) {
235 		device_printf(dev, "cannot map I/O space nor memory space\n");
236 		return (error);
237 	}
238 
239 	if (vtpci_is_msix_available(&sc->vtpci_common) &&
240 	    !vtpci_legacy_setup_msix(sc)) {
241 		device_printf(dev, "cannot setup MSI-x resources\n");
242 		error = ENXIO;
243 		goto fail;
244 	}
245 
246 	vtpci_legacy_reset(sc);
247 
248 	/* Tell the host we've noticed this device. */
249 	vtpci_legacy_set_status(sc, VIRTIO_CONFIG_STATUS_ACK);
250 
251 	error = vtpci_add_child(&sc->vtpci_common);
252 	if (error)
253 		goto fail;
254 
255 	vtpci_legacy_probe_and_attach_child(sc);
256 
257 	return (0);
258 
259 fail:
260 	vtpci_legacy_set_status(sc, VIRTIO_CONFIG_STATUS_FAILED);
261 	vtpci_legacy_detach(dev);
262 
263 	return (error);
264 }
265 
266 static int
267 vtpci_legacy_detach(device_t dev)
268 {
269 	struct vtpci_legacy_softc *sc;
270 	int error;
271 
272 	sc = device_get_softc(dev);
273 
274 	error = vtpci_delete_child(&sc->vtpci_common);
275 	if (error)
276 		return (error);
277 
278 	vtpci_legacy_reset(sc);
279 	vtpci_legacy_teardown_msix(sc);
280 	vtpci_legacy_free_resources(sc);
281 
282 	return (0);
283 }
284 
285 static int
286 vtpci_legacy_suspend(device_t dev)
287 {
288 	return (bus_generic_suspend(dev));
289 }
290 
291 static int
292 vtpci_legacy_resume(device_t dev)
293 {
294 	return (bus_generic_resume(dev));
295 }
296 
297 static int
298 vtpci_legacy_shutdown(device_t dev)
299 {
300 	(void) bus_generic_shutdown(dev);
301 	/* Forcibly stop the host device. */
302 	vtpci_legacy_stop(dev);
303 
304 	return (0);
305 }
306 
307 static void
308 vtpci_legacy_driver_added(device_t dev, driver_t *driver)
309 {
310 	vtpci_legacy_probe_and_attach_child(device_get_softc(dev));
311 }
312 
313 static void
314 vtpci_legacy_child_detached(device_t dev, device_t child)
315 {
316 	struct vtpci_legacy_softc *sc;
317 
318 	sc = device_get_softc(dev);
319 
320 	vtpci_legacy_reset(sc);
321 	vtpci_child_detached(&sc->vtpci_common);
322 
323 	/* After the reset, retell the host we've noticed this device. */
324 	vtpci_legacy_set_status(sc, VIRTIO_CONFIG_STATUS_ACK);
325 }
326 
327 static int
328 vtpci_legacy_read_ivar(device_t dev, device_t child, int index,
329     uintptr_t *result)
330 {
331 	struct vtpci_legacy_softc *sc;
332 	struct vtpci_common *cn;
333 
334 	sc = device_get_softc(dev);
335 	cn = &sc->vtpci_common;
336 
337 	if (vtpci_child_device(cn) != child)
338 		return (ENOENT);
339 
340 	switch (index) {
341 	case VIRTIO_IVAR_DEVTYPE:
342 		*result = pci_get_subdevice(dev);
343 		break;
344 	default:
345 		return (vtpci_read_ivar(cn, index, result));
346 	}
347 
348 	return (0);
349 }
350 
351 static int
352 vtpci_legacy_write_ivar(device_t dev, device_t child, int index, uintptr_t value)
353 {
354 	struct vtpci_legacy_softc *sc;
355 	struct vtpci_common *cn;
356 
357 	sc = device_get_softc(dev);
358 	cn = &sc->vtpci_common;
359 
360 	if (vtpci_child_device(cn) != child)
361 		return (ENOENT);
362 
363 	switch (index) {
364 	default:
365 		return (vtpci_write_ivar(cn, index, value));
366 	}
367 
368 	return (0);
369 }
370 
371 static uint64_t
372 vtpci_legacy_negotiate_features(device_t dev, uint64_t child_features)
373 {
374 	struct vtpci_legacy_softc *sc;
375 	uint64_t host_features, features;
376 
377 	sc = device_get_softc(dev);
378 	host_features = vtpci_legacy_read_header_4(sc, VIRTIO_PCI_HOST_FEATURES);
379 
380 	features = vtpci_negotiate_features(&sc->vtpci_common,
381 	    child_features, host_features);
382 	vtpci_legacy_write_header_4(sc, VIRTIO_PCI_GUEST_FEATURES, features);
383 
384 	return (features);
385 }
386 
387 static int
388 vtpci_legacy_with_feature(device_t dev, uint64_t feature)
389 {
390 	struct vtpci_legacy_softc *sc;
391 
392 	sc = device_get_softc(dev);
393 
394 	return (vtpci_with_feature(&sc->vtpci_common, feature));
395 }
396 
397 static int
398 vtpci_legacy_alloc_virtqueues(device_t dev, int flags, int nvqs,
399     struct vq_alloc_info *vq_info)
400 {
401 	struct vtpci_legacy_softc *sc;
402 	struct vtpci_common *cn;
403 
404 	sc = device_get_softc(dev);
405 	cn = &sc->vtpci_common;
406 
407 	return (vtpci_alloc_virtqueues(cn, flags, nvqs, vq_info));
408 }
409 
410 static int
411 vtpci_legacy_setup_interrupts(device_t dev, enum intr_type type)
412 {
413 	struct vtpci_legacy_softc *sc;
414 
415 	sc = device_get_softc(dev);
416 
417 	return (vtpci_setup_interrupts(&sc->vtpci_common, type));
418 }
419 
420 static void
421 vtpci_legacy_stop(device_t dev)
422 {
423 	vtpci_legacy_reset(device_get_softc(dev));
424 }
425 
426 static int
427 vtpci_legacy_reinit(device_t dev, uint64_t features)
428 {
429 	struct vtpci_legacy_softc *sc;
430 	struct vtpci_common *cn;
431 	int error;
432 
433 	sc = device_get_softc(dev);
434 	cn = &sc->vtpci_common;
435 
436 	/*
437 	 * Redrive the device initialization. This is a bit of an abuse of
438 	 * the specification, but VirtualBox, QEMU/KVM, and BHyVe seem to
439 	 * play nice.
440 	 *
441 	 * We do not allow the host device to change from what was originally
442 	 * negotiated beyond what the guest driver changed. MSIX state should
443 	 * not change, number of virtqueues and their size remain the same, etc.
444 	 * This will need to be rethought when we want to support migration.
445 	 */
446 
447 	if (vtpci_legacy_get_status(sc) != VIRTIO_CONFIG_STATUS_RESET)
448 		vtpci_legacy_stop(dev);
449 
450 	/*
451 	 * Quickly drive the status through ACK and DRIVER. The device does
452 	 * not become usable again until DRIVER_OK in reinit complete.
453 	 */
454 	vtpci_legacy_set_status(sc, VIRTIO_CONFIG_STATUS_ACK);
455 	vtpci_legacy_set_status(sc, VIRTIO_CONFIG_STATUS_DRIVER);
456 
457 	vtpci_legacy_negotiate_features(dev, features);
458 
459 	error = vtpci_reinit(cn);
460 	if (error)
461 		return (error);
462 
463 	return (0);
464 }
465 
466 static void
467 vtpci_legacy_reinit_complete(device_t dev)
468 {
469 	struct vtpci_legacy_softc *sc;
470 
471 	sc = device_get_softc(dev);
472 
473 	vtpci_legacy_set_status(sc, VIRTIO_CONFIG_STATUS_DRIVER_OK);
474 }
475 
476 static void
477 vtpci_legacy_notify_vq(device_t dev, uint16_t queue, bus_size_t offset)
478 {
479 	struct vtpci_legacy_softc *sc;
480 
481 	sc = device_get_softc(dev);
482 	MPASS(offset == VIRTIO_PCI_QUEUE_NOTIFY);
483 
484 	vtpci_legacy_write_header_2(sc, offset, queue);
485 }
486 
487 static uint8_t
488 vtpci_legacy_get_status(struct vtpci_legacy_softc *sc)
489 {
490 	return (vtpci_legacy_read_config_1(sc, VIRTIO_PCI_STATUS));
491 }
492 
493 static void
494 vtpci_legacy_set_status(struct vtpci_legacy_softc *sc, uint8_t status)
495 {
496 	if (status != VIRTIO_CONFIG_STATUS_RESET)
497 		status |= vtpci_legacy_get_status(sc);
498 
499 	vtpci_legacy_write_config_1(sc, VIRTIO_PCI_STATUS, status);
500 }
501 
502 static void
503 vtpci_legacy_read_dev_config(device_t dev, bus_size_t offset,
504     void *dst, int length)
505 {
506 	struct vtpci_legacy_softc *sc;
507 	bus_size_t off;
508 	uint8_t *d;
509 	int i;
510 
511 	sc = device_get_softc(dev);
512 	off = VIRTIO_PCI_LEGACY_CONFIG(sc) + offset;
513 
514 	d = dst;
515 	for (i = 0; i < length; i++) {
516 		d[i] = vtpci_legacy_read_config_1(sc, off + i);
517 	}
518 }
519 
520 static void
521 vtpci_legacy_write_dev_config(device_t dev, bus_size_t offset,
522     const void *src, int length)
523 {
524 	struct vtpci_legacy_softc *sc;
525 	bus_size_t off;
526 	const uint8_t *s;
527 	int i;
528 
529 	sc = device_get_softc(dev);
530 	off = VIRTIO_PCI_LEGACY_CONFIG(sc) + offset;
531 
532 	s = src;
533 	for (i = 0; i < length; i++) {
534 		vtpci_legacy_write_config_1(sc, off + i, s[i]);
535 	}
536 }
537 
538 static bool
539 vtpci_legacy_setup_msix(struct vtpci_legacy_softc *sc)
540 {
541 	device_t dev;
542 	int rid, table_rid;
543 
544 	dev = sc->vtpci_dev;
545 
546 	rid = table_rid = pci_msix_table_bar(dev);
547 	if (rid != PCIR_BAR(0)) {
548 		sc->vtpci_msix_table_res = bus_alloc_resource_any(
549 		    dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
550 		if (sc->vtpci_msix_table_res == NULL)
551 			return (false);
552 	}
553 
554 	rid = pci_msix_pba_bar(dev);
555 	if (rid != table_rid && rid != PCIR_BAR(0)) {
556 		sc->vtpci_msix_pba_res = bus_alloc_resource_any(
557 		    dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
558 		if (sc->vtpci_msix_pba_res == NULL)
559 			return (false);
560 	}
561 
562 	return (true);
563 }
564 
565 static void
566 vtpci_legacy_teardown_msix(struct vtpci_legacy_softc *sc)
567 {
568 	device_t dev;
569 
570 	dev = sc->vtpci_dev;
571 
572 	if (sc->vtpci_msix_pba_res != NULL) {
573 		bus_release_resource(dev, SYS_RES_MEMORY,
574 		    rman_get_rid(sc->vtpci_msix_pba_res),
575 		    sc->vtpci_msix_pba_res);
576 		sc->vtpci_msix_pba_res = NULL;
577 	}
578 	if (sc->vtpci_msix_table_res != NULL) {
579 		bus_release_resource(dev, SYS_RES_MEMORY,
580 		    rman_get_rid(sc->vtpci_msix_table_res),
581 		    sc->vtpci_msix_table_res);
582 		sc->vtpci_msix_table_res = NULL;
583 	}
584 }
585 
586 static int
587 vtpci_legacy_alloc_resources(struct vtpci_legacy_softc *sc)
588 {
589 	const int res_types[] = { SYS_RES_IOPORT, SYS_RES_MEMORY };
590 	device_t dev;
591 	int rid, i;
592 
593 	dev = sc->vtpci_dev;
594 
595 	/*
596 	 * Most hypervisors export the common configuration structure in IO
597 	 * space, but some use memory space; try both.
598 	 */
599 	for (i = 0; nitems(res_types); i++) {
600 		rid = PCIR_BAR(0);
601 		sc->vtpci_res_type = res_types[i];
602 		sc->vtpci_res = bus_alloc_resource_any(dev, res_types[i], &rid,
603 		    RF_ACTIVE);
604 		if (sc->vtpci_res != NULL)
605 			break;
606 	}
607 	if (sc->vtpci_res == NULL)
608 		return (ENXIO);
609 
610 	return (0);
611 }
612 
613 static void
614 vtpci_legacy_free_resources(struct vtpci_legacy_softc *sc)
615 {
616 	device_t dev;
617 
618 	dev = sc->vtpci_dev;
619 
620 	if (sc->vtpci_res != NULL) {
621 		bus_release_resource(dev, sc->vtpci_res_type, PCIR_BAR(0),
622 		    sc->vtpci_res);
623 		sc->vtpci_res = NULL;
624 	}
625 }
626 
627 static void
628 vtpci_legacy_probe_and_attach_child(struct vtpci_legacy_softc *sc)
629 {
630 	device_t dev, child;
631 
632 	dev = sc->vtpci_dev;
633 	child = vtpci_child_device(&sc->vtpci_common);
634 
635 	if (child == NULL || device_get_state(child) != DS_NOTPRESENT)
636 		return;
637 
638 	if (device_probe(child) != 0)
639 		return;
640 
641 	vtpci_legacy_set_status(sc, VIRTIO_CONFIG_STATUS_DRIVER);
642 
643 	if (device_attach(child) != 0) {
644 		vtpci_legacy_set_status(sc, VIRTIO_CONFIG_STATUS_FAILED);
645 		/* Reset status for future attempt. */
646 		vtpci_legacy_child_detached(dev, child);
647 	} else {
648 		vtpci_legacy_set_status(sc, VIRTIO_CONFIG_STATUS_DRIVER_OK);
649 		VIRTIO_ATTACH_COMPLETED(child);
650 	}
651 }
652 
653 static int
654 vtpci_legacy_register_msix(struct vtpci_legacy_softc *sc, int offset,
655     struct vtpci_interrupt *intr)
656 {
657 	device_t dev;
658 	uint16_t vector;
659 
660 	dev = sc->vtpci_dev;
661 
662 	if (intr != NULL) {
663 		/* Map from guest rid to host vector. */
664 		vector = intr->vti_rid - 1;
665 	} else
666 		vector = VIRTIO_MSI_NO_VECTOR;
667 
668 	vtpci_legacy_write_header_2(sc, offset, vector);
669 	return (vtpci_legacy_read_header_2(sc, offset) == vector ? 0 : ENODEV);
670 }
671 
672 static int
673 vtpci_legacy_register_cfg_msix(device_t dev, struct vtpci_interrupt *intr)
674 {
675 	struct vtpci_legacy_softc *sc;
676 	int error;
677 
678 	sc = device_get_softc(dev);
679 
680 	error = vtpci_legacy_register_msix(sc, VIRTIO_MSI_CONFIG_VECTOR, intr);
681 	if (error) {
682 		device_printf(dev,
683 		    "unable to register config MSIX interrupt\n");
684 		return (error);
685 	}
686 
687 	return (0);
688 }
689 
690 static int
691 vtpci_legacy_register_vq_msix(device_t dev, int idx,
692     struct vtpci_interrupt *intr)
693 {
694 	struct vtpci_legacy_softc *sc;
695 	int error;
696 
697 	sc = device_get_softc(dev);
698 
699 	vtpci_legacy_select_virtqueue(sc, idx);
700 	error = vtpci_legacy_register_msix(sc, VIRTIO_MSI_QUEUE_VECTOR, intr);
701 	if (error) {
702 		device_printf(dev,
703 		    "unable to register virtqueue MSIX interrupt\n");
704 		return (error);
705 	}
706 
707 	return (0);
708 }
709 
710 static void
711 vtpci_legacy_reset(struct vtpci_legacy_softc *sc)
712 {
713 	/*
714 	 * Setting the status to RESET sets the host device to the
715 	 * original, uninitialized state.
716 	 */
717 	vtpci_legacy_set_status(sc, VIRTIO_CONFIG_STATUS_RESET);
718 	(void) vtpci_legacy_get_status(sc);
719 }
720 
721 static void
722 vtpci_legacy_select_virtqueue(struct vtpci_legacy_softc *sc, int idx)
723 {
724 	vtpci_legacy_write_header_2(sc, VIRTIO_PCI_QUEUE_SEL, idx);
725 }
726 
727 static uint8_t
728 vtpci_legacy_read_isr(device_t dev)
729 {
730 	struct vtpci_legacy_softc *sc;
731 
732 	sc = device_get_softc(dev);
733 
734 	return (vtpci_legacy_read_config_1(sc, VIRTIO_PCI_ISR));
735 }
736 
737 static uint16_t
738 vtpci_legacy_get_vq_size(device_t dev, int idx)
739 {
740 	struct vtpci_legacy_softc *sc;
741 
742 	sc = device_get_softc(dev);
743 
744 	vtpci_legacy_select_virtqueue(sc, idx);
745 	return (vtpci_legacy_read_header_2(sc, VIRTIO_PCI_QUEUE_NUM));
746 }
747 
748 static bus_size_t
749 vtpci_legacy_get_vq_notify_off(device_t dev, int idx)
750 {
751 	return (VIRTIO_PCI_QUEUE_NOTIFY);
752 }
753 
754 static void
755 vtpci_legacy_set_vq(device_t dev, struct virtqueue *vq)
756 {
757 	struct vtpci_legacy_softc *sc;
758 
759 	sc = device_get_softc(dev);
760 
761 	vtpci_legacy_select_virtqueue(sc, virtqueue_index(vq));
762 	vtpci_legacy_write_header_4(sc, VIRTIO_PCI_QUEUE_PFN,
763 	    virtqueue_paddr(vq) >> VIRTIO_PCI_QUEUE_ADDR_SHIFT);
764 }
765 
766 static void
767 vtpci_legacy_disable_vq(device_t dev, int idx)
768 {
769 	struct vtpci_legacy_softc *sc;
770 
771 	sc = device_get_softc(dev);
772 
773 	vtpci_legacy_select_virtqueue(sc, idx);
774 	vtpci_legacy_write_header_4(sc, VIRTIO_PCI_QUEUE_PFN, 0);
775 }
776