bindings: AST LPC binding implementation
Add a binding implementation for the ASPEED 2500 LPC + KCS devices.
While not a standard, this does give us a hardware binding
implementation to use on current hardware.
Signed-off-by: Jeremy Kerr <jk@ozlabs.org>
diff --git a/Makefile.inc b/Makefile.inc
index 98df834..fd63032 100644
--- a/Makefile.inc
+++ b/Makefile.inc
@@ -1,6 +1,6 @@
LIBMCTP_DIR ?= libmctp/
LIBMCTP_OBJS = core.o alloc.o
-LIBMCTP_BINDINGS ?= serial
+LIBMCTP_BINDINGS ?= serial astlpc
LIBMCTP_OBJS += $(LIBMCTP_BINDINGS:%=%.o)
diff --git a/astlpc.c b/astlpc.c
new file mode 100644
index 0000000..ab97f79
--- /dev/null
+++ b/astlpc.c
@@ -0,0 +1,346 @@
+/* SPDX-License-Identifier: Apache-2.0 */
+
+#include <assert.h>
+#include <fcntl.h>
+#include <stdbool.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <sys/ioctl.h>
+#include <sys/mman.h>
+
+#include <linux/aspeed-lpc-ctrl.h>
+
+#define pr_fmt(x) "astlpc: " x
+
+#include "libmctp.h"
+#include "libmctp-alloc.h"
+#include "libmctp-log.h"
+#include "libmctp-astlpc.h"
+
+struct mctp_binding_astlpc {
+ struct mctp_binding binding;
+ void *lpc_map_base;
+ union {
+ void *lpc_map;
+ struct mctp_lpcmap_hdr *lpc_hdr;
+ };
+ int kcs_fd;
+ uint8_t kcs_status;
+
+ bool running;
+
+ /* temporary transmit buffer */
+ uint8_t txbuf[256];
+};
+
+#ifndef container_of
+#define container_of(ptr, type, member) \
+ (type *)((char *)(ptr) - (char *)&((type *)0)->member)
+#endif
+
+#define binding_to_astlpc(b) \
+ container_of(b, struct mctp_binding_astlpc, binding)
+
+#define MCTP_MAGIC 0x4d435450
+#define BMC_VER_MIN 1
+#define BMC_VER_CUR 1
+
+struct mctp_lpcmap_hdr {
+ uint32_t magic;
+
+ uint16_t bmc_ver_min;
+ uint16_t bmc_ver_cur;
+ uint16_t host_ver_min;
+ uint16_t host_ver_cur;
+ uint16_t negotiated_ver;
+ uint16_t pad0;
+
+ uint32_t rx_offset;
+ uint32_t rx_size;
+ uint32_t tx_offset;
+ uint32_t tx_size;
+} __attribute__((packed));
+
+/* layout of TX/RX areas */
+static const uint32_t rx_offset = 0x100;
+static const uint32_t rx_size = 0x100;
+static const uint32_t tx_offset = 0x200;
+static const uint32_t tx_size = 0x100;
+
+/* kernel interface */
+static const char *kcs_path = "/dev/mctp0";
+static const char *lpc_path = "/dev/aspeed-lpc-ctrl";
+
+#define LPC_WIN_SIZE (1 * 1024 * 1024)
+
+enum {
+ KCS_REG_DATA = 0,
+ KCS_REG_STATUS = 1,
+};
+
+#define KCS_STATUS_BMC_READY 0x80
+#define KCS_STATUS_CHANNEL_ACTIVE 0x40
+#define KCS_STATUS_IBF 0x02
+#define KCS_STATUS_OBF 0x01
+
+static int mctp_astlpc_kcs_set_status(struct mctp_binding_astlpc *astlpc,
+ uint8_t status)
+{
+ int rc;
+
+ rc = pwrite(astlpc->kcs_fd, &status, 1, KCS_REG_STATUS);
+ if (rc != 1) {
+ mctp_prwarn("KCS status write failed");
+ return -1;
+ }
+ return 0;
+}
+
+static int mctp_astlpc_kcs_send(struct mctp_binding_astlpc *astlpc,
+ uint8_t data)
+{
+ uint8_t status;
+ int rc;
+
+ for (;;) {
+ rc = pread(astlpc->kcs_fd, &status, 1, KCS_REG_STATUS);
+ if (rc != 1) {
+ mctp_prwarn("KCE status read failed");
+ return -1;
+ }
+ if (!(status & KCS_STATUS_OBF))
+ break;
+ /* todo: timeout */
+ }
+
+ rc = pwrite(astlpc->kcs_fd, &data, 1, KCS_REG_DATA);
+ if (rc != 1) {
+ mctp_prwarn("KCS data write failed");
+ return -1;
+ }
+
+ return 0;
+}
+
+static int mctp_binding_astlpc_tx(struct mctp_binding *b,
+ struct mctp_pktbuf *pkt)
+{
+ struct mctp_binding_astlpc *astlpc = binding_to_astlpc(b);
+ uint32_t len;
+
+ len = mctp_pktbuf_size(pkt);
+ if (len > rx_size - 4) {
+ mctp_prwarn("invalid TX len 0x%x", len);
+ return -1;
+ }
+
+ *(uint32_t *)(astlpc->lpc_map + rx_offset) = htobe32(len);
+
+ memcpy(astlpc->lpc_map + rx_offset + 4, mctp_pktbuf_hdr(pkt), len);
+
+ mctp_binding_set_tx_enabled(b, false);
+
+ mctp_astlpc_kcs_send(astlpc, 0x1);
+ return 0;
+}
+
+static void mctp_astlpc_init_channel(struct mctp_binding_astlpc *astlpc)
+{
+ /* todo: actual version negotiation */
+ astlpc->lpc_hdr->negotiated_ver = htobe16(1);
+ mctp_astlpc_kcs_set_status(astlpc,
+ KCS_STATUS_BMC_READY | KCS_STATUS_CHANNEL_ACTIVE |
+ KCS_STATUS_OBF);
+
+ mctp_binding_set_tx_enabled(&astlpc->binding, true);
+}
+
+static void mctp_astlpc_rx_start(struct mctp_binding_astlpc *astlpc)
+{
+ struct mctp_pktbuf *pkt;
+ uint32_t len;
+
+ len = htobe32(*(uint32_t *)(astlpc->lpc_map + tx_offset));
+ if (len > tx_size - 4) {
+ mctp_prwarn("invalid RX len 0x%x", len);
+ return;
+ }
+
+ if (len > MCTP_MTU + sizeof(struct mctp_hdr)) {
+ mctp_prwarn("invalid RX len 0x%x", len);
+ return;
+ }
+
+ pkt = mctp_pktbuf_alloc(len);
+ if (!pkt)
+ goto out_complete;
+
+ memcpy(mctp_pktbuf_hdr(pkt), astlpc->lpc_map + tx_offset + 4, len);
+
+ mctp_bus_rx(&astlpc->binding, pkt);
+
+out_complete:
+ mctp_astlpc_kcs_send(astlpc, 0x2);
+}
+
+static void mctp_astlpc_tx_complete(struct mctp_binding_astlpc *astlpc)
+{
+ mctp_binding_set_tx_enabled(&astlpc->binding, true);
+}
+
+int mctp_astlpc_poll(struct mctp_binding_astlpc *astlpc)
+{
+ uint8_t kcs_regs[2], data;
+ int rc;
+
+ rc = pread(astlpc->kcs_fd, kcs_regs, 2, 0);
+ if (rc < 0) {
+ mctp_prwarn("KCS read error");
+ return -1;
+ } else if (rc != 2) {
+ mctp_prwarn("KCS short read (%d)", rc);
+ return -1;
+ }
+
+ if (!(kcs_regs[KCS_REG_STATUS] & KCS_STATUS_IBF))
+ return 0;
+
+ data = kcs_regs[KCS_REG_DATA];
+ switch (data) {
+ case 0x0:
+ mctp_astlpc_init_channel(astlpc);
+ break;
+ case 0x1:
+ mctp_astlpc_rx_start(astlpc);
+ break;
+ case 0x2:
+ mctp_astlpc_tx_complete(astlpc);
+ break;
+ default:
+ mctp_prwarn("unknown message 0x%x", data);
+ }
+ return 0;
+}
+
+int mctp_astlpc_get_fd(struct mctp_binding_astlpc *astlpc)
+{
+ return astlpc->kcs_fd;
+}
+
+
+void mctp_astlpc_register_bus(struct mctp_binding_astlpc *astlpc,
+ struct mctp *mctp, mctp_eid_t eid)
+{
+ mctp_register_bus(mctp, &astlpc->binding, eid);
+}
+
+static int mctp_astlpc_init_bmc(struct mctp_binding_astlpc *astlpc)
+{
+ uint8_t status;
+ int rc;
+
+ astlpc->lpc_hdr->magic = htobe32(MCTP_MAGIC);
+ astlpc->lpc_hdr->bmc_ver_min = htobe16(BMC_VER_MIN);
+ astlpc->lpc_hdr->bmc_ver_cur = htobe16(BMC_VER_CUR);
+
+ astlpc->lpc_hdr->rx_offset = htobe32(rx_offset);
+ astlpc->lpc_hdr->rx_size = htobe32(rx_size);
+ astlpc->lpc_hdr->tx_offset = htobe32(tx_offset);
+ astlpc->lpc_hdr->tx_size = htobe32(tx_size);
+
+ /* set status indicating that the BMC is now active */
+ status = KCS_STATUS_BMC_READY | KCS_STATUS_OBF;
+ rc = pwrite(astlpc->kcs_fd, &status, 1, KCS_REG_STATUS);
+ if (rc != 1) {
+ mctp_prwarn("KCS write failed");
+ rc = -1;
+ } else {
+ rc = 0;
+ }
+
+ return rc;
+}
+
+static int mctp_astlpc_init_lpc(struct mctp_binding_astlpc *astlpc)
+{
+ struct aspeed_lpc_ctrl_mapping map = {
+ .window_type = ASPEED_LPC_CTRL_WINDOW_MEMORY,
+ .window_id = 0, /* There's only one */
+ .flags = 0,
+ .addr = 0,
+ .offset = 0,
+ .size = 0
+ };
+ int fd, rc;
+
+ fd = open(lpc_path, O_RDWR | O_SYNC);
+ if (fd < 0) {
+ mctp_prwarn("LPC open (%s) failed", lpc_path);
+ return -1;
+ }
+
+ rc = ioctl(fd, ASPEED_LPC_CTRL_IOCTL_GET_SIZE, &map);
+ if (rc) {
+ mctp_prwarn("LPC GET_SIZE failed");
+ close(fd);
+ return -1;
+ }
+
+ astlpc->lpc_map_base = mmap(NULL, map.size, PROT_READ | PROT_WRITE,
+ MAP_SHARED, fd, 0);
+ if (astlpc->lpc_map_base == MAP_FAILED) {
+ mctp_prwarn("LPC mmap failed");
+ rc = -1;
+ } else {
+ astlpc->lpc_map = astlpc->lpc_map_base +
+ map.size - LPC_WIN_SIZE;
+ }
+
+ close(fd);
+
+ return rc;
+}
+
+static int mctp_astlpc_init_kcs(struct mctp_binding_astlpc *astlpc)
+{
+ astlpc->kcs_fd = open(kcs_path, O_RDWR);
+ if (astlpc->kcs_fd < 0)
+ return -1;
+
+ return 0;
+}
+
+struct mctp_binding_astlpc *mctp_astlpc_init(void)
+{
+ struct mctp_binding_astlpc *astlpc;
+ int rc;
+
+ astlpc = __mctp_alloc(sizeof(*astlpc));
+ memset(astlpc, 0, sizeof(*astlpc));
+ astlpc->binding.name = "astlpc";
+ astlpc->binding.version = 1;
+ astlpc->binding.tx = mctp_binding_astlpc_tx;
+
+ rc = mctp_astlpc_init_lpc(astlpc);
+ if (rc) {
+ free(astlpc);
+ return NULL;
+ }
+
+ rc = mctp_astlpc_init_kcs(astlpc);
+ if (rc) {
+ free(astlpc);
+ return NULL;
+ }
+
+ rc = mctp_astlpc_init_bmc(astlpc);
+ if (rc) {
+ free(astlpc);
+ return NULL;
+ }
+
+ return astlpc;
+}
+
diff --git a/libmctp-astlpc.h b/libmctp-astlpc.h
new file mode 100644
index 0000000..ba60950
--- /dev/null
+++ b/libmctp-astlpc.h
@@ -0,0 +1,24 @@
+/* SPDX-License-Identifier: Apache-2.0 */
+
+#ifndef _LIBMCTP_ASTLPCL_H
+#define _LIBMCTP_ASTLPCL_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include "libmctp.h"
+
+struct mctp_binding_astlpc;
+
+struct mctp_binding_astlpc *mctp_astlpc_init(void);
+int mctp_astlpc_get_fd(struct mctp_binding_astlpc *astlpc);
+void mctp_astlpc_register_bus(struct mctp_binding_astlpc *astlpc,
+ struct mctp *mctp, mctp_eid_t eid);
+int mctp_astlpc_poll(struct mctp_binding_astlpc *astlpc);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* _LIBMCTP_ASTLPCL_H */
diff --git a/utils/mctp-astlpc-daemon.c b/utils/mctp-astlpc-daemon.c
new file mode 100644
index 0000000..c5c428e
--- /dev/null
+++ b/utils/mctp-astlpc-daemon.c
@@ -0,0 +1,112 @@
+
+#include <assert.h>
+#include <err.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <sys/poll.h>
+#include <sys/socket.h>
+
+#include "libmctp.h"
+#include "libmctp-astlpc.h"
+
+static const mctp_eid_t local_eid = 8;
+static const mctp_eid_t remote_eid = 9;
+
+static const uint8_t echo_req = 1;
+static const uint8_t echo_resp = 2;
+
+struct ctx {
+ struct mctp *mctp;
+};
+
+static void tx_message(struct ctx *ctx, mctp_eid_t eid, void *msg, size_t len)
+{
+ uint8_t type;
+
+ type = len > 0 ? *(uint8_t *)(msg) : 0x00;
+
+ fprintf(stderr, "TX: dest EID 0x%02x: %zd bytes, first byte [0x%02x]\n",
+ eid, len, type);
+ mctp_message_tx(ctx->mctp, eid, msg, len);
+}
+
+static void rx_message(uint8_t eid, void *data, void *msg, size_t len)
+{
+ struct ctx *ctx = data;
+ uint8_t type;
+
+ type = len > 0 ? *(uint8_t *)(msg) : 0x00;
+
+ fprintf(stderr, "RX: src EID 0x%02x: %zd bytes, first byte [0x%02x]\n",
+ eid, len, type);
+
+ if (type == echo_req) {
+ *(uint8_t *)(msg) = echo_resp;
+ tx_message(ctx, eid, msg, len);
+ }
+}
+
+int main(void)
+{
+ struct mctp_binding_astlpc *astlpc;
+ struct mctp *mctp;
+ struct ctx *ctx, _ctx;
+ int rc;
+
+ mctp = mctp_init();
+ assert(mctp);
+
+ astlpc = mctp_astlpc_init();
+ assert(astlpc);
+
+ mctp_astlpc_register_bus(astlpc, mctp, local_eid);
+
+ ctx = &_ctx;
+ ctx->mctp = mctp;
+
+ mctp_set_rx_all(mctp, rx_message, ctx);
+
+ for (;;) {
+#if 0
+
+ struct pollfd pollfds[2];
+
+ pollfds[0].fd = STDIN_FILENO;
+ pollfds[0].events = POLLIN;
+
+ pollfds[1].fd = mctp_astlpc_get_fd(astlpc);
+ pollfds[1].events = POLLIN;
+
+ rc = poll(pollfds, 2, -1);
+ if (rc < 0)
+ err(EXIT_FAILURE, "poll");
+
+ if (pollfds[0].revents) {
+ uint8_t buf[1024];
+ rc = read(STDIN_FILENO, buf, sizeof(buf));
+ if (rc == 0)
+ break;
+ if (rc < 0)
+ err(EXIT_FAILURE, "read");
+ tx_message(ctx, remote_eid, buf, rc);
+ }
+
+ if (pollfds[1].revents) {
+ rc = mctp_astlpc_poll(astlpc);
+ if (rc)
+ break;
+ }
+#else
+ (void)remote_eid;
+ rc = mctp_astlpc_poll(astlpc);
+ if (rc)
+ break;
+
+#endif
+
+ }
+
+ return EXIT_SUCCESS;
+
+}