blob: 693ce60de3b7e98b2cedc755a869b39d1c46cec4 [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 Jeffery3a540662020-05-26 19:55:30 +09307#define ASTLPC_VER_CUR 2
Andrew Jefferydf5f6b92020-05-12 21:39:06 +09308#include "astlpc.c"
9
10#ifdef pr_fmt
11#undef pr_fmt
12#define pr_fmt(x) "test: " x
13#endif
14
Andrew Jeffery0247c732020-02-06 11:48:52 +103015#include "libmctp-astlpc.h"
16#include "libmctp-log.h"
Przemyslaw Czarnowskiff25d7e2020-03-26 11:39:37 +010017#include "container_of.h"
Andrew Jeffery0247c732020-02-06 11:48:52 +103018
19#ifdef NDEBUG
20#undef NDEBUG
21#endif
22
Andrew Jeffery0247c732020-02-06 11:48:52 +103023#include <assert.h>
Andrew Jeffery91f09ed2020-05-22 20:52:26 +093024#include <limits.h>
Andrew Jeffery0247c732020-02-06 11:48:52 +103025#include <stdint.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
Andrew Jefferyc2b833e2020-10-28 14:28:37 +103029#include <sys/random.h>
Andrew Jeffery0247c732020-02-06 11:48:52 +103030
Andrew Jeffery91f09ed2020-05-22 20:52:26 +093031#ifndef ARRAY_SIZE
32#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
33#endif
34
Andrew Jeffery0247c732020-02-06 11:48:52 +103035struct mctp_binding_astlpc_mmio {
36 struct mctp_binding_astlpc astlpc;
Andrew Jefferyd5e3cd72020-05-12 22:10:22 +093037 bool bmc;
Andrew Jeffery0247c732020-02-06 11:48:52 +103038
Andrew Jefferyd5e3cd72020-05-12 22:10:22 +093039 uint8_t (*kcs)[2];
Andrew Jeffery0247c732020-02-06 11:48:52 +103040
41 size_t lpc_size;
42 uint8_t *lpc;
43};
44
Andrew Jeffery5a7c2db2020-05-22 20:13:42 +093045struct astlpc_endpoint {
46 struct mctp_binding_astlpc_mmio mmio;
47 struct mctp_binding_astlpc *astlpc;
48 struct mctp *mctp;
49};
50
51struct astlpc_test {
52 struct astlpc_endpoint bmc;
53 struct astlpc_endpoint host;
54 uint8_t kcs[2];
55 uint8_t *lpc_mem;
56
57 void *msg;
58 uint8_t count;
59};
60
61#define binding_to_mmio(b) \
Andrew Jeffery0247c732020-02-06 11:48:52 +103062 container_of(b, struct mctp_binding_astlpc_mmio, astlpc)
63
Andrew Jeffery53ea1472020-05-23 21:06:24 +093064static int mctp_astlpc_mmio_kcs_read(void *data,
65 enum mctp_binding_astlpc_kcs_reg reg,
66 uint8_t *val)
Andrew Jeffery0247c732020-02-06 11:48:52 +103067{
68 struct mctp_binding_astlpc_mmio *mmio = binding_to_mmio(data);
69
Andrew Jefferyd5e3cd72020-05-12 22:10:22 +093070 *val = (*mmio->kcs)[reg];
Andrew Jeffery0247c732020-02-06 11:48:52 +103071
Andrew Jefferyd3c0bf02020-05-28 15:28:40 +093072 mctp_prdebug("%s: 0x%hhx from %s", __func__, *val,
73 reg ? "status" : "data");
Andrew Jeffery0247c732020-02-06 11:48:52 +103074
Andrew Jefferyd5e3cd72020-05-12 22:10:22 +093075 if (reg == MCTP_ASTLPC_KCS_REG_DATA) {
76 uint8_t flag = mmio->bmc ? KCS_STATUS_IBF : KCS_STATUS_OBF;
77 (*mmio->kcs)[MCTP_ASTLPC_KCS_REG_STATUS] &= ~flag;
78 }
Andrew Jeffery0247c732020-02-06 11:48:52 +103079
80 return 0;
81}
82
Andrew Jeffery53ea1472020-05-23 21:06:24 +093083static int mctp_astlpc_mmio_kcs_write(void *data,
84 enum mctp_binding_astlpc_kcs_reg reg,
85 uint8_t val)
Andrew Jeffery0247c732020-02-06 11:48:52 +103086{
87 struct mctp_binding_astlpc_mmio *mmio = binding_to_mmio(data);
Andrew Jefferyd5e3cd72020-05-12 22:10:22 +093088 uint8_t *regp;
Andrew Jeffery0247c732020-02-06 11:48:52 +103089
Andrew Jefferyd5e3cd72020-05-12 22:10:22 +093090 assert(reg == MCTP_ASTLPC_KCS_REG_DATA ||
91 reg == MCTP_ASTLPC_KCS_REG_STATUS);
Andrew Jeffery0247c732020-02-06 11:48:52 +103092
Andrew Jefferyd5e3cd72020-05-12 22:10:22 +093093 if (reg == MCTP_ASTLPC_KCS_REG_DATA) {
94 uint8_t flag = mmio->bmc ? KCS_STATUS_OBF : KCS_STATUS_IBF;
95 (*mmio->kcs)[MCTP_ASTLPC_KCS_REG_STATUS] |= flag;
96 }
97
98 regp = &(*mmio->kcs)[reg];
Andrew Jeffery0247c732020-02-06 11:48:52 +103099 if (reg == MCTP_ASTLPC_KCS_REG_STATUS)
Andrew Jefferyd5e3cd72020-05-12 22:10:22 +0930100 *regp = (val & ~0xbU) | (val & *regp & 1);
Andrew Jeffery0247c732020-02-06 11:48:52 +1030101 else
Andrew Jefferyd5e3cd72020-05-12 22:10:22 +0930102 *regp = val;
Andrew Jeffery0247c732020-02-06 11:48:52 +1030103
Andrew Jefferyd3c0bf02020-05-28 15:28:40 +0930104 mctp_prdebug("%s: 0x%hhx to %s", __func__, val,
105 reg ? "status" : "data");
Andrew Jeffery0247c732020-02-06 11:48:52 +1030106
107 return 0;
108}
Andrew Jeffery85c04e42020-10-28 14:27:15 +1030109
110static const struct mctp_binding_astlpc_ops astlpc_direct_mmio_ops = {
111 .kcs_read = mctp_astlpc_mmio_kcs_read,
112 .kcs_write = mctp_astlpc_mmio_kcs_write,
113};
114
Andrew Jeffery06b2cd82020-03-17 23:12:27 +1030115int mctp_astlpc_mmio_lpc_read(void *data, void *buf, long offset, size_t len)
Andrew Jeffery0247c732020-02-06 11:48:52 +1030116{
117 struct mctp_binding_astlpc_mmio *mmio = binding_to_mmio(data);
118
Andrew Jeffery1dbf0212020-05-12 13:53:50 +0930119 mctp_prdebug("%s: %zu bytes from 0x%lx", __func__, len, offset);
120
Andrew Jeffery06b2cd82020-03-17 23:12:27 +1030121 assert(offset >= 0L);
Andrew Jeffery0247c732020-02-06 11:48:52 +1030122 assert(offset + len < mmio->lpc_size);
123
124 memcpy(buf, mmio->lpc + offset, len);
125
Andrew Jeffery0247c732020-02-06 11:48:52 +1030126 return 0;
127}
128
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930129int mctp_astlpc_mmio_lpc_write(void *data, const void *buf, long offset,
130 size_t len)
Andrew Jeffery0247c732020-02-06 11:48:52 +1030131{
132 struct mctp_binding_astlpc_mmio *mmio = binding_to_mmio(data);
133
Andrew Jeffery1dbf0212020-05-12 13:53:50 +0930134 mctp_prdebug("%s: %zu bytes to 0x%lx", __func__, len, offset);
135
Andrew Jeffery06b2cd82020-03-17 23:12:27 +1030136 assert(offset >= 0L);
Andrew Jeffery0247c732020-02-06 11:48:52 +1030137 assert(offset + len < mmio->lpc_size);
138
139 memcpy(mmio->lpc + offset, buf, len);
140
Andrew Jeffery0247c732020-02-06 11:48:52 +1030141 return 0;
142}
143
Andrew Jeffery85c04e42020-10-28 14:27:15 +1030144static const struct mctp_binding_astlpc_ops astlpc_indirect_mmio_ops = {
145 .kcs_read = mctp_astlpc_mmio_kcs_read,
146 .kcs_write = mctp_astlpc_mmio_kcs_write,
147 .lpc_read = mctp_astlpc_mmio_lpc_read,
148 .lpc_write = mctp_astlpc_mmio_lpc_write,
149};
150
Andrew Jefferyb93b6112020-06-05 14:13:44 +0930151#define __unused __attribute__((unused))
152
153static void rx_message(uint8_t eid __unused, void *data __unused, void *msg,
154 size_t len)
Andrew Jeffery0247c732020-02-06 11:48:52 +1030155{
Andrew Jeffery5a7c2db2020-05-22 20:13:42 +0930156 struct astlpc_test *test = data;
Andrew Jeffery0247c732020-02-06 11:48:52 +1030157
Andrew Jeffery5a7c2db2020-05-22 20:13:42 +0930158 mctp_prdebug("MCTP message received: msg: %p, len %zd", msg, len);
Andrew Jeffery0247c732020-02-06 11:48:52 +1030159
Andrew Jeffery5a7c2db2020-05-22 20:13:42 +0930160 assert(len > 0);
161 assert(msg);
162 assert(test);
163 assert(test->msg);
164 assert(!memcmp(test->msg, msg, len));
165
166 test->count++;
Andrew Jeffery0247c732020-02-06 11:48:52 +1030167}
168
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930169static int endpoint_init(struct astlpc_endpoint *ep, mctp_eid_t eid,
Andrew Jefferya9368982020-06-09 13:07:39 +0930170 uint8_t mode, uint32_t mtu, uint8_t (*kcs)[2],
171 void *lpc_mem)
Andrew Jefferyd5e3cd72020-05-12 22:10:22 +0930172{
173 /*
174 * Configure the direction of the KCS interface so we know whether to
175 * set or clear IBF or OBF on writes or reads.
176 */
177 ep->mmio.bmc = (mode == MCTP_BINDING_ASTLPC_MODE_BMC);
178
179 ep->mctp = mctp_init();
180 assert(ep->mctp);
181
Andrew Jefferyd5e3cd72020-05-12 22:10:22 +0930182 /* Inject KCS registers */
183 ep->mmio.kcs = kcs;
184
Andrew Jefferyd5e3cd72020-05-12 22:10:22 +0930185 /* Initialise the binding */
Andrew Jefferya9368982020-06-09 13:07:39 +0930186 ep->astlpc = mctp_astlpc_init(mode, mtu, lpc_mem,
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930187 &astlpc_direct_mmio_ops, &ep->mmio);
Andrew Jeffery3a540662020-05-26 19:55:30 +0930188 assert(ep->astlpc);
Andrew Jefferyd5e3cd72020-05-12 22:10:22 +0930189
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930190 return mctp_register_bus(ep->mctp, &ep->astlpc->binding, eid);
Andrew Jefferyd5e3cd72020-05-12 22:10:22 +0930191}
192
193static void endpoint_destroy(struct astlpc_endpoint *ep)
194{
195 mctp_astlpc_destroy(ep->astlpc);
196 mctp_destroy(ep->mctp);
197}
198
Andrew Jefferycb5d55c2020-05-22 13:26:27 +0930199static void network_init(struct astlpc_test *ctx)
200{
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930201 int rc;
202
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930203 ctx->lpc_mem = calloc(1, 1 * 1024 * 1024);
Andrew Jefferycb5d55c2020-05-22 13:26:27 +0930204 assert(ctx->lpc_mem);
205
206 /* BMC initialisation */
Andrew Jefferya9368982020-06-09 13:07:39 +0930207 rc = endpoint_init(&ctx->bmc, 8, MCTP_BINDING_ASTLPC_MODE_BMC, MCTP_BTU,
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930208 &ctx->kcs, ctx->lpc_mem);
209 assert(!rc);
Andrew Jeffery3f325072020-05-28 09:19:51 +0930210 assert(ctx->kcs[MCTP_ASTLPC_KCS_REG_STATUS] & KCS_STATUS_BMC_READY);
Andrew Jefferycb5d55c2020-05-22 13:26:27 +0930211
212 /* Host initialisation */
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930213 rc = endpoint_init(&ctx->host, 9, MCTP_BINDING_ASTLPC_MODE_HOST,
Andrew Jefferya9368982020-06-09 13:07:39 +0930214 MCTP_BTU, &ctx->kcs, ctx->lpc_mem);
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930215 assert(!rc);
Andrew Jefferycb5d55c2020-05-22 13:26:27 +0930216
217 /* BMC processes host channel init request, alerts host */
218 mctp_astlpc_poll(ctx->bmc.astlpc);
Andrew Jeffery3f325072020-05-28 09:19:51 +0930219 assert(ctx->kcs[MCTP_ASTLPC_KCS_REG_STATUS] &
220 KCS_STATUS_CHANNEL_ACTIVE);
221 assert(ctx->kcs[MCTP_ASTLPC_KCS_REG_DATA] == 0xff);
Andrew Jefferycb5d55c2020-05-22 13:26:27 +0930222
223 /* Host dequeues channel init result */
224 mctp_astlpc_poll(ctx->host.astlpc);
225}
226
227static void network_destroy(struct astlpc_test *ctx)
228{
229 endpoint_destroy(&ctx->bmc);
230 endpoint_destroy(&ctx->host);
231 free(ctx->lpc_mem);
232}
233
Andrew Jeffery129ef932020-05-22 16:24:19 +0930234static void astlpc_assert_tx_packet(struct astlpc_endpoint *src,
235 const void *expected, size_t len)
236{
237 const size_t tx_body = src->astlpc->layout.tx.offset + 4 + 4;
238 const void *test = ((char *)src->astlpc->lpc_map) + tx_body;
239 assert(!memcmp(test, expected, len));
240}
241
Andrew Jefferye756de82020-05-22 12:30:58 +0930242static void astlpc_test_packetised_message_bmc_to_host(void)
Andrew Jefferyd5e3cd72020-05-12 22:10:22 +0930243{
Andrew Jefferycb5d55c2020-05-22 13:26:27 +0930244 struct astlpc_test ctx = { 0 };
Andrew Jefferyd5e3cd72020-05-12 22:10:22 +0930245 uint8_t msg[2 * MCTP_BTU];
Andrew Jeffery0247c732020-02-06 11:48:52 +1030246 int rc;
247
Andrew Jefferyd5e3cd72020-05-12 22:10:22 +0930248 /* Test harness initialisation */
249
Andrew Jeffery5a7c2db2020-05-22 20:13:42 +0930250 network_init(&ctx);
251
Andrew Jeffery0247c732020-02-06 11:48:52 +1030252 memset(&msg[0], 0x5a, MCTP_BTU);
253 memset(&msg[MCTP_BTU], 0xa5, MCTP_BTU);
254
Andrew Jeffery5a7c2db2020-05-22 20:13:42 +0930255 ctx.msg = &msg[0];
256 ctx.count = 0;
257 mctp_set_rx_all(ctx.host.mctp, rx_message, &ctx);
Andrew Jeffery0247c732020-02-06 11:48:52 +1030258
259 /* BMC sends a message */
Andrew Jefferycb5d55c2020-05-22 13:26:27 +0930260 rc = mctp_message_tx(ctx.bmc.mctp, 9, msg, sizeof(msg));
Andrew Jeffery0247c732020-02-06 11:48:52 +1030261 assert(rc == 0);
Andrew Jefferyd5e3cd72020-05-12 22:10:22 +0930262
Andrew Jeffery129ef932020-05-22 16:24:19 +0930263 /* Host receives the first packet */
Andrew Jefferycb5d55c2020-05-22 13:26:27 +0930264 mctp_astlpc_poll(ctx.host.astlpc);
Andrew Jeffery0247c732020-02-06 11:48:52 +1030265
Andrew Jeffery0247c732020-02-06 11:48:52 +1030266 /* BMC dequeues ownership hand-over and sends the queued packet */
Andrew Jefferycb5d55c2020-05-22 13:26:27 +0930267 rc = mctp_astlpc_poll(ctx.bmc.astlpc);
Andrew Jeffery0247c732020-02-06 11:48:52 +1030268 assert(rc == 0);
269
Andrew Jeffery129ef932020-05-22 16:24:19 +0930270 /* Host receives the next packet */
Andrew Jefferycb5d55c2020-05-22 13:26:27 +0930271 assert(ctx.kcs[MCTP_ASTLPC_KCS_REG_STATUS] & KCS_STATUS_OBF);
272 assert(ctx.kcs[MCTP_ASTLPC_KCS_REG_DATA] == 0x01);
Andrew Jeffery0247c732020-02-06 11:48:52 +1030273
Andrew Jeffery129ef932020-05-22 16:24:19 +0930274 astlpc_assert_tx_packet(&ctx.bmc, &msg[MCTP_BTU], MCTP_BTU);
Andrew Jeffery0247c732020-02-06 11:48:52 +1030275
Andrew Jeffery5a7c2db2020-05-22 20:13:42 +0930276 /* Host receives final packet */
277 mctp_astlpc_poll(ctx.host.astlpc);
278 assert(ctx.count == 1);
279
Andrew Jefferycb5d55c2020-05-22 13:26:27 +0930280 network_destroy(&ctx);
Andrew Jefferye756de82020-05-22 12:30:58 +0930281}
282
Andrew Jefferyec9a0062020-05-22 21:21:55 +0930283static void astlpc_test_simple_message_host_to_bmc(void)
284{
285 struct astlpc_test ctx = { 0 };
286 uint8_t msg[MCTP_BTU];
287 int rc;
288
289 /* Test harness initialisation */
290
291 network_init(&ctx);
292
293 memset(&msg[0], 0xa5, MCTP_BTU);
294
295 ctx.msg = &msg[0];
296 ctx.count = 0;
297 mctp_set_rx_all(ctx.bmc.mctp, rx_message, &ctx);
298
299 /* Host sends the single-packet message */
300 rc = mctp_message_tx(ctx.host.mctp, 8, msg, sizeof(msg));
301 assert(rc == 0);
302 assert(ctx.kcs[MCTP_ASTLPC_KCS_REG_STATUS] & KCS_STATUS_IBF);
303 assert(ctx.kcs[MCTP_ASTLPC_KCS_REG_DATA] == 0x01);
304
305 astlpc_assert_tx_packet(&ctx.host, &msg[0], MCTP_BTU);
306
307 /* BMC receives the single-packet message */
308 mctp_astlpc_poll(ctx.bmc.astlpc);
309 assert(ctx.count == 1);
310
311 /* BMC returns Tx area ownership to Host */
312 assert(!(ctx.kcs[MCTP_ASTLPC_KCS_REG_STATUS] & KCS_STATUS_IBF));
313 assert(ctx.kcs[MCTP_ASTLPC_KCS_REG_DATA] == 0x02);
314 assert(ctx.kcs[MCTP_ASTLPC_KCS_REG_STATUS] & KCS_STATUS_OBF);
315
316 /* Host dequeues ownership hand-over and sends the queued packet */
317 rc = mctp_astlpc_poll(ctx.host.astlpc);
318 assert(rc == 0);
319
320 network_destroy(&ctx);
321}
322
Andrew Jeffery8f3eb722020-05-22 20:23:49 +0930323static void astlpc_test_simple_message_bmc_to_host(void)
324{
325 struct astlpc_test ctx = { 0 };
326 uint8_t msg[MCTP_BTU];
327 int rc;
328
329 /* Test harness initialisation */
330
331 network_init(&ctx);
332
333 memset(&msg[0], 0x5a, MCTP_BTU);
334
335 ctx.msg = &msg[0];
336 ctx.count = 0;
337 mctp_set_rx_all(ctx.host.mctp, rx_message, &ctx);
338
339 /* BMC sends the single-packet message */
340 rc = mctp_message_tx(ctx.bmc.mctp, 9, msg, sizeof(msg));
341 assert(rc == 0);
342 assert(ctx.kcs[MCTP_ASTLPC_KCS_REG_STATUS] & KCS_STATUS_OBF);
343 assert(ctx.kcs[MCTP_ASTLPC_KCS_REG_DATA] == 0x01);
344
345 astlpc_assert_tx_packet(&ctx.bmc, &msg[0], MCTP_BTU);
346
347 /* Host receives the single-packet message */
348 mctp_astlpc_poll(ctx.host.astlpc);
349 assert(ctx.count == 1);
350
351 /* Host returns Rx area ownership to BMC */
352 assert(!(ctx.kcs[MCTP_ASTLPC_KCS_REG_STATUS] & KCS_STATUS_OBF));
353 assert(ctx.kcs[MCTP_ASTLPC_KCS_REG_DATA] == 0x02);
354 assert(ctx.kcs[MCTP_ASTLPC_KCS_REG_STATUS] & KCS_STATUS_IBF);
355
356 /* BMC dequeues ownership hand-over and sends the queued packet */
357 rc = mctp_astlpc_poll(ctx.bmc.astlpc);
358 assert(rc == 0);
359
360 network_destroy(&ctx);
361}
362
Andrew Jefferyf1cdb162020-05-23 21:25:21 +0930363static void astlpc_test_host_before_bmc(void)
364{
365 struct mctp_binding_astlpc_mmio mmio = { 0 };
366 struct mctp_binding_astlpc *astlpc;
367 uint8_t kcs[2] = { 0 };
368 struct mctp *mctp;
369 int rc;
370
371 mctp = mctp_init();
372 assert(mctp);
373
374 /* Inject KCS registers */
375 mmio.kcs = &kcs;
376
377 /* Initialise the binding */
378 astlpc = mctp_astlpc_init(MCTP_BINDING_ASTLPC_MODE_HOST, MCTP_BTU, NULL,
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930379 &astlpc_direct_mmio_ops, &mmio);
Andrew Jefferyf1cdb162020-05-23 21:25:21 +0930380
381 /* Register the binding to trigger the start-up sequence */
382 rc = mctp_register_bus(mctp, &astlpc->binding, 8);
383
384 /* Start-up should fail as we haven't initialised the BMC */
385 assert(rc < 0);
386
387 mctp_astlpc_destroy(astlpc);
388 mctp_destroy(mctp);
389}
390
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930391static void astlpc_test_bad_version(void)
392{
393 assert(0 ==
394 mctp_astlpc_negotiate_version(ASTLPC_VER_BAD, ASTLPC_VER_CUR,
395 ASTLPC_VER_MIN, ASTLPC_VER_CUR));
396 assert(0 ==
397 mctp_astlpc_negotiate_version(ASTLPC_VER_MIN, ASTLPC_VER_BAD,
398 ASTLPC_VER_MIN, ASTLPC_VER_CUR));
399 assert(0 ==
400 mctp_astlpc_negotiate_version(ASTLPC_VER_MIN, ASTLPC_VER_CUR,
401 ASTLPC_VER_BAD, ASTLPC_VER_CUR));
402 assert(0 ==
403 mctp_astlpc_negotiate_version(ASTLPC_VER_MIN, ASTLPC_VER_CUR,
404 ASTLPC_VER_MIN, ASTLPC_VER_BAD));
405 assert(0 == mctp_astlpc_negotiate_version(
406 ASTLPC_VER_CUR + 1, ASTLPC_VER_CUR, ASTLPC_VER_MIN,
407 ASTLPC_VER_CUR + 1));
408 assert(0 == mctp_astlpc_negotiate_version(
409 ASTLPC_VER_MIN, ASTLPC_VER_CUR + 1,
410 ASTLPC_VER_CUR + 1, ASTLPC_VER_CUR));
411}
412
413static void astlpc_test_incompatible_versions(void)
414{
415 assert(0 == mctp_astlpc_negotiate_version(
416 ASTLPC_VER_CUR, ASTLPC_VER_CUR, ASTLPC_VER_CUR + 1,
417 ASTLPC_VER_CUR + 1));
418 assert(0 == mctp_astlpc_negotiate_version(
419 ASTLPC_VER_CUR + 1, ASTLPC_VER_CUR + 1,
420 ASTLPC_VER_CUR, ASTLPC_VER_CUR));
421}
422
423static void astlpc_test_choose_bmc_ver_cur(void)
424{
425 assert(2 == mctp_astlpc_negotiate_version(1, 2, 2, 3));
426}
427
428static void astlpc_test_choose_host_ver_cur(void)
429{
430 assert(2 == mctp_astlpc_negotiate_version(2, 3, 1, 2));
431}
432
433static void astlpc_test_version_host_fails_negotiation(void)
Andrew Jefferyf1a21312020-05-22 12:48:21 +0930434{
435 struct astlpc_endpoint bmc, host;
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930436 struct mctp_lpcmap_hdr *hdr;
Andrew Jefferyf1a21312020-05-22 12:48:21 +0930437 uint8_t kcs[2] = { 0 };
Andrew Jefferyf1a21312020-05-22 12:48:21 +0930438 void *lpc_mem;
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930439 int rc;
Andrew Jefferyf1a21312020-05-22 12:48:21 +0930440
441 /* Test harness initialisation */
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930442 lpc_mem = calloc(1, 1 * 1024 * 1024);
Andrew Jefferyf1a21312020-05-22 12:48:21 +0930443 assert(lpc_mem);
444
Andrew Jeffery8f3eb722020-05-22 20:23:49 +0930445 /* BMC initialisation */
Andrew Jefferya9368982020-06-09 13:07:39 +0930446 rc = endpoint_init(&bmc, 8, MCTP_BINDING_ASTLPC_MODE_BMC, MCTP_BTU,
447 &kcs, lpc_mem);
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930448 assert(!rc);
449
450 /* Now the BMC is initialised, break its version announcement */
451 hdr = lpc_mem;
452 hdr->bmc_ver_cur = ASTLPC_VER_BAD;
453
454 /* Host initialisation */
Andrew Jefferya9368982020-06-09 13:07:39 +0930455 rc = endpoint_init(&host, 9, MCTP_BINDING_ASTLPC_MODE_HOST, MCTP_BTU,
456 &kcs, lpc_mem);
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930457 assert(rc < 0);
458
459 endpoint_destroy(&bmc);
460 endpoint_destroy(&host);
461 free(lpc_mem);
462}
463
464static void astlpc_test_version_bmc_fails_negotiation(void)
465{
466 struct astlpc_endpoint bmc, host;
467 struct mctp_lpcmap_hdr *hdr;
468 uint8_t kcs[2] = { 0 };
469 void *lpc_mem;
470 int rc;
471
472 /* Test harness initialisation */
473 lpc_mem = calloc(1, 1 * 1024 * 1024);
474 assert(lpc_mem);
475
476 /* BMC initialisation */
Andrew Jefferya9368982020-06-09 13:07:39 +0930477 rc = endpoint_init(&bmc, 8, MCTP_BINDING_ASTLPC_MODE_BMC, MCTP_BTU,
478 &kcs, lpc_mem);
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930479 assert(!rc);
480
481 /* Host initialisation */
Andrew Jefferya9368982020-06-09 13:07:39 +0930482 rc = endpoint_init(&host, 9, MCTP_BINDING_ASTLPC_MODE_HOST, MCTP_BTU,
483 &kcs, lpc_mem);
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930484 assert(!rc);
485
486 /* Now the host is initialised, break its version announcement */
487 hdr = lpc_mem;
488 hdr->host_ver_cur = ASTLPC_VER_BAD;
489
490 /* Poll the BMC to detect the broken host version */
491 mctp_astlpc_poll(bmc.astlpc);
492 assert(!(kcs[MCTP_ASTLPC_KCS_REG_STATUS] & KCS_STATUS_CHANNEL_ACTIVE));
493
494 /* Poll the host so it detects failed negotiation */
495 rc = mctp_astlpc_poll(host.astlpc);
496 assert(rc < 0);
497
498 endpoint_destroy(&bmc);
499 endpoint_destroy(&host);
500 free(lpc_mem);
501}
502
503static void astlpc_test_simple_init(void)
504{
505 struct astlpc_endpoint bmc, host;
506 uint8_t kcs[2] = { 0 };
507 void *lpc_mem;
508 int rc;
509
510 /* Test harness initialisation */
511 lpc_mem = calloc(1, 1 * 1024 * 1024);
512 assert(lpc_mem);
513
514 /* BMC initialisation */
Andrew Jefferya9368982020-06-09 13:07:39 +0930515 rc = endpoint_init(&bmc, 8, MCTP_BINDING_ASTLPC_MODE_BMC, MCTP_BTU,
516 &kcs, lpc_mem);
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930517 assert(!rc);
Andrew Jefferyf1a21312020-05-22 12:48:21 +0930518
519 /* Verify the BMC binding was initialised */
520 assert(kcs[MCTP_ASTLPC_KCS_REG_STATUS] & KCS_STATUS_BMC_READY);
521
Andrew Jeffery8f3eb722020-05-22 20:23:49 +0930522 /* Host initialisation */
Andrew Jefferya9368982020-06-09 13:07:39 +0930523 rc = endpoint_init(&host, 9, MCTP_BINDING_ASTLPC_MODE_HOST, MCTP_BTU,
524 &kcs, lpc_mem);
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930525 assert(!rc);
Andrew Jefferyf1a21312020-05-22 12:48:21 +0930526
527 /* Host sends channel init command */
528 assert(kcs[MCTP_ASTLPC_KCS_REG_STATUS] & KCS_STATUS_IBF);
529 assert(kcs[MCTP_ASTLPC_KCS_REG_DATA] == 0x00);
530
531 /* BMC receives host channel init request */
532 mctp_astlpc_poll(bmc.astlpc);
533
534 /* BMC sends init response */
535 assert(kcs[MCTP_ASTLPC_KCS_REG_STATUS] & KCS_STATUS_OBF);
536 assert(kcs[MCTP_ASTLPC_KCS_REG_STATUS] & KCS_STATUS_CHANNEL_ACTIVE);
537 assert(kcs[MCTP_ASTLPC_KCS_REG_DATA] == 0xff);
538
539 /* Host dequeues data */
540 mctp_astlpc_poll(host.astlpc);
541
542 endpoint_destroy(&bmc);
543 endpoint_destroy(&host);
544 free(lpc_mem);
545}
546
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930547static void astlpc_test_simple_indirect_message_bmc_to_host(void)
548{
549 struct astlpc_test ctx = { 0 };
550 uint8_t kcs[2] = { 0 };
551 uint8_t msg[MCTP_BTU];
552 int rc;
553
554 ctx.lpc_mem = calloc(1, LPC_WIN_SIZE);
555 assert(ctx.lpc_mem);
556
557 /* Test message data */
558 memset(&msg[0], 0x5a, MCTP_BTU);
559
560 /* Manually set up the network so we can inject the indirect ops */
561
562 /* BMC initialisation */
563 ctx.bmc.mmio.bmc = true;
564 ctx.bmc.mctp = mctp_init();
565 assert(ctx.bmc.mctp);
566 ctx.bmc.mmio.kcs = &kcs;
567 ctx.bmc.mmio.lpc = ctx.lpc_mem;
568 ctx.bmc.mmio.lpc_size = LPC_WIN_SIZE;
569 ctx.bmc.astlpc =
570 mctp_astlpc_init(MCTP_BINDING_ASTLPC_MODE_BMC, MCTP_BTU, NULL,
571 &astlpc_indirect_mmio_ops, &ctx.bmc.mmio);
572 mctp_register_bus(ctx.bmc.mctp, &ctx.bmc.astlpc->binding, 8);
573
574 /* Host initialisation */
575 ctx.host.mmio.bmc = false;
576 ctx.host.mctp = mctp_init();
577 assert(ctx.host.mctp);
578 ctx.host.mmio.kcs = &kcs;
579 ctx.host.mmio.lpc = ctx.lpc_mem;
580 ctx.host.mmio.lpc_size = LPC_WIN_SIZE;
581 ctx.host.astlpc =
582 mctp_astlpc_init(MCTP_BINDING_ASTLPC_MODE_HOST, MCTP_BTU, NULL,
583 &astlpc_indirect_mmio_ops, &ctx.host.mmio);
584 mctp_register_bus(ctx.host.mctp, &ctx.host.astlpc->binding, 9);
585
586 /* BMC processes host channel init request, alerts host */
587 mctp_astlpc_poll(ctx.bmc.astlpc);
588
589 /* Host dequeues channel init result */
590 mctp_astlpc_poll(ctx.host.astlpc);
591
592 ctx.msg = &msg[0];
593 ctx.count = 0;
594 mctp_set_rx_all(ctx.host.mctp, rx_message, &ctx);
595
596 /* BMC sends the single-packet message */
597 rc = mctp_message_tx(ctx.bmc.mctp, 9, msg, sizeof(msg));
598 assert(rc == 0);
599
600 /* Host receives the single-packet message */
601 rc = mctp_astlpc_poll(ctx.host.astlpc);
602 assert(rc == 0);
603 assert(ctx.count == 1);
604
605 /* BMC dequeues ownership hand-over and sends the queued packet */
606 rc = mctp_astlpc_poll(ctx.bmc.astlpc);
607 assert(rc == 0);
608
609 /* Can still tear-down the network in the normal fashion */
610 network_destroy(&ctx);
611}
612
Andrew Jefferyd0f5da02020-05-28 09:12:55 +0930613static void astlpc_test_host_tx_bmc_gone(void)
614{
615 struct astlpc_test ctx = { 0 };
616 uint8_t unwritten[MCTP_BTU];
617 uint8_t msg[MCTP_BTU];
618 int rc;
619
620 /* Test harness initialisation */
621
622 network_init(&ctx);
623
624 memset(&msg[0], 0x5a, sizeof(msg));
625 memset(&unwritten[0], 0, sizeof(unwritten));
626
627 ctx.msg = &msg[0];
628 ctx.count = 0;
629
630 /* Clear bmc-ready */
631 endpoint_destroy(&ctx.bmc);
632
633 /* Host detects that the BMC is disabled */
634 mctp_astlpc_poll(ctx.host.astlpc);
635
636 /* Host attempts to send the single-packet message, but is prevented */
637 rc = mctp_message_tx(ctx.host.mctp, 8, msg, sizeof(msg));
638 assert(rc == 0);
639 assert(!(ctx.kcs[MCTP_ASTLPC_KCS_REG_STATUS] & KCS_STATUS_OBF));
640 astlpc_assert_tx_packet(&ctx.host, &unwritten[0], MCTP_BTU);
641
642 /* BMC comes back */
Andrew Jefferya9368982020-06-09 13:07:39 +0930643 rc = endpoint_init(&ctx.bmc, 8, MCTP_BINDING_ASTLPC_MODE_BMC, MCTP_BTU,
644 &ctx.kcs, ctx.lpc_mem);
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930645 assert(!rc);
Andrew Jefferyd0f5da02020-05-28 09:12:55 +0930646 mctp_set_rx_all(ctx.bmc.mctp, rx_message, &ctx);
647
648 /* Host triggers channel init */
649 mctp_astlpc_poll(ctx.host.astlpc);
650
651 /* BMC handles channel init */
652 mctp_astlpc_poll(ctx.bmc.astlpc);
653
654 /* Host completes channel init, flushing the Tx queue */
655 mctp_astlpc_poll(ctx.host.astlpc);
656
657 /* BMC receives the single-packet message */
658 mctp_astlpc_poll(ctx.bmc.astlpc);
659 assert(ctx.count == 1);
660
661 network_destroy(&ctx);
662}
663
Andrew Jefferyf3d94dc2020-05-28 10:50:18 +0930664static void astlpc_test_poll_not_ready(void)
665{
666 struct astlpc_endpoint bmc;
667 uint8_t kcs[2] = { 0 };
668 void *lpc_mem;
669 int rc;
670
671 /* Test harness initialisation */
672 lpc_mem = calloc(1, 1 * 1024 * 1024);
673 assert(lpc_mem);
674
675 /* BMC initialisation */
Andrew Jefferya9368982020-06-09 13:07:39 +0930676 rc = endpoint_init(&bmc, 8, MCTP_BINDING_ASTLPC_MODE_BMC, MCTP_BTU,
677 &kcs, lpc_mem);
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930678 assert(!rc);
Andrew Jefferyf3d94dc2020-05-28 10:50:18 +0930679
680 /* Check for a command despite none present */
681 rc = mctp_astlpc_poll(bmc.astlpc);
682
683 /* Make sure it doesn't fail */
684 assert(rc == 0);
685
686 endpoint_destroy(&bmc);
687 free(lpc_mem);
688}
689
Andrew Jeffery67655e82020-05-28 10:51:27 +0930690static void astlpc_test_undefined_command(void)
691{
692 struct astlpc_endpoint bmc;
693 uint8_t kcs[2] = { 0 };
694 void *lpc_mem;
695 int rc;
696
697 /* Test harness initialisation */
698 lpc_mem = calloc(1, 1 * 1024 * 1024);
699 assert(lpc_mem);
700
701 /* BMC initialisation */
Andrew Jefferya9368982020-06-09 13:07:39 +0930702 rc = endpoint_init(&bmc, 8, MCTP_BINDING_ASTLPC_MODE_BMC, MCTP_BTU,
703 &kcs, lpc_mem);
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930704 assert(!rc);
Andrew Jeffery67655e82020-05-28 10:51:27 +0930705
706 /* 0x5a isn't legal in v1 or v2 */
707 kcs[MCTP_ASTLPC_KCS_REG_DATA] = 0x5a;
708 kcs[MCTP_ASTLPC_KCS_REG_STATUS] |= KCS_STATUS_IBF;
709
710 /* Check for a command despite none present */
711 rc = mctp_astlpc_poll(bmc.astlpc);
712
713 /* Make sure it doesn't fail, bad command should be discarded */
714 assert(rc == 0);
715
716 endpoint_destroy(&bmc);
717 free(lpc_mem);
718}
719
Andrew Jeffery3a540662020-05-26 19:55:30 +0930720#define BUFFER_MIN ASTLPC_PACKET_SIZE(MCTP_PACKET_SIZE(MCTP_BTU))
721
722static void astlpc_test_buffers_rx_offset_overflow(void)
723{
724 struct mctp_astlpc_layout l = {
725 .rx = { UINT32_MAX, BUFFER_MIN },
726 .tx = { control_size, BUFFER_MIN },
727 };
728
729 assert(!mctp_astlpc_layout_validate(&l));
730}
731
732static void astlpc_test_buffers_tx_offset_overflow(void)
733{
734 struct mctp_astlpc_layout l = {
735 .rx = { control_size, BUFFER_MIN },
736 .tx = { UINT32_MAX, BUFFER_MIN },
737 };
738
739 assert(!mctp_astlpc_layout_validate(&l));
740}
741
742static void astlpc_test_buffers_rx_size_overflow(void)
743{
744 struct mctp_astlpc_layout l = {
745 .rx = { control_size + BUFFER_MIN, UINT32_MAX },
746 .tx = { control_size, BUFFER_MIN },
747 };
748
749 assert(!mctp_astlpc_layout_validate(&l));
750}
751
752static void astlpc_test_buffers_tx_size_overflow(void)
753{
754 struct mctp_astlpc_layout l = {
755 .rx = { control_size, BUFFER_MIN },
756 .tx = { control_size + BUFFER_MIN, UINT32_MAX },
757 };
758
759 assert(!mctp_astlpc_layout_validate(&l));
760}
761
762static void astlpc_test_buffers_rx_window_violation(void)
763{
764 struct mctp_astlpc_layout l = {
765 .rx = { LPC_WIN_SIZE - BUFFER_MIN + 1, BUFFER_MIN },
766 .tx = { control_size, BUFFER_MIN },
767 };
768
769 assert(!mctp_astlpc_layout_validate(&l));
770}
771
772static void astlpc_test_buffers_tx_window_violation(void)
773{
774 struct mctp_astlpc_layout l = {
775 .rx = { control_size, BUFFER_MIN },
776 .tx = { LPC_WIN_SIZE - BUFFER_MIN + 1, BUFFER_MIN },
777 };
778
779 assert(!mctp_astlpc_layout_validate(&l));
780}
781
782static void astlpc_test_buffers_rx_size_fails_btu(void)
783{
784 struct mctp_astlpc_layout l = {
785 .rx = { control_size, BUFFER_MIN - 1 },
786 .tx = { control_size + BUFFER_MIN, BUFFER_MIN },
787 };
788
789 assert(!mctp_astlpc_layout_validate(&l));
790}
791
792static void astlpc_test_buffers_tx_size_fails_btu(void)
793{
794 struct mctp_astlpc_layout l = {
795 .rx = { control_size, BUFFER_MIN },
796 .tx = { control_size + BUFFER_MIN, BUFFER_MIN - 1 },
797 };
798
799 assert(!mctp_astlpc_layout_validate(&l));
800}
801
802static void astlpc_test_buffers_overlap_rx_low(void)
803{
804 struct mctp_astlpc_layout l = {
805 .rx = { control_size, 2 * BUFFER_MIN },
806 .tx = { control_size + BUFFER_MIN, 2 * BUFFER_MIN },
807 };
808
809 assert(!mctp_astlpc_layout_validate(&l));
810}
811
812static void astlpc_test_buffers_overlap_tx_low(void)
813{
814 struct mctp_astlpc_layout l = {
815 .rx = { control_size + BUFFER_MIN, 2 * BUFFER_MIN },
816 .tx = { control_size, 2 * BUFFER_MIN },
817 };
818
819 assert(!mctp_astlpc_layout_validate(&l));
820}
821
822static void astlpc_test_buffers_overlap_exact(void)
823{
824 struct mctp_astlpc_layout l = {
825 .rx = { control_size, 2 * BUFFER_MIN },
826 .tx = { control_size, 2 * BUFFER_MIN },
827 };
828
829 assert(!mctp_astlpc_layout_validate(&l));
830}
831
832static void astlpc_test_buffers_overlap_control(void)
833{
834 struct mctp_astlpc_layout l = {
835 .rx = { 0, BUFFER_MIN },
836 .tx = { control_size + BUFFER_MIN, BUFFER_MIN },
837 };
838
839 assert(!mctp_astlpc_layout_validate(&l));
840}
841
842static void astlpc_test_buffers_bad_host_proposal(void)
843{
844 struct astlpc_endpoint bmc, host;
845 struct mctp_lpcmap_hdr *hdr;
846 uint8_t kcs[2] = { 0 };
847 void *lpc_mem;
Andrew Jefferya9368982020-06-09 13:07:39 +0930848 int rc;
Andrew Jeffery3a540662020-05-26 19:55:30 +0930849
850 /* Test harness initialisation */
851 lpc_mem = calloc(1, 1 * 1024 * 1024);
852 assert(lpc_mem);
853
854 /* BMC initialisation */
Andrew Jefferya9368982020-06-09 13:07:39 +0930855 rc = endpoint_init(&bmc, 8, MCTP_BINDING_ASTLPC_MODE_BMC, MCTP_BTU,
856 &kcs, lpc_mem);
857 assert(!rc);
Andrew Jeffery3a540662020-05-26 19:55:30 +0930858
859 /* Host initialisation */
Andrew Jefferya9368982020-06-09 13:07:39 +0930860 rc = endpoint_init(&host, 9, MCTP_BINDING_ASTLPC_MODE_HOST, MCTP_BTU,
861 &kcs, lpc_mem);
862 assert(!rc);
Andrew Jeffery3a540662020-05-26 19:55:30 +0930863
864 /*
865 * Now that the host has initialised the control area, break
866 * something before polling the BMC
867 */
868 hdr = lpc_mem;
869 hdr->layout.rx_size = 0;
870
871 mctp_astlpc_poll(bmc.astlpc);
872
873 /* Make sure the BMC has not set the channel to active */
874 assert(!(kcs[MCTP_ASTLPC_KCS_REG_STATUS] & KCS_STATUS_CHANNEL_ACTIVE));
875
876 endpoint_destroy(&host);
877 endpoint_destroy(&bmc);
878 free(lpc_mem);
879}
880
881static void astlpc_test_buffers_bad_bmc_proposal(void)
882{
883 struct astlpc_endpoint bmc, host;
884 struct mctp_lpcmap_hdr *hdr;
885 uint8_t kcs[2] = { 0 };
886 void *lpc_mem;
887 int rc;
888
889 /* Test harness initialisation */
890 lpc_mem = calloc(1, 1 * 1024 * 1024);
891 assert(lpc_mem);
892
893 /* BMC initialisation */
Andrew Jefferya9368982020-06-09 13:07:39 +0930894 rc = endpoint_init(&bmc, 8, MCTP_BINDING_ASTLPC_MODE_BMC, MCTP_BTU,
895 &kcs, lpc_mem);
896 assert(!rc);
Andrew Jeffery3a540662020-05-26 19:55:30 +0930897
898 /*
899 * Now that the BMC has initialised the control area, break something
900 * before initialising the host
901 */
902 hdr = lpc_mem;
903 hdr->layout.rx_size = 0;
904
905 /* Host initialisation: Fails due to bad layout */
Andrew Jefferya9368982020-06-09 13:07:39 +0930906 rc = endpoint_init(&host, 9, MCTP_BINDING_ASTLPC_MODE_HOST, MCTP_BTU,
907 &kcs, lpc_mem);
Andrew Jeffery3a540662020-05-26 19:55:30 +0930908 assert(rc < 0);
909
910 endpoint_destroy(&host);
911 endpoint_destroy(&bmc);
912 free(lpc_mem);
913}
914
915static void astlpc_test_buffers_bad_bmc_negotiation(void)
916{
917 struct astlpc_endpoint bmc, host;
918 struct mctp_lpcmap_hdr *hdr;
919 uint8_t kcs[2] = { 0 };
920 void *lpc_mem;
921 int rc;
922
923 /* Test harness initialisation */
924 lpc_mem = calloc(1, 1 * 1024 * 1024);
925 assert(lpc_mem);
926
927 /* BMC initialisation */
Andrew Jefferya9368982020-06-09 13:07:39 +0930928 rc = endpoint_init(&bmc, 8, MCTP_BINDING_ASTLPC_MODE_BMC, MCTP_BTU,
929 &kcs, lpc_mem);
930 assert(!rc);
Andrew Jeffery3a540662020-05-26 19:55:30 +0930931
932 /* Host initialisation */
Andrew Jefferya9368982020-06-09 13:07:39 +0930933 rc = endpoint_init(&host, 9, MCTP_BINDING_ASTLPC_MODE_HOST, MCTP_BTU,
934 &kcs, lpc_mem);
935 assert(!rc);
Andrew Jeffery3a540662020-05-26 19:55:30 +0930936
937 mctp_astlpc_poll(bmc.astlpc);
938
939 /*
940 * Now that the BMC has initialised the control area, break something
941 * before polling the host
942 */
943 hdr = lpc_mem;
944 hdr->layout.rx_size = 0;
945
946 rc = mctp_astlpc_poll(host.astlpc);
947 assert(rc < 0);
948
949 endpoint_destroy(&host);
950 endpoint_destroy(&bmc);
951 free(lpc_mem);
952}
953
Andrew Jefferya9368982020-06-09 13:07:39 +0930954static void astlpc_test_buffers_bad_host_init(void)
955{
956 struct astlpc_endpoint host;
957 uint8_t kcs[2] = { 0 };
958 void *lpc_mem;
959 int rc;
960
961 /* Test harness initialisation */
962 lpc_mem = calloc(1, 1 * 1024 * 1024);
963 assert(lpc_mem);
964
965 host.mctp = mctp_init();
966 assert(host.mctp);
967 host.mmio.kcs = &kcs;
968 host.mmio.bmc = false;
969
970 /* Set the MTU to 0 to provoke a failure */
971 host.astlpc =
972 mctp_astlpc_init(MCTP_BINDING_ASTLPC_MODE_HOST, 0, lpc_mem,
973 &astlpc_direct_mmio_ops, &host.mmio);
974
975 rc = mctp_register_bus(host.mctp, &host.astlpc->binding, 8);
976 assert(rc < 0);
977
978 mctp_astlpc_destroy(host.astlpc);
979 mctp_destroy(host.mctp);
980 free(lpc_mem);
981}
982
983static void astlpc_test_negotiate_increased_mtu(void)
984{
985 struct astlpc_endpoint bmc, host;
986 uint8_t kcs[2] = { 0 };
987 void *lpc_mem;
988 int rc;
989
990 /* Test harness initialisation */
991 lpc_mem = calloc(1, 1 * 1024 * 1024);
992 assert(lpc_mem);
993
994 /* BMC initialisation */
995 rc = endpoint_init(&bmc, 8, MCTP_BINDING_ASTLPC_MODE_BMC, 3 * MCTP_BTU,
996 &kcs, lpc_mem);
997 assert(!rc);
998
999 /* Host initialisation */
1000 rc = endpoint_init(&host, 9, MCTP_BINDING_ASTLPC_MODE_HOST,
1001 2 * MCTP_BTU, &kcs, lpc_mem);
1002 assert(!rc);
1003
1004 rc = mctp_astlpc_poll(bmc.astlpc);
1005 assert(rc == 0);
1006
1007 rc = mctp_astlpc_poll(host.astlpc);
1008 assert(rc == 0);
1009
1010 endpoint_destroy(&host);
1011 endpoint_destroy(&bmc);
1012 free(lpc_mem);
1013}
1014
1015static void astlpc_test_negotiate_mtu_low_high(void)
1016{
1017 struct astlpc_endpoint bmc, host;
1018 uint8_t kcs[2] = { 0 };
1019 void *lpc_mem;
1020 int rc;
1021
1022 /* Test harness initialisation */
1023 lpc_mem = calloc(1, 1 * 1024 * 1024);
1024 assert(lpc_mem);
1025
1026 /* BMC initialisation */
1027 rc = endpoint_init(&bmc, 8, MCTP_BINDING_ASTLPC_MODE_BMC, 3 * MCTP_BTU,
1028 &kcs, lpc_mem);
1029 assert(!rc);
1030
1031 /* Host initialisation with low MTU */
1032 rc = endpoint_init(&host, 9, MCTP_BINDING_ASTLPC_MODE_HOST,
1033 2 * MCTP_BTU, &kcs, lpc_mem);
1034 assert(!rc);
1035
1036 /* Process low MTU proposal */
1037 rc = mctp_astlpc_poll(bmc.astlpc);
1038 assert(rc == 0);
1039
1040 /* Accept low MTU proposal */
1041 rc = mctp_astlpc_poll(host.astlpc);
1042 assert(rc == 0);
1043
1044 assert(host.astlpc->layout.rx.size ==
1045 ASTLPC_PACKET_SIZE(MCTP_PACKET_SIZE(2 * MCTP_BTU)));
1046
1047 /* Tear-down the host so we can bring up a new one */
1048 endpoint_destroy(&host);
1049
1050 /*
1051 * Bring up a new host endpoint with a higher MTU than we previously
1052 * negotiated
1053 */
1054 rc = endpoint_init(&host, 9, MCTP_BINDING_ASTLPC_MODE_HOST,
1055 3 * MCTP_BTU, &kcs, lpc_mem);
1056 assert(!rc);
1057
1058 /* Process high MTU proposal */
1059 rc = mctp_astlpc_poll(bmc.astlpc);
1060 assert(rc == 0);
1061
1062 /* Accept high MTU proposal */
1063 rc = mctp_astlpc_poll(host.astlpc);
1064 assert(rc == 0);
1065
1066 assert(host.astlpc->layout.rx.size ==
1067 ASTLPC_PACKET_SIZE(MCTP_PACKET_SIZE(3 * MCTP_BTU)));
1068
1069 endpoint_destroy(&host);
1070 endpoint_destroy(&bmc);
1071 free(lpc_mem);
1072}
1073
Andrew Jefferyc2b833e2020-10-28 14:28:37 +10301074static void astlpc_test_send_large_packet(void)
1075{
1076 struct astlpc_endpoint *bmc, *host;
1077 struct astlpc_test ctx;
1078 uint8_t kcs[2] = { 0 };
1079 void *lpc_mem;
1080 int rc;
1081
1082 host = &ctx.host;
1083 bmc = &ctx.bmc;
1084
1085 /* Test harness initialisation */
1086 lpc_mem = calloc(1, 1 * 1024 * 1024);
1087 assert(lpc_mem);
1088
1089 /* BMC initialisation */
1090 rc = endpoint_init(bmc, 8, MCTP_BINDING_ASTLPC_MODE_BMC, 8192, &kcs,
1091 lpc_mem);
1092 assert(!rc);
1093
1094 /* Host initialisation */
1095 rc = endpoint_init(host, 9, MCTP_BINDING_ASTLPC_MODE_HOST, 8192, &kcs,
1096 lpc_mem);
1097 assert(!rc);
1098
1099 ctx.count = 0;
1100 mctp_set_rx_all(bmc->mctp, rx_message, &ctx);
1101
1102 rc = mctp_astlpc_poll(bmc->astlpc);
1103 assert(rc == 0);
1104
1105 rc = mctp_astlpc_poll(host->astlpc);
1106 assert(rc == 0);
1107
1108 ctx.msg = malloc(2 * MCTP_BODY_SIZE(8192));
1109 assert(ctx.msg);
1110
1111 memset(ctx.msg, 0x5a, 2 * MCTP_BODY_SIZE(8192));
1112
1113 rc = mctp_message_tx(host->mctp, 8, ctx.msg, 2 * MCTP_BODY_SIZE(8192));
1114 assert(rc == 0);
1115 rc = mctp_astlpc_poll(bmc->astlpc);
1116 assert(rc == 0);
1117 rc = mctp_astlpc_poll(host->astlpc);
1118 assert(rc == 0);
1119 rc = mctp_astlpc_poll(bmc->astlpc);
1120 assert(rc == 0);
1121 rc = mctp_astlpc_poll(host->astlpc);
1122 assert(rc == 0);
1123
1124 assert(ctx.count == 1);
1125
1126 free(ctx.msg);
1127 endpoint_destroy(host);
1128 endpoint_destroy(bmc);
1129 free(lpc_mem);
1130}
1131
Andrew Jeffery91f09ed2020-05-22 20:52:26 +09301132/* clang-format off */
1133#define TEST_CASE(test) { #test, test }
1134static const struct {
1135 const char *name;
1136 void (*test)(void);
1137} astlpc_tests[] = {
1138 TEST_CASE(astlpc_test_simple_init),
Andrew Jeffery4e8264b2020-05-23 20:34:33 +09301139 TEST_CASE(astlpc_test_bad_version),
1140 TEST_CASE(astlpc_test_incompatible_versions),
1141 TEST_CASE(astlpc_test_choose_bmc_ver_cur),
1142 TEST_CASE(astlpc_test_choose_host_ver_cur),
1143 TEST_CASE(astlpc_test_version_host_fails_negotiation),
1144 TEST_CASE(astlpc_test_version_bmc_fails_negotiation),
Andrew Jefferyf1cdb162020-05-23 21:25:21 +09301145 TEST_CASE(astlpc_test_host_before_bmc),
Andrew Jeffery91f09ed2020-05-22 20:52:26 +09301146 TEST_CASE(astlpc_test_simple_message_bmc_to_host),
Andrew Jefferyec9a0062020-05-22 21:21:55 +09301147 TEST_CASE(astlpc_test_simple_message_host_to_bmc),
Andrew Jeffery91f09ed2020-05-22 20:52:26 +09301148 TEST_CASE(astlpc_test_packetised_message_bmc_to_host),
Andrew Jefferyd0f5da02020-05-28 09:12:55 +09301149 TEST_CASE(astlpc_test_simple_indirect_message_bmc_to_host),
1150 TEST_CASE(astlpc_test_host_tx_bmc_gone),
Andrew Jefferyf3d94dc2020-05-28 10:50:18 +09301151 TEST_CASE(astlpc_test_poll_not_ready),
Andrew Jeffery67655e82020-05-28 10:51:27 +09301152 TEST_CASE(astlpc_test_undefined_command),
Andrew Jeffery3a540662020-05-26 19:55:30 +09301153 TEST_CASE(astlpc_test_buffers_rx_offset_overflow),
1154 TEST_CASE(astlpc_test_buffers_tx_offset_overflow),
1155 TEST_CASE(astlpc_test_buffers_rx_size_overflow),
1156 TEST_CASE(astlpc_test_buffers_tx_size_overflow),
1157 TEST_CASE(astlpc_test_buffers_rx_window_violation),
1158 TEST_CASE(astlpc_test_buffers_tx_window_violation),
1159 TEST_CASE(astlpc_test_buffers_rx_size_fails_btu),
1160 TEST_CASE(astlpc_test_buffers_tx_size_fails_btu),
1161 TEST_CASE(astlpc_test_buffers_overlap_rx_low),
1162 TEST_CASE(astlpc_test_buffers_overlap_tx_low),
1163 TEST_CASE(astlpc_test_buffers_bad_host_proposal),
1164 TEST_CASE(astlpc_test_buffers_bad_bmc_proposal),
1165 TEST_CASE(astlpc_test_buffers_bad_bmc_negotiation),
1166 TEST_CASE(astlpc_test_buffers_overlap_exact),
1167 TEST_CASE(astlpc_test_buffers_overlap_control),
Andrew Jefferya9368982020-06-09 13:07:39 +09301168 TEST_CASE(astlpc_test_buffers_bad_host_init),
1169 TEST_CASE(astlpc_test_negotiate_increased_mtu),
1170 TEST_CASE(astlpc_test_negotiate_mtu_low_high),
Andrew Jefferyc2b833e2020-10-28 14:28:37 +10301171 TEST_CASE(astlpc_test_send_large_packet),
Andrew Jeffery91f09ed2020-05-22 20:52:26 +09301172};
1173/* clang-format on */
1174
1175#ifndef BUILD_ASSERT
1176#define BUILD_ASSERT(x) \
1177 do { \
1178 (void)sizeof(char[0 - (!(x))]); \
1179 } while (0)
1180#endif
1181
Andrew Jefferye756de82020-05-22 12:30:58 +09301182int main(void)
1183{
Andrew Jeffery91f09ed2020-05-22 20:52:26 +09301184 size_t i;
1185
Andrew Jefferye756de82020-05-22 12:30:58 +09301186 mctp_set_log_stdio(MCTP_LOG_DEBUG);
1187
Andrew Jeffery91f09ed2020-05-22 20:52:26 +09301188 BUILD_ASSERT(ARRAY_SIZE(astlpc_tests) < SIZE_MAX);
1189 for (i = 0; i < ARRAY_SIZE(astlpc_tests); i++) {
1190 mctp_prlog(MCTP_LOG_DEBUG, "begin: %s", astlpc_tests[i].name);
1191 astlpc_tests[i].test();
1192 mctp_prlog(MCTP_LOG_DEBUG, "end: %s\n", astlpc_tests[i].name);
1193 }
Andrew Jeffery11b7e922020-03-10 23:37:09 +10301194
Andrew Jeffery0247c732020-02-06 11:48:52 +10301195 return 0;
1196}