blob: 2d637de0e89c98c52d7903723962d5a4eb19f0df [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 Jeffery06b2cd82020-03-17 23:12:27 +1030101 assert(offset >= 0L);
Andrew Jeffery0247c732020-02-06 11:48:52 +1030102 assert(offset + len < mmio->lpc_size);
103
104 memcpy(buf, mmio->lpc + offset, len);
105
Andrew Jeffery06b2cd82020-03-17 23:12:27 +1030106 mctp_prdebug("%s: %zu bytes from 0x%lx", __func__, len, offset);
Andrew Jeffery0247c732020-02-06 11:48:52 +1030107
108 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 Jeffery06b2cd82020-03-17 23:12:27 +1030115 assert(offset >= 0L);
Andrew Jeffery0247c732020-02-06 11:48:52 +1030116 assert(offset + len < mmio->lpc_size);
117
118 memcpy(mmio->lpc + offset, buf, len);
119
Andrew Jeffery06b2cd82020-03-17 23:12:27 +1030120 mctp_prdebug("%s: %zu bytes to 0x%lx", __func__, len, offset);
Andrew Jeffery0247c732020-02-06 11:48:52 +1030121
122 return 0;
123}
124
125static void rx_message(uint8_t eid, void *data, void *msg, size_t len)
126{
127 uint8_t type;
128
129 type = *(uint8_t *)msg;
130
131 mctp_prdebug("MCTP message received: len %zd, type %d",
132 len, type);
133}
134
135const struct mctp_binding_astlpc_ops mctp_binding_astlpc_mmio_ops = {
136 .kcs_read = mctp_astlpc_mmio_kcs_read,
137 .kcs_write = mctp_astlpc_mmio_kcs_write,
138 .lpc_read = mctp_astlpc_mmio_lpc_read,
139 .lpc_write = mctp_astlpc_mmio_lpc_write,
140};
141
142int main(void)
143{
144 struct mctp_binding_astlpc_mmio mmio;
145 struct mctp_binding_astlpc *astlpc;
146 uint8_t msg[2 * MCTP_BTU];
147 struct mctp *mctp;
148 int rc;
149
150 memset(&msg[0], 0x5a, MCTP_BTU);
151 memset(&msg[MCTP_BTU], 0xa5, MCTP_BTU);
152
153 mmio.lpc_size = 1 * 1024 * 1024;
154 mmio.lpc = calloc(1, mmio.lpc_size);
155
156 mctp_set_log_stdio(MCTP_LOG_DEBUG);
157
158 mctp = mctp_init();
159 assert(mctp);
160
161 mctp_set_rx_all(mctp, rx_message, NULL);
162
163 astlpc = mctp_astlpc_init_ops(&mctp_binding_astlpc_mmio_ops, &mmio, NULL);
164
165 mctp_register_bus(mctp, &astlpc->binding, 8);
166
167 /* Verify the binding was initialised */
168 assert(mmio.kcs[MCTP_ASTLPC_KCS_REG_STATUS] & KCS_STATUS_BMC_READY);
169
170 /* Host sends channel init command */
171 mmio.kcs[MCTP_ASTLPC_KCS_REG_STATUS] |= KCS_STATUS_IBF;
172 mmio.kcs[MCTP_ASTLPC_KCS_REG_DATA] = 0x00;
173
174 /* Receive host init */
175 mctp_astlpc_poll(astlpc);
176
177 /* Host receives init response */
178 assert(mmio.kcs[MCTP_ASTLPC_KCS_REG_STATUS] & KCS_STATUS_OBF);
179 assert(mmio.kcs[MCTP_ASTLPC_KCS_REG_STATUS] & KCS_STATUS_CHANNEL_ACTIVE);
180
181 /* Host dequeues data */
182 assert(mmio.kcs[MCTP_ASTLPC_KCS_REG_DATA] == 0xff);
183 mmio.kcs[MCTP_ASTLPC_KCS_REG_STATUS] &= ~KCS_STATUS_OBF;
184
185 /* BMC sends a message */
186 rc = mctp_message_tx(mctp, 9, msg, sizeof(msg));
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[0], MCTP_BTU));
195
196 /* Host returns Rx area ownership to BMC */
197 mmio.kcs[MCTP_ASTLPC_KCS_REG_STATUS] &= ~KCS_STATUS_OBF;
198 mmio.kcs[MCTP_ASTLPC_KCS_REG_DATA] = 0x02;
199 mmio.kcs[MCTP_ASTLPC_KCS_REG_STATUS] |= KCS_STATUS_IBF;
200
201 /* BMC dequeues ownership hand-over and sends the queued packet */
202 rc = mctp_astlpc_poll(astlpc);
203 assert(rc == 0);
204
205 /* Host receives a message */
206 assert(mmio.kcs[MCTP_ASTLPC_KCS_REG_STATUS] & KCS_STATUS_OBF);
207 assert(mmio.kcs[MCTP_ASTLPC_KCS_REG_DATA] == 0x01);
208
209 /* Verify it's the packet we expect */
210 assert(!memcmp(mmio.lpc + RX_BUFFER_DATA, &msg[MCTP_BTU], MCTP_BTU));
211
Andrew Jeffery11b7e922020-03-10 23:37:09 +1030212 mctp_astlpc_destroy(astlpc);
213 mctp_destroy(mctp);
214 free(mmio.lpc);
215
Andrew Jeffery0247c732020-02-06 11:48:52 +1030216 return 0;
217}