Jeremy Kerr | 3d36ee2 | 2019-05-30 11:15:37 +0800 | [diff] [blame] | 1 | /* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */ |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 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 | |
Andrew Jeffery | 73c268e | 2020-01-30 10:16:09 +1030 | [diff] [blame] | 34 | /* Baseline Transmission Unit and packet size */ |
| 35 | #define MCTP_BTU 64 |
| 36 | #define MCTP_PACKET_SIZE(unit) ((unit) + sizeof(struct mctp_hdr)) |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 37 | |
| 38 | /* packet buffers */ |
| 39 | |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 40 | struct mctp_pktbuf { |
Jeremy Kerr | df15f7e | 2019-08-05 15:41:19 +0800 | [diff] [blame] | 41 | size_t start, end, size; |
| 42 | size_t mctp_hdr_off; |
Jeremy Kerr | e16eaab | 2019-02-08 09:30:10 +0800 | [diff] [blame] | 43 | struct mctp_pktbuf *next; |
Jeremy Kerr | df15f7e | 2019-08-05 15:41:19 +0800 | [diff] [blame] | 44 | unsigned char data[]; |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 45 | }; |
| 46 | |
Jeremy Kerr | df15f7e | 2019-08-05 15:41:19 +0800 | [diff] [blame] | 47 | struct mctp_binding; |
| 48 | |
| 49 | struct mctp_pktbuf *mctp_pktbuf_alloc(struct mctp_binding *hw, size_t len); |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 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); |
Jeremy Kerr | df15f7e | 2019-08-05 15:41:19 +0800 | [diff] [blame] | 54 | void *mctp_pktbuf_alloc_start(struct mctp_pktbuf *pkt, size_t size); |
| 55 | void *mctp_pktbuf_alloc_end(struct mctp_pktbuf *pkt, size_t size); |
| 56 | int mctp_pktbuf_push(struct mctp_pktbuf *pkt, void *data, size_t len); |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 57 | |
| 58 | /* MCTP core */ |
| 59 | struct mctp; |
Jeremy Kerr | 7520cec | 2019-03-01 07:13:18 +0800 | [diff] [blame] | 60 | struct mctp_bus; |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 61 | |
| 62 | struct mctp *mctp_init(void); |
Andrew Jeffery | fa56ca5 | 2020-03-10 23:18:22 +1030 | [diff] [blame] | 63 | void mctp_destroy(struct mctp *mctp); |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 64 | |
Jeremy Kerr | 7520cec | 2019-03-01 07:13:18 +0800 | [diff] [blame] | 65 | /* Register a binding to the MCTP core, and creates a bus (populating |
| 66 | * binding->bus). |
Jeremy Kerr | 1a4ec3c | 2019-09-03 11:01:50 +0800 | [diff] [blame] | 67 | * |
| 68 | * If this function is called, the MCTP stack is initialised as an 'endpoint', |
| 69 | * and will deliver local packets to a RX callback - see `mctp_set_rx_all()` |
| 70 | * below. |
Jeremy Kerr | 7520cec | 2019-03-01 07:13:18 +0800 | [diff] [blame] | 71 | */ |
| 72 | int mctp_register_bus(struct mctp *mctp, |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 73 | struct mctp_binding *binding, |
| 74 | mctp_eid_t eid); |
| 75 | |
Jeremy Kerr | 1a4ec3c | 2019-09-03 11:01:50 +0800 | [diff] [blame] | 76 | /* Create a simple bidirectional bridge between busses. |
| 77 | * |
| 78 | * In this mode, the MCTP stack is initialised as a bridge. There is no EID |
| 79 | * defined, so no packets are considered local. Instead, all messages from one |
| 80 | * binding are forwarded to the other. |
| 81 | */ |
| 82 | int mctp_bridge_busses(struct mctp *mctp, |
| 83 | struct mctp_binding *b1, struct mctp_binding *b2); |
| 84 | |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 85 | typedef void (*mctp_rx_fn)(uint8_t src_eid, void *data, |
| 86 | void *msg, size_t len); |
| 87 | |
| 88 | int mctp_set_rx_all(struct mctp *mctp, mctp_rx_fn fn, void *data); |
| 89 | |
| 90 | int mctp_message_tx(struct mctp *mctp, mctp_eid_t eid, |
| 91 | void *msg, size_t msg_len); |
| 92 | |
| 93 | /* hardware bindings */ |
| 94 | struct mctp_binding { |
| 95 | const char *name; |
| 96 | uint8_t version; |
Jeremy Kerr | 7520cec | 2019-03-01 07:13:18 +0800 | [diff] [blame] | 97 | struct mctp_bus *bus; |
Jeremy Kerr | 0a00dca | 2019-03-01 08:01:35 +0800 | [diff] [blame] | 98 | struct mctp *mctp; |
Jeremy Kerr | df15f7e | 2019-08-05 15:41:19 +0800 | [diff] [blame] | 99 | int pkt_size; |
| 100 | int pkt_pad; |
Jeremy Kerr | 3b36d17 | 2019-09-04 11:56:09 +0800 | [diff] [blame] | 101 | int (*start)(struct mctp_binding *binding); |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 102 | int (*tx)(struct mctp_binding *binding, |
| 103 | struct mctp_pktbuf *pkt); |
| 104 | }; |
| 105 | |
Jeremy Kerr | 1cd3118 | 2019-02-27 18:01:00 +0800 | [diff] [blame] | 106 | void mctp_binding_set_tx_enabled(struct mctp_binding *binding, bool enable); |
| 107 | |
Jeremy Kerr | c1693af | 2019-08-05 14:30:59 +0800 | [diff] [blame] | 108 | /* |
| 109 | * Receive a packet from binding to core. Takes ownership of pkt, free()-ing it |
| 110 | * after use. |
| 111 | */ |
Jeremy Kerr | 0a00dca | 2019-03-01 08:01:35 +0800 | [diff] [blame] | 112 | void mctp_bus_rx(struct mctp_binding *binding, struct mctp_pktbuf *pkt); |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 113 | |
| 114 | /* environment-specific allocation */ |
| 115 | void mctp_set_alloc_ops(void *(*alloc)(size_t), |
| 116 | void (*free)(void *), |
| 117 | void *(realloc)(void *, size_t)); |
| 118 | |
Jeremy Kerr | c7e764a | 2019-05-28 16:49:03 +0800 | [diff] [blame] | 119 | /* environment-specific logging */ |
Jeremy Kerr | cc49e16 | 2019-05-30 21:11:16 +0800 | [diff] [blame] | 120 | |
Jeremy Kerr | c7e764a | 2019-05-28 16:49:03 +0800 | [diff] [blame] | 121 | void mctp_set_log_stdio(int level); |
| 122 | void mctp_set_log_syslog(void); |
| 123 | void mctp_set_log_custom(void (*fn)(int, const char *, va_list)); |
| 124 | |
Jeremy Kerr | cc49e16 | 2019-05-30 21:11:16 +0800 | [diff] [blame] | 125 | /* these should match the syslog-standard LOG_* definitions, for |
| 126 | * easier use with syslog */ |
| 127 | #define MCTP_LOG_ERR 3 |
| 128 | #define MCTP_LOG_WARNING 4 |
| 129 | #define MCTP_LOG_NOTICE 5 |
| 130 | #define MCTP_LOG_INFO 6 |
| 131 | #define MCTP_LOG_DEBUG 7 |
| 132 | |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 133 | |
Deepak Kodihalli | b11ad2c | 2019-02-28 03:42:22 -0600 | [diff] [blame] | 134 | #ifdef __cplusplus |
| 135 | } |
| 136 | #endif |
| 137 | |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 138 | #endif /* _LIBMCTP_H */ |