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