blob: 6de3e3701183255509da616db6f001f9bea39bfc [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
Andrew Jeffery3a540662020-05-26 19:55:30 +0930873static uint32_t mctp_astlpc_calculate_mtu(struct mctp_binding_astlpc *astlpc,
874 struct mctp_astlpc_layout *layout)
875{
Andrew Jeffery88412be2021-03-09 22:05:22 +1030876 uint32_t low, high, limit, rpkt;
Andrew Jeffery3a540662020-05-26 19:55:30 +0930877
878 /* Derive the largest MTU the BMC _can_ support */
879 low = MIN(astlpc->layout.rx.offset, astlpc->layout.tx.offset);
880 high = MAX(astlpc->layout.rx.offset, astlpc->layout.tx.offset);
881 limit = high - low;
882
Andrew Jefferya9368982020-06-09 13:07:39 +0930883 /* Determine the largest MTU the BMC _wants_ to support */
884 if (astlpc->requested_mtu) {
Andrew Jeffery88412be2021-03-09 22:05:22 +1030885 uint32_t rmtu = astlpc->requested_mtu;
Andrew Jefferya9368982020-06-09 13:07:39 +0930886
Andrew Jeffery88412be2021-03-09 22:05:22 +1030887 rpkt = astlpc->proto->packet_size(MCTP_PACKET_SIZE(rmtu));
888 limit = MIN(limit, rpkt);
Andrew Jefferya9368982020-06-09 13:07:39 +0930889 }
Andrew Jeffery3a540662020-05-26 19:55:30 +0930890
891 /* Determine the accepted MTU, applied both directions by convention */
Andrew Jeffery88412be2021-03-09 22:05:22 +1030892 rpkt = MIN(limit, layout->tx.size);
893 return MCTP_BODY_SIZE(astlpc->proto->body_size(rpkt));
Andrew Jeffery3a540662020-05-26 19:55:30 +0930894}
895
Andrew Jeffery88412be2021-03-09 22:05:22 +1030896static int mctp_astlpc_negotiate_layout_bmc(struct mctp_binding_astlpc *astlpc)
Andrew Jeffery3a540662020-05-26 19:55:30 +0930897{
898 struct mctp_astlpc_layout proposed, pending;
899 uint32_t sz, mtu;
900 int rc;
901
Andrew Jeffery88412be2021-03-09 22:05:22 +1030902 /* Do we have a valid protocol version? */
903 if (!astlpc->proto->version)
904 return -EINVAL;
905
Andrew Jeffery3a540662020-05-26 19:55:30 +0930906 /* Extract the host's proposed layout */
907 rc = mctp_astlpc_layout_read(astlpc, &proposed);
908 if (rc < 0)
909 return rc;
910
Andrew Jeffery88412be2021-03-09 22:05:22 +1030911 /* Do we have a reasonable layout? */
912 if (!mctp_astlpc_layout_validate(astlpc, &proposed))
Andrew Jeffery3a540662020-05-26 19:55:30 +0930913 return -EINVAL;
914
915 /* Negotiate the MTU */
916 mtu = mctp_astlpc_calculate_mtu(astlpc, &proposed);
Andrew Jeffery88412be2021-03-09 22:05:22 +1030917 sz = astlpc->proto->packet_size(MCTP_PACKET_SIZE(mtu));
Andrew Jeffery3a540662020-05-26 19:55:30 +0930918
919 /*
920 * Use symmetric MTUs by convention and to pass constraints in rx/tx
921 * functions
922 */
923 pending = astlpc->layout;
924 pending.tx.size = sz;
925 pending.rx.size = sz;
926
Andrew Jeffery88412be2021-03-09 22:05:22 +1030927 if (mctp_astlpc_layout_validate(astlpc, &pending)) {
Andrew Jeffery3a540662020-05-26 19:55:30 +0930928 /* We found a sensible Rx MTU, so honour it */
929 astlpc->layout = pending;
930
931 /* Enforce the negotiated MTU */
932 rc = mctp_astlpc_layout_write(astlpc, &astlpc->layout);
933 if (rc < 0)
934 return rc;
935
936 astlpc_prinfo(astlpc, "Negotiated an MTU of %" PRIu32 " bytes",
937 mtu);
938 } else {
939 astlpc_prwarn(astlpc, "MTU negotiation failed");
940 return -EINVAL;
941 }
942
Andrew Jeffery88412be2021-03-09 22:05:22 +1030943 if (astlpc->proto->version >= 2)
Andrew Jeffery3a540662020-05-26 19:55:30 +0930944 astlpc->binding.pkt_size = MCTP_PACKET_SIZE(mtu);
945
946 return 0;
947}
948
Jeremy Kerr672c8852019-03-01 12:18:07 +0800949static void mctp_astlpc_init_channel(struct mctp_binding_astlpc *astlpc)
950{
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930951 uint16_t negotiated, negotiated_be;
952 struct mctp_lpcmap_hdr hdr;
953 uint8_t status;
Andrew Jeffery3a540662020-05-26 19:55:30 +0930954 int rc;
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930955
956 mctp_astlpc_lpc_read(astlpc, &hdr, 0, sizeof(hdr));
957
958 /* Version negotiation */
Andrew Jeffery7f7fdc12023-05-12 15:56:47 +0930959 negotiated = mctp_astlpc_negotiate_version(ASTLPC_VER_MIN,
960 ASTLPC_VER_CUR,
961 be16toh(hdr.host_ver_min),
962 be16toh(hdr.host_ver_cur));
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930963
Andrew Jeffery88412be2021-03-09 22:05:22 +1030964 /* MTU negotiation requires knowing which protocol we'll use */
965 assert(negotiated < ARRAY_SIZE(astlpc_protocol_version));
966 astlpc->proto = &astlpc_protocol_version[negotiated];
967
Andrew Jeffery3a540662020-05-26 19:55:30 +0930968 /* Host Rx MTU negotiation: Failure terminates channel init */
Andrew Jeffery88412be2021-03-09 22:05:22 +1030969 rc = mctp_astlpc_negotiate_layout_bmc(astlpc);
Andrew Jeffery3a540662020-05-26 19:55:30 +0930970 if (rc < 0)
971 negotiated = ASTLPC_VER_BAD;
972
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930973 /* Populate the negotiated version */
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930974 negotiated_be = htobe16(negotiated);
975 mctp_astlpc_lpc_write(astlpc, &negotiated_be,
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930976 offsetof(struct mctp_lpcmap_hdr, negotiated_ver),
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930977 sizeof(negotiated_be));
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930978
Andrew Jefferyfe763e92022-08-05 23:16:17 +0930979 /* Track buffer ownership */
980 astlpc->layout.tx.state = buffer_state_acquired;
981 astlpc->layout.rx.state = buffer_state_released;
982
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930983 /* Finalise the configuration */
984 status = KCS_STATUS_BMC_READY | KCS_STATUS_OBF;
985 if (negotiated > 0) {
986 astlpc_prinfo(astlpc, "Negotiated binding version %" PRIu16,
987 negotiated);
988 status |= KCS_STATUS_CHANNEL_ACTIVE;
989 } else {
Andrew Jeffery88412be2021-03-09 22:05:22 +1030990 astlpc_prerr(astlpc, "Failed to initialise channel");
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930991 }
Jeremy Kerr672c8852019-03-01 12:18:07 +0800992
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930993 mctp_astlpc_kcs_set_status(astlpc, status);
994
995 mctp_binding_set_tx_enabled(&astlpc->binding,
996 status & KCS_STATUS_CHANNEL_ACTIVE);
Jeremy Kerr672c8852019-03-01 12:18:07 +0800997}
998
999static void mctp_astlpc_rx_start(struct mctp_binding_astlpc *astlpc)
1000{
1001 struct mctp_pktbuf *pkt;
Rashmica Guptaf447b4f2022-10-11 17:44:31 +11001002 struct mctp_hdr *hdr;
Andrew Jefferyeba19a32021-03-09 23:09:40 +10301003 uint32_t body, packet;
Jeremy Kerr672c8852019-03-01 12:18:07 +08001004
Andrew Jefferyeba19a32021-03-09 23:09:40 +10301005 mctp_astlpc_lpc_read(astlpc, &body, astlpc->layout.rx.offset,
1006 sizeof(body));
1007 body = be32toh(body);
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301008
Andrew Jefferyeba19a32021-03-09 23:09:40 +10301009 if (body > astlpc->proto->body_size(astlpc->layout.rx.size)) {
1010 astlpc_prwarn(astlpc, "invalid RX len 0x%x", body);
Jeremy Kerr672c8852019-03-01 12:18:07 +08001011 return;
1012 }
1013
Andrew Jeffery0f05b6c2022-11-15 11:37:50 +10301014 if ((size_t)body > astlpc->binding.pkt_size) {
Andrew Jefferyeba19a32021-03-09 23:09:40 +10301015 astlpc_prwarn(astlpc, "invalid RX len 0x%x", body);
Jeremy Kerr672c8852019-03-01 12:18:07 +08001016 return;
1017 }
1018
Andrew Jefferyeba19a32021-03-09 23:09:40 +10301019 /* Eliminate the medium-specific header that we just read */
1020 packet = astlpc->proto->packet_size(body) - 4;
1021 pkt = mctp_pktbuf_alloc(&astlpc->binding, packet);
Christian Geddesae59f4f2021-09-01 09:54:25 -05001022 if (!pkt) {
Patrick Williamsa721c2d2022-12-04 14:30:26 -06001023 astlpc_prwarn(astlpc, "unable to allocate pktbuf len 0x%x",
1024 packet);
Christian Geddesae59f4f2021-09-01 09:54:25 -05001025 return;
1026 }
Jeremy Kerr672c8852019-03-01 12:18:07 +08001027
Andrew Jefferyeba19a32021-03-09 23:09:40 +10301028 /*
1029 * Read payload and medium-specific trailer from immediately after the
1030 * medium-specific header.
1031 */
Andrew Jeffery55fb90b2020-05-12 13:54:37 +09301032 mctp_astlpc_lpc_read(astlpc, mctp_pktbuf_hdr(pkt),
Andrew Jefferyeba19a32021-03-09 23:09:40 +10301033 astlpc->layout.rx.offset + 4, packet);
Jeremy Kerr672c8852019-03-01 12:18:07 +08001034
Andrew Jefferyfe763e92022-08-05 23:16:17 +09301035 astlpc->layout.rx.state = buffer_state_prepared;
1036
Christian Geddesae59f4f2021-09-01 09:54:25 -05001037 /* Inform the other side of the MCTP interface that we have read
1038 * the packet off the bus before handling the contents of the packet.
1039 */
Rashmica Gupta45d13322022-08-10 12:12:14 +10001040 if (!mctp_astlpc_kcs_send(astlpc, cmd_rx_complete))
Andrew Jefferyfe763e92022-08-05 23:16:17 +09301041 astlpc->layout.rx.state = buffer_state_released;
Christian Geddesae59f4f2021-09-01 09:54:25 -05001042
Rashmica Guptaf447b4f2022-10-11 17:44:31 +11001043 hdr = mctp_pktbuf_hdr(pkt);
1044 if (hdr->ver != 1) {
1045 mctp_pktbuf_free(pkt);
1046 astlpc_prdebug(astlpc, "Dropped packet with invalid version");
1047 return;
1048 }
1049
Andrew Jefferyeba19a32021-03-09 23:09:40 +10301050 /*
1051 * v3 will validate the CRC32 in the medium-specific trailer and adjust
1052 * the packet size accordingly. On older protocols validation is a no-op
1053 * that always returns true.
1054 */
1055 if (astlpc->proto->pktbuf_validate(pkt)) {
1056 mctp_bus_rx(&astlpc->binding, pkt);
1057 } else {
1058 /* TODO: Drop any associated assembly */
1059 mctp_pktbuf_free(pkt);
1060 astlpc_prdebug(astlpc, "Dropped corrupt packet");
1061 }
Jeremy Kerr672c8852019-03-01 12:18:07 +08001062}
1063
1064static void mctp_astlpc_tx_complete(struct mctp_binding_astlpc *astlpc)
1065{
Andrew Jefferyfe763e92022-08-05 23:16:17 +09301066 astlpc->layout.tx.state = buffer_state_acquired;
Jeremy Kerr672c8852019-03-01 12:18:07 +08001067 mctp_binding_set_tx_enabled(&astlpc->binding, true);
1068}
1069
Andrew Jeffery4e8264b2020-05-23 20:34:33 +09301070static int mctp_astlpc_finalise_channel(struct mctp_binding_astlpc *astlpc)
1071{
Andrew Jeffery3a540662020-05-26 19:55:30 +09301072 struct mctp_astlpc_layout layout;
Andrew Jeffery4e8264b2020-05-23 20:34:33 +09301073 uint16_t negotiated;
1074 int rc;
1075
1076 rc = mctp_astlpc_lpc_read(astlpc, &negotiated,
1077 offsetof(struct mctp_lpcmap_hdr,
1078 negotiated_ver),
1079 sizeof(negotiated));
1080 if (rc < 0)
1081 return rc;
1082
1083 negotiated = be16toh(negotiated);
Andrew Jeffery88412be2021-03-09 22:05:22 +10301084 astlpc_prerr(astlpc, "Version negotiation got: %u", negotiated);
Andrew Jeffery4e8264b2020-05-23 20:34:33 +09301085
1086 if (negotiated == ASTLPC_VER_BAD || negotiated < ASTLPC_VER_MIN ||
1087 negotiated > ASTLPC_VER_CUR) {
1088 astlpc_prerr(astlpc, "Failed to negotiate version, got: %u\n",
1089 negotiated);
1090 return -EINVAL;
1091 }
1092
Andrew Jeffery88412be2021-03-09 22:05:22 +10301093 assert(negotiated < ARRAY_SIZE(astlpc_protocol_version));
1094 astlpc->proto = &astlpc_protocol_version[negotiated];
Andrew Jeffery4e8264b2020-05-23 20:34:33 +09301095
Andrew Jeffery3a540662020-05-26 19:55:30 +09301096 rc = mctp_astlpc_layout_read(astlpc, &layout);
1097 if (rc < 0)
1098 return rc;
1099
Andrew Jeffery88412be2021-03-09 22:05:22 +10301100 if (!mctp_astlpc_layout_validate(astlpc, &layout)) {
Andrew Jeffery3a540662020-05-26 19:55:30 +09301101 mctp_prerr("BMC proposed invalid buffer parameters");
1102 return -EINVAL;
1103 }
1104
1105 astlpc->layout = layout;
1106
1107 if (negotiated >= 2)
1108 astlpc->binding.pkt_size =
Andrew Jeffery88412be2021-03-09 22:05:22 +10301109 astlpc->proto->body_size(astlpc->layout.tx.size);
Andrew Jeffery3a540662020-05-26 19:55:30 +09301110
Andrew Jefferyfe763e92022-08-05 23:16:17 +09301111 /* Track buffer ownership */
1112 astlpc->layout.tx.state = buffer_state_acquired;
1113 astlpc->layout.rx.state = buffer_state_released;
1114
Andrew Jeffery4e8264b2020-05-23 20:34:33 +09301115 return 0;
1116}
1117
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301118static int mctp_astlpc_update_channel(struct mctp_binding_astlpc *astlpc,
1119 uint8_t status)
1120{
1121 uint8_t updated;
1122 int rc = 0;
1123
1124 assert(astlpc->mode == MCTP_BINDING_ASTLPC_MODE_HOST);
1125
1126 updated = astlpc->kcs_status ^ status;
1127
Andrew Jefferyd0f5da02020-05-28 09:12:55 +09301128 astlpc_prdebug(astlpc, "%s: status: 0x%x, update: 0x%x", __func__,
1129 status, updated);
1130
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301131 if (updated & KCS_STATUS_BMC_READY) {
Andrew Jefferyd0f5da02020-05-28 09:12:55 +09301132 if (status & KCS_STATUS_BMC_READY) {
1133 astlpc->kcs_status = status;
1134 return astlpc->binding.start(&astlpc->binding);
1135 } else {
Andrew Jefferyfe763e92022-08-05 23:16:17 +09301136 /* Shut down the channel */
1137 astlpc->layout.rx.state = buffer_state_idle;
1138 astlpc->layout.tx.state = buffer_state_idle;
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301139 mctp_binding_set_tx_enabled(&astlpc->binding, false);
Andrew Jefferyd0f5da02020-05-28 09:12:55 +09301140 }
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301141 }
1142
Andrew Jeffery88412be2021-03-09 22:05:22 +10301143 if (astlpc->proto->version == 0 ||
Patrick Williamsa721c2d2022-12-04 14:30:26 -06001144 updated & KCS_STATUS_CHANNEL_ACTIVE) {
Andrew Jeffery4e8264b2020-05-23 20:34:33 +09301145 bool enable;
1146
Andrew Jefferyfe763e92022-08-05 23:16:17 +09301147 astlpc->layout.rx.state = buffer_state_idle;
1148 astlpc->layout.tx.state = buffer_state_idle;
Andrew Jeffery4e8264b2020-05-23 20:34:33 +09301149 rc = mctp_astlpc_finalise_channel(astlpc);
1150 enable = (status & KCS_STATUS_CHANNEL_ACTIVE) && rc == 0;
Andrew Jeffery4e8264b2020-05-23 20:34:33 +09301151 mctp_binding_set_tx_enabled(&astlpc->binding, enable);
1152 }
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301153
1154 astlpc->kcs_status = status;
1155
1156 return rc;
1157}
1158
Frederic Barrat4df106d2023-05-16 17:45:42 +02001159bool mctp_astlpc_tx_done(struct mctp_binding_astlpc *astlpc)
1160{
1161 return astlpc->layout.tx.state == buffer_state_acquired;
1162}
1163
Jeremy Kerr672c8852019-03-01 12:18:07 +08001164int mctp_astlpc_poll(struct mctp_binding_astlpc *astlpc)
1165{
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301166 uint8_t status, data;
Jeremy Kerr672c8852019-03-01 12:18:07 +08001167 int rc;
1168
Andrew Jefferyfe763e92022-08-05 23:16:17 +09301169 if (astlpc->layout.rx.state == buffer_state_prepared)
Rashmica Gupta45d13322022-08-10 12:12:14 +10001170 if (!mctp_astlpc_kcs_send(astlpc, cmd_rx_complete))
Andrew Jefferyfe763e92022-08-05 23:16:17 +09301171 astlpc->layout.rx.state = buffer_state_released;
1172
1173 if (astlpc->layout.tx.state == buffer_state_prepared)
Rashmica Gupta45d13322022-08-10 12:12:14 +10001174 if (!mctp_astlpc_kcs_send(astlpc, cmd_tx_begin))
Andrew Jefferyfe763e92022-08-05 23:16:17 +09301175 astlpc->layout.tx.state = buffer_state_released;
1176
Andrew Jefferyf13cb972020-05-28 09:30:09 +09301177 rc = mctp_astlpc_kcs_read(astlpc, MCTP_ASTLPC_KCS_REG_STATUS, &status);
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301178 if (rc) {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +09301179 astlpc_prwarn(astlpc, "KCS read error");
Jeremy Kerr672c8852019-03-01 12:18:07 +08001180 return -1;
Jeremy Kerr672c8852019-03-01 12:18:07 +08001181 }
1182
Andrew Jeffery9101a2a2020-05-22 16:08:03 +09301183 astlpc_prdebug(astlpc, "%s: status: 0x%hhx", __func__, status);
Andrew Jefferyedfe3832020-02-06 11:52:11 +10301184
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301185 if (!mctp_astlpc_kcs_read_ready(astlpc, status))
Jeremy Kerr672c8852019-03-01 12:18:07 +08001186 return 0;
1187
Andrew Jefferyf13cb972020-05-28 09:30:09 +09301188 rc = mctp_astlpc_kcs_read(astlpc, MCTP_ASTLPC_KCS_REG_DATA, &data);
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301189 if (rc) {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +09301190 astlpc_prwarn(astlpc, "KCS data read error");
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301191 return -1;
1192 }
1193
Andrew Jeffery9101a2a2020-05-22 16:08:03 +09301194 astlpc_prdebug(astlpc, "%s: data: 0x%hhx", __func__, data);
Andrew Jefferyedfe3832020-02-06 11:52:11 +10301195
Rashmica Gupta45d13322022-08-10 12:12:14 +10001196 if (!astlpc->proto->version &&
1197 !(data == cmd_initialise || data == cmd_dummy_value)) {
Andrew Jefferyafcb7012021-01-28 17:00:36 +10301198 astlpc_prwarn(astlpc, "Invalid message for binding state: 0x%x",
1199 data);
1200 return 0;
1201 }
1202
Jeremy Kerr672c8852019-03-01 12:18:07 +08001203 switch (data) {
Rashmica Gupta45d13322022-08-10 12:12:14 +10001204 case cmd_initialise:
Jeremy Kerr672c8852019-03-01 12:18:07 +08001205 mctp_astlpc_init_channel(astlpc);
1206 break;
Rashmica Gupta45d13322022-08-10 12:12:14 +10001207 case cmd_tx_begin:
Andrew Jefferyfe763e92022-08-05 23:16:17 +09301208 if (astlpc->layout.rx.state != buffer_state_released) {
Patrick Williamsa721c2d2022-12-04 14:30:26 -06001209 astlpc_prerr(
1210 astlpc,
1211 "Protocol error: Invalid Rx buffer state for event %d: %d\n",
1212 data, astlpc->layout.rx.state);
Andrew Jefferyfe763e92022-08-05 23:16:17 +09301213 return 0;
1214 }
Jeremy Kerr672c8852019-03-01 12:18:07 +08001215 mctp_astlpc_rx_start(astlpc);
1216 break;
Rashmica Gupta45d13322022-08-10 12:12:14 +10001217 case cmd_rx_complete:
Andrew Jefferyfe763e92022-08-05 23:16:17 +09301218 if (astlpc->layout.tx.state != buffer_state_released) {
Patrick Williamsa721c2d2022-12-04 14:30:26 -06001219 astlpc_prerr(
1220 astlpc,
1221 "Protocol error: Invalid Tx buffer state for event %d: %d\n",
1222 data, astlpc->layout.tx.state);
Andrew Jefferyfe763e92022-08-05 23:16:17 +09301223 return 0;
1224 }
Jeremy Kerr672c8852019-03-01 12:18:07 +08001225 mctp_astlpc_tx_complete(astlpc);
1226 break;
Rashmica Gupta45d13322022-08-10 12:12:14 +10001227 case cmd_dummy_value:
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301228 /* No responsibilities for the BMC on 0xff */
Andrew Jefferyd0f5da02020-05-28 09:12:55 +09301229 if (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_HOST) {
1230 rc = mctp_astlpc_update_channel(astlpc, status);
1231 if (rc < 0)
1232 return rc;
1233 }
1234 break;
Jeremy Kerr672c8852019-03-01 12:18:07 +08001235 default:
Andrew Jeffery9101a2a2020-05-22 16:08:03 +09301236 astlpc_prwarn(astlpc, "unknown message 0x%x", data);
Jeremy Kerr672c8852019-03-01 12:18:07 +08001237 }
Andrew Jefferyd0f5da02020-05-28 09:12:55 +09301238
1239 /* Handle silent loss of bmc-ready */
1240 if (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_HOST) {
Rashmica Gupta45d13322022-08-10 12:12:14 +10001241 if (!(status & KCS_STATUS_BMC_READY && data == cmd_dummy_value))
Andrew Jefferyd0f5da02020-05-28 09:12:55 +09301242 return mctp_astlpc_update_channel(astlpc, status);
1243 }
1244
1245 return rc;
Jeremy Kerr672c8852019-03-01 12:18:07 +08001246}
1247
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301248/* allocate and basic initialisation */
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301249static struct mctp_binding_astlpc *__mctp_astlpc_init(uint8_t mode,
1250 uint32_t mtu)
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301251{
1252 struct mctp_binding_astlpc *astlpc;
1253
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301254 assert((mode == MCTP_BINDING_ASTLPC_MODE_BMC) ||
1255 (mode == MCTP_BINDING_ASTLPC_MODE_HOST));
1256
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301257 astlpc = __mctp_alloc(sizeof(*astlpc));
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301258 if (!astlpc)
1259 return NULL;
1260
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301261 memset(astlpc, 0, sizeof(*astlpc));
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301262 astlpc->mode = mode;
1263 astlpc->lpc_map = NULL;
Andrew Jefferyfe763e92022-08-05 23:16:17 +09301264 astlpc->layout.rx.state = buffer_state_idle;
1265 astlpc->layout.tx.state = buffer_state_idle;
Andrew Jefferya9368982020-06-09 13:07:39 +09301266 astlpc->requested_mtu = mtu;
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301267 astlpc->binding.name = "astlpc";
1268 astlpc->binding.version = 1;
Andrew Jeffery1a4f4412021-01-28 13:42:02 +10301269 astlpc->binding.pkt_size =
1270 MCTP_PACKET_SIZE(mtu > MCTP_BTU ? mtu : MCTP_BTU);
Andrew Jefferyeba19a32021-03-09 23:09:40 +10301271 astlpc->binding.pkt_header = 4;
1272 astlpc->binding.pkt_trailer = 4;
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301273 astlpc->binding.tx = mctp_binding_astlpc_tx;
1274 if (mode == MCTP_BINDING_ASTLPC_MODE_BMC)
1275 astlpc->binding.start = mctp_binding_astlpc_start_bmc;
1276 else if (mode == MCTP_BINDING_ASTLPC_MODE_HOST)
1277 astlpc->binding.start = mctp_binding_astlpc_start_host;
1278 else {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +09301279 astlpc_prerr(astlpc, "%s: Invalid mode: %d\n", __func__, mode);
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301280 __mctp_free(astlpc);
1281 return NULL;
1282 }
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301283
1284 return astlpc;
1285}
1286
Jeremy Kerr3b36d172019-09-04 11:56:09 +08001287struct mctp_binding *mctp_binding_astlpc_core(struct mctp_binding_astlpc *b)
1288{
1289 return &b->binding;
1290}
1291
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301292struct mctp_binding_astlpc *
1293mctp_astlpc_init(uint8_t mode, uint32_t mtu, void *lpc_map,
1294 const struct mctp_binding_astlpc_ops *ops, void *ops_data)
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301295{
1296 struct mctp_binding_astlpc *astlpc;
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301297
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301298 if (!(mode == MCTP_BINDING_ASTLPC_MODE_BMC ||
1299 mode == MCTP_BINDING_ASTLPC_MODE_HOST)) {
1300 mctp_prerr("Unknown binding mode: %u", mode);
1301 return NULL;
1302 }
1303
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301304 astlpc = __mctp_astlpc_init(mode, mtu);
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301305 if (!astlpc)
1306 return NULL;
1307
1308 memcpy(&astlpc->ops, ops, sizeof(astlpc->ops));
1309 astlpc->ops_data = ops_data;
1310 astlpc->lpc_map = lpc_map;
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301311 astlpc->mode = mode;
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301312
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301313 return astlpc;
1314}
1315
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301316struct mctp_binding_astlpc *
1317mctp_astlpc_init_ops(const struct mctp_binding_astlpc_ops *ops, void *ops_data,
1318 void *lpc_map)
1319{
1320 return mctp_astlpc_init(MCTP_BINDING_ASTLPC_MODE_BMC, MCTP_BTU, lpc_map,
1321 ops, ops_data);
1322}
1323
Andrew Jeffery4663f672020-03-10 23:36:24 +10301324void mctp_astlpc_destroy(struct mctp_binding_astlpc *astlpc)
1325{
Andrew Jefferyd0f5da02020-05-28 09:12:55 +09301326 /* Clear channel-active and bmc-ready */
1327 if (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_BMC)
Andrew Jeffery7c4509a2021-08-26 14:56:16 +09301328 mctp_astlpc_kcs_set_status(astlpc, 0);
Andrew Jeffery4663f672020-03-10 23:36:24 +10301329 __mctp_free(astlpc);
1330}
1331
Jeremy Kerrb214c642019-11-27 11:34:00 +08001332#ifdef MCTP_HAVE_FILEIO
Andrew Jeffery55fb90b2020-05-12 13:54:37 +09301333
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301334static int mctp_astlpc_init_fileio_lpc(struct mctp_binding_astlpc *astlpc)
Jeremy Kerr672c8852019-03-01 12:18:07 +08001335{
1336 struct aspeed_lpc_ctrl_mapping map = {
1337 .window_type = ASPEED_LPC_CTRL_WINDOW_MEMORY,
1338 .window_id = 0, /* There's only one */
1339 .flags = 0,
1340 .addr = 0,
1341 .offset = 0,
1342 .size = 0
1343 };
Andrew Jeffery979c6a12020-05-23 20:04:49 +09301344 void *lpc_map_base;
Jeremy Kerr672c8852019-03-01 12:18:07 +08001345 int fd, rc;
1346
1347 fd = open(lpc_path, O_RDWR | O_SYNC);
1348 if (fd < 0) {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +09301349 astlpc_prwarn(astlpc, "LPC open (%s) failed", lpc_path);
Jeremy Kerr672c8852019-03-01 12:18:07 +08001350 return -1;
1351 }
1352
1353 rc = ioctl(fd, ASPEED_LPC_CTRL_IOCTL_GET_SIZE, &map);
1354 if (rc) {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +09301355 astlpc_prwarn(astlpc, "LPC GET_SIZE failed");
Jeremy Kerr672c8852019-03-01 12:18:07 +08001356 close(fd);
1357 return -1;
1358 }
1359
Andrew Jefferyc1d5c542021-10-27 15:25:48 +10301360 /*
1361 * 🚨🚨🚨
1362 *
1363 * Decouple ourselves from hiomapd[1] (another user of the FW2AHB) by
1364 * mapping the FW2AHB to the reserved memory here as well.
1365 *
1366 * It's not possible to use the MCTP ASTLPC binding on machines that
1367 * need the FW2AHB bridge mapped anywhere except to the reserved memory
1368 * (e.g. the host SPI NOR).
1369 *
1370 * [1] https://github.com/openbmc/hiomapd/
1371 *
1372 * 🚨🚨🚨
1373 *
1374 * The following calculation must align with what's going on in
1375 * hiomapd's lpc.c so as not to disrupt its behaviour:
1376 *
1377 * https://github.com/openbmc/hiomapd/blob/5ff50e3cbd7702aefc185264e4adfb9952040575/lpc.c#L68
1378 *
1379 * 🚨🚨🚨
1380 */
1381
1382 /* Map the reserved memory at the top of the 28-bit LPC firmware address space */
1383 map.addr = 0x0FFFFFFF & -map.size;
Patrick Williamsa721c2d2022-12-04 14:30:26 -06001384 astlpc_prinfo(
1385 astlpc,
1386 "Configuring FW2AHB to map reserved memory at 0x%08x for 0x%x in the LPC FW cycle address-space",
1387 map.addr, map.size);
Andrew Jefferyc1d5c542021-10-27 15:25:48 +10301388
1389 rc = ioctl(fd, ASPEED_LPC_CTRL_IOCTL_MAP, &map);
1390 if (rc) {
Patrick Williamsa721c2d2022-12-04 14:30:26 -06001391 astlpc_prwarn(astlpc,
1392 "Failed to map FW2AHB to reserved memory");
Andrew Jefferyc1d5c542021-10-27 15:25:48 +10301393 close(fd);
1394 return -1;
1395 }
1396
1397 /* Map the reserved memory into our address space */
Andrew Jeffery979c6a12020-05-23 20:04:49 +09301398 lpc_map_base =
1399 mmap(NULL, map.size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
1400 if (lpc_map_base == MAP_FAILED) {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +09301401 astlpc_prwarn(astlpc, "LPC mmap failed");
Jeremy Kerr672c8852019-03-01 12:18:07 +08001402 rc = -1;
1403 } else {
Andrew Jeffery979c6a12020-05-23 20:04:49 +09301404 astlpc->lpc_map = lpc_map_base + map.size - LPC_WIN_SIZE;
Jeremy Kerr672c8852019-03-01 12:18:07 +08001405 }
1406
1407 close(fd);
1408
1409 return rc;
1410}
1411
Konstantin Aladyshev400766f2023-09-07 15:57:25 +03001412static int mctp_astlpc_init_fileio_kcs(struct mctp_binding_astlpc *astlpc,
1413 const char *kcs_path)
Jeremy Kerr672c8852019-03-01 12:18:07 +08001414{
1415 astlpc->kcs_fd = open(kcs_path, O_RDWR);
1416 if (astlpc->kcs_fd < 0)
1417 return -1;
1418
1419 return 0;
1420}
1421
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301422static int __mctp_astlpc_fileio_kcs_read(void *arg,
Patrick Williamsa721c2d2022-12-04 14:30:26 -06001423 enum mctp_binding_astlpc_kcs_reg reg,
1424 uint8_t *val)
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301425{
1426 struct mctp_binding_astlpc *astlpc = arg;
1427 off_t offset = reg;
1428 int rc;
1429
1430 rc = pread(astlpc->kcs_fd, val, 1, offset);
1431
1432 return rc == 1 ? 0 : -1;
1433}
1434
1435static int __mctp_astlpc_fileio_kcs_write(void *arg,
Patrick Williamsa721c2d2022-12-04 14:30:26 -06001436 enum mctp_binding_astlpc_kcs_reg reg,
1437 uint8_t val)
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301438{
1439 struct mctp_binding_astlpc *astlpc = arg;
1440 off_t offset = reg;
1441 int rc;
1442
1443 rc = pwrite(astlpc->kcs_fd, &val, 1, offset);
1444
1445 return rc == 1 ? 0 : -1;
1446}
1447
Andrew Jeffery1111c6a2022-07-25 20:44:39 +09301448int mctp_astlpc_init_pollfd(struct mctp_binding_astlpc *astlpc,
1449 struct pollfd *pollfd)
1450{
Andrew Jefferyfe763e92022-08-05 23:16:17 +09301451 bool release;
1452
Andrew Jeffery1111c6a2022-07-25 20:44:39 +09301453 pollfd->fd = astlpc->kcs_fd;
Andrew Jefferyfe763e92022-08-05 23:16:17 +09301454 pollfd->events = 0;
1455
1456 release = astlpc->layout.rx.state == buffer_state_prepared ||
Patrick Williamsa721c2d2022-12-04 14:30:26 -06001457 astlpc->layout.tx.state == buffer_state_prepared;
Andrew Jefferyfe763e92022-08-05 23:16:17 +09301458
1459 pollfd->events = release ? POLLOUT : POLLIN;
Andrew Jeffery1111c6a2022-07-25 20:44:39 +09301460
1461 return 0;
1462}
1463
Konstantin Aladyshev400766f2023-09-07 15:57:25 +03001464struct mctp_binding_astlpc *mctp_astlpc_init_fileio(const char *kcs_path)
Jeremy Kerr672c8852019-03-01 12:18:07 +08001465{
1466 struct mctp_binding_astlpc *astlpc;
1467 int rc;
1468
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301469 /*
1470 * If we're doing file IO then we're very likely not running
Andrew Jeffery8877c462020-06-15 12:22:53 +09301471 * freestanding, so lets assume that we're on the BMC side.
1472 *
1473 * Requesting an MTU of 0 requests the largest possible MTU, whatever
1474 * value that might take.
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301475 */
Andrew Jeffery8877c462020-06-15 12:22:53 +09301476 astlpc = __mctp_astlpc_init(MCTP_BINDING_ASTLPC_MODE_BMC, 0);
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301477 if (!astlpc)
1478 return NULL;
Jeremy Kerr672c8852019-03-01 12:18:07 +08001479
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301480 /* Set internal operations for kcs. We use direct accesses to the lpc
1481 * map area */
1482 astlpc->ops.kcs_read = __mctp_astlpc_fileio_kcs_read;
1483 astlpc->ops.kcs_write = __mctp_astlpc_fileio_kcs_write;
1484 astlpc->ops_data = astlpc;
1485
1486 rc = mctp_astlpc_init_fileio_lpc(astlpc);
Jeremy Kerr672c8852019-03-01 12:18:07 +08001487 if (rc) {
1488 free(astlpc);
1489 return NULL;
1490 }
1491
Konstantin Aladyshev400766f2023-09-07 15:57:25 +03001492 rc = mctp_astlpc_init_fileio_kcs(astlpc, kcs_path);
Jeremy Kerr672c8852019-03-01 12:18:07 +08001493 if (rc) {
1494 free(astlpc);
1495 return NULL;
1496 }
1497
Jeremy Kerr672c8852019-03-01 12:18:07 +08001498 return astlpc;
1499}
Jeremy Kerr92a10a62019-08-28 16:55:54 +05301500#else
Konstantin Aladyshev400766f2023-09-07 15:57:25 +03001501struct mctp_binding_astlpc *
1502mctp_astlpc_init_fileio(const char *kcs_path __unused)
Jeremy Kerr92a10a62019-08-28 16:55:54 +05301503{
Andrew Jeffery5b2702d2022-07-25 16:21:43 +09301504 mctp_prlog(MCTP_LOG_ERR, "%s: Missing support for file IO", __func__);
Jeremy Kerr92a10a62019-08-28 16:55:54 +05301505 return NULL;
1506}
Jeremy Kerr672c8852019-03-01 12:18:07 +08001507
Andrew Jeffery1111c6a2022-07-25 20:44:39 +09301508int mctp_astlpc_init_pollfd(struct mctp_binding_astlpc *astlpc __unused,
1509 struct pollfd *pollfd __unused)
1510{
1511 mctp_prlog(MCTP_LOG_ERR, "%s: Missing support for file IO", __func__);
1512 return -1;
1513}
Jeremy Kerr92a10a62019-08-28 16:55:54 +05301514#endif