blob: 455276d4a6e64f12d46c504afa728cbecb19d62f [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 */
39static const char *kcs_path = "/dev/mctp0";
40static const char *lpc_path = "/dev/aspeed-lpc-ctrl";
41
42#endif
43
Andrew Jefferyfe763e92022-08-05 23:16:17 +093044enum mctp_astlpc_buffer_state {
45 /*
46 * Prior to "Channel Ready" we mark the buffers as "idle" to catch illegal accesses. In this
47 * state neither side is considered the owner of the buffer.
48 *
49 * Upon "Channel Ready", each side transitions the buffers from the initial "idle" state
50 * to the following target states:
51 *
52 * Tx buffer: "acquired"
53 * Rx buffer: "released"
54 */
55 buffer_state_idle,
56
57 /*
58 * Beyond initialisation by "Channel Ready", buffers are in the "acquired" state once:
59 *
60 * 1. We dequeue a control command transferring the buffer to our ownership out of the KCS
61 * interface, and
62 * 2. We are yet to complete all of our required accesses to the buffer
63 *
64 * * The Tx buffer enters the "acquired" state when we dequeue the "Rx Complete" command
65 * * The Rx buffer enters the "acquired" state when we dequeue the "Tx Begin" command
66 *
67 * It is a failure of implementation if it's possible for both sides to simultaneously
68 * consider a buffer as "acquired".
69 */
70 buffer_state_acquired,
71
72 /*
73 * Buffers are in the "prepared" state when:
74 *
75 * 1. We have completed all of our required accesses (read or write) for the buffer, and
76 * 2. We have not yet successfully enqueued the control command to hand off ownership
77 */
78 buffer_state_prepared,
79
80 /*
81 * Beyond initialisation by "Channel Ready", buffers are in the "released" state once:
82 *
83 * 1. We successfully enqueue the control command transferring ownership to the remote
84 * side in to the KCS interface
85 *
86 * * The Tx buffer enters the "released" state when we enqueue the "Tx Begin" command
87 * * The Rx buffer enters the "released" state when we enqueue the "Rx Complete" command
88 *
89 * It may be the case that both sides simultaneously consider a buffer to be in the
90 * "released" state. However, if this is true, it must also be true that a buffer ownership
91 * transfer command has been enqueued in the KCS interface and is yet to be dequeued.
92 */
93 buffer_state_released,
94};
95
Andrew Jeffery7cd72f12020-05-12 20:27:59 +093096struct mctp_astlpc_buffer {
97 uint32_t offset;
98 uint32_t size;
Andrew Jefferyfe763e92022-08-05 23:16:17 +093099 enum mctp_astlpc_buffer_state state;
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930100};
101
102struct mctp_astlpc_layout {
103 struct mctp_astlpc_buffer rx;
104 struct mctp_astlpc_buffer tx;
105};
106
Andrew Jeffery88412be2021-03-09 22:05:22 +1030107struct mctp_astlpc_protocol {
108 uint16_t version;
109 uint32_t (*packet_size)(uint32_t body);
110 uint32_t (*body_size)(uint32_t packet);
Andrew Jefferyeba19a32021-03-09 23:09:40 +1030111 void (*pktbuf_protect)(struct mctp_pktbuf *pkt);
112 bool (*pktbuf_validate)(struct mctp_pktbuf *pkt);
Andrew Jeffery88412be2021-03-09 22:05:22 +1030113};
114
Jeremy Kerr672c8852019-03-01 12:18:07 +0800115struct mctp_binding_astlpc {
116 struct mctp_binding binding;
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530117
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930118 void *lpc_map;
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930119 struct mctp_astlpc_layout layout;
120
121 uint8_t mode;
Andrew Jefferya9368982020-06-09 13:07:39 +0930122 uint32_t requested_mtu;
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530123
Andrew Jeffery88412be2021-03-09 22:05:22 +1030124 const struct mctp_astlpc_protocol *proto;
125
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530126 /* direct ops data */
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930127 struct mctp_binding_astlpc_ops ops;
128 void *ops_data;
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530129
130 /* fileio ops data */
Andrew Jeffery979c6a12020-05-23 20:04:49 +0930131 int kcs_fd;
132 uint8_t kcs_status;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800133};
134
Jeremy Kerr672c8852019-03-01 12:18:07 +0800135#define binding_to_astlpc(b) \
136 container_of(b, struct mctp_binding_astlpc, binding)
137
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930138#define astlpc_prlog(ctx, lvl, fmt, ...) \
139 do { \
140 bool __bmc = ((ctx)->mode == MCTP_BINDING_ASTLPC_MODE_BMC); \
141 mctp_prlog(lvl, pr_fmt("%s: " fmt), __bmc ? "bmc" : "host", \
142 ##__VA_ARGS__); \
143 } while (0)
144
145#define astlpc_prerr(ctx, fmt, ...) \
146 astlpc_prlog(ctx, MCTP_LOG_ERR, fmt, ##__VA_ARGS__)
147#define astlpc_prwarn(ctx, fmt, ...) \
148 astlpc_prlog(ctx, MCTP_LOG_WARNING, fmt, ##__VA_ARGS__)
149#define astlpc_prinfo(ctx, fmt, ...) \
150 astlpc_prlog(ctx, MCTP_LOG_INFO, fmt, ##__VA_ARGS__)
151#define astlpc_prdebug(ctx, fmt, ...) \
152 astlpc_prlog(ctx, MCTP_LOG_DEBUG, fmt, ##__VA_ARGS__)
153
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930154/* clang-format off */
155#define ASTLPC_MCTP_MAGIC 0x4d435450
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930156#define ASTLPC_VER_BAD 0
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930157#define ASTLPC_VER_MIN 1
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930158
Andrew Jeffery3a540662020-05-26 19:55:30 +0930159/* Support testing of new binding protocols */
160#ifndef ASTLPC_VER_CUR
Andrew Jefferyeba19a32021-03-09 23:09:40 +1030161#define ASTLPC_VER_CUR 3
Andrew Jeffery3a540662020-05-26 19:55:30 +0930162#endif
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930163/* clang-format on */
Jeremy Kerr672c8852019-03-01 12:18:07 +0800164
Andrew Jeffery88412be2021-03-09 22:05:22 +1030165#ifndef ARRAY_SIZE
166#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
167#endif
168
169static uint32_t astlpc_packet_size_v1(uint32_t body)
170{
171 assert((body + 4) > body);
172
173 return body + 4;
174}
175
176static uint32_t astlpc_body_size_v1(uint32_t packet)
177{
178 assert((packet - 4) < packet);
179
180 return packet - 4;
181}
182
Andrew Jefferyeba19a32021-03-09 23:09:40 +1030183void astlpc_pktbuf_protect_v1(struct mctp_pktbuf *pkt)
184{
185 (void)pkt;
186}
187
188bool astlpc_pktbuf_validate_v1(struct mctp_pktbuf *pkt)
189{
190 (void)pkt;
191 return true;
192}
193
194static uint32_t astlpc_packet_size_v3(uint32_t body)
195{
196 assert((body + 4 + 4) > body);
197
198 return body + 4 + 4;
199}
200
201static uint32_t astlpc_body_size_v3(uint32_t packet)
202{
203 assert((packet - 4 - 4) < packet);
204
205 return packet - 4 - 4;
206}
207
208void astlpc_pktbuf_protect_v3(struct mctp_pktbuf *pkt)
209{
210 uint32_t code;
211
212 code = htobe32(crc32(mctp_pktbuf_hdr(pkt), mctp_pktbuf_size(pkt)));
213 mctp_prdebug("%s: 0x%" PRIx32, __func__, code);
214 mctp_pktbuf_push(pkt, &code, 4);
215}
216
217bool astlpc_pktbuf_validate_v3(struct mctp_pktbuf *pkt)
218{
219 uint32_t code;
220 void *check;
221
222 code = be32toh(crc32(mctp_pktbuf_hdr(pkt), mctp_pktbuf_size(pkt) - 4));
223 mctp_prdebug("%s: 0x%" PRIx32, __func__, code);
224 check = mctp_pktbuf_pop(pkt, 4);
225 return check && !memcmp(&code, check, 4);
226}
227
Andrew Jeffery88412be2021-03-09 22:05:22 +1030228static const struct mctp_astlpc_protocol astlpc_protocol_version[] = {
229 [0] = {
230 .version = 0,
231 .packet_size = NULL,
232 .body_size = NULL,
Andrew Jefferyeba19a32021-03-09 23:09:40 +1030233 .pktbuf_protect = NULL,
234 .pktbuf_validate = NULL,
Andrew Jeffery88412be2021-03-09 22:05:22 +1030235 },
236 [1] = {
237 .version = 1,
238 .packet_size = astlpc_packet_size_v1,
239 .body_size = astlpc_body_size_v1,
Andrew Jefferyeba19a32021-03-09 23:09:40 +1030240 .pktbuf_protect = astlpc_pktbuf_protect_v1,
241 .pktbuf_validate = astlpc_pktbuf_validate_v1,
Andrew Jeffery88412be2021-03-09 22:05:22 +1030242 },
243 [2] = {
244 .version = 2,
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,
249 },
250 [3] = {
251 .version = 3,
252 .packet_size = astlpc_packet_size_v3,
253 .body_size = astlpc_body_size_v3,
254 .pktbuf_protect = astlpc_pktbuf_protect_v3,
255 .pktbuf_validate = astlpc_pktbuf_validate_v3,
Andrew Jeffery88412be2021-03-09 22:05:22 +1030256 },
257};
258
Jeremy Kerr672c8852019-03-01 12:18:07 +0800259struct mctp_lpcmap_hdr {
Andrew Jeffery3a540662020-05-26 19:55:30 +0930260 uint32_t magic;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800261
Andrew Jeffery3a540662020-05-26 19:55:30 +0930262 uint16_t bmc_ver_min;
263 uint16_t bmc_ver_cur;
264 uint16_t host_ver_min;
265 uint16_t host_ver_cur;
266 uint16_t negotiated_ver;
267 uint16_t pad0;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800268
Andrew Jeffery3a540662020-05-26 19:55:30 +0930269 struct {
270 uint32_t rx_offset;
271 uint32_t rx_size;
272 uint32_t tx_offset;
273 uint32_t tx_size;
274 } layout;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800275} __attribute__((packed));
276
Andrew Jeffery3a540662020-05-26 19:55:30 +0930277static const uint32_t control_size = 0x100;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800278
Jeremy Kerr672c8852019-03-01 12:18:07 +0800279#define LPC_WIN_SIZE (1 * 1024 * 1024)
280
Jeremy Kerr672c8852019-03-01 12:18:07 +0800281#define KCS_STATUS_BMC_READY 0x80
282#define KCS_STATUS_CHANNEL_ACTIVE 0x40
283#define KCS_STATUS_IBF 0x02
284#define KCS_STATUS_OBF 0x01
285
Andrew Jefferyf13cb972020-05-28 09:30:09 +0930286static inline int mctp_astlpc_kcs_write(struct mctp_binding_astlpc *astlpc,
287 enum mctp_binding_astlpc_kcs_reg reg,
288 uint8_t val)
289{
290 return astlpc->ops.kcs_write(astlpc->ops_data, reg, val);
291}
292
293static inline int mctp_astlpc_kcs_read(struct mctp_binding_astlpc *astlpc,
294 enum mctp_binding_astlpc_kcs_reg reg,
295 uint8_t *val)
296{
297 return astlpc->ops.kcs_read(astlpc->ops_data, reg, val);
298}
299
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930300static inline int mctp_astlpc_lpc_write(struct mctp_binding_astlpc *astlpc,
301 const void *buf, long offset,
302 size_t len)
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530303{
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930304 astlpc_prdebug(astlpc, "%s: %zu bytes to 0x%lx", __func__, len, offset);
305
306 assert(offset >= 0);
307
308 /* Indirect access */
309 if (astlpc->ops.lpc_write) {
310 void *data = astlpc->ops_data;
311
312 return astlpc->ops.lpc_write(data, buf, offset, len);
313 }
314
315 /* Direct mapping */
316 assert(astlpc->lpc_map);
317 memcpy(&((char *)astlpc->lpc_map)[offset], buf, len);
318
319 return 0;
320}
321
322static inline int mctp_astlpc_lpc_read(struct mctp_binding_astlpc *astlpc,
323 void *buf, long offset, size_t len)
324{
325 astlpc_prdebug(astlpc, "%s: %zu bytes from 0x%lx", __func__, len,
326 offset);
327
328 assert(offset >= 0);
329
330 /* Indirect access */
331 if (astlpc->ops.lpc_read) {
332 void *data = astlpc->ops_data;
333
334 return astlpc->ops.lpc_read(data, buf, offset, len);
335 }
336
337 /* Direct mapping */
338 assert(astlpc->lpc_map);
339 memcpy(buf, &((char *)astlpc->lpc_map)[offset], len);
340
341 return 0;
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530342}
343
Andrew Jefferyd0f5da02020-05-28 09:12:55 +0930344static int mctp_astlpc_kcs_set_status(struct mctp_binding_astlpc *astlpc,
345 uint8_t status)
346{
347 uint8_t data;
348 int rc;
349
350 /* Since we're setting the status register, we want the other endpoint
351 * to be interrupted. However, some hardware may only raise a host-side
352 * interrupt on an ODR event.
353 * So, write a dummy value of 0xff to ODR, which will ensure that an
354 * interrupt is triggered, and can be ignored by the host.
355 */
356 data = 0xff;
Andrew Jefferyd0f5da02020-05-28 09:12:55 +0930357
Andrew Jefferyf13cb972020-05-28 09:30:09 +0930358 rc = mctp_astlpc_kcs_write(astlpc, MCTP_ASTLPC_KCS_REG_STATUS, status);
Andrew Jefferyd0f5da02020-05-28 09:12:55 +0930359 if (rc) {
360 astlpc_prwarn(astlpc, "KCS status write failed");
361 return -1;
362 }
363
Andrew Jefferyf13cb972020-05-28 09:30:09 +0930364 rc = mctp_astlpc_kcs_write(astlpc, MCTP_ASTLPC_KCS_REG_DATA, data);
Andrew Jefferyd0f5da02020-05-28 09:12:55 +0930365 if (rc) {
366 astlpc_prwarn(astlpc, "KCS dummy data write failed");
367 return -1;
368 }
369
370 return 0;
371}
372
Andrew Jeffery3a540662020-05-26 19:55:30 +0930373static int mctp_astlpc_layout_read(struct mctp_binding_astlpc *astlpc,
374 struct mctp_astlpc_layout *layout)
375{
376 struct mctp_lpcmap_hdr hdr;
377 int rc;
378
379 rc = mctp_astlpc_lpc_read(astlpc, &hdr, 0, sizeof(hdr));
380 if (rc < 0)
381 return rc;
382
383 /* Flip the buffers as the names are defined in terms of the host */
384 if (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_BMC) {
385 layout->rx.offset = be32toh(hdr.layout.tx_offset);
386 layout->rx.size = be32toh(hdr.layout.tx_size);
387 layout->tx.offset = be32toh(hdr.layout.rx_offset);
388 layout->tx.size = be32toh(hdr.layout.rx_size);
389 } else {
390 assert(astlpc->mode == MCTP_BINDING_ASTLPC_MODE_HOST);
391
392 layout->rx.offset = be32toh(hdr.layout.rx_offset);
393 layout->rx.size = be32toh(hdr.layout.rx_size);
394 layout->tx.offset = be32toh(hdr.layout.tx_offset);
395 layout->tx.size = be32toh(hdr.layout.tx_size);
396 }
397
398 return 0;
399}
400
401static int mctp_astlpc_layout_write(struct mctp_binding_astlpc *astlpc,
402 struct mctp_astlpc_layout *layout)
403{
404 uint32_t rx_size_be;
405
406 if (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_BMC) {
407 struct mctp_lpcmap_hdr hdr;
408
409 /*
410 * Flip the buffers as the names are defined in terms of the
411 * host
412 */
413 hdr.layout.rx_offset = htobe32(layout->tx.offset);
414 hdr.layout.rx_size = htobe32(layout->tx.size);
415 hdr.layout.tx_offset = htobe32(layout->rx.offset);
416 hdr.layout.tx_size = htobe32(layout->rx.size);
417
418 return mctp_astlpc_lpc_write(astlpc, &hdr.layout,
419 offsetof(struct mctp_lpcmap_hdr, layout),
420 sizeof(hdr.layout));
421 }
422
423 assert(astlpc->mode == MCTP_BINDING_ASTLPC_MODE_HOST);
424
425 /*
426 * As of v2 we only need to write rx_size - the offsets are controlled
427 * by the BMC, as is the BMC's rx_size (host tx_size).
428 */
429 rx_size_be = htobe32(layout->rx.size);
430 return mctp_astlpc_lpc_write(astlpc, &rx_size_be,
431 offsetof(struct mctp_lpcmap_hdr, layout.rx_size),
432 sizeof(rx_size_be));
433}
434
Andrew Jeffery88412be2021-03-09 22:05:22 +1030435static bool
436mctp_astlpc_buffer_validate(const struct mctp_binding_astlpc *astlpc,
437 const struct mctp_astlpc_buffer *buf,
438 const char *name)
Andrew Jeffery3a540662020-05-26 19:55:30 +0930439{
440 /* Check for overflow */
441 if (buf->offset + buf->size < buf->offset) {
442 mctp_prerr(
443 "%s packet buffer parameters overflow: offset: 0x%" PRIx32
444 ", size: %" PRIu32,
445 name, buf->offset, buf->size);
446 return false;
447 }
448
449 /* Check that the buffers are contained within the allocated space */
450 if (buf->offset + buf->size > LPC_WIN_SIZE) {
451 mctp_prerr(
452 "%s packet buffer parameters exceed %uM window size: offset: 0x%" PRIx32
453 ", size: %" PRIu32,
454 name, (LPC_WIN_SIZE / (1024 * 1024)), buf->offset,
455 buf->size);
456 return false;
457 }
458
459 /* Check that the baseline transmission unit is supported */
Andrew Jeffery88412be2021-03-09 22:05:22 +1030460 if (buf->size < astlpc->proto->packet_size(MCTP_PACKET_SIZE(MCTP_BTU))) {
Andrew Jeffery3a540662020-05-26 19:55:30 +0930461 mctp_prerr(
Andrew Jeffery88412be2021-03-09 22:05:22 +1030462 "%s packet buffer too small: Require %" PRIu32 " bytes to support the %u byte baseline transmission unit, found %" PRIu32,
463 name,
464 astlpc->proto->packet_size(MCTP_PACKET_SIZE(MCTP_BTU)),
Andrew Jeffery3a540662020-05-26 19:55:30 +0930465 MCTP_BTU, buf->size);
466 return false;
467 }
468
469 /* Check for overlap with the control space */
470 if (buf->offset < control_size) {
471 mctp_prerr(
472 "%s packet buffer overlaps control region {0x%" PRIx32
473 ", %" PRIu32 "}: Rx {0x%" PRIx32 ", %" PRIu32 "}",
474 name, 0U, control_size, buf->offset, buf->size);
475 return false;
476 }
477
478 return true;
479}
480
Andrew Jeffery88412be2021-03-09 22:05:22 +1030481static bool
482mctp_astlpc_layout_validate(const struct mctp_binding_astlpc *astlpc,
483 const struct mctp_astlpc_layout *layout)
Andrew Jeffery3a540662020-05-26 19:55:30 +0930484{
Andrew Jeffery88412be2021-03-09 22:05:22 +1030485 const struct mctp_astlpc_buffer *rx = &layout->rx;
486 const struct mctp_astlpc_buffer *tx = &layout->tx;
Andrew Jeffery3a540662020-05-26 19:55:30 +0930487 bool rx_valid, tx_valid;
488
Andrew Jeffery88412be2021-03-09 22:05:22 +1030489 rx_valid = mctp_astlpc_buffer_validate(astlpc, rx, "Rx");
490 tx_valid = mctp_astlpc_buffer_validate(astlpc, tx, "Tx");
Andrew Jeffery3a540662020-05-26 19:55:30 +0930491
492 if (!(rx_valid && tx_valid))
493 return false;
494
495 /* Check that the buffers are disjoint */
496 if ((rx->offset <= tx->offset && rx->offset + rx->size > tx->offset) ||
497 (tx->offset <= rx->offset && tx->offset + tx->size > rx->offset)) {
498 mctp_prerr("Rx and Tx packet buffers overlap: Rx {0x%" PRIx32
499 ", %" PRIu32 "}, Tx {0x%" PRIx32 ", %" PRIu32 "}",
500 rx->offset, rx->size, tx->offset, tx->size);
501 return false;
502 }
503
504 return true;
505}
506
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930507static int mctp_astlpc_init_bmc(struct mctp_binding_astlpc *astlpc)
508{
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930509 struct mctp_lpcmap_hdr hdr = { 0 };
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930510 uint8_t status;
Andrew Jeffery88412be2021-03-09 22:05:22 +1030511 uint32_t sz;
Andrew Jeffery3a540662020-05-26 19:55:30 +0930512
513 /*
514 * The largest buffer size is half of the allocated MCTP space
515 * excluding the control space.
516 */
517 sz = ((LPC_WIN_SIZE - control_size) / 2);
518
519 /*
520 * Trim the MTU to a multiple of 16 to meet the requirements of 12.17
521 * Query Hop in DSP0236 v1.3.0.
522 */
Andrew Jeffery88412be2021-03-09 22:05:22 +1030523 sz = MCTP_BODY_SIZE(astlpc->proto->body_size(sz));
Andrew Jeffery3a540662020-05-26 19:55:30 +0930524 sz &= ~0xfUL;
Andrew Jeffery88412be2021-03-09 22:05:22 +1030525 sz = astlpc->proto->packet_size(MCTP_PACKET_SIZE(sz));
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930526
Andrew Jefferya9368982020-06-09 13:07:39 +0930527 if (astlpc->requested_mtu) {
Andrew Jeffery88412be2021-03-09 22:05:22 +1030528 uint32_t rpkt, rmtu;
Andrew Jefferya9368982020-06-09 13:07:39 +0930529
Andrew Jeffery88412be2021-03-09 22:05:22 +1030530 rmtu = astlpc->requested_mtu;
531 rpkt = astlpc->proto->packet_size(MCTP_PACKET_SIZE(rmtu));
532 sz = MIN(sz, rpkt);
Andrew Jefferya9368982020-06-09 13:07:39 +0930533 }
534
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930535 /* Flip the buffers as the names are defined in terms of the host */
Andrew Jeffery3a540662020-05-26 19:55:30 +0930536 astlpc->layout.tx.offset = control_size;
537 astlpc->layout.tx.size = sz;
538 astlpc->layout.rx.offset =
539 astlpc->layout.tx.offset + astlpc->layout.tx.size;
540 astlpc->layout.rx.size = sz;
541
Andrew Jeffery88412be2021-03-09 22:05:22 +1030542 if (!mctp_astlpc_layout_validate(astlpc, &astlpc->layout)) {
543 astlpc_prerr(astlpc, "Cannot support an MTU of %" PRIu32, sz);
Andrew Jefferya9368982020-06-09 13:07:39 +0930544 return -EINVAL;
545 }
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930546
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930547 hdr = (struct mctp_lpcmap_hdr){
548 .magic = htobe32(ASTLPC_MCTP_MAGIC),
549 .bmc_ver_min = htobe16(ASTLPC_VER_MIN),
550 .bmc_ver_cur = htobe16(ASTLPC_VER_CUR),
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930551
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930552 /* Flip the buffers back as we're now describing the host's
553 * configuration to the host */
Andrew Jeffery3a540662020-05-26 19:55:30 +0930554 .layout.rx_offset = htobe32(astlpc->layout.tx.offset),
555 .layout.rx_size = htobe32(astlpc->layout.tx.size),
556 .layout.tx_offset = htobe32(astlpc->layout.rx.offset),
557 .layout.tx_size = htobe32(astlpc->layout.rx.size),
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930558 };
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930559
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930560 mctp_astlpc_lpc_write(astlpc, &hdr, 0, sizeof(hdr));
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930561
Andrew Jefferyb3b55a62020-07-06 13:34:18 +0930562 /*
563 * Set status indicating that the BMC is now active. Be explicit about
564 * clearing OBF; we're reinitialising the binding and so any previous
565 * buffer state is irrelevant.
566 */
567 status = KCS_STATUS_BMC_READY & ~KCS_STATUS_OBF;
Andrew Jefferyd0f5da02020-05-28 09:12:55 +0930568 return mctp_astlpc_kcs_set_status(astlpc, status);
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930569}
570
571static int mctp_binding_astlpc_start_bmc(struct mctp_binding *b)
572{
573 struct mctp_binding_astlpc *astlpc =
574 container_of(b, struct mctp_binding_astlpc, binding);
575
Andrew Jeffery88412be2021-03-09 22:05:22 +1030576 astlpc->proto = &astlpc_protocol_version[ASTLPC_VER_CUR];
577
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930578 return mctp_astlpc_init_bmc(astlpc);
579}
580
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930581static bool mctp_astlpc_validate_version(uint16_t bmc_ver_min,
582 uint16_t bmc_ver_cur,
583 uint16_t host_ver_min,
584 uint16_t host_ver_cur)
585{
586 if (!(bmc_ver_min && bmc_ver_cur && host_ver_min && host_ver_cur)) {
587 mctp_prerr("Invalid version present in [%" PRIu16 ", %" PRIu16
588 "], [%" PRIu16 ", %" PRIu16 "]",
589 bmc_ver_min, bmc_ver_cur, host_ver_min,
590 host_ver_cur);
591 return false;
592 } else if (bmc_ver_min > bmc_ver_cur) {
593 mctp_prerr("Invalid bmc version range [%" PRIu16 ", %" PRIu16
594 "]",
595 bmc_ver_min, bmc_ver_cur);
596 return false;
597 } else if (host_ver_min > host_ver_cur) {
598 mctp_prerr("Invalid host version range [%" PRIu16 ", %" PRIu16
599 "]",
600 host_ver_min, host_ver_cur);
601 return false;
602 } else if ((host_ver_cur < bmc_ver_min) ||
603 (host_ver_min > bmc_ver_cur)) {
604 mctp_prerr(
605 "Unable to satisfy version negotiation with ranges [%" PRIu16
606 ", %" PRIu16 "] and [%" PRIu16 ", %" PRIu16 "]",
607 bmc_ver_min, bmc_ver_cur, host_ver_min, host_ver_cur);
608 return false;
609 }
610
611 return true;
612}
613
Andrew Jeffery3a540662020-05-26 19:55:30 +0930614static int mctp_astlpc_negotiate_layout_host(struct mctp_binding_astlpc *astlpc)
615{
616 struct mctp_astlpc_layout layout;
Andrew Jeffery88412be2021-03-09 22:05:22 +1030617 uint32_t rmtu;
Andrew Jeffery3a540662020-05-26 19:55:30 +0930618 uint32_t sz;
619 int rc;
620
621 rc = mctp_astlpc_layout_read(astlpc, &layout);
622 if (rc < 0)
623 return rc;
624
Andrew Jeffery88412be2021-03-09 22:05:22 +1030625 if (!mctp_astlpc_layout_validate(astlpc, &layout)) {
Andrew Jeffery3a540662020-05-26 19:55:30 +0930626 astlpc_prerr(
627 astlpc,
628 "BMC provided invalid buffer layout: Rx {0x%" PRIx32
629 ", %" PRIu32 "}, Tx {0x%" PRIx32 ", %" PRIu32 "}",
630 layout.rx.offset, layout.rx.size, layout.tx.offset,
631 layout.tx.size);
632 return -EINVAL;
633 }
634
Andrew Jefferya9368982020-06-09 13:07:39 +0930635 astlpc_prinfo(astlpc, "Desire an MTU of %" PRIu32 " bytes",
636 astlpc->requested_mtu);
637
Andrew Jeffery88412be2021-03-09 22:05:22 +1030638 rmtu = astlpc->requested_mtu;
639 sz = astlpc->proto->packet_size(MCTP_PACKET_SIZE(rmtu));
Andrew Jeffery3a540662020-05-26 19:55:30 +0930640 layout.rx.size = sz;
641
Andrew Jeffery88412be2021-03-09 22:05:22 +1030642 if (!mctp_astlpc_layout_validate(astlpc, &layout)) {
Andrew Jeffery3a540662020-05-26 19:55:30 +0930643 astlpc_prerr(
644 astlpc,
645 "Generated invalid buffer layout with size %" PRIu32
646 ": Rx {0x%" PRIx32 ", %" PRIu32 "}, Tx {0x%" PRIx32
647 ", %" PRIu32 "}",
648 sz, layout.rx.offset, layout.rx.size, layout.tx.offset,
649 layout.tx.size);
650 return -EINVAL;
651 }
652
Andrew Jefferya9368982020-06-09 13:07:39 +0930653 astlpc_prinfo(astlpc, "Requesting MTU of %" PRIu32 " bytes",
654 astlpc->requested_mtu);
Andrew Jeffery3a540662020-05-26 19:55:30 +0930655
656 return mctp_astlpc_layout_write(astlpc, &layout);
657}
658
Andrew Jeffery88412be2021-03-09 22:05:22 +1030659static uint16_t mctp_astlpc_negotiate_version(uint16_t bmc_ver_min,
660 uint16_t bmc_ver_cur,
661 uint16_t host_ver_min,
662 uint16_t host_ver_cur)
663{
664 if (!mctp_astlpc_validate_version(bmc_ver_min, bmc_ver_cur,
665 host_ver_min, host_ver_cur))
666 return ASTLPC_VER_BAD;
667
668 if (bmc_ver_cur < host_ver_cur)
669 return bmc_ver_cur;
670
671 return host_ver_cur;
672}
673
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930674static int mctp_astlpc_init_host(struct mctp_binding_astlpc *astlpc)
675{
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930676 const uint16_t ver_min_be = htobe16(ASTLPC_VER_MIN);
677 const uint16_t ver_cur_be = htobe16(ASTLPC_VER_CUR);
Andrew Jeffery88412be2021-03-09 22:05:22 +1030678 uint16_t bmc_ver_min, bmc_ver_cur, negotiated;
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930679 struct mctp_lpcmap_hdr hdr;
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930680 uint8_t status;
681 int rc;
682
Andrew Jefferyf13cb972020-05-28 09:30:09 +0930683 rc = mctp_astlpc_kcs_read(astlpc, MCTP_ASTLPC_KCS_REG_STATUS, &status);
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930684 if (rc) {
685 mctp_prwarn("KCS status read failed");
686 return rc;
687 }
688
689 astlpc->kcs_status = status;
690
691 if (!(status & KCS_STATUS_BMC_READY))
692 return -EHOSTDOWN;
693
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930694 mctp_astlpc_lpc_read(astlpc, &hdr, 0, sizeof(hdr));
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930695
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930696 bmc_ver_min = be16toh(hdr.bmc_ver_min);
697 bmc_ver_cur = be16toh(hdr.bmc_ver_cur);
698
Andrew Jeffery88412be2021-03-09 22:05:22 +1030699 /* Calculate the expected value of negotiated_ver */
700 negotiated = mctp_astlpc_negotiate_version(bmc_ver_min, bmc_ver_cur,
701 ASTLPC_VER_MIN,
702 ASTLPC_VER_CUR);
703 if (!negotiated) {
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930704 astlpc_prerr(astlpc, "Cannot negotiate with invalid versions");
705 return -EINVAL;
706 }
707
Andrew Jeffery88412be2021-03-09 22:05:22 +1030708 /* Assign protocol ops so we can calculate the packet buffer sizes */
709 assert(negotiated < ARRAY_SIZE(astlpc_protocol_version));
710 astlpc->proto = &astlpc_protocol_version[negotiated];
711
712 /* Negotiate packet buffers in v2 style if the BMC supports it */
713 if (negotiated >= 2) {
Andrew Jeffery3a540662020-05-26 19:55:30 +0930714 rc = mctp_astlpc_negotiate_layout_host(astlpc);
715 if (rc < 0)
716 return rc;
717 }
718
Andrew Jeffery88412be2021-03-09 22:05:22 +1030719 /* Advertise the host's supported protocol versions */
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930720 mctp_astlpc_lpc_write(astlpc, &ver_min_be,
721 offsetof(struct mctp_lpcmap_hdr, host_ver_min),
722 sizeof(ver_min_be));
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930723
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930724 mctp_astlpc_lpc_write(astlpc, &ver_cur_be,
725 offsetof(struct mctp_lpcmap_hdr, host_ver_cur),
726 sizeof(ver_cur_be));
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930727
728 /* Send channel init command */
Andrew Jefferyf13cb972020-05-28 09:30:09 +0930729 rc = mctp_astlpc_kcs_write(astlpc, MCTP_ASTLPC_KCS_REG_DATA, 0x0);
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930730 if (rc) {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930731 astlpc_prwarn(astlpc, "KCS write failed");
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930732 }
733
Andrew Jeffery88412be2021-03-09 22:05:22 +1030734 /*
735 * Configure the host so `astlpc->proto->version == 0` holds until we
736 * receive a subsequent status update from the BMC. Until then,
737 * `astlpc->proto->version == 0` indicates that we're yet to complete
738 * the channel initialisation handshake.
739 *
740 * When the BMC provides a status update with KCS_STATUS_CHANNEL_ACTIVE
741 * set we will assign the appropriate protocol ops struct in accordance
742 * with `negotiated_ver`.
743 */
744 astlpc->proto = &astlpc_protocol_version[ASTLPC_VER_BAD];
745
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930746 return rc;
747}
748
749static int mctp_binding_astlpc_start_host(struct mctp_binding *b)
750{
751 struct mctp_binding_astlpc *astlpc =
752 container_of(b, struct mctp_binding_astlpc, binding);
753
754 return mctp_astlpc_init_host(astlpc);
755}
756
757static bool __mctp_astlpc_kcs_ready(struct mctp_binding_astlpc *astlpc,
758 uint8_t status, bool is_write)
759{
760 bool is_bmc;
761 bool ready_state;
762 uint8_t flag;
763
764 is_bmc = (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_BMC);
765 flag = (is_bmc ^ is_write) ? KCS_STATUS_IBF : KCS_STATUS_OBF;
766 ready_state = is_write ? 0 : 1;
767
768 return !!(status & flag) == ready_state;
769}
770
771static inline bool
772mctp_astlpc_kcs_read_ready(struct mctp_binding_astlpc *astlpc, uint8_t status)
773{
774 return __mctp_astlpc_kcs_ready(astlpc, status, false);
775}
776
777static inline bool
778mctp_astlpc_kcs_write_ready(struct mctp_binding_astlpc *astlpc, uint8_t status)
779{
780 return __mctp_astlpc_kcs_ready(astlpc, status, true);
781}
782
Jeremy Kerr672c8852019-03-01 12:18:07 +0800783static int mctp_astlpc_kcs_send(struct mctp_binding_astlpc *astlpc,
784 uint8_t data)
785{
786 uint8_t status;
787 int rc;
788
Andrew Jefferyfe763e92022-08-05 23:16:17 +0930789 rc = mctp_astlpc_kcs_read(astlpc, MCTP_ASTLPC_KCS_REG_STATUS,
790 &status);
791 if (rc) {
792 astlpc_prwarn(astlpc, "KCS status read failed");
793 return -EIO;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800794 }
Andrew Jefferyfe763e92022-08-05 23:16:17 +0930795 if (!mctp_astlpc_kcs_write_ready(astlpc, status))
796 return -EBUSY;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800797
Andrew Jefferyf13cb972020-05-28 09:30:09 +0930798 rc = mctp_astlpc_kcs_write(astlpc, MCTP_ASTLPC_KCS_REG_DATA, data);
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530799 if (rc) {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930800 astlpc_prwarn(astlpc, "KCS data write failed");
Andrew Jefferyfe763e92022-08-05 23:16:17 +0930801 return -EIO;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800802 }
803
804 return 0;
805}
806
807static int mctp_binding_astlpc_tx(struct mctp_binding *b,
808 struct mctp_pktbuf *pkt)
809{
810 struct mctp_binding_astlpc *astlpc = binding_to_astlpc(b);
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930811 uint32_t len, len_be;
Andrew Jefferyedfe3832020-02-06 11:52:11 +1030812 struct mctp_hdr *hdr;
Andrew Jefferyfe763e92022-08-05 23:16:17 +0930813 int rc;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800814
Andrew Jefferyedfe3832020-02-06 11:52:11 +1030815 hdr = mctp_pktbuf_hdr(pkt);
Jeremy Kerr672c8852019-03-01 12:18:07 +0800816 len = mctp_pktbuf_size(pkt);
Andrew Jefferyedfe3832020-02-06 11:52:11 +1030817
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930818 astlpc_prdebug(astlpc,
819 "%s: Transmitting %" PRIu32
820 "-byte packet (%hhu, %hhu, 0x%hhx)",
821 __func__, len, hdr->src, hdr->dest, hdr->flags_seq_tag);
Andrew Jefferyedfe3832020-02-06 11:52:11 +1030822
Andrew Jeffery88412be2021-03-09 22:05:22 +1030823 if (len > astlpc->proto->body_size(astlpc->layout.tx.size)) {
Andrew Jeffery0721f582022-09-29 12:12:39 +0930824 astlpc_prwarn(astlpc, "invalid TX len %" PRIu32 ": %" PRIu32,
825 len,
826 astlpc->proto->body_size(astlpc->layout.tx.size));
827 return -EMSGSIZE;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800828 }
829
Andrew Jefferyfe763e92022-08-05 23:16:17 +0930830 mctp_binding_set_tx_enabled(b, false);
831
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930832 len_be = htobe32(len);
833 mctp_astlpc_lpc_write(astlpc, &len_be, astlpc->layout.tx.offset,
834 sizeof(len_be));
Andrew Jefferyeba19a32021-03-09 23:09:40 +1030835
836 astlpc->proto->pktbuf_protect(pkt);
837 len = mctp_pktbuf_size(pkt);
838
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930839 mctp_astlpc_lpc_write(astlpc, hdr, astlpc->layout.tx.offset + 4, len);
Jeremy Kerr672c8852019-03-01 12:18:07 +0800840
Andrew Jefferyfe763e92022-08-05 23:16:17 +0930841 astlpc->layout.tx.state = buffer_state_prepared;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800842
Andrew Jefferyfe763e92022-08-05 23:16:17 +0930843 rc = mctp_astlpc_kcs_send(astlpc, 0x1);
844 if (!rc)
845 astlpc->layout.tx.state = buffer_state_released;
Andrew Jefferyeba19a32021-03-09 23:09:40 +1030846
Andrew Jefferyfe763e92022-08-05 23:16:17 +0930847 return rc == -EBUSY ? 0 : rc;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800848}
849
Andrew Jeffery3a540662020-05-26 19:55:30 +0930850static uint32_t mctp_astlpc_calculate_mtu(struct mctp_binding_astlpc *astlpc,
851 struct mctp_astlpc_layout *layout)
852{
Andrew Jeffery88412be2021-03-09 22:05:22 +1030853 uint32_t low, high, limit, rpkt;
Andrew Jeffery3a540662020-05-26 19:55:30 +0930854
855 /* Derive the largest MTU the BMC _can_ support */
856 low = MIN(astlpc->layout.rx.offset, astlpc->layout.tx.offset);
857 high = MAX(astlpc->layout.rx.offset, astlpc->layout.tx.offset);
858 limit = high - low;
859
Andrew Jefferya9368982020-06-09 13:07:39 +0930860 /* Determine the largest MTU the BMC _wants_ to support */
861 if (astlpc->requested_mtu) {
Andrew Jeffery88412be2021-03-09 22:05:22 +1030862 uint32_t rmtu = astlpc->requested_mtu;
Andrew Jefferya9368982020-06-09 13:07:39 +0930863
Andrew Jeffery88412be2021-03-09 22:05:22 +1030864 rpkt = astlpc->proto->packet_size(MCTP_PACKET_SIZE(rmtu));
865 limit = MIN(limit, rpkt);
Andrew Jefferya9368982020-06-09 13:07:39 +0930866 }
Andrew Jeffery3a540662020-05-26 19:55:30 +0930867
868 /* Determine the accepted MTU, applied both directions by convention */
Andrew Jeffery88412be2021-03-09 22:05:22 +1030869 rpkt = MIN(limit, layout->tx.size);
870 return MCTP_BODY_SIZE(astlpc->proto->body_size(rpkt));
Andrew Jeffery3a540662020-05-26 19:55:30 +0930871}
872
Andrew Jeffery88412be2021-03-09 22:05:22 +1030873static int mctp_astlpc_negotiate_layout_bmc(struct mctp_binding_astlpc *astlpc)
Andrew Jeffery3a540662020-05-26 19:55:30 +0930874{
875 struct mctp_astlpc_layout proposed, pending;
876 uint32_t sz, mtu;
877 int rc;
878
Andrew Jeffery88412be2021-03-09 22:05:22 +1030879 /* Do we have a valid protocol version? */
880 if (!astlpc->proto->version)
881 return -EINVAL;
882
Andrew Jeffery3a540662020-05-26 19:55:30 +0930883 /* Extract the host's proposed layout */
884 rc = mctp_astlpc_layout_read(astlpc, &proposed);
885 if (rc < 0)
886 return rc;
887
Andrew Jeffery88412be2021-03-09 22:05:22 +1030888 /* Do we have a reasonable layout? */
889 if (!mctp_astlpc_layout_validate(astlpc, &proposed))
Andrew Jeffery3a540662020-05-26 19:55:30 +0930890 return -EINVAL;
891
892 /* Negotiate the MTU */
893 mtu = mctp_astlpc_calculate_mtu(astlpc, &proposed);
Andrew Jeffery88412be2021-03-09 22:05:22 +1030894 sz = astlpc->proto->packet_size(MCTP_PACKET_SIZE(mtu));
Andrew Jeffery3a540662020-05-26 19:55:30 +0930895
896 /*
897 * Use symmetric MTUs by convention and to pass constraints in rx/tx
898 * functions
899 */
900 pending = astlpc->layout;
901 pending.tx.size = sz;
902 pending.rx.size = sz;
903
Andrew Jeffery88412be2021-03-09 22:05:22 +1030904 if (mctp_astlpc_layout_validate(astlpc, &pending)) {
Andrew Jeffery3a540662020-05-26 19:55:30 +0930905 /* We found a sensible Rx MTU, so honour it */
906 astlpc->layout = pending;
907
908 /* Enforce the negotiated MTU */
909 rc = mctp_astlpc_layout_write(astlpc, &astlpc->layout);
910 if (rc < 0)
911 return rc;
912
913 astlpc_prinfo(astlpc, "Negotiated an MTU of %" PRIu32 " bytes",
914 mtu);
915 } else {
916 astlpc_prwarn(astlpc, "MTU negotiation failed");
917 return -EINVAL;
918 }
919
Andrew Jeffery88412be2021-03-09 22:05:22 +1030920 if (astlpc->proto->version >= 2)
Andrew Jeffery3a540662020-05-26 19:55:30 +0930921 astlpc->binding.pkt_size = MCTP_PACKET_SIZE(mtu);
922
923 return 0;
924}
925
Jeremy Kerr672c8852019-03-01 12:18:07 +0800926static void mctp_astlpc_init_channel(struct mctp_binding_astlpc *astlpc)
927{
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930928 uint16_t negotiated, negotiated_be;
929 struct mctp_lpcmap_hdr hdr;
930 uint8_t status;
Andrew Jeffery3a540662020-05-26 19:55:30 +0930931 int rc;
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930932
933 mctp_astlpc_lpc_read(astlpc, &hdr, 0, sizeof(hdr));
934
935 /* Version negotiation */
936 negotiated =
937 mctp_astlpc_negotiate_version(ASTLPC_VER_MIN, ASTLPC_VER_CUR,
938 be16toh(hdr.host_ver_min),
939 be16toh(hdr.host_ver_cur));
940
Andrew Jeffery88412be2021-03-09 22:05:22 +1030941 /* MTU negotiation requires knowing which protocol we'll use */
942 assert(negotiated < ARRAY_SIZE(astlpc_protocol_version));
943 astlpc->proto = &astlpc_protocol_version[negotiated];
944
Andrew Jeffery3a540662020-05-26 19:55:30 +0930945 /* Host Rx MTU negotiation: Failure terminates channel init */
Andrew Jeffery88412be2021-03-09 22:05:22 +1030946 rc = mctp_astlpc_negotiate_layout_bmc(astlpc);
Andrew Jeffery3a540662020-05-26 19:55:30 +0930947 if (rc < 0)
948 negotiated = ASTLPC_VER_BAD;
949
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930950 /* Populate the negotiated version */
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930951 negotiated_be = htobe16(negotiated);
952 mctp_astlpc_lpc_write(astlpc, &negotiated_be,
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930953 offsetof(struct mctp_lpcmap_hdr, negotiated_ver),
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930954 sizeof(negotiated_be));
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930955
Andrew Jefferyfe763e92022-08-05 23:16:17 +0930956 /* Track buffer ownership */
957 astlpc->layout.tx.state = buffer_state_acquired;
958 astlpc->layout.rx.state = buffer_state_released;
959
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930960 /* Finalise the configuration */
961 status = KCS_STATUS_BMC_READY | KCS_STATUS_OBF;
962 if (negotiated > 0) {
963 astlpc_prinfo(astlpc, "Negotiated binding version %" PRIu16,
964 negotiated);
965 status |= KCS_STATUS_CHANNEL_ACTIVE;
966 } else {
Andrew Jeffery88412be2021-03-09 22:05:22 +1030967 astlpc_prerr(astlpc, "Failed to initialise channel");
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930968 }
Jeremy Kerr672c8852019-03-01 12:18:07 +0800969
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930970 mctp_astlpc_kcs_set_status(astlpc, status);
971
972 mctp_binding_set_tx_enabled(&astlpc->binding,
973 status & KCS_STATUS_CHANNEL_ACTIVE);
Jeremy Kerr672c8852019-03-01 12:18:07 +0800974}
975
976static void mctp_astlpc_rx_start(struct mctp_binding_astlpc *astlpc)
977{
978 struct mctp_pktbuf *pkt;
Andrew Jefferyeba19a32021-03-09 23:09:40 +1030979 uint32_t body, packet;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800980
Andrew Jefferyeba19a32021-03-09 23:09:40 +1030981 mctp_astlpc_lpc_read(astlpc, &body, astlpc->layout.rx.offset,
982 sizeof(body));
983 body = be32toh(body);
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530984
Andrew Jefferyeba19a32021-03-09 23:09:40 +1030985 if (body > astlpc->proto->body_size(astlpc->layout.rx.size)) {
986 astlpc_prwarn(astlpc, "invalid RX len 0x%x", body);
Jeremy Kerr672c8852019-03-01 12:18:07 +0800987 return;
988 }
989
Andrew Jefferyb93b6112020-06-05 14:13:44 +0930990 assert(astlpc->binding.pkt_size >= 0);
Andrew Jefferyeba19a32021-03-09 23:09:40 +1030991 if (body > (uint32_t)astlpc->binding.pkt_size) {
992 astlpc_prwarn(astlpc, "invalid RX len 0x%x", body);
Jeremy Kerr672c8852019-03-01 12:18:07 +0800993 return;
994 }
995
Andrew Jefferyeba19a32021-03-09 23:09:40 +1030996 /* Eliminate the medium-specific header that we just read */
997 packet = astlpc->proto->packet_size(body) - 4;
998 pkt = mctp_pktbuf_alloc(&astlpc->binding, packet);
Christian Geddesae59f4f2021-09-01 09:54:25 -0500999 if (!pkt) {
1000 astlpc_prwarn(astlpc, "unable to allocate pktbuf len 0x%x", packet);
1001 return;
1002 }
Jeremy Kerr672c8852019-03-01 12:18:07 +08001003
Andrew Jefferyeba19a32021-03-09 23:09:40 +10301004 /*
1005 * Read payload and medium-specific trailer from immediately after the
1006 * medium-specific header.
1007 */
Andrew Jeffery55fb90b2020-05-12 13:54:37 +09301008 mctp_astlpc_lpc_read(astlpc, mctp_pktbuf_hdr(pkt),
Andrew Jefferyeba19a32021-03-09 23:09:40 +10301009 astlpc->layout.rx.offset + 4, packet);
Jeremy Kerr672c8852019-03-01 12:18:07 +08001010
Andrew Jefferyfe763e92022-08-05 23:16:17 +09301011 astlpc->layout.rx.state = buffer_state_prepared;
1012
Christian Geddesae59f4f2021-09-01 09:54:25 -05001013 /* Inform the other side of the MCTP interface that we have read
1014 * the packet off the bus before handling the contents of the packet.
1015 */
Andrew Jefferyfe763e92022-08-05 23:16:17 +09301016 if (!mctp_astlpc_kcs_send(astlpc, 0x2))
1017 astlpc->layout.rx.state = buffer_state_released;
Christian Geddesae59f4f2021-09-01 09:54:25 -05001018
Andrew Jefferyeba19a32021-03-09 23:09:40 +10301019 /*
1020 * v3 will validate the CRC32 in the medium-specific trailer and adjust
1021 * the packet size accordingly. On older protocols validation is a no-op
1022 * that always returns true.
1023 */
1024 if (astlpc->proto->pktbuf_validate(pkt)) {
1025 mctp_bus_rx(&astlpc->binding, pkt);
1026 } else {
1027 /* TODO: Drop any associated assembly */
1028 mctp_pktbuf_free(pkt);
1029 astlpc_prdebug(astlpc, "Dropped corrupt packet");
1030 }
Jeremy Kerr672c8852019-03-01 12:18:07 +08001031}
1032
1033static void mctp_astlpc_tx_complete(struct mctp_binding_astlpc *astlpc)
1034{
Andrew Jefferyfe763e92022-08-05 23:16:17 +09301035 astlpc->layout.tx.state = buffer_state_acquired;
Jeremy Kerr672c8852019-03-01 12:18:07 +08001036 mctp_binding_set_tx_enabled(&astlpc->binding, true);
1037}
1038
Andrew Jeffery4e8264b2020-05-23 20:34:33 +09301039static int mctp_astlpc_finalise_channel(struct mctp_binding_astlpc *astlpc)
1040{
Andrew Jeffery3a540662020-05-26 19:55:30 +09301041 struct mctp_astlpc_layout layout;
Andrew Jeffery4e8264b2020-05-23 20:34:33 +09301042 uint16_t negotiated;
1043 int rc;
1044
1045 rc = mctp_astlpc_lpc_read(astlpc, &negotiated,
1046 offsetof(struct mctp_lpcmap_hdr,
1047 negotiated_ver),
1048 sizeof(negotiated));
1049 if (rc < 0)
1050 return rc;
1051
1052 negotiated = be16toh(negotiated);
Andrew Jeffery88412be2021-03-09 22:05:22 +10301053 astlpc_prerr(astlpc, "Version negotiation got: %u", negotiated);
Andrew Jeffery4e8264b2020-05-23 20:34:33 +09301054
1055 if (negotiated == ASTLPC_VER_BAD || negotiated < ASTLPC_VER_MIN ||
1056 negotiated > ASTLPC_VER_CUR) {
1057 astlpc_prerr(astlpc, "Failed to negotiate version, got: %u\n",
1058 negotiated);
1059 return -EINVAL;
1060 }
1061
Andrew Jeffery88412be2021-03-09 22:05:22 +10301062 assert(negotiated < ARRAY_SIZE(astlpc_protocol_version));
1063 astlpc->proto = &astlpc_protocol_version[negotiated];
Andrew Jeffery4e8264b2020-05-23 20:34:33 +09301064
Andrew Jeffery3a540662020-05-26 19:55:30 +09301065 rc = mctp_astlpc_layout_read(astlpc, &layout);
1066 if (rc < 0)
1067 return rc;
1068
Andrew Jeffery88412be2021-03-09 22:05:22 +10301069 if (!mctp_astlpc_layout_validate(astlpc, &layout)) {
Andrew Jeffery3a540662020-05-26 19:55:30 +09301070 mctp_prerr("BMC proposed invalid buffer parameters");
1071 return -EINVAL;
1072 }
1073
1074 astlpc->layout = layout;
1075
1076 if (negotiated >= 2)
1077 astlpc->binding.pkt_size =
Andrew Jeffery88412be2021-03-09 22:05:22 +10301078 astlpc->proto->body_size(astlpc->layout.tx.size);
Andrew Jeffery3a540662020-05-26 19:55:30 +09301079
Andrew Jefferyfe763e92022-08-05 23:16:17 +09301080 /* Track buffer ownership */
1081 astlpc->layout.tx.state = buffer_state_acquired;
1082 astlpc->layout.rx.state = buffer_state_released;
1083
Andrew Jeffery4e8264b2020-05-23 20:34:33 +09301084 return 0;
1085}
1086
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301087static int mctp_astlpc_update_channel(struct mctp_binding_astlpc *astlpc,
1088 uint8_t status)
1089{
1090 uint8_t updated;
1091 int rc = 0;
1092
1093 assert(astlpc->mode == MCTP_BINDING_ASTLPC_MODE_HOST);
1094
1095 updated = astlpc->kcs_status ^ status;
1096
Andrew Jefferyd0f5da02020-05-28 09:12:55 +09301097 astlpc_prdebug(astlpc, "%s: status: 0x%x, update: 0x%x", __func__,
1098 status, updated);
1099
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301100 if (updated & KCS_STATUS_BMC_READY) {
Andrew Jefferyd0f5da02020-05-28 09:12:55 +09301101 if (status & KCS_STATUS_BMC_READY) {
1102 astlpc->kcs_status = status;
1103 return astlpc->binding.start(&astlpc->binding);
1104 } else {
Andrew Jefferyfe763e92022-08-05 23:16:17 +09301105 /* Shut down the channel */
1106 astlpc->layout.rx.state = buffer_state_idle;
1107 astlpc->layout.tx.state = buffer_state_idle;
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301108 mctp_binding_set_tx_enabled(&astlpc->binding, false);
Andrew Jefferyd0f5da02020-05-28 09:12:55 +09301109 }
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301110 }
1111
Andrew Jeffery88412be2021-03-09 22:05:22 +10301112 if (astlpc->proto->version == 0 ||
1113 updated & KCS_STATUS_CHANNEL_ACTIVE) {
Andrew Jeffery4e8264b2020-05-23 20:34:33 +09301114 bool enable;
1115
Andrew Jefferyfe763e92022-08-05 23:16:17 +09301116 astlpc->layout.rx.state = buffer_state_idle;
1117 astlpc->layout.tx.state = buffer_state_idle;
Andrew Jeffery4e8264b2020-05-23 20:34:33 +09301118 rc = mctp_astlpc_finalise_channel(astlpc);
1119 enable = (status & KCS_STATUS_CHANNEL_ACTIVE) && rc == 0;
Andrew Jeffery4e8264b2020-05-23 20:34:33 +09301120 mctp_binding_set_tx_enabled(&astlpc->binding, enable);
1121 }
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301122
1123 astlpc->kcs_status = status;
1124
1125 return rc;
1126}
1127
Jeremy Kerr672c8852019-03-01 12:18:07 +08001128int mctp_astlpc_poll(struct mctp_binding_astlpc *astlpc)
1129{
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301130 uint8_t status, data;
Jeremy Kerr672c8852019-03-01 12:18:07 +08001131 int rc;
1132
Andrew Jefferyfe763e92022-08-05 23:16:17 +09301133 if (astlpc->layout.rx.state == buffer_state_prepared)
1134 if (!mctp_astlpc_kcs_send(astlpc, 0x2))
1135 astlpc->layout.rx.state = buffer_state_released;
1136
1137 if (astlpc->layout.tx.state == buffer_state_prepared)
1138 if (!mctp_astlpc_kcs_send(astlpc, 0x1))
1139 astlpc->layout.tx.state = buffer_state_released;
1140
Andrew Jefferyf13cb972020-05-28 09:30:09 +09301141 rc = mctp_astlpc_kcs_read(astlpc, MCTP_ASTLPC_KCS_REG_STATUS, &status);
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301142 if (rc) {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +09301143 astlpc_prwarn(astlpc, "KCS read error");
Jeremy Kerr672c8852019-03-01 12:18:07 +08001144 return -1;
Jeremy Kerr672c8852019-03-01 12:18:07 +08001145 }
1146
Andrew Jeffery9101a2a2020-05-22 16:08:03 +09301147 astlpc_prdebug(astlpc, "%s: status: 0x%hhx", __func__, status);
Andrew Jefferyedfe3832020-02-06 11:52:11 +10301148
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301149 if (!mctp_astlpc_kcs_read_ready(astlpc, status))
Jeremy Kerr672c8852019-03-01 12:18:07 +08001150 return 0;
1151
Andrew Jefferyf13cb972020-05-28 09:30:09 +09301152 rc = mctp_astlpc_kcs_read(astlpc, MCTP_ASTLPC_KCS_REG_DATA, &data);
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301153 if (rc) {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +09301154 astlpc_prwarn(astlpc, "KCS data read error");
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301155 return -1;
1156 }
1157
Andrew Jeffery9101a2a2020-05-22 16:08:03 +09301158 astlpc_prdebug(astlpc, "%s: data: 0x%hhx", __func__, data);
Andrew Jefferyedfe3832020-02-06 11:52:11 +10301159
Andrew Jeffery88412be2021-03-09 22:05:22 +10301160 if (!astlpc->proto->version && !(data == 0x0 || data == 0xff)) {
Andrew Jefferyafcb7012021-01-28 17:00:36 +10301161 astlpc_prwarn(astlpc, "Invalid message for binding state: 0x%x",
1162 data);
1163 return 0;
1164 }
1165
Jeremy Kerr672c8852019-03-01 12:18:07 +08001166 switch (data) {
1167 case 0x0:
1168 mctp_astlpc_init_channel(astlpc);
1169 break;
1170 case 0x1:
Andrew Jefferyfe763e92022-08-05 23:16:17 +09301171 if (astlpc->layout.rx.state != buffer_state_released) {
1172 astlpc_prerr(astlpc,
1173 "Protocol error: Invalid Rx buffer state for event %d: %d\n",
1174 data, astlpc->layout.rx.state);
1175 return 0;
1176 }
Jeremy Kerr672c8852019-03-01 12:18:07 +08001177 mctp_astlpc_rx_start(astlpc);
1178 break;
1179 case 0x2:
Andrew Jefferyfe763e92022-08-05 23:16:17 +09301180 if (astlpc->layout.tx.state != buffer_state_released) {
1181 astlpc_prerr(astlpc,
1182 "Protocol error: Invalid Tx buffer state for event %d: %d\n",
1183 data, astlpc->layout.tx.state);
1184 return 0;
1185 }
Jeremy Kerr672c8852019-03-01 12:18:07 +08001186 mctp_astlpc_tx_complete(astlpc);
1187 break;
Jeremy Kerr1a4b55a2019-06-24 14:22:39 +08001188 case 0xff:
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301189 /* No responsibilities for the BMC on 0xff */
Andrew Jefferyd0f5da02020-05-28 09:12:55 +09301190 if (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_HOST) {
1191 rc = mctp_astlpc_update_channel(astlpc, status);
1192 if (rc < 0)
1193 return rc;
1194 }
1195 break;
Jeremy Kerr672c8852019-03-01 12:18:07 +08001196 default:
Andrew Jeffery9101a2a2020-05-22 16:08:03 +09301197 astlpc_prwarn(astlpc, "unknown message 0x%x", data);
Jeremy Kerr672c8852019-03-01 12:18:07 +08001198 }
Andrew Jefferyd0f5da02020-05-28 09:12:55 +09301199
1200 /* Handle silent loss of bmc-ready */
1201 if (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_HOST) {
1202 if (!(status & KCS_STATUS_BMC_READY && data == 0xff))
1203 return mctp_astlpc_update_channel(astlpc, status);
1204 }
1205
1206 return rc;
Jeremy Kerr672c8852019-03-01 12:18:07 +08001207}
1208
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301209/* allocate and basic initialisation */
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301210static struct mctp_binding_astlpc *__mctp_astlpc_init(uint8_t mode,
1211 uint32_t mtu)
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301212{
1213 struct mctp_binding_astlpc *astlpc;
1214
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301215 assert((mode == MCTP_BINDING_ASTLPC_MODE_BMC) ||
1216 (mode == MCTP_BINDING_ASTLPC_MODE_HOST));
1217
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301218 astlpc = __mctp_alloc(sizeof(*astlpc));
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301219 if (!astlpc)
1220 return NULL;
1221
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301222 memset(astlpc, 0, sizeof(*astlpc));
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301223 astlpc->mode = mode;
1224 astlpc->lpc_map = NULL;
Andrew Jefferyfe763e92022-08-05 23:16:17 +09301225 astlpc->layout.rx.state = buffer_state_idle;
1226 astlpc->layout.tx.state = buffer_state_idle;
Andrew Jefferya9368982020-06-09 13:07:39 +09301227 astlpc->requested_mtu = mtu;
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301228 astlpc->binding.name = "astlpc";
1229 astlpc->binding.version = 1;
Andrew Jeffery1a4f4412021-01-28 13:42:02 +10301230 astlpc->binding.pkt_size =
1231 MCTP_PACKET_SIZE(mtu > MCTP_BTU ? mtu : MCTP_BTU);
Andrew Jefferyeba19a32021-03-09 23:09:40 +10301232 astlpc->binding.pkt_header = 4;
1233 astlpc->binding.pkt_trailer = 4;
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301234 astlpc->binding.tx = mctp_binding_astlpc_tx;
1235 if (mode == MCTP_BINDING_ASTLPC_MODE_BMC)
1236 astlpc->binding.start = mctp_binding_astlpc_start_bmc;
1237 else if (mode == MCTP_BINDING_ASTLPC_MODE_HOST)
1238 astlpc->binding.start = mctp_binding_astlpc_start_host;
1239 else {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +09301240 astlpc_prerr(astlpc, "%s: Invalid mode: %d\n", __func__, mode);
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301241 __mctp_free(astlpc);
1242 return NULL;
1243 }
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301244
1245 return astlpc;
1246}
1247
Jeremy Kerr3b36d172019-09-04 11:56:09 +08001248struct mctp_binding *mctp_binding_astlpc_core(struct mctp_binding_astlpc *b)
1249{
1250 return &b->binding;
1251}
1252
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301253struct mctp_binding_astlpc *
1254mctp_astlpc_init(uint8_t mode, uint32_t mtu, void *lpc_map,
1255 const struct mctp_binding_astlpc_ops *ops, void *ops_data)
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301256{
1257 struct mctp_binding_astlpc *astlpc;
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301258
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301259 if (!(mode == MCTP_BINDING_ASTLPC_MODE_BMC ||
1260 mode == MCTP_BINDING_ASTLPC_MODE_HOST)) {
1261 mctp_prerr("Unknown binding mode: %u", mode);
1262 return NULL;
1263 }
1264
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301265 astlpc = __mctp_astlpc_init(mode, mtu);
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301266 if (!astlpc)
1267 return NULL;
1268
1269 memcpy(&astlpc->ops, ops, sizeof(astlpc->ops));
1270 astlpc->ops_data = ops_data;
1271 astlpc->lpc_map = lpc_map;
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301272 astlpc->mode = mode;
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301273
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301274 return astlpc;
1275}
1276
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301277struct mctp_binding_astlpc *
1278mctp_astlpc_init_ops(const struct mctp_binding_astlpc_ops *ops, void *ops_data,
1279 void *lpc_map)
1280{
1281 return mctp_astlpc_init(MCTP_BINDING_ASTLPC_MODE_BMC, MCTP_BTU, lpc_map,
1282 ops, ops_data);
1283}
1284
Andrew Jeffery4663f672020-03-10 23:36:24 +10301285void mctp_astlpc_destroy(struct mctp_binding_astlpc *astlpc)
1286{
Andrew Jefferyd0f5da02020-05-28 09:12:55 +09301287 /* Clear channel-active and bmc-ready */
1288 if (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_BMC)
Andrew Jeffery7c4509a2021-08-26 14:56:16 +09301289 mctp_astlpc_kcs_set_status(astlpc, 0);
Andrew Jeffery4663f672020-03-10 23:36:24 +10301290 __mctp_free(astlpc);
1291}
1292
Jeremy Kerrb214c642019-11-27 11:34:00 +08001293#ifdef MCTP_HAVE_FILEIO
Andrew Jeffery55fb90b2020-05-12 13:54:37 +09301294
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301295static int mctp_astlpc_init_fileio_lpc(struct mctp_binding_astlpc *astlpc)
Jeremy Kerr672c8852019-03-01 12:18:07 +08001296{
1297 struct aspeed_lpc_ctrl_mapping map = {
1298 .window_type = ASPEED_LPC_CTRL_WINDOW_MEMORY,
1299 .window_id = 0, /* There's only one */
1300 .flags = 0,
1301 .addr = 0,
1302 .offset = 0,
1303 .size = 0
1304 };
Andrew Jeffery979c6a12020-05-23 20:04:49 +09301305 void *lpc_map_base;
Jeremy Kerr672c8852019-03-01 12:18:07 +08001306 int fd, rc;
1307
1308 fd = open(lpc_path, O_RDWR | O_SYNC);
1309 if (fd < 0) {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +09301310 astlpc_prwarn(astlpc, "LPC open (%s) failed", lpc_path);
Jeremy Kerr672c8852019-03-01 12:18:07 +08001311 return -1;
1312 }
1313
1314 rc = ioctl(fd, ASPEED_LPC_CTRL_IOCTL_GET_SIZE, &map);
1315 if (rc) {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +09301316 astlpc_prwarn(astlpc, "LPC GET_SIZE failed");
Jeremy Kerr672c8852019-03-01 12:18:07 +08001317 close(fd);
1318 return -1;
1319 }
1320
Andrew Jefferyc1d5c542021-10-27 15:25:48 +10301321 /*
1322 * 🚨🚨🚨
1323 *
1324 * Decouple ourselves from hiomapd[1] (another user of the FW2AHB) by
1325 * mapping the FW2AHB to the reserved memory here as well.
1326 *
1327 * It's not possible to use the MCTP ASTLPC binding on machines that
1328 * need the FW2AHB bridge mapped anywhere except to the reserved memory
1329 * (e.g. the host SPI NOR).
1330 *
1331 * [1] https://github.com/openbmc/hiomapd/
1332 *
1333 * 🚨🚨🚨
1334 *
1335 * The following calculation must align with what's going on in
1336 * hiomapd's lpc.c so as not to disrupt its behaviour:
1337 *
1338 * https://github.com/openbmc/hiomapd/blob/5ff50e3cbd7702aefc185264e4adfb9952040575/lpc.c#L68
1339 *
1340 * 🚨🚨🚨
1341 */
1342
1343 /* Map the reserved memory at the top of the 28-bit LPC firmware address space */
1344 map.addr = 0x0FFFFFFF & -map.size;
1345 astlpc_prinfo(astlpc,
1346 "Configuring FW2AHB to map reserved memory at 0x%08x for 0x%x in the LPC FW cycle address-space",
1347 map.addr, map.size);
1348
1349 rc = ioctl(fd, ASPEED_LPC_CTRL_IOCTL_MAP, &map);
1350 if (rc) {
1351 astlpc_prwarn(astlpc, "Failed to map FW2AHB to reserved memory");
1352 close(fd);
1353 return -1;
1354 }
1355
1356 /* Map the reserved memory into our address space */
Andrew Jeffery979c6a12020-05-23 20:04:49 +09301357 lpc_map_base =
1358 mmap(NULL, map.size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
1359 if (lpc_map_base == MAP_FAILED) {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +09301360 astlpc_prwarn(astlpc, "LPC mmap failed");
Jeremy Kerr672c8852019-03-01 12:18:07 +08001361 rc = -1;
1362 } else {
Andrew Jeffery979c6a12020-05-23 20:04:49 +09301363 astlpc->lpc_map = lpc_map_base + map.size - LPC_WIN_SIZE;
Jeremy Kerr672c8852019-03-01 12:18:07 +08001364 }
1365
1366 close(fd);
1367
1368 return rc;
1369}
1370
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301371static int mctp_astlpc_init_fileio_kcs(struct mctp_binding_astlpc *astlpc)
Jeremy Kerr672c8852019-03-01 12:18:07 +08001372{
1373 astlpc->kcs_fd = open(kcs_path, O_RDWR);
1374 if (astlpc->kcs_fd < 0)
1375 return -1;
1376
1377 return 0;
1378}
1379
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301380static int __mctp_astlpc_fileio_kcs_read(void *arg,
1381 enum mctp_binding_astlpc_kcs_reg reg, uint8_t *val)
1382{
1383 struct mctp_binding_astlpc *astlpc = arg;
1384 off_t offset = reg;
1385 int rc;
1386
1387 rc = pread(astlpc->kcs_fd, val, 1, offset);
1388
1389 return rc == 1 ? 0 : -1;
1390}
1391
1392static int __mctp_astlpc_fileio_kcs_write(void *arg,
1393 enum mctp_binding_astlpc_kcs_reg reg, uint8_t val)
1394{
1395 struct mctp_binding_astlpc *astlpc = arg;
1396 off_t offset = reg;
1397 int rc;
1398
1399 rc = pwrite(astlpc->kcs_fd, &val, 1, offset);
1400
1401 return rc == 1 ? 0 : -1;
1402}
1403
Andrew Jeffery1111c6a2022-07-25 20:44:39 +09301404int mctp_astlpc_init_pollfd(struct mctp_binding_astlpc *astlpc,
1405 struct pollfd *pollfd)
1406{
Andrew Jefferyfe763e92022-08-05 23:16:17 +09301407 bool release;
1408
Andrew Jeffery1111c6a2022-07-25 20:44:39 +09301409 pollfd->fd = astlpc->kcs_fd;
Andrew Jefferyfe763e92022-08-05 23:16:17 +09301410 pollfd->events = 0;
1411
1412 release = astlpc->layout.rx.state == buffer_state_prepared ||
1413 astlpc->layout.tx.state == buffer_state_prepared;
1414
1415 pollfd->events = release ? POLLOUT : POLLIN;
Andrew Jeffery1111c6a2022-07-25 20:44:39 +09301416
1417 return 0;
1418}
1419
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301420struct mctp_binding_astlpc *mctp_astlpc_init_fileio(void)
Jeremy Kerr672c8852019-03-01 12:18:07 +08001421{
1422 struct mctp_binding_astlpc *astlpc;
1423 int rc;
1424
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301425 /*
1426 * If we're doing file IO then we're very likely not running
Andrew Jeffery8877c462020-06-15 12:22:53 +09301427 * freestanding, so lets assume that we're on the BMC side.
1428 *
1429 * Requesting an MTU of 0 requests the largest possible MTU, whatever
1430 * value that might take.
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301431 */
Andrew Jeffery8877c462020-06-15 12:22:53 +09301432 astlpc = __mctp_astlpc_init(MCTP_BINDING_ASTLPC_MODE_BMC, 0);
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301433 if (!astlpc)
1434 return NULL;
Jeremy Kerr672c8852019-03-01 12:18:07 +08001435
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301436 /* Set internal operations for kcs. We use direct accesses to the lpc
1437 * map area */
1438 astlpc->ops.kcs_read = __mctp_astlpc_fileio_kcs_read;
1439 astlpc->ops.kcs_write = __mctp_astlpc_fileio_kcs_write;
1440 astlpc->ops_data = astlpc;
1441
1442 rc = mctp_astlpc_init_fileio_lpc(astlpc);
Jeremy Kerr672c8852019-03-01 12:18:07 +08001443 if (rc) {
1444 free(astlpc);
1445 return NULL;
1446 }
1447
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301448 rc = mctp_astlpc_init_fileio_kcs(astlpc);
Jeremy Kerr672c8852019-03-01 12:18:07 +08001449 if (rc) {
1450 free(astlpc);
1451 return NULL;
1452 }
1453
Jeremy Kerr672c8852019-03-01 12:18:07 +08001454 return astlpc;
1455}
Jeremy Kerr92a10a62019-08-28 16:55:54 +05301456#else
Andrew Jeffery5b2702d2022-07-25 16:21:43 +09301457struct mctp_binding_astlpc *mctp_astlpc_init_fileio(void)
Jeremy Kerr92a10a62019-08-28 16:55:54 +05301458{
Andrew Jeffery5b2702d2022-07-25 16:21:43 +09301459 mctp_prlog(MCTP_LOG_ERR, "%s: Missing support for file IO", __func__);
Jeremy Kerr92a10a62019-08-28 16:55:54 +05301460 return NULL;
1461}
Jeremy Kerr672c8852019-03-01 12:18:07 +08001462
Andrew Jeffery1111c6a2022-07-25 20:44:39 +09301463int mctp_astlpc_init_pollfd(struct mctp_binding_astlpc *astlpc __unused,
1464 struct pollfd *pollfd __unused)
1465{
1466 mctp_prlog(MCTP_LOG_ERR, "%s: Missing support for file IO", __func__);
1467 return -1;
1468}
Jeremy Kerr92a10a62019-08-28 16:55:54 +05301469#endif