blob: 274cd91bc38b614dd689eafbf971c1f8c3616bbb [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>
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Ċ‚gowskiba6727e2020-03-13 18:25:01 +010018#include "libmctp-cmds.h"
Andrew Jefferyc2b833e2020-10-28 14:28:37 +103019#include "range.h"
Matt Johnston4a09e1d2024-09-13 14:55:58 +080020#include "compiler.h"
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080021
Matt Johnston722d0db2024-09-13 15:51:30 +080022/* 64kb should be sufficient for a single message. Applications
23 * requiring higher sizes can override by setting max_message_size.*/
24#ifndef MCTP_MAX_MESSAGE_SIZE
25#define MCTP_MAX_MESSAGE_SIZE 65536
26#endif
27
28/* Must be >= 2 for bridge busses */
29#ifndef MCTP_MAX_BUSSES
30#define MCTP_MAX_BUSSES 2
31#endif
32
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080033/* Internal data structures */
34
Andrew Jefferyc61501c2021-01-27 23:24:18 +103035enum mctp_bus_state {
36 mctp_bus_state_constructed = 0,
37 mctp_bus_state_tx_enabled,
38 mctp_bus_state_tx_disabled,
39};
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080040
Andrew Jefferyc61501c2021-01-27 23:24:18 +103041struct mctp_bus {
42 mctp_eid_t eid;
43 struct mctp_binding *binding;
44 enum mctp_bus_state state;
Matt Johnston4a09e1d2024-09-13 14:55:58 +080045 struct mctp *mctp;
Andrew Jefferyc61501c2021-01-27 23:24:18 +103046
Matt Johnston4a09e1d2024-09-13 14:55:58 +080047 /* Current message to transmit */
48 void *tx_msg;
49 /* Position in tx_msg */
50 size_t tx_msgpos;
51 /* Length of tx_msg */
52 size_t tx_msglen;
53 /* Length of current packet payload */
54 size_t tx_pktlen;
55 uint8_t tx_seq;
56 uint8_t tx_src;
57 uint8_t tx_dest;
58 bool tx_to;
59 uint8_t tx_tag;
Jeremy Kerrcc2458d2019-03-01 08:23:33 +080060
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080061 /* todo: routing */
62};
63
Jeremy Kerr24db71f2019-02-07 21:37:35 +080064struct mctp_msg_ctx {
Matt Johnston4a09e1d2024-09-13 14:55:58 +080065 /* NULL buf indicates an unused mctp_msg_ctx */
66 void *buf;
67
Patrick Williamsa721c2d2022-12-04 14:30:26 -060068 uint8_t src;
69 uint8_t dest;
70 uint8_t tag;
71 uint8_t last_seq;
Patrick Williamsa721c2d2022-12-04 14:30:26 -060072 size_t buf_size;
73 size_t buf_alloc_size;
74 size_t fragment_size;
Jeremy Kerr24db71f2019-02-07 21:37:35 +080075};
76
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080077struct mctp {
Patrick Williamsa721c2d2022-12-04 14:30:26 -060078 int n_busses;
Matt Johnston722d0db2024-09-13 15:51:30 +080079 struct mctp_bus busses[MCTP_MAX_BUSSES];
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080080
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080081 /* Message RX callback */
Patrick Williamsa721c2d2022-12-04 14:30:26 -060082 mctp_rx_fn message_rx;
83 void *message_rx_data;
Jeremy Kerr24db71f2019-02-07 21:37:35 +080084
Andrew Jeffery5d3d4e62021-08-20 16:44:40 +093085 /* Packet capture callback */
Patrick Williamsa721c2d2022-12-04 14:30:26 -060086 mctp_capture_fn capture;
87 void *capture_data;
Andrew Jeffery5d3d4e62021-08-20 16:44:40 +093088
Jeremy Kerr24db71f2019-02-07 21:37:35 +080089 /* Message reassembly.
90 * @todo: flexible context count
91 */
Patrick Williamsa721c2d2022-12-04 14:30:26 -060092 struct mctp_msg_ctx msg_ctxs[16];
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +080093
94 enum {
95 ROUTE_ENDPOINT,
96 ROUTE_BRIDGE,
Patrick Williamsa721c2d2022-12-04 14:30:26 -060097 } route_policy;
Sumanth Bhat2c820c52020-07-02 00:26:25 +053098 size_t max_message_size;
Matt Johnston4a09e1d2024-09-13 14:55:58 +080099
100 void *alloc_ctx;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800101};
102
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800103#ifndef ARRAY_SIZE
104#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
105#endif
106
Andrew Jefferyb93b6112020-06-05 14:13:44 +0930107static int mctp_message_tx_on_bus(struct mctp_bus *bus, mctp_eid_t src,
Sumanth Bhatf39c3852022-01-10 17:04:10 +0530108 mctp_eid_t dest, bool tag_owner,
109 uint8_t msg_tag, void *msg, size_t msg_len);
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800110
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +0800111struct mctp_pktbuf *mctp_pktbuf_alloc(struct mctp_binding *binding, size_t len)
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800112{
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800113 size_t size =
114 binding->pkt_size + binding->pkt_header + binding->pkt_trailer;
Rashmica Gupta487b31e2022-09-14 18:49:45 +1000115 if (len > size) {
116 return NULL;
117 }
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800118
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800119 void *storage = __mctp_alloc(size + sizeof(struct mctp_pktbuf));
120 if (!storage) {
Pedro Martelletto2608b292023-03-30 13:28:28 +0000121 return NULL;
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800122 }
123 struct mctp_pktbuf *pkt = mctp_pktbuf_init(binding, storage);
124 pkt->alloc = true;
125 pkt->end = pkt->start + len;
126 return pkt;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800127}
128
129void mctp_pktbuf_free(struct mctp_pktbuf *pkt)
130{
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800131 if (pkt->alloc) {
132 __mctp_free(pkt);
133 } else {
134 mctp_prdebug("pktbuf_free called for non-alloced");
135 }
136}
137
138struct mctp_pktbuf *mctp_pktbuf_init(struct mctp_binding *binding,
139 void *storage)
140{
141 size_t size =
142 binding->pkt_size + binding->pkt_header + binding->pkt_trailer;
143 struct mctp_pktbuf *buf = (struct mctp_pktbuf *)storage;
144 buf->size = size;
145 buf->start = binding->pkt_header;
146 buf->end = buf->start;
147 buf->mctp_hdr_off = buf->start;
148 buf->alloc = false;
149
150 return buf;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800151}
152
153struct mctp_hdr *mctp_pktbuf_hdr(struct mctp_pktbuf *pkt)
154{
Moritz Fischer7aaccb52022-06-28 20:04:04 -0700155 return (struct mctp_hdr *)(pkt->data + pkt->mctp_hdr_off);
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800156}
157
158void *mctp_pktbuf_data(struct mctp_pktbuf *pkt)
159{
Moritz Fischer7aaccb52022-06-28 20:04:04 -0700160 return pkt->data + pkt->mctp_hdr_off + sizeof(struct mctp_hdr);
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800161}
162
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800163size_t mctp_pktbuf_size(const struct mctp_pktbuf *pkt)
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800164{
165 return pkt->end - pkt->start;
166}
167
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +0800168void *mctp_pktbuf_alloc_start(struct mctp_pktbuf *pkt, size_t size)
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800169{
170 assert(size <= pkt->start);
171 pkt->start -= size;
172 return pkt->data + pkt->start;
173}
174
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +0800175void *mctp_pktbuf_alloc_end(struct mctp_pktbuf *pkt, size_t size)
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800176{
177 void *buf;
178
Andrew Jeffery3ac70d62020-07-01 00:50:44 +0930179 assert(size <= (pkt->size - pkt->end));
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800180 buf = pkt->data + pkt->end;
181 pkt->end += size;
182 return buf;
183}
184
Matt Johnstondfbf0fd2024-10-28 14:40:29 +0800185int mctp_pktbuf_push(struct mctp_pktbuf *pkt, const void *data, size_t len)
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800186{
187 void *p;
188
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +0800189 if (pkt->end + len > pkt->size)
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800190 return -1;
191
192 p = pkt->data + pkt->end;
193
194 pkt->end += len;
195 memcpy(p, data, len);
196
197 return 0;
198}
199
Andrew Jefferyeba19a32021-03-09 23:09:40 +1030200void *mctp_pktbuf_pop(struct mctp_pktbuf *pkt, size_t len)
201{
202 if (len > mctp_pktbuf_size(pkt))
203 return NULL;
204
205 pkt->end -= len;
206 return pkt->data + pkt->end;
207}
208
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800209/* Allocate a duplicate of the message and copy it */
210static void *mctp_msg_dup(const void *msg, size_t msg_len, struct mctp *mctp)
211{
212 void *copy = __mctp_msg_alloc(msg_len, mctp);
213 if (!copy) {
214 mctp_prdebug("msg dup len %zu failed", msg_len);
215 return NULL;
216 }
217
218 memcpy(copy, msg, msg_len);
219 return copy;
220}
221
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800222/* Message reassembly */
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600223static struct mctp_msg_ctx *mctp_msg_ctx_lookup(struct mctp *mctp, uint8_t src,
224 uint8_t dest, uint8_t tag)
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800225{
226 unsigned int i;
227
228 /* @todo: better lookup, if we add support for more outstanding
229 * message contexts */
230 for (i = 0; i < ARRAY_SIZE(mctp->msg_ctxs); i++) {
231 struct mctp_msg_ctx *ctx = &mctp->msg_ctxs[i];
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800232 if (ctx->buf && ctx->src == src && ctx->dest == dest &&
233 ctx->tag == tag)
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800234 return ctx;
235 }
236
237 return NULL;
238}
239
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600240static struct mctp_msg_ctx *mctp_msg_ctx_create(struct mctp *mctp, uint8_t src,
241 uint8_t dest, uint8_t tag)
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800242{
Jeremy Kerr11a234e2019-02-27 17:59:53 +0800243 struct mctp_msg_ctx *ctx = NULL;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800244 unsigned int i;
245
246 for (i = 0; i < ARRAY_SIZE(mctp->msg_ctxs); i++) {
247 struct mctp_msg_ctx *tmp = &mctp->msg_ctxs[i];
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800248 if (!tmp->buf) {
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800249 ctx = tmp;
250 break;
251 }
252 }
253
254 if (!ctx)
255 return NULL;
256
257 ctx->src = src;
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800258 ctx->dest = dest;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800259 ctx->tag = tag;
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800260
Jeremy Kerr9a3da812019-08-02 15:57:53 +0800261 ctx->buf_size = 0;
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800262 ctx->buf_alloc_size = mctp->max_message_size;
263 ctx->buf = __mctp_msg_alloc(ctx->buf_alloc_size, mctp);
264 if (!ctx->buf) {
265 return NULL;
266 }
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800267
268 return ctx;
269}
270
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800271static void mctp_msg_ctx_drop(struct mctp_bus *bus, struct mctp_msg_ctx *ctx)
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800272{
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800273 /* Free and mark as unused */
274 __mctp_msg_free(ctx->buf, bus->mctp);
275 ctx->buf = NULL;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800276}
277
278static void mctp_msg_ctx_reset(struct mctp_msg_ctx *ctx)
279{
280 ctx->buf_size = 0;
Sumanth Bhat69f545f2021-05-18 09:16:43 +0000281 ctx->fragment_size = 0;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800282}
283
284static int mctp_msg_ctx_add_pkt(struct mctp_msg_ctx *ctx,
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800285 struct mctp_pktbuf *pkt)
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800286{
287 size_t len;
288
289 len = mctp_pktbuf_size(pkt) - sizeof(struct mctp_hdr);
290
Sumanth Bhatbc79c242021-06-16 12:36:56 +0530291 if (len + ctx->buf_size < ctx->buf_size) {
292 return -1;
293 }
294
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800295 if (ctx->buf_size + len > ctx->buf_alloc_size) {
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800296 return -1;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800297 }
298
Moritz Fischer7aaccb52022-06-28 20:04:04 -0700299 memcpy((uint8_t *)ctx->buf + ctx->buf_size, mctp_pktbuf_data(pkt), len);
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800300 ctx->buf_size += len;
301
302 return 0;
303}
304
305/* Core API functions */
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800306struct mctp *mctp_init(void)
307{
308 struct mctp *mctp;
309
310 mctp = __mctp_alloc(sizeof(*mctp));
Sumanth Bhat96d54492020-07-14 17:10:04 +0530311
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600312 if (!mctp)
Sumanth Bhat96d54492020-07-14 17:10:04 +0530313 return NULL;
314
Matt Johnston722d0db2024-09-13 15:51:30 +0800315 mctp_setup(mctp);
316 return mctp;
317}
318
319void mctp_setup(struct mctp *mctp)
320{
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800321 memset(mctp, 0, sizeof(*mctp));
Sumanth Bhat2c820c52020-07-02 00:26:25 +0530322 mctp->max_message_size = MCTP_MAX_MESSAGE_SIZE;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800323}
324
Sumanth Bhat2c820c52020-07-02 00:26:25 +0530325void mctp_set_max_message_size(struct mctp *mctp, size_t message_size)
326{
327 mctp->max_message_size = message_size;
328}
329
Andrew Jeffery5d3d4e62021-08-20 16:44:40 +0930330void mctp_set_capture_handler(struct mctp *mctp, mctp_capture_fn fn, void *user)
331{
332 mctp->capture = fn;
333 mctp->capture_data = user;
334}
335
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800336static void mctp_bus_destroy(struct mctp_bus *bus, struct mctp *mctp)
Andrew Jeffery3ae89dc2021-01-28 15:24:36 +1030337{
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800338 if (bus->tx_msg) {
339 __mctp_msg_free(bus->tx_msg, mctp);
340 bus->tx_msg = NULL;
Andrew Jeffery3ae89dc2021-01-28 15:24:36 +1030341 }
342}
343
Matt Johnston722d0db2024-09-13 15:51:30 +0800344void mctp_cleanup(struct mctp *mctp)
Andrew Jefferyfa56ca52020-03-10 23:18:22 +1030345{
Andrew Jefferyb93b6112020-06-05 14:13:44 +0930346 size_t i;
Andrew Jefferyfa56ca52020-03-10 23:18:22 +1030347
348 /* Cleanup message assembly contexts */
Matt Johnston3ef47782024-12-11 15:19:06 +0800349 static_assert(ARRAY_SIZE(mctp->msg_ctxs) < SIZE_MAX, "size");
Andrew Jefferyfa56ca52020-03-10 23:18:22 +1030350 for (i = 0; i < ARRAY_SIZE(mctp->msg_ctxs); i++) {
351 struct mctp_msg_ctx *tmp = &mctp->msg_ctxs[i];
352 if (tmp->buf)
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800353 __mctp_msg_free(tmp->buf, mctp);
Andrew Jefferyfa56ca52020-03-10 23:18:22 +1030354 }
355
Andrew Jeffery3ae89dc2021-01-28 15:24:36 +1030356 while (mctp->n_busses--)
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800357 mctp_bus_destroy(&mctp->busses[mctp->n_busses], mctp);
Matt Johnston722d0db2024-09-13 15:51:30 +0800358}
Andrew Jeffery3ae89dc2021-01-28 15:24:36 +1030359
Matt Johnston722d0db2024-09-13 15:51:30 +0800360void mctp_destroy(struct mctp *mctp)
361{
362 mctp_cleanup(mctp);
Andrew Jefferyfa56ca52020-03-10 23:18:22 +1030363 __mctp_free(mctp);
364}
365
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800366int mctp_set_rx_all(struct mctp *mctp, mctp_rx_fn fn, void *data)
367{
368 mctp->message_rx = fn;
369 mctp->message_rx_data = data;
370 return 0;
371}
372
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600373static struct mctp_bus *find_bus_for_eid(struct mctp *mctp, mctp_eid_t dest
374 __attribute__((unused)))
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800375{
Brad Bishop663ec392021-10-07 21:16:48 -0400376 if (mctp->n_busses == 0)
377 return NULL;
378
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800379 /* for now, just use the first bus. For full routing support,
380 * we will need a table of neighbours */
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800381 return &mctp->busses[0];
382}
383
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600384int mctp_register_bus(struct mctp *mctp, struct mctp_binding *binding,
385 mctp_eid_t eid)
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800386{
Andrew Jeffery3e8a12a2020-06-05 16:08:30 +0930387 int rc = 0;
388
Jeremy Kerr7520cec2019-03-01 07:13:18 +0800389 /* todo: multiple busses */
Matt Johnston722d0db2024-09-13 15:51:30 +0800390 static_assert(MCTP_MAX_BUSSES >= 1, "need a bus");
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800391 assert(mctp->n_busses == 0);
392 mctp->n_busses = 1;
Andrew Jeffery3e8a12a2020-06-05 16:08:30 +0930393
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800394 assert(binding->tx_storage);
395
James Feist62d72362019-12-13 13:43:32 -0800396 memset(mctp->busses, 0, sizeof(struct mctp_bus));
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800397 mctp->busses[0].mctp = mctp;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800398 mctp->busses[0].binding = binding;
399 mctp->busses[0].eid = eid;
Jeremy Kerr7520cec2019-03-01 07:13:18 +0800400 binding->bus = &mctp->busses[0];
Jeremy Kerr0a00dca2019-03-01 08:01:35 +0800401 binding->mctp = mctp;
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800402 mctp->route_policy = ROUTE_ENDPOINT;
Jeremy Kerr3b36d172019-09-04 11:56:09 +0800403
Andrew Jeffery3e8a12a2020-06-05 16:08:30 +0930404 if (binding->start) {
405 rc = binding->start(binding);
406 if (rc < 0) {
407 mctp_prerr("Failed to start binding: %d", rc);
Andrew Jeffery19275232021-01-29 14:13:25 +1030408 binding->bus = NULL;
Andrew Jeffery2304c832021-01-29 11:52:49 +1030409 mctp->n_busses = 0;
Andrew Jeffery3e8a12a2020-06-05 16:08:30 +0930410 }
411 }
Jeremy Kerr3b36d172019-09-04 11:56:09 +0800412
Andrew Jeffery3e8a12a2020-06-05 16:08:30 +0930413 return rc;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800414}
415
Andrew Jeffery2094c3c2021-08-26 12:32:46 +0930416void mctp_unregister_bus(struct mctp *mctp, struct mctp_binding *binding)
417{
418 /*
419 * We only support one bus right now; once the call completes we will
420 * have no more busses
421 */
422 mctp->n_busses = 0;
423 binding->mctp = NULL;
424 binding->bus = NULL;
Andrew Jeffery2094c3c2021-08-26 12:32:46 +0930425}
426
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600427int mctp_bridge_busses(struct mctp *mctp, struct mctp_binding *b1,
428 struct mctp_binding *b2)
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800429{
Andrew Jeffery19275232021-01-29 14:13:25 +1030430 int rc = 0;
431
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800432 assert(b1->tx_storage);
433 assert(b2->tx_storage);
434
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800435 assert(mctp->n_busses == 0);
Matt Johnston722d0db2024-09-13 15:51:30 +0800436 assert(MCTP_MAX_BUSSES >= 2);
James Feist62d72362019-12-13 13:43:32 -0800437 memset(mctp->busses, 0, 2 * sizeof(struct mctp_bus));
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800438 mctp->n_busses = 2;
439 mctp->busses[0].binding = b1;
440 b1->bus = &mctp->busses[0];
441 b1->mctp = mctp;
442 mctp->busses[1].binding = b2;
443 b2->bus = &mctp->busses[1];
444 b2->mctp = mctp;
445
446 mctp->route_policy = ROUTE_BRIDGE;
Jeremy Kerr3b36d172019-09-04 11:56:09 +0800447
Andrew Jeffery19275232021-01-29 14:13:25 +1030448 if (b1->start) {
449 rc = b1->start(b1);
450 if (rc < 0) {
451 mctp_prerr("Failed to start bridged bus %s: %d",
452 b1->name, rc);
453 goto done;
454 }
455 }
Jeremy Kerr3b36d172019-09-04 11:56:09 +0800456
Andrew Jeffery19275232021-01-29 14:13:25 +1030457 if (b2->start) {
458 rc = b2->start(b2);
459 if (rc < 0) {
460 mctp_prerr("Failed to start bridged bus %s: %d",
461 b2->name, rc);
462 goto done;
463 }
464 }
Jeremy Kerr3b36d172019-09-04 11:56:09 +0800465
Andrew Jeffery19275232021-01-29 14:13:25 +1030466done:
467 return rc;
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800468}
469
Wiktor GoĊ‚gowskiba6727e2020-03-13 18:25:01 +0100470static inline bool mctp_ctrl_cmd_is_transport(struct mctp_ctrl_msg_hdr *hdr)
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800471{
Matt Johnston3ef47782024-12-11 15:19:06 +0800472#pragma GCC diagnostic push
473#pragma GCC diagnostic ignored "-Wtype-limits"
Wiktor GoĊ‚gowskiba6727e2020-03-13 18:25:01 +0100474 return ((hdr->command_code >= MCTP_CTRL_CMD_FIRST_TRANSPORT) &&
475 (hdr->command_code <= MCTP_CTRL_CMD_LAST_TRANSPORT));
Matt Johnston3ef47782024-12-11 15:19:06 +0800476#pragma GCC diagnostic pop
Wiktor GoĊ‚gowskiba6727e2020-03-13 18:25:01 +0100477}
478
Andrew Jefferyb93b6112020-06-05 14:13:44 +0930479static bool mctp_ctrl_handle_msg(struct mctp_bus *bus, mctp_eid_t src,
Sumanth Bhatf39c3852022-01-10 17:04:10 +0530480 uint8_t msg_tag, bool tag_owner, void *buffer,
481 size_t length)
Wiktor GoĊ‚gowskiba6727e2020-03-13 18:25:01 +0100482{
483 struct mctp_ctrl_msg_hdr *msg_hdr = buffer;
484
485 /*
486 * Control message is received. If a transport control message handler
487 * is provided, it will called. If there is no dedicated handler, this
488 * function returns false and data can be handled by the generic
489 * message handler. The transport control message handler will be
490 * provided with messages in the command range 0xF0 - 0xFF.
491 */
492 if (mctp_ctrl_cmd_is_transport(msg_hdr)) {
493 if (bus->binding->control_rx != NULL) {
494 /* MCTP bus binding handler */
Sumanth Bhatf39c3852022-01-10 17:04:10 +0530495 bus->binding->control_rx(src, msg_tag, tag_owner,
Wiktor GoĊ‚gowskiba6727e2020-03-13 18:25:01 +0100496 bus->binding->control_rx_data,
497 buffer, length);
498 return true;
499 }
500 }
501
502 /*
503 * Command was not handled, due to lack of specific callback.
504 * It will be passed to regular message_rx handler.
505 */
506 return false;
507}
508
509static inline bool mctp_rx_dest_is_local(struct mctp_bus *bus, mctp_eid_t dest)
510{
511 return dest == bus->eid || dest == MCTP_EID_NULL ||
512 dest == MCTP_EID_BROADCAST;
513}
514
515static inline bool mctp_ctrl_cmd_is_request(struct mctp_ctrl_msg_hdr *hdr)
516{
517 return hdr->ic_msg_type == MCTP_CTRL_HDR_MSG_TYPE &&
518 hdr->rq_dgram_inst & MCTP_CTRL_HDR_FLAG_REQUEST;
519}
520
521/*
522 * Receive the complete MCTP message and route it.
523 * Asserts:
524 * 'buf' is not NULL.
525 */
526static void mctp_rx(struct mctp *mctp, struct mctp_bus *bus, mctp_eid_t src,
Sumanth Bhatf39c3852022-01-10 17:04:10 +0530527 mctp_eid_t dest, bool tag_owner, uint8_t msg_tag, void *buf,
528 size_t len)
Wiktor GoĊ‚gowskiba6727e2020-03-13 18:25:01 +0100529{
530 assert(buf != NULL);
531
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800532 if (mctp->route_policy == ROUTE_ENDPOINT &&
Wiktor GoĊ‚gowskiba6727e2020-03-13 18:25:01 +0100533 mctp_rx_dest_is_local(bus, dest)) {
534 /* Handle MCTP Control Messages: */
535 if (len >= sizeof(struct mctp_ctrl_msg_hdr)) {
536 struct mctp_ctrl_msg_hdr *msg_hdr = buf;
537
538 /*
539 * Identify if this is a control request message.
540 * See DSP0236 v1.3.0 sec. 11.5.
541 */
542 if (mctp_ctrl_cmd_is_request(msg_hdr)) {
543 bool handled;
Sumanth Bhatf39c3852022-01-10 17:04:10 +0530544 handled = mctp_ctrl_handle_msg(
545 bus, src, msg_tag, tag_owner, buf, len);
Wiktor GoĊ‚gowskiba6727e2020-03-13 18:25:01 +0100546 if (handled)
547 return;
548 }
549 }
Sumanth Bhatf39c3852022-01-10 17:04:10 +0530550
Wiktor GoĊ‚gowskiba6727e2020-03-13 18:25:01 +0100551 if (mctp->message_rx)
Sumanth Bhatf39c3852022-01-10 17:04:10 +0530552 mctp->message_rx(src, tag_owner, msg_tag,
553 mctp->message_rx_data, buf, len);
Wiktor GoĊ‚gowskiba6727e2020-03-13 18:25:01 +0100554 }
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800555
556 if (mctp->route_policy == ROUTE_BRIDGE) {
557 int i;
558
559 for (i = 0; i < mctp->n_busses; i++) {
560 struct mctp_bus *dest_bus = &mctp->busses[i];
561 if (dest_bus == bus)
562 continue;
563
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800564 void *copy = mctp_msg_dup(buf, len, mctp);
565 if (!copy) {
566 return;
567 }
568
Sumanth Bhatf39c3852022-01-10 17:04:10 +0530569 mctp_message_tx_on_bus(dest_bus, src, dest, tag_owner,
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800570 msg_tag, copy, len);
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800571 }
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800572 }
573}
574
Jeremy Kerr0a00dca2019-03-01 08:01:35 +0800575void mctp_bus_rx(struct mctp_binding *binding, struct mctp_pktbuf *pkt)
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800576{
Jeremy Kerr7520cec2019-03-01 07:13:18 +0800577 struct mctp_bus *bus = binding->bus;
Jeremy Kerr0a00dca2019-03-01 08:01:35 +0800578 struct mctp *mctp = binding->mctp;
Ed Tanousc2def9f2019-02-21 08:33:08 -0800579 uint8_t flags, exp_seq, seq, tag;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800580 struct mctp_msg_ctx *ctx;
581 struct mctp_hdr *hdr;
Sumanth Bhatf39c3852022-01-10 17:04:10 +0530582 bool tag_owner;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800583 size_t len;
584 void *p;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800585 int rc;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800586
Jeremy Kerr7520cec2019-03-01 07:13:18 +0800587 assert(bus);
588
Sumanth Bhatd97869d2020-07-02 00:46:13 +0530589 /* Drop packet if it was smaller than mctp hdr size */
Matt Johnston86e9a972024-10-28 15:06:33 +0800590 if (mctp_pktbuf_size(pkt) < sizeof(struct mctp_hdr))
Sumanth Bhatd97869d2020-07-02 00:46:13 +0530591 goto out;
592
Andrew Jeffery5d3d4e62021-08-20 16:44:40 +0930593 if (mctp->capture)
Rashmica Guptaf2988972022-11-09 12:26:44 +1100594 mctp->capture(pkt, MCTP_MESSAGE_CAPTURE_INCOMING,
595 mctp->capture_data);
Andrew Jeffery5d3d4e62021-08-20 16:44:40 +0930596
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800597 hdr = mctp_pktbuf_hdr(pkt);
598
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800599 /* small optimisation: don't bother reassembly if we're going to
600 * drop the packet in mctp_rx anyway */
John Chung133df7a2024-05-14 16:19:56 +0800601 if (mctp->route_policy == ROUTE_ENDPOINT &&
602 !mctp_rx_dest_is_local(bus, hdr->dest))
Jeremy Kerrc1693af2019-08-05 14:30:59 +0800603 goto out;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800604
605 flags = hdr->flags_seq_tag & (MCTP_HDR_FLAG_SOM | MCTP_HDR_FLAG_EOM);
606 tag = (hdr->flags_seq_tag >> MCTP_HDR_TAG_SHIFT) & MCTP_HDR_TAG_MASK;
607 seq = (hdr->flags_seq_tag >> MCTP_HDR_SEQ_SHIFT) & MCTP_HDR_SEQ_MASK;
Andrew Jeffery7f7fdc12023-05-12 15:56:47 +0930608 tag_owner = (hdr->flags_seq_tag >> MCTP_HDR_TO_SHIFT) &
609 MCTP_HDR_TO_MASK;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800610
611 switch (flags) {
612 case MCTP_HDR_FLAG_SOM | MCTP_HDR_FLAG_EOM:
613 /* single-packet message - send straight up to rx function,
614 * no need to create a message context */
615 len = pkt->end - pkt->mctp_hdr_off - sizeof(struct mctp_hdr);
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800616 p = mctp_msg_dup(pkt->data + pkt->mctp_hdr_off +
617 sizeof(struct mctp_hdr),
618 len, mctp);
619 if (p) {
620 mctp_rx(mctp, bus, hdr->src, hdr->dest, tag_owner, tag,
621 p, len);
622 __mctp_msg_free(p, mctp);
623 }
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800624 break;
625
626 case MCTP_HDR_FLAG_SOM:
627 /* start of a new message - start the new context for
628 * future message reception. If an existing context is
629 * already present, drop it. */
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800630 ctx = mctp_msg_ctx_lookup(mctp, hdr->src, hdr->dest, tag);
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800631 if (ctx) {
632 mctp_msg_ctx_reset(ctx);
633 } else {
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600634 ctx = mctp_msg_ctx_create(mctp, hdr->src, hdr->dest,
635 tag);
Sumanth Bhat34d4c962021-06-16 12:50:48 +0530636 /* If context creation fails due to exhaution of contexts we
637 * can support, drop the packet */
638 if (!ctx) {
639 mctp_prdebug("Context buffers exhausted.");
640 goto out;
641 }
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800642 }
643
Sumanth Bhat69f545f2021-05-18 09:16:43 +0000644 /* Save the fragment size, subsequent middle fragments
645 * should of the same size */
646 ctx->fragment_size = mctp_pktbuf_size(pkt);
647
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800648 rc = mctp_msg_ctx_add_pkt(ctx, pkt);
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800649 if (rc) {
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800650 mctp_msg_ctx_drop(bus, ctx);
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800651 } else {
652 ctx->last_seq = seq;
653 }
654
655 break;
656
657 case MCTP_HDR_FLAG_EOM:
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800658 ctx = mctp_msg_ctx_lookup(mctp, hdr->src, hdr->dest, tag);
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800659 if (!ctx)
Jeremy Kerrc1693af2019-08-05 14:30:59 +0800660 goto out;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800661
Ed Tanousc2def9f2019-02-21 08:33:08 -0800662 exp_seq = (ctx->last_seq + 1) % 4;
663
664 if (exp_seq != seq) {
665 mctp_prdebug(
666 "Sequence number %d does not match expected %d",
667 seq, exp_seq);
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800668 mctp_msg_ctx_drop(bus, ctx);
Jeremy Kerrc1693af2019-08-05 14:30:59 +0800669 goto out;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800670 }
671
Sumanth Bhat69f545f2021-05-18 09:16:43 +0000672 len = mctp_pktbuf_size(pkt);
673
674 if (len > ctx->fragment_size) {
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600675 mctp_prdebug("Unexpected fragment size. Expected"
676 " less than %zu, received = %zu",
677 ctx->fragment_size, len);
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800678 mctp_msg_ctx_drop(bus, ctx);
Sumanth Bhat69f545f2021-05-18 09:16:43 +0000679 goto out;
680 }
681
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800682 rc = mctp_msg_ctx_add_pkt(ctx, pkt);
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800683 if (!rc)
Sumanth Bhatf39c3852022-01-10 17:04:10 +0530684 mctp_rx(mctp, bus, ctx->src, ctx->dest, tag_owner, tag,
685 ctx->buf, ctx->buf_size);
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800686
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800687 mctp_msg_ctx_drop(bus, ctx);
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800688 break;
Ed Tanousc2def9f2019-02-21 08:33:08 -0800689
690 case 0:
691 /* Neither SOM nor EOM */
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600692 ctx = mctp_msg_ctx_lookup(mctp, hdr->src, hdr->dest, tag);
Ed Tanousc2def9f2019-02-21 08:33:08 -0800693 if (!ctx)
Jeremy Kerrc1693af2019-08-05 14:30:59 +0800694 goto out;
Ed Tanousc2def9f2019-02-21 08:33:08 -0800695
696 exp_seq = (ctx->last_seq + 1) % 4;
697 if (exp_seq != seq) {
698 mctp_prdebug(
699 "Sequence number %d does not match expected %d",
700 seq, exp_seq);
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800701 mctp_msg_ctx_drop(bus, ctx);
Jeremy Kerrc1693af2019-08-05 14:30:59 +0800702 goto out;
Ed Tanousc2def9f2019-02-21 08:33:08 -0800703 }
704
Sumanth Bhat69f545f2021-05-18 09:16:43 +0000705 len = mctp_pktbuf_size(pkt);
706
707 if (len != ctx->fragment_size) {
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600708 mctp_prdebug("Unexpected fragment size. Expected = %zu "
709 "received = %zu",
710 ctx->fragment_size, len);
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800711 mctp_msg_ctx_drop(bus, ctx);
Sumanth Bhat69f545f2021-05-18 09:16:43 +0000712 goto out;
713 }
714
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800715 rc = mctp_msg_ctx_add_pkt(ctx, pkt);
Ed Tanousc2def9f2019-02-21 08:33:08 -0800716 if (rc) {
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800717 mctp_msg_ctx_drop(bus, ctx);
Jeremy Kerrc1693af2019-08-05 14:30:59 +0800718 goto out;
Ed Tanousc2def9f2019-02-21 08:33:08 -0800719 }
720 ctx->last_seq = seq;
721
722 break;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800723 }
Jeremy Kerrc1693af2019-08-05 14:30:59 +0800724out:
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800725 return;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800726}
727
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600728static int mctp_packet_tx(struct mctp_bus *bus, struct mctp_pktbuf *pkt)
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800729{
Andrew Jeffery5d3d4e62021-08-20 16:44:40 +0930730 struct mctp *mctp = bus->binding->mctp;
731
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800732 if (bus->state != mctp_bus_state_tx_enabled) {
733 mctp_prdebug("tx with bus disabled");
Jeremy Kerr1cd31182019-02-27 18:01:00 +0800734 return -1;
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800735 }
Jeremy Kerr1cd31182019-02-27 18:01:00 +0800736
Andrew Jeffery5d3d4e62021-08-20 16:44:40 +0930737 if (mctp->capture)
Rashmica Guptaf2988972022-11-09 12:26:44 +1100738 mctp->capture(pkt, MCTP_MESSAGE_CAPTURE_OUTGOING,
739 mctp->capture_data);
Andrew Jeffery5d3d4e62021-08-20 16:44:40 +0930740
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800741 return bus->binding->tx(bus->binding, pkt);
742}
743
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800744/* Returns a pointer to the binding's tx_storage */
745static struct mctp_pktbuf *mctp_next_tx_pkt(struct mctp_bus *bus)
746{
747 if (!bus->tx_msg) {
748 return NULL;
749 }
750
751 size_t p = bus->tx_msgpos;
752 size_t msg_len = bus->tx_msglen;
753 size_t payload_len = msg_len - p;
754 size_t max_payload_len = MCTP_BODY_SIZE(bus->binding->pkt_size);
755 if (payload_len > max_payload_len)
756 payload_len = max_payload_len;
757
758 struct mctp_pktbuf *pkt =
759 mctp_pktbuf_init(bus->binding, bus->binding->tx_storage);
760 struct mctp_hdr *hdr = mctp_pktbuf_hdr(pkt);
761
762 hdr->ver = bus->binding->version & 0xf;
763 hdr->dest = bus->tx_dest;
764 hdr->src = bus->tx_src;
765 hdr->flags_seq_tag = (bus->tx_to << MCTP_HDR_TO_SHIFT) |
766 (bus->tx_tag << MCTP_HDR_TAG_SHIFT);
767
768 if (p == 0)
769 hdr->flags_seq_tag |= MCTP_HDR_FLAG_SOM;
770 if (p + payload_len >= msg_len)
771 hdr->flags_seq_tag |= MCTP_HDR_FLAG_EOM;
772 hdr->flags_seq_tag |= bus->tx_seq << MCTP_HDR_SEQ_SHIFT;
773
774 memcpy(mctp_pktbuf_data(pkt), (uint8_t *)bus->tx_msg + p, payload_len);
775 pkt->end = pkt->start + sizeof(*hdr) + payload_len;
776 bus->tx_pktlen = payload_len;
777
778 mctp_prdebug(
779 "tx dst %d tag %d payload len %zu seq %d. msg pos %zu len %zu",
780 hdr->dest, bus->tx_tag, payload_len, bus->tx_seq, p, msg_len);
781
782 return pkt;
783}
784
785/* Called when a packet has successfully been sent */
786static void mctp_tx_complete(struct mctp_bus *bus)
787{
788 if (!bus->tx_msg) {
789 mctp_prdebug("tx complete no message");
790 return;
791 }
792
793 bus->tx_seq = (bus->tx_seq + 1) & MCTP_HDR_SEQ_MASK;
794 bus->tx_msgpos += bus->tx_pktlen;
795
796 if (bus->tx_msgpos >= bus->tx_msglen) {
797 __mctp_msg_free(bus->tx_msg, bus->binding->mctp);
798 bus->tx_msg = NULL;
799 }
800}
801
Jeremy Kerrcc2458d2019-03-01 08:23:33 +0800802static void mctp_send_tx_queue(struct mctp_bus *bus)
Jeremy Kerr1cd31182019-02-27 18:01:00 +0800803{
804 struct mctp_pktbuf *pkt;
805
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800806 while (bus->tx_msg && bus->state == mctp_bus_state_tx_enabled) {
Jeremy Kerr1cd31182019-02-27 18:01:00 +0800807 int rc;
808
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800809 pkt = mctp_next_tx_pkt(bus);
810
Jeremy Kerr1cd31182019-02-27 18:01:00 +0800811 rc = mctp_packet_tx(bus, pkt);
Andrew Jeffery0721f582022-09-29 12:12:39 +0930812 switch (rc) {
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800813 /* If transmission succeded */
Andrew Jeffery0721f582022-09-29 12:12:39 +0930814 case 0:
Andrew Jeffery0721f582022-09-29 12:12:39 +0930815 /* Drop the packet */
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800816 mctp_tx_complete(bus);
Jeremy Kerr1cd31182019-02-27 18:01:00 +0800817 break;
818
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800819 /* If the binding was busy */
Andrew Jeffery0721f582022-09-29 12:12:39 +0930820 case -EBUSY:
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800821 /* Keep the packet for next try */
822 mctp_prdebug("tx EBUSY");
823 return;
824
Andrew Jeffery0721f582022-09-29 12:12:39 +0930825 /* Some other unknown error occurred */
826 default:
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800827 /* Drop the packet */
828 mctp_prdebug("tx drop %d", rc);
829 mctp_tx_complete(bus);
830 return;
Andrew Jeffery0721f582022-09-29 12:12:39 +0930831 };
Jeremy Kerr1cd31182019-02-27 18:01:00 +0800832 }
Jeremy Kerr1cd31182019-02-27 18:01:00 +0800833}
834
835void mctp_binding_set_tx_enabled(struct mctp_binding *binding, bool enable)
836{
837 struct mctp_bus *bus = binding->bus;
Andrew Jefferyc61501c2021-01-27 23:24:18 +1030838
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600839 switch (bus->state) {
Andrew Jefferyc61501c2021-01-27 23:24:18 +1030840 case mctp_bus_state_constructed:
841 if (!enable)
842 return;
843
Andrew Jeffery1fa707e2021-01-28 15:22:11 +1030844 if (binding->pkt_size < MCTP_PACKET_SIZE(MCTP_BTU)) {
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600845 mctp_prerr(
846 "Cannot start %s binding with invalid MTU: %zu",
847 binding->name,
848 MCTP_BODY_SIZE(binding->pkt_size));
Andrew Jeffery1fa707e2021-01-28 15:22:11 +1030849 return;
850 }
851
Andrew Jefferyc61501c2021-01-27 23:24:18 +1030852 bus->state = mctp_bus_state_tx_enabled;
853 mctp_prinfo("%s binding started", binding->name);
854 return;
855 case mctp_bus_state_tx_enabled:
856 if (enable)
857 return;
858
859 bus->state = mctp_bus_state_tx_disabled;
860 mctp_prdebug("%s binding Tx disabled", binding->name);
861 return;
862 case mctp_bus_state_tx_disabled:
863 if (!enable)
864 return;
865
866 bus->state = mctp_bus_state_tx_enabled;
867 mctp_prdebug("%s binding Tx enabled", binding->name);
Jeremy Kerrcc2458d2019-03-01 08:23:33 +0800868 mctp_send_tx_queue(bus);
Andrew Jefferyc61501c2021-01-27 23:24:18 +1030869 return;
870 }
Jeremy Kerr1cd31182019-02-27 18:01:00 +0800871}
872
Andrew Jefferyb93b6112020-06-05 14:13:44 +0930873static int mctp_message_tx_on_bus(struct mctp_bus *bus, mctp_eid_t src,
Sumanth Bhatf39c3852022-01-10 17:04:10 +0530874 mctp_eid_t dest, bool tag_owner,
875 uint8_t msg_tag, void *msg, size_t msg_len)
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800876{
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800877 size_t max_payload_len;
878 int rc;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800879
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800880 if (bus->state == mctp_bus_state_constructed) {
881 rc = -ENXIO;
882 goto err;
883 }
Andrew Jefferyc61501c2021-01-27 23:24:18 +1030884
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800885 if ((msg_tag & MCTP_HDR_TAG_MASK) != msg_tag) {
886 rc = -EINVAL;
887 goto err;
888 }
Sumanth Bhatf39c3852022-01-10 17:04:10 +0530889
Andrew Jeffery1fa707e2021-01-28 15:22:11 +1030890 max_payload_len = MCTP_BODY_SIZE(bus->binding->pkt_size);
891
892 {
893 const bool valid_mtu = max_payload_len >= MCTP_BTU;
894 assert(valid_mtu);
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800895 if (!valid_mtu) {
896 rc = -EINVAL;
897 goto err;
898 }
Andrew Jeffery1fa707e2021-01-28 15:22:11 +1030899 }
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800900
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600901 mctp_prdebug(
902 "%s: Generating packets for transmission of %zu byte message from %hhu to %hhu",
903 __func__, msg_len, src, dest);
Andrew Jeffery298865f2020-02-06 11:51:29 +1030904
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800905 if (bus->tx_msg) {
906 mctp_prdebug("Bus busy");
907 rc = -EBUSY;
908 goto err;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800909 }
910
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800911 /* Take the message to send */
912 bus->tx_msg = msg;
913 bus->tx_msglen = msg_len;
914 bus->tx_msgpos = 0;
915 /* bus->tx_seq is allowed to continue from previous message */
916 bus->tx_src = src;
917 bus->tx_dest = dest;
918 bus->tx_to = tag_owner;
919 bus->tx_tag = msg_tag;
Andrew Jeffery298865f2020-02-06 11:51:29 +1030920
Jeremy Kerrcc2458d2019-03-01 08:23:33 +0800921 mctp_send_tx_queue(bus);
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800922 return 0;
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800923
924err:
925 __mctp_msg_free(msg, bus->binding->mctp);
926 return rc;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800927}
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800928
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800929int mctp_message_tx_alloced(struct mctp *mctp, mctp_eid_t eid, bool tag_owner,
930 uint8_t msg_tag, void *msg, size_t msg_len)
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800931{
932 struct mctp_bus *bus;
933
Sumanth Bhatf39c3852022-01-10 17:04:10 +0530934 /* TODO: Protect against same tag being used across
935 * different callers */
936 if ((msg_tag & MCTP_HDR_TAG_MASK) != msg_tag) {
937 mctp_prerr("Incorrect message tag %u passed.", msg_tag);
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800938 __mctp_msg_free(msg, mctp);
Sumanth Bhatf39c3852022-01-10 17:04:10 +0530939 return -EINVAL;
940 }
941
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800942 bus = find_bus_for_eid(mctp, eid);
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800943 if (!bus) {
944 __mctp_msg_free(msg, mctp);
Brad Bishop663ec392021-10-07 21:16:48 -0400945 return 0;
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800946 }
Brad Bishop663ec392021-10-07 21:16:48 -0400947
Sumanth Bhatf39c3852022-01-10 17:04:10 +0530948 return mctp_message_tx_on_bus(bus, bus->eid, eid, tag_owner, msg_tag,
949 msg, msg_len);
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800950}
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800951
952int mctp_message_tx(struct mctp *mctp, mctp_eid_t eid, bool tag_owner,
953 uint8_t msg_tag, const void *msg, size_t msg_len)
954{
955 void *copy = mctp_msg_dup(msg, msg_len, mctp);
956 if (!copy) {
957 return -ENOMEM;
958 }
959
960 return mctp_message_tx_alloced(mctp, eid, tag_owner, msg_tag, copy,
961 msg_len);
962}
963
964bool mctp_is_tx_ready(struct mctp *mctp, mctp_eid_t eid)
965{
966 struct mctp_bus *bus;
967
968 bus = find_bus_for_eid(mctp, eid);
969 if (!bus) {
970 return true;
971 }
972 return bus->tx_msg == NULL;
973}
974
975void *mctp_get_alloc_ctx(struct mctp *mctp)
976{
977 return mctp->alloc_ctx;
978}
979
980void mctp_set_alloc_ctx(struct mctp *mctp, void *ctx)
981{
982 mctp->alloc_ctx = ctx;
983}