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) |
| 68 | mmio->kcs[reg] = val & ~0xaU; |
| 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); |
| 134 | |
| 135 | mmio.lpc_size = 1 * 1024 * 1024; |
| 136 | mmio.lpc = calloc(1, mmio.lpc_size); |
| 137 | |
| 138 | mctp_set_log_stdio(MCTP_LOG_DEBUG); |
| 139 | |
| 140 | mctp = mctp_init(); |
| 141 | assert(mctp); |
| 142 | |
| 143 | mctp_set_rx_all(mctp, rx_message, NULL); |
| 144 | |
| 145 | astlpc = mctp_astlpc_init_ops(&mctp_binding_astlpc_mmio_ops, &mmio, NULL); |
| 146 | |
| 147 | mctp_register_bus(mctp, &astlpc->binding, 8); |
| 148 | |
| 149 | /* Verify the binding was initialised */ |
| 150 | assert(mmio.kcs[MCTP_ASTLPC_KCS_REG_STATUS] & KCS_STATUS_BMC_READY); |
| 151 | |
| 152 | /* Host sends channel init command */ |
| 153 | mmio.kcs[MCTP_ASTLPC_KCS_REG_STATUS] |= KCS_STATUS_IBF; |
| 154 | mmio.kcs[MCTP_ASTLPC_KCS_REG_DATA] = 0x00; |
| 155 | |
| 156 | /* Receive host init */ |
| 157 | mctp_astlpc_poll(astlpc); |
| 158 | |
| 159 | /* Host receives init response */ |
| 160 | assert(mmio.kcs[MCTP_ASTLPC_KCS_REG_STATUS] & KCS_STATUS_OBF); |
| 161 | assert(mmio.kcs[MCTP_ASTLPC_KCS_REG_STATUS] & KCS_STATUS_CHANNEL_ACTIVE); |
| 162 | |
| 163 | /* Host dequeues data */ |
| 164 | assert(mmio.kcs[MCTP_ASTLPC_KCS_REG_DATA] == 0xff); |
| 165 | mmio.kcs[MCTP_ASTLPC_KCS_REG_STATUS] &= ~KCS_STATUS_OBF; |
| 166 | |
| 167 | /* BMC sends a message */ |
| 168 | rc = mctp_message_tx(mctp, 9, msg, sizeof(msg)); |
| 169 | assert(rc == 0); |
| 170 | |
| 171 | /* Host receives a message */ |
| 172 | assert(mmio.kcs[MCTP_ASTLPC_KCS_REG_STATUS] & KCS_STATUS_OBF); |
| 173 | assert(mmio.kcs[MCTP_ASTLPC_KCS_REG_DATA] == 0x01); |
| 174 | |
| 175 | /* Verify it's the packet we expect */ |
| 176 | assert(!memcmp(mmio.lpc + RX_BUFFER_DATA, &msg[0], MCTP_BTU)); |
| 177 | |
| 178 | /* Host returns Rx area ownership to BMC */ |
| 179 | mmio.kcs[MCTP_ASTLPC_KCS_REG_STATUS] &= ~KCS_STATUS_OBF; |
| 180 | mmio.kcs[MCTP_ASTLPC_KCS_REG_DATA] = 0x02; |
| 181 | mmio.kcs[MCTP_ASTLPC_KCS_REG_STATUS] |= KCS_STATUS_IBF; |
| 182 | |
| 183 | /* BMC dequeues ownership hand-over and sends the queued packet */ |
| 184 | rc = mctp_astlpc_poll(astlpc); |
| 185 | assert(rc == 0); |
| 186 | |
| 187 | /* Host receives a message */ |
| 188 | assert(mmio.kcs[MCTP_ASTLPC_KCS_REG_STATUS] & KCS_STATUS_OBF); |
| 189 | assert(mmio.kcs[MCTP_ASTLPC_KCS_REG_DATA] == 0x01); |
| 190 | |
| 191 | /* Verify it's the packet we expect */ |
| 192 | assert(!memcmp(mmio.lpc + RX_BUFFER_DATA, &msg[MCTP_BTU], MCTP_BTU)); |
| 193 | |
Andrew Jeffery | 11b7e92 | 2020-03-10 23:37:09 +1030 | [diff] [blame] | 194 | mctp_astlpc_destroy(astlpc); |
| 195 | mctp_destroy(mctp); |
| 196 | free(mmio.lpc); |
| 197 | |
Andrew Jeffery | 0247c73 | 2020-02-06 11:48:52 +1030 | [diff] [blame] | 198 | return 0; |
| 199 | } |