2 * Copyright (c) 2011, Bryan Venteicher <bryanv@daemoninthecloset.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 * $FreeBSD: src/sys/dev/virtio/pci/virtio_pci.c,v 1.3 2012/04/14 05:48:04 grehan Exp $
29 /* Driver for the VirtIO PCI interface. */
31 #include <sys/cdefs.h>
33 #include <sys/param.h>
34 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/module.h>
38 #include <sys/malloc.h>
40 #include <bus/pci/pcivar.h>
41 #include <bus/pci/pcireg.h>
44 #include <sys/param.h>
47 #include <dev/virtual/virtio/virtio/virtio.h>
48 #include <dev/virtual/virtio/virtio/virtqueue.h>
49 #include "virtio_pci.h"
50 #include "virtio_if.h"
51 #include "virtio_bus_if.h"
55 struct resource *vtpci_res;
56 struct resource *vtpci_msix_res;
57 uint64_t vtpci_features;
61 #define VIRTIO_PCI_FLAG_NO_MSI 0x0001
62 #define VIRTIO_PCI_FLAG_MSI 0x0002
63 #define VIRTIO_PCI_FLAG_NO_MSIX 0x0010
64 #define VIRTIO_PCI_FLAG_MSIX 0x0020
65 #define VIRTIO_PCI_FLAG_SHARED_MSIX 0x0040
67 device_t vtpci_child_dev;
68 struct virtio_feature_desc *vtpci_child_feat_desc;
71 * Ideally, each virtqueue that the driver provides a callback for
72 * will receive its own MSIX vector. If there are not sufficient
73 * vectors available, we will then attempt to have all the VQs
74 * share one vector. Note that when using MSIX, the configuration
75 * changed notifications must be on their own vector.
77 * If MSIX is not available, we will attempt to have the whole
78 * device share one MSI vector, and then, finally, one legacy
82 struct vtpci_virtqueue {
85 /* Index into vtpci_intr_res[] below. Unused, then -1. */
87 } vtpci_vqx[VIRTIO_MAX_VIRTQUEUES];
90 * When using MSIX interrupts, the first element of vtpci_intr_res[]
91 * is always the configuration changed notifications. The remaining
92 * element(s) are used for the virtqueues.
94 * With MSI and legacy interrupts, only the first element of
95 * vtpci_intr_res[] is used.
98 struct vtpci_intr_resource {
102 } vtpci_intr_res[1 + VIRTIO_MAX_VIRTQUEUES];
105 static int vtpci_probe(device_t);
106 static int vtpci_attach(device_t);
107 static int vtpci_detach(device_t);
108 static int vtpci_suspend(device_t);
109 static int vtpci_resume(device_t);
110 static int vtpci_shutdown(device_t);
111 static void vtpci_driver_added(device_t, driver_t *);
112 static void vtpci_child_detached(device_t, device_t);
113 static int vtpci_read_ivar(device_t, device_t, int, uintptr_t *);
114 static int vtpci_write_ivar(device_t, device_t, int, uintptr_t);
116 static uint64_t vtpci_negotiate_features(device_t, uint64_t);
117 static int vtpci_with_feature(device_t, uint64_t);
118 static int vtpci_alloc_virtqueues(device_t, int, int,
119 struct vq_alloc_info *);
120 static int vtpci_setup_intr(device_t);
121 static void vtpci_stop(device_t);
122 static int vtpci_reinit(device_t, uint64_t);
123 static void vtpci_reinit_complete(device_t);
124 static void vtpci_notify_virtqueue(device_t, uint16_t);
125 static uint8_t vtpci_get_status(device_t);
126 static void vtpci_set_status(device_t, uint8_t);
127 static void vtpci_read_dev_config(device_t, bus_size_t, void *, int);
128 static void vtpci_write_dev_config(device_t, bus_size_t, void *, int);
130 static void vtpci_describe_features(struct vtpci_softc *, const char *,
132 static void vtpci_probe_and_attach_child(struct vtpci_softc *);
134 static int vtpci_alloc_interrupts(struct vtpci_softc *, int, int,
135 struct vq_alloc_info *);
136 static int vtpci_alloc_intr_resources(struct vtpci_softc *, int,
137 struct vq_alloc_info *);
138 static int vtpci_alloc_msi(struct vtpci_softc *);
139 static int vtpci_alloc_msix(struct vtpci_softc *, int);
140 static int vtpci_register_msix_vector(struct vtpci_softc *, int, int);
142 static void vtpci_free_interrupts(struct vtpci_softc *);
143 static void vtpci_free_virtqueues(struct vtpci_softc *);
144 static void vtpci_release_child_resources(struct vtpci_softc *);
145 static void vtpci_reset(struct vtpci_softc *);
147 static int vtpci_legacy_intr(void *);
148 static int vtpci_vq_shared_intr(void *);
149 static int vtpci_vq_intr(void *);
150 static int vtpci_config_intr(void *);
153 * I/O port read/write wrappers.
155 #define vtpci_read_config_1(sc, o) bus_read_1((sc)->vtpci_res, (o))
156 #define vtpci_read_config_2(sc, o) bus_read_2((sc)->vtpci_res, (o))
157 #define vtpci_read_config_4(sc, o) bus_read_4((sc)->vtpci_res, (o))
158 #define vtpci_write_config_1(sc, o, v) bus_write_1((sc)->vtpci_res, (o), (v))
159 #define vtpci_write_config_2(sc, o, v) bus_write_2((sc)->vtpci_res, (o), (v))
160 #define vtpci_write_config_4(sc, o, v) bus_write_4((sc)->vtpci_res, (o), (v))
163 static int vtpci_disable_msix = 0;
164 TUNABLE_INT("hw.virtio.pci.disable_msix", &vtpci_disable_msix);
166 static device_method_t vtpci_methods[] = {
167 /* Device interface. */
168 DEVMETHOD(device_probe, vtpci_probe),
169 DEVMETHOD(device_attach, vtpci_attach),
170 DEVMETHOD(device_detach, vtpci_detach),
171 DEVMETHOD(device_suspend, vtpci_suspend),
172 DEVMETHOD(device_resume, vtpci_resume),
173 DEVMETHOD(device_shutdown, vtpci_shutdown),
176 DEVMETHOD(bus_driver_added, vtpci_driver_added),
177 DEVMETHOD(bus_child_detached, vtpci_child_detached),
178 DEVMETHOD(bus_read_ivar, vtpci_read_ivar),
179 DEVMETHOD(bus_write_ivar, vtpci_write_ivar),
181 /* VirtIO bus interface. */
182 DEVMETHOD(virtio_bus_negotiate_features, vtpci_negotiate_features),
183 DEVMETHOD(virtio_bus_with_feature, vtpci_with_feature),
184 DEVMETHOD(virtio_bus_alloc_virtqueues, vtpci_alloc_virtqueues),
185 DEVMETHOD(virtio_bus_setup_intr, vtpci_setup_intr),
186 DEVMETHOD(virtio_bus_stop, vtpci_stop),
187 DEVMETHOD(virtio_bus_reinit, vtpci_reinit),
188 DEVMETHOD(virtio_bus_reinit_complete, vtpci_reinit_complete),
189 DEVMETHOD(virtio_bus_notify_vq, vtpci_notify_virtqueue),
190 DEVMETHOD(virtio_bus_read_device_config, vtpci_read_dev_config),
191 DEVMETHOD(virtio_bus_write_device_config, vtpci_write_dev_config),
196 static driver_t vtpci_driver = {
199 sizeof(struct vtpci_softc)
202 devclass_t vtpci_devclass;
204 DRIVER_MODULE(virtio_pci, pci, vtpci_driver, vtpci_devclass, 0, 0);
205 MODULE_VERSION(virtio_pci, 1);
206 MODULE_DEPEND(virtio_pci, pci, 1, 1, 1);
207 MODULE_DEPEND(virtio_pci, virtio, 1, 1, 1);
210 vtpci_probe(device_t dev)
215 if (pci_get_vendor(dev) != VIRTIO_PCI_VENDORID)
218 if (pci_get_device(dev) < VIRTIO_PCI_DEVICEID_MIN ||
219 pci_get_device(dev) > VIRTIO_PCI_DEVICEID_MAX)
222 if (pci_get_revid(dev) != VIRTIO_PCI_ABI_VERSION)
225 name = virtio_device_name(pci_get_subdevice(dev));
229 ksnprintf(desc, sizeof(desc), "VirtIO PCI %s adapter", name);
230 device_set_desc_copy(dev, desc);
232 return (BUS_PROBE_DEFAULT);
236 vtpci_attach(device_t dev)
238 struct vtpci_softc *sc;
242 sc = device_get_softc(dev);
245 pci_enable_busmaster(dev);
248 sc->vtpci_res = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid,
250 if (sc->vtpci_res == NULL) {
251 device_printf(dev, "cannot map I/O space\n");
255 if (pci_find_extcap(dev, PCIY_MSI, NULL) != 0)
256 sc->vtpci_flags |= VIRTIO_PCI_FLAG_NO_MSI;
257 /* XXX(vsrinivas): Check out how to get MSI-X */
259 if (pci_find_extcap(dev, PCIY_MSIX, NULL) == 0) {
261 sc->vtpci_msix_res = bus_alloc_resource_any(dev,
262 SYS_RES_MEMORY, &rid, RF_ACTIVE);
265 if (sc->vtpci_msix_res == NULL)
266 sc->vtpci_flags |= VIRTIO_PCI_FLAG_NO_MSIX;
270 /* Tell the host we've noticed this device. */
271 vtpci_set_status(dev, VIRTIO_CONFIG_STATUS_ACK);
273 if ((child = device_add_child(dev, NULL, -1)) == NULL) {
274 device_printf(dev, "cannot create child device\n");
275 vtpci_set_status(dev, VIRTIO_CONFIG_STATUS_FAILED);
280 sc->vtpci_child_dev = child;
281 vtpci_probe_and_attach_child(sc);
287 vtpci_detach(device_t dev)
289 struct vtpci_softc *sc;
293 sc = device_get_softc(dev);
295 if ((child = sc->vtpci_child_dev) != NULL) {
296 error = device_delete_child(dev, child);
299 sc->vtpci_child_dev = NULL;
304 if (sc->vtpci_msix_res != NULL) {
305 bus_release_resource(dev, SYS_RES_MEMORY, PCIR_BAR(1),
307 sc->vtpci_msix_res = NULL;
310 if (sc->vtpci_res != NULL) {
311 bus_release_resource(dev, SYS_RES_IOPORT, PCIR_BAR(0),
313 sc->vtpci_res = NULL;
320 vtpci_suspend(device_t dev)
323 return (bus_generic_suspend(dev));
327 vtpci_resume(device_t dev)
330 return (bus_generic_resume(dev));
334 vtpci_shutdown(device_t dev)
337 (void) bus_generic_shutdown(dev);
338 /* Forcibly stop the host device. */
345 vtpci_driver_added(device_t dev, driver_t *driver)
347 struct vtpci_softc *sc;
349 sc = device_get_softc(dev);
351 vtpci_probe_and_attach_child(sc);
355 vtpci_child_detached(device_t dev, device_t child)
357 struct vtpci_softc *sc;
359 sc = device_get_softc(dev);
362 vtpci_release_child_resources(sc);
366 vtpci_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
368 struct vtpci_softc *sc;
370 sc = device_get_softc(dev);
372 if (sc->vtpci_child_dev != child)
376 case VIRTIO_IVAR_DEVTYPE:
377 *result = pci_get_subdevice(dev);
387 vtpci_write_ivar(device_t dev, device_t child, int index, uintptr_t value)
389 struct vtpci_softc *sc;
391 sc = device_get_softc(dev);
393 if (sc->vtpci_child_dev != child)
397 case VIRTIO_IVAR_FEATURE_DESC:
398 sc->vtpci_child_feat_desc = (void *) value;
408 vtpci_negotiate_features(device_t dev, uint64_t child_features)
410 struct vtpci_softc *sc;
411 uint64_t host_features, features;
413 sc = device_get_softc(dev);
415 host_features = vtpci_read_config_4(sc, VIRTIO_PCI_HOST_FEATURES);
416 vtpci_describe_features(sc, "host", host_features);
419 * Limit negotiated features to what the driver, virtqueue, and
422 features = host_features & child_features;
423 features = virtqueue_filter_features(features);
424 sc->vtpci_features = features;
426 vtpci_describe_features(sc, "negotiated", features);
427 vtpci_write_config_4(sc, VIRTIO_PCI_GUEST_FEATURES, features);
433 vtpci_with_feature(device_t dev, uint64_t feature)
435 struct vtpci_softc *sc;
437 sc = device_get_softc(dev);
439 return ((sc->vtpci_features & feature) != 0);
443 vtpci_alloc_virtqueues(device_t dev, int flags, int nvqs,
444 struct vq_alloc_info *vq_info)
446 struct vtpci_softc *sc;
447 struct vtpci_virtqueue *vqx;
448 struct vq_alloc_info *info;
452 sc = device_get_softc(dev);
454 if (sc->vtpci_nvqs != 0 || nvqs <= 0 ||
455 nvqs > VIRTIO_MAX_VIRTQUEUES)
458 error = vtpci_alloc_interrupts(sc, flags, nvqs, vq_info);
460 device_printf(dev, "cannot allocate interrupts\n");
464 if (sc->vtpci_flags & VIRTIO_PCI_FLAG_MSIX) {
465 error = vtpci_register_msix_vector(sc,
466 VIRTIO_MSI_CONFIG_VECTOR, 0);
471 for (queue = 0; queue < nvqs; queue++) {
472 vqx = &sc->vtpci_vqx[queue];
473 info = &vq_info[queue];
475 vtpci_write_config_2(sc, VIRTIO_PCI_QUEUE_SEL, queue);
477 vq_size = vtpci_read_config_2(sc, VIRTIO_PCI_QUEUE_NUM);
478 error = virtqueue_alloc(dev, queue, vq_size,
479 VIRTIO_PCI_VRING_ALIGN, 0xFFFFFFFFUL, info, &vqx->vq);
483 if (sc->vtpci_flags & VIRTIO_PCI_FLAG_MSIX) {
484 error = vtpci_register_msix_vector(sc,
485 VIRTIO_MSI_QUEUE_VECTOR, vqx->ires_idx);
490 vtpci_write_config_4(sc, VIRTIO_PCI_QUEUE_PFN,
491 virtqueue_paddr(vqx->vq) >> VIRTIO_PCI_QUEUE_ADDR_SHIFT);
493 *info->vqai_vq = vqx->vq;
501 vtpci_setup_intr(device_t dev)
503 struct vtpci_softc *sc;
504 struct vtpci_intr_resource *ires;
505 struct vtpci_virtqueue *vqx;
508 sc = device_get_softc(dev);
510 ires = &sc->vtpci_intr_res[0];
512 if ((sc->vtpci_flags & VIRTIO_PCI_FLAG_MSIX) == 0) {
513 error = bus_setup_intr(dev, ires->irq, flags,
514 (driver_intr_t *) vtpci_legacy_intr, sc, &ires->intrhand, NULL);
519 error = bus_setup_intr(dev, ires->irq, flags,(driver_intr_t *) vtpci_config_intr,
520 sc, &ires->intrhand, NULL);
524 if (sc->vtpci_flags & VIRTIO_PCI_FLAG_SHARED_MSIX) {
525 ires = &sc->vtpci_intr_res[1];
526 error = bus_setup_intr(dev, ires->irq, flags,
527 (driver_intr_t *) vtpci_vq_shared_intr, sc, &ires->intrhand, NULL);
532 /* Setup an interrupt handler for each virtqueue. */
533 for (i = 0; i < sc->vtpci_nvqs; i++) {
534 vqx = &sc->vtpci_vqx[i];
535 if (vqx->ires_idx < 1)
538 ires = &sc->vtpci_intr_res[vqx->ires_idx];
539 error = bus_setup_intr(dev, ires->irq, flags,
540 (driver_intr_t *) vtpci_vq_intr, vqx->vq, &ires->intrhand, NULL);
549 vtpci_stop(device_t dev)
552 vtpci_reset(device_get_softc(dev));
556 vtpci_reinit(device_t dev, uint64_t features)
558 struct vtpci_softc *sc;
559 struct vtpci_virtqueue *vqx;
560 struct virtqueue *vq;
564 sc = device_get_softc(dev);
567 * Redrive the device initialization. This is a bit of an abuse
568 * of the specification, but both VirtualBox and QEMU/KVM seem
569 * to play nice. We do not allow the host device to change from
570 * what was originally negotiated beyond what the guest driver
571 * changed (MSIX state should not change, number of virtqueues
572 * and their size remain the same, etc).
575 if (vtpci_get_status(dev) != VIRTIO_CONFIG_STATUS_RESET)
579 * Quickly drive the status through ACK and DRIVER. The device
580 * does not become usable again until vtpci_reinit_complete().
582 vtpci_set_status(dev, VIRTIO_CONFIG_STATUS_ACK);
583 vtpci_set_status(dev, VIRTIO_CONFIG_STATUS_DRIVER);
585 vtpci_negotiate_features(dev, features);
587 if (sc->vtpci_flags & VIRTIO_PCI_FLAG_MSIX) {
588 error = vtpci_register_msix_vector(sc,
589 VIRTIO_MSI_CONFIG_VECTOR, 0);
594 for (queue = 0; queue < sc->vtpci_nvqs; queue++) {
595 vqx = &sc->vtpci_vqx[queue];
598 KASSERT(vq != NULL, ("vq %d not allocated", queue));
599 vtpci_write_config_2(sc, VIRTIO_PCI_QUEUE_SEL, queue);
601 vq_size = vtpci_read_config_2(sc, VIRTIO_PCI_QUEUE_NUM);
602 error = virtqueue_reinit(vq, vq_size);
606 if (sc->vtpci_flags & VIRTIO_PCI_FLAG_MSIX) {
607 error = vtpci_register_msix_vector(sc,
608 VIRTIO_MSI_QUEUE_VECTOR, vqx->ires_idx);
613 vtpci_write_config_4(sc, VIRTIO_PCI_QUEUE_PFN,
614 virtqueue_paddr(vqx->vq) >> VIRTIO_PCI_QUEUE_ADDR_SHIFT);
621 vtpci_reinit_complete(device_t dev)
624 vtpci_set_status(dev, VIRTIO_CONFIG_STATUS_DRIVER_OK);
628 vtpci_notify_virtqueue(device_t dev, uint16_t queue)
630 struct vtpci_softc *sc;
632 sc = device_get_softc(dev);
634 vtpci_write_config_2(sc, VIRTIO_PCI_QUEUE_NOTIFY, queue);
638 vtpci_get_status(device_t dev)
640 struct vtpci_softc *sc;
642 sc = device_get_softc(dev);
644 return (vtpci_read_config_1(sc, VIRTIO_PCI_STATUS));
648 vtpci_set_status(device_t dev, uint8_t status)
650 struct vtpci_softc *sc;
652 sc = device_get_softc(dev);
654 if (status != VIRTIO_CONFIG_STATUS_RESET)
655 status |= vtpci_get_status(dev);
657 vtpci_write_config_1(sc, VIRTIO_PCI_STATUS, status);
661 vtpci_read_dev_config(device_t dev, bus_size_t offset,
662 void *dst, int length)
664 struct vtpci_softc *sc;
669 sc = device_get_softc(dev);
670 off = VIRTIO_PCI_CONFIG(sc) + offset;
672 for (d = dst; length > 0; d += size, off += size, length -= size) {
675 *(uint32_t *)d = vtpci_read_config_4(sc, off);
676 } else if (length >= 2) {
678 *(uint16_t *)d = vtpci_read_config_2(sc, off);
681 *d = vtpci_read_config_1(sc, off);
687 vtpci_write_dev_config(device_t dev, bus_size_t offset,
688 void *src, int length)
690 struct vtpci_softc *sc;
695 sc = device_get_softc(dev);
696 off = VIRTIO_PCI_CONFIG(sc) + offset;
698 for (s = src; length > 0; s += size, off += size, length -= size) {
701 vtpci_write_config_4(sc, off, *(uint32_t *)s);
702 } else if (length >= 2) {
704 vtpci_write_config_2(sc, off, *(uint16_t *)s);
707 vtpci_write_config_1(sc, off, *s);
713 vtpci_describe_features(struct vtpci_softc *sc, const char *msg,
719 child = sc->vtpci_child_dev;
721 if (device_is_attached(child) && bootverbose == 0)
724 virtio_describe(dev, msg, features, sc->vtpci_child_feat_desc);
728 vtpci_probe_and_attach_child(struct vtpci_softc *sc)
733 child = sc->vtpci_child_dev;
738 if (device_get_state(child) != DS_NOTPRESENT)
741 if (device_probe_child(dev, child) != 0)
744 vtpci_set_status(dev, VIRTIO_CONFIG_STATUS_DRIVER);
745 if (DEVICE_ATTACH(child) != 0) {
746 vtpci_set_status(dev, VIRTIO_CONFIG_STATUS_FAILED);
748 vtpci_release_child_resources(sc);
750 /* Reset status for future attempt. */
751 vtpci_set_status(dev, VIRTIO_CONFIG_STATUS_ACK);
753 vtpci_set_status(dev, VIRTIO_CONFIG_STATUS_DRIVER_OK);
757 vtpci_alloc_interrupts(struct vtpci_softc *sc, int flags, int nvqs,
758 struct vq_alloc_info *vq_info)
760 int i, nvectors, error;
763 * Only allocate a vector for virtqueues that are actually
764 * expecting an interrupt.
766 for (nvectors = 0, i = 0; i < nvqs; i++)
767 if (vq_info[i].vqai_intr != NULL)
770 if (vtpci_disable_msix != 0 ||
771 sc->vtpci_flags & VIRTIO_PCI_FLAG_NO_MSIX ||
772 flags & VIRTIO_ALLOC_VQS_DISABLE_MSIX ||
773 vtpci_alloc_msix(sc, nvectors) != 0) {
775 * Use MSI interrupts if available. Otherwise, we fallback
776 * to legacy interrupts.
778 if ((sc->vtpci_flags & VIRTIO_PCI_FLAG_NO_MSI) == 0 &&
779 vtpci_alloc_msi(sc) == 0)
780 sc->vtpci_flags |= VIRTIO_PCI_FLAG_MSI;
782 sc->vtpci_nintr_res = 1;
785 error = vtpci_alloc_intr_resources(sc, nvqs, vq_info);
791 vtpci_alloc_intr_resources(struct vtpci_softc *sc, int nvqs,
792 struct vq_alloc_info *vq_info)
795 struct resource *irq;
796 struct vtpci_virtqueue *vqx;
797 int i, rid, flags, res_idx;
802 if ((sc->vtpci_flags &
803 (VIRTIO_PCI_FLAG_MSI | VIRTIO_PCI_FLAG_MSIX)) == 0) {
805 flags |= RF_SHAREABLE;
809 for (i = 0; i < sc->vtpci_nintr_res; i++) {
810 irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, flags);
814 sc->vtpci_intr_res[i].irq = irq;
815 sc->vtpci_intr_res[i].rid = rid++;
819 * Map the virtqueue into the correct index in vq_intr_res[]. Note the
820 * first index is reserved for configuration changes notifications.
822 for (i = 0, res_idx = 1; i < nvqs; i++) {
823 vqx = &sc->vtpci_vqx[i];
825 if (sc->vtpci_flags & VIRTIO_PCI_FLAG_MSIX) {
826 if (vq_info[i].vqai_intr == NULL)
828 else if (sc->vtpci_flags & VIRTIO_PCI_FLAG_SHARED_MSIX)
829 vqx->ires_idx = res_idx;
831 vqx->ires_idx = res_idx++;
840 vtpci_alloc_msi(struct vtpci_softc *sc)
847 nmsi = pci_msi_count(dev);
852 sc->vtpci_irq_rid = 0;
853 sc->vtpci_irq_type = pci_alloc_1intr(dev, 1,
854 &sc->vtpci_irq_rid, &irq_flags);
861 vtpci_alloc_msix(struct vtpci_softc *sc, int nvectors)
863 /* XXX(vsrinivas): Huh? Is this how MSI-X works?*/
864 /* XXX(vsrinivas): All of this was disabled... */
867 int nmsix, cnt, required;
871 nmsix = pci_msix_count(dev);
875 /* An additional vector is needed for the config changes. */
876 required = nvectors + 1;
877 if (nmsix >= required) {
879 if (pci_alloc_msix(dev, &cnt) == 0 && cnt >= required)
882 pci_release_msi(dev);
885 /* Attempt shared MSIX configuration. */
887 if (nmsix >= required) {
889 if (pci_alloc_msix(dev, &cnt) == 0 && cnt >= required) {
890 sc->vtpci_flags |= VIRTIO_PCI_FLAG_SHARED_MSIX;
894 pci_release_msi(dev);
900 sc->vtpci_nintr_res = required;
901 sc->vtpci_flags |= VIRTIO_PCI_FLAG_MSIX;
904 if (sc->vtpci_flags & VIRTIO_PCI_FLAG_SHARED_MSIX)
905 device_printf(dev, "using shared virtqueue MSIX\n");
907 device_printf(dev, "using per virtqueue MSIX\n");
914 vtpci_register_msix_vector(struct vtpci_softc *sc, int offset, int res_idx)
921 if (offset != VIRTIO_MSI_CONFIG_VECTOR &&
922 offset != VIRTIO_MSI_QUEUE_VECTOR)
926 /* Map from rid to host vector. */
927 vector = sc->vtpci_intr_res[res_idx].rid - 1;
929 vector = VIRTIO_MSI_NO_VECTOR;
931 /* The first resource is special; make sure it is used correctly. */
933 KASSERT(vector == 0, ("unexpected config vector"));
934 KASSERT(offset == VIRTIO_MSI_CONFIG_VECTOR,
935 ("unexpected config offset"));
938 vtpci_write_config_2(sc, offset, vector);
940 if (vtpci_read_config_2(sc, offset) != vector) {
941 device_printf(dev, "insufficient host resources for "
942 "MSIX interrupts\n");
950 vtpci_free_interrupts(struct vtpci_softc *sc)
953 struct vtpci_intr_resource *ires;
957 sc->vtpci_nintr_res = 0;
959 if (sc->vtpci_flags & (VIRTIO_PCI_FLAG_MSI | VIRTIO_PCI_FLAG_MSIX)) {
960 pci_release_msi(dev);
961 sc->vtpci_flags &= ~(VIRTIO_PCI_FLAG_MSI |
962 VIRTIO_PCI_FLAG_MSIX | VIRTIO_PCI_FLAG_SHARED_MSIX);
965 for (i = 0; i < 1 + VIRTIO_MAX_VIRTQUEUES; i++) {
966 ires = &sc->vtpci_intr_res[i];
968 if (ires->intrhand != NULL) {
969 bus_teardown_intr(dev, ires->irq, ires->intrhand);
970 ires->intrhand = NULL;
973 if (ires->irq != NULL) {
974 bus_release_resource(dev, SYS_RES_IRQ, ires->rid,
984 vtpci_free_virtqueues(struct vtpci_softc *sc)
986 struct vtpci_virtqueue *vqx;
991 for (i = 0; i < VIRTIO_MAX_VIRTQUEUES; i++) {
992 vqx = &sc->vtpci_vqx[i];
994 if (vqx->vq != NULL) {
995 virtqueue_free(vqx->vq);
1002 vtpci_release_child_resources(struct vtpci_softc *sc)
1005 vtpci_free_interrupts(sc);
1006 vtpci_free_virtqueues(sc);
1010 vtpci_reset(struct vtpci_softc *sc)
1014 * Setting the status to RESET sets the host device to
1015 * the original, uninitialized state.
1017 vtpci_set_status(sc->vtpci_dev, VIRTIO_CONFIG_STATUS_RESET);
1021 vtpci_legacy_intr(void *xsc)
1023 struct vtpci_softc *sc;
1024 struct vtpci_virtqueue *vqx;
1029 vqx = &sc->vtpci_vqx[0];
1031 /* Reading the ISR also clears it. */
1032 isr = vtpci_read_config_1(sc, VIRTIO_PCI_ISR);
1034 if (isr & VIRTIO_PCI_ISR_CONFIG)
1035 vtpci_config_intr(sc);
1037 if (isr & VIRTIO_PCI_ISR_INTR)
1038 for (i = 0; i < sc->vtpci_nvqs; i++, vqx++)
1039 virtqueue_intr(vqx->vq);
1045 vtpci_vq_shared_intr(void *xsc)
1047 struct vtpci_softc *sc;
1048 struct vtpci_virtqueue *vqx;
1053 vqx = &sc->vtpci_vqx[0];
1055 for (i = 0; i < sc->vtpci_nvqs; i++, vqx++)
1056 rc |= virtqueue_intr(vqx->vq);
1062 vtpci_vq_intr(void *xvq)
1064 struct virtqueue *vq;
1068 rc = virtqueue_intr(vq);
1074 vtpci_config_intr(void *xsc)
1076 struct vtpci_softc *sc;
1082 child = sc->vtpci_child_dev;
1085 rc = VIRTIO_CONFIG_CHANGE(child);