freebsd-skq/sys/dev/vmd/vmd_bus.c
Doug Ambrisko 0c852bb9b9 Add support for some more Intel VMD controllers. Some of the
newer controller have a sparce bus space that can be figured
out by probing the HW.  This gives the starting bus number.
When reading the PCI config. space behind the VMD controller,
the offset of the starting bus needs to be subtracted from
the bus being read.

Fixed a bug in which in which not all of the devices
directly attached to the VMD controller would be probed.
On my initial test HW, a switch was found at bus 0, slot 0
and function 0.  All of the NVME drives were behind that
switch.  Now scan for all slots and functions attached to
bus 0.  If a something was found then run attach after the
scan.  On detach also go through all slots and functions
on bus 0.

Tested with device ID's: 0x201d & 0x9a0b

Tested by:	nc@
MFC after:	7 days
PR:		252253
2021-01-28 15:12:14 -08:00

221 lines
5.8 KiB
C

/*-
* SPDX-License-Identifier: BSD-2-Clause
*
* Copyright 2019 Cisco Systems, Inc.
* 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 AND CONTRIBUTORS ``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 OR CONTRIBUTORS 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>
__FBSDID("$FreeBSD$");
#include <sys/types.h>
#include <sys/param.h>
#include <sys/bus.h>
#include <sys/conf.h>
#include <sys/kernel.h>
#include <sys/module.h>
#include <sys/systm.h>
#include <sys/malloc.h>
#include <machine/bus.h>
#include <machine/resource.h>
#include <sys/rman.h>
#include <sys/taskqueue.h>
#include <sys/pciio.h>
#include <dev/pci/pcivar.h>
#include <dev/pci/pcireg.h>
#include <dev/pci/pci_private.h>
#include <dev/pci/pcib_private.h>
#include <dev/pci/pci_host_generic.h>
#include <dev/vmd/vmd.h>
#include "pcib_if.h"
#include "pci_if.h"
static int
vmd_bus_probe(device_t dev)
{
device_set_desc(dev, "VMD bus");
return (-1000);
}
/* PCI interface. */
static int
vmd_bus_attach(device_t dev)
{
struct vmd_softc *sc;
struct pci_devinfo *dinfo;
rman_res_t start, end;
int b, s, f;
int found;
sc = device_get_softc(device_get_parent(dev));
/*
* Start at max PCI vmd_domain and work down. Only VMD
* starting bus is connect to VMD device directly. Scan al
* lslots and function connected to starting bus.
*/
b = sc->vmd_bus_start;
found = 0;
for (s = 0; s < PCI_SLOTMAX; s++) {
for (f = 0; f < PCI_FUNCMAX; f++) {
dinfo = pci_read_device(device_get_parent(dev), dev,
PCI_DOMAINMAX - device_get_unit(
device_get_parent(dev)), b, s, f);
if (dinfo != NULL) {
found = 1;
pci_add_child(dev, dinfo);
start = rman_get_start(sc->vmd_regs_resource[1]);
end = rman_get_end(sc->vmd_regs_resource[1]);
resource_list_add_next(&dinfo->resources,
SYS_RES_MEMORY, start, end, end - start + 1);
start = rman_get_start(sc->vmd_io_resource);
end = rman_get_end(sc->vmd_io_resource);
resource_list_add_next(&dinfo->resources,
SYS_RES_IOPORT, start, end, end - start + 1);
}
}
}
if (found) {
bus_generic_attach(dev);
}
return (0);
}
static int
vmd_bus_detach(device_t dev)
{
struct vmd_softc *sc;
struct pci_devinfo *dinfo;
int b, s, f;
device_delete_children(dev);
sc = device_get_softc(device_get_parent(dev));
b = sc->vmd_bus_start;
for (s = 0; s < PCI_SLOTMAX; s++) {
for (f = 0; f < PCI_FUNCMAX; f++) {
dinfo = pci_read_device(device_get_parent(dev), dev,
PCI_DOMAINMAX - device_get_unit(
device_get_parent(dev)), b, s, f);
if (dinfo != NULL)
resource_list_free(&dinfo->resources);
}
}
return (0);
}
static int
vmd_bus_adjust_resource(device_t dev, device_t child, int type,
struct resource *r, rman_res_t start, rman_res_t end)
{
struct resource *res = r;
if (type == SYS_RES_MEMORY) {
/* VMD device controls this */
return (0);
}
return (bus_generic_adjust_resource(dev, child, type, res, start, end));
}
static int
vmd_bus_release_resource(device_t dev, device_t child, int type, int rid,
struct resource *r)
{
if (type == SYS_RES_MEMORY) {
/* VMD device controls this */
return (0);
}
return (pci_release_resource(dev, child, type, rid, r));
}
static struct resource *
vmd_bus_alloc_resource(device_t dev, device_t child, int type, int *rid,
rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
{
struct vmd_softc *sc;
sc = device_get_softc(device_get_parent(dev));
if (type == SYS_RES_MEMORY) {
/* remap to VMD resources */
if (*rid == PCIR_MEMBASE_1) {
return (sc->vmd_regs_resource[1]);
} else if (*rid == PCIR_PMBASEL_1) {
return (sc->vmd_regs_resource[2]);
} else {
return (sc->vmd_regs_resource[2]);
}
}
return (pci_alloc_resource(dev, child, type, rid, start, end,
count, flags));
}
static int
vmd_bus_shutdown(device_t dev)
{
return (0);
}
static device_method_t vmd_bus_pci_methods[] = {
/* Device interface */
DEVMETHOD(device_probe, vmd_bus_probe),
DEVMETHOD(device_attach, vmd_bus_attach),
DEVMETHOD(device_detach, vmd_bus_detach),
DEVMETHOD(device_shutdown, vmd_bus_shutdown),
/* Bus interface */
DEVMETHOD(bus_alloc_resource, vmd_bus_alloc_resource),
DEVMETHOD(bus_adjust_resource, vmd_bus_adjust_resource),
DEVMETHOD(bus_release_resource, vmd_bus_release_resource),
/* pci interface */
DEVMETHOD(pci_read_config, pci_read_config_method),
DEVMETHOD(pci_write_config, pci_write_config_method),
DEVMETHOD(pci_alloc_devinfo, pci_alloc_devinfo_method),
DEVMETHOD_END
};
static devclass_t vmd_bus_devclass;
DEFINE_CLASS_1(vmd_bus, vmd_bus_pci_driver, vmd_bus_pci_methods,
sizeof(struct pci_softc), pci_driver);
DRIVER_MODULE(vmd_bus, vmd, vmd_bus_pci_driver, vmd_bus_devclass, NULL, NULL);
MODULE_VERSION(vmd_bus, 1);