blob: 5ec4fcbf61d1171fef04e11b39eadb43f4000287 [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;
48};
49
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080050struct mctp {
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +080051 int n_busses;
52 struct mctp_bus *busses;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080053
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080054 /* Message RX callback */
55 mctp_rx_fn message_rx;
56 void *message_rx_data;
Jeremy Kerr24db71f2019-02-07 21:37:35 +080057
58 /* Message reassembly.
59 * @todo: flexible context count
60 */
61 struct mctp_msg_ctx msg_ctxs[16];
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +080062
63 enum {
64 ROUTE_ENDPOINT,
65 ROUTE_BRIDGE,
66 } route_policy;
Sumanth Bhat2c820c52020-07-02 00:26:25 +053067 size_t max_message_size;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080068};
69
70#ifndef BUILD_ASSERT
71#define BUILD_ASSERT(x) \
72 do { (void)sizeof(char[0-(!(x))]); } while (0)
73#endif
74
Jeremy Kerr24db71f2019-02-07 21:37:35 +080075#ifndef ARRAY_SIZE
76#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
77#endif
78
Sumanth Bhat2c820c52020-07-02 00:26:25 +053079/* 64kb should be sufficient for a single message. Applications
80 * requiring higher sizes can override by setting max_message_size.*/
81#ifndef MCTP_MAX_MESSAGE_SIZE
82#define MCTP_MAX_MESSAGE_SIZE 65536
83#endif
84
Andrew Jefferyb93b6112020-06-05 14:13:44 +093085static int mctp_message_tx_on_bus(struct mctp_bus *bus, mctp_eid_t src,
86 mctp_eid_t dest, void *msg, size_t msg_len);
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +080087
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +080088struct mctp_pktbuf *mctp_pktbuf_alloc(struct mctp_binding *binding, size_t len)
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080089{
90 struct mctp_pktbuf *buf;
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +080091 size_t size;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080092
Andrew Jeffery39da3d02021-03-12 16:51:26 +103093 size = binding->pkt_size + binding->pkt_header + binding->pkt_trailer;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080094
95 /* todo: pools */
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +080096 buf = __mctp_alloc(sizeof(*buf) + size);
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080097
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +080098 buf->size = size;
Andrew Jeffery39da3d02021-03-12 16:51:26 +103099 buf->start = binding->pkt_header;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800100 buf->end = buf->start + len;
101 buf->mctp_hdr_off = buf->start;
Jeremy Kerrdd109f12019-04-04 11:46:49 +0800102 buf->next = NULL;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800103
104 return buf;
105}
106
107void mctp_pktbuf_free(struct mctp_pktbuf *pkt)
108{
109 __mctp_free(pkt);
110}
111
112struct mctp_hdr *mctp_pktbuf_hdr(struct mctp_pktbuf *pkt)
113{
114 return (void *)pkt->data + pkt->mctp_hdr_off;
115}
116
117void *mctp_pktbuf_data(struct mctp_pktbuf *pkt)
118{
119 return (void *)pkt->data + pkt->mctp_hdr_off + sizeof(struct mctp_hdr);
120}
121
Andrew Jefferyb942e3a2020-06-23 09:54:02 +0930122size_t mctp_pktbuf_size(struct mctp_pktbuf *pkt)
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800123{
124 return pkt->end - pkt->start;
125}
126
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +0800127void *mctp_pktbuf_alloc_start(struct mctp_pktbuf *pkt, size_t size)
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800128{
129 assert(size <= pkt->start);
130 pkt->start -= size;
131 return pkt->data + pkt->start;
132}
133
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +0800134void *mctp_pktbuf_alloc_end(struct mctp_pktbuf *pkt, size_t size)
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800135{
136 void *buf;
137
Andrew Jeffery3ac70d62020-07-01 00:50:44 +0930138 assert(size <= (pkt->size - pkt->end));
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800139 buf = pkt->data + pkt->end;
140 pkt->end += size;
141 return buf;
142}
143
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +0800144int mctp_pktbuf_push(struct mctp_pktbuf *pkt, void *data, size_t len)
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800145{
146 void *p;
147
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +0800148 if (pkt->end + len > pkt->size)
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800149 return -1;
150
151 p = pkt->data + pkt->end;
152
153 pkt->end += len;
154 memcpy(p, data, len);
155
156 return 0;
157}
158
Andrew Jefferyeba19a32021-03-09 23:09:40 +1030159void *mctp_pktbuf_pop(struct mctp_pktbuf *pkt, size_t len)
160{
161 if (len > mctp_pktbuf_size(pkt))
162 return NULL;
163
164 pkt->end -= len;
165 return pkt->data + pkt->end;
166}
167
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800168/* Message reassembly */
169static struct mctp_msg_ctx *mctp_msg_ctx_lookup(struct mctp *mctp,
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800170 uint8_t src, uint8_t dest, uint8_t tag)
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800171{
172 unsigned int i;
173
174 /* @todo: better lookup, if we add support for more outstanding
175 * message contexts */
176 for (i = 0; i < ARRAY_SIZE(mctp->msg_ctxs); i++) {
177 struct mctp_msg_ctx *ctx = &mctp->msg_ctxs[i];
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800178 if (ctx->src == src && ctx->dest == dest && ctx->tag == tag)
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800179 return ctx;
180 }
181
182 return NULL;
183}
184
185static struct mctp_msg_ctx *mctp_msg_ctx_create(struct mctp *mctp,
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800186 uint8_t src, uint8_t dest, uint8_t tag)
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800187{
Jeremy Kerr11a234e2019-02-27 17:59:53 +0800188 struct mctp_msg_ctx *ctx = NULL;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800189 unsigned int i;
190
191 for (i = 0; i < ARRAY_SIZE(mctp->msg_ctxs); i++) {
192 struct mctp_msg_ctx *tmp = &mctp->msg_ctxs[i];
193 if (!tmp->src) {
194 ctx = tmp;
195 break;
196 }
197 }
198
199 if (!ctx)
200 return NULL;
201
202 ctx->src = src;
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800203 ctx->dest = dest;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800204 ctx->tag = tag;
Jeremy Kerr9a3da812019-08-02 15:57:53 +0800205 ctx->buf_size = 0;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800206
207 return ctx;
208}
209
210static void mctp_msg_ctx_drop(struct mctp_msg_ctx *ctx)
211{
212 ctx->src = 0;
213}
214
215static void mctp_msg_ctx_reset(struct mctp_msg_ctx *ctx)
216{
217 ctx->buf_size = 0;
218}
219
220static int mctp_msg_ctx_add_pkt(struct mctp_msg_ctx *ctx,
Sumanth Bhat2c820c52020-07-02 00:26:25 +0530221 struct mctp_pktbuf *pkt, size_t max_size)
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800222{
223 size_t len;
224
225 len = mctp_pktbuf_size(pkt) - sizeof(struct mctp_hdr);
226
227 if (ctx->buf_size + len > ctx->buf_alloc_size) {
228 size_t new_alloc_size;
Andrew Jeffery00ecc6c2020-03-10 23:16:53 +1030229 void *lbuf;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800230
Andrew Jeffery5a508912020-11-03 22:21:45 +1030231 /* @todo: finer-grained allocation */
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800232 if (!ctx->buf_alloc_size) {
Andrew Jefferyc2b833e2020-10-28 14:28:37 +1030233 new_alloc_size = MAX(len, 4096UL);
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800234 } else {
235 new_alloc_size = ctx->buf_alloc_size * 2;
236 }
Andrew Jeffery00ecc6c2020-03-10 23:16:53 +1030237
Sumanth Bhat2c820c52020-07-02 00:26:25 +0530238 /* Don't allow heap to grow beyond a limit */
239 if (new_alloc_size > max_size)
240 return -1;
241
242
Andrew Jeffery00ecc6c2020-03-10 23:16:53 +1030243 lbuf = __mctp_realloc(ctx->buf, new_alloc_size);
244 if (lbuf) {
245 ctx->buf = lbuf;
246 ctx->buf_alloc_size = new_alloc_size;
247 } else {
248 __mctp_free(ctx->buf);
249 return -1;
250 }
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800251 }
252
253 memcpy(ctx->buf + ctx->buf_size, mctp_pktbuf_data(pkt), len);
254 ctx->buf_size += len;
255
256 return 0;
257}
258
259/* Core API functions */
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800260struct mctp *mctp_init(void)
261{
262 struct mctp *mctp;
263
264 mctp = __mctp_alloc(sizeof(*mctp));
Sumanth Bhat96d54492020-07-14 17:10:04 +0530265
266 if(!mctp)
267 return NULL;
268
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800269 memset(mctp, 0, sizeof(*mctp));
Sumanth Bhat2c820c52020-07-02 00:26:25 +0530270 mctp->max_message_size = MCTP_MAX_MESSAGE_SIZE;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800271
272 return mctp;
273}
274
Sumanth Bhat2c820c52020-07-02 00:26:25 +0530275void mctp_set_max_message_size(struct mctp *mctp, size_t message_size)
276{
277 mctp->max_message_size = message_size;
278}
279
Andrew Jeffery3ae89dc2021-01-28 15:24:36 +1030280static void mctp_bus_destroy(struct mctp_bus *bus)
281{
282 while (bus->tx_queue_head) {
283 struct mctp_pktbuf *curr = bus->tx_queue_head;
284
285 bus->tx_queue_head = curr->next;
286 mctp_pktbuf_free(curr);
287 }
288}
289
Andrew Jefferyfa56ca52020-03-10 23:18:22 +1030290void mctp_destroy(struct mctp *mctp)
291{
Andrew Jefferyb93b6112020-06-05 14:13:44 +0930292 size_t i;
Andrew Jefferyfa56ca52020-03-10 23:18:22 +1030293
294 /* Cleanup message assembly contexts */
Andrew Jefferyb93b6112020-06-05 14:13:44 +0930295 BUILD_ASSERT(ARRAY_SIZE(mctp->msg_ctxs) < SIZE_MAX);
Andrew Jefferyfa56ca52020-03-10 23:18:22 +1030296 for (i = 0; i < ARRAY_SIZE(mctp->msg_ctxs); i++) {
297 struct mctp_msg_ctx *tmp = &mctp->msg_ctxs[i];
298 if (tmp->buf)
299 __mctp_free(tmp->buf);
300 }
301
Andrew Jeffery3ae89dc2021-01-28 15:24:36 +1030302 while (mctp->n_busses--)
303 mctp_bus_destroy(&mctp->busses[mctp->n_busses]);
304
Andrew Jefferyfa56ca52020-03-10 23:18:22 +1030305 __mctp_free(mctp->busses);
306 __mctp_free(mctp);
307}
308
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800309int mctp_set_rx_all(struct mctp *mctp, mctp_rx_fn fn, void *data)
310{
311 mctp->message_rx = fn;
312 mctp->message_rx_data = data;
313 return 0;
314}
315
316static struct mctp_bus *find_bus_for_eid(struct mctp *mctp,
317 mctp_eid_t dest __attribute__((unused)))
318{
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800319 /* for now, just use the first bus. For full routing support,
320 * we will need a table of neighbours */
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800321 return &mctp->busses[0];
322}
323
Jeremy Kerr7520cec2019-03-01 07:13:18 +0800324int mctp_register_bus(struct mctp *mctp,
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800325 struct mctp_binding *binding,
326 mctp_eid_t eid)
327{
Andrew Jeffery3e8a12a2020-06-05 16:08:30 +0930328 int rc = 0;
329
Jeremy Kerr7520cec2019-03-01 07:13:18 +0800330 /* todo: multiple busses */
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800331 assert(mctp->n_busses == 0);
332 mctp->n_busses = 1;
Andrew Jeffery3e8a12a2020-06-05 16:08:30 +0930333
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800334 mctp->busses = __mctp_alloc(sizeof(struct mctp_bus));
Andrew Jeffery3e8a12a2020-06-05 16:08:30 +0930335 if (!mctp->busses)
336 return -ENOMEM;
337
James Feist62d72362019-12-13 13:43:32 -0800338 memset(mctp->busses, 0, sizeof(struct mctp_bus));
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800339 mctp->busses[0].binding = binding;
340 mctp->busses[0].eid = eid;
Jeremy Kerr7520cec2019-03-01 07:13:18 +0800341 binding->bus = &mctp->busses[0];
Jeremy Kerr0a00dca2019-03-01 08:01:35 +0800342 binding->mctp = mctp;
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800343 mctp->route_policy = ROUTE_ENDPOINT;
Jeremy Kerr3b36d172019-09-04 11:56:09 +0800344
Andrew Jeffery3e8a12a2020-06-05 16:08:30 +0930345 if (binding->start) {
346 rc = binding->start(binding);
347 if (rc < 0) {
348 mctp_prerr("Failed to start binding: %d", rc);
Andrew Jeffery19275232021-01-29 14:13:25 +1030349 binding->bus = NULL;
Andrew Jeffery3e8a12a2020-06-05 16:08:30 +0930350 __mctp_free(mctp->busses);
351 mctp->busses = NULL;
Andrew Jeffery2304c832021-01-29 11:52:49 +1030352 mctp->n_busses = 0;
Andrew Jeffery3e8a12a2020-06-05 16:08:30 +0930353 }
354 }
Jeremy Kerr3b36d172019-09-04 11:56:09 +0800355
Andrew Jeffery3e8a12a2020-06-05 16:08:30 +0930356 return rc;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800357}
358
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800359int mctp_bridge_busses(struct mctp *mctp,
360 struct mctp_binding *b1, struct mctp_binding *b2)
361{
Andrew Jeffery19275232021-01-29 14:13:25 +1030362 int rc = 0;
363
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800364 assert(mctp->n_busses == 0);
365 mctp->busses = __mctp_alloc(2 * sizeof(struct mctp_bus));
Helen Huanga523bcc2021-05-19 15:44:56 +0800366 if (!mctp->busses)
367 return -ENOMEM;
James Feist62d72362019-12-13 13:43:32 -0800368 memset(mctp->busses, 0, 2 * sizeof(struct mctp_bus));
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800369 mctp->n_busses = 2;
370 mctp->busses[0].binding = b1;
371 b1->bus = &mctp->busses[0];
372 b1->mctp = mctp;
373 mctp->busses[1].binding = b2;
374 b2->bus = &mctp->busses[1];
375 b2->mctp = mctp;
376
377 mctp->route_policy = ROUTE_BRIDGE;
Jeremy Kerr3b36d172019-09-04 11:56:09 +0800378
Andrew Jeffery19275232021-01-29 14:13:25 +1030379 if (b1->start) {
380 rc = b1->start(b1);
381 if (rc < 0) {
382 mctp_prerr("Failed to start bridged bus %s: %d",
383 b1->name, rc);
384 goto done;
385 }
386 }
Jeremy Kerr3b36d172019-09-04 11:56:09 +0800387
Andrew Jeffery19275232021-01-29 14:13:25 +1030388 if (b2->start) {
389 rc = b2->start(b2);
390 if (rc < 0) {
391 mctp_prerr("Failed to start bridged bus %s: %d",
392 b2->name, rc);
393 goto done;
394 }
395 }
Jeremy Kerr3b36d172019-09-04 11:56:09 +0800396
Andrew Jeffery19275232021-01-29 14:13:25 +1030397done:
398 return rc;
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800399}
400
Wiktor GoĊ‚gowskiba6727e2020-03-13 18:25:01 +0100401static inline bool mctp_ctrl_cmd_is_transport(struct mctp_ctrl_msg_hdr *hdr)
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800402{
Wiktor GoĊ‚gowskiba6727e2020-03-13 18:25:01 +0100403 return ((hdr->command_code >= MCTP_CTRL_CMD_FIRST_TRANSPORT) &&
404 (hdr->command_code <= MCTP_CTRL_CMD_LAST_TRANSPORT));
405}
406
Andrew Jefferyb93b6112020-06-05 14:13:44 +0930407static bool mctp_ctrl_handle_msg(struct mctp_bus *bus, mctp_eid_t src,
408 void *buffer, size_t length)
Wiktor GoĊ‚gowskiba6727e2020-03-13 18:25:01 +0100409{
410 struct mctp_ctrl_msg_hdr *msg_hdr = buffer;
411
412 /*
413 * Control message is received. If a transport control message handler
414 * is provided, it will called. If there is no dedicated handler, this
415 * function returns false and data can be handled by the generic
416 * message handler. The transport control message handler will be
417 * provided with messages in the command range 0xF0 - 0xFF.
418 */
419 if (mctp_ctrl_cmd_is_transport(msg_hdr)) {
420 if (bus->binding->control_rx != NULL) {
421 /* MCTP bus binding handler */
422 bus->binding->control_rx(src,
423 bus->binding->control_rx_data,
424 buffer, length);
425 return true;
426 }
427 }
428
429 /*
430 * Command was not handled, due to lack of specific callback.
431 * It will be passed to regular message_rx handler.
432 */
433 return false;
434}
435
436static inline bool mctp_rx_dest_is_local(struct mctp_bus *bus, mctp_eid_t dest)
437{
438 return dest == bus->eid || dest == MCTP_EID_NULL ||
439 dest == MCTP_EID_BROADCAST;
440}
441
442static inline bool mctp_ctrl_cmd_is_request(struct mctp_ctrl_msg_hdr *hdr)
443{
444 return hdr->ic_msg_type == MCTP_CTRL_HDR_MSG_TYPE &&
445 hdr->rq_dgram_inst & MCTP_CTRL_HDR_FLAG_REQUEST;
446}
447
448/*
449 * Receive the complete MCTP message and route it.
450 * Asserts:
451 * 'buf' is not NULL.
452 */
453static void mctp_rx(struct mctp *mctp, struct mctp_bus *bus, mctp_eid_t src,
454 mctp_eid_t dest, void *buf, size_t len)
455{
456 assert(buf != NULL);
457
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800458 if (mctp->route_policy == ROUTE_ENDPOINT &&
Wiktor GoĊ‚gowskiba6727e2020-03-13 18:25:01 +0100459 mctp_rx_dest_is_local(bus, dest)) {
460 /* Handle MCTP Control Messages: */
461 if (len >= sizeof(struct mctp_ctrl_msg_hdr)) {
462 struct mctp_ctrl_msg_hdr *msg_hdr = buf;
463
464 /*
465 * Identify if this is a control request message.
466 * See DSP0236 v1.3.0 sec. 11.5.
467 */
468 if (mctp_ctrl_cmd_is_request(msg_hdr)) {
469 bool handled;
Andrew Jefferyb93b6112020-06-05 14:13:44 +0930470 handled = mctp_ctrl_handle_msg(bus, src, buf,
471 len);
Wiktor GoĊ‚gowskiba6727e2020-03-13 18:25:01 +0100472 if (handled)
473 return;
474 }
475 }
476 if (mctp->message_rx)
477 mctp->message_rx(src, mctp->message_rx_data, buf, len);
478 }
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800479
480 if (mctp->route_policy == ROUTE_BRIDGE) {
481 int i;
482
483 for (i = 0; i < mctp->n_busses; i++) {
484 struct mctp_bus *dest_bus = &mctp->busses[i];
485 if (dest_bus == bus)
486 continue;
487
Andrew Jefferyb93b6112020-06-05 14:13:44 +0930488 mctp_message_tx_on_bus(dest_bus, src, dest, buf, len);
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800489 }
490
491 }
492}
493
Jeremy Kerr0a00dca2019-03-01 08:01:35 +0800494void mctp_bus_rx(struct mctp_binding *binding, struct mctp_pktbuf *pkt)
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800495{
Jeremy Kerr7520cec2019-03-01 07:13:18 +0800496 struct mctp_bus *bus = binding->bus;
Jeremy Kerr0a00dca2019-03-01 08:01:35 +0800497 struct mctp *mctp = binding->mctp;
Ed Tanousc2def9f2019-02-21 08:33:08 -0800498 uint8_t flags, exp_seq, seq, tag;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800499 struct mctp_msg_ctx *ctx;
500 struct mctp_hdr *hdr;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800501 size_t len;
502 void *p;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800503 int rc;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800504
Jeremy Kerr7520cec2019-03-01 07:13:18 +0800505 assert(bus);
506
Sumanth Bhatd97869d2020-07-02 00:46:13 +0530507 /* Drop packet if it was smaller than mctp hdr size */
508 if (mctp_pktbuf_size(pkt) <= sizeof(struct mctp_hdr))
509 goto out;
510
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800511 hdr = mctp_pktbuf_hdr(pkt);
512
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800513 /* small optimisation: don't bother reassembly if we're going to
514 * drop the packet in mctp_rx anyway */
515 if (mctp->route_policy == ROUTE_ENDPOINT && hdr->dest != bus->eid)
Jeremy Kerrc1693af2019-08-05 14:30:59 +0800516 goto out;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800517
518 flags = hdr->flags_seq_tag & (MCTP_HDR_FLAG_SOM | MCTP_HDR_FLAG_EOM);
519 tag = (hdr->flags_seq_tag >> MCTP_HDR_TAG_SHIFT) & MCTP_HDR_TAG_MASK;
520 seq = (hdr->flags_seq_tag >> MCTP_HDR_SEQ_SHIFT) & MCTP_HDR_SEQ_MASK;
521
522 switch (flags) {
523 case MCTP_HDR_FLAG_SOM | MCTP_HDR_FLAG_EOM:
524 /* single-packet message - send straight up to rx function,
525 * no need to create a message context */
526 len = pkt->end - pkt->mctp_hdr_off - sizeof(struct mctp_hdr);
Andrew Jefferyb4ae00b2021-01-18 15:52:09 +1030527 p = pkt->data + pkt->mctp_hdr_off + sizeof(struct mctp_hdr);
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800528 mctp_rx(mctp, bus, hdr->src, hdr->dest, p, len);
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800529 break;
530
531 case MCTP_HDR_FLAG_SOM:
532 /* start of a new message - start the new context for
533 * future message reception. If an existing context is
534 * already present, drop it. */
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800535 ctx = mctp_msg_ctx_lookup(mctp, hdr->src, hdr->dest, tag);
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800536 if (ctx) {
537 mctp_msg_ctx_reset(ctx);
538 } else {
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800539 ctx = mctp_msg_ctx_create(mctp,
540 hdr->src, hdr->dest, tag);
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800541 }
542
Sumanth Bhat2c820c52020-07-02 00:26:25 +0530543 rc = mctp_msg_ctx_add_pkt(ctx, pkt, mctp->max_message_size);
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800544 if (rc) {
545 mctp_msg_ctx_drop(ctx);
546 } else {
547 ctx->last_seq = seq;
548 }
549
550 break;
551
552 case MCTP_HDR_FLAG_EOM:
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800553 ctx = mctp_msg_ctx_lookup(mctp, hdr->src, hdr->dest, tag);
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800554 if (!ctx)
Jeremy Kerrc1693af2019-08-05 14:30:59 +0800555 goto out;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800556
Ed Tanousc2def9f2019-02-21 08:33:08 -0800557 exp_seq = (ctx->last_seq + 1) % 4;
558
559 if (exp_seq != seq) {
560 mctp_prdebug(
561 "Sequence number %d does not match expected %d",
562 seq, exp_seq);
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800563 mctp_msg_ctx_drop(ctx);
Jeremy Kerrc1693af2019-08-05 14:30:59 +0800564 goto out;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800565 }
566
Sumanth Bhat2c820c52020-07-02 00:26:25 +0530567 rc = mctp_msg_ctx_add_pkt(ctx, pkt, mctp->max_message_size);
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800568 if (!rc)
569 mctp_rx(mctp, bus, ctx->src, ctx->dest,
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800570 ctx->buf, ctx->buf_size);
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800571
572 mctp_msg_ctx_drop(ctx);
573 break;
Ed Tanousc2def9f2019-02-21 08:33:08 -0800574
575 case 0:
576 /* Neither SOM nor EOM */
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800577 ctx = mctp_msg_ctx_lookup(mctp, hdr->src,hdr->dest, tag);
Ed Tanousc2def9f2019-02-21 08:33:08 -0800578 if (!ctx)
Jeremy Kerrc1693af2019-08-05 14:30:59 +0800579 goto out;
Ed Tanousc2def9f2019-02-21 08:33:08 -0800580
581 exp_seq = (ctx->last_seq + 1) % 4;
582 if (exp_seq != seq) {
583 mctp_prdebug(
584 "Sequence number %d does not match expected %d",
585 seq, exp_seq);
586 mctp_msg_ctx_drop(ctx);
Jeremy Kerrc1693af2019-08-05 14:30:59 +0800587 goto out;
Ed Tanousc2def9f2019-02-21 08:33:08 -0800588 }
589
Sumanth Bhat2c820c52020-07-02 00:26:25 +0530590 rc = mctp_msg_ctx_add_pkt(ctx, pkt, mctp->max_message_size);
Ed Tanousc2def9f2019-02-21 08:33:08 -0800591 if (rc) {
592 mctp_msg_ctx_drop(ctx);
Jeremy Kerrc1693af2019-08-05 14:30:59 +0800593 goto out;
Ed Tanousc2def9f2019-02-21 08:33:08 -0800594 }
595 ctx->last_seq = seq;
596
597 break;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800598 }
Jeremy Kerrc1693af2019-08-05 14:30:59 +0800599out:
600 mctp_pktbuf_free(pkt);
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800601}
602
Jeremy Kerr0a00dca2019-03-01 08:01:35 +0800603static int mctp_packet_tx(struct mctp_bus *bus,
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800604 struct mctp_pktbuf *pkt)
605{
Andrew Jefferyc61501c2021-01-27 23:24:18 +1030606 if (bus->state != mctp_bus_state_tx_enabled)
Jeremy Kerr1cd31182019-02-27 18:01:00 +0800607 return -1;
608
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800609 return bus->binding->tx(bus->binding, pkt);
610}
611
Jeremy Kerrcc2458d2019-03-01 08:23:33 +0800612static void mctp_send_tx_queue(struct mctp_bus *bus)
Jeremy Kerr1cd31182019-02-27 18:01:00 +0800613{
614 struct mctp_pktbuf *pkt;
615
Jeremy Kerrcc2458d2019-03-01 08:23:33 +0800616 while ((pkt = bus->tx_queue_head)) {
Jeremy Kerr1cd31182019-02-27 18:01:00 +0800617 int rc;
618
619 rc = mctp_packet_tx(bus, pkt);
620 if (rc)
621 break;
622
Jeremy Kerrcc2458d2019-03-01 08:23:33 +0800623 bus->tx_queue_head = pkt->next;
Jeremy Kerr1cd31182019-02-27 18:01:00 +0800624 mctp_pktbuf_free(pkt);
625 }
626
Jeremy Kerrcc2458d2019-03-01 08:23:33 +0800627 if (!bus->tx_queue_head)
628 bus->tx_queue_tail = NULL;
Jeremy Kerr1cd31182019-02-27 18:01:00 +0800629
630}
631
632void mctp_binding_set_tx_enabled(struct mctp_binding *binding, bool enable)
633{
634 struct mctp_bus *bus = binding->bus;
Andrew Jefferyc61501c2021-01-27 23:24:18 +1030635
636 switch(bus->state) {
637 case mctp_bus_state_constructed:
638 if (!enable)
639 return;
640
Andrew Jeffery1fa707e2021-01-28 15:22:11 +1030641 if (binding->pkt_size < MCTP_PACKET_SIZE(MCTP_BTU)) {
642 mctp_prerr("Cannot start %s binding with invalid MTU: %zu",
643 binding->name,
644 MCTP_BODY_SIZE(binding->pkt_size));
645 return;
646 }
647
Andrew Jefferyc61501c2021-01-27 23:24:18 +1030648 bus->state = mctp_bus_state_tx_enabled;
649 mctp_prinfo("%s binding started", binding->name);
650 return;
651 case mctp_bus_state_tx_enabled:
652 if (enable)
653 return;
654
655 bus->state = mctp_bus_state_tx_disabled;
656 mctp_prdebug("%s binding Tx disabled", binding->name);
657 return;
658 case mctp_bus_state_tx_disabled:
659 if (!enable)
660 return;
661
662 bus->state = mctp_bus_state_tx_enabled;
663 mctp_prdebug("%s binding Tx enabled", binding->name);
Jeremy Kerrcc2458d2019-03-01 08:23:33 +0800664 mctp_send_tx_queue(bus);
Andrew Jefferyc61501c2021-01-27 23:24:18 +1030665 return;
666 }
Jeremy Kerr1cd31182019-02-27 18:01:00 +0800667}
668
Andrew Jefferyb93b6112020-06-05 14:13:44 +0930669static int mctp_message_tx_on_bus(struct mctp_bus *bus, mctp_eid_t src,
670 mctp_eid_t dest, void *msg, size_t msg_len)
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800671{
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +0800672 size_t max_payload_len, payload_len, p;
Jeremy Kerr1cd31182019-02-27 18:01:00 +0800673 struct mctp_pktbuf *pkt;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800674 struct mctp_hdr *hdr;
Jeremy Kerrc855d7b2019-08-01 21:18:09 +0800675 int i;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800676
Andrew Jefferyc61501c2021-01-27 23:24:18 +1030677 if (bus->state == mctp_bus_state_constructed)
678 return -ENXIO;
679
Andrew Jeffery1fa707e2021-01-28 15:22:11 +1030680 max_payload_len = MCTP_BODY_SIZE(bus->binding->pkt_size);
681
682 {
683 const bool valid_mtu = max_payload_len >= MCTP_BTU;
684 assert(valid_mtu);
685 if (!valid_mtu)
686 return -EINVAL;
687 }
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800688
Andrew Jeffery298865f2020-02-06 11:51:29 +1030689 mctp_prdebug("%s: Generating packets for transmission of %zu byte message from %hhu to %hhu",
690 __func__, msg_len, src, dest);
691
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800692 /* queue up packets, each of max MCTP_MTU size */
Jeremy Kerrc855d7b2019-08-01 21:18:09 +0800693 for (p = 0, i = 0; p < msg_len; i++) {
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +0800694 payload_len = msg_len - p;
695 if (payload_len > max_payload_len)
696 payload_len = max_payload_len;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800697
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800698 pkt = mctp_pktbuf_alloc(bus->binding,
699 payload_len + sizeof(*hdr));
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800700 hdr = mctp_pktbuf_hdr(pkt);
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800701
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800702 /* todo: tags */
703 hdr->ver = bus->binding->version & 0xf;
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800704 hdr->dest = dest;
705 hdr->src = src;
Jeremy Kerrc855d7b2019-08-01 21:18:09 +0800706 hdr->flags_seq_tag = MCTP_HDR_FLAG_TO |
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800707 (0 << MCTP_HDR_TAG_SHIFT);
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800708
Jeremy Kerrc855d7b2019-08-01 21:18:09 +0800709 if (i == 0)
710 hdr->flags_seq_tag |= MCTP_HDR_FLAG_SOM;
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +0800711 if (p + payload_len >= msg_len)
Jeremy Kerrc855d7b2019-08-01 21:18:09 +0800712 hdr->flags_seq_tag |= MCTP_HDR_FLAG_EOM;
713 hdr->flags_seq_tag |=
714 (i & MCTP_HDR_SEQ_MASK) << MCTP_HDR_SEQ_SHIFT;
715
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +0800716 memcpy(mctp_pktbuf_data(pkt), msg + p, payload_len);
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800717
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800718 /* add to tx queue */
Jeremy Kerrcc2458d2019-03-01 08:23:33 +0800719 if (bus->tx_queue_tail)
720 bus->tx_queue_tail->next = pkt;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800721 else
Jeremy Kerrcc2458d2019-03-01 08:23:33 +0800722 bus->tx_queue_head = pkt;
723 bus->tx_queue_tail = pkt;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800724
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +0800725 p += payload_len;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800726 }
727
Andrew Jeffery298865f2020-02-06 11:51:29 +1030728 mctp_prdebug("%s: Enqueued %d packets", __func__, i);
729
Jeremy Kerrcc2458d2019-03-01 08:23:33 +0800730 mctp_send_tx_queue(bus);
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800731
732 return 0;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800733}
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800734
735int mctp_message_tx(struct mctp *mctp, mctp_eid_t eid,
736 void *msg, size_t msg_len)
737{
738 struct mctp_bus *bus;
739
740 bus = find_bus_for_eid(mctp, eid);
Andrew Jefferyb93b6112020-06-05 14:13:44 +0930741 return mctp_message_tx_on_bus(bus, bus->eid, eid, msg, msg_len);
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800742}