xref: /openbmc/libmctp/libmctp.h (revision df15f7e9)
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 int mctp_register_bus(struct mctp *mctp,
68 		struct mctp_binding *binding,
69 		mctp_eid_t eid);
70 
71 typedef void (*mctp_rx_fn)(uint8_t src_eid, void *data,
72 		void *msg, size_t len);
73 
74 int mctp_set_rx_all(struct mctp *mctp, mctp_rx_fn fn, void *data);
75 
76 int mctp_message_tx(struct mctp *mctp, mctp_eid_t eid,
77 		void *msg, size_t msg_len);
78 
79 /* hardware bindings */
80 struct mctp_binding {
81 	const char	*name;
82 	uint8_t		version;
83 	struct mctp_bus	*bus;
84 	struct mctp	*mctp;
85 	int		pkt_size;
86 	int		pkt_pad;
87 	int		(*tx)(struct mctp_binding *binding,
88 				struct mctp_pktbuf *pkt);
89 };
90 
91 void mctp_binding_set_tx_enabled(struct mctp_binding *binding, bool enable);
92 
93 /*
94  * Receive a packet from binding to core. Takes ownership of pkt, free()-ing it
95  * after use.
96  */
97 void mctp_bus_rx(struct mctp_binding *binding, struct mctp_pktbuf *pkt);
98 
99 /* environment-specific allocation */
100 void mctp_set_alloc_ops(void *(*alloc)(size_t),
101 		void (*free)(void *),
102 		void *(realloc)(void *, size_t));
103 
104 /* environment-specific logging */
105 
106 void mctp_set_log_stdio(int level);
107 void mctp_set_log_syslog(void);
108 void mctp_set_log_custom(void (*fn)(int, const char *, va_list));
109 
110 /* these should match the syslog-standard LOG_* definitions, for
111  * easier use with syslog */
112 #define MCTP_LOG_ERR		3
113 #define MCTP_LOG_WARNING	4
114 #define MCTP_LOG_NOTICE		5
115 #define MCTP_LOG_INFO		6
116 #define MCTP_LOG_DEBUG		7
117 
118 
119 #ifdef __cplusplus
120 }
121 #endif
122 
123 #endif /* _LIBMCTP_H */
124