blob: 0aca12b5140906ba2d4afb76a5780829b53213bf [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);
340 __mctp_free(mctp->busses);
341 mctp->busses = NULL;
Andrew Jeffery2304c832021-01-29 11:52:49 +1030342 mctp->n_busses = 0;
Andrew Jeffery3e8a12a2020-06-05 16:08:30 +0930343 }
344 }
Jeremy Kerr3b36d172019-09-04 11:56:09 +0800345
Andrew Jeffery3e8a12a2020-06-05 16:08:30 +0930346 return rc;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800347}
348
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800349int mctp_bridge_busses(struct mctp *mctp,
350 struct mctp_binding *b1, struct mctp_binding *b2)
351{
352 assert(mctp->n_busses == 0);
353 mctp->busses = __mctp_alloc(2 * sizeof(struct mctp_bus));
James Feist62d72362019-12-13 13:43:32 -0800354 memset(mctp->busses, 0, 2 * sizeof(struct mctp_bus));
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800355 mctp->n_busses = 2;
356 mctp->busses[0].binding = b1;
357 b1->bus = &mctp->busses[0];
358 b1->mctp = mctp;
359 mctp->busses[1].binding = b2;
360 b2->bus = &mctp->busses[1];
361 b2->mctp = mctp;
362
363 mctp->route_policy = ROUTE_BRIDGE;
Jeremy Kerr3b36d172019-09-04 11:56:09 +0800364
365 if (b1->start)
366 b1->start(b1);
367
368 if (b2->start)
369 b2->start(b2);
370
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800371 return 0;
372}
373
Wiktor GoĊ‚gowskiba6727e2020-03-13 18:25:01 +0100374static inline bool mctp_ctrl_cmd_is_transport(struct mctp_ctrl_msg_hdr *hdr)
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800375{
Wiktor GoĊ‚gowskiba6727e2020-03-13 18:25:01 +0100376 return ((hdr->command_code >= MCTP_CTRL_CMD_FIRST_TRANSPORT) &&
377 (hdr->command_code <= MCTP_CTRL_CMD_LAST_TRANSPORT));
378}
379
Andrew Jefferyb93b6112020-06-05 14:13:44 +0930380static bool mctp_ctrl_handle_msg(struct mctp_bus *bus, mctp_eid_t src,
381 void *buffer, size_t length)
Wiktor GoĊ‚gowskiba6727e2020-03-13 18:25:01 +0100382{
383 struct mctp_ctrl_msg_hdr *msg_hdr = buffer;
384
385 /*
386 * Control message is received. If a transport control message handler
387 * is provided, it will called. If there is no dedicated handler, this
388 * function returns false and data can be handled by the generic
389 * message handler. The transport control message handler will be
390 * provided with messages in the command range 0xF0 - 0xFF.
391 */
392 if (mctp_ctrl_cmd_is_transport(msg_hdr)) {
393 if (bus->binding->control_rx != NULL) {
394 /* MCTP bus binding handler */
395 bus->binding->control_rx(src,
396 bus->binding->control_rx_data,
397 buffer, length);
398 return true;
399 }
400 }
401
402 /*
403 * Command was not handled, due to lack of specific callback.
404 * It will be passed to regular message_rx handler.
405 */
406 return false;
407}
408
409static inline bool mctp_rx_dest_is_local(struct mctp_bus *bus, mctp_eid_t dest)
410{
411 return dest == bus->eid || dest == MCTP_EID_NULL ||
412 dest == MCTP_EID_BROADCAST;
413}
414
415static inline bool mctp_ctrl_cmd_is_request(struct mctp_ctrl_msg_hdr *hdr)
416{
417 return hdr->ic_msg_type == MCTP_CTRL_HDR_MSG_TYPE &&
418 hdr->rq_dgram_inst & MCTP_CTRL_HDR_FLAG_REQUEST;
419}
420
421/*
422 * Receive the complete MCTP message and route it.
423 * Asserts:
424 * 'buf' is not NULL.
425 */
426static void mctp_rx(struct mctp *mctp, struct mctp_bus *bus, mctp_eid_t src,
427 mctp_eid_t dest, void *buf, size_t len)
428{
429 assert(buf != NULL);
430
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800431 if (mctp->route_policy == ROUTE_ENDPOINT &&
Wiktor GoĊ‚gowskiba6727e2020-03-13 18:25:01 +0100432 mctp_rx_dest_is_local(bus, dest)) {
433 /* Handle MCTP Control Messages: */
434 if (len >= sizeof(struct mctp_ctrl_msg_hdr)) {
435 struct mctp_ctrl_msg_hdr *msg_hdr = buf;
436
437 /*
438 * Identify if this is a control request message.
439 * See DSP0236 v1.3.0 sec. 11.5.
440 */
441 if (mctp_ctrl_cmd_is_request(msg_hdr)) {
442 bool handled;
Andrew Jefferyb93b6112020-06-05 14:13:44 +0930443 handled = mctp_ctrl_handle_msg(bus, src, buf,
444 len);
Wiktor GoĊ‚gowskiba6727e2020-03-13 18:25:01 +0100445 if (handled)
446 return;
447 }
448 }
449 if (mctp->message_rx)
450 mctp->message_rx(src, mctp->message_rx_data, buf, len);
451 }
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800452
453 if (mctp->route_policy == ROUTE_BRIDGE) {
454 int i;
455
456 for (i = 0; i < mctp->n_busses; i++) {
457 struct mctp_bus *dest_bus = &mctp->busses[i];
458 if (dest_bus == bus)
459 continue;
460
Andrew Jefferyb93b6112020-06-05 14:13:44 +0930461 mctp_message_tx_on_bus(dest_bus, src, dest, buf, len);
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800462 }
463
464 }
465}
466
Jeremy Kerr0a00dca2019-03-01 08:01:35 +0800467void mctp_bus_rx(struct mctp_binding *binding, struct mctp_pktbuf *pkt)
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800468{
Jeremy Kerr7520cec2019-03-01 07:13:18 +0800469 struct mctp_bus *bus = binding->bus;
Jeremy Kerr0a00dca2019-03-01 08:01:35 +0800470 struct mctp *mctp = binding->mctp;
Ed Tanousc2def9f2019-02-21 08:33:08 -0800471 uint8_t flags, exp_seq, seq, tag;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800472 struct mctp_msg_ctx *ctx;
473 struct mctp_hdr *hdr;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800474 size_t len;
475 void *p;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800476 int rc;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800477
Jeremy Kerr7520cec2019-03-01 07:13:18 +0800478 assert(bus);
479
Sumanth Bhatd97869d2020-07-02 00:46:13 +0530480 /* Drop packet if it was smaller than mctp hdr size */
481 if (mctp_pktbuf_size(pkt) <= sizeof(struct mctp_hdr))
482 goto out;
483
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800484 hdr = mctp_pktbuf_hdr(pkt);
485
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800486 /* small optimisation: don't bother reassembly if we're going to
487 * drop the packet in mctp_rx anyway */
488 if (mctp->route_policy == ROUTE_ENDPOINT && hdr->dest != bus->eid)
Jeremy Kerrc1693af2019-08-05 14:30:59 +0800489 goto out;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800490
491 flags = hdr->flags_seq_tag & (MCTP_HDR_FLAG_SOM | MCTP_HDR_FLAG_EOM);
492 tag = (hdr->flags_seq_tag >> MCTP_HDR_TAG_SHIFT) & MCTP_HDR_TAG_MASK;
493 seq = (hdr->flags_seq_tag >> MCTP_HDR_SEQ_SHIFT) & MCTP_HDR_SEQ_MASK;
494
495 switch (flags) {
496 case MCTP_HDR_FLAG_SOM | MCTP_HDR_FLAG_EOM:
497 /* single-packet message - send straight up to rx function,
498 * no need to create a message context */
499 len = pkt->end - pkt->mctp_hdr_off - sizeof(struct mctp_hdr);
Andrew Jefferyb4ae00b2021-01-18 15:52:09 +1030500 p = pkt->data + pkt->mctp_hdr_off + sizeof(struct mctp_hdr);
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800501 mctp_rx(mctp, bus, hdr->src, hdr->dest, p, len);
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800502 break;
503
504 case MCTP_HDR_FLAG_SOM:
505 /* start of a new message - start the new context for
506 * future message reception. If an existing context is
507 * already present, drop it. */
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800508 ctx = mctp_msg_ctx_lookup(mctp, hdr->src, hdr->dest, tag);
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800509 if (ctx) {
510 mctp_msg_ctx_reset(ctx);
511 } else {
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800512 ctx = mctp_msg_ctx_create(mctp,
513 hdr->src, hdr->dest, tag);
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800514 }
515
Sumanth Bhat2c820c52020-07-02 00:26:25 +0530516 rc = mctp_msg_ctx_add_pkt(ctx, pkt, mctp->max_message_size);
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800517 if (rc) {
518 mctp_msg_ctx_drop(ctx);
519 } else {
520 ctx->last_seq = seq;
521 }
522
523 break;
524
525 case MCTP_HDR_FLAG_EOM:
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800526 ctx = mctp_msg_ctx_lookup(mctp, hdr->src, hdr->dest, tag);
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800527 if (!ctx)
Jeremy Kerrc1693af2019-08-05 14:30:59 +0800528 goto out;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800529
Ed Tanousc2def9f2019-02-21 08:33:08 -0800530 exp_seq = (ctx->last_seq + 1) % 4;
531
532 if (exp_seq != seq) {
533 mctp_prdebug(
534 "Sequence number %d does not match expected %d",
535 seq, exp_seq);
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800536 mctp_msg_ctx_drop(ctx);
Jeremy Kerrc1693af2019-08-05 14:30:59 +0800537 goto out;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800538 }
539
Sumanth Bhat2c820c52020-07-02 00:26:25 +0530540 rc = mctp_msg_ctx_add_pkt(ctx, pkt, mctp->max_message_size);
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800541 if (!rc)
542 mctp_rx(mctp, bus, ctx->src, ctx->dest,
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800543 ctx->buf, ctx->buf_size);
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800544
545 mctp_msg_ctx_drop(ctx);
546 break;
Ed Tanousc2def9f2019-02-21 08:33:08 -0800547
548 case 0:
549 /* Neither SOM nor EOM */
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800550 ctx = mctp_msg_ctx_lookup(mctp, hdr->src,hdr->dest, tag);
Ed Tanousc2def9f2019-02-21 08:33:08 -0800551 if (!ctx)
Jeremy Kerrc1693af2019-08-05 14:30:59 +0800552 goto out;
Ed Tanousc2def9f2019-02-21 08:33:08 -0800553
554 exp_seq = (ctx->last_seq + 1) % 4;
555 if (exp_seq != seq) {
556 mctp_prdebug(
557 "Sequence number %d does not match expected %d",
558 seq, exp_seq);
559 mctp_msg_ctx_drop(ctx);
Jeremy Kerrc1693af2019-08-05 14:30:59 +0800560 goto out;
Ed Tanousc2def9f2019-02-21 08:33:08 -0800561 }
562
Sumanth Bhat2c820c52020-07-02 00:26:25 +0530563 rc = mctp_msg_ctx_add_pkt(ctx, pkt, mctp->max_message_size);
Ed Tanousc2def9f2019-02-21 08:33:08 -0800564 if (rc) {
565 mctp_msg_ctx_drop(ctx);
Jeremy Kerrc1693af2019-08-05 14:30:59 +0800566 goto out;
Ed Tanousc2def9f2019-02-21 08:33:08 -0800567 }
568 ctx->last_seq = seq;
569
570 break;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800571 }
Jeremy Kerrc1693af2019-08-05 14:30:59 +0800572out:
573 mctp_pktbuf_free(pkt);
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800574}
575
Jeremy Kerr0a00dca2019-03-01 08:01:35 +0800576static int mctp_packet_tx(struct mctp_bus *bus,
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800577 struct mctp_pktbuf *pkt)
578{
Andrew Jefferyc61501c2021-01-27 23:24:18 +1030579 if (bus->state != mctp_bus_state_tx_enabled)
Jeremy Kerr1cd31182019-02-27 18:01:00 +0800580 return -1;
581
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800582 return bus->binding->tx(bus->binding, pkt);
583}
584
Jeremy Kerrcc2458d2019-03-01 08:23:33 +0800585static void mctp_send_tx_queue(struct mctp_bus *bus)
Jeremy Kerr1cd31182019-02-27 18:01:00 +0800586{
587 struct mctp_pktbuf *pkt;
588
Jeremy Kerrcc2458d2019-03-01 08:23:33 +0800589 while ((pkt = bus->tx_queue_head)) {
Jeremy Kerr1cd31182019-02-27 18:01:00 +0800590 int rc;
591
592 rc = mctp_packet_tx(bus, pkt);
593 if (rc)
594 break;
595
Jeremy Kerrcc2458d2019-03-01 08:23:33 +0800596 bus->tx_queue_head = pkt->next;
Jeremy Kerr1cd31182019-02-27 18:01:00 +0800597 mctp_pktbuf_free(pkt);
598 }
599
Jeremy Kerrcc2458d2019-03-01 08:23:33 +0800600 if (!bus->tx_queue_head)
601 bus->tx_queue_tail = NULL;
Jeremy Kerr1cd31182019-02-27 18:01:00 +0800602
603}
604
605void mctp_binding_set_tx_enabled(struct mctp_binding *binding, bool enable)
606{
607 struct mctp_bus *bus = binding->bus;
Andrew Jefferyc61501c2021-01-27 23:24:18 +1030608
609 switch(bus->state) {
610 case mctp_bus_state_constructed:
611 if (!enable)
612 return;
613
Andrew Jeffery1fa707e2021-01-28 15:22:11 +1030614 if (binding->pkt_size < MCTP_PACKET_SIZE(MCTP_BTU)) {
615 mctp_prerr("Cannot start %s binding with invalid MTU: %zu",
616 binding->name,
617 MCTP_BODY_SIZE(binding->pkt_size));
618 return;
619 }
620
Andrew Jefferyc61501c2021-01-27 23:24:18 +1030621 bus->state = mctp_bus_state_tx_enabled;
622 mctp_prinfo("%s binding started", binding->name);
623 return;
624 case mctp_bus_state_tx_enabled:
625 if (enable)
626 return;
627
628 bus->state = mctp_bus_state_tx_disabled;
629 mctp_prdebug("%s binding Tx disabled", binding->name);
630 return;
631 case mctp_bus_state_tx_disabled:
632 if (!enable)
633 return;
634
635 bus->state = mctp_bus_state_tx_enabled;
636 mctp_prdebug("%s binding Tx enabled", binding->name);
Jeremy Kerrcc2458d2019-03-01 08:23:33 +0800637 mctp_send_tx_queue(bus);
Andrew Jefferyc61501c2021-01-27 23:24:18 +1030638 return;
639 }
Jeremy Kerr1cd31182019-02-27 18:01:00 +0800640}
641
Andrew Jefferyb93b6112020-06-05 14:13:44 +0930642static int mctp_message_tx_on_bus(struct mctp_bus *bus, mctp_eid_t src,
643 mctp_eid_t dest, void *msg, size_t msg_len)
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800644{
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +0800645 size_t max_payload_len, payload_len, p;
Jeremy Kerr1cd31182019-02-27 18:01:00 +0800646 struct mctp_pktbuf *pkt;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800647 struct mctp_hdr *hdr;
Jeremy Kerrc855d7b2019-08-01 21:18:09 +0800648 int i;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800649
Andrew Jefferyc61501c2021-01-27 23:24:18 +1030650 if (bus->state == mctp_bus_state_constructed)
651 return -ENXIO;
652
Andrew Jeffery1fa707e2021-01-28 15:22:11 +1030653 max_payload_len = MCTP_BODY_SIZE(bus->binding->pkt_size);
654
655 {
656 const bool valid_mtu = max_payload_len >= MCTP_BTU;
657 assert(valid_mtu);
658 if (!valid_mtu)
659 return -EINVAL;
660 }
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800661
Andrew Jeffery298865f2020-02-06 11:51:29 +1030662 mctp_prdebug("%s: Generating packets for transmission of %zu byte message from %hhu to %hhu",
663 __func__, msg_len, src, dest);
664
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800665 /* queue up packets, each of max MCTP_MTU size */
Jeremy Kerrc855d7b2019-08-01 21:18:09 +0800666 for (p = 0, i = 0; p < msg_len; i++) {
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +0800667 payload_len = msg_len - p;
668 if (payload_len > max_payload_len)
669 payload_len = max_payload_len;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800670
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800671 pkt = mctp_pktbuf_alloc(bus->binding,
672 payload_len + sizeof(*hdr));
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800673 hdr = mctp_pktbuf_hdr(pkt);
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800674
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800675 /* todo: tags */
676 hdr->ver = bus->binding->version & 0xf;
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800677 hdr->dest = dest;
678 hdr->src = src;
Jeremy Kerrc855d7b2019-08-01 21:18:09 +0800679 hdr->flags_seq_tag = MCTP_HDR_FLAG_TO |
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800680 (0 << MCTP_HDR_TAG_SHIFT);
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800681
Jeremy Kerrc855d7b2019-08-01 21:18:09 +0800682 if (i == 0)
683 hdr->flags_seq_tag |= MCTP_HDR_FLAG_SOM;
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +0800684 if (p + payload_len >= msg_len)
Jeremy Kerrc855d7b2019-08-01 21:18:09 +0800685 hdr->flags_seq_tag |= MCTP_HDR_FLAG_EOM;
686 hdr->flags_seq_tag |=
687 (i & MCTP_HDR_SEQ_MASK) << MCTP_HDR_SEQ_SHIFT;
688
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +0800689 memcpy(mctp_pktbuf_data(pkt), msg + p, payload_len);
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800690
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800691 /* add to tx queue */
Jeremy Kerrcc2458d2019-03-01 08:23:33 +0800692 if (bus->tx_queue_tail)
693 bus->tx_queue_tail->next = pkt;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800694 else
Jeremy Kerrcc2458d2019-03-01 08:23:33 +0800695 bus->tx_queue_head = pkt;
696 bus->tx_queue_tail = pkt;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800697
Jeremy Kerrdf15f7e2019-08-05 15:41:19 +0800698 p += payload_len;
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800699 }
700
Andrew Jeffery298865f2020-02-06 11:51:29 +1030701 mctp_prdebug("%s: Enqueued %d packets", __func__, i);
702
Jeremy Kerrcc2458d2019-03-01 08:23:33 +0800703 mctp_send_tx_queue(bus);
Jeremy Kerr24db71f2019-02-07 21:37:35 +0800704
705 return 0;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800706}
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800707
708int mctp_message_tx(struct mctp *mctp, mctp_eid_t eid,
709 void *msg, size_t msg_len)
710{
711 struct mctp_bus *bus;
712
713 bus = find_bus_for_eid(mctp, eid);
Andrew Jefferyb93b6112020-06-05 14:13:44 +0930714 return mctp_message_tx_on_bus(bus, bus->eid, eid, msg, msg_len);
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800715}