diff options
author | Mark Kettenis <kettenis@cvs.openbsd.org> | 2019-08-19 18:31:04 +0000 |
---|---|---|
committer | Mark Kettenis <kettenis@cvs.openbsd.org> | 2019-08-19 18:31:04 +0000 |
commit | 4777074779ce0b723b0f6748dfb10af92c5e7c59 (patch) | |
tree | f0912c8544f7788d08fa7dbb592f2108570a719e /sys | |
parent | 39d5aa59196b1732703dab758d2d606b05705111 (diff) |
Add support for SMBus System Interface (SSIF).
ok jmatthew@
Diffstat (limited to 'sys')
-rw-r--r-- | sys/dev/i2c/files.i2c | 6 | ||||
-rw-r--r-- | sys/dev/i2c/ipmi_i2c.c | 225 | ||||
-rw-r--r-- | sys/dev/ipmi.c | 24 | ||||
-rw-r--r-- | sys/dev/ipmivar.h | 10 |
4 files changed, 254 insertions, 11 deletions
diff --git a/sys/dev/i2c/files.i2c b/sys/dev/i2c/files.i2c index fc4c134ecbe..1018a660db9 100644 --- a/sys/dev/i2c/files.i2c +++ b/sys/dev/i2c/files.i2c @@ -1,4 +1,4 @@ -# $OpenBSD: files.i2c,v 1.62 2019/01/02 21:15:47 patrick Exp $ +# $OpenBSD: files.i2c,v 1.63 2019/08/19 18:31:02 kettenis Exp $ # $NetBSD: files.i2c,v 1.3 2003/10/20 16:24:10 briggs Exp $ define i2c {[addr = -1], [size = -1]} @@ -229,3 +229,7 @@ file dev/i2c/isl1208.c islrtc device abcrtc attach abcrtc at i2c file dev/i2c/abx80x.c abcrtc + +# IPMI SMBus System Interface (SSIF) +attach ipmi at i2c with ipmi_i2c +file dev/i2c/ipmi_i2c.c ipmi_i2c diff --git a/sys/dev/i2c/ipmi_i2c.c b/sys/dev/i2c/ipmi_i2c.c new file mode 100644 index 00000000000..20ec0d3e0a2 --- /dev/null +++ b/sys/dev/i2c/ipmi_i2c.c @@ -0,0 +1,225 @@ +/* $OpenBSD: ipmi_i2c.c,v 1.1 2019/08/19 18:31:03 kettenis Exp $ */ +/* + * Copyright (c) 2019 Mark Kettenis <kettenis@openbsd.org> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include <sys/param.h> +#include <sys/systm.h> +#include <sys/device.h> + +#include <machine/bus.h> + +#include <dev/i2c/i2cvar.h> +#include <dev/ipmivar.h> + +#define BMC_SA 0x20 /* BMC/ESM3 */ +#define BMC_LUN 0 + +struct ipmi_i2c_softc { + struct ipmi_softc sc; + i2c_tag_t sc_tag; + i2c_addr_t sc_addr; + uint8_t sc_rev; +}; + +void cmn_buildmsg(struct ipmi_cmd *); +int ssif_sendmsg(struct ipmi_cmd *); +int ssif_recvmsg(struct ipmi_cmd *); +int ssif_reset(struct ipmi_softc *); +int ssif_probe(struct ipmi_softc *); + +struct ipmi_if ssif_if = { + "SSIF", + 0, + cmn_buildmsg, + ssif_sendmsg, + ssif_recvmsg, + ssif_reset, + ssif_probe, + IPMI_MSG_DATASND, + IPMI_MSG_DATARCV +}; + +extern void ipmi_attach(struct device *, struct device *, void *); + +int ipmi_i2c_match(struct device *, void *, void *); +void ipmi_i2c_attach(struct device *, struct device *, void *); + +struct cfattach ipmi_i2c_ca = { + sizeof(struct ipmi_i2c_softc), ipmi_i2c_match, ipmi_i2c_attach +}; + +int ipmi_i2c_get_interface_caps(struct ipmi_i2c_softc *); +int ipmi_i2c_get_device_id(struct ipmi_i2c_softc *); + +int +ipmi_i2c_match(struct device *parent, void *match, void *aux) +{ + struct i2c_attach_args *ia = aux; + + return (strcmp(ia->ia_name, "IPI0001") == 0 || + strcmp(ia->ia_name, "APMC0D8A") == 0); +} + +void +ipmi_i2c_attach(struct device *parent, struct device *self, void *aux) +{ + struct ipmi_i2c_softc *sc = (struct ipmi_i2c_softc *)self; + struct i2c_attach_args *ia = aux; + struct ipmi_attach_args iaa; + + sc->sc_tag = ia->ia_tag; + sc->sc_addr = ia->ia_addr; + sc->sc.sc_if = &ssif_if; + + if (ipmi_i2c_get_interface_caps(sc)) { + printf(": can't get system interface capabilities\n"); + return; + } + + if (ipmi_i2c_get_device_id(sc)) { + printf(": can't get system interface capabilities\n"); + return; + } + + memset(&iaa, 0, sizeof(iaa)); + iaa.iaa_if_type = IPMI_IF_SSIF; + iaa.iaa_if_rev = (sc->sc_rev >> 4 | sc->sc_rev << 4); + iaa.iaa_if_irq = -1; + ipmi_attach_common(&sc->sc, &iaa); +} + +int +ipmi_i2c_get_interface_caps(struct ipmi_i2c_softc *sc) +{ + struct ipmi_cmd c; + uint8_t data[5]; + + data[0] = 0; /* SSIF */ + + c.c_sc = &sc->sc; + c.c_rssa = BMC_SA; + c.c_rslun = BMC_LUN; + c.c_netfn = APP_NETFN; + c.c_cmd = APP_GET_SYSTEM_INTERFACE_CAPS; + c.c_txlen = 1; + c.c_rxlen = 0; + c.c_maxrxlen = sizeof(data); + c.c_data = data; + if (ipmi_sendcmd(&c) || ipmi_recvcmd(&c)) + return EIO; + + /* Check SSIF version number. */ + if ((data[1] & 0x7) != 0) + return EINVAL; + /* Check input and output message sizes. */ + if (data[2] < 32 || data[3] < 32) + return EINVAL; + + return 0; +} + +int +ipmi_i2c_get_device_id(struct ipmi_i2c_softc *sc) +{ + struct ipmi_cmd c; + uint8_t data[16]; + + c.c_sc = &sc->sc; + c.c_rssa = BMC_SA; + c.c_rslun = BMC_LUN; + c.c_netfn = APP_NETFN; + c.c_cmd = APP_GET_DEVICE_ID; + c.c_txlen = 0; + c.c_rxlen = 0; + c.c_maxrxlen = sizeof(data); + c.c_data = data; + if (ipmi_sendcmd(&c) || ipmi_recvcmd(&c)) + return EIO; + + sc->sc_rev = data[4]; + return 0; +} + +int +ssif_sendmsg(struct ipmi_cmd *c) +{ + struct ipmi_i2c_softc *sc = (struct ipmi_i2c_softc *)c->c_sc; + uint8_t *buf = sc->sc.sc_buf; + uint8_t cmd[2]; + int error, retry; + + /* We only support single-part writes. */ + if (c->c_txlen > 32) + return -1; + + iic_acquire_bus(sc->sc_tag, 0); + + while (retry--) { + cmd[0] = 2; + cmd[1] = c->c_txlen; + error = iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, + sc->sc_addr, cmd, sizeof(cmd), buf, c->c_txlen, 0); + if (!error) + break; + } + + iic_release_bus(sc->sc_tag, 0); + + return (error ? -1 : 0); +} + +int +ssif_recvmsg(struct ipmi_cmd *c) +{ + struct ipmi_i2c_softc *sc = (struct ipmi_i2c_softc *)c->c_sc; + uint8_t buf[IPMI_MAX_RX + 16]; + uint8_t cmd[1]; + int error, retry; + + /* We only support single-part reads. */ + if (c->c_maxrxlen > 32) + return -1; + + iic_acquire_bus(sc->sc_tag, 0); + + cmd[0] = 3; + for (retry = 0; retry < 250; retry++) { + memset(buf, 0, c->c_maxrxlen + 1); + error = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, + sc->sc_addr, cmd, sizeof(cmd), buf, c->c_maxrxlen + 1, 0); + if (!error) { + c->c_rxlen = buf[0]; + memcpy(sc->sc.sc_buf, &buf[1], c->c_maxrxlen); + break; + } + } + + iic_release_bus(sc->sc_tag, 0); + + return (error ? -1 : 0); +} + +int +ssif_reset(struct ipmi_softc *sc) +{ + return -1; +} + +int +ssif_probe(struct ipmi_softc *sc) +{ + return 0; +} diff --git a/sys/dev/ipmi.c b/sys/dev/ipmi.c index 70205c79322..8d2be074cbf 100644 --- a/sys/dev/ipmi.c +++ b/sys/dev/ipmi.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ipmi.c,v 1.104 2019/08/13 18:31:23 kettenis Exp $ */ +/* $OpenBSD: ipmi.c,v 1.105 2019/08/19 18:31:02 kettenis Exp $ */ /* * Copyright (c) 2015 Masao Uebayashi @@ -70,8 +70,6 @@ int ipmi_enabled = 0; #define IPMI_MSG_NFLN 0 #define IPMI_MSG_CMD 1 #define IPMI_MSG_CCODE 2 -#define IPMI_MSG_DATASND 2 -#define IPMI_MSG_DATARCV 3 #define IPMI_SENSOR_TYPE_TEMP 0x0101 #define IPMI_SENSOR_TYPE_VOLT 0x0102 @@ -1435,6 +1433,9 @@ ipmi_refresh_sensors(struct ipmi_softc *sc) int ipmi_map_regs(struct ipmi_softc *sc, struct ipmi_attach_args *ia) { + if (sc->sc_if && sc->sc_if->nregs == 0) + return (0); + sc->sc_if = ipmi_get_if(ia->iaa_if_type); if (sc->sc_if == NULL) return (-1); @@ -1461,8 +1462,10 @@ ipmi_map_regs(struct ipmi_softc *sc, struct ipmi_attach_args *ia) void ipmi_unmap_regs(struct ipmi_softc *sc) { - bus_space_unmap(sc->sc_iot, sc->sc_ioh, - sc->sc_if->nregs * sc->sc_if_iospacing); + if (sc->sc_if->nregs > 0) { + bus_space_unmap(sc->sc_iot, sc->sc_ioh, + sc->sc_if->nregs * sc->sc_if_iospacing); + } } void @@ -1534,10 +1537,13 @@ ipmi_attach_common(struct ipmi_softc *sc, struct ipmi_attach_args *ia) /* Setup threads */ kthread_create_deferred(ipmi_create_thread, sc); - printf(": version %d.%d interface %s %sbase 0x%x/%x spacing %d", - ia->iaa_if_rev >> 4, ia->iaa_if_rev & 0xF, sc->sc_if->name, - ia->iaa_if_iotype == 'i' ? "io" : "mem", ia->iaa_if_iobase, - ia->iaa_if_iospacing * sc->sc_if->nregs, ia->iaa_if_iospacing); + printf(": version %d.%d interface %s", + ia->iaa_if_rev >> 4, ia->iaa_if_rev & 0xF, sc->sc_if->name); + if (sc->sc_if->nregs > 0) + printf(" %sbase 0x%x/%x spacing %d", + ia->iaa_if_iotype == 'i' ? "io" : "mem", ia->iaa_if_iobase, + ia->iaa_if_iospacing * sc->sc_if->nregs, + ia->iaa_if_iospacing); if (ia->iaa_if_irq != -1) printf(" irq %d", ia->iaa_if_irq); printf("\n"); diff --git a/sys/dev/ipmivar.h b/sys/dev/ipmivar.h index 468280b3881..d2eb268ef35 100644 --- a/sys/dev/ipmivar.h +++ b/sys/dev/ipmivar.h @@ -1,4 +1,4 @@ -/* $OpenBSD: ipmivar.h,v 1.30 2019/08/13 18:31:23 kettenis Exp $ */ +/* $OpenBSD: ipmivar.h,v 1.31 2019/08/19 18:31:02 kettenis Exp $ */ /* * Copyright (c) 2005 Jordan Hargrave @@ -39,6 +39,7 @@ #define IPMI_IF_KCS 1 #define IPMI_IF_SMIC 2 #define IPMI_IF_BT 3 +#define IPMI_IF_SSIF 4 #define IPMI_IF_KCS_NREGS 2 #define IPMI_IF_SMIC_NREGS 3 @@ -169,11 +170,18 @@ int ipmi_probe(void *); void ipmi_attach_common(struct ipmi_softc *, struct ipmi_attach_args *); int ipmi_activate(struct device *, int); +int ipmi_sendcmd(struct ipmi_cmd *); +int ipmi_recvcmd(struct ipmi_cmd *); + +#define IPMI_MSG_DATASND 2 +#define IPMI_MSG_DATARCV 3 + #define APP_NETFN 0x06 #define APP_GET_DEVICE_ID 0x01 #define APP_RESET_WATCHDOG 0x22 #define APP_SET_WATCHDOG_TIMER 0x24 #define APP_GET_WATCHDOG_TIMER 0x25 +#define APP_GET_SYSTEM_INTERFACE_CAPS 0x57 #define TRANSPORT_NETFN 0xC #define BRIDGE_NETFN 0x2 |