blob: e3a125cd748e0c245b92c2afbf8c7b4e65abe9f2 [file] [log] [blame]
Jeremy Kerr4cdc2002019-02-07 16:49:12 +08001/* SPDX-License-Identifier: Apache-2.0 */
2
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
34/* Maximum size of *payload* data in a MCTP packet
35 * @todo: dynamic sixing based on channel implementation.
36 */
37#define MCTP_MTU 64
38
39/* packet buffers */
40
41/* Allow a little space before the MCTP header in the packet, for bindings that
42 * may add their own header
43 */
44#define MCTP_PKTBUF_BINDING_PAD 2
45
46#define MCTP_PKTBUF_SIZE (MCTP_PKTBUF_BINDING_PAD + \
Ed Tanous9f101222019-02-11 11:22:03 -080047 (sizeof(struct mctp_hdr) + MCTP_MTU))
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080048
49struct mctp_pktbuf {
50 unsigned char data[MCTP_PKTBUF_SIZE];
51 uint8_t start, end;
52 uint8_t mctp_hdr_off;
Jeremy Kerre16eaab2019-02-08 09:30:10 +080053 struct mctp_pktbuf *next;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080054};
55
56struct mctp_pktbuf *mctp_pktbuf_alloc(uint8_t len);
57void mctp_pktbuf_free(struct mctp_pktbuf *pkt);
58struct mctp_hdr *mctp_pktbuf_hdr(struct mctp_pktbuf *pkt);
59void *mctp_pktbuf_data(struct mctp_pktbuf *pkt);
60uint8_t mctp_pktbuf_size(struct mctp_pktbuf *pkt);
61void *mctp_pktbuf_alloc_start(struct mctp_pktbuf *pkt, uint8_t size);
62void *mctp_pktbuf_alloc_end(struct mctp_pktbuf *pkt, uint8_t size);
63int mctp_pktbuf_push(struct mctp_pktbuf *pkt, void *data, uint8_t len);
64
65/* MCTP core */
66struct mctp;
Jeremy Kerr7520cec2019-03-01 07:13:18 +080067struct mctp_bus;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080068struct mctp_binding;
69
70struct mctp *mctp_init(void);
71
Jeremy Kerr7520cec2019-03-01 07:13:18 +080072/* Register a binding to the MCTP core, and creates a bus (populating
73 * binding->bus).
74 */
75int mctp_register_bus(struct mctp *mctp,
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080076 struct mctp_binding *binding,
77 mctp_eid_t eid);
78
79typedef void (*mctp_rx_fn)(uint8_t src_eid, void *data,
80 void *msg, size_t len);
81
82int mctp_set_rx_all(struct mctp *mctp, mctp_rx_fn fn, void *data);
83
84int mctp_message_tx(struct mctp *mctp, mctp_eid_t eid,
85 void *msg, size_t msg_len);
86
87/* hardware bindings */
88struct mctp_binding {
89 const char *name;
90 uint8_t version;
Jeremy Kerr7520cec2019-03-01 07:13:18 +080091 struct mctp_bus *bus;
Jeremy Kerr0a00dca2019-03-01 08:01:35 +080092 struct mctp *mctp;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080093 int (*tx)(struct mctp_binding *binding,
94 struct mctp_pktbuf *pkt);
95};
96
Jeremy Kerr1cd31182019-02-27 18:01:00 +080097void mctp_binding_set_tx_enabled(struct mctp_binding *binding, bool enable);
98
Jeremy Kerr0a00dca2019-03-01 08:01:35 +080099void mctp_bus_rx(struct mctp_binding *binding, struct mctp_pktbuf *pkt);
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800100
101/* environment-specific allocation */
102void mctp_set_alloc_ops(void *(*alloc)(size_t),
103 void (*free)(void *),
104 void *(realloc)(void *, size_t));
105
Jeremy Kerrc7e764a2019-05-28 16:49:03 +0800106/* environment-specific logging */
107void mctp_set_log_stdio(int level);
108void mctp_set_log_syslog(void);
109void mctp_set_log_custom(void (*fn)(int, const char *, va_list));
110
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800111
Deepak Kodihallib11ad2c2019-02-28 03:42:22 -0600112#ifdef __cplusplus
113}
114#endif
115
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800116#endif /* _LIBMCTP_H */