blob: bc0cb5bd820935c7c3c1d42c3ddb3db7450aed55 [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 */
18#define MCTP_EID_NULL 0
19#define MCTP_EID_BROADCAST 0xff
20
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080021/* MCTP packet definitions */
22struct 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)
Ed Tanousc2def9f2019-02-21 08:33:08 -080033#define MCTP_HDR_SEQ_SHIFT (4)
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080034#define MCTP_HDR_SEQ_MASK (0x3)
35#define MCTP_HDR_TAG_SHIFT (0)
36#define MCTP_HDR_TAG_MASK (0x7)
37
Andrew Jeffery73c268e2020-01-30 10:16:09 +103038/* Baseline Transmission Unit and packet size */
39#define MCTP_BTU 64
40#define MCTP_PACKET_SIZE(unit) ((unit) + sizeof(struct mctp_hdr))
Andrew Jeffery3a540662020-05-26 19:55:30 +093041#define MCTP_BODY_SIZE(unit) ((unit) - sizeof(struct mctp_hdr))
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080042
43/* packet buffers */
44
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080045struct mctp_pktbuf {
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +080046 size_t start, end, size;
47 size_t mctp_hdr_off;
Jeremy Kerre16eaab2019-02-08 09:30:10 +080048 struct mctp_pktbuf *next;
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +080049 unsigned char data[];
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080050};
51
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +080052struct mctp_binding;
53
54struct mctp_pktbuf *mctp_pktbuf_alloc(struct mctp_binding *hw, size_t len);
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080055void mctp_pktbuf_free(struct mctp_pktbuf *pkt);
56struct mctp_hdr *mctp_pktbuf_hdr(struct mctp_pktbuf *pkt);
57void *mctp_pktbuf_data(struct mctp_pktbuf *pkt);
Andrew Jefferyb942e3a2020-06-23 09:54:02 +093058size_t mctp_pktbuf_size(struct mctp_pktbuf *pkt);
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +080059void *mctp_pktbuf_alloc_start(struct mctp_pktbuf *pkt, size_t size);
60void *mctp_pktbuf_alloc_end(struct mctp_pktbuf *pkt, size_t size);
61int mctp_pktbuf_push(struct mctp_pktbuf *pkt, void *data, size_t len);
Andrew Jefferyeba19a32021-03-09 23:09:40 +103062void *mctp_pktbuf_pop(struct mctp_pktbuf *pkt, size_t len);
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080063
64/* MCTP core */
65struct mctp;
Jeremy Kerr7520cec2019-03-01 07:13:18 +080066struct mctp_bus;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080067
68struct mctp *mctp_init(void);
Sumanth Bhat2c820c52020-07-02 00:26:25 +053069void mctp_set_max_message_size(struct mctp *mctp, size_t message_size);
Andrew Jeffery5d3d4e62021-08-20 16:44:40 +093070typedef void (*mctp_capture_fn)(struct mctp_pktbuf *pkt, void *user);
71void mctp_set_capture_handler(struct mctp *mctp, mctp_capture_fn fn, void *user);
Andrew Jefferyfa56ca52020-03-10 23:18:22 +103072void mctp_destroy(struct mctp *mctp);
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080073
Jeremy Kerr7520cec2019-03-01 07:13:18 +080074/* Register a binding to the MCTP core, and creates a bus (populating
75 * binding->bus).
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +080076 *
77 * If this function is called, the MCTP stack is initialised as an 'endpoint',
78 * and will deliver local packets to a RX callback - see `mctp_set_rx_all()`
79 * below.
Jeremy Kerr7520cec2019-03-01 07:13:18 +080080 */
81int mctp_register_bus(struct mctp *mctp,
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080082 struct mctp_binding *binding,
83 mctp_eid_t eid);
84
Andrew Jeffery2094c3c2021-08-26 12:32:46 +093085void mctp_unregister_bus(struct mctp *mctp, struct mctp_binding *binding);
86
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +080087/* Create a simple bidirectional bridge between busses.
88 *
89 * In this mode, the MCTP stack is initialised as a bridge. There is no EID
90 * defined, so no packets are considered local. Instead, all messages from one
91 * binding are forwarded to the other.
92 */
93int mctp_bridge_busses(struct mctp *mctp,
94 struct mctp_binding *b1, struct mctp_binding *b2);
95
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080096typedef void (*mctp_rx_fn)(uint8_t src_eid, void *data,
97 void *msg, size_t len);
98
99int mctp_set_rx_all(struct mctp *mctp, mctp_rx_fn fn, void *data);
100
101int mctp_message_tx(struct mctp *mctp, mctp_eid_t eid,
102 void *msg, size_t msg_len);
103
104/* hardware bindings */
105struct mctp_binding {
Wiktor Gołgowskiba6727e2020-03-13 18:25:01 +0100106 const char *name;
107 uint8_t version;
108 struct mctp_bus *bus;
109 struct mctp *mctp;
Andrew Jefferye889b192021-03-12 12:19:33 +1030110 size_t pkt_size;
Andrew Jeffery39da3d02021-03-12 16:51:26 +1030111 size_t pkt_header;
112 size_t pkt_trailer;
Wiktor Gołgowskiba6727e2020-03-13 18:25:01 +0100113 int (*start)(struct mctp_binding *binding);
114 int (*tx)(struct mctp_binding *binding, struct mctp_pktbuf *pkt);
115 mctp_rx_fn control_rx;
116 void *control_rx_data;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800117};
118
Jeremy Kerr1cd31182019-02-27 18:01:00 +0800119void mctp_binding_set_tx_enabled(struct mctp_binding *binding, bool enable);
120
Jeremy Kerrc1693af2019-08-05 14:30:59 +0800121/*
122 * Receive a packet from binding to core. Takes ownership of pkt, free()-ing it
123 * after use.
124 */
Jeremy Kerr0a00dca2019-03-01 08:01:35 +0800125void mctp_bus_rx(struct mctp_binding *binding, struct mctp_pktbuf *pkt);
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800126
127/* environment-specific allocation */
128void mctp_set_alloc_ops(void *(*alloc)(size_t),
129 void (*free)(void *),
130 void *(realloc)(void *, size_t));
131
Jeremy Kerrc7e764a2019-05-28 16:49:03 +0800132/* environment-specific logging */
Jeremy Kerrcc49e162019-05-30 21:11:16 +0800133
Jeremy Kerrc7e764a2019-05-28 16:49:03 +0800134void mctp_set_log_stdio(int level);
135void mctp_set_log_syslog(void);
136void mctp_set_log_custom(void (*fn)(int, const char *, va_list));
137
Jeremy Kerrcc49e162019-05-30 21:11:16 +0800138/* these should match the syslog-standard LOG_* definitions, for
139 * easier use with syslog */
140#define MCTP_LOG_ERR 3
141#define MCTP_LOG_WARNING 4
142#define MCTP_LOG_NOTICE 5
143#define MCTP_LOG_INFO 6
144#define MCTP_LOG_DEBUG 7
145
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800146
Deepak Kodihallib11ad2c2019-02-28 03:42:22 -0600147#ifdef __cplusplus
148}
149#endif
150
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800151#endif /* _LIBMCTP_H */