blob: 5400788d162b049ded928ef5261d3a502fb36bef [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
6#include <stdint.h>
7
8typedef uint8_t mctp_eid_t;
9
10/* MCTP packet definitions */
11struct mctp_hdr {
12 uint8_t ver;
13 uint8_t dest;
14 uint8_t src;
15 uint8_t flags_seq_tag;
16};
17
18/* Definitions for flags_seq_tag field */
19#define MCTP_HDR_FLAG_SOM (1<<7)
20#define MCTP_HDR_FLAG_EOM (1<<6)
21#define MCTP_HDR_FLAG_TO (1<<3)
22#define MCTP_HDR_SEQ_SHIFT (5)
23#define MCTP_HDR_SEQ_MASK (0x3)
24#define MCTP_HDR_TAG_SHIFT (0)
25#define MCTP_HDR_TAG_MASK (0x7)
26
27/* Maximum size of *payload* data in a MCTP packet
28 * @todo: dynamic sixing based on channel implementation.
29 */
30#define MCTP_MTU 64
31
32/* packet buffers */
33
34/* Allow a little space before the MCTP header in the packet, for bindings that
35 * may add their own header
36 */
37#define MCTP_PKTBUF_BINDING_PAD 2
38
39#define MCTP_PKTBUF_SIZE (MCTP_PKTBUF_BINDING_PAD + \
40 (sizeof(struct mctp_hdr) + MCTP_MTU)
41
42struct mctp_pktbuf {
43 unsigned char data[MCTP_PKTBUF_SIZE];
44 uint8_t start, end;
45 uint8_t mctp_hdr_off;
46};
47
48struct mctp_pktbuf *mctp_pktbuf_alloc(uint8_t len);
49void mctp_pktbuf_free(struct mctp_pktbuf *pkt);
50struct mctp_hdr *mctp_pktbuf_hdr(struct mctp_pktbuf *pkt);
51void *mctp_pktbuf_data(struct mctp_pktbuf *pkt);
52uint8_t mctp_pktbuf_size(struct mctp_pktbuf *pkt);
53void *mctp_pktbuf_alloc_start(struct mctp_pktbuf *pkt, uint8_t size);
54void *mctp_pktbuf_alloc_end(struct mctp_pktbuf *pkt, uint8_t size);
55int mctp_pktbuf_push(struct mctp_pktbuf *pkt, void *data, uint8_t len);
56
57/* MCTP core */
58struct mctp;
59struct mctp_binding;
60
61struct mctp *mctp_init(void);
62
63unsigned long mctp_register_bus(struct mctp *mctp,
64 struct mctp_binding *binding,
65 mctp_eid_t eid);
66
67typedef void (*mctp_rx_fn)(uint8_t src_eid, void *data,
68 void *msg, size_t len);
69
70int mctp_set_rx_all(struct mctp *mctp, mctp_rx_fn fn, void *data);
71
72int mctp_message_tx(struct mctp *mctp, mctp_eid_t eid,
73 void *msg, size_t msg_len);
74
75/* hardware bindings */
76struct mctp_binding {
77 const char *name;
78 uint8_t version;
79 int (*tx)(struct mctp_binding *binding,
80 struct mctp_pktbuf *pkt);
81};
82
83void mctp_bus_rx(struct mctp *mctp, unsigned long bus_id,
84 struct mctp_pktbuf *pkt);
85
86/* environment-specific allocation */
87void mctp_set_alloc_ops(void *(*alloc)(size_t),
88 void (*free)(void *),
89 void *(realloc)(void *, size_t));
90
91
92#endif /* _LIBMCTP_H */