1 /* SPDX-License-Identifier: Apache-2.0 */ 2 3 #ifndef _LIBMCTP_H 4 #define _LIBMCTP_H 5 6 #ifdef __cplusplus 7 extern "C" { 8 #endif 9 10 #include <stdarg.h> 11 #include <stdbool.h> 12 #include <stdint.h> 13 #include <stddef.h> 14 15 typedef uint8_t mctp_eid_t; 16 17 /* MCTP packet definitions */ 18 struct 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) 29 #define MCTP_HDR_SEQ_SHIFT (4) 30 #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 + \ 47 (sizeof(struct mctp_hdr) + MCTP_MTU)) 48 49 struct mctp_pktbuf { 50 unsigned char data[MCTP_PKTBUF_SIZE]; 51 uint8_t start, end; 52 uint8_t mctp_hdr_off; 53 struct mctp_pktbuf *next; 54 }; 55 56 struct mctp_pktbuf *mctp_pktbuf_alloc(uint8_t len); 57 void mctp_pktbuf_free(struct mctp_pktbuf *pkt); 58 struct mctp_hdr *mctp_pktbuf_hdr(struct mctp_pktbuf *pkt); 59 void *mctp_pktbuf_data(struct mctp_pktbuf *pkt); 60 uint8_t mctp_pktbuf_size(struct mctp_pktbuf *pkt); 61 void *mctp_pktbuf_alloc_start(struct mctp_pktbuf *pkt, uint8_t size); 62 void *mctp_pktbuf_alloc_end(struct mctp_pktbuf *pkt, uint8_t size); 63 int mctp_pktbuf_push(struct mctp_pktbuf *pkt, void *data, uint8_t len); 64 65 /* MCTP core */ 66 struct mctp; 67 struct mctp_bus; 68 struct mctp_binding; 69 70 struct mctp *mctp_init(void); 71 72 /* Register a binding to the MCTP core, and creates a bus (populating 73 * binding->bus). 74 */ 75 int mctp_register_bus(struct mctp *mctp, 76 struct mctp_binding *binding, 77 mctp_eid_t eid); 78 79 typedef void (*mctp_rx_fn)(uint8_t src_eid, void *data, 80 void *msg, size_t len); 81 82 int mctp_set_rx_all(struct mctp *mctp, mctp_rx_fn fn, void *data); 83 84 int mctp_message_tx(struct mctp *mctp, mctp_eid_t eid, 85 void *msg, size_t msg_len); 86 87 /* hardware bindings */ 88 struct mctp_binding { 89 const char *name; 90 uint8_t version; 91 struct mctp_bus *bus; 92 struct mctp *mctp; 93 int (*tx)(struct mctp_binding *binding, 94 struct mctp_pktbuf *pkt); 95 }; 96 97 void mctp_binding_set_tx_enabled(struct mctp_binding *binding, bool enable); 98 99 void mctp_bus_rx(struct mctp_binding *binding, struct mctp_pktbuf *pkt); 100 101 /* environment-specific allocation */ 102 void mctp_set_alloc_ops(void *(*alloc)(size_t), 103 void (*free)(void *), 104 void *(realloc)(void *, size_t)); 105 106 /* environment-specific logging */ 107 void mctp_set_log_stdio(int level); 108 void mctp_set_log_syslog(void); 109 void mctp_set_log_custom(void (*fn)(int, const char *, va_list)); 110 111 112 #ifdef __cplusplus 113 } 114 #endif 115 116 #endif /* _LIBMCTP_H */ 117