blob: d1cf50900a40c1a26820115353bfdfe936115f12 [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>
Jeremy Kerr672c8852019-03-01 12:18:07 +08008
Jeremy Kerr672c8852019-03-01 12:18:07 +08009#define pr_fmt(x) "astlpc: " x
10
11#include "libmctp.h"
12#include "libmctp-alloc.h"
13#include "libmctp-log.h"
14#include "libmctp-astlpc.h"
15
Jeremy Kerrb214c642019-11-27 11:34:00 +080016#ifdef MCTP_HAVE_FILEIO
Jeremy Kerr92a10a62019-08-28 16:55:54 +053017
Jeremy Kerrc6f676d2019-12-19 09:24:06 +080018#include <unistd.h>
Jeremy Kerr92a10a62019-08-28 16:55:54 +053019#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 Kerr8081beb2019-09-04 12:33:00 +0800335static int mctp_binding_astlpc_start(struct mctp_binding *b)
336{
337 struct mctp_binding_astlpc *astlpc = container_of(b,
338 struct mctp_binding_astlpc, binding);
339
340 return mctp_astlpc_init_bmc(astlpc);
341}
342
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530343/* allocate and basic initialisation */
344static struct mctp_binding_astlpc *__mctp_astlpc_init(void)
345{
346 struct mctp_binding_astlpc *astlpc;
347
348 astlpc = __mctp_alloc(sizeof(*astlpc));
349 memset(astlpc, 0, sizeof(*astlpc));
350 astlpc->binding.name = "astlpc";
351 astlpc->binding.version = 1;
352 astlpc->binding.tx = mctp_binding_astlpc_tx;
Jeremy Kerr8081beb2019-09-04 12:33:00 +0800353 astlpc->binding.start = mctp_binding_astlpc_start;
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530354 astlpc->binding.pkt_size = MCTP_BMTU;
355 astlpc->binding.pkt_pad = 0;
356 astlpc->lpc_map = NULL;
357
358 return astlpc;
359}
360
Jeremy Kerr3b36d172019-09-04 11:56:09 +0800361struct mctp_binding *mctp_binding_astlpc_core(struct mctp_binding_astlpc *b)
362{
363 return &b->binding;
364}
365
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530366struct mctp_binding_astlpc *mctp_astlpc_init_ops(
367 struct mctp_binding_astlpc_ops *ops,
368 void *ops_data, void *lpc_map)
369{
370 struct mctp_binding_astlpc *astlpc;
371 int rc;
372
373 astlpc = __mctp_astlpc_init();
374 if (!astlpc)
375 return NULL;
376
377 memcpy(&astlpc->ops, ops, sizeof(astlpc->ops));
378 astlpc->ops_data = ops_data;
379 astlpc->lpc_map = lpc_map;
380
381 /* In indirect mode, we keep a separate buffer of header data.
382 * We need to sync this through the lpc_read/lpc_write ops.
383 */
384 if (!astlpc->lpc_map)
385 astlpc->priv_hdr = __mctp_alloc(sizeof(*astlpc->priv_hdr));
386
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530387 return astlpc;
388}
389
Jeremy Kerrb214c642019-11-27 11:34:00 +0800390#ifdef MCTP_HAVE_FILEIO
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530391static int mctp_astlpc_init_fileio_lpc(struct mctp_binding_astlpc *astlpc)
Jeremy Kerr672c8852019-03-01 12:18:07 +0800392{
393 struct aspeed_lpc_ctrl_mapping map = {
394 .window_type = ASPEED_LPC_CTRL_WINDOW_MEMORY,
395 .window_id = 0, /* There's only one */
396 .flags = 0,
397 .addr = 0,
398 .offset = 0,
399 .size = 0
400 };
401 int fd, rc;
402
403 fd = open(lpc_path, O_RDWR | O_SYNC);
404 if (fd < 0) {
405 mctp_prwarn("LPC open (%s) failed", lpc_path);
406 return -1;
407 }
408
409 rc = ioctl(fd, ASPEED_LPC_CTRL_IOCTL_GET_SIZE, &map);
410 if (rc) {
411 mctp_prwarn("LPC GET_SIZE failed");
412 close(fd);
413 return -1;
414 }
415
416 astlpc->lpc_map_base = mmap(NULL, map.size, PROT_READ | PROT_WRITE,
417 MAP_SHARED, fd, 0);
418 if (astlpc->lpc_map_base == MAP_FAILED) {
419 mctp_prwarn("LPC mmap failed");
420 rc = -1;
421 } else {
422 astlpc->lpc_map = astlpc->lpc_map_base +
423 map.size - LPC_WIN_SIZE;
424 }
425
426 close(fd);
427
428 return rc;
429}
430
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530431static int mctp_astlpc_init_fileio_kcs(struct mctp_binding_astlpc *astlpc)
Jeremy Kerr672c8852019-03-01 12:18:07 +0800432{
433 astlpc->kcs_fd = open(kcs_path, O_RDWR);
434 if (astlpc->kcs_fd < 0)
435 return -1;
436
437 return 0;
438}
439
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530440static int __mctp_astlpc_fileio_kcs_read(void *arg,
441 enum mctp_binding_astlpc_kcs_reg reg, uint8_t *val)
442{
443 struct mctp_binding_astlpc *astlpc = arg;
444 off_t offset = reg;
445 int rc;
446
447 rc = pread(astlpc->kcs_fd, val, 1, offset);
448
449 return rc == 1 ? 0 : -1;
450}
451
452static int __mctp_astlpc_fileio_kcs_write(void *arg,
453 enum mctp_binding_astlpc_kcs_reg reg, uint8_t val)
454{
455 struct mctp_binding_astlpc *astlpc = arg;
456 off_t offset = reg;
457 int rc;
458
459 rc = pwrite(astlpc->kcs_fd, &val, 1, offset);
460
461 return rc == 1 ? 0 : -1;
462}
463
464int mctp_astlpc_get_fd(struct mctp_binding_astlpc *astlpc)
465{
466 return astlpc->kcs_fd;
467}
468
469struct mctp_binding_astlpc *mctp_astlpc_init_fileio(void)
Jeremy Kerr672c8852019-03-01 12:18:07 +0800470{
471 struct mctp_binding_astlpc *astlpc;
472 int rc;
473
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530474 astlpc = __mctp_astlpc_init();
475 if (!astlpc)
476 return NULL;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800477
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530478 /* Set internal operations for kcs. We use direct accesses to the lpc
479 * map area */
480 astlpc->ops.kcs_read = __mctp_astlpc_fileio_kcs_read;
481 astlpc->ops.kcs_write = __mctp_astlpc_fileio_kcs_write;
482 astlpc->ops_data = astlpc;
483
484 rc = mctp_astlpc_init_fileio_lpc(astlpc);
Jeremy Kerr672c8852019-03-01 12:18:07 +0800485 if (rc) {
486 free(astlpc);
487 return NULL;
488 }
489
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530490 rc = mctp_astlpc_init_fileio_kcs(astlpc);
Jeremy Kerr672c8852019-03-01 12:18:07 +0800491 if (rc) {
492 free(astlpc);
493 return NULL;
494 }
495
Jeremy Kerr672c8852019-03-01 12:18:07 +0800496 return astlpc;
497}
Jeremy Kerr92a10a62019-08-28 16:55:54 +0530498#else
499struct mctp_binding_astlpc * __attribute__((const))
500 mctp_astlpc_init_fileio(void)
501{
502 return NULL;
503}
Jeremy Kerr672c8852019-03-01 12:18:07 +0800504
Jeremy Kerr92a10a62019-08-28 16:55:54 +0530505int __attribute__((const)) mctp_astlpc_get_fd(
506 struct mctp_binding_astlpc *astlpc __attribute__((unused)))
507{
508 return -1;
509}
510#endif