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