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