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