xref: /freebsd-13-stable/sys/dev/ipmi/ipmi_acpi.c (revision 3bc80996974a61a4223eae4c1ccd47b6ee32a48a)
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/kernel.h>
35 #include <sys/module.h>
36 #include <sys/rman.h>
37 #include <sys/selinfo.h>
38 
39 #include <contrib/dev/acpica/include/acpi.h>
40 
41 #include <dev/acpica/acpivar.h>
42 
43 /* Hooks for the ACPI CA debugging infrastructure */
44 #define _COMPONENT	ACPI_BUTTON
45 ACPI_MODULE_NAME("IPMI")
46 
47 #ifdef LOCAL_MODULE
48 #include <ipmi.h>
49 #include <ipmivars.h>
50 #else
51 #include <sys/ipmi.h>
52 #include <dev/ipmi/ipmivars.h>
53 #endif
54 
55 static int ipmi_acpi_probe(device_t);
56 static int ipmi_acpi_attach(device_t);
57 
58 int
ipmi_acpi_probe(device_t dev)59 ipmi_acpi_probe(device_t dev)
60 {
61 	static char *ipmi_ids[] = {"IPI0001", NULL};
62 	int rv;
63 
64 	if (ipmi_attached)
65 		return (EBUSY);
66 
67 	if (acpi_disabled("ipmi"))
68 		return (ENXIO);
69 	rv = ACPI_ID_PROBE(device_get_parent(dev), dev, ipmi_ids, NULL);
70 	if (rv <= 0)
71 		device_set_desc(dev, "IPMI System Interface");
72 
73 	return (rv);
74 }
75 
76 static int
ipmi_acpi_attach(device_t dev)77 ipmi_acpi_attach(device_t dev)
78 {
79 	ACPI_HANDLE devh;
80 	const char *mode;
81 	struct ipmi_get_info info;
82 	struct ipmi_softc *sc = device_get_softc(dev);
83 	int count, error, flags, i, type;
84 	int interface_type = 0, interface_version = 0;
85 
86 	error = 0;
87 	devh = acpi_get_handle(dev);
88 	if (ACPI_FAILURE(acpi_GetInteger(devh, "_IFT", &interface_type)))
89 		return (ENXIO);
90 
91 	if (ACPI_FAILURE(acpi_GetInteger(devh, "_SRV", &interface_version)))
92 		return (ENXIO);
93 
94 	switch (interface_type) {
95 	case KCS_MODE:
96 		count = IPMI_IF_KCS_NRES;
97 		mode = "KCS";
98 		break;
99 	case SMIC_MODE:
100 		count = IPMI_IF_SMIC_NRES;
101 		mode = "SMIC";
102 		break;
103 	case BT_MODE:
104 		count = IPMI_IF_BT_NRES;
105 		mode = "BT";
106 		break;
107 	case SSIF_MODE:
108 		if (ACPI_FAILURE(acpi_GetInteger(devh, "_ADR", &flags)))
109 			return (ENXIO);
110 		info.address = flags;
111 		device_printf(dev, "SSIF interface not supported on ACPI\n");
112 		return (0);
113 	default:
114 		return (ENXIO);
115 	}
116 
117 	if (bus_get_resource(dev, SYS_RES_IOPORT, 0, NULL, NULL) == 0)
118 		type = SYS_RES_IOPORT;
119 	else if (bus_get_resource(dev, SYS_RES_MEMORY, 0, NULL, NULL) == 0)
120 		type = SYS_RES_MEMORY;
121 	else {
122 		device_printf(dev, "unknown resource type\n");
123 		return (ENXIO);
124 	}
125 
126 	sc->ipmi_io_rid = 0;
127 	sc->ipmi_io_res[0] = bus_alloc_resource_any(dev, type,
128 	    &sc->ipmi_io_rid, RF_ACTIVE);
129 	sc->ipmi_io_type = type;
130 	sc->ipmi_io_spacing = 1;
131 	if (sc->ipmi_io_res[0] == NULL) {
132 		device_printf(dev, "couldn't configure I/O resource\n");
133 		return (ENXIO);
134 	}
135 
136 	/* If we have multiple resources, allocate up to MAX_RES. */
137 	for (i = 1; i < MAX_RES; i++) {
138 		sc->ipmi_io_rid = i;
139 		sc->ipmi_io_res[i] = bus_alloc_resource_any(dev, type,
140 		    &sc->ipmi_io_rid, RF_ACTIVE);
141 		if (sc->ipmi_io_res[i] == NULL)
142 			break;
143 	}
144 	sc->ipmi_io_rid = 0;
145 
146 	/* If we have multiple resources, make sure we have enough of them. */
147 	if (sc->ipmi_io_res[1] != NULL && sc->ipmi_io_res[count - 1] == NULL) {
148 		device_printf(dev, "too few I/O resources\n");
149 		error = ENXIO;
150 		goto bad;
151 	}
152 
153 	device_printf(dev, "%s mode found at %s 0x%jx on %s\n",
154 	    mode, type == SYS_RES_IOPORT ? "io" : "mem",
155 	    (uintmax_t)rman_get_start(sc->ipmi_io_res[0]),
156 	    device_get_name(device_get_parent(dev)));
157 
158 	sc->ipmi_dev = dev;
159 
160 	/*
161 	 * Setup an interrupt if we have an interrupt resource.  We
162 	 * don't support GPE interrupts via _GPE yet.
163 	 */
164 	sc->ipmi_irq_rid = 0;
165 	sc->ipmi_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ,
166 	    &sc->ipmi_irq_rid, RF_SHAREABLE | RF_ACTIVE);
167 
168 	/* Warn if _GPE exists. */
169 	if (ACPI_SUCCESS(AcpiEvaluateObject(devh, "_GPE", NULL, NULL)))
170 		device_printf(dev, "_GPE support not implemented\n");
171 
172 	/*
173 	 * We assume an alignment of 1 byte as currently the IPMI spec
174 	 * doesn't provide any way to determine the alignment via ACPI.
175 	 */
176 	error = ENXIO;
177 	switch (interface_type) {
178 	case KCS_MODE:
179 		error = ipmi_kcs_attach(sc);
180 		break;
181 	case SMIC_MODE:
182 		error = ipmi_smic_attach(sc);
183 		break;
184 	case BT_MODE:
185 		error = ipmi_bt_attach(sc);
186 		break;
187 	}
188 	if (error)
189 		goto bad;
190 	error = ipmi_attach(dev);
191 	if (error)
192 		goto bad;
193 
194 	return (0);
195 bad:
196 	ipmi_release_resources(dev);
197 	return (error);
198 }
199 
200 static device_method_t ipmi_methods[] = {
201 	/* Device interface */
202 	DEVMETHOD(device_probe,		ipmi_acpi_probe),
203 	DEVMETHOD(device_attach,	ipmi_acpi_attach),
204 	DEVMETHOD(device_detach,	ipmi_detach),
205 	{ 0, 0 }
206 };
207 
208 static driver_t ipmi_acpi_driver = {
209 	"ipmi",
210 	ipmi_methods,
211 	sizeof(struct ipmi_softc),
212 };
213 
214 DRIVER_MODULE(ipmi_acpi, acpi, ipmi_acpi_driver, ipmi_devclass, 0, 0);
215 MODULE_DEPEND(ipmi_acpi, acpi, 1, 1, 1);
216