1265055Smarcel/*- 2285707Smarcel * Copyright (c) 2014, 2015 Marcel Moolenaar 3265055Smarcel * All rights reserved. 4265055Smarcel * 5265055Smarcel * Redistribution and use in source and binary forms, with or without 6265055Smarcel * modification, are permitted provided that the following conditions 7265055Smarcel * are met: 8265055Smarcel * 1. Redistributions of source code must retain the above copyright 9265055Smarcel * notice, this list of conditions and the following disclaimer. 10265055Smarcel * 2. Redistributions in binary form must reproduce the above copyright 11265055Smarcel * notice, this list of conditions and the following disclaimer in the 12265055Smarcel * documentation and/or other materials provided with the distribution. 13265055Smarcel * 14265055Smarcel * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 15265055Smarcel * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16265055Smarcel * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17265055Smarcel * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 18265055Smarcel * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19265055Smarcel * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20265055Smarcel * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21265055Smarcel * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22265055Smarcel * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23265055Smarcel * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24265055Smarcel */ 25265055Smarcel 26265055Smarcel#include <sys/cdefs.h> 27265055Smarcel__FBSDID("$FreeBSD: releng/11.0/sys/dev/proto/proto_bus_pci.c 285707 2015-07-19 23:37:45Z marcel $"); 28265055Smarcel 29265055Smarcel#include <sys/param.h> 30265055Smarcel#include <sys/systm.h> 31265055Smarcel#include <sys/bus.h> 32265055Smarcel#include <sys/conf.h> 33265055Smarcel#include <sys/kernel.h> 34265055Smarcel#include <sys/module.h> 35265055Smarcel#include <machine/bus.h> 36265055Smarcel#include <sys/rman.h> 37265055Smarcel#include <machine/resource.h> 38265055Smarcel#include <sys/sbuf.h> 39265055Smarcel 40265055Smarcel#include <dev/pci/pcireg.h> 41265055Smarcel#include <dev/pci/pcivar.h> 42265055Smarcel 43265055Smarcel#include <dev/proto/proto.h> 44265055Smarcel 45265055Smarcelstatic int proto_pci_probe(device_t dev); 46265055Smarcelstatic int proto_pci_attach(device_t dev); 47265055Smarcel 48265055Smarcelstatic device_method_t proto_pci_methods[] = { 49265055Smarcel /* Device interface */ 50265055Smarcel DEVMETHOD(device_probe, proto_pci_probe), 51265055Smarcel DEVMETHOD(device_attach, proto_pci_attach), 52265055Smarcel DEVMETHOD(device_detach, proto_detach), 53265055Smarcel DEVMETHOD_END 54265055Smarcel}; 55265055Smarcel 56265055Smarcelstatic driver_t proto_pci_driver = { 57265055Smarcel proto_driver_name, 58265055Smarcel proto_pci_methods, 59265055Smarcel sizeof(struct proto_softc), 60265055Smarcel}; 61265055Smarcel 62285707Smarcelstatic char proto_pci_prefix[] = "pci"; 63285707Smarcelstatic char **proto_pci_devnames; 64285707Smarcel 65265055Smarcelstatic int 66265055Smarcelproto_pci_probe(device_t dev) 67265055Smarcel{ 68265055Smarcel struct sbuf *sb; 69265055Smarcel 70284689Smarcel if ((pci_read_config(dev, PCIR_HDRTYPE, 1) & PCIM_HDRTYPE) != 0) 71265055Smarcel return (ENXIO); 72265055Smarcel 73265055Smarcel sb = sbuf_new_auto(); 74285707Smarcel sbuf_printf(sb, "%s%d:%d:%d:%d", proto_pci_prefix, pci_get_domain(dev), 75265055Smarcel pci_get_bus(dev), pci_get_slot(dev), pci_get_function(dev)); 76265055Smarcel sbuf_finish(sb); 77265055Smarcel device_set_desc_copy(dev, sbuf_data(sb)); 78265055Smarcel sbuf_delete(sb); 79285707Smarcel return (proto_probe(dev, proto_pci_prefix, &proto_pci_devnames)); 80265055Smarcel} 81265055Smarcel 82265055Smarcelstatic int 83265055Smarcelproto_pci_attach(device_t dev) 84265055Smarcel{ 85265055Smarcel struct proto_softc *sc; 86265055Smarcel struct resource *res; 87284315Smarcel uint32_t val; 88265055Smarcel int bar, rid, type; 89265055Smarcel 90265055Smarcel sc = device_get_softc(dev); 91265055Smarcel 92265055Smarcel proto_add_resource(sc, PROTO_RES_PCICFG, 0, NULL); 93284079Smarcel proto_add_resource(sc, PROTO_RES_BUSDMA, 0, NULL); 94265055Smarcel 95265055Smarcel for (bar = 0; bar < PCIR_MAX_BAR_0; bar++) { 96265055Smarcel rid = PCIR_BAR(bar); 97284315Smarcel val = pci_read_config(dev, rid, 4); 98284315Smarcel type = (PCI_BAR_IO(val)) ? SYS_RES_IOPORT : SYS_RES_MEMORY; 99265055Smarcel res = bus_alloc_resource_any(dev, type, &rid, RF_ACTIVE); 100284315Smarcel if (res == NULL) 101284315Smarcel continue; 102284315Smarcel proto_add_resource(sc, type, rid, res); 103284315Smarcel if (type == SYS_RES_IOPORT) 104284315Smarcel continue; 105284315Smarcel /* Skip over adjacent BAR for 64-bit memory BARs. */ 106284315Smarcel if ((val & PCIM_BAR_MEM_TYPE) == PCIM_BAR_MEM_64) 107284315Smarcel bar++; 108265055Smarcel } 109265055Smarcel 110265055Smarcel rid = 0; 111265055Smarcel type = SYS_RES_IRQ; 112265055Smarcel res = bus_alloc_resource_any(dev, type, &rid, RF_ACTIVE | RF_SHAREABLE); 113265055Smarcel if (res != NULL) 114265055Smarcel proto_add_resource(sc, type, rid, res); 115265055Smarcel return (proto_attach(dev)); 116265055Smarcel} 117265055Smarcel 118265055SmarcelDRIVER_MODULE(proto, pci, proto_pci_driver, proto_devclass, NULL, NULL); 119