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