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