blob: 0e7e9f8b99ad2ba368308de6357654a213256ff1 [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 Jefferyeba19a32021-03-09 23:09:40 +1030824 astlpc_prwarn(astlpc, "invalid TX len %" PRIu32 ": %" PRIu32, len,
825 astlpc->proto->body_size(astlpc->layout.tx.size));
Jeremy Kerr672c8852019-03-01 12:18:07 +0800826 return -1;
827 }
828
Andrew Jefferyfe763e92022-08-05 23:16:17 +0930829 mctp_binding_set_tx_enabled(b, false);
830
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930831 len_be = htobe32(len);
832 mctp_astlpc_lpc_write(astlpc, &len_be, astlpc->layout.tx.offset,
833 sizeof(len_be));
Andrew Jefferyeba19a32021-03-09 23:09:40 +1030834
835 astlpc->proto->pktbuf_protect(pkt);
836 len = mctp_pktbuf_size(pkt);
837
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930838 mctp_astlpc_lpc_write(astlpc, hdr, astlpc->layout.tx.offset + 4, len);
Jeremy Kerr672c8852019-03-01 12:18:07 +0800839
Andrew Jefferyfe763e92022-08-05 23:16:17 +0930840 astlpc->layout.tx.state = buffer_state_prepared;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800841
Andrew Jefferyfe763e92022-08-05 23:16:17 +0930842 rc = mctp_astlpc_kcs_send(astlpc, 0x1);
843 if (!rc)
844 astlpc->layout.tx.state = buffer_state_released;
Andrew Jefferyeba19a32021-03-09 23:09:40 +1030845
Andrew Jefferyfe763e92022-08-05 23:16:17 +0930846 return rc == -EBUSY ? 0 : rc;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800847}
848
Andrew Jeffery3a540662020-05-26 19:55:30 +0930849static uint32_t mctp_astlpc_calculate_mtu(struct mctp_binding_astlpc *astlpc,
850 struct mctp_astlpc_layout *layout)
851{
Andrew Jeffery88412be2021-03-09 22:05:22 +1030852 uint32_t low, high, limit, rpkt;
Andrew Jeffery3a540662020-05-26 19:55:30 +0930853
854 /* Derive the largest MTU the BMC _can_ support */
855 low = MIN(astlpc->layout.rx.offset, astlpc->layout.tx.offset);
856 high = MAX(astlpc->layout.rx.offset, astlpc->layout.tx.offset);
857 limit = high - low;
858
Andrew Jefferya9368982020-06-09 13:07:39 +0930859 /* Determine the largest MTU the BMC _wants_ to support */
860 if (astlpc->requested_mtu) {
Andrew Jeffery88412be2021-03-09 22:05:22 +1030861 uint32_t rmtu = astlpc->requested_mtu;
Andrew Jefferya9368982020-06-09 13:07:39 +0930862
Andrew Jeffery88412be2021-03-09 22:05:22 +1030863 rpkt = astlpc->proto->packet_size(MCTP_PACKET_SIZE(rmtu));
864 limit = MIN(limit, rpkt);
Andrew Jefferya9368982020-06-09 13:07:39 +0930865 }
Andrew Jeffery3a540662020-05-26 19:55:30 +0930866
867 /* Determine the accepted MTU, applied both directions by convention */
Andrew Jeffery88412be2021-03-09 22:05:22 +1030868 rpkt = MIN(limit, layout->tx.size);
869 return MCTP_BODY_SIZE(astlpc->proto->body_size(rpkt));
Andrew Jeffery3a540662020-05-26 19:55:30 +0930870}
871
Andrew Jeffery88412be2021-03-09 22:05:22 +1030872static int mctp_astlpc_negotiate_layout_bmc(struct mctp_binding_astlpc *astlpc)
Andrew Jeffery3a540662020-05-26 19:55:30 +0930873{
874 struct mctp_astlpc_layout proposed, pending;
875 uint32_t sz, mtu;
876 int rc;
877
Andrew Jeffery88412be2021-03-09 22:05:22 +1030878 /* Do we have a valid protocol version? */
879 if (!astlpc->proto->version)
880 return -EINVAL;
881
Andrew Jeffery3a540662020-05-26 19:55:30 +0930882 /* Extract the host's proposed layout */
883 rc = mctp_astlpc_layout_read(astlpc, &proposed);
884 if (rc < 0)
885 return rc;
886
Andrew Jeffery88412be2021-03-09 22:05:22 +1030887 /* Do we have a reasonable layout? */
888 if (!mctp_astlpc_layout_validate(astlpc, &proposed))
Andrew Jeffery3a540662020-05-26 19:55:30 +0930889 return -EINVAL;
890
891 /* Negotiate the MTU */
892 mtu = mctp_astlpc_calculate_mtu(astlpc, &proposed);
Andrew Jeffery88412be2021-03-09 22:05:22 +1030893 sz = astlpc->proto->packet_size(MCTP_PACKET_SIZE(mtu));
Andrew Jeffery3a540662020-05-26 19:55:30 +0930894
895 /*
896 * Use symmetric MTUs by convention and to pass constraints in rx/tx
897 * functions
898 */
899 pending = astlpc->layout;
900 pending.tx.size = sz;
901 pending.rx.size = sz;
902
Andrew Jeffery88412be2021-03-09 22:05:22 +1030903 if (mctp_astlpc_layout_validate(astlpc, &pending)) {
Andrew Jeffery3a540662020-05-26 19:55:30 +0930904 /* We found a sensible Rx MTU, so honour it */
905 astlpc->layout = pending;
906
907 /* Enforce the negotiated MTU */
908 rc = mctp_astlpc_layout_write(astlpc, &astlpc->layout);
909 if (rc < 0)
910 return rc;
911
912 astlpc_prinfo(astlpc, "Negotiated an MTU of %" PRIu32 " bytes",
913 mtu);
914 } else {
915 astlpc_prwarn(astlpc, "MTU negotiation failed");
916 return -EINVAL;
917 }
918
Andrew Jeffery88412be2021-03-09 22:05:22 +1030919 if (astlpc->proto->version >= 2)
Andrew Jeffery3a540662020-05-26 19:55:30 +0930920 astlpc->binding.pkt_size = MCTP_PACKET_SIZE(mtu);
921
922 return 0;
923}
924
Jeremy Kerr672c8852019-03-01 12:18:07 +0800925static void mctp_astlpc_init_channel(struct mctp_binding_astlpc *astlpc)
926{
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930927 uint16_t negotiated, negotiated_be;
928 struct mctp_lpcmap_hdr hdr;
929 uint8_t status;
Andrew Jeffery3a540662020-05-26 19:55:30 +0930930 int rc;
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930931
932 mctp_astlpc_lpc_read(astlpc, &hdr, 0, sizeof(hdr));
933
934 /* Version negotiation */
935 negotiated =
936 mctp_astlpc_negotiate_version(ASTLPC_VER_MIN, ASTLPC_VER_CUR,
937 be16toh(hdr.host_ver_min),
938 be16toh(hdr.host_ver_cur));
939
Andrew Jeffery88412be2021-03-09 22:05:22 +1030940 /* MTU negotiation requires knowing which protocol we'll use */
941 assert(negotiated < ARRAY_SIZE(astlpc_protocol_version));
942 astlpc->proto = &astlpc_protocol_version[negotiated];
943
Andrew Jeffery3a540662020-05-26 19:55:30 +0930944 /* Host Rx MTU negotiation: Failure terminates channel init */
Andrew Jeffery88412be2021-03-09 22:05:22 +1030945 rc = mctp_astlpc_negotiate_layout_bmc(astlpc);
Andrew Jeffery3a540662020-05-26 19:55:30 +0930946 if (rc < 0)
947 negotiated = ASTLPC_VER_BAD;
948
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930949 /* Populate the negotiated version */
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930950 negotiated_be = htobe16(negotiated);
951 mctp_astlpc_lpc_write(astlpc, &negotiated_be,
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930952 offsetof(struct mctp_lpcmap_hdr, negotiated_ver),
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930953 sizeof(negotiated_be));
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930954
Andrew Jefferyfe763e92022-08-05 23:16:17 +0930955 /* Track buffer ownership */
956 astlpc->layout.tx.state = buffer_state_acquired;
957 astlpc->layout.rx.state = buffer_state_released;
958
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930959 /* Finalise the configuration */
960 status = KCS_STATUS_BMC_READY | KCS_STATUS_OBF;
961 if (negotiated > 0) {
962 astlpc_prinfo(astlpc, "Negotiated binding version %" PRIu16,
963 negotiated);
964 status |= KCS_STATUS_CHANNEL_ACTIVE;
965 } else {
Andrew Jeffery88412be2021-03-09 22:05:22 +1030966 astlpc_prerr(astlpc, "Failed to initialise channel");
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930967 }
Jeremy Kerr672c8852019-03-01 12:18:07 +0800968
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930969 mctp_astlpc_kcs_set_status(astlpc, status);
970
971 mctp_binding_set_tx_enabled(&astlpc->binding,
972 status & KCS_STATUS_CHANNEL_ACTIVE);
Jeremy Kerr672c8852019-03-01 12:18:07 +0800973}
974
975static void mctp_astlpc_rx_start(struct mctp_binding_astlpc *astlpc)
976{
977 struct mctp_pktbuf *pkt;
Andrew Jefferyeba19a32021-03-09 23:09:40 +1030978 uint32_t body, packet;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800979
Andrew Jefferyeba19a32021-03-09 23:09:40 +1030980 mctp_astlpc_lpc_read(astlpc, &body, astlpc->layout.rx.offset,
981 sizeof(body));
982 body = be32toh(body);
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530983
Andrew Jefferyeba19a32021-03-09 23:09:40 +1030984 if (body > astlpc->proto->body_size(astlpc->layout.rx.size)) {
985 astlpc_prwarn(astlpc, "invalid RX len 0x%x", body);
Jeremy Kerr672c8852019-03-01 12:18:07 +0800986 return;
987 }
988
Andrew Jefferyb93b6112020-06-05 14:13:44 +0930989 assert(astlpc->binding.pkt_size >= 0);
Andrew Jefferyeba19a32021-03-09 23:09:40 +1030990 if (body > (uint32_t)astlpc->binding.pkt_size) {
991 astlpc_prwarn(astlpc, "invalid RX len 0x%x", body);
Jeremy Kerr672c8852019-03-01 12:18:07 +0800992 return;
993 }
994
Andrew Jefferyeba19a32021-03-09 23:09:40 +1030995 /* Eliminate the medium-specific header that we just read */
996 packet = astlpc->proto->packet_size(body) - 4;
997 pkt = mctp_pktbuf_alloc(&astlpc->binding, packet);
Christian Geddesae59f4f2021-09-01 09:54:25 -0500998 if (!pkt) {
999 astlpc_prwarn(astlpc, "unable to allocate pktbuf len 0x%x", packet);
1000 return;
1001 }
Jeremy Kerr672c8852019-03-01 12:18:07 +08001002
Andrew Jefferyeba19a32021-03-09 23:09:40 +10301003 /*
1004 * Read payload and medium-specific trailer from immediately after the
1005 * medium-specific header.
1006 */
Andrew Jeffery55fb90b2020-05-12 13:54:37 +09301007 mctp_astlpc_lpc_read(astlpc, mctp_pktbuf_hdr(pkt),
Andrew Jefferyeba19a32021-03-09 23:09:40 +10301008 astlpc->layout.rx.offset + 4, packet);
Jeremy Kerr672c8852019-03-01 12:18:07 +08001009
Andrew Jefferyfe763e92022-08-05 23:16:17 +09301010 astlpc->layout.rx.state = buffer_state_prepared;
1011
Christian Geddesae59f4f2021-09-01 09:54:25 -05001012 /* Inform the other side of the MCTP interface that we have read
1013 * the packet off the bus before handling the contents of the packet.
1014 */
Andrew Jefferyfe763e92022-08-05 23:16:17 +09301015 if (!mctp_astlpc_kcs_send(astlpc, 0x2))
1016 astlpc->layout.rx.state = buffer_state_released;
Christian Geddesae59f4f2021-09-01 09:54:25 -05001017
Andrew Jefferyeba19a32021-03-09 23:09:40 +10301018 /*
1019 * v3 will validate the CRC32 in the medium-specific trailer and adjust
1020 * the packet size accordingly. On older protocols validation is a no-op
1021 * that always returns true.
1022 */
1023 if (astlpc->proto->pktbuf_validate(pkt)) {
1024 mctp_bus_rx(&astlpc->binding, pkt);
1025 } else {
1026 /* TODO: Drop any associated assembly */
1027 mctp_pktbuf_free(pkt);
1028 astlpc_prdebug(astlpc, "Dropped corrupt packet");
1029 }
Jeremy Kerr672c8852019-03-01 12:18:07 +08001030}
1031
1032static void mctp_astlpc_tx_complete(struct mctp_binding_astlpc *astlpc)
1033{
Andrew Jefferyfe763e92022-08-05 23:16:17 +09301034 astlpc->layout.tx.state = buffer_state_acquired;
Jeremy Kerr672c8852019-03-01 12:18:07 +08001035 mctp_binding_set_tx_enabled(&astlpc->binding, true);
1036}
1037
Andrew Jeffery4e8264b2020-05-23 20:34:33 +09301038static int mctp_astlpc_finalise_channel(struct mctp_binding_astlpc *astlpc)
1039{
Andrew Jeffery3a540662020-05-26 19:55:30 +09301040 struct mctp_astlpc_layout layout;
Andrew Jeffery4e8264b2020-05-23 20:34:33 +09301041 uint16_t negotiated;
1042 int rc;
1043
1044 rc = mctp_astlpc_lpc_read(astlpc, &negotiated,
1045 offsetof(struct mctp_lpcmap_hdr,
1046 negotiated_ver),
1047 sizeof(negotiated));
1048 if (rc < 0)
1049 return rc;
1050
1051 negotiated = be16toh(negotiated);
Andrew Jeffery88412be2021-03-09 22:05:22 +10301052 astlpc_prerr(astlpc, "Version negotiation got: %u", negotiated);
Andrew Jeffery4e8264b2020-05-23 20:34:33 +09301053
1054 if (negotiated == ASTLPC_VER_BAD || negotiated < ASTLPC_VER_MIN ||
1055 negotiated > ASTLPC_VER_CUR) {
1056 astlpc_prerr(astlpc, "Failed to negotiate version, got: %u\n",
1057 negotiated);
1058 return -EINVAL;
1059 }
1060
Andrew Jeffery88412be2021-03-09 22:05:22 +10301061 assert(negotiated < ARRAY_SIZE(astlpc_protocol_version));
1062 astlpc->proto = &astlpc_protocol_version[negotiated];
Andrew Jeffery4e8264b2020-05-23 20:34:33 +09301063
Andrew Jeffery3a540662020-05-26 19:55:30 +09301064 rc = mctp_astlpc_layout_read(astlpc, &layout);
1065 if (rc < 0)
1066 return rc;
1067
Andrew Jeffery88412be2021-03-09 22:05:22 +10301068 if (!mctp_astlpc_layout_validate(astlpc, &layout)) {
Andrew Jeffery3a540662020-05-26 19:55:30 +09301069 mctp_prerr("BMC proposed invalid buffer parameters");
1070 return -EINVAL;
1071 }
1072
1073 astlpc->layout = layout;
1074
1075 if (negotiated >= 2)
1076 astlpc->binding.pkt_size =
Andrew Jeffery88412be2021-03-09 22:05:22 +10301077 astlpc->proto->body_size(astlpc->layout.tx.size);
Andrew Jeffery3a540662020-05-26 19:55:30 +09301078
Andrew Jefferyfe763e92022-08-05 23:16:17 +09301079 /* Track buffer ownership */
1080 astlpc->layout.tx.state = buffer_state_acquired;
1081 astlpc->layout.rx.state = buffer_state_released;
1082
Andrew Jeffery4e8264b2020-05-23 20:34:33 +09301083 return 0;
1084}
1085
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301086static int mctp_astlpc_update_channel(struct mctp_binding_astlpc *astlpc,
1087 uint8_t status)
1088{
1089 uint8_t updated;
1090 int rc = 0;
1091
1092 assert(astlpc->mode == MCTP_BINDING_ASTLPC_MODE_HOST);
1093
1094 updated = astlpc->kcs_status ^ status;
1095
Andrew Jefferyd0f5da02020-05-28 09:12:55 +09301096 astlpc_prdebug(astlpc, "%s: status: 0x%x, update: 0x%x", __func__,
1097 status, updated);
1098
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301099 if (updated & KCS_STATUS_BMC_READY) {
Andrew Jefferyd0f5da02020-05-28 09:12:55 +09301100 if (status & KCS_STATUS_BMC_READY) {
1101 astlpc->kcs_status = status;
1102 return astlpc->binding.start(&astlpc->binding);
1103 } else {
Andrew Jefferyfe763e92022-08-05 23:16:17 +09301104 /* Shut down the channel */
1105 astlpc->layout.rx.state = buffer_state_idle;
1106 astlpc->layout.tx.state = buffer_state_idle;
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301107 mctp_binding_set_tx_enabled(&astlpc->binding, false);
Andrew Jefferyd0f5da02020-05-28 09:12:55 +09301108 }
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301109 }
1110
Andrew Jeffery88412be2021-03-09 22:05:22 +10301111 if (astlpc->proto->version == 0 ||
1112 updated & KCS_STATUS_CHANNEL_ACTIVE) {
Andrew Jeffery4e8264b2020-05-23 20:34:33 +09301113 bool enable;
1114
Andrew Jefferyfe763e92022-08-05 23:16:17 +09301115 astlpc->layout.rx.state = buffer_state_idle;
1116 astlpc->layout.tx.state = buffer_state_idle;
Andrew Jeffery4e8264b2020-05-23 20:34:33 +09301117 rc = mctp_astlpc_finalise_channel(astlpc);
1118 enable = (status & KCS_STATUS_CHANNEL_ACTIVE) && rc == 0;
Andrew Jeffery4e8264b2020-05-23 20:34:33 +09301119 mctp_binding_set_tx_enabled(&astlpc->binding, enable);
1120 }
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301121
1122 astlpc->kcs_status = status;
1123
1124 return rc;
1125}
1126
Jeremy Kerr672c8852019-03-01 12:18:07 +08001127int mctp_astlpc_poll(struct mctp_binding_astlpc *astlpc)
1128{
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301129 uint8_t status, data;
Jeremy Kerr672c8852019-03-01 12:18:07 +08001130 int rc;
1131
Andrew Jefferyfe763e92022-08-05 23:16:17 +09301132 if (astlpc->layout.rx.state == buffer_state_prepared)
1133 if (!mctp_astlpc_kcs_send(astlpc, 0x2))
1134 astlpc->layout.rx.state = buffer_state_released;
1135
1136 if (astlpc->layout.tx.state == buffer_state_prepared)
1137 if (!mctp_astlpc_kcs_send(astlpc, 0x1))
1138 astlpc->layout.tx.state = buffer_state_released;
1139
Andrew Jefferyf13cb972020-05-28 09:30:09 +09301140 rc = mctp_astlpc_kcs_read(astlpc, MCTP_ASTLPC_KCS_REG_STATUS, &status);
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301141 if (rc) {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +09301142 astlpc_prwarn(astlpc, "KCS read error");
Jeremy Kerr672c8852019-03-01 12:18:07 +08001143 return -1;
Jeremy Kerr672c8852019-03-01 12:18:07 +08001144 }
1145
Andrew Jeffery9101a2a2020-05-22 16:08:03 +09301146 astlpc_prdebug(astlpc, "%s: status: 0x%hhx", __func__, status);
Andrew Jefferyedfe3832020-02-06 11:52:11 +10301147
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301148 if (!mctp_astlpc_kcs_read_ready(astlpc, status))
Jeremy Kerr672c8852019-03-01 12:18:07 +08001149 return 0;
1150
Andrew Jefferyf13cb972020-05-28 09:30:09 +09301151 rc = mctp_astlpc_kcs_read(astlpc, MCTP_ASTLPC_KCS_REG_DATA, &data);
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301152 if (rc) {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +09301153 astlpc_prwarn(astlpc, "KCS data read error");
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301154 return -1;
1155 }
1156
Andrew Jeffery9101a2a2020-05-22 16:08:03 +09301157 astlpc_prdebug(astlpc, "%s: data: 0x%hhx", __func__, data);
Andrew Jefferyedfe3832020-02-06 11:52:11 +10301158
Andrew Jeffery88412be2021-03-09 22:05:22 +10301159 if (!astlpc->proto->version && !(data == 0x0 || data == 0xff)) {
Andrew Jefferyafcb7012021-01-28 17:00:36 +10301160 astlpc_prwarn(astlpc, "Invalid message for binding state: 0x%x",
1161 data);
1162 return 0;
1163 }
1164
Jeremy Kerr672c8852019-03-01 12:18:07 +08001165 switch (data) {
1166 case 0x0:
1167 mctp_astlpc_init_channel(astlpc);
1168 break;
1169 case 0x1:
Andrew Jefferyfe763e92022-08-05 23:16:17 +09301170 if (astlpc->layout.rx.state != buffer_state_released) {
1171 astlpc_prerr(astlpc,
1172 "Protocol error: Invalid Rx buffer state for event %d: %d\n",
1173 data, astlpc->layout.rx.state);
1174 return 0;
1175 }
Jeremy Kerr672c8852019-03-01 12:18:07 +08001176 mctp_astlpc_rx_start(astlpc);
1177 break;
1178 case 0x2:
Andrew Jefferyfe763e92022-08-05 23:16:17 +09301179 if (astlpc->layout.tx.state != buffer_state_released) {
1180 astlpc_prerr(astlpc,
1181 "Protocol error: Invalid Tx buffer state for event %d: %d\n",
1182 data, astlpc->layout.tx.state);
1183 return 0;
1184 }
Jeremy Kerr672c8852019-03-01 12:18:07 +08001185 mctp_astlpc_tx_complete(astlpc);
1186 break;
Jeremy Kerr1a4b55a2019-06-24 14:22:39 +08001187 case 0xff:
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301188 /* No responsibilities for the BMC on 0xff */
Andrew Jefferyd0f5da02020-05-28 09:12:55 +09301189 if (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_HOST) {
1190 rc = mctp_astlpc_update_channel(astlpc, status);
1191 if (rc < 0)
1192 return rc;
1193 }
1194 break;
Jeremy Kerr672c8852019-03-01 12:18:07 +08001195 default:
Andrew Jeffery9101a2a2020-05-22 16:08:03 +09301196 astlpc_prwarn(astlpc, "unknown message 0x%x", data);
Jeremy Kerr672c8852019-03-01 12:18:07 +08001197 }
Andrew Jefferyd0f5da02020-05-28 09:12:55 +09301198
1199 /* Handle silent loss of bmc-ready */
1200 if (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_HOST) {
1201 if (!(status & KCS_STATUS_BMC_READY && data == 0xff))
1202 return mctp_astlpc_update_channel(astlpc, status);
1203 }
1204
1205 return rc;
Jeremy Kerr672c8852019-03-01 12:18:07 +08001206}
1207
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301208/* allocate and basic initialisation */
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301209static struct mctp_binding_astlpc *__mctp_astlpc_init(uint8_t mode,
1210 uint32_t mtu)
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301211{
1212 struct mctp_binding_astlpc *astlpc;
1213
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301214 assert((mode == MCTP_BINDING_ASTLPC_MODE_BMC) ||
1215 (mode == MCTP_BINDING_ASTLPC_MODE_HOST));
1216
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301217 astlpc = __mctp_alloc(sizeof(*astlpc));
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301218 if (!astlpc)
1219 return NULL;
1220
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301221 memset(astlpc, 0, sizeof(*astlpc));
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301222 astlpc->mode = mode;
1223 astlpc->lpc_map = NULL;
Andrew Jefferyfe763e92022-08-05 23:16:17 +09301224 astlpc->layout.rx.state = buffer_state_idle;
1225 astlpc->layout.tx.state = buffer_state_idle;
Andrew Jefferya9368982020-06-09 13:07:39 +09301226 astlpc->requested_mtu = mtu;
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301227 astlpc->binding.name = "astlpc";
1228 astlpc->binding.version = 1;
Andrew Jeffery1a4f4412021-01-28 13:42:02 +10301229 astlpc->binding.pkt_size =
1230 MCTP_PACKET_SIZE(mtu > MCTP_BTU ? mtu : MCTP_BTU);
Andrew Jefferyeba19a32021-03-09 23:09:40 +10301231 astlpc->binding.pkt_header = 4;
1232 astlpc->binding.pkt_trailer = 4;
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301233 astlpc->binding.tx = mctp_binding_astlpc_tx;
1234 if (mode == MCTP_BINDING_ASTLPC_MODE_BMC)
1235 astlpc->binding.start = mctp_binding_astlpc_start_bmc;
1236 else if (mode == MCTP_BINDING_ASTLPC_MODE_HOST)
1237 astlpc->binding.start = mctp_binding_astlpc_start_host;
1238 else {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +09301239 astlpc_prerr(astlpc, "%s: Invalid mode: %d\n", __func__, mode);
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301240 __mctp_free(astlpc);
1241 return NULL;
1242 }
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301243
1244 return astlpc;
1245}
1246
Jeremy Kerr3b36d172019-09-04 11:56:09 +08001247struct mctp_binding *mctp_binding_astlpc_core(struct mctp_binding_astlpc *b)
1248{
1249 return &b->binding;
1250}
1251
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301252struct mctp_binding_astlpc *
1253mctp_astlpc_init(uint8_t mode, uint32_t mtu, void *lpc_map,
1254 const struct mctp_binding_astlpc_ops *ops, void *ops_data)
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301255{
1256 struct mctp_binding_astlpc *astlpc;
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301257
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301258 if (!(mode == MCTP_BINDING_ASTLPC_MODE_BMC ||
1259 mode == MCTP_BINDING_ASTLPC_MODE_HOST)) {
1260 mctp_prerr("Unknown binding mode: %u", mode);
1261 return NULL;
1262 }
1263
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301264 astlpc = __mctp_astlpc_init(mode, mtu);
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301265 if (!astlpc)
1266 return NULL;
1267
1268 memcpy(&astlpc->ops, ops, sizeof(astlpc->ops));
1269 astlpc->ops_data = ops_data;
1270 astlpc->lpc_map = lpc_map;
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301271 astlpc->mode = mode;
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301272
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301273 return astlpc;
1274}
1275
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301276struct mctp_binding_astlpc *
1277mctp_astlpc_init_ops(const struct mctp_binding_astlpc_ops *ops, void *ops_data,
1278 void *lpc_map)
1279{
1280 return mctp_astlpc_init(MCTP_BINDING_ASTLPC_MODE_BMC, MCTP_BTU, lpc_map,
1281 ops, ops_data);
1282}
1283
Andrew Jeffery4663f672020-03-10 23:36:24 +10301284void mctp_astlpc_destroy(struct mctp_binding_astlpc *astlpc)
1285{
Andrew Jefferyd0f5da02020-05-28 09:12:55 +09301286 /* Clear channel-active and bmc-ready */
1287 if (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_BMC)
Andrew Jeffery7c4509a2021-08-26 14:56:16 +09301288 mctp_astlpc_kcs_set_status(astlpc, 0);
Andrew Jeffery4663f672020-03-10 23:36:24 +10301289 __mctp_free(astlpc);
1290}
1291
Jeremy Kerrb214c642019-11-27 11:34:00 +08001292#ifdef MCTP_HAVE_FILEIO
Andrew Jeffery55fb90b2020-05-12 13:54:37 +09301293
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301294static int mctp_astlpc_init_fileio_lpc(struct mctp_binding_astlpc *astlpc)
Jeremy Kerr672c8852019-03-01 12:18:07 +08001295{
1296 struct aspeed_lpc_ctrl_mapping map = {
1297 .window_type = ASPEED_LPC_CTRL_WINDOW_MEMORY,
1298 .window_id = 0, /* There's only one */
1299 .flags = 0,
1300 .addr = 0,
1301 .offset = 0,
1302 .size = 0
1303 };
Andrew Jeffery979c6a12020-05-23 20:04:49 +09301304 void *lpc_map_base;
Jeremy Kerr672c8852019-03-01 12:18:07 +08001305 int fd, rc;
1306
1307 fd = open(lpc_path, O_RDWR | O_SYNC);
1308 if (fd < 0) {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +09301309 astlpc_prwarn(astlpc, "LPC open (%s) failed", lpc_path);
Jeremy Kerr672c8852019-03-01 12:18:07 +08001310 return -1;
1311 }
1312
1313 rc = ioctl(fd, ASPEED_LPC_CTRL_IOCTL_GET_SIZE, &map);
1314 if (rc) {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +09301315 astlpc_prwarn(astlpc, "LPC GET_SIZE failed");
Jeremy Kerr672c8852019-03-01 12:18:07 +08001316 close(fd);
1317 return -1;
1318 }
1319
Andrew Jefferyc1d5c542021-10-27 15:25:48 +10301320 /*
1321 * 🚨🚨🚨
1322 *
1323 * Decouple ourselves from hiomapd[1] (another user of the FW2AHB) by
1324 * mapping the FW2AHB to the reserved memory here as well.
1325 *
1326 * It's not possible to use the MCTP ASTLPC binding on machines that
1327 * need the FW2AHB bridge mapped anywhere except to the reserved memory
1328 * (e.g. the host SPI NOR).
1329 *
1330 * [1] https://github.com/openbmc/hiomapd/
1331 *
1332 * 🚨🚨🚨
1333 *
1334 * The following calculation must align with what's going on in
1335 * hiomapd's lpc.c so as not to disrupt its behaviour:
1336 *
1337 * https://github.com/openbmc/hiomapd/blob/5ff50e3cbd7702aefc185264e4adfb9952040575/lpc.c#L68
1338 *
1339 * 🚨🚨🚨
1340 */
1341
1342 /* Map the reserved memory at the top of the 28-bit LPC firmware address space */
1343 map.addr = 0x0FFFFFFF & -map.size;
1344 astlpc_prinfo(astlpc,
1345 "Configuring FW2AHB to map reserved memory at 0x%08x for 0x%x in the LPC FW cycle address-space",
1346 map.addr, map.size);
1347
1348 rc = ioctl(fd, ASPEED_LPC_CTRL_IOCTL_MAP, &map);
1349 if (rc) {
1350 astlpc_prwarn(astlpc, "Failed to map FW2AHB to reserved memory");
1351 close(fd);
1352 return -1;
1353 }
1354
1355 /* Map the reserved memory into our address space */
Andrew Jeffery979c6a12020-05-23 20:04:49 +09301356 lpc_map_base =
1357 mmap(NULL, map.size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
1358 if (lpc_map_base == MAP_FAILED) {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +09301359 astlpc_prwarn(astlpc, "LPC mmap failed");
Jeremy Kerr672c8852019-03-01 12:18:07 +08001360 rc = -1;
1361 } else {
Andrew Jeffery979c6a12020-05-23 20:04:49 +09301362 astlpc->lpc_map = lpc_map_base + map.size - LPC_WIN_SIZE;
Jeremy Kerr672c8852019-03-01 12:18:07 +08001363 }
1364
1365 close(fd);
1366
1367 return rc;
1368}
1369
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301370static int mctp_astlpc_init_fileio_kcs(struct mctp_binding_astlpc *astlpc)
Jeremy Kerr672c8852019-03-01 12:18:07 +08001371{
1372 astlpc->kcs_fd = open(kcs_path, O_RDWR);
1373 if (astlpc->kcs_fd < 0)
1374 return -1;
1375
1376 return 0;
1377}
1378
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301379static int __mctp_astlpc_fileio_kcs_read(void *arg,
1380 enum mctp_binding_astlpc_kcs_reg reg, uint8_t *val)
1381{
1382 struct mctp_binding_astlpc *astlpc = arg;
1383 off_t offset = reg;
1384 int rc;
1385
1386 rc = pread(astlpc->kcs_fd, val, 1, offset);
1387
1388 return rc == 1 ? 0 : -1;
1389}
1390
1391static int __mctp_astlpc_fileio_kcs_write(void *arg,
1392 enum mctp_binding_astlpc_kcs_reg reg, uint8_t val)
1393{
1394 struct mctp_binding_astlpc *astlpc = arg;
1395 off_t offset = reg;
1396 int rc;
1397
1398 rc = pwrite(astlpc->kcs_fd, &val, 1, offset);
1399
1400 return rc == 1 ? 0 : -1;
1401}
1402
Andrew Jeffery1111c6a2022-07-25 20:44:39 +09301403int mctp_astlpc_init_pollfd(struct mctp_binding_astlpc *astlpc,
1404 struct pollfd *pollfd)
1405{
Andrew Jefferyfe763e92022-08-05 23:16:17 +09301406 bool release;
1407
Andrew Jeffery1111c6a2022-07-25 20:44:39 +09301408 pollfd->fd = astlpc->kcs_fd;
Andrew Jefferyfe763e92022-08-05 23:16:17 +09301409 pollfd->events = 0;
1410
1411 release = astlpc->layout.rx.state == buffer_state_prepared ||
1412 astlpc->layout.tx.state == buffer_state_prepared;
1413
1414 pollfd->events = release ? POLLOUT : POLLIN;
Andrew Jeffery1111c6a2022-07-25 20:44:39 +09301415
1416 return 0;
1417}
1418
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301419struct mctp_binding_astlpc *mctp_astlpc_init_fileio(void)
Jeremy Kerr672c8852019-03-01 12:18:07 +08001420{
1421 struct mctp_binding_astlpc *astlpc;
1422 int rc;
1423
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301424 /*
1425 * If we're doing file IO then we're very likely not running
Andrew Jeffery8877c462020-06-15 12:22:53 +09301426 * freestanding, so lets assume that we're on the BMC side.
1427 *
1428 * Requesting an MTU of 0 requests the largest possible MTU, whatever
1429 * value that might take.
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301430 */
Andrew Jeffery8877c462020-06-15 12:22:53 +09301431 astlpc = __mctp_astlpc_init(MCTP_BINDING_ASTLPC_MODE_BMC, 0);
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301432 if (!astlpc)
1433 return NULL;
Jeremy Kerr672c8852019-03-01 12:18:07 +08001434
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301435 /* Set internal operations for kcs. We use direct accesses to the lpc
1436 * map area */
1437 astlpc->ops.kcs_read = __mctp_astlpc_fileio_kcs_read;
1438 astlpc->ops.kcs_write = __mctp_astlpc_fileio_kcs_write;
1439 astlpc->ops_data = astlpc;
1440
1441 rc = mctp_astlpc_init_fileio_lpc(astlpc);
Jeremy Kerr672c8852019-03-01 12:18:07 +08001442 if (rc) {
1443 free(astlpc);
1444 return NULL;
1445 }
1446
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301447 rc = mctp_astlpc_init_fileio_kcs(astlpc);
Jeremy Kerr672c8852019-03-01 12:18:07 +08001448 if (rc) {
1449 free(astlpc);
1450 return NULL;
1451 }
1452
Jeremy Kerr672c8852019-03-01 12:18:07 +08001453 return astlpc;
1454}
Jeremy Kerr92a10a62019-08-28 16:55:54 +05301455#else
Andrew Jeffery5b2702d2022-07-25 16:21:43 +09301456struct mctp_binding_astlpc *mctp_astlpc_init_fileio(void)
Jeremy Kerr92a10a62019-08-28 16:55:54 +05301457{
Andrew Jeffery5b2702d2022-07-25 16:21:43 +09301458 mctp_prlog(MCTP_LOG_ERR, "%s: Missing support for file IO", __func__);
Jeremy Kerr92a10a62019-08-28 16:55:54 +05301459 return NULL;
1460}
Jeremy Kerr672c8852019-03-01 12:18:07 +08001461
Andrew Jeffery1111c6a2022-07-25 20:44:39 +09301462int mctp_astlpc_init_pollfd(struct mctp_binding_astlpc *astlpc __unused,
1463 struct pollfd *pollfd __unused)
1464{
1465 mctp_prlog(MCTP_LOG_ERR, "%s: Missing support for file IO", __func__);
1466 return -1;
1467}
Jeremy Kerr92a10a62019-08-28 16:55:54 +05301468#endif