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