1 /*- 2 * Copyright (c) 2006 IronPort Systems Inc. <ambrisko@ironport.com> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 __FBSDID("$FreeBSD$"); 29 30 #include <sys/param.h> 31 #include <sys/systm.h> 32 #include <sys/bus.h> 33 #include <sys/condvar.h> 34 #include <sys/eventhandler.h> 35 #include <sys/kernel.h> 36 #include <sys/module.h> 37 #include <sys/rman.h> 38 #include <sys/selinfo.h> 39 40 #include <machine/pci_cfgreg.h> 41 #include <dev/pci/pcireg.h> 42 43 #include <isa/isavar.h> 44 45 #ifdef LOCAL_MODULE 46 #include <ipmi.h> 47 #include <ipmivars.h> 48 #else 49 #include <sys/ipmi.h> 50 #include <dev/ipmi/ipmivars.h> 51 #endif 52 53 static void 54 ipmi_isa_identify(driver_t *driver, device_t parent) 55 { 56 struct ipmi_get_info info; 57 uint32_t devid; 58 59 if (ipmi_smbios_identify(&info) && info.iface_type != SSIF_MODE && 60 device_find_child(parent, "ipmi", -1) == NULL) { 61 /* 62 * XXX: Hack alert. On some broken systems, the IPMI 63 * interface is described via SMBIOS, but the actual 64 * IO resource is in a PCI device BAR, so we have to let 65 * the PCI device attach ipmi instead. In that case don't 66 * create an isa ipmi device. For now we hardcode the list 67 * of bus, device, function tuples. 68 */ 69 devid = pci_cfgregread(0, 4, 2, PCIR_DEVVENDOR, 4); 70 if (devid != 0xffffffff && 71 ipmi_pci_match(devid & 0xffff, devid >> 16) != NULL) 72 return; 73 BUS_ADD_CHILD(parent, 0, "ipmi", -1); 74 } 75 } 76 77 static int 78 ipmi_isa_probe(device_t dev) 79 { 80 81 /* Skip any PNP devices. */ 82 if (isa_get_logicalid(dev) != 0) 83 return (ENXIO); 84 85 device_set_desc(dev, "IPMI System Interface"); 86 return (BUS_PROBE_DEFAULT); 87 } 88 89 static int 90 ipmi_isa_attach(device_t dev) 91 { 92 struct ipmi_softc *sc = device_get_softc(dev); 93 struct ipmi_get_info info; 94 const char *mode; 95 int count, error, i, type; 96 97 /* This should never fail. */ 98 if (!ipmi_smbios_identify(&info)) 99 return (ENXIO); 100 101 /* 102 * Give other drivers precedence. Unfortunately, this doesn't 103 * work if we have an SMBIOS table that duplicates a PCI device 104 * that's later on the bus than the PCI-ISA bridge. 105 */ 106 if (ipmi_attached) 107 return (EBUSY); 108 109 switch (info.iface_type) { 110 case KCS_MODE: 111 count = 2; 112 mode = "KCS"; 113 break; 114 case SMIC_MODE: 115 count = 3; 116 mode = "SMIC"; 117 break; 118 case BT_MODE: 119 device_printf(dev, "BT mode is unsupported\n"); 120 return (ENXIO); 121 default: 122 return (ENXIO); 123 } 124 error = 0; 125 sc->ipmi_dev = dev; 126 127 device_printf(dev, "%s mode found at %s 0x%jx alignment 0x%x on %s\n", 128 mode, info.io_mode ? "io" : "mem", 129 (uintmax_t)info.address, info.offset, 130 device_get_name(device_get_parent(dev))); 131 if (info.io_mode) 132 type = SYS_RES_IOPORT; 133 else 134 type = SYS_RES_MEMORY; 135 136 sc->ipmi_io_type = type; 137 sc->ipmi_io_spacing = info.offset; 138 if (info.offset == 1) { 139 sc->ipmi_io_rid = 0; 140 sc->ipmi_io_res[0] = bus_alloc_resource(dev, type, 141 &sc->ipmi_io_rid, info.address, info.address + count - 1, 142 count, RF_ACTIVE); 143 if (sc->ipmi_io_res[0] == NULL) { 144 device_printf(dev, "couldn't configure I/O resource\n"); 145 return (ENXIO); 146 } 147 } else { 148 for (i = 0; i < count; i++) { 149 sc->ipmi_io_rid = i; 150 sc->ipmi_io_res[i] = bus_alloc_resource(dev, type, 151 &sc->ipmi_io_rid, info.address + i * info.offset, 152 info.address + i * info.offset, 1, RF_ACTIVE); 153 if (sc->ipmi_io_res[i] == NULL) { 154 device_printf(dev, 155 "couldn't configure I/O resource\n"); 156 error = ENXIO; 157 sc->ipmi_io_rid = 0; 158 goto bad; 159 } 160 } 161 sc->ipmi_io_rid = 0; 162 } 163 164 if (info.irq != 0) { 165 sc->ipmi_irq_rid = 0; 166 sc->ipmi_irq_res = bus_alloc_resource(dev, SYS_RES_IRQ, 167 &sc->ipmi_irq_rid, info.irq, info.irq, 1, 168 RF_SHAREABLE | RF_ACTIVE); 169 } 170 171 switch (info.iface_type) { 172 case KCS_MODE: 173 error = ipmi_kcs_attach(sc); 174 if (error) 175 goto bad; 176 break; 177 case SMIC_MODE: 178 error = ipmi_smic_attach(sc); 179 if (error) 180 goto bad; 181 break; 182 } 183 184 error = ipmi_attach(dev); 185 if (error) 186 goto bad; 187 188 return (0); 189 bad: 190 ipmi_release_resources(dev); 191 return (error); 192 } 193 194 static device_method_t ipmi_methods[] = { 195 /* Device interface */ 196 DEVMETHOD(device_identify, ipmi_isa_identify), 197 DEVMETHOD(device_probe, ipmi_isa_probe), 198 DEVMETHOD(device_attach, ipmi_isa_attach), 199 DEVMETHOD(device_detach, ipmi_detach), 200 { 0, 0 } 201 }; 202 203 static driver_t ipmi_isa_driver = { 204 "ipmi", 205 ipmi_methods, 206 sizeof(struct ipmi_softc), 207 }; 208 209 DRIVER_MODULE(ipmi_isa, isa, ipmi_isa_driver, ipmi_devclass, 0, 0); 210