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> |
| 4 | #include <stdarg.h> |
| 5 | #include <stddef.h> |
| 6 | #include <stdint.h> |
| 7 | #include <stdio.h> |
| 8 | #include <stdlib.h> |
| 9 | #include <string.h> |
| 10 | |
| 11 | #undef pr_fmt |
| 12 | #define pr_fmt(fmt) "core: " fmt |
| 13 | |
| 14 | #include "libmctp.h" |
| 15 | #include "libmctp-alloc.h" |
| 16 | #include "libmctp-log.h" |
| 17 | |
| 18 | /* Internal data structures */ |
| 19 | |
| 20 | struct mctp_bus { |
| 21 | mctp_eid_t eid; |
| 22 | struct mctp_binding *binding; |
Jeremy Kerr | 1cd3118 | 2019-02-27 18:01:00 +0800 | [diff] [blame] | 23 | bool tx_enabled; |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 24 | |
Jeremy Kerr | cc2458d | 2019-03-01 08:23:33 +0800 | [diff] [blame] | 25 | struct mctp_pktbuf *tx_queue_head; |
| 26 | struct mctp_pktbuf *tx_queue_tail; |
| 27 | |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 28 | /* todo: routing */ |
| 29 | }; |
| 30 | |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 31 | struct mctp_msg_ctx { |
| 32 | uint8_t src; |
Jeremy Kerr | 1a4ec3c | 2019-09-03 11:01:50 +0800 | [diff] [blame] | 33 | uint8_t dest; |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 34 | uint8_t tag; |
| 35 | uint8_t last_seq; |
| 36 | void *buf; |
| 37 | size_t buf_size; |
| 38 | size_t buf_alloc_size; |
| 39 | }; |
| 40 | |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 41 | struct mctp { |
Jeremy Kerr | 1a4ec3c | 2019-09-03 11:01:50 +0800 | [diff] [blame] | 42 | int n_busses; |
| 43 | struct mctp_bus *busses; |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 44 | |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 45 | /* Message RX callback */ |
| 46 | mctp_rx_fn message_rx; |
| 47 | void *message_rx_data; |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 48 | |
| 49 | /* Message reassembly. |
| 50 | * @todo: flexible context count |
| 51 | */ |
| 52 | struct mctp_msg_ctx msg_ctxs[16]; |
Jeremy Kerr | 1a4ec3c | 2019-09-03 11:01:50 +0800 | [diff] [blame] | 53 | |
| 54 | enum { |
| 55 | ROUTE_ENDPOINT, |
| 56 | ROUTE_BRIDGE, |
| 57 | } route_policy; |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 58 | }; |
| 59 | |
| 60 | #ifndef BUILD_ASSERT |
| 61 | #define BUILD_ASSERT(x) \ |
| 62 | do { (void)sizeof(char[0-(!(x))]); } while (0) |
| 63 | #endif |
| 64 | |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 65 | #ifndef ARRAY_SIZE |
| 66 | #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) |
| 67 | #endif |
| 68 | |
Jeremy Kerr | 1a4ec3c | 2019-09-03 11:01:50 +0800 | [diff] [blame] | 69 | static int mctp_message_tx_on_bus(struct mctp *mctp, struct mctp_bus *bus, |
| 70 | mctp_eid_t src, mctp_eid_t dest, void *msg, size_t msg_len); |
| 71 | |
Jeremy Kerr | df15f7e | 2019-08-05 15:41:19 +0800 | [diff] [blame] | 72 | 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] | 73 | { |
| 74 | struct mctp_pktbuf *buf; |
Jeremy Kerr | df15f7e | 2019-08-05 15:41:19 +0800 | [diff] [blame] | 75 | size_t size; |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 76 | |
Jeremy Kerr | df15f7e | 2019-08-05 15:41:19 +0800 | [diff] [blame] | 77 | size = binding->pkt_size + binding->pkt_pad; |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 78 | |
| 79 | /* todo: pools */ |
Jeremy Kerr | df15f7e | 2019-08-05 15:41:19 +0800 | [diff] [blame] | 80 | buf = __mctp_alloc(sizeof(*buf) + size); |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 81 | |
Jeremy Kerr | df15f7e | 2019-08-05 15:41:19 +0800 | [diff] [blame] | 82 | buf->size = size; |
| 83 | buf->start = binding->pkt_pad; |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 84 | buf->end = buf->start + len; |
| 85 | buf->mctp_hdr_off = buf->start; |
Jeremy Kerr | dd109f1 | 2019-04-04 11:46:49 +0800 | [diff] [blame] | 86 | buf->next = NULL; |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 87 | |
| 88 | return buf; |
| 89 | } |
| 90 | |
| 91 | void mctp_pktbuf_free(struct mctp_pktbuf *pkt) |
| 92 | { |
| 93 | __mctp_free(pkt); |
| 94 | } |
| 95 | |
| 96 | struct mctp_hdr *mctp_pktbuf_hdr(struct mctp_pktbuf *pkt) |
| 97 | { |
| 98 | return (void *)pkt->data + pkt->mctp_hdr_off; |
| 99 | } |
| 100 | |
| 101 | void *mctp_pktbuf_data(struct mctp_pktbuf *pkt) |
| 102 | { |
| 103 | return (void *)pkt->data + pkt->mctp_hdr_off + sizeof(struct mctp_hdr); |
| 104 | } |
| 105 | |
| 106 | uint8_t mctp_pktbuf_size(struct mctp_pktbuf *pkt) |
| 107 | { |
| 108 | return pkt->end - pkt->start; |
| 109 | } |
| 110 | |
Jeremy Kerr | df15f7e | 2019-08-05 15:41:19 +0800 | [diff] [blame] | 111 | void *mctp_pktbuf_alloc_start(struct mctp_pktbuf *pkt, size_t size) |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 112 | { |
| 113 | assert(size <= pkt->start); |
| 114 | pkt->start -= size; |
| 115 | return pkt->data + pkt->start; |
| 116 | } |
| 117 | |
Jeremy Kerr | df15f7e | 2019-08-05 15:41:19 +0800 | [diff] [blame] | 118 | void *mctp_pktbuf_alloc_end(struct mctp_pktbuf *pkt, size_t size) |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 119 | { |
| 120 | void *buf; |
| 121 | |
Jeremy Kerr | df15f7e | 2019-08-05 15:41:19 +0800 | [diff] [blame] | 122 | assert(size < (pkt->size - pkt->end)); |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 123 | buf = pkt->data + pkt->end; |
| 124 | pkt->end += size; |
| 125 | return buf; |
| 126 | } |
| 127 | |
Jeremy Kerr | df15f7e | 2019-08-05 15:41:19 +0800 | [diff] [blame] | 128 | 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] | 129 | { |
| 130 | void *p; |
| 131 | |
Jeremy Kerr | df15f7e | 2019-08-05 15:41:19 +0800 | [diff] [blame] | 132 | if (pkt->end + len > pkt->size) |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 133 | return -1; |
| 134 | |
| 135 | p = pkt->data + pkt->end; |
| 136 | |
| 137 | pkt->end += len; |
| 138 | memcpy(p, data, len); |
| 139 | |
| 140 | return 0; |
| 141 | } |
| 142 | |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 143 | /* Message reassembly */ |
| 144 | static struct mctp_msg_ctx *mctp_msg_ctx_lookup(struct mctp *mctp, |
Jeremy Kerr | 1a4ec3c | 2019-09-03 11:01:50 +0800 | [diff] [blame] | 145 | uint8_t src, uint8_t dest, uint8_t tag) |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 146 | { |
| 147 | unsigned int i; |
| 148 | |
| 149 | /* @todo: better lookup, if we add support for more outstanding |
| 150 | * message contexts */ |
| 151 | for (i = 0; i < ARRAY_SIZE(mctp->msg_ctxs); i++) { |
| 152 | struct mctp_msg_ctx *ctx = &mctp->msg_ctxs[i]; |
Jeremy Kerr | 1a4ec3c | 2019-09-03 11:01:50 +0800 | [diff] [blame] | 153 | if (ctx->src == src && ctx->dest == dest && ctx->tag == tag) |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 154 | return ctx; |
| 155 | } |
| 156 | |
| 157 | return NULL; |
| 158 | } |
| 159 | |
| 160 | static struct mctp_msg_ctx *mctp_msg_ctx_create(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 | { |
Jeremy Kerr | 11a234e | 2019-02-27 17:59:53 +0800 | [diff] [blame] | 163 | struct mctp_msg_ctx *ctx = NULL; |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 164 | unsigned int i; |
| 165 | |
| 166 | for (i = 0; i < ARRAY_SIZE(mctp->msg_ctxs); i++) { |
| 167 | struct mctp_msg_ctx *tmp = &mctp->msg_ctxs[i]; |
| 168 | if (!tmp->src) { |
| 169 | ctx = tmp; |
| 170 | break; |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | if (!ctx) |
| 175 | return NULL; |
| 176 | |
| 177 | ctx->src = src; |
Jeremy Kerr | 1a4ec3c | 2019-09-03 11:01:50 +0800 | [diff] [blame] | 178 | ctx->dest = dest; |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 179 | ctx->tag = tag; |
Jeremy Kerr | 9a3da81 | 2019-08-02 15:57:53 +0800 | [diff] [blame] | 180 | ctx->buf_size = 0; |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 181 | |
| 182 | return ctx; |
| 183 | } |
| 184 | |
| 185 | static void mctp_msg_ctx_drop(struct mctp_msg_ctx *ctx) |
| 186 | { |
| 187 | ctx->src = 0; |
| 188 | } |
| 189 | |
| 190 | static void mctp_msg_ctx_reset(struct mctp_msg_ctx *ctx) |
| 191 | { |
| 192 | ctx->buf_size = 0; |
| 193 | } |
| 194 | |
| 195 | static int mctp_msg_ctx_add_pkt(struct mctp_msg_ctx *ctx, |
| 196 | struct mctp_pktbuf *pkt) |
| 197 | { |
| 198 | size_t len; |
| 199 | |
| 200 | len = mctp_pktbuf_size(pkt) - sizeof(struct mctp_hdr); |
| 201 | |
| 202 | if (ctx->buf_size + len > ctx->buf_alloc_size) { |
| 203 | size_t new_alloc_size; |
Andrew Jeffery | 00ecc6c | 2020-03-10 23:16:53 +1030 | [diff] [blame^] | 204 | void *lbuf; |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 205 | |
| 206 | /* @todo: finer-grained allocation, size limits */ |
| 207 | if (!ctx->buf_alloc_size) { |
| 208 | new_alloc_size = 4096; |
| 209 | } else { |
| 210 | new_alloc_size = ctx->buf_alloc_size * 2; |
| 211 | } |
Andrew Jeffery | 00ecc6c | 2020-03-10 23:16:53 +1030 | [diff] [blame^] | 212 | |
| 213 | lbuf = __mctp_realloc(ctx->buf, new_alloc_size); |
| 214 | if (lbuf) { |
| 215 | ctx->buf = lbuf; |
| 216 | ctx->buf_alloc_size = new_alloc_size; |
| 217 | } else { |
| 218 | __mctp_free(ctx->buf); |
| 219 | return -1; |
| 220 | } |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | memcpy(ctx->buf + ctx->buf_size, mctp_pktbuf_data(pkt), len); |
| 224 | ctx->buf_size += len; |
| 225 | |
| 226 | return 0; |
| 227 | } |
| 228 | |
| 229 | /* Core API functions */ |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 230 | struct mctp *mctp_init(void) |
| 231 | { |
| 232 | struct mctp *mctp; |
| 233 | |
| 234 | mctp = __mctp_alloc(sizeof(*mctp)); |
| 235 | memset(mctp, 0, sizeof(*mctp)); |
| 236 | |
| 237 | return mctp; |
| 238 | } |
| 239 | |
| 240 | int mctp_set_rx_all(struct mctp *mctp, mctp_rx_fn fn, void *data) |
| 241 | { |
| 242 | mctp->message_rx = fn; |
| 243 | mctp->message_rx_data = data; |
| 244 | return 0; |
| 245 | } |
| 246 | |
| 247 | static struct mctp_bus *find_bus_for_eid(struct mctp *mctp, |
| 248 | mctp_eid_t dest __attribute__((unused))) |
| 249 | { |
Jeremy Kerr | 1a4ec3c | 2019-09-03 11:01:50 +0800 | [diff] [blame] | 250 | /* for now, just use the first bus. For full routing support, |
| 251 | * we will need a table of neighbours */ |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 252 | return &mctp->busses[0]; |
| 253 | } |
| 254 | |
Jeremy Kerr | 7520cec | 2019-03-01 07:13:18 +0800 | [diff] [blame] | 255 | int mctp_register_bus(struct mctp *mctp, |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 256 | struct mctp_binding *binding, |
| 257 | mctp_eid_t eid) |
| 258 | { |
Jeremy Kerr | 7520cec | 2019-03-01 07:13:18 +0800 | [diff] [blame] | 259 | /* todo: multiple busses */ |
Jeremy Kerr | 1a4ec3c | 2019-09-03 11:01:50 +0800 | [diff] [blame] | 260 | assert(mctp->n_busses == 0); |
| 261 | mctp->n_busses = 1; |
| 262 | mctp->busses = __mctp_alloc(sizeof(struct mctp_bus)); |
James Feist | 62d7236 | 2019-12-13 13:43:32 -0800 | [diff] [blame] | 263 | memset(mctp->busses, 0, sizeof(struct mctp_bus)); |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 264 | mctp->busses[0].binding = binding; |
| 265 | mctp->busses[0].eid = eid; |
Jeremy Kerr | 7520cec | 2019-03-01 07:13:18 +0800 | [diff] [blame] | 266 | binding->bus = &mctp->busses[0]; |
Jeremy Kerr | 0a00dca | 2019-03-01 08:01:35 +0800 | [diff] [blame] | 267 | binding->mctp = mctp; |
Jeremy Kerr | 1a4ec3c | 2019-09-03 11:01:50 +0800 | [diff] [blame] | 268 | mctp->route_policy = ROUTE_ENDPOINT; |
Jeremy Kerr | 3b36d17 | 2019-09-04 11:56:09 +0800 | [diff] [blame] | 269 | |
| 270 | if (binding->start) |
| 271 | binding->start(binding); |
| 272 | |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 273 | return 0; |
| 274 | } |
| 275 | |
Jeremy Kerr | 1a4ec3c | 2019-09-03 11:01:50 +0800 | [diff] [blame] | 276 | int mctp_bridge_busses(struct mctp *mctp, |
| 277 | struct mctp_binding *b1, struct mctp_binding *b2) |
| 278 | { |
| 279 | assert(mctp->n_busses == 0); |
| 280 | mctp->busses = __mctp_alloc(2 * sizeof(struct mctp_bus)); |
James Feist | 62d7236 | 2019-12-13 13:43:32 -0800 | [diff] [blame] | 281 | memset(mctp->busses, 0, 2 * sizeof(struct mctp_bus)); |
Jeremy Kerr | 1a4ec3c | 2019-09-03 11:01:50 +0800 | [diff] [blame] | 282 | mctp->n_busses = 2; |
| 283 | mctp->busses[0].binding = b1; |
| 284 | b1->bus = &mctp->busses[0]; |
| 285 | b1->mctp = mctp; |
| 286 | mctp->busses[1].binding = b2; |
| 287 | b2->bus = &mctp->busses[1]; |
| 288 | b2->mctp = mctp; |
| 289 | |
| 290 | mctp->route_policy = ROUTE_BRIDGE; |
Jeremy Kerr | 3b36d17 | 2019-09-04 11:56:09 +0800 | [diff] [blame] | 291 | |
| 292 | if (b1->start) |
| 293 | b1->start(b1); |
| 294 | |
| 295 | if (b2->start) |
| 296 | b2->start(b2); |
| 297 | |
Jeremy Kerr | 1a4ec3c | 2019-09-03 11:01:50 +0800 | [diff] [blame] | 298 | return 0; |
| 299 | } |
| 300 | |
| 301 | static void mctp_rx(struct mctp *mctp, struct mctp_bus *bus, |
| 302 | mctp_eid_t src, mctp_eid_t dest, void *buf, size_t len) |
| 303 | { |
| 304 | if (mctp->route_policy == ROUTE_ENDPOINT && |
| 305 | dest == bus->eid && mctp->message_rx) |
| 306 | mctp->message_rx(src, mctp->message_rx_data, buf, len); |
| 307 | |
| 308 | if (mctp->route_policy == ROUTE_BRIDGE) { |
| 309 | int i; |
| 310 | |
| 311 | for (i = 0; i < mctp->n_busses; i++) { |
| 312 | struct mctp_bus *dest_bus = &mctp->busses[i]; |
| 313 | if (dest_bus == bus) |
| 314 | continue; |
| 315 | |
| 316 | mctp_message_tx_on_bus(mctp, dest_bus, |
| 317 | src, dest, buf, len); |
| 318 | } |
| 319 | |
| 320 | } |
| 321 | } |
| 322 | |
Jeremy Kerr | 0a00dca | 2019-03-01 08:01:35 +0800 | [diff] [blame] | 323 | void mctp_bus_rx(struct mctp_binding *binding, struct mctp_pktbuf *pkt) |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 324 | { |
Jeremy Kerr | 7520cec | 2019-03-01 07:13:18 +0800 | [diff] [blame] | 325 | struct mctp_bus *bus = binding->bus; |
Jeremy Kerr | 0a00dca | 2019-03-01 08:01:35 +0800 | [diff] [blame] | 326 | struct mctp *mctp = binding->mctp; |
Ed Tanous | c2def9f | 2019-02-21 08:33:08 -0800 | [diff] [blame] | 327 | uint8_t flags, exp_seq, seq, tag; |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 328 | struct mctp_msg_ctx *ctx; |
| 329 | struct mctp_hdr *hdr; |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 330 | size_t len; |
| 331 | void *p; |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 332 | int rc; |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 333 | |
Jeremy Kerr | 7520cec | 2019-03-01 07:13:18 +0800 | [diff] [blame] | 334 | assert(bus); |
| 335 | |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 336 | hdr = mctp_pktbuf_hdr(pkt); |
| 337 | |
Jeremy Kerr | 1a4ec3c | 2019-09-03 11:01:50 +0800 | [diff] [blame] | 338 | /* small optimisation: don't bother reassembly if we're going to |
| 339 | * drop the packet in mctp_rx anyway */ |
| 340 | if (mctp->route_policy == ROUTE_ENDPOINT && hdr->dest != bus->eid) |
Jeremy Kerr | c1693af | 2019-08-05 14:30:59 +0800 | [diff] [blame] | 341 | goto out; |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 342 | |
| 343 | flags = hdr->flags_seq_tag & (MCTP_HDR_FLAG_SOM | MCTP_HDR_FLAG_EOM); |
| 344 | tag = (hdr->flags_seq_tag >> MCTP_HDR_TAG_SHIFT) & MCTP_HDR_TAG_MASK; |
| 345 | seq = (hdr->flags_seq_tag >> MCTP_HDR_SEQ_SHIFT) & MCTP_HDR_SEQ_MASK; |
| 346 | |
| 347 | switch (flags) { |
| 348 | case MCTP_HDR_FLAG_SOM | MCTP_HDR_FLAG_EOM: |
| 349 | /* single-packet message - send straight up to rx function, |
| 350 | * no need to create a message context */ |
| 351 | len = pkt->end - pkt->mctp_hdr_off - sizeof(struct mctp_hdr); |
| 352 | p = pkt->data + pkt->mctp_hdr_off + sizeof(struct mctp_hdr), |
Jeremy Kerr | 1a4ec3c | 2019-09-03 11:01:50 +0800 | [diff] [blame] | 353 | mctp_rx(mctp, bus, hdr->src, hdr->dest, p, len); |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 354 | break; |
| 355 | |
| 356 | case MCTP_HDR_FLAG_SOM: |
| 357 | /* start of a new message - start the new context for |
| 358 | * future message reception. If an existing context is |
| 359 | * already present, drop it. */ |
Jeremy Kerr | 1a4ec3c | 2019-09-03 11:01:50 +0800 | [diff] [blame] | 360 | ctx = mctp_msg_ctx_lookup(mctp, hdr->src, hdr->dest, tag); |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 361 | if (ctx) { |
| 362 | mctp_msg_ctx_reset(ctx); |
| 363 | } else { |
Jeremy Kerr | 1a4ec3c | 2019-09-03 11:01:50 +0800 | [diff] [blame] | 364 | ctx = mctp_msg_ctx_create(mctp, |
| 365 | hdr->src, hdr->dest, tag); |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | rc = mctp_msg_ctx_add_pkt(ctx, pkt); |
| 369 | if (rc) { |
| 370 | mctp_msg_ctx_drop(ctx); |
| 371 | } else { |
| 372 | ctx->last_seq = seq; |
| 373 | } |
| 374 | |
| 375 | break; |
| 376 | |
| 377 | case MCTP_HDR_FLAG_EOM: |
Jeremy Kerr | 1a4ec3c | 2019-09-03 11:01:50 +0800 | [diff] [blame] | 378 | ctx = mctp_msg_ctx_lookup(mctp, hdr->src, hdr->dest, tag); |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 379 | if (!ctx) |
Jeremy Kerr | c1693af | 2019-08-05 14:30:59 +0800 | [diff] [blame] | 380 | goto out; |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 381 | |
Ed Tanous | c2def9f | 2019-02-21 08:33:08 -0800 | [diff] [blame] | 382 | exp_seq = (ctx->last_seq + 1) % 4; |
| 383 | |
| 384 | if (exp_seq != seq) { |
| 385 | mctp_prdebug( |
| 386 | "Sequence number %d does not match expected %d", |
| 387 | seq, exp_seq); |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 388 | mctp_msg_ctx_drop(ctx); |
Jeremy Kerr | c1693af | 2019-08-05 14:30:59 +0800 | [diff] [blame] | 389 | goto out; |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | rc = mctp_msg_ctx_add_pkt(ctx, pkt); |
Jeremy Kerr | 1a4ec3c | 2019-09-03 11:01:50 +0800 | [diff] [blame] | 393 | if (!rc) |
| 394 | mctp_rx(mctp, bus, ctx->src, ctx->dest, |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 395 | ctx->buf, ctx->buf_size); |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 396 | |
| 397 | mctp_msg_ctx_drop(ctx); |
| 398 | break; |
Ed Tanous | c2def9f | 2019-02-21 08:33:08 -0800 | [diff] [blame] | 399 | |
| 400 | case 0: |
| 401 | /* Neither SOM nor EOM */ |
Jeremy Kerr | 1a4ec3c | 2019-09-03 11:01:50 +0800 | [diff] [blame] | 402 | ctx = mctp_msg_ctx_lookup(mctp, hdr->src,hdr->dest, tag); |
Ed Tanous | c2def9f | 2019-02-21 08:33:08 -0800 | [diff] [blame] | 403 | if (!ctx) |
Jeremy Kerr | c1693af | 2019-08-05 14:30:59 +0800 | [diff] [blame] | 404 | goto out; |
Ed Tanous | c2def9f | 2019-02-21 08:33:08 -0800 | [diff] [blame] | 405 | |
| 406 | exp_seq = (ctx->last_seq + 1) % 4; |
| 407 | if (exp_seq != seq) { |
| 408 | mctp_prdebug( |
| 409 | "Sequence number %d does not match expected %d", |
| 410 | seq, exp_seq); |
| 411 | mctp_msg_ctx_drop(ctx); |
Jeremy Kerr | c1693af | 2019-08-05 14:30:59 +0800 | [diff] [blame] | 412 | goto out; |
Ed Tanous | c2def9f | 2019-02-21 08:33:08 -0800 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | rc = mctp_msg_ctx_add_pkt(ctx, pkt); |
| 416 | if (rc) { |
| 417 | mctp_msg_ctx_drop(ctx); |
Jeremy Kerr | c1693af | 2019-08-05 14:30:59 +0800 | [diff] [blame] | 418 | goto out; |
Ed Tanous | c2def9f | 2019-02-21 08:33:08 -0800 | [diff] [blame] | 419 | } |
| 420 | ctx->last_seq = seq; |
| 421 | |
| 422 | break; |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 423 | } |
Jeremy Kerr | c1693af | 2019-08-05 14:30:59 +0800 | [diff] [blame] | 424 | out: |
| 425 | mctp_pktbuf_free(pkt); |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 426 | } |
| 427 | |
Jeremy Kerr | 0a00dca | 2019-03-01 08:01:35 +0800 | [diff] [blame] | 428 | static int mctp_packet_tx(struct mctp_bus *bus, |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 429 | struct mctp_pktbuf *pkt) |
| 430 | { |
Jeremy Kerr | 1cd3118 | 2019-02-27 18:01:00 +0800 | [diff] [blame] | 431 | if (!bus->tx_enabled) |
| 432 | return -1; |
| 433 | |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 434 | return bus->binding->tx(bus->binding, pkt); |
| 435 | } |
| 436 | |
Jeremy Kerr | cc2458d | 2019-03-01 08:23:33 +0800 | [diff] [blame] | 437 | static void mctp_send_tx_queue(struct mctp_bus *bus) |
Jeremy Kerr | 1cd3118 | 2019-02-27 18:01:00 +0800 | [diff] [blame] | 438 | { |
| 439 | struct mctp_pktbuf *pkt; |
| 440 | |
Jeremy Kerr | cc2458d | 2019-03-01 08:23:33 +0800 | [diff] [blame] | 441 | while ((pkt = bus->tx_queue_head)) { |
Jeremy Kerr | 1cd3118 | 2019-02-27 18:01:00 +0800 | [diff] [blame] | 442 | int rc; |
| 443 | |
| 444 | rc = mctp_packet_tx(bus, pkt); |
| 445 | if (rc) |
| 446 | break; |
| 447 | |
Jeremy Kerr | cc2458d | 2019-03-01 08:23:33 +0800 | [diff] [blame] | 448 | bus->tx_queue_head = pkt->next; |
Jeremy Kerr | 1cd3118 | 2019-02-27 18:01:00 +0800 | [diff] [blame] | 449 | mctp_pktbuf_free(pkt); |
| 450 | } |
| 451 | |
Jeremy Kerr | cc2458d | 2019-03-01 08:23:33 +0800 | [diff] [blame] | 452 | if (!bus->tx_queue_head) |
| 453 | bus->tx_queue_tail = NULL; |
Jeremy Kerr | 1cd3118 | 2019-02-27 18:01:00 +0800 | [diff] [blame] | 454 | |
| 455 | } |
| 456 | |
| 457 | void mctp_binding_set_tx_enabled(struct mctp_binding *binding, bool enable) |
| 458 | { |
| 459 | struct mctp_bus *bus = binding->bus; |
| 460 | bus->tx_enabled = enable; |
| 461 | if (enable) |
Jeremy Kerr | cc2458d | 2019-03-01 08:23:33 +0800 | [diff] [blame] | 462 | mctp_send_tx_queue(bus); |
Jeremy Kerr | 1cd3118 | 2019-02-27 18:01:00 +0800 | [diff] [blame] | 463 | } |
| 464 | |
Jeremy Kerr | 1a4ec3c | 2019-09-03 11:01:50 +0800 | [diff] [blame] | 465 | static int mctp_message_tx_on_bus(struct mctp *mctp, struct mctp_bus *bus, |
| 466 | mctp_eid_t src, mctp_eid_t dest, void *msg, size_t msg_len) |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 467 | { |
Jeremy Kerr | df15f7e | 2019-08-05 15:41:19 +0800 | [diff] [blame] | 468 | size_t max_payload_len, payload_len, p; |
Jeremy Kerr | 1cd3118 | 2019-02-27 18:01:00 +0800 | [diff] [blame] | 469 | struct mctp_pktbuf *pkt; |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 470 | struct mctp_hdr *hdr; |
Jeremy Kerr | c855d7b | 2019-08-01 21:18:09 +0800 | [diff] [blame] | 471 | int i; |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 472 | |
Jeremy Kerr | df15f7e | 2019-08-05 15:41:19 +0800 | [diff] [blame] | 473 | max_payload_len = bus->binding->pkt_size - sizeof(*hdr); |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 474 | |
Andrew Jeffery | 298865f | 2020-02-06 11:51:29 +1030 | [diff] [blame] | 475 | mctp_prdebug("%s: Generating packets for transmission of %zu byte message from %hhu to %hhu", |
| 476 | __func__, msg_len, src, dest); |
| 477 | |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 478 | /* queue up packets, each of max MCTP_MTU size */ |
Jeremy Kerr | c855d7b | 2019-08-01 21:18:09 +0800 | [diff] [blame] | 479 | for (p = 0, i = 0; p < msg_len; i++) { |
Jeremy Kerr | df15f7e | 2019-08-05 15:41:19 +0800 | [diff] [blame] | 480 | payload_len = msg_len - p; |
| 481 | if (payload_len > max_payload_len) |
| 482 | payload_len = max_payload_len; |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 483 | |
Jeremy Kerr | 1a4ec3c | 2019-09-03 11:01:50 +0800 | [diff] [blame] | 484 | pkt = mctp_pktbuf_alloc(bus->binding, |
| 485 | payload_len + sizeof(*hdr)); |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 486 | hdr = mctp_pktbuf_hdr(pkt); |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 487 | |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 488 | /* todo: tags */ |
| 489 | hdr->ver = bus->binding->version & 0xf; |
Jeremy Kerr | 1a4ec3c | 2019-09-03 11:01:50 +0800 | [diff] [blame] | 490 | hdr->dest = dest; |
| 491 | hdr->src = src; |
Jeremy Kerr | c855d7b | 2019-08-01 21:18:09 +0800 | [diff] [blame] | 492 | hdr->flags_seq_tag = MCTP_HDR_FLAG_TO | |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 493 | (0 << MCTP_HDR_TAG_SHIFT); |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 494 | |
Jeremy Kerr | c855d7b | 2019-08-01 21:18:09 +0800 | [diff] [blame] | 495 | if (i == 0) |
| 496 | hdr->flags_seq_tag |= MCTP_HDR_FLAG_SOM; |
Jeremy Kerr | df15f7e | 2019-08-05 15:41:19 +0800 | [diff] [blame] | 497 | if (p + payload_len >= msg_len) |
Jeremy Kerr | c855d7b | 2019-08-01 21:18:09 +0800 | [diff] [blame] | 498 | hdr->flags_seq_tag |= MCTP_HDR_FLAG_EOM; |
| 499 | hdr->flags_seq_tag |= |
| 500 | (i & MCTP_HDR_SEQ_MASK) << MCTP_HDR_SEQ_SHIFT; |
| 501 | |
Jeremy Kerr | df15f7e | 2019-08-05 15:41:19 +0800 | [diff] [blame] | 502 | memcpy(mctp_pktbuf_data(pkt), msg + p, payload_len); |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 503 | |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 504 | /* add to tx queue */ |
Jeremy Kerr | cc2458d | 2019-03-01 08:23:33 +0800 | [diff] [blame] | 505 | if (bus->tx_queue_tail) |
| 506 | bus->tx_queue_tail->next = pkt; |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 507 | else |
Jeremy Kerr | cc2458d | 2019-03-01 08:23:33 +0800 | [diff] [blame] | 508 | bus->tx_queue_head = pkt; |
| 509 | bus->tx_queue_tail = pkt; |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 510 | |
Jeremy Kerr | df15f7e | 2019-08-05 15:41:19 +0800 | [diff] [blame] | 511 | p += payload_len; |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 512 | } |
| 513 | |
Andrew Jeffery | 298865f | 2020-02-06 11:51:29 +1030 | [diff] [blame] | 514 | mctp_prdebug("%s: Enqueued %d packets", __func__, i); |
| 515 | |
Jeremy Kerr | cc2458d | 2019-03-01 08:23:33 +0800 | [diff] [blame] | 516 | mctp_send_tx_queue(bus); |
Jeremy Kerr | 24db71f | 2019-02-07 21:37:35 +0800 | [diff] [blame] | 517 | |
| 518 | return 0; |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 519 | } |
Jeremy Kerr | 1a4ec3c | 2019-09-03 11:01:50 +0800 | [diff] [blame] | 520 | |
| 521 | int mctp_message_tx(struct mctp *mctp, mctp_eid_t eid, |
| 522 | void *msg, size_t msg_len) |
| 523 | { |
| 524 | struct mctp_bus *bus; |
| 525 | |
| 526 | bus = find_bus_for_eid(mctp, eid); |
| 527 | return mctp_message_tx_on_bus(mctp, bus, bus->eid, eid, msg, msg_len); |
| 528 | } |