kernel: Import the IPMI driver from FreeBSD.
[dragonfly.git] / sys / dev / misc / ipmi / ipmi_isa.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_isa.c 253813 2013-07-30 18:54:24Z sbruno $
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/conf.h>
38
39 #include <bus/pci/pci_cfgreg.h>
40 #include <bus/pci/pcireg.h>
41
42 #include <bus/isa/isavar.h>
43
44 #ifdef LOCAL_MODULE
45 #include <ipmi.h>
46 #include <ipmivars.h>
47 #else
48 #include <sys/ipmi.h>
49 #include <dev/misc/ipmi/ipmivars.h>
50 #endif
51
52 static void
53 ipmi_isa_identify(driver_t *driver, device_t parent)
54 {
55         struct ipmi_get_info info;
56         uint32_t devid;
57
58         if (ipmi_smbios_identify(&info) && info.iface_type != SSIF_MODE &&
59             device_find_child(parent, "ipmi", -1) == NULL) {
60                 /*
61                  * XXX: Hack alert.  On some broken systems, the IPMI
62                  * interface is described via SMBIOS, but the actual
63                  * IO resource is in a PCI device BAR, so we have to let
64                  * the PCI device attach ipmi instead.  In that case don't
65                  * create an isa ipmi device.  For now we hardcode the list
66                  * of bus, device, function tuples.
67                  */
68                 devid = pci_cfgregread(0, 4, 2, PCIR_DEVVENDOR, 4);
69                 if (devid != 0xffffffff &&
70                     ipmi_pci_match(devid & 0xffff, devid >> 16) != NULL)
71                         return;
72                 BUS_ADD_CHILD(parent, parent, 0, "ipmi", -1);
73         }
74 }
75
76 static int
77 ipmi_isa_probe(device_t dev)
78 {
79
80         /*
81          * Give other drivers precedence.  Unfortunately, this doesn't
82          * work if we have an SMBIOS table that duplicates a PCI device
83          * that's later on the bus than the PCI-ISA bridge.
84          */
85         if (ipmi_attached)
86                 return (ENXIO);
87
88         /* Skip any PNP devices. */
89         if (isa_get_logicalid(dev) != 0)
90                 return (ENXIO);
91
92         device_set_desc(dev, "IPMI System Interface");
93         return (BUS_PROBE_DEFAULT);
94 }
95
96 static int
97 ipmi_hint_identify(device_t dev, struct ipmi_get_info *info)
98 {
99         const char *mode, *name;
100         int i, unit, val;
101
102         /* We require at least a "mode" hint. */
103         name = device_get_name(dev);
104         unit = device_get_unit(dev);
105         if (resource_string_value(name, unit, "mode", &mode) != 0)
106                 return (0);
107
108         /* Set the mode and default I/O resources for each mode. */
109         bzero(info, sizeof(struct ipmi_get_info));
110         if (strcasecmp(mode, "KCS") == 0) {
111                 info->iface_type = KCS_MODE;
112                 info->address = 0xca2;
113                 info->io_mode = 1;
114                 info->offset = 1;
115         } else if (strcasecmp(mode, "SMIC") == 0) {
116                 info->iface_type = SMIC_MODE;
117                 info->address = 0xca9;
118                 info->io_mode = 1;
119                 info->offset = 1;
120         } else if (strcasecmp(mode, "BT") == 0) {
121                 info->iface_type = BT_MODE;
122                 info->address = 0xe4;
123                 info->io_mode = 1;
124                 info->offset = 1;
125         } else {
126                 device_printf(dev, "Invalid mode %s\n", mode);
127                 return (0);
128         }
129
130         /*
131          * Kill any resources that isahint.c might have setup for us
132          * since it will conflict with how we do resources.
133          */
134         for (i = 0; i < 2; i++) {
135                 bus_delete_resource(dev, SYS_RES_MEMORY, i);
136                 bus_delete_resource(dev, SYS_RES_IOPORT, i);
137         }
138
139         /* Allow the I/O address to be overriden via hints. */
140         if (resource_int_value(name, unit, "port", &val) == 0 && val != 0) {
141                 info->address = val;
142                 info->io_mode = 1;
143         } else if (resource_int_value(name, unit, "maddr", &val) == 0 &&
144             val != 0) {
145                 info->address = val;
146                 info->io_mode = 0;
147         }
148
149         /* Allow the spacing to be overriden. */
150         if (resource_int_value(name, unit, "spacing", &val) == 0) {
151                 switch (val) {
152                 case 8:
153                         info->offset = 1;
154                         break;
155                 case 16:
156                         info->offset = 2;
157                         break;
158                 case 32:
159                         info->offset = 4;
160                         break;
161                 default:
162                         device_printf(dev, "Invalid register spacing\n");
163                         return (0);
164                 }
165         }
166         return (1);
167 }
168
169 static int
170 ipmi_isa_attach(device_t dev)
171 {
172         struct ipmi_softc *sc = device_get_softc(dev);
173         struct ipmi_get_info info;
174         const char *mode;
175         int count, error, i, type;
176
177         /*
178          * Pull info out of the SMBIOS table.  If that doesn't work, use
179          * hints to enumerate a device.
180          */
181         if (!ipmi_smbios_identify(&info) &&
182             !ipmi_hint_identify(dev, &info))
183                 return (ENXIO);
184
185         switch (info.iface_type) {
186         case KCS_MODE:
187                 count = 2;
188                 mode = "KCS";
189                 break;
190         case SMIC_MODE:
191                 count = 3;
192                 mode = "SMIC";
193                 break;
194         case BT_MODE:
195                 device_printf(dev, "BT mode is unsupported\n");
196                 return (ENXIO);
197         default:
198                 return (ENXIO);
199         }
200         error = 0;
201         sc->ipmi_dev = dev;
202
203         device_printf(dev, "%s mode found at %s 0x%jx alignment 0x%x on %s\n",
204             mode, info.io_mode ? "io" : "mem",
205             (uintmax_t)info.address, info.offset,
206             device_get_name(device_get_parent(dev)));
207         if (info.io_mode)
208                 type = SYS_RES_IOPORT;
209         else
210                 type = SYS_RES_MEMORY;
211
212         sc->ipmi_io_type = type;
213         sc->ipmi_io_spacing = info.offset;
214         if (info.offset == 1) {
215                 sc->ipmi_io_rid = 0;
216                 sc->ipmi_io_res[0] = bus_alloc_resource(dev, type,
217                     &sc->ipmi_io_rid, info.address, info.address + count - 1,
218                     count, RF_ACTIVE);
219                 if (sc->ipmi_io_res[0] == NULL) {
220                         device_printf(dev, "couldn't configure I/O resource\n");
221                         return (ENXIO);
222                 }
223         } else {
224                 for (i = 0; i < count; i++) {
225                         sc->ipmi_io_rid = i;
226                         sc->ipmi_io_res[i] = bus_alloc_resource(dev, type,
227                             &sc->ipmi_io_rid, info.address + i * info.offset,
228                             info.address + i * info.offset, 1, RF_ACTIVE);
229                         if (sc->ipmi_io_res[i] == NULL) {
230                                 device_printf(dev,
231                                     "couldn't configure I/O resource\n");
232                                 error = ENXIO;
233                                 sc->ipmi_io_rid = 0;
234                                 goto bad;
235                         }
236                 }
237                 sc->ipmi_io_rid = 0;
238         }
239
240         if (info.irq != 0) {
241                 sc->ipmi_irq_rid = 0;
242                 sc->ipmi_irq_res = bus_alloc_resource(dev, SYS_RES_IRQ,
243                     &sc->ipmi_irq_rid, info.irq, info.irq, 1,
244                     RF_SHAREABLE | RF_ACTIVE);
245         }
246
247         switch (info.iface_type) {
248         case KCS_MODE:
249                 error = ipmi_kcs_attach(sc);
250                 if (error)
251                         goto bad;
252                 break;
253         case SMIC_MODE:
254                 error = ipmi_smic_attach(sc);
255                 if (error)
256                         goto bad;
257                 break;
258         }
259
260         error = ipmi_attach(dev);
261         if (error)
262                 goto bad;
263
264         return (0);
265 bad:
266         ipmi_release_resources(dev);
267         return (error);
268 }
269
270 static device_method_t ipmi_methods[] = {
271         /* Device interface */
272         DEVMETHOD(device_identify,      ipmi_isa_identify),
273         DEVMETHOD(device_probe,         ipmi_isa_probe),
274         DEVMETHOD(device_attach,        ipmi_isa_attach),
275         DEVMETHOD(device_detach,        ipmi_detach),
276         { 0, 0 }
277 };
278
279 static driver_t ipmi_isa_driver = {
280         "ipmi",
281         ipmi_methods,
282         sizeof(struct ipmi_softc),
283 };
284
285 DRIVER_MODULE(ipmi_isa, isa, ipmi_isa_driver, ipmi_devclass, 0, 0);