Reorg the virtio(4) config(9) code to be explicitly pci specific.

We'll later use this for a non-pci virtio(4) usecase.

ok martin
This commit is contained in:
cherry 2017-08-02 08:39:14 +00:00
parent 0bd083dcfb
commit da413d611f
5 changed files with 242 additions and 184 deletions

View File

@ -1,4 +1,4 @@
# $NetBSD: files.pci,v 1.388 2017/04/13 10:37:36 knakahara Exp $
# $NetBSD: files.pci,v 1.389 2017/08/02 08:39:14 cherry Exp $
#
# Config file and device description for machine-independent PCI code.
# Included by ports that need it. Requires that the SCSI files be
@ -1137,29 +1137,11 @@ device sisfb: wsemuldisplaydev, rasops8, rasops15, rasops16, rasops32, vcons
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
# VirtIO: XXX: move to dev/vmm/virtio
include "dev/pci/files.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
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

25
sys/dev/pci/files.virtio Normal file
View File

@ -0,0 +1,25 @@
# $NetBSD: files.virtio,v 1.1 2017/08/02 08:39:14 cherry Exp $
# 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

View File

@ -1,4 +1,4 @@
/* $NetBSD: virtio.c,v 1.28 2017/06/01 02:45:11 chs Exp $ */
/* $NetBSD: virtio.c,v 1.29 2017/08/02 08:39:14 cherry Exp $ */
/*
* Copyright (c) 2010 Minoura Makoto.
@ -26,7 +26,7 @@
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: virtio.c,v 1.28 2017/06/01 02:45:11 chs Exp $");
__KERNEL_RCSID(0, "$NetBSD: virtio.c,v 1.29 2017/08/02 08:39:14 cherry Exp $");
#include <sys/param.h>
#include <sys/systm.h>
@ -43,15 +43,11 @@ __KERNEL_RCSID(0, "$NetBSD: virtio.c,v 1.28 2017/06/01 02:45:11 chs Exp $");
#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_soft_intr(void *arg);
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 @@ nbo_bus_space_write_4(bus_space_tag_t space, bus_space_handle_t handle,
#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 @@ virtio_set_status(struct virtio_softc *sc, int status)
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 @@ virtio_free_interrupts(struct virtio_softc *sc)
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.

201
sys/dev/pci/virtio_pci.c Normal file
View File

@ -0,0 +1,201 @@
/* $NetBSD: virtio_pci.c,v 1.1 2017/08/02 08:39:14 cherry Exp $ */
/*
* 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;
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: virtiovar.h,v 1.9 2017/03/26 12:36:43 martin Exp $ */
/* $NetBSD: virtiovar.h,v 1.10 2017/08/02 08:39:14 cherry Exp $ */
/*
* Copyright (c) 2010 Minoura Makoto.
@ -214,4 +214,9 @@ device_t virtio_child(struct virtio_softc *);
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_ */