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