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