xref: /openbmc/libmctp/astlpc.c (revision 4622cadfb7b6b6f867518c9bd163c041dd8fb79c)
13d36ee2eSJeremy Kerr /* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */
2672c8852SJeremy Kerr 
33286f176SAndrew Jeffery #if HAVE_CONFIG_H
43286f176SAndrew Jeffery #include "config.h"
53286f176SAndrew Jeffery #endif
63286f176SAndrew Jeffery 
73286f176SAndrew Jeffery #if HAVE_ENDIAN_H
892a10a6bSJeremy Kerr #include <endian.h>
93286f176SAndrew Jeffery #endif
103286f176SAndrew Jeffery 
113286f176SAndrew Jeffery #include <assert.h>
1259c6a5c9SAndrew Jeffery #include <err.h>
137cd72f14SAndrew Jeffery #include <errno.h>
14edfe383fSAndrew Jeffery #include <inttypes.h>
15672c8852SJeremy Kerr #include <stdbool.h>
16672c8852SJeremy Kerr #include <stdlib.h>
17672c8852SJeremy Kerr #include <string.h>
18672c8852SJeremy Kerr 
19672c8852SJeremy Kerr #define pr_fmt(x) "astlpc: " x
20672c8852SJeremy Kerr 
21672c8852SJeremy Kerr #include "libmctp.h"
22672c8852SJeremy Kerr #include "libmctp-alloc.h"
23672c8852SJeremy Kerr #include "libmctp-log.h"
24672c8852SJeremy Kerr #include "libmctp-astlpc.h"
25ff25d7ebSPrzemyslaw Czarnowski #include "container_of.h"
26*4622cadfSAndrew Jeffery #include "range.h"
27672c8852SJeremy Kerr 
28b214c643SJeremy Kerr #ifdef MCTP_HAVE_FILEIO
2992a10a6bSJeremy Kerr 
30c6f676d1SJeremy Kerr #include <unistd.h>
3192a10a6bSJeremy Kerr #include <fcntl.h>
3292a10a6bSJeremy Kerr #include <sys/ioctl.h>
3392a10a6bSJeremy Kerr #include <sys/mman.h>
3492a10a6bSJeremy Kerr #include <linux/aspeed-lpc-ctrl.h>
3592a10a6bSJeremy Kerr 
3692a10a6bSJeremy Kerr /* kernel interface */
3792a10a6bSJeremy Kerr static const char *kcs_path = "/dev/mctp0";
3892a10a6bSJeremy Kerr static const char *lpc_path = "/dev/aspeed-lpc-ctrl";
3992a10a6bSJeremy Kerr 
4092a10a6bSJeremy Kerr #endif
4192a10a6bSJeremy Kerr 
427cd72f14SAndrew Jeffery struct mctp_astlpc_buffer {
437cd72f14SAndrew Jeffery 	uint32_t offset;
447cd72f14SAndrew Jeffery 	uint32_t size;
457cd72f14SAndrew Jeffery };
467cd72f14SAndrew Jeffery 
477cd72f14SAndrew Jeffery struct mctp_astlpc_layout {
487cd72f14SAndrew Jeffery 	struct mctp_astlpc_buffer rx;
497cd72f14SAndrew Jeffery 	struct mctp_astlpc_buffer tx;
507cd72f14SAndrew Jeffery };
517cd72f14SAndrew Jeffery 
52672c8852SJeremy Kerr struct mctp_binding_astlpc {
53672c8852SJeremy Kerr 	struct mctp_binding	binding;
54bc53d35aSJeremy Kerr 
55672c8852SJeremy Kerr 	void *lpc_map;
567cd72f14SAndrew Jeffery 	struct mctp_astlpc_layout layout;
577cd72f14SAndrew Jeffery 
587cd72f14SAndrew Jeffery 	uint8_t mode;
594e8264b7SAndrew Jeffery 	uint16_t version;
60a9368980SAndrew Jeffery 	uint32_t requested_mtu;
61bc53d35aSJeremy Kerr 
62bc53d35aSJeremy Kerr 	/* direct ops data */
63bc53d35aSJeremy Kerr 	struct mctp_binding_astlpc_ops ops;
64bc53d35aSJeremy Kerr 	void *ops_data;
65bc53d35aSJeremy Kerr 
66bc53d35aSJeremy Kerr 	/* fileio ops data */
67672c8852SJeremy Kerr 	int kcs_fd;
68672c8852SJeremy Kerr 	uint8_t kcs_status;
69672c8852SJeremy Kerr 
70672c8852SJeremy Kerr 	bool			running;
71672c8852SJeremy Kerr };
72672c8852SJeremy Kerr 
73672c8852SJeremy Kerr #define binding_to_astlpc(b) \
74672c8852SJeremy Kerr 	container_of(b, struct mctp_binding_astlpc, binding)
75672c8852SJeremy Kerr 
769101a2abSAndrew Jeffery #define astlpc_prlog(ctx, lvl, fmt, ...)                                       \
779101a2abSAndrew Jeffery 	do {                                                                   \
789101a2abSAndrew Jeffery 		bool __bmc = ((ctx)->mode == MCTP_BINDING_ASTLPC_MODE_BMC);    \
799101a2abSAndrew Jeffery 		mctp_prlog(lvl, pr_fmt("%s: " fmt), __bmc ? "bmc" : "host",    \
809101a2abSAndrew Jeffery 			   ##__VA_ARGS__);                                     \
819101a2abSAndrew Jeffery 	} while (0)
829101a2abSAndrew Jeffery 
839101a2abSAndrew Jeffery #define astlpc_prerr(ctx, fmt, ...)                                            \
849101a2abSAndrew Jeffery 	astlpc_prlog(ctx, MCTP_LOG_ERR, fmt, ##__VA_ARGS__)
859101a2abSAndrew Jeffery #define astlpc_prwarn(ctx, fmt, ...)                                           \
869101a2abSAndrew Jeffery 	astlpc_prlog(ctx, MCTP_LOG_WARNING, fmt, ##__VA_ARGS__)
879101a2abSAndrew Jeffery #define astlpc_prinfo(ctx, fmt, ...)                                           \
889101a2abSAndrew Jeffery 	astlpc_prlog(ctx, MCTP_LOG_INFO, fmt, ##__VA_ARGS__)
899101a2abSAndrew Jeffery #define astlpc_prdebug(ctx, fmt, ...)                                          \
909101a2abSAndrew Jeffery 	astlpc_prlog(ctx, MCTP_LOG_DEBUG, fmt, ##__VA_ARGS__)
919101a2abSAndrew Jeffery 
927cd72f14SAndrew Jeffery /* clang-format off */
937cd72f14SAndrew Jeffery #define ASTLPC_MCTP_MAGIC	0x4d435450
944e8264b7SAndrew Jeffery #define ASTLPC_VER_BAD	0
957cd72f14SAndrew Jeffery #define ASTLPC_VER_MIN	1
967cd72f14SAndrew Jeffery 
973a540664SAndrew Jeffery /* Support testing of new binding protocols */
983a540664SAndrew Jeffery #ifndef ASTLPC_VER_CUR
995303d9c5SAndrew Jeffery #define ASTLPC_VER_CUR	2
1003a540664SAndrew Jeffery #endif
1013a540664SAndrew Jeffery 
1023a540664SAndrew Jeffery #define ASTLPC_PACKET_SIZE(sz)	(4 + (sz))
1037cd72f14SAndrew Jeffery #define ASTLPC_BODY_SIZE(sz)	((sz) - 4)
1047cd72f14SAndrew Jeffery /* clang-format on */
105672c8852SJeremy Kerr 
106672c8852SJeremy Kerr struct mctp_lpcmap_hdr {
107672c8852SJeremy Kerr 	uint32_t magic;
108672c8852SJeremy Kerr 
109672c8852SJeremy Kerr 	uint16_t bmc_ver_min;
110672c8852SJeremy Kerr 	uint16_t bmc_ver_cur;
111672c8852SJeremy Kerr 	uint16_t host_ver_min;
112672c8852SJeremy Kerr 	uint16_t host_ver_cur;
113672c8852SJeremy Kerr 	uint16_t negotiated_ver;
114672c8852SJeremy Kerr 	uint16_t pad0;
115672c8852SJeremy Kerr 
1163a540664SAndrew Jeffery 	struct {
117672c8852SJeremy Kerr 		uint32_t rx_offset;
118672c8852SJeremy Kerr 		uint32_t rx_size;
119672c8852SJeremy Kerr 		uint32_t tx_offset;
120672c8852SJeremy Kerr 		uint32_t tx_size;
1213a540664SAndrew Jeffery 	} layout;
122672c8852SJeremy Kerr } __attribute__((packed));
123672c8852SJeremy Kerr 
1243a540664SAndrew Jeffery static const uint32_t control_size = 0x100;
125672c8852SJeremy Kerr 
126672c8852SJeremy Kerr #define LPC_WIN_SIZE                (1 * 1024 * 1024)
127672c8852SJeremy Kerr 
128672c8852SJeremy Kerr #define KCS_STATUS_BMC_READY		0x80
129672c8852SJeremy Kerr #define KCS_STATUS_CHANNEL_ACTIVE	0x40
130672c8852SJeremy Kerr #define KCS_STATUS_IBF			0x02
131672c8852SJeremy Kerr #define KCS_STATUS_OBF			0x01
132672c8852SJeremy Kerr 
133f13cb971SAndrew Jeffery static inline int mctp_astlpc_kcs_write(struct mctp_binding_astlpc *astlpc,
134f13cb971SAndrew Jeffery 					enum mctp_binding_astlpc_kcs_reg reg,
135f13cb971SAndrew Jeffery 					uint8_t val)
136f13cb971SAndrew Jeffery {
137f13cb971SAndrew Jeffery 	return astlpc->ops.kcs_write(astlpc->ops_data, reg, val);
138f13cb971SAndrew Jeffery }
139f13cb971SAndrew Jeffery 
140f13cb971SAndrew Jeffery static inline int mctp_astlpc_kcs_read(struct mctp_binding_astlpc *astlpc,
141f13cb971SAndrew Jeffery 				       enum mctp_binding_astlpc_kcs_reg reg,
142f13cb971SAndrew Jeffery 				       uint8_t *val)
143f13cb971SAndrew Jeffery {
144f13cb971SAndrew Jeffery 	return astlpc->ops.kcs_read(astlpc->ops_data, reg, val);
145f13cb971SAndrew Jeffery }
146f13cb971SAndrew Jeffery 
14755fb90beSAndrew Jeffery static inline int mctp_astlpc_lpc_write(struct mctp_binding_astlpc *astlpc,
14855fb90beSAndrew Jeffery 					const void *buf, long offset,
14955fb90beSAndrew Jeffery 					size_t len)
150bc53d35aSJeremy Kerr {
15155fb90beSAndrew Jeffery 	astlpc_prdebug(astlpc, "%s: %zu bytes to 0x%lx", __func__, len, offset);
15255fb90beSAndrew Jeffery 
15355fb90beSAndrew Jeffery 	assert(offset >= 0);
15455fb90beSAndrew Jeffery 
15555fb90beSAndrew Jeffery 	/* Indirect access */
15655fb90beSAndrew Jeffery 	if (astlpc->ops.lpc_write) {
15755fb90beSAndrew Jeffery 		void *data = astlpc->ops_data;
15855fb90beSAndrew Jeffery 
15955fb90beSAndrew Jeffery 		return astlpc->ops.lpc_write(data, buf, offset, len);
16055fb90beSAndrew Jeffery 	}
16155fb90beSAndrew Jeffery 
16255fb90beSAndrew Jeffery 	/* Direct mapping */
16355fb90beSAndrew Jeffery 	assert(astlpc->lpc_map);
16455fb90beSAndrew Jeffery 	memcpy(&((char *)astlpc->lpc_map)[offset], buf, len);
16555fb90beSAndrew Jeffery 
16655fb90beSAndrew Jeffery 	return 0;
16755fb90beSAndrew Jeffery }
16855fb90beSAndrew Jeffery 
16955fb90beSAndrew Jeffery static inline int mctp_astlpc_lpc_read(struct mctp_binding_astlpc *astlpc,
17055fb90beSAndrew Jeffery 				       void *buf, long offset, size_t len)
17155fb90beSAndrew Jeffery {
17255fb90beSAndrew Jeffery 	astlpc_prdebug(astlpc, "%s: %zu bytes from 0x%lx", __func__, len,
17355fb90beSAndrew Jeffery 		       offset);
17455fb90beSAndrew Jeffery 
17555fb90beSAndrew Jeffery 	assert(offset >= 0);
17655fb90beSAndrew Jeffery 
17755fb90beSAndrew Jeffery 	/* Indirect access */
17855fb90beSAndrew Jeffery 	if (astlpc->ops.lpc_read) {
17955fb90beSAndrew Jeffery 		void *data = astlpc->ops_data;
18055fb90beSAndrew Jeffery 
18155fb90beSAndrew Jeffery 		return astlpc->ops.lpc_read(data, buf, offset, len);
18255fb90beSAndrew Jeffery 	}
18355fb90beSAndrew Jeffery 
18455fb90beSAndrew Jeffery 	/* Direct mapping */
18555fb90beSAndrew Jeffery 	assert(astlpc->lpc_map);
18655fb90beSAndrew Jeffery 	memcpy(buf, &((char *)astlpc->lpc_map)[offset], len);
18755fb90beSAndrew Jeffery 
18855fb90beSAndrew Jeffery 	return 0;
189bc53d35aSJeremy Kerr }
190bc53d35aSJeremy Kerr 
191d0f5da0dSAndrew Jeffery static int mctp_astlpc_kcs_set_status(struct mctp_binding_astlpc *astlpc,
192d0f5da0dSAndrew Jeffery 				      uint8_t status)
193d0f5da0dSAndrew Jeffery {
194d0f5da0dSAndrew Jeffery 	uint8_t data;
195d0f5da0dSAndrew Jeffery 	int rc;
196d0f5da0dSAndrew Jeffery 
197d0f5da0dSAndrew Jeffery 	/* Since we're setting the status register, we want the other endpoint
198d0f5da0dSAndrew Jeffery 	 * to be interrupted. However, some hardware may only raise a host-side
199d0f5da0dSAndrew Jeffery 	 * interrupt on an ODR event.
200d0f5da0dSAndrew Jeffery 	 * So, write a dummy value of 0xff to ODR, which will ensure that an
201d0f5da0dSAndrew Jeffery 	 * interrupt is triggered, and can be ignored by the host.
202d0f5da0dSAndrew Jeffery 	 */
203d0f5da0dSAndrew Jeffery 	data = 0xff;
204d0f5da0dSAndrew Jeffery 
205f13cb971SAndrew Jeffery 	rc = mctp_astlpc_kcs_write(astlpc, MCTP_ASTLPC_KCS_REG_STATUS, status);
206d0f5da0dSAndrew Jeffery 	if (rc) {
207d0f5da0dSAndrew Jeffery 		astlpc_prwarn(astlpc, "KCS status write failed");
208d0f5da0dSAndrew Jeffery 		return -1;
209d0f5da0dSAndrew Jeffery 	}
210d0f5da0dSAndrew Jeffery 
211f13cb971SAndrew Jeffery 	rc = mctp_astlpc_kcs_write(astlpc, MCTP_ASTLPC_KCS_REG_DATA, data);
212d0f5da0dSAndrew Jeffery 	if (rc) {
213d0f5da0dSAndrew Jeffery 		astlpc_prwarn(astlpc, "KCS dummy data write failed");
214d0f5da0dSAndrew Jeffery 		return -1;
215d0f5da0dSAndrew Jeffery 	}
216d0f5da0dSAndrew Jeffery 
217d0f5da0dSAndrew Jeffery 	return 0;
218d0f5da0dSAndrew Jeffery }
219d0f5da0dSAndrew Jeffery 
2203a540664SAndrew Jeffery static int mctp_astlpc_layout_read(struct mctp_binding_astlpc *astlpc,
2213a540664SAndrew Jeffery 				   struct mctp_astlpc_layout *layout)
2223a540664SAndrew Jeffery {
2233a540664SAndrew Jeffery 	struct mctp_lpcmap_hdr hdr;
2243a540664SAndrew Jeffery 	int rc;
2253a540664SAndrew Jeffery 
2263a540664SAndrew Jeffery 	rc = mctp_astlpc_lpc_read(astlpc, &hdr, 0, sizeof(hdr));
2273a540664SAndrew Jeffery 	if (rc < 0)
2283a540664SAndrew Jeffery 		return rc;
2293a540664SAndrew Jeffery 
2303a540664SAndrew Jeffery 	/* Flip the buffers as the names are defined in terms of the host */
2313a540664SAndrew Jeffery 	if (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_BMC) {
2323a540664SAndrew Jeffery 		layout->rx.offset = be32toh(hdr.layout.tx_offset);
2333a540664SAndrew Jeffery 		layout->rx.size = be32toh(hdr.layout.tx_size);
2343a540664SAndrew Jeffery 		layout->tx.offset = be32toh(hdr.layout.rx_offset);
2353a540664SAndrew Jeffery 		layout->tx.size = be32toh(hdr.layout.rx_size);
2363a540664SAndrew Jeffery 	} else {
2373a540664SAndrew Jeffery 		assert(astlpc->mode == MCTP_BINDING_ASTLPC_MODE_HOST);
2383a540664SAndrew Jeffery 
2393a540664SAndrew Jeffery 		layout->rx.offset = be32toh(hdr.layout.rx_offset);
2403a540664SAndrew Jeffery 		layout->rx.size = be32toh(hdr.layout.rx_size);
2413a540664SAndrew Jeffery 		layout->tx.offset = be32toh(hdr.layout.tx_offset);
2423a540664SAndrew Jeffery 		layout->tx.size = be32toh(hdr.layout.tx_size);
2433a540664SAndrew Jeffery 	}
2443a540664SAndrew Jeffery 
2453a540664SAndrew Jeffery 	return 0;
2463a540664SAndrew Jeffery }
2473a540664SAndrew Jeffery 
2483a540664SAndrew Jeffery static int mctp_astlpc_layout_write(struct mctp_binding_astlpc *astlpc,
2493a540664SAndrew Jeffery 				    struct mctp_astlpc_layout *layout)
2503a540664SAndrew Jeffery {
2513a540664SAndrew Jeffery 	uint32_t rx_size_be;
2523a540664SAndrew Jeffery 
2533a540664SAndrew Jeffery 	if (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_BMC) {
2543a540664SAndrew Jeffery 		struct mctp_lpcmap_hdr hdr;
2553a540664SAndrew Jeffery 
2563a540664SAndrew Jeffery 		/*
2573a540664SAndrew Jeffery 		 * Flip the buffers as the names are defined in terms of the
2583a540664SAndrew Jeffery 		 * host
2593a540664SAndrew Jeffery 		 */
2603a540664SAndrew Jeffery 		hdr.layout.rx_offset = htobe32(layout->tx.offset);
2613a540664SAndrew Jeffery 		hdr.layout.rx_size = htobe32(layout->tx.size);
2623a540664SAndrew Jeffery 		hdr.layout.tx_offset = htobe32(layout->rx.offset);
2633a540664SAndrew Jeffery 		hdr.layout.tx_size = htobe32(layout->rx.size);
2643a540664SAndrew Jeffery 
2653a540664SAndrew Jeffery 		return mctp_astlpc_lpc_write(astlpc, &hdr.layout,
2663a540664SAndrew Jeffery 				offsetof(struct mctp_lpcmap_hdr, layout),
2673a540664SAndrew Jeffery 				sizeof(hdr.layout));
2683a540664SAndrew Jeffery 	}
2693a540664SAndrew Jeffery 
2703a540664SAndrew Jeffery 	assert(astlpc->mode == MCTP_BINDING_ASTLPC_MODE_HOST);
2713a540664SAndrew Jeffery 
2723a540664SAndrew Jeffery 	/*
2733a540664SAndrew Jeffery 	 * As of v2 we only need to write rx_size - the offsets are controlled
2743a540664SAndrew Jeffery 	 * by the BMC, as is the BMC's rx_size (host tx_size).
2753a540664SAndrew Jeffery 	 */
2763a540664SAndrew Jeffery 	rx_size_be = htobe32(layout->rx.size);
2773a540664SAndrew Jeffery 	return mctp_astlpc_lpc_write(astlpc, &rx_size_be,
2783a540664SAndrew Jeffery 			offsetof(struct mctp_lpcmap_hdr, layout.rx_size),
2793a540664SAndrew Jeffery 			sizeof(rx_size_be));
2803a540664SAndrew Jeffery }
2813a540664SAndrew Jeffery 
2823a540664SAndrew Jeffery static bool mctp_astlpc_buffer_validate(struct mctp_astlpc_buffer *buf,
2833a540664SAndrew Jeffery 					const char *name)
2843a540664SAndrew Jeffery {
2853a540664SAndrew Jeffery 	/* Check for overflow */
2863a540664SAndrew Jeffery 	if (buf->offset + buf->size < buf->offset) {
2873a540664SAndrew Jeffery 		mctp_prerr(
2883a540664SAndrew Jeffery 			"%s packet buffer parameters overflow: offset: 0x%" PRIx32
2893a540664SAndrew Jeffery 			", size: %" PRIu32,
2903a540664SAndrew Jeffery 			name, buf->offset, buf->size);
2913a540664SAndrew Jeffery 		return false;
2923a540664SAndrew Jeffery 	}
2933a540664SAndrew Jeffery 
2943a540664SAndrew Jeffery 	/* Check that the buffers are contained within the allocated space */
2953a540664SAndrew Jeffery 	if (buf->offset + buf->size > LPC_WIN_SIZE) {
2963a540664SAndrew Jeffery 		mctp_prerr(
2973a540664SAndrew Jeffery 			"%s packet buffer parameters exceed %uM window size: offset: 0x%" PRIx32
2983a540664SAndrew Jeffery 			", size: %" PRIu32,
2993a540664SAndrew Jeffery 			name, (LPC_WIN_SIZE / (1024 * 1024)), buf->offset,
3003a540664SAndrew Jeffery 			buf->size);
3013a540664SAndrew Jeffery 		return false;
3023a540664SAndrew Jeffery 	}
3033a540664SAndrew Jeffery 
3043a540664SAndrew Jeffery 	/* Check that the baseline transmission unit is supported */
3053a540664SAndrew Jeffery 	if (buf->size < ASTLPC_PACKET_SIZE(MCTP_PACKET_SIZE(MCTP_BTU))) {
3063a540664SAndrew Jeffery 		mctp_prerr(
3073a540664SAndrew Jeffery 			"%s packet buffer too small: Require %zu bytes to support the %u byte baseline transmission unit, found %" PRIu32,
3083a540664SAndrew Jeffery 			name, ASTLPC_PACKET_SIZE(MCTP_PACKET_SIZE(MCTP_BTU)),
3093a540664SAndrew Jeffery 			MCTP_BTU, buf->size);
3103a540664SAndrew Jeffery 		return false;
3113a540664SAndrew Jeffery 	}
3123a540664SAndrew Jeffery 
3133a540664SAndrew Jeffery 	/* Check for overlap with the control space */
3143a540664SAndrew Jeffery 	if (buf->offset < control_size) {
3153a540664SAndrew Jeffery 		mctp_prerr(
3163a540664SAndrew Jeffery 			"%s packet buffer overlaps control region {0x%" PRIx32
3173a540664SAndrew Jeffery 			", %" PRIu32 "}: Rx {0x%" PRIx32 ", %" PRIu32 "}",
3183a540664SAndrew Jeffery 			name, 0U, control_size, buf->offset, buf->size);
3193a540664SAndrew Jeffery 		return false;
3203a540664SAndrew Jeffery 	}
3213a540664SAndrew Jeffery 
3223a540664SAndrew Jeffery 	return true;
3233a540664SAndrew Jeffery }
3243a540664SAndrew Jeffery 
3253a540664SAndrew Jeffery static bool mctp_astlpc_layout_validate(struct mctp_astlpc_layout *layout)
3263a540664SAndrew Jeffery {
3273a540664SAndrew Jeffery 	struct mctp_astlpc_buffer *rx = &layout->rx;
3283a540664SAndrew Jeffery 	struct mctp_astlpc_buffer *tx = &layout->tx;
3293a540664SAndrew Jeffery 	bool rx_valid, tx_valid;
3303a540664SAndrew Jeffery 
3313a540664SAndrew Jeffery 	rx_valid = mctp_astlpc_buffer_validate(rx, "Rx");
3323a540664SAndrew Jeffery 	tx_valid = mctp_astlpc_buffer_validate(tx, "Tx");
3333a540664SAndrew Jeffery 
3343a540664SAndrew Jeffery 	if (!(rx_valid && tx_valid))
3353a540664SAndrew Jeffery 		return false;
3363a540664SAndrew Jeffery 
3373a540664SAndrew Jeffery 	/* Check that the buffers are disjoint */
3383a540664SAndrew Jeffery 	if ((rx->offset <= tx->offset && rx->offset + rx->size > tx->offset) ||
3393a540664SAndrew Jeffery 	    (tx->offset <= rx->offset && tx->offset + tx->size > rx->offset)) {
3403a540664SAndrew Jeffery 		mctp_prerr("Rx and Tx packet buffers overlap: Rx {0x%" PRIx32
3413a540664SAndrew Jeffery 			   ", %" PRIu32 "}, Tx {0x%" PRIx32 ", %" PRIu32 "}",
3423a540664SAndrew Jeffery 			   rx->offset, rx->size, tx->offset, tx->size);
3433a540664SAndrew Jeffery 		return false;
3443a540664SAndrew Jeffery 	}
3453a540664SAndrew Jeffery 
3463a540664SAndrew Jeffery 	return true;
3473a540664SAndrew Jeffery }
3483a540664SAndrew Jeffery 
3497cd72f14SAndrew Jeffery static int mctp_astlpc_init_bmc(struct mctp_binding_astlpc *astlpc)
3507cd72f14SAndrew Jeffery {
35155fb90beSAndrew Jeffery 	struct mctp_lpcmap_hdr hdr = { 0 };
3527cd72f14SAndrew Jeffery 	uint8_t status;
3533a540664SAndrew Jeffery 	size_t sz;
3543a540664SAndrew Jeffery 
3553a540664SAndrew Jeffery 	/*
3563a540664SAndrew Jeffery 	 * The largest buffer size is half of the allocated MCTP space
3573a540664SAndrew Jeffery 	 * excluding the control space.
3583a540664SAndrew Jeffery 	 */
3593a540664SAndrew Jeffery 	sz = ((LPC_WIN_SIZE - control_size) / 2);
3603a540664SAndrew Jeffery 
3613a540664SAndrew Jeffery 	/*
3623a540664SAndrew Jeffery 	 * Trim the MTU to a multiple of 16 to meet the requirements of 12.17
3633a540664SAndrew Jeffery 	 * Query Hop in DSP0236 v1.3.0.
3643a540664SAndrew Jeffery 	 */
3653a540664SAndrew Jeffery 	sz = MCTP_BODY_SIZE(ASTLPC_BODY_SIZE(sz));
3663a540664SAndrew Jeffery 	sz &= ~0xfUL;
3673a540664SAndrew Jeffery 	sz = ASTLPC_PACKET_SIZE(MCTP_PACKET_SIZE(sz));
3687cd72f14SAndrew Jeffery 
369a9368980SAndrew Jeffery 	if (astlpc->requested_mtu) {
370a9368980SAndrew Jeffery 		size_t r;
371a9368980SAndrew Jeffery 
372a9368980SAndrew Jeffery 		r = ASTLPC_PACKET_SIZE(MCTP_PACKET_SIZE(astlpc->requested_mtu));
373a9368980SAndrew Jeffery 		sz = MIN(sz, r);
374a9368980SAndrew Jeffery 	}
375a9368980SAndrew Jeffery 
3767cd72f14SAndrew Jeffery 	/* Flip the buffers as the names are defined in terms of the host */
3773a540664SAndrew Jeffery 	astlpc->layout.tx.offset = control_size;
3783a540664SAndrew Jeffery 	astlpc->layout.tx.size = sz;
3793a540664SAndrew Jeffery 	astlpc->layout.rx.offset =
3803a540664SAndrew Jeffery 		astlpc->layout.tx.offset + astlpc->layout.tx.size;
3813a540664SAndrew Jeffery 	astlpc->layout.rx.size = sz;
3823a540664SAndrew Jeffery 
383a9368980SAndrew Jeffery 	if (!mctp_astlpc_layout_validate(&astlpc->layout)) {
384a9368980SAndrew Jeffery 		astlpc_prerr(astlpc, "Cannot support an MTU of %zu", sz);
385a9368980SAndrew Jeffery 		return -EINVAL;
386a9368980SAndrew Jeffery 	}
3877cd72f14SAndrew Jeffery 
38855fb90beSAndrew Jeffery 	hdr = (struct mctp_lpcmap_hdr){
38955fb90beSAndrew Jeffery 		.magic = htobe32(ASTLPC_MCTP_MAGIC),
39055fb90beSAndrew Jeffery 		.bmc_ver_min = htobe16(ASTLPC_VER_MIN),
39155fb90beSAndrew Jeffery 		.bmc_ver_cur = htobe16(ASTLPC_VER_CUR),
3927cd72f14SAndrew Jeffery 
3937cd72f14SAndrew Jeffery 		/* Flip the buffers back as we're now describing the host's
3947cd72f14SAndrew Jeffery 		 * configuration to the host */
3953a540664SAndrew Jeffery 		.layout.rx_offset = htobe32(astlpc->layout.tx.offset),
3963a540664SAndrew Jeffery 		.layout.rx_size = htobe32(astlpc->layout.tx.size),
3973a540664SAndrew Jeffery 		.layout.tx_offset = htobe32(astlpc->layout.rx.offset),
3983a540664SAndrew Jeffery 		.layout.tx_size = htobe32(astlpc->layout.rx.size),
39955fb90beSAndrew Jeffery 	};
4007cd72f14SAndrew Jeffery 
40155fb90beSAndrew Jeffery 	mctp_astlpc_lpc_write(astlpc, &hdr, 0, sizeof(hdr));
4027cd72f14SAndrew Jeffery 
403b3b55a6bSAndrew Jeffery 	/*
404b3b55a6bSAndrew Jeffery 	 * Set status indicating that the BMC is now active. Be explicit about
405b3b55a6bSAndrew Jeffery 	 * clearing OBF; we're reinitialising the binding and so any previous
406b3b55a6bSAndrew Jeffery 	 * buffer state is irrelevant.
407b3b55a6bSAndrew Jeffery 	 */
408b3b55a6bSAndrew Jeffery 	status = KCS_STATUS_BMC_READY & ~KCS_STATUS_OBF;
409d0f5da0dSAndrew Jeffery 	return mctp_astlpc_kcs_set_status(astlpc, status);
4107cd72f14SAndrew Jeffery }
4117cd72f14SAndrew Jeffery 
4127cd72f14SAndrew Jeffery static int mctp_binding_astlpc_start_bmc(struct mctp_binding *b)
4137cd72f14SAndrew Jeffery {
4147cd72f14SAndrew Jeffery 	struct mctp_binding_astlpc *astlpc =
4157cd72f14SAndrew Jeffery 		container_of(b, struct mctp_binding_astlpc, binding);
4167cd72f14SAndrew Jeffery 
4177cd72f14SAndrew Jeffery 	return mctp_astlpc_init_bmc(astlpc);
4187cd72f14SAndrew Jeffery }
4197cd72f14SAndrew Jeffery 
4204e8264b7SAndrew Jeffery static bool mctp_astlpc_validate_version(uint16_t bmc_ver_min,
4214e8264b7SAndrew Jeffery 					 uint16_t bmc_ver_cur,
4224e8264b7SAndrew Jeffery 					 uint16_t host_ver_min,
4234e8264b7SAndrew Jeffery 					 uint16_t host_ver_cur)
4244e8264b7SAndrew Jeffery {
4254e8264b7SAndrew Jeffery 	if (!(bmc_ver_min && bmc_ver_cur && host_ver_min && host_ver_cur)) {
4264e8264b7SAndrew Jeffery 		mctp_prerr("Invalid version present in [%" PRIu16 ", %" PRIu16
4274e8264b7SAndrew Jeffery 			   "], [%" PRIu16 ", %" PRIu16 "]",
4284e8264b7SAndrew Jeffery 			   bmc_ver_min, bmc_ver_cur, host_ver_min,
4294e8264b7SAndrew Jeffery 			   host_ver_cur);
4304e8264b7SAndrew Jeffery 		return false;
4314e8264b7SAndrew Jeffery 	} else if (bmc_ver_min > bmc_ver_cur) {
4324e8264b7SAndrew Jeffery 		mctp_prerr("Invalid bmc version range [%" PRIu16 ", %" PRIu16
4334e8264b7SAndrew Jeffery 			   "]",
4344e8264b7SAndrew Jeffery 			   bmc_ver_min, bmc_ver_cur);
4354e8264b7SAndrew Jeffery 		return false;
4364e8264b7SAndrew Jeffery 	} else if (host_ver_min > host_ver_cur) {
4374e8264b7SAndrew Jeffery 		mctp_prerr("Invalid host version range [%" PRIu16 ", %" PRIu16
4384e8264b7SAndrew Jeffery 			   "]",
4394e8264b7SAndrew Jeffery 			   host_ver_min, host_ver_cur);
4404e8264b7SAndrew Jeffery 		return false;
4414e8264b7SAndrew Jeffery 	} else if ((host_ver_cur < bmc_ver_min) ||
4424e8264b7SAndrew Jeffery 		   (host_ver_min > bmc_ver_cur)) {
4434e8264b7SAndrew Jeffery 		mctp_prerr(
4444e8264b7SAndrew Jeffery 			"Unable to satisfy version negotiation with ranges [%" PRIu16
4454e8264b7SAndrew Jeffery 			", %" PRIu16 "] and [%" PRIu16 ", %" PRIu16 "]",
4464e8264b7SAndrew Jeffery 			bmc_ver_min, bmc_ver_cur, host_ver_min, host_ver_cur);
4474e8264b7SAndrew Jeffery 		return false;
4484e8264b7SAndrew Jeffery 	}
4494e8264b7SAndrew Jeffery 
4504e8264b7SAndrew Jeffery 	return true;
4514e8264b7SAndrew Jeffery }
4524e8264b7SAndrew Jeffery 
4533a540664SAndrew Jeffery static int mctp_astlpc_negotiate_layout_host(struct mctp_binding_astlpc *astlpc)
4543a540664SAndrew Jeffery {
4553a540664SAndrew Jeffery 	struct mctp_astlpc_layout layout;
4563a540664SAndrew Jeffery 	uint32_t sz;
4573a540664SAndrew Jeffery 	int rc;
4583a540664SAndrew Jeffery 
4593a540664SAndrew Jeffery 	rc = mctp_astlpc_layout_read(astlpc, &layout);
4603a540664SAndrew Jeffery 	if (rc < 0)
4613a540664SAndrew Jeffery 		return rc;
4623a540664SAndrew Jeffery 
4633a540664SAndrew Jeffery 	if (!mctp_astlpc_layout_validate(&layout)) {
4643a540664SAndrew Jeffery 		astlpc_prerr(
4653a540664SAndrew Jeffery 			astlpc,
4663a540664SAndrew Jeffery 			"BMC provided invalid buffer layout: Rx {0x%" PRIx32
4673a540664SAndrew Jeffery 			", %" PRIu32 "}, Tx {0x%" PRIx32 ", %" PRIu32 "}",
4683a540664SAndrew Jeffery 			layout.rx.offset, layout.rx.size, layout.tx.offset,
4693a540664SAndrew Jeffery 			layout.tx.size);
4703a540664SAndrew Jeffery 		return -EINVAL;
4713a540664SAndrew Jeffery 	}
4723a540664SAndrew Jeffery 
473a9368980SAndrew Jeffery 	astlpc_prinfo(astlpc, "Desire an MTU of %" PRIu32 " bytes",
474a9368980SAndrew Jeffery 		      astlpc->requested_mtu);
475a9368980SAndrew Jeffery 
476a9368980SAndrew Jeffery 	sz = ASTLPC_PACKET_SIZE(MCTP_PACKET_SIZE(astlpc->requested_mtu));
4773a540664SAndrew Jeffery 	layout.rx.size = sz;
4783a540664SAndrew Jeffery 
4793a540664SAndrew Jeffery 	if (!mctp_astlpc_layout_validate(&layout)) {
4803a540664SAndrew Jeffery 		astlpc_prerr(
4813a540664SAndrew Jeffery 			astlpc,
4823a540664SAndrew Jeffery 			"Generated invalid buffer layout with size %" PRIu32
4833a540664SAndrew Jeffery 			": Rx {0x%" PRIx32 ", %" PRIu32 "}, Tx {0x%" PRIx32
4843a540664SAndrew Jeffery 			", %" PRIu32 "}",
4853a540664SAndrew Jeffery 			sz, layout.rx.offset, layout.rx.size, layout.tx.offset,
4863a540664SAndrew Jeffery 			layout.tx.size);
4873a540664SAndrew Jeffery 		return -EINVAL;
4883a540664SAndrew Jeffery 	}
4893a540664SAndrew Jeffery 
490a9368980SAndrew Jeffery 	astlpc_prinfo(astlpc, "Requesting MTU of %" PRIu32 " bytes",
491a9368980SAndrew Jeffery 		      astlpc->requested_mtu);
4923a540664SAndrew Jeffery 
4933a540664SAndrew Jeffery 	return mctp_astlpc_layout_write(astlpc, &layout);
4943a540664SAndrew Jeffery }
4953a540664SAndrew Jeffery 
4967cd72f14SAndrew Jeffery static int mctp_astlpc_init_host(struct mctp_binding_astlpc *astlpc)
4977cd72f14SAndrew Jeffery {
49855fb90beSAndrew Jeffery 	const uint16_t ver_min_be = htobe16(ASTLPC_VER_MIN);
49955fb90beSAndrew Jeffery 	const uint16_t ver_cur_be = htobe16(ASTLPC_VER_CUR);
5004e8264b7SAndrew Jeffery 	uint16_t bmc_ver_min, bmc_ver_cur;
50155fb90beSAndrew Jeffery 	struct mctp_lpcmap_hdr hdr;
5027cd72f14SAndrew Jeffery 	uint8_t status;
5037cd72f14SAndrew Jeffery 	int rc;
5047cd72f14SAndrew Jeffery 
505f13cb971SAndrew Jeffery 	rc = mctp_astlpc_kcs_read(astlpc, MCTP_ASTLPC_KCS_REG_STATUS, &status);
5067cd72f14SAndrew Jeffery 	if (rc) {
5077cd72f14SAndrew Jeffery 		mctp_prwarn("KCS status read failed");
5087cd72f14SAndrew Jeffery 		return rc;
5097cd72f14SAndrew Jeffery 	}
5107cd72f14SAndrew Jeffery 
5117cd72f14SAndrew Jeffery 	astlpc->kcs_status = status;
5127cd72f14SAndrew Jeffery 
5137cd72f14SAndrew Jeffery 	if (!(status & KCS_STATUS_BMC_READY))
5147cd72f14SAndrew Jeffery 		return -EHOSTDOWN;
5157cd72f14SAndrew Jeffery 
51655fb90beSAndrew Jeffery 	mctp_astlpc_lpc_read(astlpc, &hdr, 0, sizeof(hdr));
5177cd72f14SAndrew Jeffery 
5184e8264b7SAndrew Jeffery 	bmc_ver_min = be16toh(hdr.bmc_ver_min);
5194e8264b7SAndrew Jeffery 	bmc_ver_cur = be16toh(hdr.bmc_ver_cur);
5204e8264b7SAndrew Jeffery 
5214e8264b7SAndrew Jeffery 	if (!mctp_astlpc_validate_version(bmc_ver_min, bmc_ver_cur,
5224e8264b7SAndrew Jeffery 					  ASTLPC_VER_MIN, ASTLPC_VER_CUR)) {
5234e8264b7SAndrew Jeffery 		astlpc_prerr(astlpc, "Cannot negotiate with invalid versions");
5244e8264b7SAndrew Jeffery 		return -EINVAL;
5254e8264b7SAndrew Jeffery 	}
5264e8264b7SAndrew Jeffery 
5273a540664SAndrew Jeffery 	/*
5283a540664SAndrew Jeffery 	 * Negotation always chooses the highest protocol version that
5293a540664SAndrew Jeffery 	 * satisfies the version constraints. So check whether the BMC supports
5303a540664SAndrew Jeffery 	 * v2, and if so, negotiate in v2 style.
5313a540664SAndrew Jeffery 	 */
5323a540664SAndrew Jeffery 	if (ASTLPC_VER_CUR >= 2 && bmc_ver_cur >= 2) {
5333a540664SAndrew Jeffery 		rc = mctp_astlpc_negotiate_layout_host(astlpc);
5343a540664SAndrew Jeffery 		if (rc < 0)
5353a540664SAndrew Jeffery 			return rc;
5363a540664SAndrew Jeffery 	}
5373a540664SAndrew Jeffery 
5383a540664SAndrew Jeffery 	/* Version negotiation */
53955fb90beSAndrew Jeffery 	mctp_astlpc_lpc_write(astlpc, &ver_min_be,
54055fb90beSAndrew Jeffery 			      offsetof(struct mctp_lpcmap_hdr, host_ver_min),
54155fb90beSAndrew Jeffery 			      sizeof(ver_min_be));
5427cd72f14SAndrew Jeffery 
54355fb90beSAndrew Jeffery 	mctp_astlpc_lpc_write(astlpc, &ver_cur_be,
54455fb90beSAndrew Jeffery 			      offsetof(struct mctp_lpcmap_hdr, host_ver_cur),
54555fb90beSAndrew Jeffery 			      sizeof(ver_cur_be));
5467cd72f14SAndrew Jeffery 
5477cd72f14SAndrew Jeffery 	/* Send channel init command */
548f13cb971SAndrew Jeffery 	rc = mctp_astlpc_kcs_write(astlpc, MCTP_ASTLPC_KCS_REG_DATA, 0x0);
5497cd72f14SAndrew Jeffery 	if (rc) {
5509101a2abSAndrew Jeffery 		astlpc_prwarn(astlpc, "KCS write failed");
5517cd72f14SAndrew Jeffery 	}
5527cd72f14SAndrew Jeffery 
5537cd72f14SAndrew Jeffery 	return rc;
5547cd72f14SAndrew Jeffery }
5557cd72f14SAndrew Jeffery 
5567cd72f14SAndrew Jeffery static int mctp_binding_astlpc_start_host(struct mctp_binding *b)
5577cd72f14SAndrew Jeffery {
5587cd72f14SAndrew Jeffery 	struct mctp_binding_astlpc *astlpc =
5597cd72f14SAndrew Jeffery 		container_of(b, struct mctp_binding_astlpc, binding);
5607cd72f14SAndrew Jeffery 
5617cd72f14SAndrew Jeffery 	return mctp_astlpc_init_host(astlpc);
5627cd72f14SAndrew Jeffery }
5637cd72f14SAndrew Jeffery 
5647cd72f14SAndrew Jeffery static bool __mctp_astlpc_kcs_ready(struct mctp_binding_astlpc *astlpc,
5657cd72f14SAndrew Jeffery 				    uint8_t status, bool is_write)
5667cd72f14SAndrew Jeffery {
5677cd72f14SAndrew Jeffery 	bool is_bmc;
5687cd72f14SAndrew Jeffery 	bool ready_state;
5697cd72f14SAndrew Jeffery 	uint8_t flag;
5707cd72f14SAndrew Jeffery 
5717cd72f14SAndrew Jeffery 	is_bmc = (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_BMC);
5727cd72f14SAndrew Jeffery 	flag = (is_bmc ^ is_write) ? KCS_STATUS_IBF : KCS_STATUS_OBF;
5737cd72f14SAndrew Jeffery 	ready_state = is_write ? 0 : 1;
5747cd72f14SAndrew Jeffery 
5757cd72f14SAndrew Jeffery 	return !!(status & flag) == ready_state;
5767cd72f14SAndrew Jeffery }
5777cd72f14SAndrew Jeffery 
5787cd72f14SAndrew Jeffery static inline bool
5797cd72f14SAndrew Jeffery mctp_astlpc_kcs_read_ready(struct mctp_binding_astlpc *astlpc, uint8_t status)
5807cd72f14SAndrew Jeffery {
5817cd72f14SAndrew Jeffery 	return __mctp_astlpc_kcs_ready(astlpc, status, false);
5827cd72f14SAndrew Jeffery }
5837cd72f14SAndrew Jeffery 
5847cd72f14SAndrew Jeffery static inline bool
5857cd72f14SAndrew Jeffery mctp_astlpc_kcs_write_ready(struct mctp_binding_astlpc *astlpc, uint8_t status)
5867cd72f14SAndrew Jeffery {
5877cd72f14SAndrew Jeffery 	return __mctp_astlpc_kcs_ready(astlpc, status, true);
5887cd72f14SAndrew Jeffery }
5897cd72f14SAndrew Jeffery 
590672c8852SJeremy Kerr static int mctp_astlpc_kcs_send(struct mctp_binding_astlpc *astlpc,
591672c8852SJeremy Kerr 		uint8_t data)
592672c8852SJeremy Kerr {
593672c8852SJeremy Kerr 	uint8_t status;
594672c8852SJeremy Kerr 	int rc;
595672c8852SJeremy Kerr 
596672c8852SJeremy Kerr 	for (;;) {
597f13cb971SAndrew Jeffery 		rc = mctp_astlpc_kcs_read(astlpc, MCTP_ASTLPC_KCS_REG_STATUS,
598f13cb971SAndrew Jeffery 					  &status);
5991b27fe87SAndrew Jeffery 		if (rc) {
6009101a2abSAndrew Jeffery 			astlpc_prwarn(astlpc, "KCS status read failed");
601672c8852SJeremy Kerr 			return -1;
602672c8852SJeremy Kerr 		}
6037cd72f14SAndrew Jeffery 		if (mctp_astlpc_kcs_write_ready(astlpc, status))
604672c8852SJeremy Kerr 			break;
605672c8852SJeremy Kerr 		/* todo: timeout */
606672c8852SJeremy Kerr 	}
607672c8852SJeremy Kerr 
608f13cb971SAndrew Jeffery 	rc = mctp_astlpc_kcs_write(astlpc, MCTP_ASTLPC_KCS_REG_DATA, data);
609bc53d35aSJeremy Kerr 	if (rc) {
6109101a2abSAndrew Jeffery 		astlpc_prwarn(astlpc, "KCS data write failed");
611672c8852SJeremy Kerr 		return -1;
612672c8852SJeremy Kerr 	}
613672c8852SJeremy Kerr 
614672c8852SJeremy Kerr 	return 0;
615672c8852SJeremy Kerr }
616672c8852SJeremy Kerr 
617672c8852SJeremy Kerr static int mctp_binding_astlpc_tx(struct mctp_binding *b,
618672c8852SJeremy Kerr 		struct mctp_pktbuf *pkt)
619672c8852SJeremy Kerr {
620672c8852SJeremy Kerr 	struct mctp_binding_astlpc *astlpc = binding_to_astlpc(b);
62155fb90beSAndrew Jeffery 	uint32_t len, len_be;
622edfe383fSAndrew Jeffery 	struct mctp_hdr *hdr;
623672c8852SJeremy Kerr 
624edfe383fSAndrew Jeffery 	hdr = mctp_pktbuf_hdr(pkt);
625672c8852SJeremy Kerr 	len = mctp_pktbuf_size(pkt);
626edfe383fSAndrew Jeffery 
6279101a2abSAndrew Jeffery 	astlpc_prdebug(astlpc,
6289101a2abSAndrew Jeffery 		       "%s: Transmitting %" PRIu32
6299101a2abSAndrew Jeffery 		       "-byte packet (%hhu, %hhu, 0x%hhx)",
630edfe383fSAndrew Jeffery 		       __func__, len, hdr->src, hdr->dest, hdr->flags_seq_tag);
631edfe383fSAndrew Jeffery 
6327cd72f14SAndrew Jeffery 	if (len > ASTLPC_BODY_SIZE(astlpc->layout.tx.size)) {
6339101a2abSAndrew Jeffery 		astlpc_prwarn(astlpc, "invalid TX len 0x%x", len);
634672c8852SJeremy Kerr 		return -1;
635672c8852SJeremy Kerr 	}
636672c8852SJeremy Kerr 
63755fb90beSAndrew Jeffery 	len_be = htobe32(len);
63855fb90beSAndrew Jeffery 	mctp_astlpc_lpc_write(astlpc, &len_be, astlpc->layout.tx.offset,
63955fb90beSAndrew Jeffery 			      sizeof(len_be));
64055fb90beSAndrew Jeffery 	mctp_astlpc_lpc_write(astlpc, hdr, astlpc->layout.tx.offset + 4, len);
641672c8852SJeremy Kerr 
642672c8852SJeremy Kerr 	mctp_binding_set_tx_enabled(b, false);
643672c8852SJeremy Kerr 
644672c8852SJeremy Kerr 	mctp_astlpc_kcs_send(astlpc, 0x1);
645672c8852SJeremy Kerr 	return 0;
646672c8852SJeremy Kerr }
647672c8852SJeremy Kerr 
6484e8264b7SAndrew Jeffery static uint16_t mctp_astlpc_negotiate_version(uint16_t bmc_ver_min,
6494e8264b7SAndrew Jeffery 					      uint16_t bmc_ver_cur,
6504e8264b7SAndrew Jeffery 					      uint16_t host_ver_min,
6514e8264b7SAndrew Jeffery 					      uint16_t host_ver_cur)
6524e8264b7SAndrew Jeffery {
6534e8264b7SAndrew Jeffery 	if (!mctp_astlpc_validate_version(bmc_ver_min, bmc_ver_cur,
6544e8264b7SAndrew Jeffery 					  host_ver_min, host_ver_cur))
6554e8264b7SAndrew Jeffery 		return ASTLPC_VER_BAD;
6564e8264b7SAndrew Jeffery 
6574e8264b7SAndrew Jeffery 	if (bmc_ver_cur < host_ver_cur)
6584e8264b7SAndrew Jeffery 		return bmc_ver_cur;
6594e8264b7SAndrew Jeffery 
6604e8264b7SAndrew Jeffery 	return host_ver_cur;
6614e8264b7SAndrew Jeffery }
6624e8264b7SAndrew Jeffery 
6633a540664SAndrew Jeffery static uint32_t mctp_astlpc_calculate_mtu(struct mctp_binding_astlpc *astlpc,
6643a540664SAndrew Jeffery 					  struct mctp_astlpc_layout *layout)
6653a540664SAndrew Jeffery {
6663a540664SAndrew Jeffery 	uint32_t low, high, limit;
6673a540664SAndrew Jeffery 
6683a540664SAndrew Jeffery 	/* Derive the largest MTU the BMC _can_ support */
6693a540664SAndrew Jeffery 	low = MIN(astlpc->layout.rx.offset, astlpc->layout.tx.offset);
6703a540664SAndrew Jeffery 	high = MAX(astlpc->layout.rx.offset, astlpc->layout.tx.offset);
6713a540664SAndrew Jeffery 	limit = high - low;
6723a540664SAndrew Jeffery 
673a9368980SAndrew Jeffery 	/* Determine the largest MTU the BMC _wants_ to support */
674a9368980SAndrew Jeffery 	if (astlpc->requested_mtu) {
675a9368980SAndrew Jeffery 		uint32_t req = astlpc->requested_mtu;
676a9368980SAndrew Jeffery 
677a9368980SAndrew Jeffery 		limit = MIN(limit, ASTLPC_PACKET_SIZE(MCTP_PACKET_SIZE(req)));
678a9368980SAndrew Jeffery 	}
6793a540664SAndrew Jeffery 
6803a540664SAndrew Jeffery 	/* Determine the accepted MTU, applied both directions by convention */
6813a540664SAndrew Jeffery 	return MCTP_BODY_SIZE(ASTLPC_BODY_SIZE(MIN(limit, layout->tx.size)));
6823a540664SAndrew Jeffery }
6833a540664SAndrew Jeffery 
6843a540664SAndrew Jeffery static int mctp_astlpc_negotiate_layout_bmc(struct mctp_binding_astlpc *astlpc)
6853a540664SAndrew Jeffery {
6863a540664SAndrew Jeffery 	struct mctp_astlpc_layout proposed, pending;
6873a540664SAndrew Jeffery 	uint32_t sz, mtu;
6883a540664SAndrew Jeffery 	int rc;
6893a540664SAndrew Jeffery 
6903a540664SAndrew Jeffery 	/* Extract the host's proposed layout */
6913a540664SAndrew Jeffery 	rc = mctp_astlpc_layout_read(astlpc, &proposed);
6923a540664SAndrew Jeffery 	if (rc < 0)
6933a540664SAndrew Jeffery 		return rc;
6943a540664SAndrew Jeffery 
6953a540664SAndrew Jeffery 	if (!mctp_astlpc_layout_validate(&proposed))
6963a540664SAndrew Jeffery 		return -EINVAL;
6973a540664SAndrew Jeffery 
6983a540664SAndrew Jeffery 	/* Negotiate the MTU */
6993a540664SAndrew Jeffery 	mtu = mctp_astlpc_calculate_mtu(astlpc, &proposed);
7003a540664SAndrew Jeffery 	sz = ASTLPC_PACKET_SIZE(MCTP_PACKET_SIZE(mtu));
7013a540664SAndrew Jeffery 
7023a540664SAndrew Jeffery 	/*
7033a540664SAndrew Jeffery 	 * Use symmetric MTUs by convention and to pass constraints in rx/tx
7043a540664SAndrew Jeffery 	 * functions
7053a540664SAndrew Jeffery 	 */
7063a540664SAndrew Jeffery 	pending = astlpc->layout;
7073a540664SAndrew Jeffery 	pending.tx.size = sz;
7083a540664SAndrew Jeffery 	pending.rx.size = sz;
7093a540664SAndrew Jeffery 
7103a540664SAndrew Jeffery 	if (mctp_astlpc_layout_validate(&pending)) {
7113a540664SAndrew Jeffery 		/* We found a sensible Rx MTU, so honour it */
7123a540664SAndrew Jeffery 		astlpc->layout = pending;
7133a540664SAndrew Jeffery 
7143a540664SAndrew Jeffery 		/* Enforce the negotiated MTU */
7153a540664SAndrew Jeffery 		rc = mctp_astlpc_layout_write(astlpc, &astlpc->layout);
7163a540664SAndrew Jeffery 		if (rc < 0)
7173a540664SAndrew Jeffery 			return rc;
7183a540664SAndrew Jeffery 
7193a540664SAndrew Jeffery 		astlpc_prinfo(astlpc, "Negotiated an MTU of %" PRIu32 " bytes",
7203a540664SAndrew Jeffery 			      mtu);
7213a540664SAndrew Jeffery 	} else {
7223a540664SAndrew Jeffery 		astlpc_prwarn(astlpc, "MTU negotiation failed");
7233a540664SAndrew Jeffery 		return -EINVAL;
7243a540664SAndrew Jeffery 	}
7253a540664SAndrew Jeffery 
7263a540664SAndrew Jeffery 	if (astlpc->version >= 2)
7273a540664SAndrew Jeffery 		astlpc->binding.pkt_size = MCTP_PACKET_SIZE(mtu);
7283a540664SAndrew Jeffery 
7293a540664SAndrew Jeffery 	return 0;
7303a540664SAndrew Jeffery }
7313a540664SAndrew Jeffery 
732672c8852SJeremy Kerr static void mctp_astlpc_init_channel(struct mctp_binding_astlpc *astlpc)
733672c8852SJeremy Kerr {
7344e8264b7SAndrew Jeffery 	uint16_t negotiated, negotiated_be;
7354e8264b7SAndrew Jeffery 	struct mctp_lpcmap_hdr hdr;
7364e8264b7SAndrew Jeffery 	uint8_t status;
7373a540664SAndrew Jeffery 	int rc;
7384e8264b7SAndrew Jeffery 
7394e8264b7SAndrew Jeffery 	mctp_astlpc_lpc_read(astlpc, &hdr, 0, sizeof(hdr));
7404e8264b7SAndrew Jeffery 
7414e8264b7SAndrew Jeffery 	/* Version negotiation */
7424e8264b7SAndrew Jeffery 	negotiated =
7434e8264b7SAndrew Jeffery 		mctp_astlpc_negotiate_version(ASTLPC_VER_MIN, ASTLPC_VER_CUR,
7444e8264b7SAndrew Jeffery 					      be16toh(hdr.host_ver_min),
7454e8264b7SAndrew Jeffery 					      be16toh(hdr.host_ver_cur));
7464e8264b7SAndrew Jeffery 
7473a540664SAndrew Jeffery 	/* Host Rx MTU negotiation: Failure terminates channel init */
7483a540664SAndrew Jeffery 	rc = mctp_astlpc_negotiate_layout_bmc(astlpc);
7493a540664SAndrew Jeffery 	if (rc < 0)
7503a540664SAndrew Jeffery 		negotiated = ASTLPC_VER_BAD;
7513a540664SAndrew Jeffery 
7524e8264b7SAndrew Jeffery 	/* Populate the negotiated version */
7534e8264b7SAndrew Jeffery 	astlpc->version = negotiated;
7544e8264b7SAndrew Jeffery 	negotiated_be = htobe16(negotiated);
7554e8264b7SAndrew Jeffery 	mctp_astlpc_lpc_write(astlpc, &negotiated_be,
75655fb90beSAndrew Jeffery 			      offsetof(struct mctp_lpcmap_hdr, negotiated_ver),
7574e8264b7SAndrew Jeffery 			      sizeof(negotiated_be));
75855fb90beSAndrew Jeffery 
7594e8264b7SAndrew Jeffery 	/* Finalise the configuration */
7604e8264b7SAndrew Jeffery 	status = KCS_STATUS_BMC_READY | KCS_STATUS_OBF;
7614e8264b7SAndrew Jeffery 	if (negotiated > 0) {
7624e8264b7SAndrew Jeffery 		astlpc_prinfo(astlpc, "Negotiated binding version %" PRIu16,
7634e8264b7SAndrew Jeffery 			      negotiated);
7644e8264b7SAndrew Jeffery 		status |= KCS_STATUS_CHANNEL_ACTIVE;
7654e8264b7SAndrew Jeffery 	} else {
7664e8264b7SAndrew Jeffery 		astlpc_prerr(astlpc, "Failed to initialise channel\n");
7674e8264b7SAndrew Jeffery 	}
768672c8852SJeremy Kerr 
7694e8264b7SAndrew Jeffery 	mctp_astlpc_kcs_set_status(astlpc, status);
7704e8264b7SAndrew Jeffery 
7714e8264b7SAndrew Jeffery 	mctp_binding_set_tx_enabled(&astlpc->binding,
7724e8264b7SAndrew Jeffery 				    status & KCS_STATUS_CHANNEL_ACTIVE);
773672c8852SJeremy Kerr }
774672c8852SJeremy Kerr 
775672c8852SJeremy Kerr static void mctp_astlpc_rx_start(struct mctp_binding_astlpc *astlpc)
776672c8852SJeremy Kerr {
777672c8852SJeremy Kerr 	struct mctp_pktbuf *pkt;
778672c8852SJeremy Kerr 	uint32_t len;
779672c8852SJeremy Kerr 
78055fb90beSAndrew Jeffery 	mctp_astlpc_lpc_read(astlpc, &len, astlpc->layout.rx.offset,
78155fb90beSAndrew Jeffery 			     sizeof(len));
782bc53d35aSJeremy Kerr 	len = be32toh(len);
783bc53d35aSJeremy Kerr 
7847cd72f14SAndrew Jeffery 	if (len > ASTLPC_BODY_SIZE(astlpc->layout.rx.size)) {
7859101a2abSAndrew Jeffery 		astlpc_prwarn(astlpc, "invalid RX len 0x%x", len);
786672c8852SJeremy Kerr 		return;
787672c8852SJeremy Kerr 	}
788672c8852SJeremy Kerr 
789b93b6112SAndrew Jeffery 	assert(astlpc->binding.pkt_size >= 0);
790b93b6112SAndrew Jeffery 	if (len > (uint32_t)astlpc->binding.pkt_size) {
791672c8852SJeremy Kerr 		mctp_prwarn("invalid RX len 0x%x", len);
7929101a2abSAndrew Jeffery 		astlpc_prwarn(astlpc, "invalid RX len 0x%x", len);
793672c8852SJeremy Kerr 		return;
794672c8852SJeremy Kerr 	}
795672c8852SJeremy Kerr 
796df15f7e9SJeremy Kerr 	pkt = mctp_pktbuf_alloc(&astlpc->binding, len);
797672c8852SJeremy Kerr 	if (!pkt)
798672c8852SJeremy Kerr 		goto out_complete;
799672c8852SJeremy Kerr 
80055fb90beSAndrew Jeffery 	mctp_astlpc_lpc_read(astlpc, mctp_pktbuf_hdr(pkt),
8017cd72f14SAndrew Jeffery 			     astlpc->layout.rx.offset + 4, len);
802672c8852SJeremy Kerr 
803672c8852SJeremy Kerr 	mctp_bus_rx(&astlpc->binding, pkt);
804672c8852SJeremy Kerr 
805672c8852SJeremy Kerr out_complete:
806672c8852SJeremy Kerr 	mctp_astlpc_kcs_send(astlpc, 0x2);
807672c8852SJeremy Kerr }
808672c8852SJeremy Kerr 
809672c8852SJeremy Kerr static void mctp_astlpc_tx_complete(struct mctp_binding_astlpc *astlpc)
810672c8852SJeremy Kerr {
811672c8852SJeremy Kerr 	mctp_binding_set_tx_enabled(&astlpc->binding, true);
812672c8852SJeremy Kerr }
813672c8852SJeremy Kerr 
8144e8264b7SAndrew Jeffery static int mctp_astlpc_finalise_channel(struct mctp_binding_astlpc *astlpc)
8154e8264b7SAndrew Jeffery {
8163a540664SAndrew Jeffery 	struct mctp_astlpc_layout layout;
8174e8264b7SAndrew Jeffery 	uint16_t negotiated;
8184e8264b7SAndrew Jeffery 	int rc;
8194e8264b7SAndrew Jeffery 
8204e8264b7SAndrew Jeffery 	rc = mctp_astlpc_lpc_read(astlpc, &negotiated,
8214e8264b7SAndrew Jeffery 				  offsetof(struct mctp_lpcmap_hdr,
8224e8264b7SAndrew Jeffery 					   negotiated_ver),
8234e8264b7SAndrew Jeffery 				  sizeof(negotiated));
8244e8264b7SAndrew Jeffery 	if (rc < 0)
8254e8264b7SAndrew Jeffery 		return rc;
8264e8264b7SAndrew Jeffery 
8274e8264b7SAndrew Jeffery 	negotiated = be16toh(negotiated);
8284e8264b7SAndrew Jeffery 
8294e8264b7SAndrew Jeffery 	if (negotiated == ASTLPC_VER_BAD || negotiated < ASTLPC_VER_MIN ||
8304e8264b7SAndrew Jeffery 	    negotiated > ASTLPC_VER_CUR) {
8314e8264b7SAndrew Jeffery 		astlpc_prerr(astlpc, "Failed to negotiate version, got: %u\n",
8324e8264b7SAndrew Jeffery 			     negotiated);
8334e8264b7SAndrew Jeffery 		return -EINVAL;
8344e8264b7SAndrew Jeffery 	}
8354e8264b7SAndrew Jeffery 
8364e8264b7SAndrew Jeffery 	astlpc->version = negotiated;
8374e8264b7SAndrew Jeffery 
8383a540664SAndrew Jeffery 	rc = mctp_astlpc_layout_read(astlpc, &layout);
8393a540664SAndrew Jeffery 	if (rc < 0)
8403a540664SAndrew Jeffery 		return rc;
8413a540664SAndrew Jeffery 
8423a540664SAndrew Jeffery 	if (!mctp_astlpc_layout_validate(&layout)) {
8433a540664SAndrew Jeffery 		mctp_prerr("BMC proposed invalid buffer parameters");
8443a540664SAndrew Jeffery 		return -EINVAL;
8453a540664SAndrew Jeffery 	}
8463a540664SAndrew Jeffery 
8473a540664SAndrew Jeffery 	astlpc->layout = layout;
8483a540664SAndrew Jeffery 
8493a540664SAndrew Jeffery 	if (negotiated >= 2)
8503a540664SAndrew Jeffery 		astlpc->binding.pkt_size =
8513a540664SAndrew Jeffery 			ASTLPC_BODY_SIZE(astlpc->layout.tx.size);
8523a540664SAndrew Jeffery 
8534e8264b7SAndrew Jeffery 	return 0;
8544e8264b7SAndrew Jeffery }
8554e8264b7SAndrew Jeffery 
8567cd72f14SAndrew Jeffery static int mctp_astlpc_update_channel(struct mctp_binding_astlpc *astlpc,
8577cd72f14SAndrew Jeffery 				      uint8_t status)
8587cd72f14SAndrew Jeffery {
8597cd72f14SAndrew Jeffery 	uint8_t updated;
8607cd72f14SAndrew Jeffery 	int rc = 0;
8617cd72f14SAndrew Jeffery 
8627cd72f14SAndrew Jeffery 	assert(astlpc->mode == MCTP_BINDING_ASTLPC_MODE_HOST);
8637cd72f14SAndrew Jeffery 
8647cd72f14SAndrew Jeffery 	updated = astlpc->kcs_status ^ status;
8657cd72f14SAndrew Jeffery 
866d0f5da0dSAndrew Jeffery 	astlpc_prdebug(astlpc, "%s: status: 0x%x, update: 0x%x", __func__,
867d0f5da0dSAndrew Jeffery 		       status, updated);
868d0f5da0dSAndrew Jeffery 
8697cd72f14SAndrew Jeffery 	if (updated & KCS_STATUS_BMC_READY) {
870d0f5da0dSAndrew Jeffery 		if (status & KCS_STATUS_BMC_READY) {
871d0f5da0dSAndrew Jeffery 			astlpc->kcs_status = status;
872d0f5da0dSAndrew Jeffery 			return astlpc->binding.start(&astlpc->binding);
873d0f5da0dSAndrew Jeffery 		} else {
8747cd72f14SAndrew Jeffery 			mctp_binding_set_tx_enabled(&astlpc->binding, false);
8757cd72f14SAndrew Jeffery 		}
876d0f5da0dSAndrew Jeffery 	}
8777cd72f14SAndrew Jeffery 
8784e8264b7SAndrew Jeffery 	if (astlpc->version == 0 || updated & KCS_STATUS_CHANNEL_ACTIVE) {
8794e8264b7SAndrew Jeffery 		bool enable;
8804e8264b7SAndrew Jeffery 
8814e8264b7SAndrew Jeffery 		rc = mctp_astlpc_finalise_channel(astlpc);
8824e8264b7SAndrew Jeffery 		enable = (status & KCS_STATUS_CHANNEL_ACTIVE) && rc == 0;
8834e8264b7SAndrew Jeffery 
8844e8264b7SAndrew Jeffery 		mctp_binding_set_tx_enabled(&astlpc->binding, enable);
8854e8264b7SAndrew Jeffery 	}
8867cd72f14SAndrew Jeffery 
8877cd72f14SAndrew Jeffery 	astlpc->kcs_status = status;
8887cd72f14SAndrew Jeffery 
8897cd72f14SAndrew Jeffery 	return rc;
8907cd72f14SAndrew Jeffery }
8917cd72f14SAndrew Jeffery 
892672c8852SJeremy Kerr int mctp_astlpc_poll(struct mctp_binding_astlpc *astlpc)
893672c8852SJeremy Kerr {
894bc53d35aSJeremy Kerr 	uint8_t status, data;
895672c8852SJeremy Kerr 	int rc;
896672c8852SJeremy Kerr 
897f13cb971SAndrew Jeffery 	rc = mctp_astlpc_kcs_read(astlpc, MCTP_ASTLPC_KCS_REG_STATUS, &status);
898bc53d35aSJeremy Kerr 	if (rc) {
8999101a2abSAndrew Jeffery 		astlpc_prwarn(astlpc, "KCS read error");
900672c8852SJeremy Kerr 		return -1;
901672c8852SJeremy Kerr 	}
902672c8852SJeremy Kerr 
9039101a2abSAndrew Jeffery 	astlpc_prdebug(astlpc, "%s: status: 0x%hhx", __func__, status);
904edfe383fSAndrew Jeffery 
9057cd72f14SAndrew Jeffery 	if (!mctp_astlpc_kcs_read_ready(astlpc, status))
906672c8852SJeremy Kerr 		return 0;
907672c8852SJeremy Kerr 
908f13cb971SAndrew Jeffery 	rc = mctp_astlpc_kcs_read(astlpc, MCTP_ASTLPC_KCS_REG_DATA, &data);
909bc53d35aSJeremy Kerr 	if (rc) {
9109101a2abSAndrew Jeffery 		astlpc_prwarn(astlpc, "KCS data read error");
911bc53d35aSJeremy Kerr 		return -1;
912bc53d35aSJeremy Kerr 	}
913bc53d35aSJeremy Kerr 
9149101a2abSAndrew Jeffery 	astlpc_prdebug(astlpc, "%s: data: 0x%hhx", __func__, data);
915edfe383fSAndrew Jeffery 
916672c8852SJeremy Kerr 	switch (data) {
917672c8852SJeremy Kerr 	case 0x0:
918672c8852SJeremy Kerr 		mctp_astlpc_init_channel(astlpc);
919672c8852SJeremy Kerr 		break;
920672c8852SJeremy Kerr 	case 0x1:
921672c8852SJeremy Kerr 		mctp_astlpc_rx_start(astlpc);
922672c8852SJeremy Kerr 		break;
923672c8852SJeremy Kerr 	case 0x2:
924672c8852SJeremy Kerr 		mctp_astlpc_tx_complete(astlpc);
925672c8852SJeremy Kerr 		break;
9261a4b55acSJeremy Kerr 	case 0xff:
9277cd72f14SAndrew Jeffery 		/* No responsibilities for the BMC on 0xff */
928d0f5da0dSAndrew Jeffery 		if (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_HOST) {
929d0f5da0dSAndrew Jeffery 			rc = mctp_astlpc_update_channel(astlpc, status);
930d0f5da0dSAndrew Jeffery 			if (rc < 0)
931d0f5da0dSAndrew Jeffery 				return rc;
932d0f5da0dSAndrew Jeffery 		}
933d0f5da0dSAndrew Jeffery 		break;
934672c8852SJeremy Kerr 	default:
9359101a2abSAndrew Jeffery 		astlpc_prwarn(astlpc, "unknown message 0x%x", data);
936672c8852SJeremy Kerr 	}
937d0f5da0dSAndrew Jeffery 
938d0f5da0dSAndrew Jeffery 	/* Handle silent loss of bmc-ready */
939d0f5da0dSAndrew Jeffery 	if (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_HOST) {
940d0f5da0dSAndrew Jeffery 		if (!(status & KCS_STATUS_BMC_READY && data == 0xff))
941d0f5da0dSAndrew Jeffery 			return mctp_astlpc_update_channel(astlpc, status);
942d0f5da0dSAndrew Jeffery 	}
943d0f5da0dSAndrew Jeffery 
944d0f5da0dSAndrew Jeffery 	return rc;
945672c8852SJeremy Kerr }
946672c8852SJeremy Kerr 
947bc53d35aSJeremy Kerr /* allocate and basic initialisation */
9487cd72f14SAndrew Jeffery static struct mctp_binding_astlpc *__mctp_astlpc_init(uint8_t mode,
9497cd72f14SAndrew Jeffery 						      uint32_t mtu)
950bc53d35aSJeremy Kerr {
951bc53d35aSJeremy Kerr 	struct mctp_binding_astlpc *astlpc;
952bc53d35aSJeremy Kerr 
9537cd72f14SAndrew Jeffery 	assert((mode == MCTP_BINDING_ASTLPC_MODE_BMC) ||
9547cd72f14SAndrew Jeffery 	       (mode == MCTP_BINDING_ASTLPC_MODE_HOST));
9557cd72f14SAndrew Jeffery 
956bc53d35aSJeremy Kerr 	astlpc = __mctp_alloc(sizeof(*astlpc));
9577cd72f14SAndrew Jeffery 	if (!astlpc)
9587cd72f14SAndrew Jeffery 		return NULL;
9597cd72f14SAndrew Jeffery 
960bc53d35aSJeremy Kerr 	memset(astlpc, 0, sizeof(*astlpc));
9617cd72f14SAndrew Jeffery 	astlpc->mode = mode;
9627cd72f14SAndrew Jeffery 	astlpc->lpc_map = NULL;
963a9368980SAndrew Jeffery 	astlpc->requested_mtu = mtu;
964bc53d35aSJeremy Kerr 	astlpc->binding.name = "astlpc";
965bc53d35aSJeremy Kerr 	astlpc->binding.version = 1;
9667cd72f14SAndrew Jeffery 	astlpc->binding.pkt_size = MCTP_PACKET_SIZE(mtu);
967bc53d35aSJeremy Kerr 	astlpc->binding.pkt_pad = 0;
9687cd72f14SAndrew Jeffery 	astlpc->binding.tx = mctp_binding_astlpc_tx;
9697cd72f14SAndrew Jeffery 	if (mode == MCTP_BINDING_ASTLPC_MODE_BMC)
9707cd72f14SAndrew Jeffery 		astlpc->binding.start = mctp_binding_astlpc_start_bmc;
9717cd72f14SAndrew Jeffery 	else if (mode == MCTP_BINDING_ASTLPC_MODE_HOST)
9727cd72f14SAndrew Jeffery 		astlpc->binding.start = mctp_binding_astlpc_start_host;
9737cd72f14SAndrew Jeffery 	else {
9749101a2abSAndrew Jeffery 		astlpc_prerr(astlpc, "%s: Invalid mode: %d\n", __func__, mode);
9757cd72f14SAndrew Jeffery 		__mctp_free(astlpc);
9767cd72f14SAndrew Jeffery 		return NULL;
9777cd72f14SAndrew Jeffery 	}
978bc53d35aSJeremy Kerr 
979bc53d35aSJeremy Kerr 	return astlpc;
980bc53d35aSJeremy Kerr }
981bc53d35aSJeremy Kerr 
9823b36d17cSJeremy Kerr struct mctp_binding *mctp_binding_astlpc_core(struct mctp_binding_astlpc *b)
9833b36d17cSJeremy Kerr {
9843b36d17cSJeremy Kerr 	return &b->binding;
9853b36d17cSJeremy Kerr }
9863b36d17cSJeremy Kerr 
9877cd72f14SAndrew Jeffery struct mctp_binding_astlpc *
9887cd72f14SAndrew Jeffery mctp_astlpc_init(uint8_t mode, uint32_t mtu, void *lpc_map,
9897cd72f14SAndrew Jeffery 		 const struct mctp_binding_astlpc_ops *ops, void *ops_data)
990bc53d35aSJeremy Kerr {
991bc53d35aSJeremy Kerr 	struct mctp_binding_astlpc *astlpc;
992bc53d35aSJeremy Kerr 
9937cd72f14SAndrew Jeffery 	if (!(mode == MCTP_BINDING_ASTLPC_MODE_BMC ||
9947cd72f14SAndrew Jeffery 	      mode == MCTP_BINDING_ASTLPC_MODE_HOST)) {
9957cd72f14SAndrew Jeffery 		mctp_prerr("Unknown binding mode: %u", mode);
9967cd72f14SAndrew Jeffery 		return NULL;
9977cd72f14SAndrew Jeffery 	}
9987cd72f14SAndrew Jeffery 
9997cd72f14SAndrew Jeffery 	astlpc = __mctp_astlpc_init(mode, mtu);
1000bc53d35aSJeremy Kerr 	if (!astlpc)
1001bc53d35aSJeremy Kerr 		return NULL;
1002bc53d35aSJeremy Kerr 
1003bc53d35aSJeremy Kerr 	memcpy(&astlpc->ops, ops, sizeof(astlpc->ops));
1004bc53d35aSJeremy Kerr 	astlpc->ops_data = ops_data;
1005bc53d35aSJeremy Kerr 	astlpc->lpc_map = lpc_map;
10067cd72f14SAndrew Jeffery 	astlpc->mode = mode;
1007bc53d35aSJeremy Kerr 
1008bc53d35aSJeremy Kerr 	return astlpc;
1009bc53d35aSJeremy Kerr }
1010bc53d35aSJeremy Kerr 
10117cd72f14SAndrew Jeffery struct mctp_binding_astlpc *
10127cd72f14SAndrew Jeffery mctp_astlpc_init_ops(const struct mctp_binding_astlpc_ops *ops, void *ops_data,
10137cd72f14SAndrew Jeffery 		     void *lpc_map)
10147cd72f14SAndrew Jeffery {
10157cd72f14SAndrew Jeffery 	return mctp_astlpc_init(MCTP_BINDING_ASTLPC_MODE_BMC, MCTP_BTU, lpc_map,
10167cd72f14SAndrew Jeffery 				ops, ops_data);
10177cd72f14SAndrew Jeffery }
10187cd72f14SAndrew Jeffery 
10194663f67cSAndrew Jeffery void mctp_astlpc_destroy(struct mctp_binding_astlpc *astlpc)
10204663f67cSAndrew Jeffery {
1021d0f5da0dSAndrew Jeffery 	/* Clear channel-active and bmc-ready */
1022d0f5da0dSAndrew Jeffery 	if (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_BMC)
1023d0f5da0dSAndrew Jeffery 		mctp_astlpc_kcs_set_status(astlpc, KCS_STATUS_OBF);
10244663f67cSAndrew Jeffery 	__mctp_free(astlpc);
10254663f67cSAndrew Jeffery }
10264663f67cSAndrew Jeffery 
1027b214c643SJeremy Kerr #ifdef MCTP_HAVE_FILEIO
102855fb90beSAndrew Jeffery 
1029bc53d35aSJeremy Kerr static int mctp_astlpc_init_fileio_lpc(struct mctp_binding_astlpc *astlpc)
1030672c8852SJeremy Kerr {
1031672c8852SJeremy Kerr 	struct aspeed_lpc_ctrl_mapping map = {
1032672c8852SJeremy Kerr 		.window_type = ASPEED_LPC_CTRL_WINDOW_MEMORY,
1033672c8852SJeremy Kerr 		.window_id = 0, /* There's only one */
1034672c8852SJeremy Kerr 		.flags = 0,
1035672c8852SJeremy Kerr 		.addr = 0,
1036672c8852SJeremy Kerr 		.offset = 0,
1037672c8852SJeremy Kerr 		.size = 0
1038672c8852SJeremy Kerr 	};
1039979c6a16SAndrew Jeffery 	void *lpc_map_base;
1040672c8852SJeremy Kerr 	int fd, rc;
1041672c8852SJeremy Kerr 
1042672c8852SJeremy Kerr 	fd = open(lpc_path, O_RDWR | O_SYNC);
1043672c8852SJeremy Kerr 	if (fd < 0) {
10449101a2abSAndrew Jeffery 		astlpc_prwarn(astlpc, "LPC open (%s) failed", lpc_path);
1045672c8852SJeremy Kerr 		return -1;
1046672c8852SJeremy Kerr 	}
1047672c8852SJeremy Kerr 
1048672c8852SJeremy Kerr 	rc = ioctl(fd, ASPEED_LPC_CTRL_IOCTL_GET_SIZE, &map);
1049672c8852SJeremy Kerr 	if (rc) {
10509101a2abSAndrew Jeffery 		astlpc_prwarn(astlpc, "LPC GET_SIZE failed");
1051672c8852SJeremy Kerr 		close(fd);
1052672c8852SJeremy Kerr 		return -1;
1053672c8852SJeremy Kerr 	}
1054672c8852SJeremy Kerr 
1055979c6a16SAndrew Jeffery 	lpc_map_base =
1056979c6a16SAndrew Jeffery 		mmap(NULL, map.size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
1057979c6a16SAndrew Jeffery 	if (lpc_map_base == MAP_FAILED) {
10589101a2abSAndrew Jeffery 		astlpc_prwarn(astlpc, "LPC mmap failed");
1059672c8852SJeremy Kerr 		rc = -1;
1060672c8852SJeremy Kerr 	} else {
1061979c6a16SAndrew Jeffery 		astlpc->lpc_map = lpc_map_base + map.size - LPC_WIN_SIZE;
1062672c8852SJeremy Kerr 	}
1063672c8852SJeremy Kerr 
1064672c8852SJeremy Kerr 	close(fd);
1065672c8852SJeremy Kerr 
1066672c8852SJeremy Kerr 	return rc;
1067672c8852SJeremy Kerr }
1068672c8852SJeremy Kerr 
1069bc53d35aSJeremy Kerr static int mctp_astlpc_init_fileio_kcs(struct mctp_binding_astlpc *astlpc)
1070672c8852SJeremy Kerr {
1071672c8852SJeremy Kerr 	astlpc->kcs_fd = open(kcs_path, O_RDWR);
1072672c8852SJeremy Kerr 	if (astlpc->kcs_fd < 0)
1073672c8852SJeremy Kerr 		return -1;
1074672c8852SJeremy Kerr 
1075672c8852SJeremy Kerr 	return 0;
1076672c8852SJeremy Kerr }
1077672c8852SJeremy Kerr 
1078bc53d35aSJeremy Kerr static int __mctp_astlpc_fileio_kcs_read(void *arg,
1079bc53d35aSJeremy Kerr 		enum mctp_binding_astlpc_kcs_reg reg, uint8_t *val)
1080bc53d35aSJeremy Kerr {
1081bc53d35aSJeremy Kerr 	struct mctp_binding_astlpc *astlpc = arg;
1082bc53d35aSJeremy Kerr 	off_t offset = reg;
1083bc53d35aSJeremy Kerr 	int rc;
1084bc53d35aSJeremy Kerr 
1085bc53d35aSJeremy Kerr 	rc = pread(astlpc->kcs_fd, val, 1, offset);
1086bc53d35aSJeremy Kerr 
1087bc53d35aSJeremy Kerr 	return rc == 1 ? 0 : -1;
1088bc53d35aSJeremy Kerr }
1089bc53d35aSJeremy Kerr 
1090bc53d35aSJeremy Kerr static int __mctp_astlpc_fileio_kcs_write(void *arg,
1091bc53d35aSJeremy Kerr 		enum mctp_binding_astlpc_kcs_reg reg, uint8_t val)
1092bc53d35aSJeremy Kerr {
1093bc53d35aSJeremy Kerr 	struct mctp_binding_astlpc *astlpc = arg;
1094bc53d35aSJeremy Kerr 	off_t offset = reg;
1095bc53d35aSJeremy Kerr 	int rc;
1096bc53d35aSJeremy Kerr 
1097bc53d35aSJeremy Kerr 	rc = pwrite(astlpc->kcs_fd, &val, 1, offset);
1098bc53d35aSJeremy Kerr 
1099bc53d35aSJeremy Kerr 	return rc == 1 ? 0 : -1;
1100bc53d35aSJeremy Kerr }
1101bc53d35aSJeremy Kerr 
1102bc53d35aSJeremy Kerr int mctp_astlpc_get_fd(struct mctp_binding_astlpc *astlpc)
1103bc53d35aSJeremy Kerr {
1104bc53d35aSJeremy Kerr 	return astlpc->kcs_fd;
1105bc53d35aSJeremy Kerr }
1106bc53d35aSJeremy Kerr 
1107bc53d35aSJeremy Kerr struct mctp_binding_astlpc *mctp_astlpc_init_fileio(void)
1108672c8852SJeremy Kerr {
1109672c8852SJeremy Kerr 	struct mctp_binding_astlpc *astlpc;
1110672c8852SJeremy Kerr 	int rc;
1111672c8852SJeremy Kerr 
11127cd72f14SAndrew Jeffery 	/*
11137cd72f14SAndrew Jeffery 	 * If we're doing file IO then we're very likely not running
11148877c460SAndrew Jeffery 	 * freestanding, so lets assume that we're on the BMC side.
11158877c460SAndrew Jeffery 	 *
11168877c460SAndrew Jeffery 	 * Requesting an MTU of 0 requests the largest possible MTU, whatever
11178877c460SAndrew Jeffery 	 * value that might take.
11187cd72f14SAndrew Jeffery 	 */
11198877c460SAndrew Jeffery 	astlpc = __mctp_astlpc_init(MCTP_BINDING_ASTLPC_MODE_BMC, 0);
1120bc53d35aSJeremy Kerr 	if (!astlpc)
1121bc53d35aSJeremy Kerr 		return NULL;
1122672c8852SJeremy Kerr 
1123bc53d35aSJeremy Kerr 	/* Set internal operations for kcs. We use direct accesses to the lpc
1124bc53d35aSJeremy Kerr 	 * map area */
1125bc53d35aSJeremy Kerr 	astlpc->ops.kcs_read = __mctp_astlpc_fileio_kcs_read;
1126bc53d35aSJeremy Kerr 	astlpc->ops.kcs_write = __mctp_astlpc_fileio_kcs_write;
1127bc53d35aSJeremy Kerr 	astlpc->ops_data = astlpc;
1128bc53d35aSJeremy Kerr 
1129bc53d35aSJeremy Kerr 	rc = mctp_astlpc_init_fileio_lpc(astlpc);
1130672c8852SJeremy Kerr 	if (rc) {
1131672c8852SJeremy Kerr 		free(astlpc);
1132672c8852SJeremy Kerr 		return NULL;
1133672c8852SJeremy Kerr 	}
1134672c8852SJeremy Kerr 
1135bc53d35aSJeremy Kerr 	rc = mctp_astlpc_init_fileio_kcs(astlpc);
1136672c8852SJeremy Kerr 	if (rc) {
1137672c8852SJeremy Kerr 		free(astlpc);
1138672c8852SJeremy Kerr 		return NULL;
1139672c8852SJeremy Kerr 	}
1140672c8852SJeremy Kerr 
1141672c8852SJeremy Kerr 	return astlpc;
1142672c8852SJeremy Kerr }
114392a10a6bSJeremy Kerr #else
114492a10a6bSJeremy Kerr struct mctp_binding_astlpc * __attribute__((const))
114592a10a6bSJeremy Kerr 	mctp_astlpc_init_fileio(void)
114692a10a6bSJeremy Kerr {
11479101a2abSAndrew Jeffery 	astlpc_prerr(astlpc, "Missing support for file IO");
114892a10a6bSJeremy Kerr 	return NULL;
114992a10a6bSJeremy Kerr }
1150672c8852SJeremy Kerr 
115192a10a6bSJeremy Kerr int __attribute__((const)) mctp_astlpc_get_fd(
115292a10a6bSJeremy Kerr 		struct mctp_binding_astlpc *astlpc __attribute__((unused)))
115392a10a6bSJeremy Kerr {
11549101a2abSAndrew Jeffery 	astlpc_prerr(astlpc, "Missing support for file IO");
115592a10a6bSJeremy Kerr 	return -1;
115692a10a6bSJeremy Kerr }
115792a10a6bSJeremy Kerr #endif
1158