blob: 320be6a0c68ebacc4e58243b6c726296eed640c1 [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
Andrew Jeffery3286f172020-03-17 23:04:13 +10303#if HAVE_CONFIG_H
4#include "config.h"
5#endif
6
7#if HAVE_ENDIAN_H
Jeremy Kerr92a10a62019-08-28 16:55:54 +05308#include <endian.h>
Andrew Jeffery3286f172020-03-17 23:04:13 +10309#endif
10
11#include <assert.h>
Andrew Jeffery59c6a5c2020-01-17 15:52:51 +103012#include <err.h>
Andrew Jeffery7cd72f12020-05-12 20:27:59 +093013#include <errno.h>
Andrew Jefferyedfe3832020-02-06 11:52:11 +103014#include <inttypes.h>
Jeremy Kerr672c8852019-03-01 12:18:07 +080015#include <stdbool.h>
16#include <stdlib.h>
17#include <string.h>
Jeremy Kerr672c8852019-03-01 12:18:07 +080018
Jeremy Kerr672c8852019-03-01 12:18:07 +080019#define pr_fmt(x) "astlpc: " x
20
21#include "libmctp.h"
22#include "libmctp-alloc.h"
23#include "libmctp-log.h"
24#include "libmctp-astlpc.h"
Przemyslaw Czarnowskiff25d7e2020-03-26 11:39:37 +010025#include "container_of.h"
Jeremy Kerr672c8852019-03-01 12:18:07 +080026
Jeremy Kerrb214c642019-11-27 11:34:00 +080027#ifdef MCTP_HAVE_FILEIO
Jeremy Kerr92a10a62019-08-28 16:55:54 +053028
Jeremy Kerrc6f676d2019-12-19 09:24:06 +080029#include <unistd.h>
Jeremy Kerr92a10a62019-08-28 16:55:54 +053030#include <fcntl.h>
31#include <sys/ioctl.h>
32#include <sys/mman.h>
33#include <linux/aspeed-lpc-ctrl.h>
34
35/* kernel interface */
36static const char *kcs_path = "/dev/mctp0";
37static const char *lpc_path = "/dev/aspeed-lpc-ctrl";
38
39#endif
40
Andrew Jeffery7cd72f12020-05-12 20:27:59 +093041struct mctp_astlpc_buffer {
42 uint32_t offset;
43 uint32_t size;
44};
45
46struct mctp_astlpc_layout {
47 struct mctp_astlpc_buffer rx;
48 struct mctp_astlpc_buffer tx;
49};
50
Jeremy Kerr672c8852019-03-01 12:18:07 +080051struct mctp_binding_astlpc {
52 struct mctp_binding binding;
Jeremy Kerrbc53d352019-08-28 14:26:14 +053053
Andrew Jeffery55fb90b2020-05-12 13:54:37 +093054 void *lpc_map;
Andrew Jeffery7cd72f12020-05-12 20:27:59 +093055 struct mctp_astlpc_layout layout;
56
57 uint8_t mode;
Jeremy Kerrbc53d352019-08-28 14:26:14 +053058
59 /* direct ops data */
Andrew Jeffery55fb90b2020-05-12 13:54:37 +093060 struct mctp_binding_astlpc_ops ops;
61 void *ops_data;
Jeremy Kerrbc53d352019-08-28 14:26:14 +053062
63 /* fileio ops data */
64 void *lpc_map_base;
Jeremy Kerr672c8852019-03-01 12:18:07 +080065 int kcs_fd;
66 uint8_t kcs_status;
67
68 bool running;
Jeremy Kerr672c8852019-03-01 12:18:07 +080069};
70
Jeremy Kerr672c8852019-03-01 12:18:07 +080071#define binding_to_astlpc(b) \
72 container_of(b, struct mctp_binding_astlpc, binding)
73
Andrew Jeffery9101a2a2020-05-22 16:08:03 +093074#define astlpc_prlog(ctx, lvl, fmt, ...) \
75 do { \
76 bool __bmc = ((ctx)->mode == MCTP_BINDING_ASTLPC_MODE_BMC); \
77 mctp_prlog(lvl, pr_fmt("%s: " fmt), __bmc ? "bmc" : "host", \
78 ##__VA_ARGS__); \
79 } while (0)
80
81#define astlpc_prerr(ctx, fmt, ...) \
82 astlpc_prlog(ctx, MCTP_LOG_ERR, fmt, ##__VA_ARGS__)
83#define astlpc_prwarn(ctx, fmt, ...) \
84 astlpc_prlog(ctx, MCTP_LOG_WARNING, fmt, ##__VA_ARGS__)
85#define astlpc_prinfo(ctx, fmt, ...) \
86 astlpc_prlog(ctx, MCTP_LOG_INFO, fmt, ##__VA_ARGS__)
87#define astlpc_prdebug(ctx, fmt, ...) \
88 astlpc_prlog(ctx, MCTP_LOG_DEBUG, fmt, ##__VA_ARGS__)
89
Andrew Jeffery7cd72f12020-05-12 20:27:59 +093090/* clang-format off */
91#define ASTLPC_MCTP_MAGIC 0x4d435450
92#define ASTLPC_VER_MIN 1
93#define ASTLPC_VER_CUR 1
94
95#define ASTLPC_BODY_SIZE(sz) ((sz) - 4)
96/* clang-format on */
Jeremy Kerr672c8852019-03-01 12:18:07 +080097
98struct mctp_lpcmap_hdr {
99 uint32_t magic;
100
101 uint16_t bmc_ver_min;
102 uint16_t bmc_ver_cur;
103 uint16_t host_ver_min;
104 uint16_t host_ver_cur;
105 uint16_t negotiated_ver;
106 uint16_t pad0;
107
108 uint32_t rx_offset;
109 uint32_t rx_size;
110 uint32_t tx_offset;
111 uint32_t tx_size;
112} __attribute__((packed));
113
114/* layout of TX/RX areas */
115static const uint32_t rx_offset = 0x100;
116static const uint32_t rx_size = 0x100;
117static const uint32_t tx_offset = 0x200;
118static const uint32_t tx_size = 0x100;
119
Jeremy Kerr672c8852019-03-01 12:18:07 +0800120#define LPC_WIN_SIZE (1 * 1024 * 1024)
121
Jeremy Kerr672c8852019-03-01 12:18:07 +0800122#define KCS_STATUS_BMC_READY 0x80
123#define KCS_STATUS_CHANNEL_ACTIVE 0x40
124#define KCS_STATUS_IBF 0x02
125#define KCS_STATUS_OBF 0x01
126
Andrew Jefferyf13cb972020-05-28 09:30:09 +0930127static inline int mctp_astlpc_kcs_write(struct mctp_binding_astlpc *astlpc,
128 enum mctp_binding_astlpc_kcs_reg reg,
129 uint8_t val)
130{
131 return astlpc->ops.kcs_write(astlpc->ops_data, reg, val);
132}
133
134static inline int mctp_astlpc_kcs_read(struct mctp_binding_astlpc *astlpc,
135 enum mctp_binding_astlpc_kcs_reg reg,
136 uint8_t *val)
137{
138 return astlpc->ops.kcs_read(astlpc->ops_data, reg, val);
139}
140
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930141static inline int mctp_astlpc_lpc_write(struct mctp_binding_astlpc *astlpc,
142 const void *buf, long offset,
143 size_t len)
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530144{
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930145 astlpc_prdebug(astlpc, "%s: %zu bytes to 0x%lx", __func__, len, offset);
146
147 assert(offset >= 0);
148
149 /* Indirect access */
150 if (astlpc->ops.lpc_write) {
151 void *data = astlpc->ops_data;
152
153 return astlpc->ops.lpc_write(data, buf, offset, len);
154 }
155
156 /* Direct mapping */
157 assert(astlpc->lpc_map);
158 memcpy(&((char *)astlpc->lpc_map)[offset], buf, len);
159
160 return 0;
161}
162
163static inline int mctp_astlpc_lpc_read(struct mctp_binding_astlpc *astlpc,
164 void *buf, long offset, size_t len)
165{
166 astlpc_prdebug(astlpc, "%s: %zu bytes from 0x%lx", __func__, len,
167 offset);
168
169 assert(offset >= 0);
170
171 /* Indirect access */
172 if (astlpc->ops.lpc_read) {
173 void *data = astlpc->ops_data;
174
175 return astlpc->ops.lpc_read(data, buf, offset, len);
176 }
177
178 /* Direct mapping */
179 assert(astlpc->lpc_map);
180 memcpy(buf, &((char *)astlpc->lpc_map)[offset], len);
181
182 return 0;
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530183}
184
Andrew Jefferyd0f5da02020-05-28 09:12:55 +0930185static int mctp_astlpc_kcs_set_status(struct mctp_binding_astlpc *astlpc,
186 uint8_t status)
187{
188 uint8_t data;
189 int rc;
190
191 /* Since we're setting the status register, we want the other endpoint
192 * to be interrupted. However, some hardware may only raise a host-side
193 * interrupt on an ODR event.
194 * So, write a dummy value of 0xff to ODR, which will ensure that an
195 * interrupt is triggered, and can be ignored by the host.
196 */
197 data = 0xff;
198 status |= KCS_STATUS_OBF;
199
Andrew Jefferyf13cb972020-05-28 09:30:09 +0930200 rc = mctp_astlpc_kcs_write(astlpc, MCTP_ASTLPC_KCS_REG_STATUS, status);
Andrew Jefferyd0f5da02020-05-28 09:12:55 +0930201 if (rc) {
202 astlpc_prwarn(astlpc, "KCS status write failed");
203 return -1;
204 }
205
Andrew Jefferyf13cb972020-05-28 09:30:09 +0930206 rc = mctp_astlpc_kcs_write(astlpc, MCTP_ASTLPC_KCS_REG_DATA, data);
Andrew Jefferyd0f5da02020-05-28 09:12:55 +0930207 if (rc) {
208 astlpc_prwarn(astlpc, "KCS dummy data write failed");
209 return -1;
210 }
211
212 return 0;
213}
214
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930215static int mctp_astlpc_init_bmc(struct mctp_binding_astlpc *astlpc)
216{
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930217 struct mctp_lpcmap_hdr hdr = { 0 };
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930218 uint8_t status;
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930219
220 /* Flip the buffers as the names are defined in terms of the host */
221 astlpc->layout.rx.offset = tx_offset;
222 astlpc->layout.rx.size = tx_size;
223 astlpc->layout.tx.offset = rx_offset;
224 astlpc->layout.tx.size = rx_size;
225
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930226 hdr = (struct mctp_lpcmap_hdr){
227 .magic = htobe32(ASTLPC_MCTP_MAGIC),
228 .bmc_ver_min = htobe16(ASTLPC_VER_MIN),
229 .bmc_ver_cur = htobe16(ASTLPC_VER_CUR),
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930230
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930231 /* Flip the buffers back as we're now describing the host's
232 * configuration to the host */
233 .rx_offset = htobe32(astlpc->layout.tx.offset),
234 .rx_size = htobe32(astlpc->layout.tx.size),
235 .tx_offset = htobe32(astlpc->layout.rx.offset),
236 .tx_size = htobe32(astlpc->layout.rx.size),
237 };
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930238
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930239 mctp_astlpc_lpc_write(astlpc, &hdr, 0, sizeof(hdr));
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930240
241 /* set status indicating that the BMC is now active */
242 status = KCS_STATUS_BMC_READY | KCS_STATUS_OBF;
Andrew Jefferyd0f5da02020-05-28 09:12:55 +0930243 return mctp_astlpc_kcs_set_status(astlpc, status);
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930244}
245
246static int mctp_binding_astlpc_start_bmc(struct mctp_binding *b)
247{
248 struct mctp_binding_astlpc *astlpc =
249 container_of(b, struct mctp_binding_astlpc, binding);
250
251 return mctp_astlpc_init_bmc(astlpc);
252}
253
254static int mctp_astlpc_init_host(struct mctp_binding_astlpc *astlpc)
255{
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930256 const uint16_t ver_min_be = htobe16(ASTLPC_VER_MIN);
257 const uint16_t ver_cur_be = htobe16(ASTLPC_VER_CUR);
258 struct mctp_lpcmap_hdr hdr;
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930259 uint8_t status;
260 int rc;
261
Andrew Jefferyf13cb972020-05-28 09:30:09 +0930262 rc = mctp_astlpc_kcs_read(astlpc, MCTP_ASTLPC_KCS_REG_STATUS, &status);
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930263 if (rc) {
264 mctp_prwarn("KCS status read failed");
265 return rc;
266 }
267
268 astlpc->kcs_status = status;
269
270 if (!(status & KCS_STATUS_BMC_READY))
271 return -EHOSTDOWN;
272
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930273 mctp_astlpc_lpc_read(astlpc, &hdr, 0, sizeof(hdr));
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930274
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930275 astlpc->layout.rx.offset = be32toh(hdr.rx_offset);
276 astlpc->layout.rx.size = be32toh(hdr.rx_size);
277 astlpc->layout.tx.offset = be32toh(hdr.tx_offset);
278 astlpc->layout.tx.size = be32toh(hdr.tx_size);
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930279
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930280 mctp_astlpc_lpc_write(astlpc, &ver_min_be,
281 offsetof(struct mctp_lpcmap_hdr, host_ver_min),
282 sizeof(ver_min_be));
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930283
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930284 mctp_astlpc_lpc_write(astlpc, &ver_cur_be,
285 offsetof(struct mctp_lpcmap_hdr, host_ver_cur),
286 sizeof(ver_cur_be));
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930287
288 /* Send channel init command */
Andrew Jefferyf13cb972020-05-28 09:30:09 +0930289 rc = mctp_astlpc_kcs_write(astlpc, MCTP_ASTLPC_KCS_REG_DATA, 0x0);
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930290 if (rc) {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930291 astlpc_prwarn(astlpc, "KCS write failed");
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930292 }
293
294 return rc;
295}
296
297static int mctp_binding_astlpc_start_host(struct mctp_binding *b)
298{
299 struct mctp_binding_astlpc *astlpc =
300 container_of(b, struct mctp_binding_astlpc, binding);
301
302 return mctp_astlpc_init_host(astlpc);
303}
304
305static bool __mctp_astlpc_kcs_ready(struct mctp_binding_astlpc *astlpc,
306 uint8_t status, bool is_write)
307{
308 bool is_bmc;
309 bool ready_state;
310 uint8_t flag;
311
312 is_bmc = (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_BMC);
313 flag = (is_bmc ^ is_write) ? KCS_STATUS_IBF : KCS_STATUS_OBF;
314 ready_state = is_write ? 0 : 1;
315
316 return !!(status & flag) == ready_state;
317}
318
319static inline bool
320mctp_astlpc_kcs_read_ready(struct mctp_binding_astlpc *astlpc, uint8_t status)
321{
322 return __mctp_astlpc_kcs_ready(astlpc, status, false);
323}
324
325static inline bool
326mctp_astlpc_kcs_write_ready(struct mctp_binding_astlpc *astlpc, uint8_t status)
327{
328 return __mctp_astlpc_kcs_ready(astlpc, status, true);
329}
330
Jeremy Kerr672c8852019-03-01 12:18:07 +0800331static int mctp_astlpc_kcs_send(struct mctp_binding_astlpc *astlpc,
332 uint8_t data)
333{
334 uint8_t status;
335 int rc;
336
337 for (;;) {
Andrew Jefferyf13cb972020-05-28 09:30:09 +0930338 rc = mctp_astlpc_kcs_read(astlpc, MCTP_ASTLPC_KCS_REG_STATUS,
339 &status);
Andrew Jeffery1b27fe82020-01-24 16:05:11 +1030340 if (rc) {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930341 astlpc_prwarn(astlpc, "KCS status read failed");
Jeremy Kerr672c8852019-03-01 12:18:07 +0800342 return -1;
343 }
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930344 if (mctp_astlpc_kcs_write_ready(astlpc, status))
Jeremy Kerr672c8852019-03-01 12:18:07 +0800345 break;
346 /* todo: timeout */
347 }
348
Andrew Jefferyf13cb972020-05-28 09:30:09 +0930349 rc = mctp_astlpc_kcs_write(astlpc, MCTP_ASTLPC_KCS_REG_DATA, data);
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530350 if (rc) {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930351 astlpc_prwarn(astlpc, "KCS data write failed");
Jeremy Kerr672c8852019-03-01 12:18:07 +0800352 return -1;
353 }
354
355 return 0;
356}
357
358static int mctp_binding_astlpc_tx(struct mctp_binding *b,
359 struct mctp_pktbuf *pkt)
360{
361 struct mctp_binding_astlpc *astlpc = binding_to_astlpc(b);
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930362 uint32_t len, len_be;
Andrew Jefferyedfe3832020-02-06 11:52:11 +1030363 struct mctp_hdr *hdr;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800364
Andrew Jefferyedfe3832020-02-06 11:52:11 +1030365 hdr = mctp_pktbuf_hdr(pkt);
Jeremy Kerr672c8852019-03-01 12:18:07 +0800366 len = mctp_pktbuf_size(pkt);
Andrew Jefferyedfe3832020-02-06 11:52:11 +1030367
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930368 astlpc_prdebug(astlpc,
369 "%s: Transmitting %" PRIu32
370 "-byte packet (%hhu, %hhu, 0x%hhx)",
371 __func__, len, hdr->src, hdr->dest, hdr->flags_seq_tag);
Andrew Jefferyedfe3832020-02-06 11:52:11 +1030372
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930373 if (len > ASTLPC_BODY_SIZE(astlpc->layout.tx.size)) {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930374 astlpc_prwarn(astlpc, "invalid TX len 0x%x", len);
Jeremy Kerr672c8852019-03-01 12:18:07 +0800375 return -1;
376 }
377
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930378 len_be = htobe32(len);
379 mctp_astlpc_lpc_write(astlpc, &len_be, astlpc->layout.tx.offset,
380 sizeof(len_be));
381 mctp_astlpc_lpc_write(astlpc, hdr, astlpc->layout.tx.offset + 4, len);
Jeremy Kerr672c8852019-03-01 12:18:07 +0800382
383 mctp_binding_set_tx_enabled(b, false);
384
385 mctp_astlpc_kcs_send(astlpc, 0x1);
386 return 0;
387}
388
389static void mctp_astlpc_init_channel(struct mctp_binding_astlpc *astlpc)
390{
391 /* todo: actual version negotiation */
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930392 uint16_t negotiated = htobe16(1);
393 mctp_astlpc_lpc_write(astlpc, &negotiated,
394 offsetof(struct mctp_lpcmap_hdr, negotiated_ver),
395 sizeof(negotiated));
396
397 mctp_astlpc_kcs_set_status(astlpc, KCS_STATUS_BMC_READY |
398 KCS_STATUS_CHANNEL_ACTIVE |
399 KCS_STATUS_OBF);
Jeremy Kerr672c8852019-03-01 12:18:07 +0800400
401 mctp_binding_set_tx_enabled(&astlpc->binding, true);
402}
403
404static void mctp_astlpc_rx_start(struct mctp_binding_astlpc *astlpc)
405{
406 struct mctp_pktbuf *pkt;
407 uint32_t len;
408
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930409 mctp_astlpc_lpc_read(astlpc, &len, astlpc->layout.rx.offset,
410 sizeof(len));
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530411 len = be32toh(len);
412
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930413 if (len > ASTLPC_BODY_SIZE(astlpc->layout.rx.size)) {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930414 astlpc_prwarn(astlpc, "invalid RX len 0x%x", len);
Jeremy Kerr672c8852019-03-01 12:18:07 +0800415 return;
416 }
417
Andrew Jefferyb93b6112020-06-05 14:13:44 +0930418 assert(astlpc->binding.pkt_size >= 0);
419 if (len > (uint32_t)astlpc->binding.pkt_size) {
Jeremy Kerr672c8852019-03-01 12:18:07 +0800420 mctp_prwarn("invalid RX len 0x%x", len);
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930421 astlpc_prwarn(astlpc, "invalid RX len 0x%x", len);
Jeremy Kerr672c8852019-03-01 12:18:07 +0800422 return;
423 }
424
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +0800425 pkt = mctp_pktbuf_alloc(&astlpc->binding, len);
Jeremy Kerr672c8852019-03-01 12:18:07 +0800426 if (!pkt)
427 goto out_complete;
428
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930429 mctp_astlpc_lpc_read(astlpc, mctp_pktbuf_hdr(pkt),
430 astlpc->layout.rx.offset + 4, len);
Jeremy Kerr672c8852019-03-01 12:18:07 +0800431
432 mctp_bus_rx(&astlpc->binding, pkt);
433
434out_complete:
435 mctp_astlpc_kcs_send(astlpc, 0x2);
436}
437
438static void mctp_astlpc_tx_complete(struct mctp_binding_astlpc *astlpc)
439{
440 mctp_binding_set_tx_enabled(&astlpc->binding, true);
441}
442
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930443static int mctp_astlpc_update_channel(struct mctp_binding_astlpc *astlpc,
444 uint8_t status)
445{
446 uint8_t updated;
447 int rc = 0;
448
449 assert(astlpc->mode == MCTP_BINDING_ASTLPC_MODE_HOST);
450
451 updated = astlpc->kcs_status ^ status;
452
Andrew Jefferyd0f5da02020-05-28 09:12:55 +0930453 astlpc_prdebug(astlpc, "%s: status: 0x%x, update: 0x%x", __func__,
454 status, updated);
455
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930456 if (updated & KCS_STATUS_BMC_READY) {
Andrew Jefferyd0f5da02020-05-28 09:12:55 +0930457 if (status & KCS_STATUS_BMC_READY) {
458 astlpc->kcs_status = status;
459 return astlpc->binding.start(&astlpc->binding);
460 } else {
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930461 mctp_binding_set_tx_enabled(&astlpc->binding, false);
Andrew Jefferyd0f5da02020-05-28 09:12:55 +0930462 }
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930463 }
464
465 if (updated & KCS_STATUS_CHANNEL_ACTIVE)
466 mctp_binding_set_tx_enabled(&astlpc->binding,
467 status & KCS_STATUS_CHANNEL_ACTIVE);
468
469 astlpc->kcs_status = status;
470
471 return rc;
472}
473
Jeremy Kerr672c8852019-03-01 12:18:07 +0800474int mctp_astlpc_poll(struct mctp_binding_astlpc *astlpc)
475{
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530476 uint8_t status, data;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800477 int rc;
478
Andrew Jefferyf13cb972020-05-28 09:30:09 +0930479 rc = mctp_astlpc_kcs_read(astlpc, MCTP_ASTLPC_KCS_REG_STATUS, &status);
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530480 if (rc) {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930481 astlpc_prwarn(astlpc, "KCS read error");
Jeremy Kerr672c8852019-03-01 12:18:07 +0800482 return -1;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800483 }
484
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930485 astlpc_prdebug(astlpc, "%s: status: 0x%hhx", __func__, status);
Andrew Jefferyedfe3832020-02-06 11:52:11 +1030486
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930487 if (!mctp_astlpc_kcs_read_ready(astlpc, status))
Jeremy Kerr672c8852019-03-01 12:18:07 +0800488 return 0;
489
Andrew Jefferyf13cb972020-05-28 09:30:09 +0930490 rc = mctp_astlpc_kcs_read(astlpc, MCTP_ASTLPC_KCS_REG_DATA, &data);
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530491 if (rc) {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930492 astlpc_prwarn(astlpc, "KCS data read error");
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530493 return -1;
494 }
495
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930496 astlpc_prdebug(astlpc, "%s: data: 0x%hhx", __func__, data);
Andrew Jefferyedfe3832020-02-06 11:52:11 +1030497
Jeremy Kerr672c8852019-03-01 12:18:07 +0800498 switch (data) {
499 case 0x0:
500 mctp_astlpc_init_channel(astlpc);
501 break;
502 case 0x1:
503 mctp_astlpc_rx_start(astlpc);
504 break;
505 case 0x2:
506 mctp_astlpc_tx_complete(astlpc);
507 break;
Jeremy Kerr1a4b55a2019-06-24 14:22:39 +0800508 case 0xff:
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930509 /* No responsibilities for the BMC on 0xff */
Andrew Jefferyd0f5da02020-05-28 09:12:55 +0930510 if (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_HOST) {
511 rc = mctp_astlpc_update_channel(astlpc, status);
512 if (rc < 0)
513 return rc;
514 }
515 break;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800516 default:
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930517 astlpc_prwarn(astlpc, "unknown message 0x%x", data);
Jeremy Kerr672c8852019-03-01 12:18:07 +0800518 }
Andrew Jefferyd0f5da02020-05-28 09:12:55 +0930519
520 /* Handle silent loss of bmc-ready */
521 if (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_HOST) {
522 if (!(status & KCS_STATUS_BMC_READY && data == 0xff))
523 return mctp_astlpc_update_channel(astlpc, status);
524 }
525
526 return rc;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800527}
528
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530529/* allocate and basic initialisation */
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930530static struct mctp_binding_astlpc *__mctp_astlpc_init(uint8_t mode,
531 uint32_t mtu)
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530532{
533 struct mctp_binding_astlpc *astlpc;
534
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930535 assert((mode == MCTP_BINDING_ASTLPC_MODE_BMC) ||
536 (mode == MCTP_BINDING_ASTLPC_MODE_HOST));
537
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530538 astlpc = __mctp_alloc(sizeof(*astlpc));
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930539 if (!astlpc)
540 return NULL;
541
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530542 memset(astlpc, 0, sizeof(*astlpc));
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930543 astlpc->mode = mode;
544 astlpc->lpc_map = NULL;
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530545 astlpc->binding.name = "astlpc";
546 astlpc->binding.version = 1;
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930547 astlpc->binding.pkt_size = MCTP_PACKET_SIZE(mtu);
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530548 astlpc->binding.pkt_pad = 0;
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930549 astlpc->binding.tx = mctp_binding_astlpc_tx;
550 if (mode == MCTP_BINDING_ASTLPC_MODE_BMC)
551 astlpc->binding.start = mctp_binding_astlpc_start_bmc;
552 else if (mode == MCTP_BINDING_ASTLPC_MODE_HOST)
553 astlpc->binding.start = mctp_binding_astlpc_start_host;
554 else {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930555 astlpc_prerr(astlpc, "%s: Invalid mode: %d\n", __func__, mode);
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930556 __mctp_free(astlpc);
557 return NULL;
558 }
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530559
560 return astlpc;
561}
562
Jeremy Kerr3b36d172019-09-04 11:56:09 +0800563struct mctp_binding *mctp_binding_astlpc_core(struct mctp_binding_astlpc *b)
564{
565 return &b->binding;
566}
567
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930568struct mctp_binding_astlpc *
569mctp_astlpc_init(uint8_t mode, uint32_t mtu, void *lpc_map,
570 const struct mctp_binding_astlpc_ops *ops, void *ops_data)
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530571{
572 struct mctp_binding_astlpc *astlpc;
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530573
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930574 if (!(mode == MCTP_BINDING_ASTLPC_MODE_BMC ||
575 mode == MCTP_BINDING_ASTLPC_MODE_HOST)) {
576 mctp_prerr("Unknown binding mode: %u", mode);
577 return NULL;
578 }
579
580 if (mtu != MCTP_BTU) {
581 mctp_prwarn("Unable to negotiate the MTU, using %u instead",
582 MCTP_BTU);
583 mtu = MCTP_BTU;
584 }
585
586 astlpc = __mctp_astlpc_init(mode, mtu);
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530587 if (!astlpc)
588 return NULL;
589
590 memcpy(&astlpc->ops, ops, sizeof(astlpc->ops));
591 astlpc->ops_data = ops_data;
592 astlpc->lpc_map = lpc_map;
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930593 astlpc->mode = mode;
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530594
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530595 return astlpc;
596}
597
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930598struct mctp_binding_astlpc *
599mctp_astlpc_init_ops(const struct mctp_binding_astlpc_ops *ops, void *ops_data,
600 void *lpc_map)
601{
602 return mctp_astlpc_init(MCTP_BINDING_ASTLPC_MODE_BMC, MCTP_BTU, lpc_map,
603 ops, ops_data);
604}
605
Andrew Jeffery4663f672020-03-10 23:36:24 +1030606void mctp_astlpc_destroy(struct mctp_binding_astlpc *astlpc)
607{
Andrew Jefferyd0f5da02020-05-28 09:12:55 +0930608 /* Clear channel-active and bmc-ready */
609 if (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_BMC)
610 mctp_astlpc_kcs_set_status(astlpc, KCS_STATUS_OBF);
Andrew Jeffery4663f672020-03-10 23:36:24 +1030611 __mctp_free(astlpc);
612}
613
Jeremy Kerrb214c642019-11-27 11:34:00 +0800614#ifdef MCTP_HAVE_FILEIO
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930615
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530616static int mctp_astlpc_init_fileio_lpc(struct mctp_binding_astlpc *astlpc)
Jeremy Kerr672c8852019-03-01 12:18:07 +0800617{
618 struct aspeed_lpc_ctrl_mapping map = {
619 .window_type = ASPEED_LPC_CTRL_WINDOW_MEMORY,
620 .window_id = 0, /* There's only one */
621 .flags = 0,
622 .addr = 0,
623 .offset = 0,
624 .size = 0
625 };
626 int fd, rc;
627
628 fd = open(lpc_path, O_RDWR | O_SYNC);
629 if (fd < 0) {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930630 astlpc_prwarn(astlpc, "LPC open (%s) failed", lpc_path);
Jeremy Kerr672c8852019-03-01 12:18:07 +0800631 return -1;
632 }
633
634 rc = ioctl(fd, ASPEED_LPC_CTRL_IOCTL_GET_SIZE, &map);
635 if (rc) {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930636 astlpc_prwarn(astlpc, "LPC GET_SIZE failed");
Jeremy Kerr672c8852019-03-01 12:18:07 +0800637 close(fd);
638 return -1;
639 }
640
641 astlpc->lpc_map_base = mmap(NULL, map.size, PROT_READ | PROT_WRITE,
642 MAP_SHARED, fd, 0);
643 if (astlpc->lpc_map_base == MAP_FAILED) {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930644 astlpc_prwarn(astlpc, "LPC mmap failed");
Jeremy Kerr672c8852019-03-01 12:18:07 +0800645 rc = -1;
646 } else {
647 astlpc->lpc_map = astlpc->lpc_map_base +
648 map.size - LPC_WIN_SIZE;
649 }
650
651 close(fd);
652
653 return rc;
654}
655
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530656static int mctp_astlpc_init_fileio_kcs(struct mctp_binding_astlpc *astlpc)
Jeremy Kerr672c8852019-03-01 12:18:07 +0800657{
658 astlpc->kcs_fd = open(kcs_path, O_RDWR);
659 if (astlpc->kcs_fd < 0)
660 return -1;
661
662 return 0;
663}
664
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530665static int __mctp_astlpc_fileio_kcs_read(void *arg,
666 enum mctp_binding_astlpc_kcs_reg reg, uint8_t *val)
667{
668 struct mctp_binding_astlpc *astlpc = arg;
669 off_t offset = reg;
670 int rc;
671
672 rc = pread(astlpc->kcs_fd, val, 1, offset);
673
674 return rc == 1 ? 0 : -1;
675}
676
677static int __mctp_astlpc_fileio_kcs_write(void *arg,
678 enum mctp_binding_astlpc_kcs_reg reg, uint8_t val)
679{
680 struct mctp_binding_astlpc *astlpc = arg;
681 off_t offset = reg;
682 int rc;
683
684 rc = pwrite(astlpc->kcs_fd, &val, 1, offset);
685
686 return rc == 1 ? 0 : -1;
687}
688
689int mctp_astlpc_get_fd(struct mctp_binding_astlpc *astlpc)
690{
691 return astlpc->kcs_fd;
692}
693
694struct mctp_binding_astlpc *mctp_astlpc_init_fileio(void)
Jeremy Kerr672c8852019-03-01 12:18:07 +0800695{
696 struct mctp_binding_astlpc *astlpc;
697 int rc;
698
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930699 /*
700 * If we're doing file IO then we're very likely not running
701 * freestanding, so lets assume that we're on the BMC side
702 */
703 astlpc = __mctp_astlpc_init(MCTP_BINDING_ASTLPC_MODE_BMC, MCTP_BTU);
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530704 if (!astlpc)
705 return NULL;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800706
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530707 /* Set internal operations for kcs. We use direct accesses to the lpc
708 * map area */
709 astlpc->ops.kcs_read = __mctp_astlpc_fileio_kcs_read;
710 astlpc->ops.kcs_write = __mctp_astlpc_fileio_kcs_write;
711 astlpc->ops_data = astlpc;
712
713 rc = mctp_astlpc_init_fileio_lpc(astlpc);
Jeremy Kerr672c8852019-03-01 12:18:07 +0800714 if (rc) {
715 free(astlpc);
716 return NULL;
717 }
718
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530719 rc = mctp_astlpc_init_fileio_kcs(astlpc);
Jeremy Kerr672c8852019-03-01 12:18:07 +0800720 if (rc) {
721 free(astlpc);
722 return NULL;
723 }
724
Jeremy Kerr672c8852019-03-01 12:18:07 +0800725 return astlpc;
726}
Jeremy Kerr92a10a62019-08-28 16:55:54 +0530727#else
728struct mctp_binding_astlpc * __attribute__((const))
729 mctp_astlpc_init_fileio(void)
730{
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930731 astlpc_prerr(astlpc, "Missing support for file IO");
Jeremy Kerr92a10a62019-08-28 16:55:54 +0530732 return NULL;
733}
Jeremy Kerr672c8852019-03-01 12:18:07 +0800734
Jeremy Kerr92a10a62019-08-28 16:55:54 +0530735int __attribute__((const)) mctp_astlpc_get_fd(
736 struct mctp_binding_astlpc *astlpc __attribute__((unused)))
737{
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930738 astlpc_prerr(astlpc, "Missing support for file IO");
Jeremy Kerr92a10a62019-08-28 16:55:54 +0530739 return -1;
740}
741#endif