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 | df5f6b9 | 2020-05-12 21:39:06 +0930 | [diff] [blame] | 7 | #include "astlpc.c" |
| 8 | |
| 9 | #ifdef pr_fmt |
| 10 | #undef pr_fmt |
| 11 | #define pr_fmt(x) "test: " x |
| 12 | #endif |
| 13 | |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 14 | #include "libmctp-astlpc.h" |
| 15 | #include "libmctp-log.h" |
Przemyslaw Czarnowski | ff25d7e | 2020-03-26 11:39:37 +0100 | [diff] [blame] | 16 | #include "container_of.h" |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 17 | |
| 18 | #ifdef NDEBUG |
| 19 | #undef NDEBUG |
| 20 | #endif |
| 21 | |
| 22 | #define RX_BUFFER_DATA 0x100 + 4 + 4 |
| 23 | |
| 24 | #include <assert.h> |
| 25 | #include <stdint.h> |
| 26 | #include <stdio.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <string.h> |
| 29 | |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 30 | struct mctp_binding_astlpc_mmio { |
| 31 | struct mctp_binding_astlpc astlpc; |
Andrew Jeffery | d5e3cd7 | 2020-05-12 22:10:22 +0930 | [diff] [blame] | 32 | bool bmc; |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 33 | |
Andrew Jeffery | d5e3cd7 | 2020-05-12 22:10:22 +0930 | [diff] [blame] | 34 | uint8_t (*kcs)[2]; |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 35 | |
| 36 | size_t lpc_size; |
| 37 | uint8_t *lpc; |
| 38 | }; |
| 39 | |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 40 | #define binding_to_mmio(b) \ |
| 41 | container_of(b, struct mctp_binding_astlpc_mmio, astlpc) |
| 42 | |
Andrew Jeffery | 53ea147 | 2020-05-23 21:06:24 +0930 | [diff] [blame] | 43 | static int mctp_astlpc_mmio_kcs_read(void *data, |
| 44 | enum mctp_binding_astlpc_kcs_reg reg, |
| 45 | uint8_t *val) |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 46 | { |
| 47 | struct mctp_binding_astlpc_mmio *mmio = binding_to_mmio(data); |
| 48 | |
Andrew Jeffery | d5e3cd7 | 2020-05-12 22:10:22 +0930 | [diff] [blame] | 49 | *val = (*mmio->kcs)[reg]; |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 50 | |
| 51 | mctp_prdebug("%s: 0x%hhx from %s", __func__, *val, reg ? "status" : "data"); |
| 52 | |
Andrew Jeffery | d5e3cd7 | 2020-05-12 22:10:22 +0930 | [diff] [blame] | 53 | if (reg == MCTP_ASTLPC_KCS_REG_DATA) { |
| 54 | uint8_t flag = mmio->bmc ? KCS_STATUS_IBF : KCS_STATUS_OBF; |
| 55 | (*mmio->kcs)[MCTP_ASTLPC_KCS_REG_STATUS] &= ~flag; |
| 56 | } |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 57 | |
| 58 | return 0; |
| 59 | } |
| 60 | |
Andrew Jeffery | 53ea147 | 2020-05-23 21:06:24 +0930 | [diff] [blame] | 61 | static int mctp_astlpc_mmio_kcs_write(void *data, |
| 62 | enum mctp_binding_astlpc_kcs_reg reg, |
| 63 | uint8_t val) |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 64 | { |
| 65 | struct mctp_binding_astlpc_mmio *mmio = binding_to_mmio(data); |
Andrew Jeffery | d5e3cd7 | 2020-05-12 22:10:22 +0930 | [diff] [blame] | 66 | uint8_t *regp; |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 67 | |
Andrew Jeffery | d5e3cd7 | 2020-05-12 22:10:22 +0930 | [diff] [blame] | 68 | assert(reg == MCTP_ASTLPC_KCS_REG_DATA || |
| 69 | reg == MCTP_ASTLPC_KCS_REG_STATUS); |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 70 | |
Andrew Jeffery | d5e3cd7 | 2020-05-12 22:10:22 +0930 | [diff] [blame] | 71 | if (reg == MCTP_ASTLPC_KCS_REG_DATA) { |
| 72 | uint8_t flag = mmio->bmc ? KCS_STATUS_OBF : KCS_STATUS_IBF; |
| 73 | (*mmio->kcs)[MCTP_ASTLPC_KCS_REG_STATUS] |= flag; |
| 74 | } |
| 75 | |
| 76 | regp = &(*mmio->kcs)[reg]; |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 77 | if (reg == MCTP_ASTLPC_KCS_REG_STATUS) |
Andrew Jeffery | d5e3cd7 | 2020-05-12 22:10:22 +0930 | [diff] [blame] | 78 | *regp = (val & ~0xbU) | (val & *regp & 1); |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 79 | else |
Andrew Jeffery | d5e3cd7 | 2020-05-12 22:10:22 +0930 | [diff] [blame] | 80 | *regp = val; |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 81 | |
| 82 | mctp_prdebug("%s: 0x%hhx to %s", __func__, val, reg ? "status" : "data"); |
| 83 | |
| 84 | return 0; |
| 85 | } |
Andrew Jeffery | 06b2cd8 | 2020-03-17 23:12:27 +1030 | [diff] [blame] | 86 | int mctp_astlpc_mmio_lpc_read(void *data, void *buf, long offset, size_t len) |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 87 | { |
| 88 | struct mctp_binding_astlpc_mmio *mmio = binding_to_mmio(data); |
| 89 | |
Andrew Jeffery | 1dbf021 | 2020-05-12 13:53:50 +0930 | [diff] [blame] | 90 | mctp_prdebug("%s: %zu bytes from 0x%lx", __func__, len, offset); |
| 91 | |
Andrew Jeffery | 06b2cd8 | 2020-03-17 23:12:27 +1030 | [diff] [blame] | 92 | assert(offset >= 0L); |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 93 | assert(offset + len < mmio->lpc_size); |
| 94 | |
| 95 | memcpy(buf, mmio->lpc + offset, len); |
| 96 | |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 97 | return 0; |
| 98 | } |
| 99 | |
Andrew Jeffery | 06b2cd8 | 2020-03-17 23:12:27 +1030 | [diff] [blame] | 100 | int mctp_astlpc_mmio_lpc_write(void *data, void *buf, long offset, size_t len) |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 101 | { |
| 102 | struct mctp_binding_astlpc_mmio *mmio = binding_to_mmio(data); |
| 103 | |
Andrew Jeffery | 1dbf021 | 2020-05-12 13:53:50 +0930 | [diff] [blame] | 104 | mctp_prdebug("%s: %zu bytes to 0x%lx", __func__, len, offset); |
| 105 | |
Andrew Jeffery | 06b2cd8 | 2020-03-17 23:12:27 +1030 | [diff] [blame] | 106 | assert(offset >= 0L); |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 107 | assert(offset + len < mmio->lpc_size); |
| 108 | |
| 109 | memcpy(mmio->lpc + offset, buf, len); |
| 110 | |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 111 | return 0; |
| 112 | } |
| 113 | |
Andrew Jeffery | b93b611 | 2020-06-05 14:13:44 +0930 | [diff] [blame] | 114 | #define __unused __attribute__((unused)) |
| 115 | |
| 116 | static void rx_message(uint8_t eid __unused, void *data __unused, void *msg, |
| 117 | size_t len) |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 118 | { |
| 119 | uint8_t type; |
| 120 | |
| 121 | type = *(uint8_t *)msg; |
| 122 | |
| 123 | mctp_prdebug("MCTP message received: len %zd, type %d", |
| 124 | len, type); |
| 125 | } |
| 126 | |
Andrew Jeffery | 53ea147 | 2020-05-23 21:06:24 +0930 | [diff] [blame] | 127 | static const struct mctp_binding_astlpc_ops mctp_binding_astlpc_mmio_ops = { |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 128 | .kcs_read = mctp_astlpc_mmio_kcs_read, |
| 129 | .kcs_write = mctp_astlpc_mmio_kcs_write, |
| 130 | .lpc_read = mctp_astlpc_mmio_lpc_read, |
| 131 | .lpc_write = mctp_astlpc_mmio_lpc_write, |
| 132 | }; |
| 133 | |
Andrew Jeffery | d5e3cd7 | 2020-05-12 22:10:22 +0930 | [diff] [blame] | 134 | struct astlpc_endpoint { |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 135 | struct mctp_binding_astlpc_mmio mmio; |
| 136 | struct mctp_binding_astlpc *astlpc; |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 137 | struct mctp *mctp; |
Andrew Jeffery | d5e3cd7 | 2020-05-12 22:10:22 +0930 | [diff] [blame] | 138 | }; |
| 139 | |
| 140 | static void endpoint_init(struct astlpc_endpoint *ep, mctp_eid_t eid, |
| 141 | uint8_t mode, uint8_t (*kcs)[2], void *lpc_mem, |
| 142 | size_t lpc_size) |
| 143 | { |
| 144 | /* |
| 145 | * Configure the direction of the KCS interface so we know whether to |
| 146 | * set or clear IBF or OBF on writes or reads. |
| 147 | */ |
| 148 | ep->mmio.bmc = (mode == MCTP_BINDING_ASTLPC_MODE_BMC); |
| 149 | |
| 150 | ep->mctp = mctp_init(); |
| 151 | assert(ep->mctp); |
| 152 | |
| 153 | mctp_set_rx_all(ep->mctp, rx_message, NULL); |
| 154 | |
| 155 | /* Inject KCS registers */ |
| 156 | ep->mmio.kcs = kcs; |
| 157 | |
| 158 | /* Inject the heap allocation as the LPC mapping */ |
| 159 | ep->mmio.lpc_size = lpc_size; |
| 160 | ep->mmio.lpc = lpc_mem; |
| 161 | |
| 162 | /* Initialise the binding */ |
| 163 | ep->astlpc = mctp_astlpc_init(mode, MCTP_BTU, NULL, |
| 164 | &mctp_binding_astlpc_mmio_ops, &ep->mmio); |
| 165 | |
| 166 | mctp_register_bus(ep->mctp, &ep->astlpc->binding, eid); |
| 167 | } |
| 168 | |
| 169 | static void endpoint_destroy(struct astlpc_endpoint *ep) |
| 170 | { |
| 171 | mctp_astlpc_destroy(ep->astlpc); |
| 172 | mctp_destroy(ep->mctp); |
| 173 | } |
| 174 | |
Andrew Jeffery | e756de8 | 2020-05-22 12:30:58 +0930 | [diff] [blame] | 175 | static void astlpc_test_packetised_message_bmc_to_host(void) |
Andrew Jeffery | d5e3cd7 | 2020-05-12 22:10:22 +0930 | [diff] [blame] | 176 | { |
| 177 | uint8_t msg[2 * MCTP_BTU]; |
| 178 | struct astlpc_endpoint bmc, host; |
| 179 | size_t lpc_size; |
| 180 | uint8_t kcs[2] = { 0 }; |
| 181 | void *lpc_mem; |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 182 | int rc; |
| 183 | |
Andrew Jeffery | d5e3cd7 | 2020-05-12 22:10:22 +0930 | [diff] [blame] | 184 | /* Test harness initialisation */ |
| 185 | |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 186 | memset(&msg[0], 0x5a, MCTP_BTU); |
| 187 | memset(&msg[MCTP_BTU], 0xa5, MCTP_BTU); |
| 188 | |
Andrew Jeffery | d5e3cd7 | 2020-05-12 22:10:22 +0930 | [diff] [blame] | 189 | lpc_size = 1 * 1024 * 1024; |
| 190 | lpc_mem = calloc(1, lpc_size); |
| 191 | assert(lpc_mem); |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 192 | |
Andrew Jeffery | d5e3cd7 | 2020-05-12 22:10:22 +0930 | [diff] [blame] | 193 | /* BMC initialisation */ |
| 194 | endpoint_init(&bmc, 8, MCTP_BINDING_ASTLPC_MODE_BMC, &kcs, lpc_mem, |
| 195 | lpc_size); |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 196 | |
Andrew Jeffery | d5e3cd7 | 2020-05-12 22:10:22 +0930 | [diff] [blame] | 197 | /* Host initialisation */ |
| 198 | endpoint_init(&host, 9, MCTP_BINDING_ASTLPC_MODE_HOST, &kcs, lpc_mem, |
| 199 | lpc_size); |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 200 | |
Andrew Jeffery | d5e3cd7 | 2020-05-12 22:10:22 +0930 | [diff] [blame] | 201 | /* BMC receives host channel init request */ |
| 202 | mctp_astlpc_poll(bmc.astlpc); |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 203 | |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 204 | /* Host dequeues data */ |
Andrew Jeffery | d5e3cd7 | 2020-05-12 22:10:22 +0930 | [diff] [blame] | 205 | mctp_astlpc_poll(host.astlpc); |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 206 | |
| 207 | /* BMC sends a message */ |
Andrew Jeffery | d5e3cd7 | 2020-05-12 22:10:22 +0930 | [diff] [blame] | 208 | rc = mctp_message_tx(bmc.mctp, 9, msg, sizeof(msg)); |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 209 | assert(rc == 0); |
Andrew Jeffery | d5e3cd7 | 2020-05-12 22:10:22 +0930 | [diff] [blame] | 210 | assert(kcs[MCTP_ASTLPC_KCS_REG_STATUS] & KCS_STATUS_OBF); |
| 211 | assert(kcs[MCTP_ASTLPC_KCS_REG_DATA] == 0x01); |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 212 | |
| 213 | /* Verify it's the packet we expect */ |
Andrew Jeffery | d5e3cd7 | 2020-05-12 22:10:22 +0930 | [diff] [blame] | 214 | assert(!memcmp(lpc_mem + RX_BUFFER_DATA, &msg[0], MCTP_BTU)); |
| 215 | |
| 216 | /* Host receives a packet */ |
| 217 | mctp_astlpc_poll(host.astlpc); |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 218 | |
| 219 | /* Host returns Rx area ownership to BMC */ |
Andrew Jeffery | d5e3cd7 | 2020-05-12 22:10:22 +0930 | [diff] [blame] | 220 | assert(!(kcs[MCTP_ASTLPC_KCS_REG_STATUS] & KCS_STATUS_OBF)); |
| 221 | assert(kcs[MCTP_ASTLPC_KCS_REG_DATA] == 0x02); |
| 222 | assert(kcs[MCTP_ASTLPC_KCS_REG_STATUS] & KCS_STATUS_IBF); |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 223 | |
| 224 | /* BMC dequeues ownership hand-over and sends the queued packet */ |
Andrew Jeffery | d5e3cd7 | 2020-05-12 22:10:22 +0930 | [diff] [blame] | 225 | rc = mctp_astlpc_poll(bmc.astlpc); |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 226 | assert(rc == 0); |
| 227 | |
| 228 | /* Host receives a message */ |
Andrew Jeffery | d5e3cd7 | 2020-05-12 22:10:22 +0930 | [diff] [blame] | 229 | assert(kcs[MCTP_ASTLPC_KCS_REG_STATUS] & KCS_STATUS_OBF); |
| 230 | assert(kcs[MCTP_ASTLPC_KCS_REG_DATA] == 0x01); |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 231 | |
| 232 | /* Verify it's the packet we expect */ |
Andrew Jeffery | d5e3cd7 | 2020-05-12 22:10:22 +0930 | [diff] [blame] | 233 | assert(!memcmp(lpc_mem + RX_BUFFER_DATA, &msg[MCTP_BTU], MCTP_BTU)); |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 234 | |
Andrew Jeffery | d5e3cd7 | 2020-05-12 22:10:22 +0930 | [diff] [blame] | 235 | endpoint_destroy(&bmc); |
| 236 | endpoint_destroy(&host); |
| 237 | free(lpc_mem); |
Andrew Jeffery | e756de8 | 2020-05-22 12:30:58 +0930 | [diff] [blame] | 238 | } |
| 239 | |
Andrew Jeffery | f1a2131 | 2020-05-22 12:48:21 +0930 | [diff] [blame^] | 240 | static void astlpc_test_simple_init(void) |
| 241 | { |
| 242 | struct astlpc_endpoint bmc, host; |
| 243 | uint8_t kcs[2] = { 0 }; |
| 244 | size_t lpc_size; |
| 245 | void *lpc_mem; |
| 246 | |
| 247 | /* Test harness initialisation */ |
| 248 | lpc_size = 1 * 1024 * 1024; |
| 249 | lpc_mem = calloc(1, lpc_size); |
| 250 | assert(lpc_mem); |
| 251 | |
| 252 | /* Bus owner (BMC) initialisation */ |
| 253 | endpoint_init(&bmc, 8, MCTP_BINDING_ASTLPC_MODE_BMC, &kcs, lpc_mem, |
| 254 | lpc_size); |
| 255 | |
| 256 | /* Verify the BMC binding was initialised */ |
| 257 | assert(kcs[MCTP_ASTLPC_KCS_REG_STATUS] & KCS_STATUS_BMC_READY); |
| 258 | |
| 259 | /* Device (Host) initialisation */ |
| 260 | endpoint_init(&host, 9, MCTP_BINDING_ASTLPC_MODE_HOST, &kcs, lpc_mem, |
| 261 | lpc_size); |
| 262 | |
| 263 | /* Host sends channel init command */ |
| 264 | assert(kcs[MCTP_ASTLPC_KCS_REG_STATUS] & KCS_STATUS_IBF); |
| 265 | assert(kcs[MCTP_ASTLPC_KCS_REG_DATA] == 0x00); |
| 266 | |
| 267 | /* BMC receives host channel init request */ |
| 268 | mctp_astlpc_poll(bmc.astlpc); |
| 269 | |
| 270 | /* BMC sends init response */ |
| 271 | assert(kcs[MCTP_ASTLPC_KCS_REG_STATUS] & KCS_STATUS_OBF); |
| 272 | assert(kcs[MCTP_ASTLPC_KCS_REG_STATUS] & KCS_STATUS_CHANNEL_ACTIVE); |
| 273 | assert(kcs[MCTP_ASTLPC_KCS_REG_DATA] == 0xff); |
| 274 | |
| 275 | /* Host dequeues data */ |
| 276 | mctp_astlpc_poll(host.astlpc); |
| 277 | |
| 278 | endpoint_destroy(&bmc); |
| 279 | endpoint_destroy(&host); |
| 280 | free(lpc_mem); |
| 281 | } |
| 282 | |
Andrew Jeffery | e756de8 | 2020-05-22 12:30:58 +0930 | [diff] [blame] | 283 | int main(void) |
| 284 | { |
| 285 | mctp_set_log_stdio(MCTP_LOG_DEBUG); |
| 286 | |
Andrew Jeffery | f1a2131 | 2020-05-22 12:48:21 +0930 | [diff] [blame^] | 287 | astlpc_test_simple_init(); |
Andrew Jeffery | e756de8 | 2020-05-22 12:30:58 +0930 | [diff] [blame] | 288 | astlpc_test_packetised_message_bmc_to_host(); |
Andrew Jeffery | 11b7e92 | 2020-03-10 23:37:09 +1030 | [diff] [blame] | 289 | |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 290 | return 0; |
| 291 | } |