153a374c1SSascha Wildner /*-
253a374c1SSascha Wildner * Copyright (c) 2006 IronPort Systems Inc. <ambrisko@ironport.com>
353a374c1SSascha Wildner * All rights reserved.
453a374c1SSascha Wildner *
553a374c1SSascha Wildner * Redistribution and use in source and binary forms, with or without
653a374c1SSascha Wildner * modification, are permitted provided that the following conditions
753a374c1SSascha Wildner * are met:
853a374c1SSascha Wildner * 1. Redistributions of source code must retain the above copyright
953a374c1SSascha Wildner * notice, this list of conditions and the following disclaimer.
1053a374c1SSascha Wildner * 2. Redistributions in binary form must reproduce the above copyright
1153a374c1SSascha Wildner * notice, this list of conditions and the following disclaimer in the
1253a374c1SSascha Wildner * documentation and/or other materials provided with the distribution.
1353a374c1SSascha Wildner *
1453a374c1SSascha Wildner * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1553a374c1SSascha Wildner * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1653a374c1SSascha Wildner * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1753a374c1SSascha Wildner * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1853a374c1SSascha Wildner * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1953a374c1SSascha Wildner * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2053a374c1SSascha Wildner * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2153a374c1SSascha Wildner * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2253a374c1SSascha Wildner * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2353a374c1SSascha Wildner * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2453a374c1SSascha Wildner * SUCH DAMAGE.
2553a374c1SSascha Wildner *
2653a374c1SSascha Wildner * $FreeBSD: head/sys/dev/ipmi/ipmi_pci.c 168162 2007-03-31 21:39:02Z jhb $
2753a374c1SSascha Wildner */
2853a374c1SSascha Wildner
2953a374c1SSascha Wildner #include <sys/param.h>
3053a374c1SSascha Wildner #include <sys/systm.h>
3153a374c1SSascha Wildner #include <sys/bus.h>
3253a374c1SSascha Wildner #include <sys/condvar.h>
3353a374c1SSascha Wildner #include <sys/eventhandler.h>
3453a374c1SSascha Wildner #include <sys/kernel.h>
3553a374c1SSascha Wildner #include <sys/module.h>
3653a374c1SSascha Wildner #include <sys/rman.h>
3753a374c1SSascha Wildner #include <sys/conf.h>
3853a374c1SSascha Wildner
3953a374c1SSascha Wildner #include <bus/pci/pcireg.h>
4053a374c1SSascha Wildner #include <bus/pci/pcivar.h>
4153a374c1SSascha Wildner
4253a374c1SSascha Wildner #ifdef LOCAL_MODULE
4353a374c1SSascha Wildner #include <ipmivars.h>
4453a374c1SSascha Wildner #else
4553a374c1SSascha Wildner #include <dev/misc/ipmi/ipmivars.h>
4653a374c1SSascha Wildner #endif
4753a374c1SSascha Wildner
4853a374c1SSascha Wildner static int ipmi_pci_probe(device_t dev);
4953a374c1SSascha Wildner static int ipmi_pci_attach(device_t dev);
5053a374c1SSascha Wildner
5153a374c1SSascha Wildner static struct ipmi_ident
5253a374c1SSascha Wildner {
5353a374c1SSascha Wildner u_int16_t vendor;
5453a374c1SSascha Wildner u_int16_t device;
5553a374c1SSascha Wildner char *desc;
5653a374c1SSascha Wildner } ipmi_identifiers[] = {
5753a374c1SSascha Wildner {0x1028, 0x000d, "Dell PE2650 SMIC interface"},
5853a374c1SSascha Wildner {0, 0, 0}
5953a374c1SSascha Wildner };
6053a374c1SSascha Wildner
6153a374c1SSascha Wildner const char *
ipmi_pci_match(uint16_t vendor,uint16_t device)6253a374c1SSascha Wildner ipmi_pci_match(uint16_t vendor, uint16_t device)
6353a374c1SSascha Wildner {
6453a374c1SSascha Wildner struct ipmi_ident *m;
6553a374c1SSascha Wildner
6653a374c1SSascha Wildner for (m = ipmi_identifiers; m->vendor != 0; m++)
6753a374c1SSascha Wildner if (m->vendor == vendor && m->device == device)
6853a374c1SSascha Wildner return (m->desc);
6953a374c1SSascha Wildner return (NULL);
7053a374c1SSascha Wildner }
7153a374c1SSascha Wildner
7253a374c1SSascha Wildner static int
ipmi_pci_probe(device_t dev)7353a374c1SSascha Wildner ipmi_pci_probe(device_t dev)
7453a374c1SSascha Wildner {
7553a374c1SSascha Wildner const char *desc;
7653a374c1SSascha Wildner
7753a374c1SSascha Wildner if (ipmi_attached)
7853a374c1SSascha Wildner return (ENXIO);
7953a374c1SSascha Wildner
8053a374c1SSascha Wildner desc = ipmi_pci_match(pci_get_vendor(dev), pci_get_device(dev));
8153a374c1SSascha Wildner if (desc != NULL) {
8253a374c1SSascha Wildner device_set_desc(dev, desc);
8353a374c1SSascha Wildner return (BUS_PROBE_DEFAULT);
8453a374c1SSascha Wildner }
8553a374c1SSascha Wildner
8653a374c1SSascha Wildner return (ENXIO);
8753a374c1SSascha Wildner }
8853a374c1SSascha Wildner
8953a374c1SSascha Wildner static int
ipmi_pci_attach(device_t dev)9053a374c1SSascha Wildner ipmi_pci_attach(device_t dev)
9153a374c1SSascha Wildner {
9253a374c1SSascha Wildner struct ipmi_softc *sc = device_get_softc(dev);
9353a374c1SSascha Wildner struct ipmi_get_info info;
9453a374c1SSascha Wildner const char *mode;
9553a374c1SSascha Wildner int error, type;
9653a374c1SSascha Wildner
9753a374c1SSascha Wildner /* Look for an IPMI entry in the SMBIOS table. */
9853a374c1SSascha Wildner if (!ipmi_smbios_identify(&info))
9953a374c1SSascha Wildner return (ENXIO);
10053a374c1SSascha Wildner
10153a374c1SSascha Wildner sc->ipmi_dev = dev;
10253a374c1SSascha Wildner
10353a374c1SSascha Wildner switch (info.iface_type) {
10453a374c1SSascha Wildner case KCS_MODE:
10553a374c1SSascha Wildner mode = "KCS";
10653a374c1SSascha Wildner break;
10753a374c1SSascha Wildner case SMIC_MODE:
10853a374c1SSascha Wildner mode = "SMIC";
10953a374c1SSascha Wildner break;
11053a374c1SSascha Wildner case BT_MODE:
11153a374c1SSascha Wildner device_printf(dev, "BT mode is unsupported\n");
11253a374c1SSascha Wildner return (ENXIO);
11353a374c1SSascha Wildner default:
11453a374c1SSascha Wildner device_printf(dev, "No IPMI interface found\n");
11553a374c1SSascha Wildner return (ENXIO);
11653a374c1SSascha Wildner }
11753a374c1SSascha Wildner
11853a374c1SSascha Wildner device_printf(dev, "%s mode found at %s 0x%jx alignment 0x%x on %s\n",
11953a374c1SSascha Wildner mode, info.io_mode ? "io" : "mem",
12053a374c1SSascha Wildner (uintmax_t)info.address, info.offset,
12153a374c1SSascha Wildner device_get_name(device_get_parent(dev)));
12253a374c1SSascha Wildner if (info.io_mode)
12353a374c1SSascha Wildner type = SYS_RES_IOPORT;
12453a374c1SSascha Wildner else
12553a374c1SSascha Wildner type = SYS_RES_MEMORY;
12653a374c1SSascha Wildner
12753a374c1SSascha Wildner sc->ipmi_io_rid = PCIR_BAR(0);
12853a374c1SSascha Wildner sc->ipmi_io_res[0] = bus_alloc_resource_any(dev, type,
12953a374c1SSascha Wildner &sc->ipmi_io_rid, RF_ACTIVE);
13053a374c1SSascha Wildner sc->ipmi_io_type = type;
13153a374c1SSascha Wildner sc->ipmi_io_spacing = info.offset;
13253a374c1SSascha Wildner
13353a374c1SSascha Wildner if (sc->ipmi_io_res[0] == NULL) {
13453a374c1SSascha Wildner device_printf(dev, "couldn't configure pci io res\n");
13553a374c1SSascha Wildner return (ENXIO);
13653a374c1SSascha Wildner }
13753a374c1SSascha Wildner
13853a374c1SSascha Wildner sc->ipmi_irq_rid = 0;
13953a374c1SSascha Wildner sc->ipmi_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ,
14053a374c1SSascha Wildner &sc->ipmi_irq_rid, RF_SHAREABLE | RF_ACTIVE);
14153a374c1SSascha Wildner
14253a374c1SSascha Wildner switch (info.iface_type) {
14353a374c1SSascha Wildner case KCS_MODE:
14453a374c1SSascha Wildner error = ipmi_kcs_attach(sc);
14553a374c1SSascha Wildner if (error)
14653a374c1SSascha Wildner goto bad;
14753a374c1SSascha Wildner break;
14853a374c1SSascha Wildner case SMIC_MODE:
14953a374c1SSascha Wildner error = ipmi_smic_attach(sc);
15053a374c1SSascha Wildner if (error)
15153a374c1SSascha Wildner goto bad;
15253a374c1SSascha Wildner break;
15353a374c1SSascha Wildner }
15453a374c1SSascha Wildner error = ipmi_attach(dev);
15553a374c1SSascha Wildner if (error)
15653a374c1SSascha Wildner goto bad;
15753a374c1SSascha Wildner
15853a374c1SSascha Wildner return (0);
15953a374c1SSascha Wildner bad:
16053a374c1SSascha Wildner ipmi_release_resources(dev);
16153a374c1SSascha Wildner return (error);
16253a374c1SSascha Wildner }
16353a374c1SSascha Wildner
16453a374c1SSascha Wildner static device_method_t ipmi_methods[] = {
16553a374c1SSascha Wildner /* Device interface */
16653a374c1SSascha Wildner DEVMETHOD(device_probe, ipmi_pci_probe),
16753a374c1SSascha Wildner DEVMETHOD(device_attach, ipmi_pci_attach),
16853a374c1SSascha Wildner DEVMETHOD(device_detach, ipmi_detach),
16953a374c1SSascha Wildner { 0, 0 }
17053a374c1SSascha Wildner };
17153a374c1SSascha Wildner
17253a374c1SSascha Wildner static driver_t ipmi_pci_driver = {
17353a374c1SSascha Wildner "ipmi",
17453a374c1SSascha Wildner ipmi_methods,
17553a374c1SSascha Wildner sizeof(struct ipmi_softc)
17653a374c1SSascha Wildner };
17753a374c1SSascha Wildner
178*aa6ac96eSSascha Wildner DRIVER_MODULE(ipmi_pci, pci, ipmi_pci_driver, ipmi_devclass, NULL, NULL);
17953a374c1SSascha Wildner
18053a374c1SSascha Wildner /* Native IPMI on PCI driver. */
18153a374c1SSascha Wildner
18253a374c1SSascha Wildner static int
ipmi2_pci_probe(device_t dev)18353a374c1SSascha Wildner ipmi2_pci_probe(device_t dev)
18453a374c1SSascha Wildner {
18553a374c1SSascha Wildner
18653a374c1SSascha Wildner if (pci_get_class(dev) == PCIC_SERIALBUS &&
18753a374c1SSascha Wildner pci_get_subclass(dev) == PCIS_SERIALBUS_IPMI) {
18853a374c1SSascha Wildner device_set_desc(dev, "IPMI System Interface");
18953a374c1SSascha Wildner return (BUS_PROBE_GENERIC);
19053a374c1SSascha Wildner }
19153a374c1SSascha Wildner
19253a374c1SSascha Wildner return (ENXIO);
19353a374c1SSascha Wildner }
19453a374c1SSascha Wildner
19553a374c1SSascha Wildner static int
ipmi2_pci_attach(device_t dev)19653a374c1SSascha Wildner ipmi2_pci_attach(device_t dev)
19753a374c1SSascha Wildner {
19853a374c1SSascha Wildner struct ipmi_softc *sc;
19953a374c1SSascha Wildner int error, iface, type;
20053a374c1SSascha Wildner
20153a374c1SSascha Wildner sc = device_get_softc(dev);
20253a374c1SSascha Wildner sc->ipmi_dev = dev;
20353a374c1SSascha Wildner
20453a374c1SSascha Wildner /* Interface is determined by progif. */
20553a374c1SSascha Wildner switch (pci_get_progif(dev)) {
20653a374c1SSascha Wildner case PCIP_SERIALBUS_IPMI_SMIC:
20753a374c1SSascha Wildner iface = SMIC_MODE;
20853a374c1SSascha Wildner break;
20953a374c1SSascha Wildner case PCIP_SERIALBUS_IPMI_KCS:
21053a374c1SSascha Wildner iface = KCS_MODE;
21153a374c1SSascha Wildner break;
21253a374c1SSascha Wildner case PCIP_SERIALBUS_IPMI_BT:
21353a374c1SSascha Wildner iface = BT_MODE;
21453a374c1SSascha Wildner device_printf(dev, "BT interface unsupported\n");
21553a374c1SSascha Wildner return (ENXIO);
21653a374c1SSascha Wildner default:
21753a374c1SSascha Wildner device_printf(dev, "Unsupported interface: %d\n",
21853a374c1SSascha Wildner pci_get_progif(dev));
21953a374c1SSascha Wildner return (ENXIO);
22053a374c1SSascha Wildner }
22153a374c1SSascha Wildner
22253a374c1SSascha Wildner /* Check the BAR to determine our resource type. */
22353a374c1SSascha Wildner sc->ipmi_io_rid = PCIR_BAR(0);
22453a374c1SSascha Wildner if (PCI_BAR_IO(pci_read_config(dev, PCIR_BAR(0), 4)))
22553a374c1SSascha Wildner type = SYS_RES_IOPORT;
22653a374c1SSascha Wildner else
22753a374c1SSascha Wildner type = SYS_RES_MEMORY;
22853a374c1SSascha Wildner sc->ipmi_io_type = type;
22953a374c1SSascha Wildner sc->ipmi_io_spacing = 1;
23053a374c1SSascha Wildner sc->ipmi_io_res[0] = bus_alloc_resource_any(dev, type,
23153a374c1SSascha Wildner &sc->ipmi_io_rid, RF_ACTIVE);
23253a374c1SSascha Wildner if (sc->ipmi_io_res[0] == NULL) {
23353a374c1SSascha Wildner device_printf(dev, "couldn't map ports/memory\n");
23453a374c1SSascha Wildner return (ENXIO);
23553a374c1SSascha Wildner }
23653a374c1SSascha Wildner
23753a374c1SSascha Wildner sc->ipmi_irq_rid = 0;
23853a374c1SSascha Wildner sc->ipmi_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ,
23953a374c1SSascha Wildner &sc->ipmi_irq_rid, RF_SHAREABLE | RF_ACTIVE);
24053a374c1SSascha Wildner
24153a374c1SSascha Wildner switch (iface) {
24253a374c1SSascha Wildner case KCS_MODE:
24353a374c1SSascha Wildner device_printf(dev, "using KSC interface\n");
24453a374c1SSascha Wildner
24553a374c1SSascha Wildner /*
24653a374c1SSascha Wildner * We have to examine the resource directly to determine the
24753a374c1SSascha Wildner * alignment.
24853a374c1SSascha Wildner */
24953a374c1SSascha Wildner if (!ipmi_kcs_probe_align(sc)) {
25053a374c1SSascha Wildner device_printf(dev, "Unable to determine alignment\n");
25153a374c1SSascha Wildner error = ENXIO;
25253a374c1SSascha Wildner goto bad;
25353a374c1SSascha Wildner }
25453a374c1SSascha Wildner
25553a374c1SSascha Wildner error = ipmi_kcs_attach(sc);
25653a374c1SSascha Wildner if (error)
25753a374c1SSascha Wildner goto bad;
25853a374c1SSascha Wildner break;
25953a374c1SSascha Wildner case SMIC_MODE:
26053a374c1SSascha Wildner device_printf(dev, "using SMIC interface\n");
26153a374c1SSascha Wildner
26253a374c1SSascha Wildner error = ipmi_smic_attach(sc);
26353a374c1SSascha Wildner if (error)
26453a374c1SSascha Wildner goto bad;
26553a374c1SSascha Wildner break;
26653a374c1SSascha Wildner }
26753a374c1SSascha Wildner error = ipmi_attach(dev);
26853a374c1SSascha Wildner if (error)
26953a374c1SSascha Wildner goto bad;
27053a374c1SSascha Wildner
27153a374c1SSascha Wildner return (0);
27253a374c1SSascha Wildner bad:
27353a374c1SSascha Wildner ipmi_release_resources(dev);
27453a374c1SSascha Wildner return (error);
27553a374c1SSascha Wildner }
27653a374c1SSascha Wildner
27753a374c1SSascha Wildner static device_method_t ipmi2_methods[] = {
27853a374c1SSascha Wildner /* Device interface */
27953a374c1SSascha Wildner DEVMETHOD(device_probe, ipmi2_pci_probe),
28053a374c1SSascha Wildner DEVMETHOD(device_attach, ipmi2_pci_attach),
28153a374c1SSascha Wildner DEVMETHOD(device_detach, ipmi_detach),
28253a374c1SSascha Wildner { 0, 0 }
28353a374c1SSascha Wildner };
28453a374c1SSascha Wildner
28553a374c1SSascha Wildner static driver_t ipmi2_pci_driver = {
28653a374c1SSascha Wildner "ipmi",
28753a374c1SSascha Wildner ipmi2_methods,
28853a374c1SSascha Wildner sizeof(struct ipmi_softc)
28953a374c1SSascha Wildner };
29053a374c1SSascha Wildner
291*aa6ac96eSSascha Wildner DRIVER_MODULE(ipmi2_pci, pci, ipmi2_pci_driver, ipmi_devclass, NULL, NULL);
292