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