blob: fcc92793cf2cd79c23ae38adacfe16ef74eccb66 [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
75#ifndef BUILD_ASSERT
Patrick Williamsa721c2d2022-12-04 14:30:26 -060076#define BUILD_ASSERT(x) \
77 do { \
78 (void)sizeof(char[0 - (!(x))]); \
79 } while (0)
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080080#endif
81
Jeremy Kerr24db71f2019-02-07 21:37:35 +080082#ifndef ARRAY_SIZE
83#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
84#endif
85
Sumanth Bhat2c820c52020-07-02 00:26:25 +053086/* 64kb should be sufficient for a single message. Applications
87 * requiring higher sizes can override by setting max_message_size.*/
88#ifndef MCTP_MAX_MESSAGE_SIZE
89#define MCTP_MAX_MESSAGE_SIZE 65536
90#endif
91
Andrew Jefferyb93b6112020-06-05 14:13:44 +093092static int mctp_message_tx_on_bus(struct mctp_bus *bus, mctp_eid_t src,
Sumanth Bhatf39c3852022-01-10 17:04:10 +053093 mctp_eid_t dest, bool tag_owner,
94 uint8_t msg_tag, void *msg, size_t msg_len);
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +080095
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +080096struct mctp_pktbuf *mctp_pktbuf_alloc(struct mctp_binding *binding, size_t len)
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080097{
98 struct mctp_pktbuf *buf;
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +080099 size_t size;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800100
Andrew Jeffery39da3d02021-03-12 16:51:26 +1030101 size = binding->pkt_size + binding->pkt_header + binding->pkt_trailer;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800102
103 /* todo: pools */
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +0800104 buf = __mctp_alloc(sizeof(*buf) + size);
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800105
Pedro Martelletto2608b292023-03-30 13:28:28 +0000106 if (!buf)
107 return NULL;
108
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +0800109 buf->size = size;
Andrew Jeffery39da3d02021-03-12 16:51:26 +1030110 buf->start = binding->pkt_header;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800111 buf->end = buf->start + len;
112 buf->mctp_hdr_off = buf->start;
Jeremy Kerrdd109f12019-04-04 11:46:49 +0800113 buf->next = NULL;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800114
115 return buf;
116}
117
118void mctp_pktbuf_free(struct mctp_pktbuf *pkt)
119{
120 __mctp_free(pkt);
121}
122
123struct mctp_hdr *mctp_pktbuf_hdr(struct mctp_pktbuf *pkt)
124{
Moritz Fischer7aaccb52022-06-28 20:04:04 -0700125 return (struct mctp_hdr *)(pkt->data + pkt->mctp_hdr_off);
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800126}
127
128void *mctp_pktbuf_data(struct mctp_pktbuf *pkt)
129{
Moritz Fischer7aaccb52022-06-28 20:04:04 -0700130 return pkt->data + pkt->mctp_hdr_off + sizeof(struct mctp_hdr);
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800131}
132
Andrew Jefferyb942e3a2020-06-23 09:54:02 +0930133size_t mctp_pktbuf_size(struct mctp_pktbuf *pkt)
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800134{
135 return pkt->end - pkt->start;
136}
137
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +0800138void *mctp_pktbuf_alloc_start(struct mctp_pktbuf *pkt, size_t size)
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800139{
140 assert(size <= pkt->start);
141 pkt->start -= size;
142 return pkt->data + pkt->start;
143}
144
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +0800145void *mctp_pktbuf_alloc_end(struct mctp_pktbuf *pkt, size_t size)
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800146{
147 void *buf;
148
Andrew Jeffery3ac70d62020-07-01 00:50:44 +0930149 assert(size <= (pkt->size - pkt->end));
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800150 buf = pkt->data + pkt->end;
151 pkt->end += size;
152 return buf;
153}
154
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +0800155int mctp_pktbuf_push(struct mctp_pktbuf *pkt, void *data, size_t len)
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800156{
157 void *p;
158
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +0800159 if (pkt->end + len > pkt->size)
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800160 return -1;
161
162 p = pkt->data + pkt->end;
163
164 pkt->end += len;
165 memcpy(p, data, len);
166
167 return 0;
168}
169
Andrew Jefferyeba19a32021-03-09 23:09:40 +1030170void *mctp_pktbuf_pop(struct mctp_pktbuf *pkt, size_t len)
171{
172 if (len > mctp_pktbuf_size(pkt))
173 return NULL;
174
175 pkt->end -= len;
176 return pkt->data + pkt->end;
177}
178
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800179/* Message reassembly */
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600180static struct mctp_msg_ctx *mctp_msg_ctx_lookup(struct mctp *mctp, uint8_t src,
181 uint8_t dest, uint8_t tag)
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800182{
183 unsigned int i;
184
185 /* @todo: better lookup, if we add support for more outstanding
186 * message contexts */
187 for (i = 0; i < ARRAY_SIZE(mctp->msg_ctxs); i++) {
188 struct mctp_msg_ctx *ctx = &mctp->msg_ctxs[i];
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800189 if (ctx->src == src && ctx->dest == dest && ctx->tag == tag)
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800190 return ctx;
191 }
192
193 return NULL;
194}
195
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600196static struct mctp_msg_ctx *mctp_msg_ctx_create(struct mctp *mctp, uint8_t src,
197 uint8_t dest, uint8_t tag)
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800198{
Jeremy Kerr11a234e2019-02-27 17:59:53 +0800199 struct mctp_msg_ctx *ctx = NULL;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800200 unsigned int i;
201
202 for (i = 0; i < ARRAY_SIZE(mctp->msg_ctxs); i++) {
203 struct mctp_msg_ctx *tmp = &mctp->msg_ctxs[i];
204 if (!tmp->src) {
205 ctx = tmp;
206 break;
207 }
208 }
209
210 if (!ctx)
211 return NULL;
212
213 ctx->src = src;
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800214 ctx->dest = dest;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800215 ctx->tag = tag;
Jeremy Kerr9a3da812019-08-02 15:57:53 +0800216 ctx->buf_size = 0;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800217
218 return ctx;
219}
220
221static void mctp_msg_ctx_drop(struct mctp_msg_ctx *ctx)
222{
223 ctx->src = 0;
224}
225
226static void mctp_msg_ctx_reset(struct mctp_msg_ctx *ctx)
227{
228 ctx->buf_size = 0;
Sumanth Bhat69f545f2021-05-18 09:16:43 +0000229 ctx->fragment_size = 0;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800230}
231
232static int mctp_msg_ctx_add_pkt(struct mctp_msg_ctx *ctx,
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600233 struct mctp_pktbuf *pkt, size_t max_size)
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800234{
235 size_t len;
236
237 len = mctp_pktbuf_size(pkt) - sizeof(struct mctp_hdr);
238
Sumanth Bhatbc79c242021-06-16 12:36:56 +0530239 if (len + ctx->buf_size < ctx->buf_size) {
240 return -1;
241 }
242
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800243 if (ctx->buf_size + len > ctx->buf_alloc_size) {
244 size_t new_alloc_size;
Andrew Jeffery00ecc6c2020-03-10 23:16:53 +1030245 void *lbuf;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800246
Andrew Jeffery5a508912020-11-03 22:21:45 +1030247 /* @todo: finer-grained allocation */
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800248 if (!ctx->buf_alloc_size) {
Andrew Jefferyc2b833e2020-10-28 14:28:37 +1030249 new_alloc_size = MAX(len, 4096UL);
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800250 } else {
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600251 new_alloc_size = MAX(ctx->buf_alloc_size * 2,
252 len + ctx->buf_size);
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800253 }
Andrew Jeffery00ecc6c2020-03-10 23:16:53 +1030254
Sumanth Bhat2c820c52020-07-02 00:26:25 +0530255 /* Don't allow heap to grow beyond a limit */
256 if (new_alloc_size > max_size)
257 return -1;
258
Andrew Jeffery00ecc6c2020-03-10 23:16:53 +1030259 lbuf = __mctp_realloc(ctx->buf, new_alloc_size);
260 if (lbuf) {
261 ctx->buf = lbuf;
262 ctx->buf_alloc_size = new_alloc_size;
263 } else {
264 __mctp_free(ctx->buf);
265 return -1;
266 }
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800267 }
268
Moritz Fischer7aaccb52022-06-28 20:04:04 -0700269 memcpy((uint8_t *)ctx->buf + ctx->buf_size, mctp_pktbuf_data(pkt), len);
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800270 ctx->buf_size += len;
271
272 return 0;
273}
274
275/* Core API functions */
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800276struct mctp *mctp_init(void)
277{
278 struct mctp *mctp;
279
280 mctp = __mctp_alloc(sizeof(*mctp));
Sumanth Bhat96d54492020-07-14 17:10:04 +0530281
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600282 if (!mctp)
Sumanth Bhat96d54492020-07-14 17:10:04 +0530283 return NULL;
284
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800285 memset(mctp, 0, sizeof(*mctp));
Sumanth Bhat2c820c52020-07-02 00:26:25 +0530286 mctp->max_message_size = MCTP_MAX_MESSAGE_SIZE;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800287
288 return mctp;
289}
290
Sumanth Bhat2c820c52020-07-02 00:26:25 +0530291void mctp_set_max_message_size(struct mctp *mctp, size_t message_size)
292{
293 mctp->max_message_size = message_size;
294}
295
Andrew Jeffery5d3d4e62021-08-20 16:44:40 +0930296void mctp_set_capture_handler(struct mctp *mctp, mctp_capture_fn fn, void *user)
297{
298 mctp->capture = fn;
299 mctp->capture_data = user;
300}
301
Andrew Jeffery3ae89dc2021-01-28 15:24:36 +1030302static void mctp_bus_destroy(struct mctp_bus *bus)
303{
304 while (bus->tx_queue_head) {
305 struct mctp_pktbuf *curr = bus->tx_queue_head;
306
307 bus->tx_queue_head = curr->next;
308 mctp_pktbuf_free(curr);
309 }
310}
311
Andrew Jefferyfa56ca52020-03-10 23:18:22 +1030312void mctp_destroy(struct mctp *mctp)
313{
Andrew Jefferyb93b6112020-06-05 14:13:44 +0930314 size_t i;
Andrew Jefferyfa56ca52020-03-10 23:18:22 +1030315
316 /* Cleanup message assembly contexts */
Andrew Jefferyb93b6112020-06-05 14:13:44 +0930317 BUILD_ASSERT(ARRAY_SIZE(mctp->msg_ctxs) < SIZE_MAX);
Andrew Jefferyfa56ca52020-03-10 23:18:22 +1030318 for (i = 0; i < ARRAY_SIZE(mctp->msg_ctxs); i++) {
319 struct mctp_msg_ctx *tmp = &mctp->msg_ctxs[i];
320 if (tmp->buf)
321 __mctp_free(tmp->buf);
322 }
323
Andrew Jeffery3ae89dc2021-01-28 15:24:36 +1030324 while (mctp->n_busses--)
325 mctp_bus_destroy(&mctp->busses[mctp->n_busses]);
326
Andrew Jefferyfa56ca52020-03-10 23:18:22 +1030327 __mctp_free(mctp->busses);
328 __mctp_free(mctp);
329}
330
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800331int mctp_set_rx_all(struct mctp *mctp, mctp_rx_fn fn, void *data)
332{
333 mctp->message_rx = fn;
334 mctp->message_rx_data = data;
335 return 0;
336}
337
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600338static struct mctp_bus *find_bus_for_eid(struct mctp *mctp, mctp_eid_t dest
339 __attribute__((unused)))
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800340{
Brad Bishop663ec392021-10-07 21:16:48 -0400341 if (mctp->n_busses == 0)
342 return NULL;
343
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800344 /* for now, just use the first bus. For full routing support,
345 * we will need a table of neighbours */
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800346 return &mctp->busses[0];
347}
348
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600349int mctp_register_bus(struct mctp *mctp, struct mctp_binding *binding,
350 mctp_eid_t eid)
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800351{
Andrew Jeffery3e8a12a2020-06-05 16:08:30 +0930352 int rc = 0;
353
Jeremy Kerr7520cec2019-03-01 07:13:18 +0800354 /* todo: multiple busses */
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800355 assert(mctp->n_busses == 0);
356 mctp->n_busses = 1;
Andrew Jeffery3e8a12a2020-06-05 16:08:30 +0930357
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800358 mctp->busses = __mctp_alloc(sizeof(struct mctp_bus));
Andrew Jeffery3e8a12a2020-06-05 16:08:30 +0930359 if (!mctp->busses)
360 return -ENOMEM;
361
James Feist62d72362019-12-13 13:43:32 -0800362 memset(mctp->busses, 0, sizeof(struct mctp_bus));
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800363 mctp->busses[0].binding = binding;
364 mctp->busses[0].eid = eid;
Jeremy Kerr7520cec2019-03-01 07:13:18 +0800365 binding->bus = &mctp->busses[0];
Jeremy Kerr0a00dca2019-03-01 08:01:35 +0800366 binding->mctp = mctp;
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800367 mctp->route_policy = ROUTE_ENDPOINT;
Jeremy Kerr3b36d172019-09-04 11:56:09 +0800368
Andrew Jeffery3e8a12a2020-06-05 16:08:30 +0930369 if (binding->start) {
370 rc = binding->start(binding);
371 if (rc < 0) {
372 mctp_prerr("Failed to start binding: %d", rc);
Andrew Jeffery19275232021-01-29 14:13:25 +1030373 binding->bus = NULL;
Andrew Jeffery3e8a12a2020-06-05 16:08:30 +0930374 __mctp_free(mctp->busses);
375 mctp->busses = NULL;
Andrew Jeffery2304c832021-01-29 11:52:49 +1030376 mctp->n_busses = 0;
Andrew Jeffery3e8a12a2020-06-05 16:08:30 +0930377 }
378 }
Jeremy Kerr3b36d172019-09-04 11:56:09 +0800379
Andrew Jeffery3e8a12a2020-06-05 16:08:30 +0930380 return rc;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800381}
382
Andrew Jeffery2094c3c2021-08-26 12:32:46 +0930383void mctp_unregister_bus(struct mctp *mctp, struct mctp_binding *binding)
384{
385 /*
386 * We only support one bus right now; once the call completes we will
387 * have no more busses
388 */
389 mctp->n_busses = 0;
390 binding->mctp = NULL;
391 binding->bus = NULL;
392 free(mctp->busses);
393}
394
Patrick Williamsa721c2d2022-12-04 14:30:26 -0600395int mctp_bridge_busses(struct mctp *mctp, struct mctp_binding *b1,
396 struct mctp_binding *b2)
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800397{
Andrew Jeffery19275232021-01-29 14:13:25 +1030398 int rc = 0;
399
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800400 assert(mctp->n_busses == 0);
401 mctp->busses = __mctp_alloc(2 * sizeof(struct mctp_bus));
Helen Huanga523bcc2021-05-19 15:44:56 +0800402 if (!mctp->busses)
403 return -ENOMEM;
James Feist62d72362019-12-13 13:43:32 -0800404 memset(mctp->busses, 0, 2 * sizeof(struct mctp_bus));
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800405 mctp->n_busses = 2;
406 mctp->busses[0].binding = b1;
407 b1->bus = &mctp->busses[0];
408 b1->mctp = mctp;
409 mctp->busses[1].binding = b2;
410 b2->bus = &mctp->busses[1];
411 b2->mctp = mctp;
412
413 mctp->route_policy = ROUTE_BRIDGE;
Jeremy Kerr3b36d172019-09-04 11:56:09 +0800414
Andrew Jeffery19275232021-01-29 14:13:25 +1030415 if (b1->start) {
416 rc = b1->start(b1);
417 if (rc < 0) {
418 mctp_prerr("Failed to start bridged bus %s: %d",
419 b1->name, rc);
420 goto done;
421 }
422 }
Jeremy Kerr3b36d172019-09-04 11:56:09 +0800423
Andrew Jeffery19275232021-01-29 14:13:25 +1030424 if (b2->start) {
425 rc = b2->start(b2);
426 if (rc < 0) {
427 mctp_prerr("Failed to start bridged bus %s: %d",
428 b2->name, rc);
429 goto done;
430 }
431 }
Jeremy Kerr3b36d172019-09-04 11:56:09 +0800432
Andrew Jeffery19275232021-01-29 14:13:25 +1030433done:
434 return rc;
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800435}
436
Wiktor GoĊ‚gowskiba6727e2020-03-13 18:25:01 +0100437static inline bool mctp_ctrl_cmd_is_transport(struct mctp_ctrl_msg_hdr *hdr)
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800438{
Wiktor GoĊ‚gowskiba6727e2020-03-13 18:25:01 +0100439 return ((hdr->command_code >= MCTP_CTRL_CMD_FIRST_TRANSPORT) &&
440 (hdr->command_code <= MCTP_CTRL_CMD_LAST_TRANSPORT));
441}
442
Andrew Jefferyb93b6112020-06-05 14:13:44 +0930443static bool mctp_ctrl_handle_msg(struct mctp_bus *bus, mctp_eid_t src,
Sumanth Bhatf39c3852022-01-10 17:04:10 +0530444 uint8_t msg_tag, bool tag_owner, void *buffer,
445 size_t length)
Wiktor GoĊ‚gowskiba6727e2020-03-13 18:25:01 +0100446{
447 struct mctp_ctrl_msg_hdr *msg_hdr = buffer;
448
449 /*
450 * Control message is received. If a transport control message handler
451 * is provided, it will called. If there is no dedicated handler, this
452 * function returns false and data can be handled by the generic
453 * message handler. The transport control message handler will be
454 * provided with messages in the command range 0xF0 - 0xFF.
455 */
456 if (mctp_ctrl_cmd_is_transport(msg_hdr)) {
457 if (bus->binding->control_rx != NULL) {
458 /* MCTP bus binding handler */
Sumanth Bhatf39c3852022-01-10 17:04:10 +0530459 bus->binding->control_rx(src, msg_tag, tag_owner,
Wiktor GoĊ‚gowskiba6727e2020-03-13 18:25:01 +0100460 bus->binding->control_rx_data,
461 buffer, length);
462 return true;
463 }
464 }
465
466 /*
467 * Command was not handled, due to lack of specific callback.
468 * It will be passed to regular message_rx handler.
469 */
470 return false;
471}
472
473static inline bool mctp_rx_dest_is_local(struct mctp_bus *bus, mctp_eid_t dest)
474{
475 return dest == bus->eid || dest == MCTP_EID_NULL ||
476 dest == MCTP_EID_BROADCAST;
477}
478
479static inline bool mctp_ctrl_cmd_is_request(struct mctp_ctrl_msg_hdr *hdr)
480{
481 return hdr->ic_msg_type == MCTP_CTRL_HDR_MSG_TYPE &&
482 hdr->rq_dgram_inst & MCTP_CTRL_HDR_FLAG_REQUEST;
483}
484
485/*
486 * Receive the complete MCTP message and route it.
487 * Asserts:
488 * 'buf' is not NULL.
489 */
490static void mctp_rx(struct mctp *mctp, struct mctp_bus *bus, mctp_eid_t src,
Sumanth Bhatf39c3852022-01-10 17:04:10 +0530491 mctp_eid_t dest, bool tag_owner, uint8_t msg_tag, void *buf,
492 size_t len)
Wiktor GoĊ‚gowskiba6727e2020-03-13 18:25:01 +0100493{
494 assert(buf != NULL);
495
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800496 if (mctp->route_policy == ROUTE_ENDPOINT &&
Wiktor GoĊ‚gowskiba6727e2020-03-13 18:25:01 +0100497 mctp_rx_dest_is_local(bus, dest)) {
498 /* Handle MCTP Control Messages: */
499 if (len >= sizeof(struct mctp_ctrl_msg_hdr)) {
500 struct mctp_ctrl_msg_hdr *msg_hdr = buf;
501
502 /*
503 * Identify if this is a control request message.
504 * See DSP0236 v1.3.0 sec. 11.5.
505 */
506 if (mctp_ctrl_cmd_is_request(msg_hdr)) {
507 bool handled;
Sumanth Bhatf39c3852022-01-10 17:04:10 +0530508 handled = mctp_ctrl_handle_msg(
509 bus, src, msg_tag, tag_owner, buf, len);
Wiktor GoĊ‚gowskiba6727e2020-03-13 18:25:01 +0100510 if (handled)
511 return;
512 }
513 }
Sumanth Bhatf39c3852022-01-10 17:04:10 +0530514
Wiktor GoĊ‚gowskiba6727e2020-03-13 18:25:01 +0100515 if (mctp->message_rx)
Sumanth Bhatf39c3852022-01-10 17:04:10 +0530516 mctp->message_rx(src, tag_owner, msg_tag,
517 mctp->message_rx_data, buf, len);
Wiktor GoĊ‚gowskiba6727e2020-03-13 18:25:01 +0100518 }
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800519
520 if (mctp->route_policy == ROUTE_BRIDGE) {
521 int i;
522
523 for (i = 0; i < mctp->n_busses; i++) {
524 struct mctp_bus *dest_bus = &mctp->busses[i];
525 if (dest_bus == bus)
526 continue;
527
Sumanth Bhatf39c3852022-01-10 17:04:10 +0530528 mctp_message_tx_on_bus(dest_bus, src, dest, tag_owner,
529 msg_tag, buf, len);
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800530 }
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800531 }
532}
533
Jeremy Kerr0a00dca2019-03-01 08:01:35 +0800534void mctp_bus_rx(struct mctp_binding *binding, struct mctp_pktbuf *pkt)
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800535{
Jeremy Kerr7520cec2019-03-01 07:13:18 +0800536 struct mctp_bus *bus = binding->bus;
Jeremy Kerr0a00dca2019-03-01 08:01:35 +0800537 struct mctp *mctp = binding->mctp;
Ed Tanousc2def9f2019-02-21 08:33:08 -0800538 uint8_t flags, exp_seq, seq, tag;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800539 struct mctp_msg_ctx *ctx;
540 struct mctp_hdr *hdr;
Sumanth Bhatf39c3852022-01-10 17:04:10 +0530541 bool tag_owner;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800542 size_t len;
543 void *p;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800544 int rc;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800545
Jeremy Kerr7520cec2019-03-01 07:13:18 +0800546 assert(bus);
547
Sumanth Bhatd97869d2020-07-02 00:46:13 +0530548 /* Drop packet if it was smaller than mctp hdr size */
549 if (mctp_pktbuf_size(pkt) <= sizeof(struct mctp_hdr))
550 goto out;
551
Andrew Jeffery5d3d4e62021-08-20 16:44:40 +0930552 if (mctp->capture)
Rashmica Guptaf2988972022-11-09 12:26:44 +1100553 mctp->capture(pkt, MCTP_MESSAGE_CAPTURE_INCOMING,
554 mctp->capture_data);
Andrew Jeffery5d3d4e62021-08-20 16:44:40 +0930555
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800556 hdr = mctp_pktbuf_hdr(pkt);
557
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800558 /* small optimisation: don't bother reassembly if we're going to
559 * drop the packet in mctp_rx anyway */
560 if (mctp->route_policy == ROUTE_ENDPOINT && hdr->dest != bus->eid)
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;
Sumanth Bhatf39c3852022-01-10 17:04:10 +0530566 tag_owner =
567 (hdr->flags_seq_tag >> MCTP_HDR_TO_SHIFT) & 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}