xref: /openbmc/libmctp/libmctp.h (revision c2def9f3)
1 /* SPDX-License-Identifier: Apache-2.0 */
2 
3 #ifndef _LIBMCTP_H
4 #define _LIBMCTP_H
5 
6 #ifdef __cplusplus
7 extern "C" {
8 #endif
9 
10 #include <stdbool.h>
11 #include <stdint.h>
12 #include <stddef.h>
13 
14 typedef uint8_t mctp_eid_t;
15 
16 /* MCTP packet definitions */
17 struct 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	(4)
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 + \
46 		(sizeof(struct mctp_hdr) + MCTP_MTU))
47 
48 struct mctp_pktbuf {
49 	unsigned char	data[MCTP_PKTBUF_SIZE];
50 	uint8_t		start, end;
51 	uint8_t		mctp_hdr_off;
52 	struct mctp_pktbuf *next;
53 };
54 
55 struct mctp_pktbuf *mctp_pktbuf_alloc(uint8_t len);
56 void mctp_pktbuf_free(struct mctp_pktbuf *pkt);
57 struct mctp_hdr *mctp_pktbuf_hdr(struct mctp_pktbuf *pkt);
58 void *mctp_pktbuf_data(struct mctp_pktbuf *pkt);
59 uint8_t mctp_pktbuf_size(struct mctp_pktbuf *pkt);
60 void *mctp_pktbuf_alloc_start(struct mctp_pktbuf *pkt, uint8_t size);
61 void *mctp_pktbuf_alloc_end(struct mctp_pktbuf *pkt, uint8_t size);
62 int mctp_pktbuf_push(struct mctp_pktbuf *pkt, void *data, uint8_t len);
63 
64 /* MCTP core */
65 struct mctp;
66 struct mctp_bus;
67 struct mctp_binding;
68 
69 struct mctp *mctp_init(void);
70 
71 /* Register a binding to the MCTP core, and creates a bus (populating
72  * binding->bus).
73  */
74 int mctp_register_bus(struct mctp *mctp,
75 		struct mctp_binding *binding,
76 		mctp_eid_t eid);
77 
78 typedef void (*mctp_rx_fn)(uint8_t src_eid, void *data,
79 		void *msg, size_t len);
80 
81 int mctp_set_rx_all(struct mctp *mctp, mctp_rx_fn fn, void *data);
82 
83 int mctp_message_tx(struct mctp *mctp, mctp_eid_t eid,
84 		void *msg, size_t msg_len);
85 
86 /* hardware bindings */
87 struct mctp_binding {
88 	const char	*name;
89 	uint8_t		version;
90 	struct mctp_bus	*bus;
91 	struct mctp	*mctp;
92 	int		(*tx)(struct mctp_binding *binding,
93 				struct mctp_pktbuf *pkt);
94 };
95 
96 void mctp_binding_set_tx_enabled(struct mctp_binding *binding, bool enable);
97 
98 void mctp_bus_rx(struct mctp_binding *binding, struct mctp_pktbuf *pkt);
99 
100 /* environment-specific allocation */
101 void mctp_set_alloc_ops(void *(*alloc)(size_t),
102 		void (*free)(void *),
103 		void *(realloc)(void *, size_t));
104 
105 
106 #ifdef __cplusplus
107 }
108 #endif
109 
110 #endif /* _LIBMCTP_H */
111