xref: /openbmc/libmctp/astlpc.c (revision 94977187)
1 /* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */
2 
3 #if HAVE_CONFIG_H
4 #include "config.h"
5 #endif
6 
7 #if HAVE_ENDIAN_H
8 #include <endian.h>
9 #endif
10 
11 #include <assert.h>
12 #include <err.h>
13 #include <errno.h>
14 #include <inttypes.h>
15 #include <stdbool.h>
16 #include <stdlib.h>
17 #include <string.h>
18 
19 #define pr_fmt(x) "astlpc: " x
20 
21 #include "container_of.h"
22 #include "crc32.h"
23 #include "libmctp.h"
24 #include "libmctp-alloc.h"
25 #include "libmctp-log.h"
26 #include "libmctp-astlpc.h"
27 #include "range.h"
28 
29 #ifdef MCTP_HAVE_FILEIO
30 
31 #include <unistd.h>
32 #include <fcntl.h>
33 #include <poll.h>
34 #include <sys/ioctl.h>
35 #include <sys/mman.h>
36 #include <linux/aspeed-lpc-ctrl.h>
37 
38 /* kernel interface */
39 static const char *kcs_path = "/dev/mctp0";
40 static const char *lpc_path = "/dev/aspeed-lpc-ctrl";
41 
42 #endif
43 
44 enum 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 
96 struct mctp_astlpc_buffer {
97 	uint32_t offset;
98 	uint32_t size;
99 	enum mctp_astlpc_buffer_state state;
100 };
101 
102 struct mctp_astlpc_layout {
103 	struct mctp_astlpc_buffer rx;
104 	struct mctp_astlpc_buffer tx;
105 };
106 
107 struct mctp_astlpc_protocol {
108 	uint16_t version;
109 	uint32_t (*packet_size)(uint32_t body);
110 	uint32_t (*body_size)(uint32_t packet);
111 	void (*pktbuf_protect)(struct mctp_pktbuf *pkt);
112 	bool (*pktbuf_validate)(struct mctp_pktbuf *pkt);
113 };
114 
115 struct mctp_binding_astlpc {
116 	struct mctp_binding	binding;
117 
118 	void *lpc_map;
119 	struct mctp_astlpc_layout layout;
120 
121 	uint8_t mode;
122 	uint32_t requested_mtu;
123 
124 	const struct mctp_astlpc_protocol *proto;
125 
126 	/* direct ops data */
127 	struct mctp_binding_astlpc_ops ops;
128 	void *ops_data;
129 
130 	/* fileio ops data */
131 	int kcs_fd;
132 	uint8_t kcs_status;
133 };
134 
135 #define binding_to_astlpc(b) \
136 	container_of(b, struct mctp_binding_astlpc, binding)
137 
138 #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 
154 /* clang-format off */
155 #define ASTLPC_MCTP_MAGIC	0x4d435450
156 #define ASTLPC_VER_BAD	0
157 #define ASTLPC_VER_MIN	1
158 
159 /* Support testing of new binding protocols */
160 #ifndef ASTLPC_VER_CUR
161 #define ASTLPC_VER_CUR	3
162 #endif
163 /* clang-format on */
164 
165 #ifndef ARRAY_SIZE
166 #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
167 #endif
168 
169 static uint32_t astlpc_packet_size_v1(uint32_t body)
170 {
171 	assert((body + 4) > body);
172 
173 	return body + 4;
174 }
175 
176 static uint32_t astlpc_body_size_v1(uint32_t packet)
177 {
178 	assert((packet - 4) < packet);
179 
180 	return packet - 4;
181 }
182 
183 void astlpc_pktbuf_protect_v1(struct mctp_pktbuf *pkt)
184 {
185 	(void)pkt;
186 }
187 
188 bool astlpc_pktbuf_validate_v1(struct mctp_pktbuf *pkt)
189 {
190 	(void)pkt;
191 	return true;
192 }
193 
194 static uint32_t astlpc_packet_size_v3(uint32_t body)
195 {
196 	assert((body + 4 + 4) > body);
197 
198 	return body + 4 + 4;
199 }
200 
201 static uint32_t astlpc_body_size_v3(uint32_t packet)
202 {
203 	assert((packet - 4 - 4) < packet);
204 
205 	return packet - 4 - 4;
206 }
207 
208 void 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 
217 bool 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 
228 static const struct mctp_astlpc_protocol astlpc_protocol_version[] = {
229 	[0] = {
230 		.version = 0,
231 		.packet_size = NULL,
232 		.body_size = NULL,
233 		.pktbuf_protect = NULL,
234 		.pktbuf_validate = NULL,
235 	},
236 	[1] = {
237 		.version = 1,
238 		.packet_size = astlpc_packet_size_v1,
239 		.body_size = astlpc_body_size_v1,
240 		.pktbuf_protect = astlpc_pktbuf_protect_v1,
241 		.pktbuf_validate = astlpc_pktbuf_validate_v1,
242 	},
243 	[2] = {
244 		.version = 2,
245 		.packet_size = astlpc_packet_size_v1,
246 		.body_size = astlpc_body_size_v1,
247 		.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,
256 	},
257 };
258 
259 struct mctp_lpcmap_hdr {
260 	uint32_t magic;
261 
262 	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;
268 
269 	struct {
270 		uint32_t rx_offset;
271 		uint32_t rx_size;
272 		uint32_t tx_offset;
273 		uint32_t tx_size;
274 	} layout;
275 } __attribute__((packed));
276 
277 static const uint32_t control_size = 0x100;
278 
279 #define LPC_WIN_SIZE                (1 * 1024 * 1024)
280 
281 #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 
286 static 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 
293 static 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 
300 static inline int mctp_astlpc_lpc_write(struct mctp_binding_astlpc *astlpc,
301 					const void *buf, long offset,
302 					size_t len)
303 {
304 	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 
322 static 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;
342 }
343 
344 static 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;
357 
358 	rc = mctp_astlpc_kcs_write(astlpc, MCTP_ASTLPC_KCS_REG_STATUS, status);
359 	if (rc) {
360 		astlpc_prwarn(astlpc, "KCS status write failed");
361 		return -1;
362 	}
363 
364 	rc = mctp_astlpc_kcs_write(astlpc, MCTP_ASTLPC_KCS_REG_DATA, data);
365 	if (rc) {
366 		astlpc_prwarn(astlpc, "KCS dummy data write failed");
367 		return -1;
368 	}
369 
370 	return 0;
371 }
372 
373 static 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 
401 static 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 
435 static bool
436 mctp_astlpc_buffer_validate(const struct mctp_binding_astlpc *astlpc,
437 			    const struct mctp_astlpc_buffer *buf,
438 			    const char *name)
439 {
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 */
460 	if (buf->size < astlpc->proto->packet_size(MCTP_PACKET_SIZE(MCTP_BTU))) {
461 		mctp_prerr(
462 			"%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)),
465 			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 
481 static bool
482 mctp_astlpc_layout_validate(const struct mctp_binding_astlpc *astlpc,
483 			    const struct mctp_astlpc_layout *layout)
484 {
485 	const struct mctp_astlpc_buffer *rx = &layout->rx;
486 	const struct mctp_astlpc_buffer *tx = &layout->tx;
487 	bool rx_valid, tx_valid;
488 
489 	rx_valid = mctp_astlpc_buffer_validate(astlpc, rx, "Rx");
490 	tx_valid = mctp_astlpc_buffer_validate(astlpc, tx, "Tx");
491 
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 
507 static int mctp_astlpc_init_bmc(struct mctp_binding_astlpc *astlpc)
508 {
509 	struct mctp_lpcmap_hdr hdr = { 0 };
510 	uint8_t status;
511 	uint32_t sz;
512 
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 	 */
523 	sz = MCTP_BODY_SIZE(astlpc->proto->body_size(sz));
524 	sz &= ~0xfUL;
525 	sz = astlpc->proto->packet_size(MCTP_PACKET_SIZE(sz));
526 
527 	if (astlpc->requested_mtu) {
528 		uint32_t rpkt, rmtu;
529 
530 		rmtu = astlpc->requested_mtu;
531 		rpkt = astlpc->proto->packet_size(MCTP_PACKET_SIZE(rmtu));
532 		sz = MIN(sz, rpkt);
533 	}
534 
535 	/* Flip the buffers as the names are defined in terms of the host */
536 	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 
542 	if (!mctp_astlpc_layout_validate(astlpc, &astlpc->layout)) {
543 		astlpc_prerr(astlpc, "Cannot support an MTU of %" PRIu32, sz);
544 		return -EINVAL;
545 	}
546 
547 	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),
551 
552 		/* Flip the buffers back as we're now describing the host's
553 		 * configuration to the host */
554 		.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),
558 	};
559 
560 	mctp_astlpc_lpc_write(astlpc, &hdr, 0, sizeof(hdr));
561 
562 	/*
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;
568 	return mctp_astlpc_kcs_set_status(astlpc, status);
569 }
570 
571 static 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 
576 	astlpc->proto = &astlpc_protocol_version[ASTLPC_VER_CUR];
577 
578 	return mctp_astlpc_init_bmc(astlpc);
579 }
580 
581 static 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 
614 static int mctp_astlpc_negotiate_layout_host(struct mctp_binding_astlpc *astlpc)
615 {
616 	struct mctp_astlpc_layout layout;
617 	uint32_t rmtu;
618 	uint32_t sz;
619 	int rc;
620 
621 	rc = mctp_astlpc_layout_read(astlpc, &layout);
622 	if (rc < 0)
623 		return rc;
624 
625 	if (!mctp_astlpc_layout_validate(astlpc, &layout)) {
626 		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 
635 	astlpc_prinfo(astlpc, "Desire an MTU of %" PRIu32 " bytes",
636 		      astlpc->requested_mtu);
637 
638 	rmtu = astlpc->requested_mtu;
639 	sz = astlpc->proto->packet_size(MCTP_PACKET_SIZE(rmtu));
640 	layout.rx.size = sz;
641 
642 	if (!mctp_astlpc_layout_validate(astlpc, &layout)) {
643 		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 
653 	astlpc_prinfo(astlpc, "Requesting MTU of %" PRIu32 " bytes",
654 		      astlpc->requested_mtu);
655 
656 	return mctp_astlpc_layout_write(astlpc, &layout);
657 }
658 
659 static 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 
674 static int mctp_astlpc_init_host(struct mctp_binding_astlpc *astlpc)
675 {
676 	const uint16_t ver_min_be = htobe16(ASTLPC_VER_MIN);
677 	const uint16_t ver_cur_be = htobe16(ASTLPC_VER_CUR);
678 	uint16_t bmc_ver_min, bmc_ver_cur, negotiated;
679 	struct mctp_lpcmap_hdr hdr;
680 	uint8_t status;
681 	int rc;
682 
683 	rc = mctp_astlpc_kcs_read(astlpc, MCTP_ASTLPC_KCS_REG_STATUS, &status);
684 	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 
694 	mctp_astlpc_lpc_read(astlpc, &hdr, 0, sizeof(hdr));
695 
696 	bmc_ver_min = be16toh(hdr.bmc_ver_min);
697 	bmc_ver_cur = be16toh(hdr.bmc_ver_cur);
698 
699 	/* 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) {
704 		astlpc_prerr(astlpc, "Cannot negotiate with invalid versions");
705 		return -EINVAL;
706 	}
707 
708 	/* 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) {
714 		rc = mctp_astlpc_negotiate_layout_host(astlpc);
715 		if (rc < 0)
716 			return rc;
717 	}
718 
719 	/* Advertise the host's supported protocol versions */
720 	mctp_astlpc_lpc_write(astlpc, &ver_min_be,
721 			      offsetof(struct mctp_lpcmap_hdr, host_ver_min),
722 			      sizeof(ver_min_be));
723 
724 	mctp_astlpc_lpc_write(astlpc, &ver_cur_be,
725 			      offsetof(struct mctp_lpcmap_hdr, host_ver_cur),
726 			      sizeof(ver_cur_be));
727 
728 	/* Send channel init command */
729 	rc = mctp_astlpc_kcs_write(astlpc, MCTP_ASTLPC_KCS_REG_DATA, 0x0);
730 	if (rc) {
731 		astlpc_prwarn(astlpc, "KCS write failed");
732 	}
733 
734 	/*
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 
746 	return rc;
747 }
748 
749 static 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 
757 static 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 
771 static inline bool
772 mctp_astlpc_kcs_read_ready(struct mctp_binding_astlpc *astlpc, uint8_t status)
773 {
774 	return __mctp_astlpc_kcs_ready(astlpc, status, false);
775 }
776 
777 static inline bool
778 mctp_astlpc_kcs_write_ready(struct mctp_binding_astlpc *astlpc, uint8_t status)
779 {
780 	return __mctp_astlpc_kcs_ready(astlpc, status, true);
781 }
782 
783 static int mctp_astlpc_kcs_send(struct mctp_binding_astlpc *astlpc,
784 		uint8_t data)
785 {
786 	uint8_t status;
787 	int rc;
788 
789 	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;
794 	}
795 	if (!mctp_astlpc_kcs_write_ready(astlpc, status))
796 		return -EBUSY;
797 
798 	rc = mctp_astlpc_kcs_write(astlpc, MCTP_ASTLPC_KCS_REG_DATA, data);
799 	if (rc) {
800 		astlpc_prwarn(astlpc, "KCS data write failed");
801 		return -EIO;
802 	}
803 
804 	return 0;
805 }
806 
807 static 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);
811 	uint32_t len, len_be;
812 	struct mctp_hdr *hdr;
813 	int rc;
814 
815 	hdr = mctp_pktbuf_hdr(pkt);
816 	len = mctp_pktbuf_size(pkt);
817 
818 	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);
822 
823 	if (len > astlpc->proto->body_size(astlpc->layout.tx.size)) {
824 		astlpc_prwarn(astlpc, "invalid TX len %" PRIu32 ": %" PRIu32,
825 			      len,
826 			      astlpc->proto->body_size(astlpc->layout.tx.size));
827 		return -EMSGSIZE;
828 	}
829 
830 	mctp_binding_set_tx_enabled(b, false);
831 
832 	len_be = htobe32(len);
833 	mctp_astlpc_lpc_write(astlpc, &len_be, astlpc->layout.tx.offset,
834 			      sizeof(len_be));
835 
836 	astlpc->proto->pktbuf_protect(pkt);
837 	len = mctp_pktbuf_size(pkt);
838 
839 	mctp_astlpc_lpc_write(astlpc, hdr, astlpc->layout.tx.offset + 4, len);
840 
841 	astlpc->layout.tx.state = buffer_state_prepared;
842 
843 	rc = mctp_astlpc_kcs_send(astlpc, 0x1);
844 	if (!rc)
845 		astlpc->layout.tx.state = buffer_state_released;
846 
847 	return rc == -EBUSY ? 0 : rc;
848 }
849 
850 static uint32_t mctp_astlpc_calculate_mtu(struct mctp_binding_astlpc *astlpc,
851 					  struct mctp_astlpc_layout *layout)
852 {
853 	uint32_t low, high, limit, rpkt;
854 
855 	/* Derive the largest MTU the BMC _can_ support */
856 	low = MIN(astlpc->layout.rx.offset, astlpc->layout.tx.offset);
857 	high = MAX(astlpc->layout.rx.offset, astlpc->layout.tx.offset);
858 	limit = high - low;
859 
860 	/* Determine the largest MTU the BMC _wants_ to support */
861 	if (astlpc->requested_mtu) {
862 		uint32_t rmtu = astlpc->requested_mtu;
863 
864 		rpkt = astlpc->proto->packet_size(MCTP_PACKET_SIZE(rmtu));
865 		limit = MIN(limit, rpkt);
866 	}
867 
868 	/* Determine the accepted MTU, applied both directions by convention */
869 	rpkt = MIN(limit, layout->tx.size);
870 	return MCTP_BODY_SIZE(astlpc->proto->body_size(rpkt));
871 }
872 
873 static int mctp_astlpc_negotiate_layout_bmc(struct mctp_binding_astlpc *astlpc)
874 {
875 	struct mctp_astlpc_layout proposed, pending;
876 	uint32_t sz, mtu;
877 	int rc;
878 
879 	/* Do we have a valid protocol version? */
880 	if (!astlpc->proto->version)
881 		return -EINVAL;
882 
883 	/* Extract the host's proposed layout */
884 	rc = mctp_astlpc_layout_read(astlpc, &proposed);
885 	if (rc < 0)
886 		return rc;
887 
888 	/* Do we have a reasonable layout? */
889 	if (!mctp_astlpc_layout_validate(astlpc, &proposed))
890 		return -EINVAL;
891 
892 	/* Negotiate the MTU */
893 	mtu = mctp_astlpc_calculate_mtu(astlpc, &proposed);
894 	sz = astlpc->proto->packet_size(MCTP_PACKET_SIZE(mtu));
895 
896 	/*
897 	 * Use symmetric MTUs by convention and to pass constraints in rx/tx
898 	 * functions
899 	 */
900 	pending = astlpc->layout;
901 	pending.tx.size = sz;
902 	pending.rx.size = sz;
903 
904 	if (mctp_astlpc_layout_validate(astlpc, &pending)) {
905 		/* We found a sensible Rx MTU, so honour it */
906 		astlpc->layout = pending;
907 
908 		/* Enforce the negotiated MTU */
909 		rc = mctp_astlpc_layout_write(astlpc, &astlpc->layout);
910 		if (rc < 0)
911 			return rc;
912 
913 		astlpc_prinfo(astlpc, "Negotiated an MTU of %" PRIu32 " bytes",
914 			      mtu);
915 	} else {
916 		astlpc_prwarn(astlpc, "MTU negotiation failed");
917 		return -EINVAL;
918 	}
919 
920 	if (astlpc->proto->version >= 2)
921 		astlpc->binding.pkt_size = MCTP_PACKET_SIZE(mtu);
922 
923 	return 0;
924 }
925 
926 static void mctp_astlpc_init_channel(struct mctp_binding_astlpc *astlpc)
927 {
928 	uint16_t negotiated, negotiated_be;
929 	struct mctp_lpcmap_hdr hdr;
930 	uint8_t status;
931 	int rc;
932 
933 	mctp_astlpc_lpc_read(astlpc, &hdr, 0, sizeof(hdr));
934 
935 	/* Version negotiation */
936 	negotiated =
937 		mctp_astlpc_negotiate_version(ASTLPC_VER_MIN, ASTLPC_VER_CUR,
938 					      be16toh(hdr.host_ver_min),
939 					      be16toh(hdr.host_ver_cur));
940 
941 	/* MTU negotiation requires knowing which protocol we'll use */
942 	assert(negotiated < ARRAY_SIZE(astlpc_protocol_version));
943 	astlpc->proto = &astlpc_protocol_version[negotiated];
944 
945 	/* Host Rx MTU negotiation: Failure terminates channel init */
946 	rc = mctp_astlpc_negotiate_layout_bmc(astlpc);
947 	if (rc < 0)
948 		negotiated = ASTLPC_VER_BAD;
949 
950 	/* Populate the negotiated version */
951 	negotiated_be = htobe16(negotiated);
952 	mctp_astlpc_lpc_write(astlpc, &negotiated_be,
953 			      offsetof(struct mctp_lpcmap_hdr, negotiated_ver),
954 			      sizeof(negotiated_be));
955 
956 	/* Track buffer ownership */
957 	astlpc->layout.tx.state = buffer_state_acquired;
958 	astlpc->layout.rx.state = buffer_state_released;
959 
960 	/* Finalise the configuration */
961 	status = KCS_STATUS_BMC_READY | KCS_STATUS_OBF;
962 	if (negotiated > 0) {
963 		astlpc_prinfo(astlpc, "Negotiated binding version %" PRIu16,
964 			      negotiated);
965 		status |= KCS_STATUS_CHANNEL_ACTIVE;
966 	} else {
967 		astlpc_prerr(astlpc, "Failed to initialise channel");
968 	}
969 
970 	mctp_astlpc_kcs_set_status(astlpc, status);
971 
972 	mctp_binding_set_tx_enabled(&astlpc->binding,
973 				    status & KCS_STATUS_CHANNEL_ACTIVE);
974 }
975 
976 static void mctp_astlpc_rx_start(struct mctp_binding_astlpc *astlpc)
977 {
978 	struct mctp_pktbuf *pkt;
979 	uint32_t body, packet;
980 
981 	mctp_astlpc_lpc_read(astlpc, &body, astlpc->layout.rx.offset,
982 			     sizeof(body));
983 	body = be32toh(body);
984 
985 	if (body > astlpc->proto->body_size(astlpc->layout.rx.size)) {
986 		astlpc_prwarn(astlpc, "invalid RX len 0x%x", body);
987 		return;
988 	}
989 
990 	assert(astlpc->binding.pkt_size >= 0);
991 	if (body > (uint32_t)astlpc->binding.pkt_size) {
992 		astlpc_prwarn(astlpc, "invalid RX len 0x%x", body);
993 		return;
994 	}
995 
996 	/* Eliminate the medium-specific header that we just read */
997 	packet = astlpc->proto->packet_size(body) - 4;
998 	pkt = mctp_pktbuf_alloc(&astlpc->binding, packet);
999 	if (!pkt) {
1000 		astlpc_prwarn(astlpc, "unable to allocate pktbuf len 0x%x", packet);
1001 		return;
1002 	}
1003 
1004 	/*
1005 	 * Read payload and medium-specific trailer from immediately after the
1006 	 * medium-specific header.
1007 	 */
1008 	mctp_astlpc_lpc_read(astlpc, mctp_pktbuf_hdr(pkt),
1009 			     astlpc->layout.rx.offset + 4, packet);
1010 
1011 	astlpc->layout.rx.state = buffer_state_prepared;
1012 
1013 	/* Inform the other side of the MCTP interface that we have read
1014 	 * the packet off the bus before handling the contents of the packet.
1015 	 */
1016 	if (!mctp_astlpc_kcs_send(astlpc, 0x2))
1017 		astlpc->layout.rx.state = buffer_state_released;
1018 
1019 	/*
1020 	 * v3 will validate the CRC32 in the medium-specific trailer and adjust
1021 	 * the packet size accordingly. On older protocols validation is a no-op
1022 	 * that always returns true.
1023 	 */
1024 	if (astlpc->proto->pktbuf_validate(pkt)) {
1025 		mctp_bus_rx(&astlpc->binding, pkt);
1026 	} else {
1027 		/* TODO: Drop any associated assembly */
1028 		mctp_pktbuf_free(pkt);
1029 		astlpc_prdebug(astlpc, "Dropped corrupt packet");
1030 	}
1031 }
1032 
1033 static void mctp_astlpc_tx_complete(struct mctp_binding_astlpc *astlpc)
1034 {
1035 	astlpc->layout.tx.state = buffer_state_acquired;
1036 	mctp_binding_set_tx_enabled(&astlpc->binding, true);
1037 }
1038 
1039 static int mctp_astlpc_finalise_channel(struct mctp_binding_astlpc *astlpc)
1040 {
1041 	struct mctp_astlpc_layout layout;
1042 	uint16_t negotiated;
1043 	int rc;
1044 
1045 	rc = mctp_astlpc_lpc_read(astlpc, &negotiated,
1046 				  offsetof(struct mctp_lpcmap_hdr,
1047 					   negotiated_ver),
1048 				  sizeof(negotiated));
1049 	if (rc < 0)
1050 		return rc;
1051 
1052 	negotiated = be16toh(negotiated);
1053 	astlpc_prerr(astlpc, "Version negotiation got: %u", negotiated);
1054 
1055 	if (negotiated == ASTLPC_VER_BAD || negotiated < ASTLPC_VER_MIN ||
1056 	    negotiated > ASTLPC_VER_CUR) {
1057 		astlpc_prerr(astlpc, "Failed to negotiate version, got: %u\n",
1058 			     negotiated);
1059 		return -EINVAL;
1060 	}
1061 
1062 	assert(negotiated < ARRAY_SIZE(astlpc_protocol_version));
1063 	astlpc->proto = &astlpc_protocol_version[negotiated];
1064 
1065 	rc = mctp_astlpc_layout_read(astlpc, &layout);
1066 	if (rc < 0)
1067 		return rc;
1068 
1069 	if (!mctp_astlpc_layout_validate(astlpc, &layout)) {
1070 		mctp_prerr("BMC proposed invalid buffer parameters");
1071 		return -EINVAL;
1072 	}
1073 
1074 	astlpc->layout = layout;
1075 
1076 	if (negotiated >= 2)
1077 		astlpc->binding.pkt_size =
1078 			astlpc->proto->body_size(astlpc->layout.tx.size);
1079 
1080 	/* Track buffer ownership */
1081 	astlpc->layout.tx.state = buffer_state_acquired;
1082 	astlpc->layout.rx.state = buffer_state_released;
1083 
1084 	return 0;
1085 }
1086 
1087 static int mctp_astlpc_update_channel(struct mctp_binding_astlpc *astlpc,
1088 				      uint8_t status)
1089 {
1090 	uint8_t updated;
1091 	int rc = 0;
1092 
1093 	assert(astlpc->mode == MCTP_BINDING_ASTLPC_MODE_HOST);
1094 
1095 	updated = astlpc->kcs_status ^ status;
1096 
1097 	astlpc_prdebug(astlpc, "%s: status: 0x%x, update: 0x%x", __func__,
1098 		       status, updated);
1099 
1100 	if (updated & KCS_STATUS_BMC_READY) {
1101 		if (status & KCS_STATUS_BMC_READY) {
1102 			astlpc->kcs_status = status;
1103 			return astlpc->binding.start(&astlpc->binding);
1104 		} else {
1105 			/* Shut down the channel */
1106 			astlpc->layout.rx.state = buffer_state_idle;
1107 			astlpc->layout.tx.state = buffer_state_idle;
1108 			mctp_binding_set_tx_enabled(&astlpc->binding, false);
1109 		}
1110 	}
1111 
1112 	if (astlpc->proto->version == 0 ||
1113 			updated & KCS_STATUS_CHANNEL_ACTIVE) {
1114 		bool enable;
1115 
1116 		astlpc->layout.rx.state = buffer_state_idle;
1117 		astlpc->layout.tx.state = buffer_state_idle;
1118 		rc = mctp_astlpc_finalise_channel(astlpc);
1119 		enable = (status & KCS_STATUS_CHANNEL_ACTIVE) && rc == 0;
1120 		mctp_binding_set_tx_enabled(&astlpc->binding, enable);
1121 	}
1122 
1123 	astlpc->kcs_status = status;
1124 
1125 	return rc;
1126 }
1127 
1128 int mctp_astlpc_poll(struct mctp_binding_astlpc *astlpc)
1129 {
1130 	uint8_t status, data;
1131 	int rc;
1132 
1133 	if (astlpc->layout.rx.state == buffer_state_prepared)
1134 		if (!mctp_astlpc_kcs_send(astlpc, 0x2))
1135 			astlpc->layout.rx.state = buffer_state_released;
1136 
1137 	if (astlpc->layout.tx.state == buffer_state_prepared)
1138 		if (!mctp_astlpc_kcs_send(astlpc, 0x1))
1139 			astlpc->layout.tx.state = buffer_state_released;
1140 
1141 	rc = mctp_astlpc_kcs_read(astlpc, MCTP_ASTLPC_KCS_REG_STATUS, &status);
1142 	if (rc) {
1143 		astlpc_prwarn(astlpc, "KCS read error");
1144 		return -1;
1145 	}
1146 
1147 	astlpc_prdebug(astlpc, "%s: status: 0x%hhx", __func__, status);
1148 
1149 	if (!mctp_astlpc_kcs_read_ready(astlpc, status))
1150 		return 0;
1151 
1152 	rc = mctp_astlpc_kcs_read(astlpc, MCTP_ASTLPC_KCS_REG_DATA, &data);
1153 	if (rc) {
1154 		astlpc_prwarn(astlpc, "KCS data read error");
1155 		return -1;
1156 	}
1157 
1158 	astlpc_prdebug(astlpc, "%s: data: 0x%hhx", __func__, data);
1159 
1160 	if (!astlpc->proto->version && !(data == 0x0 || data == 0xff)) {
1161 		astlpc_prwarn(astlpc, "Invalid message for binding state: 0x%x",
1162 			      data);
1163 		return 0;
1164 	}
1165 
1166 	switch (data) {
1167 	case 0x0:
1168 		mctp_astlpc_init_channel(astlpc);
1169 		break;
1170 	case 0x1:
1171 		if (astlpc->layout.rx.state != buffer_state_released) {
1172 			astlpc_prerr(astlpc,
1173 				     "Protocol error: Invalid Rx buffer state for event %d: %d\n",
1174 				     data, astlpc->layout.rx.state);
1175 			return 0;
1176 		}
1177 		mctp_astlpc_rx_start(astlpc);
1178 		break;
1179 	case 0x2:
1180 		if (astlpc->layout.tx.state != buffer_state_released) {
1181 			astlpc_prerr(astlpc,
1182 				     "Protocol error: Invalid Tx buffer state for event %d: %d\n",
1183 				     data, astlpc->layout.tx.state);
1184 			return 0;
1185 		}
1186 		mctp_astlpc_tx_complete(astlpc);
1187 		break;
1188 	case 0xff:
1189 		/* No responsibilities for the BMC on 0xff */
1190 		if (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_HOST) {
1191 			rc = mctp_astlpc_update_channel(astlpc, status);
1192 			if (rc < 0)
1193 				return rc;
1194 		}
1195 		break;
1196 	default:
1197 		astlpc_prwarn(astlpc, "unknown message 0x%x", data);
1198 	}
1199 
1200 	/* Handle silent loss of bmc-ready */
1201 	if (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_HOST) {
1202 		if (!(status & KCS_STATUS_BMC_READY && data == 0xff))
1203 			return mctp_astlpc_update_channel(astlpc, status);
1204 	}
1205 
1206 	return rc;
1207 }
1208 
1209 /* allocate and basic initialisation */
1210 static struct mctp_binding_astlpc *__mctp_astlpc_init(uint8_t mode,
1211 						      uint32_t mtu)
1212 {
1213 	struct mctp_binding_astlpc *astlpc;
1214 
1215 	assert((mode == MCTP_BINDING_ASTLPC_MODE_BMC) ||
1216 	       (mode == MCTP_BINDING_ASTLPC_MODE_HOST));
1217 
1218 	astlpc = __mctp_alloc(sizeof(*astlpc));
1219 	if (!astlpc)
1220 		return NULL;
1221 
1222 	memset(astlpc, 0, sizeof(*astlpc));
1223 	astlpc->mode = mode;
1224 	astlpc->lpc_map = NULL;
1225 	astlpc->layout.rx.state = buffer_state_idle;
1226 	astlpc->layout.tx.state = buffer_state_idle;
1227 	astlpc->requested_mtu = mtu;
1228 	astlpc->binding.name = "astlpc";
1229 	astlpc->binding.version = 1;
1230 	astlpc->binding.pkt_size =
1231 		MCTP_PACKET_SIZE(mtu > MCTP_BTU ? mtu : MCTP_BTU);
1232 	astlpc->binding.pkt_header = 4;
1233 	astlpc->binding.pkt_trailer = 4;
1234 	astlpc->binding.tx = mctp_binding_astlpc_tx;
1235 	if (mode == MCTP_BINDING_ASTLPC_MODE_BMC)
1236 		astlpc->binding.start = mctp_binding_astlpc_start_bmc;
1237 	else if (mode == MCTP_BINDING_ASTLPC_MODE_HOST)
1238 		astlpc->binding.start = mctp_binding_astlpc_start_host;
1239 	else {
1240 		astlpc_prerr(astlpc, "%s: Invalid mode: %d\n", __func__, mode);
1241 		__mctp_free(astlpc);
1242 		return NULL;
1243 	}
1244 
1245 	return astlpc;
1246 }
1247 
1248 struct mctp_binding *mctp_binding_astlpc_core(struct mctp_binding_astlpc *b)
1249 {
1250 	return &b->binding;
1251 }
1252 
1253 struct mctp_binding_astlpc *
1254 mctp_astlpc_init(uint8_t mode, uint32_t mtu, void *lpc_map,
1255 		 const struct mctp_binding_astlpc_ops *ops, void *ops_data)
1256 {
1257 	struct mctp_binding_astlpc *astlpc;
1258 
1259 	if (!(mode == MCTP_BINDING_ASTLPC_MODE_BMC ||
1260 	      mode == MCTP_BINDING_ASTLPC_MODE_HOST)) {
1261 		mctp_prerr("Unknown binding mode: %u", mode);
1262 		return NULL;
1263 	}
1264 
1265 	astlpc = __mctp_astlpc_init(mode, mtu);
1266 	if (!astlpc)
1267 		return NULL;
1268 
1269 	memcpy(&astlpc->ops, ops, sizeof(astlpc->ops));
1270 	astlpc->ops_data = ops_data;
1271 	astlpc->lpc_map = lpc_map;
1272 	astlpc->mode = mode;
1273 
1274 	return astlpc;
1275 }
1276 
1277 struct mctp_binding_astlpc *
1278 mctp_astlpc_init_ops(const struct mctp_binding_astlpc_ops *ops, void *ops_data,
1279 		     void *lpc_map)
1280 {
1281 	return mctp_astlpc_init(MCTP_BINDING_ASTLPC_MODE_BMC, MCTP_BTU, lpc_map,
1282 				ops, ops_data);
1283 }
1284 
1285 void mctp_astlpc_destroy(struct mctp_binding_astlpc *astlpc)
1286 {
1287 	/* Clear channel-active and bmc-ready */
1288 	if (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_BMC)
1289 		mctp_astlpc_kcs_set_status(astlpc, 0);
1290 	__mctp_free(astlpc);
1291 }
1292 
1293 #ifdef MCTP_HAVE_FILEIO
1294 
1295 static int mctp_astlpc_init_fileio_lpc(struct mctp_binding_astlpc *astlpc)
1296 {
1297 	struct aspeed_lpc_ctrl_mapping map = {
1298 		.window_type = ASPEED_LPC_CTRL_WINDOW_MEMORY,
1299 		.window_id = 0, /* There's only one */
1300 		.flags = 0,
1301 		.addr = 0,
1302 		.offset = 0,
1303 		.size = 0
1304 	};
1305 	void *lpc_map_base;
1306 	int fd, rc;
1307 
1308 	fd = open(lpc_path, O_RDWR | O_SYNC);
1309 	if (fd < 0) {
1310 		astlpc_prwarn(astlpc, "LPC open (%s) failed", lpc_path);
1311 		return -1;
1312 	}
1313 
1314 	rc = ioctl(fd, ASPEED_LPC_CTRL_IOCTL_GET_SIZE, &map);
1315 	if (rc) {
1316 		astlpc_prwarn(astlpc, "LPC GET_SIZE failed");
1317 		close(fd);
1318 		return -1;
1319 	}
1320 
1321 	/*
1322 	 * ������
1323 	 *
1324 	 * Decouple ourselves from hiomapd[1] (another user of the FW2AHB) by
1325 	 * mapping the FW2AHB to the reserved memory here as well.
1326 	 *
1327 	 * It's not possible to use the MCTP ASTLPC binding on machines that
1328 	 * need the FW2AHB bridge mapped anywhere except to the reserved memory
1329 	 * (e.g. the host SPI NOR).
1330 	 *
1331 	 * [1] https://github.com/openbmc/hiomapd/
1332 	 *
1333 	 * ������
1334 	 *
1335 	 * The following calculation must align with what's going on in
1336 	 * hiomapd's lpc.c so as not to disrupt its behaviour:
1337 	 *
1338 	 * https://github.com/openbmc/hiomapd/blob/5ff50e3cbd7702aefc185264e4adfb9952040575/lpc.c#L68
1339 	 *
1340 	 * ������
1341 	 */
1342 
1343 	/* Map the reserved memory at the top of the 28-bit LPC firmware address space */
1344 	map.addr = 0x0FFFFFFF & -map.size;
1345 	astlpc_prinfo(astlpc,
1346 		      "Configuring FW2AHB to map reserved memory at 0x%08x for 0x%x in the LPC FW cycle address-space",
1347 		      map.addr, map.size);
1348 
1349 	rc = ioctl(fd, ASPEED_LPC_CTRL_IOCTL_MAP, &map);
1350 	if (rc) {
1351 		astlpc_prwarn(astlpc, "Failed to map FW2AHB to reserved memory");
1352 		close(fd);
1353 		return -1;
1354 	}
1355 
1356 	/* Map the reserved memory into our address space */
1357 	lpc_map_base =
1358 		mmap(NULL, map.size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
1359 	if (lpc_map_base == MAP_FAILED) {
1360 		astlpc_prwarn(astlpc, "LPC mmap failed");
1361 		rc = -1;
1362 	} else {
1363 		astlpc->lpc_map = lpc_map_base + map.size - LPC_WIN_SIZE;
1364 	}
1365 
1366 	close(fd);
1367 
1368 	return rc;
1369 }
1370 
1371 static int mctp_astlpc_init_fileio_kcs(struct mctp_binding_astlpc *astlpc)
1372 {
1373 	astlpc->kcs_fd = open(kcs_path, O_RDWR);
1374 	if (astlpc->kcs_fd < 0)
1375 		return -1;
1376 
1377 	return 0;
1378 }
1379 
1380 static int __mctp_astlpc_fileio_kcs_read(void *arg,
1381 		enum mctp_binding_astlpc_kcs_reg reg, uint8_t *val)
1382 {
1383 	struct mctp_binding_astlpc *astlpc = arg;
1384 	off_t offset = reg;
1385 	int rc;
1386 
1387 	rc = pread(astlpc->kcs_fd, val, 1, offset);
1388 
1389 	return rc == 1 ? 0 : -1;
1390 }
1391 
1392 static int __mctp_astlpc_fileio_kcs_write(void *arg,
1393 		enum mctp_binding_astlpc_kcs_reg reg, uint8_t val)
1394 {
1395 	struct mctp_binding_astlpc *astlpc = arg;
1396 	off_t offset = reg;
1397 	int rc;
1398 
1399 	rc = pwrite(astlpc->kcs_fd, &val, 1, offset);
1400 
1401 	return rc == 1 ? 0 : -1;
1402 }
1403 
1404 int mctp_astlpc_init_pollfd(struct mctp_binding_astlpc *astlpc,
1405 			    struct pollfd *pollfd)
1406 {
1407 	bool release;
1408 
1409 	pollfd->fd = astlpc->kcs_fd;
1410 	pollfd->events = 0;
1411 
1412 	release = astlpc->layout.rx.state == buffer_state_prepared ||
1413 			astlpc->layout.tx.state == buffer_state_prepared;
1414 
1415 	pollfd->events = release ? POLLOUT : POLLIN;
1416 
1417 	return 0;
1418 }
1419 
1420 struct mctp_binding_astlpc *mctp_astlpc_init_fileio(void)
1421 {
1422 	struct mctp_binding_astlpc *astlpc;
1423 	int rc;
1424 
1425 	/*
1426 	 * If we're doing file IO then we're very likely not running
1427 	 * freestanding, so lets assume that we're on the BMC side.
1428 	 *
1429 	 * Requesting an MTU of 0 requests the largest possible MTU, whatever
1430 	 * value that might take.
1431 	 */
1432 	astlpc = __mctp_astlpc_init(MCTP_BINDING_ASTLPC_MODE_BMC, 0);
1433 	if (!astlpc)
1434 		return NULL;
1435 
1436 	/* Set internal operations for kcs. We use direct accesses to the lpc
1437 	 * map area */
1438 	astlpc->ops.kcs_read = __mctp_astlpc_fileio_kcs_read;
1439 	astlpc->ops.kcs_write = __mctp_astlpc_fileio_kcs_write;
1440 	astlpc->ops_data = astlpc;
1441 
1442 	rc = mctp_astlpc_init_fileio_lpc(astlpc);
1443 	if (rc) {
1444 		free(astlpc);
1445 		return NULL;
1446 	}
1447 
1448 	rc = mctp_astlpc_init_fileio_kcs(astlpc);
1449 	if (rc) {
1450 		free(astlpc);
1451 		return NULL;
1452 	}
1453 
1454 	return astlpc;
1455 }
1456 #else
1457 struct mctp_binding_astlpc *mctp_astlpc_init_fileio(void)
1458 {
1459 	mctp_prlog(MCTP_LOG_ERR, "%s: Missing support for file IO", __func__);
1460 	return NULL;
1461 }
1462 
1463 int mctp_astlpc_init_pollfd(struct mctp_binding_astlpc *astlpc __unused,
1464 			    struct pollfd *pollfd __unused)
1465 {
1466 	mctp_prlog(MCTP_LOG_ERR, "%s: Missing support for file IO", __func__);
1467 	return -1;
1468 }
1469 #endif
1470