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