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 | |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 22 | #include <assert.h> |
Andrew Jeffery | 91f09ed | 2020-05-22 20:52:26 +0930 | [diff] [blame] | 23 | #include <limits.h> |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 24 | #include <stdint.h> |
| 25 | #include <stdio.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <string.h> |
| 28 | |
Andrew Jeffery | 91f09ed | 2020-05-22 20:52:26 +0930 | [diff] [blame] | 29 | #ifndef ARRAY_SIZE |
| 30 | #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) |
| 31 | #endif |
| 32 | |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 33 | struct mctp_binding_astlpc_mmio { |
| 34 | struct mctp_binding_astlpc astlpc; |
Andrew Jeffery | d5e3cd7 | 2020-05-12 22:10:22 +0930 | [diff] [blame] | 35 | bool bmc; |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 36 | |
Andrew Jeffery | d5e3cd7 | 2020-05-12 22:10:22 +0930 | [diff] [blame] | 37 | uint8_t (*kcs)[2]; |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 38 | |
| 39 | size_t lpc_size; |
| 40 | uint8_t *lpc; |
| 41 | }; |
| 42 | |
Andrew Jeffery | 5a7c2db | 2020-05-22 20:13:42 +0930 | [diff] [blame] | 43 | struct astlpc_endpoint { |
| 44 | struct mctp_binding_astlpc_mmio mmio; |
| 45 | struct mctp_binding_astlpc *astlpc; |
| 46 | struct mctp *mctp; |
| 47 | }; |
| 48 | |
| 49 | struct astlpc_test { |
| 50 | struct astlpc_endpoint bmc; |
| 51 | struct astlpc_endpoint host; |
| 52 | uint8_t kcs[2]; |
| 53 | uint8_t *lpc_mem; |
| 54 | |
| 55 | void *msg; |
| 56 | uint8_t count; |
| 57 | }; |
| 58 | |
| 59 | #define binding_to_mmio(b) \ |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 60 | container_of(b, struct mctp_binding_astlpc_mmio, astlpc) |
| 61 | |
Andrew Jeffery | 53ea147 | 2020-05-23 21:06:24 +0930 | [diff] [blame] | 62 | static int mctp_astlpc_mmio_kcs_read(void *data, |
| 63 | enum mctp_binding_astlpc_kcs_reg reg, |
| 64 | uint8_t *val) |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 65 | { |
| 66 | struct mctp_binding_astlpc_mmio *mmio = binding_to_mmio(data); |
| 67 | |
Andrew Jeffery | d5e3cd7 | 2020-05-12 22:10:22 +0930 | [diff] [blame] | 68 | *val = (*mmio->kcs)[reg]; |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 69 | |
Andrew Jeffery | d3c0bf0 | 2020-05-28 15:28:40 +0930 | [diff] [blame] | 70 | mctp_prdebug("%s: 0x%hhx from %s", __func__, *val, |
| 71 | reg ? "status" : "data"); |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 72 | |
Andrew Jeffery | d5e3cd7 | 2020-05-12 22:10:22 +0930 | [diff] [blame] | 73 | if (reg == MCTP_ASTLPC_KCS_REG_DATA) { |
| 74 | uint8_t flag = mmio->bmc ? KCS_STATUS_IBF : KCS_STATUS_OBF; |
| 75 | (*mmio->kcs)[MCTP_ASTLPC_KCS_REG_STATUS] &= ~flag; |
| 76 | } |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 77 | |
| 78 | return 0; |
| 79 | } |
| 80 | |
Andrew Jeffery | 53ea147 | 2020-05-23 21:06:24 +0930 | [diff] [blame] | 81 | static int mctp_astlpc_mmio_kcs_write(void *data, |
| 82 | enum mctp_binding_astlpc_kcs_reg reg, |
| 83 | uint8_t val) |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 84 | { |
| 85 | struct mctp_binding_astlpc_mmio *mmio = binding_to_mmio(data); |
Andrew Jeffery | d5e3cd7 | 2020-05-12 22:10:22 +0930 | [diff] [blame] | 86 | uint8_t *regp; |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 87 | |
Andrew Jeffery | d5e3cd7 | 2020-05-12 22:10:22 +0930 | [diff] [blame] | 88 | assert(reg == MCTP_ASTLPC_KCS_REG_DATA || |
| 89 | reg == MCTP_ASTLPC_KCS_REG_STATUS); |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 90 | |
Andrew Jeffery | d5e3cd7 | 2020-05-12 22:10:22 +0930 | [diff] [blame] | 91 | if (reg == MCTP_ASTLPC_KCS_REG_DATA) { |
| 92 | uint8_t flag = mmio->bmc ? KCS_STATUS_OBF : KCS_STATUS_IBF; |
| 93 | (*mmio->kcs)[MCTP_ASTLPC_KCS_REG_STATUS] |= flag; |
| 94 | } |
| 95 | |
| 96 | regp = &(*mmio->kcs)[reg]; |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 97 | if (reg == MCTP_ASTLPC_KCS_REG_STATUS) |
Andrew Jeffery | d5e3cd7 | 2020-05-12 22:10:22 +0930 | [diff] [blame] | 98 | *regp = (val & ~0xbU) | (val & *regp & 1); |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 99 | else |
Andrew Jeffery | d5e3cd7 | 2020-05-12 22:10:22 +0930 | [diff] [blame] | 100 | *regp = val; |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 101 | |
Andrew Jeffery | d3c0bf0 | 2020-05-28 15:28:40 +0930 | [diff] [blame] | 102 | mctp_prdebug("%s: 0x%hhx to %s", __func__, val, |
| 103 | reg ? "status" : "data"); |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 104 | |
| 105 | return 0; |
| 106 | } |
Andrew Jeffery | 06b2cd8 | 2020-03-17 23:12:27 +1030 | [diff] [blame] | 107 | 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] | 108 | { |
| 109 | struct mctp_binding_astlpc_mmio *mmio = binding_to_mmio(data); |
| 110 | |
Andrew Jeffery | 1dbf021 | 2020-05-12 13:53:50 +0930 | [diff] [blame] | 111 | mctp_prdebug("%s: %zu bytes from 0x%lx", __func__, len, offset); |
| 112 | |
Andrew Jeffery | 06b2cd8 | 2020-03-17 23:12:27 +1030 | [diff] [blame] | 113 | assert(offset >= 0L); |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 114 | assert(offset + len < mmio->lpc_size); |
| 115 | |
| 116 | memcpy(buf, mmio->lpc + offset, len); |
| 117 | |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 118 | return 0; |
| 119 | } |
| 120 | |
Andrew Jeffery | 55fb90b | 2020-05-12 13:54:37 +0930 | [diff] [blame] | 121 | int mctp_astlpc_mmio_lpc_write(void *data, const void *buf, long offset, |
| 122 | size_t len) |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 123 | { |
| 124 | struct mctp_binding_astlpc_mmio *mmio = binding_to_mmio(data); |
| 125 | |
Andrew Jeffery | 1dbf021 | 2020-05-12 13:53:50 +0930 | [diff] [blame] | 126 | mctp_prdebug("%s: %zu bytes to 0x%lx", __func__, len, offset); |
| 127 | |
Andrew Jeffery | 06b2cd8 | 2020-03-17 23:12:27 +1030 | [diff] [blame] | 128 | assert(offset >= 0L); |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 129 | assert(offset + len < mmio->lpc_size); |
| 130 | |
| 131 | memcpy(mmio->lpc + offset, buf, len); |
| 132 | |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 133 | return 0; |
| 134 | } |
| 135 | |
Andrew Jeffery | b93b611 | 2020-06-05 14:13:44 +0930 | [diff] [blame] | 136 | #define __unused __attribute__((unused)) |
| 137 | |
| 138 | static void rx_message(uint8_t eid __unused, void *data __unused, void *msg, |
| 139 | size_t len) |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 140 | { |
Andrew Jeffery | 5a7c2db | 2020-05-22 20:13:42 +0930 | [diff] [blame] | 141 | struct astlpc_test *test = data; |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 142 | |
Andrew Jeffery | 5a7c2db | 2020-05-22 20:13:42 +0930 | [diff] [blame] | 143 | mctp_prdebug("MCTP message received: msg: %p, len %zd", msg, len); |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 144 | |
Andrew Jeffery | 5a7c2db | 2020-05-22 20:13:42 +0930 | [diff] [blame] | 145 | assert(len > 0); |
| 146 | assert(msg); |
| 147 | assert(test); |
| 148 | assert(test->msg); |
| 149 | assert(!memcmp(test->msg, msg, len)); |
| 150 | |
| 151 | test->count++; |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 152 | } |
| 153 | |
Andrew Jeffery | 55fb90b | 2020-05-12 13:54:37 +0930 | [diff] [blame] | 154 | static const struct mctp_binding_astlpc_ops astlpc_direct_mmio_ops = { |
| 155 | .kcs_read = mctp_astlpc_mmio_kcs_read, |
| 156 | .kcs_write = mctp_astlpc_mmio_kcs_write, |
| 157 | }; |
| 158 | |
| 159 | static const struct mctp_binding_astlpc_ops astlpc_indirect_mmio_ops = { |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 160 | .kcs_read = mctp_astlpc_mmio_kcs_read, |
| 161 | .kcs_write = mctp_astlpc_mmio_kcs_write, |
| 162 | .lpc_read = mctp_astlpc_mmio_lpc_read, |
| 163 | .lpc_write = mctp_astlpc_mmio_lpc_write, |
| 164 | }; |
| 165 | |
Andrew Jeffery | d5e3cd7 | 2020-05-12 22:10:22 +0930 | [diff] [blame] | 166 | static void endpoint_init(struct astlpc_endpoint *ep, mctp_eid_t eid, |
Andrew Jeffery | 55fb90b | 2020-05-12 13:54:37 +0930 | [diff] [blame] | 167 | uint8_t mode, uint8_t (*kcs)[2], void *lpc_mem) |
Andrew Jeffery | d5e3cd7 | 2020-05-12 22:10:22 +0930 | [diff] [blame] | 168 | { |
| 169 | /* |
| 170 | * Configure the direction of the KCS interface so we know whether to |
| 171 | * set or clear IBF or OBF on writes or reads. |
| 172 | */ |
| 173 | ep->mmio.bmc = (mode == MCTP_BINDING_ASTLPC_MODE_BMC); |
| 174 | |
| 175 | ep->mctp = mctp_init(); |
| 176 | assert(ep->mctp); |
| 177 | |
Andrew Jeffery | d5e3cd7 | 2020-05-12 22:10:22 +0930 | [diff] [blame] | 178 | /* Inject KCS registers */ |
| 179 | ep->mmio.kcs = kcs; |
| 180 | |
Andrew Jeffery | d5e3cd7 | 2020-05-12 22:10:22 +0930 | [diff] [blame] | 181 | /* Initialise the binding */ |
Andrew Jeffery | 129ef93 | 2020-05-22 16:24:19 +0930 | [diff] [blame] | 182 | ep->astlpc = mctp_astlpc_init(mode, MCTP_BTU, lpc_mem, |
Andrew Jeffery | 55fb90b | 2020-05-12 13:54:37 +0930 | [diff] [blame] | 183 | &astlpc_direct_mmio_ops, &ep->mmio); |
Andrew Jeffery | d5e3cd7 | 2020-05-12 22:10:22 +0930 | [diff] [blame] | 184 | |
| 185 | mctp_register_bus(ep->mctp, &ep->astlpc->binding, eid); |
| 186 | } |
| 187 | |
| 188 | static void endpoint_destroy(struct astlpc_endpoint *ep) |
| 189 | { |
| 190 | mctp_astlpc_destroy(ep->astlpc); |
| 191 | mctp_destroy(ep->mctp); |
| 192 | } |
| 193 | |
Andrew Jeffery | cb5d55c | 2020-05-22 13:26:27 +0930 | [diff] [blame] | 194 | static void network_init(struct astlpc_test *ctx) |
| 195 | { |
Andrew Jeffery | 55fb90b | 2020-05-12 13:54:37 +0930 | [diff] [blame] | 196 | ctx->lpc_mem = calloc(1, 1 * 1024 * 1024); |
Andrew Jeffery | cb5d55c | 2020-05-22 13:26:27 +0930 | [diff] [blame] | 197 | assert(ctx->lpc_mem); |
| 198 | |
| 199 | /* BMC initialisation */ |
| 200 | endpoint_init(&ctx->bmc, 8, MCTP_BINDING_ASTLPC_MODE_BMC, &ctx->kcs, |
Andrew Jeffery | 55fb90b | 2020-05-12 13:54:37 +0930 | [diff] [blame] | 201 | ctx->lpc_mem); |
Andrew Jeffery | d0f5da0 | 2020-05-28 09:12:55 +0930 | [diff] [blame^] | 202 | assert(ctx->kcs[KCS_REG_STATUS] & KCS_STATUS_BMC_READY); |
Andrew Jeffery | cb5d55c | 2020-05-22 13:26:27 +0930 | [diff] [blame] | 203 | |
| 204 | /* Host initialisation */ |
| 205 | endpoint_init(&ctx->host, 9, MCTP_BINDING_ASTLPC_MODE_HOST, &ctx->kcs, |
Andrew Jeffery | 55fb90b | 2020-05-12 13:54:37 +0930 | [diff] [blame] | 206 | ctx->lpc_mem); |
Andrew Jeffery | cb5d55c | 2020-05-22 13:26:27 +0930 | [diff] [blame] | 207 | |
| 208 | /* BMC processes host channel init request, alerts host */ |
| 209 | mctp_astlpc_poll(ctx->bmc.astlpc); |
| 210 | assert(ctx->kcs[KCS_REG_STATUS] & KCS_STATUS_CHANNEL_ACTIVE); |
| 211 | assert(ctx->kcs[KCS_REG_DATA] == 0xff); |
| 212 | |
| 213 | /* Host dequeues channel init result */ |
| 214 | mctp_astlpc_poll(ctx->host.astlpc); |
| 215 | } |
| 216 | |
| 217 | static void network_destroy(struct astlpc_test *ctx) |
| 218 | { |
| 219 | endpoint_destroy(&ctx->bmc); |
| 220 | endpoint_destroy(&ctx->host); |
| 221 | free(ctx->lpc_mem); |
| 222 | } |
| 223 | |
Andrew Jeffery | 129ef93 | 2020-05-22 16:24:19 +0930 | [diff] [blame] | 224 | static void astlpc_assert_tx_packet(struct astlpc_endpoint *src, |
| 225 | const void *expected, size_t len) |
| 226 | { |
| 227 | const size_t tx_body = src->astlpc->layout.tx.offset + 4 + 4; |
| 228 | const void *test = ((char *)src->astlpc->lpc_map) + tx_body; |
| 229 | assert(!memcmp(test, expected, len)); |
| 230 | } |
| 231 | |
Andrew Jeffery | e756de8 | 2020-05-22 12:30:58 +0930 | [diff] [blame] | 232 | static void astlpc_test_packetised_message_bmc_to_host(void) |
Andrew Jeffery | d5e3cd7 | 2020-05-12 22:10:22 +0930 | [diff] [blame] | 233 | { |
Andrew Jeffery | cb5d55c | 2020-05-22 13:26:27 +0930 | [diff] [blame] | 234 | struct astlpc_test ctx = { 0 }; |
Andrew Jeffery | d5e3cd7 | 2020-05-12 22:10:22 +0930 | [diff] [blame] | 235 | uint8_t msg[2 * MCTP_BTU]; |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 236 | int rc; |
| 237 | |
Andrew Jeffery | d5e3cd7 | 2020-05-12 22:10:22 +0930 | [diff] [blame] | 238 | /* Test harness initialisation */ |
| 239 | |
Andrew Jeffery | 5a7c2db | 2020-05-22 20:13:42 +0930 | [diff] [blame] | 240 | network_init(&ctx); |
| 241 | |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 242 | memset(&msg[0], 0x5a, MCTP_BTU); |
| 243 | memset(&msg[MCTP_BTU], 0xa5, MCTP_BTU); |
| 244 | |
Andrew Jeffery | 5a7c2db | 2020-05-22 20:13:42 +0930 | [diff] [blame] | 245 | ctx.msg = &msg[0]; |
| 246 | ctx.count = 0; |
| 247 | mctp_set_rx_all(ctx.host.mctp, rx_message, &ctx); |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 248 | |
| 249 | /* BMC sends a message */ |
Andrew Jeffery | cb5d55c | 2020-05-22 13:26:27 +0930 | [diff] [blame] | 250 | rc = mctp_message_tx(ctx.bmc.mctp, 9, msg, sizeof(msg)); |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 251 | assert(rc == 0); |
Andrew Jeffery | d5e3cd7 | 2020-05-12 22:10:22 +0930 | [diff] [blame] | 252 | |
Andrew Jeffery | 129ef93 | 2020-05-22 16:24:19 +0930 | [diff] [blame] | 253 | /* Host receives the first packet */ |
Andrew Jeffery | cb5d55c | 2020-05-22 13:26:27 +0930 | [diff] [blame] | 254 | mctp_astlpc_poll(ctx.host.astlpc); |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 255 | |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 256 | /* BMC dequeues ownership hand-over and sends the queued packet */ |
Andrew Jeffery | cb5d55c | 2020-05-22 13:26:27 +0930 | [diff] [blame] | 257 | rc = mctp_astlpc_poll(ctx.bmc.astlpc); |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 258 | assert(rc == 0); |
| 259 | |
Andrew Jeffery | 129ef93 | 2020-05-22 16:24:19 +0930 | [diff] [blame] | 260 | /* Host receives the next packet */ |
Andrew Jeffery | cb5d55c | 2020-05-22 13:26:27 +0930 | [diff] [blame] | 261 | assert(ctx.kcs[MCTP_ASTLPC_KCS_REG_STATUS] & KCS_STATUS_OBF); |
| 262 | assert(ctx.kcs[MCTP_ASTLPC_KCS_REG_DATA] == 0x01); |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 263 | |
Andrew Jeffery | 129ef93 | 2020-05-22 16:24:19 +0930 | [diff] [blame] | 264 | astlpc_assert_tx_packet(&ctx.bmc, &msg[MCTP_BTU], MCTP_BTU); |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 265 | |
Andrew Jeffery | 5a7c2db | 2020-05-22 20:13:42 +0930 | [diff] [blame] | 266 | /* Host receives final packet */ |
| 267 | mctp_astlpc_poll(ctx.host.astlpc); |
| 268 | assert(ctx.count == 1); |
| 269 | |
Andrew Jeffery | cb5d55c | 2020-05-22 13:26:27 +0930 | [diff] [blame] | 270 | network_destroy(&ctx); |
Andrew Jeffery | e756de8 | 2020-05-22 12:30:58 +0930 | [diff] [blame] | 271 | } |
| 272 | |
Andrew Jeffery | ec9a006 | 2020-05-22 21:21:55 +0930 | [diff] [blame] | 273 | static void astlpc_test_simple_message_host_to_bmc(void) |
| 274 | { |
| 275 | struct astlpc_test ctx = { 0 }; |
| 276 | uint8_t msg[MCTP_BTU]; |
| 277 | int rc; |
| 278 | |
| 279 | /* Test harness initialisation */ |
| 280 | |
| 281 | network_init(&ctx); |
| 282 | |
| 283 | memset(&msg[0], 0xa5, MCTP_BTU); |
| 284 | |
| 285 | ctx.msg = &msg[0]; |
| 286 | ctx.count = 0; |
| 287 | mctp_set_rx_all(ctx.bmc.mctp, rx_message, &ctx); |
| 288 | |
| 289 | /* Host sends the single-packet message */ |
| 290 | rc = mctp_message_tx(ctx.host.mctp, 8, msg, sizeof(msg)); |
| 291 | assert(rc == 0); |
| 292 | assert(ctx.kcs[MCTP_ASTLPC_KCS_REG_STATUS] & KCS_STATUS_IBF); |
| 293 | assert(ctx.kcs[MCTP_ASTLPC_KCS_REG_DATA] == 0x01); |
| 294 | |
| 295 | astlpc_assert_tx_packet(&ctx.host, &msg[0], MCTP_BTU); |
| 296 | |
| 297 | /* BMC receives the single-packet message */ |
| 298 | mctp_astlpc_poll(ctx.bmc.astlpc); |
| 299 | assert(ctx.count == 1); |
| 300 | |
| 301 | /* BMC returns Tx area ownership to Host */ |
| 302 | assert(!(ctx.kcs[MCTP_ASTLPC_KCS_REG_STATUS] & KCS_STATUS_IBF)); |
| 303 | assert(ctx.kcs[MCTP_ASTLPC_KCS_REG_DATA] == 0x02); |
| 304 | assert(ctx.kcs[MCTP_ASTLPC_KCS_REG_STATUS] & KCS_STATUS_OBF); |
| 305 | |
| 306 | /* Host dequeues ownership hand-over and sends the queued packet */ |
| 307 | rc = mctp_astlpc_poll(ctx.host.astlpc); |
| 308 | assert(rc == 0); |
| 309 | |
| 310 | network_destroy(&ctx); |
| 311 | } |
| 312 | |
Andrew Jeffery | 8f3eb72 | 2020-05-22 20:23:49 +0930 | [diff] [blame] | 313 | static void astlpc_test_simple_message_bmc_to_host(void) |
| 314 | { |
| 315 | struct astlpc_test ctx = { 0 }; |
| 316 | uint8_t msg[MCTP_BTU]; |
| 317 | int rc; |
| 318 | |
| 319 | /* Test harness initialisation */ |
| 320 | |
| 321 | network_init(&ctx); |
| 322 | |
| 323 | memset(&msg[0], 0x5a, MCTP_BTU); |
| 324 | |
| 325 | ctx.msg = &msg[0]; |
| 326 | ctx.count = 0; |
| 327 | mctp_set_rx_all(ctx.host.mctp, rx_message, &ctx); |
| 328 | |
| 329 | /* BMC sends the single-packet message */ |
| 330 | rc = mctp_message_tx(ctx.bmc.mctp, 9, msg, sizeof(msg)); |
| 331 | assert(rc == 0); |
| 332 | assert(ctx.kcs[MCTP_ASTLPC_KCS_REG_STATUS] & KCS_STATUS_OBF); |
| 333 | assert(ctx.kcs[MCTP_ASTLPC_KCS_REG_DATA] == 0x01); |
| 334 | |
| 335 | astlpc_assert_tx_packet(&ctx.bmc, &msg[0], MCTP_BTU); |
| 336 | |
| 337 | /* Host receives the single-packet message */ |
| 338 | mctp_astlpc_poll(ctx.host.astlpc); |
| 339 | assert(ctx.count == 1); |
| 340 | |
| 341 | /* Host returns Rx area ownership to BMC */ |
| 342 | assert(!(ctx.kcs[MCTP_ASTLPC_KCS_REG_STATUS] & KCS_STATUS_OBF)); |
| 343 | assert(ctx.kcs[MCTP_ASTLPC_KCS_REG_DATA] == 0x02); |
| 344 | assert(ctx.kcs[MCTP_ASTLPC_KCS_REG_STATUS] & KCS_STATUS_IBF); |
| 345 | |
| 346 | /* BMC dequeues ownership hand-over and sends the queued packet */ |
| 347 | rc = mctp_astlpc_poll(ctx.bmc.astlpc); |
| 348 | assert(rc == 0); |
| 349 | |
| 350 | network_destroy(&ctx); |
| 351 | } |
| 352 | |
Andrew Jeffery | f1cdb16 | 2020-05-23 21:25:21 +0930 | [diff] [blame] | 353 | static void astlpc_test_host_before_bmc(void) |
| 354 | { |
| 355 | struct mctp_binding_astlpc_mmio mmio = { 0 }; |
| 356 | struct mctp_binding_astlpc *astlpc; |
| 357 | uint8_t kcs[2] = { 0 }; |
| 358 | struct mctp *mctp; |
| 359 | int rc; |
| 360 | |
| 361 | mctp = mctp_init(); |
| 362 | assert(mctp); |
| 363 | |
| 364 | /* Inject KCS registers */ |
| 365 | mmio.kcs = &kcs; |
| 366 | |
| 367 | /* Initialise the binding */ |
| 368 | astlpc = mctp_astlpc_init(MCTP_BINDING_ASTLPC_MODE_HOST, MCTP_BTU, NULL, |
Andrew Jeffery | 55fb90b | 2020-05-12 13:54:37 +0930 | [diff] [blame] | 369 | &astlpc_direct_mmio_ops, &mmio); |
Andrew Jeffery | f1cdb16 | 2020-05-23 21:25:21 +0930 | [diff] [blame] | 370 | |
| 371 | /* Register the binding to trigger the start-up sequence */ |
| 372 | rc = mctp_register_bus(mctp, &astlpc->binding, 8); |
| 373 | |
| 374 | /* Start-up should fail as we haven't initialised the BMC */ |
| 375 | assert(rc < 0); |
| 376 | |
| 377 | mctp_astlpc_destroy(astlpc); |
| 378 | mctp_destroy(mctp); |
| 379 | } |
| 380 | |
Andrew Jeffery | f1a2131 | 2020-05-22 12:48:21 +0930 | [diff] [blame] | 381 | static void astlpc_test_simple_init(void) |
| 382 | { |
| 383 | struct astlpc_endpoint bmc, host; |
| 384 | uint8_t kcs[2] = { 0 }; |
Andrew Jeffery | f1a2131 | 2020-05-22 12:48:21 +0930 | [diff] [blame] | 385 | void *lpc_mem; |
| 386 | |
| 387 | /* Test harness initialisation */ |
Andrew Jeffery | 55fb90b | 2020-05-12 13:54:37 +0930 | [diff] [blame] | 388 | lpc_mem = calloc(1, 1 * 1024 * 1024); |
Andrew Jeffery | f1a2131 | 2020-05-22 12:48:21 +0930 | [diff] [blame] | 389 | assert(lpc_mem); |
| 390 | |
Andrew Jeffery | 8f3eb72 | 2020-05-22 20:23:49 +0930 | [diff] [blame] | 391 | /* BMC initialisation */ |
Andrew Jeffery | 55fb90b | 2020-05-12 13:54:37 +0930 | [diff] [blame] | 392 | endpoint_init(&bmc, 8, MCTP_BINDING_ASTLPC_MODE_BMC, &kcs, lpc_mem); |
Andrew Jeffery | f1a2131 | 2020-05-22 12:48:21 +0930 | [diff] [blame] | 393 | |
| 394 | /* Verify the BMC binding was initialised */ |
| 395 | assert(kcs[MCTP_ASTLPC_KCS_REG_STATUS] & KCS_STATUS_BMC_READY); |
| 396 | |
Andrew Jeffery | 8f3eb72 | 2020-05-22 20:23:49 +0930 | [diff] [blame] | 397 | /* Host initialisation */ |
Andrew Jeffery | 55fb90b | 2020-05-12 13:54:37 +0930 | [diff] [blame] | 398 | endpoint_init(&host, 9, MCTP_BINDING_ASTLPC_MODE_HOST, &kcs, lpc_mem); |
Andrew Jeffery | f1a2131 | 2020-05-22 12:48:21 +0930 | [diff] [blame] | 399 | |
| 400 | /* Host sends channel init command */ |
| 401 | assert(kcs[MCTP_ASTLPC_KCS_REG_STATUS] & KCS_STATUS_IBF); |
| 402 | assert(kcs[MCTP_ASTLPC_KCS_REG_DATA] == 0x00); |
| 403 | |
| 404 | /* BMC receives host channel init request */ |
| 405 | mctp_astlpc_poll(bmc.astlpc); |
| 406 | |
| 407 | /* BMC sends init response */ |
| 408 | assert(kcs[MCTP_ASTLPC_KCS_REG_STATUS] & KCS_STATUS_OBF); |
| 409 | assert(kcs[MCTP_ASTLPC_KCS_REG_STATUS] & KCS_STATUS_CHANNEL_ACTIVE); |
| 410 | assert(kcs[MCTP_ASTLPC_KCS_REG_DATA] == 0xff); |
| 411 | |
| 412 | /* Host dequeues data */ |
| 413 | mctp_astlpc_poll(host.astlpc); |
| 414 | |
| 415 | endpoint_destroy(&bmc); |
| 416 | endpoint_destroy(&host); |
| 417 | free(lpc_mem); |
| 418 | } |
| 419 | |
Andrew Jeffery | 55fb90b | 2020-05-12 13:54:37 +0930 | [diff] [blame] | 420 | static void astlpc_test_simple_indirect_message_bmc_to_host(void) |
| 421 | { |
| 422 | struct astlpc_test ctx = { 0 }; |
| 423 | uint8_t kcs[2] = { 0 }; |
| 424 | uint8_t msg[MCTP_BTU]; |
| 425 | int rc; |
| 426 | |
| 427 | ctx.lpc_mem = calloc(1, LPC_WIN_SIZE); |
| 428 | assert(ctx.lpc_mem); |
| 429 | |
| 430 | /* Test message data */ |
| 431 | memset(&msg[0], 0x5a, MCTP_BTU); |
| 432 | |
| 433 | /* Manually set up the network so we can inject the indirect ops */ |
| 434 | |
| 435 | /* BMC initialisation */ |
| 436 | ctx.bmc.mmio.bmc = true; |
| 437 | ctx.bmc.mctp = mctp_init(); |
| 438 | assert(ctx.bmc.mctp); |
| 439 | ctx.bmc.mmio.kcs = &kcs; |
| 440 | ctx.bmc.mmio.lpc = ctx.lpc_mem; |
| 441 | ctx.bmc.mmio.lpc_size = LPC_WIN_SIZE; |
| 442 | ctx.bmc.astlpc = |
| 443 | mctp_astlpc_init(MCTP_BINDING_ASTLPC_MODE_BMC, MCTP_BTU, NULL, |
| 444 | &astlpc_indirect_mmio_ops, &ctx.bmc.mmio); |
| 445 | mctp_register_bus(ctx.bmc.mctp, &ctx.bmc.astlpc->binding, 8); |
| 446 | |
| 447 | /* Host initialisation */ |
| 448 | ctx.host.mmio.bmc = false; |
| 449 | ctx.host.mctp = mctp_init(); |
| 450 | assert(ctx.host.mctp); |
| 451 | ctx.host.mmio.kcs = &kcs; |
| 452 | ctx.host.mmio.lpc = ctx.lpc_mem; |
| 453 | ctx.host.mmio.lpc_size = LPC_WIN_SIZE; |
| 454 | ctx.host.astlpc = |
| 455 | mctp_astlpc_init(MCTP_BINDING_ASTLPC_MODE_HOST, MCTP_BTU, NULL, |
| 456 | &astlpc_indirect_mmio_ops, &ctx.host.mmio); |
| 457 | mctp_register_bus(ctx.host.mctp, &ctx.host.astlpc->binding, 9); |
| 458 | |
| 459 | /* BMC processes host channel init request, alerts host */ |
| 460 | mctp_astlpc_poll(ctx.bmc.astlpc); |
| 461 | |
| 462 | /* Host dequeues channel init result */ |
| 463 | mctp_astlpc_poll(ctx.host.astlpc); |
| 464 | |
| 465 | ctx.msg = &msg[0]; |
| 466 | ctx.count = 0; |
| 467 | mctp_set_rx_all(ctx.host.mctp, rx_message, &ctx); |
| 468 | |
| 469 | /* BMC sends the single-packet message */ |
| 470 | rc = mctp_message_tx(ctx.bmc.mctp, 9, msg, sizeof(msg)); |
| 471 | assert(rc == 0); |
| 472 | |
| 473 | /* Host receives the single-packet message */ |
| 474 | rc = mctp_astlpc_poll(ctx.host.astlpc); |
| 475 | assert(rc == 0); |
| 476 | assert(ctx.count == 1); |
| 477 | |
| 478 | /* BMC dequeues ownership hand-over and sends the queued packet */ |
| 479 | rc = mctp_astlpc_poll(ctx.bmc.astlpc); |
| 480 | assert(rc == 0); |
| 481 | |
| 482 | /* Can still tear-down the network in the normal fashion */ |
| 483 | network_destroy(&ctx); |
| 484 | } |
| 485 | |
Andrew Jeffery | d0f5da0 | 2020-05-28 09:12:55 +0930 | [diff] [blame^] | 486 | static void astlpc_test_host_tx_bmc_gone(void) |
| 487 | { |
| 488 | struct astlpc_test ctx = { 0 }; |
| 489 | uint8_t unwritten[MCTP_BTU]; |
| 490 | uint8_t msg[MCTP_BTU]; |
| 491 | int rc; |
| 492 | |
| 493 | /* Test harness initialisation */ |
| 494 | |
| 495 | network_init(&ctx); |
| 496 | |
| 497 | memset(&msg[0], 0x5a, sizeof(msg)); |
| 498 | memset(&unwritten[0], 0, sizeof(unwritten)); |
| 499 | |
| 500 | ctx.msg = &msg[0]; |
| 501 | ctx.count = 0; |
| 502 | |
| 503 | /* Clear bmc-ready */ |
| 504 | endpoint_destroy(&ctx.bmc); |
| 505 | |
| 506 | /* Host detects that the BMC is disabled */ |
| 507 | mctp_astlpc_poll(ctx.host.astlpc); |
| 508 | |
| 509 | /* Host attempts to send the single-packet message, but is prevented */ |
| 510 | rc = mctp_message_tx(ctx.host.mctp, 8, msg, sizeof(msg)); |
| 511 | assert(rc == 0); |
| 512 | assert(!(ctx.kcs[MCTP_ASTLPC_KCS_REG_STATUS] & KCS_STATUS_OBF)); |
| 513 | astlpc_assert_tx_packet(&ctx.host, &unwritten[0], MCTP_BTU); |
| 514 | |
| 515 | /* BMC comes back */ |
| 516 | endpoint_init(&ctx.bmc, 8, MCTP_BINDING_ASTLPC_MODE_BMC, &ctx.kcs, |
| 517 | ctx.lpc_mem); |
| 518 | mctp_set_rx_all(ctx.bmc.mctp, rx_message, &ctx); |
| 519 | |
| 520 | /* Host triggers channel init */ |
| 521 | mctp_astlpc_poll(ctx.host.astlpc); |
| 522 | |
| 523 | /* BMC handles channel init */ |
| 524 | mctp_astlpc_poll(ctx.bmc.astlpc); |
| 525 | |
| 526 | /* Host completes channel init, flushing the Tx queue */ |
| 527 | mctp_astlpc_poll(ctx.host.astlpc); |
| 528 | |
| 529 | /* BMC receives the single-packet message */ |
| 530 | mctp_astlpc_poll(ctx.bmc.astlpc); |
| 531 | assert(ctx.count == 1); |
| 532 | |
| 533 | network_destroy(&ctx); |
| 534 | } |
| 535 | |
Andrew Jeffery | 91f09ed | 2020-05-22 20:52:26 +0930 | [diff] [blame] | 536 | /* clang-format off */ |
| 537 | #define TEST_CASE(test) { #test, test } |
| 538 | static const struct { |
| 539 | const char *name; |
| 540 | void (*test)(void); |
| 541 | } astlpc_tests[] = { |
| 542 | TEST_CASE(astlpc_test_simple_init), |
Andrew Jeffery | f1cdb16 | 2020-05-23 21:25:21 +0930 | [diff] [blame] | 543 | TEST_CASE(astlpc_test_host_before_bmc), |
Andrew Jeffery | 91f09ed | 2020-05-22 20:52:26 +0930 | [diff] [blame] | 544 | TEST_CASE(astlpc_test_simple_message_bmc_to_host), |
Andrew Jeffery | ec9a006 | 2020-05-22 21:21:55 +0930 | [diff] [blame] | 545 | TEST_CASE(astlpc_test_simple_message_host_to_bmc), |
Andrew Jeffery | 91f09ed | 2020-05-22 20:52:26 +0930 | [diff] [blame] | 546 | TEST_CASE(astlpc_test_packetised_message_bmc_to_host), |
Andrew Jeffery | d0f5da0 | 2020-05-28 09:12:55 +0930 | [diff] [blame^] | 547 | TEST_CASE(astlpc_test_simple_indirect_message_bmc_to_host), |
| 548 | TEST_CASE(astlpc_test_host_tx_bmc_gone), |
Andrew Jeffery | 91f09ed | 2020-05-22 20:52:26 +0930 | [diff] [blame] | 549 | }; |
| 550 | /* clang-format on */ |
| 551 | |
| 552 | #ifndef BUILD_ASSERT |
| 553 | #define BUILD_ASSERT(x) \ |
| 554 | do { \ |
| 555 | (void)sizeof(char[0 - (!(x))]); \ |
| 556 | } while (0) |
| 557 | #endif |
| 558 | |
Andrew Jeffery | e756de8 | 2020-05-22 12:30:58 +0930 | [diff] [blame] | 559 | int main(void) |
| 560 | { |
Andrew Jeffery | 91f09ed | 2020-05-22 20:52:26 +0930 | [diff] [blame] | 561 | size_t i; |
| 562 | |
Andrew Jeffery | e756de8 | 2020-05-22 12:30:58 +0930 | [diff] [blame] | 563 | mctp_set_log_stdio(MCTP_LOG_DEBUG); |
| 564 | |
Andrew Jeffery | 91f09ed | 2020-05-22 20:52:26 +0930 | [diff] [blame] | 565 | BUILD_ASSERT(ARRAY_SIZE(astlpc_tests) < SIZE_MAX); |
| 566 | for (i = 0; i < ARRAY_SIZE(astlpc_tests); i++) { |
| 567 | mctp_prlog(MCTP_LOG_DEBUG, "begin: %s", astlpc_tests[i].name); |
| 568 | astlpc_tests[i].test(); |
| 569 | mctp_prlog(MCTP_LOG_DEBUG, "end: %s\n", astlpc_tests[i].name); |
| 570 | } |
Andrew Jeffery | 11b7e92 | 2020-03-10 23:37:09 +1030 | [diff] [blame] | 571 | |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 572 | return 0; |
| 573 | } |