blob: 3ea58aaed6c9cc15be443b74023060b56c15707d [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 Jeffery7cd72f12020-05-12 20:27:59 +093012#include <errno.h>
Andrew Jefferyedfe3832020-02-06 11:52:11 +103013#include <inttypes.h>
Jeremy Kerr672c8852019-03-01 12:18:07 +080014#include <stdbool.h>
15#include <stdlib.h>
16#include <string.h>
Jeremy Kerr672c8852019-03-01 12:18:07 +080017
Jeremy Kerr672c8852019-03-01 12:18:07 +080018#define pr_fmt(x) "astlpc: " x
19
Andrew Jefferyeba19a32021-03-09 23:09:40 +103020#include "container_of.h"
21#include "crc32.h"
Jeremy Kerr672c8852019-03-01 12:18:07 +080022#include "libmctp.h"
23#include "libmctp-alloc.h"
24#include "libmctp-log.h"
25#include "libmctp-astlpc.h"
Andrew Jeffery4622cad2020-11-03 22:20:18 +103026#include "range.h"
Jeremy Kerr672c8852019-03-01 12:18:07 +080027
Jeremy Kerrb214c642019-11-27 11:34:00 +080028#ifdef MCTP_HAVE_FILEIO
Jeremy Kerr92a10a62019-08-28 16:55:54 +053029
Jeremy Kerrc6f676d2019-12-19 09:24:06 +080030#include <unistd.h>
Jeremy Kerr92a10a62019-08-28 16:55:54 +053031#include <fcntl.h>
Andrew Jeffery1111c6a2022-07-25 20:44:39 +093032#include <poll.h>
Jeremy Kerr92a10a62019-08-28 16:55:54 +053033#include <sys/ioctl.h>
34#include <sys/mman.h>
35#include <linux/aspeed-lpc-ctrl.h>
36
37/* kernel interface */
Jeremy Kerr92a10a62019-08-28 16:55:54 +053038static const char *lpc_path = "/dev/aspeed-lpc-ctrl";
39
40#endif
41
Rashmica Gupta45d13322022-08-10 12:12:14 +100042enum mctp_astlpc_cmd {
43 cmd_initialise = 0x00,
44 cmd_tx_begin = 0x01,
45 cmd_rx_complete = 0x02,
46 cmd_dummy_value = 0xff,
47};
48
Andrew Jefferyfe763e92022-08-05 23:16:17 +093049enum mctp_astlpc_buffer_state {
50 /*
51 * Prior to "Channel Ready" we mark the buffers as "idle" to catch illegal accesses. In this
52 * state neither side is considered the owner of the buffer.
53 *
54 * Upon "Channel Ready", each side transitions the buffers from the initial "idle" state
55 * to the following target states:
56 *
57 * Tx buffer: "acquired"
58 * Rx buffer: "released"
59 */
60 buffer_state_idle,
61
62 /*
63 * Beyond initialisation by "Channel Ready", buffers are in the "acquired" state once:
64 *
65 * 1. We dequeue a control command transferring the buffer to our ownership out of the KCS
66 * interface, and
67 * 2. We are yet to complete all of our required accesses to the buffer
68 *
69 * * The Tx buffer enters the "acquired" state when we dequeue the "Rx Complete" command
70 * * The Rx buffer enters the "acquired" state when we dequeue the "Tx Begin" command
71 *
72 * It is a failure of implementation if it's possible for both sides to simultaneously
73 * consider a buffer as "acquired".
74 */
75 buffer_state_acquired,
76
77 /*
78 * Buffers are in the "prepared" state when:
79 *
80 * 1. We have completed all of our required accesses (read or write) for the buffer, and
81 * 2. We have not yet successfully enqueued the control command to hand off ownership
82 */
83 buffer_state_prepared,
84
85 /*
86 * Beyond initialisation by "Channel Ready", buffers are in the "released" state once:
87 *
88 * 1. We successfully enqueue the control command transferring ownership to the remote
89 * side in to the KCS interface
90 *
91 * * The Tx buffer enters the "released" state when we enqueue the "Tx Begin" command
92 * * The Rx buffer enters the "released" state when we enqueue the "Rx Complete" command
93 *
94 * It may be the case that both sides simultaneously consider a buffer to be in the
95 * "released" state. However, if this is true, it must also be true that a buffer ownership
96 * transfer command has been enqueued in the KCS interface and is yet to be dequeued.
97 */
98 buffer_state_released,
99};
100
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930101struct mctp_astlpc_buffer {
102 uint32_t offset;
103 uint32_t size;
Andrew Jefferyfe763e92022-08-05 23:16:17 +0930104 enum mctp_astlpc_buffer_state state;
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930105};
106
107struct mctp_astlpc_layout {
108 struct mctp_astlpc_buffer rx;
109 struct mctp_astlpc_buffer tx;
110};
111
Andrew Jeffery88412be2021-03-09 22:05:22 +1030112struct mctp_astlpc_protocol {
113 uint16_t version;
114 uint32_t (*packet_size)(uint32_t body);
115 uint32_t (*body_size)(uint32_t packet);
Andrew Jefferyeba19a32021-03-09 23:09:40 +1030116 void (*pktbuf_protect)(struct mctp_pktbuf *pkt);
117 bool (*pktbuf_validate)(struct mctp_pktbuf *pkt);
Andrew Jeffery88412be2021-03-09 22:05:22 +1030118};
119
Jeremy Kerr672c8852019-03-01 12:18:07 +0800120struct mctp_binding_astlpc {
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600121 struct mctp_binding binding;
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530122
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930123 void *lpc_map;
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930124 struct mctp_astlpc_layout layout;
125
126 uint8_t mode;
Andrew Jefferya9368982020-06-09 13:07:39 +0930127 uint32_t requested_mtu;
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530128
Andrew Jeffery88412be2021-03-09 22:05:22 +1030129 const struct mctp_astlpc_protocol *proto;
130
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530131 /* direct ops data */
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930132 struct mctp_binding_astlpc_ops ops;
133 void *ops_data;
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530134
135 /* fileio ops data */
Andrew Jeffery979c6a12020-05-23 20:04:49 +0930136 int kcs_fd;
137 uint8_t kcs_status;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800138};
139
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600140#define binding_to_astlpc(b) \
Jeremy Kerr672c8852019-03-01 12:18:07 +0800141 container_of(b, struct mctp_binding_astlpc, binding)
142
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930143#define astlpc_prlog(ctx, lvl, fmt, ...) \
144 do { \
145 bool __bmc = ((ctx)->mode == MCTP_BINDING_ASTLPC_MODE_BMC); \
146 mctp_prlog(lvl, pr_fmt("%s: " fmt), __bmc ? "bmc" : "host", \
147 ##__VA_ARGS__); \
148 } while (0)
149
150#define astlpc_prerr(ctx, fmt, ...) \
151 astlpc_prlog(ctx, MCTP_LOG_ERR, fmt, ##__VA_ARGS__)
152#define astlpc_prwarn(ctx, fmt, ...) \
153 astlpc_prlog(ctx, MCTP_LOG_WARNING, fmt, ##__VA_ARGS__)
Andrew Jeffery99b9d232023-06-23 15:15:30 +0930154#define astlpc_prnotice(ctx, fmt, ...) \
155 astlpc_prlog(ctx, MCTP_LOG_NOTICE, fmt, ##__VA_ARGS__)
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930156#define astlpc_prinfo(ctx, fmt, ...) \
157 astlpc_prlog(ctx, MCTP_LOG_INFO, fmt, ##__VA_ARGS__)
158#define astlpc_prdebug(ctx, fmt, ...) \
159 astlpc_prlog(ctx, MCTP_LOG_DEBUG, fmt, ##__VA_ARGS__)
160
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930161/* clang-format off */
162#define ASTLPC_MCTP_MAGIC 0x4d435450
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930163#define ASTLPC_VER_BAD 0
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930164#define ASTLPC_VER_MIN 1
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930165
Andrew Jeffery3a540662020-05-26 19:55:30 +0930166/* Support testing of new binding protocols */
167#ifndef ASTLPC_VER_CUR
Andrew Jefferyeba19a32021-03-09 23:09:40 +1030168#define ASTLPC_VER_CUR 3
Andrew Jeffery3a540662020-05-26 19:55:30 +0930169#endif
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930170/* clang-format on */
Jeremy Kerr672c8852019-03-01 12:18:07 +0800171
Andrew Jeffery88412be2021-03-09 22:05:22 +1030172#ifndef ARRAY_SIZE
173#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
174#endif
175
176static uint32_t astlpc_packet_size_v1(uint32_t body)
177{
178 assert((body + 4) > body);
179
180 return body + 4;
181}
182
183static uint32_t astlpc_body_size_v1(uint32_t packet)
184{
185 assert((packet - 4) < packet);
186
187 return packet - 4;
188}
189
Andrew Jefferyeba19a32021-03-09 23:09:40 +1030190void astlpc_pktbuf_protect_v1(struct mctp_pktbuf *pkt)
191{
192 (void)pkt;
193}
194
195bool astlpc_pktbuf_validate_v1(struct mctp_pktbuf *pkt)
196{
197 (void)pkt;
198 return true;
199}
200
201static uint32_t astlpc_packet_size_v3(uint32_t body)
202{
203 assert((body + 4 + 4) > body);
204
205 return body + 4 + 4;
206}
207
208static uint32_t astlpc_body_size_v3(uint32_t packet)
209{
210 assert((packet - 4 - 4) < packet);
211
212 return packet - 4 - 4;
213}
214
215void astlpc_pktbuf_protect_v3(struct mctp_pktbuf *pkt)
216{
217 uint32_t code;
218
219 code = htobe32(crc32(mctp_pktbuf_hdr(pkt), mctp_pktbuf_size(pkt)));
220 mctp_prdebug("%s: 0x%" PRIx32, __func__, code);
221 mctp_pktbuf_push(pkt, &code, 4);
222}
223
224bool astlpc_pktbuf_validate_v3(struct mctp_pktbuf *pkt)
225{
226 uint32_t code;
227 void *check;
228
229 code = be32toh(crc32(mctp_pktbuf_hdr(pkt), mctp_pktbuf_size(pkt) - 4));
230 mctp_prdebug("%s: 0x%" PRIx32, __func__, code);
231 check = mctp_pktbuf_pop(pkt, 4);
232 return check && !memcmp(&code, check, 4);
233}
234
Andrew Jeffery88412be2021-03-09 22:05:22 +1030235static const struct mctp_astlpc_protocol astlpc_protocol_version[] = {
236 [0] = {
237 .version = 0,
238 .packet_size = NULL,
239 .body_size = NULL,
Andrew Jefferyeba19a32021-03-09 23:09:40 +1030240 .pktbuf_protect = NULL,
241 .pktbuf_validate = NULL,
Andrew Jeffery88412be2021-03-09 22:05:22 +1030242 },
243 [1] = {
244 .version = 1,
245 .packet_size = astlpc_packet_size_v1,
246 .body_size = astlpc_body_size_v1,
Andrew Jefferyeba19a32021-03-09 23:09:40 +1030247 .pktbuf_protect = astlpc_pktbuf_protect_v1,
248 .pktbuf_validate = astlpc_pktbuf_validate_v1,
Andrew Jeffery88412be2021-03-09 22:05:22 +1030249 },
250 [2] = {
251 .version = 2,
252 .packet_size = astlpc_packet_size_v1,
253 .body_size = astlpc_body_size_v1,
Andrew Jefferyeba19a32021-03-09 23:09:40 +1030254 .pktbuf_protect = astlpc_pktbuf_protect_v1,
255 .pktbuf_validate = astlpc_pktbuf_validate_v1,
256 },
257 [3] = {
258 .version = 3,
259 .packet_size = astlpc_packet_size_v3,
260 .body_size = astlpc_body_size_v3,
261 .pktbuf_protect = astlpc_pktbuf_protect_v3,
262 .pktbuf_validate = astlpc_pktbuf_validate_v3,
Andrew Jeffery88412be2021-03-09 22:05:22 +1030263 },
264};
265
Jeremy Kerr672c8852019-03-01 12:18:07 +0800266struct mctp_lpcmap_hdr {
Andrew Jeffery3a540662020-05-26 19:55:30 +0930267 uint32_t magic;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800268
Andrew Jeffery3a540662020-05-26 19:55:30 +0930269 uint16_t bmc_ver_min;
270 uint16_t bmc_ver_cur;
271 uint16_t host_ver_min;
272 uint16_t host_ver_cur;
273 uint16_t negotiated_ver;
274 uint16_t pad0;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800275
Andrew Jeffery3a540662020-05-26 19:55:30 +0930276 struct {
277 uint32_t rx_offset;
278 uint32_t rx_size;
279 uint32_t tx_offset;
280 uint32_t tx_size;
281 } layout;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800282} __attribute__((packed));
283
Andrew Jeffery3a540662020-05-26 19:55:30 +0930284static const uint32_t control_size = 0x100;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800285
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600286#define LPC_WIN_SIZE (1 * 1024 * 1024)
Jeremy Kerr672c8852019-03-01 12:18:07 +0800287
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600288#define KCS_STATUS_BMC_READY 0x80
289#define KCS_STATUS_CHANNEL_ACTIVE 0x40
290#define KCS_STATUS_IBF 0x02
291#define KCS_STATUS_OBF 0x01
Jeremy Kerr672c8852019-03-01 12:18:07 +0800292
Andrew Jefferyf13cb972020-05-28 09:30:09 +0930293static inline int mctp_astlpc_kcs_write(struct mctp_binding_astlpc *astlpc,
294 enum mctp_binding_astlpc_kcs_reg reg,
295 uint8_t val)
296{
297 return astlpc->ops.kcs_write(astlpc->ops_data, reg, val);
298}
299
300static inline int mctp_astlpc_kcs_read(struct mctp_binding_astlpc *astlpc,
301 enum mctp_binding_astlpc_kcs_reg reg,
302 uint8_t *val)
303{
304 return astlpc->ops.kcs_read(astlpc->ops_data, reg, val);
305}
306
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930307static inline int mctp_astlpc_lpc_write(struct mctp_binding_astlpc *astlpc,
308 const void *buf, long offset,
309 size_t len)
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530310{
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930311 astlpc_prdebug(astlpc, "%s: %zu bytes to 0x%lx", __func__, len, offset);
312
313 assert(offset >= 0);
314
315 /* Indirect access */
316 if (astlpc->ops.lpc_write) {
317 void *data = astlpc->ops_data;
318
319 return astlpc->ops.lpc_write(data, buf, offset, len);
320 }
321
322 /* Direct mapping */
323 assert(astlpc->lpc_map);
324 memcpy(&((char *)astlpc->lpc_map)[offset], buf, len);
325
326 return 0;
327}
328
329static inline int mctp_astlpc_lpc_read(struct mctp_binding_astlpc *astlpc,
330 void *buf, long offset, size_t len)
331{
332 astlpc_prdebug(astlpc, "%s: %zu bytes from 0x%lx", __func__, len,
333 offset);
334
335 assert(offset >= 0);
336
337 /* Indirect access */
338 if (astlpc->ops.lpc_read) {
339 void *data = astlpc->ops_data;
340
341 return astlpc->ops.lpc_read(data, buf, offset, len);
342 }
343
344 /* Direct mapping */
345 assert(astlpc->lpc_map);
346 memcpy(buf, &((char *)astlpc->lpc_map)[offset], len);
347
348 return 0;
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530349}
350
Andrew Jeffery99b9d232023-06-23 15:15:30 +0930351static void
352mctp_astlpc_kcs_print_status_write(struct mctp_binding_astlpc *astlpc,
353 uint8_t status)
354{
355 astlpc_prnotice(
356 astlpc, "Binding state is 0x%hhx: BMC %s, Channel %s, OBF %s",
357 status, status & KCS_STATUS_BMC_READY ? "active" : "inactive",
358 status & KCS_STATUS_CHANNEL_ACTIVE ? "active" : "inactive",
359 status & KCS_STATUS_OBF ? "preserved" : "cleared");
360}
361
Andrew Jefferyd0f5da02020-05-28 09:12:55 +0930362static int mctp_astlpc_kcs_set_status(struct mctp_binding_astlpc *astlpc,
363 uint8_t status)
364{
365 uint8_t data;
366 int rc;
367
368 /* Since we're setting the status register, we want the other endpoint
369 * to be interrupted. However, some hardware may only raise a host-side
370 * interrupt on an ODR event.
371 * So, write a dummy value of 0xff to ODR, which will ensure that an
372 * interrupt is triggered, and can be ignored by the host.
373 */
Rashmica Gupta45d13322022-08-10 12:12:14 +1000374 data = cmd_dummy_value;
Andrew Jefferyd0f5da02020-05-28 09:12:55 +0930375
Andrew Jefferyf13cb972020-05-28 09:30:09 +0930376 rc = mctp_astlpc_kcs_write(astlpc, MCTP_ASTLPC_KCS_REG_STATUS, status);
Andrew Jefferyd0f5da02020-05-28 09:12:55 +0930377 if (rc) {
378 astlpc_prwarn(astlpc, "KCS status write failed");
379 return -1;
380 }
381
Andrew Jeffery99b9d232023-06-23 15:15:30 +0930382 mctp_astlpc_kcs_print_status_write(astlpc, status);
383
Andrew Jefferyf13cb972020-05-28 09:30:09 +0930384 rc = mctp_astlpc_kcs_write(astlpc, MCTP_ASTLPC_KCS_REG_DATA, data);
Andrew Jefferyd0f5da02020-05-28 09:12:55 +0930385 if (rc) {
386 astlpc_prwarn(astlpc, "KCS dummy data write failed");
387 return -1;
388 }
389
390 return 0;
391}
392
Andrew Jeffery3a540662020-05-26 19:55:30 +0930393static int mctp_astlpc_layout_read(struct mctp_binding_astlpc *astlpc,
394 struct mctp_astlpc_layout *layout)
395{
396 struct mctp_lpcmap_hdr hdr;
397 int rc;
398
399 rc = mctp_astlpc_lpc_read(astlpc, &hdr, 0, sizeof(hdr));
400 if (rc < 0)
401 return rc;
402
403 /* Flip the buffers as the names are defined in terms of the host */
404 if (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_BMC) {
405 layout->rx.offset = be32toh(hdr.layout.tx_offset);
406 layout->rx.size = be32toh(hdr.layout.tx_size);
407 layout->tx.offset = be32toh(hdr.layout.rx_offset);
408 layout->tx.size = be32toh(hdr.layout.rx_size);
409 } else {
410 assert(astlpc->mode == MCTP_BINDING_ASTLPC_MODE_HOST);
411
412 layout->rx.offset = be32toh(hdr.layout.rx_offset);
413 layout->rx.size = be32toh(hdr.layout.rx_size);
414 layout->tx.offset = be32toh(hdr.layout.tx_offset);
415 layout->tx.size = be32toh(hdr.layout.tx_size);
416 }
417
418 return 0;
419}
420
421static int mctp_astlpc_layout_write(struct mctp_binding_astlpc *astlpc,
422 struct mctp_astlpc_layout *layout)
423{
424 uint32_t rx_size_be;
425
426 if (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_BMC) {
427 struct mctp_lpcmap_hdr hdr;
428
429 /*
430 * Flip the buffers as the names are defined in terms of the
431 * host
432 */
433 hdr.layout.rx_offset = htobe32(layout->tx.offset);
434 hdr.layout.rx_size = htobe32(layout->tx.size);
435 hdr.layout.tx_offset = htobe32(layout->rx.offset);
436 hdr.layout.tx_size = htobe32(layout->rx.size);
437
438 return mctp_astlpc_lpc_write(astlpc, &hdr.layout,
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600439 offsetof(struct mctp_lpcmap_hdr,
440 layout),
441 sizeof(hdr.layout));
Andrew Jeffery3a540662020-05-26 19:55:30 +0930442 }
443
444 assert(astlpc->mode == MCTP_BINDING_ASTLPC_MODE_HOST);
445
446 /*
447 * As of v2 we only need to write rx_size - the offsets are controlled
448 * by the BMC, as is the BMC's rx_size (host tx_size).
449 */
450 rx_size_be = htobe32(layout->rx.size);
451 return mctp_astlpc_lpc_write(astlpc, &rx_size_be,
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600452 offsetof(struct mctp_lpcmap_hdr,
453 layout.rx_size),
454 sizeof(rx_size_be));
Andrew Jeffery3a540662020-05-26 19:55:30 +0930455}
456
Andrew Jeffery88412be2021-03-09 22:05:22 +1030457static bool
458mctp_astlpc_buffer_validate(const struct mctp_binding_astlpc *astlpc,
459 const struct mctp_astlpc_buffer *buf,
460 const char *name)
Andrew Jeffery3a540662020-05-26 19:55:30 +0930461{
462 /* Check for overflow */
463 if (buf->offset + buf->size < buf->offset) {
464 mctp_prerr(
465 "%s packet buffer parameters overflow: offset: 0x%" PRIx32
466 ", size: %" PRIu32,
467 name, buf->offset, buf->size);
468 return false;
469 }
470
471 /* Check that the buffers are contained within the allocated space */
472 if (buf->offset + buf->size > LPC_WIN_SIZE) {
473 mctp_prerr(
474 "%s packet buffer parameters exceed %uM window size: offset: 0x%" PRIx32
475 ", size: %" PRIu32,
476 name, (LPC_WIN_SIZE / (1024 * 1024)), buf->offset,
477 buf->size);
478 return false;
479 }
480
481 /* Check that the baseline transmission unit is supported */
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600482 if (buf->size <
483 astlpc->proto->packet_size(MCTP_PACKET_SIZE(MCTP_BTU))) {
Andrew Jeffery3a540662020-05-26 19:55:30 +0930484 mctp_prerr(
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600485 "%s packet buffer too small: Require %" PRIu32
486 " bytes to support the %u byte baseline transmission unit, found %" PRIu32,
Andrew Jeffery88412be2021-03-09 22:05:22 +1030487 name,
488 astlpc->proto->packet_size(MCTP_PACKET_SIZE(MCTP_BTU)),
Andrew Jeffery3a540662020-05-26 19:55:30 +0930489 MCTP_BTU, buf->size);
490 return false;
491 }
492
493 /* Check for overlap with the control space */
494 if (buf->offset < control_size) {
495 mctp_prerr(
496 "%s packet buffer overlaps control region {0x%" PRIx32
497 ", %" PRIu32 "}: Rx {0x%" PRIx32 ", %" PRIu32 "}",
498 name, 0U, control_size, buf->offset, buf->size);
499 return false;
500 }
501
502 return true;
503}
504
Andrew Jeffery88412be2021-03-09 22:05:22 +1030505static bool
506mctp_astlpc_layout_validate(const struct mctp_binding_astlpc *astlpc,
507 const struct mctp_astlpc_layout *layout)
Andrew Jeffery3a540662020-05-26 19:55:30 +0930508{
Andrew Jeffery88412be2021-03-09 22:05:22 +1030509 const struct mctp_astlpc_buffer *rx = &layout->rx;
510 const struct mctp_astlpc_buffer *tx = &layout->tx;
Andrew Jeffery3a540662020-05-26 19:55:30 +0930511 bool rx_valid, tx_valid;
512
Andrew Jeffery88412be2021-03-09 22:05:22 +1030513 rx_valid = mctp_astlpc_buffer_validate(astlpc, rx, "Rx");
514 tx_valid = mctp_astlpc_buffer_validate(astlpc, tx, "Tx");
Andrew Jeffery3a540662020-05-26 19:55:30 +0930515
516 if (!(rx_valid && tx_valid))
517 return false;
518
519 /* Check that the buffers are disjoint */
520 if ((rx->offset <= tx->offset && rx->offset + rx->size > tx->offset) ||
521 (tx->offset <= rx->offset && tx->offset + tx->size > rx->offset)) {
522 mctp_prerr("Rx and Tx packet buffers overlap: Rx {0x%" PRIx32
523 ", %" PRIu32 "}, Tx {0x%" PRIx32 ", %" PRIu32 "}",
524 rx->offset, rx->size, tx->offset, tx->size);
525 return false;
526 }
527
528 return true;
529}
530
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930531static int mctp_astlpc_init_bmc(struct mctp_binding_astlpc *astlpc)
532{
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930533 struct mctp_lpcmap_hdr hdr = { 0 };
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930534 uint8_t status;
Andrew Jeffery88412be2021-03-09 22:05:22 +1030535 uint32_t sz;
Andrew Jeffery3a540662020-05-26 19:55:30 +0930536
537 /*
538 * The largest buffer size is half of the allocated MCTP space
539 * excluding the control space.
540 */
541 sz = ((LPC_WIN_SIZE - control_size) / 2);
542
543 /*
544 * Trim the MTU to a multiple of 16 to meet the requirements of 12.17
545 * Query Hop in DSP0236 v1.3.0.
546 */
Andrew Jeffery88412be2021-03-09 22:05:22 +1030547 sz = MCTP_BODY_SIZE(astlpc->proto->body_size(sz));
Andrew Jeffery3a540662020-05-26 19:55:30 +0930548 sz &= ~0xfUL;
Andrew Jeffery88412be2021-03-09 22:05:22 +1030549 sz = astlpc->proto->packet_size(MCTP_PACKET_SIZE(sz));
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930550
Andrew Jefferya9368982020-06-09 13:07:39 +0930551 if (astlpc->requested_mtu) {
Andrew Jeffery88412be2021-03-09 22:05:22 +1030552 uint32_t rpkt, rmtu;
Andrew Jefferya9368982020-06-09 13:07:39 +0930553
Andrew Jeffery88412be2021-03-09 22:05:22 +1030554 rmtu = astlpc->requested_mtu;
555 rpkt = astlpc->proto->packet_size(MCTP_PACKET_SIZE(rmtu));
556 sz = MIN(sz, rpkt);
Andrew Jefferya9368982020-06-09 13:07:39 +0930557 }
558
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930559 /* Flip the buffers as the names are defined in terms of the host */
Andrew Jeffery3a540662020-05-26 19:55:30 +0930560 astlpc->layout.tx.offset = control_size;
561 astlpc->layout.tx.size = sz;
562 astlpc->layout.rx.offset =
563 astlpc->layout.tx.offset + astlpc->layout.tx.size;
564 astlpc->layout.rx.size = sz;
565
Andrew Jeffery88412be2021-03-09 22:05:22 +1030566 if (!mctp_astlpc_layout_validate(astlpc, &astlpc->layout)) {
567 astlpc_prerr(astlpc, "Cannot support an MTU of %" PRIu32, sz);
Andrew Jefferya9368982020-06-09 13:07:39 +0930568 return -EINVAL;
569 }
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930570
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930571 hdr = (struct mctp_lpcmap_hdr){
572 .magic = htobe32(ASTLPC_MCTP_MAGIC),
573 .bmc_ver_min = htobe16(ASTLPC_VER_MIN),
574 .bmc_ver_cur = htobe16(ASTLPC_VER_CUR),
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930575
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930576 /* Flip the buffers back as we're now describing the host's
577 * configuration to the host */
Andrew Jeffery3a540662020-05-26 19:55:30 +0930578 .layout.rx_offset = htobe32(astlpc->layout.tx.offset),
579 .layout.rx_size = htobe32(astlpc->layout.tx.size),
580 .layout.tx_offset = htobe32(astlpc->layout.rx.offset),
581 .layout.tx_size = htobe32(astlpc->layout.rx.size),
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930582 };
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930583
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930584 mctp_astlpc_lpc_write(astlpc, &hdr, 0, sizeof(hdr));
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930585
Andrew Jefferyb3b55a62020-07-06 13:34:18 +0930586 /*
587 * Set status indicating that the BMC is now active. Be explicit about
588 * clearing OBF; we're reinitialising the binding and so any previous
589 * buffer state is irrelevant.
590 */
591 status = KCS_STATUS_BMC_READY & ~KCS_STATUS_OBF;
Andrew Jefferyd0f5da02020-05-28 09:12:55 +0930592 return mctp_astlpc_kcs_set_status(astlpc, status);
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930593}
594
595static int mctp_binding_astlpc_start_bmc(struct mctp_binding *b)
596{
597 struct mctp_binding_astlpc *astlpc =
598 container_of(b, struct mctp_binding_astlpc, binding);
599
Andrew Jeffery88412be2021-03-09 22:05:22 +1030600 astlpc->proto = &astlpc_protocol_version[ASTLPC_VER_CUR];
601
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930602 return mctp_astlpc_init_bmc(astlpc);
603}
604
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930605static bool mctp_astlpc_validate_version(uint16_t bmc_ver_min,
606 uint16_t bmc_ver_cur,
607 uint16_t host_ver_min,
608 uint16_t host_ver_cur)
609{
610 if (!(bmc_ver_min && bmc_ver_cur && host_ver_min && host_ver_cur)) {
611 mctp_prerr("Invalid version present in [%" PRIu16 ", %" PRIu16
612 "], [%" PRIu16 ", %" PRIu16 "]",
613 bmc_ver_min, bmc_ver_cur, host_ver_min,
614 host_ver_cur);
615 return false;
616 } else if (bmc_ver_min > bmc_ver_cur) {
617 mctp_prerr("Invalid bmc version range [%" PRIu16 ", %" PRIu16
618 "]",
619 bmc_ver_min, bmc_ver_cur);
620 return false;
621 } else if (host_ver_min > host_ver_cur) {
622 mctp_prerr("Invalid host version range [%" PRIu16 ", %" PRIu16
623 "]",
624 host_ver_min, host_ver_cur);
625 return false;
626 } else if ((host_ver_cur < bmc_ver_min) ||
627 (host_ver_min > bmc_ver_cur)) {
628 mctp_prerr(
629 "Unable to satisfy version negotiation with ranges [%" PRIu16
630 ", %" PRIu16 "] and [%" PRIu16 ", %" PRIu16 "]",
631 bmc_ver_min, bmc_ver_cur, host_ver_min, host_ver_cur);
632 return false;
633 }
634
635 return true;
636}
637
Andrew Jeffery3a540662020-05-26 19:55:30 +0930638static int mctp_astlpc_negotiate_layout_host(struct mctp_binding_astlpc *astlpc)
639{
640 struct mctp_astlpc_layout layout;
Andrew Jeffery88412be2021-03-09 22:05:22 +1030641 uint32_t rmtu;
Andrew Jeffery3a540662020-05-26 19:55:30 +0930642 uint32_t sz;
643 int rc;
644
645 rc = mctp_astlpc_layout_read(astlpc, &layout);
646 if (rc < 0)
647 return rc;
648
Andrew Jeffery88412be2021-03-09 22:05:22 +1030649 if (!mctp_astlpc_layout_validate(astlpc, &layout)) {
Andrew Jeffery3a540662020-05-26 19:55:30 +0930650 astlpc_prerr(
651 astlpc,
652 "BMC provided invalid buffer layout: Rx {0x%" PRIx32
653 ", %" PRIu32 "}, Tx {0x%" PRIx32 ", %" PRIu32 "}",
654 layout.rx.offset, layout.rx.size, layout.tx.offset,
655 layout.tx.size);
656 return -EINVAL;
657 }
658
Andrew Jefferya9368982020-06-09 13:07:39 +0930659 astlpc_prinfo(astlpc, "Desire an MTU of %" PRIu32 " bytes",
660 astlpc->requested_mtu);
661
Andrew Jeffery88412be2021-03-09 22:05:22 +1030662 rmtu = astlpc->requested_mtu;
663 sz = astlpc->proto->packet_size(MCTP_PACKET_SIZE(rmtu));
Andrew Jeffery3a540662020-05-26 19:55:30 +0930664 layout.rx.size = sz;
665
Andrew Jeffery88412be2021-03-09 22:05:22 +1030666 if (!mctp_astlpc_layout_validate(astlpc, &layout)) {
Andrew Jeffery3a540662020-05-26 19:55:30 +0930667 astlpc_prerr(
668 astlpc,
669 "Generated invalid buffer layout with size %" PRIu32
670 ": Rx {0x%" PRIx32 ", %" PRIu32 "}, Tx {0x%" PRIx32
671 ", %" PRIu32 "}",
672 sz, layout.rx.offset, layout.rx.size, layout.tx.offset,
673 layout.tx.size);
674 return -EINVAL;
675 }
676
Andrew Jefferya9368982020-06-09 13:07:39 +0930677 astlpc_prinfo(astlpc, "Requesting MTU of %" PRIu32 " bytes",
678 astlpc->requested_mtu);
Andrew Jeffery3a540662020-05-26 19:55:30 +0930679
680 return mctp_astlpc_layout_write(astlpc, &layout);
681}
682
Andrew Jeffery88412be2021-03-09 22:05:22 +1030683static uint16_t mctp_astlpc_negotiate_version(uint16_t bmc_ver_min,
684 uint16_t bmc_ver_cur,
685 uint16_t host_ver_min,
686 uint16_t host_ver_cur)
687{
688 if (!mctp_astlpc_validate_version(bmc_ver_min, bmc_ver_cur,
689 host_ver_min, host_ver_cur))
690 return ASTLPC_VER_BAD;
691
692 if (bmc_ver_cur < host_ver_cur)
693 return bmc_ver_cur;
694
695 return host_ver_cur;
696}
697
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930698static int mctp_astlpc_init_host(struct mctp_binding_astlpc *astlpc)
699{
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930700 const uint16_t ver_min_be = htobe16(ASTLPC_VER_MIN);
701 const uint16_t ver_cur_be = htobe16(ASTLPC_VER_CUR);
Andrew Jeffery88412be2021-03-09 22:05:22 +1030702 uint16_t bmc_ver_min, bmc_ver_cur, negotiated;
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930703 struct mctp_lpcmap_hdr hdr;
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930704 uint8_t status;
705 int rc;
706
Andrew Jefferyf13cb972020-05-28 09:30:09 +0930707 rc = mctp_astlpc_kcs_read(astlpc, MCTP_ASTLPC_KCS_REG_STATUS, &status);
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930708 if (rc) {
709 mctp_prwarn("KCS status read failed");
710 return rc;
711 }
712
713 astlpc->kcs_status = status;
714
715 if (!(status & KCS_STATUS_BMC_READY))
716 return -EHOSTDOWN;
717
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930718 mctp_astlpc_lpc_read(astlpc, &hdr, 0, sizeof(hdr));
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930719
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930720 bmc_ver_min = be16toh(hdr.bmc_ver_min);
721 bmc_ver_cur = be16toh(hdr.bmc_ver_cur);
722
Andrew Jeffery88412be2021-03-09 22:05:22 +1030723 /* Calculate the expected value of negotiated_ver */
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600724 negotiated = mctp_astlpc_negotiate_version(
725 bmc_ver_min, bmc_ver_cur, ASTLPC_VER_MIN, ASTLPC_VER_CUR);
Andrew Jeffery88412be2021-03-09 22:05:22 +1030726 if (!negotiated) {
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930727 astlpc_prerr(astlpc, "Cannot negotiate with invalid versions");
728 return -EINVAL;
729 }
730
Andrew Jeffery88412be2021-03-09 22:05:22 +1030731 /* Assign protocol ops so we can calculate the packet buffer sizes */
732 assert(negotiated < ARRAY_SIZE(astlpc_protocol_version));
733 astlpc->proto = &astlpc_protocol_version[negotiated];
734
735 /* Negotiate packet buffers in v2 style if the BMC supports it */
736 if (negotiated >= 2) {
Andrew Jeffery3a540662020-05-26 19:55:30 +0930737 rc = mctp_astlpc_negotiate_layout_host(astlpc);
738 if (rc < 0)
739 return rc;
740 }
741
Andrew Jeffery88412be2021-03-09 22:05:22 +1030742 /* Advertise the host's supported protocol versions */
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930743 mctp_astlpc_lpc_write(astlpc, &ver_min_be,
744 offsetof(struct mctp_lpcmap_hdr, host_ver_min),
745 sizeof(ver_min_be));
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930746
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930747 mctp_astlpc_lpc_write(astlpc, &ver_cur_be,
748 offsetof(struct mctp_lpcmap_hdr, host_ver_cur),
749 sizeof(ver_cur_be));
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930750
751 /* Send channel init command */
Andrew Jefferyf13cb972020-05-28 09:30:09 +0930752 rc = mctp_astlpc_kcs_write(astlpc, MCTP_ASTLPC_KCS_REG_DATA, 0x0);
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930753 if (rc) {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930754 astlpc_prwarn(astlpc, "KCS write failed");
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930755 }
756
Andrew Jeffery88412be2021-03-09 22:05:22 +1030757 /*
758 * Configure the host so `astlpc->proto->version == 0` holds until we
759 * receive a subsequent status update from the BMC. Until then,
760 * `astlpc->proto->version == 0` indicates that we're yet to complete
761 * the channel initialisation handshake.
762 *
763 * When the BMC provides a status update with KCS_STATUS_CHANNEL_ACTIVE
764 * set we will assign the appropriate protocol ops struct in accordance
765 * with `negotiated_ver`.
766 */
767 astlpc->proto = &astlpc_protocol_version[ASTLPC_VER_BAD];
768
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930769 return rc;
770}
771
772static int mctp_binding_astlpc_start_host(struct mctp_binding *b)
773{
774 struct mctp_binding_astlpc *astlpc =
775 container_of(b, struct mctp_binding_astlpc, binding);
776
777 return mctp_astlpc_init_host(astlpc);
778}
779
780static bool __mctp_astlpc_kcs_ready(struct mctp_binding_astlpc *astlpc,
781 uint8_t status, bool is_write)
782{
783 bool is_bmc;
784 bool ready_state;
785 uint8_t flag;
786
787 is_bmc = (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_BMC);
788 flag = (is_bmc ^ is_write) ? KCS_STATUS_IBF : KCS_STATUS_OBF;
789 ready_state = is_write ? 0 : 1;
790
791 return !!(status & flag) == ready_state;
792}
793
794static inline bool
795mctp_astlpc_kcs_read_ready(struct mctp_binding_astlpc *astlpc, uint8_t status)
796{
797 return __mctp_astlpc_kcs_ready(astlpc, status, false);
798}
799
800static inline bool
801mctp_astlpc_kcs_write_ready(struct mctp_binding_astlpc *astlpc, uint8_t status)
802{
803 return __mctp_astlpc_kcs_ready(astlpc, status, true);
804}
805
Jeremy Kerr672c8852019-03-01 12:18:07 +0800806static int mctp_astlpc_kcs_send(struct mctp_binding_astlpc *astlpc,
Rashmica Gupta45d13322022-08-10 12:12:14 +1000807 enum mctp_astlpc_cmd data)
Jeremy Kerr672c8852019-03-01 12:18:07 +0800808{
809 uint8_t status;
810 int rc;
811
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600812 rc = mctp_astlpc_kcs_read(astlpc, MCTP_ASTLPC_KCS_REG_STATUS, &status);
Andrew Jefferyfe763e92022-08-05 23:16:17 +0930813 if (rc) {
814 astlpc_prwarn(astlpc, "KCS status read failed");
815 return -EIO;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800816 }
Andrew Jefferyfe763e92022-08-05 23:16:17 +0930817 if (!mctp_astlpc_kcs_write_ready(astlpc, status))
818 return -EBUSY;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800819
Andrew Jefferyf13cb972020-05-28 09:30:09 +0930820 rc = mctp_astlpc_kcs_write(astlpc, MCTP_ASTLPC_KCS_REG_DATA, data);
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530821 if (rc) {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930822 astlpc_prwarn(astlpc, "KCS data write failed");
Andrew Jefferyfe763e92022-08-05 23:16:17 +0930823 return -EIO;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800824 }
825
826 return 0;
827}
828
829static int mctp_binding_astlpc_tx(struct mctp_binding *b,
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600830 struct mctp_pktbuf *pkt)
Jeremy Kerr672c8852019-03-01 12:18:07 +0800831{
832 struct mctp_binding_astlpc *astlpc = binding_to_astlpc(b);
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930833 uint32_t len, len_be;
Andrew Jefferyedfe3832020-02-06 11:52:11 +1030834 struct mctp_hdr *hdr;
Andrew Jefferyfe763e92022-08-05 23:16:17 +0930835 int rc;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800836
Andrew Jefferyedfe3832020-02-06 11:52:11 +1030837 hdr = mctp_pktbuf_hdr(pkt);
Jeremy Kerr672c8852019-03-01 12:18:07 +0800838 len = mctp_pktbuf_size(pkt);
Andrew Jefferyedfe3832020-02-06 11:52:11 +1030839
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930840 astlpc_prdebug(astlpc,
841 "%s: Transmitting %" PRIu32
842 "-byte packet (%hhu, %hhu, 0x%hhx)",
843 __func__, len, hdr->src, hdr->dest, hdr->flags_seq_tag);
Andrew Jefferyedfe3832020-02-06 11:52:11 +1030844
Andrew Jeffery88412be2021-03-09 22:05:22 +1030845 if (len > astlpc->proto->body_size(astlpc->layout.tx.size)) {
Andrew Jeffery0721f582022-09-29 12:12:39 +0930846 astlpc_prwarn(astlpc, "invalid TX len %" PRIu32 ": %" PRIu32,
847 len,
848 astlpc->proto->body_size(astlpc->layout.tx.size));
849 return -EMSGSIZE;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800850 }
851
Andrew Jefferyfe763e92022-08-05 23:16:17 +0930852 mctp_binding_set_tx_enabled(b, false);
853
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930854 len_be = htobe32(len);
855 mctp_astlpc_lpc_write(astlpc, &len_be, astlpc->layout.tx.offset,
856 sizeof(len_be));
Andrew Jefferyeba19a32021-03-09 23:09:40 +1030857
858 astlpc->proto->pktbuf_protect(pkt);
859 len = mctp_pktbuf_size(pkt);
860
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930861 mctp_astlpc_lpc_write(astlpc, hdr, astlpc->layout.tx.offset + 4, len);
Jeremy Kerr672c8852019-03-01 12:18:07 +0800862
Andrew Jefferyfe763e92022-08-05 23:16:17 +0930863 astlpc->layout.tx.state = buffer_state_prepared;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800864
Rashmica Gupta45d13322022-08-10 12:12:14 +1000865 rc = mctp_astlpc_kcs_send(astlpc, cmd_tx_begin);
Andrew Jefferyfe763e92022-08-05 23:16:17 +0930866 if (!rc)
867 astlpc->layout.tx.state = buffer_state_released;
Andrew Jefferyeba19a32021-03-09 23:09:40 +1030868
Andrew Jefferyfe763e92022-08-05 23:16:17 +0930869 return rc == -EBUSY ? 0 : rc;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800870}
871
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800872/* Update binding pkt_size and reallocate tx_storage */
873static int mctp_astlpc_set_pkt_size(struct mctp_binding_astlpc *astlpc,
874 size_t pkt_size)
875{
876 size_t body = MCTP_BODY_SIZE(pkt_size);
877 body += astlpc->binding.pkt_header + astlpc->binding.pkt_trailer;
878 size_t pktbuf_size = MCTP_PKTBUF_SIZE(body);
879 /* Reallocate TX storage */
880 if (astlpc->binding.tx_storage) {
881 __mctp_free(astlpc->binding.tx_storage);
882 }
883 astlpc->binding.tx_storage = __mctp_alloc(pktbuf_size);
884 if (!astlpc->binding.tx_storage) {
885 return -ENOMEM;
886 }
887
888 astlpc->binding.pkt_size = pkt_size;
889 return 0;
890}
891
Andrew Jeffery3a540662020-05-26 19:55:30 +0930892static uint32_t mctp_astlpc_calculate_mtu(struct mctp_binding_astlpc *astlpc,
893 struct mctp_astlpc_layout *layout)
894{
Andrew Jeffery88412be2021-03-09 22:05:22 +1030895 uint32_t low, high, limit, rpkt;
Andrew Jeffery3a540662020-05-26 19:55:30 +0930896
897 /* Derive the largest MTU the BMC _can_ support */
898 low = MIN(astlpc->layout.rx.offset, astlpc->layout.tx.offset);
899 high = MAX(astlpc->layout.rx.offset, astlpc->layout.tx.offset);
900 limit = high - low;
901
Andrew Jefferya9368982020-06-09 13:07:39 +0930902 /* Determine the largest MTU the BMC _wants_ to support */
903 if (astlpc->requested_mtu) {
Andrew Jeffery88412be2021-03-09 22:05:22 +1030904 uint32_t rmtu = astlpc->requested_mtu;
Andrew Jefferya9368982020-06-09 13:07:39 +0930905
Andrew Jeffery88412be2021-03-09 22:05:22 +1030906 rpkt = astlpc->proto->packet_size(MCTP_PACKET_SIZE(rmtu));
907 limit = MIN(limit, rpkt);
Andrew Jefferya9368982020-06-09 13:07:39 +0930908 }
Andrew Jeffery3a540662020-05-26 19:55:30 +0930909
910 /* Determine the accepted MTU, applied both directions by convention */
Andrew Jeffery88412be2021-03-09 22:05:22 +1030911 rpkt = MIN(limit, layout->tx.size);
912 return MCTP_BODY_SIZE(astlpc->proto->body_size(rpkt));
Andrew Jeffery3a540662020-05-26 19:55:30 +0930913}
914
Andrew Jeffery88412be2021-03-09 22:05:22 +1030915static int mctp_astlpc_negotiate_layout_bmc(struct mctp_binding_astlpc *astlpc)
Andrew Jeffery3a540662020-05-26 19:55:30 +0930916{
917 struct mctp_astlpc_layout proposed, pending;
918 uint32_t sz, mtu;
919 int rc;
920
Andrew Jeffery88412be2021-03-09 22:05:22 +1030921 /* Do we have a valid protocol version? */
922 if (!astlpc->proto->version)
923 return -EINVAL;
924
Andrew Jeffery3a540662020-05-26 19:55:30 +0930925 /* Extract the host's proposed layout */
926 rc = mctp_astlpc_layout_read(astlpc, &proposed);
927 if (rc < 0)
928 return rc;
929
Andrew Jeffery88412be2021-03-09 22:05:22 +1030930 /* Do we have a reasonable layout? */
931 if (!mctp_astlpc_layout_validate(astlpc, &proposed))
Andrew Jeffery3a540662020-05-26 19:55:30 +0930932 return -EINVAL;
933
934 /* Negotiate the MTU */
935 mtu = mctp_astlpc_calculate_mtu(astlpc, &proposed);
Andrew Jeffery88412be2021-03-09 22:05:22 +1030936 sz = astlpc->proto->packet_size(MCTP_PACKET_SIZE(mtu));
Andrew Jeffery3a540662020-05-26 19:55:30 +0930937
938 /*
939 * Use symmetric MTUs by convention and to pass constraints in rx/tx
940 * functions
941 */
942 pending = astlpc->layout;
943 pending.tx.size = sz;
944 pending.rx.size = sz;
945
Andrew Jeffery88412be2021-03-09 22:05:22 +1030946 if (mctp_astlpc_layout_validate(astlpc, &pending)) {
Andrew Jeffery3a540662020-05-26 19:55:30 +0930947 /* We found a sensible Rx MTU, so honour it */
948 astlpc->layout = pending;
949
950 /* Enforce the negotiated MTU */
951 rc = mctp_astlpc_layout_write(astlpc, &astlpc->layout);
952 if (rc < 0)
953 return rc;
954
955 astlpc_prinfo(astlpc, "Negotiated an MTU of %" PRIu32 " bytes",
956 mtu);
957 } else {
958 astlpc_prwarn(astlpc, "MTU negotiation failed");
959 return -EINVAL;
960 }
961
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800962 if (astlpc->proto->version >= 2) {
963 rc = mctp_astlpc_set_pkt_size(astlpc, MCTP_PACKET_SIZE(mtu));
964 if (rc) {
965 astlpc_prwarn(astlpc, "Allocation error");
966 return rc;
967 }
968 }
Andrew Jeffery3a540662020-05-26 19:55:30 +0930969
970 return 0;
971}
972
Jeremy Kerr672c8852019-03-01 12:18:07 +0800973static void mctp_astlpc_init_channel(struct mctp_binding_astlpc *astlpc)
974{
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930975 uint16_t negotiated, negotiated_be;
976 struct mctp_lpcmap_hdr hdr;
977 uint8_t status;
Andrew Jeffery3a540662020-05-26 19:55:30 +0930978 int rc;
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930979
980 mctp_astlpc_lpc_read(astlpc, &hdr, 0, sizeof(hdr));
981
982 /* Version negotiation */
Andrew Jeffery7f7fdc12023-05-12 15:56:47 +0930983 negotiated = mctp_astlpc_negotiate_version(ASTLPC_VER_MIN,
984 ASTLPC_VER_CUR,
985 be16toh(hdr.host_ver_min),
986 be16toh(hdr.host_ver_cur));
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930987
Andrew Jeffery88412be2021-03-09 22:05:22 +1030988 /* MTU negotiation requires knowing which protocol we'll use */
989 assert(negotiated < ARRAY_SIZE(astlpc_protocol_version));
990 astlpc->proto = &astlpc_protocol_version[negotiated];
991
Andrew Jeffery3a540662020-05-26 19:55:30 +0930992 /* Host Rx MTU negotiation: Failure terminates channel init */
Andrew Jeffery88412be2021-03-09 22:05:22 +1030993 rc = mctp_astlpc_negotiate_layout_bmc(astlpc);
Andrew Jeffery3a540662020-05-26 19:55:30 +0930994 if (rc < 0)
995 negotiated = ASTLPC_VER_BAD;
996
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930997 /* Populate the negotiated version */
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930998 negotiated_be = htobe16(negotiated);
999 mctp_astlpc_lpc_write(astlpc, &negotiated_be,
Andrew Jeffery55fb90b2020-05-12 13:54:37 +09301000 offsetof(struct mctp_lpcmap_hdr, negotiated_ver),
Andrew Jeffery4e8264b2020-05-23 20:34:33 +09301001 sizeof(negotiated_be));
Andrew Jeffery55fb90b2020-05-12 13:54:37 +09301002
Andrew Jefferyfe763e92022-08-05 23:16:17 +09301003 /* Track buffer ownership */
1004 astlpc->layout.tx.state = buffer_state_acquired;
1005 astlpc->layout.rx.state = buffer_state_released;
1006
Andrew Jeffery4e8264b2020-05-23 20:34:33 +09301007 /* Finalise the configuration */
1008 status = KCS_STATUS_BMC_READY | KCS_STATUS_OBF;
1009 if (negotiated > 0) {
1010 astlpc_prinfo(astlpc, "Negotiated binding version %" PRIu16,
1011 negotiated);
1012 status |= KCS_STATUS_CHANNEL_ACTIVE;
1013 } else {
Andrew Jeffery88412be2021-03-09 22:05:22 +10301014 astlpc_prerr(astlpc, "Failed to initialise channel");
Andrew Jeffery4e8264b2020-05-23 20:34:33 +09301015 }
Jeremy Kerr672c8852019-03-01 12:18:07 +08001016
Andrew Jeffery4e8264b2020-05-23 20:34:33 +09301017 mctp_astlpc_kcs_set_status(astlpc, status);
1018
1019 mctp_binding_set_tx_enabled(&astlpc->binding,
1020 status & KCS_STATUS_CHANNEL_ACTIVE);
Jeremy Kerr672c8852019-03-01 12:18:07 +08001021}
1022
1023static void mctp_astlpc_rx_start(struct mctp_binding_astlpc *astlpc)
1024{
1025 struct mctp_pktbuf *pkt;
Rashmica Guptaf447b4f2022-10-11 17:44:31 +11001026 struct mctp_hdr *hdr;
Andrew Jefferyeba19a32021-03-09 23:09:40 +10301027 uint32_t body, packet;
Jeremy Kerr672c8852019-03-01 12:18:07 +08001028
Andrew Jefferyeba19a32021-03-09 23:09:40 +10301029 mctp_astlpc_lpc_read(astlpc, &body, astlpc->layout.rx.offset,
1030 sizeof(body));
1031 body = be32toh(body);
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301032
Andrew Jefferyeba19a32021-03-09 23:09:40 +10301033 if (body > astlpc->proto->body_size(astlpc->layout.rx.size)) {
1034 astlpc_prwarn(astlpc, "invalid RX len 0x%x", body);
Jeremy Kerr672c8852019-03-01 12:18:07 +08001035 return;
1036 }
1037
Andrew Jeffery0f05b6c2022-11-15 11:37:50 +10301038 if ((size_t)body > astlpc->binding.pkt_size) {
Andrew Jefferyeba19a32021-03-09 23:09:40 +10301039 astlpc_prwarn(astlpc, "invalid RX len 0x%x", body);
Jeremy Kerr672c8852019-03-01 12:18:07 +08001040 return;
1041 }
1042
Andrew Jefferyeba19a32021-03-09 23:09:40 +10301043 /* Eliminate the medium-specific header that we just read */
1044 packet = astlpc->proto->packet_size(body) - 4;
1045 pkt = mctp_pktbuf_alloc(&astlpc->binding, packet);
Christian Geddesae59f4f2021-09-01 09:54:25 -05001046 if (!pkt) {
Patrick Williamsa721c2d2022-12-04 14:30:26 -06001047 astlpc_prwarn(astlpc, "unable to allocate pktbuf len 0x%x",
1048 packet);
Christian Geddesae59f4f2021-09-01 09:54:25 -05001049 return;
1050 }
Jeremy Kerr672c8852019-03-01 12:18:07 +08001051
Andrew Jefferyeba19a32021-03-09 23:09:40 +10301052 /*
1053 * Read payload and medium-specific trailer from immediately after the
1054 * medium-specific header.
1055 */
Andrew Jeffery55fb90b2020-05-12 13:54:37 +09301056 mctp_astlpc_lpc_read(astlpc, mctp_pktbuf_hdr(pkt),
Andrew Jefferyeba19a32021-03-09 23:09:40 +10301057 astlpc->layout.rx.offset + 4, packet);
Jeremy Kerr672c8852019-03-01 12:18:07 +08001058
Andrew Jefferyfe763e92022-08-05 23:16:17 +09301059 astlpc->layout.rx.state = buffer_state_prepared;
1060
Christian Geddesae59f4f2021-09-01 09:54:25 -05001061 /* Inform the other side of the MCTP interface that we have read
1062 * the packet off the bus before handling the contents of the packet.
1063 */
Rashmica Gupta45d13322022-08-10 12:12:14 +10001064 if (!mctp_astlpc_kcs_send(astlpc, cmd_rx_complete))
Andrew Jefferyfe763e92022-08-05 23:16:17 +09301065 astlpc->layout.rx.state = buffer_state_released;
Christian Geddesae59f4f2021-09-01 09:54:25 -05001066
Rashmica Guptaf447b4f2022-10-11 17:44:31 +11001067 hdr = mctp_pktbuf_hdr(pkt);
1068 if (hdr->ver != 1) {
1069 mctp_pktbuf_free(pkt);
1070 astlpc_prdebug(astlpc, "Dropped packet with invalid version");
1071 return;
1072 }
1073
Andrew Jefferyeba19a32021-03-09 23:09:40 +10301074 /*
1075 * v3 will validate the CRC32 in the medium-specific trailer and adjust
1076 * the packet size accordingly. On older protocols validation is a no-op
1077 * that always returns true.
1078 */
1079 if (astlpc->proto->pktbuf_validate(pkt)) {
1080 mctp_bus_rx(&astlpc->binding, pkt);
1081 } else {
1082 /* TODO: Drop any associated assembly */
Andrew Jefferyeba19a32021-03-09 23:09:40 +10301083 astlpc_prdebug(astlpc, "Dropped corrupt packet");
1084 }
Matt Johnston4a09e1d2024-09-13 14:55:58 +08001085 mctp_pktbuf_free(pkt);
Jeremy Kerr672c8852019-03-01 12:18:07 +08001086}
1087
1088static void mctp_astlpc_tx_complete(struct mctp_binding_astlpc *astlpc)
1089{
Andrew Jefferyfe763e92022-08-05 23:16:17 +09301090 astlpc->layout.tx.state = buffer_state_acquired;
Jeremy Kerr672c8852019-03-01 12:18:07 +08001091 mctp_binding_set_tx_enabled(&astlpc->binding, true);
1092}
1093
Andrew Jeffery4e8264b2020-05-23 20:34:33 +09301094static int mctp_astlpc_finalise_channel(struct mctp_binding_astlpc *astlpc)
1095{
Andrew Jeffery3a540662020-05-26 19:55:30 +09301096 struct mctp_astlpc_layout layout;
Andrew Jeffery4e8264b2020-05-23 20:34:33 +09301097 uint16_t negotiated;
1098 int rc;
1099
1100 rc = mctp_astlpc_lpc_read(astlpc, &negotiated,
1101 offsetof(struct mctp_lpcmap_hdr,
1102 negotiated_ver),
1103 sizeof(negotiated));
1104 if (rc < 0)
1105 return rc;
1106
1107 negotiated = be16toh(negotiated);
Andrew Jeffery88412be2021-03-09 22:05:22 +10301108 astlpc_prerr(astlpc, "Version negotiation got: %u", negotiated);
Andrew Jeffery4e8264b2020-05-23 20:34:33 +09301109
1110 if (negotiated == ASTLPC_VER_BAD || negotiated < ASTLPC_VER_MIN ||
1111 negotiated > ASTLPC_VER_CUR) {
1112 astlpc_prerr(astlpc, "Failed to negotiate version, got: %u\n",
1113 negotiated);
1114 return -EINVAL;
1115 }
1116
Andrew Jeffery88412be2021-03-09 22:05:22 +10301117 assert(negotiated < ARRAY_SIZE(astlpc_protocol_version));
1118 astlpc->proto = &astlpc_protocol_version[negotiated];
Andrew Jeffery4e8264b2020-05-23 20:34:33 +09301119
Andrew Jeffery3a540662020-05-26 19:55:30 +09301120 rc = mctp_astlpc_layout_read(astlpc, &layout);
1121 if (rc < 0)
1122 return rc;
1123
Andrew Jeffery88412be2021-03-09 22:05:22 +10301124 if (!mctp_astlpc_layout_validate(astlpc, &layout)) {
Andrew Jeffery3a540662020-05-26 19:55:30 +09301125 mctp_prerr("BMC proposed invalid buffer parameters");
1126 return -EINVAL;
1127 }
1128
1129 astlpc->layout = layout;
1130
1131 if (negotiated >= 2)
1132 astlpc->binding.pkt_size =
Andrew Jeffery88412be2021-03-09 22:05:22 +10301133 astlpc->proto->body_size(astlpc->layout.tx.size);
Andrew Jeffery3a540662020-05-26 19:55:30 +09301134
Andrew Jefferyfe763e92022-08-05 23:16:17 +09301135 /* Track buffer ownership */
1136 astlpc->layout.tx.state = buffer_state_acquired;
1137 astlpc->layout.rx.state = buffer_state_released;
1138
Andrew Jeffery4e8264b2020-05-23 20:34:33 +09301139 return 0;
1140}
1141
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301142static int mctp_astlpc_update_channel(struct mctp_binding_astlpc *astlpc,
1143 uint8_t status)
1144{
1145 uint8_t updated;
1146 int rc = 0;
1147
1148 assert(astlpc->mode == MCTP_BINDING_ASTLPC_MODE_HOST);
1149
1150 updated = astlpc->kcs_status ^ status;
1151
Andrew Jefferyd0f5da02020-05-28 09:12:55 +09301152 astlpc_prdebug(astlpc, "%s: status: 0x%x, update: 0x%x", __func__,
1153 status, updated);
1154
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301155 if (updated & KCS_STATUS_BMC_READY) {
Andrew Jefferyd0f5da02020-05-28 09:12:55 +09301156 if (status & KCS_STATUS_BMC_READY) {
1157 astlpc->kcs_status = status;
1158 return astlpc->binding.start(&astlpc->binding);
1159 } else {
Andrew Jefferyfe763e92022-08-05 23:16:17 +09301160 /* Shut down the channel */
1161 astlpc->layout.rx.state = buffer_state_idle;
1162 astlpc->layout.tx.state = buffer_state_idle;
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301163 mctp_binding_set_tx_enabled(&astlpc->binding, false);
Andrew Jefferyd0f5da02020-05-28 09:12:55 +09301164 }
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301165 }
1166
Andrew Jeffery88412be2021-03-09 22:05:22 +10301167 if (astlpc->proto->version == 0 ||
Patrick Williamsa721c2d2022-12-04 14:30:26 -06001168 updated & KCS_STATUS_CHANNEL_ACTIVE) {
Andrew Jeffery4e8264b2020-05-23 20:34:33 +09301169 bool enable;
1170
Andrew Jefferyfe763e92022-08-05 23:16:17 +09301171 astlpc->layout.rx.state = buffer_state_idle;
1172 astlpc->layout.tx.state = buffer_state_idle;
Andrew Jeffery4e8264b2020-05-23 20:34:33 +09301173 rc = mctp_astlpc_finalise_channel(astlpc);
1174 enable = (status & KCS_STATUS_CHANNEL_ACTIVE) && rc == 0;
Andrew Jeffery4e8264b2020-05-23 20:34:33 +09301175 mctp_binding_set_tx_enabled(&astlpc->binding, enable);
1176 }
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301177
1178 astlpc->kcs_status = status;
1179
1180 return rc;
1181}
1182
Frederic Barrat4df106d2023-05-16 17:45:42 +02001183bool mctp_astlpc_tx_done(struct mctp_binding_astlpc *astlpc)
1184{
1185 return astlpc->layout.tx.state == buffer_state_acquired;
1186}
1187
Jeremy Kerr672c8852019-03-01 12:18:07 +08001188int mctp_astlpc_poll(struct mctp_binding_astlpc *astlpc)
1189{
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301190 uint8_t status, data;
Jeremy Kerr672c8852019-03-01 12:18:07 +08001191 int rc;
1192
Andrew Jefferyfe763e92022-08-05 23:16:17 +09301193 if (astlpc->layout.rx.state == buffer_state_prepared)
Rashmica Gupta45d13322022-08-10 12:12:14 +10001194 if (!mctp_astlpc_kcs_send(astlpc, cmd_rx_complete))
Andrew Jefferyfe763e92022-08-05 23:16:17 +09301195 astlpc->layout.rx.state = buffer_state_released;
1196
1197 if (astlpc->layout.tx.state == buffer_state_prepared)
Rashmica Gupta45d13322022-08-10 12:12:14 +10001198 if (!mctp_astlpc_kcs_send(astlpc, cmd_tx_begin))
Andrew Jefferyfe763e92022-08-05 23:16:17 +09301199 astlpc->layout.tx.state = buffer_state_released;
1200
Andrew Jefferyf13cb972020-05-28 09:30:09 +09301201 rc = mctp_astlpc_kcs_read(astlpc, MCTP_ASTLPC_KCS_REG_STATUS, &status);
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301202 if (rc) {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +09301203 astlpc_prwarn(astlpc, "KCS read error");
Jeremy Kerr672c8852019-03-01 12:18:07 +08001204 return -1;
Jeremy Kerr672c8852019-03-01 12:18:07 +08001205 }
1206
Andrew Jeffery9101a2a2020-05-22 16:08:03 +09301207 astlpc_prdebug(astlpc, "%s: status: 0x%hhx", __func__, status);
Andrew Jefferyedfe3832020-02-06 11:52:11 +10301208
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301209 if (!mctp_astlpc_kcs_read_ready(astlpc, status))
Jeremy Kerr672c8852019-03-01 12:18:07 +08001210 return 0;
1211
Andrew Jefferyf13cb972020-05-28 09:30:09 +09301212 rc = mctp_astlpc_kcs_read(astlpc, MCTP_ASTLPC_KCS_REG_DATA, &data);
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301213 if (rc) {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +09301214 astlpc_prwarn(astlpc, "KCS data read error");
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301215 return -1;
1216 }
1217
Andrew Jeffery9101a2a2020-05-22 16:08:03 +09301218 astlpc_prdebug(astlpc, "%s: data: 0x%hhx", __func__, data);
Andrew Jefferyedfe3832020-02-06 11:52:11 +10301219
Rashmica Gupta45d13322022-08-10 12:12:14 +10001220 if (!astlpc->proto->version &&
1221 !(data == cmd_initialise || data == cmd_dummy_value)) {
Andrew Jefferyafcb7012021-01-28 17:00:36 +10301222 astlpc_prwarn(astlpc, "Invalid message for binding state: 0x%x",
1223 data);
1224 return 0;
1225 }
1226
Jeremy Kerr672c8852019-03-01 12:18:07 +08001227 switch (data) {
Rashmica Gupta45d13322022-08-10 12:12:14 +10001228 case cmd_initialise:
Jeremy Kerr672c8852019-03-01 12:18:07 +08001229 mctp_astlpc_init_channel(astlpc);
1230 break;
Rashmica Gupta45d13322022-08-10 12:12:14 +10001231 case cmd_tx_begin:
Andrew Jefferyfe763e92022-08-05 23:16:17 +09301232 if (astlpc->layout.rx.state != buffer_state_released) {
Patrick Williamsa721c2d2022-12-04 14:30:26 -06001233 astlpc_prerr(
1234 astlpc,
1235 "Protocol error: Invalid Rx buffer state for event %d: %d\n",
1236 data, astlpc->layout.rx.state);
Andrew Jefferyfe763e92022-08-05 23:16:17 +09301237 return 0;
1238 }
Jeremy Kerr672c8852019-03-01 12:18:07 +08001239 mctp_astlpc_rx_start(astlpc);
1240 break;
Rashmica Gupta45d13322022-08-10 12:12:14 +10001241 case cmd_rx_complete:
Andrew Jefferyfe763e92022-08-05 23:16:17 +09301242 if (astlpc->layout.tx.state != buffer_state_released) {
Patrick Williamsa721c2d2022-12-04 14:30:26 -06001243 astlpc_prerr(
1244 astlpc,
1245 "Protocol error: Invalid Tx buffer state for event %d: %d\n",
1246 data, astlpc->layout.tx.state);
Andrew Jefferyfe763e92022-08-05 23:16:17 +09301247 return 0;
1248 }
Jeremy Kerr672c8852019-03-01 12:18:07 +08001249 mctp_astlpc_tx_complete(astlpc);
1250 break;
Rashmica Gupta45d13322022-08-10 12:12:14 +10001251 case cmd_dummy_value:
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301252 /* No responsibilities for the BMC on 0xff */
Andrew Jefferyd0f5da02020-05-28 09:12:55 +09301253 if (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_HOST) {
1254 rc = mctp_astlpc_update_channel(astlpc, status);
1255 if (rc < 0)
1256 return rc;
1257 }
1258 break;
Jeremy Kerr672c8852019-03-01 12:18:07 +08001259 default:
Andrew Jeffery9101a2a2020-05-22 16:08:03 +09301260 astlpc_prwarn(astlpc, "unknown message 0x%x", data);
Jeremy Kerr672c8852019-03-01 12:18:07 +08001261 }
Andrew Jefferyd0f5da02020-05-28 09:12:55 +09301262
1263 /* Handle silent loss of bmc-ready */
1264 if (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_HOST) {
Rashmica Gupta45d13322022-08-10 12:12:14 +10001265 if (!(status & KCS_STATUS_BMC_READY && data == cmd_dummy_value))
Andrew Jefferyd0f5da02020-05-28 09:12:55 +09301266 return mctp_astlpc_update_channel(astlpc, status);
1267 }
1268
1269 return rc;
Jeremy Kerr672c8852019-03-01 12:18:07 +08001270}
1271
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301272/* allocate and basic initialisation */
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301273static struct mctp_binding_astlpc *__mctp_astlpc_init(uint8_t mode,
1274 uint32_t mtu)
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301275{
1276 struct mctp_binding_astlpc *astlpc;
1277
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301278 assert((mode == MCTP_BINDING_ASTLPC_MODE_BMC) ||
1279 (mode == MCTP_BINDING_ASTLPC_MODE_HOST));
1280
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301281 astlpc = __mctp_alloc(sizeof(*astlpc));
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301282 if (!astlpc)
1283 return NULL;
1284
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301285 memset(astlpc, 0, sizeof(*astlpc));
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301286 astlpc->mode = mode;
1287 astlpc->lpc_map = NULL;
Andrew Jefferyfe763e92022-08-05 23:16:17 +09301288 astlpc->layout.rx.state = buffer_state_idle;
1289 astlpc->layout.tx.state = buffer_state_idle;
Andrew Jefferya9368982020-06-09 13:07:39 +09301290 astlpc->requested_mtu = mtu;
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301291 astlpc->binding.name = "astlpc";
1292 astlpc->binding.version = 1;
Andrew Jefferyeba19a32021-03-09 23:09:40 +10301293 astlpc->binding.pkt_header = 4;
1294 astlpc->binding.pkt_trailer = 4;
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301295 astlpc->binding.tx = mctp_binding_astlpc_tx;
1296 if (mode == MCTP_BINDING_ASTLPC_MODE_BMC)
1297 astlpc->binding.start = mctp_binding_astlpc_start_bmc;
1298 else if (mode == MCTP_BINDING_ASTLPC_MODE_HOST)
1299 astlpc->binding.start = mctp_binding_astlpc_start_host;
1300 else {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +09301301 astlpc_prerr(astlpc, "%s: Invalid mode: %d\n", __func__, mode);
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301302 __mctp_free(astlpc);
1303 return NULL;
1304 }
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301305
Matt Johnston4a09e1d2024-09-13 14:55:58 +08001306 if (mctp_astlpc_set_pkt_size(
1307 astlpc,
1308 MCTP_PACKET_SIZE(mtu > MCTP_BTU ? mtu : MCTP_BTU)) != 0) {
1309 astlpc_prerr(astlpc, "%s: Allocation error", __func__);
1310 __mctp_free(astlpc);
1311 return NULL;
1312 }
1313
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301314 return astlpc;
1315}
1316
Jeremy Kerr3b36d172019-09-04 11:56:09 +08001317struct mctp_binding *mctp_binding_astlpc_core(struct mctp_binding_astlpc *b)
1318{
1319 return &b->binding;
1320}
1321
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301322struct mctp_binding_astlpc *
1323mctp_astlpc_init(uint8_t mode, uint32_t mtu, void *lpc_map,
1324 const struct mctp_binding_astlpc_ops *ops, void *ops_data)
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301325{
1326 struct mctp_binding_astlpc *astlpc;
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301327
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301328 if (!(mode == MCTP_BINDING_ASTLPC_MODE_BMC ||
1329 mode == MCTP_BINDING_ASTLPC_MODE_HOST)) {
1330 mctp_prerr("Unknown binding mode: %u", mode);
1331 return NULL;
1332 }
1333
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301334 astlpc = __mctp_astlpc_init(mode, mtu);
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301335 if (!astlpc)
1336 return NULL;
1337
1338 memcpy(&astlpc->ops, ops, sizeof(astlpc->ops));
1339 astlpc->ops_data = ops_data;
1340 astlpc->lpc_map = lpc_map;
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301341 astlpc->mode = mode;
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301342
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301343 return astlpc;
1344}
1345
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301346struct mctp_binding_astlpc *
1347mctp_astlpc_init_ops(const struct mctp_binding_astlpc_ops *ops, void *ops_data,
1348 void *lpc_map)
1349{
1350 return mctp_astlpc_init(MCTP_BINDING_ASTLPC_MODE_BMC, MCTP_BTU, lpc_map,
1351 ops, ops_data);
1352}
1353
Andrew Jeffery4663f672020-03-10 23:36:24 +10301354void mctp_astlpc_destroy(struct mctp_binding_astlpc *astlpc)
1355{
Andrew Jefferyd0f5da02020-05-28 09:12:55 +09301356 /* Clear channel-active and bmc-ready */
1357 if (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_BMC)
Andrew Jeffery7c4509a2021-08-26 14:56:16 +09301358 mctp_astlpc_kcs_set_status(astlpc, 0);
Matt Johnston4a09e1d2024-09-13 14:55:58 +08001359 __mctp_free(astlpc->binding.tx_storage);
Andrew Jeffery4663f672020-03-10 23:36:24 +10301360 __mctp_free(astlpc);
1361}
1362
Jeremy Kerrb214c642019-11-27 11:34:00 +08001363#ifdef MCTP_HAVE_FILEIO
Andrew Jeffery55fb90b2020-05-12 13:54:37 +09301364
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301365static int mctp_astlpc_init_fileio_lpc(struct mctp_binding_astlpc *astlpc)
Jeremy Kerr672c8852019-03-01 12:18:07 +08001366{
1367 struct aspeed_lpc_ctrl_mapping map = {
1368 .window_type = ASPEED_LPC_CTRL_WINDOW_MEMORY,
1369 .window_id = 0, /* There's only one */
1370 .flags = 0,
1371 .addr = 0,
1372 .offset = 0,
1373 .size = 0
1374 };
Andrew Jeffery979c6a12020-05-23 20:04:49 +09301375 void *lpc_map_base;
Jeremy Kerr672c8852019-03-01 12:18:07 +08001376 int fd, rc;
1377
1378 fd = open(lpc_path, O_RDWR | O_SYNC);
1379 if (fd < 0) {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +09301380 astlpc_prwarn(astlpc, "LPC open (%s) failed", lpc_path);
Jeremy Kerr672c8852019-03-01 12:18:07 +08001381 return -1;
1382 }
1383
1384 rc = ioctl(fd, ASPEED_LPC_CTRL_IOCTL_GET_SIZE, &map);
1385 if (rc) {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +09301386 astlpc_prwarn(astlpc, "LPC GET_SIZE failed");
Jeremy Kerr672c8852019-03-01 12:18:07 +08001387 close(fd);
1388 return -1;
1389 }
1390
Andrew Jefferyc1d5c542021-10-27 15:25:48 +10301391 /*
1392 * 🚨🚨🚨
1393 *
1394 * Decouple ourselves from hiomapd[1] (another user of the FW2AHB) by
1395 * mapping the FW2AHB to the reserved memory here as well.
1396 *
1397 * It's not possible to use the MCTP ASTLPC binding on machines that
1398 * need the FW2AHB bridge mapped anywhere except to the reserved memory
1399 * (e.g. the host SPI NOR).
1400 *
1401 * [1] https://github.com/openbmc/hiomapd/
1402 *
1403 * 🚨🚨🚨
1404 *
1405 * The following calculation must align with what's going on in
1406 * hiomapd's lpc.c so as not to disrupt its behaviour:
1407 *
1408 * https://github.com/openbmc/hiomapd/blob/5ff50e3cbd7702aefc185264e4adfb9952040575/lpc.c#L68
1409 *
1410 * 🚨🚨🚨
1411 */
1412
1413 /* Map the reserved memory at the top of the 28-bit LPC firmware address space */
1414 map.addr = 0x0FFFFFFF & -map.size;
Patrick Williamsa721c2d2022-12-04 14:30:26 -06001415 astlpc_prinfo(
1416 astlpc,
1417 "Configuring FW2AHB to map reserved memory at 0x%08x for 0x%x in the LPC FW cycle address-space",
1418 map.addr, map.size);
Andrew Jefferyc1d5c542021-10-27 15:25:48 +10301419
1420 rc = ioctl(fd, ASPEED_LPC_CTRL_IOCTL_MAP, &map);
1421 if (rc) {
Patrick Williamsa721c2d2022-12-04 14:30:26 -06001422 astlpc_prwarn(astlpc,
1423 "Failed to map FW2AHB to reserved memory");
Andrew Jefferyc1d5c542021-10-27 15:25:48 +10301424 close(fd);
1425 return -1;
1426 }
1427
1428 /* Map the reserved memory into our address space */
Andrew Jeffery979c6a12020-05-23 20:04:49 +09301429 lpc_map_base =
1430 mmap(NULL, map.size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
1431 if (lpc_map_base == MAP_FAILED) {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +09301432 astlpc_prwarn(astlpc, "LPC mmap failed");
Jeremy Kerr672c8852019-03-01 12:18:07 +08001433 rc = -1;
1434 } else {
Matt Johnston3ef47782024-12-11 15:19:06 +08001435 astlpc->lpc_map =
1436 (uint8_t *)lpc_map_base + map.size - LPC_WIN_SIZE;
Jeremy Kerr672c8852019-03-01 12:18:07 +08001437 }
1438
1439 close(fd);
1440
1441 return rc;
1442}
1443
Konstantin Aladyshev400766f2023-09-07 15:57:25 +03001444static int mctp_astlpc_init_fileio_kcs(struct mctp_binding_astlpc *astlpc,
1445 const char *kcs_path)
Jeremy Kerr672c8852019-03-01 12:18:07 +08001446{
1447 astlpc->kcs_fd = open(kcs_path, O_RDWR);
1448 if (astlpc->kcs_fd < 0)
1449 return -1;
1450
1451 return 0;
1452}
1453
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301454static int __mctp_astlpc_fileio_kcs_read(void *arg,
Patrick Williamsa721c2d2022-12-04 14:30:26 -06001455 enum mctp_binding_astlpc_kcs_reg reg,
1456 uint8_t *val)
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301457{
1458 struct mctp_binding_astlpc *astlpc = arg;
1459 off_t offset = reg;
1460 int rc;
1461
1462 rc = pread(astlpc->kcs_fd, val, 1, offset);
1463
1464 return rc == 1 ? 0 : -1;
1465}
1466
1467static int __mctp_astlpc_fileio_kcs_write(void *arg,
Patrick Williamsa721c2d2022-12-04 14:30:26 -06001468 enum mctp_binding_astlpc_kcs_reg reg,
1469 uint8_t val)
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301470{
1471 struct mctp_binding_astlpc *astlpc = arg;
1472 off_t offset = reg;
1473 int rc;
1474
1475 rc = pwrite(astlpc->kcs_fd, &val, 1, offset);
1476
1477 return rc == 1 ? 0 : -1;
1478}
1479
Andrew Jeffery1111c6a2022-07-25 20:44:39 +09301480int mctp_astlpc_init_pollfd(struct mctp_binding_astlpc *astlpc,
1481 struct pollfd *pollfd)
1482{
Andrew Jefferyfe763e92022-08-05 23:16:17 +09301483 bool release;
1484
Andrew Jeffery1111c6a2022-07-25 20:44:39 +09301485 pollfd->fd = astlpc->kcs_fd;
Andrew Jefferyfe763e92022-08-05 23:16:17 +09301486 pollfd->events = 0;
1487
1488 release = astlpc->layout.rx.state == buffer_state_prepared ||
Patrick Williamsa721c2d2022-12-04 14:30:26 -06001489 astlpc->layout.tx.state == buffer_state_prepared;
Andrew Jefferyfe763e92022-08-05 23:16:17 +09301490
1491 pollfd->events = release ? POLLOUT : POLLIN;
Andrew Jeffery1111c6a2022-07-25 20:44:39 +09301492
1493 return 0;
1494}
1495
Konstantin Aladyshev400766f2023-09-07 15:57:25 +03001496struct mctp_binding_astlpc *mctp_astlpc_init_fileio(const char *kcs_path)
Jeremy Kerr672c8852019-03-01 12:18:07 +08001497{
1498 struct mctp_binding_astlpc *astlpc;
1499 int rc;
1500
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301501 /*
1502 * If we're doing file IO then we're very likely not running
Andrew Jeffery8877c462020-06-15 12:22:53 +09301503 * freestanding, so lets assume that we're on the BMC side.
1504 *
1505 * Requesting an MTU of 0 requests the largest possible MTU, whatever
1506 * value that might take.
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301507 */
Andrew Jeffery8877c462020-06-15 12:22:53 +09301508 astlpc = __mctp_astlpc_init(MCTP_BINDING_ASTLPC_MODE_BMC, 0);
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301509 if (!astlpc)
1510 return NULL;
Jeremy Kerr672c8852019-03-01 12:18:07 +08001511
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301512 /* Set internal operations for kcs. We use direct accesses to the lpc
1513 * map area */
1514 astlpc->ops.kcs_read = __mctp_astlpc_fileio_kcs_read;
1515 astlpc->ops.kcs_write = __mctp_astlpc_fileio_kcs_write;
1516 astlpc->ops_data = astlpc;
1517
1518 rc = mctp_astlpc_init_fileio_lpc(astlpc);
Jeremy Kerr672c8852019-03-01 12:18:07 +08001519 if (rc) {
1520 free(astlpc);
1521 return NULL;
1522 }
1523
Konstantin Aladyshev400766f2023-09-07 15:57:25 +03001524 rc = mctp_astlpc_init_fileio_kcs(astlpc, kcs_path);
Jeremy Kerr672c8852019-03-01 12:18:07 +08001525 if (rc) {
1526 free(astlpc);
1527 return NULL;
1528 }
1529
Jeremy Kerr672c8852019-03-01 12:18:07 +08001530 return astlpc;
1531}
Jeremy Kerr92a10a62019-08-28 16:55:54 +05301532#else
Konstantin Aladyshev400766f2023-09-07 15:57:25 +03001533struct mctp_binding_astlpc *
1534mctp_astlpc_init_fileio(const char *kcs_path __unused)
Jeremy Kerr92a10a62019-08-28 16:55:54 +05301535{
Andrew Jeffery5b2702d2022-07-25 16:21:43 +09301536 mctp_prlog(MCTP_LOG_ERR, "%s: Missing support for file IO", __func__);
Jeremy Kerr92a10a62019-08-28 16:55:54 +05301537 return NULL;
1538}
Jeremy Kerr672c8852019-03-01 12:18:07 +08001539
Andrew Jeffery1111c6a2022-07-25 20:44:39 +09301540int mctp_astlpc_init_pollfd(struct mctp_binding_astlpc *astlpc __unused,
1541 struct pollfd *pollfd __unused)
1542{
1543 mctp_prlog(MCTP_LOG_ERR, "%s: Missing support for file IO", __func__);
1544 return -1;
1545}
Jeremy Kerr92a10a62019-08-28 16:55:54 +05301546#endif