xref: /openbmc/libmctp/libmctp.h (revision f2988977)
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_TO_SHIFT  (3)
34 #define MCTP_HDR_TO_MASK   (1)
35 #define MCTP_HDR_SEQ_SHIFT (4)
36 #define MCTP_HDR_SEQ_MASK  (0x3)
37 #define MCTP_HDR_TAG_SHIFT (0)
38 #define MCTP_HDR_TAG_MASK  (0x7)
39 
40 #define MCTP_MESSAGE_TO_SRC	      true
41 #define MCTP_MESSAGE_TO_DST	      false
42 #define MCTP_MESSAGE_CAPTURE_OUTGOING true
43 #define MCTP_MESSAGE_CAPTURE_INCOMING false
44 
45 /* Baseline Transmission Unit and packet size */
46 #define MCTP_BTU	       64
47 #define MCTP_PACKET_SIZE(unit) ((unit) + sizeof(struct mctp_hdr))
48 #define MCTP_BODY_SIZE(unit)   ((unit) - sizeof(struct mctp_hdr))
49 
50 /* packet buffers */
51 
52 struct mctp_pktbuf {
53 	size_t start, end, size;
54 	size_t mctp_hdr_off;
55 	struct mctp_pktbuf *next;
56 	unsigned char data[];
57 };
58 
59 struct mctp_binding;
60 
61 struct mctp_pktbuf *mctp_pktbuf_alloc(struct mctp_binding *hw, size_t len);
62 void mctp_pktbuf_free(struct mctp_pktbuf *pkt);
63 struct mctp_hdr *mctp_pktbuf_hdr(struct mctp_pktbuf *pkt);
64 void *mctp_pktbuf_data(struct mctp_pktbuf *pkt);
65 size_t mctp_pktbuf_size(struct mctp_pktbuf *pkt);
66 void *mctp_pktbuf_alloc_start(struct mctp_pktbuf *pkt, size_t size);
67 void *mctp_pktbuf_alloc_end(struct mctp_pktbuf *pkt, size_t size);
68 int mctp_pktbuf_push(struct mctp_pktbuf *pkt, void *data, size_t len);
69 void *mctp_pktbuf_pop(struct mctp_pktbuf *pkt, size_t len);
70 
71 /* MCTP core */
72 struct mctp;
73 struct mctp_bus;
74 
75 struct mctp *mctp_init(void);
76 void mctp_set_max_message_size(struct mctp *mctp, size_t message_size);
77 typedef void (*mctp_capture_fn)(struct mctp_pktbuf *pkt, bool outgoing,
78 				void *user);
79 void mctp_set_capture_handler(struct mctp *mctp, mctp_capture_fn fn,
80 			      void *user);
81 void mctp_destroy(struct mctp *mctp);
82 
83 /* Register a binding to the MCTP core, and creates a bus (populating
84  * binding->bus).
85  *
86  * If this function is called, the MCTP stack is initialised as an 'endpoint',
87  * and will deliver local packets to a RX callback - see `mctp_set_rx_all()`
88  * below.
89  */
90 int mctp_register_bus(struct mctp *mctp, struct mctp_binding *binding,
91 		      mctp_eid_t eid);
92 
93 void mctp_unregister_bus(struct mctp *mctp, struct mctp_binding *binding);
94 
95 /* Create a simple bidirectional bridge between busses.
96  *
97  * In this mode, the MCTP stack is initialised as a bridge. There is no EID
98  * defined, so no packets are considered local. Instead, all messages from one
99  * binding are forwarded to the other.
100  */
101 int mctp_bridge_busses(struct mctp *mctp, struct mctp_binding *b1,
102 		       struct mctp_binding *b2);
103 
104 typedef void (*mctp_rx_fn)(uint8_t src_eid, bool tag_owner, uint8_t msg_tag,
105 			   void *data, void *msg, size_t len);
106 
107 int mctp_set_rx_all(struct mctp *mctp, mctp_rx_fn fn, void *data);
108 
109 int mctp_message_tx(struct mctp *mctp, mctp_eid_t eid, bool tag_owner,
110 		    uint8_t msg_tag, void *msg, size_t msg_len);
111 
112 /* hardware bindings */
113 
114 /**
115  * @tx: Binding function to transmit one packet on the interface
116  *      Return:
117  *      * 0 - Success, pktbuf can be released
118  *	* -EMSGSIZE - Packet exceeds binding MTU, pktbuf must be dropped
119  *	* -EBUSY - Packet unable to be transmitted, pktbuf must be retained
120  */
121 struct mctp_binding {
122 	const char *name;
123 	uint8_t version;
124 	struct mctp_bus *bus;
125 	struct mctp *mctp;
126 	size_t pkt_size;
127 	size_t pkt_header;
128 	size_t pkt_trailer;
129 	int (*start)(struct mctp_binding *binding);
130 	int (*tx)(struct mctp_binding *binding, struct mctp_pktbuf *pkt);
131 	mctp_rx_fn control_rx;
132 	void *control_rx_data;
133 };
134 
135 void mctp_binding_set_tx_enabled(struct mctp_binding *binding, bool enable);
136 
137 /*
138  * Receive a packet from binding to core. Takes ownership of pkt, free()-ing it
139  * after use.
140  */
141 void mctp_bus_rx(struct mctp_binding *binding, struct mctp_pktbuf *pkt);
142 
143 /* environment-specific allocation */
144 void mctp_set_alloc_ops(void *(*alloc)(size_t), void (*free)(void *),
145 			void *(realloc)(void *, size_t));
146 
147 /* environment-specific logging */
148 
149 void mctp_set_log_stdio(int level);
150 void mctp_set_log_syslog(void);
151 void mctp_set_log_custom(void (*fn)(int, const char *, va_list));
152 
153 /* these should match the syslog-standard LOG_* definitions, for
154  * easier use with syslog */
155 #define MCTP_LOG_ERR	 3
156 #define MCTP_LOG_WARNING 4
157 #define MCTP_LOG_NOTICE	 5
158 #define MCTP_LOG_INFO	 6
159 #define MCTP_LOG_DEBUG	 7
160 
161 #ifdef __cplusplus
162 }
163 #endif
164 
165 #endif /* _LIBMCTP_H */
166