blob: 0a40c0e12c88f876ac88679c0dd3afaf29fe8b69 [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
17/* MCTP packet definitions */
18struct 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)
Ed Tanousc2def9f2019-02-21 08:33:08 -080029#define MCTP_HDR_SEQ_SHIFT (4)
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080030#define MCTP_HDR_SEQ_MASK (0x3)
31#define MCTP_HDR_TAG_SHIFT (0)
32#define MCTP_HDR_TAG_MASK (0x7)
33
Andrew Jeffery73c268e2020-01-30 10:16:09 +103034/* Baseline Transmission Unit and packet size */
35#define MCTP_BTU 64
36#define MCTP_PACKET_SIZE(unit) ((unit) + sizeof(struct mctp_hdr))
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080037
38/* packet buffers */
39
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080040struct mctp_pktbuf {
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +080041 size_t start, end, size;
42 size_t mctp_hdr_off;
Jeremy Kerre16eaab2019-02-08 09:30:10 +080043 struct mctp_pktbuf *next;
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +080044 unsigned char data[];
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080045};
46
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +080047struct mctp_binding;
48
49struct mctp_pktbuf *mctp_pktbuf_alloc(struct mctp_binding *hw, size_t len);
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080050void mctp_pktbuf_free(struct mctp_pktbuf *pkt);
51struct mctp_hdr *mctp_pktbuf_hdr(struct mctp_pktbuf *pkt);
52void *mctp_pktbuf_data(struct mctp_pktbuf *pkt);
53uint8_t mctp_pktbuf_size(struct mctp_pktbuf *pkt);
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +080054void *mctp_pktbuf_alloc_start(struct mctp_pktbuf *pkt, size_t size);
55void *mctp_pktbuf_alloc_end(struct mctp_pktbuf *pkt, size_t size);
56int mctp_pktbuf_push(struct mctp_pktbuf *pkt, void *data, size_t len);
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080057
58/* MCTP core */
59struct mctp;
Jeremy Kerr7520cec2019-03-01 07:13:18 +080060struct mctp_bus;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080061
62struct mctp *mctp_init(void);
Andrew Jefferyfa56ca52020-03-10 23:18:22 +103063void mctp_destroy(struct mctp *mctp);
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080064
Jeremy Kerr7520cec2019-03-01 07:13:18 +080065/* Register a binding to the MCTP core, and creates a bus (populating
66 * binding->bus).
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +080067 *
68 * If this function is called, the MCTP stack is initialised as an 'endpoint',
69 * and will deliver local packets to a RX callback - see `mctp_set_rx_all()`
70 * below.
Jeremy Kerr7520cec2019-03-01 07:13:18 +080071 */
72int mctp_register_bus(struct mctp *mctp,
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080073 struct mctp_binding *binding,
74 mctp_eid_t eid);
75
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +080076/* Create a simple bidirectional bridge between busses.
77 *
78 * In this mode, the MCTP stack is initialised as a bridge. There is no EID
79 * defined, so no packets are considered local. Instead, all messages from one
80 * binding are forwarded to the other.
81 */
82int mctp_bridge_busses(struct mctp *mctp,
83 struct mctp_binding *b1, struct mctp_binding *b2);
84
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080085typedef void (*mctp_rx_fn)(uint8_t src_eid, void *data,
86 void *msg, size_t len);
87
88int mctp_set_rx_all(struct mctp *mctp, mctp_rx_fn fn, void *data);
89
90int mctp_message_tx(struct mctp *mctp, mctp_eid_t eid,
91 void *msg, size_t msg_len);
92
93/* hardware bindings */
94struct mctp_binding {
95 const char *name;
96 uint8_t version;
Jeremy Kerr7520cec2019-03-01 07:13:18 +080097 struct mctp_bus *bus;
Jeremy Kerr0a00dca2019-03-01 08:01:35 +080098 struct mctp *mctp;
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +080099 int pkt_size;
100 int pkt_pad;
Jeremy Kerr3b36d172019-09-04 11:56:09 +0800101 int (*start)(struct mctp_binding *binding);
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800102 int (*tx)(struct mctp_binding *binding,
103 struct mctp_pktbuf *pkt);
104};
105
Jeremy Kerr1cd31182019-02-27 18:01:00 +0800106void mctp_binding_set_tx_enabled(struct mctp_binding *binding, bool enable);
107
Jeremy Kerrc1693af2019-08-05 14:30:59 +0800108/*
109 * Receive a packet from binding to core. Takes ownership of pkt, free()-ing it
110 * after use.
111 */
Jeremy Kerr0a00dca2019-03-01 08:01:35 +0800112void mctp_bus_rx(struct mctp_binding *binding, struct mctp_pktbuf *pkt);
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800113
114/* environment-specific allocation */
115void mctp_set_alloc_ops(void *(*alloc)(size_t),
116 void (*free)(void *),
117 void *(realloc)(void *, size_t));
118
Jeremy Kerrc7e764a2019-05-28 16:49:03 +0800119/* environment-specific logging */
Jeremy Kerrcc49e162019-05-30 21:11:16 +0800120
Jeremy Kerrc7e764a2019-05-28 16:49:03 +0800121void mctp_set_log_stdio(int level);
122void mctp_set_log_syslog(void);
123void mctp_set_log_custom(void (*fn)(int, const char *, va_list));
124
Jeremy Kerrcc49e162019-05-30 21:11:16 +0800125/* these should match the syslog-standard LOG_* definitions, for
126 * easier use with syslog */
127#define MCTP_LOG_ERR 3
128#define MCTP_LOG_WARNING 4
129#define MCTP_LOG_NOTICE 5
130#define MCTP_LOG_INFO 6
131#define MCTP_LOG_DEBUG 7
132
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800133
Deepak Kodihallib11ad2c2019-02-28 03:42:22 -0600134#ifdef __cplusplus
135}
136#endif
137
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800138#endif /* _LIBMCTP_H */