blob: 0acc39bd07291537a6cec4ab0327fdeceb1c1ec8 [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
Andrew Jefferyeba19a32021-03-09 23:09:40 +103021#include "container_of.h"
22#include "crc32.h"
Jeremy Kerr672c8852019-03-01 12:18:07 +080023#include "libmctp.h"
24#include "libmctp-alloc.h"
25#include "libmctp-log.h"
26#include "libmctp-astlpc.h"
Andrew Jeffery4622cad2020-11-03 22:20:18 +103027#include "range.h"
Jeremy Kerr672c8852019-03-01 12:18:07 +080028
Jeremy Kerrb214c642019-11-27 11:34:00 +080029#ifdef MCTP_HAVE_FILEIO
Jeremy Kerr92a10a62019-08-28 16:55:54 +053030
Jeremy Kerrc6f676d2019-12-19 09:24:06 +080031#include <unistd.h>
Jeremy Kerr92a10a62019-08-28 16:55:54 +053032#include <fcntl.h>
Andrew Jeffery1111c6a2022-07-25 20:44:39 +093033#include <poll.h>
Jeremy Kerr92a10a62019-08-28 16:55:54 +053034#include <sys/ioctl.h>
35#include <sys/mman.h>
36#include <linux/aspeed-lpc-ctrl.h>
37
38/* kernel interface */
Jeremy Kerr92a10a62019-08-28 16:55:54 +053039static const char *lpc_path = "/dev/aspeed-lpc-ctrl";
40
41#endif
42
Rashmica Gupta45d13322022-08-10 12:12:14 +100043enum mctp_astlpc_cmd {
44 cmd_initialise = 0x00,
45 cmd_tx_begin = 0x01,
46 cmd_rx_complete = 0x02,
47 cmd_dummy_value = 0xff,
48};
49
Andrew Jefferyfe763e92022-08-05 23:16:17 +093050enum mctp_astlpc_buffer_state {
51 /*
52 * Prior to "Channel Ready" we mark the buffers as "idle" to catch illegal accesses. In this
53 * state neither side is considered the owner of the buffer.
54 *
55 * Upon "Channel Ready", each side transitions the buffers from the initial "idle" state
56 * to the following target states:
57 *
58 * Tx buffer: "acquired"
59 * Rx buffer: "released"
60 */
61 buffer_state_idle,
62
63 /*
64 * Beyond initialisation by "Channel Ready", buffers are in the "acquired" state once:
65 *
66 * 1. We dequeue a control command transferring the buffer to our ownership out of the KCS
67 * interface, and
68 * 2. We are yet to complete all of our required accesses to the buffer
69 *
70 * * The Tx buffer enters the "acquired" state when we dequeue the "Rx Complete" command
71 * * The Rx buffer enters the "acquired" state when we dequeue the "Tx Begin" command
72 *
73 * It is a failure of implementation if it's possible for both sides to simultaneously
74 * consider a buffer as "acquired".
75 */
76 buffer_state_acquired,
77
78 /*
79 * Buffers are in the "prepared" state when:
80 *
81 * 1. We have completed all of our required accesses (read or write) for the buffer, and
82 * 2. We have not yet successfully enqueued the control command to hand off ownership
83 */
84 buffer_state_prepared,
85
86 /*
87 * Beyond initialisation by "Channel Ready", buffers are in the "released" state once:
88 *
89 * 1. We successfully enqueue the control command transferring ownership to the remote
90 * side in to the KCS interface
91 *
92 * * The Tx buffer enters the "released" state when we enqueue the "Tx Begin" command
93 * * The Rx buffer enters the "released" state when we enqueue the "Rx Complete" command
94 *
95 * It may be the case that both sides simultaneously consider a buffer to be in the
96 * "released" state. However, if this is true, it must also be true that a buffer ownership
97 * transfer command has been enqueued in the KCS interface and is yet to be dequeued.
98 */
99 buffer_state_released,
100};
101
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930102struct mctp_astlpc_buffer {
103 uint32_t offset;
104 uint32_t size;
Andrew Jefferyfe763e92022-08-05 23:16:17 +0930105 enum mctp_astlpc_buffer_state state;
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930106};
107
108struct mctp_astlpc_layout {
109 struct mctp_astlpc_buffer rx;
110 struct mctp_astlpc_buffer tx;
111};
112
Andrew Jeffery88412be2021-03-09 22:05:22 +1030113struct mctp_astlpc_protocol {
114 uint16_t version;
115 uint32_t (*packet_size)(uint32_t body);
116 uint32_t (*body_size)(uint32_t packet);
Andrew Jefferyeba19a32021-03-09 23:09:40 +1030117 void (*pktbuf_protect)(struct mctp_pktbuf *pkt);
118 bool (*pktbuf_validate)(struct mctp_pktbuf *pkt);
Andrew Jeffery88412be2021-03-09 22:05:22 +1030119};
120
Jeremy Kerr672c8852019-03-01 12:18:07 +0800121struct mctp_binding_astlpc {
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600122 struct mctp_binding binding;
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530123
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930124 void *lpc_map;
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930125 struct mctp_astlpc_layout layout;
126
127 uint8_t mode;
Andrew Jefferya9368982020-06-09 13:07:39 +0930128 uint32_t requested_mtu;
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530129
Andrew Jeffery88412be2021-03-09 22:05:22 +1030130 const struct mctp_astlpc_protocol *proto;
131
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530132 /* direct ops data */
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930133 struct mctp_binding_astlpc_ops ops;
134 void *ops_data;
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530135
136 /* fileio ops data */
Andrew Jeffery979c6a12020-05-23 20:04:49 +0930137 int kcs_fd;
138 uint8_t kcs_status;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800139};
140
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600141#define binding_to_astlpc(b) \
Jeremy Kerr672c8852019-03-01 12:18:07 +0800142 container_of(b, struct mctp_binding_astlpc, binding)
143
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930144#define astlpc_prlog(ctx, lvl, fmt, ...) \
145 do { \
146 bool __bmc = ((ctx)->mode == MCTP_BINDING_ASTLPC_MODE_BMC); \
147 mctp_prlog(lvl, pr_fmt("%s: " fmt), __bmc ? "bmc" : "host", \
148 ##__VA_ARGS__); \
149 } while (0)
150
151#define astlpc_prerr(ctx, fmt, ...) \
152 astlpc_prlog(ctx, MCTP_LOG_ERR, fmt, ##__VA_ARGS__)
153#define astlpc_prwarn(ctx, fmt, ...) \
154 astlpc_prlog(ctx, MCTP_LOG_WARNING, fmt, ##__VA_ARGS__)
Andrew Jeffery99b9d232023-06-23 15:15:30 +0930155#define astlpc_prnotice(ctx, fmt, ...) \
156 astlpc_prlog(ctx, MCTP_LOG_NOTICE, fmt, ##__VA_ARGS__)
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930157#define astlpc_prinfo(ctx, fmt, ...) \
158 astlpc_prlog(ctx, MCTP_LOG_INFO, fmt, ##__VA_ARGS__)
159#define astlpc_prdebug(ctx, fmt, ...) \
160 astlpc_prlog(ctx, MCTP_LOG_DEBUG, fmt, ##__VA_ARGS__)
161
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930162/* clang-format off */
163#define ASTLPC_MCTP_MAGIC 0x4d435450
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930164#define ASTLPC_VER_BAD 0
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930165#define ASTLPC_VER_MIN 1
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930166
Andrew Jeffery3a540662020-05-26 19:55:30 +0930167/* Support testing of new binding protocols */
168#ifndef ASTLPC_VER_CUR
Andrew Jefferyeba19a32021-03-09 23:09:40 +1030169#define ASTLPC_VER_CUR 3
Andrew Jeffery3a540662020-05-26 19:55:30 +0930170#endif
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930171/* clang-format on */
Jeremy Kerr672c8852019-03-01 12:18:07 +0800172
Andrew Jeffery88412be2021-03-09 22:05:22 +1030173#ifndef ARRAY_SIZE
174#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
175#endif
176
177static uint32_t astlpc_packet_size_v1(uint32_t body)
178{
179 assert((body + 4) > body);
180
181 return body + 4;
182}
183
184static uint32_t astlpc_body_size_v1(uint32_t packet)
185{
186 assert((packet - 4) < packet);
187
188 return packet - 4;
189}
190
Andrew Jefferyeba19a32021-03-09 23:09:40 +1030191void astlpc_pktbuf_protect_v1(struct mctp_pktbuf *pkt)
192{
193 (void)pkt;
194}
195
196bool astlpc_pktbuf_validate_v1(struct mctp_pktbuf *pkt)
197{
198 (void)pkt;
199 return true;
200}
201
202static uint32_t astlpc_packet_size_v3(uint32_t body)
203{
204 assert((body + 4 + 4) > body);
205
206 return body + 4 + 4;
207}
208
209static uint32_t astlpc_body_size_v3(uint32_t packet)
210{
211 assert((packet - 4 - 4) < packet);
212
213 return packet - 4 - 4;
214}
215
216void astlpc_pktbuf_protect_v3(struct mctp_pktbuf *pkt)
217{
218 uint32_t code;
219
220 code = htobe32(crc32(mctp_pktbuf_hdr(pkt), mctp_pktbuf_size(pkt)));
221 mctp_prdebug("%s: 0x%" PRIx32, __func__, code);
222 mctp_pktbuf_push(pkt, &code, 4);
223}
224
225bool astlpc_pktbuf_validate_v3(struct mctp_pktbuf *pkt)
226{
227 uint32_t code;
228 void *check;
229
230 code = be32toh(crc32(mctp_pktbuf_hdr(pkt), mctp_pktbuf_size(pkt) - 4));
231 mctp_prdebug("%s: 0x%" PRIx32, __func__, code);
232 check = mctp_pktbuf_pop(pkt, 4);
233 return check && !memcmp(&code, check, 4);
234}
235
Andrew Jeffery88412be2021-03-09 22:05:22 +1030236static const struct mctp_astlpc_protocol astlpc_protocol_version[] = {
237 [0] = {
238 .version = 0,
239 .packet_size = NULL,
240 .body_size = NULL,
Andrew Jefferyeba19a32021-03-09 23:09:40 +1030241 .pktbuf_protect = NULL,
242 .pktbuf_validate = NULL,
Andrew Jeffery88412be2021-03-09 22:05:22 +1030243 },
244 [1] = {
245 .version = 1,
246 .packet_size = astlpc_packet_size_v1,
247 .body_size = astlpc_body_size_v1,
Andrew Jefferyeba19a32021-03-09 23:09:40 +1030248 .pktbuf_protect = astlpc_pktbuf_protect_v1,
249 .pktbuf_validate = astlpc_pktbuf_validate_v1,
Andrew Jeffery88412be2021-03-09 22:05:22 +1030250 },
251 [2] = {
252 .version = 2,
253 .packet_size = astlpc_packet_size_v1,
254 .body_size = astlpc_body_size_v1,
Andrew Jefferyeba19a32021-03-09 23:09:40 +1030255 .pktbuf_protect = astlpc_pktbuf_protect_v1,
256 .pktbuf_validate = astlpc_pktbuf_validate_v1,
257 },
258 [3] = {
259 .version = 3,
260 .packet_size = astlpc_packet_size_v3,
261 .body_size = astlpc_body_size_v3,
262 .pktbuf_protect = astlpc_pktbuf_protect_v3,
263 .pktbuf_validate = astlpc_pktbuf_validate_v3,
Andrew Jeffery88412be2021-03-09 22:05:22 +1030264 },
265};
266
Jeremy Kerr672c8852019-03-01 12:18:07 +0800267struct mctp_lpcmap_hdr {
Andrew Jeffery3a540662020-05-26 19:55:30 +0930268 uint32_t magic;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800269
Andrew Jeffery3a540662020-05-26 19:55:30 +0930270 uint16_t bmc_ver_min;
271 uint16_t bmc_ver_cur;
272 uint16_t host_ver_min;
273 uint16_t host_ver_cur;
274 uint16_t negotiated_ver;
275 uint16_t pad0;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800276
Andrew Jeffery3a540662020-05-26 19:55:30 +0930277 struct {
278 uint32_t rx_offset;
279 uint32_t rx_size;
280 uint32_t tx_offset;
281 uint32_t tx_size;
282 } layout;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800283} __attribute__((packed));
284
Andrew Jeffery3a540662020-05-26 19:55:30 +0930285static const uint32_t control_size = 0x100;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800286
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600287#define LPC_WIN_SIZE (1 * 1024 * 1024)
Jeremy Kerr672c8852019-03-01 12:18:07 +0800288
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600289#define KCS_STATUS_BMC_READY 0x80
290#define KCS_STATUS_CHANNEL_ACTIVE 0x40
291#define KCS_STATUS_IBF 0x02
292#define KCS_STATUS_OBF 0x01
Jeremy Kerr672c8852019-03-01 12:18:07 +0800293
Andrew Jefferyf13cb972020-05-28 09:30:09 +0930294static inline int mctp_astlpc_kcs_write(struct mctp_binding_astlpc *astlpc,
295 enum mctp_binding_astlpc_kcs_reg reg,
296 uint8_t val)
297{
298 return astlpc->ops.kcs_write(astlpc->ops_data, reg, val);
299}
300
301static inline int mctp_astlpc_kcs_read(struct mctp_binding_astlpc *astlpc,
302 enum mctp_binding_astlpc_kcs_reg reg,
303 uint8_t *val)
304{
305 return astlpc->ops.kcs_read(astlpc->ops_data, reg, val);
306}
307
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930308static inline int mctp_astlpc_lpc_write(struct mctp_binding_astlpc *astlpc,
309 const void *buf, long offset,
310 size_t len)
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530311{
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930312 astlpc_prdebug(astlpc, "%s: %zu bytes to 0x%lx", __func__, len, offset);
313
314 assert(offset >= 0);
315
316 /* Indirect access */
317 if (astlpc->ops.lpc_write) {
318 void *data = astlpc->ops_data;
319
320 return astlpc->ops.lpc_write(data, buf, offset, len);
321 }
322
323 /* Direct mapping */
324 assert(astlpc->lpc_map);
325 memcpy(&((char *)astlpc->lpc_map)[offset], buf, len);
326
327 return 0;
328}
329
330static inline int mctp_astlpc_lpc_read(struct mctp_binding_astlpc *astlpc,
331 void *buf, long offset, size_t len)
332{
333 astlpc_prdebug(astlpc, "%s: %zu bytes from 0x%lx", __func__, len,
334 offset);
335
336 assert(offset >= 0);
337
338 /* Indirect access */
339 if (astlpc->ops.lpc_read) {
340 void *data = astlpc->ops_data;
341
342 return astlpc->ops.lpc_read(data, buf, offset, len);
343 }
344
345 /* Direct mapping */
346 assert(astlpc->lpc_map);
347 memcpy(buf, &((char *)astlpc->lpc_map)[offset], len);
348
349 return 0;
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530350}
351
Andrew Jeffery99b9d232023-06-23 15:15:30 +0930352static void
353mctp_astlpc_kcs_print_status_write(struct mctp_binding_astlpc *astlpc,
354 uint8_t status)
355{
356 astlpc_prnotice(
357 astlpc, "Binding state is 0x%hhx: BMC %s, Channel %s, OBF %s",
358 status, status & KCS_STATUS_BMC_READY ? "active" : "inactive",
359 status & KCS_STATUS_CHANNEL_ACTIVE ? "active" : "inactive",
360 status & KCS_STATUS_OBF ? "preserved" : "cleared");
361}
362
Andrew Jefferyd0f5da02020-05-28 09:12:55 +0930363static int mctp_astlpc_kcs_set_status(struct mctp_binding_astlpc *astlpc,
364 uint8_t status)
365{
366 uint8_t data;
367 int rc;
368
369 /* Since we're setting the status register, we want the other endpoint
370 * to be interrupted. However, some hardware may only raise a host-side
371 * interrupt on an ODR event.
372 * So, write a dummy value of 0xff to ODR, which will ensure that an
373 * interrupt is triggered, and can be ignored by the host.
374 */
Rashmica Gupta45d13322022-08-10 12:12:14 +1000375 data = cmd_dummy_value;
Andrew Jefferyd0f5da02020-05-28 09:12:55 +0930376
Andrew Jefferyf13cb972020-05-28 09:30:09 +0930377 rc = mctp_astlpc_kcs_write(astlpc, MCTP_ASTLPC_KCS_REG_STATUS, status);
Andrew Jefferyd0f5da02020-05-28 09:12:55 +0930378 if (rc) {
379 astlpc_prwarn(astlpc, "KCS status write failed");
380 return -1;
381 }
382
Andrew Jeffery99b9d232023-06-23 15:15:30 +0930383 mctp_astlpc_kcs_print_status_write(astlpc, status);
384
Andrew Jefferyf13cb972020-05-28 09:30:09 +0930385 rc = mctp_astlpc_kcs_write(astlpc, MCTP_ASTLPC_KCS_REG_DATA, data);
Andrew Jefferyd0f5da02020-05-28 09:12:55 +0930386 if (rc) {
387 astlpc_prwarn(astlpc, "KCS dummy data write failed");
388 return -1;
389 }
390
391 return 0;
392}
393
Andrew Jeffery3a540662020-05-26 19:55:30 +0930394static int mctp_astlpc_layout_read(struct mctp_binding_astlpc *astlpc,
395 struct mctp_astlpc_layout *layout)
396{
397 struct mctp_lpcmap_hdr hdr;
398 int rc;
399
400 rc = mctp_astlpc_lpc_read(astlpc, &hdr, 0, sizeof(hdr));
401 if (rc < 0)
402 return rc;
403
404 /* Flip the buffers as the names are defined in terms of the host */
405 if (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_BMC) {
406 layout->rx.offset = be32toh(hdr.layout.tx_offset);
407 layout->rx.size = be32toh(hdr.layout.tx_size);
408 layout->tx.offset = be32toh(hdr.layout.rx_offset);
409 layout->tx.size = be32toh(hdr.layout.rx_size);
410 } else {
411 assert(astlpc->mode == MCTP_BINDING_ASTLPC_MODE_HOST);
412
413 layout->rx.offset = be32toh(hdr.layout.rx_offset);
414 layout->rx.size = be32toh(hdr.layout.rx_size);
415 layout->tx.offset = be32toh(hdr.layout.tx_offset);
416 layout->tx.size = be32toh(hdr.layout.tx_size);
417 }
418
419 return 0;
420}
421
422static int mctp_astlpc_layout_write(struct mctp_binding_astlpc *astlpc,
423 struct mctp_astlpc_layout *layout)
424{
425 uint32_t rx_size_be;
426
427 if (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_BMC) {
428 struct mctp_lpcmap_hdr hdr;
429
430 /*
431 * Flip the buffers as the names are defined in terms of the
432 * host
433 */
434 hdr.layout.rx_offset = htobe32(layout->tx.offset);
435 hdr.layout.rx_size = htobe32(layout->tx.size);
436 hdr.layout.tx_offset = htobe32(layout->rx.offset);
437 hdr.layout.tx_size = htobe32(layout->rx.size);
438
439 return mctp_astlpc_lpc_write(astlpc, &hdr.layout,
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600440 offsetof(struct mctp_lpcmap_hdr,
441 layout),
442 sizeof(hdr.layout));
Andrew Jeffery3a540662020-05-26 19:55:30 +0930443 }
444
445 assert(astlpc->mode == MCTP_BINDING_ASTLPC_MODE_HOST);
446
447 /*
448 * As of v2 we only need to write rx_size - the offsets are controlled
449 * by the BMC, as is the BMC's rx_size (host tx_size).
450 */
451 rx_size_be = htobe32(layout->rx.size);
452 return mctp_astlpc_lpc_write(astlpc, &rx_size_be,
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600453 offsetof(struct mctp_lpcmap_hdr,
454 layout.rx_size),
455 sizeof(rx_size_be));
Andrew Jeffery3a540662020-05-26 19:55:30 +0930456}
457
Andrew Jeffery88412be2021-03-09 22:05:22 +1030458static bool
459mctp_astlpc_buffer_validate(const struct mctp_binding_astlpc *astlpc,
460 const struct mctp_astlpc_buffer *buf,
461 const char *name)
Andrew Jeffery3a540662020-05-26 19:55:30 +0930462{
463 /* Check for overflow */
464 if (buf->offset + buf->size < buf->offset) {
465 mctp_prerr(
466 "%s packet buffer parameters overflow: offset: 0x%" PRIx32
467 ", size: %" PRIu32,
468 name, buf->offset, buf->size);
469 return false;
470 }
471
472 /* Check that the buffers are contained within the allocated space */
473 if (buf->offset + buf->size > LPC_WIN_SIZE) {
474 mctp_prerr(
475 "%s packet buffer parameters exceed %uM window size: offset: 0x%" PRIx32
476 ", size: %" PRIu32,
477 name, (LPC_WIN_SIZE / (1024 * 1024)), buf->offset,
478 buf->size);
479 return false;
480 }
481
482 /* Check that the baseline transmission unit is supported */
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600483 if (buf->size <
484 astlpc->proto->packet_size(MCTP_PACKET_SIZE(MCTP_BTU))) {
Andrew Jeffery3a540662020-05-26 19:55:30 +0930485 mctp_prerr(
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600486 "%s packet buffer too small: Require %" PRIu32
487 " bytes to support the %u byte baseline transmission unit, found %" PRIu32,
Andrew Jeffery88412be2021-03-09 22:05:22 +1030488 name,
489 astlpc->proto->packet_size(MCTP_PACKET_SIZE(MCTP_BTU)),
Andrew Jeffery3a540662020-05-26 19:55:30 +0930490 MCTP_BTU, buf->size);
491 return false;
492 }
493
494 /* Check for overlap with the control space */
495 if (buf->offset < control_size) {
496 mctp_prerr(
497 "%s packet buffer overlaps control region {0x%" PRIx32
498 ", %" PRIu32 "}: Rx {0x%" PRIx32 ", %" PRIu32 "}",
499 name, 0U, control_size, buf->offset, buf->size);
500 return false;
501 }
502
503 return true;
504}
505
Andrew Jeffery88412be2021-03-09 22:05:22 +1030506static bool
507mctp_astlpc_layout_validate(const struct mctp_binding_astlpc *astlpc,
508 const struct mctp_astlpc_layout *layout)
Andrew Jeffery3a540662020-05-26 19:55:30 +0930509{
Andrew Jeffery88412be2021-03-09 22:05:22 +1030510 const struct mctp_astlpc_buffer *rx = &layout->rx;
511 const struct mctp_astlpc_buffer *tx = &layout->tx;
Andrew Jeffery3a540662020-05-26 19:55:30 +0930512 bool rx_valid, tx_valid;
513
Andrew Jeffery88412be2021-03-09 22:05:22 +1030514 rx_valid = mctp_astlpc_buffer_validate(astlpc, rx, "Rx");
515 tx_valid = mctp_astlpc_buffer_validate(astlpc, tx, "Tx");
Andrew Jeffery3a540662020-05-26 19:55:30 +0930516
517 if (!(rx_valid && tx_valid))
518 return false;
519
520 /* Check that the buffers are disjoint */
521 if ((rx->offset <= tx->offset && rx->offset + rx->size > tx->offset) ||
522 (tx->offset <= rx->offset && tx->offset + tx->size > rx->offset)) {
523 mctp_prerr("Rx and Tx packet buffers overlap: Rx {0x%" PRIx32
524 ", %" PRIu32 "}, Tx {0x%" PRIx32 ", %" PRIu32 "}",
525 rx->offset, rx->size, tx->offset, tx->size);
526 return false;
527 }
528
529 return true;
530}
531
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930532static int mctp_astlpc_init_bmc(struct mctp_binding_astlpc *astlpc)
533{
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930534 struct mctp_lpcmap_hdr hdr = { 0 };
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930535 uint8_t status;
Andrew Jeffery88412be2021-03-09 22:05:22 +1030536 uint32_t sz;
Andrew Jeffery3a540662020-05-26 19:55:30 +0930537
538 /*
539 * The largest buffer size is half of the allocated MCTP space
540 * excluding the control space.
541 */
542 sz = ((LPC_WIN_SIZE - control_size) / 2);
543
544 /*
545 * Trim the MTU to a multiple of 16 to meet the requirements of 12.17
546 * Query Hop in DSP0236 v1.3.0.
547 */
Andrew Jeffery88412be2021-03-09 22:05:22 +1030548 sz = MCTP_BODY_SIZE(astlpc->proto->body_size(sz));
Andrew Jeffery3a540662020-05-26 19:55:30 +0930549 sz &= ~0xfUL;
Andrew Jeffery88412be2021-03-09 22:05:22 +1030550 sz = astlpc->proto->packet_size(MCTP_PACKET_SIZE(sz));
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930551
Andrew Jefferya9368982020-06-09 13:07:39 +0930552 if (astlpc->requested_mtu) {
Andrew Jeffery88412be2021-03-09 22:05:22 +1030553 uint32_t rpkt, rmtu;
Andrew Jefferya9368982020-06-09 13:07:39 +0930554
Andrew Jeffery88412be2021-03-09 22:05:22 +1030555 rmtu = astlpc->requested_mtu;
556 rpkt = astlpc->proto->packet_size(MCTP_PACKET_SIZE(rmtu));
557 sz = MIN(sz, rpkt);
Andrew Jefferya9368982020-06-09 13:07:39 +0930558 }
559
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930560 /* Flip the buffers as the names are defined in terms of the host */
Andrew Jeffery3a540662020-05-26 19:55:30 +0930561 astlpc->layout.tx.offset = control_size;
562 astlpc->layout.tx.size = sz;
563 astlpc->layout.rx.offset =
564 astlpc->layout.tx.offset + astlpc->layout.tx.size;
565 astlpc->layout.rx.size = sz;
566
Andrew Jeffery88412be2021-03-09 22:05:22 +1030567 if (!mctp_astlpc_layout_validate(astlpc, &astlpc->layout)) {
568 astlpc_prerr(astlpc, "Cannot support an MTU of %" PRIu32, sz);
Andrew Jefferya9368982020-06-09 13:07:39 +0930569 return -EINVAL;
570 }
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930571
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930572 hdr = (struct mctp_lpcmap_hdr){
573 .magic = htobe32(ASTLPC_MCTP_MAGIC),
574 .bmc_ver_min = htobe16(ASTLPC_VER_MIN),
575 .bmc_ver_cur = htobe16(ASTLPC_VER_CUR),
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930576
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930577 /* Flip the buffers back as we're now describing the host's
578 * configuration to the host */
Andrew Jeffery3a540662020-05-26 19:55:30 +0930579 .layout.rx_offset = htobe32(astlpc->layout.tx.offset),
580 .layout.rx_size = htobe32(astlpc->layout.tx.size),
581 .layout.tx_offset = htobe32(astlpc->layout.rx.offset),
582 .layout.tx_size = htobe32(astlpc->layout.rx.size),
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930583 };
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930584
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930585 mctp_astlpc_lpc_write(astlpc, &hdr, 0, sizeof(hdr));
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930586
Andrew Jefferyb3b55a62020-07-06 13:34:18 +0930587 /*
588 * Set status indicating that the BMC is now active. Be explicit about
589 * clearing OBF; we're reinitialising the binding and so any previous
590 * buffer state is irrelevant.
591 */
592 status = KCS_STATUS_BMC_READY & ~KCS_STATUS_OBF;
Andrew Jefferyd0f5da02020-05-28 09:12:55 +0930593 return mctp_astlpc_kcs_set_status(astlpc, status);
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930594}
595
596static int mctp_binding_astlpc_start_bmc(struct mctp_binding *b)
597{
598 struct mctp_binding_astlpc *astlpc =
599 container_of(b, struct mctp_binding_astlpc, binding);
600
Andrew Jeffery88412be2021-03-09 22:05:22 +1030601 astlpc->proto = &astlpc_protocol_version[ASTLPC_VER_CUR];
602
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930603 return mctp_astlpc_init_bmc(astlpc);
604}
605
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930606static bool mctp_astlpc_validate_version(uint16_t bmc_ver_min,
607 uint16_t bmc_ver_cur,
608 uint16_t host_ver_min,
609 uint16_t host_ver_cur)
610{
611 if (!(bmc_ver_min && bmc_ver_cur && host_ver_min && host_ver_cur)) {
612 mctp_prerr("Invalid version present in [%" PRIu16 ", %" PRIu16
613 "], [%" PRIu16 ", %" PRIu16 "]",
614 bmc_ver_min, bmc_ver_cur, host_ver_min,
615 host_ver_cur);
616 return false;
617 } else if (bmc_ver_min > bmc_ver_cur) {
618 mctp_prerr("Invalid bmc version range [%" PRIu16 ", %" PRIu16
619 "]",
620 bmc_ver_min, bmc_ver_cur);
621 return false;
622 } else if (host_ver_min > host_ver_cur) {
623 mctp_prerr("Invalid host version range [%" PRIu16 ", %" PRIu16
624 "]",
625 host_ver_min, host_ver_cur);
626 return false;
627 } else if ((host_ver_cur < bmc_ver_min) ||
628 (host_ver_min > bmc_ver_cur)) {
629 mctp_prerr(
630 "Unable to satisfy version negotiation with ranges [%" PRIu16
631 ", %" PRIu16 "] and [%" PRIu16 ", %" PRIu16 "]",
632 bmc_ver_min, bmc_ver_cur, host_ver_min, host_ver_cur);
633 return false;
634 }
635
636 return true;
637}
638
Andrew Jeffery3a540662020-05-26 19:55:30 +0930639static int mctp_astlpc_negotiate_layout_host(struct mctp_binding_astlpc *astlpc)
640{
641 struct mctp_astlpc_layout layout;
Andrew Jeffery88412be2021-03-09 22:05:22 +1030642 uint32_t rmtu;
Andrew Jeffery3a540662020-05-26 19:55:30 +0930643 uint32_t sz;
644 int rc;
645
646 rc = mctp_astlpc_layout_read(astlpc, &layout);
647 if (rc < 0)
648 return rc;
649
Andrew Jeffery88412be2021-03-09 22:05:22 +1030650 if (!mctp_astlpc_layout_validate(astlpc, &layout)) {
Andrew Jeffery3a540662020-05-26 19:55:30 +0930651 astlpc_prerr(
652 astlpc,
653 "BMC provided invalid buffer layout: Rx {0x%" PRIx32
654 ", %" PRIu32 "}, Tx {0x%" PRIx32 ", %" PRIu32 "}",
655 layout.rx.offset, layout.rx.size, layout.tx.offset,
656 layout.tx.size);
657 return -EINVAL;
658 }
659
Andrew Jefferya9368982020-06-09 13:07:39 +0930660 astlpc_prinfo(astlpc, "Desire an MTU of %" PRIu32 " bytes",
661 astlpc->requested_mtu);
662
Andrew Jeffery88412be2021-03-09 22:05:22 +1030663 rmtu = astlpc->requested_mtu;
664 sz = astlpc->proto->packet_size(MCTP_PACKET_SIZE(rmtu));
Andrew Jeffery3a540662020-05-26 19:55:30 +0930665 layout.rx.size = sz;
666
Andrew Jeffery88412be2021-03-09 22:05:22 +1030667 if (!mctp_astlpc_layout_validate(astlpc, &layout)) {
Andrew Jeffery3a540662020-05-26 19:55:30 +0930668 astlpc_prerr(
669 astlpc,
670 "Generated invalid buffer layout with size %" PRIu32
671 ": Rx {0x%" PRIx32 ", %" PRIu32 "}, Tx {0x%" PRIx32
672 ", %" PRIu32 "}",
673 sz, layout.rx.offset, layout.rx.size, layout.tx.offset,
674 layout.tx.size);
675 return -EINVAL;
676 }
677
Andrew Jefferya9368982020-06-09 13:07:39 +0930678 astlpc_prinfo(astlpc, "Requesting MTU of %" PRIu32 " bytes",
679 astlpc->requested_mtu);
Andrew Jeffery3a540662020-05-26 19:55:30 +0930680
681 return mctp_astlpc_layout_write(astlpc, &layout);
682}
683
Andrew Jeffery88412be2021-03-09 22:05:22 +1030684static uint16_t mctp_astlpc_negotiate_version(uint16_t bmc_ver_min,
685 uint16_t bmc_ver_cur,
686 uint16_t host_ver_min,
687 uint16_t host_ver_cur)
688{
689 if (!mctp_astlpc_validate_version(bmc_ver_min, bmc_ver_cur,
690 host_ver_min, host_ver_cur))
691 return ASTLPC_VER_BAD;
692
693 if (bmc_ver_cur < host_ver_cur)
694 return bmc_ver_cur;
695
696 return host_ver_cur;
697}
698
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930699static int mctp_astlpc_init_host(struct mctp_binding_astlpc *astlpc)
700{
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930701 const uint16_t ver_min_be = htobe16(ASTLPC_VER_MIN);
702 const uint16_t ver_cur_be = htobe16(ASTLPC_VER_CUR);
Andrew Jeffery88412be2021-03-09 22:05:22 +1030703 uint16_t bmc_ver_min, bmc_ver_cur, negotiated;
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930704 struct mctp_lpcmap_hdr hdr;
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930705 uint8_t status;
706 int rc;
707
Andrew Jefferyf13cb972020-05-28 09:30:09 +0930708 rc = mctp_astlpc_kcs_read(astlpc, MCTP_ASTLPC_KCS_REG_STATUS, &status);
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930709 if (rc) {
710 mctp_prwarn("KCS status read failed");
711 return rc;
712 }
713
714 astlpc->kcs_status = status;
715
716 if (!(status & KCS_STATUS_BMC_READY))
717 return -EHOSTDOWN;
718
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930719 mctp_astlpc_lpc_read(astlpc, &hdr, 0, sizeof(hdr));
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930720
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930721 bmc_ver_min = be16toh(hdr.bmc_ver_min);
722 bmc_ver_cur = be16toh(hdr.bmc_ver_cur);
723
Andrew Jeffery88412be2021-03-09 22:05:22 +1030724 /* Calculate the expected value of negotiated_ver */
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600725 negotiated = mctp_astlpc_negotiate_version(
726 bmc_ver_min, bmc_ver_cur, ASTLPC_VER_MIN, ASTLPC_VER_CUR);
Andrew Jeffery88412be2021-03-09 22:05:22 +1030727 if (!negotiated) {
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930728 astlpc_prerr(astlpc, "Cannot negotiate with invalid versions");
729 return -EINVAL;
730 }
731
Andrew Jeffery88412be2021-03-09 22:05:22 +1030732 /* Assign protocol ops so we can calculate the packet buffer sizes */
733 assert(negotiated < ARRAY_SIZE(astlpc_protocol_version));
734 astlpc->proto = &astlpc_protocol_version[negotiated];
735
736 /* Negotiate packet buffers in v2 style if the BMC supports it */
737 if (negotiated >= 2) {
Andrew Jeffery3a540662020-05-26 19:55:30 +0930738 rc = mctp_astlpc_negotiate_layout_host(astlpc);
739 if (rc < 0)
740 return rc;
741 }
742
Andrew Jeffery88412be2021-03-09 22:05:22 +1030743 /* Advertise the host's supported protocol versions */
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930744 mctp_astlpc_lpc_write(astlpc, &ver_min_be,
745 offsetof(struct mctp_lpcmap_hdr, host_ver_min),
746 sizeof(ver_min_be));
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930747
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930748 mctp_astlpc_lpc_write(astlpc, &ver_cur_be,
749 offsetof(struct mctp_lpcmap_hdr, host_ver_cur),
750 sizeof(ver_cur_be));
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930751
752 /* Send channel init command */
Andrew Jefferyf13cb972020-05-28 09:30:09 +0930753 rc = mctp_astlpc_kcs_write(astlpc, MCTP_ASTLPC_KCS_REG_DATA, 0x0);
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930754 if (rc) {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930755 astlpc_prwarn(astlpc, "KCS write failed");
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930756 }
757
Andrew Jeffery88412be2021-03-09 22:05:22 +1030758 /*
759 * Configure the host so `astlpc->proto->version == 0` holds until we
760 * receive a subsequent status update from the BMC. Until then,
761 * `astlpc->proto->version == 0` indicates that we're yet to complete
762 * the channel initialisation handshake.
763 *
764 * When the BMC provides a status update with KCS_STATUS_CHANNEL_ACTIVE
765 * set we will assign the appropriate protocol ops struct in accordance
766 * with `negotiated_ver`.
767 */
768 astlpc->proto = &astlpc_protocol_version[ASTLPC_VER_BAD];
769
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930770 return rc;
771}
772
773static int mctp_binding_astlpc_start_host(struct mctp_binding *b)
774{
775 struct mctp_binding_astlpc *astlpc =
776 container_of(b, struct mctp_binding_astlpc, binding);
777
778 return mctp_astlpc_init_host(astlpc);
779}
780
781static bool __mctp_astlpc_kcs_ready(struct mctp_binding_astlpc *astlpc,
782 uint8_t status, bool is_write)
783{
784 bool is_bmc;
785 bool ready_state;
786 uint8_t flag;
787
788 is_bmc = (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_BMC);
789 flag = (is_bmc ^ is_write) ? KCS_STATUS_IBF : KCS_STATUS_OBF;
790 ready_state = is_write ? 0 : 1;
791
792 return !!(status & flag) == ready_state;
793}
794
795static inline bool
796mctp_astlpc_kcs_read_ready(struct mctp_binding_astlpc *astlpc, uint8_t status)
797{
798 return __mctp_astlpc_kcs_ready(astlpc, status, false);
799}
800
801static inline bool
802mctp_astlpc_kcs_write_ready(struct mctp_binding_astlpc *astlpc, uint8_t status)
803{
804 return __mctp_astlpc_kcs_ready(astlpc, status, true);
805}
806
Jeremy Kerr672c8852019-03-01 12:18:07 +0800807static int mctp_astlpc_kcs_send(struct mctp_binding_astlpc *astlpc,
Rashmica Gupta45d13322022-08-10 12:12:14 +1000808 enum mctp_astlpc_cmd data)
Jeremy Kerr672c8852019-03-01 12:18:07 +0800809{
810 uint8_t status;
811 int rc;
812
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600813 rc = mctp_astlpc_kcs_read(astlpc, MCTP_ASTLPC_KCS_REG_STATUS, &status);
Andrew Jefferyfe763e92022-08-05 23:16:17 +0930814 if (rc) {
815 astlpc_prwarn(astlpc, "KCS status read failed");
816 return -EIO;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800817 }
Andrew Jefferyfe763e92022-08-05 23:16:17 +0930818 if (!mctp_astlpc_kcs_write_ready(astlpc, status))
819 return -EBUSY;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800820
Andrew Jefferyf13cb972020-05-28 09:30:09 +0930821 rc = mctp_astlpc_kcs_write(astlpc, MCTP_ASTLPC_KCS_REG_DATA, data);
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530822 if (rc) {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930823 astlpc_prwarn(astlpc, "KCS data write failed");
Andrew Jefferyfe763e92022-08-05 23:16:17 +0930824 return -EIO;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800825 }
826
827 return 0;
828}
829
830static int mctp_binding_astlpc_tx(struct mctp_binding *b,
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600831 struct mctp_pktbuf *pkt)
Jeremy Kerr672c8852019-03-01 12:18:07 +0800832{
833 struct mctp_binding_astlpc *astlpc = binding_to_astlpc(b);
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930834 uint32_t len, len_be;
Andrew Jefferyedfe3832020-02-06 11:52:11 +1030835 struct mctp_hdr *hdr;
Andrew Jefferyfe763e92022-08-05 23:16:17 +0930836 int rc;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800837
Andrew Jefferyedfe3832020-02-06 11:52:11 +1030838 hdr = mctp_pktbuf_hdr(pkt);
Jeremy Kerr672c8852019-03-01 12:18:07 +0800839 len = mctp_pktbuf_size(pkt);
Andrew Jefferyedfe3832020-02-06 11:52:11 +1030840
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930841 astlpc_prdebug(astlpc,
842 "%s: Transmitting %" PRIu32
843 "-byte packet (%hhu, %hhu, 0x%hhx)",
844 __func__, len, hdr->src, hdr->dest, hdr->flags_seq_tag);
Andrew Jefferyedfe3832020-02-06 11:52:11 +1030845
Andrew Jeffery88412be2021-03-09 22:05:22 +1030846 if (len > astlpc->proto->body_size(astlpc->layout.tx.size)) {
Andrew Jeffery0721f582022-09-29 12:12:39 +0930847 astlpc_prwarn(astlpc, "invalid TX len %" PRIu32 ": %" PRIu32,
848 len,
849 astlpc->proto->body_size(astlpc->layout.tx.size));
850 return -EMSGSIZE;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800851 }
852
Andrew Jefferyfe763e92022-08-05 23:16:17 +0930853 mctp_binding_set_tx_enabled(b, false);
854
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930855 len_be = htobe32(len);
856 mctp_astlpc_lpc_write(astlpc, &len_be, astlpc->layout.tx.offset,
857 sizeof(len_be));
Andrew Jefferyeba19a32021-03-09 23:09:40 +1030858
859 astlpc->proto->pktbuf_protect(pkt);
860 len = mctp_pktbuf_size(pkt);
861
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930862 mctp_astlpc_lpc_write(astlpc, hdr, astlpc->layout.tx.offset + 4, len);
Jeremy Kerr672c8852019-03-01 12:18:07 +0800863
Andrew Jefferyfe763e92022-08-05 23:16:17 +0930864 astlpc->layout.tx.state = buffer_state_prepared;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800865
Rashmica Gupta45d13322022-08-10 12:12:14 +1000866 rc = mctp_astlpc_kcs_send(astlpc, cmd_tx_begin);
Andrew Jefferyfe763e92022-08-05 23:16:17 +0930867 if (!rc)
868 astlpc->layout.tx.state = buffer_state_released;
Andrew Jefferyeba19a32021-03-09 23:09:40 +1030869
Andrew Jefferyfe763e92022-08-05 23:16:17 +0930870 return rc == -EBUSY ? 0 : rc;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800871}
872
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800873/* Update binding pkt_size and reallocate tx_storage */
874static int mctp_astlpc_set_pkt_size(struct mctp_binding_astlpc *astlpc,
875 size_t pkt_size)
876{
877 size_t body = MCTP_BODY_SIZE(pkt_size);
878 body += astlpc->binding.pkt_header + astlpc->binding.pkt_trailer;
879 size_t pktbuf_size = MCTP_PKTBUF_SIZE(body);
880 /* Reallocate TX storage */
881 if (astlpc->binding.tx_storage) {
882 __mctp_free(astlpc->binding.tx_storage);
883 }
884 astlpc->binding.tx_storage = __mctp_alloc(pktbuf_size);
885 if (!astlpc->binding.tx_storage) {
886 return -ENOMEM;
887 }
888
889 astlpc->binding.pkt_size = pkt_size;
890 return 0;
891}
892
Andrew Jeffery3a540662020-05-26 19:55:30 +0930893static uint32_t mctp_astlpc_calculate_mtu(struct mctp_binding_astlpc *astlpc,
894 struct mctp_astlpc_layout *layout)
895{
Andrew Jeffery88412be2021-03-09 22:05:22 +1030896 uint32_t low, high, limit, rpkt;
Andrew Jeffery3a540662020-05-26 19:55:30 +0930897
898 /* Derive the largest MTU the BMC _can_ support */
899 low = MIN(astlpc->layout.rx.offset, astlpc->layout.tx.offset);
900 high = MAX(astlpc->layout.rx.offset, astlpc->layout.tx.offset);
901 limit = high - low;
902
Andrew Jefferya9368982020-06-09 13:07:39 +0930903 /* Determine the largest MTU the BMC _wants_ to support */
904 if (astlpc->requested_mtu) {
Andrew Jeffery88412be2021-03-09 22:05:22 +1030905 uint32_t rmtu = astlpc->requested_mtu;
Andrew Jefferya9368982020-06-09 13:07:39 +0930906
Andrew Jeffery88412be2021-03-09 22:05:22 +1030907 rpkt = astlpc->proto->packet_size(MCTP_PACKET_SIZE(rmtu));
908 limit = MIN(limit, rpkt);
Andrew Jefferya9368982020-06-09 13:07:39 +0930909 }
Andrew Jeffery3a540662020-05-26 19:55:30 +0930910
911 /* Determine the accepted MTU, applied both directions by convention */
Andrew Jeffery88412be2021-03-09 22:05:22 +1030912 rpkt = MIN(limit, layout->tx.size);
913 return MCTP_BODY_SIZE(astlpc->proto->body_size(rpkt));
Andrew Jeffery3a540662020-05-26 19:55:30 +0930914}
915
Andrew Jeffery88412be2021-03-09 22:05:22 +1030916static int mctp_astlpc_negotiate_layout_bmc(struct mctp_binding_astlpc *astlpc)
Andrew Jeffery3a540662020-05-26 19:55:30 +0930917{
918 struct mctp_astlpc_layout proposed, pending;
919 uint32_t sz, mtu;
920 int rc;
921
Andrew Jeffery88412be2021-03-09 22:05:22 +1030922 /* Do we have a valid protocol version? */
923 if (!astlpc->proto->version)
924 return -EINVAL;
925
Andrew Jeffery3a540662020-05-26 19:55:30 +0930926 /* Extract the host's proposed layout */
927 rc = mctp_astlpc_layout_read(astlpc, &proposed);
928 if (rc < 0)
929 return rc;
930
Andrew Jeffery88412be2021-03-09 22:05:22 +1030931 /* Do we have a reasonable layout? */
932 if (!mctp_astlpc_layout_validate(astlpc, &proposed))
Andrew Jeffery3a540662020-05-26 19:55:30 +0930933 return -EINVAL;
934
935 /* Negotiate the MTU */
936 mtu = mctp_astlpc_calculate_mtu(astlpc, &proposed);
Andrew Jeffery88412be2021-03-09 22:05:22 +1030937 sz = astlpc->proto->packet_size(MCTP_PACKET_SIZE(mtu));
Andrew Jeffery3a540662020-05-26 19:55:30 +0930938
939 /*
940 * Use symmetric MTUs by convention and to pass constraints in rx/tx
941 * functions
942 */
943 pending = astlpc->layout;
944 pending.tx.size = sz;
945 pending.rx.size = sz;
946
Andrew Jeffery88412be2021-03-09 22:05:22 +1030947 if (mctp_astlpc_layout_validate(astlpc, &pending)) {
Andrew Jeffery3a540662020-05-26 19:55:30 +0930948 /* We found a sensible Rx MTU, so honour it */
949 astlpc->layout = pending;
950
951 /* Enforce the negotiated MTU */
952 rc = mctp_astlpc_layout_write(astlpc, &astlpc->layout);
953 if (rc < 0)
954 return rc;
955
956 astlpc_prinfo(astlpc, "Negotiated an MTU of %" PRIu32 " bytes",
957 mtu);
958 } else {
959 astlpc_prwarn(astlpc, "MTU negotiation failed");
960 return -EINVAL;
961 }
962
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800963 if (astlpc->proto->version >= 2) {
964 rc = mctp_astlpc_set_pkt_size(astlpc, MCTP_PACKET_SIZE(mtu));
965 if (rc) {
966 astlpc_prwarn(astlpc, "Allocation error");
967 return rc;
968 }
969 }
Andrew Jeffery3a540662020-05-26 19:55:30 +0930970
971 return 0;
972}
973
Jeremy Kerr672c8852019-03-01 12:18:07 +0800974static void mctp_astlpc_init_channel(struct mctp_binding_astlpc *astlpc)
975{
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930976 uint16_t negotiated, negotiated_be;
977 struct mctp_lpcmap_hdr hdr;
978 uint8_t status;
Andrew Jeffery3a540662020-05-26 19:55:30 +0930979 int rc;
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930980
981 mctp_astlpc_lpc_read(astlpc, &hdr, 0, sizeof(hdr));
982
983 /* Version negotiation */
Andrew Jeffery7f7fdc12023-05-12 15:56:47 +0930984 negotiated = mctp_astlpc_negotiate_version(ASTLPC_VER_MIN,
985 ASTLPC_VER_CUR,
986 be16toh(hdr.host_ver_min),
987 be16toh(hdr.host_ver_cur));
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930988
Andrew Jeffery88412be2021-03-09 22:05:22 +1030989 /* MTU negotiation requires knowing which protocol we'll use */
990 assert(negotiated < ARRAY_SIZE(astlpc_protocol_version));
991 astlpc->proto = &astlpc_protocol_version[negotiated];
992
Andrew Jeffery3a540662020-05-26 19:55:30 +0930993 /* Host Rx MTU negotiation: Failure terminates channel init */
Andrew Jeffery88412be2021-03-09 22:05:22 +1030994 rc = mctp_astlpc_negotiate_layout_bmc(astlpc);
Andrew Jeffery3a540662020-05-26 19:55:30 +0930995 if (rc < 0)
996 negotiated = ASTLPC_VER_BAD;
997
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930998 /* Populate the negotiated version */
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930999 negotiated_be = htobe16(negotiated);
1000 mctp_astlpc_lpc_write(astlpc, &negotiated_be,
Andrew Jeffery55fb90b2020-05-12 13:54:37 +09301001 offsetof(struct mctp_lpcmap_hdr, negotiated_ver),
Andrew Jeffery4e8264b2020-05-23 20:34:33 +09301002 sizeof(negotiated_be));
Andrew Jeffery55fb90b2020-05-12 13:54:37 +09301003
Andrew Jefferyfe763e92022-08-05 23:16:17 +09301004 /* Track buffer ownership */
1005 astlpc->layout.tx.state = buffer_state_acquired;
1006 astlpc->layout.rx.state = buffer_state_released;
1007
Andrew Jeffery4e8264b2020-05-23 20:34:33 +09301008 /* Finalise the configuration */
1009 status = KCS_STATUS_BMC_READY | KCS_STATUS_OBF;
1010 if (negotiated > 0) {
1011 astlpc_prinfo(astlpc, "Negotiated binding version %" PRIu16,
1012 negotiated);
1013 status |= KCS_STATUS_CHANNEL_ACTIVE;
1014 } else {
Andrew Jeffery88412be2021-03-09 22:05:22 +10301015 astlpc_prerr(astlpc, "Failed to initialise channel");
Andrew Jeffery4e8264b2020-05-23 20:34:33 +09301016 }
Jeremy Kerr672c8852019-03-01 12:18:07 +08001017
Andrew Jeffery4e8264b2020-05-23 20:34:33 +09301018 mctp_astlpc_kcs_set_status(astlpc, status);
1019
1020 mctp_binding_set_tx_enabled(&astlpc->binding,
1021 status & KCS_STATUS_CHANNEL_ACTIVE);
Jeremy Kerr672c8852019-03-01 12:18:07 +08001022}
1023
1024static void mctp_astlpc_rx_start(struct mctp_binding_astlpc *astlpc)
1025{
1026 struct mctp_pktbuf *pkt;
Rashmica Guptaf447b4f2022-10-11 17:44:31 +11001027 struct mctp_hdr *hdr;
Andrew Jefferyeba19a32021-03-09 23:09:40 +10301028 uint32_t body, packet;
Jeremy Kerr672c8852019-03-01 12:18:07 +08001029
Andrew Jefferyeba19a32021-03-09 23:09:40 +10301030 mctp_astlpc_lpc_read(astlpc, &body, astlpc->layout.rx.offset,
1031 sizeof(body));
1032 body = be32toh(body);
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301033
Andrew Jefferyeba19a32021-03-09 23:09:40 +10301034 if (body > astlpc->proto->body_size(astlpc->layout.rx.size)) {
1035 astlpc_prwarn(astlpc, "invalid RX len 0x%x", body);
Jeremy Kerr672c8852019-03-01 12:18:07 +08001036 return;
1037 }
1038
Andrew Jeffery0f05b6c2022-11-15 11:37:50 +10301039 if ((size_t)body > astlpc->binding.pkt_size) {
Andrew Jefferyeba19a32021-03-09 23:09:40 +10301040 astlpc_prwarn(astlpc, "invalid RX len 0x%x", body);
Jeremy Kerr672c8852019-03-01 12:18:07 +08001041 return;
1042 }
1043
Andrew Jefferyeba19a32021-03-09 23:09:40 +10301044 /* Eliminate the medium-specific header that we just read */
1045 packet = astlpc->proto->packet_size(body) - 4;
1046 pkt = mctp_pktbuf_alloc(&astlpc->binding, packet);
Christian Geddesae59f4f2021-09-01 09:54:25 -05001047 if (!pkt) {
Patrick Williamsa721c2d2022-12-04 14:30:26 -06001048 astlpc_prwarn(astlpc, "unable to allocate pktbuf len 0x%x",
1049 packet);
Christian Geddesae59f4f2021-09-01 09:54:25 -05001050 return;
1051 }
Jeremy Kerr672c8852019-03-01 12:18:07 +08001052
Andrew Jefferyeba19a32021-03-09 23:09:40 +10301053 /*
1054 * Read payload and medium-specific trailer from immediately after the
1055 * medium-specific header.
1056 */
Andrew Jeffery55fb90b2020-05-12 13:54:37 +09301057 mctp_astlpc_lpc_read(astlpc, mctp_pktbuf_hdr(pkt),
Andrew Jefferyeba19a32021-03-09 23:09:40 +10301058 astlpc->layout.rx.offset + 4, packet);
Jeremy Kerr672c8852019-03-01 12:18:07 +08001059
Andrew Jefferyfe763e92022-08-05 23:16:17 +09301060 astlpc->layout.rx.state = buffer_state_prepared;
1061
Christian Geddesae59f4f2021-09-01 09:54:25 -05001062 /* Inform the other side of the MCTP interface that we have read
1063 * the packet off the bus before handling the contents of the packet.
1064 */
Rashmica Gupta45d13322022-08-10 12:12:14 +10001065 if (!mctp_astlpc_kcs_send(astlpc, cmd_rx_complete))
Andrew Jefferyfe763e92022-08-05 23:16:17 +09301066 astlpc->layout.rx.state = buffer_state_released;
Christian Geddesae59f4f2021-09-01 09:54:25 -05001067
Rashmica Guptaf447b4f2022-10-11 17:44:31 +11001068 hdr = mctp_pktbuf_hdr(pkt);
1069 if (hdr->ver != 1) {
1070 mctp_pktbuf_free(pkt);
1071 astlpc_prdebug(astlpc, "Dropped packet with invalid version");
1072 return;
1073 }
1074
Andrew Jefferyeba19a32021-03-09 23:09:40 +10301075 /*
1076 * v3 will validate the CRC32 in the medium-specific trailer and adjust
1077 * the packet size accordingly. On older protocols validation is a no-op
1078 * that always returns true.
1079 */
1080 if (astlpc->proto->pktbuf_validate(pkt)) {
1081 mctp_bus_rx(&astlpc->binding, pkt);
1082 } else {
1083 /* TODO: Drop any associated assembly */
Andrew Jefferyeba19a32021-03-09 23:09:40 +10301084 astlpc_prdebug(astlpc, "Dropped corrupt packet");
1085 }
Matt Johnston4a09e1d2024-09-13 14:55:58 +08001086 mctp_pktbuf_free(pkt);
Jeremy Kerr672c8852019-03-01 12:18:07 +08001087}
1088
1089static void mctp_astlpc_tx_complete(struct mctp_binding_astlpc *astlpc)
1090{
Andrew Jefferyfe763e92022-08-05 23:16:17 +09301091 astlpc->layout.tx.state = buffer_state_acquired;
Jeremy Kerr672c8852019-03-01 12:18:07 +08001092 mctp_binding_set_tx_enabled(&astlpc->binding, true);
1093}
1094
Andrew Jeffery4e8264b2020-05-23 20:34:33 +09301095static int mctp_astlpc_finalise_channel(struct mctp_binding_astlpc *astlpc)
1096{
Andrew Jeffery3a540662020-05-26 19:55:30 +09301097 struct mctp_astlpc_layout layout;
Andrew Jeffery4e8264b2020-05-23 20:34:33 +09301098 uint16_t negotiated;
1099 int rc;
1100
1101 rc = mctp_astlpc_lpc_read(astlpc, &negotiated,
1102 offsetof(struct mctp_lpcmap_hdr,
1103 negotiated_ver),
1104 sizeof(negotiated));
1105 if (rc < 0)
1106 return rc;
1107
1108 negotiated = be16toh(negotiated);
Andrew Jeffery88412be2021-03-09 22:05:22 +10301109 astlpc_prerr(astlpc, "Version negotiation got: %u", negotiated);
Andrew Jeffery4e8264b2020-05-23 20:34:33 +09301110
1111 if (negotiated == ASTLPC_VER_BAD || negotiated < ASTLPC_VER_MIN ||
1112 negotiated > ASTLPC_VER_CUR) {
1113 astlpc_prerr(astlpc, "Failed to negotiate version, got: %u\n",
1114 negotiated);
1115 return -EINVAL;
1116 }
1117
Andrew Jeffery88412be2021-03-09 22:05:22 +10301118 assert(negotiated < ARRAY_SIZE(astlpc_protocol_version));
1119 astlpc->proto = &astlpc_protocol_version[negotiated];
Andrew Jeffery4e8264b2020-05-23 20:34:33 +09301120
Andrew Jeffery3a540662020-05-26 19:55:30 +09301121 rc = mctp_astlpc_layout_read(astlpc, &layout);
1122 if (rc < 0)
1123 return rc;
1124
Andrew Jeffery88412be2021-03-09 22:05:22 +10301125 if (!mctp_astlpc_layout_validate(astlpc, &layout)) {
Andrew Jeffery3a540662020-05-26 19:55:30 +09301126 mctp_prerr("BMC proposed invalid buffer parameters");
1127 return -EINVAL;
1128 }
1129
1130 astlpc->layout = layout;
1131
1132 if (negotiated >= 2)
1133 astlpc->binding.pkt_size =
Andrew Jeffery88412be2021-03-09 22:05:22 +10301134 astlpc->proto->body_size(astlpc->layout.tx.size);
Andrew Jeffery3a540662020-05-26 19:55:30 +09301135
Andrew Jefferyfe763e92022-08-05 23:16:17 +09301136 /* Track buffer ownership */
1137 astlpc->layout.tx.state = buffer_state_acquired;
1138 astlpc->layout.rx.state = buffer_state_released;
1139
Andrew Jeffery4e8264b2020-05-23 20:34:33 +09301140 return 0;
1141}
1142
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301143static int mctp_astlpc_update_channel(struct mctp_binding_astlpc *astlpc,
1144 uint8_t status)
1145{
1146 uint8_t updated;
1147 int rc = 0;
1148
1149 assert(astlpc->mode == MCTP_BINDING_ASTLPC_MODE_HOST);
1150
1151 updated = astlpc->kcs_status ^ status;
1152
Andrew Jefferyd0f5da02020-05-28 09:12:55 +09301153 astlpc_prdebug(astlpc, "%s: status: 0x%x, update: 0x%x", __func__,
1154 status, updated);
1155
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301156 if (updated & KCS_STATUS_BMC_READY) {
Andrew Jefferyd0f5da02020-05-28 09:12:55 +09301157 if (status & KCS_STATUS_BMC_READY) {
1158 astlpc->kcs_status = status;
1159 return astlpc->binding.start(&astlpc->binding);
1160 } else {
Andrew Jefferyfe763e92022-08-05 23:16:17 +09301161 /* Shut down the channel */
1162 astlpc->layout.rx.state = buffer_state_idle;
1163 astlpc->layout.tx.state = buffer_state_idle;
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301164 mctp_binding_set_tx_enabled(&astlpc->binding, false);
Andrew Jefferyd0f5da02020-05-28 09:12:55 +09301165 }
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301166 }
1167
Andrew Jeffery88412be2021-03-09 22:05:22 +10301168 if (astlpc->proto->version == 0 ||
Patrick Williamsa721c2d2022-12-04 14:30:26 -06001169 updated & KCS_STATUS_CHANNEL_ACTIVE) {
Andrew Jeffery4e8264b2020-05-23 20:34:33 +09301170 bool enable;
1171
Andrew Jefferyfe763e92022-08-05 23:16:17 +09301172 astlpc->layout.rx.state = buffer_state_idle;
1173 astlpc->layout.tx.state = buffer_state_idle;
Andrew Jeffery4e8264b2020-05-23 20:34:33 +09301174 rc = mctp_astlpc_finalise_channel(astlpc);
1175 enable = (status & KCS_STATUS_CHANNEL_ACTIVE) && rc == 0;
Andrew Jeffery4e8264b2020-05-23 20:34:33 +09301176 mctp_binding_set_tx_enabled(&astlpc->binding, enable);
1177 }
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301178
1179 astlpc->kcs_status = status;
1180
1181 return rc;
1182}
1183
Frederic Barrat4df106d2023-05-16 17:45:42 +02001184bool mctp_astlpc_tx_done(struct mctp_binding_astlpc *astlpc)
1185{
1186 return astlpc->layout.tx.state == buffer_state_acquired;
1187}
1188
Jeremy Kerr672c8852019-03-01 12:18:07 +08001189int mctp_astlpc_poll(struct mctp_binding_astlpc *astlpc)
1190{
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301191 uint8_t status, data;
Jeremy Kerr672c8852019-03-01 12:18:07 +08001192 int rc;
1193
Andrew Jefferyfe763e92022-08-05 23:16:17 +09301194 if (astlpc->layout.rx.state == buffer_state_prepared)
Rashmica Gupta45d13322022-08-10 12:12:14 +10001195 if (!mctp_astlpc_kcs_send(astlpc, cmd_rx_complete))
Andrew Jefferyfe763e92022-08-05 23:16:17 +09301196 astlpc->layout.rx.state = buffer_state_released;
1197
1198 if (astlpc->layout.tx.state == buffer_state_prepared)
Rashmica Gupta45d13322022-08-10 12:12:14 +10001199 if (!mctp_astlpc_kcs_send(astlpc, cmd_tx_begin))
Andrew Jefferyfe763e92022-08-05 23:16:17 +09301200 astlpc->layout.tx.state = buffer_state_released;
1201
Andrew Jefferyf13cb972020-05-28 09:30:09 +09301202 rc = mctp_astlpc_kcs_read(astlpc, MCTP_ASTLPC_KCS_REG_STATUS, &status);
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301203 if (rc) {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +09301204 astlpc_prwarn(astlpc, "KCS read error");
Jeremy Kerr672c8852019-03-01 12:18:07 +08001205 return -1;
Jeremy Kerr672c8852019-03-01 12:18:07 +08001206 }
1207
Andrew Jeffery9101a2a2020-05-22 16:08:03 +09301208 astlpc_prdebug(astlpc, "%s: status: 0x%hhx", __func__, status);
Andrew Jefferyedfe3832020-02-06 11:52:11 +10301209
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301210 if (!mctp_astlpc_kcs_read_ready(astlpc, status))
Jeremy Kerr672c8852019-03-01 12:18:07 +08001211 return 0;
1212
Andrew Jefferyf13cb972020-05-28 09:30:09 +09301213 rc = mctp_astlpc_kcs_read(astlpc, MCTP_ASTLPC_KCS_REG_DATA, &data);
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301214 if (rc) {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +09301215 astlpc_prwarn(astlpc, "KCS data read error");
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301216 return -1;
1217 }
1218
Andrew Jeffery9101a2a2020-05-22 16:08:03 +09301219 astlpc_prdebug(astlpc, "%s: data: 0x%hhx", __func__, data);
Andrew Jefferyedfe3832020-02-06 11:52:11 +10301220
Rashmica Gupta45d13322022-08-10 12:12:14 +10001221 if (!astlpc->proto->version &&
1222 !(data == cmd_initialise || data == cmd_dummy_value)) {
Andrew Jefferyafcb7012021-01-28 17:00:36 +10301223 astlpc_prwarn(astlpc, "Invalid message for binding state: 0x%x",
1224 data);
1225 return 0;
1226 }
1227
Jeremy Kerr672c8852019-03-01 12:18:07 +08001228 switch (data) {
Rashmica Gupta45d13322022-08-10 12:12:14 +10001229 case cmd_initialise:
Jeremy Kerr672c8852019-03-01 12:18:07 +08001230 mctp_astlpc_init_channel(astlpc);
1231 break;
Rashmica Gupta45d13322022-08-10 12:12:14 +10001232 case cmd_tx_begin:
Andrew Jefferyfe763e92022-08-05 23:16:17 +09301233 if (astlpc->layout.rx.state != buffer_state_released) {
Patrick Williamsa721c2d2022-12-04 14:30:26 -06001234 astlpc_prerr(
1235 astlpc,
1236 "Protocol error: Invalid Rx buffer state for event %d: %d\n",
1237 data, astlpc->layout.rx.state);
Andrew Jefferyfe763e92022-08-05 23:16:17 +09301238 return 0;
1239 }
Jeremy Kerr672c8852019-03-01 12:18:07 +08001240 mctp_astlpc_rx_start(astlpc);
1241 break;
Rashmica Gupta45d13322022-08-10 12:12:14 +10001242 case cmd_rx_complete:
Andrew Jefferyfe763e92022-08-05 23:16:17 +09301243 if (astlpc->layout.tx.state != buffer_state_released) {
Patrick Williamsa721c2d2022-12-04 14:30:26 -06001244 astlpc_prerr(
1245 astlpc,
1246 "Protocol error: Invalid Tx buffer state for event %d: %d\n",
1247 data, astlpc->layout.tx.state);
Andrew Jefferyfe763e92022-08-05 23:16:17 +09301248 return 0;
1249 }
Jeremy Kerr672c8852019-03-01 12:18:07 +08001250 mctp_astlpc_tx_complete(astlpc);
1251 break;
Rashmica Gupta45d13322022-08-10 12:12:14 +10001252 case cmd_dummy_value:
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301253 /* No responsibilities for the BMC on 0xff */
Andrew Jefferyd0f5da02020-05-28 09:12:55 +09301254 if (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_HOST) {
1255 rc = mctp_astlpc_update_channel(astlpc, status);
1256 if (rc < 0)
1257 return rc;
1258 }
1259 break;
Jeremy Kerr672c8852019-03-01 12:18:07 +08001260 default:
Andrew Jeffery9101a2a2020-05-22 16:08:03 +09301261 astlpc_prwarn(astlpc, "unknown message 0x%x", data);
Jeremy Kerr672c8852019-03-01 12:18:07 +08001262 }
Andrew Jefferyd0f5da02020-05-28 09:12:55 +09301263
1264 /* Handle silent loss of bmc-ready */
1265 if (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_HOST) {
Rashmica Gupta45d13322022-08-10 12:12:14 +10001266 if (!(status & KCS_STATUS_BMC_READY && data == cmd_dummy_value))
Andrew Jefferyd0f5da02020-05-28 09:12:55 +09301267 return mctp_astlpc_update_channel(astlpc, status);
1268 }
1269
1270 return rc;
Jeremy Kerr672c8852019-03-01 12:18:07 +08001271}
1272
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301273/* allocate and basic initialisation */
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301274static struct mctp_binding_astlpc *__mctp_astlpc_init(uint8_t mode,
1275 uint32_t mtu)
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301276{
1277 struct mctp_binding_astlpc *astlpc;
1278
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301279 assert((mode == MCTP_BINDING_ASTLPC_MODE_BMC) ||
1280 (mode == MCTP_BINDING_ASTLPC_MODE_HOST));
1281
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301282 astlpc = __mctp_alloc(sizeof(*astlpc));
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301283 if (!astlpc)
1284 return NULL;
1285
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301286 memset(astlpc, 0, sizeof(*astlpc));
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301287 astlpc->mode = mode;
1288 astlpc->lpc_map = NULL;
Andrew Jefferyfe763e92022-08-05 23:16:17 +09301289 astlpc->layout.rx.state = buffer_state_idle;
1290 astlpc->layout.tx.state = buffer_state_idle;
Andrew Jefferya9368982020-06-09 13:07:39 +09301291 astlpc->requested_mtu = mtu;
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301292 astlpc->binding.name = "astlpc";
1293 astlpc->binding.version = 1;
Andrew Jefferyeba19a32021-03-09 23:09:40 +10301294 astlpc->binding.pkt_header = 4;
1295 astlpc->binding.pkt_trailer = 4;
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301296 astlpc->binding.tx = mctp_binding_astlpc_tx;
1297 if (mode == MCTP_BINDING_ASTLPC_MODE_BMC)
1298 astlpc->binding.start = mctp_binding_astlpc_start_bmc;
1299 else if (mode == MCTP_BINDING_ASTLPC_MODE_HOST)
1300 astlpc->binding.start = mctp_binding_astlpc_start_host;
1301 else {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +09301302 astlpc_prerr(astlpc, "%s: Invalid mode: %d\n", __func__, mode);
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301303 __mctp_free(astlpc);
1304 return NULL;
1305 }
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301306
Matt Johnston4a09e1d2024-09-13 14:55:58 +08001307 if (mctp_astlpc_set_pkt_size(
1308 astlpc,
1309 MCTP_PACKET_SIZE(mtu > MCTP_BTU ? mtu : MCTP_BTU)) != 0) {
1310 astlpc_prerr(astlpc, "%s: Allocation error", __func__);
1311 __mctp_free(astlpc);
1312 return NULL;
1313 }
1314
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301315 return astlpc;
1316}
1317
Jeremy Kerr3b36d172019-09-04 11:56:09 +08001318struct mctp_binding *mctp_binding_astlpc_core(struct mctp_binding_astlpc *b)
1319{
1320 return &b->binding;
1321}
1322
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301323struct mctp_binding_astlpc *
1324mctp_astlpc_init(uint8_t mode, uint32_t mtu, void *lpc_map,
1325 const struct mctp_binding_astlpc_ops *ops, void *ops_data)
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301326{
1327 struct mctp_binding_astlpc *astlpc;
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301328
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301329 if (!(mode == MCTP_BINDING_ASTLPC_MODE_BMC ||
1330 mode == MCTP_BINDING_ASTLPC_MODE_HOST)) {
1331 mctp_prerr("Unknown binding mode: %u", mode);
1332 return NULL;
1333 }
1334
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301335 astlpc = __mctp_astlpc_init(mode, mtu);
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301336 if (!astlpc)
1337 return NULL;
1338
1339 memcpy(&astlpc->ops, ops, sizeof(astlpc->ops));
1340 astlpc->ops_data = ops_data;
1341 astlpc->lpc_map = lpc_map;
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301342 astlpc->mode = mode;
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301343
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301344 return astlpc;
1345}
1346
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301347struct mctp_binding_astlpc *
1348mctp_astlpc_init_ops(const struct mctp_binding_astlpc_ops *ops, void *ops_data,
1349 void *lpc_map)
1350{
1351 return mctp_astlpc_init(MCTP_BINDING_ASTLPC_MODE_BMC, MCTP_BTU, lpc_map,
1352 ops, ops_data);
1353}
1354
Andrew Jeffery4663f672020-03-10 23:36:24 +10301355void mctp_astlpc_destroy(struct mctp_binding_astlpc *astlpc)
1356{
Andrew Jefferyd0f5da02020-05-28 09:12:55 +09301357 /* Clear channel-active and bmc-ready */
1358 if (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_BMC)
Andrew Jeffery7c4509a2021-08-26 14:56:16 +09301359 mctp_astlpc_kcs_set_status(astlpc, 0);
Matt Johnston4a09e1d2024-09-13 14:55:58 +08001360 __mctp_free(astlpc->binding.tx_storage);
Andrew Jeffery4663f672020-03-10 23:36:24 +10301361 __mctp_free(astlpc);
1362}
1363
Jeremy Kerrb214c642019-11-27 11:34:00 +08001364#ifdef MCTP_HAVE_FILEIO
Andrew Jeffery55fb90b2020-05-12 13:54:37 +09301365
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301366static int mctp_astlpc_init_fileio_lpc(struct mctp_binding_astlpc *astlpc)
Jeremy Kerr672c8852019-03-01 12:18:07 +08001367{
1368 struct aspeed_lpc_ctrl_mapping map = {
1369 .window_type = ASPEED_LPC_CTRL_WINDOW_MEMORY,
1370 .window_id = 0, /* There's only one */
1371 .flags = 0,
1372 .addr = 0,
1373 .offset = 0,
1374 .size = 0
1375 };
Andrew Jeffery979c6a12020-05-23 20:04:49 +09301376 void *lpc_map_base;
Jeremy Kerr672c8852019-03-01 12:18:07 +08001377 int fd, rc;
1378
1379 fd = open(lpc_path, O_RDWR | O_SYNC);
1380 if (fd < 0) {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +09301381 astlpc_prwarn(astlpc, "LPC open (%s) failed", lpc_path);
Jeremy Kerr672c8852019-03-01 12:18:07 +08001382 return -1;
1383 }
1384
1385 rc = ioctl(fd, ASPEED_LPC_CTRL_IOCTL_GET_SIZE, &map);
1386 if (rc) {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +09301387 astlpc_prwarn(astlpc, "LPC GET_SIZE failed");
Jeremy Kerr672c8852019-03-01 12:18:07 +08001388 close(fd);
1389 return -1;
1390 }
1391
Andrew Jefferyc1d5c542021-10-27 15:25:48 +10301392 /*
1393 * 🚨🚨🚨
1394 *
1395 * Decouple ourselves from hiomapd[1] (another user of the FW2AHB) by
1396 * mapping the FW2AHB to the reserved memory here as well.
1397 *
1398 * It's not possible to use the MCTP ASTLPC binding on machines that
1399 * need the FW2AHB bridge mapped anywhere except to the reserved memory
1400 * (e.g. the host SPI NOR).
1401 *
1402 * [1] https://github.com/openbmc/hiomapd/
1403 *
1404 * 🚨🚨🚨
1405 *
1406 * The following calculation must align with what's going on in
1407 * hiomapd's lpc.c so as not to disrupt its behaviour:
1408 *
1409 * https://github.com/openbmc/hiomapd/blob/5ff50e3cbd7702aefc185264e4adfb9952040575/lpc.c#L68
1410 *
1411 * 🚨🚨🚨
1412 */
1413
1414 /* Map the reserved memory at the top of the 28-bit LPC firmware address space */
1415 map.addr = 0x0FFFFFFF & -map.size;
Patrick Williamsa721c2d2022-12-04 14:30:26 -06001416 astlpc_prinfo(
1417 astlpc,
1418 "Configuring FW2AHB to map reserved memory at 0x%08x for 0x%x in the LPC FW cycle address-space",
1419 map.addr, map.size);
Andrew Jefferyc1d5c542021-10-27 15:25:48 +10301420
1421 rc = ioctl(fd, ASPEED_LPC_CTRL_IOCTL_MAP, &map);
1422 if (rc) {
Patrick Williamsa721c2d2022-12-04 14:30:26 -06001423 astlpc_prwarn(astlpc,
1424 "Failed to map FW2AHB to reserved memory");
Andrew Jefferyc1d5c542021-10-27 15:25:48 +10301425 close(fd);
1426 return -1;
1427 }
1428
1429 /* Map the reserved memory into our address space */
Andrew Jeffery979c6a12020-05-23 20:04:49 +09301430 lpc_map_base =
1431 mmap(NULL, map.size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
1432 if (lpc_map_base == MAP_FAILED) {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +09301433 astlpc_prwarn(astlpc, "LPC mmap failed");
Jeremy Kerr672c8852019-03-01 12:18:07 +08001434 rc = -1;
1435 } else {
Matt Johnston3ef47782024-12-11 15:19:06 +08001436 astlpc->lpc_map =
1437 (uint8_t *)lpc_map_base + map.size - LPC_WIN_SIZE;
Jeremy Kerr672c8852019-03-01 12:18:07 +08001438 }
1439
1440 close(fd);
1441
1442 return rc;
1443}
1444
Konstantin Aladyshev400766f2023-09-07 15:57:25 +03001445static int mctp_astlpc_init_fileio_kcs(struct mctp_binding_astlpc *astlpc,
1446 const char *kcs_path)
Jeremy Kerr672c8852019-03-01 12:18:07 +08001447{
1448 astlpc->kcs_fd = open(kcs_path, O_RDWR);
1449 if (astlpc->kcs_fd < 0)
1450 return -1;
1451
1452 return 0;
1453}
1454
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301455static int __mctp_astlpc_fileio_kcs_read(void *arg,
Patrick Williamsa721c2d2022-12-04 14:30:26 -06001456 enum mctp_binding_astlpc_kcs_reg reg,
1457 uint8_t *val)
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301458{
1459 struct mctp_binding_astlpc *astlpc = arg;
1460 off_t offset = reg;
1461 int rc;
1462
1463 rc = pread(astlpc->kcs_fd, val, 1, offset);
1464
1465 return rc == 1 ? 0 : -1;
1466}
1467
1468static int __mctp_astlpc_fileio_kcs_write(void *arg,
Patrick Williamsa721c2d2022-12-04 14:30:26 -06001469 enum mctp_binding_astlpc_kcs_reg reg,
1470 uint8_t val)
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301471{
1472 struct mctp_binding_astlpc *astlpc = arg;
1473 off_t offset = reg;
1474 int rc;
1475
1476 rc = pwrite(astlpc->kcs_fd, &val, 1, offset);
1477
1478 return rc == 1 ? 0 : -1;
1479}
1480
Andrew Jeffery1111c6a2022-07-25 20:44:39 +09301481int mctp_astlpc_init_pollfd(struct mctp_binding_astlpc *astlpc,
1482 struct pollfd *pollfd)
1483{
Andrew Jefferyfe763e92022-08-05 23:16:17 +09301484 bool release;
1485
Andrew Jeffery1111c6a2022-07-25 20:44:39 +09301486 pollfd->fd = astlpc->kcs_fd;
Andrew Jefferyfe763e92022-08-05 23:16:17 +09301487 pollfd->events = 0;
1488
1489 release = astlpc->layout.rx.state == buffer_state_prepared ||
Patrick Williamsa721c2d2022-12-04 14:30:26 -06001490 astlpc->layout.tx.state == buffer_state_prepared;
Andrew Jefferyfe763e92022-08-05 23:16:17 +09301491
1492 pollfd->events = release ? POLLOUT : POLLIN;
Andrew Jeffery1111c6a2022-07-25 20:44:39 +09301493
1494 return 0;
1495}
1496
Konstantin Aladyshev400766f2023-09-07 15:57:25 +03001497struct mctp_binding_astlpc *mctp_astlpc_init_fileio(const char *kcs_path)
Jeremy Kerr672c8852019-03-01 12:18:07 +08001498{
1499 struct mctp_binding_astlpc *astlpc;
1500 int rc;
1501
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301502 /*
1503 * If we're doing file IO then we're very likely not running
Andrew Jeffery8877c462020-06-15 12:22:53 +09301504 * freestanding, so lets assume that we're on the BMC side.
1505 *
1506 * Requesting an MTU of 0 requests the largest possible MTU, whatever
1507 * value that might take.
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301508 */
Andrew Jeffery8877c462020-06-15 12:22:53 +09301509 astlpc = __mctp_astlpc_init(MCTP_BINDING_ASTLPC_MODE_BMC, 0);
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301510 if (!astlpc)
1511 return NULL;
Jeremy Kerr672c8852019-03-01 12:18:07 +08001512
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301513 /* Set internal operations for kcs. We use direct accesses to the lpc
1514 * map area */
1515 astlpc->ops.kcs_read = __mctp_astlpc_fileio_kcs_read;
1516 astlpc->ops.kcs_write = __mctp_astlpc_fileio_kcs_write;
1517 astlpc->ops_data = astlpc;
1518
1519 rc = mctp_astlpc_init_fileio_lpc(astlpc);
Jeremy Kerr672c8852019-03-01 12:18:07 +08001520 if (rc) {
1521 free(astlpc);
1522 return NULL;
1523 }
1524
Konstantin Aladyshev400766f2023-09-07 15:57:25 +03001525 rc = mctp_astlpc_init_fileio_kcs(astlpc, kcs_path);
Jeremy Kerr672c8852019-03-01 12:18:07 +08001526 if (rc) {
1527 free(astlpc);
1528 return NULL;
1529 }
1530
Jeremy Kerr672c8852019-03-01 12:18:07 +08001531 return astlpc;
1532}
Jeremy Kerr92a10a62019-08-28 16:55:54 +05301533#else
Konstantin Aladyshev400766f2023-09-07 15:57:25 +03001534struct mctp_binding_astlpc *
1535mctp_astlpc_init_fileio(const char *kcs_path __unused)
Jeremy Kerr92a10a62019-08-28 16:55:54 +05301536{
Andrew Jeffery5b2702d2022-07-25 16:21:43 +09301537 mctp_prlog(MCTP_LOG_ERR, "%s: Missing support for file IO", __func__);
Jeremy Kerr92a10a62019-08-28 16:55:54 +05301538 return NULL;
1539}
Jeremy Kerr672c8852019-03-01 12:18:07 +08001540
Andrew Jeffery1111c6a2022-07-25 20:44:39 +09301541int mctp_astlpc_init_pollfd(struct mctp_binding_astlpc *astlpc __unused,
1542 struct pollfd *pollfd __unused)
1543{
1544 mctp_prlog(MCTP_LOG_ERR, "%s: Missing support for file IO", __func__);
1545 return -1;
1546}
Jeremy Kerr92a10a62019-08-28 16:55:54 +05301547#endif