blob: cf755344013a962f5add94ff58127a01e1c8a1a6 [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>
Jeremy Kerr92a10a62019-08-28 16:55:54 +05304#include <endian.h>
Jeremy Kerr672c8852019-03-01 12:18:07 +08005#include <stdbool.h>
6#include <stdlib.h>
7#include <string.h>
8#include <unistd.h>
9
Jeremy Kerr672c8852019-03-01 12:18:07 +080010#define pr_fmt(x) "astlpc: " x
11
12#include "libmctp.h"
13#include "libmctp-alloc.h"
14#include "libmctp-log.h"
15#include "libmctp-astlpc.h"
16
Jeremy Kerrb214c642019-11-27 11:34:00 +080017#ifdef MCTP_HAVE_FILEIO
Jeremy Kerr92a10a62019-08-28 16:55:54 +053018
19#include <fcntl.h>
20#include <sys/ioctl.h>
21#include <sys/mman.h>
22#include <linux/aspeed-lpc-ctrl.h>
23
24/* kernel interface */
25static const char *kcs_path = "/dev/mctp0";
26static const char *lpc_path = "/dev/aspeed-lpc-ctrl";
27
28#endif
29
Jeremy Kerr672c8852019-03-01 12:18:07 +080030struct mctp_binding_astlpc {
31 struct mctp_binding binding;
Jeremy Kerrbc53d352019-08-28 14:26:14 +053032
Jeremy Kerr672c8852019-03-01 12:18:07 +080033 union {
34 void *lpc_map;
35 struct mctp_lpcmap_hdr *lpc_hdr;
36 };
Jeremy Kerrbc53d352019-08-28 14:26:14 +053037
38 /* direct ops data */
39 struct mctp_binding_astlpc_ops ops;
40 void *ops_data;
41 struct mctp_lpcmap_hdr *priv_hdr;
42
43 /* fileio ops data */
44 void *lpc_map_base;
Jeremy Kerr672c8852019-03-01 12:18:07 +080045 int kcs_fd;
46 uint8_t kcs_status;
47
48 bool running;
49
50 /* temporary transmit buffer */
51 uint8_t txbuf[256];
52};
53
54#ifndef container_of
55#define container_of(ptr, type, member) \
56 (type *)((char *)(ptr) - (char *)&((type *)0)->member)
57#endif
58
59#define binding_to_astlpc(b) \
60 container_of(b, struct mctp_binding_astlpc, binding)
61
62#define MCTP_MAGIC 0x4d435450
63#define BMC_VER_MIN 1
64#define BMC_VER_CUR 1
65
66struct mctp_lpcmap_hdr {
67 uint32_t magic;
68
69 uint16_t bmc_ver_min;
70 uint16_t bmc_ver_cur;
71 uint16_t host_ver_min;
72 uint16_t host_ver_cur;
73 uint16_t negotiated_ver;
74 uint16_t pad0;
75
76 uint32_t rx_offset;
77 uint32_t rx_size;
78 uint32_t tx_offset;
79 uint32_t tx_size;
80} __attribute__((packed));
81
82/* layout of TX/RX areas */
83static const uint32_t rx_offset = 0x100;
84static const uint32_t rx_size = 0x100;
85static const uint32_t tx_offset = 0x200;
86static const uint32_t tx_size = 0x100;
87
Jeremy Kerr672c8852019-03-01 12:18:07 +080088#define LPC_WIN_SIZE (1 * 1024 * 1024)
89
90enum {
91 KCS_REG_DATA = 0,
92 KCS_REG_STATUS = 1,
93};
94
95#define KCS_STATUS_BMC_READY 0x80
96#define KCS_STATUS_CHANNEL_ACTIVE 0x40
97#define KCS_STATUS_IBF 0x02
98#define KCS_STATUS_OBF 0x01
99
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530100static bool lpc_direct(struct mctp_binding_astlpc *astlpc)
101{
102 return astlpc->lpc_map != NULL;
103}
104
Jeremy Kerr672c8852019-03-01 12:18:07 +0800105static int mctp_astlpc_kcs_set_status(struct mctp_binding_astlpc *astlpc,
106 uint8_t status)
107{
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530108 uint8_t data;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800109 int rc;
110
Jeremy Kerr1a4b55a2019-06-24 14:22:39 +0800111 /* Since we're setting the status register, we want the other endpoint
112 * to be interrupted. However, some hardware may only raise a host-side
113 * interrupt on an ODR event.
114 * So, write a dummy value of 0xff to ODR, which will ensure that an
115 * interrupt is triggered, and can be ignored by the host.
116 */
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530117 data = 0xff;
118 status |= KCS_STATUS_OBF;
Jeremy Kerr1a4b55a2019-06-24 14:22:39 +0800119
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530120 rc = astlpc->ops.kcs_write(astlpc->ops_data, MCTP_ASTLPC_KCS_REG_DATA,
121 data);
122 if (rc) {
123 mctp_prwarn("KCS dummy data write failed");
124 return -1;
125 }
126
127
128 rc = astlpc->ops.kcs_write(astlpc->ops_data, MCTP_ASTLPC_KCS_REG_STATUS,
129 status);
130 if (rc) {
Jeremy Kerr672c8852019-03-01 12:18:07 +0800131 mctp_prwarn("KCS status write failed");
132 return -1;
133 }
134 return 0;
135}
136
137static int mctp_astlpc_kcs_send(struct mctp_binding_astlpc *astlpc,
138 uint8_t data)
139{
140 uint8_t status;
141 int rc;
142
143 for (;;) {
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530144 rc = astlpc->ops.kcs_read(astlpc->ops_data,
145 MCTP_ASTLPC_KCS_REG_STATUS, &status);
Jeremy Kerr672c8852019-03-01 12:18:07 +0800146 if (rc != 1) {
147 mctp_prwarn("KCE status read failed");
148 return -1;
149 }
150 if (!(status & KCS_STATUS_OBF))
151 break;
152 /* todo: timeout */
153 }
154
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530155 rc = astlpc->ops.kcs_write(astlpc->ops_data, MCTP_ASTLPC_KCS_REG_DATA,
156 data);
157 if (rc) {
Jeremy Kerr672c8852019-03-01 12:18:07 +0800158 mctp_prwarn("KCS data write failed");
159 return -1;
160 }
161
162 return 0;
163}
164
165static int mctp_binding_astlpc_tx(struct mctp_binding *b,
166 struct mctp_pktbuf *pkt)
167{
168 struct mctp_binding_astlpc *astlpc = binding_to_astlpc(b);
169 uint32_t len;
170
171 len = mctp_pktbuf_size(pkt);
172 if (len > rx_size - 4) {
173 mctp_prwarn("invalid TX len 0x%x", len);
174 return -1;
175 }
176
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530177 if (lpc_direct(astlpc)) {
178 *(uint32_t *)(astlpc->lpc_map + rx_offset) = htobe32(len);
179 memcpy(astlpc->lpc_map + rx_offset + 4, mctp_pktbuf_hdr(pkt),
180 len);
181 } else {
182 uint32_t tmp = htobe32(len);
183 astlpc->ops.lpc_write(astlpc->ops_data, &tmp, rx_offset,
184 sizeof(tmp));
185 astlpc->ops.lpc_write(astlpc->ops_data, mctp_pktbuf_hdr(pkt),
186 rx_offset + 4, len);
187 }
Jeremy Kerr672c8852019-03-01 12:18:07 +0800188
189 mctp_binding_set_tx_enabled(b, false);
190
191 mctp_astlpc_kcs_send(astlpc, 0x1);
192 return 0;
193}
194
195static void mctp_astlpc_init_channel(struct mctp_binding_astlpc *astlpc)
196{
197 /* todo: actual version negotiation */
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530198 if (lpc_direct(astlpc)) {
199 astlpc->lpc_hdr->negotiated_ver = htobe16(1);
200 } else {
201 uint16_t ver = htobe16(1);
202 astlpc->ops.lpc_write(astlpc->ops_data, &ver,
203 offsetof(struct mctp_lpcmap_hdr,
204 negotiated_ver),
205 sizeof(ver));
206 }
Jeremy Kerr672c8852019-03-01 12:18:07 +0800207 mctp_astlpc_kcs_set_status(astlpc,
208 KCS_STATUS_BMC_READY | KCS_STATUS_CHANNEL_ACTIVE |
209 KCS_STATUS_OBF);
210
211 mctp_binding_set_tx_enabled(&astlpc->binding, true);
212}
213
214static void mctp_astlpc_rx_start(struct mctp_binding_astlpc *astlpc)
215{
216 struct mctp_pktbuf *pkt;
217 uint32_t len;
218
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530219 if (lpc_direct(astlpc)) {
220 len = *(uint32_t *)(astlpc->lpc_map + tx_offset);
221 } else {
222 astlpc->ops.lpc_read(astlpc->ops_data, &len,
223 tx_offset, sizeof(len));
224 }
225 len = be32toh(len);
226
Jeremy Kerr672c8852019-03-01 12:18:07 +0800227 if (len > tx_size - 4) {
228 mctp_prwarn("invalid RX len 0x%x", len);
229 return;
230 }
231
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +0800232 if (len > astlpc->binding.pkt_size) {
Jeremy Kerr672c8852019-03-01 12:18:07 +0800233 mctp_prwarn("invalid RX len 0x%x", len);
234 return;
235 }
236
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +0800237 pkt = mctp_pktbuf_alloc(&astlpc->binding, len);
Jeremy Kerr672c8852019-03-01 12:18:07 +0800238 if (!pkt)
239 goto out_complete;
240
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530241 if (lpc_direct(astlpc)) {
242 memcpy(mctp_pktbuf_hdr(pkt),
243 astlpc->lpc_map + tx_offset + 4, len);
244 } else {
245 astlpc->ops.lpc_read(astlpc->ops_data, mctp_pktbuf_hdr(pkt),
246 tx_offset + 4, len);
247 }
Jeremy Kerr672c8852019-03-01 12:18:07 +0800248
249 mctp_bus_rx(&astlpc->binding, pkt);
250
251out_complete:
252 mctp_astlpc_kcs_send(astlpc, 0x2);
253}
254
255static void mctp_astlpc_tx_complete(struct mctp_binding_astlpc *astlpc)
256{
257 mctp_binding_set_tx_enabled(&astlpc->binding, true);
258}
259
260int mctp_astlpc_poll(struct mctp_binding_astlpc *astlpc)
261{
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530262 uint8_t status, data;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800263 int rc;
264
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530265 rc = astlpc->ops.kcs_read(astlpc->ops_data, MCTP_ASTLPC_KCS_REG_STATUS,
266 &status);
267 if (rc) {
Jeremy Kerr672c8852019-03-01 12:18:07 +0800268 mctp_prwarn("KCS read error");
269 return -1;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800270 }
271
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530272 if (!(status & KCS_STATUS_IBF))
Jeremy Kerr672c8852019-03-01 12:18:07 +0800273 return 0;
274
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530275 rc = astlpc->ops.kcs_read(astlpc->ops_data, MCTP_ASTLPC_KCS_REG_DATA,
276 &data);
277 if (rc) {
278 mctp_prwarn("KCS data read error");
279 return -1;
280 }
281
Jeremy Kerr672c8852019-03-01 12:18:07 +0800282 switch (data) {
283 case 0x0:
284 mctp_astlpc_init_channel(astlpc);
285 break;
286 case 0x1:
287 mctp_astlpc_rx_start(astlpc);
288 break;
289 case 0x2:
290 mctp_astlpc_tx_complete(astlpc);
291 break;
Jeremy Kerr1a4b55a2019-06-24 14:22:39 +0800292 case 0xff:
293 /* reserved value for dummy data writes; do nothing */
294 break;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800295 default:
296 mctp_prwarn("unknown message 0x%x", data);
297 }
298 return 0;
299}
300
Jeremy Kerr672c8852019-03-01 12:18:07 +0800301static int mctp_astlpc_init_bmc(struct mctp_binding_astlpc *astlpc)
302{
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530303 struct mctp_lpcmap_hdr *hdr;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800304 uint8_t status;
305 int rc;
306
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530307 if (lpc_direct(astlpc))
308 hdr = astlpc->lpc_hdr;
309 else
310 hdr = astlpc->priv_hdr;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800311
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530312 hdr->magic = htobe32(MCTP_MAGIC);
313 hdr->bmc_ver_min = htobe16(BMC_VER_MIN);
314 hdr->bmc_ver_cur = htobe16(BMC_VER_CUR);
315
316 hdr->rx_offset = htobe32(rx_offset);
317 hdr->rx_size = htobe32(rx_size);
318 hdr->tx_offset = htobe32(tx_offset);
319 hdr->tx_size = htobe32(tx_size);
320
321 if (!lpc_direct(astlpc))
322 astlpc->ops.lpc_write(astlpc->ops_data, hdr, 0, sizeof(*hdr));
Jeremy Kerr672c8852019-03-01 12:18:07 +0800323
324 /* set status indicating that the BMC is now active */
325 status = KCS_STATUS_BMC_READY | KCS_STATUS_OBF;
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530326 rc = astlpc->ops.kcs_write(astlpc->ops_data,
327 MCTP_ASTLPC_KCS_REG_STATUS, status);
328 if (rc) {
Jeremy Kerr672c8852019-03-01 12:18:07 +0800329 mctp_prwarn("KCS write failed");
Jeremy Kerr672c8852019-03-01 12:18:07 +0800330 }
331
332 return rc;
333}
334
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530335/* allocate and basic initialisation */
336static struct mctp_binding_astlpc *__mctp_astlpc_init(void)
337{
338 struct mctp_binding_astlpc *astlpc;
339
340 astlpc = __mctp_alloc(sizeof(*astlpc));
341 memset(astlpc, 0, sizeof(*astlpc));
342 astlpc->binding.name = "astlpc";
343 astlpc->binding.version = 1;
344 astlpc->binding.tx = mctp_binding_astlpc_tx;
345 astlpc->binding.pkt_size = MCTP_BMTU;
346 astlpc->binding.pkt_pad = 0;
347 astlpc->lpc_map = NULL;
348
349 return astlpc;
350}
351
Jeremy Kerr3b36d172019-09-04 11:56:09 +0800352struct mctp_binding *mctp_binding_astlpc_core(struct mctp_binding_astlpc *b)
353{
354 return &b->binding;
355}
356
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530357struct mctp_binding_astlpc *mctp_astlpc_init_ops(
358 struct mctp_binding_astlpc_ops *ops,
359 void *ops_data, void *lpc_map)
360{
361 struct mctp_binding_astlpc *astlpc;
362 int rc;
363
364 astlpc = __mctp_astlpc_init();
365 if (!astlpc)
366 return NULL;
367
368 memcpy(&astlpc->ops, ops, sizeof(astlpc->ops));
369 astlpc->ops_data = ops_data;
370 astlpc->lpc_map = lpc_map;
371
372 /* In indirect mode, we keep a separate buffer of header data.
373 * We need to sync this through the lpc_read/lpc_write ops.
374 */
375 if (!astlpc->lpc_map)
376 astlpc->priv_hdr = __mctp_alloc(sizeof(*astlpc->priv_hdr));
377
378 rc = mctp_astlpc_init_bmc(astlpc);
379 if (rc) {
380 free(astlpc);
381 return NULL;
382 }
383
384 return astlpc;
385}
386
Jeremy Kerrb214c642019-11-27 11:34:00 +0800387#ifdef MCTP_HAVE_FILEIO
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530388static int mctp_astlpc_init_fileio_lpc(struct mctp_binding_astlpc *astlpc)
Jeremy Kerr672c8852019-03-01 12:18:07 +0800389{
390 struct aspeed_lpc_ctrl_mapping map = {
391 .window_type = ASPEED_LPC_CTRL_WINDOW_MEMORY,
392 .window_id = 0, /* There's only one */
393 .flags = 0,
394 .addr = 0,
395 .offset = 0,
396 .size = 0
397 };
398 int fd, rc;
399
400 fd = open(lpc_path, O_RDWR | O_SYNC);
401 if (fd < 0) {
402 mctp_prwarn("LPC open (%s) failed", lpc_path);
403 return -1;
404 }
405
406 rc = ioctl(fd, ASPEED_LPC_CTRL_IOCTL_GET_SIZE, &map);
407 if (rc) {
408 mctp_prwarn("LPC GET_SIZE failed");
409 close(fd);
410 return -1;
411 }
412
413 astlpc->lpc_map_base = mmap(NULL, map.size, PROT_READ | PROT_WRITE,
414 MAP_SHARED, fd, 0);
415 if (astlpc->lpc_map_base == MAP_FAILED) {
416 mctp_prwarn("LPC mmap failed");
417 rc = -1;
418 } else {
419 astlpc->lpc_map = astlpc->lpc_map_base +
420 map.size - LPC_WIN_SIZE;
421 }
422
423 close(fd);
424
425 return rc;
426}
427
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530428static int mctp_astlpc_init_fileio_kcs(struct mctp_binding_astlpc *astlpc)
Jeremy Kerr672c8852019-03-01 12:18:07 +0800429{
430 astlpc->kcs_fd = open(kcs_path, O_RDWR);
431 if (astlpc->kcs_fd < 0)
432 return -1;
433
434 return 0;
435}
436
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530437static int __mctp_astlpc_fileio_kcs_read(void *arg,
438 enum mctp_binding_astlpc_kcs_reg reg, uint8_t *val)
439{
440 struct mctp_binding_astlpc *astlpc = arg;
441 off_t offset = reg;
442 int rc;
443
444 rc = pread(astlpc->kcs_fd, val, 1, offset);
445
446 return rc == 1 ? 0 : -1;
447}
448
449static int __mctp_astlpc_fileio_kcs_write(void *arg,
450 enum mctp_binding_astlpc_kcs_reg reg, uint8_t val)
451{
452 struct mctp_binding_astlpc *astlpc = arg;
453 off_t offset = reg;
454 int rc;
455
456 rc = pwrite(astlpc->kcs_fd, &val, 1, offset);
457
458 return rc == 1 ? 0 : -1;
459}
460
461int mctp_astlpc_get_fd(struct mctp_binding_astlpc *astlpc)
462{
463 return astlpc->kcs_fd;
464}
465
466struct mctp_binding_astlpc *mctp_astlpc_init_fileio(void)
Jeremy Kerr672c8852019-03-01 12:18:07 +0800467{
468 struct mctp_binding_astlpc *astlpc;
469 int rc;
470
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530471 astlpc = __mctp_astlpc_init();
472 if (!astlpc)
473 return NULL;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800474
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530475 /* Set internal operations for kcs. We use direct accesses to the lpc
476 * map area */
477 astlpc->ops.kcs_read = __mctp_astlpc_fileio_kcs_read;
478 astlpc->ops.kcs_write = __mctp_astlpc_fileio_kcs_write;
479 astlpc->ops_data = astlpc;
480
481 rc = mctp_astlpc_init_fileio_lpc(astlpc);
Jeremy Kerr672c8852019-03-01 12:18:07 +0800482 if (rc) {
483 free(astlpc);
484 return NULL;
485 }
486
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530487 rc = mctp_astlpc_init_fileio_kcs(astlpc);
Jeremy Kerr672c8852019-03-01 12:18:07 +0800488 if (rc) {
489 free(astlpc);
490 return NULL;
491 }
492
493 rc = mctp_astlpc_init_bmc(astlpc);
494 if (rc) {
495 free(astlpc);
496 return NULL;
497 }
498
499 return astlpc;
500}
Jeremy Kerr92a10a62019-08-28 16:55:54 +0530501#else
502struct mctp_binding_astlpc * __attribute__((const))
503 mctp_astlpc_init_fileio(void)
504{
505 return NULL;
506}
Jeremy Kerr672c8852019-03-01 12:18:07 +0800507
Jeremy Kerr92a10a62019-08-28 16:55:54 +0530508int __attribute__((const)) mctp_astlpc_get_fd(
509 struct mctp_binding_astlpc *astlpc __attribute__((unused)))
510{
511 return -1;
512}
513#endif