blob: f78132b4b0b2e437a6d19f3814948c516ff21483 [file] [log] [blame]
Jeremy Kerr3d36ee22019-05-30 11:15:37 +08001/* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */
Jeremy Kerr4cdc2002019-02-07 16:49:12 +08002
3#ifndef _LIBMCTP_H
4#define _LIBMCTP_H
5
Deepak Kodihallib11ad2c2019-02-28 03:42:22 -06006#ifdef __cplusplus
7extern "C" {
8#endif
9
Jeremy Kerrc7e764a2019-05-28 16:49:03 +080010#include <stdarg.h>
Jeremy Kerr1cd31182019-02-27 18:01:00 +080011#include <stdbool.h>
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080012#include <stdint.h>
Jeremy Kerrf9ffd592019-03-06 09:09:38 +080013#include <stddef.h>
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080014
15typedef uint8_t mctp_eid_t;
16
Wiktor Gołgowskiba6727e2020-03-13 18:25:01 +010017/* Special Endpoint ID values */
Patrick Williamsa721c2d2022-12-04 14:30:26 -060018#define MCTP_EID_NULL 0
Wiktor Gołgowskiba6727e2020-03-13 18:25:01 +010019#define MCTP_EID_BROADCAST 0xff
20
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080021/* MCTP packet definitions */
22struct mctp_hdr {
Patrick Williamsa721c2d2022-12-04 14:30:26 -060023 uint8_t ver;
24 uint8_t dest;
25 uint8_t src;
26 uint8_t flags_seq_tag;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080027};
28
29/* Definitions for flags_seq_tag field */
Sumanth Bhatf39c3852022-01-10 17:04:10 +053030#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
Rashmica Guptaf2988972022-11-09 12:26:44 +110040#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
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080044
Andrew Jeffery73c268e2020-01-30 10:16:09 +103045/* Baseline Transmission Unit and packet size */
Patrick Williamsa721c2d2022-12-04 14:30:26 -060046#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))
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080049
50/* packet buffers */
51
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080052struct mctp_pktbuf {
Patrick Williamsa721c2d2022-12-04 14:30:26 -060053 size_t start, end, size;
54 size_t mctp_hdr_off;
Matt Johnston4a09e1d2024-09-13 14:55:58 +080055 bool alloc;
Patrick Williamsa721c2d2022-12-04 14:30:26 -060056 unsigned char data[];
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080057};
58
Matt Johnston4a09e1d2024-09-13 14:55:58 +080059#define MCTP_PKTBUF_SIZE(payload) \
60 (MCTP_PACKET_SIZE(payload) + sizeof(struct mctp_pktbuf))
61
62struct mctp;
63struct mctp_bus;
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +080064struct mctp_binding;
65
Matt Johnston4a09e1d2024-09-13 14:55:58 +080066/* Initialise a mctp_pktbuf in static storage. Should not be freed.
67 * Storage must be sized to fit the binding,
68 * MCTP_PKTBUF_SIZE(binding->pkt_size + binding->pkt_header + binding->pkt_trailer) */
69struct mctp_pktbuf *mctp_pktbuf_init(struct mctp_binding *binding,
70 void *storage);
71/* Allocate and initialise a mctp_pktbuf. Should be freed with
72 * mctp_pktbuf_free */
73struct mctp_pktbuf *mctp_pktbuf_alloc(struct mctp_binding *binding, size_t len);
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080074void mctp_pktbuf_free(struct mctp_pktbuf *pkt);
75struct mctp_hdr *mctp_pktbuf_hdr(struct mctp_pktbuf *pkt);
76void *mctp_pktbuf_data(struct mctp_pktbuf *pkt);
Matt Johnston4a09e1d2024-09-13 14:55:58 +080077size_t mctp_pktbuf_size(const struct mctp_pktbuf *pkt);
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +080078void *mctp_pktbuf_alloc_start(struct mctp_pktbuf *pkt, size_t size);
79void *mctp_pktbuf_alloc_end(struct mctp_pktbuf *pkt, size_t size);
Matt Johnstondfbf0fd2024-10-28 14:40:29 +080080int mctp_pktbuf_push(struct mctp_pktbuf *pkt, const void *data, size_t len);
Andrew Jefferyeba19a32021-03-09 23:09:40 +103081void *mctp_pktbuf_pop(struct mctp_pktbuf *pkt, size_t len);
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080082
83/* MCTP core */
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080084
Matt Johnston722d0db2024-09-13 15:51:30 +080085/* Allocate and setup a MCTP instance */
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080086struct mctp *mctp_init(void);
Matt Johnston722d0db2024-09-13 15:51:30 +080087/* Cleanup and deallocate a MCTP instance from mctp_init() */
88void mctp_destroy(struct mctp *mctp);
89
90/* Setup a MCTP instance */
Matt Johnstonf9b99f12024-09-17 16:48:34 +080091int mctp_setup(struct mctp *mctp, size_t struct_mctp_size);
Matt Johnston722d0db2024-09-13 15:51:30 +080092/* Release resource of a MCTP instance */
93void mctp_cleanup(struct mctp *mctp);
94
Sumanth Bhat2c820c52020-07-02 00:26:25 +053095void mctp_set_max_message_size(struct mctp *mctp, size_t message_size);
Rashmica Guptaf2988972022-11-09 12:26:44 +110096typedef void (*mctp_capture_fn)(struct mctp_pktbuf *pkt, bool outgoing,
97 void *user);
Patrick Williamsa721c2d2022-12-04 14:30:26 -060098void mctp_set_capture_handler(struct mctp *mctp, mctp_capture_fn fn,
99 void *user);
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800100
Jeremy Kerr7520cec2019-03-01 07:13:18 +0800101/* Register a binding to the MCTP core, and creates a bus (populating
102 * binding->bus).
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800103 *
104 * If this function is called, the MCTP stack is initialised as an 'endpoint',
105 * and will deliver local packets to a RX callback - see `mctp_set_rx_all()`
106 * below.
Jeremy Kerr7520cec2019-03-01 07:13:18 +0800107 */
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600108int mctp_register_bus(struct mctp *mctp, struct mctp_binding *binding,
109 mctp_eid_t eid);
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800110
Andrew Jeffery2094c3c2021-08-26 12:32:46 +0930111void mctp_unregister_bus(struct mctp *mctp, struct mctp_binding *binding);
112
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800113/* Create a simple bidirectional bridge between busses.
114 *
115 * In this mode, the MCTP stack is initialised as a bridge. There is no EID
116 * defined, so no packets are considered local. Instead, all messages from one
117 * binding are forwarded to the other.
118 */
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600119int mctp_bridge_busses(struct mctp *mctp, struct mctp_binding *b1,
120 struct mctp_binding *b2);
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800121
Sumanth Bhatf39c3852022-01-10 17:04:10 +0530122typedef void (*mctp_rx_fn)(uint8_t src_eid, bool tag_owner, uint8_t msg_tag,
123 void *data, void *msg, size_t len);
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800124
125int mctp_set_rx_all(struct mctp *mctp, mctp_rx_fn fn, void *data);
126
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800127/* Transmit a message.
128 * @msg: The message buffer to send. Must be suitable for
129 * free(), or the custom mctp_set_alloc_ops() m_msg_free.
130 * The mctp stack will take ownership of the buffer
131 * and release it when message transmission is complete or fails.
132 *
133 * If an asynchronous binding is being used, it will return -EBUSY if
134 * a message is already pending for transmission (msg will be freed as usual).
135 * Asynchronous users can test mctp_is_tx_ready() prior to sending.
136 */
137int mctp_message_tx_alloced(struct mctp *mctp, mctp_eid_t eid, bool tag_owner,
138 uint8_t msg_tag, void *msg, size_t msg_len);
139
140/* Transmit a message.
141 * @msg: The message buffer to send. Ownership of this buffer
142 * remains with the caller (a copy is made internally with __mctp_msg_alloc).
143 *
144 * If an asynchronous binding is being used, it will return -EBUSY if
145 * a message is already pending for transmission.
146 * Asynchronous users can test mctp_is_tx_ready() prior to sending.
147 *
148 * This is equivalent to duplicating `msg` then calling mctp_message_tx_alloc().
149 */
Sumanth Bhatf39c3852022-01-10 17:04:10 +0530150int mctp_message_tx(struct mctp *mctp, mctp_eid_t eid, bool tag_owner,
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800151 uint8_t msg_tag, const void *msg, size_t msg_len);
152
Matt Johnston61c95992024-09-16 16:50:35 +0800153/* Transmit a request message.
154 * @msg: The message buffer to send. Must be suitable for
155 * free(), or the custom mctp_set_alloc_ops() m_msg_free.
156 *
157 * A tag with Tag Owner bit set will allocated for the sent message,
158 * and returned to the caller (TO bit is unset in the returned @alloc_msg_tag).
159 * alloc_msg_tag may be NULL to ignore the returned tag.
160 * If no tags are spare -EBUSY will be returned.
161 *
162 * If an asynchronous binding is being used, it will return -EBUSY if
163 * a message is already pending for transmission (msg will be freed).
164 * Asynchronous users can test mctp_is_tx_ready() prior to sending.
165 */
166int mctp_message_tx_request(struct mctp *mctp, mctp_eid_t eid, void *msg,
167 size_t msg_len, uint8_t *alloc_msg_tag);
168
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800169bool mctp_is_tx_ready(struct mctp *mctp, mctp_eid_t eid);
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800170
171/* hardware bindings */
Andrew Jeffery0721f582022-09-29 12:12:39 +0930172
173/**
174 * @tx: Binding function to transmit one packet on the interface
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800175 * @tx_storage: A buffer for transmitting packets. Must be sized
176 * as MCTP_PKTBUF_SIZE(mtu).
Andrew Jeffery0721f582022-09-29 12:12:39 +0930177 * Return:
178 * * 0 - Success, pktbuf can be released
179 * * -EMSGSIZE - Packet exceeds binding MTU, pktbuf must be dropped
180 * * -EBUSY - Packet unable to be transmitted, pktbuf must be retained
181 */
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800182struct mctp_binding {
Wiktor Gołgowskiba6727e2020-03-13 18:25:01 +0100183 const char *name;
184 uint8_t version;
185 struct mctp_bus *bus;
186 struct mctp *mctp;
Andrew Jefferye889b192021-03-12 12:19:33 +1030187 size_t pkt_size;
Andrew Jeffery39da3d02021-03-12 16:51:26 +1030188 size_t pkt_header;
189 size_t pkt_trailer;
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800190 void *tx_storage;
Wiktor Gołgowskiba6727e2020-03-13 18:25:01 +0100191 int (*start)(struct mctp_binding *binding);
192 int (*tx)(struct mctp_binding *binding, struct mctp_pktbuf *pkt);
193 mctp_rx_fn control_rx;
194 void *control_rx_data;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800195};
196
Jeremy Kerr1cd31182019-02-27 18:01:00 +0800197void mctp_binding_set_tx_enabled(struct mctp_binding *binding, bool enable);
198
Jeremy Kerrc1693af2019-08-05 14:30:59 +0800199/*
200 * Receive a packet from binding to core. Takes ownership of pkt, free()-ing it
201 * after use.
202 */
Jeremy Kerr0a00dca2019-03-01 08:01:35 +0800203void mctp_bus_rx(struct mctp_binding *binding, struct mctp_pktbuf *pkt);
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800204
205/* environment-specific allocation */
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800206void mctp_set_alloc_ops(void *(*m_alloc)(size_t), void (*m_free)(void *),
207 void *(*m_msg_alloc)(size_t, void *),
208 void (*m_msg_free)(void *, void *));
209/* Gets/sets context that will be passed to custom m_msg_ ops */
210void *mctp_get_alloc_ctx(struct mctp *mctp);
211void mctp_set_alloc_ctx(struct mctp *mctp, void *ctx);
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800212
Jeremy Kerrc7e764a2019-05-28 16:49:03 +0800213/* environment-specific logging */
Jeremy Kerrcc49e162019-05-30 21:11:16 +0800214
Jeremy Kerrc7e764a2019-05-28 16:49:03 +0800215void mctp_set_log_stdio(int level);
216void mctp_set_log_syslog(void);
217void mctp_set_log_custom(void (*fn)(int, const char *, va_list));
218
Jeremy Kerrcc49e162019-05-30 21:11:16 +0800219/* these should match the syslog-standard LOG_* definitions, for
220 * easier use with syslog */
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600221#define MCTP_LOG_ERR 3
222#define MCTP_LOG_WARNING 4
223#define MCTP_LOG_NOTICE 5
224#define MCTP_LOG_INFO 6
225#define MCTP_LOG_DEBUG 7
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800226
Matt Johnston44e64df2024-11-05 16:59:42 +0800227/* Environment-specific time functionality */
228/* The `now` callback returns a timestamp in milliseconds.
229 * Timestamps should be monotonically increasing, and can have an arbitrary
230 * origin. (As long as returned timestamps aren't too close to UINT64_MAX, not
231 * a problem forany reasonable implementation). */
232void mctp_set_now_op(struct mctp *mctp, uint64_t (*now)(void *), void *ctx);
233/* Returns a timestamp in milliseconds */
234uint64_t mctp_now(struct mctp *mctp);
235
Deepak Kodihallib11ad2c2019-02-28 03:42:22 -0600236#ifdef __cplusplus
237}
238#endif
239
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800240#endif /* _LIBMCTP_H */