blob: d32211e31d503c33ea3f3f323faad4329df352de [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;
Jeremy Kerrbc53d352019-08-28 14:26:14 +053058
59 /* direct ops data */
Andrew Jeffery55fb90b2020-05-12 13:54:37 +093060 struct mctp_binding_astlpc_ops ops;
61 void *ops_data;
Jeremy Kerrbc53d352019-08-28 14:26:14 +053062
63 /* fileio ops data */
64 void *lpc_map_base;
Jeremy Kerr672c8852019-03-01 12:18:07 +080065 int kcs_fd;
66 uint8_t kcs_status;
67
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
92#define ASTLPC_VER_MIN 1
93#define ASTLPC_VER_CUR 1
94
95#define ASTLPC_BODY_SIZE(sz) ((sz) - 4)
96/* clang-format on */
Jeremy Kerr672c8852019-03-01 12:18:07 +080097
98struct mctp_lpcmap_hdr {
99 uint32_t magic;
100
101 uint16_t bmc_ver_min;
102 uint16_t bmc_ver_cur;
103 uint16_t host_ver_min;
104 uint16_t host_ver_cur;
105 uint16_t negotiated_ver;
106 uint16_t pad0;
107
108 uint32_t rx_offset;
109 uint32_t rx_size;
110 uint32_t tx_offset;
111 uint32_t tx_size;
112} __attribute__((packed));
113
114/* layout of TX/RX areas */
115static const uint32_t rx_offset = 0x100;
116static const uint32_t rx_size = 0x100;
117static const uint32_t tx_offset = 0x200;
118static const uint32_t tx_size = 0x100;
119
Jeremy Kerr672c8852019-03-01 12:18:07 +0800120#define LPC_WIN_SIZE (1 * 1024 * 1024)
121
122enum {
123 KCS_REG_DATA = 0,
124 KCS_REG_STATUS = 1,
125};
126
127#define KCS_STATUS_BMC_READY 0x80
128#define KCS_STATUS_CHANNEL_ACTIVE 0x40
129#define KCS_STATUS_IBF 0x02
130#define KCS_STATUS_OBF 0x01
131
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930132static inline int mctp_astlpc_lpc_write(struct mctp_binding_astlpc *astlpc,
133 const void *buf, long offset,
134 size_t len)
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530135{
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930136 astlpc_prdebug(astlpc, "%s: %zu bytes to 0x%lx", __func__, len, offset);
137
138 assert(offset >= 0);
139
140 /* Indirect access */
141 if (astlpc->ops.lpc_write) {
142 void *data = astlpc->ops_data;
143
144 return astlpc->ops.lpc_write(data, buf, offset, len);
145 }
146
147 /* Direct mapping */
148 assert(astlpc->lpc_map);
149 memcpy(&((char *)astlpc->lpc_map)[offset], buf, len);
150
151 return 0;
152}
153
154static inline int mctp_astlpc_lpc_read(struct mctp_binding_astlpc *astlpc,
155 void *buf, long offset, size_t len)
156{
157 astlpc_prdebug(astlpc, "%s: %zu bytes from 0x%lx", __func__, len,
158 offset);
159
160 assert(offset >= 0);
161
162 /* Indirect access */
163 if (astlpc->ops.lpc_read) {
164 void *data = astlpc->ops_data;
165
166 return astlpc->ops.lpc_read(data, buf, offset, len);
167 }
168
169 /* Direct mapping */
170 assert(astlpc->lpc_map);
171 memcpy(buf, &((char *)astlpc->lpc_map)[offset], len);
172
173 return 0;
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530174}
175
Andrew Jefferyd0f5da02020-05-28 09:12:55 +0930176static int mctp_astlpc_kcs_set_status(struct mctp_binding_astlpc *astlpc,
177 uint8_t status)
178{
179 uint8_t data;
180 int rc;
181
182 /* Since we're setting the status register, we want the other endpoint
183 * to be interrupted. However, some hardware may only raise a host-side
184 * interrupt on an ODR event.
185 * So, write a dummy value of 0xff to ODR, which will ensure that an
186 * interrupt is triggered, and can be ignored by the host.
187 */
188 data = 0xff;
189 status |= KCS_STATUS_OBF;
190
191 rc = astlpc->ops.kcs_write(astlpc->ops_data, MCTP_ASTLPC_KCS_REG_STATUS,
192 status);
193 if (rc) {
194 astlpc_prwarn(astlpc, "KCS status write failed");
195 return -1;
196 }
197
198 rc = astlpc->ops.kcs_write(astlpc->ops_data, MCTP_ASTLPC_KCS_REG_DATA,
199 data);
200 if (rc) {
201 astlpc_prwarn(astlpc, "KCS dummy data write failed");
202 return -1;
203 }
204
205 return 0;
206}
207
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930208static int mctp_astlpc_init_bmc(struct mctp_binding_astlpc *astlpc)
209{
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930210 struct mctp_lpcmap_hdr hdr = { 0 };
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930211 uint8_t status;
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930212
213 /* Flip the buffers as the names are defined in terms of the host */
214 astlpc->layout.rx.offset = tx_offset;
215 astlpc->layout.rx.size = tx_size;
216 astlpc->layout.tx.offset = rx_offset;
217 astlpc->layout.tx.size = rx_size;
218
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930219 hdr = (struct mctp_lpcmap_hdr){
220 .magic = htobe32(ASTLPC_MCTP_MAGIC),
221 .bmc_ver_min = htobe16(ASTLPC_VER_MIN),
222 .bmc_ver_cur = htobe16(ASTLPC_VER_CUR),
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930223
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930224 /* Flip the buffers back as we're now describing the host's
225 * configuration to the host */
226 .rx_offset = htobe32(astlpc->layout.tx.offset),
227 .rx_size = htobe32(astlpc->layout.tx.size),
228 .tx_offset = htobe32(astlpc->layout.rx.offset),
229 .tx_size = htobe32(astlpc->layout.rx.size),
230 };
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930231
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930232 mctp_astlpc_lpc_write(astlpc, &hdr, 0, sizeof(hdr));
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930233
234 /* set status indicating that the BMC is now active */
235 status = KCS_STATUS_BMC_READY | KCS_STATUS_OBF;
Andrew Jefferyd0f5da02020-05-28 09:12:55 +0930236 return mctp_astlpc_kcs_set_status(astlpc, status);
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930237}
238
239static int mctp_binding_astlpc_start_bmc(struct mctp_binding *b)
240{
241 struct mctp_binding_astlpc *astlpc =
242 container_of(b, struct mctp_binding_astlpc, binding);
243
244 return mctp_astlpc_init_bmc(astlpc);
245}
246
247static int mctp_astlpc_init_host(struct mctp_binding_astlpc *astlpc)
248{
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930249 const uint16_t ver_min_be = htobe16(ASTLPC_VER_MIN);
250 const uint16_t ver_cur_be = htobe16(ASTLPC_VER_CUR);
251 struct mctp_lpcmap_hdr hdr;
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930252 uint8_t status;
253 int rc;
254
255 rc = astlpc->ops.kcs_read(astlpc->ops_data, MCTP_ASTLPC_KCS_REG_STATUS,
256 &status);
257 if (rc) {
258 mctp_prwarn("KCS status read failed");
259 return rc;
260 }
261
262 astlpc->kcs_status = status;
263
264 if (!(status & KCS_STATUS_BMC_READY))
265 return -EHOSTDOWN;
266
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930267 mctp_astlpc_lpc_read(astlpc, &hdr, 0, sizeof(hdr));
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930268
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930269 astlpc->layout.rx.offset = be32toh(hdr.rx_offset);
270 astlpc->layout.rx.size = be32toh(hdr.rx_size);
271 astlpc->layout.tx.offset = be32toh(hdr.tx_offset);
272 astlpc->layout.tx.size = be32toh(hdr.tx_size);
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930273
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930274 mctp_astlpc_lpc_write(astlpc, &ver_min_be,
275 offsetof(struct mctp_lpcmap_hdr, host_ver_min),
276 sizeof(ver_min_be));
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930277
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930278 mctp_astlpc_lpc_write(astlpc, &ver_cur_be,
279 offsetof(struct mctp_lpcmap_hdr, host_ver_cur),
280 sizeof(ver_cur_be));
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930281
282 /* Send channel init command */
283 rc = astlpc->ops.kcs_write(astlpc->ops_data, MCTP_ASTLPC_KCS_REG_DATA,
284 0x0);
285 if (rc) {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930286 astlpc_prwarn(astlpc, "KCS write failed");
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930287 }
288
289 return rc;
290}
291
292static int mctp_binding_astlpc_start_host(struct mctp_binding *b)
293{
294 struct mctp_binding_astlpc *astlpc =
295 container_of(b, struct mctp_binding_astlpc, binding);
296
297 return mctp_astlpc_init_host(astlpc);
298}
299
300static bool __mctp_astlpc_kcs_ready(struct mctp_binding_astlpc *astlpc,
301 uint8_t status, bool is_write)
302{
303 bool is_bmc;
304 bool ready_state;
305 uint8_t flag;
306
307 is_bmc = (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_BMC);
308 flag = (is_bmc ^ is_write) ? KCS_STATUS_IBF : KCS_STATUS_OBF;
309 ready_state = is_write ? 0 : 1;
310
311 return !!(status & flag) == ready_state;
312}
313
314static inline bool
315mctp_astlpc_kcs_read_ready(struct mctp_binding_astlpc *astlpc, uint8_t status)
316{
317 return __mctp_astlpc_kcs_ready(astlpc, status, false);
318}
319
320static inline bool
321mctp_astlpc_kcs_write_ready(struct mctp_binding_astlpc *astlpc, uint8_t status)
322{
323 return __mctp_astlpc_kcs_ready(astlpc, status, true);
324}
325
Jeremy Kerr672c8852019-03-01 12:18:07 +0800326static int mctp_astlpc_kcs_send(struct mctp_binding_astlpc *astlpc,
327 uint8_t data)
328{
329 uint8_t status;
330 int rc;
331
332 for (;;) {
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530333 rc = astlpc->ops.kcs_read(astlpc->ops_data,
334 MCTP_ASTLPC_KCS_REG_STATUS, &status);
Andrew Jeffery1b27fe82020-01-24 16:05:11 +1030335 if (rc) {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930336 astlpc_prwarn(astlpc, "KCS status read failed");
Jeremy Kerr672c8852019-03-01 12:18:07 +0800337 return -1;
338 }
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930339 if (mctp_astlpc_kcs_write_ready(astlpc, status))
Jeremy Kerr672c8852019-03-01 12:18:07 +0800340 break;
341 /* todo: timeout */
342 }
343
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530344 rc = astlpc->ops.kcs_write(astlpc->ops_data, MCTP_ASTLPC_KCS_REG_DATA,
345 data);
346 if (rc) {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930347 astlpc_prwarn(astlpc, "KCS data write failed");
Jeremy Kerr672c8852019-03-01 12:18:07 +0800348 return -1;
349 }
350
351 return 0;
352}
353
354static int mctp_binding_astlpc_tx(struct mctp_binding *b,
355 struct mctp_pktbuf *pkt)
356{
357 struct mctp_binding_astlpc *astlpc = binding_to_astlpc(b);
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930358 uint32_t len, len_be;
Andrew Jefferyedfe3832020-02-06 11:52:11 +1030359 struct mctp_hdr *hdr;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800360
Andrew Jefferyedfe3832020-02-06 11:52:11 +1030361 hdr = mctp_pktbuf_hdr(pkt);
Jeremy Kerr672c8852019-03-01 12:18:07 +0800362 len = mctp_pktbuf_size(pkt);
Andrew Jefferyedfe3832020-02-06 11:52:11 +1030363
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930364 astlpc_prdebug(astlpc,
365 "%s: Transmitting %" PRIu32
366 "-byte packet (%hhu, %hhu, 0x%hhx)",
367 __func__, len, hdr->src, hdr->dest, hdr->flags_seq_tag);
Andrew Jefferyedfe3832020-02-06 11:52:11 +1030368
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930369 if (len > ASTLPC_BODY_SIZE(astlpc->layout.tx.size)) {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930370 astlpc_prwarn(astlpc, "invalid TX len 0x%x", len);
Jeremy Kerr672c8852019-03-01 12:18:07 +0800371 return -1;
372 }
373
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930374 len_be = htobe32(len);
375 mctp_astlpc_lpc_write(astlpc, &len_be, astlpc->layout.tx.offset,
376 sizeof(len_be));
377 mctp_astlpc_lpc_write(astlpc, hdr, astlpc->layout.tx.offset + 4, len);
Jeremy Kerr672c8852019-03-01 12:18:07 +0800378
379 mctp_binding_set_tx_enabled(b, false);
380
381 mctp_astlpc_kcs_send(astlpc, 0x1);
382 return 0;
383}
384
385static void mctp_astlpc_init_channel(struct mctp_binding_astlpc *astlpc)
386{
387 /* todo: actual version negotiation */
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930388 uint16_t negotiated = htobe16(1);
389 mctp_astlpc_lpc_write(astlpc, &negotiated,
390 offsetof(struct mctp_lpcmap_hdr, negotiated_ver),
391 sizeof(negotiated));
392
393 mctp_astlpc_kcs_set_status(astlpc, KCS_STATUS_BMC_READY |
394 KCS_STATUS_CHANNEL_ACTIVE |
395 KCS_STATUS_OBF);
Jeremy Kerr672c8852019-03-01 12:18:07 +0800396
397 mctp_binding_set_tx_enabled(&astlpc->binding, true);
398}
399
400static void mctp_astlpc_rx_start(struct mctp_binding_astlpc *astlpc)
401{
402 struct mctp_pktbuf *pkt;
403 uint32_t len;
404
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930405 mctp_astlpc_lpc_read(astlpc, &len, astlpc->layout.rx.offset,
406 sizeof(len));
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530407 len = be32toh(len);
408
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930409 if (len > ASTLPC_BODY_SIZE(astlpc->layout.rx.size)) {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930410 astlpc_prwarn(astlpc, "invalid RX len 0x%x", len);
Jeremy Kerr672c8852019-03-01 12:18:07 +0800411 return;
412 }
413
Andrew Jefferyb93b6112020-06-05 14:13:44 +0930414 assert(astlpc->binding.pkt_size >= 0);
415 if (len > (uint32_t)astlpc->binding.pkt_size) {
Jeremy Kerr672c8852019-03-01 12:18:07 +0800416 mctp_prwarn("invalid RX len 0x%x", len);
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930417 astlpc_prwarn(astlpc, "invalid RX len 0x%x", len);
Jeremy Kerr672c8852019-03-01 12:18:07 +0800418 return;
419 }
420
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +0800421 pkt = mctp_pktbuf_alloc(&astlpc->binding, len);
Jeremy Kerr672c8852019-03-01 12:18:07 +0800422 if (!pkt)
423 goto out_complete;
424
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930425 mctp_astlpc_lpc_read(astlpc, mctp_pktbuf_hdr(pkt),
426 astlpc->layout.rx.offset + 4, len);
Jeremy Kerr672c8852019-03-01 12:18:07 +0800427
428 mctp_bus_rx(&astlpc->binding, pkt);
429
430out_complete:
431 mctp_astlpc_kcs_send(astlpc, 0x2);
432}
433
434static void mctp_astlpc_tx_complete(struct mctp_binding_astlpc *astlpc)
435{
436 mctp_binding_set_tx_enabled(&astlpc->binding, true);
437}
438
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930439static int mctp_astlpc_update_channel(struct mctp_binding_astlpc *astlpc,
440 uint8_t status)
441{
442 uint8_t updated;
443 int rc = 0;
444
445 assert(astlpc->mode == MCTP_BINDING_ASTLPC_MODE_HOST);
446
447 updated = astlpc->kcs_status ^ status;
448
Andrew Jefferyd0f5da02020-05-28 09:12:55 +0930449 astlpc_prdebug(astlpc, "%s: status: 0x%x, update: 0x%x", __func__,
450 status, updated);
451
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930452 if (updated & KCS_STATUS_BMC_READY) {
Andrew Jefferyd0f5da02020-05-28 09:12:55 +0930453 if (status & KCS_STATUS_BMC_READY) {
454 astlpc->kcs_status = status;
455 return astlpc->binding.start(&astlpc->binding);
456 } else {
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930457 mctp_binding_set_tx_enabled(&astlpc->binding, false);
Andrew Jefferyd0f5da02020-05-28 09:12:55 +0930458 }
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930459 }
460
461 if (updated & KCS_STATUS_CHANNEL_ACTIVE)
462 mctp_binding_set_tx_enabled(&astlpc->binding,
463 status & KCS_STATUS_CHANNEL_ACTIVE);
464
465 astlpc->kcs_status = status;
466
467 return rc;
468}
469
Jeremy Kerr672c8852019-03-01 12:18:07 +0800470int mctp_astlpc_poll(struct mctp_binding_astlpc *astlpc)
471{
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530472 uint8_t status, data;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800473 int rc;
474
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530475 rc = astlpc->ops.kcs_read(astlpc->ops_data, MCTP_ASTLPC_KCS_REG_STATUS,
476 &status);
477 if (rc) {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930478 astlpc_prwarn(astlpc, "KCS read error");
Jeremy Kerr672c8852019-03-01 12:18:07 +0800479 return -1;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800480 }
481
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930482 astlpc_prdebug(astlpc, "%s: status: 0x%hhx", __func__, status);
Andrew Jefferyedfe3832020-02-06 11:52:11 +1030483
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930484 if (!mctp_astlpc_kcs_read_ready(astlpc, status))
Jeremy Kerr672c8852019-03-01 12:18:07 +0800485 return 0;
486
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530487 rc = astlpc->ops.kcs_read(astlpc->ops_data, MCTP_ASTLPC_KCS_REG_DATA,
488 &data);
489 if (rc) {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930490 astlpc_prwarn(astlpc, "KCS data read error");
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530491 return -1;
492 }
493
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930494 astlpc_prdebug(astlpc, "%s: data: 0x%hhx", __func__, data);
Andrew Jefferyedfe3832020-02-06 11:52:11 +1030495
Jeremy Kerr672c8852019-03-01 12:18:07 +0800496 switch (data) {
497 case 0x0:
498 mctp_astlpc_init_channel(astlpc);
499 break;
500 case 0x1:
501 mctp_astlpc_rx_start(astlpc);
502 break;
503 case 0x2:
504 mctp_astlpc_tx_complete(astlpc);
505 break;
Jeremy Kerr1a4b55a2019-06-24 14:22:39 +0800506 case 0xff:
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930507 /* No responsibilities for the BMC on 0xff */
Andrew Jefferyd0f5da02020-05-28 09:12:55 +0930508 if (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_HOST) {
509 rc = mctp_astlpc_update_channel(astlpc, status);
510 if (rc < 0)
511 return rc;
512 }
513 break;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800514 default:
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930515 astlpc_prwarn(astlpc, "unknown message 0x%x", data);
Jeremy Kerr672c8852019-03-01 12:18:07 +0800516 }
Andrew Jefferyd0f5da02020-05-28 09:12:55 +0930517
518 /* Handle silent loss of bmc-ready */
519 if (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_HOST) {
520 if (!(status & KCS_STATUS_BMC_READY && data == 0xff))
521 return mctp_astlpc_update_channel(astlpc, status);
522 }
523
524 return rc;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800525}
526
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530527/* allocate and basic initialisation */
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930528static struct mctp_binding_astlpc *__mctp_astlpc_init(uint8_t mode,
529 uint32_t mtu)
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530530{
531 struct mctp_binding_astlpc *astlpc;
532
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930533 assert((mode == MCTP_BINDING_ASTLPC_MODE_BMC) ||
534 (mode == MCTP_BINDING_ASTLPC_MODE_HOST));
535
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530536 astlpc = __mctp_alloc(sizeof(*astlpc));
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930537 if (!astlpc)
538 return NULL;
539
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530540 memset(astlpc, 0, sizeof(*astlpc));
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930541 astlpc->mode = mode;
542 astlpc->lpc_map = NULL;
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530543 astlpc->binding.name = "astlpc";
544 astlpc->binding.version = 1;
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930545 astlpc->binding.pkt_size = MCTP_PACKET_SIZE(mtu);
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530546 astlpc->binding.pkt_pad = 0;
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930547 astlpc->binding.tx = mctp_binding_astlpc_tx;
548 if (mode == MCTP_BINDING_ASTLPC_MODE_BMC)
549 astlpc->binding.start = mctp_binding_astlpc_start_bmc;
550 else if (mode == MCTP_BINDING_ASTLPC_MODE_HOST)
551 astlpc->binding.start = mctp_binding_astlpc_start_host;
552 else {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930553 astlpc_prerr(astlpc, "%s: Invalid mode: %d\n", __func__, mode);
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930554 __mctp_free(astlpc);
555 return NULL;
556 }
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530557
558 return astlpc;
559}
560
Jeremy Kerr3b36d172019-09-04 11:56:09 +0800561struct mctp_binding *mctp_binding_astlpc_core(struct mctp_binding_astlpc *b)
562{
563 return &b->binding;
564}
565
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930566struct mctp_binding_astlpc *
567mctp_astlpc_init(uint8_t mode, uint32_t mtu, void *lpc_map,
568 const struct mctp_binding_astlpc_ops *ops, void *ops_data)
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530569{
570 struct mctp_binding_astlpc *astlpc;
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530571
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930572 if (!(mode == MCTP_BINDING_ASTLPC_MODE_BMC ||
573 mode == MCTP_BINDING_ASTLPC_MODE_HOST)) {
574 mctp_prerr("Unknown binding mode: %u", mode);
575 return NULL;
576 }
577
578 if (mtu != MCTP_BTU) {
579 mctp_prwarn("Unable to negotiate the MTU, using %u instead",
580 MCTP_BTU);
581 mtu = MCTP_BTU;
582 }
583
584 astlpc = __mctp_astlpc_init(mode, mtu);
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530585 if (!astlpc)
586 return NULL;
587
588 memcpy(&astlpc->ops, ops, sizeof(astlpc->ops));
589 astlpc->ops_data = ops_data;
590 astlpc->lpc_map = lpc_map;
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930591 astlpc->mode = mode;
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530592
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530593 return astlpc;
594}
595
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930596struct mctp_binding_astlpc *
597mctp_astlpc_init_ops(const struct mctp_binding_astlpc_ops *ops, void *ops_data,
598 void *lpc_map)
599{
600 return mctp_astlpc_init(MCTP_BINDING_ASTLPC_MODE_BMC, MCTP_BTU, lpc_map,
601 ops, ops_data);
602}
603
Andrew Jeffery4663f672020-03-10 23:36:24 +1030604void mctp_astlpc_destroy(struct mctp_binding_astlpc *astlpc)
605{
Andrew Jefferyd0f5da02020-05-28 09:12:55 +0930606 /* Clear channel-active and bmc-ready */
607 if (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_BMC)
608 mctp_astlpc_kcs_set_status(astlpc, KCS_STATUS_OBF);
Andrew Jeffery4663f672020-03-10 23:36:24 +1030609 __mctp_free(astlpc);
610}
611
Jeremy Kerrb214c642019-11-27 11:34:00 +0800612#ifdef MCTP_HAVE_FILEIO
Andrew Jeffery55fb90b2020-05-12 13:54:37 +0930613
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530614static int mctp_astlpc_init_fileio_lpc(struct mctp_binding_astlpc *astlpc)
Jeremy Kerr672c8852019-03-01 12:18:07 +0800615{
616 struct aspeed_lpc_ctrl_mapping map = {
617 .window_type = ASPEED_LPC_CTRL_WINDOW_MEMORY,
618 .window_id = 0, /* There's only one */
619 .flags = 0,
620 .addr = 0,
621 .offset = 0,
622 .size = 0
623 };
624 int fd, rc;
625
626 fd = open(lpc_path, O_RDWR | O_SYNC);
627 if (fd < 0) {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930628 astlpc_prwarn(astlpc, "LPC open (%s) failed", lpc_path);
Jeremy Kerr672c8852019-03-01 12:18:07 +0800629 return -1;
630 }
631
632 rc = ioctl(fd, ASPEED_LPC_CTRL_IOCTL_GET_SIZE, &map);
633 if (rc) {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930634 astlpc_prwarn(astlpc, "LPC GET_SIZE failed");
Jeremy Kerr672c8852019-03-01 12:18:07 +0800635 close(fd);
636 return -1;
637 }
638
639 astlpc->lpc_map_base = mmap(NULL, map.size, PROT_READ | PROT_WRITE,
640 MAP_SHARED, fd, 0);
641 if (astlpc->lpc_map_base == MAP_FAILED) {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930642 astlpc_prwarn(astlpc, "LPC mmap failed");
Jeremy Kerr672c8852019-03-01 12:18:07 +0800643 rc = -1;
644 } else {
645 astlpc->lpc_map = astlpc->lpc_map_base +
646 map.size - LPC_WIN_SIZE;
647 }
648
649 close(fd);
650
651 return rc;
652}
653
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530654static int mctp_astlpc_init_fileio_kcs(struct mctp_binding_astlpc *astlpc)
Jeremy Kerr672c8852019-03-01 12:18:07 +0800655{
656 astlpc->kcs_fd = open(kcs_path, O_RDWR);
657 if (astlpc->kcs_fd < 0)
658 return -1;
659
660 return 0;
661}
662
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530663static int __mctp_astlpc_fileio_kcs_read(void *arg,
664 enum mctp_binding_astlpc_kcs_reg reg, uint8_t *val)
665{
666 struct mctp_binding_astlpc *astlpc = arg;
667 off_t offset = reg;
668 int rc;
669
670 rc = pread(astlpc->kcs_fd, val, 1, offset);
671
672 return rc == 1 ? 0 : -1;
673}
674
675static int __mctp_astlpc_fileio_kcs_write(void *arg,
676 enum mctp_binding_astlpc_kcs_reg reg, uint8_t val)
677{
678 struct mctp_binding_astlpc *astlpc = arg;
679 off_t offset = reg;
680 int rc;
681
682 rc = pwrite(astlpc->kcs_fd, &val, 1, offset);
683
684 return rc == 1 ? 0 : -1;
685}
686
687int mctp_astlpc_get_fd(struct mctp_binding_astlpc *astlpc)
688{
689 return astlpc->kcs_fd;
690}
691
692struct mctp_binding_astlpc *mctp_astlpc_init_fileio(void)
Jeremy Kerr672c8852019-03-01 12:18:07 +0800693{
694 struct mctp_binding_astlpc *astlpc;
695 int rc;
696
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930697 /*
698 * If we're doing file IO then we're very likely not running
699 * freestanding, so lets assume that we're on the BMC side
700 */
701 astlpc = __mctp_astlpc_init(MCTP_BINDING_ASTLPC_MODE_BMC, MCTP_BTU);
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530702 if (!astlpc)
703 return NULL;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800704
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530705 /* Set internal operations for kcs. We use direct accesses to the lpc
706 * map area */
707 astlpc->ops.kcs_read = __mctp_astlpc_fileio_kcs_read;
708 astlpc->ops.kcs_write = __mctp_astlpc_fileio_kcs_write;
709 astlpc->ops_data = astlpc;
710
711 rc = mctp_astlpc_init_fileio_lpc(astlpc);
Jeremy Kerr672c8852019-03-01 12:18:07 +0800712 if (rc) {
713 free(astlpc);
714 return NULL;
715 }
716
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530717 rc = mctp_astlpc_init_fileio_kcs(astlpc);
Jeremy Kerr672c8852019-03-01 12:18:07 +0800718 if (rc) {
719 free(astlpc);
720 return NULL;
721 }
722
Jeremy Kerr672c8852019-03-01 12:18:07 +0800723 return astlpc;
724}
Jeremy Kerr92a10a62019-08-28 16:55:54 +0530725#else
726struct mctp_binding_astlpc * __attribute__((const))
727 mctp_astlpc_init_fileio(void)
728{
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930729 astlpc_prerr(astlpc, "Missing support for file IO");
Jeremy Kerr92a10a62019-08-28 16:55:54 +0530730 return NULL;
731}
Jeremy Kerr672c8852019-03-01 12:18:07 +0800732
Jeremy Kerr92a10a62019-08-28 16:55:54 +0530733int __attribute__((const)) mctp_astlpc_get_fd(
734 struct mctp_binding_astlpc *astlpc __attribute__((unused)))
735{
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930736 astlpc_prerr(astlpc, "Missing support for file IO");
Jeremy Kerr92a10a62019-08-28 16:55:54 +0530737 return -1;
738}
739#endif