Index: sys/dev/pci/files.pci
===================================================================
RCS file: /cvsroot/src/sys/dev/pci/files.pci,v
retrieving revision 1.388
diff -u -r1.388 files.pci
--- sys/dev/pci/files.pci       13 Apr 2017 10:37:36 -0000      1.388
+++ sys/dev/pci/files.pci       2 Aug 2017 08:05:24 -0000
@@ -1137,29 +1137,11 @@
attach  sisfb at pci
file    dev/pci/sisfb.c                sisfb   needs-flag

-# VirtIO
-device virtio {}
-attach virtio at pci
-file   dev/pci/virtio.c        virtio
-
-attach ld at virtio with ld_virtio
-file   dev/pci/ld_virtio.c     ld_virtio
-
-device viomb
-attach viomb at virtio
-file   dev/pci/viomb.c         viomb
-
-device vioif: ether, ifnet, arp
-attach vioif at virtio
-file   dev/pci/if_vioif.c      vioif
-
-device viornd
-attach viornd at virtio
-file   dev/pci/viornd.c        viornd
-
-device vioscsi: scsi
-attach vioscsi at virtio
-file   dev/pci/vioscsi.c       vioscsi
+# VirtIO: XXX: move to dev/vmm/virtio
+include "dev/pci/files.virtio"
+
+attach virtio at pci with virtio_pci
+file   dev/pci/virtio_pci.c    virtio_pci

# Silicon Motion SM712(LynxEM+) frame buffer
device lynxfb: wsemuldisplaydev, rasops16
Index: sys/dev/pci/files.virtio
===================================================================
RCS file: sys/dev/pci/files.virtio
diff -N sys/dev/pci/files.virtio
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ sys/dev/pci/files.virtio    2 Aug 2017 08:05:24 -0000
@@ -0,0 +1,25 @@
+#      $NetBSD$
+
+# VirtIO
+device virtio {}
+file   dev/pci/virtio.c        virtio
+
+attach ld at virtio with ld_virtio
+file   dev/pci/ld_virtio.c     ld_virtio
+
+device viomb
+attach viomb at virtio
+file   dev/pci/viomb.c         viomb
+
+device vioif: ether, ifnet, arp
+attach vioif at virtio
+file   dev/pci/if_vioif.c      vioif
+
+device viornd
+attach viornd at virtio
+file   dev/pci/viornd.c        viornd
+
+device vioscsi: scsi
+attach vioscsi at virtio
+file   dev/pci/vioscsi.c       vioscsi
+
Index: sys/dev/pci/virtio.c
===================================================================
RCS file: /cvsroot/src/sys/dev/pci/virtio.c,v
retrieving revision 1.28
diff -u -r1.28 virtio.c
--- sys/dev/pci/virtio.c        1 Jun 2017 02:45:11 -0000       1.28
+++ sys/dev/pci/virtio.c        2 Aug 2017 08:05:25 -0000
@@ -43,15 +43,11 @@

#define VIRTIO_PRIVATE

-#include <dev/pci/virtioreg.h>
-#include <dev/pci/virtiovar.h>
+#include <dev/pci/virtioreg.h> /* XXX: move to non-pci */
+#include <dev/pci/virtiovar.h> /* XXX: move to non-pci */

#define MINSEG_INDIRECT                2 /* use indirect if nsegs >= this value */

-static int     virtio_match(device_t, cfdata_t, void *);
-static void    virtio_attach(device_t, device_t, void *);
-static int     virtio_rescan(device_t, const char *, const int *);
-static int     virtio_detach(device_t, int);
static int     virtio_intr(void *arg);
static int     virtio_msix_queue_intr(void *);
static int     virtio_msix_config_intr(void *);
@@ -66,9 +62,6 @@
static void    virtio_init_vq(struct virtio_softc *,
                   struct virtqueue *, const bool);

-CFATTACH_DECL3_NEW(virtio, sizeof(struct virtio_softc),
-    virtio_match, virtio_attach, virtio_detach, NULL, virtio_rescan, NULL,
-    DVF_DETACH_SHUTDOWN);

/* we use the legacy virtio spec, so the pci registers are host native
 * byte order, not pci (i.e. LE) byte order */
@@ -109,7 +102,7 @@
#define        REG_LO_OFF      0
#endif

-static void
+void
virtio_set_status(struct virtio_softc *sc, int status)
{
       int old = 0;
@@ -121,42 +114,6 @@
                         status|old);
}

-#define virtio_device_reset(sc)        virtio_set_status((sc), 0)
-
-static int
-virtio_match(device_t parent, cfdata_t match, void *aux)
-{
-       struct pci_attach_args *pa;
-
-       pa = (struct pci_attach_args *)aux;
-       switch (PCI_VENDOR(pa->pa_id)) {
-       case PCI_VENDOR_QUMRANET:
-               if ((PCI_PRODUCT_QUMRANET_VIRTIO_1000 <=
-                    PCI_PRODUCT(pa->pa_id)) &&
-                   (PCI_PRODUCT(pa->pa_id) <=
-                    PCI_PRODUCT_QUMRANET_VIRTIO_103F))
-                       return 1;
-               break;
-       }
-
-       return 0;
-}
-
-static const char *virtio_device_name[] = {
-       "Unknown (0)",                  /* 0 */
-       "Network",                      /* 1 */
-       "Block",                        /* 2 */
-       "Console",                      /* 3 */
-       "Entropy",                      /* 4 */
-       "Memory Balloon",               /* 5 */
-       "I/O Memory",                   /* 6 */
-       "Remote Processor Messaging",   /* 7 */
-       "SCSI",                         /* 8 */
-       "9P Transport",                 /* 9 */
-       "mac80211 wlan",                /* 10 */
-};
-#define NDEVNAMES      __arraycount(virtio_device_name)
-
#define VIRTIO_MSIX_CONFIG_VECTOR_INDEX        0
#define VIRTIO_MSIX_QUEUE_VECTOR_INDEX 1

@@ -383,119 +340,7 @@
       sc->sc_ihs_num = 0;
}

-static void
-virtio_attach(device_t parent, device_t self, void *aux)
-{
-       struct virtio_softc *sc = device_private(self);
-       struct pci_attach_args *pa = (struct pci_attach_args *)aux;
-       pci_chipset_tag_t pc = pa->pa_pc;
-       pcitag_t tag = pa->pa_tag;
-       int revision;
-       pcireg_t id;
-
-       revision = PCI_REVISION(pa->pa_class);
-       if (revision != 0) {
-               aprint_normal(": unknown revision 0x%02x; giving up\n",
-                             revision);
-               return;
-       }
-       aprint_normal("\n");
-       aprint_naive("\n");
-
-       /* subsystem ID shows what I am */
-       id = pci_conf_read(pc, tag, PCI_SUBSYS_ID_REG);
-       aprint_normal_dev(self, "Virtio %s Device (rev. 0x%02x)\n",
-                         (PCI_SUBSYS_ID(id) < NDEVNAMES?
-                          virtio_device_name[PCI_SUBSYS_ID(id)] : "Unknown"),
-                         revision);
-
-       sc->sc_dev = self;
-       sc->sc_pc = pc;
-       sc->sc_tag = tag;
-       sc->sc_iot = pa->pa_iot;
-       if (pci_dma64_available(pa))
-               sc->sc_dmat = pa->pa_dmat64;
-       else
-               sc->sc_dmat = pa->pa_dmat;
-       sc->sc_config_offset = VIRTIO_CONFIG_DEVICE_CONFIG_NOMSI;
-
-       if (pci_mapreg_map(pa, PCI_MAPREG_START, PCI_MAPREG_TYPE_IO, 0,
-                          &sc->sc_iot, &sc->sc_ioh, NULL, &sc->sc_iosize)) {
-               aprint_error_dev(self, "can't map i/o space\n");
-               return;
-       }
-
-       virtio_device_reset(sc);
-       virtio_set_status(sc, VIRTIO_CONFIG_DEVICE_STATUS_ACK);
-       virtio_set_status(sc, VIRTIO_CONFIG_DEVICE_STATUS_DRIVER);
-
-       sc->sc_childdevid = PCI_SUBSYS_ID(id);
-       sc->sc_child = NULL;
-       sc->sc_pa = *pa;
-       virtio_rescan(self, "virtio", 0);
-       return;
-}
-
-/* ARGSUSED */
-static int
-virtio_rescan(device_t self, const char *attr, const int *scan_flags)
-{
-       struct virtio_softc *sc;
-       struct virtio_attach_args va;
-
-       sc = device_private(self);
-       if (sc->sc_child)       /* Child already attached? */
-               return 0;
-
-       memset(&va, 0, sizeof(va));
-       va.sc_childdevid = sc->sc_childdevid;
-
-       config_found_ia(self, attr, &va, NULL);
-
-       if (sc->sc_child == NULL) {
-               aprint_error_dev(self,
-                                "no matching child driver; not configured\n");
-               return 0;
-       }
-
-       if (sc->sc_child == VIRTIO_CHILD_FAILED) {
-               aprint_error_dev(self,
-                                "virtio configuration failed\n");
-               return 0;
-       }
-
-       /*
-        * Make sure child drivers initialize interrupts via call
-        * to virtio_child_attach_finish().
-        */
-       KASSERT(sc->sc_ihs_num != 0);
-
-       return 0;
-}
-
-static int
-virtio_detach(device_t self, int flags)
-{
-       struct virtio_softc *sc = device_private(self);
-       int r;
-
-       if (sc->sc_child != NULL) {
-               r = config_detach(sc->sc_child, flags);
-               if (r)
-                       return r;
-       }

-       /* Check that child detached properly */
-       KASSERT(sc->sc_child == NULL);
-       KASSERT(sc->sc_vqs == NULL);
-       KASSERT(sc->sc_ihs_num == 0);
-
-       if (sc->sc_iosize)
-               bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_iosize);
-       sc->sc_iosize = 0;
-
-       return 0;
-}

/*
 * Reset the device.
Index: sys/dev/pci/virtio_pci.c
===================================================================
RCS file: sys/dev/pci/virtio_pci.c
diff -N sys/dev/pci/virtio_pci.c
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ sys/dev/pci/virtio_pci.c    2 Aug 2017 08:05:25 -0000
@@ -0,0 +1,201 @@
+/* $NetBSD$ */
+
+/*
+ * Copyright (c) 2010 Minoura Makoto.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__KERNEL_RCSID(0, "$NetBSD");
+
+#include <sys/param.h>
+#include <sys/systm.h>
+
+#include <sys/device.h>
+
+#include <dev/pci/pcidevs.h>
+#include <dev/pci/pcireg.h>
+#include <dev/pci/pcivar.h>
+
+#define VIRTIO_PRIVATE
+
+#include <dev/pci/virtioreg.h> /* XXX: move to non-pci */
+#include <dev/pci/virtiovar.h> /* XXX: move to non-pci */
+
+static int     virtio_match(device_t, cfdata_t, void *);
+static void    virtio_attach(device_t, device_t, void *);
+static int     virtio_rescan(device_t, const char *, const int *);
+static int     virtio_detach(device_t, int);
+
+static const char *virtio_device_name[] = {
+       "Unknown (0)",                  /* 0 */
+       "Network",                      /* 1 */
+       "Block",                        /* 2 */
+       "Console",                      /* 3 */
+       "Entropy",                      /* 4 */
+       "Memory Balloon",               /* 5 */
+       "I/O Memory",                   /* 6 */
+       "Remote Processor Messaging",   /* 7 */
+       "SCSI",                         /* 8 */
+       "9P Transport",                 /* 9 */
+       "mac80211 wlan",                /* 10 */
+};
+#define NDEVNAMES      __arraycount(virtio_device_name)
+
+CFATTACH_DECL3_NEW(virtio_pci, sizeof(struct virtio_softc),
+    virtio_match, virtio_attach, virtio_detach, NULL, virtio_rescan, NULL,
+    DVF_DETACH_SHUTDOWN);
+
+static int
+virtio_match(device_t parent, cfdata_t match, void *aux)
+{
+       struct pci_attach_args *pa;
+
+       pa = (struct pci_attach_args *)aux;
+       switch (PCI_VENDOR(pa->pa_id)) {
+       case PCI_VENDOR_QUMRANET:
+               if ((PCI_PRODUCT_QUMRANET_VIRTIO_1000 <=
+                    PCI_PRODUCT(pa->pa_id)) &&
+                   (PCI_PRODUCT(pa->pa_id) <=
+                    PCI_PRODUCT_QUMRANET_VIRTIO_103F))
+                       return 1;
+               break;
+       }
+
+       return 0;
+}
+
+static void
+virtio_attach(device_t parent, device_t self, void *aux)
+{
+       struct virtio_softc *sc = device_private(self);
+       struct pci_attach_args *pa = (struct pci_attach_args *)aux;
+       pci_chipset_tag_t pc = pa->pa_pc;
+       pcitag_t tag = pa->pa_tag;
+       int revision;
+       pcireg_t id;
+
+       revision = PCI_REVISION(pa->pa_class);
+       if (revision != 0) {
+               aprint_normal(": unknown revision 0x%02x; giving up\n",
+                             revision);
+               return;
+       }
+       aprint_normal("\n");
+       aprint_naive("\n");
+
+       /* subsystem ID shows what I am */
+       id = pci_conf_read(pc, tag, PCI_SUBSYS_ID_REG);
+       aprint_normal_dev(self, "Virtio %s Device (rev. 0x%02x)\n",
+                         (PCI_SUBSYS_ID(id) < NDEVNAMES?
+                          virtio_device_name[PCI_SUBSYS_ID(id)] : "Unknown"),
+                         revision);
+
+       sc->sc_dev = self;
+       sc->sc_pc = pc;
+       sc->sc_tag = tag;
+       sc->sc_iot = pa->pa_iot;
+       if (pci_dma64_available(pa))
+               sc->sc_dmat = pa->pa_dmat64;
+       else
+               sc->sc_dmat = pa->pa_dmat;
+       sc->sc_config_offset = VIRTIO_CONFIG_DEVICE_CONFIG_NOMSI;
+
+       if (pci_mapreg_map(pa, PCI_MAPREG_START, PCI_MAPREG_TYPE_IO, 0,
+                          &sc->sc_iot, &sc->sc_ioh, NULL, &sc->sc_iosize)) {
+               aprint_error_dev(self, "can't map i/o space\n");
+               return;
+       }
+
+       virtio_device_reset(sc);
+       virtio_set_status(sc, VIRTIO_CONFIG_DEVICE_STATUS_ACK);
+       virtio_set_status(sc, VIRTIO_CONFIG_DEVICE_STATUS_DRIVER);
+
+       sc->sc_childdevid = PCI_SUBSYS_ID(id);
+       sc->sc_child = NULL;
+       sc->sc_pa = *pa;
+       virtio_rescan(self, "virtio", 0);
+       return;
+}
+
+/* ARGSUSED */
+static int
+virtio_rescan(device_t self, const char *attr, const int *scan_flags)
+{
+       struct virtio_softc *sc;
+       struct virtio_attach_args va;
+
+       sc = device_private(self);
+       if (sc->sc_child)       /* Child already attached? */
+               return 0;
+
+       memset(&va, 0, sizeof(va));
+       va.sc_childdevid = sc->sc_childdevid;
+
+       config_found_ia(self, attr, &va, NULL);
+
+       if (sc->sc_child == NULL) {
+               aprint_error_dev(self,
+                                "no matching child driver; not configured\n");
+               return 0;
+       }
+
+       if (sc->sc_child == VIRTIO_CHILD_FAILED) {
+               aprint_error_dev(self,
+                                "virtio configuration failed\n");
+               return 0;
+       }
+
+       /*
+        * Make sure child drivers initialize interrupts via call
+        * to virtio_child_attach_finish().
+        */
+       KASSERT(sc->sc_ihs_num != 0);
+
+       return 0;
+}
+
+
+static int
+virtio_detach(device_t self, int flags)
+{
+       struct virtio_softc *sc = device_private(self);
+       int r;
+
+       if (sc->sc_child != NULL) {
+               r = config_detach(sc->sc_child, flags);
+               if (r)
+                       return r;
+       }
+
+       /* Check that child detached properly */
+       KASSERT(sc->sc_child == NULL);
+       KASSERT(sc->sc_vqs == NULL);
+       KASSERT(sc->sc_ihs_num == 0);
+
+       if (sc->sc_iosize)
+               bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_iosize);
+       sc->sc_iosize = 0;
+
+       return 0;
+}
Index: sys/dev/pci/virtiovar.h
===================================================================
RCS file: /cvsroot/src/sys/dev/pci/virtiovar.h,v
retrieving revision 1.9
diff -u -r1.9 virtiovar.h
--- sys/dev/pci/virtiovar.h     26 Mar 2017 12:36:43 -0000      1.9
+++ sys/dev/pci/virtiovar.h     2 Aug 2017 08:05:25 -0000
@@ -214,4 +214,9 @@
int            virtio_intrhand(struct virtio_softc *);
uint32_t       virtio_features(struct virtio_softc *);

+/* autoconf(9) common */
+void virtio_set_status(struct virtio_softc *, int);
+
+#define virtio_device_reset(sc)        virtio_set_status((sc), 0)
+
#endif /* _DEV_PCI_VIRTIOVAR_H_ */