kernel: Import the IPMI driver from FreeBSD.
[dragonfly.git] / sys / dev / misc / ipmi / ipmi_smbus.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_smbus.c 162562 2006-09-22 22:11:29Z jhb $
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/smbus/smbconf.h>
40 #include <bus/smbus/smbus.h>
41 #include <dev/smbus/smb/smb.h>
42
43 #include "smbus_if.h"
44
45 #ifdef LOCAL_MODULE
46 #include <ipmivars.h>
47 #else
48 #include <dev/misc/ipmi/ipmivars.h>
49 #endif
50
51 static void ipmi_smbus_identify(driver_t *driver, device_t parent);
52 static int ipmi_smbus_probe(device_t dev);
53 static int ipmi_smbus_attach(device_t dev);
54
55 static void
56 ipmi_smbus_identify(driver_t *driver, device_t parent)
57 {
58         struct ipmi_get_info info;
59
60         if (ipmi_smbios_identify(&info) && info.iface_type == SSIF_MODE &&
61             device_find_child(parent, "ipmi", -1) == NULL)
62                 BUS_ADD_CHILD(parent, parent, 0, "ipmi", -1);
63 }
64
65 static int
66 ipmi_smbus_probe(device_t dev)
67 {
68
69         device_set_desc(dev, "IPMI System Interface");
70         return (BUS_PROBE_DEFAULT);
71 }
72
73 static int
74 ipmi_smbus_attach(device_t dev)
75 {
76         struct ipmi_softc *sc = device_get_softc(dev);
77         struct ipmi_get_info info;
78         int error;
79
80         /* This should never fail. */
81         if (!ipmi_smbios_identify(&info))
82                 return (ENXIO);
83
84         if (info.iface_type != SSIF_MODE) {
85                 device_printf(dev, "No SSIF IPMI interface found\n");
86                 return (ENXIO);
87         }
88
89         sc->ipmi_dev = dev;
90
91         if (info.irq != 0) {
92                 sc->ipmi_irq_rid = 0;
93                 sc->ipmi_irq_res = bus_alloc_resource(dev, SYS_RES_IRQ,
94                     &sc->ipmi_irq_rid, info.irq, info.irq, 1,
95                     RF_SHAREABLE | RF_ACTIVE);
96         }
97
98         device_printf(dev, "SSIF mode found at address 0x%llx on %s\n",
99             (long long)info.address, device_get_name(device_get_parent(dev)));
100         error = ipmi_ssif_attach(sc, device_get_parent(dev), info.address);
101         if (error)
102                 goto bad;
103
104         error = ipmi_attach(dev);
105         if (error)
106                 goto bad;
107
108         return (0);
109 bad:
110         ipmi_release_resources(dev);
111         return (error);
112 }
113
114 static device_method_t ipmi_methods[] = {
115         /* Device interface */
116         DEVMETHOD(device_identify,      ipmi_smbus_identify),
117         DEVMETHOD(device_probe,         ipmi_smbus_probe),
118         DEVMETHOD(device_attach,        ipmi_smbus_attach),
119         DEVMETHOD(device_detach,        ipmi_detach),
120         { 0, 0 }
121 };
122
123 static driver_t ipmi_smbus_driver = {
124         "ipmi",
125         ipmi_methods,
126         sizeof(struct ipmi_softc)
127 };
128
129 DRIVER_MODULE(ipmi_smbus, smbus, ipmi_smbus_driver, ipmi_devclass, 0, 0);
130 MODULE_DEPEND(ipmi_smbus, smbus, SMBUS_MINVER, SMBUS_PREFVER, SMBUS_MAXVER);