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 | 1cd3118 | 2019-02-27 18:01:00 +0800 | [diff] [blame] | 10 | #include <stdbool.h> |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 11 | #include <stdint.h> |
Jeremy Kerr | f9ffd59 | 2019-03-06 09:09:38 +0800 | [diff] [blame] | 12 | #include <stddef.h> |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 13 | |
| 14 | typedef uint8_t mctp_eid_t; |
| 15 | |
| 16 | /* MCTP packet definitions */ |
| 17 | struct mctp_hdr { |
| 18 | uint8_t ver; |
| 19 | uint8_t dest; |
| 20 | uint8_t src; |
| 21 | uint8_t flags_seq_tag; |
| 22 | }; |
| 23 | |
| 24 | /* Definitions for flags_seq_tag field */ |
| 25 | #define MCTP_HDR_FLAG_SOM (1<<7) |
| 26 | #define MCTP_HDR_FLAG_EOM (1<<6) |
| 27 | #define MCTP_HDR_FLAG_TO (1<<3) |
Ed Tanous | c2def9f | 2019-02-21 08:33:08 -0800 | [diff] [blame] | 28 | #define MCTP_HDR_SEQ_SHIFT (4) |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 29 | #define MCTP_HDR_SEQ_MASK (0x3) |
| 30 | #define MCTP_HDR_TAG_SHIFT (0) |
| 31 | #define MCTP_HDR_TAG_MASK (0x7) |
| 32 | |
| 33 | /* Maximum size of *payload* data in a MCTP packet |
| 34 | * @todo: dynamic sixing based on channel implementation. |
| 35 | */ |
| 36 | #define MCTP_MTU 64 |
| 37 | |
| 38 | /* packet buffers */ |
| 39 | |
| 40 | /* Allow a little space before the MCTP header in the packet, for bindings that |
| 41 | * may add their own header |
| 42 | */ |
| 43 | #define MCTP_PKTBUF_BINDING_PAD 2 |
| 44 | |
| 45 | #define MCTP_PKTBUF_SIZE (MCTP_PKTBUF_BINDING_PAD + \ |
Ed Tanous | 9f10122 | 2019-02-11 11:22:03 -0800 | [diff] [blame] | 46 | (sizeof(struct mctp_hdr) + MCTP_MTU)) |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 47 | |
| 48 | struct mctp_pktbuf { |
| 49 | unsigned char data[MCTP_PKTBUF_SIZE]; |
| 50 | uint8_t start, end; |
| 51 | uint8_t mctp_hdr_off; |
Jeremy Kerr | e16eaab | 2019-02-08 09:30:10 +0800 | [diff] [blame] | 52 | struct mctp_pktbuf *next; |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 53 | }; |
| 54 | |
| 55 | struct mctp_pktbuf *mctp_pktbuf_alloc(uint8_t len); |
| 56 | void mctp_pktbuf_free(struct mctp_pktbuf *pkt); |
| 57 | struct mctp_hdr *mctp_pktbuf_hdr(struct mctp_pktbuf *pkt); |
| 58 | void *mctp_pktbuf_data(struct mctp_pktbuf *pkt); |
| 59 | uint8_t mctp_pktbuf_size(struct mctp_pktbuf *pkt); |
| 60 | void *mctp_pktbuf_alloc_start(struct mctp_pktbuf *pkt, uint8_t size); |
| 61 | void *mctp_pktbuf_alloc_end(struct mctp_pktbuf *pkt, uint8_t size); |
| 62 | int mctp_pktbuf_push(struct mctp_pktbuf *pkt, void *data, uint8_t len); |
| 63 | |
| 64 | /* MCTP core */ |
| 65 | struct mctp; |
Jeremy Kerr | 7520cec | 2019-03-01 07:13:18 +0800 | [diff] [blame] | 66 | struct mctp_bus; |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 67 | struct mctp_binding; |
| 68 | |
| 69 | struct mctp *mctp_init(void); |
| 70 | |
Jeremy Kerr | 7520cec | 2019-03-01 07:13:18 +0800 | [diff] [blame] | 71 | /* Register a binding to the MCTP core, and creates a bus (populating |
| 72 | * binding->bus). |
| 73 | */ |
| 74 | int mctp_register_bus(struct mctp *mctp, |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 75 | struct mctp_binding *binding, |
| 76 | mctp_eid_t eid); |
| 77 | |
| 78 | typedef void (*mctp_rx_fn)(uint8_t src_eid, void *data, |
| 79 | void *msg, size_t len); |
| 80 | |
| 81 | int mctp_set_rx_all(struct mctp *mctp, mctp_rx_fn fn, void *data); |
| 82 | |
| 83 | int mctp_message_tx(struct mctp *mctp, mctp_eid_t eid, |
| 84 | void *msg, size_t msg_len); |
| 85 | |
| 86 | /* hardware bindings */ |
| 87 | struct mctp_binding { |
| 88 | const char *name; |
| 89 | uint8_t version; |
Jeremy Kerr | 7520cec | 2019-03-01 07:13:18 +0800 | [diff] [blame] | 90 | struct mctp_bus *bus; |
Jeremy Kerr | 0a00dca | 2019-03-01 08:01:35 +0800 | [diff] [blame] | 91 | struct mctp *mctp; |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 92 | int (*tx)(struct mctp_binding *binding, |
| 93 | struct mctp_pktbuf *pkt); |
| 94 | }; |
| 95 | |
Jeremy Kerr | 1cd3118 | 2019-02-27 18:01:00 +0800 | [diff] [blame] | 96 | void mctp_binding_set_tx_enabled(struct mctp_binding *binding, bool enable); |
| 97 | |
Jeremy Kerr | 0a00dca | 2019-03-01 08:01:35 +0800 | [diff] [blame] | 98 | void mctp_bus_rx(struct mctp_binding *binding, struct mctp_pktbuf *pkt); |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 99 | |
| 100 | /* environment-specific allocation */ |
| 101 | void mctp_set_alloc_ops(void *(*alloc)(size_t), |
| 102 | void (*free)(void *), |
| 103 | void *(realloc)(void *, size_t)); |
| 104 | |
| 105 | |
Deepak Kodihalli | b11ad2c | 2019-02-28 03:42:22 -0600 | [diff] [blame] | 106 | #ifdef __cplusplus |
| 107 | } |
| 108 | #endif |
| 109 | |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 110 | #endif /* _LIBMCTP_H */ |