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