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