Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 1 | /* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */ |
| 2 | |
Andrew Jeffery | 2cda40f | 2020-02-28 15:26:20 +1030 | [diff] [blame] | 3 | #ifdef HAVE_CONFIG_H |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 4 | #include "config.h" |
Andrew Jeffery | 2cda40f | 2020-02-28 15:26:20 +1030 | [diff] [blame] | 5 | #endif |
| 6 | |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 7 | #include "libmctp-astlpc.h" |
| 8 | #include "libmctp-log.h" |
| 9 | |
| 10 | #ifdef NDEBUG |
| 11 | #undef NDEBUG |
| 12 | #endif |
| 13 | |
| 14 | #define RX_BUFFER_DATA 0x100 + 4 + 4 |
| 15 | |
| 16 | #include <assert.h> |
| 17 | #include <stdint.h> |
| 18 | #include <stdio.h> |
| 19 | #include <stdlib.h> |
| 20 | #include <string.h> |
| 21 | |
| 22 | /* Hack: Needs to be in sync with astlpc.c */ |
| 23 | struct mctp_binding_astlpc { |
| 24 | struct mctp_binding binding; |
| 25 | |
| 26 | union { |
| 27 | void *lpc_map; |
| 28 | struct mctp_lpcmap_hdr *lpc_hdr; |
| 29 | }; |
| 30 | |
| 31 | /* direct ops data */ |
| 32 | struct mctp_binding_astlpc_ops ops; |
| 33 | void *ops_data; |
| 34 | struct mctp_lpcmap_hdr *priv_hdr; |
| 35 | |
| 36 | /* fileio ops data */ |
| 37 | void *lpc_map_base; |
| 38 | int kcs_fd; |
| 39 | uint8_t kcs_status; |
| 40 | |
| 41 | bool running; |
| 42 | |
| 43 | /* temporary transmit buffer */ |
| 44 | uint8_t txbuf[256]; |
| 45 | }; |
| 46 | |
| 47 | #define KCS_STATUS_BMC_READY 0x80 |
| 48 | #define KCS_STATUS_CHANNEL_ACTIVE 0x40 |
| 49 | #define KCS_STATUS_IBF 0x02 |
| 50 | #define KCS_STATUS_OBF 0x01 |
| 51 | |
| 52 | struct mctp_binding_astlpc_mmio { |
| 53 | struct mctp_binding_astlpc astlpc; |
| 54 | |
| 55 | uint8_t kcs[2]; |
| 56 | |
| 57 | size_t lpc_size; |
| 58 | uint8_t *lpc; |
| 59 | }; |
| 60 | |
| 61 | #ifndef container_of |
| 62 | #define container_of(ptr, type, member) \ |
| 63 | (type *)((char *)(ptr) - (char *)&((type *)0)->member) |
| 64 | #endif |
| 65 | |
| 66 | #define binding_to_mmio(b) \ |
| 67 | container_of(b, struct mctp_binding_astlpc_mmio, astlpc) |
| 68 | |
| 69 | int mctp_astlpc_mmio_kcs_read(void *data, enum mctp_binding_astlpc_kcs_reg reg, |
| 70 | uint8_t *val) |
| 71 | { |
| 72 | struct mctp_binding_astlpc_mmio *mmio = binding_to_mmio(data); |
| 73 | |
| 74 | *val = mmio->kcs[reg]; |
| 75 | |
| 76 | mctp_prdebug("%s: 0x%hhx from %s", __func__, *val, reg ? "status" : "data"); |
| 77 | |
| 78 | if (reg == MCTP_ASTLPC_KCS_REG_DATA) |
| 79 | mmio->kcs[MCTP_ASTLPC_KCS_REG_STATUS] &= ~KCS_STATUS_IBF; |
| 80 | |
| 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | int mctp_astlpc_mmio_kcs_write(void *data, enum mctp_binding_astlpc_kcs_reg reg, |
| 85 | uint8_t val) |
| 86 | { |
| 87 | struct mctp_binding_astlpc_mmio *mmio = binding_to_mmio(data); |
| 88 | |
| 89 | if (reg == MCTP_ASTLPC_KCS_REG_DATA) |
| 90 | mmio->kcs[MCTP_ASTLPC_KCS_REG_STATUS] |= KCS_STATUS_OBF; |
| 91 | |
| 92 | if (reg == MCTP_ASTLPC_KCS_REG_STATUS) |
| 93 | mmio->kcs[reg] = val & ~0xaU; |
| 94 | else |
| 95 | mmio->kcs[reg] = val; |
| 96 | |
| 97 | mctp_prdebug("%s: 0x%hhx to %s", __func__, val, reg ? "status" : "data"); |
| 98 | |
| 99 | return 0; |
| 100 | } |
| 101 | int mctp_astlpc_mmio_lpc_read(void *data, void *buf, off_t offset, size_t len) |
| 102 | { |
| 103 | struct mctp_binding_astlpc_mmio *mmio = binding_to_mmio(data); |
| 104 | |
| 105 | assert(offset + len < mmio->lpc_size); |
| 106 | |
| 107 | memcpy(buf, mmio->lpc + offset, len); |
| 108 | |
| 109 | mctp_prdebug("%s: %zu bytes from 0x%zx", __func__, len, offset); |
| 110 | |
| 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | int mctp_astlpc_mmio_lpc_write(void *data, void *buf, off_t offset, size_t len) |
| 115 | { |
| 116 | struct mctp_binding_astlpc_mmio *mmio = binding_to_mmio(data); |
| 117 | |
| 118 | assert(offset + len < mmio->lpc_size); |
| 119 | |
| 120 | memcpy(mmio->lpc + offset, buf, len); |
| 121 | |
| 122 | mctp_prdebug("%s: %zu bytes to 0x%zx", __func__, len, offset); |
| 123 | |
| 124 | return 0; |
| 125 | } |
| 126 | |
| 127 | static void rx_message(uint8_t eid, void *data, void *msg, size_t len) |
| 128 | { |
| 129 | uint8_t type; |
| 130 | |
| 131 | type = *(uint8_t *)msg; |
| 132 | |
| 133 | mctp_prdebug("MCTP message received: len %zd, type %d", |
| 134 | len, type); |
| 135 | } |
| 136 | |
| 137 | const struct mctp_binding_astlpc_ops mctp_binding_astlpc_mmio_ops = { |
| 138 | .kcs_read = mctp_astlpc_mmio_kcs_read, |
| 139 | .kcs_write = mctp_astlpc_mmio_kcs_write, |
| 140 | .lpc_read = mctp_astlpc_mmio_lpc_read, |
| 141 | .lpc_write = mctp_astlpc_mmio_lpc_write, |
| 142 | }; |
| 143 | |
| 144 | int main(void) |
| 145 | { |
| 146 | struct mctp_binding_astlpc_mmio mmio; |
| 147 | struct mctp_binding_astlpc *astlpc; |
| 148 | uint8_t msg[2 * MCTP_BTU]; |
| 149 | struct mctp *mctp; |
| 150 | int rc; |
| 151 | |
| 152 | memset(&msg[0], 0x5a, MCTP_BTU); |
| 153 | memset(&msg[MCTP_BTU], 0xa5, MCTP_BTU); |
| 154 | |
| 155 | mmio.lpc_size = 1 * 1024 * 1024; |
| 156 | mmio.lpc = calloc(1, mmio.lpc_size); |
| 157 | |
| 158 | mctp_set_log_stdio(MCTP_LOG_DEBUG); |
| 159 | |
| 160 | mctp = mctp_init(); |
| 161 | assert(mctp); |
| 162 | |
| 163 | mctp_set_rx_all(mctp, rx_message, NULL); |
| 164 | |
| 165 | astlpc = mctp_astlpc_init_ops(&mctp_binding_astlpc_mmio_ops, &mmio, NULL); |
| 166 | |
| 167 | mctp_register_bus(mctp, &astlpc->binding, 8); |
| 168 | |
| 169 | /* Verify the binding was initialised */ |
| 170 | assert(mmio.kcs[MCTP_ASTLPC_KCS_REG_STATUS] & KCS_STATUS_BMC_READY); |
| 171 | |
| 172 | /* Host sends channel init command */ |
| 173 | mmio.kcs[MCTP_ASTLPC_KCS_REG_STATUS] |= KCS_STATUS_IBF; |
| 174 | mmio.kcs[MCTP_ASTLPC_KCS_REG_DATA] = 0x00; |
| 175 | |
| 176 | /* Receive host init */ |
| 177 | mctp_astlpc_poll(astlpc); |
| 178 | |
| 179 | /* Host receives init response */ |
| 180 | assert(mmio.kcs[MCTP_ASTLPC_KCS_REG_STATUS] & KCS_STATUS_OBF); |
| 181 | assert(mmio.kcs[MCTP_ASTLPC_KCS_REG_STATUS] & KCS_STATUS_CHANNEL_ACTIVE); |
| 182 | |
| 183 | /* Host dequeues data */ |
| 184 | assert(mmio.kcs[MCTP_ASTLPC_KCS_REG_DATA] == 0xff); |
| 185 | mmio.kcs[MCTP_ASTLPC_KCS_REG_STATUS] &= ~KCS_STATUS_OBF; |
| 186 | |
| 187 | /* BMC sends a message */ |
| 188 | rc = mctp_message_tx(mctp, 9, msg, sizeof(msg)); |
| 189 | assert(rc == 0); |
| 190 | |
| 191 | /* Host receives a message */ |
| 192 | assert(mmio.kcs[MCTP_ASTLPC_KCS_REG_STATUS] & KCS_STATUS_OBF); |
| 193 | assert(mmio.kcs[MCTP_ASTLPC_KCS_REG_DATA] == 0x01); |
| 194 | |
| 195 | /* Verify it's the packet we expect */ |
| 196 | assert(!memcmp(mmio.lpc + RX_BUFFER_DATA, &msg[0], MCTP_BTU)); |
| 197 | |
| 198 | /* Host returns Rx area ownership to BMC */ |
| 199 | mmio.kcs[MCTP_ASTLPC_KCS_REG_STATUS] &= ~KCS_STATUS_OBF; |
| 200 | mmio.kcs[MCTP_ASTLPC_KCS_REG_DATA] = 0x02; |
| 201 | mmio.kcs[MCTP_ASTLPC_KCS_REG_STATUS] |= KCS_STATUS_IBF; |
| 202 | |
| 203 | /* BMC dequeues ownership hand-over and sends the queued packet */ |
| 204 | rc = mctp_astlpc_poll(astlpc); |
| 205 | assert(rc == 0); |
| 206 | |
| 207 | /* Host receives a message */ |
| 208 | assert(mmio.kcs[MCTP_ASTLPC_KCS_REG_STATUS] & KCS_STATUS_OBF); |
| 209 | assert(mmio.kcs[MCTP_ASTLPC_KCS_REG_DATA] == 0x01); |
| 210 | |
| 211 | /* Verify it's the packet we expect */ |
| 212 | assert(!memcmp(mmio.lpc + RX_BUFFER_DATA, &msg[MCTP_BTU], MCTP_BTU)); |
| 213 | |
| 214 | return 0; |
| 215 | } |