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 | eba19a3 | 2021-03-09 23:09:40 +1030 | [diff] [blame] | 7 | #define ASTLPC_VER_CUR 3 |
Andrew Jeffery | df5f6b9 | 2020-05-12 21:39:06 +0930 | [diff] [blame] | 8 | #include "astlpc.c" |
| 9 | |
| 10 | #ifdef pr_fmt |
| 11 | #undef pr_fmt |
| 12 | #define pr_fmt(x) "test: " x |
| 13 | #endif |
| 14 | |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 15 | #include "libmctp-astlpc.h" |
| 16 | #include "libmctp-log.h" |
Przemyslaw Czarnowski | ff25d7e | 2020-03-26 11:39:37 +0100 | [diff] [blame] | 17 | #include "container_of.h" |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 18 | |
| 19 | #ifdef NDEBUG |
| 20 | #undef NDEBUG |
| 21 | #endif |
| 22 | |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 23 | #include <assert.h> |
Andrew Jeffery | 91f09ed | 2020-05-22 20:52:26 +0930 | [diff] [blame] | 24 | #include <limits.h> |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 25 | #include <stdint.h> |
| 26 | #include <stdio.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <string.h> |
Andrew Jeffery | c2b833e | 2020-10-28 14:28:37 +1030 | [diff] [blame] | 29 | #include <sys/random.h> |
Andrew Jeffery | 3f4bca9 | 2021-01-28 14:19:19 +1030 | [diff] [blame] | 30 | #include <unistd.h> |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 31 | |
Andrew Jeffery | 91f09ed | 2020-05-22 20:52:26 +0930 | [diff] [blame] | 32 | #ifndef ARRAY_SIZE |
| 33 | #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) |
| 34 | #endif |
| 35 | |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 36 | struct mctp_binding_astlpc_mmio { |
| 37 | struct mctp_binding_astlpc astlpc; |
Andrew Jeffery | d5e3cd7 | 2020-05-12 22:10:22 +0930 | [diff] [blame] | 38 | bool bmc; |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 39 | |
Andrew Jeffery | d5e3cd7 | 2020-05-12 22:10:22 +0930 | [diff] [blame] | 40 | uint8_t (*kcs)[2]; |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 41 | |
| 42 | size_t lpc_size; |
| 43 | uint8_t *lpc; |
| 44 | }; |
| 45 | |
Andrew Jeffery | 5a7c2db | 2020-05-22 20:13:42 +0930 | [diff] [blame] | 46 | struct astlpc_endpoint { |
| 47 | struct mctp_binding_astlpc_mmio mmio; |
| 48 | struct mctp_binding_astlpc *astlpc; |
| 49 | struct mctp *mctp; |
| 50 | }; |
| 51 | |
| 52 | struct astlpc_test { |
| 53 | struct astlpc_endpoint bmc; |
| 54 | struct astlpc_endpoint host; |
| 55 | uint8_t kcs[2]; |
| 56 | uint8_t *lpc_mem; |
| 57 | |
| 58 | void *msg; |
| 59 | uint8_t count; |
| 60 | }; |
| 61 | |
| 62 | #define binding_to_mmio(b) \ |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 63 | container_of(b, struct mctp_binding_astlpc_mmio, astlpc) |
| 64 | |
Andrew Jeffery | 53ea147 | 2020-05-23 21:06:24 +0930 | [diff] [blame] | 65 | static int mctp_astlpc_mmio_kcs_read(void *data, |
| 66 | enum mctp_binding_astlpc_kcs_reg reg, |
| 67 | uint8_t *val) |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 68 | { |
| 69 | struct mctp_binding_astlpc_mmio *mmio = binding_to_mmio(data); |
| 70 | |
Andrew Jeffery | d5e3cd7 | 2020-05-12 22:10:22 +0930 | [diff] [blame] | 71 | *val = (*mmio->kcs)[reg]; |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 72 | |
Andrew Jeffery | d3c0bf0 | 2020-05-28 15:28:40 +0930 | [diff] [blame] | 73 | mctp_prdebug("%s: 0x%hhx from %s", __func__, *val, |
| 74 | reg ? "status" : "data"); |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 75 | |
Andrew Jeffery | d5e3cd7 | 2020-05-12 22:10:22 +0930 | [diff] [blame] | 76 | if (reg == MCTP_ASTLPC_KCS_REG_DATA) { |
| 77 | uint8_t flag = mmio->bmc ? KCS_STATUS_IBF : KCS_STATUS_OBF; |
| 78 | (*mmio->kcs)[MCTP_ASTLPC_KCS_REG_STATUS] &= ~flag; |
| 79 | } |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 80 | |
| 81 | return 0; |
| 82 | } |
| 83 | |
Andrew Jeffery | 53ea147 | 2020-05-23 21:06:24 +0930 | [diff] [blame] | 84 | static int mctp_astlpc_mmio_kcs_write(void *data, |
| 85 | enum mctp_binding_astlpc_kcs_reg reg, |
| 86 | uint8_t val) |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 87 | { |
| 88 | struct mctp_binding_astlpc_mmio *mmio = binding_to_mmio(data); |
Andrew Jeffery | d5e3cd7 | 2020-05-12 22:10:22 +0930 | [diff] [blame] | 89 | uint8_t *regp; |
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 | assert(reg == MCTP_ASTLPC_KCS_REG_DATA || |
| 92 | reg == MCTP_ASTLPC_KCS_REG_STATUS); |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 93 | |
Andrew Jeffery | d5e3cd7 | 2020-05-12 22:10:22 +0930 | [diff] [blame] | 94 | if (reg == MCTP_ASTLPC_KCS_REG_DATA) { |
| 95 | uint8_t flag = mmio->bmc ? KCS_STATUS_OBF : KCS_STATUS_IBF; |
| 96 | (*mmio->kcs)[MCTP_ASTLPC_KCS_REG_STATUS] |= flag; |
| 97 | } |
| 98 | |
| 99 | regp = &(*mmio->kcs)[reg]; |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 100 | if (reg == MCTP_ASTLPC_KCS_REG_STATUS) |
Andrew Jeffery | d5e3cd7 | 2020-05-12 22:10:22 +0930 | [diff] [blame] | 101 | *regp = (val & ~0xbU) | (val & *regp & 1); |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 102 | else |
Andrew Jeffery | d5e3cd7 | 2020-05-12 22:10:22 +0930 | [diff] [blame] | 103 | *regp = val; |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 104 | |
Andrew Jeffery | d3c0bf0 | 2020-05-28 15:28:40 +0930 | [diff] [blame] | 105 | mctp_prdebug("%s: 0x%hhx to %s", __func__, val, |
| 106 | reg ? "status" : "data"); |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 107 | |
| 108 | return 0; |
| 109 | } |
Andrew Jeffery | 85c04e4 | 2020-10-28 14:27:15 +1030 | [diff] [blame] | 110 | |
| 111 | static const struct mctp_binding_astlpc_ops astlpc_direct_mmio_ops = { |
| 112 | .kcs_read = mctp_astlpc_mmio_kcs_read, |
| 113 | .kcs_write = mctp_astlpc_mmio_kcs_write, |
| 114 | }; |
| 115 | |
Andrew Jeffery | 06b2cd8 | 2020-03-17 23:12:27 +1030 | [diff] [blame] | 116 | 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] | 117 | { |
| 118 | struct mctp_binding_astlpc_mmio *mmio = binding_to_mmio(data); |
| 119 | |
Andrew Jeffery | 1dbf021 | 2020-05-12 13:53:50 +0930 | [diff] [blame] | 120 | mctp_prdebug("%s: %zu bytes from 0x%lx", __func__, len, offset); |
| 121 | |
Andrew Jeffery | 06b2cd8 | 2020-03-17 23:12:27 +1030 | [diff] [blame] | 122 | assert(offset >= 0L); |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 123 | assert(offset + len < mmio->lpc_size); |
| 124 | |
| 125 | memcpy(buf, mmio->lpc + offset, len); |
| 126 | |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 127 | return 0; |
| 128 | } |
| 129 | |
Andrew Jeffery | 55fb90b | 2020-05-12 13:54:37 +0930 | [diff] [blame] | 130 | int mctp_astlpc_mmio_lpc_write(void *data, const void *buf, long offset, |
| 131 | size_t len) |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 132 | { |
| 133 | struct mctp_binding_astlpc_mmio *mmio = binding_to_mmio(data); |
| 134 | |
Andrew Jeffery | 1dbf021 | 2020-05-12 13:53:50 +0930 | [diff] [blame] | 135 | mctp_prdebug("%s: %zu bytes to 0x%lx", __func__, len, offset); |
| 136 | |
Andrew Jeffery | 06b2cd8 | 2020-03-17 23:12:27 +1030 | [diff] [blame] | 137 | assert(offset >= 0L); |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 138 | assert(offset + len < mmio->lpc_size); |
| 139 | |
| 140 | memcpy(mmio->lpc + offset, buf, len); |
| 141 | |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 142 | return 0; |
| 143 | } |
| 144 | |
Andrew Jeffery | 85c04e4 | 2020-10-28 14:27:15 +1030 | [diff] [blame] | 145 | static const struct mctp_binding_astlpc_ops astlpc_indirect_mmio_ops = { |
| 146 | .kcs_read = mctp_astlpc_mmio_kcs_read, |
| 147 | .kcs_write = mctp_astlpc_mmio_kcs_write, |
| 148 | .lpc_read = mctp_astlpc_mmio_lpc_read, |
| 149 | .lpc_write = mctp_astlpc_mmio_lpc_write, |
| 150 | }; |
| 151 | |
Andrew Jeffery | b93b611 | 2020-06-05 14:13:44 +0930 | [diff] [blame] | 152 | #define __unused __attribute__((unused)) |
| 153 | |
| 154 | static void rx_message(uint8_t eid __unused, void *data __unused, void *msg, |
| 155 | size_t len) |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 156 | { |
Andrew Jeffery | 5a7c2db | 2020-05-22 20:13:42 +0930 | [diff] [blame] | 157 | struct astlpc_test *test = data; |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 158 | |
Andrew Jeffery | 5a7c2db | 2020-05-22 20:13:42 +0930 | [diff] [blame] | 159 | mctp_prdebug("MCTP message received: msg: %p, len %zd", msg, len); |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 160 | |
Andrew Jeffery | 5a7c2db | 2020-05-22 20:13:42 +0930 | [diff] [blame] | 161 | assert(len > 0); |
| 162 | assert(msg); |
| 163 | assert(test); |
| 164 | assert(test->msg); |
| 165 | assert(!memcmp(test->msg, msg, len)); |
| 166 | |
| 167 | test->count++; |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 168 | } |
| 169 | |
Andrew Jeffery | 4e8264b | 2020-05-23 20:34:33 +0930 | [diff] [blame] | 170 | static int endpoint_init(struct astlpc_endpoint *ep, mctp_eid_t eid, |
Andrew Jeffery | a936898 | 2020-06-09 13:07:39 +0930 | [diff] [blame] | 171 | uint8_t mode, uint32_t mtu, uint8_t (*kcs)[2], |
| 172 | void *lpc_mem) |
Andrew Jeffery | d5e3cd7 | 2020-05-12 22:10:22 +0930 | [diff] [blame] | 173 | { |
| 174 | /* |
| 175 | * Configure the direction of the KCS interface so we know whether to |
| 176 | * set or clear IBF or OBF on writes or reads. |
| 177 | */ |
| 178 | ep->mmio.bmc = (mode == MCTP_BINDING_ASTLPC_MODE_BMC); |
| 179 | |
| 180 | ep->mctp = mctp_init(); |
| 181 | assert(ep->mctp); |
| 182 | |
Andrew Jeffery | d5e3cd7 | 2020-05-12 22:10:22 +0930 | [diff] [blame] | 183 | /* Inject KCS registers */ |
| 184 | ep->mmio.kcs = kcs; |
| 185 | |
Andrew Jeffery | d5e3cd7 | 2020-05-12 22:10:22 +0930 | [diff] [blame] | 186 | /* Initialise the binding */ |
Andrew Jeffery | a936898 | 2020-06-09 13:07:39 +0930 | [diff] [blame] | 187 | ep->astlpc = mctp_astlpc_init(mode, mtu, lpc_mem, |
Andrew Jeffery | 55fb90b | 2020-05-12 13:54:37 +0930 | [diff] [blame] | 188 | &astlpc_direct_mmio_ops, &ep->mmio); |
Andrew Jeffery | 3a54066 | 2020-05-26 19:55:30 +0930 | [diff] [blame] | 189 | assert(ep->astlpc); |
Andrew Jeffery | d5e3cd7 | 2020-05-12 22:10:22 +0930 | [diff] [blame] | 190 | |
Andrew Jeffery | 4e8264b | 2020-05-23 20:34:33 +0930 | [diff] [blame] | 191 | return mctp_register_bus(ep->mctp, &ep->astlpc->binding, eid); |
Andrew Jeffery | d5e3cd7 | 2020-05-12 22:10:22 +0930 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | static void endpoint_destroy(struct astlpc_endpoint *ep) |
| 195 | { |
| 196 | mctp_astlpc_destroy(ep->astlpc); |
| 197 | mctp_destroy(ep->mctp); |
| 198 | } |
| 199 | |
Andrew Jeffery | cb5d55c | 2020-05-22 13:26:27 +0930 | [diff] [blame] | 200 | static void network_init(struct astlpc_test *ctx) |
| 201 | { |
Andrew Jeffery | 4e8264b | 2020-05-23 20:34:33 +0930 | [diff] [blame] | 202 | int rc; |
| 203 | |
Andrew Jeffery | 55fb90b | 2020-05-12 13:54:37 +0930 | [diff] [blame] | 204 | ctx->lpc_mem = calloc(1, 1 * 1024 * 1024); |
Andrew Jeffery | cb5d55c | 2020-05-22 13:26:27 +0930 | [diff] [blame] | 205 | assert(ctx->lpc_mem); |
| 206 | |
| 207 | /* BMC initialisation */ |
Andrew Jeffery | a936898 | 2020-06-09 13:07:39 +0930 | [diff] [blame] | 208 | rc = endpoint_init(&ctx->bmc, 8, MCTP_BINDING_ASTLPC_MODE_BMC, MCTP_BTU, |
Andrew Jeffery | 4e8264b | 2020-05-23 20:34:33 +0930 | [diff] [blame] | 209 | &ctx->kcs, ctx->lpc_mem); |
| 210 | assert(!rc); |
Andrew Jeffery | 3f32507 | 2020-05-28 09:19:51 +0930 | [diff] [blame] | 211 | assert(ctx->kcs[MCTP_ASTLPC_KCS_REG_STATUS] & KCS_STATUS_BMC_READY); |
Andrew Jeffery | cb5d55c | 2020-05-22 13:26:27 +0930 | [diff] [blame] | 212 | |
| 213 | /* Host initialisation */ |
Andrew Jeffery | 4e8264b | 2020-05-23 20:34:33 +0930 | [diff] [blame] | 214 | rc = endpoint_init(&ctx->host, 9, MCTP_BINDING_ASTLPC_MODE_HOST, |
Andrew Jeffery | a936898 | 2020-06-09 13:07:39 +0930 | [diff] [blame] | 215 | MCTP_BTU, &ctx->kcs, ctx->lpc_mem); |
Andrew Jeffery | 4e8264b | 2020-05-23 20:34:33 +0930 | [diff] [blame] | 216 | assert(!rc); |
Andrew Jeffery | cb5d55c | 2020-05-22 13:26:27 +0930 | [diff] [blame] | 217 | |
| 218 | /* BMC processes host channel init request, alerts host */ |
| 219 | mctp_astlpc_poll(ctx->bmc.astlpc); |
Andrew Jeffery | 3f32507 | 2020-05-28 09:19:51 +0930 | [diff] [blame] | 220 | assert(ctx->kcs[MCTP_ASTLPC_KCS_REG_STATUS] & |
| 221 | KCS_STATUS_CHANNEL_ACTIVE); |
| 222 | assert(ctx->kcs[MCTP_ASTLPC_KCS_REG_DATA] == 0xff); |
Andrew Jeffery | cb5d55c | 2020-05-22 13:26:27 +0930 | [diff] [blame] | 223 | |
| 224 | /* Host dequeues channel init result */ |
| 225 | mctp_astlpc_poll(ctx->host.astlpc); |
| 226 | } |
| 227 | |
| 228 | static void network_destroy(struct astlpc_test *ctx) |
| 229 | { |
| 230 | endpoint_destroy(&ctx->bmc); |
| 231 | endpoint_destroy(&ctx->host); |
| 232 | free(ctx->lpc_mem); |
| 233 | } |
| 234 | |
Andrew Jeffery | 129ef93 | 2020-05-22 16:24:19 +0930 | [diff] [blame] | 235 | static void astlpc_assert_tx_packet(struct astlpc_endpoint *src, |
| 236 | const void *expected, size_t len) |
| 237 | { |
| 238 | const size_t tx_body = src->astlpc->layout.tx.offset + 4 + 4; |
| 239 | const void *test = ((char *)src->astlpc->lpc_map) + tx_body; |
| 240 | assert(!memcmp(test, expected, len)); |
| 241 | } |
| 242 | |
Andrew Jeffery | e756de8 | 2020-05-22 12:30:58 +0930 | [diff] [blame] | 243 | static void astlpc_test_packetised_message_bmc_to_host(void) |
Andrew Jeffery | d5e3cd7 | 2020-05-12 22:10:22 +0930 | [diff] [blame] | 244 | { |
Andrew Jeffery | cb5d55c | 2020-05-22 13:26:27 +0930 | [diff] [blame] | 245 | struct astlpc_test ctx = { 0 }; |
Andrew Jeffery | d5e3cd7 | 2020-05-12 22:10:22 +0930 | [diff] [blame] | 246 | uint8_t msg[2 * MCTP_BTU]; |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 247 | int rc; |
| 248 | |
Andrew Jeffery | d5e3cd7 | 2020-05-12 22:10:22 +0930 | [diff] [blame] | 249 | /* Test harness initialisation */ |
| 250 | |
Andrew Jeffery | 5a7c2db | 2020-05-22 20:13:42 +0930 | [diff] [blame] | 251 | network_init(&ctx); |
| 252 | |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 253 | memset(&msg[0], 0x5a, MCTP_BTU); |
| 254 | memset(&msg[MCTP_BTU], 0xa5, MCTP_BTU); |
| 255 | |
Andrew Jeffery | 5a7c2db | 2020-05-22 20:13:42 +0930 | [diff] [blame] | 256 | ctx.msg = &msg[0]; |
| 257 | ctx.count = 0; |
| 258 | mctp_set_rx_all(ctx.host.mctp, rx_message, &ctx); |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 259 | |
| 260 | /* BMC sends a message */ |
Andrew Jeffery | cb5d55c | 2020-05-22 13:26:27 +0930 | [diff] [blame] | 261 | rc = mctp_message_tx(ctx.bmc.mctp, 9, msg, sizeof(msg)); |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 262 | assert(rc == 0); |
Andrew Jeffery | d5e3cd7 | 2020-05-12 22:10:22 +0930 | [diff] [blame] | 263 | |
Andrew Jeffery | 129ef93 | 2020-05-22 16:24:19 +0930 | [diff] [blame] | 264 | /* Host receives the first packet */ |
Andrew Jeffery | cb5d55c | 2020-05-22 13:26:27 +0930 | [diff] [blame] | 265 | mctp_astlpc_poll(ctx.host.astlpc); |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 266 | |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 267 | /* BMC dequeues ownership hand-over and sends the queued packet */ |
Andrew Jeffery | cb5d55c | 2020-05-22 13:26:27 +0930 | [diff] [blame] | 268 | rc = mctp_astlpc_poll(ctx.bmc.astlpc); |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 269 | assert(rc == 0); |
| 270 | |
Andrew Jeffery | 129ef93 | 2020-05-22 16:24:19 +0930 | [diff] [blame] | 271 | /* Host receives the next packet */ |
Andrew Jeffery | cb5d55c | 2020-05-22 13:26:27 +0930 | [diff] [blame] | 272 | assert(ctx.kcs[MCTP_ASTLPC_KCS_REG_STATUS] & KCS_STATUS_OBF); |
| 273 | assert(ctx.kcs[MCTP_ASTLPC_KCS_REG_DATA] == 0x01); |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 274 | |
Andrew Jeffery | 129ef93 | 2020-05-22 16:24:19 +0930 | [diff] [blame] | 275 | astlpc_assert_tx_packet(&ctx.bmc, &msg[MCTP_BTU], MCTP_BTU); |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 276 | |
Andrew Jeffery | 5a7c2db | 2020-05-22 20:13:42 +0930 | [diff] [blame] | 277 | /* Host receives final packet */ |
| 278 | mctp_astlpc_poll(ctx.host.astlpc); |
| 279 | assert(ctx.count == 1); |
| 280 | |
Andrew Jeffery | cb5d55c | 2020-05-22 13:26:27 +0930 | [diff] [blame] | 281 | network_destroy(&ctx); |
Andrew Jeffery | e756de8 | 2020-05-22 12:30:58 +0930 | [diff] [blame] | 282 | } |
| 283 | |
Andrew Jeffery | ec9a006 | 2020-05-22 21:21:55 +0930 | [diff] [blame] | 284 | static void astlpc_test_simple_message_host_to_bmc(void) |
| 285 | { |
| 286 | struct astlpc_test ctx = { 0 }; |
| 287 | uint8_t msg[MCTP_BTU]; |
| 288 | int rc; |
| 289 | |
| 290 | /* Test harness initialisation */ |
| 291 | |
| 292 | network_init(&ctx); |
| 293 | |
| 294 | memset(&msg[0], 0xa5, MCTP_BTU); |
| 295 | |
| 296 | ctx.msg = &msg[0]; |
| 297 | ctx.count = 0; |
| 298 | mctp_set_rx_all(ctx.bmc.mctp, rx_message, &ctx); |
| 299 | |
| 300 | /* Host sends the single-packet message */ |
| 301 | rc = mctp_message_tx(ctx.host.mctp, 8, msg, sizeof(msg)); |
| 302 | assert(rc == 0); |
| 303 | assert(ctx.kcs[MCTP_ASTLPC_KCS_REG_STATUS] & KCS_STATUS_IBF); |
| 304 | assert(ctx.kcs[MCTP_ASTLPC_KCS_REG_DATA] == 0x01); |
| 305 | |
| 306 | astlpc_assert_tx_packet(&ctx.host, &msg[0], MCTP_BTU); |
| 307 | |
| 308 | /* BMC receives the single-packet message */ |
| 309 | mctp_astlpc_poll(ctx.bmc.astlpc); |
| 310 | assert(ctx.count == 1); |
| 311 | |
| 312 | /* BMC returns Tx area ownership to Host */ |
| 313 | assert(!(ctx.kcs[MCTP_ASTLPC_KCS_REG_STATUS] & KCS_STATUS_IBF)); |
| 314 | assert(ctx.kcs[MCTP_ASTLPC_KCS_REG_DATA] == 0x02); |
| 315 | assert(ctx.kcs[MCTP_ASTLPC_KCS_REG_STATUS] & KCS_STATUS_OBF); |
| 316 | |
| 317 | /* Host dequeues ownership hand-over and sends the queued packet */ |
| 318 | rc = mctp_astlpc_poll(ctx.host.astlpc); |
| 319 | assert(rc == 0); |
| 320 | |
| 321 | network_destroy(&ctx); |
| 322 | } |
| 323 | |
Andrew Jeffery | 8f3eb72 | 2020-05-22 20:23:49 +0930 | [diff] [blame] | 324 | static void astlpc_test_simple_message_bmc_to_host(void) |
| 325 | { |
| 326 | struct astlpc_test ctx = { 0 }; |
| 327 | uint8_t msg[MCTP_BTU]; |
| 328 | int rc; |
| 329 | |
| 330 | /* Test harness initialisation */ |
| 331 | |
| 332 | network_init(&ctx); |
| 333 | |
| 334 | memset(&msg[0], 0x5a, MCTP_BTU); |
| 335 | |
| 336 | ctx.msg = &msg[0]; |
| 337 | ctx.count = 0; |
| 338 | mctp_set_rx_all(ctx.host.mctp, rx_message, &ctx); |
| 339 | |
| 340 | /* BMC sends the single-packet message */ |
| 341 | rc = mctp_message_tx(ctx.bmc.mctp, 9, msg, sizeof(msg)); |
| 342 | assert(rc == 0); |
| 343 | assert(ctx.kcs[MCTP_ASTLPC_KCS_REG_STATUS] & KCS_STATUS_OBF); |
| 344 | assert(ctx.kcs[MCTP_ASTLPC_KCS_REG_DATA] == 0x01); |
| 345 | |
| 346 | astlpc_assert_tx_packet(&ctx.bmc, &msg[0], MCTP_BTU); |
| 347 | |
| 348 | /* Host receives the single-packet message */ |
| 349 | mctp_astlpc_poll(ctx.host.astlpc); |
| 350 | assert(ctx.count == 1); |
| 351 | |
| 352 | /* Host returns Rx area ownership to BMC */ |
| 353 | assert(!(ctx.kcs[MCTP_ASTLPC_KCS_REG_STATUS] & KCS_STATUS_OBF)); |
| 354 | assert(ctx.kcs[MCTP_ASTLPC_KCS_REG_DATA] == 0x02); |
| 355 | assert(ctx.kcs[MCTP_ASTLPC_KCS_REG_STATUS] & KCS_STATUS_IBF); |
| 356 | |
| 357 | /* BMC dequeues ownership hand-over and sends the queued packet */ |
| 358 | rc = mctp_astlpc_poll(ctx.bmc.astlpc); |
| 359 | assert(rc == 0); |
| 360 | |
| 361 | network_destroy(&ctx); |
| 362 | } |
| 363 | |
Andrew Jeffery | f1cdb16 | 2020-05-23 21:25:21 +0930 | [diff] [blame] | 364 | static void astlpc_test_host_before_bmc(void) |
| 365 | { |
| 366 | struct mctp_binding_astlpc_mmio mmio = { 0 }; |
| 367 | struct mctp_binding_astlpc *astlpc; |
| 368 | uint8_t kcs[2] = { 0 }; |
| 369 | struct mctp *mctp; |
| 370 | int rc; |
| 371 | |
| 372 | mctp = mctp_init(); |
| 373 | assert(mctp); |
| 374 | |
| 375 | /* Inject KCS registers */ |
| 376 | mmio.kcs = &kcs; |
| 377 | |
| 378 | /* Initialise the binding */ |
| 379 | astlpc = mctp_astlpc_init(MCTP_BINDING_ASTLPC_MODE_HOST, MCTP_BTU, NULL, |
Andrew Jeffery | 55fb90b | 2020-05-12 13:54:37 +0930 | [diff] [blame] | 380 | &astlpc_direct_mmio_ops, &mmio); |
Andrew Jeffery | f1cdb16 | 2020-05-23 21:25:21 +0930 | [diff] [blame] | 381 | |
| 382 | /* Register the binding to trigger the start-up sequence */ |
| 383 | rc = mctp_register_bus(mctp, &astlpc->binding, 8); |
| 384 | |
| 385 | /* Start-up should fail as we haven't initialised the BMC */ |
| 386 | assert(rc < 0); |
| 387 | |
| 388 | mctp_astlpc_destroy(astlpc); |
| 389 | mctp_destroy(mctp); |
| 390 | } |
| 391 | |
Andrew Jeffery | 4e8264b | 2020-05-23 20:34:33 +0930 | [diff] [blame] | 392 | static void astlpc_test_bad_version(void) |
| 393 | { |
| 394 | assert(0 == |
| 395 | mctp_astlpc_negotiate_version(ASTLPC_VER_BAD, ASTLPC_VER_CUR, |
| 396 | ASTLPC_VER_MIN, ASTLPC_VER_CUR)); |
| 397 | assert(0 == |
| 398 | mctp_astlpc_negotiate_version(ASTLPC_VER_MIN, ASTLPC_VER_BAD, |
| 399 | ASTLPC_VER_MIN, ASTLPC_VER_CUR)); |
| 400 | assert(0 == |
| 401 | mctp_astlpc_negotiate_version(ASTLPC_VER_MIN, ASTLPC_VER_CUR, |
| 402 | ASTLPC_VER_BAD, ASTLPC_VER_CUR)); |
| 403 | assert(0 == |
| 404 | mctp_astlpc_negotiate_version(ASTLPC_VER_MIN, ASTLPC_VER_CUR, |
| 405 | ASTLPC_VER_MIN, ASTLPC_VER_BAD)); |
| 406 | assert(0 == mctp_astlpc_negotiate_version( |
| 407 | ASTLPC_VER_CUR + 1, ASTLPC_VER_CUR, ASTLPC_VER_MIN, |
| 408 | ASTLPC_VER_CUR + 1)); |
| 409 | assert(0 == mctp_astlpc_negotiate_version( |
| 410 | ASTLPC_VER_MIN, ASTLPC_VER_CUR + 1, |
| 411 | ASTLPC_VER_CUR + 1, ASTLPC_VER_CUR)); |
| 412 | } |
| 413 | |
| 414 | static void astlpc_test_incompatible_versions(void) |
| 415 | { |
| 416 | assert(0 == mctp_astlpc_negotiate_version( |
| 417 | ASTLPC_VER_CUR, ASTLPC_VER_CUR, ASTLPC_VER_CUR + 1, |
| 418 | ASTLPC_VER_CUR + 1)); |
| 419 | assert(0 == mctp_astlpc_negotiate_version( |
| 420 | ASTLPC_VER_CUR + 1, ASTLPC_VER_CUR + 1, |
| 421 | ASTLPC_VER_CUR, ASTLPC_VER_CUR)); |
| 422 | } |
| 423 | |
| 424 | static void astlpc_test_choose_bmc_ver_cur(void) |
| 425 | { |
| 426 | assert(2 == mctp_astlpc_negotiate_version(1, 2, 2, 3)); |
| 427 | } |
| 428 | |
| 429 | static void astlpc_test_choose_host_ver_cur(void) |
| 430 | { |
| 431 | assert(2 == mctp_astlpc_negotiate_version(2, 3, 1, 2)); |
| 432 | } |
| 433 | |
| 434 | static void astlpc_test_version_host_fails_negotiation(void) |
Andrew Jeffery | f1a2131 | 2020-05-22 12:48:21 +0930 | [diff] [blame] | 435 | { |
| 436 | struct astlpc_endpoint bmc, host; |
Andrew Jeffery | 4e8264b | 2020-05-23 20:34:33 +0930 | [diff] [blame] | 437 | struct mctp_lpcmap_hdr *hdr; |
Andrew Jeffery | f1a2131 | 2020-05-22 12:48:21 +0930 | [diff] [blame] | 438 | uint8_t kcs[2] = { 0 }; |
Andrew Jeffery | f1a2131 | 2020-05-22 12:48:21 +0930 | [diff] [blame] | 439 | void *lpc_mem; |
Andrew Jeffery | 4e8264b | 2020-05-23 20:34:33 +0930 | [diff] [blame] | 440 | int rc; |
Andrew Jeffery | f1a2131 | 2020-05-22 12:48:21 +0930 | [diff] [blame] | 441 | |
| 442 | /* Test harness initialisation */ |
Andrew Jeffery | 55fb90b | 2020-05-12 13:54:37 +0930 | [diff] [blame] | 443 | lpc_mem = calloc(1, 1 * 1024 * 1024); |
Andrew Jeffery | f1a2131 | 2020-05-22 12:48:21 +0930 | [diff] [blame] | 444 | assert(lpc_mem); |
| 445 | |
Andrew Jeffery | 8f3eb72 | 2020-05-22 20:23:49 +0930 | [diff] [blame] | 446 | /* BMC initialisation */ |
Andrew Jeffery | a936898 | 2020-06-09 13:07:39 +0930 | [diff] [blame] | 447 | rc = endpoint_init(&bmc, 8, MCTP_BINDING_ASTLPC_MODE_BMC, MCTP_BTU, |
| 448 | &kcs, lpc_mem); |
Andrew Jeffery | 4e8264b | 2020-05-23 20:34:33 +0930 | [diff] [blame] | 449 | assert(!rc); |
| 450 | |
| 451 | /* Now the BMC is initialised, break its version announcement */ |
| 452 | hdr = lpc_mem; |
| 453 | hdr->bmc_ver_cur = ASTLPC_VER_BAD; |
| 454 | |
| 455 | /* Host initialisation */ |
Andrew Jeffery | a936898 | 2020-06-09 13:07:39 +0930 | [diff] [blame] | 456 | rc = endpoint_init(&host, 9, MCTP_BINDING_ASTLPC_MODE_HOST, MCTP_BTU, |
| 457 | &kcs, lpc_mem); |
Andrew Jeffery | 4e8264b | 2020-05-23 20:34:33 +0930 | [diff] [blame] | 458 | assert(rc < 0); |
| 459 | |
| 460 | endpoint_destroy(&bmc); |
| 461 | endpoint_destroy(&host); |
| 462 | free(lpc_mem); |
| 463 | } |
| 464 | |
| 465 | static void astlpc_test_version_bmc_fails_negotiation(void) |
| 466 | { |
| 467 | struct astlpc_endpoint bmc, host; |
| 468 | struct mctp_lpcmap_hdr *hdr; |
| 469 | uint8_t kcs[2] = { 0 }; |
| 470 | void *lpc_mem; |
| 471 | int rc; |
| 472 | |
| 473 | /* Test harness initialisation */ |
| 474 | lpc_mem = calloc(1, 1 * 1024 * 1024); |
| 475 | assert(lpc_mem); |
| 476 | |
| 477 | /* BMC initialisation */ |
Andrew Jeffery | a936898 | 2020-06-09 13:07:39 +0930 | [diff] [blame] | 478 | rc = endpoint_init(&bmc, 8, MCTP_BINDING_ASTLPC_MODE_BMC, MCTP_BTU, |
| 479 | &kcs, lpc_mem); |
Andrew Jeffery | 4e8264b | 2020-05-23 20:34:33 +0930 | [diff] [blame] | 480 | assert(!rc); |
| 481 | |
| 482 | /* Host initialisation */ |
Andrew Jeffery | a936898 | 2020-06-09 13:07:39 +0930 | [diff] [blame] | 483 | rc = endpoint_init(&host, 9, MCTP_BINDING_ASTLPC_MODE_HOST, MCTP_BTU, |
| 484 | &kcs, lpc_mem); |
Andrew Jeffery | 4e8264b | 2020-05-23 20:34:33 +0930 | [diff] [blame] | 485 | assert(!rc); |
| 486 | |
| 487 | /* Now the host is initialised, break its version announcement */ |
| 488 | hdr = lpc_mem; |
| 489 | hdr->host_ver_cur = ASTLPC_VER_BAD; |
| 490 | |
| 491 | /* Poll the BMC to detect the broken host version */ |
| 492 | mctp_astlpc_poll(bmc.astlpc); |
| 493 | assert(!(kcs[MCTP_ASTLPC_KCS_REG_STATUS] & KCS_STATUS_CHANNEL_ACTIVE)); |
| 494 | |
| 495 | /* Poll the host so it detects failed negotiation */ |
| 496 | rc = mctp_astlpc_poll(host.astlpc); |
| 497 | assert(rc < 0); |
| 498 | |
| 499 | endpoint_destroy(&bmc); |
| 500 | endpoint_destroy(&host); |
| 501 | free(lpc_mem); |
| 502 | } |
| 503 | |
| 504 | static void astlpc_test_simple_init(void) |
| 505 | { |
| 506 | struct astlpc_endpoint bmc, host; |
| 507 | uint8_t kcs[2] = { 0 }; |
| 508 | void *lpc_mem; |
| 509 | int rc; |
| 510 | |
| 511 | /* Test harness initialisation */ |
| 512 | lpc_mem = calloc(1, 1 * 1024 * 1024); |
| 513 | assert(lpc_mem); |
| 514 | |
| 515 | /* BMC initialisation */ |
Andrew Jeffery | a936898 | 2020-06-09 13:07:39 +0930 | [diff] [blame] | 516 | rc = endpoint_init(&bmc, 8, MCTP_BINDING_ASTLPC_MODE_BMC, MCTP_BTU, |
| 517 | &kcs, lpc_mem); |
Andrew Jeffery | 4e8264b | 2020-05-23 20:34:33 +0930 | [diff] [blame] | 518 | assert(!rc); |
Andrew Jeffery | f1a2131 | 2020-05-22 12:48:21 +0930 | [diff] [blame] | 519 | |
| 520 | /* Verify the BMC binding was initialised */ |
| 521 | assert(kcs[MCTP_ASTLPC_KCS_REG_STATUS] & KCS_STATUS_BMC_READY); |
| 522 | |
Andrew Jeffery | 8f3eb72 | 2020-05-22 20:23:49 +0930 | [diff] [blame] | 523 | /* Host initialisation */ |
Andrew Jeffery | a936898 | 2020-06-09 13:07:39 +0930 | [diff] [blame] | 524 | rc = endpoint_init(&host, 9, MCTP_BINDING_ASTLPC_MODE_HOST, MCTP_BTU, |
| 525 | &kcs, lpc_mem); |
Andrew Jeffery | 4e8264b | 2020-05-23 20:34:33 +0930 | [diff] [blame] | 526 | assert(!rc); |
Andrew Jeffery | f1a2131 | 2020-05-22 12:48:21 +0930 | [diff] [blame] | 527 | |
| 528 | /* Host sends channel init command */ |
| 529 | assert(kcs[MCTP_ASTLPC_KCS_REG_STATUS] & KCS_STATUS_IBF); |
| 530 | assert(kcs[MCTP_ASTLPC_KCS_REG_DATA] == 0x00); |
| 531 | |
| 532 | /* BMC receives host channel init request */ |
| 533 | mctp_astlpc_poll(bmc.astlpc); |
| 534 | |
| 535 | /* BMC sends init response */ |
| 536 | assert(kcs[MCTP_ASTLPC_KCS_REG_STATUS] & KCS_STATUS_OBF); |
| 537 | assert(kcs[MCTP_ASTLPC_KCS_REG_STATUS] & KCS_STATUS_CHANNEL_ACTIVE); |
| 538 | assert(kcs[MCTP_ASTLPC_KCS_REG_DATA] == 0xff); |
| 539 | |
| 540 | /* Host dequeues data */ |
| 541 | mctp_astlpc_poll(host.astlpc); |
| 542 | |
| 543 | endpoint_destroy(&bmc); |
| 544 | endpoint_destroy(&host); |
| 545 | free(lpc_mem); |
| 546 | } |
| 547 | |
Andrew Jeffery | 55fb90b | 2020-05-12 13:54:37 +0930 | [diff] [blame] | 548 | static void astlpc_test_simple_indirect_message_bmc_to_host(void) |
| 549 | { |
| 550 | struct astlpc_test ctx = { 0 }; |
| 551 | uint8_t kcs[2] = { 0 }; |
| 552 | uint8_t msg[MCTP_BTU]; |
| 553 | int rc; |
| 554 | |
| 555 | ctx.lpc_mem = calloc(1, LPC_WIN_SIZE); |
| 556 | assert(ctx.lpc_mem); |
| 557 | |
| 558 | /* Test message data */ |
| 559 | memset(&msg[0], 0x5a, MCTP_BTU); |
| 560 | |
| 561 | /* Manually set up the network so we can inject the indirect ops */ |
| 562 | |
| 563 | /* BMC initialisation */ |
| 564 | ctx.bmc.mmio.bmc = true; |
| 565 | ctx.bmc.mctp = mctp_init(); |
| 566 | assert(ctx.bmc.mctp); |
| 567 | ctx.bmc.mmio.kcs = &kcs; |
| 568 | ctx.bmc.mmio.lpc = ctx.lpc_mem; |
| 569 | ctx.bmc.mmio.lpc_size = LPC_WIN_SIZE; |
| 570 | ctx.bmc.astlpc = |
| 571 | mctp_astlpc_init(MCTP_BINDING_ASTLPC_MODE_BMC, MCTP_BTU, NULL, |
| 572 | &astlpc_indirect_mmio_ops, &ctx.bmc.mmio); |
| 573 | mctp_register_bus(ctx.bmc.mctp, &ctx.bmc.astlpc->binding, 8); |
| 574 | |
| 575 | /* Host initialisation */ |
| 576 | ctx.host.mmio.bmc = false; |
| 577 | ctx.host.mctp = mctp_init(); |
| 578 | assert(ctx.host.mctp); |
| 579 | ctx.host.mmio.kcs = &kcs; |
| 580 | ctx.host.mmio.lpc = ctx.lpc_mem; |
| 581 | ctx.host.mmio.lpc_size = LPC_WIN_SIZE; |
| 582 | ctx.host.astlpc = |
| 583 | mctp_astlpc_init(MCTP_BINDING_ASTLPC_MODE_HOST, MCTP_BTU, NULL, |
| 584 | &astlpc_indirect_mmio_ops, &ctx.host.mmio); |
| 585 | mctp_register_bus(ctx.host.mctp, &ctx.host.astlpc->binding, 9); |
| 586 | |
| 587 | /* BMC processes host channel init request, alerts host */ |
| 588 | mctp_astlpc_poll(ctx.bmc.astlpc); |
| 589 | |
| 590 | /* Host dequeues channel init result */ |
| 591 | mctp_astlpc_poll(ctx.host.astlpc); |
| 592 | |
| 593 | ctx.msg = &msg[0]; |
| 594 | ctx.count = 0; |
| 595 | mctp_set_rx_all(ctx.host.mctp, rx_message, &ctx); |
| 596 | |
| 597 | /* BMC sends the single-packet message */ |
| 598 | rc = mctp_message_tx(ctx.bmc.mctp, 9, msg, sizeof(msg)); |
| 599 | assert(rc == 0); |
| 600 | |
| 601 | /* Host receives the single-packet message */ |
| 602 | rc = mctp_astlpc_poll(ctx.host.astlpc); |
| 603 | assert(rc == 0); |
| 604 | assert(ctx.count == 1); |
| 605 | |
| 606 | /* BMC dequeues ownership hand-over and sends the queued packet */ |
| 607 | rc = mctp_astlpc_poll(ctx.bmc.astlpc); |
| 608 | assert(rc == 0); |
| 609 | |
| 610 | /* Can still tear-down the network in the normal fashion */ |
| 611 | network_destroy(&ctx); |
| 612 | } |
| 613 | |
Andrew Jeffery | d0f5da0 | 2020-05-28 09:12:55 +0930 | [diff] [blame] | 614 | static void astlpc_test_host_tx_bmc_gone(void) |
| 615 | { |
| 616 | struct astlpc_test ctx = { 0 }; |
| 617 | uint8_t unwritten[MCTP_BTU]; |
| 618 | uint8_t msg[MCTP_BTU]; |
| 619 | int rc; |
| 620 | |
| 621 | /* Test harness initialisation */ |
| 622 | |
| 623 | network_init(&ctx); |
| 624 | |
| 625 | memset(&msg[0], 0x5a, sizeof(msg)); |
| 626 | memset(&unwritten[0], 0, sizeof(unwritten)); |
| 627 | |
| 628 | ctx.msg = &msg[0]; |
| 629 | ctx.count = 0; |
| 630 | |
| 631 | /* Clear bmc-ready */ |
| 632 | endpoint_destroy(&ctx.bmc); |
| 633 | |
| 634 | /* Host detects that the BMC is disabled */ |
| 635 | mctp_astlpc_poll(ctx.host.astlpc); |
| 636 | |
| 637 | /* Host attempts to send the single-packet message, but is prevented */ |
| 638 | rc = mctp_message_tx(ctx.host.mctp, 8, msg, sizeof(msg)); |
| 639 | assert(rc == 0); |
| 640 | assert(!(ctx.kcs[MCTP_ASTLPC_KCS_REG_STATUS] & KCS_STATUS_OBF)); |
| 641 | astlpc_assert_tx_packet(&ctx.host, &unwritten[0], MCTP_BTU); |
| 642 | |
| 643 | /* BMC comes back */ |
Andrew Jeffery | a936898 | 2020-06-09 13:07:39 +0930 | [diff] [blame] | 644 | rc = endpoint_init(&ctx.bmc, 8, MCTP_BINDING_ASTLPC_MODE_BMC, MCTP_BTU, |
| 645 | &ctx.kcs, ctx.lpc_mem); |
Andrew Jeffery | 4e8264b | 2020-05-23 20:34:33 +0930 | [diff] [blame] | 646 | assert(!rc); |
Andrew Jeffery | d0f5da0 | 2020-05-28 09:12:55 +0930 | [diff] [blame] | 647 | mctp_set_rx_all(ctx.bmc.mctp, rx_message, &ctx); |
| 648 | |
| 649 | /* Host triggers channel init */ |
| 650 | mctp_astlpc_poll(ctx.host.astlpc); |
| 651 | |
| 652 | /* BMC handles channel init */ |
| 653 | mctp_astlpc_poll(ctx.bmc.astlpc); |
| 654 | |
| 655 | /* Host completes channel init, flushing the Tx queue */ |
| 656 | mctp_astlpc_poll(ctx.host.astlpc); |
| 657 | |
| 658 | /* BMC receives the single-packet message */ |
| 659 | mctp_astlpc_poll(ctx.bmc.astlpc); |
| 660 | assert(ctx.count == 1); |
| 661 | |
| 662 | network_destroy(&ctx); |
| 663 | } |
| 664 | |
Andrew Jeffery | f3d94dc | 2020-05-28 10:50:18 +0930 | [diff] [blame] | 665 | static void astlpc_test_poll_not_ready(void) |
| 666 | { |
| 667 | struct astlpc_endpoint bmc; |
| 668 | uint8_t kcs[2] = { 0 }; |
| 669 | void *lpc_mem; |
| 670 | int rc; |
| 671 | |
| 672 | /* Test harness initialisation */ |
| 673 | lpc_mem = calloc(1, 1 * 1024 * 1024); |
| 674 | assert(lpc_mem); |
| 675 | |
| 676 | /* BMC initialisation */ |
Andrew Jeffery | a936898 | 2020-06-09 13:07:39 +0930 | [diff] [blame] | 677 | rc = endpoint_init(&bmc, 8, MCTP_BINDING_ASTLPC_MODE_BMC, MCTP_BTU, |
| 678 | &kcs, lpc_mem); |
Andrew Jeffery | 4e8264b | 2020-05-23 20:34:33 +0930 | [diff] [blame] | 679 | assert(!rc); |
Andrew Jeffery | f3d94dc | 2020-05-28 10:50:18 +0930 | [diff] [blame] | 680 | |
| 681 | /* Check for a command despite none present */ |
| 682 | rc = mctp_astlpc_poll(bmc.astlpc); |
| 683 | |
| 684 | /* Make sure it doesn't fail */ |
| 685 | assert(rc == 0); |
| 686 | |
| 687 | endpoint_destroy(&bmc); |
| 688 | free(lpc_mem); |
| 689 | } |
| 690 | |
Andrew Jeffery | 67655e8 | 2020-05-28 10:51:27 +0930 | [diff] [blame] | 691 | static void astlpc_test_undefined_command(void) |
| 692 | { |
| 693 | struct astlpc_endpoint bmc; |
| 694 | uint8_t kcs[2] = { 0 }; |
| 695 | void *lpc_mem; |
| 696 | int rc; |
| 697 | |
| 698 | /* Test harness initialisation */ |
| 699 | lpc_mem = calloc(1, 1 * 1024 * 1024); |
| 700 | assert(lpc_mem); |
| 701 | |
| 702 | /* BMC initialisation */ |
Andrew Jeffery | a936898 | 2020-06-09 13:07:39 +0930 | [diff] [blame] | 703 | rc = endpoint_init(&bmc, 8, MCTP_BINDING_ASTLPC_MODE_BMC, MCTP_BTU, |
| 704 | &kcs, lpc_mem); |
Andrew Jeffery | 4e8264b | 2020-05-23 20:34:33 +0930 | [diff] [blame] | 705 | assert(!rc); |
Andrew Jeffery | 67655e8 | 2020-05-28 10:51:27 +0930 | [diff] [blame] | 706 | |
| 707 | /* 0x5a isn't legal in v1 or v2 */ |
| 708 | kcs[MCTP_ASTLPC_KCS_REG_DATA] = 0x5a; |
| 709 | kcs[MCTP_ASTLPC_KCS_REG_STATUS] |= KCS_STATUS_IBF; |
| 710 | |
| 711 | /* Check for a command despite none present */ |
| 712 | rc = mctp_astlpc_poll(bmc.astlpc); |
| 713 | |
| 714 | /* Make sure it doesn't fail, bad command should be discarded */ |
| 715 | assert(rc == 0); |
| 716 | |
| 717 | endpoint_destroy(&bmc); |
| 718 | free(lpc_mem); |
| 719 | } |
| 720 | |
Andrew Jeffery | eba19a3 | 2021-03-09 23:09:40 +1030 | [diff] [blame] | 721 | #define BUFFER_MIN (MCTP_PACKET_SIZE(MCTP_BTU) + 4 + 4) |
Andrew Jeffery | 88412be | 2021-03-09 22:05:22 +1030 | [diff] [blame] | 722 | static const struct mctp_binding_astlpc astlpc_layout_ctx = { |
Andrew Jeffery | eba19a3 | 2021-03-09 23:09:40 +1030 | [diff] [blame] | 723 | .proto = &astlpc_protocol_version[3], |
Andrew Jeffery | 88412be | 2021-03-09 22:05:22 +1030 | [diff] [blame] | 724 | }; |
Andrew Jeffery | 3a54066 | 2020-05-26 19:55:30 +0930 | [diff] [blame] | 725 | |
| 726 | static void astlpc_test_buffers_rx_offset_overflow(void) |
| 727 | { |
| 728 | struct mctp_astlpc_layout l = { |
| 729 | .rx = { UINT32_MAX, BUFFER_MIN }, |
| 730 | .tx = { control_size, BUFFER_MIN }, |
| 731 | }; |
| 732 | |
Andrew Jeffery | 88412be | 2021-03-09 22:05:22 +1030 | [diff] [blame] | 733 | assert(!mctp_astlpc_layout_validate(&astlpc_layout_ctx, &l)); |
Andrew Jeffery | 3a54066 | 2020-05-26 19:55:30 +0930 | [diff] [blame] | 734 | } |
| 735 | |
| 736 | static void astlpc_test_buffers_tx_offset_overflow(void) |
| 737 | { |
| 738 | struct mctp_astlpc_layout l = { |
| 739 | .rx = { control_size, BUFFER_MIN }, |
| 740 | .tx = { UINT32_MAX, BUFFER_MIN }, |
| 741 | }; |
| 742 | |
Andrew Jeffery | 88412be | 2021-03-09 22:05:22 +1030 | [diff] [blame] | 743 | assert(!mctp_astlpc_layout_validate(&astlpc_layout_ctx, &l)); |
Andrew Jeffery | 3a54066 | 2020-05-26 19:55:30 +0930 | [diff] [blame] | 744 | } |
| 745 | |
| 746 | static void astlpc_test_buffers_rx_size_overflow(void) |
| 747 | { |
| 748 | struct mctp_astlpc_layout l = { |
| 749 | .rx = { control_size + BUFFER_MIN, UINT32_MAX }, |
| 750 | .tx = { control_size, BUFFER_MIN }, |
| 751 | }; |
| 752 | |
Andrew Jeffery | 88412be | 2021-03-09 22:05:22 +1030 | [diff] [blame] | 753 | assert(!mctp_astlpc_layout_validate(&astlpc_layout_ctx, &l)); |
Andrew Jeffery | 3a54066 | 2020-05-26 19:55:30 +0930 | [diff] [blame] | 754 | } |
| 755 | |
| 756 | static void astlpc_test_buffers_tx_size_overflow(void) |
| 757 | { |
| 758 | struct mctp_astlpc_layout l = { |
| 759 | .rx = { control_size, BUFFER_MIN }, |
| 760 | .tx = { control_size + BUFFER_MIN, UINT32_MAX }, |
| 761 | }; |
| 762 | |
Andrew Jeffery | 88412be | 2021-03-09 22:05:22 +1030 | [diff] [blame] | 763 | assert(!mctp_astlpc_layout_validate(&astlpc_layout_ctx, &l)); |
Andrew Jeffery | 3a54066 | 2020-05-26 19:55:30 +0930 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | static void astlpc_test_buffers_rx_window_violation(void) |
| 767 | { |
| 768 | struct mctp_astlpc_layout l = { |
| 769 | .rx = { LPC_WIN_SIZE - BUFFER_MIN + 1, BUFFER_MIN }, |
| 770 | .tx = { control_size, BUFFER_MIN }, |
| 771 | }; |
| 772 | |
Andrew Jeffery | 88412be | 2021-03-09 22:05:22 +1030 | [diff] [blame] | 773 | assert(!mctp_astlpc_layout_validate(&astlpc_layout_ctx, &l)); |
Andrew Jeffery | 3a54066 | 2020-05-26 19:55:30 +0930 | [diff] [blame] | 774 | } |
| 775 | |
| 776 | static void astlpc_test_buffers_tx_window_violation(void) |
| 777 | { |
| 778 | struct mctp_astlpc_layout l = { |
| 779 | .rx = { control_size, BUFFER_MIN }, |
| 780 | .tx = { LPC_WIN_SIZE - BUFFER_MIN + 1, BUFFER_MIN }, |
| 781 | }; |
| 782 | |
Andrew Jeffery | 88412be | 2021-03-09 22:05:22 +1030 | [diff] [blame] | 783 | assert(!mctp_astlpc_layout_validate(&astlpc_layout_ctx, &l)); |
Andrew Jeffery | 3a54066 | 2020-05-26 19:55:30 +0930 | [diff] [blame] | 784 | } |
| 785 | |
| 786 | static void astlpc_test_buffers_rx_size_fails_btu(void) |
| 787 | { |
| 788 | struct mctp_astlpc_layout l = { |
| 789 | .rx = { control_size, BUFFER_MIN - 1 }, |
| 790 | .tx = { control_size + BUFFER_MIN, BUFFER_MIN }, |
| 791 | }; |
| 792 | |
Andrew Jeffery | 88412be | 2021-03-09 22:05:22 +1030 | [diff] [blame] | 793 | assert(!mctp_astlpc_layout_validate(&astlpc_layout_ctx, &l)); |
Andrew Jeffery | 3a54066 | 2020-05-26 19:55:30 +0930 | [diff] [blame] | 794 | } |
| 795 | |
| 796 | static void astlpc_test_buffers_tx_size_fails_btu(void) |
| 797 | { |
| 798 | struct mctp_astlpc_layout l = { |
| 799 | .rx = { control_size, BUFFER_MIN }, |
| 800 | .tx = { control_size + BUFFER_MIN, BUFFER_MIN - 1 }, |
| 801 | }; |
| 802 | |
Andrew Jeffery | 88412be | 2021-03-09 22:05:22 +1030 | [diff] [blame] | 803 | assert(!mctp_astlpc_layout_validate(&astlpc_layout_ctx, &l)); |
Andrew Jeffery | 3a54066 | 2020-05-26 19:55:30 +0930 | [diff] [blame] | 804 | } |
| 805 | |
| 806 | static void astlpc_test_buffers_overlap_rx_low(void) |
| 807 | { |
| 808 | struct mctp_astlpc_layout l = { |
| 809 | .rx = { control_size, 2 * BUFFER_MIN }, |
| 810 | .tx = { control_size + BUFFER_MIN, 2 * BUFFER_MIN }, |
| 811 | }; |
| 812 | |
Andrew Jeffery | 88412be | 2021-03-09 22:05:22 +1030 | [diff] [blame] | 813 | assert(!mctp_astlpc_layout_validate(&astlpc_layout_ctx, &l)); |
Andrew Jeffery | 3a54066 | 2020-05-26 19:55:30 +0930 | [diff] [blame] | 814 | } |
| 815 | |
| 816 | static void astlpc_test_buffers_overlap_tx_low(void) |
| 817 | { |
| 818 | struct mctp_astlpc_layout l = { |
| 819 | .rx = { control_size + BUFFER_MIN, 2 * BUFFER_MIN }, |
| 820 | .tx = { control_size, 2 * BUFFER_MIN }, |
| 821 | }; |
| 822 | |
Andrew Jeffery | 88412be | 2021-03-09 22:05:22 +1030 | [diff] [blame] | 823 | assert(!mctp_astlpc_layout_validate(&astlpc_layout_ctx, &l)); |
Andrew Jeffery | 3a54066 | 2020-05-26 19:55:30 +0930 | [diff] [blame] | 824 | } |
| 825 | |
| 826 | static void astlpc_test_buffers_overlap_exact(void) |
| 827 | { |
| 828 | struct mctp_astlpc_layout l = { |
| 829 | .rx = { control_size, 2 * BUFFER_MIN }, |
| 830 | .tx = { control_size, 2 * BUFFER_MIN }, |
| 831 | }; |
| 832 | |
Andrew Jeffery | 88412be | 2021-03-09 22:05:22 +1030 | [diff] [blame] | 833 | assert(!mctp_astlpc_layout_validate(&astlpc_layout_ctx, &l)); |
Andrew Jeffery | 3a54066 | 2020-05-26 19:55:30 +0930 | [diff] [blame] | 834 | } |
| 835 | |
| 836 | static void astlpc_test_buffers_overlap_control(void) |
| 837 | { |
| 838 | struct mctp_astlpc_layout l = { |
| 839 | .rx = { 0, BUFFER_MIN }, |
| 840 | .tx = { control_size + BUFFER_MIN, BUFFER_MIN }, |
| 841 | }; |
| 842 | |
Andrew Jeffery | 88412be | 2021-03-09 22:05:22 +1030 | [diff] [blame] | 843 | assert(!mctp_astlpc_layout_validate(&astlpc_layout_ctx, &l)); |
Andrew Jeffery | 3a54066 | 2020-05-26 19:55:30 +0930 | [diff] [blame] | 844 | } |
| 845 | |
| 846 | static void astlpc_test_buffers_bad_host_proposal(void) |
| 847 | { |
| 848 | struct astlpc_endpoint bmc, host; |
| 849 | struct mctp_lpcmap_hdr *hdr; |
| 850 | uint8_t kcs[2] = { 0 }; |
| 851 | void *lpc_mem; |
Andrew Jeffery | a936898 | 2020-06-09 13:07:39 +0930 | [diff] [blame] | 852 | int rc; |
Andrew Jeffery | 3a54066 | 2020-05-26 19:55:30 +0930 | [diff] [blame] | 853 | |
| 854 | /* Test harness initialisation */ |
| 855 | lpc_mem = calloc(1, 1 * 1024 * 1024); |
| 856 | assert(lpc_mem); |
| 857 | |
| 858 | /* BMC initialisation */ |
Andrew Jeffery | a936898 | 2020-06-09 13:07:39 +0930 | [diff] [blame] | 859 | rc = endpoint_init(&bmc, 8, MCTP_BINDING_ASTLPC_MODE_BMC, MCTP_BTU, |
| 860 | &kcs, lpc_mem); |
| 861 | assert(!rc); |
Andrew Jeffery | 3a54066 | 2020-05-26 19:55:30 +0930 | [diff] [blame] | 862 | |
| 863 | /* Host initialisation */ |
Andrew Jeffery | a936898 | 2020-06-09 13:07:39 +0930 | [diff] [blame] | 864 | rc = endpoint_init(&host, 9, MCTP_BINDING_ASTLPC_MODE_HOST, MCTP_BTU, |
| 865 | &kcs, lpc_mem); |
| 866 | assert(!rc); |
Andrew Jeffery | 3a54066 | 2020-05-26 19:55:30 +0930 | [diff] [blame] | 867 | |
| 868 | /* |
| 869 | * Now that the host has initialised the control area, break |
| 870 | * something before polling the BMC |
| 871 | */ |
| 872 | hdr = lpc_mem; |
| 873 | hdr->layout.rx_size = 0; |
| 874 | |
| 875 | mctp_astlpc_poll(bmc.astlpc); |
| 876 | |
| 877 | /* Make sure the BMC has not set the channel to active */ |
| 878 | assert(!(kcs[MCTP_ASTLPC_KCS_REG_STATUS] & KCS_STATUS_CHANNEL_ACTIVE)); |
| 879 | |
| 880 | endpoint_destroy(&host); |
| 881 | endpoint_destroy(&bmc); |
| 882 | free(lpc_mem); |
| 883 | } |
| 884 | |
| 885 | static void astlpc_test_buffers_bad_bmc_proposal(void) |
| 886 | { |
| 887 | struct astlpc_endpoint bmc, host; |
| 888 | struct mctp_lpcmap_hdr *hdr; |
| 889 | uint8_t kcs[2] = { 0 }; |
| 890 | void *lpc_mem; |
| 891 | int rc; |
| 892 | |
| 893 | /* Test harness initialisation */ |
| 894 | lpc_mem = calloc(1, 1 * 1024 * 1024); |
| 895 | assert(lpc_mem); |
| 896 | |
| 897 | /* BMC initialisation */ |
Andrew Jeffery | a936898 | 2020-06-09 13:07:39 +0930 | [diff] [blame] | 898 | rc = endpoint_init(&bmc, 8, MCTP_BINDING_ASTLPC_MODE_BMC, MCTP_BTU, |
| 899 | &kcs, lpc_mem); |
| 900 | assert(!rc); |
Andrew Jeffery | 3a54066 | 2020-05-26 19:55:30 +0930 | [diff] [blame] | 901 | |
| 902 | /* |
| 903 | * Now that the BMC has initialised the control area, break something |
| 904 | * before initialising the host |
| 905 | */ |
| 906 | hdr = lpc_mem; |
| 907 | hdr->layout.rx_size = 0; |
| 908 | |
| 909 | /* Host initialisation: Fails due to bad layout */ |
Andrew Jeffery | a936898 | 2020-06-09 13:07:39 +0930 | [diff] [blame] | 910 | rc = endpoint_init(&host, 9, MCTP_BINDING_ASTLPC_MODE_HOST, MCTP_BTU, |
| 911 | &kcs, lpc_mem); |
Andrew Jeffery | 3a54066 | 2020-05-26 19:55:30 +0930 | [diff] [blame] | 912 | assert(rc < 0); |
| 913 | |
| 914 | endpoint_destroy(&host); |
| 915 | endpoint_destroy(&bmc); |
| 916 | free(lpc_mem); |
| 917 | } |
| 918 | |
| 919 | static void astlpc_test_buffers_bad_bmc_negotiation(void) |
| 920 | { |
| 921 | struct astlpc_endpoint bmc, host; |
| 922 | struct mctp_lpcmap_hdr *hdr; |
| 923 | uint8_t kcs[2] = { 0 }; |
| 924 | void *lpc_mem; |
| 925 | int rc; |
| 926 | |
| 927 | /* Test harness initialisation */ |
| 928 | lpc_mem = calloc(1, 1 * 1024 * 1024); |
| 929 | assert(lpc_mem); |
| 930 | |
| 931 | /* BMC initialisation */ |
Andrew Jeffery | a936898 | 2020-06-09 13:07:39 +0930 | [diff] [blame] | 932 | rc = endpoint_init(&bmc, 8, MCTP_BINDING_ASTLPC_MODE_BMC, MCTP_BTU, |
| 933 | &kcs, lpc_mem); |
| 934 | assert(!rc); |
Andrew Jeffery | 3a54066 | 2020-05-26 19:55:30 +0930 | [diff] [blame] | 935 | |
| 936 | /* Host initialisation */ |
Andrew Jeffery | a936898 | 2020-06-09 13:07:39 +0930 | [diff] [blame] | 937 | rc = endpoint_init(&host, 9, MCTP_BINDING_ASTLPC_MODE_HOST, MCTP_BTU, |
| 938 | &kcs, lpc_mem); |
| 939 | assert(!rc); |
Andrew Jeffery | 3a54066 | 2020-05-26 19:55:30 +0930 | [diff] [blame] | 940 | |
| 941 | mctp_astlpc_poll(bmc.astlpc); |
| 942 | |
| 943 | /* |
| 944 | * Now that the BMC has initialised the control area, break something |
| 945 | * before polling the host |
| 946 | */ |
| 947 | hdr = lpc_mem; |
| 948 | hdr->layout.rx_size = 0; |
| 949 | |
| 950 | rc = mctp_astlpc_poll(host.astlpc); |
| 951 | assert(rc < 0); |
| 952 | |
| 953 | endpoint_destroy(&host); |
| 954 | endpoint_destroy(&bmc); |
| 955 | free(lpc_mem); |
| 956 | } |
| 957 | |
Andrew Jeffery | a936898 | 2020-06-09 13:07:39 +0930 | [diff] [blame] | 958 | static void astlpc_test_buffers_bad_host_init(void) |
| 959 | { |
| 960 | struct astlpc_endpoint host; |
| 961 | uint8_t kcs[2] = { 0 }; |
| 962 | void *lpc_mem; |
| 963 | int rc; |
| 964 | |
| 965 | /* Test harness initialisation */ |
| 966 | lpc_mem = calloc(1, 1 * 1024 * 1024); |
| 967 | assert(lpc_mem); |
| 968 | |
| 969 | host.mctp = mctp_init(); |
| 970 | assert(host.mctp); |
| 971 | host.mmio.kcs = &kcs; |
| 972 | host.mmio.bmc = false; |
| 973 | |
| 974 | /* Set the MTU to 0 to provoke a failure */ |
| 975 | host.astlpc = |
| 976 | mctp_astlpc_init(MCTP_BINDING_ASTLPC_MODE_HOST, 0, lpc_mem, |
| 977 | &astlpc_direct_mmio_ops, &host.mmio); |
| 978 | |
| 979 | rc = mctp_register_bus(host.mctp, &host.astlpc->binding, 8); |
| 980 | assert(rc < 0); |
| 981 | |
| 982 | mctp_astlpc_destroy(host.astlpc); |
| 983 | mctp_destroy(host.mctp); |
| 984 | free(lpc_mem); |
| 985 | } |
| 986 | |
| 987 | static void astlpc_test_negotiate_increased_mtu(void) |
| 988 | { |
| 989 | struct astlpc_endpoint bmc, host; |
| 990 | uint8_t kcs[2] = { 0 }; |
| 991 | void *lpc_mem; |
| 992 | int rc; |
| 993 | |
| 994 | /* Test harness initialisation */ |
| 995 | lpc_mem = calloc(1, 1 * 1024 * 1024); |
| 996 | assert(lpc_mem); |
| 997 | |
| 998 | /* BMC initialisation */ |
| 999 | rc = endpoint_init(&bmc, 8, MCTP_BINDING_ASTLPC_MODE_BMC, 3 * MCTP_BTU, |
| 1000 | &kcs, lpc_mem); |
| 1001 | assert(!rc); |
| 1002 | |
| 1003 | /* Host initialisation */ |
| 1004 | rc = endpoint_init(&host, 9, MCTP_BINDING_ASTLPC_MODE_HOST, |
| 1005 | 2 * MCTP_BTU, &kcs, lpc_mem); |
| 1006 | assert(!rc); |
| 1007 | |
| 1008 | rc = mctp_astlpc_poll(bmc.astlpc); |
| 1009 | assert(rc == 0); |
| 1010 | |
| 1011 | rc = mctp_astlpc_poll(host.astlpc); |
| 1012 | assert(rc == 0); |
| 1013 | |
| 1014 | endpoint_destroy(&host); |
| 1015 | endpoint_destroy(&bmc); |
| 1016 | free(lpc_mem); |
| 1017 | } |
| 1018 | |
| 1019 | static void astlpc_test_negotiate_mtu_low_high(void) |
| 1020 | { |
| 1021 | struct astlpc_endpoint bmc, host; |
| 1022 | uint8_t kcs[2] = { 0 }; |
Andrew Jeffery | 88412be | 2021-03-09 22:05:22 +1030 | [diff] [blame] | 1023 | uint32_t bmtu, hmtu; |
Andrew Jeffery | a936898 | 2020-06-09 13:07:39 +0930 | [diff] [blame] | 1024 | void *lpc_mem; |
| 1025 | int rc; |
| 1026 | |
| 1027 | /* Test harness initialisation */ |
| 1028 | lpc_mem = calloc(1, 1 * 1024 * 1024); |
| 1029 | assert(lpc_mem); |
| 1030 | |
| 1031 | /* BMC initialisation */ |
Andrew Jeffery | 88412be | 2021-03-09 22:05:22 +1030 | [diff] [blame] | 1032 | bmtu = 3 * MCTP_BTU; |
| 1033 | rc = endpoint_init(&bmc, 8, MCTP_BINDING_ASTLPC_MODE_BMC, bmtu, &kcs, |
| 1034 | lpc_mem); |
Andrew Jeffery | a936898 | 2020-06-09 13:07:39 +0930 | [diff] [blame] | 1035 | assert(!rc); |
| 1036 | |
| 1037 | /* Host initialisation with low MTU */ |
Andrew Jeffery | 88412be | 2021-03-09 22:05:22 +1030 | [diff] [blame] | 1038 | hmtu = 2 * MCTP_BTU; |
| 1039 | rc = endpoint_init(&host, 9, MCTP_BINDING_ASTLPC_MODE_HOST, hmtu, &kcs, |
| 1040 | lpc_mem); |
Andrew Jeffery | a936898 | 2020-06-09 13:07:39 +0930 | [diff] [blame] | 1041 | assert(!rc); |
| 1042 | |
| 1043 | /* Process low MTU proposal */ |
| 1044 | rc = mctp_astlpc_poll(bmc.astlpc); |
| 1045 | assert(rc == 0); |
| 1046 | |
| 1047 | /* Accept low MTU proposal */ |
| 1048 | rc = mctp_astlpc_poll(host.astlpc); |
| 1049 | assert(rc == 0); |
| 1050 | |
| 1051 | assert(host.astlpc->layout.rx.size == |
Andrew Jeffery | 88412be | 2021-03-09 22:05:22 +1030 | [diff] [blame] | 1052 | astlpc_layout_ctx.proto->packet_size(MCTP_PACKET_SIZE(hmtu))); |
Andrew Jeffery | a936898 | 2020-06-09 13:07:39 +0930 | [diff] [blame] | 1053 | |
| 1054 | /* Tear-down the host so we can bring up a new one */ |
| 1055 | endpoint_destroy(&host); |
| 1056 | |
| 1057 | /* |
| 1058 | * Bring up a new host endpoint with a higher MTU than we previously |
| 1059 | * negotiated |
| 1060 | */ |
Andrew Jeffery | 88412be | 2021-03-09 22:05:22 +1030 | [diff] [blame] | 1061 | hmtu = 3 * MCTP_BTU; |
| 1062 | rc = endpoint_init(&host, 9, MCTP_BINDING_ASTLPC_MODE_HOST, hmtu, &kcs, |
| 1063 | lpc_mem); |
Andrew Jeffery | a936898 | 2020-06-09 13:07:39 +0930 | [diff] [blame] | 1064 | assert(!rc); |
| 1065 | |
| 1066 | /* Process high MTU proposal */ |
| 1067 | rc = mctp_astlpc_poll(bmc.astlpc); |
| 1068 | assert(rc == 0); |
| 1069 | |
| 1070 | /* Accept high MTU proposal */ |
| 1071 | rc = mctp_astlpc_poll(host.astlpc); |
| 1072 | assert(rc == 0); |
| 1073 | |
| 1074 | assert(host.astlpc->layout.rx.size == |
Andrew Jeffery | 88412be | 2021-03-09 22:05:22 +1030 | [diff] [blame] | 1075 | astlpc_layout_ctx.proto->packet_size(MCTP_PACKET_SIZE(bmtu))); |
Andrew Jeffery | a936898 | 2020-06-09 13:07:39 +0930 | [diff] [blame] | 1076 | |
| 1077 | endpoint_destroy(&host); |
| 1078 | endpoint_destroy(&bmc); |
| 1079 | free(lpc_mem); |
| 1080 | } |
| 1081 | |
Andrew Jeffery | c2b833e | 2020-10-28 14:28:37 +1030 | [diff] [blame] | 1082 | static void astlpc_test_send_large_packet(void) |
| 1083 | { |
| 1084 | struct astlpc_endpoint *bmc, *host; |
| 1085 | struct astlpc_test ctx; |
| 1086 | uint8_t kcs[2] = { 0 }; |
| 1087 | void *lpc_mem; |
| 1088 | int rc; |
| 1089 | |
| 1090 | host = &ctx.host; |
| 1091 | bmc = &ctx.bmc; |
| 1092 | |
| 1093 | /* Test harness initialisation */ |
| 1094 | lpc_mem = calloc(1, 1 * 1024 * 1024); |
| 1095 | assert(lpc_mem); |
| 1096 | |
| 1097 | /* BMC initialisation */ |
| 1098 | rc = endpoint_init(bmc, 8, MCTP_BINDING_ASTLPC_MODE_BMC, 8192, &kcs, |
| 1099 | lpc_mem); |
| 1100 | assert(!rc); |
| 1101 | |
| 1102 | /* Host initialisation */ |
| 1103 | rc = endpoint_init(host, 9, MCTP_BINDING_ASTLPC_MODE_HOST, 8192, &kcs, |
| 1104 | lpc_mem); |
| 1105 | assert(!rc); |
| 1106 | |
| 1107 | ctx.count = 0; |
| 1108 | mctp_set_rx_all(bmc->mctp, rx_message, &ctx); |
| 1109 | |
| 1110 | rc = mctp_astlpc_poll(bmc->astlpc); |
| 1111 | assert(rc == 0); |
| 1112 | |
| 1113 | rc = mctp_astlpc_poll(host->astlpc); |
| 1114 | assert(rc == 0); |
| 1115 | |
| 1116 | ctx.msg = malloc(2 * MCTP_BODY_SIZE(8192)); |
| 1117 | assert(ctx.msg); |
| 1118 | |
| 1119 | memset(ctx.msg, 0x5a, 2 * MCTP_BODY_SIZE(8192)); |
| 1120 | |
| 1121 | rc = mctp_message_tx(host->mctp, 8, ctx.msg, 2 * MCTP_BODY_SIZE(8192)); |
| 1122 | assert(rc == 0); |
| 1123 | rc = mctp_astlpc_poll(bmc->astlpc); |
| 1124 | assert(rc == 0); |
| 1125 | rc = mctp_astlpc_poll(host->astlpc); |
| 1126 | assert(rc == 0); |
| 1127 | rc = mctp_astlpc_poll(bmc->astlpc); |
| 1128 | assert(rc == 0); |
| 1129 | rc = mctp_astlpc_poll(host->astlpc); |
| 1130 | assert(rc == 0); |
| 1131 | |
| 1132 | assert(ctx.count == 1); |
| 1133 | |
| 1134 | free(ctx.msg); |
| 1135 | endpoint_destroy(host); |
| 1136 | endpoint_destroy(bmc); |
| 1137 | free(lpc_mem); |
| 1138 | } |
| 1139 | |
Andrew Jeffery | 3f4bca9 | 2021-01-28 14:19:19 +1030 | [diff] [blame] | 1140 | static void astlpc_test_tx_before_channel_init(void) |
| 1141 | { |
| 1142 | struct astlpc_endpoint *bmc; |
| 1143 | struct astlpc_test ctx; |
| 1144 | uint8_t kcs[2] = { 0 }; |
| 1145 | uint8_t msg[MCTP_BTU]; |
| 1146 | void *lpc_mem; |
| 1147 | int rc; |
| 1148 | |
| 1149 | bmc = &ctx.bmc; |
| 1150 | |
| 1151 | /* Test harness initialisation */ |
| 1152 | lpc_mem = calloc(1, 1 * 1024 * 1024); |
| 1153 | assert(lpc_mem); |
| 1154 | |
| 1155 | /* BMC initialisation */ |
| 1156 | rc = endpoint_init(bmc, 8, MCTP_BINDING_ASTLPC_MODE_BMC, 0, &kcs, |
| 1157 | lpc_mem); |
| 1158 | assert(!rc); |
| 1159 | |
| 1160 | memset(msg, '\0', sizeof(msg)); |
| 1161 | |
| 1162 | /* |
| 1163 | * There was once a bug where the calculated MTU was 0 and the |
| 1164 | * packetisation loop in mctp_message_tx_on_bus() allocated all the |
| 1165 | * memory. Catch the bug and avoid OOMing the test machine by |
| 1166 | * terminating after a period long enough to packetise the message. |
| 1167 | */ |
| 1168 | alarm(1); |
| 1169 | mctp_message_tx(bmc->mctp, 9, msg, sizeof(msg)); |
| 1170 | alarm(0); |
| 1171 | |
| 1172 | endpoint_destroy(bmc); |
| 1173 | free(lpc_mem); |
| 1174 | } |
| 1175 | |
Andrew Jeffery | eba19a3 | 2021-03-09 23:09:40 +1030 | [diff] [blame] | 1176 | static void astlpc_test_corrupt_host_tx(void) |
| 1177 | { |
| 1178 | struct astlpc_test ctx = { 0 }; |
| 1179 | struct mctp_lpcmap_hdr *hdr; |
| 1180 | uint8_t msg[MCTP_BTU]; |
| 1181 | uint32_t offset; |
| 1182 | uint32_t code; |
| 1183 | uint8_t *tlr; |
| 1184 | int rc; |
| 1185 | |
| 1186 | /* Test harness initialisation */ |
| 1187 | |
| 1188 | network_init(&ctx); |
| 1189 | |
| 1190 | memset(&msg[0], 0xa5, MCTP_BTU); |
| 1191 | |
| 1192 | ctx.msg = &msg[0]; |
| 1193 | ctx.count = 0; |
| 1194 | mctp_set_rx_all(ctx.bmc.mctp, rx_message, &ctx); |
| 1195 | |
| 1196 | /* Host sends the single-packet message */ |
| 1197 | rc = mctp_message_tx(ctx.host.mctp, 8, msg, sizeof(msg)); |
| 1198 | assert(rc == 0); |
| 1199 | assert(ctx.kcs[MCTP_ASTLPC_KCS_REG_STATUS] & KCS_STATUS_IBF); |
| 1200 | assert(ctx.kcs[MCTP_ASTLPC_KCS_REG_DATA] == 0x01); |
| 1201 | |
| 1202 | astlpc_assert_tx_packet(&ctx.host, &msg[0], MCTP_BTU); |
| 1203 | |
| 1204 | /* Corrupt the CRC-32 in the message trailer */ |
| 1205 | hdr = (struct mctp_lpcmap_hdr *)ctx.lpc_mem; |
| 1206 | offset = be32toh(hdr->layout.tx_offset); |
| 1207 | tlr = (uint8_t *)&ctx.lpc_mem[offset] + 4 + sizeof(msg); |
| 1208 | memcpy(&code, tlr, sizeof(code)); |
| 1209 | code = ~code; |
| 1210 | memcpy(tlr, &code, sizeof(code)); |
| 1211 | |
| 1212 | /* BMC receives the single-packet message */ |
| 1213 | mctp_astlpc_poll(ctx.bmc.astlpc); |
| 1214 | assert(ctx.count == 0); |
| 1215 | |
| 1216 | /* BMC returns Tx area ownership to Host */ |
| 1217 | assert(!(ctx.kcs[MCTP_ASTLPC_KCS_REG_STATUS] & KCS_STATUS_IBF)); |
| 1218 | assert(ctx.kcs[MCTP_ASTLPC_KCS_REG_DATA] == 0x02); |
| 1219 | assert(ctx.kcs[MCTP_ASTLPC_KCS_REG_STATUS] & KCS_STATUS_OBF); |
| 1220 | |
| 1221 | /* Host dequeues ownership hand-over */ |
| 1222 | rc = mctp_astlpc_poll(ctx.host.astlpc); |
| 1223 | assert(rc == 0); |
| 1224 | |
| 1225 | network_destroy(&ctx); |
| 1226 | } |
| 1227 | |
| 1228 | static void astlpc_test_corrupt_bmc_tx(void) |
| 1229 | { |
| 1230 | struct astlpc_test ctx = { 0 }; |
| 1231 | struct mctp_lpcmap_hdr *hdr; |
| 1232 | uint8_t msg[MCTP_BTU]; |
| 1233 | uint32_t offset; |
| 1234 | uint32_t code; |
| 1235 | uint8_t *tlr; |
| 1236 | int rc; |
| 1237 | |
| 1238 | /* Test harness initialisation */ |
| 1239 | |
| 1240 | network_init(&ctx); |
| 1241 | |
| 1242 | memset(&msg[0], 0x5a, MCTP_BTU); |
| 1243 | |
| 1244 | ctx.msg = &msg[0]; |
| 1245 | ctx.count = 0; |
| 1246 | mctp_set_rx_all(ctx.host.mctp, rx_message, &ctx); |
| 1247 | |
| 1248 | /* BMC sends the single-packet message */ |
| 1249 | rc = mctp_message_tx(ctx.bmc.mctp, 9, msg, sizeof(msg)); |
| 1250 | assert(rc == 0); |
| 1251 | assert(ctx.kcs[MCTP_ASTLPC_KCS_REG_STATUS] & KCS_STATUS_OBF); |
| 1252 | assert(ctx.kcs[MCTP_ASTLPC_KCS_REG_DATA] == 0x01); |
| 1253 | |
| 1254 | /* Check that the BMC sent a fully-formed packet */ |
| 1255 | astlpc_assert_tx_packet(&ctx.bmc, &msg[0], MCTP_BTU); |
| 1256 | |
| 1257 | /* Corrupt the CRC-32 in the message trailer */ |
| 1258 | hdr = (struct mctp_lpcmap_hdr *)ctx.lpc_mem; |
| 1259 | offset = be32toh(hdr->layout.rx_offset); |
| 1260 | tlr = (uint8_t *)&ctx.lpc_mem[offset] + 4 + sizeof(msg); |
| 1261 | memcpy(&code, tlr, sizeof(code)); |
| 1262 | code = ~code; |
| 1263 | memcpy(tlr, &code, sizeof(code)); |
| 1264 | |
| 1265 | /* Host drops the single-packet message */ |
| 1266 | mctp_astlpc_poll(ctx.host.astlpc); |
| 1267 | assert(ctx.count == 0); |
| 1268 | |
| 1269 | /* Host returns Rx area ownership to BMC */ |
| 1270 | assert(!(ctx.kcs[MCTP_ASTLPC_KCS_REG_STATUS] & KCS_STATUS_OBF)); |
| 1271 | assert(ctx.kcs[MCTP_ASTLPC_KCS_REG_DATA] == 0x02); |
| 1272 | assert(ctx.kcs[MCTP_ASTLPC_KCS_REG_STATUS] & KCS_STATUS_IBF); |
| 1273 | |
| 1274 | /* BMC dequeues ownership hand-over */ |
| 1275 | rc = mctp_astlpc_poll(ctx.bmc.astlpc); |
| 1276 | assert(rc == 0); |
| 1277 | |
| 1278 | network_destroy(&ctx); |
| 1279 | } |
| 1280 | |
Andrew Jeffery | 91f09ed | 2020-05-22 20:52:26 +0930 | [diff] [blame] | 1281 | /* clang-format off */ |
| 1282 | #define TEST_CASE(test) { #test, test } |
| 1283 | static const struct { |
| 1284 | const char *name; |
| 1285 | void (*test)(void); |
| 1286 | } astlpc_tests[] = { |
| 1287 | TEST_CASE(astlpc_test_simple_init), |
Andrew Jeffery | 4e8264b | 2020-05-23 20:34:33 +0930 | [diff] [blame] | 1288 | TEST_CASE(astlpc_test_bad_version), |
| 1289 | TEST_CASE(astlpc_test_incompatible_versions), |
| 1290 | TEST_CASE(astlpc_test_choose_bmc_ver_cur), |
| 1291 | TEST_CASE(astlpc_test_choose_host_ver_cur), |
| 1292 | TEST_CASE(astlpc_test_version_host_fails_negotiation), |
| 1293 | TEST_CASE(astlpc_test_version_bmc_fails_negotiation), |
Andrew Jeffery | f1cdb16 | 2020-05-23 21:25:21 +0930 | [diff] [blame] | 1294 | TEST_CASE(astlpc_test_host_before_bmc), |
Andrew Jeffery | 91f09ed | 2020-05-22 20:52:26 +0930 | [diff] [blame] | 1295 | TEST_CASE(astlpc_test_simple_message_bmc_to_host), |
Andrew Jeffery | ec9a006 | 2020-05-22 21:21:55 +0930 | [diff] [blame] | 1296 | TEST_CASE(astlpc_test_simple_message_host_to_bmc), |
Andrew Jeffery | 91f09ed | 2020-05-22 20:52:26 +0930 | [diff] [blame] | 1297 | TEST_CASE(astlpc_test_packetised_message_bmc_to_host), |
Andrew Jeffery | d0f5da0 | 2020-05-28 09:12:55 +0930 | [diff] [blame] | 1298 | TEST_CASE(astlpc_test_simple_indirect_message_bmc_to_host), |
| 1299 | TEST_CASE(astlpc_test_host_tx_bmc_gone), |
Andrew Jeffery | f3d94dc | 2020-05-28 10:50:18 +0930 | [diff] [blame] | 1300 | TEST_CASE(astlpc_test_poll_not_ready), |
Andrew Jeffery | 67655e8 | 2020-05-28 10:51:27 +0930 | [diff] [blame] | 1301 | TEST_CASE(astlpc_test_undefined_command), |
Andrew Jeffery | 3a54066 | 2020-05-26 19:55:30 +0930 | [diff] [blame] | 1302 | TEST_CASE(astlpc_test_buffers_rx_offset_overflow), |
| 1303 | TEST_CASE(astlpc_test_buffers_tx_offset_overflow), |
| 1304 | TEST_CASE(astlpc_test_buffers_rx_size_overflow), |
| 1305 | TEST_CASE(astlpc_test_buffers_tx_size_overflow), |
| 1306 | TEST_CASE(astlpc_test_buffers_rx_window_violation), |
| 1307 | TEST_CASE(astlpc_test_buffers_tx_window_violation), |
| 1308 | TEST_CASE(astlpc_test_buffers_rx_size_fails_btu), |
| 1309 | TEST_CASE(astlpc_test_buffers_tx_size_fails_btu), |
| 1310 | TEST_CASE(astlpc_test_buffers_overlap_rx_low), |
| 1311 | TEST_CASE(astlpc_test_buffers_overlap_tx_low), |
| 1312 | TEST_CASE(astlpc_test_buffers_bad_host_proposal), |
| 1313 | TEST_CASE(astlpc_test_buffers_bad_bmc_proposal), |
| 1314 | TEST_CASE(astlpc_test_buffers_bad_bmc_negotiation), |
| 1315 | TEST_CASE(astlpc_test_buffers_overlap_exact), |
| 1316 | TEST_CASE(astlpc_test_buffers_overlap_control), |
Andrew Jeffery | a936898 | 2020-06-09 13:07:39 +0930 | [diff] [blame] | 1317 | TEST_CASE(astlpc_test_buffers_bad_host_init), |
| 1318 | TEST_CASE(astlpc_test_negotiate_increased_mtu), |
| 1319 | TEST_CASE(astlpc_test_negotiate_mtu_low_high), |
Andrew Jeffery | c2b833e | 2020-10-28 14:28:37 +1030 | [diff] [blame] | 1320 | TEST_CASE(astlpc_test_send_large_packet), |
Andrew Jeffery | 3f4bca9 | 2021-01-28 14:19:19 +1030 | [diff] [blame] | 1321 | TEST_CASE(astlpc_test_tx_before_channel_init), |
Andrew Jeffery | eba19a3 | 2021-03-09 23:09:40 +1030 | [diff] [blame] | 1322 | TEST_CASE(astlpc_test_corrupt_host_tx), |
| 1323 | TEST_CASE(astlpc_test_corrupt_bmc_tx), |
Andrew Jeffery | 91f09ed | 2020-05-22 20:52:26 +0930 | [diff] [blame] | 1324 | }; |
| 1325 | /* clang-format on */ |
| 1326 | |
| 1327 | #ifndef BUILD_ASSERT |
| 1328 | #define BUILD_ASSERT(x) \ |
| 1329 | do { \ |
| 1330 | (void)sizeof(char[0 - (!(x))]); \ |
| 1331 | } while (0) |
| 1332 | #endif |
| 1333 | |
Andrew Jeffery | e756de8 | 2020-05-22 12:30:58 +0930 | [diff] [blame] | 1334 | int main(void) |
| 1335 | { |
Andrew Jeffery | 91f09ed | 2020-05-22 20:52:26 +0930 | [diff] [blame] | 1336 | size_t i; |
| 1337 | |
Andrew Jeffery | e756de8 | 2020-05-22 12:30:58 +0930 | [diff] [blame] | 1338 | mctp_set_log_stdio(MCTP_LOG_DEBUG); |
| 1339 | |
Andrew Jeffery | 91f09ed | 2020-05-22 20:52:26 +0930 | [diff] [blame] | 1340 | BUILD_ASSERT(ARRAY_SIZE(astlpc_tests) < SIZE_MAX); |
| 1341 | for (i = 0; i < ARRAY_SIZE(astlpc_tests); i++) { |
| 1342 | mctp_prlog(MCTP_LOG_DEBUG, "begin: %s", astlpc_tests[i].name); |
| 1343 | astlpc_tests[i].test(); |
| 1344 | mctp_prlog(MCTP_LOG_DEBUG, "end: %s\n", astlpc_tests[i].name); |
| 1345 | } |
Andrew Jeffery | 11b7e92 | 2020-03-10 23:37:09 +1030 | [diff] [blame] | 1346 | |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 1347 | return 0; |
| 1348 | } |