xref: /freebsd/sys/dev/virtio/pci/virtio_pci_legacy.c (revision 3ff01b231dfa83d518854c63e7c9cd1debd1139e)
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, 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_str,	  virtio_child_pnpinfo_str),
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 size;
510 
511 	sc = device_get_softc(dev);
512 	off = VIRTIO_PCI_LEGACY_CONFIG(sc) + offset;
513 
514 	for (d = dst; length > 0; d += size, off += size, length -= size) {
515 		if (length >= 4) {
516 			size = 4;
517 			*(uint32_t *)d = vtpci_legacy_read_config_4(sc, off);
518 		} else if (length >= 2) {
519 			size = 2;
520 			*(uint16_t *)d = vtpci_legacy_read_config_2(sc, off);
521 		} else {
522 			size = 1;
523 			*d = vtpci_legacy_read_config_1(sc, off);
524 		}
525 	}
526 }
527 
528 static void
529 vtpci_legacy_write_dev_config(device_t dev, bus_size_t offset,
530     void *src, int length)
531 {
532 	struct vtpci_legacy_softc *sc;
533 	bus_size_t off;
534 	uint8_t *s;
535 	int size;
536 
537 	sc = device_get_softc(dev);
538 	off = VIRTIO_PCI_LEGACY_CONFIG(sc) + offset;
539 
540 	for (s = src; length > 0; s += size, off += size, length -= size) {
541 		if (length >= 4) {
542 			size = 4;
543 			vtpci_legacy_write_config_4(sc, off, *(uint32_t *)s);
544 		} else if (length >= 2) {
545 			size = 2;
546 			vtpci_legacy_write_config_2(sc, off, *(uint16_t *)s);
547 		} else {
548 			size = 1;
549 			vtpci_legacy_write_config_1(sc, off, *s);
550 		}
551 	}
552 }
553 
554 static bool
555 vtpci_legacy_setup_msix(struct vtpci_legacy_softc *sc)
556 {
557 	device_t dev;
558 	int rid, table_rid;
559 
560 	dev = sc->vtpci_dev;
561 
562 	rid = table_rid = pci_msix_table_bar(dev);
563 	if (rid != PCIR_BAR(0)) {
564 		sc->vtpci_msix_table_res = bus_alloc_resource_any(
565 		    dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
566 		if (sc->vtpci_msix_table_res == NULL)
567 			return (false);
568 	}
569 
570 	rid = pci_msix_pba_bar(dev);
571 	if (rid != table_rid && rid != PCIR_BAR(0)) {
572 		sc->vtpci_msix_pba_res = bus_alloc_resource_any(
573 		    dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
574 		if (sc->vtpci_msix_pba_res == NULL)
575 			return (false);
576 	}
577 
578 	return (true);
579 }
580 
581 static void
582 vtpci_legacy_teardown_msix(struct vtpci_legacy_softc *sc)
583 {
584 	device_t dev;
585 
586 	dev = sc->vtpci_dev;
587 
588 	if (sc->vtpci_msix_pba_res != NULL) {
589 		bus_release_resource(dev, SYS_RES_MEMORY,
590 		    rman_get_rid(sc->vtpci_msix_pba_res),
591 		    sc->vtpci_msix_pba_res);
592 		sc->vtpci_msix_pba_res = NULL;
593 	}
594 	if (sc->vtpci_msix_table_res != NULL) {
595 		bus_release_resource(dev, SYS_RES_MEMORY,
596 		    rman_get_rid(sc->vtpci_msix_table_res),
597 		    sc->vtpci_msix_table_res);
598 		sc->vtpci_msix_table_res = NULL;
599 	}
600 }
601 
602 static int
603 vtpci_legacy_alloc_resources(struct vtpci_legacy_softc *sc)
604 {
605 	const int res_types[] = { SYS_RES_IOPORT, SYS_RES_MEMORY };
606 	device_t dev;
607 	int rid, i;
608 
609 	dev = sc->vtpci_dev;
610 
611 	/*
612 	 * Most hypervisors export the common configuration structure in IO
613 	 * space, but some use memory space; try both.
614 	 */
615 	for (i = 0; nitems(res_types); i++) {
616 		rid = PCIR_BAR(0);
617 		sc->vtpci_res_type = res_types[i];
618 		sc->vtpci_res = bus_alloc_resource_any(dev, res_types[i], &rid,
619 		    RF_ACTIVE);
620 		if (sc->vtpci_res != NULL)
621 			break;
622 	}
623 	if (sc->vtpci_res == NULL)
624 		return (ENXIO);
625 
626 	return (0);
627 }
628 
629 static void
630 vtpci_legacy_free_resources(struct vtpci_legacy_softc *sc)
631 {
632 	device_t dev;
633 
634 	dev = sc->vtpci_dev;
635 
636 	if (sc->vtpci_res != NULL) {
637 		bus_release_resource(dev, sc->vtpci_res_type, PCIR_BAR(0),
638 		    sc->vtpci_res);
639 		sc->vtpci_res = NULL;
640 	}
641 }
642 
643 static void
644 vtpci_legacy_probe_and_attach_child(struct vtpci_legacy_softc *sc)
645 {
646 	device_t dev, child;
647 
648 	dev = sc->vtpci_dev;
649 	child = vtpci_child_device(&sc->vtpci_common);
650 
651 	if (child == NULL || device_get_state(child) != DS_NOTPRESENT)
652 		return;
653 
654 	if (device_probe(child) != 0)
655 		return;
656 
657 	vtpci_legacy_set_status(sc, VIRTIO_CONFIG_STATUS_DRIVER);
658 
659 	if (device_attach(child) != 0) {
660 		vtpci_legacy_set_status(sc, VIRTIO_CONFIG_STATUS_FAILED);
661 		/* Reset status for future attempt. */
662 		vtpci_legacy_child_detached(dev, child);
663 	} else {
664 		vtpci_legacy_set_status(sc, VIRTIO_CONFIG_STATUS_DRIVER_OK);
665 		VIRTIO_ATTACH_COMPLETED(child);
666 	}
667 }
668 
669 static int
670 vtpci_legacy_register_msix(struct vtpci_legacy_softc *sc, int offset,
671     struct vtpci_interrupt *intr)
672 {
673 	device_t dev;
674 	uint16_t vector;
675 
676 	dev = sc->vtpci_dev;
677 
678 	if (intr != NULL) {
679 		/* Map from guest rid to host vector. */
680 		vector = intr->vti_rid - 1;
681 	} else
682 		vector = VIRTIO_MSI_NO_VECTOR;
683 
684 	vtpci_legacy_write_header_2(sc, offset, vector);
685 	return (vtpci_legacy_read_header_2(sc, offset) == vector ? 0 : ENODEV);
686 }
687 
688 static int
689 vtpci_legacy_register_cfg_msix(device_t dev, struct vtpci_interrupt *intr)
690 {
691 	struct vtpci_legacy_softc *sc;
692 	int error;
693 
694 	sc = device_get_softc(dev);
695 
696 	error = vtpci_legacy_register_msix(sc, VIRTIO_MSI_CONFIG_VECTOR, intr);
697 	if (error) {
698 		device_printf(dev,
699 		    "unable to register config MSIX interrupt\n");
700 		return (error);
701 	}
702 
703 	return (0);
704 }
705 
706 static int
707 vtpci_legacy_register_vq_msix(device_t dev, int idx,
708     struct vtpci_interrupt *intr)
709 {
710 	struct vtpci_legacy_softc *sc;
711 	int error;
712 
713 	sc = device_get_softc(dev);
714 
715 	vtpci_legacy_select_virtqueue(sc, idx);
716 	error = vtpci_legacy_register_msix(sc, VIRTIO_MSI_QUEUE_VECTOR, intr);
717 	if (error) {
718 		device_printf(dev,
719 		    "unable to register virtqueue MSIX interrupt\n");
720 		return (error);
721 	}
722 
723 	return (0);
724 }
725 
726 static void
727 vtpci_legacy_reset(struct vtpci_legacy_softc *sc)
728 {
729 	/*
730 	 * Setting the status to RESET sets the host device to the
731 	 * original, uninitialized state.
732 	 */
733 	vtpci_legacy_set_status(sc, VIRTIO_CONFIG_STATUS_RESET);
734 	(void) vtpci_legacy_get_status(sc);
735 }
736 
737 static void
738 vtpci_legacy_select_virtqueue(struct vtpci_legacy_softc *sc, int idx)
739 {
740 	vtpci_legacy_write_header_2(sc, VIRTIO_PCI_QUEUE_SEL, idx);
741 }
742 
743 static uint8_t
744 vtpci_legacy_read_isr(device_t dev)
745 {
746 	struct vtpci_legacy_softc *sc;
747 
748 	sc = device_get_softc(dev);
749 
750 	return (vtpci_legacy_read_config_1(sc, VIRTIO_PCI_ISR));
751 }
752 
753 static uint16_t
754 vtpci_legacy_get_vq_size(device_t dev, int idx)
755 {
756 	struct vtpci_legacy_softc *sc;
757 
758 	sc = device_get_softc(dev);
759 
760 	vtpci_legacy_select_virtqueue(sc, idx);
761 	return (vtpci_legacy_read_header_2(sc, VIRTIO_PCI_QUEUE_NUM));
762 }
763 
764 static bus_size_t
765 vtpci_legacy_get_vq_notify_off(device_t dev, int idx)
766 {
767 	return (VIRTIO_PCI_QUEUE_NOTIFY);
768 }
769 
770 static void
771 vtpci_legacy_set_vq(device_t dev, struct virtqueue *vq)
772 {
773 	struct vtpci_legacy_softc *sc;
774 
775 	sc = device_get_softc(dev);
776 
777 	vtpci_legacy_select_virtqueue(sc, virtqueue_index(vq));
778 	vtpci_legacy_write_header_4(sc, VIRTIO_PCI_QUEUE_PFN,
779 	    virtqueue_paddr(vq) >> VIRTIO_PCI_QUEUE_ADDR_SHIFT);
780 }
781 
782 static void
783 vtpci_legacy_disable_vq(device_t dev, int idx)
784 {
785 	struct vtpci_legacy_softc *sc;
786 
787 	sc = device_get_softc(dev);
788 
789 	vtpci_legacy_select_virtqueue(sc, idx);
790 	vtpci_legacy_write_header_4(sc, VIRTIO_PCI_QUEUE_PFN, 0);
791 }
792