13d36ee2eSJeremy Kerr /* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */ 2672c8852SJeremy Kerr 33286f176SAndrew Jeffery #if HAVE_CONFIG_H 43286f176SAndrew Jeffery #include "config.h" 53286f176SAndrew Jeffery #endif 63286f176SAndrew Jeffery 73286f176SAndrew Jeffery #if HAVE_ENDIAN_H 892a10a6bSJeremy Kerr #include <endian.h> 93286f176SAndrew Jeffery #endif 103286f176SAndrew Jeffery 113286f176SAndrew Jeffery #include <assert.h> 1259c6a5c9SAndrew Jeffery #include <err.h> 137cd72f14SAndrew Jeffery #include <errno.h> 14edfe383fSAndrew Jeffery #include <inttypes.h> 15672c8852SJeremy Kerr #include <stdbool.h> 16672c8852SJeremy Kerr #include <stdlib.h> 17672c8852SJeremy Kerr #include <string.h> 18672c8852SJeremy Kerr 19672c8852SJeremy Kerr #define pr_fmt(x) "astlpc: " x 20672c8852SJeremy Kerr 21eba19a3bSAndrew Jeffery #include "container_of.h" 22eba19a3bSAndrew Jeffery #include "crc32.h" 23672c8852SJeremy Kerr #include "libmctp.h" 24672c8852SJeremy Kerr #include "libmctp-alloc.h" 25672c8852SJeremy Kerr #include "libmctp-log.h" 26672c8852SJeremy Kerr #include "libmctp-astlpc.h" 274622cadfSAndrew Jeffery #include "range.h" 28672c8852SJeremy Kerr 29b214c643SJeremy Kerr #ifdef MCTP_HAVE_FILEIO 3092a10a6bSJeremy Kerr 31c6f676d1SJeremy Kerr #include <unistd.h> 3292a10a6bSJeremy Kerr #include <fcntl.h> 331111c6a5SAndrew Jeffery #include <poll.h> 3492a10a6bSJeremy Kerr #include <sys/ioctl.h> 3592a10a6bSJeremy Kerr #include <sys/mman.h> 3692a10a6bSJeremy Kerr #include <linux/aspeed-lpc-ctrl.h> 3792a10a6bSJeremy Kerr 3892a10a6bSJeremy Kerr /* kernel interface */ 3992a10a6bSJeremy Kerr static const char *kcs_path = "/dev/mctp0"; 4092a10a6bSJeremy Kerr static const char *lpc_path = "/dev/aspeed-lpc-ctrl"; 4192a10a6bSJeremy Kerr 4292a10a6bSJeremy Kerr #endif 4392a10a6bSJeremy Kerr 44fe763e98SAndrew Jeffery enum mctp_astlpc_buffer_state { 45fe763e98SAndrew Jeffery /* 46fe763e98SAndrew Jeffery * Prior to "Channel Ready" we mark the buffers as "idle" to catch illegal accesses. In this 47fe763e98SAndrew Jeffery * state neither side is considered the owner of the buffer. 48fe763e98SAndrew Jeffery * 49fe763e98SAndrew Jeffery * Upon "Channel Ready", each side transitions the buffers from the initial "idle" state 50fe763e98SAndrew Jeffery * to the following target states: 51fe763e98SAndrew Jeffery * 52fe763e98SAndrew Jeffery * Tx buffer: "acquired" 53fe763e98SAndrew Jeffery * Rx buffer: "released" 54fe763e98SAndrew Jeffery */ 55fe763e98SAndrew Jeffery buffer_state_idle, 56fe763e98SAndrew Jeffery 57fe763e98SAndrew Jeffery /* 58fe763e98SAndrew Jeffery * Beyond initialisation by "Channel Ready", buffers are in the "acquired" state once: 59fe763e98SAndrew Jeffery * 60fe763e98SAndrew Jeffery * 1. We dequeue a control command transferring the buffer to our ownership out of the KCS 61fe763e98SAndrew Jeffery * interface, and 62fe763e98SAndrew Jeffery * 2. We are yet to complete all of our required accesses to the buffer 63fe763e98SAndrew Jeffery * 64fe763e98SAndrew Jeffery * * The Tx buffer enters the "acquired" state when we dequeue the "Rx Complete" command 65fe763e98SAndrew Jeffery * * The Rx buffer enters the "acquired" state when we dequeue the "Tx Begin" command 66fe763e98SAndrew Jeffery * 67fe763e98SAndrew Jeffery * It is a failure of implementation if it's possible for both sides to simultaneously 68fe763e98SAndrew Jeffery * consider a buffer as "acquired". 69fe763e98SAndrew Jeffery */ 70fe763e98SAndrew Jeffery buffer_state_acquired, 71fe763e98SAndrew Jeffery 72fe763e98SAndrew Jeffery /* 73fe763e98SAndrew Jeffery * Buffers are in the "prepared" state when: 74fe763e98SAndrew Jeffery * 75fe763e98SAndrew Jeffery * 1. We have completed all of our required accesses (read or write) for the buffer, and 76fe763e98SAndrew Jeffery * 2. We have not yet successfully enqueued the control command to hand off ownership 77fe763e98SAndrew Jeffery */ 78fe763e98SAndrew Jeffery buffer_state_prepared, 79fe763e98SAndrew Jeffery 80fe763e98SAndrew Jeffery /* 81fe763e98SAndrew Jeffery * Beyond initialisation by "Channel Ready", buffers are in the "released" state once: 82fe763e98SAndrew Jeffery * 83fe763e98SAndrew Jeffery * 1. We successfully enqueue the control command transferring ownership to the remote 84fe763e98SAndrew Jeffery * side in to the KCS interface 85fe763e98SAndrew Jeffery * 86fe763e98SAndrew Jeffery * * The Tx buffer enters the "released" state when we enqueue the "Tx Begin" command 87fe763e98SAndrew Jeffery * * The Rx buffer enters the "released" state when we enqueue the "Rx Complete" command 88fe763e98SAndrew Jeffery * 89fe763e98SAndrew Jeffery * It may be the case that both sides simultaneously consider a buffer to be in the 90fe763e98SAndrew Jeffery * "released" state. However, if this is true, it must also be true that a buffer ownership 91fe763e98SAndrew Jeffery * transfer command has been enqueued in the KCS interface and is yet to be dequeued. 92fe763e98SAndrew Jeffery */ 93fe763e98SAndrew Jeffery buffer_state_released, 94fe763e98SAndrew Jeffery }; 95fe763e98SAndrew Jeffery 967cd72f14SAndrew Jeffery struct mctp_astlpc_buffer { 977cd72f14SAndrew Jeffery uint32_t offset; 987cd72f14SAndrew Jeffery uint32_t size; 99fe763e98SAndrew Jeffery enum mctp_astlpc_buffer_state state; 1007cd72f14SAndrew Jeffery }; 1017cd72f14SAndrew Jeffery 1027cd72f14SAndrew Jeffery struct mctp_astlpc_layout { 1037cd72f14SAndrew Jeffery struct mctp_astlpc_buffer rx; 1047cd72f14SAndrew Jeffery struct mctp_astlpc_buffer tx; 1057cd72f14SAndrew Jeffery }; 1067cd72f14SAndrew Jeffery 10788412be4SAndrew Jeffery struct mctp_astlpc_protocol { 10888412be4SAndrew Jeffery uint16_t version; 10988412be4SAndrew Jeffery uint32_t (*packet_size)(uint32_t body); 11088412be4SAndrew Jeffery uint32_t (*body_size)(uint32_t packet); 111eba19a3bSAndrew Jeffery void (*pktbuf_protect)(struct mctp_pktbuf *pkt); 112eba19a3bSAndrew Jeffery bool (*pktbuf_validate)(struct mctp_pktbuf *pkt); 11388412be4SAndrew Jeffery }; 11488412be4SAndrew Jeffery 115672c8852SJeremy Kerr struct mctp_binding_astlpc { 116672c8852SJeremy Kerr struct mctp_binding binding; 117bc53d35aSJeremy Kerr 118672c8852SJeremy Kerr void *lpc_map; 1197cd72f14SAndrew Jeffery struct mctp_astlpc_layout layout; 1207cd72f14SAndrew Jeffery 1217cd72f14SAndrew Jeffery uint8_t mode; 122a9368980SAndrew Jeffery uint32_t requested_mtu; 123bc53d35aSJeremy Kerr 12488412be4SAndrew Jeffery const struct mctp_astlpc_protocol *proto; 12588412be4SAndrew Jeffery 126bc53d35aSJeremy Kerr /* direct ops data */ 127bc53d35aSJeremy Kerr struct mctp_binding_astlpc_ops ops; 128bc53d35aSJeremy Kerr void *ops_data; 129bc53d35aSJeremy Kerr 130bc53d35aSJeremy Kerr /* fileio ops data */ 131672c8852SJeremy Kerr int kcs_fd; 132672c8852SJeremy Kerr uint8_t kcs_status; 133672c8852SJeremy Kerr }; 134672c8852SJeremy Kerr 135672c8852SJeremy Kerr #define binding_to_astlpc(b) \ 136672c8852SJeremy Kerr container_of(b, struct mctp_binding_astlpc, binding) 137672c8852SJeremy Kerr 1389101a2abSAndrew Jeffery #define astlpc_prlog(ctx, lvl, fmt, ...) \ 1399101a2abSAndrew Jeffery do { \ 1409101a2abSAndrew Jeffery bool __bmc = ((ctx)->mode == MCTP_BINDING_ASTLPC_MODE_BMC); \ 1419101a2abSAndrew Jeffery mctp_prlog(lvl, pr_fmt("%s: " fmt), __bmc ? "bmc" : "host", \ 1429101a2abSAndrew Jeffery ##__VA_ARGS__); \ 1439101a2abSAndrew Jeffery } while (0) 1449101a2abSAndrew Jeffery 1459101a2abSAndrew Jeffery #define astlpc_prerr(ctx, fmt, ...) \ 1469101a2abSAndrew Jeffery astlpc_prlog(ctx, MCTP_LOG_ERR, fmt, ##__VA_ARGS__) 1479101a2abSAndrew Jeffery #define astlpc_prwarn(ctx, fmt, ...) \ 1489101a2abSAndrew Jeffery astlpc_prlog(ctx, MCTP_LOG_WARNING, fmt, ##__VA_ARGS__) 1499101a2abSAndrew Jeffery #define astlpc_prinfo(ctx, fmt, ...) \ 1509101a2abSAndrew Jeffery astlpc_prlog(ctx, MCTP_LOG_INFO, fmt, ##__VA_ARGS__) 1519101a2abSAndrew Jeffery #define astlpc_prdebug(ctx, fmt, ...) \ 1529101a2abSAndrew Jeffery astlpc_prlog(ctx, MCTP_LOG_DEBUG, fmt, ##__VA_ARGS__) 1539101a2abSAndrew Jeffery 1547cd72f14SAndrew Jeffery /* clang-format off */ 1557cd72f14SAndrew Jeffery #define ASTLPC_MCTP_MAGIC 0x4d435450 1564e8264b7SAndrew Jeffery #define ASTLPC_VER_BAD 0 1577cd72f14SAndrew Jeffery #define ASTLPC_VER_MIN 1 1587cd72f14SAndrew Jeffery 1593a540664SAndrew Jeffery /* Support testing of new binding protocols */ 1603a540664SAndrew Jeffery #ifndef ASTLPC_VER_CUR 161eba19a3bSAndrew Jeffery #define ASTLPC_VER_CUR 3 1623a540664SAndrew Jeffery #endif 1637cd72f14SAndrew Jeffery /* clang-format on */ 164672c8852SJeremy Kerr 16588412be4SAndrew Jeffery #ifndef ARRAY_SIZE 16688412be4SAndrew Jeffery #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) 16788412be4SAndrew Jeffery #endif 16888412be4SAndrew Jeffery 16988412be4SAndrew Jeffery static uint32_t astlpc_packet_size_v1(uint32_t body) 17088412be4SAndrew Jeffery { 17188412be4SAndrew Jeffery assert((body + 4) > body); 17288412be4SAndrew Jeffery 17388412be4SAndrew Jeffery return body + 4; 17488412be4SAndrew Jeffery } 17588412be4SAndrew Jeffery 17688412be4SAndrew Jeffery static uint32_t astlpc_body_size_v1(uint32_t packet) 17788412be4SAndrew Jeffery { 17888412be4SAndrew Jeffery assert((packet - 4) < packet); 17988412be4SAndrew Jeffery 18088412be4SAndrew Jeffery return packet - 4; 18188412be4SAndrew Jeffery } 18288412be4SAndrew Jeffery 183eba19a3bSAndrew Jeffery void astlpc_pktbuf_protect_v1(struct mctp_pktbuf *pkt) 184eba19a3bSAndrew Jeffery { 185eba19a3bSAndrew Jeffery (void)pkt; 186eba19a3bSAndrew Jeffery } 187eba19a3bSAndrew Jeffery 188eba19a3bSAndrew Jeffery bool astlpc_pktbuf_validate_v1(struct mctp_pktbuf *pkt) 189eba19a3bSAndrew Jeffery { 190eba19a3bSAndrew Jeffery (void)pkt; 191eba19a3bSAndrew Jeffery return true; 192eba19a3bSAndrew Jeffery } 193eba19a3bSAndrew Jeffery 194eba19a3bSAndrew Jeffery static uint32_t astlpc_packet_size_v3(uint32_t body) 195eba19a3bSAndrew Jeffery { 196eba19a3bSAndrew Jeffery assert((body + 4 + 4) > body); 197eba19a3bSAndrew Jeffery 198eba19a3bSAndrew Jeffery return body + 4 + 4; 199eba19a3bSAndrew Jeffery } 200eba19a3bSAndrew Jeffery 201eba19a3bSAndrew Jeffery static uint32_t astlpc_body_size_v3(uint32_t packet) 202eba19a3bSAndrew Jeffery { 203eba19a3bSAndrew Jeffery assert((packet - 4 - 4) < packet); 204eba19a3bSAndrew Jeffery 205eba19a3bSAndrew Jeffery return packet - 4 - 4; 206eba19a3bSAndrew Jeffery } 207eba19a3bSAndrew Jeffery 208eba19a3bSAndrew Jeffery void astlpc_pktbuf_protect_v3(struct mctp_pktbuf *pkt) 209eba19a3bSAndrew Jeffery { 210eba19a3bSAndrew Jeffery uint32_t code; 211eba19a3bSAndrew Jeffery 212eba19a3bSAndrew Jeffery code = htobe32(crc32(mctp_pktbuf_hdr(pkt), mctp_pktbuf_size(pkt))); 213eba19a3bSAndrew Jeffery mctp_prdebug("%s: 0x%" PRIx32, __func__, code); 214eba19a3bSAndrew Jeffery mctp_pktbuf_push(pkt, &code, 4); 215eba19a3bSAndrew Jeffery } 216eba19a3bSAndrew Jeffery 217eba19a3bSAndrew Jeffery bool astlpc_pktbuf_validate_v3(struct mctp_pktbuf *pkt) 218eba19a3bSAndrew Jeffery { 219eba19a3bSAndrew Jeffery uint32_t code; 220eba19a3bSAndrew Jeffery void *check; 221eba19a3bSAndrew Jeffery 222eba19a3bSAndrew Jeffery code = be32toh(crc32(mctp_pktbuf_hdr(pkt), mctp_pktbuf_size(pkt) - 4)); 223eba19a3bSAndrew Jeffery mctp_prdebug("%s: 0x%" PRIx32, __func__, code); 224eba19a3bSAndrew Jeffery check = mctp_pktbuf_pop(pkt, 4); 225eba19a3bSAndrew Jeffery return check && !memcmp(&code, check, 4); 226eba19a3bSAndrew Jeffery } 227eba19a3bSAndrew Jeffery 22888412be4SAndrew Jeffery static const struct mctp_astlpc_protocol astlpc_protocol_version[] = { 22988412be4SAndrew Jeffery [0] = { 23088412be4SAndrew Jeffery .version = 0, 23188412be4SAndrew Jeffery .packet_size = NULL, 23288412be4SAndrew Jeffery .body_size = NULL, 233eba19a3bSAndrew Jeffery .pktbuf_protect = NULL, 234eba19a3bSAndrew Jeffery .pktbuf_validate = NULL, 23588412be4SAndrew Jeffery }, 23688412be4SAndrew Jeffery [1] = { 23788412be4SAndrew Jeffery .version = 1, 23888412be4SAndrew Jeffery .packet_size = astlpc_packet_size_v1, 23988412be4SAndrew Jeffery .body_size = astlpc_body_size_v1, 240eba19a3bSAndrew Jeffery .pktbuf_protect = astlpc_pktbuf_protect_v1, 241eba19a3bSAndrew Jeffery .pktbuf_validate = astlpc_pktbuf_validate_v1, 24288412be4SAndrew Jeffery }, 24388412be4SAndrew Jeffery [2] = { 24488412be4SAndrew Jeffery .version = 2, 24588412be4SAndrew Jeffery .packet_size = astlpc_packet_size_v1, 24688412be4SAndrew Jeffery .body_size = astlpc_body_size_v1, 247eba19a3bSAndrew Jeffery .pktbuf_protect = astlpc_pktbuf_protect_v1, 248eba19a3bSAndrew Jeffery .pktbuf_validate = astlpc_pktbuf_validate_v1, 249eba19a3bSAndrew Jeffery }, 250eba19a3bSAndrew Jeffery [3] = { 251eba19a3bSAndrew Jeffery .version = 3, 252eba19a3bSAndrew Jeffery .packet_size = astlpc_packet_size_v3, 253eba19a3bSAndrew Jeffery .body_size = astlpc_body_size_v3, 254eba19a3bSAndrew Jeffery .pktbuf_protect = astlpc_pktbuf_protect_v3, 255eba19a3bSAndrew Jeffery .pktbuf_validate = astlpc_pktbuf_validate_v3, 25688412be4SAndrew Jeffery }, 25788412be4SAndrew Jeffery }; 25888412be4SAndrew Jeffery 259672c8852SJeremy Kerr struct mctp_lpcmap_hdr { 260672c8852SJeremy Kerr uint32_t magic; 261672c8852SJeremy Kerr 262672c8852SJeremy Kerr uint16_t bmc_ver_min; 263672c8852SJeremy Kerr uint16_t bmc_ver_cur; 264672c8852SJeremy Kerr uint16_t host_ver_min; 265672c8852SJeremy Kerr uint16_t host_ver_cur; 266672c8852SJeremy Kerr uint16_t negotiated_ver; 267672c8852SJeremy Kerr uint16_t pad0; 268672c8852SJeremy Kerr 2693a540664SAndrew Jeffery struct { 270672c8852SJeremy Kerr uint32_t rx_offset; 271672c8852SJeremy Kerr uint32_t rx_size; 272672c8852SJeremy Kerr uint32_t tx_offset; 273672c8852SJeremy Kerr uint32_t tx_size; 2743a540664SAndrew Jeffery } layout; 275672c8852SJeremy Kerr } __attribute__((packed)); 276672c8852SJeremy Kerr 2773a540664SAndrew Jeffery static const uint32_t control_size = 0x100; 278672c8852SJeremy Kerr 279672c8852SJeremy Kerr #define LPC_WIN_SIZE (1 * 1024 * 1024) 280672c8852SJeremy Kerr 281672c8852SJeremy Kerr #define KCS_STATUS_BMC_READY 0x80 282672c8852SJeremy Kerr #define KCS_STATUS_CHANNEL_ACTIVE 0x40 283672c8852SJeremy Kerr #define KCS_STATUS_IBF 0x02 284672c8852SJeremy Kerr #define KCS_STATUS_OBF 0x01 285672c8852SJeremy Kerr 286f13cb971SAndrew Jeffery static inline int mctp_astlpc_kcs_write(struct mctp_binding_astlpc *astlpc, 287f13cb971SAndrew Jeffery enum mctp_binding_astlpc_kcs_reg reg, 288f13cb971SAndrew Jeffery uint8_t val) 289f13cb971SAndrew Jeffery { 290f13cb971SAndrew Jeffery return astlpc->ops.kcs_write(astlpc->ops_data, reg, val); 291f13cb971SAndrew Jeffery } 292f13cb971SAndrew Jeffery 293f13cb971SAndrew Jeffery static inline int mctp_astlpc_kcs_read(struct mctp_binding_astlpc *astlpc, 294f13cb971SAndrew Jeffery enum mctp_binding_astlpc_kcs_reg reg, 295f13cb971SAndrew Jeffery uint8_t *val) 296f13cb971SAndrew Jeffery { 297f13cb971SAndrew Jeffery return astlpc->ops.kcs_read(astlpc->ops_data, reg, val); 298f13cb971SAndrew Jeffery } 299f13cb971SAndrew Jeffery 30055fb90beSAndrew Jeffery static inline int mctp_astlpc_lpc_write(struct mctp_binding_astlpc *astlpc, 30155fb90beSAndrew Jeffery const void *buf, long offset, 30255fb90beSAndrew Jeffery size_t len) 303bc53d35aSJeremy Kerr { 30455fb90beSAndrew Jeffery astlpc_prdebug(astlpc, "%s: %zu bytes to 0x%lx", __func__, len, offset); 30555fb90beSAndrew Jeffery 30655fb90beSAndrew Jeffery assert(offset >= 0); 30755fb90beSAndrew Jeffery 30855fb90beSAndrew Jeffery /* Indirect access */ 30955fb90beSAndrew Jeffery if (astlpc->ops.lpc_write) { 31055fb90beSAndrew Jeffery void *data = astlpc->ops_data; 31155fb90beSAndrew Jeffery 31255fb90beSAndrew Jeffery return astlpc->ops.lpc_write(data, buf, offset, len); 31355fb90beSAndrew Jeffery } 31455fb90beSAndrew Jeffery 31555fb90beSAndrew Jeffery /* Direct mapping */ 31655fb90beSAndrew Jeffery assert(astlpc->lpc_map); 31755fb90beSAndrew Jeffery memcpy(&((char *)astlpc->lpc_map)[offset], buf, len); 31855fb90beSAndrew Jeffery 31955fb90beSAndrew Jeffery return 0; 32055fb90beSAndrew Jeffery } 32155fb90beSAndrew Jeffery 32255fb90beSAndrew Jeffery static inline int mctp_astlpc_lpc_read(struct mctp_binding_astlpc *astlpc, 32355fb90beSAndrew Jeffery void *buf, long offset, size_t len) 32455fb90beSAndrew Jeffery { 32555fb90beSAndrew Jeffery astlpc_prdebug(astlpc, "%s: %zu bytes from 0x%lx", __func__, len, 32655fb90beSAndrew Jeffery offset); 32755fb90beSAndrew Jeffery 32855fb90beSAndrew Jeffery assert(offset >= 0); 32955fb90beSAndrew Jeffery 33055fb90beSAndrew Jeffery /* Indirect access */ 33155fb90beSAndrew Jeffery if (astlpc->ops.lpc_read) { 33255fb90beSAndrew Jeffery void *data = astlpc->ops_data; 33355fb90beSAndrew Jeffery 33455fb90beSAndrew Jeffery return astlpc->ops.lpc_read(data, buf, offset, len); 33555fb90beSAndrew Jeffery } 33655fb90beSAndrew Jeffery 33755fb90beSAndrew Jeffery /* Direct mapping */ 33855fb90beSAndrew Jeffery assert(astlpc->lpc_map); 33955fb90beSAndrew Jeffery memcpy(buf, &((char *)astlpc->lpc_map)[offset], len); 34055fb90beSAndrew Jeffery 34155fb90beSAndrew Jeffery return 0; 342bc53d35aSJeremy Kerr } 343bc53d35aSJeremy Kerr 344d0f5da0dSAndrew Jeffery static int mctp_astlpc_kcs_set_status(struct mctp_binding_astlpc *astlpc, 345d0f5da0dSAndrew Jeffery uint8_t status) 346d0f5da0dSAndrew Jeffery { 347d0f5da0dSAndrew Jeffery uint8_t data; 348d0f5da0dSAndrew Jeffery int rc; 349d0f5da0dSAndrew Jeffery 350d0f5da0dSAndrew Jeffery /* Since we're setting the status register, we want the other endpoint 351d0f5da0dSAndrew Jeffery * to be interrupted. However, some hardware may only raise a host-side 352d0f5da0dSAndrew Jeffery * interrupt on an ODR event. 353d0f5da0dSAndrew Jeffery * So, write a dummy value of 0xff to ODR, which will ensure that an 354d0f5da0dSAndrew Jeffery * interrupt is triggered, and can be ignored by the host. 355d0f5da0dSAndrew Jeffery */ 356d0f5da0dSAndrew Jeffery data = 0xff; 357d0f5da0dSAndrew Jeffery 358f13cb971SAndrew Jeffery rc = mctp_astlpc_kcs_write(astlpc, MCTP_ASTLPC_KCS_REG_STATUS, status); 359d0f5da0dSAndrew Jeffery if (rc) { 360d0f5da0dSAndrew Jeffery astlpc_prwarn(astlpc, "KCS status write failed"); 361d0f5da0dSAndrew Jeffery return -1; 362d0f5da0dSAndrew Jeffery } 363d0f5da0dSAndrew Jeffery 364f13cb971SAndrew Jeffery rc = mctp_astlpc_kcs_write(astlpc, MCTP_ASTLPC_KCS_REG_DATA, data); 365d0f5da0dSAndrew Jeffery if (rc) { 366d0f5da0dSAndrew Jeffery astlpc_prwarn(astlpc, "KCS dummy data write failed"); 367d0f5da0dSAndrew Jeffery return -1; 368d0f5da0dSAndrew Jeffery } 369d0f5da0dSAndrew Jeffery 370d0f5da0dSAndrew Jeffery return 0; 371d0f5da0dSAndrew Jeffery } 372d0f5da0dSAndrew Jeffery 3733a540664SAndrew Jeffery static int mctp_astlpc_layout_read(struct mctp_binding_astlpc *astlpc, 3743a540664SAndrew Jeffery struct mctp_astlpc_layout *layout) 3753a540664SAndrew Jeffery { 3763a540664SAndrew Jeffery struct mctp_lpcmap_hdr hdr; 3773a540664SAndrew Jeffery int rc; 3783a540664SAndrew Jeffery 3793a540664SAndrew Jeffery rc = mctp_astlpc_lpc_read(astlpc, &hdr, 0, sizeof(hdr)); 3803a540664SAndrew Jeffery if (rc < 0) 3813a540664SAndrew Jeffery return rc; 3823a540664SAndrew Jeffery 3833a540664SAndrew Jeffery /* Flip the buffers as the names are defined in terms of the host */ 3843a540664SAndrew Jeffery if (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_BMC) { 3853a540664SAndrew Jeffery layout->rx.offset = be32toh(hdr.layout.tx_offset); 3863a540664SAndrew Jeffery layout->rx.size = be32toh(hdr.layout.tx_size); 3873a540664SAndrew Jeffery layout->tx.offset = be32toh(hdr.layout.rx_offset); 3883a540664SAndrew Jeffery layout->tx.size = be32toh(hdr.layout.rx_size); 3893a540664SAndrew Jeffery } else { 3903a540664SAndrew Jeffery assert(astlpc->mode == MCTP_BINDING_ASTLPC_MODE_HOST); 3913a540664SAndrew Jeffery 3923a540664SAndrew Jeffery layout->rx.offset = be32toh(hdr.layout.rx_offset); 3933a540664SAndrew Jeffery layout->rx.size = be32toh(hdr.layout.rx_size); 3943a540664SAndrew Jeffery layout->tx.offset = be32toh(hdr.layout.tx_offset); 3953a540664SAndrew Jeffery layout->tx.size = be32toh(hdr.layout.tx_size); 3963a540664SAndrew Jeffery } 3973a540664SAndrew Jeffery 3983a540664SAndrew Jeffery return 0; 3993a540664SAndrew Jeffery } 4003a540664SAndrew Jeffery 4013a540664SAndrew Jeffery static int mctp_astlpc_layout_write(struct mctp_binding_astlpc *astlpc, 4023a540664SAndrew Jeffery struct mctp_astlpc_layout *layout) 4033a540664SAndrew Jeffery { 4043a540664SAndrew Jeffery uint32_t rx_size_be; 4053a540664SAndrew Jeffery 4063a540664SAndrew Jeffery if (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_BMC) { 4073a540664SAndrew Jeffery struct mctp_lpcmap_hdr hdr; 4083a540664SAndrew Jeffery 4093a540664SAndrew Jeffery /* 4103a540664SAndrew Jeffery * Flip the buffers as the names are defined in terms of the 4113a540664SAndrew Jeffery * host 4123a540664SAndrew Jeffery */ 4133a540664SAndrew Jeffery hdr.layout.rx_offset = htobe32(layout->tx.offset); 4143a540664SAndrew Jeffery hdr.layout.rx_size = htobe32(layout->tx.size); 4153a540664SAndrew Jeffery hdr.layout.tx_offset = htobe32(layout->rx.offset); 4163a540664SAndrew Jeffery hdr.layout.tx_size = htobe32(layout->rx.size); 4173a540664SAndrew Jeffery 4183a540664SAndrew Jeffery return mctp_astlpc_lpc_write(astlpc, &hdr.layout, 4193a540664SAndrew Jeffery offsetof(struct mctp_lpcmap_hdr, layout), 4203a540664SAndrew Jeffery sizeof(hdr.layout)); 4213a540664SAndrew Jeffery } 4223a540664SAndrew Jeffery 4233a540664SAndrew Jeffery assert(astlpc->mode == MCTP_BINDING_ASTLPC_MODE_HOST); 4243a540664SAndrew Jeffery 4253a540664SAndrew Jeffery /* 4263a540664SAndrew Jeffery * As of v2 we only need to write rx_size - the offsets are controlled 4273a540664SAndrew Jeffery * by the BMC, as is the BMC's rx_size (host tx_size). 4283a540664SAndrew Jeffery */ 4293a540664SAndrew Jeffery rx_size_be = htobe32(layout->rx.size); 4303a540664SAndrew Jeffery return mctp_astlpc_lpc_write(astlpc, &rx_size_be, 4313a540664SAndrew Jeffery offsetof(struct mctp_lpcmap_hdr, layout.rx_size), 4323a540664SAndrew Jeffery sizeof(rx_size_be)); 4333a540664SAndrew Jeffery } 4343a540664SAndrew Jeffery 43588412be4SAndrew Jeffery static bool 43688412be4SAndrew Jeffery mctp_astlpc_buffer_validate(const struct mctp_binding_astlpc *astlpc, 43788412be4SAndrew Jeffery const struct mctp_astlpc_buffer *buf, 4383a540664SAndrew Jeffery const char *name) 4393a540664SAndrew Jeffery { 4403a540664SAndrew Jeffery /* Check for overflow */ 4413a540664SAndrew Jeffery if (buf->offset + buf->size < buf->offset) { 4423a540664SAndrew Jeffery mctp_prerr( 4433a540664SAndrew Jeffery "%s packet buffer parameters overflow: offset: 0x%" PRIx32 4443a540664SAndrew Jeffery ", size: %" PRIu32, 4453a540664SAndrew Jeffery name, buf->offset, buf->size); 4463a540664SAndrew Jeffery return false; 4473a540664SAndrew Jeffery } 4483a540664SAndrew Jeffery 4493a540664SAndrew Jeffery /* Check that the buffers are contained within the allocated space */ 4503a540664SAndrew Jeffery if (buf->offset + buf->size > LPC_WIN_SIZE) { 4513a540664SAndrew Jeffery mctp_prerr( 4523a540664SAndrew Jeffery "%s packet buffer parameters exceed %uM window size: offset: 0x%" PRIx32 4533a540664SAndrew Jeffery ", size: %" PRIu32, 4543a540664SAndrew Jeffery name, (LPC_WIN_SIZE / (1024 * 1024)), buf->offset, 4553a540664SAndrew Jeffery buf->size); 4563a540664SAndrew Jeffery return false; 4573a540664SAndrew Jeffery } 4583a540664SAndrew Jeffery 4593a540664SAndrew Jeffery /* Check that the baseline transmission unit is supported */ 46088412be4SAndrew Jeffery if (buf->size < astlpc->proto->packet_size(MCTP_PACKET_SIZE(MCTP_BTU))) { 4613a540664SAndrew Jeffery mctp_prerr( 46288412be4SAndrew Jeffery "%s packet buffer too small: Require %" PRIu32 " bytes to support the %u byte baseline transmission unit, found %" PRIu32, 46388412be4SAndrew Jeffery name, 46488412be4SAndrew Jeffery astlpc->proto->packet_size(MCTP_PACKET_SIZE(MCTP_BTU)), 4653a540664SAndrew Jeffery MCTP_BTU, buf->size); 4663a540664SAndrew Jeffery return false; 4673a540664SAndrew Jeffery } 4683a540664SAndrew Jeffery 4693a540664SAndrew Jeffery /* Check for overlap with the control space */ 4703a540664SAndrew Jeffery if (buf->offset < control_size) { 4713a540664SAndrew Jeffery mctp_prerr( 4723a540664SAndrew Jeffery "%s packet buffer overlaps control region {0x%" PRIx32 4733a540664SAndrew Jeffery ", %" PRIu32 "}: Rx {0x%" PRIx32 ", %" PRIu32 "}", 4743a540664SAndrew Jeffery name, 0U, control_size, buf->offset, buf->size); 4753a540664SAndrew Jeffery return false; 4763a540664SAndrew Jeffery } 4773a540664SAndrew Jeffery 4783a540664SAndrew Jeffery return true; 4793a540664SAndrew Jeffery } 4803a540664SAndrew Jeffery 48188412be4SAndrew Jeffery static bool 48288412be4SAndrew Jeffery mctp_astlpc_layout_validate(const struct mctp_binding_astlpc *astlpc, 48388412be4SAndrew Jeffery const struct mctp_astlpc_layout *layout) 4843a540664SAndrew Jeffery { 48588412be4SAndrew Jeffery const struct mctp_astlpc_buffer *rx = &layout->rx; 48688412be4SAndrew Jeffery const struct mctp_astlpc_buffer *tx = &layout->tx; 4873a540664SAndrew Jeffery bool rx_valid, tx_valid; 4883a540664SAndrew Jeffery 48988412be4SAndrew Jeffery rx_valid = mctp_astlpc_buffer_validate(astlpc, rx, "Rx"); 49088412be4SAndrew Jeffery tx_valid = mctp_astlpc_buffer_validate(astlpc, tx, "Tx"); 4913a540664SAndrew Jeffery 4923a540664SAndrew Jeffery if (!(rx_valid && tx_valid)) 4933a540664SAndrew Jeffery return false; 4943a540664SAndrew Jeffery 4953a540664SAndrew Jeffery /* Check that the buffers are disjoint */ 4963a540664SAndrew Jeffery if ((rx->offset <= tx->offset && rx->offset + rx->size > tx->offset) || 4973a540664SAndrew Jeffery (tx->offset <= rx->offset && tx->offset + tx->size > rx->offset)) { 4983a540664SAndrew Jeffery mctp_prerr("Rx and Tx packet buffers overlap: Rx {0x%" PRIx32 4993a540664SAndrew Jeffery ", %" PRIu32 "}, Tx {0x%" PRIx32 ", %" PRIu32 "}", 5003a540664SAndrew Jeffery rx->offset, rx->size, tx->offset, tx->size); 5013a540664SAndrew Jeffery return false; 5023a540664SAndrew Jeffery } 5033a540664SAndrew Jeffery 5043a540664SAndrew Jeffery return true; 5053a540664SAndrew Jeffery } 5063a540664SAndrew Jeffery 5077cd72f14SAndrew Jeffery static int mctp_astlpc_init_bmc(struct mctp_binding_astlpc *astlpc) 5087cd72f14SAndrew Jeffery { 50955fb90beSAndrew Jeffery struct mctp_lpcmap_hdr hdr = { 0 }; 5107cd72f14SAndrew Jeffery uint8_t status; 51188412be4SAndrew Jeffery uint32_t sz; 5123a540664SAndrew Jeffery 5133a540664SAndrew Jeffery /* 5143a540664SAndrew Jeffery * The largest buffer size is half of the allocated MCTP space 5153a540664SAndrew Jeffery * excluding the control space. 5163a540664SAndrew Jeffery */ 5173a540664SAndrew Jeffery sz = ((LPC_WIN_SIZE - control_size) / 2); 5183a540664SAndrew Jeffery 5193a540664SAndrew Jeffery /* 5203a540664SAndrew Jeffery * Trim the MTU to a multiple of 16 to meet the requirements of 12.17 5213a540664SAndrew Jeffery * Query Hop in DSP0236 v1.3.0. 5223a540664SAndrew Jeffery */ 52388412be4SAndrew Jeffery sz = MCTP_BODY_SIZE(astlpc->proto->body_size(sz)); 5243a540664SAndrew Jeffery sz &= ~0xfUL; 52588412be4SAndrew Jeffery sz = astlpc->proto->packet_size(MCTP_PACKET_SIZE(sz)); 5267cd72f14SAndrew Jeffery 527a9368980SAndrew Jeffery if (astlpc->requested_mtu) { 52888412be4SAndrew Jeffery uint32_t rpkt, rmtu; 529a9368980SAndrew Jeffery 53088412be4SAndrew Jeffery rmtu = astlpc->requested_mtu; 53188412be4SAndrew Jeffery rpkt = astlpc->proto->packet_size(MCTP_PACKET_SIZE(rmtu)); 53288412be4SAndrew Jeffery sz = MIN(sz, rpkt); 533a9368980SAndrew Jeffery } 534a9368980SAndrew Jeffery 5357cd72f14SAndrew Jeffery /* Flip the buffers as the names are defined in terms of the host */ 5363a540664SAndrew Jeffery astlpc->layout.tx.offset = control_size; 5373a540664SAndrew Jeffery astlpc->layout.tx.size = sz; 5383a540664SAndrew Jeffery astlpc->layout.rx.offset = 5393a540664SAndrew Jeffery astlpc->layout.tx.offset + astlpc->layout.tx.size; 5403a540664SAndrew Jeffery astlpc->layout.rx.size = sz; 5413a540664SAndrew Jeffery 54288412be4SAndrew Jeffery if (!mctp_astlpc_layout_validate(astlpc, &astlpc->layout)) { 54388412be4SAndrew Jeffery astlpc_prerr(astlpc, "Cannot support an MTU of %" PRIu32, sz); 544a9368980SAndrew Jeffery return -EINVAL; 545a9368980SAndrew Jeffery } 5467cd72f14SAndrew Jeffery 54755fb90beSAndrew Jeffery hdr = (struct mctp_lpcmap_hdr){ 54855fb90beSAndrew Jeffery .magic = htobe32(ASTLPC_MCTP_MAGIC), 54955fb90beSAndrew Jeffery .bmc_ver_min = htobe16(ASTLPC_VER_MIN), 55055fb90beSAndrew Jeffery .bmc_ver_cur = htobe16(ASTLPC_VER_CUR), 5517cd72f14SAndrew Jeffery 5527cd72f14SAndrew Jeffery /* Flip the buffers back as we're now describing the host's 5537cd72f14SAndrew Jeffery * configuration to the host */ 5543a540664SAndrew Jeffery .layout.rx_offset = htobe32(astlpc->layout.tx.offset), 5553a540664SAndrew Jeffery .layout.rx_size = htobe32(astlpc->layout.tx.size), 5563a540664SAndrew Jeffery .layout.tx_offset = htobe32(astlpc->layout.rx.offset), 5573a540664SAndrew Jeffery .layout.tx_size = htobe32(astlpc->layout.rx.size), 55855fb90beSAndrew Jeffery }; 5597cd72f14SAndrew Jeffery 56055fb90beSAndrew Jeffery mctp_astlpc_lpc_write(astlpc, &hdr, 0, sizeof(hdr)); 5617cd72f14SAndrew Jeffery 562b3b55a6bSAndrew Jeffery /* 563b3b55a6bSAndrew Jeffery * Set status indicating that the BMC is now active. Be explicit about 564b3b55a6bSAndrew Jeffery * clearing OBF; we're reinitialising the binding and so any previous 565b3b55a6bSAndrew Jeffery * buffer state is irrelevant. 566b3b55a6bSAndrew Jeffery */ 567b3b55a6bSAndrew Jeffery status = KCS_STATUS_BMC_READY & ~KCS_STATUS_OBF; 568d0f5da0dSAndrew Jeffery return mctp_astlpc_kcs_set_status(astlpc, status); 5697cd72f14SAndrew Jeffery } 5707cd72f14SAndrew Jeffery 5717cd72f14SAndrew Jeffery static int mctp_binding_astlpc_start_bmc(struct mctp_binding *b) 5727cd72f14SAndrew Jeffery { 5737cd72f14SAndrew Jeffery struct mctp_binding_astlpc *astlpc = 5747cd72f14SAndrew Jeffery container_of(b, struct mctp_binding_astlpc, binding); 5757cd72f14SAndrew Jeffery 57688412be4SAndrew Jeffery astlpc->proto = &astlpc_protocol_version[ASTLPC_VER_CUR]; 57788412be4SAndrew Jeffery 5787cd72f14SAndrew Jeffery return mctp_astlpc_init_bmc(astlpc); 5797cd72f14SAndrew Jeffery } 5807cd72f14SAndrew Jeffery 5814e8264b7SAndrew Jeffery static bool mctp_astlpc_validate_version(uint16_t bmc_ver_min, 5824e8264b7SAndrew Jeffery uint16_t bmc_ver_cur, 5834e8264b7SAndrew Jeffery uint16_t host_ver_min, 5844e8264b7SAndrew Jeffery uint16_t host_ver_cur) 5854e8264b7SAndrew Jeffery { 5864e8264b7SAndrew Jeffery if (!(bmc_ver_min && bmc_ver_cur && host_ver_min && host_ver_cur)) { 5874e8264b7SAndrew Jeffery mctp_prerr("Invalid version present in [%" PRIu16 ", %" PRIu16 5884e8264b7SAndrew Jeffery "], [%" PRIu16 ", %" PRIu16 "]", 5894e8264b7SAndrew Jeffery bmc_ver_min, bmc_ver_cur, host_ver_min, 5904e8264b7SAndrew Jeffery host_ver_cur); 5914e8264b7SAndrew Jeffery return false; 5924e8264b7SAndrew Jeffery } else if (bmc_ver_min > bmc_ver_cur) { 5934e8264b7SAndrew Jeffery mctp_prerr("Invalid bmc version range [%" PRIu16 ", %" PRIu16 5944e8264b7SAndrew Jeffery "]", 5954e8264b7SAndrew Jeffery bmc_ver_min, bmc_ver_cur); 5964e8264b7SAndrew Jeffery return false; 5974e8264b7SAndrew Jeffery } else if (host_ver_min > host_ver_cur) { 5984e8264b7SAndrew Jeffery mctp_prerr("Invalid host version range [%" PRIu16 ", %" PRIu16 5994e8264b7SAndrew Jeffery "]", 6004e8264b7SAndrew Jeffery host_ver_min, host_ver_cur); 6014e8264b7SAndrew Jeffery return false; 6024e8264b7SAndrew Jeffery } else if ((host_ver_cur < bmc_ver_min) || 6034e8264b7SAndrew Jeffery (host_ver_min > bmc_ver_cur)) { 6044e8264b7SAndrew Jeffery mctp_prerr( 6054e8264b7SAndrew Jeffery "Unable to satisfy version negotiation with ranges [%" PRIu16 6064e8264b7SAndrew Jeffery ", %" PRIu16 "] and [%" PRIu16 ", %" PRIu16 "]", 6074e8264b7SAndrew Jeffery bmc_ver_min, bmc_ver_cur, host_ver_min, host_ver_cur); 6084e8264b7SAndrew Jeffery return false; 6094e8264b7SAndrew Jeffery } 6104e8264b7SAndrew Jeffery 6114e8264b7SAndrew Jeffery return true; 6124e8264b7SAndrew Jeffery } 6134e8264b7SAndrew Jeffery 6143a540664SAndrew Jeffery static int mctp_astlpc_negotiate_layout_host(struct mctp_binding_astlpc *astlpc) 6153a540664SAndrew Jeffery { 6163a540664SAndrew Jeffery struct mctp_astlpc_layout layout; 61788412be4SAndrew Jeffery uint32_t rmtu; 6183a540664SAndrew Jeffery uint32_t sz; 6193a540664SAndrew Jeffery int rc; 6203a540664SAndrew Jeffery 6213a540664SAndrew Jeffery rc = mctp_astlpc_layout_read(astlpc, &layout); 6223a540664SAndrew Jeffery if (rc < 0) 6233a540664SAndrew Jeffery return rc; 6243a540664SAndrew Jeffery 62588412be4SAndrew Jeffery if (!mctp_astlpc_layout_validate(astlpc, &layout)) { 6263a540664SAndrew Jeffery astlpc_prerr( 6273a540664SAndrew Jeffery astlpc, 6283a540664SAndrew Jeffery "BMC provided invalid buffer layout: Rx {0x%" PRIx32 6293a540664SAndrew Jeffery ", %" PRIu32 "}, Tx {0x%" PRIx32 ", %" PRIu32 "}", 6303a540664SAndrew Jeffery layout.rx.offset, layout.rx.size, layout.tx.offset, 6313a540664SAndrew Jeffery layout.tx.size); 6323a540664SAndrew Jeffery return -EINVAL; 6333a540664SAndrew Jeffery } 6343a540664SAndrew Jeffery 635a9368980SAndrew Jeffery astlpc_prinfo(astlpc, "Desire an MTU of %" PRIu32 " bytes", 636a9368980SAndrew Jeffery astlpc->requested_mtu); 637a9368980SAndrew Jeffery 63888412be4SAndrew Jeffery rmtu = astlpc->requested_mtu; 63988412be4SAndrew Jeffery sz = astlpc->proto->packet_size(MCTP_PACKET_SIZE(rmtu)); 6403a540664SAndrew Jeffery layout.rx.size = sz; 6413a540664SAndrew Jeffery 64288412be4SAndrew Jeffery if (!mctp_astlpc_layout_validate(astlpc, &layout)) { 6433a540664SAndrew Jeffery astlpc_prerr( 6443a540664SAndrew Jeffery astlpc, 6453a540664SAndrew Jeffery "Generated invalid buffer layout with size %" PRIu32 6463a540664SAndrew Jeffery ": Rx {0x%" PRIx32 ", %" PRIu32 "}, Tx {0x%" PRIx32 6473a540664SAndrew Jeffery ", %" PRIu32 "}", 6483a540664SAndrew Jeffery sz, layout.rx.offset, layout.rx.size, layout.tx.offset, 6493a540664SAndrew Jeffery layout.tx.size); 6503a540664SAndrew Jeffery return -EINVAL; 6513a540664SAndrew Jeffery } 6523a540664SAndrew Jeffery 653a9368980SAndrew Jeffery astlpc_prinfo(astlpc, "Requesting MTU of %" PRIu32 " bytes", 654a9368980SAndrew Jeffery astlpc->requested_mtu); 6553a540664SAndrew Jeffery 6563a540664SAndrew Jeffery return mctp_astlpc_layout_write(astlpc, &layout); 6573a540664SAndrew Jeffery } 6583a540664SAndrew Jeffery 65988412be4SAndrew Jeffery static uint16_t mctp_astlpc_negotiate_version(uint16_t bmc_ver_min, 66088412be4SAndrew Jeffery uint16_t bmc_ver_cur, 66188412be4SAndrew Jeffery uint16_t host_ver_min, 66288412be4SAndrew Jeffery uint16_t host_ver_cur) 66388412be4SAndrew Jeffery { 66488412be4SAndrew Jeffery if (!mctp_astlpc_validate_version(bmc_ver_min, bmc_ver_cur, 66588412be4SAndrew Jeffery host_ver_min, host_ver_cur)) 66688412be4SAndrew Jeffery return ASTLPC_VER_BAD; 66788412be4SAndrew Jeffery 66888412be4SAndrew Jeffery if (bmc_ver_cur < host_ver_cur) 66988412be4SAndrew Jeffery return bmc_ver_cur; 67088412be4SAndrew Jeffery 67188412be4SAndrew Jeffery return host_ver_cur; 67288412be4SAndrew Jeffery } 67388412be4SAndrew Jeffery 6747cd72f14SAndrew Jeffery static int mctp_astlpc_init_host(struct mctp_binding_astlpc *astlpc) 6757cd72f14SAndrew Jeffery { 67655fb90beSAndrew Jeffery const uint16_t ver_min_be = htobe16(ASTLPC_VER_MIN); 67755fb90beSAndrew Jeffery const uint16_t ver_cur_be = htobe16(ASTLPC_VER_CUR); 67888412be4SAndrew Jeffery uint16_t bmc_ver_min, bmc_ver_cur, negotiated; 67955fb90beSAndrew Jeffery struct mctp_lpcmap_hdr hdr; 6807cd72f14SAndrew Jeffery uint8_t status; 6817cd72f14SAndrew Jeffery int rc; 6827cd72f14SAndrew Jeffery 683f13cb971SAndrew Jeffery rc = mctp_astlpc_kcs_read(astlpc, MCTP_ASTLPC_KCS_REG_STATUS, &status); 6847cd72f14SAndrew Jeffery if (rc) { 6857cd72f14SAndrew Jeffery mctp_prwarn("KCS status read failed"); 6867cd72f14SAndrew Jeffery return rc; 6877cd72f14SAndrew Jeffery } 6887cd72f14SAndrew Jeffery 6897cd72f14SAndrew Jeffery astlpc->kcs_status = status; 6907cd72f14SAndrew Jeffery 6917cd72f14SAndrew Jeffery if (!(status & KCS_STATUS_BMC_READY)) 6927cd72f14SAndrew Jeffery return -EHOSTDOWN; 6937cd72f14SAndrew Jeffery 69455fb90beSAndrew Jeffery mctp_astlpc_lpc_read(astlpc, &hdr, 0, sizeof(hdr)); 6957cd72f14SAndrew Jeffery 6964e8264b7SAndrew Jeffery bmc_ver_min = be16toh(hdr.bmc_ver_min); 6974e8264b7SAndrew Jeffery bmc_ver_cur = be16toh(hdr.bmc_ver_cur); 6984e8264b7SAndrew Jeffery 69988412be4SAndrew Jeffery /* Calculate the expected value of negotiated_ver */ 70088412be4SAndrew Jeffery negotiated = mctp_astlpc_negotiate_version(bmc_ver_min, bmc_ver_cur, 70188412be4SAndrew Jeffery ASTLPC_VER_MIN, 70288412be4SAndrew Jeffery ASTLPC_VER_CUR); 70388412be4SAndrew Jeffery if (!negotiated) { 7044e8264b7SAndrew Jeffery astlpc_prerr(astlpc, "Cannot negotiate with invalid versions"); 7054e8264b7SAndrew Jeffery return -EINVAL; 7064e8264b7SAndrew Jeffery } 7074e8264b7SAndrew Jeffery 70888412be4SAndrew Jeffery /* Assign protocol ops so we can calculate the packet buffer sizes */ 70988412be4SAndrew Jeffery assert(negotiated < ARRAY_SIZE(astlpc_protocol_version)); 71088412be4SAndrew Jeffery astlpc->proto = &astlpc_protocol_version[negotiated]; 71188412be4SAndrew Jeffery 71288412be4SAndrew Jeffery /* Negotiate packet buffers in v2 style if the BMC supports it */ 71388412be4SAndrew Jeffery if (negotiated >= 2) { 7143a540664SAndrew Jeffery rc = mctp_astlpc_negotiate_layout_host(astlpc); 7153a540664SAndrew Jeffery if (rc < 0) 7163a540664SAndrew Jeffery return rc; 7173a540664SAndrew Jeffery } 7183a540664SAndrew Jeffery 71988412be4SAndrew Jeffery /* Advertise the host's supported protocol versions */ 72055fb90beSAndrew Jeffery mctp_astlpc_lpc_write(astlpc, &ver_min_be, 72155fb90beSAndrew Jeffery offsetof(struct mctp_lpcmap_hdr, host_ver_min), 72255fb90beSAndrew Jeffery sizeof(ver_min_be)); 7237cd72f14SAndrew Jeffery 72455fb90beSAndrew Jeffery mctp_astlpc_lpc_write(astlpc, &ver_cur_be, 72555fb90beSAndrew Jeffery offsetof(struct mctp_lpcmap_hdr, host_ver_cur), 72655fb90beSAndrew Jeffery sizeof(ver_cur_be)); 7277cd72f14SAndrew Jeffery 7287cd72f14SAndrew Jeffery /* Send channel init command */ 729f13cb971SAndrew Jeffery rc = mctp_astlpc_kcs_write(astlpc, MCTP_ASTLPC_KCS_REG_DATA, 0x0); 7307cd72f14SAndrew Jeffery if (rc) { 7319101a2abSAndrew Jeffery astlpc_prwarn(astlpc, "KCS write failed"); 7327cd72f14SAndrew Jeffery } 7337cd72f14SAndrew Jeffery 73488412be4SAndrew Jeffery /* 73588412be4SAndrew Jeffery * Configure the host so `astlpc->proto->version == 0` holds until we 73688412be4SAndrew Jeffery * receive a subsequent status update from the BMC. Until then, 73788412be4SAndrew Jeffery * `astlpc->proto->version == 0` indicates that we're yet to complete 73888412be4SAndrew Jeffery * the channel initialisation handshake. 73988412be4SAndrew Jeffery * 74088412be4SAndrew Jeffery * When the BMC provides a status update with KCS_STATUS_CHANNEL_ACTIVE 74188412be4SAndrew Jeffery * set we will assign the appropriate protocol ops struct in accordance 74288412be4SAndrew Jeffery * with `negotiated_ver`. 74388412be4SAndrew Jeffery */ 74488412be4SAndrew Jeffery astlpc->proto = &astlpc_protocol_version[ASTLPC_VER_BAD]; 74588412be4SAndrew Jeffery 7467cd72f14SAndrew Jeffery return rc; 7477cd72f14SAndrew Jeffery } 7487cd72f14SAndrew Jeffery 7497cd72f14SAndrew Jeffery static int mctp_binding_astlpc_start_host(struct mctp_binding *b) 7507cd72f14SAndrew Jeffery { 7517cd72f14SAndrew Jeffery struct mctp_binding_astlpc *astlpc = 7527cd72f14SAndrew Jeffery container_of(b, struct mctp_binding_astlpc, binding); 7537cd72f14SAndrew Jeffery 7547cd72f14SAndrew Jeffery return mctp_astlpc_init_host(astlpc); 7557cd72f14SAndrew Jeffery } 7567cd72f14SAndrew Jeffery 7577cd72f14SAndrew Jeffery static bool __mctp_astlpc_kcs_ready(struct mctp_binding_astlpc *astlpc, 7587cd72f14SAndrew Jeffery uint8_t status, bool is_write) 7597cd72f14SAndrew Jeffery { 7607cd72f14SAndrew Jeffery bool is_bmc; 7617cd72f14SAndrew Jeffery bool ready_state; 7627cd72f14SAndrew Jeffery uint8_t flag; 7637cd72f14SAndrew Jeffery 7647cd72f14SAndrew Jeffery is_bmc = (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_BMC); 7657cd72f14SAndrew Jeffery flag = (is_bmc ^ is_write) ? KCS_STATUS_IBF : KCS_STATUS_OBF; 7667cd72f14SAndrew Jeffery ready_state = is_write ? 0 : 1; 7677cd72f14SAndrew Jeffery 7687cd72f14SAndrew Jeffery return !!(status & flag) == ready_state; 7697cd72f14SAndrew Jeffery } 7707cd72f14SAndrew Jeffery 7717cd72f14SAndrew Jeffery static inline bool 7727cd72f14SAndrew Jeffery mctp_astlpc_kcs_read_ready(struct mctp_binding_astlpc *astlpc, uint8_t status) 7737cd72f14SAndrew Jeffery { 7747cd72f14SAndrew Jeffery return __mctp_astlpc_kcs_ready(astlpc, status, false); 7757cd72f14SAndrew Jeffery } 7767cd72f14SAndrew Jeffery 7777cd72f14SAndrew Jeffery static inline bool 7787cd72f14SAndrew Jeffery mctp_astlpc_kcs_write_ready(struct mctp_binding_astlpc *astlpc, uint8_t status) 7797cd72f14SAndrew Jeffery { 7807cd72f14SAndrew Jeffery return __mctp_astlpc_kcs_ready(astlpc, status, true); 7817cd72f14SAndrew Jeffery } 7827cd72f14SAndrew Jeffery 783672c8852SJeremy Kerr static int mctp_astlpc_kcs_send(struct mctp_binding_astlpc *astlpc, 784672c8852SJeremy Kerr uint8_t data) 785672c8852SJeremy Kerr { 786672c8852SJeremy Kerr uint8_t status; 787672c8852SJeremy Kerr int rc; 788672c8852SJeremy Kerr 789f13cb971SAndrew Jeffery rc = mctp_astlpc_kcs_read(astlpc, MCTP_ASTLPC_KCS_REG_STATUS, 790f13cb971SAndrew Jeffery &status); 7911b27fe87SAndrew Jeffery if (rc) { 7929101a2abSAndrew Jeffery astlpc_prwarn(astlpc, "KCS status read failed"); 793fe763e98SAndrew Jeffery return -EIO; 794672c8852SJeremy Kerr } 795fe763e98SAndrew Jeffery if (!mctp_astlpc_kcs_write_ready(astlpc, status)) 796fe763e98SAndrew Jeffery return -EBUSY; 797672c8852SJeremy Kerr 798f13cb971SAndrew Jeffery rc = mctp_astlpc_kcs_write(astlpc, MCTP_ASTLPC_KCS_REG_DATA, data); 799bc53d35aSJeremy Kerr if (rc) { 8009101a2abSAndrew Jeffery astlpc_prwarn(astlpc, "KCS data write failed"); 801fe763e98SAndrew Jeffery return -EIO; 802672c8852SJeremy Kerr } 803672c8852SJeremy Kerr 804672c8852SJeremy Kerr return 0; 805672c8852SJeremy Kerr } 806672c8852SJeremy Kerr 807672c8852SJeremy Kerr static int mctp_binding_astlpc_tx(struct mctp_binding *b, 808672c8852SJeremy Kerr struct mctp_pktbuf *pkt) 809672c8852SJeremy Kerr { 810672c8852SJeremy Kerr struct mctp_binding_astlpc *astlpc = binding_to_astlpc(b); 81155fb90beSAndrew Jeffery uint32_t len, len_be; 812edfe383fSAndrew Jeffery struct mctp_hdr *hdr; 813fe763e98SAndrew Jeffery int rc; 814672c8852SJeremy Kerr 815edfe383fSAndrew Jeffery hdr = mctp_pktbuf_hdr(pkt); 816672c8852SJeremy Kerr len = mctp_pktbuf_size(pkt); 817edfe383fSAndrew Jeffery 8189101a2abSAndrew Jeffery astlpc_prdebug(astlpc, 8199101a2abSAndrew Jeffery "%s: Transmitting %" PRIu32 8209101a2abSAndrew Jeffery "-byte packet (%hhu, %hhu, 0x%hhx)", 821edfe383fSAndrew Jeffery __func__, len, hdr->src, hdr->dest, hdr->flags_seq_tag); 822edfe383fSAndrew Jeffery 82388412be4SAndrew Jeffery if (len > astlpc->proto->body_size(astlpc->layout.tx.size)) { 824*0721f585SAndrew Jeffery astlpc_prwarn(astlpc, "invalid TX len %" PRIu32 ": %" PRIu32, 825*0721f585SAndrew Jeffery len, 826eba19a3bSAndrew Jeffery astlpc->proto->body_size(astlpc->layout.tx.size)); 827*0721f585SAndrew Jeffery return -EMSGSIZE; 828672c8852SJeremy Kerr } 829672c8852SJeremy Kerr 830fe763e98SAndrew Jeffery mctp_binding_set_tx_enabled(b, false); 831fe763e98SAndrew Jeffery 83255fb90beSAndrew Jeffery len_be = htobe32(len); 83355fb90beSAndrew Jeffery mctp_astlpc_lpc_write(astlpc, &len_be, astlpc->layout.tx.offset, 83455fb90beSAndrew Jeffery sizeof(len_be)); 835eba19a3bSAndrew Jeffery 836eba19a3bSAndrew Jeffery astlpc->proto->pktbuf_protect(pkt); 837eba19a3bSAndrew Jeffery len = mctp_pktbuf_size(pkt); 838eba19a3bSAndrew Jeffery 83955fb90beSAndrew Jeffery mctp_astlpc_lpc_write(astlpc, hdr, astlpc->layout.tx.offset + 4, len); 840672c8852SJeremy Kerr 841fe763e98SAndrew Jeffery astlpc->layout.tx.state = buffer_state_prepared; 842672c8852SJeremy Kerr 843fe763e98SAndrew Jeffery rc = mctp_astlpc_kcs_send(astlpc, 0x1); 844fe763e98SAndrew Jeffery if (!rc) 845fe763e98SAndrew Jeffery astlpc->layout.tx.state = buffer_state_released; 846eba19a3bSAndrew Jeffery 847fe763e98SAndrew Jeffery return rc == -EBUSY ? 0 : rc; 848672c8852SJeremy Kerr } 849672c8852SJeremy Kerr 8503a540664SAndrew Jeffery static uint32_t mctp_astlpc_calculate_mtu(struct mctp_binding_astlpc *astlpc, 8513a540664SAndrew Jeffery struct mctp_astlpc_layout *layout) 8523a540664SAndrew Jeffery { 85388412be4SAndrew Jeffery uint32_t low, high, limit, rpkt; 8543a540664SAndrew Jeffery 8553a540664SAndrew Jeffery /* Derive the largest MTU the BMC _can_ support */ 8563a540664SAndrew Jeffery low = MIN(astlpc->layout.rx.offset, astlpc->layout.tx.offset); 8573a540664SAndrew Jeffery high = MAX(astlpc->layout.rx.offset, astlpc->layout.tx.offset); 8583a540664SAndrew Jeffery limit = high - low; 8593a540664SAndrew Jeffery 860a9368980SAndrew Jeffery /* Determine the largest MTU the BMC _wants_ to support */ 861a9368980SAndrew Jeffery if (astlpc->requested_mtu) { 86288412be4SAndrew Jeffery uint32_t rmtu = astlpc->requested_mtu; 863a9368980SAndrew Jeffery 86488412be4SAndrew Jeffery rpkt = astlpc->proto->packet_size(MCTP_PACKET_SIZE(rmtu)); 86588412be4SAndrew Jeffery limit = MIN(limit, rpkt); 866a9368980SAndrew Jeffery } 8673a540664SAndrew Jeffery 8683a540664SAndrew Jeffery /* Determine the accepted MTU, applied both directions by convention */ 86988412be4SAndrew Jeffery rpkt = MIN(limit, layout->tx.size); 87088412be4SAndrew Jeffery return MCTP_BODY_SIZE(astlpc->proto->body_size(rpkt)); 8713a540664SAndrew Jeffery } 8723a540664SAndrew Jeffery 87388412be4SAndrew Jeffery static int mctp_astlpc_negotiate_layout_bmc(struct mctp_binding_astlpc *astlpc) 8743a540664SAndrew Jeffery { 8753a540664SAndrew Jeffery struct mctp_astlpc_layout proposed, pending; 8763a540664SAndrew Jeffery uint32_t sz, mtu; 8773a540664SAndrew Jeffery int rc; 8783a540664SAndrew Jeffery 87988412be4SAndrew Jeffery /* Do we have a valid protocol version? */ 88088412be4SAndrew Jeffery if (!astlpc->proto->version) 88188412be4SAndrew Jeffery return -EINVAL; 88288412be4SAndrew Jeffery 8833a540664SAndrew Jeffery /* Extract the host's proposed layout */ 8843a540664SAndrew Jeffery rc = mctp_astlpc_layout_read(astlpc, &proposed); 8853a540664SAndrew Jeffery if (rc < 0) 8863a540664SAndrew Jeffery return rc; 8873a540664SAndrew Jeffery 88888412be4SAndrew Jeffery /* Do we have a reasonable layout? */ 88988412be4SAndrew Jeffery if (!mctp_astlpc_layout_validate(astlpc, &proposed)) 8903a540664SAndrew Jeffery return -EINVAL; 8913a540664SAndrew Jeffery 8923a540664SAndrew Jeffery /* Negotiate the MTU */ 8933a540664SAndrew Jeffery mtu = mctp_astlpc_calculate_mtu(astlpc, &proposed); 89488412be4SAndrew Jeffery sz = astlpc->proto->packet_size(MCTP_PACKET_SIZE(mtu)); 8953a540664SAndrew Jeffery 8963a540664SAndrew Jeffery /* 8973a540664SAndrew Jeffery * Use symmetric MTUs by convention and to pass constraints in rx/tx 8983a540664SAndrew Jeffery * functions 8993a540664SAndrew Jeffery */ 9003a540664SAndrew Jeffery pending = astlpc->layout; 9013a540664SAndrew Jeffery pending.tx.size = sz; 9023a540664SAndrew Jeffery pending.rx.size = sz; 9033a540664SAndrew Jeffery 90488412be4SAndrew Jeffery if (mctp_astlpc_layout_validate(astlpc, &pending)) { 9053a540664SAndrew Jeffery /* We found a sensible Rx MTU, so honour it */ 9063a540664SAndrew Jeffery astlpc->layout = pending; 9073a540664SAndrew Jeffery 9083a540664SAndrew Jeffery /* Enforce the negotiated MTU */ 9093a540664SAndrew Jeffery rc = mctp_astlpc_layout_write(astlpc, &astlpc->layout); 9103a540664SAndrew Jeffery if (rc < 0) 9113a540664SAndrew Jeffery return rc; 9123a540664SAndrew Jeffery 9133a540664SAndrew Jeffery astlpc_prinfo(astlpc, "Negotiated an MTU of %" PRIu32 " bytes", 9143a540664SAndrew Jeffery mtu); 9153a540664SAndrew Jeffery } else { 9163a540664SAndrew Jeffery astlpc_prwarn(astlpc, "MTU negotiation failed"); 9173a540664SAndrew Jeffery return -EINVAL; 9183a540664SAndrew Jeffery } 9193a540664SAndrew Jeffery 92088412be4SAndrew Jeffery if (astlpc->proto->version >= 2) 9213a540664SAndrew Jeffery astlpc->binding.pkt_size = MCTP_PACKET_SIZE(mtu); 9223a540664SAndrew Jeffery 9233a540664SAndrew Jeffery return 0; 9243a540664SAndrew Jeffery } 9253a540664SAndrew Jeffery 926672c8852SJeremy Kerr static void mctp_astlpc_init_channel(struct mctp_binding_astlpc *astlpc) 927672c8852SJeremy Kerr { 9284e8264b7SAndrew Jeffery uint16_t negotiated, negotiated_be; 9294e8264b7SAndrew Jeffery struct mctp_lpcmap_hdr hdr; 9304e8264b7SAndrew Jeffery uint8_t status; 9313a540664SAndrew Jeffery int rc; 9324e8264b7SAndrew Jeffery 9334e8264b7SAndrew Jeffery mctp_astlpc_lpc_read(astlpc, &hdr, 0, sizeof(hdr)); 9344e8264b7SAndrew Jeffery 9354e8264b7SAndrew Jeffery /* Version negotiation */ 9364e8264b7SAndrew Jeffery negotiated = 9374e8264b7SAndrew Jeffery mctp_astlpc_negotiate_version(ASTLPC_VER_MIN, ASTLPC_VER_CUR, 9384e8264b7SAndrew Jeffery be16toh(hdr.host_ver_min), 9394e8264b7SAndrew Jeffery be16toh(hdr.host_ver_cur)); 9404e8264b7SAndrew Jeffery 94188412be4SAndrew Jeffery /* MTU negotiation requires knowing which protocol we'll use */ 94288412be4SAndrew Jeffery assert(negotiated < ARRAY_SIZE(astlpc_protocol_version)); 94388412be4SAndrew Jeffery astlpc->proto = &astlpc_protocol_version[negotiated]; 94488412be4SAndrew Jeffery 9453a540664SAndrew Jeffery /* Host Rx MTU negotiation: Failure terminates channel init */ 94688412be4SAndrew Jeffery rc = mctp_astlpc_negotiate_layout_bmc(astlpc); 9473a540664SAndrew Jeffery if (rc < 0) 9483a540664SAndrew Jeffery negotiated = ASTLPC_VER_BAD; 9493a540664SAndrew Jeffery 9504e8264b7SAndrew Jeffery /* Populate the negotiated version */ 9514e8264b7SAndrew Jeffery negotiated_be = htobe16(negotiated); 9524e8264b7SAndrew Jeffery mctp_astlpc_lpc_write(astlpc, &negotiated_be, 95355fb90beSAndrew Jeffery offsetof(struct mctp_lpcmap_hdr, negotiated_ver), 9544e8264b7SAndrew Jeffery sizeof(negotiated_be)); 95555fb90beSAndrew Jeffery 956fe763e98SAndrew Jeffery /* Track buffer ownership */ 957fe763e98SAndrew Jeffery astlpc->layout.tx.state = buffer_state_acquired; 958fe763e98SAndrew Jeffery astlpc->layout.rx.state = buffer_state_released; 959fe763e98SAndrew Jeffery 9604e8264b7SAndrew Jeffery /* Finalise the configuration */ 9614e8264b7SAndrew Jeffery status = KCS_STATUS_BMC_READY | KCS_STATUS_OBF; 9624e8264b7SAndrew Jeffery if (negotiated > 0) { 9634e8264b7SAndrew Jeffery astlpc_prinfo(astlpc, "Negotiated binding version %" PRIu16, 9644e8264b7SAndrew Jeffery negotiated); 9654e8264b7SAndrew Jeffery status |= KCS_STATUS_CHANNEL_ACTIVE; 9664e8264b7SAndrew Jeffery } else { 96788412be4SAndrew Jeffery astlpc_prerr(astlpc, "Failed to initialise channel"); 9684e8264b7SAndrew Jeffery } 969672c8852SJeremy Kerr 9704e8264b7SAndrew Jeffery mctp_astlpc_kcs_set_status(astlpc, status); 9714e8264b7SAndrew Jeffery 9724e8264b7SAndrew Jeffery mctp_binding_set_tx_enabled(&astlpc->binding, 9734e8264b7SAndrew Jeffery status & KCS_STATUS_CHANNEL_ACTIVE); 974672c8852SJeremy Kerr } 975672c8852SJeremy Kerr 976672c8852SJeremy Kerr static void mctp_astlpc_rx_start(struct mctp_binding_astlpc *astlpc) 977672c8852SJeremy Kerr { 978672c8852SJeremy Kerr struct mctp_pktbuf *pkt; 979eba19a3bSAndrew Jeffery uint32_t body, packet; 980672c8852SJeremy Kerr 981eba19a3bSAndrew Jeffery mctp_astlpc_lpc_read(astlpc, &body, astlpc->layout.rx.offset, 982eba19a3bSAndrew Jeffery sizeof(body)); 983eba19a3bSAndrew Jeffery body = be32toh(body); 984bc53d35aSJeremy Kerr 985eba19a3bSAndrew Jeffery if (body > astlpc->proto->body_size(astlpc->layout.rx.size)) { 986eba19a3bSAndrew Jeffery astlpc_prwarn(astlpc, "invalid RX len 0x%x", body); 987672c8852SJeremy Kerr return; 988672c8852SJeremy Kerr } 989672c8852SJeremy Kerr 990b93b6112SAndrew Jeffery assert(astlpc->binding.pkt_size >= 0); 991eba19a3bSAndrew Jeffery if (body > (uint32_t)astlpc->binding.pkt_size) { 992eba19a3bSAndrew Jeffery astlpc_prwarn(astlpc, "invalid RX len 0x%x", body); 993672c8852SJeremy Kerr return; 994672c8852SJeremy Kerr } 995672c8852SJeremy Kerr 996eba19a3bSAndrew Jeffery /* Eliminate the medium-specific header that we just read */ 997eba19a3bSAndrew Jeffery packet = astlpc->proto->packet_size(body) - 4; 998eba19a3bSAndrew Jeffery pkt = mctp_pktbuf_alloc(&astlpc->binding, packet); 999ae59f4feSChristian Geddes if (!pkt) { 1000ae59f4feSChristian Geddes astlpc_prwarn(astlpc, "unable to allocate pktbuf len 0x%x", packet); 1001ae59f4feSChristian Geddes return; 1002ae59f4feSChristian Geddes } 1003672c8852SJeremy Kerr 1004eba19a3bSAndrew Jeffery /* 1005eba19a3bSAndrew Jeffery * Read payload and medium-specific trailer from immediately after the 1006eba19a3bSAndrew Jeffery * medium-specific header. 1007eba19a3bSAndrew Jeffery */ 100855fb90beSAndrew Jeffery mctp_astlpc_lpc_read(astlpc, mctp_pktbuf_hdr(pkt), 1009eba19a3bSAndrew Jeffery astlpc->layout.rx.offset + 4, packet); 1010672c8852SJeremy Kerr 1011fe763e98SAndrew Jeffery astlpc->layout.rx.state = buffer_state_prepared; 1012fe763e98SAndrew Jeffery 1013ae59f4feSChristian Geddes /* Inform the other side of the MCTP interface that we have read 1014ae59f4feSChristian Geddes * the packet off the bus before handling the contents of the packet. 1015ae59f4feSChristian Geddes */ 1016fe763e98SAndrew Jeffery if (!mctp_astlpc_kcs_send(astlpc, 0x2)) 1017fe763e98SAndrew Jeffery astlpc->layout.rx.state = buffer_state_released; 1018ae59f4feSChristian Geddes 1019eba19a3bSAndrew Jeffery /* 1020eba19a3bSAndrew Jeffery * v3 will validate the CRC32 in the medium-specific trailer and adjust 1021eba19a3bSAndrew Jeffery * the packet size accordingly. On older protocols validation is a no-op 1022eba19a3bSAndrew Jeffery * that always returns true. 1023eba19a3bSAndrew Jeffery */ 1024eba19a3bSAndrew Jeffery if (astlpc->proto->pktbuf_validate(pkt)) { 1025672c8852SJeremy Kerr mctp_bus_rx(&astlpc->binding, pkt); 1026eba19a3bSAndrew Jeffery } else { 1027eba19a3bSAndrew Jeffery /* TODO: Drop any associated assembly */ 1028eba19a3bSAndrew Jeffery mctp_pktbuf_free(pkt); 1029eba19a3bSAndrew Jeffery astlpc_prdebug(astlpc, "Dropped corrupt packet"); 1030eba19a3bSAndrew Jeffery } 1031672c8852SJeremy Kerr } 1032672c8852SJeremy Kerr 1033672c8852SJeremy Kerr static void mctp_astlpc_tx_complete(struct mctp_binding_astlpc *astlpc) 1034672c8852SJeremy Kerr { 1035fe763e98SAndrew Jeffery astlpc->layout.tx.state = buffer_state_acquired; 1036672c8852SJeremy Kerr mctp_binding_set_tx_enabled(&astlpc->binding, true); 1037672c8852SJeremy Kerr } 1038672c8852SJeremy Kerr 10394e8264b7SAndrew Jeffery static int mctp_astlpc_finalise_channel(struct mctp_binding_astlpc *astlpc) 10404e8264b7SAndrew Jeffery { 10413a540664SAndrew Jeffery struct mctp_astlpc_layout layout; 10424e8264b7SAndrew Jeffery uint16_t negotiated; 10434e8264b7SAndrew Jeffery int rc; 10444e8264b7SAndrew Jeffery 10454e8264b7SAndrew Jeffery rc = mctp_astlpc_lpc_read(astlpc, &negotiated, 10464e8264b7SAndrew Jeffery offsetof(struct mctp_lpcmap_hdr, 10474e8264b7SAndrew Jeffery negotiated_ver), 10484e8264b7SAndrew Jeffery sizeof(negotiated)); 10494e8264b7SAndrew Jeffery if (rc < 0) 10504e8264b7SAndrew Jeffery return rc; 10514e8264b7SAndrew Jeffery 10524e8264b7SAndrew Jeffery negotiated = be16toh(negotiated); 105388412be4SAndrew Jeffery astlpc_prerr(astlpc, "Version negotiation got: %u", negotiated); 10544e8264b7SAndrew Jeffery 10554e8264b7SAndrew Jeffery if (negotiated == ASTLPC_VER_BAD || negotiated < ASTLPC_VER_MIN || 10564e8264b7SAndrew Jeffery negotiated > ASTLPC_VER_CUR) { 10574e8264b7SAndrew Jeffery astlpc_prerr(astlpc, "Failed to negotiate version, got: %u\n", 10584e8264b7SAndrew Jeffery negotiated); 10594e8264b7SAndrew Jeffery return -EINVAL; 10604e8264b7SAndrew Jeffery } 10614e8264b7SAndrew Jeffery 106288412be4SAndrew Jeffery assert(negotiated < ARRAY_SIZE(astlpc_protocol_version)); 106388412be4SAndrew Jeffery astlpc->proto = &astlpc_protocol_version[negotiated]; 10644e8264b7SAndrew Jeffery 10653a540664SAndrew Jeffery rc = mctp_astlpc_layout_read(astlpc, &layout); 10663a540664SAndrew Jeffery if (rc < 0) 10673a540664SAndrew Jeffery return rc; 10683a540664SAndrew Jeffery 106988412be4SAndrew Jeffery if (!mctp_astlpc_layout_validate(astlpc, &layout)) { 10703a540664SAndrew Jeffery mctp_prerr("BMC proposed invalid buffer parameters"); 10713a540664SAndrew Jeffery return -EINVAL; 10723a540664SAndrew Jeffery } 10733a540664SAndrew Jeffery 10743a540664SAndrew Jeffery astlpc->layout = layout; 10753a540664SAndrew Jeffery 10763a540664SAndrew Jeffery if (negotiated >= 2) 10773a540664SAndrew Jeffery astlpc->binding.pkt_size = 107888412be4SAndrew Jeffery astlpc->proto->body_size(astlpc->layout.tx.size); 10793a540664SAndrew Jeffery 1080fe763e98SAndrew Jeffery /* Track buffer ownership */ 1081fe763e98SAndrew Jeffery astlpc->layout.tx.state = buffer_state_acquired; 1082fe763e98SAndrew Jeffery astlpc->layout.rx.state = buffer_state_released; 1083fe763e98SAndrew Jeffery 10844e8264b7SAndrew Jeffery return 0; 10854e8264b7SAndrew Jeffery } 10864e8264b7SAndrew Jeffery 10877cd72f14SAndrew Jeffery static int mctp_astlpc_update_channel(struct mctp_binding_astlpc *astlpc, 10887cd72f14SAndrew Jeffery uint8_t status) 10897cd72f14SAndrew Jeffery { 10907cd72f14SAndrew Jeffery uint8_t updated; 10917cd72f14SAndrew Jeffery int rc = 0; 10927cd72f14SAndrew Jeffery 10937cd72f14SAndrew Jeffery assert(astlpc->mode == MCTP_BINDING_ASTLPC_MODE_HOST); 10947cd72f14SAndrew Jeffery 10957cd72f14SAndrew Jeffery updated = astlpc->kcs_status ^ status; 10967cd72f14SAndrew Jeffery 1097d0f5da0dSAndrew Jeffery astlpc_prdebug(astlpc, "%s: status: 0x%x, update: 0x%x", __func__, 1098d0f5da0dSAndrew Jeffery status, updated); 1099d0f5da0dSAndrew Jeffery 11007cd72f14SAndrew Jeffery if (updated & KCS_STATUS_BMC_READY) { 1101d0f5da0dSAndrew Jeffery if (status & KCS_STATUS_BMC_READY) { 1102d0f5da0dSAndrew Jeffery astlpc->kcs_status = status; 1103d0f5da0dSAndrew Jeffery return astlpc->binding.start(&astlpc->binding); 1104d0f5da0dSAndrew Jeffery } else { 1105fe763e98SAndrew Jeffery /* Shut down the channel */ 1106fe763e98SAndrew Jeffery astlpc->layout.rx.state = buffer_state_idle; 1107fe763e98SAndrew Jeffery astlpc->layout.tx.state = buffer_state_idle; 11087cd72f14SAndrew Jeffery mctp_binding_set_tx_enabled(&astlpc->binding, false); 11097cd72f14SAndrew Jeffery } 1110d0f5da0dSAndrew Jeffery } 11117cd72f14SAndrew Jeffery 111288412be4SAndrew Jeffery if (astlpc->proto->version == 0 || 111388412be4SAndrew Jeffery updated & KCS_STATUS_CHANNEL_ACTIVE) { 11144e8264b7SAndrew Jeffery bool enable; 11154e8264b7SAndrew Jeffery 1116fe763e98SAndrew Jeffery astlpc->layout.rx.state = buffer_state_idle; 1117fe763e98SAndrew Jeffery astlpc->layout.tx.state = buffer_state_idle; 11184e8264b7SAndrew Jeffery rc = mctp_astlpc_finalise_channel(astlpc); 11194e8264b7SAndrew Jeffery enable = (status & KCS_STATUS_CHANNEL_ACTIVE) && rc == 0; 11204e8264b7SAndrew Jeffery mctp_binding_set_tx_enabled(&astlpc->binding, enable); 11214e8264b7SAndrew Jeffery } 11227cd72f14SAndrew Jeffery 11237cd72f14SAndrew Jeffery astlpc->kcs_status = status; 11247cd72f14SAndrew Jeffery 11257cd72f14SAndrew Jeffery return rc; 11267cd72f14SAndrew Jeffery } 11277cd72f14SAndrew Jeffery 1128672c8852SJeremy Kerr int mctp_astlpc_poll(struct mctp_binding_astlpc *astlpc) 1129672c8852SJeremy Kerr { 1130bc53d35aSJeremy Kerr uint8_t status, data; 1131672c8852SJeremy Kerr int rc; 1132672c8852SJeremy Kerr 1133fe763e98SAndrew Jeffery if (astlpc->layout.rx.state == buffer_state_prepared) 1134fe763e98SAndrew Jeffery if (!mctp_astlpc_kcs_send(astlpc, 0x2)) 1135fe763e98SAndrew Jeffery astlpc->layout.rx.state = buffer_state_released; 1136fe763e98SAndrew Jeffery 1137fe763e98SAndrew Jeffery if (astlpc->layout.tx.state == buffer_state_prepared) 1138fe763e98SAndrew Jeffery if (!mctp_astlpc_kcs_send(astlpc, 0x1)) 1139fe763e98SAndrew Jeffery astlpc->layout.tx.state = buffer_state_released; 1140fe763e98SAndrew Jeffery 1141f13cb971SAndrew Jeffery rc = mctp_astlpc_kcs_read(astlpc, MCTP_ASTLPC_KCS_REG_STATUS, &status); 1142bc53d35aSJeremy Kerr if (rc) { 11439101a2abSAndrew Jeffery astlpc_prwarn(astlpc, "KCS read error"); 1144672c8852SJeremy Kerr return -1; 1145672c8852SJeremy Kerr } 1146672c8852SJeremy Kerr 11479101a2abSAndrew Jeffery astlpc_prdebug(astlpc, "%s: status: 0x%hhx", __func__, status); 1148edfe383fSAndrew Jeffery 11497cd72f14SAndrew Jeffery if (!mctp_astlpc_kcs_read_ready(astlpc, status)) 1150672c8852SJeremy Kerr return 0; 1151672c8852SJeremy Kerr 1152f13cb971SAndrew Jeffery rc = mctp_astlpc_kcs_read(astlpc, MCTP_ASTLPC_KCS_REG_DATA, &data); 1153bc53d35aSJeremy Kerr if (rc) { 11549101a2abSAndrew Jeffery astlpc_prwarn(astlpc, "KCS data read error"); 1155bc53d35aSJeremy Kerr return -1; 1156bc53d35aSJeremy Kerr } 1157bc53d35aSJeremy Kerr 11589101a2abSAndrew Jeffery astlpc_prdebug(astlpc, "%s: data: 0x%hhx", __func__, data); 1159edfe383fSAndrew Jeffery 116088412be4SAndrew Jeffery if (!astlpc->proto->version && !(data == 0x0 || data == 0xff)) { 1161afcb701aSAndrew Jeffery astlpc_prwarn(astlpc, "Invalid message for binding state: 0x%x", 1162afcb701aSAndrew Jeffery data); 1163afcb701aSAndrew Jeffery return 0; 1164afcb701aSAndrew Jeffery } 1165afcb701aSAndrew Jeffery 1166672c8852SJeremy Kerr switch (data) { 1167672c8852SJeremy Kerr case 0x0: 1168672c8852SJeremy Kerr mctp_astlpc_init_channel(astlpc); 1169672c8852SJeremy Kerr break; 1170672c8852SJeremy Kerr case 0x1: 1171fe763e98SAndrew Jeffery if (astlpc->layout.rx.state != buffer_state_released) { 1172fe763e98SAndrew Jeffery astlpc_prerr(astlpc, 1173fe763e98SAndrew Jeffery "Protocol error: Invalid Rx buffer state for event %d: %d\n", 1174fe763e98SAndrew Jeffery data, astlpc->layout.rx.state); 1175fe763e98SAndrew Jeffery return 0; 1176fe763e98SAndrew Jeffery } 1177672c8852SJeremy Kerr mctp_astlpc_rx_start(astlpc); 1178672c8852SJeremy Kerr break; 1179672c8852SJeremy Kerr case 0x2: 1180fe763e98SAndrew Jeffery if (astlpc->layout.tx.state != buffer_state_released) { 1181fe763e98SAndrew Jeffery astlpc_prerr(astlpc, 1182fe763e98SAndrew Jeffery "Protocol error: Invalid Tx buffer state for event %d: %d\n", 1183fe763e98SAndrew Jeffery data, astlpc->layout.tx.state); 1184fe763e98SAndrew Jeffery return 0; 1185fe763e98SAndrew Jeffery } 1186672c8852SJeremy Kerr mctp_astlpc_tx_complete(astlpc); 1187672c8852SJeremy Kerr break; 11881a4b55acSJeremy Kerr case 0xff: 11897cd72f14SAndrew Jeffery /* No responsibilities for the BMC on 0xff */ 1190d0f5da0dSAndrew Jeffery if (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_HOST) { 1191d0f5da0dSAndrew Jeffery rc = mctp_astlpc_update_channel(astlpc, status); 1192d0f5da0dSAndrew Jeffery if (rc < 0) 1193d0f5da0dSAndrew Jeffery return rc; 1194d0f5da0dSAndrew Jeffery } 1195d0f5da0dSAndrew Jeffery break; 1196672c8852SJeremy Kerr default: 11979101a2abSAndrew Jeffery astlpc_prwarn(astlpc, "unknown message 0x%x", data); 1198672c8852SJeremy Kerr } 1199d0f5da0dSAndrew Jeffery 1200d0f5da0dSAndrew Jeffery /* Handle silent loss of bmc-ready */ 1201d0f5da0dSAndrew Jeffery if (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_HOST) { 1202d0f5da0dSAndrew Jeffery if (!(status & KCS_STATUS_BMC_READY && data == 0xff)) 1203d0f5da0dSAndrew Jeffery return mctp_astlpc_update_channel(astlpc, status); 1204d0f5da0dSAndrew Jeffery } 1205d0f5da0dSAndrew Jeffery 1206d0f5da0dSAndrew Jeffery return rc; 1207672c8852SJeremy Kerr } 1208672c8852SJeremy Kerr 1209bc53d35aSJeremy Kerr /* allocate and basic initialisation */ 12107cd72f14SAndrew Jeffery static struct mctp_binding_astlpc *__mctp_astlpc_init(uint8_t mode, 12117cd72f14SAndrew Jeffery uint32_t mtu) 1212bc53d35aSJeremy Kerr { 1213bc53d35aSJeremy Kerr struct mctp_binding_astlpc *astlpc; 1214bc53d35aSJeremy Kerr 12157cd72f14SAndrew Jeffery assert((mode == MCTP_BINDING_ASTLPC_MODE_BMC) || 12167cd72f14SAndrew Jeffery (mode == MCTP_BINDING_ASTLPC_MODE_HOST)); 12177cd72f14SAndrew Jeffery 1218bc53d35aSJeremy Kerr astlpc = __mctp_alloc(sizeof(*astlpc)); 12197cd72f14SAndrew Jeffery if (!astlpc) 12207cd72f14SAndrew Jeffery return NULL; 12217cd72f14SAndrew Jeffery 1222bc53d35aSJeremy Kerr memset(astlpc, 0, sizeof(*astlpc)); 12237cd72f14SAndrew Jeffery astlpc->mode = mode; 12247cd72f14SAndrew Jeffery astlpc->lpc_map = NULL; 1225fe763e98SAndrew Jeffery astlpc->layout.rx.state = buffer_state_idle; 1226fe763e98SAndrew Jeffery astlpc->layout.tx.state = buffer_state_idle; 1227a9368980SAndrew Jeffery astlpc->requested_mtu = mtu; 1228bc53d35aSJeremy Kerr astlpc->binding.name = "astlpc"; 1229bc53d35aSJeremy Kerr astlpc->binding.version = 1; 12301a4f441aSAndrew Jeffery astlpc->binding.pkt_size = 12311a4f441aSAndrew Jeffery MCTP_PACKET_SIZE(mtu > MCTP_BTU ? mtu : MCTP_BTU); 1232eba19a3bSAndrew Jeffery astlpc->binding.pkt_header = 4; 1233eba19a3bSAndrew Jeffery astlpc->binding.pkt_trailer = 4; 12347cd72f14SAndrew Jeffery astlpc->binding.tx = mctp_binding_astlpc_tx; 12357cd72f14SAndrew Jeffery if (mode == MCTP_BINDING_ASTLPC_MODE_BMC) 12367cd72f14SAndrew Jeffery astlpc->binding.start = mctp_binding_astlpc_start_bmc; 12377cd72f14SAndrew Jeffery else if (mode == MCTP_BINDING_ASTLPC_MODE_HOST) 12387cd72f14SAndrew Jeffery astlpc->binding.start = mctp_binding_astlpc_start_host; 12397cd72f14SAndrew Jeffery else { 12409101a2abSAndrew Jeffery astlpc_prerr(astlpc, "%s: Invalid mode: %d\n", __func__, mode); 12417cd72f14SAndrew Jeffery __mctp_free(astlpc); 12427cd72f14SAndrew Jeffery return NULL; 12437cd72f14SAndrew Jeffery } 1244bc53d35aSJeremy Kerr 1245bc53d35aSJeremy Kerr return astlpc; 1246bc53d35aSJeremy Kerr } 1247bc53d35aSJeremy Kerr 12483b36d17cSJeremy Kerr struct mctp_binding *mctp_binding_astlpc_core(struct mctp_binding_astlpc *b) 12493b36d17cSJeremy Kerr { 12503b36d17cSJeremy Kerr return &b->binding; 12513b36d17cSJeremy Kerr } 12523b36d17cSJeremy Kerr 12537cd72f14SAndrew Jeffery struct mctp_binding_astlpc * 12547cd72f14SAndrew Jeffery mctp_astlpc_init(uint8_t mode, uint32_t mtu, void *lpc_map, 12557cd72f14SAndrew Jeffery const struct mctp_binding_astlpc_ops *ops, void *ops_data) 1256bc53d35aSJeremy Kerr { 1257bc53d35aSJeremy Kerr struct mctp_binding_astlpc *astlpc; 1258bc53d35aSJeremy Kerr 12597cd72f14SAndrew Jeffery if (!(mode == MCTP_BINDING_ASTLPC_MODE_BMC || 12607cd72f14SAndrew Jeffery mode == MCTP_BINDING_ASTLPC_MODE_HOST)) { 12617cd72f14SAndrew Jeffery mctp_prerr("Unknown binding mode: %u", mode); 12627cd72f14SAndrew Jeffery return NULL; 12637cd72f14SAndrew Jeffery } 12647cd72f14SAndrew Jeffery 12657cd72f14SAndrew Jeffery astlpc = __mctp_astlpc_init(mode, mtu); 1266bc53d35aSJeremy Kerr if (!astlpc) 1267bc53d35aSJeremy Kerr return NULL; 1268bc53d35aSJeremy Kerr 1269bc53d35aSJeremy Kerr memcpy(&astlpc->ops, ops, sizeof(astlpc->ops)); 1270bc53d35aSJeremy Kerr astlpc->ops_data = ops_data; 1271bc53d35aSJeremy Kerr astlpc->lpc_map = lpc_map; 12727cd72f14SAndrew Jeffery astlpc->mode = mode; 1273bc53d35aSJeremy Kerr 1274bc53d35aSJeremy Kerr return astlpc; 1275bc53d35aSJeremy Kerr } 1276bc53d35aSJeremy Kerr 12777cd72f14SAndrew Jeffery struct mctp_binding_astlpc * 12787cd72f14SAndrew Jeffery mctp_astlpc_init_ops(const struct mctp_binding_astlpc_ops *ops, void *ops_data, 12797cd72f14SAndrew Jeffery void *lpc_map) 12807cd72f14SAndrew Jeffery { 12817cd72f14SAndrew Jeffery return mctp_astlpc_init(MCTP_BINDING_ASTLPC_MODE_BMC, MCTP_BTU, lpc_map, 12827cd72f14SAndrew Jeffery ops, ops_data); 12837cd72f14SAndrew Jeffery } 12847cd72f14SAndrew Jeffery 12854663f67cSAndrew Jeffery void mctp_astlpc_destroy(struct mctp_binding_astlpc *astlpc) 12864663f67cSAndrew Jeffery { 1287d0f5da0dSAndrew Jeffery /* Clear channel-active and bmc-ready */ 1288d0f5da0dSAndrew Jeffery if (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_BMC) 12897c4509aaSAndrew Jeffery mctp_astlpc_kcs_set_status(astlpc, 0); 12904663f67cSAndrew Jeffery __mctp_free(astlpc); 12914663f67cSAndrew Jeffery } 12924663f67cSAndrew Jeffery 1293b214c643SJeremy Kerr #ifdef MCTP_HAVE_FILEIO 129455fb90beSAndrew Jeffery 1295bc53d35aSJeremy Kerr static int mctp_astlpc_init_fileio_lpc(struct mctp_binding_astlpc *astlpc) 1296672c8852SJeremy Kerr { 1297672c8852SJeremy Kerr struct aspeed_lpc_ctrl_mapping map = { 1298672c8852SJeremy Kerr .window_type = ASPEED_LPC_CTRL_WINDOW_MEMORY, 1299672c8852SJeremy Kerr .window_id = 0, /* There's only one */ 1300672c8852SJeremy Kerr .flags = 0, 1301672c8852SJeremy Kerr .addr = 0, 1302672c8852SJeremy Kerr .offset = 0, 1303672c8852SJeremy Kerr .size = 0 1304672c8852SJeremy Kerr }; 1305979c6a16SAndrew Jeffery void *lpc_map_base; 1306672c8852SJeremy Kerr int fd, rc; 1307672c8852SJeremy Kerr 1308672c8852SJeremy Kerr fd = open(lpc_path, O_RDWR | O_SYNC); 1309672c8852SJeremy Kerr if (fd < 0) { 13109101a2abSAndrew Jeffery astlpc_prwarn(astlpc, "LPC open (%s) failed", lpc_path); 1311672c8852SJeremy Kerr return -1; 1312672c8852SJeremy Kerr } 1313672c8852SJeremy Kerr 1314672c8852SJeremy Kerr rc = ioctl(fd, ASPEED_LPC_CTRL_IOCTL_GET_SIZE, &map); 1315672c8852SJeremy Kerr if (rc) { 13169101a2abSAndrew Jeffery astlpc_prwarn(astlpc, "LPC GET_SIZE failed"); 1317672c8852SJeremy Kerr close(fd); 1318672c8852SJeremy Kerr return -1; 1319672c8852SJeremy Kerr } 1320672c8852SJeremy Kerr 1321c1d5c543SAndrew Jeffery /* 1322c1d5c543SAndrew Jeffery * 1323c1d5c543SAndrew Jeffery * 1324c1d5c543SAndrew Jeffery * Decouple ourselves from hiomapd[1] (another user of the FW2AHB) by 1325c1d5c543SAndrew Jeffery * mapping the FW2AHB to the reserved memory here as well. 1326c1d5c543SAndrew Jeffery * 1327c1d5c543SAndrew Jeffery * It's not possible to use the MCTP ASTLPC binding on machines that 1328c1d5c543SAndrew Jeffery * need the FW2AHB bridge mapped anywhere except to the reserved memory 1329c1d5c543SAndrew Jeffery * (e.g. the host SPI NOR). 1330c1d5c543SAndrew Jeffery * 1331c1d5c543SAndrew Jeffery * [1] https://github.com/openbmc/hiomapd/ 1332c1d5c543SAndrew Jeffery * 1333c1d5c543SAndrew Jeffery * 1334c1d5c543SAndrew Jeffery * 1335c1d5c543SAndrew Jeffery * The following calculation must align with what's going on in 1336c1d5c543SAndrew Jeffery * hiomapd's lpc.c so as not to disrupt its behaviour: 1337c1d5c543SAndrew Jeffery * 1338c1d5c543SAndrew Jeffery * https://github.com/openbmc/hiomapd/blob/5ff50e3cbd7702aefc185264e4adfb9952040575/lpc.c#L68 1339c1d5c543SAndrew Jeffery * 1340c1d5c543SAndrew Jeffery * 1341c1d5c543SAndrew Jeffery */ 1342c1d5c543SAndrew Jeffery 1343c1d5c543SAndrew Jeffery /* Map the reserved memory at the top of the 28-bit LPC firmware address space */ 1344c1d5c543SAndrew Jeffery map.addr = 0x0FFFFFFF & -map.size; 1345c1d5c543SAndrew Jeffery astlpc_prinfo(astlpc, 1346c1d5c543SAndrew Jeffery "Configuring FW2AHB to map reserved memory at 0x%08x for 0x%x in the LPC FW cycle address-space", 1347c1d5c543SAndrew Jeffery map.addr, map.size); 1348c1d5c543SAndrew Jeffery 1349c1d5c543SAndrew Jeffery rc = ioctl(fd, ASPEED_LPC_CTRL_IOCTL_MAP, &map); 1350c1d5c543SAndrew Jeffery if (rc) { 1351c1d5c543SAndrew Jeffery astlpc_prwarn(astlpc, "Failed to map FW2AHB to reserved memory"); 1352c1d5c543SAndrew Jeffery close(fd); 1353c1d5c543SAndrew Jeffery return -1; 1354c1d5c543SAndrew Jeffery } 1355c1d5c543SAndrew Jeffery 1356c1d5c543SAndrew Jeffery /* Map the reserved memory into our address space */ 1357979c6a16SAndrew Jeffery lpc_map_base = 1358979c6a16SAndrew Jeffery mmap(NULL, map.size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); 1359979c6a16SAndrew Jeffery if (lpc_map_base == MAP_FAILED) { 13609101a2abSAndrew Jeffery astlpc_prwarn(astlpc, "LPC mmap failed"); 1361672c8852SJeremy Kerr rc = -1; 1362672c8852SJeremy Kerr } else { 1363979c6a16SAndrew Jeffery astlpc->lpc_map = lpc_map_base + map.size - LPC_WIN_SIZE; 1364672c8852SJeremy Kerr } 1365672c8852SJeremy Kerr 1366672c8852SJeremy Kerr close(fd); 1367672c8852SJeremy Kerr 1368672c8852SJeremy Kerr return rc; 1369672c8852SJeremy Kerr } 1370672c8852SJeremy Kerr 1371bc53d35aSJeremy Kerr static int mctp_astlpc_init_fileio_kcs(struct mctp_binding_astlpc *astlpc) 1372672c8852SJeremy Kerr { 1373672c8852SJeremy Kerr astlpc->kcs_fd = open(kcs_path, O_RDWR); 1374672c8852SJeremy Kerr if (astlpc->kcs_fd < 0) 1375672c8852SJeremy Kerr return -1; 1376672c8852SJeremy Kerr 1377672c8852SJeremy Kerr return 0; 1378672c8852SJeremy Kerr } 1379672c8852SJeremy Kerr 1380bc53d35aSJeremy Kerr static int __mctp_astlpc_fileio_kcs_read(void *arg, 1381bc53d35aSJeremy Kerr enum mctp_binding_astlpc_kcs_reg reg, uint8_t *val) 1382bc53d35aSJeremy Kerr { 1383bc53d35aSJeremy Kerr struct mctp_binding_astlpc *astlpc = arg; 1384bc53d35aSJeremy Kerr off_t offset = reg; 1385bc53d35aSJeremy Kerr int rc; 1386bc53d35aSJeremy Kerr 1387bc53d35aSJeremy Kerr rc = pread(astlpc->kcs_fd, val, 1, offset); 1388bc53d35aSJeremy Kerr 1389bc53d35aSJeremy Kerr return rc == 1 ? 0 : -1; 1390bc53d35aSJeremy Kerr } 1391bc53d35aSJeremy Kerr 1392bc53d35aSJeremy Kerr static int __mctp_astlpc_fileio_kcs_write(void *arg, 1393bc53d35aSJeremy Kerr enum mctp_binding_astlpc_kcs_reg reg, uint8_t val) 1394bc53d35aSJeremy Kerr { 1395bc53d35aSJeremy Kerr struct mctp_binding_astlpc *astlpc = arg; 1396bc53d35aSJeremy Kerr off_t offset = reg; 1397bc53d35aSJeremy Kerr int rc; 1398bc53d35aSJeremy Kerr 1399bc53d35aSJeremy Kerr rc = pwrite(astlpc->kcs_fd, &val, 1, offset); 1400bc53d35aSJeremy Kerr 1401bc53d35aSJeremy Kerr return rc == 1 ? 0 : -1; 1402bc53d35aSJeremy Kerr } 1403bc53d35aSJeremy Kerr 14041111c6a5SAndrew Jeffery int mctp_astlpc_init_pollfd(struct mctp_binding_astlpc *astlpc, 14051111c6a5SAndrew Jeffery struct pollfd *pollfd) 14061111c6a5SAndrew Jeffery { 1407fe763e98SAndrew Jeffery bool release; 1408fe763e98SAndrew Jeffery 14091111c6a5SAndrew Jeffery pollfd->fd = astlpc->kcs_fd; 1410fe763e98SAndrew Jeffery pollfd->events = 0; 1411fe763e98SAndrew Jeffery 1412fe763e98SAndrew Jeffery release = astlpc->layout.rx.state == buffer_state_prepared || 1413fe763e98SAndrew Jeffery astlpc->layout.tx.state == buffer_state_prepared; 1414fe763e98SAndrew Jeffery 1415fe763e98SAndrew Jeffery pollfd->events = release ? POLLOUT : POLLIN; 14161111c6a5SAndrew Jeffery 14171111c6a5SAndrew Jeffery return 0; 14181111c6a5SAndrew Jeffery } 14191111c6a5SAndrew Jeffery 1420bc53d35aSJeremy Kerr struct mctp_binding_astlpc *mctp_astlpc_init_fileio(void) 1421672c8852SJeremy Kerr { 1422672c8852SJeremy Kerr struct mctp_binding_astlpc *astlpc; 1423672c8852SJeremy Kerr int rc; 1424672c8852SJeremy Kerr 14257cd72f14SAndrew Jeffery /* 14267cd72f14SAndrew Jeffery * If we're doing file IO then we're very likely not running 14278877c460SAndrew Jeffery * freestanding, so lets assume that we're on the BMC side. 14288877c460SAndrew Jeffery * 14298877c460SAndrew Jeffery * Requesting an MTU of 0 requests the largest possible MTU, whatever 14308877c460SAndrew Jeffery * value that might take. 14317cd72f14SAndrew Jeffery */ 14328877c460SAndrew Jeffery astlpc = __mctp_astlpc_init(MCTP_BINDING_ASTLPC_MODE_BMC, 0); 1433bc53d35aSJeremy Kerr if (!astlpc) 1434bc53d35aSJeremy Kerr return NULL; 1435672c8852SJeremy Kerr 1436bc53d35aSJeremy Kerr /* Set internal operations for kcs. We use direct accesses to the lpc 1437bc53d35aSJeremy Kerr * map area */ 1438bc53d35aSJeremy Kerr astlpc->ops.kcs_read = __mctp_astlpc_fileio_kcs_read; 1439bc53d35aSJeremy Kerr astlpc->ops.kcs_write = __mctp_astlpc_fileio_kcs_write; 1440bc53d35aSJeremy Kerr astlpc->ops_data = astlpc; 1441bc53d35aSJeremy Kerr 1442bc53d35aSJeremy Kerr rc = mctp_astlpc_init_fileio_lpc(astlpc); 1443672c8852SJeremy Kerr if (rc) { 1444672c8852SJeremy Kerr free(astlpc); 1445672c8852SJeremy Kerr return NULL; 1446672c8852SJeremy Kerr } 1447672c8852SJeremy Kerr 1448bc53d35aSJeremy Kerr rc = mctp_astlpc_init_fileio_kcs(astlpc); 1449672c8852SJeremy Kerr if (rc) { 1450672c8852SJeremy Kerr free(astlpc); 1451672c8852SJeremy Kerr return NULL; 1452672c8852SJeremy Kerr } 1453672c8852SJeremy Kerr 1454672c8852SJeremy Kerr return astlpc; 1455672c8852SJeremy Kerr } 145692a10a6bSJeremy Kerr #else 14575b2702dbSAndrew Jeffery struct mctp_binding_astlpc *mctp_astlpc_init_fileio(void) 145892a10a6bSJeremy Kerr { 14595b2702dbSAndrew Jeffery mctp_prlog(MCTP_LOG_ERR, "%s: Missing support for file IO", __func__); 146092a10a6bSJeremy Kerr return NULL; 146192a10a6bSJeremy Kerr } 1462672c8852SJeremy Kerr 14631111c6a5SAndrew Jeffery int mctp_astlpc_init_pollfd(struct mctp_binding_astlpc *astlpc __unused, 14641111c6a5SAndrew Jeffery struct pollfd *pollfd __unused) 14651111c6a5SAndrew Jeffery { 14661111c6a5SAndrew Jeffery mctp_prlog(MCTP_LOG_ERR, "%s: Missing support for file IO", __func__); 14671111c6a5SAndrew Jeffery return -1; 14681111c6a5SAndrew Jeffery } 146992a10a6bSJeremy Kerr #endif 1470