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