blob: c5ded9dec1fb4c0b19e70cb790d2a6f2321b7d3e [file] [log] [blame]
Jeremy Kerr3d36ee22019-05-30 11:15:37 +08001/* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */
Jeremy Kerr4cdc2002019-02-07 16:49:12 +08002
3#include <assert.h>
Andrew Jeffery3e8a12a2020-06-05 16:08:30 +09304#include <errno.h>
Jeremy Kerr4cdc2002019-02-07 16:49:12 +08005#include <stdarg.h>
6#include <stddef.h>
7#include <stdint.h>
8#include <stdio.h>
9#include <stdlib.h>
10#include <string.h>
Matt Johnstona3830d22025-01-13 14:22:11 +080011#include <stdalign.h>
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080012
13#undef pr_fmt
14#define pr_fmt(fmt) "core: " fmt
15
16#include "libmctp.h"
17#include "libmctp-alloc.h"
18#include "libmctp-log.h"
Wiktor GoĊ‚gowskiba6727e2020-03-13 18:25:01 +010019#include "libmctp-cmds.h"
Andrew Jefferyc2b833e2020-10-28 14:28:37 +103020#include "range.h"
Matt Johnston4a09e1d2024-09-13 14:55:58 +080021#include "compiler.h"
Matt Johnstonf9b99f12024-09-17 16:48:34 +080022#include "core-internal.h"
Matt Johnston4058b2c2024-11-07 14:53:50 +080023#include "control.h"
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080024
Matt Johnston44e64df2024-11-05 16:59:42 +080025#if MCTP_DEFAULT_CLOCK_GETTIME
26#include <time.h>
27#endif
28
Jeremy Kerr24db71f2019-02-07 21:37:35 +080029#ifndef ARRAY_SIZE
30#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
31#endif
32
Andrew Jefferyb93b6112020-06-05 14:13:44 +093033static int mctp_message_tx_on_bus(struct mctp_bus *bus, mctp_eid_t src,
Sumanth Bhatf39c3852022-01-10 17:04:10 +053034 mctp_eid_t dest, bool tag_owner,
35 uint8_t msg_tag, void *msg, size_t msg_len);
Matt Johnston61c95992024-09-16 16:50:35 +080036static void mctp_dealloc_tag(struct mctp_bus *bus, mctp_eid_t local,
37 mctp_eid_t remote, uint8_t tag);
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +080038
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +080039struct mctp_pktbuf *mctp_pktbuf_alloc(struct mctp_binding *binding, size_t len)
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080040{
Matt Johnston4a09e1d2024-09-13 14:55:58 +080041 size_t size =
42 binding->pkt_size + binding->pkt_header + binding->pkt_trailer;
Rashmica Gupta487b31e2022-09-14 18:49:45 +100043 if (len > size) {
44 return NULL;
45 }
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080046
Matt Johnston4a09e1d2024-09-13 14:55:58 +080047 void *storage = __mctp_alloc(size + sizeof(struct mctp_pktbuf));
48 if (!storage) {
Pedro Martelletto2608b292023-03-30 13:28:28 +000049 return NULL;
Matt Johnston4a09e1d2024-09-13 14:55:58 +080050 }
51 struct mctp_pktbuf *pkt = mctp_pktbuf_init(binding, storage);
52 pkt->alloc = true;
53 pkt->end = pkt->start + len;
54 return pkt;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080055}
56
57void mctp_pktbuf_free(struct mctp_pktbuf *pkt)
58{
Matt Johnston4a09e1d2024-09-13 14:55:58 +080059 if (pkt->alloc) {
60 __mctp_free(pkt);
61 } else {
62 mctp_prdebug("pktbuf_free called for non-alloced");
63 }
64}
65
66struct mctp_pktbuf *mctp_pktbuf_init(struct mctp_binding *binding,
67 void *storage)
68{
Matt Johnstona3830d22025-01-13 14:22:11 +080069 assert((size_t)storage % alignof(struct mctp_pktbuf) == 0);
70
Matt Johnston4a09e1d2024-09-13 14:55:58 +080071 size_t size =
72 binding->pkt_size + binding->pkt_header + binding->pkt_trailer;
73 struct mctp_pktbuf *buf = (struct mctp_pktbuf *)storage;
74 buf->size = size;
75 buf->start = binding->pkt_header;
76 buf->end = buf->start;
77 buf->mctp_hdr_off = buf->start;
78 buf->alloc = false;
79
80 return buf;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080081}
82
83struct mctp_hdr *mctp_pktbuf_hdr(struct mctp_pktbuf *pkt)
84{
Moritz Fischer7aaccb52022-06-28 20:04:04 -070085 return (struct mctp_hdr *)(pkt->data + pkt->mctp_hdr_off);
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080086}
87
88void *mctp_pktbuf_data(struct mctp_pktbuf *pkt)
89{
Moritz Fischer7aaccb52022-06-28 20:04:04 -070090 return pkt->data + pkt->mctp_hdr_off + sizeof(struct mctp_hdr);
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080091}
92
Matt Johnston4a09e1d2024-09-13 14:55:58 +080093size_t mctp_pktbuf_size(const struct mctp_pktbuf *pkt)
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080094{
95 return pkt->end - pkt->start;
96}
97
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +080098void *mctp_pktbuf_alloc_start(struct mctp_pktbuf *pkt, size_t size)
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080099{
100 assert(size <= pkt->start);
101 pkt->start -= size;
102 return pkt->data + pkt->start;
103}
104
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +0800105void *mctp_pktbuf_alloc_end(struct mctp_pktbuf *pkt, size_t size)
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800106{
107 void *buf;
108
Andrew Jeffery3ac70d62020-07-01 00:50:44 +0930109 assert(size <= (pkt->size - pkt->end));
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800110 buf = pkt->data + pkt->end;
111 pkt->end += size;
112 return buf;
113}
114
Matt Johnstondfbf0fd2024-10-28 14:40:29 +0800115int mctp_pktbuf_push(struct mctp_pktbuf *pkt, const void *data, size_t len)
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800116{
117 void *p;
118
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +0800119 if (pkt->end + len > pkt->size)
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800120 return -1;
121
122 p = pkt->data + pkt->end;
123
124 pkt->end += len;
125 memcpy(p, data, len);
126
127 return 0;
128}
129
Andrew Jefferyeba19a32021-03-09 23:09:40 +1030130void *mctp_pktbuf_pop(struct mctp_pktbuf *pkt, size_t len)
131{
132 if (len > mctp_pktbuf_size(pkt))
133 return NULL;
134
135 pkt->end -= len;
136 return pkt->data + pkt->end;
137}
138
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800139/* Allocate a duplicate of the message and copy it */
140static void *mctp_msg_dup(const void *msg, size_t msg_len, struct mctp *mctp)
141{
142 void *copy = __mctp_msg_alloc(msg_len, mctp);
143 if (!copy) {
144 mctp_prdebug("msg dup len %zu failed", msg_len);
145 return NULL;
146 }
147
148 memcpy(copy, msg, msg_len);
149 return copy;
150}
151
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800152/* Message reassembly */
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600153static struct mctp_msg_ctx *mctp_msg_ctx_lookup(struct mctp *mctp, uint8_t src,
154 uint8_t dest, uint8_t tag)
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800155{
156 unsigned int i;
157
158 /* @todo: better lookup, if we add support for more outstanding
159 * message contexts */
160 for (i = 0; i < ARRAY_SIZE(mctp->msg_ctxs); i++) {
161 struct mctp_msg_ctx *ctx = &mctp->msg_ctxs[i];
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800162 if (ctx->buf && ctx->src == src && ctx->dest == dest &&
163 ctx->tag == tag)
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800164 return ctx;
165 }
166
167 return NULL;
168}
169
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600170static struct mctp_msg_ctx *mctp_msg_ctx_create(struct mctp *mctp, uint8_t src,
171 uint8_t dest, uint8_t tag)
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800172{
Jeremy Kerr11a234e2019-02-27 17:59:53 +0800173 struct mctp_msg_ctx *ctx = NULL;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800174 unsigned int i;
175
176 for (i = 0; i < ARRAY_SIZE(mctp->msg_ctxs); i++) {
177 struct mctp_msg_ctx *tmp = &mctp->msg_ctxs[i];
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800178 if (!tmp->buf) {
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800179 ctx = tmp;
180 break;
181 }
182 }
183
184 if (!ctx)
185 return NULL;
186
187 ctx->src = src;
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800188 ctx->dest = dest;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800189 ctx->tag = tag;
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800190
Jeremy Kerr9a3da812019-08-02 15:57:53 +0800191 ctx->buf_size = 0;
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800192 ctx->buf_alloc_size = mctp->max_message_size;
193 ctx->buf = __mctp_msg_alloc(ctx->buf_alloc_size, mctp);
194 if (!ctx->buf) {
195 return NULL;
196 }
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800197
198 return ctx;
199}
200
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800201static void mctp_msg_ctx_drop(struct mctp_bus *bus, struct mctp_msg_ctx *ctx)
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800202{
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800203 /* Free and mark as unused */
204 __mctp_msg_free(ctx->buf, bus->mctp);
205 ctx->buf = NULL;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800206}
207
208static void mctp_msg_ctx_reset(struct mctp_msg_ctx *ctx)
209{
210 ctx->buf_size = 0;
Sumanth Bhat69f545f2021-05-18 09:16:43 +0000211 ctx->fragment_size = 0;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800212}
213
214static int mctp_msg_ctx_add_pkt(struct mctp_msg_ctx *ctx,
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800215 struct mctp_pktbuf *pkt)
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800216{
217 size_t len;
218
219 len = mctp_pktbuf_size(pkt) - sizeof(struct mctp_hdr);
220
Sumanth Bhatbc79c242021-06-16 12:36:56 +0530221 if (len + ctx->buf_size < ctx->buf_size) {
222 return -1;
223 }
224
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800225 if (ctx->buf_size + len > ctx->buf_alloc_size) {
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800226 return -1;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800227 }
228
Moritz Fischer7aaccb52022-06-28 20:04:04 -0700229 memcpy((uint8_t *)ctx->buf + ctx->buf_size, mctp_pktbuf_data(pkt), len);
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800230 ctx->buf_size += len;
231
232 return 0;
233}
234
235/* Core API functions */
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800236struct mctp *mctp_init(void)
237{
238 struct mctp *mctp;
239
240 mctp = __mctp_alloc(sizeof(*mctp));
Sumanth Bhat96d54492020-07-14 17:10:04 +0530241
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600242 if (!mctp)
Sumanth Bhat96d54492020-07-14 17:10:04 +0530243 return NULL;
244
Matt Johnstonf9b99f12024-09-17 16:48:34 +0800245 mctp_setup(mctp, sizeof(*mctp));
Matt Johnston722d0db2024-09-13 15:51:30 +0800246 return mctp;
247}
248
Matt Johnston44e64df2024-11-05 16:59:42 +0800249#if MCTP_DEFAULT_CLOCK_GETTIME
250static uint64_t mctp_default_now(void *ctx __attribute__((unused)))
251{
252 struct timespec tp;
253 int rc = clock_gettime(CLOCK_MONOTONIC, &tp);
254 if (rc) {
255 /* Should not be possible */
256 return 0;
257 }
258 return (uint64_t)tp.tv_sec * 1000 + tp.tv_nsec / 1000000;
259}
260#endif
261
Matt Johnstonf9b99f12024-09-17 16:48:34 +0800262int mctp_setup(struct mctp *mctp, size_t struct_mctp_size)
Matt Johnston722d0db2024-09-13 15:51:30 +0800263{
Matt Johnstonf9b99f12024-09-17 16:48:34 +0800264 if (struct_mctp_size < sizeof(struct mctp)) {
265 mctp_prdebug("Mismatching struct mctp");
266 return -EINVAL;
267 }
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800268 memset(mctp, 0, sizeof(*mctp));
Sumanth Bhat2c820c52020-07-02 00:26:25 +0530269 mctp->max_message_size = MCTP_MAX_MESSAGE_SIZE;
Matt Johnston44e64df2024-11-05 16:59:42 +0800270#if MCTP_DEFAULT_CLOCK_GETTIME
271 mctp->platform_now = mctp_default_now;
272#endif
Matt Johnston4058b2c2024-11-07 14:53:50 +0800273#if MCTP_CONTROL_HANDLER
274 mctp_control_add_type(mctp, MCTP_CTRL_HDR_MSG_TYPE);
275#endif
Matt Johnstonf9b99f12024-09-17 16:48:34 +0800276 return 0;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800277}
278
Sumanth Bhat2c820c52020-07-02 00:26:25 +0530279void mctp_set_max_message_size(struct mctp *mctp, size_t message_size)
280{
281 mctp->max_message_size = message_size;
282}
283
Andrew Jeffery5d3d4e62021-08-20 16:44:40 +0930284void mctp_set_capture_handler(struct mctp *mctp, mctp_capture_fn fn, void *user)
285{
286 mctp->capture = fn;
287 mctp->capture_data = user;
288}
289
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800290static void mctp_bus_destroy(struct mctp_bus *bus, struct mctp *mctp)
Andrew Jeffery3ae89dc2021-01-28 15:24:36 +1030291{
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800292 if (bus->tx_msg) {
293 __mctp_msg_free(bus->tx_msg, mctp);
294 bus->tx_msg = NULL;
Andrew Jeffery3ae89dc2021-01-28 15:24:36 +1030295 }
296}
297
Matt Johnston722d0db2024-09-13 15:51:30 +0800298void mctp_cleanup(struct mctp *mctp)
Andrew Jefferyfa56ca52020-03-10 23:18:22 +1030299{
Andrew Jefferyb93b6112020-06-05 14:13:44 +0930300 size_t i;
Andrew Jefferyfa56ca52020-03-10 23:18:22 +1030301
302 /* Cleanup message assembly contexts */
Matt Johnston3ef47782024-12-11 15:19:06 +0800303 static_assert(ARRAY_SIZE(mctp->msg_ctxs) < SIZE_MAX, "size");
Andrew Jefferyfa56ca52020-03-10 23:18:22 +1030304 for (i = 0; i < ARRAY_SIZE(mctp->msg_ctxs); i++) {
305 struct mctp_msg_ctx *tmp = &mctp->msg_ctxs[i];
306 if (tmp->buf)
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800307 __mctp_msg_free(tmp->buf, mctp);
Andrew Jefferyfa56ca52020-03-10 23:18:22 +1030308 }
309
Andrew Jeffery3ae89dc2021-01-28 15:24:36 +1030310 while (mctp->n_busses--)
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800311 mctp_bus_destroy(&mctp->busses[mctp->n_busses], mctp);
Matt Johnston722d0db2024-09-13 15:51:30 +0800312}
Andrew Jeffery3ae89dc2021-01-28 15:24:36 +1030313
Matt Johnston722d0db2024-09-13 15:51:30 +0800314void mctp_destroy(struct mctp *mctp)
315{
316 mctp_cleanup(mctp);
Andrew Jefferyfa56ca52020-03-10 23:18:22 +1030317 __mctp_free(mctp);
318}
319
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800320int mctp_set_rx_all(struct mctp *mctp, mctp_rx_fn fn, void *data)
321{
322 mctp->message_rx = fn;
323 mctp->message_rx_data = data;
324 return 0;
325}
326
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600327static struct mctp_bus *find_bus_for_eid(struct mctp *mctp, mctp_eid_t dest
328 __attribute__((unused)))
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800329{
Brad Bishop663ec392021-10-07 21:16:48 -0400330 if (mctp->n_busses == 0)
331 return NULL;
332
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800333 /* for now, just use the first bus. For full routing support,
334 * we will need a table of neighbours */
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800335 return &mctp->busses[0];
336}
337
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600338int mctp_register_bus(struct mctp *mctp, struct mctp_binding *binding,
339 mctp_eid_t eid)
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800340{
Andrew Jeffery3e8a12a2020-06-05 16:08:30 +0930341 int rc = 0;
342
Jeremy Kerr7520cec2019-03-01 07:13:18 +0800343 /* todo: multiple busses */
Matt Johnston722d0db2024-09-13 15:51:30 +0800344 static_assert(MCTP_MAX_BUSSES >= 1, "need a bus");
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800345 assert(mctp->n_busses == 0);
346 mctp->n_busses = 1;
Andrew Jeffery3e8a12a2020-06-05 16:08:30 +0930347
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800348 assert(binding->tx_storage);
349
James Feist62d72362019-12-13 13:43:32 -0800350 memset(mctp->busses, 0, sizeof(struct mctp_bus));
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800351 mctp->busses[0].mctp = mctp;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800352 mctp->busses[0].binding = binding;
353 mctp->busses[0].eid = eid;
Jeremy Kerr7520cec2019-03-01 07:13:18 +0800354 binding->bus = &mctp->busses[0];
Jeremy Kerr0a00dca2019-03-01 08:01:35 +0800355 binding->mctp = mctp;
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800356 mctp->route_policy = ROUTE_ENDPOINT;
Jeremy Kerr3b36d172019-09-04 11:56:09 +0800357
Andrew Jeffery3e8a12a2020-06-05 16:08:30 +0930358 if (binding->start) {
359 rc = binding->start(binding);
360 if (rc < 0) {
361 mctp_prerr("Failed to start binding: %d", rc);
Andrew Jeffery19275232021-01-29 14:13:25 +1030362 binding->bus = NULL;
Andrew Jeffery2304c832021-01-29 11:52:49 +1030363 mctp->n_busses = 0;
Andrew Jeffery3e8a12a2020-06-05 16:08:30 +0930364 }
365 }
Jeremy Kerr3b36d172019-09-04 11:56:09 +0800366
Andrew Jeffery3e8a12a2020-06-05 16:08:30 +0930367 return rc;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800368}
369
Matt Johnston4058b2c2024-11-07 14:53:50 +0800370int mctp_bus_set_eid(struct mctp_binding *binding, mctp_eid_t eid)
371{
372 if (eid < 8 || eid == 0xff) {
373 return -EINVAL;
374 }
375
376 binding->bus->eid = eid;
377 return 0;
378}
379
Andrew Jeffery2094c3c2021-08-26 12:32:46 +0930380void mctp_unregister_bus(struct mctp *mctp, struct mctp_binding *binding)
381{
382 /*
383 * We only support one bus right now; once the call completes we will
384 * have no more busses
385 */
386 mctp->n_busses = 0;
387 binding->mctp = NULL;
388 binding->bus = NULL;
Andrew Jeffery2094c3c2021-08-26 12:32:46 +0930389}
390
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600391int mctp_bridge_busses(struct mctp *mctp, struct mctp_binding *b1,
392 struct mctp_binding *b2)
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800393{
Andrew Jeffery19275232021-01-29 14:13:25 +1030394 int rc = 0;
395
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800396 assert(b1->tx_storage);
397 assert(b2->tx_storage);
398
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800399 assert(mctp->n_busses == 0);
Matt Johnston722d0db2024-09-13 15:51:30 +0800400 assert(MCTP_MAX_BUSSES >= 2);
James Feist62d72362019-12-13 13:43:32 -0800401 memset(mctp->busses, 0, 2 * sizeof(struct mctp_bus));
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800402 mctp->n_busses = 2;
403 mctp->busses[0].binding = b1;
404 b1->bus = &mctp->busses[0];
405 b1->mctp = mctp;
406 mctp->busses[1].binding = b2;
407 b2->bus = &mctp->busses[1];
408 b2->mctp = mctp;
409
410 mctp->route_policy = ROUTE_BRIDGE;
Jeremy Kerr3b36d172019-09-04 11:56:09 +0800411
Andrew Jeffery19275232021-01-29 14:13:25 +1030412 if (b1->start) {
413 rc = b1->start(b1);
414 if (rc < 0) {
415 mctp_prerr("Failed to start bridged bus %s: %d",
416 b1->name, rc);
417 goto done;
418 }
419 }
Jeremy Kerr3b36d172019-09-04 11:56:09 +0800420
Andrew Jeffery19275232021-01-29 14:13:25 +1030421 if (b2->start) {
422 rc = b2->start(b2);
423 if (rc < 0) {
424 mctp_prerr("Failed to start bridged bus %s: %d",
425 b2->name, rc);
426 goto done;
427 }
428 }
Jeremy Kerr3b36d172019-09-04 11:56:09 +0800429
Andrew Jeffery19275232021-01-29 14:13:25 +1030430done:
431 return rc;
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800432}
433
Wiktor GoĊ‚gowskiba6727e2020-03-13 18:25:01 +0100434static inline bool mctp_ctrl_cmd_is_transport(struct mctp_ctrl_msg_hdr *hdr)
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800435{
Matt Johnston3ef47782024-12-11 15:19:06 +0800436#pragma GCC diagnostic push
437#pragma GCC diagnostic ignored "-Wtype-limits"
Wiktor GoĊ‚gowskiba6727e2020-03-13 18:25:01 +0100438 return ((hdr->command_code >= MCTP_CTRL_CMD_FIRST_TRANSPORT) &&
439 (hdr->command_code <= MCTP_CTRL_CMD_LAST_TRANSPORT));
Matt Johnston3ef47782024-12-11 15:19:06 +0800440#pragma GCC diagnostic pop
Wiktor GoĊ‚gowskiba6727e2020-03-13 18:25:01 +0100441}
442
Andrew Jefferyb93b6112020-06-05 14:13:44 +0930443static bool mctp_ctrl_handle_msg(struct mctp_bus *bus, mctp_eid_t src,
Sumanth Bhatf39c3852022-01-10 17:04:10 +0530444 uint8_t msg_tag, bool tag_owner, void *buffer,
445 size_t length)
Wiktor GoĊ‚gowskiba6727e2020-03-13 18:25:01 +0100446{
447 struct mctp_ctrl_msg_hdr *msg_hdr = buffer;
448
449 /*
450 * Control message is received. If a transport control message handler
451 * is provided, it will called. If there is no dedicated handler, this
452 * function returns false and data can be handled by the generic
453 * message handler. The transport control message handler will be
454 * provided with messages in the command range 0xF0 - 0xFF.
455 */
456 if (mctp_ctrl_cmd_is_transport(msg_hdr)) {
457 if (bus->binding->control_rx != NULL) {
458 /* MCTP bus binding handler */
Sumanth Bhatf39c3852022-01-10 17:04:10 +0530459 bus->binding->control_rx(src, msg_tag, tag_owner,
Wiktor GoĊ‚gowskiba6727e2020-03-13 18:25:01 +0100460 bus->binding->control_rx_data,
461 buffer, length);
462 return true;
463 }
Matt Johnston4058b2c2024-11-07 14:53:50 +0800464 } else {
465#if MCTP_CONTROL_HANDLER
466 /* libmctp will handle control requests */
467 return mctp_control_handler(bus, src, tag_owner, msg_tag,
468 buffer, length);
469#endif
Wiktor GoĊ‚gowskiba6727e2020-03-13 18:25:01 +0100470 }
471
472 /*
473 * Command was not handled, due to lack of specific callback.
474 * It will be passed to regular message_rx handler.
475 */
476 return false;
477}
478
479static inline bool mctp_rx_dest_is_local(struct mctp_bus *bus, mctp_eid_t dest)
480{
481 return dest == bus->eid || dest == MCTP_EID_NULL ||
482 dest == MCTP_EID_BROADCAST;
483}
484
485static inline bool mctp_ctrl_cmd_is_request(struct mctp_ctrl_msg_hdr *hdr)
486{
487 return hdr->ic_msg_type == MCTP_CTRL_HDR_MSG_TYPE &&
488 hdr->rq_dgram_inst & MCTP_CTRL_HDR_FLAG_REQUEST;
489}
490
491/*
492 * Receive the complete MCTP message and route it.
493 * Asserts:
494 * 'buf' is not NULL.
495 */
496static void mctp_rx(struct mctp *mctp, struct mctp_bus *bus, mctp_eid_t src,
Sumanth Bhatf39c3852022-01-10 17:04:10 +0530497 mctp_eid_t dest, bool tag_owner, uint8_t msg_tag, void *buf,
498 size_t len)
Wiktor GoĊ‚gowskiba6727e2020-03-13 18:25:01 +0100499{
500 assert(buf != NULL);
501
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800502 if (mctp->route_policy == ROUTE_ENDPOINT &&
Wiktor GoĊ‚gowskiba6727e2020-03-13 18:25:01 +0100503 mctp_rx_dest_is_local(bus, dest)) {
Matt Johnston61c95992024-09-16 16:50:35 +0800504 /* Note responses to allocated tags */
505 if (!tag_owner) {
506 mctp_dealloc_tag(bus, dest, src, msg_tag);
507 }
508
Wiktor GoĊ‚gowskiba6727e2020-03-13 18:25:01 +0100509 /* Handle MCTP Control Messages: */
510 if (len >= sizeof(struct mctp_ctrl_msg_hdr)) {
511 struct mctp_ctrl_msg_hdr *msg_hdr = buf;
512
513 /*
514 * Identify if this is a control request message.
515 * See DSP0236 v1.3.0 sec. 11.5.
516 */
517 if (mctp_ctrl_cmd_is_request(msg_hdr)) {
518 bool handled;
Sumanth Bhatf39c3852022-01-10 17:04:10 +0530519 handled = mctp_ctrl_handle_msg(
520 bus, src, msg_tag, tag_owner, buf, len);
Wiktor GoĊ‚gowskiba6727e2020-03-13 18:25:01 +0100521 if (handled)
522 return;
523 }
524 }
Sumanth Bhatf39c3852022-01-10 17:04:10 +0530525
Wiktor GoĊ‚gowskiba6727e2020-03-13 18:25:01 +0100526 if (mctp->message_rx)
Sumanth Bhatf39c3852022-01-10 17:04:10 +0530527 mctp->message_rx(src, tag_owner, msg_tag,
528 mctp->message_rx_data, buf, len);
Wiktor GoĊ‚gowskiba6727e2020-03-13 18:25:01 +0100529 }
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800530
531 if (mctp->route_policy == ROUTE_BRIDGE) {
532 int i;
533
534 for (i = 0; i < mctp->n_busses; i++) {
535 struct mctp_bus *dest_bus = &mctp->busses[i];
536 if (dest_bus == bus)
537 continue;
538
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800539 void *copy = mctp_msg_dup(buf, len, mctp);
540 if (!copy) {
541 return;
542 }
543
Sumanth Bhatf39c3852022-01-10 17:04:10 +0530544 mctp_message_tx_on_bus(dest_bus, src, dest, tag_owner,
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800545 msg_tag, copy, len);
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800546 }
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800547 }
548}
549
Jeremy Kerr0a00dca2019-03-01 08:01:35 +0800550void mctp_bus_rx(struct mctp_binding *binding, struct mctp_pktbuf *pkt)
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800551{
Jeremy Kerr7520cec2019-03-01 07:13:18 +0800552 struct mctp_bus *bus = binding->bus;
Jeremy Kerr0a00dca2019-03-01 08:01:35 +0800553 struct mctp *mctp = binding->mctp;
Ed Tanousc2def9f2019-02-21 08:33:08 -0800554 uint8_t flags, exp_seq, seq, tag;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800555 struct mctp_msg_ctx *ctx;
556 struct mctp_hdr *hdr;
Sumanth Bhatf39c3852022-01-10 17:04:10 +0530557 bool tag_owner;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800558 size_t len;
559 void *p;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800560 int rc;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800561
Jeremy Kerr7520cec2019-03-01 07:13:18 +0800562 assert(bus);
563
Sumanth Bhatd97869d2020-07-02 00:46:13 +0530564 /* Drop packet if it was smaller than mctp hdr size */
Matt Johnston86e9a972024-10-28 15:06:33 +0800565 if (mctp_pktbuf_size(pkt) < sizeof(struct mctp_hdr))
Sumanth Bhatd97869d2020-07-02 00:46:13 +0530566 goto out;
567
Andrew Jeffery5d3d4e62021-08-20 16:44:40 +0930568 if (mctp->capture)
Rashmica Guptaf2988972022-11-09 12:26:44 +1100569 mctp->capture(pkt, MCTP_MESSAGE_CAPTURE_INCOMING,
570 mctp->capture_data);
Andrew Jeffery5d3d4e62021-08-20 16:44:40 +0930571
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800572 hdr = mctp_pktbuf_hdr(pkt);
573
Matt Johnston18b9a372024-11-22 15:41:01 +0800574 if (hdr->src == MCTP_EID_BROADCAST) {
575 /* drop packets with broadcast EID src */
576 goto out;
577 }
578
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800579 /* small optimisation: don't bother reassembly if we're going to
580 * drop the packet in mctp_rx anyway */
John Chung133df7a2024-05-14 16:19:56 +0800581 if (mctp->route_policy == ROUTE_ENDPOINT &&
582 !mctp_rx_dest_is_local(bus, hdr->dest))
Jeremy Kerrc1693af2019-08-05 14:30:59 +0800583 goto out;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800584
585 flags = hdr->flags_seq_tag & (MCTP_HDR_FLAG_SOM | MCTP_HDR_FLAG_EOM);
586 tag = (hdr->flags_seq_tag >> MCTP_HDR_TAG_SHIFT) & MCTP_HDR_TAG_MASK;
587 seq = (hdr->flags_seq_tag >> MCTP_HDR_SEQ_SHIFT) & MCTP_HDR_SEQ_MASK;
Andrew Jeffery7f7fdc12023-05-12 15:56:47 +0930588 tag_owner = (hdr->flags_seq_tag >> MCTP_HDR_TO_SHIFT) &
589 MCTP_HDR_TO_MASK;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800590
591 switch (flags) {
592 case MCTP_HDR_FLAG_SOM | MCTP_HDR_FLAG_EOM:
593 /* single-packet message - send straight up to rx function,
594 * no need to create a message context */
595 len = pkt->end - pkt->mctp_hdr_off - sizeof(struct mctp_hdr);
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800596 p = mctp_msg_dup(pkt->data + pkt->mctp_hdr_off +
597 sizeof(struct mctp_hdr),
598 len, mctp);
599 if (p) {
600 mctp_rx(mctp, bus, hdr->src, hdr->dest, tag_owner, tag,
601 p, len);
602 __mctp_msg_free(p, mctp);
603 }
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800604 break;
605
606 case MCTP_HDR_FLAG_SOM:
607 /* start of a new message - start the new context for
608 * future message reception. If an existing context is
609 * already present, drop it. */
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800610 ctx = mctp_msg_ctx_lookup(mctp, hdr->src, hdr->dest, tag);
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800611 if (ctx) {
612 mctp_msg_ctx_reset(ctx);
613 } else {
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600614 ctx = mctp_msg_ctx_create(mctp, hdr->src, hdr->dest,
615 tag);
Sumanth Bhat34d4c962021-06-16 12:50:48 +0530616 /* If context creation fails due to exhaution of contexts we
617 * can support, drop the packet */
618 if (!ctx) {
619 mctp_prdebug("Context buffers exhausted.");
620 goto out;
621 }
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800622 }
623
Sumanth Bhat69f545f2021-05-18 09:16:43 +0000624 /* Save the fragment size, subsequent middle fragments
625 * should of the same size */
626 ctx->fragment_size = mctp_pktbuf_size(pkt);
627
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800628 rc = mctp_msg_ctx_add_pkt(ctx, pkt);
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800629 if (rc) {
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800630 mctp_msg_ctx_drop(bus, ctx);
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800631 } else {
632 ctx->last_seq = seq;
633 }
634
635 break;
636
637 case MCTP_HDR_FLAG_EOM:
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800638 ctx = mctp_msg_ctx_lookup(mctp, hdr->src, hdr->dest, tag);
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800639 if (!ctx)
Jeremy Kerrc1693af2019-08-05 14:30:59 +0800640 goto out;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800641
Ed Tanousc2def9f2019-02-21 08:33:08 -0800642 exp_seq = (ctx->last_seq + 1) % 4;
643
644 if (exp_seq != seq) {
645 mctp_prdebug(
646 "Sequence number %d does not match expected %d",
647 seq, exp_seq);
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800648 mctp_msg_ctx_drop(bus, ctx);
Jeremy Kerrc1693af2019-08-05 14:30:59 +0800649 goto out;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800650 }
651
Sumanth Bhat69f545f2021-05-18 09:16:43 +0000652 len = mctp_pktbuf_size(pkt);
653
654 if (len > ctx->fragment_size) {
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600655 mctp_prdebug("Unexpected fragment size. Expected"
656 " less than %zu, received = %zu",
657 ctx->fragment_size, len);
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800658 mctp_msg_ctx_drop(bus, ctx);
Sumanth Bhat69f545f2021-05-18 09:16:43 +0000659 goto out;
660 }
661
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800662 rc = mctp_msg_ctx_add_pkt(ctx, pkt);
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800663 if (!rc)
Sumanth Bhatf39c3852022-01-10 17:04:10 +0530664 mctp_rx(mctp, bus, ctx->src, ctx->dest, tag_owner, tag,
665 ctx->buf, ctx->buf_size);
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800666
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800667 mctp_msg_ctx_drop(bus, ctx);
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800668 break;
Ed Tanousc2def9f2019-02-21 08:33:08 -0800669
670 case 0:
671 /* Neither SOM nor EOM */
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600672 ctx = mctp_msg_ctx_lookup(mctp, hdr->src, hdr->dest, tag);
Ed Tanousc2def9f2019-02-21 08:33:08 -0800673 if (!ctx)
Jeremy Kerrc1693af2019-08-05 14:30:59 +0800674 goto out;
Ed Tanousc2def9f2019-02-21 08:33:08 -0800675
676 exp_seq = (ctx->last_seq + 1) % 4;
677 if (exp_seq != seq) {
678 mctp_prdebug(
679 "Sequence number %d does not match expected %d",
680 seq, exp_seq);
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800681 mctp_msg_ctx_drop(bus, ctx);
Jeremy Kerrc1693af2019-08-05 14:30:59 +0800682 goto out;
Ed Tanousc2def9f2019-02-21 08:33:08 -0800683 }
684
Sumanth Bhat69f545f2021-05-18 09:16:43 +0000685 len = mctp_pktbuf_size(pkt);
686
687 if (len != ctx->fragment_size) {
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600688 mctp_prdebug("Unexpected fragment size. Expected = %zu "
689 "received = %zu",
690 ctx->fragment_size, len);
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800691 mctp_msg_ctx_drop(bus, ctx);
Sumanth Bhat69f545f2021-05-18 09:16:43 +0000692 goto out;
693 }
694
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800695 rc = mctp_msg_ctx_add_pkt(ctx, pkt);
Ed Tanousc2def9f2019-02-21 08:33:08 -0800696 if (rc) {
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800697 mctp_msg_ctx_drop(bus, ctx);
Jeremy Kerrc1693af2019-08-05 14:30:59 +0800698 goto out;
Ed Tanousc2def9f2019-02-21 08:33:08 -0800699 }
700 ctx->last_seq = seq;
701
702 break;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800703 }
Jeremy Kerrc1693af2019-08-05 14:30:59 +0800704out:
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800705 return;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800706}
707
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600708static int mctp_packet_tx(struct mctp_bus *bus, struct mctp_pktbuf *pkt)
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800709{
Andrew Jeffery5d3d4e62021-08-20 16:44:40 +0930710 struct mctp *mctp = bus->binding->mctp;
711
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800712 if (bus->state != mctp_bus_state_tx_enabled) {
713 mctp_prdebug("tx with bus disabled");
Jeremy Kerr1cd31182019-02-27 18:01:00 +0800714 return -1;
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800715 }
Jeremy Kerr1cd31182019-02-27 18:01:00 +0800716
Andrew Jeffery5d3d4e62021-08-20 16:44:40 +0930717 if (mctp->capture)
Rashmica Guptaf2988972022-11-09 12:26:44 +1100718 mctp->capture(pkt, MCTP_MESSAGE_CAPTURE_OUTGOING,
719 mctp->capture_data);
Andrew Jeffery5d3d4e62021-08-20 16:44:40 +0930720
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800721 return bus->binding->tx(bus->binding, pkt);
722}
723
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800724/* Returns a pointer to the binding's tx_storage */
725static struct mctp_pktbuf *mctp_next_tx_pkt(struct mctp_bus *bus)
726{
727 if (!bus->tx_msg) {
728 return NULL;
729 }
730
731 size_t p = bus->tx_msgpos;
732 size_t msg_len = bus->tx_msglen;
733 size_t payload_len = msg_len - p;
734 size_t max_payload_len = MCTP_BODY_SIZE(bus->binding->pkt_size);
735 if (payload_len > max_payload_len)
736 payload_len = max_payload_len;
737
738 struct mctp_pktbuf *pkt =
739 mctp_pktbuf_init(bus->binding, bus->binding->tx_storage);
740 struct mctp_hdr *hdr = mctp_pktbuf_hdr(pkt);
741
742 hdr->ver = bus->binding->version & 0xf;
743 hdr->dest = bus->tx_dest;
744 hdr->src = bus->tx_src;
745 hdr->flags_seq_tag = (bus->tx_to << MCTP_HDR_TO_SHIFT) |
746 (bus->tx_tag << MCTP_HDR_TAG_SHIFT);
747
748 if (p == 0)
749 hdr->flags_seq_tag |= MCTP_HDR_FLAG_SOM;
750 if (p + payload_len >= msg_len)
751 hdr->flags_seq_tag |= MCTP_HDR_FLAG_EOM;
752 hdr->flags_seq_tag |= bus->tx_seq << MCTP_HDR_SEQ_SHIFT;
753
754 memcpy(mctp_pktbuf_data(pkt), (uint8_t *)bus->tx_msg + p, payload_len);
755 pkt->end = pkt->start + sizeof(*hdr) + payload_len;
756 bus->tx_pktlen = payload_len;
757
758 mctp_prdebug(
759 "tx dst %d tag %d payload len %zu seq %d. msg pos %zu len %zu",
760 hdr->dest, bus->tx_tag, payload_len, bus->tx_seq, p, msg_len);
761
762 return pkt;
763}
764
765/* Called when a packet has successfully been sent */
766static void mctp_tx_complete(struct mctp_bus *bus)
767{
768 if (!bus->tx_msg) {
769 mctp_prdebug("tx complete no message");
770 return;
771 }
772
773 bus->tx_seq = (bus->tx_seq + 1) & MCTP_HDR_SEQ_MASK;
774 bus->tx_msgpos += bus->tx_pktlen;
775
776 if (bus->tx_msgpos >= bus->tx_msglen) {
777 __mctp_msg_free(bus->tx_msg, bus->binding->mctp);
778 bus->tx_msg = NULL;
779 }
780}
781
Jeremy Kerrcc2458d2019-03-01 08:23:33 +0800782static void mctp_send_tx_queue(struct mctp_bus *bus)
Jeremy Kerr1cd31182019-02-27 18:01:00 +0800783{
784 struct mctp_pktbuf *pkt;
785
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800786 while (bus->tx_msg && bus->state == mctp_bus_state_tx_enabled) {
Jeremy Kerr1cd31182019-02-27 18:01:00 +0800787 int rc;
788
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800789 pkt = mctp_next_tx_pkt(bus);
790
Jeremy Kerr1cd31182019-02-27 18:01:00 +0800791 rc = mctp_packet_tx(bus, pkt);
Andrew Jeffery0721f582022-09-29 12:12:39 +0930792 switch (rc) {
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800793 /* If transmission succeded */
Andrew Jeffery0721f582022-09-29 12:12:39 +0930794 case 0:
Andrew Jeffery0721f582022-09-29 12:12:39 +0930795 /* Drop the packet */
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800796 mctp_tx_complete(bus);
Jeremy Kerr1cd31182019-02-27 18:01:00 +0800797 break;
798
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800799 /* If the binding was busy */
Andrew Jeffery0721f582022-09-29 12:12:39 +0930800 case -EBUSY:
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800801 /* Keep the packet for next try */
802 mctp_prdebug("tx EBUSY");
803 return;
804
Andrew Jeffery0721f582022-09-29 12:12:39 +0930805 /* Some other unknown error occurred */
806 default:
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800807 /* Drop the packet */
808 mctp_prdebug("tx drop %d", rc);
809 mctp_tx_complete(bus);
810 return;
Andrew Jeffery0721f582022-09-29 12:12:39 +0930811 };
Jeremy Kerr1cd31182019-02-27 18:01:00 +0800812 }
Jeremy Kerr1cd31182019-02-27 18:01:00 +0800813}
814
815void mctp_binding_set_tx_enabled(struct mctp_binding *binding, bool enable)
816{
817 struct mctp_bus *bus = binding->bus;
Andrew Jefferyc61501c2021-01-27 23:24:18 +1030818
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600819 switch (bus->state) {
Andrew Jefferyc61501c2021-01-27 23:24:18 +1030820 case mctp_bus_state_constructed:
821 if (!enable)
822 return;
823
Andrew Jeffery1fa707e2021-01-28 15:22:11 +1030824 if (binding->pkt_size < MCTP_PACKET_SIZE(MCTP_BTU)) {
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600825 mctp_prerr(
826 "Cannot start %s binding with invalid MTU: %zu",
827 binding->name,
828 MCTP_BODY_SIZE(binding->pkt_size));
Andrew Jeffery1fa707e2021-01-28 15:22:11 +1030829 return;
830 }
831
Andrew Jefferyc61501c2021-01-27 23:24:18 +1030832 bus->state = mctp_bus_state_tx_enabled;
833 mctp_prinfo("%s binding started", binding->name);
834 return;
835 case mctp_bus_state_tx_enabled:
836 if (enable)
837 return;
838
839 bus->state = mctp_bus_state_tx_disabled;
840 mctp_prdebug("%s binding Tx disabled", binding->name);
841 return;
842 case mctp_bus_state_tx_disabled:
843 if (!enable)
844 return;
845
846 bus->state = mctp_bus_state_tx_enabled;
847 mctp_prdebug("%s binding Tx enabled", binding->name);
Jeremy Kerrcc2458d2019-03-01 08:23:33 +0800848 mctp_send_tx_queue(bus);
Andrew Jefferyc61501c2021-01-27 23:24:18 +1030849 return;
850 }
Jeremy Kerr1cd31182019-02-27 18:01:00 +0800851}
852
Andrew Jefferyb93b6112020-06-05 14:13:44 +0930853static int mctp_message_tx_on_bus(struct mctp_bus *bus, mctp_eid_t src,
Sumanth Bhatf39c3852022-01-10 17:04:10 +0530854 mctp_eid_t dest, bool tag_owner,
855 uint8_t msg_tag, void *msg, size_t msg_len)
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800856{
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800857 size_t max_payload_len;
858 int rc;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800859
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800860 if (bus->state == mctp_bus_state_constructed) {
861 rc = -ENXIO;
862 goto err;
863 }
Andrew Jefferyc61501c2021-01-27 23:24:18 +1030864
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800865 if ((msg_tag & MCTP_HDR_TAG_MASK) != msg_tag) {
866 rc = -EINVAL;
867 goto err;
868 }
Sumanth Bhatf39c3852022-01-10 17:04:10 +0530869
Andrew Jeffery1fa707e2021-01-28 15:22:11 +1030870 max_payload_len = MCTP_BODY_SIZE(bus->binding->pkt_size);
871
872 {
873 const bool valid_mtu = max_payload_len >= MCTP_BTU;
874 assert(valid_mtu);
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800875 if (!valid_mtu) {
876 rc = -EINVAL;
877 goto err;
878 }
Andrew Jeffery1fa707e2021-01-28 15:22:11 +1030879 }
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800880
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600881 mctp_prdebug(
882 "%s: Generating packets for transmission of %zu byte message from %hhu to %hhu",
883 __func__, msg_len, src, dest);
Andrew Jeffery298865f2020-02-06 11:51:29 +1030884
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800885 if (bus->tx_msg) {
886 mctp_prdebug("Bus busy");
887 rc = -EBUSY;
888 goto err;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800889 }
890
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800891 /* Take the message to send */
892 bus->tx_msg = msg;
893 bus->tx_msglen = msg_len;
894 bus->tx_msgpos = 0;
895 /* bus->tx_seq is allowed to continue from previous message */
896 bus->tx_src = src;
897 bus->tx_dest = dest;
898 bus->tx_to = tag_owner;
899 bus->tx_tag = msg_tag;
Andrew Jeffery298865f2020-02-06 11:51:29 +1030900
Jeremy Kerrcc2458d2019-03-01 08:23:33 +0800901 mctp_send_tx_queue(bus);
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800902 return 0;
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800903
904err:
905 __mctp_msg_free(msg, bus->binding->mctp);
906 return rc;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800907}
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800908
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800909int mctp_message_tx_alloced(struct mctp *mctp, mctp_eid_t eid, bool tag_owner,
910 uint8_t msg_tag, void *msg, size_t msg_len)
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800911{
912 struct mctp_bus *bus;
913
Sumanth Bhatf39c3852022-01-10 17:04:10 +0530914 /* TODO: Protect against same tag being used across
915 * different callers */
916 if ((msg_tag & MCTP_HDR_TAG_MASK) != msg_tag) {
917 mctp_prerr("Incorrect message tag %u passed.", msg_tag);
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800918 __mctp_msg_free(msg, mctp);
Sumanth Bhatf39c3852022-01-10 17:04:10 +0530919 return -EINVAL;
920 }
921
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800922 bus = find_bus_for_eid(mctp, eid);
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800923 if (!bus) {
924 __mctp_msg_free(msg, mctp);
Brad Bishop663ec392021-10-07 21:16:48 -0400925 return 0;
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800926 }
Brad Bishop663ec392021-10-07 21:16:48 -0400927
Sumanth Bhatf39c3852022-01-10 17:04:10 +0530928 return mctp_message_tx_on_bus(bus, bus->eid, eid, tag_owner, msg_tag,
929 msg, msg_len);
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800930}
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800931
932int mctp_message_tx(struct mctp *mctp, mctp_eid_t eid, bool tag_owner,
933 uint8_t msg_tag, const void *msg, size_t msg_len)
934{
935 void *copy = mctp_msg_dup(msg, msg_len, mctp);
936 if (!copy) {
937 return -ENOMEM;
938 }
939
940 return mctp_message_tx_alloced(mctp, eid, tag_owner, msg_tag, copy,
941 msg_len);
942}
943
Matt Johnston44e64df2024-11-05 16:59:42 +0800944void mctp_set_now_op(struct mctp *mctp, uint64_t (*now)(void *), void *ctx)
945{
946 assert(now);
947 mctp->platform_now = now;
948 mctp->platform_now_ctx = ctx;
949}
950
951uint64_t mctp_now(struct mctp *mctp)
952{
953 assert(mctp->platform_now);
954 return mctp->platform_now(mctp->platform_now_ctx);
955}
956
Matt Johnston61c95992024-09-16 16:50:35 +0800957static void mctp_dealloc_tag(struct mctp_bus *bus, mctp_eid_t local,
958 mctp_eid_t remote, uint8_t tag)
959{
960 struct mctp *mctp = bus->binding->mctp;
Matt Johnston44e64df2024-11-05 16:59:42 +0800961 if (local == 0) {
Matt Johnston61c95992024-09-16 16:50:35 +0800962 return;
963 }
964
965 for (size_t i = 0; i < ARRAY_SIZE(mctp->req_tags); i++) {
966 struct mctp_req_tag *r = &mctp->req_tags[i];
967 if (r->local == local && r->remote == remote && r->tag == tag) {
968 r->local = 0;
969 r->remote = 0;
970 r->tag = 0;
Matt Johnston44e64df2024-11-05 16:59:42 +0800971 r->expiry = 0;
Matt Johnston61c95992024-09-16 16:50:35 +0800972 return;
973 }
974 }
975}
976
977static int mctp_alloc_tag(struct mctp *mctp, mctp_eid_t local,
978 mctp_eid_t remote, uint8_t *ret_tag)
979{
980 assert(local != 0);
Matt Johnston44e64df2024-11-05 16:59:42 +0800981 uint64_t now = mctp_now(mctp);
Matt Johnston61c95992024-09-16 16:50:35 +0800982
983 uint8_t used = 0;
984 struct mctp_req_tag *spare = NULL;
985 /* Find which tags and slots are used/spare */
986 for (size_t i = 0; i < ARRAY_SIZE(mctp->req_tags); i++) {
987 struct mctp_req_tag *r = &mctp->req_tags[i];
Matt Johnston44e64df2024-11-05 16:59:42 +0800988 if (r->local == 0 || r->expiry < now) {
Matt Johnston61c95992024-09-16 16:50:35 +0800989 spare = r;
990 } else {
Matt Johnston61c95992024-09-16 16:50:35 +0800991 if (r->local == local && r->remote == remote) {
992 used |= 1 << r->tag;
993 }
994 }
995 }
996
997 if (spare == NULL) {
998 // All req_tag slots are in-use
999 return -EBUSY;
1000 }
1001
1002 for (uint8_t t = 0; t < 8; t++) {
1003 uint8_t tag = (t + mctp->tag_round_robin) % 8;
1004 if ((used & 1 << tag) == 0) {
1005 spare->local = local;
1006 spare->remote = remote;
1007 spare->tag = tag;
Matt Johnston44e64df2024-11-05 16:59:42 +08001008 spare->expiry = now + MCTP_TAG_TIMEOUT;
Matt Johnston61c95992024-09-16 16:50:35 +08001009 *ret_tag = tag;
1010 mctp->tag_round_robin = (tag + 1) % 8;
1011 return 0;
1012 }
1013 }
1014
1015 // All 8 tags are used for this src/dest pair
1016 return -EBUSY;
1017}
1018
1019int mctp_message_tx_request(struct mctp *mctp, mctp_eid_t eid, void *msg,
1020 size_t msg_len, uint8_t *ret_alloc_msg_tag)
1021{
1022 int rc;
1023 struct mctp_bus *bus;
1024
1025 bus = find_bus_for_eid(mctp, eid);
1026 if (!bus) {
1027 __mctp_msg_free(msg, mctp);
1028 return 0;
1029 }
1030
1031 uint8_t alloc_tag;
1032 rc = mctp_alloc_tag(mctp, bus->eid, eid, &alloc_tag);
1033 if (rc) {
1034 mctp_prdebug("Failed allocating tag");
1035 __mctp_msg_free(msg, mctp);
1036 return rc;
1037 }
1038
1039 if (ret_alloc_msg_tag) {
1040 *ret_alloc_msg_tag = alloc_tag;
1041 }
1042
1043 return mctp_message_tx_alloced(mctp, eid, true, alloc_tag, msg,
1044 msg_len);
1045}
1046
Matt Johnston4a09e1d2024-09-13 14:55:58 +08001047bool mctp_is_tx_ready(struct mctp *mctp, mctp_eid_t eid)
1048{
1049 struct mctp_bus *bus;
1050
1051 bus = find_bus_for_eid(mctp, eid);
1052 if (!bus) {
1053 return true;
1054 }
1055 return bus->tx_msg == NULL;
1056}
1057
1058void *mctp_get_alloc_ctx(struct mctp *mctp)
1059{
1060 return mctp->alloc_ctx;
1061}
1062
1063void mctp_set_alloc_ctx(struct mctp *mctp, void *ctx)
1064{
1065 mctp->alloc_ctx = ctx;
1066}