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