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