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 | #include <assert.h> |
Andrew Jeffery | 3e8a12a | 2020-06-05 16:08:30 +0930 | [diff] [blame] | 4 | #include <errno.h> |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 5 | #include <stdarg.h> |
| 6 | #include <stddef.h> |
| 7 | #include <stdint.h> |
| 8 | #include <stdio.h> |
| 9 | #include <stdlib.h> |
| 10 | #include <string.h> |
| 11 | |
| 12 | #undef pr_fmt |
| 13 | #define pr_fmt(fmt) "core: " fmt |
| 14 | |
| 15 | #include "libmctp.h" |
| 16 | #include "libmctp-alloc.h" |
| 17 | #include "libmctp-log.h" |
Wiktor GoĊgowski | ba6727e | 2020-03-13 18:25:01 +0100 | [diff] [blame] | 18 | #include "libmctp-cmds.h" |
Andrew Jeffery | c2b833e | 2020-10-28 14:28:37 +1030 | [diff] [blame] | 19 | #include "range.h" |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 20 | |
| 21 | /* Internal data structures */ |
| 22 | |
Andrew Jeffery | c61501c | 2021-01-27 23:24:18 +1030 | [diff] [blame] | 23 | enum mctp_bus_state { |
| 24 | mctp_bus_state_constructed = 0, |
| 25 | mctp_bus_state_tx_enabled, |
| 26 | mctp_bus_state_tx_disabled, |
| 27 | }; |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 28 | |
Andrew Jeffery | c61501c | 2021-01-27 23:24:18 +1030 | [diff] [blame] | 29 | struct mctp_bus { |
| 30 | mctp_eid_t eid; |
| 31 | struct mctp_binding *binding; |
| 32 | enum mctp_bus_state state; |
| 33 | |
| 34 | struct mctp_pktbuf *tx_queue_head; |
| 35 | struct mctp_pktbuf *tx_queue_tail; |
Jeremy Kerr | cc2458d | 2019-03-01 08:23:33 +0800 | [diff] [blame] | 36 | |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 37 | /* todo: routing */ |
| 38 | }; |
| 39 | |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 40 | struct mctp_msg_ctx { |
| 41 | uint8_t src; |
Jeremy Kerr | 1a4ec3c | 2019-09-03 11:01:50 +0800 | [diff] [blame] | 42 | uint8_t dest; |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 43 | uint8_t tag; |
| 44 | uint8_t last_seq; |
| 45 | void *buf; |
| 46 | size_t buf_size; |
| 47 | size_t buf_alloc_size; |
Sumanth Bhat | 69f545f | 2021-05-18 09:16:43 +0000 | [diff] [blame] | 48 | size_t fragment_size; |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 49 | }; |
| 50 | |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 51 | struct mctp { |
Jeremy Kerr | 1a4ec3c | 2019-09-03 11:01:50 +0800 | [diff] [blame] | 52 | int n_busses; |
| 53 | struct mctp_bus *busses; |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 54 | |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 55 | /* Message RX callback */ |
| 56 | mctp_rx_fn message_rx; |
| 57 | void *message_rx_data; |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 58 | |
Andrew Jeffery | 5d3d4e6 | 2021-08-20 16:44:40 +0930 | [diff] [blame] | 59 | /* Packet capture callback */ |
| 60 | mctp_capture_fn capture; |
| 61 | void *capture_data; |
| 62 | |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 63 | /* Message reassembly. |
| 64 | * @todo: flexible context count |
| 65 | */ |
| 66 | struct mctp_msg_ctx msg_ctxs[16]; |
Jeremy Kerr | 1a4ec3c | 2019-09-03 11:01:50 +0800 | [diff] [blame] | 67 | |
| 68 | enum { |
| 69 | ROUTE_ENDPOINT, |
| 70 | ROUTE_BRIDGE, |
| 71 | } route_policy; |
Sumanth Bhat | 2c820c5 | 2020-07-02 00:26:25 +0530 | [diff] [blame] | 72 | size_t max_message_size; |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | #ifndef BUILD_ASSERT |
| 76 | #define BUILD_ASSERT(x) \ |
| 77 | do { (void)sizeof(char[0-(!(x))]); } while (0) |
| 78 | #endif |
| 79 | |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 80 | #ifndef ARRAY_SIZE |
| 81 | #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) |
| 82 | #endif |
| 83 | |
Sumanth Bhat | 2c820c5 | 2020-07-02 00:26:25 +0530 | [diff] [blame] | 84 | /* 64kb should be sufficient for a single message. Applications |
| 85 | * requiring higher sizes can override by setting max_message_size.*/ |
| 86 | #ifndef MCTP_MAX_MESSAGE_SIZE |
| 87 | #define MCTP_MAX_MESSAGE_SIZE 65536 |
| 88 | #endif |
| 89 | |
Andrew Jeffery | b93b611 | 2020-06-05 14:13:44 +0930 | [diff] [blame] | 90 | static int mctp_message_tx_on_bus(struct mctp_bus *bus, mctp_eid_t src, |
Sumanth Bhat | f39c385 | 2022-01-10 17:04:10 +0530 | [diff] [blame^] | 91 | mctp_eid_t dest, bool tag_owner, |
| 92 | uint8_t msg_tag, void *msg, size_t msg_len); |
Jeremy Kerr | 1a4ec3c | 2019-09-03 11:01:50 +0800 | [diff] [blame] | 93 | |
Jeremy Kerr | df15f7e | 2019-08-05 15:41:19 +0800 | [diff] [blame] | 94 | struct mctp_pktbuf *mctp_pktbuf_alloc(struct mctp_binding *binding, size_t len) |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 95 | { |
| 96 | struct mctp_pktbuf *buf; |
Jeremy Kerr | df15f7e | 2019-08-05 15:41:19 +0800 | [diff] [blame] | 97 | size_t size; |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 98 | |
Andrew Jeffery | 39da3d0 | 2021-03-12 16:51:26 +1030 | [diff] [blame] | 99 | size = binding->pkt_size + binding->pkt_header + binding->pkt_trailer; |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 100 | |
| 101 | /* todo: pools */ |
Jeremy Kerr | df15f7e | 2019-08-05 15:41:19 +0800 | [diff] [blame] | 102 | buf = __mctp_alloc(sizeof(*buf) + size); |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 103 | |
Jeremy Kerr | df15f7e | 2019-08-05 15:41:19 +0800 | [diff] [blame] | 104 | buf->size = size; |
Andrew Jeffery | 39da3d0 | 2021-03-12 16:51:26 +1030 | [diff] [blame] | 105 | buf->start = binding->pkt_header; |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 106 | buf->end = buf->start + len; |
| 107 | buf->mctp_hdr_off = buf->start; |
Jeremy Kerr | dd109f1 | 2019-04-04 11:46:49 +0800 | [diff] [blame] | 108 | buf->next = NULL; |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 109 | |
| 110 | return buf; |
| 111 | } |
| 112 | |
| 113 | void mctp_pktbuf_free(struct mctp_pktbuf *pkt) |
| 114 | { |
| 115 | __mctp_free(pkt); |
| 116 | } |
| 117 | |
| 118 | struct mctp_hdr *mctp_pktbuf_hdr(struct mctp_pktbuf *pkt) |
| 119 | { |
| 120 | return (void *)pkt->data + pkt->mctp_hdr_off; |
| 121 | } |
| 122 | |
| 123 | void *mctp_pktbuf_data(struct mctp_pktbuf *pkt) |
| 124 | { |
| 125 | return (void *)pkt->data + pkt->mctp_hdr_off + sizeof(struct mctp_hdr); |
| 126 | } |
| 127 | |
Andrew Jeffery | b942e3a | 2020-06-23 09:54:02 +0930 | [diff] [blame] | 128 | size_t mctp_pktbuf_size(struct mctp_pktbuf *pkt) |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 129 | { |
| 130 | return pkt->end - pkt->start; |
| 131 | } |
| 132 | |
Jeremy Kerr | df15f7e | 2019-08-05 15:41:19 +0800 | [diff] [blame] | 133 | void *mctp_pktbuf_alloc_start(struct mctp_pktbuf *pkt, size_t size) |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 134 | { |
| 135 | assert(size <= pkt->start); |
| 136 | pkt->start -= size; |
| 137 | return pkt->data + pkt->start; |
| 138 | } |
| 139 | |
Jeremy Kerr | df15f7e | 2019-08-05 15:41:19 +0800 | [diff] [blame] | 140 | void *mctp_pktbuf_alloc_end(struct mctp_pktbuf *pkt, size_t size) |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 141 | { |
| 142 | void *buf; |
| 143 | |
Andrew Jeffery | 3ac70d6 | 2020-07-01 00:50:44 +0930 | [diff] [blame] | 144 | assert(size <= (pkt->size - pkt->end)); |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 145 | buf = pkt->data + pkt->end; |
| 146 | pkt->end += size; |
| 147 | return buf; |
| 148 | } |
| 149 | |
Jeremy Kerr | df15f7e | 2019-08-05 15:41:19 +0800 | [diff] [blame] | 150 | 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] | 151 | { |
| 152 | void *p; |
| 153 | |
Jeremy Kerr | df15f7e | 2019-08-05 15:41:19 +0800 | [diff] [blame] | 154 | if (pkt->end + len > pkt->size) |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 155 | return -1; |
| 156 | |
| 157 | p = pkt->data + pkt->end; |
| 158 | |
| 159 | pkt->end += len; |
| 160 | memcpy(p, data, len); |
| 161 | |
| 162 | return 0; |
| 163 | } |
| 164 | |
Andrew Jeffery | eba19a3 | 2021-03-09 23:09:40 +1030 | [diff] [blame] | 165 | void *mctp_pktbuf_pop(struct mctp_pktbuf *pkt, size_t len) |
| 166 | { |
| 167 | if (len > mctp_pktbuf_size(pkt)) |
| 168 | return NULL; |
| 169 | |
| 170 | pkt->end -= len; |
| 171 | return pkt->data + pkt->end; |
| 172 | } |
| 173 | |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 174 | /* Message reassembly */ |
| 175 | static struct mctp_msg_ctx *mctp_msg_ctx_lookup(struct mctp *mctp, |
Jeremy Kerr | 1a4ec3c | 2019-09-03 11:01:50 +0800 | [diff] [blame] | 176 | uint8_t src, uint8_t dest, uint8_t tag) |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 177 | { |
| 178 | unsigned int i; |
| 179 | |
| 180 | /* @todo: better lookup, if we add support for more outstanding |
| 181 | * message contexts */ |
| 182 | for (i = 0; i < ARRAY_SIZE(mctp->msg_ctxs); i++) { |
| 183 | struct mctp_msg_ctx *ctx = &mctp->msg_ctxs[i]; |
Jeremy Kerr | 1a4ec3c | 2019-09-03 11:01:50 +0800 | [diff] [blame] | 184 | if (ctx->src == src && ctx->dest == dest && ctx->tag == tag) |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 185 | return ctx; |
| 186 | } |
| 187 | |
| 188 | return NULL; |
| 189 | } |
| 190 | |
| 191 | static struct mctp_msg_ctx *mctp_msg_ctx_create(struct mctp *mctp, |
Jeremy Kerr | 1a4ec3c | 2019-09-03 11:01:50 +0800 | [diff] [blame] | 192 | uint8_t src, uint8_t dest, uint8_t tag) |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 193 | { |
Jeremy Kerr | 11a234e | 2019-02-27 17:59:53 +0800 | [diff] [blame] | 194 | struct mctp_msg_ctx *ctx = NULL; |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 195 | unsigned int i; |
| 196 | |
| 197 | for (i = 0; i < ARRAY_SIZE(mctp->msg_ctxs); i++) { |
| 198 | struct mctp_msg_ctx *tmp = &mctp->msg_ctxs[i]; |
| 199 | if (!tmp->src) { |
| 200 | ctx = tmp; |
| 201 | break; |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | if (!ctx) |
| 206 | return NULL; |
| 207 | |
| 208 | ctx->src = src; |
Jeremy Kerr | 1a4ec3c | 2019-09-03 11:01:50 +0800 | [diff] [blame] | 209 | ctx->dest = dest; |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 210 | ctx->tag = tag; |
Jeremy Kerr | 9a3da81 | 2019-08-02 15:57:53 +0800 | [diff] [blame] | 211 | ctx->buf_size = 0; |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 212 | |
| 213 | return ctx; |
| 214 | } |
| 215 | |
| 216 | static void mctp_msg_ctx_drop(struct mctp_msg_ctx *ctx) |
| 217 | { |
| 218 | ctx->src = 0; |
| 219 | } |
| 220 | |
| 221 | static void mctp_msg_ctx_reset(struct mctp_msg_ctx *ctx) |
| 222 | { |
| 223 | ctx->buf_size = 0; |
Sumanth Bhat | 69f545f | 2021-05-18 09:16:43 +0000 | [diff] [blame] | 224 | ctx->fragment_size = 0; |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | static int mctp_msg_ctx_add_pkt(struct mctp_msg_ctx *ctx, |
Sumanth Bhat | 2c820c5 | 2020-07-02 00:26:25 +0530 | [diff] [blame] | 228 | struct mctp_pktbuf *pkt, size_t max_size) |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 229 | { |
| 230 | size_t len; |
| 231 | |
| 232 | len = mctp_pktbuf_size(pkt) - sizeof(struct mctp_hdr); |
| 233 | |
Sumanth Bhat | bc79c24 | 2021-06-16 12:36:56 +0530 | [diff] [blame] | 234 | if (len + ctx->buf_size < ctx->buf_size) { |
| 235 | return -1; |
| 236 | } |
| 237 | |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 238 | if (ctx->buf_size + len > ctx->buf_alloc_size) { |
| 239 | size_t new_alloc_size; |
Andrew Jeffery | 00ecc6c | 2020-03-10 23:16:53 +1030 | [diff] [blame] | 240 | void *lbuf; |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 241 | |
Andrew Jeffery | 5a50891 | 2020-11-03 22:21:45 +1030 | [diff] [blame] | 242 | /* @todo: finer-grained allocation */ |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 243 | if (!ctx->buf_alloc_size) { |
Andrew Jeffery | c2b833e | 2020-10-28 14:28:37 +1030 | [diff] [blame] | 244 | new_alloc_size = MAX(len, 4096UL); |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 245 | } else { |
Sumanth Bhat | bc79c24 | 2021-06-16 12:36:56 +0530 | [diff] [blame] | 246 | new_alloc_size = MAX(ctx->buf_alloc_size * 2, len + ctx->buf_size); |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 247 | } |
Andrew Jeffery | 00ecc6c | 2020-03-10 23:16:53 +1030 | [diff] [blame] | 248 | |
Sumanth Bhat | 2c820c5 | 2020-07-02 00:26:25 +0530 | [diff] [blame] | 249 | /* Don't allow heap to grow beyond a limit */ |
| 250 | if (new_alloc_size > max_size) |
| 251 | return -1; |
| 252 | |
| 253 | |
Andrew Jeffery | 00ecc6c | 2020-03-10 23:16:53 +1030 | [diff] [blame] | 254 | lbuf = __mctp_realloc(ctx->buf, new_alloc_size); |
| 255 | if (lbuf) { |
| 256 | ctx->buf = lbuf; |
| 257 | ctx->buf_alloc_size = new_alloc_size; |
| 258 | } else { |
| 259 | __mctp_free(ctx->buf); |
| 260 | return -1; |
| 261 | } |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | memcpy(ctx->buf + ctx->buf_size, mctp_pktbuf_data(pkt), len); |
| 265 | ctx->buf_size += len; |
| 266 | |
| 267 | return 0; |
| 268 | } |
| 269 | |
| 270 | /* Core API functions */ |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 271 | struct mctp *mctp_init(void) |
| 272 | { |
| 273 | struct mctp *mctp; |
| 274 | |
| 275 | mctp = __mctp_alloc(sizeof(*mctp)); |
Sumanth Bhat | 96d5449 | 2020-07-14 17:10:04 +0530 | [diff] [blame] | 276 | |
| 277 | if(!mctp) |
| 278 | return NULL; |
| 279 | |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 280 | memset(mctp, 0, sizeof(*mctp)); |
Sumanth Bhat | 2c820c5 | 2020-07-02 00:26:25 +0530 | [diff] [blame] | 281 | mctp->max_message_size = MCTP_MAX_MESSAGE_SIZE; |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 282 | |
| 283 | return mctp; |
| 284 | } |
| 285 | |
Sumanth Bhat | 2c820c5 | 2020-07-02 00:26:25 +0530 | [diff] [blame] | 286 | void mctp_set_max_message_size(struct mctp *mctp, size_t message_size) |
| 287 | { |
| 288 | mctp->max_message_size = message_size; |
| 289 | } |
| 290 | |
Andrew Jeffery | 5d3d4e6 | 2021-08-20 16:44:40 +0930 | [diff] [blame] | 291 | void mctp_set_capture_handler(struct mctp *mctp, mctp_capture_fn fn, void *user) |
| 292 | { |
| 293 | mctp->capture = fn; |
| 294 | mctp->capture_data = user; |
| 295 | } |
| 296 | |
Andrew Jeffery | 3ae89dc | 2021-01-28 15:24:36 +1030 | [diff] [blame] | 297 | static void mctp_bus_destroy(struct mctp_bus *bus) |
| 298 | { |
| 299 | while (bus->tx_queue_head) { |
| 300 | struct mctp_pktbuf *curr = bus->tx_queue_head; |
| 301 | |
| 302 | bus->tx_queue_head = curr->next; |
| 303 | mctp_pktbuf_free(curr); |
| 304 | } |
| 305 | } |
| 306 | |
Andrew Jeffery | fa56ca5 | 2020-03-10 23:18:22 +1030 | [diff] [blame] | 307 | void mctp_destroy(struct mctp *mctp) |
| 308 | { |
Andrew Jeffery | b93b611 | 2020-06-05 14:13:44 +0930 | [diff] [blame] | 309 | size_t i; |
Andrew Jeffery | fa56ca5 | 2020-03-10 23:18:22 +1030 | [diff] [blame] | 310 | |
| 311 | /* Cleanup message assembly contexts */ |
Andrew Jeffery | b93b611 | 2020-06-05 14:13:44 +0930 | [diff] [blame] | 312 | BUILD_ASSERT(ARRAY_SIZE(mctp->msg_ctxs) < SIZE_MAX); |
Andrew Jeffery | fa56ca5 | 2020-03-10 23:18:22 +1030 | [diff] [blame] | 313 | for (i = 0; i < ARRAY_SIZE(mctp->msg_ctxs); i++) { |
| 314 | struct mctp_msg_ctx *tmp = &mctp->msg_ctxs[i]; |
| 315 | if (tmp->buf) |
| 316 | __mctp_free(tmp->buf); |
| 317 | } |
| 318 | |
Andrew Jeffery | 3ae89dc | 2021-01-28 15:24:36 +1030 | [diff] [blame] | 319 | while (mctp->n_busses--) |
| 320 | mctp_bus_destroy(&mctp->busses[mctp->n_busses]); |
| 321 | |
Andrew Jeffery | fa56ca5 | 2020-03-10 23:18:22 +1030 | [diff] [blame] | 322 | __mctp_free(mctp->busses); |
| 323 | __mctp_free(mctp); |
| 324 | } |
| 325 | |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 326 | int mctp_set_rx_all(struct mctp *mctp, mctp_rx_fn fn, void *data) |
| 327 | { |
| 328 | mctp->message_rx = fn; |
| 329 | mctp->message_rx_data = data; |
| 330 | return 0; |
| 331 | } |
| 332 | |
| 333 | static struct mctp_bus *find_bus_for_eid(struct mctp *mctp, |
| 334 | mctp_eid_t dest __attribute__((unused))) |
| 335 | { |
Brad Bishop | 663ec39 | 2021-10-07 21:16:48 -0400 | [diff] [blame] | 336 | if (mctp->n_busses == 0) |
| 337 | return NULL; |
| 338 | |
Jeremy Kerr | 1a4ec3c | 2019-09-03 11:01:50 +0800 | [diff] [blame] | 339 | /* for now, just use the first bus. For full routing support, |
| 340 | * we will need a table of neighbours */ |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 341 | return &mctp->busses[0]; |
| 342 | } |
| 343 | |
Jeremy Kerr | 7520cec | 2019-03-01 07:13:18 +0800 | [diff] [blame] | 344 | int mctp_register_bus(struct mctp *mctp, |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 345 | struct mctp_binding *binding, |
| 346 | mctp_eid_t eid) |
| 347 | { |
Andrew Jeffery | 3e8a12a | 2020-06-05 16:08:30 +0930 | [diff] [blame] | 348 | int rc = 0; |
| 349 | |
Jeremy Kerr | 7520cec | 2019-03-01 07:13:18 +0800 | [diff] [blame] | 350 | /* todo: multiple busses */ |
Jeremy Kerr | 1a4ec3c | 2019-09-03 11:01:50 +0800 | [diff] [blame] | 351 | assert(mctp->n_busses == 0); |
| 352 | mctp->n_busses = 1; |
Andrew Jeffery | 3e8a12a | 2020-06-05 16:08:30 +0930 | [diff] [blame] | 353 | |
Jeremy Kerr | 1a4ec3c | 2019-09-03 11:01:50 +0800 | [diff] [blame] | 354 | mctp->busses = __mctp_alloc(sizeof(struct mctp_bus)); |
Andrew Jeffery | 3e8a12a | 2020-06-05 16:08:30 +0930 | [diff] [blame] | 355 | if (!mctp->busses) |
| 356 | return -ENOMEM; |
| 357 | |
James Feist | 62d7236 | 2019-12-13 13:43:32 -0800 | [diff] [blame] | 358 | memset(mctp->busses, 0, sizeof(struct mctp_bus)); |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 359 | mctp->busses[0].binding = binding; |
| 360 | mctp->busses[0].eid = eid; |
Jeremy Kerr | 7520cec | 2019-03-01 07:13:18 +0800 | [diff] [blame] | 361 | binding->bus = &mctp->busses[0]; |
Jeremy Kerr | 0a00dca | 2019-03-01 08:01:35 +0800 | [diff] [blame] | 362 | binding->mctp = mctp; |
Jeremy Kerr | 1a4ec3c | 2019-09-03 11:01:50 +0800 | [diff] [blame] | 363 | mctp->route_policy = ROUTE_ENDPOINT; |
Jeremy Kerr | 3b36d17 | 2019-09-04 11:56:09 +0800 | [diff] [blame] | 364 | |
Andrew Jeffery | 3e8a12a | 2020-06-05 16:08:30 +0930 | [diff] [blame] | 365 | if (binding->start) { |
| 366 | rc = binding->start(binding); |
| 367 | if (rc < 0) { |
| 368 | mctp_prerr("Failed to start binding: %d", rc); |
Andrew Jeffery | 1927523 | 2021-01-29 14:13:25 +1030 | [diff] [blame] | 369 | binding->bus = NULL; |
Andrew Jeffery | 3e8a12a | 2020-06-05 16:08:30 +0930 | [diff] [blame] | 370 | __mctp_free(mctp->busses); |
| 371 | mctp->busses = NULL; |
Andrew Jeffery | 2304c83 | 2021-01-29 11:52:49 +1030 | [diff] [blame] | 372 | mctp->n_busses = 0; |
Andrew Jeffery | 3e8a12a | 2020-06-05 16:08:30 +0930 | [diff] [blame] | 373 | } |
| 374 | } |
Jeremy Kerr | 3b36d17 | 2019-09-04 11:56:09 +0800 | [diff] [blame] | 375 | |
Andrew Jeffery | 3e8a12a | 2020-06-05 16:08:30 +0930 | [diff] [blame] | 376 | return rc; |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 377 | } |
| 378 | |
Andrew Jeffery | 2094c3c | 2021-08-26 12:32:46 +0930 | [diff] [blame] | 379 | void mctp_unregister_bus(struct mctp *mctp, struct mctp_binding *binding) |
| 380 | { |
| 381 | /* |
| 382 | * We only support one bus right now; once the call completes we will |
| 383 | * have no more busses |
| 384 | */ |
| 385 | mctp->n_busses = 0; |
| 386 | binding->mctp = NULL; |
| 387 | binding->bus = NULL; |
| 388 | free(mctp->busses); |
| 389 | } |
| 390 | |
Jeremy Kerr | 1a4ec3c | 2019-09-03 11:01:50 +0800 | [diff] [blame] | 391 | int mctp_bridge_busses(struct mctp *mctp, |
| 392 | struct mctp_binding *b1, struct mctp_binding *b2) |
| 393 | { |
Andrew Jeffery | 1927523 | 2021-01-29 14:13:25 +1030 | [diff] [blame] | 394 | int rc = 0; |
| 395 | |
Jeremy Kerr | 1a4ec3c | 2019-09-03 11:01:50 +0800 | [diff] [blame] | 396 | assert(mctp->n_busses == 0); |
| 397 | mctp->busses = __mctp_alloc(2 * sizeof(struct mctp_bus)); |
Helen Huang | a523bcc | 2021-05-19 15:44:56 +0800 | [diff] [blame] | 398 | if (!mctp->busses) |
| 399 | return -ENOMEM; |
James Feist | 62d7236 | 2019-12-13 13:43:32 -0800 | [diff] [blame] | 400 | memset(mctp->busses, 0, 2 * sizeof(struct mctp_bus)); |
Jeremy Kerr | 1a4ec3c | 2019-09-03 11:01:50 +0800 | [diff] [blame] | 401 | mctp->n_busses = 2; |
| 402 | mctp->busses[0].binding = b1; |
| 403 | b1->bus = &mctp->busses[0]; |
| 404 | b1->mctp = mctp; |
| 405 | mctp->busses[1].binding = b2; |
| 406 | b2->bus = &mctp->busses[1]; |
| 407 | b2->mctp = mctp; |
| 408 | |
| 409 | mctp->route_policy = ROUTE_BRIDGE; |
Jeremy Kerr | 3b36d17 | 2019-09-04 11:56:09 +0800 | [diff] [blame] | 410 | |
Andrew Jeffery | 1927523 | 2021-01-29 14:13:25 +1030 | [diff] [blame] | 411 | if (b1->start) { |
| 412 | rc = b1->start(b1); |
| 413 | if (rc < 0) { |
| 414 | mctp_prerr("Failed to start bridged bus %s: %d", |
| 415 | b1->name, rc); |
| 416 | goto done; |
| 417 | } |
| 418 | } |
Jeremy Kerr | 3b36d17 | 2019-09-04 11:56:09 +0800 | [diff] [blame] | 419 | |
Andrew Jeffery | 1927523 | 2021-01-29 14:13:25 +1030 | [diff] [blame] | 420 | if (b2->start) { |
| 421 | rc = b2->start(b2); |
| 422 | if (rc < 0) { |
| 423 | mctp_prerr("Failed to start bridged bus %s: %d", |
| 424 | b2->name, rc); |
| 425 | goto done; |
| 426 | } |
| 427 | } |
Jeremy Kerr | 3b36d17 | 2019-09-04 11:56:09 +0800 | [diff] [blame] | 428 | |
Andrew Jeffery | 1927523 | 2021-01-29 14:13:25 +1030 | [diff] [blame] | 429 | done: |
| 430 | return rc; |
Jeremy Kerr | 1a4ec3c | 2019-09-03 11:01:50 +0800 | [diff] [blame] | 431 | } |
| 432 | |
Wiktor GoĊgowski | ba6727e | 2020-03-13 18:25:01 +0100 | [diff] [blame] | 433 | static inline bool mctp_ctrl_cmd_is_transport(struct mctp_ctrl_msg_hdr *hdr) |
Jeremy Kerr | 1a4ec3c | 2019-09-03 11:01:50 +0800 | [diff] [blame] | 434 | { |
Wiktor GoĊgowski | ba6727e | 2020-03-13 18:25:01 +0100 | [diff] [blame] | 435 | return ((hdr->command_code >= MCTP_CTRL_CMD_FIRST_TRANSPORT) && |
| 436 | (hdr->command_code <= MCTP_CTRL_CMD_LAST_TRANSPORT)); |
| 437 | } |
| 438 | |
Andrew Jeffery | b93b611 | 2020-06-05 14:13:44 +0930 | [diff] [blame] | 439 | static bool mctp_ctrl_handle_msg(struct mctp_bus *bus, mctp_eid_t src, |
Sumanth Bhat | f39c385 | 2022-01-10 17:04:10 +0530 | [diff] [blame^] | 440 | uint8_t msg_tag, bool tag_owner, void *buffer, |
| 441 | size_t length) |
Wiktor GoĊgowski | ba6727e | 2020-03-13 18:25:01 +0100 | [diff] [blame] | 442 | { |
| 443 | struct mctp_ctrl_msg_hdr *msg_hdr = buffer; |
| 444 | |
| 445 | /* |
| 446 | * Control message is received. If a transport control message handler |
| 447 | * is provided, it will called. If there is no dedicated handler, this |
| 448 | * function returns false and data can be handled by the generic |
| 449 | * message handler. The transport control message handler will be |
| 450 | * provided with messages in the command range 0xF0 - 0xFF. |
| 451 | */ |
| 452 | if (mctp_ctrl_cmd_is_transport(msg_hdr)) { |
| 453 | if (bus->binding->control_rx != NULL) { |
| 454 | /* MCTP bus binding handler */ |
Sumanth Bhat | f39c385 | 2022-01-10 17:04:10 +0530 | [diff] [blame^] | 455 | bus->binding->control_rx(src, msg_tag, tag_owner, |
Wiktor GoĊgowski | ba6727e | 2020-03-13 18:25:01 +0100 | [diff] [blame] | 456 | bus->binding->control_rx_data, |
| 457 | buffer, length); |
| 458 | return true; |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | /* |
| 463 | * Command was not handled, due to lack of specific callback. |
| 464 | * It will be passed to regular message_rx handler. |
| 465 | */ |
| 466 | return false; |
| 467 | } |
| 468 | |
| 469 | static inline bool mctp_rx_dest_is_local(struct mctp_bus *bus, mctp_eid_t dest) |
| 470 | { |
| 471 | return dest == bus->eid || dest == MCTP_EID_NULL || |
| 472 | dest == MCTP_EID_BROADCAST; |
| 473 | } |
| 474 | |
| 475 | static inline bool mctp_ctrl_cmd_is_request(struct mctp_ctrl_msg_hdr *hdr) |
| 476 | { |
| 477 | return hdr->ic_msg_type == MCTP_CTRL_HDR_MSG_TYPE && |
| 478 | hdr->rq_dgram_inst & MCTP_CTRL_HDR_FLAG_REQUEST; |
| 479 | } |
| 480 | |
| 481 | /* |
| 482 | * Receive the complete MCTP message and route it. |
| 483 | * Asserts: |
| 484 | * 'buf' is not NULL. |
| 485 | */ |
| 486 | static void mctp_rx(struct mctp *mctp, struct mctp_bus *bus, mctp_eid_t src, |
Sumanth Bhat | f39c385 | 2022-01-10 17:04:10 +0530 | [diff] [blame^] | 487 | mctp_eid_t dest, bool tag_owner, uint8_t msg_tag, void *buf, |
| 488 | size_t len) |
Wiktor GoĊgowski | ba6727e | 2020-03-13 18:25:01 +0100 | [diff] [blame] | 489 | { |
| 490 | assert(buf != NULL); |
| 491 | |
Jeremy Kerr | 1a4ec3c | 2019-09-03 11:01:50 +0800 | [diff] [blame] | 492 | if (mctp->route_policy == ROUTE_ENDPOINT && |
Wiktor GoĊgowski | ba6727e | 2020-03-13 18:25:01 +0100 | [diff] [blame] | 493 | mctp_rx_dest_is_local(bus, dest)) { |
| 494 | /* Handle MCTP Control Messages: */ |
| 495 | if (len >= sizeof(struct mctp_ctrl_msg_hdr)) { |
| 496 | struct mctp_ctrl_msg_hdr *msg_hdr = buf; |
| 497 | |
| 498 | /* |
| 499 | * Identify if this is a control request message. |
| 500 | * See DSP0236 v1.3.0 sec. 11.5. |
| 501 | */ |
| 502 | if (mctp_ctrl_cmd_is_request(msg_hdr)) { |
| 503 | bool handled; |
Sumanth Bhat | f39c385 | 2022-01-10 17:04:10 +0530 | [diff] [blame^] | 504 | handled = mctp_ctrl_handle_msg( |
| 505 | bus, src, msg_tag, tag_owner, buf, len); |
Wiktor GoĊgowski | ba6727e | 2020-03-13 18:25:01 +0100 | [diff] [blame] | 506 | if (handled) |
| 507 | return; |
| 508 | } |
| 509 | } |
Sumanth Bhat | f39c385 | 2022-01-10 17:04:10 +0530 | [diff] [blame^] | 510 | |
Wiktor GoĊgowski | ba6727e | 2020-03-13 18:25:01 +0100 | [diff] [blame] | 511 | if (mctp->message_rx) |
Sumanth Bhat | f39c385 | 2022-01-10 17:04:10 +0530 | [diff] [blame^] | 512 | mctp->message_rx(src, tag_owner, msg_tag, |
| 513 | mctp->message_rx_data, buf, len); |
Wiktor GoĊgowski | ba6727e | 2020-03-13 18:25:01 +0100 | [diff] [blame] | 514 | } |
Jeremy Kerr | 1a4ec3c | 2019-09-03 11:01:50 +0800 | [diff] [blame] | 515 | |
| 516 | if (mctp->route_policy == ROUTE_BRIDGE) { |
| 517 | int i; |
| 518 | |
| 519 | for (i = 0; i < mctp->n_busses; i++) { |
| 520 | struct mctp_bus *dest_bus = &mctp->busses[i]; |
| 521 | if (dest_bus == bus) |
| 522 | continue; |
| 523 | |
Sumanth Bhat | f39c385 | 2022-01-10 17:04:10 +0530 | [diff] [blame^] | 524 | mctp_message_tx_on_bus(dest_bus, src, dest, tag_owner, |
| 525 | msg_tag, buf, len); |
Jeremy Kerr | 1a4ec3c | 2019-09-03 11:01:50 +0800 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | } |
| 529 | } |
| 530 | |
Jeremy Kerr | 0a00dca | 2019-03-01 08:01:35 +0800 | [diff] [blame] | 531 | void mctp_bus_rx(struct mctp_binding *binding, struct mctp_pktbuf *pkt) |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 532 | { |
Jeremy Kerr | 7520cec | 2019-03-01 07:13:18 +0800 | [diff] [blame] | 533 | struct mctp_bus *bus = binding->bus; |
Jeremy Kerr | 0a00dca | 2019-03-01 08:01:35 +0800 | [diff] [blame] | 534 | struct mctp *mctp = binding->mctp; |
Ed Tanous | c2def9f | 2019-02-21 08:33:08 -0800 | [diff] [blame] | 535 | uint8_t flags, exp_seq, seq, tag; |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 536 | struct mctp_msg_ctx *ctx; |
| 537 | struct mctp_hdr *hdr; |
Sumanth Bhat | f39c385 | 2022-01-10 17:04:10 +0530 | [diff] [blame^] | 538 | bool tag_owner; |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 539 | size_t len; |
| 540 | void *p; |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 541 | int rc; |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 542 | |
Jeremy Kerr | 7520cec | 2019-03-01 07:13:18 +0800 | [diff] [blame] | 543 | assert(bus); |
| 544 | |
Sumanth Bhat | d97869d | 2020-07-02 00:46:13 +0530 | [diff] [blame] | 545 | /* Drop packet if it was smaller than mctp hdr size */ |
| 546 | if (mctp_pktbuf_size(pkt) <= sizeof(struct mctp_hdr)) |
| 547 | goto out; |
| 548 | |
Andrew Jeffery | 5d3d4e6 | 2021-08-20 16:44:40 +0930 | [diff] [blame] | 549 | if (mctp->capture) |
| 550 | mctp->capture(pkt, mctp->capture_data); |
| 551 | |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 552 | hdr = mctp_pktbuf_hdr(pkt); |
| 553 | |
Jeremy Kerr | 1a4ec3c | 2019-09-03 11:01:50 +0800 | [diff] [blame] | 554 | /* small optimisation: don't bother reassembly if we're going to |
| 555 | * drop the packet in mctp_rx anyway */ |
| 556 | if (mctp->route_policy == ROUTE_ENDPOINT && hdr->dest != bus->eid) |
Jeremy Kerr | c1693af | 2019-08-05 14:30:59 +0800 | [diff] [blame] | 557 | goto out; |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 558 | |
| 559 | flags = hdr->flags_seq_tag & (MCTP_HDR_FLAG_SOM | MCTP_HDR_FLAG_EOM); |
| 560 | tag = (hdr->flags_seq_tag >> MCTP_HDR_TAG_SHIFT) & MCTP_HDR_TAG_MASK; |
| 561 | seq = (hdr->flags_seq_tag >> MCTP_HDR_SEQ_SHIFT) & MCTP_HDR_SEQ_MASK; |
Sumanth Bhat | f39c385 | 2022-01-10 17:04:10 +0530 | [diff] [blame^] | 562 | tag_owner = |
| 563 | (hdr->flags_seq_tag >> MCTP_HDR_TO_SHIFT) & MCTP_HDR_TO_MASK; |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 564 | |
| 565 | switch (flags) { |
| 566 | case MCTP_HDR_FLAG_SOM | MCTP_HDR_FLAG_EOM: |
| 567 | /* single-packet message - send straight up to rx function, |
| 568 | * no need to create a message context */ |
| 569 | len = pkt->end - pkt->mctp_hdr_off - sizeof(struct mctp_hdr); |
Andrew Jeffery | b4ae00b | 2021-01-18 15:52:09 +1030 | [diff] [blame] | 570 | p = pkt->data + pkt->mctp_hdr_off + sizeof(struct mctp_hdr); |
Sumanth Bhat | f39c385 | 2022-01-10 17:04:10 +0530 | [diff] [blame^] | 571 | mctp_rx(mctp, bus, hdr->src, hdr->dest, tag_owner, tag, p, len); |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 572 | break; |
| 573 | |
| 574 | case MCTP_HDR_FLAG_SOM: |
| 575 | /* start of a new message - start the new context for |
| 576 | * future message reception. If an existing context is |
| 577 | * already present, drop it. */ |
Jeremy Kerr | 1a4ec3c | 2019-09-03 11:01:50 +0800 | [diff] [blame] | 578 | ctx = mctp_msg_ctx_lookup(mctp, hdr->src, hdr->dest, tag); |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 579 | if (ctx) { |
| 580 | mctp_msg_ctx_reset(ctx); |
| 581 | } else { |
Jeremy Kerr | 1a4ec3c | 2019-09-03 11:01:50 +0800 | [diff] [blame] | 582 | ctx = mctp_msg_ctx_create(mctp, |
| 583 | hdr->src, hdr->dest, tag); |
Sumanth Bhat | 34d4c96 | 2021-06-16 12:50:48 +0530 | [diff] [blame] | 584 | /* If context creation fails due to exhaution of contexts we |
| 585 | * can support, drop the packet */ |
| 586 | if (!ctx) { |
| 587 | mctp_prdebug("Context buffers exhausted."); |
| 588 | goto out; |
| 589 | } |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 590 | } |
| 591 | |
Sumanth Bhat | 69f545f | 2021-05-18 09:16:43 +0000 | [diff] [blame] | 592 | /* Save the fragment size, subsequent middle fragments |
| 593 | * should of the same size */ |
| 594 | ctx->fragment_size = mctp_pktbuf_size(pkt); |
| 595 | |
Sumanth Bhat | 2c820c5 | 2020-07-02 00:26:25 +0530 | [diff] [blame] | 596 | rc = mctp_msg_ctx_add_pkt(ctx, pkt, mctp->max_message_size); |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 597 | if (rc) { |
| 598 | mctp_msg_ctx_drop(ctx); |
| 599 | } else { |
| 600 | ctx->last_seq = seq; |
| 601 | } |
| 602 | |
| 603 | break; |
| 604 | |
| 605 | case MCTP_HDR_FLAG_EOM: |
Jeremy Kerr | 1a4ec3c | 2019-09-03 11:01:50 +0800 | [diff] [blame] | 606 | ctx = mctp_msg_ctx_lookup(mctp, hdr->src, hdr->dest, tag); |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 607 | if (!ctx) |
Jeremy Kerr | c1693af | 2019-08-05 14:30:59 +0800 | [diff] [blame] | 608 | goto out; |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 609 | |
Ed Tanous | c2def9f | 2019-02-21 08:33:08 -0800 | [diff] [blame] | 610 | exp_seq = (ctx->last_seq + 1) % 4; |
| 611 | |
| 612 | if (exp_seq != seq) { |
| 613 | mctp_prdebug( |
| 614 | "Sequence number %d does not match expected %d", |
| 615 | seq, exp_seq); |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 616 | mctp_msg_ctx_drop(ctx); |
Jeremy Kerr | c1693af | 2019-08-05 14:30:59 +0800 | [diff] [blame] | 617 | goto out; |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 618 | } |
| 619 | |
Sumanth Bhat | 69f545f | 2021-05-18 09:16:43 +0000 | [diff] [blame] | 620 | len = mctp_pktbuf_size(pkt); |
| 621 | |
| 622 | if (len > ctx->fragment_size) { |
| 623 | mctp_prdebug("Unexpected fragment size. Expected" \ |
| 624 | " less than %zu, received = %zu", |
| 625 | ctx->fragment_size, len); |
| 626 | mctp_msg_ctx_drop(ctx); |
| 627 | goto out; |
| 628 | } |
| 629 | |
Sumanth Bhat | 2c820c5 | 2020-07-02 00:26:25 +0530 | [diff] [blame] | 630 | rc = mctp_msg_ctx_add_pkt(ctx, pkt, mctp->max_message_size); |
Jeremy Kerr | 1a4ec3c | 2019-09-03 11:01:50 +0800 | [diff] [blame] | 631 | if (!rc) |
Sumanth Bhat | f39c385 | 2022-01-10 17:04:10 +0530 | [diff] [blame^] | 632 | mctp_rx(mctp, bus, ctx->src, ctx->dest, tag_owner, tag, |
| 633 | ctx->buf, ctx->buf_size); |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 634 | |
| 635 | mctp_msg_ctx_drop(ctx); |
| 636 | break; |
Ed Tanous | c2def9f | 2019-02-21 08:33:08 -0800 | [diff] [blame] | 637 | |
| 638 | case 0: |
| 639 | /* Neither SOM nor EOM */ |
Jeremy Kerr | 1a4ec3c | 2019-09-03 11:01:50 +0800 | [diff] [blame] | 640 | ctx = mctp_msg_ctx_lookup(mctp, hdr->src,hdr->dest, tag); |
Ed Tanous | c2def9f | 2019-02-21 08:33:08 -0800 | [diff] [blame] | 641 | if (!ctx) |
Jeremy Kerr | c1693af | 2019-08-05 14:30:59 +0800 | [diff] [blame] | 642 | goto out; |
Ed Tanous | c2def9f | 2019-02-21 08:33:08 -0800 | [diff] [blame] | 643 | |
| 644 | exp_seq = (ctx->last_seq + 1) % 4; |
| 645 | if (exp_seq != seq) { |
| 646 | mctp_prdebug( |
| 647 | "Sequence number %d does not match expected %d", |
| 648 | seq, exp_seq); |
| 649 | mctp_msg_ctx_drop(ctx); |
Jeremy Kerr | c1693af | 2019-08-05 14:30:59 +0800 | [diff] [blame] | 650 | goto out; |
Ed Tanous | c2def9f | 2019-02-21 08:33:08 -0800 | [diff] [blame] | 651 | } |
| 652 | |
Sumanth Bhat | 69f545f | 2021-05-18 09:16:43 +0000 | [diff] [blame] | 653 | len = mctp_pktbuf_size(pkt); |
| 654 | |
| 655 | if (len != ctx->fragment_size) { |
| 656 | mctp_prdebug("Unexpected fragment size. Expected = %zu " \ |
| 657 | "received = %zu", ctx->fragment_size, len); |
| 658 | mctp_msg_ctx_drop(ctx); |
| 659 | goto out; |
| 660 | } |
| 661 | |
Sumanth Bhat | 2c820c5 | 2020-07-02 00:26:25 +0530 | [diff] [blame] | 662 | rc = mctp_msg_ctx_add_pkt(ctx, pkt, mctp->max_message_size); |
Ed Tanous | c2def9f | 2019-02-21 08:33:08 -0800 | [diff] [blame] | 663 | if (rc) { |
| 664 | mctp_msg_ctx_drop(ctx); |
Jeremy Kerr | c1693af | 2019-08-05 14:30:59 +0800 | [diff] [blame] | 665 | goto out; |
Ed Tanous | c2def9f | 2019-02-21 08:33:08 -0800 | [diff] [blame] | 666 | } |
| 667 | ctx->last_seq = seq; |
| 668 | |
| 669 | break; |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 670 | } |
Jeremy Kerr | c1693af | 2019-08-05 14:30:59 +0800 | [diff] [blame] | 671 | out: |
| 672 | mctp_pktbuf_free(pkt); |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 673 | } |
| 674 | |
Jeremy Kerr | 0a00dca | 2019-03-01 08:01:35 +0800 | [diff] [blame] | 675 | static int mctp_packet_tx(struct mctp_bus *bus, |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 676 | struct mctp_pktbuf *pkt) |
| 677 | { |
Andrew Jeffery | 5d3d4e6 | 2021-08-20 16:44:40 +0930 | [diff] [blame] | 678 | struct mctp *mctp = bus->binding->mctp; |
| 679 | |
Andrew Jeffery | c61501c | 2021-01-27 23:24:18 +1030 | [diff] [blame] | 680 | if (bus->state != mctp_bus_state_tx_enabled) |
Jeremy Kerr | 1cd3118 | 2019-02-27 18:01:00 +0800 | [diff] [blame] | 681 | return -1; |
| 682 | |
Andrew Jeffery | 5d3d4e6 | 2021-08-20 16:44:40 +0930 | [diff] [blame] | 683 | if (mctp->capture) |
| 684 | mctp->capture(pkt, mctp->capture_data); |
| 685 | |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 686 | return bus->binding->tx(bus->binding, pkt); |
| 687 | } |
| 688 | |
Jeremy Kerr | cc2458d | 2019-03-01 08:23:33 +0800 | [diff] [blame] | 689 | static void mctp_send_tx_queue(struct mctp_bus *bus) |
Jeremy Kerr | 1cd3118 | 2019-02-27 18:01:00 +0800 | [diff] [blame] | 690 | { |
| 691 | struct mctp_pktbuf *pkt; |
| 692 | |
Jeremy Kerr | cc2458d | 2019-03-01 08:23:33 +0800 | [diff] [blame] | 693 | while ((pkt = bus->tx_queue_head)) { |
Jeremy Kerr | 1cd3118 | 2019-02-27 18:01:00 +0800 | [diff] [blame] | 694 | int rc; |
| 695 | |
| 696 | rc = mctp_packet_tx(bus, pkt); |
| 697 | if (rc) |
| 698 | break; |
| 699 | |
Jeremy Kerr | cc2458d | 2019-03-01 08:23:33 +0800 | [diff] [blame] | 700 | bus->tx_queue_head = pkt->next; |
Jeremy Kerr | 1cd3118 | 2019-02-27 18:01:00 +0800 | [diff] [blame] | 701 | mctp_pktbuf_free(pkt); |
| 702 | } |
| 703 | |
Jeremy Kerr | cc2458d | 2019-03-01 08:23:33 +0800 | [diff] [blame] | 704 | if (!bus->tx_queue_head) |
| 705 | bus->tx_queue_tail = NULL; |
Jeremy Kerr | 1cd3118 | 2019-02-27 18:01:00 +0800 | [diff] [blame] | 706 | |
| 707 | } |
| 708 | |
| 709 | void mctp_binding_set_tx_enabled(struct mctp_binding *binding, bool enable) |
| 710 | { |
| 711 | struct mctp_bus *bus = binding->bus; |
Andrew Jeffery | c61501c | 2021-01-27 23:24:18 +1030 | [diff] [blame] | 712 | |
| 713 | switch(bus->state) { |
| 714 | case mctp_bus_state_constructed: |
| 715 | if (!enable) |
| 716 | return; |
| 717 | |
Andrew Jeffery | 1fa707e | 2021-01-28 15:22:11 +1030 | [diff] [blame] | 718 | if (binding->pkt_size < MCTP_PACKET_SIZE(MCTP_BTU)) { |
| 719 | mctp_prerr("Cannot start %s binding with invalid MTU: %zu", |
| 720 | binding->name, |
| 721 | MCTP_BODY_SIZE(binding->pkt_size)); |
| 722 | return; |
| 723 | } |
| 724 | |
Andrew Jeffery | c61501c | 2021-01-27 23:24:18 +1030 | [diff] [blame] | 725 | bus->state = mctp_bus_state_tx_enabled; |
| 726 | mctp_prinfo("%s binding started", binding->name); |
| 727 | return; |
| 728 | case mctp_bus_state_tx_enabled: |
| 729 | if (enable) |
| 730 | return; |
| 731 | |
| 732 | bus->state = mctp_bus_state_tx_disabled; |
| 733 | mctp_prdebug("%s binding Tx disabled", binding->name); |
| 734 | return; |
| 735 | case mctp_bus_state_tx_disabled: |
| 736 | if (!enable) |
| 737 | return; |
| 738 | |
| 739 | bus->state = mctp_bus_state_tx_enabled; |
| 740 | mctp_prdebug("%s binding Tx enabled", binding->name); |
Jeremy Kerr | cc2458d | 2019-03-01 08:23:33 +0800 | [diff] [blame] | 741 | mctp_send_tx_queue(bus); |
Andrew Jeffery | c61501c | 2021-01-27 23:24:18 +1030 | [diff] [blame] | 742 | return; |
| 743 | } |
Jeremy Kerr | 1cd3118 | 2019-02-27 18:01:00 +0800 | [diff] [blame] | 744 | } |
| 745 | |
Andrew Jeffery | b93b611 | 2020-06-05 14:13:44 +0930 | [diff] [blame] | 746 | static int mctp_message_tx_on_bus(struct mctp_bus *bus, mctp_eid_t src, |
Sumanth Bhat | f39c385 | 2022-01-10 17:04:10 +0530 | [diff] [blame^] | 747 | mctp_eid_t dest, bool tag_owner, |
| 748 | uint8_t msg_tag, void *msg, size_t msg_len) |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 749 | { |
Jeremy Kerr | df15f7e | 2019-08-05 15:41:19 +0800 | [diff] [blame] | 750 | size_t max_payload_len, payload_len, p; |
Jeremy Kerr | 1cd3118 | 2019-02-27 18:01:00 +0800 | [diff] [blame] | 751 | struct mctp_pktbuf *pkt; |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 752 | struct mctp_hdr *hdr; |
Jeremy Kerr | c855d7b | 2019-08-01 21:18:09 +0800 | [diff] [blame] | 753 | int i; |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 754 | |
Andrew Jeffery | c61501c | 2021-01-27 23:24:18 +1030 | [diff] [blame] | 755 | if (bus->state == mctp_bus_state_constructed) |
| 756 | return -ENXIO; |
| 757 | |
Sumanth Bhat | f39c385 | 2022-01-10 17:04:10 +0530 | [diff] [blame^] | 758 | if ((msg_tag & MCTP_HDR_TAG_MASK) != msg_tag) |
| 759 | return -EINVAL; |
| 760 | |
Andrew Jeffery | 1fa707e | 2021-01-28 15:22:11 +1030 | [diff] [blame] | 761 | max_payload_len = MCTP_BODY_SIZE(bus->binding->pkt_size); |
| 762 | |
| 763 | { |
| 764 | const bool valid_mtu = max_payload_len >= MCTP_BTU; |
| 765 | assert(valid_mtu); |
| 766 | if (!valid_mtu) |
| 767 | return -EINVAL; |
| 768 | } |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 769 | |
Andrew Jeffery | 298865f | 2020-02-06 11:51:29 +1030 | [diff] [blame] | 770 | mctp_prdebug("%s: Generating packets for transmission of %zu byte message from %hhu to %hhu", |
| 771 | __func__, msg_len, src, dest); |
| 772 | |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 773 | /* queue up packets, each of max MCTP_MTU size */ |
Jeremy Kerr | c855d7b | 2019-08-01 21:18:09 +0800 | [diff] [blame] | 774 | for (p = 0, i = 0; p < msg_len; i++) { |
Jeremy Kerr | df15f7e | 2019-08-05 15:41:19 +0800 | [diff] [blame] | 775 | payload_len = msg_len - p; |
| 776 | if (payload_len > max_payload_len) |
| 777 | payload_len = max_payload_len; |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 778 | |
Jeremy Kerr | 1a4ec3c | 2019-09-03 11:01:50 +0800 | [diff] [blame] | 779 | pkt = mctp_pktbuf_alloc(bus->binding, |
| 780 | payload_len + sizeof(*hdr)); |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 781 | hdr = mctp_pktbuf_hdr(pkt); |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 782 | |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 783 | hdr->ver = bus->binding->version & 0xf; |
Jeremy Kerr | 1a4ec3c | 2019-09-03 11:01:50 +0800 | [diff] [blame] | 784 | hdr->dest = dest; |
| 785 | hdr->src = src; |
Sumanth Bhat | f39c385 | 2022-01-10 17:04:10 +0530 | [diff] [blame^] | 786 | hdr->flags_seq_tag = (tag_owner << MCTP_HDR_TO_SHIFT) | |
| 787 | (msg_tag << MCTP_HDR_TAG_SHIFT); |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 788 | |
Jeremy Kerr | c855d7b | 2019-08-01 21:18:09 +0800 | [diff] [blame] | 789 | if (i == 0) |
| 790 | hdr->flags_seq_tag |= MCTP_HDR_FLAG_SOM; |
Jeremy Kerr | df15f7e | 2019-08-05 15:41:19 +0800 | [diff] [blame] | 791 | if (p + payload_len >= msg_len) |
Jeremy Kerr | c855d7b | 2019-08-01 21:18:09 +0800 | [diff] [blame] | 792 | hdr->flags_seq_tag |= MCTP_HDR_FLAG_EOM; |
| 793 | hdr->flags_seq_tag |= |
| 794 | (i & MCTP_HDR_SEQ_MASK) << MCTP_HDR_SEQ_SHIFT; |
| 795 | |
Jeremy Kerr | df15f7e | 2019-08-05 15:41:19 +0800 | [diff] [blame] | 796 | memcpy(mctp_pktbuf_data(pkt), msg + p, payload_len); |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 797 | |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 798 | /* add to tx queue */ |
Jeremy Kerr | cc2458d | 2019-03-01 08:23:33 +0800 | [diff] [blame] | 799 | if (bus->tx_queue_tail) |
| 800 | bus->tx_queue_tail->next = pkt; |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 801 | else |
Jeremy Kerr | cc2458d | 2019-03-01 08:23:33 +0800 | [diff] [blame] | 802 | bus->tx_queue_head = pkt; |
| 803 | bus->tx_queue_tail = pkt; |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 804 | |
Jeremy Kerr | df15f7e | 2019-08-05 15:41:19 +0800 | [diff] [blame] | 805 | p += payload_len; |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 806 | } |
| 807 | |
Andrew Jeffery | 298865f | 2020-02-06 11:51:29 +1030 | [diff] [blame] | 808 | mctp_prdebug("%s: Enqueued %d packets", __func__, i); |
| 809 | |
Jeremy Kerr | cc2458d | 2019-03-01 08:23:33 +0800 | [diff] [blame] | 810 | mctp_send_tx_queue(bus); |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 811 | |
| 812 | return 0; |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 813 | } |
Jeremy Kerr | 1a4ec3c | 2019-09-03 11:01:50 +0800 | [diff] [blame] | 814 | |
Sumanth Bhat | f39c385 | 2022-01-10 17:04:10 +0530 | [diff] [blame^] | 815 | int mctp_message_tx(struct mctp *mctp, mctp_eid_t eid, bool tag_owner, |
| 816 | uint8_t msg_tag, void *msg, size_t msg_len) |
Jeremy Kerr | 1a4ec3c | 2019-09-03 11:01:50 +0800 | [diff] [blame] | 817 | { |
| 818 | struct mctp_bus *bus; |
| 819 | |
Sumanth Bhat | f39c385 | 2022-01-10 17:04:10 +0530 | [diff] [blame^] | 820 | /* TODO: Protect against same tag being used across |
| 821 | * different callers */ |
| 822 | if ((msg_tag & MCTP_HDR_TAG_MASK) != msg_tag) { |
| 823 | mctp_prerr("Incorrect message tag %u passed.", msg_tag); |
| 824 | return -EINVAL; |
| 825 | } |
| 826 | |
Jeremy Kerr | 1a4ec3c | 2019-09-03 11:01:50 +0800 | [diff] [blame] | 827 | bus = find_bus_for_eid(mctp, eid); |
Brad Bishop | 663ec39 | 2021-10-07 21:16:48 -0400 | [diff] [blame] | 828 | if (!bus) |
| 829 | return 0; |
| 830 | |
Sumanth Bhat | f39c385 | 2022-01-10 17:04:10 +0530 | [diff] [blame^] | 831 | return mctp_message_tx_on_bus(bus, bus->eid, eid, tag_owner, msg_tag, |
| 832 | msg, msg_len); |
Jeremy Kerr | 1a4ec3c | 2019-09-03 11:01:50 +0800 | [diff] [blame] | 833 | } |