blob: 11d47774b5b43cd4c29a60fcf52edffa7f551679 [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 {
41 uint8_t src;
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +080042 uint8_t dest;
Jeremy Kerr24db71f2019-02-07 21:37:35 +080043 uint8_t tag;
44 uint8_t last_seq;
45 void *buf;
46 size_t buf_size;
47 size_t buf_alloc_size;
Sumanth Bhat69f545f2021-05-18 09:16:43 +000048 size_t fragment_size;
Jeremy Kerr24db71f2019-02-07 21:37:35 +080049};
50
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080051struct mctp {
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +080052 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 */
56 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 */
60 mctp_capture_fn capture;
61 void *capture_data;
62
Jeremy Kerr24db71f2019-02-07 21:37:35 +080063 /* Message reassembly.
64 * @todo: flexible context count
65 */
66 struct mctp_msg_ctx msg_ctxs[16];
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +080067
68 enum {
69 ROUTE_ENDPOINT,
70 ROUTE_BRIDGE,
71 } 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
76#define BUILD_ASSERT(x) \
77 do { (void)sizeof(char[0-(!(x))]); } while (0)
78#endif
79
Jeremy Kerr24db71f2019-02-07 21:37:35 +080080#ifndef ARRAY_SIZE
81#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
82#endif
83
Sumanth Bhat2c820c52020-07-02 00:26:25 +053084/* 64kb should be sufficient for a single message. Applications
85 * requiring higher sizes can override by setting max_message_size.*/
86#ifndef MCTP_MAX_MESSAGE_SIZE
87#define MCTP_MAX_MESSAGE_SIZE 65536
88#endif
89
Andrew Jefferyb93b6112020-06-05 14:13:44 +093090static int mctp_message_tx_on_bus(struct mctp_bus *bus, mctp_eid_t src,
91 mctp_eid_t dest, void *msg, size_t msg_len);
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +080092
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +080093struct mctp_pktbuf *mctp_pktbuf_alloc(struct mctp_binding *binding, size_t len)
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080094{
95 struct mctp_pktbuf *buf;
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +080096 size_t size;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080097
Andrew Jeffery39da3d02021-03-12 16:51:26 +103098 size = binding->pkt_size + binding->pkt_header + binding->pkt_trailer;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080099
100 /* todo: pools */
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +0800101 buf = __mctp_alloc(sizeof(*buf) + size);
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800102
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +0800103 buf->size = size;
Andrew Jeffery39da3d02021-03-12 16:51:26 +1030104 buf->start = binding->pkt_header;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800105 buf->end = buf->start + len;
106 buf->mctp_hdr_off = buf->start;
Jeremy Kerrdd109f12019-04-04 11:46:49 +0800107 buf->next = NULL;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800108
109 return buf;
110}
111
112void mctp_pktbuf_free(struct mctp_pktbuf *pkt)
113{
114 __mctp_free(pkt);
115}
116
117struct mctp_hdr *mctp_pktbuf_hdr(struct mctp_pktbuf *pkt)
118{
119 return (void *)pkt->data + pkt->mctp_hdr_off;
120}
121
122void *mctp_pktbuf_data(struct mctp_pktbuf *pkt)
123{
124 return (void *)pkt->data + pkt->mctp_hdr_off + sizeof(struct mctp_hdr);
125}
126
Andrew Jefferyb942e3a2020-06-23 09:54:02 +0930127size_t mctp_pktbuf_size(struct mctp_pktbuf *pkt)
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800128{
129 return pkt->end - pkt->start;
130}
131
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +0800132void *mctp_pktbuf_alloc_start(struct mctp_pktbuf *pkt, size_t size)
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800133{
134 assert(size <= pkt->start);
135 pkt->start -= size;
136 return pkt->data + pkt->start;
137}
138
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +0800139void *mctp_pktbuf_alloc_end(struct mctp_pktbuf *pkt, size_t size)
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800140{
141 void *buf;
142
Andrew Jeffery3ac70d62020-07-01 00:50:44 +0930143 assert(size <= (pkt->size - pkt->end));
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800144 buf = pkt->data + pkt->end;
145 pkt->end += size;
146 return buf;
147}
148
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +0800149int mctp_pktbuf_push(struct mctp_pktbuf *pkt, void *data, size_t len)
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800150{
151 void *p;
152
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +0800153 if (pkt->end + len > pkt->size)
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800154 return -1;
155
156 p = pkt->data + pkt->end;
157
158 pkt->end += len;
159 memcpy(p, data, len);
160
161 return 0;
162}
163
Andrew Jefferyeba19a32021-03-09 23:09:40 +1030164void *mctp_pktbuf_pop(struct mctp_pktbuf *pkt, size_t len)
165{
166 if (len > mctp_pktbuf_size(pkt))
167 return NULL;
168
169 pkt->end -= len;
170 return pkt->data + pkt->end;
171}
172
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800173/* Message reassembly */
174static struct mctp_msg_ctx *mctp_msg_ctx_lookup(struct mctp *mctp,
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800175 uint8_t src, uint8_t dest, uint8_t tag)
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800176{
177 unsigned int i;
178
179 /* @todo: better lookup, if we add support for more outstanding
180 * message contexts */
181 for (i = 0; i < ARRAY_SIZE(mctp->msg_ctxs); i++) {
182 struct mctp_msg_ctx *ctx = &mctp->msg_ctxs[i];
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800183 if (ctx->src == src && ctx->dest == dest && ctx->tag == tag)
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800184 return ctx;
185 }
186
187 return NULL;
188}
189
190static struct mctp_msg_ctx *mctp_msg_ctx_create(struct mctp *mctp,
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800191 uint8_t src, uint8_t dest, uint8_t tag)
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800192{
Jeremy Kerr11a234e2019-02-27 17:59:53 +0800193 struct mctp_msg_ctx *ctx = NULL;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800194 unsigned int i;
195
196 for (i = 0; i < ARRAY_SIZE(mctp->msg_ctxs); i++) {
197 struct mctp_msg_ctx *tmp = &mctp->msg_ctxs[i];
198 if (!tmp->src) {
199 ctx = tmp;
200 break;
201 }
202 }
203
204 if (!ctx)
205 return NULL;
206
207 ctx->src = src;
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800208 ctx->dest = dest;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800209 ctx->tag = tag;
Jeremy Kerr9a3da812019-08-02 15:57:53 +0800210 ctx->buf_size = 0;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800211
212 return ctx;
213}
214
215static void mctp_msg_ctx_drop(struct mctp_msg_ctx *ctx)
216{
217 ctx->src = 0;
218}
219
220static void mctp_msg_ctx_reset(struct mctp_msg_ctx *ctx)
221{
222 ctx->buf_size = 0;
Sumanth Bhat69f545f2021-05-18 09:16:43 +0000223 ctx->fragment_size = 0;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800224}
225
226static int mctp_msg_ctx_add_pkt(struct mctp_msg_ctx *ctx,
Sumanth Bhat2c820c52020-07-02 00:26:25 +0530227 struct mctp_pktbuf *pkt, size_t max_size)
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800228{
229 size_t len;
230
231 len = mctp_pktbuf_size(pkt) - sizeof(struct mctp_hdr);
232
Sumanth Bhatbc79c242021-06-16 12:36:56 +0530233 if (len + ctx->buf_size < ctx->buf_size) {
234 return -1;
235 }
236
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800237 if (ctx->buf_size + len > ctx->buf_alloc_size) {
238 size_t new_alloc_size;
Andrew Jeffery00ecc6c2020-03-10 23:16:53 +1030239 void *lbuf;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800240
Andrew Jeffery5a508912020-11-03 22:21:45 +1030241 /* @todo: finer-grained allocation */
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800242 if (!ctx->buf_alloc_size) {
Andrew Jefferyc2b833e2020-10-28 14:28:37 +1030243 new_alloc_size = MAX(len, 4096UL);
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800244 } else {
Sumanth Bhatbc79c242021-06-16 12:36:56 +0530245 new_alloc_size = MAX(ctx->buf_alloc_size * 2, len + ctx->buf_size);
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800246 }
Andrew Jeffery00ecc6c2020-03-10 23:16:53 +1030247
Sumanth Bhat2c820c52020-07-02 00:26:25 +0530248 /* Don't allow heap to grow beyond a limit */
249 if (new_alloc_size > max_size)
250 return -1;
251
252
Andrew Jeffery00ecc6c2020-03-10 23:16:53 +1030253 lbuf = __mctp_realloc(ctx->buf, new_alloc_size);
254 if (lbuf) {
255 ctx->buf = lbuf;
256 ctx->buf_alloc_size = new_alloc_size;
257 } else {
258 __mctp_free(ctx->buf);
259 return -1;
260 }
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800261 }
262
263 memcpy(ctx->buf + ctx->buf_size, mctp_pktbuf_data(pkt), len);
264 ctx->buf_size += len;
265
266 return 0;
267}
268
269/* Core API functions */
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800270struct mctp *mctp_init(void)
271{
272 struct mctp *mctp;
273
274 mctp = __mctp_alloc(sizeof(*mctp));
Sumanth Bhat96d54492020-07-14 17:10:04 +0530275
276 if(!mctp)
277 return NULL;
278
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800279 memset(mctp, 0, sizeof(*mctp));
Sumanth Bhat2c820c52020-07-02 00:26:25 +0530280 mctp->max_message_size = MCTP_MAX_MESSAGE_SIZE;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800281
282 return mctp;
283}
284
Sumanth Bhat2c820c52020-07-02 00:26:25 +0530285void mctp_set_max_message_size(struct mctp *mctp, size_t message_size)
286{
287 mctp->max_message_size = message_size;
288}
289
Andrew Jeffery5d3d4e62021-08-20 16:44:40 +0930290void mctp_set_capture_handler(struct mctp *mctp, mctp_capture_fn fn, void *user)
291{
292 mctp->capture = fn;
293 mctp->capture_data = user;
294}
295
Andrew Jeffery3ae89dc2021-01-28 15:24:36 +1030296static void mctp_bus_destroy(struct mctp_bus *bus)
297{
298 while (bus->tx_queue_head) {
299 struct mctp_pktbuf *curr = bus->tx_queue_head;
300
301 bus->tx_queue_head = curr->next;
302 mctp_pktbuf_free(curr);
303 }
304}
305
Andrew Jefferyfa56ca52020-03-10 23:18:22 +1030306void mctp_destroy(struct mctp *mctp)
307{
Andrew Jefferyb93b6112020-06-05 14:13:44 +0930308 size_t i;
Andrew Jefferyfa56ca52020-03-10 23:18:22 +1030309
310 /* Cleanup message assembly contexts */
Andrew Jefferyb93b6112020-06-05 14:13:44 +0930311 BUILD_ASSERT(ARRAY_SIZE(mctp->msg_ctxs) < SIZE_MAX);
Andrew Jefferyfa56ca52020-03-10 23:18:22 +1030312 for (i = 0; i < ARRAY_SIZE(mctp->msg_ctxs); i++) {
313 struct mctp_msg_ctx *tmp = &mctp->msg_ctxs[i];
314 if (tmp->buf)
315 __mctp_free(tmp->buf);
316 }
317
Andrew Jeffery3ae89dc2021-01-28 15:24:36 +1030318 while (mctp->n_busses--)
319 mctp_bus_destroy(&mctp->busses[mctp->n_busses]);
320
Andrew Jefferyfa56ca52020-03-10 23:18:22 +1030321 __mctp_free(mctp->busses);
322 __mctp_free(mctp);
323}
324
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800325int mctp_set_rx_all(struct mctp *mctp, mctp_rx_fn fn, void *data)
326{
327 mctp->message_rx = fn;
328 mctp->message_rx_data = data;
329 return 0;
330}
331
332static struct mctp_bus *find_bus_for_eid(struct mctp *mctp,
333 mctp_eid_t dest __attribute__((unused)))
334{
Brad Bishop663ec392021-10-07 21:16:48 -0400335 if (mctp->n_busses == 0)
336 return NULL;
337
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800338 /* for now, just use the first bus. For full routing support,
339 * we will need a table of neighbours */
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800340 return &mctp->busses[0];
341}
342
Jeremy Kerr7520cec2019-03-01 07:13:18 +0800343int mctp_register_bus(struct mctp *mctp,
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800344 struct mctp_binding *binding,
345 mctp_eid_t eid)
346{
Andrew Jeffery3e8a12a2020-06-05 16:08:30 +0930347 int rc = 0;
348
Jeremy Kerr7520cec2019-03-01 07:13:18 +0800349 /* todo: multiple busses */
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800350 assert(mctp->n_busses == 0);
351 mctp->n_busses = 1;
Andrew Jeffery3e8a12a2020-06-05 16:08:30 +0930352
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800353 mctp->busses = __mctp_alloc(sizeof(struct mctp_bus));
Andrew Jeffery3e8a12a2020-06-05 16:08:30 +0930354 if (!mctp->busses)
355 return -ENOMEM;
356
James Feist62d72362019-12-13 13:43:32 -0800357 memset(mctp->busses, 0, sizeof(struct mctp_bus));
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800358 mctp->busses[0].binding = binding;
359 mctp->busses[0].eid = eid;
Jeremy Kerr7520cec2019-03-01 07:13:18 +0800360 binding->bus = &mctp->busses[0];
Jeremy Kerr0a00dca2019-03-01 08:01:35 +0800361 binding->mctp = mctp;
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800362 mctp->route_policy = ROUTE_ENDPOINT;
Jeremy Kerr3b36d172019-09-04 11:56:09 +0800363
Andrew Jeffery3e8a12a2020-06-05 16:08:30 +0930364 if (binding->start) {
365 rc = binding->start(binding);
366 if (rc < 0) {
367 mctp_prerr("Failed to start binding: %d", rc);
Andrew Jeffery19275232021-01-29 14:13:25 +1030368 binding->bus = NULL;
Andrew Jeffery3e8a12a2020-06-05 16:08:30 +0930369 __mctp_free(mctp->busses);
370 mctp->busses = NULL;
Andrew Jeffery2304c832021-01-29 11:52:49 +1030371 mctp->n_busses = 0;
Andrew Jeffery3e8a12a2020-06-05 16:08:30 +0930372 }
373 }
Jeremy Kerr3b36d172019-09-04 11:56:09 +0800374
Andrew Jeffery3e8a12a2020-06-05 16:08:30 +0930375 return rc;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800376}
377
Andrew Jeffery2094c3c2021-08-26 12:32:46 +0930378void mctp_unregister_bus(struct mctp *mctp, struct mctp_binding *binding)
379{
380 /*
381 * We only support one bus right now; once the call completes we will
382 * have no more busses
383 */
384 mctp->n_busses = 0;
385 binding->mctp = NULL;
386 binding->bus = NULL;
387 free(mctp->busses);
388}
389
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800390int mctp_bridge_busses(struct mctp *mctp,
391 struct mctp_binding *b1, struct mctp_binding *b2)
392{
Andrew Jeffery19275232021-01-29 14:13:25 +1030393 int rc = 0;
394
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800395 assert(mctp->n_busses == 0);
396 mctp->busses = __mctp_alloc(2 * sizeof(struct mctp_bus));
Helen Huanga523bcc2021-05-19 15:44:56 +0800397 if (!mctp->busses)
398 return -ENOMEM;
James Feist62d72362019-12-13 13:43:32 -0800399 memset(mctp->busses, 0, 2 * sizeof(struct mctp_bus));
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800400 mctp->n_busses = 2;
401 mctp->busses[0].binding = b1;
402 b1->bus = &mctp->busses[0];
403 b1->mctp = mctp;
404 mctp->busses[1].binding = b2;
405 b2->bus = &mctp->busses[1];
406 b2->mctp = mctp;
407
408 mctp->route_policy = ROUTE_BRIDGE;
Jeremy Kerr3b36d172019-09-04 11:56:09 +0800409
Andrew Jeffery19275232021-01-29 14:13:25 +1030410 if (b1->start) {
411 rc = b1->start(b1);
412 if (rc < 0) {
413 mctp_prerr("Failed to start bridged bus %s: %d",
414 b1->name, rc);
415 goto done;
416 }
417 }
Jeremy Kerr3b36d172019-09-04 11:56:09 +0800418
Andrew Jeffery19275232021-01-29 14:13:25 +1030419 if (b2->start) {
420 rc = b2->start(b2);
421 if (rc < 0) {
422 mctp_prerr("Failed to start bridged bus %s: %d",
423 b2->name, rc);
424 goto done;
425 }
426 }
Jeremy Kerr3b36d172019-09-04 11:56:09 +0800427
Andrew Jeffery19275232021-01-29 14:13:25 +1030428done:
429 return rc;
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800430}
431
Wiktor GoĊ‚gowskiba6727e2020-03-13 18:25:01 +0100432static inline bool mctp_ctrl_cmd_is_transport(struct mctp_ctrl_msg_hdr *hdr)
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800433{
Wiktor GoĊ‚gowskiba6727e2020-03-13 18:25:01 +0100434 return ((hdr->command_code >= MCTP_CTRL_CMD_FIRST_TRANSPORT) &&
435 (hdr->command_code <= MCTP_CTRL_CMD_LAST_TRANSPORT));
436}
437
Andrew Jefferyb93b6112020-06-05 14:13:44 +0930438static bool mctp_ctrl_handle_msg(struct mctp_bus *bus, mctp_eid_t src,
439 void *buffer, size_t length)
Wiktor GoĊ‚gowskiba6727e2020-03-13 18:25:01 +0100440{
441 struct mctp_ctrl_msg_hdr *msg_hdr = buffer;
442
443 /*
444 * Control message is received. If a transport control message handler
445 * is provided, it will called. If there is no dedicated handler, this
446 * function returns false and data can be handled by the generic
447 * message handler. The transport control message handler will be
448 * provided with messages in the command range 0xF0 - 0xFF.
449 */
450 if (mctp_ctrl_cmd_is_transport(msg_hdr)) {
451 if (bus->binding->control_rx != NULL) {
452 /* MCTP bus binding handler */
453 bus->binding->control_rx(src,
454 bus->binding->control_rx_data,
455 buffer, length);
456 return true;
457 }
458 }
459
460 /*
461 * Command was not handled, due to lack of specific callback.
462 * It will be passed to regular message_rx handler.
463 */
464 return false;
465}
466
467static inline bool mctp_rx_dest_is_local(struct mctp_bus *bus, mctp_eid_t dest)
468{
469 return dest == bus->eid || dest == MCTP_EID_NULL ||
470 dest == MCTP_EID_BROADCAST;
471}
472
473static inline bool mctp_ctrl_cmd_is_request(struct mctp_ctrl_msg_hdr *hdr)
474{
475 return hdr->ic_msg_type == MCTP_CTRL_HDR_MSG_TYPE &&
476 hdr->rq_dgram_inst & MCTP_CTRL_HDR_FLAG_REQUEST;
477}
478
479/*
480 * Receive the complete MCTP message and route it.
481 * Asserts:
482 * 'buf' is not NULL.
483 */
484static void mctp_rx(struct mctp *mctp, struct mctp_bus *bus, mctp_eid_t src,
485 mctp_eid_t dest, void *buf, size_t len)
486{
487 assert(buf != NULL);
488
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800489 if (mctp->route_policy == ROUTE_ENDPOINT &&
Wiktor GoĊ‚gowskiba6727e2020-03-13 18:25:01 +0100490 mctp_rx_dest_is_local(bus, dest)) {
491 /* Handle MCTP Control Messages: */
492 if (len >= sizeof(struct mctp_ctrl_msg_hdr)) {
493 struct mctp_ctrl_msg_hdr *msg_hdr = buf;
494
495 /*
496 * Identify if this is a control request message.
497 * See DSP0236 v1.3.0 sec. 11.5.
498 */
499 if (mctp_ctrl_cmd_is_request(msg_hdr)) {
500 bool handled;
Andrew Jefferyb93b6112020-06-05 14:13:44 +0930501 handled = mctp_ctrl_handle_msg(bus, src, buf,
502 len);
Wiktor GoĊ‚gowskiba6727e2020-03-13 18:25:01 +0100503 if (handled)
504 return;
505 }
506 }
507 if (mctp->message_rx)
508 mctp->message_rx(src, mctp->message_rx_data, buf, len);
509 }
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800510
511 if (mctp->route_policy == ROUTE_BRIDGE) {
512 int i;
513
514 for (i = 0; i < mctp->n_busses; i++) {
515 struct mctp_bus *dest_bus = &mctp->busses[i];
516 if (dest_bus == bus)
517 continue;
518
Andrew Jefferyb93b6112020-06-05 14:13:44 +0930519 mctp_message_tx_on_bus(dest_bus, src, dest, buf, len);
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800520 }
521
522 }
523}
524
Jeremy Kerr0a00dca2019-03-01 08:01:35 +0800525void mctp_bus_rx(struct mctp_binding *binding, struct mctp_pktbuf *pkt)
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800526{
Jeremy Kerr7520cec2019-03-01 07:13:18 +0800527 struct mctp_bus *bus = binding->bus;
Jeremy Kerr0a00dca2019-03-01 08:01:35 +0800528 struct mctp *mctp = binding->mctp;
Ed Tanousc2def9f2019-02-21 08:33:08 -0800529 uint8_t flags, exp_seq, seq, tag;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800530 struct mctp_msg_ctx *ctx;
531 struct mctp_hdr *hdr;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800532 size_t len;
533 void *p;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800534 int rc;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800535
Jeremy Kerr7520cec2019-03-01 07:13:18 +0800536 assert(bus);
537
Sumanth Bhatd97869d2020-07-02 00:46:13 +0530538 /* Drop packet if it was smaller than mctp hdr size */
539 if (mctp_pktbuf_size(pkt) <= sizeof(struct mctp_hdr))
540 goto out;
541
Andrew Jeffery5d3d4e62021-08-20 16:44:40 +0930542 if (mctp->capture)
543 mctp->capture(pkt, mctp->capture_data);
544
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800545 hdr = mctp_pktbuf_hdr(pkt);
546
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800547 /* small optimisation: don't bother reassembly if we're going to
548 * drop the packet in mctp_rx anyway */
549 if (mctp->route_policy == ROUTE_ENDPOINT && hdr->dest != bus->eid)
Jeremy Kerrc1693af2019-08-05 14:30:59 +0800550 goto out;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800551
552 flags = hdr->flags_seq_tag & (MCTP_HDR_FLAG_SOM | MCTP_HDR_FLAG_EOM);
553 tag = (hdr->flags_seq_tag >> MCTP_HDR_TAG_SHIFT) & MCTP_HDR_TAG_MASK;
554 seq = (hdr->flags_seq_tag >> MCTP_HDR_SEQ_SHIFT) & MCTP_HDR_SEQ_MASK;
555
556 switch (flags) {
557 case MCTP_HDR_FLAG_SOM | MCTP_HDR_FLAG_EOM:
558 /* single-packet message - send straight up to rx function,
559 * no need to create a message context */
560 len = pkt->end - pkt->mctp_hdr_off - sizeof(struct mctp_hdr);
Andrew Jefferyb4ae00b2021-01-18 15:52:09 +1030561 p = pkt->data + pkt->mctp_hdr_off + sizeof(struct mctp_hdr);
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800562 mctp_rx(mctp, bus, hdr->src, hdr->dest, p, len);
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800563 break;
564
565 case MCTP_HDR_FLAG_SOM:
566 /* start of a new message - start the new context for
567 * future message reception. If an existing context is
568 * already present, drop it. */
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800569 ctx = mctp_msg_ctx_lookup(mctp, hdr->src, hdr->dest, tag);
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800570 if (ctx) {
571 mctp_msg_ctx_reset(ctx);
572 } else {
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800573 ctx = mctp_msg_ctx_create(mctp,
574 hdr->src, hdr->dest, tag);
Sumanth Bhat34d4c962021-06-16 12:50:48 +0530575 /* If context creation fails due to exhaution of contexts we
576 * can support, drop the packet */
577 if (!ctx) {
578 mctp_prdebug("Context buffers exhausted.");
579 goto out;
580 }
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800581 }
582
Sumanth Bhat69f545f2021-05-18 09:16:43 +0000583 /* Save the fragment size, subsequent middle fragments
584 * should of the same size */
585 ctx->fragment_size = mctp_pktbuf_size(pkt);
586
Sumanth Bhat2c820c52020-07-02 00:26:25 +0530587 rc = mctp_msg_ctx_add_pkt(ctx, pkt, mctp->max_message_size);
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800588 if (rc) {
589 mctp_msg_ctx_drop(ctx);
590 } else {
591 ctx->last_seq = seq;
592 }
593
594 break;
595
596 case MCTP_HDR_FLAG_EOM:
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800597 ctx = mctp_msg_ctx_lookup(mctp, hdr->src, hdr->dest, tag);
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800598 if (!ctx)
Jeremy Kerrc1693af2019-08-05 14:30:59 +0800599 goto out;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800600
Ed Tanousc2def9f2019-02-21 08:33:08 -0800601 exp_seq = (ctx->last_seq + 1) % 4;
602
603 if (exp_seq != seq) {
604 mctp_prdebug(
605 "Sequence number %d does not match expected %d",
606 seq, exp_seq);
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800607 mctp_msg_ctx_drop(ctx);
Jeremy Kerrc1693af2019-08-05 14:30:59 +0800608 goto out;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800609 }
610
Sumanth Bhat69f545f2021-05-18 09:16:43 +0000611 len = mctp_pktbuf_size(pkt);
612
613 if (len > ctx->fragment_size) {
614 mctp_prdebug("Unexpected fragment size. Expected" \
615 " less than %zu, received = %zu",
616 ctx->fragment_size, len);
617 mctp_msg_ctx_drop(ctx);
618 goto out;
619 }
620
Sumanth Bhat2c820c52020-07-02 00:26:25 +0530621 rc = mctp_msg_ctx_add_pkt(ctx, pkt, mctp->max_message_size);
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800622 if (!rc)
623 mctp_rx(mctp, bus, ctx->src, ctx->dest,
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800624 ctx->buf, ctx->buf_size);
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800625
626 mctp_msg_ctx_drop(ctx);
627 break;
Ed Tanousc2def9f2019-02-21 08:33:08 -0800628
629 case 0:
630 /* Neither SOM nor EOM */
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800631 ctx = mctp_msg_ctx_lookup(mctp, hdr->src,hdr->dest, tag);
Ed Tanousc2def9f2019-02-21 08:33:08 -0800632 if (!ctx)
Jeremy Kerrc1693af2019-08-05 14:30:59 +0800633 goto out;
Ed Tanousc2def9f2019-02-21 08:33:08 -0800634
635 exp_seq = (ctx->last_seq + 1) % 4;
636 if (exp_seq != seq) {
637 mctp_prdebug(
638 "Sequence number %d does not match expected %d",
639 seq, exp_seq);
640 mctp_msg_ctx_drop(ctx);
Jeremy Kerrc1693af2019-08-05 14:30:59 +0800641 goto out;
Ed Tanousc2def9f2019-02-21 08:33:08 -0800642 }
643
Sumanth Bhat69f545f2021-05-18 09:16:43 +0000644 len = mctp_pktbuf_size(pkt);
645
646 if (len != ctx->fragment_size) {
647 mctp_prdebug("Unexpected fragment size. Expected = %zu " \
648 "received = %zu", ctx->fragment_size, len);
649 mctp_msg_ctx_drop(ctx);
650 goto out;
651 }
652
Sumanth Bhat2c820c52020-07-02 00:26:25 +0530653 rc = mctp_msg_ctx_add_pkt(ctx, pkt, mctp->max_message_size);
Ed Tanousc2def9f2019-02-21 08:33:08 -0800654 if (rc) {
655 mctp_msg_ctx_drop(ctx);
Jeremy Kerrc1693af2019-08-05 14:30:59 +0800656 goto out;
Ed Tanousc2def9f2019-02-21 08:33:08 -0800657 }
658 ctx->last_seq = seq;
659
660 break;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800661 }
Jeremy Kerrc1693af2019-08-05 14:30:59 +0800662out:
663 mctp_pktbuf_free(pkt);
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800664}
665
Jeremy Kerr0a00dca2019-03-01 08:01:35 +0800666static int mctp_packet_tx(struct mctp_bus *bus,
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800667 struct mctp_pktbuf *pkt)
668{
Andrew Jeffery5d3d4e62021-08-20 16:44:40 +0930669 struct mctp *mctp = bus->binding->mctp;
670
Andrew Jefferyc61501c2021-01-27 23:24:18 +1030671 if (bus->state != mctp_bus_state_tx_enabled)
Jeremy Kerr1cd31182019-02-27 18:01:00 +0800672 return -1;
673
Andrew Jeffery5d3d4e62021-08-20 16:44:40 +0930674 if (mctp->capture)
675 mctp->capture(pkt, mctp->capture_data);
676
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800677 return bus->binding->tx(bus->binding, pkt);
678}
679
Jeremy Kerrcc2458d2019-03-01 08:23:33 +0800680static void mctp_send_tx_queue(struct mctp_bus *bus)
Jeremy Kerr1cd31182019-02-27 18:01:00 +0800681{
682 struct mctp_pktbuf *pkt;
683
Jeremy Kerrcc2458d2019-03-01 08:23:33 +0800684 while ((pkt = bus->tx_queue_head)) {
Jeremy Kerr1cd31182019-02-27 18:01:00 +0800685 int rc;
686
687 rc = mctp_packet_tx(bus, pkt);
688 if (rc)
689 break;
690
Jeremy Kerrcc2458d2019-03-01 08:23:33 +0800691 bus->tx_queue_head = pkt->next;
Jeremy Kerr1cd31182019-02-27 18:01:00 +0800692 mctp_pktbuf_free(pkt);
693 }
694
Jeremy Kerrcc2458d2019-03-01 08:23:33 +0800695 if (!bus->tx_queue_head)
696 bus->tx_queue_tail = NULL;
Jeremy Kerr1cd31182019-02-27 18:01:00 +0800697
698}
699
700void mctp_binding_set_tx_enabled(struct mctp_binding *binding, bool enable)
701{
702 struct mctp_bus *bus = binding->bus;
Andrew Jefferyc61501c2021-01-27 23:24:18 +1030703
704 switch(bus->state) {
705 case mctp_bus_state_constructed:
706 if (!enable)
707 return;
708
Andrew Jeffery1fa707e2021-01-28 15:22:11 +1030709 if (binding->pkt_size < MCTP_PACKET_SIZE(MCTP_BTU)) {
710 mctp_prerr("Cannot start %s binding with invalid MTU: %zu",
711 binding->name,
712 MCTP_BODY_SIZE(binding->pkt_size));
713 return;
714 }
715
Andrew Jefferyc61501c2021-01-27 23:24:18 +1030716 bus->state = mctp_bus_state_tx_enabled;
717 mctp_prinfo("%s binding started", binding->name);
718 return;
719 case mctp_bus_state_tx_enabled:
720 if (enable)
721 return;
722
723 bus->state = mctp_bus_state_tx_disabled;
724 mctp_prdebug("%s binding Tx disabled", binding->name);
725 return;
726 case mctp_bus_state_tx_disabled:
727 if (!enable)
728 return;
729
730 bus->state = mctp_bus_state_tx_enabled;
731 mctp_prdebug("%s binding Tx enabled", binding->name);
Jeremy Kerrcc2458d2019-03-01 08:23:33 +0800732 mctp_send_tx_queue(bus);
Andrew Jefferyc61501c2021-01-27 23:24:18 +1030733 return;
734 }
Jeremy Kerr1cd31182019-02-27 18:01:00 +0800735}
736
Andrew Jefferyb93b6112020-06-05 14:13:44 +0930737static int mctp_message_tx_on_bus(struct mctp_bus *bus, mctp_eid_t src,
738 mctp_eid_t dest, void *msg, size_t msg_len)
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800739{
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +0800740 size_t max_payload_len, payload_len, p;
Jeremy Kerr1cd31182019-02-27 18:01:00 +0800741 struct mctp_pktbuf *pkt;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800742 struct mctp_hdr *hdr;
Jeremy Kerrc855d7b2019-08-01 21:18:09 +0800743 int i;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800744
Andrew Jefferyc61501c2021-01-27 23:24:18 +1030745 if (bus->state == mctp_bus_state_constructed)
746 return -ENXIO;
747
Andrew Jeffery1fa707e2021-01-28 15:22:11 +1030748 max_payload_len = MCTP_BODY_SIZE(bus->binding->pkt_size);
749
750 {
751 const bool valid_mtu = max_payload_len >= MCTP_BTU;
752 assert(valid_mtu);
753 if (!valid_mtu)
754 return -EINVAL;
755 }
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800756
Andrew Jeffery298865f2020-02-06 11:51:29 +1030757 mctp_prdebug("%s: Generating packets for transmission of %zu byte message from %hhu to %hhu",
758 __func__, msg_len, src, dest);
759
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800760 /* queue up packets, each of max MCTP_MTU size */
Jeremy Kerrc855d7b2019-08-01 21:18:09 +0800761 for (p = 0, i = 0; p < msg_len; i++) {
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +0800762 payload_len = msg_len - p;
763 if (payload_len > max_payload_len)
764 payload_len = max_payload_len;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800765
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800766 pkt = mctp_pktbuf_alloc(bus->binding,
767 payload_len + sizeof(*hdr));
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800768 hdr = mctp_pktbuf_hdr(pkt);
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800769
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800770 /* todo: tags */
771 hdr->ver = bus->binding->version & 0xf;
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800772 hdr->dest = dest;
773 hdr->src = src;
Jeremy Kerrc855d7b2019-08-01 21:18:09 +0800774 hdr->flags_seq_tag = MCTP_HDR_FLAG_TO |
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800775 (0 << MCTP_HDR_TAG_SHIFT);
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800776
Jeremy Kerrc855d7b2019-08-01 21:18:09 +0800777 if (i == 0)
778 hdr->flags_seq_tag |= MCTP_HDR_FLAG_SOM;
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +0800779 if (p + payload_len >= msg_len)
Jeremy Kerrc855d7b2019-08-01 21:18:09 +0800780 hdr->flags_seq_tag |= MCTP_HDR_FLAG_EOM;
781 hdr->flags_seq_tag |=
782 (i & MCTP_HDR_SEQ_MASK) << MCTP_HDR_SEQ_SHIFT;
783
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +0800784 memcpy(mctp_pktbuf_data(pkt), msg + p, payload_len);
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800785
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800786 /* add to tx queue */
Jeremy Kerrcc2458d2019-03-01 08:23:33 +0800787 if (bus->tx_queue_tail)
788 bus->tx_queue_tail->next = pkt;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800789 else
Jeremy Kerrcc2458d2019-03-01 08:23:33 +0800790 bus->tx_queue_head = pkt;
791 bus->tx_queue_tail = pkt;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800792
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +0800793 p += payload_len;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800794 }
795
Andrew Jeffery298865f2020-02-06 11:51:29 +1030796 mctp_prdebug("%s: Enqueued %d packets", __func__, i);
797
Jeremy Kerrcc2458d2019-03-01 08:23:33 +0800798 mctp_send_tx_queue(bus);
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800799
800 return 0;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800801}
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800802
803int mctp_message_tx(struct mctp *mctp, mctp_eid_t eid,
804 void *msg, size_t msg_len)
805{
806 struct mctp_bus *bus;
807
808 bus = find_bus_for_eid(mctp, eid);
Brad Bishop663ec392021-10-07 21:16:48 -0400809 if (!bus)
810 return 0;
811
Andrew Jefferyb93b6112020-06-05 14:13:44 +0930812 return mctp_message_tx_on_bus(bus, bus->eid, eid, msg, msg_len);
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800813}