blob: aec1b3934f19ff36432e392e2575d0170fa1029c [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
Jeremy Kerr672c8852019-03-01 12:18:07 +080054 union {
55 void *lpc_map;
56 struct mctp_lpcmap_hdr *lpc_hdr;
57 };
Andrew Jeffery7cd72f12020-05-12 20:27:59 +093058 struct mctp_astlpc_layout layout;
59
60 uint8_t mode;
Jeremy Kerrbc53d352019-08-28 14:26:14 +053061
62 /* direct ops data */
63 struct mctp_binding_astlpc_ops ops;
64 void *ops_data;
65 struct mctp_lpcmap_hdr *priv_hdr;
66
67 /* fileio ops data */
68 void *lpc_map_base;
Jeremy Kerr672c8852019-03-01 12:18:07 +080069 int kcs_fd;
70 uint8_t kcs_status;
71
72 bool running;
Jeremy Kerr672c8852019-03-01 12:18:07 +080073};
74
Jeremy Kerr672c8852019-03-01 12:18:07 +080075#define binding_to_astlpc(b) \
76 container_of(b, struct mctp_binding_astlpc, binding)
77
Andrew Jeffery9101a2a2020-05-22 16:08:03 +093078#define astlpc_prlog(ctx, lvl, fmt, ...) \
79 do { \
80 bool __bmc = ((ctx)->mode == MCTP_BINDING_ASTLPC_MODE_BMC); \
81 mctp_prlog(lvl, pr_fmt("%s: " fmt), __bmc ? "bmc" : "host", \
82 ##__VA_ARGS__); \
83 } while (0)
84
85#define astlpc_prerr(ctx, fmt, ...) \
86 astlpc_prlog(ctx, MCTP_LOG_ERR, fmt, ##__VA_ARGS__)
87#define astlpc_prwarn(ctx, fmt, ...) \
88 astlpc_prlog(ctx, MCTP_LOG_WARNING, fmt, ##__VA_ARGS__)
89#define astlpc_prinfo(ctx, fmt, ...) \
90 astlpc_prlog(ctx, MCTP_LOG_INFO, fmt, ##__VA_ARGS__)
91#define astlpc_prdebug(ctx, fmt, ...) \
92 astlpc_prlog(ctx, MCTP_LOG_DEBUG, fmt, ##__VA_ARGS__)
93
Andrew Jeffery7cd72f12020-05-12 20:27:59 +093094/* clang-format off */
95#define ASTLPC_MCTP_MAGIC 0x4d435450
96#define ASTLPC_VER_MIN 1
97#define ASTLPC_VER_CUR 1
98
99#define ASTLPC_BODY_SIZE(sz) ((sz) - 4)
100/* clang-format on */
Jeremy Kerr672c8852019-03-01 12:18:07 +0800101
102struct mctp_lpcmap_hdr {
103 uint32_t magic;
104
105 uint16_t bmc_ver_min;
106 uint16_t bmc_ver_cur;
107 uint16_t host_ver_min;
108 uint16_t host_ver_cur;
109 uint16_t negotiated_ver;
110 uint16_t pad0;
111
112 uint32_t rx_offset;
113 uint32_t rx_size;
114 uint32_t tx_offset;
115 uint32_t tx_size;
116} __attribute__((packed));
117
118/* layout of TX/RX areas */
119static const uint32_t rx_offset = 0x100;
120static const uint32_t rx_size = 0x100;
121static const uint32_t tx_offset = 0x200;
122static const uint32_t tx_size = 0x100;
123
Jeremy Kerr672c8852019-03-01 12:18:07 +0800124#define LPC_WIN_SIZE (1 * 1024 * 1024)
125
126enum {
127 KCS_REG_DATA = 0,
128 KCS_REG_STATUS = 1,
129};
130
131#define KCS_STATUS_BMC_READY 0x80
132#define KCS_STATUS_CHANNEL_ACTIVE 0x40
133#define KCS_STATUS_IBF 0x02
134#define KCS_STATUS_OBF 0x01
135
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530136static bool lpc_direct(struct mctp_binding_astlpc *astlpc)
137{
138 return astlpc->lpc_map != NULL;
139}
140
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930141static int mctp_astlpc_init_bmc(struct mctp_binding_astlpc *astlpc)
142{
143 struct mctp_lpcmap_hdr *hdr;
144 uint8_t status;
145 int rc;
146
147 /* Flip the buffers as the names are defined in terms of the host */
148 astlpc->layout.rx.offset = tx_offset;
149 astlpc->layout.rx.size = tx_size;
150 astlpc->layout.tx.offset = rx_offset;
151 astlpc->layout.tx.size = rx_size;
152
153 if (lpc_direct(astlpc))
154 hdr = astlpc->lpc_hdr;
155 else
156 hdr = astlpc->priv_hdr;
157
158 hdr->magic = htobe32(ASTLPC_MCTP_MAGIC);
159 hdr->bmc_ver_min = htobe16(ASTLPC_VER_MIN);
160 hdr->bmc_ver_cur = htobe16(ASTLPC_VER_CUR);
161
162 /* Flip the buffers back as we're now describing the host's
163 * configuration to the host */
164 hdr->rx_offset = htobe32(astlpc->layout.tx.offset);
165 hdr->rx_size = htobe32(astlpc->layout.tx.size);
166 hdr->tx_offset = htobe32(astlpc->layout.rx.offset);
167 hdr->tx_size = htobe32(astlpc->layout.rx.size);
168
169 if (!lpc_direct(astlpc))
170 astlpc->ops.lpc_write(astlpc->ops_data, hdr, 0, sizeof(*hdr));
171
172 /* set status indicating that the BMC is now active */
173 status = KCS_STATUS_BMC_READY | KCS_STATUS_OBF;
174 /* XXX: Should we be calling mctp_astlpc_kcs_set_status() instead? */
175 rc = astlpc->ops.kcs_write(astlpc->ops_data, MCTP_ASTLPC_KCS_REG_STATUS,
176 status);
177 if (rc) {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930178 astlpc_prwarn(astlpc, "KCS write failed");
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930179 }
180
181 return rc;
182}
183
184static int mctp_binding_astlpc_start_bmc(struct mctp_binding *b)
185{
186 struct mctp_binding_astlpc *astlpc =
187 container_of(b, struct mctp_binding_astlpc, binding);
188
189 return mctp_astlpc_init_bmc(astlpc);
190}
191
192static int mctp_astlpc_init_host(struct mctp_binding_astlpc *astlpc)
193{
194 struct mctp_lpcmap_hdr *hdr;
195 uint8_t status;
196 int rc;
197
198 rc = astlpc->ops.kcs_read(astlpc->ops_data, MCTP_ASTLPC_KCS_REG_STATUS,
199 &status);
200 if (rc) {
201 mctp_prwarn("KCS status read failed");
202 return rc;
203 }
204
205 astlpc->kcs_status = status;
206
207 if (!(status & KCS_STATUS_BMC_READY))
208 return -EHOSTDOWN;
209
210 if (lpc_direct(astlpc)) {
211 hdr = astlpc->lpc_hdr;
212 } else {
213 hdr = astlpc->priv_hdr;
214 astlpc->ops.lpc_read(astlpc->ops_data, hdr, 0, sizeof(*hdr));
215 }
216
217 astlpc->layout.rx.offset = be32toh(hdr->rx_offset);
218 astlpc->layout.rx.size = be32toh(hdr->rx_size);
219 astlpc->layout.tx.offset = be32toh(hdr->tx_offset);
220 astlpc->layout.tx.size = be32toh(hdr->tx_size);
221
222 hdr->host_ver_min = htobe16(ASTLPC_VER_MIN);
223 if (!lpc_direct(astlpc))
224 astlpc->ops.lpc_write(astlpc->ops_data, &hdr->host_ver_min,
225 offsetof(struct mctp_lpcmap_hdr,
226 host_ver_min),
227 sizeof(hdr->host_ver_min));
228
229 hdr->host_ver_cur = htobe16(ASTLPC_VER_CUR);
230 if (!lpc_direct(astlpc))
231 astlpc->ops.lpc_write(astlpc->ops_data, &hdr->host_ver_cur,
232 offsetof(struct mctp_lpcmap_hdr,
233 host_ver_cur),
234 sizeof(hdr->host_ver_cur));
235
236 /* Send channel init command */
237 rc = astlpc->ops.kcs_write(astlpc->ops_data, MCTP_ASTLPC_KCS_REG_DATA,
238 0x0);
239 if (rc) {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930240 astlpc_prwarn(astlpc, "KCS write failed");
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930241 }
242
243 return rc;
244}
245
246static int mctp_binding_astlpc_start_host(struct mctp_binding *b)
247{
248 struct mctp_binding_astlpc *astlpc =
249 container_of(b, struct mctp_binding_astlpc, binding);
250
251 return mctp_astlpc_init_host(astlpc);
252}
253
254static bool __mctp_astlpc_kcs_ready(struct mctp_binding_astlpc *astlpc,
255 uint8_t status, bool is_write)
256{
257 bool is_bmc;
258 bool ready_state;
259 uint8_t flag;
260
261 is_bmc = (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_BMC);
262 flag = (is_bmc ^ is_write) ? KCS_STATUS_IBF : KCS_STATUS_OBF;
263 ready_state = is_write ? 0 : 1;
264
265 return !!(status & flag) == ready_state;
266}
267
268static inline bool
269mctp_astlpc_kcs_read_ready(struct mctp_binding_astlpc *astlpc, uint8_t status)
270{
271 return __mctp_astlpc_kcs_ready(astlpc, status, false);
272}
273
274static inline bool
275mctp_astlpc_kcs_write_ready(struct mctp_binding_astlpc *astlpc, uint8_t status)
276{
277 return __mctp_astlpc_kcs_ready(astlpc, status, true);
278}
279
Jeremy Kerr672c8852019-03-01 12:18:07 +0800280static int mctp_astlpc_kcs_set_status(struct mctp_binding_astlpc *astlpc,
281 uint8_t status)
282{
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530283 uint8_t data;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800284 int rc;
285
Jeremy Kerr1a4b55a2019-06-24 14:22:39 +0800286 /* Since we're setting the status register, we want the other endpoint
287 * to be interrupted. However, some hardware may only raise a host-side
288 * interrupt on an ODR event.
289 * So, write a dummy value of 0xff to ODR, which will ensure that an
290 * interrupt is triggered, and can be ignored by the host.
291 */
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530292 data = 0xff;
293 status |= KCS_STATUS_OBF;
Jeremy Kerr1a4b55a2019-06-24 14:22:39 +0800294
Andrew Jefferyc84fd562020-01-30 21:18:16 +1030295 rc = astlpc->ops.kcs_write(astlpc->ops_data, MCTP_ASTLPC_KCS_REG_STATUS,
296 status);
297 if (rc) {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930298 astlpc_prwarn(astlpc, "KCS status write failed");
Andrew Jefferyc84fd562020-01-30 21:18:16 +1030299 return -1;
300 }
301
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530302 rc = astlpc->ops.kcs_write(astlpc->ops_data, MCTP_ASTLPC_KCS_REG_DATA,
303 data);
304 if (rc) {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930305 astlpc_prwarn(astlpc, "KCS dummy data write failed");
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530306 return -1;
307 }
308
Jeremy Kerr672c8852019-03-01 12:18:07 +0800309 return 0;
310}
311
312static int mctp_astlpc_kcs_send(struct mctp_binding_astlpc *astlpc,
313 uint8_t data)
314{
315 uint8_t status;
316 int rc;
317
318 for (;;) {
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530319 rc = astlpc->ops.kcs_read(astlpc->ops_data,
320 MCTP_ASTLPC_KCS_REG_STATUS, &status);
Andrew Jeffery1b27fe82020-01-24 16:05:11 +1030321 if (rc) {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930322 astlpc_prwarn(astlpc, "KCS status read failed");
Jeremy Kerr672c8852019-03-01 12:18:07 +0800323 return -1;
324 }
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930325 if (mctp_astlpc_kcs_write_ready(astlpc, status))
Jeremy Kerr672c8852019-03-01 12:18:07 +0800326 break;
327 /* todo: timeout */
328 }
329
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530330 rc = astlpc->ops.kcs_write(astlpc->ops_data, MCTP_ASTLPC_KCS_REG_DATA,
331 data);
332 if (rc) {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930333 astlpc_prwarn(astlpc, "KCS data write failed");
Jeremy Kerr672c8852019-03-01 12:18:07 +0800334 return -1;
335 }
336
337 return 0;
338}
339
340static int mctp_binding_astlpc_tx(struct mctp_binding *b,
341 struct mctp_pktbuf *pkt)
342{
343 struct mctp_binding_astlpc *astlpc = binding_to_astlpc(b);
344 uint32_t len;
Andrew Jefferyedfe3832020-02-06 11:52:11 +1030345 struct mctp_hdr *hdr;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800346
Andrew Jefferyedfe3832020-02-06 11:52:11 +1030347 hdr = mctp_pktbuf_hdr(pkt);
Jeremy Kerr672c8852019-03-01 12:18:07 +0800348 len = mctp_pktbuf_size(pkt);
Andrew Jefferyedfe3832020-02-06 11:52:11 +1030349
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930350 astlpc_prdebug(astlpc,
351 "%s: Transmitting %" PRIu32
352 "-byte packet (%hhu, %hhu, 0x%hhx)",
353 __func__, len, hdr->src, hdr->dest, hdr->flags_seq_tag);
Andrew Jefferyedfe3832020-02-06 11:52:11 +1030354
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930355 if (len > ASTLPC_BODY_SIZE(astlpc->layout.tx.size)) {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930356 astlpc_prwarn(astlpc, "invalid TX len 0x%x", len);
Jeremy Kerr672c8852019-03-01 12:18:07 +0800357 return -1;
358 }
359
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530360 if (lpc_direct(astlpc)) {
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930361 *(uint32_t *)(astlpc->lpc_map + astlpc->layout.tx.offset) =
362 htobe32(len);
363 memcpy(astlpc->lpc_map + astlpc->layout.tx.offset + 4,
364 mctp_pktbuf_hdr(pkt), len);
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530365 } else {
366 uint32_t tmp = htobe32(len);
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930367 astlpc->ops.lpc_write(astlpc->ops_data, &tmp,
368 astlpc->layout.tx.offset, sizeof(tmp));
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530369 astlpc->ops.lpc_write(astlpc->ops_data, mctp_pktbuf_hdr(pkt),
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930370 astlpc->layout.tx.offset + 4, len);
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530371 }
Jeremy Kerr672c8852019-03-01 12:18:07 +0800372
373 mctp_binding_set_tx_enabled(b, false);
374
375 mctp_astlpc_kcs_send(astlpc, 0x1);
376 return 0;
377}
378
379static void mctp_astlpc_init_channel(struct mctp_binding_astlpc *astlpc)
380{
381 /* todo: actual version negotiation */
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530382 if (lpc_direct(astlpc)) {
383 astlpc->lpc_hdr->negotiated_ver = htobe16(1);
384 } else {
385 uint16_t ver = htobe16(1);
386 astlpc->ops.lpc_write(astlpc->ops_data, &ver,
387 offsetof(struct mctp_lpcmap_hdr,
388 negotiated_ver),
389 sizeof(ver));
390 }
Jeremy Kerr672c8852019-03-01 12:18:07 +0800391 mctp_astlpc_kcs_set_status(astlpc,
392 KCS_STATUS_BMC_READY | KCS_STATUS_CHANNEL_ACTIVE |
393 KCS_STATUS_OBF);
394
395 mctp_binding_set_tx_enabled(&astlpc->binding, true);
396}
397
398static void mctp_astlpc_rx_start(struct mctp_binding_astlpc *astlpc)
399{
400 struct mctp_pktbuf *pkt;
401 uint32_t len;
402
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530403 if (lpc_direct(astlpc)) {
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930404 len = *(uint32_t *)(astlpc->lpc_map + astlpc->layout.rx.offset);
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530405 } else {
406 astlpc->ops.lpc_read(astlpc->ops_data, &len,
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930407 astlpc->layout.rx.offset, sizeof(len));
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530408 }
409 len = be32toh(len);
410
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930411 if (len > ASTLPC_BODY_SIZE(astlpc->layout.rx.size)) {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930412 astlpc_prwarn(astlpc, "invalid RX len 0x%x", len);
Jeremy Kerr672c8852019-03-01 12:18:07 +0800413 return;
414 }
415
Andrew Jefferyb93b6112020-06-05 14:13:44 +0930416 assert(astlpc->binding.pkt_size >= 0);
417 if (len > (uint32_t)astlpc->binding.pkt_size) {
Jeremy Kerr672c8852019-03-01 12:18:07 +0800418 mctp_prwarn("invalid RX len 0x%x", len);
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930419 astlpc_prwarn(astlpc, "invalid RX len 0x%x", len);
Jeremy Kerr672c8852019-03-01 12:18:07 +0800420 return;
421 }
422
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +0800423 pkt = mctp_pktbuf_alloc(&astlpc->binding, len);
Jeremy Kerr672c8852019-03-01 12:18:07 +0800424 if (!pkt)
425 goto out_complete;
426
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530427 if (lpc_direct(astlpc)) {
428 memcpy(mctp_pktbuf_hdr(pkt),
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930429 astlpc->lpc_map + astlpc->layout.rx.offset + 4, len);
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530430 } else {
431 astlpc->ops.lpc_read(astlpc->ops_data, mctp_pktbuf_hdr(pkt),
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930432 astlpc->layout.rx.offset + 4, len);
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530433 }
Jeremy Kerr672c8852019-03-01 12:18:07 +0800434
435 mctp_bus_rx(&astlpc->binding, pkt);
436
437out_complete:
438 mctp_astlpc_kcs_send(astlpc, 0x2);
439}
440
441static void mctp_astlpc_tx_complete(struct mctp_binding_astlpc *astlpc)
442{
443 mctp_binding_set_tx_enabled(&astlpc->binding, true);
444}
445
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930446static int mctp_astlpc_update_channel(struct mctp_binding_astlpc *astlpc,
447 uint8_t status)
448{
449 uint8_t updated;
450 int rc = 0;
451
452 assert(astlpc->mode == MCTP_BINDING_ASTLPC_MODE_HOST);
453
454 updated = astlpc->kcs_status ^ status;
455
456 if (updated & KCS_STATUS_BMC_READY) {
457 if (!(status & KCS_STATUS_BMC_READY))
458 mctp_binding_set_tx_enabled(&astlpc->binding, false);
459 }
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 */
508 if (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_BMC)
509 return 0;
510
511 return mctp_astlpc_update_channel(astlpc, status);
Jeremy Kerr672c8852019-03-01 12:18:07 +0800512 default:
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930513 astlpc_prwarn(astlpc, "unknown message 0x%x", data);
Jeremy Kerr672c8852019-03-01 12:18:07 +0800514 }
515 return 0;
516}
517
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530518/* allocate and basic initialisation */
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930519static struct mctp_binding_astlpc *__mctp_astlpc_init(uint8_t mode,
520 uint32_t mtu)
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530521{
522 struct mctp_binding_astlpc *astlpc;
523
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930524 assert((mode == MCTP_BINDING_ASTLPC_MODE_BMC) ||
525 (mode == MCTP_BINDING_ASTLPC_MODE_HOST));
526
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530527 astlpc = __mctp_alloc(sizeof(*astlpc));
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930528 if (!astlpc)
529 return NULL;
530
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530531 memset(astlpc, 0, sizeof(*astlpc));
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930532 astlpc->mode = mode;
533 astlpc->lpc_map = NULL;
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530534 astlpc->binding.name = "astlpc";
535 astlpc->binding.version = 1;
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930536 astlpc->binding.pkt_size = MCTP_PACKET_SIZE(mtu);
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530537 astlpc->binding.pkt_pad = 0;
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930538 astlpc->binding.tx = mctp_binding_astlpc_tx;
539 if (mode == MCTP_BINDING_ASTLPC_MODE_BMC)
540 astlpc->binding.start = mctp_binding_astlpc_start_bmc;
541 else if (mode == MCTP_BINDING_ASTLPC_MODE_HOST)
542 astlpc->binding.start = mctp_binding_astlpc_start_host;
543 else {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930544 astlpc_prerr(astlpc, "%s: Invalid mode: %d\n", __func__, mode);
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930545 __mctp_free(astlpc);
546 return NULL;
547 }
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530548
549 return astlpc;
550}
551
Jeremy Kerr3b36d172019-09-04 11:56:09 +0800552struct mctp_binding *mctp_binding_astlpc_core(struct mctp_binding_astlpc *b)
553{
554 return &b->binding;
555}
556
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930557struct mctp_binding_astlpc *
558mctp_astlpc_init(uint8_t mode, uint32_t mtu, void *lpc_map,
559 const struct mctp_binding_astlpc_ops *ops, void *ops_data)
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530560{
561 struct mctp_binding_astlpc *astlpc;
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530562
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930563 if (!(mode == MCTP_BINDING_ASTLPC_MODE_BMC ||
564 mode == MCTP_BINDING_ASTLPC_MODE_HOST)) {
565 mctp_prerr("Unknown binding mode: %u", mode);
566 return NULL;
567 }
568
569 if (mtu != MCTP_BTU) {
570 mctp_prwarn("Unable to negotiate the MTU, using %u instead",
571 MCTP_BTU);
572 mtu = MCTP_BTU;
573 }
574
575 astlpc = __mctp_astlpc_init(mode, mtu);
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530576 if (!astlpc)
577 return NULL;
578
579 memcpy(&astlpc->ops, ops, sizeof(astlpc->ops));
580 astlpc->ops_data = ops_data;
581 astlpc->lpc_map = lpc_map;
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930582 astlpc->mode = mode;
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530583
584 /* In indirect mode, we keep a separate buffer of header data.
585 * We need to sync this through the lpc_read/lpc_write ops.
586 */
587 if (!astlpc->lpc_map)
588 astlpc->priv_hdr = __mctp_alloc(sizeof(*astlpc->priv_hdr));
589
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530590 return astlpc;
591}
592
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930593struct mctp_binding_astlpc *
594mctp_astlpc_init_ops(const struct mctp_binding_astlpc_ops *ops, void *ops_data,
595 void *lpc_map)
596{
597 return mctp_astlpc_init(MCTP_BINDING_ASTLPC_MODE_BMC, MCTP_BTU, lpc_map,
598 ops, ops_data);
599}
600
Andrew Jeffery4663f672020-03-10 23:36:24 +1030601void mctp_astlpc_destroy(struct mctp_binding_astlpc *astlpc)
602{
603 if (astlpc->priv_hdr)
604 __mctp_free(astlpc->priv_hdr);
605 __mctp_free(astlpc);
606}
607
Jeremy Kerrb214c642019-11-27 11:34:00 +0800608#ifdef MCTP_HAVE_FILEIO
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530609static int mctp_astlpc_init_fileio_lpc(struct mctp_binding_astlpc *astlpc)
Jeremy Kerr672c8852019-03-01 12:18:07 +0800610{
611 struct aspeed_lpc_ctrl_mapping map = {
612 .window_type = ASPEED_LPC_CTRL_WINDOW_MEMORY,
613 .window_id = 0, /* There's only one */
614 .flags = 0,
615 .addr = 0,
616 .offset = 0,
617 .size = 0
618 };
619 int fd, rc;
620
621 fd = open(lpc_path, O_RDWR | O_SYNC);
622 if (fd < 0) {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930623 astlpc_prwarn(astlpc, "LPC open (%s) failed", lpc_path);
Jeremy Kerr672c8852019-03-01 12:18:07 +0800624 return -1;
625 }
626
627 rc = ioctl(fd, ASPEED_LPC_CTRL_IOCTL_GET_SIZE, &map);
628 if (rc) {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930629 astlpc_prwarn(astlpc, "LPC GET_SIZE failed");
Jeremy Kerr672c8852019-03-01 12:18:07 +0800630 close(fd);
631 return -1;
632 }
633
634 astlpc->lpc_map_base = mmap(NULL, map.size, PROT_READ | PROT_WRITE,
635 MAP_SHARED, fd, 0);
636 if (astlpc->lpc_map_base == MAP_FAILED) {
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930637 astlpc_prwarn(astlpc, "LPC mmap failed");
Jeremy Kerr672c8852019-03-01 12:18:07 +0800638 rc = -1;
639 } else {
640 astlpc->lpc_map = astlpc->lpc_map_base +
641 map.size - LPC_WIN_SIZE;
642 }
643
644 close(fd);
645
646 return rc;
647}
648
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530649static int mctp_astlpc_init_fileio_kcs(struct mctp_binding_astlpc *astlpc)
Jeremy Kerr672c8852019-03-01 12:18:07 +0800650{
651 astlpc->kcs_fd = open(kcs_path, O_RDWR);
652 if (astlpc->kcs_fd < 0)
653 return -1;
654
655 return 0;
656}
657
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530658static int __mctp_astlpc_fileio_kcs_read(void *arg,
659 enum mctp_binding_astlpc_kcs_reg reg, uint8_t *val)
660{
661 struct mctp_binding_astlpc *astlpc = arg;
662 off_t offset = reg;
663 int rc;
664
665 rc = pread(astlpc->kcs_fd, val, 1, offset);
666
667 return rc == 1 ? 0 : -1;
668}
669
670static int __mctp_astlpc_fileio_kcs_write(void *arg,
671 enum mctp_binding_astlpc_kcs_reg reg, uint8_t val)
672{
673 struct mctp_binding_astlpc *astlpc = arg;
674 off_t offset = reg;
675 int rc;
676
677 rc = pwrite(astlpc->kcs_fd, &val, 1, offset);
678
679 return rc == 1 ? 0 : -1;
680}
681
682int mctp_astlpc_get_fd(struct mctp_binding_astlpc *astlpc)
683{
684 return astlpc->kcs_fd;
685}
686
687struct mctp_binding_astlpc *mctp_astlpc_init_fileio(void)
Jeremy Kerr672c8852019-03-01 12:18:07 +0800688{
689 struct mctp_binding_astlpc *astlpc;
690 int rc;
691
Andrew Jeffery7cd72f12020-05-12 20:27:59 +0930692 /*
693 * If we're doing file IO then we're very likely not running
694 * freestanding, so lets assume that we're on the BMC side
695 */
696 astlpc = __mctp_astlpc_init(MCTP_BINDING_ASTLPC_MODE_BMC, MCTP_BTU);
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530697 if (!astlpc)
698 return NULL;
Jeremy Kerr672c8852019-03-01 12:18:07 +0800699
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530700 /* Set internal operations for kcs. We use direct accesses to the lpc
701 * map area */
702 astlpc->ops.kcs_read = __mctp_astlpc_fileio_kcs_read;
703 astlpc->ops.kcs_write = __mctp_astlpc_fileio_kcs_write;
704 astlpc->ops_data = astlpc;
705
706 rc = mctp_astlpc_init_fileio_lpc(astlpc);
Jeremy Kerr672c8852019-03-01 12:18:07 +0800707 if (rc) {
708 free(astlpc);
709 return NULL;
710 }
711
Jeremy Kerrbc53d352019-08-28 14:26:14 +0530712 rc = mctp_astlpc_init_fileio_kcs(astlpc);
Jeremy Kerr672c8852019-03-01 12:18:07 +0800713 if (rc) {
714 free(astlpc);
715 return NULL;
716 }
717
Jeremy Kerr672c8852019-03-01 12:18:07 +0800718 return astlpc;
719}
Jeremy Kerr92a10a62019-08-28 16:55:54 +0530720#else
721struct mctp_binding_astlpc * __attribute__((const))
722 mctp_astlpc_init_fileio(void)
723{
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930724 astlpc_prerr(astlpc, "Missing support for file IO");
Jeremy Kerr92a10a62019-08-28 16:55:54 +0530725 return NULL;
726}
Jeremy Kerr672c8852019-03-01 12:18:07 +0800727
Jeremy Kerr92a10a62019-08-28 16:55:54 +0530728int __attribute__((const)) mctp_astlpc_get_fd(
729 struct mctp_binding_astlpc *astlpc __attribute__((unused)))
730{
Andrew Jeffery9101a2a2020-05-22 16:08:03 +0930731 astlpc_prerr(astlpc, "Missing support for file IO");
Jeremy Kerr92a10a62019-08-28 16:55:54 +0530732 return -1;
733}
734#endif