blob: 64dd9de49a8c731d5255a008f5f4541c7d9c9780 [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"
Andrew Jeffery4622cad2020-11-03 22:20:18 +103026#include "range.h"
Jeremy Kerr672c8852019-03-01 12:18:07 +080027
Jeremy Kerrb214c642019-11-27 11:34:00 +080028#ifdef MCTP_HAVE_FILEIO
Jeremy Kerr92a10a62019-08-28 16:55:54 +053029
Jeremy Kerrc6f676d2019-12-19 09:24:06 +080030#include <unistd.h>
Jeremy Kerr92a10a62019-08-28 16:55:54 +053031#include <fcntl.h>
32#include <sys/ioctl.h>
33#include <sys/mman.h>
34#include <linux/aspeed-lpc-ctrl.h>
35
36/* kernel interface */
37static const char *kcs_path = "/dev/mctp0";
38static const char *lpc_path = "/dev/aspeed-lpc-ctrl";
39
40#endif
41
Andrew Jeffery7cd72f12020-05-12 20:27:59 +093042struct mctp_astlpc_buffer {
43 uint32_t offset;
44 uint32_t size;
45};
46
47struct mctp_astlpc_layout {
48 struct mctp_astlpc_buffer rx;
49 struct mctp_astlpc_buffer tx;
50};
51
Jeremy Kerr672c8852019-03-01 12:18:07 +080052struct mctp_binding_astlpc {
53 struct mctp_binding binding;
Jeremy Kerrbc53d352019-08-28 14:26:14 +053054
Andrew Jeffery55fb90b2020-05-12 13:54:37 +093055 void *lpc_map;
Andrew Jeffery7cd72f12020-05-12 20:27:59 +093056 struct mctp_astlpc_layout layout;
57
58 uint8_t mode;
Andrew Jeffery4e8264b2020-05-23 20:34:33 +093059 uint16_t version;
Andrew Jefferya9368982020-06-09 13:07:39 +093060 uint32_t requested_mtu;
Jeremy Kerrbc53d352019-08-28 14:26:14 +053061
62 /* direct ops data */
Andrew Jeffery55fb90b2020-05-12 13:54:37 +093063 struct mctp_binding_astlpc_ops ops;
64 void *ops_data;
Jeremy Kerrbc53d352019-08-28 14:26:14 +053065
66 /* fileio ops data */
Andrew Jeffery979c6a12020-05-23 20:04:49 +093067 int kcs_fd;
68 uint8_t kcs_status;
Jeremy Kerr672c8852019-03-01 12:18:07 +080069
70 bool running;
Jeremy Kerr672c8852019-03-01 12:18:07 +080071};
72
Jeremy Kerr672c8852019-03-01 12:18:07 +080073#define binding_to_astlpc(b) \
74 container_of(b, struct mctp_binding_astlpc, binding)
75
Andrew Jeffery9101a2a2020-05-22 16:08:03 +093076#define astlpc_prlog(ctx, lvl, fmt, ...) \
77 do { \
78 bool __bmc = ((ctx)->mode == MCTP_BINDING_ASTLPC_MODE_BMC); \
79 mctp_prlog(lvl, pr_fmt("%s: " fmt), __bmc ? "bmc" : "host", \
80 ##__VA_ARGS__); \
81 } while (0)
82
83#define astlpc_prerr(ctx, fmt, ...) \
84 astlpc_prlog(ctx, MCTP_LOG_ERR, fmt, ##__VA_ARGS__)
85#define astlpc_prwarn(ctx, fmt, ...) \
86 astlpc_prlog(ctx, MCTP_LOG_WARNING, fmt, ##__VA_ARGS__)
87#define astlpc_prinfo(ctx, fmt, ...) \
88 astlpc_prlog(ctx, MCTP_LOG_INFO, fmt, ##__VA_ARGS__)
89#define astlpc_prdebug(ctx, fmt, ...) \
90 astlpc_prlog(ctx, MCTP_LOG_DEBUG, fmt, ##__VA_ARGS__)
91
Andrew Jeffery7cd72f12020-05-12 20:27:59 +093092/* clang-format off */
93#define ASTLPC_MCTP_MAGIC 0x4d435450
Andrew Jeffery4e8264b2020-05-23 20:34:33 +093094#define ASTLPC_VER_BAD 0
Andrew Jeffery7cd72f12020-05-12 20:27:59 +093095#define ASTLPC_VER_MIN 1
Andrew Jeffery7cd72f12020-05-12 20:27:59 +093096
Andrew Jeffery3a540662020-05-26 19:55:30 +093097/* Support testing of new binding protocols */
98#ifndef ASTLPC_VER_CUR
Andrew Jeffery5303d9c2020-06-09 13:37:46 +093099#define ASTLPC_VER_CUR 2
Andrew Jeffery3a540662020-05-26 19:55:30 +0930100#endif
101
102#define ASTLPC_PACKET_SIZE(sz) (4 + (sz))
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930103#define ASTLPC_BODY_SIZE(sz) ((sz) - 4)
104/* clang-format on */
Jeremy Kerr672c8852019-03-01 12:18:07 +0800105
106struct mctp_lpcmap_hdr {
Andrew Jeffery3a540662020-05-26 19:55:30 +0930107 uint32_t magic;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800108
Andrew Jeffery3a540662020-05-26 19:55:30 +0930109 uint16_t bmc_ver_min;
110 uint16_t bmc_ver_cur;
111 uint16_t host_ver_min;
112 uint16_t host_ver_cur;
113 uint16_t negotiated_ver;
114 uint16_t pad0;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800115
Andrew Jeffery3a540662020-05-26 19:55:30 +0930116 struct {
117 uint32_t rx_offset;
118 uint32_t rx_size;
119 uint32_t tx_offset;
120 uint32_t tx_size;
121 } layout;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800122} __attribute__((packed));
123
Andrew Jeffery3a540662020-05-26 19:55:30 +0930124static const uint32_t control_size = 0x100;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800125
Jeremy Kerr672c8852019-03-01 12:18:07 +0800126#define LPC_WIN_SIZE (1 * 1024 * 1024)
127
Jeremy Kerr672c8852019-03-01 12:18:07 +0800128#define KCS_STATUS_BMC_READY 0x80
129#define KCS_STATUS_CHANNEL_ACTIVE 0x40
130#define KCS_STATUS_IBF 0x02
131#define KCS_STATUS_OBF 0x01
132
Andrew Jefferyf13cb972020-05-28 09:30:09 +0930133static inline int mctp_astlpc_kcs_write(struct mctp_binding_astlpc *astlpc,
134 enum mctp_binding_astlpc_kcs_reg reg,
135 uint8_t val)
136{
137 return astlpc->ops.kcs_write(astlpc->ops_data, reg, val);
138}
139
140static inline int mctp_astlpc_kcs_read(struct mctp_binding_astlpc *astlpc,
141 enum mctp_binding_astlpc_kcs_reg reg,
142 uint8_t *val)
143{
144 return astlpc->ops.kcs_read(astlpc->ops_data, reg, val);
145}
146
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930147static inline int mctp_astlpc_lpc_write(struct mctp_binding_astlpc *astlpc,
148 const void *buf, long offset,
149 size_t len)
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530150{
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930151 astlpc_prdebug(astlpc, "%s: %zu bytes to 0x%lx", __func__, len, offset);
152
153 assert(offset >= 0);
154
155 /* Indirect access */
156 if (astlpc->ops.lpc_write) {
157 void *data = astlpc->ops_data;
158
159 return astlpc->ops.lpc_write(data, buf, offset, len);
160 }
161
162 /* Direct mapping */
163 assert(astlpc->lpc_map);
164 memcpy(&((char *)astlpc->lpc_map)[offset], buf, len);
165
166 return 0;
167}
168
169static inline int mctp_astlpc_lpc_read(struct mctp_binding_astlpc *astlpc,
170 void *buf, long offset, size_t len)
171{
172 astlpc_prdebug(astlpc, "%s: %zu bytes from 0x%lx", __func__, len,
173 offset);
174
175 assert(offset >= 0);
176
177 /* Indirect access */
178 if (astlpc->ops.lpc_read) {
179 void *data = astlpc->ops_data;
180
181 return astlpc->ops.lpc_read(data, buf, offset, len);
182 }
183
184 /* Direct mapping */
185 assert(astlpc->lpc_map);
186 memcpy(buf, &((char *)astlpc->lpc_map)[offset], len);
187
188 return 0;
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530189}
190
Andrew Jefferyd0f5da02020-05-28 09:12:55 +0930191static int mctp_astlpc_kcs_set_status(struct mctp_binding_astlpc *astlpc,
192 uint8_t status)
193{
194 uint8_t data;
195 int rc;
196
197 /* Since we're setting the status register, we want the other endpoint
198 * to be interrupted. However, some hardware may only raise a host-side
199 * interrupt on an ODR event.
200 * So, write a dummy value of 0xff to ODR, which will ensure that an
201 * interrupt is triggered, and can be ignored by the host.
202 */
203 data = 0xff;
Andrew Jefferyd0f5da02020-05-28 09:12:55 +0930204
Andrew Jefferyf13cb972020-05-28 09:30:09 +0930205 rc = mctp_astlpc_kcs_write(astlpc, MCTP_ASTLPC_KCS_REG_STATUS, status);
Andrew Jefferyd0f5da02020-05-28 09:12:55 +0930206 if (rc) {
207 astlpc_prwarn(astlpc, "KCS status write failed");
208 return -1;
209 }
210
Andrew Jefferyf13cb972020-05-28 09:30:09 +0930211 rc = mctp_astlpc_kcs_write(astlpc, MCTP_ASTLPC_KCS_REG_DATA, data);
Andrew Jefferyd0f5da02020-05-28 09:12:55 +0930212 if (rc) {
213 astlpc_prwarn(astlpc, "KCS dummy data write failed");
214 return -1;
215 }
216
217 return 0;
218}
219
Andrew Jeffery3a540662020-05-26 19:55:30 +0930220static int mctp_astlpc_layout_read(struct mctp_binding_astlpc *astlpc,
221 struct mctp_astlpc_layout *layout)
222{
223 struct mctp_lpcmap_hdr hdr;
224 int rc;
225
226 rc = mctp_astlpc_lpc_read(astlpc, &hdr, 0, sizeof(hdr));
227 if (rc < 0)
228 return rc;
229
230 /* Flip the buffers as the names are defined in terms of the host */
231 if (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_BMC) {
232 layout->rx.offset = be32toh(hdr.layout.tx_offset);
233 layout->rx.size = be32toh(hdr.layout.tx_size);
234 layout->tx.offset = be32toh(hdr.layout.rx_offset);
235 layout->tx.size = be32toh(hdr.layout.rx_size);
236 } else {
237 assert(astlpc->mode == MCTP_BINDING_ASTLPC_MODE_HOST);
238
239 layout->rx.offset = be32toh(hdr.layout.rx_offset);
240 layout->rx.size = be32toh(hdr.layout.rx_size);
241 layout->tx.offset = be32toh(hdr.layout.tx_offset);
242 layout->tx.size = be32toh(hdr.layout.tx_size);
243 }
244
245 return 0;
246}
247
248static int mctp_astlpc_layout_write(struct mctp_binding_astlpc *astlpc,
249 struct mctp_astlpc_layout *layout)
250{
251 uint32_t rx_size_be;
252
253 if (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_BMC) {
254 struct mctp_lpcmap_hdr hdr;
255
256 /*
257 * Flip the buffers as the names are defined in terms of the
258 * host
259 */
260 hdr.layout.rx_offset = htobe32(layout->tx.offset);
261 hdr.layout.rx_size = htobe32(layout->tx.size);
262 hdr.layout.tx_offset = htobe32(layout->rx.offset);
263 hdr.layout.tx_size = htobe32(layout->rx.size);
264
265 return mctp_astlpc_lpc_write(astlpc, &hdr.layout,
266 offsetof(struct mctp_lpcmap_hdr, layout),
267 sizeof(hdr.layout));
268 }
269
270 assert(astlpc->mode == MCTP_BINDING_ASTLPC_MODE_HOST);
271
272 /*
273 * As of v2 we only need to write rx_size - the offsets are controlled
274 * by the BMC, as is the BMC's rx_size (host tx_size).
275 */
276 rx_size_be = htobe32(layout->rx.size);
277 return mctp_astlpc_lpc_write(astlpc, &rx_size_be,
278 offsetof(struct mctp_lpcmap_hdr, layout.rx_size),
279 sizeof(rx_size_be));
280}
281
282static bool mctp_astlpc_buffer_validate(struct mctp_astlpc_buffer *buf,
283 const char *name)
284{
285 /* Check for overflow */
286 if (buf->offset + buf->size < buf->offset) {
287 mctp_prerr(
288 "%s packet buffer parameters overflow: offset: 0x%" PRIx32
289 ", size: %" PRIu32,
290 name, buf->offset, buf->size);
291 return false;
292 }
293
294 /* Check that the buffers are contained within the allocated space */
295 if (buf->offset + buf->size > LPC_WIN_SIZE) {
296 mctp_prerr(
297 "%s packet buffer parameters exceed %uM window size: offset: 0x%" PRIx32
298 ", size: %" PRIu32,
299 name, (LPC_WIN_SIZE / (1024 * 1024)), buf->offset,
300 buf->size);
301 return false;
302 }
303
304 /* Check that the baseline transmission unit is supported */
305 if (buf->size < ASTLPC_PACKET_SIZE(MCTP_PACKET_SIZE(MCTP_BTU))) {
306 mctp_prerr(
307 "%s packet buffer too small: Require %zu bytes to support the %u byte baseline transmission unit, found %" PRIu32,
308 name, ASTLPC_PACKET_SIZE(MCTP_PACKET_SIZE(MCTP_BTU)),
309 MCTP_BTU, buf->size);
310 return false;
311 }
312
313 /* Check for overlap with the control space */
314 if (buf->offset < control_size) {
315 mctp_prerr(
316 "%s packet buffer overlaps control region {0x%" PRIx32
317 ", %" PRIu32 "}: Rx {0x%" PRIx32 ", %" PRIu32 "}",
318 name, 0U, control_size, buf->offset, buf->size);
319 return false;
320 }
321
322 return true;
323}
324
325static bool mctp_astlpc_layout_validate(struct mctp_astlpc_layout *layout)
326{
327 struct mctp_astlpc_buffer *rx = &layout->rx;
328 struct mctp_astlpc_buffer *tx = &layout->tx;
329 bool rx_valid, tx_valid;
330
331 rx_valid = mctp_astlpc_buffer_validate(rx, "Rx");
332 tx_valid = mctp_astlpc_buffer_validate(tx, "Tx");
333
334 if (!(rx_valid && tx_valid))
335 return false;
336
337 /* Check that the buffers are disjoint */
338 if ((rx->offset <= tx->offset && rx->offset + rx->size > tx->offset) ||
339 (tx->offset <= rx->offset && tx->offset + tx->size > rx->offset)) {
340 mctp_prerr("Rx and Tx packet buffers overlap: Rx {0x%" PRIx32
341 ", %" PRIu32 "}, Tx {0x%" PRIx32 ", %" PRIu32 "}",
342 rx->offset, rx->size, tx->offset, tx->size);
343 return false;
344 }
345
346 return true;
347}
348
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930349static int mctp_astlpc_init_bmc(struct mctp_binding_astlpc *astlpc)
350{
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930351 struct mctp_lpcmap_hdr hdr = { 0 };
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930352 uint8_t status;
Andrew Jeffery3a540662020-05-26 19:55:30 +0930353 size_t sz;
354
355 /*
356 * The largest buffer size is half of the allocated MCTP space
357 * excluding the control space.
358 */
359 sz = ((LPC_WIN_SIZE - control_size) / 2);
360
361 /*
362 * Trim the MTU to a multiple of 16 to meet the requirements of 12.17
363 * Query Hop in DSP0236 v1.3.0.
364 */
365 sz = MCTP_BODY_SIZE(ASTLPC_BODY_SIZE(sz));
366 sz &= ~0xfUL;
367 sz = ASTLPC_PACKET_SIZE(MCTP_PACKET_SIZE(sz));
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930368
Andrew Jefferya9368982020-06-09 13:07:39 +0930369 if (astlpc->requested_mtu) {
370 size_t r;
371
372 r = ASTLPC_PACKET_SIZE(MCTP_PACKET_SIZE(astlpc->requested_mtu));
373 sz = MIN(sz, r);
374 }
375
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930376 /* Flip the buffers as the names are defined in terms of the host */
Andrew Jeffery3a540662020-05-26 19:55:30 +0930377 astlpc->layout.tx.offset = control_size;
378 astlpc->layout.tx.size = sz;
379 astlpc->layout.rx.offset =
380 astlpc->layout.tx.offset + astlpc->layout.tx.size;
381 astlpc->layout.rx.size = sz;
382
Andrew Jefferya9368982020-06-09 13:07:39 +0930383 if (!mctp_astlpc_layout_validate(&astlpc->layout)) {
384 astlpc_prerr(astlpc, "Cannot support an MTU of %zu", sz);
385 return -EINVAL;
386 }
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930387
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930388 hdr = (struct mctp_lpcmap_hdr){
389 .magic = htobe32(ASTLPC_MCTP_MAGIC),
390 .bmc_ver_min = htobe16(ASTLPC_VER_MIN),
391 .bmc_ver_cur = htobe16(ASTLPC_VER_CUR),
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930392
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930393 /* Flip the buffers back as we're now describing the host's
394 * configuration to the host */
Andrew Jeffery3a540662020-05-26 19:55:30 +0930395 .layout.rx_offset = htobe32(astlpc->layout.tx.offset),
396 .layout.rx_size = htobe32(astlpc->layout.tx.size),
397 .layout.tx_offset = htobe32(astlpc->layout.rx.offset),
398 .layout.tx_size = htobe32(astlpc->layout.rx.size),
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930399 };
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930400
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930401 mctp_astlpc_lpc_write(astlpc, &hdr, 0, sizeof(hdr));
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930402
Andrew Jefferyb3b55a62020-07-06 13:34:18 +0930403 /*
404 * Set status indicating that the BMC is now active. Be explicit about
405 * clearing OBF; we're reinitialising the binding and so any previous
406 * buffer state is irrelevant.
407 */
408 status = KCS_STATUS_BMC_READY & ~KCS_STATUS_OBF;
Andrew Jefferyd0f5da02020-05-28 09:12:55 +0930409 return mctp_astlpc_kcs_set_status(astlpc, status);
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930410}
411
412static int mctp_binding_astlpc_start_bmc(struct mctp_binding *b)
413{
414 struct mctp_binding_astlpc *astlpc =
415 container_of(b, struct mctp_binding_astlpc, binding);
416
417 return mctp_astlpc_init_bmc(astlpc);
418}
419
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930420static bool mctp_astlpc_validate_version(uint16_t bmc_ver_min,
421 uint16_t bmc_ver_cur,
422 uint16_t host_ver_min,
423 uint16_t host_ver_cur)
424{
425 if (!(bmc_ver_min && bmc_ver_cur && host_ver_min && host_ver_cur)) {
426 mctp_prerr("Invalid version present in [%" PRIu16 ", %" PRIu16
427 "], [%" PRIu16 ", %" PRIu16 "]",
428 bmc_ver_min, bmc_ver_cur, host_ver_min,
429 host_ver_cur);
430 return false;
431 } else if (bmc_ver_min > bmc_ver_cur) {
432 mctp_prerr("Invalid bmc version range [%" PRIu16 ", %" PRIu16
433 "]",
434 bmc_ver_min, bmc_ver_cur);
435 return false;
436 } else if (host_ver_min > host_ver_cur) {
437 mctp_prerr("Invalid host version range [%" PRIu16 ", %" PRIu16
438 "]",
439 host_ver_min, host_ver_cur);
440 return false;
441 } else if ((host_ver_cur < bmc_ver_min) ||
442 (host_ver_min > bmc_ver_cur)) {
443 mctp_prerr(
444 "Unable to satisfy version negotiation with ranges [%" PRIu16
445 ", %" PRIu16 "] and [%" PRIu16 ", %" PRIu16 "]",
446 bmc_ver_min, bmc_ver_cur, host_ver_min, host_ver_cur);
447 return false;
448 }
449
450 return true;
451}
452
Andrew Jeffery3a540662020-05-26 19:55:30 +0930453static int mctp_astlpc_negotiate_layout_host(struct mctp_binding_astlpc *astlpc)
454{
455 struct mctp_astlpc_layout layout;
456 uint32_t sz;
457 int rc;
458
459 rc = mctp_astlpc_layout_read(astlpc, &layout);
460 if (rc < 0)
461 return rc;
462
463 if (!mctp_astlpc_layout_validate(&layout)) {
464 astlpc_prerr(
465 astlpc,
466 "BMC provided invalid buffer layout: Rx {0x%" PRIx32
467 ", %" PRIu32 "}, Tx {0x%" PRIx32 ", %" PRIu32 "}",
468 layout.rx.offset, layout.rx.size, layout.tx.offset,
469 layout.tx.size);
470 return -EINVAL;
471 }
472
Andrew Jefferya9368982020-06-09 13:07:39 +0930473 astlpc_prinfo(astlpc, "Desire an MTU of %" PRIu32 " bytes",
474 astlpc->requested_mtu);
475
476 sz = ASTLPC_PACKET_SIZE(MCTP_PACKET_SIZE(astlpc->requested_mtu));
Andrew Jeffery3a540662020-05-26 19:55:30 +0930477 layout.rx.size = sz;
478
479 if (!mctp_astlpc_layout_validate(&layout)) {
480 astlpc_prerr(
481 astlpc,
482 "Generated invalid buffer layout with size %" PRIu32
483 ": Rx {0x%" PRIx32 ", %" PRIu32 "}, Tx {0x%" PRIx32
484 ", %" PRIu32 "}",
485 sz, layout.rx.offset, layout.rx.size, layout.tx.offset,
486 layout.tx.size);
487 return -EINVAL;
488 }
489
Andrew Jefferya9368982020-06-09 13:07:39 +0930490 astlpc_prinfo(astlpc, "Requesting MTU of %" PRIu32 " bytes",
491 astlpc->requested_mtu);
Andrew Jeffery3a540662020-05-26 19:55:30 +0930492
493 return mctp_astlpc_layout_write(astlpc, &layout);
494}
495
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930496static int mctp_astlpc_init_host(struct mctp_binding_astlpc *astlpc)
497{
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930498 const uint16_t ver_min_be = htobe16(ASTLPC_VER_MIN);
499 const uint16_t ver_cur_be = htobe16(ASTLPC_VER_CUR);
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930500 uint16_t bmc_ver_min, bmc_ver_cur;
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930501 struct mctp_lpcmap_hdr hdr;
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930502 uint8_t status;
503 int rc;
504
Andrew Jefferyf13cb972020-05-28 09:30:09 +0930505 rc = mctp_astlpc_kcs_read(astlpc, MCTP_ASTLPC_KCS_REG_STATUS, &status);
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930506 if (rc) {
507 mctp_prwarn("KCS status read failed");
508 return rc;
509 }
510
511 astlpc->kcs_status = status;
512
513 if (!(status & KCS_STATUS_BMC_READY))
514 return -EHOSTDOWN;
515
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930516 mctp_astlpc_lpc_read(astlpc, &hdr, 0, sizeof(hdr));
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930517
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930518 bmc_ver_min = be16toh(hdr.bmc_ver_min);
519 bmc_ver_cur = be16toh(hdr.bmc_ver_cur);
520
521 if (!mctp_astlpc_validate_version(bmc_ver_min, bmc_ver_cur,
522 ASTLPC_VER_MIN, ASTLPC_VER_CUR)) {
523 astlpc_prerr(astlpc, "Cannot negotiate with invalid versions");
524 return -EINVAL;
525 }
526
Andrew Jeffery3a540662020-05-26 19:55:30 +0930527 /*
528 * Negotation always chooses the highest protocol version that
529 * satisfies the version constraints. So check whether the BMC supports
530 * v2, and if so, negotiate in v2 style.
531 */
532 if (ASTLPC_VER_CUR >= 2 && bmc_ver_cur >= 2) {
533 rc = mctp_astlpc_negotiate_layout_host(astlpc);
534 if (rc < 0)
535 return rc;
536 }
537
538 /* Version negotiation */
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930539 mctp_astlpc_lpc_write(astlpc, &ver_min_be,
540 offsetof(struct mctp_lpcmap_hdr, host_ver_min),
541 sizeof(ver_min_be));
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930542
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930543 mctp_astlpc_lpc_write(astlpc, &ver_cur_be,
544 offsetof(struct mctp_lpcmap_hdr, host_ver_cur),
545 sizeof(ver_cur_be));
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930546
547 /* Send channel init command */
Andrew Jefferyf13cb972020-05-28 09:30:09 +0930548 rc = mctp_astlpc_kcs_write(astlpc, MCTP_ASTLPC_KCS_REG_DATA, 0x0);
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930549 if (rc) {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930550 astlpc_prwarn(astlpc, "KCS write failed");
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930551 }
552
553 return rc;
554}
555
556static int mctp_binding_astlpc_start_host(struct mctp_binding *b)
557{
558 struct mctp_binding_astlpc *astlpc =
559 container_of(b, struct mctp_binding_astlpc, binding);
560
561 return mctp_astlpc_init_host(astlpc);
562}
563
564static bool __mctp_astlpc_kcs_ready(struct mctp_binding_astlpc *astlpc,
565 uint8_t status, bool is_write)
566{
567 bool is_bmc;
568 bool ready_state;
569 uint8_t flag;
570
571 is_bmc = (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_BMC);
572 flag = (is_bmc ^ is_write) ? KCS_STATUS_IBF : KCS_STATUS_OBF;
573 ready_state = is_write ? 0 : 1;
574
575 return !!(status & flag) == ready_state;
576}
577
578static inline bool
579mctp_astlpc_kcs_read_ready(struct mctp_binding_astlpc *astlpc, uint8_t status)
580{
581 return __mctp_astlpc_kcs_ready(astlpc, status, false);
582}
583
584static inline bool
585mctp_astlpc_kcs_write_ready(struct mctp_binding_astlpc *astlpc, uint8_t status)
586{
587 return __mctp_astlpc_kcs_ready(astlpc, status, true);
588}
589
Jeremy Kerr672c8852019-03-01 12:18:07 +0800590static int mctp_astlpc_kcs_send(struct mctp_binding_astlpc *astlpc,
591 uint8_t data)
592{
593 uint8_t status;
594 int rc;
595
596 for (;;) {
Andrew Jefferyf13cb972020-05-28 09:30:09 +0930597 rc = mctp_astlpc_kcs_read(astlpc, MCTP_ASTLPC_KCS_REG_STATUS,
598 &status);
Andrew Jeffery1b27fe82020-01-24 16:05:11 +1030599 if (rc) {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930600 astlpc_prwarn(astlpc, "KCS status read failed");
Jeremy Kerr672c8852019-03-01 12:18:07 +0800601 return -1;
602 }
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930603 if (mctp_astlpc_kcs_write_ready(astlpc, status))
Jeremy Kerr672c8852019-03-01 12:18:07 +0800604 break;
605 /* todo: timeout */
606 }
607
Andrew Jefferyf13cb972020-05-28 09:30:09 +0930608 rc = mctp_astlpc_kcs_write(astlpc, MCTP_ASTLPC_KCS_REG_DATA, data);
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530609 if (rc) {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930610 astlpc_prwarn(astlpc, "KCS data write failed");
Jeremy Kerr672c8852019-03-01 12:18:07 +0800611 return -1;
612 }
613
614 return 0;
615}
616
617static int mctp_binding_astlpc_tx(struct mctp_binding *b,
618 struct mctp_pktbuf *pkt)
619{
620 struct mctp_binding_astlpc *astlpc = binding_to_astlpc(b);
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930621 uint32_t len, len_be;
Andrew Jefferyedfe3832020-02-06 11:52:11 +1030622 struct mctp_hdr *hdr;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800623
Andrew Jefferyedfe3832020-02-06 11:52:11 +1030624 hdr = mctp_pktbuf_hdr(pkt);
Jeremy Kerr672c8852019-03-01 12:18:07 +0800625 len = mctp_pktbuf_size(pkt);
Andrew Jefferyedfe3832020-02-06 11:52:11 +1030626
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930627 astlpc_prdebug(astlpc,
628 "%s: Transmitting %" PRIu32
629 "-byte packet (%hhu, %hhu, 0x%hhx)",
630 __func__, len, hdr->src, hdr->dest, hdr->flags_seq_tag);
Andrew Jefferyedfe3832020-02-06 11:52:11 +1030631
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930632 if (len > ASTLPC_BODY_SIZE(astlpc->layout.tx.size)) {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930633 astlpc_prwarn(astlpc, "invalid TX len 0x%x", len);
Jeremy Kerr672c8852019-03-01 12:18:07 +0800634 return -1;
635 }
636
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930637 len_be = htobe32(len);
638 mctp_astlpc_lpc_write(astlpc, &len_be, astlpc->layout.tx.offset,
639 sizeof(len_be));
640 mctp_astlpc_lpc_write(astlpc, hdr, astlpc->layout.tx.offset + 4, len);
Jeremy Kerr672c8852019-03-01 12:18:07 +0800641
642 mctp_binding_set_tx_enabled(b, false);
643
644 mctp_astlpc_kcs_send(astlpc, 0x1);
645 return 0;
646}
647
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930648static uint16_t mctp_astlpc_negotiate_version(uint16_t bmc_ver_min,
649 uint16_t bmc_ver_cur,
650 uint16_t host_ver_min,
651 uint16_t host_ver_cur)
652{
653 if (!mctp_astlpc_validate_version(bmc_ver_min, bmc_ver_cur,
654 host_ver_min, host_ver_cur))
655 return ASTLPC_VER_BAD;
656
657 if (bmc_ver_cur < host_ver_cur)
658 return bmc_ver_cur;
659
660 return host_ver_cur;
661}
662
Andrew Jeffery3a540662020-05-26 19:55:30 +0930663static uint32_t mctp_astlpc_calculate_mtu(struct mctp_binding_astlpc *astlpc,
664 struct mctp_astlpc_layout *layout)
665{
666 uint32_t low, high, limit;
667
668 /* Derive the largest MTU the BMC _can_ support */
669 low = MIN(astlpc->layout.rx.offset, astlpc->layout.tx.offset);
670 high = MAX(astlpc->layout.rx.offset, astlpc->layout.tx.offset);
671 limit = high - low;
672
Andrew Jefferya9368982020-06-09 13:07:39 +0930673 /* Determine the largest MTU the BMC _wants_ to support */
674 if (astlpc->requested_mtu) {
675 uint32_t req = astlpc->requested_mtu;
676
677 limit = MIN(limit, ASTLPC_PACKET_SIZE(MCTP_PACKET_SIZE(req)));
678 }
Andrew Jeffery3a540662020-05-26 19:55:30 +0930679
680 /* Determine the accepted MTU, applied both directions by convention */
681 return MCTP_BODY_SIZE(ASTLPC_BODY_SIZE(MIN(limit, layout->tx.size)));
682}
683
684static int mctp_astlpc_negotiate_layout_bmc(struct mctp_binding_astlpc *astlpc)
685{
686 struct mctp_astlpc_layout proposed, pending;
687 uint32_t sz, mtu;
688 int rc;
689
690 /* Extract the host's proposed layout */
691 rc = mctp_astlpc_layout_read(astlpc, &proposed);
692 if (rc < 0)
693 return rc;
694
695 if (!mctp_astlpc_layout_validate(&proposed))
696 return -EINVAL;
697
698 /* Negotiate the MTU */
699 mtu = mctp_astlpc_calculate_mtu(astlpc, &proposed);
700 sz = ASTLPC_PACKET_SIZE(MCTP_PACKET_SIZE(mtu));
701
702 /*
703 * Use symmetric MTUs by convention and to pass constraints in rx/tx
704 * functions
705 */
706 pending = astlpc->layout;
707 pending.tx.size = sz;
708 pending.rx.size = sz;
709
710 if (mctp_astlpc_layout_validate(&pending)) {
711 /* We found a sensible Rx MTU, so honour it */
712 astlpc->layout = pending;
713
714 /* Enforce the negotiated MTU */
715 rc = mctp_astlpc_layout_write(astlpc, &astlpc->layout);
716 if (rc < 0)
717 return rc;
718
719 astlpc_prinfo(astlpc, "Negotiated an MTU of %" PRIu32 " bytes",
720 mtu);
721 } else {
722 astlpc_prwarn(astlpc, "MTU negotiation failed");
723 return -EINVAL;
724 }
725
726 if (astlpc->version >= 2)
727 astlpc->binding.pkt_size = MCTP_PACKET_SIZE(mtu);
728
729 return 0;
730}
731
Jeremy Kerr672c8852019-03-01 12:18:07 +0800732static void mctp_astlpc_init_channel(struct mctp_binding_astlpc *astlpc)
733{
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930734 uint16_t negotiated, negotiated_be;
735 struct mctp_lpcmap_hdr hdr;
736 uint8_t status;
Andrew Jeffery3a540662020-05-26 19:55:30 +0930737 int rc;
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930738
739 mctp_astlpc_lpc_read(astlpc, &hdr, 0, sizeof(hdr));
740
741 /* Version negotiation */
742 negotiated =
743 mctp_astlpc_negotiate_version(ASTLPC_VER_MIN, ASTLPC_VER_CUR,
744 be16toh(hdr.host_ver_min),
745 be16toh(hdr.host_ver_cur));
746
Andrew Jeffery3a540662020-05-26 19:55:30 +0930747 /* Host Rx MTU negotiation: Failure terminates channel init */
748 rc = mctp_astlpc_negotiate_layout_bmc(astlpc);
749 if (rc < 0)
750 negotiated = ASTLPC_VER_BAD;
751
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930752 /* Populate the negotiated version */
753 astlpc->version = negotiated;
754 negotiated_be = htobe16(negotiated);
755 mctp_astlpc_lpc_write(astlpc, &negotiated_be,
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930756 offsetof(struct mctp_lpcmap_hdr, negotiated_ver),
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930757 sizeof(negotiated_be));
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930758
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930759 /* Finalise the configuration */
760 status = KCS_STATUS_BMC_READY | KCS_STATUS_OBF;
761 if (negotiated > 0) {
762 astlpc_prinfo(astlpc, "Negotiated binding version %" PRIu16,
763 negotiated);
764 status |= KCS_STATUS_CHANNEL_ACTIVE;
765 } else {
766 astlpc_prerr(astlpc, "Failed to initialise channel\n");
767 }
Jeremy Kerr672c8852019-03-01 12:18:07 +0800768
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930769 mctp_astlpc_kcs_set_status(astlpc, status);
770
771 mctp_binding_set_tx_enabled(&astlpc->binding,
772 status & KCS_STATUS_CHANNEL_ACTIVE);
Jeremy Kerr672c8852019-03-01 12:18:07 +0800773}
774
775static void mctp_astlpc_rx_start(struct mctp_binding_astlpc *astlpc)
776{
777 struct mctp_pktbuf *pkt;
778 uint32_t len;
779
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930780 mctp_astlpc_lpc_read(astlpc, &len, astlpc->layout.rx.offset,
781 sizeof(len));
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530782 len = be32toh(len);
783
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930784 if (len > ASTLPC_BODY_SIZE(astlpc->layout.rx.size)) {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930785 astlpc_prwarn(astlpc, "invalid RX len 0x%x", len);
Jeremy Kerr672c8852019-03-01 12:18:07 +0800786 return;
787 }
788
Andrew Jefferyb93b6112020-06-05 14:13:44 +0930789 assert(astlpc->binding.pkt_size >= 0);
790 if (len > (uint32_t)astlpc->binding.pkt_size) {
Jeremy Kerr672c8852019-03-01 12:18:07 +0800791 mctp_prwarn("invalid RX len 0x%x", len);
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930792 astlpc_prwarn(astlpc, "invalid RX len 0x%x", len);
Jeremy Kerr672c8852019-03-01 12:18:07 +0800793 return;
794 }
795
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +0800796 pkt = mctp_pktbuf_alloc(&astlpc->binding, len);
Jeremy Kerr672c8852019-03-01 12:18:07 +0800797 if (!pkt)
798 goto out_complete;
799
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930800 mctp_astlpc_lpc_read(astlpc, mctp_pktbuf_hdr(pkt),
801 astlpc->layout.rx.offset + 4, len);
Jeremy Kerr672c8852019-03-01 12:18:07 +0800802
803 mctp_bus_rx(&astlpc->binding, pkt);
804
805out_complete:
806 mctp_astlpc_kcs_send(astlpc, 0x2);
807}
808
809static void mctp_astlpc_tx_complete(struct mctp_binding_astlpc *astlpc)
810{
811 mctp_binding_set_tx_enabled(&astlpc->binding, true);
812}
813
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930814static int mctp_astlpc_finalise_channel(struct mctp_binding_astlpc *astlpc)
815{
Andrew Jeffery3a540662020-05-26 19:55:30 +0930816 struct mctp_astlpc_layout layout;
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930817 uint16_t negotiated;
818 int rc;
819
820 rc = mctp_astlpc_lpc_read(astlpc, &negotiated,
821 offsetof(struct mctp_lpcmap_hdr,
822 negotiated_ver),
823 sizeof(negotiated));
824 if (rc < 0)
825 return rc;
826
827 negotiated = be16toh(negotiated);
828
829 if (negotiated == ASTLPC_VER_BAD || negotiated < ASTLPC_VER_MIN ||
830 negotiated > ASTLPC_VER_CUR) {
831 astlpc_prerr(astlpc, "Failed to negotiate version, got: %u\n",
832 negotiated);
833 return -EINVAL;
834 }
835
836 astlpc->version = negotiated;
837
Andrew Jeffery3a540662020-05-26 19:55:30 +0930838 rc = mctp_astlpc_layout_read(astlpc, &layout);
839 if (rc < 0)
840 return rc;
841
842 if (!mctp_astlpc_layout_validate(&layout)) {
843 mctp_prerr("BMC proposed invalid buffer parameters");
844 return -EINVAL;
845 }
846
847 astlpc->layout = layout;
848
849 if (negotiated >= 2)
850 astlpc->binding.pkt_size =
851 ASTLPC_BODY_SIZE(astlpc->layout.tx.size);
852
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930853 return 0;
854}
855
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930856static int mctp_astlpc_update_channel(struct mctp_binding_astlpc *astlpc,
857 uint8_t status)
858{
859 uint8_t updated;
860 int rc = 0;
861
862 assert(astlpc->mode == MCTP_BINDING_ASTLPC_MODE_HOST);
863
864 updated = astlpc->kcs_status ^ status;
865
Andrew Jefferyd0f5da02020-05-28 09:12:55 +0930866 astlpc_prdebug(astlpc, "%s: status: 0x%x, update: 0x%x", __func__,
867 status, updated);
868
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930869 if (updated & KCS_STATUS_BMC_READY) {
Andrew Jefferyd0f5da02020-05-28 09:12:55 +0930870 if (status & KCS_STATUS_BMC_READY) {
871 astlpc->kcs_status = status;
872 return astlpc->binding.start(&astlpc->binding);
873 } else {
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930874 mctp_binding_set_tx_enabled(&astlpc->binding, false);
Andrew Jefferyd0f5da02020-05-28 09:12:55 +0930875 }
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930876 }
877
Andrew Jeffery4e8264b2020-05-23 20:34:33 +0930878 if (astlpc->version == 0 || updated & KCS_STATUS_CHANNEL_ACTIVE) {
879 bool enable;
880
881 rc = mctp_astlpc_finalise_channel(astlpc);
882 enable = (status & KCS_STATUS_CHANNEL_ACTIVE) && rc == 0;
883
884 mctp_binding_set_tx_enabled(&astlpc->binding, enable);
885 }
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930886
887 astlpc->kcs_status = status;
888
889 return rc;
890}
891
Jeremy Kerr672c8852019-03-01 12:18:07 +0800892int mctp_astlpc_poll(struct mctp_binding_astlpc *astlpc)
893{
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530894 uint8_t status, data;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800895 int rc;
896
Andrew Jefferyf13cb972020-05-28 09:30:09 +0930897 rc = mctp_astlpc_kcs_read(astlpc, MCTP_ASTLPC_KCS_REG_STATUS, &status);
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530898 if (rc) {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930899 astlpc_prwarn(astlpc, "KCS read error");
Jeremy Kerr672c8852019-03-01 12:18:07 +0800900 return -1;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800901 }
902
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930903 astlpc_prdebug(astlpc, "%s: status: 0x%hhx", __func__, status);
Andrew Jefferyedfe3832020-02-06 11:52:11 +1030904
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930905 if (!mctp_astlpc_kcs_read_ready(astlpc, status))
Jeremy Kerr672c8852019-03-01 12:18:07 +0800906 return 0;
907
Andrew Jefferyf13cb972020-05-28 09:30:09 +0930908 rc = mctp_astlpc_kcs_read(astlpc, MCTP_ASTLPC_KCS_REG_DATA, &data);
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530909 if (rc) {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930910 astlpc_prwarn(astlpc, "KCS data read error");
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530911 return -1;
912 }
913
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930914 astlpc_prdebug(astlpc, "%s: data: 0x%hhx", __func__, data);
Andrew Jefferyedfe3832020-02-06 11:52:11 +1030915
Jeremy Kerr672c8852019-03-01 12:18:07 +0800916 switch (data) {
917 case 0x0:
918 mctp_astlpc_init_channel(astlpc);
919 break;
920 case 0x1:
921 mctp_astlpc_rx_start(astlpc);
922 break;
923 case 0x2:
924 mctp_astlpc_tx_complete(astlpc);
925 break;
Jeremy Kerr1a4b55a2019-06-24 14:22:39 +0800926 case 0xff:
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930927 /* No responsibilities for the BMC on 0xff */
Andrew Jefferyd0f5da02020-05-28 09:12:55 +0930928 if (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_HOST) {
929 rc = mctp_astlpc_update_channel(astlpc, status);
930 if (rc < 0)
931 return rc;
932 }
933 break;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800934 default:
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930935 astlpc_prwarn(astlpc, "unknown message 0x%x", data);
Jeremy Kerr672c8852019-03-01 12:18:07 +0800936 }
Andrew Jefferyd0f5da02020-05-28 09:12:55 +0930937
938 /* Handle silent loss of bmc-ready */
939 if (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_HOST) {
940 if (!(status & KCS_STATUS_BMC_READY && data == 0xff))
941 return mctp_astlpc_update_channel(astlpc, status);
942 }
943
944 return rc;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800945}
946
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530947/* allocate and basic initialisation */
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930948static struct mctp_binding_astlpc *__mctp_astlpc_init(uint8_t mode,
949 uint32_t mtu)
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530950{
951 struct mctp_binding_astlpc *astlpc;
952
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930953 assert((mode == MCTP_BINDING_ASTLPC_MODE_BMC) ||
954 (mode == MCTP_BINDING_ASTLPC_MODE_HOST));
955
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530956 astlpc = __mctp_alloc(sizeof(*astlpc));
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930957 if (!astlpc)
958 return NULL;
959
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530960 memset(astlpc, 0, sizeof(*astlpc));
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930961 astlpc->mode = mode;
962 astlpc->lpc_map = NULL;
Andrew Jefferya9368982020-06-09 13:07:39 +0930963 astlpc->requested_mtu = mtu;
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530964 astlpc->binding.name = "astlpc";
965 astlpc->binding.version = 1;
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930966 astlpc->binding.pkt_size = MCTP_PACKET_SIZE(mtu);
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530967 astlpc->binding.pkt_pad = 0;
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930968 astlpc->binding.tx = mctp_binding_astlpc_tx;
969 if (mode == MCTP_BINDING_ASTLPC_MODE_BMC)
970 astlpc->binding.start = mctp_binding_astlpc_start_bmc;
971 else if (mode == MCTP_BINDING_ASTLPC_MODE_HOST)
972 astlpc->binding.start = mctp_binding_astlpc_start_host;
973 else {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930974 astlpc_prerr(astlpc, "%s: Invalid mode: %d\n", __func__, mode);
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930975 __mctp_free(astlpc);
976 return NULL;
977 }
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530978
979 return astlpc;
980}
981
Jeremy Kerr3b36d172019-09-04 11:56:09 +0800982struct mctp_binding *mctp_binding_astlpc_core(struct mctp_binding_astlpc *b)
983{
984 return &b->binding;
985}
986
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930987struct mctp_binding_astlpc *
988mctp_astlpc_init(uint8_t mode, uint32_t mtu, void *lpc_map,
989 const struct mctp_binding_astlpc_ops *ops, void *ops_data)
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530990{
991 struct mctp_binding_astlpc *astlpc;
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530992
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930993 if (!(mode == MCTP_BINDING_ASTLPC_MODE_BMC ||
994 mode == MCTP_BINDING_ASTLPC_MODE_HOST)) {
995 mctp_prerr("Unknown binding mode: %u", mode);
996 return NULL;
997 }
998
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930999 astlpc = __mctp_astlpc_init(mode, mtu);
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301000 if (!astlpc)
1001 return NULL;
1002
1003 memcpy(&astlpc->ops, ops, sizeof(astlpc->ops));
1004 astlpc->ops_data = ops_data;
1005 astlpc->lpc_map = lpc_map;
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301006 astlpc->mode = mode;
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301007
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301008 return astlpc;
1009}
1010
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301011struct mctp_binding_astlpc *
1012mctp_astlpc_init_ops(const struct mctp_binding_astlpc_ops *ops, void *ops_data,
1013 void *lpc_map)
1014{
1015 return mctp_astlpc_init(MCTP_BINDING_ASTLPC_MODE_BMC, MCTP_BTU, lpc_map,
1016 ops, ops_data);
1017}
1018
Andrew Jeffery4663f672020-03-10 23:36:24 +10301019void mctp_astlpc_destroy(struct mctp_binding_astlpc *astlpc)
1020{
Andrew Jefferyd0f5da02020-05-28 09:12:55 +09301021 /* Clear channel-active and bmc-ready */
1022 if (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_BMC)
1023 mctp_astlpc_kcs_set_status(astlpc, KCS_STATUS_OBF);
Andrew Jeffery4663f672020-03-10 23:36:24 +10301024 __mctp_free(astlpc);
1025}
1026
Jeremy Kerrb214c642019-11-27 11:34:00 +08001027#ifdef MCTP_HAVE_FILEIO
Andrew Jeffery55fb90b2020-05-12 13:54:37 +09301028
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301029static int mctp_astlpc_init_fileio_lpc(struct mctp_binding_astlpc *astlpc)
Jeremy Kerr672c8852019-03-01 12:18:07 +08001030{
1031 struct aspeed_lpc_ctrl_mapping map = {
1032 .window_type = ASPEED_LPC_CTRL_WINDOW_MEMORY,
1033 .window_id = 0, /* There's only one */
1034 .flags = 0,
1035 .addr = 0,
1036 .offset = 0,
1037 .size = 0
1038 };
Andrew Jeffery979c6a12020-05-23 20:04:49 +09301039 void *lpc_map_base;
Jeremy Kerr672c8852019-03-01 12:18:07 +08001040 int fd, rc;
1041
1042 fd = open(lpc_path, O_RDWR | O_SYNC);
1043 if (fd < 0) {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +09301044 astlpc_prwarn(astlpc, "LPC open (%s) failed", lpc_path);
Jeremy Kerr672c8852019-03-01 12:18:07 +08001045 return -1;
1046 }
1047
1048 rc = ioctl(fd, ASPEED_LPC_CTRL_IOCTL_GET_SIZE, &map);
1049 if (rc) {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +09301050 astlpc_prwarn(astlpc, "LPC GET_SIZE failed");
Jeremy Kerr672c8852019-03-01 12:18:07 +08001051 close(fd);
1052 return -1;
1053 }
1054
Andrew Jeffery979c6a12020-05-23 20:04:49 +09301055 lpc_map_base =
1056 mmap(NULL, map.size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
1057 if (lpc_map_base == MAP_FAILED) {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +09301058 astlpc_prwarn(astlpc, "LPC mmap failed");
Jeremy Kerr672c8852019-03-01 12:18:07 +08001059 rc = -1;
1060 } else {
Andrew Jeffery979c6a12020-05-23 20:04:49 +09301061 astlpc->lpc_map = lpc_map_base + map.size - LPC_WIN_SIZE;
Jeremy Kerr672c8852019-03-01 12:18:07 +08001062 }
1063
1064 close(fd);
1065
1066 return rc;
1067}
1068
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301069static int mctp_astlpc_init_fileio_kcs(struct mctp_binding_astlpc *astlpc)
Jeremy Kerr672c8852019-03-01 12:18:07 +08001070{
1071 astlpc->kcs_fd = open(kcs_path, O_RDWR);
1072 if (astlpc->kcs_fd < 0)
1073 return -1;
1074
1075 return 0;
1076}
1077
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301078static int __mctp_astlpc_fileio_kcs_read(void *arg,
1079 enum mctp_binding_astlpc_kcs_reg reg, uint8_t *val)
1080{
1081 struct mctp_binding_astlpc *astlpc = arg;
1082 off_t offset = reg;
1083 int rc;
1084
1085 rc = pread(astlpc->kcs_fd, val, 1, offset);
1086
1087 return rc == 1 ? 0 : -1;
1088}
1089
1090static int __mctp_astlpc_fileio_kcs_write(void *arg,
1091 enum mctp_binding_astlpc_kcs_reg reg, uint8_t val)
1092{
1093 struct mctp_binding_astlpc *astlpc = arg;
1094 off_t offset = reg;
1095 int rc;
1096
1097 rc = pwrite(astlpc->kcs_fd, &val, 1, offset);
1098
1099 return rc == 1 ? 0 : -1;
1100}
1101
1102int mctp_astlpc_get_fd(struct mctp_binding_astlpc *astlpc)
1103{
1104 return astlpc->kcs_fd;
1105}
1106
1107struct mctp_binding_astlpc *mctp_astlpc_init_fileio(void)
Jeremy Kerr672c8852019-03-01 12:18:07 +08001108{
1109 struct mctp_binding_astlpc *astlpc;
1110 int rc;
1111
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301112 /*
1113 * If we're doing file IO then we're very likely not running
Andrew Jeffery8877c462020-06-15 12:22:53 +09301114 * freestanding, so lets assume that we're on the BMC side.
1115 *
1116 * Requesting an MTU of 0 requests the largest possible MTU, whatever
1117 * value that might take.
Andrew Jeffery7cd72f12020-05-12 20:27:59 +09301118 */
Andrew Jeffery8877c462020-06-15 12:22:53 +09301119 astlpc = __mctp_astlpc_init(MCTP_BINDING_ASTLPC_MODE_BMC, 0);
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301120 if (!astlpc)
1121 return NULL;
Jeremy Kerr672c8852019-03-01 12:18:07 +08001122
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301123 /* Set internal operations for kcs. We use direct accesses to the lpc
1124 * map area */
1125 astlpc->ops.kcs_read = __mctp_astlpc_fileio_kcs_read;
1126 astlpc->ops.kcs_write = __mctp_astlpc_fileio_kcs_write;
1127 astlpc->ops_data = astlpc;
1128
1129 rc = mctp_astlpc_init_fileio_lpc(astlpc);
Jeremy Kerr672c8852019-03-01 12:18:07 +08001130 if (rc) {
1131 free(astlpc);
1132 return NULL;
1133 }
1134
Jeremy Kerrbc53d352019-08-28 14:26:14 +05301135 rc = mctp_astlpc_init_fileio_kcs(astlpc);
Jeremy Kerr672c8852019-03-01 12:18:07 +08001136 if (rc) {
1137 free(astlpc);
1138 return NULL;
1139 }
1140
Jeremy Kerr672c8852019-03-01 12:18:07 +08001141 return astlpc;
1142}
Jeremy Kerr92a10a62019-08-28 16:55:54 +05301143#else
1144struct mctp_binding_astlpc * __attribute__((const))
1145 mctp_astlpc_init_fileio(void)
1146{
Andrew Jeffery9101a2a2020-05-22 16:08:03 +09301147 astlpc_prerr(astlpc, "Missing support for file IO");
Jeremy Kerr92a10a62019-08-28 16:55:54 +05301148 return NULL;
1149}
Jeremy Kerr672c8852019-03-01 12:18:07 +08001150
Jeremy Kerr92a10a62019-08-28 16:55:54 +05301151int __attribute__((const)) mctp_astlpc_get_fd(
1152 struct mctp_binding_astlpc *astlpc __attribute__((unused)))
1153{
Andrew Jeffery9101a2a2020-05-22 16:08:03 +09301154 astlpc_prerr(astlpc, "Missing support for file IO");
Jeremy Kerr92a10a62019-08-28 16:55:54 +05301155 return -1;
1156}
1157#endif