xref: /freebsd/sys/dev/ipmi/ipmi_isa.c (revision 5b9c547c)
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 	/*
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 overriden 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 overriden. */
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 = 2;
189 		mode = "KCS";
190 		break;
191 	case SMIC_MODE:
192 		count = 3;
193 		mode = "SMIC";
194 		break;
195 	case BT_MODE:
196 		device_printf(dev, "BT mode is unsupported\n");
197 		return (ENXIO);
198 	default:
199 		return (ENXIO);
200 	}
201 	error = 0;
202 	sc->ipmi_dev = dev;
203 
204 	device_printf(dev, "%s mode found at %s 0x%jx alignment 0x%x on %s\n",
205 	    mode, info.io_mode ? "io" : "mem",
206 	    (uintmax_t)info.address, info.offset,
207 	    device_get_name(device_get_parent(dev)));
208 	if (info.io_mode)
209 		type = SYS_RES_IOPORT;
210 	else
211 		type = SYS_RES_MEMORY;
212 
213 	sc->ipmi_io_type = type;
214 	sc->ipmi_io_spacing = info.offset;
215 	if (info.offset == 1) {
216 		sc->ipmi_io_rid = 0;
217 		sc->ipmi_io_res[0] = bus_alloc_resource(dev, type,
218 		    &sc->ipmi_io_rid, info.address, info.address + count - 1,
219 		    count, RF_ACTIVE);
220 		if (sc->ipmi_io_res[0] == NULL) {
221 			device_printf(dev, "couldn't configure I/O resource\n");
222 			return (ENXIO);
223 		}
224 	} else {
225 		for (i = 0; i < count; i++) {
226 			sc->ipmi_io_rid = i;
227 			sc->ipmi_io_res[i] = bus_alloc_resource(dev, type,
228 			    &sc->ipmi_io_rid, info.address + i * info.offset,
229 			    info.address + i * info.offset, 1, RF_ACTIVE);
230 			if (sc->ipmi_io_res[i] == NULL) {
231 				device_printf(dev,
232 				    "couldn't configure I/O resource\n");
233 				error = ENXIO;
234 				sc->ipmi_io_rid = 0;
235 				goto bad;
236 			}
237 		}
238 		sc->ipmi_io_rid = 0;
239 	}
240 
241 	if (info.irq != 0) {
242 		sc->ipmi_irq_rid = 0;
243 		sc->ipmi_irq_res = bus_alloc_resource(dev, SYS_RES_IRQ,
244 		    &sc->ipmi_irq_rid, info.irq, info.irq, 1,
245 		    RF_SHAREABLE | RF_ACTIVE);
246 	}
247 
248 	switch (info.iface_type) {
249 	case KCS_MODE:
250 		error = ipmi_kcs_attach(sc);
251 		if (error)
252 			goto bad;
253 		break;
254 	case SMIC_MODE:
255 		error = ipmi_smic_attach(sc);
256 		if (error)
257 			goto bad;
258 		break;
259 	}
260 
261 	error = ipmi_attach(dev);
262 	if (error)
263 		goto bad;
264 
265 	return (0);
266 bad:
267 	ipmi_release_resources(dev);
268 	return (error);
269 }
270 
271 static device_method_t ipmi_methods[] = {
272 	/* Device interface */
273 	DEVMETHOD(device_identify,      ipmi_isa_identify),
274 	DEVMETHOD(device_probe,		ipmi_isa_probe),
275 	DEVMETHOD(device_attach,	ipmi_isa_attach),
276 	DEVMETHOD(device_detach,	ipmi_detach),
277 	{ 0, 0 }
278 };
279 
280 static driver_t ipmi_isa_driver = {
281 	"ipmi",
282 	ipmi_methods,
283 	sizeof(struct ipmi_softc),
284 };
285 
286 DRIVER_MODULE(ipmi_isa, isa, ipmi_isa_driver, ipmi_devclass, 0, 0);
287