xref: /openbmc/libmctp/libmctp.h (revision 5d3d4e6d)
1 /* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */
2 
3 #ifndef _LIBMCTP_H
4 #define _LIBMCTP_H
5 
6 #ifdef __cplusplus
7 extern "C" {
8 #endif
9 
10 #include <stdarg.h>
11 #include <stdbool.h>
12 #include <stdint.h>
13 #include <stddef.h>
14 
15 typedef uint8_t mctp_eid_t;
16 
17 /* Special Endpoint ID values */
18 #define MCTP_EID_NULL 0
19 #define MCTP_EID_BROADCAST 0xff
20 
21 /* MCTP packet definitions */
22 struct mctp_hdr {
23 	uint8_t	ver;
24 	uint8_t	dest;
25 	uint8_t	src;
26 	uint8_t	flags_seq_tag;
27 };
28 
29 /* Definitions for flags_seq_tag field */
30 #define MCTP_HDR_FLAG_SOM	(1<<7)
31 #define MCTP_HDR_FLAG_EOM	(1<<6)
32 #define MCTP_HDR_FLAG_TO	(1<<3)
33 #define MCTP_HDR_SEQ_SHIFT	(4)
34 #define MCTP_HDR_SEQ_MASK	(0x3)
35 #define MCTP_HDR_TAG_SHIFT	(0)
36 #define MCTP_HDR_TAG_MASK	(0x7)
37 
38 /* Baseline Transmission Unit and packet size */
39 #define MCTP_BTU		64
40 #define MCTP_PACKET_SIZE(unit)	((unit) + sizeof(struct mctp_hdr))
41 #define MCTP_BODY_SIZE(unit)	((unit) - sizeof(struct mctp_hdr))
42 
43 /* packet buffers */
44 
45 struct mctp_pktbuf {
46 	size_t		start, end, size;
47 	size_t		mctp_hdr_off;
48 	struct mctp_pktbuf *next;
49 	unsigned char	data[];
50 };
51 
52 struct mctp_binding;
53 
54 struct mctp_pktbuf *mctp_pktbuf_alloc(struct mctp_binding *hw, size_t len);
55 void mctp_pktbuf_free(struct mctp_pktbuf *pkt);
56 struct mctp_hdr *mctp_pktbuf_hdr(struct mctp_pktbuf *pkt);
57 void *mctp_pktbuf_data(struct mctp_pktbuf *pkt);
58 size_t mctp_pktbuf_size(struct mctp_pktbuf *pkt);
59 void *mctp_pktbuf_alloc_start(struct mctp_pktbuf *pkt, size_t size);
60 void *mctp_pktbuf_alloc_end(struct mctp_pktbuf *pkt, size_t size);
61 int mctp_pktbuf_push(struct mctp_pktbuf *pkt, void *data, size_t len);
62 void *mctp_pktbuf_pop(struct mctp_pktbuf *pkt, size_t len);
63 
64 /* MCTP core */
65 struct mctp;
66 struct mctp_bus;
67 
68 struct mctp *mctp_init(void);
69 void mctp_set_max_message_size(struct mctp *mctp, size_t message_size);
70 typedef void (*mctp_capture_fn)(struct mctp_pktbuf *pkt, void *user);
71 void mctp_set_capture_handler(struct mctp *mctp, mctp_capture_fn fn, void *user);
72 void mctp_destroy(struct mctp *mctp);
73 
74 /* Register a binding to the MCTP core, and creates a bus (populating
75  * binding->bus).
76  *
77  * If this function is called, the MCTP stack is initialised as an 'endpoint',
78  * and will deliver local packets to a RX callback - see `mctp_set_rx_all()`
79  * below.
80  */
81 int mctp_register_bus(struct mctp *mctp,
82 		struct mctp_binding *binding,
83 		mctp_eid_t eid);
84 
85 /* Create a simple bidirectional bridge between busses.
86  *
87  * In this mode, the MCTP stack is initialised as a bridge. There is no EID
88  * defined, so no packets are considered local. Instead, all messages from one
89  * binding are forwarded to the other.
90  */
91 int mctp_bridge_busses(struct mctp *mctp,
92 		struct mctp_binding *b1, struct mctp_binding *b2);
93 
94 typedef void (*mctp_rx_fn)(uint8_t src_eid, void *data,
95 		void *msg, size_t len);
96 
97 int mctp_set_rx_all(struct mctp *mctp, mctp_rx_fn fn, void *data);
98 
99 int mctp_message_tx(struct mctp *mctp, mctp_eid_t eid,
100 		void *msg, size_t msg_len);
101 
102 /* hardware bindings */
103 struct mctp_binding {
104 	const char *name;
105 	uint8_t version;
106 	struct mctp_bus *bus;
107 	struct mctp *mctp;
108 	size_t pkt_size;
109 	size_t pkt_header;
110 	size_t pkt_trailer;
111 	int (*start)(struct mctp_binding *binding);
112 	int (*tx)(struct mctp_binding *binding, struct mctp_pktbuf *pkt);
113 	mctp_rx_fn control_rx;
114 	void *control_rx_data;
115 };
116 
117 void mctp_binding_set_tx_enabled(struct mctp_binding *binding, bool enable);
118 
119 /*
120  * Receive a packet from binding to core. Takes ownership of pkt, free()-ing it
121  * after use.
122  */
123 void mctp_bus_rx(struct mctp_binding *binding, struct mctp_pktbuf *pkt);
124 
125 /* environment-specific allocation */
126 void mctp_set_alloc_ops(void *(*alloc)(size_t),
127 		void (*free)(void *),
128 		void *(realloc)(void *, size_t));
129 
130 /* environment-specific logging */
131 
132 void mctp_set_log_stdio(int level);
133 void mctp_set_log_syslog(void);
134 void mctp_set_log_custom(void (*fn)(int, const char *, va_list));
135 
136 /* these should match the syslog-standard LOG_* definitions, for
137  * easier use with syslog */
138 #define MCTP_LOG_ERR		3
139 #define MCTP_LOG_WARNING	4
140 #define MCTP_LOG_NOTICE		5
141 #define MCTP_LOG_INFO		6
142 #define MCTP_LOG_DEBUG		7
143 
144 
145 #ifdef __cplusplus
146 }
147 #endif
148 
149 #endif /* _LIBMCTP_H */
150