ipmi_pci.c revision 168157
1132720Skan/*-
2132720Skan * Copyright (c) 2006 IronPort Systems Inc. <ambrisko@ironport.com>
3169691Skan * All rights reserved.
4132720Skan *
5132720Skan * Redistribution and use in source and binary forms, with or without
6132720Skan * modification, are permitted provided that the following conditions
7132720Skan * are met:
8132720Skan * 1. Redistributions of source code must retain the above copyright
9132720Skan *    notice, this list of conditions and the following disclaimer.
10132720Skan * 2. Redistributions in binary form must reproduce the above copyright
11132720Skan *    notice, this list of conditions and the following disclaimer in the
12132720Skan *    documentation and/or other materials provided with the distribution.
13132720Skan *
14132720Skan * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15132720Skan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16132720Skan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17132720Skan * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18169691Skan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19132720Skan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20132720Skan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21132720Skan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22132720Skan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23132720Skan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24132720Skan * SUCH DAMAGE.
25132720Skan */
26132720Skan
27132720Skan#include <sys/cdefs.h>
28132720Skan__FBSDID("$FreeBSD: head/sys/dev/ipmi/ipmi_pci.c 168157 2007-03-31 20:41:00Z jhb $");
29132720Skan
30169691Skan#include <sys/param.h>
31169691Skan#include <sys/systm.h>
32169691Skan#include <sys/bus.h>
33169691Skan#include <sys/condvar.h>
34169691Skan#include <sys/eventhandler.h>
35132720Skan#include <sys/kernel.h>
36132720Skan#include <sys/module.h>
37132720Skan#include <sys/rman.h>
38132720Skan#include <sys/selinfo.h>
39132720Skan
40169691Skan#include <dev/pci/pcireg.h>
41132720Skan#include <dev/pci/pcivar.h>
42132720Skan
43#ifdef LOCAL_MODULE
44#include <ipmivars.h>
45#else
46#include <dev/ipmi/ipmivars.h>
47#endif
48
49static int ipmi_pci_probe(device_t dev);
50static int ipmi_pci_attach(device_t dev);
51
52static struct ipmi_ident
53{
54	u_int16_t	vendor;
55	u_int16_t	device;
56	char		*desc;
57} ipmi_identifiers[] = {
58	{0x1028, 0x000d, "Dell PE2650 SMIC interface"},
59	{0, 0, 0}
60};
61
62const char *
63ipmi_pci_match(uint16_t vendor, uint16_t device)
64{
65	struct ipmi_ident *m;
66
67	for (m = ipmi_identifiers; m->vendor != 0; m++)
68		if (m->vendor == vendor && m->device == device)
69			return (m->desc);
70	return (NULL);
71}
72
73static int
74ipmi_pci_probe(device_t dev)
75{
76	const char *desc;
77
78	if (ipmi_attached)
79		return (ENXIO);
80
81	desc = ipmi_pci_match(pci_get_vendor(dev), pci_get_device(dev));
82	if (desc != NULL) {
83		device_set_desc(dev, desc);
84		return (BUS_PROBE_DEFAULT);
85	}
86
87	return (ENXIO);
88}
89
90static int
91ipmi_pci_attach(device_t dev)
92{
93	struct ipmi_softc *sc = device_get_softc(dev);
94	struct ipmi_get_info info;
95	const char *mode;
96	int error, type;
97
98	/* Look for an IPMI entry in the SMBIOS table. */
99	if (!ipmi_smbios_identify(&info))
100		return (ENXIO);
101
102	sc->ipmi_dev = dev;
103
104	switch (info.iface_type) {
105	case KCS_MODE:
106		mode = "KCS";
107		break;
108	case SMIC_MODE:
109		mode = "SMIC";
110		break;
111	case BT_MODE:
112		device_printf(dev, "BT mode is unsupported\n");
113		return (ENXIO);
114	default:
115		device_printf(dev, "No IPMI interface found\n");
116		return (ENXIO);
117	}
118
119	device_printf(dev, "%s mode found at %s 0x%jx alignment 0x%x on %s\n",
120	    mode, info.io_mode ? "io" : "mem",
121	    (uintmax_t)info.address, info.offset,
122	    device_get_name(device_get_parent(dev)));
123	if (info.io_mode)
124		type = SYS_RES_IOPORT;
125	else
126		type = SYS_RES_MEMORY;
127
128	sc->ipmi_io_rid = PCIR_BAR(0);
129	sc->ipmi_io_res[0] = bus_alloc_resource_any(dev, type,
130	    &sc->ipmi_io_rid, RF_ACTIVE);
131	sc->ipmi_io_type = type;
132	sc->ipmi_io_spacing = info.offset;
133
134	if (sc->ipmi_io_res[0] == NULL) {
135		device_printf(dev, "couldn't configure pci io res\n");
136		return (ENXIO);
137	}
138
139	sc->ipmi_irq_rid = 0;
140	sc->ipmi_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ,
141	    &sc->ipmi_irq_rid, RF_SHAREABLE | RF_ACTIVE);
142
143	switch (info.iface_type) {
144	case KCS_MODE:
145		error = ipmi_kcs_attach(sc);
146		if (error)
147			goto bad;
148		break;
149	case SMIC_MODE:
150		error = ipmi_smic_attach(sc);
151		if (error)
152			goto bad;
153		break;
154	}
155	error = ipmi_attach(dev);
156	if (error)
157		goto bad;
158
159	return (0);
160bad:
161	ipmi_release_resources(dev);
162	return (error);
163}
164
165static device_method_t ipmi_methods[] = {
166	/* Device interface */
167	DEVMETHOD(device_probe,     ipmi_pci_probe),
168	DEVMETHOD(device_attach,    ipmi_pci_attach),
169	DEVMETHOD(device_detach,    ipmi_detach),
170	{ 0, 0 }
171};
172
173static driver_t ipmi_pci_driver = {
174	"ipmi",
175	ipmi_methods,
176	sizeof(struct ipmi_softc)
177};
178
179DRIVER_MODULE(ipmi_pci, pci, ipmi_pci_driver, ipmi_devclass, 0, 0);
180
181/* Native IPMI on PCI driver. */
182
183static int
184ipmi2_pci_probe(device_t dev)
185{
186
187	if (pci_get_class(dev) == PCIC_SERIALBUS &&
188	    pci_get_subclass(dev) == PCIS_SERIALBUS_IPMI) {
189		device_set_desc(dev, "IPMI System Interface");
190		return (BUS_PROBE_GENERIC);
191	}
192
193	return (ENXIO);
194}
195
196static int
197ipmi2_pci_attach(device_t dev)
198{
199	struct ipmi_softc *sc;
200	int error, iface, type;
201
202	sc = device_get_softc(dev);
203	sc->ipmi_dev = dev;
204
205	/* Interface is determined by progif. */
206	switch (pci_get_progif(dev)) {
207	case PCIP_SERIALBUS_IPMI_SMIC:
208		iface = SMIC_MODE;
209		break;
210	case PCIP_SERIALBUS_IPMI_KCS:
211		iface = KCS_MODE;
212		break;
213	case PCIP_SERIALBUS_IPMI_BT:
214		iface = BT_MODE;
215		device_printf(dev, "BT interface unsupported\n");
216		return (ENXIO);
217	default:
218		device_printf(dev, "Unsupported interface: %d\n",
219		    pci_get_progif(dev));
220		return (ENXIO);
221	}
222
223	/*
224	 * Bottom bit of bar indicates resouce type.  There should be
225	 * constants in pcireg.h for fields in a BAR.
226	 */
227	sc->ipmi_io_rid = PCIR_BAR(0);
228	if (pci_read_config(dev, PCIR_BAR(0), 4) & 0x1)
229		type = SYS_RES_IOPORT;
230	else
231		type = SYS_RES_MEMORY;
232	sc->ipmi_io_type = type;
233	sc->ipmi_io_spacing = 1;
234	sc->ipmi_io_res[0] = bus_alloc_resource_any(dev, type,
235	    &sc->ipmi_io_rid, RF_ACTIVE);
236	if (sc->ipmi_io_res[0] == NULL) {
237		device_printf(dev, "couldn't map ports/memory\n");
238		return (ENXIO);
239	}
240
241	sc->ipmi_irq_rid = 0;
242	sc->ipmi_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ,
243	    &sc->ipmi_irq_rid, RF_SHAREABLE | RF_ACTIVE);
244
245	switch (iface) {
246	case KCS_MODE:
247		device_printf(dev, "using KSC interface\n");
248
249		/*
250		 * We have to examine the resource directly to determine the
251		 * alignment.
252		 */
253		if (!ipmi_kcs_probe_align(sc)) {
254			device_printf(dev, "Unable to determine alignment\n");
255			error = ENXIO;
256			goto bad;
257		}
258
259		error = ipmi_kcs_attach(sc);
260		if (error)
261			goto bad;
262		break;
263	case SMIC_MODE:
264		device_printf(dev, "using SMIC interface\n");
265
266		error = ipmi_smic_attach(sc);
267		if (error)
268			goto bad;
269		break;
270	}
271	error = ipmi_attach(dev);
272	if (error)
273		goto bad;
274
275	return (0);
276bad:
277	ipmi_release_resources(dev);
278	return (error);
279}
280
281static device_method_t ipmi2_methods[] = {
282	/* Device interface */
283	DEVMETHOD(device_probe,     ipmi2_pci_probe),
284	DEVMETHOD(device_attach,    ipmi2_pci_attach),
285	DEVMETHOD(device_detach,    ipmi_detach),
286	{ 0, 0 }
287};
288
289static driver_t ipmi2_pci_driver = {
290	"ipmi",
291	ipmi2_methods,
292	sizeof(struct ipmi_softc)
293};
294
295DRIVER_MODULE(ipmi2_pci, pci, ipmi2_pci_driver, ipmi_devclass, 0, 0);
296