xref: /freebsd/sys/dev/ipmi/ipmi_pci.c (revision 258a0d760aa8b42899a000e30f610f900a402556)
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 __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 #include <sys/efi.h>
42 
43 #include <dev/pci/pcireg.h>
44 #include <dev/pci/pcivar.h>
45 
46 #ifdef LOCAL_MODULE
47 #include <ipmivars.h>
48 #else
49 #include <dev/ipmi/ipmivars.h>
50 #endif
51 
52 static int ipmi_pci_probe(device_t dev);
53 static int ipmi_pci_attach(device_t dev);
54 
55 static struct ipmi_ident
56 {
57 	u_int16_t	vendor;
58 	u_int16_t	device;
59 	char		*desc;
60 } ipmi_identifiers[] = {
61 	{0x1028, 0x000d, "Dell PE2650 SMIC interface"},
62 	{0, 0, 0}
63 };
64 
65 const char *
66 ipmi_pci_match(uint16_t vendor, uint16_t device)
67 {
68 	struct ipmi_ident *m;
69 
70 	for (m = ipmi_identifiers; m->vendor != 0; m++)
71 		if (m->vendor == vendor && m->device == device)
72 			return (m->desc);
73 	return (NULL);
74 }
75 
76 static int
77 ipmi_pci_probe(device_t dev)
78 {
79 	const char *desc;
80 
81 	if (ipmi_attached)
82 		return (ENXIO);
83 
84 	desc = ipmi_pci_match(pci_get_vendor(dev), pci_get_device(dev));
85 	if (desc != NULL) {
86 		device_set_desc(dev, desc);
87 		return (BUS_PROBE_DEFAULT);
88 	}
89 
90 	return (ENXIO);
91 }
92 
93 static int
94 ipmi_pci_attach(device_t dev)
95 {
96 	struct ipmi_softc *sc = device_get_softc(dev);
97 	struct ipmi_get_info info;
98 	const char *mode;
99 	int error, type;
100 
101 	/* Look for an IPMI entry in the SMBIOS table. */
102 	if (!ipmi_smbios_identify(&info))
103 		return (ENXIO);
104 
105 	sc->ipmi_dev = dev;
106 
107 	switch (info.iface_type) {
108 	case KCS_MODE:
109 		mode = "KCS";
110 		break;
111 	case SMIC_MODE:
112 		mode = "SMIC";
113 		break;
114 	case BT_MODE:
115 		mode = "BT";
116 		break;
117 	default:
118 		device_printf(dev, "No IPMI interface found\n");
119 		return (ENXIO);
120 	}
121 
122 	device_printf(dev, "%s mode found at %s 0x%jx alignment 0x%x on %s\n",
123 	    mode, info.io_mode ? "io" : "mem",
124 	    (uintmax_t)info.address, info.offset,
125 	    device_get_name(device_get_parent(dev)));
126 	if (info.io_mode)
127 		type = SYS_RES_IOPORT;
128 	else
129 		type = SYS_RES_MEMORY;
130 
131 	sc->ipmi_io_rid = PCIR_BAR(0);
132 	sc->ipmi_io_res[0] = bus_alloc_resource_any(dev, type,
133 	    &sc->ipmi_io_rid, RF_ACTIVE);
134 	sc->ipmi_io_type = type;
135 	sc->ipmi_io_spacing = info.offset;
136 
137 	if (sc->ipmi_io_res[0] == NULL) {
138 		device_printf(dev, "couldn't configure pci io res\n");
139 		return (ENXIO);
140 	}
141 
142 	sc->ipmi_irq_rid = 0;
143 	sc->ipmi_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ,
144 	    &sc->ipmi_irq_rid, RF_SHAREABLE | RF_ACTIVE);
145 
146 	error = ENXIO;
147 	switch (info.iface_type) {
148 	case KCS_MODE:
149 		error = ipmi_kcs_attach(sc);
150 		break;
151 	case SMIC_MODE:
152 		error = ipmi_smic_attach(sc);
153 		break;
154 	case BT_MODE:
155 		error = ipmi_bt_attach(sc);
156 		break;
157 	}
158 	if (error)
159 		goto bad;
160 	error = ipmi_attach(dev);
161 	if (error)
162 		goto bad;
163 
164 	return (0);
165 bad:
166 	ipmi_release_resources(dev);
167 	return (error);
168 }
169 
170 static device_method_t ipmi_methods[] = {
171 	/* Device interface */
172 	DEVMETHOD(device_probe,     ipmi_pci_probe),
173 	DEVMETHOD(device_attach,    ipmi_pci_attach),
174 	DEVMETHOD(device_detach,    ipmi_detach),
175 	{ 0, 0 }
176 };
177 
178 static driver_t ipmi_pci_driver = {
179 	"ipmi",
180 	ipmi_methods,
181 	sizeof(struct ipmi_softc)
182 };
183 
184 DRIVER_MODULE(ipmi_pci, pci, ipmi_pci_driver, 0, 0);
185 
186 /* Native IPMI on PCI driver. */
187 
188 static int
189 ipmi2_pci_probe(device_t dev)
190 {
191 
192 	if (pci_get_class(dev) == PCIC_SERIALBUS &&
193 	    pci_get_subclass(dev) == PCIS_SERIALBUS_IPMI) {
194 		device_set_desc(dev, "IPMI System Interface");
195 		return (BUS_PROBE_GENERIC);
196 	}
197 
198 	return (ENXIO);
199 }
200 
201 static int
202 ipmi2_pci_attach(device_t dev)
203 {
204 	struct ipmi_softc *sc;
205 	int error, iface, type;
206 
207 	sc = device_get_softc(dev);
208 	sc->ipmi_dev = dev;
209 
210 	/* Interface is determined by progif. */
211 	switch (pci_get_progif(dev)) {
212 	case PCIP_SERIALBUS_IPMI_SMIC:
213 		iface = SMIC_MODE;
214 		break;
215 	case PCIP_SERIALBUS_IPMI_KCS:
216 		iface = KCS_MODE;
217 		break;
218 	case PCIP_SERIALBUS_IPMI_BT:
219 		iface = BT_MODE;
220 		break;
221 	default:
222 		device_printf(dev, "Unsupported interface: %d\n",
223 		    pci_get_progif(dev));
224 		return (ENXIO);
225 	}
226 
227 	/* Check the BAR to determine our resource type. */
228 	sc->ipmi_io_rid = PCIR_BAR(0);
229 	if (PCI_BAR_IO(pci_read_config(dev, PCIR_BAR(0), 4)))
230 		type = SYS_RES_IOPORT;
231 	else
232 		type = SYS_RES_MEMORY;
233 	sc->ipmi_io_type = type;
234 	sc->ipmi_io_spacing = 1;
235 	sc->ipmi_io_res[0] = bus_alloc_resource_any(dev, type,
236 	    &sc->ipmi_io_rid, RF_ACTIVE);
237 	if (sc->ipmi_io_res[0] == NULL) {
238 		device_printf(dev, "couldn't map ports/memory\n");
239 		return (ENXIO);
240 	}
241 
242 	sc->ipmi_irq_rid = 0;
243 	sc->ipmi_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ,
244 	    &sc->ipmi_irq_rid, RF_SHAREABLE | RF_ACTIVE);
245 
246 	error = ENXIO;
247 	switch (iface) {
248 	case KCS_MODE:
249 		device_printf(dev, "using KSC interface\n");
250 
251 		/*
252 		 * We have to examine the resource directly to determine the
253 		 * alignment.
254 		 */
255 		if (!ipmi_kcs_probe_align(sc)) {
256 			device_printf(dev, "Unable to determine alignment\n");
257 			goto bad;
258 		}
259 
260 		error = ipmi_kcs_attach(sc);
261 		if (error)
262 			goto bad;
263 		break;
264 	case SMIC_MODE:
265 		device_printf(dev, "using SMIC interface\n");
266 		error = ipmi_smic_attach(sc);
267 		break;
268 	case BT_MODE:
269 		device_printf(dev, "using BT interface\n");
270 		error = ipmi_bt_attach(sc);
271 		break;
272 	}
273 	if (error)
274 		goto bad;
275 	error = ipmi_attach(dev);
276 	if (error)
277 		goto bad;
278 
279 	return (0);
280 bad:
281 	ipmi_release_resources(dev);
282 	return (error);
283 }
284 
285 static device_method_t ipmi2_methods[] = {
286 	/* Device interface */
287 	DEVMETHOD(device_probe,     ipmi2_pci_probe),
288 	DEVMETHOD(device_attach,    ipmi2_pci_attach),
289 	DEVMETHOD(device_detach,    ipmi_detach),
290 	{ 0, 0 }
291 };
292 
293 static driver_t ipmi2_pci_driver = {
294 	"ipmi",
295 	ipmi2_methods,
296 	sizeof(struct ipmi_softc)
297 };
298 
299 DRIVER_MODULE(ipmi2_pci, pci, ipmi2_pci_driver, 0, 0);
300 #ifdef ARCH_MAY_USE_EFI
301 MODULE_DEPEND(ipmi2_pci, efirt, 1, 1, 1);
302 #endif
303