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