ipmi(4) at iic(4)

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

ipmi(4) at iic(4)

Mark Kettenis
The Ampere/Lenovo arm64 machines have a BMC that implements the IPMI
2.0.  This particular implementation uses SMBus to communicate with
the host.  This is called SMBus System Interface (SSIF).

ok?


Index: dev/ipmi.c
===================================================================
RCS file: /cvs/src/sys/dev/ipmi.c,v
retrieving revision 1.104
diff -u -p -r1.104 ipmi.c
--- dev/ipmi.c 13 Aug 2019 18:31:23 -0000 1.104
+++ dev/ipmi.c 17 Aug 2019 19:31:29 -0000
@@ -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 *
 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, str
 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
  /* 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");
Index: dev/ipmivar.h
===================================================================
RCS file: /cvs/src/sys/dev/ipmivar.h,v
retrieving revision 1.30
diff -u -p -r1.30 ipmivar.h
--- dev/ipmivar.h 13 Aug 2019 18:31:23 -0000 1.30
+++ dev/ipmivar.h 17 Aug 2019 19:31:29 -0000
@@ -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
Index: dev/i2c/files.i2c
===================================================================
RCS file: /cvs/src/sys/dev/i2c/files.i2c,v
retrieving revision 1.62
diff -u -p -r1.62 files.i2c
--- dev/i2c/files.i2c 2 Jan 2019 21:15:47 -0000 1.62
+++ dev/i2c/files.i2c 17 Aug 2019 19:31:29 -0000
@@ -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
Index: dev/i2c/ipmi_i2c.c
===================================================================
RCS file: dev/i2c/ipmi_i2c.c
diff -N dev/i2c/ipmi_i2c.c
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ dev/i2c/ipmi_i2c.c 17 Aug 2019 19:31:29 -0000
@@ -0,0 +1,225 @@
+/* $OpenBSD$ */
+/*
+ * Copyright (c) 2019 Mark Kettenis <[hidden email]>
+ *
+ * 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 siif_sendmsg(struct ipmi_cmd *);
+int siif_recvmsg(struct ipmi_cmd *);
+int siif_reset(struct ipmi_softc *);
+int siif_probe(struct ipmi_softc *);
+
+struct ipmi_if ssif_if = {
+ "SSIF",
+ 0,
+ cmn_buildmsg,
+ siif_sendmsg,
+ siif_recvmsg,
+ siif_reset,
+ siif_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 SIIF 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
+siif_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
+siif_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
+siif_reset(struct ipmi_softc *sc)
+{
+ return -1;
+}
+
+int
+siif_probe(struct ipmi_softc *sc)
+{
+ return 0;
+}