blob: dd8376a2a636e80c80f7ad67850c0be42afccaf6 [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
22/* Internal data structures */
23
Andrew Jefferyc61501c2021-01-27 23:24:18 +103024enum mctp_bus_state {
25 mctp_bus_state_constructed = 0,
26 mctp_bus_state_tx_enabled,
27 mctp_bus_state_tx_disabled,
28};
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080029
Andrew Jefferyc61501c2021-01-27 23:24:18 +103030struct mctp_bus {
31 mctp_eid_t eid;
32 struct mctp_binding *binding;
33 enum mctp_bus_state state;
Matt Johnston4a09e1d2024-09-13 14:55:58 +080034 struct mctp *mctp;
Andrew Jefferyc61501c2021-01-27 23:24:18 +103035
Matt Johnston4a09e1d2024-09-13 14:55:58 +080036 /* Current message to transmit */
37 void *tx_msg;
38 /* Position in tx_msg */
39 size_t tx_msgpos;
40 /* Length of tx_msg */
41 size_t tx_msglen;
42 /* Length of current packet payload */
43 size_t tx_pktlen;
44 uint8_t tx_seq;
45 uint8_t tx_src;
46 uint8_t tx_dest;
47 bool tx_to;
48 uint8_t tx_tag;
Jeremy Kerrcc2458d2019-03-01 08:23:33 +080049
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080050 /* todo: routing */
51};
52
Jeremy Kerr24db71f2019-02-07 21:37:35 +080053struct mctp_msg_ctx {
Matt Johnston4a09e1d2024-09-13 14:55:58 +080054 /* NULL buf indicates an unused mctp_msg_ctx */
55 void *buf;
56
Patrick Williamsa721c2d2022-12-04 14:30:26 -060057 uint8_t src;
58 uint8_t dest;
59 uint8_t tag;
60 uint8_t last_seq;
Patrick Williamsa721c2d2022-12-04 14:30:26 -060061 size_t buf_size;
62 size_t buf_alloc_size;
63 size_t fragment_size;
Jeremy Kerr24db71f2019-02-07 21:37:35 +080064};
65
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080066struct mctp {
Patrick Williamsa721c2d2022-12-04 14:30:26 -060067 int n_busses;
68 struct mctp_bus *busses;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080069
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080070 /* Message RX callback */
Patrick Williamsa721c2d2022-12-04 14:30:26 -060071 mctp_rx_fn message_rx;
72 void *message_rx_data;
Jeremy Kerr24db71f2019-02-07 21:37:35 +080073
Andrew Jeffery5d3d4e62021-08-20 16:44:40 +093074 /* Packet capture callback */
Patrick Williamsa721c2d2022-12-04 14:30:26 -060075 mctp_capture_fn capture;
76 void *capture_data;
Andrew Jeffery5d3d4e62021-08-20 16:44:40 +093077
Jeremy Kerr24db71f2019-02-07 21:37:35 +080078 /* Message reassembly.
79 * @todo: flexible context count
80 */
Patrick Williamsa721c2d2022-12-04 14:30:26 -060081 struct mctp_msg_ctx msg_ctxs[16];
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +080082
83 enum {
84 ROUTE_ENDPOINT,
85 ROUTE_BRIDGE,
Patrick Williamsa721c2d2022-12-04 14:30:26 -060086 } route_policy;
Sumanth Bhat2c820c52020-07-02 00:26:25 +053087 size_t max_message_size;
Matt Johnston4a09e1d2024-09-13 14:55:58 +080088
89 void *alloc_ctx;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080090};
91
Jeremy Kerr24db71f2019-02-07 21:37:35 +080092#ifndef ARRAY_SIZE
93#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
94#endif
95
Sumanth Bhat2c820c52020-07-02 00:26:25 +053096/* 64kb should be sufficient for a single message. Applications
97 * requiring higher sizes can override by setting max_message_size.*/
98#ifndef MCTP_MAX_MESSAGE_SIZE
99#define MCTP_MAX_MESSAGE_SIZE 65536
100#endif
101
Andrew Jefferyb93b6112020-06-05 14:13:44 +0930102static int mctp_message_tx_on_bus(struct mctp_bus *bus, mctp_eid_t src,
Sumanth Bhatf39c3852022-01-10 17:04:10 +0530103 mctp_eid_t dest, bool tag_owner,
104 uint8_t msg_tag, void *msg, size_t msg_len);
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800105
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +0800106struct mctp_pktbuf *mctp_pktbuf_alloc(struct mctp_binding *binding, size_t len)
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800107{
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800108 size_t size =
109 binding->pkt_size + binding->pkt_header + binding->pkt_trailer;
Rashmica Gupta487b31e2022-09-14 18:49:45 +1000110 if (len > size) {
111 return NULL;
112 }
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800113
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800114 void *storage = __mctp_alloc(size + sizeof(struct mctp_pktbuf));
115 if (!storage) {
Pedro Martelletto2608b292023-03-30 13:28:28 +0000116 return NULL;
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800117 }
118 struct mctp_pktbuf *pkt = mctp_pktbuf_init(binding, storage);
119 pkt->alloc = true;
120 pkt->end = pkt->start + len;
121 return pkt;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800122}
123
124void mctp_pktbuf_free(struct mctp_pktbuf *pkt)
125{
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800126 if (pkt->alloc) {
127 __mctp_free(pkt);
128 } else {
129 mctp_prdebug("pktbuf_free called for non-alloced");
130 }
131}
132
133struct mctp_pktbuf *mctp_pktbuf_init(struct mctp_binding *binding,
134 void *storage)
135{
136 size_t size =
137 binding->pkt_size + binding->pkt_header + binding->pkt_trailer;
138 struct mctp_pktbuf *buf = (struct mctp_pktbuf *)storage;
139 buf->size = size;
140 buf->start = binding->pkt_header;
141 buf->end = buf->start;
142 buf->mctp_hdr_off = buf->start;
143 buf->alloc = false;
144
145 return buf;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800146}
147
148struct mctp_hdr *mctp_pktbuf_hdr(struct mctp_pktbuf *pkt)
149{
Moritz Fischer7aaccb52022-06-28 20:04:04 -0700150 return (struct mctp_hdr *)(pkt->data + pkt->mctp_hdr_off);
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800151}
152
153void *mctp_pktbuf_data(struct mctp_pktbuf *pkt)
154{
Moritz Fischer7aaccb52022-06-28 20:04:04 -0700155 return pkt->data + pkt->mctp_hdr_off + sizeof(struct mctp_hdr);
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800156}
157
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800158size_t mctp_pktbuf_size(const struct mctp_pktbuf *pkt)
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800159{
160 return pkt->end - pkt->start;
161}
162
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +0800163void *mctp_pktbuf_alloc_start(struct mctp_pktbuf *pkt, size_t size)
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800164{
165 assert(size <= pkt->start);
166 pkt->start -= size;
167 return pkt->data + pkt->start;
168}
169
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +0800170void *mctp_pktbuf_alloc_end(struct mctp_pktbuf *pkt, size_t size)
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800171{
172 void *buf;
173
Andrew Jeffery3ac70d62020-07-01 00:50:44 +0930174 assert(size <= (pkt->size - pkt->end));
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800175 buf = pkt->data + pkt->end;
176 pkt->end += size;
177 return buf;
178}
179
Matt Johnstondfbf0fd2024-10-28 14:40:29 +0800180int mctp_pktbuf_push(struct mctp_pktbuf *pkt, const void *data, size_t len)
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800181{
182 void *p;
183
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +0800184 if (pkt->end + len > pkt->size)
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800185 return -1;
186
187 p = pkt->data + pkt->end;
188
189 pkt->end += len;
190 memcpy(p, data, len);
191
192 return 0;
193}
194
Andrew Jefferyeba19a32021-03-09 23:09:40 +1030195void *mctp_pktbuf_pop(struct mctp_pktbuf *pkt, size_t len)
196{
197 if (len > mctp_pktbuf_size(pkt))
198 return NULL;
199
200 pkt->end -= len;
201 return pkt->data + pkt->end;
202}
203
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800204/* Allocate a duplicate of the message and copy it */
205static void *mctp_msg_dup(const void *msg, size_t msg_len, struct mctp *mctp)
206{
207 void *copy = __mctp_msg_alloc(msg_len, mctp);
208 if (!copy) {
209 mctp_prdebug("msg dup len %zu failed", msg_len);
210 return NULL;
211 }
212
213 memcpy(copy, msg, msg_len);
214 return copy;
215}
216
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800217/* Message reassembly */
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600218static struct mctp_msg_ctx *mctp_msg_ctx_lookup(struct mctp *mctp, uint8_t src,
219 uint8_t dest, uint8_t tag)
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800220{
221 unsigned int i;
222
223 /* @todo: better lookup, if we add support for more outstanding
224 * message contexts */
225 for (i = 0; i < ARRAY_SIZE(mctp->msg_ctxs); i++) {
226 struct mctp_msg_ctx *ctx = &mctp->msg_ctxs[i];
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800227 if (ctx->buf && ctx->src == src && ctx->dest == dest &&
228 ctx->tag == tag)
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800229 return ctx;
230 }
231
232 return NULL;
233}
234
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600235static struct mctp_msg_ctx *mctp_msg_ctx_create(struct mctp *mctp, uint8_t src,
236 uint8_t dest, uint8_t tag)
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800237{
Jeremy Kerr11a234e2019-02-27 17:59:53 +0800238 struct mctp_msg_ctx *ctx = NULL;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800239 unsigned int i;
240
241 for (i = 0; i < ARRAY_SIZE(mctp->msg_ctxs); i++) {
242 struct mctp_msg_ctx *tmp = &mctp->msg_ctxs[i];
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800243 if (!tmp->buf) {
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800244 ctx = tmp;
245 break;
246 }
247 }
248
249 if (!ctx)
250 return NULL;
251
252 ctx->src = src;
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800253 ctx->dest = dest;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800254 ctx->tag = tag;
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800255
Jeremy Kerr9a3da812019-08-02 15:57:53 +0800256 ctx->buf_size = 0;
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800257 ctx->buf_alloc_size = mctp->max_message_size;
258 ctx->buf = __mctp_msg_alloc(ctx->buf_alloc_size, mctp);
259 if (!ctx->buf) {
260 return NULL;
261 }
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800262
263 return ctx;
264}
265
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800266static void mctp_msg_ctx_drop(struct mctp_bus *bus, struct mctp_msg_ctx *ctx)
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800267{
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800268 /* Free and mark as unused */
269 __mctp_msg_free(ctx->buf, bus->mctp);
270 ctx->buf = NULL;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800271}
272
273static void mctp_msg_ctx_reset(struct mctp_msg_ctx *ctx)
274{
275 ctx->buf_size = 0;
Sumanth Bhat69f545f2021-05-18 09:16:43 +0000276 ctx->fragment_size = 0;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800277}
278
279static int mctp_msg_ctx_add_pkt(struct mctp_msg_ctx *ctx,
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800280 struct mctp_pktbuf *pkt)
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800281{
282 size_t len;
283
284 len = mctp_pktbuf_size(pkt) - sizeof(struct mctp_hdr);
285
Sumanth Bhatbc79c242021-06-16 12:36:56 +0530286 if (len + ctx->buf_size < ctx->buf_size) {
287 return -1;
288 }
289
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800290 if (ctx->buf_size + len > ctx->buf_alloc_size) {
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800291 return -1;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800292 }
293
Moritz Fischer7aaccb52022-06-28 20:04:04 -0700294 memcpy((uint8_t *)ctx->buf + ctx->buf_size, mctp_pktbuf_data(pkt), len);
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800295 ctx->buf_size += len;
296
297 return 0;
298}
299
300/* Core API functions */
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800301struct mctp *mctp_init(void)
302{
303 struct mctp *mctp;
304
305 mctp = __mctp_alloc(sizeof(*mctp));
Sumanth Bhat96d54492020-07-14 17:10:04 +0530306
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600307 if (!mctp)
Sumanth Bhat96d54492020-07-14 17:10:04 +0530308 return NULL;
309
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800310 memset(mctp, 0, sizeof(*mctp));
Sumanth Bhat2c820c52020-07-02 00:26:25 +0530311 mctp->max_message_size = MCTP_MAX_MESSAGE_SIZE;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800312
313 return mctp;
314}
315
Sumanth Bhat2c820c52020-07-02 00:26:25 +0530316void mctp_set_max_message_size(struct mctp *mctp, size_t message_size)
317{
318 mctp->max_message_size = message_size;
319}
320
Andrew Jeffery5d3d4e62021-08-20 16:44:40 +0930321void mctp_set_capture_handler(struct mctp *mctp, mctp_capture_fn fn, void *user)
322{
323 mctp->capture = fn;
324 mctp->capture_data = user;
325}
326
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800327static void mctp_bus_destroy(struct mctp_bus *bus, struct mctp *mctp)
Andrew Jeffery3ae89dc2021-01-28 15:24:36 +1030328{
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800329 if (bus->tx_msg) {
330 __mctp_msg_free(bus->tx_msg, mctp);
331 bus->tx_msg = NULL;
Andrew Jeffery3ae89dc2021-01-28 15:24:36 +1030332 }
333}
334
Andrew Jefferyfa56ca52020-03-10 23:18:22 +1030335void mctp_destroy(struct mctp *mctp)
336{
Andrew Jefferyb93b6112020-06-05 14:13:44 +0930337 size_t i;
Andrew Jefferyfa56ca52020-03-10 23:18:22 +1030338
339 /* Cleanup message assembly contexts */
Matt Johnston3ef47782024-12-11 15:19:06 +0800340 static_assert(ARRAY_SIZE(mctp->msg_ctxs) < SIZE_MAX, "size");
Andrew Jefferyfa56ca52020-03-10 23:18:22 +1030341 for (i = 0; i < ARRAY_SIZE(mctp->msg_ctxs); i++) {
342 struct mctp_msg_ctx *tmp = &mctp->msg_ctxs[i];
343 if (tmp->buf)
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800344 __mctp_msg_free(tmp->buf, mctp);
Andrew Jefferyfa56ca52020-03-10 23:18:22 +1030345 }
346
Andrew Jeffery3ae89dc2021-01-28 15:24:36 +1030347 while (mctp->n_busses--)
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800348 mctp_bus_destroy(&mctp->busses[mctp->n_busses], mctp);
Andrew Jeffery3ae89dc2021-01-28 15:24:36 +1030349
Andrew Jefferyfa56ca52020-03-10 23:18:22 +1030350 __mctp_free(mctp->busses);
351 __mctp_free(mctp);
352}
353
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800354int mctp_set_rx_all(struct mctp *mctp, mctp_rx_fn fn, void *data)
355{
356 mctp->message_rx = fn;
357 mctp->message_rx_data = data;
358 return 0;
359}
360
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600361static struct mctp_bus *find_bus_for_eid(struct mctp *mctp, mctp_eid_t dest
362 __attribute__((unused)))
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800363{
Brad Bishop663ec392021-10-07 21:16:48 -0400364 if (mctp->n_busses == 0)
365 return NULL;
366
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800367 /* for now, just use the first bus. For full routing support,
368 * we will need a table of neighbours */
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800369 return &mctp->busses[0];
370}
371
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600372int mctp_register_bus(struct mctp *mctp, struct mctp_binding *binding,
373 mctp_eid_t eid)
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800374{
Andrew Jeffery3e8a12a2020-06-05 16:08:30 +0930375 int rc = 0;
376
Jeremy Kerr7520cec2019-03-01 07:13:18 +0800377 /* todo: multiple busses */
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800378 assert(mctp->n_busses == 0);
379 mctp->n_busses = 1;
Andrew Jeffery3e8a12a2020-06-05 16:08:30 +0930380
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800381 assert(binding->tx_storage);
382
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800383 mctp->busses = __mctp_alloc(sizeof(struct mctp_bus));
Andrew Jeffery3e8a12a2020-06-05 16:08:30 +0930384 if (!mctp->busses)
385 return -ENOMEM;
386
James Feist62d72362019-12-13 13:43:32 -0800387 memset(mctp->busses, 0, sizeof(struct mctp_bus));
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800388 mctp->busses[0].mctp = mctp;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800389 mctp->busses[0].binding = binding;
390 mctp->busses[0].eid = eid;
Jeremy Kerr7520cec2019-03-01 07:13:18 +0800391 binding->bus = &mctp->busses[0];
Jeremy Kerr0a00dca2019-03-01 08:01:35 +0800392 binding->mctp = mctp;
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800393 mctp->route_policy = ROUTE_ENDPOINT;
Jeremy Kerr3b36d172019-09-04 11:56:09 +0800394
Andrew Jeffery3e8a12a2020-06-05 16:08:30 +0930395 if (binding->start) {
396 rc = binding->start(binding);
397 if (rc < 0) {
398 mctp_prerr("Failed to start binding: %d", rc);
Andrew Jeffery19275232021-01-29 14:13:25 +1030399 binding->bus = NULL;
Andrew Jeffery3e8a12a2020-06-05 16:08:30 +0930400 __mctp_free(mctp->busses);
401 mctp->busses = NULL;
Andrew Jeffery2304c832021-01-29 11:52:49 +1030402 mctp->n_busses = 0;
Andrew Jeffery3e8a12a2020-06-05 16:08:30 +0930403 }
404 }
Jeremy Kerr3b36d172019-09-04 11:56:09 +0800405
Andrew Jeffery3e8a12a2020-06-05 16:08:30 +0930406 return rc;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800407}
408
Andrew Jeffery2094c3c2021-08-26 12:32:46 +0930409void mctp_unregister_bus(struct mctp *mctp, struct mctp_binding *binding)
410{
411 /*
412 * We only support one bus right now; once the call completes we will
413 * have no more busses
414 */
415 mctp->n_busses = 0;
416 binding->mctp = NULL;
417 binding->bus = NULL;
Benjamin Gwinb7824b62023-08-08 18:44:53 +0000418 __mctp_free(mctp->busses);
Andrew Jeffery2094c3c2021-08-26 12:32:46 +0930419}
420
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600421int mctp_bridge_busses(struct mctp *mctp, struct mctp_binding *b1,
422 struct mctp_binding *b2)
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800423{
Andrew Jeffery19275232021-01-29 14:13:25 +1030424 int rc = 0;
425
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800426 assert(b1->tx_storage);
427 assert(b2->tx_storage);
428
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800429 assert(mctp->n_busses == 0);
430 mctp->busses = __mctp_alloc(2 * sizeof(struct mctp_bus));
Helen Huanga523bcc2021-05-19 15:44:56 +0800431 if (!mctp->busses)
432 return -ENOMEM;
James Feist62d72362019-12-13 13:43:32 -0800433 memset(mctp->busses, 0, 2 * sizeof(struct mctp_bus));
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800434 mctp->n_busses = 2;
435 mctp->busses[0].binding = b1;
436 b1->bus = &mctp->busses[0];
437 b1->mctp = mctp;
438 mctp->busses[1].binding = b2;
439 b2->bus = &mctp->busses[1];
440 b2->mctp = mctp;
441
442 mctp->route_policy = ROUTE_BRIDGE;
Jeremy Kerr3b36d172019-09-04 11:56:09 +0800443
Andrew Jeffery19275232021-01-29 14:13:25 +1030444 if (b1->start) {
445 rc = b1->start(b1);
446 if (rc < 0) {
447 mctp_prerr("Failed to start bridged bus %s: %d",
448 b1->name, rc);
449 goto done;
450 }
451 }
Jeremy Kerr3b36d172019-09-04 11:56:09 +0800452
Andrew Jeffery19275232021-01-29 14:13:25 +1030453 if (b2->start) {
454 rc = b2->start(b2);
455 if (rc < 0) {
456 mctp_prerr("Failed to start bridged bus %s: %d",
457 b2->name, rc);
458 goto done;
459 }
460 }
Jeremy Kerr3b36d172019-09-04 11:56:09 +0800461
Andrew Jeffery19275232021-01-29 14:13:25 +1030462done:
463 return rc;
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800464}
465
Wiktor GoĊ‚gowskiba6727e2020-03-13 18:25:01 +0100466static inline bool mctp_ctrl_cmd_is_transport(struct mctp_ctrl_msg_hdr *hdr)
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800467{
Matt Johnston3ef47782024-12-11 15:19:06 +0800468#pragma GCC diagnostic push
469#pragma GCC diagnostic ignored "-Wtype-limits"
Wiktor GoĊ‚gowskiba6727e2020-03-13 18:25:01 +0100470 return ((hdr->command_code >= MCTP_CTRL_CMD_FIRST_TRANSPORT) &&
471 (hdr->command_code <= MCTP_CTRL_CMD_LAST_TRANSPORT));
Matt Johnston3ef47782024-12-11 15:19:06 +0800472#pragma GCC diagnostic pop
Wiktor GoĊ‚gowskiba6727e2020-03-13 18:25:01 +0100473}
474
Andrew Jefferyb93b6112020-06-05 14:13:44 +0930475static bool mctp_ctrl_handle_msg(struct mctp_bus *bus, mctp_eid_t src,
Sumanth Bhatf39c3852022-01-10 17:04:10 +0530476 uint8_t msg_tag, bool tag_owner, void *buffer,
477 size_t length)
Wiktor GoĊ‚gowskiba6727e2020-03-13 18:25:01 +0100478{
479 struct mctp_ctrl_msg_hdr *msg_hdr = buffer;
480
481 /*
482 * Control message is received. If a transport control message handler
483 * is provided, it will called. If there is no dedicated handler, this
484 * function returns false and data can be handled by the generic
485 * message handler. The transport control message handler will be
486 * provided with messages in the command range 0xF0 - 0xFF.
487 */
488 if (mctp_ctrl_cmd_is_transport(msg_hdr)) {
489 if (bus->binding->control_rx != NULL) {
490 /* MCTP bus binding handler */
Sumanth Bhatf39c3852022-01-10 17:04:10 +0530491 bus->binding->control_rx(src, msg_tag, tag_owner,
Wiktor GoĊ‚gowskiba6727e2020-03-13 18:25:01 +0100492 bus->binding->control_rx_data,
493 buffer, length);
494 return true;
495 }
496 }
497
498 /*
499 * Command was not handled, due to lack of specific callback.
500 * It will be passed to regular message_rx handler.
501 */
502 return false;
503}
504
505static inline bool mctp_rx_dest_is_local(struct mctp_bus *bus, mctp_eid_t dest)
506{
507 return dest == bus->eid || dest == MCTP_EID_NULL ||
508 dest == MCTP_EID_BROADCAST;
509}
510
511static inline bool mctp_ctrl_cmd_is_request(struct mctp_ctrl_msg_hdr *hdr)
512{
513 return hdr->ic_msg_type == MCTP_CTRL_HDR_MSG_TYPE &&
514 hdr->rq_dgram_inst & MCTP_CTRL_HDR_FLAG_REQUEST;
515}
516
517/*
518 * Receive the complete MCTP message and route it.
519 * Asserts:
520 * 'buf' is not NULL.
521 */
522static void mctp_rx(struct mctp *mctp, struct mctp_bus *bus, mctp_eid_t src,
Sumanth Bhatf39c3852022-01-10 17:04:10 +0530523 mctp_eid_t dest, bool tag_owner, uint8_t msg_tag, void *buf,
524 size_t len)
Wiktor GoĊ‚gowskiba6727e2020-03-13 18:25:01 +0100525{
526 assert(buf != NULL);
527
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800528 if (mctp->route_policy == ROUTE_ENDPOINT &&
Wiktor GoĊ‚gowskiba6727e2020-03-13 18:25:01 +0100529 mctp_rx_dest_is_local(bus, dest)) {
530 /* Handle MCTP Control Messages: */
531 if (len >= sizeof(struct mctp_ctrl_msg_hdr)) {
532 struct mctp_ctrl_msg_hdr *msg_hdr = buf;
533
534 /*
535 * Identify if this is a control request message.
536 * See DSP0236 v1.3.0 sec. 11.5.
537 */
538 if (mctp_ctrl_cmd_is_request(msg_hdr)) {
539 bool handled;
Sumanth Bhatf39c3852022-01-10 17:04:10 +0530540 handled = mctp_ctrl_handle_msg(
541 bus, src, msg_tag, tag_owner, buf, len);
Wiktor GoĊ‚gowskiba6727e2020-03-13 18:25:01 +0100542 if (handled)
543 return;
544 }
545 }
Sumanth Bhatf39c3852022-01-10 17:04:10 +0530546
Wiktor GoĊ‚gowskiba6727e2020-03-13 18:25:01 +0100547 if (mctp->message_rx)
Sumanth Bhatf39c3852022-01-10 17:04:10 +0530548 mctp->message_rx(src, tag_owner, msg_tag,
549 mctp->message_rx_data, buf, len);
Wiktor GoĊ‚gowskiba6727e2020-03-13 18:25:01 +0100550 }
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800551
552 if (mctp->route_policy == ROUTE_BRIDGE) {
553 int i;
554
555 for (i = 0; i < mctp->n_busses; i++) {
556 struct mctp_bus *dest_bus = &mctp->busses[i];
557 if (dest_bus == bus)
558 continue;
559
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800560 void *copy = mctp_msg_dup(buf, len, mctp);
561 if (!copy) {
562 return;
563 }
564
Sumanth Bhatf39c3852022-01-10 17:04:10 +0530565 mctp_message_tx_on_bus(dest_bus, src, dest, tag_owner,
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800566 msg_tag, copy, len);
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800567 }
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800568 }
569}
570
Jeremy Kerr0a00dca2019-03-01 08:01:35 +0800571void mctp_bus_rx(struct mctp_binding *binding, struct mctp_pktbuf *pkt)
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800572{
Jeremy Kerr7520cec2019-03-01 07:13:18 +0800573 struct mctp_bus *bus = binding->bus;
Jeremy Kerr0a00dca2019-03-01 08:01:35 +0800574 struct mctp *mctp = binding->mctp;
Ed Tanousc2def9f2019-02-21 08:33:08 -0800575 uint8_t flags, exp_seq, seq, tag;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800576 struct mctp_msg_ctx *ctx;
577 struct mctp_hdr *hdr;
Sumanth Bhatf39c3852022-01-10 17:04:10 +0530578 bool tag_owner;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800579 size_t len;
580 void *p;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800581 int rc;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800582
Jeremy Kerr7520cec2019-03-01 07:13:18 +0800583 assert(bus);
584
Sumanth Bhatd97869d2020-07-02 00:46:13 +0530585 /* Drop packet if it was smaller than mctp hdr size */
Matt Johnston86e9a972024-10-28 15:06:33 +0800586 if (mctp_pktbuf_size(pkt) < sizeof(struct mctp_hdr))
Sumanth Bhatd97869d2020-07-02 00:46:13 +0530587 goto out;
588
Andrew Jeffery5d3d4e62021-08-20 16:44:40 +0930589 if (mctp->capture)
Rashmica Guptaf2988972022-11-09 12:26:44 +1100590 mctp->capture(pkt, MCTP_MESSAGE_CAPTURE_INCOMING,
591 mctp->capture_data);
Andrew Jeffery5d3d4e62021-08-20 16:44:40 +0930592
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800593 hdr = mctp_pktbuf_hdr(pkt);
594
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800595 /* small optimisation: don't bother reassembly if we're going to
596 * drop the packet in mctp_rx anyway */
John Chung133df7a2024-05-14 16:19:56 +0800597 if (mctp->route_policy == ROUTE_ENDPOINT &&
598 !mctp_rx_dest_is_local(bus, hdr->dest))
Jeremy Kerrc1693af2019-08-05 14:30:59 +0800599 goto out;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800600
601 flags = hdr->flags_seq_tag & (MCTP_HDR_FLAG_SOM | MCTP_HDR_FLAG_EOM);
602 tag = (hdr->flags_seq_tag >> MCTP_HDR_TAG_SHIFT) & MCTP_HDR_TAG_MASK;
603 seq = (hdr->flags_seq_tag >> MCTP_HDR_SEQ_SHIFT) & MCTP_HDR_SEQ_MASK;
Andrew Jeffery7f7fdc12023-05-12 15:56:47 +0930604 tag_owner = (hdr->flags_seq_tag >> MCTP_HDR_TO_SHIFT) &
605 MCTP_HDR_TO_MASK;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800606
607 switch (flags) {
608 case MCTP_HDR_FLAG_SOM | MCTP_HDR_FLAG_EOM:
609 /* single-packet message - send straight up to rx function,
610 * no need to create a message context */
611 len = pkt->end - pkt->mctp_hdr_off - sizeof(struct mctp_hdr);
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800612 p = mctp_msg_dup(pkt->data + pkt->mctp_hdr_off +
613 sizeof(struct mctp_hdr),
614 len, mctp);
615 if (p) {
616 mctp_rx(mctp, bus, hdr->src, hdr->dest, tag_owner, tag,
617 p, len);
618 __mctp_msg_free(p, mctp);
619 }
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800620 break;
621
622 case MCTP_HDR_FLAG_SOM:
623 /* start of a new message - start the new context for
624 * future message reception. If an existing context is
625 * already present, drop it. */
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800626 ctx = mctp_msg_ctx_lookup(mctp, hdr->src, hdr->dest, tag);
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800627 if (ctx) {
628 mctp_msg_ctx_reset(ctx);
629 } else {
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600630 ctx = mctp_msg_ctx_create(mctp, hdr->src, hdr->dest,
631 tag);
Sumanth Bhat34d4c962021-06-16 12:50:48 +0530632 /* If context creation fails due to exhaution of contexts we
633 * can support, drop the packet */
634 if (!ctx) {
635 mctp_prdebug("Context buffers exhausted.");
636 goto out;
637 }
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800638 }
639
Sumanth Bhat69f545f2021-05-18 09:16:43 +0000640 /* Save the fragment size, subsequent middle fragments
641 * should of the same size */
642 ctx->fragment_size = mctp_pktbuf_size(pkt);
643
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800644 rc = mctp_msg_ctx_add_pkt(ctx, pkt);
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800645 if (rc) {
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800646 mctp_msg_ctx_drop(bus, ctx);
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800647 } else {
648 ctx->last_seq = seq;
649 }
650
651 break;
652
653 case MCTP_HDR_FLAG_EOM:
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800654 ctx = mctp_msg_ctx_lookup(mctp, hdr->src, hdr->dest, tag);
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800655 if (!ctx)
Jeremy Kerrc1693af2019-08-05 14:30:59 +0800656 goto out;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800657
Ed Tanousc2def9f2019-02-21 08:33:08 -0800658 exp_seq = (ctx->last_seq + 1) % 4;
659
660 if (exp_seq != seq) {
661 mctp_prdebug(
662 "Sequence number %d does not match expected %d",
663 seq, exp_seq);
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800664 mctp_msg_ctx_drop(bus, ctx);
Jeremy Kerrc1693af2019-08-05 14:30:59 +0800665 goto out;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800666 }
667
Sumanth Bhat69f545f2021-05-18 09:16:43 +0000668 len = mctp_pktbuf_size(pkt);
669
670 if (len > ctx->fragment_size) {
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600671 mctp_prdebug("Unexpected fragment size. Expected"
672 " less than %zu, received = %zu",
673 ctx->fragment_size, len);
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800674 mctp_msg_ctx_drop(bus, ctx);
Sumanth Bhat69f545f2021-05-18 09:16:43 +0000675 goto out;
676 }
677
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800678 rc = mctp_msg_ctx_add_pkt(ctx, pkt);
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800679 if (!rc)
Sumanth Bhatf39c3852022-01-10 17:04:10 +0530680 mctp_rx(mctp, bus, ctx->src, ctx->dest, tag_owner, tag,
681 ctx->buf, ctx->buf_size);
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800682
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800683 mctp_msg_ctx_drop(bus, ctx);
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800684 break;
Ed Tanousc2def9f2019-02-21 08:33:08 -0800685
686 case 0:
687 /* Neither SOM nor EOM */
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600688 ctx = mctp_msg_ctx_lookup(mctp, hdr->src, hdr->dest, tag);
Ed Tanousc2def9f2019-02-21 08:33:08 -0800689 if (!ctx)
Jeremy Kerrc1693af2019-08-05 14:30:59 +0800690 goto out;
Ed Tanousc2def9f2019-02-21 08:33:08 -0800691
692 exp_seq = (ctx->last_seq + 1) % 4;
693 if (exp_seq != seq) {
694 mctp_prdebug(
695 "Sequence number %d does not match expected %d",
696 seq, exp_seq);
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
Sumanth Bhat69f545f2021-05-18 09:16:43 +0000701 len = mctp_pktbuf_size(pkt);
702
703 if (len != ctx->fragment_size) {
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600704 mctp_prdebug("Unexpected fragment size. Expected = %zu "
705 "received = %zu",
706 ctx->fragment_size, len);
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800707 mctp_msg_ctx_drop(bus, ctx);
Sumanth Bhat69f545f2021-05-18 09:16:43 +0000708 goto out;
709 }
710
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800711 rc = mctp_msg_ctx_add_pkt(ctx, pkt);
Ed Tanousc2def9f2019-02-21 08:33:08 -0800712 if (rc) {
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800713 mctp_msg_ctx_drop(bus, ctx);
Jeremy Kerrc1693af2019-08-05 14:30:59 +0800714 goto out;
Ed Tanousc2def9f2019-02-21 08:33:08 -0800715 }
716 ctx->last_seq = seq;
717
718 break;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800719 }
Jeremy Kerrc1693af2019-08-05 14:30:59 +0800720out:
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800721 return;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800722}
723
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600724static int mctp_packet_tx(struct mctp_bus *bus, struct mctp_pktbuf *pkt)
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800725{
Andrew Jeffery5d3d4e62021-08-20 16:44:40 +0930726 struct mctp *mctp = bus->binding->mctp;
727
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800728 if (bus->state != mctp_bus_state_tx_enabled) {
729 mctp_prdebug("tx with bus disabled");
Jeremy Kerr1cd31182019-02-27 18:01:00 +0800730 return -1;
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800731 }
Jeremy Kerr1cd31182019-02-27 18:01:00 +0800732
Andrew Jeffery5d3d4e62021-08-20 16:44:40 +0930733 if (mctp->capture)
Rashmica Guptaf2988972022-11-09 12:26:44 +1100734 mctp->capture(pkt, MCTP_MESSAGE_CAPTURE_OUTGOING,
735 mctp->capture_data);
Andrew Jeffery5d3d4e62021-08-20 16:44:40 +0930736
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800737 return bus->binding->tx(bus->binding, pkt);
738}
739
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800740/* Returns a pointer to the binding's tx_storage */
741static struct mctp_pktbuf *mctp_next_tx_pkt(struct mctp_bus *bus)
742{
743 if (!bus->tx_msg) {
744 return NULL;
745 }
746
747 size_t p = bus->tx_msgpos;
748 size_t msg_len = bus->tx_msglen;
749 size_t payload_len = msg_len - p;
750 size_t max_payload_len = MCTP_BODY_SIZE(bus->binding->pkt_size);
751 if (payload_len > max_payload_len)
752 payload_len = max_payload_len;
753
754 struct mctp_pktbuf *pkt =
755 mctp_pktbuf_init(bus->binding, bus->binding->tx_storage);
756 struct mctp_hdr *hdr = mctp_pktbuf_hdr(pkt);
757
758 hdr->ver = bus->binding->version & 0xf;
759 hdr->dest = bus->tx_dest;
760 hdr->src = bus->tx_src;
761 hdr->flags_seq_tag = (bus->tx_to << MCTP_HDR_TO_SHIFT) |
762 (bus->tx_tag << MCTP_HDR_TAG_SHIFT);
763
764 if (p == 0)
765 hdr->flags_seq_tag |= MCTP_HDR_FLAG_SOM;
766 if (p + payload_len >= msg_len)
767 hdr->flags_seq_tag |= MCTP_HDR_FLAG_EOM;
768 hdr->flags_seq_tag |= bus->tx_seq << MCTP_HDR_SEQ_SHIFT;
769
770 memcpy(mctp_pktbuf_data(pkt), (uint8_t *)bus->tx_msg + p, payload_len);
771 pkt->end = pkt->start + sizeof(*hdr) + payload_len;
772 bus->tx_pktlen = payload_len;
773
774 mctp_prdebug(
775 "tx dst %d tag %d payload len %zu seq %d. msg pos %zu len %zu",
776 hdr->dest, bus->tx_tag, payload_len, bus->tx_seq, p, msg_len);
777
778 return pkt;
779}
780
781/* Called when a packet has successfully been sent */
782static void mctp_tx_complete(struct mctp_bus *bus)
783{
784 if (!bus->tx_msg) {
785 mctp_prdebug("tx complete no message");
786 return;
787 }
788
789 bus->tx_seq = (bus->tx_seq + 1) & MCTP_HDR_SEQ_MASK;
790 bus->tx_msgpos += bus->tx_pktlen;
791
792 if (bus->tx_msgpos >= bus->tx_msglen) {
793 __mctp_msg_free(bus->tx_msg, bus->binding->mctp);
794 bus->tx_msg = NULL;
795 }
796}
797
Jeremy Kerrcc2458d2019-03-01 08:23:33 +0800798static void mctp_send_tx_queue(struct mctp_bus *bus)
Jeremy Kerr1cd31182019-02-27 18:01:00 +0800799{
800 struct mctp_pktbuf *pkt;
801
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800802 while (bus->tx_msg && bus->state == mctp_bus_state_tx_enabled) {
Jeremy Kerr1cd31182019-02-27 18:01:00 +0800803 int rc;
804
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800805 pkt = mctp_next_tx_pkt(bus);
806
Jeremy Kerr1cd31182019-02-27 18:01:00 +0800807 rc = mctp_packet_tx(bus, pkt);
Andrew Jeffery0721f582022-09-29 12:12:39 +0930808 switch (rc) {
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800809 /* If transmission succeded */
Andrew Jeffery0721f582022-09-29 12:12:39 +0930810 case 0:
Andrew Jeffery0721f582022-09-29 12:12:39 +0930811 /* Drop the packet */
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800812 mctp_tx_complete(bus);
Jeremy Kerr1cd31182019-02-27 18:01:00 +0800813 break;
814
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800815 /* If the binding was busy */
Andrew Jeffery0721f582022-09-29 12:12:39 +0930816 case -EBUSY:
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800817 /* Keep the packet for next try */
818 mctp_prdebug("tx EBUSY");
819 return;
820
Andrew Jeffery0721f582022-09-29 12:12:39 +0930821 /* Some other unknown error occurred */
822 default:
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800823 /* Drop the packet */
824 mctp_prdebug("tx drop %d", rc);
825 mctp_tx_complete(bus);
826 return;
Andrew Jeffery0721f582022-09-29 12:12:39 +0930827 };
Jeremy Kerr1cd31182019-02-27 18:01:00 +0800828 }
Jeremy Kerr1cd31182019-02-27 18:01:00 +0800829}
830
831void mctp_binding_set_tx_enabled(struct mctp_binding *binding, bool enable)
832{
833 struct mctp_bus *bus = binding->bus;
Andrew Jefferyc61501c2021-01-27 23:24:18 +1030834
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600835 switch (bus->state) {
Andrew Jefferyc61501c2021-01-27 23:24:18 +1030836 case mctp_bus_state_constructed:
837 if (!enable)
838 return;
839
Andrew Jeffery1fa707e2021-01-28 15:22:11 +1030840 if (binding->pkt_size < MCTP_PACKET_SIZE(MCTP_BTU)) {
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600841 mctp_prerr(
842 "Cannot start %s binding with invalid MTU: %zu",
843 binding->name,
844 MCTP_BODY_SIZE(binding->pkt_size));
Andrew Jeffery1fa707e2021-01-28 15:22:11 +1030845 return;
846 }
847
Andrew Jefferyc61501c2021-01-27 23:24:18 +1030848 bus->state = mctp_bus_state_tx_enabled;
849 mctp_prinfo("%s binding started", binding->name);
850 return;
851 case mctp_bus_state_tx_enabled:
852 if (enable)
853 return;
854
855 bus->state = mctp_bus_state_tx_disabled;
856 mctp_prdebug("%s binding Tx disabled", binding->name);
857 return;
858 case mctp_bus_state_tx_disabled:
859 if (!enable)
860 return;
861
862 bus->state = mctp_bus_state_tx_enabled;
863 mctp_prdebug("%s binding Tx enabled", binding->name);
Jeremy Kerrcc2458d2019-03-01 08:23:33 +0800864 mctp_send_tx_queue(bus);
Andrew Jefferyc61501c2021-01-27 23:24:18 +1030865 return;
866 }
Jeremy Kerr1cd31182019-02-27 18:01:00 +0800867}
868
Andrew Jefferyb93b6112020-06-05 14:13:44 +0930869static int mctp_message_tx_on_bus(struct mctp_bus *bus, mctp_eid_t src,
Sumanth Bhatf39c3852022-01-10 17:04:10 +0530870 mctp_eid_t dest, bool tag_owner,
871 uint8_t msg_tag, void *msg, size_t msg_len)
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800872{
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800873 size_t max_payload_len;
874 int rc;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800875
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800876 if (bus->state == mctp_bus_state_constructed) {
877 rc = -ENXIO;
878 goto err;
879 }
Andrew Jefferyc61501c2021-01-27 23:24:18 +1030880
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800881 if ((msg_tag & MCTP_HDR_TAG_MASK) != msg_tag) {
882 rc = -EINVAL;
883 goto err;
884 }
Sumanth Bhatf39c3852022-01-10 17:04:10 +0530885
Andrew Jeffery1fa707e2021-01-28 15:22:11 +1030886 max_payload_len = MCTP_BODY_SIZE(bus->binding->pkt_size);
887
888 {
889 const bool valid_mtu = max_payload_len >= MCTP_BTU;
890 assert(valid_mtu);
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800891 if (!valid_mtu) {
892 rc = -EINVAL;
893 goto err;
894 }
Andrew Jeffery1fa707e2021-01-28 15:22:11 +1030895 }
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800896
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600897 mctp_prdebug(
898 "%s: Generating packets for transmission of %zu byte message from %hhu to %hhu",
899 __func__, msg_len, src, dest);
Andrew Jeffery298865f2020-02-06 11:51:29 +1030900
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800901 if (bus->tx_msg) {
902 mctp_prdebug("Bus busy");
903 rc = -EBUSY;
904 goto err;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800905 }
906
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800907 /* Take the message to send */
908 bus->tx_msg = msg;
909 bus->tx_msglen = msg_len;
910 bus->tx_msgpos = 0;
911 /* bus->tx_seq is allowed to continue from previous message */
912 bus->tx_src = src;
913 bus->tx_dest = dest;
914 bus->tx_to = tag_owner;
915 bus->tx_tag = msg_tag;
Andrew Jeffery298865f2020-02-06 11:51:29 +1030916
Jeremy Kerrcc2458d2019-03-01 08:23:33 +0800917 mctp_send_tx_queue(bus);
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800918 return 0;
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800919
920err:
921 __mctp_msg_free(msg, bus->binding->mctp);
922 return rc;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800923}
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800924
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800925int mctp_message_tx_alloced(struct mctp *mctp, mctp_eid_t eid, bool tag_owner,
926 uint8_t msg_tag, void *msg, size_t msg_len)
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800927{
928 struct mctp_bus *bus;
929
Sumanth Bhatf39c3852022-01-10 17:04:10 +0530930 /* TODO: Protect against same tag being used across
931 * different callers */
932 if ((msg_tag & MCTP_HDR_TAG_MASK) != msg_tag) {
933 mctp_prerr("Incorrect message tag %u passed.", msg_tag);
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800934 __mctp_msg_free(msg, mctp);
Sumanth Bhatf39c3852022-01-10 17:04:10 +0530935 return -EINVAL;
936 }
937
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800938 bus = find_bus_for_eid(mctp, eid);
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800939 if (!bus) {
940 __mctp_msg_free(msg, mctp);
Brad Bishop663ec392021-10-07 21:16:48 -0400941 return 0;
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800942 }
Brad Bishop663ec392021-10-07 21:16:48 -0400943
Sumanth Bhatf39c3852022-01-10 17:04:10 +0530944 return mctp_message_tx_on_bus(bus, bus->eid, eid, tag_owner, msg_tag,
945 msg, msg_len);
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800946}
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800947
948int mctp_message_tx(struct mctp *mctp, mctp_eid_t eid, bool tag_owner,
949 uint8_t msg_tag, const void *msg, size_t msg_len)
950{
951 void *copy = mctp_msg_dup(msg, msg_len, mctp);
952 if (!copy) {
953 return -ENOMEM;
954 }
955
956 return mctp_message_tx_alloced(mctp, eid, tag_owner, msg_tag, copy,
957 msg_len);
958}
959
960bool mctp_is_tx_ready(struct mctp *mctp, mctp_eid_t eid)
961{
962 struct mctp_bus *bus;
963
964 bus = find_bus_for_eid(mctp, eid);
965 if (!bus) {
966 return true;
967 }
968 return bus->tx_msg == NULL;
969}
970
971void *mctp_get_alloc_ctx(struct mctp *mctp)
972{
973 return mctp->alloc_ctx;
974}
975
976void mctp_set_alloc_ctx(struct mctp *mctp, void *ctx)
977{
978 mctp->alloc_ctx = ctx;
979}