blob: 4d945db7b85c3cde30eb122d480d4b77d43b1835 [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{
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800335 /* for now, just use the first bus. For full routing support,
336 * we will need a table of neighbours */
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800337 return &mctp->busses[0];
338}
339
Jeremy Kerr7520cec2019-03-01 07:13:18 +0800340int mctp_register_bus(struct mctp *mctp,
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800341 struct mctp_binding *binding,
342 mctp_eid_t eid)
343{
Andrew Jeffery3e8a12a2020-06-05 16:08:30 +0930344 int rc = 0;
345
Jeremy Kerr7520cec2019-03-01 07:13:18 +0800346 /* todo: multiple busses */
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800347 assert(mctp->n_busses == 0);
348 mctp->n_busses = 1;
Andrew Jeffery3e8a12a2020-06-05 16:08:30 +0930349
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800350 mctp->busses = __mctp_alloc(sizeof(struct mctp_bus));
Andrew Jeffery3e8a12a2020-06-05 16:08:30 +0930351 if (!mctp->busses)
352 return -ENOMEM;
353
James Feist62d72362019-12-13 13:43:32 -0800354 memset(mctp->busses, 0, sizeof(struct mctp_bus));
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800355 mctp->busses[0].binding = binding;
356 mctp->busses[0].eid = eid;
Jeremy Kerr7520cec2019-03-01 07:13:18 +0800357 binding->bus = &mctp->busses[0];
Jeremy Kerr0a00dca2019-03-01 08:01:35 +0800358 binding->mctp = mctp;
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800359 mctp->route_policy = ROUTE_ENDPOINT;
Jeremy Kerr3b36d172019-09-04 11:56:09 +0800360
Andrew Jeffery3e8a12a2020-06-05 16:08:30 +0930361 if (binding->start) {
362 rc = binding->start(binding);
363 if (rc < 0) {
364 mctp_prerr("Failed to start binding: %d", rc);
Andrew Jeffery19275232021-01-29 14:13:25 +1030365 binding->bus = NULL;
Andrew Jeffery3e8a12a2020-06-05 16:08:30 +0930366 __mctp_free(mctp->busses);
367 mctp->busses = NULL;
Andrew Jeffery2304c832021-01-29 11:52:49 +1030368 mctp->n_busses = 0;
Andrew Jeffery3e8a12a2020-06-05 16:08:30 +0930369 }
370 }
Jeremy Kerr3b36d172019-09-04 11:56:09 +0800371
Andrew Jeffery3e8a12a2020-06-05 16:08:30 +0930372 return rc;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800373}
374
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800375int mctp_bridge_busses(struct mctp *mctp,
376 struct mctp_binding *b1, struct mctp_binding *b2)
377{
Andrew Jeffery19275232021-01-29 14:13:25 +1030378 int rc = 0;
379
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800380 assert(mctp->n_busses == 0);
381 mctp->busses = __mctp_alloc(2 * sizeof(struct mctp_bus));
Helen Huanga523bcc2021-05-19 15:44:56 +0800382 if (!mctp->busses)
383 return -ENOMEM;
James Feist62d72362019-12-13 13:43:32 -0800384 memset(mctp->busses, 0, 2 * sizeof(struct mctp_bus));
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800385 mctp->n_busses = 2;
386 mctp->busses[0].binding = b1;
387 b1->bus = &mctp->busses[0];
388 b1->mctp = mctp;
389 mctp->busses[1].binding = b2;
390 b2->bus = &mctp->busses[1];
391 b2->mctp = mctp;
392
393 mctp->route_policy = ROUTE_BRIDGE;
Jeremy Kerr3b36d172019-09-04 11:56:09 +0800394
Andrew Jeffery19275232021-01-29 14:13:25 +1030395 if (b1->start) {
396 rc = b1->start(b1);
397 if (rc < 0) {
398 mctp_prerr("Failed to start bridged bus %s: %d",
399 b1->name, rc);
400 goto done;
401 }
402 }
Jeremy Kerr3b36d172019-09-04 11:56:09 +0800403
Andrew Jeffery19275232021-01-29 14:13:25 +1030404 if (b2->start) {
405 rc = b2->start(b2);
406 if (rc < 0) {
407 mctp_prerr("Failed to start bridged bus %s: %d",
408 b2->name, rc);
409 goto done;
410 }
411 }
Jeremy Kerr3b36d172019-09-04 11:56:09 +0800412
Andrew Jeffery19275232021-01-29 14:13:25 +1030413done:
414 return rc;
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800415}
416
Wiktor GoĊ‚gowskiba6727e2020-03-13 18:25:01 +0100417static inline bool mctp_ctrl_cmd_is_transport(struct mctp_ctrl_msg_hdr *hdr)
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800418{
Wiktor GoĊ‚gowskiba6727e2020-03-13 18:25:01 +0100419 return ((hdr->command_code >= MCTP_CTRL_CMD_FIRST_TRANSPORT) &&
420 (hdr->command_code <= MCTP_CTRL_CMD_LAST_TRANSPORT));
421}
422
Andrew Jefferyb93b6112020-06-05 14:13:44 +0930423static bool mctp_ctrl_handle_msg(struct mctp_bus *bus, mctp_eid_t src,
424 void *buffer, size_t length)
Wiktor GoĊ‚gowskiba6727e2020-03-13 18:25:01 +0100425{
426 struct mctp_ctrl_msg_hdr *msg_hdr = buffer;
427
428 /*
429 * Control message is received. If a transport control message handler
430 * is provided, it will called. If there is no dedicated handler, this
431 * function returns false and data can be handled by the generic
432 * message handler. The transport control message handler will be
433 * provided with messages in the command range 0xF0 - 0xFF.
434 */
435 if (mctp_ctrl_cmd_is_transport(msg_hdr)) {
436 if (bus->binding->control_rx != NULL) {
437 /* MCTP bus binding handler */
438 bus->binding->control_rx(src,
439 bus->binding->control_rx_data,
440 buffer, length);
441 return true;
442 }
443 }
444
445 /*
446 * Command was not handled, due to lack of specific callback.
447 * It will be passed to regular message_rx handler.
448 */
449 return false;
450}
451
452static inline bool mctp_rx_dest_is_local(struct mctp_bus *bus, mctp_eid_t dest)
453{
454 return dest == bus->eid || dest == MCTP_EID_NULL ||
455 dest == MCTP_EID_BROADCAST;
456}
457
458static inline bool mctp_ctrl_cmd_is_request(struct mctp_ctrl_msg_hdr *hdr)
459{
460 return hdr->ic_msg_type == MCTP_CTRL_HDR_MSG_TYPE &&
461 hdr->rq_dgram_inst & MCTP_CTRL_HDR_FLAG_REQUEST;
462}
463
464/*
465 * Receive the complete MCTP message and route it.
466 * Asserts:
467 * 'buf' is not NULL.
468 */
469static void mctp_rx(struct mctp *mctp, struct mctp_bus *bus, mctp_eid_t src,
470 mctp_eid_t dest, void *buf, size_t len)
471{
472 assert(buf != NULL);
473
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800474 if (mctp->route_policy == ROUTE_ENDPOINT &&
Wiktor GoĊ‚gowskiba6727e2020-03-13 18:25:01 +0100475 mctp_rx_dest_is_local(bus, dest)) {
476 /* Handle MCTP Control Messages: */
477 if (len >= sizeof(struct mctp_ctrl_msg_hdr)) {
478 struct mctp_ctrl_msg_hdr *msg_hdr = buf;
479
480 /*
481 * Identify if this is a control request message.
482 * See DSP0236 v1.3.0 sec. 11.5.
483 */
484 if (mctp_ctrl_cmd_is_request(msg_hdr)) {
485 bool handled;
Andrew Jefferyb93b6112020-06-05 14:13:44 +0930486 handled = mctp_ctrl_handle_msg(bus, src, buf,
487 len);
Wiktor GoĊ‚gowskiba6727e2020-03-13 18:25:01 +0100488 if (handled)
489 return;
490 }
491 }
492 if (mctp->message_rx)
493 mctp->message_rx(src, mctp->message_rx_data, buf, len);
494 }
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800495
496 if (mctp->route_policy == ROUTE_BRIDGE) {
497 int i;
498
499 for (i = 0; i < mctp->n_busses; i++) {
500 struct mctp_bus *dest_bus = &mctp->busses[i];
501 if (dest_bus == bus)
502 continue;
503
Andrew Jefferyb93b6112020-06-05 14:13:44 +0930504 mctp_message_tx_on_bus(dest_bus, src, dest, buf, len);
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800505 }
506
507 }
508}
509
Jeremy Kerr0a00dca2019-03-01 08:01:35 +0800510void mctp_bus_rx(struct mctp_binding *binding, struct mctp_pktbuf *pkt)
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800511{
Jeremy Kerr7520cec2019-03-01 07:13:18 +0800512 struct mctp_bus *bus = binding->bus;
Jeremy Kerr0a00dca2019-03-01 08:01:35 +0800513 struct mctp *mctp = binding->mctp;
Ed Tanousc2def9f2019-02-21 08:33:08 -0800514 uint8_t flags, exp_seq, seq, tag;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800515 struct mctp_msg_ctx *ctx;
516 struct mctp_hdr *hdr;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800517 size_t len;
518 void *p;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800519 int rc;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800520
Jeremy Kerr7520cec2019-03-01 07:13:18 +0800521 assert(bus);
522
Sumanth Bhatd97869d2020-07-02 00:46:13 +0530523 /* Drop packet if it was smaller than mctp hdr size */
524 if (mctp_pktbuf_size(pkt) <= sizeof(struct mctp_hdr))
525 goto out;
526
Andrew Jeffery5d3d4e62021-08-20 16:44:40 +0930527 if (mctp->capture)
528 mctp->capture(pkt, mctp->capture_data);
529
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800530 hdr = mctp_pktbuf_hdr(pkt);
531
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800532 /* small optimisation: don't bother reassembly if we're going to
533 * drop the packet in mctp_rx anyway */
534 if (mctp->route_policy == ROUTE_ENDPOINT && hdr->dest != bus->eid)
Jeremy Kerrc1693af2019-08-05 14:30:59 +0800535 goto out;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800536
537 flags = hdr->flags_seq_tag & (MCTP_HDR_FLAG_SOM | MCTP_HDR_FLAG_EOM);
538 tag = (hdr->flags_seq_tag >> MCTP_HDR_TAG_SHIFT) & MCTP_HDR_TAG_MASK;
539 seq = (hdr->flags_seq_tag >> MCTP_HDR_SEQ_SHIFT) & MCTP_HDR_SEQ_MASK;
540
541 switch (flags) {
542 case MCTP_HDR_FLAG_SOM | MCTP_HDR_FLAG_EOM:
543 /* single-packet message - send straight up to rx function,
544 * no need to create a message context */
545 len = pkt->end - pkt->mctp_hdr_off - sizeof(struct mctp_hdr);
Andrew Jefferyb4ae00b2021-01-18 15:52:09 +1030546 p = pkt->data + pkt->mctp_hdr_off + sizeof(struct mctp_hdr);
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800547 mctp_rx(mctp, bus, hdr->src, hdr->dest, p, len);
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800548 break;
549
550 case MCTP_HDR_FLAG_SOM:
551 /* start of a new message - start the new context for
552 * future message reception. If an existing context is
553 * already present, drop it. */
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800554 ctx = mctp_msg_ctx_lookup(mctp, hdr->src, hdr->dest, tag);
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800555 if (ctx) {
556 mctp_msg_ctx_reset(ctx);
557 } else {
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800558 ctx = mctp_msg_ctx_create(mctp,
559 hdr->src, hdr->dest, tag);
Sumanth Bhat34d4c962021-06-16 12:50:48 +0530560 /* If context creation fails due to exhaution of contexts we
561 * can support, drop the packet */
562 if (!ctx) {
563 mctp_prdebug("Context buffers exhausted.");
564 goto out;
565 }
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800566 }
567
Sumanth Bhat69f545f2021-05-18 09:16:43 +0000568 /* Save the fragment size, subsequent middle fragments
569 * should of the same size */
570 ctx->fragment_size = mctp_pktbuf_size(pkt);
571
Sumanth Bhat2c820c52020-07-02 00:26:25 +0530572 rc = mctp_msg_ctx_add_pkt(ctx, pkt, mctp->max_message_size);
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800573 if (rc) {
574 mctp_msg_ctx_drop(ctx);
575 } else {
576 ctx->last_seq = seq;
577 }
578
579 break;
580
581 case MCTP_HDR_FLAG_EOM:
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)
Jeremy Kerrc1693af2019-08-05 14:30:59 +0800584 goto out;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800585
Ed Tanousc2def9f2019-02-21 08:33:08 -0800586 exp_seq = (ctx->last_seq + 1) % 4;
587
588 if (exp_seq != seq) {
589 mctp_prdebug(
590 "Sequence number %d does not match expected %d",
591 seq, exp_seq);
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800592 mctp_msg_ctx_drop(ctx);
Jeremy Kerrc1693af2019-08-05 14:30:59 +0800593 goto out;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800594 }
595
Sumanth Bhat69f545f2021-05-18 09:16:43 +0000596 len = mctp_pktbuf_size(pkt);
597
598 if (len > ctx->fragment_size) {
599 mctp_prdebug("Unexpected fragment size. Expected" \
600 " less than %zu, received = %zu",
601 ctx->fragment_size, len);
602 mctp_msg_ctx_drop(ctx);
603 goto out;
604 }
605
Sumanth Bhat2c820c52020-07-02 00:26:25 +0530606 rc = mctp_msg_ctx_add_pkt(ctx, pkt, mctp->max_message_size);
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800607 if (!rc)
608 mctp_rx(mctp, bus, ctx->src, ctx->dest,
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800609 ctx->buf, ctx->buf_size);
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800610
611 mctp_msg_ctx_drop(ctx);
612 break;
Ed Tanousc2def9f2019-02-21 08:33:08 -0800613
614 case 0:
615 /* Neither SOM nor EOM */
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800616 ctx = mctp_msg_ctx_lookup(mctp, hdr->src,hdr->dest, tag);
Ed Tanousc2def9f2019-02-21 08:33:08 -0800617 if (!ctx)
Jeremy Kerrc1693af2019-08-05 14:30:59 +0800618 goto out;
Ed Tanousc2def9f2019-02-21 08:33:08 -0800619
620 exp_seq = (ctx->last_seq + 1) % 4;
621 if (exp_seq != seq) {
622 mctp_prdebug(
623 "Sequence number %d does not match expected %d",
624 seq, exp_seq);
625 mctp_msg_ctx_drop(ctx);
Jeremy Kerrc1693af2019-08-05 14:30:59 +0800626 goto out;
Ed Tanousc2def9f2019-02-21 08:33:08 -0800627 }
628
Sumanth Bhat69f545f2021-05-18 09:16:43 +0000629 len = mctp_pktbuf_size(pkt);
630
631 if (len != ctx->fragment_size) {
632 mctp_prdebug("Unexpected fragment size. Expected = %zu " \
633 "received = %zu", ctx->fragment_size, len);
634 mctp_msg_ctx_drop(ctx);
635 goto out;
636 }
637
Sumanth Bhat2c820c52020-07-02 00:26:25 +0530638 rc = mctp_msg_ctx_add_pkt(ctx, pkt, mctp->max_message_size);
Ed Tanousc2def9f2019-02-21 08:33:08 -0800639 if (rc) {
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 ctx->last_seq = seq;
644
645 break;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800646 }
Jeremy Kerrc1693af2019-08-05 14:30:59 +0800647out:
648 mctp_pktbuf_free(pkt);
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800649}
650
Jeremy Kerr0a00dca2019-03-01 08:01:35 +0800651static int mctp_packet_tx(struct mctp_bus *bus,
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800652 struct mctp_pktbuf *pkt)
653{
Andrew Jeffery5d3d4e62021-08-20 16:44:40 +0930654 struct mctp *mctp = bus->binding->mctp;
655
Andrew Jefferyc61501c2021-01-27 23:24:18 +1030656 if (bus->state != mctp_bus_state_tx_enabled)
Jeremy Kerr1cd31182019-02-27 18:01:00 +0800657 return -1;
658
Andrew Jeffery5d3d4e62021-08-20 16:44:40 +0930659 if (mctp->capture)
660 mctp->capture(pkt, mctp->capture_data);
661
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800662 return bus->binding->tx(bus->binding, pkt);
663}
664
Jeremy Kerrcc2458d2019-03-01 08:23:33 +0800665static void mctp_send_tx_queue(struct mctp_bus *bus)
Jeremy Kerr1cd31182019-02-27 18:01:00 +0800666{
667 struct mctp_pktbuf *pkt;
668
Jeremy Kerrcc2458d2019-03-01 08:23:33 +0800669 while ((pkt = bus->tx_queue_head)) {
Jeremy Kerr1cd31182019-02-27 18:01:00 +0800670 int rc;
671
672 rc = mctp_packet_tx(bus, pkt);
673 if (rc)
674 break;
675
Jeremy Kerrcc2458d2019-03-01 08:23:33 +0800676 bus->tx_queue_head = pkt->next;
Jeremy Kerr1cd31182019-02-27 18:01:00 +0800677 mctp_pktbuf_free(pkt);
678 }
679
Jeremy Kerrcc2458d2019-03-01 08:23:33 +0800680 if (!bus->tx_queue_head)
681 bus->tx_queue_tail = NULL;
Jeremy Kerr1cd31182019-02-27 18:01:00 +0800682
683}
684
685void mctp_binding_set_tx_enabled(struct mctp_binding *binding, bool enable)
686{
687 struct mctp_bus *bus = binding->bus;
Andrew Jefferyc61501c2021-01-27 23:24:18 +1030688
689 switch(bus->state) {
690 case mctp_bus_state_constructed:
691 if (!enable)
692 return;
693
Andrew Jeffery1fa707e2021-01-28 15:22:11 +1030694 if (binding->pkt_size < MCTP_PACKET_SIZE(MCTP_BTU)) {
695 mctp_prerr("Cannot start %s binding with invalid MTU: %zu",
696 binding->name,
697 MCTP_BODY_SIZE(binding->pkt_size));
698 return;
699 }
700
Andrew Jefferyc61501c2021-01-27 23:24:18 +1030701 bus->state = mctp_bus_state_tx_enabled;
702 mctp_prinfo("%s binding started", binding->name);
703 return;
704 case mctp_bus_state_tx_enabled:
705 if (enable)
706 return;
707
708 bus->state = mctp_bus_state_tx_disabled;
709 mctp_prdebug("%s binding Tx disabled", binding->name);
710 return;
711 case mctp_bus_state_tx_disabled:
712 if (!enable)
713 return;
714
715 bus->state = mctp_bus_state_tx_enabled;
716 mctp_prdebug("%s binding Tx enabled", binding->name);
Jeremy Kerrcc2458d2019-03-01 08:23:33 +0800717 mctp_send_tx_queue(bus);
Andrew Jefferyc61501c2021-01-27 23:24:18 +1030718 return;
719 }
Jeremy Kerr1cd31182019-02-27 18:01:00 +0800720}
721
Andrew Jefferyb93b6112020-06-05 14:13:44 +0930722static int mctp_message_tx_on_bus(struct mctp_bus *bus, mctp_eid_t src,
723 mctp_eid_t dest, void *msg, size_t msg_len)
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800724{
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +0800725 size_t max_payload_len, payload_len, p;
Jeremy Kerr1cd31182019-02-27 18:01:00 +0800726 struct mctp_pktbuf *pkt;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800727 struct mctp_hdr *hdr;
Jeremy Kerrc855d7b2019-08-01 21:18:09 +0800728 int i;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800729
Andrew Jefferyc61501c2021-01-27 23:24:18 +1030730 if (bus->state == mctp_bus_state_constructed)
731 return -ENXIO;
732
Andrew Jeffery1fa707e2021-01-28 15:22:11 +1030733 max_payload_len = MCTP_BODY_SIZE(bus->binding->pkt_size);
734
735 {
736 const bool valid_mtu = max_payload_len >= MCTP_BTU;
737 assert(valid_mtu);
738 if (!valid_mtu)
739 return -EINVAL;
740 }
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800741
Andrew Jeffery298865f2020-02-06 11:51:29 +1030742 mctp_prdebug("%s: Generating packets for transmission of %zu byte message from %hhu to %hhu",
743 __func__, msg_len, src, dest);
744
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800745 /* queue up packets, each of max MCTP_MTU size */
Jeremy Kerrc855d7b2019-08-01 21:18:09 +0800746 for (p = 0, i = 0; p < msg_len; i++) {
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +0800747 payload_len = msg_len - p;
748 if (payload_len > max_payload_len)
749 payload_len = max_payload_len;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800750
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800751 pkt = mctp_pktbuf_alloc(bus->binding,
752 payload_len + sizeof(*hdr));
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800753 hdr = mctp_pktbuf_hdr(pkt);
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800754
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800755 /* todo: tags */
756 hdr->ver = bus->binding->version & 0xf;
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800757 hdr->dest = dest;
758 hdr->src = src;
Jeremy Kerrc855d7b2019-08-01 21:18:09 +0800759 hdr->flags_seq_tag = MCTP_HDR_FLAG_TO |
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800760 (0 << MCTP_HDR_TAG_SHIFT);
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800761
Jeremy Kerrc855d7b2019-08-01 21:18:09 +0800762 if (i == 0)
763 hdr->flags_seq_tag |= MCTP_HDR_FLAG_SOM;
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +0800764 if (p + payload_len >= msg_len)
Jeremy Kerrc855d7b2019-08-01 21:18:09 +0800765 hdr->flags_seq_tag |= MCTP_HDR_FLAG_EOM;
766 hdr->flags_seq_tag |=
767 (i & MCTP_HDR_SEQ_MASK) << MCTP_HDR_SEQ_SHIFT;
768
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +0800769 memcpy(mctp_pktbuf_data(pkt), msg + p, payload_len);
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800770
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800771 /* add to tx queue */
Jeremy Kerrcc2458d2019-03-01 08:23:33 +0800772 if (bus->tx_queue_tail)
773 bus->tx_queue_tail->next = pkt;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800774 else
Jeremy Kerrcc2458d2019-03-01 08:23:33 +0800775 bus->tx_queue_head = pkt;
776 bus->tx_queue_tail = pkt;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800777
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +0800778 p += payload_len;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800779 }
780
Andrew Jeffery298865f2020-02-06 11:51:29 +1030781 mctp_prdebug("%s: Enqueued %d packets", __func__, i);
782
Jeremy Kerrcc2458d2019-03-01 08:23:33 +0800783 mctp_send_tx_queue(bus);
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800784
785 return 0;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800786}
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800787
788int mctp_message_tx(struct mctp *mctp, mctp_eid_t eid,
789 void *msg, size_t msg_len)
790{
791 struct mctp_bus *bus;
792
793 bus = find_bus_for_eid(mctp, eid);
Andrew Jefferyb93b6112020-06-05 14:13:44 +0930794 return mctp_message_tx_on_bus(bus, bus->eid, eid, msg, msg_len);
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800795}