blob: 35d00eeeb7912f0a822995f3c56555bbd887bd69 [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
Andrew Jeffery0247c732020-02-06 11:48:52 +103022#include <assert.h>
Andrew Jeffery91f09ed2020-05-22 20:52:26 +093023#include <limits.h>
Andrew Jeffery0247c732020-02-06 11:48:52 +103024#include <stdint.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28
Andrew Jeffery91f09ed2020-05-22 20:52:26 +093029#ifndef ARRAY_SIZE
30#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
31#endif
32
Andrew Jeffery0247c732020-02-06 11:48:52 +103033struct mctp_binding_astlpc_mmio {
34 struct mctp_binding_astlpc astlpc;
Andrew Jefferyd5e3cd72020-05-12 22:10:22 +093035 bool bmc;
Andrew Jeffery0247c732020-02-06 11:48:52 +103036
Andrew Jefferyd5e3cd72020-05-12 22:10:22 +093037 uint8_t (*kcs)[2];
Andrew Jeffery0247c732020-02-06 11:48:52 +103038
39 size_t lpc_size;
40 uint8_t *lpc;
41};
42
Andrew Jeffery5a7c2db2020-05-22 20:13:42 +093043struct astlpc_endpoint {
44 struct mctp_binding_astlpc_mmio mmio;
45 struct mctp_binding_astlpc *astlpc;
46 struct mctp *mctp;
47};
48
49struct astlpc_test {
50 struct astlpc_endpoint bmc;
51 struct astlpc_endpoint host;
52 uint8_t kcs[2];
53 uint8_t *lpc_mem;
54
55 void *msg;
56 uint8_t count;
57};
58
59#define binding_to_mmio(b) \
Andrew Jeffery0247c732020-02-06 11:48:52 +103060 container_of(b, struct mctp_binding_astlpc_mmio, astlpc)
61
Andrew Jeffery53ea1472020-05-23 21:06:24 +093062static int mctp_astlpc_mmio_kcs_read(void *data,
63 enum mctp_binding_astlpc_kcs_reg reg,
64 uint8_t *val)
Andrew Jeffery0247c732020-02-06 11:48:52 +103065{
66 struct mctp_binding_astlpc_mmio *mmio = binding_to_mmio(data);
67
Andrew Jefferyd5e3cd72020-05-12 22:10:22 +093068 *val = (*mmio->kcs)[reg];
Andrew Jeffery0247c732020-02-06 11:48:52 +103069
Andrew Jefferyd3c0bf02020-05-28 15:28:40 +093070 mctp_prdebug("%s: 0x%hhx from %s", __func__, *val,
71 reg ? "status" : "data");
Andrew Jeffery0247c732020-02-06 11:48:52 +103072
Andrew Jefferyd5e3cd72020-05-12 22:10:22 +093073 if (reg == MCTP_ASTLPC_KCS_REG_DATA) {
74 uint8_t flag = mmio->bmc ? KCS_STATUS_IBF : KCS_STATUS_OBF;
75 (*mmio->kcs)[MCTP_ASTLPC_KCS_REG_STATUS] &= ~flag;
76 }
Andrew Jeffery0247c732020-02-06 11:48:52 +103077
78 return 0;
79}
80
Andrew Jeffery53ea1472020-05-23 21:06:24 +093081static int mctp_astlpc_mmio_kcs_write(void *data,
82 enum mctp_binding_astlpc_kcs_reg reg,
83 uint8_t val)
Andrew Jeffery0247c732020-02-06 11:48:52 +103084{
85 struct mctp_binding_astlpc_mmio *mmio = binding_to_mmio(data);
Andrew Jefferyd5e3cd72020-05-12 22:10:22 +093086 uint8_t *regp;
Andrew Jeffery0247c732020-02-06 11:48:52 +103087
Andrew Jefferyd5e3cd72020-05-12 22:10:22 +093088 assert(reg == MCTP_ASTLPC_KCS_REG_DATA ||
89 reg == MCTP_ASTLPC_KCS_REG_STATUS);
Andrew Jeffery0247c732020-02-06 11:48:52 +103090
Andrew Jefferyd5e3cd72020-05-12 22:10:22 +093091 if (reg == MCTP_ASTLPC_KCS_REG_DATA) {
92 uint8_t flag = mmio->bmc ? KCS_STATUS_OBF : KCS_STATUS_IBF;
93 (*mmio->kcs)[MCTP_ASTLPC_KCS_REG_STATUS] |= flag;
94 }
95
96 regp = &(*mmio->kcs)[reg];
Andrew Jeffery0247c732020-02-06 11:48:52 +103097 if (reg == MCTP_ASTLPC_KCS_REG_STATUS)
Andrew Jefferyd5e3cd72020-05-12 22:10:22 +093098 *regp = (val & ~0xbU) | (val & *regp & 1);
Andrew Jeffery0247c732020-02-06 11:48:52 +103099 else
Andrew Jefferyd5e3cd72020-05-12 22:10:22 +0930100 *regp = val;
Andrew Jeffery0247c732020-02-06 11:48:52 +1030101
Andrew Jefferyd3c0bf02020-05-28 15:28:40 +0930102 mctp_prdebug("%s: 0x%hhx to %s", __func__, val,
103 reg ? "status" : "data");
Andrew Jeffery0247c732020-02-06 11:48:52 +1030104
105 return 0;
106}
Andrew Jeffery06b2cd82020-03-17 23:12:27 +1030107int mctp_astlpc_mmio_lpc_read(void *data, void *buf, long offset, size_t len)
Andrew Jeffery0247c732020-02-06 11:48:52 +1030108{
109 struct mctp_binding_astlpc_mmio *mmio = binding_to_mmio(data);
110
Andrew Jeffery1dbf0212020-05-12 13:53:50 +0930111 mctp_prdebug("%s: %zu bytes from 0x%lx", __func__, len, offset);
112
Andrew Jeffery06b2cd82020-03-17 23:12:27 +1030113 assert(offset >= 0L);
Andrew Jeffery0247c732020-02-06 11:48:52 +1030114 assert(offset + len < mmio->lpc_size);
115
116 memcpy(buf, mmio->lpc + offset, len);
117
Andrew Jeffery0247c732020-02-06 11:48:52 +1030118 return 0;
119}
120
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930121int mctp_astlpc_mmio_lpc_write(void *data, const void *buf, long offset,
122 size_t len)
Andrew Jeffery0247c732020-02-06 11:48:52 +1030123{
124 struct mctp_binding_astlpc_mmio *mmio = binding_to_mmio(data);
125
Andrew Jeffery1dbf0212020-05-12 13:53:50 +0930126 mctp_prdebug("%s: %zu bytes to 0x%lx", __func__, len, offset);
127
Andrew Jeffery06b2cd82020-03-17 23:12:27 +1030128 assert(offset >= 0L);
Andrew Jeffery0247c732020-02-06 11:48:52 +1030129 assert(offset + len < mmio->lpc_size);
130
131 memcpy(mmio->lpc + offset, buf, len);
132
Andrew Jeffery0247c732020-02-06 11:48:52 +1030133 return 0;
134}
135
Andrew Jefferyb93b6112020-06-05 14:13:44 +0930136#define __unused __attribute__((unused))
137
138static void rx_message(uint8_t eid __unused, void *data __unused, void *msg,
139 size_t len)
Andrew Jeffery0247c732020-02-06 11:48:52 +1030140{
Andrew Jeffery5a7c2db2020-05-22 20:13:42 +0930141 struct astlpc_test *test = data;
Andrew Jeffery0247c732020-02-06 11:48:52 +1030142
Andrew Jeffery5a7c2db2020-05-22 20:13:42 +0930143 mctp_prdebug("MCTP message received: msg: %p, len %zd", msg, len);
Andrew Jeffery0247c732020-02-06 11:48:52 +1030144
Andrew Jeffery5a7c2db2020-05-22 20:13:42 +0930145 assert(len > 0);
146 assert(msg);
147 assert(test);
148 assert(test->msg);
149 assert(!memcmp(test->msg, msg, len));
150
151 test->count++;
Andrew Jeffery0247c732020-02-06 11:48:52 +1030152}
153
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930154static const struct mctp_binding_astlpc_ops astlpc_direct_mmio_ops = {
155 .kcs_read = mctp_astlpc_mmio_kcs_read,
156 .kcs_write = mctp_astlpc_mmio_kcs_write,
157};
158
159static const struct mctp_binding_astlpc_ops astlpc_indirect_mmio_ops = {
Andrew Jeffery0247c732020-02-06 11:48:52 +1030160 .kcs_read = mctp_astlpc_mmio_kcs_read,
161 .kcs_write = mctp_astlpc_mmio_kcs_write,
162 .lpc_read = mctp_astlpc_mmio_lpc_read,
163 .lpc_write = mctp_astlpc_mmio_lpc_write,
164};
165
Andrew Jefferyd5e3cd72020-05-12 22:10:22 +0930166static void endpoint_init(struct astlpc_endpoint *ep, mctp_eid_t eid,
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930167 uint8_t mode, uint8_t (*kcs)[2], void *lpc_mem)
Andrew Jefferyd5e3cd72020-05-12 22:10:22 +0930168{
169 /*
170 * Configure the direction of the KCS interface so we know whether to
171 * set or clear IBF or OBF on writes or reads.
172 */
173 ep->mmio.bmc = (mode == MCTP_BINDING_ASTLPC_MODE_BMC);
174
175 ep->mctp = mctp_init();
176 assert(ep->mctp);
177
Andrew Jefferyd5e3cd72020-05-12 22:10:22 +0930178 /* Inject KCS registers */
179 ep->mmio.kcs = kcs;
180
Andrew Jefferyd5e3cd72020-05-12 22:10:22 +0930181 /* Initialise the binding */
Andrew Jeffery129ef932020-05-22 16:24:19 +0930182 ep->astlpc = mctp_astlpc_init(mode, MCTP_BTU, lpc_mem,
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930183 &astlpc_direct_mmio_ops, &ep->mmio);
Andrew Jefferyd5e3cd72020-05-12 22:10:22 +0930184
185 mctp_register_bus(ep->mctp, &ep->astlpc->binding, eid);
186}
187
188static void endpoint_destroy(struct astlpc_endpoint *ep)
189{
190 mctp_astlpc_destroy(ep->astlpc);
191 mctp_destroy(ep->mctp);
192}
193
Andrew Jefferycb5d55c2020-05-22 13:26:27 +0930194static void network_init(struct astlpc_test *ctx)
195{
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930196 ctx->lpc_mem = calloc(1, 1 * 1024 * 1024);
Andrew Jefferycb5d55c2020-05-22 13:26:27 +0930197 assert(ctx->lpc_mem);
198
199 /* BMC initialisation */
200 endpoint_init(&ctx->bmc, 8, MCTP_BINDING_ASTLPC_MODE_BMC, &ctx->kcs,
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930201 ctx->lpc_mem);
Andrew Jeffery3f325072020-05-28 09:19:51 +0930202 assert(ctx->kcs[MCTP_ASTLPC_KCS_REG_STATUS] & KCS_STATUS_BMC_READY);
Andrew Jefferycb5d55c2020-05-22 13:26:27 +0930203
204 /* Host initialisation */
205 endpoint_init(&ctx->host, 9, MCTP_BINDING_ASTLPC_MODE_HOST, &ctx->kcs,
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930206 ctx->lpc_mem);
Andrew Jefferycb5d55c2020-05-22 13:26:27 +0930207
208 /* BMC processes host channel init request, alerts host */
209 mctp_astlpc_poll(ctx->bmc.astlpc);
Andrew Jeffery3f325072020-05-28 09:19:51 +0930210 assert(ctx->kcs[MCTP_ASTLPC_KCS_REG_STATUS] &
211 KCS_STATUS_CHANNEL_ACTIVE);
212 assert(ctx->kcs[MCTP_ASTLPC_KCS_REG_DATA] == 0xff);
Andrew Jefferycb5d55c2020-05-22 13:26:27 +0930213
214 /* Host dequeues channel init result */
215 mctp_astlpc_poll(ctx->host.astlpc);
216}
217
218static void network_destroy(struct astlpc_test *ctx)
219{
220 endpoint_destroy(&ctx->bmc);
221 endpoint_destroy(&ctx->host);
222 free(ctx->lpc_mem);
223}
224
Andrew Jeffery129ef932020-05-22 16:24:19 +0930225static void astlpc_assert_tx_packet(struct astlpc_endpoint *src,
226 const void *expected, size_t len)
227{
228 const size_t tx_body = src->astlpc->layout.tx.offset + 4 + 4;
229 const void *test = ((char *)src->astlpc->lpc_map) + tx_body;
230 assert(!memcmp(test, expected, len));
231}
232
Andrew Jefferye756de82020-05-22 12:30:58 +0930233static void astlpc_test_packetised_message_bmc_to_host(void)
Andrew Jefferyd5e3cd72020-05-12 22:10:22 +0930234{
Andrew Jefferycb5d55c2020-05-22 13:26:27 +0930235 struct astlpc_test ctx = { 0 };
Andrew Jefferyd5e3cd72020-05-12 22:10:22 +0930236 uint8_t msg[2 * MCTP_BTU];
Andrew Jeffery0247c732020-02-06 11:48:52 +1030237 int rc;
238
Andrew Jefferyd5e3cd72020-05-12 22:10:22 +0930239 /* Test harness initialisation */
240
Andrew Jeffery5a7c2db2020-05-22 20:13:42 +0930241 network_init(&ctx);
242
Andrew Jeffery0247c732020-02-06 11:48:52 +1030243 memset(&msg[0], 0x5a, MCTP_BTU);
244 memset(&msg[MCTP_BTU], 0xa5, MCTP_BTU);
245
Andrew Jeffery5a7c2db2020-05-22 20:13:42 +0930246 ctx.msg = &msg[0];
247 ctx.count = 0;
248 mctp_set_rx_all(ctx.host.mctp, rx_message, &ctx);
Andrew Jeffery0247c732020-02-06 11:48:52 +1030249
250 /* BMC sends a message */
Andrew Jefferycb5d55c2020-05-22 13:26:27 +0930251 rc = mctp_message_tx(ctx.bmc.mctp, 9, msg, sizeof(msg));
Andrew Jeffery0247c732020-02-06 11:48:52 +1030252 assert(rc == 0);
Andrew Jefferyd5e3cd72020-05-12 22:10:22 +0930253
Andrew Jeffery129ef932020-05-22 16:24:19 +0930254 /* Host receives the first packet */
Andrew Jefferycb5d55c2020-05-22 13:26:27 +0930255 mctp_astlpc_poll(ctx.host.astlpc);
Andrew Jeffery0247c732020-02-06 11:48:52 +1030256
Andrew Jeffery0247c732020-02-06 11:48:52 +1030257 /* BMC dequeues ownership hand-over and sends the queued packet */
Andrew Jefferycb5d55c2020-05-22 13:26:27 +0930258 rc = mctp_astlpc_poll(ctx.bmc.astlpc);
Andrew Jeffery0247c732020-02-06 11:48:52 +1030259 assert(rc == 0);
260
Andrew Jeffery129ef932020-05-22 16:24:19 +0930261 /* Host receives the next packet */
Andrew Jefferycb5d55c2020-05-22 13:26:27 +0930262 assert(ctx.kcs[MCTP_ASTLPC_KCS_REG_STATUS] & KCS_STATUS_OBF);
263 assert(ctx.kcs[MCTP_ASTLPC_KCS_REG_DATA] == 0x01);
Andrew Jeffery0247c732020-02-06 11:48:52 +1030264
Andrew Jeffery129ef932020-05-22 16:24:19 +0930265 astlpc_assert_tx_packet(&ctx.bmc, &msg[MCTP_BTU], MCTP_BTU);
Andrew Jeffery0247c732020-02-06 11:48:52 +1030266
Andrew Jeffery5a7c2db2020-05-22 20:13:42 +0930267 /* Host receives final packet */
268 mctp_astlpc_poll(ctx.host.astlpc);
269 assert(ctx.count == 1);
270
Andrew Jefferycb5d55c2020-05-22 13:26:27 +0930271 network_destroy(&ctx);
Andrew Jefferye756de82020-05-22 12:30:58 +0930272}
273
Andrew Jefferyec9a0062020-05-22 21:21:55 +0930274static void astlpc_test_simple_message_host_to_bmc(void)
275{
276 struct astlpc_test ctx = { 0 };
277 uint8_t msg[MCTP_BTU];
278 int rc;
279
280 /* Test harness initialisation */
281
282 network_init(&ctx);
283
284 memset(&msg[0], 0xa5, MCTP_BTU);
285
286 ctx.msg = &msg[0];
287 ctx.count = 0;
288 mctp_set_rx_all(ctx.bmc.mctp, rx_message, &ctx);
289
290 /* Host sends the single-packet message */
291 rc = mctp_message_tx(ctx.host.mctp, 8, msg, sizeof(msg));
292 assert(rc == 0);
293 assert(ctx.kcs[MCTP_ASTLPC_KCS_REG_STATUS] & KCS_STATUS_IBF);
294 assert(ctx.kcs[MCTP_ASTLPC_KCS_REG_DATA] == 0x01);
295
296 astlpc_assert_tx_packet(&ctx.host, &msg[0], MCTP_BTU);
297
298 /* BMC receives the single-packet message */
299 mctp_astlpc_poll(ctx.bmc.astlpc);
300 assert(ctx.count == 1);
301
302 /* BMC returns Tx area ownership to Host */
303 assert(!(ctx.kcs[MCTP_ASTLPC_KCS_REG_STATUS] & KCS_STATUS_IBF));
304 assert(ctx.kcs[MCTP_ASTLPC_KCS_REG_DATA] == 0x02);
305 assert(ctx.kcs[MCTP_ASTLPC_KCS_REG_STATUS] & KCS_STATUS_OBF);
306
307 /* Host dequeues ownership hand-over and sends the queued packet */
308 rc = mctp_astlpc_poll(ctx.host.astlpc);
309 assert(rc == 0);
310
311 network_destroy(&ctx);
312}
313
Andrew Jeffery8f3eb722020-05-22 20:23:49 +0930314static void astlpc_test_simple_message_bmc_to_host(void)
315{
316 struct astlpc_test ctx = { 0 };
317 uint8_t msg[MCTP_BTU];
318 int rc;
319
320 /* Test harness initialisation */
321
322 network_init(&ctx);
323
324 memset(&msg[0], 0x5a, MCTP_BTU);
325
326 ctx.msg = &msg[0];
327 ctx.count = 0;
328 mctp_set_rx_all(ctx.host.mctp, rx_message, &ctx);
329
330 /* BMC sends the single-packet message */
331 rc = mctp_message_tx(ctx.bmc.mctp, 9, msg, sizeof(msg));
332 assert(rc == 0);
333 assert(ctx.kcs[MCTP_ASTLPC_KCS_REG_STATUS] & KCS_STATUS_OBF);
334 assert(ctx.kcs[MCTP_ASTLPC_KCS_REG_DATA] == 0x01);
335
336 astlpc_assert_tx_packet(&ctx.bmc, &msg[0], MCTP_BTU);
337
338 /* Host receives the single-packet message */
339 mctp_astlpc_poll(ctx.host.astlpc);
340 assert(ctx.count == 1);
341
342 /* Host returns Rx area ownership to BMC */
343 assert(!(ctx.kcs[MCTP_ASTLPC_KCS_REG_STATUS] & KCS_STATUS_OBF));
344 assert(ctx.kcs[MCTP_ASTLPC_KCS_REG_DATA] == 0x02);
345 assert(ctx.kcs[MCTP_ASTLPC_KCS_REG_STATUS] & KCS_STATUS_IBF);
346
347 /* BMC dequeues ownership hand-over and sends the queued packet */
348 rc = mctp_astlpc_poll(ctx.bmc.astlpc);
349 assert(rc == 0);
350
351 network_destroy(&ctx);
352}
353
Andrew Jefferyf1cdb162020-05-23 21:25:21 +0930354static void astlpc_test_host_before_bmc(void)
355{
356 struct mctp_binding_astlpc_mmio mmio = { 0 };
357 struct mctp_binding_astlpc *astlpc;
358 uint8_t kcs[2] = { 0 };
359 struct mctp *mctp;
360 int rc;
361
362 mctp = mctp_init();
363 assert(mctp);
364
365 /* Inject KCS registers */
366 mmio.kcs = &kcs;
367
368 /* Initialise the binding */
369 astlpc = mctp_astlpc_init(MCTP_BINDING_ASTLPC_MODE_HOST, MCTP_BTU, NULL,
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930370 &astlpc_direct_mmio_ops, &mmio);
Andrew Jefferyf1cdb162020-05-23 21:25:21 +0930371
372 /* Register the binding to trigger the start-up sequence */
373 rc = mctp_register_bus(mctp, &astlpc->binding, 8);
374
375 /* Start-up should fail as we haven't initialised the BMC */
376 assert(rc < 0);
377
378 mctp_astlpc_destroy(astlpc);
379 mctp_destroy(mctp);
380}
381
Andrew Jefferyf1a21312020-05-22 12:48:21 +0930382static void astlpc_test_simple_init(void)
383{
384 struct astlpc_endpoint bmc, host;
385 uint8_t kcs[2] = { 0 };
Andrew Jefferyf1a21312020-05-22 12:48:21 +0930386 void *lpc_mem;
387
388 /* Test harness initialisation */
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930389 lpc_mem = calloc(1, 1 * 1024 * 1024);
Andrew Jefferyf1a21312020-05-22 12:48:21 +0930390 assert(lpc_mem);
391
Andrew Jeffery8f3eb722020-05-22 20:23:49 +0930392 /* BMC initialisation */
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930393 endpoint_init(&bmc, 8, MCTP_BINDING_ASTLPC_MODE_BMC, &kcs, lpc_mem);
Andrew Jefferyf1a21312020-05-22 12:48:21 +0930394
395 /* Verify the BMC binding was initialised */
396 assert(kcs[MCTP_ASTLPC_KCS_REG_STATUS] & KCS_STATUS_BMC_READY);
397
Andrew Jeffery8f3eb722020-05-22 20:23:49 +0930398 /* Host initialisation */
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930399 endpoint_init(&host, 9, MCTP_BINDING_ASTLPC_MODE_HOST, &kcs, lpc_mem);
Andrew Jefferyf1a21312020-05-22 12:48:21 +0930400
401 /* Host sends channel init command */
402 assert(kcs[MCTP_ASTLPC_KCS_REG_STATUS] & KCS_STATUS_IBF);
403 assert(kcs[MCTP_ASTLPC_KCS_REG_DATA] == 0x00);
404
405 /* BMC receives host channel init request */
406 mctp_astlpc_poll(bmc.astlpc);
407
408 /* BMC sends init response */
409 assert(kcs[MCTP_ASTLPC_KCS_REG_STATUS] & KCS_STATUS_OBF);
410 assert(kcs[MCTP_ASTLPC_KCS_REG_STATUS] & KCS_STATUS_CHANNEL_ACTIVE);
411 assert(kcs[MCTP_ASTLPC_KCS_REG_DATA] == 0xff);
412
413 /* Host dequeues data */
414 mctp_astlpc_poll(host.astlpc);
415
416 endpoint_destroy(&bmc);
417 endpoint_destroy(&host);
418 free(lpc_mem);
419}
420
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930421static void astlpc_test_simple_indirect_message_bmc_to_host(void)
422{
423 struct astlpc_test ctx = { 0 };
424 uint8_t kcs[2] = { 0 };
425 uint8_t msg[MCTP_BTU];
426 int rc;
427
428 ctx.lpc_mem = calloc(1, LPC_WIN_SIZE);
429 assert(ctx.lpc_mem);
430
431 /* Test message data */
432 memset(&msg[0], 0x5a, MCTP_BTU);
433
434 /* Manually set up the network so we can inject the indirect ops */
435
436 /* BMC initialisation */
437 ctx.bmc.mmio.bmc = true;
438 ctx.bmc.mctp = mctp_init();
439 assert(ctx.bmc.mctp);
440 ctx.bmc.mmio.kcs = &kcs;
441 ctx.bmc.mmio.lpc = ctx.lpc_mem;
442 ctx.bmc.mmio.lpc_size = LPC_WIN_SIZE;
443 ctx.bmc.astlpc =
444 mctp_astlpc_init(MCTP_BINDING_ASTLPC_MODE_BMC, MCTP_BTU, NULL,
445 &astlpc_indirect_mmio_ops, &ctx.bmc.mmio);
446 mctp_register_bus(ctx.bmc.mctp, &ctx.bmc.astlpc->binding, 8);
447
448 /* Host initialisation */
449 ctx.host.mmio.bmc = false;
450 ctx.host.mctp = mctp_init();
451 assert(ctx.host.mctp);
452 ctx.host.mmio.kcs = &kcs;
453 ctx.host.mmio.lpc = ctx.lpc_mem;
454 ctx.host.mmio.lpc_size = LPC_WIN_SIZE;
455 ctx.host.astlpc =
456 mctp_astlpc_init(MCTP_BINDING_ASTLPC_MODE_HOST, MCTP_BTU, NULL,
457 &astlpc_indirect_mmio_ops, &ctx.host.mmio);
458 mctp_register_bus(ctx.host.mctp, &ctx.host.astlpc->binding, 9);
459
460 /* BMC processes host channel init request, alerts host */
461 mctp_astlpc_poll(ctx.bmc.astlpc);
462
463 /* Host dequeues channel init result */
464 mctp_astlpc_poll(ctx.host.astlpc);
465
466 ctx.msg = &msg[0];
467 ctx.count = 0;
468 mctp_set_rx_all(ctx.host.mctp, rx_message, &ctx);
469
470 /* BMC sends the single-packet message */
471 rc = mctp_message_tx(ctx.bmc.mctp, 9, msg, sizeof(msg));
472 assert(rc == 0);
473
474 /* Host receives the single-packet message */
475 rc = mctp_astlpc_poll(ctx.host.astlpc);
476 assert(rc == 0);
477 assert(ctx.count == 1);
478
479 /* BMC dequeues ownership hand-over and sends the queued packet */
480 rc = mctp_astlpc_poll(ctx.bmc.astlpc);
481 assert(rc == 0);
482
483 /* Can still tear-down the network in the normal fashion */
484 network_destroy(&ctx);
485}
486
Andrew Jefferyd0f5da02020-05-28 09:12:55 +0930487static void astlpc_test_host_tx_bmc_gone(void)
488{
489 struct astlpc_test ctx = { 0 };
490 uint8_t unwritten[MCTP_BTU];
491 uint8_t msg[MCTP_BTU];
492 int rc;
493
494 /* Test harness initialisation */
495
496 network_init(&ctx);
497
498 memset(&msg[0], 0x5a, sizeof(msg));
499 memset(&unwritten[0], 0, sizeof(unwritten));
500
501 ctx.msg = &msg[0];
502 ctx.count = 0;
503
504 /* Clear bmc-ready */
505 endpoint_destroy(&ctx.bmc);
506
507 /* Host detects that the BMC is disabled */
508 mctp_astlpc_poll(ctx.host.astlpc);
509
510 /* Host attempts to send the single-packet message, but is prevented */
511 rc = mctp_message_tx(ctx.host.mctp, 8, msg, sizeof(msg));
512 assert(rc == 0);
513 assert(!(ctx.kcs[MCTP_ASTLPC_KCS_REG_STATUS] & KCS_STATUS_OBF));
514 astlpc_assert_tx_packet(&ctx.host, &unwritten[0], MCTP_BTU);
515
516 /* BMC comes back */
517 endpoint_init(&ctx.bmc, 8, MCTP_BINDING_ASTLPC_MODE_BMC, &ctx.kcs,
518 ctx.lpc_mem);
519 mctp_set_rx_all(ctx.bmc.mctp, rx_message, &ctx);
520
521 /* Host triggers channel init */
522 mctp_astlpc_poll(ctx.host.astlpc);
523
524 /* BMC handles channel init */
525 mctp_astlpc_poll(ctx.bmc.astlpc);
526
527 /* Host completes channel init, flushing the Tx queue */
528 mctp_astlpc_poll(ctx.host.astlpc);
529
530 /* BMC receives the single-packet message */
531 mctp_astlpc_poll(ctx.bmc.astlpc);
532 assert(ctx.count == 1);
533
534 network_destroy(&ctx);
535}
536
Andrew Jefferyf3d94dc2020-05-28 10:50:18 +0930537static void astlpc_test_poll_not_ready(void)
538{
539 struct astlpc_endpoint bmc;
540 uint8_t kcs[2] = { 0 };
541 void *lpc_mem;
542 int rc;
543
544 /* Test harness initialisation */
545 lpc_mem = calloc(1, 1 * 1024 * 1024);
546 assert(lpc_mem);
547
548 /* BMC initialisation */
549 endpoint_init(&bmc, 8, MCTP_BINDING_ASTLPC_MODE_BMC, &kcs, lpc_mem);
550
551 /* Check for a command despite none present */
552 rc = mctp_astlpc_poll(bmc.astlpc);
553
554 /* Make sure it doesn't fail */
555 assert(rc == 0);
556
557 endpoint_destroy(&bmc);
558 free(lpc_mem);
559}
560
Andrew Jeffery91f09ed2020-05-22 20:52:26 +0930561/* clang-format off */
562#define TEST_CASE(test) { #test, test }
563static const struct {
564 const char *name;
565 void (*test)(void);
566} astlpc_tests[] = {
567 TEST_CASE(astlpc_test_simple_init),
Andrew Jefferyf1cdb162020-05-23 21:25:21 +0930568 TEST_CASE(astlpc_test_host_before_bmc),
Andrew Jeffery91f09ed2020-05-22 20:52:26 +0930569 TEST_CASE(astlpc_test_simple_message_bmc_to_host),
Andrew Jefferyec9a0062020-05-22 21:21:55 +0930570 TEST_CASE(astlpc_test_simple_message_host_to_bmc),
Andrew Jeffery91f09ed2020-05-22 20:52:26 +0930571 TEST_CASE(astlpc_test_packetised_message_bmc_to_host),
Andrew Jefferyd0f5da02020-05-28 09:12:55 +0930572 TEST_CASE(astlpc_test_simple_indirect_message_bmc_to_host),
573 TEST_CASE(astlpc_test_host_tx_bmc_gone),
Andrew Jefferyf3d94dc2020-05-28 10:50:18 +0930574 TEST_CASE(astlpc_test_poll_not_ready),
Andrew Jeffery91f09ed2020-05-22 20:52:26 +0930575};
576/* clang-format on */
577
578#ifndef BUILD_ASSERT
579#define BUILD_ASSERT(x) \
580 do { \
581 (void)sizeof(char[0 - (!(x))]); \
582 } while (0)
583#endif
584
Andrew Jefferye756de82020-05-22 12:30:58 +0930585int main(void)
586{
Andrew Jeffery91f09ed2020-05-22 20:52:26 +0930587 size_t i;
588
Andrew Jefferye756de82020-05-22 12:30:58 +0930589 mctp_set_log_stdio(MCTP_LOG_DEBUG);
590
Andrew Jeffery91f09ed2020-05-22 20:52:26 +0930591 BUILD_ASSERT(ARRAY_SIZE(astlpc_tests) < SIZE_MAX);
592 for (i = 0; i < ARRAY_SIZE(astlpc_tests); i++) {
593 mctp_prlog(MCTP_LOG_DEBUG, "begin: %s", astlpc_tests[i].name);
594 astlpc_tests[i].test();
595 mctp_prlog(MCTP_LOG_DEBUG, "end: %s\n", astlpc_tests[i].name);
596 }
Andrew Jeffery11b7e922020-03-10 23:37:09 +1030597
Andrew Jeffery0247c732020-02-06 11:48:52 +1030598 return 0;
599}