xref: /openbmc/libmctp/astlpc.c (revision 45d1332818c13df182242a8cb80cbe9f983fd057)
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 
21eba19a3bSAndrew Jeffery #include "container_of.h"
22eba19a3bSAndrew Jeffery #include "crc32.h"
23672c8852SJeremy Kerr #include "libmctp.h"
24672c8852SJeremy Kerr #include "libmctp-alloc.h"
25672c8852SJeremy Kerr #include "libmctp-log.h"
26672c8852SJeremy Kerr #include "libmctp-astlpc.h"
274622cadfSAndrew Jeffery #include "range.h"
28672c8852SJeremy Kerr 
29b214c643SJeremy Kerr #ifdef MCTP_HAVE_FILEIO
3092a10a6bSJeremy Kerr 
31c6f676d1SJeremy Kerr #include <unistd.h>
3292a10a6bSJeremy Kerr #include <fcntl.h>
331111c6a5SAndrew Jeffery #include <poll.h>
3492a10a6bSJeremy Kerr #include <sys/ioctl.h>
3592a10a6bSJeremy Kerr #include <sys/mman.h>
3692a10a6bSJeremy Kerr #include <linux/aspeed-lpc-ctrl.h>
3792a10a6bSJeremy Kerr 
3892a10a6bSJeremy Kerr /* kernel interface */
3992a10a6bSJeremy Kerr static const char *kcs_path = "/dev/mctp0";
4092a10a6bSJeremy Kerr static const char *lpc_path = "/dev/aspeed-lpc-ctrl";
4192a10a6bSJeremy Kerr 
4292a10a6bSJeremy Kerr #endif
4392a10a6bSJeremy Kerr 
44*45d13328SRashmica Gupta enum mctp_astlpc_cmd {
45*45d13328SRashmica Gupta 	cmd_initialise = 0x00,
46*45d13328SRashmica Gupta 	cmd_tx_begin = 0x01,
47*45d13328SRashmica Gupta 	cmd_rx_complete = 0x02,
48*45d13328SRashmica Gupta 	cmd_dummy_value = 0xff,
49*45d13328SRashmica Gupta };
50*45d13328SRashmica Gupta 
51fe763e98SAndrew Jeffery enum mctp_astlpc_buffer_state {
52fe763e98SAndrew Jeffery 	/*
53fe763e98SAndrew Jeffery 	 * Prior to "Channel Ready" we mark the buffers as "idle" to catch illegal accesses. In this
54fe763e98SAndrew Jeffery 	 * state neither side is considered the owner of the buffer.
55fe763e98SAndrew Jeffery 	 *
56fe763e98SAndrew Jeffery 	 * Upon "Channel Ready", each side transitions the buffers from the initial "idle" state
57fe763e98SAndrew Jeffery 	 * to the following target states:
58fe763e98SAndrew Jeffery 	 *
59fe763e98SAndrew Jeffery 	 * Tx buffer: "acquired"
60fe763e98SAndrew Jeffery 	 * Rx buffer: "released"
61fe763e98SAndrew Jeffery 	 */
62fe763e98SAndrew Jeffery 	buffer_state_idle,
63fe763e98SAndrew Jeffery 
64fe763e98SAndrew Jeffery 	/*
65fe763e98SAndrew Jeffery 	 * Beyond initialisation by "Channel Ready", buffers are in the "acquired" state once:
66fe763e98SAndrew Jeffery 	 *
67fe763e98SAndrew Jeffery 	 * 1. We dequeue a control command transferring the buffer to our ownership out of the KCS
68fe763e98SAndrew Jeffery 	 *    interface, and
69fe763e98SAndrew Jeffery 	 * 2. We are yet to complete all of our required accesses to the buffer
70fe763e98SAndrew Jeffery 	 *
71fe763e98SAndrew Jeffery 	 * * The Tx buffer enters the "acquired" state when we dequeue the "Rx Complete" command
72fe763e98SAndrew Jeffery 	 * * The Rx buffer enters the "acquired" state when we dequeue the "Tx Begin" command
73fe763e98SAndrew Jeffery 	 *
74fe763e98SAndrew Jeffery 	 * It is a failure of implementation if it's possible for both sides to simultaneously
75fe763e98SAndrew Jeffery 	 * consider a buffer as "acquired".
76fe763e98SAndrew Jeffery 	 */
77fe763e98SAndrew Jeffery 	buffer_state_acquired,
78fe763e98SAndrew Jeffery 
79fe763e98SAndrew Jeffery 	/*
80fe763e98SAndrew Jeffery 	 * Buffers are in the "prepared" state when:
81fe763e98SAndrew Jeffery 	 *
82fe763e98SAndrew Jeffery 	 * 1. We have completed all of our required accesses (read or write) for the buffer, and
83fe763e98SAndrew Jeffery 	 * 2. We have not yet successfully enqueued the control command to hand off ownership
84fe763e98SAndrew Jeffery 	 */
85fe763e98SAndrew Jeffery 	buffer_state_prepared,
86fe763e98SAndrew Jeffery 
87fe763e98SAndrew Jeffery 	/*
88fe763e98SAndrew Jeffery 	 * Beyond initialisation by "Channel Ready", buffers are in the "released" state once:
89fe763e98SAndrew Jeffery 	 *
90fe763e98SAndrew Jeffery 	 * 1. We successfully enqueue the control command transferring ownership to the remote
91fe763e98SAndrew Jeffery 	 *    side in to the KCS interface
92fe763e98SAndrew Jeffery 	 *
93fe763e98SAndrew Jeffery 	 * * The Tx buffer enters the "released" state when we enqueue the "Tx Begin" command
94fe763e98SAndrew Jeffery 	 * * The Rx buffer enters the "released" state when we enqueue the "Rx Complete" command
95fe763e98SAndrew Jeffery 	 *
96fe763e98SAndrew Jeffery 	 * It may be the case that both sides simultaneously consider a buffer to be in the
97fe763e98SAndrew Jeffery 	 * "released" state. However, if this is true, it must also be true that a buffer ownership
98fe763e98SAndrew Jeffery 	 * transfer command has been enqueued in the KCS interface and is yet to be dequeued.
99fe763e98SAndrew Jeffery 	 */
100fe763e98SAndrew Jeffery 	buffer_state_released,
101fe763e98SAndrew Jeffery };
102fe763e98SAndrew Jeffery 
1037cd72f14SAndrew Jeffery struct mctp_astlpc_buffer {
1047cd72f14SAndrew Jeffery 	uint32_t offset;
1057cd72f14SAndrew Jeffery 	uint32_t size;
106fe763e98SAndrew Jeffery 	enum mctp_astlpc_buffer_state state;
1077cd72f14SAndrew Jeffery };
1087cd72f14SAndrew Jeffery 
1097cd72f14SAndrew Jeffery struct mctp_astlpc_layout {
1107cd72f14SAndrew Jeffery 	struct mctp_astlpc_buffer rx;
1117cd72f14SAndrew Jeffery 	struct mctp_astlpc_buffer tx;
1127cd72f14SAndrew Jeffery };
1137cd72f14SAndrew Jeffery 
11488412be4SAndrew Jeffery struct mctp_astlpc_protocol {
11588412be4SAndrew Jeffery 	uint16_t version;
11688412be4SAndrew Jeffery 	uint32_t (*packet_size)(uint32_t body);
11788412be4SAndrew Jeffery 	uint32_t (*body_size)(uint32_t packet);
118eba19a3bSAndrew Jeffery 	void (*pktbuf_protect)(struct mctp_pktbuf *pkt);
119eba19a3bSAndrew Jeffery 	bool (*pktbuf_validate)(struct mctp_pktbuf *pkt);
12088412be4SAndrew Jeffery };
12188412be4SAndrew Jeffery 
122672c8852SJeremy Kerr struct mctp_binding_astlpc {
123672c8852SJeremy Kerr 	struct mctp_binding binding;
124bc53d35aSJeremy Kerr 
125672c8852SJeremy Kerr 	void *lpc_map;
1267cd72f14SAndrew Jeffery 	struct mctp_astlpc_layout layout;
1277cd72f14SAndrew Jeffery 
1287cd72f14SAndrew Jeffery 	uint8_t mode;
129a9368980SAndrew Jeffery 	uint32_t requested_mtu;
130bc53d35aSJeremy Kerr 
13188412be4SAndrew Jeffery 	const struct mctp_astlpc_protocol *proto;
13288412be4SAndrew Jeffery 
133bc53d35aSJeremy Kerr 	/* direct ops data */
134bc53d35aSJeremy Kerr 	struct mctp_binding_astlpc_ops ops;
135bc53d35aSJeremy Kerr 	void *ops_data;
136bc53d35aSJeremy Kerr 
137bc53d35aSJeremy Kerr 	/* fileio ops data */
138672c8852SJeremy Kerr 	int kcs_fd;
139672c8852SJeremy Kerr 	uint8_t kcs_status;
140672c8852SJeremy Kerr };
141672c8852SJeremy Kerr 
142672c8852SJeremy Kerr #define binding_to_astlpc(b)                                                   \
143672c8852SJeremy Kerr 	container_of(b, struct mctp_binding_astlpc, binding)
144672c8852SJeremy Kerr 
1459101a2abSAndrew Jeffery #define astlpc_prlog(ctx, lvl, fmt, ...)                                       \
1469101a2abSAndrew Jeffery 	do {                                                                   \
1479101a2abSAndrew Jeffery 		bool __bmc = ((ctx)->mode == MCTP_BINDING_ASTLPC_MODE_BMC);    \
1489101a2abSAndrew Jeffery 		mctp_prlog(lvl, pr_fmt("%s: " fmt), __bmc ? "bmc" : "host",    \
1499101a2abSAndrew Jeffery 			   ##__VA_ARGS__);                                     \
1509101a2abSAndrew Jeffery 	} while (0)
1519101a2abSAndrew Jeffery 
1529101a2abSAndrew Jeffery #define astlpc_prerr(ctx, fmt, ...)                                            \
1539101a2abSAndrew Jeffery 	astlpc_prlog(ctx, MCTP_LOG_ERR, fmt, ##__VA_ARGS__)
1549101a2abSAndrew Jeffery #define astlpc_prwarn(ctx, fmt, ...)                                           \
1559101a2abSAndrew Jeffery 	astlpc_prlog(ctx, MCTP_LOG_WARNING, fmt, ##__VA_ARGS__)
1569101a2abSAndrew Jeffery #define astlpc_prinfo(ctx, fmt, ...)                                           \
1579101a2abSAndrew Jeffery 	astlpc_prlog(ctx, MCTP_LOG_INFO, fmt, ##__VA_ARGS__)
1589101a2abSAndrew Jeffery #define astlpc_prdebug(ctx, fmt, ...)                                          \
1599101a2abSAndrew Jeffery 	astlpc_prlog(ctx, MCTP_LOG_DEBUG, fmt, ##__VA_ARGS__)
1609101a2abSAndrew Jeffery 
1617cd72f14SAndrew Jeffery /* clang-format off */
1627cd72f14SAndrew Jeffery #define ASTLPC_MCTP_MAGIC	0x4d435450
1634e8264b7SAndrew Jeffery #define ASTLPC_VER_BAD	0
1647cd72f14SAndrew Jeffery #define ASTLPC_VER_MIN	1
1657cd72f14SAndrew Jeffery 
1663a540664SAndrew Jeffery /* Support testing of new binding protocols */
1673a540664SAndrew Jeffery #ifndef ASTLPC_VER_CUR
168eba19a3bSAndrew Jeffery #define ASTLPC_VER_CUR	3
1693a540664SAndrew Jeffery #endif
1707cd72f14SAndrew Jeffery /* clang-format on */
171672c8852SJeremy Kerr 
17288412be4SAndrew Jeffery #ifndef ARRAY_SIZE
17388412be4SAndrew Jeffery #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
17488412be4SAndrew Jeffery #endif
17588412be4SAndrew Jeffery 
17688412be4SAndrew Jeffery static uint32_t astlpc_packet_size_v1(uint32_t body)
17788412be4SAndrew Jeffery {
17888412be4SAndrew Jeffery 	assert((body + 4) > body);
17988412be4SAndrew Jeffery 
18088412be4SAndrew Jeffery 	return body + 4;
18188412be4SAndrew Jeffery }
18288412be4SAndrew Jeffery 
18388412be4SAndrew Jeffery static uint32_t astlpc_body_size_v1(uint32_t packet)
18488412be4SAndrew Jeffery {
18588412be4SAndrew Jeffery 	assert((packet - 4) < packet);
18688412be4SAndrew Jeffery 
18788412be4SAndrew Jeffery 	return packet - 4;
18888412be4SAndrew Jeffery }
18988412be4SAndrew Jeffery 
190eba19a3bSAndrew Jeffery void astlpc_pktbuf_protect_v1(struct mctp_pktbuf *pkt)
191eba19a3bSAndrew Jeffery {
192eba19a3bSAndrew Jeffery 	(void)pkt;
193eba19a3bSAndrew Jeffery }
194eba19a3bSAndrew Jeffery 
195eba19a3bSAndrew Jeffery bool astlpc_pktbuf_validate_v1(struct mctp_pktbuf *pkt)
196eba19a3bSAndrew Jeffery {
197eba19a3bSAndrew Jeffery 	(void)pkt;
198eba19a3bSAndrew Jeffery 	return true;
199eba19a3bSAndrew Jeffery }
200eba19a3bSAndrew Jeffery 
201eba19a3bSAndrew Jeffery static uint32_t astlpc_packet_size_v3(uint32_t body)
202eba19a3bSAndrew Jeffery {
203eba19a3bSAndrew Jeffery 	assert((body + 4 + 4) > body);
204eba19a3bSAndrew Jeffery 
205eba19a3bSAndrew Jeffery 	return body + 4 + 4;
206eba19a3bSAndrew Jeffery }
207eba19a3bSAndrew Jeffery 
208eba19a3bSAndrew Jeffery static uint32_t astlpc_body_size_v3(uint32_t packet)
209eba19a3bSAndrew Jeffery {
210eba19a3bSAndrew Jeffery 	assert((packet - 4 - 4) < packet);
211eba19a3bSAndrew Jeffery 
212eba19a3bSAndrew Jeffery 	return packet - 4 - 4;
213eba19a3bSAndrew Jeffery }
214eba19a3bSAndrew Jeffery 
215eba19a3bSAndrew Jeffery void astlpc_pktbuf_protect_v3(struct mctp_pktbuf *pkt)
216eba19a3bSAndrew Jeffery {
217eba19a3bSAndrew Jeffery 	uint32_t code;
218eba19a3bSAndrew Jeffery 
219eba19a3bSAndrew Jeffery 	code = htobe32(crc32(mctp_pktbuf_hdr(pkt), mctp_pktbuf_size(pkt)));
220eba19a3bSAndrew Jeffery 	mctp_prdebug("%s: 0x%" PRIx32, __func__, code);
221eba19a3bSAndrew Jeffery 	mctp_pktbuf_push(pkt, &code, 4);
222eba19a3bSAndrew Jeffery }
223eba19a3bSAndrew Jeffery 
224eba19a3bSAndrew Jeffery bool astlpc_pktbuf_validate_v3(struct mctp_pktbuf *pkt)
225eba19a3bSAndrew Jeffery {
226eba19a3bSAndrew Jeffery 	uint32_t code;
227eba19a3bSAndrew Jeffery 	void *check;
228eba19a3bSAndrew Jeffery 
229eba19a3bSAndrew Jeffery 	code = be32toh(crc32(mctp_pktbuf_hdr(pkt), mctp_pktbuf_size(pkt) - 4));
230eba19a3bSAndrew Jeffery 	mctp_prdebug("%s: 0x%" PRIx32, __func__, code);
231eba19a3bSAndrew Jeffery 	check = mctp_pktbuf_pop(pkt, 4);
232eba19a3bSAndrew Jeffery 	return check && !memcmp(&code, check, 4);
233eba19a3bSAndrew Jeffery }
234eba19a3bSAndrew Jeffery 
23588412be4SAndrew Jeffery static const struct mctp_astlpc_protocol astlpc_protocol_version[] = {
23688412be4SAndrew Jeffery 	[0] = {
23788412be4SAndrew Jeffery 		.version = 0,
23888412be4SAndrew Jeffery 		.packet_size = NULL,
23988412be4SAndrew Jeffery 		.body_size = NULL,
240eba19a3bSAndrew Jeffery 		.pktbuf_protect = NULL,
241eba19a3bSAndrew Jeffery 		.pktbuf_validate = NULL,
24288412be4SAndrew Jeffery 	},
24388412be4SAndrew Jeffery 	[1] = {
24488412be4SAndrew Jeffery 		.version = 1,
24588412be4SAndrew Jeffery 		.packet_size = astlpc_packet_size_v1,
24688412be4SAndrew Jeffery 		.body_size = astlpc_body_size_v1,
247eba19a3bSAndrew Jeffery 		.pktbuf_protect = astlpc_pktbuf_protect_v1,
248eba19a3bSAndrew Jeffery 		.pktbuf_validate = astlpc_pktbuf_validate_v1,
24988412be4SAndrew Jeffery 	},
25088412be4SAndrew Jeffery 	[2] = {
25188412be4SAndrew Jeffery 		.version = 2,
25288412be4SAndrew Jeffery 		.packet_size = astlpc_packet_size_v1,
25388412be4SAndrew Jeffery 		.body_size = astlpc_body_size_v1,
254eba19a3bSAndrew Jeffery 		.pktbuf_protect = astlpc_pktbuf_protect_v1,
255eba19a3bSAndrew Jeffery 		.pktbuf_validate = astlpc_pktbuf_validate_v1,
256eba19a3bSAndrew Jeffery 	},
257eba19a3bSAndrew Jeffery 	[3] = {
258eba19a3bSAndrew Jeffery 		.version = 3,
259eba19a3bSAndrew Jeffery 		.packet_size = astlpc_packet_size_v3,
260eba19a3bSAndrew Jeffery 		.body_size = astlpc_body_size_v3,
261eba19a3bSAndrew Jeffery 		.pktbuf_protect = astlpc_pktbuf_protect_v3,
262eba19a3bSAndrew Jeffery 		.pktbuf_validate = astlpc_pktbuf_validate_v3,
26388412be4SAndrew Jeffery 	},
26488412be4SAndrew Jeffery };
26588412be4SAndrew Jeffery 
266672c8852SJeremy Kerr struct mctp_lpcmap_hdr {
267672c8852SJeremy Kerr 	uint32_t magic;
268672c8852SJeremy Kerr 
269672c8852SJeremy Kerr 	uint16_t bmc_ver_min;
270672c8852SJeremy Kerr 	uint16_t bmc_ver_cur;
271672c8852SJeremy Kerr 	uint16_t host_ver_min;
272672c8852SJeremy Kerr 	uint16_t host_ver_cur;
273672c8852SJeremy Kerr 	uint16_t negotiated_ver;
274672c8852SJeremy Kerr 	uint16_t pad0;
275672c8852SJeremy Kerr 
2763a540664SAndrew Jeffery 	struct {
277672c8852SJeremy Kerr 		uint32_t rx_offset;
278672c8852SJeremy Kerr 		uint32_t rx_size;
279672c8852SJeremy Kerr 		uint32_t tx_offset;
280672c8852SJeremy Kerr 		uint32_t tx_size;
2813a540664SAndrew Jeffery 	} layout;
282672c8852SJeremy Kerr } __attribute__((packed));
283672c8852SJeremy Kerr 
2843a540664SAndrew Jeffery static const uint32_t control_size = 0x100;
285672c8852SJeremy Kerr 
286672c8852SJeremy Kerr #define LPC_WIN_SIZE (1 * 1024 * 1024)
287672c8852SJeremy Kerr 
288672c8852SJeremy Kerr #define KCS_STATUS_BMC_READY	  0x80
289672c8852SJeremy Kerr #define KCS_STATUS_CHANNEL_ACTIVE 0x40
290672c8852SJeremy Kerr #define KCS_STATUS_IBF		  0x02
291672c8852SJeremy Kerr #define KCS_STATUS_OBF		  0x01
292672c8852SJeremy Kerr 
293f13cb971SAndrew Jeffery static inline int mctp_astlpc_kcs_write(struct mctp_binding_astlpc *astlpc,
294f13cb971SAndrew Jeffery 					enum mctp_binding_astlpc_kcs_reg reg,
295f13cb971SAndrew Jeffery 					uint8_t val)
296f13cb971SAndrew Jeffery {
297f13cb971SAndrew Jeffery 	return astlpc->ops.kcs_write(astlpc->ops_data, reg, val);
298f13cb971SAndrew Jeffery }
299f13cb971SAndrew Jeffery 
300f13cb971SAndrew Jeffery static inline int mctp_astlpc_kcs_read(struct mctp_binding_astlpc *astlpc,
301f13cb971SAndrew Jeffery 				       enum mctp_binding_astlpc_kcs_reg reg,
302f13cb971SAndrew Jeffery 				       uint8_t *val)
303f13cb971SAndrew Jeffery {
304f13cb971SAndrew Jeffery 	return astlpc->ops.kcs_read(astlpc->ops_data, reg, val);
305f13cb971SAndrew Jeffery }
306f13cb971SAndrew Jeffery 
30755fb90beSAndrew Jeffery static inline int mctp_astlpc_lpc_write(struct mctp_binding_astlpc *astlpc,
30855fb90beSAndrew Jeffery 					const void *buf, long offset,
30955fb90beSAndrew Jeffery 					size_t len)
310bc53d35aSJeremy Kerr {
31155fb90beSAndrew Jeffery 	astlpc_prdebug(astlpc, "%s: %zu bytes to 0x%lx", __func__, len, offset);
31255fb90beSAndrew Jeffery 
31355fb90beSAndrew Jeffery 	assert(offset >= 0);
31455fb90beSAndrew Jeffery 
31555fb90beSAndrew Jeffery 	/* Indirect access */
31655fb90beSAndrew Jeffery 	if (astlpc->ops.lpc_write) {
31755fb90beSAndrew Jeffery 		void *data = astlpc->ops_data;
31855fb90beSAndrew Jeffery 
31955fb90beSAndrew Jeffery 		return astlpc->ops.lpc_write(data, buf, offset, len);
32055fb90beSAndrew Jeffery 	}
32155fb90beSAndrew Jeffery 
32255fb90beSAndrew Jeffery 	/* Direct mapping */
32355fb90beSAndrew Jeffery 	assert(astlpc->lpc_map);
32455fb90beSAndrew Jeffery 	memcpy(&((char *)astlpc->lpc_map)[offset], buf, len);
32555fb90beSAndrew Jeffery 
32655fb90beSAndrew Jeffery 	return 0;
32755fb90beSAndrew Jeffery }
32855fb90beSAndrew Jeffery 
32955fb90beSAndrew Jeffery static inline int mctp_astlpc_lpc_read(struct mctp_binding_astlpc *astlpc,
33055fb90beSAndrew Jeffery 				       void *buf, long offset, size_t len)
33155fb90beSAndrew Jeffery {
33255fb90beSAndrew Jeffery 	astlpc_prdebug(astlpc, "%s: %zu bytes from 0x%lx", __func__, len,
33355fb90beSAndrew Jeffery 		       offset);
33455fb90beSAndrew Jeffery 
33555fb90beSAndrew Jeffery 	assert(offset >= 0);
33655fb90beSAndrew Jeffery 
33755fb90beSAndrew Jeffery 	/* Indirect access */
33855fb90beSAndrew Jeffery 	if (astlpc->ops.lpc_read) {
33955fb90beSAndrew Jeffery 		void *data = astlpc->ops_data;
34055fb90beSAndrew Jeffery 
34155fb90beSAndrew Jeffery 		return astlpc->ops.lpc_read(data, buf, offset, len);
34255fb90beSAndrew Jeffery 	}
34355fb90beSAndrew Jeffery 
34455fb90beSAndrew Jeffery 	/* Direct mapping */
34555fb90beSAndrew Jeffery 	assert(astlpc->lpc_map);
34655fb90beSAndrew Jeffery 	memcpy(buf, &((char *)astlpc->lpc_map)[offset], len);
34755fb90beSAndrew Jeffery 
34855fb90beSAndrew Jeffery 	return 0;
349bc53d35aSJeremy Kerr }
350bc53d35aSJeremy Kerr 
351d0f5da0dSAndrew Jeffery static int mctp_astlpc_kcs_set_status(struct mctp_binding_astlpc *astlpc,
352d0f5da0dSAndrew Jeffery 				      uint8_t status)
353d0f5da0dSAndrew Jeffery {
354d0f5da0dSAndrew Jeffery 	uint8_t data;
355d0f5da0dSAndrew Jeffery 	int rc;
356d0f5da0dSAndrew Jeffery 
357d0f5da0dSAndrew Jeffery 	/* Since we're setting the status register, we want the other endpoint
358d0f5da0dSAndrew Jeffery 	 * to be interrupted. However, some hardware may only raise a host-side
359d0f5da0dSAndrew Jeffery 	 * interrupt on an ODR event.
360d0f5da0dSAndrew Jeffery 	 * So, write a dummy value of 0xff to ODR, which will ensure that an
361d0f5da0dSAndrew Jeffery 	 * interrupt is triggered, and can be ignored by the host.
362d0f5da0dSAndrew Jeffery 	 */
363*45d13328SRashmica Gupta 	data = cmd_dummy_value;
364d0f5da0dSAndrew Jeffery 
365f13cb971SAndrew Jeffery 	rc = mctp_astlpc_kcs_write(astlpc, MCTP_ASTLPC_KCS_REG_STATUS, status);
366d0f5da0dSAndrew Jeffery 	if (rc) {
367d0f5da0dSAndrew Jeffery 		astlpc_prwarn(astlpc, "KCS status write failed");
368d0f5da0dSAndrew Jeffery 		return -1;
369d0f5da0dSAndrew Jeffery 	}
370d0f5da0dSAndrew Jeffery 
371f13cb971SAndrew Jeffery 	rc = mctp_astlpc_kcs_write(astlpc, MCTP_ASTLPC_KCS_REG_DATA, data);
372d0f5da0dSAndrew Jeffery 	if (rc) {
373d0f5da0dSAndrew Jeffery 		astlpc_prwarn(astlpc, "KCS dummy data write failed");
374d0f5da0dSAndrew Jeffery 		return -1;
375d0f5da0dSAndrew Jeffery 	}
376d0f5da0dSAndrew Jeffery 
377d0f5da0dSAndrew Jeffery 	return 0;
378d0f5da0dSAndrew Jeffery }
379d0f5da0dSAndrew Jeffery 
3803a540664SAndrew Jeffery static int mctp_astlpc_layout_read(struct mctp_binding_astlpc *astlpc,
3813a540664SAndrew Jeffery 				   struct mctp_astlpc_layout *layout)
3823a540664SAndrew Jeffery {
3833a540664SAndrew Jeffery 	struct mctp_lpcmap_hdr hdr;
3843a540664SAndrew Jeffery 	int rc;
3853a540664SAndrew Jeffery 
3863a540664SAndrew Jeffery 	rc = mctp_astlpc_lpc_read(astlpc, &hdr, 0, sizeof(hdr));
3873a540664SAndrew Jeffery 	if (rc < 0)
3883a540664SAndrew Jeffery 		return rc;
3893a540664SAndrew Jeffery 
3903a540664SAndrew Jeffery 	/* Flip the buffers as the names are defined in terms of the host */
3913a540664SAndrew Jeffery 	if (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_BMC) {
3923a540664SAndrew Jeffery 		layout->rx.offset = be32toh(hdr.layout.tx_offset);
3933a540664SAndrew Jeffery 		layout->rx.size = be32toh(hdr.layout.tx_size);
3943a540664SAndrew Jeffery 		layout->tx.offset = be32toh(hdr.layout.rx_offset);
3953a540664SAndrew Jeffery 		layout->tx.size = be32toh(hdr.layout.rx_size);
3963a540664SAndrew Jeffery 	} else {
3973a540664SAndrew Jeffery 		assert(astlpc->mode == MCTP_BINDING_ASTLPC_MODE_HOST);
3983a540664SAndrew Jeffery 
3993a540664SAndrew Jeffery 		layout->rx.offset = be32toh(hdr.layout.rx_offset);
4003a540664SAndrew Jeffery 		layout->rx.size = be32toh(hdr.layout.rx_size);
4013a540664SAndrew Jeffery 		layout->tx.offset = be32toh(hdr.layout.tx_offset);
4023a540664SAndrew Jeffery 		layout->tx.size = be32toh(hdr.layout.tx_size);
4033a540664SAndrew Jeffery 	}
4043a540664SAndrew Jeffery 
4053a540664SAndrew Jeffery 	return 0;
4063a540664SAndrew Jeffery }
4073a540664SAndrew Jeffery 
4083a540664SAndrew Jeffery static int mctp_astlpc_layout_write(struct mctp_binding_astlpc *astlpc,
4093a540664SAndrew Jeffery 				    struct mctp_astlpc_layout *layout)
4103a540664SAndrew Jeffery {
4113a540664SAndrew Jeffery 	uint32_t rx_size_be;
4123a540664SAndrew Jeffery 
4133a540664SAndrew Jeffery 	if (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_BMC) {
4143a540664SAndrew Jeffery 		struct mctp_lpcmap_hdr hdr;
4153a540664SAndrew Jeffery 
4163a540664SAndrew Jeffery 		/*
4173a540664SAndrew Jeffery 		 * Flip the buffers as the names are defined in terms of the
4183a540664SAndrew Jeffery 		 * host
4193a540664SAndrew Jeffery 		 */
4203a540664SAndrew Jeffery 		hdr.layout.rx_offset = htobe32(layout->tx.offset);
4213a540664SAndrew Jeffery 		hdr.layout.rx_size = htobe32(layout->tx.size);
4223a540664SAndrew Jeffery 		hdr.layout.tx_offset = htobe32(layout->rx.offset);
4233a540664SAndrew Jeffery 		hdr.layout.tx_size = htobe32(layout->rx.size);
4243a540664SAndrew Jeffery 
4253a540664SAndrew Jeffery 		return mctp_astlpc_lpc_write(astlpc, &hdr.layout,
426a721c2d8SPatrick Williams 					     offsetof(struct mctp_lpcmap_hdr,
427a721c2d8SPatrick Williams 						      layout),
4283a540664SAndrew Jeffery 					     sizeof(hdr.layout));
4293a540664SAndrew Jeffery 	}
4303a540664SAndrew Jeffery 
4313a540664SAndrew Jeffery 	assert(astlpc->mode == MCTP_BINDING_ASTLPC_MODE_HOST);
4323a540664SAndrew Jeffery 
4333a540664SAndrew Jeffery 	/*
4343a540664SAndrew Jeffery 	 * As of v2 we only need to write rx_size - the offsets are controlled
4353a540664SAndrew Jeffery 	 * by the BMC, as is the BMC's rx_size (host tx_size).
4363a540664SAndrew Jeffery 	 */
4373a540664SAndrew Jeffery 	rx_size_be = htobe32(layout->rx.size);
4383a540664SAndrew Jeffery 	return mctp_astlpc_lpc_write(astlpc, &rx_size_be,
439a721c2d8SPatrick Williams 				     offsetof(struct mctp_lpcmap_hdr,
440a721c2d8SPatrick Williams 					      layout.rx_size),
4413a540664SAndrew Jeffery 				     sizeof(rx_size_be));
4423a540664SAndrew Jeffery }
4433a540664SAndrew Jeffery 
44488412be4SAndrew Jeffery static bool
44588412be4SAndrew Jeffery mctp_astlpc_buffer_validate(const struct mctp_binding_astlpc *astlpc,
44688412be4SAndrew Jeffery 			    const struct mctp_astlpc_buffer *buf,
4473a540664SAndrew Jeffery 			    const char *name)
4483a540664SAndrew Jeffery {
4493a540664SAndrew Jeffery 	/* Check for overflow */
4503a540664SAndrew Jeffery 	if (buf->offset + buf->size < buf->offset) {
4513a540664SAndrew Jeffery 		mctp_prerr(
4523a540664SAndrew Jeffery 			"%s packet buffer parameters overflow: offset: 0x%" PRIx32
4533a540664SAndrew Jeffery 			", size: %" PRIu32,
4543a540664SAndrew Jeffery 			name, buf->offset, buf->size);
4553a540664SAndrew Jeffery 		return false;
4563a540664SAndrew Jeffery 	}
4573a540664SAndrew Jeffery 
4583a540664SAndrew Jeffery 	/* Check that the buffers are contained within the allocated space */
4593a540664SAndrew Jeffery 	if (buf->offset + buf->size > LPC_WIN_SIZE) {
4603a540664SAndrew Jeffery 		mctp_prerr(
4613a540664SAndrew Jeffery 			"%s packet buffer parameters exceed %uM window size: offset: 0x%" PRIx32
4623a540664SAndrew Jeffery 			", size: %" PRIu32,
4633a540664SAndrew Jeffery 			name, (LPC_WIN_SIZE / (1024 * 1024)), buf->offset,
4643a540664SAndrew Jeffery 			buf->size);
4653a540664SAndrew Jeffery 		return false;
4663a540664SAndrew Jeffery 	}
4673a540664SAndrew Jeffery 
4683a540664SAndrew Jeffery 	/* Check that the baseline transmission unit is supported */
469a721c2d8SPatrick Williams 	if (buf->size <
470a721c2d8SPatrick Williams 	    astlpc->proto->packet_size(MCTP_PACKET_SIZE(MCTP_BTU))) {
4713a540664SAndrew Jeffery 		mctp_prerr(
472a721c2d8SPatrick Williams 			"%s packet buffer too small: Require %" PRIu32
473a721c2d8SPatrick Williams 			" bytes to support the %u byte baseline transmission unit, found %" PRIu32,
47488412be4SAndrew Jeffery 			name,
47588412be4SAndrew Jeffery 			astlpc->proto->packet_size(MCTP_PACKET_SIZE(MCTP_BTU)),
4763a540664SAndrew Jeffery 			MCTP_BTU, buf->size);
4773a540664SAndrew Jeffery 		return false;
4783a540664SAndrew Jeffery 	}
4793a540664SAndrew Jeffery 
4803a540664SAndrew Jeffery 	/* Check for overlap with the control space */
4813a540664SAndrew Jeffery 	if (buf->offset < control_size) {
4823a540664SAndrew Jeffery 		mctp_prerr(
4833a540664SAndrew Jeffery 			"%s packet buffer overlaps control region {0x%" PRIx32
4843a540664SAndrew Jeffery 			", %" PRIu32 "}: Rx {0x%" PRIx32 ", %" PRIu32 "}",
4853a540664SAndrew Jeffery 			name, 0U, control_size, buf->offset, buf->size);
4863a540664SAndrew Jeffery 		return false;
4873a540664SAndrew Jeffery 	}
4883a540664SAndrew Jeffery 
4893a540664SAndrew Jeffery 	return true;
4903a540664SAndrew Jeffery }
4913a540664SAndrew Jeffery 
49288412be4SAndrew Jeffery static bool
49388412be4SAndrew Jeffery mctp_astlpc_layout_validate(const struct mctp_binding_astlpc *astlpc,
49488412be4SAndrew Jeffery 			    const struct mctp_astlpc_layout *layout)
4953a540664SAndrew Jeffery {
49688412be4SAndrew Jeffery 	const struct mctp_astlpc_buffer *rx = &layout->rx;
49788412be4SAndrew Jeffery 	const struct mctp_astlpc_buffer *tx = &layout->tx;
4983a540664SAndrew Jeffery 	bool rx_valid, tx_valid;
4993a540664SAndrew Jeffery 
50088412be4SAndrew Jeffery 	rx_valid = mctp_astlpc_buffer_validate(astlpc, rx, "Rx");
50188412be4SAndrew Jeffery 	tx_valid = mctp_astlpc_buffer_validate(astlpc, tx, "Tx");
5023a540664SAndrew Jeffery 
5033a540664SAndrew Jeffery 	if (!(rx_valid && tx_valid))
5043a540664SAndrew Jeffery 		return false;
5053a540664SAndrew Jeffery 
5063a540664SAndrew Jeffery 	/* Check that the buffers are disjoint */
5073a540664SAndrew Jeffery 	if ((rx->offset <= tx->offset && rx->offset + rx->size > tx->offset) ||
5083a540664SAndrew Jeffery 	    (tx->offset <= rx->offset && tx->offset + tx->size > rx->offset)) {
5093a540664SAndrew Jeffery 		mctp_prerr("Rx and Tx packet buffers overlap: Rx {0x%" PRIx32
5103a540664SAndrew Jeffery 			   ", %" PRIu32 "}, Tx {0x%" PRIx32 ", %" PRIu32 "}",
5113a540664SAndrew Jeffery 			   rx->offset, rx->size, tx->offset, tx->size);
5123a540664SAndrew Jeffery 		return false;
5133a540664SAndrew Jeffery 	}
5143a540664SAndrew Jeffery 
5153a540664SAndrew Jeffery 	return true;
5163a540664SAndrew Jeffery }
5173a540664SAndrew Jeffery 
5187cd72f14SAndrew Jeffery static int mctp_astlpc_init_bmc(struct mctp_binding_astlpc *astlpc)
5197cd72f14SAndrew Jeffery {
52055fb90beSAndrew Jeffery 	struct mctp_lpcmap_hdr hdr = { 0 };
5217cd72f14SAndrew Jeffery 	uint8_t status;
52288412be4SAndrew Jeffery 	uint32_t sz;
5233a540664SAndrew Jeffery 
5243a540664SAndrew Jeffery 	/*
5253a540664SAndrew Jeffery 	 * The largest buffer size is half of the allocated MCTP space
5263a540664SAndrew Jeffery 	 * excluding the control space.
5273a540664SAndrew Jeffery 	 */
5283a540664SAndrew Jeffery 	sz = ((LPC_WIN_SIZE - control_size) / 2);
5293a540664SAndrew Jeffery 
5303a540664SAndrew Jeffery 	/*
5313a540664SAndrew Jeffery 	 * Trim the MTU to a multiple of 16 to meet the requirements of 12.17
5323a540664SAndrew Jeffery 	 * Query Hop in DSP0236 v1.3.0.
5333a540664SAndrew Jeffery 	 */
53488412be4SAndrew Jeffery 	sz = MCTP_BODY_SIZE(astlpc->proto->body_size(sz));
5353a540664SAndrew Jeffery 	sz &= ~0xfUL;
53688412be4SAndrew Jeffery 	sz = astlpc->proto->packet_size(MCTP_PACKET_SIZE(sz));
5377cd72f14SAndrew Jeffery 
538a9368980SAndrew Jeffery 	if (astlpc->requested_mtu) {
53988412be4SAndrew Jeffery 		uint32_t rpkt, rmtu;
540a9368980SAndrew Jeffery 
54188412be4SAndrew Jeffery 		rmtu = astlpc->requested_mtu;
54288412be4SAndrew Jeffery 		rpkt = astlpc->proto->packet_size(MCTP_PACKET_SIZE(rmtu));
54388412be4SAndrew Jeffery 		sz = MIN(sz, rpkt);
544a9368980SAndrew Jeffery 	}
545a9368980SAndrew Jeffery 
5467cd72f14SAndrew Jeffery 	/* Flip the buffers as the names are defined in terms of the host */
5473a540664SAndrew Jeffery 	astlpc->layout.tx.offset = control_size;
5483a540664SAndrew Jeffery 	astlpc->layout.tx.size = sz;
5493a540664SAndrew Jeffery 	astlpc->layout.rx.offset =
5503a540664SAndrew Jeffery 		astlpc->layout.tx.offset + astlpc->layout.tx.size;
5513a540664SAndrew Jeffery 	astlpc->layout.rx.size = sz;
5523a540664SAndrew Jeffery 
55388412be4SAndrew Jeffery 	if (!mctp_astlpc_layout_validate(astlpc, &astlpc->layout)) {
55488412be4SAndrew Jeffery 		astlpc_prerr(astlpc, "Cannot support an MTU of %" PRIu32, sz);
555a9368980SAndrew Jeffery 		return -EINVAL;
556a9368980SAndrew Jeffery 	}
5577cd72f14SAndrew Jeffery 
55855fb90beSAndrew Jeffery 	hdr = (struct mctp_lpcmap_hdr){
55955fb90beSAndrew Jeffery 		.magic = htobe32(ASTLPC_MCTP_MAGIC),
56055fb90beSAndrew Jeffery 		.bmc_ver_min = htobe16(ASTLPC_VER_MIN),
56155fb90beSAndrew Jeffery 		.bmc_ver_cur = htobe16(ASTLPC_VER_CUR),
5627cd72f14SAndrew Jeffery 
5637cd72f14SAndrew Jeffery 		/* Flip the buffers back as we're now describing the host's
5647cd72f14SAndrew Jeffery 		 * configuration to the host */
5653a540664SAndrew Jeffery 		.layout.rx_offset = htobe32(astlpc->layout.tx.offset),
5663a540664SAndrew Jeffery 		.layout.rx_size = htobe32(astlpc->layout.tx.size),
5673a540664SAndrew Jeffery 		.layout.tx_offset = htobe32(astlpc->layout.rx.offset),
5683a540664SAndrew Jeffery 		.layout.tx_size = htobe32(astlpc->layout.rx.size),
56955fb90beSAndrew Jeffery 	};
5707cd72f14SAndrew Jeffery 
57155fb90beSAndrew Jeffery 	mctp_astlpc_lpc_write(astlpc, &hdr, 0, sizeof(hdr));
5727cd72f14SAndrew Jeffery 
573b3b55a6bSAndrew Jeffery 	/*
574b3b55a6bSAndrew Jeffery 	 * Set status indicating that the BMC is now active. Be explicit about
575b3b55a6bSAndrew Jeffery 	 * clearing OBF; we're reinitialising the binding and so any previous
576b3b55a6bSAndrew Jeffery 	 * buffer state is irrelevant.
577b3b55a6bSAndrew Jeffery 	 */
578b3b55a6bSAndrew Jeffery 	status = KCS_STATUS_BMC_READY & ~KCS_STATUS_OBF;
579d0f5da0dSAndrew Jeffery 	return mctp_astlpc_kcs_set_status(astlpc, status);
5807cd72f14SAndrew Jeffery }
5817cd72f14SAndrew Jeffery 
5827cd72f14SAndrew Jeffery static int mctp_binding_astlpc_start_bmc(struct mctp_binding *b)
5837cd72f14SAndrew Jeffery {
5847cd72f14SAndrew Jeffery 	struct mctp_binding_astlpc *astlpc =
5857cd72f14SAndrew Jeffery 		container_of(b, struct mctp_binding_astlpc, binding);
5867cd72f14SAndrew Jeffery 
58788412be4SAndrew Jeffery 	astlpc->proto = &astlpc_protocol_version[ASTLPC_VER_CUR];
58888412be4SAndrew Jeffery 
5897cd72f14SAndrew Jeffery 	return mctp_astlpc_init_bmc(astlpc);
5907cd72f14SAndrew Jeffery }
5917cd72f14SAndrew Jeffery 
5924e8264b7SAndrew Jeffery static bool mctp_astlpc_validate_version(uint16_t bmc_ver_min,
5934e8264b7SAndrew Jeffery 					 uint16_t bmc_ver_cur,
5944e8264b7SAndrew Jeffery 					 uint16_t host_ver_min,
5954e8264b7SAndrew Jeffery 					 uint16_t host_ver_cur)
5964e8264b7SAndrew Jeffery {
5974e8264b7SAndrew Jeffery 	if (!(bmc_ver_min && bmc_ver_cur && host_ver_min && host_ver_cur)) {
5984e8264b7SAndrew Jeffery 		mctp_prerr("Invalid version present in [%" PRIu16 ", %" PRIu16
5994e8264b7SAndrew Jeffery 			   "], [%" PRIu16 ", %" PRIu16 "]",
6004e8264b7SAndrew Jeffery 			   bmc_ver_min, bmc_ver_cur, host_ver_min,
6014e8264b7SAndrew Jeffery 			   host_ver_cur);
6024e8264b7SAndrew Jeffery 		return false;
6034e8264b7SAndrew Jeffery 	} else if (bmc_ver_min > bmc_ver_cur) {
6044e8264b7SAndrew Jeffery 		mctp_prerr("Invalid bmc version range [%" PRIu16 ", %" PRIu16
6054e8264b7SAndrew Jeffery 			   "]",
6064e8264b7SAndrew Jeffery 			   bmc_ver_min, bmc_ver_cur);
6074e8264b7SAndrew Jeffery 		return false;
6084e8264b7SAndrew Jeffery 	} else if (host_ver_min > host_ver_cur) {
6094e8264b7SAndrew Jeffery 		mctp_prerr("Invalid host version range [%" PRIu16 ", %" PRIu16
6104e8264b7SAndrew Jeffery 			   "]",
6114e8264b7SAndrew Jeffery 			   host_ver_min, host_ver_cur);
6124e8264b7SAndrew Jeffery 		return false;
6134e8264b7SAndrew Jeffery 	} else if ((host_ver_cur < bmc_ver_min) ||
6144e8264b7SAndrew Jeffery 		   (host_ver_min > bmc_ver_cur)) {
6154e8264b7SAndrew Jeffery 		mctp_prerr(
6164e8264b7SAndrew Jeffery 			"Unable to satisfy version negotiation with ranges [%" PRIu16
6174e8264b7SAndrew Jeffery 			", %" PRIu16 "] and [%" PRIu16 ", %" PRIu16 "]",
6184e8264b7SAndrew Jeffery 			bmc_ver_min, bmc_ver_cur, host_ver_min, host_ver_cur);
6194e8264b7SAndrew Jeffery 		return false;
6204e8264b7SAndrew Jeffery 	}
6214e8264b7SAndrew Jeffery 
6224e8264b7SAndrew Jeffery 	return true;
6234e8264b7SAndrew Jeffery }
6244e8264b7SAndrew Jeffery 
6253a540664SAndrew Jeffery static int mctp_astlpc_negotiate_layout_host(struct mctp_binding_astlpc *astlpc)
6263a540664SAndrew Jeffery {
6273a540664SAndrew Jeffery 	struct mctp_astlpc_layout layout;
62888412be4SAndrew Jeffery 	uint32_t rmtu;
6293a540664SAndrew Jeffery 	uint32_t sz;
6303a540664SAndrew Jeffery 	int rc;
6313a540664SAndrew Jeffery 
6323a540664SAndrew Jeffery 	rc = mctp_astlpc_layout_read(astlpc, &layout);
6333a540664SAndrew Jeffery 	if (rc < 0)
6343a540664SAndrew Jeffery 		return rc;
6353a540664SAndrew Jeffery 
63688412be4SAndrew Jeffery 	if (!mctp_astlpc_layout_validate(astlpc, &layout)) {
6373a540664SAndrew Jeffery 		astlpc_prerr(
6383a540664SAndrew Jeffery 			astlpc,
6393a540664SAndrew Jeffery 			"BMC provided invalid buffer layout: Rx {0x%" PRIx32
6403a540664SAndrew Jeffery 			", %" PRIu32 "}, Tx {0x%" PRIx32 ", %" PRIu32 "}",
6413a540664SAndrew Jeffery 			layout.rx.offset, layout.rx.size, layout.tx.offset,
6423a540664SAndrew Jeffery 			layout.tx.size);
6433a540664SAndrew Jeffery 		return -EINVAL;
6443a540664SAndrew Jeffery 	}
6453a540664SAndrew Jeffery 
646a9368980SAndrew Jeffery 	astlpc_prinfo(astlpc, "Desire an MTU of %" PRIu32 " bytes",
647a9368980SAndrew Jeffery 		      astlpc->requested_mtu);
648a9368980SAndrew Jeffery 
64988412be4SAndrew Jeffery 	rmtu = astlpc->requested_mtu;
65088412be4SAndrew Jeffery 	sz = astlpc->proto->packet_size(MCTP_PACKET_SIZE(rmtu));
6513a540664SAndrew Jeffery 	layout.rx.size = sz;
6523a540664SAndrew Jeffery 
65388412be4SAndrew Jeffery 	if (!mctp_astlpc_layout_validate(astlpc, &layout)) {
6543a540664SAndrew Jeffery 		astlpc_prerr(
6553a540664SAndrew Jeffery 			astlpc,
6563a540664SAndrew Jeffery 			"Generated invalid buffer layout with size %" PRIu32
6573a540664SAndrew Jeffery 			": Rx {0x%" PRIx32 ", %" PRIu32 "}, Tx {0x%" PRIx32
6583a540664SAndrew Jeffery 			", %" PRIu32 "}",
6593a540664SAndrew Jeffery 			sz, layout.rx.offset, layout.rx.size, layout.tx.offset,
6603a540664SAndrew Jeffery 			layout.tx.size);
6613a540664SAndrew Jeffery 		return -EINVAL;
6623a540664SAndrew Jeffery 	}
6633a540664SAndrew Jeffery 
664a9368980SAndrew Jeffery 	astlpc_prinfo(astlpc, "Requesting MTU of %" PRIu32 " bytes",
665a9368980SAndrew Jeffery 		      astlpc->requested_mtu);
6663a540664SAndrew Jeffery 
6673a540664SAndrew Jeffery 	return mctp_astlpc_layout_write(astlpc, &layout);
6683a540664SAndrew Jeffery }
6693a540664SAndrew Jeffery 
67088412be4SAndrew Jeffery static uint16_t mctp_astlpc_negotiate_version(uint16_t bmc_ver_min,
67188412be4SAndrew Jeffery 					      uint16_t bmc_ver_cur,
67288412be4SAndrew Jeffery 					      uint16_t host_ver_min,
67388412be4SAndrew Jeffery 					      uint16_t host_ver_cur)
67488412be4SAndrew Jeffery {
67588412be4SAndrew Jeffery 	if (!mctp_astlpc_validate_version(bmc_ver_min, bmc_ver_cur,
67688412be4SAndrew Jeffery 					  host_ver_min, host_ver_cur))
67788412be4SAndrew Jeffery 		return ASTLPC_VER_BAD;
67888412be4SAndrew Jeffery 
67988412be4SAndrew Jeffery 	if (bmc_ver_cur < host_ver_cur)
68088412be4SAndrew Jeffery 		return bmc_ver_cur;
68188412be4SAndrew Jeffery 
68288412be4SAndrew Jeffery 	return host_ver_cur;
68388412be4SAndrew Jeffery }
68488412be4SAndrew Jeffery 
6857cd72f14SAndrew Jeffery static int mctp_astlpc_init_host(struct mctp_binding_astlpc *astlpc)
6867cd72f14SAndrew Jeffery {
68755fb90beSAndrew Jeffery 	const uint16_t ver_min_be = htobe16(ASTLPC_VER_MIN);
68855fb90beSAndrew Jeffery 	const uint16_t ver_cur_be = htobe16(ASTLPC_VER_CUR);
68988412be4SAndrew Jeffery 	uint16_t bmc_ver_min, bmc_ver_cur, negotiated;
69055fb90beSAndrew Jeffery 	struct mctp_lpcmap_hdr hdr;
6917cd72f14SAndrew Jeffery 	uint8_t status;
6927cd72f14SAndrew Jeffery 	int rc;
6937cd72f14SAndrew Jeffery 
694f13cb971SAndrew Jeffery 	rc = mctp_astlpc_kcs_read(astlpc, MCTP_ASTLPC_KCS_REG_STATUS, &status);
6957cd72f14SAndrew Jeffery 	if (rc) {
6967cd72f14SAndrew Jeffery 		mctp_prwarn("KCS status read failed");
6977cd72f14SAndrew Jeffery 		return rc;
6987cd72f14SAndrew Jeffery 	}
6997cd72f14SAndrew Jeffery 
7007cd72f14SAndrew Jeffery 	astlpc->kcs_status = status;
7017cd72f14SAndrew Jeffery 
7027cd72f14SAndrew Jeffery 	if (!(status & KCS_STATUS_BMC_READY))
7037cd72f14SAndrew Jeffery 		return -EHOSTDOWN;
7047cd72f14SAndrew Jeffery 
70555fb90beSAndrew Jeffery 	mctp_astlpc_lpc_read(astlpc, &hdr, 0, sizeof(hdr));
7067cd72f14SAndrew Jeffery 
7074e8264b7SAndrew Jeffery 	bmc_ver_min = be16toh(hdr.bmc_ver_min);
7084e8264b7SAndrew Jeffery 	bmc_ver_cur = be16toh(hdr.bmc_ver_cur);
7094e8264b7SAndrew Jeffery 
71088412be4SAndrew Jeffery 	/* Calculate the expected value of negotiated_ver */
711a721c2d8SPatrick Williams 	negotiated = mctp_astlpc_negotiate_version(
712a721c2d8SPatrick Williams 		bmc_ver_min, bmc_ver_cur, ASTLPC_VER_MIN, ASTLPC_VER_CUR);
71388412be4SAndrew Jeffery 	if (!negotiated) {
7144e8264b7SAndrew Jeffery 		astlpc_prerr(astlpc, "Cannot negotiate with invalid versions");
7154e8264b7SAndrew Jeffery 		return -EINVAL;
7164e8264b7SAndrew Jeffery 	}
7174e8264b7SAndrew Jeffery 
71888412be4SAndrew Jeffery 	/* Assign protocol ops so we can calculate the packet buffer sizes */
71988412be4SAndrew Jeffery 	assert(negotiated < ARRAY_SIZE(astlpc_protocol_version));
72088412be4SAndrew Jeffery 	astlpc->proto = &astlpc_protocol_version[negotiated];
72188412be4SAndrew Jeffery 
72288412be4SAndrew Jeffery 	/* Negotiate packet buffers in v2 style if the BMC supports it */
72388412be4SAndrew Jeffery 	if (negotiated >= 2) {
7243a540664SAndrew Jeffery 		rc = mctp_astlpc_negotiate_layout_host(astlpc);
7253a540664SAndrew Jeffery 		if (rc < 0)
7263a540664SAndrew Jeffery 			return rc;
7273a540664SAndrew Jeffery 	}
7283a540664SAndrew Jeffery 
72988412be4SAndrew Jeffery 	/* Advertise the host's supported protocol versions */
73055fb90beSAndrew Jeffery 	mctp_astlpc_lpc_write(astlpc, &ver_min_be,
73155fb90beSAndrew Jeffery 			      offsetof(struct mctp_lpcmap_hdr, host_ver_min),
73255fb90beSAndrew Jeffery 			      sizeof(ver_min_be));
7337cd72f14SAndrew Jeffery 
73455fb90beSAndrew Jeffery 	mctp_astlpc_lpc_write(astlpc, &ver_cur_be,
73555fb90beSAndrew Jeffery 			      offsetof(struct mctp_lpcmap_hdr, host_ver_cur),
73655fb90beSAndrew Jeffery 			      sizeof(ver_cur_be));
7377cd72f14SAndrew Jeffery 
7387cd72f14SAndrew Jeffery 	/* Send channel init command */
739f13cb971SAndrew Jeffery 	rc = mctp_astlpc_kcs_write(astlpc, MCTP_ASTLPC_KCS_REG_DATA, 0x0);
7407cd72f14SAndrew Jeffery 	if (rc) {
7419101a2abSAndrew Jeffery 		astlpc_prwarn(astlpc, "KCS write failed");
7427cd72f14SAndrew Jeffery 	}
7437cd72f14SAndrew Jeffery 
74488412be4SAndrew Jeffery 	/*
74588412be4SAndrew Jeffery 	 * Configure the host so `astlpc->proto->version == 0` holds until we
74688412be4SAndrew Jeffery 	 * receive a subsequent status update from the BMC. Until then,
74788412be4SAndrew Jeffery 	 * `astlpc->proto->version == 0` indicates that we're yet to complete
74888412be4SAndrew Jeffery 	 * the channel initialisation handshake.
74988412be4SAndrew Jeffery 	 *
75088412be4SAndrew Jeffery 	 * When the BMC provides a status update with KCS_STATUS_CHANNEL_ACTIVE
75188412be4SAndrew Jeffery 	 * set we will assign the appropriate protocol ops struct in accordance
75288412be4SAndrew Jeffery 	 * with `negotiated_ver`.
75388412be4SAndrew Jeffery 	 */
75488412be4SAndrew Jeffery 	astlpc->proto = &astlpc_protocol_version[ASTLPC_VER_BAD];
75588412be4SAndrew Jeffery 
7567cd72f14SAndrew Jeffery 	return rc;
7577cd72f14SAndrew Jeffery }
7587cd72f14SAndrew Jeffery 
7597cd72f14SAndrew Jeffery static int mctp_binding_astlpc_start_host(struct mctp_binding *b)
7607cd72f14SAndrew Jeffery {
7617cd72f14SAndrew Jeffery 	struct mctp_binding_astlpc *astlpc =
7627cd72f14SAndrew Jeffery 		container_of(b, struct mctp_binding_astlpc, binding);
7637cd72f14SAndrew Jeffery 
7647cd72f14SAndrew Jeffery 	return mctp_astlpc_init_host(astlpc);
7657cd72f14SAndrew Jeffery }
7667cd72f14SAndrew Jeffery 
7677cd72f14SAndrew Jeffery static bool __mctp_astlpc_kcs_ready(struct mctp_binding_astlpc *astlpc,
7687cd72f14SAndrew Jeffery 				    uint8_t status, bool is_write)
7697cd72f14SAndrew Jeffery {
7707cd72f14SAndrew Jeffery 	bool is_bmc;
7717cd72f14SAndrew Jeffery 	bool ready_state;
7727cd72f14SAndrew Jeffery 	uint8_t flag;
7737cd72f14SAndrew Jeffery 
7747cd72f14SAndrew Jeffery 	is_bmc = (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_BMC);
7757cd72f14SAndrew Jeffery 	flag = (is_bmc ^ is_write) ? KCS_STATUS_IBF : KCS_STATUS_OBF;
7767cd72f14SAndrew Jeffery 	ready_state = is_write ? 0 : 1;
7777cd72f14SAndrew Jeffery 
7787cd72f14SAndrew Jeffery 	return !!(status & flag) == ready_state;
7797cd72f14SAndrew Jeffery }
7807cd72f14SAndrew Jeffery 
7817cd72f14SAndrew Jeffery static inline bool
7827cd72f14SAndrew Jeffery mctp_astlpc_kcs_read_ready(struct mctp_binding_astlpc *astlpc, uint8_t status)
7837cd72f14SAndrew Jeffery {
7847cd72f14SAndrew Jeffery 	return __mctp_astlpc_kcs_ready(astlpc, status, false);
7857cd72f14SAndrew Jeffery }
7867cd72f14SAndrew Jeffery 
7877cd72f14SAndrew Jeffery static inline bool
7887cd72f14SAndrew Jeffery mctp_astlpc_kcs_write_ready(struct mctp_binding_astlpc *astlpc, uint8_t status)
7897cd72f14SAndrew Jeffery {
7907cd72f14SAndrew Jeffery 	return __mctp_astlpc_kcs_ready(astlpc, status, true);
7917cd72f14SAndrew Jeffery }
7927cd72f14SAndrew Jeffery 
793672c8852SJeremy Kerr static int mctp_astlpc_kcs_send(struct mctp_binding_astlpc *astlpc,
794*45d13328SRashmica Gupta 				enum mctp_astlpc_cmd data)
795672c8852SJeremy Kerr {
796672c8852SJeremy Kerr 	uint8_t status;
797672c8852SJeremy Kerr 	int rc;
798672c8852SJeremy Kerr 
799a721c2d8SPatrick Williams 	rc = mctp_astlpc_kcs_read(astlpc, MCTP_ASTLPC_KCS_REG_STATUS, &status);
8001b27fe87SAndrew Jeffery 	if (rc) {
8019101a2abSAndrew Jeffery 		astlpc_prwarn(astlpc, "KCS status read failed");
802fe763e98SAndrew Jeffery 		return -EIO;
803672c8852SJeremy Kerr 	}
804fe763e98SAndrew Jeffery 	if (!mctp_astlpc_kcs_write_ready(astlpc, status))
805fe763e98SAndrew Jeffery 		return -EBUSY;
806672c8852SJeremy Kerr 
807f13cb971SAndrew Jeffery 	rc = mctp_astlpc_kcs_write(astlpc, MCTP_ASTLPC_KCS_REG_DATA, data);
808bc53d35aSJeremy Kerr 	if (rc) {
8099101a2abSAndrew Jeffery 		astlpc_prwarn(astlpc, "KCS data write failed");
810fe763e98SAndrew Jeffery 		return -EIO;
811672c8852SJeremy Kerr 	}
812672c8852SJeremy Kerr 
813672c8852SJeremy Kerr 	return 0;
814672c8852SJeremy Kerr }
815672c8852SJeremy Kerr 
816672c8852SJeremy Kerr static int mctp_binding_astlpc_tx(struct mctp_binding *b,
817672c8852SJeremy Kerr 				  struct mctp_pktbuf *pkt)
818672c8852SJeremy Kerr {
819672c8852SJeremy Kerr 	struct mctp_binding_astlpc *astlpc = binding_to_astlpc(b);
82055fb90beSAndrew Jeffery 	uint32_t len, len_be;
821edfe383fSAndrew Jeffery 	struct mctp_hdr *hdr;
822fe763e98SAndrew Jeffery 	int rc;
823672c8852SJeremy Kerr 
824edfe383fSAndrew Jeffery 	hdr = mctp_pktbuf_hdr(pkt);
825672c8852SJeremy Kerr 	len = mctp_pktbuf_size(pkt);
826edfe383fSAndrew Jeffery 
8279101a2abSAndrew Jeffery 	astlpc_prdebug(astlpc,
8289101a2abSAndrew Jeffery 		       "%s: Transmitting %" PRIu32
8299101a2abSAndrew Jeffery 		       "-byte packet (%hhu, %hhu, 0x%hhx)",
830edfe383fSAndrew Jeffery 		       __func__, len, hdr->src, hdr->dest, hdr->flags_seq_tag);
831edfe383fSAndrew Jeffery 
83288412be4SAndrew Jeffery 	if (len > astlpc->proto->body_size(astlpc->layout.tx.size)) {
8330721f585SAndrew Jeffery 		astlpc_prwarn(astlpc, "invalid TX len %" PRIu32 ": %" PRIu32,
8340721f585SAndrew Jeffery 			      len,
835eba19a3bSAndrew Jeffery 			      astlpc->proto->body_size(astlpc->layout.tx.size));
8360721f585SAndrew Jeffery 		return -EMSGSIZE;
837672c8852SJeremy Kerr 	}
838672c8852SJeremy Kerr 
839fe763e98SAndrew Jeffery 	mctp_binding_set_tx_enabled(b, false);
840fe763e98SAndrew Jeffery 
84155fb90beSAndrew Jeffery 	len_be = htobe32(len);
84255fb90beSAndrew Jeffery 	mctp_astlpc_lpc_write(astlpc, &len_be, astlpc->layout.tx.offset,
84355fb90beSAndrew Jeffery 			      sizeof(len_be));
844eba19a3bSAndrew Jeffery 
845eba19a3bSAndrew Jeffery 	astlpc->proto->pktbuf_protect(pkt);
846eba19a3bSAndrew Jeffery 	len = mctp_pktbuf_size(pkt);
847eba19a3bSAndrew Jeffery 
84855fb90beSAndrew Jeffery 	mctp_astlpc_lpc_write(astlpc, hdr, astlpc->layout.tx.offset + 4, len);
849672c8852SJeremy Kerr 
850fe763e98SAndrew Jeffery 	astlpc->layout.tx.state = buffer_state_prepared;
851672c8852SJeremy Kerr 
852*45d13328SRashmica Gupta 	rc = mctp_astlpc_kcs_send(astlpc, cmd_tx_begin);
853fe763e98SAndrew Jeffery 	if (!rc)
854fe763e98SAndrew Jeffery 		astlpc->layout.tx.state = buffer_state_released;
855eba19a3bSAndrew Jeffery 
856fe763e98SAndrew Jeffery 	return rc == -EBUSY ? 0 : rc;
857672c8852SJeremy Kerr }
858672c8852SJeremy Kerr 
8593a540664SAndrew Jeffery static uint32_t mctp_astlpc_calculate_mtu(struct mctp_binding_astlpc *astlpc,
8603a540664SAndrew Jeffery 					  struct mctp_astlpc_layout *layout)
8613a540664SAndrew Jeffery {
86288412be4SAndrew Jeffery 	uint32_t low, high, limit, rpkt;
8633a540664SAndrew Jeffery 
8643a540664SAndrew Jeffery 	/* Derive the largest MTU the BMC _can_ support */
8653a540664SAndrew Jeffery 	low = MIN(astlpc->layout.rx.offset, astlpc->layout.tx.offset);
8663a540664SAndrew Jeffery 	high = MAX(astlpc->layout.rx.offset, astlpc->layout.tx.offset);
8673a540664SAndrew Jeffery 	limit = high - low;
8683a540664SAndrew Jeffery 
869a9368980SAndrew Jeffery 	/* Determine the largest MTU the BMC _wants_ to support */
870a9368980SAndrew Jeffery 	if (astlpc->requested_mtu) {
87188412be4SAndrew Jeffery 		uint32_t rmtu = astlpc->requested_mtu;
872a9368980SAndrew Jeffery 
87388412be4SAndrew Jeffery 		rpkt = astlpc->proto->packet_size(MCTP_PACKET_SIZE(rmtu));
87488412be4SAndrew Jeffery 		limit = MIN(limit, rpkt);
875a9368980SAndrew Jeffery 	}
8763a540664SAndrew Jeffery 
8773a540664SAndrew Jeffery 	/* Determine the accepted MTU, applied both directions by convention */
87888412be4SAndrew Jeffery 	rpkt = MIN(limit, layout->tx.size);
87988412be4SAndrew Jeffery 	return MCTP_BODY_SIZE(astlpc->proto->body_size(rpkt));
8803a540664SAndrew Jeffery }
8813a540664SAndrew Jeffery 
88288412be4SAndrew Jeffery static int mctp_astlpc_negotiate_layout_bmc(struct mctp_binding_astlpc *astlpc)
8833a540664SAndrew Jeffery {
8843a540664SAndrew Jeffery 	struct mctp_astlpc_layout proposed, pending;
8853a540664SAndrew Jeffery 	uint32_t sz, mtu;
8863a540664SAndrew Jeffery 	int rc;
8873a540664SAndrew Jeffery 
88888412be4SAndrew Jeffery 	/* Do we have a valid protocol version? */
88988412be4SAndrew Jeffery 	if (!astlpc->proto->version)
89088412be4SAndrew Jeffery 		return -EINVAL;
89188412be4SAndrew Jeffery 
8923a540664SAndrew Jeffery 	/* Extract the host's proposed layout */
8933a540664SAndrew Jeffery 	rc = mctp_astlpc_layout_read(astlpc, &proposed);
8943a540664SAndrew Jeffery 	if (rc < 0)
8953a540664SAndrew Jeffery 		return rc;
8963a540664SAndrew Jeffery 
89788412be4SAndrew Jeffery 	/* Do we have a reasonable layout? */
89888412be4SAndrew Jeffery 	if (!mctp_astlpc_layout_validate(astlpc, &proposed))
8993a540664SAndrew Jeffery 		return -EINVAL;
9003a540664SAndrew Jeffery 
9013a540664SAndrew Jeffery 	/* Negotiate the MTU */
9023a540664SAndrew Jeffery 	mtu = mctp_astlpc_calculate_mtu(astlpc, &proposed);
90388412be4SAndrew Jeffery 	sz = astlpc->proto->packet_size(MCTP_PACKET_SIZE(mtu));
9043a540664SAndrew Jeffery 
9053a540664SAndrew Jeffery 	/*
9063a540664SAndrew Jeffery 	 * Use symmetric MTUs by convention and to pass constraints in rx/tx
9073a540664SAndrew Jeffery 	 * functions
9083a540664SAndrew Jeffery 	 */
9093a540664SAndrew Jeffery 	pending = astlpc->layout;
9103a540664SAndrew Jeffery 	pending.tx.size = sz;
9113a540664SAndrew Jeffery 	pending.rx.size = sz;
9123a540664SAndrew Jeffery 
91388412be4SAndrew Jeffery 	if (mctp_astlpc_layout_validate(astlpc, &pending)) {
9143a540664SAndrew Jeffery 		/* We found a sensible Rx MTU, so honour it */
9153a540664SAndrew Jeffery 		astlpc->layout = pending;
9163a540664SAndrew Jeffery 
9173a540664SAndrew Jeffery 		/* Enforce the negotiated MTU */
9183a540664SAndrew Jeffery 		rc = mctp_astlpc_layout_write(astlpc, &astlpc->layout);
9193a540664SAndrew Jeffery 		if (rc < 0)
9203a540664SAndrew Jeffery 			return rc;
9213a540664SAndrew Jeffery 
9223a540664SAndrew Jeffery 		astlpc_prinfo(astlpc, "Negotiated an MTU of %" PRIu32 " bytes",
9233a540664SAndrew Jeffery 			      mtu);
9243a540664SAndrew Jeffery 	} else {
9253a540664SAndrew Jeffery 		astlpc_prwarn(astlpc, "MTU negotiation failed");
9263a540664SAndrew Jeffery 		return -EINVAL;
9273a540664SAndrew Jeffery 	}
9283a540664SAndrew Jeffery 
92988412be4SAndrew Jeffery 	if (astlpc->proto->version >= 2)
9303a540664SAndrew Jeffery 		astlpc->binding.pkt_size = MCTP_PACKET_SIZE(mtu);
9313a540664SAndrew Jeffery 
9323a540664SAndrew Jeffery 	return 0;
9333a540664SAndrew Jeffery }
9343a540664SAndrew Jeffery 
935672c8852SJeremy Kerr static void mctp_astlpc_init_channel(struct mctp_binding_astlpc *astlpc)
936672c8852SJeremy Kerr {
9374e8264b7SAndrew Jeffery 	uint16_t negotiated, negotiated_be;
9384e8264b7SAndrew Jeffery 	struct mctp_lpcmap_hdr hdr;
9394e8264b7SAndrew Jeffery 	uint8_t status;
9403a540664SAndrew Jeffery 	int rc;
9414e8264b7SAndrew Jeffery 
9424e8264b7SAndrew Jeffery 	mctp_astlpc_lpc_read(astlpc, &hdr, 0, sizeof(hdr));
9434e8264b7SAndrew Jeffery 
9444e8264b7SAndrew Jeffery 	/* Version negotiation */
9454e8264b7SAndrew Jeffery 	negotiated =
9464e8264b7SAndrew Jeffery 		mctp_astlpc_negotiate_version(ASTLPC_VER_MIN, ASTLPC_VER_CUR,
9474e8264b7SAndrew Jeffery 					      be16toh(hdr.host_ver_min),
9484e8264b7SAndrew Jeffery 					      be16toh(hdr.host_ver_cur));
9494e8264b7SAndrew Jeffery 
95088412be4SAndrew Jeffery 	/* MTU negotiation requires knowing which protocol we'll use */
95188412be4SAndrew Jeffery 	assert(negotiated < ARRAY_SIZE(astlpc_protocol_version));
95288412be4SAndrew Jeffery 	astlpc->proto = &astlpc_protocol_version[negotiated];
95388412be4SAndrew Jeffery 
9543a540664SAndrew Jeffery 	/* Host Rx MTU negotiation: Failure terminates channel init */
95588412be4SAndrew Jeffery 	rc = mctp_astlpc_negotiate_layout_bmc(astlpc);
9563a540664SAndrew Jeffery 	if (rc < 0)
9573a540664SAndrew Jeffery 		negotiated = ASTLPC_VER_BAD;
9583a540664SAndrew Jeffery 
9594e8264b7SAndrew Jeffery 	/* Populate the negotiated version */
9604e8264b7SAndrew Jeffery 	negotiated_be = htobe16(negotiated);
9614e8264b7SAndrew Jeffery 	mctp_astlpc_lpc_write(astlpc, &negotiated_be,
96255fb90beSAndrew Jeffery 			      offsetof(struct mctp_lpcmap_hdr, negotiated_ver),
9634e8264b7SAndrew Jeffery 			      sizeof(negotiated_be));
96455fb90beSAndrew Jeffery 
965fe763e98SAndrew Jeffery 	/* Track buffer ownership */
966fe763e98SAndrew Jeffery 	astlpc->layout.tx.state = buffer_state_acquired;
967fe763e98SAndrew Jeffery 	astlpc->layout.rx.state = buffer_state_released;
968fe763e98SAndrew Jeffery 
9694e8264b7SAndrew Jeffery 	/* Finalise the configuration */
9704e8264b7SAndrew Jeffery 	status = KCS_STATUS_BMC_READY | KCS_STATUS_OBF;
9714e8264b7SAndrew Jeffery 	if (negotiated > 0) {
9724e8264b7SAndrew Jeffery 		astlpc_prinfo(astlpc, "Negotiated binding version %" PRIu16,
9734e8264b7SAndrew Jeffery 			      negotiated);
9744e8264b7SAndrew Jeffery 		status |= KCS_STATUS_CHANNEL_ACTIVE;
9754e8264b7SAndrew Jeffery 	} else {
97688412be4SAndrew Jeffery 		astlpc_prerr(astlpc, "Failed to initialise channel");
9774e8264b7SAndrew Jeffery 	}
978672c8852SJeremy Kerr 
9794e8264b7SAndrew Jeffery 	mctp_astlpc_kcs_set_status(astlpc, status);
9804e8264b7SAndrew Jeffery 
9814e8264b7SAndrew Jeffery 	mctp_binding_set_tx_enabled(&astlpc->binding,
9824e8264b7SAndrew Jeffery 				    status & KCS_STATUS_CHANNEL_ACTIVE);
983672c8852SJeremy Kerr }
984672c8852SJeremy Kerr 
985672c8852SJeremy Kerr static void mctp_astlpc_rx_start(struct mctp_binding_astlpc *astlpc)
986672c8852SJeremy Kerr {
987672c8852SJeremy Kerr 	struct mctp_pktbuf *pkt;
988f447b4f4SRashmica Gupta 	struct mctp_hdr *hdr;
989eba19a3bSAndrew Jeffery 	uint32_t body, packet;
990672c8852SJeremy Kerr 
991eba19a3bSAndrew Jeffery 	mctp_astlpc_lpc_read(astlpc, &body, astlpc->layout.rx.offset,
992eba19a3bSAndrew Jeffery 			     sizeof(body));
993eba19a3bSAndrew Jeffery 	body = be32toh(body);
994bc53d35aSJeremy Kerr 
995eba19a3bSAndrew Jeffery 	if (body > astlpc->proto->body_size(astlpc->layout.rx.size)) {
996eba19a3bSAndrew Jeffery 		astlpc_prwarn(astlpc, "invalid RX len 0x%x", body);
997672c8852SJeremy Kerr 		return;
998672c8852SJeremy Kerr 	}
999672c8852SJeremy Kerr 
10000f05b6cbSAndrew Jeffery 	if ((size_t)body > astlpc->binding.pkt_size) {
1001eba19a3bSAndrew Jeffery 		astlpc_prwarn(astlpc, "invalid RX len 0x%x", body);
1002672c8852SJeremy Kerr 		return;
1003672c8852SJeremy Kerr 	}
1004672c8852SJeremy Kerr 
1005eba19a3bSAndrew Jeffery 	/* Eliminate the medium-specific header that we just read */
1006eba19a3bSAndrew Jeffery 	packet = astlpc->proto->packet_size(body) - 4;
1007eba19a3bSAndrew Jeffery 	pkt = mctp_pktbuf_alloc(&astlpc->binding, packet);
1008ae59f4feSChristian Geddes 	if (!pkt) {
1009a721c2d8SPatrick Williams 		astlpc_prwarn(astlpc, "unable to allocate pktbuf len 0x%x",
1010a721c2d8SPatrick Williams 			      packet);
1011ae59f4feSChristian Geddes 		return;
1012ae59f4feSChristian Geddes 	}
1013672c8852SJeremy Kerr 
1014eba19a3bSAndrew Jeffery 	/*
1015eba19a3bSAndrew Jeffery 	 * Read payload and medium-specific trailer from immediately after the
1016eba19a3bSAndrew Jeffery 	 * medium-specific header.
1017eba19a3bSAndrew Jeffery 	 */
101855fb90beSAndrew Jeffery 	mctp_astlpc_lpc_read(astlpc, mctp_pktbuf_hdr(pkt),
1019eba19a3bSAndrew Jeffery 			     astlpc->layout.rx.offset + 4, packet);
1020672c8852SJeremy Kerr 
1021fe763e98SAndrew Jeffery 	astlpc->layout.rx.state = buffer_state_prepared;
1022fe763e98SAndrew Jeffery 
1023ae59f4feSChristian Geddes 	/* Inform the other side of the MCTP interface that we have read
1024ae59f4feSChristian Geddes 	 * the packet off the bus before handling the contents of the packet.
1025ae59f4feSChristian Geddes 	 */
1026*45d13328SRashmica Gupta 	if (!mctp_astlpc_kcs_send(astlpc, cmd_rx_complete))
1027fe763e98SAndrew Jeffery 		astlpc->layout.rx.state = buffer_state_released;
1028ae59f4feSChristian Geddes 
1029f447b4f4SRashmica Gupta 	hdr = mctp_pktbuf_hdr(pkt);
1030f447b4f4SRashmica Gupta 	if (hdr->ver != 1) {
1031f447b4f4SRashmica Gupta 		mctp_pktbuf_free(pkt);
1032f447b4f4SRashmica Gupta 		astlpc_prdebug(astlpc, "Dropped packet with invalid version");
1033f447b4f4SRashmica Gupta 		return;
1034f447b4f4SRashmica Gupta 	}
1035f447b4f4SRashmica Gupta 
1036eba19a3bSAndrew Jeffery 	/*
1037eba19a3bSAndrew Jeffery 	 * v3 will validate the CRC32 in the medium-specific trailer and adjust
1038eba19a3bSAndrew Jeffery 	 * the packet size accordingly. On older protocols validation is a no-op
1039eba19a3bSAndrew Jeffery 	 * that always returns true.
1040eba19a3bSAndrew Jeffery 	 */
1041eba19a3bSAndrew Jeffery 	if (astlpc->proto->pktbuf_validate(pkt)) {
1042672c8852SJeremy Kerr 		mctp_bus_rx(&astlpc->binding, pkt);
1043eba19a3bSAndrew Jeffery 	} else {
1044eba19a3bSAndrew Jeffery 		/* TODO: Drop any associated assembly */
1045eba19a3bSAndrew Jeffery 		mctp_pktbuf_free(pkt);
1046eba19a3bSAndrew Jeffery 		astlpc_prdebug(astlpc, "Dropped corrupt packet");
1047eba19a3bSAndrew Jeffery 	}
1048672c8852SJeremy Kerr }
1049672c8852SJeremy Kerr 
1050672c8852SJeremy Kerr static void mctp_astlpc_tx_complete(struct mctp_binding_astlpc *astlpc)
1051672c8852SJeremy Kerr {
1052fe763e98SAndrew Jeffery 	astlpc->layout.tx.state = buffer_state_acquired;
1053672c8852SJeremy Kerr 	mctp_binding_set_tx_enabled(&astlpc->binding, true);
1054672c8852SJeremy Kerr }
1055672c8852SJeremy Kerr 
10564e8264b7SAndrew Jeffery static int mctp_astlpc_finalise_channel(struct mctp_binding_astlpc *astlpc)
10574e8264b7SAndrew Jeffery {
10583a540664SAndrew Jeffery 	struct mctp_astlpc_layout layout;
10594e8264b7SAndrew Jeffery 	uint16_t negotiated;
10604e8264b7SAndrew Jeffery 	int rc;
10614e8264b7SAndrew Jeffery 
10624e8264b7SAndrew Jeffery 	rc = mctp_astlpc_lpc_read(astlpc, &negotiated,
10634e8264b7SAndrew Jeffery 				  offsetof(struct mctp_lpcmap_hdr,
10644e8264b7SAndrew Jeffery 					   negotiated_ver),
10654e8264b7SAndrew Jeffery 				  sizeof(negotiated));
10664e8264b7SAndrew Jeffery 	if (rc < 0)
10674e8264b7SAndrew Jeffery 		return rc;
10684e8264b7SAndrew Jeffery 
10694e8264b7SAndrew Jeffery 	negotiated = be16toh(negotiated);
107088412be4SAndrew Jeffery 	astlpc_prerr(astlpc, "Version negotiation got: %u", negotiated);
10714e8264b7SAndrew Jeffery 
10724e8264b7SAndrew Jeffery 	if (negotiated == ASTLPC_VER_BAD || negotiated < ASTLPC_VER_MIN ||
10734e8264b7SAndrew Jeffery 	    negotiated > ASTLPC_VER_CUR) {
10744e8264b7SAndrew Jeffery 		astlpc_prerr(astlpc, "Failed to negotiate version, got: %u\n",
10754e8264b7SAndrew Jeffery 			     negotiated);
10764e8264b7SAndrew Jeffery 		return -EINVAL;
10774e8264b7SAndrew Jeffery 	}
10784e8264b7SAndrew Jeffery 
107988412be4SAndrew Jeffery 	assert(negotiated < ARRAY_SIZE(astlpc_protocol_version));
108088412be4SAndrew Jeffery 	astlpc->proto = &astlpc_protocol_version[negotiated];
10814e8264b7SAndrew Jeffery 
10823a540664SAndrew Jeffery 	rc = mctp_astlpc_layout_read(astlpc, &layout);
10833a540664SAndrew Jeffery 	if (rc < 0)
10843a540664SAndrew Jeffery 		return rc;
10853a540664SAndrew Jeffery 
108688412be4SAndrew Jeffery 	if (!mctp_astlpc_layout_validate(astlpc, &layout)) {
10873a540664SAndrew Jeffery 		mctp_prerr("BMC proposed invalid buffer parameters");
10883a540664SAndrew Jeffery 		return -EINVAL;
10893a540664SAndrew Jeffery 	}
10903a540664SAndrew Jeffery 
10913a540664SAndrew Jeffery 	astlpc->layout = layout;
10923a540664SAndrew Jeffery 
10933a540664SAndrew Jeffery 	if (negotiated >= 2)
10943a540664SAndrew Jeffery 		astlpc->binding.pkt_size =
109588412be4SAndrew Jeffery 			astlpc->proto->body_size(astlpc->layout.tx.size);
10963a540664SAndrew Jeffery 
1097fe763e98SAndrew Jeffery 	/* Track buffer ownership */
1098fe763e98SAndrew Jeffery 	astlpc->layout.tx.state = buffer_state_acquired;
1099fe763e98SAndrew Jeffery 	astlpc->layout.rx.state = buffer_state_released;
1100fe763e98SAndrew Jeffery 
11014e8264b7SAndrew Jeffery 	return 0;
11024e8264b7SAndrew Jeffery }
11034e8264b7SAndrew Jeffery 
11047cd72f14SAndrew Jeffery static int mctp_astlpc_update_channel(struct mctp_binding_astlpc *astlpc,
11057cd72f14SAndrew Jeffery 				      uint8_t status)
11067cd72f14SAndrew Jeffery {
11077cd72f14SAndrew Jeffery 	uint8_t updated;
11087cd72f14SAndrew Jeffery 	int rc = 0;
11097cd72f14SAndrew Jeffery 
11107cd72f14SAndrew Jeffery 	assert(astlpc->mode == MCTP_BINDING_ASTLPC_MODE_HOST);
11117cd72f14SAndrew Jeffery 
11127cd72f14SAndrew Jeffery 	updated = astlpc->kcs_status ^ status;
11137cd72f14SAndrew Jeffery 
1114d0f5da0dSAndrew Jeffery 	astlpc_prdebug(astlpc, "%s: status: 0x%x, update: 0x%x", __func__,
1115d0f5da0dSAndrew Jeffery 		       status, updated);
1116d0f5da0dSAndrew Jeffery 
11177cd72f14SAndrew Jeffery 	if (updated & KCS_STATUS_BMC_READY) {
1118d0f5da0dSAndrew Jeffery 		if (status & KCS_STATUS_BMC_READY) {
1119d0f5da0dSAndrew Jeffery 			astlpc->kcs_status = status;
1120d0f5da0dSAndrew Jeffery 			return astlpc->binding.start(&astlpc->binding);
1121d0f5da0dSAndrew Jeffery 		} else {
1122fe763e98SAndrew Jeffery 			/* Shut down the channel */
1123fe763e98SAndrew Jeffery 			astlpc->layout.rx.state = buffer_state_idle;
1124fe763e98SAndrew Jeffery 			astlpc->layout.tx.state = buffer_state_idle;
11257cd72f14SAndrew Jeffery 			mctp_binding_set_tx_enabled(&astlpc->binding, false);
11267cd72f14SAndrew Jeffery 		}
1127d0f5da0dSAndrew Jeffery 	}
11287cd72f14SAndrew Jeffery 
112988412be4SAndrew Jeffery 	if (astlpc->proto->version == 0 ||
113088412be4SAndrew Jeffery 	    updated & KCS_STATUS_CHANNEL_ACTIVE) {
11314e8264b7SAndrew Jeffery 		bool enable;
11324e8264b7SAndrew Jeffery 
1133fe763e98SAndrew Jeffery 		astlpc->layout.rx.state = buffer_state_idle;
1134fe763e98SAndrew Jeffery 		astlpc->layout.tx.state = buffer_state_idle;
11354e8264b7SAndrew Jeffery 		rc = mctp_astlpc_finalise_channel(astlpc);
11364e8264b7SAndrew Jeffery 		enable = (status & KCS_STATUS_CHANNEL_ACTIVE) && rc == 0;
11374e8264b7SAndrew Jeffery 		mctp_binding_set_tx_enabled(&astlpc->binding, enable);
11384e8264b7SAndrew Jeffery 	}
11397cd72f14SAndrew Jeffery 
11407cd72f14SAndrew Jeffery 	astlpc->kcs_status = status;
11417cd72f14SAndrew Jeffery 
11427cd72f14SAndrew Jeffery 	return rc;
11437cd72f14SAndrew Jeffery }
11447cd72f14SAndrew Jeffery 
1145672c8852SJeremy Kerr int mctp_astlpc_poll(struct mctp_binding_astlpc *astlpc)
1146672c8852SJeremy Kerr {
1147bc53d35aSJeremy Kerr 	uint8_t status, data;
1148672c8852SJeremy Kerr 	int rc;
1149672c8852SJeremy Kerr 
1150fe763e98SAndrew Jeffery 	if (astlpc->layout.rx.state == buffer_state_prepared)
1151*45d13328SRashmica Gupta 		if (!mctp_astlpc_kcs_send(astlpc, cmd_rx_complete))
1152fe763e98SAndrew Jeffery 			astlpc->layout.rx.state = buffer_state_released;
1153fe763e98SAndrew Jeffery 
1154fe763e98SAndrew Jeffery 	if (astlpc->layout.tx.state == buffer_state_prepared)
1155*45d13328SRashmica Gupta 		if (!mctp_astlpc_kcs_send(astlpc, cmd_tx_begin))
1156fe763e98SAndrew Jeffery 			astlpc->layout.tx.state = buffer_state_released;
1157fe763e98SAndrew Jeffery 
1158f13cb971SAndrew Jeffery 	rc = mctp_astlpc_kcs_read(astlpc, MCTP_ASTLPC_KCS_REG_STATUS, &status);
1159bc53d35aSJeremy Kerr 	if (rc) {
11609101a2abSAndrew Jeffery 		astlpc_prwarn(astlpc, "KCS read error");
1161672c8852SJeremy Kerr 		return -1;
1162672c8852SJeremy Kerr 	}
1163672c8852SJeremy Kerr 
11649101a2abSAndrew Jeffery 	astlpc_prdebug(astlpc, "%s: status: 0x%hhx", __func__, status);
1165edfe383fSAndrew Jeffery 
11667cd72f14SAndrew Jeffery 	if (!mctp_astlpc_kcs_read_ready(astlpc, status))
1167672c8852SJeremy Kerr 		return 0;
1168672c8852SJeremy Kerr 
1169f13cb971SAndrew Jeffery 	rc = mctp_astlpc_kcs_read(astlpc, MCTP_ASTLPC_KCS_REG_DATA, &data);
1170bc53d35aSJeremy Kerr 	if (rc) {
11719101a2abSAndrew Jeffery 		astlpc_prwarn(astlpc, "KCS data read error");
1172bc53d35aSJeremy Kerr 		return -1;
1173bc53d35aSJeremy Kerr 	}
1174bc53d35aSJeremy Kerr 
11759101a2abSAndrew Jeffery 	astlpc_prdebug(astlpc, "%s: data: 0x%hhx", __func__, data);
1176edfe383fSAndrew Jeffery 
1177*45d13328SRashmica Gupta 	if (!astlpc->proto->version &&
1178*45d13328SRashmica Gupta 	    !(data == cmd_initialise || data == cmd_dummy_value)) {
1179afcb701aSAndrew Jeffery 		astlpc_prwarn(astlpc, "Invalid message for binding state: 0x%x",
1180afcb701aSAndrew Jeffery 			      data);
1181afcb701aSAndrew Jeffery 		return 0;
1182afcb701aSAndrew Jeffery 	}
1183afcb701aSAndrew Jeffery 
1184672c8852SJeremy Kerr 	switch (data) {
1185*45d13328SRashmica Gupta 	case cmd_initialise:
1186672c8852SJeremy Kerr 		mctp_astlpc_init_channel(astlpc);
1187672c8852SJeremy Kerr 		break;
1188*45d13328SRashmica Gupta 	case cmd_tx_begin:
1189fe763e98SAndrew Jeffery 		if (astlpc->layout.rx.state != buffer_state_released) {
1190a721c2d8SPatrick Williams 			astlpc_prerr(
1191a721c2d8SPatrick Williams 				astlpc,
1192fe763e98SAndrew Jeffery 				"Protocol error: Invalid Rx buffer state for event %d: %d\n",
1193fe763e98SAndrew Jeffery 				data, astlpc->layout.rx.state);
1194fe763e98SAndrew Jeffery 			return 0;
1195fe763e98SAndrew Jeffery 		}
1196672c8852SJeremy Kerr 		mctp_astlpc_rx_start(astlpc);
1197672c8852SJeremy Kerr 		break;
1198*45d13328SRashmica Gupta 	case cmd_rx_complete:
1199fe763e98SAndrew Jeffery 		if (astlpc->layout.tx.state != buffer_state_released) {
1200a721c2d8SPatrick Williams 			astlpc_prerr(
1201a721c2d8SPatrick Williams 				astlpc,
1202fe763e98SAndrew Jeffery 				"Protocol error: Invalid Tx buffer state for event %d: %d\n",
1203fe763e98SAndrew Jeffery 				data, astlpc->layout.tx.state);
1204fe763e98SAndrew Jeffery 			return 0;
1205fe763e98SAndrew Jeffery 		}
1206672c8852SJeremy Kerr 		mctp_astlpc_tx_complete(astlpc);
1207672c8852SJeremy Kerr 		break;
1208*45d13328SRashmica Gupta 	case cmd_dummy_value:
12097cd72f14SAndrew Jeffery 		/* No responsibilities for the BMC on 0xff */
1210d0f5da0dSAndrew Jeffery 		if (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_HOST) {
1211d0f5da0dSAndrew Jeffery 			rc = mctp_astlpc_update_channel(astlpc, status);
1212d0f5da0dSAndrew Jeffery 			if (rc < 0)
1213d0f5da0dSAndrew Jeffery 				return rc;
1214d0f5da0dSAndrew Jeffery 		}
1215d0f5da0dSAndrew Jeffery 		break;
1216672c8852SJeremy Kerr 	default:
12179101a2abSAndrew Jeffery 		astlpc_prwarn(astlpc, "unknown message 0x%x", data);
1218672c8852SJeremy Kerr 	}
1219d0f5da0dSAndrew Jeffery 
1220d0f5da0dSAndrew Jeffery 	/* Handle silent loss of bmc-ready */
1221d0f5da0dSAndrew Jeffery 	if (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_HOST) {
1222*45d13328SRashmica Gupta 		if (!(status & KCS_STATUS_BMC_READY && data == cmd_dummy_value))
1223d0f5da0dSAndrew Jeffery 			return mctp_astlpc_update_channel(astlpc, status);
1224d0f5da0dSAndrew Jeffery 	}
1225d0f5da0dSAndrew Jeffery 
1226d0f5da0dSAndrew Jeffery 	return rc;
1227672c8852SJeremy Kerr }
1228672c8852SJeremy Kerr 
1229bc53d35aSJeremy Kerr /* allocate and basic initialisation */
12307cd72f14SAndrew Jeffery static struct mctp_binding_astlpc *__mctp_astlpc_init(uint8_t mode,
12317cd72f14SAndrew Jeffery 						      uint32_t mtu)
1232bc53d35aSJeremy Kerr {
1233bc53d35aSJeremy Kerr 	struct mctp_binding_astlpc *astlpc;
1234bc53d35aSJeremy Kerr 
12357cd72f14SAndrew Jeffery 	assert((mode == MCTP_BINDING_ASTLPC_MODE_BMC) ||
12367cd72f14SAndrew Jeffery 	       (mode == MCTP_BINDING_ASTLPC_MODE_HOST));
12377cd72f14SAndrew Jeffery 
1238bc53d35aSJeremy Kerr 	astlpc = __mctp_alloc(sizeof(*astlpc));
12397cd72f14SAndrew Jeffery 	if (!astlpc)
12407cd72f14SAndrew Jeffery 		return NULL;
12417cd72f14SAndrew Jeffery 
1242bc53d35aSJeremy Kerr 	memset(astlpc, 0, sizeof(*astlpc));
12437cd72f14SAndrew Jeffery 	astlpc->mode = mode;
12447cd72f14SAndrew Jeffery 	astlpc->lpc_map = NULL;
1245fe763e98SAndrew Jeffery 	astlpc->layout.rx.state = buffer_state_idle;
1246fe763e98SAndrew Jeffery 	astlpc->layout.tx.state = buffer_state_idle;
1247a9368980SAndrew Jeffery 	astlpc->requested_mtu = mtu;
1248bc53d35aSJeremy Kerr 	astlpc->binding.name = "astlpc";
1249bc53d35aSJeremy Kerr 	astlpc->binding.version = 1;
12501a4f441aSAndrew Jeffery 	astlpc->binding.pkt_size =
12511a4f441aSAndrew Jeffery 		MCTP_PACKET_SIZE(mtu > MCTP_BTU ? mtu : MCTP_BTU);
1252eba19a3bSAndrew Jeffery 	astlpc->binding.pkt_header = 4;
1253eba19a3bSAndrew Jeffery 	astlpc->binding.pkt_trailer = 4;
12547cd72f14SAndrew Jeffery 	astlpc->binding.tx = mctp_binding_astlpc_tx;
12557cd72f14SAndrew Jeffery 	if (mode == MCTP_BINDING_ASTLPC_MODE_BMC)
12567cd72f14SAndrew Jeffery 		astlpc->binding.start = mctp_binding_astlpc_start_bmc;
12577cd72f14SAndrew Jeffery 	else if (mode == MCTP_BINDING_ASTLPC_MODE_HOST)
12587cd72f14SAndrew Jeffery 		astlpc->binding.start = mctp_binding_astlpc_start_host;
12597cd72f14SAndrew Jeffery 	else {
12609101a2abSAndrew Jeffery 		astlpc_prerr(astlpc, "%s: Invalid mode: %d\n", __func__, mode);
12617cd72f14SAndrew Jeffery 		__mctp_free(astlpc);
12627cd72f14SAndrew Jeffery 		return NULL;
12637cd72f14SAndrew Jeffery 	}
1264bc53d35aSJeremy Kerr 
1265bc53d35aSJeremy Kerr 	return astlpc;
1266bc53d35aSJeremy Kerr }
1267bc53d35aSJeremy Kerr 
12683b36d17cSJeremy Kerr struct mctp_binding *mctp_binding_astlpc_core(struct mctp_binding_astlpc *b)
12693b36d17cSJeremy Kerr {
12703b36d17cSJeremy Kerr 	return &b->binding;
12713b36d17cSJeremy Kerr }
12723b36d17cSJeremy Kerr 
12737cd72f14SAndrew Jeffery struct mctp_binding_astlpc *
12747cd72f14SAndrew Jeffery mctp_astlpc_init(uint8_t mode, uint32_t mtu, void *lpc_map,
12757cd72f14SAndrew Jeffery 		 const struct mctp_binding_astlpc_ops *ops, void *ops_data)
1276bc53d35aSJeremy Kerr {
1277bc53d35aSJeremy Kerr 	struct mctp_binding_astlpc *astlpc;
1278bc53d35aSJeremy Kerr 
12797cd72f14SAndrew Jeffery 	if (!(mode == MCTP_BINDING_ASTLPC_MODE_BMC ||
12807cd72f14SAndrew Jeffery 	      mode == MCTP_BINDING_ASTLPC_MODE_HOST)) {
12817cd72f14SAndrew Jeffery 		mctp_prerr("Unknown binding mode: %u", mode);
12827cd72f14SAndrew Jeffery 		return NULL;
12837cd72f14SAndrew Jeffery 	}
12847cd72f14SAndrew Jeffery 
12857cd72f14SAndrew Jeffery 	astlpc = __mctp_astlpc_init(mode, mtu);
1286bc53d35aSJeremy Kerr 	if (!astlpc)
1287bc53d35aSJeremy Kerr 		return NULL;
1288bc53d35aSJeremy Kerr 
1289bc53d35aSJeremy Kerr 	memcpy(&astlpc->ops, ops, sizeof(astlpc->ops));
1290bc53d35aSJeremy Kerr 	astlpc->ops_data = ops_data;
1291bc53d35aSJeremy Kerr 	astlpc->lpc_map = lpc_map;
12927cd72f14SAndrew Jeffery 	astlpc->mode = mode;
1293bc53d35aSJeremy Kerr 
1294bc53d35aSJeremy Kerr 	return astlpc;
1295bc53d35aSJeremy Kerr }
1296bc53d35aSJeremy Kerr 
12977cd72f14SAndrew Jeffery struct mctp_binding_astlpc *
12987cd72f14SAndrew Jeffery mctp_astlpc_init_ops(const struct mctp_binding_astlpc_ops *ops, void *ops_data,
12997cd72f14SAndrew Jeffery 		     void *lpc_map)
13007cd72f14SAndrew Jeffery {
13017cd72f14SAndrew Jeffery 	return mctp_astlpc_init(MCTP_BINDING_ASTLPC_MODE_BMC, MCTP_BTU, lpc_map,
13027cd72f14SAndrew Jeffery 				ops, ops_data);
13037cd72f14SAndrew Jeffery }
13047cd72f14SAndrew Jeffery 
13054663f67cSAndrew Jeffery void mctp_astlpc_destroy(struct mctp_binding_astlpc *astlpc)
13064663f67cSAndrew Jeffery {
1307d0f5da0dSAndrew Jeffery 	/* Clear channel-active and bmc-ready */
1308d0f5da0dSAndrew Jeffery 	if (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_BMC)
13097c4509aaSAndrew Jeffery 		mctp_astlpc_kcs_set_status(astlpc, 0);
13104663f67cSAndrew Jeffery 	__mctp_free(astlpc);
13114663f67cSAndrew Jeffery }
13124663f67cSAndrew Jeffery 
1313b214c643SJeremy Kerr #ifdef MCTP_HAVE_FILEIO
131455fb90beSAndrew Jeffery 
1315bc53d35aSJeremy Kerr static int mctp_astlpc_init_fileio_lpc(struct mctp_binding_astlpc *astlpc)
1316672c8852SJeremy Kerr {
1317672c8852SJeremy Kerr 	struct aspeed_lpc_ctrl_mapping map = {
1318672c8852SJeremy Kerr 		.window_type = ASPEED_LPC_CTRL_WINDOW_MEMORY,
1319672c8852SJeremy Kerr 		.window_id = 0, /* There's only one */
1320672c8852SJeremy Kerr 		.flags = 0,
1321672c8852SJeremy Kerr 		.addr = 0,
1322672c8852SJeremy Kerr 		.offset = 0,
1323672c8852SJeremy Kerr 		.size = 0
1324672c8852SJeremy Kerr 	};
1325979c6a16SAndrew Jeffery 	void *lpc_map_base;
1326672c8852SJeremy Kerr 	int fd, rc;
1327672c8852SJeremy Kerr 
1328672c8852SJeremy Kerr 	fd = open(lpc_path, O_RDWR | O_SYNC);
1329672c8852SJeremy Kerr 	if (fd < 0) {
13309101a2abSAndrew Jeffery 		astlpc_prwarn(astlpc, "LPC open (%s) failed", lpc_path);
1331672c8852SJeremy Kerr 		return -1;
1332672c8852SJeremy Kerr 	}
1333672c8852SJeremy Kerr 
1334672c8852SJeremy Kerr 	rc = ioctl(fd, ASPEED_LPC_CTRL_IOCTL_GET_SIZE, &map);
1335672c8852SJeremy Kerr 	if (rc) {
13369101a2abSAndrew Jeffery 		astlpc_prwarn(astlpc, "LPC GET_SIZE failed");
1337672c8852SJeremy Kerr 		close(fd);
1338672c8852SJeremy Kerr 		return -1;
1339672c8852SJeremy Kerr 	}
1340672c8852SJeremy Kerr 
1341c1d5c543SAndrew Jeffery 	/*
1342c1d5c543SAndrew Jeffery 	 * ������
1343c1d5c543SAndrew Jeffery 	 *
1344c1d5c543SAndrew Jeffery 	 * Decouple ourselves from hiomapd[1] (another user of the FW2AHB) by
1345c1d5c543SAndrew Jeffery 	 * mapping the FW2AHB to the reserved memory here as well.
1346c1d5c543SAndrew Jeffery 	 *
1347c1d5c543SAndrew Jeffery 	 * It's not possible to use the MCTP ASTLPC binding on machines that
1348c1d5c543SAndrew Jeffery 	 * need the FW2AHB bridge mapped anywhere except to the reserved memory
1349c1d5c543SAndrew Jeffery 	 * (e.g. the host SPI NOR).
1350c1d5c543SAndrew Jeffery 	 *
1351c1d5c543SAndrew Jeffery 	 * [1] https://github.com/openbmc/hiomapd/
1352c1d5c543SAndrew Jeffery 	 *
1353c1d5c543SAndrew Jeffery 	 * ������
1354c1d5c543SAndrew Jeffery 	 *
1355c1d5c543SAndrew Jeffery 	 * The following calculation must align with what's going on in
1356c1d5c543SAndrew Jeffery 	 * hiomapd's lpc.c so as not to disrupt its behaviour:
1357c1d5c543SAndrew Jeffery 	 *
1358c1d5c543SAndrew Jeffery 	 * https://github.com/openbmc/hiomapd/blob/5ff50e3cbd7702aefc185264e4adfb9952040575/lpc.c#L68
1359c1d5c543SAndrew Jeffery 	 *
1360c1d5c543SAndrew Jeffery 	 * ������
1361c1d5c543SAndrew Jeffery 	 */
1362c1d5c543SAndrew Jeffery 
1363c1d5c543SAndrew Jeffery 	/* Map the reserved memory at the top of the 28-bit LPC firmware address space */
1364c1d5c543SAndrew Jeffery 	map.addr = 0x0FFFFFFF & -map.size;
1365a721c2d8SPatrick Williams 	astlpc_prinfo(
1366a721c2d8SPatrick Williams 		astlpc,
1367c1d5c543SAndrew Jeffery 		"Configuring FW2AHB to map reserved memory at 0x%08x for 0x%x in the LPC FW cycle address-space",
1368c1d5c543SAndrew Jeffery 		map.addr, map.size);
1369c1d5c543SAndrew Jeffery 
1370c1d5c543SAndrew Jeffery 	rc = ioctl(fd, ASPEED_LPC_CTRL_IOCTL_MAP, &map);
1371c1d5c543SAndrew Jeffery 	if (rc) {
1372a721c2d8SPatrick Williams 		astlpc_prwarn(astlpc,
1373a721c2d8SPatrick Williams 			      "Failed to map FW2AHB to reserved memory");
1374c1d5c543SAndrew Jeffery 		close(fd);
1375c1d5c543SAndrew Jeffery 		return -1;
1376c1d5c543SAndrew Jeffery 	}
1377c1d5c543SAndrew Jeffery 
1378c1d5c543SAndrew Jeffery 	/* Map the reserved memory into our address space */
1379979c6a16SAndrew Jeffery 	lpc_map_base =
1380979c6a16SAndrew Jeffery 		mmap(NULL, map.size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
1381979c6a16SAndrew Jeffery 	if (lpc_map_base == MAP_FAILED) {
13829101a2abSAndrew Jeffery 		astlpc_prwarn(astlpc, "LPC mmap failed");
1383672c8852SJeremy Kerr 		rc = -1;
1384672c8852SJeremy Kerr 	} else {
1385979c6a16SAndrew Jeffery 		astlpc->lpc_map = lpc_map_base + map.size - LPC_WIN_SIZE;
1386672c8852SJeremy Kerr 	}
1387672c8852SJeremy Kerr 
1388672c8852SJeremy Kerr 	close(fd);
1389672c8852SJeremy Kerr 
1390672c8852SJeremy Kerr 	return rc;
1391672c8852SJeremy Kerr }
1392672c8852SJeremy Kerr 
1393bc53d35aSJeremy Kerr static int mctp_astlpc_init_fileio_kcs(struct mctp_binding_astlpc *astlpc)
1394672c8852SJeremy Kerr {
1395672c8852SJeremy Kerr 	astlpc->kcs_fd = open(kcs_path, O_RDWR);
1396672c8852SJeremy Kerr 	if (astlpc->kcs_fd < 0)
1397672c8852SJeremy Kerr 		return -1;
1398672c8852SJeremy Kerr 
1399672c8852SJeremy Kerr 	return 0;
1400672c8852SJeremy Kerr }
1401672c8852SJeremy Kerr 
1402bc53d35aSJeremy Kerr static int __mctp_astlpc_fileio_kcs_read(void *arg,
1403a721c2d8SPatrick Williams 					 enum mctp_binding_astlpc_kcs_reg reg,
1404a721c2d8SPatrick Williams 					 uint8_t *val)
1405bc53d35aSJeremy Kerr {
1406bc53d35aSJeremy Kerr 	struct mctp_binding_astlpc *astlpc = arg;
1407bc53d35aSJeremy Kerr 	off_t offset = reg;
1408bc53d35aSJeremy Kerr 	int rc;
1409bc53d35aSJeremy Kerr 
1410bc53d35aSJeremy Kerr 	rc = pread(astlpc->kcs_fd, val, 1, offset);
1411bc53d35aSJeremy Kerr 
1412bc53d35aSJeremy Kerr 	return rc == 1 ? 0 : -1;
1413bc53d35aSJeremy Kerr }
1414bc53d35aSJeremy Kerr 
1415bc53d35aSJeremy Kerr static int __mctp_astlpc_fileio_kcs_write(void *arg,
1416a721c2d8SPatrick Williams 					  enum mctp_binding_astlpc_kcs_reg reg,
1417a721c2d8SPatrick Williams 					  uint8_t val)
1418bc53d35aSJeremy Kerr {
1419bc53d35aSJeremy Kerr 	struct mctp_binding_astlpc *astlpc = arg;
1420bc53d35aSJeremy Kerr 	off_t offset = reg;
1421bc53d35aSJeremy Kerr 	int rc;
1422bc53d35aSJeremy Kerr 
1423bc53d35aSJeremy Kerr 	rc = pwrite(astlpc->kcs_fd, &val, 1, offset);
1424bc53d35aSJeremy Kerr 
1425bc53d35aSJeremy Kerr 	return rc == 1 ? 0 : -1;
1426bc53d35aSJeremy Kerr }
1427bc53d35aSJeremy Kerr 
14281111c6a5SAndrew Jeffery int mctp_astlpc_init_pollfd(struct mctp_binding_astlpc *astlpc,
14291111c6a5SAndrew Jeffery 			    struct pollfd *pollfd)
14301111c6a5SAndrew Jeffery {
1431fe763e98SAndrew Jeffery 	bool release;
1432fe763e98SAndrew Jeffery 
14331111c6a5SAndrew Jeffery 	pollfd->fd = astlpc->kcs_fd;
1434fe763e98SAndrew Jeffery 	pollfd->events = 0;
1435fe763e98SAndrew Jeffery 
1436fe763e98SAndrew Jeffery 	release = astlpc->layout.rx.state == buffer_state_prepared ||
1437fe763e98SAndrew Jeffery 		  astlpc->layout.tx.state == buffer_state_prepared;
1438fe763e98SAndrew Jeffery 
1439fe763e98SAndrew Jeffery 	pollfd->events = release ? POLLOUT : POLLIN;
14401111c6a5SAndrew Jeffery 
14411111c6a5SAndrew Jeffery 	return 0;
14421111c6a5SAndrew Jeffery }
14431111c6a5SAndrew Jeffery 
1444bc53d35aSJeremy Kerr struct mctp_binding_astlpc *mctp_astlpc_init_fileio(void)
1445672c8852SJeremy Kerr {
1446672c8852SJeremy Kerr 	struct mctp_binding_astlpc *astlpc;
1447672c8852SJeremy Kerr 	int rc;
1448672c8852SJeremy Kerr 
14497cd72f14SAndrew Jeffery 	/*
14507cd72f14SAndrew Jeffery 	 * If we're doing file IO then we're very likely not running
14518877c460SAndrew Jeffery 	 * freestanding, so lets assume that we're on the BMC side.
14528877c460SAndrew Jeffery 	 *
14538877c460SAndrew Jeffery 	 * Requesting an MTU of 0 requests the largest possible MTU, whatever
14548877c460SAndrew Jeffery 	 * value that might take.
14557cd72f14SAndrew Jeffery 	 */
14568877c460SAndrew Jeffery 	astlpc = __mctp_astlpc_init(MCTP_BINDING_ASTLPC_MODE_BMC, 0);
1457bc53d35aSJeremy Kerr 	if (!astlpc)
1458bc53d35aSJeremy Kerr 		return NULL;
1459672c8852SJeremy Kerr 
1460bc53d35aSJeremy Kerr 	/* Set internal operations for kcs. We use direct accesses to the lpc
1461bc53d35aSJeremy Kerr 	 * map area */
1462bc53d35aSJeremy Kerr 	astlpc->ops.kcs_read = __mctp_astlpc_fileio_kcs_read;
1463bc53d35aSJeremy Kerr 	astlpc->ops.kcs_write = __mctp_astlpc_fileio_kcs_write;
1464bc53d35aSJeremy Kerr 	astlpc->ops_data = astlpc;
1465bc53d35aSJeremy Kerr 
1466bc53d35aSJeremy Kerr 	rc = mctp_astlpc_init_fileio_lpc(astlpc);
1467672c8852SJeremy Kerr 	if (rc) {
1468672c8852SJeremy Kerr 		free(astlpc);
1469672c8852SJeremy Kerr 		return NULL;
1470672c8852SJeremy Kerr 	}
1471672c8852SJeremy Kerr 
1472bc53d35aSJeremy Kerr 	rc = mctp_astlpc_init_fileio_kcs(astlpc);
1473672c8852SJeremy Kerr 	if (rc) {
1474672c8852SJeremy Kerr 		free(astlpc);
1475672c8852SJeremy Kerr 		return NULL;
1476672c8852SJeremy Kerr 	}
1477672c8852SJeremy Kerr 
1478672c8852SJeremy Kerr 	return astlpc;
1479672c8852SJeremy Kerr }
148092a10a6bSJeremy Kerr #else
14815b2702dbSAndrew Jeffery struct mctp_binding_astlpc *mctp_astlpc_init_fileio(void)
148292a10a6bSJeremy Kerr {
14835b2702dbSAndrew Jeffery 	mctp_prlog(MCTP_LOG_ERR, "%s: Missing support for file IO", __func__);
148492a10a6bSJeremy Kerr 	return NULL;
148592a10a6bSJeremy Kerr }
1486672c8852SJeremy Kerr 
14871111c6a5SAndrew Jeffery int mctp_astlpc_init_pollfd(struct mctp_binding_astlpc *astlpc __unused,
14881111c6a5SAndrew Jeffery 			    struct pollfd *pollfd __unused)
14891111c6a5SAndrew Jeffery {
14901111c6a5SAndrew Jeffery 	mctp_prlog(MCTP_LOG_ERR, "%s: Missing support for file IO", __func__);
14911111c6a5SAndrew Jeffery 	return -1;
14921111c6a5SAndrew Jeffery }
149392a10a6bSJeremy Kerr #endif
1494