blob: 45cab737333bf9724f69546172caa1d086f73544 [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 Kerr92a10a62019-08-28 16:55:54 +053017#ifndef MCTP_WITHOUT_FILEIO
18
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 +0800301void mctp_astlpc_register_bus(struct mctp_binding_astlpc *astlpc,
302 struct mctp *mctp, mctp_eid_t eid)
303{
304 mctp_register_bus(mctp, &astlpc->binding, eid);
305}
306
307static int mctp_astlpc_init_bmc(struct mctp_binding_astlpc *astlpc)
308{
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530309 struct mctp_lpcmap_hdr *hdr;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800310 uint8_t status;
311 int rc;
312
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530313 if (lpc_direct(astlpc))
314 hdr = astlpc->lpc_hdr;
315 else
316 hdr = astlpc->priv_hdr;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800317
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530318 hdr->magic = htobe32(MCTP_MAGIC);
319 hdr->bmc_ver_min = htobe16(BMC_VER_MIN);
320 hdr->bmc_ver_cur = htobe16(BMC_VER_CUR);
321
322 hdr->rx_offset = htobe32(rx_offset);
323 hdr->rx_size = htobe32(rx_size);
324 hdr->tx_offset = htobe32(tx_offset);
325 hdr->tx_size = htobe32(tx_size);
326
327 if (!lpc_direct(astlpc))
328 astlpc->ops.lpc_write(astlpc->ops_data, hdr, 0, sizeof(*hdr));
Jeremy Kerr672c8852019-03-01 12:18:07 +0800329
330 /* set status indicating that the BMC is now active */
331 status = KCS_STATUS_BMC_READY | KCS_STATUS_OBF;
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530332 rc = astlpc->ops.kcs_write(astlpc->ops_data,
333 MCTP_ASTLPC_KCS_REG_STATUS, status);
334 if (rc) {
Jeremy Kerr672c8852019-03-01 12:18:07 +0800335 mctp_prwarn("KCS write failed");
Jeremy Kerr672c8852019-03-01 12:18:07 +0800336 }
337
338 return rc;
339}
340
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530341/* allocate and basic initialisation */
342static struct mctp_binding_astlpc *__mctp_astlpc_init(void)
343{
344 struct mctp_binding_astlpc *astlpc;
345
346 astlpc = __mctp_alloc(sizeof(*astlpc));
347 memset(astlpc, 0, sizeof(*astlpc));
348 astlpc->binding.name = "astlpc";
349 astlpc->binding.version = 1;
350 astlpc->binding.tx = mctp_binding_astlpc_tx;
351 astlpc->binding.pkt_size = MCTP_BMTU;
352 astlpc->binding.pkt_pad = 0;
353 astlpc->lpc_map = NULL;
354
355 return astlpc;
356}
357
358struct mctp_binding_astlpc *mctp_astlpc_init_ops(
359 struct mctp_binding_astlpc_ops *ops,
360 void *ops_data, void *lpc_map)
361{
362 struct mctp_binding_astlpc *astlpc;
363 int rc;
364
365 astlpc = __mctp_astlpc_init();
366 if (!astlpc)
367 return NULL;
368
369 memcpy(&astlpc->ops, ops, sizeof(astlpc->ops));
370 astlpc->ops_data = ops_data;
371 astlpc->lpc_map = lpc_map;
372
373 /* In indirect mode, we keep a separate buffer of header data.
374 * We need to sync this through the lpc_read/lpc_write ops.
375 */
376 if (!astlpc->lpc_map)
377 astlpc->priv_hdr = __mctp_alloc(sizeof(*astlpc->priv_hdr));
378
379 rc = mctp_astlpc_init_bmc(astlpc);
380 if (rc) {
381 free(astlpc);
382 return NULL;
383 }
384
385 return astlpc;
386}
387
Jeremy Kerr92a10a62019-08-28 16:55:54 +0530388#ifndef MCTP_WITHOUT_FILEIO
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530389static int mctp_astlpc_init_fileio_lpc(struct mctp_binding_astlpc *astlpc)
Jeremy Kerr672c8852019-03-01 12:18:07 +0800390{
391 struct aspeed_lpc_ctrl_mapping map = {
392 .window_type = ASPEED_LPC_CTRL_WINDOW_MEMORY,
393 .window_id = 0, /* There's only one */
394 .flags = 0,
395 .addr = 0,
396 .offset = 0,
397 .size = 0
398 };
399 int fd, rc;
400
401 fd = open(lpc_path, O_RDWR | O_SYNC);
402 if (fd < 0) {
403 mctp_prwarn("LPC open (%s) failed", lpc_path);
404 return -1;
405 }
406
407 rc = ioctl(fd, ASPEED_LPC_CTRL_IOCTL_GET_SIZE, &map);
408 if (rc) {
409 mctp_prwarn("LPC GET_SIZE failed");
410 close(fd);
411 return -1;
412 }
413
414 astlpc->lpc_map_base = mmap(NULL, map.size, PROT_READ | PROT_WRITE,
415 MAP_SHARED, fd, 0);
416 if (astlpc->lpc_map_base == MAP_FAILED) {
417 mctp_prwarn("LPC mmap failed");
418 rc = -1;
419 } else {
420 astlpc->lpc_map = astlpc->lpc_map_base +
421 map.size - LPC_WIN_SIZE;
422 }
423
424 close(fd);
425
426 return rc;
427}
428
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530429static int mctp_astlpc_init_fileio_kcs(struct mctp_binding_astlpc *astlpc)
Jeremy Kerr672c8852019-03-01 12:18:07 +0800430{
431 astlpc->kcs_fd = open(kcs_path, O_RDWR);
432 if (astlpc->kcs_fd < 0)
433 return -1;
434
435 return 0;
436}
437
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530438static int __mctp_astlpc_fileio_kcs_read(void *arg,
439 enum mctp_binding_astlpc_kcs_reg reg, uint8_t *val)
440{
441 struct mctp_binding_astlpc *astlpc = arg;
442 off_t offset = reg;
443 int rc;
444
445 rc = pread(astlpc->kcs_fd, val, 1, offset);
446
447 return rc == 1 ? 0 : -1;
448}
449
450static int __mctp_astlpc_fileio_kcs_write(void *arg,
451 enum mctp_binding_astlpc_kcs_reg reg, uint8_t val)
452{
453 struct mctp_binding_astlpc *astlpc = arg;
454 off_t offset = reg;
455 int rc;
456
457 rc = pwrite(astlpc->kcs_fd, &val, 1, offset);
458
459 return rc == 1 ? 0 : -1;
460}
461
462int mctp_astlpc_get_fd(struct mctp_binding_astlpc *astlpc)
463{
464 return astlpc->kcs_fd;
465}
466
467struct mctp_binding_astlpc *mctp_astlpc_init_fileio(void)
Jeremy Kerr672c8852019-03-01 12:18:07 +0800468{
469 struct mctp_binding_astlpc *astlpc;
470 int rc;
471
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530472 astlpc = __mctp_astlpc_init();
473 if (!astlpc)
474 return NULL;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800475
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530476 /* Set internal operations for kcs. We use direct accesses to the lpc
477 * map area */
478 astlpc->ops.kcs_read = __mctp_astlpc_fileio_kcs_read;
479 astlpc->ops.kcs_write = __mctp_astlpc_fileio_kcs_write;
480 astlpc->ops_data = astlpc;
481
482 rc = mctp_astlpc_init_fileio_lpc(astlpc);
Jeremy Kerr672c8852019-03-01 12:18:07 +0800483 if (rc) {
484 free(astlpc);
485 return NULL;
486 }
487
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530488 rc = mctp_astlpc_init_fileio_kcs(astlpc);
Jeremy Kerr672c8852019-03-01 12:18:07 +0800489 if (rc) {
490 free(astlpc);
491 return NULL;
492 }
493
494 rc = mctp_astlpc_init_bmc(astlpc);
495 if (rc) {
496 free(astlpc);
497 return NULL;
498 }
499
500 return astlpc;
501}
Jeremy Kerr92a10a62019-08-28 16:55:54 +0530502#else
503struct mctp_binding_astlpc * __attribute__((const))
504 mctp_astlpc_init_fileio(void)
505{
506 return NULL;
507}
Jeremy Kerr672c8852019-03-01 12:18:07 +0800508
Jeremy Kerr92a10a62019-08-28 16:55:54 +0530509int __attribute__((const)) mctp_astlpc_get_fd(
510 struct mctp_binding_astlpc *astlpc __attribute__((unused)))
511{
512 return -1;
513}
514#endif