| 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 |  | 
|  | 3 | #include "config.h" | 
|  | 4 | #include "libmctp-astlpc.h" | 
|  | 5 | #include "libmctp-log.h" | 
|  | 6 |  | 
|  | 7 | #ifdef NDEBUG | 
|  | 8 | #undef NDEBUG | 
|  | 9 | #endif | 
|  | 10 |  | 
|  | 11 | #define RX_BUFFER_DATA	0x100 + 4 + 4 | 
|  | 12 |  | 
|  | 13 | #include <assert.h> | 
|  | 14 | #include <stdint.h> | 
|  | 15 | #include <stdio.h> | 
|  | 16 | #include <stdlib.h> | 
|  | 17 | #include <string.h> | 
|  | 18 |  | 
|  | 19 | /* Hack: Needs to be in sync with astlpc.c */ | 
|  | 20 | struct mctp_binding_astlpc { | 
|  | 21 | struct mctp_binding	binding; | 
|  | 22 |  | 
|  | 23 | union { | 
|  | 24 | void			*lpc_map; | 
|  | 25 | struct mctp_lpcmap_hdr	*lpc_hdr; | 
|  | 26 | }; | 
|  | 27 |  | 
|  | 28 | /* direct ops data */ | 
|  | 29 | struct mctp_binding_astlpc_ops	ops; | 
|  | 30 | void			*ops_data; | 
|  | 31 | struct mctp_lpcmap_hdr	*priv_hdr; | 
|  | 32 |  | 
|  | 33 | /* fileio ops data */ | 
|  | 34 | void			*lpc_map_base; | 
|  | 35 | int			kcs_fd; | 
|  | 36 | uint8_t			kcs_status; | 
|  | 37 |  | 
|  | 38 | bool			running; | 
|  | 39 |  | 
|  | 40 | /* temporary transmit buffer */ | 
|  | 41 | uint8_t			txbuf[256]; | 
|  | 42 | }; | 
|  | 43 |  | 
|  | 44 | #define KCS_STATUS_BMC_READY		0x80 | 
|  | 45 | #define KCS_STATUS_CHANNEL_ACTIVE	0x40 | 
|  | 46 | #define KCS_STATUS_IBF			0x02 | 
|  | 47 | #define KCS_STATUS_OBF			0x01 | 
|  | 48 |  | 
|  | 49 | struct mctp_binding_astlpc_mmio { | 
|  | 50 | struct mctp_binding_astlpc astlpc; | 
|  | 51 |  | 
|  | 52 | uint8_t kcs[2]; | 
|  | 53 |  | 
|  | 54 | size_t lpc_size; | 
|  | 55 | uint8_t *lpc; | 
|  | 56 | }; | 
|  | 57 |  | 
|  | 58 | #ifndef container_of | 
|  | 59 | #define container_of(ptr, type, member) \ | 
|  | 60 | (type *)((char *)(ptr) - (char *)&((type *)0)->member) | 
|  | 61 | #endif | 
|  | 62 |  | 
|  | 63 | #define binding_to_mmio(b) \ | 
|  | 64 | container_of(b, struct mctp_binding_astlpc_mmio, astlpc) | 
|  | 65 |  | 
|  | 66 | int mctp_astlpc_mmio_kcs_read(void *data, enum mctp_binding_astlpc_kcs_reg reg, | 
|  | 67 | uint8_t *val) | 
|  | 68 | { | 
|  | 69 | struct mctp_binding_astlpc_mmio *mmio = binding_to_mmio(data); | 
|  | 70 |  | 
|  | 71 | *val = mmio->kcs[reg]; | 
|  | 72 |  | 
|  | 73 | mctp_prdebug("%s: 0x%hhx from %s", __func__, *val, reg ? "status" : "data"); | 
|  | 74 |  | 
|  | 75 | if (reg == MCTP_ASTLPC_KCS_REG_DATA) | 
|  | 76 | mmio->kcs[MCTP_ASTLPC_KCS_REG_STATUS] &= ~KCS_STATUS_IBF; | 
|  | 77 |  | 
|  | 78 | return 0; | 
|  | 79 | } | 
|  | 80 |  | 
|  | 81 | int mctp_astlpc_mmio_kcs_write(void *data, enum mctp_binding_astlpc_kcs_reg reg, | 
|  | 82 | uint8_t val) | 
|  | 83 | { | 
|  | 84 | struct mctp_binding_astlpc_mmio *mmio = binding_to_mmio(data); | 
|  | 85 |  | 
|  | 86 | if (reg == MCTP_ASTLPC_KCS_REG_DATA) | 
|  | 87 | mmio->kcs[MCTP_ASTLPC_KCS_REG_STATUS] |= KCS_STATUS_OBF; | 
|  | 88 |  | 
|  | 89 | if (reg == MCTP_ASTLPC_KCS_REG_STATUS) | 
|  | 90 | mmio->kcs[reg] = val & ~0xaU; | 
|  | 91 | else | 
|  | 92 | mmio->kcs[reg] = val; | 
|  | 93 |  | 
|  | 94 | mctp_prdebug("%s: 0x%hhx to %s", __func__, val, reg ? "status" : "data"); | 
|  | 95 |  | 
|  | 96 | return 0; | 
|  | 97 | } | 
|  | 98 | int mctp_astlpc_mmio_lpc_read(void *data, void *buf, off_t offset, size_t len) | 
|  | 99 | { | 
|  | 100 | struct mctp_binding_astlpc_mmio *mmio = binding_to_mmio(data); | 
|  | 101 |  | 
|  | 102 | assert(offset + len < mmio->lpc_size); | 
|  | 103 |  | 
|  | 104 | memcpy(buf, mmio->lpc + offset, len); | 
|  | 105 |  | 
|  | 106 | mctp_prdebug("%s: %zu bytes from 0x%zx", __func__, len, offset); | 
|  | 107 |  | 
|  | 108 | return 0; | 
|  | 109 | } | 
|  | 110 |  | 
|  | 111 | int mctp_astlpc_mmio_lpc_write(void *data, void *buf, off_t offset, size_t len) | 
|  | 112 | { | 
|  | 113 | struct mctp_binding_astlpc_mmio *mmio = binding_to_mmio(data); | 
|  | 114 |  | 
|  | 115 | assert(offset + len < mmio->lpc_size); | 
|  | 116 |  | 
|  | 117 | memcpy(mmio->lpc + offset, buf, len); | 
|  | 118 |  | 
|  | 119 | mctp_prdebug("%s: %zu bytes to 0x%zx", __func__, len, offset); | 
|  | 120 |  | 
|  | 121 | return 0; | 
|  | 122 | } | 
|  | 123 |  | 
|  | 124 | static void rx_message(uint8_t eid, void *data, void *msg, size_t len) | 
|  | 125 | { | 
|  | 126 | uint8_t type; | 
|  | 127 |  | 
|  | 128 | type = *(uint8_t *)msg; | 
|  | 129 |  | 
|  | 130 | mctp_prdebug("MCTP message received: len %zd, type %d", | 
|  | 131 | len, type); | 
|  | 132 | } | 
|  | 133 |  | 
|  | 134 | const struct mctp_binding_astlpc_ops mctp_binding_astlpc_mmio_ops = { | 
|  | 135 | .kcs_read = mctp_astlpc_mmio_kcs_read, | 
|  | 136 | .kcs_write = mctp_astlpc_mmio_kcs_write, | 
|  | 137 | .lpc_read = mctp_astlpc_mmio_lpc_read, | 
|  | 138 | .lpc_write = mctp_astlpc_mmio_lpc_write, | 
|  | 139 | }; | 
|  | 140 |  | 
|  | 141 | int main(void) | 
|  | 142 | { | 
|  | 143 | struct mctp_binding_astlpc_mmio mmio; | 
|  | 144 | struct mctp_binding_astlpc *astlpc; | 
|  | 145 | uint8_t msg[2 * MCTP_BTU]; | 
|  | 146 | struct mctp *mctp; | 
|  | 147 | int rc; | 
|  | 148 |  | 
|  | 149 | memset(&msg[0], 0x5a, MCTP_BTU); | 
|  | 150 | memset(&msg[MCTP_BTU], 0xa5, MCTP_BTU); | 
|  | 151 |  | 
|  | 152 | mmio.lpc_size = 1 * 1024 * 1024; | 
|  | 153 | mmio.lpc = calloc(1, mmio.lpc_size); | 
|  | 154 |  | 
|  | 155 | mctp_set_log_stdio(MCTP_LOG_DEBUG); | 
|  | 156 |  | 
|  | 157 | mctp = mctp_init(); | 
|  | 158 | assert(mctp); | 
|  | 159 |  | 
|  | 160 | mctp_set_rx_all(mctp, rx_message, NULL); | 
|  | 161 |  | 
|  | 162 | astlpc = mctp_astlpc_init_ops(&mctp_binding_astlpc_mmio_ops, &mmio, NULL); | 
|  | 163 |  | 
|  | 164 | mctp_register_bus(mctp, &astlpc->binding, 8); | 
|  | 165 |  | 
|  | 166 | /* Verify the binding was initialised */ | 
|  | 167 | assert(mmio.kcs[MCTP_ASTLPC_KCS_REG_STATUS] & KCS_STATUS_BMC_READY); | 
|  | 168 |  | 
|  | 169 | /* Host sends channel init command */ | 
|  | 170 | mmio.kcs[MCTP_ASTLPC_KCS_REG_STATUS] |= KCS_STATUS_IBF; | 
|  | 171 | mmio.kcs[MCTP_ASTLPC_KCS_REG_DATA] = 0x00; | 
|  | 172 |  | 
|  | 173 | /* Receive host init */ | 
|  | 174 | mctp_astlpc_poll(astlpc); | 
|  | 175 |  | 
|  | 176 | /* Host receives init response */ | 
|  | 177 | assert(mmio.kcs[MCTP_ASTLPC_KCS_REG_STATUS] & KCS_STATUS_OBF); | 
|  | 178 | assert(mmio.kcs[MCTP_ASTLPC_KCS_REG_STATUS] & KCS_STATUS_CHANNEL_ACTIVE); | 
|  | 179 |  | 
|  | 180 | /* Host dequeues data */ | 
|  | 181 | assert(mmio.kcs[MCTP_ASTLPC_KCS_REG_DATA] == 0xff); | 
|  | 182 | mmio.kcs[MCTP_ASTLPC_KCS_REG_STATUS] &= ~KCS_STATUS_OBF; | 
|  | 183 |  | 
|  | 184 | /* BMC sends a message */ | 
|  | 185 | rc = mctp_message_tx(mctp, 9, msg, sizeof(msg)); | 
|  | 186 | assert(rc == 0); | 
|  | 187 |  | 
|  | 188 | /* Host receives a message */ | 
|  | 189 | assert(mmio.kcs[MCTP_ASTLPC_KCS_REG_STATUS] & KCS_STATUS_OBF); | 
|  | 190 | assert(mmio.kcs[MCTP_ASTLPC_KCS_REG_DATA] == 0x01); | 
|  | 191 |  | 
|  | 192 | /* Verify it's the packet we expect */ | 
|  | 193 | assert(!memcmp(mmio.lpc + RX_BUFFER_DATA, &msg[0], MCTP_BTU)); | 
|  | 194 |  | 
|  | 195 | /* Host returns Rx area ownership to BMC */ | 
|  | 196 | mmio.kcs[MCTP_ASTLPC_KCS_REG_STATUS] &= ~KCS_STATUS_OBF; | 
|  | 197 | mmio.kcs[MCTP_ASTLPC_KCS_REG_DATA] = 0x02; | 
|  | 198 | mmio.kcs[MCTP_ASTLPC_KCS_REG_STATUS] |= KCS_STATUS_IBF; | 
|  | 199 |  | 
|  | 200 | /* BMC dequeues ownership hand-over and sends the queued packet */ | 
|  | 201 | rc = mctp_astlpc_poll(astlpc); | 
|  | 202 | assert(rc == 0); | 
|  | 203 |  | 
|  | 204 | /* Host receives a message */ | 
|  | 205 | assert(mmio.kcs[MCTP_ASTLPC_KCS_REG_STATUS] & KCS_STATUS_OBF); | 
|  | 206 | assert(mmio.kcs[MCTP_ASTLPC_KCS_REG_DATA] == 0x01); | 
|  | 207 |  | 
|  | 208 | /* Verify it's the packet we expect */ | 
|  | 209 | assert(!memcmp(mmio.lpc + RX_BUFFER_DATA, &msg[MCTP_BTU], MCTP_BTU)); | 
|  | 210 |  | 
|  | 211 | return 0; | 
|  | 212 | } |