Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 1 | /* SPDX-License-Identifier: Apache-2.0 */ |
| 2 | |
| 3 | #ifndef _LIBMCTP_H |
| 4 | #define _LIBMCTP_H |
| 5 | |
Deepak Kodihalli | b11ad2c | 2019-02-28 03:42:22 -0600 | [diff] [blame] | 6 | #ifdef __cplusplus |
| 7 | extern "C" { |
| 8 | #endif |
| 9 | |
Jeremy Kerr | c7e764a | 2019-05-28 16:49:03 +0800 | [diff] [blame^] | 10 | #include <stdarg.h> |
Jeremy Kerr | 1cd3118 | 2019-02-27 18:01:00 +0800 | [diff] [blame] | 11 | #include <stdbool.h> |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 12 | #include <stdint.h> |
Jeremy Kerr | f9ffd59 | 2019-03-06 09:09:38 +0800 | [diff] [blame] | 13 | #include <stddef.h> |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 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) |
Ed Tanous | c2def9f | 2019-02-21 08:33:08 -0800 | [diff] [blame] | 29 | #define MCTP_HDR_SEQ_SHIFT (4) |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 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 + \ |
Ed Tanous | 9f10122 | 2019-02-11 11:22:03 -0800 | [diff] [blame] | 47 | (sizeof(struct mctp_hdr) + MCTP_MTU)) |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 48 | |
| 49 | struct mctp_pktbuf { |
| 50 | unsigned char data[MCTP_PKTBUF_SIZE]; |
| 51 | uint8_t start, end; |
| 52 | uint8_t mctp_hdr_off; |
Jeremy Kerr | e16eaab | 2019-02-08 09:30:10 +0800 | [diff] [blame] | 53 | struct mctp_pktbuf *next; |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 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; |
Jeremy Kerr | 7520cec | 2019-03-01 07:13:18 +0800 | [diff] [blame] | 67 | struct mctp_bus; |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 68 | struct mctp_binding; |
| 69 | |
| 70 | struct mctp *mctp_init(void); |
| 71 | |
Jeremy Kerr | 7520cec | 2019-03-01 07:13:18 +0800 | [diff] [blame] | 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, |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 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; |
Jeremy Kerr | 7520cec | 2019-03-01 07:13:18 +0800 | [diff] [blame] | 91 | struct mctp_bus *bus; |
Jeremy Kerr | 0a00dca | 2019-03-01 08:01:35 +0800 | [diff] [blame] | 92 | struct mctp *mctp; |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 93 | int (*tx)(struct mctp_binding *binding, |
| 94 | struct mctp_pktbuf *pkt); |
| 95 | }; |
| 96 | |
Jeremy Kerr | 1cd3118 | 2019-02-27 18:01:00 +0800 | [diff] [blame] | 97 | void mctp_binding_set_tx_enabled(struct mctp_binding *binding, bool enable); |
| 98 | |
Jeremy Kerr | 0a00dca | 2019-03-01 08:01:35 +0800 | [diff] [blame] | 99 | void mctp_bus_rx(struct mctp_binding *binding, struct mctp_pktbuf *pkt); |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 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 | |
Jeremy Kerr | c7e764a | 2019-05-28 16:49:03 +0800 | [diff] [blame^] | 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 | |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 111 | |
Deepak Kodihalli | b11ad2c | 2019-02-28 03:42:22 -0600 | [diff] [blame] | 112 | #ifdef __cplusplus |
| 113 | } |
| 114 | #endif |
| 115 | |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 116 | #endif /* _LIBMCTP_H */ |