blob: 6842ae1ffe99a5e2bfd4084c30f34186b8647730 [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
Rashmica Gupta45d13322022-08-10 12:12:14 +100044enum mctp_astlpc_cmd {
45 cmd_initialise = 0x00,
46 cmd_tx_begin = 0x01,
47 cmd_rx_complete = 0x02,
48 cmd_dummy_value = 0xff,
49};
50
Andrew Jefferyfe763e92022-08-05 23:16:17 +093051enum mctp_astlpc_buffer_state {
52 /*
53 * Prior to "Channel Ready" we mark the buffers as "idle" to catch illegal accesses. In this
54 * state neither side is considered the owner of the buffer.
55 *
56 * Upon "Channel Ready", each side transitions the buffers from the initial "idle" state
57 * to the following target states:
58 *
59 * Tx buffer: "acquired"
60 * Rx buffer: "released"
61 */
62 buffer_state_idle,
63
64 /*
65 * Beyond initialisation by "Channel Ready", buffers are in the "acquired" state once:
66 *
67 * 1. We dequeue a control command transferring the buffer to our ownership out of the KCS
68 * interface, and
69 * 2. We are yet to complete all of our required accesses to the buffer
70 *
71 * * The Tx buffer enters the "acquired" state when we dequeue the "Rx Complete" command
72 * * The Rx buffer enters the "acquired" state when we dequeue the "Tx Begin" command
73 *
74 * It is a failure of implementation if it's possible for both sides to simultaneously
75 * consider a buffer as "acquired".
76 */
77 buffer_state_acquired,
78
79 /*
80 * Buffers are in the "prepared" state when:
81 *
82 * 1. We have completed all of our required accesses (read or write) for the buffer, and
83 * 2. We have not yet successfully enqueued the control command to hand off ownership
84 */
85 buffer_state_prepared,
86
87 /*
88 * Beyond initialisation by "Channel Ready", buffers are in the "released" state once:
89 *
90 * 1. We successfully enqueue the control command transferring ownership to the remote
91 * side in to the KCS interface
92 *
93 * * The Tx buffer enters the "released" state when we enqueue the "Tx Begin" command
94 * * The Rx buffer enters the "released" state when we enqueue the "Rx Complete" command
95 *
96 * It may be the case that both sides simultaneously consider a buffer to be in the
97 * "released" state. However, if this is true, it must also be true that a buffer ownership
98 * transfer command has been enqueued in the KCS interface and is yet to be dequeued.
99 */
100 buffer_state_released,
101};
102
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930103struct mctp_astlpc_buffer {
104 uint32_t offset;
105 uint32_t size;
Andrew Jefferyfe763e92022-08-05 23:16:17 +0930106 enum mctp_astlpc_buffer_state state;
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930107};
108
109struct mctp_astlpc_layout {
110 struct mctp_astlpc_buffer rx;
111 struct mctp_astlpc_buffer tx;
112};
113
Andrew Jeffery88412be2021-03-09 22:05:22 +1030114struct mctp_astlpc_protocol {
115 uint16_t version;
116 uint32_t (*packet_size)(uint32_t body);
117 uint32_t (*body_size)(uint32_t packet);
Andrew Jefferyeba19a32021-03-09 23:09:40 +1030118 void (*pktbuf_protect)(struct mctp_pktbuf *pkt);
119 bool (*pktbuf_validate)(struct mctp_pktbuf *pkt);
Andrew Jeffery88412be2021-03-09 22:05:22 +1030120};
121
Jeremy Kerr672c8852019-03-01 12:18:07 +0800122struct mctp_binding_astlpc {
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600123 struct mctp_binding binding;
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530124
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930125 void *lpc_map;
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930126 struct mctp_astlpc_layout layout;
127
128 uint8_t mode;
Andrew Jefferya9368982020-06-09 13:07:39 +0930129 uint32_t requested_mtu;
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530130
Andrew Jeffery88412be2021-03-09 22:05:22 +1030131 const struct mctp_astlpc_protocol *proto;
132
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530133 /* direct ops data */
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930134 struct mctp_binding_astlpc_ops ops;
135 void *ops_data;
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530136
137 /* fileio ops data */
Andrew Jeffery979c6a12020-05-23 20:04:49 +0930138 int kcs_fd;
139 uint8_t kcs_status;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800140};
141
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600142#define binding_to_astlpc(b) \
Jeremy Kerr672c8852019-03-01 12:18:07 +0800143 container_of(b, struct mctp_binding_astlpc, binding)
144
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930145#define astlpc_prlog(ctx, lvl, fmt, ...) \
146 do { \
147 bool __bmc = ((ctx)->mode == MCTP_BINDING_ASTLPC_MODE_BMC); \
148 mctp_prlog(lvl, pr_fmt("%s: " fmt), __bmc ? "bmc" : "host", \
149 ##__VA_ARGS__); \
150 } while (0)
151
152#define astlpc_prerr(ctx, fmt, ...) \
153 astlpc_prlog(ctx, MCTP_LOG_ERR, fmt, ##__VA_ARGS__)
154#define astlpc_prwarn(ctx, fmt, ...) \
155 astlpc_prlog(ctx, MCTP_LOG_WARNING, fmt, ##__VA_ARGS__)
156#define astlpc_prinfo(ctx, fmt, ...) \
157 astlpc_prlog(ctx, MCTP_LOG_INFO, fmt, ##__VA_ARGS__)
158#define astlpc_prdebug(ctx, fmt, ...) \
159 astlpc_prlog(ctx, MCTP_LOG_DEBUG, fmt, ##__VA_ARGS__)
160
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930161/* clang-format off */
162#define ASTLPC_MCTP_MAGIC 0x4d435450
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930163#define ASTLPC_VER_BAD 0
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930164#define ASTLPC_VER_MIN 1
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930165
Andrew Jeffery3a540662020-05-26 19:55:30 +0930166/* Support testing of new binding protocols */
167#ifndef ASTLPC_VER_CUR
Andrew Jefferyeba19a32021-03-09 23:09:40 +1030168#define ASTLPC_VER_CUR 3
Andrew Jeffery3a540662020-05-26 19:55:30 +0930169#endif
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930170/* clang-format on */
Jeremy Kerr672c8852019-03-01 12:18:07 +0800171
Andrew Jeffery88412be2021-03-09 22:05:22 +1030172#ifndef ARRAY_SIZE
173#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
174#endif
175
176static uint32_t astlpc_packet_size_v1(uint32_t body)
177{
178 assert((body + 4) > body);
179
180 return body + 4;
181}
182
183static uint32_t astlpc_body_size_v1(uint32_t packet)
184{
185 assert((packet - 4) < packet);
186
187 return packet - 4;
188}
189
Andrew Jefferyeba19a32021-03-09 23:09:40 +1030190void astlpc_pktbuf_protect_v1(struct mctp_pktbuf *pkt)
191{
192 (void)pkt;
193}
194
195bool astlpc_pktbuf_validate_v1(struct mctp_pktbuf *pkt)
196{
197 (void)pkt;
198 return true;
199}
200
201static uint32_t astlpc_packet_size_v3(uint32_t body)
202{
203 assert((body + 4 + 4) > body);
204
205 return body + 4 + 4;
206}
207
208static uint32_t astlpc_body_size_v3(uint32_t packet)
209{
210 assert((packet - 4 - 4) < packet);
211
212 return packet - 4 - 4;
213}
214
215void astlpc_pktbuf_protect_v3(struct mctp_pktbuf *pkt)
216{
217 uint32_t code;
218
219 code = htobe32(crc32(mctp_pktbuf_hdr(pkt), mctp_pktbuf_size(pkt)));
220 mctp_prdebug("%s: 0x%" PRIx32, __func__, code);
221 mctp_pktbuf_push(pkt, &code, 4);
222}
223
224bool astlpc_pktbuf_validate_v3(struct mctp_pktbuf *pkt)
225{
226 uint32_t code;
227 void *check;
228
229 code = be32toh(crc32(mctp_pktbuf_hdr(pkt), mctp_pktbuf_size(pkt) - 4));
230 mctp_prdebug("%s: 0x%" PRIx32, __func__, code);
231 check = mctp_pktbuf_pop(pkt, 4);
232 return check && !memcmp(&code, check, 4);
233}
234
Andrew Jeffery88412be2021-03-09 22:05:22 +1030235static const struct mctp_astlpc_protocol astlpc_protocol_version[] = {
236 [0] = {
237 .version = 0,
238 .packet_size = NULL,
239 .body_size = NULL,
Andrew Jefferyeba19a32021-03-09 23:09:40 +1030240 .pktbuf_protect = NULL,
241 .pktbuf_validate = NULL,
Andrew Jeffery88412be2021-03-09 22:05:22 +1030242 },
243 [1] = {
244 .version = 1,
245 .packet_size = astlpc_packet_size_v1,
246 .body_size = astlpc_body_size_v1,
Andrew Jefferyeba19a32021-03-09 23:09:40 +1030247 .pktbuf_protect = astlpc_pktbuf_protect_v1,
248 .pktbuf_validate = astlpc_pktbuf_validate_v1,
Andrew Jeffery88412be2021-03-09 22:05:22 +1030249 },
250 [2] = {
251 .version = 2,
252 .packet_size = astlpc_packet_size_v1,
253 .body_size = astlpc_body_size_v1,
Andrew Jefferyeba19a32021-03-09 23:09:40 +1030254 .pktbuf_protect = astlpc_pktbuf_protect_v1,
255 .pktbuf_validate = astlpc_pktbuf_validate_v1,
256 },
257 [3] = {
258 .version = 3,
259 .packet_size = astlpc_packet_size_v3,
260 .body_size = astlpc_body_size_v3,
261 .pktbuf_protect = astlpc_pktbuf_protect_v3,
262 .pktbuf_validate = astlpc_pktbuf_validate_v3,
Andrew Jeffery88412be2021-03-09 22:05:22 +1030263 },
264};
265
Jeremy Kerr672c8852019-03-01 12:18:07 +0800266struct mctp_lpcmap_hdr {
Andrew Jeffery3a540662020-05-26 19:55:30 +0930267 uint32_t magic;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800268
Andrew Jeffery3a540662020-05-26 19:55:30 +0930269 uint16_t bmc_ver_min;
270 uint16_t bmc_ver_cur;
271 uint16_t host_ver_min;
272 uint16_t host_ver_cur;
273 uint16_t negotiated_ver;
274 uint16_t pad0;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800275
Andrew Jeffery3a540662020-05-26 19:55:30 +0930276 struct {
277 uint32_t rx_offset;
278 uint32_t rx_size;
279 uint32_t tx_offset;
280 uint32_t tx_size;
281 } layout;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800282} __attribute__((packed));
283
Andrew Jeffery3a540662020-05-26 19:55:30 +0930284static const uint32_t control_size = 0x100;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800285
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600286#define LPC_WIN_SIZE (1 * 1024 * 1024)
Jeremy Kerr672c8852019-03-01 12:18:07 +0800287
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600288#define KCS_STATUS_BMC_READY 0x80
289#define KCS_STATUS_CHANNEL_ACTIVE 0x40
290#define KCS_STATUS_IBF 0x02
291#define KCS_STATUS_OBF 0x01
Jeremy Kerr672c8852019-03-01 12:18:07 +0800292
Andrew Jefferyf13cb972020-05-28 09:30:09 +0930293static inline int mctp_astlpc_kcs_write(struct mctp_binding_astlpc *astlpc,
294 enum mctp_binding_astlpc_kcs_reg reg,
295 uint8_t val)
296{
297 return astlpc->ops.kcs_write(astlpc->ops_data, reg, val);
298}
299
300static inline int mctp_astlpc_kcs_read(struct mctp_binding_astlpc *astlpc,
301 enum mctp_binding_astlpc_kcs_reg reg,
302 uint8_t *val)
303{
304 return astlpc->ops.kcs_read(astlpc->ops_data, reg, val);
305}
306
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930307static inline int mctp_astlpc_lpc_write(struct mctp_binding_astlpc *astlpc,
308 const void *buf, long offset,
309 size_t len)
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530310{
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930311 astlpc_prdebug(astlpc, "%s: %zu bytes to 0x%lx", __func__, len, offset);
312
313 assert(offset >= 0);
314
315 /* Indirect access */
316 if (astlpc->ops.lpc_write) {
317 void *data = astlpc->ops_data;
318
319 return astlpc->ops.lpc_write(data, buf, offset, len);
320 }
321
322 /* Direct mapping */
323 assert(astlpc->lpc_map);
324 memcpy(&((char *)astlpc->lpc_map)[offset], buf, len);
325
326 return 0;
327}
328
329static inline int mctp_astlpc_lpc_read(struct mctp_binding_astlpc *astlpc,
330 void *buf, long offset, size_t len)
331{
332 astlpc_prdebug(astlpc, "%s: %zu bytes from 0x%lx", __func__, len,
333 offset);
334
335 assert(offset >= 0);
336
337 /* Indirect access */
338 if (astlpc->ops.lpc_read) {
339 void *data = astlpc->ops_data;
340
341 return astlpc->ops.lpc_read(data, buf, offset, len);
342 }
343
344 /* Direct mapping */
345 assert(astlpc->lpc_map);
346 memcpy(buf, &((char *)astlpc->lpc_map)[offset], len);
347
348 return 0;
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530349}
350
Andrew Jefferyd0f5da02020-05-28 09:12:55 +0930351static int mctp_astlpc_kcs_set_status(struct mctp_binding_astlpc *astlpc,
352 uint8_t status)
353{
354 uint8_t data;
355 int rc;
356
357 /* Since we're setting the status register, we want the other endpoint
358 * to be interrupted. However, some hardware may only raise a host-side
359 * interrupt on an ODR event.
360 * So, write a dummy value of 0xff to ODR, which will ensure that an
361 * interrupt is triggered, and can be ignored by the host.
362 */
Rashmica Gupta45d13322022-08-10 12:12:14 +1000363 data = cmd_dummy_value;
Andrew Jefferyd0f5da02020-05-28 09:12:55 +0930364
Andrew Jefferyf13cb972020-05-28 09:30:09 +0930365 rc = mctp_astlpc_kcs_write(astlpc, MCTP_ASTLPC_KCS_REG_STATUS, status);
Andrew Jefferyd0f5da02020-05-28 09:12:55 +0930366 if (rc) {
367 astlpc_prwarn(astlpc, "KCS status write failed");
368 return -1;
369 }
370
Andrew Jefferyf13cb972020-05-28 09:30:09 +0930371 rc = mctp_astlpc_kcs_write(astlpc, MCTP_ASTLPC_KCS_REG_DATA, data);
Andrew Jefferyd0f5da02020-05-28 09:12:55 +0930372 if (rc) {
373 astlpc_prwarn(astlpc, "KCS dummy data write failed");
374 return -1;
375 }
376
377 return 0;
378}
379
Andrew Jeffery3a540662020-05-26 19:55:30 +0930380static int mctp_astlpc_layout_read(struct mctp_binding_astlpc *astlpc,
381 struct mctp_astlpc_layout *layout)
382{
383 struct mctp_lpcmap_hdr hdr;
384 int rc;
385
386 rc = mctp_astlpc_lpc_read(astlpc, &hdr, 0, sizeof(hdr));
387 if (rc < 0)
388 return rc;
389
390 /* Flip the buffers as the names are defined in terms of the host */
391 if (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_BMC) {
392 layout->rx.offset = be32toh(hdr.layout.tx_offset);
393 layout->rx.size = be32toh(hdr.layout.tx_size);
394 layout->tx.offset = be32toh(hdr.layout.rx_offset);
395 layout->tx.size = be32toh(hdr.layout.rx_size);
396 } else {
397 assert(astlpc->mode == MCTP_BINDING_ASTLPC_MODE_HOST);
398
399 layout->rx.offset = be32toh(hdr.layout.rx_offset);
400 layout->rx.size = be32toh(hdr.layout.rx_size);
401 layout->tx.offset = be32toh(hdr.layout.tx_offset);
402 layout->tx.size = be32toh(hdr.layout.tx_size);
403 }
404
405 return 0;
406}
407
408static int mctp_astlpc_layout_write(struct mctp_binding_astlpc *astlpc,
409 struct mctp_astlpc_layout *layout)
410{
411 uint32_t rx_size_be;
412
413 if (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_BMC) {
414 struct mctp_lpcmap_hdr hdr;
415
416 /*
417 * Flip the buffers as the names are defined in terms of the
418 * host
419 */
420 hdr.layout.rx_offset = htobe32(layout->tx.offset);
421 hdr.layout.rx_size = htobe32(layout->tx.size);
422 hdr.layout.tx_offset = htobe32(layout->rx.offset);
423 hdr.layout.tx_size = htobe32(layout->rx.size);
424
425 return mctp_astlpc_lpc_write(astlpc, &hdr.layout,
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600426 offsetof(struct mctp_lpcmap_hdr,
427 layout),
428 sizeof(hdr.layout));
Andrew Jeffery3a540662020-05-26 19:55:30 +0930429 }
430
431 assert(astlpc->mode == MCTP_BINDING_ASTLPC_MODE_HOST);
432
433 /*
434 * As of v2 we only need to write rx_size - the offsets are controlled
435 * by the BMC, as is the BMC's rx_size (host tx_size).
436 */
437 rx_size_be = htobe32(layout->rx.size);
438 return mctp_astlpc_lpc_write(astlpc, &rx_size_be,
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600439 offsetof(struct mctp_lpcmap_hdr,
440 layout.rx_size),
441 sizeof(rx_size_be));
Andrew Jeffery3a540662020-05-26 19:55:30 +0930442}
443
Andrew Jeffery88412be2021-03-09 22:05:22 +1030444static bool
445mctp_astlpc_buffer_validate(const struct mctp_binding_astlpc *astlpc,
446 const struct mctp_astlpc_buffer *buf,
447 const char *name)
Andrew Jeffery3a540662020-05-26 19:55:30 +0930448{
449 /* Check for overflow */
450 if (buf->offset + buf->size < buf->offset) {
451 mctp_prerr(
452 "%s packet buffer parameters overflow: offset: 0x%" PRIx32
453 ", size: %" PRIu32,
454 name, buf->offset, buf->size);
455 return false;
456 }
457
458 /* Check that the buffers are contained within the allocated space */
459 if (buf->offset + buf->size > LPC_WIN_SIZE) {
460 mctp_prerr(
461 "%s packet buffer parameters exceed %uM window size: offset: 0x%" PRIx32
462 ", size: %" PRIu32,
463 name, (LPC_WIN_SIZE / (1024 * 1024)), buf->offset,
464 buf->size);
465 return false;
466 }
467
468 /* Check that the baseline transmission unit is supported */
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600469 if (buf->size <
470 astlpc->proto->packet_size(MCTP_PACKET_SIZE(MCTP_BTU))) {
Andrew Jeffery3a540662020-05-26 19:55:30 +0930471 mctp_prerr(
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600472 "%s packet buffer too small: Require %" PRIu32
473 " bytes to support the %u byte baseline transmission unit, found %" PRIu32,
Andrew Jeffery88412be2021-03-09 22:05:22 +1030474 name,
475 astlpc->proto->packet_size(MCTP_PACKET_SIZE(MCTP_BTU)),
Andrew Jeffery3a540662020-05-26 19:55:30 +0930476 MCTP_BTU, buf->size);
477 return false;
478 }
479
480 /* Check for overlap with the control space */
481 if (buf->offset < control_size) {
482 mctp_prerr(
483 "%s packet buffer overlaps control region {0x%" PRIx32
484 ", %" PRIu32 "}: Rx {0x%" PRIx32 ", %" PRIu32 "}",
485 name, 0U, control_size, buf->offset, buf->size);
486 return false;
487 }
488
489 return true;
490}
491
Andrew Jeffery88412be2021-03-09 22:05:22 +1030492static bool
493mctp_astlpc_layout_validate(const struct mctp_binding_astlpc *astlpc,
494 const struct mctp_astlpc_layout *layout)
Andrew Jeffery3a540662020-05-26 19:55:30 +0930495{
Andrew Jeffery88412be2021-03-09 22:05:22 +1030496 const struct mctp_astlpc_buffer *rx = &layout->rx;
497 const struct mctp_astlpc_buffer *tx = &layout->tx;
Andrew Jeffery3a540662020-05-26 19:55:30 +0930498 bool rx_valid, tx_valid;
499
Andrew Jeffery88412be2021-03-09 22:05:22 +1030500 rx_valid = mctp_astlpc_buffer_validate(astlpc, rx, "Rx");
501 tx_valid = mctp_astlpc_buffer_validate(astlpc, tx, "Tx");
Andrew Jeffery3a540662020-05-26 19:55:30 +0930502
503 if (!(rx_valid && tx_valid))
504 return false;
505
506 /* Check that the buffers are disjoint */
507 if ((rx->offset <= tx->offset && rx->offset + rx->size > tx->offset) ||
508 (tx->offset <= rx->offset && tx->offset + tx->size > rx->offset)) {
509 mctp_prerr("Rx and Tx packet buffers overlap: Rx {0x%" PRIx32
510 ", %" PRIu32 "}, Tx {0x%" PRIx32 ", %" PRIu32 "}",
511 rx->offset, rx->size, tx->offset, tx->size);
512 return false;
513 }
514
515 return true;
516}
517
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930518static int mctp_astlpc_init_bmc(struct mctp_binding_astlpc *astlpc)
519{
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930520 struct mctp_lpcmap_hdr hdr = { 0 };
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930521 uint8_t status;
Andrew Jeffery88412be2021-03-09 22:05:22 +1030522 uint32_t sz;
Andrew Jeffery3a540662020-05-26 19:55:30 +0930523
524 /*
525 * The largest buffer size is half of the allocated MCTP space
526 * excluding the control space.
527 */
528 sz = ((LPC_WIN_SIZE - control_size) / 2);
529
530 /*
531 * Trim the MTU to a multiple of 16 to meet the requirements of 12.17
532 * Query Hop in DSP0236 v1.3.0.
533 */
Andrew Jeffery88412be2021-03-09 22:05:22 +1030534 sz = MCTP_BODY_SIZE(astlpc->proto->body_size(sz));
Andrew Jeffery3a540662020-05-26 19:55:30 +0930535 sz &= ~0xfUL;
Andrew Jeffery88412be2021-03-09 22:05:22 +1030536 sz = astlpc->proto->packet_size(MCTP_PACKET_SIZE(sz));
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930537
Andrew Jefferya9368982020-06-09 13:07:39 +0930538 if (astlpc->requested_mtu) {
Andrew Jeffery88412be2021-03-09 22:05:22 +1030539 uint32_t rpkt, rmtu;
Andrew Jefferya9368982020-06-09 13:07:39 +0930540
Andrew Jeffery88412be2021-03-09 22:05:22 +1030541 rmtu = astlpc->requested_mtu;
542 rpkt = astlpc->proto->packet_size(MCTP_PACKET_SIZE(rmtu));
543 sz = MIN(sz, rpkt);
Andrew Jefferya9368982020-06-09 13:07:39 +0930544 }
545
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930546 /* Flip the buffers as the names are defined in terms of the host */
Andrew Jeffery3a540662020-05-26 19:55:30 +0930547 astlpc->layout.tx.offset = control_size;
548 astlpc->layout.tx.size = sz;
549 astlpc->layout.rx.offset =
550 astlpc->layout.tx.offset + astlpc->layout.tx.size;
551 astlpc->layout.rx.size = sz;
552
Andrew Jeffery88412be2021-03-09 22:05:22 +1030553 if (!mctp_astlpc_layout_validate(astlpc, &astlpc->layout)) {
554 astlpc_prerr(astlpc, "Cannot support an MTU of %" PRIu32, sz);
Andrew Jefferya9368982020-06-09 13:07:39 +0930555 return -EINVAL;
556 }
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930557
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930558 hdr = (struct mctp_lpcmap_hdr){
559 .magic = htobe32(ASTLPC_MCTP_MAGIC),
560 .bmc_ver_min = htobe16(ASTLPC_VER_MIN),
561 .bmc_ver_cur = htobe16(ASTLPC_VER_CUR),
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930562
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930563 /* Flip the buffers back as we're now describing the host's
564 * configuration to the host */
Andrew Jeffery3a540662020-05-26 19:55:30 +0930565 .layout.rx_offset = htobe32(astlpc->layout.tx.offset),
566 .layout.rx_size = htobe32(astlpc->layout.tx.size),
567 .layout.tx_offset = htobe32(astlpc->layout.rx.offset),
568 .layout.tx_size = htobe32(astlpc->layout.rx.size),
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930569 };
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930570
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930571 mctp_astlpc_lpc_write(astlpc, &hdr, 0, sizeof(hdr));
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930572
Andrew Jefferyb3b55a62020-07-06 13:34:18 +0930573 /*
574 * Set status indicating that the BMC is now active. Be explicit about
575 * clearing OBF; we're reinitialising the binding and so any previous
576 * buffer state is irrelevant.
577 */
578 status = KCS_STATUS_BMC_READY & ~KCS_STATUS_OBF;
Andrew Jefferyd0f5da02020-05-28 09:12:55 +0930579 return mctp_astlpc_kcs_set_status(astlpc, status);
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930580}
581
582static int mctp_binding_astlpc_start_bmc(struct mctp_binding *b)
583{
584 struct mctp_binding_astlpc *astlpc =
585 container_of(b, struct mctp_binding_astlpc, binding);
586
Andrew Jeffery88412be2021-03-09 22:05:22 +1030587 astlpc->proto = &astlpc_protocol_version[ASTLPC_VER_CUR];
588
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930589 return mctp_astlpc_init_bmc(astlpc);
590}
591
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930592static bool mctp_astlpc_validate_version(uint16_t bmc_ver_min,
593 uint16_t bmc_ver_cur,
594 uint16_t host_ver_min,
595 uint16_t host_ver_cur)
596{
597 if (!(bmc_ver_min && bmc_ver_cur && host_ver_min && host_ver_cur)) {
598 mctp_prerr("Invalid version present in [%" PRIu16 ", %" PRIu16
599 "], [%" PRIu16 ", %" PRIu16 "]",
600 bmc_ver_min, bmc_ver_cur, host_ver_min,
601 host_ver_cur);
602 return false;
603 } else if (bmc_ver_min > bmc_ver_cur) {
604 mctp_prerr("Invalid bmc version range [%" PRIu16 ", %" PRIu16
605 "]",
606 bmc_ver_min, bmc_ver_cur);
607 return false;
608 } else if (host_ver_min > host_ver_cur) {
609 mctp_prerr("Invalid host version range [%" PRIu16 ", %" PRIu16
610 "]",
611 host_ver_min, host_ver_cur);
612 return false;
613 } else if ((host_ver_cur < bmc_ver_min) ||
614 (host_ver_min > bmc_ver_cur)) {
615 mctp_prerr(
616 "Unable to satisfy version negotiation with ranges [%" PRIu16
617 ", %" PRIu16 "] and [%" PRIu16 ", %" PRIu16 "]",
618 bmc_ver_min, bmc_ver_cur, host_ver_min, host_ver_cur);
619 return false;
620 }
621
622 return true;
623}
624
Andrew Jeffery3a540662020-05-26 19:55:30 +0930625static int mctp_astlpc_negotiate_layout_host(struct mctp_binding_astlpc *astlpc)
626{
627 struct mctp_astlpc_layout layout;
Andrew Jeffery88412be2021-03-09 22:05:22 +1030628 uint32_t rmtu;
Andrew Jeffery3a540662020-05-26 19:55:30 +0930629 uint32_t sz;
630 int rc;
631
632 rc = mctp_astlpc_layout_read(astlpc, &layout);
633 if (rc < 0)
634 return rc;
635
Andrew Jeffery88412be2021-03-09 22:05:22 +1030636 if (!mctp_astlpc_layout_validate(astlpc, &layout)) {
Andrew Jeffery3a540662020-05-26 19:55:30 +0930637 astlpc_prerr(
638 astlpc,
639 "BMC provided invalid buffer layout: Rx {0x%" PRIx32
640 ", %" PRIu32 "}, Tx {0x%" PRIx32 ", %" PRIu32 "}",
641 layout.rx.offset, layout.rx.size, layout.tx.offset,
642 layout.tx.size);
643 return -EINVAL;
644 }
645
Andrew Jefferya9368982020-06-09 13:07:39 +0930646 astlpc_prinfo(astlpc, "Desire an MTU of %" PRIu32 " bytes",
647 astlpc->requested_mtu);
648
Andrew Jeffery88412be2021-03-09 22:05:22 +1030649 rmtu = astlpc->requested_mtu;
650 sz = astlpc->proto->packet_size(MCTP_PACKET_SIZE(rmtu));
Andrew Jeffery3a540662020-05-26 19:55:30 +0930651 layout.rx.size = sz;
652
Andrew Jeffery88412be2021-03-09 22:05:22 +1030653 if (!mctp_astlpc_layout_validate(astlpc, &layout)) {
Andrew Jeffery3a540662020-05-26 19:55:30 +0930654 astlpc_prerr(
655 astlpc,
656 "Generated invalid buffer layout with size %" PRIu32
657 ": Rx {0x%" PRIx32 ", %" PRIu32 "}, Tx {0x%" PRIx32
658 ", %" PRIu32 "}",
659 sz, layout.rx.offset, layout.rx.size, layout.tx.offset,
660 layout.tx.size);
661 return -EINVAL;
662 }
663
Andrew Jefferya9368982020-06-09 13:07:39 +0930664 astlpc_prinfo(astlpc, "Requesting MTU of %" PRIu32 " bytes",
665 astlpc->requested_mtu);
Andrew Jeffery3a540662020-05-26 19:55:30 +0930666
667 return mctp_astlpc_layout_write(astlpc, &layout);
668}
669
Andrew Jeffery88412be2021-03-09 22:05:22 +1030670static uint16_t mctp_astlpc_negotiate_version(uint16_t bmc_ver_min,
671 uint16_t bmc_ver_cur,
672 uint16_t host_ver_min,
673 uint16_t host_ver_cur)
674{
675 if (!mctp_astlpc_validate_version(bmc_ver_min, bmc_ver_cur,
676 host_ver_min, host_ver_cur))
677 return ASTLPC_VER_BAD;
678
679 if (bmc_ver_cur < host_ver_cur)
680 return bmc_ver_cur;
681
682 return host_ver_cur;
683}
684
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930685static int mctp_astlpc_init_host(struct mctp_binding_astlpc *astlpc)
686{
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930687 const uint16_t ver_min_be = htobe16(ASTLPC_VER_MIN);
688 const uint16_t ver_cur_be = htobe16(ASTLPC_VER_CUR);
Andrew Jeffery88412be2021-03-09 22:05:22 +1030689 uint16_t bmc_ver_min, bmc_ver_cur, negotiated;
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930690 struct mctp_lpcmap_hdr hdr;
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930691 uint8_t status;
692 int rc;
693
Andrew Jefferyf13cb972020-05-28 09:30:09 +0930694 rc = mctp_astlpc_kcs_read(astlpc, MCTP_ASTLPC_KCS_REG_STATUS, &status);
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930695 if (rc) {
696 mctp_prwarn("KCS status read failed");
697 return rc;
698 }
699
700 astlpc->kcs_status = status;
701
702 if (!(status & KCS_STATUS_BMC_READY))
703 return -EHOSTDOWN;
704
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930705 mctp_astlpc_lpc_read(astlpc, &hdr, 0, sizeof(hdr));
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930706
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930707 bmc_ver_min = be16toh(hdr.bmc_ver_min);
708 bmc_ver_cur = be16toh(hdr.bmc_ver_cur);
709
Andrew Jeffery88412be2021-03-09 22:05:22 +1030710 /* Calculate the expected value of negotiated_ver */
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600711 negotiated = mctp_astlpc_negotiate_version(
712 bmc_ver_min, bmc_ver_cur, ASTLPC_VER_MIN, ASTLPC_VER_CUR);
Andrew Jeffery88412be2021-03-09 22:05:22 +1030713 if (!negotiated) {
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930714 astlpc_prerr(astlpc, "Cannot negotiate with invalid versions");
715 return -EINVAL;
716 }
717
Andrew Jeffery88412be2021-03-09 22:05:22 +1030718 /* Assign protocol ops so we can calculate the packet buffer sizes */
719 assert(negotiated < ARRAY_SIZE(astlpc_protocol_version));
720 astlpc->proto = &astlpc_protocol_version[negotiated];
721
722 /* Negotiate packet buffers in v2 style if the BMC supports it */
723 if (negotiated >= 2) {
Andrew Jeffery3a540662020-05-26 19:55:30 +0930724 rc = mctp_astlpc_negotiate_layout_host(astlpc);
725 if (rc < 0)
726 return rc;
727 }
728
Andrew Jeffery88412be2021-03-09 22:05:22 +1030729 /* Advertise the host's supported protocol versions */
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930730 mctp_astlpc_lpc_write(astlpc, &ver_min_be,
731 offsetof(struct mctp_lpcmap_hdr, host_ver_min),
732 sizeof(ver_min_be));
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930733
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930734 mctp_astlpc_lpc_write(astlpc, &ver_cur_be,
735 offsetof(struct mctp_lpcmap_hdr, host_ver_cur),
736 sizeof(ver_cur_be));
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930737
738 /* Send channel init command */
Andrew Jefferyf13cb972020-05-28 09:30:09 +0930739 rc = mctp_astlpc_kcs_write(astlpc, MCTP_ASTLPC_KCS_REG_DATA, 0x0);
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930740 if (rc) {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930741 astlpc_prwarn(astlpc, "KCS write failed");
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930742 }
743
Andrew Jeffery88412be2021-03-09 22:05:22 +1030744 /*
745 * Configure the host so `astlpc->proto->version == 0` holds until we
746 * receive a subsequent status update from the BMC. Until then,
747 * `astlpc->proto->version == 0` indicates that we're yet to complete
748 * the channel initialisation handshake.
749 *
750 * When the BMC provides a status update with KCS_STATUS_CHANNEL_ACTIVE
751 * set we will assign the appropriate protocol ops struct in accordance
752 * with `negotiated_ver`.
753 */
754 astlpc->proto = &astlpc_protocol_version[ASTLPC_VER_BAD];
755
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930756 return rc;
757}
758
759static int mctp_binding_astlpc_start_host(struct mctp_binding *b)
760{
761 struct mctp_binding_astlpc *astlpc =
762 container_of(b, struct mctp_binding_astlpc, binding);
763
764 return mctp_astlpc_init_host(astlpc);
765}
766
767static bool __mctp_astlpc_kcs_ready(struct mctp_binding_astlpc *astlpc,
768 uint8_t status, bool is_write)
769{
770 bool is_bmc;
771 bool ready_state;
772 uint8_t flag;
773
774 is_bmc = (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_BMC);
775 flag = (is_bmc ^ is_write) ? KCS_STATUS_IBF : KCS_STATUS_OBF;
776 ready_state = is_write ? 0 : 1;
777
778 return !!(status & flag) == ready_state;
779}
780
781static inline bool
782mctp_astlpc_kcs_read_ready(struct mctp_binding_astlpc *astlpc, uint8_t status)
783{
784 return __mctp_astlpc_kcs_ready(astlpc, status, false);
785}
786
787static inline bool
788mctp_astlpc_kcs_write_ready(struct mctp_binding_astlpc *astlpc, uint8_t status)
789{
790 return __mctp_astlpc_kcs_ready(astlpc, status, true);
791}
792
Jeremy Kerr672c8852019-03-01 12:18:07 +0800793static int mctp_astlpc_kcs_send(struct mctp_binding_astlpc *astlpc,
Rashmica Gupta45d13322022-08-10 12:12:14 +1000794 enum mctp_astlpc_cmd data)
Jeremy Kerr672c8852019-03-01 12:18:07 +0800795{
796 uint8_t status;
797 int rc;
798
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600799 rc = mctp_astlpc_kcs_read(astlpc, MCTP_ASTLPC_KCS_REG_STATUS, &status);
Andrew Jefferyfe763e92022-08-05 23:16:17 +0930800 if (rc) {
801 astlpc_prwarn(astlpc, "KCS status read failed");
802 return -EIO;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800803 }
Andrew Jefferyfe763e92022-08-05 23:16:17 +0930804 if (!mctp_astlpc_kcs_write_ready(astlpc, status))
805 return -EBUSY;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800806
Andrew Jefferyf13cb972020-05-28 09:30:09 +0930807 rc = mctp_astlpc_kcs_write(astlpc, MCTP_ASTLPC_KCS_REG_DATA, data);
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530808 if (rc) {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930809 astlpc_prwarn(astlpc, "KCS data write failed");
Andrew Jefferyfe763e92022-08-05 23:16:17 +0930810 return -EIO;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800811 }
812
813 return 0;
814}
815
816static int mctp_binding_astlpc_tx(struct mctp_binding *b,
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600817 struct mctp_pktbuf *pkt)
Jeremy Kerr672c8852019-03-01 12:18:07 +0800818{
819 struct mctp_binding_astlpc *astlpc = binding_to_astlpc(b);
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930820 uint32_t len, len_be;
Andrew Jefferyedfe3832020-02-06 11:52:11 +1030821 struct mctp_hdr *hdr;
Andrew Jefferyfe763e92022-08-05 23:16:17 +0930822 int rc;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800823
Andrew Jefferyedfe3832020-02-06 11:52:11 +1030824 hdr = mctp_pktbuf_hdr(pkt);
Jeremy Kerr672c8852019-03-01 12:18:07 +0800825 len = mctp_pktbuf_size(pkt);
Andrew Jefferyedfe3832020-02-06 11:52:11 +1030826
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930827 astlpc_prdebug(astlpc,
828 "%s: Transmitting %" PRIu32
829 "-byte packet (%hhu, %hhu, 0x%hhx)",
830 __func__, len, hdr->src, hdr->dest, hdr->flags_seq_tag);
Andrew Jefferyedfe3832020-02-06 11:52:11 +1030831
Andrew Jeffery88412be2021-03-09 22:05:22 +1030832 if (len > astlpc->proto->body_size(astlpc->layout.tx.size)) {
Andrew Jeffery0721f582022-09-29 12:12:39 +0930833 astlpc_prwarn(astlpc, "invalid TX len %" PRIu32 ": %" PRIu32,
834 len,
835 astlpc->proto->body_size(astlpc->layout.tx.size));
836 return -EMSGSIZE;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800837 }
838
Andrew Jefferyfe763e92022-08-05 23:16:17 +0930839 mctp_binding_set_tx_enabled(b, false);
840
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930841 len_be = htobe32(len);
842 mctp_astlpc_lpc_write(astlpc, &len_be, astlpc->layout.tx.offset,
843 sizeof(len_be));
Andrew Jefferyeba19a32021-03-09 23:09:40 +1030844
845 astlpc->proto->pktbuf_protect(pkt);
846 len = mctp_pktbuf_size(pkt);
847
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930848 mctp_astlpc_lpc_write(astlpc, hdr, astlpc->layout.tx.offset + 4, len);
Jeremy Kerr672c8852019-03-01 12:18:07 +0800849
Andrew Jefferyfe763e92022-08-05 23:16:17 +0930850 astlpc->layout.tx.state = buffer_state_prepared;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800851
Rashmica Gupta45d13322022-08-10 12:12:14 +1000852 rc = mctp_astlpc_kcs_send(astlpc, cmd_tx_begin);
Andrew Jefferyfe763e92022-08-05 23:16:17 +0930853 if (!rc)
854 astlpc->layout.tx.state = buffer_state_released;
Andrew Jefferyeba19a32021-03-09 23:09:40 +1030855
Andrew Jefferyfe763e92022-08-05 23:16:17 +0930856 return rc == -EBUSY ? 0 : rc;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800857}
858
Andrew Jeffery3a540662020-05-26 19:55:30 +0930859static uint32_t mctp_astlpc_calculate_mtu(struct mctp_binding_astlpc *astlpc,
860 struct mctp_astlpc_layout *layout)
861{
Andrew Jeffery88412be2021-03-09 22:05:22 +1030862 uint32_t low, high, limit, rpkt;
Andrew Jeffery3a540662020-05-26 19:55:30 +0930863
864 /* Derive the largest MTU the BMC _can_ support */
865 low = MIN(astlpc->layout.rx.offset, astlpc->layout.tx.offset);
866 high = MAX(astlpc->layout.rx.offset, astlpc->layout.tx.offset);
867 limit = high - low;
868
Andrew Jefferya9368982020-06-09 13:07:39 +0930869 /* Determine the largest MTU the BMC _wants_ to support */
870 if (astlpc->requested_mtu) {
Andrew Jeffery88412be2021-03-09 22:05:22 +1030871 uint32_t rmtu = astlpc->requested_mtu;
Andrew Jefferya9368982020-06-09 13:07:39 +0930872
Andrew Jeffery88412be2021-03-09 22:05:22 +1030873 rpkt = astlpc->proto->packet_size(MCTP_PACKET_SIZE(rmtu));
874 limit = MIN(limit, rpkt);
Andrew Jefferya9368982020-06-09 13:07:39 +0930875 }
Andrew Jeffery3a540662020-05-26 19:55:30 +0930876
877 /* Determine the accepted MTU, applied both directions by convention */
Andrew Jeffery88412be2021-03-09 22:05:22 +1030878 rpkt = MIN(limit, layout->tx.size);
879 return MCTP_BODY_SIZE(astlpc->proto->body_size(rpkt));
Andrew Jeffery3a540662020-05-26 19:55:30 +0930880}
881
Andrew Jeffery88412be2021-03-09 22:05:22 +1030882static int mctp_astlpc_negotiate_layout_bmc(struct mctp_binding_astlpc *astlpc)
Andrew Jeffery3a540662020-05-26 19:55:30 +0930883{
884 struct mctp_astlpc_layout proposed, pending;
885 uint32_t sz, mtu;
886 int rc;
887
Andrew Jeffery88412be2021-03-09 22:05:22 +1030888 /* Do we have a valid protocol version? */
889 if (!astlpc->proto->version)
890 return -EINVAL;
891
Andrew Jeffery3a540662020-05-26 19:55:30 +0930892 /* Extract the host's proposed layout */
893 rc = mctp_astlpc_layout_read(astlpc, &proposed);
894 if (rc < 0)
895 return rc;
896
Andrew Jeffery88412be2021-03-09 22:05:22 +1030897 /* Do we have a reasonable layout? */
898 if (!mctp_astlpc_layout_validate(astlpc, &proposed))
Andrew Jeffery3a540662020-05-26 19:55:30 +0930899 return -EINVAL;
900
901 /* Negotiate the MTU */
902 mtu = mctp_astlpc_calculate_mtu(astlpc, &proposed);
Andrew Jeffery88412be2021-03-09 22:05:22 +1030903 sz = astlpc->proto->packet_size(MCTP_PACKET_SIZE(mtu));
Andrew Jeffery3a540662020-05-26 19:55:30 +0930904
905 /*
906 * Use symmetric MTUs by convention and to pass constraints in rx/tx
907 * functions
908 */
909 pending = astlpc->layout;
910 pending.tx.size = sz;
911 pending.rx.size = sz;
912
Andrew Jeffery88412be2021-03-09 22:05:22 +1030913 if (mctp_astlpc_layout_validate(astlpc, &pending)) {
Andrew Jeffery3a540662020-05-26 19:55:30 +0930914 /* We found a sensible Rx MTU, so honour it */
915 astlpc->layout = pending;
916
917 /* Enforce the negotiated MTU */
918 rc = mctp_astlpc_layout_write(astlpc, &astlpc->layout);
919 if (rc < 0)
920 return rc;
921
922 astlpc_prinfo(astlpc, "Negotiated an MTU of %" PRIu32 " bytes",
923 mtu);
924 } else {
925 astlpc_prwarn(astlpc, "MTU negotiation failed");
926 return -EINVAL;
927 }
928
Andrew Jeffery88412be2021-03-09 22:05:22 +1030929 if (astlpc->proto->version >= 2)
Andrew Jeffery3a540662020-05-26 19:55:30 +0930930 astlpc->binding.pkt_size = MCTP_PACKET_SIZE(mtu);
931
932 return 0;
933}
934
Jeremy Kerr672c8852019-03-01 12:18:07 +0800935static void mctp_astlpc_init_channel(struct mctp_binding_astlpc *astlpc)
936{
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930937 uint16_t negotiated, negotiated_be;
938 struct mctp_lpcmap_hdr hdr;
939 uint8_t status;
Andrew Jeffery3a540662020-05-26 19:55:30 +0930940 int rc;
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930941
942 mctp_astlpc_lpc_read(astlpc, &hdr, 0, sizeof(hdr));
943
944 /* Version negotiation */
945 negotiated =
946 mctp_astlpc_negotiate_version(ASTLPC_VER_MIN, ASTLPC_VER_CUR,
947 be16toh(hdr.host_ver_min),
948 be16toh(hdr.host_ver_cur));
949
Andrew Jeffery88412be2021-03-09 22:05:22 +1030950 /* MTU negotiation requires knowing which protocol we'll use */
951 assert(negotiated < ARRAY_SIZE(astlpc_protocol_version));
952 astlpc->proto = &astlpc_protocol_version[negotiated];
953
Andrew Jeffery3a540662020-05-26 19:55:30 +0930954 /* Host Rx MTU negotiation: Failure terminates channel init */
Andrew Jeffery88412be2021-03-09 22:05:22 +1030955 rc = mctp_astlpc_negotiate_layout_bmc(astlpc);
Andrew Jeffery3a540662020-05-26 19:55:30 +0930956 if (rc < 0)
957 negotiated = ASTLPC_VER_BAD;
958
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930959 /* Populate the negotiated version */
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930960 negotiated_be = htobe16(negotiated);
961 mctp_astlpc_lpc_write(astlpc, &negotiated_be,
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930962 offsetof(struct mctp_lpcmap_hdr, negotiated_ver),
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930963 sizeof(negotiated_be));
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930964
Andrew Jefferyfe763e92022-08-05 23:16:17 +0930965 /* Track buffer ownership */
966 astlpc->layout.tx.state = buffer_state_acquired;
967 astlpc->layout.rx.state = buffer_state_released;
968
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930969 /* Finalise the configuration */
970 status = KCS_STATUS_BMC_READY | KCS_STATUS_OBF;
971 if (negotiated > 0) {
972 astlpc_prinfo(astlpc, "Negotiated binding version %" PRIu16,
973 negotiated);
974 status |= KCS_STATUS_CHANNEL_ACTIVE;
975 } else {
Andrew Jeffery88412be2021-03-09 22:05:22 +1030976 astlpc_prerr(astlpc, "Failed to initialise channel");
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930977 }
Jeremy Kerr672c8852019-03-01 12:18:07 +0800978
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930979 mctp_astlpc_kcs_set_status(astlpc, status);
980
981 mctp_binding_set_tx_enabled(&astlpc->binding,
982 status & KCS_STATUS_CHANNEL_ACTIVE);
Jeremy Kerr672c8852019-03-01 12:18:07 +0800983}
984
985static void mctp_astlpc_rx_start(struct mctp_binding_astlpc *astlpc)
986{
987 struct mctp_pktbuf *pkt;
Rashmica Guptaf447b4f2022-10-11 17:44:31 +1100988 struct mctp_hdr *hdr;
Andrew Jefferyeba19a32021-03-09 23:09:40 +1030989 uint32_t body, packet;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800990
Andrew Jefferyeba19a32021-03-09 23:09:40 +1030991 mctp_astlpc_lpc_read(astlpc, &body, astlpc->layout.rx.offset,
992 sizeof(body));
993 body = be32toh(body);
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530994
Andrew Jefferyeba19a32021-03-09 23:09:40 +1030995 if (body > astlpc->proto->body_size(astlpc->layout.rx.size)) {
996 astlpc_prwarn(astlpc, "invalid RX len 0x%x", body);
Jeremy Kerr672c8852019-03-01 12:18:07 +0800997 return;
998 }
999
Andrew Jeffery0f05b6c2022-11-15 11:37:50 +10301000 if ((size_t)body > astlpc->binding.pkt_size) {
Andrew Jefferyeba19a32021-03-09 23:09:40 +10301001 astlpc_prwarn(astlpc, "invalid RX len 0x%x", body);
Jeremy Kerr672c8852019-03-01 12:18:07 +08001002 return;
1003 }
1004
Andrew Jefferyeba19a32021-03-09 23:09:40 +10301005 /* Eliminate the medium-specific header that we just read */
1006 packet = astlpc->proto->packet_size(body) - 4;
1007 pkt = mctp_pktbuf_alloc(&astlpc->binding, packet);
Christian Geddesae59f4f2021-09-01 09:54:25 -05001008 if (!pkt) {
Patrick Williamsa721c2d2022-12-04 14:30:26 -06001009 astlpc_prwarn(astlpc, "unable to allocate pktbuf len 0x%x",
1010 packet);
Christian Geddesae59f4f2021-09-01 09:54:25 -05001011 return;
1012 }
Jeremy Kerr672c8852019-03-01 12:18:07 +08001013
Andrew Jefferyeba19a32021-03-09 23:09:40 +10301014 /*
1015 * Read payload and medium-specific trailer from immediately after the
1016 * medium-specific header.
1017 */
Andrew Jeffery55fb90b2020-05-12 13:54:37 +09301018 mctp_astlpc_lpc_read(astlpc, mctp_pktbuf_hdr(pkt),
Andrew Jefferyeba19a32021-03-09 23:09:40 +10301019 astlpc->layout.rx.offset + 4, packet);
Jeremy Kerr672c8852019-03-01 12:18:07 +08001020
Andrew Jefferyfe763e92022-08-05 23:16:17 +09301021 astlpc->layout.rx.state = buffer_state_prepared;
1022
Christian Geddesae59f4f2021-09-01 09:54:25 -05001023 /* Inform the other side of the MCTP interface that we have read
1024 * the packet off the bus before handling the contents of the packet.
1025 */
Rashmica Gupta45d13322022-08-10 12:12:14 +10001026 if (!mctp_astlpc_kcs_send(astlpc, cmd_rx_complete))
Andrew Jefferyfe763e92022-08-05 23:16:17 +09301027 astlpc->layout.rx.state = buffer_state_released;
Christian Geddesae59f4f2021-09-01 09:54:25 -05001028
Rashmica Guptaf447b4f2022-10-11 17:44:31 +11001029 hdr = mctp_pktbuf_hdr(pkt);
1030 if (hdr->ver != 1) {
1031 mctp_pktbuf_free(pkt);
1032 astlpc_prdebug(astlpc, "Dropped packet with invalid version");
1033 return;
1034 }
1035
Andrew Jefferyeba19a32021-03-09 23:09:40 +10301036 /*
1037 * v3 will validate the CRC32 in the medium-specific trailer and adjust
1038 * the packet size accordingly. On older protocols validation is a no-op
1039 * that always returns true.
1040 */
1041 if (astlpc->proto->pktbuf_validate(pkt)) {
1042 mctp_bus_rx(&astlpc->binding, pkt);
1043 } else {
1044 /* TODO: Drop any associated assembly */
1045 mctp_pktbuf_free(pkt);
1046 astlpc_prdebug(astlpc, "Dropped corrupt packet");
1047 }
Jeremy Kerr672c8852019-03-01 12:18:07 +08001048}
1049
1050static void mctp_astlpc_tx_complete(struct mctp_binding_astlpc *astlpc)
1051{
Andrew Jefferyfe763e92022-08-05 23:16:17 +09301052 astlpc->layout.tx.state = buffer_state_acquired;
Jeremy Kerr672c8852019-03-01 12:18:07 +08001053 mctp_binding_set_tx_enabled(&astlpc->binding, true);
1054}
1055
Andrew Jeffery4e8264b2020-05-23 20:34:33 +09301056static int mctp_astlpc_finalise_channel(struct mctp_binding_astlpc *astlpc)
1057{
Andrew Jeffery3a540662020-05-26 19:55:30 +09301058 struct mctp_astlpc_layout layout;
Andrew Jeffery4e8264b2020-05-23 20:34:33 +09301059 uint16_t negotiated;
1060 int rc;
1061
1062 rc = mctp_astlpc_lpc_read(astlpc, &negotiated,
1063 offsetof(struct mctp_lpcmap_hdr,
1064 negotiated_ver),
1065 sizeof(negotiated));
1066 if (rc < 0)
1067 return rc;
1068
1069 negotiated = be16toh(negotiated);
Andrew Jeffery88412be2021-03-09 22:05:22 +10301070 astlpc_prerr(astlpc, "Version negotiation got: %u", negotiated);
Andrew Jeffery4e8264b2020-05-23 20:34:33 +09301071
1072 if (negotiated == ASTLPC_VER_BAD || negotiated < ASTLPC_VER_MIN ||
1073 negotiated > ASTLPC_VER_CUR) {
1074 astlpc_prerr(astlpc, "Failed to negotiate version, got: %u\n",
1075 negotiated);
1076 return -EINVAL;
1077 }
1078
Andrew Jeffery88412be2021-03-09 22:05:22 +10301079 assert(negotiated < ARRAY_SIZE(astlpc_protocol_version));
1080 astlpc->proto = &astlpc_protocol_version[negotiated];
Andrew Jeffery4e8264b2020-05-23 20:34:33 +09301081
Andrew Jeffery3a540662020-05-26 19:55:30 +09301082 rc = mctp_astlpc_layout_read(astlpc, &layout);
1083 if (rc < 0)
1084 return rc;
1085
Andrew Jeffery88412be2021-03-09 22:05:22 +10301086 if (!mctp_astlpc_layout_validate(astlpc, &layout)) {
Andrew Jeffery3a540662020-05-26 19:55:30 +09301087 mctp_prerr("BMC proposed invalid buffer parameters");
1088 return -EINVAL;
1089 }
1090
1091 astlpc->layout = layout;
1092
1093 if (negotiated >= 2)
1094 astlpc->binding.pkt_size =
Andrew Jeffery88412be2021-03-09 22:05:22 +10301095 astlpc->proto->body_size(astlpc->layout.tx.size);
Andrew Jeffery3a540662020-05-26 19:55:30 +09301096
Andrew Jefferyfe763e92022-08-05 23:16:17 +09301097 /* Track buffer ownership */
1098 astlpc->layout.tx.state = buffer_state_acquired;
1099 astlpc->layout.rx.state = buffer_state_released;
1100
Andrew Jeffery4e8264b2020-05-23 20:34:33 +09301101 return 0;
1102}
1103
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301104static int mctp_astlpc_update_channel(struct mctp_binding_astlpc *astlpc,
1105 uint8_t status)
1106{
1107 uint8_t updated;
1108 int rc = 0;
1109
1110 assert(astlpc->mode == MCTP_BINDING_ASTLPC_MODE_HOST);
1111
1112 updated = astlpc->kcs_status ^ status;
1113
Andrew Jefferyd0f5da02020-05-28 09:12:55 +09301114 astlpc_prdebug(astlpc, "%s: status: 0x%x, update: 0x%x", __func__,
1115 status, updated);
1116
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301117 if (updated & KCS_STATUS_BMC_READY) {
Andrew Jefferyd0f5da02020-05-28 09:12:55 +09301118 if (status & KCS_STATUS_BMC_READY) {
1119 astlpc->kcs_status = status;
1120 return astlpc->binding.start(&astlpc->binding);
1121 } else {
Andrew Jefferyfe763e92022-08-05 23:16:17 +09301122 /* Shut down the channel */
1123 astlpc->layout.rx.state = buffer_state_idle;
1124 astlpc->layout.tx.state = buffer_state_idle;
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301125 mctp_binding_set_tx_enabled(&astlpc->binding, false);
Andrew Jefferyd0f5da02020-05-28 09:12:55 +09301126 }
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301127 }
1128
Andrew Jeffery88412be2021-03-09 22:05:22 +10301129 if (astlpc->proto->version == 0 ||
Patrick Williamsa721c2d2022-12-04 14:30:26 -06001130 updated & KCS_STATUS_CHANNEL_ACTIVE) {
Andrew Jeffery4e8264b2020-05-23 20:34:33 +09301131 bool enable;
1132
Andrew Jefferyfe763e92022-08-05 23:16:17 +09301133 astlpc->layout.rx.state = buffer_state_idle;
1134 astlpc->layout.tx.state = buffer_state_idle;
Andrew Jeffery4e8264b2020-05-23 20:34:33 +09301135 rc = mctp_astlpc_finalise_channel(astlpc);
1136 enable = (status & KCS_STATUS_CHANNEL_ACTIVE) && rc == 0;
Andrew Jeffery4e8264b2020-05-23 20:34:33 +09301137 mctp_binding_set_tx_enabled(&astlpc->binding, enable);
1138 }
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301139
1140 astlpc->kcs_status = status;
1141
1142 return rc;
1143}
1144
Jeremy Kerr672c8852019-03-01 12:18:07 +08001145int mctp_astlpc_poll(struct mctp_binding_astlpc *astlpc)
1146{
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301147 uint8_t status, data;
Jeremy Kerr672c8852019-03-01 12:18:07 +08001148 int rc;
1149
Andrew Jefferyfe763e92022-08-05 23:16:17 +09301150 if (astlpc->layout.rx.state == buffer_state_prepared)
Rashmica Gupta45d13322022-08-10 12:12:14 +10001151 if (!mctp_astlpc_kcs_send(astlpc, cmd_rx_complete))
Andrew Jefferyfe763e92022-08-05 23:16:17 +09301152 astlpc->layout.rx.state = buffer_state_released;
1153
1154 if (astlpc->layout.tx.state == buffer_state_prepared)
Rashmica Gupta45d13322022-08-10 12:12:14 +10001155 if (!mctp_astlpc_kcs_send(astlpc, cmd_tx_begin))
Andrew Jefferyfe763e92022-08-05 23:16:17 +09301156 astlpc->layout.tx.state = buffer_state_released;
1157
Andrew Jefferyf13cb972020-05-28 09:30:09 +09301158 rc = mctp_astlpc_kcs_read(astlpc, MCTP_ASTLPC_KCS_REG_STATUS, &status);
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301159 if (rc) {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +09301160 astlpc_prwarn(astlpc, "KCS read error");
Jeremy Kerr672c8852019-03-01 12:18:07 +08001161 return -1;
Jeremy Kerr672c8852019-03-01 12:18:07 +08001162 }
1163
Andrew Jeffery9101a2a2020-05-22 16:08:03 +09301164 astlpc_prdebug(astlpc, "%s: status: 0x%hhx", __func__, status);
Andrew Jefferyedfe3832020-02-06 11:52:11 +10301165
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301166 if (!mctp_astlpc_kcs_read_ready(astlpc, status))
Jeremy Kerr672c8852019-03-01 12:18:07 +08001167 return 0;
1168
Andrew Jefferyf13cb972020-05-28 09:30:09 +09301169 rc = mctp_astlpc_kcs_read(astlpc, MCTP_ASTLPC_KCS_REG_DATA, &data);
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301170 if (rc) {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +09301171 astlpc_prwarn(astlpc, "KCS data read error");
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301172 return -1;
1173 }
1174
Andrew Jeffery9101a2a2020-05-22 16:08:03 +09301175 astlpc_prdebug(astlpc, "%s: data: 0x%hhx", __func__, data);
Andrew Jefferyedfe3832020-02-06 11:52:11 +10301176
Rashmica Gupta45d13322022-08-10 12:12:14 +10001177 if (!astlpc->proto->version &&
1178 !(data == cmd_initialise || data == cmd_dummy_value)) {
Andrew Jefferyafcb7012021-01-28 17:00:36 +10301179 astlpc_prwarn(astlpc, "Invalid message for binding state: 0x%x",
1180 data);
1181 return 0;
1182 }
1183
Jeremy Kerr672c8852019-03-01 12:18:07 +08001184 switch (data) {
Rashmica Gupta45d13322022-08-10 12:12:14 +10001185 case cmd_initialise:
Jeremy Kerr672c8852019-03-01 12:18:07 +08001186 mctp_astlpc_init_channel(astlpc);
1187 break;
Rashmica Gupta45d13322022-08-10 12:12:14 +10001188 case cmd_tx_begin:
Andrew Jefferyfe763e92022-08-05 23:16:17 +09301189 if (astlpc->layout.rx.state != buffer_state_released) {
Patrick Williamsa721c2d2022-12-04 14:30:26 -06001190 astlpc_prerr(
1191 astlpc,
1192 "Protocol error: Invalid Rx buffer state for event %d: %d\n",
1193 data, astlpc->layout.rx.state);
Andrew Jefferyfe763e92022-08-05 23:16:17 +09301194 return 0;
1195 }
Jeremy Kerr672c8852019-03-01 12:18:07 +08001196 mctp_astlpc_rx_start(astlpc);
1197 break;
Rashmica Gupta45d13322022-08-10 12:12:14 +10001198 case cmd_rx_complete:
Andrew Jefferyfe763e92022-08-05 23:16:17 +09301199 if (astlpc->layout.tx.state != buffer_state_released) {
Patrick Williamsa721c2d2022-12-04 14:30:26 -06001200 astlpc_prerr(
1201 astlpc,
1202 "Protocol error: Invalid Tx buffer state for event %d: %d\n",
1203 data, astlpc->layout.tx.state);
Andrew Jefferyfe763e92022-08-05 23:16:17 +09301204 return 0;
1205 }
Jeremy Kerr672c8852019-03-01 12:18:07 +08001206 mctp_astlpc_tx_complete(astlpc);
1207 break;
Rashmica Gupta45d13322022-08-10 12:12:14 +10001208 case cmd_dummy_value:
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301209 /* No responsibilities for the BMC on 0xff */
Andrew Jefferyd0f5da02020-05-28 09:12:55 +09301210 if (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_HOST) {
1211 rc = mctp_astlpc_update_channel(astlpc, status);
1212 if (rc < 0)
1213 return rc;
1214 }
1215 break;
Jeremy Kerr672c8852019-03-01 12:18:07 +08001216 default:
Andrew Jeffery9101a2a2020-05-22 16:08:03 +09301217 astlpc_prwarn(astlpc, "unknown message 0x%x", data);
Jeremy Kerr672c8852019-03-01 12:18:07 +08001218 }
Andrew Jefferyd0f5da02020-05-28 09:12:55 +09301219
1220 /* Handle silent loss of bmc-ready */
1221 if (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_HOST) {
Rashmica Gupta45d13322022-08-10 12:12:14 +10001222 if (!(status & KCS_STATUS_BMC_READY && data == cmd_dummy_value))
Andrew Jefferyd0f5da02020-05-28 09:12:55 +09301223 return mctp_astlpc_update_channel(astlpc, status);
1224 }
1225
1226 return rc;
Jeremy Kerr672c8852019-03-01 12:18:07 +08001227}
1228
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301229/* allocate and basic initialisation */
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301230static struct mctp_binding_astlpc *__mctp_astlpc_init(uint8_t mode,
1231 uint32_t mtu)
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301232{
1233 struct mctp_binding_astlpc *astlpc;
1234
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301235 assert((mode == MCTP_BINDING_ASTLPC_MODE_BMC) ||
1236 (mode == MCTP_BINDING_ASTLPC_MODE_HOST));
1237
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301238 astlpc = __mctp_alloc(sizeof(*astlpc));
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301239 if (!astlpc)
1240 return NULL;
1241
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301242 memset(astlpc, 0, sizeof(*astlpc));
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301243 astlpc->mode = mode;
1244 astlpc->lpc_map = NULL;
Andrew Jefferyfe763e92022-08-05 23:16:17 +09301245 astlpc->layout.rx.state = buffer_state_idle;
1246 astlpc->layout.tx.state = buffer_state_idle;
Andrew Jefferya9368982020-06-09 13:07:39 +09301247 astlpc->requested_mtu = mtu;
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301248 astlpc->binding.name = "astlpc";
1249 astlpc->binding.version = 1;
Andrew Jeffery1a4f4412021-01-28 13:42:02 +10301250 astlpc->binding.pkt_size =
1251 MCTP_PACKET_SIZE(mtu > MCTP_BTU ? mtu : MCTP_BTU);
Andrew Jefferyeba19a32021-03-09 23:09:40 +10301252 astlpc->binding.pkt_header = 4;
1253 astlpc->binding.pkt_trailer = 4;
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301254 astlpc->binding.tx = mctp_binding_astlpc_tx;
1255 if (mode == MCTP_BINDING_ASTLPC_MODE_BMC)
1256 astlpc->binding.start = mctp_binding_astlpc_start_bmc;
1257 else if (mode == MCTP_BINDING_ASTLPC_MODE_HOST)
1258 astlpc->binding.start = mctp_binding_astlpc_start_host;
1259 else {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +09301260 astlpc_prerr(astlpc, "%s: Invalid mode: %d\n", __func__, mode);
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301261 __mctp_free(astlpc);
1262 return NULL;
1263 }
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301264
1265 return astlpc;
1266}
1267
Jeremy Kerr3b36d172019-09-04 11:56:09 +08001268struct mctp_binding *mctp_binding_astlpc_core(struct mctp_binding_astlpc *b)
1269{
1270 return &b->binding;
1271}
1272
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301273struct mctp_binding_astlpc *
1274mctp_astlpc_init(uint8_t mode, uint32_t mtu, void *lpc_map,
1275 const struct mctp_binding_astlpc_ops *ops, void *ops_data)
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301276{
1277 struct mctp_binding_astlpc *astlpc;
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301278
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301279 if (!(mode == MCTP_BINDING_ASTLPC_MODE_BMC ||
1280 mode == MCTP_BINDING_ASTLPC_MODE_HOST)) {
1281 mctp_prerr("Unknown binding mode: %u", mode);
1282 return NULL;
1283 }
1284
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301285 astlpc = __mctp_astlpc_init(mode, mtu);
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301286 if (!astlpc)
1287 return NULL;
1288
1289 memcpy(&astlpc->ops, ops, sizeof(astlpc->ops));
1290 astlpc->ops_data = ops_data;
1291 astlpc->lpc_map = lpc_map;
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301292 astlpc->mode = mode;
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301293
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301294 return astlpc;
1295}
1296
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301297struct mctp_binding_astlpc *
1298mctp_astlpc_init_ops(const struct mctp_binding_astlpc_ops *ops, void *ops_data,
1299 void *lpc_map)
1300{
1301 return mctp_astlpc_init(MCTP_BINDING_ASTLPC_MODE_BMC, MCTP_BTU, lpc_map,
1302 ops, ops_data);
1303}
1304
Andrew Jeffery4663f672020-03-10 23:36:24 +10301305void mctp_astlpc_destroy(struct mctp_binding_astlpc *astlpc)
1306{
Andrew Jefferyd0f5da02020-05-28 09:12:55 +09301307 /* Clear channel-active and bmc-ready */
1308 if (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_BMC)
Andrew Jeffery7c4509a2021-08-26 14:56:16 +09301309 mctp_astlpc_kcs_set_status(astlpc, 0);
Andrew Jeffery4663f672020-03-10 23:36:24 +10301310 __mctp_free(astlpc);
1311}
1312
Jeremy Kerrb214c642019-11-27 11:34:00 +08001313#ifdef MCTP_HAVE_FILEIO
Andrew Jeffery55fb90b2020-05-12 13:54:37 +09301314
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301315static int mctp_astlpc_init_fileio_lpc(struct mctp_binding_astlpc *astlpc)
Jeremy Kerr672c8852019-03-01 12:18:07 +08001316{
1317 struct aspeed_lpc_ctrl_mapping map = {
1318 .window_type = ASPEED_LPC_CTRL_WINDOW_MEMORY,
1319 .window_id = 0, /* There's only one */
1320 .flags = 0,
1321 .addr = 0,
1322 .offset = 0,
1323 .size = 0
1324 };
Andrew Jeffery979c6a12020-05-23 20:04:49 +09301325 void *lpc_map_base;
Jeremy Kerr672c8852019-03-01 12:18:07 +08001326 int fd, rc;
1327
1328 fd = open(lpc_path, O_RDWR | O_SYNC);
1329 if (fd < 0) {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +09301330 astlpc_prwarn(astlpc, "LPC open (%s) failed", lpc_path);
Jeremy Kerr672c8852019-03-01 12:18:07 +08001331 return -1;
1332 }
1333
1334 rc = ioctl(fd, ASPEED_LPC_CTRL_IOCTL_GET_SIZE, &map);
1335 if (rc) {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +09301336 astlpc_prwarn(astlpc, "LPC GET_SIZE failed");
Jeremy Kerr672c8852019-03-01 12:18:07 +08001337 close(fd);
1338 return -1;
1339 }
1340
Andrew Jefferyc1d5c542021-10-27 15:25:48 +10301341 /*
1342 * 🚨🚨🚨
1343 *
1344 * Decouple ourselves from hiomapd[1] (another user of the FW2AHB) by
1345 * mapping the FW2AHB to the reserved memory here as well.
1346 *
1347 * It's not possible to use the MCTP ASTLPC binding on machines that
1348 * need the FW2AHB bridge mapped anywhere except to the reserved memory
1349 * (e.g. the host SPI NOR).
1350 *
1351 * [1] https://github.com/openbmc/hiomapd/
1352 *
1353 * 🚨🚨🚨
1354 *
1355 * The following calculation must align with what's going on in
1356 * hiomapd's lpc.c so as not to disrupt its behaviour:
1357 *
1358 * https://github.com/openbmc/hiomapd/blob/5ff50e3cbd7702aefc185264e4adfb9952040575/lpc.c#L68
1359 *
1360 * 🚨🚨🚨
1361 */
1362
1363 /* Map the reserved memory at the top of the 28-bit LPC firmware address space */
1364 map.addr = 0x0FFFFFFF & -map.size;
Patrick Williamsa721c2d2022-12-04 14:30:26 -06001365 astlpc_prinfo(
1366 astlpc,
1367 "Configuring FW2AHB to map reserved memory at 0x%08x for 0x%x in the LPC FW cycle address-space",
1368 map.addr, map.size);
Andrew Jefferyc1d5c542021-10-27 15:25:48 +10301369
1370 rc = ioctl(fd, ASPEED_LPC_CTRL_IOCTL_MAP, &map);
1371 if (rc) {
Patrick Williamsa721c2d2022-12-04 14:30:26 -06001372 astlpc_prwarn(astlpc,
1373 "Failed to map FW2AHB to reserved memory");
Andrew Jefferyc1d5c542021-10-27 15:25:48 +10301374 close(fd);
1375 return -1;
1376 }
1377
1378 /* Map the reserved memory into our address space */
Andrew Jeffery979c6a12020-05-23 20:04:49 +09301379 lpc_map_base =
1380 mmap(NULL, map.size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
1381 if (lpc_map_base == MAP_FAILED) {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +09301382 astlpc_prwarn(astlpc, "LPC mmap failed");
Jeremy Kerr672c8852019-03-01 12:18:07 +08001383 rc = -1;
1384 } else {
Andrew Jeffery979c6a12020-05-23 20:04:49 +09301385 astlpc->lpc_map = lpc_map_base + map.size - LPC_WIN_SIZE;
Jeremy Kerr672c8852019-03-01 12:18:07 +08001386 }
1387
1388 close(fd);
1389
1390 return rc;
1391}
1392
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301393static int mctp_astlpc_init_fileio_kcs(struct mctp_binding_astlpc *astlpc)
Jeremy Kerr672c8852019-03-01 12:18:07 +08001394{
1395 astlpc->kcs_fd = open(kcs_path, O_RDWR);
1396 if (astlpc->kcs_fd < 0)
1397 return -1;
1398
1399 return 0;
1400}
1401
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301402static int __mctp_astlpc_fileio_kcs_read(void *arg,
Patrick Williamsa721c2d2022-12-04 14:30:26 -06001403 enum mctp_binding_astlpc_kcs_reg reg,
1404 uint8_t *val)
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301405{
1406 struct mctp_binding_astlpc *astlpc = arg;
1407 off_t offset = reg;
1408 int rc;
1409
1410 rc = pread(astlpc->kcs_fd, val, 1, offset);
1411
1412 return rc == 1 ? 0 : -1;
1413}
1414
1415static int __mctp_astlpc_fileio_kcs_write(void *arg,
Patrick Williamsa721c2d2022-12-04 14:30:26 -06001416 enum mctp_binding_astlpc_kcs_reg reg,
1417 uint8_t val)
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301418{
1419 struct mctp_binding_astlpc *astlpc = arg;
1420 off_t offset = reg;
1421 int rc;
1422
1423 rc = pwrite(astlpc->kcs_fd, &val, 1, offset);
1424
1425 return rc == 1 ? 0 : -1;
1426}
1427
Andrew Jeffery1111c6a2022-07-25 20:44:39 +09301428int mctp_astlpc_init_pollfd(struct mctp_binding_astlpc *astlpc,
1429 struct pollfd *pollfd)
1430{
Andrew Jefferyfe763e92022-08-05 23:16:17 +09301431 bool release;
1432
Andrew Jeffery1111c6a2022-07-25 20:44:39 +09301433 pollfd->fd = astlpc->kcs_fd;
Andrew Jefferyfe763e92022-08-05 23:16:17 +09301434 pollfd->events = 0;
1435
1436 release = astlpc->layout.rx.state == buffer_state_prepared ||
Patrick Williamsa721c2d2022-12-04 14:30:26 -06001437 astlpc->layout.tx.state == buffer_state_prepared;
Andrew Jefferyfe763e92022-08-05 23:16:17 +09301438
1439 pollfd->events = release ? POLLOUT : POLLIN;
Andrew Jeffery1111c6a2022-07-25 20:44:39 +09301440
1441 return 0;
1442}
1443
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301444struct mctp_binding_astlpc *mctp_astlpc_init_fileio(void)
Jeremy Kerr672c8852019-03-01 12:18:07 +08001445{
1446 struct mctp_binding_astlpc *astlpc;
1447 int rc;
1448
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301449 /*
1450 * If we're doing file IO then we're very likely not running
Andrew Jeffery8877c462020-06-15 12:22:53 +09301451 * freestanding, so lets assume that we're on the BMC side.
1452 *
1453 * Requesting an MTU of 0 requests the largest possible MTU, whatever
1454 * value that might take.
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301455 */
Andrew Jeffery8877c462020-06-15 12:22:53 +09301456 astlpc = __mctp_astlpc_init(MCTP_BINDING_ASTLPC_MODE_BMC, 0);
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301457 if (!astlpc)
1458 return NULL;
Jeremy Kerr672c8852019-03-01 12:18:07 +08001459
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301460 /* Set internal operations for kcs. We use direct accesses to the lpc
1461 * map area */
1462 astlpc->ops.kcs_read = __mctp_astlpc_fileio_kcs_read;
1463 astlpc->ops.kcs_write = __mctp_astlpc_fileio_kcs_write;
1464 astlpc->ops_data = astlpc;
1465
1466 rc = mctp_astlpc_init_fileio_lpc(astlpc);
Jeremy Kerr672c8852019-03-01 12:18:07 +08001467 if (rc) {
1468 free(astlpc);
1469 return NULL;
1470 }
1471
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301472 rc = mctp_astlpc_init_fileio_kcs(astlpc);
Jeremy Kerr672c8852019-03-01 12:18:07 +08001473 if (rc) {
1474 free(astlpc);
1475 return NULL;
1476 }
1477
Jeremy Kerr672c8852019-03-01 12:18:07 +08001478 return astlpc;
1479}
Jeremy Kerr92a10a62019-08-28 16:55:54 +05301480#else
Andrew Jeffery5b2702d2022-07-25 16:21:43 +09301481struct mctp_binding_astlpc *mctp_astlpc_init_fileio(void)
Jeremy Kerr92a10a62019-08-28 16:55:54 +05301482{
Andrew Jeffery5b2702d2022-07-25 16:21:43 +09301483 mctp_prlog(MCTP_LOG_ERR, "%s: Missing support for file IO", __func__);
Jeremy Kerr92a10a62019-08-28 16:55:54 +05301484 return NULL;
1485}
Jeremy Kerr672c8852019-03-01 12:18:07 +08001486
Andrew Jeffery1111c6a2022-07-25 20:44:39 +09301487int mctp_astlpc_init_pollfd(struct mctp_binding_astlpc *astlpc __unused,
1488 struct pollfd *pollfd __unused)
1489{
1490 mctp_prlog(MCTP_LOG_ERR, "%s: Missing support for file IO", __func__);
1491 return -1;
1492}
Jeremy Kerr92a10a62019-08-28 16:55:54 +05301493#endif