xref: /freebsd/sys/dev/ipmi/ipmi_isa.c (revision edf8578117e8844e02c0121147f45e4609b30680)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
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 #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 #include <sys/efi.h>
40 
41 #include <machine/pci_cfgreg.h>
42 #include <dev/pci/pcireg.h>
43 
44 #include <isa/isavar.h>
45 
46 #ifdef LOCAL_MODULE
47 #include <ipmi.h>
48 #include <ipmivars.h>
49 #else
50 #include <sys/ipmi.h>
51 #include <dev/ipmi/ipmivars.h>
52 #endif
53 
54 static void
55 ipmi_isa_identify(driver_t *driver, device_t parent)
56 {
57 	struct ipmi_get_info info;
58 	uint32_t devid;
59 
60 	if (ipmi_smbios_identify(&info) && info.iface_type != SSIF_MODE &&
61 	    device_find_child(parent, "ipmi", -1) == NULL) {
62 		/*
63 		 * XXX: Hack alert.  On some broken systems, the IPMI
64 		 * interface is described via SMBIOS, but the actual
65 		 * IO resource is in a PCI device BAR, so we have to let
66 		 * the PCI device attach ipmi instead.  In that case don't
67 		 * create an isa ipmi device.  For now we hardcode the list
68 		 * of bus, device, function tuples.
69 		 */
70 		devid = pci_cfgregread(0, 4, 2, PCIR_DEVVENDOR, 4);
71 		if (devid != 0xffffffff &&
72 		    ipmi_pci_match(devid & 0xffff, devid >> 16) != NULL)
73 			return;
74 		BUS_ADD_CHILD(parent, 0, "ipmi", -1);
75 	}
76 }
77 
78 static int
79 ipmi_isa_probe(device_t dev)
80 {
81 
82 	/*
83 	 * Give other drivers precedence.  Unfortunately, this doesn't
84 	 * work if we have an SMBIOS table that duplicates a PCI device
85 	 * that's later on the bus than the PCI-ISA bridge.
86 	 */
87 	if (ipmi_attached)
88 		return (ENXIO);
89 
90 	/* Skip any PNP devices. */
91 	if (isa_get_logicalid(dev) != 0)
92 		return (ENXIO);
93 
94 	device_set_desc(dev, "IPMI System Interface");
95 	return (BUS_PROBE_DEFAULT);
96 }
97 
98 static int
99 ipmi_hint_identify(device_t dev, struct ipmi_get_info *info)
100 {
101 	const char *mode, *name;
102 	int i, unit, val;
103 
104 	/* We require at least a "mode" hint. */
105 	name = device_get_name(dev);
106 	unit = device_get_unit(dev);
107 	if (resource_string_value(name, unit, "mode", &mode) != 0)
108 		return (0);
109 
110 	/* Set the mode and default I/O resources for each mode. */
111 	bzero(info, sizeof(struct ipmi_get_info));
112 	if (strcasecmp(mode, "KCS") == 0) {
113 		info->iface_type = KCS_MODE;
114 		info->address = 0xca2;
115 		info->io_mode = 1;
116 		info->offset = 1;
117 	} else if (strcasecmp(mode, "SMIC") == 0) {
118 		info->iface_type = SMIC_MODE;
119 		info->address = 0xca9;
120 		info->io_mode = 1;
121 		info->offset = 1;
122 	} else if (strcasecmp(mode, "BT") == 0) {
123 		info->iface_type = BT_MODE;
124 		info->address = 0xe4;
125 		info->io_mode = 1;
126 		info->offset = 1;
127 	} else {
128 		device_printf(dev, "Invalid mode %s\n", mode);
129 		return (0);
130 	}
131 
132 	/*
133 	 * Kill any resources that isahint.c might have setup for us
134 	 * since it will conflict with how we do resources.
135 	 */
136 	for (i = 0; i < 2; i++) {
137 		bus_delete_resource(dev, SYS_RES_MEMORY, i);
138 		bus_delete_resource(dev, SYS_RES_IOPORT, i);
139 	}
140 
141 	/* Allow the I/O address to be overridden via hints. */
142 	if (resource_int_value(name, unit, "port", &val) == 0 && val != 0) {
143 		info->address = val;
144 		info->io_mode = 1;
145 	} else if (resource_int_value(name, unit, "maddr", &val) == 0 &&
146 	    val != 0) {
147 		info->address = val;
148 		info->io_mode = 0;
149 	}
150 
151 	/* Allow the spacing to be overridden. */
152 	if (resource_int_value(name, unit, "spacing", &val) == 0) {
153 		switch (val) {
154 		case 8:
155 			info->offset = 1;
156 			break;
157 		case 16:
158 			info->offset = 2;
159 			break;
160 		case 32:
161 			info->offset = 4;
162 			break;
163 		default:
164 			device_printf(dev, "Invalid register spacing\n");
165 			return (0);
166 		}
167 	}
168 	return (1);
169 }
170 
171 static int
172 ipmi_isa_attach(device_t dev)
173 {
174 	struct ipmi_softc *sc = device_get_softc(dev);
175 	struct ipmi_get_info info;
176 	const char *mode;
177 	int count, error, i, type;
178 
179 	/*
180 	 * Pull info out of the SMBIOS table.  If that doesn't work, use
181 	 * hints to enumerate a device.
182 	 */
183 	if (!ipmi_smbios_identify(&info) &&
184 	    !ipmi_hint_identify(dev, &info))
185 		return (ENXIO);
186 
187 	switch (info.iface_type) {
188 	case KCS_MODE:
189 		count = IPMI_IF_KCS_NRES;
190 		mode = "KCS";
191 		break;
192 	case SMIC_MODE:
193 		count = IPMI_IF_SMIC_NRES;
194 		mode = "SMIC";
195 		break;
196 	case BT_MODE:
197 		count = IPMI_IF_BT_NRES;
198 		mode = "BT";
199 		break;
200 	default:
201 		return (ENXIO);
202 	}
203 	error = 0;
204 	sc->ipmi_dev = dev;
205 
206 	device_printf(dev, "%s mode found at %s 0x%jx alignment 0x%x on %s\n",
207 	    mode, info.io_mode ? "io" : "mem",
208 	    (uintmax_t)info.address, info.offset,
209 	    device_get_name(device_get_parent(dev)));
210 	if (info.io_mode)
211 		type = SYS_RES_IOPORT;
212 	else
213 		type = SYS_RES_MEMORY;
214 
215 	sc->ipmi_io_type = type;
216 	sc->ipmi_io_spacing = info.offset;
217 	if (info.offset == 1) {
218 		sc->ipmi_io_rid = 0;
219 		sc->ipmi_io_res[0] = bus_alloc_resource(dev, type,
220 		    &sc->ipmi_io_rid, info.address, info.address + count - 1,
221 		    count, RF_ACTIVE);
222 		if (sc->ipmi_io_res[0] == NULL) {
223 			device_printf(dev, "couldn't configure I/O resource\n");
224 			return (ENXIO);
225 		}
226 	} else {
227 		for (i = 0; i < count; i++) {
228 			sc->ipmi_io_rid = i;
229 			sc->ipmi_io_res[i] = bus_alloc_resource(dev, type,
230 			    &sc->ipmi_io_rid, info.address + i * info.offset,
231 			    info.address + i * info.offset, 1, RF_ACTIVE);
232 			if (sc->ipmi_io_res[i] == NULL) {
233 				device_printf(dev,
234 				    "couldn't configure I/O resource\n");
235 				error = ENXIO;
236 				sc->ipmi_io_rid = 0;
237 				goto bad;
238 			}
239 		}
240 		sc->ipmi_io_rid = 0;
241 	}
242 
243 	if (info.irq != 0) {
244 		sc->ipmi_irq_rid = 0;
245 		sc->ipmi_irq_res = bus_alloc_resource(dev, SYS_RES_IRQ,
246 		    &sc->ipmi_irq_rid, info.irq, info.irq, 1,
247 		    RF_SHAREABLE | RF_ACTIVE);
248 	}
249 
250 	error = ENXIO;
251 	switch (info.iface_type) {
252 	case KCS_MODE:
253 		error = ipmi_kcs_attach(sc);
254 		break;
255 	case SMIC_MODE:
256 		error = ipmi_smic_attach(sc);
257 		break;
258 	case BT_MODE:
259 		error = ipmi_bt_attach(sc);
260 		break;
261 	}
262 
263 	if (error)
264 		goto bad;
265 	error = ipmi_attach(dev);
266 	if (error)
267 		goto bad;
268 
269 	return (0);
270 bad:
271 	ipmi_release_resources(dev);
272 	return (error);
273 }
274 
275 static device_method_t ipmi_methods[] = {
276 	/* Device interface */
277 	DEVMETHOD(device_identify,      ipmi_isa_identify),
278 	DEVMETHOD(device_probe,		ipmi_isa_probe),
279 	DEVMETHOD(device_attach,	ipmi_isa_attach),
280 	DEVMETHOD(device_detach,	ipmi_detach),
281 	{ 0, 0 }
282 };
283 
284 static driver_t ipmi_isa_driver = {
285 	"ipmi",
286 	ipmi_methods,
287 	sizeof(struct ipmi_softc),
288 };
289 
290 DRIVER_MODULE(ipmi_isa, isa, ipmi_isa_driver, 0, 0);
291 #ifdef ARCH_MAY_USE_EFI
292 MODULE_DEPEND(ipmi_isa, efirt, 1, 1, 1);
293 #endif
294