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