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" |
Matt Johnston | 4a09e1d | 2024-09-13 14:55:58 +0800 | [diff] [blame] | 20 | #include "compiler.h" |
Matt Johnston | f9b99f1 | 2024-09-17 16:48:34 +0800 | [diff] [blame] | 21 | #include "core-internal.h" |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 22 | |
Matt Johnston | 44e64df | 2024-11-05 16:59:42 +0800 | [diff] [blame] | 23 | #if MCTP_DEFAULT_CLOCK_GETTIME |
| 24 | #include <time.h> |
| 25 | #endif |
| 26 | |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 27 | #ifndef ARRAY_SIZE |
| 28 | #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) |
| 29 | #endif |
| 30 | |
Andrew Jeffery | b93b611 | 2020-06-05 14:13:44 +0930 | [diff] [blame] | 31 | 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] | 32 | mctp_eid_t dest, bool tag_owner, |
| 33 | uint8_t msg_tag, void *msg, size_t msg_len); |
Matt Johnston | 61c9599 | 2024-09-16 16:50:35 +0800 | [diff] [blame] | 34 | static void mctp_dealloc_tag(struct mctp_bus *bus, mctp_eid_t local, |
| 35 | mctp_eid_t remote, uint8_t tag); |
Jeremy Kerr | 1a4ec3c | 2019-09-03 11:01:50 +0800 | [diff] [blame] | 36 | |
Jeremy Kerr | df15f7e | 2019-08-05 15:41:19 +0800 | [diff] [blame] | 37 | 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] | 38 | { |
Matt Johnston | 4a09e1d | 2024-09-13 14:55:58 +0800 | [diff] [blame] | 39 | size_t size = |
| 40 | binding->pkt_size + binding->pkt_header + binding->pkt_trailer; |
Rashmica Gupta | 487b31e | 2022-09-14 18:49:45 +1000 | [diff] [blame] | 41 | if (len > size) { |
| 42 | return NULL; |
| 43 | } |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 44 | |
Matt Johnston | 4a09e1d | 2024-09-13 14:55:58 +0800 | [diff] [blame] | 45 | void *storage = __mctp_alloc(size + sizeof(struct mctp_pktbuf)); |
| 46 | if (!storage) { |
Pedro Martelletto | 2608b29 | 2023-03-30 13:28:28 +0000 | [diff] [blame] | 47 | return NULL; |
Matt Johnston | 4a09e1d | 2024-09-13 14:55:58 +0800 | [diff] [blame] | 48 | } |
| 49 | struct mctp_pktbuf *pkt = mctp_pktbuf_init(binding, storage); |
| 50 | pkt->alloc = true; |
| 51 | pkt->end = pkt->start + len; |
| 52 | return pkt; |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | void mctp_pktbuf_free(struct mctp_pktbuf *pkt) |
| 56 | { |
Matt Johnston | 4a09e1d | 2024-09-13 14:55:58 +0800 | [diff] [blame] | 57 | if (pkt->alloc) { |
| 58 | __mctp_free(pkt); |
| 59 | } else { |
| 60 | mctp_prdebug("pktbuf_free called for non-alloced"); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | struct mctp_pktbuf *mctp_pktbuf_init(struct mctp_binding *binding, |
| 65 | void *storage) |
| 66 | { |
| 67 | size_t size = |
| 68 | binding->pkt_size + binding->pkt_header + binding->pkt_trailer; |
| 69 | struct mctp_pktbuf *buf = (struct mctp_pktbuf *)storage; |
| 70 | buf->size = size; |
| 71 | buf->start = binding->pkt_header; |
| 72 | buf->end = buf->start; |
| 73 | buf->mctp_hdr_off = buf->start; |
| 74 | buf->alloc = false; |
| 75 | |
| 76 | return buf; |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | struct mctp_hdr *mctp_pktbuf_hdr(struct mctp_pktbuf *pkt) |
| 80 | { |
Moritz Fischer | 7aaccb5 | 2022-06-28 20:04:04 -0700 | [diff] [blame] | 81 | return (struct mctp_hdr *)(pkt->data + pkt->mctp_hdr_off); |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | void *mctp_pktbuf_data(struct mctp_pktbuf *pkt) |
| 85 | { |
Moritz Fischer | 7aaccb5 | 2022-06-28 20:04:04 -0700 | [diff] [blame] | 86 | return pkt->data + pkt->mctp_hdr_off + sizeof(struct mctp_hdr); |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 87 | } |
| 88 | |
Matt Johnston | 4a09e1d | 2024-09-13 14:55:58 +0800 | [diff] [blame] | 89 | size_t mctp_pktbuf_size(const struct mctp_pktbuf *pkt) |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 90 | { |
| 91 | return pkt->end - pkt->start; |
| 92 | } |
| 93 | |
Jeremy Kerr | df15f7e | 2019-08-05 15:41:19 +0800 | [diff] [blame] | 94 | void *mctp_pktbuf_alloc_start(struct mctp_pktbuf *pkt, size_t size) |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 95 | { |
| 96 | assert(size <= pkt->start); |
| 97 | pkt->start -= size; |
| 98 | return pkt->data + pkt->start; |
| 99 | } |
| 100 | |
Jeremy Kerr | df15f7e | 2019-08-05 15:41:19 +0800 | [diff] [blame] | 101 | void *mctp_pktbuf_alloc_end(struct mctp_pktbuf *pkt, size_t size) |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 102 | { |
| 103 | void *buf; |
| 104 | |
Andrew Jeffery | 3ac70d6 | 2020-07-01 00:50:44 +0930 | [diff] [blame] | 105 | assert(size <= (pkt->size - pkt->end)); |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 106 | buf = pkt->data + pkt->end; |
| 107 | pkt->end += size; |
| 108 | return buf; |
| 109 | } |
| 110 | |
Matt Johnston | dfbf0fd | 2024-10-28 14:40:29 +0800 | [diff] [blame] | 111 | int mctp_pktbuf_push(struct mctp_pktbuf *pkt, const void *data, size_t len) |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 112 | { |
| 113 | void *p; |
| 114 | |
Jeremy Kerr | df15f7e | 2019-08-05 15:41:19 +0800 | [diff] [blame] | 115 | if (pkt->end + len > pkt->size) |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 116 | return -1; |
| 117 | |
| 118 | p = pkt->data + pkt->end; |
| 119 | |
| 120 | pkt->end += len; |
| 121 | memcpy(p, data, len); |
| 122 | |
| 123 | return 0; |
| 124 | } |
| 125 | |
Andrew Jeffery | eba19a3 | 2021-03-09 23:09:40 +1030 | [diff] [blame] | 126 | void *mctp_pktbuf_pop(struct mctp_pktbuf *pkt, size_t len) |
| 127 | { |
| 128 | if (len > mctp_pktbuf_size(pkt)) |
| 129 | return NULL; |
| 130 | |
| 131 | pkt->end -= len; |
| 132 | return pkt->data + pkt->end; |
| 133 | } |
| 134 | |
Matt Johnston | 4a09e1d | 2024-09-13 14:55:58 +0800 | [diff] [blame] | 135 | /* Allocate a duplicate of the message and copy it */ |
| 136 | static void *mctp_msg_dup(const void *msg, size_t msg_len, struct mctp *mctp) |
| 137 | { |
| 138 | void *copy = __mctp_msg_alloc(msg_len, mctp); |
| 139 | if (!copy) { |
| 140 | mctp_prdebug("msg dup len %zu failed", msg_len); |
| 141 | return NULL; |
| 142 | } |
| 143 | |
| 144 | memcpy(copy, msg, msg_len); |
| 145 | return copy; |
| 146 | } |
| 147 | |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 148 | /* Message reassembly */ |
Patrick Williams | a721c2d | 2022-12-04 14:30:26 -0600 | [diff] [blame] | 149 | static struct mctp_msg_ctx *mctp_msg_ctx_lookup(struct mctp *mctp, uint8_t src, |
| 150 | uint8_t dest, uint8_t tag) |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 151 | { |
| 152 | unsigned int i; |
| 153 | |
| 154 | /* @todo: better lookup, if we add support for more outstanding |
| 155 | * message contexts */ |
| 156 | for (i = 0; i < ARRAY_SIZE(mctp->msg_ctxs); i++) { |
| 157 | struct mctp_msg_ctx *ctx = &mctp->msg_ctxs[i]; |
Matt Johnston | 4a09e1d | 2024-09-13 14:55:58 +0800 | [diff] [blame] | 158 | if (ctx->buf && ctx->src == src && ctx->dest == dest && |
| 159 | ctx->tag == tag) |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 160 | return ctx; |
| 161 | } |
| 162 | |
| 163 | return NULL; |
| 164 | } |
| 165 | |
Patrick Williams | a721c2d | 2022-12-04 14:30:26 -0600 | [diff] [blame] | 166 | static struct mctp_msg_ctx *mctp_msg_ctx_create(struct mctp *mctp, uint8_t src, |
| 167 | uint8_t dest, uint8_t tag) |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 168 | { |
Jeremy Kerr | 11a234e | 2019-02-27 17:59:53 +0800 | [diff] [blame] | 169 | struct mctp_msg_ctx *ctx = NULL; |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 170 | unsigned int i; |
| 171 | |
| 172 | for (i = 0; i < ARRAY_SIZE(mctp->msg_ctxs); i++) { |
| 173 | struct mctp_msg_ctx *tmp = &mctp->msg_ctxs[i]; |
Matt Johnston | 4a09e1d | 2024-09-13 14:55:58 +0800 | [diff] [blame] | 174 | if (!tmp->buf) { |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 175 | ctx = tmp; |
| 176 | break; |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | if (!ctx) |
| 181 | return NULL; |
| 182 | |
| 183 | ctx->src = src; |
Jeremy Kerr | 1a4ec3c | 2019-09-03 11:01:50 +0800 | [diff] [blame] | 184 | ctx->dest = dest; |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 185 | ctx->tag = tag; |
Matt Johnston | 4a09e1d | 2024-09-13 14:55:58 +0800 | [diff] [blame] | 186 | |
Jeremy Kerr | 9a3da81 | 2019-08-02 15:57:53 +0800 | [diff] [blame] | 187 | ctx->buf_size = 0; |
Matt Johnston | 4a09e1d | 2024-09-13 14:55:58 +0800 | [diff] [blame] | 188 | ctx->buf_alloc_size = mctp->max_message_size; |
| 189 | ctx->buf = __mctp_msg_alloc(ctx->buf_alloc_size, mctp); |
| 190 | if (!ctx->buf) { |
| 191 | return NULL; |
| 192 | } |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 193 | |
| 194 | return ctx; |
| 195 | } |
| 196 | |
Matt Johnston | 4a09e1d | 2024-09-13 14:55:58 +0800 | [diff] [blame] | 197 | static void mctp_msg_ctx_drop(struct mctp_bus *bus, struct mctp_msg_ctx *ctx) |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 198 | { |
Matt Johnston | 4a09e1d | 2024-09-13 14:55:58 +0800 | [diff] [blame] | 199 | /* Free and mark as unused */ |
| 200 | __mctp_msg_free(ctx->buf, bus->mctp); |
| 201 | ctx->buf = NULL; |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | static void mctp_msg_ctx_reset(struct mctp_msg_ctx *ctx) |
| 205 | { |
| 206 | ctx->buf_size = 0; |
Sumanth Bhat | 69f545f | 2021-05-18 09:16:43 +0000 | [diff] [blame] | 207 | ctx->fragment_size = 0; |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | static int mctp_msg_ctx_add_pkt(struct mctp_msg_ctx *ctx, |
Matt Johnston | 4a09e1d | 2024-09-13 14:55:58 +0800 | [diff] [blame] | 211 | struct mctp_pktbuf *pkt) |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 212 | { |
| 213 | size_t len; |
| 214 | |
| 215 | len = mctp_pktbuf_size(pkt) - sizeof(struct mctp_hdr); |
| 216 | |
Sumanth Bhat | bc79c24 | 2021-06-16 12:36:56 +0530 | [diff] [blame] | 217 | if (len + ctx->buf_size < ctx->buf_size) { |
| 218 | return -1; |
| 219 | } |
| 220 | |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 221 | if (ctx->buf_size + len > ctx->buf_alloc_size) { |
Matt Johnston | 4a09e1d | 2024-09-13 14:55:58 +0800 | [diff] [blame] | 222 | return -1; |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 223 | } |
| 224 | |
Moritz Fischer | 7aaccb5 | 2022-06-28 20:04:04 -0700 | [diff] [blame] | 225 | memcpy((uint8_t *)ctx->buf + ctx->buf_size, mctp_pktbuf_data(pkt), len); |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 226 | ctx->buf_size += len; |
| 227 | |
| 228 | return 0; |
| 229 | } |
| 230 | |
| 231 | /* Core API functions */ |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 232 | struct mctp *mctp_init(void) |
| 233 | { |
| 234 | struct mctp *mctp; |
| 235 | |
| 236 | mctp = __mctp_alloc(sizeof(*mctp)); |
Sumanth Bhat | 96d5449 | 2020-07-14 17:10:04 +0530 | [diff] [blame] | 237 | |
Patrick Williams | a721c2d | 2022-12-04 14:30:26 -0600 | [diff] [blame] | 238 | if (!mctp) |
Sumanth Bhat | 96d5449 | 2020-07-14 17:10:04 +0530 | [diff] [blame] | 239 | return NULL; |
| 240 | |
Matt Johnston | f9b99f1 | 2024-09-17 16:48:34 +0800 | [diff] [blame] | 241 | mctp_setup(mctp, sizeof(*mctp)); |
Matt Johnston | 722d0db | 2024-09-13 15:51:30 +0800 | [diff] [blame] | 242 | return mctp; |
| 243 | } |
| 244 | |
Matt Johnston | 44e64df | 2024-11-05 16:59:42 +0800 | [diff] [blame] | 245 | #if MCTP_DEFAULT_CLOCK_GETTIME |
| 246 | static uint64_t mctp_default_now(void *ctx __attribute__((unused))) |
| 247 | { |
| 248 | struct timespec tp; |
| 249 | int rc = clock_gettime(CLOCK_MONOTONIC, &tp); |
| 250 | if (rc) { |
| 251 | /* Should not be possible */ |
| 252 | return 0; |
| 253 | } |
| 254 | return (uint64_t)tp.tv_sec * 1000 + tp.tv_nsec / 1000000; |
| 255 | } |
| 256 | #endif |
| 257 | |
Matt Johnston | f9b99f1 | 2024-09-17 16:48:34 +0800 | [diff] [blame] | 258 | int mctp_setup(struct mctp *mctp, size_t struct_mctp_size) |
Matt Johnston | 722d0db | 2024-09-13 15:51:30 +0800 | [diff] [blame] | 259 | { |
Matt Johnston | f9b99f1 | 2024-09-17 16:48:34 +0800 | [diff] [blame] | 260 | if (struct_mctp_size < sizeof(struct mctp)) { |
| 261 | mctp_prdebug("Mismatching struct mctp"); |
| 262 | return -EINVAL; |
| 263 | } |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 264 | memset(mctp, 0, sizeof(*mctp)); |
Sumanth Bhat | 2c820c5 | 2020-07-02 00:26:25 +0530 | [diff] [blame] | 265 | mctp->max_message_size = MCTP_MAX_MESSAGE_SIZE; |
Matt Johnston | 44e64df | 2024-11-05 16:59:42 +0800 | [diff] [blame] | 266 | #if MCTP_DEFAULT_CLOCK_GETTIME |
| 267 | mctp->platform_now = mctp_default_now; |
| 268 | #endif |
Matt Johnston | f9b99f1 | 2024-09-17 16:48:34 +0800 | [diff] [blame] | 269 | return 0; |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 270 | } |
| 271 | |
Sumanth Bhat | 2c820c5 | 2020-07-02 00:26:25 +0530 | [diff] [blame] | 272 | void mctp_set_max_message_size(struct mctp *mctp, size_t message_size) |
| 273 | { |
| 274 | mctp->max_message_size = message_size; |
| 275 | } |
| 276 | |
Andrew Jeffery | 5d3d4e6 | 2021-08-20 16:44:40 +0930 | [diff] [blame] | 277 | void mctp_set_capture_handler(struct mctp *mctp, mctp_capture_fn fn, void *user) |
| 278 | { |
| 279 | mctp->capture = fn; |
| 280 | mctp->capture_data = user; |
| 281 | } |
| 282 | |
Matt Johnston | 4a09e1d | 2024-09-13 14:55:58 +0800 | [diff] [blame] | 283 | static void mctp_bus_destroy(struct mctp_bus *bus, struct mctp *mctp) |
Andrew Jeffery | 3ae89dc | 2021-01-28 15:24:36 +1030 | [diff] [blame] | 284 | { |
Matt Johnston | 4a09e1d | 2024-09-13 14:55:58 +0800 | [diff] [blame] | 285 | if (bus->tx_msg) { |
| 286 | __mctp_msg_free(bus->tx_msg, mctp); |
| 287 | bus->tx_msg = NULL; |
Andrew Jeffery | 3ae89dc | 2021-01-28 15:24:36 +1030 | [diff] [blame] | 288 | } |
| 289 | } |
| 290 | |
Matt Johnston | 722d0db | 2024-09-13 15:51:30 +0800 | [diff] [blame] | 291 | void mctp_cleanup(struct mctp *mctp) |
Andrew Jeffery | fa56ca5 | 2020-03-10 23:18:22 +1030 | [diff] [blame] | 292 | { |
Andrew Jeffery | b93b611 | 2020-06-05 14:13:44 +0930 | [diff] [blame] | 293 | size_t i; |
Andrew Jeffery | fa56ca5 | 2020-03-10 23:18:22 +1030 | [diff] [blame] | 294 | |
| 295 | /* Cleanup message assembly contexts */ |
Matt Johnston | 3ef4778 | 2024-12-11 15:19:06 +0800 | [diff] [blame] | 296 | static_assert(ARRAY_SIZE(mctp->msg_ctxs) < SIZE_MAX, "size"); |
Andrew Jeffery | fa56ca5 | 2020-03-10 23:18:22 +1030 | [diff] [blame] | 297 | for (i = 0; i < ARRAY_SIZE(mctp->msg_ctxs); i++) { |
| 298 | struct mctp_msg_ctx *tmp = &mctp->msg_ctxs[i]; |
| 299 | if (tmp->buf) |
Matt Johnston | 4a09e1d | 2024-09-13 14:55:58 +0800 | [diff] [blame] | 300 | __mctp_msg_free(tmp->buf, mctp); |
Andrew Jeffery | fa56ca5 | 2020-03-10 23:18:22 +1030 | [diff] [blame] | 301 | } |
| 302 | |
Andrew Jeffery | 3ae89dc | 2021-01-28 15:24:36 +1030 | [diff] [blame] | 303 | while (mctp->n_busses--) |
Matt Johnston | 4a09e1d | 2024-09-13 14:55:58 +0800 | [diff] [blame] | 304 | mctp_bus_destroy(&mctp->busses[mctp->n_busses], mctp); |
Matt Johnston | 722d0db | 2024-09-13 15:51:30 +0800 | [diff] [blame] | 305 | } |
Andrew Jeffery | 3ae89dc | 2021-01-28 15:24:36 +1030 | [diff] [blame] | 306 | |
Matt Johnston | 722d0db | 2024-09-13 15:51:30 +0800 | [diff] [blame] | 307 | void mctp_destroy(struct mctp *mctp) |
| 308 | { |
| 309 | mctp_cleanup(mctp); |
Andrew Jeffery | fa56ca5 | 2020-03-10 23:18:22 +1030 | [diff] [blame] | 310 | __mctp_free(mctp); |
| 311 | } |
| 312 | |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 313 | int mctp_set_rx_all(struct mctp *mctp, mctp_rx_fn fn, void *data) |
| 314 | { |
| 315 | mctp->message_rx = fn; |
| 316 | mctp->message_rx_data = data; |
| 317 | return 0; |
| 318 | } |
| 319 | |
Patrick Williams | a721c2d | 2022-12-04 14:30:26 -0600 | [diff] [blame] | 320 | static struct mctp_bus *find_bus_for_eid(struct mctp *mctp, mctp_eid_t dest |
| 321 | __attribute__((unused))) |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 322 | { |
Brad Bishop | 663ec39 | 2021-10-07 21:16:48 -0400 | [diff] [blame] | 323 | if (mctp->n_busses == 0) |
| 324 | return NULL; |
| 325 | |
Jeremy Kerr | 1a4ec3c | 2019-09-03 11:01:50 +0800 | [diff] [blame] | 326 | /* for now, just use the first bus. For full routing support, |
| 327 | * we will need a table of neighbours */ |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 328 | return &mctp->busses[0]; |
| 329 | } |
| 330 | |
Patrick Williams | a721c2d | 2022-12-04 14:30:26 -0600 | [diff] [blame] | 331 | int mctp_register_bus(struct mctp *mctp, struct mctp_binding *binding, |
| 332 | mctp_eid_t eid) |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 333 | { |
Andrew Jeffery | 3e8a12a | 2020-06-05 16:08:30 +0930 | [diff] [blame] | 334 | int rc = 0; |
| 335 | |
Jeremy Kerr | 7520cec | 2019-03-01 07:13:18 +0800 | [diff] [blame] | 336 | /* todo: multiple busses */ |
Matt Johnston | 722d0db | 2024-09-13 15:51:30 +0800 | [diff] [blame] | 337 | static_assert(MCTP_MAX_BUSSES >= 1, "need a bus"); |
Jeremy Kerr | 1a4ec3c | 2019-09-03 11:01:50 +0800 | [diff] [blame] | 338 | assert(mctp->n_busses == 0); |
| 339 | mctp->n_busses = 1; |
Andrew Jeffery | 3e8a12a | 2020-06-05 16:08:30 +0930 | [diff] [blame] | 340 | |
Matt Johnston | 4a09e1d | 2024-09-13 14:55:58 +0800 | [diff] [blame] | 341 | assert(binding->tx_storage); |
| 342 | |
James Feist | 62d7236 | 2019-12-13 13:43:32 -0800 | [diff] [blame] | 343 | memset(mctp->busses, 0, sizeof(struct mctp_bus)); |
Matt Johnston | 4a09e1d | 2024-09-13 14:55:58 +0800 | [diff] [blame] | 344 | mctp->busses[0].mctp = mctp; |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 345 | mctp->busses[0].binding = binding; |
| 346 | mctp->busses[0].eid = eid; |
Jeremy Kerr | 7520cec | 2019-03-01 07:13:18 +0800 | [diff] [blame] | 347 | binding->bus = &mctp->busses[0]; |
Jeremy Kerr | 0a00dca | 2019-03-01 08:01:35 +0800 | [diff] [blame] | 348 | binding->mctp = mctp; |
Jeremy Kerr | 1a4ec3c | 2019-09-03 11:01:50 +0800 | [diff] [blame] | 349 | mctp->route_policy = ROUTE_ENDPOINT; |
Jeremy Kerr | 3b36d17 | 2019-09-04 11:56:09 +0800 | [diff] [blame] | 350 | |
Andrew Jeffery | 3e8a12a | 2020-06-05 16:08:30 +0930 | [diff] [blame] | 351 | if (binding->start) { |
| 352 | rc = binding->start(binding); |
| 353 | if (rc < 0) { |
| 354 | mctp_prerr("Failed to start binding: %d", rc); |
Andrew Jeffery | 1927523 | 2021-01-29 14:13:25 +1030 | [diff] [blame] | 355 | binding->bus = NULL; |
Andrew Jeffery | 2304c83 | 2021-01-29 11:52:49 +1030 | [diff] [blame] | 356 | mctp->n_busses = 0; |
Andrew Jeffery | 3e8a12a | 2020-06-05 16:08:30 +0930 | [diff] [blame] | 357 | } |
| 358 | } |
Jeremy Kerr | 3b36d17 | 2019-09-04 11:56:09 +0800 | [diff] [blame] | 359 | |
Andrew Jeffery | 3e8a12a | 2020-06-05 16:08:30 +0930 | [diff] [blame] | 360 | return rc; |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 361 | } |
| 362 | |
Andrew Jeffery | 2094c3c | 2021-08-26 12:32:46 +0930 | [diff] [blame] | 363 | void mctp_unregister_bus(struct mctp *mctp, struct mctp_binding *binding) |
| 364 | { |
| 365 | /* |
| 366 | * We only support one bus right now; once the call completes we will |
| 367 | * have no more busses |
| 368 | */ |
| 369 | mctp->n_busses = 0; |
| 370 | binding->mctp = NULL; |
| 371 | binding->bus = NULL; |
Andrew Jeffery | 2094c3c | 2021-08-26 12:32:46 +0930 | [diff] [blame] | 372 | } |
| 373 | |
Patrick Williams | a721c2d | 2022-12-04 14:30:26 -0600 | [diff] [blame] | 374 | int mctp_bridge_busses(struct mctp *mctp, struct mctp_binding *b1, |
| 375 | struct mctp_binding *b2) |
Jeremy Kerr | 1a4ec3c | 2019-09-03 11:01:50 +0800 | [diff] [blame] | 376 | { |
Andrew Jeffery | 1927523 | 2021-01-29 14:13:25 +1030 | [diff] [blame] | 377 | int rc = 0; |
| 378 | |
Matt Johnston | 4a09e1d | 2024-09-13 14:55:58 +0800 | [diff] [blame] | 379 | assert(b1->tx_storage); |
| 380 | assert(b2->tx_storage); |
| 381 | |
Jeremy Kerr | 1a4ec3c | 2019-09-03 11:01:50 +0800 | [diff] [blame] | 382 | assert(mctp->n_busses == 0); |
Matt Johnston | 722d0db | 2024-09-13 15:51:30 +0800 | [diff] [blame] | 383 | assert(MCTP_MAX_BUSSES >= 2); |
James Feist | 62d7236 | 2019-12-13 13:43:32 -0800 | [diff] [blame] | 384 | memset(mctp->busses, 0, 2 * sizeof(struct mctp_bus)); |
Jeremy Kerr | 1a4ec3c | 2019-09-03 11:01:50 +0800 | [diff] [blame] | 385 | mctp->n_busses = 2; |
| 386 | mctp->busses[0].binding = b1; |
| 387 | b1->bus = &mctp->busses[0]; |
| 388 | b1->mctp = mctp; |
| 389 | mctp->busses[1].binding = b2; |
| 390 | b2->bus = &mctp->busses[1]; |
| 391 | b2->mctp = mctp; |
| 392 | |
| 393 | mctp->route_policy = ROUTE_BRIDGE; |
Jeremy Kerr | 3b36d17 | 2019-09-04 11:56:09 +0800 | [diff] [blame] | 394 | |
Andrew Jeffery | 1927523 | 2021-01-29 14:13:25 +1030 | [diff] [blame] | 395 | if (b1->start) { |
| 396 | rc = b1->start(b1); |
| 397 | if (rc < 0) { |
| 398 | mctp_prerr("Failed to start bridged bus %s: %d", |
| 399 | b1->name, rc); |
| 400 | goto done; |
| 401 | } |
| 402 | } |
Jeremy Kerr | 3b36d17 | 2019-09-04 11:56:09 +0800 | [diff] [blame] | 403 | |
Andrew Jeffery | 1927523 | 2021-01-29 14:13:25 +1030 | [diff] [blame] | 404 | if (b2->start) { |
| 405 | rc = b2->start(b2); |
| 406 | if (rc < 0) { |
| 407 | mctp_prerr("Failed to start bridged bus %s: %d", |
| 408 | b2->name, rc); |
| 409 | goto done; |
| 410 | } |
| 411 | } |
Jeremy Kerr | 3b36d17 | 2019-09-04 11:56:09 +0800 | [diff] [blame] | 412 | |
Andrew Jeffery | 1927523 | 2021-01-29 14:13:25 +1030 | [diff] [blame] | 413 | done: |
| 414 | return rc; |
Jeremy Kerr | 1a4ec3c | 2019-09-03 11:01:50 +0800 | [diff] [blame] | 415 | } |
| 416 | |
Wiktor GoĊgowski | ba6727e | 2020-03-13 18:25:01 +0100 | [diff] [blame] | 417 | 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] | 418 | { |
Matt Johnston | 3ef4778 | 2024-12-11 15:19:06 +0800 | [diff] [blame] | 419 | #pragma GCC diagnostic push |
| 420 | #pragma GCC diagnostic ignored "-Wtype-limits" |
Wiktor GoĊgowski | ba6727e | 2020-03-13 18:25:01 +0100 | [diff] [blame] | 421 | return ((hdr->command_code >= MCTP_CTRL_CMD_FIRST_TRANSPORT) && |
| 422 | (hdr->command_code <= MCTP_CTRL_CMD_LAST_TRANSPORT)); |
Matt Johnston | 3ef4778 | 2024-12-11 15:19:06 +0800 | [diff] [blame] | 423 | #pragma GCC diagnostic pop |
Wiktor GoĊgowski | ba6727e | 2020-03-13 18:25:01 +0100 | [diff] [blame] | 424 | } |
| 425 | |
Andrew Jeffery | b93b611 | 2020-06-05 14:13:44 +0930 | [diff] [blame] | 426 | 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] | 427 | uint8_t msg_tag, bool tag_owner, void *buffer, |
| 428 | size_t length) |
Wiktor GoĊgowski | ba6727e | 2020-03-13 18:25:01 +0100 | [diff] [blame] | 429 | { |
| 430 | struct mctp_ctrl_msg_hdr *msg_hdr = buffer; |
| 431 | |
| 432 | /* |
| 433 | * Control message is received. If a transport control message handler |
| 434 | * is provided, it will called. If there is no dedicated handler, this |
| 435 | * function returns false and data can be handled by the generic |
| 436 | * message handler. The transport control message handler will be |
| 437 | * provided with messages in the command range 0xF0 - 0xFF. |
| 438 | */ |
| 439 | if (mctp_ctrl_cmd_is_transport(msg_hdr)) { |
| 440 | if (bus->binding->control_rx != NULL) { |
| 441 | /* MCTP bus binding handler */ |
Sumanth Bhat | f39c385 | 2022-01-10 17:04:10 +0530 | [diff] [blame] | 442 | bus->binding->control_rx(src, msg_tag, tag_owner, |
Wiktor GoĊgowski | ba6727e | 2020-03-13 18:25:01 +0100 | [diff] [blame] | 443 | bus->binding->control_rx_data, |
| 444 | buffer, length); |
| 445 | return true; |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | /* |
| 450 | * Command was not handled, due to lack of specific callback. |
| 451 | * It will be passed to regular message_rx handler. |
| 452 | */ |
| 453 | return false; |
| 454 | } |
| 455 | |
| 456 | static inline bool mctp_rx_dest_is_local(struct mctp_bus *bus, mctp_eid_t dest) |
| 457 | { |
| 458 | return dest == bus->eid || dest == MCTP_EID_NULL || |
| 459 | dest == MCTP_EID_BROADCAST; |
| 460 | } |
| 461 | |
| 462 | static inline bool mctp_ctrl_cmd_is_request(struct mctp_ctrl_msg_hdr *hdr) |
| 463 | { |
| 464 | return hdr->ic_msg_type == MCTP_CTRL_HDR_MSG_TYPE && |
| 465 | hdr->rq_dgram_inst & MCTP_CTRL_HDR_FLAG_REQUEST; |
| 466 | } |
| 467 | |
| 468 | /* |
| 469 | * Receive the complete MCTP message and route it. |
| 470 | * Asserts: |
| 471 | * 'buf' is not NULL. |
| 472 | */ |
| 473 | 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] | 474 | mctp_eid_t dest, bool tag_owner, uint8_t msg_tag, void *buf, |
| 475 | size_t len) |
Wiktor GoĊgowski | ba6727e | 2020-03-13 18:25:01 +0100 | [diff] [blame] | 476 | { |
| 477 | assert(buf != NULL); |
| 478 | |
Jeremy Kerr | 1a4ec3c | 2019-09-03 11:01:50 +0800 | [diff] [blame] | 479 | if (mctp->route_policy == ROUTE_ENDPOINT && |
Wiktor GoĊgowski | ba6727e | 2020-03-13 18:25:01 +0100 | [diff] [blame] | 480 | mctp_rx_dest_is_local(bus, dest)) { |
Matt Johnston | 61c9599 | 2024-09-16 16:50:35 +0800 | [diff] [blame] | 481 | /* Note responses to allocated tags */ |
| 482 | if (!tag_owner) { |
| 483 | mctp_dealloc_tag(bus, dest, src, msg_tag); |
| 484 | } |
| 485 | |
Wiktor GoĊgowski | ba6727e | 2020-03-13 18:25:01 +0100 | [diff] [blame] | 486 | /* Handle MCTP Control Messages: */ |
| 487 | if (len >= sizeof(struct mctp_ctrl_msg_hdr)) { |
| 488 | struct mctp_ctrl_msg_hdr *msg_hdr = buf; |
| 489 | |
| 490 | /* |
| 491 | * Identify if this is a control request message. |
| 492 | * See DSP0236 v1.3.0 sec. 11.5. |
| 493 | */ |
| 494 | if (mctp_ctrl_cmd_is_request(msg_hdr)) { |
| 495 | bool handled; |
Sumanth Bhat | f39c385 | 2022-01-10 17:04:10 +0530 | [diff] [blame] | 496 | handled = mctp_ctrl_handle_msg( |
| 497 | bus, src, msg_tag, tag_owner, buf, len); |
Wiktor GoĊgowski | ba6727e | 2020-03-13 18:25:01 +0100 | [diff] [blame] | 498 | if (handled) |
| 499 | return; |
| 500 | } |
| 501 | } |
Sumanth Bhat | f39c385 | 2022-01-10 17:04:10 +0530 | [diff] [blame] | 502 | |
Wiktor GoĊgowski | ba6727e | 2020-03-13 18:25:01 +0100 | [diff] [blame] | 503 | if (mctp->message_rx) |
Sumanth Bhat | f39c385 | 2022-01-10 17:04:10 +0530 | [diff] [blame] | 504 | mctp->message_rx(src, tag_owner, msg_tag, |
| 505 | mctp->message_rx_data, buf, len); |
Wiktor GoĊgowski | ba6727e | 2020-03-13 18:25:01 +0100 | [diff] [blame] | 506 | } |
Jeremy Kerr | 1a4ec3c | 2019-09-03 11:01:50 +0800 | [diff] [blame] | 507 | |
| 508 | if (mctp->route_policy == ROUTE_BRIDGE) { |
| 509 | int i; |
| 510 | |
| 511 | for (i = 0; i < mctp->n_busses; i++) { |
| 512 | struct mctp_bus *dest_bus = &mctp->busses[i]; |
| 513 | if (dest_bus == bus) |
| 514 | continue; |
| 515 | |
Matt Johnston | 4a09e1d | 2024-09-13 14:55:58 +0800 | [diff] [blame] | 516 | void *copy = mctp_msg_dup(buf, len, mctp); |
| 517 | if (!copy) { |
| 518 | return; |
| 519 | } |
| 520 | |
Sumanth Bhat | f39c385 | 2022-01-10 17:04:10 +0530 | [diff] [blame] | 521 | mctp_message_tx_on_bus(dest_bus, src, dest, tag_owner, |
Matt Johnston | 4a09e1d | 2024-09-13 14:55:58 +0800 | [diff] [blame] | 522 | msg_tag, copy, len); |
Jeremy Kerr | 1a4ec3c | 2019-09-03 11:01:50 +0800 | [diff] [blame] | 523 | } |
Jeremy Kerr | 1a4ec3c | 2019-09-03 11:01:50 +0800 | [diff] [blame] | 524 | } |
| 525 | } |
| 526 | |
Jeremy Kerr | 0a00dca | 2019-03-01 08:01:35 +0800 | [diff] [blame] | 527 | void mctp_bus_rx(struct mctp_binding *binding, struct mctp_pktbuf *pkt) |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 528 | { |
Jeremy Kerr | 7520cec | 2019-03-01 07:13:18 +0800 | [diff] [blame] | 529 | struct mctp_bus *bus = binding->bus; |
Jeremy Kerr | 0a00dca | 2019-03-01 08:01:35 +0800 | [diff] [blame] | 530 | struct mctp *mctp = binding->mctp; |
Ed Tanous | c2def9f | 2019-02-21 08:33:08 -0800 | [diff] [blame] | 531 | uint8_t flags, exp_seq, seq, tag; |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 532 | struct mctp_msg_ctx *ctx; |
| 533 | struct mctp_hdr *hdr; |
Sumanth Bhat | f39c385 | 2022-01-10 17:04:10 +0530 | [diff] [blame] | 534 | bool tag_owner; |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 535 | size_t len; |
| 536 | void *p; |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 537 | int rc; |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 538 | |
Jeremy Kerr | 7520cec | 2019-03-01 07:13:18 +0800 | [diff] [blame] | 539 | assert(bus); |
| 540 | |
Sumanth Bhat | d97869d | 2020-07-02 00:46:13 +0530 | [diff] [blame] | 541 | /* Drop packet if it was smaller than mctp hdr size */ |
Matt Johnston | 86e9a97 | 2024-10-28 15:06:33 +0800 | [diff] [blame] | 542 | if (mctp_pktbuf_size(pkt) < sizeof(struct mctp_hdr)) |
Sumanth Bhat | d97869d | 2020-07-02 00:46:13 +0530 | [diff] [blame] | 543 | goto out; |
| 544 | |
Andrew Jeffery | 5d3d4e6 | 2021-08-20 16:44:40 +0930 | [diff] [blame] | 545 | if (mctp->capture) |
Rashmica Gupta | f298897 | 2022-11-09 12:26:44 +1100 | [diff] [blame] | 546 | mctp->capture(pkt, MCTP_MESSAGE_CAPTURE_INCOMING, |
| 547 | mctp->capture_data); |
Andrew Jeffery | 5d3d4e6 | 2021-08-20 16:44:40 +0930 | [diff] [blame] | 548 | |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 549 | hdr = mctp_pktbuf_hdr(pkt); |
| 550 | |
Jeremy Kerr | 1a4ec3c | 2019-09-03 11:01:50 +0800 | [diff] [blame] | 551 | /* small optimisation: don't bother reassembly if we're going to |
| 552 | * drop the packet in mctp_rx anyway */ |
John Chung | 133df7a | 2024-05-14 16:19:56 +0800 | [diff] [blame] | 553 | if (mctp->route_policy == ROUTE_ENDPOINT && |
| 554 | !mctp_rx_dest_is_local(bus, hdr->dest)) |
Jeremy Kerr | c1693af | 2019-08-05 14:30:59 +0800 | [diff] [blame] | 555 | goto out; |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 556 | |
| 557 | flags = hdr->flags_seq_tag & (MCTP_HDR_FLAG_SOM | MCTP_HDR_FLAG_EOM); |
| 558 | tag = (hdr->flags_seq_tag >> MCTP_HDR_TAG_SHIFT) & MCTP_HDR_TAG_MASK; |
| 559 | seq = (hdr->flags_seq_tag >> MCTP_HDR_SEQ_SHIFT) & MCTP_HDR_SEQ_MASK; |
Andrew Jeffery | 7f7fdc1 | 2023-05-12 15:56:47 +0930 | [diff] [blame] | 560 | tag_owner = (hdr->flags_seq_tag >> MCTP_HDR_TO_SHIFT) & |
| 561 | MCTP_HDR_TO_MASK; |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 562 | |
| 563 | switch (flags) { |
| 564 | case MCTP_HDR_FLAG_SOM | MCTP_HDR_FLAG_EOM: |
| 565 | /* single-packet message - send straight up to rx function, |
| 566 | * no need to create a message context */ |
| 567 | len = pkt->end - pkt->mctp_hdr_off - sizeof(struct mctp_hdr); |
Matt Johnston | 4a09e1d | 2024-09-13 14:55:58 +0800 | [diff] [blame] | 568 | p = mctp_msg_dup(pkt->data + pkt->mctp_hdr_off + |
| 569 | sizeof(struct mctp_hdr), |
| 570 | len, mctp); |
| 571 | if (p) { |
| 572 | mctp_rx(mctp, bus, hdr->src, hdr->dest, tag_owner, tag, |
| 573 | p, len); |
| 574 | __mctp_msg_free(p, mctp); |
| 575 | } |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 576 | break; |
| 577 | |
| 578 | case MCTP_HDR_FLAG_SOM: |
| 579 | /* start of a new message - start the new context for |
| 580 | * future message reception. If an existing context is |
| 581 | * already present, drop it. */ |
Jeremy Kerr | 1a4ec3c | 2019-09-03 11:01:50 +0800 | [diff] [blame] | 582 | ctx = mctp_msg_ctx_lookup(mctp, hdr->src, hdr->dest, tag); |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 583 | if (ctx) { |
| 584 | mctp_msg_ctx_reset(ctx); |
| 585 | } else { |
Patrick Williams | a721c2d | 2022-12-04 14:30:26 -0600 | [diff] [blame] | 586 | ctx = mctp_msg_ctx_create(mctp, hdr->src, hdr->dest, |
| 587 | tag); |
Sumanth Bhat | 34d4c96 | 2021-06-16 12:50:48 +0530 | [diff] [blame] | 588 | /* If context creation fails due to exhaution of contexts we |
| 589 | * can support, drop the packet */ |
| 590 | if (!ctx) { |
| 591 | mctp_prdebug("Context buffers exhausted."); |
| 592 | goto out; |
| 593 | } |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 594 | } |
| 595 | |
Sumanth Bhat | 69f545f | 2021-05-18 09:16:43 +0000 | [diff] [blame] | 596 | /* Save the fragment size, subsequent middle fragments |
| 597 | * should of the same size */ |
| 598 | ctx->fragment_size = mctp_pktbuf_size(pkt); |
| 599 | |
Matt Johnston | 4a09e1d | 2024-09-13 14:55:58 +0800 | [diff] [blame] | 600 | rc = mctp_msg_ctx_add_pkt(ctx, pkt); |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 601 | if (rc) { |
Matt Johnston | 4a09e1d | 2024-09-13 14:55:58 +0800 | [diff] [blame] | 602 | mctp_msg_ctx_drop(bus, ctx); |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 603 | } else { |
| 604 | ctx->last_seq = seq; |
| 605 | } |
| 606 | |
| 607 | break; |
| 608 | |
| 609 | case MCTP_HDR_FLAG_EOM: |
Jeremy Kerr | 1a4ec3c | 2019-09-03 11:01:50 +0800 | [diff] [blame] | 610 | ctx = mctp_msg_ctx_lookup(mctp, hdr->src, hdr->dest, tag); |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 611 | if (!ctx) |
Jeremy Kerr | c1693af | 2019-08-05 14:30:59 +0800 | [diff] [blame] | 612 | goto out; |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 613 | |
Ed Tanous | c2def9f | 2019-02-21 08:33:08 -0800 | [diff] [blame] | 614 | exp_seq = (ctx->last_seq + 1) % 4; |
| 615 | |
| 616 | if (exp_seq != seq) { |
| 617 | mctp_prdebug( |
| 618 | "Sequence number %d does not match expected %d", |
| 619 | seq, exp_seq); |
Matt Johnston | 4a09e1d | 2024-09-13 14:55:58 +0800 | [diff] [blame] | 620 | mctp_msg_ctx_drop(bus, ctx); |
Jeremy Kerr | c1693af | 2019-08-05 14:30:59 +0800 | [diff] [blame] | 621 | goto out; |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 622 | } |
| 623 | |
Sumanth Bhat | 69f545f | 2021-05-18 09:16:43 +0000 | [diff] [blame] | 624 | len = mctp_pktbuf_size(pkt); |
| 625 | |
| 626 | if (len > ctx->fragment_size) { |
Patrick Williams | a721c2d | 2022-12-04 14:30:26 -0600 | [diff] [blame] | 627 | mctp_prdebug("Unexpected fragment size. Expected" |
| 628 | " less than %zu, received = %zu", |
| 629 | ctx->fragment_size, len); |
Matt Johnston | 4a09e1d | 2024-09-13 14:55:58 +0800 | [diff] [blame] | 630 | mctp_msg_ctx_drop(bus, ctx); |
Sumanth Bhat | 69f545f | 2021-05-18 09:16:43 +0000 | [diff] [blame] | 631 | goto out; |
| 632 | } |
| 633 | |
Matt Johnston | 4a09e1d | 2024-09-13 14:55:58 +0800 | [diff] [blame] | 634 | rc = mctp_msg_ctx_add_pkt(ctx, pkt); |
Jeremy Kerr | 1a4ec3c | 2019-09-03 11:01:50 +0800 | [diff] [blame] | 635 | if (!rc) |
Sumanth Bhat | f39c385 | 2022-01-10 17:04:10 +0530 | [diff] [blame] | 636 | mctp_rx(mctp, bus, ctx->src, ctx->dest, tag_owner, tag, |
| 637 | ctx->buf, ctx->buf_size); |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 638 | |
Matt Johnston | 4a09e1d | 2024-09-13 14:55:58 +0800 | [diff] [blame] | 639 | mctp_msg_ctx_drop(bus, ctx); |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 640 | break; |
Ed Tanous | c2def9f | 2019-02-21 08:33:08 -0800 | [diff] [blame] | 641 | |
| 642 | case 0: |
| 643 | /* Neither SOM nor EOM */ |
Patrick Williams | a721c2d | 2022-12-04 14:30:26 -0600 | [diff] [blame] | 644 | ctx = mctp_msg_ctx_lookup(mctp, hdr->src, hdr->dest, tag); |
Ed Tanous | c2def9f | 2019-02-21 08:33:08 -0800 | [diff] [blame] | 645 | if (!ctx) |
Jeremy Kerr | c1693af | 2019-08-05 14:30:59 +0800 | [diff] [blame] | 646 | goto out; |
Ed Tanous | c2def9f | 2019-02-21 08:33:08 -0800 | [diff] [blame] | 647 | |
| 648 | exp_seq = (ctx->last_seq + 1) % 4; |
| 649 | if (exp_seq != seq) { |
| 650 | mctp_prdebug( |
| 651 | "Sequence number %d does not match expected %d", |
| 652 | seq, exp_seq); |
Matt Johnston | 4a09e1d | 2024-09-13 14:55:58 +0800 | [diff] [blame] | 653 | mctp_msg_ctx_drop(bus, ctx); |
Jeremy Kerr | c1693af | 2019-08-05 14:30:59 +0800 | [diff] [blame] | 654 | goto out; |
Ed Tanous | c2def9f | 2019-02-21 08:33:08 -0800 | [diff] [blame] | 655 | } |
| 656 | |
Sumanth Bhat | 69f545f | 2021-05-18 09:16:43 +0000 | [diff] [blame] | 657 | len = mctp_pktbuf_size(pkt); |
| 658 | |
| 659 | if (len != ctx->fragment_size) { |
Patrick Williams | a721c2d | 2022-12-04 14:30:26 -0600 | [diff] [blame] | 660 | mctp_prdebug("Unexpected fragment size. Expected = %zu " |
| 661 | "received = %zu", |
| 662 | ctx->fragment_size, len); |
Matt Johnston | 4a09e1d | 2024-09-13 14:55:58 +0800 | [diff] [blame] | 663 | mctp_msg_ctx_drop(bus, ctx); |
Sumanth Bhat | 69f545f | 2021-05-18 09:16:43 +0000 | [diff] [blame] | 664 | goto out; |
| 665 | } |
| 666 | |
Matt Johnston | 4a09e1d | 2024-09-13 14:55:58 +0800 | [diff] [blame] | 667 | rc = mctp_msg_ctx_add_pkt(ctx, pkt); |
Ed Tanous | c2def9f | 2019-02-21 08:33:08 -0800 | [diff] [blame] | 668 | if (rc) { |
Matt Johnston | 4a09e1d | 2024-09-13 14:55:58 +0800 | [diff] [blame] | 669 | mctp_msg_ctx_drop(bus, ctx); |
Jeremy Kerr | c1693af | 2019-08-05 14:30:59 +0800 | [diff] [blame] | 670 | goto out; |
Ed Tanous | c2def9f | 2019-02-21 08:33:08 -0800 | [diff] [blame] | 671 | } |
| 672 | ctx->last_seq = seq; |
| 673 | |
| 674 | break; |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 675 | } |
Jeremy Kerr | c1693af | 2019-08-05 14:30:59 +0800 | [diff] [blame] | 676 | out: |
Matt Johnston | 4a09e1d | 2024-09-13 14:55:58 +0800 | [diff] [blame] | 677 | return; |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 678 | } |
| 679 | |
Patrick Williams | a721c2d | 2022-12-04 14:30:26 -0600 | [diff] [blame] | 680 | static int mctp_packet_tx(struct mctp_bus *bus, struct mctp_pktbuf *pkt) |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 681 | { |
Andrew Jeffery | 5d3d4e6 | 2021-08-20 16:44:40 +0930 | [diff] [blame] | 682 | struct mctp *mctp = bus->binding->mctp; |
| 683 | |
Matt Johnston | 4a09e1d | 2024-09-13 14:55:58 +0800 | [diff] [blame] | 684 | if (bus->state != mctp_bus_state_tx_enabled) { |
| 685 | mctp_prdebug("tx with bus disabled"); |
Jeremy Kerr | 1cd3118 | 2019-02-27 18:01:00 +0800 | [diff] [blame] | 686 | return -1; |
Matt Johnston | 4a09e1d | 2024-09-13 14:55:58 +0800 | [diff] [blame] | 687 | } |
Jeremy Kerr | 1cd3118 | 2019-02-27 18:01:00 +0800 | [diff] [blame] | 688 | |
Andrew Jeffery | 5d3d4e6 | 2021-08-20 16:44:40 +0930 | [diff] [blame] | 689 | if (mctp->capture) |
Rashmica Gupta | f298897 | 2022-11-09 12:26:44 +1100 | [diff] [blame] | 690 | mctp->capture(pkt, MCTP_MESSAGE_CAPTURE_OUTGOING, |
| 691 | mctp->capture_data); |
Andrew Jeffery | 5d3d4e6 | 2021-08-20 16:44:40 +0930 | [diff] [blame] | 692 | |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 693 | return bus->binding->tx(bus->binding, pkt); |
| 694 | } |
| 695 | |
Matt Johnston | 4a09e1d | 2024-09-13 14:55:58 +0800 | [diff] [blame] | 696 | /* Returns a pointer to the binding's tx_storage */ |
| 697 | static struct mctp_pktbuf *mctp_next_tx_pkt(struct mctp_bus *bus) |
| 698 | { |
| 699 | if (!bus->tx_msg) { |
| 700 | return NULL; |
| 701 | } |
| 702 | |
| 703 | size_t p = bus->tx_msgpos; |
| 704 | size_t msg_len = bus->tx_msglen; |
| 705 | size_t payload_len = msg_len - p; |
| 706 | size_t max_payload_len = MCTP_BODY_SIZE(bus->binding->pkt_size); |
| 707 | if (payload_len > max_payload_len) |
| 708 | payload_len = max_payload_len; |
| 709 | |
| 710 | struct mctp_pktbuf *pkt = |
| 711 | mctp_pktbuf_init(bus->binding, bus->binding->tx_storage); |
| 712 | struct mctp_hdr *hdr = mctp_pktbuf_hdr(pkt); |
| 713 | |
| 714 | hdr->ver = bus->binding->version & 0xf; |
| 715 | hdr->dest = bus->tx_dest; |
| 716 | hdr->src = bus->tx_src; |
| 717 | hdr->flags_seq_tag = (bus->tx_to << MCTP_HDR_TO_SHIFT) | |
| 718 | (bus->tx_tag << MCTP_HDR_TAG_SHIFT); |
| 719 | |
| 720 | if (p == 0) |
| 721 | hdr->flags_seq_tag |= MCTP_HDR_FLAG_SOM; |
| 722 | if (p + payload_len >= msg_len) |
| 723 | hdr->flags_seq_tag |= MCTP_HDR_FLAG_EOM; |
| 724 | hdr->flags_seq_tag |= bus->tx_seq << MCTP_HDR_SEQ_SHIFT; |
| 725 | |
| 726 | memcpy(mctp_pktbuf_data(pkt), (uint8_t *)bus->tx_msg + p, payload_len); |
| 727 | pkt->end = pkt->start + sizeof(*hdr) + payload_len; |
| 728 | bus->tx_pktlen = payload_len; |
| 729 | |
| 730 | mctp_prdebug( |
| 731 | "tx dst %d tag %d payload len %zu seq %d. msg pos %zu len %zu", |
| 732 | hdr->dest, bus->tx_tag, payload_len, bus->tx_seq, p, msg_len); |
| 733 | |
| 734 | return pkt; |
| 735 | } |
| 736 | |
| 737 | /* Called when a packet has successfully been sent */ |
| 738 | static void mctp_tx_complete(struct mctp_bus *bus) |
| 739 | { |
| 740 | if (!bus->tx_msg) { |
| 741 | mctp_prdebug("tx complete no message"); |
| 742 | return; |
| 743 | } |
| 744 | |
| 745 | bus->tx_seq = (bus->tx_seq + 1) & MCTP_HDR_SEQ_MASK; |
| 746 | bus->tx_msgpos += bus->tx_pktlen; |
| 747 | |
| 748 | if (bus->tx_msgpos >= bus->tx_msglen) { |
| 749 | __mctp_msg_free(bus->tx_msg, bus->binding->mctp); |
| 750 | bus->tx_msg = NULL; |
| 751 | } |
| 752 | } |
| 753 | |
Jeremy Kerr | cc2458d | 2019-03-01 08:23:33 +0800 | [diff] [blame] | 754 | static void mctp_send_tx_queue(struct mctp_bus *bus) |
Jeremy Kerr | 1cd3118 | 2019-02-27 18:01:00 +0800 | [diff] [blame] | 755 | { |
| 756 | struct mctp_pktbuf *pkt; |
| 757 | |
Matt Johnston | 4a09e1d | 2024-09-13 14:55:58 +0800 | [diff] [blame] | 758 | while (bus->tx_msg && bus->state == mctp_bus_state_tx_enabled) { |
Jeremy Kerr | 1cd3118 | 2019-02-27 18:01:00 +0800 | [diff] [blame] | 759 | int rc; |
| 760 | |
Matt Johnston | 4a09e1d | 2024-09-13 14:55:58 +0800 | [diff] [blame] | 761 | pkt = mctp_next_tx_pkt(bus); |
| 762 | |
Jeremy Kerr | 1cd3118 | 2019-02-27 18:01:00 +0800 | [diff] [blame] | 763 | rc = mctp_packet_tx(bus, pkt); |
Andrew Jeffery | 0721f58 | 2022-09-29 12:12:39 +0930 | [diff] [blame] | 764 | switch (rc) { |
Matt Johnston | 4a09e1d | 2024-09-13 14:55:58 +0800 | [diff] [blame] | 765 | /* If transmission succeded */ |
Andrew Jeffery | 0721f58 | 2022-09-29 12:12:39 +0930 | [diff] [blame] | 766 | case 0: |
Andrew Jeffery | 0721f58 | 2022-09-29 12:12:39 +0930 | [diff] [blame] | 767 | /* Drop the packet */ |
Matt Johnston | 4a09e1d | 2024-09-13 14:55:58 +0800 | [diff] [blame] | 768 | mctp_tx_complete(bus); |
Jeremy Kerr | 1cd3118 | 2019-02-27 18:01:00 +0800 | [diff] [blame] | 769 | break; |
| 770 | |
Matt Johnston | 4a09e1d | 2024-09-13 14:55:58 +0800 | [diff] [blame] | 771 | /* If the binding was busy */ |
Andrew Jeffery | 0721f58 | 2022-09-29 12:12:39 +0930 | [diff] [blame] | 772 | case -EBUSY: |
Matt Johnston | 4a09e1d | 2024-09-13 14:55:58 +0800 | [diff] [blame] | 773 | /* Keep the packet for next try */ |
| 774 | mctp_prdebug("tx EBUSY"); |
| 775 | return; |
| 776 | |
Andrew Jeffery | 0721f58 | 2022-09-29 12:12:39 +0930 | [diff] [blame] | 777 | /* Some other unknown error occurred */ |
| 778 | default: |
Matt Johnston | 4a09e1d | 2024-09-13 14:55:58 +0800 | [diff] [blame] | 779 | /* Drop the packet */ |
| 780 | mctp_prdebug("tx drop %d", rc); |
| 781 | mctp_tx_complete(bus); |
| 782 | return; |
Andrew Jeffery | 0721f58 | 2022-09-29 12:12:39 +0930 | [diff] [blame] | 783 | }; |
Jeremy Kerr | 1cd3118 | 2019-02-27 18:01:00 +0800 | [diff] [blame] | 784 | } |
Jeremy Kerr | 1cd3118 | 2019-02-27 18:01:00 +0800 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | void mctp_binding_set_tx_enabled(struct mctp_binding *binding, bool enable) |
| 788 | { |
| 789 | struct mctp_bus *bus = binding->bus; |
Andrew Jeffery | c61501c | 2021-01-27 23:24:18 +1030 | [diff] [blame] | 790 | |
Patrick Williams | a721c2d | 2022-12-04 14:30:26 -0600 | [diff] [blame] | 791 | switch (bus->state) { |
Andrew Jeffery | c61501c | 2021-01-27 23:24:18 +1030 | [diff] [blame] | 792 | case mctp_bus_state_constructed: |
| 793 | if (!enable) |
| 794 | return; |
| 795 | |
Andrew Jeffery | 1fa707e | 2021-01-28 15:22:11 +1030 | [diff] [blame] | 796 | if (binding->pkt_size < MCTP_PACKET_SIZE(MCTP_BTU)) { |
Patrick Williams | a721c2d | 2022-12-04 14:30:26 -0600 | [diff] [blame] | 797 | mctp_prerr( |
| 798 | "Cannot start %s binding with invalid MTU: %zu", |
| 799 | binding->name, |
| 800 | MCTP_BODY_SIZE(binding->pkt_size)); |
Andrew Jeffery | 1fa707e | 2021-01-28 15:22:11 +1030 | [diff] [blame] | 801 | return; |
| 802 | } |
| 803 | |
Andrew Jeffery | c61501c | 2021-01-27 23:24:18 +1030 | [diff] [blame] | 804 | bus->state = mctp_bus_state_tx_enabled; |
| 805 | mctp_prinfo("%s binding started", binding->name); |
| 806 | return; |
| 807 | case mctp_bus_state_tx_enabled: |
| 808 | if (enable) |
| 809 | return; |
| 810 | |
| 811 | bus->state = mctp_bus_state_tx_disabled; |
| 812 | mctp_prdebug("%s binding Tx disabled", binding->name); |
| 813 | return; |
| 814 | case mctp_bus_state_tx_disabled: |
| 815 | if (!enable) |
| 816 | return; |
| 817 | |
| 818 | bus->state = mctp_bus_state_tx_enabled; |
| 819 | mctp_prdebug("%s binding Tx enabled", binding->name); |
Jeremy Kerr | cc2458d | 2019-03-01 08:23:33 +0800 | [diff] [blame] | 820 | mctp_send_tx_queue(bus); |
Andrew Jeffery | c61501c | 2021-01-27 23:24:18 +1030 | [diff] [blame] | 821 | return; |
| 822 | } |
Jeremy Kerr | 1cd3118 | 2019-02-27 18:01:00 +0800 | [diff] [blame] | 823 | } |
| 824 | |
Andrew Jeffery | b93b611 | 2020-06-05 14:13:44 +0930 | [diff] [blame] | 825 | 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] | 826 | mctp_eid_t dest, bool tag_owner, |
| 827 | uint8_t msg_tag, void *msg, size_t msg_len) |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 828 | { |
Matt Johnston | 4a09e1d | 2024-09-13 14:55:58 +0800 | [diff] [blame] | 829 | size_t max_payload_len; |
| 830 | int rc; |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 831 | |
Matt Johnston | 4a09e1d | 2024-09-13 14:55:58 +0800 | [diff] [blame] | 832 | if (bus->state == mctp_bus_state_constructed) { |
| 833 | rc = -ENXIO; |
| 834 | goto err; |
| 835 | } |
Andrew Jeffery | c61501c | 2021-01-27 23:24:18 +1030 | [diff] [blame] | 836 | |
Matt Johnston | 4a09e1d | 2024-09-13 14:55:58 +0800 | [diff] [blame] | 837 | if ((msg_tag & MCTP_HDR_TAG_MASK) != msg_tag) { |
| 838 | rc = -EINVAL; |
| 839 | goto err; |
| 840 | } |
Sumanth Bhat | f39c385 | 2022-01-10 17:04:10 +0530 | [diff] [blame] | 841 | |
Andrew Jeffery | 1fa707e | 2021-01-28 15:22:11 +1030 | [diff] [blame] | 842 | max_payload_len = MCTP_BODY_SIZE(bus->binding->pkt_size); |
| 843 | |
| 844 | { |
| 845 | const bool valid_mtu = max_payload_len >= MCTP_BTU; |
| 846 | assert(valid_mtu); |
Matt Johnston | 4a09e1d | 2024-09-13 14:55:58 +0800 | [diff] [blame] | 847 | if (!valid_mtu) { |
| 848 | rc = -EINVAL; |
| 849 | goto err; |
| 850 | } |
Andrew Jeffery | 1fa707e | 2021-01-28 15:22:11 +1030 | [diff] [blame] | 851 | } |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 852 | |
Patrick Williams | a721c2d | 2022-12-04 14:30:26 -0600 | [diff] [blame] | 853 | mctp_prdebug( |
| 854 | "%s: Generating packets for transmission of %zu byte message from %hhu to %hhu", |
| 855 | __func__, msg_len, src, dest); |
Andrew Jeffery | 298865f | 2020-02-06 11:51:29 +1030 | [diff] [blame] | 856 | |
Matt Johnston | 4a09e1d | 2024-09-13 14:55:58 +0800 | [diff] [blame] | 857 | if (bus->tx_msg) { |
| 858 | mctp_prdebug("Bus busy"); |
| 859 | rc = -EBUSY; |
| 860 | goto err; |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 861 | } |
| 862 | |
Matt Johnston | 4a09e1d | 2024-09-13 14:55:58 +0800 | [diff] [blame] | 863 | /* Take the message to send */ |
| 864 | bus->tx_msg = msg; |
| 865 | bus->tx_msglen = msg_len; |
| 866 | bus->tx_msgpos = 0; |
| 867 | /* bus->tx_seq is allowed to continue from previous message */ |
| 868 | bus->tx_src = src; |
| 869 | bus->tx_dest = dest; |
| 870 | bus->tx_to = tag_owner; |
| 871 | bus->tx_tag = msg_tag; |
Andrew Jeffery | 298865f | 2020-02-06 11:51:29 +1030 | [diff] [blame] | 872 | |
Jeremy Kerr | cc2458d | 2019-03-01 08:23:33 +0800 | [diff] [blame] | 873 | mctp_send_tx_queue(bus); |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 874 | return 0; |
Matt Johnston | 4a09e1d | 2024-09-13 14:55:58 +0800 | [diff] [blame] | 875 | |
| 876 | err: |
| 877 | __mctp_msg_free(msg, bus->binding->mctp); |
| 878 | return rc; |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 879 | } |
Jeremy Kerr | 1a4ec3c | 2019-09-03 11:01:50 +0800 | [diff] [blame] | 880 | |
Matt Johnston | 4a09e1d | 2024-09-13 14:55:58 +0800 | [diff] [blame] | 881 | int mctp_message_tx_alloced(struct mctp *mctp, mctp_eid_t eid, bool tag_owner, |
| 882 | uint8_t msg_tag, void *msg, size_t msg_len) |
Jeremy Kerr | 1a4ec3c | 2019-09-03 11:01:50 +0800 | [diff] [blame] | 883 | { |
| 884 | struct mctp_bus *bus; |
| 885 | |
Sumanth Bhat | f39c385 | 2022-01-10 17:04:10 +0530 | [diff] [blame] | 886 | /* TODO: Protect against same tag being used across |
| 887 | * different callers */ |
| 888 | if ((msg_tag & MCTP_HDR_TAG_MASK) != msg_tag) { |
| 889 | mctp_prerr("Incorrect message tag %u passed.", msg_tag); |
Matt Johnston | 4a09e1d | 2024-09-13 14:55:58 +0800 | [diff] [blame] | 890 | __mctp_msg_free(msg, mctp); |
Sumanth Bhat | f39c385 | 2022-01-10 17:04:10 +0530 | [diff] [blame] | 891 | return -EINVAL; |
| 892 | } |
| 893 | |
Jeremy Kerr | 1a4ec3c | 2019-09-03 11:01:50 +0800 | [diff] [blame] | 894 | bus = find_bus_for_eid(mctp, eid); |
Matt Johnston | 4a09e1d | 2024-09-13 14:55:58 +0800 | [diff] [blame] | 895 | if (!bus) { |
| 896 | __mctp_msg_free(msg, mctp); |
Brad Bishop | 663ec39 | 2021-10-07 21:16:48 -0400 | [diff] [blame] | 897 | return 0; |
Matt Johnston | 4a09e1d | 2024-09-13 14:55:58 +0800 | [diff] [blame] | 898 | } |
Brad Bishop | 663ec39 | 2021-10-07 21:16:48 -0400 | [diff] [blame] | 899 | |
Sumanth Bhat | f39c385 | 2022-01-10 17:04:10 +0530 | [diff] [blame] | 900 | return mctp_message_tx_on_bus(bus, bus->eid, eid, tag_owner, msg_tag, |
| 901 | msg, msg_len); |
Jeremy Kerr | 1a4ec3c | 2019-09-03 11:01:50 +0800 | [diff] [blame] | 902 | } |
Matt Johnston | 4a09e1d | 2024-09-13 14:55:58 +0800 | [diff] [blame] | 903 | |
| 904 | int mctp_message_tx(struct mctp *mctp, mctp_eid_t eid, bool tag_owner, |
| 905 | uint8_t msg_tag, const void *msg, size_t msg_len) |
| 906 | { |
| 907 | void *copy = mctp_msg_dup(msg, msg_len, mctp); |
| 908 | if (!copy) { |
| 909 | return -ENOMEM; |
| 910 | } |
| 911 | |
| 912 | return mctp_message_tx_alloced(mctp, eid, tag_owner, msg_tag, copy, |
| 913 | msg_len); |
| 914 | } |
| 915 | |
Matt Johnston | 44e64df | 2024-11-05 16:59:42 +0800 | [diff] [blame] | 916 | void mctp_set_now_op(struct mctp *mctp, uint64_t (*now)(void *), void *ctx) |
| 917 | { |
| 918 | assert(now); |
| 919 | mctp->platform_now = now; |
| 920 | mctp->platform_now_ctx = ctx; |
| 921 | } |
| 922 | |
| 923 | uint64_t mctp_now(struct mctp *mctp) |
| 924 | { |
| 925 | assert(mctp->platform_now); |
| 926 | return mctp->platform_now(mctp->platform_now_ctx); |
| 927 | } |
| 928 | |
Matt Johnston | 61c9599 | 2024-09-16 16:50:35 +0800 | [diff] [blame] | 929 | static void mctp_dealloc_tag(struct mctp_bus *bus, mctp_eid_t local, |
| 930 | mctp_eid_t remote, uint8_t tag) |
| 931 | { |
| 932 | struct mctp *mctp = bus->binding->mctp; |
Matt Johnston | 44e64df | 2024-11-05 16:59:42 +0800 | [diff] [blame] | 933 | if (local == 0) { |
Matt Johnston | 61c9599 | 2024-09-16 16:50:35 +0800 | [diff] [blame] | 934 | return; |
| 935 | } |
| 936 | |
| 937 | for (size_t i = 0; i < ARRAY_SIZE(mctp->req_tags); i++) { |
| 938 | struct mctp_req_tag *r = &mctp->req_tags[i]; |
| 939 | if (r->local == local && r->remote == remote && r->tag == tag) { |
| 940 | r->local = 0; |
| 941 | r->remote = 0; |
| 942 | r->tag = 0; |
Matt Johnston | 44e64df | 2024-11-05 16:59:42 +0800 | [diff] [blame] | 943 | r->expiry = 0; |
Matt Johnston | 61c9599 | 2024-09-16 16:50:35 +0800 | [diff] [blame] | 944 | return; |
| 945 | } |
| 946 | } |
| 947 | } |
| 948 | |
| 949 | static int mctp_alloc_tag(struct mctp *mctp, mctp_eid_t local, |
| 950 | mctp_eid_t remote, uint8_t *ret_tag) |
| 951 | { |
| 952 | assert(local != 0); |
Matt Johnston | 44e64df | 2024-11-05 16:59:42 +0800 | [diff] [blame] | 953 | uint64_t now = mctp_now(mctp); |
Matt Johnston | 61c9599 | 2024-09-16 16:50:35 +0800 | [diff] [blame] | 954 | |
| 955 | uint8_t used = 0; |
| 956 | struct mctp_req_tag *spare = NULL; |
| 957 | /* Find which tags and slots are used/spare */ |
| 958 | for (size_t i = 0; i < ARRAY_SIZE(mctp->req_tags); i++) { |
| 959 | struct mctp_req_tag *r = &mctp->req_tags[i]; |
Matt Johnston | 44e64df | 2024-11-05 16:59:42 +0800 | [diff] [blame] | 960 | if (r->local == 0 || r->expiry < now) { |
Matt Johnston | 61c9599 | 2024-09-16 16:50:35 +0800 | [diff] [blame] | 961 | spare = r; |
| 962 | } else { |
Matt Johnston | 61c9599 | 2024-09-16 16:50:35 +0800 | [diff] [blame] | 963 | if (r->local == local && r->remote == remote) { |
| 964 | used |= 1 << r->tag; |
| 965 | } |
| 966 | } |
| 967 | } |
| 968 | |
| 969 | if (spare == NULL) { |
| 970 | // All req_tag slots are in-use |
| 971 | return -EBUSY; |
| 972 | } |
| 973 | |
| 974 | for (uint8_t t = 0; t < 8; t++) { |
| 975 | uint8_t tag = (t + mctp->tag_round_robin) % 8; |
| 976 | if ((used & 1 << tag) == 0) { |
| 977 | spare->local = local; |
| 978 | spare->remote = remote; |
| 979 | spare->tag = tag; |
Matt Johnston | 44e64df | 2024-11-05 16:59:42 +0800 | [diff] [blame] | 980 | spare->expiry = now + MCTP_TAG_TIMEOUT; |
Matt Johnston | 61c9599 | 2024-09-16 16:50:35 +0800 | [diff] [blame] | 981 | *ret_tag = tag; |
| 982 | mctp->tag_round_robin = (tag + 1) % 8; |
| 983 | return 0; |
| 984 | } |
| 985 | } |
| 986 | |
| 987 | // All 8 tags are used for this src/dest pair |
| 988 | return -EBUSY; |
| 989 | } |
| 990 | |
| 991 | int mctp_message_tx_request(struct mctp *mctp, mctp_eid_t eid, void *msg, |
| 992 | size_t msg_len, uint8_t *ret_alloc_msg_tag) |
| 993 | { |
| 994 | int rc; |
| 995 | struct mctp_bus *bus; |
| 996 | |
| 997 | bus = find_bus_for_eid(mctp, eid); |
| 998 | if (!bus) { |
| 999 | __mctp_msg_free(msg, mctp); |
| 1000 | return 0; |
| 1001 | } |
| 1002 | |
| 1003 | uint8_t alloc_tag; |
| 1004 | rc = mctp_alloc_tag(mctp, bus->eid, eid, &alloc_tag); |
| 1005 | if (rc) { |
| 1006 | mctp_prdebug("Failed allocating tag"); |
| 1007 | __mctp_msg_free(msg, mctp); |
| 1008 | return rc; |
| 1009 | } |
| 1010 | |
| 1011 | if (ret_alloc_msg_tag) { |
| 1012 | *ret_alloc_msg_tag = alloc_tag; |
| 1013 | } |
| 1014 | |
| 1015 | return mctp_message_tx_alloced(mctp, eid, true, alloc_tag, msg, |
| 1016 | msg_len); |
| 1017 | } |
| 1018 | |
Matt Johnston | 4a09e1d | 2024-09-13 14:55:58 +0800 | [diff] [blame] | 1019 | bool mctp_is_tx_ready(struct mctp *mctp, mctp_eid_t eid) |
| 1020 | { |
| 1021 | struct mctp_bus *bus; |
| 1022 | |
| 1023 | bus = find_bus_for_eid(mctp, eid); |
| 1024 | if (!bus) { |
| 1025 | return true; |
| 1026 | } |
| 1027 | return bus->tx_msg == NULL; |
| 1028 | } |
| 1029 | |
| 1030 | void *mctp_get_alloc_ctx(struct mctp *mctp) |
| 1031 | { |
| 1032 | return mctp->alloc_ctx; |
| 1033 | } |
| 1034 | |
| 1035 | void mctp_set_alloc_ctx(struct mctp *mctp, void *ctx) |
| 1036 | { |
| 1037 | mctp->alloc_ctx = ctx; |
| 1038 | } |