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