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; |
| 32 | |
| 33 | uint8_t kcs[2]; |
| 34 | |
| 35 | size_t lpc_size; |
| 36 | uint8_t *lpc; |
| 37 | }; |
| 38 | |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 39 | #define binding_to_mmio(b) \ |
| 40 | container_of(b, struct mctp_binding_astlpc_mmio, astlpc) |
| 41 | |
Andrew Jeffery | 53ea147 | 2020-05-23 21:06:24 +0930 | [diff] [blame] | 42 | static int mctp_astlpc_mmio_kcs_read(void *data, |
| 43 | enum mctp_binding_astlpc_kcs_reg reg, |
| 44 | uint8_t *val) |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 45 | { |
| 46 | struct mctp_binding_astlpc_mmio *mmio = binding_to_mmio(data); |
| 47 | |
| 48 | *val = mmio->kcs[reg]; |
| 49 | |
| 50 | mctp_prdebug("%s: 0x%hhx from %s", __func__, *val, reg ? "status" : "data"); |
| 51 | |
| 52 | if (reg == MCTP_ASTLPC_KCS_REG_DATA) |
| 53 | mmio->kcs[MCTP_ASTLPC_KCS_REG_STATUS] &= ~KCS_STATUS_IBF; |
| 54 | |
| 55 | return 0; |
| 56 | } |
| 57 | |
Andrew Jeffery | 53ea147 | 2020-05-23 21:06:24 +0930 | [diff] [blame] | 58 | static int mctp_astlpc_mmio_kcs_write(void *data, |
| 59 | enum mctp_binding_astlpc_kcs_reg reg, |
| 60 | uint8_t val) |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 61 | { |
| 62 | struct mctp_binding_astlpc_mmio *mmio = binding_to_mmio(data); |
| 63 | |
| 64 | if (reg == MCTP_ASTLPC_KCS_REG_DATA) |
| 65 | mmio->kcs[MCTP_ASTLPC_KCS_REG_STATUS] |= KCS_STATUS_OBF; |
| 66 | |
| 67 | if (reg == MCTP_ASTLPC_KCS_REG_STATUS) |
Andrew Jeffery | 7cceeba | 2020-05-13 14:03:53 +0930 | [diff] [blame^] | 68 | mmio->kcs[reg] = (val & ~0xbU) | (val & mmio->kcs[reg] & 1); |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 69 | else |
| 70 | mmio->kcs[reg] = val; |
| 71 | |
| 72 | mctp_prdebug("%s: 0x%hhx to %s", __func__, val, reg ? "status" : "data"); |
| 73 | |
| 74 | return 0; |
| 75 | } |
Andrew Jeffery | 06b2cd8 | 2020-03-17 23:12:27 +1030 | [diff] [blame] | 76 | 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] | 77 | { |
| 78 | struct mctp_binding_astlpc_mmio *mmio = binding_to_mmio(data); |
| 79 | |
Andrew Jeffery | 1dbf021 | 2020-05-12 13:53:50 +0930 | [diff] [blame] | 80 | mctp_prdebug("%s: %zu bytes from 0x%lx", __func__, len, offset); |
| 81 | |
Andrew Jeffery | 06b2cd8 | 2020-03-17 23:12:27 +1030 | [diff] [blame] | 82 | assert(offset >= 0L); |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 83 | assert(offset + len < mmio->lpc_size); |
| 84 | |
| 85 | memcpy(buf, mmio->lpc + offset, len); |
| 86 | |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 87 | return 0; |
| 88 | } |
| 89 | |
Andrew Jeffery | 06b2cd8 | 2020-03-17 23:12:27 +1030 | [diff] [blame] | 90 | 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] | 91 | { |
| 92 | struct mctp_binding_astlpc_mmio *mmio = binding_to_mmio(data); |
| 93 | |
Andrew Jeffery | 1dbf021 | 2020-05-12 13:53:50 +0930 | [diff] [blame] | 94 | mctp_prdebug("%s: %zu bytes to 0x%lx", __func__, len, offset); |
| 95 | |
Andrew Jeffery | 06b2cd8 | 2020-03-17 23:12:27 +1030 | [diff] [blame] | 96 | assert(offset >= 0L); |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 97 | assert(offset + len < mmio->lpc_size); |
| 98 | |
| 99 | memcpy(mmio->lpc + offset, buf, len); |
| 100 | |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 101 | return 0; |
| 102 | } |
| 103 | |
Andrew Jeffery | b93b611 | 2020-06-05 14:13:44 +0930 | [diff] [blame] | 104 | #define __unused __attribute__((unused)) |
| 105 | |
| 106 | static void rx_message(uint8_t eid __unused, void *data __unused, void *msg, |
| 107 | size_t len) |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 108 | { |
| 109 | uint8_t type; |
| 110 | |
| 111 | type = *(uint8_t *)msg; |
| 112 | |
| 113 | mctp_prdebug("MCTP message received: len %zd, type %d", |
| 114 | len, type); |
| 115 | } |
| 116 | |
Andrew Jeffery | 53ea147 | 2020-05-23 21:06:24 +0930 | [diff] [blame] | 117 | static const struct mctp_binding_astlpc_ops mctp_binding_astlpc_mmio_ops = { |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 118 | .kcs_read = mctp_astlpc_mmio_kcs_read, |
| 119 | .kcs_write = mctp_astlpc_mmio_kcs_write, |
| 120 | .lpc_read = mctp_astlpc_mmio_lpc_read, |
| 121 | .lpc_write = mctp_astlpc_mmio_lpc_write, |
| 122 | }; |
| 123 | |
| 124 | int main(void) |
| 125 | { |
| 126 | struct mctp_binding_astlpc_mmio mmio; |
| 127 | struct mctp_binding_astlpc *astlpc; |
| 128 | uint8_t msg[2 * MCTP_BTU]; |
| 129 | struct mctp *mctp; |
| 130 | int rc; |
| 131 | |
| 132 | memset(&msg[0], 0x5a, MCTP_BTU); |
| 133 | memset(&msg[MCTP_BTU], 0xa5, MCTP_BTU); |
Andrew Jeffery | 7cceeba | 2020-05-13 14:03:53 +0930 | [diff] [blame^] | 134 | mmio.kcs[MCTP_ASTLPC_KCS_REG_STATUS] = 0; |
| 135 | mmio.kcs[MCTP_ASTLPC_KCS_REG_DATA] = 0; |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 136 | |
| 137 | mmio.lpc_size = 1 * 1024 * 1024; |
| 138 | mmio.lpc = calloc(1, mmio.lpc_size); |
| 139 | |
| 140 | mctp_set_log_stdio(MCTP_LOG_DEBUG); |
| 141 | |
| 142 | mctp = mctp_init(); |
| 143 | assert(mctp); |
| 144 | |
| 145 | mctp_set_rx_all(mctp, rx_message, NULL); |
| 146 | |
| 147 | astlpc = mctp_astlpc_init_ops(&mctp_binding_astlpc_mmio_ops, &mmio, NULL); |
| 148 | |
| 149 | mctp_register_bus(mctp, &astlpc->binding, 8); |
| 150 | |
| 151 | /* Verify the binding was initialised */ |
| 152 | assert(mmio.kcs[MCTP_ASTLPC_KCS_REG_STATUS] & KCS_STATUS_BMC_READY); |
| 153 | |
| 154 | /* Host sends channel init command */ |
| 155 | mmio.kcs[MCTP_ASTLPC_KCS_REG_STATUS] |= KCS_STATUS_IBF; |
| 156 | mmio.kcs[MCTP_ASTLPC_KCS_REG_DATA] = 0x00; |
| 157 | |
| 158 | /* Receive host init */ |
| 159 | mctp_astlpc_poll(astlpc); |
| 160 | |
| 161 | /* Host receives init response */ |
| 162 | assert(mmio.kcs[MCTP_ASTLPC_KCS_REG_STATUS] & KCS_STATUS_OBF); |
| 163 | assert(mmio.kcs[MCTP_ASTLPC_KCS_REG_STATUS] & KCS_STATUS_CHANNEL_ACTIVE); |
| 164 | |
| 165 | /* Host dequeues data */ |
| 166 | assert(mmio.kcs[MCTP_ASTLPC_KCS_REG_DATA] == 0xff); |
| 167 | mmio.kcs[MCTP_ASTLPC_KCS_REG_STATUS] &= ~KCS_STATUS_OBF; |
| 168 | |
| 169 | /* BMC sends a message */ |
| 170 | rc = mctp_message_tx(mctp, 9, msg, sizeof(msg)); |
| 171 | assert(rc == 0); |
| 172 | |
| 173 | /* Host receives a message */ |
| 174 | assert(mmio.kcs[MCTP_ASTLPC_KCS_REG_STATUS] & KCS_STATUS_OBF); |
| 175 | assert(mmio.kcs[MCTP_ASTLPC_KCS_REG_DATA] == 0x01); |
| 176 | |
| 177 | /* Verify it's the packet we expect */ |
| 178 | assert(!memcmp(mmio.lpc + RX_BUFFER_DATA, &msg[0], MCTP_BTU)); |
| 179 | |
| 180 | /* Host returns Rx area ownership to BMC */ |
| 181 | mmio.kcs[MCTP_ASTLPC_KCS_REG_STATUS] &= ~KCS_STATUS_OBF; |
| 182 | mmio.kcs[MCTP_ASTLPC_KCS_REG_DATA] = 0x02; |
| 183 | mmio.kcs[MCTP_ASTLPC_KCS_REG_STATUS] |= KCS_STATUS_IBF; |
| 184 | |
| 185 | /* BMC dequeues ownership hand-over and sends the queued packet */ |
| 186 | rc = mctp_astlpc_poll(astlpc); |
| 187 | assert(rc == 0); |
| 188 | |
| 189 | /* Host receives a message */ |
| 190 | assert(mmio.kcs[MCTP_ASTLPC_KCS_REG_STATUS] & KCS_STATUS_OBF); |
| 191 | assert(mmio.kcs[MCTP_ASTLPC_KCS_REG_DATA] == 0x01); |
| 192 | |
| 193 | /* Verify it's the packet we expect */ |
| 194 | assert(!memcmp(mmio.lpc + RX_BUFFER_DATA, &msg[MCTP_BTU], MCTP_BTU)); |
| 195 | |
Andrew Jeffery | 11b7e92 | 2020-03-10 23:37:09 +1030 | [diff] [blame] | 196 | mctp_astlpc_destroy(astlpc); |
| 197 | mctp_destroy(mctp); |
| 198 | free(mmio.lpc); |
| 199 | |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 200 | return 0; |
| 201 | } |