proto_bus_pci.c revision 284689
1265055Smarcel/*- 2265055Smarcel * Copyright (c) 2014 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: head/sys/dev/proto/proto_bus_pci.c 284689 2015-06-22 00:34:29Z 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 62265055Smarcelstatic int 63265055Smarcelproto_pci_probe(device_t dev) 64265055Smarcel{ 65265055Smarcel struct sbuf *sb; 66265055Smarcel 67284689Smarcel if ((pci_read_config(dev, PCIR_HDRTYPE, 1) & PCIM_HDRTYPE) != 0) 68265055Smarcel return (ENXIO); 69265055Smarcel 70265055Smarcel sb = sbuf_new_auto(); 71265055Smarcel sbuf_printf(sb, "pci%d:%d:%d:%d", pci_get_domain(dev), 72265055Smarcel pci_get_bus(dev), pci_get_slot(dev), pci_get_function(dev)); 73265055Smarcel sbuf_finish(sb); 74265055Smarcel device_set_desc_copy(dev, sbuf_data(sb)); 75265055Smarcel sbuf_delete(sb); 76265055Smarcel return (BUS_PROBE_HOOVER); 77265055Smarcel} 78265055Smarcel 79265055Smarcelstatic int 80265055Smarcelproto_pci_attach(device_t dev) 81265055Smarcel{ 82265055Smarcel struct proto_softc *sc; 83265055Smarcel struct resource *res; 84284315Smarcel uint32_t val; 85265055Smarcel int bar, rid, type; 86265055Smarcel 87265055Smarcel sc = device_get_softc(dev); 88265055Smarcel 89265055Smarcel proto_add_resource(sc, PROTO_RES_PCICFG, 0, NULL); 90284079Smarcel proto_add_resource(sc, PROTO_RES_BUSDMA, 0, NULL); 91265055Smarcel 92265055Smarcel for (bar = 0; bar < PCIR_MAX_BAR_0; bar++) { 93265055Smarcel rid = PCIR_BAR(bar); 94284315Smarcel val = pci_read_config(dev, rid, 4); 95284315Smarcel type = (PCI_BAR_IO(val)) ? SYS_RES_IOPORT : SYS_RES_MEMORY; 96265055Smarcel res = bus_alloc_resource_any(dev, type, &rid, RF_ACTIVE); 97284315Smarcel if (res == NULL) 98284315Smarcel continue; 99284315Smarcel proto_add_resource(sc, type, rid, res); 100284315Smarcel if (type == SYS_RES_IOPORT) 101284315Smarcel continue; 102284315Smarcel /* Skip over adjacent BAR for 64-bit memory BARs. */ 103284315Smarcel if ((val & PCIM_BAR_MEM_TYPE) == PCIM_BAR_MEM_64) 104284315Smarcel bar++; 105265055Smarcel } 106265055Smarcel 107265055Smarcel rid = 0; 108265055Smarcel type = SYS_RES_IRQ; 109265055Smarcel res = bus_alloc_resource_any(dev, type, &rid, RF_ACTIVE | RF_SHAREABLE); 110265055Smarcel if (res != NULL) 111265055Smarcel proto_add_resource(sc, type, rid, res); 112265055Smarcel return (proto_attach(dev)); 113265055Smarcel} 114265055Smarcel 115265055SmarcelDRIVER_MODULE(proto, pci, proto_pci_driver, proto_devclass, NULL, NULL); 116