blob: 7b1d07260c6b7b3fc9deb0a8c8bc333f1be17309 [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
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +080093 size = binding->pkt_size + binding->pkt_pad;
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;
99 buf->start = binding->pkt_pad;
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
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800159/* Message reassembly */
160static struct mctp_msg_ctx *mctp_msg_ctx_lookup(struct mctp *mctp,
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800161 uint8_t src, uint8_t dest, uint8_t tag)
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800162{
163 unsigned int i;
164
165 /* @todo: better lookup, if we add support for more outstanding
166 * message contexts */
167 for (i = 0; i < ARRAY_SIZE(mctp->msg_ctxs); i++) {
168 struct mctp_msg_ctx *ctx = &mctp->msg_ctxs[i];
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800169 if (ctx->src == src && ctx->dest == dest && ctx->tag == tag)
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800170 return ctx;
171 }
172
173 return NULL;
174}
175
176static struct mctp_msg_ctx *mctp_msg_ctx_create(struct mctp *mctp,
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800177 uint8_t src, uint8_t dest, uint8_t tag)
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800178{
Jeremy Kerr11a234e2019-02-27 17:59:53 +0800179 struct mctp_msg_ctx *ctx = NULL;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800180 unsigned int i;
181
182 for (i = 0; i < ARRAY_SIZE(mctp->msg_ctxs); i++) {
183 struct mctp_msg_ctx *tmp = &mctp->msg_ctxs[i];
184 if (!tmp->src) {
185 ctx = tmp;
186 break;
187 }
188 }
189
190 if (!ctx)
191 return NULL;
192
193 ctx->src = src;
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800194 ctx->dest = dest;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800195 ctx->tag = tag;
Jeremy Kerr9a3da812019-08-02 15:57:53 +0800196 ctx->buf_size = 0;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800197
198 return ctx;
199}
200
201static void mctp_msg_ctx_drop(struct mctp_msg_ctx *ctx)
202{
203 ctx->src = 0;
204}
205
206static void mctp_msg_ctx_reset(struct mctp_msg_ctx *ctx)
207{
208 ctx->buf_size = 0;
209}
210
211static int mctp_msg_ctx_add_pkt(struct mctp_msg_ctx *ctx,
Sumanth Bhat2c820c52020-07-02 00:26:25 +0530212 struct mctp_pktbuf *pkt, size_t max_size)
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800213{
214 size_t len;
215
216 len = mctp_pktbuf_size(pkt) - sizeof(struct mctp_hdr);
217
218 if (ctx->buf_size + len > ctx->buf_alloc_size) {
219 size_t new_alloc_size;
Andrew Jeffery00ecc6c2020-03-10 23:16:53 +1030220 void *lbuf;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800221
Andrew Jeffery5a508912020-11-03 22:21:45 +1030222 /* @todo: finer-grained allocation */
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800223 if (!ctx->buf_alloc_size) {
Andrew Jefferyc2b833e2020-10-28 14:28:37 +1030224 new_alloc_size = MAX(len, 4096UL);
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800225 } else {
226 new_alloc_size = ctx->buf_alloc_size * 2;
227 }
Andrew Jeffery00ecc6c2020-03-10 23:16:53 +1030228
Sumanth Bhat2c820c52020-07-02 00:26:25 +0530229 /* Don't allow heap to grow beyond a limit */
230 if (new_alloc_size > max_size)
231 return -1;
232
233
Andrew Jeffery00ecc6c2020-03-10 23:16:53 +1030234 lbuf = __mctp_realloc(ctx->buf, new_alloc_size);
235 if (lbuf) {
236 ctx->buf = lbuf;
237 ctx->buf_alloc_size = new_alloc_size;
238 } else {
239 __mctp_free(ctx->buf);
240 return -1;
241 }
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800242 }
243
244 memcpy(ctx->buf + ctx->buf_size, mctp_pktbuf_data(pkt), len);
245 ctx->buf_size += len;
246
247 return 0;
248}
249
250/* Core API functions */
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800251struct mctp *mctp_init(void)
252{
253 struct mctp *mctp;
254
255 mctp = __mctp_alloc(sizeof(*mctp));
Sumanth Bhat96d54492020-07-14 17:10:04 +0530256
257 if(!mctp)
258 return NULL;
259
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800260 memset(mctp, 0, sizeof(*mctp));
Sumanth Bhat2c820c52020-07-02 00:26:25 +0530261 mctp->max_message_size = MCTP_MAX_MESSAGE_SIZE;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800262
263 return mctp;
264}
265
Sumanth Bhat2c820c52020-07-02 00:26:25 +0530266void mctp_set_max_message_size(struct mctp *mctp, size_t message_size)
267{
268 mctp->max_message_size = message_size;
269}
270
Andrew Jeffery3ae89dc2021-01-28 15:24:36 +1030271static void mctp_bus_destroy(struct mctp_bus *bus)
272{
273 while (bus->tx_queue_head) {
274 struct mctp_pktbuf *curr = bus->tx_queue_head;
275
276 bus->tx_queue_head = curr->next;
277 mctp_pktbuf_free(curr);
278 }
279}
280
Andrew Jefferyfa56ca52020-03-10 23:18:22 +1030281void mctp_destroy(struct mctp *mctp)
282{
Andrew Jefferyb93b6112020-06-05 14:13:44 +0930283 size_t i;
Andrew Jefferyfa56ca52020-03-10 23:18:22 +1030284
285 /* Cleanup message assembly contexts */
Andrew Jefferyb93b6112020-06-05 14:13:44 +0930286 BUILD_ASSERT(ARRAY_SIZE(mctp->msg_ctxs) < SIZE_MAX);
Andrew Jefferyfa56ca52020-03-10 23:18:22 +1030287 for (i = 0; i < ARRAY_SIZE(mctp->msg_ctxs); i++) {
288 struct mctp_msg_ctx *tmp = &mctp->msg_ctxs[i];
289 if (tmp->buf)
290 __mctp_free(tmp->buf);
291 }
292
Andrew Jeffery3ae89dc2021-01-28 15:24:36 +1030293 while (mctp->n_busses--)
294 mctp_bus_destroy(&mctp->busses[mctp->n_busses]);
295
Andrew Jefferyfa56ca52020-03-10 23:18:22 +1030296 __mctp_free(mctp->busses);
297 __mctp_free(mctp);
298}
299
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800300int mctp_set_rx_all(struct mctp *mctp, mctp_rx_fn fn, void *data)
301{
302 mctp->message_rx = fn;
303 mctp->message_rx_data = data;
304 return 0;
305}
306
307static struct mctp_bus *find_bus_for_eid(struct mctp *mctp,
308 mctp_eid_t dest __attribute__((unused)))
309{
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800310 /* for now, just use the first bus. For full routing support,
311 * we will need a table of neighbours */
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800312 return &mctp->busses[0];
313}
314
Jeremy Kerr7520cec2019-03-01 07:13:18 +0800315int mctp_register_bus(struct mctp *mctp,
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800316 struct mctp_binding *binding,
317 mctp_eid_t eid)
318{
Andrew Jeffery3e8a12a2020-06-05 16:08:30 +0930319 int rc = 0;
320
Jeremy Kerr7520cec2019-03-01 07:13:18 +0800321 /* todo: multiple busses */
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800322 assert(mctp->n_busses == 0);
323 mctp->n_busses = 1;
Andrew Jeffery3e8a12a2020-06-05 16:08:30 +0930324
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800325 mctp->busses = __mctp_alloc(sizeof(struct mctp_bus));
Andrew Jeffery3e8a12a2020-06-05 16:08:30 +0930326 if (!mctp->busses)
327 return -ENOMEM;
328
James Feist62d72362019-12-13 13:43:32 -0800329 memset(mctp->busses, 0, sizeof(struct mctp_bus));
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800330 mctp->busses[0].binding = binding;
331 mctp->busses[0].eid = eid;
Jeremy Kerr7520cec2019-03-01 07:13:18 +0800332 binding->bus = &mctp->busses[0];
Jeremy Kerr0a00dca2019-03-01 08:01:35 +0800333 binding->mctp = mctp;
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800334 mctp->route_policy = ROUTE_ENDPOINT;
Jeremy Kerr3b36d172019-09-04 11:56:09 +0800335
Andrew Jeffery3e8a12a2020-06-05 16:08:30 +0930336 if (binding->start) {
337 rc = binding->start(binding);
338 if (rc < 0) {
339 mctp_prerr("Failed to start binding: %d", rc);
Andrew Jeffery19275232021-01-29 14:13:25 +1030340 binding->bus = NULL;
Andrew Jeffery3e8a12a2020-06-05 16:08:30 +0930341 __mctp_free(mctp->busses);
342 mctp->busses = NULL;
Andrew Jeffery2304c832021-01-29 11:52:49 +1030343 mctp->n_busses = 0;
Andrew Jeffery3e8a12a2020-06-05 16:08:30 +0930344 }
345 }
Jeremy Kerr3b36d172019-09-04 11:56:09 +0800346
Andrew Jeffery3e8a12a2020-06-05 16:08:30 +0930347 return rc;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800348}
349
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800350int mctp_bridge_busses(struct mctp *mctp,
351 struct mctp_binding *b1, struct mctp_binding *b2)
352{
Andrew Jeffery19275232021-01-29 14:13:25 +1030353 int rc = 0;
354
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800355 assert(mctp->n_busses == 0);
356 mctp->busses = __mctp_alloc(2 * sizeof(struct mctp_bus));
James Feist62d72362019-12-13 13:43:32 -0800357 memset(mctp->busses, 0, 2 * sizeof(struct mctp_bus));
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800358 mctp->n_busses = 2;
359 mctp->busses[0].binding = b1;
360 b1->bus = &mctp->busses[0];
361 b1->mctp = mctp;
362 mctp->busses[1].binding = b2;
363 b2->bus = &mctp->busses[1];
364 b2->mctp = mctp;
365
366 mctp->route_policy = ROUTE_BRIDGE;
Jeremy Kerr3b36d172019-09-04 11:56:09 +0800367
Andrew Jeffery19275232021-01-29 14:13:25 +1030368 if (b1->start) {
369 rc = b1->start(b1);
370 if (rc < 0) {
371 mctp_prerr("Failed to start bridged bus %s: %d",
372 b1->name, rc);
373 goto done;
374 }
375 }
Jeremy Kerr3b36d172019-09-04 11:56:09 +0800376
Andrew Jeffery19275232021-01-29 14:13:25 +1030377 if (b2->start) {
378 rc = b2->start(b2);
379 if (rc < 0) {
380 mctp_prerr("Failed to start bridged bus %s: %d",
381 b2->name, rc);
382 goto done;
383 }
384 }
Jeremy Kerr3b36d172019-09-04 11:56:09 +0800385
Andrew Jeffery19275232021-01-29 14:13:25 +1030386done:
387 return rc;
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800388}
389
Wiktor GoĊ‚gowskiba6727e2020-03-13 18:25:01 +0100390static inline bool mctp_ctrl_cmd_is_transport(struct mctp_ctrl_msg_hdr *hdr)
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800391{
Wiktor GoĊ‚gowskiba6727e2020-03-13 18:25:01 +0100392 return ((hdr->command_code >= MCTP_CTRL_CMD_FIRST_TRANSPORT) &&
393 (hdr->command_code <= MCTP_CTRL_CMD_LAST_TRANSPORT));
394}
395
Andrew Jefferyb93b6112020-06-05 14:13:44 +0930396static bool mctp_ctrl_handle_msg(struct mctp_bus *bus, mctp_eid_t src,
397 void *buffer, size_t length)
Wiktor GoĊ‚gowskiba6727e2020-03-13 18:25:01 +0100398{
399 struct mctp_ctrl_msg_hdr *msg_hdr = buffer;
400
401 /*
402 * Control message is received. If a transport control message handler
403 * is provided, it will called. If there is no dedicated handler, this
404 * function returns false and data can be handled by the generic
405 * message handler. The transport control message handler will be
406 * provided with messages in the command range 0xF0 - 0xFF.
407 */
408 if (mctp_ctrl_cmd_is_transport(msg_hdr)) {
409 if (bus->binding->control_rx != NULL) {
410 /* MCTP bus binding handler */
411 bus->binding->control_rx(src,
412 bus->binding->control_rx_data,
413 buffer, length);
414 return true;
415 }
416 }
417
418 /*
419 * Command was not handled, due to lack of specific callback.
420 * It will be passed to regular message_rx handler.
421 */
422 return false;
423}
424
425static inline bool mctp_rx_dest_is_local(struct mctp_bus *bus, mctp_eid_t dest)
426{
427 return dest == bus->eid || dest == MCTP_EID_NULL ||
428 dest == MCTP_EID_BROADCAST;
429}
430
431static inline bool mctp_ctrl_cmd_is_request(struct mctp_ctrl_msg_hdr *hdr)
432{
433 return hdr->ic_msg_type == MCTP_CTRL_HDR_MSG_TYPE &&
434 hdr->rq_dgram_inst & MCTP_CTRL_HDR_FLAG_REQUEST;
435}
436
437/*
438 * Receive the complete MCTP message and route it.
439 * Asserts:
440 * 'buf' is not NULL.
441 */
442static void mctp_rx(struct mctp *mctp, struct mctp_bus *bus, mctp_eid_t src,
443 mctp_eid_t dest, void *buf, size_t len)
444{
445 assert(buf != NULL);
446
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800447 if (mctp->route_policy == ROUTE_ENDPOINT &&
Wiktor GoĊ‚gowskiba6727e2020-03-13 18:25:01 +0100448 mctp_rx_dest_is_local(bus, dest)) {
449 /* Handle MCTP Control Messages: */
450 if (len >= sizeof(struct mctp_ctrl_msg_hdr)) {
451 struct mctp_ctrl_msg_hdr *msg_hdr = buf;
452
453 /*
454 * Identify if this is a control request message.
455 * See DSP0236 v1.3.0 sec. 11.5.
456 */
457 if (mctp_ctrl_cmd_is_request(msg_hdr)) {
458 bool handled;
Andrew Jefferyb93b6112020-06-05 14:13:44 +0930459 handled = mctp_ctrl_handle_msg(bus, src, buf,
460 len);
Wiktor GoĊ‚gowskiba6727e2020-03-13 18:25:01 +0100461 if (handled)
462 return;
463 }
464 }
465 if (mctp->message_rx)
466 mctp->message_rx(src, mctp->message_rx_data, buf, len);
467 }
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800468
469 if (mctp->route_policy == ROUTE_BRIDGE) {
470 int i;
471
472 for (i = 0; i < mctp->n_busses; i++) {
473 struct mctp_bus *dest_bus = &mctp->busses[i];
474 if (dest_bus == bus)
475 continue;
476
Andrew Jefferyb93b6112020-06-05 14:13:44 +0930477 mctp_message_tx_on_bus(dest_bus, src, dest, buf, len);
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800478 }
479
480 }
481}
482
Jeremy Kerr0a00dca2019-03-01 08:01:35 +0800483void mctp_bus_rx(struct mctp_binding *binding, struct mctp_pktbuf *pkt)
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800484{
Jeremy Kerr7520cec2019-03-01 07:13:18 +0800485 struct mctp_bus *bus = binding->bus;
Jeremy Kerr0a00dca2019-03-01 08:01:35 +0800486 struct mctp *mctp = binding->mctp;
Ed Tanousc2def9f2019-02-21 08:33:08 -0800487 uint8_t flags, exp_seq, seq, tag;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800488 struct mctp_msg_ctx *ctx;
489 struct mctp_hdr *hdr;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800490 size_t len;
491 void *p;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800492 int rc;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800493
Jeremy Kerr7520cec2019-03-01 07:13:18 +0800494 assert(bus);
495
Sumanth Bhatd97869d2020-07-02 00:46:13 +0530496 /* Drop packet if it was smaller than mctp hdr size */
497 if (mctp_pktbuf_size(pkt) <= sizeof(struct mctp_hdr))
498 goto out;
499
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800500 hdr = mctp_pktbuf_hdr(pkt);
501
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800502 /* small optimisation: don't bother reassembly if we're going to
503 * drop the packet in mctp_rx anyway */
504 if (mctp->route_policy == ROUTE_ENDPOINT && hdr->dest != bus->eid)
Jeremy Kerrc1693af2019-08-05 14:30:59 +0800505 goto out;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800506
507 flags = hdr->flags_seq_tag & (MCTP_HDR_FLAG_SOM | MCTP_HDR_FLAG_EOM);
508 tag = (hdr->flags_seq_tag >> MCTP_HDR_TAG_SHIFT) & MCTP_HDR_TAG_MASK;
509 seq = (hdr->flags_seq_tag >> MCTP_HDR_SEQ_SHIFT) & MCTP_HDR_SEQ_MASK;
510
511 switch (flags) {
512 case MCTP_HDR_FLAG_SOM | MCTP_HDR_FLAG_EOM:
513 /* single-packet message - send straight up to rx function,
514 * no need to create a message context */
515 len = pkt->end - pkt->mctp_hdr_off - sizeof(struct mctp_hdr);
Andrew Jefferyb4ae00b2021-01-18 15:52:09 +1030516 p = pkt->data + pkt->mctp_hdr_off + sizeof(struct mctp_hdr);
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800517 mctp_rx(mctp, bus, hdr->src, hdr->dest, p, len);
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800518 break;
519
520 case MCTP_HDR_FLAG_SOM:
521 /* start of a new message - start the new context for
522 * future message reception. If an existing context is
523 * already present, drop it. */
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800524 ctx = mctp_msg_ctx_lookup(mctp, hdr->src, hdr->dest, tag);
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800525 if (ctx) {
526 mctp_msg_ctx_reset(ctx);
527 } else {
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800528 ctx = mctp_msg_ctx_create(mctp,
529 hdr->src, hdr->dest, tag);
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800530 }
531
Sumanth Bhat2c820c52020-07-02 00:26:25 +0530532 rc = mctp_msg_ctx_add_pkt(ctx, pkt, mctp->max_message_size);
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800533 if (rc) {
534 mctp_msg_ctx_drop(ctx);
535 } else {
536 ctx->last_seq = seq;
537 }
538
539 break;
540
541 case MCTP_HDR_FLAG_EOM:
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800542 ctx = mctp_msg_ctx_lookup(mctp, hdr->src, hdr->dest, tag);
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800543 if (!ctx)
Jeremy Kerrc1693af2019-08-05 14:30:59 +0800544 goto out;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800545
Ed Tanousc2def9f2019-02-21 08:33:08 -0800546 exp_seq = (ctx->last_seq + 1) % 4;
547
548 if (exp_seq != seq) {
549 mctp_prdebug(
550 "Sequence number %d does not match expected %d",
551 seq, exp_seq);
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800552 mctp_msg_ctx_drop(ctx);
Jeremy Kerrc1693af2019-08-05 14:30:59 +0800553 goto out;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800554 }
555
Sumanth Bhat2c820c52020-07-02 00:26:25 +0530556 rc = mctp_msg_ctx_add_pkt(ctx, pkt, mctp->max_message_size);
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800557 if (!rc)
558 mctp_rx(mctp, bus, ctx->src, ctx->dest,
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800559 ctx->buf, ctx->buf_size);
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800560
561 mctp_msg_ctx_drop(ctx);
562 break;
Ed Tanousc2def9f2019-02-21 08:33:08 -0800563
564 case 0:
565 /* Neither SOM nor EOM */
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800566 ctx = mctp_msg_ctx_lookup(mctp, hdr->src,hdr->dest, tag);
Ed Tanousc2def9f2019-02-21 08:33:08 -0800567 if (!ctx)
Jeremy Kerrc1693af2019-08-05 14:30:59 +0800568 goto out;
Ed Tanousc2def9f2019-02-21 08:33:08 -0800569
570 exp_seq = (ctx->last_seq + 1) % 4;
571 if (exp_seq != seq) {
572 mctp_prdebug(
573 "Sequence number %d does not match expected %d",
574 seq, exp_seq);
575 mctp_msg_ctx_drop(ctx);
Jeremy Kerrc1693af2019-08-05 14:30:59 +0800576 goto out;
Ed Tanousc2def9f2019-02-21 08:33:08 -0800577 }
578
Sumanth Bhat2c820c52020-07-02 00:26:25 +0530579 rc = mctp_msg_ctx_add_pkt(ctx, pkt, mctp->max_message_size);
Ed Tanousc2def9f2019-02-21 08:33:08 -0800580 if (rc) {
581 mctp_msg_ctx_drop(ctx);
Jeremy Kerrc1693af2019-08-05 14:30:59 +0800582 goto out;
Ed Tanousc2def9f2019-02-21 08:33:08 -0800583 }
584 ctx->last_seq = seq;
585
586 break;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800587 }
Jeremy Kerrc1693af2019-08-05 14:30:59 +0800588out:
589 mctp_pktbuf_free(pkt);
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800590}
591
Jeremy Kerr0a00dca2019-03-01 08:01:35 +0800592static int mctp_packet_tx(struct mctp_bus *bus,
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800593 struct mctp_pktbuf *pkt)
594{
Andrew Jefferyc61501c2021-01-27 23:24:18 +1030595 if (bus->state != mctp_bus_state_tx_enabled)
Jeremy Kerr1cd31182019-02-27 18:01:00 +0800596 return -1;
597
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800598 return bus->binding->tx(bus->binding, pkt);
599}
600
Jeremy Kerrcc2458d2019-03-01 08:23:33 +0800601static void mctp_send_tx_queue(struct mctp_bus *bus)
Jeremy Kerr1cd31182019-02-27 18:01:00 +0800602{
603 struct mctp_pktbuf *pkt;
604
Jeremy Kerrcc2458d2019-03-01 08:23:33 +0800605 while ((pkt = bus->tx_queue_head)) {
Jeremy Kerr1cd31182019-02-27 18:01:00 +0800606 int rc;
607
608 rc = mctp_packet_tx(bus, pkt);
609 if (rc)
610 break;
611
Jeremy Kerrcc2458d2019-03-01 08:23:33 +0800612 bus->tx_queue_head = pkt->next;
Jeremy Kerr1cd31182019-02-27 18:01:00 +0800613 mctp_pktbuf_free(pkt);
614 }
615
Jeremy Kerrcc2458d2019-03-01 08:23:33 +0800616 if (!bus->tx_queue_head)
617 bus->tx_queue_tail = NULL;
Jeremy Kerr1cd31182019-02-27 18:01:00 +0800618
619}
620
621void mctp_binding_set_tx_enabled(struct mctp_binding *binding, bool enable)
622{
623 struct mctp_bus *bus = binding->bus;
Andrew Jefferyc61501c2021-01-27 23:24:18 +1030624
625 switch(bus->state) {
626 case mctp_bus_state_constructed:
627 if (!enable)
628 return;
629
Andrew Jeffery1fa707e2021-01-28 15:22:11 +1030630 if (binding->pkt_size < MCTP_PACKET_SIZE(MCTP_BTU)) {
631 mctp_prerr("Cannot start %s binding with invalid MTU: %zu",
632 binding->name,
633 MCTP_BODY_SIZE(binding->pkt_size));
634 return;
635 }
636
Andrew Jefferyc61501c2021-01-27 23:24:18 +1030637 bus->state = mctp_bus_state_tx_enabled;
638 mctp_prinfo("%s binding started", binding->name);
639 return;
640 case mctp_bus_state_tx_enabled:
641 if (enable)
642 return;
643
644 bus->state = mctp_bus_state_tx_disabled;
645 mctp_prdebug("%s binding Tx disabled", binding->name);
646 return;
647 case mctp_bus_state_tx_disabled:
648 if (!enable)
649 return;
650
651 bus->state = mctp_bus_state_tx_enabled;
652 mctp_prdebug("%s binding Tx enabled", binding->name);
Jeremy Kerrcc2458d2019-03-01 08:23:33 +0800653 mctp_send_tx_queue(bus);
Andrew Jefferyc61501c2021-01-27 23:24:18 +1030654 return;
655 }
Jeremy Kerr1cd31182019-02-27 18:01:00 +0800656}
657
Andrew Jefferyb93b6112020-06-05 14:13:44 +0930658static int mctp_message_tx_on_bus(struct mctp_bus *bus, mctp_eid_t src,
659 mctp_eid_t dest, void *msg, size_t msg_len)
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800660{
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +0800661 size_t max_payload_len, payload_len, p;
Jeremy Kerr1cd31182019-02-27 18:01:00 +0800662 struct mctp_pktbuf *pkt;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800663 struct mctp_hdr *hdr;
Jeremy Kerrc855d7b2019-08-01 21:18:09 +0800664 int i;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800665
Andrew Jefferyc61501c2021-01-27 23:24:18 +1030666 if (bus->state == mctp_bus_state_constructed)
667 return -ENXIO;
668
Andrew Jeffery1fa707e2021-01-28 15:22:11 +1030669 max_payload_len = MCTP_BODY_SIZE(bus->binding->pkt_size);
670
671 {
672 const bool valid_mtu = max_payload_len >= MCTP_BTU;
673 assert(valid_mtu);
674 if (!valid_mtu)
675 return -EINVAL;
676 }
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800677
Andrew Jeffery298865f2020-02-06 11:51:29 +1030678 mctp_prdebug("%s: Generating packets for transmission of %zu byte message from %hhu to %hhu",
679 __func__, msg_len, src, dest);
680
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800681 /* queue up packets, each of max MCTP_MTU size */
Jeremy Kerrc855d7b2019-08-01 21:18:09 +0800682 for (p = 0, i = 0; p < msg_len; i++) {
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +0800683 payload_len = msg_len - p;
684 if (payload_len > max_payload_len)
685 payload_len = max_payload_len;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800686
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800687 pkt = mctp_pktbuf_alloc(bus->binding,
688 payload_len + sizeof(*hdr));
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800689 hdr = mctp_pktbuf_hdr(pkt);
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800690
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800691 /* todo: tags */
692 hdr->ver = bus->binding->version & 0xf;
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800693 hdr->dest = dest;
694 hdr->src = src;
Jeremy Kerrc855d7b2019-08-01 21:18:09 +0800695 hdr->flags_seq_tag = MCTP_HDR_FLAG_TO |
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800696 (0 << MCTP_HDR_TAG_SHIFT);
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800697
Jeremy Kerrc855d7b2019-08-01 21:18:09 +0800698 if (i == 0)
699 hdr->flags_seq_tag |= MCTP_HDR_FLAG_SOM;
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +0800700 if (p + payload_len >= msg_len)
Jeremy Kerrc855d7b2019-08-01 21:18:09 +0800701 hdr->flags_seq_tag |= MCTP_HDR_FLAG_EOM;
702 hdr->flags_seq_tag |=
703 (i & MCTP_HDR_SEQ_MASK) << MCTP_HDR_SEQ_SHIFT;
704
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +0800705 memcpy(mctp_pktbuf_data(pkt), msg + p, payload_len);
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800706
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800707 /* add to tx queue */
Jeremy Kerrcc2458d2019-03-01 08:23:33 +0800708 if (bus->tx_queue_tail)
709 bus->tx_queue_tail->next = pkt;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800710 else
Jeremy Kerrcc2458d2019-03-01 08:23:33 +0800711 bus->tx_queue_head = pkt;
712 bus->tx_queue_tail = pkt;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800713
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +0800714 p += payload_len;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800715 }
716
Andrew Jeffery298865f2020-02-06 11:51:29 +1030717 mctp_prdebug("%s: Enqueued %d packets", __func__, i);
718
Jeremy Kerrcc2458d2019-03-01 08:23:33 +0800719 mctp_send_tx_queue(bus);
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800720
721 return 0;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800722}
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800723
724int mctp_message_tx(struct mctp *mctp, mctp_eid_t eid,
725 void *msg, size_t msg_len)
726{
727 struct mctp_bus *bus;
728
729 bus = find_bus_for_eid(mctp, eid);
Andrew Jefferyb93b6112020-06-05 14:13:44 +0930730 return mctp_message_tx_on_bus(bus, bus->eid, eid, msg, msg_len);
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800731}