blob: 5cee3551552a9045f58fbe715ee09213fc91806a [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)
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 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);
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 Jeffery11b7e922020-03-10 23:37:09 +1030194 mctp_astlpc_destroy(astlpc);
195 mctp_destroy(mctp);
196 free(mmio.lpc);
197
Andrew Jeffery0247c732020-02-06 11:48:52 +1030198 return 0;
199}