blob: 69ec46a96a44d1a309babcef5f13f545e4a68b07 [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);
63
Jeremy Kerr7520cec2019-03-01 07:13:18 +080064/* Register a binding to the MCTP core, and creates a bus (populating
65 * binding->bus).
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +080066 *
67 * If this function is called, the MCTP stack is initialised as an 'endpoint',
68 * and will deliver local packets to a RX callback - see `mctp_set_rx_all()`
69 * below.
Jeremy Kerr7520cec2019-03-01 07:13:18 +080070 */
71int mctp_register_bus(struct mctp *mctp,
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080072 struct mctp_binding *binding,
73 mctp_eid_t eid);
74
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +080075/* Create a simple bidirectional bridge between busses.
76 *
77 * In this mode, the MCTP stack is initialised as a bridge. There is no EID
78 * defined, so no packets are considered local. Instead, all messages from one
79 * binding are forwarded to the other.
80 */
81int mctp_bridge_busses(struct mctp *mctp,
82 struct mctp_binding *b1, struct mctp_binding *b2);
83
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080084typedef void (*mctp_rx_fn)(uint8_t src_eid, void *data,
85 void *msg, size_t len);
86
87int mctp_set_rx_all(struct mctp *mctp, mctp_rx_fn fn, void *data);
88
89int mctp_message_tx(struct mctp *mctp, mctp_eid_t eid,
90 void *msg, size_t msg_len);
91
92/* hardware bindings */
93struct mctp_binding {
94 const char *name;
95 uint8_t version;
Jeremy Kerr7520cec2019-03-01 07:13:18 +080096 struct mctp_bus *bus;
Jeremy Kerr0a00dca2019-03-01 08:01:35 +080097 struct mctp *mctp;
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +080098 int pkt_size;
99 int pkt_pad;
Jeremy Kerr3b36d172019-09-04 11:56:09 +0800100 int (*start)(struct mctp_binding *binding);
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800101 int (*tx)(struct mctp_binding *binding,
102 struct mctp_pktbuf *pkt);
103};
104
Jeremy Kerr1cd31182019-02-27 18:01:00 +0800105void mctp_binding_set_tx_enabled(struct mctp_binding *binding, bool enable);
106
Jeremy Kerrc1693af2019-08-05 14:30:59 +0800107/*
108 * Receive a packet from binding to core. Takes ownership of pkt, free()-ing it
109 * after use.
110 */
Jeremy Kerr0a00dca2019-03-01 08:01:35 +0800111void mctp_bus_rx(struct mctp_binding *binding, struct mctp_pktbuf *pkt);
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800112
113/* environment-specific allocation */
114void mctp_set_alloc_ops(void *(*alloc)(size_t),
115 void (*free)(void *),
116 void *(realloc)(void *, size_t));
117
Jeremy Kerrc7e764a2019-05-28 16:49:03 +0800118/* environment-specific logging */
Jeremy Kerrcc49e162019-05-30 21:11:16 +0800119
Jeremy Kerrc7e764a2019-05-28 16:49:03 +0800120void mctp_set_log_stdio(int level);
121void mctp_set_log_syslog(void);
122void mctp_set_log_custom(void (*fn)(int, const char *, va_list));
123
Jeremy Kerrcc49e162019-05-30 21:11:16 +0800124/* these should match the syslog-standard LOG_* definitions, for
125 * easier use with syslog */
126#define MCTP_LOG_ERR 3
127#define MCTP_LOG_WARNING 4
128#define MCTP_LOG_NOTICE 5
129#define MCTP_LOG_INFO 6
130#define MCTP_LOG_DEBUG 7
131
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800132
Deepak Kodihallib11ad2c2019-02-28 03:42:22 -0600133#ifdef __cplusplus
134}
135#endif
136
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800137#endif /* _LIBMCTP_H */