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 *lpc_path = "/dev/aspeed-lpc-ctrl";
4092a10a6bSJeremy Kerr
4192a10a6bSJeremy Kerr #endif
4292a10a6bSJeremy Kerr
4345d13328SRashmica Gupta enum mctp_astlpc_cmd {
4445d13328SRashmica Gupta cmd_initialise = 0x00,
4545d13328SRashmica Gupta cmd_tx_begin = 0x01,
4645d13328SRashmica Gupta cmd_rx_complete = 0x02,
4745d13328SRashmica Gupta cmd_dummy_value = 0xff,
4845d13328SRashmica Gupta };
4945d13328SRashmica Gupta
50fe763e98SAndrew Jeffery enum mctp_astlpc_buffer_state {
51fe763e98SAndrew Jeffery /*
52fe763e98SAndrew Jeffery * Prior to "Channel Ready" we mark the buffers as "idle" to catch illegal accesses. In this
53fe763e98SAndrew Jeffery * state neither side is considered the owner of the buffer.
54fe763e98SAndrew Jeffery *
55fe763e98SAndrew Jeffery * Upon "Channel Ready", each side transitions the buffers from the initial "idle" state
56fe763e98SAndrew Jeffery * to the following target states:
57fe763e98SAndrew Jeffery *
58fe763e98SAndrew Jeffery * Tx buffer: "acquired"
59fe763e98SAndrew Jeffery * Rx buffer: "released"
60fe763e98SAndrew Jeffery */
61fe763e98SAndrew Jeffery buffer_state_idle,
62fe763e98SAndrew Jeffery
63fe763e98SAndrew Jeffery /*
64fe763e98SAndrew Jeffery * Beyond initialisation by "Channel Ready", buffers are in the "acquired" state once:
65fe763e98SAndrew Jeffery *
66fe763e98SAndrew Jeffery * 1. We dequeue a control command transferring the buffer to our ownership out of the KCS
67fe763e98SAndrew Jeffery * interface, and
68fe763e98SAndrew Jeffery * 2. We are yet to complete all of our required accesses to the buffer
69fe763e98SAndrew Jeffery *
70fe763e98SAndrew Jeffery * * The Tx buffer enters the "acquired" state when we dequeue the "Rx Complete" command
71fe763e98SAndrew Jeffery * * The Rx buffer enters the "acquired" state when we dequeue the "Tx Begin" command
72fe763e98SAndrew Jeffery *
73fe763e98SAndrew Jeffery * It is a failure of implementation if it's possible for both sides to simultaneously
74fe763e98SAndrew Jeffery * consider a buffer as "acquired".
75fe763e98SAndrew Jeffery */
76fe763e98SAndrew Jeffery buffer_state_acquired,
77fe763e98SAndrew Jeffery
78fe763e98SAndrew Jeffery /*
79fe763e98SAndrew Jeffery * Buffers are in the "prepared" state when:
80fe763e98SAndrew Jeffery *
81fe763e98SAndrew Jeffery * 1. We have completed all of our required accesses (read or write) for the buffer, and
82fe763e98SAndrew Jeffery * 2. We have not yet successfully enqueued the control command to hand off ownership
83fe763e98SAndrew Jeffery */
84fe763e98SAndrew Jeffery buffer_state_prepared,
85fe763e98SAndrew Jeffery
86fe763e98SAndrew Jeffery /*
87fe763e98SAndrew Jeffery * Beyond initialisation by "Channel Ready", buffers are in the "released" state once:
88fe763e98SAndrew Jeffery *
89fe763e98SAndrew Jeffery * 1. We successfully enqueue the control command transferring ownership to the remote
90fe763e98SAndrew Jeffery * side in to the KCS interface
91fe763e98SAndrew Jeffery *
92fe763e98SAndrew Jeffery * * The Tx buffer enters the "released" state when we enqueue the "Tx Begin" command
93fe763e98SAndrew Jeffery * * The Rx buffer enters the "released" state when we enqueue the "Rx Complete" command
94fe763e98SAndrew Jeffery *
95fe763e98SAndrew Jeffery * It may be the case that both sides simultaneously consider a buffer to be in the
96fe763e98SAndrew Jeffery * "released" state. However, if this is true, it must also be true that a buffer ownership
97fe763e98SAndrew Jeffery * transfer command has been enqueued in the KCS interface and is yet to be dequeued.
98fe763e98SAndrew Jeffery */
99fe763e98SAndrew Jeffery buffer_state_released,
100fe763e98SAndrew Jeffery };
101fe763e98SAndrew Jeffery
1027cd72f14SAndrew Jeffery struct mctp_astlpc_buffer {
1037cd72f14SAndrew Jeffery uint32_t offset;
1047cd72f14SAndrew Jeffery uint32_t size;
105fe763e98SAndrew Jeffery enum mctp_astlpc_buffer_state state;
1067cd72f14SAndrew Jeffery };
1077cd72f14SAndrew Jeffery
1087cd72f14SAndrew Jeffery struct mctp_astlpc_layout {
1097cd72f14SAndrew Jeffery struct mctp_astlpc_buffer rx;
1107cd72f14SAndrew Jeffery struct mctp_astlpc_buffer tx;
1117cd72f14SAndrew Jeffery };
1127cd72f14SAndrew Jeffery
11388412be4SAndrew Jeffery struct mctp_astlpc_protocol {
11488412be4SAndrew Jeffery uint16_t version;
11588412be4SAndrew Jeffery uint32_t (*packet_size)(uint32_t body);
11688412be4SAndrew Jeffery uint32_t (*body_size)(uint32_t packet);
117eba19a3bSAndrew Jeffery void (*pktbuf_protect)(struct mctp_pktbuf *pkt);
118eba19a3bSAndrew Jeffery bool (*pktbuf_validate)(struct mctp_pktbuf *pkt);
11988412be4SAndrew Jeffery };
12088412be4SAndrew Jeffery
121672c8852SJeremy Kerr struct mctp_binding_astlpc {
122672c8852SJeremy Kerr struct mctp_binding binding;
123bc53d35aSJeremy Kerr
124672c8852SJeremy Kerr void *lpc_map;
1257cd72f14SAndrew Jeffery struct mctp_astlpc_layout layout;
1267cd72f14SAndrew Jeffery
1277cd72f14SAndrew Jeffery uint8_t mode;
128a9368980SAndrew Jeffery uint32_t requested_mtu;
129bc53d35aSJeremy Kerr
13088412be4SAndrew Jeffery const struct mctp_astlpc_protocol *proto;
13188412be4SAndrew Jeffery
132bc53d35aSJeremy Kerr /* direct ops data */
133bc53d35aSJeremy Kerr struct mctp_binding_astlpc_ops ops;
134bc53d35aSJeremy Kerr void *ops_data;
135bc53d35aSJeremy Kerr
136bc53d35aSJeremy Kerr /* fileio ops data */
137672c8852SJeremy Kerr int kcs_fd;
138672c8852SJeremy Kerr uint8_t kcs_status;
139672c8852SJeremy Kerr };
140672c8852SJeremy Kerr
141672c8852SJeremy Kerr #define binding_to_astlpc(b) \
142672c8852SJeremy Kerr container_of(b, struct mctp_binding_astlpc, binding)
143672c8852SJeremy Kerr
1449101a2abSAndrew Jeffery #define astlpc_prlog(ctx, lvl, fmt, ...) \
1459101a2abSAndrew Jeffery do { \
1469101a2abSAndrew Jeffery bool __bmc = ((ctx)->mode == MCTP_BINDING_ASTLPC_MODE_BMC); \
1479101a2abSAndrew Jeffery mctp_prlog(lvl, pr_fmt("%s: " fmt), __bmc ? "bmc" : "host", \
1489101a2abSAndrew Jeffery ##__VA_ARGS__); \
1499101a2abSAndrew Jeffery } while (0)
1509101a2abSAndrew Jeffery
1519101a2abSAndrew Jeffery #define astlpc_prerr(ctx, fmt, ...) \
1529101a2abSAndrew Jeffery astlpc_prlog(ctx, MCTP_LOG_ERR, fmt, ##__VA_ARGS__)
1539101a2abSAndrew Jeffery #define astlpc_prwarn(ctx, fmt, ...) \
1549101a2abSAndrew Jeffery astlpc_prlog(ctx, MCTP_LOG_WARNING, fmt, ##__VA_ARGS__)
15599b9d238SAndrew Jeffery #define astlpc_prnotice(ctx, fmt, ...) \
15699b9d238SAndrew Jeffery astlpc_prlog(ctx, MCTP_LOG_NOTICE, fmt, ##__VA_ARGS__)
1579101a2abSAndrew Jeffery #define astlpc_prinfo(ctx, fmt, ...) \
1589101a2abSAndrew Jeffery astlpc_prlog(ctx, MCTP_LOG_INFO, fmt, ##__VA_ARGS__)
1599101a2abSAndrew Jeffery #define astlpc_prdebug(ctx, fmt, ...) \
1609101a2abSAndrew Jeffery astlpc_prlog(ctx, MCTP_LOG_DEBUG, fmt, ##__VA_ARGS__)
1619101a2abSAndrew Jeffery
1627cd72f14SAndrew Jeffery /* clang-format off */
1637cd72f14SAndrew Jeffery #define ASTLPC_MCTP_MAGIC 0x4d435450
1644e8264b7SAndrew Jeffery #define ASTLPC_VER_BAD 0
1657cd72f14SAndrew Jeffery #define ASTLPC_VER_MIN 1
1667cd72f14SAndrew Jeffery
1673a540664SAndrew Jeffery /* Support testing of new binding protocols */
1683a540664SAndrew Jeffery #ifndef ASTLPC_VER_CUR
169eba19a3bSAndrew Jeffery #define ASTLPC_VER_CUR 3
1703a540664SAndrew Jeffery #endif
1717cd72f14SAndrew Jeffery /* clang-format on */
172672c8852SJeremy Kerr
17388412be4SAndrew Jeffery #ifndef ARRAY_SIZE
17488412be4SAndrew Jeffery #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
17588412be4SAndrew Jeffery #endif
17688412be4SAndrew Jeffery
astlpc_packet_size_v1(uint32_t body)17788412be4SAndrew Jeffery static uint32_t astlpc_packet_size_v1(uint32_t body)
17888412be4SAndrew Jeffery {
17988412be4SAndrew Jeffery assert((body + 4) > body);
18088412be4SAndrew Jeffery
18188412be4SAndrew Jeffery return body + 4;
18288412be4SAndrew Jeffery }
18388412be4SAndrew Jeffery
astlpc_body_size_v1(uint32_t packet)18488412be4SAndrew Jeffery static uint32_t astlpc_body_size_v1(uint32_t packet)
18588412be4SAndrew Jeffery {
18688412be4SAndrew Jeffery assert((packet - 4) < packet);
18788412be4SAndrew Jeffery
18888412be4SAndrew Jeffery return packet - 4;
18988412be4SAndrew Jeffery }
19088412be4SAndrew Jeffery
astlpc_pktbuf_protect_v1(struct mctp_pktbuf * pkt)191eba19a3bSAndrew Jeffery void astlpc_pktbuf_protect_v1(struct mctp_pktbuf *pkt)
192eba19a3bSAndrew Jeffery {
193eba19a3bSAndrew Jeffery (void)pkt;
194eba19a3bSAndrew Jeffery }
195eba19a3bSAndrew Jeffery
astlpc_pktbuf_validate_v1(struct mctp_pktbuf * pkt)196eba19a3bSAndrew Jeffery bool astlpc_pktbuf_validate_v1(struct mctp_pktbuf *pkt)
197eba19a3bSAndrew Jeffery {
198eba19a3bSAndrew Jeffery (void)pkt;
199eba19a3bSAndrew Jeffery return true;
200eba19a3bSAndrew Jeffery }
201eba19a3bSAndrew Jeffery
astlpc_packet_size_v3(uint32_t body)202eba19a3bSAndrew Jeffery static uint32_t astlpc_packet_size_v3(uint32_t body)
203eba19a3bSAndrew Jeffery {
204eba19a3bSAndrew Jeffery assert((body + 4 + 4) > body);
205eba19a3bSAndrew Jeffery
206eba19a3bSAndrew Jeffery return body + 4 + 4;
207eba19a3bSAndrew Jeffery }
208eba19a3bSAndrew Jeffery
astlpc_body_size_v3(uint32_t packet)209eba19a3bSAndrew Jeffery static uint32_t astlpc_body_size_v3(uint32_t packet)
210eba19a3bSAndrew Jeffery {
211eba19a3bSAndrew Jeffery assert((packet - 4 - 4) < packet);
212eba19a3bSAndrew Jeffery
213eba19a3bSAndrew Jeffery return packet - 4 - 4;
214eba19a3bSAndrew Jeffery }
215eba19a3bSAndrew Jeffery
astlpc_pktbuf_protect_v3(struct mctp_pktbuf * pkt)216eba19a3bSAndrew Jeffery void astlpc_pktbuf_protect_v3(struct mctp_pktbuf *pkt)
217eba19a3bSAndrew Jeffery {
218eba19a3bSAndrew Jeffery uint32_t code;
219eba19a3bSAndrew Jeffery
220eba19a3bSAndrew Jeffery code = htobe32(crc32(mctp_pktbuf_hdr(pkt), mctp_pktbuf_size(pkt)));
221eba19a3bSAndrew Jeffery mctp_prdebug("%s: 0x%" PRIx32, __func__, code);
222eba19a3bSAndrew Jeffery mctp_pktbuf_push(pkt, &code, 4);
223eba19a3bSAndrew Jeffery }
224eba19a3bSAndrew Jeffery
astlpc_pktbuf_validate_v3(struct mctp_pktbuf * pkt)225eba19a3bSAndrew Jeffery bool astlpc_pktbuf_validate_v3(struct mctp_pktbuf *pkt)
226eba19a3bSAndrew Jeffery {
227eba19a3bSAndrew Jeffery uint32_t code;
228eba19a3bSAndrew Jeffery void *check;
229eba19a3bSAndrew Jeffery
230eba19a3bSAndrew Jeffery code = be32toh(crc32(mctp_pktbuf_hdr(pkt), mctp_pktbuf_size(pkt) - 4));
231eba19a3bSAndrew Jeffery mctp_prdebug("%s: 0x%" PRIx32, __func__, code);
232eba19a3bSAndrew Jeffery check = mctp_pktbuf_pop(pkt, 4);
233eba19a3bSAndrew Jeffery return check && !memcmp(&code, check, 4);
234eba19a3bSAndrew Jeffery }
235eba19a3bSAndrew Jeffery
23688412be4SAndrew Jeffery static const struct mctp_astlpc_protocol astlpc_protocol_version[] = {
23788412be4SAndrew Jeffery [0] = {
23888412be4SAndrew Jeffery .version = 0,
23988412be4SAndrew Jeffery .packet_size = NULL,
24088412be4SAndrew Jeffery .body_size = NULL,
241eba19a3bSAndrew Jeffery .pktbuf_protect = NULL,
242eba19a3bSAndrew Jeffery .pktbuf_validate = NULL,
24388412be4SAndrew Jeffery },
24488412be4SAndrew Jeffery [1] = {
24588412be4SAndrew Jeffery .version = 1,
24688412be4SAndrew Jeffery .packet_size = astlpc_packet_size_v1,
24788412be4SAndrew Jeffery .body_size = astlpc_body_size_v1,
248eba19a3bSAndrew Jeffery .pktbuf_protect = astlpc_pktbuf_protect_v1,
249eba19a3bSAndrew Jeffery .pktbuf_validate = astlpc_pktbuf_validate_v1,
25088412be4SAndrew Jeffery },
25188412be4SAndrew Jeffery [2] = {
25288412be4SAndrew Jeffery .version = 2,
25388412be4SAndrew Jeffery .packet_size = astlpc_packet_size_v1,
25488412be4SAndrew Jeffery .body_size = astlpc_body_size_v1,
255eba19a3bSAndrew Jeffery .pktbuf_protect = astlpc_pktbuf_protect_v1,
256eba19a3bSAndrew Jeffery .pktbuf_validate = astlpc_pktbuf_validate_v1,
257eba19a3bSAndrew Jeffery },
258eba19a3bSAndrew Jeffery [3] = {
259eba19a3bSAndrew Jeffery .version = 3,
260eba19a3bSAndrew Jeffery .packet_size = astlpc_packet_size_v3,
261eba19a3bSAndrew Jeffery .body_size = astlpc_body_size_v3,
262eba19a3bSAndrew Jeffery .pktbuf_protect = astlpc_pktbuf_protect_v3,
263eba19a3bSAndrew Jeffery .pktbuf_validate = astlpc_pktbuf_validate_v3,
26488412be4SAndrew Jeffery },
26588412be4SAndrew Jeffery };
26688412be4SAndrew Jeffery
267672c8852SJeremy Kerr struct mctp_lpcmap_hdr {
268672c8852SJeremy Kerr uint32_t magic;
269672c8852SJeremy Kerr
270672c8852SJeremy Kerr uint16_t bmc_ver_min;
271672c8852SJeremy Kerr uint16_t bmc_ver_cur;
272672c8852SJeremy Kerr uint16_t host_ver_min;
273672c8852SJeremy Kerr uint16_t host_ver_cur;
274672c8852SJeremy Kerr uint16_t negotiated_ver;
275672c8852SJeremy Kerr uint16_t pad0;
276672c8852SJeremy Kerr
2773a540664SAndrew Jeffery struct {
278672c8852SJeremy Kerr uint32_t rx_offset;
279672c8852SJeremy Kerr uint32_t rx_size;
280672c8852SJeremy Kerr uint32_t tx_offset;
281672c8852SJeremy Kerr uint32_t tx_size;
2823a540664SAndrew Jeffery } layout;
283672c8852SJeremy Kerr } __attribute__((packed));
284672c8852SJeremy Kerr
2853a540664SAndrew Jeffery static const uint32_t control_size = 0x100;
286672c8852SJeremy Kerr
287672c8852SJeremy Kerr #define LPC_WIN_SIZE (1 * 1024 * 1024)
288672c8852SJeremy Kerr
289672c8852SJeremy Kerr #define KCS_STATUS_BMC_READY 0x80
290672c8852SJeremy Kerr #define KCS_STATUS_CHANNEL_ACTIVE 0x40
291672c8852SJeremy Kerr #define KCS_STATUS_IBF 0x02
292672c8852SJeremy Kerr #define KCS_STATUS_OBF 0x01
293672c8852SJeremy Kerr
mctp_astlpc_kcs_write(struct mctp_binding_astlpc * astlpc,enum mctp_binding_astlpc_kcs_reg reg,uint8_t val)294f13cb971SAndrew Jeffery static inline int mctp_astlpc_kcs_write(struct mctp_binding_astlpc *astlpc,
295f13cb971SAndrew Jeffery enum mctp_binding_astlpc_kcs_reg reg,
296f13cb971SAndrew Jeffery uint8_t val)
297f13cb971SAndrew Jeffery {
298f13cb971SAndrew Jeffery return astlpc->ops.kcs_write(astlpc->ops_data, reg, val);
299f13cb971SAndrew Jeffery }
300f13cb971SAndrew Jeffery
mctp_astlpc_kcs_read(struct mctp_binding_astlpc * astlpc,enum mctp_binding_astlpc_kcs_reg reg,uint8_t * val)301f13cb971SAndrew Jeffery static inline int mctp_astlpc_kcs_read(struct mctp_binding_astlpc *astlpc,
302f13cb971SAndrew Jeffery enum mctp_binding_astlpc_kcs_reg reg,
303f13cb971SAndrew Jeffery uint8_t *val)
304f13cb971SAndrew Jeffery {
305f13cb971SAndrew Jeffery return astlpc->ops.kcs_read(astlpc->ops_data, reg, val);
306f13cb971SAndrew Jeffery }
307f13cb971SAndrew Jeffery
mctp_astlpc_lpc_write(struct mctp_binding_astlpc * astlpc,const void * buf,long offset,size_t len)30855fb90beSAndrew Jeffery static inline int mctp_astlpc_lpc_write(struct mctp_binding_astlpc *astlpc,
30955fb90beSAndrew Jeffery const void *buf, long offset,
31055fb90beSAndrew Jeffery size_t len)
311bc53d35aSJeremy Kerr {
31255fb90beSAndrew Jeffery astlpc_prdebug(astlpc, "%s: %zu bytes to 0x%lx", __func__, len, offset);
31355fb90beSAndrew Jeffery
31455fb90beSAndrew Jeffery assert(offset >= 0);
31555fb90beSAndrew Jeffery
31655fb90beSAndrew Jeffery /* Indirect access */
31755fb90beSAndrew Jeffery if (astlpc->ops.lpc_write) {
31855fb90beSAndrew Jeffery void *data = astlpc->ops_data;
31955fb90beSAndrew Jeffery
32055fb90beSAndrew Jeffery return astlpc->ops.lpc_write(data, buf, offset, len);
32155fb90beSAndrew Jeffery }
32255fb90beSAndrew Jeffery
32355fb90beSAndrew Jeffery /* Direct mapping */
32455fb90beSAndrew Jeffery assert(astlpc->lpc_map);
32555fb90beSAndrew Jeffery memcpy(&((char *)astlpc->lpc_map)[offset], buf, len);
32655fb90beSAndrew Jeffery
32755fb90beSAndrew Jeffery return 0;
32855fb90beSAndrew Jeffery }
32955fb90beSAndrew Jeffery
mctp_astlpc_lpc_read(struct mctp_binding_astlpc * astlpc,void * buf,long offset,size_t len)33055fb90beSAndrew Jeffery static inline int mctp_astlpc_lpc_read(struct mctp_binding_astlpc *astlpc,
33155fb90beSAndrew Jeffery void *buf, long offset, size_t len)
33255fb90beSAndrew Jeffery {
33355fb90beSAndrew Jeffery astlpc_prdebug(astlpc, "%s: %zu bytes from 0x%lx", __func__, len,
33455fb90beSAndrew Jeffery offset);
33555fb90beSAndrew Jeffery
33655fb90beSAndrew Jeffery assert(offset >= 0);
33755fb90beSAndrew Jeffery
33855fb90beSAndrew Jeffery /* Indirect access */
33955fb90beSAndrew Jeffery if (astlpc->ops.lpc_read) {
34055fb90beSAndrew Jeffery void *data = astlpc->ops_data;
34155fb90beSAndrew Jeffery
34255fb90beSAndrew Jeffery return astlpc->ops.lpc_read(data, buf, offset, len);
34355fb90beSAndrew Jeffery }
34455fb90beSAndrew Jeffery
34555fb90beSAndrew Jeffery /* Direct mapping */
34655fb90beSAndrew Jeffery assert(astlpc->lpc_map);
34755fb90beSAndrew Jeffery memcpy(buf, &((char *)astlpc->lpc_map)[offset], len);
34855fb90beSAndrew Jeffery
34955fb90beSAndrew Jeffery return 0;
350bc53d35aSJeremy Kerr }
351bc53d35aSJeremy Kerr
35299b9d238SAndrew Jeffery static void
mctp_astlpc_kcs_print_status_write(struct mctp_binding_astlpc * astlpc,uint8_t status)35399b9d238SAndrew Jeffery mctp_astlpc_kcs_print_status_write(struct mctp_binding_astlpc *astlpc,
35499b9d238SAndrew Jeffery uint8_t status)
35599b9d238SAndrew Jeffery {
35699b9d238SAndrew Jeffery astlpc_prnotice(
35799b9d238SAndrew Jeffery astlpc, "Binding state is 0x%hhx: BMC %s, Channel %s, OBF %s",
35899b9d238SAndrew Jeffery status, status & KCS_STATUS_BMC_READY ? "active" : "inactive",
35999b9d238SAndrew Jeffery status & KCS_STATUS_CHANNEL_ACTIVE ? "active" : "inactive",
36099b9d238SAndrew Jeffery status & KCS_STATUS_OBF ? "preserved" : "cleared");
36199b9d238SAndrew Jeffery }
36299b9d238SAndrew Jeffery
mctp_astlpc_kcs_set_status(struct mctp_binding_astlpc * astlpc,uint8_t status)363d0f5da0dSAndrew Jeffery static int mctp_astlpc_kcs_set_status(struct mctp_binding_astlpc *astlpc,
364d0f5da0dSAndrew Jeffery uint8_t status)
365d0f5da0dSAndrew Jeffery {
366d0f5da0dSAndrew Jeffery uint8_t data;
367d0f5da0dSAndrew Jeffery int rc;
368d0f5da0dSAndrew Jeffery
369d0f5da0dSAndrew Jeffery /* Since we're setting the status register, we want the other endpoint
370d0f5da0dSAndrew Jeffery * to be interrupted. However, some hardware may only raise a host-side
371d0f5da0dSAndrew Jeffery * interrupt on an ODR event.
372d0f5da0dSAndrew Jeffery * So, write a dummy value of 0xff to ODR, which will ensure that an
373d0f5da0dSAndrew Jeffery * interrupt is triggered, and can be ignored by the host.
374d0f5da0dSAndrew Jeffery */
37545d13328SRashmica Gupta data = cmd_dummy_value;
376d0f5da0dSAndrew Jeffery
377f13cb971SAndrew Jeffery rc = mctp_astlpc_kcs_write(astlpc, MCTP_ASTLPC_KCS_REG_STATUS, status);
378d0f5da0dSAndrew Jeffery if (rc) {
379d0f5da0dSAndrew Jeffery astlpc_prwarn(astlpc, "KCS status write failed");
380d0f5da0dSAndrew Jeffery return -1;
381d0f5da0dSAndrew Jeffery }
382d0f5da0dSAndrew Jeffery
38399b9d238SAndrew Jeffery mctp_astlpc_kcs_print_status_write(astlpc, status);
38499b9d238SAndrew Jeffery
385f13cb971SAndrew Jeffery rc = mctp_astlpc_kcs_write(astlpc, MCTP_ASTLPC_KCS_REG_DATA, data);
386d0f5da0dSAndrew Jeffery if (rc) {
387d0f5da0dSAndrew Jeffery astlpc_prwarn(astlpc, "KCS dummy data write failed");
388d0f5da0dSAndrew Jeffery return -1;
389d0f5da0dSAndrew Jeffery }
390d0f5da0dSAndrew Jeffery
391d0f5da0dSAndrew Jeffery return 0;
392d0f5da0dSAndrew Jeffery }
393d0f5da0dSAndrew Jeffery
mctp_astlpc_layout_read(struct mctp_binding_astlpc * astlpc,struct mctp_astlpc_layout * layout)3943a540664SAndrew Jeffery static int mctp_astlpc_layout_read(struct mctp_binding_astlpc *astlpc,
3953a540664SAndrew Jeffery struct mctp_astlpc_layout *layout)
3963a540664SAndrew Jeffery {
3973a540664SAndrew Jeffery struct mctp_lpcmap_hdr hdr;
3983a540664SAndrew Jeffery int rc;
3993a540664SAndrew Jeffery
4003a540664SAndrew Jeffery rc = mctp_astlpc_lpc_read(astlpc, &hdr, 0, sizeof(hdr));
4013a540664SAndrew Jeffery if (rc < 0)
4023a540664SAndrew Jeffery return rc;
4033a540664SAndrew Jeffery
4043a540664SAndrew Jeffery /* Flip the buffers as the names are defined in terms of the host */
4053a540664SAndrew Jeffery if (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_BMC) {
4063a540664SAndrew Jeffery layout->rx.offset = be32toh(hdr.layout.tx_offset);
4073a540664SAndrew Jeffery layout->rx.size = be32toh(hdr.layout.tx_size);
4083a540664SAndrew Jeffery layout->tx.offset = be32toh(hdr.layout.rx_offset);
4093a540664SAndrew Jeffery layout->tx.size = be32toh(hdr.layout.rx_size);
4103a540664SAndrew Jeffery } else {
4113a540664SAndrew Jeffery assert(astlpc->mode == MCTP_BINDING_ASTLPC_MODE_HOST);
4123a540664SAndrew Jeffery
4133a540664SAndrew Jeffery layout->rx.offset = be32toh(hdr.layout.rx_offset);
4143a540664SAndrew Jeffery layout->rx.size = be32toh(hdr.layout.rx_size);
4153a540664SAndrew Jeffery layout->tx.offset = be32toh(hdr.layout.tx_offset);
4163a540664SAndrew Jeffery layout->tx.size = be32toh(hdr.layout.tx_size);
4173a540664SAndrew Jeffery }
4183a540664SAndrew Jeffery
4193a540664SAndrew Jeffery return 0;
4203a540664SAndrew Jeffery }
4213a540664SAndrew Jeffery
mctp_astlpc_layout_write(struct mctp_binding_astlpc * astlpc,struct mctp_astlpc_layout * layout)4223a540664SAndrew Jeffery static int mctp_astlpc_layout_write(struct mctp_binding_astlpc *astlpc,
4233a540664SAndrew Jeffery struct mctp_astlpc_layout *layout)
4243a540664SAndrew Jeffery {
4253a540664SAndrew Jeffery uint32_t rx_size_be;
4263a540664SAndrew Jeffery
4273a540664SAndrew Jeffery if (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_BMC) {
4283a540664SAndrew Jeffery struct mctp_lpcmap_hdr hdr;
4293a540664SAndrew Jeffery
4303a540664SAndrew Jeffery /*
4313a540664SAndrew Jeffery * Flip the buffers as the names are defined in terms of the
4323a540664SAndrew Jeffery * host
4333a540664SAndrew Jeffery */
4343a540664SAndrew Jeffery hdr.layout.rx_offset = htobe32(layout->tx.offset);
4353a540664SAndrew Jeffery hdr.layout.rx_size = htobe32(layout->tx.size);
4363a540664SAndrew Jeffery hdr.layout.tx_offset = htobe32(layout->rx.offset);
4373a540664SAndrew Jeffery hdr.layout.tx_size = htobe32(layout->rx.size);
4383a540664SAndrew Jeffery
4393a540664SAndrew Jeffery return mctp_astlpc_lpc_write(astlpc, &hdr.layout,
440a721c2d8SPatrick Williams offsetof(struct mctp_lpcmap_hdr,
441a721c2d8SPatrick Williams layout),
4423a540664SAndrew Jeffery sizeof(hdr.layout));
4433a540664SAndrew Jeffery }
4443a540664SAndrew Jeffery
4453a540664SAndrew Jeffery assert(astlpc->mode == MCTP_BINDING_ASTLPC_MODE_HOST);
4463a540664SAndrew Jeffery
4473a540664SAndrew Jeffery /*
4483a540664SAndrew Jeffery * As of v2 we only need to write rx_size - the offsets are controlled
4493a540664SAndrew Jeffery * by the BMC, as is the BMC's rx_size (host tx_size).
4503a540664SAndrew Jeffery */
4513a540664SAndrew Jeffery rx_size_be = htobe32(layout->rx.size);
4523a540664SAndrew Jeffery return mctp_astlpc_lpc_write(astlpc, &rx_size_be,
453a721c2d8SPatrick Williams offsetof(struct mctp_lpcmap_hdr,
454a721c2d8SPatrick Williams layout.rx_size),
4553a540664SAndrew Jeffery sizeof(rx_size_be));
4563a540664SAndrew Jeffery }
4573a540664SAndrew Jeffery
45888412be4SAndrew Jeffery static bool
mctp_astlpc_buffer_validate(const struct mctp_binding_astlpc * astlpc,const struct mctp_astlpc_buffer * buf,const char * name)45988412be4SAndrew Jeffery mctp_astlpc_buffer_validate(const struct mctp_binding_astlpc *astlpc,
46088412be4SAndrew Jeffery const struct mctp_astlpc_buffer *buf,
4613a540664SAndrew Jeffery const char *name)
4623a540664SAndrew Jeffery {
4633a540664SAndrew Jeffery /* Check for overflow */
4643a540664SAndrew Jeffery if (buf->offset + buf->size < buf->offset) {
4653a540664SAndrew Jeffery mctp_prerr(
4663a540664SAndrew Jeffery "%s packet buffer parameters overflow: offset: 0x%" PRIx32
4673a540664SAndrew Jeffery ", size: %" PRIu32,
4683a540664SAndrew Jeffery name, buf->offset, buf->size);
4693a540664SAndrew Jeffery return false;
4703a540664SAndrew Jeffery }
4713a540664SAndrew Jeffery
4723a540664SAndrew Jeffery /* Check that the buffers are contained within the allocated space */
4733a540664SAndrew Jeffery if (buf->offset + buf->size > LPC_WIN_SIZE) {
4743a540664SAndrew Jeffery mctp_prerr(
4753a540664SAndrew Jeffery "%s packet buffer parameters exceed %uM window size: offset: 0x%" PRIx32
4763a540664SAndrew Jeffery ", size: %" PRIu32,
4773a540664SAndrew Jeffery name, (LPC_WIN_SIZE / (1024 * 1024)), buf->offset,
4783a540664SAndrew Jeffery buf->size);
4793a540664SAndrew Jeffery return false;
4803a540664SAndrew Jeffery }
4813a540664SAndrew Jeffery
4823a540664SAndrew Jeffery /* Check that the baseline transmission unit is supported */
483a721c2d8SPatrick Williams if (buf->size <
484a721c2d8SPatrick Williams astlpc->proto->packet_size(MCTP_PACKET_SIZE(MCTP_BTU))) {
4853a540664SAndrew Jeffery mctp_prerr(
486a721c2d8SPatrick Williams "%s packet buffer too small: Require %" PRIu32
487a721c2d8SPatrick Williams " bytes to support the %u byte baseline transmission unit, found %" PRIu32,
48888412be4SAndrew Jeffery name,
48988412be4SAndrew Jeffery astlpc->proto->packet_size(MCTP_PACKET_SIZE(MCTP_BTU)),
4903a540664SAndrew Jeffery MCTP_BTU, buf->size);
4913a540664SAndrew Jeffery return false;
4923a540664SAndrew Jeffery }
4933a540664SAndrew Jeffery
4943a540664SAndrew Jeffery /* Check for overlap with the control space */
4953a540664SAndrew Jeffery if (buf->offset < control_size) {
4963a540664SAndrew Jeffery mctp_prerr(
4973a540664SAndrew Jeffery "%s packet buffer overlaps control region {0x%" PRIx32
4983a540664SAndrew Jeffery ", %" PRIu32 "}: Rx {0x%" PRIx32 ", %" PRIu32 "}",
4993a540664SAndrew Jeffery name, 0U, control_size, buf->offset, buf->size);
5003a540664SAndrew Jeffery return false;
5013a540664SAndrew Jeffery }
5023a540664SAndrew Jeffery
5033a540664SAndrew Jeffery return true;
5043a540664SAndrew Jeffery }
5053a540664SAndrew Jeffery
50688412be4SAndrew Jeffery static bool
mctp_astlpc_layout_validate(const struct mctp_binding_astlpc * astlpc,const struct mctp_astlpc_layout * layout)50788412be4SAndrew Jeffery mctp_astlpc_layout_validate(const struct mctp_binding_astlpc *astlpc,
50888412be4SAndrew Jeffery const struct mctp_astlpc_layout *layout)
5093a540664SAndrew Jeffery {
51088412be4SAndrew Jeffery const struct mctp_astlpc_buffer *rx = &layout->rx;
51188412be4SAndrew Jeffery const struct mctp_astlpc_buffer *tx = &layout->tx;
5123a540664SAndrew Jeffery bool rx_valid, tx_valid;
5133a540664SAndrew Jeffery
51488412be4SAndrew Jeffery rx_valid = mctp_astlpc_buffer_validate(astlpc, rx, "Rx");
51588412be4SAndrew Jeffery tx_valid = mctp_astlpc_buffer_validate(astlpc, tx, "Tx");
5163a540664SAndrew Jeffery
5173a540664SAndrew Jeffery if (!(rx_valid && tx_valid))
5183a540664SAndrew Jeffery return false;
5193a540664SAndrew Jeffery
5203a540664SAndrew Jeffery /* Check that the buffers are disjoint */
5213a540664SAndrew Jeffery if ((rx->offset <= tx->offset && rx->offset + rx->size > tx->offset) ||
5223a540664SAndrew Jeffery (tx->offset <= rx->offset && tx->offset + tx->size > rx->offset)) {
5233a540664SAndrew Jeffery mctp_prerr("Rx and Tx packet buffers overlap: Rx {0x%" PRIx32
5243a540664SAndrew Jeffery ", %" PRIu32 "}, Tx {0x%" PRIx32 ", %" PRIu32 "}",
5253a540664SAndrew Jeffery rx->offset, rx->size, tx->offset, tx->size);
5263a540664SAndrew Jeffery return false;
5273a540664SAndrew Jeffery }
5283a540664SAndrew Jeffery
5293a540664SAndrew Jeffery return true;
5303a540664SAndrew Jeffery }
5313a540664SAndrew Jeffery
mctp_astlpc_init_bmc(struct mctp_binding_astlpc * astlpc)5327cd72f14SAndrew Jeffery static int mctp_astlpc_init_bmc(struct mctp_binding_astlpc *astlpc)
5337cd72f14SAndrew Jeffery {
53455fb90beSAndrew Jeffery struct mctp_lpcmap_hdr hdr = { 0 };
5357cd72f14SAndrew Jeffery uint8_t status;
53688412be4SAndrew Jeffery uint32_t sz;
5373a540664SAndrew Jeffery
5383a540664SAndrew Jeffery /*
5393a540664SAndrew Jeffery * The largest buffer size is half of the allocated MCTP space
5403a540664SAndrew Jeffery * excluding the control space.
5413a540664SAndrew Jeffery */
5423a540664SAndrew Jeffery sz = ((LPC_WIN_SIZE - control_size) / 2);
5433a540664SAndrew Jeffery
5443a540664SAndrew Jeffery /*
5453a540664SAndrew Jeffery * Trim the MTU to a multiple of 16 to meet the requirements of 12.17
5463a540664SAndrew Jeffery * Query Hop in DSP0236 v1.3.0.
5473a540664SAndrew Jeffery */
54888412be4SAndrew Jeffery sz = MCTP_BODY_SIZE(astlpc->proto->body_size(sz));
5493a540664SAndrew Jeffery sz &= ~0xfUL;
55088412be4SAndrew Jeffery sz = astlpc->proto->packet_size(MCTP_PACKET_SIZE(sz));
5517cd72f14SAndrew Jeffery
552a9368980SAndrew Jeffery if (astlpc->requested_mtu) {
55388412be4SAndrew Jeffery uint32_t rpkt, rmtu;
554a9368980SAndrew Jeffery
55588412be4SAndrew Jeffery rmtu = astlpc->requested_mtu;
55688412be4SAndrew Jeffery rpkt = astlpc->proto->packet_size(MCTP_PACKET_SIZE(rmtu));
55788412be4SAndrew Jeffery sz = MIN(sz, rpkt);
558a9368980SAndrew Jeffery }
559a9368980SAndrew Jeffery
5607cd72f14SAndrew Jeffery /* Flip the buffers as the names are defined in terms of the host */
5613a540664SAndrew Jeffery astlpc->layout.tx.offset = control_size;
5623a540664SAndrew Jeffery astlpc->layout.tx.size = sz;
5633a540664SAndrew Jeffery astlpc->layout.rx.offset =
5643a540664SAndrew Jeffery astlpc->layout.tx.offset + astlpc->layout.tx.size;
5653a540664SAndrew Jeffery astlpc->layout.rx.size = sz;
5663a540664SAndrew Jeffery
56788412be4SAndrew Jeffery if (!mctp_astlpc_layout_validate(astlpc, &astlpc->layout)) {
56888412be4SAndrew Jeffery astlpc_prerr(astlpc, "Cannot support an MTU of %" PRIu32, sz);
569a9368980SAndrew Jeffery return -EINVAL;
570a9368980SAndrew Jeffery }
5717cd72f14SAndrew Jeffery
57255fb90beSAndrew Jeffery hdr = (struct mctp_lpcmap_hdr){
57355fb90beSAndrew Jeffery .magic = htobe32(ASTLPC_MCTP_MAGIC),
57455fb90beSAndrew Jeffery .bmc_ver_min = htobe16(ASTLPC_VER_MIN),
57555fb90beSAndrew Jeffery .bmc_ver_cur = htobe16(ASTLPC_VER_CUR),
5767cd72f14SAndrew Jeffery
5777cd72f14SAndrew Jeffery /* Flip the buffers back as we're now describing the host's
5787cd72f14SAndrew Jeffery * configuration to the host */
5793a540664SAndrew Jeffery .layout.rx_offset = htobe32(astlpc->layout.tx.offset),
5803a540664SAndrew Jeffery .layout.rx_size = htobe32(astlpc->layout.tx.size),
5813a540664SAndrew Jeffery .layout.tx_offset = htobe32(astlpc->layout.rx.offset),
5823a540664SAndrew Jeffery .layout.tx_size = htobe32(astlpc->layout.rx.size),
58355fb90beSAndrew Jeffery };
5847cd72f14SAndrew Jeffery
58555fb90beSAndrew Jeffery mctp_astlpc_lpc_write(astlpc, &hdr, 0, sizeof(hdr));
5867cd72f14SAndrew Jeffery
587b3b55a6bSAndrew Jeffery /*
588b3b55a6bSAndrew Jeffery * Set status indicating that the BMC is now active. Be explicit about
589b3b55a6bSAndrew Jeffery * clearing OBF; we're reinitialising the binding and so any previous
590b3b55a6bSAndrew Jeffery * buffer state is irrelevant.
591b3b55a6bSAndrew Jeffery */
592b3b55a6bSAndrew Jeffery status = KCS_STATUS_BMC_READY & ~KCS_STATUS_OBF;
593d0f5da0dSAndrew Jeffery return mctp_astlpc_kcs_set_status(astlpc, status);
5947cd72f14SAndrew Jeffery }
5957cd72f14SAndrew Jeffery
mctp_binding_astlpc_start_bmc(struct mctp_binding * b)5967cd72f14SAndrew Jeffery static int mctp_binding_astlpc_start_bmc(struct mctp_binding *b)
5977cd72f14SAndrew Jeffery {
5987cd72f14SAndrew Jeffery struct mctp_binding_astlpc *astlpc =
5997cd72f14SAndrew Jeffery container_of(b, struct mctp_binding_astlpc, binding);
6007cd72f14SAndrew Jeffery
60188412be4SAndrew Jeffery astlpc->proto = &astlpc_protocol_version[ASTLPC_VER_CUR];
60288412be4SAndrew Jeffery
6037cd72f14SAndrew Jeffery return mctp_astlpc_init_bmc(astlpc);
6047cd72f14SAndrew Jeffery }
6057cd72f14SAndrew Jeffery
mctp_astlpc_validate_version(uint16_t bmc_ver_min,uint16_t bmc_ver_cur,uint16_t host_ver_min,uint16_t host_ver_cur)6064e8264b7SAndrew Jeffery static bool mctp_astlpc_validate_version(uint16_t bmc_ver_min,
6074e8264b7SAndrew Jeffery uint16_t bmc_ver_cur,
6084e8264b7SAndrew Jeffery uint16_t host_ver_min,
6094e8264b7SAndrew Jeffery uint16_t host_ver_cur)
6104e8264b7SAndrew Jeffery {
6114e8264b7SAndrew Jeffery if (!(bmc_ver_min && bmc_ver_cur && host_ver_min && host_ver_cur)) {
6124e8264b7SAndrew Jeffery mctp_prerr("Invalid version present in [%" PRIu16 ", %" PRIu16
6134e8264b7SAndrew Jeffery "], [%" PRIu16 ", %" PRIu16 "]",
6144e8264b7SAndrew Jeffery bmc_ver_min, bmc_ver_cur, host_ver_min,
6154e8264b7SAndrew Jeffery host_ver_cur);
6164e8264b7SAndrew Jeffery return false;
6174e8264b7SAndrew Jeffery } else if (bmc_ver_min > bmc_ver_cur) {
6184e8264b7SAndrew Jeffery mctp_prerr("Invalid bmc version range [%" PRIu16 ", %" PRIu16
6194e8264b7SAndrew Jeffery "]",
6204e8264b7SAndrew Jeffery bmc_ver_min, bmc_ver_cur);
6214e8264b7SAndrew Jeffery return false;
6224e8264b7SAndrew Jeffery } else if (host_ver_min > host_ver_cur) {
6234e8264b7SAndrew Jeffery mctp_prerr("Invalid host version range [%" PRIu16 ", %" PRIu16
6244e8264b7SAndrew Jeffery "]",
6254e8264b7SAndrew Jeffery host_ver_min, host_ver_cur);
6264e8264b7SAndrew Jeffery return false;
6274e8264b7SAndrew Jeffery } else if ((host_ver_cur < bmc_ver_min) ||
6284e8264b7SAndrew Jeffery (host_ver_min > bmc_ver_cur)) {
6294e8264b7SAndrew Jeffery mctp_prerr(
6304e8264b7SAndrew Jeffery "Unable to satisfy version negotiation with ranges [%" PRIu16
6314e8264b7SAndrew Jeffery ", %" PRIu16 "] and [%" PRIu16 ", %" PRIu16 "]",
6324e8264b7SAndrew Jeffery bmc_ver_min, bmc_ver_cur, host_ver_min, host_ver_cur);
6334e8264b7SAndrew Jeffery return false;
6344e8264b7SAndrew Jeffery }
6354e8264b7SAndrew Jeffery
6364e8264b7SAndrew Jeffery return true;
6374e8264b7SAndrew Jeffery }
6384e8264b7SAndrew Jeffery
mctp_astlpc_negotiate_layout_host(struct mctp_binding_astlpc * astlpc)6393a540664SAndrew Jeffery static int mctp_astlpc_negotiate_layout_host(struct mctp_binding_astlpc *astlpc)
6403a540664SAndrew Jeffery {
6413a540664SAndrew Jeffery struct mctp_astlpc_layout layout;
64288412be4SAndrew Jeffery uint32_t rmtu;
6433a540664SAndrew Jeffery uint32_t sz;
6443a540664SAndrew Jeffery int rc;
6453a540664SAndrew Jeffery
6463a540664SAndrew Jeffery rc = mctp_astlpc_layout_read(astlpc, &layout);
6473a540664SAndrew Jeffery if (rc < 0)
6483a540664SAndrew Jeffery return rc;
6493a540664SAndrew Jeffery
65088412be4SAndrew Jeffery if (!mctp_astlpc_layout_validate(astlpc, &layout)) {
6513a540664SAndrew Jeffery astlpc_prerr(
6523a540664SAndrew Jeffery astlpc,
6533a540664SAndrew Jeffery "BMC provided invalid buffer layout: Rx {0x%" PRIx32
6543a540664SAndrew Jeffery ", %" PRIu32 "}, Tx {0x%" PRIx32 ", %" PRIu32 "}",
6553a540664SAndrew Jeffery layout.rx.offset, layout.rx.size, layout.tx.offset,
6563a540664SAndrew Jeffery layout.tx.size);
6573a540664SAndrew Jeffery return -EINVAL;
6583a540664SAndrew Jeffery }
6593a540664SAndrew Jeffery
660a9368980SAndrew Jeffery astlpc_prinfo(astlpc, "Desire an MTU of %" PRIu32 " bytes",
661a9368980SAndrew Jeffery astlpc->requested_mtu);
662a9368980SAndrew Jeffery
66388412be4SAndrew Jeffery rmtu = astlpc->requested_mtu;
66488412be4SAndrew Jeffery sz = astlpc->proto->packet_size(MCTP_PACKET_SIZE(rmtu));
6653a540664SAndrew Jeffery layout.rx.size = sz;
6663a540664SAndrew Jeffery
66788412be4SAndrew Jeffery if (!mctp_astlpc_layout_validate(astlpc, &layout)) {
6683a540664SAndrew Jeffery astlpc_prerr(
6693a540664SAndrew Jeffery astlpc,
6703a540664SAndrew Jeffery "Generated invalid buffer layout with size %" PRIu32
6713a540664SAndrew Jeffery ": Rx {0x%" PRIx32 ", %" PRIu32 "}, Tx {0x%" PRIx32
6723a540664SAndrew Jeffery ", %" PRIu32 "}",
6733a540664SAndrew Jeffery sz, layout.rx.offset, layout.rx.size, layout.tx.offset,
6743a540664SAndrew Jeffery layout.tx.size);
6753a540664SAndrew Jeffery return -EINVAL;
6763a540664SAndrew Jeffery }
6773a540664SAndrew Jeffery
678a9368980SAndrew Jeffery astlpc_prinfo(astlpc, "Requesting MTU of %" PRIu32 " bytes",
679a9368980SAndrew Jeffery astlpc->requested_mtu);
6803a540664SAndrew Jeffery
6813a540664SAndrew Jeffery return mctp_astlpc_layout_write(astlpc, &layout);
6823a540664SAndrew Jeffery }
6833a540664SAndrew Jeffery
mctp_astlpc_negotiate_version(uint16_t bmc_ver_min,uint16_t bmc_ver_cur,uint16_t host_ver_min,uint16_t host_ver_cur)68488412be4SAndrew Jeffery static uint16_t mctp_astlpc_negotiate_version(uint16_t bmc_ver_min,
68588412be4SAndrew Jeffery uint16_t bmc_ver_cur,
68688412be4SAndrew Jeffery uint16_t host_ver_min,
68788412be4SAndrew Jeffery uint16_t host_ver_cur)
68888412be4SAndrew Jeffery {
68988412be4SAndrew Jeffery if (!mctp_astlpc_validate_version(bmc_ver_min, bmc_ver_cur,
69088412be4SAndrew Jeffery host_ver_min, host_ver_cur))
69188412be4SAndrew Jeffery return ASTLPC_VER_BAD;
69288412be4SAndrew Jeffery
69388412be4SAndrew Jeffery if (bmc_ver_cur < host_ver_cur)
69488412be4SAndrew Jeffery return bmc_ver_cur;
69588412be4SAndrew Jeffery
69688412be4SAndrew Jeffery return host_ver_cur;
69788412be4SAndrew Jeffery }
69888412be4SAndrew Jeffery
mctp_astlpc_init_host(struct mctp_binding_astlpc * astlpc)6997cd72f14SAndrew Jeffery static int mctp_astlpc_init_host(struct mctp_binding_astlpc *astlpc)
7007cd72f14SAndrew Jeffery {
70155fb90beSAndrew Jeffery const uint16_t ver_min_be = htobe16(ASTLPC_VER_MIN);
70255fb90beSAndrew Jeffery const uint16_t ver_cur_be = htobe16(ASTLPC_VER_CUR);
70388412be4SAndrew Jeffery uint16_t bmc_ver_min, bmc_ver_cur, negotiated;
70455fb90beSAndrew Jeffery struct mctp_lpcmap_hdr hdr;
7057cd72f14SAndrew Jeffery uint8_t status;
7067cd72f14SAndrew Jeffery int rc;
7077cd72f14SAndrew Jeffery
708f13cb971SAndrew Jeffery rc = mctp_astlpc_kcs_read(astlpc, MCTP_ASTLPC_KCS_REG_STATUS, &status);
7097cd72f14SAndrew Jeffery if (rc) {
7107cd72f14SAndrew Jeffery mctp_prwarn("KCS status read failed");
7117cd72f14SAndrew Jeffery return rc;
7127cd72f14SAndrew Jeffery }
7137cd72f14SAndrew Jeffery
7147cd72f14SAndrew Jeffery astlpc->kcs_status = status;
7157cd72f14SAndrew Jeffery
7167cd72f14SAndrew Jeffery if (!(status & KCS_STATUS_BMC_READY))
7177cd72f14SAndrew Jeffery return -EHOSTDOWN;
7187cd72f14SAndrew Jeffery
71955fb90beSAndrew Jeffery mctp_astlpc_lpc_read(astlpc, &hdr, 0, sizeof(hdr));
7207cd72f14SAndrew Jeffery
7214e8264b7SAndrew Jeffery bmc_ver_min = be16toh(hdr.bmc_ver_min);
7224e8264b7SAndrew Jeffery bmc_ver_cur = be16toh(hdr.bmc_ver_cur);
7234e8264b7SAndrew Jeffery
72488412be4SAndrew Jeffery /* Calculate the expected value of negotiated_ver */
725a721c2d8SPatrick Williams negotiated = mctp_astlpc_negotiate_version(
726a721c2d8SPatrick Williams bmc_ver_min, bmc_ver_cur, ASTLPC_VER_MIN, ASTLPC_VER_CUR);
72788412be4SAndrew Jeffery if (!negotiated) {
7284e8264b7SAndrew Jeffery astlpc_prerr(astlpc, "Cannot negotiate with invalid versions");
7294e8264b7SAndrew Jeffery return -EINVAL;
7304e8264b7SAndrew Jeffery }
7314e8264b7SAndrew Jeffery
73288412be4SAndrew Jeffery /* Assign protocol ops so we can calculate the packet buffer sizes */
73388412be4SAndrew Jeffery assert(negotiated < ARRAY_SIZE(astlpc_protocol_version));
73488412be4SAndrew Jeffery astlpc->proto = &astlpc_protocol_version[negotiated];
73588412be4SAndrew Jeffery
73688412be4SAndrew Jeffery /* Negotiate packet buffers in v2 style if the BMC supports it */
73788412be4SAndrew Jeffery if (negotiated >= 2) {
7383a540664SAndrew Jeffery rc = mctp_astlpc_negotiate_layout_host(astlpc);
7393a540664SAndrew Jeffery if (rc < 0)
7403a540664SAndrew Jeffery return rc;
7413a540664SAndrew Jeffery }
7423a540664SAndrew Jeffery
74388412be4SAndrew Jeffery /* Advertise the host's supported protocol versions */
74455fb90beSAndrew Jeffery mctp_astlpc_lpc_write(astlpc, &ver_min_be,
74555fb90beSAndrew Jeffery offsetof(struct mctp_lpcmap_hdr, host_ver_min),
74655fb90beSAndrew Jeffery sizeof(ver_min_be));
7477cd72f14SAndrew Jeffery
74855fb90beSAndrew Jeffery mctp_astlpc_lpc_write(astlpc, &ver_cur_be,
74955fb90beSAndrew Jeffery offsetof(struct mctp_lpcmap_hdr, host_ver_cur),
75055fb90beSAndrew Jeffery sizeof(ver_cur_be));
7517cd72f14SAndrew Jeffery
7527cd72f14SAndrew Jeffery /* Send channel init command */
753f13cb971SAndrew Jeffery rc = mctp_astlpc_kcs_write(astlpc, MCTP_ASTLPC_KCS_REG_DATA, 0x0);
7547cd72f14SAndrew Jeffery if (rc) {
7559101a2abSAndrew Jeffery astlpc_prwarn(astlpc, "KCS write failed");
7567cd72f14SAndrew Jeffery }
7577cd72f14SAndrew Jeffery
75888412be4SAndrew Jeffery /*
75988412be4SAndrew Jeffery * Configure the host so `astlpc->proto->version == 0` holds until we
76088412be4SAndrew Jeffery * receive a subsequent status update from the BMC. Until then,
76188412be4SAndrew Jeffery * `astlpc->proto->version == 0` indicates that we're yet to complete
76288412be4SAndrew Jeffery * the channel initialisation handshake.
76388412be4SAndrew Jeffery *
76488412be4SAndrew Jeffery * When the BMC provides a status update with KCS_STATUS_CHANNEL_ACTIVE
76588412be4SAndrew Jeffery * set we will assign the appropriate protocol ops struct in accordance
76688412be4SAndrew Jeffery * with `negotiated_ver`.
76788412be4SAndrew Jeffery */
76888412be4SAndrew Jeffery astlpc->proto = &astlpc_protocol_version[ASTLPC_VER_BAD];
76988412be4SAndrew Jeffery
7707cd72f14SAndrew Jeffery return rc;
7717cd72f14SAndrew Jeffery }
7727cd72f14SAndrew Jeffery
mctp_binding_astlpc_start_host(struct mctp_binding * b)7737cd72f14SAndrew Jeffery static int mctp_binding_astlpc_start_host(struct mctp_binding *b)
7747cd72f14SAndrew Jeffery {
7757cd72f14SAndrew Jeffery struct mctp_binding_astlpc *astlpc =
7767cd72f14SAndrew Jeffery container_of(b, struct mctp_binding_astlpc, binding);
7777cd72f14SAndrew Jeffery
7787cd72f14SAndrew Jeffery return mctp_astlpc_init_host(astlpc);
7797cd72f14SAndrew Jeffery }
7807cd72f14SAndrew Jeffery
__mctp_astlpc_kcs_ready(struct mctp_binding_astlpc * astlpc,uint8_t status,bool is_write)7817cd72f14SAndrew Jeffery static bool __mctp_astlpc_kcs_ready(struct mctp_binding_astlpc *astlpc,
7827cd72f14SAndrew Jeffery uint8_t status, bool is_write)
7837cd72f14SAndrew Jeffery {
7847cd72f14SAndrew Jeffery bool is_bmc;
7857cd72f14SAndrew Jeffery bool ready_state;
7867cd72f14SAndrew Jeffery uint8_t flag;
7877cd72f14SAndrew Jeffery
7887cd72f14SAndrew Jeffery is_bmc = (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_BMC);
7897cd72f14SAndrew Jeffery flag = (is_bmc ^ is_write) ? KCS_STATUS_IBF : KCS_STATUS_OBF;
7907cd72f14SAndrew Jeffery ready_state = is_write ? 0 : 1;
7917cd72f14SAndrew Jeffery
7927cd72f14SAndrew Jeffery return !!(status & flag) == ready_state;
7937cd72f14SAndrew Jeffery }
7947cd72f14SAndrew Jeffery
7957cd72f14SAndrew Jeffery static inline bool
mctp_astlpc_kcs_read_ready(struct mctp_binding_astlpc * astlpc,uint8_t status)7967cd72f14SAndrew Jeffery mctp_astlpc_kcs_read_ready(struct mctp_binding_astlpc *astlpc, uint8_t status)
7977cd72f14SAndrew Jeffery {
7987cd72f14SAndrew Jeffery return __mctp_astlpc_kcs_ready(astlpc, status, false);
7997cd72f14SAndrew Jeffery }
8007cd72f14SAndrew Jeffery
8017cd72f14SAndrew Jeffery static inline bool
mctp_astlpc_kcs_write_ready(struct mctp_binding_astlpc * astlpc,uint8_t status)8027cd72f14SAndrew Jeffery mctp_astlpc_kcs_write_ready(struct mctp_binding_astlpc *astlpc, uint8_t status)
8037cd72f14SAndrew Jeffery {
8047cd72f14SAndrew Jeffery return __mctp_astlpc_kcs_ready(astlpc, status, true);
8057cd72f14SAndrew Jeffery }
8067cd72f14SAndrew Jeffery
mctp_astlpc_kcs_send(struct mctp_binding_astlpc * astlpc,enum mctp_astlpc_cmd data)807672c8852SJeremy Kerr static int mctp_astlpc_kcs_send(struct mctp_binding_astlpc *astlpc,
80845d13328SRashmica Gupta enum mctp_astlpc_cmd data)
809672c8852SJeremy Kerr {
810672c8852SJeremy Kerr uint8_t status;
811672c8852SJeremy Kerr int rc;
812672c8852SJeremy Kerr
813a721c2d8SPatrick Williams rc = mctp_astlpc_kcs_read(astlpc, MCTP_ASTLPC_KCS_REG_STATUS, &status);
8141b27fe87SAndrew Jeffery if (rc) {
8159101a2abSAndrew Jeffery astlpc_prwarn(astlpc, "KCS status read failed");
816fe763e98SAndrew Jeffery return -EIO;
817672c8852SJeremy Kerr }
818fe763e98SAndrew Jeffery if (!mctp_astlpc_kcs_write_ready(astlpc, status))
819fe763e98SAndrew Jeffery return -EBUSY;
820672c8852SJeremy Kerr
821f13cb971SAndrew Jeffery rc = mctp_astlpc_kcs_write(astlpc, MCTP_ASTLPC_KCS_REG_DATA, data);
822bc53d35aSJeremy Kerr if (rc) {
8239101a2abSAndrew Jeffery astlpc_prwarn(astlpc, "KCS data write failed");
824fe763e98SAndrew Jeffery return -EIO;
825672c8852SJeremy Kerr }
826672c8852SJeremy Kerr
827672c8852SJeremy Kerr return 0;
828672c8852SJeremy Kerr }
829672c8852SJeremy Kerr
mctp_binding_astlpc_tx(struct mctp_binding * b,struct mctp_pktbuf * pkt)830672c8852SJeremy Kerr static int mctp_binding_astlpc_tx(struct mctp_binding *b,
831672c8852SJeremy Kerr struct mctp_pktbuf *pkt)
832672c8852SJeremy Kerr {
833672c8852SJeremy Kerr struct mctp_binding_astlpc *astlpc = binding_to_astlpc(b);
83455fb90beSAndrew Jeffery uint32_t len, len_be;
835edfe383fSAndrew Jeffery struct mctp_hdr *hdr;
836fe763e98SAndrew Jeffery int rc;
837672c8852SJeremy Kerr
838edfe383fSAndrew Jeffery hdr = mctp_pktbuf_hdr(pkt);
839672c8852SJeremy Kerr len = mctp_pktbuf_size(pkt);
840edfe383fSAndrew Jeffery
8419101a2abSAndrew Jeffery astlpc_prdebug(astlpc,
8429101a2abSAndrew Jeffery "%s: Transmitting %" PRIu32
8439101a2abSAndrew Jeffery "-byte packet (%hhu, %hhu, 0x%hhx)",
844edfe383fSAndrew Jeffery __func__, len, hdr->src, hdr->dest, hdr->flags_seq_tag);
845edfe383fSAndrew Jeffery
84688412be4SAndrew Jeffery if (len > astlpc->proto->body_size(astlpc->layout.tx.size)) {
8470721f585SAndrew Jeffery astlpc_prwarn(astlpc, "invalid TX len %" PRIu32 ": %" PRIu32,
8480721f585SAndrew Jeffery len,
849eba19a3bSAndrew Jeffery astlpc->proto->body_size(astlpc->layout.tx.size));
8500721f585SAndrew Jeffery return -EMSGSIZE;
851672c8852SJeremy Kerr }
852672c8852SJeremy Kerr
853fe763e98SAndrew Jeffery mctp_binding_set_tx_enabled(b, false);
854fe763e98SAndrew Jeffery
85555fb90beSAndrew Jeffery len_be = htobe32(len);
85655fb90beSAndrew Jeffery mctp_astlpc_lpc_write(astlpc, &len_be, astlpc->layout.tx.offset,
85755fb90beSAndrew Jeffery sizeof(len_be));
858eba19a3bSAndrew Jeffery
859eba19a3bSAndrew Jeffery astlpc->proto->pktbuf_protect(pkt);
860eba19a3bSAndrew Jeffery len = mctp_pktbuf_size(pkt);
861eba19a3bSAndrew Jeffery
86255fb90beSAndrew Jeffery mctp_astlpc_lpc_write(astlpc, hdr, astlpc->layout.tx.offset + 4, len);
863672c8852SJeremy Kerr
864fe763e98SAndrew Jeffery astlpc->layout.tx.state = buffer_state_prepared;
865672c8852SJeremy Kerr
86645d13328SRashmica Gupta rc = mctp_astlpc_kcs_send(astlpc, cmd_tx_begin);
867fe763e98SAndrew Jeffery if (!rc)
868fe763e98SAndrew Jeffery astlpc->layout.tx.state = buffer_state_released;
869eba19a3bSAndrew Jeffery
870fe763e98SAndrew Jeffery return rc == -EBUSY ? 0 : rc;
871672c8852SJeremy Kerr }
872672c8852SJeremy Kerr
873*4a09e1dcSMatt Johnston /* Update binding pkt_size and reallocate tx_storage */
mctp_astlpc_set_pkt_size(struct mctp_binding_astlpc * astlpc,size_t pkt_size)874*4a09e1dcSMatt Johnston static int mctp_astlpc_set_pkt_size(struct mctp_binding_astlpc *astlpc,
875*4a09e1dcSMatt Johnston size_t pkt_size)
876*4a09e1dcSMatt Johnston {
877*4a09e1dcSMatt Johnston size_t body = MCTP_BODY_SIZE(pkt_size);
878*4a09e1dcSMatt Johnston body += astlpc->binding.pkt_header + astlpc->binding.pkt_trailer;
879*4a09e1dcSMatt Johnston size_t pktbuf_size = MCTP_PKTBUF_SIZE(body);
880*4a09e1dcSMatt Johnston /* Reallocate TX storage */
881*4a09e1dcSMatt Johnston if (astlpc->binding.tx_storage) {
882*4a09e1dcSMatt Johnston __mctp_free(astlpc->binding.tx_storage);
883*4a09e1dcSMatt Johnston }
884*4a09e1dcSMatt Johnston astlpc->binding.tx_storage = __mctp_alloc(pktbuf_size);
885*4a09e1dcSMatt Johnston if (!astlpc->binding.tx_storage) {
886*4a09e1dcSMatt Johnston return -ENOMEM;
887*4a09e1dcSMatt Johnston }
888*4a09e1dcSMatt Johnston
889*4a09e1dcSMatt Johnston astlpc->binding.pkt_size = pkt_size;
890*4a09e1dcSMatt Johnston return 0;
891*4a09e1dcSMatt Johnston }
892*4a09e1dcSMatt Johnston
mctp_astlpc_calculate_mtu(struct mctp_binding_astlpc * astlpc,struct mctp_astlpc_layout * layout)8933a540664SAndrew Jeffery static uint32_t mctp_astlpc_calculate_mtu(struct mctp_binding_astlpc *astlpc,
8943a540664SAndrew Jeffery struct mctp_astlpc_layout *layout)
8953a540664SAndrew Jeffery {
89688412be4SAndrew Jeffery uint32_t low, high, limit, rpkt;
8973a540664SAndrew Jeffery
8983a540664SAndrew Jeffery /* Derive the largest MTU the BMC _can_ support */
8993a540664SAndrew Jeffery low = MIN(astlpc->layout.rx.offset, astlpc->layout.tx.offset);
9003a540664SAndrew Jeffery high = MAX(astlpc->layout.rx.offset, astlpc->layout.tx.offset);
9013a540664SAndrew Jeffery limit = high - low;
9023a540664SAndrew Jeffery
903a9368980SAndrew Jeffery /* Determine the largest MTU the BMC _wants_ to support */
904a9368980SAndrew Jeffery if (astlpc->requested_mtu) {
90588412be4SAndrew Jeffery uint32_t rmtu = astlpc->requested_mtu;
906a9368980SAndrew Jeffery
90788412be4SAndrew Jeffery rpkt = astlpc->proto->packet_size(MCTP_PACKET_SIZE(rmtu));
90888412be4SAndrew Jeffery limit = MIN(limit, rpkt);
909a9368980SAndrew Jeffery }
9103a540664SAndrew Jeffery
9113a540664SAndrew Jeffery /* Determine the accepted MTU, applied both directions by convention */
91288412be4SAndrew Jeffery rpkt = MIN(limit, layout->tx.size);
91388412be4SAndrew Jeffery return MCTP_BODY_SIZE(astlpc->proto->body_size(rpkt));
9143a540664SAndrew Jeffery }
9153a540664SAndrew Jeffery
mctp_astlpc_negotiate_layout_bmc(struct mctp_binding_astlpc * astlpc)91688412be4SAndrew Jeffery static int mctp_astlpc_negotiate_layout_bmc(struct mctp_binding_astlpc *astlpc)
9173a540664SAndrew Jeffery {
9183a540664SAndrew Jeffery struct mctp_astlpc_layout proposed, pending;
9193a540664SAndrew Jeffery uint32_t sz, mtu;
9203a540664SAndrew Jeffery int rc;
9213a540664SAndrew Jeffery
92288412be4SAndrew Jeffery /* Do we have a valid protocol version? */
92388412be4SAndrew Jeffery if (!astlpc->proto->version)
92488412be4SAndrew Jeffery return -EINVAL;
92588412be4SAndrew Jeffery
9263a540664SAndrew Jeffery /* Extract the host's proposed layout */
9273a540664SAndrew Jeffery rc = mctp_astlpc_layout_read(astlpc, &proposed);
9283a540664SAndrew Jeffery if (rc < 0)
9293a540664SAndrew Jeffery return rc;
9303a540664SAndrew Jeffery
93188412be4SAndrew Jeffery /* Do we have a reasonable layout? */
93288412be4SAndrew Jeffery if (!mctp_astlpc_layout_validate(astlpc, &proposed))
9333a540664SAndrew Jeffery return -EINVAL;
9343a540664SAndrew Jeffery
9353a540664SAndrew Jeffery /* Negotiate the MTU */
9363a540664SAndrew Jeffery mtu = mctp_astlpc_calculate_mtu(astlpc, &proposed);
93788412be4SAndrew Jeffery sz = astlpc->proto->packet_size(MCTP_PACKET_SIZE(mtu));
9383a540664SAndrew Jeffery
9393a540664SAndrew Jeffery /*
9403a540664SAndrew Jeffery * Use symmetric MTUs by convention and to pass constraints in rx/tx
9413a540664SAndrew Jeffery * functions
9423a540664SAndrew Jeffery */
9433a540664SAndrew Jeffery pending = astlpc->layout;
9443a540664SAndrew Jeffery pending.tx.size = sz;
9453a540664SAndrew Jeffery pending.rx.size = sz;
9463a540664SAndrew Jeffery
94788412be4SAndrew Jeffery if (mctp_astlpc_layout_validate(astlpc, &pending)) {
9483a540664SAndrew Jeffery /* We found a sensible Rx MTU, so honour it */
9493a540664SAndrew Jeffery astlpc->layout = pending;
9503a540664SAndrew Jeffery
9513a540664SAndrew Jeffery /* Enforce the negotiated MTU */
9523a540664SAndrew Jeffery rc = mctp_astlpc_layout_write(astlpc, &astlpc->layout);
9533a540664SAndrew Jeffery if (rc < 0)
9543a540664SAndrew Jeffery return rc;
9553a540664SAndrew Jeffery
9563a540664SAndrew Jeffery astlpc_prinfo(astlpc, "Negotiated an MTU of %" PRIu32 " bytes",
9573a540664SAndrew Jeffery mtu);
9583a540664SAndrew Jeffery } else {
9593a540664SAndrew Jeffery astlpc_prwarn(astlpc, "MTU negotiation failed");
9603a540664SAndrew Jeffery return -EINVAL;
9613a540664SAndrew Jeffery }
9623a540664SAndrew Jeffery
963*4a09e1dcSMatt Johnston if (astlpc->proto->version >= 2) {
964*4a09e1dcSMatt Johnston rc = mctp_astlpc_set_pkt_size(astlpc, MCTP_PACKET_SIZE(mtu));
965*4a09e1dcSMatt Johnston if (rc) {
966*4a09e1dcSMatt Johnston astlpc_prwarn(astlpc, "Allocation error");
967*4a09e1dcSMatt Johnston return rc;
968*4a09e1dcSMatt Johnston }
969*4a09e1dcSMatt Johnston }
9703a540664SAndrew Jeffery
9713a540664SAndrew Jeffery return 0;
9723a540664SAndrew Jeffery }
9733a540664SAndrew Jeffery
mctp_astlpc_init_channel(struct mctp_binding_astlpc * astlpc)974672c8852SJeremy Kerr static void mctp_astlpc_init_channel(struct mctp_binding_astlpc *astlpc)
975672c8852SJeremy Kerr {
9764e8264b7SAndrew Jeffery uint16_t negotiated, negotiated_be;
9774e8264b7SAndrew Jeffery struct mctp_lpcmap_hdr hdr;
9784e8264b7SAndrew Jeffery uint8_t status;
9793a540664SAndrew Jeffery int rc;
9804e8264b7SAndrew Jeffery
9814e8264b7SAndrew Jeffery mctp_astlpc_lpc_read(astlpc, &hdr, 0, sizeof(hdr));
9824e8264b7SAndrew Jeffery
9834e8264b7SAndrew Jeffery /* Version negotiation */
9847f7fdc1dSAndrew Jeffery negotiated = mctp_astlpc_negotiate_version(ASTLPC_VER_MIN,
9857f7fdc1dSAndrew Jeffery ASTLPC_VER_CUR,
9864e8264b7SAndrew Jeffery be16toh(hdr.host_ver_min),
9874e8264b7SAndrew Jeffery be16toh(hdr.host_ver_cur));
9884e8264b7SAndrew Jeffery
98988412be4SAndrew Jeffery /* MTU negotiation requires knowing which protocol we'll use */
99088412be4SAndrew Jeffery assert(negotiated < ARRAY_SIZE(astlpc_protocol_version));
99188412be4SAndrew Jeffery astlpc->proto = &astlpc_protocol_version[negotiated];
99288412be4SAndrew Jeffery
9933a540664SAndrew Jeffery /* Host Rx MTU negotiation: Failure terminates channel init */
99488412be4SAndrew Jeffery rc = mctp_astlpc_negotiate_layout_bmc(astlpc);
9953a540664SAndrew Jeffery if (rc < 0)
9963a540664SAndrew Jeffery negotiated = ASTLPC_VER_BAD;
9973a540664SAndrew Jeffery
9984e8264b7SAndrew Jeffery /* Populate the negotiated version */
9994e8264b7SAndrew Jeffery negotiated_be = htobe16(negotiated);
10004e8264b7SAndrew Jeffery mctp_astlpc_lpc_write(astlpc, &negotiated_be,
100155fb90beSAndrew Jeffery offsetof(struct mctp_lpcmap_hdr, negotiated_ver),
10024e8264b7SAndrew Jeffery sizeof(negotiated_be));
100355fb90beSAndrew Jeffery
1004fe763e98SAndrew Jeffery /* Track buffer ownership */
1005fe763e98SAndrew Jeffery astlpc->layout.tx.state = buffer_state_acquired;
1006fe763e98SAndrew Jeffery astlpc->layout.rx.state = buffer_state_released;
1007fe763e98SAndrew Jeffery
10084e8264b7SAndrew Jeffery /* Finalise the configuration */
10094e8264b7SAndrew Jeffery status = KCS_STATUS_BMC_READY | KCS_STATUS_OBF;
10104e8264b7SAndrew Jeffery if (negotiated > 0) {
10114e8264b7SAndrew Jeffery astlpc_prinfo(astlpc, "Negotiated binding version %" PRIu16,
10124e8264b7SAndrew Jeffery negotiated);
10134e8264b7SAndrew Jeffery status |= KCS_STATUS_CHANNEL_ACTIVE;
10144e8264b7SAndrew Jeffery } else {
101588412be4SAndrew Jeffery astlpc_prerr(astlpc, "Failed to initialise channel");
10164e8264b7SAndrew Jeffery }
1017672c8852SJeremy Kerr
10184e8264b7SAndrew Jeffery mctp_astlpc_kcs_set_status(astlpc, status);
10194e8264b7SAndrew Jeffery
10204e8264b7SAndrew Jeffery mctp_binding_set_tx_enabled(&astlpc->binding,
10214e8264b7SAndrew Jeffery status & KCS_STATUS_CHANNEL_ACTIVE);
1022672c8852SJeremy Kerr }
1023672c8852SJeremy Kerr
mctp_astlpc_rx_start(struct mctp_binding_astlpc * astlpc)1024672c8852SJeremy Kerr static void mctp_astlpc_rx_start(struct mctp_binding_astlpc *astlpc)
1025672c8852SJeremy Kerr {
1026672c8852SJeremy Kerr struct mctp_pktbuf *pkt;
1027f447b4f4SRashmica Gupta struct mctp_hdr *hdr;
1028eba19a3bSAndrew Jeffery uint32_t body, packet;
1029672c8852SJeremy Kerr
1030eba19a3bSAndrew Jeffery mctp_astlpc_lpc_read(astlpc, &body, astlpc->layout.rx.offset,
1031eba19a3bSAndrew Jeffery sizeof(body));
1032eba19a3bSAndrew Jeffery body = be32toh(body);
1033bc53d35aSJeremy Kerr
1034eba19a3bSAndrew Jeffery if (body > astlpc->proto->body_size(astlpc->layout.rx.size)) {
1035eba19a3bSAndrew Jeffery astlpc_prwarn(astlpc, "invalid RX len 0x%x", body);
1036672c8852SJeremy Kerr return;
1037672c8852SJeremy Kerr }
1038672c8852SJeremy Kerr
10390f05b6cbSAndrew Jeffery if ((size_t)body > astlpc->binding.pkt_size) {
1040eba19a3bSAndrew Jeffery astlpc_prwarn(astlpc, "invalid RX len 0x%x", body);
1041672c8852SJeremy Kerr return;
1042672c8852SJeremy Kerr }
1043672c8852SJeremy Kerr
1044eba19a3bSAndrew Jeffery /* Eliminate the medium-specific header that we just read */
1045eba19a3bSAndrew Jeffery packet = astlpc->proto->packet_size(body) - 4;
1046eba19a3bSAndrew Jeffery pkt = mctp_pktbuf_alloc(&astlpc->binding, packet);
1047ae59f4feSChristian Geddes if (!pkt) {
1048a721c2d8SPatrick Williams astlpc_prwarn(astlpc, "unable to allocate pktbuf len 0x%x",
1049a721c2d8SPatrick Williams packet);
1050ae59f4feSChristian Geddes return;
1051ae59f4feSChristian Geddes }
1052672c8852SJeremy Kerr
1053eba19a3bSAndrew Jeffery /*
1054eba19a3bSAndrew Jeffery * Read payload and medium-specific trailer from immediately after the
1055eba19a3bSAndrew Jeffery * medium-specific header.
1056eba19a3bSAndrew Jeffery */
105755fb90beSAndrew Jeffery mctp_astlpc_lpc_read(astlpc, mctp_pktbuf_hdr(pkt),
1058eba19a3bSAndrew Jeffery astlpc->layout.rx.offset + 4, packet);
1059672c8852SJeremy Kerr
1060fe763e98SAndrew Jeffery astlpc->layout.rx.state = buffer_state_prepared;
1061fe763e98SAndrew Jeffery
1062ae59f4feSChristian Geddes /* Inform the other side of the MCTP interface that we have read
1063ae59f4feSChristian Geddes * the packet off the bus before handling the contents of the packet.
1064ae59f4feSChristian Geddes */
106545d13328SRashmica Gupta if (!mctp_astlpc_kcs_send(astlpc, cmd_rx_complete))
1066fe763e98SAndrew Jeffery astlpc->layout.rx.state = buffer_state_released;
1067ae59f4feSChristian Geddes
1068f447b4f4SRashmica Gupta hdr = mctp_pktbuf_hdr(pkt);
1069f447b4f4SRashmica Gupta if (hdr->ver != 1) {
1070f447b4f4SRashmica Gupta mctp_pktbuf_free(pkt);
1071f447b4f4SRashmica Gupta astlpc_prdebug(astlpc, "Dropped packet with invalid version");
1072f447b4f4SRashmica Gupta return;
1073f447b4f4SRashmica Gupta }
1074f447b4f4SRashmica Gupta
1075eba19a3bSAndrew Jeffery /*
1076eba19a3bSAndrew Jeffery * v3 will validate the CRC32 in the medium-specific trailer and adjust
1077eba19a3bSAndrew Jeffery * the packet size accordingly. On older protocols validation is a no-op
1078eba19a3bSAndrew Jeffery * that always returns true.
1079eba19a3bSAndrew Jeffery */
1080eba19a3bSAndrew Jeffery if (astlpc->proto->pktbuf_validate(pkt)) {
1081672c8852SJeremy Kerr mctp_bus_rx(&astlpc->binding, pkt);
1082eba19a3bSAndrew Jeffery } else {
1083eba19a3bSAndrew Jeffery /* TODO: Drop any associated assembly */
1084eba19a3bSAndrew Jeffery astlpc_prdebug(astlpc, "Dropped corrupt packet");
1085eba19a3bSAndrew Jeffery }
1086*4a09e1dcSMatt Johnston mctp_pktbuf_free(pkt);
1087672c8852SJeremy Kerr }
1088672c8852SJeremy Kerr
mctp_astlpc_tx_complete(struct mctp_binding_astlpc * astlpc)1089672c8852SJeremy Kerr static void mctp_astlpc_tx_complete(struct mctp_binding_astlpc *astlpc)
1090672c8852SJeremy Kerr {
1091fe763e98SAndrew Jeffery astlpc->layout.tx.state = buffer_state_acquired;
1092672c8852SJeremy Kerr mctp_binding_set_tx_enabled(&astlpc->binding, true);
1093672c8852SJeremy Kerr }
1094672c8852SJeremy Kerr
mctp_astlpc_finalise_channel(struct mctp_binding_astlpc * astlpc)10954e8264b7SAndrew Jeffery static int mctp_astlpc_finalise_channel(struct mctp_binding_astlpc *astlpc)
10964e8264b7SAndrew Jeffery {
10973a540664SAndrew Jeffery struct mctp_astlpc_layout layout;
10984e8264b7SAndrew Jeffery uint16_t negotiated;
10994e8264b7SAndrew Jeffery int rc;
11004e8264b7SAndrew Jeffery
11014e8264b7SAndrew Jeffery rc = mctp_astlpc_lpc_read(astlpc, &negotiated,
11024e8264b7SAndrew Jeffery offsetof(struct mctp_lpcmap_hdr,
11034e8264b7SAndrew Jeffery negotiated_ver),
11044e8264b7SAndrew Jeffery sizeof(negotiated));
11054e8264b7SAndrew Jeffery if (rc < 0)
11064e8264b7SAndrew Jeffery return rc;
11074e8264b7SAndrew Jeffery
11084e8264b7SAndrew Jeffery negotiated = be16toh(negotiated);
110988412be4SAndrew Jeffery astlpc_prerr(astlpc, "Version negotiation got: %u", negotiated);
11104e8264b7SAndrew Jeffery
11114e8264b7SAndrew Jeffery if (negotiated == ASTLPC_VER_BAD || negotiated < ASTLPC_VER_MIN ||
11124e8264b7SAndrew Jeffery negotiated > ASTLPC_VER_CUR) {
11134e8264b7SAndrew Jeffery astlpc_prerr(astlpc, "Failed to negotiate version, got: %u\n",
11144e8264b7SAndrew Jeffery negotiated);
11154e8264b7SAndrew Jeffery return -EINVAL;
11164e8264b7SAndrew Jeffery }
11174e8264b7SAndrew Jeffery
111888412be4SAndrew Jeffery assert(negotiated < ARRAY_SIZE(astlpc_protocol_version));
111988412be4SAndrew Jeffery astlpc->proto = &astlpc_protocol_version[negotiated];
11204e8264b7SAndrew Jeffery
11213a540664SAndrew Jeffery rc = mctp_astlpc_layout_read(astlpc, &layout);
11223a540664SAndrew Jeffery if (rc < 0)
11233a540664SAndrew Jeffery return rc;
11243a540664SAndrew Jeffery
112588412be4SAndrew Jeffery if (!mctp_astlpc_layout_validate(astlpc, &layout)) {
11263a540664SAndrew Jeffery mctp_prerr("BMC proposed invalid buffer parameters");
11273a540664SAndrew Jeffery return -EINVAL;
11283a540664SAndrew Jeffery }
11293a540664SAndrew Jeffery
11303a540664SAndrew Jeffery astlpc->layout = layout;
11313a540664SAndrew Jeffery
11323a540664SAndrew Jeffery if (negotiated >= 2)
11333a540664SAndrew Jeffery astlpc->binding.pkt_size =
113488412be4SAndrew Jeffery astlpc->proto->body_size(astlpc->layout.tx.size);
11353a540664SAndrew Jeffery
1136fe763e98SAndrew Jeffery /* Track buffer ownership */
1137fe763e98SAndrew Jeffery astlpc->layout.tx.state = buffer_state_acquired;
1138fe763e98SAndrew Jeffery astlpc->layout.rx.state = buffer_state_released;
1139fe763e98SAndrew Jeffery
11404e8264b7SAndrew Jeffery return 0;
11414e8264b7SAndrew Jeffery }
11424e8264b7SAndrew Jeffery
mctp_astlpc_update_channel(struct mctp_binding_astlpc * astlpc,uint8_t status)11437cd72f14SAndrew Jeffery static int mctp_astlpc_update_channel(struct mctp_binding_astlpc *astlpc,
11447cd72f14SAndrew Jeffery uint8_t status)
11457cd72f14SAndrew Jeffery {
11467cd72f14SAndrew Jeffery uint8_t updated;
11477cd72f14SAndrew Jeffery int rc = 0;
11487cd72f14SAndrew Jeffery
11497cd72f14SAndrew Jeffery assert(astlpc->mode == MCTP_BINDING_ASTLPC_MODE_HOST);
11507cd72f14SAndrew Jeffery
11517cd72f14SAndrew Jeffery updated = astlpc->kcs_status ^ status;
11527cd72f14SAndrew Jeffery
1153d0f5da0dSAndrew Jeffery astlpc_prdebug(astlpc, "%s: status: 0x%x, update: 0x%x", __func__,
1154d0f5da0dSAndrew Jeffery status, updated);
1155d0f5da0dSAndrew Jeffery
11567cd72f14SAndrew Jeffery if (updated & KCS_STATUS_BMC_READY) {
1157d0f5da0dSAndrew Jeffery if (status & KCS_STATUS_BMC_READY) {
1158d0f5da0dSAndrew Jeffery astlpc->kcs_status = status;
1159d0f5da0dSAndrew Jeffery return astlpc->binding.start(&astlpc->binding);
1160d0f5da0dSAndrew Jeffery } else {
1161fe763e98SAndrew Jeffery /* Shut down the channel */
1162fe763e98SAndrew Jeffery astlpc->layout.rx.state = buffer_state_idle;
1163fe763e98SAndrew Jeffery astlpc->layout.tx.state = buffer_state_idle;
11647cd72f14SAndrew Jeffery mctp_binding_set_tx_enabled(&astlpc->binding, false);
11657cd72f14SAndrew Jeffery }
1166d0f5da0dSAndrew Jeffery }
11677cd72f14SAndrew Jeffery
116888412be4SAndrew Jeffery if (astlpc->proto->version == 0 ||
116988412be4SAndrew Jeffery updated & KCS_STATUS_CHANNEL_ACTIVE) {
11704e8264b7SAndrew Jeffery bool enable;
11714e8264b7SAndrew Jeffery
1172fe763e98SAndrew Jeffery astlpc->layout.rx.state = buffer_state_idle;
1173fe763e98SAndrew Jeffery astlpc->layout.tx.state = buffer_state_idle;
11744e8264b7SAndrew Jeffery rc = mctp_astlpc_finalise_channel(astlpc);
11754e8264b7SAndrew Jeffery enable = (status & KCS_STATUS_CHANNEL_ACTIVE) && rc == 0;
11764e8264b7SAndrew Jeffery mctp_binding_set_tx_enabled(&astlpc->binding, enable);
11774e8264b7SAndrew Jeffery }
11787cd72f14SAndrew Jeffery
11797cd72f14SAndrew Jeffery astlpc->kcs_status = status;
11807cd72f14SAndrew Jeffery
11817cd72f14SAndrew Jeffery return rc;
11827cd72f14SAndrew Jeffery }
11837cd72f14SAndrew Jeffery
mctp_astlpc_tx_done(struct mctp_binding_astlpc * astlpc)11844df106dbSFrederic Barrat bool mctp_astlpc_tx_done(struct mctp_binding_astlpc *astlpc)
11854df106dbSFrederic Barrat {
11864df106dbSFrederic Barrat return astlpc->layout.tx.state == buffer_state_acquired;
11874df106dbSFrederic Barrat }
11884df106dbSFrederic Barrat
mctp_astlpc_poll(struct mctp_binding_astlpc * astlpc)1189672c8852SJeremy Kerr int mctp_astlpc_poll(struct mctp_binding_astlpc *astlpc)
1190672c8852SJeremy Kerr {
1191bc53d35aSJeremy Kerr uint8_t status, data;
1192672c8852SJeremy Kerr int rc;
1193672c8852SJeremy Kerr
1194fe763e98SAndrew Jeffery if (astlpc->layout.rx.state == buffer_state_prepared)
119545d13328SRashmica Gupta if (!mctp_astlpc_kcs_send(astlpc, cmd_rx_complete))
1196fe763e98SAndrew Jeffery astlpc->layout.rx.state = buffer_state_released;
1197fe763e98SAndrew Jeffery
1198fe763e98SAndrew Jeffery if (astlpc->layout.tx.state == buffer_state_prepared)
119945d13328SRashmica Gupta if (!mctp_astlpc_kcs_send(astlpc, cmd_tx_begin))
1200fe763e98SAndrew Jeffery astlpc->layout.tx.state = buffer_state_released;
1201fe763e98SAndrew Jeffery
1202f13cb971SAndrew Jeffery rc = mctp_astlpc_kcs_read(astlpc, MCTP_ASTLPC_KCS_REG_STATUS, &status);
1203bc53d35aSJeremy Kerr if (rc) {
12049101a2abSAndrew Jeffery astlpc_prwarn(astlpc, "KCS read error");
1205672c8852SJeremy Kerr return -1;
1206672c8852SJeremy Kerr }
1207672c8852SJeremy Kerr
12089101a2abSAndrew Jeffery astlpc_prdebug(astlpc, "%s: status: 0x%hhx", __func__, status);
1209edfe383fSAndrew Jeffery
12107cd72f14SAndrew Jeffery if (!mctp_astlpc_kcs_read_ready(astlpc, status))
1211672c8852SJeremy Kerr return 0;
1212672c8852SJeremy Kerr
1213f13cb971SAndrew Jeffery rc = mctp_astlpc_kcs_read(astlpc, MCTP_ASTLPC_KCS_REG_DATA, &data);
1214bc53d35aSJeremy Kerr if (rc) {
12159101a2abSAndrew Jeffery astlpc_prwarn(astlpc, "KCS data read error");
1216bc53d35aSJeremy Kerr return -1;
1217bc53d35aSJeremy Kerr }
1218bc53d35aSJeremy Kerr
12199101a2abSAndrew Jeffery astlpc_prdebug(astlpc, "%s: data: 0x%hhx", __func__, data);
1220edfe383fSAndrew Jeffery
122145d13328SRashmica Gupta if (!astlpc->proto->version &&
122245d13328SRashmica Gupta !(data == cmd_initialise || data == cmd_dummy_value)) {
1223afcb701aSAndrew Jeffery astlpc_prwarn(astlpc, "Invalid message for binding state: 0x%x",
1224afcb701aSAndrew Jeffery data);
1225afcb701aSAndrew Jeffery return 0;
1226afcb701aSAndrew Jeffery }
1227afcb701aSAndrew Jeffery
1228672c8852SJeremy Kerr switch (data) {
122945d13328SRashmica Gupta case cmd_initialise:
1230672c8852SJeremy Kerr mctp_astlpc_init_channel(astlpc);
1231672c8852SJeremy Kerr break;
123245d13328SRashmica Gupta case cmd_tx_begin:
1233fe763e98SAndrew Jeffery if (astlpc->layout.rx.state != buffer_state_released) {
1234a721c2d8SPatrick Williams astlpc_prerr(
1235a721c2d8SPatrick Williams astlpc,
1236fe763e98SAndrew Jeffery "Protocol error: Invalid Rx buffer state for event %d: %d\n",
1237fe763e98SAndrew Jeffery data, astlpc->layout.rx.state);
1238fe763e98SAndrew Jeffery return 0;
1239fe763e98SAndrew Jeffery }
1240672c8852SJeremy Kerr mctp_astlpc_rx_start(astlpc);
1241672c8852SJeremy Kerr break;
124245d13328SRashmica Gupta case cmd_rx_complete:
1243fe763e98SAndrew Jeffery if (astlpc->layout.tx.state != buffer_state_released) {
1244a721c2d8SPatrick Williams astlpc_prerr(
1245a721c2d8SPatrick Williams astlpc,
1246fe763e98SAndrew Jeffery "Protocol error: Invalid Tx buffer state for event %d: %d\n",
1247fe763e98SAndrew Jeffery data, astlpc->layout.tx.state);
1248fe763e98SAndrew Jeffery return 0;
1249fe763e98SAndrew Jeffery }
1250672c8852SJeremy Kerr mctp_astlpc_tx_complete(astlpc);
1251672c8852SJeremy Kerr break;
125245d13328SRashmica Gupta case cmd_dummy_value:
12537cd72f14SAndrew Jeffery /* No responsibilities for the BMC on 0xff */
1254d0f5da0dSAndrew Jeffery if (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_HOST) {
1255d0f5da0dSAndrew Jeffery rc = mctp_astlpc_update_channel(astlpc, status);
1256d0f5da0dSAndrew Jeffery if (rc < 0)
1257d0f5da0dSAndrew Jeffery return rc;
1258d0f5da0dSAndrew Jeffery }
1259d0f5da0dSAndrew Jeffery break;
1260672c8852SJeremy Kerr default:
12619101a2abSAndrew Jeffery astlpc_prwarn(astlpc, "unknown message 0x%x", data);
1262672c8852SJeremy Kerr }
1263d0f5da0dSAndrew Jeffery
1264d0f5da0dSAndrew Jeffery /* Handle silent loss of bmc-ready */
1265d0f5da0dSAndrew Jeffery if (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_HOST) {
126645d13328SRashmica Gupta if (!(status & KCS_STATUS_BMC_READY && data == cmd_dummy_value))
1267d0f5da0dSAndrew Jeffery return mctp_astlpc_update_channel(astlpc, status);
1268d0f5da0dSAndrew Jeffery }
1269d0f5da0dSAndrew Jeffery
1270d0f5da0dSAndrew Jeffery return rc;
1271672c8852SJeremy Kerr }
1272672c8852SJeremy Kerr
1273bc53d35aSJeremy Kerr /* allocate and basic initialisation */
__mctp_astlpc_init(uint8_t mode,uint32_t mtu)12747cd72f14SAndrew Jeffery static struct mctp_binding_astlpc *__mctp_astlpc_init(uint8_t mode,
12757cd72f14SAndrew Jeffery uint32_t mtu)
1276bc53d35aSJeremy Kerr {
1277bc53d35aSJeremy Kerr struct mctp_binding_astlpc *astlpc;
1278bc53d35aSJeremy Kerr
12797cd72f14SAndrew Jeffery assert((mode == MCTP_BINDING_ASTLPC_MODE_BMC) ||
12807cd72f14SAndrew Jeffery (mode == MCTP_BINDING_ASTLPC_MODE_HOST));
12817cd72f14SAndrew Jeffery
1282bc53d35aSJeremy Kerr astlpc = __mctp_alloc(sizeof(*astlpc));
12837cd72f14SAndrew Jeffery if (!astlpc)
12847cd72f14SAndrew Jeffery return NULL;
12857cd72f14SAndrew Jeffery
1286bc53d35aSJeremy Kerr memset(astlpc, 0, sizeof(*astlpc));
12877cd72f14SAndrew Jeffery astlpc->mode = mode;
12887cd72f14SAndrew Jeffery astlpc->lpc_map = NULL;
1289fe763e98SAndrew Jeffery astlpc->layout.rx.state = buffer_state_idle;
1290fe763e98SAndrew Jeffery astlpc->layout.tx.state = buffer_state_idle;
1291a9368980SAndrew Jeffery astlpc->requested_mtu = mtu;
1292bc53d35aSJeremy Kerr astlpc->binding.name = "astlpc";
1293bc53d35aSJeremy Kerr astlpc->binding.version = 1;
1294eba19a3bSAndrew Jeffery astlpc->binding.pkt_header = 4;
1295eba19a3bSAndrew Jeffery astlpc->binding.pkt_trailer = 4;
12967cd72f14SAndrew Jeffery astlpc->binding.tx = mctp_binding_astlpc_tx;
12977cd72f14SAndrew Jeffery if (mode == MCTP_BINDING_ASTLPC_MODE_BMC)
12987cd72f14SAndrew Jeffery astlpc->binding.start = mctp_binding_astlpc_start_bmc;
12997cd72f14SAndrew Jeffery else if (mode == MCTP_BINDING_ASTLPC_MODE_HOST)
13007cd72f14SAndrew Jeffery astlpc->binding.start = mctp_binding_astlpc_start_host;
13017cd72f14SAndrew Jeffery else {
13029101a2abSAndrew Jeffery astlpc_prerr(astlpc, "%s: Invalid mode: %d\n", __func__, mode);
13037cd72f14SAndrew Jeffery __mctp_free(astlpc);
13047cd72f14SAndrew Jeffery return NULL;
13057cd72f14SAndrew Jeffery }
1306bc53d35aSJeremy Kerr
1307*4a09e1dcSMatt Johnston if (mctp_astlpc_set_pkt_size(
1308*4a09e1dcSMatt Johnston astlpc,
1309*4a09e1dcSMatt Johnston MCTP_PACKET_SIZE(mtu > MCTP_BTU ? mtu : MCTP_BTU)) != 0) {
1310*4a09e1dcSMatt Johnston astlpc_prerr(astlpc, "%s: Allocation error", __func__);
1311*4a09e1dcSMatt Johnston __mctp_free(astlpc);
1312*4a09e1dcSMatt Johnston return NULL;
1313*4a09e1dcSMatt Johnston }
1314*4a09e1dcSMatt Johnston
1315bc53d35aSJeremy Kerr return astlpc;
1316bc53d35aSJeremy Kerr }
1317bc53d35aSJeremy Kerr
mctp_binding_astlpc_core(struct mctp_binding_astlpc * b)13183b36d17cSJeremy Kerr struct mctp_binding *mctp_binding_astlpc_core(struct mctp_binding_astlpc *b)
13193b36d17cSJeremy Kerr {
13203b36d17cSJeremy Kerr return &b->binding;
13213b36d17cSJeremy Kerr }
13223b36d17cSJeremy Kerr
13237cd72f14SAndrew Jeffery struct mctp_binding_astlpc *
mctp_astlpc_init(uint8_t mode,uint32_t mtu,void * lpc_map,const struct mctp_binding_astlpc_ops * ops,void * ops_data)13247cd72f14SAndrew Jeffery mctp_astlpc_init(uint8_t mode, uint32_t mtu, void *lpc_map,
13257cd72f14SAndrew Jeffery const struct mctp_binding_astlpc_ops *ops, void *ops_data)
1326bc53d35aSJeremy Kerr {
1327bc53d35aSJeremy Kerr struct mctp_binding_astlpc *astlpc;
1328bc53d35aSJeremy Kerr
13297cd72f14SAndrew Jeffery if (!(mode == MCTP_BINDING_ASTLPC_MODE_BMC ||
13307cd72f14SAndrew Jeffery mode == MCTP_BINDING_ASTLPC_MODE_HOST)) {
13317cd72f14SAndrew Jeffery mctp_prerr("Unknown binding mode: %u", mode);
13327cd72f14SAndrew Jeffery return NULL;
13337cd72f14SAndrew Jeffery }
13347cd72f14SAndrew Jeffery
13357cd72f14SAndrew Jeffery astlpc = __mctp_astlpc_init(mode, mtu);
1336bc53d35aSJeremy Kerr if (!astlpc)
1337bc53d35aSJeremy Kerr return NULL;
1338bc53d35aSJeremy Kerr
1339bc53d35aSJeremy Kerr memcpy(&astlpc->ops, ops, sizeof(astlpc->ops));
1340bc53d35aSJeremy Kerr astlpc->ops_data = ops_data;
1341bc53d35aSJeremy Kerr astlpc->lpc_map = lpc_map;
13427cd72f14SAndrew Jeffery astlpc->mode = mode;
1343bc53d35aSJeremy Kerr
1344bc53d35aSJeremy Kerr return astlpc;
1345bc53d35aSJeremy Kerr }
1346bc53d35aSJeremy Kerr
13477cd72f14SAndrew Jeffery struct mctp_binding_astlpc *
mctp_astlpc_init_ops(const struct mctp_binding_astlpc_ops * ops,void * ops_data,void * lpc_map)13487cd72f14SAndrew Jeffery mctp_astlpc_init_ops(const struct mctp_binding_astlpc_ops *ops, void *ops_data,
13497cd72f14SAndrew Jeffery void *lpc_map)
13507cd72f14SAndrew Jeffery {
13517cd72f14SAndrew Jeffery return mctp_astlpc_init(MCTP_BINDING_ASTLPC_MODE_BMC, MCTP_BTU, lpc_map,
13527cd72f14SAndrew Jeffery ops, ops_data);
13537cd72f14SAndrew Jeffery }
13547cd72f14SAndrew Jeffery
mctp_astlpc_destroy(struct mctp_binding_astlpc * astlpc)13554663f67cSAndrew Jeffery void mctp_astlpc_destroy(struct mctp_binding_astlpc *astlpc)
13564663f67cSAndrew Jeffery {
1357d0f5da0dSAndrew Jeffery /* Clear channel-active and bmc-ready */
1358d0f5da0dSAndrew Jeffery if (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_BMC)
13597c4509aaSAndrew Jeffery mctp_astlpc_kcs_set_status(astlpc, 0);
1360*4a09e1dcSMatt Johnston __mctp_free(astlpc->binding.tx_storage);
13614663f67cSAndrew Jeffery __mctp_free(astlpc);
13624663f67cSAndrew Jeffery }
13634663f67cSAndrew Jeffery
1364b214c643SJeremy Kerr #ifdef MCTP_HAVE_FILEIO
136555fb90beSAndrew Jeffery
mctp_astlpc_init_fileio_lpc(struct mctp_binding_astlpc * astlpc)1366bc53d35aSJeremy Kerr static int mctp_astlpc_init_fileio_lpc(struct mctp_binding_astlpc *astlpc)
1367672c8852SJeremy Kerr {
1368672c8852SJeremy Kerr struct aspeed_lpc_ctrl_mapping map = {
1369672c8852SJeremy Kerr .window_type = ASPEED_LPC_CTRL_WINDOW_MEMORY,
1370672c8852SJeremy Kerr .window_id = 0, /* There's only one */
1371672c8852SJeremy Kerr .flags = 0,
1372672c8852SJeremy Kerr .addr = 0,
1373672c8852SJeremy Kerr .offset = 0,
1374672c8852SJeremy Kerr .size = 0
1375672c8852SJeremy Kerr };
1376979c6a16SAndrew Jeffery void *lpc_map_base;
1377672c8852SJeremy Kerr int fd, rc;
1378672c8852SJeremy Kerr
1379672c8852SJeremy Kerr fd = open(lpc_path, O_RDWR | O_SYNC);
1380672c8852SJeremy Kerr if (fd < 0) {
13819101a2abSAndrew Jeffery astlpc_prwarn(astlpc, "LPC open (%s) failed", lpc_path);
1382672c8852SJeremy Kerr return -1;
1383672c8852SJeremy Kerr }
1384672c8852SJeremy Kerr
1385672c8852SJeremy Kerr rc = ioctl(fd, ASPEED_LPC_CTRL_IOCTL_GET_SIZE, &map);
1386672c8852SJeremy Kerr if (rc) {
13879101a2abSAndrew Jeffery astlpc_prwarn(astlpc, "LPC GET_SIZE failed");
1388672c8852SJeremy Kerr close(fd);
1389672c8852SJeremy Kerr return -1;
1390672c8852SJeremy Kerr }
1391672c8852SJeremy Kerr
1392c1d5c543SAndrew Jeffery /*
1393c1d5c543SAndrew Jeffery *
1394c1d5c543SAndrew Jeffery *
1395c1d5c543SAndrew Jeffery * Decouple ourselves from hiomapd[1] (another user of the FW2AHB) by
1396c1d5c543SAndrew Jeffery * mapping the FW2AHB to the reserved memory here as well.
1397c1d5c543SAndrew Jeffery *
1398c1d5c543SAndrew Jeffery * It's not possible to use the MCTP ASTLPC binding on machines that
1399c1d5c543SAndrew Jeffery * need the FW2AHB bridge mapped anywhere except to the reserved memory
1400c1d5c543SAndrew Jeffery * (e.g. the host SPI NOR).
1401c1d5c543SAndrew Jeffery *
1402c1d5c543SAndrew Jeffery * [1] https://github.com/openbmc/hiomapd/
1403c1d5c543SAndrew Jeffery *
1404c1d5c543SAndrew Jeffery *
1405c1d5c543SAndrew Jeffery *
1406c1d5c543SAndrew Jeffery * The following calculation must align with what's going on in
1407c1d5c543SAndrew Jeffery * hiomapd's lpc.c so as not to disrupt its behaviour:
1408c1d5c543SAndrew Jeffery *
1409c1d5c543SAndrew Jeffery * https://github.com/openbmc/hiomapd/blob/5ff50e3cbd7702aefc185264e4adfb9952040575/lpc.c#L68
1410c1d5c543SAndrew Jeffery *
1411c1d5c543SAndrew Jeffery *
1412c1d5c543SAndrew Jeffery */
1413c1d5c543SAndrew Jeffery
1414c1d5c543SAndrew Jeffery /* Map the reserved memory at the top of the 28-bit LPC firmware address space */
1415c1d5c543SAndrew Jeffery map.addr = 0x0FFFFFFF & -map.size;
1416a721c2d8SPatrick Williams astlpc_prinfo(
1417a721c2d8SPatrick Williams astlpc,
1418c1d5c543SAndrew Jeffery "Configuring FW2AHB to map reserved memory at 0x%08x for 0x%x in the LPC FW cycle address-space",
1419c1d5c543SAndrew Jeffery map.addr, map.size);
1420c1d5c543SAndrew Jeffery
1421c1d5c543SAndrew Jeffery rc = ioctl(fd, ASPEED_LPC_CTRL_IOCTL_MAP, &map);
1422c1d5c543SAndrew Jeffery if (rc) {
1423a721c2d8SPatrick Williams astlpc_prwarn(astlpc,
1424a721c2d8SPatrick Williams "Failed to map FW2AHB to reserved memory");
1425c1d5c543SAndrew Jeffery close(fd);
1426c1d5c543SAndrew Jeffery return -1;
1427c1d5c543SAndrew Jeffery }
1428c1d5c543SAndrew Jeffery
1429c1d5c543SAndrew Jeffery /* Map the reserved memory into our address space */
1430979c6a16SAndrew Jeffery lpc_map_base =
1431979c6a16SAndrew Jeffery mmap(NULL, map.size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
1432979c6a16SAndrew Jeffery if (lpc_map_base == MAP_FAILED) {
14339101a2abSAndrew Jeffery astlpc_prwarn(astlpc, "LPC mmap failed");
1434672c8852SJeremy Kerr rc = -1;
1435672c8852SJeremy Kerr } else {
14363ef47785SMatt Johnston astlpc->lpc_map =
14373ef47785SMatt Johnston (uint8_t *)lpc_map_base + map.size - LPC_WIN_SIZE;
1438672c8852SJeremy Kerr }
1439672c8852SJeremy Kerr
1440672c8852SJeremy Kerr close(fd);
1441672c8852SJeremy Kerr
1442672c8852SJeremy Kerr return rc;
1443672c8852SJeremy Kerr }
1444672c8852SJeremy Kerr
mctp_astlpc_init_fileio_kcs(struct mctp_binding_astlpc * astlpc,const char * kcs_path)1445400766f9SKonstantin Aladyshev static int mctp_astlpc_init_fileio_kcs(struct mctp_binding_astlpc *astlpc,
1446400766f9SKonstantin Aladyshev const char *kcs_path)
1447672c8852SJeremy Kerr {
1448672c8852SJeremy Kerr astlpc->kcs_fd = open(kcs_path, O_RDWR);
1449672c8852SJeremy Kerr if (astlpc->kcs_fd < 0)
1450672c8852SJeremy Kerr return -1;
1451672c8852SJeremy Kerr
1452672c8852SJeremy Kerr return 0;
1453672c8852SJeremy Kerr }
1454672c8852SJeremy Kerr
__mctp_astlpc_fileio_kcs_read(void * arg,enum mctp_binding_astlpc_kcs_reg reg,uint8_t * val)1455bc53d35aSJeremy Kerr static int __mctp_astlpc_fileio_kcs_read(void *arg,
1456a721c2d8SPatrick Williams enum mctp_binding_astlpc_kcs_reg reg,
1457a721c2d8SPatrick Williams uint8_t *val)
1458bc53d35aSJeremy Kerr {
1459bc53d35aSJeremy Kerr struct mctp_binding_astlpc *astlpc = arg;
1460bc53d35aSJeremy Kerr off_t offset = reg;
1461bc53d35aSJeremy Kerr int rc;
1462bc53d35aSJeremy Kerr
1463bc53d35aSJeremy Kerr rc = pread(astlpc->kcs_fd, val, 1, offset);
1464bc53d35aSJeremy Kerr
1465bc53d35aSJeremy Kerr return rc == 1 ? 0 : -1;
1466bc53d35aSJeremy Kerr }
1467bc53d35aSJeremy Kerr
__mctp_astlpc_fileio_kcs_write(void * arg,enum mctp_binding_astlpc_kcs_reg reg,uint8_t val)1468bc53d35aSJeremy Kerr static int __mctp_astlpc_fileio_kcs_write(void *arg,
1469a721c2d8SPatrick Williams enum mctp_binding_astlpc_kcs_reg reg,
1470a721c2d8SPatrick Williams uint8_t val)
1471bc53d35aSJeremy Kerr {
1472bc53d35aSJeremy Kerr struct mctp_binding_astlpc *astlpc = arg;
1473bc53d35aSJeremy Kerr off_t offset = reg;
1474bc53d35aSJeremy Kerr int rc;
1475bc53d35aSJeremy Kerr
1476bc53d35aSJeremy Kerr rc = pwrite(astlpc->kcs_fd, &val, 1, offset);
1477bc53d35aSJeremy Kerr
1478bc53d35aSJeremy Kerr return rc == 1 ? 0 : -1;
1479bc53d35aSJeremy Kerr }
1480bc53d35aSJeremy Kerr
mctp_astlpc_init_pollfd(struct mctp_binding_astlpc * astlpc,struct pollfd * pollfd)14811111c6a5SAndrew Jeffery int mctp_astlpc_init_pollfd(struct mctp_binding_astlpc *astlpc,
14821111c6a5SAndrew Jeffery struct pollfd *pollfd)
14831111c6a5SAndrew Jeffery {
1484fe763e98SAndrew Jeffery bool release;
1485fe763e98SAndrew Jeffery
14861111c6a5SAndrew Jeffery pollfd->fd = astlpc->kcs_fd;
1487fe763e98SAndrew Jeffery pollfd->events = 0;
1488fe763e98SAndrew Jeffery
1489fe763e98SAndrew Jeffery release = astlpc->layout.rx.state == buffer_state_prepared ||
1490fe763e98SAndrew Jeffery astlpc->layout.tx.state == buffer_state_prepared;
1491fe763e98SAndrew Jeffery
1492fe763e98SAndrew Jeffery pollfd->events = release ? POLLOUT : POLLIN;
14931111c6a5SAndrew Jeffery
14941111c6a5SAndrew Jeffery return 0;
14951111c6a5SAndrew Jeffery }
14961111c6a5SAndrew Jeffery
mctp_astlpc_init_fileio(const char * kcs_path)1497400766f9SKonstantin Aladyshev struct mctp_binding_astlpc *mctp_astlpc_init_fileio(const char *kcs_path)
1498672c8852SJeremy Kerr {
1499672c8852SJeremy Kerr struct mctp_binding_astlpc *astlpc;
1500672c8852SJeremy Kerr int rc;
1501672c8852SJeremy Kerr
15027cd72f14SAndrew Jeffery /*
15037cd72f14SAndrew Jeffery * If we're doing file IO then we're very likely not running
15048877c460SAndrew Jeffery * freestanding, so lets assume that we're on the BMC side.
15058877c460SAndrew Jeffery *
15068877c460SAndrew Jeffery * Requesting an MTU of 0 requests the largest possible MTU, whatever
15078877c460SAndrew Jeffery * value that might take.
15087cd72f14SAndrew Jeffery */
15098877c460SAndrew Jeffery astlpc = __mctp_astlpc_init(MCTP_BINDING_ASTLPC_MODE_BMC, 0);
1510bc53d35aSJeremy Kerr if (!astlpc)
1511bc53d35aSJeremy Kerr return NULL;
1512672c8852SJeremy Kerr
1513bc53d35aSJeremy Kerr /* Set internal operations for kcs. We use direct accesses to the lpc
1514bc53d35aSJeremy Kerr * map area */
1515bc53d35aSJeremy Kerr astlpc->ops.kcs_read = __mctp_astlpc_fileio_kcs_read;
1516bc53d35aSJeremy Kerr astlpc->ops.kcs_write = __mctp_astlpc_fileio_kcs_write;
1517bc53d35aSJeremy Kerr astlpc->ops_data = astlpc;
1518bc53d35aSJeremy Kerr
1519bc53d35aSJeremy Kerr rc = mctp_astlpc_init_fileio_lpc(astlpc);
1520672c8852SJeremy Kerr if (rc) {
1521672c8852SJeremy Kerr free(astlpc);
1522672c8852SJeremy Kerr return NULL;
1523672c8852SJeremy Kerr }
1524672c8852SJeremy Kerr
1525400766f9SKonstantin Aladyshev rc = mctp_astlpc_init_fileio_kcs(astlpc, kcs_path);
1526672c8852SJeremy Kerr if (rc) {
1527672c8852SJeremy Kerr free(astlpc);
1528672c8852SJeremy Kerr return NULL;
1529672c8852SJeremy Kerr }
1530672c8852SJeremy Kerr
1531672c8852SJeremy Kerr return astlpc;
1532672c8852SJeremy Kerr }
153392a10a6bSJeremy Kerr #else
1534400766f9SKonstantin Aladyshev struct mctp_binding_astlpc *
mctp_astlpc_init_fileio(const char * kcs_path __unused)1535400766f9SKonstantin Aladyshev mctp_astlpc_init_fileio(const char *kcs_path __unused)
153692a10a6bSJeremy Kerr {
15375b2702dbSAndrew Jeffery mctp_prlog(MCTP_LOG_ERR, "%s: Missing support for file IO", __func__);
153892a10a6bSJeremy Kerr return NULL;
153992a10a6bSJeremy Kerr }
1540672c8852SJeremy Kerr
mctp_astlpc_init_pollfd(struct mctp_binding_astlpc * astlpc __unused,struct pollfd * pollfd __unused)15411111c6a5SAndrew Jeffery int mctp_astlpc_init_pollfd(struct mctp_binding_astlpc *astlpc __unused,
15421111c6a5SAndrew Jeffery struct pollfd *pollfd __unused)
15431111c6a5SAndrew Jeffery {
15441111c6a5SAndrew Jeffery mctp_prlog(MCTP_LOG_ERR, "%s: Missing support for file IO", __func__);
15451111c6a5SAndrew Jeffery return -1;
15461111c6a5SAndrew Jeffery }
154792a10a6bSJeremy Kerr #endif
1548