xref: /openbmc/libmctp/astlpc.c (revision 3a540664c5fbf8cabeca0c1a1af27b1b979eb253)
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"
26672c8852SJeremy Kerr 
27b214c643SJeremy Kerr #ifdef MCTP_HAVE_FILEIO
2892a10a6bSJeremy Kerr 
29c6f676d1SJeremy Kerr #include <unistd.h>
3092a10a6bSJeremy Kerr #include <fcntl.h>
3192a10a6bSJeremy Kerr #include <sys/ioctl.h>
3292a10a6bSJeremy Kerr #include <sys/mman.h>
3392a10a6bSJeremy Kerr #include <linux/aspeed-lpc-ctrl.h>
3492a10a6bSJeremy Kerr 
3592a10a6bSJeremy Kerr /* kernel interface */
3692a10a6bSJeremy Kerr static const char *kcs_path = "/dev/mctp0";
3792a10a6bSJeremy Kerr static const char *lpc_path = "/dev/aspeed-lpc-ctrl";
3892a10a6bSJeremy Kerr 
3992a10a6bSJeremy Kerr #endif
4092a10a6bSJeremy Kerr 
417cd72f14SAndrew Jeffery struct mctp_astlpc_buffer {
427cd72f14SAndrew Jeffery 	uint32_t offset;
437cd72f14SAndrew Jeffery 	uint32_t size;
447cd72f14SAndrew Jeffery };
457cd72f14SAndrew Jeffery 
467cd72f14SAndrew Jeffery struct mctp_astlpc_layout {
477cd72f14SAndrew Jeffery 	struct mctp_astlpc_buffer rx;
487cd72f14SAndrew Jeffery 	struct mctp_astlpc_buffer tx;
497cd72f14SAndrew Jeffery };
507cd72f14SAndrew Jeffery 
51672c8852SJeremy Kerr struct mctp_binding_astlpc {
52672c8852SJeremy Kerr 	struct mctp_binding	binding;
53bc53d35aSJeremy Kerr 
54672c8852SJeremy Kerr 	void *lpc_map;
557cd72f14SAndrew Jeffery 	struct mctp_astlpc_layout layout;
567cd72f14SAndrew Jeffery 
577cd72f14SAndrew Jeffery 	uint8_t mode;
584e8264b7SAndrew Jeffery 	uint16_t version;
59bc53d35aSJeremy Kerr 
60bc53d35aSJeremy Kerr 	/* direct ops data */
61bc53d35aSJeremy Kerr 	struct mctp_binding_astlpc_ops ops;
62bc53d35aSJeremy Kerr 	void *ops_data;
63bc53d35aSJeremy Kerr 
64bc53d35aSJeremy Kerr 	/* fileio ops data */
65672c8852SJeremy Kerr 	int kcs_fd;
66672c8852SJeremy Kerr 	uint8_t kcs_status;
67672c8852SJeremy Kerr 
68672c8852SJeremy Kerr 	bool			running;
69672c8852SJeremy Kerr };
70672c8852SJeremy Kerr 
71672c8852SJeremy Kerr #define binding_to_astlpc(b) \
72672c8852SJeremy Kerr 	container_of(b, struct mctp_binding_astlpc, binding)
73672c8852SJeremy Kerr 
749101a2abSAndrew Jeffery #define astlpc_prlog(ctx, lvl, fmt, ...)                                       \
759101a2abSAndrew Jeffery 	do {                                                                   \
769101a2abSAndrew Jeffery 		bool __bmc = ((ctx)->mode == MCTP_BINDING_ASTLPC_MODE_BMC);    \
779101a2abSAndrew Jeffery 		mctp_prlog(lvl, pr_fmt("%s: " fmt), __bmc ? "bmc" : "host",    \
789101a2abSAndrew Jeffery 			   ##__VA_ARGS__);                                     \
799101a2abSAndrew Jeffery 	} while (0)
809101a2abSAndrew Jeffery 
819101a2abSAndrew Jeffery #define astlpc_prerr(ctx, fmt, ...)                                            \
829101a2abSAndrew Jeffery 	astlpc_prlog(ctx, MCTP_LOG_ERR, fmt, ##__VA_ARGS__)
839101a2abSAndrew Jeffery #define astlpc_prwarn(ctx, fmt, ...)                                           \
849101a2abSAndrew Jeffery 	astlpc_prlog(ctx, MCTP_LOG_WARNING, fmt, ##__VA_ARGS__)
859101a2abSAndrew Jeffery #define astlpc_prinfo(ctx, fmt, ...)                                           \
869101a2abSAndrew Jeffery 	astlpc_prlog(ctx, MCTP_LOG_INFO, fmt, ##__VA_ARGS__)
879101a2abSAndrew Jeffery #define astlpc_prdebug(ctx, fmt, ...)                                          \
889101a2abSAndrew Jeffery 	astlpc_prlog(ctx, MCTP_LOG_DEBUG, fmt, ##__VA_ARGS__)
899101a2abSAndrew Jeffery 
907cd72f14SAndrew Jeffery /* clang-format off */
917cd72f14SAndrew Jeffery #define ASTLPC_MCTP_MAGIC	0x4d435450
924e8264b7SAndrew Jeffery #define ASTLPC_VER_BAD	0
937cd72f14SAndrew Jeffery #define ASTLPC_VER_MIN	1
947cd72f14SAndrew Jeffery 
95*3a540664SAndrew Jeffery /* Support testing of new binding protocols */
96*3a540664SAndrew Jeffery #ifndef ASTLPC_VER_CUR
97*3a540664SAndrew Jeffery #define ASTLPC_VER_CUR	1
98*3a540664SAndrew Jeffery #endif
99*3a540664SAndrew Jeffery 
100*3a540664SAndrew Jeffery #define ASTLPC_PACKET_SIZE(sz)	(4 + (sz))
1017cd72f14SAndrew Jeffery #define ASTLPC_BODY_SIZE(sz)	((sz) - 4)
1027cd72f14SAndrew Jeffery /* clang-format on */
103672c8852SJeremy Kerr 
104672c8852SJeremy Kerr struct mctp_lpcmap_hdr {
105672c8852SJeremy Kerr 	uint32_t magic;
106672c8852SJeremy Kerr 
107672c8852SJeremy Kerr 	uint16_t bmc_ver_min;
108672c8852SJeremy Kerr 	uint16_t bmc_ver_cur;
109672c8852SJeremy Kerr 	uint16_t host_ver_min;
110672c8852SJeremy Kerr 	uint16_t host_ver_cur;
111672c8852SJeremy Kerr 	uint16_t negotiated_ver;
112672c8852SJeremy Kerr 	uint16_t pad0;
113672c8852SJeremy Kerr 
114*3a540664SAndrew Jeffery 	struct {
115672c8852SJeremy Kerr 		uint32_t rx_offset;
116672c8852SJeremy Kerr 		uint32_t rx_size;
117672c8852SJeremy Kerr 		uint32_t tx_offset;
118672c8852SJeremy Kerr 		uint32_t tx_size;
119*3a540664SAndrew Jeffery 	} layout;
120672c8852SJeremy Kerr } __attribute__((packed));
121672c8852SJeremy Kerr 
122*3a540664SAndrew Jeffery static const uint32_t control_size = 0x100;
123672c8852SJeremy Kerr 
124672c8852SJeremy Kerr #define LPC_WIN_SIZE                (1 * 1024 * 1024)
125672c8852SJeremy Kerr 
126672c8852SJeremy Kerr #define KCS_STATUS_BMC_READY		0x80
127672c8852SJeremy Kerr #define KCS_STATUS_CHANNEL_ACTIVE	0x40
128672c8852SJeremy Kerr #define KCS_STATUS_IBF			0x02
129672c8852SJeremy Kerr #define KCS_STATUS_OBF			0x01
130672c8852SJeremy Kerr 
131*3a540664SAndrew Jeffery #define MIN(a, b)                                                              \
132*3a540664SAndrew Jeffery 	({                                                                     \
133*3a540664SAndrew Jeffery 		typeof(a) _a = a;                                              \
134*3a540664SAndrew Jeffery 		typeof(b) _b = b;                                              \
135*3a540664SAndrew Jeffery 		_a < _b ? _a : _b;                                             \
136*3a540664SAndrew Jeffery 	})
137*3a540664SAndrew Jeffery 
138*3a540664SAndrew Jeffery #define MAX(a, b)                                                              \
139*3a540664SAndrew Jeffery 	({                                                                     \
140*3a540664SAndrew Jeffery 		typeof(a) _a = a;                                              \
141*3a540664SAndrew Jeffery 		typeof(b) _b = b;                                              \
142*3a540664SAndrew Jeffery 		_a > _b ? _a : _b;                                             \
143*3a540664SAndrew Jeffery 	})
144*3a540664SAndrew Jeffery 
145f13cb971SAndrew Jeffery static inline int mctp_astlpc_kcs_write(struct mctp_binding_astlpc *astlpc,
146f13cb971SAndrew Jeffery 					enum mctp_binding_astlpc_kcs_reg reg,
147f13cb971SAndrew Jeffery 					uint8_t val)
148f13cb971SAndrew Jeffery {
149f13cb971SAndrew Jeffery 	return astlpc->ops.kcs_write(astlpc->ops_data, reg, val);
150f13cb971SAndrew Jeffery }
151f13cb971SAndrew Jeffery 
152f13cb971SAndrew Jeffery static inline int mctp_astlpc_kcs_read(struct mctp_binding_astlpc *astlpc,
153f13cb971SAndrew Jeffery 				       enum mctp_binding_astlpc_kcs_reg reg,
154f13cb971SAndrew Jeffery 				       uint8_t *val)
155f13cb971SAndrew Jeffery {
156f13cb971SAndrew Jeffery 	return astlpc->ops.kcs_read(astlpc->ops_data, reg, val);
157f13cb971SAndrew Jeffery }
158f13cb971SAndrew Jeffery 
15955fb90beSAndrew Jeffery static inline int mctp_astlpc_lpc_write(struct mctp_binding_astlpc *astlpc,
16055fb90beSAndrew Jeffery 					const void *buf, long offset,
16155fb90beSAndrew Jeffery 					size_t len)
162bc53d35aSJeremy Kerr {
16355fb90beSAndrew Jeffery 	astlpc_prdebug(astlpc, "%s: %zu bytes to 0x%lx", __func__, len, offset);
16455fb90beSAndrew Jeffery 
16555fb90beSAndrew Jeffery 	assert(offset >= 0);
16655fb90beSAndrew Jeffery 
16755fb90beSAndrew Jeffery 	/* Indirect access */
16855fb90beSAndrew Jeffery 	if (astlpc->ops.lpc_write) {
16955fb90beSAndrew Jeffery 		void *data = astlpc->ops_data;
17055fb90beSAndrew Jeffery 
17155fb90beSAndrew Jeffery 		return astlpc->ops.lpc_write(data, buf, offset, len);
17255fb90beSAndrew Jeffery 	}
17355fb90beSAndrew Jeffery 
17455fb90beSAndrew Jeffery 	/* Direct mapping */
17555fb90beSAndrew Jeffery 	assert(astlpc->lpc_map);
17655fb90beSAndrew Jeffery 	memcpy(&((char *)astlpc->lpc_map)[offset], buf, len);
17755fb90beSAndrew Jeffery 
17855fb90beSAndrew Jeffery 	return 0;
17955fb90beSAndrew Jeffery }
18055fb90beSAndrew Jeffery 
18155fb90beSAndrew Jeffery static inline int mctp_astlpc_lpc_read(struct mctp_binding_astlpc *astlpc,
18255fb90beSAndrew Jeffery 				       void *buf, long offset, size_t len)
18355fb90beSAndrew Jeffery {
18455fb90beSAndrew Jeffery 	astlpc_prdebug(astlpc, "%s: %zu bytes from 0x%lx", __func__, len,
18555fb90beSAndrew Jeffery 		       offset);
18655fb90beSAndrew Jeffery 
18755fb90beSAndrew Jeffery 	assert(offset >= 0);
18855fb90beSAndrew Jeffery 
18955fb90beSAndrew Jeffery 	/* Indirect access */
19055fb90beSAndrew Jeffery 	if (astlpc->ops.lpc_read) {
19155fb90beSAndrew Jeffery 		void *data = astlpc->ops_data;
19255fb90beSAndrew Jeffery 
19355fb90beSAndrew Jeffery 		return astlpc->ops.lpc_read(data, buf, offset, len);
19455fb90beSAndrew Jeffery 	}
19555fb90beSAndrew Jeffery 
19655fb90beSAndrew Jeffery 	/* Direct mapping */
19755fb90beSAndrew Jeffery 	assert(astlpc->lpc_map);
19855fb90beSAndrew Jeffery 	memcpy(buf, &((char *)astlpc->lpc_map)[offset], len);
19955fb90beSAndrew Jeffery 
20055fb90beSAndrew Jeffery 	return 0;
201bc53d35aSJeremy Kerr }
202bc53d35aSJeremy Kerr 
203d0f5da0dSAndrew Jeffery static int mctp_astlpc_kcs_set_status(struct mctp_binding_astlpc *astlpc,
204d0f5da0dSAndrew Jeffery 				      uint8_t status)
205d0f5da0dSAndrew Jeffery {
206d0f5da0dSAndrew Jeffery 	uint8_t data;
207d0f5da0dSAndrew Jeffery 	int rc;
208d0f5da0dSAndrew Jeffery 
209d0f5da0dSAndrew Jeffery 	/* Since we're setting the status register, we want the other endpoint
210d0f5da0dSAndrew Jeffery 	 * to be interrupted. However, some hardware may only raise a host-side
211d0f5da0dSAndrew Jeffery 	 * interrupt on an ODR event.
212d0f5da0dSAndrew Jeffery 	 * So, write a dummy value of 0xff to ODR, which will ensure that an
213d0f5da0dSAndrew Jeffery 	 * interrupt is triggered, and can be ignored by the host.
214d0f5da0dSAndrew Jeffery 	 */
215d0f5da0dSAndrew Jeffery 	data = 0xff;
216d0f5da0dSAndrew Jeffery 	status |= KCS_STATUS_OBF;
217d0f5da0dSAndrew Jeffery 
218f13cb971SAndrew Jeffery 	rc = mctp_astlpc_kcs_write(astlpc, MCTP_ASTLPC_KCS_REG_STATUS, status);
219d0f5da0dSAndrew Jeffery 	if (rc) {
220d0f5da0dSAndrew Jeffery 		astlpc_prwarn(astlpc, "KCS status write failed");
221d0f5da0dSAndrew Jeffery 		return -1;
222d0f5da0dSAndrew Jeffery 	}
223d0f5da0dSAndrew Jeffery 
224f13cb971SAndrew Jeffery 	rc = mctp_astlpc_kcs_write(astlpc, MCTP_ASTLPC_KCS_REG_DATA, data);
225d0f5da0dSAndrew Jeffery 	if (rc) {
226d0f5da0dSAndrew Jeffery 		astlpc_prwarn(astlpc, "KCS dummy data write failed");
227d0f5da0dSAndrew Jeffery 		return -1;
228d0f5da0dSAndrew Jeffery 	}
229d0f5da0dSAndrew Jeffery 
230d0f5da0dSAndrew Jeffery 	return 0;
231d0f5da0dSAndrew Jeffery }
232d0f5da0dSAndrew Jeffery 
233*3a540664SAndrew Jeffery static int mctp_astlpc_layout_read(struct mctp_binding_astlpc *astlpc,
234*3a540664SAndrew Jeffery 				   struct mctp_astlpc_layout *layout)
235*3a540664SAndrew Jeffery {
236*3a540664SAndrew Jeffery 	struct mctp_lpcmap_hdr hdr;
237*3a540664SAndrew Jeffery 	int rc;
238*3a540664SAndrew Jeffery 
239*3a540664SAndrew Jeffery 	rc = mctp_astlpc_lpc_read(astlpc, &hdr, 0, sizeof(hdr));
240*3a540664SAndrew Jeffery 	if (rc < 0)
241*3a540664SAndrew Jeffery 		return rc;
242*3a540664SAndrew Jeffery 
243*3a540664SAndrew Jeffery 	/* Flip the buffers as the names are defined in terms of the host */
244*3a540664SAndrew Jeffery 	if (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_BMC) {
245*3a540664SAndrew Jeffery 		layout->rx.offset = be32toh(hdr.layout.tx_offset);
246*3a540664SAndrew Jeffery 		layout->rx.size = be32toh(hdr.layout.tx_size);
247*3a540664SAndrew Jeffery 		layout->tx.offset = be32toh(hdr.layout.rx_offset);
248*3a540664SAndrew Jeffery 		layout->tx.size = be32toh(hdr.layout.rx_size);
249*3a540664SAndrew Jeffery 	} else {
250*3a540664SAndrew Jeffery 		assert(astlpc->mode == MCTP_BINDING_ASTLPC_MODE_HOST);
251*3a540664SAndrew Jeffery 
252*3a540664SAndrew Jeffery 		layout->rx.offset = be32toh(hdr.layout.rx_offset);
253*3a540664SAndrew Jeffery 		layout->rx.size = be32toh(hdr.layout.rx_size);
254*3a540664SAndrew Jeffery 		layout->tx.offset = be32toh(hdr.layout.tx_offset);
255*3a540664SAndrew Jeffery 		layout->tx.size = be32toh(hdr.layout.tx_size);
256*3a540664SAndrew Jeffery 	}
257*3a540664SAndrew Jeffery 
258*3a540664SAndrew Jeffery 	return 0;
259*3a540664SAndrew Jeffery }
260*3a540664SAndrew Jeffery 
261*3a540664SAndrew Jeffery static int mctp_astlpc_layout_write(struct mctp_binding_astlpc *astlpc,
262*3a540664SAndrew Jeffery 				    struct mctp_astlpc_layout *layout)
263*3a540664SAndrew Jeffery {
264*3a540664SAndrew Jeffery 	uint32_t rx_size_be;
265*3a540664SAndrew Jeffery 
266*3a540664SAndrew Jeffery 	if (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_BMC) {
267*3a540664SAndrew Jeffery 		struct mctp_lpcmap_hdr hdr;
268*3a540664SAndrew Jeffery 
269*3a540664SAndrew Jeffery 		/*
270*3a540664SAndrew Jeffery 		 * Flip the buffers as the names are defined in terms of the
271*3a540664SAndrew Jeffery 		 * host
272*3a540664SAndrew Jeffery 		 */
273*3a540664SAndrew Jeffery 		hdr.layout.rx_offset = htobe32(layout->tx.offset);
274*3a540664SAndrew Jeffery 		hdr.layout.rx_size = htobe32(layout->tx.size);
275*3a540664SAndrew Jeffery 		hdr.layout.tx_offset = htobe32(layout->rx.offset);
276*3a540664SAndrew Jeffery 		hdr.layout.tx_size = htobe32(layout->rx.size);
277*3a540664SAndrew Jeffery 
278*3a540664SAndrew Jeffery 		return mctp_astlpc_lpc_write(astlpc, &hdr.layout,
279*3a540664SAndrew Jeffery 				offsetof(struct mctp_lpcmap_hdr, layout),
280*3a540664SAndrew Jeffery 				sizeof(hdr.layout));
281*3a540664SAndrew Jeffery 	}
282*3a540664SAndrew Jeffery 
283*3a540664SAndrew Jeffery 	assert(astlpc->mode == MCTP_BINDING_ASTLPC_MODE_HOST);
284*3a540664SAndrew Jeffery 
285*3a540664SAndrew Jeffery 	/*
286*3a540664SAndrew Jeffery 	 * As of v2 we only need to write rx_size - the offsets are controlled
287*3a540664SAndrew Jeffery 	 * by the BMC, as is the BMC's rx_size (host tx_size).
288*3a540664SAndrew Jeffery 	 */
289*3a540664SAndrew Jeffery 	rx_size_be = htobe32(layout->rx.size);
290*3a540664SAndrew Jeffery 	return mctp_astlpc_lpc_write(astlpc, &rx_size_be,
291*3a540664SAndrew Jeffery 			offsetof(struct mctp_lpcmap_hdr, layout.rx_size),
292*3a540664SAndrew Jeffery 			sizeof(rx_size_be));
293*3a540664SAndrew Jeffery }
294*3a540664SAndrew Jeffery 
295*3a540664SAndrew Jeffery static bool mctp_astlpc_buffer_validate(struct mctp_astlpc_buffer *buf,
296*3a540664SAndrew Jeffery 					const char *name)
297*3a540664SAndrew Jeffery {
298*3a540664SAndrew Jeffery 	/* Check for overflow */
299*3a540664SAndrew Jeffery 	if (buf->offset + buf->size < buf->offset) {
300*3a540664SAndrew Jeffery 		mctp_prerr(
301*3a540664SAndrew Jeffery 			"%s packet buffer parameters overflow: offset: 0x%" PRIx32
302*3a540664SAndrew Jeffery 			", size: %" PRIu32,
303*3a540664SAndrew Jeffery 			name, buf->offset, buf->size);
304*3a540664SAndrew Jeffery 		return false;
305*3a540664SAndrew Jeffery 	}
306*3a540664SAndrew Jeffery 
307*3a540664SAndrew Jeffery 	/* Check that the buffers are contained within the allocated space */
308*3a540664SAndrew Jeffery 	if (buf->offset + buf->size > LPC_WIN_SIZE) {
309*3a540664SAndrew Jeffery 		mctp_prerr(
310*3a540664SAndrew Jeffery 			"%s packet buffer parameters exceed %uM window size: offset: 0x%" PRIx32
311*3a540664SAndrew Jeffery 			", size: %" PRIu32,
312*3a540664SAndrew Jeffery 			name, (LPC_WIN_SIZE / (1024 * 1024)), buf->offset,
313*3a540664SAndrew Jeffery 			buf->size);
314*3a540664SAndrew Jeffery 		return false;
315*3a540664SAndrew Jeffery 	}
316*3a540664SAndrew Jeffery 
317*3a540664SAndrew Jeffery 	/* Check that the baseline transmission unit is supported */
318*3a540664SAndrew Jeffery 	if (buf->size < ASTLPC_PACKET_SIZE(MCTP_PACKET_SIZE(MCTP_BTU))) {
319*3a540664SAndrew Jeffery 		mctp_prerr(
320*3a540664SAndrew Jeffery 			"%s packet buffer too small: Require %zu bytes to support the %u byte baseline transmission unit, found %" PRIu32,
321*3a540664SAndrew Jeffery 			name, ASTLPC_PACKET_SIZE(MCTP_PACKET_SIZE(MCTP_BTU)),
322*3a540664SAndrew Jeffery 			MCTP_BTU, buf->size);
323*3a540664SAndrew Jeffery 		return false;
324*3a540664SAndrew Jeffery 	}
325*3a540664SAndrew Jeffery 
326*3a540664SAndrew Jeffery 	/* Check for overlap with the control space */
327*3a540664SAndrew Jeffery 	if (buf->offset < control_size) {
328*3a540664SAndrew Jeffery 		mctp_prerr(
329*3a540664SAndrew Jeffery 			"%s packet buffer overlaps control region {0x%" PRIx32
330*3a540664SAndrew Jeffery 			", %" PRIu32 "}: Rx {0x%" PRIx32 ", %" PRIu32 "}",
331*3a540664SAndrew Jeffery 			name, 0U, control_size, buf->offset, buf->size);
332*3a540664SAndrew Jeffery 		return false;
333*3a540664SAndrew Jeffery 	}
334*3a540664SAndrew Jeffery 
335*3a540664SAndrew Jeffery 	return true;
336*3a540664SAndrew Jeffery }
337*3a540664SAndrew Jeffery 
338*3a540664SAndrew Jeffery static bool mctp_astlpc_layout_validate(struct mctp_astlpc_layout *layout)
339*3a540664SAndrew Jeffery {
340*3a540664SAndrew Jeffery 	struct mctp_astlpc_buffer *rx = &layout->rx;
341*3a540664SAndrew Jeffery 	struct mctp_astlpc_buffer *tx = &layout->tx;
342*3a540664SAndrew Jeffery 	bool rx_valid, tx_valid;
343*3a540664SAndrew Jeffery 
344*3a540664SAndrew Jeffery 	rx_valid = mctp_astlpc_buffer_validate(rx, "Rx");
345*3a540664SAndrew Jeffery 	tx_valid = mctp_astlpc_buffer_validate(tx, "Tx");
346*3a540664SAndrew Jeffery 
347*3a540664SAndrew Jeffery 	if (!(rx_valid && tx_valid))
348*3a540664SAndrew Jeffery 		return false;
349*3a540664SAndrew Jeffery 
350*3a540664SAndrew Jeffery 	/* Check that the buffers are disjoint */
351*3a540664SAndrew Jeffery 	if ((rx->offset <= tx->offset && rx->offset + rx->size > tx->offset) ||
352*3a540664SAndrew Jeffery 	    (tx->offset <= rx->offset && tx->offset + tx->size > rx->offset)) {
353*3a540664SAndrew Jeffery 		mctp_prerr("Rx and Tx packet buffers overlap: Rx {0x%" PRIx32
354*3a540664SAndrew Jeffery 			   ", %" PRIu32 "}, Tx {0x%" PRIx32 ", %" PRIu32 "}",
355*3a540664SAndrew Jeffery 			   rx->offset, rx->size, tx->offset, tx->size);
356*3a540664SAndrew Jeffery 		return false;
357*3a540664SAndrew Jeffery 	}
358*3a540664SAndrew Jeffery 
359*3a540664SAndrew Jeffery 	return true;
360*3a540664SAndrew Jeffery }
361*3a540664SAndrew Jeffery 
3627cd72f14SAndrew Jeffery static int mctp_astlpc_init_bmc(struct mctp_binding_astlpc *astlpc)
3637cd72f14SAndrew Jeffery {
36455fb90beSAndrew Jeffery 	struct mctp_lpcmap_hdr hdr = { 0 };
3657cd72f14SAndrew Jeffery 	uint8_t status;
366*3a540664SAndrew Jeffery 	size_t sz;
367*3a540664SAndrew Jeffery 
368*3a540664SAndrew Jeffery 	/*
369*3a540664SAndrew Jeffery 	 * The largest buffer size is half of the allocated MCTP space
370*3a540664SAndrew Jeffery 	 * excluding the control space.
371*3a540664SAndrew Jeffery 	 */
372*3a540664SAndrew Jeffery 	sz = ((LPC_WIN_SIZE - control_size) / 2);
373*3a540664SAndrew Jeffery 
374*3a540664SAndrew Jeffery 	/*
375*3a540664SAndrew Jeffery 	 * Trim the MTU to a multiple of 16 to meet the requirements of 12.17
376*3a540664SAndrew Jeffery 	 * Query Hop in DSP0236 v1.3.0.
377*3a540664SAndrew Jeffery 	 */
378*3a540664SAndrew Jeffery 	sz = MCTP_BODY_SIZE(ASTLPC_BODY_SIZE(sz));
379*3a540664SAndrew Jeffery 	sz &= ~0xfUL;
380*3a540664SAndrew Jeffery 	sz = ASTLPC_PACKET_SIZE(MCTP_PACKET_SIZE(sz));
3817cd72f14SAndrew Jeffery 
3827cd72f14SAndrew Jeffery 	/* Flip the buffers as the names are defined in terms of the host */
383*3a540664SAndrew Jeffery 	astlpc->layout.tx.offset = control_size;
384*3a540664SAndrew Jeffery 	astlpc->layout.tx.size = sz;
385*3a540664SAndrew Jeffery 	astlpc->layout.rx.offset =
386*3a540664SAndrew Jeffery 		astlpc->layout.tx.offset + astlpc->layout.tx.size;
387*3a540664SAndrew Jeffery 	astlpc->layout.rx.size = sz;
388*3a540664SAndrew Jeffery 
389*3a540664SAndrew Jeffery 	/* Sanity check that can be eliminated if asserts are off */
390*3a540664SAndrew Jeffery 	assert(mctp_astlpc_layout_validate(&astlpc->layout));
3917cd72f14SAndrew Jeffery 
39255fb90beSAndrew Jeffery 	hdr = (struct mctp_lpcmap_hdr){
39355fb90beSAndrew Jeffery 		.magic = htobe32(ASTLPC_MCTP_MAGIC),
39455fb90beSAndrew Jeffery 		.bmc_ver_min = htobe16(ASTLPC_VER_MIN),
39555fb90beSAndrew Jeffery 		.bmc_ver_cur = htobe16(ASTLPC_VER_CUR),
3967cd72f14SAndrew Jeffery 
3977cd72f14SAndrew Jeffery 		/* Flip the buffers back as we're now describing the host's
3987cd72f14SAndrew Jeffery 		 * configuration to the host */
399*3a540664SAndrew Jeffery 		.layout.rx_offset = htobe32(astlpc->layout.tx.offset),
400*3a540664SAndrew Jeffery 		.layout.rx_size = htobe32(astlpc->layout.tx.size),
401*3a540664SAndrew Jeffery 		.layout.tx_offset = htobe32(astlpc->layout.rx.offset),
402*3a540664SAndrew Jeffery 		.layout.tx_size = htobe32(astlpc->layout.rx.size),
40355fb90beSAndrew Jeffery 	};
4047cd72f14SAndrew Jeffery 
40555fb90beSAndrew Jeffery 	mctp_astlpc_lpc_write(astlpc, &hdr, 0, sizeof(hdr));
4067cd72f14SAndrew Jeffery 
4077cd72f14SAndrew Jeffery 	/* set status indicating that the BMC is now active */
4087cd72f14SAndrew 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 
453*3a540664SAndrew Jeffery static int mctp_astlpc_negotiate_layout_host(struct mctp_binding_astlpc *astlpc)
454*3a540664SAndrew Jeffery {
455*3a540664SAndrew Jeffery 	struct mctp_astlpc_layout layout;
456*3a540664SAndrew Jeffery 	uint32_t sz;
457*3a540664SAndrew Jeffery 	int rc;
458*3a540664SAndrew Jeffery 
459*3a540664SAndrew Jeffery 	rc = mctp_astlpc_layout_read(astlpc, &layout);
460*3a540664SAndrew Jeffery 	if (rc < 0)
461*3a540664SAndrew Jeffery 		return rc;
462*3a540664SAndrew Jeffery 
463*3a540664SAndrew Jeffery 	if (!mctp_astlpc_layout_validate(&layout)) {
464*3a540664SAndrew Jeffery 		astlpc_prerr(
465*3a540664SAndrew Jeffery 			astlpc,
466*3a540664SAndrew Jeffery 			"BMC provided invalid buffer layout: Rx {0x%" PRIx32
467*3a540664SAndrew Jeffery 			", %" PRIu32 "}, Tx {0x%" PRIx32 ", %" PRIu32 "}",
468*3a540664SAndrew Jeffery 			layout.rx.offset, layout.rx.size, layout.tx.offset,
469*3a540664SAndrew Jeffery 			layout.tx.size);
470*3a540664SAndrew Jeffery 		return -EINVAL;
471*3a540664SAndrew Jeffery 	}
472*3a540664SAndrew Jeffery 
473*3a540664SAndrew Jeffery 	sz = ASTLPC_PACKET_SIZE(MCTP_PACKET_SIZE(MCTP_BTU));
474*3a540664SAndrew Jeffery 	layout.rx.size = sz;
475*3a540664SAndrew Jeffery 
476*3a540664SAndrew Jeffery 	if (!mctp_astlpc_layout_validate(&layout)) {
477*3a540664SAndrew Jeffery 		astlpc_prerr(
478*3a540664SAndrew Jeffery 			astlpc,
479*3a540664SAndrew Jeffery 			"Generated invalid buffer layout with size %" PRIu32
480*3a540664SAndrew Jeffery 			": Rx {0x%" PRIx32 ", %" PRIu32 "}, Tx {0x%" PRIx32
481*3a540664SAndrew Jeffery 			", %" PRIu32 "}",
482*3a540664SAndrew Jeffery 			sz, layout.rx.offset, layout.rx.size, layout.tx.offset,
483*3a540664SAndrew Jeffery 			layout.tx.size);
484*3a540664SAndrew Jeffery 		return -EINVAL;
485*3a540664SAndrew Jeffery 	}
486*3a540664SAndrew Jeffery 
487*3a540664SAndrew Jeffery 	astlpc_prinfo(astlpc, "Requesting MTU of %" PRIu32 " bytes", MCTP_BTU);
488*3a540664SAndrew Jeffery 
489*3a540664SAndrew Jeffery 	return mctp_astlpc_layout_write(astlpc, &layout);
490*3a540664SAndrew Jeffery }
491*3a540664SAndrew Jeffery 
4927cd72f14SAndrew Jeffery static int mctp_astlpc_init_host(struct mctp_binding_astlpc *astlpc)
4937cd72f14SAndrew Jeffery {
49455fb90beSAndrew Jeffery 	const uint16_t ver_min_be = htobe16(ASTLPC_VER_MIN);
49555fb90beSAndrew Jeffery 	const uint16_t ver_cur_be = htobe16(ASTLPC_VER_CUR);
4964e8264b7SAndrew Jeffery 	uint16_t bmc_ver_min, bmc_ver_cur;
49755fb90beSAndrew Jeffery 	struct mctp_lpcmap_hdr hdr;
4987cd72f14SAndrew Jeffery 	uint8_t status;
4997cd72f14SAndrew Jeffery 	int rc;
5007cd72f14SAndrew Jeffery 
501f13cb971SAndrew Jeffery 	rc = mctp_astlpc_kcs_read(astlpc, MCTP_ASTLPC_KCS_REG_STATUS, &status);
5027cd72f14SAndrew Jeffery 	if (rc) {
5037cd72f14SAndrew Jeffery 		mctp_prwarn("KCS status read failed");
5047cd72f14SAndrew Jeffery 		return rc;
5057cd72f14SAndrew Jeffery 	}
5067cd72f14SAndrew Jeffery 
5077cd72f14SAndrew Jeffery 	astlpc->kcs_status = status;
5087cd72f14SAndrew Jeffery 
5097cd72f14SAndrew Jeffery 	if (!(status & KCS_STATUS_BMC_READY))
5107cd72f14SAndrew Jeffery 		return -EHOSTDOWN;
5117cd72f14SAndrew Jeffery 
51255fb90beSAndrew Jeffery 	mctp_astlpc_lpc_read(astlpc, &hdr, 0, sizeof(hdr));
5137cd72f14SAndrew Jeffery 
5144e8264b7SAndrew Jeffery 	bmc_ver_min = be16toh(hdr.bmc_ver_min);
5154e8264b7SAndrew Jeffery 	bmc_ver_cur = be16toh(hdr.bmc_ver_cur);
5164e8264b7SAndrew Jeffery 
5174e8264b7SAndrew Jeffery 	if (!mctp_astlpc_validate_version(bmc_ver_min, bmc_ver_cur,
5184e8264b7SAndrew Jeffery 					  ASTLPC_VER_MIN, ASTLPC_VER_CUR)) {
5194e8264b7SAndrew Jeffery 		astlpc_prerr(astlpc, "Cannot negotiate with invalid versions");
5204e8264b7SAndrew Jeffery 		return -EINVAL;
5214e8264b7SAndrew Jeffery 	}
5224e8264b7SAndrew Jeffery 
523*3a540664SAndrew Jeffery 	/*
524*3a540664SAndrew Jeffery 	 * Negotation always chooses the highest protocol version that
525*3a540664SAndrew Jeffery 	 * satisfies the version constraints. So check whether the BMC supports
526*3a540664SAndrew Jeffery 	 * v2, and if so, negotiate in v2 style.
527*3a540664SAndrew Jeffery 	 */
528*3a540664SAndrew Jeffery 	if (ASTLPC_VER_CUR >= 2 && bmc_ver_cur >= 2) {
529*3a540664SAndrew Jeffery 		rc = mctp_astlpc_negotiate_layout_host(astlpc);
530*3a540664SAndrew Jeffery 		if (rc < 0)
531*3a540664SAndrew Jeffery 			return rc;
532*3a540664SAndrew Jeffery 	}
533*3a540664SAndrew Jeffery 
534*3a540664SAndrew Jeffery 	/* Version negotiation */
53555fb90beSAndrew Jeffery 	mctp_astlpc_lpc_write(astlpc, &ver_min_be,
53655fb90beSAndrew Jeffery 			      offsetof(struct mctp_lpcmap_hdr, host_ver_min),
53755fb90beSAndrew Jeffery 			      sizeof(ver_min_be));
5387cd72f14SAndrew Jeffery 
53955fb90beSAndrew Jeffery 	mctp_astlpc_lpc_write(astlpc, &ver_cur_be,
54055fb90beSAndrew Jeffery 			      offsetof(struct mctp_lpcmap_hdr, host_ver_cur),
54155fb90beSAndrew Jeffery 			      sizeof(ver_cur_be));
5427cd72f14SAndrew Jeffery 
5437cd72f14SAndrew Jeffery 	/* Send channel init command */
544f13cb971SAndrew Jeffery 	rc = mctp_astlpc_kcs_write(astlpc, MCTP_ASTLPC_KCS_REG_DATA, 0x0);
5457cd72f14SAndrew Jeffery 	if (rc) {
5469101a2abSAndrew Jeffery 		astlpc_prwarn(astlpc, "KCS write failed");
5477cd72f14SAndrew Jeffery 	}
5487cd72f14SAndrew Jeffery 
5497cd72f14SAndrew Jeffery 	return rc;
5507cd72f14SAndrew Jeffery }
5517cd72f14SAndrew Jeffery 
5527cd72f14SAndrew Jeffery static int mctp_binding_astlpc_start_host(struct mctp_binding *b)
5537cd72f14SAndrew Jeffery {
5547cd72f14SAndrew Jeffery 	struct mctp_binding_astlpc *astlpc =
5557cd72f14SAndrew Jeffery 		container_of(b, struct mctp_binding_astlpc, binding);
5567cd72f14SAndrew Jeffery 
5577cd72f14SAndrew Jeffery 	return mctp_astlpc_init_host(astlpc);
5587cd72f14SAndrew Jeffery }
5597cd72f14SAndrew Jeffery 
5607cd72f14SAndrew Jeffery static bool __mctp_astlpc_kcs_ready(struct mctp_binding_astlpc *astlpc,
5617cd72f14SAndrew Jeffery 				    uint8_t status, bool is_write)
5627cd72f14SAndrew Jeffery {
5637cd72f14SAndrew Jeffery 	bool is_bmc;
5647cd72f14SAndrew Jeffery 	bool ready_state;
5657cd72f14SAndrew Jeffery 	uint8_t flag;
5667cd72f14SAndrew Jeffery 
5677cd72f14SAndrew Jeffery 	is_bmc = (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_BMC);
5687cd72f14SAndrew Jeffery 	flag = (is_bmc ^ is_write) ? KCS_STATUS_IBF : KCS_STATUS_OBF;
5697cd72f14SAndrew Jeffery 	ready_state = is_write ? 0 : 1;
5707cd72f14SAndrew Jeffery 
5717cd72f14SAndrew Jeffery 	return !!(status & flag) == ready_state;
5727cd72f14SAndrew Jeffery }
5737cd72f14SAndrew Jeffery 
5747cd72f14SAndrew Jeffery static inline bool
5757cd72f14SAndrew Jeffery mctp_astlpc_kcs_read_ready(struct mctp_binding_astlpc *astlpc, uint8_t status)
5767cd72f14SAndrew Jeffery {
5777cd72f14SAndrew Jeffery 	return __mctp_astlpc_kcs_ready(astlpc, status, false);
5787cd72f14SAndrew Jeffery }
5797cd72f14SAndrew Jeffery 
5807cd72f14SAndrew Jeffery static inline bool
5817cd72f14SAndrew Jeffery mctp_astlpc_kcs_write_ready(struct mctp_binding_astlpc *astlpc, uint8_t status)
5827cd72f14SAndrew Jeffery {
5837cd72f14SAndrew Jeffery 	return __mctp_astlpc_kcs_ready(astlpc, status, true);
5847cd72f14SAndrew Jeffery }
5857cd72f14SAndrew Jeffery 
586672c8852SJeremy Kerr static int mctp_astlpc_kcs_send(struct mctp_binding_astlpc *astlpc,
587672c8852SJeremy Kerr 		uint8_t data)
588672c8852SJeremy Kerr {
589672c8852SJeremy Kerr 	uint8_t status;
590672c8852SJeremy Kerr 	int rc;
591672c8852SJeremy Kerr 
592672c8852SJeremy Kerr 	for (;;) {
593f13cb971SAndrew Jeffery 		rc = mctp_astlpc_kcs_read(astlpc, MCTP_ASTLPC_KCS_REG_STATUS,
594f13cb971SAndrew Jeffery 					  &status);
5951b27fe87SAndrew Jeffery 		if (rc) {
5969101a2abSAndrew Jeffery 			astlpc_prwarn(astlpc, "KCS status read failed");
597672c8852SJeremy Kerr 			return -1;
598672c8852SJeremy Kerr 		}
5997cd72f14SAndrew Jeffery 		if (mctp_astlpc_kcs_write_ready(astlpc, status))
600672c8852SJeremy Kerr 			break;
601672c8852SJeremy Kerr 		/* todo: timeout */
602672c8852SJeremy Kerr 	}
603672c8852SJeremy Kerr 
604f13cb971SAndrew Jeffery 	rc = mctp_astlpc_kcs_write(astlpc, MCTP_ASTLPC_KCS_REG_DATA, data);
605bc53d35aSJeremy Kerr 	if (rc) {
6069101a2abSAndrew Jeffery 		astlpc_prwarn(astlpc, "KCS data write failed");
607672c8852SJeremy Kerr 		return -1;
608672c8852SJeremy Kerr 	}
609672c8852SJeremy Kerr 
610672c8852SJeremy Kerr 	return 0;
611672c8852SJeremy Kerr }
612672c8852SJeremy Kerr 
613672c8852SJeremy Kerr static int mctp_binding_astlpc_tx(struct mctp_binding *b,
614672c8852SJeremy Kerr 		struct mctp_pktbuf *pkt)
615672c8852SJeremy Kerr {
616672c8852SJeremy Kerr 	struct mctp_binding_astlpc *astlpc = binding_to_astlpc(b);
61755fb90beSAndrew Jeffery 	uint32_t len, len_be;
618edfe383fSAndrew Jeffery 	struct mctp_hdr *hdr;
619672c8852SJeremy Kerr 
620edfe383fSAndrew Jeffery 	hdr = mctp_pktbuf_hdr(pkt);
621672c8852SJeremy Kerr 	len = mctp_pktbuf_size(pkt);
622edfe383fSAndrew Jeffery 
6239101a2abSAndrew Jeffery 	astlpc_prdebug(astlpc,
6249101a2abSAndrew Jeffery 		       "%s: Transmitting %" PRIu32
6259101a2abSAndrew Jeffery 		       "-byte packet (%hhu, %hhu, 0x%hhx)",
626edfe383fSAndrew Jeffery 		       __func__, len, hdr->src, hdr->dest, hdr->flags_seq_tag);
627edfe383fSAndrew Jeffery 
6287cd72f14SAndrew Jeffery 	if (len > ASTLPC_BODY_SIZE(astlpc->layout.tx.size)) {
6299101a2abSAndrew Jeffery 		astlpc_prwarn(astlpc, "invalid TX len 0x%x", len);
630672c8852SJeremy Kerr 		return -1;
631672c8852SJeremy Kerr 	}
632672c8852SJeremy Kerr 
63355fb90beSAndrew Jeffery 	len_be = htobe32(len);
63455fb90beSAndrew Jeffery 	mctp_astlpc_lpc_write(astlpc, &len_be, astlpc->layout.tx.offset,
63555fb90beSAndrew Jeffery 			      sizeof(len_be));
63655fb90beSAndrew Jeffery 	mctp_astlpc_lpc_write(astlpc, hdr, astlpc->layout.tx.offset + 4, len);
637672c8852SJeremy Kerr 
638672c8852SJeremy Kerr 	mctp_binding_set_tx_enabled(b, false);
639672c8852SJeremy Kerr 
640672c8852SJeremy Kerr 	mctp_astlpc_kcs_send(astlpc, 0x1);
641672c8852SJeremy Kerr 	return 0;
642672c8852SJeremy Kerr }
643672c8852SJeremy Kerr 
6444e8264b7SAndrew Jeffery static uint16_t mctp_astlpc_negotiate_version(uint16_t bmc_ver_min,
6454e8264b7SAndrew Jeffery 					      uint16_t bmc_ver_cur,
6464e8264b7SAndrew Jeffery 					      uint16_t host_ver_min,
6474e8264b7SAndrew Jeffery 					      uint16_t host_ver_cur)
6484e8264b7SAndrew Jeffery {
6494e8264b7SAndrew Jeffery 	if (!mctp_astlpc_validate_version(bmc_ver_min, bmc_ver_cur,
6504e8264b7SAndrew Jeffery 					  host_ver_min, host_ver_cur))
6514e8264b7SAndrew Jeffery 		return ASTLPC_VER_BAD;
6524e8264b7SAndrew Jeffery 
6534e8264b7SAndrew Jeffery 	if (bmc_ver_cur < host_ver_cur)
6544e8264b7SAndrew Jeffery 		return bmc_ver_cur;
6554e8264b7SAndrew Jeffery 
6564e8264b7SAndrew Jeffery 	return host_ver_cur;
6574e8264b7SAndrew Jeffery }
6584e8264b7SAndrew Jeffery 
659*3a540664SAndrew Jeffery static uint32_t mctp_astlpc_calculate_mtu(struct mctp_binding_astlpc *astlpc,
660*3a540664SAndrew Jeffery 					  struct mctp_astlpc_layout *layout)
661*3a540664SAndrew Jeffery {
662*3a540664SAndrew Jeffery 	uint32_t low, high, limit;
663*3a540664SAndrew Jeffery 
664*3a540664SAndrew Jeffery 	/* Derive the largest MTU the BMC _can_ support */
665*3a540664SAndrew Jeffery 	low = MIN(astlpc->layout.rx.offset, astlpc->layout.tx.offset);
666*3a540664SAndrew Jeffery 	high = MAX(astlpc->layout.rx.offset, astlpc->layout.tx.offset);
667*3a540664SAndrew Jeffery 	limit = high - low;
668*3a540664SAndrew Jeffery 
669*3a540664SAndrew Jeffery 
670*3a540664SAndrew Jeffery 	/* Determine the accepted MTU, applied both directions by convention */
671*3a540664SAndrew Jeffery 	return MCTP_BODY_SIZE(ASTLPC_BODY_SIZE(MIN(limit, layout->tx.size)));
672*3a540664SAndrew Jeffery }
673*3a540664SAndrew Jeffery 
674*3a540664SAndrew Jeffery static int mctp_astlpc_negotiate_layout_bmc(struct mctp_binding_astlpc *astlpc)
675*3a540664SAndrew Jeffery {
676*3a540664SAndrew Jeffery 	struct mctp_astlpc_layout proposed, pending;
677*3a540664SAndrew Jeffery 	uint32_t sz, mtu;
678*3a540664SAndrew Jeffery 	int rc;
679*3a540664SAndrew Jeffery 
680*3a540664SAndrew Jeffery 	/* Extract the host's proposed layout */
681*3a540664SAndrew Jeffery 	rc = mctp_astlpc_layout_read(astlpc, &proposed);
682*3a540664SAndrew Jeffery 	if (rc < 0)
683*3a540664SAndrew Jeffery 		return rc;
684*3a540664SAndrew Jeffery 
685*3a540664SAndrew Jeffery 	if (!mctp_astlpc_layout_validate(&proposed))
686*3a540664SAndrew Jeffery 		return -EINVAL;
687*3a540664SAndrew Jeffery 
688*3a540664SAndrew Jeffery 	/* Negotiate the MTU */
689*3a540664SAndrew Jeffery 	mtu = mctp_astlpc_calculate_mtu(astlpc, &proposed);
690*3a540664SAndrew Jeffery 	sz = ASTLPC_PACKET_SIZE(MCTP_PACKET_SIZE(mtu));
691*3a540664SAndrew Jeffery 
692*3a540664SAndrew Jeffery 	/*
693*3a540664SAndrew Jeffery 	 * Use symmetric MTUs by convention and to pass constraints in rx/tx
694*3a540664SAndrew Jeffery 	 * functions
695*3a540664SAndrew Jeffery 	 */
696*3a540664SAndrew Jeffery 	pending = astlpc->layout;
697*3a540664SAndrew Jeffery 	pending.tx.size = sz;
698*3a540664SAndrew Jeffery 	pending.rx.size = sz;
699*3a540664SAndrew Jeffery 
700*3a540664SAndrew Jeffery 	if (mctp_astlpc_layout_validate(&pending)) {
701*3a540664SAndrew Jeffery 		/* We found a sensible Rx MTU, so honour it */
702*3a540664SAndrew Jeffery 		astlpc->layout = pending;
703*3a540664SAndrew Jeffery 
704*3a540664SAndrew Jeffery 		/* Enforce the negotiated MTU */
705*3a540664SAndrew Jeffery 		rc = mctp_astlpc_layout_write(astlpc, &astlpc->layout);
706*3a540664SAndrew Jeffery 		if (rc < 0)
707*3a540664SAndrew Jeffery 			return rc;
708*3a540664SAndrew Jeffery 
709*3a540664SAndrew Jeffery 		astlpc_prinfo(astlpc, "Negotiated an MTU of %" PRIu32 " bytes",
710*3a540664SAndrew Jeffery 			      mtu);
711*3a540664SAndrew Jeffery 	} else {
712*3a540664SAndrew Jeffery 		astlpc_prwarn(astlpc, "MTU negotiation failed");
713*3a540664SAndrew Jeffery 		return -EINVAL;
714*3a540664SAndrew Jeffery 	}
715*3a540664SAndrew Jeffery 
716*3a540664SAndrew Jeffery 	if (astlpc->version >= 2)
717*3a540664SAndrew Jeffery 		astlpc->binding.pkt_size = MCTP_PACKET_SIZE(mtu);
718*3a540664SAndrew Jeffery 
719*3a540664SAndrew Jeffery 	return 0;
720*3a540664SAndrew Jeffery }
721*3a540664SAndrew Jeffery 
722672c8852SJeremy Kerr static void mctp_astlpc_init_channel(struct mctp_binding_astlpc *astlpc)
723672c8852SJeremy Kerr {
7244e8264b7SAndrew Jeffery 	uint16_t negotiated, negotiated_be;
7254e8264b7SAndrew Jeffery 	struct mctp_lpcmap_hdr hdr;
7264e8264b7SAndrew Jeffery 	uint8_t status;
727*3a540664SAndrew Jeffery 	int rc;
7284e8264b7SAndrew Jeffery 
7294e8264b7SAndrew Jeffery 	mctp_astlpc_lpc_read(astlpc, &hdr, 0, sizeof(hdr));
7304e8264b7SAndrew Jeffery 
7314e8264b7SAndrew Jeffery 	/* Version negotiation */
7324e8264b7SAndrew Jeffery 	negotiated =
7334e8264b7SAndrew Jeffery 		mctp_astlpc_negotiate_version(ASTLPC_VER_MIN, ASTLPC_VER_CUR,
7344e8264b7SAndrew Jeffery 					      be16toh(hdr.host_ver_min),
7354e8264b7SAndrew Jeffery 					      be16toh(hdr.host_ver_cur));
7364e8264b7SAndrew Jeffery 
737*3a540664SAndrew Jeffery 	/* Host Rx MTU negotiation: Failure terminates channel init */
738*3a540664SAndrew Jeffery 	rc = mctp_astlpc_negotiate_layout_bmc(astlpc);
739*3a540664SAndrew Jeffery 	if (rc < 0)
740*3a540664SAndrew Jeffery 		negotiated = ASTLPC_VER_BAD;
741*3a540664SAndrew Jeffery 
7424e8264b7SAndrew Jeffery 	/* Populate the negotiated version */
7434e8264b7SAndrew Jeffery 	astlpc->version = negotiated;
7444e8264b7SAndrew Jeffery 	negotiated_be = htobe16(negotiated);
7454e8264b7SAndrew Jeffery 	mctp_astlpc_lpc_write(astlpc, &negotiated_be,
74655fb90beSAndrew Jeffery 			      offsetof(struct mctp_lpcmap_hdr, negotiated_ver),
7474e8264b7SAndrew Jeffery 			      sizeof(negotiated_be));
74855fb90beSAndrew Jeffery 
7494e8264b7SAndrew Jeffery 	/* Finalise the configuration */
7504e8264b7SAndrew Jeffery 	status = KCS_STATUS_BMC_READY | KCS_STATUS_OBF;
7514e8264b7SAndrew Jeffery 	if (negotiated > 0) {
7524e8264b7SAndrew Jeffery 		astlpc_prinfo(astlpc, "Negotiated binding version %" PRIu16,
7534e8264b7SAndrew Jeffery 			      negotiated);
7544e8264b7SAndrew Jeffery 		status |= KCS_STATUS_CHANNEL_ACTIVE;
7554e8264b7SAndrew Jeffery 	} else {
7564e8264b7SAndrew Jeffery 		astlpc_prerr(astlpc, "Failed to initialise channel\n");
7574e8264b7SAndrew Jeffery 	}
758672c8852SJeremy Kerr 
7594e8264b7SAndrew Jeffery 	mctp_astlpc_kcs_set_status(astlpc, status);
7604e8264b7SAndrew Jeffery 
7614e8264b7SAndrew Jeffery 	mctp_binding_set_tx_enabled(&astlpc->binding,
7624e8264b7SAndrew Jeffery 				    status & KCS_STATUS_CHANNEL_ACTIVE);
763672c8852SJeremy Kerr }
764672c8852SJeremy Kerr 
765672c8852SJeremy Kerr static void mctp_astlpc_rx_start(struct mctp_binding_astlpc *astlpc)
766672c8852SJeremy Kerr {
767672c8852SJeremy Kerr 	struct mctp_pktbuf *pkt;
768672c8852SJeremy Kerr 	uint32_t len;
769672c8852SJeremy Kerr 
77055fb90beSAndrew Jeffery 	mctp_astlpc_lpc_read(astlpc, &len, astlpc->layout.rx.offset,
77155fb90beSAndrew Jeffery 			     sizeof(len));
772bc53d35aSJeremy Kerr 	len = be32toh(len);
773bc53d35aSJeremy Kerr 
7747cd72f14SAndrew Jeffery 	if (len > ASTLPC_BODY_SIZE(astlpc->layout.rx.size)) {
7759101a2abSAndrew Jeffery 		astlpc_prwarn(astlpc, "invalid RX len 0x%x", len);
776672c8852SJeremy Kerr 		return;
777672c8852SJeremy Kerr 	}
778672c8852SJeremy Kerr 
779b93b6112SAndrew Jeffery 	assert(astlpc->binding.pkt_size >= 0);
780b93b6112SAndrew Jeffery 	if (len > (uint32_t)astlpc->binding.pkt_size) {
781672c8852SJeremy Kerr 		mctp_prwarn("invalid RX len 0x%x", len);
7829101a2abSAndrew Jeffery 		astlpc_prwarn(astlpc, "invalid RX len 0x%x", len);
783672c8852SJeremy Kerr 		return;
784672c8852SJeremy Kerr 	}
785672c8852SJeremy Kerr 
786df15f7e9SJeremy Kerr 	pkt = mctp_pktbuf_alloc(&astlpc->binding, len);
787672c8852SJeremy Kerr 	if (!pkt)
788672c8852SJeremy Kerr 		goto out_complete;
789672c8852SJeremy Kerr 
79055fb90beSAndrew Jeffery 	mctp_astlpc_lpc_read(astlpc, mctp_pktbuf_hdr(pkt),
7917cd72f14SAndrew Jeffery 			     astlpc->layout.rx.offset + 4, len);
792672c8852SJeremy Kerr 
793672c8852SJeremy Kerr 	mctp_bus_rx(&astlpc->binding, pkt);
794672c8852SJeremy Kerr 
795672c8852SJeremy Kerr out_complete:
796672c8852SJeremy Kerr 	mctp_astlpc_kcs_send(astlpc, 0x2);
797672c8852SJeremy Kerr }
798672c8852SJeremy Kerr 
799672c8852SJeremy Kerr static void mctp_astlpc_tx_complete(struct mctp_binding_astlpc *astlpc)
800672c8852SJeremy Kerr {
801672c8852SJeremy Kerr 	mctp_binding_set_tx_enabled(&astlpc->binding, true);
802672c8852SJeremy Kerr }
803672c8852SJeremy Kerr 
8044e8264b7SAndrew Jeffery static int mctp_astlpc_finalise_channel(struct mctp_binding_astlpc *astlpc)
8054e8264b7SAndrew Jeffery {
806*3a540664SAndrew Jeffery 	struct mctp_astlpc_layout layout;
8074e8264b7SAndrew Jeffery 	uint16_t negotiated;
8084e8264b7SAndrew Jeffery 	int rc;
8094e8264b7SAndrew Jeffery 
8104e8264b7SAndrew Jeffery 	rc = mctp_astlpc_lpc_read(astlpc, &negotiated,
8114e8264b7SAndrew Jeffery 				  offsetof(struct mctp_lpcmap_hdr,
8124e8264b7SAndrew Jeffery 					   negotiated_ver),
8134e8264b7SAndrew Jeffery 				  sizeof(negotiated));
8144e8264b7SAndrew Jeffery 	if (rc < 0)
8154e8264b7SAndrew Jeffery 		return rc;
8164e8264b7SAndrew Jeffery 
8174e8264b7SAndrew Jeffery 	negotiated = be16toh(negotiated);
8184e8264b7SAndrew Jeffery 
8194e8264b7SAndrew Jeffery 	if (negotiated == ASTLPC_VER_BAD || negotiated < ASTLPC_VER_MIN ||
8204e8264b7SAndrew Jeffery 	    negotiated > ASTLPC_VER_CUR) {
8214e8264b7SAndrew Jeffery 		astlpc_prerr(astlpc, "Failed to negotiate version, got: %u\n",
8224e8264b7SAndrew Jeffery 			     negotiated);
8234e8264b7SAndrew Jeffery 		return -EINVAL;
8244e8264b7SAndrew Jeffery 	}
8254e8264b7SAndrew Jeffery 
8264e8264b7SAndrew Jeffery 	astlpc->version = negotiated;
8274e8264b7SAndrew Jeffery 
828*3a540664SAndrew Jeffery 	rc = mctp_astlpc_layout_read(astlpc, &layout);
829*3a540664SAndrew Jeffery 	if (rc < 0)
830*3a540664SAndrew Jeffery 		return rc;
831*3a540664SAndrew Jeffery 
832*3a540664SAndrew Jeffery 	if (!mctp_astlpc_layout_validate(&layout)) {
833*3a540664SAndrew Jeffery 		mctp_prerr("BMC proposed invalid buffer parameters");
834*3a540664SAndrew Jeffery 		return -EINVAL;
835*3a540664SAndrew Jeffery 	}
836*3a540664SAndrew Jeffery 
837*3a540664SAndrew Jeffery 	astlpc->layout = layout;
838*3a540664SAndrew Jeffery 
839*3a540664SAndrew Jeffery 	if (negotiated >= 2)
840*3a540664SAndrew Jeffery 		astlpc->binding.pkt_size =
841*3a540664SAndrew Jeffery 			ASTLPC_BODY_SIZE(astlpc->layout.tx.size);
842*3a540664SAndrew Jeffery 
8434e8264b7SAndrew Jeffery 	return 0;
8444e8264b7SAndrew Jeffery }
8454e8264b7SAndrew Jeffery 
8467cd72f14SAndrew Jeffery static int mctp_astlpc_update_channel(struct mctp_binding_astlpc *astlpc,
8477cd72f14SAndrew Jeffery 				      uint8_t status)
8487cd72f14SAndrew Jeffery {
8497cd72f14SAndrew Jeffery 	uint8_t updated;
8507cd72f14SAndrew Jeffery 	int rc = 0;
8517cd72f14SAndrew Jeffery 
8527cd72f14SAndrew Jeffery 	assert(astlpc->mode == MCTP_BINDING_ASTLPC_MODE_HOST);
8537cd72f14SAndrew Jeffery 
8547cd72f14SAndrew Jeffery 	updated = astlpc->kcs_status ^ status;
8557cd72f14SAndrew Jeffery 
856d0f5da0dSAndrew Jeffery 	astlpc_prdebug(astlpc, "%s: status: 0x%x, update: 0x%x", __func__,
857d0f5da0dSAndrew Jeffery 		       status, updated);
858d0f5da0dSAndrew Jeffery 
8597cd72f14SAndrew Jeffery 	if (updated & KCS_STATUS_BMC_READY) {
860d0f5da0dSAndrew Jeffery 		if (status & KCS_STATUS_BMC_READY) {
861d0f5da0dSAndrew Jeffery 			astlpc->kcs_status = status;
862d0f5da0dSAndrew Jeffery 			return astlpc->binding.start(&astlpc->binding);
863d0f5da0dSAndrew Jeffery 		} else {
8647cd72f14SAndrew Jeffery 			mctp_binding_set_tx_enabled(&astlpc->binding, false);
8657cd72f14SAndrew Jeffery 		}
866d0f5da0dSAndrew Jeffery 	}
8677cd72f14SAndrew Jeffery 
8684e8264b7SAndrew Jeffery 	if (astlpc->version == 0 || updated & KCS_STATUS_CHANNEL_ACTIVE) {
8694e8264b7SAndrew Jeffery 		bool enable;
8704e8264b7SAndrew Jeffery 
8714e8264b7SAndrew Jeffery 		rc = mctp_astlpc_finalise_channel(astlpc);
8724e8264b7SAndrew Jeffery 		enable = (status & KCS_STATUS_CHANNEL_ACTIVE) && rc == 0;
8734e8264b7SAndrew Jeffery 
8744e8264b7SAndrew Jeffery 		mctp_binding_set_tx_enabled(&astlpc->binding, enable);
8754e8264b7SAndrew Jeffery 	}
8767cd72f14SAndrew Jeffery 
8777cd72f14SAndrew Jeffery 	astlpc->kcs_status = status;
8787cd72f14SAndrew Jeffery 
8797cd72f14SAndrew Jeffery 	return rc;
8807cd72f14SAndrew Jeffery }
8817cd72f14SAndrew Jeffery 
882672c8852SJeremy Kerr int mctp_astlpc_poll(struct mctp_binding_astlpc *astlpc)
883672c8852SJeremy Kerr {
884bc53d35aSJeremy Kerr 	uint8_t status, data;
885672c8852SJeremy Kerr 	int rc;
886672c8852SJeremy Kerr 
887f13cb971SAndrew Jeffery 	rc = mctp_astlpc_kcs_read(astlpc, MCTP_ASTLPC_KCS_REG_STATUS, &status);
888bc53d35aSJeremy Kerr 	if (rc) {
8899101a2abSAndrew Jeffery 		astlpc_prwarn(astlpc, "KCS read error");
890672c8852SJeremy Kerr 		return -1;
891672c8852SJeremy Kerr 	}
892672c8852SJeremy Kerr 
8939101a2abSAndrew Jeffery 	astlpc_prdebug(astlpc, "%s: status: 0x%hhx", __func__, status);
894edfe383fSAndrew Jeffery 
8957cd72f14SAndrew Jeffery 	if (!mctp_astlpc_kcs_read_ready(astlpc, status))
896672c8852SJeremy Kerr 		return 0;
897672c8852SJeremy Kerr 
898f13cb971SAndrew Jeffery 	rc = mctp_astlpc_kcs_read(astlpc, MCTP_ASTLPC_KCS_REG_DATA, &data);
899bc53d35aSJeremy Kerr 	if (rc) {
9009101a2abSAndrew Jeffery 		astlpc_prwarn(astlpc, "KCS data read error");
901bc53d35aSJeremy Kerr 		return -1;
902bc53d35aSJeremy Kerr 	}
903bc53d35aSJeremy Kerr 
9049101a2abSAndrew Jeffery 	astlpc_prdebug(astlpc, "%s: data: 0x%hhx", __func__, data);
905edfe383fSAndrew Jeffery 
906672c8852SJeremy Kerr 	switch (data) {
907672c8852SJeremy Kerr 	case 0x0:
908672c8852SJeremy Kerr 		mctp_astlpc_init_channel(astlpc);
909672c8852SJeremy Kerr 		break;
910672c8852SJeremy Kerr 	case 0x1:
911672c8852SJeremy Kerr 		mctp_astlpc_rx_start(astlpc);
912672c8852SJeremy Kerr 		break;
913672c8852SJeremy Kerr 	case 0x2:
914672c8852SJeremy Kerr 		mctp_astlpc_tx_complete(astlpc);
915672c8852SJeremy Kerr 		break;
9161a4b55acSJeremy Kerr 	case 0xff:
9177cd72f14SAndrew Jeffery 		/* No responsibilities for the BMC on 0xff */
918d0f5da0dSAndrew Jeffery 		if (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_HOST) {
919d0f5da0dSAndrew Jeffery 			rc = mctp_astlpc_update_channel(astlpc, status);
920d0f5da0dSAndrew Jeffery 			if (rc < 0)
921d0f5da0dSAndrew Jeffery 				return rc;
922d0f5da0dSAndrew Jeffery 		}
923d0f5da0dSAndrew Jeffery 		break;
924672c8852SJeremy Kerr 	default:
9259101a2abSAndrew Jeffery 		astlpc_prwarn(astlpc, "unknown message 0x%x", data);
926672c8852SJeremy Kerr 	}
927d0f5da0dSAndrew Jeffery 
928d0f5da0dSAndrew Jeffery 	/* Handle silent loss of bmc-ready */
929d0f5da0dSAndrew Jeffery 	if (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_HOST) {
930d0f5da0dSAndrew Jeffery 		if (!(status & KCS_STATUS_BMC_READY && data == 0xff))
931d0f5da0dSAndrew Jeffery 			return mctp_astlpc_update_channel(astlpc, status);
932d0f5da0dSAndrew Jeffery 	}
933d0f5da0dSAndrew Jeffery 
934d0f5da0dSAndrew Jeffery 	return rc;
935672c8852SJeremy Kerr }
936672c8852SJeremy Kerr 
937bc53d35aSJeremy Kerr /* allocate and basic initialisation */
9387cd72f14SAndrew Jeffery static struct mctp_binding_astlpc *__mctp_astlpc_init(uint8_t mode,
9397cd72f14SAndrew Jeffery 						      uint32_t mtu)
940bc53d35aSJeremy Kerr {
941bc53d35aSJeremy Kerr 	struct mctp_binding_astlpc *astlpc;
942bc53d35aSJeremy Kerr 
9437cd72f14SAndrew Jeffery 	assert((mode == MCTP_BINDING_ASTLPC_MODE_BMC) ||
9447cd72f14SAndrew Jeffery 	       (mode == MCTP_BINDING_ASTLPC_MODE_HOST));
9457cd72f14SAndrew Jeffery 
946bc53d35aSJeremy Kerr 	astlpc = __mctp_alloc(sizeof(*astlpc));
9477cd72f14SAndrew Jeffery 	if (!astlpc)
9487cd72f14SAndrew Jeffery 		return NULL;
9497cd72f14SAndrew Jeffery 
950bc53d35aSJeremy Kerr 	memset(astlpc, 0, sizeof(*astlpc));
9517cd72f14SAndrew Jeffery 	astlpc->mode = mode;
9527cd72f14SAndrew Jeffery 	astlpc->lpc_map = NULL;
953bc53d35aSJeremy Kerr 	astlpc->binding.name = "astlpc";
954bc53d35aSJeremy Kerr 	astlpc->binding.version = 1;
9557cd72f14SAndrew Jeffery 	astlpc->binding.pkt_size = MCTP_PACKET_SIZE(mtu);
956bc53d35aSJeremy Kerr 	astlpc->binding.pkt_pad = 0;
9577cd72f14SAndrew Jeffery 	astlpc->binding.tx = mctp_binding_astlpc_tx;
9587cd72f14SAndrew Jeffery 	if (mode == MCTP_BINDING_ASTLPC_MODE_BMC)
9597cd72f14SAndrew Jeffery 		astlpc->binding.start = mctp_binding_astlpc_start_bmc;
9607cd72f14SAndrew Jeffery 	else if (mode == MCTP_BINDING_ASTLPC_MODE_HOST)
9617cd72f14SAndrew Jeffery 		astlpc->binding.start = mctp_binding_astlpc_start_host;
9627cd72f14SAndrew Jeffery 	else {
9639101a2abSAndrew Jeffery 		astlpc_prerr(astlpc, "%s: Invalid mode: %d\n", __func__, mode);
9647cd72f14SAndrew Jeffery 		__mctp_free(astlpc);
9657cd72f14SAndrew Jeffery 		return NULL;
9667cd72f14SAndrew Jeffery 	}
967bc53d35aSJeremy Kerr 
968bc53d35aSJeremy Kerr 	return astlpc;
969bc53d35aSJeremy Kerr }
970bc53d35aSJeremy Kerr 
9713b36d17cSJeremy Kerr struct mctp_binding *mctp_binding_astlpc_core(struct mctp_binding_astlpc *b)
9723b36d17cSJeremy Kerr {
9733b36d17cSJeremy Kerr 	return &b->binding;
9743b36d17cSJeremy Kerr }
9753b36d17cSJeremy Kerr 
9767cd72f14SAndrew Jeffery struct mctp_binding_astlpc *
9777cd72f14SAndrew Jeffery mctp_astlpc_init(uint8_t mode, uint32_t mtu, void *lpc_map,
9787cd72f14SAndrew Jeffery 		 const struct mctp_binding_astlpc_ops *ops, void *ops_data)
979bc53d35aSJeremy Kerr {
980bc53d35aSJeremy Kerr 	struct mctp_binding_astlpc *astlpc;
981bc53d35aSJeremy Kerr 
9827cd72f14SAndrew Jeffery 	if (!(mode == MCTP_BINDING_ASTLPC_MODE_BMC ||
9837cd72f14SAndrew Jeffery 	      mode == MCTP_BINDING_ASTLPC_MODE_HOST)) {
9847cd72f14SAndrew Jeffery 		mctp_prerr("Unknown binding mode: %u", mode);
9857cd72f14SAndrew Jeffery 		return NULL;
9867cd72f14SAndrew Jeffery 	}
9877cd72f14SAndrew Jeffery 
9887cd72f14SAndrew Jeffery 	if (mtu != MCTP_BTU) {
9897cd72f14SAndrew Jeffery 		mctp_prwarn("Unable to negotiate the MTU, using %u instead",
9907cd72f14SAndrew Jeffery 			    MCTP_BTU);
9917cd72f14SAndrew Jeffery 		mtu = MCTP_BTU;
9927cd72f14SAndrew Jeffery 	}
9937cd72f14SAndrew Jeffery 
9947cd72f14SAndrew Jeffery 	astlpc = __mctp_astlpc_init(mode, mtu);
995bc53d35aSJeremy Kerr 	if (!astlpc)
996bc53d35aSJeremy Kerr 		return NULL;
997bc53d35aSJeremy Kerr 
998bc53d35aSJeremy Kerr 	memcpy(&astlpc->ops, ops, sizeof(astlpc->ops));
999bc53d35aSJeremy Kerr 	astlpc->ops_data = ops_data;
1000bc53d35aSJeremy Kerr 	astlpc->lpc_map = lpc_map;
10017cd72f14SAndrew Jeffery 	astlpc->mode = mode;
1002bc53d35aSJeremy Kerr 
1003bc53d35aSJeremy Kerr 	return astlpc;
1004bc53d35aSJeremy Kerr }
1005bc53d35aSJeremy Kerr 
10067cd72f14SAndrew Jeffery struct mctp_binding_astlpc *
10077cd72f14SAndrew Jeffery mctp_astlpc_init_ops(const struct mctp_binding_astlpc_ops *ops, void *ops_data,
10087cd72f14SAndrew Jeffery 		     void *lpc_map)
10097cd72f14SAndrew Jeffery {
10107cd72f14SAndrew Jeffery 	return mctp_astlpc_init(MCTP_BINDING_ASTLPC_MODE_BMC, MCTP_BTU, lpc_map,
10117cd72f14SAndrew Jeffery 				ops, ops_data);
10127cd72f14SAndrew Jeffery }
10137cd72f14SAndrew Jeffery 
10144663f67cSAndrew Jeffery void mctp_astlpc_destroy(struct mctp_binding_astlpc *astlpc)
10154663f67cSAndrew Jeffery {
1016d0f5da0dSAndrew Jeffery 	/* Clear channel-active and bmc-ready */
1017d0f5da0dSAndrew Jeffery 	if (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_BMC)
1018d0f5da0dSAndrew Jeffery 		mctp_astlpc_kcs_set_status(astlpc, KCS_STATUS_OBF);
10194663f67cSAndrew Jeffery 	__mctp_free(astlpc);
10204663f67cSAndrew Jeffery }
10214663f67cSAndrew Jeffery 
1022b214c643SJeremy Kerr #ifdef MCTP_HAVE_FILEIO
102355fb90beSAndrew Jeffery 
1024bc53d35aSJeremy Kerr static int mctp_astlpc_init_fileio_lpc(struct mctp_binding_astlpc *astlpc)
1025672c8852SJeremy Kerr {
1026672c8852SJeremy Kerr 	struct aspeed_lpc_ctrl_mapping map = {
1027672c8852SJeremy Kerr 		.window_type = ASPEED_LPC_CTRL_WINDOW_MEMORY,
1028672c8852SJeremy Kerr 		.window_id = 0, /* There's only one */
1029672c8852SJeremy Kerr 		.flags = 0,
1030672c8852SJeremy Kerr 		.addr = 0,
1031672c8852SJeremy Kerr 		.offset = 0,
1032672c8852SJeremy Kerr 		.size = 0
1033672c8852SJeremy Kerr 	};
1034979c6a16SAndrew Jeffery 	void *lpc_map_base;
1035672c8852SJeremy Kerr 	int fd, rc;
1036672c8852SJeremy Kerr 
1037672c8852SJeremy Kerr 	fd = open(lpc_path, O_RDWR | O_SYNC);
1038672c8852SJeremy Kerr 	if (fd < 0) {
10399101a2abSAndrew Jeffery 		astlpc_prwarn(astlpc, "LPC open (%s) failed", lpc_path);
1040672c8852SJeremy Kerr 		return -1;
1041672c8852SJeremy Kerr 	}
1042672c8852SJeremy Kerr 
1043672c8852SJeremy Kerr 	rc = ioctl(fd, ASPEED_LPC_CTRL_IOCTL_GET_SIZE, &map);
1044672c8852SJeremy Kerr 	if (rc) {
10459101a2abSAndrew Jeffery 		astlpc_prwarn(astlpc, "LPC GET_SIZE failed");
1046672c8852SJeremy Kerr 		close(fd);
1047672c8852SJeremy Kerr 		return -1;
1048672c8852SJeremy Kerr 	}
1049672c8852SJeremy Kerr 
1050979c6a16SAndrew Jeffery 	lpc_map_base =
1051979c6a16SAndrew Jeffery 		mmap(NULL, map.size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
1052979c6a16SAndrew Jeffery 	if (lpc_map_base == MAP_FAILED) {
10539101a2abSAndrew Jeffery 		astlpc_prwarn(astlpc, "LPC mmap failed");
1054672c8852SJeremy Kerr 		rc = -1;
1055672c8852SJeremy Kerr 	} else {
1056979c6a16SAndrew Jeffery 		astlpc->lpc_map = lpc_map_base + map.size - LPC_WIN_SIZE;
1057672c8852SJeremy Kerr 	}
1058672c8852SJeremy Kerr 
1059672c8852SJeremy Kerr 	close(fd);
1060672c8852SJeremy Kerr 
1061672c8852SJeremy Kerr 	return rc;
1062672c8852SJeremy Kerr }
1063672c8852SJeremy Kerr 
1064bc53d35aSJeremy Kerr static int mctp_astlpc_init_fileio_kcs(struct mctp_binding_astlpc *astlpc)
1065672c8852SJeremy Kerr {
1066672c8852SJeremy Kerr 	astlpc->kcs_fd = open(kcs_path, O_RDWR);
1067672c8852SJeremy Kerr 	if (astlpc->kcs_fd < 0)
1068672c8852SJeremy Kerr 		return -1;
1069672c8852SJeremy Kerr 
1070672c8852SJeremy Kerr 	return 0;
1071672c8852SJeremy Kerr }
1072672c8852SJeremy Kerr 
1073bc53d35aSJeremy Kerr static int __mctp_astlpc_fileio_kcs_read(void *arg,
1074bc53d35aSJeremy Kerr 		enum mctp_binding_astlpc_kcs_reg reg, uint8_t *val)
1075bc53d35aSJeremy Kerr {
1076bc53d35aSJeremy Kerr 	struct mctp_binding_astlpc *astlpc = arg;
1077bc53d35aSJeremy Kerr 	off_t offset = reg;
1078bc53d35aSJeremy Kerr 	int rc;
1079bc53d35aSJeremy Kerr 
1080bc53d35aSJeremy Kerr 	rc = pread(astlpc->kcs_fd, val, 1, offset);
1081bc53d35aSJeremy Kerr 
1082bc53d35aSJeremy Kerr 	return rc == 1 ? 0 : -1;
1083bc53d35aSJeremy Kerr }
1084bc53d35aSJeremy Kerr 
1085bc53d35aSJeremy Kerr static int __mctp_astlpc_fileio_kcs_write(void *arg,
1086bc53d35aSJeremy Kerr 		enum mctp_binding_astlpc_kcs_reg reg, uint8_t val)
1087bc53d35aSJeremy Kerr {
1088bc53d35aSJeremy Kerr 	struct mctp_binding_astlpc *astlpc = arg;
1089bc53d35aSJeremy Kerr 	off_t offset = reg;
1090bc53d35aSJeremy Kerr 	int rc;
1091bc53d35aSJeremy Kerr 
1092bc53d35aSJeremy Kerr 	rc = pwrite(astlpc->kcs_fd, &val, 1, offset);
1093bc53d35aSJeremy Kerr 
1094bc53d35aSJeremy Kerr 	return rc == 1 ? 0 : -1;
1095bc53d35aSJeremy Kerr }
1096bc53d35aSJeremy Kerr 
1097bc53d35aSJeremy Kerr int mctp_astlpc_get_fd(struct mctp_binding_astlpc *astlpc)
1098bc53d35aSJeremy Kerr {
1099bc53d35aSJeremy Kerr 	return astlpc->kcs_fd;
1100bc53d35aSJeremy Kerr }
1101bc53d35aSJeremy Kerr 
1102bc53d35aSJeremy Kerr struct mctp_binding_astlpc *mctp_astlpc_init_fileio(void)
1103672c8852SJeremy Kerr {
1104672c8852SJeremy Kerr 	struct mctp_binding_astlpc *astlpc;
1105672c8852SJeremy Kerr 	int rc;
1106672c8852SJeremy Kerr 
11077cd72f14SAndrew Jeffery 	/*
11087cd72f14SAndrew Jeffery 	 * If we're doing file IO then we're very likely not running
11097cd72f14SAndrew Jeffery 	 * freestanding, so lets assume that we're on the BMC side
11107cd72f14SAndrew Jeffery 	 */
11117cd72f14SAndrew Jeffery 	astlpc = __mctp_astlpc_init(MCTP_BINDING_ASTLPC_MODE_BMC, MCTP_BTU);
1112bc53d35aSJeremy Kerr 	if (!astlpc)
1113bc53d35aSJeremy Kerr 		return NULL;
1114672c8852SJeremy Kerr 
1115bc53d35aSJeremy Kerr 	/* Set internal operations for kcs. We use direct accesses to the lpc
1116bc53d35aSJeremy Kerr 	 * map area */
1117bc53d35aSJeremy Kerr 	astlpc->ops.kcs_read = __mctp_astlpc_fileio_kcs_read;
1118bc53d35aSJeremy Kerr 	astlpc->ops.kcs_write = __mctp_astlpc_fileio_kcs_write;
1119bc53d35aSJeremy Kerr 	astlpc->ops_data = astlpc;
1120bc53d35aSJeremy Kerr 
1121bc53d35aSJeremy Kerr 	rc = mctp_astlpc_init_fileio_lpc(astlpc);
1122672c8852SJeremy Kerr 	if (rc) {
1123672c8852SJeremy Kerr 		free(astlpc);
1124672c8852SJeremy Kerr 		return NULL;
1125672c8852SJeremy Kerr 	}
1126672c8852SJeremy Kerr 
1127bc53d35aSJeremy Kerr 	rc = mctp_astlpc_init_fileio_kcs(astlpc);
1128672c8852SJeremy Kerr 	if (rc) {
1129672c8852SJeremy Kerr 		free(astlpc);
1130672c8852SJeremy Kerr 		return NULL;
1131672c8852SJeremy Kerr 	}
1132672c8852SJeremy Kerr 
1133672c8852SJeremy Kerr 	return astlpc;
1134672c8852SJeremy Kerr }
113592a10a6bSJeremy Kerr #else
113692a10a6bSJeremy Kerr struct mctp_binding_astlpc * __attribute__((const))
113792a10a6bSJeremy Kerr 	mctp_astlpc_init_fileio(void)
113892a10a6bSJeremy Kerr {
11399101a2abSAndrew Jeffery 	astlpc_prerr(astlpc, "Missing support for file IO");
114092a10a6bSJeremy Kerr 	return NULL;
114192a10a6bSJeremy Kerr }
1142672c8852SJeremy Kerr 
114392a10a6bSJeremy Kerr int __attribute__((const)) mctp_astlpc_get_fd(
114492a10a6bSJeremy Kerr 		struct mctp_binding_astlpc *astlpc __attribute__((unused)))
114592a10a6bSJeremy Kerr {
11469101a2abSAndrew Jeffery 	astlpc_prerr(astlpc, "Missing support for file IO");
114792a10a6bSJeremy Kerr 	return -1;
114892a10a6bSJeremy Kerr }
114992a10a6bSJeremy Kerr #endif
1150