blob: a47eb352cea2d0b81c8eab3ec5fcb8edd5d09b87 [file] [log] [blame]
Jeremy Kerr4cdc2002019-02-07 16:49:12 +08001/* SPDX-License-Identifier: Apache-2.0 */
2
3#ifndef _LIBMCTP_H
4#define _LIBMCTP_H
5
Deepak Kodihallib11ad2c2019-02-28 03:42:22 -06006#ifdef __cplusplus
7extern "C" {
8#endif
9
Jeremy Kerr1cd31182019-02-27 18:01:00 +080010#include <stdbool.h>
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080011#include <stdint.h>
Jeremy Kerrf9ffd592019-03-06 09:09:38 +080012#include <stddef.h>
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080013
14typedef uint8_t mctp_eid_t;
15
16/* MCTP packet definitions */
17struct mctp_hdr {
18 uint8_t ver;
19 uint8_t dest;
20 uint8_t src;
21 uint8_t flags_seq_tag;
22};
23
24/* Definitions for flags_seq_tag field */
25#define MCTP_HDR_FLAG_SOM (1<<7)
26#define MCTP_HDR_FLAG_EOM (1<<6)
27#define MCTP_HDR_FLAG_TO (1<<3)
28#define MCTP_HDR_SEQ_SHIFT (5)
29#define MCTP_HDR_SEQ_MASK (0x3)
30#define MCTP_HDR_TAG_SHIFT (0)
31#define MCTP_HDR_TAG_MASK (0x7)
32
33/* Maximum size of *payload* data in a MCTP packet
34 * @todo: dynamic sixing based on channel implementation.
35 */
36#define MCTP_MTU 64
37
38/* packet buffers */
39
40/* Allow a little space before the MCTP header in the packet, for bindings that
41 * may add their own header
42 */
43#define MCTP_PKTBUF_BINDING_PAD 2
44
45#define MCTP_PKTBUF_SIZE (MCTP_PKTBUF_BINDING_PAD + \
Ed Tanous9f101222019-02-11 11:22:03 -080046 (sizeof(struct mctp_hdr) + MCTP_MTU))
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080047
48struct mctp_pktbuf {
49 unsigned char data[MCTP_PKTBUF_SIZE];
50 uint8_t start, end;
51 uint8_t mctp_hdr_off;
Jeremy Kerre16eaab2019-02-08 09:30:10 +080052 struct mctp_pktbuf *next;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080053};
54
55struct mctp_pktbuf *mctp_pktbuf_alloc(uint8_t len);
56void mctp_pktbuf_free(struct mctp_pktbuf *pkt);
57struct mctp_hdr *mctp_pktbuf_hdr(struct mctp_pktbuf *pkt);
58void *mctp_pktbuf_data(struct mctp_pktbuf *pkt);
59uint8_t mctp_pktbuf_size(struct mctp_pktbuf *pkt);
60void *mctp_pktbuf_alloc_start(struct mctp_pktbuf *pkt, uint8_t size);
61void *mctp_pktbuf_alloc_end(struct mctp_pktbuf *pkt, uint8_t size);
62int mctp_pktbuf_push(struct mctp_pktbuf *pkt, void *data, uint8_t len);
63
64/* MCTP core */
65struct mctp;
Jeremy Kerr7520cec2019-03-01 07:13:18 +080066struct mctp_bus;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080067struct mctp_binding;
68
69struct mctp *mctp_init(void);
70
Jeremy Kerr7520cec2019-03-01 07:13:18 +080071/* Register a binding to the MCTP core, and creates a bus (populating
72 * binding->bus).
73 */
74int mctp_register_bus(struct mctp *mctp,
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080075 struct mctp_binding *binding,
76 mctp_eid_t eid);
77
78typedef void (*mctp_rx_fn)(uint8_t src_eid, void *data,
79 void *msg, size_t len);
80
81int mctp_set_rx_all(struct mctp *mctp, mctp_rx_fn fn, void *data);
82
83int mctp_message_tx(struct mctp *mctp, mctp_eid_t eid,
84 void *msg, size_t msg_len);
85
86/* hardware bindings */
87struct mctp_binding {
88 const char *name;
89 uint8_t version;
Jeremy Kerr7520cec2019-03-01 07:13:18 +080090 struct mctp_bus *bus;
Jeremy Kerr0a00dca2019-03-01 08:01:35 +080091 struct mctp *mctp;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080092 int (*tx)(struct mctp_binding *binding,
93 struct mctp_pktbuf *pkt);
94};
95
Jeremy Kerr1cd31182019-02-27 18:01:00 +080096void mctp_binding_set_tx_enabled(struct mctp_binding *binding, bool enable);
97
Jeremy Kerr0a00dca2019-03-01 08:01:35 +080098void mctp_bus_rx(struct mctp_binding *binding, struct mctp_pktbuf *pkt);
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080099
100/* environment-specific allocation */
101void mctp_set_alloc_ops(void *(*alloc)(size_t),
102 void (*free)(void *),
103 void *(realloc)(void *, size_t));
104
105
Deepak Kodihallib11ad2c2019-02-28 03:42:22 -0600106#ifdef __cplusplus
107}
108#endif
109
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800110#endif /* _LIBMCTP_H */