xref: /freebsd/sys/dev/ipmi/ipmi_isa.c (revision aa0a1e58)
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_hint_identify(device_t dev, struct ipmi_get_info *info)
91 {
92 	const char *mode, *name;
93 	int i, unit, val;
94 
95 	/* We require at least a "mode" hint. */
96 	name = device_get_name(dev);
97 	unit = device_get_unit(dev);
98 	if (resource_string_value(name, unit, "mode", &mode) != 0)
99 		return (0);
100 
101 	/* Set the mode and default I/O resources for each mode. */
102 	bzero(info, sizeof(struct ipmi_get_info));
103 	if (strcasecmp(mode, "KCS") == 0) {
104 		info->iface_type = KCS_MODE;
105 		info->address = 0xca2;
106 		info->io_mode = 1;
107 		info->offset = 1;
108 	} else if (strcasecmp(mode, "SMIC") == 0) {
109 		info->iface_type = SMIC_MODE;
110 		info->address = 0xca9;
111 		info->io_mode = 1;
112 		info->offset = 1;
113 	} else if (strcasecmp(mode, "BT") == 0) {
114 		info->iface_type = BT_MODE;
115 		info->address = 0xe4;
116 		info->io_mode = 1;
117 		info->offset = 1;
118 	} else {
119 		device_printf(dev, "Invalid mode %s\n", mode);
120 		return (0);
121 	}
122 
123 	/*
124 	 * Kill any resources that isahint.c might have setup for us
125 	 * since it will conflict with how we do resources.
126 	 */
127 	for (i = 0; i < 2; i++) {
128 		bus_delete_resource(dev, SYS_RES_MEMORY, i);
129 		bus_delete_resource(dev, SYS_RES_IOPORT, i);
130 	}
131 
132 	/* Allow the I/O address to be overriden via hints. */
133 	if (resource_int_value(name, unit, "port", &val) == 0 && val != 0) {
134 		info->address = val;
135 		info->io_mode = 1;
136 	} else if (resource_int_value(name, unit, "maddr", &val) == 0 &&
137 	    val != 0) {
138 		info->address = val;
139 		info->io_mode = 0;
140 	}
141 
142 	/* Allow the spacing to be overriden. */
143 	if (resource_int_value(name, unit, "spacing", &val) == 0) {
144 		switch (val) {
145 		case 8:
146 			info->offset = 1;
147 			break;
148 		case 16:
149 			info->offset = 2;
150 			break;
151 		case 32:
152 			info->offset = 4;
153 			break;
154 		default:
155 			device_printf(dev, "Invalid register spacing\n");
156 			return (0);
157 		}
158 	}
159 	return (1);
160 }
161 
162 static int
163 ipmi_isa_attach(device_t dev)
164 {
165 	struct ipmi_softc *sc = device_get_softc(dev);
166 	struct ipmi_get_info info;
167 	const char *mode;
168 	int count, error, i, type;
169 
170 	/*
171 	 * Pull info out of the SMBIOS table.  If that doesn't work, use
172 	 * hints to enumerate a device.
173 	 */
174 	if (!ipmi_smbios_identify(&info) &&
175 	    !ipmi_hint_identify(dev, &info))
176 		return (ENXIO);
177 
178 	/*
179 	 * Give other drivers precedence.  Unfortunately, this doesn't
180 	 * work if we have an SMBIOS table that duplicates a PCI device
181 	 * that's later on the bus than the PCI-ISA bridge.
182 	 */
183 	if (ipmi_attached)
184 		return (EBUSY);
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