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