blob: 97ea0d269aa905fba56f7f3e1544ab108c4cd2e1 [file] [log] [blame]
Jeremy Kerr3d36ee22019-05-30 11:15:37 +08001/* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */
Jeremy Kerr672c8852019-03-01 12:18:07 +08002
Andrew Jeffery3286f172020-03-17 23:04:13 +10303#if HAVE_CONFIG_H
4#include "config.h"
5#endif
6
7#if HAVE_ENDIAN_H
Jeremy Kerr92a10a62019-08-28 16:55:54 +05308#include <endian.h>
Andrew Jeffery3286f172020-03-17 23:04:13 +10309#endif
10
11#include <assert.h>
Andrew Jeffery59c6a5c2020-01-17 15:52:51 +103012#include <err.h>
Andrew Jeffery7cd72f12020-05-12 20:27:59 +093013#include <errno.h>
Andrew Jefferyedfe3832020-02-06 11:52:11 +103014#include <inttypes.h>
Jeremy Kerr672c8852019-03-01 12:18:07 +080015#include <stdbool.h>
16#include <stdlib.h>
17#include <string.h>
Jeremy Kerr672c8852019-03-01 12:18:07 +080018
Jeremy Kerr672c8852019-03-01 12:18:07 +080019#define pr_fmt(x) "astlpc: " x
20
21#include "libmctp.h"
22#include "libmctp-alloc.h"
23#include "libmctp-log.h"
24#include "libmctp-astlpc.h"
Przemyslaw Czarnowskiff25d7e2020-03-26 11:39:37 +010025#include "container_of.h"
Jeremy Kerr672c8852019-03-01 12:18:07 +080026
Jeremy Kerrb214c642019-11-27 11:34:00 +080027#ifdef MCTP_HAVE_FILEIO
Jeremy Kerr92a10a62019-08-28 16:55:54 +053028
Jeremy Kerrc6f676d2019-12-19 09:24:06 +080029#include <unistd.h>
Jeremy Kerr92a10a62019-08-28 16:55:54 +053030#include <fcntl.h>
31#include <sys/ioctl.h>
32#include <sys/mman.h>
33#include <linux/aspeed-lpc-ctrl.h>
34
35/* kernel interface */
36static const char *kcs_path = "/dev/mctp0";
37static const char *lpc_path = "/dev/aspeed-lpc-ctrl";
38
39#endif
40
Andrew Jeffery7cd72f12020-05-12 20:27:59 +093041struct mctp_astlpc_buffer {
42 uint32_t offset;
43 uint32_t size;
44};
45
46struct mctp_astlpc_layout {
47 struct mctp_astlpc_buffer rx;
48 struct mctp_astlpc_buffer tx;
49};
50
Jeremy Kerr672c8852019-03-01 12:18:07 +080051struct mctp_binding_astlpc {
52 struct mctp_binding binding;
Jeremy Kerrbc53d352019-08-28 14:26:14 +053053
Andrew Jeffery55fb90b2020-05-12 13:54:37 +093054 void *lpc_map;
Andrew Jeffery7cd72f12020-05-12 20:27:59 +093055 struct mctp_astlpc_layout layout;
56
57 uint8_t mode;
Andrew Jeffery4e8264b2020-05-23 20:34:33 +093058 uint16_t version;
Jeremy Kerrbc53d352019-08-28 14:26:14 +053059
60 /* direct ops data */
Andrew Jeffery55fb90b2020-05-12 13:54:37 +093061 struct mctp_binding_astlpc_ops ops;
62 void *ops_data;
Jeremy Kerrbc53d352019-08-28 14:26:14 +053063
64 /* fileio ops data */
Andrew Jeffery979c6a12020-05-23 20:04:49 +093065 int kcs_fd;
66 uint8_t kcs_status;
Jeremy Kerr672c8852019-03-01 12:18:07 +080067
68 bool running;
Jeremy Kerr672c8852019-03-01 12:18:07 +080069};
70
Jeremy Kerr672c8852019-03-01 12:18:07 +080071#define binding_to_astlpc(b) \
72 container_of(b, struct mctp_binding_astlpc, binding)
73
Andrew Jeffery9101a2a2020-05-22 16:08:03 +093074#define astlpc_prlog(ctx, lvl, fmt, ...) \
75 do { \
76 bool __bmc = ((ctx)->mode == MCTP_BINDING_ASTLPC_MODE_BMC); \
77 mctp_prlog(lvl, pr_fmt("%s: " fmt), __bmc ? "bmc" : "host", \
78 ##__VA_ARGS__); \
79 } while (0)
80
81#define astlpc_prerr(ctx, fmt, ...) \
82 astlpc_prlog(ctx, MCTP_LOG_ERR, fmt, ##__VA_ARGS__)
83#define astlpc_prwarn(ctx, fmt, ...) \
84 astlpc_prlog(ctx, MCTP_LOG_WARNING, fmt, ##__VA_ARGS__)
85#define astlpc_prinfo(ctx, fmt, ...) \
86 astlpc_prlog(ctx, MCTP_LOG_INFO, fmt, ##__VA_ARGS__)
87#define astlpc_prdebug(ctx, fmt, ...) \
88 astlpc_prlog(ctx, MCTP_LOG_DEBUG, fmt, ##__VA_ARGS__)
89
Andrew Jeffery7cd72f12020-05-12 20:27:59 +093090/* clang-format off */
91#define ASTLPC_MCTP_MAGIC 0x4d435450
Andrew Jeffery4e8264b2020-05-23 20:34:33 +093092#define ASTLPC_VER_BAD 0
Andrew Jeffery7cd72f12020-05-12 20:27:59 +093093#define ASTLPC_VER_MIN 1
94#define ASTLPC_VER_CUR 1
95
96#define ASTLPC_BODY_SIZE(sz) ((sz) - 4)
97/* clang-format on */
Jeremy Kerr672c8852019-03-01 12:18:07 +080098
99struct mctp_lpcmap_hdr {
100 uint32_t magic;
101
102 uint16_t bmc_ver_min;
103 uint16_t bmc_ver_cur;
104 uint16_t host_ver_min;
105 uint16_t host_ver_cur;
106 uint16_t negotiated_ver;
107 uint16_t pad0;
108
109 uint32_t rx_offset;
110 uint32_t rx_size;
111 uint32_t tx_offset;
112 uint32_t tx_size;
113} __attribute__((packed));
114
115/* layout of TX/RX areas */
116static const uint32_t rx_offset = 0x100;
117static const uint32_t rx_size = 0x100;
118static const uint32_t tx_offset = 0x200;
119static const uint32_t tx_size = 0x100;
120
Jeremy Kerr672c8852019-03-01 12:18:07 +0800121#define LPC_WIN_SIZE (1 * 1024 * 1024)
122
Jeremy Kerr672c8852019-03-01 12:18:07 +0800123#define KCS_STATUS_BMC_READY 0x80
124#define KCS_STATUS_CHANNEL_ACTIVE 0x40
125#define KCS_STATUS_IBF 0x02
126#define KCS_STATUS_OBF 0x01
127
Andrew Jefferyf13cb972020-05-28 09:30:09 +0930128static inline int mctp_astlpc_kcs_write(struct mctp_binding_astlpc *astlpc,
129 enum mctp_binding_astlpc_kcs_reg reg,
130 uint8_t val)
131{
132 return astlpc->ops.kcs_write(astlpc->ops_data, reg, val);
133}
134
135static inline int mctp_astlpc_kcs_read(struct mctp_binding_astlpc *astlpc,
136 enum mctp_binding_astlpc_kcs_reg reg,
137 uint8_t *val)
138{
139 return astlpc->ops.kcs_read(astlpc->ops_data, reg, val);
140}
141
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930142static inline int mctp_astlpc_lpc_write(struct mctp_binding_astlpc *astlpc,
143 const void *buf, long offset,
144 size_t len)
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530145{
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930146 astlpc_prdebug(astlpc, "%s: %zu bytes to 0x%lx", __func__, len, offset);
147
148 assert(offset >= 0);
149
150 /* Indirect access */
151 if (astlpc->ops.lpc_write) {
152 void *data = astlpc->ops_data;
153
154 return astlpc->ops.lpc_write(data, buf, offset, len);
155 }
156
157 /* Direct mapping */
158 assert(astlpc->lpc_map);
159 memcpy(&((char *)astlpc->lpc_map)[offset], buf, len);
160
161 return 0;
162}
163
164static inline int mctp_astlpc_lpc_read(struct mctp_binding_astlpc *astlpc,
165 void *buf, long offset, size_t len)
166{
167 astlpc_prdebug(astlpc, "%s: %zu bytes from 0x%lx", __func__, len,
168 offset);
169
170 assert(offset >= 0);
171
172 /* Indirect access */
173 if (astlpc->ops.lpc_read) {
174 void *data = astlpc->ops_data;
175
176 return astlpc->ops.lpc_read(data, buf, offset, len);
177 }
178
179 /* Direct mapping */
180 assert(astlpc->lpc_map);
181 memcpy(buf, &((char *)astlpc->lpc_map)[offset], len);
182
183 return 0;
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530184}
185
Andrew Jefferyd0f5da02020-05-28 09:12:55 +0930186static int mctp_astlpc_kcs_set_status(struct mctp_binding_astlpc *astlpc,
187 uint8_t status)
188{
189 uint8_t data;
190 int rc;
191
192 /* Since we're setting the status register, we want the other endpoint
193 * to be interrupted. However, some hardware may only raise a host-side
194 * interrupt on an ODR event.
195 * So, write a dummy value of 0xff to ODR, which will ensure that an
196 * interrupt is triggered, and can be ignored by the host.
197 */
198 data = 0xff;
199 status |= KCS_STATUS_OBF;
200
Andrew Jefferyf13cb972020-05-28 09:30:09 +0930201 rc = mctp_astlpc_kcs_write(astlpc, MCTP_ASTLPC_KCS_REG_STATUS, status);
Andrew Jefferyd0f5da02020-05-28 09:12:55 +0930202 if (rc) {
203 astlpc_prwarn(astlpc, "KCS status write failed");
204 return -1;
205 }
206
Andrew Jefferyf13cb972020-05-28 09:30:09 +0930207 rc = mctp_astlpc_kcs_write(astlpc, MCTP_ASTLPC_KCS_REG_DATA, data);
Andrew Jefferyd0f5da02020-05-28 09:12:55 +0930208 if (rc) {
209 astlpc_prwarn(astlpc, "KCS dummy data write failed");
210 return -1;
211 }
212
213 return 0;
214}
215
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930216static int mctp_astlpc_init_bmc(struct mctp_binding_astlpc *astlpc)
217{
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930218 struct mctp_lpcmap_hdr hdr = { 0 };
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930219 uint8_t status;
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930220
221 /* Flip the buffers as the names are defined in terms of the host */
222 astlpc->layout.rx.offset = tx_offset;
223 astlpc->layout.rx.size = tx_size;
224 astlpc->layout.tx.offset = rx_offset;
225 astlpc->layout.tx.size = rx_size;
226
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930227 hdr = (struct mctp_lpcmap_hdr){
228 .magic = htobe32(ASTLPC_MCTP_MAGIC),
229 .bmc_ver_min = htobe16(ASTLPC_VER_MIN),
230 .bmc_ver_cur = htobe16(ASTLPC_VER_CUR),
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930231
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930232 /* Flip the buffers back as we're now describing the host's
233 * configuration to the host */
234 .rx_offset = htobe32(astlpc->layout.tx.offset),
235 .rx_size = htobe32(astlpc->layout.tx.size),
236 .tx_offset = htobe32(astlpc->layout.rx.offset),
237 .tx_size = htobe32(astlpc->layout.rx.size),
238 };
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930239
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930240 mctp_astlpc_lpc_write(astlpc, &hdr, 0, sizeof(hdr));
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930241
242 /* set status indicating that the BMC is now active */
243 status = KCS_STATUS_BMC_READY | KCS_STATUS_OBF;
Andrew Jefferyd0f5da02020-05-28 09:12:55 +0930244 return mctp_astlpc_kcs_set_status(astlpc, status);
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930245}
246
247static int mctp_binding_astlpc_start_bmc(struct mctp_binding *b)
248{
249 struct mctp_binding_astlpc *astlpc =
250 container_of(b, struct mctp_binding_astlpc, binding);
251
252 return mctp_astlpc_init_bmc(astlpc);
253}
254
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930255static bool mctp_astlpc_validate_version(uint16_t bmc_ver_min,
256 uint16_t bmc_ver_cur,
257 uint16_t host_ver_min,
258 uint16_t host_ver_cur)
259{
260 if (!(bmc_ver_min && bmc_ver_cur && host_ver_min && host_ver_cur)) {
261 mctp_prerr("Invalid version present in [%" PRIu16 ", %" PRIu16
262 "], [%" PRIu16 ", %" PRIu16 "]",
263 bmc_ver_min, bmc_ver_cur, host_ver_min,
264 host_ver_cur);
265 return false;
266 } else if (bmc_ver_min > bmc_ver_cur) {
267 mctp_prerr("Invalid bmc version range [%" PRIu16 ", %" PRIu16
268 "]",
269 bmc_ver_min, bmc_ver_cur);
270 return false;
271 } else if (host_ver_min > host_ver_cur) {
272 mctp_prerr("Invalid host version range [%" PRIu16 ", %" PRIu16
273 "]",
274 host_ver_min, host_ver_cur);
275 return false;
276 } else if ((host_ver_cur < bmc_ver_min) ||
277 (host_ver_min > bmc_ver_cur)) {
278 mctp_prerr(
279 "Unable to satisfy version negotiation with ranges [%" PRIu16
280 ", %" PRIu16 "] and [%" PRIu16 ", %" PRIu16 "]",
281 bmc_ver_min, bmc_ver_cur, host_ver_min, host_ver_cur);
282 return false;
283 }
284
285 return true;
286}
287
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930288static int mctp_astlpc_init_host(struct mctp_binding_astlpc *astlpc)
289{
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930290 const uint16_t ver_min_be = htobe16(ASTLPC_VER_MIN);
291 const uint16_t ver_cur_be = htobe16(ASTLPC_VER_CUR);
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930292 uint16_t bmc_ver_min, bmc_ver_cur;
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930293 struct mctp_lpcmap_hdr hdr;
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930294 uint8_t status;
295 int rc;
296
Andrew Jefferyf13cb972020-05-28 09:30:09 +0930297 rc = mctp_astlpc_kcs_read(astlpc, MCTP_ASTLPC_KCS_REG_STATUS, &status);
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930298 if (rc) {
299 mctp_prwarn("KCS status read failed");
300 return rc;
301 }
302
303 astlpc->kcs_status = status;
304
305 if (!(status & KCS_STATUS_BMC_READY))
306 return -EHOSTDOWN;
307
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930308 mctp_astlpc_lpc_read(astlpc, &hdr, 0, sizeof(hdr));
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930309
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930310 astlpc->layout.rx.offset = be32toh(hdr.rx_offset);
311 astlpc->layout.rx.size = be32toh(hdr.rx_size);
312 astlpc->layout.tx.offset = be32toh(hdr.tx_offset);
313 astlpc->layout.tx.size = be32toh(hdr.tx_size);
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930314
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930315 bmc_ver_min = be16toh(hdr.bmc_ver_min);
316 bmc_ver_cur = be16toh(hdr.bmc_ver_cur);
317
318 if (!mctp_astlpc_validate_version(bmc_ver_min, bmc_ver_cur,
319 ASTLPC_VER_MIN, ASTLPC_VER_CUR)) {
320 astlpc_prerr(astlpc, "Cannot negotiate with invalid versions");
321 return -EINVAL;
322 }
323
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930324 mctp_astlpc_lpc_write(astlpc, &ver_min_be,
325 offsetof(struct mctp_lpcmap_hdr, host_ver_min),
326 sizeof(ver_min_be));
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930327
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930328 mctp_astlpc_lpc_write(astlpc, &ver_cur_be,
329 offsetof(struct mctp_lpcmap_hdr, host_ver_cur),
330 sizeof(ver_cur_be));
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930331
332 /* Send channel init command */
Andrew Jefferyf13cb972020-05-28 09:30:09 +0930333 rc = mctp_astlpc_kcs_write(astlpc, MCTP_ASTLPC_KCS_REG_DATA, 0x0);
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930334 if (rc) {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930335 astlpc_prwarn(astlpc, "KCS write failed");
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930336 }
337
338 return rc;
339}
340
341static int mctp_binding_astlpc_start_host(struct mctp_binding *b)
342{
343 struct mctp_binding_astlpc *astlpc =
344 container_of(b, struct mctp_binding_astlpc, binding);
345
346 return mctp_astlpc_init_host(astlpc);
347}
348
349static bool __mctp_astlpc_kcs_ready(struct mctp_binding_astlpc *astlpc,
350 uint8_t status, bool is_write)
351{
352 bool is_bmc;
353 bool ready_state;
354 uint8_t flag;
355
356 is_bmc = (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_BMC);
357 flag = (is_bmc ^ is_write) ? KCS_STATUS_IBF : KCS_STATUS_OBF;
358 ready_state = is_write ? 0 : 1;
359
360 return !!(status & flag) == ready_state;
361}
362
363static inline bool
364mctp_astlpc_kcs_read_ready(struct mctp_binding_astlpc *astlpc, uint8_t status)
365{
366 return __mctp_astlpc_kcs_ready(astlpc, status, false);
367}
368
369static inline bool
370mctp_astlpc_kcs_write_ready(struct mctp_binding_astlpc *astlpc, uint8_t status)
371{
372 return __mctp_astlpc_kcs_ready(astlpc, status, true);
373}
374
Jeremy Kerr672c8852019-03-01 12:18:07 +0800375static int mctp_astlpc_kcs_send(struct mctp_binding_astlpc *astlpc,
376 uint8_t data)
377{
378 uint8_t status;
379 int rc;
380
381 for (;;) {
Andrew Jefferyf13cb972020-05-28 09:30:09 +0930382 rc = mctp_astlpc_kcs_read(astlpc, MCTP_ASTLPC_KCS_REG_STATUS,
383 &status);
Andrew Jeffery1b27fe82020-01-24 16:05:11 +1030384 if (rc) {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930385 astlpc_prwarn(astlpc, "KCS status read failed");
Jeremy Kerr672c8852019-03-01 12:18:07 +0800386 return -1;
387 }
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930388 if (mctp_astlpc_kcs_write_ready(astlpc, status))
Jeremy Kerr672c8852019-03-01 12:18:07 +0800389 break;
390 /* todo: timeout */
391 }
392
Andrew Jefferyf13cb972020-05-28 09:30:09 +0930393 rc = mctp_astlpc_kcs_write(astlpc, MCTP_ASTLPC_KCS_REG_DATA, data);
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530394 if (rc) {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930395 astlpc_prwarn(astlpc, "KCS data write failed");
Jeremy Kerr672c8852019-03-01 12:18:07 +0800396 return -1;
397 }
398
399 return 0;
400}
401
402static int mctp_binding_astlpc_tx(struct mctp_binding *b,
403 struct mctp_pktbuf *pkt)
404{
405 struct mctp_binding_astlpc *astlpc = binding_to_astlpc(b);
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930406 uint32_t len, len_be;
Andrew Jefferyedfe3832020-02-06 11:52:11 +1030407 struct mctp_hdr *hdr;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800408
Andrew Jefferyedfe3832020-02-06 11:52:11 +1030409 hdr = mctp_pktbuf_hdr(pkt);
Jeremy Kerr672c8852019-03-01 12:18:07 +0800410 len = mctp_pktbuf_size(pkt);
Andrew Jefferyedfe3832020-02-06 11:52:11 +1030411
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930412 astlpc_prdebug(astlpc,
413 "%s: Transmitting %" PRIu32
414 "-byte packet (%hhu, %hhu, 0x%hhx)",
415 __func__, len, hdr->src, hdr->dest, hdr->flags_seq_tag);
Andrew Jefferyedfe3832020-02-06 11:52:11 +1030416
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930417 if (len > ASTLPC_BODY_SIZE(astlpc->layout.tx.size)) {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930418 astlpc_prwarn(astlpc, "invalid TX len 0x%x", len);
Jeremy Kerr672c8852019-03-01 12:18:07 +0800419 return -1;
420 }
421
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930422 len_be = htobe32(len);
423 mctp_astlpc_lpc_write(astlpc, &len_be, astlpc->layout.tx.offset,
424 sizeof(len_be));
425 mctp_astlpc_lpc_write(astlpc, hdr, astlpc->layout.tx.offset + 4, len);
Jeremy Kerr672c8852019-03-01 12:18:07 +0800426
427 mctp_binding_set_tx_enabled(b, false);
428
429 mctp_astlpc_kcs_send(astlpc, 0x1);
430 return 0;
431}
432
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930433static uint16_t mctp_astlpc_negotiate_version(uint16_t bmc_ver_min,
434 uint16_t bmc_ver_cur,
435 uint16_t host_ver_min,
436 uint16_t host_ver_cur)
437{
438 if (!mctp_astlpc_validate_version(bmc_ver_min, bmc_ver_cur,
439 host_ver_min, host_ver_cur))
440 return ASTLPC_VER_BAD;
441
442 if (bmc_ver_cur < host_ver_cur)
443 return bmc_ver_cur;
444
445 return host_ver_cur;
446}
447
Jeremy Kerr672c8852019-03-01 12:18:07 +0800448static void mctp_astlpc_init_channel(struct mctp_binding_astlpc *astlpc)
449{
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930450 uint16_t negotiated, negotiated_be;
451 struct mctp_lpcmap_hdr hdr;
452 uint8_t status;
453
454 mctp_astlpc_lpc_read(astlpc, &hdr, 0, sizeof(hdr));
455
456 /* Version negotiation */
457 negotiated =
458 mctp_astlpc_negotiate_version(ASTLPC_VER_MIN, ASTLPC_VER_CUR,
459 be16toh(hdr.host_ver_min),
460 be16toh(hdr.host_ver_cur));
461
462 /* Populate the negotiated version */
463 astlpc->version = negotiated;
464 negotiated_be = htobe16(negotiated);
465 mctp_astlpc_lpc_write(astlpc, &negotiated_be,
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930466 offsetof(struct mctp_lpcmap_hdr, negotiated_ver),
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930467 sizeof(negotiated_be));
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930468
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930469 /* Finalise the configuration */
470 status = KCS_STATUS_BMC_READY | KCS_STATUS_OBF;
471 if (negotiated > 0) {
472 astlpc_prinfo(astlpc, "Negotiated binding version %" PRIu16,
473 negotiated);
474 status |= KCS_STATUS_CHANNEL_ACTIVE;
475 } else {
476 astlpc_prerr(astlpc, "Failed to initialise channel\n");
477 }
Jeremy Kerr672c8852019-03-01 12:18:07 +0800478
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930479 mctp_astlpc_kcs_set_status(astlpc, status);
480
481 mctp_binding_set_tx_enabled(&astlpc->binding,
482 status & KCS_STATUS_CHANNEL_ACTIVE);
Jeremy Kerr672c8852019-03-01 12:18:07 +0800483}
484
485static void mctp_astlpc_rx_start(struct mctp_binding_astlpc *astlpc)
486{
487 struct mctp_pktbuf *pkt;
488 uint32_t len;
489
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930490 mctp_astlpc_lpc_read(astlpc, &len, astlpc->layout.rx.offset,
491 sizeof(len));
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530492 len = be32toh(len);
493
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930494 if (len > ASTLPC_BODY_SIZE(astlpc->layout.rx.size)) {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930495 astlpc_prwarn(astlpc, "invalid RX len 0x%x", len);
Jeremy Kerr672c8852019-03-01 12:18:07 +0800496 return;
497 }
498
Andrew Jefferyb93b6112020-06-05 14:13:44 +0930499 assert(astlpc->binding.pkt_size >= 0);
500 if (len > (uint32_t)astlpc->binding.pkt_size) {
Jeremy Kerr672c8852019-03-01 12:18:07 +0800501 mctp_prwarn("invalid RX len 0x%x", len);
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930502 astlpc_prwarn(astlpc, "invalid RX len 0x%x", len);
Jeremy Kerr672c8852019-03-01 12:18:07 +0800503 return;
504 }
505
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +0800506 pkt = mctp_pktbuf_alloc(&astlpc->binding, len);
Jeremy Kerr672c8852019-03-01 12:18:07 +0800507 if (!pkt)
508 goto out_complete;
509
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930510 mctp_astlpc_lpc_read(astlpc, mctp_pktbuf_hdr(pkt),
511 astlpc->layout.rx.offset + 4, len);
Jeremy Kerr672c8852019-03-01 12:18:07 +0800512
513 mctp_bus_rx(&astlpc->binding, pkt);
514
515out_complete:
516 mctp_astlpc_kcs_send(astlpc, 0x2);
517}
518
519static void mctp_astlpc_tx_complete(struct mctp_binding_astlpc *astlpc)
520{
521 mctp_binding_set_tx_enabled(&astlpc->binding, true);
522}
523
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930524static int mctp_astlpc_finalise_channel(struct mctp_binding_astlpc *astlpc)
525{
526 uint16_t negotiated;
527 int rc;
528
529 rc = mctp_astlpc_lpc_read(astlpc, &negotiated,
530 offsetof(struct mctp_lpcmap_hdr,
531 negotiated_ver),
532 sizeof(negotiated));
533 if (rc < 0)
534 return rc;
535
536 negotiated = be16toh(negotiated);
537
538 if (negotiated == ASTLPC_VER_BAD || negotiated < ASTLPC_VER_MIN ||
539 negotiated > ASTLPC_VER_CUR) {
540 astlpc_prerr(astlpc, "Failed to negotiate version, got: %u\n",
541 negotiated);
542 return -EINVAL;
543 }
544
545 astlpc->version = negotiated;
546
547 return 0;
548}
549
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930550static int mctp_astlpc_update_channel(struct mctp_binding_astlpc *astlpc,
551 uint8_t status)
552{
553 uint8_t updated;
554 int rc = 0;
555
556 assert(astlpc->mode == MCTP_BINDING_ASTLPC_MODE_HOST);
557
558 updated = astlpc->kcs_status ^ status;
559
Andrew Jefferyd0f5da02020-05-28 09:12:55 +0930560 astlpc_prdebug(astlpc, "%s: status: 0x%x, update: 0x%x", __func__,
561 status, updated);
562
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930563 if (updated & KCS_STATUS_BMC_READY) {
Andrew Jefferyd0f5da02020-05-28 09:12:55 +0930564 if (status & KCS_STATUS_BMC_READY) {
565 astlpc->kcs_status = status;
566 return astlpc->binding.start(&astlpc->binding);
567 } else {
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930568 mctp_binding_set_tx_enabled(&astlpc->binding, false);
Andrew Jefferyd0f5da02020-05-28 09:12:55 +0930569 }
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930570 }
571
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930572 if (astlpc->version == 0 || updated & KCS_STATUS_CHANNEL_ACTIVE) {
573 bool enable;
574
575 rc = mctp_astlpc_finalise_channel(astlpc);
576 enable = (status & KCS_STATUS_CHANNEL_ACTIVE) && rc == 0;
577
578 mctp_binding_set_tx_enabled(&astlpc->binding, enable);
579 }
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930580
581 astlpc->kcs_status = status;
582
583 return rc;
584}
585
Jeremy Kerr672c8852019-03-01 12:18:07 +0800586int mctp_astlpc_poll(struct mctp_binding_astlpc *astlpc)
587{
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530588 uint8_t status, data;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800589 int rc;
590
Andrew Jefferyf13cb972020-05-28 09:30:09 +0930591 rc = mctp_astlpc_kcs_read(astlpc, MCTP_ASTLPC_KCS_REG_STATUS, &status);
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530592 if (rc) {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930593 astlpc_prwarn(astlpc, "KCS read error");
Jeremy Kerr672c8852019-03-01 12:18:07 +0800594 return -1;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800595 }
596
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930597 astlpc_prdebug(astlpc, "%s: status: 0x%hhx", __func__, status);
Andrew Jefferyedfe3832020-02-06 11:52:11 +1030598
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930599 if (!mctp_astlpc_kcs_read_ready(astlpc, status))
Jeremy Kerr672c8852019-03-01 12:18:07 +0800600 return 0;
601
Andrew Jefferyf13cb972020-05-28 09:30:09 +0930602 rc = mctp_astlpc_kcs_read(astlpc, MCTP_ASTLPC_KCS_REG_DATA, &data);
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530603 if (rc) {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930604 astlpc_prwarn(astlpc, "KCS data read error");
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530605 return -1;
606 }
607
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930608 astlpc_prdebug(astlpc, "%s: data: 0x%hhx", __func__, data);
Andrew Jefferyedfe3832020-02-06 11:52:11 +1030609
Jeremy Kerr672c8852019-03-01 12:18:07 +0800610 switch (data) {
611 case 0x0:
612 mctp_astlpc_init_channel(astlpc);
613 break;
614 case 0x1:
615 mctp_astlpc_rx_start(astlpc);
616 break;
617 case 0x2:
618 mctp_astlpc_tx_complete(astlpc);
619 break;
Jeremy Kerr1a4b55a2019-06-24 14:22:39 +0800620 case 0xff:
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930621 /* No responsibilities for the BMC on 0xff */
Andrew Jefferyd0f5da02020-05-28 09:12:55 +0930622 if (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_HOST) {
623 rc = mctp_astlpc_update_channel(astlpc, status);
624 if (rc < 0)
625 return rc;
626 }
627 break;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800628 default:
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930629 astlpc_prwarn(astlpc, "unknown message 0x%x", data);
Jeremy Kerr672c8852019-03-01 12:18:07 +0800630 }
Andrew Jefferyd0f5da02020-05-28 09:12:55 +0930631
632 /* Handle silent loss of bmc-ready */
633 if (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_HOST) {
634 if (!(status & KCS_STATUS_BMC_READY && data == 0xff))
635 return mctp_astlpc_update_channel(astlpc, status);
636 }
637
638 return rc;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800639}
640
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530641/* allocate and basic initialisation */
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930642static struct mctp_binding_astlpc *__mctp_astlpc_init(uint8_t mode,
643 uint32_t mtu)
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530644{
645 struct mctp_binding_astlpc *astlpc;
646
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930647 assert((mode == MCTP_BINDING_ASTLPC_MODE_BMC) ||
648 (mode == MCTP_BINDING_ASTLPC_MODE_HOST));
649
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530650 astlpc = __mctp_alloc(sizeof(*astlpc));
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930651 if (!astlpc)
652 return NULL;
653
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530654 memset(astlpc, 0, sizeof(*astlpc));
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930655 astlpc->mode = mode;
656 astlpc->lpc_map = NULL;
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530657 astlpc->binding.name = "astlpc";
658 astlpc->binding.version = 1;
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930659 astlpc->binding.pkt_size = MCTP_PACKET_SIZE(mtu);
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530660 astlpc->binding.pkt_pad = 0;
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930661 astlpc->binding.tx = mctp_binding_astlpc_tx;
662 if (mode == MCTP_BINDING_ASTLPC_MODE_BMC)
663 astlpc->binding.start = mctp_binding_astlpc_start_bmc;
664 else if (mode == MCTP_BINDING_ASTLPC_MODE_HOST)
665 astlpc->binding.start = mctp_binding_astlpc_start_host;
666 else {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930667 astlpc_prerr(astlpc, "%s: Invalid mode: %d\n", __func__, mode);
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930668 __mctp_free(astlpc);
669 return NULL;
670 }
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530671
672 return astlpc;
673}
674
Jeremy Kerr3b36d172019-09-04 11:56:09 +0800675struct mctp_binding *mctp_binding_astlpc_core(struct mctp_binding_astlpc *b)
676{
677 return &b->binding;
678}
679
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930680struct mctp_binding_astlpc *
681mctp_astlpc_init(uint8_t mode, uint32_t mtu, void *lpc_map,
682 const struct mctp_binding_astlpc_ops *ops, void *ops_data)
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530683{
684 struct mctp_binding_astlpc *astlpc;
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530685
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930686 if (!(mode == MCTP_BINDING_ASTLPC_MODE_BMC ||
687 mode == MCTP_BINDING_ASTLPC_MODE_HOST)) {
688 mctp_prerr("Unknown binding mode: %u", mode);
689 return NULL;
690 }
691
692 if (mtu != MCTP_BTU) {
693 mctp_prwarn("Unable to negotiate the MTU, using %u instead",
694 MCTP_BTU);
695 mtu = MCTP_BTU;
696 }
697
698 astlpc = __mctp_astlpc_init(mode, mtu);
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530699 if (!astlpc)
700 return NULL;
701
702 memcpy(&astlpc->ops, ops, sizeof(astlpc->ops));
703 astlpc->ops_data = ops_data;
704 astlpc->lpc_map = lpc_map;
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930705 astlpc->mode = mode;
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530706
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530707 return astlpc;
708}
709
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930710struct mctp_binding_astlpc *
711mctp_astlpc_init_ops(const struct mctp_binding_astlpc_ops *ops, void *ops_data,
712 void *lpc_map)
713{
714 return mctp_astlpc_init(MCTP_BINDING_ASTLPC_MODE_BMC, MCTP_BTU, lpc_map,
715 ops, ops_data);
716}
717
Andrew Jeffery4663f672020-03-10 23:36:24 +1030718void mctp_astlpc_destroy(struct mctp_binding_astlpc *astlpc)
719{
Andrew Jefferyd0f5da02020-05-28 09:12:55 +0930720 /* Clear channel-active and bmc-ready */
721 if (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_BMC)
722 mctp_astlpc_kcs_set_status(astlpc, KCS_STATUS_OBF);
Andrew Jeffery4663f672020-03-10 23:36:24 +1030723 __mctp_free(astlpc);
724}
725
Jeremy Kerrb214c642019-11-27 11:34:00 +0800726#ifdef MCTP_HAVE_FILEIO
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930727
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530728static int mctp_astlpc_init_fileio_lpc(struct mctp_binding_astlpc *astlpc)
Jeremy Kerr672c8852019-03-01 12:18:07 +0800729{
730 struct aspeed_lpc_ctrl_mapping map = {
731 .window_type = ASPEED_LPC_CTRL_WINDOW_MEMORY,
732 .window_id = 0, /* There's only one */
733 .flags = 0,
734 .addr = 0,
735 .offset = 0,
736 .size = 0
737 };
Andrew Jeffery979c6a12020-05-23 20:04:49 +0930738 void *lpc_map_base;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800739 int fd, rc;
740
741 fd = open(lpc_path, O_RDWR | O_SYNC);
742 if (fd < 0) {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930743 astlpc_prwarn(astlpc, "LPC open (%s) failed", lpc_path);
Jeremy Kerr672c8852019-03-01 12:18:07 +0800744 return -1;
745 }
746
747 rc = ioctl(fd, ASPEED_LPC_CTRL_IOCTL_GET_SIZE, &map);
748 if (rc) {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930749 astlpc_prwarn(astlpc, "LPC GET_SIZE failed");
Jeremy Kerr672c8852019-03-01 12:18:07 +0800750 close(fd);
751 return -1;
752 }
753
Andrew Jeffery979c6a12020-05-23 20:04:49 +0930754 lpc_map_base =
755 mmap(NULL, map.size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
756 if (lpc_map_base == MAP_FAILED) {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930757 astlpc_prwarn(astlpc, "LPC mmap failed");
Jeremy Kerr672c8852019-03-01 12:18:07 +0800758 rc = -1;
759 } else {
Andrew Jeffery979c6a12020-05-23 20:04:49 +0930760 astlpc->lpc_map = lpc_map_base + map.size - LPC_WIN_SIZE;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800761 }
762
763 close(fd);
764
765 return rc;
766}
767
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530768static int mctp_astlpc_init_fileio_kcs(struct mctp_binding_astlpc *astlpc)
Jeremy Kerr672c8852019-03-01 12:18:07 +0800769{
770 astlpc->kcs_fd = open(kcs_path, O_RDWR);
771 if (astlpc->kcs_fd < 0)
772 return -1;
773
774 return 0;
775}
776
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530777static int __mctp_astlpc_fileio_kcs_read(void *arg,
778 enum mctp_binding_astlpc_kcs_reg reg, uint8_t *val)
779{
780 struct mctp_binding_astlpc *astlpc = arg;
781 off_t offset = reg;
782 int rc;
783
784 rc = pread(astlpc->kcs_fd, val, 1, offset);
785
786 return rc == 1 ? 0 : -1;
787}
788
789static int __mctp_astlpc_fileio_kcs_write(void *arg,
790 enum mctp_binding_astlpc_kcs_reg reg, uint8_t val)
791{
792 struct mctp_binding_astlpc *astlpc = arg;
793 off_t offset = reg;
794 int rc;
795
796 rc = pwrite(astlpc->kcs_fd, &val, 1, offset);
797
798 return rc == 1 ? 0 : -1;
799}
800
801int mctp_astlpc_get_fd(struct mctp_binding_astlpc *astlpc)
802{
803 return astlpc->kcs_fd;
804}
805
806struct mctp_binding_astlpc *mctp_astlpc_init_fileio(void)
Jeremy Kerr672c8852019-03-01 12:18:07 +0800807{
808 struct mctp_binding_astlpc *astlpc;
809 int rc;
810
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930811 /*
812 * If we're doing file IO then we're very likely not running
813 * freestanding, so lets assume that we're on the BMC side
814 */
815 astlpc = __mctp_astlpc_init(MCTP_BINDING_ASTLPC_MODE_BMC, MCTP_BTU);
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530816 if (!astlpc)
817 return NULL;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800818
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530819 /* Set internal operations for kcs. We use direct accesses to the lpc
820 * map area */
821 astlpc->ops.kcs_read = __mctp_astlpc_fileio_kcs_read;
822 astlpc->ops.kcs_write = __mctp_astlpc_fileio_kcs_write;
823 astlpc->ops_data = astlpc;
824
825 rc = mctp_astlpc_init_fileio_lpc(astlpc);
Jeremy Kerr672c8852019-03-01 12:18:07 +0800826 if (rc) {
827 free(astlpc);
828 return NULL;
829 }
830
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530831 rc = mctp_astlpc_init_fileio_kcs(astlpc);
Jeremy Kerr672c8852019-03-01 12:18:07 +0800832 if (rc) {
833 free(astlpc);
834 return NULL;
835 }
836
Jeremy Kerr672c8852019-03-01 12:18:07 +0800837 return astlpc;
838}
Jeremy Kerr92a10a62019-08-28 16:55:54 +0530839#else
840struct mctp_binding_astlpc * __attribute__((const))
841 mctp_astlpc_init_fileio(void)
842{
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930843 astlpc_prerr(astlpc, "Missing support for file IO");
Jeremy Kerr92a10a62019-08-28 16:55:54 +0530844 return NULL;
845}
Jeremy Kerr672c8852019-03-01 12:18:07 +0800846
Jeremy Kerr92a10a62019-08-28 16:55:54 +0530847int __attribute__((const)) mctp_astlpc_get_fd(
848 struct mctp_binding_astlpc *astlpc __attribute__((unused)))
849{
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930850 astlpc_prerr(astlpc, "Missing support for file IO");
Jeremy Kerr92a10a62019-08-28 16:55:54 +0530851 return -1;
852}
853#endif