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