1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2006 IronPort Systems Inc. <ambrisko@ironport.com> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 #include <sys/param.h> 33 #include <sys/systm.h> 34 #include <sys/bus.h> 35 #include <sys/condvar.h> 36 #include <sys/eventhandler.h> 37 #include <sys/kernel.h> 38 #include <sys/module.h> 39 #include <sys/rman.h> 40 #include <sys/selinfo.h> 41 42 #include <dev/pci/pcireg.h> 43 #include <dev/pci/pcivar.h> 44 45 #ifdef LOCAL_MODULE 46 #include <ipmivars.h> 47 #else 48 #include <dev/ipmi/ipmivars.h> 49 #endif 50 51 static int ipmi_pci_probe(device_t dev); 52 static int ipmi_pci_attach(device_t dev); 53 54 static struct ipmi_ident 55 { 56 u_int16_t vendor; 57 u_int16_t device; 58 char *desc; 59 } ipmi_identifiers[] = { 60 {0x1028, 0x000d, "Dell PE2650 SMIC interface"}, 61 {0, 0, 0} 62 }; 63 64 const char * 65 ipmi_pci_match(uint16_t vendor, uint16_t device) 66 { 67 struct ipmi_ident *m; 68 69 for (m = ipmi_identifiers; m->vendor != 0; m++) 70 if (m->vendor == vendor && m->device == device) 71 return (m->desc); 72 return (NULL); 73 } 74 75 static int 76 ipmi_pci_probe(device_t dev) 77 { 78 const char *desc; 79 80 if (ipmi_attached) 81 return (ENXIO); 82 83 desc = ipmi_pci_match(pci_get_vendor(dev), pci_get_device(dev)); 84 if (desc != NULL) { 85 device_set_desc(dev, desc); 86 return (BUS_PROBE_DEFAULT); 87 } 88 89 return (ENXIO); 90 } 91 92 static int 93 ipmi_pci_attach(device_t dev) 94 { 95 struct ipmi_softc *sc = device_get_softc(dev); 96 struct ipmi_get_info info; 97 const char *mode; 98 int error, type; 99 100 /* Look for an IPMI entry in the SMBIOS table. */ 101 if (!ipmi_smbios_identify(&info)) 102 return (ENXIO); 103 104 sc->ipmi_dev = dev; 105 106 switch (info.iface_type) { 107 case KCS_MODE: 108 mode = "KCS"; 109 break; 110 case SMIC_MODE: 111 mode = "SMIC"; 112 break; 113 case BT_MODE: 114 device_printf(dev, "BT mode is unsupported\n"); 115 return (ENXIO); 116 default: 117 device_printf(dev, "No IPMI interface found\n"); 118 return (ENXIO); 119 } 120 121 device_printf(dev, "%s mode found at %s 0x%jx alignment 0x%x on %s\n", 122 mode, info.io_mode ? "io" : "mem", 123 (uintmax_t)info.address, info.offset, 124 device_get_name(device_get_parent(dev))); 125 if (info.io_mode) 126 type = SYS_RES_IOPORT; 127 else 128 type = SYS_RES_MEMORY; 129 130 sc->ipmi_io_rid = PCIR_BAR(0); 131 sc->ipmi_io_res[0] = bus_alloc_resource_any(dev, type, 132 &sc->ipmi_io_rid, RF_ACTIVE); 133 sc->ipmi_io_type = type; 134 sc->ipmi_io_spacing = info.offset; 135 136 if (sc->ipmi_io_res[0] == NULL) { 137 device_printf(dev, "couldn't configure pci io res\n"); 138 return (ENXIO); 139 } 140 141 sc->ipmi_irq_rid = 0; 142 sc->ipmi_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, 143 &sc->ipmi_irq_rid, RF_SHAREABLE | RF_ACTIVE); 144 145 switch (info.iface_type) { 146 case KCS_MODE: 147 error = ipmi_kcs_attach(sc); 148 if (error) 149 goto bad; 150 break; 151 case SMIC_MODE: 152 error = ipmi_smic_attach(sc); 153 if (error) 154 goto bad; 155 break; 156 } 157 error = ipmi_attach(dev); 158 if (error) 159 goto bad; 160 161 return (0); 162 bad: 163 ipmi_release_resources(dev); 164 return (error); 165 } 166 167 static device_method_t ipmi_methods[] = { 168 /* Device interface */ 169 DEVMETHOD(device_probe, ipmi_pci_probe), 170 DEVMETHOD(device_attach, ipmi_pci_attach), 171 DEVMETHOD(device_detach, ipmi_detach), 172 { 0, 0 } 173 }; 174 175 static driver_t ipmi_pci_driver = { 176 "ipmi", 177 ipmi_methods, 178 sizeof(struct ipmi_softc) 179 }; 180 181 DRIVER_MODULE(ipmi_pci, pci, ipmi_pci_driver, ipmi_devclass, 0, 0); 182 MODULE_DEPEND(ipmi_pci, smbios, 1, 1, 1); 183 184 /* Native IPMI on PCI driver. */ 185 186 static int 187 ipmi2_pci_probe(device_t dev) 188 { 189 190 if (pci_get_class(dev) == PCIC_SERIALBUS && 191 pci_get_subclass(dev) == PCIS_SERIALBUS_IPMI) { 192 device_set_desc(dev, "IPMI System Interface"); 193 return (BUS_PROBE_GENERIC); 194 } 195 196 return (ENXIO); 197 } 198 199 static int 200 ipmi2_pci_attach(device_t dev) 201 { 202 struct ipmi_softc *sc; 203 int error, iface, type; 204 205 sc = device_get_softc(dev); 206 sc->ipmi_dev = dev; 207 208 /* Interface is determined by progif. */ 209 switch (pci_get_progif(dev)) { 210 case PCIP_SERIALBUS_IPMI_SMIC: 211 iface = SMIC_MODE; 212 break; 213 case PCIP_SERIALBUS_IPMI_KCS: 214 iface = KCS_MODE; 215 break; 216 case PCIP_SERIALBUS_IPMI_BT: 217 iface = BT_MODE; 218 device_printf(dev, "BT interface unsupported\n"); 219 return (ENXIO); 220 default: 221 device_printf(dev, "Unsupported interface: %d\n", 222 pci_get_progif(dev)); 223 return (ENXIO); 224 } 225 226 /* Check the BAR to determine our resource type. */ 227 sc->ipmi_io_rid = PCIR_BAR(0); 228 if (PCI_BAR_IO(pci_read_config(dev, PCIR_BAR(0), 4))) 229 type = SYS_RES_IOPORT; 230 else 231 type = SYS_RES_MEMORY; 232 sc->ipmi_io_type = type; 233 sc->ipmi_io_spacing = 1; 234 sc->ipmi_io_res[0] = bus_alloc_resource_any(dev, type, 235 &sc->ipmi_io_rid, RF_ACTIVE); 236 if (sc->ipmi_io_res[0] == NULL) { 237 device_printf(dev, "couldn't map ports/memory\n"); 238 return (ENXIO); 239 } 240 241 sc->ipmi_irq_rid = 0; 242 sc->ipmi_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, 243 &sc->ipmi_irq_rid, RF_SHAREABLE | RF_ACTIVE); 244 245 switch (iface) { 246 case KCS_MODE: 247 device_printf(dev, "using KSC interface\n"); 248 249 /* 250 * We have to examine the resource directly to determine the 251 * alignment. 252 */ 253 if (!ipmi_kcs_probe_align(sc)) { 254 device_printf(dev, "Unable to determine alignment\n"); 255 error = ENXIO; 256 goto bad; 257 } 258 259 error = ipmi_kcs_attach(sc); 260 if (error) 261 goto bad; 262 break; 263 case SMIC_MODE: 264 device_printf(dev, "using SMIC interface\n"); 265 266 error = ipmi_smic_attach(sc); 267 if (error) 268 goto bad; 269 break; 270 } 271 error = ipmi_attach(dev); 272 if (error) 273 goto bad; 274 275 return (0); 276 bad: 277 ipmi_release_resources(dev); 278 return (error); 279 } 280 281 static device_method_t ipmi2_methods[] = { 282 /* Device interface */ 283 DEVMETHOD(device_probe, ipmi2_pci_probe), 284 DEVMETHOD(device_attach, ipmi2_pci_attach), 285 DEVMETHOD(device_detach, ipmi_detach), 286 { 0, 0 } 287 }; 288 289 static driver_t ipmi2_pci_driver = { 290 "ipmi", 291 ipmi2_methods, 292 sizeof(struct ipmi_softc) 293 }; 294 295 DRIVER_MODULE(ipmi2_pci, pci, ipmi2_pci_driver, ipmi_devclass, 0, 0); 296