xref: /openbmc/libmctp/astlpc.c (revision 85c04e47)
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 "libmctp.h"
22 #include "libmctp-alloc.h"
23 #include "libmctp-log.h"
24 #include "libmctp-astlpc.h"
25 #include "container_of.h"
26 
27 #ifdef MCTP_HAVE_FILEIO
28 
29 #include <unistd.h>
30 #include <fcntl.h>
31 #include <sys/ioctl.h>
32 #include <sys/mman.h>
33 #include <linux/aspeed-lpc-ctrl.h>
34 
35 /* kernel interface */
36 static const char *kcs_path = "/dev/mctp0";
37 static const char *lpc_path = "/dev/aspeed-lpc-ctrl";
38 
39 #endif
40 
41 struct mctp_astlpc_buffer {
42 	uint32_t offset;
43 	uint32_t size;
44 };
45 
46 struct mctp_astlpc_layout {
47 	struct mctp_astlpc_buffer rx;
48 	struct mctp_astlpc_buffer tx;
49 };
50 
51 struct mctp_binding_astlpc {
52 	struct mctp_binding	binding;
53 
54 	void *lpc_map;
55 	struct mctp_astlpc_layout layout;
56 
57 	uint8_t mode;
58 	uint16_t version;
59 	uint32_t requested_mtu;
60 
61 	/* direct ops data */
62 	struct mctp_binding_astlpc_ops ops;
63 	void *ops_data;
64 
65 	/* fileio ops data */
66 	int kcs_fd;
67 	uint8_t kcs_status;
68 
69 	bool			running;
70 };
71 
72 #define binding_to_astlpc(b) \
73 	container_of(b, struct mctp_binding_astlpc, binding)
74 
75 #define astlpc_prlog(ctx, lvl, fmt, ...)                                       \
76 	do {                                                                   \
77 		bool __bmc = ((ctx)->mode == MCTP_BINDING_ASTLPC_MODE_BMC);    \
78 		mctp_prlog(lvl, pr_fmt("%s: " fmt), __bmc ? "bmc" : "host",    \
79 			   ##__VA_ARGS__);                                     \
80 	} while (0)
81 
82 #define astlpc_prerr(ctx, fmt, ...)                                            \
83 	astlpc_prlog(ctx, MCTP_LOG_ERR, fmt, ##__VA_ARGS__)
84 #define astlpc_prwarn(ctx, fmt, ...)                                           \
85 	astlpc_prlog(ctx, MCTP_LOG_WARNING, fmt, ##__VA_ARGS__)
86 #define astlpc_prinfo(ctx, fmt, ...)                                           \
87 	astlpc_prlog(ctx, MCTP_LOG_INFO, fmt, ##__VA_ARGS__)
88 #define astlpc_prdebug(ctx, fmt, ...)                                          \
89 	astlpc_prlog(ctx, MCTP_LOG_DEBUG, fmt, ##__VA_ARGS__)
90 
91 /* clang-format off */
92 #define ASTLPC_MCTP_MAGIC	0x4d435450
93 #define ASTLPC_VER_BAD	0
94 #define ASTLPC_VER_MIN	1
95 
96 /* Support testing of new binding protocols */
97 #ifndef ASTLPC_VER_CUR
98 #define ASTLPC_VER_CUR	2
99 #endif
100 
101 #define ASTLPC_PACKET_SIZE(sz)	(4 + (sz))
102 #define ASTLPC_BODY_SIZE(sz)	((sz) - 4)
103 /* clang-format on */
104 
105 struct mctp_lpcmap_hdr {
106 	uint32_t magic;
107 
108 	uint16_t bmc_ver_min;
109 	uint16_t bmc_ver_cur;
110 	uint16_t host_ver_min;
111 	uint16_t host_ver_cur;
112 	uint16_t negotiated_ver;
113 	uint16_t pad0;
114 
115 	struct {
116 		uint32_t rx_offset;
117 		uint32_t rx_size;
118 		uint32_t tx_offset;
119 		uint32_t tx_size;
120 	} layout;
121 } __attribute__((packed));
122 
123 static const uint32_t control_size = 0x100;
124 
125 #define LPC_WIN_SIZE                (1 * 1024 * 1024)
126 
127 #define KCS_STATUS_BMC_READY		0x80
128 #define KCS_STATUS_CHANNEL_ACTIVE	0x40
129 #define KCS_STATUS_IBF			0x02
130 #define KCS_STATUS_OBF			0x01
131 
132 #define MIN(a, b)                                                              \
133 	({                                                                     \
134 		typeof(a) _a = a;                                              \
135 		typeof(b) _b = b;                                              \
136 		_a < _b ? _a : _b;                                             \
137 	})
138 
139 #define MAX(a, b)                                                              \
140 	({                                                                     \
141 		typeof(a) _a = a;                                              \
142 		typeof(b) _b = b;                                              \
143 		_a > _b ? _a : _b;                                             \
144 	})
145 
146 static inline int mctp_astlpc_kcs_write(struct mctp_binding_astlpc *astlpc,
147 					enum mctp_binding_astlpc_kcs_reg reg,
148 					uint8_t val)
149 {
150 	return astlpc->ops.kcs_write(astlpc->ops_data, reg, val);
151 }
152 
153 static inline int mctp_astlpc_kcs_read(struct mctp_binding_astlpc *astlpc,
154 				       enum mctp_binding_astlpc_kcs_reg reg,
155 				       uint8_t *val)
156 {
157 	return astlpc->ops.kcs_read(astlpc->ops_data, reg, val);
158 }
159 
160 static inline int mctp_astlpc_lpc_write(struct mctp_binding_astlpc *astlpc,
161 					const void *buf, long offset,
162 					size_t len)
163 {
164 	astlpc_prdebug(astlpc, "%s: %zu bytes to 0x%lx", __func__, len, offset);
165 
166 	assert(offset >= 0);
167 
168 	/* Indirect access */
169 	if (astlpc->ops.lpc_write) {
170 		void *data = astlpc->ops_data;
171 
172 		return astlpc->ops.lpc_write(data, buf, offset, len);
173 	}
174 
175 	/* Direct mapping */
176 	assert(astlpc->lpc_map);
177 	memcpy(&((char *)astlpc->lpc_map)[offset], buf, len);
178 
179 	return 0;
180 }
181 
182 static inline int mctp_astlpc_lpc_read(struct mctp_binding_astlpc *astlpc,
183 				       void *buf, long offset, size_t len)
184 {
185 	astlpc_prdebug(astlpc, "%s: %zu bytes from 0x%lx", __func__, len,
186 		       offset);
187 
188 	assert(offset >= 0);
189 
190 	/* Indirect access */
191 	if (astlpc->ops.lpc_read) {
192 		void *data = astlpc->ops_data;
193 
194 		return astlpc->ops.lpc_read(data, buf, offset, len);
195 	}
196 
197 	/* Direct mapping */
198 	assert(astlpc->lpc_map);
199 	memcpy(buf, &((char *)astlpc->lpc_map)[offset], len);
200 
201 	return 0;
202 }
203 
204 static int mctp_astlpc_kcs_set_status(struct mctp_binding_astlpc *astlpc,
205 				      uint8_t status)
206 {
207 	uint8_t data;
208 	int rc;
209 
210 	/* Since we're setting the status register, we want the other endpoint
211 	 * to be interrupted. However, some hardware may only raise a host-side
212 	 * interrupt on an ODR event.
213 	 * So, write a dummy value of 0xff to ODR, which will ensure that an
214 	 * interrupt is triggered, and can be ignored by the host.
215 	 */
216 	data = 0xff;
217 
218 	rc = mctp_astlpc_kcs_write(astlpc, MCTP_ASTLPC_KCS_REG_STATUS, status);
219 	if (rc) {
220 		astlpc_prwarn(astlpc, "KCS status write failed");
221 		return -1;
222 	}
223 
224 	rc = mctp_astlpc_kcs_write(astlpc, MCTP_ASTLPC_KCS_REG_DATA, data);
225 	if (rc) {
226 		astlpc_prwarn(astlpc, "KCS dummy data write failed");
227 		return -1;
228 	}
229 
230 	return 0;
231 }
232 
233 static int mctp_astlpc_layout_read(struct mctp_binding_astlpc *astlpc,
234 				   struct mctp_astlpc_layout *layout)
235 {
236 	struct mctp_lpcmap_hdr hdr;
237 	int rc;
238 
239 	rc = mctp_astlpc_lpc_read(astlpc, &hdr, 0, sizeof(hdr));
240 	if (rc < 0)
241 		return rc;
242 
243 	/* Flip the buffers as the names are defined in terms of the host */
244 	if (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_BMC) {
245 		layout->rx.offset = be32toh(hdr.layout.tx_offset);
246 		layout->rx.size = be32toh(hdr.layout.tx_size);
247 		layout->tx.offset = be32toh(hdr.layout.rx_offset);
248 		layout->tx.size = be32toh(hdr.layout.rx_size);
249 	} else {
250 		assert(astlpc->mode == MCTP_BINDING_ASTLPC_MODE_HOST);
251 
252 		layout->rx.offset = be32toh(hdr.layout.rx_offset);
253 		layout->rx.size = be32toh(hdr.layout.rx_size);
254 		layout->tx.offset = be32toh(hdr.layout.tx_offset);
255 		layout->tx.size = be32toh(hdr.layout.tx_size);
256 	}
257 
258 	return 0;
259 }
260 
261 static int mctp_astlpc_layout_write(struct mctp_binding_astlpc *astlpc,
262 				    struct mctp_astlpc_layout *layout)
263 {
264 	uint32_t rx_size_be;
265 
266 	if (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_BMC) {
267 		struct mctp_lpcmap_hdr hdr;
268 
269 		/*
270 		 * Flip the buffers as the names are defined in terms of the
271 		 * host
272 		 */
273 		hdr.layout.rx_offset = htobe32(layout->tx.offset);
274 		hdr.layout.rx_size = htobe32(layout->tx.size);
275 		hdr.layout.tx_offset = htobe32(layout->rx.offset);
276 		hdr.layout.tx_size = htobe32(layout->rx.size);
277 
278 		return mctp_astlpc_lpc_write(astlpc, &hdr.layout,
279 				offsetof(struct mctp_lpcmap_hdr, layout),
280 				sizeof(hdr.layout));
281 	}
282 
283 	assert(astlpc->mode == MCTP_BINDING_ASTLPC_MODE_HOST);
284 
285 	/*
286 	 * As of v2 we only need to write rx_size - the offsets are controlled
287 	 * by the BMC, as is the BMC's rx_size (host tx_size).
288 	 */
289 	rx_size_be = htobe32(layout->rx.size);
290 	return mctp_astlpc_lpc_write(astlpc, &rx_size_be,
291 			offsetof(struct mctp_lpcmap_hdr, layout.rx_size),
292 			sizeof(rx_size_be));
293 }
294 
295 static bool mctp_astlpc_buffer_validate(struct mctp_astlpc_buffer *buf,
296 					const char *name)
297 {
298 	/* Check for overflow */
299 	if (buf->offset + buf->size < buf->offset) {
300 		mctp_prerr(
301 			"%s packet buffer parameters overflow: offset: 0x%" PRIx32
302 			", size: %" PRIu32,
303 			name, buf->offset, buf->size);
304 		return false;
305 	}
306 
307 	/* Check that the buffers are contained within the allocated space */
308 	if (buf->offset + buf->size > LPC_WIN_SIZE) {
309 		mctp_prerr(
310 			"%s packet buffer parameters exceed %uM window size: offset: 0x%" PRIx32
311 			", size: %" PRIu32,
312 			name, (LPC_WIN_SIZE / (1024 * 1024)), buf->offset,
313 			buf->size);
314 		return false;
315 	}
316 
317 	/* Check that the baseline transmission unit is supported */
318 	if (buf->size < ASTLPC_PACKET_SIZE(MCTP_PACKET_SIZE(MCTP_BTU))) {
319 		mctp_prerr(
320 			"%s packet buffer too small: Require %zu bytes to support the %u byte baseline transmission unit, found %" PRIu32,
321 			name, ASTLPC_PACKET_SIZE(MCTP_PACKET_SIZE(MCTP_BTU)),
322 			MCTP_BTU, buf->size);
323 		return false;
324 	}
325 
326 	/* Check for overlap with the control space */
327 	if (buf->offset < control_size) {
328 		mctp_prerr(
329 			"%s packet buffer overlaps control region {0x%" PRIx32
330 			", %" PRIu32 "}: Rx {0x%" PRIx32 ", %" PRIu32 "}",
331 			name, 0U, control_size, buf->offset, buf->size);
332 		return false;
333 	}
334 
335 	return true;
336 }
337 
338 static bool mctp_astlpc_layout_validate(struct mctp_astlpc_layout *layout)
339 {
340 	struct mctp_astlpc_buffer *rx = &layout->rx;
341 	struct mctp_astlpc_buffer *tx = &layout->tx;
342 	bool rx_valid, tx_valid;
343 
344 	rx_valid = mctp_astlpc_buffer_validate(rx, "Rx");
345 	tx_valid = mctp_astlpc_buffer_validate(tx, "Tx");
346 
347 	if (!(rx_valid && tx_valid))
348 		return false;
349 
350 	/* Check that the buffers are disjoint */
351 	if ((rx->offset <= tx->offset && rx->offset + rx->size > tx->offset) ||
352 	    (tx->offset <= rx->offset && tx->offset + tx->size > rx->offset)) {
353 		mctp_prerr("Rx and Tx packet buffers overlap: Rx {0x%" PRIx32
354 			   ", %" PRIu32 "}, Tx {0x%" PRIx32 ", %" PRIu32 "}",
355 			   rx->offset, rx->size, tx->offset, tx->size);
356 		return false;
357 	}
358 
359 	return true;
360 }
361 
362 static int mctp_astlpc_init_bmc(struct mctp_binding_astlpc *astlpc)
363 {
364 	struct mctp_lpcmap_hdr hdr = { 0 };
365 	uint8_t status;
366 	size_t sz;
367 
368 	/*
369 	 * The largest buffer size is half of the allocated MCTP space
370 	 * excluding the control space.
371 	 */
372 	sz = ((LPC_WIN_SIZE - control_size) / 2);
373 
374 	/*
375 	 * Trim the MTU to a multiple of 16 to meet the requirements of 12.17
376 	 * Query Hop in DSP0236 v1.3.0.
377 	 */
378 	sz = MCTP_BODY_SIZE(ASTLPC_BODY_SIZE(sz));
379 	sz &= ~0xfUL;
380 	sz = ASTLPC_PACKET_SIZE(MCTP_PACKET_SIZE(sz));
381 
382 	if (astlpc->requested_mtu) {
383 		size_t r;
384 
385 		r = ASTLPC_PACKET_SIZE(MCTP_PACKET_SIZE(astlpc->requested_mtu));
386 		sz = MIN(sz, r);
387 	}
388 
389 	/* Flip the buffers as the names are defined in terms of the host */
390 	astlpc->layout.tx.offset = control_size;
391 	astlpc->layout.tx.size = sz;
392 	astlpc->layout.rx.offset =
393 		astlpc->layout.tx.offset + astlpc->layout.tx.size;
394 	astlpc->layout.rx.size = sz;
395 
396 	if (!mctp_astlpc_layout_validate(&astlpc->layout)) {
397 		astlpc_prerr(astlpc, "Cannot support an MTU of %zu", sz);
398 		return -EINVAL;
399 	}
400 
401 	hdr = (struct mctp_lpcmap_hdr){
402 		.magic = htobe32(ASTLPC_MCTP_MAGIC),
403 		.bmc_ver_min = htobe16(ASTLPC_VER_MIN),
404 		.bmc_ver_cur = htobe16(ASTLPC_VER_CUR),
405 
406 		/* Flip the buffers back as we're now describing the host's
407 		 * configuration to the host */
408 		.layout.rx_offset = htobe32(astlpc->layout.tx.offset),
409 		.layout.rx_size = htobe32(astlpc->layout.tx.size),
410 		.layout.tx_offset = htobe32(astlpc->layout.rx.offset),
411 		.layout.tx_size = htobe32(astlpc->layout.rx.size),
412 	};
413 
414 	mctp_astlpc_lpc_write(astlpc, &hdr, 0, sizeof(hdr));
415 
416 	/*
417 	 * Set status indicating that the BMC is now active. Be explicit about
418 	 * clearing OBF; we're reinitialising the binding and so any previous
419 	 * buffer state is irrelevant.
420 	 */
421 	status = KCS_STATUS_BMC_READY & ~KCS_STATUS_OBF;
422 	return mctp_astlpc_kcs_set_status(astlpc, status);
423 }
424 
425 static int mctp_binding_astlpc_start_bmc(struct mctp_binding *b)
426 {
427 	struct mctp_binding_astlpc *astlpc =
428 		container_of(b, struct mctp_binding_astlpc, binding);
429 
430 	return mctp_astlpc_init_bmc(astlpc);
431 }
432 
433 static bool mctp_astlpc_validate_version(uint16_t bmc_ver_min,
434 					 uint16_t bmc_ver_cur,
435 					 uint16_t host_ver_min,
436 					 uint16_t host_ver_cur)
437 {
438 	if (!(bmc_ver_min && bmc_ver_cur && host_ver_min && host_ver_cur)) {
439 		mctp_prerr("Invalid version present in [%" PRIu16 ", %" PRIu16
440 			   "], [%" PRIu16 ", %" PRIu16 "]",
441 			   bmc_ver_min, bmc_ver_cur, host_ver_min,
442 			   host_ver_cur);
443 		return false;
444 	} else if (bmc_ver_min > bmc_ver_cur) {
445 		mctp_prerr("Invalid bmc version range [%" PRIu16 ", %" PRIu16
446 			   "]",
447 			   bmc_ver_min, bmc_ver_cur);
448 		return false;
449 	} else if (host_ver_min > host_ver_cur) {
450 		mctp_prerr("Invalid host version range [%" PRIu16 ", %" PRIu16
451 			   "]",
452 			   host_ver_min, host_ver_cur);
453 		return false;
454 	} else if ((host_ver_cur < bmc_ver_min) ||
455 		   (host_ver_min > bmc_ver_cur)) {
456 		mctp_prerr(
457 			"Unable to satisfy version negotiation with ranges [%" PRIu16
458 			", %" PRIu16 "] and [%" PRIu16 ", %" PRIu16 "]",
459 			bmc_ver_min, bmc_ver_cur, host_ver_min, host_ver_cur);
460 		return false;
461 	}
462 
463 	return true;
464 }
465 
466 static int mctp_astlpc_negotiate_layout_host(struct mctp_binding_astlpc *astlpc)
467 {
468 	struct mctp_astlpc_layout layout;
469 	uint32_t sz;
470 	int rc;
471 
472 	rc = mctp_astlpc_layout_read(astlpc, &layout);
473 	if (rc < 0)
474 		return rc;
475 
476 	if (!mctp_astlpc_layout_validate(&layout)) {
477 		astlpc_prerr(
478 			astlpc,
479 			"BMC provided invalid buffer layout: Rx {0x%" PRIx32
480 			", %" PRIu32 "}, Tx {0x%" PRIx32 ", %" PRIu32 "}",
481 			layout.rx.offset, layout.rx.size, layout.tx.offset,
482 			layout.tx.size);
483 		return -EINVAL;
484 	}
485 
486 	astlpc_prinfo(astlpc, "Desire an MTU of %" PRIu32 " bytes",
487 		      astlpc->requested_mtu);
488 
489 	sz = ASTLPC_PACKET_SIZE(MCTP_PACKET_SIZE(astlpc->requested_mtu));
490 	layout.rx.size = sz;
491 
492 	if (!mctp_astlpc_layout_validate(&layout)) {
493 		astlpc_prerr(
494 			astlpc,
495 			"Generated invalid buffer layout with size %" PRIu32
496 			": Rx {0x%" PRIx32 ", %" PRIu32 "}, Tx {0x%" PRIx32
497 			", %" PRIu32 "}",
498 			sz, layout.rx.offset, layout.rx.size, layout.tx.offset,
499 			layout.tx.size);
500 		return -EINVAL;
501 	}
502 
503 	astlpc_prinfo(astlpc, "Requesting MTU of %" PRIu32 " bytes",
504 		      astlpc->requested_mtu);
505 
506 	return mctp_astlpc_layout_write(astlpc, &layout);
507 }
508 
509 static int mctp_astlpc_init_host(struct mctp_binding_astlpc *astlpc)
510 {
511 	const uint16_t ver_min_be = htobe16(ASTLPC_VER_MIN);
512 	const uint16_t ver_cur_be = htobe16(ASTLPC_VER_CUR);
513 	uint16_t bmc_ver_min, bmc_ver_cur;
514 	struct mctp_lpcmap_hdr hdr;
515 	uint8_t status;
516 	int rc;
517 
518 	rc = mctp_astlpc_kcs_read(astlpc, MCTP_ASTLPC_KCS_REG_STATUS, &status);
519 	if (rc) {
520 		mctp_prwarn("KCS status read failed");
521 		return rc;
522 	}
523 
524 	astlpc->kcs_status = status;
525 
526 	if (!(status & KCS_STATUS_BMC_READY))
527 		return -EHOSTDOWN;
528 
529 	mctp_astlpc_lpc_read(astlpc, &hdr, 0, sizeof(hdr));
530 
531 	bmc_ver_min = be16toh(hdr.bmc_ver_min);
532 	bmc_ver_cur = be16toh(hdr.bmc_ver_cur);
533 
534 	if (!mctp_astlpc_validate_version(bmc_ver_min, bmc_ver_cur,
535 					  ASTLPC_VER_MIN, ASTLPC_VER_CUR)) {
536 		astlpc_prerr(astlpc, "Cannot negotiate with invalid versions");
537 		return -EINVAL;
538 	}
539 
540 	/*
541 	 * Negotation always chooses the highest protocol version that
542 	 * satisfies the version constraints. So check whether the BMC supports
543 	 * v2, and if so, negotiate in v2 style.
544 	 */
545 	if (ASTLPC_VER_CUR >= 2 && bmc_ver_cur >= 2) {
546 		rc = mctp_astlpc_negotiate_layout_host(astlpc);
547 		if (rc < 0)
548 			return rc;
549 	}
550 
551 	/* Version negotiation */
552 	mctp_astlpc_lpc_write(astlpc, &ver_min_be,
553 			      offsetof(struct mctp_lpcmap_hdr, host_ver_min),
554 			      sizeof(ver_min_be));
555 
556 	mctp_astlpc_lpc_write(astlpc, &ver_cur_be,
557 			      offsetof(struct mctp_lpcmap_hdr, host_ver_cur),
558 			      sizeof(ver_cur_be));
559 
560 	/* Send channel init command */
561 	rc = mctp_astlpc_kcs_write(astlpc, MCTP_ASTLPC_KCS_REG_DATA, 0x0);
562 	if (rc) {
563 		astlpc_prwarn(astlpc, "KCS write failed");
564 	}
565 
566 	return rc;
567 }
568 
569 static int mctp_binding_astlpc_start_host(struct mctp_binding *b)
570 {
571 	struct mctp_binding_astlpc *astlpc =
572 		container_of(b, struct mctp_binding_astlpc, binding);
573 
574 	return mctp_astlpc_init_host(astlpc);
575 }
576 
577 static bool __mctp_astlpc_kcs_ready(struct mctp_binding_astlpc *astlpc,
578 				    uint8_t status, bool is_write)
579 {
580 	bool is_bmc;
581 	bool ready_state;
582 	uint8_t flag;
583 
584 	is_bmc = (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_BMC);
585 	flag = (is_bmc ^ is_write) ? KCS_STATUS_IBF : KCS_STATUS_OBF;
586 	ready_state = is_write ? 0 : 1;
587 
588 	return !!(status & flag) == ready_state;
589 }
590 
591 static inline bool
592 mctp_astlpc_kcs_read_ready(struct mctp_binding_astlpc *astlpc, uint8_t status)
593 {
594 	return __mctp_astlpc_kcs_ready(astlpc, status, false);
595 }
596 
597 static inline bool
598 mctp_astlpc_kcs_write_ready(struct mctp_binding_astlpc *astlpc, uint8_t status)
599 {
600 	return __mctp_astlpc_kcs_ready(astlpc, status, true);
601 }
602 
603 static int mctp_astlpc_kcs_send(struct mctp_binding_astlpc *astlpc,
604 		uint8_t data)
605 {
606 	uint8_t status;
607 	int rc;
608 
609 	for (;;) {
610 		rc = mctp_astlpc_kcs_read(astlpc, MCTP_ASTLPC_KCS_REG_STATUS,
611 					  &status);
612 		if (rc) {
613 			astlpc_prwarn(astlpc, "KCS status read failed");
614 			return -1;
615 		}
616 		if (mctp_astlpc_kcs_write_ready(astlpc, status))
617 			break;
618 		/* todo: timeout */
619 	}
620 
621 	rc = mctp_astlpc_kcs_write(astlpc, MCTP_ASTLPC_KCS_REG_DATA, data);
622 	if (rc) {
623 		astlpc_prwarn(astlpc, "KCS data write failed");
624 		return -1;
625 	}
626 
627 	return 0;
628 }
629 
630 static int mctp_binding_astlpc_tx(struct mctp_binding *b,
631 		struct mctp_pktbuf *pkt)
632 {
633 	struct mctp_binding_astlpc *astlpc = binding_to_astlpc(b);
634 	uint32_t len, len_be;
635 	struct mctp_hdr *hdr;
636 
637 	hdr = mctp_pktbuf_hdr(pkt);
638 	len = mctp_pktbuf_size(pkt);
639 
640 	astlpc_prdebug(astlpc,
641 		       "%s: Transmitting %" PRIu32
642 		       "-byte packet (%hhu, %hhu, 0x%hhx)",
643 		       __func__, len, hdr->src, hdr->dest, hdr->flags_seq_tag);
644 
645 	if (len > ASTLPC_BODY_SIZE(astlpc->layout.tx.size)) {
646 		astlpc_prwarn(astlpc, "invalid TX len 0x%x", len);
647 		return -1;
648 	}
649 
650 	len_be = htobe32(len);
651 	mctp_astlpc_lpc_write(astlpc, &len_be, astlpc->layout.tx.offset,
652 			      sizeof(len_be));
653 	mctp_astlpc_lpc_write(astlpc, hdr, astlpc->layout.tx.offset + 4, len);
654 
655 	mctp_binding_set_tx_enabled(b, false);
656 
657 	mctp_astlpc_kcs_send(astlpc, 0x1);
658 	return 0;
659 }
660 
661 static uint16_t mctp_astlpc_negotiate_version(uint16_t bmc_ver_min,
662 					      uint16_t bmc_ver_cur,
663 					      uint16_t host_ver_min,
664 					      uint16_t host_ver_cur)
665 {
666 	if (!mctp_astlpc_validate_version(bmc_ver_min, bmc_ver_cur,
667 					  host_ver_min, host_ver_cur))
668 		return ASTLPC_VER_BAD;
669 
670 	if (bmc_ver_cur < host_ver_cur)
671 		return bmc_ver_cur;
672 
673 	return host_ver_cur;
674 }
675 
676 static uint32_t mctp_astlpc_calculate_mtu(struct mctp_binding_astlpc *astlpc,
677 					  struct mctp_astlpc_layout *layout)
678 {
679 	uint32_t low, high, limit;
680 
681 	/* Derive the largest MTU the BMC _can_ support */
682 	low = MIN(astlpc->layout.rx.offset, astlpc->layout.tx.offset);
683 	high = MAX(astlpc->layout.rx.offset, astlpc->layout.tx.offset);
684 	limit = high - low;
685 
686 	/* Determine the largest MTU the BMC _wants_ to support */
687 	if (astlpc->requested_mtu) {
688 		uint32_t req = astlpc->requested_mtu;
689 
690 		limit = MIN(limit, ASTLPC_PACKET_SIZE(MCTP_PACKET_SIZE(req)));
691 	}
692 
693 	/* Determine the accepted MTU, applied both directions by convention */
694 	return MCTP_BODY_SIZE(ASTLPC_BODY_SIZE(MIN(limit, layout->tx.size)));
695 }
696 
697 static int mctp_astlpc_negotiate_layout_bmc(struct mctp_binding_astlpc *astlpc)
698 {
699 	struct mctp_astlpc_layout proposed, pending;
700 	uint32_t sz, mtu;
701 	int rc;
702 
703 	/* Extract the host's proposed layout */
704 	rc = mctp_astlpc_layout_read(astlpc, &proposed);
705 	if (rc < 0)
706 		return rc;
707 
708 	if (!mctp_astlpc_layout_validate(&proposed))
709 		return -EINVAL;
710 
711 	/* Negotiate the MTU */
712 	mtu = mctp_astlpc_calculate_mtu(astlpc, &proposed);
713 	sz = ASTLPC_PACKET_SIZE(MCTP_PACKET_SIZE(mtu));
714 
715 	/*
716 	 * Use symmetric MTUs by convention and to pass constraints in rx/tx
717 	 * functions
718 	 */
719 	pending = astlpc->layout;
720 	pending.tx.size = sz;
721 	pending.rx.size = sz;
722 
723 	if (mctp_astlpc_layout_validate(&pending)) {
724 		/* We found a sensible Rx MTU, so honour it */
725 		astlpc->layout = pending;
726 
727 		/* Enforce the negotiated MTU */
728 		rc = mctp_astlpc_layout_write(astlpc, &astlpc->layout);
729 		if (rc < 0)
730 			return rc;
731 
732 		astlpc_prinfo(astlpc, "Negotiated an MTU of %" PRIu32 " bytes",
733 			      mtu);
734 	} else {
735 		astlpc_prwarn(astlpc, "MTU negotiation failed");
736 		return -EINVAL;
737 	}
738 
739 	if (astlpc->version >= 2)
740 		astlpc->binding.pkt_size = MCTP_PACKET_SIZE(mtu);
741 
742 	return 0;
743 }
744 
745 static void mctp_astlpc_init_channel(struct mctp_binding_astlpc *astlpc)
746 {
747 	uint16_t negotiated, negotiated_be;
748 	struct mctp_lpcmap_hdr hdr;
749 	uint8_t status;
750 	int rc;
751 
752 	mctp_astlpc_lpc_read(astlpc, &hdr, 0, sizeof(hdr));
753 
754 	/* Version negotiation */
755 	negotiated =
756 		mctp_astlpc_negotiate_version(ASTLPC_VER_MIN, ASTLPC_VER_CUR,
757 					      be16toh(hdr.host_ver_min),
758 					      be16toh(hdr.host_ver_cur));
759 
760 	/* Host Rx MTU negotiation: Failure terminates channel init */
761 	rc = mctp_astlpc_negotiate_layout_bmc(astlpc);
762 	if (rc < 0)
763 		negotiated = ASTLPC_VER_BAD;
764 
765 	/* Populate the negotiated version */
766 	astlpc->version = negotiated;
767 	negotiated_be = htobe16(negotiated);
768 	mctp_astlpc_lpc_write(astlpc, &negotiated_be,
769 			      offsetof(struct mctp_lpcmap_hdr, negotiated_ver),
770 			      sizeof(negotiated_be));
771 
772 	/* Finalise the configuration */
773 	status = KCS_STATUS_BMC_READY | KCS_STATUS_OBF;
774 	if (negotiated > 0) {
775 		astlpc_prinfo(astlpc, "Negotiated binding version %" PRIu16,
776 			      negotiated);
777 		status |= KCS_STATUS_CHANNEL_ACTIVE;
778 	} else {
779 		astlpc_prerr(astlpc, "Failed to initialise channel\n");
780 	}
781 
782 	mctp_astlpc_kcs_set_status(astlpc, status);
783 
784 	mctp_binding_set_tx_enabled(&astlpc->binding,
785 				    status & KCS_STATUS_CHANNEL_ACTIVE);
786 }
787 
788 static void mctp_astlpc_rx_start(struct mctp_binding_astlpc *astlpc)
789 {
790 	struct mctp_pktbuf *pkt;
791 	uint32_t len;
792 
793 	mctp_astlpc_lpc_read(astlpc, &len, astlpc->layout.rx.offset,
794 			     sizeof(len));
795 	len = be32toh(len);
796 
797 	if (len > ASTLPC_BODY_SIZE(astlpc->layout.rx.size)) {
798 		astlpc_prwarn(astlpc, "invalid RX len 0x%x", len);
799 		return;
800 	}
801 
802 	assert(astlpc->binding.pkt_size >= 0);
803 	if (len > (uint32_t)astlpc->binding.pkt_size) {
804 		mctp_prwarn("invalid RX len 0x%x", len);
805 		astlpc_prwarn(astlpc, "invalid RX len 0x%x", len);
806 		return;
807 	}
808 
809 	pkt = mctp_pktbuf_alloc(&astlpc->binding, len);
810 	if (!pkt)
811 		goto out_complete;
812 
813 	mctp_astlpc_lpc_read(astlpc, mctp_pktbuf_hdr(pkt),
814 			     astlpc->layout.rx.offset + 4, len);
815 
816 	mctp_bus_rx(&astlpc->binding, pkt);
817 
818 out_complete:
819 	mctp_astlpc_kcs_send(astlpc, 0x2);
820 }
821 
822 static void mctp_astlpc_tx_complete(struct mctp_binding_astlpc *astlpc)
823 {
824 	mctp_binding_set_tx_enabled(&astlpc->binding, true);
825 }
826 
827 static int mctp_astlpc_finalise_channel(struct mctp_binding_astlpc *astlpc)
828 {
829 	struct mctp_astlpc_layout layout;
830 	uint16_t negotiated;
831 	int rc;
832 
833 	rc = mctp_astlpc_lpc_read(astlpc, &negotiated,
834 				  offsetof(struct mctp_lpcmap_hdr,
835 					   negotiated_ver),
836 				  sizeof(negotiated));
837 	if (rc < 0)
838 		return rc;
839 
840 	negotiated = be16toh(negotiated);
841 
842 	if (negotiated == ASTLPC_VER_BAD || negotiated < ASTLPC_VER_MIN ||
843 	    negotiated > ASTLPC_VER_CUR) {
844 		astlpc_prerr(astlpc, "Failed to negotiate version, got: %u\n",
845 			     negotiated);
846 		return -EINVAL;
847 	}
848 
849 	astlpc->version = negotiated;
850 
851 	rc = mctp_astlpc_layout_read(astlpc, &layout);
852 	if (rc < 0)
853 		return rc;
854 
855 	if (!mctp_astlpc_layout_validate(&layout)) {
856 		mctp_prerr("BMC proposed invalid buffer parameters");
857 		return -EINVAL;
858 	}
859 
860 	astlpc->layout = layout;
861 
862 	if (negotiated >= 2)
863 		astlpc->binding.pkt_size =
864 			ASTLPC_BODY_SIZE(astlpc->layout.tx.size);
865 
866 	return 0;
867 }
868 
869 static int mctp_astlpc_update_channel(struct mctp_binding_astlpc *astlpc,
870 				      uint8_t status)
871 {
872 	uint8_t updated;
873 	int rc = 0;
874 
875 	assert(astlpc->mode == MCTP_BINDING_ASTLPC_MODE_HOST);
876 
877 	updated = astlpc->kcs_status ^ status;
878 
879 	astlpc_prdebug(astlpc, "%s: status: 0x%x, update: 0x%x", __func__,
880 		       status, updated);
881 
882 	if (updated & KCS_STATUS_BMC_READY) {
883 		if (status & KCS_STATUS_BMC_READY) {
884 			astlpc->kcs_status = status;
885 			return astlpc->binding.start(&astlpc->binding);
886 		} else {
887 			mctp_binding_set_tx_enabled(&astlpc->binding, false);
888 		}
889 	}
890 
891 	if (astlpc->version == 0 || updated & KCS_STATUS_CHANNEL_ACTIVE) {
892 		bool enable;
893 
894 		rc = mctp_astlpc_finalise_channel(astlpc);
895 		enable = (status & KCS_STATUS_CHANNEL_ACTIVE) && rc == 0;
896 
897 		mctp_binding_set_tx_enabled(&astlpc->binding, enable);
898 	}
899 
900 	astlpc->kcs_status = status;
901 
902 	return rc;
903 }
904 
905 int mctp_astlpc_poll(struct mctp_binding_astlpc *astlpc)
906 {
907 	uint8_t status, data;
908 	int rc;
909 
910 	rc = mctp_astlpc_kcs_read(astlpc, MCTP_ASTLPC_KCS_REG_STATUS, &status);
911 	if (rc) {
912 		astlpc_prwarn(astlpc, "KCS read error");
913 		return -1;
914 	}
915 
916 	astlpc_prdebug(astlpc, "%s: status: 0x%hhx", __func__, status);
917 
918 	if (!mctp_astlpc_kcs_read_ready(astlpc, status))
919 		return 0;
920 
921 	rc = mctp_astlpc_kcs_read(astlpc, MCTP_ASTLPC_KCS_REG_DATA, &data);
922 	if (rc) {
923 		astlpc_prwarn(astlpc, "KCS data read error");
924 		return -1;
925 	}
926 
927 	astlpc_prdebug(astlpc, "%s: data: 0x%hhx", __func__, data);
928 
929 	switch (data) {
930 	case 0x0:
931 		mctp_astlpc_init_channel(astlpc);
932 		break;
933 	case 0x1:
934 		mctp_astlpc_rx_start(astlpc);
935 		break;
936 	case 0x2:
937 		mctp_astlpc_tx_complete(astlpc);
938 		break;
939 	case 0xff:
940 		/* No responsibilities for the BMC on 0xff */
941 		if (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_HOST) {
942 			rc = mctp_astlpc_update_channel(astlpc, status);
943 			if (rc < 0)
944 				return rc;
945 		}
946 		break;
947 	default:
948 		astlpc_prwarn(astlpc, "unknown message 0x%x", data);
949 	}
950 
951 	/* Handle silent loss of bmc-ready */
952 	if (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_HOST) {
953 		if (!(status & KCS_STATUS_BMC_READY && data == 0xff))
954 			return mctp_astlpc_update_channel(astlpc, status);
955 	}
956 
957 	return rc;
958 }
959 
960 /* allocate and basic initialisation */
961 static struct mctp_binding_astlpc *__mctp_astlpc_init(uint8_t mode,
962 						      uint32_t mtu)
963 {
964 	struct mctp_binding_astlpc *astlpc;
965 
966 	assert((mode == MCTP_BINDING_ASTLPC_MODE_BMC) ||
967 	       (mode == MCTP_BINDING_ASTLPC_MODE_HOST));
968 
969 	astlpc = __mctp_alloc(sizeof(*astlpc));
970 	if (!astlpc)
971 		return NULL;
972 
973 	memset(astlpc, 0, sizeof(*astlpc));
974 	astlpc->mode = mode;
975 	astlpc->lpc_map = NULL;
976 	astlpc->requested_mtu = mtu;
977 	astlpc->binding.name = "astlpc";
978 	astlpc->binding.version = 1;
979 	astlpc->binding.pkt_size = MCTP_PACKET_SIZE(mtu);
980 	astlpc->binding.pkt_pad = 0;
981 	astlpc->binding.tx = mctp_binding_astlpc_tx;
982 	if (mode == MCTP_BINDING_ASTLPC_MODE_BMC)
983 		astlpc->binding.start = mctp_binding_astlpc_start_bmc;
984 	else if (mode == MCTP_BINDING_ASTLPC_MODE_HOST)
985 		astlpc->binding.start = mctp_binding_astlpc_start_host;
986 	else {
987 		astlpc_prerr(astlpc, "%s: Invalid mode: %d\n", __func__, mode);
988 		__mctp_free(astlpc);
989 		return NULL;
990 	}
991 
992 	return astlpc;
993 }
994 
995 struct mctp_binding *mctp_binding_astlpc_core(struct mctp_binding_astlpc *b)
996 {
997 	return &b->binding;
998 }
999 
1000 struct mctp_binding_astlpc *
1001 mctp_astlpc_init(uint8_t mode, uint32_t mtu, void *lpc_map,
1002 		 const struct mctp_binding_astlpc_ops *ops, void *ops_data)
1003 {
1004 	struct mctp_binding_astlpc *astlpc;
1005 
1006 	if (!(mode == MCTP_BINDING_ASTLPC_MODE_BMC ||
1007 	      mode == MCTP_BINDING_ASTLPC_MODE_HOST)) {
1008 		mctp_prerr("Unknown binding mode: %u", mode);
1009 		return NULL;
1010 	}
1011 
1012 	astlpc = __mctp_astlpc_init(mode, mtu);
1013 	if (!astlpc)
1014 		return NULL;
1015 
1016 	memcpy(&astlpc->ops, ops, sizeof(astlpc->ops));
1017 	astlpc->ops_data = ops_data;
1018 	astlpc->lpc_map = lpc_map;
1019 	astlpc->mode = mode;
1020 
1021 	return astlpc;
1022 }
1023 
1024 struct mctp_binding_astlpc *
1025 mctp_astlpc_init_ops(const struct mctp_binding_astlpc_ops *ops, void *ops_data,
1026 		     void *lpc_map)
1027 {
1028 	return mctp_astlpc_init(MCTP_BINDING_ASTLPC_MODE_BMC, MCTP_BTU, lpc_map,
1029 				ops, ops_data);
1030 }
1031 
1032 void mctp_astlpc_destroy(struct mctp_binding_astlpc *astlpc)
1033 {
1034 	/* Clear channel-active and bmc-ready */
1035 	if (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_BMC)
1036 		mctp_astlpc_kcs_set_status(astlpc, KCS_STATUS_OBF);
1037 	__mctp_free(astlpc);
1038 }
1039 
1040 #ifdef MCTP_HAVE_FILEIO
1041 
1042 static int mctp_astlpc_init_fileio_lpc(struct mctp_binding_astlpc *astlpc)
1043 {
1044 	struct aspeed_lpc_ctrl_mapping map = {
1045 		.window_type = ASPEED_LPC_CTRL_WINDOW_MEMORY,
1046 		.window_id = 0, /* There's only one */
1047 		.flags = 0,
1048 		.addr = 0,
1049 		.offset = 0,
1050 		.size = 0
1051 	};
1052 	void *lpc_map_base;
1053 	int fd, rc;
1054 
1055 	fd = open(lpc_path, O_RDWR | O_SYNC);
1056 	if (fd < 0) {
1057 		astlpc_prwarn(astlpc, "LPC open (%s) failed", lpc_path);
1058 		return -1;
1059 	}
1060 
1061 	rc = ioctl(fd, ASPEED_LPC_CTRL_IOCTL_GET_SIZE, &map);
1062 	if (rc) {
1063 		astlpc_prwarn(astlpc, "LPC GET_SIZE failed");
1064 		close(fd);
1065 		return -1;
1066 	}
1067 
1068 	lpc_map_base =
1069 		mmap(NULL, map.size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
1070 	if (lpc_map_base == MAP_FAILED) {
1071 		astlpc_prwarn(astlpc, "LPC mmap failed");
1072 		rc = -1;
1073 	} else {
1074 		astlpc->lpc_map = lpc_map_base + map.size - LPC_WIN_SIZE;
1075 	}
1076 
1077 	close(fd);
1078 
1079 	return rc;
1080 }
1081 
1082 static int mctp_astlpc_init_fileio_kcs(struct mctp_binding_astlpc *astlpc)
1083 {
1084 	astlpc->kcs_fd = open(kcs_path, O_RDWR);
1085 	if (astlpc->kcs_fd < 0)
1086 		return -1;
1087 
1088 	return 0;
1089 }
1090 
1091 static int __mctp_astlpc_fileio_kcs_read(void *arg,
1092 		enum mctp_binding_astlpc_kcs_reg reg, uint8_t *val)
1093 {
1094 	struct mctp_binding_astlpc *astlpc = arg;
1095 	off_t offset = reg;
1096 	int rc;
1097 
1098 	rc = pread(astlpc->kcs_fd, val, 1, offset);
1099 
1100 	return rc == 1 ? 0 : -1;
1101 }
1102 
1103 static int __mctp_astlpc_fileio_kcs_write(void *arg,
1104 		enum mctp_binding_astlpc_kcs_reg reg, uint8_t val)
1105 {
1106 	struct mctp_binding_astlpc *astlpc = arg;
1107 	off_t offset = reg;
1108 	int rc;
1109 
1110 	rc = pwrite(astlpc->kcs_fd, &val, 1, offset);
1111 
1112 	return rc == 1 ? 0 : -1;
1113 }
1114 
1115 int mctp_astlpc_get_fd(struct mctp_binding_astlpc *astlpc)
1116 {
1117 	return astlpc->kcs_fd;
1118 }
1119 
1120 struct mctp_binding_astlpc *mctp_astlpc_init_fileio(void)
1121 {
1122 	struct mctp_binding_astlpc *astlpc;
1123 	int rc;
1124 
1125 	/*
1126 	 * If we're doing file IO then we're very likely not running
1127 	 * freestanding, so lets assume that we're on the BMC side.
1128 	 *
1129 	 * Requesting an MTU of 0 requests the largest possible MTU, whatever
1130 	 * value that might take.
1131 	 */
1132 	astlpc = __mctp_astlpc_init(MCTP_BINDING_ASTLPC_MODE_BMC, 0);
1133 	if (!astlpc)
1134 		return NULL;
1135 
1136 	/* Set internal operations for kcs. We use direct accesses to the lpc
1137 	 * map area */
1138 	astlpc->ops.kcs_read = __mctp_astlpc_fileio_kcs_read;
1139 	astlpc->ops.kcs_write = __mctp_astlpc_fileio_kcs_write;
1140 	astlpc->ops_data = astlpc;
1141 
1142 	rc = mctp_astlpc_init_fileio_lpc(astlpc);
1143 	if (rc) {
1144 		free(astlpc);
1145 		return NULL;
1146 	}
1147 
1148 	rc = mctp_astlpc_init_fileio_kcs(astlpc);
1149 	if (rc) {
1150 		free(astlpc);
1151 		return NULL;
1152 	}
1153 
1154 	return astlpc;
1155 }
1156 #else
1157 struct mctp_binding_astlpc * __attribute__((const))
1158 	mctp_astlpc_init_fileio(void)
1159 {
1160 	astlpc_prerr(astlpc, "Missing support for file IO");
1161 	return NULL;
1162 }
1163 
1164 int __attribute__((const)) mctp_astlpc_get_fd(
1165 		struct mctp_binding_astlpc *astlpc __attribute__((unused)))
1166 {
1167 	astlpc_prerr(astlpc, "Missing support for file IO");
1168 	return -1;
1169 }
1170 #endif
1171