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