kernel: Import the IPMI driver from FreeBSD.
[dragonfly.git] / sys / dev / misc / ipmi / ipmi_acpi.c
1 /*-
2  * Copyright (c) 2006 IronPort Systems Inc. <ambrisko@ironport.com>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD: head/sys/dev/ipmi/ipmi_acpi.c 193530 2009-06-05 18:44:36Z jkim $
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/kernel.h>
34 #include <sys/module.h>
35 #include <sys/rman.h>
36 #include <sys/conf.h>
37
38 #include <contrib/dev/acpica/source/include/acpi.h>
39
40 #include <dev/acpica/acpivar.h>
41
42 /* Hooks for the ACPI CA debugging infrastructure */
43 #define _COMPONENT      ACPI_BUTTON
44 ACPI_MODULE_NAME("IPMI")
45
46 #ifdef LOCAL_MODULE
47 #include <ipmi.h>
48 #include <ipmivars.h>
49 #else
50 #include <sys/ipmi.h>
51 #include <dev/misc/ipmi/ipmivars.h>
52 #endif
53
54 static int ipmi_acpi_probe(device_t);
55 static int ipmi_acpi_attach(device_t);
56
57 int
58 ipmi_acpi_probe(device_t dev)
59 {
60         static char *ipmi_ids[] = {"IPI0001", NULL};
61
62         if (ipmi_attached)
63                 return (EBUSY);
64
65         if (acpi_disabled("ipmi") ||
66             ACPI_ID_PROBE(device_get_parent(dev), dev, ipmi_ids) == NULL)
67                 return (ENXIO);
68
69         device_set_desc(dev, "IPMI System Interface");
70
71         return (0);
72 }
73
74 static int
75 ipmi_acpi_attach(device_t dev)
76 {
77         ACPI_HANDLE devh;
78         const char *mode;
79         struct ipmi_get_info info;
80         struct ipmi_softc *sc = device_get_softc(dev);
81         int count, error, flags, i, type;
82         int interface_type = 0, interface_version = 0;
83
84         error = 0;
85         devh = acpi_get_handle(dev);
86         if (ACPI_FAILURE(acpi_GetInteger(devh, "_IFT", &interface_type)))
87                 return (ENXIO);
88
89         if (ACPI_FAILURE(acpi_GetInteger(devh, "_SRV", &interface_version)))
90                 return (ENXIO);
91
92         switch (interface_type) {
93         case KCS_MODE:
94                 count = 2;
95                 mode = "KCS";
96                 break;
97         case SMIC_MODE:
98                 count = 3;
99                 mode = "SMIC";
100                 break;
101         case BT_MODE:
102                 device_printf(dev, "BT interface not supported\n");
103                 return (ENXIO);
104         case SSIF_MODE:
105                 if (ACPI_FAILURE(acpi_GetInteger(devh, "_ADR", &flags)))
106                         return (ENXIO);
107                 info.address = flags;
108                 device_printf(dev, "SSIF interface not supported on ACPI\n");
109                 return (0);
110         default:
111                 return (ENXIO);
112         }
113
114         if (bus_get_resource(dev, SYS_RES_IOPORT, 0, NULL, NULL) == 0)
115                 type = SYS_RES_IOPORT;
116         else if (bus_get_resource(dev, SYS_RES_MEMORY, 0, NULL, NULL) == 0)
117                 type = SYS_RES_MEMORY;
118         else {
119                 device_printf(dev, "unknown resource type\n");
120                 return (ENXIO);
121         }
122
123         sc->ipmi_io_rid = 0;
124         sc->ipmi_io_res[0] = bus_alloc_resource_any(dev, type,
125             &sc->ipmi_io_rid, RF_ACTIVE);
126         sc->ipmi_io_type = type;
127         sc->ipmi_io_spacing = 1;
128         if (sc->ipmi_io_res[0] == NULL) {
129                 device_printf(dev, "couldn't configure I/O resource\n");
130                 return (ENXIO);
131         }
132
133         /* If we have multiple resources, allocate up to MAX_RES. */
134         for (i = 1; i < MAX_RES; i++) {
135                 sc->ipmi_io_rid = i;
136                 sc->ipmi_io_res[i] = bus_alloc_resource_any(dev, type,
137                     &sc->ipmi_io_rid, RF_ACTIVE);
138                 if (sc->ipmi_io_res[i] == NULL)
139                         break;
140         }
141         sc->ipmi_io_rid = 0;
142
143         /* If we have multiple resources, make sure we have enough of them. */
144         if (sc->ipmi_io_res[1] != NULL && sc->ipmi_io_res[count - 1] == NULL) {
145                 device_printf(dev, "too few I/O resources\n");
146                 error = ENXIO;
147                 goto bad;
148         }
149
150         device_printf(dev, "%s mode found at %s 0x%jx on %s\n",
151             mode, type == SYS_RES_IOPORT ? "io" : "mem",
152             (uintmax_t)rman_get_start(sc->ipmi_io_res[0]),
153             device_get_name(device_get_parent(dev)));
154
155         sc->ipmi_dev = dev;
156
157         /*
158          * Setup an interrupt if we have an interrupt resource.  We
159          * don't support GPE interrupts via _GPE yet.
160          */
161         sc->ipmi_irq_rid = 0;
162         sc->ipmi_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ,
163             &sc->ipmi_irq_rid, RF_SHAREABLE | RF_ACTIVE);
164
165         /* Warn if _GPE exists. */
166         if (ACPI_SUCCESS(AcpiEvaluateObject(devh, "_GPE", NULL, NULL)))
167                 device_printf(dev, "_GPE support not implemented\n");
168
169         /*
170          * We assume an alignment of 1 byte as currently the IPMI spec
171          * doesn't provide any way to determine the alignment via ACPI.
172          */
173         switch (interface_type) {
174         case KCS_MODE:
175                 error = ipmi_kcs_attach(sc);
176                 if (error)
177                         goto bad;
178                 break;
179         case SMIC_MODE:
180                 error = ipmi_smic_attach(sc);
181                 if (error)
182                         goto bad;
183                 break;
184         }
185         error = ipmi_attach(dev);
186         if (error)
187                 goto bad;
188
189         return (0);
190 bad:
191         ipmi_release_resources(dev);
192         return (error);
193 }
194
195 static device_method_t ipmi_methods[] = {
196         /* Device interface */
197         DEVMETHOD(device_probe,         ipmi_acpi_probe),
198         DEVMETHOD(device_attach,        ipmi_acpi_attach),
199         DEVMETHOD(device_detach,        ipmi_detach),
200         { 0, 0 }
201 };
202
203 static driver_t ipmi_acpi_driver = {
204         "ipmi",
205         ipmi_methods,
206         sizeof(struct ipmi_softc),
207 };
208
209 DRIVER_MODULE(ipmi_acpi, acpi, ipmi_acpi_driver, ipmi_devclass, 0, 0);
210 MODULE_DEPEND(ipmi_acpi, acpi, 1, 1, 1);