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 | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 10 | #include <stdint.h> |
| 11 | |
| 12 | typedef uint8_t mctp_eid_t; |
| 13 | |
| 14 | /* MCTP packet definitions */ |
| 15 | struct 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 Tanous | 9f10122 | 2019-02-11 11:22:03 -0800 | [diff] [blame] | 44 | (sizeof(struct mctp_hdr) + MCTP_MTU)) |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 45 | |
| 46 | struct mctp_pktbuf { |
| 47 | unsigned char data[MCTP_PKTBUF_SIZE]; |
| 48 | uint8_t start, end; |
| 49 | uint8_t mctp_hdr_off; |
Jeremy Kerr | e16eaab | 2019-02-08 09:30:10 +0800 | [diff] [blame] | 50 | struct mctp_pktbuf *next; |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 51 | }; |
| 52 | |
| 53 | struct mctp_pktbuf *mctp_pktbuf_alloc(uint8_t len); |
| 54 | void mctp_pktbuf_free(struct mctp_pktbuf *pkt); |
| 55 | struct mctp_hdr *mctp_pktbuf_hdr(struct mctp_pktbuf *pkt); |
| 56 | void *mctp_pktbuf_data(struct mctp_pktbuf *pkt); |
| 57 | uint8_t mctp_pktbuf_size(struct mctp_pktbuf *pkt); |
| 58 | void *mctp_pktbuf_alloc_start(struct mctp_pktbuf *pkt, uint8_t size); |
| 59 | void *mctp_pktbuf_alloc_end(struct mctp_pktbuf *pkt, uint8_t size); |
| 60 | int mctp_pktbuf_push(struct mctp_pktbuf *pkt, void *data, uint8_t len); |
| 61 | |
| 62 | /* MCTP core */ |
| 63 | struct mctp; |
| 64 | struct mctp_binding; |
| 65 | |
| 66 | struct mctp *mctp_init(void); |
| 67 | |
| 68 | unsigned long 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 | int (*tx)(struct mctp_binding *binding, |
| 85 | struct mctp_pktbuf *pkt); |
| 86 | }; |
| 87 | |
| 88 | void mctp_bus_rx(struct mctp *mctp, unsigned long bus_id, |
| 89 | struct mctp_pktbuf *pkt); |
| 90 | |
| 91 | /* environment-specific allocation */ |
| 92 | void mctp_set_alloc_ops(void *(*alloc)(size_t), |
| 93 | void (*free)(void *), |
| 94 | void *(realloc)(void *, size_t)); |
| 95 | |
| 96 | |
Deepak Kodihalli | b11ad2c | 2019-02-28 03:42:22 -0600 | [diff] [blame^] | 97 | #ifdef __cplusplus |
| 98 | } |
| 99 | #endif |
| 100 | |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 101 | #endif /* _LIBMCTP_H */ |