blob: d1696a4a382b2c7ab1ea07ebf44b49971afd4988 [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 Kerr4cdc2002019-02-07 16:49:12 +080010#include <stdint.h>
11
12typedef uint8_t mctp_eid_t;
13
14/* MCTP packet definitions */
15struct mctp_hdr {
16 uint8_t ver;
17 uint8_t dest;
18 uint8_t src;
19 uint8_t flags_seq_tag;
20};
21
22/* Definitions for flags_seq_tag field */
23#define MCTP_HDR_FLAG_SOM (1<<7)
24#define MCTP_HDR_FLAG_EOM (1<<6)
25#define MCTP_HDR_FLAG_TO (1<<3)
26#define MCTP_HDR_SEQ_SHIFT (5)
27#define MCTP_HDR_SEQ_MASK (0x3)
28#define MCTP_HDR_TAG_SHIFT (0)
29#define MCTP_HDR_TAG_MASK (0x7)
30
31/* Maximum size of *payload* data in a MCTP packet
32 * @todo: dynamic sixing based on channel implementation.
33 */
34#define MCTP_MTU 64
35
36/* packet buffers */
37
38/* Allow a little space before the MCTP header in the packet, for bindings that
39 * may add their own header
40 */
41#define MCTP_PKTBUF_BINDING_PAD 2
42
43#define MCTP_PKTBUF_SIZE (MCTP_PKTBUF_BINDING_PAD + \
Ed Tanous9f101222019-02-11 11:22:03 -080044 (sizeof(struct mctp_hdr) + MCTP_MTU))
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080045
46struct mctp_pktbuf {
47 unsigned char data[MCTP_PKTBUF_SIZE];
48 uint8_t start, end;
49 uint8_t mctp_hdr_off;
Jeremy Kerre16eaab2019-02-08 09:30:10 +080050 struct mctp_pktbuf *next;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080051};
52
53struct mctp_pktbuf *mctp_pktbuf_alloc(uint8_t len);
54void mctp_pktbuf_free(struct mctp_pktbuf *pkt);
55struct mctp_hdr *mctp_pktbuf_hdr(struct mctp_pktbuf *pkt);
56void *mctp_pktbuf_data(struct mctp_pktbuf *pkt);
57uint8_t mctp_pktbuf_size(struct mctp_pktbuf *pkt);
58void *mctp_pktbuf_alloc_start(struct mctp_pktbuf *pkt, uint8_t size);
59void *mctp_pktbuf_alloc_end(struct mctp_pktbuf *pkt, uint8_t size);
60int mctp_pktbuf_push(struct mctp_pktbuf *pkt, void *data, uint8_t len);
61
62/* MCTP core */
63struct mctp;
64struct mctp_binding;
65
66struct mctp *mctp_init(void);
67
68unsigned long mctp_register_bus(struct mctp *mctp,
69 struct mctp_binding *binding,
70 mctp_eid_t eid);
71
72typedef void (*mctp_rx_fn)(uint8_t src_eid, void *data,
73 void *msg, size_t len);
74
75int mctp_set_rx_all(struct mctp *mctp, mctp_rx_fn fn, void *data);
76
77int mctp_message_tx(struct mctp *mctp, mctp_eid_t eid,
78 void *msg, size_t msg_len);
79
80/* hardware bindings */
81struct mctp_binding {
82 const char *name;
83 uint8_t version;
84 int (*tx)(struct mctp_binding *binding,
85 struct mctp_pktbuf *pkt);
86};
87
88void mctp_bus_rx(struct mctp *mctp, unsigned long bus_id,
89 struct mctp_pktbuf *pkt);
90
91/* environment-specific allocation */
92void mctp_set_alloc_ops(void *(*alloc)(size_t),
93 void (*free)(void *),
94 void *(realloc)(void *, size_t));
95
96
Deepak Kodihallib11ad2c2019-02-28 03:42:22 -060097#ifdef __cplusplus
98}
99#endif
100
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800101#endif /* _LIBMCTP_H */