blob: 5218c926e2a5eeeee04d36c48c43bb4f6140aa10 [file] [log] [blame]
Jeremy Kerr3d36ee22019-05-30 11:15:37 +08001/* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */
Jeremy Kerr672c8852019-03-01 12:18:07 +08002
3#include <assert.h>
4#include <fcntl.h>
5#include <stdbool.h>
6#include <stdlib.h>
7#include <string.h>
8#include <unistd.h>
9
10#include <sys/ioctl.h>
11#include <sys/mman.h>
12
13#include <linux/aspeed-lpc-ctrl.h>
14
15#define pr_fmt(x) "astlpc: " x
16
17#include "libmctp.h"
18#include "libmctp-alloc.h"
19#include "libmctp-log.h"
20#include "libmctp-astlpc.h"
21
22struct mctp_binding_astlpc {
23 struct mctp_binding binding;
24 void *lpc_map_base;
25 union {
26 void *lpc_map;
27 struct mctp_lpcmap_hdr *lpc_hdr;
28 };
29 int kcs_fd;
30 uint8_t kcs_status;
31
32 bool running;
33
34 /* temporary transmit buffer */
35 uint8_t txbuf[256];
36};
37
38#ifndef container_of
39#define container_of(ptr, type, member) \
40 (type *)((char *)(ptr) - (char *)&((type *)0)->member)
41#endif
42
43#define binding_to_astlpc(b) \
44 container_of(b, struct mctp_binding_astlpc, binding)
45
46#define MCTP_MAGIC 0x4d435450
47#define BMC_VER_MIN 1
48#define BMC_VER_CUR 1
49
50struct mctp_lpcmap_hdr {
51 uint32_t magic;
52
53 uint16_t bmc_ver_min;
54 uint16_t bmc_ver_cur;
55 uint16_t host_ver_min;
56 uint16_t host_ver_cur;
57 uint16_t negotiated_ver;
58 uint16_t pad0;
59
60 uint32_t rx_offset;
61 uint32_t rx_size;
62 uint32_t tx_offset;
63 uint32_t tx_size;
64} __attribute__((packed));
65
66/* layout of TX/RX areas */
67static const uint32_t rx_offset = 0x100;
68static const uint32_t rx_size = 0x100;
69static const uint32_t tx_offset = 0x200;
70static const uint32_t tx_size = 0x100;
71
72/* kernel interface */
73static const char *kcs_path = "/dev/mctp0";
74static const char *lpc_path = "/dev/aspeed-lpc-ctrl";
75
76#define LPC_WIN_SIZE (1 * 1024 * 1024)
77
78enum {
79 KCS_REG_DATA = 0,
80 KCS_REG_STATUS = 1,
81};
82
83#define KCS_STATUS_BMC_READY 0x80
84#define KCS_STATUS_CHANNEL_ACTIVE 0x40
85#define KCS_STATUS_IBF 0x02
86#define KCS_STATUS_OBF 0x01
87
88static int mctp_astlpc_kcs_set_status(struct mctp_binding_astlpc *astlpc,
89 uint8_t status)
90{
Jeremy Kerr1a4b55a2019-06-24 14:22:39 +080091 uint8_t buf[2];
Jeremy Kerr672c8852019-03-01 12:18:07 +080092 int rc;
93
Jeremy Kerr1a4b55a2019-06-24 14:22:39 +080094 /* Since we're setting the status register, we want the other endpoint
95 * to be interrupted. However, some hardware may only raise a host-side
96 * interrupt on an ODR event.
97 * So, write a dummy value of 0xff to ODR, which will ensure that an
98 * interrupt is triggered, and can be ignored by the host.
99 */
100 buf[KCS_REG_DATA] = 0xff;
101 buf[KCS_REG_STATUS] = status;
102
103 rc = pwrite(astlpc->kcs_fd, buf, 2, 0);
Jeremy Kerr672c8852019-03-01 12:18:07 +0800104 if (rc != 1) {
105 mctp_prwarn("KCS status write failed");
106 return -1;
107 }
108 return 0;
109}
110
111static int mctp_astlpc_kcs_send(struct mctp_binding_astlpc *astlpc,
112 uint8_t data)
113{
114 uint8_t status;
115 int rc;
116
117 for (;;) {
118 rc = pread(astlpc->kcs_fd, &status, 1, KCS_REG_STATUS);
119 if (rc != 1) {
120 mctp_prwarn("KCE status read failed");
121 return -1;
122 }
123 if (!(status & KCS_STATUS_OBF))
124 break;
125 /* todo: timeout */
126 }
127
128 rc = pwrite(astlpc->kcs_fd, &data, 1, KCS_REG_DATA);
129 if (rc != 1) {
130 mctp_prwarn("KCS data write failed");
131 return -1;
132 }
133
134 return 0;
135}
136
137static int mctp_binding_astlpc_tx(struct mctp_binding *b,
138 struct mctp_pktbuf *pkt)
139{
140 struct mctp_binding_astlpc *astlpc = binding_to_astlpc(b);
141 uint32_t len;
142
143 len = mctp_pktbuf_size(pkt);
144 if (len > rx_size - 4) {
145 mctp_prwarn("invalid TX len 0x%x", len);
146 return -1;
147 }
148
149 *(uint32_t *)(astlpc->lpc_map + rx_offset) = htobe32(len);
150
151 memcpy(astlpc->lpc_map + rx_offset + 4, mctp_pktbuf_hdr(pkt), len);
152
153 mctp_binding_set_tx_enabled(b, false);
154
155 mctp_astlpc_kcs_send(astlpc, 0x1);
156 return 0;
157}
158
159static void mctp_astlpc_init_channel(struct mctp_binding_astlpc *astlpc)
160{
161 /* todo: actual version negotiation */
162 astlpc->lpc_hdr->negotiated_ver = htobe16(1);
163 mctp_astlpc_kcs_set_status(astlpc,
164 KCS_STATUS_BMC_READY | KCS_STATUS_CHANNEL_ACTIVE |
165 KCS_STATUS_OBF);
166
167 mctp_binding_set_tx_enabled(&astlpc->binding, true);
168}
169
170static void mctp_astlpc_rx_start(struct mctp_binding_astlpc *astlpc)
171{
172 struct mctp_pktbuf *pkt;
173 uint32_t len;
174
175 len = htobe32(*(uint32_t *)(astlpc->lpc_map + tx_offset));
176 if (len > tx_size - 4) {
177 mctp_prwarn("invalid RX len 0x%x", len);
178 return;
179 }
180
181 if (len > MCTP_MTU + sizeof(struct mctp_hdr)) {
182 mctp_prwarn("invalid RX len 0x%x", len);
183 return;
184 }
185
186 pkt = mctp_pktbuf_alloc(len);
187 if (!pkt)
188 goto out_complete;
189
190 memcpy(mctp_pktbuf_hdr(pkt), astlpc->lpc_map + tx_offset + 4, len);
191
192 mctp_bus_rx(&astlpc->binding, pkt);
193
194out_complete:
195 mctp_astlpc_kcs_send(astlpc, 0x2);
196}
197
198static void mctp_astlpc_tx_complete(struct mctp_binding_astlpc *astlpc)
199{
200 mctp_binding_set_tx_enabled(&astlpc->binding, true);
201}
202
203int mctp_astlpc_poll(struct mctp_binding_astlpc *astlpc)
204{
205 uint8_t kcs_regs[2], data;
206 int rc;
207
208 rc = pread(astlpc->kcs_fd, kcs_regs, 2, 0);
209 if (rc < 0) {
210 mctp_prwarn("KCS read error");
211 return -1;
212 } else if (rc != 2) {
213 mctp_prwarn("KCS short read (%d)", rc);
214 return -1;
215 }
216
217 if (!(kcs_regs[KCS_REG_STATUS] & KCS_STATUS_IBF))
218 return 0;
219
220 data = kcs_regs[KCS_REG_DATA];
221 switch (data) {
222 case 0x0:
223 mctp_astlpc_init_channel(astlpc);
224 break;
225 case 0x1:
226 mctp_astlpc_rx_start(astlpc);
227 break;
228 case 0x2:
229 mctp_astlpc_tx_complete(astlpc);
230 break;
Jeremy Kerr1a4b55a2019-06-24 14:22:39 +0800231 case 0xff:
232 /* reserved value for dummy data writes; do nothing */
233 break;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800234 default:
235 mctp_prwarn("unknown message 0x%x", data);
236 }
237 return 0;
238}
239
240int mctp_astlpc_get_fd(struct mctp_binding_astlpc *astlpc)
241{
242 return astlpc->kcs_fd;
243}
244
245
246void mctp_astlpc_register_bus(struct mctp_binding_astlpc *astlpc,
247 struct mctp *mctp, mctp_eid_t eid)
248{
249 mctp_register_bus(mctp, &astlpc->binding, eid);
250}
251
252static int mctp_astlpc_init_bmc(struct mctp_binding_astlpc *astlpc)
253{
254 uint8_t status;
255 int rc;
256
257 astlpc->lpc_hdr->magic = htobe32(MCTP_MAGIC);
258 astlpc->lpc_hdr->bmc_ver_min = htobe16(BMC_VER_MIN);
259 astlpc->lpc_hdr->bmc_ver_cur = htobe16(BMC_VER_CUR);
260
261 astlpc->lpc_hdr->rx_offset = htobe32(rx_offset);
262 astlpc->lpc_hdr->rx_size = htobe32(rx_size);
263 astlpc->lpc_hdr->tx_offset = htobe32(tx_offset);
264 astlpc->lpc_hdr->tx_size = htobe32(tx_size);
265
266 /* set status indicating that the BMC is now active */
267 status = KCS_STATUS_BMC_READY | KCS_STATUS_OBF;
268 rc = pwrite(astlpc->kcs_fd, &status, 1, KCS_REG_STATUS);
269 if (rc != 1) {
270 mctp_prwarn("KCS write failed");
271 rc = -1;
272 } else {
273 rc = 0;
274 }
275
276 return rc;
277}
278
279static int mctp_astlpc_init_lpc(struct mctp_binding_astlpc *astlpc)
280{
281 struct aspeed_lpc_ctrl_mapping map = {
282 .window_type = ASPEED_LPC_CTRL_WINDOW_MEMORY,
283 .window_id = 0, /* There's only one */
284 .flags = 0,
285 .addr = 0,
286 .offset = 0,
287 .size = 0
288 };
289 int fd, rc;
290
291 fd = open(lpc_path, O_RDWR | O_SYNC);
292 if (fd < 0) {
293 mctp_prwarn("LPC open (%s) failed", lpc_path);
294 return -1;
295 }
296
297 rc = ioctl(fd, ASPEED_LPC_CTRL_IOCTL_GET_SIZE, &map);
298 if (rc) {
299 mctp_prwarn("LPC GET_SIZE failed");
300 close(fd);
301 return -1;
302 }
303
304 astlpc->lpc_map_base = mmap(NULL, map.size, PROT_READ | PROT_WRITE,
305 MAP_SHARED, fd, 0);
306 if (astlpc->lpc_map_base == MAP_FAILED) {
307 mctp_prwarn("LPC mmap failed");
308 rc = -1;
309 } else {
310 astlpc->lpc_map = astlpc->lpc_map_base +
311 map.size - LPC_WIN_SIZE;
312 }
313
314 close(fd);
315
316 return rc;
317}
318
319static int mctp_astlpc_init_kcs(struct mctp_binding_astlpc *astlpc)
320{
321 astlpc->kcs_fd = open(kcs_path, O_RDWR);
322 if (astlpc->kcs_fd < 0)
323 return -1;
324
325 return 0;
326}
327
328struct mctp_binding_astlpc *mctp_astlpc_init(void)
329{
330 struct mctp_binding_astlpc *astlpc;
331 int rc;
332
333 astlpc = __mctp_alloc(sizeof(*astlpc));
334 memset(astlpc, 0, sizeof(*astlpc));
335 astlpc->binding.name = "astlpc";
336 astlpc->binding.version = 1;
337 astlpc->binding.tx = mctp_binding_astlpc_tx;
338
339 rc = mctp_astlpc_init_lpc(astlpc);
340 if (rc) {
341 free(astlpc);
342 return NULL;
343 }
344
345 rc = mctp_astlpc_init_kcs(astlpc);
346 if (rc) {
347 free(astlpc);
348 return NULL;
349 }
350
351 rc = mctp_astlpc_init_bmc(astlpc);
352 if (rc) {
353 free(astlpc);
354 return NULL;
355 }
356
357 return astlpc;
358}
359