blob: 16e1ca5f52ebd9bddc32fc98e16708a79fe6b854 [file] [log] [blame]
Andrew Jeffery0247c732020-02-06 11:48:52 +10301/* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */
2
Andrew Jeffery2cda40f2020-02-28 15:26:20 +10303#ifdef HAVE_CONFIG_H
Andrew Jeffery0247c732020-02-06 11:48:52 +10304#include "config.h"
Andrew Jeffery2cda40f2020-02-28 15:26:20 +10305#endif
6
Andrew Jefferydf5f6b92020-05-12 21:39:06 +09307#include "astlpc.c"
8
9#ifdef pr_fmt
10#undef pr_fmt
11#define pr_fmt(x) "test: " x
12#endif
13
Andrew Jeffery0247c732020-02-06 11:48:52 +103014#include "libmctp-astlpc.h"
15#include "libmctp-log.h"
Przemyslaw Czarnowskiff25d7e2020-03-26 11:39:37 +010016#include "container_of.h"
Andrew Jeffery0247c732020-02-06 11:48:52 +103017
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 Jeffery0247c732020-02-06 11:48:52 +103030struct 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 Jeffery0247c732020-02-06 11:48:52 +103039#define binding_to_mmio(b) \
40 container_of(b, struct mctp_binding_astlpc_mmio, astlpc)
41
Andrew Jeffery53ea1472020-05-23 21:06:24 +093042static int mctp_astlpc_mmio_kcs_read(void *data,
43 enum mctp_binding_astlpc_kcs_reg reg,
44 uint8_t *val)
Andrew Jeffery0247c732020-02-06 11:48:52 +103045{
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 Jeffery53ea1472020-05-23 21:06:24 +093058static int mctp_astlpc_mmio_kcs_write(void *data,
59 enum mctp_binding_astlpc_kcs_reg reg,
60 uint8_t val)
Andrew Jeffery0247c732020-02-06 11:48:52 +103061{
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 Jeffery7cceeba2020-05-13 14:03:53 +093068 mmio->kcs[reg] = (val & ~0xbU) | (val & mmio->kcs[reg] & 1);
Andrew Jeffery0247c732020-02-06 11:48:52 +103069 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 Jeffery06b2cd82020-03-17 23:12:27 +103076int mctp_astlpc_mmio_lpc_read(void *data, void *buf, long offset, size_t len)
Andrew Jeffery0247c732020-02-06 11:48:52 +103077{
78 struct mctp_binding_astlpc_mmio *mmio = binding_to_mmio(data);
79
Andrew Jeffery1dbf0212020-05-12 13:53:50 +093080 mctp_prdebug("%s: %zu bytes from 0x%lx", __func__, len, offset);
81
Andrew Jeffery06b2cd82020-03-17 23:12:27 +103082 assert(offset >= 0L);
Andrew Jeffery0247c732020-02-06 11:48:52 +103083 assert(offset + len < mmio->lpc_size);
84
85 memcpy(buf, mmio->lpc + offset, len);
86
Andrew Jeffery0247c732020-02-06 11:48:52 +103087 return 0;
88}
89
Andrew Jeffery06b2cd82020-03-17 23:12:27 +103090int mctp_astlpc_mmio_lpc_write(void *data, void *buf, long offset, size_t len)
Andrew Jeffery0247c732020-02-06 11:48:52 +103091{
92 struct mctp_binding_astlpc_mmio *mmio = binding_to_mmio(data);
93
Andrew Jeffery1dbf0212020-05-12 13:53:50 +093094 mctp_prdebug("%s: %zu bytes to 0x%lx", __func__, len, offset);
95
Andrew Jeffery06b2cd82020-03-17 23:12:27 +103096 assert(offset >= 0L);
Andrew Jeffery0247c732020-02-06 11:48:52 +103097 assert(offset + len < mmio->lpc_size);
98
99 memcpy(mmio->lpc + offset, buf, len);
100
Andrew Jeffery0247c732020-02-06 11:48:52 +1030101 return 0;
102}
103
Andrew Jefferyb93b6112020-06-05 14:13:44 +0930104#define __unused __attribute__((unused))
105
106static void rx_message(uint8_t eid __unused, void *data __unused, void *msg,
107 size_t len)
Andrew Jeffery0247c732020-02-06 11:48:52 +1030108{
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 Jeffery53ea1472020-05-23 21:06:24 +0930117static const struct mctp_binding_astlpc_ops mctp_binding_astlpc_mmio_ops = {
Andrew Jeffery0247c732020-02-06 11:48:52 +1030118 .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
124int 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 Jeffery7cceeba2020-05-13 14:03:53 +0930134 mmio.kcs[MCTP_ASTLPC_KCS_REG_STATUS] = 0;
135 mmio.kcs[MCTP_ASTLPC_KCS_REG_DATA] = 0;
Andrew Jeffery0247c732020-02-06 11:48:52 +1030136
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 Jeffery11b7e922020-03-10 23:37:09 +1030196 mctp_astlpc_destroy(astlpc);
197 mctp_destroy(mctp);
198 free(mmio.lpc);
199
Andrew Jeffery0247c732020-02-06 11:48:52 +1030200 return 0;
201}