blob: 33f509388fd7177aa32fda50da3fcb8e5b5f0f26 [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"
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080020
21/* Internal data structures */
22
Andrew Jefferyc61501c2021-01-27 23:24:18 +103023enum mctp_bus_state {
24 mctp_bus_state_constructed = 0,
25 mctp_bus_state_tx_enabled,
26 mctp_bus_state_tx_disabled,
27};
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080028
Andrew Jefferyc61501c2021-01-27 23:24:18 +103029struct mctp_bus {
30 mctp_eid_t eid;
31 struct mctp_binding *binding;
32 enum mctp_bus_state state;
33
34 struct mctp_pktbuf *tx_queue_head;
35 struct mctp_pktbuf *tx_queue_tail;
Jeremy Kerrcc2458d2019-03-01 08:23:33 +080036
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080037 /* todo: routing */
38};
39
Jeremy Kerr24db71f2019-02-07 21:37:35 +080040struct mctp_msg_ctx {
Patrick Williamsa721c2d2022-12-04 14:30:26 -060041 uint8_t src;
42 uint8_t dest;
43 uint8_t tag;
44 uint8_t last_seq;
45 void *buf;
46 size_t buf_size;
47 size_t buf_alloc_size;
48 size_t fragment_size;
Jeremy Kerr24db71f2019-02-07 21:37:35 +080049};
50
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080051struct mctp {
Patrick Williamsa721c2d2022-12-04 14:30:26 -060052 int n_busses;
53 struct mctp_bus *busses;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080054
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080055 /* Message RX callback */
Patrick Williamsa721c2d2022-12-04 14:30:26 -060056 mctp_rx_fn message_rx;
57 void *message_rx_data;
Jeremy Kerr24db71f2019-02-07 21:37:35 +080058
Andrew Jeffery5d3d4e62021-08-20 16:44:40 +093059 /* Packet capture callback */
Patrick Williamsa721c2d2022-12-04 14:30:26 -060060 mctp_capture_fn capture;
61 void *capture_data;
Andrew Jeffery5d3d4e62021-08-20 16:44:40 +093062
Jeremy Kerr24db71f2019-02-07 21:37:35 +080063 /* Message reassembly.
64 * @todo: flexible context count
65 */
Patrick Williamsa721c2d2022-12-04 14:30:26 -060066 struct mctp_msg_ctx msg_ctxs[16];
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +080067
68 enum {
69 ROUTE_ENDPOINT,
70 ROUTE_BRIDGE,
Patrick Williamsa721c2d2022-12-04 14:30:26 -060071 } route_policy;
Sumanth Bhat2c820c52020-07-02 00:26:25 +053072 size_t max_message_size;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080073};
74
Jeremy Kerr24db71f2019-02-07 21:37:35 +080075#ifndef ARRAY_SIZE
76#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
77#endif
78
Sumanth Bhat2c820c52020-07-02 00:26:25 +053079/* 64kb should be sufficient for a single message. Applications
80 * requiring higher sizes can override by setting max_message_size.*/
81#ifndef MCTP_MAX_MESSAGE_SIZE
82#define MCTP_MAX_MESSAGE_SIZE 65536
83#endif
84
Andrew Jefferyb93b6112020-06-05 14:13:44 +093085static int mctp_message_tx_on_bus(struct mctp_bus *bus, mctp_eid_t src,
Sumanth Bhatf39c3852022-01-10 17:04:10 +053086 mctp_eid_t dest, bool tag_owner,
87 uint8_t msg_tag, void *msg, size_t msg_len);
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +080088
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +080089struct mctp_pktbuf *mctp_pktbuf_alloc(struct mctp_binding *binding, size_t len)
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080090{
91 struct mctp_pktbuf *buf;
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +080092 size_t size;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080093
Andrew Jeffery39da3d02021-03-12 16:51:26 +103094 size = binding->pkt_size + binding->pkt_header + binding->pkt_trailer;
Rashmica Gupta487b31e2022-09-14 18:49:45 +100095 if (len > size) {
96 return NULL;
97 }
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080098
99 /* todo: pools */
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +0800100 buf = __mctp_alloc(sizeof(*buf) + size);
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800101
Pedro Martelletto2608b292023-03-30 13:28:28 +0000102 if (!buf)
103 return NULL;
104
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +0800105 buf->size = size;
Andrew Jeffery39da3d02021-03-12 16:51:26 +1030106 buf->start = binding->pkt_header;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800107 buf->end = buf->start + len;
108 buf->mctp_hdr_off = buf->start;
Jeremy Kerrdd109f12019-04-04 11:46:49 +0800109 buf->next = NULL;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800110
111 return buf;
112}
113
114void mctp_pktbuf_free(struct mctp_pktbuf *pkt)
115{
116 __mctp_free(pkt);
117}
118
119struct mctp_hdr *mctp_pktbuf_hdr(struct mctp_pktbuf *pkt)
120{
Moritz Fischer7aaccb52022-06-28 20:04:04 -0700121 return (struct mctp_hdr *)(pkt->data + pkt->mctp_hdr_off);
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800122}
123
124void *mctp_pktbuf_data(struct mctp_pktbuf *pkt)
125{
Moritz Fischer7aaccb52022-06-28 20:04:04 -0700126 return pkt->data + pkt->mctp_hdr_off + sizeof(struct mctp_hdr);
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800127}
128
Andrew Jefferyb942e3a2020-06-23 09:54:02 +0930129size_t mctp_pktbuf_size(struct mctp_pktbuf *pkt)
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800130{
131 return pkt->end - pkt->start;
132}
133
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +0800134void *mctp_pktbuf_alloc_start(struct mctp_pktbuf *pkt, size_t size)
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800135{
136 assert(size <= pkt->start);
137 pkt->start -= size;
138 return pkt->data + pkt->start;
139}
140
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +0800141void *mctp_pktbuf_alloc_end(struct mctp_pktbuf *pkt, size_t size)
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800142{
143 void *buf;
144
Andrew Jeffery3ac70d62020-07-01 00:50:44 +0930145 assert(size <= (pkt->size - pkt->end));
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800146 buf = pkt->data + pkt->end;
147 pkt->end += size;
148 return buf;
149}
150
Matt Johnstondfbf0fd2024-10-28 14:40:29 +0800151int mctp_pktbuf_push(struct mctp_pktbuf *pkt, const void *data, size_t len)
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800152{
153 void *p;
154
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +0800155 if (pkt->end + len > pkt->size)
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800156 return -1;
157
158 p = pkt->data + pkt->end;
159
160 pkt->end += len;
161 memcpy(p, data, len);
162
163 return 0;
164}
165
Andrew Jefferyeba19a32021-03-09 23:09:40 +1030166void *mctp_pktbuf_pop(struct mctp_pktbuf *pkt, size_t len)
167{
168 if (len > mctp_pktbuf_size(pkt))
169 return NULL;
170
171 pkt->end -= len;
172 return pkt->data + pkt->end;
173}
174
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800175/* Message reassembly */
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600176static struct mctp_msg_ctx *mctp_msg_ctx_lookup(struct mctp *mctp, uint8_t src,
177 uint8_t dest, uint8_t tag)
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800178{
179 unsigned int i;
180
181 /* @todo: better lookup, if we add support for more outstanding
182 * message contexts */
183 for (i = 0; i < ARRAY_SIZE(mctp->msg_ctxs); i++) {
184 struct mctp_msg_ctx *ctx = &mctp->msg_ctxs[i];
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800185 if (ctx->src == src && ctx->dest == dest && ctx->tag == tag)
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800186 return ctx;
187 }
188
189 return NULL;
190}
191
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600192static struct mctp_msg_ctx *mctp_msg_ctx_create(struct mctp *mctp, uint8_t src,
193 uint8_t dest, uint8_t tag)
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800194{
Jeremy Kerr11a234e2019-02-27 17:59:53 +0800195 struct mctp_msg_ctx *ctx = NULL;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800196 unsigned int i;
197
198 for (i = 0; i < ARRAY_SIZE(mctp->msg_ctxs); i++) {
199 struct mctp_msg_ctx *tmp = &mctp->msg_ctxs[i];
200 if (!tmp->src) {
201 ctx = tmp;
202 break;
203 }
204 }
205
206 if (!ctx)
207 return NULL;
208
209 ctx->src = src;
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800210 ctx->dest = dest;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800211 ctx->tag = tag;
Jeremy Kerr9a3da812019-08-02 15:57:53 +0800212 ctx->buf_size = 0;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800213
214 return ctx;
215}
216
217static void mctp_msg_ctx_drop(struct mctp_msg_ctx *ctx)
218{
219 ctx->src = 0;
220}
221
222static void mctp_msg_ctx_reset(struct mctp_msg_ctx *ctx)
223{
224 ctx->buf_size = 0;
Sumanth Bhat69f545f2021-05-18 09:16:43 +0000225 ctx->fragment_size = 0;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800226}
227
228static int mctp_msg_ctx_add_pkt(struct mctp_msg_ctx *ctx,
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600229 struct mctp_pktbuf *pkt, size_t max_size)
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800230{
231 size_t len;
232
233 len = mctp_pktbuf_size(pkt) - sizeof(struct mctp_hdr);
234
Sumanth Bhatbc79c242021-06-16 12:36:56 +0530235 if (len + ctx->buf_size < ctx->buf_size) {
236 return -1;
237 }
238
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800239 if (ctx->buf_size + len > ctx->buf_alloc_size) {
240 size_t new_alloc_size;
Andrew Jeffery00ecc6c2020-03-10 23:16:53 +1030241 void *lbuf;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800242
Andrew Jeffery5a508912020-11-03 22:21:45 +1030243 /* @todo: finer-grained allocation */
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800244 if (!ctx->buf_alloc_size) {
Andrew Jefferyc2b833e2020-10-28 14:28:37 +1030245 new_alloc_size = MAX(len, 4096UL);
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800246 } else {
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600247 new_alloc_size = MAX(ctx->buf_alloc_size * 2,
248 len + ctx->buf_size);
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800249 }
Andrew Jeffery00ecc6c2020-03-10 23:16:53 +1030250
Sumanth Bhat2c820c52020-07-02 00:26:25 +0530251 /* Don't allow heap to grow beyond a limit */
252 if (new_alloc_size > max_size)
253 return -1;
254
Andrew Jeffery00ecc6c2020-03-10 23:16:53 +1030255 lbuf = __mctp_realloc(ctx->buf, new_alloc_size);
256 if (lbuf) {
257 ctx->buf = lbuf;
258 ctx->buf_alloc_size = new_alloc_size;
259 } else {
260 __mctp_free(ctx->buf);
261 return -1;
262 }
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800263 }
264
Moritz Fischer7aaccb52022-06-28 20:04:04 -0700265 memcpy((uint8_t *)ctx->buf + ctx->buf_size, mctp_pktbuf_data(pkt), len);
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800266 ctx->buf_size += len;
267
268 return 0;
269}
270
271/* Core API functions */
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800272struct mctp *mctp_init(void)
273{
274 struct mctp *mctp;
275
276 mctp = __mctp_alloc(sizeof(*mctp));
Sumanth Bhat96d54492020-07-14 17:10:04 +0530277
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600278 if (!mctp)
Sumanth Bhat96d54492020-07-14 17:10:04 +0530279 return NULL;
280
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800281 memset(mctp, 0, sizeof(*mctp));
Sumanth Bhat2c820c52020-07-02 00:26:25 +0530282 mctp->max_message_size = MCTP_MAX_MESSAGE_SIZE;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800283
284 return mctp;
285}
286
Sumanth Bhat2c820c52020-07-02 00:26:25 +0530287void mctp_set_max_message_size(struct mctp *mctp, size_t message_size)
288{
289 mctp->max_message_size = message_size;
290}
291
Andrew Jeffery5d3d4e62021-08-20 16:44:40 +0930292void mctp_set_capture_handler(struct mctp *mctp, mctp_capture_fn fn, void *user)
293{
294 mctp->capture = fn;
295 mctp->capture_data = user;
296}
297
Andrew Jeffery3ae89dc2021-01-28 15:24:36 +1030298static void mctp_bus_destroy(struct mctp_bus *bus)
299{
300 while (bus->tx_queue_head) {
301 struct mctp_pktbuf *curr = bus->tx_queue_head;
302
303 bus->tx_queue_head = curr->next;
304 mctp_pktbuf_free(curr);
305 }
306}
307
Andrew Jefferyfa56ca52020-03-10 23:18:22 +1030308void mctp_destroy(struct mctp *mctp)
309{
Andrew Jefferyb93b6112020-06-05 14:13:44 +0930310 size_t i;
Andrew Jefferyfa56ca52020-03-10 23:18:22 +1030311
312 /* Cleanup message assembly contexts */
Matt Johnston3ef47782024-12-11 15:19:06 +0800313 static_assert(ARRAY_SIZE(mctp->msg_ctxs) < SIZE_MAX, "size");
Andrew Jefferyfa56ca52020-03-10 23:18:22 +1030314 for (i = 0; i < ARRAY_SIZE(mctp->msg_ctxs); i++) {
315 struct mctp_msg_ctx *tmp = &mctp->msg_ctxs[i];
316 if (tmp->buf)
317 __mctp_free(tmp->buf);
318 }
319
Andrew Jeffery3ae89dc2021-01-28 15:24:36 +1030320 while (mctp->n_busses--)
321 mctp_bus_destroy(&mctp->busses[mctp->n_busses]);
322
Andrew Jefferyfa56ca52020-03-10 23:18:22 +1030323 __mctp_free(mctp->busses);
324 __mctp_free(mctp);
325}
326
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800327int mctp_set_rx_all(struct mctp *mctp, mctp_rx_fn fn, void *data)
328{
329 mctp->message_rx = fn;
330 mctp->message_rx_data = data;
331 return 0;
332}
333
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600334static struct mctp_bus *find_bus_for_eid(struct mctp *mctp, mctp_eid_t dest
335 __attribute__((unused)))
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800336{
Brad Bishop663ec392021-10-07 21:16:48 -0400337 if (mctp->n_busses == 0)
338 return NULL;
339
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800340 /* for now, just use the first bus. For full routing support,
341 * we will need a table of neighbours */
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800342 return &mctp->busses[0];
343}
344
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600345int mctp_register_bus(struct mctp *mctp, struct mctp_binding *binding,
346 mctp_eid_t eid)
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800347{
Andrew Jeffery3e8a12a2020-06-05 16:08:30 +0930348 int rc = 0;
349
Jeremy Kerr7520cec2019-03-01 07:13:18 +0800350 /* todo: multiple busses */
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800351 assert(mctp->n_busses == 0);
352 mctp->n_busses = 1;
Andrew Jeffery3e8a12a2020-06-05 16:08:30 +0930353
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800354 mctp->busses = __mctp_alloc(sizeof(struct mctp_bus));
Andrew Jeffery3e8a12a2020-06-05 16:08:30 +0930355 if (!mctp->busses)
356 return -ENOMEM;
357
James Feist62d72362019-12-13 13:43:32 -0800358 memset(mctp->busses, 0, sizeof(struct mctp_bus));
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800359 mctp->busses[0].binding = binding;
360 mctp->busses[0].eid = eid;
Jeremy Kerr7520cec2019-03-01 07:13:18 +0800361 binding->bus = &mctp->busses[0];
Jeremy Kerr0a00dca2019-03-01 08:01:35 +0800362 binding->mctp = mctp;
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800363 mctp->route_policy = ROUTE_ENDPOINT;
Jeremy Kerr3b36d172019-09-04 11:56:09 +0800364
Andrew Jeffery3e8a12a2020-06-05 16:08:30 +0930365 if (binding->start) {
366 rc = binding->start(binding);
367 if (rc < 0) {
368 mctp_prerr("Failed to start binding: %d", rc);
Andrew Jeffery19275232021-01-29 14:13:25 +1030369 binding->bus = NULL;
Andrew Jeffery3e8a12a2020-06-05 16:08:30 +0930370 __mctp_free(mctp->busses);
371 mctp->busses = NULL;
Andrew Jeffery2304c832021-01-29 11:52:49 +1030372 mctp->n_busses = 0;
Andrew Jeffery3e8a12a2020-06-05 16:08:30 +0930373 }
374 }
Jeremy Kerr3b36d172019-09-04 11:56:09 +0800375
Andrew Jeffery3e8a12a2020-06-05 16:08:30 +0930376 return rc;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800377}
378
Andrew Jeffery2094c3c2021-08-26 12:32:46 +0930379void mctp_unregister_bus(struct mctp *mctp, struct mctp_binding *binding)
380{
381 /*
382 * We only support one bus right now; once the call completes we will
383 * have no more busses
384 */
385 mctp->n_busses = 0;
386 binding->mctp = NULL;
387 binding->bus = NULL;
Benjamin Gwinb7824b62023-08-08 18:44:53 +0000388 __mctp_free(mctp->busses);
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
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800396 assert(mctp->n_busses == 0);
397 mctp->busses = __mctp_alloc(2 * sizeof(struct mctp_bus));
Helen Huanga523bcc2021-05-19 15:44:56 +0800398 if (!mctp->busses)
399 return -ENOMEM;
James Feist62d72362019-12-13 13:43:32 -0800400 memset(mctp->busses, 0, 2 * sizeof(struct mctp_bus));
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800401 mctp->n_busses = 2;
402 mctp->busses[0].binding = b1;
403 b1->bus = &mctp->busses[0];
404 b1->mctp = mctp;
405 mctp->busses[1].binding = b2;
406 b2->bus = &mctp->busses[1];
407 b2->mctp = mctp;
408
409 mctp->route_policy = ROUTE_BRIDGE;
Jeremy Kerr3b36d172019-09-04 11:56:09 +0800410
Andrew Jeffery19275232021-01-29 14:13:25 +1030411 if (b1->start) {
412 rc = b1->start(b1);
413 if (rc < 0) {
414 mctp_prerr("Failed to start bridged bus %s: %d",
415 b1->name, rc);
416 goto done;
417 }
418 }
Jeremy Kerr3b36d172019-09-04 11:56:09 +0800419
Andrew Jeffery19275232021-01-29 14:13:25 +1030420 if (b2->start) {
421 rc = b2->start(b2);
422 if (rc < 0) {
423 mctp_prerr("Failed to start bridged bus %s: %d",
424 b2->name, rc);
425 goto done;
426 }
427 }
Jeremy Kerr3b36d172019-09-04 11:56:09 +0800428
Andrew Jeffery19275232021-01-29 14:13:25 +1030429done:
430 return rc;
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800431}
432
Wiktor GoĊ‚gowskiba6727e2020-03-13 18:25:01 +0100433static inline bool mctp_ctrl_cmd_is_transport(struct mctp_ctrl_msg_hdr *hdr)
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800434{
Matt Johnston3ef47782024-12-11 15:19:06 +0800435#pragma GCC diagnostic push
436#pragma GCC diagnostic ignored "-Wtype-limits"
Wiktor GoĊ‚gowskiba6727e2020-03-13 18:25:01 +0100437 return ((hdr->command_code >= MCTP_CTRL_CMD_FIRST_TRANSPORT) &&
438 (hdr->command_code <= MCTP_CTRL_CMD_LAST_TRANSPORT));
Matt Johnston3ef47782024-12-11 15:19:06 +0800439#pragma GCC diagnostic pop
Wiktor GoĊ‚gowskiba6727e2020-03-13 18:25:01 +0100440}
441
Andrew Jefferyb93b6112020-06-05 14:13:44 +0930442static bool mctp_ctrl_handle_msg(struct mctp_bus *bus, mctp_eid_t src,
Sumanth Bhatf39c3852022-01-10 17:04:10 +0530443 uint8_t msg_tag, bool tag_owner, void *buffer,
444 size_t length)
Wiktor GoĊ‚gowskiba6727e2020-03-13 18:25:01 +0100445{
446 struct mctp_ctrl_msg_hdr *msg_hdr = buffer;
447
448 /*
449 * Control message is received. If a transport control message handler
450 * is provided, it will called. If there is no dedicated handler, this
451 * function returns false and data can be handled by the generic
452 * message handler. The transport control message handler will be
453 * provided with messages in the command range 0xF0 - 0xFF.
454 */
455 if (mctp_ctrl_cmd_is_transport(msg_hdr)) {
456 if (bus->binding->control_rx != NULL) {
457 /* MCTP bus binding handler */
Sumanth Bhatf39c3852022-01-10 17:04:10 +0530458 bus->binding->control_rx(src, msg_tag, tag_owner,
Wiktor GoĊ‚gowskiba6727e2020-03-13 18:25:01 +0100459 bus->binding->control_rx_data,
460 buffer, length);
461 return true;
462 }
463 }
464
465 /*
466 * Command was not handled, due to lack of specific callback.
467 * It will be passed to regular message_rx handler.
468 */
469 return false;
470}
471
472static inline bool mctp_rx_dest_is_local(struct mctp_bus *bus, mctp_eid_t dest)
473{
474 return dest == bus->eid || dest == MCTP_EID_NULL ||
475 dest == MCTP_EID_BROADCAST;
476}
477
478static inline bool mctp_ctrl_cmd_is_request(struct mctp_ctrl_msg_hdr *hdr)
479{
480 return hdr->ic_msg_type == MCTP_CTRL_HDR_MSG_TYPE &&
481 hdr->rq_dgram_inst & MCTP_CTRL_HDR_FLAG_REQUEST;
482}
483
484/*
485 * Receive the complete MCTP message and route it.
486 * Asserts:
487 * 'buf' is not NULL.
488 */
489static void mctp_rx(struct mctp *mctp, struct mctp_bus *bus, mctp_eid_t src,
Sumanth Bhatf39c3852022-01-10 17:04:10 +0530490 mctp_eid_t dest, bool tag_owner, uint8_t msg_tag, void *buf,
491 size_t len)
Wiktor GoĊ‚gowskiba6727e2020-03-13 18:25:01 +0100492{
493 assert(buf != NULL);
494
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800495 if (mctp->route_policy == ROUTE_ENDPOINT &&
Wiktor GoĊ‚gowskiba6727e2020-03-13 18:25:01 +0100496 mctp_rx_dest_is_local(bus, dest)) {
497 /* Handle MCTP Control Messages: */
498 if (len >= sizeof(struct mctp_ctrl_msg_hdr)) {
499 struct mctp_ctrl_msg_hdr *msg_hdr = buf;
500
501 /*
502 * Identify if this is a control request message.
503 * See DSP0236 v1.3.0 sec. 11.5.
504 */
505 if (mctp_ctrl_cmd_is_request(msg_hdr)) {
506 bool handled;
Sumanth Bhatf39c3852022-01-10 17:04:10 +0530507 handled = mctp_ctrl_handle_msg(
508 bus, src, msg_tag, tag_owner, buf, len);
Wiktor GoĊ‚gowskiba6727e2020-03-13 18:25:01 +0100509 if (handled)
510 return;
511 }
512 }
Sumanth Bhatf39c3852022-01-10 17:04:10 +0530513
Wiktor GoĊ‚gowskiba6727e2020-03-13 18:25:01 +0100514 if (mctp->message_rx)
Sumanth Bhatf39c3852022-01-10 17:04:10 +0530515 mctp->message_rx(src, tag_owner, msg_tag,
516 mctp->message_rx_data, buf, len);
Wiktor GoĊ‚gowskiba6727e2020-03-13 18:25:01 +0100517 }
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800518
519 if (mctp->route_policy == ROUTE_BRIDGE) {
520 int i;
521
522 for (i = 0; i < mctp->n_busses; i++) {
523 struct mctp_bus *dest_bus = &mctp->busses[i];
524 if (dest_bus == bus)
525 continue;
526
Sumanth Bhatf39c3852022-01-10 17:04:10 +0530527 mctp_message_tx_on_bus(dest_bus, src, dest, tag_owner,
528 msg_tag, buf, len);
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800529 }
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800530 }
531}
532
Jeremy Kerr0a00dca2019-03-01 08:01:35 +0800533void mctp_bus_rx(struct mctp_binding *binding, struct mctp_pktbuf *pkt)
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800534{
Jeremy Kerr7520cec2019-03-01 07:13:18 +0800535 struct mctp_bus *bus = binding->bus;
Jeremy Kerr0a00dca2019-03-01 08:01:35 +0800536 struct mctp *mctp = binding->mctp;
Ed Tanousc2def9f2019-02-21 08:33:08 -0800537 uint8_t flags, exp_seq, seq, tag;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800538 struct mctp_msg_ctx *ctx;
539 struct mctp_hdr *hdr;
Sumanth Bhatf39c3852022-01-10 17:04:10 +0530540 bool tag_owner;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800541 size_t len;
542 void *p;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800543 int rc;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800544
Jeremy Kerr7520cec2019-03-01 07:13:18 +0800545 assert(bus);
546
Sumanth Bhatd97869d2020-07-02 00:46:13 +0530547 /* Drop packet if it was smaller than mctp hdr size */
Matt Johnston86e9a972024-10-28 15:06:33 +0800548 if (mctp_pktbuf_size(pkt) < sizeof(struct mctp_hdr))
Sumanth Bhatd97869d2020-07-02 00:46:13 +0530549 goto out;
550
Andrew Jeffery5d3d4e62021-08-20 16:44:40 +0930551 if (mctp->capture)
Rashmica Guptaf2988972022-11-09 12:26:44 +1100552 mctp->capture(pkt, MCTP_MESSAGE_CAPTURE_INCOMING,
553 mctp->capture_data);
Andrew Jeffery5d3d4e62021-08-20 16:44:40 +0930554
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800555 hdr = mctp_pktbuf_hdr(pkt);
556
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800557 /* small optimisation: don't bother reassembly if we're going to
558 * drop the packet in mctp_rx anyway */
John Chung133df7a2024-05-14 16:19:56 +0800559 if (mctp->route_policy == ROUTE_ENDPOINT &&
560 !mctp_rx_dest_is_local(bus, hdr->dest))
Jeremy Kerrc1693af2019-08-05 14:30:59 +0800561 goto out;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800562
563 flags = hdr->flags_seq_tag & (MCTP_HDR_FLAG_SOM | MCTP_HDR_FLAG_EOM);
564 tag = (hdr->flags_seq_tag >> MCTP_HDR_TAG_SHIFT) & MCTP_HDR_TAG_MASK;
565 seq = (hdr->flags_seq_tag >> MCTP_HDR_SEQ_SHIFT) & MCTP_HDR_SEQ_MASK;
Andrew Jeffery7f7fdc12023-05-12 15:56:47 +0930566 tag_owner = (hdr->flags_seq_tag >> MCTP_HDR_TO_SHIFT) &
567 MCTP_HDR_TO_MASK;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800568
569 switch (flags) {
570 case MCTP_HDR_FLAG_SOM | MCTP_HDR_FLAG_EOM:
571 /* single-packet message - send straight up to rx function,
572 * no need to create a message context */
573 len = pkt->end - pkt->mctp_hdr_off - sizeof(struct mctp_hdr);
Andrew Jefferyb4ae00b2021-01-18 15:52:09 +1030574 p = pkt->data + pkt->mctp_hdr_off + sizeof(struct mctp_hdr);
Sumanth Bhatf39c3852022-01-10 17:04:10 +0530575 mctp_rx(mctp, bus, hdr->src, hdr->dest, tag_owner, tag, p, len);
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800576 break;
577
578 case MCTP_HDR_FLAG_SOM:
579 /* start of a new message - start the new context for
580 * future message reception. If an existing context is
581 * already present, drop it. */
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800582 ctx = mctp_msg_ctx_lookup(mctp, hdr->src, hdr->dest, tag);
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800583 if (ctx) {
584 mctp_msg_ctx_reset(ctx);
585 } else {
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600586 ctx = mctp_msg_ctx_create(mctp, hdr->src, hdr->dest,
587 tag);
Sumanth Bhat34d4c962021-06-16 12:50:48 +0530588 /* If context creation fails due to exhaution of contexts we
589 * can support, drop the packet */
590 if (!ctx) {
591 mctp_prdebug("Context buffers exhausted.");
592 goto out;
593 }
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800594 }
595
Sumanth Bhat69f545f2021-05-18 09:16:43 +0000596 /* Save the fragment size, subsequent middle fragments
597 * should of the same size */
598 ctx->fragment_size = mctp_pktbuf_size(pkt);
599
Sumanth Bhat2c820c52020-07-02 00:26:25 +0530600 rc = mctp_msg_ctx_add_pkt(ctx, pkt, mctp->max_message_size);
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800601 if (rc) {
602 mctp_msg_ctx_drop(ctx);
603 } else {
604 ctx->last_seq = seq;
605 }
606
607 break;
608
609 case MCTP_HDR_FLAG_EOM:
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)
Jeremy Kerrc1693af2019-08-05 14:30:59 +0800612 goto out;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800613
Ed Tanousc2def9f2019-02-21 08:33:08 -0800614 exp_seq = (ctx->last_seq + 1) % 4;
615
616 if (exp_seq != seq) {
617 mctp_prdebug(
618 "Sequence number %d does not match expected %d",
619 seq, exp_seq);
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800620 mctp_msg_ctx_drop(ctx);
Jeremy Kerrc1693af2019-08-05 14:30:59 +0800621 goto out;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800622 }
623
Sumanth Bhat69f545f2021-05-18 09:16:43 +0000624 len = mctp_pktbuf_size(pkt);
625
626 if (len > ctx->fragment_size) {
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600627 mctp_prdebug("Unexpected fragment size. Expected"
628 " less than %zu, received = %zu",
629 ctx->fragment_size, len);
Sumanth Bhat69f545f2021-05-18 09:16:43 +0000630 mctp_msg_ctx_drop(ctx);
631 goto out;
632 }
633
Sumanth Bhat2c820c52020-07-02 00:26:25 +0530634 rc = mctp_msg_ctx_add_pkt(ctx, pkt, mctp->max_message_size);
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800635 if (!rc)
Sumanth Bhatf39c3852022-01-10 17:04:10 +0530636 mctp_rx(mctp, bus, ctx->src, ctx->dest, tag_owner, tag,
637 ctx->buf, ctx->buf_size);
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800638
639 mctp_msg_ctx_drop(ctx);
640 break;
Ed Tanousc2def9f2019-02-21 08:33:08 -0800641
642 case 0:
643 /* Neither SOM nor EOM */
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600644 ctx = mctp_msg_ctx_lookup(mctp, hdr->src, hdr->dest, tag);
Ed Tanousc2def9f2019-02-21 08:33:08 -0800645 if (!ctx)
Jeremy Kerrc1693af2019-08-05 14:30:59 +0800646 goto out;
Ed Tanousc2def9f2019-02-21 08:33:08 -0800647
648 exp_seq = (ctx->last_seq + 1) % 4;
649 if (exp_seq != seq) {
650 mctp_prdebug(
651 "Sequence number %d does not match expected %d",
652 seq, exp_seq);
653 mctp_msg_ctx_drop(ctx);
Jeremy Kerrc1693af2019-08-05 14:30:59 +0800654 goto out;
Ed Tanousc2def9f2019-02-21 08:33:08 -0800655 }
656
Sumanth Bhat69f545f2021-05-18 09:16:43 +0000657 len = mctp_pktbuf_size(pkt);
658
659 if (len != ctx->fragment_size) {
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600660 mctp_prdebug("Unexpected fragment size. Expected = %zu "
661 "received = %zu",
662 ctx->fragment_size, len);
Sumanth Bhat69f545f2021-05-18 09:16:43 +0000663 mctp_msg_ctx_drop(ctx);
664 goto out;
665 }
666
Sumanth Bhat2c820c52020-07-02 00:26:25 +0530667 rc = mctp_msg_ctx_add_pkt(ctx, pkt, mctp->max_message_size);
Ed Tanousc2def9f2019-02-21 08:33:08 -0800668 if (rc) {
669 mctp_msg_ctx_drop(ctx);
Jeremy Kerrc1693af2019-08-05 14:30:59 +0800670 goto out;
Ed Tanousc2def9f2019-02-21 08:33:08 -0800671 }
672 ctx->last_seq = seq;
673
674 break;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800675 }
Jeremy Kerrc1693af2019-08-05 14:30:59 +0800676out:
677 mctp_pktbuf_free(pkt);
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800678}
679
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600680static int mctp_packet_tx(struct mctp_bus *bus, struct mctp_pktbuf *pkt)
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800681{
Andrew Jeffery5d3d4e62021-08-20 16:44:40 +0930682 struct mctp *mctp = bus->binding->mctp;
683
Andrew Jefferyc61501c2021-01-27 23:24:18 +1030684 if (bus->state != mctp_bus_state_tx_enabled)
Jeremy Kerr1cd31182019-02-27 18:01:00 +0800685 return -1;
686
Andrew Jeffery5d3d4e62021-08-20 16:44:40 +0930687 if (mctp->capture)
Rashmica Guptaf2988972022-11-09 12:26:44 +1100688 mctp->capture(pkt, MCTP_MESSAGE_CAPTURE_OUTGOING,
689 mctp->capture_data);
Andrew Jeffery5d3d4e62021-08-20 16:44:40 +0930690
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800691 return bus->binding->tx(bus->binding, pkt);
692}
693
Jeremy Kerrcc2458d2019-03-01 08:23:33 +0800694static void mctp_send_tx_queue(struct mctp_bus *bus)
Jeremy Kerr1cd31182019-02-27 18:01:00 +0800695{
696 struct mctp_pktbuf *pkt;
697
Jeremy Kerrcc2458d2019-03-01 08:23:33 +0800698 while ((pkt = bus->tx_queue_head)) {
Jeremy Kerr1cd31182019-02-27 18:01:00 +0800699 int rc;
700
701 rc = mctp_packet_tx(bus, pkt);
Andrew Jeffery0721f582022-09-29 12:12:39 +0930702 switch (rc) {
703 /* If transmission succeded, or */
704 case 0:
705 /* If the packet is somehow too large */
706 case -EMSGSIZE:
707 /* Drop the packet */
708 bus->tx_queue_head = pkt->next;
709 mctp_pktbuf_free(pkt);
Jeremy Kerr1cd31182019-02-27 18:01:00 +0800710 break;
711
Andrew Jeffery0721f582022-09-29 12:12:39 +0930712 /* If the binding was busy, or */
713 case -EBUSY:
714 /* Some other unknown error occurred */
715 default:
716 /* Make sure the tail pointer is consistent and retry later */
717 goto cleanup_tail;
718 };
Jeremy Kerr1cd31182019-02-27 18:01:00 +0800719 }
720
Andrew Jeffery0721f582022-09-29 12:12:39 +0930721cleanup_tail:
Jeremy Kerrcc2458d2019-03-01 08:23:33 +0800722 if (!bus->tx_queue_head)
723 bus->tx_queue_tail = NULL;
Jeremy Kerr1cd31182019-02-27 18:01:00 +0800724}
725
726void mctp_binding_set_tx_enabled(struct mctp_binding *binding, bool enable)
727{
728 struct mctp_bus *bus = binding->bus;
Andrew Jefferyc61501c2021-01-27 23:24:18 +1030729
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600730 switch (bus->state) {
Andrew Jefferyc61501c2021-01-27 23:24:18 +1030731 case mctp_bus_state_constructed:
732 if (!enable)
733 return;
734
Andrew Jeffery1fa707e2021-01-28 15:22:11 +1030735 if (binding->pkt_size < MCTP_PACKET_SIZE(MCTP_BTU)) {
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600736 mctp_prerr(
737 "Cannot start %s binding with invalid MTU: %zu",
738 binding->name,
739 MCTP_BODY_SIZE(binding->pkt_size));
Andrew Jeffery1fa707e2021-01-28 15:22:11 +1030740 return;
741 }
742
Andrew Jefferyc61501c2021-01-27 23:24:18 +1030743 bus->state = mctp_bus_state_tx_enabled;
744 mctp_prinfo("%s binding started", binding->name);
745 return;
746 case mctp_bus_state_tx_enabled:
747 if (enable)
748 return;
749
750 bus->state = mctp_bus_state_tx_disabled;
751 mctp_prdebug("%s binding Tx disabled", binding->name);
752 return;
753 case mctp_bus_state_tx_disabled:
754 if (!enable)
755 return;
756
757 bus->state = mctp_bus_state_tx_enabled;
758 mctp_prdebug("%s binding Tx enabled", binding->name);
Jeremy Kerrcc2458d2019-03-01 08:23:33 +0800759 mctp_send_tx_queue(bus);
Andrew Jefferyc61501c2021-01-27 23:24:18 +1030760 return;
761 }
Jeremy Kerr1cd31182019-02-27 18:01:00 +0800762}
763
Andrew Jefferyb93b6112020-06-05 14:13:44 +0930764static int mctp_message_tx_on_bus(struct mctp_bus *bus, mctp_eid_t src,
Sumanth Bhatf39c3852022-01-10 17:04:10 +0530765 mctp_eid_t dest, bool tag_owner,
766 uint8_t msg_tag, void *msg, size_t msg_len)
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800767{
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +0800768 size_t max_payload_len, payload_len, p;
Jeremy Kerr1cd31182019-02-27 18:01:00 +0800769 struct mctp_pktbuf *pkt;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800770 struct mctp_hdr *hdr;
Jeremy Kerrc855d7b2019-08-01 21:18:09 +0800771 int i;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800772
Andrew Jefferyc61501c2021-01-27 23:24:18 +1030773 if (bus->state == mctp_bus_state_constructed)
774 return -ENXIO;
775
Sumanth Bhatf39c3852022-01-10 17:04:10 +0530776 if ((msg_tag & MCTP_HDR_TAG_MASK) != msg_tag)
777 return -EINVAL;
778
Andrew Jeffery1fa707e2021-01-28 15:22:11 +1030779 max_payload_len = MCTP_BODY_SIZE(bus->binding->pkt_size);
780
781 {
782 const bool valid_mtu = max_payload_len >= MCTP_BTU;
783 assert(valid_mtu);
784 if (!valid_mtu)
785 return -EINVAL;
786 }
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800787
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600788 mctp_prdebug(
789 "%s: Generating packets for transmission of %zu byte message from %hhu to %hhu",
790 __func__, msg_len, src, dest);
Andrew Jeffery298865f2020-02-06 11:51:29 +1030791
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800792 /* queue up packets, each of max MCTP_MTU size */
Jeremy Kerrc855d7b2019-08-01 21:18:09 +0800793 for (p = 0, i = 0; p < msg_len; i++) {
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +0800794 payload_len = msg_len - p;
795 if (payload_len > max_payload_len)
796 payload_len = max_payload_len;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800797
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800798 pkt = mctp_pktbuf_alloc(bus->binding,
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600799 payload_len + sizeof(*hdr));
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800800 hdr = mctp_pktbuf_hdr(pkt);
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800801
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800802 hdr->ver = bus->binding->version & 0xf;
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800803 hdr->dest = dest;
804 hdr->src = src;
Sumanth Bhatf39c3852022-01-10 17:04:10 +0530805 hdr->flags_seq_tag = (tag_owner << MCTP_HDR_TO_SHIFT) |
806 (msg_tag << MCTP_HDR_TAG_SHIFT);
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800807
Jeremy Kerrc855d7b2019-08-01 21:18:09 +0800808 if (i == 0)
809 hdr->flags_seq_tag |= MCTP_HDR_FLAG_SOM;
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +0800810 if (p + payload_len >= msg_len)
Jeremy Kerrc855d7b2019-08-01 21:18:09 +0800811 hdr->flags_seq_tag |= MCTP_HDR_FLAG_EOM;
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600812 hdr->flags_seq_tag |= (i & MCTP_HDR_SEQ_MASK)
813 << MCTP_HDR_SEQ_SHIFT;
Jeremy Kerrc855d7b2019-08-01 21:18:09 +0800814
Moritz Fischer7aaccb52022-06-28 20:04:04 -0700815 memcpy(mctp_pktbuf_data(pkt), (uint8_t *)msg + p, payload_len);
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800816
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800817 /* add to tx queue */
Jeremy Kerrcc2458d2019-03-01 08:23:33 +0800818 if (bus->tx_queue_tail)
819 bus->tx_queue_tail->next = pkt;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800820 else
Jeremy Kerrcc2458d2019-03-01 08:23:33 +0800821 bus->tx_queue_head = pkt;
822 bus->tx_queue_tail = pkt;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800823
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +0800824 p += payload_len;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800825 }
826
Andrew Jeffery298865f2020-02-06 11:51:29 +1030827 mctp_prdebug("%s: Enqueued %d packets", __func__, i);
828
Jeremy Kerrcc2458d2019-03-01 08:23:33 +0800829 mctp_send_tx_queue(bus);
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800830
831 return 0;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800832}
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800833
Sumanth Bhatf39c3852022-01-10 17:04:10 +0530834int mctp_message_tx(struct mctp *mctp, mctp_eid_t eid, bool tag_owner,
835 uint8_t msg_tag, void *msg, size_t msg_len)
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800836{
837 struct mctp_bus *bus;
838
Sumanth Bhatf39c3852022-01-10 17:04:10 +0530839 /* TODO: Protect against same tag being used across
840 * different callers */
841 if ((msg_tag & MCTP_HDR_TAG_MASK) != msg_tag) {
842 mctp_prerr("Incorrect message tag %u passed.", msg_tag);
843 return -EINVAL;
844 }
845
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800846 bus = find_bus_for_eid(mctp, eid);
Brad Bishop663ec392021-10-07 21:16:48 -0400847 if (!bus)
848 return 0;
849
Sumanth Bhatf39c3852022-01-10 17:04:10 +0530850 return mctp_message_tx_on_bus(bus, bus->eid, eid, tag_owner, msg_tag,
851 msg, msg_len);
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800852}